blob: d2da6282ba703bd31adc3a08aa42ed88a24ff06e [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>
Christopher Faulet8da67aa2022-03-29 17:53:09 +020024#include <haproxy/conn_stream.h>
25#include <haproxy/cs_utils.h>
Willy Tarreau36979d92020-06-05 17:27:29 +020026#include <haproxy/errors.h>
Willy Tarreauc7babd82020-06-04 21:29:29 +020027#include <haproxy/filters.h>
Willy Tarreaucd72d8c2020-06-02 19:11:26 +020028#include <haproxy/http.h>
Willy Tarreauc2b1ff02020-06-04 21:21:03 +020029#include <haproxy/http_ana.h>
Willy Tarreau87735332020-06-04 09:08:41 +020030#include <haproxy/http_htx.h>
Willy Tarreau16f958c2020-06-03 08:44:35 +020031#include <haproxy/htx.h>
Willy Tarreauaeed4a82020-06-04 22:01:04 +020032#include <haproxy/log.h>
Willy Tarreau6131d6a2020-06-02 16:48:09 +020033#include <haproxy/net_helper.h>
Willy Tarreaua264d962020-06-04 22:29:18 +020034#include <haproxy/proxy.h>
Willy Tarreau7cd8b6e2020-06-02 17:32:26 +020035#include <haproxy/regex.h>
Willy Tarreau1e56f922020-06-04 23:20:13 +020036#include <haproxy/server-t.h>
Willy Tarreau2eec9b52020-06-04 19:58:55 +020037#include <haproxy/stats.h>
Willy 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 Faulet3e964192018-10-24 11:39:23 +020063
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020064static void http_manage_client_side_cookies(struct stream *s, struct channel *req);
65static void http_manage_server_side_cookies(struct stream *s, struct channel *res);
Christopher Fauletfcda7c62018-10-24 11:56:22 +020066
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020067static int http_stats_check_uri(struct stream *s, struct http_txn *txn, struct proxy *backend);
68static int http_handle_stats(struct stream *s, struct channel *req);
Christopher Faulet377c5a52018-10-24 21:21:30 +020069
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020070static int http_handle_expect_hdr(struct stream *s, struct htx *htx, struct http_msg *msg);
71static int http_reply_100_continue(struct stream *s);
Christopher Faulet23a3c792018-11-28 10:01:23 +010072
Christopher Faulete0768eb2018-10-03 16:38:02 +020073/* This stream analyser waits for a complete HTTP request. It returns 1 if the
74 * processing can continue on next analysers, or zero if it either needs more
75 * data or wants to immediately abort the request (eg: timeout, error, ...). It
76 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req.analysers
77 * when it has nothing left to do, and may remove any analyser when it wants to
78 * abort.
79 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020080int http_wait_for_request(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +020081{
Christopher Faulet9768c262018-10-22 09:34:31 +020082
Christopher Faulete0768eb2018-10-03 16:38:02 +020083 /*
Christopher Faulet9768c262018-10-22 09:34:31 +020084 * We will analyze a complete HTTP request to check the its syntax.
Christopher Faulete0768eb2018-10-03 16:38:02 +020085 *
Christopher Faulet9768c262018-10-22 09:34:31 +020086 * Once the start line and all headers are received, we may perform a
87 * capture of the error (if any), and we will set a few fields. We also
88 * check for monitor-uri, logging and finally headers capture.
Christopher Faulete0768eb2018-10-03 16:38:02 +020089 */
Christopher Faulete0768eb2018-10-03 16:38:02 +020090 struct session *sess = s->sess;
91 struct http_txn *txn = s->txn;
92 struct http_msg *msg = &txn->req;
Christopher Faulet9768c262018-10-22 09:34:31 +020093 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +010094 struct htx_sl *sl;
Christopher Faulete0768eb2018-10-03 16:38:02 +020095
Christopher Fauleteea8fc72019-11-05 16:18:10 +010096 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +020097
Christopher Fauletda46a0d2021-01-21 17:32:58 +010098 if (unlikely(!IS_HTX_STRM(s))) {
99 /* It is only possible when a TCP stream is upgrade to HTTP.
100 * There is a transition period during which there is no
101 * data. The stream is still in raw mode and SF_IGNORE flag is
102 * still set. When this happens, the new mux is responsible to
Ilya Shipitsinacf84592021-02-06 22:29:08 +0500103 * handle all errors. Thus we may leave immediately.
Christopher Fauletda46a0d2021-01-21 17:32:58 +0100104 */
105 BUG_ON(!(s->flags & SF_IGNORE) || !c_empty(&s->req));
Christopher Faulet9768c262018-10-22 09:34:31 +0200106
Christopher Faulet97b3a612021-03-15 17:10:12 +0100107 /* Don't connect for now */
108 channel_dont_connect(req);
109
110 /* A SHUTR at this stage means we are performing a "destructive"
111 * HTTP upgrade (TCP>H2). In this case, we can leave.
112 */
113 if (req->flags & CF_SHUTR) {
114 s->logs.logwait = 0;
115 s->logs.level = 0;
116 channel_abort(&s->req);
117 channel_abort(&s->res);
118 req->analysers &= AN_REQ_FLT_END;
119 req->analyse_exp = TICK_ETERNITY;
120 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA, s);
121 return 1;
122 }
Christopher Fauletda46a0d2021-01-21 17:32:58 +0100123 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA, s);
124 return 0;
125 }
126
127 htx = htxbuf(&req->buf);
Christopher Faulet8bebd2f2020-10-06 17:54:56 +0200128
Willy Tarreau4236f032019-03-05 10:43:32 +0100129 /* Parsing errors are caught here */
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200130 if (htx->flags & (HTX_FL_PARSING_ERROR|HTX_FL_PROCESSING_ERROR)) {
Willy Tarreau4236f032019-03-05 10:43:32 +0100131 stream_inc_http_req_ctr(s);
Emeric Brun28976442020-10-07 08:50:09 +0200132 proxy_inc_fe_req_ctr(sess->listener, sess->fe);
Christopher Fauletbf7175f2021-02-10 14:58:01 +0100133 if (htx->flags & HTX_FL_PARSING_ERROR) {
134 stream_inc_http_err_ctr(s);
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200135 goto return_bad_req;
Christopher Fauletbf7175f2021-02-10 14:58:01 +0100136 }
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200137 else
138 goto return_int_err;
Willy Tarreau4236f032019-03-05 10:43:32 +0100139 }
140
Christopher Faulete0768eb2018-10-03 16:38:02 +0200141 /* we're speaking HTTP here, so let's speak HTTP to the client */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200142 s->srv_error = http_return_srv_error;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200143
Christopher Faulet9768c262018-10-22 09:34:31 +0200144 msg->msg_state = HTTP_MSG_BODY;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200145 stream_inc_http_req_ctr(s);
Emeric Brun28976442020-10-07 08:50:09 +0200146 proxy_inc_fe_req_ctr(sess->listener, sess->fe); /* one more valid request for this FE */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200147
Christopher Faulet9768c262018-10-22 09:34:31 +0200148 /* kill the pending keep-alive timeout */
Christopher Faulet9768c262018-10-22 09:34:31 +0200149 req->analyse_exp = TICK_ETERNITY;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200150
Christopher Faulet29f17582019-05-23 11:03:26 +0200151 BUG_ON(htx_get_first_type(htx) != HTX_BLK_REQ_SL);
Christopher Faulet297fbb42019-05-13 14:41:27 +0200152 sl = http_get_stline(htx);
Christopher Faulet03599112018-11-27 11:21:21 +0100153
Christopher Faulet9768c262018-10-22 09:34:31 +0200154 /* 0: we might have to print this header in debug mode */
155 if (unlikely((global.mode & MODE_DEBUG) &&
156 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
157 int32_t pos;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200158
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200159 http_debug_stline("clireq", s, sl);
Christopher Faulet9768c262018-10-22 09:34:31 +0200160
Christopher Fauleta3f15502019-05-13 15:27:23 +0200161 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet9768c262018-10-22 09:34:31 +0200162 struct htx_blk *blk = htx_get_blk(htx, pos);
163 enum htx_blk_type type = htx_get_blk_type(blk);
164
165 if (type == HTX_BLK_EOH)
166 break;
167 if (type != HTX_BLK_HDR)
168 continue;
169
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200170 http_debug_hdr("clihdr", s,
171 htx_get_blk_name(htx, blk),
172 htx_get_blk_value(htx, blk));
Christopher Faulet9768c262018-10-22 09:34:31 +0200173 }
174 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200175
176 /*
Christopher Faulet03599112018-11-27 11:21:21 +0100177 * 1: identify the method and the version. Also set HTTP flags
Christopher Faulete0768eb2018-10-03 16:38:02 +0200178 */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100179 txn->meth = sl->info.req.meth;
Christopher Faulet03599112018-11-27 11:21:21 +0100180 if (sl->flags & HTX_SL_F_VER_11)
Christopher Faulet9768c262018-10-22 09:34:31 +0200181 msg->flags |= HTTP_MSGF_VER_11;
Christopher Faulet03599112018-11-27 11:21:21 +0100182 msg->flags |= HTTP_MSGF_XFER_LEN;
Christopher Faulet2a408542020-11-20 14:22:37 +0100183 if (sl->flags & HTX_SL_F_CLEN)
184 msg->flags |= HTTP_MSGF_CNT_LEN;
185 else if (sl->flags & HTX_SL_F_CHNK)
186 msg->flags |= HTTP_MSGF_TE_CHNK;
Christopher Fauletb2db4fa2018-11-27 16:51:09 +0100187 if (sl->flags & HTX_SL_F_BODYLESS)
188 msg->flags |= HTTP_MSGF_BODYLESS;
Christopher Faulet576c3582021-01-08 15:53:01 +0100189 if (sl->flags & HTX_SL_F_CONN_UPG)
190 msg->flags |= HTTP_MSGF_CONN_UPG;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200191
192 /* we can make use of server redirect on GET and HEAD */
193 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
194 s->flags |= SF_REDIRECTABLE;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100195 else if (txn->meth == HTTP_METH_OTHER && isteqi(htx_sl_req_meth(sl), ist("PRI"))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200196 /* PRI is reserved for the HTTP/2 preface */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200197 goto return_bad_req;
198 }
199
200 /*
Christopher Faulet6072beb2020-02-18 15:34:58 +0100201 * 2: check if the URI matches the monitor_uri. We have to do this for
202 * every request which gets in, because the monitor-uri is defined by
203 * the frontend. If the monitor-uri starts with a '/', the matching is
204 * done against the request's path. Otherwise, the request's uri is
205 * used. It is a workaround to let HTTP/2 health-checks work as
206 * expected.
Christopher Faulete0768eb2018-10-03 16:38:02 +0200207 */
Tim Duesterhus4b1fcaa2022-03-05 00:52:40 +0100208 if (unlikely(isttest(sess->fe->monitor_uri))) {
209 const struct ist monitor_uri = sess->fe->monitor_uri;
Amaury Denoyellec453f952021-07-06 11:40:12 +0200210 struct http_uri_parser parser = http_uri_parser_init(htx_sl_req_uri(sl));
Christopher Faulete0768eb2018-10-03 16:38:02 +0200211
Amaury Denoyelle5a9bd372021-07-06 11:23:10 +0200212 if ((istptr(monitor_uri)[0] == '/' &&
Amaury Denoyellec453f952021-07-06 11:40:12 +0200213 isteq(http_parse_path(&parser), monitor_uri)) ||
Amaury Denoyelle5a9bd372021-07-06 11:23:10 +0200214 isteq(htx_sl_req_uri(sl), monitor_uri)) {
215 /*
216 * We have found the monitor URI
217 */
218 struct acl_cond *cond;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200219
Amaury Denoyelle5a9bd372021-07-06 11:23:10 +0200220 s->flags |= SF_MONITOR;
221 _HA_ATOMIC_INC(&sess->fe->fe_counters.intercepted_req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200222
Amaury Denoyelle5a9bd372021-07-06 11:23:10 +0200223 /* Check if we want to fail this monitor request or not */
224 list_for_each_entry(cond, &sess->fe->mon_fail_cond, list) {
225 int ret = acl_exec_cond(cond, sess->fe, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200226
Amaury Denoyelle5a9bd372021-07-06 11:23:10 +0200227 ret = acl_pass(ret);
228 if (cond->pol == ACL_COND_UNLESS)
229 ret = !ret;
230
231 if (ret) {
232 /* we fail this request, let's return 503 service unavail */
233 txn->status = 503;
234 if (!(s->flags & SF_ERR_MASK))
235 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
236 goto return_prx_cond;
237 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200238 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200239
Amaury Denoyelle5a9bd372021-07-06 11:23:10 +0200240 /* nothing to fail, let's reply normally */
241 txn->status = 200;
242 if (!(s->flags & SF_ERR_MASK))
243 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
244 goto return_prx_cond;
245 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200246 }
247
248 /*
249 * 3: Maybe we have to copy the original REQURI for the logs ?
250 * Note: we cannot log anymore if the request has been
251 * classified as invalid.
252 */
253 if (unlikely(s->logs.logwait & LW_REQ)) {
254 /* we have a complete HTTP request that we must log */
255 if ((txn->uri = pool_alloc(pool_head_requri)) != NULL) {
Christopher Faulet9768c262018-10-22 09:34:31 +0200256 size_t len;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200257
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200258 len = http_fmt_req_line(sl, txn->uri, global.tune.requri_len - 1);
Christopher Faulet9768c262018-10-22 09:34:31 +0200259 txn->uri[len] = 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200260
261 if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT)))
262 s->do_log(s);
263 } else {
264 ha_alert("HTTP logging : out of memory.\n");
265 }
266 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200267
Christopher Faulete0768eb2018-10-03 16:38:02 +0200268 /* if the frontend has "option http-use-proxy-header", we'll check if
269 * we have what looks like a proxied connection instead of a connection,
270 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
271 * Note that this is *not* RFC-compliant, however browsers and proxies
272 * happen to do that despite being non-standard :-(
273 * We consider that a request not beginning with either '/' or '*' is
274 * a proxied connection, which covers both "scheme://location" and
275 * CONNECT ip:port.
276 */
277 if ((sess->fe->options2 & PR_O2_USE_PXHDR) &&
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100278 *HTX_SL_REQ_UPTR(sl) != '/' && *HTX_SL_REQ_UPTR(sl) != '*')
Christopher Faulete0768eb2018-10-03 16:38:02 +0200279 txn->flags |= TX_USE_PX_CONN;
280
Christopher Faulete0768eb2018-10-03 16:38:02 +0200281 /* 5: we may need to capture headers */
282 if (unlikely((s->logs.logwait & LW_REQHDR) && s->req_cap))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200283 http_capture_headers(htx, s->req_cap, sess->fe->req_cap);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200284
Christopher Faulete0768eb2018-10-03 16:38:02 +0200285 /* we may have to wait for the request's body */
Christopher Faulet9768c262018-10-22 09:34:31 +0200286 if (s->be->options & PR_O_WREQ_BODY)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200287 req->analysers |= AN_REQ_HTTP_BODY;
288
289 /*
290 * RFC7234#4:
291 * A cache MUST write through requests with methods
292 * that are unsafe (Section 4.2.1 of [RFC7231]) to
293 * the origin server; i.e., a cache is not allowed
294 * to generate a reply to such a request before
295 * having forwarded the request and having received
296 * a corresponding response.
297 *
298 * RFC7231#4.2.1:
299 * Of the request methods defined by this
300 * specification, the GET, HEAD, OPTIONS, and TRACE
301 * methods are defined to be safe.
302 */
303 if (likely(txn->meth == HTTP_METH_GET ||
304 txn->meth == HTTP_METH_HEAD ||
305 txn->meth == HTTP_METH_OPTIONS ||
306 txn->meth == HTTP_METH_TRACE))
307 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
308
309 /* end of job, return OK */
310 req->analysers &= ~an_bit;
311 req->analyse_exp = TICK_ETERNITY;
Christopher Faulet9768c262018-10-22 09:34:31 +0200312
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100313 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200314 return 1;
315
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200316 return_int_err:
317 txn->status = 500;
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200318 if (!(s->flags & SF_ERR_MASK))
319 s->flags |= SF_ERR_INTERNAL;
Willy Tarreau4781b152021-04-06 13:53:36 +0200320 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
William Lallemand36119de2021-03-08 15:26:48 +0100321 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200322 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200323 goto return_prx_cond;
324
Christopher Faulete0768eb2018-10-03 16:38:02 +0200325 return_bad_req:
Christopher Faulet9768c262018-10-22 09:34:31 +0200326 txn->status = 400;
Willy Tarreau4781b152021-04-06 13:53:36 +0200327 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req);
William Lallemand36119de2021-03-08 15:26:48 +0100328 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200329 _HA_ATOMIC_INC(&sess->listener->counters->failed_req);
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200330 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200331
332 return_prx_cond:
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200333 http_reply_and_close(s, txn->status, http_error_message(s));
334
Christopher Faulete0768eb2018-10-03 16:38:02 +0200335 if (!(s->flags & SF_ERR_MASK))
336 s->flags |= SF_ERR_PRXCOND;
337 if (!(s->flags & SF_FINST_MASK))
338 s->flags |= SF_FINST_R;
339
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100340 DBG_TRACE_DEVEL("leaving on error",
341 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200342 return 0;
343}
344
345
346/* This stream analyser runs all HTTP request processing which is common to
347 * frontends and backends, which means blocking ACLs, filters, connection-close,
348 * reqadd, stats and redirects. This is performed for the designated proxy.
349 * It returns 1 if the processing can continue on next analysers, or zero if it
350 * either needs more data or wants to immediately abort the request (eg: deny,
351 * error, ...).
352 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200353int http_process_req_common(struct stream *s, struct channel *req, int an_bit, struct proxy *px)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200354{
Christopher Fauletd4150ad2021-10-13 15:35:55 +0200355 struct list *def_rules, *rules;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200356 struct session *sess = s->sess;
357 struct http_txn *txn = s->txn;
358 struct http_msg *msg = &txn->req;
Christopher Fauletff2759f2018-10-24 11:13:16 +0200359 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200360 struct redirect_rule *rule;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200361 enum rule_result verdict;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200362 struct connection *conn = objt_conn(sess->origin);
363
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100364 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200365
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100366 htx = htxbuf(&req->buf);
Christopher Fauletff2759f2018-10-24 11:13:16 +0200367
Christopher Faulet1907ccc2019-04-29 13:12:02 +0200368 /* just in case we have some per-backend tracking. Only called the first
369 * execution of the analyser. */
Christopher Fauletd4150ad2021-10-13 15:35:55 +0200370 if (!s->current_rule && !s->current_rule_list)
Christopher Faulet1907ccc2019-04-29 13:12:02 +0200371 stream_inc_be_http_req_ctr(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200372
Christopher Fauletd4150ad2021-10-13 15:35:55 +0200373 def_rules = ((px->defpx && (an_bit == AN_REQ_HTTP_PROCESS_FE || px != sess->fe)) ? &px->defpx->http_req_rules : NULL);
374 rules = &px->http_req_rules;
375
Christopher Faulete0768eb2018-10-03 16:38:02 +0200376 /* evaluate http-request rules */
Christopher Fauletd4150ad2021-10-13 15:35:55 +0200377 if ((def_rules && !LIST_ISEMPTY(def_rules)) || !LIST_ISEMPTY(rules)) {
378 verdict = http_req_get_intercept_rule(px, def_rules, rules, s);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200379
380 switch (verdict) {
381 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
382 goto return_prx_yield;
383
384 case HTTP_RULE_RES_CONT:
385 case HTTP_RULE_RES_STOP: /* nothing to do */
386 break;
387
388 case HTTP_RULE_RES_DENY: /* deny or tarpit */
389 if (txn->flags & TX_CLTARPIT)
390 goto tarpit;
391 goto deny;
392
393 case HTTP_RULE_RES_ABRT: /* abort request, response already sent. Eg: auth */
394 goto return_prx_cond;
395
396 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
397 goto done;
398
399 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
400 goto return_bad_req;
Christopher Faulet3a26bee2019-12-16 12:47:40 +0100401
402 case HTTP_RULE_RES_ERROR: /* failed with a bad request */
403 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200404 }
405 }
406
407 if (conn && (conn->flags & CO_FL_EARLY_DATA) &&
Olivier Houchard220a26c2020-01-23 14:57:36 +0100408 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_SSL_WAIT_HS))) {
Christopher Fauletff2759f2018-10-24 11:13:16 +0200409 struct http_hdr_ctx ctx;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200410
Christopher Fauletff2759f2018-10-24 11:13:16 +0200411 ctx.blk = NULL;
412 if (!http_find_header(htx, ist("Early-Data"), &ctx, 0)) {
413 if (unlikely(!http_add_header(htx, ist("Early-Data"), ist("1"))))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100414 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200415 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200416 }
417
418 /* OK at this stage, we know that the request was accepted according to
419 * the http-request rules, we can check for the stats. Note that the
420 * URI is detected *before* the req* rules in order not to be affected
421 * by a possible reqrep, while they are processed *after* so that a
422 * reqdeny can still block them. This clearly needs to change in 1.6!
423 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200424 if (!s->target && http_stats_check_uri(s, txn, px)) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200425 s->target = &http_stats_applet.obj_type;
Christopher Faulet1336ccf2022-04-12 18:15:16 +0200426 if (unlikely(!cs_applet_create(s->csb, objt_applet(s->target)))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200427 s->logs.tv_request = now;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200428 if (!(s->flags & SF_ERR_MASK))
429 s->flags |= SF_ERR_RESOURCE;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100430 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200431 }
432
433 /* parse the whole stats request and extract the relevant information */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200434 http_handle_stats(s, req);
Christopher Fauletd4150ad2021-10-13 15:35:55 +0200435 verdict = http_req_get_intercept_rule(px, NULL, &px->uri_auth->http_req_rules, s);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200436 /* not all actions implemented: deny, allow, auth */
437
438 if (verdict == HTTP_RULE_RES_DENY) /* stats http-request deny */
439 goto deny;
440
441 if (verdict == HTTP_RULE_RES_ABRT) /* stats auth / stats http-request auth */
442 goto return_prx_cond;
Christopher Faulet3a26bee2019-12-16 12:47:40 +0100443
444 if (verdict == HTTP_RULE_RES_BADREQ) /* failed with a bad request */
445 goto return_bad_req;
446
447 if (verdict == HTTP_RULE_RES_ERROR) /* failed with a bad request */
448 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200449 }
450
Christopher Faulet2571bc62019-03-01 11:44:26 +0100451 /* Proceed with the applets now. */
452 if (unlikely(objt_applet(s->target))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200453 if (sess->fe == s->be) /* report it if the request was intercepted by the frontend */
Willy Tarreau4781b152021-04-06 13:53:36 +0200454 _HA_ATOMIC_INC(&sess->fe->fe_counters.intercepted_req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200455
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200456 if (http_handle_expect_hdr(s, htx, msg) == -1)
Christopher Fauletb8a53712019-12-16 11:29:38 +0100457 goto return_int_err;
Christopher Fauletbcf242a2019-03-01 11:36:26 +0100458
Christopher Faulete0768eb2018-10-03 16:38:02 +0200459 if (!(s->flags & SF_ERR_MASK)) // this is not really an error but it is
460 s->flags |= SF_ERR_LOCAL; // to mark that it comes from the proxy
461 if (!(s->flags & SF_FINST_MASK))
462 s->flags |= SF_FINST_R;
463
Christopher Fauletc2ac5e42021-03-08 18:20:09 +0100464 if (HAS_FILTERS(s))
465 req->analysers |= AN_REQ_FLT_HTTP_HDRS;
466
Christopher Faulete0768eb2018-10-03 16:38:02 +0200467 /* enable the minimally required analyzers to handle keep-alive and compression on the HTTP response */
468 req->analysers &= (AN_REQ_HTTP_BODY | AN_REQ_FLT_HTTP_HDRS | AN_REQ_FLT_END);
469 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
470 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Christopher Fauletbcf242a2019-03-01 11:36:26 +0100471
472 req->flags |= CF_SEND_DONTWAIT;
473 s->flags |= SF_ASSIGNED;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200474 goto done;
475 }
476
477 /* check whether we have some ACLs set to redirect this request */
478 list_for_each_entry(rule, &px->redirect_rules, list) {
479 if (rule->cond) {
480 int ret;
481
482 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
483 ret = acl_pass(ret);
484 if (rule->cond->pol == ACL_COND_UNLESS)
485 ret = !ret;
486 if (!ret)
487 continue;
488 }
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200489 if (!http_apply_redirect_rule(rule, s, txn))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100490 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200491 goto done;
492 }
493
494 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
495 * If this happens, then the data will not come immediately, so we must
496 * send all what we have without waiting. Note that due to the small gain
497 * in waiting for the body of the request, it's easier to simply put the
498 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
499 * itself once used.
500 */
501 req->flags |= CF_SEND_DONTWAIT;
502
503 done: /* done with this analyser, continue with next ones that the calling
504 * points will have set, if any.
505 */
506 req->analyse_exp = TICK_ETERNITY;
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +0500507 done_without_exp: /* done with this analyser, but don't reset the analyse_exp. */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200508 req->analysers &= ~an_bit;
Christopher Fauletd4150ad2021-10-13 15:35:55 +0200509 s->current_rule = s->current_rule_list = NULL;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100510 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200511 return 1;
512
513 tarpit:
514 /* Allow cookie logging
515 */
516 if (s->be->cookie_name || sess->fe->capture_name)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200517 http_manage_client_side_cookies(s, req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200518
519 /* When a connection is tarpitted, we use the tarpit timeout,
520 * which may be the same as the connect timeout if unspecified.
521 * If unset, then set it to zero because we really want it to
522 * eventually expire. We build the tarpit as an analyser.
523 */
Christopher Faulet202c6ce2019-01-07 14:57:35 +0100524 channel_htx_erase(&s->req, htx);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200525
526 /* wipe the request out so that we can drop the connection early
527 * if the client closes first.
528 */
529 channel_dont_connect(req);
530
Christopher Faulete0768eb2018-10-03 16:38:02 +0200531 req->analysers &= AN_REQ_FLT_END; /* remove switching rules etc... */
532 req->analysers |= AN_REQ_HTTP_TARPIT;
533 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
534 if (!req->analyse_exp)
535 req->analyse_exp = tick_add(now_ms, 0);
536 stream_inc_http_err_ctr(s);
Willy Tarreau4781b152021-04-06 13:53:36 +0200537 _HA_ATOMIC_INC(&sess->fe->fe_counters.denied_req);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100538 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau4781b152021-04-06 13:53:36 +0200539 _HA_ATOMIC_INC(&s->be->be_counters.denied_req);
William Lallemand36119de2021-03-08 15:26:48 +0100540 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200541 _HA_ATOMIC_INC(&sess->listener->counters->denied_req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200542 goto done_without_exp;
543
544 deny: /* this request was blocked (denied) */
545
546 /* Allow cookie logging
547 */
548 if (s->be->cookie_name || sess->fe->capture_name)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200549 http_manage_client_side_cookies(s, req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200550
Christopher Faulete0768eb2018-10-03 16:38:02 +0200551 s->logs.tv_request = now;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200552 stream_inc_http_err_ctr(s);
Willy Tarreau4781b152021-04-06 13:53:36 +0200553 _HA_ATOMIC_INC(&sess->fe->fe_counters.denied_req);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100554 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau4781b152021-04-06 13:53:36 +0200555 _HA_ATOMIC_INC(&s->be->be_counters.denied_req);
William Lallemand36119de2021-03-08 15:26:48 +0100556 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200557 _HA_ATOMIC_INC(&sess->listener->counters->denied_req);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100558 goto return_prx_err;
559
560 return_int_err:
561 txn->status = 500;
562 if (!(s->flags & SF_ERR_MASK))
563 s->flags |= SF_ERR_INTERNAL;
Willy Tarreau4781b152021-04-06 13:53:36 +0200564 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100565 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau4781b152021-04-06 13:53:36 +0200566 _HA_ATOMIC_INC(&s->be->be_counters.internal_errors);
William Lallemand36119de2021-03-08 15:26:48 +0100567 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200568 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100569 goto return_prx_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200570
571 return_bad_req:
Christopher Faulete0768eb2018-10-03 16:38:02 +0200572 txn->status = 400;
Willy Tarreau4781b152021-04-06 13:53:36 +0200573 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req);
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->failed_req);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100576 /* fall through */
577
578 return_prx_err:
579 http_reply_and_close(s, txn->status, http_error_message(s));
580 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200581
582 return_prx_cond:
583 if (!(s->flags & SF_ERR_MASK))
584 s->flags |= SF_ERR_PRXCOND;
585 if (!(s->flags & SF_FINST_MASK))
586 s->flags |= SF_FINST_R;
587
588 req->analysers &= AN_REQ_FLT_END;
589 req->analyse_exp = TICK_ETERNITY;
Christopher Fauletd4150ad2021-10-13 15:35:55 +0200590 s->current_rule = s->current_rule_list = NULL;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100591 DBG_TRACE_DEVEL("leaving on error",
592 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200593 return 0;
594
595 return_prx_yield:
596 channel_dont_connect(req);
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100597 DBG_TRACE_DEVEL("waiting for more data",
598 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200599 return 0;
600}
601
602/* This function performs all the processing enabled for the current request.
603 * It returns 1 if the processing can continue on next analysers, or zero if it
604 * needs more data, encounters an error, or wants to immediately abort the
605 * request. It relies on buffers flags, and updates s->req.analysers.
606 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200607int http_process_request(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200608{
609 struct session *sess = s->sess;
610 struct http_txn *txn = s->txn;
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200611 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200612 struct connection *cli_conn = objt_conn(strm_sess(s)->origin);
613
Christopher Faulet8bebd2f2020-10-06 17:54:56 +0200614 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200615
616 /*
617 * Right now, we know that we have processed the entire headers
618 * and that unwanted requests have been filtered out. We can do
619 * whatever we want with the remaining request. Also, now we
620 * may have separate values for ->fe, ->be.
621 */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100622 htx = htxbuf(&req->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200623
624 /*
Christopher Faulete0768eb2018-10-03 16:38:02 +0200625 * 7: Now we can work with the cookies.
626 * Note that doing so might move headers in the request, but
627 * the fields will stay coherent and the URI will not move.
628 * This should only be performed in the backend.
629 */
630 if (s->be->cookie_name || sess->fe->capture_name)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200631 http_manage_client_side_cookies(s, req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200632
Tim Duesterhus2825b4b2020-02-28 15:13:34 +0100633 /* 8: Generate unique ID if a "unique-id-format" is defined.
634 *
635 * A unique ID is generated even when it is not sent to ensure that the ID can make use of
636 * fetches only available in the HTTP request processing stage.
637 */
638 if (!LIST_ISEMPTY(&sess->fe->format_unique_id)) {
Tim Duesterhusa17e6622020-03-05 20:19:02 +0100639 struct ist unique_id = stream_generate_unique_id(s, &sess->fe->format_unique_id);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200640
Tim Duesterhusa17e6622020-03-05 20:19:02 +0100641 if (!isttest(unique_id)) {
Christopher Fauletb8a53712019-12-16 11:29:38 +0100642 if (!(s->flags & SF_ERR_MASK))
643 s->flags |= SF_ERR_RESOURCE;
644 goto return_int_err;
645 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200646
Tim Duesterhus2825b4b2020-02-28 15:13:34 +0100647 /* send unique ID if a "unique-id-header" is defined */
Tim Duesterhus0643b0e2020-03-05 17:56:35 +0100648 if (isttest(sess->fe->header_unique_id) &&
Tim Duesterhusa17e6622020-03-05 20:19:02 +0100649 unlikely(!http_add_header(htx, sess->fe->header_unique_id, s->unique_id)))
Tim Duesterhus2825b4b2020-02-28 15:13:34 +0100650 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200651 }
652
653 /*
654 * 9: add X-Forwarded-For if either the frontend or the backend
655 * asks for it.
656 */
657 if ((sess->fe->options | s->be->options) & PR_O_FWDFOR) {
Christopher Faulet8da67aa2022-03-29 17:53:09 +0200658 const struct sockaddr_storage *src = cs_src(s->csf);
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200659 struct http_hdr_ctx ctx = { .blk = NULL };
Tim Duesterhusb50ab842022-03-05 00:52:41 +0100660 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 +0200661
Christopher Faulete0768eb2018-10-03 16:38:02 +0200662 if (!((sess->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200663 http_find_header(htx, hdr, &ctx, 0)) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200664 /* The header is set to be added only if none is present
665 * and we found it, so don't do anything.
666 */
667 }
Christopher Faulet8a104ba2021-10-25 07:41:30 +0200668 else if (src && src->ss_family == AF_INET) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200669 /* Add an X-Forwarded-For header unless the source IP is
670 * in the 'except' network range.
671 */
Christopher Faulet8a104ba2021-10-25 07:41:30 +0200672 if (ipcmp2net(src, &sess->fe->except_xff_net) &&
673 ipcmp2net(src, &s->be->except_xff_net)) {
674 unsigned char *pn = (unsigned char *)&((struct sockaddr_in *)src)->sin_addr;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200675
676 /* Note: we rely on the backend to get the header name to be used for
677 * x-forwarded-for, because the header is really meant for the backends.
678 * However, if the backend did not specify any option, we have to rely
679 * on the frontend's header name.
680 */
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200681 chunk_printf(&trash, "%d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
682 if (unlikely(!http_add_header(htx, hdr, ist2(trash.area, trash.data))))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100683 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200684 }
685 }
Christopher Faulet8a104ba2021-10-25 07:41:30 +0200686 else if (src && src->ss_family == AF_INET6) {
Christopher Faulet5d1def62021-02-26 09:19:15 +0100687 /* Add an X-Forwarded-For header unless the source IP is
688 * in the 'except' network range.
Christopher Faulete0768eb2018-10-03 16:38:02 +0200689 */
Christopher Faulet8a104ba2021-10-25 07:41:30 +0200690 if (ipcmp2net(src, &sess->fe->except_xff_net) &&
691 ipcmp2net(src, &s->be->except_xff_net)) {
Christopher Faulet5d1def62021-02-26 09:19:15 +0100692 char pn[INET6_ADDRSTRLEN];
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200693
Christopher Faulet5d1def62021-02-26 09:19:15 +0100694 inet_ntop(AF_INET6,
Christopher Faulet8a104ba2021-10-25 07:41:30 +0200695 (const void *)&((struct sockaddr_in6 *)(src))->sin6_addr,
Christopher Faulet5d1def62021-02-26 09:19:15 +0100696 pn, sizeof(pn));
Christopher Faulete0768eb2018-10-03 16:38:02 +0200697
Christopher Faulet5d1def62021-02-26 09:19:15 +0100698 /* Note: we rely on the backend to get the header name to be used for
699 * x-forwarded-for, because the header is really meant for the backends.
700 * However, if the backend did not specify any option, we have to rely
701 * on the frontend's header name.
702 */
703 chunk_printf(&trash, "%s", pn);
704 if (unlikely(!http_add_header(htx, hdr, ist2(trash.area, trash.data))))
705 goto return_int_err;
706 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200707 }
708 }
709
710 /*
711 * 10: add X-Original-To if either the frontend or the backend
712 * asks for it.
713 */
714 if ((sess->fe->options | s->be->options) & PR_O_ORGTO) {
Christopher Faulet8da67aa2022-03-29 17:53:09 +0200715 const struct sockaddr_storage *dst = cs_dst(s->csf);
Tim Duesterhuse502c3e2022-03-05 00:52:42 +0100716 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 +0200717
Christopher Faulet8a104ba2021-10-25 07:41:30 +0200718 if (dst && dst->ss_family == AF_INET) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200719 /* Add an X-Original-To header unless the destination IP is
720 * in the 'except' network range.
721 */
Christopher Faulet8a104ba2021-10-25 07:41:30 +0200722 if (ipcmp2net(dst, &sess->fe->except_xot_net) &&
723 ipcmp2net(dst, &s->be->except_xot_net)) {
724 unsigned char *pn = (unsigned char *)&((struct sockaddr_in *)dst)->sin_addr;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200725
726 /* Note: we rely on the backend to get the header name to be used for
727 * x-original-to, because the header is really meant for the backends.
728 * However, if the backend did not specify any option, we have to rely
729 * on the frontend's header name.
730 */
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200731 chunk_printf(&trash, "%d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
732 if (unlikely(!http_add_header(htx, hdr, ist2(trash.area, trash.data))))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100733 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200734 }
735 }
Christopher Faulet8a104ba2021-10-25 07:41:30 +0200736 else if (dst && dst->ss_family == AF_INET6) {
Christopher Faulet5d1def62021-02-26 09:19:15 +0100737 /* Add an X-Original-To header unless the source IP is
738 * in the 'except' network range.
739 */
Christopher Faulet8a104ba2021-10-25 07:41:30 +0200740 if (ipcmp2net(dst, &sess->fe->except_xot_net) &&
741 ipcmp2net(dst, &s->be->except_xot_net)) {
Christopher Faulet5d1def62021-02-26 09:19:15 +0100742 char pn[INET6_ADDRSTRLEN];
743
744 inet_ntop(AF_INET6,
Christopher Faulet8a104ba2021-10-25 07:41:30 +0200745 (const void *)&((struct sockaddr_in6 *)dst)->sin6_addr,
Christopher Faulet5d1def62021-02-26 09:19:15 +0100746 pn, sizeof(pn));
747
748 /* Note: we rely on the backend to get the header name to be used for
749 * x-forwarded-for, because the header is really meant for the backends.
750 * However, if the backend did not specify any option, we have to rely
751 * on the frontend's header name.
752 */
753 chunk_printf(&trash, "%s", pn);
754 if (unlikely(!http_add_header(htx, hdr, ist2(trash.area, trash.data))))
755 goto return_int_err;
756 }
757 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200758 }
759
Christopher Fauletc2ac5e42021-03-08 18:20:09 +0100760 /* Filter the request headers if there are filters attached to the
761 * stream.
762 */
763 if (HAS_FILTERS(s))
764 req->analysers |= AN_REQ_FLT_HTTP_HDRS;
765
Christopher Faulete0768eb2018-10-03 16:38:02 +0200766 /* If we have no server assigned yet and we're balancing on url_param
767 * with a POST request, we may be interested in checking the body for
768 * that parameter. This will be done in another analyser.
769 */
770 if (!(s->flags & (SF_ASSIGNED|SF_DIRECT)) &&
Willy Tarreau089eaa02019-01-14 15:17:46 +0100771 s->txn->meth == HTTP_METH_POST &&
772 (s->be->lbprm.algo & BE_LB_ALGO) == BE_LB_ALGO_PH) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200773 channel_dont_connect(req);
774 req->analysers |= AN_REQ_HTTP_BODY;
775 }
776
777 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
778 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau1a18b542018-12-11 16:37:42 +0100779
Christopher Faulete0768eb2018-10-03 16:38:02 +0200780 /* We expect some data from the client. Unless we know for sure
781 * we already have a full request, we have to re-enable quick-ack
782 * in case we previously disabled it, otherwise we might cause
783 * the client to delay further data.
784 */
William Lallemand36119de2021-03-08 15:26:48 +0100785 if ((sess->listener && (sess->listener->options & LI_O_NOQUICKACK)) && !(htx->flags & HTX_FL_EOM))
Willy Tarreau1a18b542018-12-11 16:37:42 +0100786 conn_set_quickack(cli_conn, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200787
788 /*************************************************************
789 * OK, that's finished for the headers. We have done what we *
790 * could. Let's switch to the DATA state. *
791 ************************************************************/
792 req->analyse_exp = TICK_ETERNITY;
793 req->analysers &= ~an_bit;
794
795 s->logs.tv_request = now;
796 /* OK let's go on with the BODY now */
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100797 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200798 return 1;
799
Christopher Fauletb8a53712019-12-16 11:29:38 +0100800 return_int_err:
801 txn->status = 500;
802 if (!(s->flags & SF_ERR_MASK))
803 s->flags |= SF_ERR_INTERNAL;
Willy Tarreau4781b152021-04-06 13:53:36 +0200804 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100805 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau4781b152021-04-06 13:53:36 +0200806 _HA_ATOMIC_INC(&s->be->be_counters.internal_errors);
William Lallemand36119de2021-03-08 15:26:48 +0100807 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200808 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100809
Christopher Fauletb8a53712019-12-16 11:29:38 +0100810 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +0200811
812 if (!(s->flags & SF_ERR_MASK))
813 s->flags |= SF_ERR_PRXCOND;
814 if (!(s->flags & SF_FINST_MASK))
815 s->flags |= SF_FINST_R;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100816
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100817 DBG_TRACE_DEVEL("leaving on error",
818 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200819 return 0;
820}
821
822/* This function is an analyser which processes the HTTP tarpit. It always
823 * returns zero, at the beginning because it prevents any other processing
824 * from occurring, and at the end because it terminates the request.
825 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200826int http_process_tarpit(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200827{
828 struct http_txn *txn = s->txn;
829
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100830 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, &txn->req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200831 /* This connection is being tarpitted. The CLIENT side has
832 * already set the connect expiration date to the right
833 * timeout. We just have to check that the client is still
834 * there and that the timeout has not expired.
835 */
836 channel_dont_connect(req);
837 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100838 !tick_is_expired(req->analyse_exp, now_ms)) {
Christopher Fauletb0c87f12021-10-29 14:37:07 +0200839 /* Be sure to drain all data from the request channel */
840 channel_htx_erase(req, htxbuf(&req->buf));
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100841 DBG_TRACE_DEVEL("waiting for tarpit timeout expiry",
842 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200843 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100844 }
845
Christopher Faulete0768eb2018-10-03 16:38:02 +0200846
847 /* We will set the queue timer to the time spent, just for
848 * logging purposes. We fake a 500 server error, so that the
849 * attacker will not suspect his connection has been tarpitted.
850 * It will not cause trouble to the logs because we can exclude
851 * the tarpitted connections by filtering on the 'PT' status flags.
852 */
853 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
854
Christopher Faulet8dfeccf2020-05-15 14:16:29 +0200855 http_reply_and_close(s, txn->status, (!(req->flags & CF_READ_ERROR) ? http_error_message(s) : NULL));
Christopher Faulet5cb513a2020-05-13 17:56:56 +0200856
Christopher Faulete0768eb2018-10-03 16:38:02 +0200857 if (!(s->flags & SF_ERR_MASK))
858 s->flags |= SF_ERR_PRXCOND;
859 if (!(s->flags & SF_FINST_MASK))
860 s->flags |= SF_FINST_T;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100861
862 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200863 return 0;
864}
865
866/* This function is an analyser which waits for the HTTP request body. It waits
867 * for either the buffer to be full, or the full advertised contents to have
868 * reached the buffer. It must only be called after the standard HTTP request
869 * processing has occurred, because it expects the request to be parsed and will
870 * look for the Expect header. It may send a 100-Continue interim response. It
871 * takes in input any state starting from HTTP_MSG_BODY and leaves with one of
872 * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it
873 * needs to read more data, or 1 once it has completed its analysis.
874 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200875int http_wait_for_request_body(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200876{
877 struct session *sess = s->sess;
878 struct http_txn *txn = s->txn;
879 struct http_msg *msg = &s->txn->req;
880
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100881 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Fauletf76ebe82018-10-24 11:16:22 +0200882
Christopher Fauletf76ebe82018-10-24 11:16:22 +0200883
Christopher Faulet021a8e42021-03-29 10:46:38 +0200884 switch (http_wait_for_msg_body(s, req, s->be->timeout.httpreq, 0)) {
885 case HTTP_RULE_RES_CONT:
886 goto http_end;
887 case HTTP_RULE_RES_YIELD:
888 goto missing_data_or_waiting;
889 case HTTP_RULE_RES_BADREQ:
Willy Tarreau4236f032019-03-05 10:43:32 +0100890 goto return_bad_req;
Christopher Faulet021a8e42021-03-29 10:46:38 +0200891 case HTTP_RULE_RES_ERROR:
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200892 goto return_int_err;
Christopher Faulet021a8e42021-03-29 10:46:38 +0200893 case HTTP_RULE_RES_ABRT:
Christopher Fauletb8a53712019-12-16 11:29:38 +0100894 goto return_prx_cond;
Christopher Faulet021a8e42021-03-29 10:46:38 +0200895 default:
896 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200897 }
898
899 http_end:
900 /* The situation will not evolve, so let's give up on the analysis. */
901 s->logs.tv_request = now; /* update the request timer to reflect full request */
902 req->analysers &= ~an_bit;
903 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100904 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200905 return 1;
906
Christopher Faulet021a8e42021-03-29 10:46:38 +0200907 missing_data_or_waiting:
908 channel_dont_connect(req);
909 DBG_TRACE_DEVEL("waiting for more data",
910 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
911 return 0;
912
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200913 return_int_err:
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200914 txn->status = 500;
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200915 if (!(s->flags & SF_ERR_MASK))
916 s->flags |= SF_ERR_INTERNAL;
Willy Tarreau4781b152021-04-06 13:53:36 +0200917 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100918 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau4781b152021-04-06 13:53:36 +0200919 _HA_ATOMIC_INC(&s->be->be_counters.internal_errors);
William Lallemand36119de2021-03-08 15:26:48 +0100920 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200921 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Faulet021a8e42021-03-29 10:46:38 +0200922 goto return_prx_err;
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200923
Christopher Faulete0768eb2018-10-03 16:38:02 +0200924 return_bad_req: /* let's centralize all bad requests */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200925 txn->status = 400;
Willy Tarreau4781b152021-04-06 13:53:36 +0200926 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req);
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->failed_req);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100929 /* fall through */
930
Christopher Faulet021a8e42021-03-29 10:46:38 +0200931 return_prx_err:
Christopher Fauletb8a53712019-12-16 11:29:38 +0100932 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet021a8e42021-03-29 10:46:38 +0200933 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200934
Christopher Faulet021a8e42021-03-29 10:46:38 +0200935 return_prx_cond:
Christopher Faulete0768eb2018-10-03 16:38:02 +0200936 if (!(s->flags & SF_ERR_MASK))
937 s->flags |= SF_ERR_PRXCOND;
938 if (!(s->flags & SF_FINST_MASK))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100939 s->flags |= (msg->msg_state < HTTP_MSG_DATA ? SF_FINST_R : SF_FINST_D);
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200940
Christopher Faulete0768eb2018-10-03 16:38:02 +0200941 req->analysers &= AN_REQ_FLT_END;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100942 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100943 DBG_TRACE_DEVEL("leaving on error",
944 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200945 return 0;
946}
947
948/* This function is an analyser which forwards request body (including chunk
949 * sizes if any). It is called as soon as we must forward, even if we forward
950 * zero byte. The only situation where it must not be called is when we're in
951 * tunnel mode and we want to forward till the close. It's used both to forward
952 * remaining data and to resync after end of body. It expects the msg_state to
953 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
954 * read more data, or 1 once we can go on with next request or end the stream.
955 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
956 * bytes of pending data + the headers if not already done.
957 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200958int http_request_forward_body(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200959{
960 struct session *sess = s->sess;
961 struct http_txn *txn = s->txn;
Christopher Faulet9768c262018-10-22 09:34:31 +0200962 struct http_msg *msg = &txn->req;
963 struct htx *htx;
Christopher Faulet93e02d82019-03-08 14:18:50 +0100964 short status = 0;
Christopher Fauletaed82cf2018-11-30 22:22:32 +0100965 int ret;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200966
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100967 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200968
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100969 htx = htxbuf(&req->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200970
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200971 if (htx->flags & HTX_FL_PARSING_ERROR)
972 goto return_bad_req;
973 if (htx->flags & HTX_FL_PROCESSING_ERROR)
974 goto return_int_err;
975
Christopher Faulete0768eb2018-10-03 16:38:02 +0200976 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
977 ((req->flags & CF_SHUTW) && (req->to_forward || co_data(req)))) {
978 /* Output closed while we were sending data. We must abort and
979 * wake the other side up.
Christopher Fauletf506d962021-04-27 10:56:28 +0200980 *
981 * If we have finished to send the request and the response is
982 * still in progress, don't catch write error on the request
983 * side if it is in fact a read error on the server side.
Christopher Faulete0768eb2018-10-03 16:38:02 +0200984 */
Christopher Fauletf506d962021-04-27 10:56:28 +0200985 if (msg->msg_state == HTTP_MSG_DONE && (s->res.flags & CF_READ_ERROR) && s->res.analysers)
986 return 0;
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200987
Olivier Houchard29cac3c2019-07-12 15:48:58 +0200988 /* Don't abort yet if we had L7 retries activated and it
989 * was a write error, we may recover.
990 */
991 if (!(req->flags & (CF_READ_ERROR | CF_READ_TIMEOUT)) &&
Christopher Faulete05bf9e2022-03-29 15:23:40 +0200992 (txn->flags & TX_L7_RETRY)) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100993 DBG_TRACE_DEVEL("leaving on L7 retry",
994 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Olivier Houchard29cac3c2019-07-12 15:48:58 +0200995 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100996 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200997 msg->msg_state = HTTP_MSG_ERROR;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200998 http_end_request(s);
999 http_end_response(s);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001000 DBG_TRACE_DEVEL("leaving on error",
1001 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001002 return 1;
1003 }
1004
1005 /* Note that we don't have to send 100-continue back because we don't
1006 * need the data to complete our job, and it's up to the server to
1007 * decide whether to return 100, 417 or anything else in return of
1008 * an "Expect: 100-continue" header.
1009 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001010 if (msg->msg_state == HTTP_MSG_BODY)
1011 msg->msg_state = HTTP_MSG_DATA;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001012
Christopher Faulete0768eb2018-10-03 16:38:02 +02001013 /* in most states, we should abort in case of early close */
1014 channel_auto_close(req);
1015
1016 if (req->to_forward) {
Christopher Faulet66af0b22019-03-22 14:54:52 +01001017 if (req->to_forward == CHN_INFINITE_FORWARD) {
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001018 if (req->flags & CF_EOI)
1019 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet66af0b22019-03-22 14:54:52 +01001020 }
1021 else {
1022 /* We can't process the buffer's contents yet */
1023 req->flags |= CF_WAKE_WRITE;
1024 goto missing_data_or_waiting;
1025 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001026 }
1027
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001028 if (msg->msg_state >= HTTP_MSG_ENDING)
1029 goto ending;
1030
1031 if (txn->meth == HTTP_METH_CONNECT) {
1032 msg->msg_state = HTTP_MSG_ENDING;
1033 goto ending;
1034 }
1035
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001036 /* Forward input data. We get it by removing all outgoing data not
1037 * forwarded yet from HTX data size. If there are some data filters, we
1038 * let them decide the amount of data to forward.
Christopher Faulet9768c262018-10-22 09:34:31 +02001039 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001040 if (HAS_REQ_DATA_FILTERS(s)) {
1041 ret = flt_http_payload(s, msg, htx->data);
1042 if (ret < 0)
1043 goto return_bad_req;
Christopher Faulet421e7692019-06-13 11:16:45 +02001044 c_adv(req, ret);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001045 }
1046 else {
Christopher Faulet421e7692019-06-13 11:16:45 +02001047 c_adv(req, htx->data - co_data(req));
Christopher Faulet66af0b22019-03-22 14:54:52 +01001048 if (msg->flags & HTTP_MSGF_XFER_LEN)
1049 channel_htx_forward_forever(req, htx);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001050 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001051
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001052 if (htx->data != co_data(req))
1053 goto missing_data_or_waiting;
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001054
Christopher Faulet9768c262018-10-22 09:34:31 +02001055 /* Check if the end-of-message is reached and if so, switch the message
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001056 * in HTTP_MSG_ENDING state. Then if all data was marked to be
1057 * forwarded, set the state to HTTP_MSG_DONE.
Christopher Faulet9768c262018-10-22 09:34:31 +02001058 */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001059 if (!(htx->flags & HTX_FL_EOM))
Christopher Faulet9768c262018-10-22 09:34:31 +02001060 goto missing_data_or_waiting;
1061
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001062 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet9768c262018-10-22 09:34:31 +02001063
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001064 ending:
Christopher Faulet2151cdd2020-07-22 16:34:59 +02001065 req->flags &= ~CF_EXPECT_MORE; /* no more data are expected */
1066
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001067 /* other states, ENDING...TUNNEL */
1068 if (msg->msg_state >= HTTP_MSG_DONE)
1069 goto done;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001070
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001071 if (HAS_REQ_DATA_FILTERS(s)) {
1072 ret = flt_http_end(s, msg);
1073 if (ret <= 0) {
1074 if (!ret)
1075 goto missing_data_or_waiting;
1076 goto return_bad_req;
1077 }
1078 }
1079
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001080 if (txn->meth == HTTP_METH_CONNECT)
1081 msg->msg_state = HTTP_MSG_TUNNEL;
1082 else {
1083 msg->msg_state = HTTP_MSG_DONE;
1084 req->to_forward = 0;
1085 }
1086
1087 done:
1088 /* we don't want to forward closes on DONE except in tunnel mode. */
1089 if (!(txn->flags & TX_CON_WANT_TUN))
1090 channel_dont_close(req);
1091
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001092 http_end_request(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001093 if (!(req->analysers & an_bit)) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001094 http_end_response(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001095 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
1096 if (req->flags & CF_SHUTW) {
1097 /* request errors are most likely due to the
1098 * server aborting the transfer. */
Christopher Faulet93e02d82019-03-08 14:18:50 +01001099 goto return_srv_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001100 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001101 goto return_bad_req;
1102 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001103 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001104 return 1;
1105 }
1106
1107 /* If "option abortonclose" is set on the backend, we want to monitor
1108 * the client's connection and forward any shutdown notification to the
1109 * server, which will decide whether to close or to go on processing the
1110 * request. We only do that in tunnel mode, and not in other modes since
1111 * it can be abused to exhaust source ports. */
Christopher Faulet769d0e92019-03-22 14:23:18 +01001112 if (s->be->options & PR_O_ABRT_CLOSE) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001113 channel_auto_read(req);
Christopher Fauletc41547b2019-07-16 14:32:23 +02001114 if ((req->flags & (CF_SHUTR|CF_READ_NULL)) && !(txn->flags & TX_CON_WANT_TUN))
Christopher Faulet8abe7122022-03-30 15:10:18 +02001115 s->csb->flags |= CS_FL_NOLINGER;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001116 channel_auto_close(req);
1117 }
1118 else if (s->txn->meth == HTTP_METH_POST) {
1119 /* POST requests may require to read extra CRLF sent by broken
1120 * browsers and which could cause an RST to be sent upon close
1121 * on some systems (eg: Linux). */
1122 channel_auto_read(req);
1123 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001124 DBG_TRACE_DEVEL("waiting for the end of the HTTP txn",
1125 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001126 return 0;
1127
1128 missing_data_or_waiting:
1129 /* stop waiting for data if the input is closed before the end */
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001130 if (msg->msg_state < HTTP_MSG_ENDING && req->flags & CF_SHUTR)
Christopher Faulet93e02d82019-03-08 14:18:50 +01001131 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001132
1133 waiting:
1134 /* waiting for the last bits to leave the buffer */
1135 if (req->flags & CF_SHUTW)
Christopher Faulet93e02d82019-03-08 14:18:50 +01001136 goto return_srv_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001137
1138 /* When TE: chunked is used, we need to get there again to parse remaining
1139 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
1140 * And when content-length is used, we never want to let the possible
1141 * shutdown be forwarded to the other side, as the state machine will
1142 * take care of it once the client responds. It's also important to
1143 * prevent TIME_WAITs from accumulating on the backend side, and for
1144 * HTTP/2 where the last frame comes with a shutdown.
1145 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001146 if (msg->flags & HTTP_MSGF_XFER_LEN)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001147 channel_dont_close(req);
1148
1149 /* We know that more data are expected, but we couldn't send more that
1150 * what we did. So we always set the CF_EXPECT_MORE flag so that the
1151 * system knows it must not set a PUSH on this first part. Interactive
1152 * modes are already handled by the stream sock layer. We must not do
1153 * this in content-length mode because it could present the MSG_MORE
1154 * flag with the last block of forwarded data, which would cause an
1155 * additional delay to be observed by the receiver.
1156 */
Christopher Faulet2151cdd2020-07-22 16:34:59 +02001157 if (HAS_REQ_DATA_FILTERS(s))
Christopher Faulete0768eb2018-10-03 16:38:02 +02001158 req->flags |= CF_EXPECT_MORE;
1159
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001160 DBG_TRACE_DEVEL("waiting for more data to forward",
1161 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001162 return 0;
1163
Christopher Faulet93e02d82019-03-08 14:18:50 +01001164 return_cli_abort:
Willy Tarreau4781b152021-04-06 13:53:36 +02001165 _HA_ATOMIC_INC(&sess->fe->fe_counters.cli_aborts);
1166 _HA_ATOMIC_INC(&s->be->be_counters.cli_aborts);
William Lallemand36119de2021-03-08 15:26:48 +01001167 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001168 _HA_ATOMIC_INC(&sess->listener->counters->cli_aborts);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001169 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001170 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.cli_aborts);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001171 if (!(s->flags & SF_ERR_MASK))
1172 s->flags |= SF_ERR_CLICL;
1173 status = 400;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001174 goto return_prx_cond;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001175
1176 return_srv_abort:
Willy Tarreau4781b152021-04-06 13:53:36 +02001177 _HA_ATOMIC_INC(&sess->fe->fe_counters.srv_aborts);
1178 _HA_ATOMIC_INC(&s->be->be_counters.srv_aborts);
William Lallemand36119de2021-03-08 15:26:48 +01001179 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001180 _HA_ATOMIC_INC(&sess->listener->counters->srv_aborts);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001181 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001182 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.srv_aborts);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001183 if (!(s->flags & SF_ERR_MASK))
1184 s->flags |= SF_ERR_SRVCL;
1185 status = 502;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001186 goto return_prx_cond;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001187
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001188 return_int_err:
1189 if (!(s->flags & SF_ERR_MASK))
1190 s->flags |= SF_ERR_INTERNAL;
Willy Tarreau4781b152021-04-06 13:53:36 +02001191 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
1192 _HA_ATOMIC_INC(&s->be->be_counters.internal_errors);
William Lallemand36119de2021-03-08 15:26:48 +01001193 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001194 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001195 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001196 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.internal_errors);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001197 status = 500;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001198 goto return_prx_cond;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001199
Christopher Faulet93e02d82019-03-08 14:18:50 +01001200 return_bad_req:
Willy Tarreau4781b152021-04-06 13:53:36 +02001201 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req);
William Lallemand36119de2021-03-08 15:26:48 +01001202 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001203 _HA_ATOMIC_INC(&sess->listener->counters->failed_req);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001204 status = 400;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001205 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001206
Christopher Fauletb8a53712019-12-16 11:29:38 +01001207 return_prx_cond:
Christopher Faulet9768c262018-10-22 09:34:31 +02001208 if (txn->status > 0) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001209 /* Note: we don't send any error if some data were already sent */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001210 http_reply_and_close(s, txn->status, NULL);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001211 } else {
Christopher Faulet93e02d82019-03-08 14:18:50 +01001212 txn->status = status;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001213 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001214 }
Christopher Fauletb8a53712019-12-16 11:29:38 +01001215 if (!(s->flags & SF_ERR_MASK))
1216 s->flags |= SF_ERR_PRXCOND;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001217 if (!(s->flags & SF_FINST_MASK))
1218 s->flags |= ((txn->rsp.msg_state < HTTP_MSG_ERROR) ? SF_FINST_H : SF_FINST_D);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001219 DBG_TRACE_DEVEL("leaving on error ",
1220 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001221 return 0;
1222}
1223
Christopher Faulet77397992022-04-04 11:07:08 +02001224/* Reset the stream and the backend conn_stream to a situation suitable for attemption connection */
Olivier Houcharda254a372019-04-05 15:30:12 +02001225/* Returns 0 if we can attempt to retry, -1 otherwise */
Christopher Faulet77397992022-04-04 11:07:08 +02001226static __inline int do_l7_retry(struct stream *s, struct conn_stream *cs)
Olivier Houcharda254a372019-04-05 15:30:12 +02001227{
Christopher Faulet9f5382e2021-05-21 13:46:14 +02001228 struct channel *req, *res;
1229 int co_data;
Olivier Houcharda254a372019-04-05 15:30:12 +02001230
Christopher Faulet731c8e62022-03-29 16:08:44 +02001231 s->conn_retries++;
1232 if (s->conn_retries >= s->be->conn_retries)
Christopher Faulet552601d2021-05-26 10:31:06 +02001233 return -1;
Christopher Faulet5b82cc52020-10-12 15:18:50 +02001234
Christopher Faulete763c8c2021-05-05 18:23:59 +02001235 if (objt_server(s->target)) {
1236 if (s->flags & SF_CURR_SESS) {
1237 s->flags &= ~SF_CURR_SESS;
1238 _HA_ATOMIC_DEC(&__objt_server(s->target)->cur_sess);
1239 }
Willy Tarreau4781b152021-04-06 13:53:36 +02001240 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.retries);
Christopher Faulete763c8c2021-05-05 18:23:59 +02001241 }
Willy Tarreau4781b152021-04-06 13:53:36 +02001242 _HA_ATOMIC_INC(&s->be->be_counters.retries);
Willy Tarreau223995e2019-05-04 10:38:31 +02001243
Christopher Faulet9f5382e2021-05-21 13:46:14 +02001244 req = &s->req;
1245 res = &s->res;
Olivier Houcharda254a372019-04-05 15:30:12 +02001246 /* Remove any write error from the request, and read error from the response */
1247 req->flags &= ~(CF_WRITE_ERROR | CF_WRITE_TIMEOUT | CF_SHUTW | CF_SHUTW_NOW);
1248 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 +01001249 res->analysers &= AN_RES_FLT_END;
Christopher Faulet50264b42022-03-30 19:39:30 +02001250 s->conn_err_type = STRM_ET_NONE;
Christopher Fauletae024ce2022-03-29 19:02:31 +02001251 s->flags &= ~(SF_CONN_EXP | SF_ERR_MASK | SF_FINST_MASK);
1252 s->conn_exp = TICK_ETERNITY;
Olivier Houchard4bd58672019-07-12 16:16:59 +02001253 stream_choose_redispatch(s);
Olivier Houcharda254a372019-04-05 15:30:12 +02001254 res->rex = TICK_ETERNITY;
1255 res->to_forward = 0;
1256 res->analyse_exp = TICK_ETERNITY;
1257 res->total = 0;
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +01001258
1259 if (cs_reset_endp(s->csb) < 0) {
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +01001260 if (!(s->flags & SF_ERR_MASK))
1261 s->flags |= SF_ERR_INTERNAL;
1262 return -1;
1263 }
Christopher Fauleta6c4a482022-04-28 18:25:24 +02001264 cs->endp->flags &= ~CS_EP_RXBLK_SHUT;
Olivier Houcharda254a372019-04-05 15:30:12 +02001265
Christopher Faulet9f5382e2021-05-21 13:46:14 +02001266 b_free(&req->buf);
1267 /* Swap the L7 buffer with the channel buffer */
1268 /* We know we stored the co_data as b_data, so get it there */
Christopher Faulete05bf9e2022-03-29 15:23:40 +02001269 co_data = b_data(&s->txn->l7_buffer);
1270 b_set_data(&s->txn->l7_buffer, b_size(&s->txn->l7_buffer));
1271 b_xfer(&req->buf, &s->txn->l7_buffer, b_data(&s->txn->l7_buffer));
Christopher Faulet9f5382e2021-05-21 13:46:14 +02001272 co_set_data(req, co_data);
Christopher Faulet5b82cc52020-10-12 15:18:50 +02001273
Ilya Shipitsinacf84592021-02-06 22:29:08 +05001274 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 +02001275
Olivier Houcharda254a372019-04-05 15:30:12 +02001276 b_reset(&res->buf);
1277 co_set_data(res, 0);
1278 return 0;
1279}
1280
Christopher Faulete0768eb2018-10-03 16:38:02 +02001281/* This stream analyser waits for a complete HTTP response. It returns 1 if the
1282 * processing can continue on next analysers, or zero if it either needs more
1283 * data or wants to immediately abort the response (eg: timeout, error, ...). It
1284 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->res.analysers
1285 * when it has nothing left to do, and may remove any analyser when it wants to
1286 * abort.
1287 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001288int http_wait_for_response(struct stream *s, struct channel *rep, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001289{
Christopher Faulet9768c262018-10-22 09:34:31 +02001290 /*
1291 * We will analyze a complete HTTP response to check the its syntax.
1292 *
1293 * Once the start line and all headers are received, we may perform a
1294 * capture of the error (if any), and we will set a few fields. We also
1295 * logging and finally headers capture.
1296 */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001297 struct session *sess = s->sess;
1298 struct http_txn *txn = s->txn;
1299 struct http_msg *msg = &txn->rsp;
Christopher Faulet9768c262018-10-22 09:34:31 +02001300 struct htx *htx;
Christopher Faulet61608322018-11-23 16:23:45 +01001301 struct connection *srv_conn;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001302 struct htx_sl *sl;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001303 int n;
1304
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001305 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001306
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001307 htx = htxbuf(&rep->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001308
Willy Tarreau4236f032019-03-05 10:43:32 +01001309 /* Parsing errors are caught here */
1310 if (htx->flags & HTX_FL_PARSING_ERROR)
1311 goto return_bad_res;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001312 if (htx->flags & HTX_FL_PROCESSING_ERROR)
1313 goto return_int_err;
Willy Tarreau4236f032019-03-05 10:43:32 +01001314
Christopher Faulete0768eb2018-10-03 16:38:02 +02001315 /*
1316 * Now we quickly check if we have found a full valid response.
1317 * If not so, we check the FD and buffer states before leaving.
1318 * A full response is indicated by the fact that we have seen
1319 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
1320 * responses are checked first.
1321 *
1322 * Depending on whether the client is still there or not, we
1323 * may send an error response back or not. Note that normally
1324 * we should only check for HTTP status there, and check I/O
1325 * errors somewhere else.
1326 */
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001327 next_one:
Christopher Faulet29f17582019-05-23 11:03:26 +02001328 if (unlikely(htx_is_empty(htx) || htx->first == -1)) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001329 /* 1: have we encountered a read error ? */
1330 if (rep->flags & CF_READ_ERROR) {
Christopher Faulet95a61e82021-12-22 14:22:03 +01001331 struct connection *conn = cs_conn(s->csb);
Olivier Houchard865d8392019-05-03 22:46:27 +02001332
Christopher Fauletd9769232021-05-26 12:15:37 +02001333 /* Perform a L7 retry because server refuses the early data. */
Christopher Faulete05bf9e2022-03-29 15:23:40 +02001334 if ((txn->flags & TX_L7_RETRY) &&
Christopher Fauletd9769232021-05-26 12:15:37 +02001335 (s->be->retry_type & PR_RE_EARLY_ERROR) &&
1336 conn && conn->err_code == CO_ER_SSL_EARLY_FAILED &&
Christopher Faulet77397992022-04-04 11:07:08 +02001337 do_l7_retry(s, s->csb) == 0) {
Christopher Fauletd9769232021-05-26 12:15:37 +02001338 DBG_TRACE_DEVEL("leaving on L7 retry",
1339 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
1340 return 0;
1341 }
1342
Olivier Houchard6db16992019-05-17 15:40:49 +02001343 if (txn->flags & TX_NOT_FIRST)
1344 goto abort_keep_alive;
1345
Willy Tarreau4781b152021-04-06 13:53:36 +02001346 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001347 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001348 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001349 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001350 }
1351
Christopher Fauletd9769232021-05-26 12:15:37 +02001352 /* if the server refused the early data, just send a 425 */
1353 if (conn && conn->err_code == CO_ER_SSL_EARLY_FAILED)
Olivier Houchard865d8392019-05-03 22:46:27 +02001354 txn->status = 425;
Christopher Fauletd9769232021-05-26 12:15:37 +02001355 else {
1356 txn->status = 502;
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001357 stream_inc_http_fail_ctr(s);
Christopher Fauletd9769232021-05-26 12:15:37 +02001358 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001359
Christopher Faulet8abe7122022-03-30 15:10:18 +02001360 s->csb->flags |= CS_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001361 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001362
1363 if (!(s->flags & SF_ERR_MASK))
1364 s->flags |= SF_ERR_SRVCL;
1365 if (!(s->flags & SF_FINST_MASK))
1366 s->flags |= SF_FINST_H;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001367 DBG_TRACE_DEVEL("leaving on error",
1368 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001369 return 0;
1370 }
1371
Christopher Faulet9768c262018-10-22 09:34:31 +02001372 /* 2: read timeout : return a 504 to the client. */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001373 else if (rep->flags & CF_READ_TIMEOUT) {
Christopher Faulete05bf9e2022-03-29 15:23:40 +02001374 if ((txn->flags & TX_L7_RETRY) &&
Olivier Houcharda254a372019-04-05 15:30:12 +02001375 (s->be->retry_type & PR_RE_TIMEOUT)) {
Christopher Faulet77397992022-04-04 11:07:08 +02001376 if (co_data(rep) || do_l7_retry(s, s->csb) == 0) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001377 DBG_TRACE_DEVEL("leaving on L7 retry",
1378 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharda254a372019-04-05 15:30:12 +02001379 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001380 }
Olivier Houcharda254a372019-04-05 15:30:12 +02001381 }
Willy Tarreau4781b152021-04-06 13:53:36 +02001382 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001383 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001384 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001385 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001386 }
1387
Christopher Faulete0768eb2018-10-03 16:38:02 +02001388 txn->status = 504;
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001389 stream_inc_http_fail_ctr(s);
Christopher Faulet8abe7122022-03-30 15:10:18 +02001390 s->csb->flags |= CS_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001391 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001392
1393 if (!(s->flags & SF_ERR_MASK))
1394 s->flags |= SF_ERR_SRVTO;
1395 if (!(s->flags & SF_FINST_MASK))
1396 s->flags |= SF_FINST_H;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001397 DBG_TRACE_DEVEL("leaving on error",
1398 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001399 return 0;
1400 }
1401
Christopher Faulet9768c262018-10-22 09:34:31 +02001402 /* 3: client abort with an abortonclose */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001403 else if ((rep->flags & CF_SHUTR) && ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001404 _HA_ATOMIC_INC(&sess->fe->fe_counters.cli_aborts);
1405 _HA_ATOMIC_INC(&s->be->be_counters.cli_aborts);
William Lallemand36119de2021-03-08 15:26:48 +01001406 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001407 _HA_ATOMIC_INC(&sess->listener->counters->cli_aborts);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001408 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001409 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.cli_aborts);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001410
Christopher Faulete0768eb2018-10-03 16:38:02 +02001411 txn->status = 400;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001412 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001413
1414 if (!(s->flags & SF_ERR_MASK))
1415 s->flags |= SF_ERR_CLICL;
1416 if (!(s->flags & SF_FINST_MASK))
1417 s->flags |= SF_FINST_H;
1418
1419 /* process_stream() will take care of the error */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001420 DBG_TRACE_DEVEL("leaving on error",
1421 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001422 return 0;
1423 }
1424
Christopher Faulet9768c262018-10-22 09:34:31 +02001425 /* 4: close from server, capture the response if the server has started to respond */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001426 else if (rep->flags & CF_SHUTR) {
Christopher Faulete05bf9e2022-03-29 15:23:40 +02001427 if ((txn->flags & TX_L7_RETRY) &&
Olivier Houcharda254a372019-04-05 15:30:12 +02001428 (s->be->retry_type & PR_RE_DISCONNECTED)) {
Christopher Faulet77397992022-04-04 11:07:08 +02001429 if (co_data(rep) || do_l7_retry(s, s->csb) == 0) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001430 DBG_TRACE_DEVEL("leaving on L7 retry",
1431 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharda254a372019-04-05 15:30:12 +02001432 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001433 }
Olivier Houcharda254a372019-04-05 15:30:12 +02001434 }
1435
Olivier Houchard6db16992019-05-17 15:40:49 +02001436 if (txn->flags & TX_NOT_FIRST)
1437 goto abort_keep_alive;
1438
Willy Tarreau4781b152021-04-06 13:53:36 +02001439 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001440 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001441 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001442 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001443 }
1444
Christopher Faulete0768eb2018-10-03 16:38:02 +02001445 txn->status = 502;
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001446 stream_inc_http_fail_ctr(s);
Christopher Faulet8abe7122022-03-30 15:10:18 +02001447 s->csb->flags |= CS_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001448 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001449
1450 if (!(s->flags & SF_ERR_MASK))
1451 s->flags |= SF_ERR_SRVCL;
1452 if (!(s->flags & SF_FINST_MASK))
1453 s->flags |= SF_FINST_H;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001454 DBG_TRACE_DEVEL("leaving on error",
1455 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001456 return 0;
1457 }
1458
Christopher Faulet9768c262018-10-22 09:34:31 +02001459 /* 5: write error to client (we don't send any message then) */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001460 else if (rep->flags & CF_WRITE_ERROR) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001461 if (txn->flags & TX_NOT_FIRST)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001462 goto abort_keep_alive;
1463
Willy Tarreau4781b152021-04-06 13:53:36 +02001464 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001465 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001466 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001467 rep->analysers &= AN_RES_FLT_END;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001468
1469 if (!(s->flags & SF_ERR_MASK))
1470 s->flags |= SF_ERR_CLICL;
1471 if (!(s->flags & SF_FINST_MASK))
1472 s->flags |= SF_FINST_H;
1473
1474 /* process_stream() will take care of the error */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001475 DBG_TRACE_DEVEL("leaving on error",
1476 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001477 return 0;
1478 }
1479
1480 channel_dont_close(rep);
1481 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001482 DBG_TRACE_DEVEL("waiting for more data",
1483 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001484 return 0;
1485 }
1486
1487 /* More interesting part now : we know that we have a complete
1488 * response which at least looks like HTTP. We have an indicator
1489 * of each header's length, so we can parse them quickly.
1490 */
Christopher Faulet29f17582019-05-23 11:03:26 +02001491 BUG_ON(htx_get_first_type(htx) != HTX_BLK_RES_SL);
Christopher Faulet297fbb42019-05-13 14:41:27 +02001492 sl = http_get_stline(htx);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001493
Christopher Faulet1f08bff2021-05-26 13:14:39 +02001494 /* Perform a L7 retry because of the status code */
Christopher Faulete05bf9e2022-03-29 15:23:40 +02001495 if ((txn->flags & TX_L7_RETRY) &&
Christopher Faulet1f08bff2021-05-26 13:14:39 +02001496 l7_status_match(s->be, sl->info.res.status) &&
Christopher Faulet77397992022-04-04 11:07:08 +02001497 do_l7_retry(s, s->csb) == 0) {
Christopher Faulet1f08bff2021-05-26 13:14:39 +02001498 DBG_TRACE_DEVEL("leaving on L7 retry", STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
1499 return 0;
1500 }
1501
1502 /* Now, L7 buffer is useless, it can be released */
Christopher Faulete05bf9e2022-03-29 15:23:40 +02001503 b_free(&txn->l7_buffer);
Christopher Faulet1f08bff2021-05-26 13:14:39 +02001504
1505 msg->msg_state = HTTP_MSG_BODY;
1506
1507
Christopher Faulet9768c262018-10-22 09:34:31 +02001508 /* 0: we might have to print this header in debug mode */
1509 if (unlikely((global.mode & MODE_DEBUG) &&
1510 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
1511 int32_t pos;
1512
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001513 http_debug_stline("srvrep", s, sl);
Christopher Faulet9768c262018-10-22 09:34:31 +02001514
Christopher Fauleta3f15502019-05-13 15:27:23 +02001515 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001516 struct htx_blk *blk = htx_get_blk(htx, pos);
1517 enum htx_blk_type type = htx_get_blk_type(blk);
1518
1519 if (type == HTX_BLK_EOH)
1520 break;
1521 if (type != HTX_BLK_HDR)
1522 continue;
1523
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001524 http_debug_hdr("srvhdr", s,
1525 htx_get_blk_name(htx, blk),
1526 htx_get_blk_value(htx, blk));
Christopher Faulet9768c262018-10-22 09:34:31 +02001527 }
1528 }
1529
Christopher Faulet03599112018-11-27 11:21:21 +01001530 /* 1: get the status code and the version. Also set HTTP flags */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001531 txn->status = sl->info.res.status;
Christopher Faulet03599112018-11-27 11:21:21 +01001532 if (sl->flags & HTX_SL_F_VER_11)
Christopher Faulet9768c262018-10-22 09:34:31 +02001533 msg->flags |= HTTP_MSGF_VER_11;
Christopher Faulet03599112018-11-27 11:21:21 +01001534 if (sl->flags & HTX_SL_F_XFER_LEN) {
1535 msg->flags |= HTTP_MSGF_XFER_LEN;
Christopher Faulet2a408542020-11-20 14:22:37 +01001536 if (sl->flags & HTX_SL_F_CLEN)
1537 msg->flags |= HTTP_MSGF_CNT_LEN;
1538 else if (sl->flags & HTX_SL_F_CHNK)
1539 msg->flags |= HTTP_MSGF_TE_CHNK;
Christopher Faulet03599112018-11-27 11:21:21 +01001540 }
Christopher Faulet2a408542020-11-20 14:22:37 +01001541 if (sl->flags & HTX_SL_F_BODYLESS)
1542 msg->flags |= HTTP_MSGF_BODYLESS;
Christopher Faulet576c3582021-01-08 15:53:01 +01001543 if (sl->flags & HTX_SL_F_CONN_UPG)
1544 msg->flags |= HTTP_MSGF_CONN_UPG;
Christopher Faulet9768c262018-10-22 09:34:31 +02001545
1546 n = txn->status / 100;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001547 if (n < 1 || n > 5)
1548 n = 0;
Christopher Faulet9768c262018-10-22 09:34:31 +02001549
Christopher Faulete0768eb2018-10-03 16:38:02 +02001550 /* when the client triggers a 4xx from the server, it's most often due
1551 * to a missing object or permission. These events should be tracked
1552 * because if they happen often, it may indicate a brute force or a
1553 * vulnerability scan.
1554 */
1555 if (n == 4)
1556 stream_inc_http_err_ctr(s);
1557
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001558 if (n == 5 && txn->status != 501 && txn->status != 505)
1559 stream_inc_http_fail_ctr(s);
1560
Marcin Deranek3c27dda2020-05-15 18:32:51 +02001561 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001562 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.p.http.rsp[n]);
1563 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.p.http.cum_req);
Marcin Deranek3c27dda2020-05-15 18:32:51 +02001564 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001565
Christopher Faulete0768eb2018-10-03 16:38:02 +02001566 /* Adjust server's health based on status code. Note: status codes 501
1567 * and 505 are triggered on demand by client request, so we must not
1568 * count them as server failures.
1569 */
1570 if (objt_server(s->target)) {
1571 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001572 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_OK);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001573 else
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001574 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_STS);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001575 }
1576
1577 /*
1578 * We may be facing a 100-continue response, or any other informational
1579 * 1xx response which is non-final, in which case this is not the right
1580 * response, and we're waiting for the next one. Let's allow this response
1581 * to go to the client and wait for the next one. There's an exception for
1582 * 101 which is used later in the code to switch protocols.
1583 */
1584 if (txn->status < 200 &&
1585 (txn->status == 100 || txn->status >= 102)) {
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001586 FLT_STRM_CB(s, flt_http_reset(s, msg));
Christopher Faulet421e7692019-06-13 11:16:45 +02001587 htx->first = channel_htx_fwd_headers(rep, htx);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001588 msg->msg_state = HTTP_MSG_RPBEFORE;
Christopher Faulet3499f622019-09-03 15:23:54 +02001589 msg->flags = 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001590 txn->status = 0;
1591 s->logs.t_data = -1; /* was not a response yet */
Christopher Faulet7d518452020-08-31 11:07:07 +02001592 rep->flags |= CF_SEND_DONTWAIT; /* Send ASAP informational messages */
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001593 goto next_one;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001594 }
1595
Christopher Faulet6e6c7b12021-01-08 16:02:05 +01001596 /* A 101-switching-protocols must contains a Connection header with the
1597 * "upgrade" option and the request too. It means both are agree to
1598 * upgrade. It is not so strict because there is no test on the Upgrade
1599 * header content. But it is probably stronger enough for now.
1600 */
1601 if (txn->status == 101 &&
1602 (!(txn->req.flags & HTTP_MSGF_CONN_UPG) || !(txn->rsp.flags & HTTP_MSGF_CONN_UPG)))
1603 goto return_bad_res;
1604
Christopher Faulete0768eb2018-10-03 16:38:02 +02001605 /*
1606 * 2: check for cacheability.
1607 */
1608
1609 switch (txn->status) {
1610 case 200:
1611 case 203:
1612 case 204:
1613 case 206:
1614 case 300:
1615 case 301:
1616 case 404:
1617 case 405:
1618 case 410:
1619 case 414:
1620 case 501:
1621 break;
1622 default:
1623 /* RFC7231#6.1:
1624 * Responses with status codes that are defined as
1625 * cacheable by default (e.g., 200, 203, 204, 206,
1626 * 300, 301, 404, 405, 410, 414, and 501 in this
1627 * specification) can be reused by a cache with
1628 * heuristic expiration unless otherwise indicated
1629 * by the method definition or explicit cache
1630 * controls [RFC7234]; all other status codes are
1631 * not cacheable by default.
1632 */
1633 txn->flags &= ~(TX_CACHEABLE | TX_CACHE_COOK);
1634 break;
1635 }
1636
1637 /*
1638 * 3: we may need to capture headers
1639 */
1640 s->logs.logwait &= ~LW_RESP;
1641 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->res_cap))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001642 http_capture_headers(htx, s->res_cap, sess->fe->rsp_cap);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001643
Christopher Faulet9768c262018-10-22 09:34:31 +02001644 /* Skip parsing if no content length is possible. */
Christopher Fauletc75668e2020-12-07 18:10:32 +01001645 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status >= 200 && txn->status < 300) ||
Christopher Faulete0768eb2018-10-03 16:38:02 +02001646 txn->status == 101)) {
1647 /* Either we've established an explicit tunnel, or we're
1648 * switching the protocol. In both cases, we're very unlikely
1649 * to understand the next protocols. We have to switch to tunnel
1650 * mode, so that we transfer the request and responses then let
1651 * this protocol pass unmodified. When we later implement specific
1652 * parsers for such protocols, we'll want to check the Upgrade
1653 * header which contains information about that protocol for
1654 * responses with status 101 (eg: see RFC2817 about TLS).
1655 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02001656 txn->flags |= TX_CON_WANT_TUN;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001657 }
1658
Christopher Faulet61608322018-11-23 16:23:45 +01001659 /* check for NTML authentication headers in 401 (WWW-Authenticate) and
1660 * 407 (Proxy-Authenticate) responses and set the connection to private
1661 */
Christopher Faulet95a61e82021-12-22 14:22:03 +01001662 srv_conn = cs_conn(s->csb);
Christopher Faulet61608322018-11-23 16:23:45 +01001663 if (srv_conn) {
1664 struct ist hdr;
1665 struct http_hdr_ctx ctx;
1666
1667 if (txn->status == 401)
1668 hdr = ist("WWW-Authenticate");
1669 else if (txn->status == 407)
1670 hdr = ist("Proxy-Authenticate");
1671 else
1672 goto end;
1673
1674 ctx.blk = NULL;
1675 while (http_find_header(htx, hdr, &ctx, 0)) {
Willy Tarreauf1dcced2020-05-07 19:27:02 +02001676 /* If www-authenticate contains "Negotiate", "Nego2", or "NTLM",
1677 * possibly followed by blanks and a base64 string, the connection
1678 * is private. Since it's a mess to deal with, we only check for
1679 * values starting with "NTLM" or "Nego". Note that often multiple
1680 * headers are sent by the server there.
1681 */
1682 if ((ctx.value.len >= 4 && strncasecmp(ctx.value.ptr, "Nego", 4) == 0) ||
Willy Tarreau49a1d282020-05-07 19:10:15 +02001683 (ctx.value.len >= 4 && strncasecmp(ctx.value.ptr, "NTLM", 4) == 0)) {
Olivier Houchard250031e2019-05-29 15:01:50 +02001684 sess->flags |= SESS_FL_PREFER_LAST;
Christopher Faulet08016ab2020-07-01 16:10:06 +02001685 conn_set_owner(srv_conn, sess, NULL);
Christopher Faulet21ddc742020-07-01 15:26:14 +02001686 conn_set_private(srv_conn);
Ilya Shipitsin6b79f382020-07-23 00:32:55 +05001687 /* If it fail now, the same will be done in mux->detach() callback */
Christopher Faulet08016ab2020-07-01 16:10:06 +02001688 session_add_conn(srv_conn->owner, srv_conn, srv_conn->target);
Willy Tarreauf1dcced2020-05-07 19:27:02 +02001689 break;
Olivier Houchard250031e2019-05-29 15:01:50 +02001690 }
Christopher Faulet61608322018-11-23 16:23:45 +01001691 }
1692 }
1693
1694 end:
Christopher Faulete0768eb2018-10-03 16:38:02 +02001695 /* we want to have the response time before we start processing it */
1696 s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now);
1697
1698 /* end of job, return OK */
1699 rep->analysers &= ~an_bit;
1700 rep->analyse_exp = TICK_ETERNITY;
1701 channel_auto_close(rep);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001702 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001703 return 1;
1704
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001705 return_int_err:
Willy Tarreau4781b152021-04-06 13:53:36 +02001706 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
1707 _HA_ATOMIC_INC(&s->be->be_counters.internal_errors);
William Lallemand36119de2021-03-08 15:26:48 +01001708 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001709 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Fauletb8a53712019-12-16 11:29:38 +01001710 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001711 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.internal_errors);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001712 txn->status = 500;
1713 if (!(s->flags & SF_ERR_MASK))
1714 s->flags |= SF_ERR_INTERNAL;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001715 goto return_prx_cond;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001716
1717 return_bad_res:
Willy Tarreau4781b152021-04-06 13:53:36 +02001718 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Faulet47365272018-10-31 17:40:50 +01001719 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001720 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001721 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Christopher Faulet47365272018-10-31 17:40:50 +01001722 }
Olivier Houcharde3249a92019-05-03 23:01:47 +02001723 if ((s->be->retry_type & PR_RE_JUNK_REQUEST) &&
Christopher Faulete05bf9e2022-03-29 15:23:40 +02001724 (txn->flags & TX_L7_RETRY) &&
Christopher Faulet77397992022-04-04 11:07:08 +02001725 do_l7_retry(s, s->csb) == 0) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001726 DBG_TRACE_DEVEL("leaving on L7 retry",
1727 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharde3249a92019-05-03 23:01:47 +02001728 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001729 }
Christopher Faulet47365272018-10-31 17:40:50 +01001730 txn->status = 502;
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001731 stream_inc_http_fail_ctr(s);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001732 /* fall through */
1733
Christopher Fauletb8a53712019-12-16 11:29:38 +01001734 return_prx_cond:
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001735 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet47365272018-10-31 17:40:50 +01001736
1737 if (!(s->flags & SF_ERR_MASK))
1738 s->flags |= SF_ERR_PRXCOND;
1739 if (!(s->flags & SF_FINST_MASK))
1740 s->flags |= SF_FINST_H;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001741
Christopher Faulet8abe7122022-03-30 15:10:18 +02001742 s->csb->flags |= CS_FL_NOLINGER;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001743 DBG_TRACE_DEVEL("leaving on error",
1744 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulet47365272018-10-31 17:40:50 +01001745 return 0;
1746
Christopher Faulete0768eb2018-10-03 16:38:02 +02001747 abort_keep_alive:
1748 /* A keep-alive request to the server failed on a network error.
1749 * The client is required to retry. We need to close without returning
1750 * any other information so that the client retries.
1751 */
1752 txn->status = 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001753 s->logs.logwait = 0;
1754 s->logs.level = 0;
1755 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001756 http_reply_and_close(s, txn->status, NULL);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001757 DBG_TRACE_DEVEL("leaving by closing K/A connection",
1758 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001759 return 0;
1760}
1761
1762/* This function performs all the processing enabled for the current response.
1763 * It normally returns 1 unless it wants to break. It relies on buffers flags,
1764 * and updates s->res.analysers. It might make sense to explode it into several
1765 * other functions. It works like process_request (see indications above).
1766 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001767int http_process_res_common(struct stream *s, struct channel *rep, int an_bit, struct proxy *px)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001768{
1769 struct session *sess = s->sess;
1770 struct http_txn *txn = s->txn;
1771 struct http_msg *msg = &txn->rsp;
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001772 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001773 struct proxy *cur_proxy;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001774 enum rule_result ret = HTTP_RULE_RES_CONT;
1775
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001776 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
1777 return 0;
Christopher Faulet9768c262018-10-22 09:34:31 +02001778
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001779 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001780
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001781 htx = htxbuf(&rep->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001782
1783 /* The stats applet needs to adjust the Connection header but we don't
1784 * apply any filter there.
1785 */
1786 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
1787 rep->analysers &= ~an_bit;
1788 rep->analyse_exp = TICK_ETERNITY;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001789 goto end;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001790 }
1791
1792 /*
1793 * We will have to evaluate the filters.
1794 * As opposed to version 1.2, now they will be evaluated in the
1795 * filters order and not in the header order. This means that
1796 * each filter has to be validated among all headers.
1797 *
1798 * Filters are tried with ->be first, then with ->fe if it is
1799 * different from ->be.
1800 *
1801 * Maybe we are in resume condiion. In this case I choose the
1802 * "struct proxy" which contains the rule list matching the resume
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05001803 * pointer. If none of these "struct proxy" match, I initialise
Christopher Faulete0768eb2018-10-03 16:38:02 +02001804 * the process with the first one.
1805 *
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05001806 * In fact, I check only correspondence between the current list
Christopher Faulete0768eb2018-10-03 16:38:02 +02001807 * pointer and the ->fe rule list. If it doesn't match, I initialize
1808 * the loop with the ->be.
1809 */
Christopher Fauletd4150ad2021-10-13 15:35:55 +02001810 if (s->current_rule_list == &sess->fe->http_res_rules ||
1811 (sess->fe->defpx && s->current_rule_list == &sess->fe->defpx->http_res_rules))
Christopher Faulete0768eb2018-10-03 16:38:02 +02001812 cur_proxy = sess->fe;
1813 else
1814 cur_proxy = s->be;
Christopher Fauletd4150ad2021-10-13 15:35:55 +02001815
Christopher Faulete0768eb2018-10-03 16:38:02 +02001816 while (1) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001817 /* evaluate http-response rules */
Christopher Faulet46f46df2021-11-09 16:33:25 +01001818 if (ret == HTTP_RULE_RES_CONT || ret == HTTP_RULE_RES_STOP) {
Christopher Fauletd4150ad2021-10-13 15:35:55 +02001819 struct list *def_rules, *rules;
1820
1821 def_rules = ((cur_proxy->defpx && (cur_proxy == s->be || cur_proxy->defpx != s->be->defpx)) ? &cur_proxy->defpx->http_res_rules : NULL);
1822 rules = &cur_proxy->http_res_rules;
1823
1824 ret = http_res_get_intercept_rule(cur_proxy, def_rules, rules, s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001825
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001826 switch (ret) {
1827 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
1828 goto return_prx_yield;
1829
1830 case HTTP_RULE_RES_CONT:
1831 case HTTP_RULE_RES_STOP: /* nothing to do */
1832 break;
1833
1834 case HTTP_RULE_RES_DENY: /* deny or tarpit */
1835 goto deny;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001836
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001837 case HTTP_RULE_RES_ABRT: /* abort request, response already sent */
1838 goto return_prx_cond;
1839
1840 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
Christopher Fauletb8a53712019-12-16 11:29:38 +01001841 goto done;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001842
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001843 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
1844 goto return_bad_res;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001845
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001846 case HTTP_RULE_RES_ERROR: /* failed with a bad request */
1847 goto return_int_err;
1848 }
1849
1850 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001851
Christopher Faulete0768eb2018-10-03 16:38:02 +02001852 /* check whether we're already working on the frontend */
1853 if (cur_proxy == sess->fe)
1854 break;
1855 cur_proxy = sess->fe;
1856 }
1857
Christopher Faulete0768eb2018-10-03 16:38:02 +02001858 /* OK that's all we can do for 1xx responses */
1859 if (unlikely(txn->status < 200 && txn->status != 101))
Christopher Fauletf2824e62018-10-01 12:12:37 +02001860 goto end;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001861
1862 /*
1863 * Now check for a server cookie.
1864 */
1865 if (s->be->cookie_name || sess->fe->capture_name || (s->be->options & PR_O_CHK_CACHE))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001866 http_manage_server_side_cookies(s, rep);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001867
1868 /*
1869 * Check for cache-control or pragma headers if required.
1870 */
1871 if ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001872 http_check_response_for_cacheability(s, rep);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001873
1874 /*
1875 * Add server cookie in the response if needed
1876 */
1877 if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) &&
1878 !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) &&
1879 (!(s->flags & SF_DIRECT) ||
1880 ((s->be->cookie_maxidle || txn->cookie_last_date) &&
1881 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
1882 (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
1883 (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
1884 (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
1885 !(s->flags & SF_IGNORE_PRST)) {
1886 /* the server is known, it's not the one the client requested, or the
1887 * cookie's last seen date needs to be refreshed. We have to
1888 * insert a set-cookie here, except if we want to insert only on POST
1889 * requests and this one isn't. Note that servers which don't have cookies
1890 * (eg: some backup servers) will return a full cookie removal request.
1891 */
Willy Tarreau88bc8002021-12-06 07:01:02 +00001892 if (!__objt_server(s->target)->cookie) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001893 chunk_printf(&trash,
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001894 "%s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
Christopher Faulete0768eb2018-10-03 16:38:02 +02001895 s->be->cookie_name);
1896 }
1897 else {
Willy Tarreau88bc8002021-12-06 07:01:02 +00001898 chunk_printf(&trash, "%s=%s", s->be->cookie_name, __objt_server(s->target)->cookie);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001899
1900 if (s->be->cookie_maxidle || s->be->cookie_maxlife) {
1901 /* emit last_date, which is mandatory */
1902 trash.area[trash.data++] = COOKIE_DELIM_DATE;
1903 s30tob64((date.tv_sec+3) >> 2,
1904 trash.area + trash.data);
1905 trash.data += 5;
1906
1907 if (s->be->cookie_maxlife) {
1908 /* emit first_date, which is either the original one or
1909 * the current date.
1910 */
1911 trash.area[trash.data++] = COOKIE_DELIM_DATE;
1912 s30tob64(txn->cookie_first_date ?
1913 txn->cookie_first_date >> 2 :
1914 (date.tv_sec+3) >> 2,
1915 trash.area + trash.data);
1916 trash.data += 5;
1917 }
1918 }
1919 chunk_appendf(&trash, "; path=/");
1920 }
1921
1922 if (s->be->cookie_domain)
1923 chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain);
1924
1925 if (s->be->ck_opts & PR_CK_HTTPONLY)
1926 chunk_appendf(&trash, "; HttpOnly");
1927
1928 if (s->be->ck_opts & PR_CK_SECURE)
1929 chunk_appendf(&trash, "; Secure");
1930
Christopher Faulet2f533902020-01-21 11:06:48 +01001931 if (s->be->cookie_attrs)
1932 chunk_appendf(&trash, "; %s", s->be->cookie_attrs);
1933
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001934 if (unlikely(!http_add_header(htx, ist("Set-Cookie"), ist2(trash.area, trash.data))))
Christopher Fauletb8a53712019-12-16 11:29:38 +01001935 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001936
1937 txn->flags &= ~TX_SCK_MASK;
1938 if (__objt_server(s->target)->cookie && (s->flags & SF_DIRECT))
1939 /* the server did not change, only the date was updated */
1940 txn->flags |= TX_SCK_UPDATED;
1941 else
1942 txn->flags |= TX_SCK_INSERTED;
1943
1944 /* Here, we will tell an eventual cache on the client side that we don't
1945 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
1946 * Some caches understand the correct form: 'no-cache="set-cookie"', but
1947 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
1948 */
1949 if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
1950
1951 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
1952
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001953 if (unlikely(!http_add_header(htx, ist("Cache-control"), ist("private"))))
Christopher Fauletb8a53712019-12-16 11:29:38 +01001954 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001955 }
1956 }
1957
1958 /*
1959 * Check if result will be cacheable with a cookie.
1960 * We'll block the response if security checks have caught
1961 * nasty things such as a cacheable cookie.
1962 */
1963 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
1964 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
1965 (s->be->options & PR_O_CHK_CACHE)) {
1966 /* we're in presence of a cacheable response containing
1967 * a set-cookie header. We'll block it as requested by
1968 * the 'checkcache' option, and send an alert.
1969 */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001970 ha_alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreau88bc8002021-12-06 07:01:02 +00001971 s->be->id, objt_server(s->target) ? __objt_server(s->target)->id : "<dispatch>");
Christopher Faulete0768eb2018-10-03 16:38:02 +02001972 send_log(s->be, LOG_ALERT,
1973 "Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreau88bc8002021-12-06 07:01:02 +00001974 s->be->id, objt_server(s->target) ? __objt_server(s->target)->id : "<dispatch>");
Christopher Fauletb8a53712019-12-16 11:29:38 +01001975 goto deny;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001976 }
1977
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001978 end:
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01001979 /*
1980 * Evaluate after-response rules before forwarding the response. rules
1981 * from the backend are evaluated first, then one from the frontend if
1982 * it differs.
1983 */
1984 if (!http_eval_after_res_rules(s))
1985 goto return_int_err;
1986
Christopher Fauletc2ac5e42021-03-08 18:20:09 +01001987 /* Filter the response headers if there are filters attached to the
1988 * stream.
1989 */
1990 if (HAS_FILTERS(s))
1991 rep->analysers |= AN_RES_FLT_HTTP_HDRS;
1992
Christopher Faulete0768eb2018-10-03 16:38:02 +02001993 /* Always enter in the body analyzer */
1994 rep->analysers &= ~AN_RES_FLT_XFER_DATA;
1995 rep->analysers |= AN_RES_HTTP_XFER_BODY;
1996
1997 /* if the user wants to log as soon as possible, without counting
1998 * bytes from the server, then this is the right moment. We have
1999 * to temporarily assign bytes_out to log what we currently have.
2000 */
2001 if (!LIST_ISEMPTY(&sess->fe->logformat) && !(s->logs.logwait & LW_BYTES)) {
2002 s->logs.t_close = s->logs.t_data; /* to get a valid end date */
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002003 s->logs.bytes_out = htx->data;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002004 s->do_log(s);
2005 s->logs.bytes_out = 0;
2006 }
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002007
Christopher Fauletb8a53712019-12-16 11:29:38 +01002008 done:
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002009 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002010 rep->analysers &= ~an_bit;
2011 rep->analyse_exp = TICK_ETERNITY;
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002012 s->current_rule = s->current_rule_list = NULL;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002013 return 1;
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002014
Christopher Fauletb8a53712019-12-16 11:29:38 +01002015 deny:
Willy Tarreau4781b152021-04-06 13:53:36 +02002016 _HA_ATOMIC_INC(&sess->fe->fe_counters.denied_resp);
2017 _HA_ATOMIC_INC(&s->be->be_counters.denied_resp);
William Lallemand36119de2021-03-08 15:26:48 +01002018 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02002019 _HA_ATOMIC_INC(&sess->listener->counters->denied_resp);
Christopher Fauleta08546b2019-12-16 16:07:34 +01002020 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02002021 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.denied_resp);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002022 goto return_prx_err;
2023
2024 return_int_err:
2025 txn->status = 500;
2026 if (!(s->flags & SF_ERR_MASK))
2027 s->flags |= SF_ERR_INTERNAL;
Willy Tarreau4781b152021-04-06 13:53:36 +02002028 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
2029 _HA_ATOMIC_INC(&s->be->be_counters.internal_errors);
Dragan Dosen9a006f92021-09-21 13:02:09 +02002030 if (sess->listener && sess->listener->counters)
2031 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Fauletcff0f732019-12-16 16:13:44 +01002032 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02002033 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.internal_errors);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002034 goto return_prx_err;
2035
2036 return_bad_res:
2037 txn->status = 502;
Willy Tarreau826f3ab2021-02-10 12:07:15 +01002038 stream_inc_http_fail_ctr(s);
Willy Tarreau4781b152021-04-06 13:53:36 +02002039 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Fauleta20a6532020-02-05 10:16:41 +01002040 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02002041 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Christopher Fauleta20a6532020-02-05 10:16:41 +01002042 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_RSP);
2043 }
Christopher Fauletb8a53712019-12-16 11:29:38 +01002044 /* fall through */
2045
2046 return_prx_err:
2047 http_reply_and_close(s, txn->status, http_error_message(s));
2048 /* fall through */
2049
2050 return_prx_cond:
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002051 s->logs.t_data = -1; /* was not a valid response */
Christopher Faulet8abe7122022-03-30 15:10:18 +02002052 s->csb->flags |= CS_FL_NOLINGER;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002053
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002054 if (!(s->flags & SF_ERR_MASK))
2055 s->flags |= SF_ERR_PRXCOND;
2056 if (!(s->flags & SF_FINST_MASK))
2057 s->flags |= SF_FINST_H;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002058
Christopher Faulete58c0002020-03-02 16:21:01 +01002059 rep->analysers &= AN_RES_FLT_END;
2060 s->req.analysers &= AN_REQ_FLT_END;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002061 rep->analyse_exp = TICK_ETERNITY;
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002062 s->current_rule = s->current_rule_list = NULL;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002063 DBG_TRACE_DEVEL("leaving on error",
2064 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002065 return 0;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002066
2067 return_prx_yield:
2068 channel_dont_close(rep);
2069 DBG_TRACE_DEVEL("waiting for more data",
2070 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
2071 return 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002072}
2073
2074/* This function is an analyser which forwards response body (including chunk
2075 * sizes if any). It is called as soon as we must forward, even if we forward
2076 * zero byte. The only situation where it must not be called is when we're in
2077 * tunnel mode and we want to forward till the close. It's used both to forward
2078 * remaining data and to resync after end of body. It expects the msg_state to
2079 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
2080 * read more data, or 1 once we can go on with next request or end the stream.
2081 *
2082 * It is capable of compressing response data both in content-length mode and
2083 * in chunked mode. The state machines follows different flows depending on
2084 * whether content-length and chunked modes are used, since there are no
2085 * trailers in content-length :
2086 *
2087 * chk-mode cl-mode
2088 * ,----- BODY -----.
2089 * / \
2090 * V size > 0 V chk-mode
2091 * .--> SIZE -------------> DATA -------------> CRLF
2092 * | | size == 0 | last byte |
2093 * | v final crlf v inspected |
2094 * | TRAILERS -----------> DONE |
2095 * | |
2096 * `----------------------------------------------'
2097 *
2098 * Compression only happens in the DATA state, and must be flushed in final
2099 * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding
2100 * is performed at once on final states for all bytes parsed, or when leaving
2101 * on missing data.
2102 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002103int http_response_forward_body(struct stream *s, struct channel *res, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +02002104{
2105 struct session *sess = s->sess;
2106 struct http_txn *txn = s->txn;
2107 struct http_msg *msg = &s->txn->rsp;
Christopher Faulet9768c262018-10-22 09:34:31 +02002108 struct htx *htx;
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002109 int ret;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002110
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002111 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002112
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002113 htx = htxbuf(&res->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002114
Christopher Fauletb9a92f32019-09-09 10:15:21 +02002115 if (htx->flags & HTX_FL_PARSING_ERROR)
2116 goto return_bad_res;
2117 if (htx->flags & HTX_FL_PROCESSING_ERROR)
2118 goto return_int_err;
2119
Christopher Faulete0768eb2018-10-03 16:38:02 +02002120 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Christopher Fauletf2824e62018-10-01 12:12:37 +02002121 ((res->flags & CF_SHUTW) && (res->to_forward || co_data(res)))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02002122 /* Output closed while we were sending data. We must abort and
2123 * wake the other side up.
2124 */
Christopher Faulete0768eb2018-10-03 16:38:02 +02002125 msg->msg_state = HTTP_MSG_ERROR;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002126 http_end_response(s);
2127 http_end_request(s);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002128 DBG_TRACE_DEVEL("leaving on error",
2129 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002130 return 1;
2131 }
2132
Christopher Faulet9768c262018-10-22 09:34:31 +02002133 if (msg->msg_state == HTTP_MSG_BODY)
2134 msg->msg_state = HTTP_MSG_DATA;
2135
Christopher Faulete0768eb2018-10-03 16:38:02 +02002136 /* in most states, we should abort in case of early close */
2137 channel_auto_close(res);
2138
Christopher Faulete0768eb2018-10-03 16:38:02 +02002139 if (res->to_forward) {
Christopher Faulet66af0b22019-03-22 14:54:52 +01002140 if (res->to_forward == CHN_INFINITE_FORWARD) {
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002141 if (res->flags & CF_EOI)
2142 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet66af0b22019-03-22 14:54:52 +01002143 }
2144 else {
2145 /* We can't process the buffer's contents yet */
2146 res->flags |= CF_WAKE_WRITE;
2147 goto missing_data_or_waiting;
2148 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02002149 }
2150
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002151 if (msg->msg_state >= HTTP_MSG_ENDING)
2152 goto ending;
2153
Christopher Fauletc75668e2020-12-07 18:10:32 +01002154 if ((txn->meth == HTTP_METH_CONNECT && txn->status >= 200 && txn->status < 300) || txn->status == 101 ||
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002155 (!(msg->flags & HTTP_MSGF_XFER_LEN) && !HAS_RSP_DATA_FILTERS(s))) {
2156 msg->msg_state = HTTP_MSG_ENDING;
2157 goto ending;
2158 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002159
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002160 /* Forward input data. We get it by removing all outgoing data not
2161 * forwarded yet from HTX data size. If there are some data filters, we
2162 * let them decide the amount of data to forward.
Christopher Faulet9768c262018-10-22 09:34:31 +02002163 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002164 if (HAS_RSP_DATA_FILTERS(s)) {
2165 ret = flt_http_payload(s, msg, htx->data);
2166 if (ret < 0)
2167 goto return_bad_res;
Christopher Faulet421e7692019-06-13 11:16:45 +02002168 c_adv(res, ret);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002169 }
2170 else {
Christopher Faulet421e7692019-06-13 11:16:45 +02002171 c_adv(res, htx->data - co_data(res));
Christopher Faulet66af0b22019-03-22 14:54:52 +01002172 if (msg->flags & HTTP_MSGF_XFER_LEN)
2173 channel_htx_forward_forever(res, htx);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002174 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002175
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002176 if (htx->data != co_data(res))
2177 goto missing_data_or_waiting;
2178
2179 if (!(msg->flags & HTTP_MSGF_XFER_LEN) && res->flags & CF_SHUTR) {
2180 msg->msg_state = HTTP_MSG_ENDING;
2181 goto ending;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002182 }
2183
Christopher Faulet9768c262018-10-22 09:34:31 +02002184 /* Check if the end-of-message is reached and if so, switch the message
Christopher Fauletd20fdb02019-06-13 16:43:22 +02002185 * in HTTP_MSG_ENDING state. Then if all data was marked to be
2186 * forwarded, set the state to HTTP_MSG_DONE.
Christopher Faulet9768c262018-10-22 09:34:31 +02002187 */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002188 if (!(htx->flags & HTX_FL_EOM))
Christopher Faulet9768c262018-10-22 09:34:31 +02002189 goto missing_data_or_waiting;
2190
Christopher Fauletd20fdb02019-06-13 16:43:22 +02002191 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet9768c262018-10-22 09:34:31 +02002192
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002193 ending:
Christopher Faulet2151cdd2020-07-22 16:34:59 +02002194 res->flags &= ~CF_EXPECT_MORE; /* no more data are expected */
2195
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002196 /* other states, ENDING...TUNNEL */
2197 if (msg->msg_state >= HTTP_MSG_DONE)
2198 goto done;
Christopher Faulet9768c262018-10-22 09:34:31 +02002199
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002200 if (HAS_RSP_DATA_FILTERS(s)) {
2201 ret = flt_http_end(s, msg);
2202 if (ret <= 0) {
2203 if (!ret)
2204 goto missing_data_or_waiting;
2205 goto return_bad_res;
2206 }
2207 }
2208
Christopher Fauletc75668e2020-12-07 18:10:32 +01002209 if ((txn->meth == HTTP_METH_CONNECT && txn->status >= 200 && txn->status < 300) || txn->status == 101 ||
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002210 !(msg->flags & HTTP_MSGF_XFER_LEN)) {
2211 msg->msg_state = HTTP_MSG_TUNNEL;
2212 goto ending;
2213 }
2214 else {
2215 msg->msg_state = HTTP_MSG_DONE;
2216 res->to_forward = 0;
2217 }
2218
2219 done:
2220
2221 channel_dont_close(res);
2222
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002223 http_end_response(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002224 if (!(res->analysers & an_bit)) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002225 http_end_request(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002226 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
2227 if (res->flags & CF_SHUTW) {
2228 /* response errors are most likely due to the
2229 * client aborting the transfer. */
Christopher Faulet93e02d82019-03-08 14:18:50 +01002230 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002231 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02002232 goto return_bad_res;
2233 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002234 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002235 return 1;
2236 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002237 DBG_TRACE_DEVEL("waiting for the end of the HTTP txn",
2238 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002239 return 0;
2240
2241 missing_data_or_waiting:
2242 if (res->flags & CF_SHUTW)
Christopher Faulet93e02d82019-03-08 14:18:50 +01002243 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002244
2245 /* stop waiting for data if the input is closed before the end. If the
2246 * client side was already closed, it means that the client has aborted,
2247 * so we don't want to count this as a server abort. Otherwise it's a
2248 * server abort.
2249 */
Christopher Fauletd20fdb02019-06-13 16:43:22 +02002250 if (msg->msg_state < HTTP_MSG_ENDING && res->flags & CF_SHUTR) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02002251 if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))
Christopher Faulet93e02d82019-03-08 14:18:50 +01002252 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002253 /* If we have some pending data, we continue the processing */
Christopher Faulet93e02d82019-03-08 14:18:50 +01002254 if (htx_is_empty(htx))
2255 goto return_srv_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002256 }
2257
Christopher Faulete0768eb2018-10-03 16:38:02 +02002258 /* When TE: chunked is used, we need to get there again to parse
2259 * remaining chunks even if the server has closed, so we don't want to
Christopher Faulet9768c262018-10-22 09:34:31 +02002260 * set CF_DONTCLOSE. Similarly when there is a content-leng or if there
2261 * are filters registered on the stream, we don't want to forward a
2262 * close
Christopher Faulete0768eb2018-10-03 16:38:02 +02002263 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002264 if ((msg->flags & HTTP_MSGF_XFER_LEN) || HAS_RSP_DATA_FILTERS(s))
Christopher Faulete0768eb2018-10-03 16:38:02 +02002265 channel_dont_close(res);
2266
2267 /* We know that more data are expected, but we couldn't send more that
2268 * what we did. So we always set the CF_EXPECT_MORE flag so that the
2269 * system knows it must not set a PUSH on this first part. Interactive
2270 * modes are already handled by the stream sock layer. We must not do
2271 * this in content-length mode because it could present the MSG_MORE
2272 * flag with the last block of forwarded data, which would cause an
2273 * additional delay to be observed by the receiver.
2274 */
Christopher Faulet2151cdd2020-07-22 16:34:59 +02002275 if (HAS_RSP_DATA_FILTERS(s))
Christopher Faulete0768eb2018-10-03 16:38:02 +02002276 res->flags |= CF_EXPECT_MORE;
2277
2278 /* the stream handler will take care of timeouts and errors */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002279 DBG_TRACE_DEVEL("waiting for more data to forward",
2280 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002281 return 0;
2282
Christopher Faulet93e02d82019-03-08 14:18:50 +01002283 return_srv_abort:
Willy Tarreau4781b152021-04-06 13:53:36 +02002284 _HA_ATOMIC_INC(&sess->fe->fe_counters.srv_aborts);
2285 _HA_ATOMIC_INC(&s->be->be_counters.srv_aborts);
William Lallemand36119de2021-03-08 15:26:48 +01002286 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02002287 _HA_ATOMIC_INC(&sess->listener->counters->srv_aborts);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002288 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02002289 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.srv_aborts);
Willy Tarreau826f3ab2021-02-10 12:07:15 +01002290 stream_inc_http_fail_ctr(s);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002291 if (!(s->flags & SF_ERR_MASK))
2292 s->flags |= SF_ERR_SRVCL;
2293 goto return_error;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002294
Christopher Faulet93e02d82019-03-08 14:18:50 +01002295 return_cli_abort:
Willy Tarreau4781b152021-04-06 13:53:36 +02002296 _HA_ATOMIC_INC(&sess->fe->fe_counters.cli_aborts);
2297 _HA_ATOMIC_INC(&s->be->be_counters.cli_aborts);
William Lallemand36119de2021-03-08 15:26:48 +01002298 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02002299 _HA_ATOMIC_INC(&sess->listener->counters->cli_aborts);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002300 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02002301 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.cli_aborts);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002302 if (!(s->flags & SF_ERR_MASK))
2303 s->flags |= SF_ERR_CLICL;
2304 goto return_error;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002305
Christopher Fauletb9a92f32019-09-09 10:15:21 +02002306 return_int_err:
Willy Tarreau4781b152021-04-06 13:53:36 +02002307 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
2308 _HA_ATOMIC_INC(&s->be->be_counters.internal_errors);
William Lallemand36119de2021-03-08 15:26:48 +01002309 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02002310 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002311 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02002312 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.internal_errors);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02002313 if (!(s->flags & SF_ERR_MASK))
2314 s->flags |= SF_ERR_INTERNAL;
2315 goto return_error;
2316
Christopher Faulet93e02d82019-03-08 14:18:50 +01002317 return_bad_res:
Willy Tarreau4781b152021-04-06 13:53:36 +02002318 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002319 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02002320 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002321 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_RSP);
2322 }
Willy Tarreau826f3ab2021-02-10 12:07:15 +01002323 stream_inc_http_fail_ctr(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002324 if (!(s->flags & SF_ERR_MASK))
Christopher Faulet93e02d82019-03-08 14:18:50 +01002325 s->flags |= SF_ERR_SRVCL;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002326 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +02002327
Christopher Faulet93e02d82019-03-08 14:18:50 +01002328 return_error:
Christopher Faulete0768eb2018-10-03 16:38:02 +02002329 /* don't send any error message as we're in the body */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002330 http_reply_and_close(s, txn->status, NULL);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002331 if (!(s->flags & SF_FINST_MASK))
2332 s->flags |= SF_FINST_D;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002333 DBG_TRACE_DEVEL("leaving on error",
2334 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002335 return 0;
2336}
2337
Christopher Fauletf2824e62018-10-01 12:12:37 +02002338/* Perform an HTTP redirect based on the information in <rule>. The function
Willy Tarreaubc1223b2021-09-02 16:54:33 +02002339 * returns zero in case of an irrecoverable error such as too large a request
2340 * to build a valid response, 1 in case of successful redirect (hence the rule
2341 * is final), or 2 if the rule has to be silently skipped.
Christopher Fauletf2824e62018-10-01 12:12:37 +02002342 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002343int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn)
Christopher Fauletf2824e62018-10-01 12:12:37 +02002344{
Christopher Faulet99daf282018-11-28 22:58:13 +01002345 struct channel *req = &s->req;
2346 struct channel *res = &s->res;
2347 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01002348 struct htx_sl *sl;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002349 struct buffer *chunk;
Christopher Faulet99daf282018-11-28 22:58:13 +01002350 struct ist status, reason, location;
2351 unsigned int flags;
Christopher Fauleteab17572022-04-26 20:34:38 +02002352 int ret = 1, close = 0; /* Try to keep the connection alive byt default */
Christopher Fauletf2824e62018-10-01 12:12:37 +02002353
2354 chunk = alloc_trash_chunk();
Christopher Fauletb8a53712019-12-16 11:29:38 +01002355 if (!chunk) {
2356 if (!(s->flags & SF_ERR_MASK))
2357 s->flags |= SF_ERR_RESOURCE;
Christopher Faulet99daf282018-11-28 22:58:13 +01002358 goto fail;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002359 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002360
Christopher Faulet99daf282018-11-28 22:58:13 +01002361 /*
2362 * Create the location
2363 */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002364 htx = htxbuf(&req->buf);
Christopher Fauletf2824e62018-10-01 12:12:37 +02002365 switch(rule->type) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002366 case REDIRECT_TYPE_SCHEME: {
2367 struct http_hdr_ctx ctx;
2368 struct ist path, host;
Amaury Denoyellec453f952021-07-06 11:40:12 +02002369 struct http_uri_parser parser;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002370
Christopher Faulet99daf282018-11-28 22:58:13 +01002371 host = ist("");
2372 ctx.blk = NULL;
2373 if (http_find_header(htx, ist("Host"), &ctx, 0))
2374 host = ctx.value;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002375
Christopher Faulet297fbb42019-05-13 14:41:27 +02002376 sl = http_get_stline(htx);
Amaury Denoyellec453f952021-07-06 11:40:12 +02002377 parser = http_uri_parser_init(htx_sl_req_uri(sl));
2378 path = http_parse_path(&parser);
Christopher Faulet99daf282018-11-28 22:58:13 +01002379 /* build message using path */
Tim Duesterhused526372020-03-05 17:56:33 +01002380 if (isttest(path)) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002381 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2382 int qs = 0;
2383 while (qs < path.len) {
2384 if (*(path.ptr + qs) == '?') {
2385 path.len = qs;
2386 break;
2387 }
2388 qs++;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002389 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002390 }
2391 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002392 else
2393 path = ist("/");
Christopher Fauletf2824e62018-10-01 12:12:37 +02002394
Christopher Faulet99daf282018-11-28 22:58:13 +01002395 if (rule->rdr_str) { /* this is an old "redirect" rule */
2396 /* add scheme */
2397 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2398 goto fail;
2399 }
2400 else {
2401 /* add scheme with executing log format */
2402 chunk->data += build_logline(s, chunk->area + chunk->data,
2403 chunk->size - chunk->data,
2404 &rule->rdr_fmt);
2405 }
2406 /* add "://" + host + path */
2407 if (!chunk_memcat(chunk, "://", 3) ||
2408 !chunk_memcat(chunk, host.ptr, host.len) ||
2409 !chunk_memcat(chunk, path.ptr, path.len))
2410 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002411
Christopher Faulet99daf282018-11-28 22:58:13 +01002412 /* append a slash at the end of the location if needed and missing */
2413 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
2414 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
2415 if (chunk->data + 1 >= chunk->size)
2416 goto fail;
2417 chunk->area[chunk->data++] = '/';
2418 }
2419 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002420 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002421
Christopher Faulet99daf282018-11-28 22:58:13 +01002422 case REDIRECT_TYPE_PREFIX: {
2423 struct ist path;
Amaury Denoyellec453f952021-07-06 11:40:12 +02002424 struct http_uri_parser parser;
Christopher Faulet99daf282018-11-28 22:58:13 +01002425
Christopher Faulet297fbb42019-05-13 14:41:27 +02002426 sl = http_get_stline(htx);
Amaury Denoyellec453f952021-07-06 11:40:12 +02002427 parser = http_uri_parser_init(htx_sl_req_uri(sl));
2428 path = http_parse_path(&parser);
Christopher Faulet99daf282018-11-28 22:58:13 +01002429 /* build message using path */
Tim Duesterhused526372020-03-05 17:56:33 +01002430 if (isttest(path)) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002431 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2432 int qs = 0;
2433 while (qs < path.len) {
2434 if (*(path.ptr + qs) == '?') {
2435 path.len = qs;
2436 break;
2437 }
2438 qs++;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002439 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002440 }
2441 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002442 else
2443 path = ist("/");
Christopher Fauletf2824e62018-10-01 12:12:37 +02002444
Christopher Faulet99daf282018-11-28 22:58:13 +01002445 if (rule->rdr_str) { /* this is an old "redirect" rule */
2446 /* add prefix. Note that if prefix == "/", we don't want to
2447 * add anything, otherwise it makes it hard for the user to
2448 * configure a self-redirection.
2449 */
2450 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
2451 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2452 goto fail;
2453 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002454 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002455 else {
2456 /* add prefix with executing log format */
2457 chunk->data += build_logline(s, chunk->area + chunk->data,
2458 chunk->size - chunk->data,
2459 &rule->rdr_fmt);
2460 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002461
Christopher Faulet99daf282018-11-28 22:58:13 +01002462 /* add path */
2463 if (!chunk_memcat(chunk, path.ptr, path.len))
2464 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002465
Christopher Faulet99daf282018-11-28 22:58:13 +01002466 /* append a slash at the end of the location if needed and missing */
2467 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
2468 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
2469 if (chunk->data + 1 >= chunk->size)
2470 goto fail;
2471 chunk->area[chunk->data++] = '/';
2472 }
2473 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002474 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002475 case REDIRECT_TYPE_LOCATION:
2476 default:
2477 if (rule->rdr_str) { /* this is an old "redirect" rule */
2478 /* add location */
2479 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2480 goto fail;
2481 }
2482 else {
2483 /* add location with executing log format */
Willy Tarreaubc1223b2021-09-02 16:54:33 +02002484 int len = build_logline(s, chunk->area + chunk->data,
2485 chunk->size - chunk->data,
2486 &rule->rdr_fmt);
Christopher Fauleteab17572022-04-26 20:34:38 +02002487 if (!len && rule->flags & REDIRECT_FLAG_IGNORE_EMPTY) {
2488 ret = 2;
2489 goto out;
2490 }
Willy Tarreaubc1223b2021-09-02 16:54:33 +02002491
2492 chunk->data += len;
Christopher Faulet99daf282018-11-28 22:58:13 +01002493 }
2494 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002495 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002496 location = ist2(chunk->area, chunk->data);
2497
2498 /*
2499 * Create the 30x response
2500 */
2501 switch (rule->code) {
2502 case 308:
2503 status = ist("308");
2504 reason = ist("Permanent Redirect");
2505 break;
2506 case 307:
2507 status = ist("307");
2508 reason = ist("Temporary Redirect");
2509 break;
2510 case 303:
2511 status = ist("303");
2512 reason = ist("See Other");
2513 break;
2514 case 301:
2515 status = ist("301");
2516 reason = ist("Moved Permanently");
2517 break;
2518 case 302:
2519 default:
2520 status = ist("302");
2521 reason = ist("Found");
2522 break;
2523 }
2524
Christopher Faulet08e66462019-05-23 16:44:59 +02002525 if (!(txn->req.flags & HTTP_MSGF_BODYLESS) && txn->req.msg_state != HTTP_MSG_DONE)
2526 close = 1;
2527
Christopher Faulet99daf282018-11-28 22:58:13 +01002528 htx = htx_from_buf(&res->buf);
Kevin Zhu96b36392020-01-07 09:42:55 +01002529 /* Trim any possible response */
2530 channel_htx_truncate(&s->res, htx);
Christopher Faulet99daf282018-11-28 22:58:13 +01002531 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
2532 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), status, reason);
2533 if (!sl)
2534 goto fail;
2535 sl->info.res.status = rule->code;
2536 s->txn->status = rule->code;
2537
Christopher Faulet08e66462019-05-23 16:44:59 +02002538 if (close && !htx_add_header(htx, ist("Connection"), ist("close")))
2539 goto fail;
2540
2541 if (!htx_add_header(htx, ist("Content-length"), ist("0")) ||
Christopher Faulet99daf282018-11-28 22:58:13 +01002542 !htx_add_header(htx, ist("Location"), location))
2543 goto fail;
2544
2545 if (rule->code == 302 || rule->code == 303 || rule->code == 307) {
2546 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")))
2547 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002548 }
2549
2550 if (rule->cookie_len) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002551 if (!htx_add_header(htx, ist("Set-Cookie"), ist2(rule->cookie_str, rule->cookie_len)))
2552 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002553 }
2554
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002555 if (!htx_add_endof(htx, HTX_BLK_EOH))
Christopher Faulet99daf282018-11-28 22:58:13 +01002556 goto fail;
2557
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002558 htx->flags |= HTX_FL_EOM;
Kevin Zhu96b36392020-01-07 09:42:55 +01002559 htx_to_buf(htx, &res->buf);
Christopher Fauleta72a7e42020-01-28 09:28:11 +01002560 if (!http_forward_proxy_resp(s, 1))
2561 goto fail;
Christopher Faulet99daf282018-11-28 22:58:13 +01002562
Christopher Faulet60b33a52020-01-28 09:18:10 +01002563 if (rule->flags & REDIRECT_FLAG_FROM_REQ) {
2564 /* let's log the request time */
2565 s->logs.tv_request = now;
Christopher Fauletd3475882021-10-04 14:16:46 +02002566 req->analysers &= AN_REQ_FLT_END;
Christopher Faulet99daf282018-11-28 22:58:13 +01002567
Christopher Faulet60b33a52020-01-28 09:18:10 +01002568 if (s->sess->fe == s->be) /* report it if the request was intercepted by the frontend */
Willy Tarreau4781b152021-04-06 13:53:36 +02002569 _HA_ATOMIC_INC(&s->sess->fe->fe_counters.intercepted_req);
Christopher Faulet60b33a52020-01-28 09:18:10 +01002570 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002571
2572 if (!(s->flags & SF_ERR_MASK))
2573 s->flags |= SF_ERR_LOCAL;
2574 if (!(s->flags & SF_FINST_MASK))
Christopher Faulet60b33a52020-01-28 09:18:10 +01002575 s->flags |= ((rule->flags & REDIRECT_FLAG_FROM_REQ) ? SF_FINST_R : SF_FINST_H);
Christopher Fauletf2824e62018-10-01 12:12:37 +02002576
Christopher Fauleteab17572022-04-26 20:34:38 +02002577 out:
Christopher Faulet99daf282018-11-28 22:58:13 +01002578 free_trash_chunk(chunk);
Christopher Fauleteab17572022-04-26 20:34:38 +02002579 return ret;
Christopher Faulet99daf282018-11-28 22:58:13 +01002580
2581 fail:
2582 /* If an error occurred, remove the incomplete HTTP response from the
2583 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01002584 channel_htx_truncate(res, htxbuf(&res->buf));
Christopher Fauleteab17572022-04-26 20:34:38 +02002585 ret = 0;
2586 goto out;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002587}
2588
Christopher Faulet92d34fe2019-12-17 09:20:34 +01002589/* Replace all headers matching the name <name>. The header value is replaced if
2590 * it matches the regex <re>. <str> is used for the replacement. If <full> is
2591 * set to 1, the full-line is matched and replaced. Otherwise, comma-separated
2592 * values are evaluated one by one. It returns 0 on success and -1 on error.
2593 */
2594int http_replace_hdrs(struct stream* s, struct htx *htx, struct ist name,
2595 const char *str, struct my_regex *re, int full)
Christopher Faulet72333522018-10-24 11:25:02 +02002596{
2597 struct http_hdr_ctx ctx;
2598 struct buffer *output = get_trash_chunk();
2599
Christopher Faulet72333522018-10-24 11:25:02 +02002600 ctx.blk = NULL;
Christopher Faulet92d34fe2019-12-17 09:20:34 +01002601 while (http_find_header(htx, name, &ctx, full)) {
Christopher Faulet72333522018-10-24 11:25:02 +02002602 if (!regex_exec_match2(re, ctx.value.ptr, ctx.value.len, MAX_MATCH, pmatch, 0))
2603 continue;
2604
2605 output->data = exp_replace(output->area, output->size, ctx.value.ptr, str, pmatch);
2606 if (output->data == -1)
2607 return -1;
2608 if (!http_replace_header_value(htx, &ctx, ist2(output->area, output->data)))
2609 return -1;
2610 }
2611 return 0;
2612}
2613
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002614/* This function executes one of the set-{method,path,query,uri} actions. It
2615 * takes the string from the variable 'replace' with length 'len', then modifies
2616 * the relevant part of the request line accordingly. Then it updates various
2617 * pointers to the next elements which were moved, and the total buffer length.
2618 * It finds the action to be performed in p[2], previously filled by function
2619 * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal
2620 * error, though this can be revisited when this code is finally exploited.
2621 *
2622 * 'action' can be '0' to replace method, '1' to replace path, '2' to replace
Christopher Faulet312294f2020-09-02 17:17:44 +02002623 * query string, 3 to replace uri or 4 to replace the path+query.
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002624 *
2625 * In query string case, the mark question '?' must be set at the start of the
2626 * string by the caller, event if the replacement query string is empty.
2627 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002628int http_req_replace_stline(int action, const char *replace, int len,
2629 struct proxy *px, struct stream *s)
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002630{
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002631 struct htx *htx = htxbuf(&s->req.buf);
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002632
2633 switch (action) {
2634 case 0: // method
2635 if (!http_replace_req_meth(htx, ist2(replace, len)))
2636 return -1;
2637 break;
2638
2639 case 1: // path
Christopher Fauletb8ce5052020-08-31 16:11:57 +02002640 if (!http_replace_req_path(htx, ist2(replace, len), 0))
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002641 return -1;
2642 break;
2643
2644 case 2: // query
2645 if (!http_replace_req_query(htx, ist2(replace, len)))
2646 return -1;
2647 break;
2648
2649 case 3: // uri
2650 if (!http_replace_req_uri(htx, ist2(replace, len)))
2651 return -1;
2652 break;
2653
Christopher Faulet312294f2020-09-02 17:17:44 +02002654 case 4: // path + query
2655 if (!http_replace_req_path(htx, ist2(replace, len), 1))
2656 return -1;
2657 break;
2658
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002659 default:
2660 return -1;
2661 }
2662 return 0;
2663}
2664
2665/* This function replace the HTTP status code and the associated message. The
Christopher Faulete00d06c2019-12-16 17:18:42 +01002666 * variable <status> contains the new status code. This function never fails. It
2667 * returns 0 in case of success, -1 in case of internal error.
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002668 */
Christopher Faulet96bff762019-12-17 13:46:18 +01002669int http_res_set_status(unsigned int status, struct ist reason, struct stream *s)
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002670{
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002671 struct htx *htx = htxbuf(&s->res.buf);
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002672 char *res;
2673
2674 chunk_reset(&trash);
2675 res = ultoa_o(status, trash.area, trash.size);
2676 trash.data = res - trash.area;
2677
2678 /* Do we have a custom reason format string? */
Tim Duesterhuse296d3e2020-03-05 17:56:31 +01002679 if (!isttest(reason)) {
Christopher Faulet96bff762019-12-17 13:46:18 +01002680 const char *str = http_get_reason(status);
Tim Duesterhusdcf753a2021-03-04 17:31:47 +01002681 reason = ist(str);
Christopher Faulet96bff762019-12-17 13:46:18 +01002682 }
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002683
Christopher Fauletbde2c4c2020-08-31 16:43:34 +02002684 if (!http_replace_res_status(htx, ist2(trash.area, trash.data), reason))
Christopher Faulete00d06c2019-12-16 17:18:42 +01002685 return -1;
2686 return 0;
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002687}
2688
Christopher Faulet3e964192018-10-24 11:39:23 +02002689/* Executes the http-request rules <rules> for stream <s>, proxy <px> and
2690 * transaction <txn>. Returns the verdict of the first rule that prevents
2691 * further processing of the request (auth, deny, ...), and defaults to
2692 * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or
2693 * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT
2694 * on txn->flags if it encounters a tarpit rule. If <deny_status> is not NULL
2695 * and a deny/tarpit rule is matched, it will be filled with this rule's deny
2696 * status.
2697 */
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002698static enum rule_result http_req_get_intercept_rule(struct proxy *px, struct list *def_rules,
2699 struct list *rules, struct stream *s)
Christopher Faulet3e964192018-10-24 11:39:23 +02002700{
2701 struct session *sess = strm_sess(s);
2702 struct http_txn *txn = s->txn;
Christopher Faulet3e964192018-10-24 11:39:23 +02002703 struct act_rule *rule;
Christopher Faulet3e964192018-10-24 11:39:23 +02002704 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002705 int act_opts = 0;
Christopher Faulet3e964192018-10-24 11:39:23 +02002706
Christopher Faulet3e964192018-10-24 11:39:23 +02002707 /* If "the current_rule_list" match the executed rule list, we are in
2708 * resume condition. If a resume is needed it is always in the action
2709 * and never in the ACL or converters. In this case, we initialise the
2710 * current rule, and go to the action execution point.
2711 */
2712 if (s->current_rule) {
2713 rule = s->current_rule;
2714 s->current_rule = NULL;
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002715 if (s->current_rule_list == rules || (def_rules && s->current_rule_list == def_rules))
Christopher Faulet3e964192018-10-24 11:39:23 +02002716 goto resume_execution;
2717 }
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002718 s->current_rule_list = ((!def_rules || s->current_rule_list == def_rules) ? rules : def_rules);
Christopher Faulet3e964192018-10-24 11:39:23 +02002719
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002720 restart:
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002721 /* start the ruleset evaluation in strict mode */
2722 txn->req.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01002723
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002724 list_for_each_entry(rule, s->current_rule_list, list) {
Christopher Faulet3e964192018-10-24 11:39:23 +02002725 /* check optional condition */
2726 if (rule->cond) {
2727 int ret;
2728
2729 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
2730 ret = acl_pass(ret);
2731
2732 if (rule->cond->pol == ACL_COND_UNLESS)
2733 ret = !ret;
2734
2735 if (!ret) /* condition not matched */
2736 continue;
2737 }
2738
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002739 act_opts |= ACT_OPT_FIRST;
Christopher Faulet3e964192018-10-24 11:39:23 +02002740 resume_execution:
Amaury Denoyelle03517732021-05-07 14:25:01 +02002741 if (rule->kw->flags & KWF_EXPERIMENTAL)
2742 mark_tainted(TAINTED_ACTION_EXP_EXECUTED);
2743
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002744 /* Always call the action function if defined */
2745 if (rule->action_ptr) {
2746 if ((s->req.flags & CF_READ_ERROR) ||
2747 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
2748 (px->options & PR_O_ABRT_CLOSE)))
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002749 act_opts |= ACT_OPT_FINAL;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002750
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002751 switch (rule->action_ptr(rule, px, sess, s, act_opts)) {
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002752 case ACT_RET_CONT:
2753 break;
2754 case ACT_RET_STOP:
2755 rule_ret = HTTP_RULE_RES_STOP;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002756 s->last_rule_file = rule->conf.file;
2757 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002758 goto end;
2759 case ACT_RET_YIELD:
2760 s->current_rule = rule;
2761 rule_ret = HTTP_RULE_RES_YIELD;
2762 goto end;
2763 case ACT_RET_ERR:
2764 rule_ret = HTTP_RULE_RES_ERROR;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002765 s->last_rule_file = rule->conf.file;
2766 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002767 goto end;
2768 case ACT_RET_DONE:
2769 rule_ret = HTTP_RULE_RES_DONE;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002770 s->last_rule_file = rule->conf.file;
2771 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002772 goto end;
2773 case ACT_RET_DENY:
Christopher Fauletb58f62b2020-01-13 16:40:13 +01002774 if (txn->status == -1)
2775 txn->status = 403;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002776 rule_ret = HTTP_RULE_RES_DENY;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002777 s->last_rule_file = rule->conf.file;
2778 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002779 goto end;
2780 case ACT_RET_ABRT:
2781 rule_ret = HTTP_RULE_RES_ABRT;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002782 s->last_rule_file = rule->conf.file;
2783 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002784 goto end;
2785 case ACT_RET_INV:
2786 rule_ret = HTTP_RULE_RES_BADREQ;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002787 s->last_rule_file = rule->conf.file;
2788 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002789 goto end;
2790 }
2791 continue; /* eval the next rule */
2792 }
2793
2794 /* If not action function defined, check for known actions */
Christopher Faulet3e964192018-10-24 11:39:23 +02002795 switch (rule->action) {
2796 case ACT_ACTION_ALLOW:
2797 rule_ret = HTTP_RULE_RES_STOP;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002798 s->last_rule_file = rule->conf.file;
2799 s->last_rule_line = rule->conf.line;
Christopher Faulet3e964192018-10-24 11:39:23 +02002800 goto end;
2801
2802 case ACT_ACTION_DENY:
Christopher Faulet5cb513a2020-05-13 17:56:56 +02002803 txn->status = rule->arg.http_reply->status;
2804 txn->http_reply = rule->arg.http_reply;
Christopher Faulet3e964192018-10-24 11:39:23 +02002805 rule_ret = HTTP_RULE_RES_DENY;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002806 s->last_rule_file = rule->conf.file;
2807 s->last_rule_line = rule->conf.line;
Christopher Faulet3e964192018-10-24 11:39:23 +02002808 goto end;
2809
2810 case ACT_HTTP_REQ_TARPIT:
2811 txn->flags |= TX_CLTARPIT;
Christopher Faulet5cb513a2020-05-13 17:56:56 +02002812 txn->status = rule->arg.http_reply->status;
2813 txn->http_reply = rule->arg.http_reply;
Christopher Faulet3e964192018-10-24 11:39:23 +02002814 rule_ret = HTTP_RULE_RES_DENY;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002815 s->last_rule_file = rule->conf.file;
2816 s->last_rule_line = rule->conf.line;
Christopher Faulet3e964192018-10-24 11:39:23 +02002817 goto end;
2818
Willy Tarreaubc1223b2021-09-02 16:54:33 +02002819 case ACT_HTTP_REDIR: {
2820 int ret = http_apply_redirect_rule(rule->arg.redir, s, txn);
2821
2822 if (ret == 2) // 2 == skip
2823 break;
2824
2825 rule_ret = ret ? HTTP_RULE_RES_ABRT : HTTP_RULE_RES_ERROR;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002826 s->last_rule_file = rule->conf.file;
2827 s->last_rule_line = rule->conf.line;
Christopher Faulet3e964192018-10-24 11:39:23 +02002828 goto end;
Willy Tarreaubc1223b2021-09-02 16:54:33 +02002829 }
Christopher Faulet3e964192018-10-24 11:39:23 +02002830
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002831 /* other flags exists, but normally, they never be matched. */
Christopher Faulet3e964192018-10-24 11:39:23 +02002832 default:
2833 break;
2834 }
2835 }
2836
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002837 if (def_rules && s->current_rule_list == def_rules) {
2838 s->current_rule_list = rules;
2839 goto restart;
2840 }
2841
Christopher Faulet3e964192018-10-24 11:39:23 +02002842 end:
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002843 /* if the ruleset evaluation is finished reset the strict mode */
Christopher Faulet46f95542019-12-20 10:07:22 +01002844 if (rule_ret != HTTP_RULE_RES_YIELD)
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002845 txn->req.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01002846
Christopher Faulet3e964192018-10-24 11:39:23 +02002847 /* we reached the end of the rules, nothing to report */
2848 return rule_ret;
2849}
2850
2851/* Executes the http-response rules <rules> for stream <s> and proxy <px>. It
2852 * returns one of 5 possible statuses: HTTP_RULE_RES_CONT, HTTP_RULE_RES_STOP,
2853 * HTTP_RULE_RES_DONE, HTTP_RULE_RES_YIELD, or HTTP_RULE_RES_BADREQ. If *CONT
2854 * is returned, the process can continue the evaluation of next rule list. If
2855 * *STOP or *DONE is returned, the process must stop the evaluation. If *BADREQ
2856 * is returned, it means the operation could not be processed and a server error
Christopher Fauleta53abad2020-05-13 08:12:22 +02002857 * must be returned. If *YIELD is returned, the caller must call again the
2858 * function with the same context.
Christopher Faulet3e964192018-10-24 11:39:23 +02002859 */
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002860static enum rule_result http_res_get_intercept_rule(struct proxy *px, struct list *def_rules,
2861 struct list *rules, struct stream *s)
Christopher Faulet3e964192018-10-24 11:39:23 +02002862{
2863 struct session *sess = strm_sess(s);
2864 struct http_txn *txn = s->txn;
Christopher Faulet3e964192018-10-24 11:39:23 +02002865 struct act_rule *rule;
Christopher Faulet3e964192018-10-24 11:39:23 +02002866 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002867 int act_opts = 0;
Christopher Faulet3e964192018-10-24 11:39:23 +02002868
Christopher Faulet3e964192018-10-24 11:39:23 +02002869 /* If "the current_rule_list" match the executed rule list, we are in
2870 * resume condition. If a resume is needed it is always in the action
2871 * and never in the ACL or converters. In this case, we initialise the
2872 * current rule, and go to the action execution point.
2873 */
2874 if (s->current_rule) {
2875 rule = s->current_rule;
2876 s->current_rule = NULL;
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002877 if (s->current_rule_list == rules || (def_rules && s->current_rule_list == def_rules))
Christopher Faulet3e964192018-10-24 11:39:23 +02002878 goto resume_execution;
2879 }
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002880 s->current_rule_list = ((!def_rules || s->current_rule_list == def_rules) ? rules : def_rules);
2881
2882 restart:
Christopher Faulet3e964192018-10-24 11:39:23 +02002883
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002884 /* start the ruleset evaluation in strict mode */
2885 txn->rsp.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01002886
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002887 list_for_each_entry(rule, s->current_rule_list, list) {
Christopher Faulet3e964192018-10-24 11:39:23 +02002888 /* check optional condition */
2889 if (rule->cond) {
2890 int ret;
2891
2892 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
2893 ret = acl_pass(ret);
2894
2895 if (rule->cond->pol == ACL_COND_UNLESS)
2896 ret = !ret;
2897
2898 if (!ret) /* condition not matched */
2899 continue;
2900 }
2901
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002902 act_opts |= ACT_OPT_FIRST;
Christopher Faulet3e964192018-10-24 11:39:23 +02002903resume_execution:
Amaury Denoyelle03517732021-05-07 14:25:01 +02002904 if (rule->kw->flags & KWF_EXPERIMENTAL)
2905 mark_tainted(TAINTED_ACTION_EXP_EXECUTED);
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002906
2907 /* Always call the action function if defined */
2908 if (rule->action_ptr) {
2909 if ((s->req.flags & CF_READ_ERROR) ||
2910 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
2911 (px->options & PR_O_ABRT_CLOSE)))
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002912 act_opts |= ACT_OPT_FINAL;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002913
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002914 switch (rule->action_ptr(rule, px, sess, s, act_opts)) {
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002915 case ACT_RET_CONT:
2916 break;
2917 case ACT_RET_STOP:
2918 rule_ret = HTTP_RULE_RES_STOP;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002919 s->last_rule_file = rule->conf.file;
2920 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002921 goto end;
2922 case ACT_RET_YIELD:
2923 s->current_rule = rule;
2924 rule_ret = HTTP_RULE_RES_YIELD;
2925 goto end;
2926 case ACT_RET_ERR:
2927 rule_ret = HTTP_RULE_RES_ERROR;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002928 s->last_rule_file = rule->conf.file;
2929 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002930 goto end;
2931 case ACT_RET_DONE:
2932 rule_ret = HTTP_RULE_RES_DONE;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002933 s->last_rule_file = rule->conf.file;
2934 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002935 goto end;
2936 case ACT_RET_DENY:
Christopher Fauletb58f62b2020-01-13 16:40:13 +01002937 if (txn->status == -1)
2938 txn->status = 502;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002939 rule_ret = HTTP_RULE_RES_DENY;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002940 s->last_rule_file = rule->conf.file;
2941 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002942 goto end;
2943 case ACT_RET_ABRT:
2944 rule_ret = HTTP_RULE_RES_ABRT;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002945 s->last_rule_file = rule->conf.file;
2946 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002947 goto end;
2948 case ACT_RET_INV:
2949 rule_ret = HTTP_RULE_RES_BADREQ;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002950 s->last_rule_file = rule->conf.file;
2951 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002952 goto end;
2953 }
2954 continue; /* eval the next rule */
2955 }
2956
2957 /* If not action function defined, check for known actions */
Christopher Faulet3e964192018-10-24 11:39:23 +02002958 switch (rule->action) {
2959 case ACT_ACTION_ALLOW:
2960 rule_ret = HTTP_RULE_RES_STOP; /* "allow" rules are OK */
Willy Tarreauc6dae862022-03-09 17:23:10 +01002961 s->last_rule_file = rule->conf.file;
2962 s->last_rule_line = rule->conf.line;
Christopher Faulet3e964192018-10-24 11:39:23 +02002963 goto end;
2964
2965 case ACT_ACTION_DENY:
Christopher Faulet5cb513a2020-05-13 17:56:56 +02002966 txn->status = rule->arg.http_reply->status;
2967 txn->http_reply = rule->arg.http_reply;
Christopher Faulet3a26bee2019-12-16 12:47:40 +01002968 rule_ret = HTTP_RULE_RES_DENY;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002969 s->last_rule_file = rule->conf.file;
2970 s->last_rule_line = rule->conf.line;
Christopher Faulet3e964192018-10-24 11:39:23 +02002971 goto end;
2972
Willy Tarreaubc1223b2021-09-02 16:54:33 +02002973 case ACT_HTTP_REDIR: {
2974 int ret = http_apply_redirect_rule(rule->arg.redir, s, txn);
Christopher Faulet3e964192018-10-24 11:39:23 +02002975
Willy Tarreaubc1223b2021-09-02 16:54:33 +02002976 if (ret == 2) // 2 == skip
2977 break;
2978
2979 rule_ret = ret ? HTTP_RULE_RES_ABRT : HTTP_RULE_RES_ERROR;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002980 s->last_rule_file = rule->conf.file;
2981 s->last_rule_line = rule->conf.line;
Willy Tarreaubc1223b2021-09-02 16:54:33 +02002982 goto end;
2983 }
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002984 /* other flags exists, but normally, they never be matched. */
Christopher Faulet3e964192018-10-24 11:39:23 +02002985 default:
2986 break;
2987 }
2988 }
2989
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002990 if (def_rules && s->current_rule_list == def_rules) {
2991 s->current_rule_list = rules;
2992 goto restart;
2993 }
2994
Christopher Faulet3e964192018-10-24 11:39:23 +02002995 end:
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002996 /* if the ruleset evaluation is finished reset the strict mode */
Christopher Faulet46f95542019-12-20 10:07:22 +01002997 if (rule_ret != HTTP_RULE_RES_YIELD)
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002998 txn->rsp.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01002999
Christopher Faulet3e964192018-10-24 11:39:23 +02003000 /* we reached the end of the rules, nothing to report */
3001 return rule_ret;
3002}
3003
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01003004/* Executes backend and frontend http-after-response rules for the stream <s>,
3005 * in that order. it return 1 on success and 0 on error. It is the caller
3006 * responsibility to catch error or ignore it. If it catches it, this function
3007 * may be called a second time, for the internal error.
3008 */
3009int http_eval_after_res_rules(struct stream *s)
3010{
Christopher Fauletd4150ad2021-10-13 15:35:55 +02003011 struct list *def_rules, *rules;
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01003012 struct session *sess = s->sess;
3013 enum rule_result ret = HTTP_RULE_RES_CONT;
3014
Christopher Faulet507479b2020-05-15 12:29:46 +02003015 /* Eval after-response ruleset only if the reply is not const */
3016 if (s->txn->flags & TX_CONST_REPLY)
3017 goto end;
3018
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01003019 /* prune the request variables if not already done and swap to the response variables. */
3020 if (s->vars_reqres.scope != SCOPE_RES) {
3021 if (!LIST_ISEMPTY(&s->vars_reqres.head))
3022 vars_prune(&s->vars_reqres, s->sess, s);
Willy Tarreaub7bfcb32021-08-31 08:13:25 +02003023 vars_init_head(&s->vars_reqres, SCOPE_RES);
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01003024 }
3025
Christopher Fauletd4150ad2021-10-13 15:35:55 +02003026 def_rules = (s->be->defpx ? &s->be->defpx->http_after_res_rules : NULL);
3027 rules = &s->be->http_after_res_rules;
3028
3029 ret = http_res_get_intercept_rule(s->be, def_rules, rules, s);
Christopher Faulet4c5a5912021-11-09 17:48:39 +01003030 if ((ret == HTTP_RULE_RES_CONT || ret == HTTP_RULE_RES_STOP) && sess->fe != s->be) {
Christopher Fauletd4150ad2021-10-13 15:35:55 +02003031 def_rules = ((sess->fe->defpx && sess->fe->defpx != s->be->defpx) ? &sess->fe->defpx->http_after_res_rules : NULL);
3032 rules = &sess->fe->http_after_res_rules;
3033 ret = http_res_get_intercept_rule(sess->fe, def_rules, rules, s);
3034 }
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01003035
Christopher Faulet507479b2020-05-15 12:29:46 +02003036 end:
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01003037 /* All other codes than CONTINUE, STOP or DONE are forbidden */
3038 return (ret == HTTP_RULE_RES_CONT || ret == HTTP_RULE_RES_STOP || ret == HTTP_RULE_RES_DONE);
3039}
3040
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003041/*
3042 * Manage client-side cookie. It can impact performance by about 2% so it is
3043 * desirable to call it only when needed. This code is quite complex because
3044 * of the multiple very crappy and ambiguous syntaxes we have to support. it
3045 * highly recommended not to touch this part without a good reason !
3046 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003047static void http_manage_client_side_cookies(struct stream *s, struct channel *req)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003048{
3049 struct session *sess = s->sess;
3050 struct http_txn *txn = s->txn;
3051 struct htx *htx;
3052 struct http_hdr_ctx ctx;
3053 char *hdr_beg, *hdr_end, *del_from;
3054 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
3055 int preserve_hdr;
3056
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003057 htx = htxbuf(&req->buf);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003058 ctx.blk = NULL;
3059 while (http_find_header(htx, ist("Cookie"), &ctx, 1)) {
Olivier Houchardf0f42382019-07-22 17:43:46 +02003060 int is_first = 1;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003061 del_from = NULL; /* nothing to be deleted */
3062 preserve_hdr = 0; /* assume we may kill the whole header */
3063
3064 /* Now look for cookies. Conforming to RFC2109, we have to support
3065 * attributes whose name begin with a '$', and associate them with
3066 * the right cookie, if we want to delete this cookie.
3067 * So there are 3 cases for each cookie read :
3068 * 1) it's a special attribute, beginning with a '$' : ignore it.
3069 * 2) it's a server id cookie that we *MAY* want to delete : save
3070 * some pointers on it (last semi-colon, beginning of cookie...)
3071 * 3) it's an application cookie : we *MAY* have to delete a previous
3072 * "special" cookie.
3073 * At the end of loop, if a "special" cookie remains, we may have to
3074 * remove it. If no application cookie persists in the header, we
3075 * *MUST* delete it.
3076 *
3077 * Note: RFC2965 is unclear about the processing of spaces around
3078 * the equal sign in the ATTR=VALUE form. A careful inspection of
3079 * the RFC explicitly allows spaces before it, and not within the
3080 * tokens (attrs or values). An inspection of RFC2109 allows that
3081 * too but section 10.1.3 lets one think that spaces may be allowed
3082 * after the equal sign too, resulting in some (rare) buggy
3083 * implementations trying to do that. So let's do what servers do.
3084 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
3085 * allowed quoted strings in values, with any possible character
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003086 * after a backslash, including control chars and delimiters, which
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003087 * causes parsing to become ambiguous. Browsers also allow spaces
3088 * within values even without quotes.
3089 *
3090 * We have to keep multiple pointers in order to support cookie
3091 * removal at the beginning, middle or end of header without
3092 * corrupting the header. All of these headers are valid :
3093 *
3094 * hdr_beg hdr_end
3095 * | |
3096 * v |
3097 * NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3 |
3098 * NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3 v
3099 * NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3
3100 * | | | | | | |
3101 * | | | | | | |
3102 * | | | | | | +--> next
3103 * | | | | | +----> val_end
3104 * | | | | +-----------> val_beg
3105 * | | | +--------------> equal
3106 * | | +----------------> att_end
3107 * | +---------------------> att_beg
3108 * +--------------------------> prev
3109 *
3110 */
3111 hdr_beg = ctx.value.ptr;
3112 hdr_end = hdr_beg + ctx.value.len;
3113 for (prev = hdr_beg; prev < hdr_end; prev = next) {
3114 /* Iterate through all cookies on this line */
3115
3116 /* find att_beg */
3117 att_beg = prev;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003118 if (!is_first)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003119 att_beg++;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003120 is_first = 0;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003121
3122 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
3123 att_beg++;
3124
3125 /* find att_end : this is the first character after the last non
3126 * space before the equal. It may be equal to hdr_end.
3127 */
3128 equal = att_end = att_beg;
3129 while (equal < hdr_end) {
3130 if (*equal == '=' || *equal == ',' || *equal == ';')
3131 break;
3132 if (HTTP_IS_SPHT(*equal++))
3133 continue;
3134 att_end = equal;
3135 }
3136
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003137 /* here, <equal> points to '=', a delimiter or the end. <att_end>
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003138 * is between <att_beg> and <equal>, both may be identical.
3139 */
3140 /* look for end of cookie if there is an equal sign */
3141 if (equal < hdr_end && *equal == '=') {
3142 /* look for the beginning of the value */
3143 val_beg = equal + 1;
3144 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
3145 val_beg++;
3146
3147 /* find the end of the value, respecting quotes */
3148 next = http_find_cookie_value_end(val_beg, hdr_end);
3149
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003150 /* make val_end point to the first white space or delimiter after the value */
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003151 val_end = next;
3152 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
3153 val_end--;
3154 }
3155 else
3156 val_beg = val_end = next = equal;
3157
3158 /* We have nothing to do with attributes beginning with
3159 * '$'. However, they will automatically be removed if a
3160 * header before them is removed, since they're supposed
3161 * to be linked together.
3162 */
3163 if (*att_beg == '$')
3164 continue;
3165
3166 /* Ignore cookies with no equal sign */
3167 if (equal == next) {
3168 /* This is not our cookie, so we must preserve it. But if we already
3169 * scheduled another cookie for removal, we cannot remove the
3170 * complete header, but we can remove the previous block itself.
3171 */
3172 preserve_hdr = 1;
3173 if (del_from != NULL) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003174 int delta = http_del_hdr_value(hdr_beg, hdr_end, &del_from, prev);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003175 val_end += delta;
3176 next += delta;
3177 hdr_end += delta;
3178 prev = del_from;
3179 del_from = NULL;
3180 }
3181 continue;
3182 }
3183
3184 /* if there are spaces around the equal sign, we need to
3185 * strip them otherwise we'll get trouble for cookie captures,
3186 * or even for rewrites. Since this happens extremely rarely,
3187 * it does not hurt performance.
3188 */
3189 if (unlikely(att_end != equal || val_beg > equal + 1)) {
3190 int stripped_before = 0;
3191 int stripped_after = 0;
3192
3193 if (att_end != equal) {
3194 memmove(att_end, equal, hdr_end - equal);
3195 stripped_before = (att_end - equal);
3196 equal += stripped_before;
3197 val_beg += stripped_before;
3198 }
3199
3200 if (val_beg > equal + 1) {
3201 memmove(equal + 1, val_beg, hdr_end + stripped_before - val_beg);
3202 stripped_after = (equal + 1) - val_beg;
3203 val_beg += stripped_after;
3204 stripped_before += stripped_after;
3205 }
3206
3207 val_end += stripped_before;
3208 next += stripped_before;
3209 hdr_end += stripped_before;
3210 }
3211 /* now everything is as on the diagram above */
3212
3213 /* First, let's see if we want to capture this cookie. We check
3214 * that we don't already have a client side cookie, because we
3215 * can only capture one. Also as an optimisation, we ignore
3216 * cookies shorter than the declared name.
3217 */
3218 if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL &&
3219 (val_end - att_beg >= sess->fe->capture_namelen) &&
3220 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
3221 int log_len = val_end - att_beg;
3222
3223 if ((txn->cli_cookie = pool_alloc(pool_head_capture)) == NULL) {
3224 ha_alert("HTTP logging : out of memory.\n");
3225 } else {
3226 if (log_len > sess->fe->capture_len)
3227 log_len = sess->fe->capture_len;
3228 memcpy(txn->cli_cookie, att_beg, log_len);
3229 txn->cli_cookie[log_len] = 0;
3230 }
3231 }
3232
3233 /* Persistence cookies in passive, rewrite or insert mode have the
3234 * following form :
3235 *
3236 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
3237 *
3238 * For cookies in prefix mode, the form is :
3239 *
3240 * Cookie: NAME=SRV~VALUE
3241 */
3242 if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
3243 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
3244 struct server *srv = s->be->srv;
3245 char *delim;
3246
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003247 /* if we're in cookie prefix mode, we'll search the delimiter so that we
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003248 * have the server ID between val_beg and delim, and the original cookie between
3249 * delim+1 and val_end. Otherwise, delim==val_end :
3250 *
3251 * hdr_beg
3252 * |
3253 * v
3254 * NAME=SRV; # in all but prefix modes
3255 * NAME=SRV~OPAQUE ; # in prefix mode
3256 * || || | |+-> next
3257 * || || | +--> val_end
3258 * || || +---------> delim
3259 * || |+------------> val_beg
3260 * || +-------------> att_end = equal
3261 * |+-----------------> att_beg
3262 * +------------------> prev
3263 *
3264 */
3265 if (s->be->ck_opts & PR_CK_PFX) {
3266 for (delim = val_beg; delim < val_end; delim++)
3267 if (*delim == COOKIE_DELIM)
3268 break;
3269 }
3270 else {
3271 char *vbar1;
3272 delim = val_end;
3273 /* Now check if the cookie contains a date field, which would
3274 * appear after a vertical bar ('|') just after the server name
3275 * and before the delimiter.
3276 */
3277 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
3278 if (vbar1) {
3279 /* OK, so left of the bar is the server's cookie and
3280 * right is the last seen date. It is a base64 encoded
3281 * 30-bit value representing the UNIX date since the
3282 * epoch in 4-second quantities.
3283 */
3284 int val;
3285 delim = vbar1++;
3286 if (val_end - vbar1 >= 5) {
3287 val = b64tos30(vbar1);
3288 if (val > 0)
3289 txn->cookie_last_date = val << 2;
3290 }
3291 /* look for a second vertical bar */
3292 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
3293 if (vbar1 && (val_end - vbar1 > 5)) {
3294 val = b64tos30(vbar1 + 1);
3295 if (val > 0)
3296 txn->cookie_first_date = val << 2;
3297 }
3298 }
3299 }
3300
3301 /* if the cookie has an expiration date and the proxy wants to check
3302 * it, then we do that now. We first check if the cookie is too old,
3303 * then only if it has expired. We detect strict overflow because the
3304 * time resolution here is not great (4 seconds). Cookies with dates
3305 * in the future are ignored if their offset is beyond one day. This
3306 * allows an admin to fix timezone issues without expiring everyone
3307 * and at the same time avoids keeping unwanted side effects for too
3308 * long.
3309 */
3310 if (txn->cookie_first_date && s->be->cookie_maxlife &&
3311 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) ||
3312 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
3313 txn->flags &= ~TX_CK_MASK;
3314 txn->flags |= TX_CK_OLD;
3315 delim = val_beg; // let's pretend we have not found the cookie
3316 txn->cookie_first_date = 0;
3317 txn->cookie_last_date = 0;
3318 }
3319 else if (txn->cookie_last_date && s->be->cookie_maxidle &&
3320 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) ||
3321 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
3322 txn->flags &= ~TX_CK_MASK;
3323 txn->flags |= TX_CK_EXPIRED;
3324 delim = val_beg; // let's pretend we have not found the cookie
3325 txn->cookie_first_date = 0;
3326 txn->cookie_last_date = 0;
3327 }
3328
3329 /* Here, we'll look for the first running server which supports the cookie.
3330 * This allows to share a same cookie between several servers, for example
3331 * to dedicate backup servers to specific servers only.
3332 * However, to prevent clients from sticking to cookie-less backup server
3333 * when they have incidentely learned an empty cookie, we simply ignore
3334 * empty cookies and mark them as invalid.
3335 * The same behaviour is applied when persistence must be ignored.
3336 */
3337 if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
3338 srv = NULL;
3339
3340 while (srv) {
3341 if (srv->cookie && (srv->cklen == delim - val_beg) &&
3342 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
3343 if ((srv->cur_state != SRV_ST_STOPPED) ||
3344 (s->be->options & PR_O_PERSIST) ||
3345 (s->flags & SF_FORCE_PRST)) {
3346 /* we found the server and we can use it */
3347 txn->flags &= ~TX_CK_MASK;
3348 txn->flags |= (srv->cur_state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
3349 s->flags |= SF_DIRECT | SF_ASSIGNED;
3350 s->target = &srv->obj_type;
3351 break;
3352 } else {
3353 /* we found a server, but it's down,
3354 * mark it as such and go on in case
3355 * another one is available.
3356 */
3357 txn->flags &= ~TX_CK_MASK;
3358 txn->flags |= TX_CK_DOWN;
3359 }
3360 }
3361 srv = srv->next;
3362 }
3363
3364 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
3365 /* no server matched this cookie or we deliberately skipped it */
3366 txn->flags &= ~TX_CK_MASK;
3367 if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
3368 txn->flags |= TX_CK_UNUSED;
3369 else
3370 txn->flags |= TX_CK_INVALID;
3371 }
3372
3373 /* depending on the cookie mode, we may have to either :
3374 * - delete the complete cookie if we're in insert+indirect mode, so that
3375 * the server never sees it ;
3376 * - remove the server id from the cookie value, and tag the cookie as an
Joseph Herlante9d5c722018-11-25 11:00:25 -08003377 * application cookie so that it does not get accidentally removed later,
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003378 * if we're in cookie prefix mode
3379 */
3380 if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
3381 int delta; /* negative */
3382
3383 memmove(val_beg, delim + 1, hdr_end - (delim + 1));
3384 delta = val_beg - (delim + 1);
3385 val_end += delta;
3386 next += delta;
3387 hdr_end += delta;
3388 del_from = NULL;
3389 preserve_hdr = 1; /* we want to keep this cookie */
3390 }
3391 else if (del_from == NULL &&
3392 (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
3393 del_from = prev;
3394 }
3395 }
3396 else {
3397 /* This is not our cookie, so we must preserve it. But if we already
3398 * scheduled another cookie for removal, we cannot remove the
3399 * complete header, but we can remove the previous block itself.
3400 */
3401 preserve_hdr = 1;
3402
3403 if (del_from != NULL) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003404 int delta = http_del_hdr_value(hdr_beg, hdr_end, &del_from, prev);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003405 if (att_beg >= del_from)
3406 att_beg += delta;
3407 if (att_end >= del_from)
3408 att_end += delta;
3409 val_beg += delta;
3410 val_end += delta;
3411 next += delta;
3412 hdr_end += delta;
3413 prev = del_from;
3414 del_from = NULL;
3415 }
3416 }
3417
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003418 } /* for each cookie */
3419
3420
3421 /* There are no more cookies on this line.
3422 * We may still have one (or several) marked for deletion at the
3423 * end of the line. We must do this now in two ways :
3424 * - if some cookies must be preserved, we only delete from the
3425 * mark to the end of line ;
3426 * - if nothing needs to be preserved, simply delete the whole header
3427 */
3428 if (del_from) {
3429 hdr_end = (preserve_hdr ? del_from : hdr_beg);
3430 }
3431 if ((hdr_end - hdr_beg) != ctx.value.len) {
Christopher Faulet3e2638e2019-06-18 09:49:16 +02003432 if (hdr_beg != hdr_end)
3433 htx_change_blk_value_len(htx, ctx.blk, hdr_end - hdr_beg);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003434 else
3435 http_remove_header(htx, &ctx);
3436 }
3437 } /* for each "Cookie header */
3438}
3439
3440/*
3441 * Manage server-side cookies. It can impact performance by about 2% so it is
3442 * desirable to call it only when needed. This function is also used when we
3443 * just need to know if there is a cookie (eg: for check-cache).
3444 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003445static void http_manage_server_side_cookies(struct stream *s, struct channel *res)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003446{
3447 struct session *sess = s->sess;
3448 struct http_txn *txn = s->txn;
3449 struct htx *htx;
3450 struct http_hdr_ctx ctx;
3451 struct server *srv;
3452 char *hdr_beg, *hdr_end;
3453 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau6f7a02a2019-04-15 21:49:49 +02003454 int is_cookie2 = 0;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003455
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003456 htx = htxbuf(&res->buf);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003457
3458 ctx.blk = NULL;
3459 while (1) {
Olivier Houchardf0f42382019-07-22 17:43:46 +02003460 int is_first = 1;
3461
Andrew McDermottbfb15ab2022-02-11 18:26:49 +00003462 if (is_cookie2 || !http_find_header(htx, ist("Set-Cookie"), &ctx, 1)) {
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003463 if (!http_find_header(htx, ist("Set-Cookie2"), &ctx, 1))
3464 break;
3465 is_cookie2 = 1;
3466 }
3467
3468 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
3469 * <prev> points to the colon.
3470 */
3471 txn->flags |= TX_SCK_PRESENT;
3472
3473 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
3474 * check-cache is enabled) and we are not interested in checking
3475 * them. Warning, the cookie capture is declared in the frontend.
3476 */
3477 if (s->be->cookie_name == NULL && sess->fe->capture_name == NULL)
3478 break;
3479
3480 /* OK so now we know we have to process this response cookie.
3481 * The format of the Set-Cookie header is slightly different
3482 * from the format of the Cookie header in that it does not
3483 * support the comma as a cookie delimiter (thus the header
3484 * cannot be folded) because the Expires attribute described in
3485 * the original Netscape's spec may contain an unquoted date
3486 * with a comma inside. We have to live with this because
3487 * many browsers don't support Max-Age and some browsers don't
3488 * support quoted strings. However the Set-Cookie2 header is
3489 * clean.
3490 *
3491 * We have to keep multiple pointers in order to support cookie
3492 * removal at the beginning, middle or end of header without
3493 * corrupting the header (in case of set-cookie2). A special
3494 * pointer, <scav> points to the beginning of the set-cookie-av
3495 * fields after the first semi-colon. The <next> pointer points
3496 * either to the end of line (set-cookie) or next unquoted comma
3497 * (set-cookie2). All of these headers are valid :
3498 *
3499 * hdr_beg hdr_end
3500 * | |
3501 * v |
3502 * NAME1 = VALUE 1 ; Secure; Path="/" |
3503 * NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT v
3504 * NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT
3505 * NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard
3506 * | | | | | | | |
3507 * | | | | | | | +-> next
3508 * | | | | | | +------------> scav
3509 * | | | | | +--------------> val_end
3510 * | | | | +--------------------> val_beg
3511 * | | | +----------------------> equal
3512 * | | +------------------------> att_end
3513 * | +----------------------------> att_beg
3514 * +------------------------------> prev
3515 * -------------------------------> hdr_beg
3516 */
3517 hdr_beg = ctx.value.ptr;
3518 hdr_end = hdr_beg + ctx.value.len;
3519 for (prev = hdr_beg; prev < hdr_end; prev = next) {
3520
3521 /* Iterate through all cookies on this line */
3522
3523 /* find att_beg */
3524 att_beg = prev;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003525 if (!is_first)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003526 att_beg++;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003527 is_first = 0;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003528
3529 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
3530 att_beg++;
3531
3532 /* find att_end : this is the first character after the last non
3533 * space before the equal. It may be equal to hdr_end.
3534 */
3535 equal = att_end = att_beg;
3536
3537 while (equal < hdr_end) {
3538 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
3539 break;
3540 if (HTTP_IS_SPHT(*equal++))
3541 continue;
3542 att_end = equal;
3543 }
3544
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003545 /* here, <equal> points to '=', a delimiter or the end. <att_end>
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003546 * is between <att_beg> and <equal>, both may be identical.
3547 */
3548
3549 /* look for end of cookie if there is an equal sign */
3550 if (equal < hdr_end && *equal == '=') {
3551 /* look for the beginning of the value */
3552 val_beg = equal + 1;
3553 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
3554 val_beg++;
3555
3556 /* find the end of the value, respecting quotes */
3557 next = http_find_cookie_value_end(val_beg, hdr_end);
3558
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003559 /* make val_end point to the first white space or delimiter after the value */
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003560 val_end = next;
3561 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
3562 val_end--;
3563 }
3564 else {
3565 /* <equal> points to next comma, semi-colon or EOL */
3566 val_beg = val_end = next = equal;
3567 }
3568
3569 if (next < hdr_end) {
3570 /* Set-Cookie2 supports multiple cookies, and <next> points to
3571 * a colon or semi-colon before the end. So skip all attr-value
3572 * pairs and look for the next comma. For Set-Cookie, since
3573 * commas are permitted in values, skip to the end.
3574 */
3575 if (is_cookie2)
3576 next = http_find_hdr_value_end(next, hdr_end);
3577 else
3578 next = hdr_end;
3579 }
3580
3581 /* Now everything is as on the diagram above */
3582
3583 /* Ignore cookies with no equal sign */
3584 if (equal == val_end)
3585 continue;
3586
3587 /* If there are spaces around the equal sign, we need to
3588 * strip them otherwise we'll get trouble for cookie captures,
3589 * or even for rewrites. Since this happens extremely rarely,
3590 * it does not hurt performance.
3591 */
3592 if (unlikely(att_end != equal || val_beg > equal + 1)) {
3593 int stripped_before = 0;
3594 int stripped_after = 0;
3595
3596 if (att_end != equal) {
3597 memmove(att_end, equal, hdr_end - equal);
3598 stripped_before = (att_end - equal);
3599 equal += stripped_before;
3600 val_beg += stripped_before;
3601 }
3602
3603 if (val_beg > equal + 1) {
3604 memmove(equal + 1, val_beg, hdr_end + stripped_before - val_beg);
3605 stripped_after = (equal + 1) - val_beg;
3606 val_beg += stripped_after;
3607 stripped_before += stripped_after;
3608 }
3609
3610 val_end += stripped_before;
3611 next += stripped_before;
3612 hdr_end += stripped_before;
3613
Christopher Faulet3e2638e2019-06-18 09:49:16 +02003614 htx_change_blk_value_len(htx, ctx.blk, hdr_end - hdr_beg);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003615 ctx.value.len = hdr_end - hdr_beg;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003616 }
3617
3618 /* First, let's see if we want to capture this cookie. We check
3619 * that we don't already have a server side cookie, because we
3620 * can only capture one. Also as an optimisation, we ignore
3621 * cookies shorter than the declared name.
3622 */
3623 if (sess->fe->capture_name != NULL &&
3624 txn->srv_cookie == NULL &&
3625 (val_end - att_beg >= sess->fe->capture_namelen) &&
3626 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
3627 int log_len = val_end - att_beg;
3628 if ((txn->srv_cookie = pool_alloc(pool_head_capture)) == NULL) {
3629 ha_alert("HTTP logging : out of memory.\n");
3630 }
3631 else {
3632 if (log_len > sess->fe->capture_len)
3633 log_len = sess->fe->capture_len;
3634 memcpy(txn->srv_cookie, att_beg, log_len);
3635 txn->srv_cookie[log_len] = 0;
3636 }
3637 }
3638
3639 srv = objt_server(s->target);
3640 /* now check if we need to process it for persistence */
3641 if (!(s->flags & SF_IGNORE_PRST) &&
3642 (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
3643 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
3644 /* assume passive cookie by default */
3645 txn->flags &= ~TX_SCK_MASK;
3646 txn->flags |= TX_SCK_FOUND;
3647
3648 /* If the cookie is in insert mode on a known server, we'll delete
3649 * this occurrence because we'll insert another one later.
3650 * We'll delete it too if the "indirect" option is set and we're in
3651 * a direct access.
3652 */
3653 if (s->be->ck_opts & PR_CK_PSV) {
3654 /* The "preserve" flag was set, we don't want to touch the
3655 * server's cookie.
3656 */
3657 }
3658 else if ((srv && (s->be->ck_opts & PR_CK_INS)) ||
3659 ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) {
3660 /* this cookie must be deleted */
3661 if (prev == hdr_beg && next == hdr_end) {
3662 /* whole header */
3663 http_remove_header(htx, &ctx);
3664 /* note: while both invalid now, <next> and <hdr_end>
3665 * are still equal, so the for() will stop as expected.
3666 */
3667 } else {
3668 /* just remove the value */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003669 int delta = http_del_hdr_value(hdr_beg, hdr_end, &prev, next);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003670 next = prev;
3671 hdr_end += delta;
3672 }
3673 txn->flags &= ~TX_SCK_MASK;
3674 txn->flags |= TX_SCK_DELETED;
3675 /* and go on with next cookie */
3676 }
3677 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) {
3678 /* replace bytes val_beg->val_end with the cookie name associated
3679 * with this server since we know it.
3680 */
3681 int sliding, delta;
3682
3683 ctx.value = ist2(val_beg, val_end - val_beg);
3684 ctx.lws_before = ctx.lws_after = 0;
3685 http_replace_header_value(htx, &ctx, ist2(srv->cookie, srv->cklen));
3686 delta = srv->cklen - (val_end - val_beg);
3687 sliding = (ctx.value.ptr - val_beg);
3688 hdr_beg += sliding;
3689 val_beg += sliding;
3690 next += sliding + delta;
3691 hdr_end += sliding + delta;
3692
3693 txn->flags &= ~TX_SCK_MASK;
3694 txn->flags |= TX_SCK_REPLACED;
3695 }
3696 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) {
3697 /* insert the cookie name associated with this server
3698 * before existing cookie, and insert a delimiter between them..
3699 */
3700 int sliding, delta;
3701 ctx.value = ist2(val_beg, 0);
3702 ctx.lws_before = ctx.lws_after = 0;
3703 http_replace_header_value(htx, &ctx, ist2(srv->cookie, srv->cklen + 1));
3704 delta = srv->cklen + 1;
3705 sliding = (ctx.value.ptr - val_beg);
3706 hdr_beg += sliding;
3707 val_beg += sliding;
3708 next += sliding + delta;
3709 hdr_end += sliding + delta;
3710
3711 val_beg[srv->cklen] = COOKIE_DELIM;
3712 txn->flags &= ~TX_SCK_MASK;
3713 txn->flags |= TX_SCK_REPLACED;
3714 }
3715 }
3716 /* that's done for this cookie, check the next one on the same
3717 * line when next != hdr_end (only if is_cookie2).
3718 */
3719 }
3720 }
3721}
3722
Christopher Faulet25a02f62018-10-24 12:00:25 +02003723/*
3724 * Parses the Cache-Control and Pragma request header fields to determine if
3725 * the request may be served from the cache and/or if it is cacheable. Updates
3726 * s->txn->flags.
3727 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003728void http_check_request_for_cacheability(struct stream *s, struct channel *req)
Christopher Faulet25a02f62018-10-24 12:00:25 +02003729{
3730 struct http_txn *txn = s->txn;
3731 struct htx *htx;
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003732 struct http_hdr_ctx ctx = { .blk = NULL };
3733 int pragma_found, cc_found;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003734
3735 if ((txn->flags & (TX_CACHEABLE|TX_CACHE_IGNORE)) == TX_CACHE_IGNORE)
3736 return; /* nothing more to do here */
3737
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003738 htx = htxbuf(&req->buf);
Christopher Faulet25a02f62018-10-24 12:00:25 +02003739 pragma_found = cc_found = 0;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003740
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003741 /* Check "pragma" header for HTTP/1.0 compatibility. */
3742 if (http_find_header(htx, ist("pragma"), &ctx, 1)) {
3743 if (isteqi(ctx.value, ist("no-cache"))) {
3744 pragma_found = 1;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003745 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003746 }
Christopher Faulet25a02f62018-10-24 12:00:25 +02003747
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003748 ctx.blk = NULL;
3749 /* Don't use the cache and don't try to store if we found the
3750 * Authorization header */
3751 if (http_find_header(htx, ist("authorization"), &ctx, 1)) {
3752 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3753 txn->flags |= TX_CACHE_IGNORE;
3754 }
Christopher Faulet25a02f62018-10-24 12:00:25 +02003755
Christopher Faulet25a02f62018-10-24 12:00:25 +02003756
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003757 /* Look for "cache-control" header and iterate over all the values
3758 * until we find one that specifies that caching is possible or not. */
3759 ctx.blk = NULL;
3760 while (http_find_header(htx, ist("cache-control"), &ctx, 0)) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003761 cc_found = 1;
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003762 /* We don't check the values after max-age, max-stale nor min-fresh,
3763 * we simply don't use the cache when they're specified. */
3764 if (istmatchi(ctx.value, ist("max-age")) ||
3765 istmatchi(ctx.value, ist("no-cache")) ||
3766 istmatchi(ctx.value, ist("max-stale")) ||
3767 istmatchi(ctx.value, ist("min-fresh"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003768 txn->flags |= TX_CACHE_IGNORE;
3769 continue;
3770 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003771 if (istmatchi(ctx.value, ist("no-store"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003772 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3773 continue;
3774 }
3775 }
3776
3777 /* RFC7234#5.4:
3778 * When the Cache-Control header field is also present and
3779 * understood in a request, Pragma is ignored.
3780 * When the Cache-Control header field is not present in a
3781 * request, caches MUST consider the no-cache request
3782 * pragma-directive as having the same effect as if
3783 * "Cache-Control: no-cache" were present.
3784 */
3785 if (!cc_found && pragma_found)
3786 txn->flags |= TX_CACHE_IGNORE;
3787}
3788
3789/*
3790 * Check if response is cacheable or not. Updates s->txn->flags.
3791 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003792void http_check_response_for_cacheability(struct stream *s, struct channel *res)
Christopher Faulet25a02f62018-10-24 12:00:25 +02003793{
3794 struct http_txn *txn = s->txn;
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003795 struct http_hdr_ctx ctx = { .blk = NULL };
Christopher Faulet25a02f62018-10-24 12:00:25 +02003796 struct htx *htx;
Remi Tricot-Le Bretoncc9bf2e2020-11-12 11:14:41 +01003797 int has_freshness_info = 0;
3798 int has_validator = 0;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003799
3800 if (txn->status < 200) {
3801 /* do not try to cache interim responses! */
3802 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3803 return;
3804 }
3805
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003806 htx = htxbuf(&res->buf);
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003807 /* Check "pragma" header for HTTP/1.0 compatibility. */
3808 if (http_find_header(htx, ist("pragma"), &ctx, 1)) {
3809 if (isteqi(ctx.value, ist("no-cache"))) {
3810 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3811 return;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003812 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003813 }
Christopher Faulet25a02f62018-10-24 12:00:25 +02003814
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003815 /* Look for "cache-control" header and iterate over all the values
3816 * until we find one that specifies that caching is possible or not. */
3817 ctx.blk = NULL;
3818 while (http_find_header(htx, ist("cache-control"), &ctx, 0)) {
3819 if (isteqi(ctx.value, ist("public"))) {
3820 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003821 continue;
3822 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003823 if (isteqi(ctx.value, ist("private")) ||
3824 isteqi(ctx.value, ist("no-cache")) ||
3825 isteqi(ctx.value, ist("no-store")) ||
3826 isteqi(ctx.value, ist("max-age=0")) ||
3827 isteqi(ctx.value, ist("s-maxage=0"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003828 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003829 continue;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003830 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003831 /* We might have a no-cache="set-cookie" form. */
3832 if (istmatchi(ctx.value, ist("no-cache=\"set-cookie"))) {
3833 txn->flags &= ~TX_CACHE_COOK;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003834 continue;
3835 }
Remi Tricot-Le Bretoncc9bf2e2020-11-12 11:14:41 +01003836
3837 if (istmatchi(ctx.value, ist("s-maxage")) ||
3838 istmatchi(ctx.value, ist("max-age"))) {
3839 has_freshness_info = 1;
3840 continue;
3841 }
3842 }
3843
3844 /* If no freshness information could be found in Cache-Control values,
3845 * look for an Expires header. */
3846 if (!has_freshness_info) {
3847 ctx.blk = NULL;
3848 has_freshness_info = http_find_header(htx, ist("expires"), &ctx, 0);
Christopher Faulet25a02f62018-10-24 12:00:25 +02003849 }
Remi Tricot-Le Bretoncc9bf2e2020-11-12 11:14:41 +01003850
3851 /* If no freshness information could be found in Cache-Control or Expires
3852 * values, look for an explicit validator. */
3853 if (!has_freshness_info) {
3854 ctx.blk = NULL;
3855 has_validator = 1;
3856 if (!http_find_header(htx, ist("etag"), &ctx, 0)) {
3857 ctx.blk = NULL;
3858 if (!http_find_header(htx, ist("last-modified"), &ctx, 0))
3859 has_validator = 0;
3860 }
3861 }
3862
3863 /* We won't store an entry that has neither a cache validator nor an
3864 * explicit expiration time, as suggested in RFC 7234#3. */
3865 if (!has_freshness_info && !has_validator)
3866 txn->flags |= TX_CACHE_IGNORE;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003867}
3868
Christopher Faulet377c5a52018-10-24 21:21:30 +02003869/*
3870 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
3871 * for the current backend.
3872 *
3873 * It is assumed that the request is either a HEAD, GET, or POST and that the
3874 * uri_auth field is valid.
3875 *
3876 * Returns 1 if stats should be provided, otherwise 0.
3877 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003878static int http_stats_check_uri(struct stream *s, struct http_txn *txn, struct proxy *backend)
Christopher Faulet377c5a52018-10-24 21:21:30 +02003879{
3880 struct uri_auth *uri_auth = backend->uri_auth;
3881 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003882 struct htx_sl *sl;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003883 struct ist uri;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003884
3885 if (!uri_auth)
3886 return 0;
3887
3888 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
3889 return 0;
3890
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003891 htx = htxbuf(&s->req.buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02003892 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003893 uri = htx_sl_req_uri(sl);
Amaury Denoyellec453f952021-07-06 11:40:12 +02003894 if (*uri_auth->uri_prefix == '/') {
3895 struct http_uri_parser parser = http_uri_parser_init(uri);
3896 uri = http_parse_path(&parser);
3897 }
Christopher Faulet377c5a52018-10-24 21:21:30 +02003898
3899 /* check URI size */
3900 if (uri_auth->uri_len > uri.len)
3901 return 0;
3902
3903 if (memcmp(uri.ptr, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
3904 return 0;
3905
3906 return 1;
3907}
3908
3909/* This function prepares an applet to handle the stats. It can deal with the
3910 * "100-continue" expectation, check that admin rules are met for POST requests,
3911 * and program a response message if something was unexpected. It cannot fail
3912 * and always relies on the stats applet to complete the job. It does not touch
3913 * analysers nor counters, which are left to the caller. It does not touch
3914 * s->target which is supposed to already point to the stats applet. The caller
3915 * is expected to have already assigned an appctx to the stream.
3916 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003917static int http_handle_stats(struct stream *s, struct channel *req)
Christopher Faulet377c5a52018-10-24 21:21:30 +02003918{
3919 struct stats_admin_rule *stats_admin_rule;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003920 struct session *sess = s->sess;
3921 struct http_txn *txn = s->txn;
3922 struct http_msg *msg = &txn->req;
3923 struct uri_auth *uri_auth = s->be->uri_auth;
3924 const char *h, *lookup, *end;
Christopher Faulet693b23b2022-02-28 09:09:05 +01003925 struct appctx *appctx = __cs_appctx(s->csb);
Willy Tarreau91cefca2022-05-03 17:08:29 +02003926 struct show_stat_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx));
Christopher Faulet377c5a52018-10-24 21:21:30 +02003927 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003928 struct htx_sl *sl;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003929
Willy Tarreau41f88522022-05-03 18:39:27 +02003930 appctx->st1 = 0;
3931 ctx->state = STAT_ST_INIT;
Willy Tarreau91cefca2022-05-03 17:08:29 +02003932 ctx->st_code = STAT_STATUS_INIT;
3933 ctx->flags |= uri_auth->flags;
3934 ctx->flags |= STAT_FMT_HTML; /* assume HTML mode by default */
Christopher Faulet377c5a52018-10-24 21:21:30 +02003935 if ((msg->flags & HTTP_MSGF_VER_11) && (txn->meth != HTTP_METH_HEAD))
Willy Tarreau91cefca2022-05-03 17:08:29 +02003936 ctx->flags |= STAT_CHUNKED;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003937
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003938 htx = htxbuf(&req->buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02003939 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003940 lookup = HTX_SL_REQ_UPTR(sl) + uri_auth->uri_len;
3941 end = HTX_SL_REQ_UPTR(sl) + HTX_SL_REQ_ULEN(sl);
Christopher Faulet377c5a52018-10-24 21:21:30 +02003942
3943 for (h = lookup; h <= end - 3; h++) {
3944 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02003945 ctx->flags |= STAT_HIDE_DOWN;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003946 break;
3947 }
Amaury Denoyelle91e55ea2021-02-25 14:46:08 +01003948 }
3949
3950 for (h = lookup; h <= end - 9; h++) {
3951 if (memcmp(h, ";no-maint", 9) == 0) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02003952 ctx->flags |= STAT_HIDE_MAINT;
Willy Tarreau3e320362020-10-23 17:28:57 +02003953 break;
3954 }
Christopher Faulet377c5a52018-10-24 21:21:30 +02003955 }
3956
3957 if (uri_auth->refresh) {
3958 for (h = lookup; h <= end - 10; h++) {
3959 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02003960 ctx->flags |= STAT_NO_REFRESH;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003961 break;
3962 }
3963 }
3964 }
3965
3966 for (h = lookup; h <= end - 4; h++) {
3967 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02003968 ctx->flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
Christopher Faulet377c5a52018-10-24 21:21:30 +02003969 break;
3970 }
3971 }
3972
3973 for (h = lookup; h <= end - 6; h++) {
3974 if (memcmp(h, ";typed", 6) == 0) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02003975 ctx->flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
3976 ctx->flags |= STAT_FMT_TYPED;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003977 break;
3978 }
3979 }
3980
Christopher Faulet6338a082019-09-09 15:50:54 +02003981 for (h = lookup; h <= end - 5; h++) {
3982 if (memcmp(h, ";json", 5) == 0) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02003983 ctx->flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
3984 ctx->flags |= STAT_FMT_JSON;
Christopher Faulet6338a082019-09-09 15:50:54 +02003985 break;
3986 }
3987 }
3988
3989 for (h = lookup; h <= end - 12; h++) {
3990 if (memcmp(h, ";json-schema", 12) == 0) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02003991 ctx->flags &= ~STAT_FMT_MASK;
3992 ctx->flags |= STAT_JSON_SCHM;
Christopher Faulet6338a082019-09-09 15:50:54 +02003993 break;
3994 }
3995 }
3996
Christopher Faulet377c5a52018-10-24 21:21:30 +02003997 for (h = lookup; h <= end - 8; h++) {
3998 if (memcmp(h, ";st=", 4) == 0) {
3999 int i;
4000 h += 4;
Willy Tarreau91cefca2022-05-03 17:08:29 +02004001 ctx->st_code = STAT_STATUS_UNKN;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004002 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
4003 if (strncmp(stat_status_codes[i], h, 4) == 0) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02004004 ctx->st_code = i;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004005 break;
4006 }
4007 }
4008 break;
4009 }
4010 }
4011
Willy Tarreau91cefca2022-05-03 17:08:29 +02004012 ctx->scope_str = 0;
4013 ctx->scope_len = 0;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004014 for (h = lookup; h <= end - 8; h++) {
4015 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
4016 int itx = 0;
4017 const char *h2;
4018 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
4019 const char *err;
4020
4021 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
4022 h2 = h;
Willy Tarreau91cefca2022-05-03 17:08:29 +02004023 ctx->scope_str = h2 - HTX_SL_REQ_UPTR(sl);
Christopher Fauleted7a0662019-01-14 11:07:34 +01004024 while (h < end) {
Christopher Faulet377c5a52018-10-24 21:21:30 +02004025 if (*h == ';' || *h == '&' || *h == ' ')
4026 break;
4027 itx++;
4028 h++;
4029 }
4030
4031 if (itx > STAT_SCOPE_TXT_MAXLEN)
4032 itx = STAT_SCOPE_TXT_MAXLEN;
Willy Tarreau91cefca2022-05-03 17:08:29 +02004033 ctx->scope_len = itx;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004034
Willy Tarreau91cefca2022-05-03 17:08:29 +02004035 /* scope_txt = search query, ctx->scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Christopher Faulet377c5a52018-10-24 21:21:30 +02004036 memcpy(scope_txt, h2, itx);
4037 scope_txt[itx] = '\0';
4038 err = invalid_char(scope_txt);
4039 if (err) {
4040 /* bad char in search text => clear scope */
Willy Tarreau91cefca2022-05-03 17:08:29 +02004041 ctx->scope_str = 0;
4042 ctx->scope_len = 0;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004043 }
4044 break;
4045 }
4046 }
4047
4048 /* now check whether we have some admin rules for this request */
4049 list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) {
4050 int ret = 1;
4051
4052 if (stats_admin_rule->cond) {
4053 ret = acl_exec_cond(stats_admin_rule->cond, s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
4054 ret = acl_pass(ret);
4055 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
4056 ret = !ret;
4057 }
4058
4059 if (ret) {
4060 /* no rule, or the rule matches */
Willy Tarreau91cefca2022-05-03 17:08:29 +02004061 ctx->flags |= STAT_ADMIN;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004062 break;
4063 }
4064 }
4065
Christopher Faulet5d45e382019-02-27 15:15:23 +01004066 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
4067 appctx->st0 = STAT_HTTP_HEAD;
4068 else if (txn->meth == HTTP_METH_POST) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02004069 if (ctx->flags & STAT_ADMIN) {
Christopher Faulet377c5a52018-10-24 21:21:30 +02004070 appctx->st0 = STAT_HTTP_POST;
Christopher Fauletbd9e8422019-08-15 22:26:48 +02004071 if (msg->msg_state < HTTP_MSG_DATA)
4072 req->analysers |= AN_REQ_HTTP_BODY;
4073 }
Christopher Faulet377c5a52018-10-24 21:21:30 +02004074 else {
Christopher Faulet5d45e382019-02-27 15:15:23 +01004075 /* POST without admin level */
Willy Tarreau91cefca2022-05-03 17:08:29 +02004076 ctx->flags &= ~STAT_CHUNKED;
4077 ctx->st_code = STAT_STATUS_DENY;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004078 appctx->st0 = STAT_HTTP_LAST;
4079 }
4080 }
4081 else {
Christopher Faulet5d45e382019-02-27 15:15:23 +01004082 /* Unsupported method */
Willy Tarreau91cefca2022-05-03 17:08:29 +02004083 ctx->flags &= ~STAT_CHUNKED;
4084 ctx->st_code = STAT_STATUS_IVAL;
Christopher Faulet5d45e382019-02-27 15:15:23 +01004085 appctx->st0 = STAT_HTTP_LAST;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004086 }
4087
4088 s->task->nice = -32; /* small boost for HTTP statistics */
4089 return 1;
4090}
4091
Christopher Faulet021a8e42021-03-29 10:46:38 +02004092/* This function waits for the message payload at most <time> milliseconds (may
4093 * be set to TICK_ETERNITY). It stops to wait if at least <bytes> bytes of the
4094 * payload are received (0 means no limit). It returns HTTP_RULE_* depending on
4095 * the result:
4096 *
4097 * - HTTP_RULE_RES_CONT when conditions are met to stop waiting
4098 * - HTTP_RULE_RES_YIELD to wait for more data
Ilya Shipitsinb2be9a12021-04-24 13:25:42 +05004099 * - HTTP_RULE_RES_ABRT when a timeout occurred.
Christopher Faulet021a8e42021-03-29 10:46:38 +02004100 * - HTTP_RULE_RES_BADREQ if a parsing error is raised by lower level
Ilya Shipitsinb2be9a12021-04-24 13:25:42 +05004101 * - HTTP_RULE_RES_ERROR if an internal error occurred
Christopher Faulet021a8e42021-03-29 10:46:38 +02004102 *
Ilya Shipitsinb2be9a12021-04-24 13:25:42 +05004103 * If a timeout occurred, this function is responsible to emit the right response
Christopher Faulet021a8e42021-03-29 10:46:38 +02004104 * to the client, depending on the channel (408 on request side, 504 on response
4105 * side). All other errors must be handled by the caller.
4106 */
4107enum rule_result http_wait_for_msg_body(struct stream *s, struct channel *chn,
4108 unsigned int time, unsigned int bytes)
4109{
4110 struct session *sess = s->sess;
4111 struct http_txn *txn = s->txn;
4112 struct http_msg *msg = ((chn->flags & CF_ISRESP) ? &txn->rsp : &txn->req);
4113 struct htx *htx;
4114 enum rule_result ret = HTTP_RULE_RES_CONT;
4115
4116 htx = htxbuf(&chn->buf);
4117
4118 if (htx->flags & HTX_FL_PARSING_ERROR) {
4119 ret = HTTP_RULE_RES_BADREQ;
4120 goto end;
4121 }
4122 if (htx->flags & HTX_FL_PROCESSING_ERROR) {
4123 ret = HTTP_RULE_RES_ERROR;
4124 goto end;
4125 }
4126
4127 /* Do nothing for bodyless and CONNECT requests */
4128 if (txn->meth == HTTP_METH_CONNECT || (msg->flags & HTTP_MSGF_BODYLESS))
4129 goto end;
4130
4131 if (!(chn->flags & CF_ISRESP) && msg->msg_state < HTTP_MSG_DATA) {
4132 if (http_handle_expect_hdr(s, htx, msg) == -1) {
4133 ret = HTTP_RULE_RES_ERROR;
4134 goto end;
4135 }
4136 }
4137
4138 msg->msg_state = HTTP_MSG_DATA;
4139
4140 /* Now we're in HTTP_MSG_DATA. We just need to know if all data have
4141 * been received or if the buffer is full.
4142 */
Christopher Faulet78335962021-09-23 14:46:32 +02004143 if ((htx->flags & HTX_FL_EOM) ||
4144 htx_get_tail_type(htx) > HTX_BLK_DATA ||
4145 channel_htx_full(chn, htx, global.tune.maxrewrite) ||
Christopher Fauleta0bdec32022-04-04 07:51:21 +02004146 cs_rx_blocked_room(chn_prod(chn)))
Christopher Faulet021a8e42021-03-29 10:46:38 +02004147 goto end;
4148
4149 if (bytes) {
4150 struct htx_blk *blk;
4151 unsigned int len = 0;
4152
4153 for (blk = htx_get_first_blk(htx); blk; blk = htx_get_next_blk(htx, blk)) {
4154 if (htx_get_blk_type(blk) != HTX_BLK_DATA)
4155 continue;
4156 len += htx_get_blksz(blk);
4157 if (len >= bytes)
4158 goto end;
4159 }
4160 }
4161
4162 if ((chn->flags & CF_READ_TIMEOUT) || tick_is_expired(chn->analyse_exp, now_ms)) {
4163 if (!(chn->flags & CF_ISRESP))
4164 goto abort_req;
4165 goto abort_res;
4166 }
4167
4168 /* we get here if we need to wait for more data */
4169 if (!(chn->flags & (CF_SHUTR | CF_READ_ERROR))) {
4170 if (!tick_isset(chn->analyse_exp))
4171 chn->analyse_exp = tick_add_ifset(now_ms, time);
4172 ret = HTTP_RULE_RES_YIELD;
4173 }
4174
4175 end:
4176 return ret;
4177
4178 abort_req:
4179 txn->status = 408;
4180 if (!(s->flags & SF_ERR_MASK))
4181 s->flags |= SF_ERR_CLITO;
4182 if (!(s->flags & SF_FINST_MASK))
4183 s->flags |= SF_FINST_D;
Willy Tarreau4781b152021-04-06 13:53:36 +02004184 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req);
Christopher Faulet021a8e42021-03-29 10:46:38 +02004185 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02004186 _HA_ATOMIC_INC(&sess->listener->counters->failed_req);
Christopher Faulet021a8e42021-03-29 10:46:38 +02004187 http_reply_and_close(s, txn->status, http_error_message(s));
4188 ret = HTTP_RULE_RES_ABRT;
4189 goto end;
4190
4191 abort_res:
4192 txn->status = 504;
4193 if (!(s->flags & SF_ERR_MASK))
4194 s->flags |= SF_ERR_SRVTO;
4195 if (!(s->flags & SF_FINST_MASK))
4196 s->flags |= SF_FINST_D;
4197 stream_inc_http_fail_ctr(s);
4198 http_reply_and_close(s, txn->status, http_error_message(s));
4199 ret = HTTP_RULE_RES_ABRT;
4200 goto end;
4201}
4202
Christopher Faulet0eb32c02022-04-04 11:06:31 +02004203void http_perform_server_redirect(struct stream *s, struct conn_stream *cs)
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004204{
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004205 struct channel *req = &s->req;
4206 struct channel *res = &s->res;
4207 struct server *srv;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004208 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004209 struct htx_sl *sl;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004210 struct ist path, location;
4211 unsigned int flags;
Amaury Denoyellec453f952021-07-06 11:40:12 +02004212 struct http_uri_parser parser;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004213
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004214 /*
4215 * Create the location
4216 */
4217 chunk_reset(&trash);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004218
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004219 /* 1: add the server's prefix */
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004220 /* special prefix "/" means don't change URL */
4221 srv = __objt_server(s->target);
4222 if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') {
4223 if (!chunk_memcat(&trash, srv->rdr_pfx, srv->rdr_len))
4224 return;
4225 }
4226
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004227 /* 2: add the request Path */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004228 htx = htxbuf(&req->buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02004229 sl = http_get_stline(htx);
Amaury Denoyellec453f952021-07-06 11:40:12 +02004230 parser = http_uri_parser_init(htx_sl_req_uri(sl));
4231 path = http_parse_path(&parser);
Tim Duesterhused526372020-03-05 17:56:33 +01004232 if (!isttest(path))
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004233 return;
4234
4235 if (!chunk_memcat(&trash, path.ptr, path.len))
4236 return;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004237 location = ist2(trash.area, trash.data);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004238
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004239 /*
4240 * Create the 302 respone
4241 */
4242 htx = htx_from_buf(&res->buf);
4243 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
4244 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
4245 ist("HTTP/1.1"), ist("302"), ist("Found"));
4246 if (!sl)
4247 goto fail;
4248 sl->info.res.status = 302;
4249 s->txn->status = 302;
4250
4251 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) ||
4252 !htx_add_header(htx, ist("Connection"), ist("close")) ||
4253 !htx_add_header(htx, ist("Content-length"), ist("0")) ||
4254 !htx_add_header(htx, ist("Location"), location))
4255 goto fail;
4256
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004257 if (!htx_add_endof(htx, HTX_BLK_EOH))
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004258 goto fail;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004259
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004260 htx->flags |= HTX_FL_EOM;
Christopher Fauletc20afb82020-01-24 19:16:26 +01004261 htx_to_buf(htx, &res->buf);
Christopher Fauleta72a7e42020-01-28 09:28:11 +01004262 if (!http_forward_proxy_resp(s, 1))
4263 goto fail;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004264
4265 /* return without error. */
Christopher Faulet0eb32c02022-04-04 11:06:31 +02004266 cs_shutr(cs);
4267 cs_shutw(cs);
Christopher Faulet50264b42022-03-30 19:39:30 +02004268 s->conn_err_type = STRM_ET_NONE;
Christopher Faulet0eb32c02022-04-04 11:06:31 +02004269 cs->state = CS_ST_CLO;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004270
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004271 if (!(s->flags & SF_ERR_MASK))
4272 s->flags |= SF_ERR_LOCAL;
4273 if (!(s->flags & SF_FINST_MASK))
4274 s->flags |= SF_FINST_C;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004275
4276 /* FIXME: we should increase a counter of redirects per server and per backend. */
4277 srv_inc_sess_ctr(srv);
4278 srv_set_sess_last(srv);
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004279 return;
4280
4281 fail:
4282 /* If an error occurred, remove the incomplete HTTP response from the
4283 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004284 channel_htx_truncate(res, htx);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004285}
4286
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05004287/* This function terminates the request because it was completely analyzed or
Christopher Fauletf2824e62018-10-01 12:12:37 +02004288 * because an error was triggered during the body forwarding.
4289 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004290static void http_end_request(struct stream *s)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004291{
4292 struct channel *chn = &s->req;
4293 struct http_txn *txn = s->txn;
4294
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004295 DBG_TRACE_ENTER(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004296
Christopher Fauletb42a8b62018-11-19 21:59:00 +01004297 if (unlikely(txn->req.msg_state == HTTP_MSG_ERROR ||
4298 txn->rsp.msg_state == HTTP_MSG_ERROR)) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004299 channel_abort(chn);
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004300 channel_htx_truncate(chn, htxbuf(&chn->buf));
Christopher Fauletf2824e62018-10-01 12:12:37 +02004301 goto end;
4302 }
4303
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004304 if (unlikely(txn->req.msg_state < HTTP_MSG_DONE)) {
4305 DBG_TRACE_DEVEL("waiting end of the request", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004306 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004307 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004308
4309 if (txn->req.msg_state == HTTP_MSG_DONE) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004310 /* No need to read anymore, the request was completely parsed.
4311 * We can shut the read side unless we want to abort_on_close,
4312 * or we have a POST request. The issue with POST requests is
4313 * that some browsers still send a CRLF after the request, and
4314 * this CRLF must be read so that it does not remain in the kernel
4315 * buffers, otherwise a close could cause an RST on some systems
4316 * (eg: Linux).
4317 */
Christopher Faulet769d0e92019-03-22 14:23:18 +01004318 if (!(s->be->options & PR_O_ABRT_CLOSE) && txn->meth != HTTP_METH_POST)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004319 channel_dont_read(chn);
4320
4321 /* if the server closes the connection, we want to immediately react
4322 * and close the socket to save packets and syscalls.
4323 */
Christopher Faulet8abe7122022-03-30 15:10:18 +02004324 s->csb->flags |= CS_FL_NOHALF;
Christopher Fauletf2824e62018-10-01 12:12:37 +02004325
4326 /* In any case we've finished parsing the request so we must
4327 * disable Nagle when sending data because 1) we're not going
4328 * to shut this side, and 2) the server is waiting for us to
4329 * send pending data.
4330 */
4331 chn->flags |= CF_NEVER_WAIT;
4332
Christopher Fauletd01ce402019-01-02 17:44:13 +01004333 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
4334 /* The server has not finished to respond, so we
4335 * don't want to move in order not to upset it.
4336 */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004337 DBG_TRACE_DEVEL("waiting end of the response", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletd01ce402019-01-02 17:44:13 +01004338 return;
4339 }
4340
Christopher Fauletf2824e62018-10-01 12:12:37 +02004341 /* When we get here, it means that both the request and the
4342 * response have finished receiving. Depending on the connection
4343 * mode, we'll have to wait for the last bytes to leave in either
4344 * direction, and sometimes for a close to be effective.
4345 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02004346 if (txn->flags & TX_CON_WANT_TUN) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004347 /* Tunnel mode will not have any analyser so it needs to
4348 * poll for reads.
4349 */
4350 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004351 if (b_data(&chn->buf)) {
4352 DBG_TRACE_DEVEL("waiting to flush the request", STRM_EV_HTTP_ANA, s, txn);
Christopher Faulet9768c262018-10-22 09:34:31 +02004353 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004354 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004355 txn->req.msg_state = HTTP_MSG_TUNNEL;
4356 }
4357 else {
4358 /* we're not expecting any new data to come for this
4359 * transaction, so we can close it.
Christopher Faulet9768c262018-10-22 09:34:31 +02004360 *
4361 * However, there is an exception if the response
4362 * length is undefined. In this case, we need to wait
4363 * the close from the server. The response will be
4364 * switched in TUNNEL mode until the end.
Christopher Fauletf2824e62018-10-01 12:12:37 +02004365 */
4366 if (!(txn->rsp.flags & HTTP_MSGF_XFER_LEN) &&
4367 txn->rsp.msg_state != HTTP_MSG_CLOSED)
Christopher Faulet9768c262018-10-22 09:34:31 +02004368 goto check_channel_flags;
Christopher Fauletf2824e62018-10-01 12:12:37 +02004369
4370 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4371 channel_shutr_now(chn);
4372 channel_shutw_now(chn);
4373 }
4374 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004375 goto check_channel_flags;
4376 }
4377
4378 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
4379 http_msg_closing:
4380 /* nothing else to forward, just waiting for the output buffer
4381 * to be empty and for the shutw_now to take effect.
4382 */
4383 if (channel_is_empty(chn)) {
4384 txn->req.msg_state = HTTP_MSG_CLOSED;
4385 goto http_msg_closed;
4386 }
4387 else if (chn->flags & CF_SHUTW) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004388 txn->req.msg_state = HTTP_MSG_ERROR;
4389 goto end;
4390 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004391 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004392 return;
4393 }
4394
4395 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
4396 http_msg_closed:
Christopher Fauletf2824e62018-10-01 12:12:37 +02004397 /* if we don't know whether the server will close, we need to hard close */
4398 if (txn->rsp.flags & HTTP_MSGF_XFER_LEN)
Christopher Faulet8abe7122022-03-30 15:10:18 +02004399 s->csb->flags |= CS_FL_NOLINGER; /* we want to close ASAP */
Christopher Fauletf2824e62018-10-01 12:12:37 +02004400 /* see above in MSG_DONE why we only do this in these states */
Christopher Faulet769d0e92019-03-22 14:23:18 +01004401 if (!(s->be->options & PR_O_ABRT_CLOSE))
Christopher Fauletf2824e62018-10-01 12:12:37 +02004402 channel_dont_read(chn);
4403 goto end;
4404 }
4405
4406 check_channel_flags:
4407 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4408 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4409 /* if we've just closed an output, let's switch */
4410 txn->req.msg_state = HTTP_MSG_CLOSING;
4411 goto http_msg_closing;
4412 }
4413
4414 end:
4415 chn->analysers &= AN_REQ_FLT_END;
Christopher Faulet198ef8b2020-12-15 13:32:55 +01004416 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
4417 chn->flags |= CF_NEVER_WAIT;
4418 if (HAS_REQ_DATA_FILTERS(s))
Christopher Fauletf2824e62018-10-01 12:12:37 +02004419 chn->analysers |= AN_REQ_FLT_XFER_DATA;
Christopher Faulet198ef8b2020-12-15 13:32:55 +01004420 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004421 channel_auto_close(chn);
4422 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004423 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004424}
4425
4426
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05004427/* This function terminates the response because it was completely analyzed or
Christopher Fauletf2824e62018-10-01 12:12:37 +02004428 * because an error was triggered during the body forwarding.
4429 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004430static void http_end_response(struct stream *s)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004431{
4432 struct channel *chn = &s->res;
4433 struct http_txn *txn = s->txn;
4434
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004435 DBG_TRACE_ENTER(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004436
Christopher Fauletb42a8b62018-11-19 21:59:00 +01004437 if (unlikely(txn->req.msg_state == HTTP_MSG_ERROR ||
4438 txn->rsp.msg_state == HTTP_MSG_ERROR)) {
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004439 channel_htx_truncate(&s->req, htxbuf(&s->req.buf));
Christopher Faulet9768c262018-10-22 09:34:31 +02004440 channel_abort(&s->req);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004441 goto end;
4442 }
4443
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004444 if (unlikely(txn->rsp.msg_state < HTTP_MSG_DONE)) {
4445 DBG_TRACE_DEVEL("waiting end of the response", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004446 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004447 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004448
4449 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
4450 /* In theory, we don't need to read anymore, but we must
4451 * still monitor the server connection for a possible close
4452 * while the request is being uploaded, so we don't disable
4453 * reading.
4454 */
4455 /* channel_dont_read(chn); */
4456
4457 if (txn->req.msg_state < HTTP_MSG_DONE) {
4458 /* The client seems to still be sending data, probably
4459 * because we got an error response during an upload.
4460 * We have the choice of either breaking the connection
4461 * or letting it pass through. Let's do the later.
4462 */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004463 DBG_TRACE_DEVEL("waiting end of the request", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004464 return;
4465 }
4466
4467 /* When we get here, it means that both the request and the
4468 * response have finished receiving. Depending on the connection
4469 * mode, we'll have to wait for the last bytes to leave in either
4470 * direction, and sometimes for a close to be effective.
4471 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02004472 if (txn->flags & TX_CON_WANT_TUN) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004473 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004474 if (b_data(&chn->buf)) {
4475 DBG_TRACE_DEVEL("waiting to flush the respone", STRM_EV_HTTP_ANA, s, txn);
Christopher Faulet9768c262018-10-22 09:34:31 +02004476 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004477 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004478 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
4479 }
4480 else {
4481 /* we're not expecting any new data to come for this
4482 * transaction, so we can close it.
4483 */
4484 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4485 channel_shutr_now(chn);
4486 channel_shutw_now(chn);
4487 }
4488 }
4489 goto check_channel_flags;
4490 }
4491
4492 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
4493 http_msg_closing:
4494 /* nothing else to forward, just waiting for the output buffer
4495 * to be empty and for the shutw_now to take effect.
4496 */
4497 if (channel_is_empty(chn)) {
4498 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4499 goto http_msg_closed;
4500 }
4501 else if (chn->flags & CF_SHUTW) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004502 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau4781b152021-04-06 13:53:36 +02004503 _HA_ATOMIC_INC(&strm_sess(s)->fe->fe_counters.cli_aborts);
4504 _HA_ATOMIC_INC(&s->be->be_counters.cli_aborts);
William Lallemand36119de2021-03-08 15:26:48 +01004505 if (strm_sess(s)->listener && strm_sess(s)->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02004506 _HA_ATOMIC_INC(&strm_sess(s)->listener->counters->cli_aborts);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004507 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02004508 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.cli_aborts);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004509 goto end;
4510 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004511 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004512 return;
4513 }
4514
4515 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
4516 http_msg_closed:
4517 /* drop any pending data */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004518 channel_htx_truncate(&s->req, htxbuf(&s->req.buf));
Christopher Faulet9768c262018-10-22 09:34:31 +02004519 channel_abort(&s->req);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004520 goto end;
4521 }
4522
4523 check_channel_flags:
4524 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4525 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4526 /* if we've just closed an output, let's switch */
4527 txn->rsp.msg_state = HTTP_MSG_CLOSING;
4528 goto http_msg_closing;
4529 }
4530
4531 end:
4532 chn->analysers &= AN_RES_FLT_END;
Christopher Faulet198ef8b2020-12-15 13:32:55 +01004533 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
4534 chn->flags |= CF_NEVER_WAIT;
4535 if (HAS_RSP_DATA_FILTERS(s))
4536 chn->analysers |= AN_RES_FLT_XFER_DATA;
4537 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004538 channel_auto_close(chn);
4539 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004540 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004541}
4542
Christopher Fauletef70e252020-01-28 09:26:19 +01004543/* Forward a response generated by HAProxy (error/redirect/return). This
4544 * function forwards all pending incoming data. If <final> is set to 0, nothing
4545 * more is performed. It is used for 1xx informational messages. Otherwise, the
Christopher Faulet507479b2020-05-15 12:29:46 +02004546 * transaction is terminated and the request is emptied. On success 1 is
Christopher Faulet40e6b552020-06-25 16:04:50 +02004547 * returned. If an error occurred, 0 is returned. If it fails, this function
4548 * only exits. It is the caller responsibility to do the cleanup.
Christopher Fauletef70e252020-01-28 09:26:19 +01004549 */
4550int http_forward_proxy_resp(struct stream *s, int final)
4551{
4552 struct channel *req = &s->req;
4553 struct channel *res = &s->res;
4554 struct htx *htx = htxbuf(&res->buf);
4555 size_t data;
4556
4557 if (final) {
4558 htx->flags |= HTX_FL_PROXY_RESP;
Christopher Faulet507479b2020-05-15 12:29:46 +02004559
Christopher Fauletaab1b672020-11-18 16:44:02 +01004560 if (!htx_is_empty(htx) && !http_eval_after_res_rules(s))
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01004561 return 0;
Christopher Fauletef70e252020-01-28 09:26:19 +01004562
Christopher Fauletd6c48362020-10-19 18:01:38 +02004563 if (s->txn->meth == HTTP_METH_HEAD)
4564 htx_skip_msg_payload(htx);
4565
Christopher Fauletef70e252020-01-28 09:26:19 +01004566 channel_auto_read(req);
4567 channel_abort(req);
4568 channel_auto_close(req);
4569 channel_htx_erase(req, htxbuf(&req->buf));
4570
4571 res->wex = tick_add_ifset(now_ms, res->wto);
4572 channel_auto_read(res);
4573 channel_auto_close(res);
4574 channel_shutr_now(res);
Christopher Faulet1a9db7c2020-06-25 15:36:45 +02004575 res->flags |= CF_EOI; /* The response is terminated, add EOI */
Christopher Faulet42432f32020-11-20 17:43:16 +01004576 htxbuf(&res->buf)->flags |= HTX_FL_EOM; /* no more data are expected */
Christopher Fauletef70e252020-01-28 09:26:19 +01004577 }
Christopher Fauletcf6898c2020-06-25 15:55:11 +02004578 else {
4579 /* Send ASAP informational messages. Rely on CF_EOI for final
4580 * response.
4581 */
4582 res->flags |= CF_SEND_DONTWAIT;
4583 }
Christopher Fauletef70e252020-01-28 09:26:19 +01004584
4585 data = htx->data - co_data(res);
4586 c_adv(res, data);
4587 htx->first = -1;
4588 res->total += data;
4589 return 1;
4590}
4591
Christopher Faulet0eb32c02022-04-04 11:06:31 +02004592void http_server_error(struct stream *s, struct conn_stream *cs, int err,
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004593 int finst, struct http_reply *msg)
Christopher Faulet0f226952018-10-22 09:29:56 +02004594{
Christopher Faulet72c7d8d2020-01-27 15:32:25 +01004595 http_reply_and_close(s, s->txn->status, msg);
Christopher Faulet0f226952018-10-22 09:29:56 +02004596 if (!(s->flags & SF_ERR_MASK))
4597 s->flags |= err;
4598 if (!(s->flags & SF_FINST_MASK))
4599 s->flags |= finst;
4600}
4601
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004602void http_reply_and_close(struct stream *s, short status, struct http_reply *msg)
Christopher Faulet0f226952018-10-22 09:29:56 +02004603{
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004604 if (!msg) {
4605 channel_htx_truncate(&s->res, htxbuf(&s->res.buf));
4606 goto end;
4607 }
4608
4609 if (http_reply_message(s, msg) == -1) {
4610 /* On error, return a 500 error message, but don't rewrite it if
Christopher Faulet40e6b552020-06-25 16:04:50 +02004611 * it is already an internal error. If it was already a "const"
4612 * 500 error, just fail.
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004613 */
Christopher Faulet40e6b552020-06-25 16:04:50 +02004614 if (s->txn->status == 500) {
4615 if (s->txn->flags & TX_CONST_REPLY)
4616 goto end;
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004617 s->txn->flags |= TX_CONST_REPLY;
Christopher Faulet40e6b552020-06-25 16:04:50 +02004618 }
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004619 s->txn->status = 500;
4620 s->txn->http_reply = NULL;
4621 return http_reply_and_close(s, s->txn->status, http_error_message(s));
4622 }
4623
4624end:
4625 s->res.wex = tick_add_ifset(now_ms, s->res.wto);
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004626
Christopher Faulet2d565002021-09-10 09:17:50 +02004627 /* At this staged, HTTP analysis is finished */
4628 s->req.analysers &= AN_REQ_FLT_END;
4629 s->req.analyse_exp = TICK_ETERNITY;
4630
4631 s->res.analysers &= AN_RES_FLT_END;
4632 s->res.analyse_exp = TICK_ETERNITY;
4633
Christopher Faulet0f226952018-10-22 09:29:56 +02004634 channel_auto_read(&s->req);
4635 channel_abort(&s->req);
4636 channel_auto_close(&s->req);
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004637 channel_htx_erase(&s->req, htxbuf(&s->req.buf));
Christopher Faulet72c7d8d2020-01-27 15:32:25 +01004638 channel_auto_read(&s->res);
4639 channel_auto_close(&s->res);
4640 channel_shutr_now(&s->res);
Christopher Faulet0f226952018-10-22 09:29:56 +02004641}
4642
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004643struct http_reply *http_error_message(struct stream *s)
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004644{
4645 const int msgnum = http_get_status_idx(s->txn->status);
4646
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004647 if (s->txn->http_reply)
4648 return s->txn->http_reply;
4649 else if (s->be->replies[msgnum])
4650 return s->be->replies[msgnum];
4651 else if (strm_fe(s)->replies[msgnum])
4652 return strm_fe(s)->replies[msgnum];
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004653 else
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004654 return &http_err_replies[msgnum];
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004655}
4656
Christopher Faulet40e6b552020-06-25 16:04:50 +02004657/* Produces an HTX message from an http reply. Depending on the http reply type,
4658 * a, errorfile, an raw file or a log-format string is used. On success, it
4659 * returns 0. If an error occurs -1 is returned. If it fails, this function only
4660 * exits. It is the caller responsibility to do the cleanup.
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004661 */
Christopher Fauletae43b6c2020-05-27 15:24:22 +02004662int http_reply_to_htx(struct stream *s, struct htx *htx, struct http_reply *reply)
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004663{
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004664 struct buffer *errmsg;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004665 struct htx_sl *sl;
4666 struct buffer *body = NULL;
4667 const char *status, *reason, *clen, *ctype;
4668 unsigned int slflags;
4669 int ret = 0;
4670
Christopher Faulete29a97e2020-05-14 14:49:25 +02004671 /*
4672 * - HTTP_REPLY_ERRFILES unexpected here. handled as no payload if so
4673 *
4674 * - HTTP_REPLY_INDIRECT: switch on another reply if defined or handled
4675 * as no payload if NULL. the TXN status code is set with the status
4676 * of the original reply.
4677 */
4678
4679 if (reply->type == HTTP_REPLY_INDIRECT) {
4680 if (reply->body.reply)
4681 reply = reply->body.reply;
4682 }
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004683 if (reply->type == HTTP_REPLY_ERRMSG && !reply->body.errmsg) {
4684 /* get default error message */
4685 if (reply == s->txn->http_reply)
4686 s->txn->http_reply = NULL;
4687 reply = http_error_message(s);
4688 if (reply->type == HTTP_REPLY_INDIRECT) {
4689 if (reply->body.reply)
4690 reply = reply->body.reply;
4691 }
4692 }
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004693
4694 if (reply->type == HTTP_REPLY_ERRMSG) {
4695 /* implicit or explicit error message*/
4696 errmsg = reply->body.errmsg;
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004697 if (errmsg && !b_is_null(errmsg)) {
Christopher Faulet20567362020-05-15 14:52:49 +02004698 if (!htx_copy_msg(htx, errmsg))
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004699 goto fail;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004700 }
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004701 }
4702 else {
4703 /* no payload, file or log-format string */
4704 if (reply->type == HTTP_REPLY_RAW) {
4705 /* file */
4706 body = &reply->body.obj;
4707 }
4708 else if (reply->type == HTTP_REPLY_LOGFMT) {
4709 /* log-format string */
4710 body = alloc_trash_chunk();
4711 if (!body)
4712 goto fail_alloc;
4713 body->data = build_logline(s, body->area, body->size, &reply->body.fmt);
4714 }
4715 /* else no payload */
4716
4717 status = ultoa(reply->status);
4718 reason = http_get_reason(reply->status);
4719 slflags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_CLEN);
4720 if (!body || !b_data(body))
4721 slflags |= HTX_SL_F_BODYLESS;
4722 sl = htx_add_stline(htx, HTX_BLK_RES_SL, slflags, ist("HTTP/1.1"), ist(status), ist(reason));
4723 if (!sl)
4724 goto fail;
4725 sl->info.res.status = reply->status;
4726
4727 clen = (body ? ultoa(b_data(body)) : "0");
4728 ctype = reply->ctype;
4729
4730 if (!LIST_ISEMPTY(&reply->hdrs)) {
4731 struct http_reply_hdr *hdr;
4732 struct buffer *value = alloc_trash_chunk();
4733
4734 if (!value)
4735 goto fail;
4736
4737 list_for_each_entry(hdr, &reply->hdrs, list) {
4738 chunk_reset(value);
4739 value->data = build_logline(s, value->area, value->size, &hdr->value);
4740 if (b_data(value) && !htx_add_header(htx, hdr->name, ist2(b_head(value), b_data(value)))) {
4741 free_trash_chunk(value);
4742 goto fail;
4743 }
4744 chunk_reset(value);
4745 }
4746 free_trash_chunk(value);
4747 }
4748
4749 if (!htx_add_header(htx, ist("content-length"), ist(clen)) ||
4750 (body && b_data(body) && ctype && !htx_add_header(htx, ist("content-type"), ist(ctype))) ||
4751 !htx_add_endof(htx, HTX_BLK_EOH) ||
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004752 (body && b_data(body) && !htx_add_data_atonce(htx, ist2(b_head(body), b_data(body)))))
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004753 goto fail;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004754
4755 htx->flags |= HTX_FL_EOM;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004756 }
4757
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004758 leave:
4759 if (reply->type == HTTP_REPLY_LOGFMT)
4760 free_trash_chunk(body);
4761 return ret;
4762
4763 fail_alloc:
4764 if (!(s->flags & SF_ERR_MASK))
4765 s->flags |= SF_ERR_RESOURCE;
Christopher Faulet97e466c2020-05-15 15:12:47 +02004766 /* fall through */
4767 fail:
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004768 ret = -1;
4769 goto leave;
Christopher Faulet97e466c2020-05-15 15:12:47 +02004770}
4771
4772/* Send an http reply to the client. On success, it returns 0. If an error
Christopher Faulet40e6b552020-06-25 16:04:50 +02004773 * occurs -1 is returned and the response channel is truncated, removing this
4774 * way the faulty reply. This function may fail when the reply is formatted
4775 * (http_reply_to_htx) or when the reply is forwarded
4776 * (http_forward_proxy_resp). On the last case, it is because a
4777 * http-after-response rule fails.
Christopher Faulet97e466c2020-05-15 15:12:47 +02004778 */
4779int http_reply_message(struct stream *s, struct http_reply *reply)
4780{
4781 struct channel *res = &s->res;
4782 struct htx *htx = htx_from_buf(&res->buf);
4783
4784 if (s->txn->status == -1)
4785 s->txn->status = reply->status;
4786 channel_htx_truncate(res, htx);
4787
4788 if (http_reply_to_htx(s, htx, reply) == -1)
4789 goto fail;
4790
4791 htx_to_buf(htx, &s->res.buf);
4792 if (!http_forward_proxy_resp(s, 1))
4793 goto fail;
4794 return 0;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004795
4796 fail:
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004797 channel_htx_truncate(res, htx);
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004798 if (!(s->flags & SF_ERR_MASK))
4799 s->flags |= SF_ERR_PRXCOND;
Christopher Faulet97e466c2020-05-15 15:12:47 +02004800 return -1;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004801}
4802
Christopher Faulet50264b42022-03-30 19:39:30 +02004803/* Return the error message corresponding to s->conn_err_type. It is assumed
Christopher Faulet304cc402019-07-15 15:46:28 +02004804 * that the server side is closed. Note that err_type is actually a
4805 * bitmask, where almost only aborts may be cumulated with other
4806 * values. We consider that aborted operations are more important
4807 * than timeouts or errors due to the fact that nobody else in the
4808 * logs might explain incomplete retries. All others should avoid
4809 * being cumulated. It should normally not be possible to have multiple
4810 * aborts at once, but just in case, the first one in sequence is reported.
4811 * Note that connection errors appearing on the second request of a keep-alive
4812 * connection are not reported since this allows the client to retry.
4813 */
Christopher Faulet0eb32c02022-04-04 11:06:31 +02004814void http_return_srv_error(struct stream *s, struct conn_stream *cs)
Christopher Faulet304cc402019-07-15 15:46:28 +02004815{
Christopher Faulet50264b42022-03-30 19:39:30 +02004816 int err_type = s->conn_err_type;
Christopher Faulet304cc402019-07-15 15:46:28 +02004817
4818 /* set s->txn->status for http_error_message(s) */
Christopher Faulet50264b42022-03-30 19:39:30 +02004819 if (err_type & STRM_ET_QUEUE_ABRT) {
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004820 s->txn->status = -1;
Christopher Faulet0eb32c02022-04-04 11:06:31 +02004821 http_server_error(s, cs, SF_ERR_CLICL, SF_FINST_Q, NULL);
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004822 }
Christopher Faulet50264b42022-03-30 19:39:30 +02004823 else if (err_type & STRM_ET_CONN_ABRT) {
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004824 s->txn->status = -1;
Christopher Faulet0eb32c02022-04-04 11:06:31 +02004825 http_server_error(s, cs, SF_ERR_CLICL, SF_FINST_C, NULL);
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004826 }
Christopher Faulet50264b42022-03-30 19:39:30 +02004827 else if (err_type & STRM_ET_QUEUE_TO) {
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004828 s->txn->status = 503;
Christopher Faulet0eb32c02022-04-04 11:06:31 +02004829 http_server_error(s, cs, SF_ERR_SRVTO, SF_FINST_Q,
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004830 http_error_message(s));
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004831 }
Christopher Faulet50264b42022-03-30 19:39:30 +02004832 else if (err_type & STRM_ET_QUEUE_ERR) {
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004833 s->txn->status = 503;
Christopher Faulet0eb32c02022-04-04 11:06:31 +02004834 http_server_error(s, cs, SF_ERR_SRVCL, SF_FINST_Q,
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004835 http_error_message(s));
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004836 }
Christopher Faulet50264b42022-03-30 19:39:30 +02004837 else if (err_type & STRM_ET_CONN_TO) {
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004838 s->txn->status = 503;
Christopher Faulet0eb32c02022-04-04 11:06:31 +02004839 http_server_error(s, cs, SF_ERR_SRVTO, SF_FINST_C,
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004840 (s->txn->flags & TX_NOT_FIRST) ? NULL :
4841 http_error_message(s));
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004842 }
Christopher Faulet50264b42022-03-30 19:39:30 +02004843 else if (err_type & STRM_ET_CONN_ERR) {
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004844 s->txn->status = 503;
Christopher Faulet0eb32c02022-04-04 11:06:31 +02004845 http_server_error(s, cs, SF_ERR_SRVCL, SF_FINST_C,
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004846 (s->flags & SF_SRV_REUSED) ? NULL :
4847 http_error_message(s));
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004848 }
Christopher Faulet50264b42022-03-30 19:39:30 +02004849 else if (err_type & STRM_ET_CONN_RES) {
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004850 s->txn->status = 503;
Christopher Faulet0eb32c02022-04-04 11:06:31 +02004851 http_server_error(s, cs, SF_ERR_RESOURCE, SF_FINST_C,
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004852 (s->txn->flags & TX_NOT_FIRST) ? NULL :
4853 http_error_message(s));
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004854 }
Christopher Faulet50264b42022-03-30 19:39:30 +02004855 else { /* STRM_ET_CONN_OTHER and others */
Christopher Faulet304cc402019-07-15 15:46:28 +02004856 s->txn->status = 500;
Christopher Faulet0eb32c02022-04-04 11:06:31 +02004857 http_server_error(s, cs, SF_ERR_INTERNAL, SF_FINST_C,
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004858 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004859 }
4860}
4861
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004862
Christopher Faulet4a28a532019-03-01 11:19:40 +01004863/* Handle Expect: 100-continue for HTTP/1.1 messages if necessary. It returns 0
4864 * on success and -1 on error.
4865 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004866static int http_handle_expect_hdr(struct stream *s, struct htx *htx, struct http_msg *msg)
Christopher Faulet4a28a532019-03-01 11:19:40 +01004867{
4868 /* If we have HTTP/1.1 message with a body and Expect: 100-continue,
4869 * then we must send an HTTP/1.1 100 Continue intermediate response.
4870 */
4871 if (msg->msg_state == HTTP_MSG_BODY && (msg->flags & HTTP_MSGF_VER_11) &&
4872 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
4873 struct ist hdr = { .ptr = "Expect", .len = 6 };
4874 struct http_hdr_ctx ctx;
4875
4876 ctx.blk = NULL;
4877 /* Expect is allowed in 1.1, look for it */
4878 if (http_find_header(htx, hdr, &ctx, 0) &&
4879 unlikely(isteqi(ctx.value, ist2("100-continue", 12)))) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004880 if (http_reply_100_continue(s) == -1)
Christopher Faulet4a28a532019-03-01 11:19:40 +01004881 return -1;
4882 http_remove_header(htx, &ctx);
4883 }
4884 }
4885 return 0;
4886}
4887
Christopher Faulet23a3c792018-11-28 10:01:23 +01004888/* Send a 100-Continue response to the client. It returns 0 on success and -1
4889 * on error. The response channel is updated accordingly.
4890 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004891static int http_reply_100_continue(struct stream *s)
Christopher Faulet23a3c792018-11-28 10:01:23 +01004892{
4893 struct channel *res = &s->res;
4894 struct htx *htx = htx_from_buf(&res->buf);
4895 struct htx_sl *sl;
4896 unsigned int flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|
4897 HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
Christopher Faulet23a3c792018-11-28 10:01:23 +01004898
4899 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
4900 ist("HTTP/1.1"), ist("100"), ist("Continue"));
4901 if (!sl)
4902 goto fail;
4903 sl->info.res.status = 100;
4904
Christopher Faulet1d5ec092019-06-26 14:23:54 +02004905 if (!htx_add_endof(htx, HTX_BLK_EOH))
Christopher Faulet23a3c792018-11-28 10:01:23 +01004906 goto fail;
4907
Christopher Fauleta72a7e42020-01-28 09:28:11 +01004908 if (!http_forward_proxy_resp(s, 0))
4909 goto fail;
Christopher Faulet23a3c792018-11-28 10:01:23 +01004910 return 0;
4911
4912 fail:
4913 /* If an error occurred, remove the incomplete HTTP response from the
4914 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004915 channel_htx_truncate(res, htx);
Christopher Faulet23a3c792018-11-28 10:01:23 +01004916 return -1;
4917}
4918
Christopher Faulet12c51e22018-11-28 15:59:42 +01004919
Christopher Faulet0f226952018-10-22 09:29:56 +02004920/*
4921 * Capture headers from message <htx> according to header list <cap_hdr>, and
4922 * fill the <cap> pointers appropriately.
4923 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004924static void http_capture_headers(struct htx *htx, char **cap, struct cap_hdr *cap_hdr)
Christopher Faulet0f226952018-10-22 09:29:56 +02004925{
4926 struct cap_hdr *h;
4927 int32_t pos;
4928
Christopher Fauleta3f15502019-05-13 15:27:23 +02004929 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet0f226952018-10-22 09:29:56 +02004930 struct htx_blk *blk = htx_get_blk(htx, pos);
4931 enum htx_blk_type type = htx_get_blk_type(blk);
4932 struct ist n, v;
4933
4934 if (type == HTX_BLK_EOH)
4935 break;
4936 if (type != HTX_BLK_HDR)
4937 continue;
4938
4939 n = htx_get_blk_name(htx, blk);
4940
4941 for (h = cap_hdr; h; h = h->next) {
4942 if (h->namelen && (h->namelen == n.len) &&
4943 (strncasecmp(n.ptr, h->name, h->namelen) == 0)) {
4944 if (cap[h->index] == NULL)
4945 cap[h->index] =
4946 pool_alloc(h->pool);
4947
4948 if (cap[h->index] == NULL) {
4949 ha_alert("HTTP capture : out of memory.\n");
4950 break;
4951 }
4952
4953 v = htx_get_blk_value(htx, blk);
Tim Duesterhus2471f5c2021-11-08 09:05:01 +01004954 v = isttrim(v, h->len);
Christopher Faulet0f226952018-10-22 09:29:56 +02004955
4956 memcpy(cap[h->index], v.ptr, v.len);
4957 cap[h->index][v.len]=0;
4958 }
4959 }
4960 }
4961}
4962
Christopher Faulet0b6bdc52018-10-24 11:05:36 +02004963/* Delete a value in a header between delimiters <from> and <next>. The header
4964 * itself is delimited by <start> and <end> pointers. The number of characters
4965 * displaced is returned, and the pointer to the first delimiter is updated if
4966 * required. The function tries as much as possible to respect the following
4967 * principles :
4968 * - replace <from> delimiter by the <next> one unless <from> points to <start>,
4969 * in which case <next> is simply removed
4970 * - set exactly one space character after the new first delimiter, unless there
4971 * are not enough characters in the block being moved to do so.
4972 * - remove unneeded spaces before the previous delimiter and after the new
4973 * one.
4974 *
4975 * It is the caller's responsibility to ensure that :
4976 * - <from> points to a valid delimiter or <start> ;
4977 * - <next> points to a valid delimiter or <end> ;
4978 * - there are non-space chars before <from>.
4979 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004980static int http_del_hdr_value(char *start, char *end, char **from, char *next)
Christopher Faulet0b6bdc52018-10-24 11:05:36 +02004981{
4982 char *prev = *from;
4983
4984 if (prev == start) {
4985 /* We're removing the first value. eat the semicolon, if <next>
4986 * is lower than <end> */
4987 if (next < end)
4988 next++;
4989
4990 while (next < end && HTTP_IS_SPHT(*next))
4991 next++;
4992 }
4993 else {
4994 /* Remove useless spaces before the old delimiter. */
4995 while (HTTP_IS_SPHT(*(prev-1)))
4996 prev--;
4997 *from = prev;
4998
4999 /* copy the delimiter and if possible a space if we're
5000 * not at the end of the line.
5001 */
5002 if (next < end) {
5003 *prev++ = *next++;
5004 if (prev + 1 < next)
5005 *prev++ = ' ';
5006 while (next < end && HTTP_IS_SPHT(*next))
5007 next++;
5008 }
5009 }
5010 memmove(prev, next, end - next);
5011 return (prev - next);
5012}
5013
Christopher Faulet0f226952018-10-22 09:29:56 +02005014
5015/* Formats the start line of the request (without CRLF) and puts it in <str> and
Joseph Herlantc42c0e92018-11-25 10:43:27 -08005016 * return the written length. The line can be truncated if it exceeds <len>.
Christopher Faulet0f226952018-10-22 09:29:56 +02005017 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005018static size_t http_fmt_req_line(const struct htx_sl *sl, char *str, size_t len)
Christopher Faulet0f226952018-10-22 09:29:56 +02005019{
5020 struct ist dst = ist2(str, 0);
5021
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005022 if (istcat(&dst, htx_sl_req_meth(sl), len) == -1)
Christopher Faulet0f226952018-10-22 09:29:56 +02005023 goto end;
5024 if (dst.len + 1 > len)
5025 goto end;
5026 dst.ptr[dst.len++] = ' ';
5027
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005028 if (istcat(&dst, htx_sl_req_uri(sl), len) == -1)
Christopher Faulet0f226952018-10-22 09:29:56 +02005029 goto end;
5030 if (dst.len + 1 > len)
5031 goto end;
5032 dst.ptr[dst.len++] = ' ';
5033
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005034 istcat(&dst, htx_sl_req_vsn(sl), len);
Christopher Faulet0f226952018-10-22 09:29:56 +02005035 end:
5036 return dst.len;
5037}
5038
5039/*
5040 * Print a debug line with a start line.
5041 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005042static void http_debug_stline(const char *dir, struct stream *s, const struct htx_sl *sl)
Christopher Faulet0f226952018-10-22 09:29:56 +02005043{
5044 struct session *sess = strm_sess(s);
5045 int max;
5046
5047 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
5048 dir,
Willy Tarreau88bc8002021-12-06 07:01:02 +00005049 objt_conn(sess->origin) ? (unsigned short)__objt_conn(sess->origin)->handle.fd : -1,
Christopher Faulet693b23b2022-02-28 09:09:05 +01005050 cs_conn(s->csb) ? (unsigned short)(__cs_conn(s->csb))->handle.fd : -1);
Christopher Faulet0f226952018-10-22 09:29:56 +02005051
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005052 max = HTX_SL_P1_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02005053 UBOUND(max, trash.size - trash.data - 3);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005054 chunk_memcat(&trash, HTX_SL_P1_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02005055 trash.area[trash.data++] = ' ';
5056
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005057 max = HTX_SL_P2_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02005058 UBOUND(max, trash.size - trash.data - 2);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005059 chunk_memcat(&trash, HTX_SL_P2_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02005060 trash.area[trash.data++] = ' ';
5061
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005062 max = HTX_SL_P3_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02005063 UBOUND(max, trash.size - trash.data - 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005064 chunk_memcat(&trash, HTX_SL_P3_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02005065 trash.area[trash.data++] = '\n';
5066
Willy Tarreau2e8ab6b2020-03-14 11:03:20 +01005067 DISGUISE(write(1, trash.area, trash.data));
Christopher Faulet0f226952018-10-22 09:29:56 +02005068}
5069
5070/*
5071 * Print a debug line with a header.
5072 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005073static 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 +02005074{
5075 struct session *sess = strm_sess(s);
5076 int max;
5077
5078 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
5079 dir,
Willy Tarreau88bc8002021-12-06 07:01:02 +00005080 objt_conn(sess->origin) ? (unsigned short)__objt_conn(sess->origin)->handle.fd : -1,
Christopher Faulet693b23b2022-02-28 09:09:05 +01005081 cs_conn(s->csb) ? (unsigned short)(__cs_conn(s->csb))->handle.fd : -1);
Christopher Faulet0f226952018-10-22 09:29:56 +02005082
5083 max = n.len;
5084 UBOUND(max, trash.size - trash.data - 3);
5085 chunk_memcat(&trash, n.ptr, max);
5086 trash.area[trash.data++] = ':';
5087 trash.area[trash.data++] = ' ';
5088
5089 max = v.len;
5090 UBOUND(max, trash.size - trash.data - 1);
5091 chunk_memcat(&trash, v.ptr, max);
5092 trash.area[trash.data++] = '\n';
5093
Willy Tarreau2e8ab6b2020-03-14 11:03:20 +01005094 DISGUISE(write(1, trash.area, trash.data));
Christopher Faulet0f226952018-10-22 09:29:56 +02005095}
5096
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005097void http_txn_reset_req(struct http_txn *txn)
5098{
Christopher Faulet1aea50e2020-01-17 16:03:53 +01005099 txn->req.flags = 0;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005100 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
5101}
5102
5103void http_txn_reset_res(struct http_txn *txn)
5104{
Christopher Faulet1aea50e2020-01-17 16:03:53 +01005105 txn->rsp.flags = 0;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005106 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
5107}
5108
5109/*
Christopher Faulet75f619a2021-03-08 19:12:58 +01005110 * Create and initialize a new HTTP transaction for stream <s>. This should be
5111 * used before processing any new request. It returns the transaction or NLULL
5112 * on error.
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005113 */
Christopher Faulet75f619a2021-03-08 19:12:58 +01005114struct http_txn *http_create_txn(struct stream *s)
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005115{
Christopher Faulet75f619a2021-03-08 19:12:58 +01005116 struct http_txn *txn;
Christopher Faulet95a61e82021-12-22 14:22:03 +01005117 struct conn_stream *cs = s->csf;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005118
Christopher Faulet75f619a2021-03-08 19:12:58 +01005119 txn = pool_alloc(pool_head_http_txn);
5120 if (!txn)
5121 return NULL;
5122 s->txn = txn;
5123
Christopher Faulete9e48202022-03-22 18:13:29 +01005124 txn->flags = ((cs && cs->endp->flags & CS_EP_NOT_FIRST) ? TX_NOT_FIRST : 0);
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005125 txn->status = -1;
Christopher Faulet5cb513a2020-05-13 17:56:56 +02005126 txn->http_reply = NULL;
Christopher Faulete05bf9e2022-03-29 15:23:40 +02005127 txn->l7_buffer = BUF_NULL;
Willy Tarreau8b507582020-02-25 09:35:07 +01005128 write_u32(txn->cache_hash, 0);
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005129
5130 txn->cookie_first_date = 0;
5131 txn->cookie_last_date = 0;
5132
5133 txn->srv_cookie = NULL;
5134 txn->cli_cookie = NULL;
5135 txn->uri = NULL;
5136
5137 http_txn_reset_req(txn);
5138 http_txn_reset_res(txn);
5139
5140 txn->req.chn = &s->req;
5141 txn->rsp.chn = &s->res;
5142
5143 txn->auth.method = HTTP_AUTH_UNKNOWN;
5144
Willy Tarreaub7bfcb32021-08-31 08:13:25 +02005145 vars_init_head(&s->vars_txn, SCOPE_TXN);
5146 vars_init_head(&s->vars_reqres, SCOPE_REQ);
Christopher Faulet75f619a2021-03-08 19:12:58 +01005147
5148 return txn;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005149}
5150
5151/* to be used at the end of a transaction */
Christopher Faulet75f619a2021-03-08 19:12:58 +01005152void http_destroy_txn(struct stream *s)
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005153{
5154 struct http_txn *txn = s->txn;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005155
5156 /* these ones will have been dynamically allocated */
5157 pool_free(pool_head_requri, txn->uri);
5158 pool_free(pool_head_capture, txn->cli_cookie);
5159 pool_free(pool_head_capture, txn->srv_cookie);
Tim Duesterhusa17e6622020-03-05 20:19:02 +01005160 pool_free(pool_head_uniqueid, s->unique_id.ptr);
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005161
Tim Duesterhusa17e6622020-03-05 20:19:02 +01005162 s->unique_id = IST_NULL;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005163 txn->uri = NULL;
5164 txn->srv_cookie = NULL;
5165 txn->cli_cookie = NULL;
5166
Christopher Faulet59399252019-11-07 14:27:52 +01005167 if (!LIST_ISEMPTY(&s->vars_txn.head))
5168 vars_prune(&s->vars_txn, s->sess, s);
5169 if (!LIST_ISEMPTY(&s->vars_reqres.head))
5170 vars_prune(&s->vars_reqres, s->sess, s);
Christopher Faulet75f619a2021-03-08 19:12:58 +01005171
Christopher Faulete05bf9e2022-03-29 15:23:40 +02005172 b_free(&txn->l7_buffer);
5173
Christopher Faulet75f619a2021-03-08 19:12:58 +01005174 pool_free(pool_head_http_txn, txn);
5175 s->txn = NULL;
Christopher Faulet59399252019-11-07 14:27:52 +01005176}
5177
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005178
5179DECLARE_POOL(pool_head_http_txn, "http_txn", sizeof(struct http_txn));
Christopher Faulet0f226952018-10-22 09:29:56 +02005180
Christopher Fauletf4eb75d2018-10-11 15:55:07 +02005181/*
5182 * Local variables:
5183 * c-indent-level: 8
5184 * c-basic-offset: 8
5185 * End:
5186 */