blob: 31124912380b5b02617741ca5679fee9d8b1d25f [file] [log] [blame]
Christopher Fauletf4eb75d2018-10-11 15:55:07 +02001/*
2 * HTTP protocol analyzer
3 *
4 * Copyright (C) 2018 HAProxy Technologies, Christopher Faulet <cfaulet@haproxy.com>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
Willy Tarreaudcc048a2020-06-04 19:11:43 +020013#include <haproxy/acl.h>
Willy Tarreau122eba92020-06-04 10:15:32 +020014#include <haproxy/action-t.h>
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020015#include <haproxy/api.h>
Willy Tarreau91cefca2022-05-03 17:08:29 +020016#include <haproxy/applet.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020017#include <haproxy/backend.h>
Willy Tarreau8d366972020-05-27 16:10:29 +020018#include <haproxy/base64.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020019#include <haproxy/capture-t.h>
Amaury Denoyelle03517732021-05-07 14:25:01 +020020#include <haproxy/cfgparse.h>
Willy Tarreauf1d32c42020-06-04 21:07:02 +020021#include <haproxy/channel.h>
Willy Tarreau4aa573d2020-06-04 18:21:56 +020022#include <haproxy/check.h>
Willy Tarreau7ea393d2020-06-04 18:02:10 +020023#include <haproxy/connection.h>
Willy Tarreau36979d92020-06-05 17:27:29 +020024#include <haproxy/errors.h>
Willy Tarreauc7babd82020-06-04 21:29:29 +020025#include <haproxy/filters.h>
Willy Tarreaucd72d8c2020-06-02 19:11:26 +020026#include <haproxy/http.h>
Willy Tarreauc2b1ff02020-06-04 21:21:03 +020027#include <haproxy/http_ana.h>
Willy Tarreau87735332020-06-04 09:08:41 +020028#include <haproxy/http_htx.h>
Aurelien DARRAGONb2bb9252022-12-28 15:37:57 +010029#include <haproxy/http_ext.h>
Willy Tarreau16f958c2020-06-03 08:44:35 +020030#include <haproxy/htx.h>
Willy Tarreauaeed4a82020-06-04 22:01:04 +020031#include <haproxy/log.h>
Willy Tarreau6131d6a2020-06-02 16:48:09 +020032#include <haproxy/net_helper.h>
Willy Tarreaua264d962020-06-04 22:29:18 +020033#include <haproxy/proxy.h>
Willy Tarreau7cd8b6e2020-06-02 17:32:26 +020034#include <haproxy/regex.h>
Willy Tarreau5edca2f2022-05-27 09:25:10 +020035#include <haproxy/sc_strm.h>
Willy Tarreau1e56f922020-06-04 23:20:13 +020036#include <haproxy/server-t.h>
Willy Tarreau2eec9b52020-06-04 19:58:55 +020037#include <haproxy/stats.h>
Willy Tarreaucb086c62022-05-27 09:47:12 +020038#include <haproxy/stconn.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020039#include <haproxy/stream.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020040#include <haproxy/trace.h>
Willy Tarreau8c42b8a2020-06-04 19:27:34 +020041#include <haproxy/uri_auth-t.h>
Willy Tarreaua1718922020-06-04 16:25:31 +020042#include <haproxy/vars.h>
Christopher Faulete0768eb2018-10-03 16:38:02 +020043
Christopher Faulete0768eb2018-10-03 16:38:02 +020044
Christopher Fauleteea8fc72019-11-05 16:18:10 +010045#define TRACE_SOURCE &trace_strm
46
Christopher Faulet377c5a52018-10-24 21:21:30 +020047extern const char *stat_status_codes[];
Christopher Fauletf2824e62018-10-01 12:12:37 +020048
Willy Tarreauff882702021-04-10 17:23:00 +020049struct pool_head *pool_head_requri __read_mostly = NULL;
50struct pool_head *pool_head_capture __read_mostly = NULL;
Christopher Fauleta8a46e22019-07-16 14:53:09 +020051
52
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020053static void http_end_request(struct stream *s);
54static void http_end_response(struct stream *s);
Christopher Fauletf2824e62018-10-01 12:12:37 +020055
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020056static void http_capture_headers(struct htx *htx, char **cap, struct cap_hdr *cap_hdr);
57static int http_del_hdr_value(char *start, char *end, char **from, char *next);
58static size_t http_fmt_req_line(const struct htx_sl *sl, char *str, size_t len);
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020059static void http_debug_stline(const char *dir, struct stream *s, const struct htx_sl *sl);
60static void http_debug_hdr(const char *dir, struct stream *s, const struct ist n, const struct ist v);
Christopher Faulet0f226952018-10-22 09:29:56 +020061
Christopher Fauletd4150ad2021-10-13 15:35:55 +020062static enum rule_result http_req_get_intercept_rule(struct proxy *px, struct list *def_rules, struct list *rules, struct stream *s);
63static enum rule_result http_res_get_intercept_rule(struct proxy *px, struct list *def_rules, struct list *rules, struct stream *s);
Christopher Faulet18c13d32022-05-16 11:43:10 +020064static enum rule_result http_req_restrict_header_names(struct stream *s, struct htx *htx, struct proxy *px);
Christopher Faulet3e964192018-10-24 11:39:23 +020065
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020066static void http_manage_client_side_cookies(struct stream *s, struct channel *req);
67static void http_manage_server_side_cookies(struct stream *s, struct channel *res);
Christopher Fauletfcda7c62018-10-24 11:56:22 +020068
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020069static int http_stats_check_uri(struct stream *s, struct http_txn *txn, struct proxy *backend);
70static int http_handle_stats(struct stream *s, struct channel *req);
Christopher Faulet377c5a52018-10-24 21:21:30 +020071
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020072static int http_handle_expect_hdr(struct stream *s, struct htx *htx, struct http_msg *msg);
73static int http_reply_100_continue(struct stream *s);
Christopher Faulet23a3c792018-11-28 10:01:23 +010074
Christopher Faulete0768eb2018-10-03 16:38:02 +020075/* This stream analyser waits for a complete HTTP request. It returns 1 if the
76 * processing can continue on next analysers, or zero if it either needs more
77 * data or wants to immediately abort the request (eg: timeout, error, ...). It
78 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req.analysers
79 * when it has nothing left to do, and may remove any analyser when it wants to
80 * abort.
81 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020082int http_wait_for_request(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +020083{
Christopher Faulet9768c262018-10-22 09:34:31 +020084
Christopher Faulete0768eb2018-10-03 16:38:02 +020085 /*
Christopher Faulet9768c262018-10-22 09:34:31 +020086 * We will analyze a complete HTTP request to check the its syntax.
Christopher Faulete0768eb2018-10-03 16:38:02 +020087 *
Christopher Faulet9768c262018-10-22 09:34:31 +020088 * Once the start line and all headers are received, we may perform a
89 * capture of the error (if any), and we will set a few fields. We also
90 * check for monitor-uri, logging and finally headers capture.
Christopher Faulete0768eb2018-10-03 16:38:02 +020091 */
Christopher Faulete0768eb2018-10-03 16:38:02 +020092 struct session *sess = s->sess;
93 struct http_txn *txn = s->txn;
94 struct http_msg *msg = &txn->req;
Christopher Faulet9768c262018-10-22 09:34:31 +020095 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +010096 struct htx_sl *sl;
Frédéric Lécaille9969adb2023-01-18 11:52:21 +010097 char http_ver;
98 int len;
Christopher Faulete0768eb2018-10-03 16:38:02 +020099
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100100 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200101
Christopher Fauletda46a0d2021-01-21 17:32:58 +0100102 if (unlikely(!IS_HTX_STRM(s))) {
103 /* It is only possible when a TCP stream is upgrade to HTTP.
104 * There is a transition period during which there is no
105 * data. The stream is still in raw mode and SF_IGNORE flag is
106 * still set. When this happens, the new mux is responsible to
Ilya Shipitsinacf84592021-02-06 22:29:08 +0500107 * handle all errors. Thus we may leave immediately.
Christopher Fauletda46a0d2021-01-21 17:32:58 +0100108 */
109 BUG_ON(!(s->flags & SF_IGNORE) || !c_empty(&s->req));
Christopher Faulet9768c262018-10-22 09:34:31 +0200110
Christopher Faulet97b3a612021-03-15 17:10:12 +0100111 /* Don't connect for now */
112 channel_dont_connect(req);
113
114 /* A SHUTR at this stage means we are performing a "destructive"
115 * HTTP upgrade (TCP>H2). In this case, we can leave.
116 */
117 if (req->flags & CF_SHUTR) {
118 s->logs.logwait = 0;
119 s->logs.level = 0;
120 channel_abort(&s->req);
121 channel_abort(&s->res);
122 req->analysers &= AN_REQ_FLT_END;
123 req->analyse_exp = TICK_ETERNITY;
124 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA, s);
125 return 1;
126 }
Christopher Fauletda46a0d2021-01-21 17:32:58 +0100127 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA, s);
128 return 0;
129 }
130
131 htx = htxbuf(&req->buf);
Frédéric Lécaille9969adb2023-01-18 11:52:21 +0100132 sl = http_get_stline(htx);
133 len = HTX_SL_REQ_VLEN(sl);
134 if (len < 6) {
135 http_ver = 0;
136 }
137 else {
138 char *ptr;
139
140 ptr = HTX_SL_REQ_VPTR(sl);
141 http_ver = ptr[5] - '0';
142 }
Christopher Faulet8bebd2f2020-10-06 17:54:56 +0200143
Willy Tarreau4236f032019-03-05 10:43:32 +0100144 /* Parsing errors are caught here */
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200145 if (htx->flags & (HTX_FL_PARSING_ERROR|HTX_FL_PROCESSING_ERROR)) {
Willy Tarreau4236f032019-03-05 10:43:32 +0100146 stream_inc_http_req_ctr(s);
Frédéric Lécaille9969adb2023-01-18 11:52:21 +0100147 proxy_inc_fe_req_ctr(sess->listener, sess->fe, http_ver);
Christopher Fauletbf7175f2021-02-10 14:58:01 +0100148 if (htx->flags & HTX_FL_PARSING_ERROR) {
149 stream_inc_http_err_ctr(s);
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200150 goto return_bad_req;
Christopher Fauletbf7175f2021-02-10 14:58:01 +0100151 }
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200152 else
153 goto return_int_err;
Willy Tarreau4236f032019-03-05 10:43:32 +0100154 }
155
Christopher Faulete0768eb2018-10-03 16:38:02 +0200156 /* we're speaking HTTP here, so let's speak HTTP to the client */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200157 s->srv_error = http_return_srv_error;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200158
Christopher Faulet9768c262018-10-22 09:34:31 +0200159 msg->msg_state = HTTP_MSG_BODY;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200160 stream_inc_http_req_ctr(s);
Frédéric Lécaille9969adb2023-01-18 11:52:21 +0100161 proxy_inc_fe_req_ctr(sess->listener, sess->fe, http_ver); /* one more valid request for this FE */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200162
Christopher Faulet9768c262018-10-22 09:34:31 +0200163 /* kill the pending keep-alive timeout */
Christopher Faulet9768c262018-10-22 09:34:31 +0200164 req->analyse_exp = TICK_ETERNITY;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200165
Christopher Faulet29f17582019-05-23 11:03:26 +0200166 BUG_ON(htx_get_first_type(htx) != HTX_BLK_REQ_SL);
Christopher Faulet03599112018-11-27 11:21:21 +0100167
Christopher Faulet9768c262018-10-22 09:34:31 +0200168 /* 0: we might have to print this header in debug mode */
169 if (unlikely((global.mode & MODE_DEBUG) &&
170 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
171 int32_t pos;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200172
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200173 http_debug_stline("clireq", s, sl);
Christopher Faulet9768c262018-10-22 09:34:31 +0200174
Christopher Fauleta3f15502019-05-13 15:27:23 +0200175 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet9768c262018-10-22 09:34:31 +0200176 struct htx_blk *blk = htx_get_blk(htx, pos);
177 enum htx_blk_type type = htx_get_blk_type(blk);
178
179 if (type == HTX_BLK_EOH)
180 break;
181 if (type != HTX_BLK_HDR)
182 continue;
183
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200184 http_debug_hdr("clihdr", s,
185 htx_get_blk_name(htx, blk),
186 htx_get_blk_value(htx, blk));
Christopher Faulet9768c262018-10-22 09:34:31 +0200187 }
188 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200189
190 /*
Christopher Faulet03599112018-11-27 11:21:21 +0100191 * 1: identify the method and the version. Also set HTTP flags
Christopher Faulete0768eb2018-10-03 16:38:02 +0200192 */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100193 txn->meth = sl->info.req.meth;
Christopher Faulet03599112018-11-27 11:21:21 +0100194 if (sl->flags & HTX_SL_F_VER_11)
Christopher Faulet9768c262018-10-22 09:34:31 +0200195 msg->flags |= HTTP_MSGF_VER_11;
Christopher Faulet03599112018-11-27 11:21:21 +0100196 msg->flags |= HTTP_MSGF_XFER_LEN;
Christopher Faulet2a408542020-11-20 14:22:37 +0100197 if (sl->flags & HTX_SL_F_CLEN)
198 msg->flags |= HTTP_MSGF_CNT_LEN;
199 else if (sl->flags & HTX_SL_F_CHNK)
200 msg->flags |= HTTP_MSGF_TE_CHNK;
Christopher Fauletb2db4fa2018-11-27 16:51:09 +0100201 if (sl->flags & HTX_SL_F_BODYLESS)
202 msg->flags |= HTTP_MSGF_BODYLESS;
Christopher Faulet576c3582021-01-08 15:53:01 +0100203 if (sl->flags & HTX_SL_F_CONN_UPG)
204 msg->flags |= HTTP_MSGF_CONN_UPG;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200205
206 /* we can make use of server redirect on GET and HEAD */
207 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
208 s->flags |= SF_REDIRECTABLE;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100209 else if (txn->meth == HTTP_METH_OTHER && isteqi(htx_sl_req_meth(sl), ist("PRI"))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200210 /* PRI is reserved for the HTTP/2 preface */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200211 goto return_bad_req;
212 }
213
214 /*
Christopher Faulet6072beb2020-02-18 15:34:58 +0100215 * 2: check if the URI matches the monitor_uri. We have to do this for
216 * every request which gets in, because the monitor-uri is defined by
217 * the frontend. If the monitor-uri starts with a '/', the matching is
218 * done against the request's path. Otherwise, the request's uri is
219 * used. It is a workaround to let HTTP/2 health-checks work as
220 * expected.
Christopher Faulete0768eb2018-10-03 16:38:02 +0200221 */
Tim Duesterhus4b1fcaa2022-03-05 00:52:40 +0100222 if (unlikely(isttest(sess->fe->monitor_uri))) {
223 const struct ist monitor_uri = sess->fe->monitor_uri;
Amaury Denoyellec453f952021-07-06 11:40:12 +0200224 struct http_uri_parser parser = http_uri_parser_init(htx_sl_req_uri(sl));
Christopher Faulete0768eb2018-10-03 16:38:02 +0200225
Amaury Denoyelle5a9bd372021-07-06 11:23:10 +0200226 if ((istptr(monitor_uri)[0] == '/' &&
Amaury Denoyellec453f952021-07-06 11:40:12 +0200227 isteq(http_parse_path(&parser), monitor_uri)) ||
Amaury Denoyelle5a9bd372021-07-06 11:23:10 +0200228 isteq(htx_sl_req_uri(sl), monitor_uri)) {
229 /*
230 * We have found the monitor URI
231 */
232 struct acl_cond *cond;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200233
Amaury Denoyelle5a9bd372021-07-06 11:23:10 +0200234 s->flags |= SF_MONITOR;
235 _HA_ATOMIC_INC(&sess->fe->fe_counters.intercepted_req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200236
Amaury Denoyelle5a9bd372021-07-06 11:23:10 +0200237 /* Check if we want to fail this monitor request or not */
238 list_for_each_entry(cond, &sess->fe->mon_fail_cond, list) {
239 int ret = acl_exec_cond(cond, sess->fe, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200240
Amaury Denoyelle5a9bd372021-07-06 11:23:10 +0200241 ret = acl_pass(ret);
242 if (cond->pol == ACL_COND_UNLESS)
243 ret = !ret;
244
245 if (ret) {
246 /* we fail this request, let's return 503 service unavail */
247 txn->status = 503;
248 if (!(s->flags & SF_ERR_MASK))
249 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
250 goto return_prx_cond;
251 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200252 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200253
Amaury Denoyelle5a9bd372021-07-06 11:23:10 +0200254 /* nothing to fail, let's reply normally */
255 txn->status = 200;
256 if (!(s->flags & SF_ERR_MASK))
257 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
258 goto return_prx_cond;
259 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200260 }
261
262 /*
263 * 3: Maybe we have to copy the original REQURI for the logs ?
264 * Note: we cannot log anymore if the request has been
265 * classified as invalid.
266 */
267 if (unlikely(s->logs.logwait & LW_REQ)) {
268 /* we have a complete HTTP request that we must log */
269 if ((txn->uri = pool_alloc(pool_head_requri)) != NULL) {
Christopher Faulet9768c262018-10-22 09:34:31 +0200270 size_t len;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200271
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200272 len = http_fmt_req_line(sl, txn->uri, global.tune.requri_len - 1);
Christopher Faulet9768c262018-10-22 09:34:31 +0200273 txn->uri[len] = 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200274
275 if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT)))
276 s->do_log(s);
277 } else {
278 ha_alert("HTTP logging : out of memory.\n");
279 }
280 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200281
Christopher Faulete0768eb2018-10-03 16:38:02 +0200282 /* if the frontend has "option http-use-proxy-header", we'll check if
283 * we have what looks like a proxied connection instead of a connection,
284 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
285 * Note that this is *not* RFC-compliant, however browsers and proxies
286 * happen to do that despite being non-standard :-(
287 * We consider that a request not beginning with either '/' or '*' is
288 * a proxied connection, which covers both "scheme://location" and
289 * CONNECT ip:port.
290 */
291 if ((sess->fe->options2 & PR_O2_USE_PXHDR) &&
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100292 *HTX_SL_REQ_UPTR(sl) != '/' && *HTX_SL_REQ_UPTR(sl) != '*')
Christopher Faulete0768eb2018-10-03 16:38:02 +0200293 txn->flags |= TX_USE_PX_CONN;
294
Christopher Faulete0768eb2018-10-03 16:38:02 +0200295 /* 5: we may need to capture headers */
296 if (unlikely((s->logs.logwait & LW_REQHDR) && s->req_cap))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200297 http_capture_headers(htx, s->req_cap, sess->fe->req_cap);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200298
Christopher Faulete0768eb2018-10-03 16:38:02 +0200299 /* we may have to wait for the request's body */
Christopher Faulet9768c262018-10-22 09:34:31 +0200300 if (s->be->options & PR_O_WREQ_BODY)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200301 req->analysers |= AN_REQ_HTTP_BODY;
302
303 /*
304 * RFC7234#4:
305 * A cache MUST write through requests with methods
306 * that are unsafe (Section 4.2.1 of [RFC7231]) to
307 * the origin server; i.e., a cache is not allowed
308 * to generate a reply to such a request before
309 * having forwarded the request and having received
310 * a corresponding response.
311 *
312 * RFC7231#4.2.1:
313 * Of the request methods defined by this
314 * specification, the GET, HEAD, OPTIONS, and TRACE
315 * methods are defined to be safe.
316 */
317 if (likely(txn->meth == HTTP_METH_GET ||
318 txn->meth == HTTP_METH_HEAD ||
319 txn->meth == HTTP_METH_OPTIONS ||
320 txn->meth == HTTP_METH_TRACE))
321 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
322
323 /* end of job, return OK */
324 req->analysers &= ~an_bit;
325 req->analyse_exp = TICK_ETERNITY;
Christopher Faulet9768c262018-10-22 09:34:31 +0200326
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100327 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200328 return 1;
329
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200330 return_int_err:
331 txn->status = 500;
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200332 if (!(s->flags & SF_ERR_MASK))
333 s->flags |= SF_ERR_INTERNAL;
Willy Tarreau4781b152021-04-06 13:53:36 +0200334 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
William Lallemand36119de2021-03-08 15:26:48 +0100335 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200336 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200337 goto return_prx_cond;
338
Christopher Faulete0768eb2018-10-03 16:38:02 +0200339 return_bad_req:
Christopher Faulet9768c262018-10-22 09:34:31 +0200340 txn->status = 400;
Willy Tarreau4781b152021-04-06 13:53:36 +0200341 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req);
William Lallemand36119de2021-03-08 15:26:48 +0100342 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200343 _HA_ATOMIC_INC(&sess->listener->counters->failed_req);
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200344 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200345
346 return_prx_cond:
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200347 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet4a66c942023-01-13 09:43:21 +0100348 http_set_term_flags(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200349
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100350 DBG_TRACE_DEVEL("leaving on error",
351 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200352 return 0;
353}
354
355
356/* This stream analyser runs all HTTP request processing which is common to
357 * frontends and backends, which means blocking ACLs, filters, connection-close,
358 * reqadd, stats and redirects. This is performed for the designated proxy.
359 * It returns 1 if the processing can continue on next analysers, or zero if it
360 * either needs more data or wants to immediately abort the request (eg: deny,
361 * error, ...).
362 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200363int http_process_req_common(struct stream *s, struct channel *req, int an_bit, struct proxy *px)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200364{
Christopher Fauletd4150ad2021-10-13 15:35:55 +0200365 struct list *def_rules, *rules;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200366 struct session *sess = s->sess;
367 struct http_txn *txn = s->txn;
368 struct http_msg *msg = &txn->req;
Christopher Fauletff2759f2018-10-24 11:13:16 +0200369 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200370 struct redirect_rule *rule;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200371 enum rule_result verdict;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200372 struct connection *conn = objt_conn(sess->origin);
373
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100374 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200375
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100376 htx = htxbuf(&req->buf);
Christopher Fauletff2759f2018-10-24 11:13:16 +0200377
Christopher Faulet1907ccc2019-04-29 13:12:02 +0200378 /* just in case we have some per-backend tracking. Only called the first
379 * execution of the analyser. */
Christopher Fauletd4150ad2021-10-13 15:35:55 +0200380 if (!s->current_rule && !s->current_rule_list)
Christopher Faulet1907ccc2019-04-29 13:12:02 +0200381 stream_inc_be_http_req_ctr(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200382
Christopher Fauletd4150ad2021-10-13 15:35:55 +0200383 def_rules = ((px->defpx && (an_bit == AN_REQ_HTTP_PROCESS_FE || px != sess->fe)) ? &px->defpx->http_req_rules : NULL);
384 rules = &px->http_req_rules;
385
Christopher Faulete0768eb2018-10-03 16:38:02 +0200386 /* evaluate http-request rules */
Christopher Fauletd4150ad2021-10-13 15:35:55 +0200387 if ((def_rules && !LIST_ISEMPTY(def_rules)) || !LIST_ISEMPTY(rules)) {
388 verdict = http_req_get_intercept_rule(px, def_rules, rules, s);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200389
390 switch (verdict) {
391 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
392 goto return_prx_yield;
393
394 case HTTP_RULE_RES_CONT:
395 case HTTP_RULE_RES_STOP: /* nothing to do */
396 break;
397
398 case HTTP_RULE_RES_DENY: /* deny or tarpit */
399 if (txn->flags & TX_CLTARPIT)
400 goto tarpit;
401 goto deny;
402
403 case HTTP_RULE_RES_ABRT: /* abort request, response already sent. Eg: auth */
404 goto return_prx_cond;
405
406 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
407 goto done;
408
409 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
410 goto return_bad_req;
Christopher Faulet3a26bee2019-12-16 12:47:40 +0100411
412 case HTTP_RULE_RES_ERROR: /* failed with a bad request */
413 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200414 }
415 }
416
Christopher Faulet18c13d32022-05-16 11:43:10 +0200417 if (px->options2 & (PR_O2_RSTRICT_REQ_HDR_NAMES_BLK|PR_O2_RSTRICT_REQ_HDR_NAMES_DEL)) {
418 verdict = http_req_restrict_header_names(s, htx, px);
419 if (verdict == HTTP_RULE_RES_DENY)
420 goto deny;
421 }
422
Christopher Faulete0768eb2018-10-03 16:38:02 +0200423 if (conn && (conn->flags & CO_FL_EARLY_DATA) &&
Olivier Houchard220a26c2020-01-23 14:57:36 +0100424 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_SSL_WAIT_HS))) {
Christopher Fauletff2759f2018-10-24 11:13:16 +0200425 struct http_hdr_ctx ctx;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200426
Christopher Fauletff2759f2018-10-24 11:13:16 +0200427 ctx.blk = NULL;
428 if (!http_find_header(htx, ist("Early-Data"), &ctx, 0)) {
429 if (unlikely(!http_add_header(htx, ist("Early-Data"), ist("1"))))
Christopher Fauletd649b572022-06-01 17:42:35 +0200430 goto return_fail_rewrite;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200431 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200432 }
433
434 /* OK at this stage, we know that the request was accepted according to
435 * the http-request rules, we can check for the stats. Note that the
436 * URI is detected *before* the req* rules in order not to be affected
437 * by a possible reqrep, while they are processed *after* so that a
438 * reqdeny can still block them. This clearly needs to change in 1.6!
439 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200440 if (!s->target && http_stats_check_uri(s, txn, px)) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200441 s->target = &http_stats_applet.obj_type;
Willy Tarreaua0b58b52022-05-27 08:33:53 +0200442 if (unlikely(!sc_applet_create(s->scb, objt_applet(s->target)))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200443 s->logs.tv_request = now;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200444 if (!(s->flags & SF_ERR_MASK))
445 s->flags |= SF_ERR_RESOURCE;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100446 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200447 }
448
449 /* parse the whole stats request and extract the relevant information */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200450 http_handle_stats(s, req);
Christopher Fauletd4150ad2021-10-13 15:35:55 +0200451 verdict = http_req_get_intercept_rule(px, NULL, &px->uri_auth->http_req_rules, s);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200452 /* not all actions implemented: deny, allow, auth */
453
454 if (verdict == HTTP_RULE_RES_DENY) /* stats http-request deny */
455 goto deny;
456
457 if (verdict == HTTP_RULE_RES_ABRT) /* stats auth / stats http-request auth */
458 goto return_prx_cond;
Christopher Faulet3a26bee2019-12-16 12:47:40 +0100459
460 if (verdict == HTTP_RULE_RES_BADREQ) /* failed with a bad request */
461 goto return_bad_req;
462
463 if (verdict == HTTP_RULE_RES_ERROR) /* failed with a bad request */
464 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200465 }
466
Christopher Faulet2571bc62019-03-01 11:44:26 +0100467 /* Proceed with the applets now. */
468 if (unlikely(objt_applet(s->target))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200469 if (sess->fe == s->be) /* report it if the request was intercepted by the frontend */
Willy Tarreau4781b152021-04-06 13:53:36 +0200470 _HA_ATOMIC_INC(&sess->fe->fe_counters.intercepted_req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200471
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200472 if (http_handle_expect_hdr(s, htx, msg) == -1)
Christopher Fauletb8a53712019-12-16 11:29:38 +0100473 goto return_int_err;
Christopher Fauletbcf242a2019-03-01 11:36:26 +0100474
Christopher Faulete0768eb2018-10-03 16:38:02 +0200475 if (!(s->flags & SF_ERR_MASK)) // this is not really an error but it is
476 s->flags |= SF_ERR_LOCAL; // to mark that it comes from the proxy
Christopher Faulet4a66c942023-01-13 09:43:21 +0100477 http_set_term_flags(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200478
Christopher Fauletc2ac5e42021-03-08 18:20:09 +0100479 if (HAS_FILTERS(s))
480 req->analysers |= AN_REQ_FLT_HTTP_HDRS;
481
Christopher Faulete0768eb2018-10-03 16:38:02 +0200482 /* enable the minimally required analyzers to handle keep-alive and compression on the HTTP response */
483 req->analysers &= (AN_REQ_HTTP_BODY | AN_REQ_FLT_HTTP_HDRS | AN_REQ_FLT_END);
484 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
485 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Christopher Fauletbcf242a2019-03-01 11:36:26 +0100486
487 req->flags |= CF_SEND_DONTWAIT;
488 s->flags |= SF_ASSIGNED;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200489 goto done;
490 }
491
492 /* check whether we have some ACLs set to redirect this request */
493 list_for_each_entry(rule, &px->redirect_rules, list) {
494 if (rule->cond) {
495 int ret;
496
497 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
498 ret = acl_pass(ret);
499 if (rule->cond->pol == ACL_COND_UNLESS)
500 ret = !ret;
501 if (!ret)
502 continue;
503 }
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200504 if (!http_apply_redirect_rule(rule, s, txn))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100505 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200506 goto done;
507 }
508
509 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
510 * If this happens, then the data will not come immediately, so we must
511 * send all what we have without waiting. Note that due to the small gain
512 * in waiting for the body of the request, it's easier to simply put the
513 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
514 * itself once used.
515 */
516 req->flags |= CF_SEND_DONTWAIT;
517
518 done: /* done with this analyser, continue with next ones that the calling
519 * points will have set, if any.
520 */
521 req->analyse_exp = TICK_ETERNITY;
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +0500522 done_without_exp: /* done with this analyser, but don't reset the analyse_exp. */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200523 req->analysers &= ~an_bit;
Christopher Fauletd4150ad2021-10-13 15:35:55 +0200524 s->current_rule = s->current_rule_list = NULL;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100525 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200526 return 1;
527
528 tarpit:
529 /* Allow cookie logging
530 */
531 if (s->be->cookie_name || sess->fe->capture_name)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200532 http_manage_client_side_cookies(s, req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200533
534 /* When a connection is tarpitted, we use the tarpit timeout,
535 * which may be the same as the connect timeout if unspecified.
536 * If unset, then set it to zero because we really want it to
537 * eventually expire. We build the tarpit as an analyser.
538 */
Christopher Faulet202c6ce2019-01-07 14:57:35 +0100539 channel_htx_erase(&s->req, htx);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200540
541 /* wipe the request out so that we can drop the connection early
542 * if the client closes first.
543 */
544 channel_dont_connect(req);
545
Christopher Faulete0768eb2018-10-03 16:38:02 +0200546 req->analysers &= AN_REQ_FLT_END; /* remove switching rules etc... */
547 req->analysers |= AN_REQ_HTTP_TARPIT;
548 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
549 if (!req->analyse_exp)
550 req->analyse_exp = tick_add(now_ms, 0);
551 stream_inc_http_err_ctr(s);
Willy Tarreau4781b152021-04-06 13:53:36 +0200552 _HA_ATOMIC_INC(&sess->fe->fe_counters.denied_req);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100553 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau4781b152021-04-06 13:53:36 +0200554 _HA_ATOMIC_INC(&s->be->be_counters.denied_req);
William Lallemand36119de2021-03-08 15:26:48 +0100555 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200556 _HA_ATOMIC_INC(&sess->listener->counters->denied_req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200557 goto done_without_exp;
558
559 deny: /* this request was blocked (denied) */
560
561 /* Allow cookie logging
562 */
563 if (s->be->cookie_name || sess->fe->capture_name)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200564 http_manage_client_side_cookies(s, req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200565
Christopher Faulete0768eb2018-10-03 16:38:02 +0200566 s->logs.tv_request = now;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200567 stream_inc_http_err_ctr(s);
Willy Tarreau4781b152021-04-06 13:53:36 +0200568 _HA_ATOMIC_INC(&sess->fe->fe_counters.denied_req);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100569 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau4781b152021-04-06 13:53:36 +0200570 _HA_ATOMIC_INC(&s->be->be_counters.denied_req);
William Lallemand36119de2021-03-08 15:26:48 +0100571 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200572 _HA_ATOMIC_INC(&sess->listener->counters->denied_req);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100573 goto return_prx_err;
574
Christopher Fauletd649b572022-06-01 17:42:35 +0200575 return_fail_rewrite:
576 if (!(s->flags & SF_ERR_MASK))
577 s->flags |= SF_ERR_PRXCOND;
578 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_rewrites);
579 if (s->flags & SF_BE_ASSIGNED)
580 _HA_ATOMIC_INC(&s->be->be_counters.failed_rewrites);
581 if (sess->listener && sess->listener->counters)
582 _HA_ATOMIC_INC(&sess->listener->counters->failed_rewrites);
583 if (objt_server(s->target))
584 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_rewrites);
585 /* fall through */
586
Christopher Fauletb8a53712019-12-16 11:29:38 +0100587 return_int_err:
588 txn->status = 500;
589 if (!(s->flags & SF_ERR_MASK))
590 s->flags |= SF_ERR_INTERNAL;
Willy Tarreau4781b152021-04-06 13:53:36 +0200591 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100592 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau4781b152021-04-06 13:53:36 +0200593 _HA_ATOMIC_INC(&s->be->be_counters.internal_errors);
William Lallemand36119de2021-03-08 15:26:48 +0100594 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200595 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100596 goto return_prx_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200597
598 return_bad_req:
Christopher Faulete0768eb2018-10-03 16:38:02 +0200599 txn->status = 400;
Willy Tarreau4781b152021-04-06 13:53:36 +0200600 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req);
William Lallemand36119de2021-03-08 15:26:48 +0100601 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200602 _HA_ATOMIC_INC(&sess->listener->counters->failed_req);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100603 /* fall through */
604
605 return_prx_err:
606 http_reply_and_close(s, txn->status, http_error_message(s));
607 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200608
609 return_prx_cond:
Christopher Faulet4a66c942023-01-13 09:43:21 +0100610 http_set_term_flags(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200611
612 req->analysers &= AN_REQ_FLT_END;
613 req->analyse_exp = TICK_ETERNITY;
Christopher Fauletd4150ad2021-10-13 15:35:55 +0200614 s->current_rule = s->current_rule_list = NULL;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100615 DBG_TRACE_DEVEL("leaving on error",
616 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200617 return 0;
618
619 return_prx_yield:
620 channel_dont_connect(req);
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100621 DBG_TRACE_DEVEL("waiting for more data",
622 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200623 return 0;
624}
625
626/* This function performs all the processing enabled for the current request.
627 * It returns 1 if the processing can continue on next analysers, or zero if it
628 * needs more data, encounters an error, or wants to immediately abort the
629 * request. It relies on buffers flags, and updates s->req.analysers.
630 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200631int http_process_request(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200632{
633 struct session *sess = s->sess;
634 struct http_txn *txn = s->txn;
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200635 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200636 struct connection *cli_conn = objt_conn(strm_sess(s)->origin);
637
Christopher Faulet8bebd2f2020-10-06 17:54:56 +0200638 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200639
640 /*
641 * Right now, we know that we have processed the entire headers
642 * and that unwanted requests have been filtered out. We can do
643 * whatever we want with the remaining request. Also, now we
644 * may have separate values for ->fe, ->be.
645 */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100646 htx = htxbuf(&req->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200647
648 /*
Christopher Faulete0768eb2018-10-03 16:38:02 +0200649 * 7: Now we can work with the cookies.
650 * Note that doing so might move headers in the request, but
651 * the fields will stay coherent and the URI will not move.
652 * This should only be performed in the backend.
653 */
654 if (s->be->cookie_name || sess->fe->capture_name)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200655 http_manage_client_side_cookies(s, req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200656
Tim Duesterhus2825b4b2020-02-28 15:13:34 +0100657 /* 8: Generate unique ID if a "unique-id-format" is defined.
658 *
659 * A unique ID is generated even when it is not sent to ensure that the ID can make use of
660 * fetches only available in the HTTP request processing stage.
661 */
662 if (!LIST_ISEMPTY(&sess->fe->format_unique_id)) {
Tim Duesterhusa17e6622020-03-05 20:19:02 +0100663 struct ist unique_id = stream_generate_unique_id(s, &sess->fe->format_unique_id);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200664
Tim Duesterhusa17e6622020-03-05 20:19:02 +0100665 if (!isttest(unique_id)) {
Christopher Fauletb8a53712019-12-16 11:29:38 +0100666 if (!(s->flags & SF_ERR_MASK))
667 s->flags |= SF_ERR_RESOURCE;
668 goto return_int_err;
669 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200670
Tim Duesterhus2825b4b2020-02-28 15:13:34 +0100671 /* send unique ID if a "unique-id-header" is defined */
Tim Duesterhus0643b0e2020-03-05 17:56:35 +0100672 if (isttest(sess->fe->header_unique_id) &&
Tim Duesterhus7ad27d42022-05-18 00:22:15 +0200673 unlikely(!http_add_header(htx, sess->fe->header_unique_id, unique_id)))
Christopher Fauletd649b572022-06-01 17:42:35 +0200674 goto return_fail_rewrite;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200675 }
676
Aurelien DARRAGONb2e2ec52023-01-09 11:09:03 +0100677 /* handle http extensions (if configured) */
678 if (unlikely(!http_handle_7239_header(s, req)))
679 goto return_fail_rewrite;
680 if (unlikely(!http_handle_xff_header(s, req)))
681 goto return_fail_rewrite;
682 if (unlikely(!http_handle_xot_header(s, req)))
683 goto return_fail_rewrite;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200684
Christopher Fauletc2ac5e42021-03-08 18:20:09 +0100685 /* Filter the request headers if there are filters attached to the
686 * stream.
687 */
688 if (HAS_FILTERS(s))
689 req->analysers |= AN_REQ_FLT_HTTP_HDRS;
690
Christopher Faulete0768eb2018-10-03 16:38:02 +0200691 /* If we have no server assigned yet and we're balancing on url_param
692 * with a POST request, we may be interested in checking the body for
693 * that parameter. This will be done in another analyser.
694 */
695 if (!(s->flags & (SF_ASSIGNED|SF_DIRECT)) &&
Willy Tarreau089eaa02019-01-14 15:17:46 +0100696 s->txn->meth == HTTP_METH_POST &&
697 (s->be->lbprm.algo & BE_LB_ALGO) == BE_LB_ALGO_PH) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200698 channel_dont_connect(req);
699 req->analysers |= AN_REQ_HTTP_BODY;
700 }
701
702 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
703 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau1a18b542018-12-11 16:37:42 +0100704
Christopher Faulete0768eb2018-10-03 16:38:02 +0200705 /* We expect some data from the client. Unless we know for sure
706 * we already have a full request, we have to re-enable quick-ack
707 * in case we previously disabled it, otherwise we might cause
708 * the client to delay further data.
709 */
Willy Tarreau9bdcf422023-01-12 19:40:42 +0100710 if ((sess->listener && (sess->listener->bind_conf->options & BC_O_NOQUICKACK)) && !(htx->flags & HTX_FL_EOM))
Willy Tarreau1a18b542018-12-11 16:37:42 +0100711 conn_set_quickack(cli_conn, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200712
713 /*************************************************************
714 * OK, that's finished for the headers. We have done what we *
715 * could. Let's switch to the DATA state. *
716 ************************************************************/
717 req->analyse_exp = TICK_ETERNITY;
718 req->analysers &= ~an_bit;
719
720 s->logs.tv_request = now;
721 /* OK let's go on with the BODY now */
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100722 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200723 return 1;
724
Christopher Fauletd649b572022-06-01 17:42:35 +0200725 return_fail_rewrite:
726 if (!(s->flags & SF_ERR_MASK))
727 s->flags |= SF_ERR_PRXCOND;
728 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_rewrites);
729 if (s->flags & SF_BE_ASSIGNED)
730 _HA_ATOMIC_INC(&s->be->be_counters.failed_rewrites);
731 if (sess->listener && sess->listener->counters)
732 _HA_ATOMIC_INC(&sess->listener->counters->failed_rewrites);
733 if (objt_server(s->target))
734 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_rewrites);
735 /* fall through */
736
Christopher Fauletb8a53712019-12-16 11:29:38 +0100737 return_int_err:
738 txn->status = 500;
739 if (!(s->flags & SF_ERR_MASK))
740 s->flags |= SF_ERR_INTERNAL;
Willy Tarreau4781b152021-04-06 13:53:36 +0200741 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100742 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau4781b152021-04-06 13:53:36 +0200743 _HA_ATOMIC_INC(&s->be->be_counters.internal_errors);
William Lallemand36119de2021-03-08 15:26:48 +0100744 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200745 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100746
Christopher Fauletb8a53712019-12-16 11:29:38 +0100747 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet4a66c942023-01-13 09:43:21 +0100748 http_set_term_flags(s);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100749
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100750 DBG_TRACE_DEVEL("leaving on error",
751 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200752 return 0;
753}
754
755/* This function is an analyser which processes the HTTP tarpit. It always
756 * returns zero, at the beginning because it prevents any other processing
757 * from occurring, and at the end because it terminates the request.
758 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200759int http_process_tarpit(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200760{
761 struct http_txn *txn = s->txn;
762
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100763 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, &txn->req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200764 /* This connection is being tarpitted. The CLIENT side has
765 * already set the connect expiration date to the right
766 * timeout. We just have to check that the client is still
767 * there and that the timeout has not expired.
768 */
769 channel_dont_connect(req);
Christopher Faulet4b490b72023-01-04 11:55:15 +0100770 if (!(req->flags & CF_SHUTR) &&
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100771 !tick_is_expired(req->analyse_exp, now_ms)) {
Christopher Fauletb0c87f12021-10-29 14:37:07 +0200772 /* Be sure to drain all data from the request channel */
773 channel_htx_erase(req, htxbuf(&req->buf));
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100774 DBG_TRACE_DEVEL("waiting for tarpit timeout expiry",
775 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200776 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100777 }
778
Christopher Faulete0768eb2018-10-03 16:38:02 +0200779
780 /* We will set the queue timer to the time spent, just for
781 * logging purposes. We fake a 500 server error, so that the
782 * attacker will not suspect his connection has been tarpitted.
783 * It will not cause trouble to the logs because we can exclude
784 * the tarpitted connections by filtering on the 'PT' status flags.
785 */
786 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
787
Christopher Faulet2e56a732023-01-26 16:18:09 +0100788 http_reply_and_close(s, txn->status, (!sc_ep_test(s->scf, SE_FL_ERROR) ? http_error_message(s) : NULL));
Christopher Faulet4a66c942023-01-13 09:43:21 +0100789 http_set_term_flags(s);
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100790
791 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200792 return 0;
793}
794
795/* This function is an analyser which waits for the HTTP request body. It waits
796 * for either the buffer to be full, or the full advertised contents to have
797 * reached the buffer. It must only be called after the standard HTTP request
798 * processing has occurred, because it expects the request to be parsed and will
799 * look for the Expect header. It may send a 100-Continue interim response. It
800 * takes in input any state starting from HTTP_MSG_BODY and leaves with one of
801 * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it
802 * needs to read more data, or 1 once it has completed its analysis.
803 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200804int http_wait_for_request_body(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200805{
806 struct session *sess = s->sess;
807 struct http_txn *txn = s->txn;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200808
Christopher Fauletf4569bb2023-01-13 10:49:31 +0100809 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, &s->txn->req);
Christopher Fauletf76ebe82018-10-24 11:16:22 +0200810
Christopher Fauletf76ebe82018-10-24 11:16:22 +0200811
Christopher Faulet021a8e42021-03-29 10:46:38 +0200812 switch (http_wait_for_msg_body(s, req, s->be->timeout.httpreq, 0)) {
813 case HTTP_RULE_RES_CONT:
814 goto http_end;
815 case HTTP_RULE_RES_YIELD:
816 goto missing_data_or_waiting;
817 case HTTP_RULE_RES_BADREQ:
Willy Tarreau4236f032019-03-05 10:43:32 +0100818 goto return_bad_req;
Christopher Faulet021a8e42021-03-29 10:46:38 +0200819 case HTTP_RULE_RES_ERROR:
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200820 goto return_int_err;
Christopher Faulet021a8e42021-03-29 10:46:38 +0200821 case HTTP_RULE_RES_ABRT:
Christopher Fauletb8a53712019-12-16 11:29:38 +0100822 goto return_prx_cond;
Christopher Faulet021a8e42021-03-29 10:46:38 +0200823 default:
824 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200825 }
826
827 http_end:
828 /* The situation will not evolve, so let's give up on the analysis. */
829 s->logs.tv_request = now; /* update the request timer to reflect full request */
830 req->analysers &= ~an_bit;
831 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100832 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200833 return 1;
834
Christopher Faulet021a8e42021-03-29 10:46:38 +0200835 missing_data_or_waiting:
836 channel_dont_connect(req);
837 DBG_TRACE_DEVEL("waiting for more data",
838 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
839 return 0;
840
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200841 return_int_err:
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200842 txn->status = 500;
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200843 if (!(s->flags & SF_ERR_MASK))
844 s->flags |= SF_ERR_INTERNAL;
Willy Tarreau4781b152021-04-06 13:53:36 +0200845 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100846 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau4781b152021-04-06 13:53:36 +0200847 _HA_ATOMIC_INC(&s->be->be_counters.internal_errors);
William Lallemand36119de2021-03-08 15:26:48 +0100848 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200849 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Faulet021a8e42021-03-29 10:46:38 +0200850 goto return_prx_err;
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200851
Christopher Faulete0768eb2018-10-03 16:38:02 +0200852 return_bad_req: /* let's centralize all bad requests */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200853 txn->status = 400;
Willy Tarreau4781b152021-04-06 13:53:36 +0200854 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req);
William Lallemand36119de2021-03-08 15:26:48 +0100855 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200856 _HA_ATOMIC_INC(&sess->listener->counters->failed_req);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100857 /* fall through */
858
Christopher Faulet021a8e42021-03-29 10:46:38 +0200859 return_prx_err:
Christopher Fauletb8a53712019-12-16 11:29:38 +0100860 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet021a8e42021-03-29 10:46:38 +0200861 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200862
Christopher Faulet021a8e42021-03-29 10:46:38 +0200863 return_prx_cond:
Christopher Fauletf0d80df2023-01-13 10:20:20 +0100864 http_set_term_flags(s);
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200865
Christopher Faulete0768eb2018-10-03 16:38:02 +0200866 req->analysers &= AN_REQ_FLT_END;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100867 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100868 DBG_TRACE_DEVEL("leaving on error",
869 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200870 return 0;
871}
872
873/* This function is an analyser which forwards request body (including chunk
874 * sizes if any). It is called as soon as we must forward, even if we forward
875 * zero byte. The only situation where it must not be called is when we're in
876 * tunnel mode and we want to forward till the close. It's used both to forward
877 * remaining data and to resync after end of body. It expects the msg_state to
878 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
879 * read more data, or 1 once we can go on with next request or end the stream.
880 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
881 * bytes of pending data + the headers if not already done.
882 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200883int http_request_forward_body(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200884{
885 struct session *sess = s->sess;
886 struct http_txn *txn = s->txn;
Christopher Faulet9768c262018-10-22 09:34:31 +0200887 struct http_msg *msg = &txn->req;
888 struct htx *htx;
Christopher Faulet93e02d82019-03-08 14:18:50 +0100889 short status = 0;
Christopher Fauletaed82cf2018-11-30 22:22:32 +0100890 int ret;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200891
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100892 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200893
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100894 htx = htxbuf(&req->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200895
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200896 if (htx->flags & HTX_FL_PARSING_ERROR)
897 goto return_bad_req;
898 if (htx->flags & HTX_FL_PROCESSING_ERROR)
899 goto return_int_err;
900
Christopher Faulete0768eb2018-10-03 16:38:02 +0200901 /* Note that we don't have to send 100-continue back because we don't
902 * need the data to complete our job, and it's up to the server to
903 * decide whether to return 100, 417 or anything else in return of
904 * an "Expect: 100-continue" header.
905 */
Christopher Faulet9768c262018-10-22 09:34:31 +0200906 if (msg->msg_state == HTTP_MSG_BODY)
907 msg->msg_state = HTTP_MSG_DATA;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200908
Christopher Faulete0768eb2018-10-03 16:38:02 +0200909 /* in most states, we should abort in case of early close */
910 channel_auto_close(req);
911
912 if (req->to_forward) {
Christopher Faulet66af0b22019-03-22 14:54:52 +0100913 if (req->to_forward == CHN_INFINITE_FORWARD) {
Christopher Faulet1a3e0272019-11-15 16:31:46 +0100914 if (req->flags & CF_EOI)
915 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet66af0b22019-03-22 14:54:52 +0100916 }
917 else {
918 /* We can't process the buffer's contents yet */
919 req->flags |= CF_WAKE_WRITE;
920 goto missing_data_or_waiting;
921 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200922 }
923
Christopher Faulet1a3e0272019-11-15 16:31:46 +0100924 if (msg->msg_state >= HTTP_MSG_ENDING)
925 goto ending;
926
927 if (txn->meth == HTTP_METH_CONNECT) {
928 msg->msg_state = HTTP_MSG_ENDING;
929 goto ending;
930 }
931
Christopher Fauletaed82cf2018-11-30 22:22:32 +0100932 /* Forward input data. We get it by removing all outgoing data not
933 * forwarded yet from HTX data size. If there are some data filters, we
934 * let them decide the amount of data to forward.
Christopher Faulet9768c262018-10-22 09:34:31 +0200935 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +0100936 if (HAS_REQ_DATA_FILTERS(s)) {
937 ret = flt_http_payload(s, msg, htx->data);
938 if (ret < 0)
939 goto return_bad_req;
Christopher Faulet421e7692019-06-13 11:16:45 +0200940 c_adv(req, ret);
Christopher Fauletaed82cf2018-11-30 22:22:32 +0100941 }
942 else {
Christopher Faulet421e7692019-06-13 11:16:45 +0200943 c_adv(req, htx->data - co_data(req));
Christopher Faulet2f7c82b2023-02-20 14:06:52 +0100944 if ((global.tune.options & GTUNE_USE_FAST_FWD) && (msg->flags & HTTP_MSGF_XFER_LEN))
Christopher Faulet66af0b22019-03-22 14:54:52 +0100945 channel_htx_forward_forever(req, htx);
Christopher Fauletaed82cf2018-11-30 22:22:32 +0100946 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200947
Christopher Faulet1a3e0272019-11-15 16:31:46 +0100948 if (htx->data != co_data(req))
949 goto missing_data_or_waiting;
Christopher Fauletd20fdb02019-06-13 16:43:22 +0200950
Christopher Faulet9768c262018-10-22 09:34:31 +0200951 /* Check if the end-of-message is reached and if so, switch the message
Christopher Fauletd20fdb02019-06-13 16:43:22 +0200952 * in HTTP_MSG_ENDING state. Then if all data was marked to be
953 * forwarded, set the state to HTTP_MSG_DONE.
Christopher Faulet9768c262018-10-22 09:34:31 +0200954 */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +0100955 if (!(htx->flags & HTX_FL_EOM))
Christopher Faulet9768c262018-10-22 09:34:31 +0200956 goto missing_data_or_waiting;
957
Christopher Fauletd20fdb02019-06-13 16:43:22 +0200958 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet9768c262018-10-22 09:34:31 +0200959
Christopher Faulet1a3e0272019-11-15 16:31:46 +0100960 ending:
Christopher Faulet2151cdd2020-07-22 16:34:59 +0200961 req->flags &= ~CF_EXPECT_MORE; /* no more data are expected */
962
Christopher Faulet1a3e0272019-11-15 16:31:46 +0100963 /* other states, ENDING...TUNNEL */
964 if (msg->msg_state >= HTTP_MSG_DONE)
965 goto done;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200966
Christopher Fauletaed82cf2018-11-30 22:22:32 +0100967 if (HAS_REQ_DATA_FILTERS(s)) {
968 ret = flt_http_end(s, msg);
969 if (ret <= 0) {
970 if (!ret)
971 goto missing_data_or_waiting;
972 goto return_bad_req;
973 }
974 }
975
Christopher Faulet1a3e0272019-11-15 16:31:46 +0100976 if (txn->meth == HTTP_METH_CONNECT)
977 msg->msg_state = HTTP_MSG_TUNNEL;
978 else {
979 msg->msg_state = HTTP_MSG_DONE;
980 req->to_forward = 0;
981 }
982
983 done:
984 /* we don't want to forward closes on DONE except in tunnel mode. */
985 if (!(txn->flags & TX_CON_WANT_TUN))
986 channel_dont_close(req);
987
Christopher Fauletf2b02cf2023-01-13 11:02:28 +0100988 if ((req->flags & CF_SHUTW) && co_data(req)) {
989 /* request errors are most likely due to the server aborting the
990 * transfer. */
991 goto return_srv_abort;
992 }
993
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200994 http_end_request(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200995 if (!(req->analysers & an_bit)) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100996 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200997 return 1;
998 }
999
1000 /* If "option abortonclose" is set on the backend, we want to monitor
1001 * the client's connection and forward any shutdown notification to the
1002 * server, which will decide whether to close or to go on processing the
1003 * request. We only do that in tunnel mode, and not in other modes since
1004 * it can be abused to exhaust source ports. */
Christopher Faulet769d0e92019-03-22 14:23:18 +01001005 if (s->be->options & PR_O_ABRT_CLOSE) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001006 channel_auto_read(req);
Christopher Faulet446d8032022-12-12 07:53:18 +01001007 if ((req->flags & CF_SHUTR) && !(txn->flags & TX_CON_WANT_TUN))
Willy Tarreaucb041662022-05-17 19:44:42 +02001008 s->scb->flags |= SC_FL_NOLINGER;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001009 channel_auto_close(req);
1010 }
1011 else if (s->txn->meth == HTTP_METH_POST) {
1012 /* POST requests may require to read extra CRLF sent by broken
1013 * browsers and which could cause an RST to be sent upon close
1014 * on some systems (eg: Linux). */
1015 channel_auto_read(req);
1016 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001017 DBG_TRACE_DEVEL("waiting for the end of the HTTP txn",
1018 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001019 return 0;
1020
1021 missing_data_or_waiting:
1022 /* stop waiting for data if the input is closed before the end */
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001023 if (msg->msg_state < HTTP_MSG_ENDING && req->flags & CF_SHUTR)
Christopher Faulet93e02d82019-03-08 14:18:50 +01001024 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001025
1026 waiting:
1027 /* waiting for the last bits to leave the buffer */
1028 if (req->flags & CF_SHUTW)
Christopher Faulet93e02d82019-03-08 14:18:50 +01001029 goto return_srv_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001030
1031 /* When TE: chunked is used, we need to get there again to parse remaining
1032 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
1033 * And when content-length is used, we never want to let the possible
1034 * shutdown be forwarded to the other side, as the state machine will
1035 * take care of it once the client responds. It's also important to
1036 * prevent TIME_WAITs from accumulating on the backend side, and for
1037 * HTTP/2 where the last frame comes with a shutdown.
1038 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001039 if (msg->flags & HTTP_MSGF_XFER_LEN)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001040 channel_dont_close(req);
1041
1042 /* We know that more data are expected, but we couldn't send more that
1043 * what we did. So we always set the CF_EXPECT_MORE flag so that the
1044 * system knows it must not set a PUSH on this first part. Interactive
1045 * modes are already handled by the stream sock layer. We must not do
1046 * this in content-length mode because it could present the MSG_MORE
1047 * flag with the last block of forwarded data, which would cause an
1048 * additional delay to be observed by the receiver.
1049 */
Christopher Faulet2151cdd2020-07-22 16:34:59 +02001050 if (HAS_REQ_DATA_FILTERS(s))
Christopher Faulete0768eb2018-10-03 16:38:02 +02001051 req->flags |= CF_EXPECT_MORE;
1052
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001053 DBG_TRACE_DEVEL("waiting for more data to forward",
1054 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001055 return 0;
1056
Christopher Faulet93e02d82019-03-08 14:18:50 +01001057 return_cli_abort:
Willy Tarreau4781b152021-04-06 13:53:36 +02001058 _HA_ATOMIC_INC(&sess->fe->fe_counters.cli_aborts);
1059 _HA_ATOMIC_INC(&s->be->be_counters.cli_aborts);
William Lallemand36119de2021-03-08 15:26:48 +01001060 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001061 _HA_ATOMIC_INC(&sess->listener->counters->cli_aborts);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001062 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001063 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.cli_aborts);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001064 if (!(s->flags & SF_ERR_MASK))
Christopher Fauletf2b02cf2023-01-13 11:02:28 +01001065 s->flags |= ((req->flags & CF_READ_TIMEOUT) ? SF_ERR_CLITO : SF_ERR_CLICL);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001066 status = 400;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001067 goto return_prx_cond;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001068
1069 return_srv_abort:
Willy Tarreau4781b152021-04-06 13:53:36 +02001070 _HA_ATOMIC_INC(&sess->fe->fe_counters.srv_aborts);
1071 _HA_ATOMIC_INC(&s->be->be_counters.srv_aborts);
William Lallemand36119de2021-03-08 15:26:48 +01001072 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001073 _HA_ATOMIC_INC(&sess->listener->counters->srv_aborts);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001074 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001075 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.srv_aborts);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001076 if (!(s->flags & SF_ERR_MASK))
Christopher Fauletf2b02cf2023-01-13 11:02:28 +01001077 s->flags |= ((req->flags & CF_WRITE_TIMEOUT) ? SF_ERR_SRVTO : SF_ERR_SRVCL);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001078 status = 502;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001079 goto return_prx_cond;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001080
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001081 return_int_err:
1082 if (!(s->flags & SF_ERR_MASK))
1083 s->flags |= SF_ERR_INTERNAL;
Willy Tarreau4781b152021-04-06 13:53:36 +02001084 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
1085 _HA_ATOMIC_INC(&s->be->be_counters.internal_errors);
William Lallemand36119de2021-03-08 15:26:48 +01001086 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001087 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001088 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001089 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.internal_errors);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001090 status = 500;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001091 goto return_prx_cond;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001092
Christopher Faulet93e02d82019-03-08 14:18:50 +01001093 return_bad_req:
Willy Tarreau4781b152021-04-06 13:53:36 +02001094 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req);
William Lallemand36119de2021-03-08 15:26:48 +01001095 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001096 _HA_ATOMIC_INC(&sess->listener->counters->failed_req);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001097 status = 400;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001098 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001099
Christopher Fauletb8a53712019-12-16 11:29:38 +01001100 return_prx_cond:
Christopher Faulet9768c262018-10-22 09:34:31 +02001101 if (txn->status > 0) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001102 /* Note: we don't send any error if some data were already sent */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001103 http_reply_and_close(s, txn->status, NULL);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001104 } else {
Christopher Faulet93e02d82019-03-08 14:18:50 +01001105 txn->status = status;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001106 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001107 }
Christopher Fauletf2b02cf2023-01-13 11:02:28 +01001108 http_set_term_flags(s);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001109 DBG_TRACE_DEVEL("leaving on error ",
1110 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001111 return 0;
1112}
1113
Willy Tarreau4596fe22022-05-17 19:07:51 +02001114/* Reset the stream and the backend stream connector to a situation suitable for attemption connection */
Olivier Houcharda254a372019-04-05 15:30:12 +02001115/* Returns 0 if we can attempt to retry, -1 otherwise */
Willy Tarreaub49672d2022-05-27 10:13:37 +02001116static __inline int do_l7_retry(struct stream *s, struct stconn *sc)
Olivier Houcharda254a372019-04-05 15:30:12 +02001117{
Christopher Faulet9f5382e2021-05-21 13:46:14 +02001118 struct channel *req, *res;
1119 int co_data;
Olivier Houcharda254a372019-04-05 15:30:12 +02001120
Christopher Faulet731c8e62022-03-29 16:08:44 +02001121 s->conn_retries++;
1122 if (s->conn_retries >= s->be->conn_retries)
Christopher Faulet552601d2021-05-26 10:31:06 +02001123 return -1;
Christopher Faulet5b82cc52020-10-12 15:18:50 +02001124
Christopher Faulete763c8c2021-05-05 18:23:59 +02001125 if (objt_server(s->target)) {
1126 if (s->flags & SF_CURR_SESS) {
1127 s->flags &= ~SF_CURR_SESS;
1128 _HA_ATOMIC_DEC(&__objt_server(s->target)->cur_sess);
1129 }
Willy Tarreau4781b152021-04-06 13:53:36 +02001130 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.retries);
Christopher Faulete763c8c2021-05-05 18:23:59 +02001131 }
Willy Tarreau4781b152021-04-06 13:53:36 +02001132 _HA_ATOMIC_INC(&s->be->be_counters.retries);
Willy Tarreau223995e2019-05-04 10:38:31 +02001133
Christopher Faulet9f5382e2021-05-21 13:46:14 +02001134 req = &s->req;
1135 res = &s->res;
Olivier Houcharda254a372019-04-05 15:30:12 +02001136 /* Remove any write error from the request, and read error from the response */
Christopher Faulet2e56a732023-01-26 16:18:09 +01001137 req->flags &= ~(CF_WRITE_TIMEOUT | CF_SHUTW | CF_SHUTW_NOW);
1138 res->flags &= ~(CF_READ_TIMEOUT | CF_SHUTR | CF_EOI | CF_READ_EVENT | CF_SHUTR_NOW);
Christopher Faulet7bf46bb2022-01-04 10:56:03 +01001139 res->analysers &= AN_RES_FLT_END;
Christopher Faulet50264b42022-03-30 19:39:30 +02001140 s->conn_err_type = STRM_ET_NONE;
Christopher Fauletae024ce2022-03-29 19:02:31 +02001141 s->flags &= ~(SF_CONN_EXP | SF_ERR_MASK | SF_FINST_MASK);
1142 s->conn_exp = TICK_ETERNITY;
Olivier Houchard4bd58672019-07-12 16:16:59 +02001143 stream_choose_redispatch(s);
Olivier Houcharda254a372019-04-05 15:30:12 +02001144 res->rex = TICK_ETERNITY;
1145 res->to_forward = 0;
1146 res->analyse_exp = TICK_ETERNITY;
1147 res->total = 0;
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +01001148
Willy Tarreau19c65a92022-05-27 08:49:24 +02001149 if (sc_reset_endp(s->scb) < 0) {
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +01001150 if (!(s->flags & SF_ERR_MASK))
1151 s->flags |= SF_ERR_INTERNAL;
1152 return -1;
1153 }
Olivier Houcharda254a372019-04-05 15:30:12 +02001154
Christopher Faulet9f5382e2021-05-21 13:46:14 +02001155 b_free(&req->buf);
1156 /* Swap the L7 buffer with the channel buffer */
1157 /* We know we stored the co_data as b_data, so get it there */
Christopher Faulete05bf9e2022-03-29 15:23:40 +02001158 co_data = b_data(&s->txn->l7_buffer);
1159 b_set_data(&s->txn->l7_buffer, b_size(&s->txn->l7_buffer));
1160 b_xfer(&req->buf, &s->txn->l7_buffer, b_data(&s->txn->l7_buffer));
Christopher Faulet9f5382e2021-05-21 13:46:14 +02001161 co_set_data(req, co_data);
Christopher Faulet5b82cc52020-10-12 15:18:50 +02001162
Ilya Shipitsinacf84592021-02-06 22:29:08 +05001163 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 +02001164
Olivier Houcharda254a372019-04-05 15:30:12 +02001165 b_reset(&res->buf);
1166 co_set_data(res, 0);
1167 return 0;
1168}
1169
Christopher Faulete0768eb2018-10-03 16:38:02 +02001170/* This stream analyser waits for a complete HTTP response. It returns 1 if the
1171 * processing can continue on next analysers, or zero if it either needs more
1172 * data or wants to immediately abort the response (eg: timeout, error, ...). It
1173 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->res.analysers
1174 * when it has nothing left to do, and may remove any analyser when it wants to
1175 * abort.
1176 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001177int http_wait_for_response(struct stream *s, struct channel *rep, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001178{
Christopher Faulet9768c262018-10-22 09:34:31 +02001179 /*
1180 * We will analyze a complete HTTP response to check the its syntax.
1181 *
1182 * Once the start line and all headers are received, we may perform a
1183 * capture of the error (if any), and we will set a few fields. We also
1184 * logging and finally headers capture.
1185 */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001186 struct session *sess = s->sess;
1187 struct http_txn *txn = s->txn;
1188 struct http_msg *msg = &txn->rsp;
Christopher Faulet9768c262018-10-22 09:34:31 +02001189 struct htx *htx;
Christopher Faulet61608322018-11-23 16:23:45 +01001190 struct connection *srv_conn;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001191 struct htx_sl *sl;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001192 int n;
1193
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001194 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001195
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001196 htx = htxbuf(&rep->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001197
Willy Tarreau4236f032019-03-05 10:43:32 +01001198 /* Parsing errors are caught here */
1199 if (htx->flags & HTX_FL_PARSING_ERROR)
1200 goto return_bad_res;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001201 if (htx->flags & HTX_FL_PROCESSING_ERROR)
1202 goto return_int_err;
Willy Tarreau4236f032019-03-05 10:43:32 +01001203
Christopher Faulete0768eb2018-10-03 16:38:02 +02001204 /*
1205 * Now we quickly check if we have found a full valid response.
1206 * If not so, we check the FD and buffer states before leaving.
1207 * A full response is indicated by the fact that we have seen
1208 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
1209 * responses are checked first.
1210 *
1211 * Depending on whether the client is still there or not, we
1212 * may send an error response back or not. Note that normally
1213 * we should only check for HTTP status there, and check I/O
1214 * errors somewhere else.
1215 */
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001216 next_one:
Christopher Faulet29f17582019-05-23 11:03:26 +02001217 if (unlikely(htx_is_empty(htx) || htx->first == -1)) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001218 /* 1: have we encountered a read error ? */
Christopher Faulet2e56a732023-01-26 16:18:09 +01001219 if (sc_ep_test(s->scb, SE_FL_ERROR)) {
Willy Tarreaufd9417b2022-05-18 16:23:22 +02001220 struct connection *conn = sc_conn(s->scb);
Olivier Houchard865d8392019-05-03 22:46:27 +02001221
Christopher Fauletd9769232021-05-26 12:15:37 +02001222 /* Perform a L7 retry because server refuses the early data. */
Christopher Faulete05bf9e2022-03-29 15:23:40 +02001223 if ((txn->flags & TX_L7_RETRY) &&
Christopher Fauletd9769232021-05-26 12:15:37 +02001224 (s->be->retry_type & PR_RE_EARLY_ERROR) &&
1225 conn && conn->err_code == CO_ER_SSL_EARLY_FAILED &&
Willy Tarreau7cb9e6c2022-05-17 19:40:40 +02001226 do_l7_retry(s, s->scb) == 0) {
Christopher Fauletd9769232021-05-26 12:15:37 +02001227 DBG_TRACE_DEVEL("leaving on L7 retry",
1228 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
1229 return 0;
1230 }
1231
Olivier Houchard6db16992019-05-17 15:40:49 +02001232 if (txn->flags & TX_NOT_FIRST)
1233 goto abort_keep_alive;
1234
Willy Tarreau4781b152021-04-06 13:53:36 +02001235 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001236 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001237 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001238 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001239 }
1240
Christopher Fauletd9769232021-05-26 12:15:37 +02001241 /* if the server refused the early data, just send a 425 */
1242 if (conn && conn->err_code == CO_ER_SSL_EARLY_FAILED)
Olivier Houchard865d8392019-05-03 22:46:27 +02001243 txn->status = 425;
Christopher Fauletd9769232021-05-26 12:15:37 +02001244 else {
1245 txn->status = 502;
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001246 stream_inc_http_fail_ctr(s);
Christopher Fauletd9769232021-05-26 12:15:37 +02001247 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001248
Willy Tarreaucb041662022-05-17 19:44:42 +02001249 s->scb->flags |= SC_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001250 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001251
1252 if (!(s->flags & SF_ERR_MASK))
1253 s->flags |= SF_ERR_SRVCL;
Christopher Faulet4a66c942023-01-13 09:43:21 +01001254 http_set_term_flags(s);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001255 DBG_TRACE_DEVEL("leaving on error",
1256 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001257 return 0;
1258 }
1259
Christopher Faulet9768c262018-10-22 09:34:31 +02001260 /* 2: read timeout : return a 504 to the client. */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001261 else if (rep->flags & CF_READ_TIMEOUT) {
Christopher Faulete05bf9e2022-03-29 15:23:40 +02001262 if ((txn->flags & TX_L7_RETRY) &&
Olivier Houcharda254a372019-04-05 15:30:12 +02001263 (s->be->retry_type & PR_RE_TIMEOUT)) {
Willy Tarreau7cb9e6c2022-05-17 19:40:40 +02001264 if (co_data(rep) || do_l7_retry(s, s->scb) == 0) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001265 DBG_TRACE_DEVEL("leaving on L7 retry",
1266 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharda254a372019-04-05 15:30:12 +02001267 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001268 }
Olivier Houcharda254a372019-04-05 15:30:12 +02001269 }
Willy Tarreau4781b152021-04-06 13:53:36 +02001270 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001271 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001272 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001273 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001274 }
1275
Christopher Faulete0768eb2018-10-03 16:38:02 +02001276 txn->status = 504;
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001277 stream_inc_http_fail_ctr(s);
Willy Tarreaucb041662022-05-17 19:44:42 +02001278 s->scb->flags |= SC_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001279 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001280
1281 if (!(s->flags & SF_ERR_MASK))
1282 s->flags |= SF_ERR_SRVTO;
Christopher Faulet4a66c942023-01-13 09:43:21 +01001283 http_set_term_flags(s);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001284 DBG_TRACE_DEVEL("leaving on error",
1285 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001286 return 0;
1287 }
1288
Christopher Faulet9768c262018-10-22 09:34:31 +02001289 /* 3: client abort with an abortonclose */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001290 else if ((rep->flags & CF_SHUTR) && ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001291 _HA_ATOMIC_INC(&sess->fe->fe_counters.cli_aborts);
1292 _HA_ATOMIC_INC(&s->be->be_counters.cli_aborts);
William Lallemand36119de2021-03-08 15:26:48 +01001293 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001294 _HA_ATOMIC_INC(&sess->listener->counters->cli_aborts);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001295 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001296 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.cli_aborts);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001297
Christopher Faulete0768eb2018-10-03 16:38:02 +02001298 txn->status = 400;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001299 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001300
1301 if (!(s->flags & SF_ERR_MASK))
1302 s->flags |= SF_ERR_CLICL;
Christopher Faulet4a66c942023-01-13 09:43:21 +01001303 http_set_term_flags(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001304
1305 /* process_stream() will take care of the error */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001306 DBG_TRACE_DEVEL("leaving on error",
1307 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001308 return 0;
1309 }
1310
Christopher Faulet9768c262018-10-22 09:34:31 +02001311 /* 4: close from server, capture the response if the server has started to respond */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001312 else if (rep->flags & CF_SHUTR) {
Christopher Faulete05bf9e2022-03-29 15:23:40 +02001313 if ((txn->flags & TX_L7_RETRY) &&
Olivier Houcharda254a372019-04-05 15:30:12 +02001314 (s->be->retry_type & PR_RE_DISCONNECTED)) {
Willy Tarreau7cb9e6c2022-05-17 19:40:40 +02001315 if (co_data(rep) || do_l7_retry(s, s->scb) == 0) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001316 DBG_TRACE_DEVEL("leaving on L7 retry",
1317 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharda254a372019-04-05 15:30:12 +02001318 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001319 }
Olivier Houcharda254a372019-04-05 15:30:12 +02001320 }
1321
Olivier Houchard6db16992019-05-17 15:40:49 +02001322 if (txn->flags & TX_NOT_FIRST)
1323 goto abort_keep_alive;
1324
Willy Tarreau4781b152021-04-06 13:53:36 +02001325 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001326 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001327 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001328 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001329 }
1330
Christopher Faulete0768eb2018-10-03 16:38:02 +02001331 txn->status = 502;
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001332 stream_inc_http_fail_ctr(s);
Willy Tarreaucb041662022-05-17 19:44:42 +02001333 s->scb->flags |= SC_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001334 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001335
1336 if (!(s->flags & SF_ERR_MASK))
1337 s->flags |= SF_ERR_SRVCL;
Christopher Faulet4a66c942023-01-13 09:43:21 +01001338 http_set_term_flags(s);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001339 DBG_TRACE_DEVEL("leaving on error",
1340 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001341 return 0;
1342 }
1343
Christopher Faulet9768c262018-10-22 09:34:31 +02001344 /* 5: write error to client (we don't send any message then) */
Christopher Faulet2e56a732023-01-26 16:18:09 +01001345 else if (sc_ep_test(s->scf, SE_FL_ERR_PENDING)) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001346 if (txn->flags & TX_NOT_FIRST)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001347 goto abort_keep_alive;
1348
Willy Tarreau4781b152021-04-06 13:53:36 +02001349 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001350 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001351 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001352 rep->analysers &= AN_RES_FLT_END;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001353
1354 if (!(s->flags & SF_ERR_MASK))
1355 s->flags |= SF_ERR_CLICL;
Christopher Faulet4a66c942023-01-13 09:43:21 +01001356 http_set_term_flags(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001357
1358 /* process_stream() will take care of the error */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001359 DBG_TRACE_DEVEL("leaving on error",
1360 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001361 return 0;
1362 }
1363
1364 channel_dont_close(rep);
1365 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001366 DBG_TRACE_DEVEL("waiting for more data",
1367 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001368 return 0;
1369 }
1370
1371 /* More interesting part now : we know that we have a complete
1372 * response which at least looks like HTTP. We have an indicator
1373 * of each header's length, so we can parse them quickly.
1374 */
Christopher Faulet29f17582019-05-23 11:03:26 +02001375 BUG_ON(htx_get_first_type(htx) != HTX_BLK_RES_SL);
Christopher Faulet297fbb42019-05-13 14:41:27 +02001376 sl = http_get_stline(htx);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001377
Christopher Faulet1f08bff2021-05-26 13:14:39 +02001378 /* Perform a L7 retry because of the status code */
Christopher Faulete05bf9e2022-03-29 15:23:40 +02001379 if ((txn->flags & TX_L7_RETRY) &&
Christopher Faulet1f08bff2021-05-26 13:14:39 +02001380 l7_status_match(s->be, sl->info.res.status) &&
Willy Tarreau7cb9e6c2022-05-17 19:40:40 +02001381 do_l7_retry(s, s->scb) == 0) {
Christopher Faulet1f08bff2021-05-26 13:14:39 +02001382 DBG_TRACE_DEVEL("leaving on L7 retry", STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
1383 return 0;
1384 }
1385
1386 /* Now, L7 buffer is useless, it can be released */
Christopher Faulete05bf9e2022-03-29 15:23:40 +02001387 b_free(&txn->l7_buffer);
Christopher Faulet1f08bff2021-05-26 13:14:39 +02001388
1389 msg->msg_state = HTTP_MSG_BODY;
1390
1391
Christopher Faulet9768c262018-10-22 09:34:31 +02001392 /* 0: we might have to print this header in debug mode */
1393 if (unlikely((global.mode & MODE_DEBUG) &&
1394 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
1395 int32_t pos;
1396
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001397 http_debug_stline("srvrep", s, sl);
Christopher Faulet9768c262018-10-22 09:34:31 +02001398
Christopher Fauleta3f15502019-05-13 15:27:23 +02001399 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001400 struct htx_blk *blk = htx_get_blk(htx, pos);
1401 enum htx_blk_type type = htx_get_blk_type(blk);
1402
1403 if (type == HTX_BLK_EOH)
1404 break;
1405 if (type != HTX_BLK_HDR)
1406 continue;
1407
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001408 http_debug_hdr("srvhdr", s,
1409 htx_get_blk_name(htx, blk),
1410 htx_get_blk_value(htx, blk));
Christopher Faulet9768c262018-10-22 09:34:31 +02001411 }
1412 }
1413
Christopher Faulet03599112018-11-27 11:21:21 +01001414 /* 1: get the status code and the version. Also set HTTP flags */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001415 txn->status = sl->info.res.status;
Christopher Faulet03599112018-11-27 11:21:21 +01001416 if (sl->flags & HTX_SL_F_VER_11)
Christopher Faulet9768c262018-10-22 09:34:31 +02001417 msg->flags |= HTTP_MSGF_VER_11;
Christopher Faulet03599112018-11-27 11:21:21 +01001418 if (sl->flags & HTX_SL_F_XFER_LEN) {
1419 msg->flags |= HTTP_MSGF_XFER_LEN;
Christopher Faulet2a408542020-11-20 14:22:37 +01001420 if (sl->flags & HTX_SL_F_CLEN)
1421 msg->flags |= HTTP_MSGF_CNT_LEN;
1422 else if (sl->flags & HTX_SL_F_CHNK)
1423 msg->flags |= HTTP_MSGF_TE_CHNK;
Christopher Faulet03599112018-11-27 11:21:21 +01001424 }
Christopher Faulet2a408542020-11-20 14:22:37 +01001425 if (sl->flags & HTX_SL_F_BODYLESS)
1426 msg->flags |= HTTP_MSGF_BODYLESS;
Christopher Faulet576c3582021-01-08 15:53:01 +01001427 if (sl->flags & HTX_SL_F_CONN_UPG)
1428 msg->flags |= HTTP_MSGF_CONN_UPG;
Christopher Faulet9768c262018-10-22 09:34:31 +02001429
1430 n = txn->status / 100;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001431 if (n < 1 || n > 5)
1432 n = 0;
Christopher Faulet9768c262018-10-22 09:34:31 +02001433
Christopher Faulete0768eb2018-10-03 16:38:02 +02001434 /* when the client triggers a 4xx from the server, it's most often due
1435 * to a missing object or permission. These events should be tracked
1436 * because if they happen often, it may indicate a brute force or a
1437 * vulnerability scan.
1438 */
1439 if (n == 4)
1440 stream_inc_http_err_ctr(s);
1441
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001442 if (n == 5 && txn->status != 501 && txn->status != 505)
1443 stream_inc_http_fail_ctr(s);
1444
Marcin Deranek3c27dda2020-05-15 18:32:51 +02001445 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001446 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.p.http.rsp[n]);
1447 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.p.http.cum_req);
Marcin Deranek3c27dda2020-05-15 18:32:51 +02001448 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001449
Christopher Faulete0768eb2018-10-03 16:38:02 +02001450 /* Adjust server's health based on status code. Note: status codes 501
1451 * and 505 are triggered on demand by client request, so we must not
1452 * count them as server failures.
1453 */
1454 if (objt_server(s->target)) {
1455 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001456 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_OK);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001457 else
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001458 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_STS);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001459 }
1460
1461 /*
1462 * We may be facing a 100-continue response, or any other informational
1463 * 1xx response which is non-final, in which case this is not the right
1464 * response, and we're waiting for the next one. Let's allow this response
1465 * to go to the client and wait for the next one. There's an exception for
1466 * 101 which is used later in the code to switch protocols.
1467 */
1468 if (txn->status < 200 &&
1469 (txn->status == 100 || txn->status >= 102)) {
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001470 FLT_STRM_CB(s, flt_http_reset(s, msg));
Christopher Faulet421e7692019-06-13 11:16:45 +02001471 htx->first = channel_htx_fwd_headers(rep, htx);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001472 msg->msg_state = HTTP_MSG_RPBEFORE;
Christopher Faulet3499f622019-09-03 15:23:54 +02001473 msg->flags = 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001474 txn->status = 0;
1475 s->logs.t_data = -1; /* was not a response yet */
Christopher Faulet7d518452020-08-31 11:07:07 +02001476 rep->flags |= CF_SEND_DONTWAIT; /* Send ASAP informational messages */
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001477 goto next_one;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001478 }
1479
Christopher Faulet6e6c7b12021-01-08 16:02:05 +01001480 /* A 101-switching-protocols must contains a Connection header with the
1481 * "upgrade" option and the request too. It means both are agree to
1482 * upgrade. It is not so strict because there is no test on the Upgrade
1483 * header content. But it is probably stronger enough for now.
1484 */
1485 if (txn->status == 101 &&
1486 (!(txn->req.flags & HTTP_MSGF_CONN_UPG) || !(txn->rsp.flags & HTTP_MSGF_CONN_UPG)))
1487 goto return_bad_res;
1488
Christopher Faulete0768eb2018-10-03 16:38:02 +02001489 /*
1490 * 2: check for cacheability.
1491 */
1492
1493 switch (txn->status) {
1494 case 200:
1495 case 203:
1496 case 204:
1497 case 206:
1498 case 300:
1499 case 301:
1500 case 404:
1501 case 405:
1502 case 410:
1503 case 414:
1504 case 501:
1505 break;
1506 default:
1507 /* RFC7231#6.1:
1508 * Responses with status codes that are defined as
1509 * cacheable by default (e.g., 200, 203, 204, 206,
1510 * 300, 301, 404, 405, 410, 414, and 501 in this
1511 * specification) can be reused by a cache with
1512 * heuristic expiration unless otherwise indicated
1513 * by the method definition or explicit cache
1514 * controls [RFC7234]; all other status codes are
1515 * not cacheable by default.
1516 */
1517 txn->flags &= ~(TX_CACHEABLE | TX_CACHE_COOK);
1518 break;
1519 }
1520
1521 /*
1522 * 3: we may need to capture headers
1523 */
1524 s->logs.logwait &= ~LW_RESP;
1525 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->res_cap))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001526 http_capture_headers(htx, s->res_cap, sess->fe->rsp_cap);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001527
Christopher Faulet9768c262018-10-22 09:34:31 +02001528 /* Skip parsing if no content length is possible. */
Christopher Fauletc75668e2020-12-07 18:10:32 +01001529 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status >= 200 && txn->status < 300) ||
Christopher Faulete0768eb2018-10-03 16:38:02 +02001530 txn->status == 101)) {
1531 /* Either we've established an explicit tunnel, or we're
1532 * switching the protocol. In both cases, we're very unlikely
1533 * to understand the next protocols. We have to switch to tunnel
1534 * mode, so that we transfer the request and responses then let
1535 * this protocol pass unmodified. When we later implement specific
1536 * parsers for such protocols, we'll want to check the Upgrade
1537 * header which contains information about that protocol for
1538 * responses with status 101 (eg: see RFC2817 about TLS).
1539 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02001540 txn->flags |= TX_CON_WANT_TUN;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001541 }
1542
Christopher Faulet61608322018-11-23 16:23:45 +01001543 /* check for NTML authentication headers in 401 (WWW-Authenticate) and
1544 * 407 (Proxy-Authenticate) responses and set the connection to private
1545 */
Willy Tarreaufd9417b2022-05-18 16:23:22 +02001546 srv_conn = sc_conn(s->scb);
Christopher Faulet61608322018-11-23 16:23:45 +01001547 if (srv_conn) {
1548 struct ist hdr;
1549 struct http_hdr_ctx ctx;
1550
1551 if (txn->status == 401)
1552 hdr = ist("WWW-Authenticate");
1553 else if (txn->status == 407)
1554 hdr = ist("Proxy-Authenticate");
1555 else
1556 goto end;
1557
1558 ctx.blk = NULL;
1559 while (http_find_header(htx, hdr, &ctx, 0)) {
Willy Tarreauf1dcced2020-05-07 19:27:02 +02001560 /* If www-authenticate contains "Negotiate", "Nego2", or "NTLM",
1561 * possibly followed by blanks and a base64 string, the connection
1562 * is private. Since it's a mess to deal with, we only check for
1563 * values starting with "NTLM" or "Nego". Note that often multiple
1564 * headers are sent by the server there.
1565 */
1566 if ((ctx.value.len >= 4 && strncasecmp(ctx.value.ptr, "Nego", 4) == 0) ||
Willy Tarreau49a1d282020-05-07 19:10:15 +02001567 (ctx.value.len >= 4 && strncasecmp(ctx.value.ptr, "NTLM", 4) == 0)) {
Olivier Houchard250031e2019-05-29 15:01:50 +02001568 sess->flags |= SESS_FL_PREFER_LAST;
Christopher Faulet08016ab2020-07-01 16:10:06 +02001569 conn_set_owner(srv_conn, sess, NULL);
Christopher Faulet21ddc742020-07-01 15:26:14 +02001570 conn_set_private(srv_conn);
Ilya Shipitsin6b79f382020-07-23 00:32:55 +05001571 /* If it fail now, the same will be done in mux->detach() callback */
Christopher Faulet08016ab2020-07-01 16:10:06 +02001572 session_add_conn(srv_conn->owner, srv_conn, srv_conn->target);
Willy Tarreauf1dcced2020-05-07 19:27:02 +02001573 break;
Olivier Houchard250031e2019-05-29 15:01:50 +02001574 }
Christopher Faulet61608322018-11-23 16:23:45 +01001575 }
1576 }
1577
1578 end:
Christopher Faulete0768eb2018-10-03 16:38:02 +02001579 /* we want to have the response time before we start processing it */
1580 s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now);
1581
1582 /* end of job, return OK */
1583 rep->analysers &= ~an_bit;
1584 rep->analyse_exp = TICK_ETERNITY;
1585 channel_auto_close(rep);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001586 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001587 return 1;
1588
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001589 return_int_err:
Willy Tarreau4781b152021-04-06 13:53:36 +02001590 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
1591 _HA_ATOMIC_INC(&s->be->be_counters.internal_errors);
William Lallemand36119de2021-03-08 15:26:48 +01001592 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001593 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Fauletb8a53712019-12-16 11:29:38 +01001594 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001595 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.internal_errors);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001596 txn->status = 500;
1597 if (!(s->flags & SF_ERR_MASK))
1598 s->flags |= SF_ERR_INTERNAL;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001599 goto return_prx_cond;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001600
1601 return_bad_res:
Willy Tarreau4781b152021-04-06 13:53:36 +02001602 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Faulet47365272018-10-31 17:40:50 +01001603 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001604 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001605 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Christopher Faulet47365272018-10-31 17:40:50 +01001606 }
Olivier Houcharde3249a92019-05-03 23:01:47 +02001607 if ((s->be->retry_type & PR_RE_JUNK_REQUEST) &&
Christopher Faulete05bf9e2022-03-29 15:23:40 +02001608 (txn->flags & TX_L7_RETRY) &&
Willy Tarreau7cb9e6c2022-05-17 19:40:40 +02001609 do_l7_retry(s, s->scb) == 0) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001610 DBG_TRACE_DEVEL("leaving on L7 retry",
1611 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharde3249a92019-05-03 23:01:47 +02001612 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001613 }
Christopher Faulet47365272018-10-31 17:40:50 +01001614 txn->status = 502;
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001615 stream_inc_http_fail_ctr(s);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001616 /* fall through */
1617
Christopher Fauletb8a53712019-12-16 11:29:38 +01001618 return_prx_cond:
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001619 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet4a66c942023-01-13 09:43:21 +01001620 http_set_term_flags(s);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001621
Willy Tarreaucb041662022-05-17 19:44:42 +02001622 s->scb->flags |= SC_FL_NOLINGER;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001623 DBG_TRACE_DEVEL("leaving on error",
1624 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulet47365272018-10-31 17:40:50 +01001625 return 0;
1626
Christopher Faulete0768eb2018-10-03 16:38:02 +02001627 abort_keep_alive:
1628 /* A keep-alive request to the server failed on a network error.
1629 * The client is required to retry. We need to close without returning
1630 * any other information so that the client retries.
1631 */
1632 txn->status = 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001633 s->logs.logwait = 0;
1634 s->logs.level = 0;
1635 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001636 http_reply_and_close(s, txn->status, NULL);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001637 DBG_TRACE_DEVEL("leaving by closing K/A connection",
1638 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001639 return 0;
1640}
1641
1642/* This function performs all the processing enabled for the current response.
1643 * It normally returns 1 unless it wants to break. It relies on buffers flags,
1644 * and updates s->res.analysers. It might make sense to explode it into several
1645 * other functions. It works like process_request (see indications above).
1646 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001647int http_process_res_common(struct stream *s, struct channel *rep, int an_bit, struct proxy *px)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001648{
1649 struct session *sess = s->sess;
1650 struct http_txn *txn = s->txn;
1651 struct http_msg *msg = &txn->rsp;
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001652 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001653 struct proxy *cur_proxy;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001654 enum rule_result ret = HTTP_RULE_RES_CONT;
1655
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001656 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
1657 return 0;
Christopher Faulet9768c262018-10-22 09:34:31 +02001658
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001659 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001660
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001661 htx = htxbuf(&rep->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001662
1663 /* The stats applet needs to adjust the Connection header but we don't
1664 * apply any filter there.
1665 */
1666 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
1667 rep->analysers &= ~an_bit;
1668 rep->analyse_exp = TICK_ETERNITY;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001669 goto end;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001670 }
1671
1672 /*
1673 * We will have to evaluate the filters.
1674 * As opposed to version 1.2, now they will be evaluated in the
1675 * filters order and not in the header order. This means that
1676 * each filter has to be validated among all headers.
1677 *
1678 * Filters are tried with ->be first, then with ->fe if it is
1679 * different from ->be.
1680 *
1681 * Maybe we are in resume condiion. In this case I choose the
1682 * "struct proxy" which contains the rule list matching the resume
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05001683 * pointer. If none of these "struct proxy" match, I initialise
Christopher Faulete0768eb2018-10-03 16:38:02 +02001684 * the process with the first one.
1685 *
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05001686 * In fact, I check only correspondence between the current list
Christopher Faulete0768eb2018-10-03 16:38:02 +02001687 * pointer and the ->fe rule list. If it doesn't match, I initialize
1688 * the loop with the ->be.
1689 */
Christopher Fauletd4150ad2021-10-13 15:35:55 +02001690 if (s->current_rule_list == &sess->fe->http_res_rules ||
1691 (sess->fe->defpx && s->current_rule_list == &sess->fe->defpx->http_res_rules))
Christopher Faulete0768eb2018-10-03 16:38:02 +02001692 cur_proxy = sess->fe;
1693 else
1694 cur_proxy = s->be;
Christopher Fauletd4150ad2021-10-13 15:35:55 +02001695
Christopher Faulete0768eb2018-10-03 16:38:02 +02001696 while (1) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001697 /* evaluate http-response rules */
Christopher Faulet46f46df2021-11-09 16:33:25 +01001698 if (ret == HTTP_RULE_RES_CONT || ret == HTTP_RULE_RES_STOP) {
Christopher Fauletd4150ad2021-10-13 15:35:55 +02001699 struct list *def_rules, *rules;
1700
1701 def_rules = ((cur_proxy->defpx && (cur_proxy == s->be || cur_proxy->defpx != s->be->defpx)) ? &cur_proxy->defpx->http_res_rules : NULL);
1702 rules = &cur_proxy->http_res_rules;
1703
1704 ret = http_res_get_intercept_rule(cur_proxy, def_rules, rules, s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001705
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001706 switch (ret) {
1707 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
1708 goto return_prx_yield;
1709
1710 case HTTP_RULE_RES_CONT:
1711 case HTTP_RULE_RES_STOP: /* nothing to do */
1712 break;
1713
1714 case HTTP_RULE_RES_DENY: /* deny or tarpit */
1715 goto deny;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001716
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001717 case HTTP_RULE_RES_ABRT: /* abort request, response already sent */
1718 goto return_prx_cond;
1719
1720 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
Christopher Fauletb8a53712019-12-16 11:29:38 +01001721 goto done;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001722
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001723 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
1724 goto return_bad_res;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001725
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001726 case HTTP_RULE_RES_ERROR: /* failed with a bad request */
1727 goto return_int_err;
1728 }
1729
1730 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001731
Christopher Faulete0768eb2018-10-03 16:38:02 +02001732 /* check whether we're already working on the frontend */
1733 if (cur_proxy == sess->fe)
1734 break;
1735 cur_proxy = sess->fe;
1736 }
1737
Christopher Faulete0768eb2018-10-03 16:38:02 +02001738 /* OK that's all we can do for 1xx responses */
1739 if (unlikely(txn->status < 200 && txn->status != 101))
Christopher Fauletf2824e62018-10-01 12:12:37 +02001740 goto end;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001741
1742 /*
1743 * Now check for a server cookie.
1744 */
1745 if (s->be->cookie_name || sess->fe->capture_name || (s->be->options & PR_O_CHK_CACHE))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001746 http_manage_server_side_cookies(s, rep);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001747
1748 /*
1749 * Check for cache-control or pragma headers if required.
1750 */
1751 if ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001752 http_check_response_for_cacheability(s, rep);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001753
1754 /*
1755 * Add server cookie in the response if needed
1756 */
1757 if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) &&
1758 !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) &&
1759 (!(s->flags & SF_DIRECT) ||
1760 ((s->be->cookie_maxidle || txn->cookie_last_date) &&
1761 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
1762 (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
1763 (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
1764 (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
1765 !(s->flags & SF_IGNORE_PRST)) {
1766 /* the server is known, it's not the one the client requested, or the
1767 * cookie's last seen date needs to be refreshed. We have to
1768 * insert a set-cookie here, except if we want to insert only on POST
1769 * requests and this one isn't. Note that servers which don't have cookies
1770 * (eg: some backup servers) will return a full cookie removal request.
1771 */
Willy Tarreau88bc8002021-12-06 07:01:02 +00001772 if (!__objt_server(s->target)->cookie) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001773 chunk_printf(&trash,
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001774 "%s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
Christopher Faulete0768eb2018-10-03 16:38:02 +02001775 s->be->cookie_name);
1776 }
1777 else {
Willy Tarreau88bc8002021-12-06 07:01:02 +00001778 chunk_printf(&trash, "%s=%s", s->be->cookie_name, __objt_server(s->target)->cookie);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001779
1780 if (s->be->cookie_maxidle || s->be->cookie_maxlife) {
1781 /* emit last_date, which is mandatory */
1782 trash.area[trash.data++] = COOKIE_DELIM_DATE;
1783 s30tob64((date.tv_sec+3) >> 2,
1784 trash.area + trash.data);
1785 trash.data += 5;
1786
1787 if (s->be->cookie_maxlife) {
1788 /* emit first_date, which is either the original one or
1789 * the current date.
1790 */
1791 trash.area[trash.data++] = COOKIE_DELIM_DATE;
1792 s30tob64(txn->cookie_first_date ?
1793 txn->cookie_first_date >> 2 :
1794 (date.tv_sec+3) >> 2,
1795 trash.area + trash.data);
1796 trash.data += 5;
1797 }
1798 }
1799 chunk_appendf(&trash, "; path=/");
1800 }
1801
1802 if (s->be->cookie_domain)
1803 chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain);
1804
1805 if (s->be->ck_opts & PR_CK_HTTPONLY)
1806 chunk_appendf(&trash, "; HttpOnly");
1807
1808 if (s->be->ck_opts & PR_CK_SECURE)
1809 chunk_appendf(&trash, "; Secure");
1810
Christopher Faulet2f533902020-01-21 11:06:48 +01001811 if (s->be->cookie_attrs)
1812 chunk_appendf(&trash, "; %s", s->be->cookie_attrs);
1813
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001814 if (unlikely(!http_add_header(htx, ist("Set-Cookie"), ist2(trash.area, trash.data))))
Christopher Fauletd649b572022-06-01 17:42:35 +02001815 goto return_fail_rewrite;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001816
1817 txn->flags &= ~TX_SCK_MASK;
1818 if (__objt_server(s->target)->cookie && (s->flags & SF_DIRECT))
1819 /* the server did not change, only the date was updated */
1820 txn->flags |= TX_SCK_UPDATED;
1821 else
1822 txn->flags |= TX_SCK_INSERTED;
1823
1824 /* Here, we will tell an eventual cache on the client side that we don't
1825 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
1826 * Some caches understand the correct form: 'no-cache="set-cookie"', but
1827 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
1828 */
1829 if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
1830
1831 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
1832
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001833 if (unlikely(!http_add_header(htx, ist("Cache-control"), ist("private"))))
Christopher Fauletd649b572022-06-01 17:42:35 +02001834 goto return_fail_rewrite;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001835 }
1836 }
1837
1838 /*
1839 * Check if result will be cacheable with a cookie.
1840 * We'll block the response if security checks have caught
1841 * nasty things such as a cacheable cookie.
1842 */
1843 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
1844 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
1845 (s->be->options & PR_O_CHK_CACHE)) {
1846 /* we're in presence of a cacheable response containing
1847 * a set-cookie header. We'll block it as requested by
1848 * the 'checkcache' option, and send an alert.
1849 */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001850 ha_alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreau88bc8002021-12-06 07:01:02 +00001851 s->be->id, objt_server(s->target) ? __objt_server(s->target)->id : "<dispatch>");
Christopher Faulete0768eb2018-10-03 16:38:02 +02001852 send_log(s->be, LOG_ALERT,
1853 "Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreau88bc8002021-12-06 07:01:02 +00001854 s->be->id, objt_server(s->target) ? __objt_server(s->target)->id : "<dispatch>");
Christopher Fauletb8a53712019-12-16 11:29:38 +01001855 goto deny;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001856 }
1857
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001858 end:
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01001859 /*
1860 * Evaluate after-response rules before forwarding the response. rules
1861 * from the backend are evaluated first, then one from the frontend if
1862 * it differs.
1863 */
1864 if (!http_eval_after_res_rules(s))
1865 goto return_int_err;
1866
Christopher Fauletc2ac5e42021-03-08 18:20:09 +01001867 /* Filter the response headers if there are filters attached to the
1868 * stream.
1869 */
1870 if (HAS_FILTERS(s))
1871 rep->analysers |= AN_RES_FLT_HTTP_HDRS;
1872
Christopher Faulete0768eb2018-10-03 16:38:02 +02001873 /* Always enter in the body analyzer */
1874 rep->analysers &= ~AN_RES_FLT_XFER_DATA;
1875 rep->analysers |= AN_RES_HTTP_XFER_BODY;
1876
1877 /* if the user wants to log as soon as possible, without counting
1878 * bytes from the server, then this is the right moment. We have
1879 * to temporarily assign bytes_out to log what we currently have.
1880 */
1881 if (!LIST_ISEMPTY(&sess->fe->logformat) && !(s->logs.logwait & LW_BYTES)) {
1882 s->logs.t_close = s->logs.t_data; /* to get a valid end date */
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001883 s->logs.bytes_out = htx->data;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001884 s->do_log(s);
1885 s->logs.bytes_out = 0;
1886 }
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001887
Christopher Fauletb8a53712019-12-16 11:29:38 +01001888 done:
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01001889 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletb8a53712019-12-16 11:29:38 +01001890 rep->analysers &= ~an_bit;
1891 rep->analyse_exp = TICK_ETERNITY;
Christopher Fauletd4150ad2021-10-13 15:35:55 +02001892 s->current_rule = s->current_rule_list = NULL;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001893 return 1;
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001894
Christopher Fauletb8a53712019-12-16 11:29:38 +01001895 deny:
Willy Tarreau4781b152021-04-06 13:53:36 +02001896 _HA_ATOMIC_INC(&sess->fe->fe_counters.denied_resp);
1897 _HA_ATOMIC_INC(&s->be->be_counters.denied_resp);
William Lallemand36119de2021-03-08 15:26:48 +01001898 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001899 _HA_ATOMIC_INC(&sess->listener->counters->denied_resp);
Christopher Fauleta08546b2019-12-16 16:07:34 +01001900 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001901 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.denied_resp);
Christopher Fauletb8a53712019-12-16 11:29:38 +01001902 goto return_prx_err;
1903
Christopher Fauletd649b572022-06-01 17:42:35 +02001904 return_fail_rewrite:
1905 if (!(s->flags & SF_ERR_MASK))
1906 s->flags |= SF_ERR_PRXCOND;
1907 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_rewrites);
1908 _HA_ATOMIC_INC(&s->be->be_counters.failed_rewrites);
1909 if (sess->listener && sess->listener->counters)
1910 _HA_ATOMIC_INC(&sess->listener->counters->failed_rewrites);
1911 if (objt_server(s->target))
1912 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_rewrites);
1913 /* fall through */
1914
Christopher Fauletb8a53712019-12-16 11:29:38 +01001915 return_int_err:
1916 txn->status = 500;
1917 if (!(s->flags & SF_ERR_MASK))
1918 s->flags |= SF_ERR_INTERNAL;
Willy Tarreau4781b152021-04-06 13:53:36 +02001919 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
1920 _HA_ATOMIC_INC(&s->be->be_counters.internal_errors);
Dragan Dosen9a006f92021-09-21 13:02:09 +02001921 if (sess->listener && sess->listener->counters)
1922 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001923 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001924 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.internal_errors);
Christopher Fauletb8a53712019-12-16 11:29:38 +01001925 goto return_prx_err;
1926
1927 return_bad_res:
1928 txn->status = 502;
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001929 stream_inc_http_fail_ctr(s);
Willy Tarreau4781b152021-04-06 13:53:36 +02001930 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Fauleta20a6532020-02-05 10:16:41 +01001931 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001932 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Christopher Fauleta20a6532020-02-05 10:16:41 +01001933 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_RSP);
1934 }
Christopher Fauletb8a53712019-12-16 11:29:38 +01001935 /* fall through */
1936
1937 return_prx_err:
1938 http_reply_and_close(s, txn->status, http_error_message(s));
1939 /* fall through */
1940
1941 return_prx_cond:
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001942 s->logs.t_data = -1; /* was not a valid response */
Willy Tarreaucb041662022-05-17 19:44:42 +02001943 s->scb->flags |= SC_FL_NOLINGER;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001944
Christopher Faulet4a66c942023-01-13 09:43:21 +01001945 http_set_term_flags(s);
Christopher Fauletb8a53712019-12-16 11:29:38 +01001946
Christopher Faulete58c0002020-03-02 16:21:01 +01001947 rep->analysers &= AN_RES_FLT_END;
1948 s->req.analysers &= AN_REQ_FLT_END;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001949 rep->analyse_exp = TICK_ETERNITY;
Christopher Fauletd4150ad2021-10-13 15:35:55 +02001950 s->current_rule = s->current_rule_list = NULL;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001951 DBG_TRACE_DEVEL("leaving on error",
1952 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001953 return 0;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001954
1955 return_prx_yield:
1956 channel_dont_close(rep);
1957 DBG_TRACE_DEVEL("waiting for more data",
1958 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
1959 return 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001960}
1961
1962/* This function is an analyser which forwards response body (including chunk
1963 * sizes if any). It is called as soon as we must forward, even if we forward
1964 * zero byte. The only situation where it must not be called is when we're in
1965 * tunnel mode and we want to forward till the close. It's used both to forward
1966 * remaining data and to resync after end of body. It expects the msg_state to
1967 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
1968 * read more data, or 1 once we can go on with next request or end the stream.
1969 *
1970 * It is capable of compressing response data both in content-length mode and
1971 * in chunked mode. The state machines follows different flows depending on
1972 * whether content-length and chunked modes are used, since there are no
1973 * trailers in content-length :
1974 *
1975 * chk-mode cl-mode
1976 * ,----- BODY -----.
1977 * / \
1978 * V size > 0 V chk-mode
1979 * .--> SIZE -------------> DATA -------------> CRLF
1980 * | | size == 0 | last byte |
1981 * | v final crlf v inspected |
1982 * | TRAILERS -----------> DONE |
1983 * | |
1984 * `----------------------------------------------'
1985 *
1986 * Compression only happens in the DATA state, and must be flushed in final
1987 * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding
1988 * is performed at once on final states for all bytes parsed, or when leaving
1989 * on missing data.
1990 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001991int http_response_forward_body(struct stream *s, struct channel *res, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001992{
1993 struct session *sess = s->sess;
1994 struct http_txn *txn = s->txn;
1995 struct http_msg *msg = &s->txn->rsp;
Christopher Faulet9768c262018-10-22 09:34:31 +02001996 struct htx *htx;
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001997 int ret;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001998
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001999 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002000
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002001 htx = htxbuf(&res->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002002
Christopher Fauletb9a92f32019-09-09 10:15:21 +02002003 if (htx->flags & HTX_FL_PARSING_ERROR)
2004 goto return_bad_res;
2005 if (htx->flags & HTX_FL_PROCESSING_ERROR)
2006 goto return_int_err;
2007
Christopher Faulet9768c262018-10-22 09:34:31 +02002008 if (msg->msg_state == HTTP_MSG_BODY)
2009 msg->msg_state = HTTP_MSG_DATA;
2010
Christopher Faulete0768eb2018-10-03 16:38:02 +02002011 /* in most states, we should abort in case of early close */
2012 channel_auto_close(res);
2013
Christopher Faulete0768eb2018-10-03 16:38:02 +02002014 if (res->to_forward) {
Christopher Faulet66af0b22019-03-22 14:54:52 +01002015 if (res->to_forward == CHN_INFINITE_FORWARD) {
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002016 if (res->flags & CF_EOI)
2017 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet66af0b22019-03-22 14:54:52 +01002018 }
2019 else {
2020 /* We can't process the buffer's contents yet */
2021 res->flags |= CF_WAKE_WRITE;
2022 goto missing_data_or_waiting;
2023 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02002024 }
2025
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002026 if (msg->msg_state >= HTTP_MSG_ENDING)
2027 goto ending;
2028
Christopher Fauletc75668e2020-12-07 18:10:32 +01002029 if ((txn->meth == HTTP_METH_CONNECT && txn->status >= 200 && txn->status < 300) || txn->status == 101 ||
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002030 (!(msg->flags & HTTP_MSGF_XFER_LEN) && !HAS_RSP_DATA_FILTERS(s))) {
2031 msg->msg_state = HTTP_MSG_ENDING;
2032 goto ending;
2033 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002034
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002035 /* Forward input data. We get it by removing all outgoing data not
2036 * forwarded yet from HTX data size. If there are some data filters, we
2037 * let them decide the amount of data to forward.
Christopher Faulet9768c262018-10-22 09:34:31 +02002038 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002039 if (HAS_RSP_DATA_FILTERS(s)) {
2040 ret = flt_http_payload(s, msg, htx->data);
2041 if (ret < 0)
2042 goto return_bad_res;
Christopher Faulet421e7692019-06-13 11:16:45 +02002043 c_adv(res, ret);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002044 }
2045 else {
Christopher Faulet421e7692019-06-13 11:16:45 +02002046 c_adv(res, htx->data - co_data(res));
Christopher Faulet2f7c82b2023-02-20 14:06:52 +01002047 if ((global.tune.options & GTUNE_USE_FAST_FWD) && (msg->flags & HTTP_MSGF_XFER_LEN))
Christopher Faulet66af0b22019-03-22 14:54:52 +01002048 channel_htx_forward_forever(res, htx);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002049 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002050
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002051 if (htx->data != co_data(res))
2052 goto missing_data_or_waiting;
2053
2054 if (!(msg->flags & HTTP_MSGF_XFER_LEN) && res->flags & CF_SHUTR) {
2055 msg->msg_state = HTTP_MSG_ENDING;
2056 goto ending;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002057 }
2058
Christopher Faulet9768c262018-10-22 09:34:31 +02002059 /* Check if the end-of-message is reached and if so, switch the message
Christopher Fauletd20fdb02019-06-13 16:43:22 +02002060 * in HTTP_MSG_ENDING state. Then if all data was marked to be
2061 * forwarded, set the state to HTTP_MSG_DONE.
Christopher Faulet9768c262018-10-22 09:34:31 +02002062 */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002063 if (!(htx->flags & HTX_FL_EOM))
Christopher Faulet9768c262018-10-22 09:34:31 +02002064 goto missing_data_or_waiting;
2065
Christopher Fauletd20fdb02019-06-13 16:43:22 +02002066 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet9768c262018-10-22 09:34:31 +02002067
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002068 ending:
Christopher Faulet2151cdd2020-07-22 16:34:59 +02002069 res->flags &= ~CF_EXPECT_MORE; /* no more data are expected */
2070
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002071 /* other states, ENDING...TUNNEL */
2072 if (msg->msg_state >= HTTP_MSG_DONE)
2073 goto done;
Christopher Faulet9768c262018-10-22 09:34:31 +02002074
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002075 if (HAS_RSP_DATA_FILTERS(s)) {
2076 ret = flt_http_end(s, msg);
2077 if (ret <= 0) {
2078 if (!ret)
2079 goto missing_data_or_waiting;
2080 goto return_bad_res;
2081 }
2082 }
2083
Christopher Fauletc75668e2020-12-07 18:10:32 +01002084 if ((txn->meth == HTTP_METH_CONNECT && txn->status >= 200 && txn->status < 300) || txn->status == 101 ||
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002085 !(msg->flags & HTTP_MSGF_XFER_LEN)) {
2086 msg->msg_state = HTTP_MSG_TUNNEL;
2087 goto ending;
2088 }
2089 else {
2090 msg->msg_state = HTTP_MSG_DONE;
2091 res->to_forward = 0;
2092 }
2093
2094 done:
2095
2096 channel_dont_close(res);
2097
Christopher Fauletf2b02cf2023-01-13 11:02:28 +01002098 if ((res->flags & CF_SHUTW) && co_data(res)) {
2099 /* response errors are most likely due to the client aborting
2100 * the transfer. */
2101 goto return_cli_abort;
2102 }
2103
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002104 http_end_response(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002105 if (!(res->analysers & an_bit)) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002106 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002107 return 1;
2108 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002109 DBG_TRACE_DEVEL("waiting for the end of the HTTP txn",
2110 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002111 return 0;
2112
2113 missing_data_or_waiting:
2114 if (res->flags & CF_SHUTW)
Christopher Faulet93e02d82019-03-08 14:18:50 +01002115 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002116
2117 /* stop waiting for data if the input is closed before the end. If the
2118 * client side was already closed, it means that the client has aborted,
2119 * so we don't want to count this as a server abort. Otherwise it's a
2120 * server abort.
2121 */
Christopher Fauletd20fdb02019-06-13 16:43:22 +02002122 if (msg->msg_state < HTTP_MSG_ENDING && res->flags & CF_SHUTR) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02002123 if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))
Christopher Faulet93e02d82019-03-08 14:18:50 +01002124 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002125 /* If we have some pending data, we continue the processing */
Christopher Faulet93e02d82019-03-08 14:18:50 +01002126 if (htx_is_empty(htx))
2127 goto return_srv_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002128 }
2129
Christopher Faulete0768eb2018-10-03 16:38:02 +02002130 /* When TE: chunked is used, we need to get there again to parse
2131 * remaining chunks even if the server has closed, so we don't want to
Christopher Faulet9768c262018-10-22 09:34:31 +02002132 * set CF_DONTCLOSE. Similarly when there is a content-leng or if there
2133 * are filters registered on the stream, we don't want to forward a
2134 * close
Christopher Faulete0768eb2018-10-03 16:38:02 +02002135 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002136 if ((msg->flags & HTTP_MSGF_XFER_LEN) || HAS_RSP_DATA_FILTERS(s))
Christopher Faulete0768eb2018-10-03 16:38:02 +02002137 channel_dont_close(res);
2138
2139 /* We know that more data are expected, but we couldn't send more that
2140 * what we did. So we always set the CF_EXPECT_MORE flag so that the
2141 * system knows it must not set a PUSH on this first part. Interactive
2142 * modes are already handled by the stream sock layer. We must not do
2143 * this in content-length mode because it could present the MSG_MORE
2144 * flag with the last block of forwarded data, which would cause an
2145 * additional delay to be observed by the receiver.
2146 */
Christopher Faulet2151cdd2020-07-22 16:34:59 +02002147 if (HAS_RSP_DATA_FILTERS(s))
Christopher Faulete0768eb2018-10-03 16:38:02 +02002148 res->flags |= CF_EXPECT_MORE;
2149
2150 /* the stream handler will take care of timeouts and errors */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002151 DBG_TRACE_DEVEL("waiting for more data to forward",
2152 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002153 return 0;
2154
Christopher Faulet93e02d82019-03-08 14:18:50 +01002155 return_srv_abort:
Willy Tarreau4781b152021-04-06 13:53:36 +02002156 _HA_ATOMIC_INC(&sess->fe->fe_counters.srv_aborts);
2157 _HA_ATOMIC_INC(&s->be->be_counters.srv_aborts);
William Lallemand36119de2021-03-08 15:26:48 +01002158 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02002159 _HA_ATOMIC_INC(&sess->listener->counters->srv_aborts);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002160 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02002161 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.srv_aborts);
Willy Tarreau826f3ab2021-02-10 12:07:15 +01002162 stream_inc_http_fail_ctr(s);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002163 if (!(s->flags & SF_ERR_MASK))
Christopher Fauletf2b02cf2023-01-13 11:02:28 +01002164 s->flags |= ((res->flags & CF_READ_TIMEOUT) ? SF_ERR_SRVTO : SF_ERR_SRVCL);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002165 goto return_error;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002166
Christopher Faulet93e02d82019-03-08 14:18:50 +01002167 return_cli_abort:
Willy Tarreau4781b152021-04-06 13:53:36 +02002168 _HA_ATOMIC_INC(&sess->fe->fe_counters.cli_aborts);
2169 _HA_ATOMIC_INC(&s->be->be_counters.cli_aborts);
William Lallemand36119de2021-03-08 15:26:48 +01002170 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02002171 _HA_ATOMIC_INC(&sess->listener->counters->cli_aborts);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002172 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02002173 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.cli_aborts);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002174 if (!(s->flags & SF_ERR_MASK))
Christopher Fauletf2b02cf2023-01-13 11:02:28 +01002175 s->flags |= ((res->flags & CF_WRITE_TIMEOUT) ? SF_ERR_CLITO : SF_ERR_CLICL);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002176 goto return_error;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002177
Christopher Fauletb9a92f32019-09-09 10:15:21 +02002178 return_int_err:
Willy Tarreau4781b152021-04-06 13:53:36 +02002179 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
2180 _HA_ATOMIC_INC(&s->be->be_counters.internal_errors);
William Lallemand36119de2021-03-08 15:26:48 +01002181 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02002182 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002183 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02002184 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.internal_errors);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02002185 if (!(s->flags & SF_ERR_MASK))
2186 s->flags |= SF_ERR_INTERNAL;
2187 goto return_error;
2188
Christopher Faulet93e02d82019-03-08 14:18:50 +01002189 return_bad_res:
Willy Tarreau4781b152021-04-06 13:53:36 +02002190 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002191 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02002192 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002193 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_RSP);
2194 }
Willy Tarreau826f3ab2021-02-10 12:07:15 +01002195 stream_inc_http_fail_ctr(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002196 if (!(s->flags & SF_ERR_MASK))
Christopher Faulet93e02d82019-03-08 14:18:50 +01002197 s->flags |= SF_ERR_SRVCL;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002198 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +02002199
Christopher Faulet93e02d82019-03-08 14:18:50 +01002200 return_error:
Christopher Faulete0768eb2018-10-03 16:38:02 +02002201 /* don't send any error message as we're in the body */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002202 http_reply_and_close(s, txn->status, NULL);
Christopher Fauletf2b02cf2023-01-13 11:02:28 +01002203 http_set_term_flags(s);
2204 stream_inc_http_fail_ctr(s);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002205 DBG_TRACE_DEVEL("leaving on error",
2206 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002207 return 0;
2208}
2209
Christopher Fauletf2824e62018-10-01 12:12:37 +02002210/* Perform an HTTP redirect based on the information in <rule>. The function
Willy Tarreaubc1223b2021-09-02 16:54:33 +02002211 * returns zero in case of an irrecoverable error such as too large a request
2212 * to build a valid response, 1 in case of successful redirect (hence the rule
2213 * is final), or 2 if the rule has to be silently skipped.
Christopher Fauletf2824e62018-10-01 12:12:37 +02002214 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002215int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn)
Christopher Fauletf2824e62018-10-01 12:12:37 +02002216{
Christopher Faulet99daf282018-11-28 22:58:13 +01002217 struct channel *req = &s->req;
2218 struct channel *res = &s->res;
2219 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01002220 struct htx_sl *sl;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002221 struct buffer *chunk;
Christopher Faulet99daf282018-11-28 22:58:13 +01002222 struct ist status, reason, location;
2223 unsigned int flags;
Christopher Fauleteab17572022-04-26 20:34:38 +02002224 int ret = 1, close = 0; /* Try to keep the connection alive byt default */
Christopher Fauletf2824e62018-10-01 12:12:37 +02002225
2226 chunk = alloc_trash_chunk();
Christopher Fauletb8a53712019-12-16 11:29:38 +01002227 if (!chunk) {
2228 if (!(s->flags & SF_ERR_MASK))
2229 s->flags |= SF_ERR_RESOURCE;
Christopher Faulet99daf282018-11-28 22:58:13 +01002230 goto fail;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002231 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002232
Christopher Faulet99daf282018-11-28 22:58:13 +01002233 /*
2234 * Create the location
2235 */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002236 htx = htxbuf(&req->buf);
Christopher Fauletf2824e62018-10-01 12:12:37 +02002237 switch(rule->type) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002238 case REDIRECT_TYPE_SCHEME: {
2239 struct http_hdr_ctx ctx;
2240 struct ist path, host;
Amaury Denoyellec453f952021-07-06 11:40:12 +02002241 struct http_uri_parser parser;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002242
Christopher Faulet99daf282018-11-28 22:58:13 +01002243 host = ist("");
2244 ctx.blk = NULL;
2245 if (http_find_header(htx, ist("Host"), &ctx, 0))
2246 host = ctx.value;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002247
Christopher Faulet297fbb42019-05-13 14:41:27 +02002248 sl = http_get_stline(htx);
Amaury Denoyellec453f952021-07-06 11:40:12 +02002249 parser = http_uri_parser_init(htx_sl_req_uri(sl));
2250 path = http_parse_path(&parser);
Christopher Faulet99daf282018-11-28 22:58:13 +01002251 /* build message using path */
Tim Duesterhused526372020-03-05 17:56:33 +01002252 if (isttest(path)) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002253 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2254 int qs = 0;
2255 while (qs < path.len) {
2256 if (*(path.ptr + qs) == '?') {
2257 path.len = qs;
2258 break;
2259 }
2260 qs++;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002261 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002262 }
2263 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002264 else
2265 path = ist("/");
Christopher Fauletf2824e62018-10-01 12:12:37 +02002266
Christopher Faulet99daf282018-11-28 22:58:13 +01002267 if (rule->rdr_str) { /* this is an old "redirect" rule */
2268 /* add scheme */
2269 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2270 goto fail;
2271 }
2272 else {
2273 /* add scheme with executing log format */
2274 chunk->data += build_logline(s, chunk->area + chunk->data,
2275 chunk->size - chunk->data,
2276 &rule->rdr_fmt);
2277 }
2278 /* add "://" + host + path */
2279 if (!chunk_memcat(chunk, "://", 3) ||
2280 !chunk_memcat(chunk, host.ptr, host.len) ||
2281 !chunk_memcat(chunk, path.ptr, path.len))
2282 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002283
Christopher Faulet99daf282018-11-28 22:58:13 +01002284 /* append a slash at the end of the location if needed and missing */
2285 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
2286 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
2287 if (chunk->data + 1 >= chunk->size)
2288 goto fail;
2289 chunk->area[chunk->data++] = '/';
2290 }
2291 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002292 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002293
Christopher Faulet99daf282018-11-28 22:58:13 +01002294 case REDIRECT_TYPE_PREFIX: {
2295 struct ist path;
Amaury Denoyellec453f952021-07-06 11:40:12 +02002296 struct http_uri_parser parser;
Christopher Faulet99daf282018-11-28 22:58:13 +01002297
Christopher Faulet297fbb42019-05-13 14:41:27 +02002298 sl = http_get_stline(htx);
Amaury Denoyellec453f952021-07-06 11:40:12 +02002299 parser = http_uri_parser_init(htx_sl_req_uri(sl));
2300 path = http_parse_path(&parser);
Christopher Faulet99daf282018-11-28 22:58:13 +01002301 /* build message using path */
Tim Duesterhused526372020-03-05 17:56:33 +01002302 if (isttest(path)) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002303 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2304 int qs = 0;
2305 while (qs < path.len) {
2306 if (*(path.ptr + qs) == '?') {
2307 path.len = qs;
2308 break;
2309 }
2310 qs++;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002311 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002312 }
2313 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002314 else
2315 path = ist("/");
Christopher Fauletf2824e62018-10-01 12:12:37 +02002316
Christopher Faulet99daf282018-11-28 22:58:13 +01002317 if (rule->rdr_str) { /* this is an old "redirect" rule */
2318 /* add prefix. Note that if prefix == "/", we don't want to
2319 * add anything, otherwise it makes it hard for the user to
2320 * configure a self-redirection.
2321 */
2322 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
2323 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2324 goto fail;
2325 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002326 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002327 else {
2328 /* add prefix with executing log format */
2329 chunk->data += build_logline(s, chunk->area + chunk->data,
2330 chunk->size - chunk->data,
2331 &rule->rdr_fmt);
2332 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002333
Christopher Faulet99daf282018-11-28 22:58:13 +01002334 /* add path */
2335 if (!chunk_memcat(chunk, path.ptr, path.len))
2336 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002337
Christopher Faulet99daf282018-11-28 22:58:13 +01002338 /* append a slash at the end of the location if needed and missing */
2339 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
2340 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
2341 if (chunk->data + 1 >= chunk->size)
2342 goto fail;
2343 chunk->area[chunk->data++] = '/';
2344 }
2345 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002346 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002347 case REDIRECT_TYPE_LOCATION:
2348 default:
2349 if (rule->rdr_str) { /* this is an old "redirect" rule */
2350 /* add location */
2351 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2352 goto fail;
2353 }
2354 else {
2355 /* add location with executing log format */
Willy Tarreaubc1223b2021-09-02 16:54:33 +02002356 int len = build_logline(s, chunk->area + chunk->data,
2357 chunk->size - chunk->data,
2358 &rule->rdr_fmt);
Christopher Fauleteab17572022-04-26 20:34:38 +02002359 if (!len && rule->flags & REDIRECT_FLAG_IGNORE_EMPTY) {
2360 ret = 2;
2361 goto out;
2362 }
Willy Tarreaubc1223b2021-09-02 16:54:33 +02002363
2364 chunk->data += len;
Christopher Faulet99daf282018-11-28 22:58:13 +01002365 }
2366 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002367 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002368 location = ist2(chunk->area, chunk->data);
2369
2370 /*
2371 * Create the 30x response
2372 */
2373 switch (rule->code) {
2374 case 308:
2375 status = ist("308");
2376 reason = ist("Permanent Redirect");
2377 break;
2378 case 307:
2379 status = ist("307");
2380 reason = ist("Temporary Redirect");
2381 break;
2382 case 303:
2383 status = ist("303");
2384 reason = ist("See Other");
2385 break;
2386 case 301:
2387 status = ist("301");
2388 reason = ist("Moved Permanently");
2389 break;
2390 case 302:
2391 default:
2392 status = ist("302");
2393 reason = ist("Found");
2394 break;
2395 }
2396
Christopher Faulet08e66462019-05-23 16:44:59 +02002397 if (!(txn->req.flags & HTTP_MSGF_BODYLESS) && txn->req.msg_state != HTTP_MSG_DONE)
2398 close = 1;
2399
Christopher Faulet99daf282018-11-28 22:58:13 +01002400 htx = htx_from_buf(&res->buf);
Kevin Zhu96b36392020-01-07 09:42:55 +01002401 /* Trim any possible response */
2402 channel_htx_truncate(&s->res, htx);
Christopher Faulet99daf282018-11-28 22:58:13 +01002403 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
2404 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), status, reason);
2405 if (!sl)
2406 goto fail;
2407 sl->info.res.status = rule->code;
2408 s->txn->status = rule->code;
2409
Christopher Faulet08e66462019-05-23 16:44:59 +02002410 if (close && !htx_add_header(htx, ist("Connection"), ist("close")))
2411 goto fail;
2412
2413 if (!htx_add_header(htx, ist("Content-length"), ist("0")) ||
Christopher Faulet99daf282018-11-28 22:58:13 +01002414 !htx_add_header(htx, ist("Location"), location))
2415 goto fail;
2416
2417 if (rule->code == 302 || rule->code == 303 || rule->code == 307) {
2418 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")))
2419 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002420 }
2421
2422 if (rule->cookie_len) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002423 if (!htx_add_header(htx, ist("Set-Cookie"), ist2(rule->cookie_str, rule->cookie_len)))
2424 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002425 }
2426
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002427 if (!htx_add_endof(htx, HTX_BLK_EOH))
Christopher Faulet99daf282018-11-28 22:58:13 +01002428 goto fail;
2429
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002430 htx->flags |= HTX_FL_EOM;
Kevin Zhu96b36392020-01-07 09:42:55 +01002431 htx_to_buf(htx, &res->buf);
Christopher Fauleta72a7e42020-01-28 09:28:11 +01002432 if (!http_forward_proxy_resp(s, 1))
2433 goto fail;
Christopher Faulet99daf282018-11-28 22:58:13 +01002434
Christopher Faulet60b33a52020-01-28 09:18:10 +01002435 if (rule->flags & REDIRECT_FLAG_FROM_REQ) {
2436 /* let's log the request time */
2437 s->logs.tv_request = now;
Christopher Fauletd3475882021-10-04 14:16:46 +02002438 req->analysers &= AN_REQ_FLT_END;
Christopher Faulet99daf282018-11-28 22:58:13 +01002439
Christopher Faulet60b33a52020-01-28 09:18:10 +01002440 if (s->sess->fe == s->be) /* report it if the request was intercepted by the frontend */
Willy Tarreau4781b152021-04-06 13:53:36 +02002441 _HA_ATOMIC_INC(&s->sess->fe->fe_counters.intercepted_req);
Christopher Faulet60b33a52020-01-28 09:18:10 +01002442 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002443
2444 if (!(s->flags & SF_ERR_MASK))
2445 s->flags |= SF_ERR_LOCAL;
Christopher Faulet4a66c942023-01-13 09:43:21 +01002446 http_set_term_flags(s);
Christopher Fauletf2824e62018-10-01 12:12:37 +02002447
Christopher Fauleteab17572022-04-26 20:34:38 +02002448 out:
Christopher Faulet99daf282018-11-28 22:58:13 +01002449 free_trash_chunk(chunk);
Christopher Fauleteab17572022-04-26 20:34:38 +02002450 return ret;
Christopher Faulet99daf282018-11-28 22:58:13 +01002451
2452 fail:
2453 /* If an error occurred, remove the incomplete HTTP response from the
2454 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01002455 channel_htx_truncate(res, htxbuf(&res->buf));
Christopher Fauleteab17572022-04-26 20:34:38 +02002456 ret = 0;
2457 goto out;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002458}
2459
Christopher Faulet18c13d32022-05-16 11:43:10 +02002460/* This function filters the request header names to only allow [0-9a-zA-Z-]
2461 * characters. Depending on the proxy configuration, headers with a name not
2462 * matching this charset are removed or the request is rejected with a
2463 * 403-Forbidden response if such name are found. It returns HTTP_RULE_RES_CONT
2464 * to continue the request processing or HTTP_RULE_RES_DENY if the request is
2465 * rejected.
2466 */
2467static enum rule_result http_req_restrict_header_names(struct stream *s, struct htx *htx, struct proxy *px)
2468{
2469 struct htx_blk *blk;
2470 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
2471
2472 blk = htx_get_first_blk(htx);
2473 while (blk) {
2474 enum htx_blk_type type = htx_get_blk_type(blk);
2475
2476 if (type == HTX_BLK_HDR) {
2477 struct ist n = htx_get_blk_name(htx, blk);
Mateusz Malek4b85a962022-08-17 14:22:09 +02002478 int i, end = istlen(n);
Christopher Faulet18c13d32022-05-16 11:43:10 +02002479
Mateusz Malek4b85a962022-08-17 14:22:09 +02002480 for (i = 0; i < end; i++) {
Christopher Faulet18c13d32022-05-16 11:43:10 +02002481 if (!isalnum((unsigned char)n.ptr[i]) && n.ptr[i] != '-') {
Mateusz Malek4b85a962022-08-17 14:22:09 +02002482 break;
Christopher Faulet18c13d32022-05-16 11:43:10 +02002483 }
2484 }
Mateusz Malek4b85a962022-08-17 14:22:09 +02002485
2486 if (i < end) {
2487 /* Disallowed character found - block the request or remove the header */
2488 if (px->options2 & PR_O2_RSTRICT_REQ_HDR_NAMES_BLK)
2489 goto block;
2490 blk = htx_remove_blk(htx, blk);
2491 continue;
2492 }
Christopher Faulet18c13d32022-05-16 11:43:10 +02002493 }
2494 if (type == HTX_BLK_EOH)
2495 break;
2496
2497 blk = htx_get_next_blk(htx, blk);
2498 }
2499 out:
2500 return rule_ret;
2501 block:
2502 /* Block the request returning a 403-Forbidden response */
2503 s->txn->status = 403;
2504 rule_ret = HTTP_RULE_RES_DENY;
2505 goto out;
2506}
2507
Christopher Faulet92d34fe2019-12-17 09:20:34 +01002508/* Replace all headers matching the name <name>. The header value is replaced if
2509 * it matches the regex <re>. <str> is used for the replacement. If <full> is
2510 * set to 1, the full-line is matched and replaced. Otherwise, comma-separated
2511 * values are evaluated one by one. It returns 0 on success and -1 on error.
2512 */
2513int http_replace_hdrs(struct stream* s, struct htx *htx, struct ist name,
2514 const char *str, struct my_regex *re, int full)
Christopher Faulet72333522018-10-24 11:25:02 +02002515{
2516 struct http_hdr_ctx ctx;
2517 struct buffer *output = get_trash_chunk();
2518
Christopher Faulet72333522018-10-24 11:25:02 +02002519 ctx.blk = NULL;
Christopher Faulet92d34fe2019-12-17 09:20:34 +01002520 while (http_find_header(htx, name, &ctx, full)) {
Christopher Faulet72333522018-10-24 11:25:02 +02002521 if (!regex_exec_match2(re, ctx.value.ptr, ctx.value.len, MAX_MATCH, pmatch, 0))
2522 continue;
2523
2524 output->data = exp_replace(output->area, output->size, ctx.value.ptr, str, pmatch);
2525 if (output->data == -1)
2526 return -1;
2527 if (!http_replace_header_value(htx, &ctx, ist2(output->area, output->data)))
2528 return -1;
2529 }
2530 return 0;
2531}
2532
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002533/* This function executes one of the set-{method,path,query,uri} actions. It
2534 * takes the string from the variable 'replace' with length 'len', then modifies
2535 * the relevant part of the request line accordingly. Then it updates various
2536 * pointers to the next elements which were moved, and the total buffer length.
2537 * It finds the action to be performed in p[2], previously filled by function
2538 * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal
2539 * error, though this can be revisited when this code is finally exploited.
2540 *
2541 * 'action' can be '0' to replace method, '1' to replace path, '2' to replace
Christopher Faulet312294f2020-09-02 17:17:44 +02002542 * query string, 3 to replace uri or 4 to replace the path+query.
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002543 *
2544 * In query string case, the mark question '?' must be set at the start of the
2545 * string by the caller, event if the replacement query string is empty.
2546 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002547int http_req_replace_stline(int action, const char *replace, int len,
2548 struct proxy *px, struct stream *s)
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002549{
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002550 struct htx *htx = htxbuf(&s->req.buf);
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002551
2552 switch (action) {
2553 case 0: // method
2554 if (!http_replace_req_meth(htx, ist2(replace, len)))
2555 return -1;
2556 break;
2557
2558 case 1: // path
Christopher Fauletb8ce5052020-08-31 16:11:57 +02002559 if (!http_replace_req_path(htx, ist2(replace, len), 0))
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002560 return -1;
2561 break;
2562
2563 case 2: // query
2564 if (!http_replace_req_query(htx, ist2(replace, len)))
2565 return -1;
2566 break;
2567
2568 case 3: // uri
2569 if (!http_replace_req_uri(htx, ist2(replace, len)))
2570 return -1;
2571 break;
2572
Christopher Faulet312294f2020-09-02 17:17:44 +02002573 case 4: // path + query
2574 if (!http_replace_req_path(htx, ist2(replace, len), 1))
2575 return -1;
2576 break;
2577
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002578 default:
2579 return -1;
2580 }
2581 return 0;
2582}
2583
2584/* This function replace the HTTP status code and the associated message. The
Christopher Faulete00d06c2019-12-16 17:18:42 +01002585 * variable <status> contains the new status code. This function never fails. It
2586 * returns 0 in case of success, -1 in case of internal error.
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002587 */
Christopher Faulet96bff762019-12-17 13:46:18 +01002588int http_res_set_status(unsigned int status, struct ist reason, struct stream *s)
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002589{
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002590 struct htx *htx = htxbuf(&s->res.buf);
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002591 char *res;
2592
2593 chunk_reset(&trash);
2594 res = ultoa_o(status, trash.area, trash.size);
2595 trash.data = res - trash.area;
2596
2597 /* Do we have a custom reason format string? */
Tim Duesterhuse296d3e2020-03-05 17:56:31 +01002598 if (!isttest(reason)) {
Christopher Faulet96bff762019-12-17 13:46:18 +01002599 const char *str = http_get_reason(status);
Tim Duesterhusdcf753a2021-03-04 17:31:47 +01002600 reason = ist(str);
Christopher Faulet96bff762019-12-17 13:46:18 +01002601 }
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002602
Christopher Fauletbde2c4c2020-08-31 16:43:34 +02002603 if (!http_replace_res_status(htx, ist2(trash.area, trash.data), reason))
Christopher Faulete00d06c2019-12-16 17:18:42 +01002604 return -1;
Willy Tarreau640e2532023-01-10 14:50:44 +01002605 s->txn->status = status;
Christopher Faulete00d06c2019-12-16 17:18:42 +01002606 return 0;
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002607}
2608
Christopher Faulet3e964192018-10-24 11:39:23 +02002609/* Executes the http-request rules <rules> for stream <s>, proxy <px> and
2610 * transaction <txn>. Returns the verdict of the first rule that prevents
2611 * further processing of the request (auth, deny, ...), and defaults to
2612 * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or
2613 * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT
2614 * on txn->flags if it encounters a tarpit rule. If <deny_status> is not NULL
2615 * and a deny/tarpit rule is matched, it will be filled with this rule's deny
2616 * status.
2617 */
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002618static enum rule_result http_req_get_intercept_rule(struct proxy *px, struct list *def_rules,
2619 struct list *rules, struct stream *s)
Christopher Faulet3e964192018-10-24 11:39:23 +02002620{
2621 struct session *sess = strm_sess(s);
2622 struct http_txn *txn = s->txn;
Christopher Faulet3e964192018-10-24 11:39:23 +02002623 struct act_rule *rule;
Christopher Faulet3e964192018-10-24 11:39:23 +02002624 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002625 int act_opts = 0;
Christopher Faulet3e964192018-10-24 11:39:23 +02002626
Christopher Faulet3e964192018-10-24 11:39:23 +02002627 /* If "the current_rule_list" match the executed rule list, we are in
2628 * resume condition. If a resume is needed it is always in the action
2629 * and never in the ACL or converters. In this case, we initialise the
2630 * current rule, and go to the action execution point.
2631 */
2632 if (s->current_rule) {
2633 rule = s->current_rule;
2634 s->current_rule = NULL;
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002635 if (s->current_rule_list == rules || (def_rules && s->current_rule_list == def_rules))
Christopher Faulet3e964192018-10-24 11:39:23 +02002636 goto resume_execution;
2637 }
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002638 s->current_rule_list = ((!def_rules || s->current_rule_list == def_rules) ? rules : def_rules);
Christopher Faulet3e964192018-10-24 11:39:23 +02002639
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002640 restart:
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002641 /* start the ruleset evaluation in strict mode */
2642 txn->req.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01002643
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002644 list_for_each_entry(rule, s->current_rule_list, list) {
Christopher Faulet3e964192018-10-24 11:39:23 +02002645 /* check optional condition */
2646 if (rule->cond) {
2647 int ret;
2648
2649 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
2650 ret = acl_pass(ret);
2651
2652 if (rule->cond->pol == ACL_COND_UNLESS)
2653 ret = !ret;
2654
2655 if (!ret) /* condition not matched */
2656 continue;
2657 }
2658
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002659 act_opts |= ACT_OPT_FIRST;
Christopher Faulet3e964192018-10-24 11:39:23 +02002660 resume_execution:
Amaury Denoyelle03517732021-05-07 14:25:01 +02002661 if (rule->kw->flags & KWF_EXPERIMENTAL)
2662 mark_tainted(TAINTED_ACTION_EXP_EXECUTED);
2663
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002664 /* Always call the action function if defined */
2665 if (rule->action_ptr) {
Christopher Faulet2e56a732023-01-26 16:18:09 +01002666 if (sc_ep_test(s->scf, SE_FL_ERROR) ||
Christopher Faulet446d8032022-12-12 07:53:18 +01002667 ((s->req.flags & CF_SHUTR) &&
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002668 (px->options & PR_O_ABRT_CLOSE)))
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002669 act_opts |= ACT_OPT_FINAL;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002670
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002671 switch (rule->action_ptr(rule, px, sess, s, act_opts)) {
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002672 case ACT_RET_CONT:
2673 break;
2674 case ACT_RET_STOP:
2675 rule_ret = HTTP_RULE_RES_STOP;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002676 s->last_rule_file = rule->conf.file;
2677 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002678 goto end;
2679 case ACT_RET_YIELD:
2680 s->current_rule = rule;
2681 rule_ret = HTTP_RULE_RES_YIELD;
2682 goto end;
2683 case ACT_RET_ERR:
2684 rule_ret = HTTP_RULE_RES_ERROR;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002685 s->last_rule_file = rule->conf.file;
2686 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002687 goto end;
2688 case ACT_RET_DONE:
2689 rule_ret = HTTP_RULE_RES_DONE;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002690 s->last_rule_file = rule->conf.file;
2691 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002692 goto end;
2693 case ACT_RET_DENY:
Christopher Fauletb58f62b2020-01-13 16:40:13 +01002694 if (txn->status == -1)
2695 txn->status = 403;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002696 rule_ret = HTTP_RULE_RES_DENY;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002697 s->last_rule_file = rule->conf.file;
2698 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002699 goto end;
2700 case ACT_RET_ABRT:
2701 rule_ret = HTTP_RULE_RES_ABRT;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002702 s->last_rule_file = rule->conf.file;
2703 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002704 goto end;
2705 case ACT_RET_INV:
2706 rule_ret = HTTP_RULE_RES_BADREQ;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002707 s->last_rule_file = rule->conf.file;
2708 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002709 goto end;
2710 }
2711 continue; /* eval the next rule */
2712 }
2713
2714 /* If not action function defined, check for known actions */
Christopher Faulet3e964192018-10-24 11:39:23 +02002715 switch (rule->action) {
2716 case ACT_ACTION_ALLOW:
2717 rule_ret = HTTP_RULE_RES_STOP;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002718 s->last_rule_file = rule->conf.file;
2719 s->last_rule_line = rule->conf.line;
Christopher Faulet3e964192018-10-24 11:39:23 +02002720 goto end;
2721
2722 case ACT_ACTION_DENY:
Christopher Faulet5cb513a2020-05-13 17:56:56 +02002723 txn->status = rule->arg.http_reply->status;
2724 txn->http_reply = rule->arg.http_reply;
Christopher Faulet3e964192018-10-24 11:39:23 +02002725 rule_ret = HTTP_RULE_RES_DENY;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002726 s->last_rule_file = rule->conf.file;
2727 s->last_rule_line = rule->conf.line;
Christopher Faulet3e964192018-10-24 11:39:23 +02002728 goto end;
2729
2730 case ACT_HTTP_REQ_TARPIT:
2731 txn->flags |= TX_CLTARPIT;
Christopher Faulet5cb513a2020-05-13 17:56:56 +02002732 txn->status = rule->arg.http_reply->status;
2733 txn->http_reply = rule->arg.http_reply;
Christopher Faulet3e964192018-10-24 11:39:23 +02002734 rule_ret = HTTP_RULE_RES_DENY;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002735 s->last_rule_file = rule->conf.file;
2736 s->last_rule_line = rule->conf.line;
Christopher Faulet3e964192018-10-24 11:39:23 +02002737 goto end;
2738
Willy Tarreaubc1223b2021-09-02 16:54:33 +02002739 case ACT_HTTP_REDIR: {
2740 int ret = http_apply_redirect_rule(rule->arg.redir, s, txn);
2741
2742 if (ret == 2) // 2 == skip
2743 break;
2744
2745 rule_ret = ret ? HTTP_RULE_RES_ABRT : HTTP_RULE_RES_ERROR;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002746 s->last_rule_file = rule->conf.file;
2747 s->last_rule_line = rule->conf.line;
Christopher Faulet3e964192018-10-24 11:39:23 +02002748 goto end;
Willy Tarreaubc1223b2021-09-02 16:54:33 +02002749 }
Christopher Faulet3e964192018-10-24 11:39:23 +02002750
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002751 /* other flags exists, but normally, they never be matched. */
Christopher Faulet3e964192018-10-24 11:39:23 +02002752 default:
2753 break;
2754 }
2755 }
2756
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002757 if (def_rules && s->current_rule_list == def_rules) {
2758 s->current_rule_list = rules;
2759 goto restart;
2760 }
2761
Christopher Faulet3e964192018-10-24 11:39:23 +02002762 end:
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002763 /* if the ruleset evaluation is finished reset the strict mode */
Christopher Faulet46f95542019-12-20 10:07:22 +01002764 if (rule_ret != HTTP_RULE_RES_YIELD)
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002765 txn->req.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01002766
Christopher Faulet3e964192018-10-24 11:39:23 +02002767 /* we reached the end of the rules, nothing to report */
2768 return rule_ret;
2769}
2770
2771/* Executes the http-response rules <rules> for stream <s> and proxy <px>. It
2772 * returns one of 5 possible statuses: HTTP_RULE_RES_CONT, HTTP_RULE_RES_STOP,
2773 * HTTP_RULE_RES_DONE, HTTP_RULE_RES_YIELD, or HTTP_RULE_RES_BADREQ. If *CONT
2774 * is returned, the process can continue the evaluation of next rule list. If
2775 * *STOP or *DONE is returned, the process must stop the evaluation. If *BADREQ
2776 * is returned, it means the operation could not be processed and a server error
Christopher Fauleta53abad2020-05-13 08:12:22 +02002777 * must be returned. If *YIELD is returned, the caller must call again the
2778 * function with the same context.
Christopher Faulet3e964192018-10-24 11:39:23 +02002779 */
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002780static enum rule_result http_res_get_intercept_rule(struct proxy *px, struct list *def_rules,
2781 struct list *rules, struct stream *s)
Christopher Faulet3e964192018-10-24 11:39:23 +02002782{
2783 struct session *sess = strm_sess(s);
2784 struct http_txn *txn = s->txn;
Christopher Faulet3e964192018-10-24 11:39:23 +02002785 struct act_rule *rule;
Christopher Faulet3e964192018-10-24 11:39:23 +02002786 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002787 int act_opts = 0;
Christopher Faulet3e964192018-10-24 11:39:23 +02002788
Christopher Faulet3e964192018-10-24 11:39:23 +02002789 /* If "the current_rule_list" match the executed rule list, we are in
2790 * resume condition. If a resume is needed it is always in the action
2791 * and never in the ACL or converters. In this case, we initialise the
2792 * current rule, and go to the action execution point.
2793 */
2794 if (s->current_rule) {
2795 rule = s->current_rule;
2796 s->current_rule = NULL;
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002797 if (s->current_rule_list == rules || (def_rules && s->current_rule_list == def_rules))
Christopher Faulet3e964192018-10-24 11:39:23 +02002798 goto resume_execution;
2799 }
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002800 s->current_rule_list = ((!def_rules || s->current_rule_list == def_rules) ? rules : def_rules);
2801
2802 restart:
Christopher Faulet3e964192018-10-24 11:39:23 +02002803
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002804 /* start the ruleset evaluation in strict mode */
2805 txn->rsp.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01002806
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002807 list_for_each_entry(rule, s->current_rule_list, list) {
Christopher Faulet3e964192018-10-24 11:39:23 +02002808 /* check optional condition */
2809 if (rule->cond) {
2810 int ret;
2811
2812 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
2813 ret = acl_pass(ret);
2814
2815 if (rule->cond->pol == ACL_COND_UNLESS)
2816 ret = !ret;
2817
2818 if (!ret) /* condition not matched */
2819 continue;
2820 }
2821
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002822 act_opts |= ACT_OPT_FIRST;
Christopher Faulet3e964192018-10-24 11:39:23 +02002823resume_execution:
Amaury Denoyelle03517732021-05-07 14:25:01 +02002824 if (rule->kw->flags & KWF_EXPERIMENTAL)
2825 mark_tainted(TAINTED_ACTION_EXP_EXECUTED);
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002826
2827 /* Always call the action function if defined */
2828 if (rule->action_ptr) {
Christopher Faulet2e56a732023-01-26 16:18:09 +01002829 if (sc_ep_test(s->scf, SE_FL_ERROR) ||
Christopher Faulet446d8032022-12-12 07:53:18 +01002830 ((s->req.flags & CF_SHUTR) &&
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002831 (px->options & PR_O_ABRT_CLOSE)))
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002832 act_opts |= ACT_OPT_FINAL;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002833
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002834 switch (rule->action_ptr(rule, px, sess, s, act_opts)) {
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002835 case ACT_RET_CONT:
2836 break;
2837 case ACT_RET_STOP:
2838 rule_ret = HTTP_RULE_RES_STOP;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002839 s->last_rule_file = rule->conf.file;
2840 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002841 goto end;
2842 case ACT_RET_YIELD:
2843 s->current_rule = rule;
2844 rule_ret = HTTP_RULE_RES_YIELD;
2845 goto end;
2846 case ACT_RET_ERR:
2847 rule_ret = HTTP_RULE_RES_ERROR;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002848 s->last_rule_file = rule->conf.file;
2849 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002850 goto end;
2851 case ACT_RET_DONE:
2852 rule_ret = HTTP_RULE_RES_DONE;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002853 s->last_rule_file = rule->conf.file;
2854 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002855 goto end;
2856 case ACT_RET_DENY:
Christopher Fauletb58f62b2020-01-13 16:40:13 +01002857 if (txn->status == -1)
2858 txn->status = 502;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002859 rule_ret = HTTP_RULE_RES_DENY;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002860 s->last_rule_file = rule->conf.file;
2861 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002862 goto end;
2863 case ACT_RET_ABRT:
2864 rule_ret = HTTP_RULE_RES_ABRT;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002865 s->last_rule_file = rule->conf.file;
2866 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002867 goto end;
2868 case ACT_RET_INV:
2869 rule_ret = HTTP_RULE_RES_BADREQ;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002870 s->last_rule_file = rule->conf.file;
2871 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002872 goto end;
2873 }
2874 continue; /* eval the next rule */
2875 }
2876
2877 /* If not action function defined, check for known actions */
Christopher Faulet3e964192018-10-24 11:39:23 +02002878 switch (rule->action) {
2879 case ACT_ACTION_ALLOW:
2880 rule_ret = HTTP_RULE_RES_STOP; /* "allow" rules are OK */
Willy Tarreauc6dae862022-03-09 17:23:10 +01002881 s->last_rule_file = rule->conf.file;
2882 s->last_rule_line = rule->conf.line;
Christopher Faulet3e964192018-10-24 11:39:23 +02002883 goto end;
2884
2885 case ACT_ACTION_DENY:
Christopher Faulet5cb513a2020-05-13 17:56:56 +02002886 txn->status = rule->arg.http_reply->status;
2887 txn->http_reply = rule->arg.http_reply;
Christopher Faulet3a26bee2019-12-16 12:47:40 +01002888 rule_ret = HTTP_RULE_RES_DENY;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002889 s->last_rule_file = rule->conf.file;
2890 s->last_rule_line = rule->conf.line;
Christopher Faulet3e964192018-10-24 11:39:23 +02002891 goto end;
2892
Willy Tarreaubc1223b2021-09-02 16:54:33 +02002893 case ACT_HTTP_REDIR: {
2894 int ret = http_apply_redirect_rule(rule->arg.redir, s, txn);
Christopher Faulet3e964192018-10-24 11:39:23 +02002895
Willy Tarreaubc1223b2021-09-02 16:54:33 +02002896 if (ret == 2) // 2 == skip
2897 break;
2898
2899 rule_ret = ret ? HTTP_RULE_RES_ABRT : HTTP_RULE_RES_ERROR;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002900 s->last_rule_file = rule->conf.file;
2901 s->last_rule_line = rule->conf.line;
Willy Tarreaubc1223b2021-09-02 16:54:33 +02002902 goto end;
2903 }
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002904 /* other flags exists, but normally, they never be matched. */
Christopher Faulet3e964192018-10-24 11:39:23 +02002905 default:
2906 break;
2907 }
2908 }
2909
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002910 if (def_rules && s->current_rule_list == def_rules) {
2911 s->current_rule_list = rules;
2912 goto restart;
2913 }
2914
Christopher Faulet3e964192018-10-24 11:39:23 +02002915 end:
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002916 /* if the ruleset evaluation is finished reset the strict mode */
Christopher Faulet46f95542019-12-20 10:07:22 +01002917 if (rule_ret != HTTP_RULE_RES_YIELD)
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002918 txn->rsp.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01002919
Christopher Faulet3e964192018-10-24 11:39:23 +02002920 /* we reached the end of the rules, nothing to report */
2921 return rule_ret;
2922}
2923
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002924/* Executes backend and frontend http-after-response rules for the stream <s>,
2925 * in that order. it return 1 on success and 0 on error. It is the caller
2926 * responsibility to catch error or ignore it. If it catches it, this function
2927 * may be called a second time, for the internal error.
2928 */
2929int http_eval_after_res_rules(struct stream *s)
2930{
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002931 struct list *def_rules, *rules;
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002932 struct session *sess = s->sess;
2933 enum rule_result ret = HTTP_RULE_RES_CONT;
2934
Christopher Faulet507479b2020-05-15 12:29:46 +02002935 /* Eval after-response ruleset only if the reply is not const */
2936 if (s->txn->flags & TX_CONST_REPLY)
2937 goto end;
2938
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002939 /* prune the request variables if not already done and swap to the response variables. */
2940 if (s->vars_reqres.scope != SCOPE_RES) {
2941 if (!LIST_ISEMPTY(&s->vars_reqres.head))
2942 vars_prune(&s->vars_reqres, s->sess, s);
Willy Tarreaub7bfcb32021-08-31 08:13:25 +02002943 vars_init_head(&s->vars_reqres, SCOPE_RES);
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002944 }
2945
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002946 def_rules = (s->be->defpx ? &s->be->defpx->http_after_res_rules : NULL);
2947 rules = &s->be->http_after_res_rules;
2948
2949 ret = http_res_get_intercept_rule(s->be, def_rules, rules, s);
Christopher Faulet4c5a5912021-11-09 17:48:39 +01002950 if ((ret == HTTP_RULE_RES_CONT || ret == HTTP_RULE_RES_STOP) && sess->fe != s->be) {
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002951 def_rules = ((sess->fe->defpx && sess->fe->defpx != s->be->defpx) ? &sess->fe->defpx->http_after_res_rules : NULL);
2952 rules = &sess->fe->http_after_res_rules;
2953 ret = http_res_get_intercept_rule(sess->fe, def_rules, rules, s);
2954 }
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002955
Christopher Faulet507479b2020-05-15 12:29:46 +02002956 end:
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002957 /* All other codes than CONTINUE, STOP or DONE are forbidden */
2958 return (ret == HTTP_RULE_RES_CONT || ret == HTTP_RULE_RES_STOP || ret == HTTP_RULE_RES_DONE);
2959}
2960
Christopher Fauletfcda7c62018-10-24 11:56:22 +02002961/*
2962 * Manage client-side cookie. It can impact performance by about 2% so it is
2963 * desirable to call it only when needed. This code is quite complex because
2964 * of the multiple very crappy and ambiguous syntaxes we have to support. it
2965 * highly recommended not to touch this part without a good reason !
2966 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002967static void http_manage_client_side_cookies(struct stream *s, struct channel *req)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02002968{
2969 struct session *sess = s->sess;
2970 struct http_txn *txn = s->txn;
2971 struct htx *htx;
2972 struct http_hdr_ctx ctx;
2973 char *hdr_beg, *hdr_end, *del_from;
2974 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
2975 int preserve_hdr;
2976
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002977 htx = htxbuf(&req->buf);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02002978 ctx.blk = NULL;
2979 while (http_find_header(htx, ist("Cookie"), &ctx, 1)) {
Olivier Houchardf0f42382019-07-22 17:43:46 +02002980 int is_first = 1;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02002981 del_from = NULL; /* nothing to be deleted */
2982 preserve_hdr = 0; /* assume we may kill the whole header */
2983
2984 /* Now look for cookies. Conforming to RFC2109, we have to support
2985 * attributes whose name begin with a '$', and associate them with
2986 * the right cookie, if we want to delete this cookie.
2987 * So there are 3 cases for each cookie read :
2988 * 1) it's a special attribute, beginning with a '$' : ignore it.
2989 * 2) it's a server id cookie that we *MAY* want to delete : save
2990 * some pointers on it (last semi-colon, beginning of cookie...)
2991 * 3) it's an application cookie : we *MAY* have to delete a previous
2992 * "special" cookie.
2993 * At the end of loop, if a "special" cookie remains, we may have to
2994 * remove it. If no application cookie persists in the header, we
2995 * *MUST* delete it.
2996 *
2997 * Note: RFC2965 is unclear about the processing of spaces around
2998 * the equal sign in the ATTR=VALUE form. A careful inspection of
2999 * the RFC explicitly allows spaces before it, and not within the
3000 * tokens (attrs or values). An inspection of RFC2109 allows that
3001 * too but section 10.1.3 lets one think that spaces may be allowed
3002 * after the equal sign too, resulting in some (rare) buggy
3003 * implementations trying to do that. So let's do what servers do.
3004 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
3005 * allowed quoted strings in values, with any possible character
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003006 * after a backslash, including control chars and delimiters, which
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003007 * causes parsing to become ambiguous. Browsers also allow spaces
3008 * within values even without quotes.
3009 *
3010 * We have to keep multiple pointers in order to support cookie
3011 * removal at the beginning, middle or end of header without
3012 * corrupting the header. All of these headers are valid :
3013 *
3014 * hdr_beg hdr_end
3015 * | |
3016 * v |
3017 * NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3 |
3018 * NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3 v
3019 * NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3
3020 * | | | | | | |
3021 * | | | | | | |
3022 * | | | | | | +--> next
3023 * | | | | | +----> val_end
3024 * | | | | +-----------> val_beg
3025 * | | | +--------------> equal
3026 * | | +----------------> att_end
3027 * | +---------------------> att_beg
3028 * +--------------------------> prev
3029 *
3030 */
3031 hdr_beg = ctx.value.ptr;
3032 hdr_end = hdr_beg + ctx.value.len;
3033 for (prev = hdr_beg; prev < hdr_end; prev = next) {
3034 /* Iterate through all cookies on this line */
3035
3036 /* find att_beg */
3037 att_beg = prev;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003038 if (!is_first)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003039 att_beg++;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003040 is_first = 0;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003041
3042 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
3043 att_beg++;
3044
3045 /* find att_end : this is the first character after the last non
3046 * space before the equal. It may be equal to hdr_end.
3047 */
3048 equal = att_end = att_beg;
3049 while (equal < hdr_end) {
3050 if (*equal == '=' || *equal == ',' || *equal == ';')
3051 break;
3052 if (HTTP_IS_SPHT(*equal++))
3053 continue;
3054 att_end = equal;
3055 }
3056
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003057 /* here, <equal> points to '=', a delimiter or the end. <att_end>
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003058 * is between <att_beg> and <equal>, both may be identical.
3059 */
3060 /* look for end of cookie if there is an equal sign */
3061 if (equal < hdr_end && *equal == '=') {
3062 /* look for the beginning of the value */
3063 val_beg = equal + 1;
3064 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
3065 val_beg++;
3066
3067 /* find the end of the value, respecting quotes */
3068 next = http_find_cookie_value_end(val_beg, hdr_end);
3069
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003070 /* make val_end point to the first white space or delimiter after the value */
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003071 val_end = next;
3072 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
3073 val_end--;
3074 }
3075 else
3076 val_beg = val_end = next = equal;
3077
3078 /* We have nothing to do with attributes beginning with
3079 * '$'. However, they will automatically be removed if a
3080 * header before them is removed, since they're supposed
3081 * to be linked together.
3082 */
3083 if (*att_beg == '$')
3084 continue;
3085
3086 /* Ignore cookies with no equal sign */
3087 if (equal == next) {
3088 /* This is not our cookie, so we must preserve it. But if we already
3089 * scheduled another cookie for removal, we cannot remove the
3090 * complete header, but we can remove the previous block itself.
3091 */
3092 preserve_hdr = 1;
3093 if (del_from != NULL) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003094 int delta = http_del_hdr_value(hdr_beg, hdr_end, &del_from, prev);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003095 val_end += delta;
3096 next += delta;
3097 hdr_end += delta;
3098 prev = del_from;
3099 del_from = NULL;
3100 }
3101 continue;
3102 }
3103
3104 /* if there are spaces around the equal sign, we need to
3105 * strip them otherwise we'll get trouble for cookie captures,
3106 * or even for rewrites. Since this happens extremely rarely,
3107 * it does not hurt performance.
3108 */
3109 if (unlikely(att_end != equal || val_beg > equal + 1)) {
3110 int stripped_before = 0;
3111 int stripped_after = 0;
3112
3113 if (att_end != equal) {
3114 memmove(att_end, equal, hdr_end - equal);
3115 stripped_before = (att_end - equal);
3116 equal += stripped_before;
3117 val_beg += stripped_before;
3118 }
3119
3120 if (val_beg > equal + 1) {
3121 memmove(equal + 1, val_beg, hdr_end + stripped_before - val_beg);
3122 stripped_after = (equal + 1) - val_beg;
3123 val_beg += stripped_after;
3124 stripped_before += stripped_after;
3125 }
3126
3127 val_end += stripped_before;
3128 next += stripped_before;
3129 hdr_end += stripped_before;
3130 }
3131 /* now everything is as on the diagram above */
3132
3133 /* First, let's see if we want to capture this cookie. We check
3134 * that we don't already have a client side cookie, because we
3135 * can only capture one. Also as an optimisation, we ignore
3136 * cookies shorter than the declared name.
3137 */
3138 if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL &&
3139 (val_end - att_beg >= sess->fe->capture_namelen) &&
3140 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
3141 int log_len = val_end - att_beg;
3142
3143 if ((txn->cli_cookie = pool_alloc(pool_head_capture)) == NULL) {
3144 ha_alert("HTTP logging : out of memory.\n");
3145 } else {
3146 if (log_len > sess->fe->capture_len)
3147 log_len = sess->fe->capture_len;
3148 memcpy(txn->cli_cookie, att_beg, log_len);
3149 txn->cli_cookie[log_len] = 0;
3150 }
3151 }
3152
3153 /* Persistence cookies in passive, rewrite or insert mode have the
3154 * following form :
3155 *
3156 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
3157 *
3158 * For cookies in prefix mode, the form is :
3159 *
3160 * Cookie: NAME=SRV~VALUE
3161 */
3162 if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
3163 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
3164 struct server *srv = s->be->srv;
3165 char *delim;
3166
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003167 /* if we're in cookie prefix mode, we'll search the delimiter so that we
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003168 * have the server ID between val_beg and delim, and the original cookie between
3169 * delim+1 and val_end. Otherwise, delim==val_end :
3170 *
3171 * hdr_beg
3172 * |
3173 * v
3174 * NAME=SRV; # in all but prefix modes
3175 * NAME=SRV~OPAQUE ; # in prefix mode
3176 * || || | |+-> next
3177 * || || | +--> val_end
3178 * || || +---------> delim
3179 * || |+------------> val_beg
3180 * || +-------------> att_end = equal
3181 * |+-----------------> att_beg
3182 * +------------------> prev
3183 *
3184 */
3185 if (s->be->ck_opts & PR_CK_PFX) {
3186 for (delim = val_beg; delim < val_end; delim++)
3187 if (*delim == COOKIE_DELIM)
3188 break;
3189 }
3190 else {
3191 char *vbar1;
3192 delim = val_end;
3193 /* Now check if the cookie contains a date field, which would
3194 * appear after a vertical bar ('|') just after the server name
3195 * and before the delimiter.
3196 */
3197 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
3198 if (vbar1) {
3199 /* OK, so left of the bar is the server's cookie and
3200 * right is the last seen date. It is a base64 encoded
3201 * 30-bit value representing the UNIX date since the
3202 * epoch in 4-second quantities.
3203 */
3204 int val;
3205 delim = vbar1++;
3206 if (val_end - vbar1 >= 5) {
3207 val = b64tos30(vbar1);
3208 if (val > 0)
3209 txn->cookie_last_date = val << 2;
3210 }
3211 /* look for a second vertical bar */
3212 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
3213 if (vbar1 && (val_end - vbar1 > 5)) {
3214 val = b64tos30(vbar1 + 1);
3215 if (val > 0)
3216 txn->cookie_first_date = val << 2;
3217 }
3218 }
3219 }
3220
3221 /* if the cookie has an expiration date and the proxy wants to check
3222 * it, then we do that now. We first check if the cookie is too old,
3223 * then only if it has expired. We detect strict overflow because the
3224 * time resolution here is not great (4 seconds). Cookies with dates
3225 * in the future are ignored if their offset is beyond one day. This
3226 * allows an admin to fix timezone issues without expiring everyone
3227 * and at the same time avoids keeping unwanted side effects for too
3228 * long.
3229 */
3230 if (txn->cookie_first_date && s->be->cookie_maxlife &&
3231 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) ||
3232 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
3233 txn->flags &= ~TX_CK_MASK;
3234 txn->flags |= TX_CK_OLD;
3235 delim = val_beg; // let's pretend we have not found the cookie
3236 txn->cookie_first_date = 0;
3237 txn->cookie_last_date = 0;
3238 }
3239 else if (txn->cookie_last_date && s->be->cookie_maxidle &&
3240 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) ||
3241 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
3242 txn->flags &= ~TX_CK_MASK;
3243 txn->flags |= TX_CK_EXPIRED;
3244 delim = val_beg; // let's pretend we have not found the cookie
3245 txn->cookie_first_date = 0;
3246 txn->cookie_last_date = 0;
3247 }
3248
3249 /* Here, we'll look for the first running server which supports the cookie.
3250 * This allows to share a same cookie between several servers, for example
3251 * to dedicate backup servers to specific servers only.
3252 * However, to prevent clients from sticking to cookie-less backup server
3253 * when they have incidentely learned an empty cookie, we simply ignore
3254 * empty cookies and mark them as invalid.
3255 * The same behaviour is applied when persistence must be ignored.
3256 */
3257 if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
3258 srv = NULL;
3259
3260 while (srv) {
3261 if (srv->cookie && (srv->cklen == delim - val_beg) &&
3262 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
3263 if ((srv->cur_state != SRV_ST_STOPPED) ||
3264 (s->be->options & PR_O_PERSIST) ||
3265 (s->flags & SF_FORCE_PRST)) {
3266 /* we found the server and we can use it */
3267 txn->flags &= ~TX_CK_MASK;
3268 txn->flags |= (srv->cur_state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
3269 s->flags |= SF_DIRECT | SF_ASSIGNED;
3270 s->target = &srv->obj_type;
3271 break;
3272 } else {
3273 /* we found a server, but it's down,
3274 * mark it as such and go on in case
3275 * another one is available.
3276 */
3277 txn->flags &= ~TX_CK_MASK;
3278 txn->flags |= TX_CK_DOWN;
3279 }
3280 }
3281 srv = srv->next;
3282 }
3283
3284 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
3285 /* no server matched this cookie or we deliberately skipped it */
3286 txn->flags &= ~TX_CK_MASK;
3287 if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
3288 txn->flags |= TX_CK_UNUSED;
3289 else
3290 txn->flags |= TX_CK_INVALID;
3291 }
3292
3293 /* depending on the cookie mode, we may have to either :
3294 * - delete the complete cookie if we're in insert+indirect mode, so that
3295 * the server never sees it ;
3296 * - remove the server id from the cookie value, and tag the cookie as an
Joseph Herlante9d5c722018-11-25 11:00:25 -08003297 * application cookie so that it does not get accidentally removed later,
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003298 * if we're in cookie prefix mode
3299 */
3300 if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
3301 int delta; /* negative */
3302
3303 memmove(val_beg, delim + 1, hdr_end - (delim + 1));
3304 delta = val_beg - (delim + 1);
3305 val_end += delta;
3306 next += delta;
3307 hdr_end += delta;
3308 del_from = NULL;
3309 preserve_hdr = 1; /* we want to keep this cookie */
3310 }
3311 else if (del_from == NULL &&
3312 (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
3313 del_from = prev;
3314 }
3315 }
3316 else {
3317 /* This is not our cookie, so we must preserve it. But if we already
3318 * scheduled another cookie for removal, we cannot remove the
3319 * complete header, but we can remove the previous block itself.
3320 */
3321 preserve_hdr = 1;
3322
3323 if (del_from != NULL) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003324 int delta = http_del_hdr_value(hdr_beg, hdr_end, &del_from, prev);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003325 if (att_beg >= del_from)
3326 att_beg += delta;
3327 if (att_end >= del_from)
3328 att_end += delta;
3329 val_beg += delta;
3330 val_end += delta;
3331 next += delta;
3332 hdr_end += delta;
3333 prev = del_from;
3334 del_from = NULL;
3335 }
3336 }
3337
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003338 } /* for each cookie */
3339
3340
3341 /* There are no more cookies on this line.
3342 * We may still have one (or several) marked for deletion at the
3343 * end of the line. We must do this now in two ways :
3344 * - if some cookies must be preserved, we only delete from the
3345 * mark to the end of line ;
3346 * - if nothing needs to be preserved, simply delete the whole header
3347 */
3348 if (del_from) {
3349 hdr_end = (preserve_hdr ? del_from : hdr_beg);
3350 }
3351 if ((hdr_end - hdr_beg) != ctx.value.len) {
Christopher Faulet3e2638e2019-06-18 09:49:16 +02003352 if (hdr_beg != hdr_end)
3353 htx_change_blk_value_len(htx, ctx.blk, hdr_end - hdr_beg);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003354 else
3355 http_remove_header(htx, &ctx);
3356 }
3357 } /* for each "Cookie header */
3358}
3359
3360/*
3361 * Manage server-side cookies. It can impact performance by about 2% so it is
3362 * desirable to call it only when needed. This function is also used when we
3363 * just need to know if there is a cookie (eg: for check-cache).
3364 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003365static void http_manage_server_side_cookies(struct stream *s, struct channel *res)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003366{
3367 struct session *sess = s->sess;
3368 struct http_txn *txn = s->txn;
3369 struct htx *htx;
3370 struct http_hdr_ctx ctx;
3371 struct server *srv;
3372 char *hdr_beg, *hdr_end;
3373 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003374
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003375 htx = htxbuf(&res->buf);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003376
3377 ctx.blk = NULL;
Willy Tarreauaa1909e2022-11-14 18:58:35 +01003378 while (http_find_header(htx, ist("Set-Cookie"), &ctx, 1)) {
Olivier Houchardf0f42382019-07-22 17:43:46 +02003379 int is_first = 1;
3380
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003381 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
3382 * <prev> points to the colon.
3383 */
3384 txn->flags |= TX_SCK_PRESENT;
3385
3386 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
3387 * check-cache is enabled) and we are not interested in checking
3388 * them. Warning, the cookie capture is declared in the frontend.
3389 */
3390 if (s->be->cookie_name == NULL && sess->fe->capture_name == NULL)
3391 break;
3392
3393 /* OK so now we know we have to process this response cookie.
3394 * The format of the Set-Cookie header is slightly different
3395 * from the format of the Cookie header in that it does not
3396 * support the comma as a cookie delimiter (thus the header
3397 * cannot be folded) because the Expires attribute described in
3398 * the original Netscape's spec may contain an unquoted date
3399 * with a comma inside. We have to live with this because
3400 * many browsers don't support Max-Age and some browsers don't
3401 * support quoted strings. However the Set-Cookie2 header is
Willy Tarreauaa1909e2022-11-14 18:58:35 +01003402 * clean but basically nobody supports it.
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003403 *
3404 * We have to keep multiple pointers in order to support cookie
3405 * removal at the beginning, middle or end of header without
3406 * corrupting the header (in case of set-cookie2). A special
3407 * pointer, <scav> points to the beginning of the set-cookie-av
3408 * fields after the first semi-colon. The <next> pointer points
3409 * either to the end of line (set-cookie) or next unquoted comma
3410 * (set-cookie2). All of these headers are valid :
3411 *
3412 * hdr_beg hdr_end
3413 * | |
3414 * v |
3415 * NAME1 = VALUE 1 ; Secure; Path="/" |
3416 * NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT v
3417 * NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT
3418 * NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard
3419 * | | | | | | | |
3420 * | | | | | | | +-> next
3421 * | | | | | | +------------> scav
3422 * | | | | | +--------------> val_end
3423 * | | | | +--------------------> val_beg
3424 * | | | +----------------------> equal
3425 * | | +------------------------> att_end
3426 * | +----------------------------> att_beg
3427 * +------------------------------> prev
3428 * -------------------------------> hdr_beg
3429 */
3430 hdr_beg = ctx.value.ptr;
3431 hdr_end = hdr_beg + ctx.value.len;
3432 for (prev = hdr_beg; prev < hdr_end; prev = next) {
3433
3434 /* Iterate through all cookies on this line */
3435
3436 /* find att_beg */
3437 att_beg = prev;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003438 if (!is_first)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003439 att_beg++;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003440 is_first = 0;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003441
3442 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
3443 att_beg++;
3444
3445 /* find att_end : this is the first character after the last non
3446 * space before the equal. It may be equal to hdr_end.
3447 */
3448 equal = att_end = att_beg;
3449
3450 while (equal < hdr_end) {
Willy Tarreauaa1909e2022-11-14 18:58:35 +01003451 if (*equal == '=' || *equal == ';')
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003452 break;
3453 if (HTTP_IS_SPHT(*equal++))
3454 continue;
3455 att_end = equal;
3456 }
3457
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003458 /* here, <equal> points to '=', a delimiter or the end. <att_end>
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003459 * is between <att_beg> and <equal>, both may be identical.
3460 */
3461
3462 /* look for end of cookie if there is an equal sign */
3463 if (equal < hdr_end && *equal == '=') {
3464 /* look for the beginning of the value */
3465 val_beg = equal + 1;
3466 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
3467 val_beg++;
3468
3469 /* find the end of the value, respecting quotes */
3470 next = http_find_cookie_value_end(val_beg, hdr_end);
3471
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003472 /* make val_end point to the first white space or delimiter after the value */
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003473 val_end = next;
3474 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
3475 val_end--;
3476 }
3477 else {
3478 /* <equal> points to next comma, semi-colon or EOL */
3479 val_beg = val_end = next = equal;
3480 }
3481
3482 if (next < hdr_end) {
Willy Tarreauaa1909e2022-11-14 18:58:35 +01003483 /* For Set-Cookie, since commas are permitted
3484 * in values, skip to the end.
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003485 */
Willy Tarreauaa1909e2022-11-14 18:58:35 +01003486 next = hdr_end;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003487 }
3488
3489 /* Now everything is as on the diagram above */
3490
3491 /* Ignore cookies with no equal sign */
3492 if (equal == val_end)
3493 continue;
3494
3495 /* If there are spaces around the equal sign, we need to
3496 * strip them otherwise we'll get trouble for cookie captures,
3497 * or even for rewrites. Since this happens extremely rarely,
3498 * it does not hurt performance.
3499 */
3500 if (unlikely(att_end != equal || val_beg > equal + 1)) {
3501 int stripped_before = 0;
3502 int stripped_after = 0;
3503
3504 if (att_end != equal) {
3505 memmove(att_end, equal, hdr_end - equal);
3506 stripped_before = (att_end - equal);
3507 equal += stripped_before;
3508 val_beg += stripped_before;
3509 }
3510
3511 if (val_beg > equal + 1) {
3512 memmove(equal + 1, val_beg, hdr_end + stripped_before - val_beg);
3513 stripped_after = (equal + 1) - val_beg;
3514 val_beg += stripped_after;
3515 stripped_before += stripped_after;
3516 }
3517
3518 val_end += stripped_before;
3519 next += stripped_before;
3520 hdr_end += stripped_before;
3521
Christopher Faulet3e2638e2019-06-18 09:49:16 +02003522 htx_change_blk_value_len(htx, ctx.blk, hdr_end - hdr_beg);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003523 ctx.value.len = hdr_end - hdr_beg;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003524 }
3525
3526 /* First, let's see if we want to capture this cookie. We check
3527 * that we don't already have a server side cookie, because we
3528 * can only capture one. Also as an optimisation, we ignore
3529 * cookies shorter than the declared name.
3530 */
3531 if (sess->fe->capture_name != NULL &&
3532 txn->srv_cookie == NULL &&
3533 (val_end - att_beg >= sess->fe->capture_namelen) &&
3534 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
3535 int log_len = val_end - att_beg;
3536 if ((txn->srv_cookie = pool_alloc(pool_head_capture)) == NULL) {
3537 ha_alert("HTTP logging : out of memory.\n");
3538 }
3539 else {
3540 if (log_len > sess->fe->capture_len)
3541 log_len = sess->fe->capture_len;
3542 memcpy(txn->srv_cookie, att_beg, log_len);
3543 txn->srv_cookie[log_len] = 0;
3544 }
3545 }
3546
3547 srv = objt_server(s->target);
3548 /* now check if we need to process it for persistence */
3549 if (!(s->flags & SF_IGNORE_PRST) &&
3550 (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
3551 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
3552 /* assume passive cookie by default */
3553 txn->flags &= ~TX_SCK_MASK;
3554 txn->flags |= TX_SCK_FOUND;
3555
3556 /* If the cookie is in insert mode on a known server, we'll delete
3557 * this occurrence because we'll insert another one later.
3558 * We'll delete it too if the "indirect" option is set and we're in
3559 * a direct access.
3560 */
3561 if (s->be->ck_opts & PR_CK_PSV) {
3562 /* The "preserve" flag was set, we don't want to touch the
3563 * server's cookie.
3564 */
3565 }
3566 else if ((srv && (s->be->ck_opts & PR_CK_INS)) ||
3567 ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) {
3568 /* this cookie must be deleted */
3569 if (prev == hdr_beg && next == hdr_end) {
3570 /* whole header */
3571 http_remove_header(htx, &ctx);
3572 /* note: while both invalid now, <next> and <hdr_end>
3573 * are still equal, so the for() will stop as expected.
3574 */
3575 } else {
3576 /* just remove the value */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003577 int delta = http_del_hdr_value(hdr_beg, hdr_end, &prev, next);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003578 next = prev;
3579 hdr_end += delta;
3580 }
3581 txn->flags &= ~TX_SCK_MASK;
3582 txn->flags |= TX_SCK_DELETED;
3583 /* and go on with next cookie */
3584 }
3585 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) {
3586 /* replace bytes val_beg->val_end with the cookie name associated
3587 * with this server since we know it.
3588 */
3589 int sliding, delta;
3590
3591 ctx.value = ist2(val_beg, val_end - val_beg);
3592 ctx.lws_before = ctx.lws_after = 0;
3593 http_replace_header_value(htx, &ctx, ist2(srv->cookie, srv->cklen));
3594 delta = srv->cklen - (val_end - val_beg);
3595 sliding = (ctx.value.ptr - val_beg);
3596 hdr_beg += sliding;
3597 val_beg += sliding;
3598 next += sliding + delta;
3599 hdr_end += sliding + delta;
3600
3601 txn->flags &= ~TX_SCK_MASK;
3602 txn->flags |= TX_SCK_REPLACED;
3603 }
3604 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) {
3605 /* insert the cookie name associated with this server
3606 * before existing cookie, and insert a delimiter between them..
3607 */
3608 int sliding, delta;
3609 ctx.value = ist2(val_beg, 0);
3610 ctx.lws_before = ctx.lws_after = 0;
3611 http_replace_header_value(htx, &ctx, ist2(srv->cookie, srv->cklen + 1));
3612 delta = srv->cklen + 1;
3613 sliding = (ctx.value.ptr - val_beg);
3614 hdr_beg += sliding;
3615 val_beg += sliding;
3616 next += sliding + delta;
3617 hdr_end += sliding + delta;
3618
3619 val_beg[srv->cklen] = COOKIE_DELIM;
3620 txn->flags &= ~TX_SCK_MASK;
3621 txn->flags |= TX_SCK_REPLACED;
3622 }
3623 }
3624 /* that's done for this cookie, check the next one on the same
Willy Tarreauaa1909e2022-11-14 18:58:35 +01003625 * line when next != hdr_end (which should normally not happen
3626 * with set-cookie2 support removed).
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003627 */
3628 }
3629 }
3630}
3631
Christopher Faulet25a02f62018-10-24 12:00:25 +02003632/*
3633 * Parses the Cache-Control and Pragma request header fields to determine if
3634 * the request may be served from the cache and/or if it is cacheable. Updates
3635 * s->txn->flags.
3636 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003637void http_check_request_for_cacheability(struct stream *s, struct channel *req)
Christopher Faulet25a02f62018-10-24 12:00:25 +02003638{
3639 struct http_txn *txn = s->txn;
3640 struct htx *htx;
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003641 struct http_hdr_ctx ctx = { .blk = NULL };
3642 int pragma_found, cc_found;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003643
3644 if ((txn->flags & (TX_CACHEABLE|TX_CACHE_IGNORE)) == TX_CACHE_IGNORE)
3645 return; /* nothing more to do here */
3646
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003647 htx = htxbuf(&req->buf);
Christopher Faulet25a02f62018-10-24 12:00:25 +02003648 pragma_found = cc_found = 0;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003649
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003650 /* Check "pragma" header for HTTP/1.0 compatibility. */
3651 if (http_find_header(htx, ist("pragma"), &ctx, 1)) {
3652 if (isteqi(ctx.value, ist("no-cache"))) {
3653 pragma_found = 1;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003654 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003655 }
Christopher Faulet25a02f62018-10-24 12:00:25 +02003656
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003657 ctx.blk = NULL;
3658 /* Don't use the cache and don't try to store if we found the
3659 * Authorization header */
3660 if (http_find_header(htx, ist("authorization"), &ctx, 1)) {
3661 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3662 txn->flags |= TX_CACHE_IGNORE;
3663 }
Christopher Faulet25a02f62018-10-24 12:00:25 +02003664
Christopher Faulet25a02f62018-10-24 12:00:25 +02003665
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003666 /* Look for "cache-control" header and iterate over all the values
3667 * until we find one that specifies that caching is possible or not. */
3668 ctx.blk = NULL;
3669 while (http_find_header(htx, ist("cache-control"), &ctx, 0)) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003670 cc_found = 1;
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003671 /* We don't check the values after max-age, max-stale nor min-fresh,
3672 * we simply don't use the cache when they're specified. */
3673 if (istmatchi(ctx.value, ist("max-age")) ||
3674 istmatchi(ctx.value, ist("no-cache")) ||
3675 istmatchi(ctx.value, ist("max-stale")) ||
3676 istmatchi(ctx.value, ist("min-fresh"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003677 txn->flags |= TX_CACHE_IGNORE;
3678 continue;
3679 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003680 if (istmatchi(ctx.value, ist("no-store"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003681 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3682 continue;
3683 }
3684 }
3685
3686 /* RFC7234#5.4:
3687 * When the Cache-Control header field is also present and
3688 * understood in a request, Pragma is ignored.
3689 * When the Cache-Control header field is not present in a
3690 * request, caches MUST consider the no-cache request
3691 * pragma-directive as having the same effect as if
3692 * "Cache-Control: no-cache" were present.
3693 */
3694 if (!cc_found && pragma_found)
3695 txn->flags |= TX_CACHE_IGNORE;
3696}
3697
3698/*
3699 * Check if response is cacheable or not. Updates s->txn->flags.
3700 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003701void http_check_response_for_cacheability(struct stream *s, struct channel *res)
Christopher Faulet25a02f62018-10-24 12:00:25 +02003702{
3703 struct http_txn *txn = s->txn;
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003704 struct http_hdr_ctx ctx = { .blk = NULL };
Christopher Faulet25a02f62018-10-24 12:00:25 +02003705 struct htx *htx;
Remi Tricot-Le Bretoncc9bf2e2020-11-12 11:14:41 +01003706 int has_freshness_info = 0;
3707 int has_validator = 0;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003708
3709 if (txn->status < 200) {
3710 /* do not try to cache interim responses! */
3711 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3712 return;
3713 }
3714
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003715 htx = htxbuf(&res->buf);
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003716 /* Check "pragma" header for HTTP/1.0 compatibility. */
3717 if (http_find_header(htx, ist("pragma"), &ctx, 1)) {
3718 if (isteqi(ctx.value, ist("no-cache"))) {
3719 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3720 return;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003721 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003722 }
Christopher Faulet25a02f62018-10-24 12:00:25 +02003723
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003724 /* Look for "cache-control" header and iterate over all the values
3725 * until we find one that specifies that caching is possible or not. */
3726 ctx.blk = NULL;
3727 while (http_find_header(htx, ist("cache-control"), &ctx, 0)) {
3728 if (isteqi(ctx.value, ist("public"))) {
3729 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003730 continue;
3731 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003732 if (isteqi(ctx.value, ist("private")) ||
3733 isteqi(ctx.value, ist("no-cache")) ||
3734 isteqi(ctx.value, ist("no-store")) ||
3735 isteqi(ctx.value, ist("max-age=0")) ||
3736 isteqi(ctx.value, ist("s-maxage=0"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003737 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003738 continue;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003739 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003740 /* We might have a no-cache="set-cookie" form. */
3741 if (istmatchi(ctx.value, ist("no-cache=\"set-cookie"))) {
3742 txn->flags &= ~TX_CACHE_COOK;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003743 continue;
3744 }
Remi Tricot-Le Bretoncc9bf2e2020-11-12 11:14:41 +01003745
3746 if (istmatchi(ctx.value, ist("s-maxage")) ||
3747 istmatchi(ctx.value, ist("max-age"))) {
3748 has_freshness_info = 1;
3749 continue;
3750 }
3751 }
3752
3753 /* If no freshness information could be found in Cache-Control values,
3754 * look for an Expires header. */
3755 if (!has_freshness_info) {
3756 ctx.blk = NULL;
3757 has_freshness_info = http_find_header(htx, ist("expires"), &ctx, 0);
Christopher Faulet25a02f62018-10-24 12:00:25 +02003758 }
Remi Tricot-Le Bretoncc9bf2e2020-11-12 11:14:41 +01003759
3760 /* If no freshness information could be found in Cache-Control or Expires
3761 * values, look for an explicit validator. */
3762 if (!has_freshness_info) {
3763 ctx.blk = NULL;
3764 has_validator = 1;
3765 if (!http_find_header(htx, ist("etag"), &ctx, 0)) {
3766 ctx.blk = NULL;
3767 if (!http_find_header(htx, ist("last-modified"), &ctx, 0))
3768 has_validator = 0;
3769 }
3770 }
3771
3772 /* We won't store an entry that has neither a cache validator nor an
3773 * explicit expiration time, as suggested in RFC 7234#3. */
3774 if (!has_freshness_info && !has_validator)
Remi Tricot-Le Breton879debe2023-02-21 11:47:17 +01003775 txn->flags &= ~TX_CACHEABLE;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003776}
3777
Christopher Faulet377c5a52018-10-24 21:21:30 +02003778/*
3779 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
3780 * for the current backend.
3781 *
3782 * It is assumed that the request is either a HEAD, GET, or POST and that the
3783 * uri_auth field is valid.
3784 *
3785 * Returns 1 if stats should be provided, otherwise 0.
3786 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003787static int http_stats_check_uri(struct stream *s, struct http_txn *txn, struct proxy *backend)
Christopher Faulet377c5a52018-10-24 21:21:30 +02003788{
3789 struct uri_auth *uri_auth = backend->uri_auth;
3790 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003791 struct htx_sl *sl;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003792 struct ist uri;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003793
3794 if (!uri_auth)
3795 return 0;
3796
3797 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
3798 return 0;
3799
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003800 htx = htxbuf(&s->req.buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02003801 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003802 uri = htx_sl_req_uri(sl);
Amaury Denoyellec453f952021-07-06 11:40:12 +02003803 if (*uri_auth->uri_prefix == '/') {
3804 struct http_uri_parser parser = http_uri_parser_init(uri);
3805 uri = http_parse_path(&parser);
3806 }
Christopher Faulet377c5a52018-10-24 21:21:30 +02003807
3808 /* check URI size */
3809 if (uri_auth->uri_len > uri.len)
3810 return 0;
3811
3812 if (memcmp(uri.ptr, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
3813 return 0;
3814
3815 return 1;
3816}
3817
3818/* This function prepares an applet to handle the stats. It can deal with the
3819 * "100-continue" expectation, check that admin rules are met for POST requests,
3820 * and program a response message if something was unexpected. It cannot fail
3821 * and always relies on the stats applet to complete the job. It does not touch
3822 * analysers nor counters, which are left to the caller. It does not touch
3823 * s->target which is supposed to already point to the stats applet. The caller
3824 * is expected to have already assigned an appctx to the stream.
3825 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003826static int http_handle_stats(struct stream *s, struct channel *req)
Christopher Faulet377c5a52018-10-24 21:21:30 +02003827{
3828 struct stats_admin_rule *stats_admin_rule;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003829 struct session *sess = s->sess;
3830 struct http_txn *txn = s->txn;
3831 struct http_msg *msg = &txn->req;
3832 struct uri_auth *uri_auth = s->be->uri_auth;
3833 const char *h, *lookup, *end;
Willy Tarreau8e7c6e62022-05-18 17:58:02 +02003834 struct appctx *appctx = __sc_appctx(s->scb);
Willy Tarreau91cefca2022-05-03 17:08:29 +02003835 struct show_stat_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx));
Christopher Faulet377c5a52018-10-24 21:21:30 +02003836 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003837 struct htx_sl *sl;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003838
Willy Tarreau41f88522022-05-03 18:39:27 +02003839 appctx->st1 = 0;
Willy Tarreau6ef16482022-05-06 18:07:53 +02003840 ctx->state = STAT_STATE_INIT;
Willy Tarreau91cefca2022-05-03 17:08:29 +02003841 ctx->st_code = STAT_STATUS_INIT;
3842 ctx->flags |= uri_auth->flags;
3843 ctx->flags |= STAT_FMT_HTML; /* assume HTML mode by default */
Christopher Faulet377c5a52018-10-24 21:21:30 +02003844 if ((msg->flags & HTTP_MSGF_VER_11) && (txn->meth != HTTP_METH_HEAD))
Willy Tarreau91cefca2022-05-03 17:08:29 +02003845 ctx->flags |= STAT_CHUNKED;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003846
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003847 htx = htxbuf(&req->buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02003848 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003849 lookup = HTX_SL_REQ_UPTR(sl) + uri_auth->uri_len;
3850 end = HTX_SL_REQ_UPTR(sl) + HTX_SL_REQ_ULEN(sl);
Christopher Faulet377c5a52018-10-24 21:21:30 +02003851
3852 for (h = lookup; h <= end - 3; h++) {
3853 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02003854 ctx->flags |= STAT_HIDE_DOWN;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003855 break;
3856 }
Amaury Denoyelle91e55ea2021-02-25 14:46:08 +01003857 }
3858
3859 for (h = lookup; h <= end - 9; h++) {
3860 if (memcmp(h, ";no-maint", 9) == 0) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02003861 ctx->flags |= STAT_HIDE_MAINT;
Willy Tarreau3e320362020-10-23 17:28:57 +02003862 break;
3863 }
Christopher Faulet377c5a52018-10-24 21:21:30 +02003864 }
3865
3866 if (uri_auth->refresh) {
3867 for (h = lookup; h <= end - 10; h++) {
3868 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02003869 ctx->flags |= STAT_NO_REFRESH;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003870 break;
3871 }
3872 }
3873 }
3874
3875 for (h = lookup; h <= end - 4; h++) {
3876 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02003877 ctx->flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
Christopher Faulet377c5a52018-10-24 21:21:30 +02003878 break;
3879 }
3880 }
3881
3882 for (h = lookup; h <= end - 6; h++) {
3883 if (memcmp(h, ";typed", 6) == 0) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02003884 ctx->flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
3885 ctx->flags |= STAT_FMT_TYPED;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003886 break;
3887 }
3888 }
3889
Christopher Faulet6338a082019-09-09 15:50:54 +02003890 for (h = lookup; h <= end - 5; h++) {
3891 if (memcmp(h, ";json", 5) == 0) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02003892 ctx->flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
3893 ctx->flags |= STAT_FMT_JSON;
Christopher Faulet6338a082019-09-09 15:50:54 +02003894 break;
3895 }
3896 }
3897
3898 for (h = lookup; h <= end - 12; h++) {
3899 if (memcmp(h, ";json-schema", 12) == 0) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02003900 ctx->flags &= ~STAT_FMT_MASK;
3901 ctx->flags |= STAT_JSON_SCHM;
Christopher Faulet6338a082019-09-09 15:50:54 +02003902 break;
3903 }
3904 }
3905
Christopher Faulet377c5a52018-10-24 21:21:30 +02003906 for (h = lookup; h <= end - 8; h++) {
3907 if (memcmp(h, ";st=", 4) == 0) {
3908 int i;
3909 h += 4;
Willy Tarreau91cefca2022-05-03 17:08:29 +02003910 ctx->st_code = STAT_STATUS_UNKN;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003911 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
3912 if (strncmp(stat_status_codes[i], h, 4) == 0) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02003913 ctx->st_code = i;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003914 break;
3915 }
3916 }
3917 break;
3918 }
3919 }
3920
Willy Tarreau91cefca2022-05-03 17:08:29 +02003921 ctx->scope_str = 0;
3922 ctx->scope_len = 0;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003923 for (h = lookup; h <= end - 8; h++) {
3924 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
3925 int itx = 0;
3926 const char *h2;
3927 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
3928 const char *err;
3929
3930 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
3931 h2 = h;
Willy Tarreau91cefca2022-05-03 17:08:29 +02003932 ctx->scope_str = h2 - HTX_SL_REQ_UPTR(sl);
Christopher Fauleted7a0662019-01-14 11:07:34 +01003933 while (h < end) {
Christopher Faulet377c5a52018-10-24 21:21:30 +02003934 if (*h == ';' || *h == '&' || *h == ' ')
3935 break;
3936 itx++;
3937 h++;
3938 }
3939
3940 if (itx > STAT_SCOPE_TXT_MAXLEN)
3941 itx = STAT_SCOPE_TXT_MAXLEN;
Willy Tarreau91cefca2022-05-03 17:08:29 +02003942 ctx->scope_len = itx;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003943
Willy Tarreau91cefca2022-05-03 17:08:29 +02003944 /* scope_txt = search query, ctx->scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Christopher Faulet377c5a52018-10-24 21:21:30 +02003945 memcpy(scope_txt, h2, itx);
3946 scope_txt[itx] = '\0';
3947 err = invalid_char(scope_txt);
3948 if (err) {
3949 /* bad char in search text => clear scope */
Willy Tarreau91cefca2022-05-03 17:08:29 +02003950 ctx->scope_str = 0;
3951 ctx->scope_len = 0;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003952 }
3953 break;
3954 }
3955 }
3956
3957 /* now check whether we have some admin rules for this request */
3958 list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) {
3959 int ret = 1;
3960
3961 if (stats_admin_rule->cond) {
3962 ret = acl_exec_cond(stats_admin_rule->cond, s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
3963 ret = acl_pass(ret);
3964 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
3965 ret = !ret;
3966 }
3967
3968 if (ret) {
3969 /* no rule, or the rule matches */
Willy Tarreau91cefca2022-05-03 17:08:29 +02003970 ctx->flags |= STAT_ADMIN;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003971 break;
3972 }
3973 }
3974
Christopher Faulet5d45e382019-02-27 15:15:23 +01003975 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
3976 appctx->st0 = STAT_HTTP_HEAD;
3977 else if (txn->meth == HTTP_METH_POST) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02003978 if (ctx->flags & STAT_ADMIN) {
Christopher Faulet377c5a52018-10-24 21:21:30 +02003979 appctx->st0 = STAT_HTTP_POST;
Christopher Fauletbd9e8422019-08-15 22:26:48 +02003980 if (msg->msg_state < HTTP_MSG_DATA)
3981 req->analysers |= AN_REQ_HTTP_BODY;
3982 }
Christopher Faulet377c5a52018-10-24 21:21:30 +02003983 else {
Christopher Faulet5d45e382019-02-27 15:15:23 +01003984 /* POST without admin level */
Willy Tarreau91cefca2022-05-03 17:08:29 +02003985 ctx->flags &= ~STAT_CHUNKED;
3986 ctx->st_code = STAT_STATUS_DENY;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003987 appctx->st0 = STAT_HTTP_LAST;
3988 }
3989 }
3990 else {
Christopher Faulet5d45e382019-02-27 15:15:23 +01003991 /* Unsupported method */
Willy Tarreau91cefca2022-05-03 17:08:29 +02003992 ctx->flags &= ~STAT_CHUNKED;
3993 ctx->st_code = STAT_STATUS_IVAL;
Christopher Faulet5d45e382019-02-27 15:15:23 +01003994 appctx->st0 = STAT_HTTP_LAST;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003995 }
3996
3997 s->task->nice = -32; /* small boost for HTTP statistics */
3998 return 1;
3999}
4000
Christopher Faulet021a8e42021-03-29 10:46:38 +02004001/* This function waits for the message payload at most <time> milliseconds (may
4002 * be set to TICK_ETERNITY). It stops to wait if at least <bytes> bytes of the
4003 * payload are received (0 means no limit). It returns HTTP_RULE_* depending on
4004 * the result:
4005 *
4006 * - HTTP_RULE_RES_CONT when conditions are met to stop waiting
4007 * - HTTP_RULE_RES_YIELD to wait for more data
Ilya Shipitsinb2be9a12021-04-24 13:25:42 +05004008 * - HTTP_RULE_RES_ABRT when a timeout occurred.
Christopher Faulet021a8e42021-03-29 10:46:38 +02004009 * - HTTP_RULE_RES_BADREQ if a parsing error is raised by lower level
Ilya Shipitsinb2be9a12021-04-24 13:25:42 +05004010 * - HTTP_RULE_RES_ERROR if an internal error occurred
Christopher Faulet021a8e42021-03-29 10:46:38 +02004011 *
Ilya Shipitsinb2be9a12021-04-24 13:25:42 +05004012 * If a timeout occurred, this function is responsible to emit the right response
Christopher Faulet021a8e42021-03-29 10:46:38 +02004013 * to the client, depending on the channel (408 on request side, 504 on response
4014 * side). All other errors must be handled by the caller.
4015 */
4016enum rule_result http_wait_for_msg_body(struct stream *s, struct channel *chn,
4017 unsigned int time, unsigned int bytes)
4018{
4019 struct session *sess = s->sess;
4020 struct http_txn *txn = s->txn;
4021 struct http_msg *msg = ((chn->flags & CF_ISRESP) ? &txn->rsp : &txn->req);
4022 struct htx *htx;
4023 enum rule_result ret = HTTP_RULE_RES_CONT;
4024
4025 htx = htxbuf(&chn->buf);
4026
4027 if (htx->flags & HTX_FL_PARSING_ERROR) {
4028 ret = HTTP_RULE_RES_BADREQ;
4029 goto end;
4030 }
4031 if (htx->flags & HTX_FL_PROCESSING_ERROR) {
4032 ret = HTTP_RULE_RES_ERROR;
4033 goto end;
4034 }
4035
4036 /* Do nothing for bodyless and CONNECT requests */
4037 if (txn->meth == HTTP_METH_CONNECT || (msg->flags & HTTP_MSGF_BODYLESS))
4038 goto end;
4039
4040 if (!(chn->flags & CF_ISRESP) && msg->msg_state < HTTP_MSG_DATA) {
4041 if (http_handle_expect_hdr(s, htx, msg) == -1) {
4042 ret = HTTP_RULE_RES_ERROR;
4043 goto end;
4044 }
4045 }
4046
4047 msg->msg_state = HTTP_MSG_DATA;
4048
4049 /* Now we're in HTTP_MSG_DATA. We just need to know if all data have
4050 * been received or if the buffer is full.
4051 */
Christopher Faulet78335962021-09-23 14:46:32 +02004052 if ((htx->flags & HTX_FL_EOM) ||
4053 htx_get_tail_type(htx) > HTX_BLK_DATA ||
4054 channel_htx_full(chn, htx, global.tune.maxrewrite) ||
Willy Tarreau99615ed2022-05-25 07:29:36 +02004055 sc_waiting_room(chn_prod(chn)))
Christopher Faulet021a8e42021-03-29 10:46:38 +02004056 goto end;
4057
4058 if (bytes) {
4059 struct htx_blk *blk;
4060 unsigned int len = 0;
4061
4062 for (blk = htx_get_first_blk(htx); blk; blk = htx_get_next_blk(htx, blk)) {
4063 if (htx_get_blk_type(blk) != HTX_BLK_DATA)
4064 continue;
4065 len += htx_get_blksz(blk);
4066 if (len >= bytes)
4067 goto end;
4068 }
4069 }
4070
4071 if ((chn->flags & CF_READ_TIMEOUT) || tick_is_expired(chn->analyse_exp, now_ms)) {
4072 if (!(chn->flags & CF_ISRESP))
4073 goto abort_req;
4074 goto abort_res;
4075 }
4076
4077 /* we get here if we need to wait for more data */
Christopher Faulet4b490b72023-01-04 11:55:15 +01004078 if (!(chn->flags & CF_SHUTR)) {
Christopher Faulet021a8e42021-03-29 10:46:38 +02004079 if (!tick_isset(chn->analyse_exp))
4080 chn->analyse_exp = tick_add_ifset(now_ms, time);
4081 ret = HTTP_RULE_RES_YIELD;
4082 }
4083
4084 end:
4085 return ret;
4086
Christopher Fauletf0d80df2023-01-13 10:20:20 +01004087 abort:
4088 http_reply_and_close(s, txn->status, http_error_message(s));
4089 ret = HTTP_RULE_RES_ABRT;
4090 goto end;
4091
Christopher Faulet021a8e42021-03-29 10:46:38 +02004092 abort_req:
4093 txn->status = 408;
4094 if (!(s->flags & SF_ERR_MASK))
4095 s->flags |= SF_ERR_CLITO;
Willy Tarreau4781b152021-04-06 13:53:36 +02004096 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req);
Christopher Faulet021a8e42021-03-29 10:46:38 +02004097 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02004098 _HA_ATOMIC_INC(&sess->listener->counters->failed_req);
Christopher Fauletf0d80df2023-01-13 10:20:20 +01004099 goto abort;
Christopher Faulet021a8e42021-03-29 10:46:38 +02004100
4101 abort_res:
4102 txn->status = 504;
4103 if (!(s->flags & SF_ERR_MASK))
4104 s->flags |= SF_ERR_SRVTO;
Christopher Faulet021a8e42021-03-29 10:46:38 +02004105 stream_inc_http_fail_ctr(s);
Christopher Fauletf0d80df2023-01-13 10:20:20 +01004106 goto abort;
Christopher Faulet021a8e42021-03-29 10:46:38 +02004107}
4108
Willy Tarreaub49672d2022-05-27 10:13:37 +02004109void http_perform_server_redirect(struct stream *s, struct stconn *sc)
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004110{
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004111 struct channel *req = &s->req;
4112 struct channel *res = &s->res;
4113 struct server *srv;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004114 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004115 struct htx_sl *sl;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004116 struct ist path, location;
4117 unsigned int flags;
Amaury Denoyellec453f952021-07-06 11:40:12 +02004118 struct http_uri_parser parser;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004119
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004120 /*
4121 * Create the location
4122 */
4123 chunk_reset(&trash);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004124
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004125 /* 1: add the server's prefix */
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004126 /* special prefix "/" means don't change URL */
4127 srv = __objt_server(s->target);
4128 if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') {
4129 if (!chunk_memcat(&trash, srv->rdr_pfx, srv->rdr_len))
4130 return;
4131 }
4132
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004133 /* 2: add the request Path */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004134 htx = htxbuf(&req->buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02004135 sl = http_get_stline(htx);
Amaury Denoyellec453f952021-07-06 11:40:12 +02004136 parser = http_uri_parser_init(htx_sl_req_uri(sl));
4137 path = http_parse_path(&parser);
Tim Duesterhused526372020-03-05 17:56:33 +01004138 if (!isttest(path))
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004139 return;
4140
4141 if (!chunk_memcat(&trash, path.ptr, path.len))
4142 return;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004143 location = ist2(trash.area, trash.data);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004144
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004145 /*
Ilya Shipitsin4a689da2022-10-29 09:34:32 +05004146 * Create the 302 response
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004147 */
4148 htx = htx_from_buf(&res->buf);
4149 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
4150 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
4151 ist("HTTP/1.1"), ist("302"), ist("Found"));
4152 if (!sl)
4153 goto fail;
4154 sl->info.res.status = 302;
4155 s->txn->status = 302;
4156
4157 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) ||
4158 !htx_add_header(htx, ist("Connection"), ist("close")) ||
4159 !htx_add_header(htx, ist("Content-length"), ist("0")) ||
4160 !htx_add_header(htx, ist("Location"), location))
4161 goto fail;
4162
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004163 if (!htx_add_endof(htx, HTX_BLK_EOH))
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004164 goto fail;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004165
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004166 htx->flags |= HTX_FL_EOM;
Christopher Fauletc20afb82020-01-24 19:16:26 +01004167 htx_to_buf(htx, &res->buf);
Christopher Fauleta72a7e42020-01-28 09:28:11 +01004168 if (!http_forward_proxy_resp(s, 1))
4169 goto fail;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004170
4171 /* return without error. */
Willy Tarreaub49672d2022-05-27 10:13:37 +02004172 sc_shutr(sc);
4173 sc_shutw(sc);
Christopher Faulet50264b42022-03-30 19:39:30 +02004174 s->conn_err_type = STRM_ET_NONE;
Willy Tarreaub49672d2022-05-27 10:13:37 +02004175 sc->state = SC_ST_CLO;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004176
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004177 if (!(s->flags & SF_ERR_MASK))
4178 s->flags |= SF_ERR_LOCAL;
4179 if (!(s->flags & SF_FINST_MASK))
4180 s->flags |= SF_FINST_C;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004181
4182 /* FIXME: we should increase a counter of redirects per server and per backend. */
4183 srv_inc_sess_ctr(srv);
4184 srv_set_sess_last(srv);
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004185 return;
4186
4187 fail:
4188 /* If an error occurred, remove the incomplete HTTP response from the
4189 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004190 channel_htx_truncate(res, htx);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004191}
4192
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05004193/* This function terminates the request because it was completely analyzed or
Christopher Fauletf2824e62018-10-01 12:12:37 +02004194 * because an error was triggered during the body forwarding.
4195 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004196static void http_end_request(struct stream *s)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004197{
4198 struct channel *chn = &s->req;
4199 struct http_txn *txn = s->txn;
4200
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004201 DBG_TRACE_ENTER(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004202
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004203 if (unlikely(txn->req.msg_state < HTTP_MSG_DONE)) {
4204 DBG_TRACE_DEVEL("waiting end of the request", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004205 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004206 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004207
4208 if (txn->req.msg_state == HTTP_MSG_DONE) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004209 /* No need to read anymore, the request was completely parsed.
4210 * We can shut the read side unless we want to abort_on_close,
4211 * or we have a POST request. The issue with POST requests is
4212 * that some browsers still send a CRLF after the request, and
4213 * this CRLF must be read so that it does not remain in the kernel
4214 * buffers, otherwise a close could cause an RST on some systems
4215 * (eg: Linux).
4216 */
Christopher Faulet769d0e92019-03-22 14:23:18 +01004217 if (!(s->be->options & PR_O_ABRT_CLOSE) && txn->meth != HTTP_METH_POST)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004218 channel_dont_read(chn);
4219
4220 /* if the server closes the connection, we want to immediately react
4221 * and close the socket to save packets and syscalls.
4222 */
Willy Tarreaucb041662022-05-17 19:44:42 +02004223 s->scb->flags |= SC_FL_NOHALF;
Christopher Fauletf2824e62018-10-01 12:12:37 +02004224
4225 /* In any case we've finished parsing the request so we must
4226 * disable Nagle when sending data because 1) we're not going
4227 * to shut this side, and 2) the server is waiting for us to
4228 * send pending data.
4229 */
4230 chn->flags |= CF_NEVER_WAIT;
4231
Christopher Fauletaf124362023-02-14 10:48:02 +01004232 if (txn->rsp.msg_state < HTTP_MSG_DONE && s->scb->state != SC_ST_CLO) {
4233 /* The server has not finished to respond and the
4234 * backend SC is not closed, so we don't want to move in
4235 * order not to upset it.
Christopher Fauletd01ce402019-01-02 17:44:13 +01004236 */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004237 DBG_TRACE_DEVEL("waiting end of the response", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletd01ce402019-01-02 17:44:13 +01004238 return;
4239 }
4240
Christopher Fauletf2824e62018-10-01 12:12:37 +02004241 /* When we get here, it means that both the request and the
4242 * response have finished receiving. Depending on the connection
4243 * mode, we'll have to wait for the last bytes to leave in either
4244 * direction, and sometimes for a close to be effective.
4245 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02004246 if (txn->flags & TX_CON_WANT_TUN) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004247 /* Tunnel mode will not have any analyser so it needs to
4248 * poll for reads.
4249 */
4250 channel_auto_read(chn);
4251 txn->req.msg_state = HTTP_MSG_TUNNEL;
4252 }
4253 else {
4254 /* we're not expecting any new data to come for this
4255 * transaction, so we can close it.
Christopher Faulet9768c262018-10-22 09:34:31 +02004256 *
4257 * However, there is an exception if the response
4258 * length is undefined. In this case, we need to wait
4259 * the close from the server. The response will be
4260 * switched in TUNNEL mode until the end.
Christopher Fauletf2824e62018-10-01 12:12:37 +02004261 */
4262 if (!(txn->rsp.flags & HTTP_MSGF_XFER_LEN) &&
4263 txn->rsp.msg_state != HTTP_MSG_CLOSED)
Christopher Faulet9768c262018-10-22 09:34:31 +02004264 goto check_channel_flags;
Christopher Fauletf2824e62018-10-01 12:12:37 +02004265
4266 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4267 channel_shutr_now(chn);
4268 channel_shutw_now(chn);
4269 }
4270 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004271 goto check_channel_flags;
4272 }
4273
4274 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
4275 http_msg_closing:
4276 /* nothing else to forward, just waiting for the output buffer
4277 * to be empty and for the shutw_now to take effect.
4278 */
4279 if (channel_is_empty(chn)) {
4280 txn->req.msg_state = HTTP_MSG_CLOSED;
4281 goto http_msg_closed;
4282 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004283 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004284 return;
4285 }
4286
4287 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
4288 http_msg_closed:
Christopher Fauletf2824e62018-10-01 12:12:37 +02004289 /* if we don't know whether the server will close, we need to hard close */
4290 if (txn->rsp.flags & HTTP_MSGF_XFER_LEN)
Willy Tarreaucb041662022-05-17 19:44:42 +02004291 s->scb->flags |= SC_FL_NOLINGER; /* we want to close ASAP */
Christopher Fauletf2824e62018-10-01 12:12:37 +02004292 /* see above in MSG_DONE why we only do this in these states */
Christopher Faulet769d0e92019-03-22 14:23:18 +01004293 if (!(s->be->options & PR_O_ABRT_CLOSE))
Christopher Fauletf2824e62018-10-01 12:12:37 +02004294 channel_dont_read(chn);
4295 goto end;
4296 }
4297
4298 check_channel_flags:
4299 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4300 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4301 /* if we've just closed an output, let's switch */
4302 txn->req.msg_state = HTTP_MSG_CLOSING;
4303 goto http_msg_closing;
4304 }
4305
4306 end:
4307 chn->analysers &= AN_REQ_FLT_END;
Christopher Faulet198ef8b2020-12-15 13:32:55 +01004308 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
4309 chn->flags |= CF_NEVER_WAIT;
4310 if (HAS_REQ_DATA_FILTERS(s))
Christopher Fauletf2824e62018-10-01 12:12:37 +02004311 chn->analysers |= AN_REQ_FLT_XFER_DATA;
Christopher Faulet198ef8b2020-12-15 13:32:55 +01004312 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004313 channel_auto_close(chn);
4314 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004315 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004316}
4317
4318
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05004319/* This function terminates the response because it was completely analyzed or
Christopher Fauletf2824e62018-10-01 12:12:37 +02004320 * because an error was triggered during the body forwarding.
4321 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004322static void http_end_response(struct stream *s)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004323{
4324 struct channel *chn = &s->res;
4325 struct http_txn *txn = s->txn;
4326
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004327 DBG_TRACE_ENTER(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004328
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004329 if (unlikely(txn->rsp.msg_state < HTTP_MSG_DONE)) {
4330 DBG_TRACE_DEVEL("waiting end of the response", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004331 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004332 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004333
4334 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
4335 /* In theory, we don't need to read anymore, but we must
4336 * still monitor the server connection for a possible close
4337 * while the request is being uploaded, so we don't disable
4338 * reading.
4339 */
4340 /* channel_dont_read(chn); */
4341
Christopher Fauletaf124362023-02-14 10:48:02 +01004342 if (txn->req.msg_state < HTTP_MSG_DONE && s->scf->state != SC_ST_CLO) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004343 /* The client seems to still be sending data, probably
4344 * because we got an error response during an upload.
4345 * We have the choice of either breaking the connection
4346 * or letting it pass through. Let's do the later.
4347 */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004348 DBG_TRACE_DEVEL("waiting end of the request", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004349 return;
4350 }
4351
4352 /* When we get here, it means that both the request and the
4353 * response have finished receiving. Depending on the connection
4354 * mode, we'll have to wait for the last bytes to leave in either
4355 * direction, and sometimes for a close to be effective.
4356 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02004357 if (txn->flags & TX_CON_WANT_TUN) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004358 channel_auto_read(chn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004359 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
4360 }
4361 else {
4362 /* we're not expecting any new data to come for this
4363 * transaction, so we can close it.
4364 */
4365 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4366 channel_shutr_now(chn);
4367 channel_shutw_now(chn);
4368 }
4369 }
4370 goto check_channel_flags;
4371 }
4372
4373 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
4374 http_msg_closing:
4375 /* nothing else to forward, just waiting for the output buffer
4376 * to be empty and for the shutw_now to take effect.
4377 */
4378 if (channel_is_empty(chn)) {
4379 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4380 goto http_msg_closed;
4381 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004382 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004383 return;
4384 }
4385
4386 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
4387 http_msg_closed:
4388 /* drop any pending data */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004389 channel_htx_truncate(&s->req, htxbuf(&s->req.buf));
Christopher Faulet9768c262018-10-22 09:34:31 +02004390 channel_abort(&s->req);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004391 goto end;
4392 }
4393
4394 check_channel_flags:
4395 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4396 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4397 /* if we've just closed an output, let's switch */
4398 txn->rsp.msg_state = HTTP_MSG_CLOSING;
4399 goto http_msg_closing;
4400 }
4401
4402 end:
4403 chn->analysers &= AN_RES_FLT_END;
Christopher Faulet198ef8b2020-12-15 13:32:55 +01004404 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
4405 chn->flags |= CF_NEVER_WAIT;
4406 if (HAS_RSP_DATA_FILTERS(s))
4407 chn->analysers |= AN_RES_FLT_XFER_DATA;
4408 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004409 channel_auto_close(chn);
4410 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004411 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004412}
4413
Christopher Fauletef70e252020-01-28 09:26:19 +01004414/* Forward a response generated by HAProxy (error/redirect/return). This
4415 * function forwards all pending incoming data. If <final> is set to 0, nothing
4416 * more is performed. It is used for 1xx informational messages. Otherwise, the
Christopher Faulet507479b2020-05-15 12:29:46 +02004417 * transaction is terminated and the request is emptied. On success 1 is
Christopher Faulet40e6b552020-06-25 16:04:50 +02004418 * returned. If an error occurred, 0 is returned. If it fails, this function
4419 * only exits. It is the caller responsibility to do the cleanup.
Christopher Fauletef70e252020-01-28 09:26:19 +01004420 */
4421int http_forward_proxy_resp(struct stream *s, int final)
4422{
4423 struct channel *req = &s->req;
4424 struct channel *res = &s->res;
4425 struct htx *htx = htxbuf(&res->buf);
4426 size_t data;
4427
4428 if (final) {
4429 htx->flags |= HTX_FL_PROXY_RESP;
Christopher Faulet507479b2020-05-15 12:29:46 +02004430
Christopher Fauletaab1b672020-11-18 16:44:02 +01004431 if (!htx_is_empty(htx) && !http_eval_after_res_rules(s))
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01004432 return 0;
Christopher Fauletef70e252020-01-28 09:26:19 +01004433
Christopher Fauletd6c48362020-10-19 18:01:38 +02004434 if (s->txn->meth == HTTP_METH_HEAD)
4435 htx_skip_msg_payload(htx);
4436
Christopher Fauletef70e252020-01-28 09:26:19 +01004437 channel_auto_read(req);
4438 channel_abort(req);
4439 channel_auto_close(req);
4440 channel_htx_erase(req, htxbuf(&req->buf));
4441
4442 res->wex = tick_add_ifset(now_ms, res->wto);
4443 channel_auto_read(res);
4444 channel_auto_close(res);
4445 channel_shutr_now(res);
Christopher Faulet1a9db7c2020-06-25 15:36:45 +02004446 res->flags |= CF_EOI; /* The response is terminated, add EOI */
Christopher Faulet42432f32020-11-20 17:43:16 +01004447 htxbuf(&res->buf)->flags |= HTX_FL_EOM; /* no more data are expected */
Christopher Fauletef70e252020-01-28 09:26:19 +01004448 }
Christopher Fauletcf6898c2020-06-25 15:55:11 +02004449 else {
4450 /* Send ASAP informational messages. Rely on CF_EOI for final
4451 * response.
4452 */
4453 res->flags |= CF_SEND_DONTWAIT;
4454 }
Christopher Fauletef70e252020-01-28 09:26:19 +01004455
4456 data = htx->data - co_data(res);
4457 c_adv(res, data);
4458 htx->first = -1;
4459 res->total += data;
4460 return 1;
4461}
4462
Willy Tarreaub49672d2022-05-27 10:13:37 +02004463void http_server_error(struct stream *s, struct stconn *sc, int err,
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004464 int finst, struct http_reply *msg)
Christopher Faulet0f226952018-10-22 09:29:56 +02004465{
Christopher Faulet72c7d8d2020-01-27 15:32:25 +01004466 http_reply_and_close(s, s->txn->status, msg);
Christopher Faulet0f226952018-10-22 09:29:56 +02004467 if (!(s->flags & SF_ERR_MASK))
4468 s->flags |= err;
4469 if (!(s->flags & SF_FINST_MASK))
4470 s->flags |= finst;
4471}
4472
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004473void http_reply_and_close(struct stream *s, short status, struct http_reply *msg)
Christopher Faulet0f226952018-10-22 09:29:56 +02004474{
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004475 if (!msg) {
4476 channel_htx_truncate(&s->res, htxbuf(&s->res.buf));
4477 goto end;
4478 }
4479
4480 if (http_reply_message(s, msg) == -1) {
4481 /* On error, return a 500 error message, but don't rewrite it if
Christopher Faulet40e6b552020-06-25 16:04:50 +02004482 * it is already an internal error. If it was already a "const"
4483 * 500 error, just fail.
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004484 */
Christopher Faulet40e6b552020-06-25 16:04:50 +02004485 if (s->txn->status == 500) {
4486 if (s->txn->flags & TX_CONST_REPLY)
4487 goto end;
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004488 s->txn->flags |= TX_CONST_REPLY;
Christopher Faulet40e6b552020-06-25 16:04:50 +02004489 }
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004490 s->txn->status = 500;
4491 s->txn->http_reply = NULL;
4492 return http_reply_and_close(s, s->txn->status, http_error_message(s));
4493 }
4494
4495end:
4496 s->res.wex = tick_add_ifset(now_ms, s->res.wto);
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004497
Christopher Faulet2d565002021-09-10 09:17:50 +02004498 /* At this staged, HTTP analysis is finished */
4499 s->req.analysers &= AN_REQ_FLT_END;
4500 s->req.analyse_exp = TICK_ETERNITY;
4501
4502 s->res.analysers &= AN_RES_FLT_END;
4503 s->res.analyse_exp = TICK_ETERNITY;
4504
Christopher Faulet0f226952018-10-22 09:29:56 +02004505 channel_auto_read(&s->req);
4506 channel_abort(&s->req);
4507 channel_auto_close(&s->req);
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004508 channel_htx_erase(&s->req, htxbuf(&s->req.buf));
Christopher Faulet72c7d8d2020-01-27 15:32:25 +01004509 channel_auto_read(&s->res);
4510 channel_auto_close(&s->res);
4511 channel_shutr_now(&s->res);
Christopher Faulet0f226952018-10-22 09:29:56 +02004512}
4513
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004514struct http_reply *http_error_message(struct stream *s)
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004515{
4516 const int msgnum = http_get_status_idx(s->txn->status);
4517
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004518 if (s->txn->http_reply)
4519 return s->txn->http_reply;
4520 else if (s->be->replies[msgnum])
4521 return s->be->replies[msgnum];
4522 else if (strm_fe(s)->replies[msgnum])
4523 return strm_fe(s)->replies[msgnum];
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004524 else
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004525 return &http_err_replies[msgnum];
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004526}
4527
Christopher Faulet40e6b552020-06-25 16:04:50 +02004528/* Produces an HTX message from an http reply. Depending on the http reply type,
4529 * a, errorfile, an raw file or a log-format string is used. On success, it
4530 * returns 0. If an error occurs -1 is returned. If it fails, this function only
4531 * exits. It is the caller responsibility to do the cleanup.
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004532 */
Christopher Fauletae43b6c2020-05-27 15:24:22 +02004533int http_reply_to_htx(struct stream *s, struct htx *htx, struct http_reply *reply)
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004534{
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004535 struct buffer *errmsg;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004536 struct htx_sl *sl;
4537 struct buffer *body = NULL;
4538 const char *status, *reason, *clen, *ctype;
4539 unsigned int slflags;
4540 int ret = 0;
4541
Christopher Faulete29a97e2020-05-14 14:49:25 +02004542 /*
4543 * - HTTP_REPLY_ERRFILES unexpected here. handled as no payload if so
4544 *
4545 * - HTTP_REPLY_INDIRECT: switch on another reply if defined or handled
4546 * as no payload if NULL. the TXN status code is set with the status
4547 * of the original reply.
4548 */
4549
4550 if (reply->type == HTTP_REPLY_INDIRECT) {
4551 if (reply->body.reply)
4552 reply = reply->body.reply;
4553 }
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004554 if (reply->type == HTTP_REPLY_ERRMSG && !reply->body.errmsg) {
4555 /* get default error message */
4556 if (reply == s->txn->http_reply)
4557 s->txn->http_reply = NULL;
4558 reply = http_error_message(s);
4559 if (reply->type == HTTP_REPLY_INDIRECT) {
4560 if (reply->body.reply)
4561 reply = reply->body.reply;
4562 }
4563 }
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004564
4565 if (reply->type == HTTP_REPLY_ERRMSG) {
4566 /* implicit or explicit error message*/
4567 errmsg = reply->body.errmsg;
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004568 if (errmsg && !b_is_null(errmsg)) {
Christopher Faulet20567362020-05-15 14:52:49 +02004569 if (!htx_copy_msg(htx, errmsg))
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004570 goto fail;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004571 }
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004572 }
4573 else {
4574 /* no payload, file or log-format string */
4575 if (reply->type == HTTP_REPLY_RAW) {
4576 /* file */
4577 body = &reply->body.obj;
4578 }
4579 else if (reply->type == HTTP_REPLY_LOGFMT) {
4580 /* log-format string */
4581 body = alloc_trash_chunk();
4582 if (!body)
4583 goto fail_alloc;
4584 body->data = build_logline(s, body->area, body->size, &reply->body.fmt);
4585 }
4586 /* else no payload */
4587
4588 status = ultoa(reply->status);
4589 reason = http_get_reason(reply->status);
4590 slflags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_CLEN);
4591 if (!body || !b_data(body))
4592 slflags |= HTX_SL_F_BODYLESS;
4593 sl = htx_add_stline(htx, HTX_BLK_RES_SL, slflags, ist("HTTP/1.1"), ist(status), ist(reason));
4594 if (!sl)
4595 goto fail;
4596 sl->info.res.status = reply->status;
4597
4598 clen = (body ? ultoa(b_data(body)) : "0");
4599 ctype = reply->ctype;
4600
4601 if (!LIST_ISEMPTY(&reply->hdrs)) {
4602 struct http_reply_hdr *hdr;
4603 struct buffer *value = alloc_trash_chunk();
4604
4605 if (!value)
4606 goto fail;
4607
4608 list_for_each_entry(hdr, &reply->hdrs, list) {
4609 chunk_reset(value);
4610 value->data = build_logline(s, value->area, value->size, &hdr->value);
4611 if (b_data(value) && !htx_add_header(htx, hdr->name, ist2(b_head(value), b_data(value)))) {
4612 free_trash_chunk(value);
4613 goto fail;
4614 }
4615 chunk_reset(value);
4616 }
4617 free_trash_chunk(value);
4618 }
4619
4620 if (!htx_add_header(htx, ist("content-length"), ist(clen)) ||
4621 (body && b_data(body) && ctype && !htx_add_header(htx, ist("content-type"), ist(ctype))) ||
4622 !htx_add_endof(htx, HTX_BLK_EOH) ||
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004623 (body && b_data(body) && !htx_add_data_atonce(htx, ist2(b_head(body), b_data(body)))))
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004624 goto fail;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004625
4626 htx->flags |= HTX_FL_EOM;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004627 }
4628
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004629 leave:
4630 if (reply->type == HTTP_REPLY_LOGFMT)
4631 free_trash_chunk(body);
4632 return ret;
4633
4634 fail_alloc:
4635 if (!(s->flags & SF_ERR_MASK))
4636 s->flags |= SF_ERR_RESOURCE;
Christopher Faulet97e466c2020-05-15 15:12:47 +02004637 /* fall through */
4638 fail:
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004639 ret = -1;
4640 goto leave;
Christopher Faulet97e466c2020-05-15 15:12:47 +02004641}
4642
4643/* Send an http reply to the client. On success, it returns 0. If an error
Christopher Faulet40e6b552020-06-25 16:04:50 +02004644 * occurs -1 is returned and the response channel is truncated, removing this
4645 * way the faulty reply. This function may fail when the reply is formatted
4646 * (http_reply_to_htx) or when the reply is forwarded
4647 * (http_forward_proxy_resp). On the last case, it is because a
4648 * http-after-response rule fails.
Christopher Faulet97e466c2020-05-15 15:12:47 +02004649 */
4650int http_reply_message(struct stream *s, struct http_reply *reply)
4651{
4652 struct channel *res = &s->res;
4653 struct htx *htx = htx_from_buf(&res->buf);
4654
4655 if (s->txn->status == -1)
4656 s->txn->status = reply->status;
4657 channel_htx_truncate(res, htx);
4658
4659 if (http_reply_to_htx(s, htx, reply) == -1)
4660 goto fail;
4661
4662 htx_to_buf(htx, &s->res.buf);
4663 if (!http_forward_proxy_resp(s, 1))
4664 goto fail;
4665 return 0;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004666
4667 fail:
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004668 channel_htx_truncate(res, htx);
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004669 if (!(s->flags & SF_ERR_MASK))
4670 s->flags |= SF_ERR_PRXCOND;
Christopher Faulet97e466c2020-05-15 15:12:47 +02004671 return -1;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004672}
4673
Christopher Faulet50264b42022-03-30 19:39:30 +02004674/* Return the error message corresponding to s->conn_err_type. It is assumed
Christopher Faulet304cc402019-07-15 15:46:28 +02004675 * that the server side is closed. Note that err_type is actually a
4676 * bitmask, where almost only aborts may be cumulated with other
4677 * values. We consider that aborted operations are more important
4678 * than timeouts or errors due to the fact that nobody else in the
4679 * logs might explain incomplete retries. All others should avoid
4680 * being cumulated. It should normally not be possible to have multiple
4681 * aborts at once, but just in case, the first one in sequence is reported.
4682 * Note that connection errors appearing on the second request of a keep-alive
4683 * connection are not reported since this allows the client to retry.
4684 */
Willy Tarreaub49672d2022-05-27 10:13:37 +02004685void http_return_srv_error(struct stream *s, struct stconn *sc)
Christopher Faulet304cc402019-07-15 15:46:28 +02004686{
Christopher Faulet50264b42022-03-30 19:39:30 +02004687 int err_type = s->conn_err_type;
Christopher Faulet304cc402019-07-15 15:46:28 +02004688
4689 /* set s->txn->status for http_error_message(s) */
Christopher Faulet50264b42022-03-30 19:39:30 +02004690 if (err_type & STRM_ET_QUEUE_ABRT) {
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004691 s->txn->status = -1;
Willy Tarreaub49672d2022-05-27 10:13:37 +02004692 http_server_error(s, sc, SF_ERR_CLICL, SF_FINST_Q, NULL);
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004693 }
Christopher Faulet50264b42022-03-30 19:39:30 +02004694 else if (err_type & STRM_ET_CONN_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_C, NULL);
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004697 }
Christopher Faulet50264b42022-03-30 19:39:30 +02004698 else if (err_type & STRM_ET_QUEUE_TO) {
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004699 s->txn->status = 503;
Willy Tarreaub49672d2022-05-27 10:13:37 +02004700 http_server_error(s, sc, SF_ERR_SRVTO, SF_FINST_Q,
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004701 http_error_message(s));
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004702 }
Christopher Faulet50264b42022-03-30 19:39:30 +02004703 else if (err_type & STRM_ET_QUEUE_ERR) {
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004704 s->txn->status = 503;
Willy Tarreaub49672d2022-05-27 10:13:37 +02004705 http_server_error(s, sc, SF_ERR_SRVCL, SF_FINST_Q,
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004706 http_error_message(s));
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004707 }
Christopher Faulet50264b42022-03-30 19:39:30 +02004708 else if (err_type & STRM_ET_CONN_TO) {
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004709 s->txn->status = 503;
Willy Tarreaub49672d2022-05-27 10:13:37 +02004710 http_server_error(s, sc, SF_ERR_SRVTO, SF_FINST_C,
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004711 (s->txn->flags & TX_NOT_FIRST) ? NULL :
4712 http_error_message(s));
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004713 }
Christopher Faulet50264b42022-03-30 19:39:30 +02004714 else if (err_type & STRM_ET_CONN_ERR) {
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004715 s->txn->status = 503;
Willy Tarreaub49672d2022-05-27 10:13:37 +02004716 http_server_error(s, sc, SF_ERR_SRVCL, SF_FINST_C,
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004717 (s->flags & SF_SRV_REUSED) ? NULL :
4718 http_error_message(s));
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004719 }
Christopher Faulet50264b42022-03-30 19:39:30 +02004720 else if (err_type & STRM_ET_CONN_RES) {
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004721 s->txn->status = 503;
Willy Tarreaub49672d2022-05-27 10:13:37 +02004722 http_server_error(s, sc, SF_ERR_RESOURCE, SF_FINST_C,
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004723 (s->txn->flags & TX_NOT_FIRST) ? NULL :
4724 http_error_message(s));
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004725 }
Christopher Faulet50264b42022-03-30 19:39:30 +02004726 else { /* STRM_ET_CONN_OTHER and others */
Christopher Faulet304cc402019-07-15 15:46:28 +02004727 s->txn->status = 500;
Willy Tarreaub49672d2022-05-27 10:13:37 +02004728 http_server_error(s, sc, SF_ERR_INTERNAL, SF_FINST_C,
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004729 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004730 }
4731}
4732
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004733
Christopher Faulet4a28a532019-03-01 11:19:40 +01004734/* Handle Expect: 100-continue for HTTP/1.1 messages if necessary. It returns 0
4735 * on success and -1 on error.
4736 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004737static int http_handle_expect_hdr(struct stream *s, struct htx *htx, struct http_msg *msg)
Christopher Faulet4a28a532019-03-01 11:19:40 +01004738{
4739 /* If we have HTTP/1.1 message with a body and Expect: 100-continue,
4740 * then we must send an HTTP/1.1 100 Continue intermediate response.
4741 */
4742 if (msg->msg_state == HTTP_MSG_BODY && (msg->flags & HTTP_MSGF_VER_11) &&
4743 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
4744 struct ist hdr = { .ptr = "Expect", .len = 6 };
4745 struct http_hdr_ctx ctx;
4746
4747 ctx.blk = NULL;
4748 /* Expect is allowed in 1.1, look for it */
4749 if (http_find_header(htx, hdr, &ctx, 0) &&
4750 unlikely(isteqi(ctx.value, ist2("100-continue", 12)))) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004751 if (http_reply_100_continue(s) == -1)
Christopher Faulet4a28a532019-03-01 11:19:40 +01004752 return -1;
4753 http_remove_header(htx, &ctx);
4754 }
4755 }
4756 return 0;
4757}
4758
Christopher Faulet23a3c792018-11-28 10:01:23 +01004759/* Send a 100-Continue response to the client. It returns 0 on success and -1
4760 * on error. The response channel is updated accordingly.
4761 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004762static int http_reply_100_continue(struct stream *s)
Christopher Faulet23a3c792018-11-28 10:01:23 +01004763{
4764 struct channel *res = &s->res;
4765 struct htx *htx = htx_from_buf(&res->buf);
4766 struct htx_sl *sl;
4767 unsigned int flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|
4768 HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
Christopher Faulet23a3c792018-11-28 10:01:23 +01004769
4770 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
4771 ist("HTTP/1.1"), ist("100"), ist("Continue"));
4772 if (!sl)
4773 goto fail;
4774 sl->info.res.status = 100;
4775
Christopher Faulet1d5ec092019-06-26 14:23:54 +02004776 if (!htx_add_endof(htx, HTX_BLK_EOH))
Christopher Faulet23a3c792018-11-28 10:01:23 +01004777 goto fail;
4778
Christopher Fauleta72a7e42020-01-28 09:28:11 +01004779 if (!http_forward_proxy_resp(s, 0))
4780 goto fail;
Christopher Faulet23a3c792018-11-28 10:01:23 +01004781 return 0;
4782
4783 fail:
4784 /* If an error occurred, remove the incomplete HTTP response from the
4785 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004786 channel_htx_truncate(res, htx);
Christopher Faulet23a3c792018-11-28 10:01:23 +01004787 return -1;
4788}
4789
Christopher Faulet12c51e22018-11-28 15:59:42 +01004790
Christopher Faulet0f226952018-10-22 09:29:56 +02004791/*
4792 * Capture headers from message <htx> according to header list <cap_hdr>, and
4793 * fill the <cap> pointers appropriately.
4794 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004795static void http_capture_headers(struct htx *htx, char **cap, struct cap_hdr *cap_hdr)
Christopher Faulet0f226952018-10-22 09:29:56 +02004796{
4797 struct cap_hdr *h;
4798 int32_t pos;
4799
Christopher Fauleta3f15502019-05-13 15:27:23 +02004800 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet0f226952018-10-22 09:29:56 +02004801 struct htx_blk *blk = htx_get_blk(htx, pos);
4802 enum htx_blk_type type = htx_get_blk_type(blk);
4803 struct ist n, v;
4804
4805 if (type == HTX_BLK_EOH)
4806 break;
4807 if (type != HTX_BLK_HDR)
4808 continue;
4809
4810 n = htx_get_blk_name(htx, blk);
4811
4812 for (h = cap_hdr; h; h = h->next) {
4813 if (h->namelen && (h->namelen == n.len) &&
4814 (strncasecmp(n.ptr, h->name, h->namelen) == 0)) {
4815 if (cap[h->index] == NULL)
4816 cap[h->index] =
4817 pool_alloc(h->pool);
4818
4819 if (cap[h->index] == NULL) {
4820 ha_alert("HTTP capture : out of memory.\n");
4821 break;
4822 }
4823
4824 v = htx_get_blk_value(htx, blk);
Tim Duesterhus2471f5c2021-11-08 09:05:01 +01004825 v = isttrim(v, h->len);
Christopher Faulet0f226952018-10-22 09:29:56 +02004826
4827 memcpy(cap[h->index], v.ptr, v.len);
4828 cap[h->index][v.len]=0;
4829 }
4830 }
4831 }
4832}
4833
Christopher Faulet0b6bdc52018-10-24 11:05:36 +02004834/* Delete a value in a header between delimiters <from> and <next>. The header
4835 * itself is delimited by <start> and <end> pointers. The number of characters
4836 * displaced is returned, and the pointer to the first delimiter is updated if
4837 * required. The function tries as much as possible to respect the following
4838 * principles :
4839 * - replace <from> delimiter by the <next> one unless <from> points to <start>,
4840 * in which case <next> is simply removed
4841 * - set exactly one space character after the new first delimiter, unless there
4842 * are not enough characters in the block being moved to do so.
4843 * - remove unneeded spaces before the previous delimiter and after the new
4844 * one.
4845 *
4846 * It is the caller's responsibility to ensure that :
4847 * - <from> points to a valid delimiter or <start> ;
4848 * - <next> points to a valid delimiter or <end> ;
4849 * - there are non-space chars before <from>.
4850 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004851static int http_del_hdr_value(char *start, char *end, char **from, char *next)
Christopher Faulet0b6bdc52018-10-24 11:05:36 +02004852{
4853 char *prev = *from;
4854
4855 if (prev == start) {
4856 /* We're removing the first value. eat the semicolon, if <next>
4857 * is lower than <end> */
4858 if (next < end)
4859 next++;
4860
4861 while (next < end && HTTP_IS_SPHT(*next))
4862 next++;
4863 }
4864 else {
4865 /* Remove useless spaces before the old delimiter. */
4866 while (HTTP_IS_SPHT(*(prev-1)))
4867 prev--;
4868 *from = prev;
4869
4870 /* copy the delimiter and if possible a space if we're
4871 * not at the end of the line.
4872 */
4873 if (next < end) {
4874 *prev++ = *next++;
4875 if (prev + 1 < next)
4876 *prev++ = ' ';
4877 while (next < end && HTTP_IS_SPHT(*next))
4878 next++;
4879 }
4880 }
4881 memmove(prev, next, end - next);
4882 return (prev - next);
4883}
4884
Christopher Faulet0f226952018-10-22 09:29:56 +02004885
4886/* Formats the start line of the request (without CRLF) and puts it in <str> and
Joseph Herlantc42c0e92018-11-25 10:43:27 -08004887 * return the written length. The line can be truncated if it exceeds <len>.
Christopher Faulet0f226952018-10-22 09:29:56 +02004888 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004889static size_t http_fmt_req_line(const struct htx_sl *sl, char *str, size_t len)
Christopher Faulet0f226952018-10-22 09:29:56 +02004890{
4891 struct ist dst = ist2(str, 0);
4892
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004893 if (istcat(&dst, htx_sl_req_meth(sl), len) == -1)
Christopher Faulet0f226952018-10-22 09:29:56 +02004894 goto end;
4895 if (dst.len + 1 > len)
4896 goto end;
4897 dst.ptr[dst.len++] = ' ';
4898
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004899 if (istcat(&dst, htx_sl_req_uri(sl), len) == -1)
Christopher Faulet0f226952018-10-22 09:29:56 +02004900 goto end;
4901 if (dst.len + 1 > len)
4902 goto end;
4903 dst.ptr[dst.len++] = ' ';
4904
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004905 istcat(&dst, htx_sl_req_vsn(sl), len);
Christopher Faulet0f226952018-10-22 09:29:56 +02004906 end:
4907 return dst.len;
4908}
4909
4910/*
4911 * Print a debug line with a start line.
4912 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004913static void http_debug_stline(const char *dir, struct stream *s, const struct htx_sl *sl)
Christopher Faulet0f226952018-10-22 09:29:56 +02004914{
4915 struct session *sess = strm_sess(s);
4916 int max;
4917
4918 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
4919 dir,
Willy Tarreau88bc8002021-12-06 07:01:02 +00004920 objt_conn(sess->origin) ? (unsigned short)__objt_conn(sess->origin)->handle.fd : -1,
Willy Tarreaufd9417b2022-05-18 16:23:22 +02004921 sc_conn(s->scb) ? (unsigned short)(__sc_conn(s->scb))->handle.fd : -1);
Christopher Faulet0f226952018-10-22 09:29:56 +02004922
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004923 max = HTX_SL_P1_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02004924 UBOUND(max, trash.size - trash.data - 3);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004925 chunk_memcat(&trash, HTX_SL_P1_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02004926 trash.area[trash.data++] = ' ';
4927
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004928 max = HTX_SL_P2_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02004929 UBOUND(max, trash.size - trash.data - 2);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004930 chunk_memcat(&trash, HTX_SL_P2_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02004931 trash.area[trash.data++] = ' ';
4932
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004933 max = HTX_SL_P3_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02004934 UBOUND(max, trash.size - trash.data - 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004935 chunk_memcat(&trash, HTX_SL_P3_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02004936 trash.area[trash.data++] = '\n';
4937
Willy Tarreau2e8ab6b2020-03-14 11:03:20 +01004938 DISGUISE(write(1, trash.area, trash.data));
Christopher Faulet0f226952018-10-22 09:29:56 +02004939}
4940
4941/*
4942 * Print a debug line with a header.
4943 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004944static 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 +02004945{
4946 struct session *sess = strm_sess(s);
4947 int max;
4948
4949 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
4950 dir,
Willy Tarreau88bc8002021-12-06 07:01:02 +00004951 objt_conn(sess->origin) ? (unsigned short)__objt_conn(sess->origin)->handle.fd : -1,
Willy Tarreaufd9417b2022-05-18 16:23:22 +02004952 sc_conn(s->scb) ? (unsigned short)(__sc_conn(s->scb))->handle.fd : -1);
Christopher Faulet0f226952018-10-22 09:29:56 +02004953
4954 max = n.len;
4955 UBOUND(max, trash.size - trash.data - 3);
4956 chunk_memcat(&trash, n.ptr, max);
4957 trash.area[trash.data++] = ':';
4958 trash.area[trash.data++] = ' ';
4959
4960 max = v.len;
4961 UBOUND(max, trash.size - trash.data - 1);
4962 chunk_memcat(&trash, v.ptr, max);
4963 trash.area[trash.data++] = '\n';
4964
Willy Tarreau2e8ab6b2020-03-14 11:03:20 +01004965 DISGUISE(write(1, trash.area, trash.data));
Christopher Faulet0f226952018-10-22 09:29:56 +02004966}
4967
Christopher Fauleta8a46e22019-07-16 14:53:09 +02004968void http_txn_reset_req(struct http_txn *txn)
4969{
Christopher Faulet1aea50e2020-01-17 16:03:53 +01004970 txn->req.flags = 0;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02004971 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
4972}
4973
4974void http_txn_reset_res(struct http_txn *txn)
4975{
Christopher Faulet1aea50e2020-01-17 16:03:53 +01004976 txn->rsp.flags = 0;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02004977 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
4978}
4979
4980/*
Christopher Faulet75f619a2021-03-08 19:12:58 +01004981 * Create and initialize a new HTTP transaction for stream <s>. This should be
4982 * used before processing any new request. It returns the transaction or NLULL
4983 * on error.
Christopher Fauleta8a46e22019-07-16 14:53:09 +02004984 */
Christopher Faulet75f619a2021-03-08 19:12:58 +01004985struct http_txn *http_create_txn(struct stream *s)
Christopher Fauleta8a46e22019-07-16 14:53:09 +02004986{
Christopher Faulet75f619a2021-03-08 19:12:58 +01004987 struct http_txn *txn;
Willy Tarreaub49672d2022-05-27 10:13:37 +02004988 struct stconn *sc = s->scf;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02004989
Christopher Faulet75f619a2021-03-08 19:12:58 +01004990 txn = pool_alloc(pool_head_http_txn);
4991 if (!txn)
4992 return NULL;
4993 s->txn = txn;
4994
Christopher Faulet5eb67f52022-06-22 17:12:05 +02004995 txn->meth = HTTP_METH_OTHER;
Willy Tarreaub49672d2022-05-27 10:13:37 +02004996 txn->flags = ((sc && sc_ep_test(sc, SE_FL_NOT_FIRST)) ? TX_NOT_FIRST : 0);
Christopher Fauleta8a46e22019-07-16 14:53:09 +02004997 txn->status = -1;
Christopher Faulet5cb513a2020-05-13 17:56:56 +02004998 txn->http_reply = NULL;
Christopher Faulete05bf9e2022-03-29 15:23:40 +02004999 txn->l7_buffer = BUF_NULL;
Willy Tarreau8b507582020-02-25 09:35:07 +01005000 write_u32(txn->cache_hash, 0);
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005001
5002 txn->cookie_first_date = 0;
5003 txn->cookie_last_date = 0;
5004
5005 txn->srv_cookie = NULL;
5006 txn->cli_cookie = NULL;
5007 txn->uri = NULL;
5008
5009 http_txn_reset_req(txn);
5010 http_txn_reset_res(txn);
5011
5012 txn->req.chn = &s->req;
5013 txn->rsp.chn = &s->res;
5014
5015 txn->auth.method = HTTP_AUTH_UNKNOWN;
5016
Aurelien DARRAGON5ad2b642022-11-18 09:17:29 +01005017 /* here we don't want to re-initialize s->vars_txn and s->vars_reqres
5018 * variable lists, because they were already initialized upon stream
5019 * creation in stream_new(), and thus may already contain some variables
5020 */
Christopher Faulet75f619a2021-03-08 19:12:58 +01005021
5022 return txn;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005023}
5024
5025/* to be used at the end of a transaction */
Christopher Faulet75f619a2021-03-08 19:12:58 +01005026void http_destroy_txn(struct stream *s)
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005027{
5028 struct http_txn *txn = s->txn;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005029
5030 /* these ones will have been dynamically allocated */
5031 pool_free(pool_head_requri, txn->uri);
5032 pool_free(pool_head_capture, txn->cli_cookie);
5033 pool_free(pool_head_capture, txn->srv_cookie);
Tim Duesterhusa17e6622020-03-05 20:19:02 +01005034 pool_free(pool_head_uniqueid, s->unique_id.ptr);
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005035
Tim Duesterhusa17e6622020-03-05 20:19:02 +01005036 s->unique_id = IST_NULL;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005037 txn->uri = NULL;
5038 txn->srv_cookie = NULL;
5039 txn->cli_cookie = NULL;
5040
Christopher Faulet59399252019-11-07 14:27:52 +01005041 if (!LIST_ISEMPTY(&s->vars_txn.head))
5042 vars_prune(&s->vars_txn, s->sess, s);
5043 if (!LIST_ISEMPTY(&s->vars_reqres.head))
5044 vars_prune(&s->vars_reqres, s->sess, s);
Christopher Faulet75f619a2021-03-08 19:12:58 +01005045
Christopher Faulete05bf9e2022-03-29 15:23:40 +02005046 b_free(&txn->l7_buffer);
5047
Christopher Faulet75f619a2021-03-08 19:12:58 +01005048 pool_free(pool_head_http_txn, txn);
5049 s->txn = NULL;
Christopher Faulet59399252019-11-07 14:27:52 +01005050}
5051
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005052
Christopher Faulet71236de2023-01-13 09:06:38 +01005053void http_set_term_flags(struct stream *s)
5054{
5055 if (!(s->flags & SF_ERR_MASK))
5056 s->flags |= SF_ERR_PRXCOND;
5057
5058 if (!(s->flags & SF_FINST_MASK)) {
5059 if (s->scb->state == SC_ST_INI) {
5060 /* Before any connection attempt on the server side, we
5061 * are still in the request analysis. Just take case to
5062 * detect tarpit error
5063 */
5064 if (s->req.analysers & AN_REQ_HTTP_TARPIT)
5065 s->flags |= SF_FINST_T;
5066 else
5067 s->flags |= SF_FINST_R;
5068 }
5069 else if (s->scb->state == SC_ST_QUE)
5070 s->flags |= SF_FINST_Q;
5071 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)) {
5072 if (unlikely(objt_applet(s->target))) {
5073 s->flags |= SF_FINST_R;
5074 }
5075 else
5076 s->flags |= SF_FINST_C;
5077 }
5078 else {
5079 if (s->txn->rsp.msg_state < HTTP_MSG_DATA) {
5080 /* We are still processing the response headers */
5081 s->flags |= SF_FINST_H;
5082 }
Christopher Faulet5b74f992023-01-26 19:02:07 +01005083 // (res == (done|closing|closed)) & (res->flags & shutw)
5084 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 +01005085 (s->flags & (SF_ERR_CLITO|SF_ERR_CLICL))) {
5086 /* A client error was reported and we are
5087 * transmitting the last block of data
5088 */
5089 s->flags |= SF_FINST_L;
5090 }
5091 else {
5092 /* Otherwise we are in DATA phase on both sides */
5093 s->flags |= SF_FINST_D;
5094 }
5095 }
5096 }
5097}
5098
5099
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005100DECLARE_POOL(pool_head_http_txn, "http_txn", sizeof(struct http_txn));
Christopher Faulet0f226952018-10-22 09:29:56 +02005101
Christopher Fauletf4eb75d2018-10-11 15:55:07 +02005102/*
5103 * Local variables:
5104 * c-indent-level: 8
5105 * c-basic-offset: 8
5106 * End:
5107 */