blob: bee658f86dcc14ab1d784030418ab6c6608c2768 [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 */
Christopher Faulet87633c32023-04-03 18:32:50 +0200117 if (chn_prod(req)->flags & SC_FL_SHUTR) {
Christopher Faulet97b3a612021-03-15 17:10:12 +0100118 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
Christopher Faulet5c281d52023-03-16 15:53:28 +0100487 s->scb->flags |= SC_FL_SND_ASAP;
Christopher Fauletbcf242a2019-03-01 11:36:26 +0100488 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
Christopher Faulet5c281d52023-03-16 15:53:28 +0100513 * SC_FL_SND_ASAP flag on the back SC any time. It's a one-shot flag so it
514 * will remove itself once used.
Christopher Faulete0768eb2018-10-03 16:38:02 +0200515 */
Christopher Faulet5c281d52023-03-16 15:53:28 +0100516 s->scb->flags |= SC_FL_SND_ASAP;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200517
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 Faulet87633c32023-04-03 18:32:50 +0200770 if (!(chn_prod(req)->flags & SC_FL_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
Christopher Faulet2954bcc2023-04-05 10:42:03 +0200800 * returns zero if it needs to read more data, or 1 once it has completed its
801 * analysis.
Christopher Faulete0768eb2018-10-03 16:38:02 +0200802 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200803int http_wait_for_request_body(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200804{
805 struct session *sess = s->sess;
806 struct http_txn *txn = s->txn;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200807
Christopher Fauletf4569bb2023-01-13 10:49:31 +0100808 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, &s->txn->req);
Christopher Fauletf76ebe82018-10-24 11:16:22 +0200809
Christopher Fauletf76ebe82018-10-24 11:16:22 +0200810
Christopher Faulet021a8e42021-03-29 10:46:38 +0200811 switch (http_wait_for_msg_body(s, req, s->be->timeout.httpreq, 0)) {
812 case HTTP_RULE_RES_CONT:
813 goto http_end;
814 case HTTP_RULE_RES_YIELD:
815 goto missing_data_or_waiting;
816 case HTTP_RULE_RES_BADREQ:
Willy Tarreau4236f032019-03-05 10:43:32 +0100817 goto return_bad_req;
Christopher Faulet021a8e42021-03-29 10:46:38 +0200818 case HTTP_RULE_RES_ERROR:
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200819 goto return_int_err;
Christopher Faulet021a8e42021-03-29 10:46:38 +0200820 case HTTP_RULE_RES_ABRT:
Christopher Fauletb8a53712019-12-16 11:29:38 +0100821 goto return_prx_cond;
Christopher Faulet021a8e42021-03-29 10:46:38 +0200822 default:
823 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200824 }
825
826 http_end:
827 /* The situation will not evolve, so let's give up on the analysis. */
828 s->logs.tv_request = now; /* update the request timer to reflect full request */
829 req->analysers &= ~an_bit;
830 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100831 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200832 return 1;
833
Christopher Faulet021a8e42021-03-29 10:46:38 +0200834 missing_data_or_waiting:
835 channel_dont_connect(req);
836 DBG_TRACE_DEVEL("waiting for more data",
837 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
838 return 0;
839
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200840 return_int_err:
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200841 txn->status = 500;
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200842 if (!(s->flags & SF_ERR_MASK))
843 s->flags |= SF_ERR_INTERNAL;
Willy Tarreau4781b152021-04-06 13:53:36 +0200844 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100845 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau4781b152021-04-06 13:53:36 +0200846 _HA_ATOMIC_INC(&s->be->be_counters.internal_errors);
William Lallemand36119de2021-03-08 15:26:48 +0100847 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200848 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Faulet021a8e42021-03-29 10:46:38 +0200849 goto return_prx_err;
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200850
Christopher Faulete0768eb2018-10-03 16:38:02 +0200851 return_bad_req: /* let's centralize all bad requests */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200852 txn->status = 400;
Willy Tarreau4781b152021-04-06 13:53:36 +0200853 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req);
William Lallemand36119de2021-03-08 15:26:48 +0100854 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200855 _HA_ATOMIC_INC(&sess->listener->counters->failed_req);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100856 /* fall through */
857
Christopher Faulet021a8e42021-03-29 10:46:38 +0200858 return_prx_err:
Christopher Fauletb8a53712019-12-16 11:29:38 +0100859 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet021a8e42021-03-29 10:46:38 +0200860 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200861
Christopher Faulet021a8e42021-03-29 10:46:38 +0200862 return_prx_cond:
Christopher Fauletf0d80df2023-01-13 10:20:20 +0100863 http_set_term_flags(s);
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200864
Christopher Faulete0768eb2018-10-03 16:38:02 +0200865 req->analysers &= AN_REQ_FLT_END;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100866 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100867 DBG_TRACE_DEVEL("leaving on error",
868 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200869 return 0;
870}
871
872/* This function is an analyser which forwards request body (including chunk
873 * sizes if any). It is called as soon as we must forward, even if we forward
874 * zero byte. The only situation where it must not be called is when we're in
875 * tunnel mode and we want to forward till the close. It's used both to forward
876 * remaining data and to resync after end of body. It expects the msg_state to
877 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
878 * read more data, or 1 once we can go on with next request or end the stream.
879 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
880 * bytes of pending data + the headers if not already done.
881 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200882int http_request_forward_body(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200883{
884 struct session *sess = s->sess;
885 struct http_txn *txn = s->txn;
Christopher Faulet9768c262018-10-22 09:34:31 +0200886 struct http_msg *msg = &txn->req;
887 struct htx *htx;
Christopher Faulet93e02d82019-03-08 14:18:50 +0100888 short status = 0;
Christopher Fauletaed82cf2018-11-30 22:22:32 +0100889 int ret;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200890
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100891 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200892
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100893 htx = htxbuf(&req->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200894
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200895 if (htx->flags & HTX_FL_PARSING_ERROR)
896 goto return_bad_req;
897 if (htx->flags & HTX_FL_PROCESSING_ERROR)
898 goto return_int_err;
899
Christopher Faulete0768eb2018-10-03 16:38:02 +0200900 /* Note that we don't have to send 100-continue back because we don't
901 * need the data to complete our job, and it's up to the server to
902 * decide whether to return 100, 417 or anything else in return of
903 * an "Expect: 100-continue" header.
904 */
Christopher Faulet9768c262018-10-22 09:34:31 +0200905 if (msg->msg_state == HTTP_MSG_BODY)
906 msg->msg_state = HTTP_MSG_DATA;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200907
Christopher Faulete0768eb2018-10-03 16:38:02 +0200908 /* in most states, we should abort in case of early close */
909 channel_auto_close(req);
910
911 if (req->to_forward) {
Christopher Faulet66af0b22019-03-22 14:54:52 +0100912 if (req->to_forward == CHN_INFINITE_FORWARD) {
Christopher Faulet904763f2023-03-22 14:53:11 +0100913 if (s->scf->flags & SC_FL_EOI)
Christopher Faulet1a3e0272019-11-15 16:31:46 +0100914 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet66af0b22019-03-22 14:54:52 +0100915 }
916 else {
917 /* We can't process the buffer's contents yet */
918 req->flags |= CF_WAKE_WRITE;
919 goto missing_data_or_waiting;
920 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200921 }
922
Christopher Faulet1a3e0272019-11-15 16:31:46 +0100923 if (msg->msg_state >= HTTP_MSG_ENDING)
924 goto ending;
925
926 if (txn->meth == HTTP_METH_CONNECT) {
927 msg->msg_state = HTTP_MSG_ENDING;
928 goto ending;
929 }
930
Christopher Fauletaed82cf2018-11-30 22:22:32 +0100931 /* Forward input data. We get it by removing all outgoing data not
932 * forwarded yet from HTX data size. If there are some data filters, we
933 * let them decide the amount of data to forward.
Christopher Faulet9768c262018-10-22 09:34:31 +0200934 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +0100935 if (HAS_REQ_DATA_FILTERS(s)) {
936 ret = flt_http_payload(s, msg, htx->data);
937 if (ret < 0)
938 goto return_bad_req;
Christopher Faulet421e7692019-06-13 11:16:45 +0200939 c_adv(req, ret);
Christopher Fauletaed82cf2018-11-30 22:22:32 +0100940 }
941 else {
Christopher Faulet421e7692019-06-13 11:16:45 +0200942 c_adv(req, htx->data - co_data(req));
Christopher Faulet2f7c82b2023-02-20 14:06:52 +0100943 if ((global.tune.options & GTUNE_USE_FAST_FWD) && (msg->flags & HTTP_MSGF_XFER_LEN))
Christopher Faulet66af0b22019-03-22 14:54:52 +0100944 channel_htx_forward_forever(req, htx);
Christopher Fauletaed82cf2018-11-30 22:22:32 +0100945 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200946
Christopher Faulet1a3e0272019-11-15 16:31:46 +0100947 if (htx->data != co_data(req))
948 goto missing_data_or_waiting;
Christopher Fauletd20fdb02019-06-13 16:43:22 +0200949
Christopher Faulet9768c262018-10-22 09:34:31 +0200950 /* Check if the end-of-message is reached and if so, switch the message
Christopher Fauletd20fdb02019-06-13 16:43:22 +0200951 * in HTTP_MSG_ENDING state. Then if all data was marked to be
952 * forwarded, set the state to HTTP_MSG_DONE.
Christopher Faulet9768c262018-10-22 09:34:31 +0200953 */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +0100954 if (!(htx->flags & HTX_FL_EOM))
Christopher Faulet9768c262018-10-22 09:34:31 +0200955 goto missing_data_or_waiting;
956
Christopher Fauletd20fdb02019-06-13 16:43:22 +0200957 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet9768c262018-10-22 09:34:31 +0200958
Christopher Faulet1a3e0272019-11-15 16:31:46 +0100959 ending:
Christopher Faulet84d3ef92023-03-17 15:45:58 +0100960 s->scb->flags &= ~SC_FL_SND_EXP_MORE; /* no more data are expected to be send */
Christopher Faulet2151cdd2020-07-22 16:34:59 +0200961
Christopher Faulet1a3e0272019-11-15 16:31:46 +0100962 /* other states, ENDING...TUNNEL */
963 if (msg->msg_state >= HTTP_MSG_DONE)
964 goto done;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200965
Christopher Fauletaed82cf2018-11-30 22:22:32 +0100966 if (HAS_REQ_DATA_FILTERS(s)) {
967 ret = flt_http_end(s, msg);
968 if (ret <= 0) {
969 if (!ret)
970 goto missing_data_or_waiting;
971 goto return_bad_req;
972 }
973 }
974
Christopher Faulet1a3e0272019-11-15 16:31:46 +0100975 if (txn->meth == HTTP_METH_CONNECT)
976 msg->msg_state = HTTP_MSG_TUNNEL;
977 else {
978 msg->msg_state = HTTP_MSG_DONE;
979 req->to_forward = 0;
980 }
981
982 done:
983 /* we don't want to forward closes on DONE except in tunnel mode. */
984 if (!(txn->flags & TX_CON_WANT_TUN))
985 channel_dont_close(req);
986
Christopher Faulet87633c32023-04-03 18:32:50 +0200987 if ((chn_cons(req)->flags & SC_FL_SHUTW) && co_data(req)) {
Christopher Fauletf2b02cf2023-01-13 11:02:28 +0100988 /* request errors are most likely due to the server aborting the
989 * transfer. */
990 goto return_srv_abort;
991 }
992
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200993 http_end_request(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200994 if (!(req->analysers & an_bit)) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100995 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200996 return 1;
997 }
998
999 /* If "option abortonclose" is set on the backend, we want to monitor
1000 * the client's connection and forward any shutdown notification to the
1001 * server, which will decide whether to close or to go on processing the
1002 * request. We only do that in tunnel mode, and not in other modes since
1003 * it can be abused to exhaust source ports. */
Christopher Faulet769d0e92019-03-22 14:23:18 +01001004 if (s->be->options & PR_O_ABRT_CLOSE) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001005 channel_auto_read(req);
Christopher Faulet87633c32023-04-03 18:32:50 +02001006 if ((chn_prod(req)->flags & SC_FL_SHUTR) && !(txn->flags & TX_CON_WANT_TUN))
Willy Tarreaucb041662022-05-17 19:44:42 +02001007 s->scb->flags |= SC_FL_NOLINGER;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001008 channel_auto_close(req);
1009 }
1010 else if (s->txn->meth == HTTP_METH_POST) {
1011 /* POST requests may require to read extra CRLF sent by broken
1012 * browsers and which could cause an RST to be sent upon close
1013 * on some systems (eg: Linux). */
1014 channel_auto_read(req);
1015 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001016 DBG_TRACE_DEVEL("waiting for the end of the HTTP txn",
1017 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001018 return 0;
1019
1020 missing_data_or_waiting:
1021 /* stop waiting for data if the input is closed before the end */
Christopher Faulet87633c32023-04-03 18:32:50 +02001022 if (msg->msg_state < HTTP_MSG_ENDING && chn_prod(req)->flags & SC_FL_SHUTR)
Christopher Faulet93e02d82019-03-08 14:18:50 +01001023 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001024
1025 waiting:
1026 /* waiting for the last bits to leave the buffer */
Christopher Faulet87633c32023-04-03 18:32:50 +02001027 if (chn_cons(req)->flags & SC_FL_SHUTW)
Christopher Faulet93e02d82019-03-08 14:18:50 +01001028 goto return_srv_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001029
1030 /* When TE: chunked is used, we need to get there again to parse remaining
1031 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
1032 * And when content-length is used, we never want to let the possible
1033 * shutdown be forwarded to the other side, as the state machine will
1034 * take care of it once the client responds. It's also important to
1035 * prevent TIME_WAITs from accumulating on the backend side, and for
1036 * HTTP/2 where the last frame comes with a shutdown.
1037 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001038 if (msg->flags & HTTP_MSGF_XFER_LEN)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001039 channel_dont_close(req);
1040
1041 /* We know that more data are expected, but we couldn't send more that
Christopher Faulet84d3ef92023-03-17 15:45:58 +01001042 * what we did. So we always set the SC_FL_SND_EXP_MORE flag so that the
Christopher Faulete0768eb2018-10-03 16:38:02 +02001043 * system knows it must not set a PUSH on this first part. Interactive
1044 * modes are already handled by the stream sock layer. We must not do
1045 * this in content-length mode because it could present the MSG_MORE
1046 * flag with the last block of forwarded data, which would cause an
1047 * additional delay to be observed by the receiver.
1048 */
Christopher Faulet2151cdd2020-07-22 16:34:59 +02001049 if (HAS_REQ_DATA_FILTERS(s))
Christopher Faulet84d3ef92023-03-17 15:45:58 +01001050 s->scb->flags |= SC_FL_SND_EXP_MORE;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001051
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001052 DBG_TRACE_DEVEL("waiting for more data to forward",
1053 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001054 return 0;
1055
Christopher Faulet93e02d82019-03-08 14:18:50 +01001056 return_cli_abort:
Willy Tarreau4781b152021-04-06 13:53:36 +02001057 _HA_ATOMIC_INC(&sess->fe->fe_counters.cli_aborts);
1058 _HA_ATOMIC_INC(&s->be->be_counters.cli_aborts);
William Lallemand36119de2021-03-08 15:26:48 +01001059 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001060 _HA_ATOMIC_INC(&sess->listener->counters->cli_aborts);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001061 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001062 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.cli_aborts);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001063 if (!(s->flags & SF_ERR_MASK))
Christopher Fauletf2b02cf2023-01-13 11:02:28 +01001064 s->flags |= ((req->flags & CF_READ_TIMEOUT) ? SF_ERR_CLITO : SF_ERR_CLICL);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001065 status = 400;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001066 goto return_prx_cond;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001067
1068 return_srv_abort:
Willy Tarreau4781b152021-04-06 13:53:36 +02001069 _HA_ATOMIC_INC(&sess->fe->fe_counters.srv_aborts);
1070 _HA_ATOMIC_INC(&s->be->be_counters.srv_aborts);
William Lallemand36119de2021-03-08 15:26:48 +01001071 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001072 _HA_ATOMIC_INC(&sess->listener->counters->srv_aborts);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001073 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001074 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.srv_aborts);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001075 if (!(s->flags & SF_ERR_MASK))
Christopher Fauletf2b02cf2023-01-13 11:02:28 +01001076 s->flags |= ((req->flags & CF_WRITE_TIMEOUT) ? SF_ERR_SRVTO : SF_ERR_SRVCL);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001077 status = 502;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001078 goto return_prx_cond;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001079
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001080 return_int_err:
1081 if (!(s->flags & SF_ERR_MASK))
1082 s->flags |= SF_ERR_INTERNAL;
Willy Tarreau4781b152021-04-06 13:53:36 +02001083 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
1084 _HA_ATOMIC_INC(&s->be->be_counters.internal_errors);
William Lallemand36119de2021-03-08 15:26:48 +01001085 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001086 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001087 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001088 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.internal_errors);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001089 status = 500;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001090 goto return_prx_cond;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001091
Christopher Faulet93e02d82019-03-08 14:18:50 +01001092 return_bad_req:
Willy Tarreau4781b152021-04-06 13:53:36 +02001093 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req);
William Lallemand36119de2021-03-08 15:26:48 +01001094 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001095 _HA_ATOMIC_INC(&sess->listener->counters->failed_req);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001096 status = 400;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001097 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001098
Christopher Fauletb8a53712019-12-16 11:29:38 +01001099 return_prx_cond:
Christopher Faulet9768c262018-10-22 09:34:31 +02001100 if (txn->status > 0) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001101 /* Note: we don't send any error if some data were already sent */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001102 http_reply_and_close(s, txn->status, NULL);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001103 } else {
Christopher Faulet93e02d82019-03-08 14:18:50 +01001104 txn->status = status;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001105 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001106 }
Christopher Fauletf2b02cf2023-01-13 11:02:28 +01001107 http_set_term_flags(s);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001108 DBG_TRACE_DEVEL("leaving on error ",
1109 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001110 return 0;
1111}
1112
Willy Tarreau4596fe22022-05-17 19:07:51 +02001113/* Reset the stream and the backend stream connector to a situation suitable for attemption connection */
Olivier Houcharda254a372019-04-05 15:30:12 +02001114/* Returns 0 if we can attempt to retry, -1 otherwise */
Willy Tarreaub49672d2022-05-27 10:13:37 +02001115static __inline int do_l7_retry(struct stream *s, struct stconn *sc)
Olivier Houcharda254a372019-04-05 15:30:12 +02001116{
Christopher Faulet9f5382e2021-05-21 13:46:14 +02001117 struct channel *req, *res;
1118 int co_data;
Olivier Houcharda254a372019-04-05 15:30:12 +02001119
Christopher Faulet731c8e62022-03-29 16:08:44 +02001120 if (s->conn_retries >= s->be->conn_retries)
Christopher Faulet552601d2021-05-26 10:31:06 +02001121 return -1;
Christopher Faulet41ade742023-03-01 15:45:39 +01001122 s->conn_retries++;
Christopher Faulete763c8c2021-05-05 18:23:59 +02001123 if (objt_server(s->target)) {
1124 if (s->flags & SF_CURR_SESS) {
1125 s->flags &= ~SF_CURR_SESS;
1126 _HA_ATOMIC_DEC(&__objt_server(s->target)->cur_sess);
1127 }
Willy Tarreau4781b152021-04-06 13:53:36 +02001128 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.retries);
Christopher Faulete763c8c2021-05-05 18:23:59 +02001129 }
Willy Tarreau4781b152021-04-06 13:53:36 +02001130 _HA_ATOMIC_INC(&s->be->be_counters.retries);
Willy Tarreau223995e2019-05-04 10:38:31 +02001131
Christopher Faulet9f5382e2021-05-21 13:46:14 +02001132 req = &s->req;
1133 res = &s->res;
Christopher Faulet87633c32023-04-03 18:32:50 +02001134
Olivier Houcharda254a372019-04-05 15:30:12 +02001135 /* Remove any write error from the request, and read error from the response */
Christopher Faulet87633c32023-04-03 18:32:50 +02001136 s->scf->flags &= ~(SC_FL_SHUTR|SC_FL_SHUTR_NOW);
1137 req->flags &= ~CF_WRITE_TIMEOUT;
1138 res->flags &= ~(CF_READ_TIMEOUT | CF_READ_EVENT);
Christopher Faulet7bf46bb2022-01-04 10:56:03 +01001139 res->analysers &= AN_RES_FLT_END;
Christopher Faulet50264b42022-03-30 19:39:30 +02001140 s->conn_err_type = STRM_ET_NONE;
Christopher Fauletae024ce2022-03-29 19:02:31 +02001141 s->flags &= ~(SF_CONN_EXP | SF_ERR_MASK | SF_FINST_MASK);
1142 s->conn_exp = TICK_ETERNITY;
Olivier Houchard4bd58672019-07-12 16:16:59 +02001143 stream_choose_redispatch(s);
Olivier Houcharda254a372019-04-05 15:30:12 +02001144 res->to_forward = 0;
1145 res->analyse_exp = TICK_ETERNITY;
1146 res->total = 0;
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +01001147
Christopher Faulet87633c32023-04-03 18:32:50 +02001148 s->scb->flags &= ~(SC_FL_SHUTW|SC_FL_SHUTW_NOW);
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 Faulet6f78ac52023-03-01 15:47:18 +01001222
1223 if ((txn->flags & TX_L7_RETRY) &&
1224 (s->be->retry_type & PR_RE_DISCONNECTED) &&
1225 (!conn || conn->err_code != CO_ER_SSL_EARLY_FAILED)) {
1226 if (co_data(rep) || do_l7_retry(s, s->scb) == 0)
1227 return 0;
1228 }
1229
1230 /* Perform a L7 retry on empty response or because server refuses the early data. */
Christopher Faulete05bf9e2022-03-29 15:23:40 +02001231 if ((txn->flags & TX_L7_RETRY) &&
Christopher Fauletd9769232021-05-26 12:15:37 +02001232 (s->be->retry_type & PR_RE_EARLY_ERROR) &&
1233 conn && conn->err_code == CO_ER_SSL_EARLY_FAILED &&
Willy Tarreau7cb9e6c2022-05-17 19:40:40 +02001234 do_l7_retry(s, s->scb) == 0) {
Christopher Fauletd9769232021-05-26 12:15:37 +02001235 DBG_TRACE_DEVEL("leaving on L7 retry",
1236 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
1237 return 0;
1238 }
1239
Olivier Houchard6db16992019-05-17 15:40:49 +02001240 if (txn->flags & TX_NOT_FIRST)
1241 goto abort_keep_alive;
1242
Willy Tarreau4781b152021-04-06 13:53:36 +02001243 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001244 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001245 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001246 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001247 }
1248
Christopher Fauletd9769232021-05-26 12:15:37 +02001249 /* if the server refused the early data, just send a 425 */
1250 if (conn && conn->err_code == CO_ER_SSL_EARLY_FAILED)
Olivier Houchard865d8392019-05-03 22:46:27 +02001251 txn->status = 425;
Christopher Fauletd9769232021-05-26 12:15:37 +02001252 else {
1253 txn->status = 502;
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001254 stream_inc_http_fail_ctr(s);
Christopher Fauletd9769232021-05-26 12:15:37 +02001255 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001256
Willy Tarreaucb041662022-05-17 19:44:42 +02001257 s->scb->flags |= SC_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001258 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001259
1260 if (!(s->flags & SF_ERR_MASK))
1261 s->flags |= SF_ERR_SRVCL;
Christopher Faulet4a66c942023-01-13 09:43:21 +01001262 http_set_term_flags(s);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001263 DBG_TRACE_DEVEL("leaving on error",
1264 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001265 return 0;
1266 }
1267
Christopher Faulet9768c262018-10-22 09:34:31 +02001268 /* 2: read timeout : return a 504 to the client. */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001269 else if (rep->flags & CF_READ_TIMEOUT) {
Christopher Faulete05bf9e2022-03-29 15:23:40 +02001270 if ((txn->flags & TX_L7_RETRY) &&
Olivier Houcharda254a372019-04-05 15:30:12 +02001271 (s->be->retry_type & PR_RE_TIMEOUT)) {
Willy Tarreau7cb9e6c2022-05-17 19:40:40 +02001272 if (co_data(rep) || do_l7_retry(s, s->scb) == 0) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001273 DBG_TRACE_DEVEL("leaving on L7 retry",
1274 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharda254a372019-04-05 15:30:12 +02001275 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001276 }
Olivier Houcharda254a372019-04-05 15:30:12 +02001277 }
Willy Tarreau4781b152021-04-06 13:53:36 +02001278 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001279 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001280 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001281 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001282 }
1283
Christopher Faulete0768eb2018-10-03 16:38:02 +02001284 txn->status = 504;
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001285 stream_inc_http_fail_ctr(s);
Willy Tarreaucb041662022-05-17 19:44:42 +02001286 s->scb->flags |= SC_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001287 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001288
1289 if (!(s->flags & SF_ERR_MASK))
1290 s->flags |= SF_ERR_SRVTO;
Christopher Faulet4a66c942023-01-13 09:43:21 +01001291 http_set_term_flags(s);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001292 DBG_TRACE_DEVEL("leaving on error",
1293 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001294 return 0;
1295 }
1296
Christopher Faulet9768c262018-10-22 09:34:31 +02001297 /* 3: client abort with an abortonclose */
Christopher Faulet87633c32023-04-03 18:32:50 +02001298 else if ((chn_prod(rep)->flags & SC_FL_SHUTR) &&
1299 (chn_prod(&s->req)->flags & SC_FL_SHUTR) &&
1300 (chn_cons(&s->req)->flags & SC_FL_SHUTW)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001301 _HA_ATOMIC_INC(&sess->fe->fe_counters.cli_aborts);
1302 _HA_ATOMIC_INC(&s->be->be_counters.cli_aborts);
William Lallemand36119de2021-03-08 15:26:48 +01001303 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001304 _HA_ATOMIC_INC(&sess->listener->counters->cli_aborts);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001305 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001306 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.cli_aborts);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001307
Christopher Faulete0768eb2018-10-03 16:38:02 +02001308 txn->status = 400;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001309 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001310
1311 if (!(s->flags & SF_ERR_MASK))
1312 s->flags |= SF_ERR_CLICL;
Christopher Faulet4a66c942023-01-13 09:43:21 +01001313 http_set_term_flags(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001314
1315 /* process_stream() will take care of the error */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001316 DBG_TRACE_DEVEL("leaving on error",
1317 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001318 return 0;
1319 }
1320
Christopher Faulet9768c262018-10-22 09:34:31 +02001321 /* 4: close from server, capture the response if the server has started to respond */
Christopher Faulet87633c32023-04-03 18:32:50 +02001322 else if (chn_prod(rep)->flags & SC_FL_SHUTR) {
Christopher Faulete05bf9e2022-03-29 15:23:40 +02001323 if ((txn->flags & TX_L7_RETRY) &&
Olivier Houcharda254a372019-04-05 15:30:12 +02001324 (s->be->retry_type & PR_RE_DISCONNECTED)) {
Willy Tarreau7cb9e6c2022-05-17 19:40:40 +02001325 if (co_data(rep) || do_l7_retry(s, s->scb) == 0) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001326 DBG_TRACE_DEVEL("leaving on L7 retry",
1327 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharda254a372019-04-05 15:30:12 +02001328 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001329 }
Olivier Houcharda254a372019-04-05 15:30:12 +02001330 }
1331
Olivier Houchard6db16992019-05-17 15:40:49 +02001332 if (txn->flags & TX_NOT_FIRST)
1333 goto abort_keep_alive;
1334
Willy Tarreau4781b152021-04-06 13:53:36 +02001335 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001336 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001337 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001338 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001339 }
1340
Christopher Faulete0768eb2018-10-03 16:38:02 +02001341 txn->status = 502;
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001342 stream_inc_http_fail_ctr(s);
Willy Tarreaucb041662022-05-17 19:44:42 +02001343 s->scb->flags |= SC_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001344 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001345
1346 if (!(s->flags & SF_ERR_MASK))
1347 s->flags |= SF_ERR_SRVCL;
Christopher Faulet4a66c942023-01-13 09:43:21 +01001348 http_set_term_flags(s);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001349 DBG_TRACE_DEVEL("leaving on error",
1350 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001351 return 0;
1352 }
1353
Christopher Faulet9768c262018-10-22 09:34:31 +02001354 /* 5: write error to client (we don't send any message then) */
Christopher Faulet2e56a732023-01-26 16:18:09 +01001355 else if (sc_ep_test(s->scf, SE_FL_ERR_PENDING)) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001356 if (txn->flags & TX_NOT_FIRST)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001357 goto abort_keep_alive;
1358
Willy Tarreau4781b152021-04-06 13:53:36 +02001359 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001360 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001361 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001362 rep->analysers &= AN_RES_FLT_END;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001363
1364 if (!(s->flags & SF_ERR_MASK))
1365 s->flags |= SF_ERR_CLICL;
Christopher Faulet4a66c942023-01-13 09:43:21 +01001366 http_set_term_flags(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001367
1368 /* process_stream() will take care of the error */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001369 DBG_TRACE_DEVEL("leaving on error",
1370 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001371 return 0;
1372 }
1373
1374 channel_dont_close(rep);
Christopher Faulet9a790f62023-03-16 14:40:03 +01001375 s->scb->flags |= SC_FL_RCV_ONCE; /* try to get back here ASAP */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001376 DBG_TRACE_DEVEL("waiting for more data",
1377 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001378 return 0;
1379 }
1380
1381 /* More interesting part now : we know that we have a complete
1382 * response which at least looks like HTTP. We have an indicator
1383 * of each header's length, so we can parse them quickly.
1384 */
Christopher Faulet29f17582019-05-23 11:03:26 +02001385 BUG_ON(htx_get_first_type(htx) != HTX_BLK_RES_SL);
Christopher Faulet297fbb42019-05-13 14:41:27 +02001386 sl = http_get_stline(htx);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001387
Christopher Faulet1f08bff2021-05-26 13:14:39 +02001388 /* Perform a L7 retry because of the status code */
Christopher Faulete05bf9e2022-03-29 15:23:40 +02001389 if ((txn->flags & TX_L7_RETRY) &&
Christopher Faulet1f08bff2021-05-26 13:14:39 +02001390 l7_status_match(s->be, sl->info.res.status) &&
Willy Tarreau7cb9e6c2022-05-17 19:40:40 +02001391 do_l7_retry(s, s->scb) == 0) {
Christopher Faulet1f08bff2021-05-26 13:14:39 +02001392 DBG_TRACE_DEVEL("leaving on L7 retry", STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
1393 return 0;
1394 }
1395
1396 /* Now, L7 buffer is useless, it can be released */
Christopher Faulete05bf9e2022-03-29 15:23:40 +02001397 b_free(&txn->l7_buffer);
Christopher Faulet1f08bff2021-05-26 13:14:39 +02001398
1399 msg->msg_state = HTTP_MSG_BODY;
1400
1401
Christopher Faulet9768c262018-10-22 09:34:31 +02001402 /* 0: we might have to print this header in debug mode */
1403 if (unlikely((global.mode & MODE_DEBUG) &&
1404 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
1405 int32_t pos;
1406
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001407 http_debug_stline("srvrep", s, sl);
Christopher Faulet9768c262018-10-22 09:34:31 +02001408
Christopher Fauleta3f15502019-05-13 15:27:23 +02001409 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001410 struct htx_blk *blk = htx_get_blk(htx, pos);
1411 enum htx_blk_type type = htx_get_blk_type(blk);
1412
1413 if (type == HTX_BLK_EOH)
1414 break;
1415 if (type != HTX_BLK_HDR)
1416 continue;
1417
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001418 http_debug_hdr("srvhdr", s,
1419 htx_get_blk_name(htx, blk),
1420 htx_get_blk_value(htx, blk));
Christopher Faulet9768c262018-10-22 09:34:31 +02001421 }
1422 }
1423
Christopher Faulet03599112018-11-27 11:21:21 +01001424 /* 1: get the status code and the version. Also set HTTP flags */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001425 txn->status = sl->info.res.status;
Christopher Faulet03599112018-11-27 11:21:21 +01001426 if (sl->flags & HTX_SL_F_VER_11)
Christopher Faulet9768c262018-10-22 09:34:31 +02001427 msg->flags |= HTTP_MSGF_VER_11;
Christopher Faulet03599112018-11-27 11:21:21 +01001428 if (sl->flags & HTX_SL_F_XFER_LEN) {
1429 msg->flags |= HTTP_MSGF_XFER_LEN;
Christopher Faulet2a408542020-11-20 14:22:37 +01001430 if (sl->flags & HTX_SL_F_CLEN)
1431 msg->flags |= HTTP_MSGF_CNT_LEN;
1432 else if (sl->flags & HTX_SL_F_CHNK)
1433 msg->flags |= HTTP_MSGF_TE_CHNK;
Christopher Faulet03599112018-11-27 11:21:21 +01001434 }
Christopher Faulet2a408542020-11-20 14:22:37 +01001435 if (sl->flags & HTX_SL_F_BODYLESS)
1436 msg->flags |= HTTP_MSGF_BODYLESS;
Christopher Faulet576c3582021-01-08 15:53:01 +01001437 if (sl->flags & HTX_SL_F_CONN_UPG)
1438 msg->flags |= HTTP_MSGF_CONN_UPG;
Christopher Faulet9768c262018-10-22 09:34:31 +02001439
1440 n = txn->status / 100;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001441 if (n < 1 || n > 5)
1442 n = 0;
Christopher Faulet9768c262018-10-22 09:34:31 +02001443
Christopher Faulete0768eb2018-10-03 16:38:02 +02001444 /* when the client triggers a 4xx from the server, it's most often due
1445 * to a missing object or permission. These events should be tracked
1446 * because if they happen often, it may indicate a brute force or a
1447 * vulnerability scan.
1448 */
1449 if (n == 4)
1450 stream_inc_http_err_ctr(s);
1451
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001452 if (n == 5 && txn->status != 501 && txn->status != 505)
1453 stream_inc_http_fail_ctr(s);
1454
Marcin Deranek3c27dda2020-05-15 18:32:51 +02001455 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001456 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.p.http.rsp[n]);
1457 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.p.http.cum_req);
Marcin Deranek3c27dda2020-05-15 18:32:51 +02001458 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001459
Christopher Faulete0768eb2018-10-03 16:38:02 +02001460 /* Adjust server's health based on status code. Note: status codes 501
1461 * and 505 are triggered on demand by client request, so we must not
1462 * count them as server failures.
1463 */
1464 if (objt_server(s->target)) {
1465 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001466 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_OK);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001467 else
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001468 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_STS);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001469 }
1470
1471 /*
1472 * We may be facing a 100-continue response, or any other informational
1473 * 1xx response which is non-final, in which case this is not the right
1474 * response, and we're waiting for the next one. Let's allow this response
1475 * to go to the client and wait for the next one. There's an exception for
1476 * 101 which is used later in the code to switch protocols.
1477 */
1478 if (txn->status < 200 &&
1479 (txn->status == 100 || txn->status >= 102)) {
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001480 FLT_STRM_CB(s, flt_http_reset(s, msg));
Christopher Faulet421e7692019-06-13 11:16:45 +02001481 htx->first = channel_htx_fwd_headers(rep, htx);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001482 msg->msg_state = HTTP_MSG_RPBEFORE;
Christopher Faulet3499f622019-09-03 15:23:54 +02001483 msg->flags = 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001484 txn->status = 0;
1485 s->logs.t_data = -1; /* was not a response yet */
Christopher Faulet5c281d52023-03-16 15:53:28 +01001486 s->scf->flags |= SC_FL_SND_ASAP; /* Send ASAP informational messages */
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001487 goto next_one;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001488 }
1489
Christopher Faulet6e6c7b12021-01-08 16:02:05 +01001490 /* A 101-switching-protocols must contains a Connection header with the
1491 * "upgrade" option and the request too. It means both are agree to
1492 * upgrade. It is not so strict because there is no test on the Upgrade
1493 * header content. But it is probably stronger enough for now.
1494 */
1495 if (txn->status == 101 &&
1496 (!(txn->req.flags & HTTP_MSGF_CONN_UPG) || !(txn->rsp.flags & HTTP_MSGF_CONN_UPG)))
1497 goto return_bad_res;
1498
Christopher Faulete0768eb2018-10-03 16:38:02 +02001499 /*
1500 * 2: check for cacheability.
1501 */
1502
1503 switch (txn->status) {
1504 case 200:
1505 case 203:
1506 case 204:
1507 case 206:
1508 case 300:
1509 case 301:
1510 case 404:
1511 case 405:
1512 case 410:
1513 case 414:
1514 case 501:
1515 break;
1516 default:
1517 /* RFC7231#6.1:
1518 * Responses with status codes that are defined as
1519 * cacheable by default (e.g., 200, 203, 204, 206,
1520 * 300, 301, 404, 405, 410, 414, and 501 in this
1521 * specification) can be reused by a cache with
1522 * heuristic expiration unless otherwise indicated
1523 * by the method definition or explicit cache
1524 * controls [RFC7234]; all other status codes are
1525 * not cacheable by default.
1526 */
1527 txn->flags &= ~(TX_CACHEABLE | TX_CACHE_COOK);
1528 break;
1529 }
1530
1531 /*
1532 * 3: we may need to capture headers
1533 */
1534 s->logs.logwait &= ~LW_RESP;
1535 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->res_cap))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001536 http_capture_headers(htx, s->res_cap, sess->fe->rsp_cap);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001537
Christopher Faulet9768c262018-10-22 09:34:31 +02001538 /* Skip parsing if no content length is possible. */
Christopher Fauletc75668e2020-12-07 18:10:32 +01001539 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status >= 200 && txn->status < 300) ||
Christopher Faulete0768eb2018-10-03 16:38:02 +02001540 txn->status == 101)) {
1541 /* Either we've established an explicit tunnel, or we're
1542 * switching the protocol. In both cases, we're very unlikely
1543 * to understand the next protocols. We have to switch to tunnel
1544 * mode, so that we transfer the request and responses then let
1545 * this protocol pass unmodified. When we later implement specific
1546 * parsers for such protocols, we'll want to check the Upgrade
1547 * header which contains information about that protocol for
1548 * responses with status 101 (eg: see RFC2817 about TLS).
1549 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02001550 txn->flags |= TX_CON_WANT_TUN;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001551 }
1552
Christopher Faulet61608322018-11-23 16:23:45 +01001553 /* check for NTML authentication headers in 401 (WWW-Authenticate) and
1554 * 407 (Proxy-Authenticate) responses and set the connection to private
1555 */
Willy Tarreaufd9417b2022-05-18 16:23:22 +02001556 srv_conn = sc_conn(s->scb);
Christopher Faulet61608322018-11-23 16:23:45 +01001557 if (srv_conn) {
1558 struct ist hdr;
1559 struct http_hdr_ctx ctx;
1560
1561 if (txn->status == 401)
1562 hdr = ist("WWW-Authenticate");
1563 else if (txn->status == 407)
1564 hdr = ist("Proxy-Authenticate");
1565 else
1566 goto end;
1567
1568 ctx.blk = NULL;
1569 while (http_find_header(htx, hdr, &ctx, 0)) {
Willy Tarreauf1dcced2020-05-07 19:27:02 +02001570 /* If www-authenticate contains "Negotiate", "Nego2", or "NTLM",
1571 * possibly followed by blanks and a base64 string, the connection
1572 * is private. Since it's a mess to deal with, we only check for
1573 * values starting with "NTLM" or "Nego". Note that often multiple
1574 * headers are sent by the server there.
1575 */
1576 if ((ctx.value.len >= 4 && strncasecmp(ctx.value.ptr, "Nego", 4) == 0) ||
Willy Tarreau49a1d282020-05-07 19:10:15 +02001577 (ctx.value.len >= 4 && strncasecmp(ctx.value.ptr, "NTLM", 4) == 0)) {
Olivier Houchard250031e2019-05-29 15:01:50 +02001578 sess->flags |= SESS_FL_PREFER_LAST;
Christopher Faulet08016ab2020-07-01 16:10:06 +02001579 conn_set_owner(srv_conn, sess, NULL);
Christopher Faulet21ddc742020-07-01 15:26:14 +02001580 conn_set_private(srv_conn);
Ilya Shipitsin6b79f382020-07-23 00:32:55 +05001581 /* If it fail now, the same will be done in mux->detach() callback */
Christopher Faulet08016ab2020-07-01 16:10:06 +02001582 session_add_conn(srv_conn->owner, srv_conn, srv_conn->target);
Willy Tarreauf1dcced2020-05-07 19:27:02 +02001583 break;
Olivier Houchard250031e2019-05-29 15:01:50 +02001584 }
Christopher Faulet61608322018-11-23 16:23:45 +01001585 }
1586 }
1587
1588 end:
Christopher Faulete0768eb2018-10-03 16:38:02 +02001589 /* we want to have the response time before we start processing it */
1590 s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now);
1591
1592 /* end of job, return OK */
1593 rep->analysers &= ~an_bit;
1594 rep->analyse_exp = TICK_ETERNITY;
1595 channel_auto_close(rep);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001596 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001597 return 1;
1598
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001599 return_int_err:
Willy Tarreau4781b152021-04-06 13:53:36 +02001600 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
1601 _HA_ATOMIC_INC(&s->be->be_counters.internal_errors);
William Lallemand36119de2021-03-08 15:26:48 +01001602 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001603 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Fauletb8a53712019-12-16 11:29:38 +01001604 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001605 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.internal_errors);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001606 txn->status = 500;
1607 if (!(s->flags & SF_ERR_MASK))
1608 s->flags |= SF_ERR_INTERNAL;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001609 goto return_prx_cond;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001610
1611 return_bad_res:
Willy Tarreau4781b152021-04-06 13:53:36 +02001612 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Faulet47365272018-10-31 17:40:50 +01001613 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001614 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001615 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Christopher Faulet47365272018-10-31 17:40:50 +01001616 }
Olivier Houcharde3249a92019-05-03 23:01:47 +02001617 if ((s->be->retry_type & PR_RE_JUNK_REQUEST) &&
Christopher Faulete05bf9e2022-03-29 15:23:40 +02001618 (txn->flags & TX_L7_RETRY) &&
Willy Tarreau7cb9e6c2022-05-17 19:40:40 +02001619 do_l7_retry(s, s->scb) == 0) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001620 DBG_TRACE_DEVEL("leaving on L7 retry",
1621 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharde3249a92019-05-03 23:01:47 +02001622 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001623 }
Christopher Faulet47365272018-10-31 17:40:50 +01001624 txn->status = 502;
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001625 stream_inc_http_fail_ctr(s);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001626 /* fall through */
1627
Christopher Fauletb8a53712019-12-16 11:29:38 +01001628 return_prx_cond:
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001629 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet4a66c942023-01-13 09:43:21 +01001630 http_set_term_flags(s);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001631
Willy Tarreaucb041662022-05-17 19:44:42 +02001632 s->scb->flags |= SC_FL_NOLINGER;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001633 DBG_TRACE_DEVEL("leaving on error",
1634 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulet47365272018-10-31 17:40:50 +01001635 return 0;
1636
Christopher Faulete0768eb2018-10-03 16:38:02 +02001637 abort_keep_alive:
1638 /* A keep-alive request to the server failed on a network error.
1639 * The client is required to retry. We need to close without returning
1640 * any other information so that the client retries.
1641 */
1642 txn->status = 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001643 s->logs.logwait = 0;
1644 s->logs.level = 0;
Christopher Faulet84d3ef92023-03-17 15:45:58 +01001645 s->scf->flags &= ~SC_FL_SND_EXP_MORE; /* speed up sending a previous response */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001646 http_reply_and_close(s, txn->status, NULL);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001647 DBG_TRACE_DEVEL("leaving by closing K/A connection",
1648 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001649 return 0;
1650}
1651
1652/* This function performs all the processing enabled for the current response.
1653 * It normally returns 1 unless it wants to break. It relies on buffers flags,
1654 * and updates s->res.analysers. It might make sense to explode it into several
1655 * other functions. It works like process_request (see indications above).
1656 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001657int http_process_res_common(struct stream *s, struct channel *rep, int an_bit, struct proxy *px)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001658{
1659 struct session *sess = s->sess;
1660 struct http_txn *txn = s->txn;
1661 struct http_msg *msg = &txn->rsp;
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001662 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001663 struct proxy *cur_proxy;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001664 enum rule_result ret = HTTP_RULE_RES_CONT;
1665
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001666 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
1667 return 0;
Christopher Faulet9768c262018-10-22 09:34:31 +02001668
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001669 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001670
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001671 htx = htxbuf(&rep->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001672
1673 /* The stats applet needs to adjust the Connection header but we don't
1674 * apply any filter there.
1675 */
1676 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
1677 rep->analysers &= ~an_bit;
1678 rep->analyse_exp = TICK_ETERNITY;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001679 goto end;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001680 }
1681
1682 /*
1683 * We will have to evaluate the filters.
1684 * As opposed to version 1.2, now they will be evaluated in the
1685 * filters order and not in the header order. This means that
1686 * each filter has to be validated among all headers.
1687 *
1688 * Filters are tried with ->be first, then with ->fe if it is
1689 * different from ->be.
1690 *
1691 * Maybe we are in resume condiion. In this case I choose the
1692 * "struct proxy" which contains the rule list matching the resume
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05001693 * pointer. If none of these "struct proxy" match, I initialise
Christopher Faulete0768eb2018-10-03 16:38:02 +02001694 * the process with the first one.
1695 *
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05001696 * In fact, I check only correspondence between the current list
Christopher Faulete0768eb2018-10-03 16:38:02 +02001697 * pointer and the ->fe rule list. If it doesn't match, I initialize
1698 * the loop with the ->be.
1699 */
Christopher Fauletd4150ad2021-10-13 15:35:55 +02001700 if (s->current_rule_list == &sess->fe->http_res_rules ||
1701 (sess->fe->defpx && s->current_rule_list == &sess->fe->defpx->http_res_rules))
Christopher Faulete0768eb2018-10-03 16:38:02 +02001702 cur_proxy = sess->fe;
1703 else
1704 cur_proxy = s->be;
Christopher Fauletd4150ad2021-10-13 15:35:55 +02001705
Christopher Faulete0768eb2018-10-03 16:38:02 +02001706 while (1) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001707 /* evaluate http-response rules */
Christopher Faulet46f46df2021-11-09 16:33:25 +01001708 if (ret == HTTP_RULE_RES_CONT || ret == HTTP_RULE_RES_STOP) {
Christopher Fauletd4150ad2021-10-13 15:35:55 +02001709 struct list *def_rules, *rules;
1710
1711 def_rules = ((cur_proxy->defpx && (cur_proxy == s->be || cur_proxy->defpx != s->be->defpx)) ? &cur_proxy->defpx->http_res_rules : NULL);
1712 rules = &cur_proxy->http_res_rules;
1713
1714 ret = http_res_get_intercept_rule(cur_proxy, def_rules, rules, s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001715
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001716 switch (ret) {
1717 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
1718 goto return_prx_yield;
1719
1720 case HTTP_RULE_RES_CONT:
1721 case HTTP_RULE_RES_STOP: /* nothing to do */
1722 break;
1723
1724 case HTTP_RULE_RES_DENY: /* deny or tarpit */
1725 goto deny;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001726
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001727 case HTTP_RULE_RES_ABRT: /* abort request, response already sent */
1728 goto return_prx_cond;
1729
1730 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
Christopher Fauletb8a53712019-12-16 11:29:38 +01001731 goto done;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001732
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001733 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
1734 goto return_bad_res;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001735
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001736 case HTTP_RULE_RES_ERROR: /* failed with a bad request */
1737 goto return_int_err;
1738 }
1739
1740 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001741
Christopher Faulete0768eb2018-10-03 16:38:02 +02001742 /* check whether we're already working on the frontend */
1743 if (cur_proxy == sess->fe)
1744 break;
1745 cur_proxy = sess->fe;
1746 }
1747
Christopher Faulete0768eb2018-10-03 16:38:02 +02001748 /* OK that's all we can do for 1xx responses */
1749 if (unlikely(txn->status < 200 && txn->status != 101))
Christopher Fauletf2824e62018-10-01 12:12:37 +02001750 goto end;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001751
1752 /*
1753 * Now check for a server cookie.
1754 */
1755 if (s->be->cookie_name || sess->fe->capture_name || (s->be->options & PR_O_CHK_CACHE))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001756 http_manage_server_side_cookies(s, rep);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001757
1758 /*
1759 * Check for cache-control or pragma headers if required.
1760 */
1761 if ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001762 http_check_response_for_cacheability(s, rep);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001763
1764 /*
1765 * Add server cookie in the response if needed
1766 */
1767 if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) &&
1768 !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) &&
1769 (!(s->flags & SF_DIRECT) ||
1770 ((s->be->cookie_maxidle || txn->cookie_last_date) &&
1771 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
1772 (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
1773 (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
1774 (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
1775 !(s->flags & SF_IGNORE_PRST)) {
1776 /* the server is known, it's not the one the client requested, or the
1777 * cookie's last seen date needs to be refreshed. We have to
1778 * insert a set-cookie here, except if we want to insert only on POST
1779 * requests and this one isn't. Note that servers which don't have cookies
1780 * (eg: some backup servers) will return a full cookie removal request.
1781 */
Willy Tarreau88bc8002021-12-06 07:01:02 +00001782 if (!__objt_server(s->target)->cookie) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001783 chunk_printf(&trash,
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001784 "%s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
Christopher Faulete0768eb2018-10-03 16:38:02 +02001785 s->be->cookie_name);
1786 }
1787 else {
Willy Tarreau88bc8002021-12-06 07:01:02 +00001788 chunk_printf(&trash, "%s=%s", s->be->cookie_name, __objt_server(s->target)->cookie);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001789
1790 if (s->be->cookie_maxidle || s->be->cookie_maxlife) {
1791 /* emit last_date, which is mandatory */
1792 trash.area[trash.data++] = COOKIE_DELIM_DATE;
1793 s30tob64((date.tv_sec+3) >> 2,
1794 trash.area + trash.data);
1795 trash.data += 5;
1796
1797 if (s->be->cookie_maxlife) {
1798 /* emit first_date, which is either the original one or
1799 * the current date.
1800 */
1801 trash.area[trash.data++] = COOKIE_DELIM_DATE;
1802 s30tob64(txn->cookie_first_date ?
1803 txn->cookie_first_date >> 2 :
1804 (date.tv_sec+3) >> 2,
1805 trash.area + trash.data);
1806 trash.data += 5;
1807 }
1808 }
1809 chunk_appendf(&trash, "; path=/");
1810 }
1811
1812 if (s->be->cookie_domain)
1813 chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain);
1814
1815 if (s->be->ck_opts & PR_CK_HTTPONLY)
1816 chunk_appendf(&trash, "; HttpOnly");
1817
1818 if (s->be->ck_opts & PR_CK_SECURE)
1819 chunk_appendf(&trash, "; Secure");
1820
Christopher Faulet2f533902020-01-21 11:06:48 +01001821 if (s->be->cookie_attrs)
1822 chunk_appendf(&trash, "; %s", s->be->cookie_attrs);
1823
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001824 if (unlikely(!http_add_header(htx, ist("Set-Cookie"), ist2(trash.area, trash.data))))
Christopher Fauletd649b572022-06-01 17:42:35 +02001825 goto return_fail_rewrite;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001826
1827 txn->flags &= ~TX_SCK_MASK;
1828 if (__objt_server(s->target)->cookie && (s->flags & SF_DIRECT))
1829 /* the server did not change, only the date was updated */
1830 txn->flags |= TX_SCK_UPDATED;
1831 else
1832 txn->flags |= TX_SCK_INSERTED;
1833
1834 /* Here, we will tell an eventual cache on the client side that we don't
1835 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
1836 * Some caches understand the correct form: 'no-cache="set-cookie"', but
1837 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
1838 */
1839 if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
1840
1841 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
1842
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001843 if (unlikely(!http_add_header(htx, ist("Cache-control"), ist("private"))))
Christopher Fauletd649b572022-06-01 17:42:35 +02001844 goto return_fail_rewrite;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001845 }
1846 }
1847
1848 /*
1849 * Check if result will be cacheable with a cookie.
1850 * We'll block the response if security checks have caught
1851 * nasty things such as a cacheable cookie.
1852 */
1853 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
1854 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
1855 (s->be->options & PR_O_CHK_CACHE)) {
1856 /* we're in presence of a cacheable response containing
1857 * a set-cookie header. We'll block it as requested by
1858 * the 'checkcache' option, and send an alert.
1859 */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001860 ha_alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreau88bc8002021-12-06 07:01:02 +00001861 s->be->id, objt_server(s->target) ? __objt_server(s->target)->id : "<dispatch>");
Christopher Faulete0768eb2018-10-03 16:38:02 +02001862 send_log(s->be, LOG_ALERT,
1863 "Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreau88bc8002021-12-06 07:01:02 +00001864 s->be->id, objt_server(s->target) ? __objt_server(s->target)->id : "<dispatch>");
Christopher Fauletb8a53712019-12-16 11:29:38 +01001865 goto deny;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001866 }
1867
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001868 end:
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01001869 /*
1870 * Evaluate after-response rules before forwarding the response. rules
1871 * from the backend are evaluated first, then one from the frontend if
1872 * it differs.
1873 */
1874 if (!http_eval_after_res_rules(s))
1875 goto return_int_err;
1876
Christopher Fauletc2ac5e42021-03-08 18:20:09 +01001877 /* Filter the response headers if there are filters attached to the
1878 * stream.
1879 */
1880 if (HAS_FILTERS(s))
1881 rep->analysers |= AN_RES_FLT_HTTP_HDRS;
1882
Christopher Faulete0768eb2018-10-03 16:38:02 +02001883 /* Always enter in the body analyzer */
1884 rep->analysers &= ~AN_RES_FLT_XFER_DATA;
1885 rep->analysers |= AN_RES_HTTP_XFER_BODY;
1886
1887 /* if the user wants to log as soon as possible, without counting
1888 * bytes from the server, then this is the right moment. We have
1889 * to temporarily assign bytes_out to log what we currently have.
1890 */
1891 if (!LIST_ISEMPTY(&sess->fe->logformat) && !(s->logs.logwait & LW_BYTES)) {
1892 s->logs.t_close = s->logs.t_data; /* to get a valid end date */
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001893 s->logs.bytes_out = htx->data;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001894 s->do_log(s);
1895 s->logs.bytes_out = 0;
1896 }
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001897
Christopher Fauletb8a53712019-12-16 11:29:38 +01001898 done:
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01001899 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletb8a53712019-12-16 11:29:38 +01001900 rep->analysers &= ~an_bit;
1901 rep->analyse_exp = TICK_ETERNITY;
Christopher Fauletd4150ad2021-10-13 15:35:55 +02001902 s->current_rule = s->current_rule_list = NULL;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001903 return 1;
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001904
Christopher Fauletb8a53712019-12-16 11:29:38 +01001905 deny:
Willy Tarreau4781b152021-04-06 13:53:36 +02001906 _HA_ATOMIC_INC(&sess->fe->fe_counters.denied_resp);
1907 _HA_ATOMIC_INC(&s->be->be_counters.denied_resp);
William Lallemand36119de2021-03-08 15:26:48 +01001908 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001909 _HA_ATOMIC_INC(&sess->listener->counters->denied_resp);
Christopher Fauleta08546b2019-12-16 16:07:34 +01001910 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001911 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.denied_resp);
Christopher Fauletb8a53712019-12-16 11:29:38 +01001912 goto return_prx_err;
1913
Christopher Fauletd649b572022-06-01 17:42:35 +02001914 return_fail_rewrite:
1915 if (!(s->flags & SF_ERR_MASK))
1916 s->flags |= SF_ERR_PRXCOND;
1917 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_rewrites);
1918 _HA_ATOMIC_INC(&s->be->be_counters.failed_rewrites);
1919 if (sess->listener && sess->listener->counters)
1920 _HA_ATOMIC_INC(&sess->listener->counters->failed_rewrites);
1921 if (objt_server(s->target))
1922 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_rewrites);
1923 /* fall through */
1924
Christopher Fauletb8a53712019-12-16 11:29:38 +01001925 return_int_err:
1926 txn->status = 500;
1927 if (!(s->flags & SF_ERR_MASK))
1928 s->flags |= SF_ERR_INTERNAL;
Willy Tarreau4781b152021-04-06 13:53:36 +02001929 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
1930 _HA_ATOMIC_INC(&s->be->be_counters.internal_errors);
Dragan Dosen9a006f92021-09-21 13:02:09 +02001931 if (sess->listener && sess->listener->counters)
1932 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001933 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001934 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.internal_errors);
Christopher Fauletb8a53712019-12-16 11:29:38 +01001935 goto return_prx_err;
1936
1937 return_bad_res:
1938 txn->status = 502;
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001939 stream_inc_http_fail_ctr(s);
Willy Tarreau4781b152021-04-06 13:53:36 +02001940 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Fauleta20a6532020-02-05 10:16:41 +01001941 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001942 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Christopher Fauleta20a6532020-02-05 10:16:41 +01001943 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_RSP);
1944 }
Christopher Fauletb8a53712019-12-16 11:29:38 +01001945 /* fall through */
1946
1947 return_prx_err:
1948 http_reply_and_close(s, txn->status, http_error_message(s));
1949 /* fall through */
1950
1951 return_prx_cond:
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001952 s->logs.t_data = -1; /* was not a valid response */
Willy Tarreaucb041662022-05-17 19:44:42 +02001953 s->scb->flags |= SC_FL_NOLINGER;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001954
Christopher Faulet4a66c942023-01-13 09:43:21 +01001955 http_set_term_flags(s);
Christopher Fauletb8a53712019-12-16 11:29:38 +01001956
Christopher Faulete58c0002020-03-02 16:21:01 +01001957 rep->analysers &= AN_RES_FLT_END;
1958 s->req.analysers &= AN_REQ_FLT_END;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001959 rep->analyse_exp = TICK_ETERNITY;
Christopher Fauletd4150ad2021-10-13 15:35:55 +02001960 s->current_rule = s->current_rule_list = NULL;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001961 DBG_TRACE_DEVEL("leaving on error",
1962 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001963 return 0;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001964
1965 return_prx_yield:
1966 channel_dont_close(rep);
1967 DBG_TRACE_DEVEL("waiting for more data",
1968 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
1969 return 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001970}
1971
1972/* This function is an analyser which forwards response body (including chunk
1973 * sizes if any). It is called as soon as we must forward, even if we forward
1974 * zero byte. The only situation where it must not be called is when we're in
1975 * tunnel mode and we want to forward till the close. It's used both to forward
1976 * remaining data and to resync after end of body. It expects the msg_state to
1977 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
1978 * read more data, or 1 once we can go on with next request or end the stream.
1979 *
1980 * It is capable of compressing response data both in content-length mode and
1981 * in chunked mode. The state machines follows different flows depending on
1982 * whether content-length and chunked modes are used, since there are no
1983 * trailers in content-length :
1984 *
1985 * chk-mode cl-mode
1986 * ,----- BODY -----.
1987 * / \
1988 * V size > 0 V chk-mode
1989 * .--> SIZE -------------> DATA -------------> CRLF
1990 * | | size == 0 | last byte |
1991 * | v final crlf v inspected |
1992 * | TRAILERS -----------> DONE |
1993 * | |
1994 * `----------------------------------------------'
1995 *
1996 * Compression only happens in the DATA state, and must be flushed in final
1997 * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding
1998 * is performed at once on final states for all bytes parsed, or when leaving
1999 * on missing data.
2000 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002001int http_response_forward_body(struct stream *s, struct channel *res, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +02002002{
2003 struct session *sess = s->sess;
2004 struct http_txn *txn = s->txn;
2005 struct http_msg *msg = &s->txn->rsp;
Christopher Faulet9768c262018-10-22 09:34:31 +02002006 struct htx *htx;
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002007 int ret;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002008
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002009 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002010
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002011 htx = htxbuf(&res->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002012
Christopher Fauletb9a92f32019-09-09 10:15:21 +02002013 if (htx->flags & HTX_FL_PARSING_ERROR)
2014 goto return_bad_res;
2015 if (htx->flags & HTX_FL_PROCESSING_ERROR)
2016 goto return_int_err;
2017
Christopher Faulet9768c262018-10-22 09:34:31 +02002018 if (msg->msg_state == HTTP_MSG_BODY)
2019 msg->msg_state = HTTP_MSG_DATA;
2020
Christopher Faulete0768eb2018-10-03 16:38:02 +02002021 /* in most states, we should abort in case of early close */
2022 channel_auto_close(res);
2023
Christopher Faulete0768eb2018-10-03 16:38:02 +02002024 if (res->to_forward) {
Christopher Faulet66af0b22019-03-22 14:54:52 +01002025 if (res->to_forward == CHN_INFINITE_FORWARD) {
Christopher Faulet904763f2023-03-22 14:53:11 +01002026 if (s->scb->flags & SC_FL_EOI)
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002027 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet66af0b22019-03-22 14:54:52 +01002028 }
2029 else {
2030 /* We can't process the buffer's contents yet */
2031 res->flags |= CF_WAKE_WRITE;
2032 goto missing_data_or_waiting;
2033 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02002034 }
2035
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002036 if (msg->msg_state >= HTTP_MSG_ENDING)
2037 goto ending;
2038
Christopher Fauletc75668e2020-12-07 18:10:32 +01002039 if ((txn->meth == HTTP_METH_CONNECT && txn->status >= 200 && txn->status < 300) || txn->status == 101 ||
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002040 (!(msg->flags & HTTP_MSGF_XFER_LEN) && !HAS_RSP_DATA_FILTERS(s))) {
2041 msg->msg_state = HTTP_MSG_ENDING;
2042 goto ending;
2043 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002044
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002045 /* Forward input data. We get it by removing all outgoing data not
2046 * forwarded yet from HTX data size. If there are some data filters, we
2047 * let them decide the amount of data to forward.
Christopher Faulet9768c262018-10-22 09:34:31 +02002048 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002049 if (HAS_RSP_DATA_FILTERS(s)) {
2050 ret = flt_http_payload(s, msg, htx->data);
2051 if (ret < 0)
2052 goto return_bad_res;
Christopher Faulet421e7692019-06-13 11:16:45 +02002053 c_adv(res, ret);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002054 }
2055 else {
Christopher Faulet421e7692019-06-13 11:16:45 +02002056 c_adv(res, htx->data - co_data(res));
Christopher Faulet2f7c82b2023-02-20 14:06:52 +01002057 if ((global.tune.options & GTUNE_USE_FAST_FWD) && (msg->flags & HTTP_MSGF_XFER_LEN))
Christopher Faulet66af0b22019-03-22 14:54:52 +01002058 channel_htx_forward_forever(res, htx);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002059 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002060
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002061 if (htx->data != co_data(res))
2062 goto missing_data_or_waiting;
2063
Christopher Faulet87633c32023-04-03 18:32:50 +02002064 if (!(msg->flags & HTTP_MSGF_XFER_LEN) && (chn_prod(res)->flags & SC_FL_SHUTR)) {
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002065 msg->msg_state = HTTP_MSG_ENDING;
2066 goto ending;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002067 }
2068
Christopher Faulet9768c262018-10-22 09:34:31 +02002069 /* Check if the end-of-message is reached and if so, switch the message
Christopher Fauletd20fdb02019-06-13 16:43:22 +02002070 * in HTTP_MSG_ENDING state. Then if all data was marked to be
2071 * forwarded, set the state to HTTP_MSG_DONE.
Christopher Faulet9768c262018-10-22 09:34:31 +02002072 */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002073 if (!(htx->flags & HTX_FL_EOM))
Christopher Faulet9768c262018-10-22 09:34:31 +02002074 goto missing_data_or_waiting;
2075
Christopher Fauletd20fdb02019-06-13 16:43:22 +02002076 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet9768c262018-10-22 09:34:31 +02002077
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002078 ending:
Christopher Faulet84d3ef92023-03-17 15:45:58 +01002079 s->scf->flags &= ~SC_FL_SND_EXP_MORE; /* no more data are expected to be sent */
Christopher Faulet2151cdd2020-07-22 16:34:59 +02002080
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002081 /* other states, ENDING...TUNNEL */
2082 if (msg->msg_state >= HTTP_MSG_DONE)
2083 goto done;
Christopher Faulet9768c262018-10-22 09:34:31 +02002084
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002085 if (HAS_RSP_DATA_FILTERS(s)) {
2086 ret = flt_http_end(s, msg);
2087 if (ret <= 0) {
2088 if (!ret)
2089 goto missing_data_or_waiting;
2090 goto return_bad_res;
2091 }
2092 }
2093
Christopher Fauletc75668e2020-12-07 18:10:32 +01002094 if ((txn->meth == HTTP_METH_CONNECT && txn->status >= 200 && txn->status < 300) || txn->status == 101 ||
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002095 !(msg->flags & HTTP_MSGF_XFER_LEN)) {
2096 msg->msg_state = HTTP_MSG_TUNNEL;
2097 goto ending;
2098 }
2099 else {
2100 msg->msg_state = HTTP_MSG_DONE;
2101 res->to_forward = 0;
2102 }
2103
2104 done:
2105
2106 channel_dont_close(res);
2107
Christopher Faulet87633c32023-04-03 18:32:50 +02002108 if ((chn_cons(res)->flags & SC_FL_SHUTW) && co_data(res)) {
Christopher Fauletf2b02cf2023-01-13 11:02:28 +01002109 /* response errors are most likely due to the client aborting
2110 * the transfer. */
2111 goto return_cli_abort;
2112 }
2113
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002114 http_end_response(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002115 if (!(res->analysers & an_bit)) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002116 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002117 return 1;
2118 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002119 DBG_TRACE_DEVEL("waiting for the end of the HTTP txn",
2120 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002121 return 0;
2122
2123 missing_data_or_waiting:
Christopher Faulet87633c32023-04-03 18:32:50 +02002124 if (chn_cons(res)->flags & SC_FL_SHUTW)
Christopher Faulet93e02d82019-03-08 14:18:50 +01002125 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002126
2127 /* stop waiting for data if the input is closed before the end. If the
2128 * client side was already closed, it means that the client has aborted,
2129 * so we don't want to count this as a server abort. Otherwise it's a
2130 * server abort.
2131 */
Christopher Faulet87633c32023-04-03 18:32:50 +02002132 if (msg->msg_state < HTTP_MSG_ENDING && (chn_prod(res)->flags & SC_FL_SHUTR)) {
2133 if ((chn_prod(&s->req)->flags & SC_FL_SHUTR) &&
2134 (chn_cons(&s->req)->flags & SC_FL_SHUTW))
Christopher Faulet93e02d82019-03-08 14:18:50 +01002135 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002136 /* If we have some pending data, we continue the processing */
Christopher Faulet93e02d82019-03-08 14:18:50 +01002137 if (htx_is_empty(htx))
2138 goto return_srv_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002139 }
2140
Christopher Faulete0768eb2018-10-03 16:38:02 +02002141 /* When TE: chunked is used, we need to get there again to parse
2142 * remaining chunks even if the server has closed, so we don't want to
Christopher Faulet9768c262018-10-22 09:34:31 +02002143 * set CF_DONTCLOSE. Similarly when there is a content-leng or if there
2144 * are filters registered on the stream, we don't want to forward a
2145 * close
Christopher Faulete0768eb2018-10-03 16:38:02 +02002146 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002147 if ((msg->flags & HTTP_MSGF_XFER_LEN) || HAS_RSP_DATA_FILTERS(s))
Christopher Faulete0768eb2018-10-03 16:38:02 +02002148 channel_dont_close(res);
2149
2150 /* We know that more data are expected, but we couldn't send more that
Christopher Faulet84d3ef92023-03-17 15:45:58 +01002151 * what we did. So we always set the SC_FL_SND_EXP_MORE flag so that the
Christopher Faulete0768eb2018-10-03 16:38:02 +02002152 * system knows it must not set a PUSH on this first part. Interactive
2153 * modes are already handled by the stream sock layer. We must not do
2154 * this in content-length mode because it could present the MSG_MORE
2155 * flag with the last block of forwarded data, which would cause an
2156 * additional delay to be observed by the receiver.
2157 */
Christopher Faulet2151cdd2020-07-22 16:34:59 +02002158 if (HAS_RSP_DATA_FILTERS(s))
Christopher Faulet84d3ef92023-03-17 15:45:58 +01002159 s->scf->flags |= SC_FL_SND_EXP_MORE;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002160
2161 /* the stream handler will take care of timeouts and errors */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002162 DBG_TRACE_DEVEL("waiting for more data to forward",
2163 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002164 return 0;
2165
Christopher Faulet93e02d82019-03-08 14:18:50 +01002166 return_srv_abort:
Willy Tarreau4781b152021-04-06 13:53:36 +02002167 _HA_ATOMIC_INC(&sess->fe->fe_counters.srv_aborts);
2168 _HA_ATOMIC_INC(&s->be->be_counters.srv_aborts);
William Lallemand36119de2021-03-08 15:26:48 +01002169 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02002170 _HA_ATOMIC_INC(&sess->listener->counters->srv_aborts);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002171 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02002172 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.srv_aborts);
Willy Tarreau826f3ab2021-02-10 12:07:15 +01002173 stream_inc_http_fail_ctr(s);
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_READ_TIMEOUT) ? SF_ERR_SRVTO : SF_ERR_SRVCL);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002176 goto return_error;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002177
Christopher Faulet93e02d82019-03-08 14:18:50 +01002178 return_cli_abort:
Willy Tarreau4781b152021-04-06 13:53:36 +02002179 _HA_ATOMIC_INC(&sess->fe->fe_counters.cli_aborts);
2180 _HA_ATOMIC_INC(&s->be->be_counters.cli_aborts);
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->cli_aborts);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002183 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02002184 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.cli_aborts);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002185 if (!(s->flags & SF_ERR_MASK))
Christopher Fauletf2b02cf2023-01-13 11:02:28 +01002186 s->flags |= ((res->flags & CF_WRITE_TIMEOUT) ? SF_ERR_CLITO : SF_ERR_CLICL);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002187 goto return_error;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002188
Christopher Fauletb9a92f32019-09-09 10:15:21 +02002189 return_int_err:
Willy Tarreau4781b152021-04-06 13:53:36 +02002190 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
2191 _HA_ATOMIC_INC(&s->be->be_counters.internal_errors);
William Lallemand36119de2021-03-08 15:26:48 +01002192 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02002193 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002194 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02002195 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.internal_errors);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02002196 if (!(s->flags & SF_ERR_MASK))
2197 s->flags |= SF_ERR_INTERNAL;
2198 goto return_error;
2199
Christopher Faulet93e02d82019-03-08 14:18:50 +01002200 return_bad_res:
Willy Tarreau4781b152021-04-06 13:53:36 +02002201 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002202 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02002203 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002204 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_RSP);
2205 }
Willy Tarreau826f3ab2021-02-10 12:07:15 +01002206 stream_inc_http_fail_ctr(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002207 if (!(s->flags & SF_ERR_MASK))
Christopher Faulet93e02d82019-03-08 14:18:50 +01002208 s->flags |= SF_ERR_SRVCL;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002209 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +02002210
Christopher Faulet93e02d82019-03-08 14:18:50 +01002211 return_error:
Christopher Faulete0768eb2018-10-03 16:38:02 +02002212 /* don't send any error message as we're in the body */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002213 http_reply_and_close(s, txn->status, NULL);
Christopher Fauletf2b02cf2023-01-13 11:02:28 +01002214 http_set_term_flags(s);
2215 stream_inc_http_fail_ctr(s);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002216 DBG_TRACE_DEVEL("leaving on error",
2217 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002218 return 0;
2219}
2220
Christopher Fauletf2824e62018-10-01 12:12:37 +02002221/* Perform an HTTP redirect based on the information in <rule>. The function
Willy Tarreaubc1223b2021-09-02 16:54:33 +02002222 * returns zero in case of an irrecoverable error such as too large a request
2223 * to build a valid response, 1 in case of successful redirect (hence the rule
2224 * is final), or 2 if the rule has to be silently skipped.
Christopher Fauletf2824e62018-10-01 12:12:37 +02002225 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002226int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn)
Christopher Fauletf2824e62018-10-01 12:12:37 +02002227{
Christopher Faulet99daf282018-11-28 22:58:13 +01002228 struct channel *req = &s->req;
2229 struct channel *res = &s->res;
2230 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01002231 struct htx_sl *sl;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002232 struct buffer *chunk;
Christopher Faulet99daf282018-11-28 22:58:13 +01002233 struct ist status, reason, location;
2234 unsigned int flags;
Christopher Fauleteab17572022-04-26 20:34:38 +02002235 int ret = 1, close = 0; /* Try to keep the connection alive byt default */
Christopher Fauletf2824e62018-10-01 12:12:37 +02002236
2237 chunk = alloc_trash_chunk();
Christopher Fauletb8a53712019-12-16 11:29:38 +01002238 if (!chunk) {
2239 if (!(s->flags & SF_ERR_MASK))
2240 s->flags |= SF_ERR_RESOURCE;
Christopher Faulet99daf282018-11-28 22:58:13 +01002241 goto fail;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002242 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002243
Christopher Faulet99daf282018-11-28 22:58:13 +01002244 /*
2245 * Create the location
2246 */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002247 htx = htxbuf(&req->buf);
Christopher Fauletf2824e62018-10-01 12:12:37 +02002248 switch(rule->type) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002249 case REDIRECT_TYPE_SCHEME: {
2250 struct http_hdr_ctx ctx;
2251 struct ist path, host;
Amaury Denoyellec453f952021-07-06 11:40:12 +02002252 struct http_uri_parser parser;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002253
Christopher Faulet99daf282018-11-28 22:58:13 +01002254 host = ist("");
2255 ctx.blk = NULL;
2256 if (http_find_header(htx, ist("Host"), &ctx, 0))
2257 host = ctx.value;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002258
Christopher Faulet297fbb42019-05-13 14:41:27 +02002259 sl = http_get_stline(htx);
Amaury Denoyellec453f952021-07-06 11:40:12 +02002260 parser = http_uri_parser_init(htx_sl_req_uri(sl));
2261 path = http_parse_path(&parser);
Christopher Faulet99daf282018-11-28 22:58:13 +01002262 /* build message using path */
Tim Duesterhused526372020-03-05 17:56:33 +01002263 if (isttest(path)) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002264 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2265 int qs = 0;
2266 while (qs < path.len) {
2267 if (*(path.ptr + qs) == '?') {
2268 path.len = qs;
2269 break;
2270 }
2271 qs++;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002272 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002273 }
2274 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002275 else
2276 path = ist("/");
Christopher Fauletf2824e62018-10-01 12:12:37 +02002277
Christopher Faulet99daf282018-11-28 22:58:13 +01002278 if (rule->rdr_str) { /* this is an old "redirect" rule */
2279 /* add scheme */
2280 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2281 goto fail;
2282 }
2283 else {
2284 /* add scheme with executing log format */
2285 chunk->data += build_logline(s, chunk->area + chunk->data,
2286 chunk->size - chunk->data,
2287 &rule->rdr_fmt);
2288 }
2289 /* add "://" + host + path */
2290 if (!chunk_memcat(chunk, "://", 3) ||
2291 !chunk_memcat(chunk, host.ptr, host.len) ||
2292 !chunk_memcat(chunk, path.ptr, path.len))
2293 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002294
Christopher Faulet99daf282018-11-28 22:58:13 +01002295 /* append a slash at the end of the location if needed and missing */
2296 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
2297 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
2298 if (chunk->data + 1 >= chunk->size)
2299 goto fail;
2300 chunk->area[chunk->data++] = '/';
2301 }
2302 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002303 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002304
Christopher Faulet99daf282018-11-28 22:58:13 +01002305 case REDIRECT_TYPE_PREFIX: {
2306 struct ist path;
Amaury Denoyellec453f952021-07-06 11:40:12 +02002307 struct http_uri_parser parser;
Christopher Faulet99daf282018-11-28 22:58:13 +01002308
Christopher Faulet297fbb42019-05-13 14:41:27 +02002309 sl = http_get_stline(htx);
Amaury Denoyellec453f952021-07-06 11:40:12 +02002310 parser = http_uri_parser_init(htx_sl_req_uri(sl));
2311 path = http_parse_path(&parser);
Christopher Faulet99daf282018-11-28 22:58:13 +01002312 /* build message using path */
Tim Duesterhused526372020-03-05 17:56:33 +01002313 if (isttest(path)) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002314 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2315 int qs = 0;
2316 while (qs < path.len) {
2317 if (*(path.ptr + qs) == '?') {
2318 path.len = qs;
2319 break;
2320 }
2321 qs++;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002322 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002323 }
2324 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002325 else
2326 path = ist("/");
Christopher Fauletf2824e62018-10-01 12:12:37 +02002327
Christopher Faulet99daf282018-11-28 22:58:13 +01002328 if (rule->rdr_str) { /* this is an old "redirect" rule */
2329 /* add prefix. Note that if prefix == "/", we don't want to
2330 * add anything, otherwise it makes it hard for the user to
2331 * configure a self-redirection.
2332 */
2333 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
2334 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2335 goto fail;
2336 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002337 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002338 else {
2339 /* add prefix with executing log format */
2340 chunk->data += build_logline(s, chunk->area + chunk->data,
2341 chunk->size - chunk->data,
2342 &rule->rdr_fmt);
2343 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002344
Christopher Faulet99daf282018-11-28 22:58:13 +01002345 /* add path */
2346 if (!chunk_memcat(chunk, path.ptr, path.len))
2347 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002348
Christopher Faulet99daf282018-11-28 22:58:13 +01002349 /* append a slash at the end of the location if needed and missing */
2350 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
2351 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
2352 if (chunk->data + 1 >= chunk->size)
2353 goto fail;
2354 chunk->area[chunk->data++] = '/';
2355 }
2356 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002357 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002358 case REDIRECT_TYPE_LOCATION:
2359 default:
2360 if (rule->rdr_str) { /* this is an old "redirect" rule */
2361 /* add location */
2362 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2363 goto fail;
2364 }
2365 else {
2366 /* add location with executing log format */
Willy Tarreaubc1223b2021-09-02 16:54:33 +02002367 int len = build_logline(s, chunk->area + chunk->data,
2368 chunk->size - chunk->data,
2369 &rule->rdr_fmt);
Christopher Fauleteab17572022-04-26 20:34:38 +02002370 if (!len && rule->flags & REDIRECT_FLAG_IGNORE_EMPTY) {
2371 ret = 2;
2372 goto out;
2373 }
Willy Tarreaubc1223b2021-09-02 16:54:33 +02002374
2375 chunk->data += len;
Christopher Faulet99daf282018-11-28 22:58:13 +01002376 }
2377 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002378 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002379 location = ist2(chunk->area, chunk->data);
2380
2381 /*
2382 * Create the 30x response
2383 */
2384 switch (rule->code) {
2385 case 308:
2386 status = ist("308");
2387 reason = ist("Permanent Redirect");
2388 break;
2389 case 307:
2390 status = ist("307");
2391 reason = ist("Temporary Redirect");
2392 break;
2393 case 303:
2394 status = ist("303");
2395 reason = ist("See Other");
2396 break;
2397 case 301:
2398 status = ist("301");
2399 reason = ist("Moved Permanently");
2400 break;
2401 case 302:
2402 default:
2403 status = ist("302");
2404 reason = ist("Found");
2405 break;
2406 }
2407
Christopher Faulet08e66462019-05-23 16:44:59 +02002408 if (!(txn->req.flags & HTTP_MSGF_BODYLESS) && txn->req.msg_state != HTTP_MSG_DONE)
2409 close = 1;
2410
Christopher Faulet99daf282018-11-28 22:58:13 +01002411 htx = htx_from_buf(&res->buf);
Kevin Zhu96b36392020-01-07 09:42:55 +01002412 /* Trim any possible response */
2413 channel_htx_truncate(&s->res, htx);
Christopher Faulet99daf282018-11-28 22:58:13 +01002414 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
2415 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), status, reason);
2416 if (!sl)
2417 goto fail;
2418 sl->info.res.status = rule->code;
2419 s->txn->status = rule->code;
2420
Christopher Faulet08e66462019-05-23 16:44:59 +02002421 if (close && !htx_add_header(htx, ist("Connection"), ist("close")))
2422 goto fail;
2423
2424 if (!htx_add_header(htx, ist("Content-length"), ist("0")) ||
Christopher Faulet99daf282018-11-28 22:58:13 +01002425 !htx_add_header(htx, ist("Location"), location))
2426 goto fail;
2427
2428 if (rule->code == 302 || rule->code == 303 || rule->code == 307) {
2429 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")))
2430 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002431 }
2432
2433 if (rule->cookie_len) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002434 if (!htx_add_header(htx, ist("Set-Cookie"), ist2(rule->cookie_str, rule->cookie_len)))
2435 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002436 }
2437
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002438 if (!htx_add_endof(htx, HTX_BLK_EOH))
Christopher Faulet99daf282018-11-28 22:58:13 +01002439 goto fail;
2440
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002441 htx->flags |= HTX_FL_EOM;
Kevin Zhu96b36392020-01-07 09:42:55 +01002442 htx_to_buf(htx, &res->buf);
Christopher Fauleta72a7e42020-01-28 09:28:11 +01002443 if (!http_forward_proxy_resp(s, 1))
2444 goto fail;
Christopher Faulet99daf282018-11-28 22:58:13 +01002445
Christopher Faulet60b33a52020-01-28 09:18:10 +01002446 if (rule->flags & REDIRECT_FLAG_FROM_REQ) {
2447 /* let's log the request time */
2448 s->logs.tv_request = now;
Christopher Fauletd3475882021-10-04 14:16:46 +02002449 req->analysers &= AN_REQ_FLT_END;
Christopher Faulet99daf282018-11-28 22:58:13 +01002450
Christopher Faulet60b33a52020-01-28 09:18:10 +01002451 if (s->sess->fe == s->be) /* report it if the request was intercepted by the frontend */
Willy Tarreau4781b152021-04-06 13:53:36 +02002452 _HA_ATOMIC_INC(&s->sess->fe->fe_counters.intercepted_req);
Christopher Faulet60b33a52020-01-28 09:18:10 +01002453 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002454
2455 if (!(s->flags & SF_ERR_MASK))
2456 s->flags |= SF_ERR_LOCAL;
Christopher Faulet4a66c942023-01-13 09:43:21 +01002457 http_set_term_flags(s);
Christopher Fauletf2824e62018-10-01 12:12:37 +02002458
Christopher Fauleteab17572022-04-26 20:34:38 +02002459 out:
Christopher Faulet99daf282018-11-28 22:58:13 +01002460 free_trash_chunk(chunk);
Christopher Fauleteab17572022-04-26 20:34:38 +02002461 return ret;
Christopher Faulet99daf282018-11-28 22:58:13 +01002462
2463 fail:
2464 /* If an error occurred, remove the incomplete HTTP response from the
2465 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01002466 channel_htx_truncate(res, htxbuf(&res->buf));
Christopher Fauleteab17572022-04-26 20:34:38 +02002467 ret = 0;
2468 goto out;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002469}
2470
Christopher Faulet18c13d32022-05-16 11:43:10 +02002471/* This function filters the request header names to only allow [0-9a-zA-Z-]
2472 * characters. Depending on the proxy configuration, headers with a name not
2473 * matching this charset are removed or the request is rejected with a
2474 * 403-Forbidden response if such name are found. It returns HTTP_RULE_RES_CONT
2475 * to continue the request processing or HTTP_RULE_RES_DENY if the request is
2476 * rejected.
2477 */
2478static enum rule_result http_req_restrict_header_names(struct stream *s, struct htx *htx, struct proxy *px)
2479{
2480 struct htx_blk *blk;
2481 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
2482
2483 blk = htx_get_first_blk(htx);
2484 while (blk) {
2485 enum htx_blk_type type = htx_get_blk_type(blk);
2486
2487 if (type == HTX_BLK_HDR) {
2488 struct ist n = htx_get_blk_name(htx, blk);
Mateusz Malek4b85a962022-08-17 14:22:09 +02002489 int i, end = istlen(n);
Christopher Faulet18c13d32022-05-16 11:43:10 +02002490
Mateusz Malek4b85a962022-08-17 14:22:09 +02002491 for (i = 0; i < end; i++) {
Christopher Faulet18c13d32022-05-16 11:43:10 +02002492 if (!isalnum((unsigned char)n.ptr[i]) && n.ptr[i] != '-') {
Mateusz Malek4b85a962022-08-17 14:22:09 +02002493 break;
Christopher Faulet18c13d32022-05-16 11:43:10 +02002494 }
2495 }
Mateusz Malek4b85a962022-08-17 14:22:09 +02002496
2497 if (i < end) {
2498 /* Disallowed character found - block the request or remove the header */
2499 if (px->options2 & PR_O2_RSTRICT_REQ_HDR_NAMES_BLK)
2500 goto block;
2501 blk = htx_remove_blk(htx, blk);
2502 continue;
2503 }
Christopher Faulet18c13d32022-05-16 11:43:10 +02002504 }
2505 if (type == HTX_BLK_EOH)
2506 break;
2507
2508 blk = htx_get_next_blk(htx, blk);
2509 }
2510 out:
2511 return rule_ret;
2512 block:
2513 /* Block the request returning a 403-Forbidden response */
2514 s->txn->status = 403;
2515 rule_ret = HTTP_RULE_RES_DENY;
2516 goto out;
2517}
2518
Christopher Faulet92d34fe2019-12-17 09:20:34 +01002519/* Replace all headers matching the name <name>. The header value is replaced if
2520 * it matches the regex <re>. <str> is used for the replacement. If <full> is
2521 * set to 1, the full-line is matched and replaced. Otherwise, comma-separated
2522 * values are evaluated one by one. It returns 0 on success and -1 on error.
2523 */
2524int http_replace_hdrs(struct stream* s, struct htx *htx, struct ist name,
2525 const char *str, struct my_regex *re, int full)
Christopher Faulet72333522018-10-24 11:25:02 +02002526{
2527 struct http_hdr_ctx ctx;
2528 struct buffer *output = get_trash_chunk();
2529
Christopher Faulet72333522018-10-24 11:25:02 +02002530 ctx.blk = NULL;
Christopher Faulet92d34fe2019-12-17 09:20:34 +01002531 while (http_find_header(htx, name, &ctx, full)) {
Christopher Faulet72333522018-10-24 11:25:02 +02002532 if (!regex_exec_match2(re, ctx.value.ptr, ctx.value.len, MAX_MATCH, pmatch, 0))
2533 continue;
2534
2535 output->data = exp_replace(output->area, output->size, ctx.value.ptr, str, pmatch);
2536 if (output->data == -1)
2537 return -1;
2538 if (!http_replace_header_value(htx, &ctx, ist2(output->area, output->data)))
2539 return -1;
2540 }
2541 return 0;
2542}
2543
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002544/* This function executes one of the set-{method,path,query,uri} actions. It
2545 * takes the string from the variable 'replace' with length 'len', then modifies
2546 * the relevant part of the request line accordingly. Then it updates various
2547 * pointers to the next elements which were moved, and the total buffer length.
2548 * It finds the action to be performed in p[2], previously filled by function
2549 * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal
2550 * error, though this can be revisited when this code is finally exploited.
2551 *
2552 * 'action' can be '0' to replace method, '1' to replace path, '2' to replace
Christopher Faulet312294f2020-09-02 17:17:44 +02002553 * query string, 3 to replace uri or 4 to replace the path+query.
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002554 *
2555 * In query string case, the mark question '?' must be set at the start of the
2556 * string by the caller, event if the replacement query string is empty.
2557 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002558int http_req_replace_stline(int action, const char *replace, int len,
2559 struct proxy *px, struct stream *s)
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002560{
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002561 struct htx *htx = htxbuf(&s->req.buf);
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002562
2563 switch (action) {
2564 case 0: // method
2565 if (!http_replace_req_meth(htx, ist2(replace, len)))
2566 return -1;
2567 break;
2568
2569 case 1: // path
Christopher Fauletb8ce5052020-08-31 16:11:57 +02002570 if (!http_replace_req_path(htx, ist2(replace, len), 0))
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002571 return -1;
2572 break;
2573
2574 case 2: // query
2575 if (!http_replace_req_query(htx, ist2(replace, len)))
2576 return -1;
2577 break;
2578
2579 case 3: // uri
2580 if (!http_replace_req_uri(htx, ist2(replace, len)))
2581 return -1;
2582 break;
2583
Christopher Faulet312294f2020-09-02 17:17:44 +02002584 case 4: // path + query
2585 if (!http_replace_req_path(htx, ist2(replace, len), 1))
2586 return -1;
2587 break;
2588
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002589 default:
2590 return -1;
2591 }
2592 return 0;
2593}
2594
2595/* This function replace the HTTP status code and the associated message. The
Christopher Faulete00d06c2019-12-16 17:18:42 +01002596 * variable <status> contains the new status code. This function never fails. It
2597 * returns 0 in case of success, -1 in case of internal error.
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002598 */
Christopher Faulet96bff762019-12-17 13:46:18 +01002599int http_res_set_status(unsigned int status, struct ist reason, struct stream *s)
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002600{
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002601 struct htx *htx = htxbuf(&s->res.buf);
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002602 char *res;
2603
2604 chunk_reset(&trash);
2605 res = ultoa_o(status, trash.area, trash.size);
2606 trash.data = res - trash.area;
2607
2608 /* Do we have a custom reason format string? */
Tim Duesterhuse296d3e2020-03-05 17:56:31 +01002609 if (!isttest(reason)) {
Christopher Faulet96bff762019-12-17 13:46:18 +01002610 const char *str = http_get_reason(status);
Tim Duesterhusdcf753a2021-03-04 17:31:47 +01002611 reason = ist(str);
Christopher Faulet96bff762019-12-17 13:46:18 +01002612 }
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002613
Christopher Fauletbde2c4c2020-08-31 16:43:34 +02002614 if (!http_replace_res_status(htx, ist2(trash.area, trash.data), reason))
Christopher Faulete00d06c2019-12-16 17:18:42 +01002615 return -1;
Willy Tarreau640e2532023-01-10 14:50:44 +01002616 s->txn->status = status;
Christopher Faulete00d06c2019-12-16 17:18:42 +01002617 return 0;
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002618}
2619
Christopher Faulet3e964192018-10-24 11:39:23 +02002620/* Executes the http-request rules <rules> for stream <s>, proxy <px> and
2621 * transaction <txn>. Returns the verdict of the first rule that prevents
2622 * further processing of the request (auth, deny, ...), and defaults to
2623 * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or
2624 * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT
2625 * on txn->flags if it encounters a tarpit rule. If <deny_status> is not NULL
2626 * and a deny/tarpit rule is matched, it will be filled with this rule's deny
2627 * status.
2628 */
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002629static enum rule_result http_req_get_intercept_rule(struct proxy *px, struct list *def_rules,
2630 struct list *rules, struct stream *s)
Christopher Faulet3e964192018-10-24 11:39:23 +02002631{
2632 struct session *sess = strm_sess(s);
2633 struct http_txn *txn = s->txn;
Christopher Faulet3e964192018-10-24 11:39:23 +02002634 struct act_rule *rule;
Christopher Faulet3e964192018-10-24 11:39:23 +02002635 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002636 int act_opts = 0;
Christopher Faulet3e964192018-10-24 11:39:23 +02002637
Christopher Faulet3e964192018-10-24 11:39:23 +02002638 /* If "the current_rule_list" match the executed rule list, we are in
2639 * resume condition. If a resume is needed it is always in the action
2640 * and never in the ACL or converters. In this case, we initialise the
2641 * current rule, and go to the action execution point.
2642 */
2643 if (s->current_rule) {
2644 rule = s->current_rule;
2645 s->current_rule = NULL;
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002646 if (s->current_rule_list == rules || (def_rules && s->current_rule_list == def_rules))
Christopher Faulet3e964192018-10-24 11:39:23 +02002647 goto resume_execution;
2648 }
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002649 s->current_rule_list = ((!def_rules || s->current_rule_list == def_rules) ? rules : def_rules);
Christopher Faulet3e964192018-10-24 11:39:23 +02002650
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002651 restart:
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002652 /* start the ruleset evaluation in strict mode */
2653 txn->req.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01002654
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002655 list_for_each_entry(rule, s->current_rule_list, list) {
Christopher Faulet3e964192018-10-24 11:39:23 +02002656 /* check optional condition */
2657 if (rule->cond) {
2658 int ret;
2659
2660 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
2661 ret = acl_pass(ret);
2662
2663 if (rule->cond->pol == ACL_COND_UNLESS)
2664 ret = !ret;
2665
2666 if (!ret) /* condition not matched */
2667 continue;
2668 }
2669
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002670 act_opts |= ACT_OPT_FIRST;
Christopher Faulet3e964192018-10-24 11:39:23 +02002671 resume_execution:
Amaury Denoyelle03517732021-05-07 14:25:01 +02002672 if (rule->kw->flags & KWF_EXPERIMENTAL)
2673 mark_tainted(TAINTED_ACTION_EXP_EXECUTED);
2674
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002675 /* Always call the action function if defined */
2676 if (rule->action_ptr) {
Christopher Faulet2e56a732023-01-26 16:18:09 +01002677 if (sc_ep_test(s->scf, SE_FL_ERROR) ||
Christopher Faulet87633c32023-04-03 18:32:50 +02002678 ((chn_prod(&s->req)->flags & SC_FL_SHUTR) &&
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002679 (px->options & PR_O_ABRT_CLOSE)))
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002680 act_opts |= ACT_OPT_FINAL;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002681
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002682 switch (rule->action_ptr(rule, px, sess, s, act_opts)) {
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002683 case ACT_RET_CONT:
2684 break;
2685 case ACT_RET_STOP:
2686 rule_ret = HTTP_RULE_RES_STOP;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002687 s->last_rule_file = rule->conf.file;
2688 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002689 goto end;
2690 case ACT_RET_YIELD:
2691 s->current_rule = rule;
2692 rule_ret = HTTP_RULE_RES_YIELD;
2693 goto end;
2694 case ACT_RET_ERR:
2695 rule_ret = HTTP_RULE_RES_ERROR;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002696 s->last_rule_file = rule->conf.file;
2697 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002698 goto end;
2699 case ACT_RET_DONE:
2700 rule_ret = HTTP_RULE_RES_DONE;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002701 s->last_rule_file = rule->conf.file;
2702 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002703 goto end;
2704 case ACT_RET_DENY:
Christopher Fauletb58f62b2020-01-13 16:40:13 +01002705 if (txn->status == -1)
2706 txn->status = 403;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002707 rule_ret = HTTP_RULE_RES_DENY;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002708 s->last_rule_file = rule->conf.file;
2709 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002710 goto end;
2711 case ACT_RET_ABRT:
2712 rule_ret = HTTP_RULE_RES_ABRT;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002713 s->last_rule_file = rule->conf.file;
2714 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002715 goto end;
2716 case ACT_RET_INV:
2717 rule_ret = HTTP_RULE_RES_BADREQ;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002718 s->last_rule_file = rule->conf.file;
2719 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002720 goto end;
2721 }
2722 continue; /* eval the next rule */
2723 }
2724
2725 /* If not action function defined, check for known actions */
Christopher Faulet3e964192018-10-24 11:39:23 +02002726 switch (rule->action) {
2727 case ACT_ACTION_ALLOW:
2728 rule_ret = HTTP_RULE_RES_STOP;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002729 s->last_rule_file = rule->conf.file;
2730 s->last_rule_line = rule->conf.line;
Christopher Faulet3e964192018-10-24 11:39:23 +02002731 goto end;
2732
2733 case ACT_ACTION_DENY:
Christopher Faulet5cb513a2020-05-13 17:56:56 +02002734 txn->status = rule->arg.http_reply->status;
2735 txn->http_reply = rule->arg.http_reply;
Christopher Faulet3e964192018-10-24 11:39:23 +02002736 rule_ret = HTTP_RULE_RES_DENY;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002737 s->last_rule_file = rule->conf.file;
2738 s->last_rule_line = rule->conf.line;
Christopher Faulet3e964192018-10-24 11:39:23 +02002739 goto end;
2740
2741 case ACT_HTTP_REQ_TARPIT:
2742 txn->flags |= TX_CLTARPIT;
Christopher Faulet5cb513a2020-05-13 17:56:56 +02002743 txn->status = rule->arg.http_reply->status;
2744 txn->http_reply = rule->arg.http_reply;
Christopher Faulet3e964192018-10-24 11:39:23 +02002745 rule_ret = HTTP_RULE_RES_DENY;
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;
2749
Willy Tarreaubc1223b2021-09-02 16:54:33 +02002750 case ACT_HTTP_REDIR: {
2751 int ret = http_apply_redirect_rule(rule->arg.redir, s, txn);
2752
2753 if (ret == 2) // 2 == skip
2754 break;
2755
2756 rule_ret = ret ? HTTP_RULE_RES_ABRT : HTTP_RULE_RES_ERROR;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002757 s->last_rule_file = rule->conf.file;
2758 s->last_rule_line = rule->conf.line;
Christopher Faulet3e964192018-10-24 11:39:23 +02002759 goto end;
Willy Tarreaubc1223b2021-09-02 16:54:33 +02002760 }
Christopher Faulet3e964192018-10-24 11:39:23 +02002761
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002762 /* other flags exists, but normally, they never be matched. */
Christopher Faulet3e964192018-10-24 11:39:23 +02002763 default:
2764 break;
2765 }
2766 }
2767
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002768 if (def_rules && s->current_rule_list == def_rules) {
2769 s->current_rule_list = rules;
2770 goto restart;
2771 }
2772
Christopher Faulet3e964192018-10-24 11:39:23 +02002773 end:
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002774 /* if the ruleset evaluation is finished reset the strict mode */
Christopher Faulet46f95542019-12-20 10:07:22 +01002775 if (rule_ret != HTTP_RULE_RES_YIELD)
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002776 txn->req.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01002777
Christopher Faulet3e964192018-10-24 11:39:23 +02002778 /* we reached the end of the rules, nothing to report */
2779 return rule_ret;
2780}
2781
2782/* Executes the http-response rules <rules> for stream <s> and proxy <px>. It
2783 * returns one of 5 possible statuses: HTTP_RULE_RES_CONT, HTTP_RULE_RES_STOP,
2784 * HTTP_RULE_RES_DONE, HTTP_RULE_RES_YIELD, or HTTP_RULE_RES_BADREQ. If *CONT
2785 * is returned, the process can continue the evaluation of next rule list. If
2786 * *STOP or *DONE is returned, the process must stop the evaluation. If *BADREQ
2787 * is returned, it means the operation could not be processed and a server error
Christopher Fauleta53abad2020-05-13 08:12:22 +02002788 * must be returned. If *YIELD is returned, the caller must call again the
2789 * function with the same context.
Christopher Faulet3e964192018-10-24 11:39:23 +02002790 */
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002791static enum rule_result http_res_get_intercept_rule(struct proxy *px, struct list *def_rules,
2792 struct list *rules, struct stream *s)
Christopher Faulet3e964192018-10-24 11:39:23 +02002793{
2794 struct session *sess = strm_sess(s);
2795 struct http_txn *txn = s->txn;
Christopher Faulet3e964192018-10-24 11:39:23 +02002796 struct act_rule *rule;
Christopher Faulet3e964192018-10-24 11:39:23 +02002797 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002798 int act_opts = 0;
Christopher Faulet3e964192018-10-24 11:39:23 +02002799
Christopher Faulet3e964192018-10-24 11:39:23 +02002800 /* If "the current_rule_list" match the executed rule list, we are in
2801 * resume condition. If a resume is needed it is always in the action
2802 * and never in the ACL or converters. In this case, we initialise the
2803 * current rule, and go to the action execution point.
2804 */
2805 if (s->current_rule) {
2806 rule = s->current_rule;
2807 s->current_rule = NULL;
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002808 if (s->current_rule_list == rules || (def_rules && s->current_rule_list == def_rules))
Christopher Faulet3e964192018-10-24 11:39:23 +02002809 goto resume_execution;
2810 }
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002811 s->current_rule_list = ((!def_rules || s->current_rule_list == def_rules) ? rules : def_rules);
2812
2813 restart:
Christopher Faulet3e964192018-10-24 11:39:23 +02002814
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002815 /* start the ruleset evaluation in strict mode */
2816 txn->rsp.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01002817
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002818 list_for_each_entry(rule, s->current_rule_list, list) {
Christopher Faulet3e964192018-10-24 11:39:23 +02002819 /* check optional condition */
2820 if (rule->cond) {
2821 int ret;
2822
2823 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
2824 ret = acl_pass(ret);
2825
2826 if (rule->cond->pol == ACL_COND_UNLESS)
2827 ret = !ret;
2828
2829 if (!ret) /* condition not matched */
2830 continue;
2831 }
2832
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002833 act_opts |= ACT_OPT_FIRST;
Christopher Faulet3e964192018-10-24 11:39:23 +02002834resume_execution:
Amaury Denoyelle03517732021-05-07 14:25:01 +02002835 if (rule->kw->flags & KWF_EXPERIMENTAL)
2836 mark_tainted(TAINTED_ACTION_EXP_EXECUTED);
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002837
2838 /* Always call the action function if defined */
2839 if (rule->action_ptr) {
Christopher Faulet2e56a732023-01-26 16:18:09 +01002840 if (sc_ep_test(s->scf, SE_FL_ERROR) ||
Christopher Faulet87633c32023-04-03 18:32:50 +02002841 ((chn_prod(&s->req)->flags & SC_FL_SHUTR) &&
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002842 (px->options & PR_O_ABRT_CLOSE)))
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002843 act_opts |= ACT_OPT_FINAL;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002844
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002845 switch (rule->action_ptr(rule, px, sess, s, act_opts)) {
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002846 case ACT_RET_CONT:
2847 break;
2848 case ACT_RET_STOP:
2849 rule_ret = HTTP_RULE_RES_STOP;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002850 s->last_rule_file = rule->conf.file;
2851 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002852 goto end;
2853 case ACT_RET_YIELD:
2854 s->current_rule = rule;
2855 rule_ret = HTTP_RULE_RES_YIELD;
2856 goto end;
2857 case ACT_RET_ERR:
2858 rule_ret = HTTP_RULE_RES_ERROR;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002859 s->last_rule_file = rule->conf.file;
2860 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002861 goto end;
2862 case ACT_RET_DONE:
2863 rule_ret = HTTP_RULE_RES_DONE;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002864 s->last_rule_file = rule->conf.file;
2865 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002866 goto end;
2867 case ACT_RET_DENY:
Christopher Fauletb58f62b2020-01-13 16:40:13 +01002868 if (txn->status == -1)
2869 txn->status = 502;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002870 rule_ret = HTTP_RULE_RES_DENY;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002871 s->last_rule_file = rule->conf.file;
2872 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002873 goto end;
2874 case ACT_RET_ABRT:
2875 rule_ret = HTTP_RULE_RES_ABRT;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002876 s->last_rule_file = rule->conf.file;
2877 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002878 goto end;
2879 case ACT_RET_INV:
2880 rule_ret = HTTP_RULE_RES_BADREQ;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002881 s->last_rule_file = rule->conf.file;
2882 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002883 goto end;
2884 }
2885 continue; /* eval the next rule */
2886 }
2887
2888 /* If not action function defined, check for known actions */
Christopher Faulet3e964192018-10-24 11:39:23 +02002889 switch (rule->action) {
2890 case ACT_ACTION_ALLOW:
2891 rule_ret = HTTP_RULE_RES_STOP; /* "allow" rules are OK */
Willy Tarreauc6dae862022-03-09 17:23:10 +01002892 s->last_rule_file = rule->conf.file;
2893 s->last_rule_line = rule->conf.line;
Christopher Faulet3e964192018-10-24 11:39:23 +02002894 goto end;
2895
2896 case ACT_ACTION_DENY:
Christopher Faulet5cb513a2020-05-13 17:56:56 +02002897 txn->status = rule->arg.http_reply->status;
2898 txn->http_reply = rule->arg.http_reply;
Christopher Faulet3a26bee2019-12-16 12:47:40 +01002899 rule_ret = HTTP_RULE_RES_DENY;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002900 s->last_rule_file = rule->conf.file;
2901 s->last_rule_line = rule->conf.line;
Christopher Faulet3e964192018-10-24 11:39:23 +02002902 goto end;
2903
Willy Tarreaubc1223b2021-09-02 16:54:33 +02002904 case ACT_HTTP_REDIR: {
2905 int ret = http_apply_redirect_rule(rule->arg.redir, s, txn);
Christopher Faulet3e964192018-10-24 11:39:23 +02002906
Willy Tarreaubc1223b2021-09-02 16:54:33 +02002907 if (ret == 2) // 2 == skip
2908 break;
2909
2910 rule_ret = ret ? HTTP_RULE_RES_ABRT : HTTP_RULE_RES_ERROR;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002911 s->last_rule_file = rule->conf.file;
2912 s->last_rule_line = rule->conf.line;
Willy Tarreaubc1223b2021-09-02 16:54:33 +02002913 goto end;
2914 }
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002915 /* other flags exists, but normally, they never be matched. */
Christopher Faulet3e964192018-10-24 11:39:23 +02002916 default:
2917 break;
2918 }
2919 }
2920
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002921 if (def_rules && s->current_rule_list == def_rules) {
2922 s->current_rule_list = rules;
2923 goto restart;
2924 }
2925
Christopher Faulet3e964192018-10-24 11:39:23 +02002926 end:
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002927 /* if the ruleset evaluation is finished reset the strict mode */
Christopher Faulet46f95542019-12-20 10:07:22 +01002928 if (rule_ret != HTTP_RULE_RES_YIELD)
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002929 txn->rsp.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01002930
Christopher Faulet3e964192018-10-24 11:39:23 +02002931 /* we reached the end of the rules, nothing to report */
2932 return rule_ret;
2933}
2934
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002935/* Executes backend and frontend http-after-response rules for the stream <s>,
2936 * in that order. it return 1 on success and 0 on error. It is the caller
2937 * responsibility to catch error or ignore it. If it catches it, this function
2938 * may be called a second time, for the internal error.
2939 */
2940int http_eval_after_res_rules(struct stream *s)
2941{
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002942 struct list *def_rules, *rules;
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002943 struct session *sess = s->sess;
2944 enum rule_result ret = HTTP_RULE_RES_CONT;
2945
Christopher Faulet507479b2020-05-15 12:29:46 +02002946 /* Eval after-response ruleset only if the reply is not const */
2947 if (s->txn->flags & TX_CONST_REPLY)
2948 goto end;
2949
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002950 /* prune the request variables if not already done and swap to the response variables. */
2951 if (s->vars_reqres.scope != SCOPE_RES) {
2952 if (!LIST_ISEMPTY(&s->vars_reqres.head))
2953 vars_prune(&s->vars_reqres, s->sess, s);
Willy Tarreaub7bfcb32021-08-31 08:13:25 +02002954 vars_init_head(&s->vars_reqres, SCOPE_RES);
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002955 }
2956
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002957 def_rules = (s->be->defpx ? &s->be->defpx->http_after_res_rules : NULL);
2958 rules = &s->be->http_after_res_rules;
2959
2960 ret = http_res_get_intercept_rule(s->be, def_rules, rules, s);
Christopher Faulet4c5a5912021-11-09 17:48:39 +01002961 if ((ret == HTTP_RULE_RES_CONT || ret == HTTP_RULE_RES_STOP) && sess->fe != s->be) {
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002962 def_rules = ((sess->fe->defpx && sess->fe->defpx != s->be->defpx) ? &sess->fe->defpx->http_after_res_rules : NULL);
2963 rules = &sess->fe->http_after_res_rules;
2964 ret = http_res_get_intercept_rule(sess->fe, def_rules, rules, s);
2965 }
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002966
Christopher Faulet507479b2020-05-15 12:29:46 +02002967 end:
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002968 /* All other codes than CONTINUE, STOP or DONE are forbidden */
2969 return (ret == HTTP_RULE_RES_CONT || ret == HTTP_RULE_RES_STOP || ret == HTTP_RULE_RES_DONE);
2970}
2971
Christopher Fauletfcda7c62018-10-24 11:56:22 +02002972/*
2973 * Manage client-side cookie. It can impact performance by about 2% so it is
2974 * desirable to call it only when needed. This code is quite complex because
2975 * of the multiple very crappy and ambiguous syntaxes we have to support. it
2976 * highly recommended not to touch this part without a good reason !
2977 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002978static void http_manage_client_side_cookies(struct stream *s, struct channel *req)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02002979{
2980 struct session *sess = s->sess;
2981 struct http_txn *txn = s->txn;
2982 struct htx *htx;
2983 struct http_hdr_ctx ctx;
2984 char *hdr_beg, *hdr_end, *del_from;
2985 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
2986 int preserve_hdr;
2987
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002988 htx = htxbuf(&req->buf);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02002989 ctx.blk = NULL;
2990 while (http_find_header(htx, ist("Cookie"), &ctx, 1)) {
Olivier Houchardf0f42382019-07-22 17:43:46 +02002991 int is_first = 1;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02002992 del_from = NULL; /* nothing to be deleted */
2993 preserve_hdr = 0; /* assume we may kill the whole header */
2994
2995 /* Now look for cookies. Conforming to RFC2109, we have to support
2996 * attributes whose name begin with a '$', and associate them with
2997 * the right cookie, if we want to delete this cookie.
2998 * So there are 3 cases for each cookie read :
2999 * 1) it's a special attribute, beginning with a '$' : ignore it.
3000 * 2) it's a server id cookie that we *MAY* want to delete : save
3001 * some pointers on it (last semi-colon, beginning of cookie...)
3002 * 3) it's an application cookie : we *MAY* have to delete a previous
3003 * "special" cookie.
3004 * At the end of loop, if a "special" cookie remains, we may have to
3005 * remove it. If no application cookie persists in the header, we
3006 * *MUST* delete it.
3007 *
3008 * Note: RFC2965 is unclear about the processing of spaces around
3009 * the equal sign in the ATTR=VALUE form. A careful inspection of
3010 * the RFC explicitly allows spaces before it, and not within the
3011 * tokens (attrs or values). An inspection of RFC2109 allows that
3012 * too but section 10.1.3 lets one think that spaces may be allowed
3013 * after the equal sign too, resulting in some (rare) buggy
3014 * implementations trying to do that. So let's do what servers do.
3015 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
3016 * allowed quoted strings in values, with any possible character
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003017 * after a backslash, including control chars and delimiters, which
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003018 * causes parsing to become ambiguous. Browsers also allow spaces
3019 * within values even without quotes.
3020 *
3021 * We have to keep multiple pointers in order to support cookie
3022 * removal at the beginning, middle or end of header without
3023 * corrupting the header. All of these headers are valid :
3024 *
3025 * hdr_beg hdr_end
3026 * | |
3027 * v |
3028 * NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3 |
3029 * NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3 v
3030 * NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3
3031 * | | | | | | |
3032 * | | | | | | |
3033 * | | | | | | +--> next
3034 * | | | | | +----> val_end
3035 * | | | | +-----------> val_beg
3036 * | | | +--------------> equal
3037 * | | +----------------> att_end
3038 * | +---------------------> att_beg
3039 * +--------------------------> prev
3040 *
3041 */
3042 hdr_beg = ctx.value.ptr;
3043 hdr_end = hdr_beg + ctx.value.len;
3044 for (prev = hdr_beg; prev < hdr_end; prev = next) {
3045 /* Iterate through all cookies on this line */
3046
3047 /* find att_beg */
3048 att_beg = prev;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003049 if (!is_first)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003050 att_beg++;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003051 is_first = 0;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003052
3053 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
3054 att_beg++;
3055
3056 /* find att_end : this is the first character after the last non
3057 * space before the equal. It may be equal to hdr_end.
3058 */
3059 equal = att_end = att_beg;
3060 while (equal < hdr_end) {
3061 if (*equal == '=' || *equal == ',' || *equal == ';')
3062 break;
3063 if (HTTP_IS_SPHT(*equal++))
3064 continue;
3065 att_end = equal;
3066 }
3067
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003068 /* here, <equal> points to '=', a delimiter or the end. <att_end>
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003069 * is between <att_beg> and <equal>, both may be identical.
3070 */
3071 /* look for end of cookie if there is an equal sign */
3072 if (equal < hdr_end && *equal == '=') {
3073 /* look for the beginning of the value */
3074 val_beg = equal + 1;
3075 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
3076 val_beg++;
3077
3078 /* find the end of the value, respecting quotes */
3079 next = http_find_cookie_value_end(val_beg, hdr_end);
3080
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003081 /* make val_end point to the first white space or delimiter after the value */
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003082 val_end = next;
3083 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
3084 val_end--;
3085 }
3086 else
3087 val_beg = val_end = next = equal;
3088
3089 /* We have nothing to do with attributes beginning with
3090 * '$'. However, they will automatically be removed if a
3091 * header before them is removed, since they're supposed
3092 * to be linked together.
3093 */
3094 if (*att_beg == '$')
3095 continue;
3096
3097 /* Ignore cookies with no equal sign */
3098 if (equal == next) {
3099 /* This is not our cookie, so we must preserve it. But if we already
3100 * scheduled another cookie for removal, we cannot remove the
3101 * complete header, but we can remove the previous block itself.
3102 */
3103 preserve_hdr = 1;
3104 if (del_from != NULL) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003105 int delta = http_del_hdr_value(hdr_beg, hdr_end, &del_from, prev);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003106 val_end += delta;
3107 next += delta;
3108 hdr_end += delta;
3109 prev = del_from;
3110 del_from = NULL;
3111 }
3112 continue;
3113 }
3114
3115 /* if there are spaces around the equal sign, we need to
3116 * strip them otherwise we'll get trouble for cookie captures,
3117 * or even for rewrites. Since this happens extremely rarely,
3118 * it does not hurt performance.
3119 */
3120 if (unlikely(att_end != equal || val_beg > equal + 1)) {
3121 int stripped_before = 0;
3122 int stripped_after = 0;
3123
3124 if (att_end != equal) {
3125 memmove(att_end, equal, hdr_end - equal);
3126 stripped_before = (att_end - equal);
3127 equal += stripped_before;
3128 val_beg += stripped_before;
3129 }
3130
3131 if (val_beg > equal + 1) {
3132 memmove(equal + 1, val_beg, hdr_end + stripped_before - val_beg);
3133 stripped_after = (equal + 1) - val_beg;
3134 val_beg += stripped_after;
3135 stripped_before += stripped_after;
3136 }
3137
3138 val_end += stripped_before;
3139 next += stripped_before;
3140 hdr_end += stripped_before;
3141 }
3142 /* now everything is as on the diagram above */
3143
3144 /* First, let's see if we want to capture this cookie. We check
3145 * that we don't already have a client side cookie, because we
3146 * can only capture one. Also as an optimisation, we ignore
3147 * cookies shorter than the declared name.
3148 */
3149 if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL &&
3150 (val_end - att_beg >= sess->fe->capture_namelen) &&
3151 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
3152 int log_len = val_end - att_beg;
3153
3154 if ((txn->cli_cookie = pool_alloc(pool_head_capture)) == NULL) {
3155 ha_alert("HTTP logging : out of memory.\n");
3156 } else {
3157 if (log_len > sess->fe->capture_len)
3158 log_len = sess->fe->capture_len;
3159 memcpy(txn->cli_cookie, att_beg, log_len);
3160 txn->cli_cookie[log_len] = 0;
3161 }
3162 }
3163
3164 /* Persistence cookies in passive, rewrite or insert mode have the
3165 * following form :
3166 *
3167 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
3168 *
3169 * For cookies in prefix mode, the form is :
3170 *
3171 * Cookie: NAME=SRV~VALUE
3172 */
3173 if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
3174 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
3175 struct server *srv = s->be->srv;
3176 char *delim;
3177
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003178 /* if we're in cookie prefix mode, we'll search the delimiter so that we
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003179 * have the server ID between val_beg and delim, and the original cookie between
3180 * delim+1 and val_end. Otherwise, delim==val_end :
3181 *
3182 * hdr_beg
3183 * |
3184 * v
3185 * NAME=SRV; # in all but prefix modes
3186 * NAME=SRV~OPAQUE ; # in prefix mode
3187 * || || | |+-> next
3188 * || || | +--> val_end
3189 * || || +---------> delim
3190 * || |+------------> val_beg
3191 * || +-------------> att_end = equal
3192 * |+-----------------> att_beg
3193 * +------------------> prev
3194 *
3195 */
3196 if (s->be->ck_opts & PR_CK_PFX) {
3197 for (delim = val_beg; delim < val_end; delim++)
3198 if (*delim == COOKIE_DELIM)
3199 break;
3200 }
3201 else {
3202 char *vbar1;
3203 delim = val_end;
3204 /* Now check if the cookie contains a date field, which would
3205 * appear after a vertical bar ('|') just after the server name
3206 * and before the delimiter.
3207 */
3208 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
3209 if (vbar1) {
3210 /* OK, so left of the bar is the server's cookie and
3211 * right is the last seen date. It is a base64 encoded
3212 * 30-bit value representing the UNIX date since the
3213 * epoch in 4-second quantities.
3214 */
3215 int val;
3216 delim = vbar1++;
3217 if (val_end - vbar1 >= 5) {
3218 val = b64tos30(vbar1);
3219 if (val > 0)
3220 txn->cookie_last_date = val << 2;
3221 }
3222 /* look for a second vertical bar */
3223 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
3224 if (vbar1 && (val_end - vbar1 > 5)) {
3225 val = b64tos30(vbar1 + 1);
3226 if (val > 0)
3227 txn->cookie_first_date = val << 2;
3228 }
3229 }
3230 }
3231
3232 /* if the cookie has an expiration date and the proxy wants to check
3233 * it, then we do that now. We first check if the cookie is too old,
3234 * then only if it has expired. We detect strict overflow because the
3235 * time resolution here is not great (4 seconds). Cookies with dates
3236 * in the future are ignored if their offset is beyond one day. This
3237 * allows an admin to fix timezone issues without expiring everyone
3238 * and at the same time avoids keeping unwanted side effects for too
3239 * long.
3240 */
3241 if (txn->cookie_first_date && s->be->cookie_maxlife &&
3242 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) ||
3243 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
3244 txn->flags &= ~TX_CK_MASK;
3245 txn->flags |= TX_CK_OLD;
3246 delim = val_beg; // let's pretend we have not found the cookie
3247 txn->cookie_first_date = 0;
3248 txn->cookie_last_date = 0;
3249 }
3250 else if (txn->cookie_last_date && s->be->cookie_maxidle &&
3251 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) ||
3252 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
3253 txn->flags &= ~TX_CK_MASK;
3254 txn->flags |= TX_CK_EXPIRED;
3255 delim = val_beg; // let's pretend we have not found the cookie
3256 txn->cookie_first_date = 0;
3257 txn->cookie_last_date = 0;
3258 }
3259
3260 /* Here, we'll look for the first running server which supports the cookie.
3261 * This allows to share a same cookie between several servers, for example
3262 * to dedicate backup servers to specific servers only.
3263 * However, to prevent clients from sticking to cookie-less backup server
3264 * when they have incidentely learned an empty cookie, we simply ignore
3265 * empty cookies and mark them as invalid.
3266 * The same behaviour is applied when persistence must be ignored.
3267 */
3268 if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
3269 srv = NULL;
3270
3271 while (srv) {
3272 if (srv->cookie && (srv->cklen == delim - val_beg) &&
3273 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
3274 if ((srv->cur_state != SRV_ST_STOPPED) ||
3275 (s->be->options & PR_O_PERSIST) ||
3276 (s->flags & SF_FORCE_PRST)) {
3277 /* we found the server and we can use it */
3278 txn->flags &= ~TX_CK_MASK;
3279 txn->flags |= (srv->cur_state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
3280 s->flags |= SF_DIRECT | SF_ASSIGNED;
3281 s->target = &srv->obj_type;
3282 break;
3283 } else {
3284 /* we found a server, but it's down,
3285 * mark it as such and go on in case
3286 * another one is available.
3287 */
3288 txn->flags &= ~TX_CK_MASK;
3289 txn->flags |= TX_CK_DOWN;
3290 }
3291 }
3292 srv = srv->next;
3293 }
3294
3295 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
3296 /* no server matched this cookie or we deliberately skipped it */
3297 txn->flags &= ~TX_CK_MASK;
3298 if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
3299 txn->flags |= TX_CK_UNUSED;
3300 else
3301 txn->flags |= TX_CK_INVALID;
3302 }
3303
3304 /* depending on the cookie mode, we may have to either :
3305 * - delete the complete cookie if we're in insert+indirect mode, so that
3306 * the server never sees it ;
3307 * - remove the server id from the cookie value, and tag the cookie as an
Joseph Herlante9d5c722018-11-25 11:00:25 -08003308 * application cookie so that it does not get accidentally removed later,
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003309 * if we're in cookie prefix mode
3310 */
3311 if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
3312 int delta; /* negative */
3313
3314 memmove(val_beg, delim + 1, hdr_end - (delim + 1));
3315 delta = val_beg - (delim + 1);
3316 val_end += delta;
3317 next += delta;
3318 hdr_end += delta;
3319 del_from = NULL;
3320 preserve_hdr = 1; /* we want to keep this cookie */
3321 }
3322 else if (del_from == NULL &&
3323 (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
3324 del_from = prev;
3325 }
3326 }
3327 else {
3328 /* This is not our cookie, so we must preserve it. But if we already
3329 * scheduled another cookie for removal, we cannot remove the
3330 * complete header, but we can remove the previous block itself.
3331 */
3332 preserve_hdr = 1;
3333
3334 if (del_from != NULL) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003335 int delta = http_del_hdr_value(hdr_beg, hdr_end, &del_from, prev);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003336 if (att_beg >= del_from)
3337 att_beg += delta;
3338 if (att_end >= del_from)
3339 att_end += delta;
3340 val_beg += delta;
3341 val_end += delta;
3342 next += delta;
3343 hdr_end += delta;
3344 prev = del_from;
3345 del_from = NULL;
3346 }
3347 }
3348
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003349 } /* for each cookie */
3350
3351
3352 /* There are no more cookies on this line.
3353 * We may still have one (or several) marked for deletion at the
3354 * end of the line. We must do this now in two ways :
3355 * - if some cookies must be preserved, we only delete from the
3356 * mark to the end of line ;
3357 * - if nothing needs to be preserved, simply delete the whole header
3358 */
3359 if (del_from) {
3360 hdr_end = (preserve_hdr ? del_from : hdr_beg);
3361 }
3362 if ((hdr_end - hdr_beg) != ctx.value.len) {
Christopher Faulet3e2638e2019-06-18 09:49:16 +02003363 if (hdr_beg != hdr_end)
3364 htx_change_blk_value_len(htx, ctx.blk, hdr_end - hdr_beg);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003365 else
3366 http_remove_header(htx, &ctx);
3367 }
3368 } /* for each "Cookie header */
3369}
3370
3371/*
3372 * Manage server-side cookies. It can impact performance by about 2% so it is
3373 * desirable to call it only when needed. This function is also used when we
3374 * just need to know if there is a cookie (eg: for check-cache).
3375 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003376static void http_manage_server_side_cookies(struct stream *s, struct channel *res)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003377{
3378 struct session *sess = s->sess;
3379 struct http_txn *txn = s->txn;
3380 struct htx *htx;
3381 struct http_hdr_ctx ctx;
3382 struct server *srv;
3383 char *hdr_beg, *hdr_end;
3384 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003385
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003386 htx = htxbuf(&res->buf);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003387
3388 ctx.blk = NULL;
Willy Tarreauaa1909e2022-11-14 18:58:35 +01003389 while (http_find_header(htx, ist("Set-Cookie"), &ctx, 1)) {
Olivier Houchardf0f42382019-07-22 17:43:46 +02003390 int is_first = 1;
3391
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003392 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
3393 * <prev> points to the colon.
3394 */
3395 txn->flags |= TX_SCK_PRESENT;
3396
3397 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
3398 * check-cache is enabled) and we are not interested in checking
3399 * them. Warning, the cookie capture is declared in the frontend.
3400 */
3401 if (s->be->cookie_name == NULL && sess->fe->capture_name == NULL)
3402 break;
3403
3404 /* OK so now we know we have to process this response cookie.
3405 * The format of the Set-Cookie header is slightly different
3406 * from the format of the Cookie header in that it does not
3407 * support the comma as a cookie delimiter (thus the header
3408 * cannot be folded) because the Expires attribute described in
3409 * the original Netscape's spec may contain an unquoted date
3410 * with a comma inside. We have to live with this because
3411 * many browsers don't support Max-Age and some browsers don't
3412 * support quoted strings. However the Set-Cookie2 header is
Willy Tarreauaa1909e2022-11-14 18:58:35 +01003413 * clean but basically nobody supports it.
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003414 *
3415 * We have to keep multiple pointers in order to support cookie
3416 * removal at the beginning, middle or end of header without
3417 * corrupting the header (in case of set-cookie2). A special
3418 * pointer, <scav> points to the beginning of the set-cookie-av
3419 * fields after the first semi-colon. The <next> pointer points
3420 * either to the end of line (set-cookie) or next unquoted comma
3421 * (set-cookie2). All of these headers are valid :
3422 *
3423 * hdr_beg hdr_end
3424 * | |
3425 * v |
3426 * NAME1 = VALUE 1 ; Secure; Path="/" |
3427 * NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT v
3428 * NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT
3429 * NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard
3430 * | | | | | | | |
3431 * | | | | | | | +-> next
3432 * | | | | | | +------------> scav
3433 * | | | | | +--------------> val_end
3434 * | | | | +--------------------> val_beg
3435 * | | | +----------------------> equal
3436 * | | +------------------------> att_end
3437 * | +----------------------------> att_beg
3438 * +------------------------------> prev
3439 * -------------------------------> hdr_beg
3440 */
3441 hdr_beg = ctx.value.ptr;
3442 hdr_end = hdr_beg + ctx.value.len;
3443 for (prev = hdr_beg; prev < hdr_end; prev = next) {
3444
3445 /* Iterate through all cookies on this line */
3446
3447 /* find att_beg */
3448 att_beg = prev;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003449 if (!is_first)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003450 att_beg++;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003451 is_first = 0;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003452
3453 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
3454 att_beg++;
3455
3456 /* find att_end : this is the first character after the last non
3457 * space before the equal. It may be equal to hdr_end.
3458 */
3459 equal = att_end = att_beg;
3460
3461 while (equal < hdr_end) {
Willy Tarreauaa1909e2022-11-14 18:58:35 +01003462 if (*equal == '=' || *equal == ';')
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003463 break;
3464 if (HTTP_IS_SPHT(*equal++))
3465 continue;
3466 att_end = equal;
3467 }
3468
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003469 /* here, <equal> points to '=', a delimiter or the end. <att_end>
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003470 * is between <att_beg> and <equal>, both may be identical.
3471 */
3472
3473 /* look for end of cookie if there is an equal sign */
3474 if (equal < hdr_end && *equal == '=') {
3475 /* look for the beginning of the value */
3476 val_beg = equal + 1;
3477 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
3478 val_beg++;
3479
3480 /* find the end of the value, respecting quotes */
3481 next = http_find_cookie_value_end(val_beg, hdr_end);
3482
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003483 /* make val_end point to the first white space or delimiter after the value */
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003484 val_end = next;
3485 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
3486 val_end--;
3487 }
3488 else {
3489 /* <equal> points to next comma, semi-colon or EOL */
3490 val_beg = val_end = next = equal;
3491 }
3492
3493 if (next < hdr_end) {
Willy Tarreauaa1909e2022-11-14 18:58:35 +01003494 /* For Set-Cookie, since commas are permitted
3495 * in values, skip to the end.
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003496 */
Willy Tarreauaa1909e2022-11-14 18:58:35 +01003497 next = hdr_end;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003498 }
3499
3500 /* Now everything is as on the diagram above */
3501
3502 /* Ignore cookies with no equal sign */
3503 if (equal == val_end)
3504 continue;
3505
3506 /* If there are spaces around the equal sign, we need to
3507 * strip them otherwise we'll get trouble for cookie captures,
3508 * or even for rewrites. Since this happens extremely rarely,
3509 * it does not hurt performance.
3510 */
3511 if (unlikely(att_end != equal || val_beg > equal + 1)) {
3512 int stripped_before = 0;
3513 int stripped_after = 0;
3514
3515 if (att_end != equal) {
3516 memmove(att_end, equal, hdr_end - equal);
3517 stripped_before = (att_end - equal);
3518 equal += stripped_before;
3519 val_beg += stripped_before;
3520 }
3521
3522 if (val_beg > equal + 1) {
3523 memmove(equal + 1, val_beg, hdr_end + stripped_before - val_beg);
3524 stripped_after = (equal + 1) - val_beg;
3525 val_beg += stripped_after;
3526 stripped_before += stripped_after;
3527 }
3528
3529 val_end += stripped_before;
3530 next += stripped_before;
3531 hdr_end += stripped_before;
3532
Christopher Faulet3e2638e2019-06-18 09:49:16 +02003533 htx_change_blk_value_len(htx, ctx.blk, hdr_end - hdr_beg);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003534 ctx.value.len = hdr_end - hdr_beg;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003535 }
3536
3537 /* First, let's see if we want to capture this cookie. We check
3538 * that we don't already have a server side cookie, because we
3539 * can only capture one. Also as an optimisation, we ignore
3540 * cookies shorter than the declared name.
3541 */
3542 if (sess->fe->capture_name != NULL &&
3543 txn->srv_cookie == NULL &&
3544 (val_end - att_beg >= sess->fe->capture_namelen) &&
3545 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
3546 int log_len = val_end - att_beg;
3547 if ((txn->srv_cookie = pool_alloc(pool_head_capture)) == NULL) {
3548 ha_alert("HTTP logging : out of memory.\n");
3549 }
3550 else {
3551 if (log_len > sess->fe->capture_len)
3552 log_len = sess->fe->capture_len;
3553 memcpy(txn->srv_cookie, att_beg, log_len);
3554 txn->srv_cookie[log_len] = 0;
3555 }
3556 }
3557
3558 srv = objt_server(s->target);
3559 /* now check if we need to process it for persistence */
3560 if (!(s->flags & SF_IGNORE_PRST) &&
3561 (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
3562 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
3563 /* assume passive cookie by default */
3564 txn->flags &= ~TX_SCK_MASK;
3565 txn->flags |= TX_SCK_FOUND;
3566
3567 /* If the cookie is in insert mode on a known server, we'll delete
3568 * this occurrence because we'll insert another one later.
3569 * We'll delete it too if the "indirect" option is set and we're in
3570 * a direct access.
3571 */
3572 if (s->be->ck_opts & PR_CK_PSV) {
3573 /* The "preserve" flag was set, we don't want to touch the
3574 * server's cookie.
3575 */
3576 }
3577 else if ((srv && (s->be->ck_opts & PR_CK_INS)) ||
3578 ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) {
3579 /* this cookie must be deleted */
3580 if (prev == hdr_beg && next == hdr_end) {
3581 /* whole header */
3582 http_remove_header(htx, &ctx);
3583 /* note: while both invalid now, <next> and <hdr_end>
3584 * are still equal, so the for() will stop as expected.
3585 */
3586 } else {
3587 /* just remove the value */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003588 int delta = http_del_hdr_value(hdr_beg, hdr_end, &prev, next);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003589 next = prev;
3590 hdr_end += delta;
3591 }
3592 txn->flags &= ~TX_SCK_MASK;
3593 txn->flags |= TX_SCK_DELETED;
3594 /* and go on with next cookie */
3595 }
3596 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) {
3597 /* replace bytes val_beg->val_end with the cookie name associated
3598 * with this server since we know it.
3599 */
3600 int sliding, delta;
3601
3602 ctx.value = ist2(val_beg, val_end - val_beg);
3603 ctx.lws_before = ctx.lws_after = 0;
3604 http_replace_header_value(htx, &ctx, ist2(srv->cookie, srv->cklen));
3605 delta = srv->cklen - (val_end - val_beg);
3606 sliding = (ctx.value.ptr - val_beg);
3607 hdr_beg += sliding;
3608 val_beg += sliding;
3609 next += sliding + delta;
3610 hdr_end += sliding + delta;
3611
3612 txn->flags &= ~TX_SCK_MASK;
3613 txn->flags |= TX_SCK_REPLACED;
3614 }
3615 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) {
3616 /* insert the cookie name associated with this server
3617 * before existing cookie, and insert a delimiter between them..
3618 */
3619 int sliding, delta;
3620 ctx.value = ist2(val_beg, 0);
3621 ctx.lws_before = ctx.lws_after = 0;
3622 http_replace_header_value(htx, &ctx, ist2(srv->cookie, srv->cklen + 1));
3623 delta = srv->cklen + 1;
3624 sliding = (ctx.value.ptr - val_beg);
3625 hdr_beg += sliding;
3626 val_beg += sliding;
3627 next += sliding + delta;
3628 hdr_end += sliding + delta;
3629
3630 val_beg[srv->cklen] = COOKIE_DELIM;
3631 txn->flags &= ~TX_SCK_MASK;
3632 txn->flags |= TX_SCK_REPLACED;
3633 }
3634 }
3635 /* that's done for this cookie, check the next one on the same
Willy Tarreauaa1909e2022-11-14 18:58:35 +01003636 * line when next != hdr_end (which should normally not happen
3637 * with set-cookie2 support removed).
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003638 */
3639 }
3640 }
3641}
3642
Christopher Faulet25a02f62018-10-24 12:00:25 +02003643/*
3644 * Parses the Cache-Control and Pragma request header fields to determine if
3645 * the request may be served from the cache and/or if it is cacheable. Updates
3646 * s->txn->flags.
3647 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003648void http_check_request_for_cacheability(struct stream *s, struct channel *req)
Christopher Faulet25a02f62018-10-24 12:00:25 +02003649{
3650 struct http_txn *txn = s->txn;
3651 struct htx *htx;
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003652 struct http_hdr_ctx ctx = { .blk = NULL };
3653 int pragma_found, cc_found;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003654
3655 if ((txn->flags & (TX_CACHEABLE|TX_CACHE_IGNORE)) == TX_CACHE_IGNORE)
3656 return; /* nothing more to do here */
3657
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003658 htx = htxbuf(&req->buf);
Christopher Faulet25a02f62018-10-24 12:00:25 +02003659 pragma_found = cc_found = 0;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003660
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003661 /* Check "pragma" header for HTTP/1.0 compatibility. */
3662 if (http_find_header(htx, ist("pragma"), &ctx, 1)) {
3663 if (isteqi(ctx.value, ist("no-cache"))) {
3664 pragma_found = 1;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003665 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003666 }
Christopher Faulet25a02f62018-10-24 12:00:25 +02003667
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003668 ctx.blk = NULL;
3669 /* Don't use the cache and don't try to store if we found the
3670 * Authorization header */
3671 if (http_find_header(htx, ist("authorization"), &ctx, 1)) {
3672 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3673 txn->flags |= TX_CACHE_IGNORE;
3674 }
Christopher Faulet25a02f62018-10-24 12:00:25 +02003675
Christopher Faulet25a02f62018-10-24 12:00:25 +02003676
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003677 /* Look for "cache-control" header and iterate over all the values
3678 * until we find one that specifies that caching is possible or not. */
3679 ctx.blk = NULL;
3680 while (http_find_header(htx, ist("cache-control"), &ctx, 0)) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003681 cc_found = 1;
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003682 /* We don't check the values after max-age, max-stale nor min-fresh,
3683 * we simply don't use the cache when they're specified. */
3684 if (istmatchi(ctx.value, ist("max-age")) ||
3685 istmatchi(ctx.value, ist("no-cache")) ||
3686 istmatchi(ctx.value, ist("max-stale")) ||
3687 istmatchi(ctx.value, ist("min-fresh"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003688 txn->flags |= TX_CACHE_IGNORE;
3689 continue;
3690 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003691 if (istmatchi(ctx.value, ist("no-store"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003692 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3693 continue;
3694 }
3695 }
3696
3697 /* RFC7234#5.4:
3698 * When the Cache-Control header field is also present and
3699 * understood in a request, Pragma is ignored.
3700 * When the Cache-Control header field is not present in a
3701 * request, caches MUST consider the no-cache request
3702 * pragma-directive as having the same effect as if
3703 * "Cache-Control: no-cache" were present.
3704 */
3705 if (!cc_found && pragma_found)
3706 txn->flags |= TX_CACHE_IGNORE;
3707}
3708
3709/*
3710 * Check if response is cacheable or not. Updates s->txn->flags.
3711 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003712void http_check_response_for_cacheability(struct stream *s, struct channel *res)
Christopher Faulet25a02f62018-10-24 12:00:25 +02003713{
3714 struct http_txn *txn = s->txn;
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003715 struct http_hdr_ctx ctx = { .blk = NULL };
Christopher Faulet25a02f62018-10-24 12:00:25 +02003716 struct htx *htx;
Remi Tricot-Le Bretoncc9bf2e2020-11-12 11:14:41 +01003717 int has_freshness_info = 0;
3718 int has_validator = 0;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003719
3720 if (txn->status < 200) {
3721 /* do not try to cache interim responses! */
3722 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3723 return;
3724 }
3725
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003726 htx = htxbuf(&res->buf);
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003727 /* Check "pragma" header for HTTP/1.0 compatibility. */
3728 if (http_find_header(htx, ist("pragma"), &ctx, 1)) {
3729 if (isteqi(ctx.value, ist("no-cache"))) {
3730 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3731 return;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003732 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003733 }
Christopher Faulet25a02f62018-10-24 12:00:25 +02003734
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003735 /* Look for "cache-control" header and iterate over all the values
3736 * until we find one that specifies that caching is possible or not. */
3737 ctx.blk = NULL;
3738 while (http_find_header(htx, ist("cache-control"), &ctx, 0)) {
3739 if (isteqi(ctx.value, ist("public"))) {
3740 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003741 continue;
3742 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003743 if (isteqi(ctx.value, ist("private")) ||
3744 isteqi(ctx.value, ist("no-cache")) ||
3745 isteqi(ctx.value, ist("no-store")) ||
3746 isteqi(ctx.value, ist("max-age=0")) ||
3747 isteqi(ctx.value, ist("s-maxage=0"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003748 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003749 continue;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003750 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003751 /* We might have a no-cache="set-cookie" form. */
3752 if (istmatchi(ctx.value, ist("no-cache=\"set-cookie"))) {
3753 txn->flags &= ~TX_CACHE_COOK;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003754 continue;
3755 }
Remi Tricot-Le Bretoncc9bf2e2020-11-12 11:14:41 +01003756
3757 if (istmatchi(ctx.value, ist("s-maxage")) ||
3758 istmatchi(ctx.value, ist("max-age"))) {
3759 has_freshness_info = 1;
3760 continue;
3761 }
3762 }
3763
3764 /* If no freshness information could be found in Cache-Control values,
3765 * look for an Expires header. */
3766 if (!has_freshness_info) {
3767 ctx.blk = NULL;
3768 has_freshness_info = http_find_header(htx, ist("expires"), &ctx, 0);
Christopher Faulet25a02f62018-10-24 12:00:25 +02003769 }
Remi Tricot-Le Bretoncc9bf2e2020-11-12 11:14:41 +01003770
3771 /* If no freshness information could be found in Cache-Control or Expires
3772 * values, look for an explicit validator. */
3773 if (!has_freshness_info) {
3774 ctx.blk = NULL;
3775 has_validator = 1;
3776 if (!http_find_header(htx, ist("etag"), &ctx, 0)) {
3777 ctx.blk = NULL;
3778 if (!http_find_header(htx, ist("last-modified"), &ctx, 0))
3779 has_validator = 0;
3780 }
3781 }
3782
3783 /* We won't store an entry that has neither a cache validator nor an
3784 * explicit expiration time, as suggested in RFC 7234#3. */
3785 if (!has_freshness_info && !has_validator)
Remi Tricot-Le Breton879debe2023-02-21 11:47:17 +01003786 txn->flags &= ~TX_CACHEABLE;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003787}
3788
Christopher Faulet377c5a52018-10-24 21:21:30 +02003789/*
3790 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
3791 * for the current backend.
3792 *
3793 * It is assumed that the request is either a HEAD, GET, or POST and that the
3794 * uri_auth field is valid.
3795 *
3796 * Returns 1 if stats should be provided, otherwise 0.
3797 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003798static int http_stats_check_uri(struct stream *s, struct http_txn *txn, struct proxy *backend)
Christopher Faulet377c5a52018-10-24 21:21:30 +02003799{
3800 struct uri_auth *uri_auth = backend->uri_auth;
3801 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003802 struct htx_sl *sl;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003803 struct ist uri;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003804
3805 if (!uri_auth)
3806 return 0;
3807
3808 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
3809 return 0;
3810
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003811 htx = htxbuf(&s->req.buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02003812 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003813 uri = htx_sl_req_uri(sl);
Amaury Denoyellec453f952021-07-06 11:40:12 +02003814 if (*uri_auth->uri_prefix == '/') {
3815 struct http_uri_parser parser = http_uri_parser_init(uri);
3816 uri = http_parse_path(&parser);
3817 }
Christopher Faulet377c5a52018-10-24 21:21:30 +02003818
3819 /* check URI size */
3820 if (uri_auth->uri_len > uri.len)
3821 return 0;
3822
3823 if (memcmp(uri.ptr, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
3824 return 0;
3825
3826 return 1;
3827}
3828
3829/* This function prepares an applet to handle the stats. It can deal with the
3830 * "100-continue" expectation, check that admin rules are met for POST requests,
3831 * and program a response message if something was unexpected. It cannot fail
3832 * and always relies on the stats applet to complete the job. It does not touch
3833 * analysers nor counters, which are left to the caller. It does not touch
3834 * s->target which is supposed to already point to the stats applet. The caller
3835 * is expected to have already assigned an appctx to the stream.
3836 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003837static int http_handle_stats(struct stream *s, struct channel *req)
Christopher Faulet377c5a52018-10-24 21:21:30 +02003838{
3839 struct stats_admin_rule *stats_admin_rule;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003840 struct session *sess = s->sess;
3841 struct http_txn *txn = s->txn;
3842 struct http_msg *msg = &txn->req;
3843 struct uri_auth *uri_auth = s->be->uri_auth;
3844 const char *h, *lookup, *end;
Willy Tarreau8e7c6e62022-05-18 17:58:02 +02003845 struct appctx *appctx = __sc_appctx(s->scb);
Willy Tarreau91cefca2022-05-03 17:08:29 +02003846 struct show_stat_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx));
Christopher Faulet377c5a52018-10-24 21:21:30 +02003847 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003848 struct htx_sl *sl;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003849
Willy Tarreau41f88522022-05-03 18:39:27 +02003850 appctx->st1 = 0;
Willy Tarreau6ef16482022-05-06 18:07:53 +02003851 ctx->state = STAT_STATE_INIT;
Willy Tarreau91cefca2022-05-03 17:08:29 +02003852 ctx->st_code = STAT_STATUS_INIT;
3853 ctx->flags |= uri_auth->flags;
3854 ctx->flags |= STAT_FMT_HTML; /* assume HTML mode by default */
Christopher Faulet377c5a52018-10-24 21:21:30 +02003855 if ((msg->flags & HTTP_MSGF_VER_11) && (txn->meth != HTTP_METH_HEAD))
Willy Tarreau91cefca2022-05-03 17:08:29 +02003856 ctx->flags |= STAT_CHUNKED;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003857
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003858 htx = htxbuf(&req->buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02003859 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003860 lookup = HTX_SL_REQ_UPTR(sl) + uri_auth->uri_len;
3861 end = HTX_SL_REQ_UPTR(sl) + HTX_SL_REQ_ULEN(sl);
Christopher Faulet377c5a52018-10-24 21:21:30 +02003862
3863 for (h = lookup; h <= end - 3; h++) {
3864 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02003865 ctx->flags |= STAT_HIDE_DOWN;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003866 break;
3867 }
Amaury Denoyelle91e55ea2021-02-25 14:46:08 +01003868 }
3869
3870 for (h = lookup; h <= end - 9; h++) {
3871 if (memcmp(h, ";no-maint", 9) == 0) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02003872 ctx->flags |= STAT_HIDE_MAINT;
Willy Tarreau3e320362020-10-23 17:28:57 +02003873 break;
3874 }
Christopher Faulet377c5a52018-10-24 21:21:30 +02003875 }
3876
3877 if (uri_auth->refresh) {
3878 for (h = lookup; h <= end - 10; h++) {
3879 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02003880 ctx->flags |= STAT_NO_REFRESH;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003881 break;
3882 }
3883 }
3884 }
3885
3886 for (h = lookup; h <= end - 4; h++) {
3887 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02003888 ctx->flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
Christopher Faulet377c5a52018-10-24 21:21:30 +02003889 break;
3890 }
3891 }
3892
3893 for (h = lookup; h <= end - 6; h++) {
3894 if (memcmp(h, ";typed", 6) == 0) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02003895 ctx->flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
3896 ctx->flags |= STAT_FMT_TYPED;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003897 break;
3898 }
3899 }
3900
Christopher Faulet6338a082019-09-09 15:50:54 +02003901 for (h = lookup; h <= end - 5; h++) {
3902 if (memcmp(h, ";json", 5) == 0) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02003903 ctx->flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
3904 ctx->flags |= STAT_FMT_JSON;
Christopher Faulet6338a082019-09-09 15:50:54 +02003905 break;
3906 }
3907 }
3908
3909 for (h = lookup; h <= end - 12; h++) {
3910 if (memcmp(h, ";json-schema", 12) == 0) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02003911 ctx->flags &= ~STAT_FMT_MASK;
3912 ctx->flags |= STAT_JSON_SCHM;
Christopher Faulet6338a082019-09-09 15:50:54 +02003913 break;
3914 }
3915 }
3916
Christopher Faulet377c5a52018-10-24 21:21:30 +02003917 for (h = lookup; h <= end - 8; h++) {
3918 if (memcmp(h, ";st=", 4) == 0) {
3919 int i;
3920 h += 4;
Willy Tarreau91cefca2022-05-03 17:08:29 +02003921 ctx->st_code = STAT_STATUS_UNKN;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003922 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
3923 if (strncmp(stat_status_codes[i], h, 4) == 0) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02003924 ctx->st_code = i;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003925 break;
3926 }
3927 }
3928 break;
3929 }
3930 }
3931
Willy Tarreau91cefca2022-05-03 17:08:29 +02003932 ctx->scope_str = 0;
3933 ctx->scope_len = 0;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003934 for (h = lookup; h <= end - 8; h++) {
3935 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
3936 int itx = 0;
3937 const char *h2;
3938 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
3939 const char *err;
3940
3941 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
3942 h2 = h;
Willy Tarreau91cefca2022-05-03 17:08:29 +02003943 ctx->scope_str = h2 - HTX_SL_REQ_UPTR(sl);
Christopher Fauleted7a0662019-01-14 11:07:34 +01003944 while (h < end) {
Christopher Faulet377c5a52018-10-24 21:21:30 +02003945 if (*h == ';' || *h == '&' || *h == ' ')
3946 break;
3947 itx++;
3948 h++;
3949 }
3950
3951 if (itx > STAT_SCOPE_TXT_MAXLEN)
3952 itx = STAT_SCOPE_TXT_MAXLEN;
Willy Tarreau91cefca2022-05-03 17:08:29 +02003953 ctx->scope_len = itx;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003954
Willy Tarreau91cefca2022-05-03 17:08:29 +02003955 /* scope_txt = search query, ctx->scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Christopher Faulet377c5a52018-10-24 21:21:30 +02003956 memcpy(scope_txt, h2, itx);
3957 scope_txt[itx] = '\0';
3958 err = invalid_char(scope_txt);
3959 if (err) {
3960 /* bad char in search text => clear scope */
Willy Tarreau91cefca2022-05-03 17:08:29 +02003961 ctx->scope_str = 0;
3962 ctx->scope_len = 0;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003963 }
3964 break;
3965 }
3966 }
3967
3968 /* now check whether we have some admin rules for this request */
3969 list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) {
3970 int ret = 1;
3971
3972 if (stats_admin_rule->cond) {
3973 ret = acl_exec_cond(stats_admin_rule->cond, s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
3974 ret = acl_pass(ret);
3975 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
3976 ret = !ret;
3977 }
3978
3979 if (ret) {
3980 /* no rule, or the rule matches */
Willy Tarreau91cefca2022-05-03 17:08:29 +02003981 ctx->flags |= STAT_ADMIN;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003982 break;
3983 }
3984 }
3985
Christopher Faulet5d45e382019-02-27 15:15:23 +01003986 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
3987 appctx->st0 = STAT_HTTP_HEAD;
3988 else if (txn->meth == HTTP_METH_POST) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02003989 if (ctx->flags & STAT_ADMIN) {
Christopher Faulet377c5a52018-10-24 21:21:30 +02003990 appctx->st0 = STAT_HTTP_POST;
Christopher Fauletbd9e8422019-08-15 22:26:48 +02003991 if (msg->msg_state < HTTP_MSG_DATA)
3992 req->analysers |= AN_REQ_HTTP_BODY;
3993 }
Christopher Faulet377c5a52018-10-24 21:21:30 +02003994 else {
Christopher Faulet5d45e382019-02-27 15:15:23 +01003995 /* POST without admin level */
Willy Tarreau91cefca2022-05-03 17:08:29 +02003996 ctx->flags &= ~STAT_CHUNKED;
3997 ctx->st_code = STAT_STATUS_DENY;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003998 appctx->st0 = STAT_HTTP_LAST;
3999 }
4000 }
4001 else {
Christopher Faulet5d45e382019-02-27 15:15:23 +01004002 /* Unsupported method */
Willy Tarreau91cefca2022-05-03 17:08:29 +02004003 ctx->flags &= ~STAT_CHUNKED;
4004 ctx->st_code = STAT_STATUS_IVAL;
Christopher Faulet5d45e382019-02-27 15:15:23 +01004005 appctx->st0 = STAT_HTTP_LAST;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004006 }
4007
4008 s->task->nice = -32; /* small boost for HTTP statistics */
4009 return 1;
4010}
4011
Christopher Faulet021a8e42021-03-29 10:46:38 +02004012/* This function waits for the message payload at most <time> milliseconds (may
4013 * be set to TICK_ETERNITY). It stops to wait if at least <bytes> bytes of the
4014 * payload are received (0 means no limit). It returns HTTP_RULE_* depending on
4015 * the result:
4016 *
4017 * - HTTP_RULE_RES_CONT when conditions are met to stop waiting
4018 * - HTTP_RULE_RES_YIELD to wait for more data
Ilya Shipitsinb2be9a12021-04-24 13:25:42 +05004019 * - HTTP_RULE_RES_ABRT when a timeout occurred.
Christopher Faulet021a8e42021-03-29 10:46:38 +02004020 * - HTTP_RULE_RES_BADREQ if a parsing error is raised by lower level
Ilya Shipitsinb2be9a12021-04-24 13:25:42 +05004021 * - HTTP_RULE_RES_ERROR if an internal error occurred
Christopher Faulet021a8e42021-03-29 10:46:38 +02004022 *
Ilya Shipitsinb2be9a12021-04-24 13:25:42 +05004023 * If a timeout occurred, this function is responsible to emit the right response
Christopher Faulet021a8e42021-03-29 10:46:38 +02004024 * to the client, depending on the channel (408 on request side, 504 on response
4025 * side). All other errors must be handled by the caller.
4026 */
4027enum rule_result http_wait_for_msg_body(struct stream *s, struct channel *chn,
4028 unsigned int time, unsigned int bytes)
4029{
4030 struct session *sess = s->sess;
4031 struct http_txn *txn = s->txn;
4032 struct http_msg *msg = ((chn->flags & CF_ISRESP) ? &txn->rsp : &txn->req);
4033 struct htx *htx;
4034 enum rule_result ret = HTTP_RULE_RES_CONT;
4035
4036 htx = htxbuf(&chn->buf);
4037
4038 if (htx->flags & HTX_FL_PARSING_ERROR) {
4039 ret = HTTP_RULE_RES_BADREQ;
4040 goto end;
4041 }
4042 if (htx->flags & HTX_FL_PROCESSING_ERROR) {
4043 ret = HTTP_RULE_RES_ERROR;
4044 goto end;
4045 }
4046
4047 /* Do nothing for bodyless and CONNECT requests */
4048 if (txn->meth == HTTP_METH_CONNECT || (msg->flags & HTTP_MSGF_BODYLESS))
4049 goto end;
4050
Christopher Fauletffcffa82023-04-05 10:33:31 +02004051 if (!(chn->flags & CF_ISRESP)) {
Christopher Faulet021a8e42021-03-29 10:46:38 +02004052 if (http_handle_expect_hdr(s, htx, msg) == -1) {
4053 ret = HTTP_RULE_RES_ERROR;
4054 goto end;
4055 }
4056 }
4057
Christopher Faulet2954bcc2023-04-05 10:42:03 +02004058 /* Now we're are waiting for the payload. We just need to know if all
4059 * data have been received or if the buffer is full.
Christopher Faulet021a8e42021-03-29 10:46:38 +02004060 */
Christopher Faulet78335962021-09-23 14:46:32 +02004061 if ((htx->flags & HTX_FL_EOM) ||
4062 htx_get_tail_type(htx) > HTX_BLK_DATA ||
4063 channel_htx_full(chn, htx, global.tune.maxrewrite) ||
Willy Tarreau99615ed2022-05-25 07:29:36 +02004064 sc_waiting_room(chn_prod(chn)))
Christopher Faulet021a8e42021-03-29 10:46:38 +02004065 goto end;
4066
4067 if (bytes) {
4068 struct htx_blk *blk;
4069 unsigned int len = 0;
4070
4071 for (blk = htx_get_first_blk(htx); blk; blk = htx_get_next_blk(htx, blk)) {
4072 if (htx_get_blk_type(blk) != HTX_BLK_DATA)
4073 continue;
4074 len += htx_get_blksz(blk);
4075 if (len >= bytes)
4076 goto end;
4077 }
4078 }
4079
4080 if ((chn->flags & CF_READ_TIMEOUT) || tick_is_expired(chn->analyse_exp, now_ms)) {
4081 if (!(chn->flags & CF_ISRESP))
4082 goto abort_req;
4083 goto abort_res;
4084 }
4085
4086 /* we get here if we need to wait for more data */
Christopher Faulet87633c32023-04-03 18:32:50 +02004087 if (!(chn_prod(chn)->flags & SC_FL_SHUTR)) {
Christopher Faulet021a8e42021-03-29 10:46:38 +02004088 if (!tick_isset(chn->analyse_exp))
4089 chn->analyse_exp = tick_add_ifset(now_ms, time);
4090 ret = HTTP_RULE_RES_YIELD;
4091 }
4092
4093 end:
4094 return ret;
4095
Christopher Fauletf0d80df2023-01-13 10:20:20 +01004096 abort:
4097 http_reply_and_close(s, txn->status, http_error_message(s));
4098 ret = HTTP_RULE_RES_ABRT;
4099 goto end;
4100
Christopher Faulet021a8e42021-03-29 10:46:38 +02004101 abort_req:
4102 txn->status = 408;
4103 if (!(s->flags & SF_ERR_MASK))
4104 s->flags |= SF_ERR_CLITO;
Willy Tarreau4781b152021-04-06 13:53:36 +02004105 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req);
Christopher Faulet021a8e42021-03-29 10:46:38 +02004106 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02004107 _HA_ATOMIC_INC(&sess->listener->counters->failed_req);
Christopher Fauletf0d80df2023-01-13 10:20:20 +01004108 goto abort;
Christopher Faulet021a8e42021-03-29 10:46:38 +02004109
4110 abort_res:
4111 txn->status = 504;
4112 if (!(s->flags & SF_ERR_MASK))
4113 s->flags |= SF_ERR_SRVTO;
Christopher Faulet021a8e42021-03-29 10:46:38 +02004114 stream_inc_http_fail_ctr(s);
Christopher Fauletf0d80df2023-01-13 10:20:20 +01004115 goto abort;
Christopher Faulet021a8e42021-03-29 10:46:38 +02004116}
4117
Willy Tarreaub49672d2022-05-27 10:13:37 +02004118void http_perform_server_redirect(struct stream *s, struct stconn *sc)
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004119{
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004120 struct channel *req = &s->req;
4121 struct channel *res = &s->res;
4122 struct server *srv;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004123 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004124 struct htx_sl *sl;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004125 struct ist path, location;
4126 unsigned int flags;
Amaury Denoyellec453f952021-07-06 11:40:12 +02004127 struct http_uri_parser parser;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004128
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004129 /*
4130 * Create the location
4131 */
4132 chunk_reset(&trash);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004133
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004134 /* 1: add the server's prefix */
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004135 /* special prefix "/" means don't change URL */
4136 srv = __objt_server(s->target);
4137 if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') {
4138 if (!chunk_memcat(&trash, srv->rdr_pfx, srv->rdr_len))
4139 return;
4140 }
4141
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004142 /* 2: add the request Path */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004143 htx = htxbuf(&req->buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02004144 sl = http_get_stline(htx);
Amaury Denoyellec453f952021-07-06 11:40:12 +02004145 parser = http_uri_parser_init(htx_sl_req_uri(sl));
4146 path = http_parse_path(&parser);
Tim Duesterhused526372020-03-05 17:56:33 +01004147 if (!isttest(path))
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004148 return;
4149
4150 if (!chunk_memcat(&trash, path.ptr, path.len))
4151 return;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004152 location = ist2(trash.area, trash.data);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004153
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004154 /*
Ilya Shipitsin4a689da2022-10-29 09:34:32 +05004155 * Create the 302 response
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004156 */
4157 htx = htx_from_buf(&res->buf);
4158 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
4159 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
4160 ist("HTTP/1.1"), ist("302"), ist("Found"));
4161 if (!sl)
4162 goto fail;
4163 sl->info.res.status = 302;
4164 s->txn->status = 302;
4165
4166 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) ||
4167 !htx_add_header(htx, ist("Connection"), ist("close")) ||
4168 !htx_add_header(htx, ist("Content-length"), ist("0")) ||
4169 !htx_add_header(htx, ist("Location"), location))
4170 goto fail;
4171
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004172 if (!htx_add_endof(htx, HTX_BLK_EOH))
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004173 goto fail;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004174
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004175 htx->flags |= HTX_FL_EOM;
Christopher Fauletc20afb82020-01-24 19:16:26 +01004176 htx_to_buf(htx, &res->buf);
Christopher Fauleta72a7e42020-01-28 09:28:11 +01004177 if (!http_forward_proxy_resp(s, 1))
4178 goto fail;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004179
4180 /* return without error. */
Willy Tarreaub49672d2022-05-27 10:13:37 +02004181 sc_shutr(sc);
4182 sc_shutw(sc);
Christopher Faulet50264b42022-03-30 19:39:30 +02004183 s->conn_err_type = STRM_ET_NONE;
Willy Tarreaub49672d2022-05-27 10:13:37 +02004184 sc->state = SC_ST_CLO;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004185
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004186 if (!(s->flags & SF_ERR_MASK))
4187 s->flags |= SF_ERR_LOCAL;
4188 if (!(s->flags & SF_FINST_MASK))
4189 s->flags |= SF_FINST_C;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004190
4191 /* FIXME: we should increase a counter of redirects per server and per backend. */
4192 srv_inc_sess_ctr(srv);
4193 srv_set_sess_last(srv);
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004194 return;
4195
4196 fail:
4197 /* If an error occurred, remove the incomplete HTTP response from the
4198 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004199 channel_htx_truncate(res, htx);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004200}
4201
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05004202/* This function terminates the request because it was completely analyzed or
Christopher Fauletf2824e62018-10-01 12:12:37 +02004203 * because an error was triggered during the body forwarding.
4204 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004205static void http_end_request(struct stream *s)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004206{
4207 struct channel *chn = &s->req;
4208 struct http_txn *txn = s->txn;
4209
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004210 DBG_TRACE_ENTER(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004211
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004212 if (unlikely(txn->req.msg_state < HTTP_MSG_DONE)) {
4213 DBG_TRACE_DEVEL("waiting end of the request", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004214 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004215 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004216
4217 if (txn->req.msg_state == HTTP_MSG_DONE) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004218 /* No need to read anymore, the request was completely parsed.
4219 * We can shut the read side unless we want to abort_on_close,
4220 * or we have a POST request. The issue with POST requests is
4221 * that some browsers still send a CRLF after the request, and
4222 * this CRLF must be read so that it does not remain in the kernel
4223 * buffers, otherwise a close could cause an RST on some systems
4224 * (eg: Linux).
4225 */
Christopher Faulet769d0e92019-03-22 14:23:18 +01004226 if (!(s->be->options & PR_O_ABRT_CLOSE) && txn->meth != HTTP_METH_POST)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004227 channel_dont_read(chn);
4228
4229 /* if the server closes the connection, we want to immediately react
4230 * and close the socket to save packets and syscalls.
4231 */
Willy Tarreaucb041662022-05-17 19:44:42 +02004232 s->scb->flags |= SC_FL_NOHALF;
Christopher Fauletf2824e62018-10-01 12:12:37 +02004233
4234 /* In any case we've finished parsing the request so we must
4235 * disable Nagle when sending data because 1) we're not going
4236 * to shut this side, and 2) the server is waiting for us to
4237 * send pending data.
4238 */
Christopher Faulet68ef2182023-03-17 15:38:18 +01004239 s->scb->flags |= SC_FL_SND_NEVERWAIT;
Christopher Fauletf2824e62018-10-01 12:12:37 +02004240
Christopher Fauletc2fba3f2023-03-01 16:03:17 +01004241 if (txn->rsp.msg_state < HTTP_MSG_BODY ||
4242 (txn->rsp.msg_state < HTTP_MSG_DONE && s->scb->state != SC_ST_CLO)) {
Christopher Fauletaf124362023-02-14 10:48:02 +01004243 /* The server has not finished to respond and the
4244 * backend SC is not closed, so we don't want to move in
4245 * order not to upset it.
Christopher Fauletd01ce402019-01-02 17:44:13 +01004246 */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004247 DBG_TRACE_DEVEL("waiting end of the response", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletd01ce402019-01-02 17:44:13 +01004248 return;
4249 }
4250
Christopher Fauletf2824e62018-10-01 12:12:37 +02004251 /* When we get here, it means that both the request and the
4252 * response have finished receiving. Depending on the connection
4253 * mode, we'll have to wait for the last bytes to leave in either
4254 * direction, and sometimes for a close to be effective.
4255 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02004256 if (txn->flags & TX_CON_WANT_TUN) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004257 /* Tunnel mode will not have any analyser so it needs to
4258 * poll for reads.
4259 */
4260 channel_auto_read(chn);
4261 txn->req.msg_state = HTTP_MSG_TUNNEL;
4262 }
4263 else {
4264 /* we're not expecting any new data to come for this
4265 * transaction, so we can close it.
Christopher Faulet9768c262018-10-22 09:34:31 +02004266 *
4267 * However, there is an exception if the response
4268 * length is undefined. In this case, we need to wait
4269 * the close from the server. The response will be
4270 * switched in TUNNEL mode until the end.
Christopher Fauletf2824e62018-10-01 12:12:37 +02004271 */
4272 if (!(txn->rsp.flags & HTTP_MSGF_XFER_LEN) &&
4273 txn->rsp.msg_state != HTTP_MSG_CLOSED)
Christopher Faulet9768c262018-10-22 09:34:31 +02004274 goto check_channel_flags;
Christopher Fauletf2824e62018-10-01 12:12:37 +02004275
Christopher Faulet87633c32023-04-03 18:32:50 +02004276 if (!(chn_cons(chn)->flags & (SC_FL_SHUTW|SC_FL_SHUTW_NOW))) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004277 channel_shutr_now(chn);
4278 channel_shutw_now(chn);
4279 }
4280 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004281 goto check_channel_flags;
4282 }
4283
4284 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
4285 http_msg_closing:
4286 /* nothing else to forward, just waiting for the output buffer
4287 * to be empty and for the shutw_now to take effect.
4288 */
4289 if (channel_is_empty(chn)) {
4290 txn->req.msg_state = HTTP_MSG_CLOSED;
4291 goto http_msg_closed;
4292 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004293 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004294 return;
4295 }
4296
4297 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
4298 http_msg_closed:
Christopher Fauletf2824e62018-10-01 12:12:37 +02004299 /* if we don't know whether the server will close, we need to hard close */
4300 if (txn->rsp.flags & HTTP_MSGF_XFER_LEN)
Willy Tarreaucb041662022-05-17 19:44:42 +02004301 s->scb->flags |= SC_FL_NOLINGER; /* we want to close ASAP */
Christopher Fauletf2824e62018-10-01 12:12:37 +02004302 /* see above in MSG_DONE why we only do this in these states */
Christopher Faulet769d0e92019-03-22 14:23:18 +01004303 if (!(s->be->options & PR_O_ABRT_CLOSE))
Christopher Fauletf2824e62018-10-01 12:12:37 +02004304 channel_dont_read(chn);
4305 goto end;
4306 }
4307
4308 check_channel_flags:
4309 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
Christopher Faulet87633c32023-04-03 18:32:50 +02004310 if (chn_cons(chn)->flags & (SC_FL_SHUTW|SC_FL_SHUTW_NOW)) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004311 /* if we've just closed an output, let's switch */
4312 txn->req.msg_state = HTTP_MSG_CLOSING;
4313 goto http_msg_closing;
4314 }
4315
4316 end:
4317 chn->analysers &= AN_REQ_FLT_END;
Christopher Faulet198ef8b2020-12-15 13:32:55 +01004318 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
Christopher Faulet68ef2182023-03-17 15:38:18 +01004319 s->scb->flags |= SC_FL_SND_NEVERWAIT;
Christopher Faulet198ef8b2020-12-15 13:32:55 +01004320 if (HAS_REQ_DATA_FILTERS(s))
Christopher Fauletf2824e62018-10-01 12:12:37 +02004321 chn->analysers |= AN_REQ_FLT_XFER_DATA;
Christopher Faulet198ef8b2020-12-15 13:32:55 +01004322 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004323 channel_auto_close(chn);
4324 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004325 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004326}
4327
4328
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05004329/* This function terminates the response because it was completely analyzed or
Christopher Fauletf2824e62018-10-01 12:12:37 +02004330 * because an error was triggered during the body forwarding.
4331 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004332static void http_end_response(struct stream *s)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004333{
4334 struct channel *chn = &s->res;
4335 struct http_txn *txn = s->txn;
4336
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004337 DBG_TRACE_ENTER(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004338
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004339 if (unlikely(txn->rsp.msg_state < HTTP_MSG_DONE)) {
4340 DBG_TRACE_DEVEL("waiting end of the response", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004341 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004342 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004343
4344 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
4345 /* In theory, we don't need to read anymore, but we must
4346 * still monitor the server connection for a possible close
4347 * while the request is being uploaded, so we don't disable
4348 * reading.
4349 */
4350 /* channel_dont_read(chn); */
4351
Christopher Fauletaf124362023-02-14 10:48:02 +01004352 if (txn->req.msg_state < HTTP_MSG_DONE && s->scf->state != SC_ST_CLO) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004353 /* The client seems to still be sending data, probably
4354 * because we got an error response during an upload.
4355 * We have the choice of either breaking the connection
4356 * or letting it pass through. Let's do the later.
4357 */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004358 DBG_TRACE_DEVEL("waiting end of the request", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004359 return;
4360 }
4361
4362 /* When we get here, it means that both the request and the
4363 * response have finished receiving. Depending on the connection
4364 * mode, we'll have to wait for the last bytes to leave in either
4365 * direction, and sometimes for a close to be effective.
4366 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02004367 if (txn->flags & TX_CON_WANT_TUN) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004368 channel_auto_read(chn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004369 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
4370 }
4371 else {
4372 /* we're not expecting any new data to come for this
4373 * transaction, so we can close it.
4374 */
Christopher Faulet87633c32023-04-03 18:32:50 +02004375 if (!(chn_cons(chn)->flags & (SC_FL_SHUTW|SC_FL_SHUTW_NOW))) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004376 channel_shutr_now(chn);
4377 channel_shutw_now(chn);
4378 }
4379 }
4380 goto check_channel_flags;
4381 }
4382
4383 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
4384 http_msg_closing:
4385 /* nothing else to forward, just waiting for the output buffer
4386 * to be empty and for the shutw_now to take effect.
4387 */
4388 if (channel_is_empty(chn)) {
4389 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4390 goto http_msg_closed;
4391 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004392 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004393 return;
4394 }
4395
4396 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
4397 http_msg_closed:
4398 /* drop any pending data */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004399 channel_htx_truncate(&s->req, htxbuf(&s->req.buf));
Christopher Faulet9768c262018-10-22 09:34:31 +02004400 channel_abort(&s->req);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004401 goto end;
4402 }
4403
4404 check_channel_flags:
4405 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
Christopher Faulet87633c32023-04-03 18:32:50 +02004406 if (chn_cons(chn)->flags & (SC_FL_SHUTW|SC_FL_SHUTW_NOW)) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004407 /* if we've just closed an output, let's switch */
4408 txn->rsp.msg_state = HTTP_MSG_CLOSING;
4409 goto http_msg_closing;
4410 }
4411
4412 end:
4413 chn->analysers &= AN_RES_FLT_END;
Christopher Faulet198ef8b2020-12-15 13:32:55 +01004414 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
Christopher Faulet68ef2182023-03-17 15:38:18 +01004415 s->scf->flags |= SC_FL_SND_NEVERWAIT;
Christopher Faulet198ef8b2020-12-15 13:32:55 +01004416 if (HAS_RSP_DATA_FILTERS(s))
4417 chn->analysers |= AN_RES_FLT_XFER_DATA;
4418 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004419 channel_auto_close(chn);
4420 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004421 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004422}
4423
Christopher Fauletef70e252020-01-28 09:26:19 +01004424/* Forward a response generated by HAProxy (error/redirect/return). This
4425 * function forwards all pending incoming data. If <final> is set to 0, nothing
4426 * more is performed. It is used for 1xx informational messages. Otherwise, the
Christopher Faulet507479b2020-05-15 12:29:46 +02004427 * transaction is terminated and the request is emptied. On success 1 is
Christopher Faulet40e6b552020-06-25 16:04:50 +02004428 * returned. If an error occurred, 0 is returned. If it fails, this function
4429 * only exits. It is the caller responsibility to do the cleanup.
Christopher Fauletef70e252020-01-28 09:26:19 +01004430 */
4431int http_forward_proxy_resp(struct stream *s, int final)
4432{
4433 struct channel *req = &s->req;
4434 struct channel *res = &s->res;
4435 struct htx *htx = htxbuf(&res->buf);
4436 size_t data;
4437
4438 if (final) {
4439 htx->flags |= HTX_FL_PROXY_RESP;
Christopher Faulet507479b2020-05-15 12:29:46 +02004440
Christopher Fauletaab1b672020-11-18 16:44:02 +01004441 if (!htx_is_empty(htx) && !http_eval_after_res_rules(s))
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01004442 return 0;
Christopher Fauletef70e252020-01-28 09:26:19 +01004443
Christopher Fauletd6c48362020-10-19 18:01:38 +02004444 if (s->txn->meth == HTTP_METH_HEAD)
4445 htx_skip_msg_payload(htx);
4446
Christopher Fauletef70e252020-01-28 09:26:19 +01004447 channel_auto_read(req);
4448 channel_abort(req);
4449 channel_auto_close(req);
4450 channel_htx_erase(req, htxbuf(&req->buf));
4451
Christopher Fauletef70e252020-01-28 09:26:19 +01004452 channel_auto_read(res);
4453 channel_auto_close(res);
4454 channel_shutr_now(res);
Christopher Faulet904763f2023-03-22 14:53:11 +01004455 s->scb->flags |= SC_FL_EOI; /* The response is terminated, add EOI */
Christopher Faulet42432f32020-11-20 17:43:16 +01004456 htxbuf(&res->buf)->flags |= HTX_FL_EOM; /* no more data are expected */
Christopher Fauletef70e252020-01-28 09:26:19 +01004457 }
Christopher Fauletcf6898c2020-06-25 15:55:11 +02004458 else {
Christopher Faulet904763f2023-03-22 14:53:11 +01004459 /* Send ASAP informational messages. Rely on SC_FL_EOI for final
Christopher Fauletcf6898c2020-06-25 15:55:11 +02004460 * response.
4461 */
Christopher Faulet5c281d52023-03-16 15:53:28 +01004462 s->scf->flags |= SC_FL_SND_ASAP;
Christopher Fauletcf6898c2020-06-25 15:55:11 +02004463 }
Christopher Fauletef70e252020-01-28 09:26:19 +01004464
4465 data = htx->data - co_data(res);
4466 c_adv(res, data);
4467 htx->first = -1;
4468 res->total += data;
4469 return 1;
4470}
4471
Willy Tarreaub49672d2022-05-27 10:13:37 +02004472void http_server_error(struct stream *s, struct stconn *sc, int err,
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004473 int finst, struct http_reply *msg)
Christopher Faulet0f226952018-10-22 09:29:56 +02004474{
Christopher Faulet72c7d8d2020-01-27 15:32:25 +01004475 http_reply_and_close(s, s->txn->status, msg);
Christopher Faulet0f226952018-10-22 09:29:56 +02004476 if (!(s->flags & SF_ERR_MASK))
4477 s->flags |= err;
4478 if (!(s->flags & SF_FINST_MASK))
4479 s->flags |= finst;
4480}
4481
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004482void http_reply_and_close(struct stream *s, short status, struct http_reply *msg)
Christopher Faulet0f226952018-10-22 09:29:56 +02004483{
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004484 if (!msg) {
4485 channel_htx_truncate(&s->res, htxbuf(&s->res.buf));
4486 goto end;
4487 }
4488
4489 if (http_reply_message(s, msg) == -1) {
4490 /* On error, return a 500 error message, but don't rewrite it if
Christopher Faulet40e6b552020-06-25 16:04:50 +02004491 * it is already an internal error. If it was already a "const"
4492 * 500 error, just fail.
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004493 */
Christopher Faulet40e6b552020-06-25 16:04:50 +02004494 if (s->txn->status == 500) {
4495 if (s->txn->flags & TX_CONST_REPLY)
4496 goto end;
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004497 s->txn->flags |= TX_CONST_REPLY;
Christopher Faulet40e6b552020-06-25 16:04:50 +02004498 }
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004499 s->txn->status = 500;
4500 s->txn->http_reply = NULL;
4501 return http_reply_and_close(s, s->txn->status, http_error_message(s));
4502 }
4503
4504end:
Christopher Faulet2d565002021-09-10 09:17:50 +02004505 /* At this staged, HTTP analysis is finished */
4506 s->req.analysers &= AN_REQ_FLT_END;
4507 s->req.analyse_exp = TICK_ETERNITY;
4508
4509 s->res.analysers &= AN_RES_FLT_END;
4510 s->res.analyse_exp = TICK_ETERNITY;
4511
Christopher Faulet0f226952018-10-22 09:29:56 +02004512 channel_auto_read(&s->req);
4513 channel_abort(&s->req);
4514 channel_auto_close(&s->req);
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004515 channel_htx_erase(&s->req, htxbuf(&s->req.buf));
Christopher Faulet72c7d8d2020-01-27 15:32:25 +01004516 channel_auto_read(&s->res);
4517 channel_auto_close(&s->res);
4518 channel_shutr_now(&s->res);
Christopher Faulet0f226952018-10-22 09:29:56 +02004519}
4520
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004521struct http_reply *http_error_message(struct stream *s)
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004522{
4523 const int msgnum = http_get_status_idx(s->txn->status);
4524
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004525 if (s->txn->http_reply)
4526 return s->txn->http_reply;
4527 else if (s->be->replies[msgnum])
4528 return s->be->replies[msgnum];
4529 else if (strm_fe(s)->replies[msgnum])
4530 return strm_fe(s)->replies[msgnum];
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004531 else
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004532 return &http_err_replies[msgnum];
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004533}
4534
Christopher Faulet40e6b552020-06-25 16:04:50 +02004535/* Produces an HTX message from an http reply. Depending on the http reply type,
4536 * a, errorfile, an raw file or a log-format string is used. On success, it
4537 * returns 0. If an error occurs -1 is returned. If it fails, this function only
4538 * exits. It is the caller responsibility to do the cleanup.
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004539 */
Christopher Fauletae43b6c2020-05-27 15:24:22 +02004540int http_reply_to_htx(struct stream *s, struct htx *htx, struct http_reply *reply)
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004541{
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004542 struct buffer *errmsg;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004543 struct htx_sl *sl;
4544 struct buffer *body = NULL;
4545 const char *status, *reason, *clen, *ctype;
4546 unsigned int slflags;
4547 int ret = 0;
4548
Christopher Faulete29a97e2020-05-14 14:49:25 +02004549 /*
4550 * - HTTP_REPLY_ERRFILES unexpected here. handled as no payload if so
4551 *
4552 * - HTTP_REPLY_INDIRECT: switch on another reply if defined or handled
4553 * as no payload if NULL. the TXN status code is set with the status
4554 * of the original reply.
4555 */
4556
4557 if (reply->type == HTTP_REPLY_INDIRECT) {
4558 if (reply->body.reply)
4559 reply = reply->body.reply;
4560 }
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004561 if (reply->type == HTTP_REPLY_ERRMSG && !reply->body.errmsg) {
4562 /* get default error message */
4563 if (reply == s->txn->http_reply)
4564 s->txn->http_reply = NULL;
4565 reply = http_error_message(s);
4566 if (reply->type == HTTP_REPLY_INDIRECT) {
4567 if (reply->body.reply)
4568 reply = reply->body.reply;
4569 }
4570 }
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004571
4572 if (reply->type == HTTP_REPLY_ERRMSG) {
4573 /* implicit or explicit error message*/
4574 errmsg = reply->body.errmsg;
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004575 if (errmsg && !b_is_null(errmsg)) {
Christopher Faulet20567362020-05-15 14:52:49 +02004576 if (!htx_copy_msg(htx, errmsg))
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004577 goto fail;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004578 }
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004579 }
4580 else {
4581 /* no payload, file or log-format string */
4582 if (reply->type == HTTP_REPLY_RAW) {
4583 /* file */
4584 body = &reply->body.obj;
4585 }
4586 else if (reply->type == HTTP_REPLY_LOGFMT) {
4587 /* log-format string */
4588 body = alloc_trash_chunk();
4589 if (!body)
4590 goto fail_alloc;
4591 body->data = build_logline(s, body->area, body->size, &reply->body.fmt);
4592 }
4593 /* else no payload */
4594
4595 status = ultoa(reply->status);
4596 reason = http_get_reason(reply->status);
4597 slflags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_CLEN);
4598 if (!body || !b_data(body))
4599 slflags |= HTX_SL_F_BODYLESS;
4600 sl = htx_add_stline(htx, HTX_BLK_RES_SL, slflags, ist("HTTP/1.1"), ist(status), ist(reason));
4601 if (!sl)
4602 goto fail;
4603 sl->info.res.status = reply->status;
4604
4605 clen = (body ? ultoa(b_data(body)) : "0");
4606 ctype = reply->ctype;
4607
4608 if (!LIST_ISEMPTY(&reply->hdrs)) {
4609 struct http_reply_hdr *hdr;
4610 struct buffer *value = alloc_trash_chunk();
4611
4612 if (!value)
4613 goto fail;
4614
4615 list_for_each_entry(hdr, &reply->hdrs, list) {
4616 chunk_reset(value);
4617 value->data = build_logline(s, value->area, value->size, &hdr->value);
4618 if (b_data(value) && !htx_add_header(htx, hdr->name, ist2(b_head(value), b_data(value)))) {
4619 free_trash_chunk(value);
4620 goto fail;
4621 }
4622 chunk_reset(value);
4623 }
4624 free_trash_chunk(value);
4625 }
4626
4627 if (!htx_add_header(htx, ist("content-length"), ist(clen)) ||
4628 (body && b_data(body) && ctype && !htx_add_header(htx, ist("content-type"), ist(ctype))) ||
4629 !htx_add_endof(htx, HTX_BLK_EOH) ||
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004630 (body && b_data(body) && !htx_add_data_atonce(htx, ist2(b_head(body), b_data(body)))))
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004631 goto fail;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004632
4633 htx->flags |= HTX_FL_EOM;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004634 }
4635
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004636 leave:
4637 if (reply->type == HTTP_REPLY_LOGFMT)
4638 free_trash_chunk(body);
4639 return ret;
4640
4641 fail_alloc:
4642 if (!(s->flags & SF_ERR_MASK))
4643 s->flags |= SF_ERR_RESOURCE;
Christopher Faulet97e466c2020-05-15 15:12:47 +02004644 /* fall through */
4645 fail:
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004646 ret = -1;
4647 goto leave;
Christopher Faulet97e466c2020-05-15 15:12:47 +02004648}
4649
4650/* Send an http reply to the client. On success, it returns 0. If an error
Christopher Faulet40e6b552020-06-25 16:04:50 +02004651 * occurs -1 is returned and the response channel is truncated, removing this
4652 * way the faulty reply. This function may fail when the reply is formatted
4653 * (http_reply_to_htx) or when the reply is forwarded
4654 * (http_forward_proxy_resp). On the last case, it is because a
4655 * http-after-response rule fails.
Christopher Faulet97e466c2020-05-15 15:12:47 +02004656 */
4657int http_reply_message(struct stream *s, struct http_reply *reply)
4658{
4659 struct channel *res = &s->res;
4660 struct htx *htx = htx_from_buf(&res->buf);
4661
4662 if (s->txn->status == -1)
4663 s->txn->status = reply->status;
4664 channel_htx_truncate(res, htx);
4665
4666 if (http_reply_to_htx(s, htx, reply) == -1)
4667 goto fail;
4668
4669 htx_to_buf(htx, &s->res.buf);
4670 if (!http_forward_proxy_resp(s, 1))
4671 goto fail;
4672 return 0;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004673
4674 fail:
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004675 channel_htx_truncate(res, htx);
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004676 if (!(s->flags & SF_ERR_MASK))
4677 s->flags |= SF_ERR_PRXCOND;
Christopher Faulet97e466c2020-05-15 15:12:47 +02004678 return -1;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004679}
4680
Christopher Faulet50264b42022-03-30 19:39:30 +02004681/* Return the error message corresponding to s->conn_err_type. It is assumed
Christopher Faulet304cc402019-07-15 15:46:28 +02004682 * that the server side is closed. Note that err_type is actually a
4683 * bitmask, where almost only aborts may be cumulated with other
4684 * values. We consider that aborted operations are more important
4685 * than timeouts or errors due to the fact that nobody else in the
4686 * logs might explain incomplete retries. All others should avoid
4687 * being cumulated. It should normally not be possible to have multiple
4688 * aborts at once, but just in case, the first one in sequence is reported.
4689 * Note that connection errors appearing on the second request of a keep-alive
4690 * connection are not reported since this allows the client to retry.
4691 */
Willy Tarreaub49672d2022-05-27 10:13:37 +02004692void http_return_srv_error(struct stream *s, struct stconn *sc)
Christopher Faulet304cc402019-07-15 15:46:28 +02004693{
Christopher Faulet50264b42022-03-30 19:39:30 +02004694 int err_type = s->conn_err_type;
Christopher Faulet304cc402019-07-15 15:46:28 +02004695
4696 /* set s->txn->status for http_error_message(s) */
Christopher Faulet50264b42022-03-30 19:39:30 +02004697 if (err_type & STRM_ET_QUEUE_ABRT) {
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004698 s->txn->status = -1;
Willy Tarreaub49672d2022-05-27 10:13:37 +02004699 http_server_error(s, sc, SF_ERR_CLICL, SF_FINST_Q, NULL);
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004700 }
Christopher Faulet50264b42022-03-30 19:39:30 +02004701 else if (err_type & STRM_ET_CONN_ABRT) {
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004702 s->txn->status = -1;
Willy Tarreaub49672d2022-05-27 10:13:37 +02004703 http_server_error(s, sc, SF_ERR_CLICL, SF_FINST_C, NULL);
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004704 }
Christopher Faulet50264b42022-03-30 19:39:30 +02004705 else if (err_type & STRM_ET_QUEUE_TO) {
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004706 s->txn->status = 503;
Willy Tarreaub49672d2022-05-27 10:13:37 +02004707 http_server_error(s, sc, SF_ERR_SRVTO, SF_FINST_Q,
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004708 http_error_message(s));
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004709 }
Christopher Faulet50264b42022-03-30 19:39:30 +02004710 else if (err_type & STRM_ET_QUEUE_ERR) {
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004711 s->txn->status = 503;
Willy Tarreaub49672d2022-05-27 10:13:37 +02004712 http_server_error(s, sc, SF_ERR_SRVCL, SF_FINST_Q,
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004713 http_error_message(s));
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004714 }
Christopher Faulet50264b42022-03-30 19:39:30 +02004715 else if (err_type & STRM_ET_CONN_TO) {
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004716 s->txn->status = 503;
Willy Tarreaub49672d2022-05-27 10:13:37 +02004717 http_server_error(s, sc, SF_ERR_SRVTO, SF_FINST_C,
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004718 (s->txn->flags & TX_NOT_FIRST) ? NULL :
4719 http_error_message(s));
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004720 }
Christopher Faulet50264b42022-03-30 19:39:30 +02004721 else if (err_type & STRM_ET_CONN_ERR) {
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004722 s->txn->status = 503;
Willy Tarreaub49672d2022-05-27 10:13:37 +02004723 http_server_error(s, sc, SF_ERR_SRVCL, SF_FINST_C,
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004724 (s->flags & SF_SRV_REUSED) ? NULL :
4725 http_error_message(s));
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004726 }
Christopher Faulet50264b42022-03-30 19:39:30 +02004727 else if (err_type & STRM_ET_CONN_RES) {
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004728 s->txn->status = 503;
Willy Tarreaub49672d2022-05-27 10:13:37 +02004729 http_server_error(s, sc, SF_ERR_RESOURCE, SF_FINST_C,
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004730 (s->txn->flags & TX_NOT_FIRST) ? NULL :
4731 http_error_message(s));
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004732 }
Christopher Faulet50264b42022-03-30 19:39:30 +02004733 else { /* STRM_ET_CONN_OTHER and others */
Christopher Faulet304cc402019-07-15 15:46:28 +02004734 s->txn->status = 500;
Willy Tarreaub49672d2022-05-27 10:13:37 +02004735 http_server_error(s, sc, SF_ERR_INTERNAL, SF_FINST_C,
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004736 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004737 }
4738}
4739
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004740
Christopher Faulet4a28a532019-03-01 11:19:40 +01004741/* Handle Expect: 100-continue for HTTP/1.1 messages if necessary. It returns 0
4742 * on success and -1 on error.
4743 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004744static int http_handle_expect_hdr(struct stream *s, struct htx *htx, struct http_msg *msg)
Christopher Faulet4a28a532019-03-01 11:19:40 +01004745{
4746 /* If we have HTTP/1.1 message with a body and Expect: 100-continue,
4747 * then we must send an HTTP/1.1 100 Continue intermediate response.
4748 */
Christopher Fauletffcffa82023-04-05 10:33:31 +02004749 if (!(msg->flags & HTTP_MSGF_EXPECT_CHECKED) &&
4750 (msg->flags & HTTP_MSGF_VER_11) &&
Christopher Faulet4a28a532019-03-01 11:19:40 +01004751 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
4752 struct ist hdr = { .ptr = "Expect", .len = 6 };
4753 struct http_hdr_ctx ctx;
4754
4755 ctx.blk = NULL;
4756 /* Expect is allowed in 1.1, look for it */
4757 if (http_find_header(htx, hdr, &ctx, 0) &&
4758 unlikely(isteqi(ctx.value, ist2("100-continue", 12)))) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004759 if (http_reply_100_continue(s) == -1)
Christopher Faulet4a28a532019-03-01 11:19:40 +01004760 return -1;
4761 http_remove_header(htx, &ctx);
4762 }
4763 }
Christopher Fauletffcffa82023-04-05 10:33:31 +02004764 msg->flags |= HTTP_MSGF_EXPECT_CHECKED;
Christopher Faulet4a28a532019-03-01 11:19:40 +01004765 return 0;
4766}
4767
Christopher Faulet23a3c792018-11-28 10:01:23 +01004768/* Send a 100-Continue response to the client. It returns 0 on success and -1
4769 * on error. The response channel is updated accordingly.
4770 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004771static int http_reply_100_continue(struct stream *s)
Christopher Faulet23a3c792018-11-28 10:01:23 +01004772{
4773 struct channel *res = &s->res;
4774 struct htx *htx = htx_from_buf(&res->buf);
4775 struct htx_sl *sl;
4776 unsigned int flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|
4777 HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
Christopher Faulet23a3c792018-11-28 10:01:23 +01004778
4779 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
4780 ist("HTTP/1.1"), ist("100"), ist("Continue"));
4781 if (!sl)
4782 goto fail;
4783 sl->info.res.status = 100;
4784
Christopher Faulet1d5ec092019-06-26 14:23:54 +02004785 if (!htx_add_endof(htx, HTX_BLK_EOH))
Christopher Faulet23a3c792018-11-28 10:01:23 +01004786 goto fail;
4787
Christopher Fauleta72a7e42020-01-28 09:28:11 +01004788 if (!http_forward_proxy_resp(s, 0))
4789 goto fail;
Christopher Faulet23a3c792018-11-28 10:01:23 +01004790 return 0;
4791
4792 fail:
4793 /* If an error occurred, remove the incomplete HTTP response from the
4794 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004795 channel_htx_truncate(res, htx);
Christopher Faulet23a3c792018-11-28 10:01:23 +01004796 return -1;
4797}
4798
Christopher Faulet12c51e22018-11-28 15:59:42 +01004799
Christopher Faulet0f226952018-10-22 09:29:56 +02004800/*
4801 * Capture headers from message <htx> according to header list <cap_hdr>, and
4802 * fill the <cap> pointers appropriately.
4803 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004804static void http_capture_headers(struct htx *htx, char **cap, struct cap_hdr *cap_hdr)
Christopher Faulet0f226952018-10-22 09:29:56 +02004805{
4806 struct cap_hdr *h;
4807 int32_t pos;
4808
Christopher Fauleta3f15502019-05-13 15:27:23 +02004809 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet0f226952018-10-22 09:29:56 +02004810 struct htx_blk *blk = htx_get_blk(htx, pos);
4811 enum htx_blk_type type = htx_get_blk_type(blk);
4812 struct ist n, v;
4813
4814 if (type == HTX_BLK_EOH)
4815 break;
4816 if (type != HTX_BLK_HDR)
4817 continue;
4818
4819 n = htx_get_blk_name(htx, blk);
4820
4821 for (h = cap_hdr; h; h = h->next) {
4822 if (h->namelen && (h->namelen == n.len) &&
4823 (strncasecmp(n.ptr, h->name, h->namelen) == 0)) {
4824 if (cap[h->index] == NULL)
4825 cap[h->index] =
4826 pool_alloc(h->pool);
4827
4828 if (cap[h->index] == NULL) {
4829 ha_alert("HTTP capture : out of memory.\n");
4830 break;
4831 }
4832
4833 v = htx_get_blk_value(htx, blk);
Tim Duesterhus2471f5c2021-11-08 09:05:01 +01004834 v = isttrim(v, h->len);
Christopher Faulet0f226952018-10-22 09:29:56 +02004835
4836 memcpy(cap[h->index], v.ptr, v.len);
4837 cap[h->index][v.len]=0;
4838 }
4839 }
4840 }
4841}
4842
Christopher Faulet0b6bdc52018-10-24 11:05:36 +02004843/* Delete a value in a header between delimiters <from> and <next>. The header
4844 * itself is delimited by <start> and <end> pointers. The number of characters
4845 * displaced is returned, and the pointer to the first delimiter is updated if
4846 * required. The function tries as much as possible to respect the following
4847 * principles :
4848 * - replace <from> delimiter by the <next> one unless <from> points to <start>,
4849 * in which case <next> is simply removed
4850 * - set exactly one space character after the new first delimiter, unless there
4851 * are not enough characters in the block being moved to do so.
4852 * - remove unneeded spaces before the previous delimiter and after the new
4853 * one.
4854 *
4855 * It is the caller's responsibility to ensure that :
4856 * - <from> points to a valid delimiter or <start> ;
4857 * - <next> points to a valid delimiter or <end> ;
4858 * - there are non-space chars before <from>.
4859 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004860static int http_del_hdr_value(char *start, char *end, char **from, char *next)
Christopher Faulet0b6bdc52018-10-24 11:05:36 +02004861{
4862 char *prev = *from;
4863
4864 if (prev == start) {
4865 /* We're removing the first value. eat the semicolon, if <next>
4866 * is lower than <end> */
4867 if (next < end)
4868 next++;
4869
4870 while (next < end && HTTP_IS_SPHT(*next))
4871 next++;
4872 }
4873 else {
4874 /* Remove useless spaces before the old delimiter. */
4875 while (HTTP_IS_SPHT(*(prev-1)))
4876 prev--;
4877 *from = prev;
4878
4879 /* copy the delimiter and if possible a space if we're
4880 * not at the end of the line.
4881 */
4882 if (next < end) {
4883 *prev++ = *next++;
4884 if (prev + 1 < next)
4885 *prev++ = ' ';
4886 while (next < end && HTTP_IS_SPHT(*next))
4887 next++;
4888 }
4889 }
4890 memmove(prev, next, end - next);
4891 return (prev - next);
4892}
4893
Christopher Faulet0f226952018-10-22 09:29:56 +02004894
4895/* Formats the start line of the request (without CRLF) and puts it in <str> and
Joseph Herlantc42c0e92018-11-25 10:43:27 -08004896 * return the written length. The line can be truncated if it exceeds <len>.
Christopher Faulet0f226952018-10-22 09:29:56 +02004897 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004898static size_t http_fmt_req_line(const struct htx_sl *sl, char *str, size_t len)
Christopher Faulet0f226952018-10-22 09:29:56 +02004899{
4900 struct ist dst = ist2(str, 0);
4901
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004902 if (istcat(&dst, htx_sl_req_meth(sl), len) == -1)
Christopher Faulet0f226952018-10-22 09:29:56 +02004903 goto end;
4904 if (dst.len + 1 > len)
4905 goto end;
4906 dst.ptr[dst.len++] = ' ';
4907
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004908 if (istcat(&dst, htx_sl_req_uri(sl), len) == -1)
Christopher Faulet0f226952018-10-22 09:29:56 +02004909 goto end;
4910 if (dst.len + 1 > len)
4911 goto end;
4912 dst.ptr[dst.len++] = ' ';
4913
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004914 istcat(&dst, htx_sl_req_vsn(sl), len);
Christopher Faulet0f226952018-10-22 09:29:56 +02004915 end:
4916 return dst.len;
4917}
4918
4919/*
4920 * Print a debug line with a start line.
4921 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004922static void http_debug_stline(const char *dir, struct stream *s, const struct htx_sl *sl)
Christopher Faulet0f226952018-10-22 09:29:56 +02004923{
4924 struct session *sess = strm_sess(s);
4925 int max;
4926
4927 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
4928 dir,
Willy Tarreau88bc8002021-12-06 07:01:02 +00004929 objt_conn(sess->origin) ? (unsigned short)__objt_conn(sess->origin)->handle.fd : -1,
Willy Tarreaufd9417b2022-05-18 16:23:22 +02004930 sc_conn(s->scb) ? (unsigned short)(__sc_conn(s->scb))->handle.fd : -1);
Christopher Faulet0f226952018-10-22 09:29:56 +02004931
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004932 max = HTX_SL_P1_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02004933 UBOUND(max, trash.size - trash.data - 3);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004934 chunk_memcat(&trash, HTX_SL_P1_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02004935 trash.area[trash.data++] = ' ';
4936
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004937 max = HTX_SL_P2_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02004938 UBOUND(max, trash.size - trash.data - 2);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004939 chunk_memcat(&trash, HTX_SL_P2_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02004940 trash.area[trash.data++] = ' ';
4941
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004942 max = HTX_SL_P3_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02004943 UBOUND(max, trash.size - trash.data - 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004944 chunk_memcat(&trash, HTX_SL_P3_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02004945 trash.area[trash.data++] = '\n';
4946
Willy Tarreau2e8ab6b2020-03-14 11:03:20 +01004947 DISGUISE(write(1, trash.area, trash.data));
Christopher Faulet0f226952018-10-22 09:29:56 +02004948}
4949
4950/*
4951 * Print a debug line with a header.
4952 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004953static 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 +02004954{
4955 struct session *sess = strm_sess(s);
4956 int max;
4957
4958 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
4959 dir,
Willy Tarreau88bc8002021-12-06 07:01:02 +00004960 objt_conn(sess->origin) ? (unsigned short)__objt_conn(sess->origin)->handle.fd : -1,
Willy Tarreaufd9417b2022-05-18 16:23:22 +02004961 sc_conn(s->scb) ? (unsigned short)(__sc_conn(s->scb))->handle.fd : -1);
Christopher Faulet0f226952018-10-22 09:29:56 +02004962
4963 max = n.len;
4964 UBOUND(max, trash.size - trash.data - 3);
4965 chunk_memcat(&trash, n.ptr, max);
4966 trash.area[trash.data++] = ':';
4967 trash.area[trash.data++] = ' ';
4968
4969 max = v.len;
4970 UBOUND(max, trash.size - trash.data - 1);
4971 chunk_memcat(&trash, v.ptr, max);
4972 trash.area[trash.data++] = '\n';
4973
Willy Tarreau2e8ab6b2020-03-14 11:03:20 +01004974 DISGUISE(write(1, trash.area, trash.data));
Christopher Faulet0f226952018-10-22 09:29:56 +02004975}
4976
Christopher Fauleta8a46e22019-07-16 14:53:09 +02004977void http_txn_reset_req(struct http_txn *txn)
4978{
Christopher Faulet1aea50e2020-01-17 16:03:53 +01004979 txn->req.flags = 0;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02004980 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
4981}
4982
4983void http_txn_reset_res(struct http_txn *txn)
4984{
Christopher Faulet1aea50e2020-01-17 16:03:53 +01004985 txn->rsp.flags = 0;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02004986 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
4987}
4988
4989/*
Christopher Faulet75f619a2021-03-08 19:12:58 +01004990 * Create and initialize a new HTTP transaction for stream <s>. This should be
4991 * used before processing any new request. It returns the transaction or NLULL
4992 * on error.
Christopher Fauleta8a46e22019-07-16 14:53:09 +02004993 */
Christopher Faulet75f619a2021-03-08 19:12:58 +01004994struct http_txn *http_create_txn(struct stream *s)
Christopher Fauleta8a46e22019-07-16 14:53:09 +02004995{
Christopher Faulet75f619a2021-03-08 19:12:58 +01004996 struct http_txn *txn;
Willy Tarreaub49672d2022-05-27 10:13:37 +02004997 struct stconn *sc = s->scf;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02004998
Christopher Faulet75f619a2021-03-08 19:12:58 +01004999 txn = pool_alloc(pool_head_http_txn);
5000 if (!txn)
5001 return NULL;
5002 s->txn = txn;
5003
Christopher Faulet5eb67f52022-06-22 17:12:05 +02005004 txn->meth = HTTP_METH_OTHER;
Willy Tarreaub49672d2022-05-27 10:13:37 +02005005 txn->flags = ((sc && sc_ep_test(sc, SE_FL_NOT_FIRST)) ? TX_NOT_FIRST : 0);
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005006 txn->status = -1;
Christopher Faulet5cb513a2020-05-13 17:56:56 +02005007 txn->http_reply = NULL;
Christopher Faulete05bf9e2022-03-29 15:23:40 +02005008 txn->l7_buffer = BUF_NULL;
Willy Tarreau8b507582020-02-25 09:35:07 +01005009 write_u32(txn->cache_hash, 0);
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005010
5011 txn->cookie_first_date = 0;
5012 txn->cookie_last_date = 0;
5013
5014 txn->srv_cookie = NULL;
5015 txn->cli_cookie = NULL;
5016 txn->uri = NULL;
5017
5018 http_txn_reset_req(txn);
5019 http_txn_reset_res(txn);
5020
5021 txn->req.chn = &s->req;
5022 txn->rsp.chn = &s->res;
5023
5024 txn->auth.method = HTTP_AUTH_UNKNOWN;
5025
Aurelien DARRAGON5ad2b642022-11-18 09:17:29 +01005026 /* here we don't want to re-initialize s->vars_txn and s->vars_reqres
5027 * variable lists, because they were already initialized upon stream
5028 * creation in stream_new(), and thus may already contain some variables
5029 */
Christopher Faulet75f619a2021-03-08 19:12:58 +01005030
5031 return txn;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005032}
5033
5034/* to be used at the end of a transaction */
Christopher Faulet75f619a2021-03-08 19:12:58 +01005035void http_destroy_txn(struct stream *s)
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005036{
5037 struct http_txn *txn = s->txn;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005038
5039 /* these ones will have been dynamically allocated */
5040 pool_free(pool_head_requri, txn->uri);
5041 pool_free(pool_head_capture, txn->cli_cookie);
5042 pool_free(pool_head_capture, txn->srv_cookie);
Tim Duesterhusa17e6622020-03-05 20:19:02 +01005043 pool_free(pool_head_uniqueid, s->unique_id.ptr);
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005044
Tim Duesterhusa17e6622020-03-05 20:19:02 +01005045 s->unique_id = IST_NULL;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005046 txn->uri = NULL;
5047 txn->srv_cookie = NULL;
5048 txn->cli_cookie = NULL;
5049
Christopher Faulet59399252019-11-07 14:27:52 +01005050 if (!LIST_ISEMPTY(&s->vars_txn.head))
5051 vars_prune(&s->vars_txn, s->sess, s);
5052 if (!LIST_ISEMPTY(&s->vars_reqres.head))
5053 vars_prune(&s->vars_reqres, s->sess, s);
Christopher Faulet75f619a2021-03-08 19:12:58 +01005054
Christopher Faulete05bf9e2022-03-29 15:23:40 +02005055 b_free(&txn->l7_buffer);
5056
Christopher Faulet75f619a2021-03-08 19:12:58 +01005057 pool_free(pool_head_http_txn, txn);
5058 s->txn = NULL;
Christopher Faulet59399252019-11-07 14:27:52 +01005059}
5060
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005061
Christopher Faulet71236de2023-01-13 09:06:38 +01005062void http_set_term_flags(struct stream *s)
5063{
5064 if (!(s->flags & SF_ERR_MASK))
5065 s->flags |= SF_ERR_PRXCOND;
5066
5067 if (!(s->flags & SF_FINST_MASK)) {
5068 if (s->scb->state == SC_ST_INI) {
5069 /* Before any connection attempt on the server side, we
5070 * are still in the request analysis. Just take case to
5071 * detect tarpit error
5072 */
5073 if (s->req.analysers & AN_REQ_HTTP_TARPIT)
5074 s->flags |= SF_FINST_T;
5075 else
5076 s->flags |= SF_FINST_R;
5077 }
5078 else if (s->scb->state == SC_ST_QUE)
5079 s->flags |= SF_FINST_Q;
5080 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)) {
5081 if (unlikely(objt_applet(s->target))) {
5082 s->flags |= SF_FINST_R;
5083 }
5084 else
5085 s->flags |= SF_FINST_C;
5086 }
5087 else {
5088 if (s->txn->rsp.msg_state < HTTP_MSG_DATA) {
5089 /* We are still processing the response headers */
5090 s->flags |= SF_FINST_H;
5091 }
Christopher Faulet5b74f992023-01-26 19:02:07 +01005092 // (res == (done|closing|closed)) & (res->flags & shutw)
5093 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 +01005094 (s->flags & (SF_ERR_CLITO|SF_ERR_CLICL))) {
5095 /* A client error was reported and we are
5096 * transmitting the last block of data
5097 */
5098 s->flags |= SF_FINST_L;
5099 }
5100 else {
5101 /* Otherwise we are in DATA phase on both sides */
5102 s->flags |= SF_FINST_D;
5103 }
5104 }
5105 }
5106}
5107
5108
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005109DECLARE_POOL(pool_head_http_txn, "http_txn", sizeof(struct http_txn));
Christopher Faulet0f226952018-10-22 09:29:56 +02005110
Christopher Fauletf4eb75d2018-10-11 15:55:07 +02005111/*
5112 * Local variables:
5113 * c-indent-level: 8
5114 * c-basic-offset: 8
5115 * End:
5116 */