blob: bd2be1b5b20a06a2681048bbacadbf4081e08e04 [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 if (s->conn_retries >= s->be->conn_retries)
Christopher Faulet552601d2021-05-26 10:31:06 +02001122 return -1;
Christopher Faulet41ade742023-03-01 15:45:39 +01001123 s->conn_retries++;
Christopher Faulete763c8c2021-05-05 18:23:59 +02001124 if (objt_server(s->target)) {
1125 if (s->flags & SF_CURR_SESS) {
1126 s->flags &= ~SF_CURR_SESS;
1127 _HA_ATOMIC_DEC(&__objt_server(s->target)->cur_sess);
1128 }
Willy Tarreau4781b152021-04-06 13:53:36 +02001129 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.retries);
Christopher Faulete763c8c2021-05-05 18:23:59 +02001130 }
Willy Tarreau4781b152021-04-06 13:53:36 +02001131 _HA_ATOMIC_INC(&s->be->be_counters.retries);
Willy Tarreau223995e2019-05-04 10:38:31 +02001132
Christopher Faulet9f5382e2021-05-21 13:46:14 +02001133 req = &s->req;
1134 res = &s->res;
Olivier Houcharda254a372019-04-05 15:30:12 +02001135 /* Remove any write error from the request, and read error from the response */
Christopher Faulet2e56a732023-01-26 16:18:09 +01001136 req->flags &= ~(CF_WRITE_TIMEOUT | CF_SHUTW | CF_SHUTW_NOW);
1137 res->flags &= ~(CF_READ_TIMEOUT | CF_SHUTR | CF_EOI | CF_READ_EVENT | CF_SHUTR_NOW);
Christopher Faulet7bf46bb2022-01-04 10:56:03 +01001138 res->analysers &= AN_RES_FLT_END;
Christopher Faulet50264b42022-03-30 19:39:30 +02001139 s->conn_err_type = STRM_ET_NONE;
Christopher Fauletae024ce2022-03-29 19:02:31 +02001140 s->flags &= ~(SF_CONN_EXP | SF_ERR_MASK | SF_FINST_MASK);
1141 s->conn_exp = TICK_ETERNITY;
Olivier Houchard4bd58672019-07-12 16:16:59 +02001142 stream_choose_redispatch(s);
Olivier Houcharda254a372019-04-05 15:30:12 +02001143 res->to_forward = 0;
1144 res->analyse_exp = TICK_ETERNITY;
1145 res->total = 0;
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +01001146
Willy Tarreau19c65a92022-05-27 08:49:24 +02001147 if (sc_reset_endp(s->scb) < 0) {
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +01001148 if (!(s->flags & SF_ERR_MASK))
1149 s->flags |= SF_ERR_INTERNAL;
1150 return -1;
1151 }
Olivier Houcharda254a372019-04-05 15:30:12 +02001152
Christopher Faulet9f5382e2021-05-21 13:46:14 +02001153 b_free(&req->buf);
1154 /* Swap the L7 buffer with the channel buffer */
1155 /* We know we stored the co_data as b_data, so get it there */
Christopher Faulete05bf9e2022-03-29 15:23:40 +02001156 co_data = b_data(&s->txn->l7_buffer);
1157 b_set_data(&s->txn->l7_buffer, b_size(&s->txn->l7_buffer));
1158 b_xfer(&req->buf, &s->txn->l7_buffer, b_data(&s->txn->l7_buffer));
Christopher Faulet9f5382e2021-05-21 13:46:14 +02001159 co_set_data(req, co_data);
Christopher Faulet5b82cc52020-10-12 15:18:50 +02001160
Ilya Shipitsinacf84592021-02-06 22:29:08 +05001161 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 +02001162
Olivier Houcharda254a372019-04-05 15:30:12 +02001163 b_reset(&res->buf);
1164 co_set_data(res, 0);
1165 return 0;
1166}
1167
Christopher Faulete0768eb2018-10-03 16:38:02 +02001168/* This stream analyser waits for a complete HTTP response. It returns 1 if the
1169 * processing can continue on next analysers, or zero if it either needs more
1170 * data or wants to immediately abort the response (eg: timeout, error, ...). It
1171 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->res.analysers
1172 * when it has nothing left to do, and may remove any analyser when it wants to
1173 * abort.
1174 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001175int http_wait_for_response(struct stream *s, struct channel *rep, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001176{
Christopher Faulet9768c262018-10-22 09:34:31 +02001177 /*
1178 * We will analyze a complete HTTP response to check the its syntax.
1179 *
1180 * Once the start line and all headers are received, we may perform a
1181 * capture of the error (if any), and we will set a few fields. We also
1182 * logging and finally headers capture.
1183 */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001184 struct session *sess = s->sess;
1185 struct http_txn *txn = s->txn;
1186 struct http_msg *msg = &txn->rsp;
Christopher Faulet9768c262018-10-22 09:34:31 +02001187 struct htx *htx;
Christopher Faulet61608322018-11-23 16:23:45 +01001188 struct connection *srv_conn;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001189 struct htx_sl *sl;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001190 int n;
1191
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001192 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001193
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001194 htx = htxbuf(&rep->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001195
Willy Tarreau4236f032019-03-05 10:43:32 +01001196 /* Parsing errors are caught here */
1197 if (htx->flags & HTX_FL_PARSING_ERROR)
1198 goto return_bad_res;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001199 if (htx->flags & HTX_FL_PROCESSING_ERROR)
1200 goto return_int_err;
Willy Tarreau4236f032019-03-05 10:43:32 +01001201
Christopher Faulete0768eb2018-10-03 16:38:02 +02001202 /*
1203 * Now we quickly check if we have found a full valid response.
1204 * If not so, we check the FD and buffer states before leaving.
1205 * A full response is indicated by the fact that we have seen
1206 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
1207 * responses are checked first.
1208 *
1209 * Depending on whether the client is still there or not, we
1210 * may send an error response back or not. Note that normally
1211 * we should only check for HTTP status there, and check I/O
1212 * errors somewhere else.
1213 */
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001214 next_one:
Christopher Faulet29f17582019-05-23 11:03:26 +02001215 if (unlikely(htx_is_empty(htx) || htx->first == -1)) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001216 /* 1: have we encountered a read error ? */
Christopher Faulet2e56a732023-01-26 16:18:09 +01001217 if (sc_ep_test(s->scb, SE_FL_ERROR)) {
Willy Tarreaufd9417b2022-05-18 16:23:22 +02001218 struct connection *conn = sc_conn(s->scb);
Olivier Houchard865d8392019-05-03 22:46:27 +02001219
Christopher Faulet6f78ac52023-03-01 15:47:18 +01001220
1221 if ((txn->flags & TX_L7_RETRY) &&
1222 (s->be->retry_type & PR_RE_DISCONNECTED) &&
1223 (!conn || conn->err_code != CO_ER_SSL_EARLY_FAILED)) {
1224 if (co_data(rep) || do_l7_retry(s, s->scb) == 0)
1225 return 0;
1226 }
1227
1228 /* Perform a L7 retry on empty response or because server refuses the early data. */
Christopher Faulete05bf9e2022-03-29 15:23:40 +02001229 if ((txn->flags & TX_L7_RETRY) &&
Christopher Fauletd9769232021-05-26 12:15:37 +02001230 (s->be->retry_type & PR_RE_EARLY_ERROR) &&
1231 conn && conn->err_code == CO_ER_SSL_EARLY_FAILED &&
Willy Tarreau7cb9e6c2022-05-17 19:40:40 +02001232 do_l7_retry(s, s->scb) == 0) {
Christopher Fauletd9769232021-05-26 12:15:37 +02001233 DBG_TRACE_DEVEL("leaving on L7 retry",
1234 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
1235 return 0;
1236 }
1237
Olivier Houchard6db16992019-05-17 15:40:49 +02001238 if (txn->flags & TX_NOT_FIRST)
1239 goto abort_keep_alive;
1240
Willy Tarreau4781b152021-04-06 13:53:36 +02001241 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001242 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001243 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001244 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001245 }
1246
Christopher Fauletd9769232021-05-26 12:15:37 +02001247 /* if the server refused the early data, just send a 425 */
1248 if (conn && conn->err_code == CO_ER_SSL_EARLY_FAILED)
Olivier Houchard865d8392019-05-03 22:46:27 +02001249 txn->status = 425;
Christopher Fauletd9769232021-05-26 12:15:37 +02001250 else {
1251 txn->status = 502;
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001252 stream_inc_http_fail_ctr(s);
Christopher Fauletd9769232021-05-26 12:15:37 +02001253 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001254
Willy Tarreaucb041662022-05-17 19:44:42 +02001255 s->scb->flags |= SC_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001256 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001257
1258 if (!(s->flags & SF_ERR_MASK))
1259 s->flags |= SF_ERR_SRVCL;
Christopher Faulet4a66c942023-01-13 09:43:21 +01001260 http_set_term_flags(s);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001261 DBG_TRACE_DEVEL("leaving on error",
1262 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001263 return 0;
1264 }
1265
Christopher Faulet9768c262018-10-22 09:34:31 +02001266 /* 2: read timeout : return a 504 to the client. */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001267 else if (rep->flags & CF_READ_TIMEOUT) {
Christopher Faulete05bf9e2022-03-29 15:23:40 +02001268 if ((txn->flags & TX_L7_RETRY) &&
Olivier Houcharda254a372019-04-05 15:30:12 +02001269 (s->be->retry_type & PR_RE_TIMEOUT)) {
Willy Tarreau7cb9e6c2022-05-17 19:40:40 +02001270 if (co_data(rep) || do_l7_retry(s, s->scb) == 0) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001271 DBG_TRACE_DEVEL("leaving on L7 retry",
1272 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharda254a372019-04-05 15:30:12 +02001273 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001274 }
Olivier Houcharda254a372019-04-05 15:30:12 +02001275 }
Willy Tarreau4781b152021-04-06 13:53:36 +02001276 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001277 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001278 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001279 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001280 }
1281
Christopher Faulete0768eb2018-10-03 16:38:02 +02001282 txn->status = 504;
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001283 stream_inc_http_fail_ctr(s);
Willy Tarreaucb041662022-05-17 19:44:42 +02001284 s->scb->flags |= SC_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001285 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001286
1287 if (!(s->flags & SF_ERR_MASK))
1288 s->flags |= SF_ERR_SRVTO;
Christopher Faulet4a66c942023-01-13 09:43:21 +01001289 http_set_term_flags(s);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001290 DBG_TRACE_DEVEL("leaving on error",
1291 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001292 return 0;
1293 }
1294
Christopher Faulet9768c262018-10-22 09:34:31 +02001295 /* 3: client abort with an abortonclose */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001296 else if ((rep->flags & CF_SHUTR) && ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001297 _HA_ATOMIC_INC(&sess->fe->fe_counters.cli_aborts);
1298 _HA_ATOMIC_INC(&s->be->be_counters.cli_aborts);
William Lallemand36119de2021-03-08 15:26:48 +01001299 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001300 _HA_ATOMIC_INC(&sess->listener->counters->cli_aborts);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001301 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001302 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.cli_aborts);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001303
Christopher Faulete0768eb2018-10-03 16:38:02 +02001304 txn->status = 400;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001305 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001306
1307 if (!(s->flags & SF_ERR_MASK))
1308 s->flags |= SF_ERR_CLICL;
Christopher Faulet4a66c942023-01-13 09:43:21 +01001309 http_set_term_flags(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001310
1311 /* process_stream() will take care of the error */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001312 DBG_TRACE_DEVEL("leaving on error",
1313 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001314 return 0;
1315 }
1316
Christopher Faulet9768c262018-10-22 09:34:31 +02001317 /* 4: close from server, capture the response if the server has started to respond */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001318 else if (rep->flags & CF_SHUTR) {
Christopher Faulete05bf9e2022-03-29 15:23:40 +02001319 if ((txn->flags & TX_L7_RETRY) &&
Olivier Houcharda254a372019-04-05 15:30:12 +02001320 (s->be->retry_type & PR_RE_DISCONNECTED)) {
Willy Tarreau7cb9e6c2022-05-17 19:40:40 +02001321 if (co_data(rep) || do_l7_retry(s, s->scb) == 0) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001322 DBG_TRACE_DEVEL("leaving on L7 retry",
1323 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharda254a372019-04-05 15:30:12 +02001324 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001325 }
Olivier Houcharda254a372019-04-05 15:30:12 +02001326 }
1327
Olivier Houchard6db16992019-05-17 15:40:49 +02001328 if (txn->flags & TX_NOT_FIRST)
1329 goto abort_keep_alive;
1330
Willy Tarreau4781b152021-04-06 13:53:36 +02001331 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001332 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001333 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001334 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001335 }
1336
Christopher Faulete0768eb2018-10-03 16:38:02 +02001337 txn->status = 502;
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001338 stream_inc_http_fail_ctr(s);
Willy Tarreaucb041662022-05-17 19:44:42 +02001339 s->scb->flags |= SC_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001340 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001341
1342 if (!(s->flags & SF_ERR_MASK))
1343 s->flags |= SF_ERR_SRVCL;
Christopher Faulet4a66c942023-01-13 09:43:21 +01001344 http_set_term_flags(s);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001345 DBG_TRACE_DEVEL("leaving on error",
1346 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001347 return 0;
1348 }
1349
Christopher Faulet9768c262018-10-22 09:34:31 +02001350 /* 5: write error to client (we don't send any message then) */
Christopher Faulet2e56a732023-01-26 16:18:09 +01001351 else if (sc_ep_test(s->scf, SE_FL_ERR_PENDING)) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001352 if (txn->flags & TX_NOT_FIRST)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001353 goto abort_keep_alive;
1354
Willy Tarreau4781b152021-04-06 13:53:36 +02001355 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001356 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001357 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001358 rep->analysers &= AN_RES_FLT_END;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001359
1360 if (!(s->flags & SF_ERR_MASK))
1361 s->flags |= SF_ERR_CLICL;
Christopher Faulet4a66c942023-01-13 09:43:21 +01001362 http_set_term_flags(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001363
1364 /* process_stream() will take care of the error */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001365 DBG_TRACE_DEVEL("leaving on error",
1366 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001367 return 0;
1368 }
1369
1370 channel_dont_close(rep);
1371 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001372 DBG_TRACE_DEVEL("waiting for more data",
1373 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001374 return 0;
1375 }
1376
1377 /* More interesting part now : we know that we have a complete
1378 * response which at least looks like HTTP. We have an indicator
1379 * of each header's length, so we can parse them quickly.
1380 */
Christopher Faulet29f17582019-05-23 11:03:26 +02001381 BUG_ON(htx_get_first_type(htx) != HTX_BLK_RES_SL);
Christopher Faulet297fbb42019-05-13 14:41:27 +02001382 sl = http_get_stline(htx);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001383
Christopher Faulet1f08bff2021-05-26 13:14:39 +02001384 /* Perform a L7 retry because of the status code */
Christopher Faulete05bf9e2022-03-29 15:23:40 +02001385 if ((txn->flags & TX_L7_RETRY) &&
Christopher Faulet1f08bff2021-05-26 13:14:39 +02001386 l7_status_match(s->be, sl->info.res.status) &&
Willy Tarreau7cb9e6c2022-05-17 19:40:40 +02001387 do_l7_retry(s, s->scb) == 0) {
Christopher Faulet1f08bff2021-05-26 13:14:39 +02001388 DBG_TRACE_DEVEL("leaving on L7 retry", STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
1389 return 0;
1390 }
1391
1392 /* Now, L7 buffer is useless, it can be released */
Christopher Faulete05bf9e2022-03-29 15:23:40 +02001393 b_free(&txn->l7_buffer);
Christopher Faulet1f08bff2021-05-26 13:14:39 +02001394
1395 msg->msg_state = HTTP_MSG_BODY;
1396
1397
Christopher Faulet9768c262018-10-22 09:34:31 +02001398 /* 0: we might have to print this header in debug mode */
1399 if (unlikely((global.mode & MODE_DEBUG) &&
1400 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
1401 int32_t pos;
1402
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001403 http_debug_stline("srvrep", s, sl);
Christopher Faulet9768c262018-10-22 09:34:31 +02001404
Christopher Fauleta3f15502019-05-13 15:27:23 +02001405 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001406 struct htx_blk *blk = htx_get_blk(htx, pos);
1407 enum htx_blk_type type = htx_get_blk_type(blk);
1408
1409 if (type == HTX_BLK_EOH)
1410 break;
1411 if (type != HTX_BLK_HDR)
1412 continue;
1413
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001414 http_debug_hdr("srvhdr", s,
1415 htx_get_blk_name(htx, blk),
1416 htx_get_blk_value(htx, blk));
Christopher Faulet9768c262018-10-22 09:34:31 +02001417 }
1418 }
1419
Christopher Faulet03599112018-11-27 11:21:21 +01001420 /* 1: get the status code and the version. Also set HTTP flags */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001421 txn->status = sl->info.res.status;
Christopher Faulet03599112018-11-27 11:21:21 +01001422 if (sl->flags & HTX_SL_F_VER_11)
Christopher Faulet9768c262018-10-22 09:34:31 +02001423 msg->flags |= HTTP_MSGF_VER_11;
Christopher Faulet03599112018-11-27 11:21:21 +01001424 if (sl->flags & HTX_SL_F_XFER_LEN) {
1425 msg->flags |= HTTP_MSGF_XFER_LEN;
Christopher Faulet2a408542020-11-20 14:22:37 +01001426 if (sl->flags & HTX_SL_F_CLEN)
1427 msg->flags |= HTTP_MSGF_CNT_LEN;
1428 else if (sl->flags & HTX_SL_F_CHNK)
1429 msg->flags |= HTTP_MSGF_TE_CHNK;
Christopher Faulet03599112018-11-27 11:21:21 +01001430 }
Christopher Faulet2a408542020-11-20 14:22:37 +01001431 if (sl->flags & HTX_SL_F_BODYLESS)
1432 msg->flags |= HTTP_MSGF_BODYLESS;
Christopher Faulet576c3582021-01-08 15:53:01 +01001433 if (sl->flags & HTX_SL_F_CONN_UPG)
1434 msg->flags |= HTTP_MSGF_CONN_UPG;
Christopher Faulet9768c262018-10-22 09:34:31 +02001435
1436 n = txn->status / 100;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001437 if (n < 1 || n > 5)
1438 n = 0;
Christopher Faulet9768c262018-10-22 09:34:31 +02001439
Christopher Faulete0768eb2018-10-03 16:38:02 +02001440 /* when the client triggers a 4xx from the server, it's most often due
1441 * to a missing object or permission. These events should be tracked
1442 * because if they happen often, it may indicate a brute force or a
1443 * vulnerability scan.
1444 */
1445 if (n == 4)
1446 stream_inc_http_err_ctr(s);
1447
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001448 if (n == 5 && txn->status != 501 && txn->status != 505)
1449 stream_inc_http_fail_ctr(s);
1450
Marcin Deranek3c27dda2020-05-15 18:32:51 +02001451 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001452 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.p.http.rsp[n]);
1453 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.p.http.cum_req);
Marcin Deranek3c27dda2020-05-15 18:32:51 +02001454 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001455
Christopher Faulete0768eb2018-10-03 16:38:02 +02001456 /* Adjust server's health based on status code. Note: status codes 501
1457 * and 505 are triggered on demand by client request, so we must not
1458 * count them as server failures.
1459 */
1460 if (objt_server(s->target)) {
1461 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001462 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_OK);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001463 else
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001464 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_STS);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001465 }
1466
1467 /*
1468 * We may be facing a 100-continue response, or any other informational
1469 * 1xx response which is non-final, in which case this is not the right
1470 * response, and we're waiting for the next one. Let's allow this response
1471 * to go to the client and wait for the next one. There's an exception for
1472 * 101 which is used later in the code to switch protocols.
1473 */
1474 if (txn->status < 200 &&
1475 (txn->status == 100 || txn->status >= 102)) {
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001476 FLT_STRM_CB(s, flt_http_reset(s, msg));
Christopher Faulet421e7692019-06-13 11:16:45 +02001477 htx->first = channel_htx_fwd_headers(rep, htx);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001478 msg->msg_state = HTTP_MSG_RPBEFORE;
Christopher Faulet3499f622019-09-03 15:23:54 +02001479 msg->flags = 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001480 txn->status = 0;
1481 s->logs.t_data = -1; /* was not a response yet */
Christopher Faulet7d518452020-08-31 11:07:07 +02001482 rep->flags |= CF_SEND_DONTWAIT; /* Send ASAP informational messages */
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001483 goto next_one;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001484 }
1485
Christopher Faulet6e6c7b12021-01-08 16:02:05 +01001486 /* A 101-switching-protocols must contains a Connection header with the
1487 * "upgrade" option and the request too. It means both are agree to
1488 * upgrade. It is not so strict because there is no test on the Upgrade
1489 * header content. But it is probably stronger enough for now.
1490 */
1491 if (txn->status == 101 &&
1492 (!(txn->req.flags & HTTP_MSGF_CONN_UPG) || !(txn->rsp.flags & HTTP_MSGF_CONN_UPG)))
1493 goto return_bad_res;
1494
Christopher Faulete0768eb2018-10-03 16:38:02 +02001495 /*
1496 * 2: check for cacheability.
1497 */
1498
1499 switch (txn->status) {
1500 case 200:
1501 case 203:
1502 case 204:
1503 case 206:
1504 case 300:
1505 case 301:
1506 case 404:
1507 case 405:
1508 case 410:
1509 case 414:
1510 case 501:
1511 break;
1512 default:
1513 /* RFC7231#6.1:
1514 * Responses with status codes that are defined as
1515 * cacheable by default (e.g., 200, 203, 204, 206,
1516 * 300, 301, 404, 405, 410, 414, and 501 in this
1517 * specification) can be reused by a cache with
1518 * heuristic expiration unless otherwise indicated
1519 * by the method definition or explicit cache
1520 * controls [RFC7234]; all other status codes are
1521 * not cacheable by default.
1522 */
1523 txn->flags &= ~(TX_CACHEABLE | TX_CACHE_COOK);
1524 break;
1525 }
1526
1527 /*
1528 * 3: we may need to capture headers
1529 */
1530 s->logs.logwait &= ~LW_RESP;
1531 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->res_cap))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001532 http_capture_headers(htx, s->res_cap, sess->fe->rsp_cap);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001533
Christopher Faulet9768c262018-10-22 09:34:31 +02001534 /* Skip parsing if no content length is possible. */
Christopher Fauletc75668e2020-12-07 18:10:32 +01001535 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status >= 200 && txn->status < 300) ||
Christopher Faulete0768eb2018-10-03 16:38:02 +02001536 txn->status == 101)) {
1537 /* Either we've established an explicit tunnel, or we're
1538 * switching the protocol. In both cases, we're very unlikely
1539 * to understand the next protocols. We have to switch to tunnel
1540 * mode, so that we transfer the request and responses then let
1541 * this protocol pass unmodified. When we later implement specific
1542 * parsers for such protocols, we'll want to check the Upgrade
1543 * header which contains information about that protocol for
1544 * responses with status 101 (eg: see RFC2817 about TLS).
1545 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02001546 txn->flags |= TX_CON_WANT_TUN;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001547 }
1548
Christopher Faulet61608322018-11-23 16:23:45 +01001549 /* check for NTML authentication headers in 401 (WWW-Authenticate) and
1550 * 407 (Proxy-Authenticate) responses and set the connection to private
1551 */
Willy Tarreaufd9417b2022-05-18 16:23:22 +02001552 srv_conn = sc_conn(s->scb);
Christopher Faulet61608322018-11-23 16:23:45 +01001553 if (srv_conn) {
1554 struct ist hdr;
1555 struct http_hdr_ctx ctx;
1556
1557 if (txn->status == 401)
1558 hdr = ist("WWW-Authenticate");
1559 else if (txn->status == 407)
1560 hdr = ist("Proxy-Authenticate");
1561 else
1562 goto end;
1563
1564 ctx.blk = NULL;
1565 while (http_find_header(htx, hdr, &ctx, 0)) {
Willy Tarreauf1dcced2020-05-07 19:27:02 +02001566 /* If www-authenticate contains "Negotiate", "Nego2", or "NTLM",
1567 * possibly followed by blanks and a base64 string, the connection
1568 * is private. Since it's a mess to deal with, we only check for
1569 * values starting with "NTLM" or "Nego". Note that often multiple
1570 * headers are sent by the server there.
1571 */
1572 if ((ctx.value.len >= 4 && strncasecmp(ctx.value.ptr, "Nego", 4) == 0) ||
Willy Tarreau49a1d282020-05-07 19:10:15 +02001573 (ctx.value.len >= 4 && strncasecmp(ctx.value.ptr, "NTLM", 4) == 0)) {
Olivier Houchard250031e2019-05-29 15:01:50 +02001574 sess->flags |= SESS_FL_PREFER_LAST;
Christopher Faulet08016ab2020-07-01 16:10:06 +02001575 conn_set_owner(srv_conn, sess, NULL);
Christopher Faulet21ddc742020-07-01 15:26:14 +02001576 conn_set_private(srv_conn);
Ilya Shipitsin6b79f382020-07-23 00:32:55 +05001577 /* If it fail now, the same will be done in mux->detach() callback */
Christopher Faulet08016ab2020-07-01 16:10:06 +02001578 session_add_conn(srv_conn->owner, srv_conn, srv_conn->target);
Willy Tarreauf1dcced2020-05-07 19:27:02 +02001579 break;
Olivier Houchard250031e2019-05-29 15:01:50 +02001580 }
Christopher Faulet61608322018-11-23 16:23:45 +01001581 }
1582 }
1583
1584 end:
Christopher Faulete0768eb2018-10-03 16:38:02 +02001585 /* we want to have the response time before we start processing it */
1586 s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now);
1587
1588 /* end of job, return OK */
1589 rep->analysers &= ~an_bit;
1590 rep->analyse_exp = TICK_ETERNITY;
1591 channel_auto_close(rep);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001592 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001593 return 1;
1594
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001595 return_int_err:
Willy Tarreau4781b152021-04-06 13:53:36 +02001596 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
1597 _HA_ATOMIC_INC(&s->be->be_counters.internal_errors);
William Lallemand36119de2021-03-08 15:26:48 +01001598 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001599 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Fauletb8a53712019-12-16 11:29:38 +01001600 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001601 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.internal_errors);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001602 txn->status = 500;
1603 if (!(s->flags & SF_ERR_MASK))
1604 s->flags |= SF_ERR_INTERNAL;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001605 goto return_prx_cond;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001606
1607 return_bad_res:
Willy Tarreau4781b152021-04-06 13:53:36 +02001608 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Faulet47365272018-10-31 17:40:50 +01001609 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001610 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001611 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Christopher Faulet47365272018-10-31 17:40:50 +01001612 }
Olivier Houcharde3249a92019-05-03 23:01:47 +02001613 if ((s->be->retry_type & PR_RE_JUNK_REQUEST) &&
Christopher Faulete05bf9e2022-03-29 15:23:40 +02001614 (txn->flags & TX_L7_RETRY) &&
Willy Tarreau7cb9e6c2022-05-17 19:40:40 +02001615 do_l7_retry(s, s->scb) == 0) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001616 DBG_TRACE_DEVEL("leaving on L7 retry",
1617 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharde3249a92019-05-03 23:01:47 +02001618 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001619 }
Christopher Faulet47365272018-10-31 17:40:50 +01001620 txn->status = 502;
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001621 stream_inc_http_fail_ctr(s);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001622 /* fall through */
1623
Christopher Fauletb8a53712019-12-16 11:29:38 +01001624 return_prx_cond:
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001625 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet4a66c942023-01-13 09:43:21 +01001626 http_set_term_flags(s);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001627
Willy Tarreaucb041662022-05-17 19:44:42 +02001628 s->scb->flags |= SC_FL_NOLINGER;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001629 DBG_TRACE_DEVEL("leaving on error",
1630 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulet47365272018-10-31 17:40:50 +01001631 return 0;
1632
Christopher Faulete0768eb2018-10-03 16:38:02 +02001633 abort_keep_alive:
1634 /* A keep-alive request to the server failed on a network error.
1635 * The client is required to retry. We need to close without returning
1636 * any other information so that the client retries.
1637 */
1638 txn->status = 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001639 s->logs.logwait = 0;
1640 s->logs.level = 0;
1641 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001642 http_reply_and_close(s, txn->status, NULL);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001643 DBG_TRACE_DEVEL("leaving by closing K/A connection",
1644 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001645 return 0;
1646}
1647
1648/* This function performs all the processing enabled for the current response.
1649 * It normally returns 1 unless it wants to break. It relies on buffers flags,
1650 * and updates s->res.analysers. It might make sense to explode it into several
1651 * other functions. It works like process_request (see indications above).
1652 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001653int http_process_res_common(struct stream *s, struct channel *rep, int an_bit, struct proxy *px)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001654{
1655 struct session *sess = s->sess;
1656 struct http_txn *txn = s->txn;
1657 struct http_msg *msg = &txn->rsp;
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001658 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001659 struct proxy *cur_proxy;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001660 enum rule_result ret = HTTP_RULE_RES_CONT;
1661
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001662 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
1663 return 0;
Christopher Faulet9768c262018-10-22 09:34:31 +02001664
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001665 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001666
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001667 htx = htxbuf(&rep->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001668
1669 /* The stats applet needs to adjust the Connection header but we don't
1670 * apply any filter there.
1671 */
1672 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
1673 rep->analysers &= ~an_bit;
1674 rep->analyse_exp = TICK_ETERNITY;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001675 goto end;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001676 }
1677
1678 /*
1679 * We will have to evaluate the filters.
1680 * As opposed to version 1.2, now they will be evaluated in the
1681 * filters order and not in the header order. This means that
1682 * each filter has to be validated among all headers.
1683 *
1684 * Filters are tried with ->be first, then with ->fe if it is
1685 * different from ->be.
1686 *
1687 * Maybe we are in resume condiion. In this case I choose the
1688 * "struct proxy" which contains the rule list matching the resume
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05001689 * pointer. If none of these "struct proxy" match, I initialise
Christopher Faulete0768eb2018-10-03 16:38:02 +02001690 * the process with the first one.
1691 *
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05001692 * In fact, I check only correspondence between the current list
Christopher Faulete0768eb2018-10-03 16:38:02 +02001693 * pointer and the ->fe rule list. If it doesn't match, I initialize
1694 * the loop with the ->be.
1695 */
Christopher Fauletd4150ad2021-10-13 15:35:55 +02001696 if (s->current_rule_list == &sess->fe->http_res_rules ||
1697 (sess->fe->defpx && s->current_rule_list == &sess->fe->defpx->http_res_rules))
Christopher Faulete0768eb2018-10-03 16:38:02 +02001698 cur_proxy = sess->fe;
1699 else
1700 cur_proxy = s->be;
Christopher Fauletd4150ad2021-10-13 15:35:55 +02001701
Christopher Faulete0768eb2018-10-03 16:38:02 +02001702 while (1) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001703 /* evaluate http-response rules */
Christopher Faulet46f46df2021-11-09 16:33:25 +01001704 if (ret == HTTP_RULE_RES_CONT || ret == HTTP_RULE_RES_STOP) {
Christopher Fauletd4150ad2021-10-13 15:35:55 +02001705 struct list *def_rules, *rules;
1706
1707 def_rules = ((cur_proxy->defpx && (cur_proxy == s->be || cur_proxy->defpx != s->be->defpx)) ? &cur_proxy->defpx->http_res_rules : NULL);
1708 rules = &cur_proxy->http_res_rules;
1709
1710 ret = http_res_get_intercept_rule(cur_proxy, def_rules, rules, s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001711
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001712 switch (ret) {
1713 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
1714 goto return_prx_yield;
1715
1716 case HTTP_RULE_RES_CONT:
1717 case HTTP_RULE_RES_STOP: /* nothing to do */
1718 break;
1719
1720 case HTTP_RULE_RES_DENY: /* deny or tarpit */
1721 goto deny;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001722
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001723 case HTTP_RULE_RES_ABRT: /* abort request, response already sent */
1724 goto return_prx_cond;
1725
1726 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
Christopher Fauletb8a53712019-12-16 11:29:38 +01001727 goto done;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001728
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001729 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
1730 goto return_bad_res;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001731
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001732 case HTTP_RULE_RES_ERROR: /* failed with a bad request */
1733 goto return_int_err;
1734 }
1735
1736 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001737
Christopher Faulete0768eb2018-10-03 16:38:02 +02001738 /* check whether we're already working on the frontend */
1739 if (cur_proxy == sess->fe)
1740 break;
1741 cur_proxy = sess->fe;
1742 }
1743
Christopher Faulete0768eb2018-10-03 16:38:02 +02001744 /* OK that's all we can do for 1xx responses */
1745 if (unlikely(txn->status < 200 && txn->status != 101))
Christopher Fauletf2824e62018-10-01 12:12:37 +02001746 goto end;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001747
1748 /*
1749 * Now check for a server cookie.
1750 */
1751 if (s->be->cookie_name || sess->fe->capture_name || (s->be->options & PR_O_CHK_CACHE))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001752 http_manage_server_side_cookies(s, rep);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001753
1754 /*
1755 * Check for cache-control or pragma headers if required.
1756 */
1757 if ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001758 http_check_response_for_cacheability(s, rep);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001759
1760 /*
1761 * Add server cookie in the response if needed
1762 */
1763 if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) &&
1764 !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) &&
1765 (!(s->flags & SF_DIRECT) ||
1766 ((s->be->cookie_maxidle || txn->cookie_last_date) &&
1767 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
1768 (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
1769 (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
1770 (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
1771 !(s->flags & SF_IGNORE_PRST)) {
1772 /* the server is known, it's not the one the client requested, or the
1773 * cookie's last seen date needs to be refreshed. We have to
1774 * insert a set-cookie here, except if we want to insert only on POST
1775 * requests and this one isn't. Note that servers which don't have cookies
1776 * (eg: some backup servers) will return a full cookie removal request.
1777 */
Willy Tarreau88bc8002021-12-06 07:01:02 +00001778 if (!__objt_server(s->target)->cookie) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001779 chunk_printf(&trash,
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001780 "%s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
Christopher Faulete0768eb2018-10-03 16:38:02 +02001781 s->be->cookie_name);
1782 }
1783 else {
Willy Tarreau88bc8002021-12-06 07:01:02 +00001784 chunk_printf(&trash, "%s=%s", s->be->cookie_name, __objt_server(s->target)->cookie);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001785
1786 if (s->be->cookie_maxidle || s->be->cookie_maxlife) {
1787 /* emit last_date, which is mandatory */
1788 trash.area[trash.data++] = COOKIE_DELIM_DATE;
1789 s30tob64((date.tv_sec+3) >> 2,
1790 trash.area + trash.data);
1791 trash.data += 5;
1792
1793 if (s->be->cookie_maxlife) {
1794 /* emit first_date, which is either the original one or
1795 * the current date.
1796 */
1797 trash.area[trash.data++] = COOKIE_DELIM_DATE;
1798 s30tob64(txn->cookie_first_date ?
1799 txn->cookie_first_date >> 2 :
1800 (date.tv_sec+3) >> 2,
1801 trash.area + trash.data);
1802 trash.data += 5;
1803 }
1804 }
1805 chunk_appendf(&trash, "; path=/");
1806 }
1807
1808 if (s->be->cookie_domain)
1809 chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain);
1810
1811 if (s->be->ck_opts & PR_CK_HTTPONLY)
1812 chunk_appendf(&trash, "; HttpOnly");
1813
1814 if (s->be->ck_opts & PR_CK_SECURE)
1815 chunk_appendf(&trash, "; Secure");
1816
Christopher Faulet2f533902020-01-21 11:06:48 +01001817 if (s->be->cookie_attrs)
1818 chunk_appendf(&trash, "; %s", s->be->cookie_attrs);
1819
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001820 if (unlikely(!http_add_header(htx, ist("Set-Cookie"), ist2(trash.area, trash.data))))
Christopher Fauletd649b572022-06-01 17:42:35 +02001821 goto return_fail_rewrite;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001822
1823 txn->flags &= ~TX_SCK_MASK;
1824 if (__objt_server(s->target)->cookie && (s->flags & SF_DIRECT))
1825 /* the server did not change, only the date was updated */
1826 txn->flags |= TX_SCK_UPDATED;
1827 else
1828 txn->flags |= TX_SCK_INSERTED;
1829
1830 /* Here, we will tell an eventual cache on the client side that we don't
1831 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
1832 * Some caches understand the correct form: 'no-cache="set-cookie"', but
1833 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
1834 */
1835 if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
1836
1837 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
1838
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001839 if (unlikely(!http_add_header(htx, ist("Cache-control"), ist("private"))))
Christopher Fauletd649b572022-06-01 17:42:35 +02001840 goto return_fail_rewrite;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001841 }
1842 }
1843
1844 /*
1845 * Check if result will be cacheable with a cookie.
1846 * We'll block the response if security checks have caught
1847 * nasty things such as a cacheable cookie.
1848 */
1849 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
1850 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
1851 (s->be->options & PR_O_CHK_CACHE)) {
1852 /* we're in presence of a cacheable response containing
1853 * a set-cookie header. We'll block it as requested by
1854 * the 'checkcache' option, and send an alert.
1855 */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001856 ha_alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreau88bc8002021-12-06 07:01:02 +00001857 s->be->id, objt_server(s->target) ? __objt_server(s->target)->id : "<dispatch>");
Christopher Faulete0768eb2018-10-03 16:38:02 +02001858 send_log(s->be, LOG_ALERT,
1859 "Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreau88bc8002021-12-06 07:01:02 +00001860 s->be->id, objt_server(s->target) ? __objt_server(s->target)->id : "<dispatch>");
Christopher Fauletb8a53712019-12-16 11:29:38 +01001861 goto deny;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001862 }
1863
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001864 end:
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01001865 /*
1866 * Evaluate after-response rules before forwarding the response. rules
1867 * from the backend are evaluated first, then one from the frontend if
1868 * it differs.
1869 */
1870 if (!http_eval_after_res_rules(s))
1871 goto return_int_err;
1872
Christopher Fauletc2ac5e42021-03-08 18:20:09 +01001873 /* Filter the response headers if there are filters attached to the
1874 * stream.
1875 */
1876 if (HAS_FILTERS(s))
1877 rep->analysers |= AN_RES_FLT_HTTP_HDRS;
1878
Christopher Faulete0768eb2018-10-03 16:38:02 +02001879 /* Always enter in the body analyzer */
1880 rep->analysers &= ~AN_RES_FLT_XFER_DATA;
1881 rep->analysers |= AN_RES_HTTP_XFER_BODY;
1882
1883 /* if the user wants to log as soon as possible, without counting
1884 * bytes from the server, then this is the right moment. We have
1885 * to temporarily assign bytes_out to log what we currently have.
1886 */
1887 if (!LIST_ISEMPTY(&sess->fe->logformat) && !(s->logs.logwait & LW_BYTES)) {
1888 s->logs.t_close = s->logs.t_data; /* to get a valid end date */
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001889 s->logs.bytes_out = htx->data;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001890 s->do_log(s);
1891 s->logs.bytes_out = 0;
1892 }
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001893
Christopher Fauletb8a53712019-12-16 11:29:38 +01001894 done:
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01001895 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletb8a53712019-12-16 11:29:38 +01001896 rep->analysers &= ~an_bit;
1897 rep->analyse_exp = TICK_ETERNITY;
Christopher Fauletd4150ad2021-10-13 15:35:55 +02001898 s->current_rule = s->current_rule_list = NULL;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001899 return 1;
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001900
Christopher Fauletb8a53712019-12-16 11:29:38 +01001901 deny:
Willy Tarreau4781b152021-04-06 13:53:36 +02001902 _HA_ATOMIC_INC(&sess->fe->fe_counters.denied_resp);
1903 _HA_ATOMIC_INC(&s->be->be_counters.denied_resp);
William Lallemand36119de2021-03-08 15:26:48 +01001904 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001905 _HA_ATOMIC_INC(&sess->listener->counters->denied_resp);
Christopher Fauleta08546b2019-12-16 16:07:34 +01001906 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001907 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.denied_resp);
Christopher Fauletb8a53712019-12-16 11:29:38 +01001908 goto return_prx_err;
1909
Christopher Fauletd649b572022-06-01 17:42:35 +02001910 return_fail_rewrite:
1911 if (!(s->flags & SF_ERR_MASK))
1912 s->flags |= SF_ERR_PRXCOND;
1913 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_rewrites);
1914 _HA_ATOMIC_INC(&s->be->be_counters.failed_rewrites);
1915 if (sess->listener && sess->listener->counters)
1916 _HA_ATOMIC_INC(&sess->listener->counters->failed_rewrites);
1917 if (objt_server(s->target))
1918 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_rewrites);
1919 /* fall through */
1920
Christopher Fauletb8a53712019-12-16 11:29:38 +01001921 return_int_err:
1922 txn->status = 500;
1923 if (!(s->flags & SF_ERR_MASK))
1924 s->flags |= SF_ERR_INTERNAL;
Willy Tarreau4781b152021-04-06 13:53:36 +02001925 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
1926 _HA_ATOMIC_INC(&s->be->be_counters.internal_errors);
Dragan Dosen9a006f92021-09-21 13:02:09 +02001927 if (sess->listener && sess->listener->counters)
1928 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001929 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001930 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.internal_errors);
Christopher Fauletb8a53712019-12-16 11:29:38 +01001931 goto return_prx_err;
1932
1933 return_bad_res:
1934 txn->status = 502;
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001935 stream_inc_http_fail_ctr(s);
Willy Tarreau4781b152021-04-06 13:53:36 +02001936 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Fauleta20a6532020-02-05 10:16:41 +01001937 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001938 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Christopher Fauleta20a6532020-02-05 10:16:41 +01001939 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_RSP);
1940 }
Christopher Fauletb8a53712019-12-16 11:29:38 +01001941 /* fall through */
1942
1943 return_prx_err:
1944 http_reply_and_close(s, txn->status, http_error_message(s));
1945 /* fall through */
1946
1947 return_prx_cond:
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001948 s->logs.t_data = -1; /* was not a valid response */
Willy Tarreaucb041662022-05-17 19:44:42 +02001949 s->scb->flags |= SC_FL_NOLINGER;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001950
Christopher Faulet4a66c942023-01-13 09:43:21 +01001951 http_set_term_flags(s);
Christopher Fauletb8a53712019-12-16 11:29:38 +01001952
Christopher Faulete58c0002020-03-02 16:21:01 +01001953 rep->analysers &= AN_RES_FLT_END;
1954 s->req.analysers &= AN_REQ_FLT_END;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001955 rep->analyse_exp = TICK_ETERNITY;
Christopher Fauletd4150ad2021-10-13 15:35:55 +02001956 s->current_rule = s->current_rule_list = NULL;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001957 DBG_TRACE_DEVEL("leaving on error",
1958 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001959 return 0;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001960
1961 return_prx_yield:
1962 channel_dont_close(rep);
1963 DBG_TRACE_DEVEL("waiting for more data",
1964 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
1965 return 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001966}
1967
1968/* This function is an analyser which forwards response body (including chunk
1969 * sizes if any). It is called as soon as we must forward, even if we forward
1970 * zero byte. The only situation where it must not be called is when we're in
1971 * tunnel mode and we want to forward till the close. It's used both to forward
1972 * remaining data and to resync after end of body. It expects the msg_state to
1973 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
1974 * read more data, or 1 once we can go on with next request or end the stream.
1975 *
1976 * It is capable of compressing response data both in content-length mode and
1977 * in chunked mode. The state machines follows different flows depending on
1978 * whether content-length and chunked modes are used, since there are no
1979 * trailers in content-length :
1980 *
1981 * chk-mode cl-mode
1982 * ,----- BODY -----.
1983 * / \
1984 * V size > 0 V chk-mode
1985 * .--> SIZE -------------> DATA -------------> CRLF
1986 * | | size == 0 | last byte |
1987 * | v final crlf v inspected |
1988 * | TRAILERS -----------> DONE |
1989 * | |
1990 * `----------------------------------------------'
1991 *
1992 * Compression only happens in the DATA state, and must be flushed in final
1993 * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding
1994 * is performed at once on final states for all bytes parsed, or when leaving
1995 * on missing data.
1996 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001997int http_response_forward_body(struct stream *s, struct channel *res, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001998{
1999 struct session *sess = s->sess;
2000 struct http_txn *txn = s->txn;
2001 struct http_msg *msg = &s->txn->rsp;
Christopher Faulet9768c262018-10-22 09:34:31 +02002002 struct htx *htx;
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002003 int ret;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002004
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002005 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002006
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002007 htx = htxbuf(&res->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002008
Christopher Fauletb9a92f32019-09-09 10:15:21 +02002009 if (htx->flags & HTX_FL_PARSING_ERROR)
2010 goto return_bad_res;
2011 if (htx->flags & HTX_FL_PROCESSING_ERROR)
2012 goto return_int_err;
2013
Christopher Faulet9768c262018-10-22 09:34:31 +02002014 if (msg->msg_state == HTTP_MSG_BODY)
2015 msg->msg_state = HTTP_MSG_DATA;
2016
Christopher Faulete0768eb2018-10-03 16:38:02 +02002017 /* in most states, we should abort in case of early close */
2018 channel_auto_close(res);
2019
Christopher Faulete0768eb2018-10-03 16:38:02 +02002020 if (res->to_forward) {
Christopher Faulet66af0b22019-03-22 14:54:52 +01002021 if (res->to_forward == CHN_INFINITE_FORWARD) {
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002022 if (res->flags & CF_EOI)
2023 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet66af0b22019-03-22 14:54:52 +01002024 }
2025 else {
2026 /* We can't process the buffer's contents yet */
2027 res->flags |= CF_WAKE_WRITE;
2028 goto missing_data_or_waiting;
2029 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02002030 }
2031
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002032 if (msg->msg_state >= HTTP_MSG_ENDING)
2033 goto ending;
2034
Christopher Fauletc75668e2020-12-07 18:10:32 +01002035 if ((txn->meth == HTTP_METH_CONNECT && txn->status >= 200 && txn->status < 300) || txn->status == 101 ||
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002036 (!(msg->flags & HTTP_MSGF_XFER_LEN) && !HAS_RSP_DATA_FILTERS(s))) {
2037 msg->msg_state = HTTP_MSG_ENDING;
2038 goto ending;
2039 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002040
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002041 /* Forward input data. We get it by removing all outgoing data not
2042 * forwarded yet from HTX data size. If there are some data filters, we
2043 * let them decide the amount of data to forward.
Christopher Faulet9768c262018-10-22 09:34:31 +02002044 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002045 if (HAS_RSP_DATA_FILTERS(s)) {
2046 ret = flt_http_payload(s, msg, htx->data);
2047 if (ret < 0)
2048 goto return_bad_res;
Christopher Faulet421e7692019-06-13 11:16:45 +02002049 c_adv(res, ret);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002050 }
2051 else {
Christopher Faulet421e7692019-06-13 11:16:45 +02002052 c_adv(res, htx->data - co_data(res));
Christopher Faulet2f7c82b2023-02-20 14:06:52 +01002053 if ((global.tune.options & GTUNE_USE_FAST_FWD) && (msg->flags & HTTP_MSGF_XFER_LEN))
Christopher Faulet66af0b22019-03-22 14:54:52 +01002054 channel_htx_forward_forever(res, htx);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002055 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002056
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002057 if (htx->data != co_data(res))
2058 goto missing_data_or_waiting;
2059
2060 if (!(msg->flags & HTTP_MSGF_XFER_LEN) && res->flags & CF_SHUTR) {
2061 msg->msg_state = HTTP_MSG_ENDING;
2062 goto ending;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002063 }
2064
Christopher Faulet9768c262018-10-22 09:34:31 +02002065 /* Check if the end-of-message is reached and if so, switch the message
Christopher Fauletd20fdb02019-06-13 16:43:22 +02002066 * in HTTP_MSG_ENDING state. Then if all data was marked to be
2067 * forwarded, set the state to HTTP_MSG_DONE.
Christopher Faulet9768c262018-10-22 09:34:31 +02002068 */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002069 if (!(htx->flags & HTX_FL_EOM))
Christopher Faulet9768c262018-10-22 09:34:31 +02002070 goto missing_data_or_waiting;
2071
Christopher Fauletd20fdb02019-06-13 16:43:22 +02002072 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet9768c262018-10-22 09:34:31 +02002073
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002074 ending:
Christopher Faulet2151cdd2020-07-22 16:34:59 +02002075 res->flags &= ~CF_EXPECT_MORE; /* no more data are expected */
2076
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002077 /* other states, ENDING...TUNNEL */
2078 if (msg->msg_state >= HTTP_MSG_DONE)
2079 goto done;
Christopher Faulet9768c262018-10-22 09:34:31 +02002080
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002081 if (HAS_RSP_DATA_FILTERS(s)) {
2082 ret = flt_http_end(s, msg);
2083 if (ret <= 0) {
2084 if (!ret)
2085 goto missing_data_or_waiting;
2086 goto return_bad_res;
2087 }
2088 }
2089
Christopher Fauletc75668e2020-12-07 18:10:32 +01002090 if ((txn->meth == HTTP_METH_CONNECT && txn->status >= 200 && txn->status < 300) || txn->status == 101 ||
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002091 !(msg->flags & HTTP_MSGF_XFER_LEN)) {
2092 msg->msg_state = HTTP_MSG_TUNNEL;
2093 goto ending;
2094 }
2095 else {
2096 msg->msg_state = HTTP_MSG_DONE;
2097 res->to_forward = 0;
2098 }
2099
2100 done:
2101
2102 channel_dont_close(res);
2103
Christopher Fauletf2b02cf2023-01-13 11:02:28 +01002104 if ((res->flags & CF_SHUTW) && co_data(res)) {
2105 /* response errors are most likely due to the client aborting
2106 * the transfer. */
2107 goto return_cli_abort;
2108 }
2109
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002110 http_end_response(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002111 if (!(res->analysers & an_bit)) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002112 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002113 return 1;
2114 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002115 DBG_TRACE_DEVEL("waiting for the end of the HTTP txn",
2116 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002117 return 0;
2118
2119 missing_data_or_waiting:
2120 if (res->flags & CF_SHUTW)
Christopher Faulet93e02d82019-03-08 14:18:50 +01002121 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002122
2123 /* stop waiting for data if the input is closed before the end. If the
2124 * client side was already closed, it means that the client has aborted,
2125 * so we don't want to count this as a server abort. Otherwise it's a
2126 * server abort.
2127 */
Christopher Fauletd20fdb02019-06-13 16:43:22 +02002128 if (msg->msg_state < HTTP_MSG_ENDING && res->flags & CF_SHUTR) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02002129 if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))
Christopher Faulet93e02d82019-03-08 14:18:50 +01002130 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002131 /* If we have some pending data, we continue the processing */
Christopher Faulet93e02d82019-03-08 14:18:50 +01002132 if (htx_is_empty(htx))
2133 goto return_srv_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002134 }
2135
Christopher Faulete0768eb2018-10-03 16:38:02 +02002136 /* When TE: chunked is used, we need to get there again to parse
2137 * remaining chunks even if the server has closed, so we don't want to
Christopher Faulet9768c262018-10-22 09:34:31 +02002138 * set CF_DONTCLOSE. Similarly when there is a content-leng or if there
2139 * are filters registered on the stream, we don't want to forward a
2140 * close
Christopher Faulete0768eb2018-10-03 16:38:02 +02002141 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002142 if ((msg->flags & HTTP_MSGF_XFER_LEN) || HAS_RSP_DATA_FILTERS(s))
Christopher Faulete0768eb2018-10-03 16:38:02 +02002143 channel_dont_close(res);
2144
2145 /* We know that more data are expected, but we couldn't send more that
2146 * what we did. So we always set the CF_EXPECT_MORE flag so that the
2147 * system knows it must not set a PUSH on this first part. Interactive
2148 * modes are already handled by the stream sock layer. We must not do
2149 * this in content-length mode because it could present the MSG_MORE
2150 * flag with the last block of forwarded data, which would cause an
2151 * additional delay to be observed by the receiver.
2152 */
Christopher Faulet2151cdd2020-07-22 16:34:59 +02002153 if (HAS_RSP_DATA_FILTERS(s))
Christopher Faulete0768eb2018-10-03 16:38:02 +02002154 res->flags |= CF_EXPECT_MORE;
2155
2156 /* the stream handler will take care of timeouts and errors */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002157 DBG_TRACE_DEVEL("waiting for more data to forward",
2158 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002159 return 0;
2160
Christopher Faulet93e02d82019-03-08 14:18:50 +01002161 return_srv_abort:
Willy Tarreau4781b152021-04-06 13:53:36 +02002162 _HA_ATOMIC_INC(&sess->fe->fe_counters.srv_aborts);
2163 _HA_ATOMIC_INC(&s->be->be_counters.srv_aborts);
William Lallemand36119de2021-03-08 15:26:48 +01002164 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02002165 _HA_ATOMIC_INC(&sess->listener->counters->srv_aborts);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002166 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02002167 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.srv_aborts);
Willy Tarreau826f3ab2021-02-10 12:07:15 +01002168 stream_inc_http_fail_ctr(s);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002169 if (!(s->flags & SF_ERR_MASK))
Christopher Fauletf2b02cf2023-01-13 11:02:28 +01002170 s->flags |= ((res->flags & CF_READ_TIMEOUT) ? SF_ERR_SRVTO : SF_ERR_SRVCL);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002171 goto return_error;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002172
Christopher Faulet93e02d82019-03-08 14:18:50 +01002173 return_cli_abort:
Willy Tarreau4781b152021-04-06 13:53:36 +02002174 _HA_ATOMIC_INC(&sess->fe->fe_counters.cli_aborts);
2175 _HA_ATOMIC_INC(&s->be->be_counters.cli_aborts);
William Lallemand36119de2021-03-08 15:26:48 +01002176 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02002177 _HA_ATOMIC_INC(&sess->listener->counters->cli_aborts);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002178 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02002179 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.cli_aborts);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002180 if (!(s->flags & SF_ERR_MASK))
Christopher Fauletf2b02cf2023-01-13 11:02:28 +01002181 s->flags |= ((res->flags & CF_WRITE_TIMEOUT) ? SF_ERR_CLITO : SF_ERR_CLICL);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002182 goto return_error;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002183
Christopher Fauletb9a92f32019-09-09 10:15:21 +02002184 return_int_err:
Willy Tarreau4781b152021-04-06 13:53:36 +02002185 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
2186 _HA_ATOMIC_INC(&s->be->be_counters.internal_errors);
William Lallemand36119de2021-03-08 15:26:48 +01002187 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02002188 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002189 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02002190 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.internal_errors);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02002191 if (!(s->flags & SF_ERR_MASK))
2192 s->flags |= SF_ERR_INTERNAL;
2193 goto return_error;
2194
Christopher Faulet93e02d82019-03-08 14:18:50 +01002195 return_bad_res:
Willy Tarreau4781b152021-04-06 13:53:36 +02002196 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002197 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02002198 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002199 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_RSP);
2200 }
Willy Tarreau826f3ab2021-02-10 12:07:15 +01002201 stream_inc_http_fail_ctr(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002202 if (!(s->flags & SF_ERR_MASK))
Christopher Faulet93e02d82019-03-08 14:18:50 +01002203 s->flags |= SF_ERR_SRVCL;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002204 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +02002205
Christopher Faulet93e02d82019-03-08 14:18:50 +01002206 return_error:
Christopher Faulete0768eb2018-10-03 16:38:02 +02002207 /* don't send any error message as we're in the body */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002208 http_reply_and_close(s, txn->status, NULL);
Christopher Fauletf2b02cf2023-01-13 11:02:28 +01002209 http_set_term_flags(s);
2210 stream_inc_http_fail_ctr(s);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002211 DBG_TRACE_DEVEL("leaving on error",
2212 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002213 return 0;
2214}
2215
Christopher Fauletf2824e62018-10-01 12:12:37 +02002216/* Perform an HTTP redirect based on the information in <rule>. The function
Willy Tarreaubc1223b2021-09-02 16:54:33 +02002217 * returns zero in case of an irrecoverable error such as too large a request
2218 * to build a valid response, 1 in case of successful redirect (hence the rule
2219 * is final), or 2 if the rule has to be silently skipped.
Christopher Fauletf2824e62018-10-01 12:12:37 +02002220 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002221int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn)
Christopher Fauletf2824e62018-10-01 12:12:37 +02002222{
Christopher Faulet99daf282018-11-28 22:58:13 +01002223 struct channel *req = &s->req;
2224 struct channel *res = &s->res;
2225 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01002226 struct htx_sl *sl;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002227 struct buffer *chunk;
Christopher Faulet99daf282018-11-28 22:58:13 +01002228 struct ist status, reason, location;
2229 unsigned int flags;
Christopher Fauleteab17572022-04-26 20:34:38 +02002230 int ret = 1, close = 0; /* Try to keep the connection alive byt default */
Christopher Fauletf2824e62018-10-01 12:12:37 +02002231
2232 chunk = alloc_trash_chunk();
Christopher Fauletb8a53712019-12-16 11:29:38 +01002233 if (!chunk) {
2234 if (!(s->flags & SF_ERR_MASK))
2235 s->flags |= SF_ERR_RESOURCE;
Christopher Faulet99daf282018-11-28 22:58:13 +01002236 goto fail;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002237 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002238
Christopher Faulet99daf282018-11-28 22:58:13 +01002239 /*
2240 * Create the location
2241 */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002242 htx = htxbuf(&req->buf);
Christopher Fauletf2824e62018-10-01 12:12:37 +02002243 switch(rule->type) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002244 case REDIRECT_TYPE_SCHEME: {
2245 struct http_hdr_ctx ctx;
2246 struct ist path, host;
Amaury Denoyellec453f952021-07-06 11:40:12 +02002247 struct http_uri_parser parser;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002248
Christopher Faulet99daf282018-11-28 22:58:13 +01002249 host = ist("");
2250 ctx.blk = NULL;
2251 if (http_find_header(htx, ist("Host"), &ctx, 0))
2252 host = ctx.value;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002253
Christopher Faulet297fbb42019-05-13 14:41:27 +02002254 sl = http_get_stline(htx);
Amaury Denoyellec453f952021-07-06 11:40:12 +02002255 parser = http_uri_parser_init(htx_sl_req_uri(sl));
2256 path = http_parse_path(&parser);
Christopher Faulet99daf282018-11-28 22:58:13 +01002257 /* build message using path */
Tim Duesterhused526372020-03-05 17:56:33 +01002258 if (isttest(path)) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002259 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2260 int qs = 0;
2261 while (qs < path.len) {
2262 if (*(path.ptr + qs) == '?') {
2263 path.len = qs;
2264 break;
2265 }
2266 qs++;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002267 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002268 }
2269 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002270 else
2271 path = ist("/");
Christopher Fauletf2824e62018-10-01 12:12:37 +02002272
Christopher Faulet99daf282018-11-28 22:58:13 +01002273 if (rule->rdr_str) { /* this is an old "redirect" rule */
2274 /* add scheme */
2275 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2276 goto fail;
2277 }
2278 else {
2279 /* add scheme with executing log format */
2280 chunk->data += build_logline(s, chunk->area + chunk->data,
2281 chunk->size - chunk->data,
2282 &rule->rdr_fmt);
2283 }
2284 /* add "://" + host + path */
2285 if (!chunk_memcat(chunk, "://", 3) ||
2286 !chunk_memcat(chunk, host.ptr, host.len) ||
2287 !chunk_memcat(chunk, path.ptr, path.len))
2288 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002289
Christopher Faulet99daf282018-11-28 22:58:13 +01002290 /* append a slash at the end of the location if needed and missing */
2291 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
2292 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
2293 if (chunk->data + 1 >= chunk->size)
2294 goto fail;
2295 chunk->area[chunk->data++] = '/';
2296 }
2297 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002298 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002299
Christopher Faulet99daf282018-11-28 22:58:13 +01002300 case REDIRECT_TYPE_PREFIX: {
2301 struct ist path;
Amaury Denoyellec453f952021-07-06 11:40:12 +02002302 struct http_uri_parser parser;
Christopher Faulet99daf282018-11-28 22:58:13 +01002303
Christopher Faulet297fbb42019-05-13 14:41:27 +02002304 sl = http_get_stline(htx);
Amaury Denoyellec453f952021-07-06 11:40:12 +02002305 parser = http_uri_parser_init(htx_sl_req_uri(sl));
2306 path = http_parse_path(&parser);
Christopher Faulet99daf282018-11-28 22:58:13 +01002307 /* build message using path */
Tim Duesterhused526372020-03-05 17:56:33 +01002308 if (isttest(path)) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002309 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2310 int qs = 0;
2311 while (qs < path.len) {
2312 if (*(path.ptr + qs) == '?') {
2313 path.len = qs;
2314 break;
2315 }
2316 qs++;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002317 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002318 }
2319 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002320 else
2321 path = ist("/");
Christopher Fauletf2824e62018-10-01 12:12:37 +02002322
Christopher Faulet99daf282018-11-28 22:58:13 +01002323 if (rule->rdr_str) { /* this is an old "redirect" rule */
2324 /* add prefix. Note that if prefix == "/", we don't want to
2325 * add anything, otherwise it makes it hard for the user to
2326 * configure a self-redirection.
2327 */
2328 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
2329 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2330 goto fail;
2331 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002332 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002333 else {
2334 /* add prefix with executing log format */
2335 chunk->data += build_logline(s, chunk->area + chunk->data,
2336 chunk->size - chunk->data,
2337 &rule->rdr_fmt);
2338 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002339
Christopher Faulet99daf282018-11-28 22:58:13 +01002340 /* add path */
2341 if (!chunk_memcat(chunk, path.ptr, path.len))
2342 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002343
Christopher Faulet99daf282018-11-28 22:58:13 +01002344 /* append a slash at the end of the location if needed and missing */
2345 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
2346 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
2347 if (chunk->data + 1 >= chunk->size)
2348 goto fail;
2349 chunk->area[chunk->data++] = '/';
2350 }
2351 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002352 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002353 case REDIRECT_TYPE_LOCATION:
2354 default:
2355 if (rule->rdr_str) { /* this is an old "redirect" rule */
2356 /* add location */
2357 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2358 goto fail;
2359 }
2360 else {
2361 /* add location with executing log format */
Willy Tarreaubc1223b2021-09-02 16:54:33 +02002362 int len = build_logline(s, chunk->area + chunk->data,
2363 chunk->size - chunk->data,
2364 &rule->rdr_fmt);
Christopher Fauleteab17572022-04-26 20:34:38 +02002365 if (!len && rule->flags & REDIRECT_FLAG_IGNORE_EMPTY) {
2366 ret = 2;
2367 goto out;
2368 }
Willy Tarreaubc1223b2021-09-02 16:54:33 +02002369
2370 chunk->data += len;
Christopher Faulet99daf282018-11-28 22:58:13 +01002371 }
2372 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002373 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002374 location = ist2(chunk->area, chunk->data);
2375
2376 /*
2377 * Create the 30x response
2378 */
2379 switch (rule->code) {
2380 case 308:
2381 status = ist("308");
2382 reason = ist("Permanent Redirect");
2383 break;
2384 case 307:
2385 status = ist("307");
2386 reason = ist("Temporary Redirect");
2387 break;
2388 case 303:
2389 status = ist("303");
2390 reason = ist("See Other");
2391 break;
2392 case 301:
2393 status = ist("301");
2394 reason = ist("Moved Permanently");
2395 break;
2396 case 302:
2397 default:
2398 status = ist("302");
2399 reason = ist("Found");
2400 break;
2401 }
2402
Christopher Faulet08e66462019-05-23 16:44:59 +02002403 if (!(txn->req.flags & HTTP_MSGF_BODYLESS) && txn->req.msg_state != HTTP_MSG_DONE)
2404 close = 1;
2405
Christopher Faulet99daf282018-11-28 22:58:13 +01002406 htx = htx_from_buf(&res->buf);
Kevin Zhu96b36392020-01-07 09:42:55 +01002407 /* Trim any possible response */
2408 channel_htx_truncate(&s->res, htx);
Christopher Faulet99daf282018-11-28 22:58:13 +01002409 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
2410 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), status, reason);
2411 if (!sl)
2412 goto fail;
2413 sl->info.res.status = rule->code;
2414 s->txn->status = rule->code;
2415
Christopher Faulet08e66462019-05-23 16:44:59 +02002416 if (close && !htx_add_header(htx, ist("Connection"), ist("close")))
2417 goto fail;
2418
2419 if (!htx_add_header(htx, ist("Content-length"), ist("0")) ||
Christopher Faulet99daf282018-11-28 22:58:13 +01002420 !htx_add_header(htx, ist("Location"), location))
2421 goto fail;
2422
2423 if (rule->code == 302 || rule->code == 303 || rule->code == 307) {
2424 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")))
2425 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002426 }
2427
2428 if (rule->cookie_len) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002429 if (!htx_add_header(htx, ist("Set-Cookie"), ist2(rule->cookie_str, rule->cookie_len)))
2430 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002431 }
2432
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002433 if (!htx_add_endof(htx, HTX_BLK_EOH))
Christopher Faulet99daf282018-11-28 22:58:13 +01002434 goto fail;
2435
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002436 htx->flags |= HTX_FL_EOM;
Kevin Zhu96b36392020-01-07 09:42:55 +01002437 htx_to_buf(htx, &res->buf);
Christopher Fauleta72a7e42020-01-28 09:28:11 +01002438 if (!http_forward_proxy_resp(s, 1))
2439 goto fail;
Christopher Faulet99daf282018-11-28 22:58:13 +01002440
Christopher Faulet60b33a52020-01-28 09:18:10 +01002441 if (rule->flags & REDIRECT_FLAG_FROM_REQ) {
2442 /* let's log the request time */
2443 s->logs.tv_request = now;
Christopher Fauletd3475882021-10-04 14:16:46 +02002444 req->analysers &= AN_REQ_FLT_END;
Christopher Faulet99daf282018-11-28 22:58:13 +01002445
Christopher Faulet60b33a52020-01-28 09:18:10 +01002446 if (s->sess->fe == s->be) /* report it if the request was intercepted by the frontend */
Willy Tarreau4781b152021-04-06 13:53:36 +02002447 _HA_ATOMIC_INC(&s->sess->fe->fe_counters.intercepted_req);
Christopher Faulet60b33a52020-01-28 09:18:10 +01002448 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002449
2450 if (!(s->flags & SF_ERR_MASK))
2451 s->flags |= SF_ERR_LOCAL;
Christopher Faulet4a66c942023-01-13 09:43:21 +01002452 http_set_term_flags(s);
Christopher Fauletf2824e62018-10-01 12:12:37 +02002453
Christopher Fauleteab17572022-04-26 20:34:38 +02002454 out:
Christopher Faulet99daf282018-11-28 22:58:13 +01002455 free_trash_chunk(chunk);
Christopher Fauleteab17572022-04-26 20:34:38 +02002456 return ret;
Christopher Faulet99daf282018-11-28 22:58:13 +01002457
2458 fail:
2459 /* If an error occurred, remove the incomplete HTTP response from the
2460 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01002461 channel_htx_truncate(res, htxbuf(&res->buf));
Christopher Fauleteab17572022-04-26 20:34:38 +02002462 ret = 0;
2463 goto out;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002464}
2465
Christopher Faulet18c13d32022-05-16 11:43:10 +02002466/* This function filters the request header names to only allow [0-9a-zA-Z-]
2467 * characters. Depending on the proxy configuration, headers with a name not
2468 * matching this charset are removed or the request is rejected with a
2469 * 403-Forbidden response if such name are found. It returns HTTP_RULE_RES_CONT
2470 * to continue the request processing or HTTP_RULE_RES_DENY if the request is
2471 * rejected.
2472 */
2473static enum rule_result http_req_restrict_header_names(struct stream *s, struct htx *htx, struct proxy *px)
2474{
2475 struct htx_blk *blk;
2476 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
2477
2478 blk = htx_get_first_blk(htx);
2479 while (blk) {
2480 enum htx_blk_type type = htx_get_blk_type(blk);
2481
2482 if (type == HTX_BLK_HDR) {
2483 struct ist n = htx_get_blk_name(htx, blk);
Mateusz Malek4b85a962022-08-17 14:22:09 +02002484 int i, end = istlen(n);
Christopher Faulet18c13d32022-05-16 11:43:10 +02002485
Mateusz Malek4b85a962022-08-17 14:22:09 +02002486 for (i = 0; i < end; i++) {
Christopher Faulet18c13d32022-05-16 11:43:10 +02002487 if (!isalnum((unsigned char)n.ptr[i]) && n.ptr[i] != '-') {
Mateusz Malek4b85a962022-08-17 14:22:09 +02002488 break;
Christopher Faulet18c13d32022-05-16 11:43:10 +02002489 }
2490 }
Mateusz Malek4b85a962022-08-17 14:22:09 +02002491
2492 if (i < end) {
2493 /* Disallowed character found - block the request or remove the header */
2494 if (px->options2 & PR_O2_RSTRICT_REQ_HDR_NAMES_BLK)
2495 goto block;
2496 blk = htx_remove_blk(htx, blk);
2497 continue;
2498 }
Christopher Faulet18c13d32022-05-16 11:43:10 +02002499 }
2500 if (type == HTX_BLK_EOH)
2501 break;
2502
2503 blk = htx_get_next_blk(htx, blk);
2504 }
2505 out:
2506 return rule_ret;
2507 block:
2508 /* Block the request returning a 403-Forbidden response */
2509 s->txn->status = 403;
2510 rule_ret = HTTP_RULE_RES_DENY;
2511 goto out;
2512}
2513
Christopher Faulet92d34fe2019-12-17 09:20:34 +01002514/* Replace all headers matching the name <name>. The header value is replaced if
2515 * it matches the regex <re>. <str> is used for the replacement. If <full> is
2516 * set to 1, the full-line is matched and replaced. Otherwise, comma-separated
2517 * values are evaluated one by one. It returns 0 on success and -1 on error.
2518 */
2519int http_replace_hdrs(struct stream* s, struct htx *htx, struct ist name,
2520 const char *str, struct my_regex *re, int full)
Christopher Faulet72333522018-10-24 11:25:02 +02002521{
2522 struct http_hdr_ctx ctx;
2523 struct buffer *output = get_trash_chunk();
2524
Christopher Faulet72333522018-10-24 11:25:02 +02002525 ctx.blk = NULL;
Christopher Faulet92d34fe2019-12-17 09:20:34 +01002526 while (http_find_header(htx, name, &ctx, full)) {
Christopher Faulet72333522018-10-24 11:25:02 +02002527 if (!regex_exec_match2(re, ctx.value.ptr, ctx.value.len, MAX_MATCH, pmatch, 0))
2528 continue;
2529
2530 output->data = exp_replace(output->area, output->size, ctx.value.ptr, str, pmatch);
2531 if (output->data == -1)
2532 return -1;
2533 if (!http_replace_header_value(htx, &ctx, ist2(output->area, output->data)))
2534 return -1;
2535 }
2536 return 0;
2537}
2538
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002539/* This function executes one of the set-{method,path,query,uri} actions. It
2540 * takes the string from the variable 'replace' with length 'len', then modifies
2541 * the relevant part of the request line accordingly. Then it updates various
2542 * pointers to the next elements which were moved, and the total buffer length.
2543 * It finds the action to be performed in p[2], previously filled by function
2544 * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal
2545 * error, though this can be revisited when this code is finally exploited.
2546 *
2547 * 'action' can be '0' to replace method, '1' to replace path, '2' to replace
Christopher Faulet312294f2020-09-02 17:17:44 +02002548 * query string, 3 to replace uri or 4 to replace the path+query.
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002549 *
2550 * In query string case, the mark question '?' must be set at the start of the
2551 * string by the caller, event if the replacement query string is empty.
2552 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002553int http_req_replace_stline(int action, const char *replace, int len,
2554 struct proxy *px, struct stream *s)
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002555{
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002556 struct htx *htx = htxbuf(&s->req.buf);
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002557
2558 switch (action) {
2559 case 0: // method
2560 if (!http_replace_req_meth(htx, ist2(replace, len)))
2561 return -1;
2562 break;
2563
2564 case 1: // path
Christopher Fauletb8ce5052020-08-31 16:11:57 +02002565 if (!http_replace_req_path(htx, ist2(replace, len), 0))
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002566 return -1;
2567 break;
2568
2569 case 2: // query
2570 if (!http_replace_req_query(htx, ist2(replace, len)))
2571 return -1;
2572 break;
2573
2574 case 3: // uri
2575 if (!http_replace_req_uri(htx, ist2(replace, len)))
2576 return -1;
2577 break;
2578
Christopher Faulet312294f2020-09-02 17:17:44 +02002579 case 4: // path + query
2580 if (!http_replace_req_path(htx, ist2(replace, len), 1))
2581 return -1;
2582 break;
2583
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002584 default:
2585 return -1;
2586 }
2587 return 0;
2588}
2589
2590/* This function replace the HTTP status code and the associated message. The
Christopher Faulete00d06c2019-12-16 17:18:42 +01002591 * variable <status> contains the new status code. This function never fails. It
2592 * returns 0 in case of success, -1 in case of internal error.
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002593 */
Christopher Faulet96bff762019-12-17 13:46:18 +01002594int http_res_set_status(unsigned int status, struct ist reason, struct stream *s)
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002595{
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002596 struct htx *htx = htxbuf(&s->res.buf);
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002597 char *res;
2598
2599 chunk_reset(&trash);
2600 res = ultoa_o(status, trash.area, trash.size);
2601 trash.data = res - trash.area;
2602
2603 /* Do we have a custom reason format string? */
Tim Duesterhuse296d3e2020-03-05 17:56:31 +01002604 if (!isttest(reason)) {
Christopher Faulet96bff762019-12-17 13:46:18 +01002605 const char *str = http_get_reason(status);
Tim Duesterhusdcf753a2021-03-04 17:31:47 +01002606 reason = ist(str);
Christopher Faulet96bff762019-12-17 13:46:18 +01002607 }
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002608
Christopher Fauletbde2c4c2020-08-31 16:43:34 +02002609 if (!http_replace_res_status(htx, ist2(trash.area, trash.data), reason))
Christopher Faulete00d06c2019-12-16 17:18:42 +01002610 return -1;
Willy Tarreau640e2532023-01-10 14:50:44 +01002611 s->txn->status = status;
Christopher Faulete00d06c2019-12-16 17:18:42 +01002612 return 0;
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002613}
2614
Christopher Faulet3e964192018-10-24 11:39:23 +02002615/* Executes the http-request rules <rules> for stream <s>, proxy <px> and
2616 * transaction <txn>. Returns the verdict of the first rule that prevents
2617 * further processing of the request (auth, deny, ...), and defaults to
2618 * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or
2619 * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT
2620 * on txn->flags if it encounters a tarpit rule. If <deny_status> is not NULL
2621 * and a deny/tarpit rule is matched, it will be filled with this rule's deny
2622 * status.
2623 */
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002624static enum rule_result http_req_get_intercept_rule(struct proxy *px, struct list *def_rules,
2625 struct list *rules, struct stream *s)
Christopher Faulet3e964192018-10-24 11:39:23 +02002626{
2627 struct session *sess = strm_sess(s);
2628 struct http_txn *txn = s->txn;
Christopher Faulet3e964192018-10-24 11:39:23 +02002629 struct act_rule *rule;
Christopher Faulet3e964192018-10-24 11:39:23 +02002630 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002631 int act_opts = 0;
Christopher Faulet3e964192018-10-24 11:39:23 +02002632
Christopher Faulet3e964192018-10-24 11:39:23 +02002633 /* If "the current_rule_list" match the executed rule list, we are in
2634 * resume condition. If a resume is needed it is always in the action
2635 * and never in the ACL or converters. In this case, we initialise the
2636 * current rule, and go to the action execution point.
2637 */
2638 if (s->current_rule) {
2639 rule = s->current_rule;
2640 s->current_rule = NULL;
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002641 if (s->current_rule_list == rules || (def_rules && s->current_rule_list == def_rules))
Christopher Faulet3e964192018-10-24 11:39:23 +02002642 goto resume_execution;
2643 }
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002644 s->current_rule_list = ((!def_rules || s->current_rule_list == def_rules) ? rules : def_rules);
Christopher Faulet3e964192018-10-24 11:39:23 +02002645
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002646 restart:
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002647 /* start the ruleset evaluation in strict mode */
2648 txn->req.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01002649
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002650 list_for_each_entry(rule, s->current_rule_list, list) {
Christopher Faulet3e964192018-10-24 11:39:23 +02002651 /* check optional condition */
2652 if (rule->cond) {
2653 int ret;
2654
2655 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
2656 ret = acl_pass(ret);
2657
2658 if (rule->cond->pol == ACL_COND_UNLESS)
2659 ret = !ret;
2660
2661 if (!ret) /* condition not matched */
2662 continue;
2663 }
2664
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002665 act_opts |= ACT_OPT_FIRST;
Christopher Faulet3e964192018-10-24 11:39:23 +02002666 resume_execution:
Amaury Denoyelle03517732021-05-07 14:25:01 +02002667 if (rule->kw->flags & KWF_EXPERIMENTAL)
2668 mark_tainted(TAINTED_ACTION_EXP_EXECUTED);
2669
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002670 /* Always call the action function if defined */
2671 if (rule->action_ptr) {
Christopher Faulet2e56a732023-01-26 16:18:09 +01002672 if (sc_ep_test(s->scf, SE_FL_ERROR) ||
Christopher Faulet446d8032022-12-12 07:53:18 +01002673 ((s->req.flags & CF_SHUTR) &&
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002674 (px->options & PR_O_ABRT_CLOSE)))
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002675 act_opts |= ACT_OPT_FINAL;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002676
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002677 switch (rule->action_ptr(rule, px, sess, s, act_opts)) {
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002678 case ACT_RET_CONT:
2679 break;
2680 case ACT_RET_STOP:
2681 rule_ret = HTTP_RULE_RES_STOP;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002682 s->last_rule_file = rule->conf.file;
2683 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002684 goto end;
2685 case ACT_RET_YIELD:
2686 s->current_rule = rule;
2687 rule_ret = HTTP_RULE_RES_YIELD;
2688 goto end;
2689 case ACT_RET_ERR:
2690 rule_ret = HTTP_RULE_RES_ERROR;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002691 s->last_rule_file = rule->conf.file;
2692 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002693 goto end;
2694 case ACT_RET_DONE:
2695 rule_ret = HTTP_RULE_RES_DONE;
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_DENY:
Christopher Fauletb58f62b2020-01-13 16:40:13 +01002700 if (txn->status == -1)
2701 txn->status = 403;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002702 rule_ret = HTTP_RULE_RES_DENY;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002703 s->last_rule_file = rule->conf.file;
2704 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002705 goto end;
2706 case ACT_RET_ABRT:
2707 rule_ret = HTTP_RULE_RES_ABRT;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002708 s->last_rule_file = rule->conf.file;
2709 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002710 goto end;
2711 case ACT_RET_INV:
2712 rule_ret = HTTP_RULE_RES_BADREQ;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002713 s->last_rule_file = rule->conf.file;
2714 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002715 goto end;
2716 }
2717 continue; /* eval the next rule */
2718 }
2719
2720 /* If not action function defined, check for known actions */
Christopher Faulet3e964192018-10-24 11:39:23 +02002721 switch (rule->action) {
2722 case ACT_ACTION_ALLOW:
2723 rule_ret = HTTP_RULE_RES_STOP;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002724 s->last_rule_file = rule->conf.file;
2725 s->last_rule_line = rule->conf.line;
Christopher Faulet3e964192018-10-24 11:39:23 +02002726 goto end;
2727
2728 case ACT_ACTION_DENY:
Christopher Faulet5cb513a2020-05-13 17:56:56 +02002729 txn->status = rule->arg.http_reply->status;
2730 txn->http_reply = rule->arg.http_reply;
Christopher Faulet3e964192018-10-24 11:39:23 +02002731 rule_ret = HTTP_RULE_RES_DENY;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002732 s->last_rule_file = rule->conf.file;
2733 s->last_rule_line = rule->conf.line;
Christopher Faulet3e964192018-10-24 11:39:23 +02002734 goto end;
2735
2736 case ACT_HTTP_REQ_TARPIT:
2737 txn->flags |= TX_CLTARPIT;
Christopher Faulet5cb513a2020-05-13 17:56:56 +02002738 txn->status = rule->arg.http_reply->status;
2739 txn->http_reply = rule->arg.http_reply;
Christopher Faulet3e964192018-10-24 11:39:23 +02002740 rule_ret = HTTP_RULE_RES_DENY;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002741 s->last_rule_file = rule->conf.file;
2742 s->last_rule_line = rule->conf.line;
Christopher Faulet3e964192018-10-24 11:39:23 +02002743 goto end;
2744
Willy Tarreaubc1223b2021-09-02 16:54:33 +02002745 case ACT_HTTP_REDIR: {
2746 int ret = http_apply_redirect_rule(rule->arg.redir, s, txn);
2747
2748 if (ret == 2) // 2 == skip
2749 break;
2750
2751 rule_ret = ret ? HTTP_RULE_RES_ABRT : HTTP_RULE_RES_ERROR;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002752 s->last_rule_file = rule->conf.file;
2753 s->last_rule_line = rule->conf.line;
Christopher Faulet3e964192018-10-24 11:39:23 +02002754 goto end;
Willy Tarreaubc1223b2021-09-02 16:54:33 +02002755 }
Christopher Faulet3e964192018-10-24 11:39:23 +02002756
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002757 /* other flags exists, but normally, they never be matched. */
Christopher Faulet3e964192018-10-24 11:39:23 +02002758 default:
2759 break;
2760 }
2761 }
2762
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002763 if (def_rules && s->current_rule_list == def_rules) {
2764 s->current_rule_list = rules;
2765 goto restart;
2766 }
2767
Christopher Faulet3e964192018-10-24 11:39:23 +02002768 end:
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002769 /* if the ruleset evaluation is finished reset the strict mode */
Christopher Faulet46f95542019-12-20 10:07:22 +01002770 if (rule_ret != HTTP_RULE_RES_YIELD)
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002771 txn->req.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01002772
Christopher Faulet3e964192018-10-24 11:39:23 +02002773 /* we reached the end of the rules, nothing to report */
2774 return rule_ret;
2775}
2776
2777/* Executes the http-response rules <rules> for stream <s> and proxy <px>. It
2778 * returns one of 5 possible statuses: HTTP_RULE_RES_CONT, HTTP_RULE_RES_STOP,
2779 * HTTP_RULE_RES_DONE, HTTP_RULE_RES_YIELD, or HTTP_RULE_RES_BADREQ. If *CONT
2780 * is returned, the process can continue the evaluation of next rule list. If
2781 * *STOP or *DONE is returned, the process must stop the evaluation. If *BADREQ
2782 * is returned, it means the operation could not be processed and a server error
Christopher Fauleta53abad2020-05-13 08:12:22 +02002783 * must be returned. If *YIELD is returned, the caller must call again the
2784 * function with the same context.
Christopher Faulet3e964192018-10-24 11:39:23 +02002785 */
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002786static enum rule_result http_res_get_intercept_rule(struct proxy *px, struct list *def_rules,
2787 struct list *rules, struct stream *s)
Christopher Faulet3e964192018-10-24 11:39:23 +02002788{
2789 struct session *sess = strm_sess(s);
2790 struct http_txn *txn = s->txn;
Christopher Faulet3e964192018-10-24 11:39:23 +02002791 struct act_rule *rule;
Christopher Faulet3e964192018-10-24 11:39:23 +02002792 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002793 int act_opts = 0;
Christopher Faulet3e964192018-10-24 11:39:23 +02002794
Christopher Faulet3e964192018-10-24 11:39:23 +02002795 /* If "the current_rule_list" match the executed rule list, we are in
2796 * resume condition. If a resume is needed it is always in the action
2797 * and never in the ACL or converters. In this case, we initialise the
2798 * current rule, and go to the action execution point.
2799 */
2800 if (s->current_rule) {
2801 rule = s->current_rule;
2802 s->current_rule = NULL;
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002803 if (s->current_rule_list == rules || (def_rules && s->current_rule_list == def_rules))
Christopher Faulet3e964192018-10-24 11:39:23 +02002804 goto resume_execution;
2805 }
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002806 s->current_rule_list = ((!def_rules || s->current_rule_list == def_rules) ? rules : def_rules);
2807
2808 restart:
Christopher Faulet3e964192018-10-24 11:39:23 +02002809
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002810 /* start the ruleset evaluation in strict mode */
2811 txn->rsp.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01002812
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002813 list_for_each_entry(rule, s->current_rule_list, list) {
Christopher Faulet3e964192018-10-24 11:39:23 +02002814 /* check optional condition */
2815 if (rule->cond) {
2816 int ret;
2817
2818 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
2819 ret = acl_pass(ret);
2820
2821 if (rule->cond->pol == ACL_COND_UNLESS)
2822 ret = !ret;
2823
2824 if (!ret) /* condition not matched */
2825 continue;
2826 }
2827
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002828 act_opts |= ACT_OPT_FIRST;
Christopher Faulet3e964192018-10-24 11:39:23 +02002829resume_execution:
Amaury Denoyelle03517732021-05-07 14:25:01 +02002830 if (rule->kw->flags & KWF_EXPERIMENTAL)
2831 mark_tainted(TAINTED_ACTION_EXP_EXECUTED);
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002832
2833 /* Always call the action function if defined */
2834 if (rule->action_ptr) {
Christopher Faulet2e56a732023-01-26 16:18:09 +01002835 if (sc_ep_test(s->scf, SE_FL_ERROR) ||
Christopher Faulet446d8032022-12-12 07:53:18 +01002836 ((s->req.flags & CF_SHUTR) &&
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002837 (px->options & PR_O_ABRT_CLOSE)))
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002838 act_opts |= ACT_OPT_FINAL;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002839
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002840 switch (rule->action_ptr(rule, px, sess, s, act_opts)) {
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002841 case ACT_RET_CONT:
2842 break;
2843 case ACT_RET_STOP:
2844 rule_ret = HTTP_RULE_RES_STOP;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002845 s->last_rule_file = rule->conf.file;
2846 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002847 goto end;
2848 case ACT_RET_YIELD:
2849 s->current_rule = rule;
2850 rule_ret = HTTP_RULE_RES_YIELD;
2851 goto end;
2852 case ACT_RET_ERR:
2853 rule_ret = HTTP_RULE_RES_ERROR;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002854 s->last_rule_file = rule->conf.file;
2855 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002856 goto end;
2857 case ACT_RET_DONE:
2858 rule_ret = HTTP_RULE_RES_DONE;
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_DENY:
Christopher Fauletb58f62b2020-01-13 16:40:13 +01002863 if (txn->status == -1)
2864 txn->status = 502;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002865 rule_ret = HTTP_RULE_RES_DENY;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002866 s->last_rule_file = rule->conf.file;
2867 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002868 goto end;
2869 case ACT_RET_ABRT:
2870 rule_ret = HTTP_RULE_RES_ABRT;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002871 s->last_rule_file = rule->conf.file;
2872 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002873 goto end;
2874 case ACT_RET_INV:
2875 rule_ret = HTTP_RULE_RES_BADREQ;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002876 s->last_rule_file = rule->conf.file;
2877 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002878 goto end;
2879 }
2880 continue; /* eval the next rule */
2881 }
2882
2883 /* If not action function defined, check for known actions */
Christopher Faulet3e964192018-10-24 11:39:23 +02002884 switch (rule->action) {
2885 case ACT_ACTION_ALLOW:
2886 rule_ret = HTTP_RULE_RES_STOP; /* "allow" rules are OK */
Willy Tarreauc6dae862022-03-09 17:23:10 +01002887 s->last_rule_file = rule->conf.file;
2888 s->last_rule_line = rule->conf.line;
Christopher Faulet3e964192018-10-24 11:39:23 +02002889 goto end;
2890
2891 case ACT_ACTION_DENY:
Christopher Faulet5cb513a2020-05-13 17:56:56 +02002892 txn->status = rule->arg.http_reply->status;
2893 txn->http_reply = rule->arg.http_reply;
Christopher Faulet3a26bee2019-12-16 12:47:40 +01002894 rule_ret = HTTP_RULE_RES_DENY;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002895 s->last_rule_file = rule->conf.file;
2896 s->last_rule_line = rule->conf.line;
Christopher Faulet3e964192018-10-24 11:39:23 +02002897 goto end;
2898
Willy Tarreaubc1223b2021-09-02 16:54:33 +02002899 case ACT_HTTP_REDIR: {
2900 int ret = http_apply_redirect_rule(rule->arg.redir, s, txn);
Christopher Faulet3e964192018-10-24 11:39:23 +02002901
Willy Tarreaubc1223b2021-09-02 16:54:33 +02002902 if (ret == 2) // 2 == skip
2903 break;
2904
2905 rule_ret = ret ? HTTP_RULE_RES_ABRT : HTTP_RULE_RES_ERROR;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002906 s->last_rule_file = rule->conf.file;
2907 s->last_rule_line = rule->conf.line;
Willy Tarreaubc1223b2021-09-02 16:54:33 +02002908 goto end;
2909 }
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002910 /* other flags exists, but normally, they never be matched. */
Christopher Faulet3e964192018-10-24 11:39:23 +02002911 default:
2912 break;
2913 }
2914 }
2915
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002916 if (def_rules && s->current_rule_list == def_rules) {
2917 s->current_rule_list = rules;
2918 goto restart;
2919 }
2920
Christopher Faulet3e964192018-10-24 11:39:23 +02002921 end:
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002922 /* if the ruleset evaluation is finished reset the strict mode */
Christopher Faulet46f95542019-12-20 10:07:22 +01002923 if (rule_ret != HTTP_RULE_RES_YIELD)
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002924 txn->rsp.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01002925
Christopher Faulet3e964192018-10-24 11:39:23 +02002926 /* we reached the end of the rules, nothing to report */
2927 return rule_ret;
2928}
2929
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002930/* Executes backend and frontend http-after-response rules for the stream <s>,
2931 * in that order. it return 1 on success and 0 on error. It is the caller
2932 * responsibility to catch error or ignore it. If it catches it, this function
2933 * may be called a second time, for the internal error.
2934 */
2935int http_eval_after_res_rules(struct stream *s)
2936{
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002937 struct list *def_rules, *rules;
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002938 struct session *sess = s->sess;
2939 enum rule_result ret = HTTP_RULE_RES_CONT;
2940
Christopher Faulet507479b2020-05-15 12:29:46 +02002941 /* Eval after-response ruleset only if the reply is not const */
2942 if (s->txn->flags & TX_CONST_REPLY)
2943 goto end;
2944
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002945 /* prune the request variables if not already done and swap to the response variables. */
2946 if (s->vars_reqres.scope != SCOPE_RES) {
2947 if (!LIST_ISEMPTY(&s->vars_reqres.head))
2948 vars_prune(&s->vars_reqres, s->sess, s);
Willy Tarreaub7bfcb32021-08-31 08:13:25 +02002949 vars_init_head(&s->vars_reqres, SCOPE_RES);
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002950 }
2951
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002952 def_rules = (s->be->defpx ? &s->be->defpx->http_after_res_rules : NULL);
2953 rules = &s->be->http_after_res_rules;
2954
2955 ret = http_res_get_intercept_rule(s->be, def_rules, rules, s);
Christopher Faulet4c5a5912021-11-09 17:48:39 +01002956 if ((ret == HTTP_RULE_RES_CONT || ret == HTTP_RULE_RES_STOP) && sess->fe != s->be) {
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002957 def_rules = ((sess->fe->defpx && sess->fe->defpx != s->be->defpx) ? &sess->fe->defpx->http_after_res_rules : NULL);
2958 rules = &sess->fe->http_after_res_rules;
2959 ret = http_res_get_intercept_rule(sess->fe, def_rules, rules, s);
2960 }
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002961
Christopher Faulet507479b2020-05-15 12:29:46 +02002962 end:
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002963 /* All other codes than CONTINUE, STOP or DONE are forbidden */
2964 return (ret == HTTP_RULE_RES_CONT || ret == HTTP_RULE_RES_STOP || ret == HTTP_RULE_RES_DONE);
2965}
2966
Christopher Fauletfcda7c62018-10-24 11:56:22 +02002967/*
2968 * Manage client-side cookie. It can impact performance by about 2% so it is
2969 * desirable to call it only when needed. This code is quite complex because
2970 * of the multiple very crappy and ambiguous syntaxes we have to support. it
2971 * highly recommended not to touch this part without a good reason !
2972 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002973static void http_manage_client_side_cookies(struct stream *s, struct channel *req)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02002974{
2975 struct session *sess = s->sess;
2976 struct http_txn *txn = s->txn;
2977 struct htx *htx;
2978 struct http_hdr_ctx ctx;
2979 char *hdr_beg, *hdr_end, *del_from;
2980 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
2981 int preserve_hdr;
2982
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002983 htx = htxbuf(&req->buf);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02002984 ctx.blk = NULL;
2985 while (http_find_header(htx, ist("Cookie"), &ctx, 1)) {
Olivier Houchardf0f42382019-07-22 17:43:46 +02002986 int is_first = 1;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02002987 del_from = NULL; /* nothing to be deleted */
2988 preserve_hdr = 0; /* assume we may kill the whole header */
2989
2990 /* Now look for cookies. Conforming to RFC2109, we have to support
2991 * attributes whose name begin with a '$', and associate them with
2992 * the right cookie, if we want to delete this cookie.
2993 * So there are 3 cases for each cookie read :
2994 * 1) it's a special attribute, beginning with a '$' : ignore it.
2995 * 2) it's a server id cookie that we *MAY* want to delete : save
2996 * some pointers on it (last semi-colon, beginning of cookie...)
2997 * 3) it's an application cookie : we *MAY* have to delete a previous
2998 * "special" cookie.
2999 * At the end of loop, if a "special" cookie remains, we may have to
3000 * remove it. If no application cookie persists in the header, we
3001 * *MUST* delete it.
3002 *
3003 * Note: RFC2965 is unclear about the processing of spaces around
3004 * the equal sign in the ATTR=VALUE form. A careful inspection of
3005 * the RFC explicitly allows spaces before it, and not within the
3006 * tokens (attrs or values). An inspection of RFC2109 allows that
3007 * too but section 10.1.3 lets one think that spaces may be allowed
3008 * after the equal sign too, resulting in some (rare) buggy
3009 * implementations trying to do that. So let's do what servers do.
3010 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
3011 * allowed quoted strings in values, with any possible character
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003012 * after a backslash, including control chars and delimiters, which
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003013 * causes parsing to become ambiguous. Browsers also allow spaces
3014 * within values even without quotes.
3015 *
3016 * We have to keep multiple pointers in order to support cookie
3017 * removal at the beginning, middle or end of header without
3018 * corrupting the header. All of these headers are valid :
3019 *
3020 * hdr_beg hdr_end
3021 * | |
3022 * v |
3023 * NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3 |
3024 * NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3 v
3025 * NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3
3026 * | | | | | | |
3027 * | | | | | | |
3028 * | | | | | | +--> next
3029 * | | | | | +----> val_end
3030 * | | | | +-----------> val_beg
3031 * | | | +--------------> equal
3032 * | | +----------------> att_end
3033 * | +---------------------> att_beg
3034 * +--------------------------> prev
3035 *
3036 */
3037 hdr_beg = ctx.value.ptr;
3038 hdr_end = hdr_beg + ctx.value.len;
3039 for (prev = hdr_beg; prev < hdr_end; prev = next) {
3040 /* Iterate through all cookies on this line */
3041
3042 /* find att_beg */
3043 att_beg = prev;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003044 if (!is_first)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003045 att_beg++;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003046 is_first = 0;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003047
3048 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
3049 att_beg++;
3050
3051 /* find att_end : this is the first character after the last non
3052 * space before the equal. It may be equal to hdr_end.
3053 */
3054 equal = att_end = att_beg;
3055 while (equal < hdr_end) {
3056 if (*equal == '=' || *equal == ',' || *equal == ';')
3057 break;
3058 if (HTTP_IS_SPHT(*equal++))
3059 continue;
3060 att_end = equal;
3061 }
3062
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003063 /* here, <equal> points to '=', a delimiter or the end. <att_end>
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003064 * is between <att_beg> and <equal>, both may be identical.
3065 */
3066 /* look for end of cookie if there is an equal sign */
3067 if (equal < hdr_end && *equal == '=') {
3068 /* look for the beginning of the value */
3069 val_beg = equal + 1;
3070 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
3071 val_beg++;
3072
3073 /* find the end of the value, respecting quotes */
3074 next = http_find_cookie_value_end(val_beg, hdr_end);
3075
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003076 /* make val_end point to the first white space or delimiter after the value */
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003077 val_end = next;
3078 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
3079 val_end--;
3080 }
3081 else
3082 val_beg = val_end = next = equal;
3083
3084 /* We have nothing to do with attributes beginning with
3085 * '$'. However, they will automatically be removed if a
3086 * header before them is removed, since they're supposed
3087 * to be linked together.
3088 */
3089 if (*att_beg == '$')
3090 continue;
3091
3092 /* Ignore cookies with no equal sign */
3093 if (equal == next) {
3094 /* This is not our cookie, so we must preserve it. But if we already
3095 * scheduled another cookie for removal, we cannot remove the
3096 * complete header, but we can remove the previous block itself.
3097 */
3098 preserve_hdr = 1;
3099 if (del_from != NULL) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003100 int delta = http_del_hdr_value(hdr_beg, hdr_end, &del_from, prev);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003101 val_end += delta;
3102 next += delta;
3103 hdr_end += delta;
3104 prev = del_from;
3105 del_from = NULL;
3106 }
3107 continue;
3108 }
3109
3110 /* if there are spaces around the equal sign, we need to
3111 * strip them otherwise we'll get trouble for cookie captures,
3112 * or even for rewrites. Since this happens extremely rarely,
3113 * it does not hurt performance.
3114 */
3115 if (unlikely(att_end != equal || val_beg > equal + 1)) {
3116 int stripped_before = 0;
3117 int stripped_after = 0;
3118
3119 if (att_end != equal) {
3120 memmove(att_end, equal, hdr_end - equal);
3121 stripped_before = (att_end - equal);
3122 equal += stripped_before;
3123 val_beg += stripped_before;
3124 }
3125
3126 if (val_beg > equal + 1) {
3127 memmove(equal + 1, val_beg, hdr_end + stripped_before - val_beg);
3128 stripped_after = (equal + 1) - val_beg;
3129 val_beg += stripped_after;
3130 stripped_before += stripped_after;
3131 }
3132
3133 val_end += stripped_before;
3134 next += stripped_before;
3135 hdr_end += stripped_before;
3136 }
3137 /* now everything is as on the diagram above */
3138
3139 /* First, let's see if we want to capture this cookie. We check
3140 * that we don't already have a client side cookie, because we
3141 * can only capture one. Also as an optimisation, we ignore
3142 * cookies shorter than the declared name.
3143 */
3144 if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL &&
3145 (val_end - att_beg >= sess->fe->capture_namelen) &&
3146 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
3147 int log_len = val_end - att_beg;
3148
3149 if ((txn->cli_cookie = pool_alloc(pool_head_capture)) == NULL) {
3150 ha_alert("HTTP logging : out of memory.\n");
3151 } else {
3152 if (log_len > sess->fe->capture_len)
3153 log_len = sess->fe->capture_len;
3154 memcpy(txn->cli_cookie, att_beg, log_len);
3155 txn->cli_cookie[log_len] = 0;
3156 }
3157 }
3158
3159 /* Persistence cookies in passive, rewrite or insert mode have the
3160 * following form :
3161 *
3162 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
3163 *
3164 * For cookies in prefix mode, the form is :
3165 *
3166 * Cookie: NAME=SRV~VALUE
3167 */
3168 if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
3169 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
3170 struct server *srv = s->be->srv;
3171 char *delim;
3172
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003173 /* if we're in cookie prefix mode, we'll search the delimiter so that we
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003174 * have the server ID between val_beg and delim, and the original cookie between
3175 * delim+1 and val_end. Otherwise, delim==val_end :
3176 *
3177 * hdr_beg
3178 * |
3179 * v
3180 * NAME=SRV; # in all but prefix modes
3181 * NAME=SRV~OPAQUE ; # in prefix mode
3182 * || || | |+-> next
3183 * || || | +--> val_end
3184 * || || +---------> delim
3185 * || |+------------> val_beg
3186 * || +-------------> att_end = equal
3187 * |+-----------------> att_beg
3188 * +------------------> prev
3189 *
3190 */
3191 if (s->be->ck_opts & PR_CK_PFX) {
3192 for (delim = val_beg; delim < val_end; delim++)
3193 if (*delim == COOKIE_DELIM)
3194 break;
3195 }
3196 else {
3197 char *vbar1;
3198 delim = val_end;
3199 /* Now check if the cookie contains a date field, which would
3200 * appear after a vertical bar ('|') just after the server name
3201 * and before the delimiter.
3202 */
3203 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
3204 if (vbar1) {
3205 /* OK, so left of the bar is the server's cookie and
3206 * right is the last seen date. It is a base64 encoded
3207 * 30-bit value representing the UNIX date since the
3208 * epoch in 4-second quantities.
3209 */
3210 int val;
3211 delim = vbar1++;
3212 if (val_end - vbar1 >= 5) {
3213 val = b64tos30(vbar1);
3214 if (val > 0)
3215 txn->cookie_last_date = val << 2;
3216 }
3217 /* look for a second vertical bar */
3218 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
3219 if (vbar1 && (val_end - vbar1 > 5)) {
3220 val = b64tos30(vbar1 + 1);
3221 if (val > 0)
3222 txn->cookie_first_date = val << 2;
3223 }
3224 }
3225 }
3226
3227 /* if the cookie has an expiration date and the proxy wants to check
3228 * it, then we do that now. We first check if the cookie is too old,
3229 * then only if it has expired. We detect strict overflow because the
3230 * time resolution here is not great (4 seconds). Cookies with dates
3231 * in the future are ignored if their offset is beyond one day. This
3232 * allows an admin to fix timezone issues without expiring everyone
3233 * and at the same time avoids keeping unwanted side effects for too
3234 * long.
3235 */
3236 if (txn->cookie_first_date && s->be->cookie_maxlife &&
3237 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) ||
3238 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
3239 txn->flags &= ~TX_CK_MASK;
3240 txn->flags |= TX_CK_OLD;
3241 delim = val_beg; // let's pretend we have not found the cookie
3242 txn->cookie_first_date = 0;
3243 txn->cookie_last_date = 0;
3244 }
3245 else if (txn->cookie_last_date && s->be->cookie_maxidle &&
3246 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) ||
3247 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
3248 txn->flags &= ~TX_CK_MASK;
3249 txn->flags |= TX_CK_EXPIRED;
3250 delim = val_beg; // let's pretend we have not found the cookie
3251 txn->cookie_first_date = 0;
3252 txn->cookie_last_date = 0;
3253 }
3254
3255 /* Here, we'll look for the first running server which supports the cookie.
3256 * This allows to share a same cookie between several servers, for example
3257 * to dedicate backup servers to specific servers only.
3258 * However, to prevent clients from sticking to cookie-less backup server
3259 * when they have incidentely learned an empty cookie, we simply ignore
3260 * empty cookies and mark them as invalid.
3261 * The same behaviour is applied when persistence must be ignored.
3262 */
3263 if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
3264 srv = NULL;
3265
3266 while (srv) {
3267 if (srv->cookie && (srv->cklen == delim - val_beg) &&
3268 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
3269 if ((srv->cur_state != SRV_ST_STOPPED) ||
3270 (s->be->options & PR_O_PERSIST) ||
3271 (s->flags & SF_FORCE_PRST)) {
3272 /* we found the server and we can use it */
3273 txn->flags &= ~TX_CK_MASK;
3274 txn->flags |= (srv->cur_state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
3275 s->flags |= SF_DIRECT | SF_ASSIGNED;
3276 s->target = &srv->obj_type;
3277 break;
3278 } else {
3279 /* we found a server, but it's down,
3280 * mark it as such and go on in case
3281 * another one is available.
3282 */
3283 txn->flags &= ~TX_CK_MASK;
3284 txn->flags |= TX_CK_DOWN;
3285 }
3286 }
3287 srv = srv->next;
3288 }
3289
3290 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
3291 /* no server matched this cookie or we deliberately skipped it */
3292 txn->flags &= ~TX_CK_MASK;
3293 if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
3294 txn->flags |= TX_CK_UNUSED;
3295 else
3296 txn->flags |= TX_CK_INVALID;
3297 }
3298
3299 /* depending on the cookie mode, we may have to either :
3300 * - delete the complete cookie if we're in insert+indirect mode, so that
3301 * the server never sees it ;
3302 * - remove the server id from the cookie value, and tag the cookie as an
Joseph Herlante9d5c722018-11-25 11:00:25 -08003303 * application cookie so that it does not get accidentally removed later,
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003304 * if we're in cookie prefix mode
3305 */
3306 if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
3307 int delta; /* negative */
3308
3309 memmove(val_beg, delim + 1, hdr_end - (delim + 1));
3310 delta = val_beg - (delim + 1);
3311 val_end += delta;
3312 next += delta;
3313 hdr_end += delta;
3314 del_from = NULL;
3315 preserve_hdr = 1; /* we want to keep this cookie */
3316 }
3317 else if (del_from == NULL &&
3318 (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
3319 del_from = prev;
3320 }
3321 }
3322 else {
3323 /* This is not our cookie, so we must preserve it. But if we already
3324 * scheduled another cookie for removal, we cannot remove the
3325 * complete header, but we can remove the previous block itself.
3326 */
3327 preserve_hdr = 1;
3328
3329 if (del_from != NULL) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003330 int delta = http_del_hdr_value(hdr_beg, hdr_end, &del_from, prev);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003331 if (att_beg >= del_from)
3332 att_beg += delta;
3333 if (att_end >= del_from)
3334 att_end += delta;
3335 val_beg += delta;
3336 val_end += delta;
3337 next += delta;
3338 hdr_end += delta;
3339 prev = del_from;
3340 del_from = NULL;
3341 }
3342 }
3343
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003344 } /* for each cookie */
3345
3346
3347 /* There are no more cookies on this line.
3348 * We may still have one (or several) marked for deletion at the
3349 * end of the line. We must do this now in two ways :
3350 * - if some cookies must be preserved, we only delete from the
3351 * mark to the end of line ;
3352 * - if nothing needs to be preserved, simply delete the whole header
3353 */
3354 if (del_from) {
3355 hdr_end = (preserve_hdr ? del_from : hdr_beg);
3356 }
3357 if ((hdr_end - hdr_beg) != ctx.value.len) {
Christopher Faulet3e2638e2019-06-18 09:49:16 +02003358 if (hdr_beg != hdr_end)
3359 htx_change_blk_value_len(htx, ctx.blk, hdr_end - hdr_beg);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003360 else
3361 http_remove_header(htx, &ctx);
3362 }
3363 } /* for each "Cookie header */
3364}
3365
3366/*
3367 * Manage server-side cookies. It can impact performance by about 2% so it is
3368 * desirable to call it only when needed. This function is also used when we
3369 * just need to know if there is a cookie (eg: for check-cache).
3370 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003371static void http_manage_server_side_cookies(struct stream *s, struct channel *res)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003372{
3373 struct session *sess = s->sess;
3374 struct http_txn *txn = s->txn;
3375 struct htx *htx;
3376 struct http_hdr_ctx ctx;
3377 struct server *srv;
3378 char *hdr_beg, *hdr_end;
3379 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003380
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003381 htx = htxbuf(&res->buf);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003382
3383 ctx.blk = NULL;
Willy Tarreauaa1909e2022-11-14 18:58:35 +01003384 while (http_find_header(htx, ist("Set-Cookie"), &ctx, 1)) {
Olivier Houchardf0f42382019-07-22 17:43:46 +02003385 int is_first = 1;
3386
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003387 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
3388 * <prev> points to the colon.
3389 */
3390 txn->flags |= TX_SCK_PRESENT;
3391
3392 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
3393 * check-cache is enabled) and we are not interested in checking
3394 * them. Warning, the cookie capture is declared in the frontend.
3395 */
3396 if (s->be->cookie_name == NULL && sess->fe->capture_name == NULL)
3397 break;
3398
3399 /* OK so now we know we have to process this response cookie.
3400 * The format of the Set-Cookie header is slightly different
3401 * from the format of the Cookie header in that it does not
3402 * support the comma as a cookie delimiter (thus the header
3403 * cannot be folded) because the Expires attribute described in
3404 * the original Netscape's spec may contain an unquoted date
3405 * with a comma inside. We have to live with this because
3406 * many browsers don't support Max-Age and some browsers don't
3407 * support quoted strings. However the Set-Cookie2 header is
Willy Tarreauaa1909e2022-11-14 18:58:35 +01003408 * clean but basically nobody supports it.
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003409 *
3410 * We have to keep multiple pointers in order to support cookie
3411 * removal at the beginning, middle or end of header without
3412 * corrupting the header (in case of set-cookie2). A special
3413 * pointer, <scav> points to the beginning of the set-cookie-av
3414 * fields after the first semi-colon. The <next> pointer points
3415 * either to the end of line (set-cookie) or next unquoted comma
3416 * (set-cookie2). All of these headers are valid :
3417 *
3418 * hdr_beg hdr_end
3419 * | |
3420 * v |
3421 * NAME1 = VALUE 1 ; Secure; Path="/" |
3422 * NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT v
3423 * NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT
3424 * NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard
3425 * | | | | | | | |
3426 * | | | | | | | +-> next
3427 * | | | | | | +------------> scav
3428 * | | | | | +--------------> val_end
3429 * | | | | +--------------------> val_beg
3430 * | | | +----------------------> equal
3431 * | | +------------------------> att_end
3432 * | +----------------------------> att_beg
3433 * +------------------------------> prev
3434 * -------------------------------> hdr_beg
3435 */
3436 hdr_beg = ctx.value.ptr;
3437 hdr_end = hdr_beg + ctx.value.len;
3438 for (prev = hdr_beg; prev < hdr_end; prev = next) {
3439
3440 /* Iterate through all cookies on this line */
3441
3442 /* find att_beg */
3443 att_beg = prev;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003444 if (!is_first)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003445 att_beg++;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003446 is_first = 0;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003447
3448 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
3449 att_beg++;
3450
3451 /* find att_end : this is the first character after the last non
3452 * space before the equal. It may be equal to hdr_end.
3453 */
3454 equal = att_end = att_beg;
3455
3456 while (equal < hdr_end) {
Willy Tarreauaa1909e2022-11-14 18:58:35 +01003457 if (*equal == '=' || *equal == ';')
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003458 break;
3459 if (HTTP_IS_SPHT(*equal++))
3460 continue;
3461 att_end = equal;
3462 }
3463
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003464 /* here, <equal> points to '=', a delimiter or the end. <att_end>
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003465 * is between <att_beg> and <equal>, both may be identical.
3466 */
3467
3468 /* look for end of cookie if there is an equal sign */
3469 if (equal < hdr_end && *equal == '=') {
3470 /* look for the beginning of the value */
3471 val_beg = equal + 1;
3472 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
3473 val_beg++;
3474
3475 /* find the end of the value, respecting quotes */
3476 next = http_find_cookie_value_end(val_beg, hdr_end);
3477
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003478 /* make val_end point to the first white space or delimiter after the value */
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003479 val_end = next;
3480 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
3481 val_end--;
3482 }
3483 else {
3484 /* <equal> points to next comma, semi-colon or EOL */
3485 val_beg = val_end = next = equal;
3486 }
3487
3488 if (next < hdr_end) {
Willy Tarreauaa1909e2022-11-14 18:58:35 +01003489 /* For Set-Cookie, since commas are permitted
3490 * in values, skip to the end.
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003491 */
Willy Tarreauaa1909e2022-11-14 18:58:35 +01003492 next = hdr_end;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003493 }
3494
3495 /* Now everything is as on the diagram above */
3496
3497 /* Ignore cookies with no equal sign */
3498 if (equal == val_end)
3499 continue;
3500
3501 /* If there are spaces around the equal sign, we need to
3502 * strip them otherwise we'll get trouble for cookie captures,
3503 * or even for rewrites. Since this happens extremely rarely,
3504 * it does not hurt performance.
3505 */
3506 if (unlikely(att_end != equal || val_beg > equal + 1)) {
3507 int stripped_before = 0;
3508 int stripped_after = 0;
3509
3510 if (att_end != equal) {
3511 memmove(att_end, equal, hdr_end - equal);
3512 stripped_before = (att_end - equal);
3513 equal += stripped_before;
3514 val_beg += stripped_before;
3515 }
3516
3517 if (val_beg > equal + 1) {
3518 memmove(equal + 1, val_beg, hdr_end + stripped_before - val_beg);
3519 stripped_after = (equal + 1) - val_beg;
3520 val_beg += stripped_after;
3521 stripped_before += stripped_after;
3522 }
3523
3524 val_end += stripped_before;
3525 next += stripped_before;
3526 hdr_end += stripped_before;
3527
Christopher Faulet3e2638e2019-06-18 09:49:16 +02003528 htx_change_blk_value_len(htx, ctx.blk, hdr_end - hdr_beg);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003529 ctx.value.len = hdr_end - hdr_beg;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003530 }
3531
3532 /* First, let's see if we want to capture this cookie. We check
3533 * that we don't already have a server side cookie, because we
3534 * can only capture one. Also as an optimisation, we ignore
3535 * cookies shorter than the declared name.
3536 */
3537 if (sess->fe->capture_name != NULL &&
3538 txn->srv_cookie == NULL &&
3539 (val_end - att_beg >= sess->fe->capture_namelen) &&
3540 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
3541 int log_len = val_end - att_beg;
3542 if ((txn->srv_cookie = pool_alloc(pool_head_capture)) == NULL) {
3543 ha_alert("HTTP logging : out of memory.\n");
3544 }
3545 else {
3546 if (log_len > sess->fe->capture_len)
3547 log_len = sess->fe->capture_len;
3548 memcpy(txn->srv_cookie, att_beg, log_len);
3549 txn->srv_cookie[log_len] = 0;
3550 }
3551 }
3552
3553 srv = objt_server(s->target);
3554 /* now check if we need to process it for persistence */
3555 if (!(s->flags & SF_IGNORE_PRST) &&
3556 (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
3557 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
3558 /* assume passive cookie by default */
3559 txn->flags &= ~TX_SCK_MASK;
3560 txn->flags |= TX_SCK_FOUND;
3561
3562 /* If the cookie is in insert mode on a known server, we'll delete
3563 * this occurrence because we'll insert another one later.
3564 * We'll delete it too if the "indirect" option is set and we're in
3565 * a direct access.
3566 */
3567 if (s->be->ck_opts & PR_CK_PSV) {
3568 /* The "preserve" flag was set, we don't want to touch the
3569 * server's cookie.
3570 */
3571 }
3572 else if ((srv && (s->be->ck_opts & PR_CK_INS)) ||
3573 ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) {
3574 /* this cookie must be deleted */
3575 if (prev == hdr_beg && next == hdr_end) {
3576 /* whole header */
3577 http_remove_header(htx, &ctx);
3578 /* note: while both invalid now, <next> and <hdr_end>
3579 * are still equal, so the for() will stop as expected.
3580 */
3581 } else {
3582 /* just remove the value */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003583 int delta = http_del_hdr_value(hdr_beg, hdr_end, &prev, next);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003584 next = prev;
3585 hdr_end += delta;
3586 }
3587 txn->flags &= ~TX_SCK_MASK;
3588 txn->flags |= TX_SCK_DELETED;
3589 /* and go on with next cookie */
3590 }
3591 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) {
3592 /* replace bytes val_beg->val_end with the cookie name associated
3593 * with this server since we know it.
3594 */
3595 int sliding, delta;
3596
3597 ctx.value = ist2(val_beg, val_end - val_beg);
3598 ctx.lws_before = ctx.lws_after = 0;
3599 http_replace_header_value(htx, &ctx, ist2(srv->cookie, srv->cklen));
3600 delta = srv->cklen - (val_end - val_beg);
3601 sliding = (ctx.value.ptr - val_beg);
3602 hdr_beg += sliding;
3603 val_beg += sliding;
3604 next += sliding + delta;
3605 hdr_end += sliding + delta;
3606
3607 txn->flags &= ~TX_SCK_MASK;
3608 txn->flags |= TX_SCK_REPLACED;
3609 }
3610 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) {
3611 /* insert the cookie name associated with this server
3612 * before existing cookie, and insert a delimiter between them..
3613 */
3614 int sliding, delta;
3615 ctx.value = ist2(val_beg, 0);
3616 ctx.lws_before = ctx.lws_after = 0;
3617 http_replace_header_value(htx, &ctx, ist2(srv->cookie, srv->cklen + 1));
3618 delta = srv->cklen + 1;
3619 sliding = (ctx.value.ptr - val_beg);
3620 hdr_beg += sliding;
3621 val_beg += sliding;
3622 next += sliding + delta;
3623 hdr_end += sliding + delta;
3624
3625 val_beg[srv->cklen] = COOKIE_DELIM;
3626 txn->flags &= ~TX_SCK_MASK;
3627 txn->flags |= TX_SCK_REPLACED;
3628 }
3629 }
3630 /* that's done for this cookie, check the next one on the same
Willy Tarreauaa1909e2022-11-14 18:58:35 +01003631 * line when next != hdr_end (which should normally not happen
3632 * with set-cookie2 support removed).
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003633 */
3634 }
3635 }
3636}
3637
Christopher Faulet25a02f62018-10-24 12:00:25 +02003638/*
3639 * Parses the Cache-Control and Pragma request header fields to determine if
3640 * the request may be served from the cache and/or if it is cacheable. Updates
3641 * s->txn->flags.
3642 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003643void http_check_request_for_cacheability(struct stream *s, struct channel *req)
Christopher Faulet25a02f62018-10-24 12:00:25 +02003644{
3645 struct http_txn *txn = s->txn;
3646 struct htx *htx;
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003647 struct http_hdr_ctx ctx = { .blk = NULL };
3648 int pragma_found, cc_found;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003649
3650 if ((txn->flags & (TX_CACHEABLE|TX_CACHE_IGNORE)) == TX_CACHE_IGNORE)
3651 return; /* nothing more to do here */
3652
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003653 htx = htxbuf(&req->buf);
Christopher Faulet25a02f62018-10-24 12:00:25 +02003654 pragma_found = cc_found = 0;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003655
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003656 /* Check "pragma" header for HTTP/1.0 compatibility. */
3657 if (http_find_header(htx, ist("pragma"), &ctx, 1)) {
3658 if (isteqi(ctx.value, ist("no-cache"))) {
3659 pragma_found = 1;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003660 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003661 }
Christopher Faulet25a02f62018-10-24 12:00:25 +02003662
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003663 ctx.blk = NULL;
3664 /* Don't use the cache and don't try to store if we found the
3665 * Authorization header */
3666 if (http_find_header(htx, ist("authorization"), &ctx, 1)) {
3667 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3668 txn->flags |= TX_CACHE_IGNORE;
3669 }
Christopher Faulet25a02f62018-10-24 12:00:25 +02003670
Christopher Faulet25a02f62018-10-24 12:00:25 +02003671
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003672 /* Look for "cache-control" header and iterate over all the values
3673 * until we find one that specifies that caching is possible or not. */
3674 ctx.blk = NULL;
3675 while (http_find_header(htx, ist("cache-control"), &ctx, 0)) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003676 cc_found = 1;
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003677 /* We don't check the values after max-age, max-stale nor min-fresh,
3678 * we simply don't use the cache when they're specified. */
3679 if (istmatchi(ctx.value, ist("max-age")) ||
3680 istmatchi(ctx.value, ist("no-cache")) ||
3681 istmatchi(ctx.value, ist("max-stale")) ||
3682 istmatchi(ctx.value, ist("min-fresh"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003683 txn->flags |= TX_CACHE_IGNORE;
3684 continue;
3685 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003686 if (istmatchi(ctx.value, ist("no-store"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003687 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3688 continue;
3689 }
3690 }
3691
3692 /* RFC7234#5.4:
3693 * When the Cache-Control header field is also present and
3694 * understood in a request, Pragma is ignored.
3695 * When the Cache-Control header field is not present in a
3696 * request, caches MUST consider the no-cache request
3697 * pragma-directive as having the same effect as if
3698 * "Cache-Control: no-cache" were present.
3699 */
3700 if (!cc_found && pragma_found)
3701 txn->flags |= TX_CACHE_IGNORE;
3702}
3703
3704/*
3705 * Check if response is cacheable or not. Updates s->txn->flags.
3706 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003707void http_check_response_for_cacheability(struct stream *s, struct channel *res)
Christopher Faulet25a02f62018-10-24 12:00:25 +02003708{
3709 struct http_txn *txn = s->txn;
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003710 struct http_hdr_ctx ctx = { .blk = NULL };
Christopher Faulet25a02f62018-10-24 12:00:25 +02003711 struct htx *htx;
Remi Tricot-Le Bretoncc9bf2e2020-11-12 11:14:41 +01003712 int has_freshness_info = 0;
3713 int has_validator = 0;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003714
3715 if (txn->status < 200) {
3716 /* do not try to cache interim responses! */
3717 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3718 return;
3719 }
3720
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003721 htx = htxbuf(&res->buf);
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003722 /* Check "pragma" header for HTTP/1.0 compatibility. */
3723 if (http_find_header(htx, ist("pragma"), &ctx, 1)) {
3724 if (isteqi(ctx.value, ist("no-cache"))) {
3725 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3726 return;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003727 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003728 }
Christopher Faulet25a02f62018-10-24 12:00:25 +02003729
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003730 /* Look for "cache-control" header and iterate over all the values
3731 * until we find one that specifies that caching is possible or not. */
3732 ctx.blk = NULL;
3733 while (http_find_header(htx, ist("cache-control"), &ctx, 0)) {
3734 if (isteqi(ctx.value, ist("public"))) {
3735 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003736 continue;
3737 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003738 if (isteqi(ctx.value, ist("private")) ||
3739 isteqi(ctx.value, ist("no-cache")) ||
3740 isteqi(ctx.value, ist("no-store")) ||
3741 isteqi(ctx.value, ist("max-age=0")) ||
3742 isteqi(ctx.value, ist("s-maxage=0"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003743 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003744 continue;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003745 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003746 /* We might have a no-cache="set-cookie" form. */
3747 if (istmatchi(ctx.value, ist("no-cache=\"set-cookie"))) {
3748 txn->flags &= ~TX_CACHE_COOK;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003749 continue;
3750 }
Remi Tricot-Le Bretoncc9bf2e2020-11-12 11:14:41 +01003751
3752 if (istmatchi(ctx.value, ist("s-maxage")) ||
3753 istmatchi(ctx.value, ist("max-age"))) {
3754 has_freshness_info = 1;
3755 continue;
3756 }
3757 }
3758
3759 /* If no freshness information could be found in Cache-Control values,
3760 * look for an Expires header. */
3761 if (!has_freshness_info) {
3762 ctx.blk = NULL;
3763 has_freshness_info = http_find_header(htx, ist("expires"), &ctx, 0);
Christopher Faulet25a02f62018-10-24 12:00:25 +02003764 }
Remi Tricot-Le Bretoncc9bf2e2020-11-12 11:14:41 +01003765
3766 /* If no freshness information could be found in Cache-Control or Expires
3767 * values, look for an explicit validator. */
3768 if (!has_freshness_info) {
3769 ctx.blk = NULL;
3770 has_validator = 1;
3771 if (!http_find_header(htx, ist("etag"), &ctx, 0)) {
3772 ctx.blk = NULL;
3773 if (!http_find_header(htx, ist("last-modified"), &ctx, 0))
3774 has_validator = 0;
3775 }
3776 }
3777
3778 /* We won't store an entry that has neither a cache validator nor an
3779 * explicit expiration time, as suggested in RFC 7234#3. */
3780 if (!has_freshness_info && !has_validator)
Remi Tricot-Le Breton879debe2023-02-21 11:47:17 +01003781 txn->flags &= ~TX_CACHEABLE;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003782}
3783
Christopher Faulet377c5a52018-10-24 21:21:30 +02003784/*
3785 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
3786 * for the current backend.
3787 *
3788 * It is assumed that the request is either a HEAD, GET, or POST and that the
3789 * uri_auth field is valid.
3790 *
3791 * Returns 1 if stats should be provided, otherwise 0.
3792 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003793static int http_stats_check_uri(struct stream *s, struct http_txn *txn, struct proxy *backend)
Christopher Faulet377c5a52018-10-24 21:21:30 +02003794{
3795 struct uri_auth *uri_auth = backend->uri_auth;
3796 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003797 struct htx_sl *sl;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003798 struct ist uri;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003799
3800 if (!uri_auth)
3801 return 0;
3802
3803 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
3804 return 0;
3805
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003806 htx = htxbuf(&s->req.buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02003807 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003808 uri = htx_sl_req_uri(sl);
Amaury Denoyellec453f952021-07-06 11:40:12 +02003809 if (*uri_auth->uri_prefix == '/') {
3810 struct http_uri_parser parser = http_uri_parser_init(uri);
3811 uri = http_parse_path(&parser);
3812 }
Christopher Faulet377c5a52018-10-24 21:21:30 +02003813
3814 /* check URI size */
3815 if (uri_auth->uri_len > uri.len)
3816 return 0;
3817
3818 if (memcmp(uri.ptr, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
3819 return 0;
3820
3821 return 1;
3822}
3823
3824/* This function prepares an applet to handle the stats. It can deal with the
3825 * "100-continue" expectation, check that admin rules are met for POST requests,
3826 * and program a response message if something was unexpected. It cannot fail
3827 * and always relies on the stats applet to complete the job. It does not touch
3828 * analysers nor counters, which are left to the caller. It does not touch
3829 * s->target which is supposed to already point to the stats applet. The caller
3830 * is expected to have already assigned an appctx to the stream.
3831 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003832static int http_handle_stats(struct stream *s, struct channel *req)
Christopher Faulet377c5a52018-10-24 21:21:30 +02003833{
3834 struct stats_admin_rule *stats_admin_rule;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003835 struct session *sess = s->sess;
3836 struct http_txn *txn = s->txn;
3837 struct http_msg *msg = &txn->req;
3838 struct uri_auth *uri_auth = s->be->uri_auth;
3839 const char *h, *lookup, *end;
Willy Tarreau8e7c6e62022-05-18 17:58:02 +02003840 struct appctx *appctx = __sc_appctx(s->scb);
Willy Tarreau91cefca2022-05-03 17:08:29 +02003841 struct show_stat_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx));
Christopher Faulet377c5a52018-10-24 21:21:30 +02003842 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003843 struct htx_sl *sl;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003844
Willy Tarreau41f88522022-05-03 18:39:27 +02003845 appctx->st1 = 0;
Willy Tarreau6ef16482022-05-06 18:07:53 +02003846 ctx->state = STAT_STATE_INIT;
Willy Tarreau91cefca2022-05-03 17:08:29 +02003847 ctx->st_code = STAT_STATUS_INIT;
3848 ctx->flags |= uri_auth->flags;
3849 ctx->flags |= STAT_FMT_HTML; /* assume HTML mode by default */
Christopher Faulet377c5a52018-10-24 21:21:30 +02003850 if ((msg->flags & HTTP_MSGF_VER_11) && (txn->meth != HTTP_METH_HEAD))
Willy Tarreau91cefca2022-05-03 17:08:29 +02003851 ctx->flags |= STAT_CHUNKED;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003852
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003853 htx = htxbuf(&req->buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02003854 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003855 lookup = HTX_SL_REQ_UPTR(sl) + uri_auth->uri_len;
3856 end = HTX_SL_REQ_UPTR(sl) + HTX_SL_REQ_ULEN(sl);
Christopher Faulet377c5a52018-10-24 21:21:30 +02003857
3858 for (h = lookup; h <= end - 3; h++) {
3859 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02003860 ctx->flags |= STAT_HIDE_DOWN;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003861 break;
3862 }
Amaury Denoyelle91e55ea2021-02-25 14:46:08 +01003863 }
3864
3865 for (h = lookup; h <= end - 9; h++) {
3866 if (memcmp(h, ";no-maint", 9) == 0) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02003867 ctx->flags |= STAT_HIDE_MAINT;
Willy Tarreau3e320362020-10-23 17:28:57 +02003868 break;
3869 }
Christopher Faulet377c5a52018-10-24 21:21:30 +02003870 }
3871
3872 if (uri_auth->refresh) {
3873 for (h = lookup; h <= end - 10; h++) {
3874 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02003875 ctx->flags |= STAT_NO_REFRESH;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003876 break;
3877 }
3878 }
3879 }
3880
3881 for (h = lookup; h <= end - 4; h++) {
3882 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02003883 ctx->flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
Christopher Faulet377c5a52018-10-24 21:21:30 +02003884 break;
3885 }
3886 }
3887
3888 for (h = lookup; h <= end - 6; h++) {
3889 if (memcmp(h, ";typed", 6) == 0) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02003890 ctx->flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
3891 ctx->flags |= STAT_FMT_TYPED;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003892 break;
3893 }
3894 }
3895
Christopher Faulet6338a082019-09-09 15:50:54 +02003896 for (h = lookup; h <= end - 5; h++) {
3897 if (memcmp(h, ";json", 5) == 0) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02003898 ctx->flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
3899 ctx->flags |= STAT_FMT_JSON;
Christopher Faulet6338a082019-09-09 15:50:54 +02003900 break;
3901 }
3902 }
3903
3904 for (h = lookup; h <= end - 12; h++) {
3905 if (memcmp(h, ";json-schema", 12) == 0) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02003906 ctx->flags &= ~STAT_FMT_MASK;
3907 ctx->flags |= STAT_JSON_SCHM;
Christopher Faulet6338a082019-09-09 15:50:54 +02003908 break;
3909 }
3910 }
3911
Christopher Faulet377c5a52018-10-24 21:21:30 +02003912 for (h = lookup; h <= end - 8; h++) {
3913 if (memcmp(h, ";st=", 4) == 0) {
3914 int i;
3915 h += 4;
Willy Tarreau91cefca2022-05-03 17:08:29 +02003916 ctx->st_code = STAT_STATUS_UNKN;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003917 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
3918 if (strncmp(stat_status_codes[i], h, 4) == 0) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02003919 ctx->st_code = i;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003920 break;
3921 }
3922 }
3923 break;
3924 }
3925 }
3926
Willy Tarreau91cefca2022-05-03 17:08:29 +02003927 ctx->scope_str = 0;
3928 ctx->scope_len = 0;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003929 for (h = lookup; h <= end - 8; h++) {
3930 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
3931 int itx = 0;
3932 const char *h2;
3933 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
3934 const char *err;
3935
3936 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
3937 h2 = h;
Willy Tarreau91cefca2022-05-03 17:08:29 +02003938 ctx->scope_str = h2 - HTX_SL_REQ_UPTR(sl);
Christopher Fauleted7a0662019-01-14 11:07:34 +01003939 while (h < end) {
Christopher Faulet377c5a52018-10-24 21:21:30 +02003940 if (*h == ';' || *h == '&' || *h == ' ')
3941 break;
3942 itx++;
3943 h++;
3944 }
3945
3946 if (itx > STAT_SCOPE_TXT_MAXLEN)
3947 itx = STAT_SCOPE_TXT_MAXLEN;
Willy Tarreau91cefca2022-05-03 17:08:29 +02003948 ctx->scope_len = itx;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003949
Willy Tarreau91cefca2022-05-03 17:08:29 +02003950 /* scope_txt = search query, ctx->scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Christopher Faulet377c5a52018-10-24 21:21:30 +02003951 memcpy(scope_txt, h2, itx);
3952 scope_txt[itx] = '\0';
3953 err = invalid_char(scope_txt);
3954 if (err) {
3955 /* bad char in search text => clear scope */
Willy Tarreau91cefca2022-05-03 17:08:29 +02003956 ctx->scope_str = 0;
3957 ctx->scope_len = 0;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003958 }
3959 break;
3960 }
3961 }
3962
3963 /* now check whether we have some admin rules for this request */
3964 list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) {
3965 int ret = 1;
3966
3967 if (stats_admin_rule->cond) {
3968 ret = acl_exec_cond(stats_admin_rule->cond, s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
3969 ret = acl_pass(ret);
3970 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
3971 ret = !ret;
3972 }
3973
3974 if (ret) {
3975 /* no rule, or the rule matches */
Willy Tarreau91cefca2022-05-03 17:08:29 +02003976 ctx->flags |= STAT_ADMIN;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003977 break;
3978 }
3979 }
3980
Christopher Faulet5d45e382019-02-27 15:15:23 +01003981 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
3982 appctx->st0 = STAT_HTTP_HEAD;
3983 else if (txn->meth == HTTP_METH_POST) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02003984 if (ctx->flags & STAT_ADMIN) {
Christopher Faulet377c5a52018-10-24 21:21:30 +02003985 appctx->st0 = STAT_HTTP_POST;
Christopher Fauletbd9e8422019-08-15 22:26:48 +02003986 if (msg->msg_state < HTTP_MSG_DATA)
3987 req->analysers |= AN_REQ_HTTP_BODY;
3988 }
Christopher Faulet377c5a52018-10-24 21:21:30 +02003989 else {
Christopher Faulet5d45e382019-02-27 15:15:23 +01003990 /* POST without admin level */
Willy Tarreau91cefca2022-05-03 17:08:29 +02003991 ctx->flags &= ~STAT_CHUNKED;
3992 ctx->st_code = STAT_STATUS_DENY;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003993 appctx->st0 = STAT_HTTP_LAST;
3994 }
3995 }
3996 else {
Christopher Faulet5d45e382019-02-27 15:15:23 +01003997 /* Unsupported method */
Willy Tarreau91cefca2022-05-03 17:08:29 +02003998 ctx->flags &= ~STAT_CHUNKED;
3999 ctx->st_code = STAT_STATUS_IVAL;
Christopher Faulet5d45e382019-02-27 15:15:23 +01004000 appctx->st0 = STAT_HTTP_LAST;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004001 }
4002
4003 s->task->nice = -32; /* small boost for HTTP statistics */
4004 return 1;
4005}
4006
Christopher Faulet021a8e42021-03-29 10:46:38 +02004007/* This function waits for the message payload at most <time> milliseconds (may
4008 * be set to TICK_ETERNITY). It stops to wait if at least <bytes> bytes of the
4009 * payload are received (0 means no limit). It returns HTTP_RULE_* depending on
4010 * the result:
4011 *
4012 * - HTTP_RULE_RES_CONT when conditions are met to stop waiting
4013 * - HTTP_RULE_RES_YIELD to wait for more data
Ilya Shipitsinb2be9a12021-04-24 13:25:42 +05004014 * - HTTP_RULE_RES_ABRT when a timeout occurred.
Christopher Faulet021a8e42021-03-29 10:46:38 +02004015 * - HTTP_RULE_RES_BADREQ if a parsing error is raised by lower level
Ilya Shipitsinb2be9a12021-04-24 13:25:42 +05004016 * - HTTP_RULE_RES_ERROR if an internal error occurred
Christopher Faulet021a8e42021-03-29 10:46:38 +02004017 *
Ilya Shipitsinb2be9a12021-04-24 13:25:42 +05004018 * If a timeout occurred, this function is responsible to emit the right response
Christopher Faulet021a8e42021-03-29 10:46:38 +02004019 * to the client, depending on the channel (408 on request side, 504 on response
4020 * side). All other errors must be handled by the caller.
4021 */
4022enum rule_result http_wait_for_msg_body(struct stream *s, struct channel *chn,
4023 unsigned int time, unsigned int bytes)
4024{
4025 struct session *sess = s->sess;
4026 struct http_txn *txn = s->txn;
4027 struct http_msg *msg = ((chn->flags & CF_ISRESP) ? &txn->rsp : &txn->req);
4028 struct htx *htx;
4029 enum rule_result ret = HTTP_RULE_RES_CONT;
4030
4031 htx = htxbuf(&chn->buf);
4032
4033 if (htx->flags & HTX_FL_PARSING_ERROR) {
4034 ret = HTTP_RULE_RES_BADREQ;
4035 goto end;
4036 }
4037 if (htx->flags & HTX_FL_PROCESSING_ERROR) {
4038 ret = HTTP_RULE_RES_ERROR;
4039 goto end;
4040 }
4041
4042 /* Do nothing for bodyless and CONNECT requests */
4043 if (txn->meth == HTTP_METH_CONNECT || (msg->flags & HTTP_MSGF_BODYLESS))
4044 goto end;
4045
4046 if (!(chn->flags & CF_ISRESP) && msg->msg_state < HTTP_MSG_DATA) {
4047 if (http_handle_expect_hdr(s, htx, msg) == -1) {
4048 ret = HTTP_RULE_RES_ERROR;
4049 goto end;
4050 }
4051 }
4052
4053 msg->msg_state = HTTP_MSG_DATA;
4054
4055 /* Now we're in HTTP_MSG_DATA. We just need to know if all data have
4056 * been received or if the buffer is full.
4057 */
Christopher Faulet78335962021-09-23 14:46:32 +02004058 if ((htx->flags & HTX_FL_EOM) ||
4059 htx_get_tail_type(htx) > HTX_BLK_DATA ||
4060 channel_htx_full(chn, htx, global.tune.maxrewrite) ||
Willy Tarreau99615ed2022-05-25 07:29:36 +02004061 sc_waiting_room(chn_prod(chn)))
Christopher Faulet021a8e42021-03-29 10:46:38 +02004062 goto end;
4063
4064 if (bytes) {
4065 struct htx_blk *blk;
4066 unsigned int len = 0;
4067
4068 for (blk = htx_get_first_blk(htx); blk; blk = htx_get_next_blk(htx, blk)) {
4069 if (htx_get_blk_type(blk) != HTX_BLK_DATA)
4070 continue;
4071 len += htx_get_blksz(blk);
4072 if (len >= bytes)
4073 goto end;
4074 }
4075 }
4076
4077 if ((chn->flags & CF_READ_TIMEOUT) || tick_is_expired(chn->analyse_exp, now_ms)) {
4078 if (!(chn->flags & CF_ISRESP))
4079 goto abort_req;
4080 goto abort_res;
4081 }
4082
4083 /* we get here if we need to wait for more data */
Christopher Faulet4b490b72023-01-04 11:55:15 +01004084 if (!(chn->flags & CF_SHUTR)) {
Christopher Faulet021a8e42021-03-29 10:46:38 +02004085 if (!tick_isset(chn->analyse_exp))
4086 chn->analyse_exp = tick_add_ifset(now_ms, time);
4087 ret = HTTP_RULE_RES_YIELD;
4088 }
4089
4090 end:
4091 return ret;
4092
Christopher Fauletf0d80df2023-01-13 10:20:20 +01004093 abort:
4094 http_reply_and_close(s, txn->status, http_error_message(s));
4095 ret = HTTP_RULE_RES_ABRT;
4096 goto end;
4097
Christopher Faulet021a8e42021-03-29 10:46:38 +02004098 abort_req:
4099 txn->status = 408;
4100 if (!(s->flags & SF_ERR_MASK))
4101 s->flags |= SF_ERR_CLITO;
Willy Tarreau4781b152021-04-06 13:53:36 +02004102 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req);
Christopher Faulet021a8e42021-03-29 10:46:38 +02004103 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02004104 _HA_ATOMIC_INC(&sess->listener->counters->failed_req);
Christopher Fauletf0d80df2023-01-13 10:20:20 +01004105 goto abort;
Christopher Faulet021a8e42021-03-29 10:46:38 +02004106
4107 abort_res:
4108 txn->status = 504;
4109 if (!(s->flags & SF_ERR_MASK))
4110 s->flags |= SF_ERR_SRVTO;
Christopher Faulet021a8e42021-03-29 10:46:38 +02004111 stream_inc_http_fail_ctr(s);
Christopher Fauletf0d80df2023-01-13 10:20:20 +01004112 goto abort;
Christopher Faulet021a8e42021-03-29 10:46:38 +02004113}
4114
Willy Tarreaub49672d2022-05-27 10:13:37 +02004115void http_perform_server_redirect(struct stream *s, struct stconn *sc)
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004116{
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004117 struct channel *req = &s->req;
4118 struct channel *res = &s->res;
4119 struct server *srv;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004120 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004121 struct htx_sl *sl;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004122 struct ist path, location;
4123 unsigned int flags;
Amaury Denoyellec453f952021-07-06 11:40:12 +02004124 struct http_uri_parser parser;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004125
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004126 /*
4127 * Create the location
4128 */
4129 chunk_reset(&trash);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004130
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004131 /* 1: add the server's prefix */
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004132 /* special prefix "/" means don't change URL */
4133 srv = __objt_server(s->target);
4134 if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') {
4135 if (!chunk_memcat(&trash, srv->rdr_pfx, srv->rdr_len))
4136 return;
4137 }
4138
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004139 /* 2: add the request Path */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004140 htx = htxbuf(&req->buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02004141 sl = http_get_stline(htx);
Amaury Denoyellec453f952021-07-06 11:40:12 +02004142 parser = http_uri_parser_init(htx_sl_req_uri(sl));
4143 path = http_parse_path(&parser);
Tim Duesterhused526372020-03-05 17:56:33 +01004144 if (!isttest(path))
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004145 return;
4146
4147 if (!chunk_memcat(&trash, path.ptr, path.len))
4148 return;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004149 location = ist2(trash.area, trash.data);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004150
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004151 /*
Ilya Shipitsin4a689da2022-10-29 09:34:32 +05004152 * Create the 302 response
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004153 */
4154 htx = htx_from_buf(&res->buf);
4155 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
4156 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
4157 ist("HTTP/1.1"), ist("302"), ist("Found"));
4158 if (!sl)
4159 goto fail;
4160 sl->info.res.status = 302;
4161 s->txn->status = 302;
4162
4163 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) ||
4164 !htx_add_header(htx, ist("Connection"), ist("close")) ||
4165 !htx_add_header(htx, ist("Content-length"), ist("0")) ||
4166 !htx_add_header(htx, ist("Location"), location))
4167 goto fail;
4168
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004169 if (!htx_add_endof(htx, HTX_BLK_EOH))
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004170 goto fail;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004171
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004172 htx->flags |= HTX_FL_EOM;
Christopher Fauletc20afb82020-01-24 19:16:26 +01004173 htx_to_buf(htx, &res->buf);
Christopher Fauleta72a7e42020-01-28 09:28:11 +01004174 if (!http_forward_proxy_resp(s, 1))
4175 goto fail;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004176
4177 /* return without error. */
Willy Tarreaub49672d2022-05-27 10:13:37 +02004178 sc_shutr(sc);
4179 sc_shutw(sc);
Christopher Faulet50264b42022-03-30 19:39:30 +02004180 s->conn_err_type = STRM_ET_NONE;
Willy Tarreaub49672d2022-05-27 10:13:37 +02004181 sc->state = SC_ST_CLO;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004182
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004183 if (!(s->flags & SF_ERR_MASK))
4184 s->flags |= SF_ERR_LOCAL;
4185 if (!(s->flags & SF_FINST_MASK))
4186 s->flags |= SF_FINST_C;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004187
4188 /* FIXME: we should increase a counter of redirects per server and per backend. */
4189 srv_inc_sess_ctr(srv);
4190 srv_set_sess_last(srv);
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004191 return;
4192
4193 fail:
4194 /* If an error occurred, remove the incomplete HTTP response from the
4195 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004196 channel_htx_truncate(res, htx);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004197}
4198
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05004199/* This function terminates the request because it was completely analyzed or
Christopher Fauletf2824e62018-10-01 12:12:37 +02004200 * because an error was triggered during the body forwarding.
4201 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004202static void http_end_request(struct stream *s)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004203{
4204 struct channel *chn = &s->req;
4205 struct http_txn *txn = s->txn;
4206
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004207 DBG_TRACE_ENTER(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004208
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004209 if (unlikely(txn->req.msg_state < HTTP_MSG_DONE)) {
4210 DBG_TRACE_DEVEL("waiting end of the request", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004211 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004212 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004213
4214 if (txn->req.msg_state == HTTP_MSG_DONE) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004215 /* No need to read anymore, the request was completely parsed.
4216 * We can shut the read side unless we want to abort_on_close,
4217 * or we have a POST request. The issue with POST requests is
4218 * that some browsers still send a CRLF after the request, and
4219 * this CRLF must be read so that it does not remain in the kernel
4220 * buffers, otherwise a close could cause an RST on some systems
4221 * (eg: Linux).
4222 */
Christopher Faulet769d0e92019-03-22 14:23:18 +01004223 if (!(s->be->options & PR_O_ABRT_CLOSE) && txn->meth != HTTP_METH_POST)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004224 channel_dont_read(chn);
4225
4226 /* if the server closes the connection, we want to immediately react
4227 * and close the socket to save packets and syscalls.
4228 */
Willy Tarreaucb041662022-05-17 19:44:42 +02004229 s->scb->flags |= SC_FL_NOHALF;
Christopher Fauletf2824e62018-10-01 12:12:37 +02004230
4231 /* In any case we've finished parsing the request so we must
4232 * disable Nagle when sending data because 1) we're not going
4233 * to shut this side, and 2) the server is waiting for us to
4234 * send pending data.
4235 */
4236 chn->flags |= CF_NEVER_WAIT;
4237
Christopher Fauletc2fba3f2023-03-01 16:03:17 +01004238 if (txn->rsp.msg_state < HTTP_MSG_BODY ||
4239 (txn->rsp.msg_state < HTTP_MSG_DONE && s->scb->state != SC_ST_CLO)) {
Christopher Fauletaf124362023-02-14 10:48:02 +01004240 /* The server has not finished to respond and the
4241 * backend SC is not closed, so we don't want to move in
4242 * order not to upset it.
Christopher Fauletd01ce402019-01-02 17:44:13 +01004243 */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004244 DBG_TRACE_DEVEL("waiting end of the response", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletd01ce402019-01-02 17:44:13 +01004245 return;
4246 }
4247
Christopher Fauletf2824e62018-10-01 12:12:37 +02004248 /* When we get here, it means that both the request and the
4249 * response have finished receiving. Depending on the connection
4250 * mode, we'll have to wait for the last bytes to leave in either
4251 * direction, and sometimes for a close to be effective.
4252 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02004253 if (txn->flags & TX_CON_WANT_TUN) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004254 /* Tunnel mode will not have any analyser so it needs to
4255 * poll for reads.
4256 */
4257 channel_auto_read(chn);
4258 txn->req.msg_state = HTTP_MSG_TUNNEL;
4259 }
4260 else {
4261 /* we're not expecting any new data to come for this
4262 * transaction, so we can close it.
Christopher Faulet9768c262018-10-22 09:34:31 +02004263 *
4264 * However, there is an exception if the response
4265 * length is undefined. In this case, we need to wait
4266 * the close from the server. The response will be
4267 * switched in TUNNEL mode until the end.
Christopher Fauletf2824e62018-10-01 12:12:37 +02004268 */
4269 if (!(txn->rsp.flags & HTTP_MSGF_XFER_LEN) &&
4270 txn->rsp.msg_state != HTTP_MSG_CLOSED)
Christopher Faulet9768c262018-10-22 09:34:31 +02004271 goto check_channel_flags;
Christopher Fauletf2824e62018-10-01 12:12:37 +02004272
4273 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4274 channel_shutr_now(chn);
4275 channel_shutw_now(chn);
4276 }
4277 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004278 goto check_channel_flags;
4279 }
4280
4281 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
4282 http_msg_closing:
4283 /* nothing else to forward, just waiting for the output buffer
4284 * to be empty and for the shutw_now to take effect.
4285 */
4286 if (channel_is_empty(chn)) {
4287 txn->req.msg_state = HTTP_MSG_CLOSED;
4288 goto http_msg_closed;
4289 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004290 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004291 return;
4292 }
4293
4294 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
4295 http_msg_closed:
Christopher Fauletf2824e62018-10-01 12:12:37 +02004296 /* if we don't know whether the server will close, we need to hard close */
4297 if (txn->rsp.flags & HTTP_MSGF_XFER_LEN)
Willy Tarreaucb041662022-05-17 19:44:42 +02004298 s->scb->flags |= SC_FL_NOLINGER; /* we want to close ASAP */
Christopher Fauletf2824e62018-10-01 12:12:37 +02004299 /* see above in MSG_DONE why we only do this in these states */
Christopher Faulet769d0e92019-03-22 14:23:18 +01004300 if (!(s->be->options & PR_O_ABRT_CLOSE))
Christopher Fauletf2824e62018-10-01 12:12:37 +02004301 channel_dont_read(chn);
4302 goto end;
4303 }
4304
4305 check_channel_flags:
4306 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4307 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4308 /* if we've just closed an output, let's switch */
4309 txn->req.msg_state = HTTP_MSG_CLOSING;
4310 goto http_msg_closing;
4311 }
4312
4313 end:
4314 chn->analysers &= AN_REQ_FLT_END;
Christopher Faulet198ef8b2020-12-15 13:32:55 +01004315 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
4316 chn->flags |= CF_NEVER_WAIT;
4317 if (HAS_REQ_DATA_FILTERS(s))
Christopher Fauletf2824e62018-10-01 12:12:37 +02004318 chn->analysers |= AN_REQ_FLT_XFER_DATA;
Christopher Faulet198ef8b2020-12-15 13:32:55 +01004319 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004320 channel_auto_close(chn);
4321 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004322 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004323}
4324
4325
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05004326/* This function terminates the response because it was completely analyzed or
Christopher Fauletf2824e62018-10-01 12:12:37 +02004327 * because an error was triggered during the body forwarding.
4328 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004329static void http_end_response(struct stream *s)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004330{
4331 struct channel *chn = &s->res;
4332 struct http_txn *txn = s->txn;
4333
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004334 DBG_TRACE_ENTER(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004335
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004336 if (unlikely(txn->rsp.msg_state < HTTP_MSG_DONE)) {
4337 DBG_TRACE_DEVEL("waiting end of the response", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004338 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004339 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004340
4341 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
4342 /* In theory, we don't need to read anymore, but we must
4343 * still monitor the server connection for a possible close
4344 * while the request is being uploaded, so we don't disable
4345 * reading.
4346 */
4347 /* channel_dont_read(chn); */
4348
Christopher Fauletaf124362023-02-14 10:48:02 +01004349 if (txn->req.msg_state < HTTP_MSG_DONE && s->scf->state != SC_ST_CLO) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004350 /* The client seems to still be sending data, probably
4351 * because we got an error response during an upload.
4352 * We have the choice of either breaking the connection
4353 * or letting it pass through. Let's do the later.
4354 */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004355 DBG_TRACE_DEVEL("waiting end of the request", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004356 return;
4357 }
4358
4359 /* When we get here, it means that both the request and the
4360 * response have finished receiving. Depending on the connection
4361 * mode, we'll have to wait for the last bytes to leave in either
4362 * direction, and sometimes for a close to be effective.
4363 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02004364 if (txn->flags & TX_CON_WANT_TUN) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004365 channel_auto_read(chn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004366 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
4367 }
4368 else {
4369 /* we're not expecting any new data to come for this
4370 * transaction, so we can close it.
4371 */
4372 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4373 channel_shutr_now(chn);
4374 channel_shutw_now(chn);
4375 }
4376 }
4377 goto check_channel_flags;
4378 }
4379
4380 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
4381 http_msg_closing:
4382 /* nothing else to forward, just waiting for the output buffer
4383 * to be empty and for the shutw_now to take effect.
4384 */
4385 if (channel_is_empty(chn)) {
4386 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4387 goto http_msg_closed;
4388 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004389 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004390 return;
4391 }
4392
4393 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
4394 http_msg_closed:
4395 /* drop any pending data */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004396 channel_htx_truncate(&s->req, htxbuf(&s->req.buf));
Christopher Faulet9768c262018-10-22 09:34:31 +02004397 channel_abort(&s->req);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004398 goto end;
4399 }
4400
4401 check_channel_flags:
4402 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4403 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4404 /* if we've just closed an output, let's switch */
4405 txn->rsp.msg_state = HTTP_MSG_CLOSING;
4406 goto http_msg_closing;
4407 }
4408
4409 end:
4410 chn->analysers &= AN_RES_FLT_END;
Christopher Faulet198ef8b2020-12-15 13:32:55 +01004411 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
4412 chn->flags |= CF_NEVER_WAIT;
4413 if (HAS_RSP_DATA_FILTERS(s))
4414 chn->analysers |= AN_RES_FLT_XFER_DATA;
4415 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004416 channel_auto_close(chn);
4417 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004418 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004419}
4420
Christopher Fauletef70e252020-01-28 09:26:19 +01004421/* Forward a response generated by HAProxy (error/redirect/return). This
4422 * function forwards all pending incoming data. If <final> is set to 0, nothing
4423 * more is performed. It is used for 1xx informational messages. Otherwise, the
Christopher Faulet507479b2020-05-15 12:29:46 +02004424 * transaction is terminated and the request is emptied. On success 1 is
Christopher Faulet40e6b552020-06-25 16:04:50 +02004425 * returned. If an error occurred, 0 is returned. If it fails, this function
4426 * only exits. It is the caller responsibility to do the cleanup.
Christopher Fauletef70e252020-01-28 09:26:19 +01004427 */
4428int http_forward_proxy_resp(struct stream *s, int final)
4429{
4430 struct channel *req = &s->req;
4431 struct channel *res = &s->res;
4432 struct htx *htx = htxbuf(&res->buf);
4433 size_t data;
4434
4435 if (final) {
4436 htx->flags |= HTX_FL_PROXY_RESP;
Christopher Faulet507479b2020-05-15 12:29:46 +02004437
Christopher Fauletaab1b672020-11-18 16:44:02 +01004438 if (!htx_is_empty(htx) && !http_eval_after_res_rules(s))
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01004439 return 0;
Christopher Fauletef70e252020-01-28 09:26:19 +01004440
Christopher Fauletd6c48362020-10-19 18:01:38 +02004441 if (s->txn->meth == HTTP_METH_HEAD)
4442 htx_skip_msg_payload(htx);
4443
Christopher Fauletef70e252020-01-28 09:26:19 +01004444 channel_auto_read(req);
4445 channel_abort(req);
4446 channel_auto_close(req);
4447 channel_htx_erase(req, htxbuf(&req->buf));
4448
Christopher Fauletef70e252020-01-28 09:26:19 +01004449 channel_auto_read(res);
4450 channel_auto_close(res);
4451 channel_shutr_now(res);
Christopher Faulet1a9db7c2020-06-25 15:36:45 +02004452 res->flags |= CF_EOI; /* The response is terminated, add EOI */
Christopher Faulet42432f32020-11-20 17:43:16 +01004453 htxbuf(&res->buf)->flags |= HTX_FL_EOM; /* no more data are expected */
Christopher Fauletef70e252020-01-28 09:26:19 +01004454 }
Christopher Fauletcf6898c2020-06-25 15:55:11 +02004455 else {
4456 /* Send ASAP informational messages. Rely on CF_EOI for final
4457 * response.
4458 */
4459 res->flags |= CF_SEND_DONTWAIT;
4460 }
Christopher Fauletef70e252020-01-28 09:26:19 +01004461
4462 data = htx->data - co_data(res);
4463 c_adv(res, data);
4464 htx->first = -1;
4465 res->total += data;
4466 return 1;
4467}
4468
Willy Tarreaub49672d2022-05-27 10:13:37 +02004469void http_server_error(struct stream *s, struct stconn *sc, int err,
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004470 int finst, struct http_reply *msg)
Christopher Faulet0f226952018-10-22 09:29:56 +02004471{
Christopher Faulet72c7d8d2020-01-27 15:32:25 +01004472 http_reply_and_close(s, s->txn->status, msg);
Christopher Faulet0f226952018-10-22 09:29:56 +02004473 if (!(s->flags & SF_ERR_MASK))
4474 s->flags |= err;
4475 if (!(s->flags & SF_FINST_MASK))
4476 s->flags |= finst;
4477}
4478
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004479void http_reply_and_close(struct stream *s, short status, struct http_reply *msg)
Christopher Faulet0f226952018-10-22 09:29:56 +02004480{
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004481 if (!msg) {
4482 channel_htx_truncate(&s->res, htxbuf(&s->res.buf));
4483 goto end;
4484 }
4485
4486 if (http_reply_message(s, msg) == -1) {
4487 /* On error, return a 500 error message, but don't rewrite it if
Christopher Faulet40e6b552020-06-25 16:04:50 +02004488 * it is already an internal error. If it was already a "const"
4489 * 500 error, just fail.
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004490 */
Christopher Faulet40e6b552020-06-25 16:04:50 +02004491 if (s->txn->status == 500) {
4492 if (s->txn->flags & TX_CONST_REPLY)
4493 goto end;
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004494 s->txn->flags |= TX_CONST_REPLY;
Christopher Faulet40e6b552020-06-25 16:04:50 +02004495 }
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004496 s->txn->status = 500;
4497 s->txn->http_reply = NULL;
4498 return http_reply_and_close(s, s->txn->status, http_error_message(s));
4499 }
4500
4501end:
Christopher Faulet2d565002021-09-10 09:17:50 +02004502 /* At this staged, HTTP analysis is finished */
4503 s->req.analysers &= AN_REQ_FLT_END;
4504 s->req.analyse_exp = TICK_ETERNITY;
4505
4506 s->res.analysers &= AN_RES_FLT_END;
4507 s->res.analyse_exp = TICK_ETERNITY;
4508
Christopher Faulet0f226952018-10-22 09:29:56 +02004509 channel_auto_read(&s->req);
4510 channel_abort(&s->req);
4511 channel_auto_close(&s->req);
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004512 channel_htx_erase(&s->req, htxbuf(&s->req.buf));
Christopher Faulet72c7d8d2020-01-27 15:32:25 +01004513 channel_auto_read(&s->res);
4514 channel_auto_close(&s->res);
4515 channel_shutr_now(&s->res);
Christopher Faulet0f226952018-10-22 09:29:56 +02004516}
4517
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004518struct http_reply *http_error_message(struct stream *s)
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004519{
4520 const int msgnum = http_get_status_idx(s->txn->status);
4521
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004522 if (s->txn->http_reply)
4523 return s->txn->http_reply;
4524 else if (s->be->replies[msgnum])
4525 return s->be->replies[msgnum];
4526 else if (strm_fe(s)->replies[msgnum])
4527 return strm_fe(s)->replies[msgnum];
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004528 else
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004529 return &http_err_replies[msgnum];
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004530}
4531
Christopher Faulet40e6b552020-06-25 16:04:50 +02004532/* Produces an HTX message from an http reply. Depending on the http reply type,
4533 * a, errorfile, an raw file or a log-format string is used. On success, it
4534 * returns 0. If an error occurs -1 is returned. If it fails, this function only
4535 * exits. It is the caller responsibility to do the cleanup.
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004536 */
Christopher Fauletae43b6c2020-05-27 15:24:22 +02004537int http_reply_to_htx(struct stream *s, struct htx *htx, struct http_reply *reply)
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004538{
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004539 struct buffer *errmsg;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004540 struct htx_sl *sl;
4541 struct buffer *body = NULL;
4542 const char *status, *reason, *clen, *ctype;
4543 unsigned int slflags;
4544 int ret = 0;
4545
Christopher Faulete29a97e2020-05-14 14:49:25 +02004546 /*
4547 * - HTTP_REPLY_ERRFILES unexpected here. handled as no payload if so
4548 *
4549 * - HTTP_REPLY_INDIRECT: switch on another reply if defined or handled
4550 * as no payload if NULL. the TXN status code is set with the status
4551 * of the original reply.
4552 */
4553
4554 if (reply->type == HTTP_REPLY_INDIRECT) {
4555 if (reply->body.reply)
4556 reply = reply->body.reply;
4557 }
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004558 if (reply->type == HTTP_REPLY_ERRMSG && !reply->body.errmsg) {
4559 /* get default error message */
4560 if (reply == s->txn->http_reply)
4561 s->txn->http_reply = NULL;
4562 reply = http_error_message(s);
4563 if (reply->type == HTTP_REPLY_INDIRECT) {
4564 if (reply->body.reply)
4565 reply = reply->body.reply;
4566 }
4567 }
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004568
4569 if (reply->type == HTTP_REPLY_ERRMSG) {
4570 /* implicit or explicit error message*/
4571 errmsg = reply->body.errmsg;
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004572 if (errmsg && !b_is_null(errmsg)) {
Christopher Faulet20567362020-05-15 14:52:49 +02004573 if (!htx_copy_msg(htx, errmsg))
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004574 goto fail;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004575 }
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004576 }
4577 else {
4578 /* no payload, file or log-format string */
4579 if (reply->type == HTTP_REPLY_RAW) {
4580 /* file */
4581 body = &reply->body.obj;
4582 }
4583 else if (reply->type == HTTP_REPLY_LOGFMT) {
4584 /* log-format string */
4585 body = alloc_trash_chunk();
4586 if (!body)
4587 goto fail_alloc;
4588 body->data = build_logline(s, body->area, body->size, &reply->body.fmt);
4589 }
4590 /* else no payload */
4591
4592 status = ultoa(reply->status);
4593 reason = http_get_reason(reply->status);
4594 slflags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_CLEN);
4595 if (!body || !b_data(body))
4596 slflags |= HTX_SL_F_BODYLESS;
4597 sl = htx_add_stline(htx, HTX_BLK_RES_SL, slflags, ist("HTTP/1.1"), ist(status), ist(reason));
4598 if (!sl)
4599 goto fail;
4600 sl->info.res.status = reply->status;
4601
4602 clen = (body ? ultoa(b_data(body)) : "0");
4603 ctype = reply->ctype;
4604
4605 if (!LIST_ISEMPTY(&reply->hdrs)) {
4606 struct http_reply_hdr *hdr;
4607 struct buffer *value = alloc_trash_chunk();
4608
4609 if (!value)
4610 goto fail;
4611
4612 list_for_each_entry(hdr, &reply->hdrs, list) {
4613 chunk_reset(value);
4614 value->data = build_logline(s, value->area, value->size, &hdr->value);
4615 if (b_data(value) && !htx_add_header(htx, hdr->name, ist2(b_head(value), b_data(value)))) {
4616 free_trash_chunk(value);
4617 goto fail;
4618 }
4619 chunk_reset(value);
4620 }
4621 free_trash_chunk(value);
4622 }
4623
4624 if (!htx_add_header(htx, ist("content-length"), ist(clen)) ||
4625 (body && b_data(body) && ctype && !htx_add_header(htx, ist("content-type"), ist(ctype))) ||
4626 !htx_add_endof(htx, HTX_BLK_EOH) ||
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004627 (body && b_data(body) && !htx_add_data_atonce(htx, ist2(b_head(body), b_data(body)))))
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004628 goto fail;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004629
4630 htx->flags |= HTX_FL_EOM;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004631 }
4632
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004633 leave:
4634 if (reply->type == HTTP_REPLY_LOGFMT)
4635 free_trash_chunk(body);
4636 return ret;
4637
4638 fail_alloc:
4639 if (!(s->flags & SF_ERR_MASK))
4640 s->flags |= SF_ERR_RESOURCE;
Christopher Faulet97e466c2020-05-15 15:12:47 +02004641 /* fall through */
4642 fail:
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004643 ret = -1;
4644 goto leave;
Christopher Faulet97e466c2020-05-15 15:12:47 +02004645}
4646
4647/* Send an http reply to the client. On success, it returns 0. If an error
Christopher Faulet40e6b552020-06-25 16:04:50 +02004648 * occurs -1 is returned and the response channel is truncated, removing this
4649 * way the faulty reply. This function may fail when the reply is formatted
4650 * (http_reply_to_htx) or when the reply is forwarded
4651 * (http_forward_proxy_resp). On the last case, it is because a
4652 * http-after-response rule fails.
Christopher Faulet97e466c2020-05-15 15:12:47 +02004653 */
4654int http_reply_message(struct stream *s, struct http_reply *reply)
4655{
4656 struct channel *res = &s->res;
4657 struct htx *htx = htx_from_buf(&res->buf);
4658
4659 if (s->txn->status == -1)
4660 s->txn->status = reply->status;
4661 channel_htx_truncate(res, htx);
4662
4663 if (http_reply_to_htx(s, htx, reply) == -1)
4664 goto fail;
4665
4666 htx_to_buf(htx, &s->res.buf);
4667 if (!http_forward_proxy_resp(s, 1))
4668 goto fail;
4669 return 0;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004670
4671 fail:
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004672 channel_htx_truncate(res, htx);
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004673 if (!(s->flags & SF_ERR_MASK))
4674 s->flags |= SF_ERR_PRXCOND;
Christopher Faulet97e466c2020-05-15 15:12:47 +02004675 return -1;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004676}
4677
Christopher Faulet50264b42022-03-30 19:39:30 +02004678/* Return the error message corresponding to s->conn_err_type. It is assumed
Christopher Faulet304cc402019-07-15 15:46:28 +02004679 * that the server side is closed. Note that err_type is actually a
4680 * bitmask, where almost only aborts may be cumulated with other
4681 * values. We consider that aborted operations are more important
4682 * than timeouts or errors due to the fact that nobody else in the
4683 * logs might explain incomplete retries. All others should avoid
4684 * being cumulated. It should normally not be possible to have multiple
4685 * aborts at once, but just in case, the first one in sequence is reported.
4686 * Note that connection errors appearing on the second request of a keep-alive
4687 * connection are not reported since this allows the client to retry.
4688 */
Willy Tarreaub49672d2022-05-27 10:13:37 +02004689void http_return_srv_error(struct stream *s, struct stconn *sc)
Christopher Faulet304cc402019-07-15 15:46:28 +02004690{
Christopher Faulet50264b42022-03-30 19:39:30 +02004691 int err_type = s->conn_err_type;
Christopher Faulet304cc402019-07-15 15:46:28 +02004692
4693 /* set s->txn->status for http_error_message(s) */
Christopher Faulet50264b42022-03-30 19:39:30 +02004694 if (err_type & STRM_ET_QUEUE_ABRT) {
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004695 s->txn->status = -1;
Willy Tarreaub49672d2022-05-27 10:13:37 +02004696 http_server_error(s, sc, SF_ERR_CLICL, SF_FINST_Q, NULL);
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004697 }
Christopher Faulet50264b42022-03-30 19:39:30 +02004698 else if (err_type & STRM_ET_CONN_ABRT) {
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004699 s->txn->status = -1;
Willy Tarreaub49672d2022-05-27 10:13:37 +02004700 http_server_error(s, sc, SF_ERR_CLICL, SF_FINST_C, NULL);
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004701 }
Christopher Faulet50264b42022-03-30 19:39:30 +02004702 else if (err_type & STRM_ET_QUEUE_TO) {
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004703 s->txn->status = 503;
Willy Tarreaub49672d2022-05-27 10:13:37 +02004704 http_server_error(s, sc, SF_ERR_SRVTO, SF_FINST_Q,
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004705 http_error_message(s));
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004706 }
Christopher Faulet50264b42022-03-30 19:39:30 +02004707 else if (err_type & STRM_ET_QUEUE_ERR) {
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004708 s->txn->status = 503;
Willy Tarreaub49672d2022-05-27 10:13:37 +02004709 http_server_error(s, sc, SF_ERR_SRVCL, SF_FINST_Q,
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004710 http_error_message(s));
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004711 }
Christopher Faulet50264b42022-03-30 19:39:30 +02004712 else if (err_type & STRM_ET_CONN_TO) {
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004713 s->txn->status = 503;
Willy Tarreaub49672d2022-05-27 10:13:37 +02004714 http_server_error(s, sc, SF_ERR_SRVTO, SF_FINST_C,
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004715 (s->txn->flags & TX_NOT_FIRST) ? NULL :
4716 http_error_message(s));
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004717 }
Christopher Faulet50264b42022-03-30 19:39:30 +02004718 else if (err_type & STRM_ET_CONN_ERR) {
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004719 s->txn->status = 503;
Willy Tarreaub49672d2022-05-27 10:13:37 +02004720 http_server_error(s, sc, SF_ERR_SRVCL, SF_FINST_C,
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004721 (s->flags & SF_SRV_REUSED) ? NULL :
4722 http_error_message(s));
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004723 }
Christopher Faulet50264b42022-03-30 19:39:30 +02004724 else if (err_type & STRM_ET_CONN_RES) {
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004725 s->txn->status = 503;
Willy Tarreaub49672d2022-05-27 10:13:37 +02004726 http_server_error(s, sc, SF_ERR_RESOURCE, SF_FINST_C,
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004727 (s->txn->flags & TX_NOT_FIRST) ? NULL :
4728 http_error_message(s));
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004729 }
Christopher Faulet50264b42022-03-30 19:39:30 +02004730 else { /* STRM_ET_CONN_OTHER and others */
Christopher Faulet304cc402019-07-15 15:46:28 +02004731 s->txn->status = 500;
Willy Tarreaub49672d2022-05-27 10:13:37 +02004732 http_server_error(s, sc, SF_ERR_INTERNAL, SF_FINST_C,
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004733 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004734 }
4735}
4736
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004737
Christopher Faulet4a28a532019-03-01 11:19:40 +01004738/* Handle Expect: 100-continue for HTTP/1.1 messages if necessary. It returns 0
4739 * on success and -1 on error.
4740 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004741static int http_handle_expect_hdr(struct stream *s, struct htx *htx, struct http_msg *msg)
Christopher Faulet4a28a532019-03-01 11:19:40 +01004742{
4743 /* If we have HTTP/1.1 message with a body and Expect: 100-continue,
4744 * then we must send an HTTP/1.1 100 Continue intermediate response.
4745 */
4746 if (msg->msg_state == HTTP_MSG_BODY && (msg->flags & HTTP_MSGF_VER_11) &&
4747 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
4748 struct ist hdr = { .ptr = "Expect", .len = 6 };
4749 struct http_hdr_ctx ctx;
4750
4751 ctx.blk = NULL;
4752 /* Expect is allowed in 1.1, look for it */
4753 if (http_find_header(htx, hdr, &ctx, 0) &&
4754 unlikely(isteqi(ctx.value, ist2("100-continue", 12)))) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004755 if (http_reply_100_continue(s) == -1)
Christopher Faulet4a28a532019-03-01 11:19:40 +01004756 return -1;
4757 http_remove_header(htx, &ctx);
4758 }
4759 }
4760 return 0;
4761}
4762
Christopher Faulet23a3c792018-11-28 10:01:23 +01004763/* Send a 100-Continue response to the client. It returns 0 on success and -1
4764 * on error. The response channel is updated accordingly.
4765 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004766static int http_reply_100_continue(struct stream *s)
Christopher Faulet23a3c792018-11-28 10:01:23 +01004767{
4768 struct channel *res = &s->res;
4769 struct htx *htx = htx_from_buf(&res->buf);
4770 struct htx_sl *sl;
4771 unsigned int flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|
4772 HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
Christopher Faulet23a3c792018-11-28 10:01:23 +01004773
4774 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
4775 ist("HTTP/1.1"), ist("100"), ist("Continue"));
4776 if (!sl)
4777 goto fail;
4778 sl->info.res.status = 100;
4779
Christopher Faulet1d5ec092019-06-26 14:23:54 +02004780 if (!htx_add_endof(htx, HTX_BLK_EOH))
Christopher Faulet23a3c792018-11-28 10:01:23 +01004781 goto fail;
4782
Christopher Fauleta72a7e42020-01-28 09:28:11 +01004783 if (!http_forward_proxy_resp(s, 0))
4784 goto fail;
Christopher Faulet23a3c792018-11-28 10:01:23 +01004785 return 0;
4786
4787 fail:
4788 /* If an error occurred, remove the incomplete HTTP response from the
4789 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004790 channel_htx_truncate(res, htx);
Christopher Faulet23a3c792018-11-28 10:01:23 +01004791 return -1;
4792}
4793
Christopher Faulet12c51e22018-11-28 15:59:42 +01004794
Christopher Faulet0f226952018-10-22 09:29:56 +02004795/*
4796 * Capture headers from message <htx> according to header list <cap_hdr>, and
4797 * fill the <cap> pointers appropriately.
4798 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004799static void http_capture_headers(struct htx *htx, char **cap, struct cap_hdr *cap_hdr)
Christopher Faulet0f226952018-10-22 09:29:56 +02004800{
4801 struct cap_hdr *h;
4802 int32_t pos;
4803
Christopher Fauleta3f15502019-05-13 15:27:23 +02004804 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet0f226952018-10-22 09:29:56 +02004805 struct htx_blk *blk = htx_get_blk(htx, pos);
4806 enum htx_blk_type type = htx_get_blk_type(blk);
4807 struct ist n, v;
4808
4809 if (type == HTX_BLK_EOH)
4810 break;
4811 if (type != HTX_BLK_HDR)
4812 continue;
4813
4814 n = htx_get_blk_name(htx, blk);
4815
4816 for (h = cap_hdr; h; h = h->next) {
4817 if (h->namelen && (h->namelen == n.len) &&
4818 (strncasecmp(n.ptr, h->name, h->namelen) == 0)) {
4819 if (cap[h->index] == NULL)
4820 cap[h->index] =
4821 pool_alloc(h->pool);
4822
4823 if (cap[h->index] == NULL) {
4824 ha_alert("HTTP capture : out of memory.\n");
4825 break;
4826 }
4827
4828 v = htx_get_blk_value(htx, blk);
Tim Duesterhus2471f5c2021-11-08 09:05:01 +01004829 v = isttrim(v, h->len);
Christopher Faulet0f226952018-10-22 09:29:56 +02004830
4831 memcpy(cap[h->index], v.ptr, v.len);
4832 cap[h->index][v.len]=0;
4833 }
4834 }
4835 }
4836}
4837
Christopher Faulet0b6bdc52018-10-24 11:05:36 +02004838/* Delete a value in a header between delimiters <from> and <next>. The header
4839 * itself is delimited by <start> and <end> pointers. The number of characters
4840 * displaced is returned, and the pointer to the first delimiter is updated if
4841 * required. The function tries as much as possible to respect the following
4842 * principles :
4843 * - replace <from> delimiter by the <next> one unless <from> points to <start>,
4844 * in which case <next> is simply removed
4845 * - set exactly one space character after the new first delimiter, unless there
4846 * are not enough characters in the block being moved to do so.
4847 * - remove unneeded spaces before the previous delimiter and after the new
4848 * one.
4849 *
4850 * It is the caller's responsibility to ensure that :
4851 * - <from> points to a valid delimiter or <start> ;
4852 * - <next> points to a valid delimiter or <end> ;
4853 * - there are non-space chars before <from>.
4854 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004855static int http_del_hdr_value(char *start, char *end, char **from, char *next)
Christopher Faulet0b6bdc52018-10-24 11:05:36 +02004856{
4857 char *prev = *from;
4858
4859 if (prev == start) {
4860 /* We're removing the first value. eat the semicolon, if <next>
4861 * is lower than <end> */
4862 if (next < end)
4863 next++;
4864
4865 while (next < end && HTTP_IS_SPHT(*next))
4866 next++;
4867 }
4868 else {
4869 /* Remove useless spaces before the old delimiter. */
4870 while (HTTP_IS_SPHT(*(prev-1)))
4871 prev--;
4872 *from = prev;
4873
4874 /* copy the delimiter and if possible a space if we're
4875 * not at the end of the line.
4876 */
4877 if (next < end) {
4878 *prev++ = *next++;
4879 if (prev + 1 < next)
4880 *prev++ = ' ';
4881 while (next < end && HTTP_IS_SPHT(*next))
4882 next++;
4883 }
4884 }
4885 memmove(prev, next, end - next);
4886 return (prev - next);
4887}
4888
Christopher Faulet0f226952018-10-22 09:29:56 +02004889
4890/* Formats the start line of the request (without CRLF) and puts it in <str> and
Joseph Herlantc42c0e92018-11-25 10:43:27 -08004891 * return the written length. The line can be truncated if it exceeds <len>.
Christopher Faulet0f226952018-10-22 09:29:56 +02004892 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004893static size_t http_fmt_req_line(const struct htx_sl *sl, char *str, size_t len)
Christopher Faulet0f226952018-10-22 09:29:56 +02004894{
4895 struct ist dst = ist2(str, 0);
4896
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004897 if (istcat(&dst, htx_sl_req_meth(sl), len) == -1)
Christopher Faulet0f226952018-10-22 09:29:56 +02004898 goto end;
4899 if (dst.len + 1 > len)
4900 goto end;
4901 dst.ptr[dst.len++] = ' ';
4902
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004903 if (istcat(&dst, htx_sl_req_uri(sl), len) == -1)
Christopher Faulet0f226952018-10-22 09:29:56 +02004904 goto end;
4905 if (dst.len + 1 > len)
4906 goto end;
4907 dst.ptr[dst.len++] = ' ';
4908
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004909 istcat(&dst, htx_sl_req_vsn(sl), len);
Christopher Faulet0f226952018-10-22 09:29:56 +02004910 end:
4911 return dst.len;
4912}
4913
4914/*
4915 * Print a debug line with a start line.
4916 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004917static void http_debug_stline(const char *dir, struct stream *s, const struct htx_sl *sl)
Christopher Faulet0f226952018-10-22 09:29:56 +02004918{
4919 struct session *sess = strm_sess(s);
4920 int max;
4921
4922 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
4923 dir,
Willy Tarreau88bc8002021-12-06 07:01:02 +00004924 objt_conn(sess->origin) ? (unsigned short)__objt_conn(sess->origin)->handle.fd : -1,
Willy Tarreaufd9417b2022-05-18 16:23:22 +02004925 sc_conn(s->scb) ? (unsigned short)(__sc_conn(s->scb))->handle.fd : -1);
Christopher Faulet0f226952018-10-22 09:29:56 +02004926
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004927 max = HTX_SL_P1_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02004928 UBOUND(max, trash.size - trash.data - 3);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004929 chunk_memcat(&trash, HTX_SL_P1_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02004930 trash.area[trash.data++] = ' ';
4931
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004932 max = HTX_SL_P2_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02004933 UBOUND(max, trash.size - trash.data - 2);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004934 chunk_memcat(&trash, HTX_SL_P2_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02004935 trash.area[trash.data++] = ' ';
4936
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004937 max = HTX_SL_P3_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02004938 UBOUND(max, trash.size - trash.data - 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004939 chunk_memcat(&trash, HTX_SL_P3_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02004940 trash.area[trash.data++] = '\n';
4941
Willy Tarreau2e8ab6b2020-03-14 11:03:20 +01004942 DISGUISE(write(1, trash.area, trash.data));
Christopher Faulet0f226952018-10-22 09:29:56 +02004943}
4944
4945/*
4946 * Print a debug line with a header.
4947 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004948static 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 +02004949{
4950 struct session *sess = strm_sess(s);
4951 int max;
4952
4953 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
4954 dir,
Willy Tarreau88bc8002021-12-06 07:01:02 +00004955 objt_conn(sess->origin) ? (unsigned short)__objt_conn(sess->origin)->handle.fd : -1,
Willy Tarreaufd9417b2022-05-18 16:23:22 +02004956 sc_conn(s->scb) ? (unsigned short)(__sc_conn(s->scb))->handle.fd : -1);
Christopher Faulet0f226952018-10-22 09:29:56 +02004957
4958 max = n.len;
4959 UBOUND(max, trash.size - trash.data - 3);
4960 chunk_memcat(&trash, n.ptr, max);
4961 trash.area[trash.data++] = ':';
4962 trash.area[trash.data++] = ' ';
4963
4964 max = v.len;
4965 UBOUND(max, trash.size - trash.data - 1);
4966 chunk_memcat(&trash, v.ptr, max);
4967 trash.area[trash.data++] = '\n';
4968
Willy Tarreau2e8ab6b2020-03-14 11:03:20 +01004969 DISGUISE(write(1, trash.area, trash.data));
Christopher Faulet0f226952018-10-22 09:29:56 +02004970}
4971
Christopher Fauleta8a46e22019-07-16 14:53:09 +02004972void http_txn_reset_req(struct http_txn *txn)
4973{
Christopher Faulet1aea50e2020-01-17 16:03:53 +01004974 txn->req.flags = 0;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02004975 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
4976}
4977
4978void http_txn_reset_res(struct http_txn *txn)
4979{
Christopher Faulet1aea50e2020-01-17 16:03:53 +01004980 txn->rsp.flags = 0;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02004981 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
4982}
4983
4984/*
Christopher Faulet75f619a2021-03-08 19:12:58 +01004985 * Create and initialize a new HTTP transaction for stream <s>. This should be
4986 * used before processing any new request. It returns the transaction or NLULL
4987 * on error.
Christopher Fauleta8a46e22019-07-16 14:53:09 +02004988 */
Christopher Faulet75f619a2021-03-08 19:12:58 +01004989struct http_txn *http_create_txn(struct stream *s)
Christopher Fauleta8a46e22019-07-16 14:53:09 +02004990{
Christopher Faulet75f619a2021-03-08 19:12:58 +01004991 struct http_txn *txn;
Willy Tarreaub49672d2022-05-27 10:13:37 +02004992 struct stconn *sc = s->scf;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02004993
Christopher Faulet75f619a2021-03-08 19:12:58 +01004994 txn = pool_alloc(pool_head_http_txn);
4995 if (!txn)
4996 return NULL;
4997 s->txn = txn;
4998
Christopher Faulet5eb67f52022-06-22 17:12:05 +02004999 txn->meth = HTTP_METH_OTHER;
Willy Tarreaub49672d2022-05-27 10:13:37 +02005000 txn->flags = ((sc && sc_ep_test(sc, SE_FL_NOT_FIRST)) ? TX_NOT_FIRST : 0);
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005001 txn->status = -1;
Christopher Faulet5cb513a2020-05-13 17:56:56 +02005002 txn->http_reply = NULL;
Christopher Faulete05bf9e2022-03-29 15:23:40 +02005003 txn->l7_buffer = BUF_NULL;
Willy Tarreau8b507582020-02-25 09:35:07 +01005004 write_u32(txn->cache_hash, 0);
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005005
5006 txn->cookie_first_date = 0;
5007 txn->cookie_last_date = 0;
5008
5009 txn->srv_cookie = NULL;
5010 txn->cli_cookie = NULL;
5011 txn->uri = NULL;
5012
5013 http_txn_reset_req(txn);
5014 http_txn_reset_res(txn);
5015
5016 txn->req.chn = &s->req;
5017 txn->rsp.chn = &s->res;
5018
5019 txn->auth.method = HTTP_AUTH_UNKNOWN;
5020
Aurelien DARRAGON5ad2b642022-11-18 09:17:29 +01005021 /* here we don't want to re-initialize s->vars_txn and s->vars_reqres
5022 * variable lists, because they were already initialized upon stream
5023 * creation in stream_new(), and thus may already contain some variables
5024 */
Christopher Faulet75f619a2021-03-08 19:12:58 +01005025
5026 return txn;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005027}
5028
5029/* to be used at the end of a transaction */
Christopher Faulet75f619a2021-03-08 19:12:58 +01005030void http_destroy_txn(struct stream *s)
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005031{
5032 struct http_txn *txn = s->txn;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005033
5034 /* these ones will have been dynamically allocated */
5035 pool_free(pool_head_requri, txn->uri);
5036 pool_free(pool_head_capture, txn->cli_cookie);
5037 pool_free(pool_head_capture, txn->srv_cookie);
Tim Duesterhusa17e6622020-03-05 20:19:02 +01005038 pool_free(pool_head_uniqueid, s->unique_id.ptr);
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005039
Tim Duesterhusa17e6622020-03-05 20:19:02 +01005040 s->unique_id = IST_NULL;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005041 txn->uri = NULL;
5042 txn->srv_cookie = NULL;
5043 txn->cli_cookie = NULL;
5044
Christopher Faulet59399252019-11-07 14:27:52 +01005045 if (!LIST_ISEMPTY(&s->vars_txn.head))
5046 vars_prune(&s->vars_txn, s->sess, s);
5047 if (!LIST_ISEMPTY(&s->vars_reqres.head))
5048 vars_prune(&s->vars_reqres, s->sess, s);
Christopher Faulet75f619a2021-03-08 19:12:58 +01005049
Christopher Faulete05bf9e2022-03-29 15:23:40 +02005050 b_free(&txn->l7_buffer);
5051
Christopher Faulet75f619a2021-03-08 19:12:58 +01005052 pool_free(pool_head_http_txn, txn);
5053 s->txn = NULL;
Christopher Faulet59399252019-11-07 14:27:52 +01005054}
5055
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005056
Christopher Faulet71236de2023-01-13 09:06:38 +01005057void http_set_term_flags(struct stream *s)
5058{
5059 if (!(s->flags & SF_ERR_MASK))
5060 s->flags |= SF_ERR_PRXCOND;
5061
5062 if (!(s->flags & SF_FINST_MASK)) {
5063 if (s->scb->state == SC_ST_INI) {
5064 /* Before any connection attempt on the server side, we
5065 * are still in the request analysis. Just take case to
5066 * detect tarpit error
5067 */
5068 if (s->req.analysers & AN_REQ_HTTP_TARPIT)
5069 s->flags |= SF_FINST_T;
5070 else
5071 s->flags |= SF_FINST_R;
5072 }
5073 else if (s->scb->state == SC_ST_QUE)
5074 s->flags |= SF_FINST_Q;
5075 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)) {
5076 if (unlikely(objt_applet(s->target))) {
5077 s->flags |= SF_FINST_R;
5078 }
5079 else
5080 s->flags |= SF_FINST_C;
5081 }
5082 else {
5083 if (s->txn->rsp.msg_state < HTTP_MSG_DATA) {
5084 /* We are still processing the response headers */
5085 s->flags |= SF_FINST_H;
5086 }
Christopher Faulet5b74f992023-01-26 19:02:07 +01005087 // (res == (done|closing|closed)) & (res->flags & shutw)
5088 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 +01005089 (s->flags & (SF_ERR_CLITO|SF_ERR_CLICL))) {
5090 /* A client error was reported and we are
5091 * transmitting the last block of data
5092 */
5093 s->flags |= SF_FINST_L;
5094 }
5095 else {
5096 /* Otherwise we are in DATA phase on both sides */
5097 s->flags |= SF_FINST_D;
5098 }
5099 }
5100 }
5101}
5102
5103
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005104DECLARE_POOL(pool_head_http_txn, "http_txn", sizeof(struct http_txn));
Christopher Faulet0f226952018-10-22 09:29:56 +02005105
Christopher Fauletf4eb75d2018-10-11 15:55:07 +02005106/*
5107 * Local variables:
5108 * c-indent-level: 8
5109 * c-basic-offset: 8
5110 * End:
5111 */