blob: df88d3d97d1f838219aab9f627da37fa3175ce70 [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>
Willy Tarreau16f958c2020-06-03 08:44:35 +020029#include <haproxy/htx.h>
Willy Tarreauaeed4a82020-06-04 22:01:04 +020030#include <haproxy/log.h>
Willy Tarreau6131d6a2020-06-02 16:48:09 +020031#include <haproxy/net_helper.h>
Willy Tarreaua264d962020-06-04 22:29:18 +020032#include <haproxy/proxy.h>
Willy Tarreau7cd8b6e2020-06-02 17:32:26 +020033#include <haproxy/regex.h>
Willy Tarreau5edca2f2022-05-27 09:25:10 +020034#include <haproxy/sc_strm.h>
Willy Tarreau1e56f922020-06-04 23:20:13 +020035#include <haproxy/server-t.h>
Willy Tarreau2eec9b52020-06-04 19:58:55 +020036#include <haproxy/stats.h>
Willy Tarreaucb086c62022-05-27 09:47:12 +020037#include <haproxy/stconn.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020038#include <haproxy/stream.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020039#include <haproxy/trace.h>
Willy Tarreau8c42b8a2020-06-04 19:27:34 +020040#include <haproxy/uri_auth-t.h>
Willy Tarreaua1718922020-06-04 16:25:31 +020041#include <haproxy/vars.h>
Christopher Faulete0768eb2018-10-03 16:38:02 +020042
Christopher Faulete0768eb2018-10-03 16:38:02 +020043
Christopher Fauleteea8fc72019-11-05 16:18:10 +010044#define TRACE_SOURCE &trace_strm
45
Christopher Faulet377c5a52018-10-24 21:21:30 +020046extern const char *stat_status_codes[];
Christopher Fauletf2824e62018-10-01 12:12:37 +020047
Willy Tarreauff882702021-04-10 17:23:00 +020048struct pool_head *pool_head_requri __read_mostly = NULL;
49struct pool_head *pool_head_capture __read_mostly = NULL;
Christopher Fauleta8a46e22019-07-16 14:53:09 +020050
51
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020052static void http_end_request(struct stream *s);
53static void http_end_response(struct stream *s);
Christopher Fauletf2824e62018-10-01 12:12:37 +020054
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020055static void http_capture_headers(struct htx *htx, char **cap, struct cap_hdr *cap_hdr);
56static int http_del_hdr_value(char *start, char *end, char **from, char *next);
57static size_t http_fmt_req_line(const struct htx_sl *sl, char *str, size_t len);
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020058static void http_debug_stline(const char *dir, struct stream *s, const struct htx_sl *sl);
59static 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 +020060
Christopher Fauletd4150ad2021-10-13 15:35:55 +020061static enum rule_result http_req_get_intercept_rule(struct proxy *px, struct list *def_rules, struct list *rules, struct stream *s);
62static 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 +020063static enum rule_result http_req_restrict_header_names(struct stream *s, struct htx *htx, struct proxy *px);
Christopher Faulet3e964192018-10-24 11:39:23 +020064
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020065static void http_manage_client_side_cookies(struct stream *s, struct channel *req);
66static void http_manage_server_side_cookies(struct stream *s, struct channel *res);
Christopher Fauletfcda7c62018-10-24 11:56:22 +020067
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020068static int http_stats_check_uri(struct stream *s, struct http_txn *txn, struct proxy *backend);
69static int http_handle_stats(struct stream *s, struct channel *req);
Christopher Faulet377c5a52018-10-24 21:21:30 +020070
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020071static int http_handle_expect_hdr(struct stream *s, struct htx *htx, struct http_msg *msg);
72static int http_reply_100_continue(struct stream *s);
Christopher Faulet23a3c792018-11-28 10:01:23 +010073
Christopher Faulete0768eb2018-10-03 16:38:02 +020074/* This stream analyser waits for a complete HTTP request. It returns 1 if the
75 * processing can continue on next analysers, or zero if it either needs more
76 * data or wants to immediately abort the request (eg: timeout, error, ...). It
77 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req.analysers
78 * when it has nothing left to do, and may remove any analyser when it wants to
79 * abort.
80 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020081int http_wait_for_request(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +020082{
Christopher Faulet9768c262018-10-22 09:34:31 +020083
Christopher Faulete0768eb2018-10-03 16:38:02 +020084 /*
Christopher Faulet9768c262018-10-22 09:34:31 +020085 * We will analyze a complete HTTP request to check the its syntax.
Christopher Faulete0768eb2018-10-03 16:38:02 +020086 *
Christopher Faulet9768c262018-10-22 09:34:31 +020087 * Once the start line and all headers are received, we may perform a
88 * capture of the error (if any), and we will set a few fields. We also
89 * check for monitor-uri, logging and finally headers capture.
Christopher Faulete0768eb2018-10-03 16:38:02 +020090 */
Christopher Faulete0768eb2018-10-03 16:38:02 +020091 struct session *sess = s->sess;
92 struct http_txn *txn = s->txn;
93 struct http_msg *msg = &txn->req;
Christopher Faulet9768c262018-10-22 09:34:31 +020094 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +010095 struct htx_sl *sl;
Christopher Faulete0768eb2018-10-03 16:38:02 +020096
Christopher Fauleteea8fc72019-11-05 16:18:10 +010097 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +020098
Christopher Fauletda46a0d2021-01-21 17:32:58 +010099 if (unlikely(!IS_HTX_STRM(s))) {
100 /* It is only possible when a TCP stream is upgrade to HTTP.
101 * There is a transition period during which there is no
102 * data. The stream is still in raw mode and SF_IGNORE flag is
103 * still set. When this happens, the new mux is responsible to
Ilya Shipitsinacf84592021-02-06 22:29:08 +0500104 * handle all errors. Thus we may leave immediately.
Christopher Fauletda46a0d2021-01-21 17:32:58 +0100105 */
106 BUG_ON(!(s->flags & SF_IGNORE) || !c_empty(&s->req));
Christopher Faulet9768c262018-10-22 09:34:31 +0200107
Christopher Faulet97b3a612021-03-15 17:10:12 +0100108 /* Don't connect for now */
109 channel_dont_connect(req);
110
111 /* A SHUTR at this stage means we are performing a "destructive"
112 * HTTP upgrade (TCP>H2). In this case, we can leave.
113 */
114 if (req->flags & CF_SHUTR) {
115 s->logs.logwait = 0;
116 s->logs.level = 0;
117 channel_abort(&s->req);
118 channel_abort(&s->res);
119 req->analysers &= AN_REQ_FLT_END;
120 req->analyse_exp = TICK_ETERNITY;
121 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA, s);
122 return 1;
123 }
Christopher Fauletda46a0d2021-01-21 17:32:58 +0100124 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA, s);
125 return 0;
126 }
127
128 htx = htxbuf(&req->buf);
Christopher Faulet8bebd2f2020-10-06 17:54:56 +0200129
Willy Tarreau4236f032019-03-05 10:43:32 +0100130 /* Parsing errors are caught here */
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200131 if (htx->flags & (HTX_FL_PARSING_ERROR|HTX_FL_PROCESSING_ERROR)) {
Willy Tarreau4236f032019-03-05 10:43:32 +0100132 stream_inc_http_req_ctr(s);
Emeric Brun28976442020-10-07 08:50:09 +0200133 proxy_inc_fe_req_ctr(sess->listener, sess->fe);
Christopher Fauletbf7175f2021-02-10 14:58:01 +0100134 if (htx->flags & HTX_FL_PARSING_ERROR) {
135 stream_inc_http_err_ctr(s);
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200136 goto return_bad_req;
Christopher Fauletbf7175f2021-02-10 14:58:01 +0100137 }
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200138 else
139 goto return_int_err;
Willy Tarreau4236f032019-03-05 10:43:32 +0100140 }
141
Christopher Faulete0768eb2018-10-03 16:38:02 +0200142 /* we're speaking HTTP here, so let's speak HTTP to the client */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200143 s->srv_error = http_return_srv_error;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200144
Christopher Faulet9768c262018-10-22 09:34:31 +0200145 msg->msg_state = HTTP_MSG_BODY;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200146 stream_inc_http_req_ctr(s);
Emeric Brun28976442020-10-07 08:50:09 +0200147 proxy_inc_fe_req_ctr(sess->listener, sess->fe); /* one more valid request for this FE */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200148
Christopher Faulet9768c262018-10-22 09:34:31 +0200149 /* kill the pending keep-alive timeout */
Christopher Faulet9768c262018-10-22 09:34:31 +0200150 req->analyse_exp = TICK_ETERNITY;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200151
Christopher Faulet29f17582019-05-23 11:03:26 +0200152 BUG_ON(htx_get_first_type(htx) != HTX_BLK_REQ_SL);
Christopher Faulet297fbb42019-05-13 14:41:27 +0200153 sl = http_get_stline(htx);
Christopher Faulet03599112018-11-27 11:21:21 +0100154
Christopher Faulet9768c262018-10-22 09:34:31 +0200155 /* 0: we might have to print this header in debug mode */
156 if (unlikely((global.mode & MODE_DEBUG) &&
157 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
158 int32_t pos;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200159
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200160 http_debug_stline("clireq", s, sl);
Christopher Faulet9768c262018-10-22 09:34:31 +0200161
Christopher Fauleta3f15502019-05-13 15:27:23 +0200162 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet9768c262018-10-22 09:34:31 +0200163 struct htx_blk *blk = htx_get_blk(htx, pos);
164 enum htx_blk_type type = htx_get_blk_type(blk);
165
166 if (type == HTX_BLK_EOH)
167 break;
168 if (type != HTX_BLK_HDR)
169 continue;
170
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200171 http_debug_hdr("clihdr", s,
172 htx_get_blk_name(htx, blk),
173 htx_get_blk_value(htx, blk));
Christopher Faulet9768c262018-10-22 09:34:31 +0200174 }
175 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200176
177 /*
Christopher Faulet03599112018-11-27 11:21:21 +0100178 * 1: identify the method and the version. Also set HTTP flags
Christopher Faulete0768eb2018-10-03 16:38:02 +0200179 */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100180 txn->meth = sl->info.req.meth;
Christopher Faulet03599112018-11-27 11:21:21 +0100181 if (sl->flags & HTX_SL_F_VER_11)
Christopher Faulet9768c262018-10-22 09:34:31 +0200182 msg->flags |= HTTP_MSGF_VER_11;
Christopher Faulet03599112018-11-27 11:21:21 +0100183 msg->flags |= HTTP_MSGF_XFER_LEN;
Christopher Faulet2a408542020-11-20 14:22:37 +0100184 if (sl->flags & HTX_SL_F_CLEN)
185 msg->flags |= HTTP_MSGF_CNT_LEN;
186 else if (sl->flags & HTX_SL_F_CHNK)
187 msg->flags |= HTTP_MSGF_TE_CHNK;
Christopher Fauletb2db4fa2018-11-27 16:51:09 +0100188 if (sl->flags & HTX_SL_F_BODYLESS)
189 msg->flags |= HTTP_MSGF_BODYLESS;
Christopher Faulet576c3582021-01-08 15:53:01 +0100190 if (sl->flags & HTX_SL_F_CONN_UPG)
191 msg->flags |= HTTP_MSGF_CONN_UPG;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200192
193 /* we can make use of server redirect on GET and HEAD */
194 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
195 s->flags |= SF_REDIRECTABLE;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100196 else if (txn->meth == HTTP_METH_OTHER && isteqi(htx_sl_req_meth(sl), ist("PRI"))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200197 /* PRI is reserved for the HTTP/2 preface */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200198 goto return_bad_req;
199 }
200
201 /*
Christopher Faulet6072beb2020-02-18 15:34:58 +0100202 * 2: check if the URI matches the monitor_uri. We have to do this for
203 * every request which gets in, because the monitor-uri is defined by
204 * the frontend. If the monitor-uri starts with a '/', the matching is
205 * done against the request's path. Otherwise, the request's uri is
206 * used. It is a workaround to let HTTP/2 health-checks work as
207 * expected.
Christopher Faulete0768eb2018-10-03 16:38:02 +0200208 */
Tim Duesterhus4b1fcaa2022-03-05 00:52:40 +0100209 if (unlikely(isttest(sess->fe->monitor_uri))) {
210 const struct ist monitor_uri = sess->fe->monitor_uri;
Amaury Denoyellec453f952021-07-06 11:40:12 +0200211 struct http_uri_parser parser = http_uri_parser_init(htx_sl_req_uri(sl));
Christopher Faulete0768eb2018-10-03 16:38:02 +0200212
Amaury Denoyelle5a9bd372021-07-06 11:23:10 +0200213 if ((istptr(monitor_uri)[0] == '/' &&
Amaury Denoyellec453f952021-07-06 11:40:12 +0200214 isteq(http_parse_path(&parser), monitor_uri)) ||
Amaury Denoyelle5a9bd372021-07-06 11:23:10 +0200215 isteq(htx_sl_req_uri(sl), monitor_uri)) {
216 /*
217 * We have found the monitor URI
218 */
219 struct acl_cond *cond;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200220
Amaury Denoyelle5a9bd372021-07-06 11:23:10 +0200221 s->flags |= SF_MONITOR;
222 _HA_ATOMIC_INC(&sess->fe->fe_counters.intercepted_req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200223
Amaury Denoyelle5a9bd372021-07-06 11:23:10 +0200224 /* Check if we want to fail this monitor request or not */
225 list_for_each_entry(cond, &sess->fe->mon_fail_cond, list) {
226 int ret = acl_exec_cond(cond, sess->fe, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200227
Amaury Denoyelle5a9bd372021-07-06 11:23:10 +0200228 ret = acl_pass(ret);
229 if (cond->pol == ACL_COND_UNLESS)
230 ret = !ret;
231
232 if (ret) {
233 /* we fail this request, let's return 503 service unavail */
234 txn->status = 503;
235 if (!(s->flags & SF_ERR_MASK))
236 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
237 goto return_prx_cond;
238 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200239 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200240
Amaury Denoyelle5a9bd372021-07-06 11:23:10 +0200241 /* nothing to fail, let's reply normally */
242 txn->status = 200;
243 if (!(s->flags & SF_ERR_MASK))
244 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
245 goto return_prx_cond;
246 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200247 }
248
249 /*
250 * 3: Maybe we have to copy the original REQURI for the logs ?
251 * Note: we cannot log anymore if the request has been
252 * classified as invalid.
253 */
254 if (unlikely(s->logs.logwait & LW_REQ)) {
255 /* we have a complete HTTP request that we must log */
256 if ((txn->uri = pool_alloc(pool_head_requri)) != NULL) {
Christopher Faulet9768c262018-10-22 09:34:31 +0200257 size_t len;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200258
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200259 len = http_fmt_req_line(sl, txn->uri, global.tune.requri_len - 1);
Christopher Faulet9768c262018-10-22 09:34:31 +0200260 txn->uri[len] = 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200261
262 if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT)))
263 s->do_log(s);
264 } else {
265 ha_alert("HTTP logging : out of memory.\n");
266 }
267 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200268
Christopher Faulete0768eb2018-10-03 16:38:02 +0200269 /* if the frontend has "option http-use-proxy-header", we'll check if
270 * we have what looks like a proxied connection instead of a connection,
271 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
272 * Note that this is *not* RFC-compliant, however browsers and proxies
273 * happen to do that despite being non-standard :-(
274 * We consider that a request not beginning with either '/' or '*' is
275 * a proxied connection, which covers both "scheme://location" and
276 * CONNECT ip:port.
277 */
278 if ((sess->fe->options2 & PR_O2_USE_PXHDR) &&
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100279 *HTX_SL_REQ_UPTR(sl) != '/' && *HTX_SL_REQ_UPTR(sl) != '*')
Christopher Faulete0768eb2018-10-03 16:38:02 +0200280 txn->flags |= TX_USE_PX_CONN;
281
Christopher Faulete0768eb2018-10-03 16:38:02 +0200282 /* 5: we may need to capture headers */
283 if (unlikely((s->logs.logwait & LW_REQHDR) && s->req_cap))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200284 http_capture_headers(htx, s->req_cap, sess->fe->req_cap);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200285
Christopher Faulete0768eb2018-10-03 16:38:02 +0200286 /* we may have to wait for the request's body */
Christopher Faulet9768c262018-10-22 09:34:31 +0200287 if (s->be->options & PR_O_WREQ_BODY)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200288 req->analysers |= AN_REQ_HTTP_BODY;
289
290 /*
291 * RFC7234#4:
292 * A cache MUST write through requests with methods
293 * that are unsafe (Section 4.2.1 of [RFC7231]) to
294 * the origin server; i.e., a cache is not allowed
295 * to generate a reply to such a request before
296 * having forwarded the request and having received
297 * a corresponding response.
298 *
299 * RFC7231#4.2.1:
300 * Of the request methods defined by this
301 * specification, the GET, HEAD, OPTIONS, and TRACE
302 * methods are defined to be safe.
303 */
304 if (likely(txn->meth == HTTP_METH_GET ||
305 txn->meth == HTTP_METH_HEAD ||
306 txn->meth == HTTP_METH_OPTIONS ||
307 txn->meth == HTTP_METH_TRACE))
308 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
309
310 /* end of job, return OK */
311 req->analysers &= ~an_bit;
312 req->analyse_exp = TICK_ETERNITY;
Christopher Faulet9768c262018-10-22 09:34:31 +0200313
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100314 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200315 return 1;
316
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200317 return_int_err:
318 txn->status = 500;
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200319 if (!(s->flags & SF_ERR_MASK))
320 s->flags |= SF_ERR_INTERNAL;
Willy Tarreau4781b152021-04-06 13:53:36 +0200321 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
William Lallemand36119de2021-03-08 15:26:48 +0100322 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200323 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200324 goto return_prx_cond;
325
Christopher Faulete0768eb2018-10-03 16:38:02 +0200326 return_bad_req:
Christopher Faulet9768c262018-10-22 09:34:31 +0200327 txn->status = 400;
Willy Tarreau4781b152021-04-06 13:53:36 +0200328 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req);
William Lallemand36119de2021-03-08 15:26:48 +0100329 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200330 _HA_ATOMIC_INC(&sess->listener->counters->failed_req);
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200331 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200332
333 return_prx_cond:
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200334 http_reply_and_close(s, txn->status, http_error_message(s));
335
Christopher Faulete0768eb2018-10-03 16:38:02 +0200336 if (!(s->flags & SF_ERR_MASK))
337 s->flags |= SF_ERR_PRXCOND;
338 if (!(s->flags & SF_FINST_MASK))
339 s->flags |= SF_FINST_R;
340
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100341 DBG_TRACE_DEVEL("leaving on error",
342 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200343 return 0;
344}
345
346
347/* This stream analyser runs all HTTP request processing which is common to
348 * frontends and backends, which means blocking ACLs, filters, connection-close,
349 * reqadd, stats and redirects. This is performed for the designated proxy.
350 * It returns 1 if the processing can continue on next analysers, or zero if it
351 * either needs more data or wants to immediately abort the request (eg: deny,
352 * error, ...).
353 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200354int http_process_req_common(struct stream *s, struct channel *req, int an_bit, struct proxy *px)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200355{
Christopher Fauletd4150ad2021-10-13 15:35:55 +0200356 struct list *def_rules, *rules;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200357 struct session *sess = s->sess;
358 struct http_txn *txn = s->txn;
359 struct http_msg *msg = &txn->req;
Christopher Fauletff2759f2018-10-24 11:13:16 +0200360 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200361 struct redirect_rule *rule;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200362 enum rule_result verdict;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200363 struct connection *conn = objt_conn(sess->origin);
364
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100365 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200366
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100367 htx = htxbuf(&req->buf);
Christopher Fauletff2759f2018-10-24 11:13:16 +0200368
Christopher Faulet1907ccc2019-04-29 13:12:02 +0200369 /* just in case we have some per-backend tracking. Only called the first
370 * execution of the analyser. */
Christopher Fauletd4150ad2021-10-13 15:35:55 +0200371 if (!s->current_rule && !s->current_rule_list)
Christopher Faulet1907ccc2019-04-29 13:12:02 +0200372 stream_inc_be_http_req_ctr(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200373
Christopher Fauletd4150ad2021-10-13 15:35:55 +0200374 def_rules = ((px->defpx && (an_bit == AN_REQ_HTTP_PROCESS_FE || px != sess->fe)) ? &px->defpx->http_req_rules : NULL);
375 rules = &px->http_req_rules;
376
Christopher Faulete0768eb2018-10-03 16:38:02 +0200377 /* evaluate http-request rules */
Christopher Fauletd4150ad2021-10-13 15:35:55 +0200378 if ((def_rules && !LIST_ISEMPTY(def_rules)) || !LIST_ISEMPTY(rules)) {
379 verdict = http_req_get_intercept_rule(px, def_rules, rules, s);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200380
381 switch (verdict) {
382 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
383 goto return_prx_yield;
384
385 case HTTP_RULE_RES_CONT:
386 case HTTP_RULE_RES_STOP: /* nothing to do */
387 break;
388
389 case HTTP_RULE_RES_DENY: /* deny or tarpit */
390 if (txn->flags & TX_CLTARPIT)
391 goto tarpit;
392 goto deny;
393
394 case HTTP_RULE_RES_ABRT: /* abort request, response already sent. Eg: auth */
395 goto return_prx_cond;
396
397 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
398 goto done;
399
400 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
401 goto return_bad_req;
Christopher Faulet3a26bee2019-12-16 12:47:40 +0100402
403 case HTTP_RULE_RES_ERROR: /* failed with a bad request */
404 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200405 }
406 }
407
Christopher Faulet18c13d32022-05-16 11:43:10 +0200408 if (px->options2 & (PR_O2_RSTRICT_REQ_HDR_NAMES_BLK|PR_O2_RSTRICT_REQ_HDR_NAMES_DEL)) {
409 verdict = http_req_restrict_header_names(s, htx, px);
410 if (verdict == HTTP_RULE_RES_DENY)
411 goto deny;
412 }
413
Christopher Faulete0768eb2018-10-03 16:38:02 +0200414 if (conn && (conn->flags & CO_FL_EARLY_DATA) &&
Olivier Houchard220a26c2020-01-23 14:57:36 +0100415 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_SSL_WAIT_HS))) {
Christopher Fauletff2759f2018-10-24 11:13:16 +0200416 struct http_hdr_ctx ctx;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200417
Christopher Fauletff2759f2018-10-24 11:13:16 +0200418 ctx.blk = NULL;
419 if (!http_find_header(htx, ist("Early-Data"), &ctx, 0)) {
420 if (unlikely(!http_add_header(htx, ist("Early-Data"), ist("1"))))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100421 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200422 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200423 }
424
425 /* OK at this stage, we know that the request was accepted according to
426 * the http-request rules, we can check for the stats. Note that the
427 * URI is detected *before* the req* rules in order not to be affected
428 * by a possible reqrep, while they are processed *after* so that a
429 * reqdeny can still block them. This clearly needs to change in 1.6!
430 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200431 if (!s->target && http_stats_check_uri(s, txn, px)) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200432 s->target = &http_stats_applet.obj_type;
Willy Tarreaua0b58b52022-05-27 08:33:53 +0200433 if (unlikely(!sc_applet_create(s->scb, objt_applet(s->target)))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200434 s->logs.tv_request = now;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200435 if (!(s->flags & SF_ERR_MASK))
436 s->flags |= SF_ERR_RESOURCE;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100437 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200438 }
439
440 /* parse the whole stats request and extract the relevant information */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200441 http_handle_stats(s, req);
Christopher Fauletd4150ad2021-10-13 15:35:55 +0200442 verdict = http_req_get_intercept_rule(px, NULL, &px->uri_auth->http_req_rules, s);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200443 /* not all actions implemented: deny, allow, auth */
444
445 if (verdict == HTTP_RULE_RES_DENY) /* stats http-request deny */
446 goto deny;
447
448 if (verdict == HTTP_RULE_RES_ABRT) /* stats auth / stats http-request auth */
449 goto return_prx_cond;
Christopher Faulet3a26bee2019-12-16 12:47:40 +0100450
451 if (verdict == HTTP_RULE_RES_BADREQ) /* failed with a bad request */
452 goto return_bad_req;
453
454 if (verdict == HTTP_RULE_RES_ERROR) /* failed with a bad request */
455 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200456 }
457
Christopher Faulet2571bc62019-03-01 11:44:26 +0100458 /* Proceed with the applets now. */
459 if (unlikely(objt_applet(s->target))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200460 if (sess->fe == s->be) /* report it if the request was intercepted by the frontend */
Willy Tarreau4781b152021-04-06 13:53:36 +0200461 _HA_ATOMIC_INC(&sess->fe->fe_counters.intercepted_req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200462
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200463 if (http_handle_expect_hdr(s, htx, msg) == -1)
Christopher Fauletb8a53712019-12-16 11:29:38 +0100464 goto return_int_err;
Christopher Fauletbcf242a2019-03-01 11:36:26 +0100465
Christopher Faulete0768eb2018-10-03 16:38:02 +0200466 if (!(s->flags & SF_ERR_MASK)) // this is not really an error but it is
467 s->flags |= SF_ERR_LOCAL; // to mark that it comes from the proxy
468 if (!(s->flags & SF_FINST_MASK))
469 s->flags |= SF_FINST_R;
470
Christopher Fauletc2ac5e42021-03-08 18:20:09 +0100471 if (HAS_FILTERS(s))
472 req->analysers |= AN_REQ_FLT_HTTP_HDRS;
473
Christopher Faulete0768eb2018-10-03 16:38:02 +0200474 /* enable the minimally required analyzers to handle keep-alive and compression on the HTTP response */
475 req->analysers &= (AN_REQ_HTTP_BODY | AN_REQ_FLT_HTTP_HDRS | AN_REQ_FLT_END);
476 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
477 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Christopher Fauletbcf242a2019-03-01 11:36:26 +0100478
479 req->flags |= CF_SEND_DONTWAIT;
480 s->flags |= SF_ASSIGNED;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200481 goto done;
482 }
483
484 /* check whether we have some ACLs set to redirect this request */
485 list_for_each_entry(rule, &px->redirect_rules, list) {
486 if (rule->cond) {
487 int ret;
488
489 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
490 ret = acl_pass(ret);
491 if (rule->cond->pol == ACL_COND_UNLESS)
492 ret = !ret;
493 if (!ret)
494 continue;
495 }
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200496 if (!http_apply_redirect_rule(rule, s, txn))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100497 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200498 goto done;
499 }
500
501 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
502 * If this happens, then the data will not come immediately, so we must
503 * send all what we have without waiting. Note that due to the small gain
504 * in waiting for the body of the request, it's easier to simply put the
505 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
506 * itself once used.
507 */
508 req->flags |= CF_SEND_DONTWAIT;
509
510 done: /* done with this analyser, continue with next ones that the calling
511 * points will have set, if any.
512 */
513 req->analyse_exp = TICK_ETERNITY;
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +0500514 done_without_exp: /* done with this analyser, but don't reset the analyse_exp. */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200515 req->analysers &= ~an_bit;
Christopher Fauletd4150ad2021-10-13 15:35:55 +0200516 s->current_rule = s->current_rule_list = NULL;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100517 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200518 return 1;
519
520 tarpit:
521 /* Allow cookie logging
522 */
523 if (s->be->cookie_name || sess->fe->capture_name)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200524 http_manage_client_side_cookies(s, req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200525
526 /* When a connection is tarpitted, we use the tarpit timeout,
527 * which may be the same as the connect timeout if unspecified.
528 * If unset, then set it to zero because we really want it to
529 * eventually expire. We build the tarpit as an analyser.
530 */
Christopher Faulet202c6ce2019-01-07 14:57:35 +0100531 channel_htx_erase(&s->req, htx);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200532
533 /* wipe the request out so that we can drop the connection early
534 * if the client closes first.
535 */
536 channel_dont_connect(req);
537
Christopher Faulete0768eb2018-10-03 16:38:02 +0200538 req->analysers &= AN_REQ_FLT_END; /* remove switching rules etc... */
539 req->analysers |= AN_REQ_HTTP_TARPIT;
540 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
541 if (!req->analyse_exp)
542 req->analyse_exp = tick_add(now_ms, 0);
543 stream_inc_http_err_ctr(s);
Willy Tarreau4781b152021-04-06 13:53:36 +0200544 _HA_ATOMIC_INC(&sess->fe->fe_counters.denied_req);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100545 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau4781b152021-04-06 13:53:36 +0200546 _HA_ATOMIC_INC(&s->be->be_counters.denied_req);
William Lallemand36119de2021-03-08 15:26:48 +0100547 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200548 _HA_ATOMIC_INC(&sess->listener->counters->denied_req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200549 goto done_without_exp;
550
551 deny: /* this request was blocked (denied) */
552
553 /* Allow cookie logging
554 */
555 if (s->be->cookie_name || sess->fe->capture_name)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200556 http_manage_client_side_cookies(s, req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200557
Christopher Faulete0768eb2018-10-03 16:38:02 +0200558 s->logs.tv_request = now;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200559 stream_inc_http_err_ctr(s);
Willy Tarreau4781b152021-04-06 13:53:36 +0200560 _HA_ATOMIC_INC(&sess->fe->fe_counters.denied_req);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100561 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau4781b152021-04-06 13:53:36 +0200562 _HA_ATOMIC_INC(&s->be->be_counters.denied_req);
William Lallemand36119de2021-03-08 15:26:48 +0100563 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200564 _HA_ATOMIC_INC(&sess->listener->counters->denied_req);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100565 goto return_prx_err;
566
567 return_int_err:
568 txn->status = 500;
569 if (!(s->flags & SF_ERR_MASK))
570 s->flags |= SF_ERR_INTERNAL;
Willy Tarreau4781b152021-04-06 13:53:36 +0200571 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100572 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau4781b152021-04-06 13:53:36 +0200573 _HA_ATOMIC_INC(&s->be->be_counters.internal_errors);
William Lallemand36119de2021-03-08 15:26:48 +0100574 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200575 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100576 goto return_prx_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200577
578 return_bad_req:
Christopher Faulete0768eb2018-10-03 16:38:02 +0200579 txn->status = 400;
Willy Tarreau4781b152021-04-06 13:53:36 +0200580 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req);
William Lallemand36119de2021-03-08 15:26:48 +0100581 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200582 _HA_ATOMIC_INC(&sess->listener->counters->failed_req);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100583 /* fall through */
584
585 return_prx_err:
586 http_reply_and_close(s, txn->status, http_error_message(s));
587 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200588
589 return_prx_cond:
590 if (!(s->flags & SF_ERR_MASK))
591 s->flags |= SF_ERR_PRXCOND;
592 if (!(s->flags & SF_FINST_MASK))
593 s->flags |= SF_FINST_R;
594
595 req->analysers &= AN_REQ_FLT_END;
596 req->analyse_exp = TICK_ETERNITY;
Christopher Fauletd4150ad2021-10-13 15:35:55 +0200597 s->current_rule = s->current_rule_list = NULL;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100598 DBG_TRACE_DEVEL("leaving on error",
599 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200600 return 0;
601
602 return_prx_yield:
603 channel_dont_connect(req);
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100604 DBG_TRACE_DEVEL("waiting for more data",
605 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200606 return 0;
607}
608
609/* This function performs all the processing enabled for the current request.
610 * It returns 1 if the processing can continue on next analysers, or zero if it
611 * needs more data, encounters an error, or wants to immediately abort the
612 * request. It relies on buffers flags, and updates s->req.analysers.
613 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200614int http_process_request(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200615{
616 struct session *sess = s->sess;
617 struct http_txn *txn = s->txn;
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200618 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200619 struct connection *cli_conn = objt_conn(strm_sess(s)->origin);
620
Christopher Faulet8bebd2f2020-10-06 17:54:56 +0200621 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200622
623 /*
624 * Right now, we know that we have processed the entire headers
625 * and that unwanted requests have been filtered out. We can do
626 * whatever we want with the remaining request. Also, now we
627 * may have separate values for ->fe, ->be.
628 */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100629 htx = htxbuf(&req->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200630
631 /*
Christopher Faulete0768eb2018-10-03 16:38:02 +0200632 * 7: Now we can work with the cookies.
633 * Note that doing so might move headers in the request, but
634 * the fields will stay coherent and the URI will not move.
635 * This should only be performed in the backend.
636 */
637 if (s->be->cookie_name || sess->fe->capture_name)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200638 http_manage_client_side_cookies(s, req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200639
Tim Duesterhus2825b4b2020-02-28 15:13:34 +0100640 /* 8: Generate unique ID if a "unique-id-format" is defined.
641 *
642 * A unique ID is generated even when it is not sent to ensure that the ID can make use of
643 * fetches only available in the HTTP request processing stage.
644 */
645 if (!LIST_ISEMPTY(&sess->fe->format_unique_id)) {
Tim Duesterhusa17e6622020-03-05 20:19:02 +0100646 struct ist unique_id = stream_generate_unique_id(s, &sess->fe->format_unique_id);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200647
Tim Duesterhusa17e6622020-03-05 20:19:02 +0100648 if (!isttest(unique_id)) {
Christopher Fauletb8a53712019-12-16 11:29:38 +0100649 if (!(s->flags & SF_ERR_MASK))
650 s->flags |= SF_ERR_RESOURCE;
651 goto return_int_err;
652 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200653
Tim Duesterhus2825b4b2020-02-28 15:13:34 +0100654 /* send unique ID if a "unique-id-header" is defined */
Tim Duesterhus0643b0e2020-03-05 17:56:35 +0100655 if (isttest(sess->fe->header_unique_id) &&
Tim Duesterhus7ad27d42022-05-18 00:22:15 +0200656 unlikely(!http_add_header(htx, sess->fe->header_unique_id, unique_id)))
Tim Duesterhus2825b4b2020-02-28 15:13:34 +0100657 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200658 }
659
660 /*
661 * 9: add X-Forwarded-For if either the frontend or the backend
662 * asks for it.
663 */
664 if ((sess->fe->options | s->be->options) & PR_O_FWDFOR) {
Willy Tarreaud68ff012022-05-27 08:57:21 +0200665 const struct sockaddr_storage *src = sc_src(s->scf);
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200666 struct http_hdr_ctx ctx = { .blk = NULL };
Tim Duesterhusb50ab842022-03-05 00:52:41 +0100667 struct ist hdr = isttest(s->be->fwdfor_hdr_name) ? s->be->fwdfor_hdr_name : sess->fe->fwdfor_hdr_name;
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200668
Christopher Faulete0768eb2018-10-03 16:38:02 +0200669 if (!((sess->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200670 http_find_header(htx, hdr, &ctx, 0)) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200671 /* The header is set to be added only if none is present
672 * and we found it, so don't do anything.
673 */
674 }
Christopher Faulet8a104ba2021-10-25 07:41:30 +0200675 else if (src && src->ss_family == AF_INET) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200676 /* Add an X-Forwarded-For header unless the source IP is
677 * in the 'except' network range.
678 */
Christopher Faulet8a104ba2021-10-25 07:41:30 +0200679 if (ipcmp2net(src, &sess->fe->except_xff_net) &&
680 ipcmp2net(src, &s->be->except_xff_net)) {
681 unsigned char *pn = (unsigned char *)&((struct sockaddr_in *)src)->sin_addr;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200682
683 /* Note: we rely on the backend to get the header name to be used for
684 * x-forwarded-for, because the header is really meant for the backends.
685 * However, if the backend did not specify any option, we have to rely
686 * on the frontend's header name.
687 */
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200688 chunk_printf(&trash, "%d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
689 if (unlikely(!http_add_header(htx, hdr, ist2(trash.area, trash.data))))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100690 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200691 }
692 }
Christopher Faulet8a104ba2021-10-25 07:41:30 +0200693 else if (src && src->ss_family == AF_INET6) {
Christopher Faulet5d1def62021-02-26 09:19:15 +0100694 /* Add an X-Forwarded-For header unless the source IP is
695 * in the 'except' network range.
Christopher Faulete0768eb2018-10-03 16:38:02 +0200696 */
Christopher Faulet8a104ba2021-10-25 07:41:30 +0200697 if (ipcmp2net(src, &sess->fe->except_xff_net) &&
698 ipcmp2net(src, &s->be->except_xff_net)) {
Christopher Faulet5d1def62021-02-26 09:19:15 +0100699 char pn[INET6_ADDRSTRLEN];
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200700
Christopher Faulet5d1def62021-02-26 09:19:15 +0100701 inet_ntop(AF_INET6,
Christopher Faulet8a104ba2021-10-25 07:41:30 +0200702 (const void *)&((struct sockaddr_in6 *)(src))->sin6_addr,
Christopher Faulet5d1def62021-02-26 09:19:15 +0100703 pn, sizeof(pn));
Christopher Faulete0768eb2018-10-03 16:38:02 +0200704
Christopher Faulet5d1def62021-02-26 09:19:15 +0100705 /* Note: we rely on the backend to get the header name to be used for
706 * x-forwarded-for, because the header is really meant for the backends.
707 * However, if the backend did not specify any option, we have to rely
708 * on the frontend's header name.
709 */
710 chunk_printf(&trash, "%s", pn);
711 if (unlikely(!http_add_header(htx, hdr, ist2(trash.area, trash.data))))
712 goto return_int_err;
713 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200714 }
715 }
716
717 /*
718 * 10: add X-Original-To if either the frontend or the backend
719 * asks for it.
720 */
721 if ((sess->fe->options | s->be->options) & PR_O_ORGTO) {
Willy Tarreaud68ff012022-05-27 08:57:21 +0200722 const struct sockaddr_storage *dst = sc_dst(s->scf);
Tim Duesterhuse502c3e2022-03-05 00:52:42 +0100723 struct ist hdr = isttest(s->be->orgto_hdr_name) ? s->be->orgto_hdr_name : sess->fe->orgto_hdr_name;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200724
Christopher Faulet8a104ba2021-10-25 07:41:30 +0200725 if (dst && dst->ss_family == AF_INET) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200726 /* Add an X-Original-To header unless the destination IP is
727 * in the 'except' network range.
728 */
Christopher Faulet8a104ba2021-10-25 07:41:30 +0200729 if (ipcmp2net(dst, &sess->fe->except_xot_net) &&
730 ipcmp2net(dst, &s->be->except_xot_net)) {
731 unsigned char *pn = (unsigned char *)&((struct sockaddr_in *)dst)->sin_addr;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200732
733 /* Note: we rely on the backend to get the header name to be used for
734 * x-original-to, because the header is really meant for the backends.
735 * However, if the backend did not specify any option, we have to rely
736 * on the frontend's header name.
737 */
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200738 chunk_printf(&trash, "%d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
739 if (unlikely(!http_add_header(htx, hdr, ist2(trash.area, trash.data))))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100740 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200741 }
742 }
Christopher Faulet8a104ba2021-10-25 07:41:30 +0200743 else if (dst && dst->ss_family == AF_INET6) {
Christopher Faulet5d1def62021-02-26 09:19:15 +0100744 /* Add an X-Original-To header unless the source IP is
745 * in the 'except' network range.
746 */
Christopher Faulet8a104ba2021-10-25 07:41:30 +0200747 if (ipcmp2net(dst, &sess->fe->except_xot_net) &&
748 ipcmp2net(dst, &s->be->except_xot_net)) {
Christopher Faulet5d1def62021-02-26 09:19:15 +0100749 char pn[INET6_ADDRSTRLEN];
750
751 inet_ntop(AF_INET6,
Christopher Faulet8a104ba2021-10-25 07:41:30 +0200752 (const void *)&((struct sockaddr_in6 *)dst)->sin6_addr,
Christopher Faulet5d1def62021-02-26 09:19:15 +0100753 pn, sizeof(pn));
754
755 /* Note: we rely on the backend to get the header name to be used for
756 * x-forwarded-for, because the header is really meant for the backends.
757 * However, if the backend did not specify any option, we have to rely
758 * on the frontend's header name.
759 */
760 chunk_printf(&trash, "%s", pn);
761 if (unlikely(!http_add_header(htx, hdr, ist2(trash.area, trash.data))))
762 goto return_int_err;
763 }
764 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200765 }
766
Christopher Fauletc2ac5e42021-03-08 18:20:09 +0100767 /* Filter the request headers if there are filters attached to the
768 * stream.
769 */
770 if (HAS_FILTERS(s))
771 req->analysers |= AN_REQ_FLT_HTTP_HDRS;
772
Christopher Faulete0768eb2018-10-03 16:38:02 +0200773 /* If we have no server assigned yet and we're balancing on url_param
774 * with a POST request, we may be interested in checking the body for
775 * that parameter. This will be done in another analyser.
776 */
777 if (!(s->flags & (SF_ASSIGNED|SF_DIRECT)) &&
Willy Tarreau089eaa02019-01-14 15:17:46 +0100778 s->txn->meth == HTTP_METH_POST &&
779 (s->be->lbprm.algo & BE_LB_ALGO) == BE_LB_ALGO_PH) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200780 channel_dont_connect(req);
781 req->analysers |= AN_REQ_HTTP_BODY;
782 }
783
784 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
785 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau1a18b542018-12-11 16:37:42 +0100786
Christopher Faulete0768eb2018-10-03 16:38:02 +0200787 /* We expect some data from the client. Unless we know for sure
788 * we already have a full request, we have to re-enable quick-ack
789 * in case we previously disabled it, otherwise we might cause
790 * the client to delay further data.
791 */
William Lallemand36119de2021-03-08 15:26:48 +0100792 if ((sess->listener && (sess->listener->options & LI_O_NOQUICKACK)) && !(htx->flags & HTX_FL_EOM))
Willy Tarreau1a18b542018-12-11 16:37:42 +0100793 conn_set_quickack(cli_conn, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200794
795 /*************************************************************
796 * OK, that's finished for the headers. We have done what we *
797 * could. Let's switch to the DATA state. *
798 ************************************************************/
799 req->analyse_exp = TICK_ETERNITY;
800 req->analysers &= ~an_bit;
801
802 s->logs.tv_request = now;
803 /* OK let's go on with the BODY now */
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100804 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200805 return 1;
806
Christopher Fauletb8a53712019-12-16 11:29:38 +0100807 return_int_err:
808 txn->status = 500;
809 if (!(s->flags & SF_ERR_MASK))
810 s->flags |= SF_ERR_INTERNAL;
Willy Tarreau4781b152021-04-06 13:53:36 +0200811 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100812 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau4781b152021-04-06 13:53:36 +0200813 _HA_ATOMIC_INC(&s->be->be_counters.internal_errors);
William Lallemand36119de2021-03-08 15:26:48 +0100814 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200815 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100816
Christopher Fauletb8a53712019-12-16 11:29:38 +0100817 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +0200818
819 if (!(s->flags & SF_ERR_MASK))
820 s->flags |= SF_ERR_PRXCOND;
821 if (!(s->flags & SF_FINST_MASK))
822 s->flags |= SF_FINST_R;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100823
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100824 DBG_TRACE_DEVEL("leaving on error",
825 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200826 return 0;
827}
828
829/* This function is an analyser which processes the HTTP tarpit. It always
830 * returns zero, at the beginning because it prevents any other processing
831 * from occurring, and at the end because it terminates the request.
832 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200833int http_process_tarpit(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200834{
835 struct http_txn *txn = s->txn;
836
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100837 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, &txn->req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200838 /* This connection is being tarpitted. The CLIENT side has
839 * already set the connect expiration date to the right
840 * timeout. We just have to check that the client is still
841 * there and that the timeout has not expired.
842 */
843 channel_dont_connect(req);
844 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100845 !tick_is_expired(req->analyse_exp, now_ms)) {
Christopher Fauletb0c87f12021-10-29 14:37:07 +0200846 /* Be sure to drain all data from the request channel */
847 channel_htx_erase(req, htxbuf(&req->buf));
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100848 DBG_TRACE_DEVEL("waiting for tarpit timeout expiry",
849 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200850 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100851 }
852
Christopher Faulete0768eb2018-10-03 16:38:02 +0200853
854 /* We will set the queue timer to the time spent, just for
855 * logging purposes. We fake a 500 server error, so that the
856 * attacker will not suspect his connection has been tarpitted.
857 * It will not cause trouble to the logs because we can exclude
858 * the tarpitted connections by filtering on the 'PT' status flags.
859 */
860 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
861
Christopher Faulet8dfeccf2020-05-15 14:16:29 +0200862 http_reply_and_close(s, txn->status, (!(req->flags & CF_READ_ERROR) ? http_error_message(s) : NULL));
Christopher Faulet5cb513a2020-05-13 17:56:56 +0200863
Christopher Faulete0768eb2018-10-03 16:38:02 +0200864 if (!(s->flags & SF_ERR_MASK))
865 s->flags |= SF_ERR_PRXCOND;
866 if (!(s->flags & SF_FINST_MASK))
867 s->flags |= SF_FINST_T;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100868
869 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200870 return 0;
871}
872
873/* This function is an analyser which waits for the HTTP request body. It waits
874 * for either the buffer to be full, or the full advertised contents to have
875 * reached the buffer. It must only be called after the standard HTTP request
876 * processing has occurred, because it expects the request to be parsed and will
877 * look for the Expect header. It may send a 100-Continue interim response. It
878 * takes in input any state starting from HTTP_MSG_BODY and leaves with one of
879 * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it
880 * needs to read more data, or 1 once it has completed its analysis.
881 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200882int http_wait_for_request_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;
886 struct http_msg *msg = &s->txn->req;
887
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100888 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Fauletf76ebe82018-10-24 11:16:22 +0200889
Christopher Fauletf76ebe82018-10-24 11:16:22 +0200890
Christopher Faulet021a8e42021-03-29 10:46:38 +0200891 switch (http_wait_for_msg_body(s, req, s->be->timeout.httpreq, 0)) {
892 case HTTP_RULE_RES_CONT:
893 goto http_end;
894 case HTTP_RULE_RES_YIELD:
895 goto missing_data_or_waiting;
896 case HTTP_RULE_RES_BADREQ:
Willy Tarreau4236f032019-03-05 10:43:32 +0100897 goto return_bad_req;
Christopher Faulet021a8e42021-03-29 10:46:38 +0200898 case HTTP_RULE_RES_ERROR:
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200899 goto return_int_err;
Christopher Faulet021a8e42021-03-29 10:46:38 +0200900 case HTTP_RULE_RES_ABRT:
Christopher Fauletb8a53712019-12-16 11:29:38 +0100901 goto return_prx_cond;
Christopher Faulet021a8e42021-03-29 10:46:38 +0200902 default:
903 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200904 }
905
906 http_end:
907 /* The situation will not evolve, so let's give up on the analysis. */
908 s->logs.tv_request = now; /* update the request timer to reflect full request */
909 req->analysers &= ~an_bit;
910 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100911 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200912 return 1;
913
Christopher Faulet021a8e42021-03-29 10:46:38 +0200914 missing_data_or_waiting:
915 channel_dont_connect(req);
916 DBG_TRACE_DEVEL("waiting for more data",
917 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
918 return 0;
919
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200920 return_int_err:
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200921 txn->status = 500;
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200922 if (!(s->flags & SF_ERR_MASK))
923 s->flags |= SF_ERR_INTERNAL;
Willy Tarreau4781b152021-04-06 13:53:36 +0200924 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100925 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau4781b152021-04-06 13:53:36 +0200926 _HA_ATOMIC_INC(&s->be->be_counters.internal_errors);
William Lallemand36119de2021-03-08 15:26:48 +0100927 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200928 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Faulet021a8e42021-03-29 10:46:38 +0200929 goto return_prx_err;
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200930
Christopher Faulete0768eb2018-10-03 16:38:02 +0200931 return_bad_req: /* let's centralize all bad requests */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200932 txn->status = 400;
Willy Tarreau4781b152021-04-06 13:53:36 +0200933 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req);
William Lallemand36119de2021-03-08 15:26:48 +0100934 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200935 _HA_ATOMIC_INC(&sess->listener->counters->failed_req);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100936 /* fall through */
937
Christopher Faulet021a8e42021-03-29 10:46:38 +0200938 return_prx_err:
Christopher Fauletb8a53712019-12-16 11:29:38 +0100939 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet021a8e42021-03-29 10:46:38 +0200940 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200941
Christopher Faulet021a8e42021-03-29 10:46:38 +0200942 return_prx_cond:
Christopher Faulete0768eb2018-10-03 16:38:02 +0200943 if (!(s->flags & SF_ERR_MASK))
944 s->flags |= SF_ERR_PRXCOND;
945 if (!(s->flags & SF_FINST_MASK))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100946 s->flags |= (msg->msg_state < HTTP_MSG_DATA ? SF_FINST_R : SF_FINST_D);
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200947
Christopher Faulete0768eb2018-10-03 16:38:02 +0200948 req->analysers &= AN_REQ_FLT_END;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100949 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100950 DBG_TRACE_DEVEL("leaving on error",
951 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200952 return 0;
953}
954
955/* This function is an analyser which forwards request body (including chunk
956 * sizes if any). It is called as soon as we must forward, even if we forward
957 * zero byte. The only situation where it must not be called is when we're in
958 * tunnel mode and we want to forward till the close. It's used both to forward
959 * remaining data and to resync after end of body. It expects the msg_state to
960 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
961 * read more data, or 1 once we can go on with next request or end the stream.
962 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
963 * bytes of pending data + the headers if not already done.
964 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200965int http_request_forward_body(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200966{
967 struct session *sess = s->sess;
968 struct http_txn *txn = s->txn;
Christopher Faulet9768c262018-10-22 09:34:31 +0200969 struct http_msg *msg = &txn->req;
970 struct htx *htx;
Christopher Faulet93e02d82019-03-08 14:18:50 +0100971 short status = 0;
Christopher Fauletaed82cf2018-11-30 22:22:32 +0100972 int ret;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200973
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100974 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200975
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100976 htx = htxbuf(&req->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200977
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200978 if (htx->flags & HTX_FL_PARSING_ERROR)
979 goto return_bad_req;
980 if (htx->flags & HTX_FL_PROCESSING_ERROR)
981 goto return_int_err;
982
Christopher Faulete0768eb2018-10-03 16:38:02 +0200983 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
984 ((req->flags & CF_SHUTW) && (req->to_forward || co_data(req)))) {
985 /* Output closed while we were sending data. We must abort and
986 * wake the other side up.
Christopher Fauletf506d962021-04-27 10:56:28 +0200987 *
988 * If we have finished to send the request and the response is
989 * still in progress, don't catch write error on the request
990 * side if it is in fact a read error on the server side.
Christopher Faulete0768eb2018-10-03 16:38:02 +0200991 */
Christopher Fauletf506d962021-04-27 10:56:28 +0200992 if (msg->msg_state == HTTP_MSG_DONE && (s->res.flags & CF_READ_ERROR) && s->res.analysers)
993 return 0;
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200994
Olivier Houchard29cac3c2019-07-12 15:48:58 +0200995 /* Don't abort yet if we had L7 retries activated and it
996 * was a write error, we may recover.
997 */
998 if (!(req->flags & (CF_READ_ERROR | CF_READ_TIMEOUT)) &&
Christopher Faulete05bf9e2022-03-29 15:23:40 +0200999 (txn->flags & TX_L7_RETRY)) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001000 DBG_TRACE_DEVEL("leaving on L7 retry",
1001 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Olivier Houchard29cac3c2019-07-12 15:48:58 +02001002 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001003 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001004 msg->msg_state = HTTP_MSG_ERROR;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001005 http_end_request(s);
1006 http_end_response(s);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001007 DBG_TRACE_DEVEL("leaving on error",
1008 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001009 return 1;
1010 }
1011
1012 /* Note that we don't have to send 100-continue back because we don't
1013 * need the data to complete our job, and it's up to the server to
1014 * decide whether to return 100, 417 or anything else in return of
1015 * an "Expect: 100-continue" header.
1016 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001017 if (msg->msg_state == HTTP_MSG_BODY)
1018 msg->msg_state = HTTP_MSG_DATA;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001019
Christopher Faulete0768eb2018-10-03 16:38:02 +02001020 /* in most states, we should abort in case of early close */
1021 channel_auto_close(req);
1022
1023 if (req->to_forward) {
Christopher Faulet66af0b22019-03-22 14:54:52 +01001024 if (req->to_forward == CHN_INFINITE_FORWARD) {
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001025 if (req->flags & CF_EOI)
1026 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet66af0b22019-03-22 14:54:52 +01001027 }
1028 else {
1029 /* We can't process the buffer's contents yet */
1030 req->flags |= CF_WAKE_WRITE;
1031 goto missing_data_or_waiting;
1032 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001033 }
1034
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001035 if (msg->msg_state >= HTTP_MSG_ENDING)
1036 goto ending;
1037
1038 if (txn->meth == HTTP_METH_CONNECT) {
1039 msg->msg_state = HTTP_MSG_ENDING;
1040 goto ending;
1041 }
1042
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001043 /* Forward input data. We get it by removing all outgoing data not
1044 * forwarded yet from HTX data size. If there are some data filters, we
1045 * let them decide the amount of data to forward.
Christopher Faulet9768c262018-10-22 09:34:31 +02001046 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001047 if (HAS_REQ_DATA_FILTERS(s)) {
1048 ret = flt_http_payload(s, msg, htx->data);
1049 if (ret < 0)
1050 goto return_bad_req;
Christopher Faulet421e7692019-06-13 11:16:45 +02001051 c_adv(req, ret);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001052 }
1053 else {
Christopher Faulet421e7692019-06-13 11:16:45 +02001054 c_adv(req, htx->data - co_data(req));
Christopher Faulet66af0b22019-03-22 14:54:52 +01001055 if (msg->flags & HTTP_MSGF_XFER_LEN)
1056 channel_htx_forward_forever(req, htx);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001057 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001058
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001059 if (htx->data != co_data(req))
1060 goto missing_data_or_waiting;
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001061
Christopher Faulet9768c262018-10-22 09:34:31 +02001062 /* Check if the end-of-message is reached and if so, switch the message
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001063 * in HTTP_MSG_ENDING state. Then if all data was marked to be
1064 * forwarded, set the state to HTTP_MSG_DONE.
Christopher Faulet9768c262018-10-22 09:34:31 +02001065 */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001066 if (!(htx->flags & HTX_FL_EOM))
Christopher Faulet9768c262018-10-22 09:34:31 +02001067 goto missing_data_or_waiting;
1068
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001069 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet9768c262018-10-22 09:34:31 +02001070
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001071 ending:
Christopher Faulet2151cdd2020-07-22 16:34:59 +02001072 req->flags &= ~CF_EXPECT_MORE; /* no more data are expected */
1073
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001074 /* other states, ENDING...TUNNEL */
1075 if (msg->msg_state >= HTTP_MSG_DONE)
1076 goto done;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001077
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001078 if (HAS_REQ_DATA_FILTERS(s)) {
1079 ret = flt_http_end(s, msg);
1080 if (ret <= 0) {
1081 if (!ret)
1082 goto missing_data_or_waiting;
1083 goto return_bad_req;
1084 }
1085 }
1086
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001087 if (txn->meth == HTTP_METH_CONNECT)
1088 msg->msg_state = HTTP_MSG_TUNNEL;
1089 else {
1090 msg->msg_state = HTTP_MSG_DONE;
1091 req->to_forward = 0;
1092 }
1093
1094 done:
1095 /* we don't want to forward closes on DONE except in tunnel mode. */
1096 if (!(txn->flags & TX_CON_WANT_TUN))
1097 channel_dont_close(req);
1098
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001099 http_end_request(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001100 if (!(req->analysers & an_bit)) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001101 http_end_response(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001102 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
1103 if (req->flags & CF_SHUTW) {
1104 /* request errors are most likely due to the
1105 * server aborting the transfer. */
Christopher Faulet93e02d82019-03-08 14:18:50 +01001106 goto return_srv_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001107 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001108 goto return_bad_req;
1109 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001110 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001111 return 1;
1112 }
1113
1114 /* If "option abortonclose" is set on the backend, we want to monitor
1115 * the client's connection and forward any shutdown notification to the
1116 * server, which will decide whether to close or to go on processing the
1117 * request. We only do that in tunnel mode, and not in other modes since
1118 * it can be abused to exhaust source ports. */
Christopher Faulet769d0e92019-03-22 14:23:18 +01001119 if (s->be->options & PR_O_ABRT_CLOSE) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001120 channel_auto_read(req);
Christopher Fauletc41547b2019-07-16 14:32:23 +02001121 if ((req->flags & (CF_SHUTR|CF_READ_NULL)) && !(txn->flags & TX_CON_WANT_TUN))
Willy Tarreaucb041662022-05-17 19:44:42 +02001122 s->scb->flags |= SC_FL_NOLINGER;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001123 channel_auto_close(req);
1124 }
1125 else if (s->txn->meth == HTTP_METH_POST) {
1126 /* POST requests may require to read extra CRLF sent by broken
1127 * browsers and which could cause an RST to be sent upon close
1128 * on some systems (eg: Linux). */
1129 channel_auto_read(req);
1130 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001131 DBG_TRACE_DEVEL("waiting for the end of the HTTP txn",
1132 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001133 return 0;
1134
1135 missing_data_or_waiting:
1136 /* stop waiting for data if the input is closed before the end */
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001137 if (msg->msg_state < HTTP_MSG_ENDING && req->flags & CF_SHUTR)
Christopher Faulet93e02d82019-03-08 14:18:50 +01001138 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001139
1140 waiting:
1141 /* waiting for the last bits to leave the buffer */
1142 if (req->flags & CF_SHUTW)
Christopher Faulet93e02d82019-03-08 14:18:50 +01001143 goto return_srv_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001144
1145 /* When TE: chunked is used, we need to get there again to parse remaining
1146 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
1147 * And when content-length is used, we never want to let the possible
1148 * shutdown be forwarded to the other side, as the state machine will
1149 * take care of it once the client responds. It's also important to
1150 * prevent TIME_WAITs from accumulating on the backend side, and for
1151 * HTTP/2 where the last frame comes with a shutdown.
1152 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001153 if (msg->flags & HTTP_MSGF_XFER_LEN)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001154 channel_dont_close(req);
1155
1156 /* We know that more data are expected, but we couldn't send more that
1157 * what we did. So we always set the CF_EXPECT_MORE flag so that the
1158 * system knows it must not set a PUSH on this first part. Interactive
1159 * modes are already handled by the stream sock layer. We must not do
1160 * this in content-length mode because it could present the MSG_MORE
1161 * flag with the last block of forwarded data, which would cause an
1162 * additional delay to be observed by the receiver.
1163 */
Christopher Faulet2151cdd2020-07-22 16:34:59 +02001164 if (HAS_REQ_DATA_FILTERS(s))
Christopher Faulete0768eb2018-10-03 16:38:02 +02001165 req->flags |= CF_EXPECT_MORE;
1166
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001167 DBG_TRACE_DEVEL("waiting for more data to forward",
1168 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001169 return 0;
1170
Christopher Faulet93e02d82019-03-08 14:18:50 +01001171 return_cli_abort:
Willy Tarreau4781b152021-04-06 13:53:36 +02001172 _HA_ATOMIC_INC(&sess->fe->fe_counters.cli_aborts);
1173 _HA_ATOMIC_INC(&s->be->be_counters.cli_aborts);
William Lallemand36119de2021-03-08 15:26:48 +01001174 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001175 _HA_ATOMIC_INC(&sess->listener->counters->cli_aborts);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001176 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001177 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.cli_aborts);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001178 if (!(s->flags & SF_ERR_MASK))
1179 s->flags |= SF_ERR_CLICL;
1180 status = 400;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001181 goto return_prx_cond;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001182
1183 return_srv_abort:
Willy Tarreau4781b152021-04-06 13:53:36 +02001184 _HA_ATOMIC_INC(&sess->fe->fe_counters.srv_aborts);
1185 _HA_ATOMIC_INC(&s->be->be_counters.srv_aborts);
William Lallemand36119de2021-03-08 15:26:48 +01001186 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001187 _HA_ATOMIC_INC(&sess->listener->counters->srv_aborts);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001188 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001189 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.srv_aborts);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001190 if (!(s->flags & SF_ERR_MASK))
1191 s->flags |= SF_ERR_SRVCL;
1192 status = 502;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001193 goto return_prx_cond;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001194
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001195 return_int_err:
1196 if (!(s->flags & SF_ERR_MASK))
1197 s->flags |= SF_ERR_INTERNAL;
Willy Tarreau4781b152021-04-06 13:53:36 +02001198 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
1199 _HA_ATOMIC_INC(&s->be->be_counters.internal_errors);
William Lallemand36119de2021-03-08 15:26:48 +01001200 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001201 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001202 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001203 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.internal_errors);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001204 status = 500;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001205 goto return_prx_cond;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001206
Christopher Faulet93e02d82019-03-08 14:18:50 +01001207 return_bad_req:
Willy Tarreau4781b152021-04-06 13:53:36 +02001208 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req);
William Lallemand36119de2021-03-08 15:26:48 +01001209 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001210 _HA_ATOMIC_INC(&sess->listener->counters->failed_req);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001211 status = 400;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001212 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001213
Christopher Fauletb8a53712019-12-16 11:29:38 +01001214 return_prx_cond:
Christopher Faulet9768c262018-10-22 09:34:31 +02001215 if (txn->status > 0) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001216 /* Note: we don't send any error if some data were already sent */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001217 http_reply_and_close(s, txn->status, NULL);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001218 } else {
Christopher Faulet93e02d82019-03-08 14:18:50 +01001219 txn->status = status;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001220 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001221 }
Christopher Fauletb8a53712019-12-16 11:29:38 +01001222 if (!(s->flags & SF_ERR_MASK))
1223 s->flags |= SF_ERR_PRXCOND;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001224 if (!(s->flags & SF_FINST_MASK))
1225 s->flags |= ((txn->rsp.msg_state < HTTP_MSG_ERROR) ? SF_FINST_H : SF_FINST_D);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001226 DBG_TRACE_DEVEL("leaving on error ",
1227 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001228 return 0;
1229}
1230
Willy Tarreau4596fe22022-05-17 19:07:51 +02001231/* Reset the stream and the backend stream connector to a situation suitable for attemption connection */
Olivier Houcharda254a372019-04-05 15:30:12 +02001232/* Returns 0 if we can attempt to retry, -1 otherwise */
Willy Tarreaub49672d2022-05-27 10:13:37 +02001233static __inline int do_l7_retry(struct stream *s, struct stconn *sc)
Olivier Houcharda254a372019-04-05 15:30:12 +02001234{
Christopher Faulet9f5382e2021-05-21 13:46:14 +02001235 struct channel *req, *res;
1236 int co_data;
Olivier Houcharda254a372019-04-05 15:30:12 +02001237
Christopher Faulet731c8e62022-03-29 16:08:44 +02001238 s->conn_retries++;
1239 if (s->conn_retries >= s->be->conn_retries)
Christopher Faulet552601d2021-05-26 10:31:06 +02001240 return -1;
Christopher Faulet5b82cc52020-10-12 15:18:50 +02001241
Christopher Faulete763c8c2021-05-05 18:23:59 +02001242 if (objt_server(s->target)) {
1243 if (s->flags & SF_CURR_SESS) {
1244 s->flags &= ~SF_CURR_SESS;
1245 _HA_ATOMIC_DEC(&__objt_server(s->target)->cur_sess);
1246 }
Willy Tarreau4781b152021-04-06 13:53:36 +02001247 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.retries);
Christopher Faulete763c8c2021-05-05 18:23:59 +02001248 }
Willy Tarreau4781b152021-04-06 13:53:36 +02001249 _HA_ATOMIC_INC(&s->be->be_counters.retries);
Willy Tarreau223995e2019-05-04 10:38:31 +02001250
Christopher Faulet9f5382e2021-05-21 13:46:14 +02001251 req = &s->req;
1252 res = &s->res;
Olivier Houcharda254a372019-04-05 15:30:12 +02001253 /* Remove any write error from the request, and read error from the response */
1254 req->flags &= ~(CF_WRITE_ERROR | CF_WRITE_TIMEOUT | CF_SHUTW | CF_SHUTW_NOW);
1255 res->flags &= ~(CF_READ_ERROR | CF_READ_TIMEOUT | CF_SHUTR | CF_EOI | CF_READ_NULL | CF_SHUTR_NOW);
Christopher Faulet7bf46bb2022-01-04 10:56:03 +01001256 res->analysers &= AN_RES_FLT_END;
Christopher Faulet50264b42022-03-30 19:39:30 +02001257 s->conn_err_type = STRM_ET_NONE;
Christopher Fauletae024ce2022-03-29 19:02:31 +02001258 s->flags &= ~(SF_CONN_EXP | SF_ERR_MASK | SF_FINST_MASK);
1259 s->conn_exp = TICK_ETERNITY;
Olivier Houchard4bd58672019-07-12 16:16:59 +02001260 stream_choose_redispatch(s);
Olivier Houcharda254a372019-04-05 15:30:12 +02001261 res->rex = TICK_ETERNITY;
1262 res->to_forward = 0;
1263 res->analyse_exp = TICK_ETERNITY;
1264 res->total = 0;
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +01001265
Willy Tarreau19c65a92022-05-27 08:49:24 +02001266 if (sc_reset_endp(s->scb) < 0) {
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +01001267 if (!(s->flags & SF_ERR_MASK))
1268 s->flags |= SF_ERR_INTERNAL;
1269 return -1;
1270 }
Olivier Houcharda254a372019-04-05 15:30:12 +02001271
Christopher Faulet9f5382e2021-05-21 13:46:14 +02001272 b_free(&req->buf);
1273 /* Swap the L7 buffer with the channel buffer */
1274 /* We know we stored the co_data as b_data, so get it there */
Christopher Faulete05bf9e2022-03-29 15:23:40 +02001275 co_data = b_data(&s->txn->l7_buffer);
1276 b_set_data(&s->txn->l7_buffer, b_size(&s->txn->l7_buffer));
1277 b_xfer(&req->buf, &s->txn->l7_buffer, b_data(&s->txn->l7_buffer));
Christopher Faulet9f5382e2021-05-21 13:46:14 +02001278 co_set_data(req, co_data);
Christopher Faulet5b82cc52020-10-12 15:18:50 +02001279
Ilya Shipitsinacf84592021-02-06 22:29:08 +05001280 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 +02001281
Olivier Houcharda254a372019-04-05 15:30:12 +02001282 b_reset(&res->buf);
1283 co_set_data(res, 0);
1284 return 0;
1285}
1286
Christopher Faulete0768eb2018-10-03 16:38:02 +02001287/* This stream analyser waits for a complete HTTP response. It returns 1 if the
1288 * processing can continue on next analysers, or zero if it either needs more
1289 * data or wants to immediately abort the response (eg: timeout, error, ...). It
1290 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->res.analysers
1291 * when it has nothing left to do, and may remove any analyser when it wants to
1292 * abort.
1293 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001294int http_wait_for_response(struct stream *s, struct channel *rep, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001295{
Christopher Faulet9768c262018-10-22 09:34:31 +02001296 /*
1297 * We will analyze a complete HTTP response to check the its syntax.
1298 *
1299 * Once the start line and all headers are received, we may perform a
1300 * capture of the error (if any), and we will set a few fields. We also
1301 * logging and finally headers capture.
1302 */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001303 struct session *sess = s->sess;
1304 struct http_txn *txn = s->txn;
1305 struct http_msg *msg = &txn->rsp;
Christopher Faulet9768c262018-10-22 09:34:31 +02001306 struct htx *htx;
Christopher Faulet61608322018-11-23 16:23:45 +01001307 struct connection *srv_conn;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001308 struct htx_sl *sl;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001309 int n;
1310
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001311 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001312
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001313 htx = htxbuf(&rep->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001314
Willy Tarreau4236f032019-03-05 10:43:32 +01001315 /* Parsing errors are caught here */
1316 if (htx->flags & HTX_FL_PARSING_ERROR)
1317 goto return_bad_res;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001318 if (htx->flags & HTX_FL_PROCESSING_ERROR)
1319 goto return_int_err;
Willy Tarreau4236f032019-03-05 10:43:32 +01001320
Christopher Faulete0768eb2018-10-03 16:38:02 +02001321 /*
1322 * Now we quickly check if we have found a full valid response.
1323 * If not so, we check the FD and buffer states before leaving.
1324 * A full response is indicated by the fact that we have seen
1325 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
1326 * responses are checked first.
1327 *
1328 * Depending on whether the client is still there or not, we
1329 * may send an error response back or not. Note that normally
1330 * we should only check for HTTP status there, and check I/O
1331 * errors somewhere else.
1332 */
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001333 next_one:
Christopher Faulet29f17582019-05-23 11:03:26 +02001334 if (unlikely(htx_is_empty(htx) || htx->first == -1)) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001335 /* 1: have we encountered a read error ? */
1336 if (rep->flags & CF_READ_ERROR) {
Willy Tarreaufd9417b2022-05-18 16:23:22 +02001337 struct connection *conn = sc_conn(s->scb);
Olivier Houchard865d8392019-05-03 22:46:27 +02001338
Christopher Fauletd9769232021-05-26 12:15:37 +02001339 /* Perform a L7 retry because server refuses the early data. */
Christopher Faulete05bf9e2022-03-29 15:23:40 +02001340 if ((txn->flags & TX_L7_RETRY) &&
Christopher Fauletd9769232021-05-26 12:15:37 +02001341 (s->be->retry_type & PR_RE_EARLY_ERROR) &&
1342 conn && conn->err_code == CO_ER_SSL_EARLY_FAILED &&
Willy Tarreau7cb9e6c2022-05-17 19:40:40 +02001343 do_l7_retry(s, s->scb) == 0) {
Christopher Fauletd9769232021-05-26 12:15:37 +02001344 DBG_TRACE_DEVEL("leaving on L7 retry",
1345 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
1346 return 0;
1347 }
1348
Olivier Houchard6db16992019-05-17 15:40:49 +02001349 if (txn->flags & TX_NOT_FIRST)
1350 goto abort_keep_alive;
1351
Willy Tarreau4781b152021-04-06 13:53:36 +02001352 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001353 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001354 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001355 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001356 }
1357
Christopher Fauletd9769232021-05-26 12:15:37 +02001358 /* if the server refused the early data, just send a 425 */
1359 if (conn && conn->err_code == CO_ER_SSL_EARLY_FAILED)
Olivier Houchard865d8392019-05-03 22:46:27 +02001360 txn->status = 425;
Christopher Fauletd9769232021-05-26 12:15:37 +02001361 else {
1362 txn->status = 502;
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001363 stream_inc_http_fail_ctr(s);
Christopher Fauletd9769232021-05-26 12:15:37 +02001364 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001365
Willy Tarreaucb041662022-05-17 19:44:42 +02001366 s->scb->flags |= SC_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001367 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001368
1369 if (!(s->flags & SF_ERR_MASK))
1370 s->flags |= SF_ERR_SRVCL;
1371 if (!(s->flags & SF_FINST_MASK))
1372 s->flags |= SF_FINST_H;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001373 DBG_TRACE_DEVEL("leaving on error",
1374 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001375 return 0;
1376 }
1377
Christopher Faulet9768c262018-10-22 09:34:31 +02001378 /* 2: read timeout : return a 504 to the client. */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001379 else if (rep->flags & CF_READ_TIMEOUT) {
Christopher Faulete05bf9e2022-03-29 15:23:40 +02001380 if ((txn->flags & TX_L7_RETRY) &&
Olivier Houcharda254a372019-04-05 15:30:12 +02001381 (s->be->retry_type & PR_RE_TIMEOUT)) {
Willy Tarreau7cb9e6c2022-05-17 19:40:40 +02001382 if (co_data(rep) || do_l7_retry(s, s->scb) == 0) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001383 DBG_TRACE_DEVEL("leaving on L7 retry",
1384 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharda254a372019-04-05 15:30:12 +02001385 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001386 }
Olivier Houcharda254a372019-04-05 15:30:12 +02001387 }
Willy Tarreau4781b152021-04-06 13:53:36 +02001388 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001389 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001390 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001391 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001392 }
1393
Christopher Faulete0768eb2018-10-03 16:38:02 +02001394 txn->status = 504;
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001395 stream_inc_http_fail_ctr(s);
Willy Tarreaucb041662022-05-17 19:44:42 +02001396 s->scb->flags |= SC_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001397 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001398
1399 if (!(s->flags & SF_ERR_MASK))
1400 s->flags |= SF_ERR_SRVTO;
1401 if (!(s->flags & SF_FINST_MASK))
1402 s->flags |= SF_FINST_H;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001403 DBG_TRACE_DEVEL("leaving on error",
1404 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001405 return 0;
1406 }
1407
Christopher Faulet9768c262018-10-22 09:34:31 +02001408 /* 3: client abort with an abortonclose */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001409 else if ((rep->flags & CF_SHUTR) && ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001410 _HA_ATOMIC_INC(&sess->fe->fe_counters.cli_aborts);
1411 _HA_ATOMIC_INC(&s->be->be_counters.cli_aborts);
William Lallemand36119de2021-03-08 15:26:48 +01001412 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001413 _HA_ATOMIC_INC(&sess->listener->counters->cli_aborts);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001414 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001415 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.cli_aborts);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001416
Christopher Faulete0768eb2018-10-03 16:38:02 +02001417 txn->status = 400;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001418 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001419
1420 if (!(s->flags & SF_ERR_MASK))
1421 s->flags |= SF_ERR_CLICL;
1422 if (!(s->flags & SF_FINST_MASK))
1423 s->flags |= SF_FINST_H;
1424
1425 /* process_stream() will take care of the error */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001426 DBG_TRACE_DEVEL("leaving on error",
1427 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001428 return 0;
1429 }
1430
Christopher Faulet9768c262018-10-22 09:34:31 +02001431 /* 4: close from server, capture the response if the server has started to respond */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001432 else if (rep->flags & CF_SHUTR) {
Christopher Faulete05bf9e2022-03-29 15:23:40 +02001433 if ((txn->flags & TX_L7_RETRY) &&
Olivier Houcharda254a372019-04-05 15:30:12 +02001434 (s->be->retry_type & PR_RE_DISCONNECTED)) {
Willy Tarreau7cb9e6c2022-05-17 19:40:40 +02001435 if (co_data(rep) || do_l7_retry(s, s->scb) == 0) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001436 DBG_TRACE_DEVEL("leaving on L7 retry",
1437 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharda254a372019-04-05 15:30:12 +02001438 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001439 }
Olivier Houcharda254a372019-04-05 15:30:12 +02001440 }
1441
Olivier Houchard6db16992019-05-17 15:40:49 +02001442 if (txn->flags & TX_NOT_FIRST)
1443 goto abort_keep_alive;
1444
Willy Tarreau4781b152021-04-06 13:53:36 +02001445 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001446 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001447 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001448 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001449 }
1450
Christopher Faulete0768eb2018-10-03 16:38:02 +02001451 txn->status = 502;
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001452 stream_inc_http_fail_ctr(s);
Willy Tarreaucb041662022-05-17 19:44:42 +02001453 s->scb->flags |= SC_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001454 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001455
1456 if (!(s->flags & SF_ERR_MASK))
1457 s->flags |= SF_ERR_SRVCL;
1458 if (!(s->flags & SF_FINST_MASK))
1459 s->flags |= SF_FINST_H;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001460 DBG_TRACE_DEVEL("leaving on error",
1461 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001462 return 0;
1463 }
1464
Christopher Faulet9768c262018-10-22 09:34:31 +02001465 /* 5: write error to client (we don't send any message then) */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001466 else if (rep->flags & CF_WRITE_ERROR) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001467 if (txn->flags & TX_NOT_FIRST)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001468 goto abort_keep_alive;
1469
Willy Tarreau4781b152021-04-06 13:53:36 +02001470 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001471 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001472 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001473 rep->analysers &= AN_RES_FLT_END;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001474
1475 if (!(s->flags & SF_ERR_MASK))
1476 s->flags |= SF_ERR_CLICL;
1477 if (!(s->flags & SF_FINST_MASK))
1478 s->flags |= SF_FINST_H;
1479
1480 /* process_stream() will take care of the error */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001481 DBG_TRACE_DEVEL("leaving on error",
1482 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001483 return 0;
1484 }
1485
1486 channel_dont_close(rep);
1487 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001488 DBG_TRACE_DEVEL("waiting for more data",
1489 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001490 return 0;
1491 }
1492
1493 /* More interesting part now : we know that we have a complete
1494 * response which at least looks like HTTP. We have an indicator
1495 * of each header's length, so we can parse them quickly.
1496 */
Christopher Faulet29f17582019-05-23 11:03:26 +02001497 BUG_ON(htx_get_first_type(htx) != HTX_BLK_RES_SL);
Christopher Faulet297fbb42019-05-13 14:41:27 +02001498 sl = http_get_stline(htx);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001499
Christopher Faulet1f08bff2021-05-26 13:14:39 +02001500 /* Perform a L7 retry because of the status code */
Christopher Faulete05bf9e2022-03-29 15:23:40 +02001501 if ((txn->flags & TX_L7_RETRY) &&
Christopher Faulet1f08bff2021-05-26 13:14:39 +02001502 l7_status_match(s->be, sl->info.res.status) &&
Willy Tarreau7cb9e6c2022-05-17 19:40:40 +02001503 do_l7_retry(s, s->scb) == 0) {
Christopher Faulet1f08bff2021-05-26 13:14:39 +02001504 DBG_TRACE_DEVEL("leaving on L7 retry", STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
1505 return 0;
1506 }
1507
1508 /* Now, L7 buffer is useless, it can be released */
Christopher Faulete05bf9e2022-03-29 15:23:40 +02001509 b_free(&txn->l7_buffer);
Christopher Faulet1f08bff2021-05-26 13:14:39 +02001510
1511 msg->msg_state = HTTP_MSG_BODY;
1512
1513
Christopher Faulet9768c262018-10-22 09:34:31 +02001514 /* 0: we might have to print this header in debug mode */
1515 if (unlikely((global.mode & MODE_DEBUG) &&
1516 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
1517 int32_t pos;
1518
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001519 http_debug_stline("srvrep", s, sl);
Christopher Faulet9768c262018-10-22 09:34:31 +02001520
Christopher Fauleta3f15502019-05-13 15:27:23 +02001521 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001522 struct htx_blk *blk = htx_get_blk(htx, pos);
1523 enum htx_blk_type type = htx_get_blk_type(blk);
1524
1525 if (type == HTX_BLK_EOH)
1526 break;
1527 if (type != HTX_BLK_HDR)
1528 continue;
1529
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001530 http_debug_hdr("srvhdr", s,
1531 htx_get_blk_name(htx, blk),
1532 htx_get_blk_value(htx, blk));
Christopher Faulet9768c262018-10-22 09:34:31 +02001533 }
1534 }
1535
Christopher Faulet03599112018-11-27 11:21:21 +01001536 /* 1: get the status code and the version. Also set HTTP flags */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001537 txn->status = sl->info.res.status;
Christopher Faulet03599112018-11-27 11:21:21 +01001538 if (sl->flags & HTX_SL_F_VER_11)
Christopher Faulet9768c262018-10-22 09:34:31 +02001539 msg->flags |= HTTP_MSGF_VER_11;
Christopher Faulet03599112018-11-27 11:21:21 +01001540 if (sl->flags & HTX_SL_F_XFER_LEN) {
1541 msg->flags |= HTTP_MSGF_XFER_LEN;
Christopher Faulet2a408542020-11-20 14:22:37 +01001542 if (sl->flags & HTX_SL_F_CLEN)
1543 msg->flags |= HTTP_MSGF_CNT_LEN;
1544 else if (sl->flags & HTX_SL_F_CHNK)
1545 msg->flags |= HTTP_MSGF_TE_CHNK;
Christopher Faulet03599112018-11-27 11:21:21 +01001546 }
Christopher Faulet2a408542020-11-20 14:22:37 +01001547 if (sl->flags & HTX_SL_F_BODYLESS)
1548 msg->flags |= HTTP_MSGF_BODYLESS;
Christopher Faulet576c3582021-01-08 15:53:01 +01001549 if (sl->flags & HTX_SL_F_CONN_UPG)
1550 msg->flags |= HTTP_MSGF_CONN_UPG;
Christopher Faulet9768c262018-10-22 09:34:31 +02001551
1552 n = txn->status / 100;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001553 if (n < 1 || n > 5)
1554 n = 0;
Christopher Faulet9768c262018-10-22 09:34:31 +02001555
Christopher Faulete0768eb2018-10-03 16:38:02 +02001556 /* when the client triggers a 4xx from the server, it's most often due
1557 * to a missing object or permission. These events should be tracked
1558 * because if they happen often, it may indicate a brute force or a
1559 * vulnerability scan.
1560 */
1561 if (n == 4)
1562 stream_inc_http_err_ctr(s);
1563
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001564 if (n == 5 && txn->status != 501 && txn->status != 505)
1565 stream_inc_http_fail_ctr(s);
1566
Marcin Deranek3c27dda2020-05-15 18:32:51 +02001567 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001568 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.p.http.rsp[n]);
1569 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.p.http.cum_req);
Marcin Deranek3c27dda2020-05-15 18:32:51 +02001570 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001571
Christopher Faulete0768eb2018-10-03 16:38:02 +02001572 /* Adjust server's health based on status code. Note: status codes 501
1573 * and 505 are triggered on demand by client request, so we must not
1574 * count them as server failures.
1575 */
1576 if (objt_server(s->target)) {
1577 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001578 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_OK);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001579 else
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001580 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_STS);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001581 }
1582
1583 /*
1584 * We may be facing a 100-continue response, or any other informational
1585 * 1xx response which is non-final, in which case this is not the right
1586 * response, and we're waiting for the next one. Let's allow this response
1587 * to go to the client and wait for the next one. There's an exception for
1588 * 101 which is used later in the code to switch protocols.
1589 */
1590 if (txn->status < 200 &&
1591 (txn->status == 100 || txn->status >= 102)) {
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001592 FLT_STRM_CB(s, flt_http_reset(s, msg));
Christopher Faulet421e7692019-06-13 11:16:45 +02001593 htx->first = channel_htx_fwd_headers(rep, htx);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001594 msg->msg_state = HTTP_MSG_RPBEFORE;
Christopher Faulet3499f622019-09-03 15:23:54 +02001595 msg->flags = 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001596 txn->status = 0;
1597 s->logs.t_data = -1; /* was not a response yet */
Christopher Faulet7d518452020-08-31 11:07:07 +02001598 rep->flags |= CF_SEND_DONTWAIT; /* Send ASAP informational messages */
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001599 goto next_one;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001600 }
1601
Christopher Faulet6e6c7b12021-01-08 16:02:05 +01001602 /* A 101-switching-protocols must contains a Connection header with the
1603 * "upgrade" option and the request too. It means both are agree to
1604 * upgrade. It is not so strict because there is no test on the Upgrade
1605 * header content. But it is probably stronger enough for now.
1606 */
1607 if (txn->status == 101 &&
1608 (!(txn->req.flags & HTTP_MSGF_CONN_UPG) || !(txn->rsp.flags & HTTP_MSGF_CONN_UPG)))
1609 goto return_bad_res;
1610
Christopher Faulete0768eb2018-10-03 16:38:02 +02001611 /*
1612 * 2: check for cacheability.
1613 */
1614
1615 switch (txn->status) {
1616 case 200:
1617 case 203:
1618 case 204:
1619 case 206:
1620 case 300:
1621 case 301:
1622 case 404:
1623 case 405:
1624 case 410:
1625 case 414:
1626 case 501:
1627 break;
1628 default:
1629 /* RFC7231#6.1:
1630 * Responses with status codes that are defined as
1631 * cacheable by default (e.g., 200, 203, 204, 206,
1632 * 300, 301, 404, 405, 410, 414, and 501 in this
1633 * specification) can be reused by a cache with
1634 * heuristic expiration unless otherwise indicated
1635 * by the method definition or explicit cache
1636 * controls [RFC7234]; all other status codes are
1637 * not cacheable by default.
1638 */
1639 txn->flags &= ~(TX_CACHEABLE | TX_CACHE_COOK);
1640 break;
1641 }
1642
1643 /*
1644 * 3: we may need to capture headers
1645 */
1646 s->logs.logwait &= ~LW_RESP;
1647 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->res_cap))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001648 http_capture_headers(htx, s->res_cap, sess->fe->rsp_cap);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001649
Christopher Faulet9768c262018-10-22 09:34:31 +02001650 /* Skip parsing if no content length is possible. */
Christopher Fauletc75668e2020-12-07 18:10:32 +01001651 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status >= 200 && txn->status < 300) ||
Christopher Faulete0768eb2018-10-03 16:38:02 +02001652 txn->status == 101)) {
1653 /* Either we've established an explicit tunnel, or we're
1654 * switching the protocol. In both cases, we're very unlikely
1655 * to understand the next protocols. We have to switch to tunnel
1656 * mode, so that we transfer the request and responses then let
1657 * this protocol pass unmodified. When we later implement specific
1658 * parsers for such protocols, we'll want to check the Upgrade
1659 * header which contains information about that protocol for
1660 * responses with status 101 (eg: see RFC2817 about TLS).
1661 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02001662 txn->flags |= TX_CON_WANT_TUN;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001663 }
1664
Christopher Faulet61608322018-11-23 16:23:45 +01001665 /* check for NTML authentication headers in 401 (WWW-Authenticate) and
1666 * 407 (Proxy-Authenticate) responses and set the connection to private
1667 */
Willy Tarreaufd9417b2022-05-18 16:23:22 +02001668 srv_conn = sc_conn(s->scb);
Christopher Faulet61608322018-11-23 16:23:45 +01001669 if (srv_conn) {
1670 struct ist hdr;
1671 struct http_hdr_ctx ctx;
1672
1673 if (txn->status == 401)
1674 hdr = ist("WWW-Authenticate");
1675 else if (txn->status == 407)
1676 hdr = ist("Proxy-Authenticate");
1677 else
1678 goto end;
1679
1680 ctx.blk = NULL;
1681 while (http_find_header(htx, hdr, &ctx, 0)) {
Willy Tarreauf1dcced2020-05-07 19:27:02 +02001682 /* If www-authenticate contains "Negotiate", "Nego2", or "NTLM",
1683 * possibly followed by blanks and a base64 string, the connection
1684 * is private. Since it's a mess to deal with, we only check for
1685 * values starting with "NTLM" or "Nego". Note that often multiple
1686 * headers are sent by the server there.
1687 */
1688 if ((ctx.value.len >= 4 && strncasecmp(ctx.value.ptr, "Nego", 4) == 0) ||
Willy Tarreau49a1d282020-05-07 19:10:15 +02001689 (ctx.value.len >= 4 && strncasecmp(ctx.value.ptr, "NTLM", 4) == 0)) {
Olivier Houchard250031e2019-05-29 15:01:50 +02001690 sess->flags |= SESS_FL_PREFER_LAST;
Christopher Faulet08016ab2020-07-01 16:10:06 +02001691 conn_set_owner(srv_conn, sess, NULL);
Christopher Faulet21ddc742020-07-01 15:26:14 +02001692 conn_set_private(srv_conn);
Ilya Shipitsin6b79f382020-07-23 00:32:55 +05001693 /* If it fail now, the same will be done in mux->detach() callback */
Christopher Faulet08016ab2020-07-01 16:10:06 +02001694 session_add_conn(srv_conn->owner, srv_conn, srv_conn->target);
Willy Tarreauf1dcced2020-05-07 19:27:02 +02001695 break;
Olivier Houchard250031e2019-05-29 15:01:50 +02001696 }
Christopher Faulet61608322018-11-23 16:23:45 +01001697 }
1698 }
1699
1700 end:
Christopher Faulete0768eb2018-10-03 16:38:02 +02001701 /* we want to have the response time before we start processing it */
1702 s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now);
1703
1704 /* end of job, return OK */
1705 rep->analysers &= ~an_bit;
1706 rep->analyse_exp = TICK_ETERNITY;
1707 channel_auto_close(rep);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001708 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001709 return 1;
1710
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001711 return_int_err:
Willy Tarreau4781b152021-04-06 13:53:36 +02001712 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
1713 _HA_ATOMIC_INC(&s->be->be_counters.internal_errors);
William Lallemand36119de2021-03-08 15:26:48 +01001714 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001715 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Fauletb8a53712019-12-16 11:29:38 +01001716 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001717 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.internal_errors);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001718 txn->status = 500;
1719 if (!(s->flags & SF_ERR_MASK))
1720 s->flags |= SF_ERR_INTERNAL;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001721 goto return_prx_cond;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001722
1723 return_bad_res:
Willy Tarreau4781b152021-04-06 13:53:36 +02001724 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Faulet47365272018-10-31 17:40:50 +01001725 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001726 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001727 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Christopher Faulet47365272018-10-31 17:40:50 +01001728 }
Olivier Houcharde3249a92019-05-03 23:01:47 +02001729 if ((s->be->retry_type & PR_RE_JUNK_REQUEST) &&
Christopher Faulete05bf9e2022-03-29 15:23:40 +02001730 (txn->flags & TX_L7_RETRY) &&
Willy Tarreau7cb9e6c2022-05-17 19:40:40 +02001731 do_l7_retry(s, s->scb) == 0) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001732 DBG_TRACE_DEVEL("leaving on L7 retry",
1733 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharde3249a92019-05-03 23:01:47 +02001734 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001735 }
Christopher Faulet47365272018-10-31 17:40:50 +01001736 txn->status = 502;
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001737 stream_inc_http_fail_ctr(s);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001738 /* fall through */
1739
Christopher Fauletb8a53712019-12-16 11:29:38 +01001740 return_prx_cond:
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001741 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet47365272018-10-31 17:40:50 +01001742
1743 if (!(s->flags & SF_ERR_MASK))
1744 s->flags |= SF_ERR_PRXCOND;
1745 if (!(s->flags & SF_FINST_MASK))
1746 s->flags |= SF_FINST_H;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001747
Willy Tarreaucb041662022-05-17 19:44:42 +02001748 s->scb->flags |= SC_FL_NOLINGER;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001749 DBG_TRACE_DEVEL("leaving on error",
1750 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulet47365272018-10-31 17:40:50 +01001751 return 0;
1752
Christopher Faulete0768eb2018-10-03 16:38:02 +02001753 abort_keep_alive:
1754 /* A keep-alive request to the server failed on a network error.
1755 * The client is required to retry. We need to close without returning
1756 * any other information so that the client retries.
1757 */
1758 txn->status = 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001759 s->logs.logwait = 0;
1760 s->logs.level = 0;
1761 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001762 http_reply_and_close(s, txn->status, NULL);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001763 DBG_TRACE_DEVEL("leaving by closing K/A connection",
1764 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001765 return 0;
1766}
1767
1768/* This function performs all the processing enabled for the current response.
1769 * It normally returns 1 unless it wants to break. It relies on buffers flags,
1770 * and updates s->res.analysers. It might make sense to explode it into several
1771 * other functions. It works like process_request (see indications above).
1772 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001773int http_process_res_common(struct stream *s, struct channel *rep, int an_bit, struct proxy *px)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001774{
1775 struct session *sess = s->sess;
1776 struct http_txn *txn = s->txn;
1777 struct http_msg *msg = &txn->rsp;
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001778 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001779 struct proxy *cur_proxy;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001780 enum rule_result ret = HTTP_RULE_RES_CONT;
1781
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001782 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
1783 return 0;
Christopher Faulet9768c262018-10-22 09:34:31 +02001784
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001785 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001786
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001787 htx = htxbuf(&rep->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001788
1789 /* The stats applet needs to adjust the Connection header but we don't
1790 * apply any filter there.
1791 */
1792 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
1793 rep->analysers &= ~an_bit;
1794 rep->analyse_exp = TICK_ETERNITY;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001795 goto end;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001796 }
1797
1798 /*
1799 * We will have to evaluate the filters.
1800 * As opposed to version 1.2, now they will be evaluated in the
1801 * filters order and not in the header order. This means that
1802 * each filter has to be validated among all headers.
1803 *
1804 * Filters are tried with ->be first, then with ->fe if it is
1805 * different from ->be.
1806 *
1807 * Maybe we are in resume condiion. In this case I choose the
1808 * "struct proxy" which contains the rule list matching the resume
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05001809 * pointer. If none of these "struct proxy" match, I initialise
Christopher Faulete0768eb2018-10-03 16:38:02 +02001810 * the process with the first one.
1811 *
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05001812 * In fact, I check only correspondence between the current list
Christopher Faulete0768eb2018-10-03 16:38:02 +02001813 * pointer and the ->fe rule list. If it doesn't match, I initialize
1814 * the loop with the ->be.
1815 */
Christopher Fauletd4150ad2021-10-13 15:35:55 +02001816 if (s->current_rule_list == &sess->fe->http_res_rules ||
1817 (sess->fe->defpx && s->current_rule_list == &sess->fe->defpx->http_res_rules))
Christopher Faulete0768eb2018-10-03 16:38:02 +02001818 cur_proxy = sess->fe;
1819 else
1820 cur_proxy = s->be;
Christopher Fauletd4150ad2021-10-13 15:35:55 +02001821
Christopher Faulete0768eb2018-10-03 16:38:02 +02001822 while (1) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001823 /* evaluate http-response rules */
Christopher Faulet46f46df2021-11-09 16:33:25 +01001824 if (ret == HTTP_RULE_RES_CONT || ret == HTTP_RULE_RES_STOP) {
Christopher Fauletd4150ad2021-10-13 15:35:55 +02001825 struct list *def_rules, *rules;
1826
1827 def_rules = ((cur_proxy->defpx && (cur_proxy == s->be || cur_proxy->defpx != s->be->defpx)) ? &cur_proxy->defpx->http_res_rules : NULL);
1828 rules = &cur_proxy->http_res_rules;
1829
1830 ret = http_res_get_intercept_rule(cur_proxy, def_rules, rules, s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001831
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001832 switch (ret) {
1833 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
1834 goto return_prx_yield;
1835
1836 case HTTP_RULE_RES_CONT:
1837 case HTTP_RULE_RES_STOP: /* nothing to do */
1838 break;
1839
1840 case HTTP_RULE_RES_DENY: /* deny or tarpit */
1841 goto deny;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001842
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001843 case HTTP_RULE_RES_ABRT: /* abort request, response already sent */
1844 goto return_prx_cond;
1845
1846 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
Christopher Fauletb8a53712019-12-16 11:29:38 +01001847 goto done;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001848
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001849 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
1850 goto return_bad_res;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001851
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001852 case HTTP_RULE_RES_ERROR: /* failed with a bad request */
1853 goto return_int_err;
1854 }
1855
1856 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001857
Christopher Faulete0768eb2018-10-03 16:38:02 +02001858 /* check whether we're already working on the frontend */
1859 if (cur_proxy == sess->fe)
1860 break;
1861 cur_proxy = sess->fe;
1862 }
1863
Christopher Faulete0768eb2018-10-03 16:38:02 +02001864 /* OK that's all we can do for 1xx responses */
1865 if (unlikely(txn->status < 200 && txn->status != 101))
Christopher Fauletf2824e62018-10-01 12:12:37 +02001866 goto end;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001867
1868 /*
1869 * Now check for a server cookie.
1870 */
1871 if (s->be->cookie_name || sess->fe->capture_name || (s->be->options & PR_O_CHK_CACHE))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001872 http_manage_server_side_cookies(s, rep);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001873
1874 /*
1875 * Check for cache-control or pragma headers if required.
1876 */
1877 if ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001878 http_check_response_for_cacheability(s, rep);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001879
1880 /*
1881 * Add server cookie in the response if needed
1882 */
1883 if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) &&
1884 !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) &&
1885 (!(s->flags & SF_DIRECT) ||
1886 ((s->be->cookie_maxidle || txn->cookie_last_date) &&
1887 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
1888 (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
1889 (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
1890 (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
1891 !(s->flags & SF_IGNORE_PRST)) {
1892 /* the server is known, it's not the one the client requested, or the
1893 * cookie's last seen date needs to be refreshed. We have to
1894 * insert a set-cookie here, except if we want to insert only on POST
1895 * requests and this one isn't. Note that servers which don't have cookies
1896 * (eg: some backup servers) will return a full cookie removal request.
1897 */
Willy Tarreau88bc8002021-12-06 07:01:02 +00001898 if (!__objt_server(s->target)->cookie) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001899 chunk_printf(&trash,
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001900 "%s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
Christopher Faulete0768eb2018-10-03 16:38:02 +02001901 s->be->cookie_name);
1902 }
1903 else {
Willy Tarreau88bc8002021-12-06 07:01:02 +00001904 chunk_printf(&trash, "%s=%s", s->be->cookie_name, __objt_server(s->target)->cookie);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001905
1906 if (s->be->cookie_maxidle || s->be->cookie_maxlife) {
1907 /* emit last_date, which is mandatory */
1908 trash.area[trash.data++] = COOKIE_DELIM_DATE;
1909 s30tob64((date.tv_sec+3) >> 2,
1910 trash.area + trash.data);
1911 trash.data += 5;
1912
1913 if (s->be->cookie_maxlife) {
1914 /* emit first_date, which is either the original one or
1915 * the current date.
1916 */
1917 trash.area[trash.data++] = COOKIE_DELIM_DATE;
1918 s30tob64(txn->cookie_first_date ?
1919 txn->cookie_first_date >> 2 :
1920 (date.tv_sec+3) >> 2,
1921 trash.area + trash.data);
1922 trash.data += 5;
1923 }
1924 }
1925 chunk_appendf(&trash, "; path=/");
1926 }
1927
1928 if (s->be->cookie_domain)
1929 chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain);
1930
1931 if (s->be->ck_opts & PR_CK_HTTPONLY)
1932 chunk_appendf(&trash, "; HttpOnly");
1933
1934 if (s->be->ck_opts & PR_CK_SECURE)
1935 chunk_appendf(&trash, "; Secure");
1936
Christopher Faulet2f533902020-01-21 11:06:48 +01001937 if (s->be->cookie_attrs)
1938 chunk_appendf(&trash, "; %s", s->be->cookie_attrs);
1939
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001940 if (unlikely(!http_add_header(htx, ist("Set-Cookie"), ist2(trash.area, trash.data))))
Christopher Fauletb8a53712019-12-16 11:29:38 +01001941 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001942
1943 txn->flags &= ~TX_SCK_MASK;
1944 if (__objt_server(s->target)->cookie && (s->flags & SF_DIRECT))
1945 /* the server did not change, only the date was updated */
1946 txn->flags |= TX_SCK_UPDATED;
1947 else
1948 txn->flags |= TX_SCK_INSERTED;
1949
1950 /* Here, we will tell an eventual cache on the client side that we don't
1951 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
1952 * Some caches understand the correct form: 'no-cache="set-cookie"', but
1953 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
1954 */
1955 if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
1956
1957 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
1958
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001959 if (unlikely(!http_add_header(htx, ist("Cache-control"), ist("private"))))
Christopher Fauletb8a53712019-12-16 11:29:38 +01001960 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001961 }
1962 }
1963
1964 /*
1965 * Check if result will be cacheable with a cookie.
1966 * We'll block the response if security checks have caught
1967 * nasty things such as a cacheable cookie.
1968 */
1969 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
1970 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
1971 (s->be->options & PR_O_CHK_CACHE)) {
1972 /* we're in presence of a cacheable response containing
1973 * a set-cookie header. We'll block it as requested by
1974 * the 'checkcache' option, and send an alert.
1975 */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001976 ha_alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreau88bc8002021-12-06 07:01:02 +00001977 s->be->id, objt_server(s->target) ? __objt_server(s->target)->id : "<dispatch>");
Christopher Faulete0768eb2018-10-03 16:38:02 +02001978 send_log(s->be, LOG_ALERT,
1979 "Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreau88bc8002021-12-06 07:01:02 +00001980 s->be->id, objt_server(s->target) ? __objt_server(s->target)->id : "<dispatch>");
Christopher Fauletb8a53712019-12-16 11:29:38 +01001981 goto deny;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001982 }
1983
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001984 end:
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01001985 /*
1986 * Evaluate after-response rules before forwarding the response. rules
1987 * from the backend are evaluated first, then one from the frontend if
1988 * it differs.
1989 */
1990 if (!http_eval_after_res_rules(s))
1991 goto return_int_err;
1992
Christopher Fauletc2ac5e42021-03-08 18:20:09 +01001993 /* Filter the response headers if there are filters attached to the
1994 * stream.
1995 */
1996 if (HAS_FILTERS(s))
1997 rep->analysers |= AN_RES_FLT_HTTP_HDRS;
1998
Christopher Faulete0768eb2018-10-03 16:38:02 +02001999 /* Always enter in the body analyzer */
2000 rep->analysers &= ~AN_RES_FLT_XFER_DATA;
2001 rep->analysers |= AN_RES_HTTP_XFER_BODY;
2002
2003 /* if the user wants to log as soon as possible, without counting
2004 * bytes from the server, then this is the right moment. We have
2005 * to temporarily assign bytes_out to log what we currently have.
2006 */
2007 if (!LIST_ISEMPTY(&sess->fe->logformat) && !(s->logs.logwait & LW_BYTES)) {
2008 s->logs.t_close = s->logs.t_data; /* to get a valid end date */
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002009 s->logs.bytes_out = htx->data;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002010 s->do_log(s);
2011 s->logs.bytes_out = 0;
2012 }
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002013
Christopher Fauletb8a53712019-12-16 11:29:38 +01002014 done:
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002015 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002016 rep->analysers &= ~an_bit;
2017 rep->analyse_exp = TICK_ETERNITY;
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002018 s->current_rule = s->current_rule_list = NULL;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002019 return 1;
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002020
Christopher Fauletb8a53712019-12-16 11:29:38 +01002021 deny:
Willy Tarreau4781b152021-04-06 13:53:36 +02002022 _HA_ATOMIC_INC(&sess->fe->fe_counters.denied_resp);
2023 _HA_ATOMIC_INC(&s->be->be_counters.denied_resp);
William Lallemand36119de2021-03-08 15:26:48 +01002024 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02002025 _HA_ATOMIC_INC(&sess->listener->counters->denied_resp);
Christopher Fauleta08546b2019-12-16 16:07:34 +01002026 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02002027 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.denied_resp);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002028 goto return_prx_err;
2029
2030 return_int_err:
2031 txn->status = 500;
2032 if (!(s->flags & SF_ERR_MASK))
2033 s->flags |= SF_ERR_INTERNAL;
Willy Tarreau4781b152021-04-06 13:53:36 +02002034 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
2035 _HA_ATOMIC_INC(&s->be->be_counters.internal_errors);
Dragan Dosen9a006f92021-09-21 13:02:09 +02002036 if (sess->listener && sess->listener->counters)
2037 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Fauletcff0f732019-12-16 16:13:44 +01002038 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02002039 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.internal_errors);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002040 goto return_prx_err;
2041
2042 return_bad_res:
2043 txn->status = 502;
Willy Tarreau826f3ab2021-02-10 12:07:15 +01002044 stream_inc_http_fail_ctr(s);
Willy Tarreau4781b152021-04-06 13:53:36 +02002045 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Fauleta20a6532020-02-05 10:16:41 +01002046 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02002047 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Christopher Fauleta20a6532020-02-05 10:16:41 +01002048 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_RSP);
2049 }
Christopher Fauletb8a53712019-12-16 11:29:38 +01002050 /* fall through */
2051
2052 return_prx_err:
2053 http_reply_and_close(s, txn->status, http_error_message(s));
2054 /* fall through */
2055
2056 return_prx_cond:
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002057 s->logs.t_data = -1; /* was not a valid response */
Willy Tarreaucb041662022-05-17 19:44:42 +02002058 s->scb->flags |= SC_FL_NOLINGER;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002059
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002060 if (!(s->flags & SF_ERR_MASK))
2061 s->flags |= SF_ERR_PRXCOND;
2062 if (!(s->flags & SF_FINST_MASK))
2063 s->flags |= SF_FINST_H;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002064
Christopher Faulete58c0002020-03-02 16:21:01 +01002065 rep->analysers &= AN_RES_FLT_END;
2066 s->req.analysers &= AN_REQ_FLT_END;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002067 rep->analyse_exp = TICK_ETERNITY;
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002068 s->current_rule = s->current_rule_list = NULL;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002069 DBG_TRACE_DEVEL("leaving on error",
2070 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002071 return 0;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002072
2073 return_prx_yield:
2074 channel_dont_close(rep);
2075 DBG_TRACE_DEVEL("waiting for more data",
2076 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
2077 return 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002078}
2079
2080/* This function is an analyser which forwards response body (including chunk
2081 * sizes if any). It is called as soon as we must forward, even if we forward
2082 * zero byte. The only situation where it must not be called is when we're in
2083 * tunnel mode and we want to forward till the close. It's used both to forward
2084 * remaining data and to resync after end of body. It expects the msg_state to
2085 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
2086 * read more data, or 1 once we can go on with next request or end the stream.
2087 *
2088 * It is capable of compressing response data both in content-length mode and
2089 * in chunked mode. The state machines follows different flows depending on
2090 * whether content-length and chunked modes are used, since there are no
2091 * trailers in content-length :
2092 *
2093 * chk-mode cl-mode
2094 * ,----- BODY -----.
2095 * / \
2096 * V size > 0 V chk-mode
2097 * .--> SIZE -------------> DATA -------------> CRLF
2098 * | | size == 0 | last byte |
2099 * | v final crlf v inspected |
2100 * | TRAILERS -----------> DONE |
2101 * | |
2102 * `----------------------------------------------'
2103 *
2104 * Compression only happens in the DATA state, and must be flushed in final
2105 * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding
2106 * is performed at once on final states for all bytes parsed, or when leaving
2107 * on missing data.
2108 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002109int http_response_forward_body(struct stream *s, struct channel *res, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +02002110{
2111 struct session *sess = s->sess;
2112 struct http_txn *txn = s->txn;
2113 struct http_msg *msg = &s->txn->rsp;
Christopher Faulet9768c262018-10-22 09:34:31 +02002114 struct htx *htx;
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002115 int ret;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002116
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002117 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002118
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002119 htx = htxbuf(&res->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002120
Christopher Fauletb9a92f32019-09-09 10:15:21 +02002121 if (htx->flags & HTX_FL_PARSING_ERROR)
2122 goto return_bad_res;
2123 if (htx->flags & HTX_FL_PROCESSING_ERROR)
2124 goto return_int_err;
2125
Christopher Faulete0768eb2018-10-03 16:38:02 +02002126 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Christopher Fauletf2824e62018-10-01 12:12:37 +02002127 ((res->flags & CF_SHUTW) && (res->to_forward || co_data(res)))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02002128 /* Output closed while we were sending data. We must abort and
2129 * wake the other side up.
2130 */
Christopher Faulete0768eb2018-10-03 16:38:02 +02002131 msg->msg_state = HTTP_MSG_ERROR;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002132 http_end_response(s);
2133 http_end_request(s);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002134 DBG_TRACE_DEVEL("leaving on error",
2135 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002136 return 1;
2137 }
2138
Christopher Faulet9768c262018-10-22 09:34:31 +02002139 if (msg->msg_state == HTTP_MSG_BODY)
2140 msg->msg_state = HTTP_MSG_DATA;
2141
Christopher Faulete0768eb2018-10-03 16:38:02 +02002142 /* in most states, we should abort in case of early close */
2143 channel_auto_close(res);
2144
Christopher Faulete0768eb2018-10-03 16:38:02 +02002145 if (res->to_forward) {
Christopher Faulet66af0b22019-03-22 14:54:52 +01002146 if (res->to_forward == CHN_INFINITE_FORWARD) {
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002147 if (res->flags & CF_EOI)
2148 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet66af0b22019-03-22 14:54:52 +01002149 }
2150 else {
2151 /* We can't process the buffer's contents yet */
2152 res->flags |= CF_WAKE_WRITE;
2153 goto missing_data_or_waiting;
2154 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02002155 }
2156
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002157 if (msg->msg_state >= HTTP_MSG_ENDING)
2158 goto ending;
2159
Christopher Fauletc75668e2020-12-07 18:10:32 +01002160 if ((txn->meth == HTTP_METH_CONNECT && txn->status >= 200 && txn->status < 300) || txn->status == 101 ||
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002161 (!(msg->flags & HTTP_MSGF_XFER_LEN) && !HAS_RSP_DATA_FILTERS(s))) {
2162 msg->msg_state = HTTP_MSG_ENDING;
2163 goto ending;
2164 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002165
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002166 /* Forward input data. We get it by removing all outgoing data not
2167 * forwarded yet from HTX data size. If there are some data filters, we
2168 * let them decide the amount of data to forward.
Christopher Faulet9768c262018-10-22 09:34:31 +02002169 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002170 if (HAS_RSP_DATA_FILTERS(s)) {
2171 ret = flt_http_payload(s, msg, htx->data);
2172 if (ret < 0)
2173 goto return_bad_res;
Christopher Faulet421e7692019-06-13 11:16:45 +02002174 c_adv(res, ret);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002175 }
2176 else {
Christopher Faulet421e7692019-06-13 11:16:45 +02002177 c_adv(res, htx->data - co_data(res));
Christopher Faulet66af0b22019-03-22 14:54:52 +01002178 if (msg->flags & HTTP_MSGF_XFER_LEN)
2179 channel_htx_forward_forever(res, htx);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002180 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002181
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002182 if (htx->data != co_data(res))
2183 goto missing_data_or_waiting;
2184
2185 if (!(msg->flags & HTTP_MSGF_XFER_LEN) && res->flags & CF_SHUTR) {
2186 msg->msg_state = HTTP_MSG_ENDING;
2187 goto ending;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002188 }
2189
Christopher Faulet9768c262018-10-22 09:34:31 +02002190 /* Check if the end-of-message is reached and if so, switch the message
Christopher Fauletd20fdb02019-06-13 16:43:22 +02002191 * in HTTP_MSG_ENDING state. Then if all data was marked to be
2192 * forwarded, set the state to HTTP_MSG_DONE.
Christopher Faulet9768c262018-10-22 09:34:31 +02002193 */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002194 if (!(htx->flags & HTX_FL_EOM))
Christopher Faulet9768c262018-10-22 09:34:31 +02002195 goto missing_data_or_waiting;
2196
Christopher Fauletd20fdb02019-06-13 16:43:22 +02002197 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet9768c262018-10-22 09:34:31 +02002198
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002199 ending:
Christopher Faulet2151cdd2020-07-22 16:34:59 +02002200 res->flags &= ~CF_EXPECT_MORE; /* no more data are expected */
2201
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002202 /* other states, ENDING...TUNNEL */
2203 if (msg->msg_state >= HTTP_MSG_DONE)
2204 goto done;
Christopher Faulet9768c262018-10-22 09:34:31 +02002205
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002206 if (HAS_RSP_DATA_FILTERS(s)) {
2207 ret = flt_http_end(s, msg);
2208 if (ret <= 0) {
2209 if (!ret)
2210 goto missing_data_or_waiting;
2211 goto return_bad_res;
2212 }
2213 }
2214
Christopher Fauletc75668e2020-12-07 18:10:32 +01002215 if ((txn->meth == HTTP_METH_CONNECT && txn->status >= 200 && txn->status < 300) || txn->status == 101 ||
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002216 !(msg->flags & HTTP_MSGF_XFER_LEN)) {
2217 msg->msg_state = HTTP_MSG_TUNNEL;
2218 goto ending;
2219 }
2220 else {
2221 msg->msg_state = HTTP_MSG_DONE;
2222 res->to_forward = 0;
2223 }
2224
2225 done:
2226
2227 channel_dont_close(res);
2228
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002229 http_end_response(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002230 if (!(res->analysers & an_bit)) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002231 http_end_request(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002232 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
2233 if (res->flags & CF_SHUTW) {
2234 /* response errors are most likely due to the
2235 * client aborting the transfer. */
Christopher Faulet93e02d82019-03-08 14:18:50 +01002236 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002237 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02002238 goto return_bad_res;
2239 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002240 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002241 return 1;
2242 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002243 DBG_TRACE_DEVEL("waiting for the end of the HTTP txn",
2244 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002245 return 0;
2246
2247 missing_data_or_waiting:
2248 if (res->flags & CF_SHUTW)
Christopher Faulet93e02d82019-03-08 14:18:50 +01002249 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002250
2251 /* stop waiting for data if the input is closed before the end. If the
2252 * client side was already closed, it means that the client has aborted,
2253 * so we don't want to count this as a server abort. Otherwise it's a
2254 * server abort.
2255 */
Christopher Fauletd20fdb02019-06-13 16:43:22 +02002256 if (msg->msg_state < HTTP_MSG_ENDING && res->flags & CF_SHUTR) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02002257 if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))
Christopher Faulet93e02d82019-03-08 14:18:50 +01002258 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002259 /* If we have some pending data, we continue the processing */
Christopher Faulet93e02d82019-03-08 14:18:50 +01002260 if (htx_is_empty(htx))
2261 goto return_srv_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002262 }
2263
Christopher Faulete0768eb2018-10-03 16:38:02 +02002264 /* When TE: chunked is used, we need to get there again to parse
2265 * remaining chunks even if the server has closed, so we don't want to
Christopher Faulet9768c262018-10-22 09:34:31 +02002266 * set CF_DONTCLOSE. Similarly when there is a content-leng or if there
2267 * are filters registered on the stream, we don't want to forward a
2268 * close
Christopher Faulete0768eb2018-10-03 16:38:02 +02002269 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002270 if ((msg->flags & HTTP_MSGF_XFER_LEN) || HAS_RSP_DATA_FILTERS(s))
Christopher Faulete0768eb2018-10-03 16:38:02 +02002271 channel_dont_close(res);
2272
2273 /* We know that more data are expected, but we couldn't send more that
2274 * what we did. So we always set the CF_EXPECT_MORE flag so that the
2275 * system knows it must not set a PUSH on this first part. Interactive
2276 * modes are already handled by the stream sock layer. We must not do
2277 * this in content-length mode because it could present the MSG_MORE
2278 * flag with the last block of forwarded data, which would cause an
2279 * additional delay to be observed by the receiver.
2280 */
Christopher Faulet2151cdd2020-07-22 16:34:59 +02002281 if (HAS_RSP_DATA_FILTERS(s))
Christopher Faulete0768eb2018-10-03 16:38:02 +02002282 res->flags |= CF_EXPECT_MORE;
2283
2284 /* the stream handler will take care of timeouts and errors */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002285 DBG_TRACE_DEVEL("waiting for more data to forward",
2286 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002287 return 0;
2288
Christopher Faulet93e02d82019-03-08 14:18:50 +01002289 return_srv_abort:
Willy Tarreau4781b152021-04-06 13:53:36 +02002290 _HA_ATOMIC_INC(&sess->fe->fe_counters.srv_aborts);
2291 _HA_ATOMIC_INC(&s->be->be_counters.srv_aborts);
William Lallemand36119de2021-03-08 15:26:48 +01002292 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02002293 _HA_ATOMIC_INC(&sess->listener->counters->srv_aborts);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002294 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02002295 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.srv_aborts);
Willy Tarreau826f3ab2021-02-10 12:07:15 +01002296 stream_inc_http_fail_ctr(s);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002297 if (!(s->flags & SF_ERR_MASK))
2298 s->flags |= SF_ERR_SRVCL;
2299 goto return_error;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002300
Christopher Faulet93e02d82019-03-08 14:18:50 +01002301 return_cli_abort:
Willy Tarreau4781b152021-04-06 13:53:36 +02002302 _HA_ATOMIC_INC(&sess->fe->fe_counters.cli_aborts);
2303 _HA_ATOMIC_INC(&s->be->be_counters.cli_aborts);
William Lallemand36119de2021-03-08 15:26:48 +01002304 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02002305 _HA_ATOMIC_INC(&sess->listener->counters->cli_aborts);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002306 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02002307 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.cli_aborts);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002308 if (!(s->flags & SF_ERR_MASK))
2309 s->flags |= SF_ERR_CLICL;
2310 goto return_error;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002311
Christopher Fauletb9a92f32019-09-09 10:15:21 +02002312 return_int_err:
Willy Tarreau4781b152021-04-06 13:53:36 +02002313 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
2314 _HA_ATOMIC_INC(&s->be->be_counters.internal_errors);
William Lallemand36119de2021-03-08 15:26:48 +01002315 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02002316 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002317 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02002318 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.internal_errors);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02002319 if (!(s->flags & SF_ERR_MASK))
2320 s->flags |= SF_ERR_INTERNAL;
2321 goto return_error;
2322
Christopher Faulet93e02d82019-03-08 14:18:50 +01002323 return_bad_res:
Willy Tarreau4781b152021-04-06 13:53:36 +02002324 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002325 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02002326 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002327 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_RSP);
2328 }
Willy Tarreau826f3ab2021-02-10 12:07:15 +01002329 stream_inc_http_fail_ctr(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002330 if (!(s->flags & SF_ERR_MASK))
Christopher Faulet93e02d82019-03-08 14:18:50 +01002331 s->flags |= SF_ERR_SRVCL;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002332 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +02002333
Christopher Faulet93e02d82019-03-08 14:18:50 +01002334 return_error:
Christopher Faulete0768eb2018-10-03 16:38:02 +02002335 /* don't send any error message as we're in the body */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002336 http_reply_and_close(s, txn->status, NULL);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002337 if (!(s->flags & SF_FINST_MASK))
2338 s->flags |= SF_FINST_D;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002339 DBG_TRACE_DEVEL("leaving on error",
2340 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002341 return 0;
2342}
2343
Christopher Fauletf2824e62018-10-01 12:12:37 +02002344/* Perform an HTTP redirect based on the information in <rule>. The function
Willy Tarreaubc1223b2021-09-02 16:54:33 +02002345 * returns zero in case of an irrecoverable error such as too large a request
2346 * to build a valid response, 1 in case of successful redirect (hence the rule
2347 * is final), or 2 if the rule has to be silently skipped.
Christopher Fauletf2824e62018-10-01 12:12:37 +02002348 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002349int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn)
Christopher Fauletf2824e62018-10-01 12:12:37 +02002350{
Christopher Faulet99daf282018-11-28 22:58:13 +01002351 struct channel *req = &s->req;
2352 struct channel *res = &s->res;
2353 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01002354 struct htx_sl *sl;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002355 struct buffer *chunk;
Christopher Faulet99daf282018-11-28 22:58:13 +01002356 struct ist status, reason, location;
2357 unsigned int flags;
Christopher Fauleteab17572022-04-26 20:34:38 +02002358 int ret = 1, close = 0; /* Try to keep the connection alive byt default */
Christopher Fauletf2824e62018-10-01 12:12:37 +02002359
2360 chunk = alloc_trash_chunk();
Christopher Fauletb8a53712019-12-16 11:29:38 +01002361 if (!chunk) {
2362 if (!(s->flags & SF_ERR_MASK))
2363 s->flags |= SF_ERR_RESOURCE;
Christopher Faulet99daf282018-11-28 22:58:13 +01002364 goto fail;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002365 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002366
Christopher Faulet99daf282018-11-28 22:58:13 +01002367 /*
2368 * Create the location
2369 */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002370 htx = htxbuf(&req->buf);
Christopher Fauletf2824e62018-10-01 12:12:37 +02002371 switch(rule->type) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002372 case REDIRECT_TYPE_SCHEME: {
2373 struct http_hdr_ctx ctx;
2374 struct ist path, host;
Amaury Denoyellec453f952021-07-06 11:40:12 +02002375 struct http_uri_parser parser;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002376
Christopher Faulet99daf282018-11-28 22:58:13 +01002377 host = ist("");
2378 ctx.blk = NULL;
2379 if (http_find_header(htx, ist("Host"), &ctx, 0))
2380 host = ctx.value;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002381
Christopher Faulet297fbb42019-05-13 14:41:27 +02002382 sl = http_get_stline(htx);
Amaury Denoyellec453f952021-07-06 11:40:12 +02002383 parser = http_uri_parser_init(htx_sl_req_uri(sl));
2384 path = http_parse_path(&parser);
Christopher Faulet99daf282018-11-28 22:58:13 +01002385 /* build message using path */
Tim Duesterhused526372020-03-05 17:56:33 +01002386 if (isttest(path)) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002387 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2388 int qs = 0;
2389 while (qs < path.len) {
2390 if (*(path.ptr + qs) == '?') {
2391 path.len = qs;
2392 break;
2393 }
2394 qs++;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002395 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002396 }
2397 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002398 else
2399 path = ist("/");
Christopher Fauletf2824e62018-10-01 12:12:37 +02002400
Christopher Faulet99daf282018-11-28 22:58:13 +01002401 if (rule->rdr_str) { /* this is an old "redirect" rule */
2402 /* add scheme */
2403 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2404 goto fail;
2405 }
2406 else {
2407 /* add scheme with executing log format */
2408 chunk->data += build_logline(s, chunk->area + chunk->data,
2409 chunk->size - chunk->data,
2410 &rule->rdr_fmt);
2411 }
2412 /* add "://" + host + path */
2413 if (!chunk_memcat(chunk, "://", 3) ||
2414 !chunk_memcat(chunk, host.ptr, host.len) ||
2415 !chunk_memcat(chunk, path.ptr, path.len))
2416 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002417
Christopher Faulet99daf282018-11-28 22:58:13 +01002418 /* append a slash at the end of the location if needed and missing */
2419 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
2420 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
2421 if (chunk->data + 1 >= chunk->size)
2422 goto fail;
2423 chunk->area[chunk->data++] = '/';
2424 }
2425 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002426 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002427
Christopher Faulet99daf282018-11-28 22:58:13 +01002428 case REDIRECT_TYPE_PREFIX: {
2429 struct ist path;
Amaury Denoyellec453f952021-07-06 11:40:12 +02002430 struct http_uri_parser parser;
Christopher Faulet99daf282018-11-28 22:58:13 +01002431
Christopher Faulet297fbb42019-05-13 14:41:27 +02002432 sl = http_get_stline(htx);
Amaury Denoyellec453f952021-07-06 11:40:12 +02002433 parser = http_uri_parser_init(htx_sl_req_uri(sl));
2434 path = http_parse_path(&parser);
Christopher Faulet99daf282018-11-28 22:58:13 +01002435 /* build message using path */
Tim Duesterhused526372020-03-05 17:56:33 +01002436 if (isttest(path)) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002437 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2438 int qs = 0;
2439 while (qs < path.len) {
2440 if (*(path.ptr + qs) == '?') {
2441 path.len = qs;
2442 break;
2443 }
2444 qs++;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002445 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002446 }
2447 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002448 else
2449 path = ist("/");
Christopher Fauletf2824e62018-10-01 12:12:37 +02002450
Christopher Faulet99daf282018-11-28 22:58:13 +01002451 if (rule->rdr_str) { /* this is an old "redirect" rule */
2452 /* add prefix. Note that if prefix == "/", we don't want to
2453 * add anything, otherwise it makes it hard for the user to
2454 * configure a self-redirection.
2455 */
2456 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
2457 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2458 goto fail;
2459 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002460 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002461 else {
2462 /* add prefix with executing log format */
2463 chunk->data += build_logline(s, chunk->area + chunk->data,
2464 chunk->size - chunk->data,
2465 &rule->rdr_fmt);
2466 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002467
Christopher Faulet99daf282018-11-28 22:58:13 +01002468 /* add path */
2469 if (!chunk_memcat(chunk, path.ptr, path.len))
2470 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002471
Christopher Faulet99daf282018-11-28 22:58:13 +01002472 /* append a slash at the end of the location if needed and missing */
2473 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
2474 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
2475 if (chunk->data + 1 >= chunk->size)
2476 goto fail;
2477 chunk->area[chunk->data++] = '/';
2478 }
2479 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002480 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002481 case REDIRECT_TYPE_LOCATION:
2482 default:
2483 if (rule->rdr_str) { /* this is an old "redirect" rule */
2484 /* add location */
2485 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2486 goto fail;
2487 }
2488 else {
2489 /* add location with executing log format */
Willy Tarreaubc1223b2021-09-02 16:54:33 +02002490 int len = build_logline(s, chunk->area + chunk->data,
2491 chunk->size - chunk->data,
2492 &rule->rdr_fmt);
Christopher Fauleteab17572022-04-26 20:34:38 +02002493 if (!len && rule->flags & REDIRECT_FLAG_IGNORE_EMPTY) {
2494 ret = 2;
2495 goto out;
2496 }
Willy Tarreaubc1223b2021-09-02 16:54:33 +02002497
2498 chunk->data += len;
Christopher Faulet99daf282018-11-28 22:58:13 +01002499 }
2500 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002501 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002502 location = ist2(chunk->area, chunk->data);
2503
2504 /*
2505 * Create the 30x response
2506 */
2507 switch (rule->code) {
2508 case 308:
2509 status = ist("308");
2510 reason = ist("Permanent Redirect");
2511 break;
2512 case 307:
2513 status = ist("307");
2514 reason = ist("Temporary Redirect");
2515 break;
2516 case 303:
2517 status = ist("303");
2518 reason = ist("See Other");
2519 break;
2520 case 301:
2521 status = ist("301");
2522 reason = ist("Moved Permanently");
2523 break;
2524 case 302:
2525 default:
2526 status = ist("302");
2527 reason = ist("Found");
2528 break;
2529 }
2530
Christopher Faulet08e66462019-05-23 16:44:59 +02002531 if (!(txn->req.flags & HTTP_MSGF_BODYLESS) && txn->req.msg_state != HTTP_MSG_DONE)
2532 close = 1;
2533
Christopher Faulet99daf282018-11-28 22:58:13 +01002534 htx = htx_from_buf(&res->buf);
Kevin Zhu96b36392020-01-07 09:42:55 +01002535 /* Trim any possible response */
2536 channel_htx_truncate(&s->res, htx);
Christopher Faulet99daf282018-11-28 22:58:13 +01002537 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
2538 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), status, reason);
2539 if (!sl)
2540 goto fail;
2541 sl->info.res.status = rule->code;
2542 s->txn->status = rule->code;
2543
Christopher Faulet08e66462019-05-23 16:44:59 +02002544 if (close && !htx_add_header(htx, ist("Connection"), ist("close")))
2545 goto fail;
2546
2547 if (!htx_add_header(htx, ist("Content-length"), ist("0")) ||
Christopher Faulet99daf282018-11-28 22:58:13 +01002548 !htx_add_header(htx, ist("Location"), location))
2549 goto fail;
2550
2551 if (rule->code == 302 || rule->code == 303 || rule->code == 307) {
2552 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")))
2553 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002554 }
2555
2556 if (rule->cookie_len) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002557 if (!htx_add_header(htx, ist("Set-Cookie"), ist2(rule->cookie_str, rule->cookie_len)))
2558 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002559 }
2560
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002561 if (!htx_add_endof(htx, HTX_BLK_EOH))
Christopher Faulet99daf282018-11-28 22:58:13 +01002562 goto fail;
2563
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002564 htx->flags |= HTX_FL_EOM;
Kevin Zhu96b36392020-01-07 09:42:55 +01002565 htx_to_buf(htx, &res->buf);
Christopher Fauleta72a7e42020-01-28 09:28:11 +01002566 if (!http_forward_proxy_resp(s, 1))
2567 goto fail;
Christopher Faulet99daf282018-11-28 22:58:13 +01002568
Christopher Faulet60b33a52020-01-28 09:18:10 +01002569 if (rule->flags & REDIRECT_FLAG_FROM_REQ) {
2570 /* let's log the request time */
2571 s->logs.tv_request = now;
Christopher Fauletd3475882021-10-04 14:16:46 +02002572 req->analysers &= AN_REQ_FLT_END;
Christopher Faulet99daf282018-11-28 22:58:13 +01002573
Christopher Faulet60b33a52020-01-28 09:18:10 +01002574 if (s->sess->fe == s->be) /* report it if the request was intercepted by the frontend */
Willy Tarreau4781b152021-04-06 13:53:36 +02002575 _HA_ATOMIC_INC(&s->sess->fe->fe_counters.intercepted_req);
Christopher Faulet60b33a52020-01-28 09:18:10 +01002576 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002577
2578 if (!(s->flags & SF_ERR_MASK))
2579 s->flags |= SF_ERR_LOCAL;
2580 if (!(s->flags & SF_FINST_MASK))
Christopher Faulet60b33a52020-01-28 09:18:10 +01002581 s->flags |= ((rule->flags & REDIRECT_FLAG_FROM_REQ) ? SF_FINST_R : SF_FINST_H);
Christopher Fauletf2824e62018-10-01 12:12:37 +02002582
Christopher Fauleteab17572022-04-26 20:34:38 +02002583 out:
Christopher Faulet99daf282018-11-28 22:58:13 +01002584 free_trash_chunk(chunk);
Christopher Fauleteab17572022-04-26 20:34:38 +02002585 return ret;
Christopher Faulet99daf282018-11-28 22:58:13 +01002586
2587 fail:
2588 /* If an error occurred, remove the incomplete HTTP response from the
2589 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01002590 channel_htx_truncate(res, htxbuf(&res->buf));
Christopher Fauleteab17572022-04-26 20:34:38 +02002591 ret = 0;
2592 goto out;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002593}
2594
Christopher Faulet18c13d32022-05-16 11:43:10 +02002595/* This function filters the request header names to only allow [0-9a-zA-Z-]
2596 * characters. Depending on the proxy configuration, headers with a name not
2597 * matching this charset are removed or the request is rejected with a
2598 * 403-Forbidden response if such name are found. It returns HTTP_RULE_RES_CONT
2599 * to continue the request processing or HTTP_RULE_RES_DENY if the request is
2600 * rejected.
2601 */
2602static enum rule_result http_req_restrict_header_names(struct stream *s, struct htx *htx, struct proxy *px)
2603{
2604 struct htx_blk *blk;
2605 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
2606
2607 blk = htx_get_first_blk(htx);
2608 while (blk) {
2609 enum htx_blk_type type = htx_get_blk_type(blk);
2610
2611 if (type == HTX_BLK_HDR) {
2612 struct ist n = htx_get_blk_name(htx, blk);
2613 int i;
2614
2615 for (i = 0; i < istlen(n); i++) {
2616 if (!isalnum((unsigned char)n.ptr[i]) && n.ptr[i] != '-') {
2617 /* Block the request or remove the header */
2618 if (px->options2 & PR_O2_RSTRICT_REQ_HDR_NAMES_BLK)
2619 goto block;
2620 blk = htx_remove_blk(htx, blk);
2621 continue;
2622 }
2623 }
2624 }
2625 if (type == HTX_BLK_EOH)
2626 break;
2627
2628 blk = htx_get_next_blk(htx, blk);
2629 }
2630 out:
2631 return rule_ret;
2632 block:
2633 /* Block the request returning a 403-Forbidden response */
2634 s->txn->status = 403;
2635 rule_ret = HTTP_RULE_RES_DENY;
2636 goto out;
2637}
2638
Christopher Faulet92d34fe2019-12-17 09:20:34 +01002639/* Replace all headers matching the name <name>. The header value is replaced if
2640 * it matches the regex <re>. <str> is used for the replacement. If <full> is
2641 * set to 1, the full-line is matched and replaced. Otherwise, comma-separated
2642 * values are evaluated one by one. It returns 0 on success and -1 on error.
2643 */
2644int http_replace_hdrs(struct stream* s, struct htx *htx, struct ist name,
2645 const char *str, struct my_regex *re, int full)
Christopher Faulet72333522018-10-24 11:25:02 +02002646{
2647 struct http_hdr_ctx ctx;
2648 struct buffer *output = get_trash_chunk();
2649
Christopher Faulet72333522018-10-24 11:25:02 +02002650 ctx.blk = NULL;
Christopher Faulet92d34fe2019-12-17 09:20:34 +01002651 while (http_find_header(htx, name, &ctx, full)) {
Christopher Faulet72333522018-10-24 11:25:02 +02002652 if (!regex_exec_match2(re, ctx.value.ptr, ctx.value.len, MAX_MATCH, pmatch, 0))
2653 continue;
2654
2655 output->data = exp_replace(output->area, output->size, ctx.value.ptr, str, pmatch);
2656 if (output->data == -1)
2657 return -1;
2658 if (!http_replace_header_value(htx, &ctx, ist2(output->area, output->data)))
2659 return -1;
2660 }
2661 return 0;
2662}
2663
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002664/* This function executes one of the set-{method,path,query,uri} actions. It
2665 * takes the string from the variable 'replace' with length 'len', then modifies
2666 * the relevant part of the request line accordingly. Then it updates various
2667 * pointers to the next elements which were moved, and the total buffer length.
2668 * It finds the action to be performed in p[2], previously filled by function
2669 * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal
2670 * error, though this can be revisited when this code is finally exploited.
2671 *
2672 * 'action' can be '0' to replace method, '1' to replace path, '2' to replace
Christopher Faulet312294f2020-09-02 17:17:44 +02002673 * query string, 3 to replace uri or 4 to replace the path+query.
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002674 *
2675 * In query string case, the mark question '?' must be set at the start of the
2676 * string by the caller, event if the replacement query string is empty.
2677 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002678int http_req_replace_stline(int action, const char *replace, int len,
2679 struct proxy *px, struct stream *s)
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002680{
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002681 struct htx *htx = htxbuf(&s->req.buf);
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002682
2683 switch (action) {
2684 case 0: // method
2685 if (!http_replace_req_meth(htx, ist2(replace, len)))
2686 return -1;
2687 break;
2688
2689 case 1: // path
Christopher Fauletb8ce5052020-08-31 16:11:57 +02002690 if (!http_replace_req_path(htx, ist2(replace, len), 0))
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002691 return -1;
2692 break;
2693
2694 case 2: // query
2695 if (!http_replace_req_query(htx, ist2(replace, len)))
2696 return -1;
2697 break;
2698
2699 case 3: // uri
2700 if (!http_replace_req_uri(htx, ist2(replace, len)))
2701 return -1;
2702 break;
2703
Christopher Faulet312294f2020-09-02 17:17:44 +02002704 case 4: // path + query
2705 if (!http_replace_req_path(htx, ist2(replace, len), 1))
2706 return -1;
2707 break;
2708
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002709 default:
2710 return -1;
2711 }
2712 return 0;
2713}
2714
2715/* This function replace the HTTP status code and the associated message. The
Christopher Faulete00d06c2019-12-16 17:18:42 +01002716 * variable <status> contains the new status code. This function never fails. It
2717 * returns 0 in case of success, -1 in case of internal error.
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002718 */
Christopher Faulet96bff762019-12-17 13:46:18 +01002719int http_res_set_status(unsigned int status, struct ist reason, struct stream *s)
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002720{
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002721 struct htx *htx = htxbuf(&s->res.buf);
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002722 char *res;
2723
2724 chunk_reset(&trash);
2725 res = ultoa_o(status, trash.area, trash.size);
2726 trash.data = res - trash.area;
2727
2728 /* Do we have a custom reason format string? */
Tim Duesterhuse296d3e2020-03-05 17:56:31 +01002729 if (!isttest(reason)) {
Christopher Faulet96bff762019-12-17 13:46:18 +01002730 const char *str = http_get_reason(status);
Tim Duesterhusdcf753a2021-03-04 17:31:47 +01002731 reason = ist(str);
Christopher Faulet96bff762019-12-17 13:46:18 +01002732 }
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002733
Christopher Fauletbde2c4c2020-08-31 16:43:34 +02002734 if (!http_replace_res_status(htx, ist2(trash.area, trash.data), reason))
Christopher Faulete00d06c2019-12-16 17:18:42 +01002735 return -1;
2736 return 0;
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002737}
2738
Christopher Faulet3e964192018-10-24 11:39:23 +02002739/* Executes the http-request rules <rules> for stream <s>, proxy <px> and
2740 * transaction <txn>. Returns the verdict of the first rule that prevents
2741 * further processing of the request (auth, deny, ...), and defaults to
2742 * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or
2743 * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT
2744 * on txn->flags if it encounters a tarpit rule. If <deny_status> is not NULL
2745 * and a deny/tarpit rule is matched, it will be filled with this rule's deny
2746 * status.
2747 */
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002748static enum rule_result http_req_get_intercept_rule(struct proxy *px, struct list *def_rules,
2749 struct list *rules, struct stream *s)
Christopher Faulet3e964192018-10-24 11:39:23 +02002750{
2751 struct session *sess = strm_sess(s);
2752 struct http_txn *txn = s->txn;
Christopher Faulet3e964192018-10-24 11:39:23 +02002753 struct act_rule *rule;
Christopher Faulet3e964192018-10-24 11:39:23 +02002754 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002755 int act_opts = 0;
Christopher Faulet3e964192018-10-24 11:39:23 +02002756
Christopher Faulet3e964192018-10-24 11:39:23 +02002757 /* If "the current_rule_list" match the executed rule list, we are in
2758 * resume condition. If a resume is needed it is always in the action
2759 * and never in the ACL or converters. In this case, we initialise the
2760 * current rule, and go to the action execution point.
2761 */
2762 if (s->current_rule) {
2763 rule = s->current_rule;
2764 s->current_rule = NULL;
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002765 if (s->current_rule_list == rules || (def_rules && s->current_rule_list == def_rules))
Christopher Faulet3e964192018-10-24 11:39:23 +02002766 goto resume_execution;
2767 }
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002768 s->current_rule_list = ((!def_rules || s->current_rule_list == def_rules) ? rules : def_rules);
Christopher Faulet3e964192018-10-24 11:39:23 +02002769
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002770 restart:
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002771 /* start the ruleset evaluation in strict mode */
2772 txn->req.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01002773
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002774 list_for_each_entry(rule, s->current_rule_list, list) {
Christopher Faulet3e964192018-10-24 11:39:23 +02002775 /* check optional condition */
2776 if (rule->cond) {
2777 int ret;
2778
2779 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
2780 ret = acl_pass(ret);
2781
2782 if (rule->cond->pol == ACL_COND_UNLESS)
2783 ret = !ret;
2784
2785 if (!ret) /* condition not matched */
2786 continue;
2787 }
2788
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002789 act_opts |= ACT_OPT_FIRST;
Christopher Faulet3e964192018-10-24 11:39:23 +02002790 resume_execution:
Amaury Denoyelle03517732021-05-07 14:25:01 +02002791 if (rule->kw->flags & KWF_EXPERIMENTAL)
2792 mark_tainted(TAINTED_ACTION_EXP_EXECUTED);
2793
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002794 /* Always call the action function if defined */
2795 if (rule->action_ptr) {
2796 if ((s->req.flags & CF_READ_ERROR) ||
2797 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
2798 (px->options & PR_O_ABRT_CLOSE)))
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002799 act_opts |= ACT_OPT_FINAL;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002800
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002801 switch (rule->action_ptr(rule, px, sess, s, act_opts)) {
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002802 case ACT_RET_CONT:
2803 break;
2804 case ACT_RET_STOP:
2805 rule_ret = HTTP_RULE_RES_STOP;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002806 s->last_rule_file = rule->conf.file;
2807 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002808 goto end;
2809 case ACT_RET_YIELD:
2810 s->current_rule = rule;
2811 rule_ret = HTTP_RULE_RES_YIELD;
2812 goto end;
2813 case ACT_RET_ERR:
2814 rule_ret = HTTP_RULE_RES_ERROR;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002815 s->last_rule_file = rule->conf.file;
2816 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002817 goto end;
2818 case ACT_RET_DONE:
2819 rule_ret = HTTP_RULE_RES_DONE;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002820 s->last_rule_file = rule->conf.file;
2821 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002822 goto end;
2823 case ACT_RET_DENY:
Christopher Fauletb58f62b2020-01-13 16:40:13 +01002824 if (txn->status == -1)
2825 txn->status = 403;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002826 rule_ret = HTTP_RULE_RES_DENY;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002827 s->last_rule_file = rule->conf.file;
2828 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002829 goto end;
2830 case ACT_RET_ABRT:
2831 rule_ret = HTTP_RULE_RES_ABRT;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002832 s->last_rule_file = rule->conf.file;
2833 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002834 goto end;
2835 case ACT_RET_INV:
2836 rule_ret = HTTP_RULE_RES_BADREQ;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002837 s->last_rule_file = rule->conf.file;
2838 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002839 goto end;
2840 }
2841 continue; /* eval the next rule */
2842 }
2843
2844 /* If not action function defined, check for known actions */
Christopher Faulet3e964192018-10-24 11:39:23 +02002845 switch (rule->action) {
2846 case ACT_ACTION_ALLOW:
2847 rule_ret = HTTP_RULE_RES_STOP;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002848 s->last_rule_file = rule->conf.file;
2849 s->last_rule_line = rule->conf.line;
Christopher Faulet3e964192018-10-24 11:39:23 +02002850 goto end;
2851
2852 case ACT_ACTION_DENY:
Christopher Faulet5cb513a2020-05-13 17:56:56 +02002853 txn->status = rule->arg.http_reply->status;
2854 txn->http_reply = rule->arg.http_reply;
Christopher Faulet3e964192018-10-24 11:39:23 +02002855 rule_ret = HTTP_RULE_RES_DENY;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002856 s->last_rule_file = rule->conf.file;
2857 s->last_rule_line = rule->conf.line;
Christopher Faulet3e964192018-10-24 11:39:23 +02002858 goto end;
2859
2860 case ACT_HTTP_REQ_TARPIT:
2861 txn->flags |= TX_CLTARPIT;
Christopher Faulet5cb513a2020-05-13 17:56:56 +02002862 txn->status = rule->arg.http_reply->status;
2863 txn->http_reply = rule->arg.http_reply;
Christopher Faulet3e964192018-10-24 11:39:23 +02002864 rule_ret = HTTP_RULE_RES_DENY;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002865 s->last_rule_file = rule->conf.file;
2866 s->last_rule_line = rule->conf.line;
Christopher Faulet3e964192018-10-24 11:39:23 +02002867 goto end;
2868
Willy Tarreaubc1223b2021-09-02 16:54:33 +02002869 case ACT_HTTP_REDIR: {
2870 int ret = http_apply_redirect_rule(rule->arg.redir, s, txn);
2871
2872 if (ret == 2) // 2 == skip
2873 break;
2874
2875 rule_ret = ret ? HTTP_RULE_RES_ABRT : HTTP_RULE_RES_ERROR;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002876 s->last_rule_file = rule->conf.file;
2877 s->last_rule_line = rule->conf.line;
Christopher Faulet3e964192018-10-24 11:39:23 +02002878 goto end;
Willy Tarreaubc1223b2021-09-02 16:54:33 +02002879 }
Christopher Faulet3e964192018-10-24 11:39:23 +02002880
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002881 /* other flags exists, but normally, they never be matched. */
Christopher Faulet3e964192018-10-24 11:39:23 +02002882 default:
2883 break;
2884 }
2885 }
2886
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002887 if (def_rules && s->current_rule_list == def_rules) {
2888 s->current_rule_list = rules;
2889 goto restart;
2890 }
2891
Christopher Faulet3e964192018-10-24 11:39:23 +02002892 end:
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002893 /* if the ruleset evaluation is finished reset the strict mode */
Christopher Faulet46f95542019-12-20 10:07:22 +01002894 if (rule_ret != HTTP_RULE_RES_YIELD)
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002895 txn->req.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01002896
Christopher Faulet3e964192018-10-24 11:39:23 +02002897 /* we reached the end of the rules, nothing to report */
2898 return rule_ret;
2899}
2900
2901/* Executes the http-response rules <rules> for stream <s> and proxy <px>. It
2902 * returns one of 5 possible statuses: HTTP_RULE_RES_CONT, HTTP_RULE_RES_STOP,
2903 * HTTP_RULE_RES_DONE, HTTP_RULE_RES_YIELD, or HTTP_RULE_RES_BADREQ. If *CONT
2904 * is returned, the process can continue the evaluation of next rule list. If
2905 * *STOP or *DONE is returned, the process must stop the evaluation. If *BADREQ
2906 * is returned, it means the operation could not be processed and a server error
Christopher Fauleta53abad2020-05-13 08:12:22 +02002907 * must be returned. If *YIELD is returned, the caller must call again the
2908 * function with the same context.
Christopher Faulet3e964192018-10-24 11:39:23 +02002909 */
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002910static enum rule_result http_res_get_intercept_rule(struct proxy *px, struct list *def_rules,
2911 struct list *rules, struct stream *s)
Christopher Faulet3e964192018-10-24 11:39:23 +02002912{
2913 struct session *sess = strm_sess(s);
2914 struct http_txn *txn = s->txn;
Christopher Faulet3e964192018-10-24 11:39:23 +02002915 struct act_rule *rule;
Christopher Faulet3e964192018-10-24 11:39:23 +02002916 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002917 int act_opts = 0;
Christopher Faulet3e964192018-10-24 11:39:23 +02002918
Christopher Faulet3e964192018-10-24 11:39:23 +02002919 /* If "the current_rule_list" match the executed rule list, we are in
2920 * resume condition. If a resume is needed it is always in the action
2921 * and never in the ACL or converters. In this case, we initialise the
2922 * current rule, and go to the action execution point.
2923 */
2924 if (s->current_rule) {
2925 rule = s->current_rule;
2926 s->current_rule = NULL;
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002927 if (s->current_rule_list == rules || (def_rules && s->current_rule_list == def_rules))
Christopher Faulet3e964192018-10-24 11:39:23 +02002928 goto resume_execution;
2929 }
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002930 s->current_rule_list = ((!def_rules || s->current_rule_list == def_rules) ? rules : def_rules);
2931
2932 restart:
Christopher Faulet3e964192018-10-24 11:39:23 +02002933
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002934 /* start the ruleset evaluation in strict mode */
2935 txn->rsp.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01002936
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002937 list_for_each_entry(rule, s->current_rule_list, list) {
Christopher Faulet3e964192018-10-24 11:39:23 +02002938 /* check optional condition */
2939 if (rule->cond) {
2940 int ret;
2941
2942 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
2943 ret = acl_pass(ret);
2944
2945 if (rule->cond->pol == ACL_COND_UNLESS)
2946 ret = !ret;
2947
2948 if (!ret) /* condition not matched */
2949 continue;
2950 }
2951
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002952 act_opts |= ACT_OPT_FIRST;
Christopher Faulet3e964192018-10-24 11:39:23 +02002953resume_execution:
Amaury Denoyelle03517732021-05-07 14:25:01 +02002954 if (rule->kw->flags & KWF_EXPERIMENTAL)
2955 mark_tainted(TAINTED_ACTION_EXP_EXECUTED);
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002956
2957 /* Always call the action function if defined */
2958 if (rule->action_ptr) {
2959 if ((s->req.flags & CF_READ_ERROR) ||
2960 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
2961 (px->options & PR_O_ABRT_CLOSE)))
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002962 act_opts |= ACT_OPT_FINAL;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002963
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002964 switch (rule->action_ptr(rule, px, sess, s, act_opts)) {
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002965 case ACT_RET_CONT:
2966 break;
2967 case ACT_RET_STOP:
2968 rule_ret = HTTP_RULE_RES_STOP;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002969 s->last_rule_file = rule->conf.file;
2970 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002971 goto end;
2972 case ACT_RET_YIELD:
2973 s->current_rule = rule;
2974 rule_ret = HTTP_RULE_RES_YIELD;
2975 goto end;
2976 case ACT_RET_ERR:
2977 rule_ret = HTTP_RULE_RES_ERROR;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002978 s->last_rule_file = rule->conf.file;
2979 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002980 goto end;
2981 case ACT_RET_DONE:
2982 rule_ret = HTTP_RULE_RES_DONE;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002983 s->last_rule_file = rule->conf.file;
2984 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002985 goto end;
2986 case ACT_RET_DENY:
Christopher Fauletb58f62b2020-01-13 16:40:13 +01002987 if (txn->status == -1)
2988 txn->status = 502;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002989 rule_ret = HTTP_RULE_RES_DENY;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002990 s->last_rule_file = rule->conf.file;
2991 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002992 goto end;
2993 case ACT_RET_ABRT:
2994 rule_ret = HTTP_RULE_RES_ABRT;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002995 s->last_rule_file = rule->conf.file;
2996 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002997 goto end;
2998 case ACT_RET_INV:
2999 rule_ret = HTTP_RULE_RES_BADREQ;
Willy Tarreauc6dae862022-03-09 17:23:10 +01003000 s->last_rule_file = rule->conf.file;
3001 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01003002 goto end;
3003 }
3004 continue; /* eval the next rule */
3005 }
3006
3007 /* If not action function defined, check for known actions */
Christopher Faulet3e964192018-10-24 11:39:23 +02003008 switch (rule->action) {
3009 case ACT_ACTION_ALLOW:
3010 rule_ret = HTTP_RULE_RES_STOP; /* "allow" rules are OK */
Willy Tarreauc6dae862022-03-09 17:23:10 +01003011 s->last_rule_file = rule->conf.file;
3012 s->last_rule_line = rule->conf.line;
Christopher Faulet3e964192018-10-24 11:39:23 +02003013 goto end;
3014
3015 case ACT_ACTION_DENY:
Christopher Faulet5cb513a2020-05-13 17:56:56 +02003016 txn->status = rule->arg.http_reply->status;
3017 txn->http_reply = rule->arg.http_reply;
Christopher Faulet3a26bee2019-12-16 12:47:40 +01003018 rule_ret = HTTP_RULE_RES_DENY;
Willy Tarreauc6dae862022-03-09 17:23:10 +01003019 s->last_rule_file = rule->conf.file;
3020 s->last_rule_line = rule->conf.line;
Christopher Faulet3e964192018-10-24 11:39:23 +02003021 goto end;
3022
Willy Tarreaubc1223b2021-09-02 16:54:33 +02003023 case ACT_HTTP_REDIR: {
3024 int ret = http_apply_redirect_rule(rule->arg.redir, s, txn);
Christopher Faulet3e964192018-10-24 11:39:23 +02003025
Willy Tarreaubc1223b2021-09-02 16:54:33 +02003026 if (ret == 2) // 2 == skip
3027 break;
3028
3029 rule_ret = ret ? HTTP_RULE_RES_ABRT : HTTP_RULE_RES_ERROR;
Willy Tarreauc6dae862022-03-09 17:23:10 +01003030 s->last_rule_file = rule->conf.file;
3031 s->last_rule_line = rule->conf.line;
Willy Tarreaubc1223b2021-09-02 16:54:33 +02003032 goto end;
3033 }
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01003034 /* other flags exists, but normally, they never be matched. */
Christopher Faulet3e964192018-10-24 11:39:23 +02003035 default:
3036 break;
3037 }
3038 }
3039
Christopher Fauletd4150ad2021-10-13 15:35:55 +02003040 if (def_rules && s->current_rule_list == def_rules) {
3041 s->current_rule_list = rules;
3042 goto restart;
3043 }
3044
Christopher Faulet3e964192018-10-24 11:39:23 +02003045 end:
Christopher Faulet1aea50e2020-01-17 16:03:53 +01003046 /* if the ruleset evaluation is finished reset the strict mode */
Christopher Faulet46f95542019-12-20 10:07:22 +01003047 if (rule_ret != HTTP_RULE_RES_YIELD)
Christopher Faulet1aea50e2020-01-17 16:03:53 +01003048 txn->rsp.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01003049
Christopher Faulet3e964192018-10-24 11:39:23 +02003050 /* we reached the end of the rules, nothing to report */
3051 return rule_ret;
3052}
3053
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01003054/* Executes backend and frontend http-after-response rules for the stream <s>,
3055 * in that order. it return 1 on success and 0 on error. It is the caller
3056 * responsibility to catch error or ignore it. If it catches it, this function
3057 * may be called a second time, for the internal error.
3058 */
3059int http_eval_after_res_rules(struct stream *s)
3060{
Christopher Fauletd4150ad2021-10-13 15:35:55 +02003061 struct list *def_rules, *rules;
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01003062 struct session *sess = s->sess;
3063 enum rule_result ret = HTTP_RULE_RES_CONT;
3064
Christopher Faulet507479b2020-05-15 12:29:46 +02003065 /* Eval after-response ruleset only if the reply is not const */
3066 if (s->txn->flags & TX_CONST_REPLY)
3067 goto end;
3068
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01003069 /* prune the request variables if not already done and swap to the response variables. */
3070 if (s->vars_reqres.scope != SCOPE_RES) {
3071 if (!LIST_ISEMPTY(&s->vars_reqres.head))
3072 vars_prune(&s->vars_reqres, s->sess, s);
Willy Tarreaub7bfcb32021-08-31 08:13:25 +02003073 vars_init_head(&s->vars_reqres, SCOPE_RES);
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01003074 }
3075
Christopher Fauletd4150ad2021-10-13 15:35:55 +02003076 def_rules = (s->be->defpx ? &s->be->defpx->http_after_res_rules : NULL);
3077 rules = &s->be->http_after_res_rules;
3078
3079 ret = http_res_get_intercept_rule(s->be, def_rules, rules, s);
Christopher Faulet4c5a5912021-11-09 17:48:39 +01003080 if ((ret == HTTP_RULE_RES_CONT || ret == HTTP_RULE_RES_STOP) && sess->fe != s->be) {
Christopher Fauletd4150ad2021-10-13 15:35:55 +02003081 def_rules = ((sess->fe->defpx && sess->fe->defpx != s->be->defpx) ? &sess->fe->defpx->http_after_res_rules : NULL);
3082 rules = &sess->fe->http_after_res_rules;
3083 ret = http_res_get_intercept_rule(sess->fe, def_rules, rules, s);
3084 }
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01003085
Christopher Faulet507479b2020-05-15 12:29:46 +02003086 end:
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01003087 /* All other codes than CONTINUE, STOP or DONE are forbidden */
3088 return (ret == HTTP_RULE_RES_CONT || ret == HTTP_RULE_RES_STOP || ret == HTTP_RULE_RES_DONE);
3089}
3090
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003091/*
3092 * Manage client-side cookie. It can impact performance by about 2% so it is
3093 * desirable to call it only when needed. This code is quite complex because
3094 * of the multiple very crappy and ambiguous syntaxes we have to support. it
3095 * highly recommended not to touch this part without a good reason !
3096 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003097static void http_manage_client_side_cookies(struct stream *s, struct channel *req)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003098{
3099 struct session *sess = s->sess;
3100 struct http_txn *txn = s->txn;
3101 struct htx *htx;
3102 struct http_hdr_ctx ctx;
3103 char *hdr_beg, *hdr_end, *del_from;
3104 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
3105 int preserve_hdr;
3106
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003107 htx = htxbuf(&req->buf);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003108 ctx.blk = NULL;
3109 while (http_find_header(htx, ist("Cookie"), &ctx, 1)) {
Olivier Houchardf0f42382019-07-22 17:43:46 +02003110 int is_first = 1;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003111 del_from = NULL; /* nothing to be deleted */
3112 preserve_hdr = 0; /* assume we may kill the whole header */
3113
3114 /* Now look for cookies. Conforming to RFC2109, we have to support
3115 * attributes whose name begin with a '$', and associate them with
3116 * the right cookie, if we want to delete this cookie.
3117 * So there are 3 cases for each cookie read :
3118 * 1) it's a special attribute, beginning with a '$' : ignore it.
3119 * 2) it's a server id cookie that we *MAY* want to delete : save
3120 * some pointers on it (last semi-colon, beginning of cookie...)
3121 * 3) it's an application cookie : we *MAY* have to delete a previous
3122 * "special" cookie.
3123 * At the end of loop, if a "special" cookie remains, we may have to
3124 * remove it. If no application cookie persists in the header, we
3125 * *MUST* delete it.
3126 *
3127 * Note: RFC2965 is unclear about the processing of spaces around
3128 * the equal sign in the ATTR=VALUE form. A careful inspection of
3129 * the RFC explicitly allows spaces before it, and not within the
3130 * tokens (attrs or values). An inspection of RFC2109 allows that
3131 * too but section 10.1.3 lets one think that spaces may be allowed
3132 * after the equal sign too, resulting in some (rare) buggy
3133 * implementations trying to do that. So let's do what servers do.
3134 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
3135 * allowed quoted strings in values, with any possible character
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003136 * after a backslash, including control chars and delimiters, which
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003137 * causes parsing to become ambiguous. Browsers also allow spaces
3138 * within values even without quotes.
3139 *
3140 * We have to keep multiple pointers in order to support cookie
3141 * removal at the beginning, middle or end of header without
3142 * corrupting the header. All of these headers are valid :
3143 *
3144 * hdr_beg hdr_end
3145 * | |
3146 * v |
3147 * NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3 |
3148 * NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3 v
3149 * NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3
3150 * | | | | | | |
3151 * | | | | | | |
3152 * | | | | | | +--> next
3153 * | | | | | +----> val_end
3154 * | | | | +-----------> val_beg
3155 * | | | +--------------> equal
3156 * | | +----------------> att_end
3157 * | +---------------------> att_beg
3158 * +--------------------------> prev
3159 *
3160 */
3161 hdr_beg = ctx.value.ptr;
3162 hdr_end = hdr_beg + ctx.value.len;
3163 for (prev = hdr_beg; prev < hdr_end; prev = next) {
3164 /* Iterate through all cookies on this line */
3165
3166 /* find att_beg */
3167 att_beg = prev;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003168 if (!is_first)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003169 att_beg++;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003170 is_first = 0;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003171
3172 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
3173 att_beg++;
3174
3175 /* find att_end : this is the first character after the last non
3176 * space before the equal. It may be equal to hdr_end.
3177 */
3178 equal = att_end = att_beg;
3179 while (equal < hdr_end) {
3180 if (*equal == '=' || *equal == ',' || *equal == ';')
3181 break;
3182 if (HTTP_IS_SPHT(*equal++))
3183 continue;
3184 att_end = equal;
3185 }
3186
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003187 /* here, <equal> points to '=', a delimiter or the end. <att_end>
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003188 * is between <att_beg> and <equal>, both may be identical.
3189 */
3190 /* look for end of cookie if there is an equal sign */
3191 if (equal < hdr_end && *equal == '=') {
3192 /* look for the beginning of the value */
3193 val_beg = equal + 1;
3194 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
3195 val_beg++;
3196
3197 /* find the end of the value, respecting quotes */
3198 next = http_find_cookie_value_end(val_beg, hdr_end);
3199
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003200 /* make val_end point to the first white space or delimiter after the value */
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003201 val_end = next;
3202 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
3203 val_end--;
3204 }
3205 else
3206 val_beg = val_end = next = equal;
3207
3208 /* We have nothing to do with attributes beginning with
3209 * '$'. However, they will automatically be removed if a
3210 * header before them is removed, since they're supposed
3211 * to be linked together.
3212 */
3213 if (*att_beg == '$')
3214 continue;
3215
3216 /* Ignore cookies with no equal sign */
3217 if (equal == next) {
3218 /* This is not our cookie, so we must preserve it. But if we already
3219 * scheduled another cookie for removal, we cannot remove the
3220 * complete header, but we can remove the previous block itself.
3221 */
3222 preserve_hdr = 1;
3223 if (del_from != NULL) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003224 int delta = http_del_hdr_value(hdr_beg, hdr_end, &del_from, prev);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003225 val_end += delta;
3226 next += delta;
3227 hdr_end += delta;
3228 prev = del_from;
3229 del_from = NULL;
3230 }
3231 continue;
3232 }
3233
3234 /* if there are spaces around the equal sign, we need to
3235 * strip them otherwise we'll get trouble for cookie captures,
3236 * or even for rewrites. Since this happens extremely rarely,
3237 * it does not hurt performance.
3238 */
3239 if (unlikely(att_end != equal || val_beg > equal + 1)) {
3240 int stripped_before = 0;
3241 int stripped_after = 0;
3242
3243 if (att_end != equal) {
3244 memmove(att_end, equal, hdr_end - equal);
3245 stripped_before = (att_end - equal);
3246 equal += stripped_before;
3247 val_beg += stripped_before;
3248 }
3249
3250 if (val_beg > equal + 1) {
3251 memmove(equal + 1, val_beg, hdr_end + stripped_before - val_beg);
3252 stripped_after = (equal + 1) - val_beg;
3253 val_beg += stripped_after;
3254 stripped_before += stripped_after;
3255 }
3256
3257 val_end += stripped_before;
3258 next += stripped_before;
3259 hdr_end += stripped_before;
3260 }
3261 /* now everything is as on the diagram above */
3262
3263 /* First, let's see if we want to capture this cookie. We check
3264 * that we don't already have a client side cookie, because we
3265 * can only capture one. Also as an optimisation, we ignore
3266 * cookies shorter than the declared name.
3267 */
3268 if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL &&
3269 (val_end - att_beg >= sess->fe->capture_namelen) &&
3270 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
3271 int log_len = val_end - att_beg;
3272
3273 if ((txn->cli_cookie = pool_alloc(pool_head_capture)) == NULL) {
3274 ha_alert("HTTP logging : out of memory.\n");
3275 } else {
3276 if (log_len > sess->fe->capture_len)
3277 log_len = sess->fe->capture_len;
3278 memcpy(txn->cli_cookie, att_beg, log_len);
3279 txn->cli_cookie[log_len] = 0;
3280 }
3281 }
3282
3283 /* Persistence cookies in passive, rewrite or insert mode have the
3284 * following form :
3285 *
3286 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
3287 *
3288 * For cookies in prefix mode, the form is :
3289 *
3290 * Cookie: NAME=SRV~VALUE
3291 */
3292 if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
3293 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
3294 struct server *srv = s->be->srv;
3295 char *delim;
3296
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003297 /* if we're in cookie prefix mode, we'll search the delimiter so that we
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003298 * have the server ID between val_beg and delim, and the original cookie between
3299 * delim+1 and val_end. Otherwise, delim==val_end :
3300 *
3301 * hdr_beg
3302 * |
3303 * v
3304 * NAME=SRV; # in all but prefix modes
3305 * NAME=SRV~OPAQUE ; # in prefix mode
3306 * || || | |+-> next
3307 * || || | +--> val_end
3308 * || || +---------> delim
3309 * || |+------------> val_beg
3310 * || +-------------> att_end = equal
3311 * |+-----------------> att_beg
3312 * +------------------> prev
3313 *
3314 */
3315 if (s->be->ck_opts & PR_CK_PFX) {
3316 for (delim = val_beg; delim < val_end; delim++)
3317 if (*delim == COOKIE_DELIM)
3318 break;
3319 }
3320 else {
3321 char *vbar1;
3322 delim = val_end;
3323 /* Now check if the cookie contains a date field, which would
3324 * appear after a vertical bar ('|') just after the server name
3325 * and before the delimiter.
3326 */
3327 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
3328 if (vbar1) {
3329 /* OK, so left of the bar is the server's cookie and
3330 * right is the last seen date. It is a base64 encoded
3331 * 30-bit value representing the UNIX date since the
3332 * epoch in 4-second quantities.
3333 */
3334 int val;
3335 delim = vbar1++;
3336 if (val_end - vbar1 >= 5) {
3337 val = b64tos30(vbar1);
3338 if (val > 0)
3339 txn->cookie_last_date = val << 2;
3340 }
3341 /* look for a second vertical bar */
3342 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
3343 if (vbar1 && (val_end - vbar1 > 5)) {
3344 val = b64tos30(vbar1 + 1);
3345 if (val > 0)
3346 txn->cookie_first_date = val << 2;
3347 }
3348 }
3349 }
3350
3351 /* if the cookie has an expiration date and the proxy wants to check
3352 * it, then we do that now. We first check if the cookie is too old,
3353 * then only if it has expired. We detect strict overflow because the
3354 * time resolution here is not great (4 seconds). Cookies with dates
3355 * in the future are ignored if their offset is beyond one day. This
3356 * allows an admin to fix timezone issues without expiring everyone
3357 * and at the same time avoids keeping unwanted side effects for too
3358 * long.
3359 */
3360 if (txn->cookie_first_date && s->be->cookie_maxlife &&
3361 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) ||
3362 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
3363 txn->flags &= ~TX_CK_MASK;
3364 txn->flags |= TX_CK_OLD;
3365 delim = val_beg; // let's pretend we have not found the cookie
3366 txn->cookie_first_date = 0;
3367 txn->cookie_last_date = 0;
3368 }
3369 else if (txn->cookie_last_date && s->be->cookie_maxidle &&
3370 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) ||
3371 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
3372 txn->flags &= ~TX_CK_MASK;
3373 txn->flags |= TX_CK_EXPIRED;
3374 delim = val_beg; // let's pretend we have not found the cookie
3375 txn->cookie_first_date = 0;
3376 txn->cookie_last_date = 0;
3377 }
3378
3379 /* Here, we'll look for the first running server which supports the cookie.
3380 * This allows to share a same cookie between several servers, for example
3381 * to dedicate backup servers to specific servers only.
3382 * However, to prevent clients from sticking to cookie-less backup server
3383 * when they have incidentely learned an empty cookie, we simply ignore
3384 * empty cookies and mark them as invalid.
3385 * The same behaviour is applied when persistence must be ignored.
3386 */
3387 if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
3388 srv = NULL;
3389
3390 while (srv) {
3391 if (srv->cookie && (srv->cklen == delim - val_beg) &&
3392 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
3393 if ((srv->cur_state != SRV_ST_STOPPED) ||
3394 (s->be->options & PR_O_PERSIST) ||
3395 (s->flags & SF_FORCE_PRST)) {
3396 /* we found the server and we can use it */
3397 txn->flags &= ~TX_CK_MASK;
3398 txn->flags |= (srv->cur_state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
3399 s->flags |= SF_DIRECT | SF_ASSIGNED;
3400 s->target = &srv->obj_type;
3401 break;
3402 } else {
3403 /* we found a server, but it's down,
3404 * mark it as such and go on in case
3405 * another one is available.
3406 */
3407 txn->flags &= ~TX_CK_MASK;
3408 txn->flags |= TX_CK_DOWN;
3409 }
3410 }
3411 srv = srv->next;
3412 }
3413
3414 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
3415 /* no server matched this cookie or we deliberately skipped it */
3416 txn->flags &= ~TX_CK_MASK;
3417 if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
3418 txn->flags |= TX_CK_UNUSED;
3419 else
3420 txn->flags |= TX_CK_INVALID;
3421 }
3422
3423 /* depending on the cookie mode, we may have to either :
3424 * - delete the complete cookie if we're in insert+indirect mode, so that
3425 * the server never sees it ;
3426 * - remove the server id from the cookie value, and tag the cookie as an
Joseph Herlante9d5c722018-11-25 11:00:25 -08003427 * application cookie so that it does not get accidentally removed later,
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003428 * if we're in cookie prefix mode
3429 */
3430 if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
3431 int delta; /* negative */
3432
3433 memmove(val_beg, delim + 1, hdr_end - (delim + 1));
3434 delta = val_beg - (delim + 1);
3435 val_end += delta;
3436 next += delta;
3437 hdr_end += delta;
3438 del_from = NULL;
3439 preserve_hdr = 1; /* we want to keep this cookie */
3440 }
3441 else if (del_from == NULL &&
3442 (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
3443 del_from = prev;
3444 }
3445 }
3446 else {
3447 /* This is not our cookie, so we must preserve it. But if we already
3448 * scheduled another cookie for removal, we cannot remove the
3449 * complete header, but we can remove the previous block itself.
3450 */
3451 preserve_hdr = 1;
3452
3453 if (del_from != NULL) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003454 int delta = http_del_hdr_value(hdr_beg, hdr_end, &del_from, prev);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003455 if (att_beg >= del_from)
3456 att_beg += delta;
3457 if (att_end >= del_from)
3458 att_end += delta;
3459 val_beg += delta;
3460 val_end += delta;
3461 next += delta;
3462 hdr_end += delta;
3463 prev = del_from;
3464 del_from = NULL;
3465 }
3466 }
3467
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003468 } /* for each cookie */
3469
3470
3471 /* There are no more cookies on this line.
3472 * We may still have one (or several) marked for deletion at the
3473 * end of the line. We must do this now in two ways :
3474 * - if some cookies must be preserved, we only delete from the
3475 * mark to the end of line ;
3476 * - if nothing needs to be preserved, simply delete the whole header
3477 */
3478 if (del_from) {
3479 hdr_end = (preserve_hdr ? del_from : hdr_beg);
3480 }
3481 if ((hdr_end - hdr_beg) != ctx.value.len) {
Christopher Faulet3e2638e2019-06-18 09:49:16 +02003482 if (hdr_beg != hdr_end)
3483 htx_change_blk_value_len(htx, ctx.blk, hdr_end - hdr_beg);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003484 else
3485 http_remove_header(htx, &ctx);
3486 }
3487 } /* for each "Cookie header */
3488}
3489
3490/*
3491 * Manage server-side cookies. It can impact performance by about 2% so it is
3492 * desirable to call it only when needed. This function is also used when we
3493 * just need to know if there is a cookie (eg: for check-cache).
3494 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003495static void http_manage_server_side_cookies(struct stream *s, struct channel *res)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003496{
3497 struct session *sess = s->sess;
3498 struct http_txn *txn = s->txn;
3499 struct htx *htx;
3500 struct http_hdr_ctx ctx;
3501 struct server *srv;
3502 char *hdr_beg, *hdr_end;
3503 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau6f7a02a2019-04-15 21:49:49 +02003504 int is_cookie2 = 0;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003505
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003506 htx = htxbuf(&res->buf);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003507
3508 ctx.blk = NULL;
3509 while (1) {
Olivier Houchardf0f42382019-07-22 17:43:46 +02003510 int is_first = 1;
3511
Andrew McDermottbfb15ab2022-02-11 18:26:49 +00003512 if (is_cookie2 || !http_find_header(htx, ist("Set-Cookie"), &ctx, 1)) {
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003513 if (!http_find_header(htx, ist("Set-Cookie2"), &ctx, 1))
3514 break;
3515 is_cookie2 = 1;
3516 }
3517
3518 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
3519 * <prev> points to the colon.
3520 */
3521 txn->flags |= TX_SCK_PRESENT;
3522
3523 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
3524 * check-cache is enabled) and we are not interested in checking
3525 * them. Warning, the cookie capture is declared in the frontend.
3526 */
3527 if (s->be->cookie_name == NULL && sess->fe->capture_name == NULL)
3528 break;
3529
3530 /* OK so now we know we have to process this response cookie.
3531 * The format of the Set-Cookie header is slightly different
3532 * from the format of the Cookie header in that it does not
3533 * support the comma as a cookie delimiter (thus the header
3534 * cannot be folded) because the Expires attribute described in
3535 * the original Netscape's spec may contain an unquoted date
3536 * with a comma inside. We have to live with this because
3537 * many browsers don't support Max-Age and some browsers don't
3538 * support quoted strings. However the Set-Cookie2 header is
3539 * clean.
3540 *
3541 * We have to keep multiple pointers in order to support cookie
3542 * removal at the beginning, middle or end of header without
3543 * corrupting the header (in case of set-cookie2). A special
3544 * pointer, <scav> points to the beginning of the set-cookie-av
3545 * fields after the first semi-colon. The <next> pointer points
3546 * either to the end of line (set-cookie) or next unquoted comma
3547 * (set-cookie2). All of these headers are valid :
3548 *
3549 * hdr_beg hdr_end
3550 * | |
3551 * v |
3552 * NAME1 = VALUE 1 ; Secure; Path="/" |
3553 * NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT v
3554 * NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT
3555 * NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard
3556 * | | | | | | | |
3557 * | | | | | | | +-> next
3558 * | | | | | | +------------> scav
3559 * | | | | | +--------------> val_end
3560 * | | | | +--------------------> val_beg
3561 * | | | +----------------------> equal
3562 * | | +------------------------> att_end
3563 * | +----------------------------> att_beg
3564 * +------------------------------> prev
3565 * -------------------------------> hdr_beg
3566 */
3567 hdr_beg = ctx.value.ptr;
3568 hdr_end = hdr_beg + ctx.value.len;
3569 for (prev = hdr_beg; prev < hdr_end; prev = next) {
3570
3571 /* Iterate through all cookies on this line */
3572
3573 /* find att_beg */
3574 att_beg = prev;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003575 if (!is_first)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003576 att_beg++;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003577 is_first = 0;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003578
3579 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
3580 att_beg++;
3581
3582 /* find att_end : this is the first character after the last non
3583 * space before the equal. It may be equal to hdr_end.
3584 */
3585 equal = att_end = att_beg;
3586
3587 while (equal < hdr_end) {
3588 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
3589 break;
3590 if (HTTP_IS_SPHT(*equal++))
3591 continue;
3592 att_end = equal;
3593 }
3594
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003595 /* here, <equal> points to '=', a delimiter or the end. <att_end>
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003596 * is between <att_beg> and <equal>, both may be identical.
3597 */
3598
3599 /* look for end of cookie if there is an equal sign */
3600 if (equal < hdr_end && *equal == '=') {
3601 /* look for the beginning of the value */
3602 val_beg = equal + 1;
3603 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
3604 val_beg++;
3605
3606 /* find the end of the value, respecting quotes */
3607 next = http_find_cookie_value_end(val_beg, hdr_end);
3608
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003609 /* make val_end point to the first white space or delimiter after the value */
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003610 val_end = next;
3611 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
3612 val_end--;
3613 }
3614 else {
3615 /* <equal> points to next comma, semi-colon or EOL */
3616 val_beg = val_end = next = equal;
3617 }
3618
3619 if (next < hdr_end) {
3620 /* Set-Cookie2 supports multiple cookies, and <next> points to
3621 * a colon or semi-colon before the end. So skip all attr-value
3622 * pairs and look for the next comma. For Set-Cookie, since
3623 * commas are permitted in values, skip to the end.
3624 */
3625 if (is_cookie2)
3626 next = http_find_hdr_value_end(next, hdr_end);
3627 else
3628 next = hdr_end;
3629 }
3630
3631 /* Now everything is as on the diagram above */
3632
3633 /* Ignore cookies with no equal sign */
3634 if (equal == val_end)
3635 continue;
3636
3637 /* If there are spaces around the equal sign, we need to
3638 * strip them otherwise we'll get trouble for cookie captures,
3639 * or even for rewrites. Since this happens extremely rarely,
3640 * it does not hurt performance.
3641 */
3642 if (unlikely(att_end != equal || val_beg > equal + 1)) {
3643 int stripped_before = 0;
3644 int stripped_after = 0;
3645
3646 if (att_end != equal) {
3647 memmove(att_end, equal, hdr_end - equal);
3648 stripped_before = (att_end - equal);
3649 equal += stripped_before;
3650 val_beg += stripped_before;
3651 }
3652
3653 if (val_beg > equal + 1) {
3654 memmove(equal + 1, val_beg, hdr_end + stripped_before - val_beg);
3655 stripped_after = (equal + 1) - val_beg;
3656 val_beg += stripped_after;
3657 stripped_before += stripped_after;
3658 }
3659
3660 val_end += stripped_before;
3661 next += stripped_before;
3662 hdr_end += stripped_before;
3663
Christopher Faulet3e2638e2019-06-18 09:49:16 +02003664 htx_change_blk_value_len(htx, ctx.blk, hdr_end - hdr_beg);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003665 ctx.value.len = hdr_end - hdr_beg;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003666 }
3667
3668 /* First, let's see if we want to capture this cookie. We check
3669 * that we don't already have a server side cookie, because we
3670 * can only capture one. Also as an optimisation, we ignore
3671 * cookies shorter than the declared name.
3672 */
3673 if (sess->fe->capture_name != NULL &&
3674 txn->srv_cookie == NULL &&
3675 (val_end - att_beg >= sess->fe->capture_namelen) &&
3676 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
3677 int log_len = val_end - att_beg;
3678 if ((txn->srv_cookie = pool_alloc(pool_head_capture)) == NULL) {
3679 ha_alert("HTTP logging : out of memory.\n");
3680 }
3681 else {
3682 if (log_len > sess->fe->capture_len)
3683 log_len = sess->fe->capture_len;
3684 memcpy(txn->srv_cookie, att_beg, log_len);
3685 txn->srv_cookie[log_len] = 0;
3686 }
3687 }
3688
3689 srv = objt_server(s->target);
3690 /* now check if we need to process it for persistence */
3691 if (!(s->flags & SF_IGNORE_PRST) &&
3692 (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
3693 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
3694 /* assume passive cookie by default */
3695 txn->flags &= ~TX_SCK_MASK;
3696 txn->flags |= TX_SCK_FOUND;
3697
3698 /* If the cookie is in insert mode on a known server, we'll delete
3699 * this occurrence because we'll insert another one later.
3700 * We'll delete it too if the "indirect" option is set and we're in
3701 * a direct access.
3702 */
3703 if (s->be->ck_opts & PR_CK_PSV) {
3704 /* The "preserve" flag was set, we don't want to touch the
3705 * server's cookie.
3706 */
3707 }
3708 else if ((srv && (s->be->ck_opts & PR_CK_INS)) ||
3709 ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) {
3710 /* this cookie must be deleted */
3711 if (prev == hdr_beg && next == hdr_end) {
3712 /* whole header */
3713 http_remove_header(htx, &ctx);
3714 /* note: while both invalid now, <next> and <hdr_end>
3715 * are still equal, so the for() will stop as expected.
3716 */
3717 } else {
3718 /* just remove the value */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003719 int delta = http_del_hdr_value(hdr_beg, hdr_end, &prev, next);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003720 next = prev;
3721 hdr_end += delta;
3722 }
3723 txn->flags &= ~TX_SCK_MASK;
3724 txn->flags |= TX_SCK_DELETED;
3725 /* and go on with next cookie */
3726 }
3727 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) {
3728 /* replace bytes val_beg->val_end with the cookie name associated
3729 * with this server since we know it.
3730 */
3731 int sliding, delta;
3732
3733 ctx.value = ist2(val_beg, val_end - val_beg);
3734 ctx.lws_before = ctx.lws_after = 0;
3735 http_replace_header_value(htx, &ctx, ist2(srv->cookie, srv->cklen));
3736 delta = srv->cklen - (val_end - val_beg);
3737 sliding = (ctx.value.ptr - val_beg);
3738 hdr_beg += sliding;
3739 val_beg += sliding;
3740 next += sliding + delta;
3741 hdr_end += sliding + delta;
3742
3743 txn->flags &= ~TX_SCK_MASK;
3744 txn->flags |= TX_SCK_REPLACED;
3745 }
3746 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) {
3747 /* insert the cookie name associated with this server
3748 * before existing cookie, and insert a delimiter between them..
3749 */
3750 int sliding, delta;
3751 ctx.value = ist2(val_beg, 0);
3752 ctx.lws_before = ctx.lws_after = 0;
3753 http_replace_header_value(htx, &ctx, ist2(srv->cookie, srv->cklen + 1));
3754 delta = srv->cklen + 1;
3755 sliding = (ctx.value.ptr - val_beg);
3756 hdr_beg += sliding;
3757 val_beg += sliding;
3758 next += sliding + delta;
3759 hdr_end += sliding + delta;
3760
3761 val_beg[srv->cklen] = COOKIE_DELIM;
3762 txn->flags &= ~TX_SCK_MASK;
3763 txn->flags |= TX_SCK_REPLACED;
3764 }
3765 }
3766 /* that's done for this cookie, check the next one on the same
3767 * line when next != hdr_end (only if is_cookie2).
3768 */
3769 }
3770 }
3771}
3772
Christopher Faulet25a02f62018-10-24 12:00:25 +02003773/*
3774 * Parses the Cache-Control and Pragma request header fields to determine if
3775 * the request may be served from the cache and/or if it is cacheable. Updates
3776 * s->txn->flags.
3777 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003778void http_check_request_for_cacheability(struct stream *s, struct channel *req)
Christopher Faulet25a02f62018-10-24 12:00:25 +02003779{
3780 struct http_txn *txn = s->txn;
3781 struct htx *htx;
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003782 struct http_hdr_ctx ctx = { .blk = NULL };
3783 int pragma_found, cc_found;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003784
3785 if ((txn->flags & (TX_CACHEABLE|TX_CACHE_IGNORE)) == TX_CACHE_IGNORE)
3786 return; /* nothing more to do here */
3787
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003788 htx = htxbuf(&req->buf);
Christopher Faulet25a02f62018-10-24 12:00:25 +02003789 pragma_found = cc_found = 0;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003790
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003791 /* Check "pragma" header for HTTP/1.0 compatibility. */
3792 if (http_find_header(htx, ist("pragma"), &ctx, 1)) {
3793 if (isteqi(ctx.value, ist("no-cache"))) {
3794 pragma_found = 1;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003795 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003796 }
Christopher Faulet25a02f62018-10-24 12:00:25 +02003797
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003798 ctx.blk = NULL;
3799 /* Don't use the cache and don't try to store if we found the
3800 * Authorization header */
3801 if (http_find_header(htx, ist("authorization"), &ctx, 1)) {
3802 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3803 txn->flags |= TX_CACHE_IGNORE;
3804 }
Christopher Faulet25a02f62018-10-24 12:00:25 +02003805
Christopher Faulet25a02f62018-10-24 12:00:25 +02003806
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003807 /* Look for "cache-control" header and iterate over all the values
3808 * until we find one that specifies that caching is possible or not. */
3809 ctx.blk = NULL;
3810 while (http_find_header(htx, ist("cache-control"), &ctx, 0)) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003811 cc_found = 1;
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003812 /* We don't check the values after max-age, max-stale nor min-fresh,
3813 * we simply don't use the cache when they're specified. */
3814 if (istmatchi(ctx.value, ist("max-age")) ||
3815 istmatchi(ctx.value, ist("no-cache")) ||
3816 istmatchi(ctx.value, ist("max-stale")) ||
3817 istmatchi(ctx.value, ist("min-fresh"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003818 txn->flags |= TX_CACHE_IGNORE;
3819 continue;
3820 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003821 if (istmatchi(ctx.value, ist("no-store"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003822 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3823 continue;
3824 }
3825 }
3826
3827 /* RFC7234#5.4:
3828 * When the Cache-Control header field is also present and
3829 * understood in a request, Pragma is ignored.
3830 * When the Cache-Control header field is not present in a
3831 * request, caches MUST consider the no-cache request
3832 * pragma-directive as having the same effect as if
3833 * "Cache-Control: no-cache" were present.
3834 */
3835 if (!cc_found && pragma_found)
3836 txn->flags |= TX_CACHE_IGNORE;
3837}
3838
3839/*
3840 * Check if response is cacheable or not. Updates s->txn->flags.
3841 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003842void http_check_response_for_cacheability(struct stream *s, struct channel *res)
Christopher Faulet25a02f62018-10-24 12:00:25 +02003843{
3844 struct http_txn *txn = s->txn;
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003845 struct http_hdr_ctx ctx = { .blk = NULL };
Christopher Faulet25a02f62018-10-24 12:00:25 +02003846 struct htx *htx;
Remi Tricot-Le Bretoncc9bf2e2020-11-12 11:14:41 +01003847 int has_freshness_info = 0;
3848 int has_validator = 0;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003849
3850 if (txn->status < 200) {
3851 /* do not try to cache interim responses! */
3852 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3853 return;
3854 }
3855
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003856 htx = htxbuf(&res->buf);
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003857 /* Check "pragma" header for HTTP/1.0 compatibility. */
3858 if (http_find_header(htx, ist("pragma"), &ctx, 1)) {
3859 if (isteqi(ctx.value, ist("no-cache"))) {
3860 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3861 return;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003862 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003863 }
Christopher Faulet25a02f62018-10-24 12:00:25 +02003864
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003865 /* Look for "cache-control" header and iterate over all the values
3866 * until we find one that specifies that caching is possible or not. */
3867 ctx.blk = NULL;
3868 while (http_find_header(htx, ist("cache-control"), &ctx, 0)) {
3869 if (isteqi(ctx.value, ist("public"))) {
3870 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003871 continue;
3872 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003873 if (isteqi(ctx.value, ist("private")) ||
3874 isteqi(ctx.value, ist("no-cache")) ||
3875 isteqi(ctx.value, ist("no-store")) ||
3876 isteqi(ctx.value, ist("max-age=0")) ||
3877 isteqi(ctx.value, ist("s-maxage=0"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003878 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003879 continue;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003880 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003881 /* We might have a no-cache="set-cookie" form. */
3882 if (istmatchi(ctx.value, ist("no-cache=\"set-cookie"))) {
3883 txn->flags &= ~TX_CACHE_COOK;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003884 continue;
3885 }
Remi Tricot-Le Bretoncc9bf2e2020-11-12 11:14:41 +01003886
3887 if (istmatchi(ctx.value, ist("s-maxage")) ||
3888 istmatchi(ctx.value, ist("max-age"))) {
3889 has_freshness_info = 1;
3890 continue;
3891 }
3892 }
3893
3894 /* If no freshness information could be found in Cache-Control values,
3895 * look for an Expires header. */
3896 if (!has_freshness_info) {
3897 ctx.blk = NULL;
3898 has_freshness_info = http_find_header(htx, ist("expires"), &ctx, 0);
Christopher Faulet25a02f62018-10-24 12:00:25 +02003899 }
Remi Tricot-Le Bretoncc9bf2e2020-11-12 11:14:41 +01003900
3901 /* If no freshness information could be found in Cache-Control or Expires
3902 * values, look for an explicit validator. */
3903 if (!has_freshness_info) {
3904 ctx.blk = NULL;
3905 has_validator = 1;
3906 if (!http_find_header(htx, ist("etag"), &ctx, 0)) {
3907 ctx.blk = NULL;
3908 if (!http_find_header(htx, ist("last-modified"), &ctx, 0))
3909 has_validator = 0;
3910 }
3911 }
3912
3913 /* We won't store an entry that has neither a cache validator nor an
3914 * explicit expiration time, as suggested in RFC 7234#3. */
3915 if (!has_freshness_info && !has_validator)
3916 txn->flags |= TX_CACHE_IGNORE;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003917}
3918
Christopher Faulet377c5a52018-10-24 21:21:30 +02003919/*
3920 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
3921 * for the current backend.
3922 *
3923 * It is assumed that the request is either a HEAD, GET, or POST and that the
3924 * uri_auth field is valid.
3925 *
3926 * Returns 1 if stats should be provided, otherwise 0.
3927 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003928static int http_stats_check_uri(struct stream *s, struct http_txn *txn, struct proxy *backend)
Christopher Faulet377c5a52018-10-24 21:21:30 +02003929{
3930 struct uri_auth *uri_auth = backend->uri_auth;
3931 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003932 struct htx_sl *sl;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003933 struct ist uri;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003934
3935 if (!uri_auth)
3936 return 0;
3937
3938 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
3939 return 0;
3940
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003941 htx = htxbuf(&s->req.buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02003942 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003943 uri = htx_sl_req_uri(sl);
Amaury Denoyellec453f952021-07-06 11:40:12 +02003944 if (*uri_auth->uri_prefix == '/') {
3945 struct http_uri_parser parser = http_uri_parser_init(uri);
3946 uri = http_parse_path(&parser);
3947 }
Christopher Faulet377c5a52018-10-24 21:21:30 +02003948
3949 /* check URI size */
3950 if (uri_auth->uri_len > uri.len)
3951 return 0;
3952
3953 if (memcmp(uri.ptr, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
3954 return 0;
3955
3956 return 1;
3957}
3958
3959/* This function prepares an applet to handle the stats. It can deal with the
3960 * "100-continue" expectation, check that admin rules are met for POST requests,
3961 * and program a response message if something was unexpected. It cannot fail
3962 * and always relies on the stats applet to complete the job. It does not touch
3963 * analysers nor counters, which are left to the caller. It does not touch
3964 * s->target which is supposed to already point to the stats applet. The caller
3965 * is expected to have already assigned an appctx to the stream.
3966 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003967static int http_handle_stats(struct stream *s, struct channel *req)
Christopher Faulet377c5a52018-10-24 21:21:30 +02003968{
3969 struct stats_admin_rule *stats_admin_rule;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003970 struct session *sess = s->sess;
3971 struct http_txn *txn = s->txn;
3972 struct http_msg *msg = &txn->req;
3973 struct uri_auth *uri_auth = s->be->uri_auth;
3974 const char *h, *lookup, *end;
Willy Tarreau8e7c6e62022-05-18 17:58:02 +02003975 struct appctx *appctx = __sc_appctx(s->scb);
Willy Tarreau91cefca2022-05-03 17:08:29 +02003976 struct show_stat_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx));
Christopher Faulet377c5a52018-10-24 21:21:30 +02003977 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003978 struct htx_sl *sl;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003979
Willy Tarreau41f88522022-05-03 18:39:27 +02003980 appctx->st1 = 0;
Willy Tarreau6ef16482022-05-06 18:07:53 +02003981 ctx->state = STAT_STATE_INIT;
Willy Tarreau91cefca2022-05-03 17:08:29 +02003982 ctx->st_code = STAT_STATUS_INIT;
3983 ctx->flags |= uri_auth->flags;
3984 ctx->flags |= STAT_FMT_HTML; /* assume HTML mode by default */
Christopher Faulet377c5a52018-10-24 21:21:30 +02003985 if ((msg->flags & HTTP_MSGF_VER_11) && (txn->meth != HTTP_METH_HEAD))
Willy Tarreau91cefca2022-05-03 17:08:29 +02003986 ctx->flags |= STAT_CHUNKED;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003987
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003988 htx = htxbuf(&req->buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02003989 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003990 lookup = HTX_SL_REQ_UPTR(sl) + uri_auth->uri_len;
3991 end = HTX_SL_REQ_UPTR(sl) + HTX_SL_REQ_ULEN(sl);
Christopher Faulet377c5a52018-10-24 21:21:30 +02003992
3993 for (h = lookup; h <= end - 3; h++) {
3994 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02003995 ctx->flags |= STAT_HIDE_DOWN;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003996 break;
3997 }
Amaury Denoyelle91e55ea2021-02-25 14:46:08 +01003998 }
3999
4000 for (h = lookup; h <= end - 9; h++) {
4001 if (memcmp(h, ";no-maint", 9) == 0) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02004002 ctx->flags |= STAT_HIDE_MAINT;
Willy Tarreau3e320362020-10-23 17:28:57 +02004003 break;
4004 }
Christopher Faulet377c5a52018-10-24 21:21:30 +02004005 }
4006
4007 if (uri_auth->refresh) {
4008 for (h = lookup; h <= end - 10; h++) {
4009 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02004010 ctx->flags |= STAT_NO_REFRESH;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004011 break;
4012 }
4013 }
4014 }
4015
4016 for (h = lookup; h <= end - 4; h++) {
4017 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02004018 ctx->flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
Christopher Faulet377c5a52018-10-24 21:21:30 +02004019 break;
4020 }
4021 }
4022
4023 for (h = lookup; h <= end - 6; h++) {
4024 if (memcmp(h, ";typed", 6) == 0) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02004025 ctx->flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
4026 ctx->flags |= STAT_FMT_TYPED;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004027 break;
4028 }
4029 }
4030
Christopher Faulet6338a082019-09-09 15:50:54 +02004031 for (h = lookup; h <= end - 5; h++) {
4032 if (memcmp(h, ";json", 5) == 0) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02004033 ctx->flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
4034 ctx->flags |= STAT_FMT_JSON;
Christopher Faulet6338a082019-09-09 15:50:54 +02004035 break;
4036 }
4037 }
4038
4039 for (h = lookup; h <= end - 12; h++) {
4040 if (memcmp(h, ";json-schema", 12) == 0) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02004041 ctx->flags &= ~STAT_FMT_MASK;
4042 ctx->flags |= STAT_JSON_SCHM;
Christopher Faulet6338a082019-09-09 15:50:54 +02004043 break;
4044 }
4045 }
4046
Christopher Faulet377c5a52018-10-24 21:21:30 +02004047 for (h = lookup; h <= end - 8; h++) {
4048 if (memcmp(h, ";st=", 4) == 0) {
4049 int i;
4050 h += 4;
Willy Tarreau91cefca2022-05-03 17:08:29 +02004051 ctx->st_code = STAT_STATUS_UNKN;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004052 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
4053 if (strncmp(stat_status_codes[i], h, 4) == 0) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02004054 ctx->st_code = i;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004055 break;
4056 }
4057 }
4058 break;
4059 }
4060 }
4061
Willy Tarreau91cefca2022-05-03 17:08:29 +02004062 ctx->scope_str = 0;
4063 ctx->scope_len = 0;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004064 for (h = lookup; h <= end - 8; h++) {
4065 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
4066 int itx = 0;
4067 const char *h2;
4068 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
4069 const char *err;
4070
4071 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
4072 h2 = h;
Willy Tarreau91cefca2022-05-03 17:08:29 +02004073 ctx->scope_str = h2 - HTX_SL_REQ_UPTR(sl);
Christopher Fauleted7a0662019-01-14 11:07:34 +01004074 while (h < end) {
Christopher Faulet377c5a52018-10-24 21:21:30 +02004075 if (*h == ';' || *h == '&' || *h == ' ')
4076 break;
4077 itx++;
4078 h++;
4079 }
4080
4081 if (itx > STAT_SCOPE_TXT_MAXLEN)
4082 itx = STAT_SCOPE_TXT_MAXLEN;
Willy Tarreau91cefca2022-05-03 17:08:29 +02004083 ctx->scope_len = itx;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004084
Willy Tarreau91cefca2022-05-03 17:08:29 +02004085 /* scope_txt = search query, ctx->scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Christopher Faulet377c5a52018-10-24 21:21:30 +02004086 memcpy(scope_txt, h2, itx);
4087 scope_txt[itx] = '\0';
4088 err = invalid_char(scope_txt);
4089 if (err) {
4090 /* bad char in search text => clear scope */
Willy Tarreau91cefca2022-05-03 17:08:29 +02004091 ctx->scope_str = 0;
4092 ctx->scope_len = 0;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004093 }
4094 break;
4095 }
4096 }
4097
4098 /* now check whether we have some admin rules for this request */
4099 list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) {
4100 int ret = 1;
4101
4102 if (stats_admin_rule->cond) {
4103 ret = acl_exec_cond(stats_admin_rule->cond, s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
4104 ret = acl_pass(ret);
4105 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
4106 ret = !ret;
4107 }
4108
4109 if (ret) {
4110 /* no rule, or the rule matches */
Willy Tarreau91cefca2022-05-03 17:08:29 +02004111 ctx->flags |= STAT_ADMIN;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004112 break;
4113 }
4114 }
4115
Christopher Faulet5d45e382019-02-27 15:15:23 +01004116 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
4117 appctx->st0 = STAT_HTTP_HEAD;
4118 else if (txn->meth == HTTP_METH_POST) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02004119 if (ctx->flags & STAT_ADMIN) {
Christopher Faulet377c5a52018-10-24 21:21:30 +02004120 appctx->st0 = STAT_HTTP_POST;
Christopher Fauletbd9e8422019-08-15 22:26:48 +02004121 if (msg->msg_state < HTTP_MSG_DATA)
4122 req->analysers |= AN_REQ_HTTP_BODY;
4123 }
Christopher Faulet377c5a52018-10-24 21:21:30 +02004124 else {
Christopher Faulet5d45e382019-02-27 15:15:23 +01004125 /* POST without admin level */
Willy Tarreau91cefca2022-05-03 17:08:29 +02004126 ctx->flags &= ~STAT_CHUNKED;
4127 ctx->st_code = STAT_STATUS_DENY;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004128 appctx->st0 = STAT_HTTP_LAST;
4129 }
4130 }
4131 else {
Christopher Faulet5d45e382019-02-27 15:15:23 +01004132 /* Unsupported method */
Willy Tarreau91cefca2022-05-03 17:08:29 +02004133 ctx->flags &= ~STAT_CHUNKED;
4134 ctx->st_code = STAT_STATUS_IVAL;
Christopher Faulet5d45e382019-02-27 15:15:23 +01004135 appctx->st0 = STAT_HTTP_LAST;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004136 }
4137
4138 s->task->nice = -32; /* small boost for HTTP statistics */
4139 return 1;
4140}
4141
Christopher Faulet021a8e42021-03-29 10:46:38 +02004142/* This function waits for the message payload at most <time> milliseconds (may
4143 * be set to TICK_ETERNITY). It stops to wait if at least <bytes> bytes of the
4144 * payload are received (0 means no limit). It returns HTTP_RULE_* depending on
4145 * the result:
4146 *
4147 * - HTTP_RULE_RES_CONT when conditions are met to stop waiting
4148 * - HTTP_RULE_RES_YIELD to wait for more data
Ilya Shipitsinb2be9a12021-04-24 13:25:42 +05004149 * - HTTP_RULE_RES_ABRT when a timeout occurred.
Christopher Faulet021a8e42021-03-29 10:46:38 +02004150 * - HTTP_RULE_RES_BADREQ if a parsing error is raised by lower level
Ilya Shipitsinb2be9a12021-04-24 13:25:42 +05004151 * - HTTP_RULE_RES_ERROR if an internal error occurred
Christopher Faulet021a8e42021-03-29 10:46:38 +02004152 *
Ilya Shipitsinb2be9a12021-04-24 13:25:42 +05004153 * If a timeout occurred, this function is responsible to emit the right response
Christopher Faulet021a8e42021-03-29 10:46:38 +02004154 * to the client, depending on the channel (408 on request side, 504 on response
4155 * side). All other errors must be handled by the caller.
4156 */
4157enum rule_result http_wait_for_msg_body(struct stream *s, struct channel *chn,
4158 unsigned int time, unsigned int bytes)
4159{
4160 struct session *sess = s->sess;
4161 struct http_txn *txn = s->txn;
4162 struct http_msg *msg = ((chn->flags & CF_ISRESP) ? &txn->rsp : &txn->req);
4163 struct htx *htx;
4164 enum rule_result ret = HTTP_RULE_RES_CONT;
4165
4166 htx = htxbuf(&chn->buf);
4167
4168 if (htx->flags & HTX_FL_PARSING_ERROR) {
4169 ret = HTTP_RULE_RES_BADREQ;
4170 goto end;
4171 }
4172 if (htx->flags & HTX_FL_PROCESSING_ERROR) {
4173 ret = HTTP_RULE_RES_ERROR;
4174 goto end;
4175 }
4176
4177 /* Do nothing for bodyless and CONNECT requests */
4178 if (txn->meth == HTTP_METH_CONNECT || (msg->flags & HTTP_MSGF_BODYLESS))
4179 goto end;
4180
4181 if (!(chn->flags & CF_ISRESP) && msg->msg_state < HTTP_MSG_DATA) {
4182 if (http_handle_expect_hdr(s, htx, msg) == -1) {
4183 ret = HTTP_RULE_RES_ERROR;
4184 goto end;
4185 }
4186 }
4187
4188 msg->msg_state = HTTP_MSG_DATA;
4189
4190 /* Now we're in HTTP_MSG_DATA. We just need to know if all data have
4191 * been received or if the buffer is full.
4192 */
Christopher Faulet78335962021-09-23 14:46:32 +02004193 if ((htx->flags & HTX_FL_EOM) ||
4194 htx_get_tail_type(htx) > HTX_BLK_DATA ||
4195 channel_htx_full(chn, htx, global.tune.maxrewrite) ||
Willy Tarreau99615ed2022-05-25 07:29:36 +02004196 sc_waiting_room(chn_prod(chn)))
Christopher Faulet021a8e42021-03-29 10:46:38 +02004197 goto end;
4198
4199 if (bytes) {
4200 struct htx_blk *blk;
4201 unsigned int len = 0;
4202
4203 for (blk = htx_get_first_blk(htx); blk; blk = htx_get_next_blk(htx, blk)) {
4204 if (htx_get_blk_type(blk) != HTX_BLK_DATA)
4205 continue;
4206 len += htx_get_blksz(blk);
4207 if (len >= bytes)
4208 goto end;
4209 }
4210 }
4211
4212 if ((chn->flags & CF_READ_TIMEOUT) || tick_is_expired(chn->analyse_exp, now_ms)) {
4213 if (!(chn->flags & CF_ISRESP))
4214 goto abort_req;
4215 goto abort_res;
4216 }
4217
4218 /* we get here if we need to wait for more data */
4219 if (!(chn->flags & (CF_SHUTR | CF_READ_ERROR))) {
4220 if (!tick_isset(chn->analyse_exp))
4221 chn->analyse_exp = tick_add_ifset(now_ms, time);
4222 ret = HTTP_RULE_RES_YIELD;
4223 }
4224
4225 end:
4226 return ret;
4227
4228 abort_req:
4229 txn->status = 408;
4230 if (!(s->flags & SF_ERR_MASK))
4231 s->flags |= SF_ERR_CLITO;
4232 if (!(s->flags & SF_FINST_MASK))
4233 s->flags |= SF_FINST_D;
Willy Tarreau4781b152021-04-06 13:53:36 +02004234 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req);
Christopher Faulet021a8e42021-03-29 10:46:38 +02004235 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02004236 _HA_ATOMIC_INC(&sess->listener->counters->failed_req);
Christopher Faulet021a8e42021-03-29 10:46:38 +02004237 http_reply_and_close(s, txn->status, http_error_message(s));
4238 ret = HTTP_RULE_RES_ABRT;
4239 goto end;
4240
4241 abort_res:
4242 txn->status = 504;
4243 if (!(s->flags & SF_ERR_MASK))
4244 s->flags |= SF_ERR_SRVTO;
4245 if (!(s->flags & SF_FINST_MASK))
4246 s->flags |= SF_FINST_D;
4247 stream_inc_http_fail_ctr(s);
4248 http_reply_and_close(s, txn->status, http_error_message(s));
4249 ret = HTTP_RULE_RES_ABRT;
4250 goto end;
4251}
4252
Willy Tarreaub49672d2022-05-27 10:13:37 +02004253void http_perform_server_redirect(struct stream *s, struct stconn *sc)
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004254{
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004255 struct channel *req = &s->req;
4256 struct channel *res = &s->res;
4257 struct server *srv;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004258 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004259 struct htx_sl *sl;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004260 struct ist path, location;
4261 unsigned int flags;
Amaury Denoyellec453f952021-07-06 11:40:12 +02004262 struct http_uri_parser parser;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004263
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004264 /*
4265 * Create the location
4266 */
4267 chunk_reset(&trash);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004268
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004269 /* 1: add the server's prefix */
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004270 /* special prefix "/" means don't change URL */
4271 srv = __objt_server(s->target);
4272 if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') {
4273 if (!chunk_memcat(&trash, srv->rdr_pfx, srv->rdr_len))
4274 return;
4275 }
4276
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004277 /* 2: add the request Path */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004278 htx = htxbuf(&req->buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02004279 sl = http_get_stline(htx);
Amaury Denoyellec453f952021-07-06 11:40:12 +02004280 parser = http_uri_parser_init(htx_sl_req_uri(sl));
4281 path = http_parse_path(&parser);
Tim Duesterhused526372020-03-05 17:56:33 +01004282 if (!isttest(path))
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004283 return;
4284
4285 if (!chunk_memcat(&trash, path.ptr, path.len))
4286 return;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004287 location = ist2(trash.area, trash.data);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004288
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004289 /*
4290 * Create the 302 respone
4291 */
4292 htx = htx_from_buf(&res->buf);
4293 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
4294 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
4295 ist("HTTP/1.1"), ist("302"), ist("Found"));
4296 if (!sl)
4297 goto fail;
4298 sl->info.res.status = 302;
4299 s->txn->status = 302;
4300
4301 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) ||
4302 !htx_add_header(htx, ist("Connection"), ist("close")) ||
4303 !htx_add_header(htx, ist("Content-length"), ist("0")) ||
4304 !htx_add_header(htx, ist("Location"), location))
4305 goto fail;
4306
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004307 if (!htx_add_endof(htx, HTX_BLK_EOH))
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004308 goto fail;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004309
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004310 htx->flags |= HTX_FL_EOM;
Christopher Fauletc20afb82020-01-24 19:16:26 +01004311 htx_to_buf(htx, &res->buf);
Christopher Fauleta72a7e42020-01-28 09:28:11 +01004312 if (!http_forward_proxy_resp(s, 1))
4313 goto fail;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004314
4315 /* return without error. */
Willy Tarreaub49672d2022-05-27 10:13:37 +02004316 sc_shutr(sc);
4317 sc_shutw(sc);
Christopher Faulet50264b42022-03-30 19:39:30 +02004318 s->conn_err_type = STRM_ET_NONE;
Willy Tarreaub49672d2022-05-27 10:13:37 +02004319 sc->state = SC_ST_CLO;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004320
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004321 if (!(s->flags & SF_ERR_MASK))
4322 s->flags |= SF_ERR_LOCAL;
4323 if (!(s->flags & SF_FINST_MASK))
4324 s->flags |= SF_FINST_C;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004325
4326 /* FIXME: we should increase a counter of redirects per server and per backend. */
4327 srv_inc_sess_ctr(srv);
4328 srv_set_sess_last(srv);
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004329 return;
4330
4331 fail:
4332 /* If an error occurred, remove the incomplete HTTP response from the
4333 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004334 channel_htx_truncate(res, htx);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004335}
4336
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05004337/* This function terminates the request because it was completely analyzed or
Christopher Fauletf2824e62018-10-01 12:12:37 +02004338 * because an error was triggered during the body forwarding.
4339 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004340static void http_end_request(struct stream *s)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004341{
4342 struct channel *chn = &s->req;
4343 struct http_txn *txn = s->txn;
4344
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004345 DBG_TRACE_ENTER(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004346
Christopher Fauletb42a8b62018-11-19 21:59:00 +01004347 if (unlikely(txn->req.msg_state == HTTP_MSG_ERROR ||
4348 txn->rsp.msg_state == HTTP_MSG_ERROR)) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004349 channel_abort(chn);
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004350 channel_htx_truncate(chn, htxbuf(&chn->buf));
Christopher Fauletf2824e62018-10-01 12:12:37 +02004351 goto end;
4352 }
4353
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004354 if (unlikely(txn->req.msg_state < HTTP_MSG_DONE)) {
4355 DBG_TRACE_DEVEL("waiting end of the request", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004356 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004357 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004358
4359 if (txn->req.msg_state == HTTP_MSG_DONE) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004360 /* No need to read anymore, the request was completely parsed.
4361 * We can shut the read side unless we want to abort_on_close,
4362 * or we have a POST request. The issue with POST requests is
4363 * that some browsers still send a CRLF after the request, and
4364 * this CRLF must be read so that it does not remain in the kernel
4365 * buffers, otherwise a close could cause an RST on some systems
4366 * (eg: Linux).
4367 */
Christopher Faulet769d0e92019-03-22 14:23:18 +01004368 if (!(s->be->options & PR_O_ABRT_CLOSE) && txn->meth != HTTP_METH_POST)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004369 channel_dont_read(chn);
4370
4371 /* if the server closes the connection, we want to immediately react
4372 * and close the socket to save packets and syscalls.
4373 */
Willy Tarreaucb041662022-05-17 19:44:42 +02004374 s->scb->flags |= SC_FL_NOHALF;
Christopher Fauletf2824e62018-10-01 12:12:37 +02004375
4376 /* In any case we've finished parsing the request so we must
4377 * disable Nagle when sending data because 1) we're not going
4378 * to shut this side, and 2) the server is waiting for us to
4379 * send pending data.
4380 */
4381 chn->flags |= CF_NEVER_WAIT;
4382
Christopher Fauletd01ce402019-01-02 17:44:13 +01004383 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
4384 /* The server has not finished to respond, so we
4385 * don't want to move in order not to upset it.
4386 */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004387 DBG_TRACE_DEVEL("waiting end of the response", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletd01ce402019-01-02 17:44:13 +01004388 return;
4389 }
4390
Christopher Fauletf2824e62018-10-01 12:12:37 +02004391 /* When we get here, it means that both the request and the
4392 * response have finished receiving. Depending on the connection
4393 * mode, we'll have to wait for the last bytes to leave in either
4394 * direction, and sometimes for a close to be effective.
4395 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02004396 if (txn->flags & TX_CON_WANT_TUN) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004397 /* Tunnel mode will not have any analyser so it needs to
4398 * poll for reads.
4399 */
4400 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004401 if (b_data(&chn->buf)) {
4402 DBG_TRACE_DEVEL("waiting to flush the request", STRM_EV_HTTP_ANA, s, txn);
Christopher Faulet9768c262018-10-22 09:34:31 +02004403 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004404 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004405 txn->req.msg_state = HTTP_MSG_TUNNEL;
4406 }
4407 else {
4408 /* we're not expecting any new data to come for this
4409 * transaction, so we can close it.
Christopher Faulet9768c262018-10-22 09:34:31 +02004410 *
4411 * However, there is an exception if the response
4412 * length is undefined. In this case, we need to wait
4413 * the close from the server. The response will be
4414 * switched in TUNNEL mode until the end.
Christopher Fauletf2824e62018-10-01 12:12:37 +02004415 */
4416 if (!(txn->rsp.flags & HTTP_MSGF_XFER_LEN) &&
4417 txn->rsp.msg_state != HTTP_MSG_CLOSED)
Christopher Faulet9768c262018-10-22 09:34:31 +02004418 goto check_channel_flags;
Christopher Fauletf2824e62018-10-01 12:12:37 +02004419
4420 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4421 channel_shutr_now(chn);
4422 channel_shutw_now(chn);
4423 }
4424 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004425 goto check_channel_flags;
4426 }
4427
4428 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
4429 http_msg_closing:
4430 /* nothing else to forward, just waiting for the output buffer
4431 * to be empty and for the shutw_now to take effect.
4432 */
4433 if (channel_is_empty(chn)) {
4434 txn->req.msg_state = HTTP_MSG_CLOSED;
4435 goto http_msg_closed;
4436 }
4437 else if (chn->flags & CF_SHUTW) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004438 txn->req.msg_state = HTTP_MSG_ERROR;
4439 goto end;
4440 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004441 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004442 return;
4443 }
4444
4445 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
4446 http_msg_closed:
Christopher Fauletf2824e62018-10-01 12:12:37 +02004447 /* if we don't know whether the server will close, we need to hard close */
4448 if (txn->rsp.flags & HTTP_MSGF_XFER_LEN)
Willy Tarreaucb041662022-05-17 19:44:42 +02004449 s->scb->flags |= SC_FL_NOLINGER; /* we want to close ASAP */
Christopher Fauletf2824e62018-10-01 12:12:37 +02004450 /* see above in MSG_DONE why we only do this in these states */
Christopher Faulet769d0e92019-03-22 14:23:18 +01004451 if (!(s->be->options & PR_O_ABRT_CLOSE))
Christopher Fauletf2824e62018-10-01 12:12:37 +02004452 channel_dont_read(chn);
4453 goto end;
4454 }
4455
4456 check_channel_flags:
4457 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4458 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4459 /* if we've just closed an output, let's switch */
4460 txn->req.msg_state = HTTP_MSG_CLOSING;
4461 goto http_msg_closing;
4462 }
4463
4464 end:
4465 chn->analysers &= AN_REQ_FLT_END;
Christopher Faulet198ef8b2020-12-15 13:32:55 +01004466 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
4467 chn->flags |= CF_NEVER_WAIT;
4468 if (HAS_REQ_DATA_FILTERS(s))
Christopher Fauletf2824e62018-10-01 12:12:37 +02004469 chn->analysers |= AN_REQ_FLT_XFER_DATA;
Christopher Faulet198ef8b2020-12-15 13:32:55 +01004470 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004471 channel_auto_close(chn);
4472 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004473 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004474}
4475
4476
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05004477/* This function terminates the response because it was completely analyzed or
Christopher Fauletf2824e62018-10-01 12:12:37 +02004478 * because an error was triggered during the body forwarding.
4479 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004480static void http_end_response(struct stream *s)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004481{
4482 struct channel *chn = &s->res;
4483 struct http_txn *txn = s->txn;
4484
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004485 DBG_TRACE_ENTER(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004486
Christopher Fauletb42a8b62018-11-19 21:59:00 +01004487 if (unlikely(txn->req.msg_state == HTTP_MSG_ERROR ||
4488 txn->rsp.msg_state == HTTP_MSG_ERROR)) {
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004489 channel_htx_truncate(&s->req, htxbuf(&s->req.buf));
Christopher Faulet9768c262018-10-22 09:34:31 +02004490 channel_abort(&s->req);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004491 goto end;
4492 }
4493
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004494 if (unlikely(txn->rsp.msg_state < HTTP_MSG_DONE)) {
4495 DBG_TRACE_DEVEL("waiting end of the response", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004496 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004497 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004498
4499 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
4500 /* In theory, we don't need to read anymore, but we must
4501 * still monitor the server connection for a possible close
4502 * while the request is being uploaded, so we don't disable
4503 * reading.
4504 */
4505 /* channel_dont_read(chn); */
4506
4507 if (txn->req.msg_state < HTTP_MSG_DONE) {
4508 /* The client seems to still be sending data, probably
4509 * because we got an error response during an upload.
4510 * We have the choice of either breaking the connection
4511 * or letting it pass through. Let's do the later.
4512 */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004513 DBG_TRACE_DEVEL("waiting end of the request", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004514 return;
4515 }
4516
4517 /* When we get here, it means that both the request and the
4518 * response have finished receiving. Depending on the connection
4519 * mode, we'll have to wait for the last bytes to leave in either
4520 * direction, and sometimes for a close to be effective.
4521 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02004522 if (txn->flags & TX_CON_WANT_TUN) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004523 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004524 if (b_data(&chn->buf)) {
4525 DBG_TRACE_DEVEL("waiting to flush the respone", STRM_EV_HTTP_ANA, s, txn);
Christopher Faulet9768c262018-10-22 09:34:31 +02004526 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004527 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004528 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
4529 }
4530 else {
4531 /* we're not expecting any new data to come for this
4532 * transaction, so we can close it.
4533 */
4534 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4535 channel_shutr_now(chn);
4536 channel_shutw_now(chn);
4537 }
4538 }
4539 goto check_channel_flags;
4540 }
4541
4542 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
4543 http_msg_closing:
4544 /* nothing else to forward, just waiting for the output buffer
4545 * to be empty and for the shutw_now to take effect.
4546 */
4547 if (channel_is_empty(chn)) {
4548 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4549 goto http_msg_closed;
4550 }
4551 else if (chn->flags & CF_SHUTW) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004552 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau4781b152021-04-06 13:53:36 +02004553 _HA_ATOMIC_INC(&strm_sess(s)->fe->fe_counters.cli_aborts);
4554 _HA_ATOMIC_INC(&s->be->be_counters.cli_aborts);
William Lallemand36119de2021-03-08 15:26:48 +01004555 if (strm_sess(s)->listener && strm_sess(s)->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02004556 _HA_ATOMIC_INC(&strm_sess(s)->listener->counters->cli_aborts);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004557 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02004558 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.cli_aborts);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004559 goto end;
4560 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004561 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004562 return;
4563 }
4564
4565 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
4566 http_msg_closed:
4567 /* drop any pending data */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004568 channel_htx_truncate(&s->req, htxbuf(&s->req.buf));
Christopher Faulet9768c262018-10-22 09:34:31 +02004569 channel_abort(&s->req);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004570 goto end;
4571 }
4572
4573 check_channel_flags:
4574 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4575 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4576 /* if we've just closed an output, let's switch */
4577 txn->rsp.msg_state = HTTP_MSG_CLOSING;
4578 goto http_msg_closing;
4579 }
4580
4581 end:
4582 chn->analysers &= AN_RES_FLT_END;
Christopher Faulet198ef8b2020-12-15 13:32:55 +01004583 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
4584 chn->flags |= CF_NEVER_WAIT;
4585 if (HAS_RSP_DATA_FILTERS(s))
4586 chn->analysers |= AN_RES_FLT_XFER_DATA;
4587 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004588 channel_auto_close(chn);
4589 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004590 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004591}
4592
Christopher Fauletef70e252020-01-28 09:26:19 +01004593/* Forward a response generated by HAProxy (error/redirect/return). This
4594 * function forwards all pending incoming data. If <final> is set to 0, nothing
4595 * more is performed. It is used for 1xx informational messages. Otherwise, the
Christopher Faulet507479b2020-05-15 12:29:46 +02004596 * transaction is terminated and the request is emptied. On success 1 is
Christopher Faulet40e6b552020-06-25 16:04:50 +02004597 * returned. If an error occurred, 0 is returned. If it fails, this function
4598 * only exits. It is the caller responsibility to do the cleanup.
Christopher Fauletef70e252020-01-28 09:26:19 +01004599 */
4600int http_forward_proxy_resp(struct stream *s, int final)
4601{
4602 struct channel *req = &s->req;
4603 struct channel *res = &s->res;
4604 struct htx *htx = htxbuf(&res->buf);
4605 size_t data;
4606
4607 if (final) {
4608 htx->flags |= HTX_FL_PROXY_RESP;
Christopher Faulet507479b2020-05-15 12:29:46 +02004609
Christopher Fauletaab1b672020-11-18 16:44:02 +01004610 if (!htx_is_empty(htx) && !http_eval_after_res_rules(s))
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01004611 return 0;
Christopher Fauletef70e252020-01-28 09:26:19 +01004612
Christopher Fauletd6c48362020-10-19 18:01:38 +02004613 if (s->txn->meth == HTTP_METH_HEAD)
4614 htx_skip_msg_payload(htx);
4615
Christopher Fauletef70e252020-01-28 09:26:19 +01004616 channel_auto_read(req);
4617 channel_abort(req);
4618 channel_auto_close(req);
4619 channel_htx_erase(req, htxbuf(&req->buf));
4620
4621 res->wex = tick_add_ifset(now_ms, res->wto);
4622 channel_auto_read(res);
4623 channel_auto_close(res);
4624 channel_shutr_now(res);
Christopher Faulet1a9db7c2020-06-25 15:36:45 +02004625 res->flags |= CF_EOI; /* The response is terminated, add EOI */
Christopher Faulet42432f32020-11-20 17:43:16 +01004626 htxbuf(&res->buf)->flags |= HTX_FL_EOM; /* no more data are expected */
Christopher Fauletef70e252020-01-28 09:26:19 +01004627 }
Christopher Fauletcf6898c2020-06-25 15:55:11 +02004628 else {
4629 /* Send ASAP informational messages. Rely on CF_EOI for final
4630 * response.
4631 */
4632 res->flags |= CF_SEND_DONTWAIT;
4633 }
Christopher Fauletef70e252020-01-28 09:26:19 +01004634
4635 data = htx->data - co_data(res);
4636 c_adv(res, data);
4637 htx->first = -1;
4638 res->total += data;
4639 return 1;
4640}
4641
Willy Tarreaub49672d2022-05-27 10:13:37 +02004642void http_server_error(struct stream *s, struct stconn *sc, int err,
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004643 int finst, struct http_reply *msg)
Christopher Faulet0f226952018-10-22 09:29:56 +02004644{
Christopher Faulet72c7d8d2020-01-27 15:32:25 +01004645 http_reply_and_close(s, s->txn->status, msg);
Christopher Faulet0f226952018-10-22 09:29:56 +02004646 if (!(s->flags & SF_ERR_MASK))
4647 s->flags |= err;
4648 if (!(s->flags & SF_FINST_MASK))
4649 s->flags |= finst;
4650}
4651
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004652void http_reply_and_close(struct stream *s, short status, struct http_reply *msg)
Christopher Faulet0f226952018-10-22 09:29:56 +02004653{
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004654 if (!msg) {
4655 channel_htx_truncate(&s->res, htxbuf(&s->res.buf));
4656 goto end;
4657 }
4658
4659 if (http_reply_message(s, msg) == -1) {
4660 /* On error, return a 500 error message, but don't rewrite it if
Christopher Faulet40e6b552020-06-25 16:04:50 +02004661 * it is already an internal error. If it was already a "const"
4662 * 500 error, just fail.
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004663 */
Christopher Faulet40e6b552020-06-25 16:04:50 +02004664 if (s->txn->status == 500) {
4665 if (s->txn->flags & TX_CONST_REPLY)
4666 goto end;
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004667 s->txn->flags |= TX_CONST_REPLY;
Christopher Faulet40e6b552020-06-25 16:04:50 +02004668 }
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004669 s->txn->status = 500;
4670 s->txn->http_reply = NULL;
4671 return http_reply_and_close(s, s->txn->status, http_error_message(s));
4672 }
4673
4674end:
4675 s->res.wex = tick_add_ifset(now_ms, s->res.wto);
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004676
Christopher Faulet2d565002021-09-10 09:17:50 +02004677 /* At this staged, HTTP analysis is finished */
4678 s->req.analysers &= AN_REQ_FLT_END;
4679 s->req.analyse_exp = TICK_ETERNITY;
4680
4681 s->res.analysers &= AN_RES_FLT_END;
4682 s->res.analyse_exp = TICK_ETERNITY;
4683
Christopher Faulet0f226952018-10-22 09:29:56 +02004684 channel_auto_read(&s->req);
4685 channel_abort(&s->req);
4686 channel_auto_close(&s->req);
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004687 channel_htx_erase(&s->req, htxbuf(&s->req.buf));
Christopher Faulet72c7d8d2020-01-27 15:32:25 +01004688 channel_auto_read(&s->res);
4689 channel_auto_close(&s->res);
4690 channel_shutr_now(&s->res);
Christopher Faulet0f226952018-10-22 09:29:56 +02004691}
4692
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004693struct http_reply *http_error_message(struct stream *s)
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004694{
4695 const int msgnum = http_get_status_idx(s->txn->status);
4696
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004697 if (s->txn->http_reply)
4698 return s->txn->http_reply;
4699 else if (s->be->replies[msgnum])
4700 return s->be->replies[msgnum];
4701 else if (strm_fe(s)->replies[msgnum])
4702 return strm_fe(s)->replies[msgnum];
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004703 else
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004704 return &http_err_replies[msgnum];
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004705}
4706
Christopher Faulet40e6b552020-06-25 16:04:50 +02004707/* Produces an HTX message from an http reply. Depending on the http reply type,
4708 * a, errorfile, an raw file or a log-format string is used. On success, it
4709 * returns 0. If an error occurs -1 is returned. If it fails, this function only
4710 * exits. It is the caller responsibility to do the cleanup.
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004711 */
Christopher Fauletae43b6c2020-05-27 15:24:22 +02004712int http_reply_to_htx(struct stream *s, struct htx *htx, struct http_reply *reply)
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004713{
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004714 struct buffer *errmsg;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004715 struct htx_sl *sl;
4716 struct buffer *body = NULL;
4717 const char *status, *reason, *clen, *ctype;
4718 unsigned int slflags;
4719 int ret = 0;
4720
Christopher Faulete29a97e2020-05-14 14:49:25 +02004721 /*
4722 * - HTTP_REPLY_ERRFILES unexpected here. handled as no payload if so
4723 *
4724 * - HTTP_REPLY_INDIRECT: switch on another reply if defined or handled
4725 * as no payload if NULL. the TXN status code is set with the status
4726 * of the original reply.
4727 */
4728
4729 if (reply->type == HTTP_REPLY_INDIRECT) {
4730 if (reply->body.reply)
4731 reply = reply->body.reply;
4732 }
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004733 if (reply->type == HTTP_REPLY_ERRMSG && !reply->body.errmsg) {
4734 /* get default error message */
4735 if (reply == s->txn->http_reply)
4736 s->txn->http_reply = NULL;
4737 reply = http_error_message(s);
4738 if (reply->type == HTTP_REPLY_INDIRECT) {
4739 if (reply->body.reply)
4740 reply = reply->body.reply;
4741 }
4742 }
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004743
4744 if (reply->type == HTTP_REPLY_ERRMSG) {
4745 /* implicit or explicit error message*/
4746 errmsg = reply->body.errmsg;
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004747 if (errmsg && !b_is_null(errmsg)) {
Christopher Faulet20567362020-05-15 14:52:49 +02004748 if (!htx_copy_msg(htx, errmsg))
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004749 goto fail;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004750 }
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004751 }
4752 else {
4753 /* no payload, file or log-format string */
4754 if (reply->type == HTTP_REPLY_RAW) {
4755 /* file */
4756 body = &reply->body.obj;
4757 }
4758 else if (reply->type == HTTP_REPLY_LOGFMT) {
4759 /* log-format string */
4760 body = alloc_trash_chunk();
4761 if (!body)
4762 goto fail_alloc;
4763 body->data = build_logline(s, body->area, body->size, &reply->body.fmt);
4764 }
4765 /* else no payload */
4766
4767 status = ultoa(reply->status);
4768 reason = http_get_reason(reply->status);
4769 slflags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_CLEN);
4770 if (!body || !b_data(body))
4771 slflags |= HTX_SL_F_BODYLESS;
4772 sl = htx_add_stline(htx, HTX_BLK_RES_SL, slflags, ist("HTTP/1.1"), ist(status), ist(reason));
4773 if (!sl)
4774 goto fail;
4775 sl->info.res.status = reply->status;
4776
4777 clen = (body ? ultoa(b_data(body)) : "0");
4778 ctype = reply->ctype;
4779
4780 if (!LIST_ISEMPTY(&reply->hdrs)) {
4781 struct http_reply_hdr *hdr;
4782 struct buffer *value = alloc_trash_chunk();
4783
4784 if (!value)
4785 goto fail;
4786
4787 list_for_each_entry(hdr, &reply->hdrs, list) {
4788 chunk_reset(value);
4789 value->data = build_logline(s, value->area, value->size, &hdr->value);
4790 if (b_data(value) && !htx_add_header(htx, hdr->name, ist2(b_head(value), b_data(value)))) {
4791 free_trash_chunk(value);
4792 goto fail;
4793 }
4794 chunk_reset(value);
4795 }
4796 free_trash_chunk(value);
4797 }
4798
4799 if (!htx_add_header(htx, ist("content-length"), ist(clen)) ||
4800 (body && b_data(body) && ctype && !htx_add_header(htx, ist("content-type"), ist(ctype))) ||
4801 !htx_add_endof(htx, HTX_BLK_EOH) ||
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004802 (body && b_data(body) && !htx_add_data_atonce(htx, ist2(b_head(body), b_data(body)))))
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004803 goto fail;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004804
4805 htx->flags |= HTX_FL_EOM;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004806 }
4807
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004808 leave:
4809 if (reply->type == HTTP_REPLY_LOGFMT)
4810 free_trash_chunk(body);
4811 return ret;
4812
4813 fail_alloc:
4814 if (!(s->flags & SF_ERR_MASK))
4815 s->flags |= SF_ERR_RESOURCE;
Christopher Faulet97e466c2020-05-15 15:12:47 +02004816 /* fall through */
4817 fail:
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004818 ret = -1;
4819 goto leave;
Christopher Faulet97e466c2020-05-15 15:12:47 +02004820}
4821
4822/* Send an http reply to the client. On success, it returns 0. If an error
Christopher Faulet40e6b552020-06-25 16:04:50 +02004823 * occurs -1 is returned and the response channel is truncated, removing this
4824 * way the faulty reply. This function may fail when the reply is formatted
4825 * (http_reply_to_htx) or when the reply is forwarded
4826 * (http_forward_proxy_resp). On the last case, it is because a
4827 * http-after-response rule fails.
Christopher Faulet97e466c2020-05-15 15:12:47 +02004828 */
4829int http_reply_message(struct stream *s, struct http_reply *reply)
4830{
4831 struct channel *res = &s->res;
4832 struct htx *htx = htx_from_buf(&res->buf);
4833
4834 if (s->txn->status == -1)
4835 s->txn->status = reply->status;
4836 channel_htx_truncate(res, htx);
4837
4838 if (http_reply_to_htx(s, htx, reply) == -1)
4839 goto fail;
4840
4841 htx_to_buf(htx, &s->res.buf);
4842 if (!http_forward_proxy_resp(s, 1))
4843 goto fail;
4844 return 0;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004845
4846 fail:
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004847 channel_htx_truncate(res, htx);
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004848 if (!(s->flags & SF_ERR_MASK))
4849 s->flags |= SF_ERR_PRXCOND;
Christopher Faulet97e466c2020-05-15 15:12:47 +02004850 return -1;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004851}
4852
Christopher Faulet50264b42022-03-30 19:39:30 +02004853/* Return the error message corresponding to s->conn_err_type. It is assumed
Christopher Faulet304cc402019-07-15 15:46:28 +02004854 * that the server side is closed. Note that err_type is actually a
4855 * bitmask, where almost only aborts may be cumulated with other
4856 * values. We consider that aborted operations are more important
4857 * than timeouts or errors due to the fact that nobody else in the
4858 * logs might explain incomplete retries. All others should avoid
4859 * being cumulated. It should normally not be possible to have multiple
4860 * aborts at once, but just in case, the first one in sequence is reported.
4861 * Note that connection errors appearing on the second request of a keep-alive
4862 * connection are not reported since this allows the client to retry.
4863 */
Willy Tarreaub49672d2022-05-27 10:13:37 +02004864void http_return_srv_error(struct stream *s, struct stconn *sc)
Christopher Faulet304cc402019-07-15 15:46:28 +02004865{
Christopher Faulet50264b42022-03-30 19:39:30 +02004866 int err_type = s->conn_err_type;
Christopher Faulet304cc402019-07-15 15:46:28 +02004867
4868 /* set s->txn->status for http_error_message(s) */
Christopher Faulet50264b42022-03-30 19:39:30 +02004869 if (err_type & STRM_ET_QUEUE_ABRT) {
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004870 s->txn->status = -1;
Willy Tarreaub49672d2022-05-27 10:13:37 +02004871 http_server_error(s, sc, SF_ERR_CLICL, SF_FINST_Q, NULL);
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004872 }
Christopher Faulet50264b42022-03-30 19:39:30 +02004873 else if (err_type & STRM_ET_CONN_ABRT) {
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004874 s->txn->status = -1;
Willy Tarreaub49672d2022-05-27 10:13:37 +02004875 http_server_error(s, sc, SF_ERR_CLICL, SF_FINST_C, NULL);
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004876 }
Christopher Faulet50264b42022-03-30 19:39:30 +02004877 else if (err_type & STRM_ET_QUEUE_TO) {
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004878 s->txn->status = 503;
Willy Tarreaub49672d2022-05-27 10:13:37 +02004879 http_server_error(s, sc, SF_ERR_SRVTO, SF_FINST_Q,
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004880 http_error_message(s));
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004881 }
Christopher Faulet50264b42022-03-30 19:39:30 +02004882 else if (err_type & STRM_ET_QUEUE_ERR) {
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004883 s->txn->status = 503;
Willy Tarreaub49672d2022-05-27 10:13:37 +02004884 http_server_error(s, sc, SF_ERR_SRVCL, SF_FINST_Q,
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004885 http_error_message(s));
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004886 }
Christopher Faulet50264b42022-03-30 19:39:30 +02004887 else if (err_type & STRM_ET_CONN_TO) {
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004888 s->txn->status = 503;
Willy Tarreaub49672d2022-05-27 10:13:37 +02004889 http_server_error(s, sc, SF_ERR_SRVTO, SF_FINST_C,
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004890 (s->txn->flags & TX_NOT_FIRST) ? NULL :
4891 http_error_message(s));
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004892 }
Christopher Faulet50264b42022-03-30 19:39:30 +02004893 else if (err_type & STRM_ET_CONN_ERR) {
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004894 s->txn->status = 503;
Willy Tarreaub49672d2022-05-27 10:13:37 +02004895 http_server_error(s, sc, SF_ERR_SRVCL, SF_FINST_C,
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004896 (s->flags & SF_SRV_REUSED) ? NULL :
4897 http_error_message(s));
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004898 }
Christopher Faulet50264b42022-03-30 19:39:30 +02004899 else if (err_type & STRM_ET_CONN_RES) {
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004900 s->txn->status = 503;
Willy Tarreaub49672d2022-05-27 10:13:37 +02004901 http_server_error(s, sc, SF_ERR_RESOURCE, SF_FINST_C,
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004902 (s->txn->flags & TX_NOT_FIRST) ? NULL :
4903 http_error_message(s));
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004904 }
Christopher Faulet50264b42022-03-30 19:39:30 +02004905 else { /* STRM_ET_CONN_OTHER and others */
Christopher Faulet304cc402019-07-15 15:46:28 +02004906 s->txn->status = 500;
Willy Tarreaub49672d2022-05-27 10:13:37 +02004907 http_server_error(s, sc, SF_ERR_INTERNAL, SF_FINST_C,
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004908 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004909 }
4910}
4911
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004912
Christopher Faulet4a28a532019-03-01 11:19:40 +01004913/* Handle Expect: 100-continue for HTTP/1.1 messages if necessary. It returns 0
4914 * on success and -1 on error.
4915 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004916static int http_handle_expect_hdr(struct stream *s, struct htx *htx, struct http_msg *msg)
Christopher Faulet4a28a532019-03-01 11:19:40 +01004917{
4918 /* If we have HTTP/1.1 message with a body and Expect: 100-continue,
4919 * then we must send an HTTP/1.1 100 Continue intermediate response.
4920 */
4921 if (msg->msg_state == HTTP_MSG_BODY && (msg->flags & HTTP_MSGF_VER_11) &&
4922 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
4923 struct ist hdr = { .ptr = "Expect", .len = 6 };
4924 struct http_hdr_ctx ctx;
4925
4926 ctx.blk = NULL;
4927 /* Expect is allowed in 1.1, look for it */
4928 if (http_find_header(htx, hdr, &ctx, 0) &&
4929 unlikely(isteqi(ctx.value, ist2("100-continue", 12)))) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004930 if (http_reply_100_continue(s) == -1)
Christopher Faulet4a28a532019-03-01 11:19:40 +01004931 return -1;
4932 http_remove_header(htx, &ctx);
4933 }
4934 }
4935 return 0;
4936}
4937
Christopher Faulet23a3c792018-11-28 10:01:23 +01004938/* Send a 100-Continue response to the client. It returns 0 on success and -1
4939 * on error. The response channel is updated accordingly.
4940 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004941static int http_reply_100_continue(struct stream *s)
Christopher Faulet23a3c792018-11-28 10:01:23 +01004942{
4943 struct channel *res = &s->res;
4944 struct htx *htx = htx_from_buf(&res->buf);
4945 struct htx_sl *sl;
4946 unsigned int flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|
4947 HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
Christopher Faulet23a3c792018-11-28 10:01:23 +01004948
4949 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
4950 ist("HTTP/1.1"), ist("100"), ist("Continue"));
4951 if (!sl)
4952 goto fail;
4953 sl->info.res.status = 100;
4954
Christopher Faulet1d5ec092019-06-26 14:23:54 +02004955 if (!htx_add_endof(htx, HTX_BLK_EOH))
Christopher Faulet23a3c792018-11-28 10:01:23 +01004956 goto fail;
4957
Christopher Fauleta72a7e42020-01-28 09:28:11 +01004958 if (!http_forward_proxy_resp(s, 0))
4959 goto fail;
Christopher Faulet23a3c792018-11-28 10:01:23 +01004960 return 0;
4961
4962 fail:
4963 /* If an error occurred, remove the incomplete HTTP response from the
4964 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004965 channel_htx_truncate(res, htx);
Christopher Faulet23a3c792018-11-28 10:01:23 +01004966 return -1;
4967}
4968
Christopher Faulet12c51e22018-11-28 15:59:42 +01004969
Christopher Faulet0f226952018-10-22 09:29:56 +02004970/*
4971 * Capture headers from message <htx> according to header list <cap_hdr>, and
4972 * fill the <cap> pointers appropriately.
4973 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004974static void http_capture_headers(struct htx *htx, char **cap, struct cap_hdr *cap_hdr)
Christopher Faulet0f226952018-10-22 09:29:56 +02004975{
4976 struct cap_hdr *h;
4977 int32_t pos;
4978
Christopher Fauleta3f15502019-05-13 15:27:23 +02004979 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet0f226952018-10-22 09:29:56 +02004980 struct htx_blk *blk = htx_get_blk(htx, pos);
4981 enum htx_blk_type type = htx_get_blk_type(blk);
4982 struct ist n, v;
4983
4984 if (type == HTX_BLK_EOH)
4985 break;
4986 if (type != HTX_BLK_HDR)
4987 continue;
4988
4989 n = htx_get_blk_name(htx, blk);
4990
4991 for (h = cap_hdr; h; h = h->next) {
4992 if (h->namelen && (h->namelen == n.len) &&
4993 (strncasecmp(n.ptr, h->name, h->namelen) == 0)) {
4994 if (cap[h->index] == NULL)
4995 cap[h->index] =
4996 pool_alloc(h->pool);
4997
4998 if (cap[h->index] == NULL) {
4999 ha_alert("HTTP capture : out of memory.\n");
5000 break;
5001 }
5002
5003 v = htx_get_blk_value(htx, blk);
Tim Duesterhus2471f5c2021-11-08 09:05:01 +01005004 v = isttrim(v, h->len);
Christopher Faulet0f226952018-10-22 09:29:56 +02005005
5006 memcpy(cap[h->index], v.ptr, v.len);
5007 cap[h->index][v.len]=0;
5008 }
5009 }
5010 }
5011}
5012
Christopher Faulet0b6bdc52018-10-24 11:05:36 +02005013/* Delete a value in a header between delimiters <from> and <next>. The header
5014 * itself is delimited by <start> and <end> pointers. The number of characters
5015 * displaced is returned, and the pointer to the first delimiter is updated if
5016 * required. The function tries as much as possible to respect the following
5017 * principles :
5018 * - replace <from> delimiter by the <next> one unless <from> points to <start>,
5019 * in which case <next> is simply removed
5020 * - set exactly one space character after the new first delimiter, unless there
5021 * are not enough characters in the block being moved to do so.
5022 * - remove unneeded spaces before the previous delimiter and after the new
5023 * one.
5024 *
5025 * It is the caller's responsibility to ensure that :
5026 * - <from> points to a valid delimiter or <start> ;
5027 * - <next> points to a valid delimiter or <end> ;
5028 * - there are non-space chars before <from>.
5029 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005030static int http_del_hdr_value(char *start, char *end, char **from, char *next)
Christopher Faulet0b6bdc52018-10-24 11:05:36 +02005031{
5032 char *prev = *from;
5033
5034 if (prev == start) {
5035 /* We're removing the first value. eat the semicolon, if <next>
5036 * is lower than <end> */
5037 if (next < end)
5038 next++;
5039
5040 while (next < end && HTTP_IS_SPHT(*next))
5041 next++;
5042 }
5043 else {
5044 /* Remove useless spaces before the old delimiter. */
5045 while (HTTP_IS_SPHT(*(prev-1)))
5046 prev--;
5047 *from = prev;
5048
5049 /* copy the delimiter and if possible a space if we're
5050 * not at the end of the line.
5051 */
5052 if (next < end) {
5053 *prev++ = *next++;
5054 if (prev + 1 < next)
5055 *prev++ = ' ';
5056 while (next < end && HTTP_IS_SPHT(*next))
5057 next++;
5058 }
5059 }
5060 memmove(prev, next, end - next);
5061 return (prev - next);
5062}
5063
Christopher Faulet0f226952018-10-22 09:29:56 +02005064
5065/* Formats the start line of the request (without CRLF) and puts it in <str> and
Joseph Herlantc42c0e92018-11-25 10:43:27 -08005066 * return the written length. The line can be truncated if it exceeds <len>.
Christopher Faulet0f226952018-10-22 09:29:56 +02005067 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005068static size_t http_fmt_req_line(const struct htx_sl *sl, char *str, size_t len)
Christopher Faulet0f226952018-10-22 09:29:56 +02005069{
5070 struct ist dst = ist2(str, 0);
5071
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005072 if (istcat(&dst, htx_sl_req_meth(sl), len) == -1)
Christopher Faulet0f226952018-10-22 09:29:56 +02005073 goto end;
5074 if (dst.len + 1 > len)
5075 goto end;
5076 dst.ptr[dst.len++] = ' ';
5077
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005078 if (istcat(&dst, htx_sl_req_uri(sl), len) == -1)
Christopher Faulet0f226952018-10-22 09:29:56 +02005079 goto end;
5080 if (dst.len + 1 > len)
5081 goto end;
5082 dst.ptr[dst.len++] = ' ';
5083
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005084 istcat(&dst, htx_sl_req_vsn(sl), len);
Christopher Faulet0f226952018-10-22 09:29:56 +02005085 end:
5086 return dst.len;
5087}
5088
5089/*
5090 * Print a debug line with a start line.
5091 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005092static void http_debug_stline(const char *dir, struct stream *s, const struct htx_sl *sl)
Christopher Faulet0f226952018-10-22 09:29:56 +02005093{
5094 struct session *sess = strm_sess(s);
5095 int max;
5096
5097 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
5098 dir,
Willy Tarreau88bc8002021-12-06 07:01:02 +00005099 objt_conn(sess->origin) ? (unsigned short)__objt_conn(sess->origin)->handle.fd : -1,
Willy Tarreaufd9417b2022-05-18 16:23:22 +02005100 sc_conn(s->scb) ? (unsigned short)(__sc_conn(s->scb))->handle.fd : -1);
Christopher Faulet0f226952018-10-22 09:29:56 +02005101
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005102 max = HTX_SL_P1_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02005103 UBOUND(max, trash.size - trash.data - 3);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005104 chunk_memcat(&trash, HTX_SL_P1_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02005105 trash.area[trash.data++] = ' ';
5106
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005107 max = HTX_SL_P2_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02005108 UBOUND(max, trash.size - trash.data - 2);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005109 chunk_memcat(&trash, HTX_SL_P2_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02005110 trash.area[trash.data++] = ' ';
5111
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005112 max = HTX_SL_P3_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02005113 UBOUND(max, trash.size - trash.data - 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005114 chunk_memcat(&trash, HTX_SL_P3_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02005115 trash.area[trash.data++] = '\n';
5116
Willy Tarreau2e8ab6b2020-03-14 11:03:20 +01005117 DISGUISE(write(1, trash.area, trash.data));
Christopher Faulet0f226952018-10-22 09:29:56 +02005118}
5119
5120/*
5121 * Print a debug line with a header.
5122 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005123static 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 +02005124{
5125 struct session *sess = strm_sess(s);
5126 int max;
5127
5128 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
5129 dir,
Willy Tarreau88bc8002021-12-06 07:01:02 +00005130 objt_conn(sess->origin) ? (unsigned short)__objt_conn(sess->origin)->handle.fd : -1,
Willy Tarreaufd9417b2022-05-18 16:23:22 +02005131 sc_conn(s->scb) ? (unsigned short)(__sc_conn(s->scb))->handle.fd : -1);
Christopher Faulet0f226952018-10-22 09:29:56 +02005132
5133 max = n.len;
5134 UBOUND(max, trash.size - trash.data - 3);
5135 chunk_memcat(&trash, n.ptr, max);
5136 trash.area[trash.data++] = ':';
5137 trash.area[trash.data++] = ' ';
5138
5139 max = v.len;
5140 UBOUND(max, trash.size - trash.data - 1);
5141 chunk_memcat(&trash, v.ptr, max);
5142 trash.area[trash.data++] = '\n';
5143
Willy Tarreau2e8ab6b2020-03-14 11:03:20 +01005144 DISGUISE(write(1, trash.area, trash.data));
Christopher Faulet0f226952018-10-22 09:29:56 +02005145}
5146
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005147void http_txn_reset_req(struct http_txn *txn)
5148{
Christopher Faulet1aea50e2020-01-17 16:03:53 +01005149 txn->req.flags = 0;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005150 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
5151}
5152
5153void http_txn_reset_res(struct http_txn *txn)
5154{
Christopher Faulet1aea50e2020-01-17 16:03:53 +01005155 txn->rsp.flags = 0;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005156 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
5157}
5158
5159/*
Christopher Faulet75f619a2021-03-08 19:12:58 +01005160 * Create and initialize a new HTTP transaction for stream <s>. This should be
5161 * used before processing any new request. It returns the transaction or NLULL
5162 * on error.
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005163 */
Christopher Faulet75f619a2021-03-08 19:12:58 +01005164struct http_txn *http_create_txn(struct stream *s)
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005165{
Christopher Faulet75f619a2021-03-08 19:12:58 +01005166 struct http_txn *txn;
Willy Tarreaub49672d2022-05-27 10:13:37 +02005167 struct stconn *sc = s->scf;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005168
Christopher Faulet75f619a2021-03-08 19:12:58 +01005169 txn = pool_alloc(pool_head_http_txn);
5170 if (!txn)
5171 return NULL;
5172 s->txn = txn;
5173
Willy Tarreaub49672d2022-05-27 10:13:37 +02005174 txn->flags = ((sc && sc_ep_test(sc, SE_FL_NOT_FIRST)) ? TX_NOT_FIRST : 0);
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005175 txn->status = -1;
Christopher Faulet5cb513a2020-05-13 17:56:56 +02005176 txn->http_reply = NULL;
Christopher Faulete05bf9e2022-03-29 15:23:40 +02005177 txn->l7_buffer = BUF_NULL;
Willy Tarreau8b507582020-02-25 09:35:07 +01005178 write_u32(txn->cache_hash, 0);
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005179
5180 txn->cookie_first_date = 0;
5181 txn->cookie_last_date = 0;
5182
5183 txn->srv_cookie = NULL;
5184 txn->cli_cookie = NULL;
5185 txn->uri = NULL;
5186
5187 http_txn_reset_req(txn);
5188 http_txn_reset_res(txn);
5189
5190 txn->req.chn = &s->req;
5191 txn->rsp.chn = &s->res;
5192
5193 txn->auth.method = HTTP_AUTH_UNKNOWN;
5194
Willy Tarreaub7bfcb32021-08-31 08:13:25 +02005195 vars_init_head(&s->vars_txn, SCOPE_TXN);
5196 vars_init_head(&s->vars_reqres, SCOPE_REQ);
Christopher Faulet75f619a2021-03-08 19:12:58 +01005197
5198 return txn;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005199}
5200
5201/* to be used at the end of a transaction */
Christopher Faulet75f619a2021-03-08 19:12:58 +01005202void http_destroy_txn(struct stream *s)
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005203{
5204 struct http_txn *txn = s->txn;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005205
5206 /* these ones will have been dynamically allocated */
5207 pool_free(pool_head_requri, txn->uri);
5208 pool_free(pool_head_capture, txn->cli_cookie);
5209 pool_free(pool_head_capture, txn->srv_cookie);
Tim Duesterhusa17e6622020-03-05 20:19:02 +01005210 pool_free(pool_head_uniqueid, s->unique_id.ptr);
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005211
Tim Duesterhusa17e6622020-03-05 20:19:02 +01005212 s->unique_id = IST_NULL;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005213 txn->uri = NULL;
5214 txn->srv_cookie = NULL;
5215 txn->cli_cookie = NULL;
5216
Christopher Faulet59399252019-11-07 14:27:52 +01005217 if (!LIST_ISEMPTY(&s->vars_txn.head))
5218 vars_prune(&s->vars_txn, s->sess, s);
5219 if (!LIST_ISEMPTY(&s->vars_reqres.head))
5220 vars_prune(&s->vars_reqres, s->sess, s);
Christopher Faulet75f619a2021-03-08 19:12:58 +01005221
Christopher Faulete05bf9e2022-03-29 15:23:40 +02005222 b_free(&txn->l7_buffer);
5223
Christopher Faulet75f619a2021-03-08 19:12:58 +01005224 pool_free(pool_head_http_txn, txn);
5225 s->txn = NULL;
Christopher Faulet59399252019-11-07 14:27:52 +01005226}
5227
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005228
5229DECLARE_POOL(pool_head_http_txn, "http_txn", sizeof(struct http_txn));
Christopher Faulet0f226952018-10-22 09:29:56 +02005230
Christopher Fauletf4eb75d2018-10-11 15:55:07 +02005231/*
5232 * Local variables:
5233 * c-indent-level: 8
5234 * c-basic-offset: 8
5235 * End:
5236 */