blob: faa2d239d81457caf85ff8cbe4a4438bbce6ec75 [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 Faulet18c13d32022-05-16 11:43:10 +020063static enum rule_result http_req_restrict_header_names(struct stream *s, struct htx *htx, struct proxy *px);
Christopher Faulet3e964192018-10-24 11:39:23 +020064
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020065static void http_manage_client_side_cookies(struct stream *s, struct channel *req);
66static void http_manage_server_side_cookies(struct stream *s, struct channel *res);
Christopher Fauletfcda7c62018-10-24 11:56:22 +020067
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020068static int http_stats_check_uri(struct stream *s, struct http_txn *txn, struct proxy *backend);
69static int http_handle_stats(struct stream *s, struct channel *req);
Christopher Faulet377c5a52018-10-24 21:21:30 +020070
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020071static int http_handle_expect_hdr(struct stream *s, struct htx *htx, struct http_msg *msg);
72static int http_reply_100_continue(struct stream *s);
Christopher Faulet23a3c792018-11-28 10:01:23 +010073
Christopher Faulete0768eb2018-10-03 16:38:02 +020074/* This stream analyser waits for a complete HTTP request. It returns 1 if the
75 * processing can continue on next analysers, or zero if it either needs more
76 * data or wants to immediately abort the request (eg: timeout, error, ...). It
77 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req.analysers
78 * when it has nothing left to do, and may remove any analyser when it wants to
79 * abort.
80 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020081int http_wait_for_request(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +020082{
Christopher Faulet9768c262018-10-22 09:34:31 +020083
Christopher Faulete0768eb2018-10-03 16:38:02 +020084 /*
Christopher Faulet9768c262018-10-22 09:34:31 +020085 * We will analyze a complete HTTP request to check the its syntax.
Christopher Faulete0768eb2018-10-03 16:38:02 +020086 *
Christopher Faulet9768c262018-10-22 09:34:31 +020087 * Once the start line and all headers are received, we may perform a
88 * capture of the error (if any), and we will set a few fields. We also
89 * check for monitor-uri, logging and finally headers capture.
Christopher Faulete0768eb2018-10-03 16:38:02 +020090 */
Christopher Faulete0768eb2018-10-03 16:38:02 +020091 struct session *sess = s->sess;
92 struct http_txn *txn = s->txn;
93 struct http_msg *msg = &txn->req;
Christopher Faulet9768c262018-10-22 09:34:31 +020094 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +010095 struct htx_sl *sl;
Christopher Faulete0768eb2018-10-03 16:38:02 +020096
Christopher Fauleteea8fc72019-11-05 16:18:10 +010097 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +020098
Christopher Fauletda46a0d2021-01-21 17:32:58 +010099 if (unlikely(!IS_HTX_STRM(s))) {
100 /* It is only possible when a TCP stream is upgrade to HTTP.
101 * There is a transition period during which there is no
102 * data. The stream is still in raw mode and SF_IGNORE flag is
103 * still set. When this happens, the new mux is responsible to
Ilya Shipitsinacf84592021-02-06 22:29:08 +0500104 * handle all errors. Thus we may leave immediately.
Christopher Fauletda46a0d2021-01-21 17:32:58 +0100105 */
106 BUG_ON(!(s->flags & SF_IGNORE) || !c_empty(&s->req));
Christopher Faulet9768c262018-10-22 09:34:31 +0200107
Christopher Faulet97b3a612021-03-15 17:10:12 +0100108 /* Don't connect for now */
109 channel_dont_connect(req);
110
111 /* A SHUTR at this stage means we are performing a "destructive"
112 * HTTP upgrade (TCP>H2). In this case, we can leave.
113 */
114 if (req->flags & CF_SHUTR) {
115 s->logs.logwait = 0;
116 s->logs.level = 0;
117 channel_abort(&s->req);
118 channel_abort(&s->res);
119 req->analysers &= AN_REQ_FLT_END;
120 req->analyse_exp = TICK_ETERNITY;
121 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA, s);
122 return 1;
123 }
Christopher Fauletda46a0d2021-01-21 17:32:58 +0100124 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA, s);
125 return 0;
126 }
127
128 htx = htxbuf(&req->buf);
Christopher Faulet8bebd2f2020-10-06 17:54:56 +0200129
Willy Tarreau4236f032019-03-05 10:43:32 +0100130 /* Parsing errors are caught here */
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200131 if (htx->flags & (HTX_FL_PARSING_ERROR|HTX_FL_PROCESSING_ERROR)) {
Willy Tarreau4236f032019-03-05 10:43:32 +0100132 stream_inc_http_req_ctr(s);
Emeric Brun28976442020-10-07 08:50:09 +0200133 proxy_inc_fe_req_ctr(sess->listener, sess->fe);
Christopher Fauletbf7175f2021-02-10 14:58:01 +0100134 if (htx->flags & HTX_FL_PARSING_ERROR) {
135 stream_inc_http_err_ctr(s);
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200136 goto return_bad_req;
Christopher Fauletbf7175f2021-02-10 14:58:01 +0100137 }
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200138 else
139 goto return_int_err;
Willy Tarreau4236f032019-03-05 10:43:32 +0100140 }
141
Christopher Faulete0768eb2018-10-03 16:38:02 +0200142 /* we're speaking HTTP here, so let's speak HTTP to the client */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200143 s->srv_error = http_return_srv_error;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200144
Christopher Faulet9768c262018-10-22 09:34:31 +0200145 msg->msg_state = HTTP_MSG_BODY;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200146 stream_inc_http_req_ctr(s);
Emeric Brun28976442020-10-07 08:50:09 +0200147 proxy_inc_fe_req_ctr(sess->listener, sess->fe); /* one more valid request for this FE */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200148
Christopher Faulet9768c262018-10-22 09:34:31 +0200149 /* kill the pending keep-alive timeout */
Christopher Faulet9768c262018-10-22 09:34:31 +0200150 req->analyse_exp = TICK_ETERNITY;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200151
Christopher Faulet29f17582019-05-23 11:03:26 +0200152 BUG_ON(htx_get_first_type(htx) != HTX_BLK_REQ_SL);
Christopher Faulet297fbb42019-05-13 14:41:27 +0200153 sl = http_get_stline(htx);
Christopher Faulet03599112018-11-27 11:21:21 +0100154
Christopher Faulet9768c262018-10-22 09:34:31 +0200155 /* 0: we might have to print this header in debug mode */
156 if (unlikely((global.mode & MODE_DEBUG) &&
157 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
158 int32_t pos;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200159
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200160 http_debug_stline("clireq", s, sl);
Christopher Faulet9768c262018-10-22 09:34:31 +0200161
Christopher Fauleta3f15502019-05-13 15:27:23 +0200162 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet9768c262018-10-22 09:34:31 +0200163 struct htx_blk *blk = htx_get_blk(htx, pos);
164 enum htx_blk_type type = htx_get_blk_type(blk);
165
166 if (type == HTX_BLK_EOH)
167 break;
168 if (type != HTX_BLK_HDR)
169 continue;
170
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200171 http_debug_hdr("clihdr", s,
172 htx_get_blk_name(htx, blk),
173 htx_get_blk_value(htx, blk));
Christopher Faulet9768c262018-10-22 09:34:31 +0200174 }
175 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200176
177 /*
Christopher Faulet03599112018-11-27 11:21:21 +0100178 * 1: identify the method and the version. Also set HTTP flags
Christopher Faulete0768eb2018-10-03 16:38:02 +0200179 */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100180 txn->meth = sl->info.req.meth;
Christopher Faulet03599112018-11-27 11:21:21 +0100181 if (sl->flags & HTX_SL_F_VER_11)
Christopher Faulet9768c262018-10-22 09:34:31 +0200182 msg->flags |= HTTP_MSGF_VER_11;
Christopher Faulet03599112018-11-27 11:21:21 +0100183 msg->flags |= HTTP_MSGF_XFER_LEN;
Christopher Faulet2a408542020-11-20 14:22:37 +0100184 if (sl->flags & HTX_SL_F_CLEN)
185 msg->flags |= HTTP_MSGF_CNT_LEN;
186 else if (sl->flags & HTX_SL_F_CHNK)
187 msg->flags |= HTTP_MSGF_TE_CHNK;
Christopher Fauletb2db4fa2018-11-27 16:51:09 +0100188 if (sl->flags & HTX_SL_F_BODYLESS)
189 msg->flags |= HTTP_MSGF_BODYLESS;
Christopher Faulet576c3582021-01-08 15:53:01 +0100190 if (sl->flags & HTX_SL_F_CONN_UPG)
191 msg->flags |= HTTP_MSGF_CONN_UPG;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200192
193 /* we can make use of server redirect on GET and HEAD */
194 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
195 s->flags |= SF_REDIRECTABLE;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100196 else if (txn->meth == HTTP_METH_OTHER && isteqi(htx_sl_req_meth(sl), ist("PRI"))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200197 /* PRI is reserved for the HTTP/2 preface */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200198 goto return_bad_req;
199 }
200
201 /*
Christopher Faulet6072beb2020-02-18 15:34:58 +0100202 * 2: check if the URI matches the monitor_uri. We have to do this for
203 * every request which gets in, because the monitor-uri is defined by
204 * the frontend. If the monitor-uri starts with a '/', the matching is
205 * done against the request's path. Otherwise, the request's uri is
206 * used. It is a workaround to let HTTP/2 health-checks work as
207 * expected.
Christopher Faulete0768eb2018-10-03 16:38:02 +0200208 */
Tim Duesterhus4b1fcaa2022-03-05 00:52:40 +0100209 if (unlikely(isttest(sess->fe->monitor_uri))) {
210 const struct ist monitor_uri = sess->fe->monitor_uri;
Amaury Denoyellec453f952021-07-06 11:40:12 +0200211 struct http_uri_parser parser = http_uri_parser_init(htx_sl_req_uri(sl));
Christopher Faulete0768eb2018-10-03 16:38:02 +0200212
Amaury Denoyelle5a9bd372021-07-06 11:23:10 +0200213 if ((istptr(monitor_uri)[0] == '/' &&
Amaury Denoyellec453f952021-07-06 11:40:12 +0200214 isteq(http_parse_path(&parser), monitor_uri)) ||
Amaury Denoyelle5a9bd372021-07-06 11:23:10 +0200215 isteq(htx_sl_req_uri(sl), monitor_uri)) {
216 /*
217 * We have found the monitor URI
218 */
219 struct acl_cond *cond;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200220
Amaury Denoyelle5a9bd372021-07-06 11:23:10 +0200221 s->flags |= SF_MONITOR;
222 _HA_ATOMIC_INC(&sess->fe->fe_counters.intercepted_req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200223
Amaury Denoyelle5a9bd372021-07-06 11:23:10 +0200224 /* Check if we want to fail this monitor request or not */
225 list_for_each_entry(cond, &sess->fe->mon_fail_cond, list) {
226 int ret = acl_exec_cond(cond, sess->fe, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200227
Amaury Denoyelle5a9bd372021-07-06 11:23:10 +0200228 ret = acl_pass(ret);
229 if (cond->pol == ACL_COND_UNLESS)
230 ret = !ret;
231
232 if (ret) {
233 /* we fail this request, let's return 503 service unavail */
234 txn->status = 503;
235 if (!(s->flags & SF_ERR_MASK))
236 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
237 goto return_prx_cond;
238 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200239 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200240
Amaury Denoyelle5a9bd372021-07-06 11:23:10 +0200241 /* nothing to fail, let's reply normally */
242 txn->status = 200;
243 if (!(s->flags & SF_ERR_MASK))
244 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
245 goto return_prx_cond;
246 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200247 }
248
249 /*
250 * 3: Maybe we have to copy the original REQURI for the logs ?
251 * Note: we cannot log anymore if the request has been
252 * classified as invalid.
253 */
254 if (unlikely(s->logs.logwait & LW_REQ)) {
255 /* we have a complete HTTP request that we must log */
256 if ((txn->uri = pool_alloc(pool_head_requri)) != NULL) {
Christopher Faulet9768c262018-10-22 09:34:31 +0200257 size_t len;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200258
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200259 len = http_fmt_req_line(sl, txn->uri, global.tune.requri_len - 1);
Christopher Faulet9768c262018-10-22 09:34:31 +0200260 txn->uri[len] = 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200261
262 if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT)))
263 s->do_log(s);
264 } else {
265 ha_alert("HTTP logging : out of memory.\n");
266 }
267 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200268
Christopher Faulete0768eb2018-10-03 16:38:02 +0200269 /* if the frontend has "option http-use-proxy-header", we'll check if
270 * we have what looks like a proxied connection instead of a connection,
271 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
272 * Note that this is *not* RFC-compliant, however browsers and proxies
273 * happen to do that despite being non-standard :-(
274 * We consider that a request not beginning with either '/' or '*' is
275 * a proxied connection, which covers both "scheme://location" and
276 * CONNECT ip:port.
277 */
278 if ((sess->fe->options2 & PR_O2_USE_PXHDR) &&
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100279 *HTX_SL_REQ_UPTR(sl) != '/' && *HTX_SL_REQ_UPTR(sl) != '*')
Christopher Faulete0768eb2018-10-03 16:38:02 +0200280 txn->flags |= TX_USE_PX_CONN;
281
Christopher Faulete0768eb2018-10-03 16:38:02 +0200282 /* 5: we may need to capture headers */
283 if (unlikely((s->logs.logwait & LW_REQHDR) && s->req_cap))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200284 http_capture_headers(htx, s->req_cap, sess->fe->req_cap);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200285
Christopher Faulete0768eb2018-10-03 16:38:02 +0200286 /* we may have to wait for the request's body */
Christopher Faulet9768c262018-10-22 09:34:31 +0200287 if (s->be->options & PR_O_WREQ_BODY)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200288 req->analysers |= AN_REQ_HTTP_BODY;
289
290 /*
291 * RFC7234#4:
292 * A cache MUST write through requests with methods
293 * that are unsafe (Section 4.2.1 of [RFC7231]) to
294 * the origin server; i.e., a cache is not allowed
295 * to generate a reply to such a request before
296 * having forwarded the request and having received
297 * a corresponding response.
298 *
299 * RFC7231#4.2.1:
300 * Of the request methods defined by this
301 * specification, the GET, HEAD, OPTIONS, and TRACE
302 * methods are defined to be safe.
303 */
304 if (likely(txn->meth == HTTP_METH_GET ||
305 txn->meth == HTTP_METH_HEAD ||
306 txn->meth == HTTP_METH_OPTIONS ||
307 txn->meth == HTTP_METH_TRACE))
308 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
309
310 /* end of job, return OK */
311 req->analysers &= ~an_bit;
312 req->analyse_exp = TICK_ETERNITY;
Christopher Faulet9768c262018-10-22 09:34:31 +0200313
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100314 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200315 return 1;
316
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200317 return_int_err:
318 txn->status = 500;
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200319 if (!(s->flags & SF_ERR_MASK))
320 s->flags |= SF_ERR_INTERNAL;
Willy Tarreau4781b152021-04-06 13:53:36 +0200321 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
William Lallemand36119de2021-03-08 15:26:48 +0100322 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200323 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200324 goto return_prx_cond;
325
Christopher Faulete0768eb2018-10-03 16:38:02 +0200326 return_bad_req:
Christopher Faulet9768c262018-10-22 09:34:31 +0200327 txn->status = 400;
Willy Tarreau4781b152021-04-06 13:53:36 +0200328 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req);
William Lallemand36119de2021-03-08 15:26:48 +0100329 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200330 _HA_ATOMIC_INC(&sess->listener->counters->failed_req);
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200331 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200332
333 return_prx_cond:
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200334 http_reply_and_close(s, txn->status, http_error_message(s));
335
Christopher Faulete0768eb2018-10-03 16:38:02 +0200336 if (!(s->flags & SF_ERR_MASK))
337 s->flags |= SF_ERR_PRXCOND;
338 if (!(s->flags & SF_FINST_MASK))
339 s->flags |= SF_FINST_R;
340
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100341 DBG_TRACE_DEVEL("leaving on error",
342 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200343 return 0;
344}
345
346
347/* This stream analyser runs all HTTP request processing which is common to
348 * frontends and backends, which means blocking ACLs, filters, connection-close,
349 * reqadd, stats and redirects. This is performed for the designated proxy.
350 * It returns 1 if the processing can continue on next analysers, or zero if it
351 * either needs more data or wants to immediately abort the request (eg: deny,
352 * error, ...).
353 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200354int http_process_req_common(struct stream *s, struct channel *req, int an_bit, struct proxy *px)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200355{
Christopher Fauletd4150ad2021-10-13 15:35:55 +0200356 struct list *def_rules, *rules;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200357 struct session *sess = s->sess;
358 struct http_txn *txn = s->txn;
359 struct http_msg *msg = &txn->req;
Christopher Fauletff2759f2018-10-24 11:13:16 +0200360 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200361 struct redirect_rule *rule;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200362 enum rule_result verdict;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200363 struct connection *conn = objt_conn(sess->origin);
364
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100365 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200366
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100367 htx = htxbuf(&req->buf);
Christopher Fauletff2759f2018-10-24 11:13:16 +0200368
Christopher Faulet1907ccc2019-04-29 13:12:02 +0200369 /* just in case we have some per-backend tracking. Only called the first
370 * execution of the analyser. */
Christopher Fauletd4150ad2021-10-13 15:35:55 +0200371 if (!s->current_rule && !s->current_rule_list)
Christopher Faulet1907ccc2019-04-29 13:12:02 +0200372 stream_inc_be_http_req_ctr(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200373
Christopher Fauletd4150ad2021-10-13 15:35:55 +0200374 def_rules = ((px->defpx && (an_bit == AN_REQ_HTTP_PROCESS_FE || px != sess->fe)) ? &px->defpx->http_req_rules : NULL);
375 rules = &px->http_req_rules;
376
Christopher Faulete0768eb2018-10-03 16:38:02 +0200377 /* evaluate http-request rules */
Christopher Fauletd4150ad2021-10-13 15:35:55 +0200378 if ((def_rules && !LIST_ISEMPTY(def_rules)) || !LIST_ISEMPTY(rules)) {
379 verdict = http_req_get_intercept_rule(px, def_rules, rules, s);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200380
381 switch (verdict) {
382 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
383 goto return_prx_yield;
384
385 case HTTP_RULE_RES_CONT:
386 case HTTP_RULE_RES_STOP: /* nothing to do */
387 break;
388
389 case HTTP_RULE_RES_DENY: /* deny or tarpit */
390 if (txn->flags & TX_CLTARPIT)
391 goto tarpit;
392 goto deny;
393
394 case HTTP_RULE_RES_ABRT: /* abort request, response already sent. Eg: auth */
395 goto return_prx_cond;
396
397 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
398 goto done;
399
400 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
401 goto return_bad_req;
Christopher Faulet3a26bee2019-12-16 12:47:40 +0100402
403 case HTTP_RULE_RES_ERROR: /* failed with a bad request */
404 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200405 }
406 }
407
Christopher Faulet18c13d32022-05-16 11:43:10 +0200408 if (px->options2 & (PR_O2_RSTRICT_REQ_HDR_NAMES_BLK|PR_O2_RSTRICT_REQ_HDR_NAMES_DEL)) {
409 verdict = http_req_restrict_header_names(s, htx, px);
410 if (verdict == HTTP_RULE_RES_DENY)
411 goto deny;
412 }
413
Christopher Faulete0768eb2018-10-03 16:38:02 +0200414 if (conn && (conn->flags & CO_FL_EARLY_DATA) &&
Olivier Houchard220a26c2020-01-23 14:57:36 +0100415 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_SSL_WAIT_HS))) {
Christopher Fauletff2759f2018-10-24 11:13:16 +0200416 struct http_hdr_ctx ctx;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200417
Christopher Fauletff2759f2018-10-24 11:13:16 +0200418 ctx.blk = NULL;
419 if (!http_find_header(htx, ist("Early-Data"), &ctx, 0)) {
420 if (unlikely(!http_add_header(htx, ist("Early-Data"), ist("1"))))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100421 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200422 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200423 }
424
425 /* OK at this stage, we know that the request was accepted according to
426 * the http-request rules, we can check for the stats. Note that the
427 * URI is detected *before* the req* rules in order not to be affected
428 * by a possible reqrep, while they are processed *after* so that a
429 * reqdeny can still block them. This clearly needs to change in 1.6!
430 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200431 if (!s->target && http_stats_check_uri(s, txn, px)) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200432 s->target = &http_stats_applet.obj_type;
Christopher Faulet1336ccf2022-04-12 18:15:16 +0200433 if (unlikely(!cs_applet_create(s->csb, objt_applet(s->target)))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200434 s->logs.tv_request = now;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200435 if (!(s->flags & SF_ERR_MASK))
436 s->flags |= SF_ERR_RESOURCE;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100437 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200438 }
439
440 /* parse the whole stats request and extract the relevant information */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200441 http_handle_stats(s, req);
Christopher Fauletd4150ad2021-10-13 15:35:55 +0200442 verdict = http_req_get_intercept_rule(px, NULL, &px->uri_auth->http_req_rules, s);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200443 /* not all actions implemented: deny, allow, auth */
444
445 if (verdict == HTTP_RULE_RES_DENY) /* stats http-request deny */
446 goto deny;
447
448 if (verdict == HTTP_RULE_RES_ABRT) /* stats auth / stats http-request auth */
449 goto return_prx_cond;
Christopher Faulet3a26bee2019-12-16 12:47:40 +0100450
451 if (verdict == HTTP_RULE_RES_BADREQ) /* failed with a bad request */
452 goto return_bad_req;
453
454 if (verdict == HTTP_RULE_RES_ERROR) /* failed with a bad request */
455 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200456 }
457
Christopher Faulet2571bc62019-03-01 11:44:26 +0100458 /* Proceed with the applets now. */
459 if (unlikely(objt_applet(s->target))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200460 if (sess->fe == s->be) /* report it if the request was intercepted by the frontend */
Willy Tarreau4781b152021-04-06 13:53:36 +0200461 _HA_ATOMIC_INC(&sess->fe->fe_counters.intercepted_req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200462
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200463 if (http_handle_expect_hdr(s, htx, msg) == -1)
Christopher Fauletb8a53712019-12-16 11:29:38 +0100464 goto return_int_err;
Christopher Fauletbcf242a2019-03-01 11:36:26 +0100465
Christopher Faulete0768eb2018-10-03 16:38:02 +0200466 if (!(s->flags & SF_ERR_MASK)) // this is not really an error but it is
467 s->flags |= SF_ERR_LOCAL; // to mark that it comes from the proxy
468 if (!(s->flags & SF_FINST_MASK))
469 s->flags |= SF_FINST_R;
470
Christopher Fauletc2ac5e42021-03-08 18:20:09 +0100471 if (HAS_FILTERS(s))
472 req->analysers |= AN_REQ_FLT_HTTP_HDRS;
473
Christopher Faulete0768eb2018-10-03 16:38:02 +0200474 /* enable the minimally required analyzers to handle keep-alive and compression on the HTTP response */
475 req->analysers &= (AN_REQ_HTTP_BODY | AN_REQ_FLT_HTTP_HDRS | AN_REQ_FLT_END);
476 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
477 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Christopher Fauletbcf242a2019-03-01 11:36:26 +0100478
479 req->flags |= CF_SEND_DONTWAIT;
480 s->flags |= SF_ASSIGNED;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200481 goto done;
482 }
483
484 /* check whether we have some ACLs set to redirect this request */
485 list_for_each_entry(rule, &px->redirect_rules, list) {
486 if (rule->cond) {
487 int ret;
488
489 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
490 ret = acl_pass(ret);
491 if (rule->cond->pol == ACL_COND_UNLESS)
492 ret = !ret;
493 if (!ret)
494 continue;
495 }
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200496 if (!http_apply_redirect_rule(rule, s, txn))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100497 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200498 goto done;
499 }
500
501 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
502 * If this happens, then the data will not come immediately, so we must
503 * send all what we have without waiting. Note that due to the small gain
504 * in waiting for the body of the request, it's easier to simply put the
505 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
506 * itself once used.
507 */
508 req->flags |= CF_SEND_DONTWAIT;
509
510 done: /* done with this analyser, continue with next ones that the calling
511 * points will have set, if any.
512 */
513 req->analyse_exp = TICK_ETERNITY;
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +0500514 done_without_exp: /* done with this analyser, but don't reset the analyse_exp. */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200515 req->analysers &= ~an_bit;
Christopher Fauletd4150ad2021-10-13 15:35:55 +0200516 s->current_rule = s->current_rule_list = NULL;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100517 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200518 return 1;
519
520 tarpit:
521 /* Allow cookie logging
522 */
523 if (s->be->cookie_name || sess->fe->capture_name)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200524 http_manage_client_side_cookies(s, req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200525
526 /* When a connection is tarpitted, we use the tarpit timeout,
527 * which may be the same as the connect timeout if unspecified.
528 * If unset, then set it to zero because we really want it to
529 * eventually expire. We build the tarpit as an analyser.
530 */
Christopher Faulet202c6ce2019-01-07 14:57:35 +0100531 channel_htx_erase(&s->req, htx);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200532
533 /* wipe the request out so that we can drop the connection early
534 * if the client closes first.
535 */
536 channel_dont_connect(req);
537
Christopher Faulete0768eb2018-10-03 16:38:02 +0200538 req->analysers &= AN_REQ_FLT_END; /* remove switching rules etc... */
539 req->analysers |= AN_REQ_HTTP_TARPIT;
540 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
541 if (!req->analyse_exp)
542 req->analyse_exp = tick_add(now_ms, 0);
543 stream_inc_http_err_ctr(s);
Willy Tarreau4781b152021-04-06 13:53:36 +0200544 _HA_ATOMIC_INC(&sess->fe->fe_counters.denied_req);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100545 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau4781b152021-04-06 13:53:36 +0200546 _HA_ATOMIC_INC(&s->be->be_counters.denied_req);
William Lallemand36119de2021-03-08 15:26:48 +0100547 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200548 _HA_ATOMIC_INC(&sess->listener->counters->denied_req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200549 goto done_without_exp;
550
551 deny: /* this request was blocked (denied) */
552
553 /* Allow cookie logging
554 */
555 if (s->be->cookie_name || sess->fe->capture_name)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200556 http_manage_client_side_cookies(s, req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200557
Christopher Faulete0768eb2018-10-03 16:38:02 +0200558 s->logs.tv_request = now;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200559 stream_inc_http_err_ctr(s);
Willy Tarreau4781b152021-04-06 13:53:36 +0200560 _HA_ATOMIC_INC(&sess->fe->fe_counters.denied_req);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100561 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau4781b152021-04-06 13:53:36 +0200562 _HA_ATOMIC_INC(&s->be->be_counters.denied_req);
William Lallemand36119de2021-03-08 15:26:48 +0100563 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200564 _HA_ATOMIC_INC(&sess->listener->counters->denied_req);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100565 goto return_prx_err;
566
567 return_int_err:
568 txn->status = 500;
569 if (!(s->flags & SF_ERR_MASK))
570 s->flags |= SF_ERR_INTERNAL;
Willy Tarreau4781b152021-04-06 13:53:36 +0200571 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100572 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau4781b152021-04-06 13:53:36 +0200573 _HA_ATOMIC_INC(&s->be->be_counters.internal_errors);
William Lallemand36119de2021-03-08 15:26:48 +0100574 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200575 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100576 goto return_prx_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200577
578 return_bad_req:
Christopher Faulete0768eb2018-10-03 16:38:02 +0200579 txn->status = 400;
Willy Tarreau4781b152021-04-06 13:53:36 +0200580 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req);
William Lallemand36119de2021-03-08 15:26:48 +0100581 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200582 _HA_ATOMIC_INC(&sess->listener->counters->failed_req);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100583 /* fall through */
584
585 return_prx_err:
586 http_reply_and_close(s, txn->status, http_error_message(s));
587 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200588
589 return_prx_cond:
590 if (!(s->flags & SF_ERR_MASK))
591 s->flags |= SF_ERR_PRXCOND;
592 if (!(s->flags & SF_FINST_MASK))
593 s->flags |= SF_FINST_R;
594
595 req->analysers &= AN_REQ_FLT_END;
596 req->analyse_exp = TICK_ETERNITY;
Christopher Fauletd4150ad2021-10-13 15:35:55 +0200597 s->current_rule = s->current_rule_list = NULL;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100598 DBG_TRACE_DEVEL("leaving on error",
599 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200600 return 0;
601
602 return_prx_yield:
603 channel_dont_connect(req);
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100604 DBG_TRACE_DEVEL("waiting for more data",
605 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200606 return 0;
607}
608
609/* This function performs all the processing enabled for the current request.
610 * It returns 1 if the processing can continue on next analysers, or zero if it
611 * needs more data, encounters an error, or wants to immediately abort the
612 * request. It relies on buffers flags, and updates s->req.analysers.
613 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200614int http_process_request(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200615{
616 struct session *sess = s->sess;
617 struct http_txn *txn = s->txn;
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200618 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200619 struct connection *cli_conn = objt_conn(strm_sess(s)->origin);
620
Christopher Faulet8bebd2f2020-10-06 17:54:56 +0200621 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200622
623 /*
624 * Right now, we know that we have processed the entire headers
625 * and that unwanted requests have been filtered out. We can do
626 * whatever we want with the remaining request. Also, now we
627 * may have separate values for ->fe, ->be.
628 */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100629 htx = htxbuf(&req->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200630
631 /*
Christopher Faulete0768eb2018-10-03 16:38:02 +0200632 * 7: Now we can work with the cookies.
633 * Note that doing so might move headers in the request, but
634 * the fields will stay coherent and the URI will not move.
635 * This should only be performed in the backend.
636 */
637 if (s->be->cookie_name || sess->fe->capture_name)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200638 http_manage_client_side_cookies(s, req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200639
Tim Duesterhus2825b4b2020-02-28 15:13:34 +0100640 /* 8: Generate unique ID if a "unique-id-format" is defined.
641 *
642 * A unique ID is generated even when it is not sent to ensure that the ID can make use of
643 * fetches only available in the HTTP request processing stage.
644 */
645 if (!LIST_ISEMPTY(&sess->fe->format_unique_id)) {
Tim Duesterhusa17e6622020-03-05 20:19:02 +0100646 struct ist unique_id = stream_generate_unique_id(s, &sess->fe->format_unique_id);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200647
Tim Duesterhusa17e6622020-03-05 20:19:02 +0100648 if (!isttest(unique_id)) {
Christopher Fauletb8a53712019-12-16 11:29:38 +0100649 if (!(s->flags & SF_ERR_MASK))
650 s->flags |= SF_ERR_RESOURCE;
651 goto return_int_err;
652 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200653
Tim Duesterhus2825b4b2020-02-28 15:13:34 +0100654 /* send unique ID if a "unique-id-header" is defined */
Tim Duesterhus0643b0e2020-03-05 17:56:35 +0100655 if (isttest(sess->fe->header_unique_id) &&
Tim Duesterhus7ad27d42022-05-18 00:22:15 +0200656 unlikely(!http_add_header(htx, sess->fe->header_unique_id, unique_id)))
Tim Duesterhus2825b4b2020-02-28 15:13:34 +0100657 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200658 }
659
660 /*
661 * 9: add X-Forwarded-For if either the frontend or the backend
662 * asks for it.
663 */
664 if ((sess->fe->options | s->be->options) & PR_O_FWDFOR) {
Christopher Faulet8da67aa2022-03-29 17:53:09 +0200665 const struct sockaddr_storage *src = cs_src(s->csf);
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200666 struct http_hdr_ctx ctx = { .blk = NULL };
Tim Duesterhusb50ab842022-03-05 00:52:41 +0100667 struct ist hdr = isttest(s->be->fwdfor_hdr_name) ? s->be->fwdfor_hdr_name : sess->fe->fwdfor_hdr_name;
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200668
Christopher Faulete0768eb2018-10-03 16:38:02 +0200669 if (!((sess->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200670 http_find_header(htx, hdr, &ctx, 0)) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200671 /* The header is set to be added only if none is present
672 * and we found it, so don't do anything.
673 */
674 }
Christopher Faulet8a104ba2021-10-25 07:41:30 +0200675 else if (src && src->ss_family == AF_INET) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200676 /* Add an X-Forwarded-For header unless the source IP is
677 * in the 'except' network range.
678 */
Christopher Faulet8a104ba2021-10-25 07:41:30 +0200679 if (ipcmp2net(src, &sess->fe->except_xff_net) &&
680 ipcmp2net(src, &s->be->except_xff_net)) {
681 unsigned char *pn = (unsigned char *)&((struct sockaddr_in *)src)->sin_addr;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200682
683 /* Note: we rely on the backend to get the header name to be used for
684 * x-forwarded-for, because the header is really meant for the backends.
685 * However, if the backend did not specify any option, we have to rely
686 * on the frontend's header name.
687 */
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200688 chunk_printf(&trash, "%d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
689 if (unlikely(!http_add_header(htx, hdr, ist2(trash.area, trash.data))))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100690 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200691 }
692 }
Christopher Faulet8a104ba2021-10-25 07:41:30 +0200693 else if (src && src->ss_family == AF_INET6) {
Christopher Faulet5d1def62021-02-26 09:19:15 +0100694 /* Add an X-Forwarded-For header unless the source IP is
695 * in the 'except' network range.
Christopher Faulete0768eb2018-10-03 16:38:02 +0200696 */
Christopher Faulet8a104ba2021-10-25 07:41:30 +0200697 if (ipcmp2net(src, &sess->fe->except_xff_net) &&
698 ipcmp2net(src, &s->be->except_xff_net)) {
Christopher Faulet5d1def62021-02-26 09:19:15 +0100699 char pn[INET6_ADDRSTRLEN];
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200700
Christopher Faulet5d1def62021-02-26 09:19:15 +0100701 inet_ntop(AF_INET6,
Christopher Faulet8a104ba2021-10-25 07:41:30 +0200702 (const void *)&((struct sockaddr_in6 *)(src))->sin6_addr,
Christopher Faulet5d1def62021-02-26 09:19:15 +0100703 pn, sizeof(pn));
Christopher Faulete0768eb2018-10-03 16:38:02 +0200704
Christopher Faulet5d1def62021-02-26 09:19:15 +0100705 /* Note: we rely on the backend to get the header name to be used for
706 * x-forwarded-for, because the header is really meant for the backends.
707 * However, if the backend did not specify any option, we have to rely
708 * on the frontend's header name.
709 */
710 chunk_printf(&trash, "%s", pn);
711 if (unlikely(!http_add_header(htx, hdr, ist2(trash.area, trash.data))))
712 goto return_int_err;
713 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200714 }
715 }
716
717 /*
718 * 10: add X-Original-To if either the frontend or the backend
719 * asks for it.
720 */
721 if ((sess->fe->options | s->be->options) & PR_O_ORGTO) {
Christopher Faulet8da67aa2022-03-29 17:53:09 +0200722 const struct sockaddr_storage *dst = cs_dst(s->csf);
Tim Duesterhuse502c3e2022-03-05 00:52:42 +0100723 struct ist hdr = isttest(s->be->orgto_hdr_name) ? s->be->orgto_hdr_name : sess->fe->orgto_hdr_name;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200724
Christopher Faulet8a104ba2021-10-25 07:41:30 +0200725 if (dst && dst->ss_family == AF_INET) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200726 /* Add an X-Original-To header unless the destination IP is
727 * in the 'except' network range.
728 */
Christopher Faulet8a104ba2021-10-25 07:41:30 +0200729 if (ipcmp2net(dst, &sess->fe->except_xot_net) &&
730 ipcmp2net(dst, &s->be->except_xot_net)) {
731 unsigned char *pn = (unsigned char *)&((struct sockaddr_in *)dst)->sin_addr;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200732
733 /* Note: we rely on the backend to get the header name to be used for
734 * x-original-to, because the header is really meant for the backends.
735 * However, if the backend did not specify any option, we have to rely
736 * on the frontend's header name.
737 */
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200738 chunk_printf(&trash, "%d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
739 if (unlikely(!http_add_header(htx, hdr, ist2(trash.area, trash.data))))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100740 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200741 }
742 }
Christopher Faulet8a104ba2021-10-25 07:41:30 +0200743 else if (dst && dst->ss_family == AF_INET6) {
Christopher Faulet5d1def62021-02-26 09:19:15 +0100744 /* Add an X-Original-To header unless the source IP is
745 * in the 'except' network range.
746 */
Christopher Faulet8a104ba2021-10-25 07:41:30 +0200747 if (ipcmp2net(dst, &sess->fe->except_xot_net) &&
748 ipcmp2net(dst, &s->be->except_xot_net)) {
Christopher Faulet5d1def62021-02-26 09:19:15 +0100749 char pn[INET6_ADDRSTRLEN];
750
751 inet_ntop(AF_INET6,
Christopher Faulet8a104ba2021-10-25 07:41:30 +0200752 (const void *)&((struct sockaddr_in6 *)dst)->sin6_addr,
Christopher Faulet5d1def62021-02-26 09:19:15 +0100753 pn, sizeof(pn));
754
755 /* Note: we rely on the backend to get the header name to be used for
756 * x-forwarded-for, because the header is really meant for the backends.
757 * However, if the backend did not specify any option, we have to rely
758 * on the frontend's header name.
759 */
760 chunk_printf(&trash, "%s", pn);
761 if (unlikely(!http_add_header(htx, hdr, ist2(trash.area, trash.data))))
762 goto return_int_err;
763 }
764 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200765 }
766
Christopher Fauletc2ac5e42021-03-08 18:20:09 +0100767 /* Filter the request headers if there are filters attached to the
768 * stream.
769 */
770 if (HAS_FILTERS(s))
771 req->analysers |= AN_REQ_FLT_HTTP_HDRS;
772
Christopher Faulete0768eb2018-10-03 16:38:02 +0200773 /* If we have no server assigned yet and we're balancing on url_param
774 * with a POST request, we may be interested in checking the body for
775 * that parameter. This will be done in another analyser.
776 */
777 if (!(s->flags & (SF_ASSIGNED|SF_DIRECT)) &&
Willy Tarreau089eaa02019-01-14 15:17:46 +0100778 s->txn->meth == HTTP_METH_POST &&
779 (s->be->lbprm.algo & BE_LB_ALGO) == BE_LB_ALGO_PH) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200780 channel_dont_connect(req);
781 req->analysers |= AN_REQ_HTTP_BODY;
782 }
783
784 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
785 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau1a18b542018-12-11 16:37:42 +0100786
Christopher Faulete0768eb2018-10-03 16:38:02 +0200787 /* We expect some data from the client. Unless we know for sure
788 * we already have a full request, we have to re-enable quick-ack
789 * in case we previously disabled it, otherwise we might cause
790 * the client to delay further data.
791 */
William Lallemand36119de2021-03-08 15:26:48 +0100792 if ((sess->listener && (sess->listener->options & LI_O_NOQUICKACK)) && !(htx->flags & HTX_FL_EOM))
Willy Tarreau1a18b542018-12-11 16:37:42 +0100793 conn_set_quickack(cli_conn, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200794
795 /*************************************************************
796 * OK, that's finished for the headers. We have done what we *
797 * could. Let's switch to the DATA state. *
798 ************************************************************/
799 req->analyse_exp = TICK_ETERNITY;
800 req->analysers &= ~an_bit;
801
802 s->logs.tv_request = now;
803 /* OK let's go on with the BODY now */
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100804 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200805 return 1;
806
Christopher Fauletb8a53712019-12-16 11:29:38 +0100807 return_int_err:
808 txn->status = 500;
809 if (!(s->flags & SF_ERR_MASK))
810 s->flags |= SF_ERR_INTERNAL;
Willy Tarreau4781b152021-04-06 13:53:36 +0200811 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100812 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau4781b152021-04-06 13:53:36 +0200813 _HA_ATOMIC_INC(&s->be->be_counters.internal_errors);
William Lallemand36119de2021-03-08 15:26:48 +0100814 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200815 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100816
Christopher Fauletb8a53712019-12-16 11:29:38 +0100817 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +0200818
819 if (!(s->flags & SF_ERR_MASK))
820 s->flags |= SF_ERR_PRXCOND;
821 if (!(s->flags & SF_FINST_MASK))
822 s->flags |= SF_FINST_R;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100823
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100824 DBG_TRACE_DEVEL("leaving on error",
825 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200826 return 0;
827}
828
829/* This function is an analyser which processes the HTTP tarpit. It always
830 * returns zero, at the beginning because it prevents any other processing
831 * from occurring, and at the end because it terminates the request.
832 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200833int http_process_tarpit(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200834{
835 struct http_txn *txn = s->txn;
836
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100837 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, &txn->req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200838 /* This connection is being tarpitted. The CLIENT side has
839 * already set the connect expiration date to the right
840 * timeout. We just have to check that the client is still
841 * there and that the timeout has not expired.
842 */
843 channel_dont_connect(req);
844 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100845 !tick_is_expired(req->analyse_exp, now_ms)) {
Christopher Fauletb0c87f12021-10-29 14:37:07 +0200846 /* Be sure to drain all data from the request channel */
847 channel_htx_erase(req, htxbuf(&req->buf));
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100848 DBG_TRACE_DEVEL("waiting for tarpit timeout expiry",
849 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200850 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100851 }
852
Christopher Faulete0768eb2018-10-03 16:38:02 +0200853
854 /* We will set the queue timer to the time spent, just for
855 * logging purposes. We fake a 500 server error, so that the
856 * attacker will not suspect his connection has been tarpitted.
857 * It will not cause trouble to the logs because we can exclude
858 * the tarpitted connections by filtering on the 'PT' status flags.
859 */
860 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
861
Christopher Faulet8dfeccf2020-05-15 14:16:29 +0200862 http_reply_and_close(s, txn->status, (!(req->flags & CF_READ_ERROR) ? http_error_message(s) : NULL));
Christopher Faulet5cb513a2020-05-13 17:56:56 +0200863
Christopher Faulete0768eb2018-10-03 16:38:02 +0200864 if (!(s->flags & SF_ERR_MASK))
865 s->flags |= SF_ERR_PRXCOND;
866 if (!(s->flags & SF_FINST_MASK))
867 s->flags |= SF_FINST_T;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100868
869 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200870 return 0;
871}
872
873/* This function is an analyser which waits for the HTTP request body. It waits
874 * for either the buffer to be full, or the full advertised contents to have
875 * reached the buffer. It must only be called after the standard HTTP request
876 * processing has occurred, because it expects the request to be parsed and will
877 * look for the Expect header. It may send a 100-Continue interim response. It
878 * takes in input any state starting from HTTP_MSG_BODY and leaves with one of
879 * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it
880 * needs to read more data, or 1 once it has completed its analysis.
881 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200882int http_wait_for_request_body(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200883{
884 struct session *sess = s->sess;
885 struct http_txn *txn = s->txn;
886 struct http_msg *msg = &s->txn->req;
887
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100888 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Fauletf76ebe82018-10-24 11:16:22 +0200889
Christopher Fauletf76ebe82018-10-24 11:16:22 +0200890
Christopher Faulet021a8e42021-03-29 10:46:38 +0200891 switch (http_wait_for_msg_body(s, req, s->be->timeout.httpreq, 0)) {
892 case HTTP_RULE_RES_CONT:
893 goto http_end;
894 case HTTP_RULE_RES_YIELD:
895 goto missing_data_or_waiting;
896 case HTTP_RULE_RES_BADREQ:
Willy Tarreau4236f032019-03-05 10:43:32 +0100897 goto return_bad_req;
Christopher Faulet021a8e42021-03-29 10:46:38 +0200898 case HTTP_RULE_RES_ERROR:
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200899 goto return_int_err;
Christopher Faulet021a8e42021-03-29 10:46:38 +0200900 case HTTP_RULE_RES_ABRT:
Christopher Fauletb8a53712019-12-16 11:29:38 +0100901 goto return_prx_cond;
Christopher Faulet021a8e42021-03-29 10:46:38 +0200902 default:
903 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200904 }
905
906 http_end:
907 /* The situation will not evolve, so let's give up on the analysis. */
908 s->logs.tv_request = now; /* update the request timer to reflect full request */
909 req->analysers &= ~an_bit;
910 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100911 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200912 return 1;
913
Christopher Faulet021a8e42021-03-29 10:46:38 +0200914 missing_data_or_waiting:
915 channel_dont_connect(req);
916 DBG_TRACE_DEVEL("waiting for more data",
917 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
918 return 0;
919
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200920 return_int_err:
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200921 txn->status = 500;
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200922 if (!(s->flags & SF_ERR_MASK))
923 s->flags |= SF_ERR_INTERNAL;
Willy Tarreau4781b152021-04-06 13:53:36 +0200924 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100925 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau4781b152021-04-06 13:53:36 +0200926 _HA_ATOMIC_INC(&s->be->be_counters.internal_errors);
William Lallemand36119de2021-03-08 15:26:48 +0100927 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200928 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Faulet021a8e42021-03-29 10:46:38 +0200929 goto return_prx_err;
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200930
Christopher Faulete0768eb2018-10-03 16:38:02 +0200931 return_bad_req: /* let's centralize all bad requests */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200932 txn->status = 400;
Willy Tarreau4781b152021-04-06 13:53:36 +0200933 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req);
William Lallemand36119de2021-03-08 15:26:48 +0100934 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200935 _HA_ATOMIC_INC(&sess->listener->counters->failed_req);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100936 /* fall through */
937
Christopher Faulet021a8e42021-03-29 10:46:38 +0200938 return_prx_err:
Christopher Fauletb8a53712019-12-16 11:29:38 +0100939 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet021a8e42021-03-29 10:46:38 +0200940 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200941
Christopher Faulet021a8e42021-03-29 10:46:38 +0200942 return_prx_cond:
Christopher Faulete0768eb2018-10-03 16:38:02 +0200943 if (!(s->flags & SF_ERR_MASK))
944 s->flags |= SF_ERR_PRXCOND;
945 if (!(s->flags & SF_FINST_MASK))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100946 s->flags |= (msg->msg_state < HTTP_MSG_DATA ? SF_FINST_R : SF_FINST_D);
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200947
Christopher Faulete0768eb2018-10-03 16:38:02 +0200948 req->analysers &= AN_REQ_FLT_END;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100949 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100950 DBG_TRACE_DEVEL("leaving on error",
951 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200952 return 0;
953}
954
955/* This function is an analyser which forwards request body (including chunk
956 * sizes if any). It is called as soon as we must forward, even if we forward
957 * zero byte. The only situation where it must not be called is when we're in
958 * tunnel mode and we want to forward till the close. It's used both to forward
959 * remaining data and to resync after end of body. It expects the msg_state to
960 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
961 * read more data, or 1 once we can go on with next request or end the stream.
962 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
963 * bytes of pending data + the headers if not already done.
964 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200965int http_request_forward_body(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200966{
967 struct session *sess = s->sess;
968 struct http_txn *txn = s->txn;
Christopher Faulet9768c262018-10-22 09:34:31 +0200969 struct http_msg *msg = &txn->req;
970 struct htx *htx;
Christopher Faulet93e02d82019-03-08 14:18:50 +0100971 short status = 0;
Christopher Fauletaed82cf2018-11-30 22:22:32 +0100972 int ret;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200973
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100974 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200975
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100976 htx = htxbuf(&req->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200977
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200978 if (htx->flags & HTX_FL_PARSING_ERROR)
979 goto return_bad_req;
980 if (htx->flags & HTX_FL_PROCESSING_ERROR)
981 goto return_int_err;
982
Christopher Faulete0768eb2018-10-03 16:38:02 +0200983 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
984 ((req->flags & CF_SHUTW) && (req->to_forward || co_data(req)))) {
985 /* Output closed while we were sending data. We must abort and
986 * wake the other side up.
Christopher Fauletf506d962021-04-27 10:56:28 +0200987 *
988 * If we have finished to send the request and the response is
989 * still in progress, don't catch write error on the request
990 * side if it is in fact a read error on the server side.
Christopher Faulete0768eb2018-10-03 16:38:02 +0200991 */
Christopher Fauletf506d962021-04-27 10:56:28 +0200992 if (msg->msg_state == HTTP_MSG_DONE && (s->res.flags & CF_READ_ERROR) && s->res.analysers)
993 return 0;
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200994
Olivier Houchard29cac3c2019-07-12 15:48:58 +0200995 /* Don't abort yet if we had L7 retries activated and it
996 * was a write error, we may recover.
997 */
998 if (!(req->flags & (CF_READ_ERROR | CF_READ_TIMEOUT)) &&
Christopher Faulete05bf9e2022-03-29 15:23:40 +0200999 (txn->flags & TX_L7_RETRY)) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001000 DBG_TRACE_DEVEL("leaving on L7 retry",
1001 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Olivier Houchard29cac3c2019-07-12 15:48:58 +02001002 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001003 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001004 msg->msg_state = HTTP_MSG_ERROR;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001005 http_end_request(s);
1006 http_end_response(s);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001007 DBG_TRACE_DEVEL("leaving on error",
1008 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001009 return 1;
1010 }
1011
1012 /* Note that we don't have to send 100-continue back because we don't
1013 * need the data to complete our job, and it's up to the server to
1014 * decide whether to return 100, 417 or anything else in return of
1015 * an "Expect: 100-continue" header.
1016 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001017 if (msg->msg_state == HTTP_MSG_BODY)
1018 msg->msg_state = HTTP_MSG_DATA;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001019
Christopher Faulete0768eb2018-10-03 16:38:02 +02001020 /* in most states, we should abort in case of early close */
1021 channel_auto_close(req);
1022
1023 if (req->to_forward) {
Christopher Faulet66af0b22019-03-22 14:54:52 +01001024 if (req->to_forward == CHN_INFINITE_FORWARD) {
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001025 if (req->flags & CF_EOI)
1026 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet66af0b22019-03-22 14:54:52 +01001027 }
1028 else {
1029 /* We can't process the buffer's contents yet */
1030 req->flags |= CF_WAKE_WRITE;
1031 goto missing_data_or_waiting;
1032 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001033 }
1034
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001035 if (msg->msg_state >= HTTP_MSG_ENDING)
1036 goto ending;
1037
1038 if (txn->meth == HTTP_METH_CONNECT) {
1039 msg->msg_state = HTTP_MSG_ENDING;
1040 goto ending;
1041 }
1042
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001043 /* Forward input data. We get it by removing all outgoing data not
1044 * forwarded yet from HTX data size. If there are some data filters, we
1045 * let them decide the amount of data to forward.
Christopher Faulet9768c262018-10-22 09:34:31 +02001046 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001047 if (HAS_REQ_DATA_FILTERS(s)) {
1048 ret = flt_http_payload(s, msg, htx->data);
1049 if (ret < 0)
1050 goto return_bad_req;
Christopher Faulet421e7692019-06-13 11:16:45 +02001051 c_adv(req, ret);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001052 }
1053 else {
Christopher Faulet421e7692019-06-13 11:16:45 +02001054 c_adv(req, htx->data - co_data(req));
Christopher Faulet66af0b22019-03-22 14:54:52 +01001055 if (msg->flags & HTTP_MSGF_XFER_LEN)
1056 channel_htx_forward_forever(req, htx);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001057 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001058
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001059 if (htx->data != co_data(req))
1060 goto missing_data_or_waiting;
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001061
Christopher Faulet9768c262018-10-22 09:34:31 +02001062 /* Check if the end-of-message is reached and if so, switch the message
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001063 * in HTTP_MSG_ENDING state. Then if all data was marked to be
1064 * forwarded, set the state to HTTP_MSG_DONE.
Christopher Faulet9768c262018-10-22 09:34:31 +02001065 */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001066 if (!(htx->flags & HTX_FL_EOM))
Christopher Faulet9768c262018-10-22 09:34:31 +02001067 goto missing_data_or_waiting;
1068
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001069 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet9768c262018-10-22 09:34:31 +02001070
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001071 ending:
Christopher Faulet2151cdd2020-07-22 16:34:59 +02001072 req->flags &= ~CF_EXPECT_MORE; /* no more data are expected */
1073
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001074 /* other states, ENDING...TUNNEL */
1075 if (msg->msg_state >= HTTP_MSG_DONE)
1076 goto done;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001077
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001078 if (HAS_REQ_DATA_FILTERS(s)) {
1079 ret = flt_http_end(s, msg);
1080 if (ret <= 0) {
1081 if (!ret)
1082 goto missing_data_or_waiting;
1083 goto return_bad_req;
1084 }
1085 }
1086
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001087 if (txn->meth == HTTP_METH_CONNECT)
1088 msg->msg_state = HTTP_MSG_TUNNEL;
1089 else {
1090 msg->msg_state = HTTP_MSG_DONE;
1091 req->to_forward = 0;
1092 }
1093
1094 done:
1095 /* we don't want to forward closes on DONE except in tunnel mode. */
1096 if (!(txn->flags & TX_CON_WANT_TUN))
1097 channel_dont_close(req);
1098
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001099 http_end_request(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001100 if (!(req->analysers & an_bit)) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001101 http_end_response(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001102 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
1103 if (req->flags & CF_SHUTW) {
1104 /* request errors are most likely due to the
1105 * server aborting the transfer. */
Christopher Faulet93e02d82019-03-08 14:18:50 +01001106 goto return_srv_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001107 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001108 goto return_bad_req;
1109 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001110 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001111 return 1;
1112 }
1113
1114 /* If "option abortonclose" is set on the backend, we want to monitor
1115 * the client's connection and forward any shutdown notification to the
1116 * server, which will decide whether to close or to go on processing the
1117 * request. We only do that in tunnel mode, and not in other modes since
1118 * it can be abused to exhaust source ports. */
Christopher Faulet769d0e92019-03-22 14:23:18 +01001119 if (s->be->options & PR_O_ABRT_CLOSE) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001120 channel_auto_read(req);
Christopher Fauletc41547b2019-07-16 14:32:23 +02001121 if ((req->flags & (CF_SHUTR|CF_READ_NULL)) && !(txn->flags & TX_CON_WANT_TUN))
Christopher Faulet8abe7122022-03-30 15:10:18 +02001122 s->csb->flags |= CS_FL_NOLINGER;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001123 channel_auto_close(req);
1124 }
1125 else if (s->txn->meth == HTTP_METH_POST) {
1126 /* POST requests may require to read extra CRLF sent by broken
1127 * browsers and which could cause an RST to be sent upon close
1128 * on some systems (eg: Linux). */
1129 channel_auto_read(req);
1130 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001131 DBG_TRACE_DEVEL("waiting for the end of the HTTP txn",
1132 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001133 return 0;
1134
1135 missing_data_or_waiting:
1136 /* stop waiting for data if the input is closed before the end */
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001137 if (msg->msg_state < HTTP_MSG_ENDING && req->flags & CF_SHUTR)
Christopher Faulet93e02d82019-03-08 14:18:50 +01001138 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001139
1140 waiting:
1141 /* waiting for the last bits to leave the buffer */
1142 if (req->flags & CF_SHUTW)
Christopher Faulet93e02d82019-03-08 14:18:50 +01001143 goto return_srv_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001144
1145 /* When TE: chunked is used, we need to get there again to parse remaining
1146 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
1147 * And when content-length is used, we never want to let the possible
1148 * shutdown be forwarded to the other side, as the state machine will
1149 * take care of it once the client responds. It's also important to
1150 * prevent TIME_WAITs from accumulating on the backend side, and for
1151 * HTTP/2 where the last frame comes with a shutdown.
1152 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001153 if (msg->flags & HTTP_MSGF_XFER_LEN)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001154 channel_dont_close(req);
1155
1156 /* We know that more data are expected, but we couldn't send more that
1157 * what we did. So we always set the CF_EXPECT_MORE flag so that the
1158 * system knows it must not set a PUSH on this first part. Interactive
1159 * modes are already handled by the stream sock layer. We must not do
1160 * this in content-length mode because it could present the MSG_MORE
1161 * flag with the last block of forwarded data, which would cause an
1162 * additional delay to be observed by the receiver.
1163 */
Christopher Faulet2151cdd2020-07-22 16:34:59 +02001164 if (HAS_REQ_DATA_FILTERS(s))
Christopher Faulete0768eb2018-10-03 16:38:02 +02001165 req->flags |= CF_EXPECT_MORE;
1166
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001167 DBG_TRACE_DEVEL("waiting for more data to forward",
1168 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001169 return 0;
1170
Christopher Faulet93e02d82019-03-08 14:18:50 +01001171 return_cli_abort:
Willy Tarreau4781b152021-04-06 13:53:36 +02001172 _HA_ATOMIC_INC(&sess->fe->fe_counters.cli_aborts);
1173 _HA_ATOMIC_INC(&s->be->be_counters.cli_aborts);
William Lallemand36119de2021-03-08 15:26:48 +01001174 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001175 _HA_ATOMIC_INC(&sess->listener->counters->cli_aborts);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001176 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001177 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.cli_aborts);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001178 if (!(s->flags & SF_ERR_MASK))
1179 s->flags |= SF_ERR_CLICL;
1180 status = 400;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001181 goto return_prx_cond;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001182
1183 return_srv_abort:
Willy Tarreau4781b152021-04-06 13:53:36 +02001184 _HA_ATOMIC_INC(&sess->fe->fe_counters.srv_aborts);
1185 _HA_ATOMIC_INC(&s->be->be_counters.srv_aborts);
William Lallemand36119de2021-03-08 15:26:48 +01001186 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001187 _HA_ATOMIC_INC(&sess->listener->counters->srv_aborts);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001188 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001189 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.srv_aborts);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001190 if (!(s->flags & SF_ERR_MASK))
1191 s->flags |= SF_ERR_SRVCL;
1192 status = 502;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001193 goto return_prx_cond;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001194
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001195 return_int_err:
1196 if (!(s->flags & SF_ERR_MASK))
1197 s->flags |= SF_ERR_INTERNAL;
Willy Tarreau4781b152021-04-06 13:53:36 +02001198 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
1199 _HA_ATOMIC_INC(&s->be->be_counters.internal_errors);
William Lallemand36119de2021-03-08 15:26:48 +01001200 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001201 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001202 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001203 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.internal_errors);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001204 status = 500;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001205 goto return_prx_cond;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001206
Christopher Faulet93e02d82019-03-08 14:18:50 +01001207 return_bad_req:
Willy Tarreau4781b152021-04-06 13:53:36 +02001208 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req);
William Lallemand36119de2021-03-08 15:26:48 +01001209 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001210 _HA_ATOMIC_INC(&sess->listener->counters->failed_req);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001211 status = 400;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001212 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001213
Christopher Fauletb8a53712019-12-16 11:29:38 +01001214 return_prx_cond:
Christopher Faulet9768c262018-10-22 09:34:31 +02001215 if (txn->status > 0) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001216 /* Note: we don't send any error if some data were already sent */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001217 http_reply_and_close(s, txn->status, NULL);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001218 } else {
Christopher Faulet93e02d82019-03-08 14:18:50 +01001219 txn->status = status;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001220 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001221 }
Christopher Fauletb8a53712019-12-16 11:29:38 +01001222 if (!(s->flags & SF_ERR_MASK))
1223 s->flags |= SF_ERR_PRXCOND;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001224 if (!(s->flags & SF_FINST_MASK))
1225 s->flags |= ((txn->rsp.msg_state < HTTP_MSG_ERROR) ? SF_FINST_H : SF_FINST_D);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001226 DBG_TRACE_DEVEL("leaving on error ",
1227 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001228 return 0;
1229}
1230
Christopher Faulet77397992022-04-04 11:07:08 +02001231/* Reset the stream and the backend conn_stream to a situation suitable for attemption connection */
Olivier Houcharda254a372019-04-05 15:30:12 +02001232/* Returns 0 if we can attempt to retry, -1 otherwise */
Christopher Faulet77397992022-04-04 11:07:08 +02001233static __inline int do_l7_retry(struct stream *s, struct conn_stream *cs)
Olivier Houcharda254a372019-04-05 15:30:12 +02001234{
Christopher Faulet9f5382e2021-05-21 13:46:14 +02001235 struct channel *req, *res;
1236 int co_data;
Olivier Houcharda254a372019-04-05 15:30:12 +02001237
Christopher Faulet731c8e62022-03-29 16:08:44 +02001238 s->conn_retries++;
1239 if (s->conn_retries >= s->be->conn_retries)
Christopher Faulet552601d2021-05-26 10:31:06 +02001240 return -1;
Christopher Faulet5b82cc52020-10-12 15:18:50 +02001241
Christopher Faulete763c8c2021-05-05 18:23:59 +02001242 if (objt_server(s->target)) {
1243 if (s->flags & SF_CURR_SESS) {
1244 s->flags &= ~SF_CURR_SESS;
1245 _HA_ATOMIC_DEC(&__objt_server(s->target)->cur_sess);
1246 }
Willy Tarreau4781b152021-04-06 13:53:36 +02001247 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.retries);
Christopher Faulete763c8c2021-05-05 18:23:59 +02001248 }
Willy Tarreau4781b152021-04-06 13:53:36 +02001249 _HA_ATOMIC_INC(&s->be->be_counters.retries);
Willy Tarreau223995e2019-05-04 10:38:31 +02001250
Christopher Faulet9f5382e2021-05-21 13:46:14 +02001251 req = &s->req;
1252 res = &s->res;
Olivier Houcharda254a372019-04-05 15:30:12 +02001253 /* Remove any write error from the request, and read error from the response */
1254 req->flags &= ~(CF_WRITE_ERROR | CF_WRITE_TIMEOUT | CF_SHUTW | CF_SHUTW_NOW);
1255 res->flags &= ~(CF_READ_ERROR | CF_READ_TIMEOUT | CF_SHUTR | CF_EOI | CF_READ_NULL | CF_SHUTR_NOW);
Christopher Faulet7bf46bb2022-01-04 10:56:03 +01001256 res->analysers &= AN_RES_FLT_END;
Christopher Faulet50264b42022-03-30 19:39:30 +02001257 s->conn_err_type = STRM_ET_NONE;
Christopher Fauletae024ce2022-03-29 19:02:31 +02001258 s->flags &= ~(SF_CONN_EXP | SF_ERR_MASK | SF_FINST_MASK);
1259 s->conn_exp = TICK_ETERNITY;
Olivier Houchard4bd58672019-07-12 16:16:59 +02001260 stream_choose_redispatch(s);
Olivier Houcharda254a372019-04-05 15:30:12 +02001261 res->rex = TICK_ETERNITY;
1262 res->to_forward = 0;
1263 res->analyse_exp = TICK_ETERNITY;
1264 res->total = 0;
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +01001265
1266 if (cs_reset_endp(s->csb) < 0) {
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +01001267 if (!(s->flags & SF_ERR_MASK))
1268 s->flags |= SF_ERR_INTERNAL;
1269 return -1;
1270 }
Christopher Fauleta6c4a482022-04-28 18:25:24 +02001271 cs->endp->flags &= ~CS_EP_RXBLK_SHUT;
Olivier Houcharda254a372019-04-05 15:30:12 +02001272
Christopher Faulet9f5382e2021-05-21 13:46:14 +02001273 b_free(&req->buf);
1274 /* Swap the L7 buffer with the channel buffer */
1275 /* We know we stored the co_data as b_data, so get it there */
Christopher Faulete05bf9e2022-03-29 15:23:40 +02001276 co_data = b_data(&s->txn->l7_buffer);
1277 b_set_data(&s->txn->l7_buffer, b_size(&s->txn->l7_buffer));
1278 b_xfer(&req->buf, &s->txn->l7_buffer, b_data(&s->txn->l7_buffer));
Christopher Faulet9f5382e2021-05-21 13:46:14 +02001279 co_set_data(req, co_data);
Christopher Faulet5b82cc52020-10-12 15:18:50 +02001280
Ilya Shipitsinacf84592021-02-06 22:29:08 +05001281 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 +02001282
Olivier Houcharda254a372019-04-05 15:30:12 +02001283 b_reset(&res->buf);
1284 co_set_data(res, 0);
1285 return 0;
1286}
1287
Christopher Faulete0768eb2018-10-03 16:38:02 +02001288/* This stream analyser waits for a complete HTTP response. It returns 1 if the
1289 * processing can continue on next analysers, or zero if it either needs more
1290 * data or wants to immediately abort the response (eg: timeout, error, ...). It
1291 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->res.analysers
1292 * when it has nothing left to do, and may remove any analyser when it wants to
1293 * abort.
1294 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001295int http_wait_for_response(struct stream *s, struct channel *rep, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001296{
Christopher Faulet9768c262018-10-22 09:34:31 +02001297 /*
1298 * We will analyze a complete HTTP response to check the its syntax.
1299 *
1300 * Once the start line and all headers are received, we may perform a
1301 * capture of the error (if any), and we will set a few fields. We also
1302 * logging and finally headers capture.
1303 */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001304 struct session *sess = s->sess;
1305 struct http_txn *txn = s->txn;
1306 struct http_msg *msg = &txn->rsp;
Christopher Faulet9768c262018-10-22 09:34:31 +02001307 struct htx *htx;
Christopher Faulet61608322018-11-23 16:23:45 +01001308 struct connection *srv_conn;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001309 struct htx_sl *sl;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001310 int n;
1311
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001312 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001313
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001314 htx = htxbuf(&rep->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001315
Willy Tarreau4236f032019-03-05 10:43:32 +01001316 /* Parsing errors are caught here */
1317 if (htx->flags & HTX_FL_PARSING_ERROR)
1318 goto return_bad_res;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001319 if (htx->flags & HTX_FL_PROCESSING_ERROR)
1320 goto return_int_err;
Willy Tarreau4236f032019-03-05 10:43:32 +01001321
Christopher Faulete0768eb2018-10-03 16:38:02 +02001322 /*
1323 * Now we quickly check if we have found a full valid response.
1324 * If not so, we check the FD and buffer states before leaving.
1325 * A full response is indicated by the fact that we have seen
1326 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
1327 * responses are checked first.
1328 *
1329 * Depending on whether the client is still there or not, we
1330 * may send an error response back or not. Note that normally
1331 * we should only check for HTTP status there, and check I/O
1332 * errors somewhere else.
1333 */
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001334 next_one:
Christopher Faulet29f17582019-05-23 11:03:26 +02001335 if (unlikely(htx_is_empty(htx) || htx->first == -1)) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001336 /* 1: have we encountered a read error ? */
1337 if (rep->flags & CF_READ_ERROR) {
Christopher Faulet95a61e82021-12-22 14:22:03 +01001338 struct connection *conn = cs_conn(s->csb);
Olivier Houchard865d8392019-05-03 22:46:27 +02001339
Christopher Fauletd9769232021-05-26 12:15:37 +02001340 /* Perform a L7 retry because server refuses the early data. */
Christopher Faulete05bf9e2022-03-29 15:23:40 +02001341 if ((txn->flags & TX_L7_RETRY) &&
Christopher Fauletd9769232021-05-26 12:15:37 +02001342 (s->be->retry_type & PR_RE_EARLY_ERROR) &&
1343 conn && conn->err_code == CO_ER_SSL_EARLY_FAILED &&
Christopher Faulet77397992022-04-04 11:07:08 +02001344 do_l7_retry(s, s->csb) == 0) {
Christopher Fauletd9769232021-05-26 12:15:37 +02001345 DBG_TRACE_DEVEL("leaving on L7 retry",
1346 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
1347 return 0;
1348 }
1349
Olivier Houchard6db16992019-05-17 15:40:49 +02001350 if (txn->flags & TX_NOT_FIRST)
1351 goto abort_keep_alive;
1352
Willy Tarreau4781b152021-04-06 13:53:36 +02001353 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001354 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001355 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001356 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001357 }
1358
Christopher Fauletd9769232021-05-26 12:15:37 +02001359 /* if the server refused the early data, just send a 425 */
1360 if (conn && conn->err_code == CO_ER_SSL_EARLY_FAILED)
Olivier Houchard865d8392019-05-03 22:46:27 +02001361 txn->status = 425;
Christopher Fauletd9769232021-05-26 12:15:37 +02001362 else {
1363 txn->status = 502;
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001364 stream_inc_http_fail_ctr(s);
Christopher Fauletd9769232021-05-26 12:15:37 +02001365 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001366
Christopher Faulet8abe7122022-03-30 15:10:18 +02001367 s->csb->flags |= CS_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001368 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001369
1370 if (!(s->flags & SF_ERR_MASK))
1371 s->flags |= SF_ERR_SRVCL;
1372 if (!(s->flags & SF_FINST_MASK))
1373 s->flags |= SF_FINST_H;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001374 DBG_TRACE_DEVEL("leaving on error",
1375 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001376 return 0;
1377 }
1378
Christopher Faulet9768c262018-10-22 09:34:31 +02001379 /* 2: read timeout : return a 504 to the client. */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001380 else if (rep->flags & CF_READ_TIMEOUT) {
Christopher Faulete05bf9e2022-03-29 15:23:40 +02001381 if ((txn->flags & TX_L7_RETRY) &&
Olivier Houcharda254a372019-04-05 15:30:12 +02001382 (s->be->retry_type & PR_RE_TIMEOUT)) {
Christopher Faulet77397992022-04-04 11:07:08 +02001383 if (co_data(rep) || do_l7_retry(s, s->csb) == 0) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001384 DBG_TRACE_DEVEL("leaving on L7 retry",
1385 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharda254a372019-04-05 15:30:12 +02001386 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001387 }
Olivier Houcharda254a372019-04-05 15:30:12 +02001388 }
Willy Tarreau4781b152021-04-06 13:53:36 +02001389 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001390 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001391 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001392 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001393 }
1394
Christopher Faulete0768eb2018-10-03 16:38:02 +02001395 txn->status = 504;
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001396 stream_inc_http_fail_ctr(s);
Christopher Faulet8abe7122022-03-30 15:10:18 +02001397 s->csb->flags |= CS_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001398 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001399
1400 if (!(s->flags & SF_ERR_MASK))
1401 s->flags |= SF_ERR_SRVTO;
1402 if (!(s->flags & SF_FINST_MASK))
1403 s->flags |= SF_FINST_H;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001404 DBG_TRACE_DEVEL("leaving on error",
1405 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001406 return 0;
1407 }
1408
Christopher Faulet9768c262018-10-22 09:34:31 +02001409 /* 3: client abort with an abortonclose */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001410 else if ((rep->flags & CF_SHUTR) && ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001411 _HA_ATOMIC_INC(&sess->fe->fe_counters.cli_aborts);
1412 _HA_ATOMIC_INC(&s->be->be_counters.cli_aborts);
William Lallemand36119de2021-03-08 15:26:48 +01001413 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001414 _HA_ATOMIC_INC(&sess->listener->counters->cli_aborts);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001415 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001416 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.cli_aborts);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001417
Christopher Faulete0768eb2018-10-03 16:38:02 +02001418 txn->status = 400;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001419 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001420
1421 if (!(s->flags & SF_ERR_MASK))
1422 s->flags |= SF_ERR_CLICL;
1423 if (!(s->flags & SF_FINST_MASK))
1424 s->flags |= SF_FINST_H;
1425
1426 /* process_stream() will take care of the error */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001427 DBG_TRACE_DEVEL("leaving on error",
1428 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001429 return 0;
1430 }
1431
Christopher Faulet9768c262018-10-22 09:34:31 +02001432 /* 4: close from server, capture the response if the server has started to respond */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001433 else if (rep->flags & CF_SHUTR) {
Christopher Faulete05bf9e2022-03-29 15:23:40 +02001434 if ((txn->flags & TX_L7_RETRY) &&
Olivier Houcharda254a372019-04-05 15:30:12 +02001435 (s->be->retry_type & PR_RE_DISCONNECTED)) {
Christopher Faulet77397992022-04-04 11:07:08 +02001436 if (co_data(rep) || do_l7_retry(s, s->csb) == 0) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001437 DBG_TRACE_DEVEL("leaving on L7 retry",
1438 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharda254a372019-04-05 15:30:12 +02001439 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001440 }
Olivier Houcharda254a372019-04-05 15:30:12 +02001441 }
1442
Olivier Houchard6db16992019-05-17 15:40:49 +02001443 if (txn->flags & TX_NOT_FIRST)
1444 goto abort_keep_alive;
1445
Willy Tarreau4781b152021-04-06 13:53:36 +02001446 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001447 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001448 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001449 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001450 }
1451
Christopher Faulete0768eb2018-10-03 16:38:02 +02001452 txn->status = 502;
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001453 stream_inc_http_fail_ctr(s);
Christopher Faulet8abe7122022-03-30 15:10:18 +02001454 s->csb->flags |= CS_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001455 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001456
1457 if (!(s->flags & SF_ERR_MASK))
1458 s->flags |= SF_ERR_SRVCL;
1459 if (!(s->flags & SF_FINST_MASK))
1460 s->flags |= SF_FINST_H;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001461 DBG_TRACE_DEVEL("leaving on error",
1462 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001463 return 0;
1464 }
1465
Christopher Faulet9768c262018-10-22 09:34:31 +02001466 /* 5: write error to client (we don't send any message then) */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001467 else if (rep->flags & CF_WRITE_ERROR) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001468 if (txn->flags & TX_NOT_FIRST)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001469 goto abort_keep_alive;
1470
Willy Tarreau4781b152021-04-06 13:53:36 +02001471 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001472 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001473 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001474 rep->analysers &= AN_RES_FLT_END;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001475
1476 if (!(s->flags & SF_ERR_MASK))
1477 s->flags |= SF_ERR_CLICL;
1478 if (!(s->flags & SF_FINST_MASK))
1479 s->flags |= SF_FINST_H;
1480
1481 /* process_stream() will take care of the error */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001482 DBG_TRACE_DEVEL("leaving on error",
1483 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001484 return 0;
1485 }
1486
1487 channel_dont_close(rep);
1488 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001489 DBG_TRACE_DEVEL("waiting for more data",
1490 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001491 return 0;
1492 }
1493
1494 /* More interesting part now : we know that we have a complete
1495 * response which at least looks like HTTP. We have an indicator
1496 * of each header's length, so we can parse them quickly.
1497 */
Christopher Faulet29f17582019-05-23 11:03:26 +02001498 BUG_ON(htx_get_first_type(htx) != HTX_BLK_RES_SL);
Christopher Faulet297fbb42019-05-13 14:41:27 +02001499 sl = http_get_stline(htx);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001500
Christopher Faulet1f08bff2021-05-26 13:14:39 +02001501 /* Perform a L7 retry because of the status code */
Christopher Faulete05bf9e2022-03-29 15:23:40 +02001502 if ((txn->flags & TX_L7_RETRY) &&
Christopher Faulet1f08bff2021-05-26 13:14:39 +02001503 l7_status_match(s->be, sl->info.res.status) &&
Christopher Faulet77397992022-04-04 11:07:08 +02001504 do_l7_retry(s, s->csb) == 0) {
Christopher Faulet1f08bff2021-05-26 13:14:39 +02001505 DBG_TRACE_DEVEL("leaving on L7 retry", STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
1506 return 0;
1507 }
1508
1509 /* Now, L7 buffer is useless, it can be released */
Christopher Faulete05bf9e2022-03-29 15:23:40 +02001510 b_free(&txn->l7_buffer);
Christopher Faulet1f08bff2021-05-26 13:14:39 +02001511
1512 msg->msg_state = HTTP_MSG_BODY;
1513
1514
Christopher Faulet9768c262018-10-22 09:34:31 +02001515 /* 0: we might have to print this header in debug mode */
1516 if (unlikely((global.mode & MODE_DEBUG) &&
1517 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
1518 int32_t pos;
1519
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001520 http_debug_stline("srvrep", s, sl);
Christopher Faulet9768c262018-10-22 09:34:31 +02001521
Christopher Fauleta3f15502019-05-13 15:27:23 +02001522 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001523 struct htx_blk *blk = htx_get_blk(htx, pos);
1524 enum htx_blk_type type = htx_get_blk_type(blk);
1525
1526 if (type == HTX_BLK_EOH)
1527 break;
1528 if (type != HTX_BLK_HDR)
1529 continue;
1530
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001531 http_debug_hdr("srvhdr", s,
1532 htx_get_blk_name(htx, blk),
1533 htx_get_blk_value(htx, blk));
Christopher Faulet9768c262018-10-22 09:34:31 +02001534 }
1535 }
1536
Christopher Faulet03599112018-11-27 11:21:21 +01001537 /* 1: get the status code and the version. Also set HTTP flags */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001538 txn->status = sl->info.res.status;
Christopher Faulet03599112018-11-27 11:21:21 +01001539 if (sl->flags & HTX_SL_F_VER_11)
Christopher Faulet9768c262018-10-22 09:34:31 +02001540 msg->flags |= HTTP_MSGF_VER_11;
Christopher Faulet03599112018-11-27 11:21:21 +01001541 if (sl->flags & HTX_SL_F_XFER_LEN) {
1542 msg->flags |= HTTP_MSGF_XFER_LEN;
Christopher Faulet2a408542020-11-20 14:22:37 +01001543 if (sl->flags & HTX_SL_F_CLEN)
1544 msg->flags |= HTTP_MSGF_CNT_LEN;
1545 else if (sl->flags & HTX_SL_F_CHNK)
1546 msg->flags |= HTTP_MSGF_TE_CHNK;
Christopher Faulet03599112018-11-27 11:21:21 +01001547 }
Christopher Faulet2a408542020-11-20 14:22:37 +01001548 if (sl->flags & HTX_SL_F_BODYLESS)
1549 msg->flags |= HTTP_MSGF_BODYLESS;
Christopher Faulet576c3582021-01-08 15:53:01 +01001550 if (sl->flags & HTX_SL_F_CONN_UPG)
1551 msg->flags |= HTTP_MSGF_CONN_UPG;
Christopher Faulet9768c262018-10-22 09:34:31 +02001552
1553 n = txn->status / 100;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001554 if (n < 1 || n > 5)
1555 n = 0;
Christopher Faulet9768c262018-10-22 09:34:31 +02001556
Christopher Faulete0768eb2018-10-03 16:38:02 +02001557 /* when the client triggers a 4xx from the server, it's most often due
1558 * to a missing object or permission. These events should be tracked
1559 * because if they happen often, it may indicate a brute force or a
1560 * vulnerability scan.
1561 */
1562 if (n == 4)
1563 stream_inc_http_err_ctr(s);
1564
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001565 if (n == 5 && txn->status != 501 && txn->status != 505)
1566 stream_inc_http_fail_ctr(s);
1567
Marcin Deranek3c27dda2020-05-15 18:32:51 +02001568 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001569 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.p.http.rsp[n]);
1570 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.p.http.cum_req);
Marcin Deranek3c27dda2020-05-15 18:32:51 +02001571 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001572
Christopher Faulete0768eb2018-10-03 16:38:02 +02001573 /* Adjust server's health based on status code. Note: status codes 501
1574 * and 505 are triggered on demand by client request, so we must not
1575 * count them as server failures.
1576 */
1577 if (objt_server(s->target)) {
1578 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001579 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_OK);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001580 else
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001581 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_STS);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001582 }
1583
1584 /*
1585 * We may be facing a 100-continue response, or any other informational
1586 * 1xx response which is non-final, in which case this is not the right
1587 * response, and we're waiting for the next one. Let's allow this response
1588 * to go to the client and wait for the next one. There's an exception for
1589 * 101 which is used later in the code to switch protocols.
1590 */
1591 if (txn->status < 200 &&
1592 (txn->status == 100 || txn->status >= 102)) {
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001593 FLT_STRM_CB(s, flt_http_reset(s, msg));
Christopher Faulet421e7692019-06-13 11:16:45 +02001594 htx->first = channel_htx_fwd_headers(rep, htx);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001595 msg->msg_state = HTTP_MSG_RPBEFORE;
Christopher Faulet3499f622019-09-03 15:23:54 +02001596 msg->flags = 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001597 txn->status = 0;
1598 s->logs.t_data = -1; /* was not a response yet */
Christopher Faulet7d518452020-08-31 11:07:07 +02001599 rep->flags |= CF_SEND_DONTWAIT; /* Send ASAP informational messages */
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001600 goto next_one;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001601 }
1602
Christopher Faulet6e6c7b12021-01-08 16:02:05 +01001603 /* A 101-switching-protocols must contains a Connection header with the
1604 * "upgrade" option and the request too. It means both are agree to
1605 * upgrade. It is not so strict because there is no test on the Upgrade
1606 * header content. But it is probably stronger enough for now.
1607 */
1608 if (txn->status == 101 &&
1609 (!(txn->req.flags & HTTP_MSGF_CONN_UPG) || !(txn->rsp.flags & HTTP_MSGF_CONN_UPG)))
1610 goto return_bad_res;
1611
Christopher Faulete0768eb2018-10-03 16:38:02 +02001612 /*
1613 * 2: check for cacheability.
1614 */
1615
1616 switch (txn->status) {
1617 case 200:
1618 case 203:
1619 case 204:
1620 case 206:
1621 case 300:
1622 case 301:
1623 case 404:
1624 case 405:
1625 case 410:
1626 case 414:
1627 case 501:
1628 break;
1629 default:
1630 /* RFC7231#6.1:
1631 * Responses with status codes that are defined as
1632 * cacheable by default (e.g., 200, 203, 204, 206,
1633 * 300, 301, 404, 405, 410, 414, and 501 in this
1634 * specification) can be reused by a cache with
1635 * heuristic expiration unless otherwise indicated
1636 * by the method definition or explicit cache
1637 * controls [RFC7234]; all other status codes are
1638 * not cacheable by default.
1639 */
1640 txn->flags &= ~(TX_CACHEABLE | TX_CACHE_COOK);
1641 break;
1642 }
1643
1644 /*
1645 * 3: we may need to capture headers
1646 */
1647 s->logs.logwait &= ~LW_RESP;
1648 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->res_cap))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001649 http_capture_headers(htx, s->res_cap, sess->fe->rsp_cap);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001650
Christopher Faulet9768c262018-10-22 09:34:31 +02001651 /* Skip parsing if no content length is possible. */
Christopher Fauletc75668e2020-12-07 18:10:32 +01001652 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status >= 200 && txn->status < 300) ||
Christopher Faulete0768eb2018-10-03 16:38:02 +02001653 txn->status == 101)) {
1654 /* Either we've established an explicit tunnel, or we're
1655 * switching the protocol. In both cases, we're very unlikely
1656 * to understand the next protocols. We have to switch to tunnel
1657 * mode, so that we transfer the request and responses then let
1658 * this protocol pass unmodified. When we later implement specific
1659 * parsers for such protocols, we'll want to check the Upgrade
1660 * header which contains information about that protocol for
1661 * responses with status 101 (eg: see RFC2817 about TLS).
1662 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02001663 txn->flags |= TX_CON_WANT_TUN;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001664 }
1665
Christopher Faulet61608322018-11-23 16:23:45 +01001666 /* check for NTML authentication headers in 401 (WWW-Authenticate) and
1667 * 407 (Proxy-Authenticate) responses and set the connection to private
1668 */
Christopher Faulet95a61e82021-12-22 14:22:03 +01001669 srv_conn = cs_conn(s->csb);
Christopher Faulet61608322018-11-23 16:23:45 +01001670 if (srv_conn) {
1671 struct ist hdr;
1672 struct http_hdr_ctx ctx;
1673
1674 if (txn->status == 401)
1675 hdr = ist("WWW-Authenticate");
1676 else if (txn->status == 407)
1677 hdr = ist("Proxy-Authenticate");
1678 else
1679 goto end;
1680
1681 ctx.blk = NULL;
1682 while (http_find_header(htx, hdr, &ctx, 0)) {
Willy Tarreauf1dcced2020-05-07 19:27:02 +02001683 /* If www-authenticate contains "Negotiate", "Nego2", or "NTLM",
1684 * possibly followed by blanks and a base64 string, the connection
1685 * is private. Since it's a mess to deal with, we only check for
1686 * values starting with "NTLM" or "Nego". Note that often multiple
1687 * headers are sent by the server there.
1688 */
1689 if ((ctx.value.len >= 4 && strncasecmp(ctx.value.ptr, "Nego", 4) == 0) ||
Willy Tarreau49a1d282020-05-07 19:10:15 +02001690 (ctx.value.len >= 4 && strncasecmp(ctx.value.ptr, "NTLM", 4) == 0)) {
Olivier Houchard250031e2019-05-29 15:01:50 +02001691 sess->flags |= SESS_FL_PREFER_LAST;
Christopher Faulet08016ab2020-07-01 16:10:06 +02001692 conn_set_owner(srv_conn, sess, NULL);
Christopher Faulet21ddc742020-07-01 15:26:14 +02001693 conn_set_private(srv_conn);
Ilya Shipitsin6b79f382020-07-23 00:32:55 +05001694 /* If it fail now, the same will be done in mux->detach() callback */
Christopher Faulet08016ab2020-07-01 16:10:06 +02001695 session_add_conn(srv_conn->owner, srv_conn, srv_conn->target);
Willy Tarreauf1dcced2020-05-07 19:27:02 +02001696 break;
Olivier Houchard250031e2019-05-29 15:01:50 +02001697 }
Christopher Faulet61608322018-11-23 16:23:45 +01001698 }
1699 }
1700
1701 end:
Christopher Faulete0768eb2018-10-03 16:38:02 +02001702 /* we want to have the response time before we start processing it */
1703 s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now);
1704
1705 /* end of job, return OK */
1706 rep->analysers &= ~an_bit;
1707 rep->analyse_exp = TICK_ETERNITY;
1708 channel_auto_close(rep);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001709 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001710 return 1;
1711
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001712 return_int_err:
Willy Tarreau4781b152021-04-06 13:53:36 +02001713 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
1714 _HA_ATOMIC_INC(&s->be->be_counters.internal_errors);
William Lallemand36119de2021-03-08 15:26:48 +01001715 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001716 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Fauletb8a53712019-12-16 11:29:38 +01001717 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001718 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.internal_errors);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001719 txn->status = 500;
1720 if (!(s->flags & SF_ERR_MASK))
1721 s->flags |= SF_ERR_INTERNAL;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001722 goto return_prx_cond;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001723
1724 return_bad_res:
Willy Tarreau4781b152021-04-06 13:53:36 +02001725 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Faulet47365272018-10-31 17:40:50 +01001726 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001727 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001728 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Christopher Faulet47365272018-10-31 17:40:50 +01001729 }
Olivier Houcharde3249a92019-05-03 23:01:47 +02001730 if ((s->be->retry_type & PR_RE_JUNK_REQUEST) &&
Christopher Faulete05bf9e2022-03-29 15:23:40 +02001731 (txn->flags & TX_L7_RETRY) &&
Christopher Faulet77397992022-04-04 11:07:08 +02001732 do_l7_retry(s, s->csb) == 0) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001733 DBG_TRACE_DEVEL("leaving on L7 retry",
1734 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharde3249a92019-05-03 23:01:47 +02001735 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001736 }
Christopher Faulet47365272018-10-31 17:40:50 +01001737 txn->status = 502;
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001738 stream_inc_http_fail_ctr(s);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001739 /* fall through */
1740
Christopher Fauletb8a53712019-12-16 11:29:38 +01001741 return_prx_cond:
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001742 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet47365272018-10-31 17:40:50 +01001743
1744 if (!(s->flags & SF_ERR_MASK))
1745 s->flags |= SF_ERR_PRXCOND;
1746 if (!(s->flags & SF_FINST_MASK))
1747 s->flags |= SF_FINST_H;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001748
Christopher Faulet8abe7122022-03-30 15:10:18 +02001749 s->csb->flags |= CS_FL_NOLINGER;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001750 DBG_TRACE_DEVEL("leaving on error",
1751 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulet47365272018-10-31 17:40:50 +01001752 return 0;
1753
Christopher Faulete0768eb2018-10-03 16:38:02 +02001754 abort_keep_alive:
1755 /* A keep-alive request to the server failed on a network error.
1756 * The client is required to retry. We need to close without returning
1757 * any other information so that the client retries.
1758 */
1759 txn->status = 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001760 s->logs.logwait = 0;
1761 s->logs.level = 0;
1762 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001763 http_reply_and_close(s, txn->status, NULL);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001764 DBG_TRACE_DEVEL("leaving by closing K/A connection",
1765 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001766 return 0;
1767}
1768
1769/* This function performs all the processing enabled for the current response.
1770 * It normally returns 1 unless it wants to break. It relies on buffers flags,
1771 * and updates s->res.analysers. It might make sense to explode it into several
1772 * other functions. It works like process_request (see indications above).
1773 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001774int http_process_res_common(struct stream *s, struct channel *rep, int an_bit, struct proxy *px)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001775{
1776 struct session *sess = s->sess;
1777 struct http_txn *txn = s->txn;
1778 struct http_msg *msg = &txn->rsp;
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001779 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001780 struct proxy *cur_proxy;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001781 enum rule_result ret = HTTP_RULE_RES_CONT;
1782
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001783 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
1784 return 0;
Christopher Faulet9768c262018-10-22 09:34:31 +02001785
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001786 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001787
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001788 htx = htxbuf(&rep->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001789
1790 /* The stats applet needs to adjust the Connection header but we don't
1791 * apply any filter there.
1792 */
1793 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
1794 rep->analysers &= ~an_bit;
1795 rep->analyse_exp = TICK_ETERNITY;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001796 goto end;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001797 }
1798
1799 /*
1800 * We will have to evaluate the filters.
1801 * As opposed to version 1.2, now they will be evaluated in the
1802 * filters order and not in the header order. This means that
1803 * each filter has to be validated among all headers.
1804 *
1805 * Filters are tried with ->be first, then with ->fe if it is
1806 * different from ->be.
1807 *
1808 * Maybe we are in resume condiion. In this case I choose the
1809 * "struct proxy" which contains the rule list matching the resume
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05001810 * pointer. If none of these "struct proxy" match, I initialise
Christopher Faulete0768eb2018-10-03 16:38:02 +02001811 * the process with the first one.
1812 *
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05001813 * In fact, I check only correspondence between the current list
Christopher Faulete0768eb2018-10-03 16:38:02 +02001814 * pointer and the ->fe rule list. If it doesn't match, I initialize
1815 * the loop with the ->be.
1816 */
Christopher Fauletd4150ad2021-10-13 15:35:55 +02001817 if (s->current_rule_list == &sess->fe->http_res_rules ||
1818 (sess->fe->defpx && s->current_rule_list == &sess->fe->defpx->http_res_rules))
Christopher Faulete0768eb2018-10-03 16:38:02 +02001819 cur_proxy = sess->fe;
1820 else
1821 cur_proxy = s->be;
Christopher Fauletd4150ad2021-10-13 15:35:55 +02001822
Christopher Faulete0768eb2018-10-03 16:38:02 +02001823 while (1) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001824 /* evaluate http-response rules */
Christopher Faulet46f46df2021-11-09 16:33:25 +01001825 if (ret == HTTP_RULE_RES_CONT || ret == HTTP_RULE_RES_STOP) {
Christopher Fauletd4150ad2021-10-13 15:35:55 +02001826 struct list *def_rules, *rules;
1827
1828 def_rules = ((cur_proxy->defpx && (cur_proxy == s->be || cur_proxy->defpx != s->be->defpx)) ? &cur_proxy->defpx->http_res_rules : NULL);
1829 rules = &cur_proxy->http_res_rules;
1830
1831 ret = http_res_get_intercept_rule(cur_proxy, def_rules, rules, s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001832
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001833 switch (ret) {
1834 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
1835 goto return_prx_yield;
1836
1837 case HTTP_RULE_RES_CONT:
1838 case HTTP_RULE_RES_STOP: /* nothing to do */
1839 break;
1840
1841 case HTTP_RULE_RES_DENY: /* deny or tarpit */
1842 goto deny;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001843
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001844 case HTTP_RULE_RES_ABRT: /* abort request, response already sent */
1845 goto return_prx_cond;
1846
1847 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
Christopher Fauletb8a53712019-12-16 11:29:38 +01001848 goto done;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001849
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001850 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
1851 goto return_bad_res;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001852
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001853 case HTTP_RULE_RES_ERROR: /* failed with a bad request */
1854 goto return_int_err;
1855 }
1856
1857 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001858
Christopher Faulete0768eb2018-10-03 16:38:02 +02001859 /* check whether we're already working on the frontend */
1860 if (cur_proxy == sess->fe)
1861 break;
1862 cur_proxy = sess->fe;
1863 }
1864
Christopher Faulete0768eb2018-10-03 16:38:02 +02001865 /* OK that's all we can do for 1xx responses */
1866 if (unlikely(txn->status < 200 && txn->status != 101))
Christopher Fauletf2824e62018-10-01 12:12:37 +02001867 goto end;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001868
1869 /*
1870 * Now check for a server cookie.
1871 */
1872 if (s->be->cookie_name || sess->fe->capture_name || (s->be->options & PR_O_CHK_CACHE))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001873 http_manage_server_side_cookies(s, rep);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001874
1875 /*
1876 * Check for cache-control or pragma headers if required.
1877 */
1878 if ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001879 http_check_response_for_cacheability(s, rep);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001880
1881 /*
1882 * Add server cookie in the response if needed
1883 */
1884 if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) &&
1885 !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) &&
1886 (!(s->flags & SF_DIRECT) ||
1887 ((s->be->cookie_maxidle || txn->cookie_last_date) &&
1888 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
1889 (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
1890 (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
1891 (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
1892 !(s->flags & SF_IGNORE_PRST)) {
1893 /* the server is known, it's not the one the client requested, or the
1894 * cookie's last seen date needs to be refreshed. We have to
1895 * insert a set-cookie here, except if we want to insert only on POST
1896 * requests and this one isn't. Note that servers which don't have cookies
1897 * (eg: some backup servers) will return a full cookie removal request.
1898 */
Willy Tarreau88bc8002021-12-06 07:01:02 +00001899 if (!__objt_server(s->target)->cookie) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001900 chunk_printf(&trash,
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001901 "%s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
Christopher Faulete0768eb2018-10-03 16:38:02 +02001902 s->be->cookie_name);
1903 }
1904 else {
Willy Tarreau88bc8002021-12-06 07:01:02 +00001905 chunk_printf(&trash, "%s=%s", s->be->cookie_name, __objt_server(s->target)->cookie);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001906
1907 if (s->be->cookie_maxidle || s->be->cookie_maxlife) {
1908 /* emit last_date, which is mandatory */
1909 trash.area[trash.data++] = COOKIE_DELIM_DATE;
1910 s30tob64((date.tv_sec+3) >> 2,
1911 trash.area + trash.data);
1912 trash.data += 5;
1913
1914 if (s->be->cookie_maxlife) {
1915 /* emit first_date, which is either the original one or
1916 * the current date.
1917 */
1918 trash.area[trash.data++] = COOKIE_DELIM_DATE;
1919 s30tob64(txn->cookie_first_date ?
1920 txn->cookie_first_date >> 2 :
1921 (date.tv_sec+3) >> 2,
1922 trash.area + trash.data);
1923 trash.data += 5;
1924 }
1925 }
1926 chunk_appendf(&trash, "; path=/");
1927 }
1928
1929 if (s->be->cookie_domain)
1930 chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain);
1931
1932 if (s->be->ck_opts & PR_CK_HTTPONLY)
1933 chunk_appendf(&trash, "; HttpOnly");
1934
1935 if (s->be->ck_opts & PR_CK_SECURE)
1936 chunk_appendf(&trash, "; Secure");
1937
Christopher Faulet2f533902020-01-21 11:06:48 +01001938 if (s->be->cookie_attrs)
1939 chunk_appendf(&trash, "; %s", s->be->cookie_attrs);
1940
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001941 if (unlikely(!http_add_header(htx, ist("Set-Cookie"), ist2(trash.area, trash.data))))
Christopher Fauletb8a53712019-12-16 11:29:38 +01001942 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001943
1944 txn->flags &= ~TX_SCK_MASK;
1945 if (__objt_server(s->target)->cookie && (s->flags & SF_DIRECT))
1946 /* the server did not change, only the date was updated */
1947 txn->flags |= TX_SCK_UPDATED;
1948 else
1949 txn->flags |= TX_SCK_INSERTED;
1950
1951 /* Here, we will tell an eventual cache on the client side that we don't
1952 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
1953 * Some caches understand the correct form: 'no-cache="set-cookie"', but
1954 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
1955 */
1956 if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
1957
1958 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
1959
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001960 if (unlikely(!http_add_header(htx, ist("Cache-control"), ist("private"))))
Christopher Fauletb8a53712019-12-16 11:29:38 +01001961 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001962 }
1963 }
1964
1965 /*
1966 * Check if result will be cacheable with a cookie.
1967 * We'll block the response if security checks have caught
1968 * nasty things such as a cacheable cookie.
1969 */
1970 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
1971 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
1972 (s->be->options & PR_O_CHK_CACHE)) {
1973 /* we're in presence of a cacheable response containing
1974 * a set-cookie header. We'll block it as requested by
1975 * the 'checkcache' option, and send an alert.
1976 */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001977 ha_alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreau88bc8002021-12-06 07:01:02 +00001978 s->be->id, objt_server(s->target) ? __objt_server(s->target)->id : "<dispatch>");
Christopher Faulete0768eb2018-10-03 16:38:02 +02001979 send_log(s->be, LOG_ALERT,
1980 "Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreau88bc8002021-12-06 07:01:02 +00001981 s->be->id, objt_server(s->target) ? __objt_server(s->target)->id : "<dispatch>");
Christopher Fauletb8a53712019-12-16 11:29:38 +01001982 goto deny;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001983 }
1984
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001985 end:
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01001986 /*
1987 * Evaluate after-response rules before forwarding the response. rules
1988 * from the backend are evaluated first, then one from the frontend if
1989 * it differs.
1990 */
1991 if (!http_eval_after_res_rules(s))
1992 goto return_int_err;
1993
Christopher Fauletc2ac5e42021-03-08 18:20:09 +01001994 /* Filter the response headers if there are filters attached to the
1995 * stream.
1996 */
1997 if (HAS_FILTERS(s))
1998 rep->analysers |= AN_RES_FLT_HTTP_HDRS;
1999
Christopher Faulete0768eb2018-10-03 16:38:02 +02002000 /* Always enter in the body analyzer */
2001 rep->analysers &= ~AN_RES_FLT_XFER_DATA;
2002 rep->analysers |= AN_RES_HTTP_XFER_BODY;
2003
2004 /* if the user wants to log as soon as possible, without counting
2005 * bytes from the server, then this is the right moment. We have
2006 * to temporarily assign bytes_out to log what we currently have.
2007 */
2008 if (!LIST_ISEMPTY(&sess->fe->logformat) && !(s->logs.logwait & LW_BYTES)) {
2009 s->logs.t_close = s->logs.t_data; /* to get a valid end date */
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002010 s->logs.bytes_out = htx->data;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002011 s->do_log(s);
2012 s->logs.bytes_out = 0;
2013 }
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002014
Christopher Fauletb8a53712019-12-16 11:29:38 +01002015 done:
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002016 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002017 rep->analysers &= ~an_bit;
2018 rep->analyse_exp = TICK_ETERNITY;
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002019 s->current_rule = s->current_rule_list = NULL;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002020 return 1;
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002021
Christopher Fauletb8a53712019-12-16 11:29:38 +01002022 deny:
Willy Tarreau4781b152021-04-06 13:53:36 +02002023 _HA_ATOMIC_INC(&sess->fe->fe_counters.denied_resp);
2024 _HA_ATOMIC_INC(&s->be->be_counters.denied_resp);
William Lallemand36119de2021-03-08 15:26:48 +01002025 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02002026 _HA_ATOMIC_INC(&sess->listener->counters->denied_resp);
Christopher Fauleta08546b2019-12-16 16:07:34 +01002027 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02002028 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.denied_resp);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002029 goto return_prx_err;
2030
2031 return_int_err:
2032 txn->status = 500;
2033 if (!(s->flags & SF_ERR_MASK))
2034 s->flags |= SF_ERR_INTERNAL;
Willy Tarreau4781b152021-04-06 13:53:36 +02002035 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
2036 _HA_ATOMIC_INC(&s->be->be_counters.internal_errors);
Dragan Dosen9a006f92021-09-21 13:02:09 +02002037 if (sess->listener && sess->listener->counters)
2038 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Fauletcff0f732019-12-16 16:13:44 +01002039 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02002040 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.internal_errors);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002041 goto return_prx_err;
2042
2043 return_bad_res:
2044 txn->status = 502;
Willy Tarreau826f3ab2021-02-10 12:07:15 +01002045 stream_inc_http_fail_ctr(s);
Willy Tarreau4781b152021-04-06 13:53:36 +02002046 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Fauleta20a6532020-02-05 10:16:41 +01002047 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02002048 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Christopher Fauleta20a6532020-02-05 10:16:41 +01002049 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_RSP);
2050 }
Christopher Fauletb8a53712019-12-16 11:29:38 +01002051 /* fall through */
2052
2053 return_prx_err:
2054 http_reply_and_close(s, txn->status, http_error_message(s));
2055 /* fall through */
2056
2057 return_prx_cond:
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002058 s->logs.t_data = -1; /* was not a valid response */
Christopher Faulet8abe7122022-03-30 15:10:18 +02002059 s->csb->flags |= CS_FL_NOLINGER;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002060
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002061 if (!(s->flags & SF_ERR_MASK))
2062 s->flags |= SF_ERR_PRXCOND;
2063 if (!(s->flags & SF_FINST_MASK))
2064 s->flags |= SF_FINST_H;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002065
Christopher Faulete58c0002020-03-02 16:21:01 +01002066 rep->analysers &= AN_RES_FLT_END;
2067 s->req.analysers &= AN_REQ_FLT_END;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002068 rep->analyse_exp = TICK_ETERNITY;
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002069 s->current_rule = s->current_rule_list = NULL;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002070 DBG_TRACE_DEVEL("leaving on error",
2071 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002072 return 0;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002073
2074 return_prx_yield:
2075 channel_dont_close(rep);
2076 DBG_TRACE_DEVEL("waiting for more data",
2077 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
2078 return 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002079}
2080
2081/* This function is an analyser which forwards response body (including chunk
2082 * sizes if any). It is called as soon as we must forward, even if we forward
2083 * zero byte. The only situation where it must not be called is when we're in
2084 * tunnel mode and we want to forward till the close. It's used both to forward
2085 * remaining data and to resync after end of body. It expects the msg_state to
2086 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
2087 * read more data, or 1 once we can go on with next request or end the stream.
2088 *
2089 * It is capable of compressing response data both in content-length mode and
2090 * in chunked mode. The state machines follows different flows depending on
2091 * whether content-length and chunked modes are used, since there are no
2092 * trailers in content-length :
2093 *
2094 * chk-mode cl-mode
2095 * ,----- BODY -----.
2096 * / \
2097 * V size > 0 V chk-mode
2098 * .--> SIZE -------------> DATA -------------> CRLF
2099 * | | size == 0 | last byte |
2100 * | v final crlf v inspected |
2101 * | TRAILERS -----------> DONE |
2102 * | |
2103 * `----------------------------------------------'
2104 *
2105 * Compression only happens in the DATA state, and must be flushed in final
2106 * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding
2107 * is performed at once on final states for all bytes parsed, or when leaving
2108 * on missing data.
2109 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002110int http_response_forward_body(struct stream *s, struct channel *res, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +02002111{
2112 struct session *sess = s->sess;
2113 struct http_txn *txn = s->txn;
2114 struct http_msg *msg = &s->txn->rsp;
Christopher Faulet9768c262018-10-22 09:34:31 +02002115 struct htx *htx;
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002116 int ret;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002117
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002118 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002119
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002120 htx = htxbuf(&res->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002121
Christopher Fauletb9a92f32019-09-09 10:15:21 +02002122 if (htx->flags & HTX_FL_PARSING_ERROR)
2123 goto return_bad_res;
2124 if (htx->flags & HTX_FL_PROCESSING_ERROR)
2125 goto return_int_err;
2126
Christopher Faulete0768eb2018-10-03 16:38:02 +02002127 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Christopher Fauletf2824e62018-10-01 12:12:37 +02002128 ((res->flags & CF_SHUTW) && (res->to_forward || co_data(res)))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02002129 /* Output closed while we were sending data. We must abort and
2130 * wake the other side up.
2131 */
Christopher Faulete0768eb2018-10-03 16:38:02 +02002132 msg->msg_state = HTTP_MSG_ERROR;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002133 http_end_response(s);
2134 http_end_request(s);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002135 DBG_TRACE_DEVEL("leaving on error",
2136 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002137 return 1;
2138 }
2139
Christopher Faulet9768c262018-10-22 09:34:31 +02002140 if (msg->msg_state == HTTP_MSG_BODY)
2141 msg->msg_state = HTTP_MSG_DATA;
2142
Christopher Faulete0768eb2018-10-03 16:38:02 +02002143 /* in most states, we should abort in case of early close */
2144 channel_auto_close(res);
2145
Christopher Faulete0768eb2018-10-03 16:38:02 +02002146 if (res->to_forward) {
Christopher Faulet66af0b22019-03-22 14:54:52 +01002147 if (res->to_forward == CHN_INFINITE_FORWARD) {
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002148 if (res->flags & CF_EOI)
2149 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet66af0b22019-03-22 14:54:52 +01002150 }
2151 else {
2152 /* We can't process the buffer's contents yet */
2153 res->flags |= CF_WAKE_WRITE;
2154 goto missing_data_or_waiting;
2155 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02002156 }
2157
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002158 if (msg->msg_state >= HTTP_MSG_ENDING)
2159 goto ending;
2160
Christopher Fauletc75668e2020-12-07 18:10:32 +01002161 if ((txn->meth == HTTP_METH_CONNECT && txn->status >= 200 && txn->status < 300) || txn->status == 101 ||
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002162 (!(msg->flags & HTTP_MSGF_XFER_LEN) && !HAS_RSP_DATA_FILTERS(s))) {
2163 msg->msg_state = HTTP_MSG_ENDING;
2164 goto ending;
2165 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002166
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002167 /* Forward input data. We get it by removing all outgoing data not
2168 * forwarded yet from HTX data size. If there are some data filters, we
2169 * let them decide the amount of data to forward.
Christopher Faulet9768c262018-10-22 09:34:31 +02002170 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002171 if (HAS_RSP_DATA_FILTERS(s)) {
2172 ret = flt_http_payload(s, msg, htx->data);
2173 if (ret < 0)
2174 goto return_bad_res;
Christopher Faulet421e7692019-06-13 11:16:45 +02002175 c_adv(res, ret);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002176 }
2177 else {
Christopher Faulet421e7692019-06-13 11:16:45 +02002178 c_adv(res, htx->data - co_data(res));
Christopher Faulet66af0b22019-03-22 14:54:52 +01002179 if (msg->flags & HTTP_MSGF_XFER_LEN)
2180 channel_htx_forward_forever(res, htx);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002181 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002182
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002183 if (htx->data != co_data(res))
2184 goto missing_data_or_waiting;
2185
2186 if (!(msg->flags & HTTP_MSGF_XFER_LEN) && res->flags & CF_SHUTR) {
2187 msg->msg_state = HTTP_MSG_ENDING;
2188 goto ending;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002189 }
2190
Christopher Faulet9768c262018-10-22 09:34:31 +02002191 /* Check if the end-of-message is reached and if so, switch the message
Christopher Fauletd20fdb02019-06-13 16:43:22 +02002192 * in HTTP_MSG_ENDING state. Then if all data was marked to be
2193 * forwarded, set the state to HTTP_MSG_DONE.
Christopher Faulet9768c262018-10-22 09:34:31 +02002194 */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002195 if (!(htx->flags & HTX_FL_EOM))
Christopher Faulet9768c262018-10-22 09:34:31 +02002196 goto missing_data_or_waiting;
2197
Christopher Fauletd20fdb02019-06-13 16:43:22 +02002198 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet9768c262018-10-22 09:34:31 +02002199
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002200 ending:
Christopher Faulet2151cdd2020-07-22 16:34:59 +02002201 res->flags &= ~CF_EXPECT_MORE; /* no more data are expected */
2202
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002203 /* other states, ENDING...TUNNEL */
2204 if (msg->msg_state >= HTTP_MSG_DONE)
2205 goto done;
Christopher Faulet9768c262018-10-22 09:34:31 +02002206
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002207 if (HAS_RSP_DATA_FILTERS(s)) {
2208 ret = flt_http_end(s, msg);
2209 if (ret <= 0) {
2210 if (!ret)
2211 goto missing_data_or_waiting;
2212 goto return_bad_res;
2213 }
2214 }
2215
Christopher Fauletc75668e2020-12-07 18:10:32 +01002216 if ((txn->meth == HTTP_METH_CONNECT && txn->status >= 200 && txn->status < 300) || txn->status == 101 ||
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002217 !(msg->flags & HTTP_MSGF_XFER_LEN)) {
2218 msg->msg_state = HTTP_MSG_TUNNEL;
2219 goto ending;
2220 }
2221 else {
2222 msg->msg_state = HTTP_MSG_DONE;
2223 res->to_forward = 0;
2224 }
2225
2226 done:
2227
2228 channel_dont_close(res);
2229
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002230 http_end_response(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002231 if (!(res->analysers & an_bit)) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002232 http_end_request(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002233 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
2234 if (res->flags & CF_SHUTW) {
2235 /* response errors are most likely due to the
2236 * client aborting the transfer. */
Christopher Faulet93e02d82019-03-08 14:18:50 +01002237 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002238 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02002239 goto return_bad_res;
2240 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002241 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002242 return 1;
2243 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002244 DBG_TRACE_DEVEL("waiting for the end of the HTTP txn",
2245 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002246 return 0;
2247
2248 missing_data_or_waiting:
2249 if (res->flags & CF_SHUTW)
Christopher Faulet93e02d82019-03-08 14:18:50 +01002250 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002251
2252 /* stop waiting for data if the input is closed before the end. If the
2253 * client side was already closed, it means that the client has aborted,
2254 * so we don't want to count this as a server abort. Otherwise it's a
2255 * server abort.
2256 */
Christopher Fauletd20fdb02019-06-13 16:43:22 +02002257 if (msg->msg_state < HTTP_MSG_ENDING && res->flags & CF_SHUTR) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02002258 if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))
Christopher Faulet93e02d82019-03-08 14:18:50 +01002259 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002260 /* If we have some pending data, we continue the processing */
Christopher Faulet93e02d82019-03-08 14:18:50 +01002261 if (htx_is_empty(htx))
2262 goto return_srv_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002263 }
2264
Christopher Faulete0768eb2018-10-03 16:38:02 +02002265 /* When TE: chunked is used, we need to get there again to parse
2266 * remaining chunks even if the server has closed, so we don't want to
Christopher Faulet9768c262018-10-22 09:34:31 +02002267 * set CF_DONTCLOSE. Similarly when there is a content-leng or if there
2268 * are filters registered on the stream, we don't want to forward a
2269 * close
Christopher Faulete0768eb2018-10-03 16:38:02 +02002270 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002271 if ((msg->flags & HTTP_MSGF_XFER_LEN) || HAS_RSP_DATA_FILTERS(s))
Christopher Faulete0768eb2018-10-03 16:38:02 +02002272 channel_dont_close(res);
2273
2274 /* We know that more data are expected, but we couldn't send more that
2275 * what we did. So we always set the CF_EXPECT_MORE flag so that the
2276 * system knows it must not set a PUSH on this first part. Interactive
2277 * modes are already handled by the stream sock layer. We must not do
2278 * this in content-length mode because it could present the MSG_MORE
2279 * flag with the last block of forwarded data, which would cause an
2280 * additional delay to be observed by the receiver.
2281 */
Christopher Faulet2151cdd2020-07-22 16:34:59 +02002282 if (HAS_RSP_DATA_FILTERS(s))
Christopher Faulete0768eb2018-10-03 16:38:02 +02002283 res->flags |= CF_EXPECT_MORE;
2284
2285 /* the stream handler will take care of timeouts and errors */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002286 DBG_TRACE_DEVEL("waiting for more data to forward",
2287 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002288 return 0;
2289
Christopher Faulet93e02d82019-03-08 14:18:50 +01002290 return_srv_abort:
Willy Tarreau4781b152021-04-06 13:53:36 +02002291 _HA_ATOMIC_INC(&sess->fe->fe_counters.srv_aborts);
2292 _HA_ATOMIC_INC(&s->be->be_counters.srv_aborts);
William Lallemand36119de2021-03-08 15:26:48 +01002293 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02002294 _HA_ATOMIC_INC(&sess->listener->counters->srv_aborts);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002295 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02002296 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.srv_aborts);
Willy Tarreau826f3ab2021-02-10 12:07:15 +01002297 stream_inc_http_fail_ctr(s);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002298 if (!(s->flags & SF_ERR_MASK))
2299 s->flags |= SF_ERR_SRVCL;
2300 goto return_error;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002301
Christopher Faulet93e02d82019-03-08 14:18:50 +01002302 return_cli_abort:
Willy Tarreau4781b152021-04-06 13:53:36 +02002303 _HA_ATOMIC_INC(&sess->fe->fe_counters.cli_aborts);
2304 _HA_ATOMIC_INC(&s->be->be_counters.cli_aborts);
William Lallemand36119de2021-03-08 15:26:48 +01002305 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02002306 _HA_ATOMIC_INC(&sess->listener->counters->cli_aborts);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002307 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02002308 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.cli_aborts);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002309 if (!(s->flags & SF_ERR_MASK))
2310 s->flags |= SF_ERR_CLICL;
2311 goto return_error;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002312
Christopher Fauletb9a92f32019-09-09 10:15:21 +02002313 return_int_err:
Willy Tarreau4781b152021-04-06 13:53:36 +02002314 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
2315 _HA_ATOMIC_INC(&s->be->be_counters.internal_errors);
William Lallemand36119de2021-03-08 15:26:48 +01002316 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02002317 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002318 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02002319 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.internal_errors);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02002320 if (!(s->flags & SF_ERR_MASK))
2321 s->flags |= SF_ERR_INTERNAL;
2322 goto return_error;
2323
Christopher Faulet93e02d82019-03-08 14:18:50 +01002324 return_bad_res:
Willy Tarreau4781b152021-04-06 13:53:36 +02002325 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002326 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02002327 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002328 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_RSP);
2329 }
Willy Tarreau826f3ab2021-02-10 12:07:15 +01002330 stream_inc_http_fail_ctr(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002331 if (!(s->flags & SF_ERR_MASK))
Christopher Faulet93e02d82019-03-08 14:18:50 +01002332 s->flags |= SF_ERR_SRVCL;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002333 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +02002334
Christopher Faulet93e02d82019-03-08 14:18:50 +01002335 return_error:
Christopher Faulete0768eb2018-10-03 16:38:02 +02002336 /* don't send any error message as we're in the body */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002337 http_reply_and_close(s, txn->status, NULL);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002338 if (!(s->flags & SF_FINST_MASK))
2339 s->flags |= SF_FINST_D;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002340 DBG_TRACE_DEVEL("leaving on error",
2341 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002342 return 0;
2343}
2344
Christopher Fauletf2824e62018-10-01 12:12:37 +02002345/* Perform an HTTP redirect based on the information in <rule>. The function
Willy Tarreaubc1223b2021-09-02 16:54:33 +02002346 * returns zero in case of an irrecoverable error such as too large a request
2347 * to build a valid response, 1 in case of successful redirect (hence the rule
2348 * is final), or 2 if the rule has to be silently skipped.
Christopher Fauletf2824e62018-10-01 12:12:37 +02002349 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002350int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn)
Christopher Fauletf2824e62018-10-01 12:12:37 +02002351{
Christopher Faulet99daf282018-11-28 22:58:13 +01002352 struct channel *req = &s->req;
2353 struct channel *res = &s->res;
2354 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01002355 struct htx_sl *sl;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002356 struct buffer *chunk;
Christopher Faulet99daf282018-11-28 22:58:13 +01002357 struct ist status, reason, location;
2358 unsigned int flags;
Christopher Fauleteab17572022-04-26 20:34:38 +02002359 int ret = 1, close = 0; /* Try to keep the connection alive byt default */
Christopher Fauletf2824e62018-10-01 12:12:37 +02002360
2361 chunk = alloc_trash_chunk();
Christopher Fauletb8a53712019-12-16 11:29:38 +01002362 if (!chunk) {
2363 if (!(s->flags & SF_ERR_MASK))
2364 s->flags |= SF_ERR_RESOURCE;
Christopher Faulet99daf282018-11-28 22:58:13 +01002365 goto fail;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002366 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002367
Christopher Faulet99daf282018-11-28 22:58:13 +01002368 /*
2369 * Create the location
2370 */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002371 htx = htxbuf(&req->buf);
Christopher Fauletf2824e62018-10-01 12:12:37 +02002372 switch(rule->type) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002373 case REDIRECT_TYPE_SCHEME: {
2374 struct http_hdr_ctx ctx;
2375 struct ist path, host;
Amaury Denoyellec453f952021-07-06 11:40:12 +02002376 struct http_uri_parser parser;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002377
Christopher Faulet99daf282018-11-28 22:58:13 +01002378 host = ist("");
2379 ctx.blk = NULL;
2380 if (http_find_header(htx, ist("Host"), &ctx, 0))
2381 host = ctx.value;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002382
Christopher Faulet297fbb42019-05-13 14:41:27 +02002383 sl = http_get_stline(htx);
Amaury Denoyellec453f952021-07-06 11:40:12 +02002384 parser = http_uri_parser_init(htx_sl_req_uri(sl));
2385 path = http_parse_path(&parser);
Christopher Faulet99daf282018-11-28 22:58:13 +01002386 /* build message using path */
Tim Duesterhused526372020-03-05 17:56:33 +01002387 if (isttest(path)) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002388 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2389 int qs = 0;
2390 while (qs < path.len) {
2391 if (*(path.ptr + qs) == '?') {
2392 path.len = qs;
2393 break;
2394 }
2395 qs++;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002396 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002397 }
2398 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002399 else
2400 path = ist("/");
Christopher Fauletf2824e62018-10-01 12:12:37 +02002401
Christopher Faulet99daf282018-11-28 22:58:13 +01002402 if (rule->rdr_str) { /* this is an old "redirect" rule */
2403 /* add scheme */
2404 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2405 goto fail;
2406 }
2407 else {
2408 /* add scheme with executing log format */
2409 chunk->data += build_logline(s, chunk->area + chunk->data,
2410 chunk->size - chunk->data,
2411 &rule->rdr_fmt);
2412 }
2413 /* add "://" + host + path */
2414 if (!chunk_memcat(chunk, "://", 3) ||
2415 !chunk_memcat(chunk, host.ptr, host.len) ||
2416 !chunk_memcat(chunk, path.ptr, path.len))
2417 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002418
Christopher Faulet99daf282018-11-28 22:58:13 +01002419 /* append a slash at the end of the location if needed and missing */
2420 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
2421 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
2422 if (chunk->data + 1 >= chunk->size)
2423 goto fail;
2424 chunk->area[chunk->data++] = '/';
2425 }
2426 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002427 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002428
Christopher Faulet99daf282018-11-28 22:58:13 +01002429 case REDIRECT_TYPE_PREFIX: {
2430 struct ist path;
Amaury Denoyellec453f952021-07-06 11:40:12 +02002431 struct http_uri_parser parser;
Christopher Faulet99daf282018-11-28 22:58:13 +01002432
Christopher Faulet297fbb42019-05-13 14:41:27 +02002433 sl = http_get_stline(htx);
Amaury Denoyellec453f952021-07-06 11:40:12 +02002434 parser = http_uri_parser_init(htx_sl_req_uri(sl));
2435 path = http_parse_path(&parser);
Christopher Faulet99daf282018-11-28 22:58:13 +01002436 /* build message using path */
Tim Duesterhused526372020-03-05 17:56:33 +01002437 if (isttest(path)) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002438 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2439 int qs = 0;
2440 while (qs < path.len) {
2441 if (*(path.ptr + qs) == '?') {
2442 path.len = qs;
2443 break;
2444 }
2445 qs++;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002446 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002447 }
2448 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002449 else
2450 path = ist("/");
Christopher Fauletf2824e62018-10-01 12:12:37 +02002451
Christopher Faulet99daf282018-11-28 22:58:13 +01002452 if (rule->rdr_str) { /* this is an old "redirect" rule */
2453 /* add prefix. Note that if prefix == "/", we don't want to
2454 * add anything, otherwise it makes it hard for the user to
2455 * configure a self-redirection.
2456 */
2457 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
2458 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2459 goto fail;
2460 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002461 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002462 else {
2463 /* add prefix with executing log format */
2464 chunk->data += build_logline(s, chunk->area + chunk->data,
2465 chunk->size - chunk->data,
2466 &rule->rdr_fmt);
2467 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002468
Christopher Faulet99daf282018-11-28 22:58:13 +01002469 /* add path */
2470 if (!chunk_memcat(chunk, path.ptr, path.len))
2471 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002472
Christopher Faulet99daf282018-11-28 22:58:13 +01002473 /* append a slash at the end of the location if needed and missing */
2474 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
2475 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
2476 if (chunk->data + 1 >= chunk->size)
2477 goto fail;
2478 chunk->area[chunk->data++] = '/';
2479 }
2480 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002481 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002482 case REDIRECT_TYPE_LOCATION:
2483 default:
2484 if (rule->rdr_str) { /* this is an old "redirect" rule */
2485 /* add location */
2486 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2487 goto fail;
2488 }
2489 else {
2490 /* add location with executing log format */
Willy Tarreaubc1223b2021-09-02 16:54:33 +02002491 int len = build_logline(s, chunk->area + chunk->data,
2492 chunk->size - chunk->data,
2493 &rule->rdr_fmt);
Christopher Fauleteab17572022-04-26 20:34:38 +02002494 if (!len && rule->flags & REDIRECT_FLAG_IGNORE_EMPTY) {
2495 ret = 2;
2496 goto out;
2497 }
Willy Tarreaubc1223b2021-09-02 16:54:33 +02002498
2499 chunk->data += len;
Christopher Faulet99daf282018-11-28 22:58:13 +01002500 }
2501 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002502 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002503 location = ist2(chunk->area, chunk->data);
2504
2505 /*
2506 * Create the 30x response
2507 */
2508 switch (rule->code) {
2509 case 308:
2510 status = ist("308");
2511 reason = ist("Permanent Redirect");
2512 break;
2513 case 307:
2514 status = ist("307");
2515 reason = ist("Temporary Redirect");
2516 break;
2517 case 303:
2518 status = ist("303");
2519 reason = ist("See Other");
2520 break;
2521 case 301:
2522 status = ist("301");
2523 reason = ist("Moved Permanently");
2524 break;
2525 case 302:
2526 default:
2527 status = ist("302");
2528 reason = ist("Found");
2529 break;
2530 }
2531
Christopher Faulet08e66462019-05-23 16:44:59 +02002532 if (!(txn->req.flags & HTTP_MSGF_BODYLESS) && txn->req.msg_state != HTTP_MSG_DONE)
2533 close = 1;
2534
Christopher Faulet99daf282018-11-28 22:58:13 +01002535 htx = htx_from_buf(&res->buf);
Kevin Zhu96b36392020-01-07 09:42:55 +01002536 /* Trim any possible response */
2537 channel_htx_truncate(&s->res, htx);
Christopher Faulet99daf282018-11-28 22:58:13 +01002538 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
2539 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), status, reason);
2540 if (!sl)
2541 goto fail;
2542 sl->info.res.status = rule->code;
2543 s->txn->status = rule->code;
2544
Christopher Faulet08e66462019-05-23 16:44:59 +02002545 if (close && !htx_add_header(htx, ist("Connection"), ist("close")))
2546 goto fail;
2547
2548 if (!htx_add_header(htx, ist("Content-length"), ist("0")) ||
Christopher Faulet99daf282018-11-28 22:58:13 +01002549 !htx_add_header(htx, ist("Location"), location))
2550 goto fail;
2551
2552 if (rule->code == 302 || rule->code == 303 || rule->code == 307) {
2553 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")))
2554 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002555 }
2556
2557 if (rule->cookie_len) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002558 if (!htx_add_header(htx, ist("Set-Cookie"), ist2(rule->cookie_str, rule->cookie_len)))
2559 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002560 }
2561
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002562 if (!htx_add_endof(htx, HTX_BLK_EOH))
Christopher Faulet99daf282018-11-28 22:58:13 +01002563 goto fail;
2564
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002565 htx->flags |= HTX_FL_EOM;
Kevin Zhu96b36392020-01-07 09:42:55 +01002566 htx_to_buf(htx, &res->buf);
Christopher Fauleta72a7e42020-01-28 09:28:11 +01002567 if (!http_forward_proxy_resp(s, 1))
2568 goto fail;
Christopher Faulet99daf282018-11-28 22:58:13 +01002569
Christopher Faulet60b33a52020-01-28 09:18:10 +01002570 if (rule->flags & REDIRECT_FLAG_FROM_REQ) {
2571 /* let's log the request time */
2572 s->logs.tv_request = now;
Christopher Fauletd3475882021-10-04 14:16:46 +02002573 req->analysers &= AN_REQ_FLT_END;
Christopher Faulet99daf282018-11-28 22:58:13 +01002574
Christopher Faulet60b33a52020-01-28 09:18:10 +01002575 if (s->sess->fe == s->be) /* report it if the request was intercepted by the frontend */
Willy Tarreau4781b152021-04-06 13:53:36 +02002576 _HA_ATOMIC_INC(&s->sess->fe->fe_counters.intercepted_req);
Christopher Faulet60b33a52020-01-28 09:18:10 +01002577 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002578
2579 if (!(s->flags & SF_ERR_MASK))
2580 s->flags |= SF_ERR_LOCAL;
2581 if (!(s->flags & SF_FINST_MASK))
Christopher Faulet60b33a52020-01-28 09:18:10 +01002582 s->flags |= ((rule->flags & REDIRECT_FLAG_FROM_REQ) ? SF_FINST_R : SF_FINST_H);
Christopher Fauletf2824e62018-10-01 12:12:37 +02002583
Christopher Fauleteab17572022-04-26 20:34:38 +02002584 out:
Christopher Faulet99daf282018-11-28 22:58:13 +01002585 free_trash_chunk(chunk);
Christopher Fauleteab17572022-04-26 20:34:38 +02002586 return ret;
Christopher Faulet99daf282018-11-28 22:58:13 +01002587
2588 fail:
2589 /* If an error occurred, remove the incomplete HTTP response from the
2590 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01002591 channel_htx_truncate(res, htxbuf(&res->buf));
Christopher Fauleteab17572022-04-26 20:34:38 +02002592 ret = 0;
2593 goto out;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002594}
2595
Christopher Faulet18c13d32022-05-16 11:43:10 +02002596/* This function filters the request header names to only allow [0-9a-zA-Z-]
2597 * characters. Depending on the proxy configuration, headers with a name not
2598 * matching this charset are removed or the request is rejected with a
2599 * 403-Forbidden response if such name are found. It returns HTTP_RULE_RES_CONT
2600 * to continue the request processing or HTTP_RULE_RES_DENY if the request is
2601 * rejected.
2602 */
2603static enum rule_result http_req_restrict_header_names(struct stream *s, struct htx *htx, struct proxy *px)
2604{
2605 struct htx_blk *blk;
2606 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
2607
2608 blk = htx_get_first_blk(htx);
2609 while (blk) {
2610 enum htx_blk_type type = htx_get_blk_type(blk);
2611
2612 if (type == HTX_BLK_HDR) {
2613 struct ist n = htx_get_blk_name(htx, blk);
2614 int i;
2615
2616 for (i = 0; i < istlen(n); i++) {
2617 if (!isalnum((unsigned char)n.ptr[i]) && n.ptr[i] != '-') {
2618 /* Block the request or remove the header */
2619 if (px->options2 & PR_O2_RSTRICT_REQ_HDR_NAMES_BLK)
2620 goto block;
2621 blk = htx_remove_blk(htx, blk);
2622 continue;
2623 }
2624 }
2625 }
2626 if (type == HTX_BLK_EOH)
2627 break;
2628
2629 blk = htx_get_next_blk(htx, blk);
2630 }
2631 out:
2632 return rule_ret;
2633 block:
2634 /* Block the request returning a 403-Forbidden response */
2635 s->txn->status = 403;
2636 rule_ret = HTTP_RULE_RES_DENY;
2637 goto out;
2638}
2639
Christopher Faulet92d34fe2019-12-17 09:20:34 +01002640/* Replace all headers matching the name <name>. The header value is replaced if
2641 * it matches the regex <re>. <str> is used for the replacement. If <full> is
2642 * set to 1, the full-line is matched and replaced. Otherwise, comma-separated
2643 * values are evaluated one by one. It returns 0 on success and -1 on error.
2644 */
2645int http_replace_hdrs(struct stream* s, struct htx *htx, struct ist name,
2646 const char *str, struct my_regex *re, int full)
Christopher Faulet72333522018-10-24 11:25:02 +02002647{
2648 struct http_hdr_ctx ctx;
2649 struct buffer *output = get_trash_chunk();
2650
Christopher Faulet72333522018-10-24 11:25:02 +02002651 ctx.blk = NULL;
Christopher Faulet92d34fe2019-12-17 09:20:34 +01002652 while (http_find_header(htx, name, &ctx, full)) {
Christopher Faulet72333522018-10-24 11:25:02 +02002653 if (!regex_exec_match2(re, ctx.value.ptr, ctx.value.len, MAX_MATCH, pmatch, 0))
2654 continue;
2655
2656 output->data = exp_replace(output->area, output->size, ctx.value.ptr, str, pmatch);
2657 if (output->data == -1)
2658 return -1;
2659 if (!http_replace_header_value(htx, &ctx, ist2(output->area, output->data)))
2660 return -1;
2661 }
2662 return 0;
2663}
2664
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002665/* This function executes one of the set-{method,path,query,uri} actions. It
2666 * takes the string from the variable 'replace' with length 'len', then modifies
2667 * the relevant part of the request line accordingly. Then it updates various
2668 * pointers to the next elements which were moved, and the total buffer length.
2669 * It finds the action to be performed in p[2], previously filled by function
2670 * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal
2671 * error, though this can be revisited when this code is finally exploited.
2672 *
2673 * 'action' can be '0' to replace method, '1' to replace path, '2' to replace
Christopher Faulet312294f2020-09-02 17:17:44 +02002674 * query string, 3 to replace uri or 4 to replace the path+query.
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002675 *
2676 * In query string case, the mark question '?' must be set at the start of the
2677 * string by the caller, event if the replacement query string is empty.
2678 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002679int http_req_replace_stline(int action, const char *replace, int len,
2680 struct proxy *px, struct stream *s)
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002681{
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002682 struct htx *htx = htxbuf(&s->req.buf);
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002683
2684 switch (action) {
2685 case 0: // method
2686 if (!http_replace_req_meth(htx, ist2(replace, len)))
2687 return -1;
2688 break;
2689
2690 case 1: // path
Christopher Fauletb8ce5052020-08-31 16:11:57 +02002691 if (!http_replace_req_path(htx, ist2(replace, len), 0))
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002692 return -1;
2693 break;
2694
2695 case 2: // query
2696 if (!http_replace_req_query(htx, ist2(replace, len)))
2697 return -1;
2698 break;
2699
2700 case 3: // uri
2701 if (!http_replace_req_uri(htx, ist2(replace, len)))
2702 return -1;
2703 break;
2704
Christopher Faulet312294f2020-09-02 17:17:44 +02002705 case 4: // path + query
2706 if (!http_replace_req_path(htx, ist2(replace, len), 1))
2707 return -1;
2708 break;
2709
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002710 default:
2711 return -1;
2712 }
2713 return 0;
2714}
2715
2716/* This function replace the HTTP status code and the associated message. The
Christopher Faulete00d06c2019-12-16 17:18:42 +01002717 * variable <status> contains the new status code. This function never fails. It
2718 * returns 0 in case of success, -1 in case of internal error.
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002719 */
Christopher Faulet96bff762019-12-17 13:46:18 +01002720int http_res_set_status(unsigned int status, struct ist reason, struct stream *s)
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002721{
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002722 struct htx *htx = htxbuf(&s->res.buf);
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002723 char *res;
2724
2725 chunk_reset(&trash);
2726 res = ultoa_o(status, trash.area, trash.size);
2727 trash.data = res - trash.area;
2728
2729 /* Do we have a custom reason format string? */
Tim Duesterhuse296d3e2020-03-05 17:56:31 +01002730 if (!isttest(reason)) {
Christopher Faulet96bff762019-12-17 13:46:18 +01002731 const char *str = http_get_reason(status);
Tim Duesterhusdcf753a2021-03-04 17:31:47 +01002732 reason = ist(str);
Christopher Faulet96bff762019-12-17 13:46:18 +01002733 }
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002734
Christopher Fauletbde2c4c2020-08-31 16:43:34 +02002735 if (!http_replace_res_status(htx, ist2(trash.area, trash.data), reason))
Christopher Faulete00d06c2019-12-16 17:18:42 +01002736 return -1;
2737 return 0;
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002738}
2739
Christopher Faulet3e964192018-10-24 11:39:23 +02002740/* Executes the http-request rules <rules> for stream <s>, proxy <px> and
2741 * transaction <txn>. Returns the verdict of the first rule that prevents
2742 * further processing of the request (auth, deny, ...), and defaults to
2743 * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or
2744 * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT
2745 * on txn->flags if it encounters a tarpit rule. If <deny_status> is not NULL
2746 * and a deny/tarpit rule is matched, it will be filled with this rule's deny
2747 * status.
2748 */
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002749static enum rule_result http_req_get_intercept_rule(struct proxy *px, struct list *def_rules,
2750 struct list *rules, struct stream *s)
Christopher Faulet3e964192018-10-24 11:39:23 +02002751{
2752 struct session *sess = strm_sess(s);
2753 struct http_txn *txn = s->txn;
Christopher Faulet3e964192018-10-24 11:39:23 +02002754 struct act_rule *rule;
Christopher Faulet3e964192018-10-24 11:39:23 +02002755 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002756 int act_opts = 0;
Christopher Faulet3e964192018-10-24 11:39:23 +02002757
Christopher Faulet3e964192018-10-24 11:39:23 +02002758 /* If "the current_rule_list" match the executed rule list, we are in
2759 * resume condition. If a resume is needed it is always in the action
2760 * and never in the ACL or converters. In this case, we initialise the
2761 * current rule, and go to the action execution point.
2762 */
2763 if (s->current_rule) {
2764 rule = s->current_rule;
2765 s->current_rule = NULL;
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002766 if (s->current_rule_list == rules || (def_rules && s->current_rule_list == def_rules))
Christopher Faulet3e964192018-10-24 11:39:23 +02002767 goto resume_execution;
2768 }
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002769 s->current_rule_list = ((!def_rules || s->current_rule_list == def_rules) ? rules : def_rules);
Christopher Faulet3e964192018-10-24 11:39:23 +02002770
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002771 restart:
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002772 /* start the ruleset evaluation in strict mode */
2773 txn->req.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01002774
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002775 list_for_each_entry(rule, s->current_rule_list, list) {
Christopher Faulet3e964192018-10-24 11:39:23 +02002776 /* check optional condition */
2777 if (rule->cond) {
2778 int ret;
2779
2780 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
2781 ret = acl_pass(ret);
2782
2783 if (rule->cond->pol == ACL_COND_UNLESS)
2784 ret = !ret;
2785
2786 if (!ret) /* condition not matched */
2787 continue;
2788 }
2789
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002790 act_opts |= ACT_OPT_FIRST;
Christopher Faulet3e964192018-10-24 11:39:23 +02002791 resume_execution:
Amaury Denoyelle03517732021-05-07 14:25:01 +02002792 if (rule->kw->flags & KWF_EXPERIMENTAL)
2793 mark_tainted(TAINTED_ACTION_EXP_EXECUTED);
2794
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002795 /* Always call the action function if defined */
2796 if (rule->action_ptr) {
2797 if ((s->req.flags & CF_READ_ERROR) ||
2798 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
2799 (px->options & PR_O_ABRT_CLOSE)))
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002800 act_opts |= ACT_OPT_FINAL;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002801
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002802 switch (rule->action_ptr(rule, px, sess, s, act_opts)) {
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002803 case ACT_RET_CONT:
2804 break;
2805 case ACT_RET_STOP:
2806 rule_ret = HTTP_RULE_RES_STOP;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002807 s->last_rule_file = rule->conf.file;
2808 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002809 goto end;
2810 case ACT_RET_YIELD:
2811 s->current_rule = rule;
2812 rule_ret = HTTP_RULE_RES_YIELD;
2813 goto end;
2814 case ACT_RET_ERR:
2815 rule_ret = HTTP_RULE_RES_ERROR;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002816 s->last_rule_file = rule->conf.file;
2817 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002818 goto end;
2819 case ACT_RET_DONE:
2820 rule_ret = HTTP_RULE_RES_DONE;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002821 s->last_rule_file = rule->conf.file;
2822 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002823 goto end;
2824 case ACT_RET_DENY:
Christopher Fauletb58f62b2020-01-13 16:40:13 +01002825 if (txn->status == -1)
2826 txn->status = 403;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002827 rule_ret = HTTP_RULE_RES_DENY;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002828 s->last_rule_file = rule->conf.file;
2829 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002830 goto end;
2831 case ACT_RET_ABRT:
2832 rule_ret = HTTP_RULE_RES_ABRT;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002833 s->last_rule_file = rule->conf.file;
2834 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002835 goto end;
2836 case ACT_RET_INV:
2837 rule_ret = HTTP_RULE_RES_BADREQ;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002838 s->last_rule_file = rule->conf.file;
2839 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002840 goto end;
2841 }
2842 continue; /* eval the next rule */
2843 }
2844
2845 /* If not action function defined, check for known actions */
Christopher Faulet3e964192018-10-24 11:39:23 +02002846 switch (rule->action) {
2847 case ACT_ACTION_ALLOW:
2848 rule_ret = HTTP_RULE_RES_STOP;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002849 s->last_rule_file = rule->conf.file;
2850 s->last_rule_line = rule->conf.line;
Christopher Faulet3e964192018-10-24 11:39:23 +02002851 goto end;
2852
2853 case ACT_ACTION_DENY:
Christopher Faulet5cb513a2020-05-13 17:56:56 +02002854 txn->status = rule->arg.http_reply->status;
2855 txn->http_reply = rule->arg.http_reply;
Christopher Faulet3e964192018-10-24 11:39:23 +02002856 rule_ret = HTTP_RULE_RES_DENY;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002857 s->last_rule_file = rule->conf.file;
2858 s->last_rule_line = rule->conf.line;
Christopher Faulet3e964192018-10-24 11:39:23 +02002859 goto end;
2860
2861 case ACT_HTTP_REQ_TARPIT:
2862 txn->flags |= TX_CLTARPIT;
Christopher Faulet5cb513a2020-05-13 17:56:56 +02002863 txn->status = rule->arg.http_reply->status;
2864 txn->http_reply = rule->arg.http_reply;
Christopher Faulet3e964192018-10-24 11:39:23 +02002865 rule_ret = HTTP_RULE_RES_DENY;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002866 s->last_rule_file = rule->conf.file;
2867 s->last_rule_line = rule->conf.line;
Christopher Faulet3e964192018-10-24 11:39:23 +02002868 goto end;
2869
Willy Tarreaubc1223b2021-09-02 16:54:33 +02002870 case ACT_HTTP_REDIR: {
2871 int ret = http_apply_redirect_rule(rule->arg.redir, s, txn);
2872
2873 if (ret == 2) // 2 == skip
2874 break;
2875
2876 rule_ret = ret ? HTTP_RULE_RES_ABRT : HTTP_RULE_RES_ERROR;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002877 s->last_rule_file = rule->conf.file;
2878 s->last_rule_line = rule->conf.line;
Christopher Faulet3e964192018-10-24 11:39:23 +02002879 goto end;
Willy Tarreaubc1223b2021-09-02 16:54:33 +02002880 }
Christopher Faulet3e964192018-10-24 11:39:23 +02002881
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002882 /* other flags exists, but normally, they never be matched. */
Christopher Faulet3e964192018-10-24 11:39:23 +02002883 default:
2884 break;
2885 }
2886 }
2887
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002888 if (def_rules && s->current_rule_list == def_rules) {
2889 s->current_rule_list = rules;
2890 goto restart;
2891 }
2892
Christopher Faulet3e964192018-10-24 11:39:23 +02002893 end:
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002894 /* if the ruleset evaluation is finished reset the strict mode */
Christopher Faulet46f95542019-12-20 10:07:22 +01002895 if (rule_ret != HTTP_RULE_RES_YIELD)
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002896 txn->req.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01002897
Christopher Faulet3e964192018-10-24 11:39:23 +02002898 /* we reached the end of the rules, nothing to report */
2899 return rule_ret;
2900}
2901
2902/* Executes the http-response rules <rules> for stream <s> and proxy <px>. It
2903 * returns one of 5 possible statuses: HTTP_RULE_RES_CONT, HTTP_RULE_RES_STOP,
2904 * HTTP_RULE_RES_DONE, HTTP_RULE_RES_YIELD, or HTTP_RULE_RES_BADREQ. If *CONT
2905 * is returned, the process can continue the evaluation of next rule list. If
2906 * *STOP or *DONE is returned, the process must stop the evaluation. If *BADREQ
2907 * is returned, it means the operation could not be processed and a server error
Christopher Fauleta53abad2020-05-13 08:12:22 +02002908 * must be returned. If *YIELD is returned, the caller must call again the
2909 * function with the same context.
Christopher Faulet3e964192018-10-24 11:39:23 +02002910 */
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002911static enum rule_result http_res_get_intercept_rule(struct proxy *px, struct list *def_rules,
2912 struct list *rules, struct stream *s)
Christopher Faulet3e964192018-10-24 11:39:23 +02002913{
2914 struct session *sess = strm_sess(s);
2915 struct http_txn *txn = s->txn;
Christopher Faulet3e964192018-10-24 11:39:23 +02002916 struct act_rule *rule;
Christopher Faulet3e964192018-10-24 11:39:23 +02002917 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002918 int act_opts = 0;
Christopher Faulet3e964192018-10-24 11:39:23 +02002919
Christopher Faulet3e964192018-10-24 11:39:23 +02002920 /* If "the current_rule_list" match the executed rule list, we are in
2921 * resume condition. If a resume is needed it is always in the action
2922 * and never in the ACL or converters. In this case, we initialise the
2923 * current rule, and go to the action execution point.
2924 */
2925 if (s->current_rule) {
2926 rule = s->current_rule;
2927 s->current_rule = NULL;
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002928 if (s->current_rule_list == rules || (def_rules && s->current_rule_list == def_rules))
Christopher Faulet3e964192018-10-24 11:39:23 +02002929 goto resume_execution;
2930 }
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002931 s->current_rule_list = ((!def_rules || s->current_rule_list == def_rules) ? rules : def_rules);
2932
2933 restart:
Christopher Faulet3e964192018-10-24 11:39:23 +02002934
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002935 /* start the ruleset evaluation in strict mode */
2936 txn->rsp.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01002937
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002938 list_for_each_entry(rule, s->current_rule_list, list) {
Christopher Faulet3e964192018-10-24 11:39:23 +02002939 /* check optional condition */
2940 if (rule->cond) {
2941 int ret;
2942
2943 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
2944 ret = acl_pass(ret);
2945
2946 if (rule->cond->pol == ACL_COND_UNLESS)
2947 ret = !ret;
2948
2949 if (!ret) /* condition not matched */
2950 continue;
2951 }
2952
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002953 act_opts |= ACT_OPT_FIRST;
Christopher Faulet3e964192018-10-24 11:39:23 +02002954resume_execution:
Amaury Denoyelle03517732021-05-07 14:25:01 +02002955 if (rule->kw->flags & KWF_EXPERIMENTAL)
2956 mark_tainted(TAINTED_ACTION_EXP_EXECUTED);
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002957
2958 /* Always call the action function if defined */
2959 if (rule->action_ptr) {
2960 if ((s->req.flags & CF_READ_ERROR) ||
2961 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
2962 (px->options & PR_O_ABRT_CLOSE)))
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002963 act_opts |= ACT_OPT_FINAL;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002964
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002965 switch (rule->action_ptr(rule, px, sess, s, act_opts)) {
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002966 case ACT_RET_CONT:
2967 break;
2968 case ACT_RET_STOP:
2969 rule_ret = HTTP_RULE_RES_STOP;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002970 s->last_rule_file = rule->conf.file;
2971 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002972 goto end;
2973 case ACT_RET_YIELD:
2974 s->current_rule = rule;
2975 rule_ret = HTTP_RULE_RES_YIELD;
2976 goto end;
2977 case ACT_RET_ERR:
2978 rule_ret = HTTP_RULE_RES_ERROR;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002979 s->last_rule_file = rule->conf.file;
2980 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002981 goto end;
2982 case ACT_RET_DONE:
2983 rule_ret = HTTP_RULE_RES_DONE;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002984 s->last_rule_file = rule->conf.file;
2985 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002986 goto end;
2987 case ACT_RET_DENY:
Christopher Fauletb58f62b2020-01-13 16:40:13 +01002988 if (txn->status == -1)
2989 txn->status = 502;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002990 rule_ret = HTTP_RULE_RES_DENY;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002991 s->last_rule_file = rule->conf.file;
2992 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002993 goto end;
2994 case ACT_RET_ABRT:
2995 rule_ret = HTTP_RULE_RES_ABRT;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002996 s->last_rule_file = rule->conf.file;
2997 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002998 goto end;
2999 case ACT_RET_INV:
3000 rule_ret = HTTP_RULE_RES_BADREQ;
Willy Tarreauc6dae862022-03-09 17:23:10 +01003001 s->last_rule_file = rule->conf.file;
3002 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01003003 goto end;
3004 }
3005 continue; /* eval the next rule */
3006 }
3007
3008 /* If not action function defined, check for known actions */
Christopher Faulet3e964192018-10-24 11:39:23 +02003009 switch (rule->action) {
3010 case ACT_ACTION_ALLOW:
3011 rule_ret = HTTP_RULE_RES_STOP; /* "allow" rules are OK */
Willy Tarreauc6dae862022-03-09 17:23:10 +01003012 s->last_rule_file = rule->conf.file;
3013 s->last_rule_line = rule->conf.line;
Christopher Faulet3e964192018-10-24 11:39:23 +02003014 goto end;
3015
3016 case ACT_ACTION_DENY:
Christopher Faulet5cb513a2020-05-13 17:56:56 +02003017 txn->status = rule->arg.http_reply->status;
3018 txn->http_reply = rule->arg.http_reply;
Christopher Faulet3a26bee2019-12-16 12:47:40 +01003019 rule_ret = HTTP_RULE_RES_DENY;
Willy Tarreauc6dae862022-03-09 17:23:10 +01003020 s->last_rule_file = rule->conf.file;
3021 s->last_rule_line = rule->conf.line;
Christopher Faulet3e964192018-10-24 11:39:23 +02003022 goto end;
3023
Willy Tarreaubc1223b2021-09-02 16:54:33 +02003024 case ACT_HTTP_REDIR: {
3025 int ret = http_apply_redirect_rule(rule->arg.redir, s, txn);
Christopher Faulet3e964192018-10-24 11:39:23 +02003026
Willy Tarreaubc1223b2021-09-02 16:54:33 +02003027 if (ret == 2) // 2 == skip
3028 break;
3029
3030 rule_ret = ret ? HTTP_RULE_RES_ABRT : HTTP_RULE_RES_ERROR;
Willy Tarreauc6dae862022-03-09 17:23:10 +01003031 s->last_rule_file = rule->conf.file;
3032 s->last_rule_line = rule->conf.line;
Willy Tarreaubc1223b2021-09-02 16:54:33 +02003033 goto end;
3034 }
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01003035 /* other flags exists, but normally, they never be matched. */
Christopher Faulet3e964192018-10-24 11:39:23 +02003036 default:
3037 break;
3038 }
3039 }
3040
Christopher Fauletd4150ad2021-10-13 15:35:55 +02003041 if (def_rules && s->current_rule_list == def_rules) {
3042 s->current_rule_list = rules;
3043 goto restart;
3044 }
3045
Christopher Faulet3e964192018-10-24 11:39:23 +02003046 end:
Christopher Faulet1aea50e2020-01-17 16:03:53 +01003047 /* if the ruleset evaluation is finished reset the strict mode */
Christopher Faulet46f95542019-12-20 10:07:22 +01003048 if (rule_ret != HTTP_RULE_RES_YIELD)
Christopher Faulet1aea50e2020-01-17 16:03:53 +01003049 txn->rsp.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01003050
Christopher Faulet3e964192018-10-24 11:39:23 +02003051 /* we reached the end of the rules, nothing to report */
3052 return rule_ret;
3053}
3054
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01003055/* Executes backend and frontend http-after-response rules for the stream <s>,
3056 * in that order. it return 1 on success and 0 on error. It is the caller
3057 * responsibility to catch error or ignore it. If it catches it, this function
3058 * may be called a second time, for the internal error.
3059 */
3060int http_eval_after_res_rules(struct stream *s)
3061{
Christopher Fauletd4150ad2021-10-13 15:35:55 +02003062 struct list *def_rules, *rules;
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01003063 struct session *sess = s->sess;
3064 enum rule_result ret = HTTP_RULE_RES_CONT;
3065
Christopher Faulet507479b2020-05-15 12:29:46 +02003066 /* Eval after-response ruleset only if the reply is not const */
3067 if (s->txn->flags & TX_CONST_REPLY)
3068 goto end;
3069
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01003070 /* prune the request variables if not already done and swap to the response variables. */
3071 if (s->vars_reqres.scope != SCOPE_RES) {
3072 if (!LIST_ISEMPTY(&s->vars_reqres.head))
3073 vars_prune(&s->vars_reqres, s->sess, s);
Willy Tarreaub7bfcb32021-08-31 08:13:25 +02003074 vars_init_head(&s->vars_reqres, SCOPE_RES);
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01003075 }
3076
Christopher Fauletd4150ad2021-10-13 15:35:55 +02003077 def_rules = (s->be->defpx ? &s->be->defpx->http_after_res_rules : NULL);
3078 rules = &s->be->http_after_res_rules;
3079
3080 ret = http_res_get_intercept_rule(s->be, def_rules, rules, s);
Christopher Faulet4c5a5912021-11-09 17:48:39 +01003081 if ((ret == HTTP_RULE_RES_CONT || ret == HTTP_RULE_RES_STOP) && sess->fe != s->be) {
Christopher Fauletd4150ad2021-10-13 15:35:55 +02003082 def_rules = ((sess->fe->defpx && sess->fe->defpx != s->be->defpx) ? &sess->fe->defpx->http_after_res_rules : NULL);
3083 rules = &sess->fe->http_after_res_rules;
3084 ret = http_res_get_intercept_rule(sess->fe, def_rules, rules, s);
3085 }
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01003086
Christopher Faulet507479b2020-05-15 12:29:46 +02003087 end:
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01003088 /* All other codes than CONTINUE, STOP or DONE are forbidden */
3089 return (ret == HTTP_RULE_RES_CONT || ret == HTTP_RULE_RES_STOP || ret == HTTP_RULE_RES_DONE);
3090}
3091
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003092/*
3093 * Manage client-side cookie. It can impact performance by about 2% so it is
3094 * desirable to call it only when needed. This code is quite complex because
3095 * of the multiple very crappy and ambiguous syntaxes we have to support. it
3096 * highly recommended not to touch this part without a good reason !
3097 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003098static void http_manage_client_side_cookies(struct stream *s, struct channel *req)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003099{
3100 struct session *sess = s->sess;
3101 struct http_txn *txn = s->txn;
3102 struct htx *htx;
3103 struct http_hdr_ctx ctx;
3104 char *hdr_beg, *hdr_end, *del_from;
3105 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
3106 int preserve_hdr;
3107
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003108 htx = htxbuf(&req->buf);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003109 ctx.blk = NULL;
3110 while (http_find_header(htx, ist("Cookie"), &ctx, 1)) {
Olivier Houchardf0f42382019-07-22 17:43:46 +02003111 int is_first = 1;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003112 del_from = NULL; /* nothing to be deleted */
3113 preserve_hdr = 0; /* assume we may kill the whole header */
3114
3115 /* Now look for cookies. Conforming to RFC2109, we have to support
3116 * attributes whose name begin with a '$', and associate them with
3117 * the right cookie, if we want to delete this cookie.
3118 * So there are 3 cases for each cookie read :
3119 * 1) it's a special attribute, beginning with a '$' : ignore it.
3120 * 2) it's a server id cookie that we *MAY* want to delete : save
3121 * some pointers on it (last semi-colon, beginning of cookie...)
3122 * 3) it's an application cookie : we *MAY* have to delete a previous
3123 * "special" cookie.
3124 * At the end of loop, if a "special" cookie remains, we may have to
3125 * remove it. If no application cookie persists in the header, we
3126 * *MUST* delete it.
3127 *
3128 * Note: RFC2965 is unclear about the processing of spaces around
3129 * the equal sign in the ATTR=VALUE form. A careful inspection of
3130 * the RFC explicitly allows spaces before it, and not within the
3131 * tokens (attrs or values). An inspection of RFC2109 allows that
3132 * too but section 10.1.3 lets one think that spaces may be allowed
3133 * after the equal sign too, resulting in some (rare) buggy
3134 * implementations trying to do that. So let's do what servers do.
3135 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
3136 * allowed quoted strings in values, with any possible character
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003137 * after a backslash, including control chars and delimiters, which
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003138 * causes parsing to become ambiguous. Browsers also allow spaces
3139 * within values even without quotes.
3140 *
3141 * We have to keep multiple pointers in order to support cookie
3142 * removal at the beginning, middle or end of header without
3143 * corrupting the header. All of these headers are valid :
3144 *
3145 * hdr_beg hdr_end
3146 * | |
3147 * v |
3148 * NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3 |
3149 * NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3 v
3150 * NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3
3151 * | | | | | | |
3152 * | | | | | | |
3153 * | | | | | | +--> next
3154 * | | | | | +----> val_end
3155 * | | | | +-----------> val_beg
3156 * | | | +--------------> equal
3157 * | | +----------------> att_end
3158 * | +---------------------> att_beg
3159 * +--------------------------> prev
3160 *
3161 */
3162 hdr_beg = ctx.value.ptr;
3163 hdr_end = hdr_beg + ctx.value.len;
3164 for (prev = hdr_beg; prev < hdr_end; prev = next) {
3165 /* Iterate through all cookies on this line */
3166
3167 /* find att_beg */
3168 att_beg = prev;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003169 if (!is_first)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003170 att_beg++;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003171 is_first = 0;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003172
3173 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
3174 att_beg++;
3175
3176 /* find att_end : this is the first character after the last non
3177 * space before the equal. It may be equal to hdr_end.
3178 */
3179 equal = att_end = att_beg;
3180 while (equal < hdr_end) {
3181 if (*equal == '=' || *equal == ',' || *equal == ';')
3182 break;
3183 if (HTTP_IS_SPHT(*equal++))
3184 continue;
3185 att_end = equal;
3186 }
3187
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003188 /* here, <equal> points to '=', a delimiter or the end. <att_end>
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003189 * is between <att_beg> and <equal>, both may be identical.
3190 */
3191 /* look for end of cookie if there is an equal sign */
3192 if (equal < hdr_end && *equal == '=') {
3193 /* look for the beginning of the value */
3194 val_beg = equal + 1;
3195 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
3196 val_beg++;
3197
3198 /* find the end of the value, respecting quotes */
3199 next = http_find_cookie_value_end(val_beg, hdr_end);
3200
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003201 /* make val_end point to the first white space or delimiter after the value */
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003202 val_end = next;
3203 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
3204 val_end--;
3205 }
3206 else
3207 val_beg = val_end = next = equal;
3208
3209 /* We have nothing to do with attributes beginning with
3210 * '$'. However, they will automatically be removed if a
3211 * header before them is removed, since they're supposed
3212 * to be linked together.
3213 */
3214 if (*att_beg == '$')
3215 continue;
3216
3217 /* Ignore cookies with no equal sign */
3218 if (equal == next) {
3219 /* This is not our cookie, so we must preserve it. But if we already
3220 * scheduled another cookie for removal, we cannot remove the
3221 * complete header, but we can remove the previous block itself.
3222 */
3223 preserve_hdr = 1;
3224 if (del_from != NULL) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003225 int delta = http_del_hdr_value(hdr_beg, hdr_end, &del_from, prev);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003226 val_end += delta;
3227 next += delta;
3228 hdr_end += delta;
3229 prev = del_from;
3230 del_from = NULL;
3231 }
3232 continue;
3233 }
3234
3235 /* if there are spaces around the equal sign, we need to
3236 * strip them otherwise we'll get trouble for cookie captures,
3237 * or even for rewrites. Since this happens extremely rarely,
3238 * it does not hurt performance.
3239 */
3240 if (unlikely(att_end != equal || val_beg > equal + 1)) {
3241 int stripped_before = 0;
3242 int stripped_after = 0;
3243
3244 if (att_end != equal) {
3245 memmove(att_end, equal, hdr_end - equal);
3246 stripped_before = (att_end - equal);
3247 equal += stripped_before;
3248 val_beg += stripped_before;
3249 }
3250
3251 if (val_beg > equal + 1) {
3252 memmove(equal + 1, val_beg, hdr_end + stripped_before - val_beg);
3253 stripped_after = (equal + 1) - val_beg;
3254 val_beg += stripped_after;
3255 stripped_before += stripped_after;
3256 }
3257
3258 val_end += stripped_before;
3259 next += stripped_before;
3260 hdr_end += stripped_before;
3261 }
3262 /* now everything is as on the diagram above */
3263
3264 /* First, let's see if we want to capture this cookie. We check
3265 * that we don't already have a client side cookie, because we
3266 * can only capture one. Also as an optimisation, we ignore
3267 * cookies shorter than the declared name.
3268 */
3269 if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL &&
3270 (val_end - att_beg >= sess->fe->capture_namelen) &&
3271 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
3272 int log_len = val_end - att_beg;
3273
3274 if ((txn->cli_cookie = pool_alloc(pool_head_capture)) == NULL) {
3275 ha_alert("HTTP logging : out of memory.\n");
3276 } else {
3277 if (log_len > sess->fe->capture_len)
3278 log_len = sess->fe->capture_len;
3279 memcpy(txn->cli_cookie, att_beg, log_len);
3280 txn->cli_cookie[log_len] = 0;
3281 }
3282 }
3283
3284 /* Persistence cookies in passive, rewrite or insert mode have the
3285 * following form :
3286 *
3287 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
3288 *
3289 * For cookies in prefix mode, the form is :
3290 *
3291 * Cookie: NAME=SRV~VALUE
3292 */
3293 if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
3294 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
3295 struct server *srv = s->be->srv;
3296 char *delim;
3297
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003298 /* if we're in cookie prefix mode, we'll search the delimiter so that we
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003299 * have the server ID between val_beg and delim, and the original cookie between
3300 * delim+1 and val_end. Otherwise, delim==val_end :
3301 *
3302 * hdr_beg
3303 * |
3304 * v
3305 * NAME=SRV; # in all but prefix modes
3306 * NAME=SRV~OPAQUE ; # in prefix mode
3307 * || || | |+-> next
3308 * || || | +--> val_end
3309 * || || +---------> delim
3310 * || |+------------> val_beg
3311 * || +-------------> att_end = equal
3312 * |+-----------------> att_beg
3313 * +------------------> prev
3314 *
3315 */
3316 if (s->be->ck_opts & PR_CK_PFX) {
3317 for (delim = val_beg; delim < val_end; delim++)
3318 if (*delim == COOKIE_DELIM)
3319 break;
3320 }
3321 else {
3322 char *vbar1;
3323 delim = val_end;
3324 /* Now check if the cookie contains a date field, which would
3325 * appear after a vertical bar ('|') just after the server name
3326 * and before the delimiter.
3327 */
3328 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
3329 if (vbar1) {
3330 /* OK, so left of the bar is the server's cookie and
3331 * right is the last seen date. It is a base64 encoded
3332 * 30-bit value representing the UNIX date since the
3333 * epoch in 4-second quantities.
3334 */
3335 int val;
3336 delim = vbar1++;
3337 if (val_end - vbar1 >= 5) {
3338 val = b64tos30(vbar1);
3339 if (val > 0)
3340 txn->cookie_last_date = val << 2;
3341 }
3342 /* look for a second vertical bar */
3343 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
3344 if (vbar1 && (val_end - vbar1 > 5)) {
3345 val = b64tos30(vbar1 + 1);
3346 if (val > 0)
3347 txn->cookie_first_date = val << 2;
3348 }
3349 }
3350 }
3351
3352 /* if the cookie has an expiration date and the proxy wants to check
3353 * it, then we do that now. We first check if the cookie is too old,
3354 * then only if it has expired. We detect strict overflow because the
3355 * time resolution here is not great (4 seconds). Cookies with dates
3356 * in the future are ignored if their offset is beyond one day. This
3357 * allows an admin to fix timezone issues without expiring everyone
3358 * and at the same time avoids keeping unwanted side effects for too
3359 * long.
3360 */
3361 if (txn->cookie_first_date && s->be->cookie_maxlife &&
3362 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) ||
3363 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
3364 txn->flags &= ~TX_CK_MASK;
3365 txn->flags |= TX_CK_OLD;
3366 delim = val_beg; // let's pretend we have not found the cookie
3367 txn->cookie_first_date = 0;
3368 txn->cookie_last_date = 0;
3369 }
3370 else if (txn->cookie_last_date && s->be->cookie_maxidle &&
3371 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) ||
3372 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
3373 txn->flags &= ~TX_CK_MASK;
3374 txn->flags |= TX_CK_EXPIRED;
3375 delim = val_beg; // let's pretend we have not found the cookie
3376 txn->cookie_first_date = 0;
3377 txn->cookie_last_date = 0;
3378 }
3379
3380 /* Here, we'll look for the first running server which supports the cookie.
3381 * This allows to share a same cookie between several servers, for example
3382 * to dedicate backup servers to specific servers only.
3383 * However, to prevent clients from sticking to cookie-less backup server
3384 * when they have incidentely learned an empty cookie, we simply ignore
3385 * empty cookies and mark them as invalid.
3386 * The same behaviour is applied when persistence must be ignored.
3387 */
3388 if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
3389 srv = NULL;
3390
3391 while (srv) {
3392 if (srv->cookie && (srv->cklen == delim - val_beg) &&
3393 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
3394 if ((srv->cur_state != SRV_ST_STOPPED) ||
3395 (s->be->options & PR_O_PERSIST) ||
3396 (s->flags & SF_FORCE_PRST)) {
3397 /* we found the server and we can use it */
3398 txn->flags &= ~TX_CK_MASK;
3399 txn->flags |= (srv->cur_state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
3400 s->flags |= SF_DIRECT | SF_ASSIGNED;
3401 s->target = &srv->obj_type;
3402 break;
3403 } else {
3404 /* we found a server, but it's down,
3405 * mark it as such and go on in case
3406 * another one is available.
3407 */
3408 txn->flags &= ~TX_CK_MASK;
3409 txn->flags |= TX_CK_DOWN;
3410 }
3411 }
3412 srv = srv->next;
3413 }
3414
3415 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
3416 /* no server matched this cookie or we deliberately skipped it */
3417 txn->flags &= ~TX_CK_MASK;
3418 if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
3419 txn->flags |= TX_CK_UNUSED;
3420 else
3421 txn->flags |= TX_CK_INVALID;
3422 }
3423
3424 /* depending on the cookie mode, we may have to either :
3425 * - delete the complete cookie if we're in insert+indirect mode, so that
3426 * the server never sees it ;
3427 * - remove the server id from the cookie value, and tag the cookie as an
Joseph Herlante9d5c722018-11-25 11:00:25 -08003428 * application cookie so that it does not get accidentally removed later,
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003429 * if we're in cookie prefix mode
3430 */
3431 if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
3432 int delta; /* negative */
3433
3434 memmove(val_beg, delim + 1, hdr_end - (delim + 1));
3435 delta = val_beg - (delim + 1);
3436 val_end += delta;
3437 next += delta;
3438 hdr_end += delta;
3439 del_from = NULL;
3440 preserve_hdr = 1; /* we want to keep this cookie */
3441 }
3442 else if (del_from == NULL &&
3443 (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
3444 del_from = prev;
3445 }
3446 }
3447 else {
3448 /* This is not our cookie, so we must preserve it. But if we already
3449 * scheduled another cookie for removal, we cannot remove the
3450 * complete header, but we can remove the previous block itself.
3451 */
3452 preserve_hdr = 1;
3453
3454 if (del_from != NULL) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003455 int delta = http_del_hdr_value(hdr_beg, hdr_end, &del_from, prev);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003456 if (att_beg >= del_from)
3457 att_beg += delta;
3458 if (att_end >= del_from)
3459 att_end += delta;
3460 val_beg += delta;
3461 val_end += delta;
3462 next += delta;
3463 hdr_end += delta;
3464 prev = del_from;
3465 del_from = NULL;
3466 }
3467 }
3468
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003469 } /* for each cookie */
3470
3471
3472 /* There are no more cookies on this line.
3473 * We may still have one (or several) marked for deletion at the
3474 * end of the line. We must do this now in two ways :
3475 * - if some cookies must be preserved, we only delete from the
3476 * mark to the end of line ;
3477 * - if nothing needs to be preserved, simply delete the whole header
3478 */
3479 if (del_from) {
3480 hdr_end = (preserve_hdr ? del_from : hdr_beg);
3481 }
3482 if ((hdr_end - hdr_beg) != ctx.value.len) {
Christopher Faulet3e2638e2019-06-18 09:49:16 +02003483 if (hdr_beg != hdr_end)
3484 htx_change_blk_value_len(htx, ctx.blk, hdr_end - hdr_beg);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003485 else
3486 http_remove_header(htx, &ctx);
3487 }
3488 } /* for each "Cookie header */
3489}
3490
3491/*
3492 * Manage server-side cookies. It can impact performance by about 2% so it is
3493 * desirable to call it only when needed. This function is also used when we
3494 * just need to know if there is a cookie (eg: for check-cache).
3495 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003496static void http_manage_server_side_cookies(struct stream *s, struct channel *res)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003497{
3498 struct session *sess = s->sess;
3499 struct http_txn *txn = s->txn;
3500 struct htx *htx;
3501 struct http_hdr_ctx ctx;
3502 struct server *srv;
3503 char *hdr_beg, *hdr_end;
3504 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau6f7a02a2019-04-15 21:49:49 +02003505 int is_cookie2 = 0;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003506
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003507 htx = htxbuf(&res->buf);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003508
3509 ctx.blk = NULL;
3510 while (1) {
Olivier Houchardf0f42382019-07-22 17:43:46 +02003511 int is_first = 1;
3512
Andrew McDermottbfb15ab2022-02-11 18:26:49 +00003513 if (is_cookie2 || !http_find_header(htx, ist("Set-Cookie"), &ctx, 1)) {
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003514 if (!http_find_header(htx, ist("Set-Cookie2"), &ctx, 1))
3515 break;
3516 is_cookie2 = 1;
3517 }
3518
3519 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
3520 * <prev> points to the colon.
3521 */
3522 txn->flags |= TX_SCK_PRESENT;
3523
3524 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
3525 * check-cache is enabled) and we are not interested in checking
3526 * them. Warning, the cookie capture is declared in the frontend.
3527 */
3528 if (s->be->cookie_name == NULL && sess->fe->capture_name == NULL)
3529 break;
3530
3531 /* OK so now we know we have to process this response cookie.
3532 * The format of the Set-Cookie header is slightly different
3533 * from the format of the Cookie header in that it does not
3534 * support the comma as a cookie delimiter (thus the header
3535 * cannot be folded) because the Expires attribute described in
3536 * the original Netscape's spec may contain an unquoted date
3537 * with a comma inside. We have to live with this because
3538 * many browsers don't support Max-Age and some browsers don't
3539 * support quoted strings. However the Set-Cookie2 header is
3540 * clean.
3541 *
3542 * We have to keep multiple pointers in order to support cookie
3543 * removal at the beginning, middle or end of header without
3544 * corrupting the header (in case of set-cookie2). A special
3545 * pointer, <scav> points to the beginning of the set-cookie-av
3546 * fields after the first semi-colon. The <next> pointer points
3547 * either to the end of line (set-cookie) or next unquoted comma
3548 * (set-cookie2). All of these headers are valid :
3549 *
3550 * hdr_beg hdr_end
3551 * | |
3552 * v |
3553 * NAME1 = VALUE 1 ; Secure; Path="/" |
3554 * NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT v
3555 * NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT
3556 * NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard
3557 * | | | | | | | |
3558 * | | | | | | | +-> next
3559 * | | | | | | +------------> scav
3560 * | | | | | +--------------> val_end
3561 * | | | | +--------------------> val_beg
3562 * | | | +----------------------> equal
3563 * | | +------------------------> att_end
3564 * | +----------------------------> att_beg
3565 * +------------------------------> prev
3566 * -------------------------------> hdr_beg
3567 */
3568 hdr_beg = ctx.value.ptr;
3569 hdr_end = hdr_beg + ctx.value.len;
3570 for (prev = hdr_beg; prev < hdr_end; prev = next) {
3571
3572 /* Iterate through all cookies on this line */
3573
3574 /* find att_beg */
3575 att_beg = prev;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003576 if (!is_first)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003577 att_beg++;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003578 is_first = 0;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003579
3580 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
3581 att_beg++;
3582
3583 /* find att_end : this is the first character after the last non
3584 * space before the equal. It may be equal to hdr_end.
3585 */
3586 equal = att_end = att_beg;
3587
3588 while (equal < hdr_end) {
3589 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
3590 break;
3591 if (HTTP_IS_SPHT(*equal++))
3592 continue;
3593 att_end = equal;
3594 }
3595
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003596 /* here, <equal> points to '=', a delimiter or the end. <att_end>
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003597 * is between <att_beg> and <equal>, both may be identical.
3598 */
3599
3600 /* look for end of cookie if there is an equal sign */
3601 if (equal < hdr_end && *equal == '=') {
3602 /* look for the beginning of the value */
3603 val_beg = equal + 1;
3604 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
3605 val_beg++;
3606
3607 /* find the end of the value, respecting quotes */
3608 next = http_find_cookie_value_end(val_beg, hdr_end);
3609
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003610 /* make val_end point to the first white space or delimiter after the value */
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003611 val_end = next;
3612 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
3613 val_end--;
3614 }
3615 else {
3616 /* <equal> points to next comma, semi-colon or EOL */
3617 val_beg = val_end = next = equal;
3618 }
3619
3620 if (next < hdr_end) {
3621 /* Set-Cookie2 supports multiple cookies, and <next> points to
3622 * a colon or semi-colon before the end. So skip all attr-value
3623 * pairs and look for the next comma. For Set-Cookie, since
3624 * commas are permitted in values, skip to the end.
3625 */
3626 if (is_cookie2)
3627 next = http_find_hdr_value_end(next, hdr_end);
3628 else
3629 next = hdr_end;
3630 }
3631
3632 /* Now everything is as on the diagram above */
3633
3634 /* Ignore cookies with no equal sign */
3635 if (equal == val_end)
3636 continue;
3637
3638 /* If there are spaces around the equal sign, we need to
3639 * strip them otherwise we'll get trouble for cookie captures,
3640 * or even for rewrites. Since this happens extremely rarely,
3641 * it does not hurt performance.
3642 */
3643 if (unlikely(att_end != equal || val_beg > equal + 1)) {
3644 int stripped_before = 0;
3645 int stripped_after = 0;
3646
3647 if (att_end != equal) {
3648 memmove(att_end, equal, hdr_end - equal);
3649 stripped_before = (att_end - equal);
3650 equal += stripped_before;
3651 val_beg += stripped_before;
3652 }
3653
3654 if (val_beg > equal + 1) {
3655 memmove(equal + 1, val_beg, hdr_end + stripped_before - val_beg);
3656 stripped_after = (equal + 1) - val_beg;
3657 val_beg += stripped_after;
3658 stripped_before += stripped_after;
3659 }
3660
3661 val_end += stripped_before;
3662 next += stripped_before;
3663 hdr_end += stripped_before;
3664
Christopher Faulet3e2638e2019-06-18 09:49:16 +02003665 htx_change_blk_value_len(htx, ctx.blk, hdr_end - hdr_beg);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003666 ctx.value.len = hdr_end - hdr_beg;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003667 }
3668
3669 /* First, let's see if we want to capture this cookie. We check
3670 * that we don't already have a server side cookie, because we
3671 * can only capture one. Also as an optimisation, we ignore
3672 * cookies shorter than the declared name.
3673 */
3674 if (sess->fe->capture_name != NULL &&
3675 txn->srv_cookie == NULL &&
3676 (val_end - att_beg >= sess->fe->capture_namelen) &&
3677 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
3678 int log_len = val_end - att_beg;
3679 if ((txn->srv_cookie = pool_alloc(pool_head_capture)) == NULL) {
3680 ha_alert("HTTP logging : out of memory.\n");
3681 }
3682 else {
3683 if (log_len > sess->fe->capture_len)
3684 log_len = sess->fe->capture_len;
3685 memcpy(txn->srv_cookie, att_beg, log_len);
3686 txn->srv_cookie[log_len] = 0;
3687 }
3688 }
3689
3690 srv = objt_server(s->target);
3691 /* now check if we need to process it for persistence */
3692 if (!(s->flags & SF_IGNORE_PRST) &&
3693 (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
3694 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
3695 /* assume passive cookie by default */
3696 txn->flags &= ~TX_SCK_MASK;
3697 txn->flags |= TX_SCK_FOUND;
3698
3699 /* If the cookie is in insert mode on a known server, we'll delete
3700 * this occurrence because we'll insert another one later.
3701 * We'll delete it too if the "indirect" option is set and we're in
3702 * a direct access.
3703 */
3704 if (s->be->ck_opts & PR_CK_PSV) {
3705 /* The "preserve" flag was set, we don't want to touch the
3706 * server's cookie.
3707 */
3708 }
3709 else if ((srv && (s->be->ck_opts & PR_CK_INS)) ||
3710 ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) {
3711 /* this cookie must be deleted */
3712 if (prev == hdr_beg && next == hdr_end) {
3713 /* whole header */
3714 http_remove_header(htx, &ctx);
3715 /* note: while both invalid now, <next> and <hdr_end>
3716 * are still equal, so the for() will stop as expected.
3717 */
3718 } else {
3719 /* just remove the value */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003720 int delta = http_del_hdr_value(hdr_beg, hdr_end, &prev, next);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003721 next = prev;
3722 hdr_end += delta;
3723 }
3724 txn->flags &= ~TX_SCK_MASK;
3725 txn->flags |= TX_SCK_DELETED;
3726 /* and go on with next cookie */
3727 }
3728 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) {
3729 /* replace bytes val_beg->val_end with the cookie name associated
3730 * with this server since we know it.
3731 */
3732 int sliding, delta;
3733
3734 ctx.value = ist2(val_beg, val_end - val_beg);
3735 ctx.lws_before = ctx.lws_after = 0;
3736 http_replace_header_value(htx, &ctx, ist2(srv->cookie, srv->cklen));
3737 delta = srv->cklen - (val_end - val_beg);
3738 sliding = (ctx.value.ptr - val_beg);
3739 hdr_beg += sliding;
3740 val_beg += sliding;
3741 next += sliding + delta;
3742 hdr_end += sliding + delta;
3743
3744 txn->flags &= ~TX_SCK_MASK;
3745 txn->flags |= TX_SCK_REPLACED;
3746 }
3747 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) {
3748 /* insert the cookie name associated with this server
3749 * before existing cookie, and insert a delimiter between them..
3750 */
3751 int sliding, delta;
3752 ctx.value = ist2(val_beg, 0);
3753 ctx.lws_before = ctx.lws_after = 0;
3754 http_replace_header_value(htx, &ctx, ist2(srv->cookie, srv->cklen + 1));
3755 delta = srv->cklen + 1;
3756 sliding = (ctx.value.ptr - val_beg);
3757 hdr_beg += sliding;
3758 val_beg += sliding;
3759 next += sliding + delta;
3760 hdr_end += sliding + delta;
3761
3762 val_beg[srv->cklen] = COOKIE_DELIM;
3763 txn->flags &= ~TX_SCK_MASK;
3764 txn->flags |= TX_SCK_REPLACED;
3765 }
3766 }
3767 /* that's done for this cookie, check the next one on the same
3768 * line when next != hdr_end (only if is_cookie2).
3769 */
3770 }
3771 }
3772}
3773
Christopher Faulet25a02f62018-10-24 12:00:25 +02003774/*
3775 * Parses the Cache-Control and Pragma request header fields to determine if
3776 * the request may be served from the cache and/or if it is cacheable. Updates
3777 * s->txn->flags.
3778 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003779void http_check_request_for_cacheability(struct stream *s, struct channel *req)
Christopher Faulet25a02f62018-10-24 12:00:25 +02003780{
3781 struct http_txn *txn = s->txn;
3782 struct htx *htx;
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003783 struct http_hdr_ctx ctx = { .blk = NULL };
3784 int pragma_found, cc_found;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003785
3786 if ((txn->flags & (TX_CACHEABLE|TX_CACHE_IGNORE)) == TX_CACHE_IGNORE)
3787 return; /* nothing more to do here */
3788
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003789 htx = htxbuf(&req->buf);
Christopher Faulet25a02f62018-10-24 12:00:25 +02003790 pragma_found = cc_found = 0;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003791
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003792 /* Check "pragma" header for HTTP/1.0 compatibility. */
3793 if (http_find_header(htx, ist("pragma"), &ctx, 1)) {
3794 if (isteqi(ctx.value, ist("no-cache"))) {
3795 pragma_found = 1;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003796 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003797 }
Christopher Faulet25a02f62018-10-24 12:00:25 +02003798
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003799 ctx.blk = NULL;
3800 /* Don't use the cache and don't try to store if we found the
3801 * Authorization header */
3802 if (http_find_header(htx, ist("authorization"), &ctx, 1)) {
3803 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3804 txn->flags |= TX_CACHE_IGNORE;
3805 }
Christopher Faulet25a02f62018-10-24 12:00:25 +02003806
Christopher Faulet25a02f62018-10-24 12:00:25 +02003807
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003808 /* Look for "cache-control" header and iterate over all the values
3809 * until we find one that specifies that caching is possible or not. */
3810 ctx.blk = NULL;
3811 while (http_find_header(htx, ist("cache-control"), &ctx, 0)) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003812 cc_found = 1;
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003813 /* We don't check the values after max-age, max-stale nor min-fresh,
3814 * we simply don't use the cache when they're specified. */
3815 if (istmatchi(ctx.value, ist("max-age")) ||
3816 istmatchi(ctx.value, ist("no-cache")) ||
3817 istmatchi(ctx.value, ist("max-stale")) ||
3818 istmatchi(ctx.value, ist("min-fresh"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003819 txn->flags |= TX_CACHE_IGNORE;
3820 continue;
3821 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003822 if (istmatchi(ctx.value, ist("no-store"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003823 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3824 continue;
3825 }
3826 }
3827
3828 /* RFC7234#5.4:
3829 * When the Cache-Control header field is also present and
3830 * understood in a request, Pragma is ignored.
3831 * When the Cache-Control header field is not present in a
3832 * request, caches MUST consider the no-cache request
3833 * pragma-directive as having the same effect as if
3834 * "Cache-Control: no-cache" were present.
3835 */
3836 if (!cc_found && pragma_found)
3837 txn->flags |= TX_CACHE_IGNORE;
3838}
3839
3840/*
3841 * Check if response is cacheable or not. Updates s->txn->flags.
3842 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003843void http_check_response_for_cacheability(struct stream *s, struct channel *res)
Christopher Faulet25a02f62018-10-24 12:00:25 +02003844{
3845 struct http_txn *txn = s->txn;
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003846 struct http_hdr_ctx ctx = { .blk = NULL };
Christopher Faulet25a02f62018-10-24 12:00:25 +02003847 struct htx *htx;
Remi Tricot-Le Bretoncc9bf2e2020-11-12 11:14:41 +01003848 int has_freshness_info = 0;
3849 int has_validator = 0;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003850
3851 if (txn->status < 200) {
3852 /* do not try to cache interim responses! */
3853 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3854 return;
3855 }
3856
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003857 htx = htxbuf(&res->buf);
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003858 /* Check "pragma" header for HTTP/1.0 compatibility. */
3859 if (http_find_header(htx, ist("pragma"), &ctx, 1)) {
3860 if (isteqi(ctx.value, ist("no-cache"))) {
3861 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3862 return;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003863 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003864 }
Christopher Faulet25a02f62018-10-24 12:00:25 +02003865
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003866 /* Look for "cache-control" header and iterate over all the values
3867 * until we find one that specifies that caching is possible or not. */
3868 ctx.blk = NULL;
3869 while (http_find_header(htx, ist("cache-control"), &ctx, 0)) {
3870 if (isteqi(ctx.value, ist("public"))) {
3871 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003872 continue;
3873 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003874 if (isteqi(ctx.value, ist("private")) ||
3875 isteqi(ctx.value, ist("no-cache")) ||
3876 isteqi(ctx.value, ist("no-store")) ||
3877 isteqi(ctx.value, ist("max-age=0")) ||
3878 isteqi(ctx.value, ist("s-maxage=0"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003879 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003880 continue;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003881 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003882 /* We might have a no-cache="set-cookie" form. */
3883 if (istmatchi(ctx.value, ist("no-cache=\"set-cookie"))) {
3884 txn->flags &= ~TX_CACHE_COOK;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003885 continue;
3886 }
Remi Tricot-Le Bretoncc9bf2e2020-11-12 11:14:41 +01003887
3888 if (istmatchi(ctx.value, ist("s-maxage")) ||
3889 istmatchi(ctx.value, ist("max-age"))) {
3890 has_freshness_info = 1;
3891 continue;
3892 }
3893 }
3894
3895 /* If no freshness information could be found in Cache-Control values,
3896 * look for an Expires header. */
3897 if (!has_freshness_info) {
3898 ctx.blk = NULL;
3899 has_freshness_info = http_find_header(htx, ist("expires"), &ctx, 0);
Christopher Faulet25a02f62018-10-24 12:00:25 +02003900 }
Remi Tricot-Le Bretoncc9bf2e2020-11-12 11:14:41 +01003901
3902 /* If no freshness information could be found in Cache-Control or Expires
3903 * values, look for an explicit validator. */
3904 if (!has_freshness_info) {
3905 ctx.blk = NULL;
3906 has_validator = 1;
3907 if (!http_find_header(htx, ist("etag"), &ctx, 0)) {
3908 ctx.blk = NULL;
3909 if (!http_find_header(htx, ist("last-modified"), &ctx, 0))
3910 has_validator = 0;
3911 }
3912 }
3913
3914 /* We won't store an entry that has neither a cache validator nor an
3915 * explicit expiration time, as suggested in RFC 7234#3. */
3916 if (!has_freshness_info && !has_validator)
3917 txn->flags |= TX_CACHE_IGNORE;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003918}
3919
Christopher Faulet377c5a52018-10-24 21:21:30 +02003920/*
3921 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
3922 * for the current backend.
3923 *
3924 * It is assumed that the request is either a HEAD, GET, or POST and that the
3925 * uri_auth field is valid.
3926 *
3927 * Returns 1 if stats should be provided, otherwise 0.
3928 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003929static int http_stats_check_uri(struct stream *s, struct http_txn *txn, struct proxy *backend)
Christopher Faulet377c5a52018-10-24 21:21:30 +02003930{
3931 struct uri_auth *uri_auth = backend->uri_auth;
3932 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003933 struct htx_sl *sl;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003934 struct ist uri;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003935
3936 if (!uri_auth)
3937 return 0;
3938
3939 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
3940 return 0;
3941
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003942 htx = htxbuf(&s->req.buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02003943 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003944 uri = htx_sl_req_uri(sl);
Amaury Denoyellec453f952021-07-06 11:40:12 +02003945 if (*uri_auth->uri_prefix == '/') {
3946 struct http_uri_parser parser = http_uri_parser_init(uri);
3947 uri = http_parse_path(&parser);
3948 }
Christopher Faulet377c5a52018-10-24 21:21:30 +02003949
3950 /* check URI size */
3951 if (uri_auth->uri_len > uri.len)
3952 return 0;
3953
3954 if (memcmp(uri.ptr, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
3955 return 0;
3956
3957 return 1;
3958}
3959
3960/* This function prepares an applet to handle the stats. It can deal with the
3961 * "100-continue" expectation, check that admin rules are met for POST requests,
3962 * and program a response message if something was unexpected. It cannot fail
3963 * and always relies on the stats applet to complete the job. It does not touch
3964 * analysers nor counters, which are left to the caller. It does not touch
3965 * s->target which is supposed to already point to the stats applet. The caller
3966 * is expected to have already assigned an appctx to the stream.
3967 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003968static int http_handle_stats(struct stream *s, struct channel *req)
Christopher Faulet377c5a52018-10-24 21:21:30 +02003969{
3970 struct stats_admin_rule *stats_admin_rule;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003971 struct session *sess = s->sess;
3972 struct http_txn *txn = s->txn;
3973 struct http_msg *msg = &txn->req;
3974 struct uri_auth *uri_auth = s->be->uri_auth;
3975 const char *h, *lookup, *end;
Christopher Faulet693b23b2022-02-28 09:09:05 +01003976 struct appctx *appctx = __cs_appctx(s->csb);
Willy Tarreau91cefca2022-05-03 17:08:29 +02003977 struct show_stat_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx));
Christopher Faulet377c5a52018-10-24 21:21:30 +02003978 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003979 struct htx_sl *sl;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003980
Willy Tarreau41f88522022-05-03 18:39:27 +02003981 appctx->st1 = 0;
Willy Tarreau6ef16482022-05-06 18:07:53 +02003982 ctx->state = STAT_STATE_INIT;
Willy Tarreau91cefca2022-05-03 17:08:29 +02003983 ctx->st_code = STAT_STATUS_INIT;
3984 ctx->flags |= uri_auth->flags;
3985 ctx->flags |= STAT_FMT_HTML; /* assume HTML mode by default */
Christopher Faulet377c5a52018-10-24 21:21:30 +02003986 if ((msg->flags & HTTP_MSGF_VER_11) && (txn->meth != HTTP_METH_HEAD))
Willy Tarreau91cefca2022-05-03 17:08:29 +02003987 ctx->flags |= STAT_CHUNKED;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003988
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003989 htx = htxbuf(&req->buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02003990 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003991 lookup = HTX_SL_REQ_UPTR(sl) + uri_auth->uri_len;
3992 end = HTX_SL_REQ_UPTR(sl) + HTX_SL_REQ_ULEN(sl);
Christopher Faulet377c5a52018-10-24 21:21:30 +02003993
3994 for (h = lookup; h <= end - 3; h++) {
3995 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02003996 ctx->flags |= STAT_HIDE_DOWN;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003997 break;
3998 }
Amaury Denoyelle91e55ea2021-02-25 14:46:08 +01003999 }
4000
4001 for (h = lookup; h <= end - 9; h++) {
4002 if (memcmp(h, ";no-maint", 9) == 0) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02004003 ctx->flags |= STAT_HIDE_MAINT;
Willy Tarreau3e320362020-10-23 17:28:57 +02004004 break;
4005 }
Christopher Faulet377c5a52018-10-24 21:21:30 +02004006 }
4007
4008 if (uri_auth->refresh) {
4009 for (h = lookup; h <= end - 10; h++) {
4010 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02004011 ctx->flags |= STAT_NO_REFRESH;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004012 break;
4013 }
4014 }
4015 }
4016
4017 for (h = lookup; h <= end - 4; h++) {
4018 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02004019 ctx->flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
Christopher Faulet377c5a52018-10-24 21:21:30 +02004020 break;
4021 }
4022 }
4023
4024 for (h = lookup; h <= end - 6; h++) {
4025 if (memcmp(h, ";typed", 6) == 0) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02004026 ctx->flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
4027 ctx->flags |= STAT_FMT_TYPED;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004028 break;
4029 }
4030 }
4031
Christopher Faulet6338a082019-09-09 15:50:54 +02004032 for (h = lookup; h <= end - 5; h++) {
4033 if (memcmp(h, ";json", 5) == 0) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02004034 ctx->flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
4035 ctx->flags |= STAT_FMT_JSON;
Christopher Faulet6338a082019-09-09 15:50:54 +02004036 break;
4037 }
4038 }
4039
4040 for (h = lookup; h <= end - 12; h++) {
4041 if (memcmp(h, ";json-schema", 12) == 0) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02004042 ctx->flags &= ~STAT_FMT_MASK;
4043 ctx->flags |= STAT_JSON_SCHM;
Christopher Faulet6338a082019-09-09 15:50:54 +02004044 break;
4045 }
4046 }
4047
Christopher Faulet377c5a52018-10-24 21:21:30 +02004048 for (h = lookup; h <= end - 8; h++) {
4049 if (memcmp(h, ";st=", 4) == 0) {
4050 int i;
4051 h += 4;
Willy Tarreau91cefca2022-05-03 17:08:29 +02004052 ctx->st_code = STAT_STATUS_UNKN;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004053 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
4054 if (strncmp(stat_status_codes[i], h, 4) == 0) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02004055 ctx->st_code = i;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004056 break;
4057 }
4058 }
4059 break;
4060 }
4061 }
4062
Willy Tarreau91cefca2022-05-03 17:08:29 +02004063 ctx->scope_str = 0;
4064 ctx->scope_len = 0;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004065 for (h = lookup; h <= end - 8; h++) {
4066 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
4067 int itx = 0;
4068 const char *h2;
4069 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
4070 const char *err;
4071
4072 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
4073 h2 = h;
Willy Tarreau91cefca2022-05-03 17:08:29 +02004074 ctx->scope_str = h2 - HTX_SL_REQ_UPTR(sl);
Christopher Fauleted7a0662019-01-14 11:07:34 +01004075 while (h < end) {
Christopher Faulet377c5a52018-10-24 21:21:30 +02004076 if (*h == ';' || *h == '&' || *h == ' ')
4077 break;
4078 itx++;
4079 h++;
4080 }
4081
4082 if (itx > STAT_SCOPE_TXT_MAXLEN)
4083 itx = STAT_SCOPE_TXT_MAXLEN;
Willy Tarreau91cefca2022-05-03 17:08:29 +02004084 ctx->scope_len = itx;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004085
Willy Tarreau91cefca2022-05-03 17:08:29 +02004086 /* scope_txt = search query, ctx->scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Christopher Faulet377c5a52018-10-24 21:21:30 +02004087 memcpy(scope_txt, h2, itx);
4088 scope_txt[itx] = '\0';
4089 err = invalid_char(scope_txt);
4090 if (err) {
4091 /* bad char in search text => clear scope */
Willy Tarreau91cefca2022-05-03 17:08:29 +02004092 ctx->scope_str = 0;
4093 ctx->scope_len = 0;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004094 }
4095 break;
4096 }
4097 }
4098
4099 /* now check whether we have some admin rules for this request */
4100 list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) {
4101 int ret = 1;
4102
4103 if (stats_admin_rule->cond) {
4104 ret = acl_exec_cond(stats_admin_rule->cond, s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
4105 ret = acl_pass(ret);
4106 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
4107 ret = !ret;
4108 }
4109
4110 if (ret) {
4111 /* no rule, or the rule matches */
Willy Tarreau91cefca2022-05-03 17:08:29 +02004112 ctx->flags |= STAT_ADMIN;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004113 break;
4114 }
4115 }
4116
Christopher Faulet5d45e382019-02-27 15:15:23 +01004117 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
4118 appctx->st0 = STAT_HTTP_HEAD;
4119 else if (txn->meth == HTTP_METH_POST) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02004120 if (ctx->flags & STAT_ADMIN) {
Christopher Faulet377c5a52018-10-24 21:21:30 +02004121 appctx->st0 = STAT_HTTP_POST;
Christopher Fauletbd9e8422019-08-15 22:26:48 +02004122 if (msg->msg_state < HTTP_MSG_DATA)
4123 req->analysers |= AN_REQ_HTTP_BODY;
4124 }
Christopher Faulet377c5a52018-10-24 21:21:30 +02004125 else {
Christopher Faulet5d45e382019-02-27 15:15:23 +01004126 /* POST without admin level */
Willy Tarreau91cefca2022-05-03 17:08:29 +02004127 ctx->flags &= ~STAT_CHUNKED;
4128 ctx->st_code = STAT_STATUS_DENY;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004129 appctx->st0 = STAT_HTTP_LAST;
4130 }
4131 }
4132 else {
Christopher Faulet5d45e382019-02-27 15:15:23 +01004133 /* Unsupported method */
Willy Tarreau91cefca2022-05-03 17:08:29 +02004134 ctx->flags &= ~STAT_CHUNKED;
4135 ctx->st_code = STAT_STATUS_IVAL;
Christopher Faulet5d45e382019-02-27 15:15:23 +01004136 appctx->st0 = STAT_HTTP_LAST;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004137 }
4138
4139 s->task->nice = -32; /* small boost for HTTP statistics */
4140 return 1;
4141}
4142
Christopher Faulet021a8e42021-03-29 10:46:38 +02004143/* This function waits for the message payload at most <time> milliseconds (may
4144 * be set to TICK_ETERNITY). It stops to wait if at least <bytes> bytes of the
4145 * payload are received (0 means no limit). It returns HTTP_RULE_* depending on
4146 * the result:
4147 *
4148 * - HTTP_RULE_RES_CONT when conditions are met to stop waiting
4149 * - HTTP_RULE_RES_YIELD to wait for more data
Ilya Shipitsinb2be9a12021-04-24 13:25:42 +05004150 * - HTTP_RULE_RES_ABRT when a timeout occurred.
Christopher Faulet021a8e42021-03-29 10:46:38 +02004151 * - HTTP_RULE_RES_BADREQ if a parsing error is raised by lower level
Ilya Shipitsinb2be9a12021-04-24 13:25:42 +05004152 * - HTTP_RULE_RES_ERROR if an internal error occurred
Christopher Faulet021a8e42021-03-29 10:46:38 +02004153 *
Ilya Shipitsinb2be9a12021-04-24 13:25:42 +05004154 * If a timeout occurred, this function is responsible to emit the right response
Christopher Faulet021a8e42021-03-29 10:46:38 +02004155 * to the client, depending on the channel (408 on request side, 504 on response
4156 * side). All other errors must be handled by the caller.
4157 */
4158enum rule_result http_wait_for_msg_body(struct stream *s, struct channel *chn,
4159 unsigned int time, unsigned int bytes)
4160{
4161 struct session *sess = s->sess;
4162 struct http_txn *txn = s->txn;
4163 struct http_msg *msg = ((chn->flags & CF_ISRESP) ? &txn->rsp : &txn->req);
4164 struct htx *htx;
4165 enum rule_result ret = HTTP_RULE_RES_CONT;
4166
4167 htx = htxbuf(&chn->buf);
4168
4169 if (htx->flags & HTX_FL_PARSING_ERROR) {
4170 ret = HTTP_RULE_RES_BADREQ;
4171 goto end;
4172 }
4173 if (htx->flags & HTX_FL_PROCESSING_ERROR) {
4174 ret = HTTP_RULE_RES_ERROR;
4175 goto end;
4176 }
4177
4178 /* Do nothing for bodyless and CONNECT requests */
4179 if (txn->meth == HTTP_METH_CONNECT || (msg->flags & HTTP_MSGF_BODYLESS))
4180 goto end;
4181
4182 if (!(chn->flags & CF_ISRESP) && msg->msg_state < HTTP_MSG_DATA) {
4183 if (http_handle_expect_hdr(s, htx, msg) == -1) {
4184 ret = HTTP_RULE_RES_ERROR;
4185 goto end;
4186 }
4187 }
4188
4189 msg->msg_state = HTTP_MSG_DATA;
4190
4191 /* Now we're in HTTP_MSG_DATA. We just need to know if all data have
4192 * been received or if the buffer is full.
4193 */
Christopher Faulet78335962021-09-23 14:46:32 +02004194 if ((htx->flags & HTX_FL_EOM) ||
4195 htx_get_tail_type(htx) > HTX_BLK_DATA ||
4196 channel_htx_full(chn, htx, global.tune.maxrewrite) ||
Christopher Fauleta0bdec32022-04-04 07:51:21 +02004197 cs_rx_blocked_room(chn_prod(chn)))
Christopher Faulet021a8e42021-03-29 10:46:38 +02004198 goto end;
4199
4200 if (bytes) {
4201 struct htx_blk *blk;
4202 unsigned int len = 0;
4203
4204 for (blk = htx_get_first_blk(htx); blk; blk = htx_get_next_blk(htx, blk)) {
4205 if (htx_get_blk_type(blk) != HTX_BLK_DATA)
4206 continue;
4207 len += htx_get_blksz(blk);
4208 if (len >= bytes)
4209 goto end;
4210 }
4211 }
4212
4213 if ((chn->flags & CF_READ_TIMEOUT) || tick_is_expired(chn->analyse_exp, now_ms)) {
4214 if (!(chn->flags & CF_ISRESP))
4215 goto abort_req;
4216 goto abort_res;
4217 }
4218
4219 /* we get here if we need to wait for more data */
4220 if (!(chn->flags & (CF_SHUTR | CF_READ_ERROR))) {
4221 if (!tick_isset(chn->analyse_exp))
4222 chn->analyse_exp = tick_add_ifset(now_ms, time);
4223 ret = HTTP_RULE_RES_YIELD;
4224 }
4225
4226 end:
4227 return ret;
4228
4229 abort_req:
4230 txn->status = 408;
4231 if (!(s->flags & SF_ERR_MASK))
4232 s->flags |= SF_ERR_CLITO;
4233 if (!(s->flags & SF_FINST_MASK))
4234 s->flags |= SF_FINST_D;
Willy Tarreau4781b152021-04-06 13:53:36 +02004235 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req);
Christopher Faulet021a8e42021-03-29 10:46:38 +02004236 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02004237 _HA_ATOMIC_INC(&sess->listener->counters->failed_req);
Christopher Faulet021a8e42021-03-29 10:46:38 +02004238 http_reply_and_close(s, txn->status, http_error_message(s));
4239 ret = HTTP_RULE_RES_ABRT;
4240 goto end;
4241
4242 abort_res:
4243 txn->status = 504;
4244 if (!(s->flags & SF_ERR_MASK))
4245 s->flags |= SF_ERR_SRVTO;
4246 if (!(s->flags & SF_FINST_MASK))
4247 s->flags |= SF_FINST_D;
4248 stream_inc_http_fail_ctr(s);
4249 http_reply_and_close(s, txn->status, http_error_message(s));
4250 ret = HTTP_RULE_RES_ABRT;
4251 goto end;
4252}
4253
Christopher Faulet0eb32c02022-04-04 11:06:31 +02004254void http_perform_server_redirect(struct stream *s, struct conn_stream *cs)
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004255{
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004256 struct channel *req = &s->req;
4257 struct channel *res = &s->res;
4258 struct server *srv;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004259 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004260 struct htx_sl *sl;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004261 struct ist path, location;
4262 unsigned int flags;
Amaury Denoyellec453f952021-07-06 11:40:12 +02004263 struct http_uri_parser parser;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004264
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004265 /*
4266 * Create the location
4267 */
4268 chunk_reset(&trash);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004269
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004270 /* 1: add the server's prefix */
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004271 /* special prefix "/" means don't change URL */
4272 srv = __objt_server(s->target);
4273 if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') {
4274 if (!chunk_memcat(&trash, srv->rdr_pfx, srv->rdr_len))
4275 return;
4276 }
4277
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004278 /* 2: add the request Path */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004279 htx = htxbuf(&req->buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02004280 sl = http_get_stline(htx);
Amaury Denoyellec453f952021-07-06 11:40:12 +02004281 parser = http_uri_parser_init(htx_sl_req_uri(sl));
4282 path = http_parse_path(&parser);
Tim Duesterhused526372020-03-05 17:56:33 +01004283 if (!isttest(path))
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004284 return;
4285
4286 if (!chunk_memcat(&trash, path.ptr, path.len))
4287 return;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004288 location = ist2(trash.area, trash.data);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004289
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004290 /*
4291 * Create the 302 respone
4292 */
4293 htx = htx_from_buf(&res->buf);
4294 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
4295 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
4296 ist("HTTP/1.1"), ist("302"), ist("Found"));
4297 if (!sl)
4298 goto fail;
4299 sl->info.res.status = 302;
4300 s->txn->status = 302;
4301
4302 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) ||
4303 !htx_add_header(htx, ist("Connection"), ist("close")) ||
4304 !htx_add_header(htx, ist("Content-length"), ist("0")) ||
4305 !htx_add_header(htx, ist("Location"), location))
4306 goto fail;
4307
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004308 if (!htx_add_endof(htx, HTX_BLK_EOH))
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004309 goto fail;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004310
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004311 htx->flags |= HTX_FL_EOM;
Christopher Fauletc20afb82020-01-24 19:16:26 +01004312 htx_to_buf(htx, &res->buf);
Christopher Fauleta72a7e42020-01-28 09:28:11 +01004313 if (!http_forward_proxy_resp(s, 1))
4314 goto fail;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004315
4316 /* return without error. */
Christopher Faulet0eb32c02022-04-04 11:06:31 +02004317 cs_shutr(cs);
4318 cs_shutw(cs);
Christopher Faulet50264b42022-03-30 19:39:30 +02004319 s->conn_err_type = STRM_ET_NONE;
Christopher Faulet0eb32c02022-04-04 11:06:31 +02004320 cs->state = CS_ST_CLO;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004321
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004322 if (!(s->flags & SF_ERR_MASK))
4323 s->flags |= SF_ERR_LOCAL;
4324 if (!(s->flags & SF_FINST_MASK))
4325 s->flags |= SF_FINST_C;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004326
4327 /* FIXME: we should increase a counter of redirects per server and per backend. */
4328 srv_inc_sess_ctr(srv);
4329 srv_set_sess_last(srv);
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004330 return;
4331
4332 fail:
4333 /* If an error occurred, remove the incomplete HTTP response from the
4334 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004335 channel_htx_truncate(res, htx);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004336}
4337
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05004338/* This function terminates the request because it was completely analyzed or
Christopher Fauletf2824e62018-10-01 12:12:37 +02004339 * because an error was triggered during the body forwarding.
4340 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004341static void http_end_request(struct stream *s)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004342{
4343 struct channel *chn = &s->req;
4344 struct http_txn *txn = s->txn;
4345
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004346 DBG_TRACE_ENTER(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004347
Christopher Fauletb42a8b62018-11-19 21:59:00 +01004348 if (unlikely(txn->req.msg_state == HTTP_MSG_ERROR ||
4349 txn->rsp.msg_state == HTTP_MSG_ERROR)) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004350 channel_abort(chn);
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004351 channel_htx_truncate(chn, htxbuf(&chn->buf));
Christopher Fauletf2824e62018-10-01 12:12:37 +02004352 goto end;
4353 }
4354
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004355 if (unlikely(txn->req.msg_state < HTTP_MSG_DONE)) {
4356 DBG_TRACE_DEVEL("waiting end of the request", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004357 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004358 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004359
4360 if (txn->req.msg_state == HTTP_MSG_DONE) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004361 /* No need to read anymore, the request was completely parsed.
4362 * We can shut the read side unless we want to abort_on_close,
4363 * or we have a POST request. The issue with POST requests is
4364 * that some browsers still send a CRLF after the request, and
4365 * this CRLF must be read so that it does not remain in the kernel
4366 * buffers, otherwise a close could cause an RST on some systems
4367 * (eg: Linux).
4368 */
Christopher Faulet769d0e92019-03-22 14:23:18 +01004369 if (!(s->be->options & PR_O_ABRT_CLOSE) && txn->meth != HTTP_METH_POST)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004370 channel_dont_read(chn);
4371
4372 /* if the server closes the connection, we want to immediately react
4373 * and close the socket to save packets and syscalls.
4374 */
Christopher Faulet8abe7122022-03-30 15:10:18 +02004375 s->csb->flags |= CS_FL_NOHALF;
Christopher Fauletf2824e62018-10-01 12:12:37 +02004376
4377 /* In any case we've finished parsing the request so we must
4378 * disable Nagle when sending data because 1) we're not going
4379 * to shut this side, and 2) the server is waiting for us to
4380 * send pending data.
4381 */
4382 chn->flags |= CF_NEVER_WAIT;
4383
Christopher Fauletd01ce402019-01-02 17:44:13 +01004384 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
4385 /* The server has not finished to respond, so we
4386 * don't want to move in order not to upset it.
4387 */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004388 DBG_TRACE_DEVEL("waiting end of the response", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletd01ce402019-01-02 17:44:13 +01004389 return;
4390 }
4391
Christopher Fauletf2824e62018-10-01 12:12:37 +02004392 /* When we get here, it means that both the request and the
4393 * response have finished receiving. Depending on the connection
4394 * mode, we'll have to wait for the last bytes to leave in either
4395 * direction, and sometimes for a close to be effective.
4396 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02004397 if (txn->flags & TX_CON_WANT_TUN) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004398 /* Tunnel mode will not have any analyser so it needs to
4399 * poll for reads.
4400 */
4401 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004402 if (b_data(&chn->buf)) {
4403 DBG_TRACE_DEVEL("waiting to flush the request", STRM_EV_HTTP_ANA, s, txn);
Christopher Faulet9768c262018-10-22 09:34:31 +02004404 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004405 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004406 txn->req.msg_state = HTTP_MSG_TUNNEL;
4407 }
4408 else {
4409 /* we're not expecting any new data to come for this
4410 * transaction, so we can close it.
Christopher Faulet9768c262018-10-22 09:34:31 +02004411 *
4412 * However, there is an exception if the response
4413 * length is undefined. In this case, we need to wait
4414 * the close from the server. The response will be
4415 * switched in TUNNEL mode until the end.
Christopher Fauletf2824e62018-10-01 12:12:37 +02004416 */
4417 if (!(txn->rsp.flags & HTTP_MSGF_XFER_LEN) &&
4418 txn->rsp.msg_state != HTTP_MSG_CLOSED)
Christopher Faulet9768c262018-10-22 09:34:31 +02004419 goto check_channel_flags;
Christopher Fauletf2824e62018-10-01 12:12:37 +02004420
4421 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4422 channel_shutr_now(chn);
4423 channel_shutw_now(chn);
4424 }
4425 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004426 goto check_channel_flags;
4427 }
4428
4429 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
4430 http_msg_closing:
4431 /* nothing else to forward, just waiting for the output buffer
4432 * to be empty and for the shutw_now to take effect.
4433 */
4434 if (channel_is_empty(chn)) {
4435 txn->req.msg_state = HTTP_MSG_CLOSED;
4436 goto http_msg_closed;
4437 }
4438 else if (chn->flags & CF_SHUTW) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004439 txn->req.msg_state = HTTP_MSG_ERROR;
4440 goto end;
4441 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004442 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004443 return;
4444 }
4445
4446 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
4447 http_msg_closed:
Christopher Fauletf2824e62018-10-01 12:12:37 +02004448 /* if we don't know whether the server will close, we need to hard close */
4449 if (txn->rsp.flags & HTTP_MSGF_XFER_LEN)
Christopher Faulet8abe7122022-03-30 15:10:18 +02004450 s->csb->flags |= CS_FL_NOLINGER; /* we want to close ASAP */
Christopher Fauletf2824e62018-10-01 12:12:37 +02004451 /* see above in MSG_DONE why we only do this in these states */
Christopher Faulet769d0e92019-03-22 14:23:18 +01004452 if (!(s->be->options & PR_O_ABRT_CLOSE))
Christopher Fauletf2824e62018-10-01 12:12:37 +02004453 channel_dont_read(chn);
4454 goto end;
4455 }
4456
4457 check_channel_flags:
4458 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4459 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4460 /* if we've just closed an output, let's switch */
4461 txn->req.msg_state = HTTP_MSG_CLOSING;
4462 goto http_msg_closing;
4463 }
4464
4465 end:
4466 chn->analysers &= AN_REQ_FLT_END;
Christopher Faulet198ef8b2020-12-15 13:32:55 +01004467 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
4468 chn->flags |= CF_NEVER_WAIT;
4469 if (HAS_REQ_DATA_FILTERS(s))
Christopher Fauletf2824e62018-10-01 12:12:37 +02004470 chn->analysers |= AN_REQ_FLT_XFER_DATA;
Christopher Faulet198ef8b2020-12-15 13:32:55 +01004471 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004472 channel_auto_close(chn);
4473 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004474 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004475}
4476
4477
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05004478/* This function terminates the response because it was completely analyzed or
Christopher Fauletf2824e62018-10-01 12:12:37 +02004479 * because an error was triggered during the body forwarding.
4480 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004481static void http_end_response(struct stream *s)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004482{
4483 struct channel *chn = &s->res;
4484 struct http_txn *txn = s->txn;
4485
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004486 DBG_TRACE_ENTER(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004487
Christopher Fauletb42a8b62018-11-19 21:59:00 +01004488 if (unlikely(txn->req.msg_state == HTTP_MSG_ERROR ||
4489 txn->rsp.msg_state == HTTP_MSG_ERROR)) {
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004490 channel_htx_truncate(&s->req, htxbuf(&s->req.buf));
Christopher Faulet9768c262018-10-22 09:34:31 +02004491 channel_abort(&s->req);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004492 goto end;
4493 }
4494
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004495 if (unlikely(txn->rsp.msg_state < HTTP_MSG_DONE)) {
4496 DBG_TRACE_DEVEL("waiting end of the response", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004497 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004498 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004499
4500 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
4501 /* In theory, we don't need to read anymore, but we must
4502 * still monitor the server connection for a possible close
4503 * while the request is being uploaded, so we don't disable
4504 * reading.
4505 */
4506 /* channel_dont_read(chn); */
4507
4508 if (txn->req.msg_state < HTTP_MSG_DONE) {
4509 /* The client seems to still be sending data, probably
4510 * because we got an error response during an upload.
4511 * We have the choice of either breaking the connection
4512 * or letting it pass through. Let's do the later.
4513 */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004514 DBG_TRACE_DEVEL("waiting end of the request", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004515 return;
4516 }
4517
4518 /* When we get here, it means that both the request and the
4519 * response have finished receiving. Depending on the connection
4520 * mode, we'll have to wait for the last bytes to leave in either
4521 * direction, and sometimes for a close to be effective.
4522 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02004523 if (txn->flags & TX_CON_WANT_TUN) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004524 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004525 if (b_data(&chn->buf)) {
4526 DBG_TRACE_DEVEL("waiting to flush the respone", STRM_EV_HTTP_ANA, s, txn);
Christopher Faulet9768c262018-10-22 09:34:31 +02004527 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004528 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004529 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
4530 }
4531 else {
4532 /* we're not expecting any new data to come for this
4533 * transaction, so we can close it.
4534 */
4535 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4536 channel_shutr_now(chn);
4537 channel_shutw_now(chn);
4538 }
4539 }
4540 goto check_channel_flags;
4541 }
4542
4543 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
4544 http_msg_closing:
4545 /* nothing else to forward, just waiting for the output buffer
4546 * to be empty and for the shutw_now to take effect.
4547 */
4548 if (channel_is_empty(chn)) {
4549 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4550 goto http_msg_closed;
4551 }
4552 else if (chn->flags & CF_SHUTW) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004553 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau4781b152021-04-06 13:53:36 +02004554 _HA_ATOMIC_INC(&strm_sess(s)->fe->fe_counters.cli_aborts);
4555 _HA_ATOMIC_INC(&s->be->be_counters.cli_aborts);
William Lallemand36119de2021-03-08 15:26:48 +01004556 if (strm_sess(s)->listener && strm_sess(s)->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02004557 _HA_ATOMIC_INC(&strm_sess(s)->listener->counters->cli_aborts);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004558 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02004559 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.cli_aborts);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004560 goto end;
4561 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004562 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004563 return;
4564 }
4565
4566 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
4567 http_msg_closed:
4568 /* drop any pending data */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004569 channel_htx_truncate(&s->req, htxbuf(&s->req.buf));
Christopher Faulet9768c262018-10-22 09:34:31 +02004570 channel_abort(&s->req);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004571 goto end;
4572 }
4573
4574 check_channel_flags:
4575 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4576 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4577 /* if we've just closed an output, let's switch */
4578 txn->rsp.msg_state = HTTP_MSG_CLOSING;
4579 goto http_msg_closing;
4580 }
4581
4582 end:
4583 chn->analysers &= AN_RES_FLT_END;
Christopher Faulet198ef8b2020-12-15 13:32:55 +01004584 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
4585 chn->flags |= CF_NEVER_WAIT;
4586 if (HAS_RSP_DATA_FILTERS(s))
4587 chn->analysers |= AN_RES_FLT_XFER_DATA;
4588 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004589 channel_auto_close(chn);
4590 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004591 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004592}
4593
Christopher Fauletef70e252020-01-28 09:26:19 +01004594/* Forward a response generated by HAProxy (error/redirect/return). This
4595 * function forwards all pending incoming data. If <final> is set to 0, nothing
4596 * more is performed. It is used for 1xx informational messages. Otherwise, the
Christopher Faulet507479b2020-05-15 12:29:46 +02004597 * transaction is terminated and the request is emptied. On success 1 is
Christopher Faulet40e6b552020-06-25 16:04:50 +02004598 * returned. If an error occurred, 0 is returned. If it fails, this function
4599 * only exits. It is the caller responsibility to do the cleanup.
Christopher Fauletef70e252020-01-28 09:26:19 +01004600 */
4601int http_forward_proxy_resp(struct stream *s, int final)
4602{
4603 struct channel *req = &s->req;
4604 struct channel *res = &s->res;
4605 struct htx *htx = htxbuf(&res->buf);
4606 size_t data;
4607
4608 if (final) {
4609 htx->flags |= HTX_FL_PROXY_RESP;
Christopher Faulet507479b2020-05-15 12:29:46 +02004610
Christopher Fauletaab1b672020-11-18 16:44:02 +01004611 if (!htx_is_empty(htx) && !http_eval_after_res_rules(s))
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01004612 return 0;
Christopher Fauletef70e252020-01-28 09:26:19 +01004613
Christopher Fauletd6c48362020-10-19 18:01:38 +02004614 if (s->txn->meth == HTTP_METH_HEAD)
4615 htx_skip_msg_payload(htx);
4616
Christopher Fauletef70e252020-01-28 09:26:19 +01004617 channel_auto_read(req);
4618 channel_abort(req);
4619 channel_auto_close(req);
4620 channel_htx_erase(req, htxbuf(&req->buf));
4621
4622 res->wex = tick_add_ifset(now_ms, res->wto);
4623 channel_auto_read(res);
4624 channel_auto_close(res);
4625 channel_shutr_now(res);
Christopher Faulet1a9db7c2020-06-25 15:36:45 +02004626 res->flags |= CF_EOI; /* The response is terminated, add EOI */
Christopher Faulet42432f32020-11-20 17:43:16 +01004627 htxbuf(&res->buf)->flags |= HTX_FL_EOM; /* no more data are expected */
Christopher Fauletef70e252020-01-28 09:26:19 +01004628 }
Christopher Fauletcf6898c2020-06-25 15:55:11 +02004629 else {
4630 /* Send ASAP informational messages. Rely on CF_EOI for final
4631 * response.
4632 */
4633 res->flags |= CF_SEND_DONTWAIT;
4634 }
Christopher Fauletef70e252020-01-28 09:26:19 +01004635
4636 data = htx->data - co_data(res);
4637 c_adv(res, data);
4638 htx->first = -1;
4639 res->total += data;
4640 return 1;
4641}
4642
Christopher Faulet0eb32c02022-04-04 11:06:31 +02004643void http_server_error(struct stream *s, struct conn_stream *cs, int err,
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004644 int finst, struct http_reply *msg)
Christopher Faulet0f226952018-10-22 09:29:56 +02004645{
Christopher Faulet72c7d8d2020-01-27 15:32:25 +01004646 http_reply_and_close(s, s->txn->status, msg);
Christopher Faulet0f226952018-10-22 09:29:56 +02004647 if (!(s->flags & SF_ERR_MASK))
4648 s->flags |= err;
4649 if (!(s->flags & SF_FINST_MASK))
4650 s->flags |= finst;
4651}
4652
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004653void http_reply_and_close(struct stream *s, short status, struct http_reply *msg)
Christopher Faulet0f226952018-10-22 09:29:56 +02004654{
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004655 if (!msg) {
4656 channel_htx_truncate(&s->res, htxbuf(&s->res.buf));
4657 goto end;
4658 }
4659
4660 if (http_reply_message(s, msg) == -1) {
4661 /* On error, return a 500 error message, but don't rewrite it if
Christopher Faulet40e6b552020-06-25 16:04:50 +02004662 * it is already an internal error. If it was already a "const"
4663 * 500 error, just fail.
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004664 */
Christopher Faulet40e6b552020-06-25 16:04:50 +02004665 if (s->txn->status == 500) {
4666 if (s->txn->flags & TX_CONST_REPLY)
4667 goto end;
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004668 s->txn->flags |= TX_CONST_REPLY;
Christopher Faulet40e6b552020-06-25 16:04:50 +02004669 }
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004670 s->txn->status = 500;
4671 s->txn->http_reply = NULL;
4672 return http_reply_and_close(s, s->txn->status, http_error_message(s));
4673 }
4674
4675end:
4676 s->res.wex = tick_add_ifset(now_ms, s->res.wto);
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004677
Christopher Faulet2d565002021-09-10 09:17:50 +02004678 /* At this staged, HTTP analysis is finished */
4679 s->req.analysers &= AN_REQ_FLT_END;
4680 s->req.analyse_exp = TICK_ETERNITY;
4681
4682 s->res.analysers &= AN_RES_FLT_END;
4683 s->res.analyse_exp = TICK_ETERNITY;
4684
Christopher Faulet0f226952018-10-22 09:29:56 +02004685 channel_auto_read(&s->req);
4686 channel_abort(&s->req);
4687 channel_auto_close(&s->req);
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004688 channel_htx_erase(&s->req, htxbuf(&s->req.buf));
Christopher Faulet72c7d8d2020-01-27 15:32:25 +01004689 channel_auto_read(&s->res);
4690 channel_auto_close(&s->res);
4691 channel_shutr_now(&s->res);
Christopher Faulet0f226952018-10-22 09:29:56 +02004692}
4693
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004694struct http_reply *http_error_message(struct stream *s)
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004695{
4696 const int msgnum = http_get_status_idx(s->txn->status);
4697
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004698 if (s->txn->http_reply)
4699 return s->txn->http_reply;
4700 else if (s->be->replies[msgnum])
4701 return s->be->replies[msgnum];
4702 else if (strm_fe(s)->replies[msgnum])
4703 return strm_fe(s)->replies[msgnum];
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004704 else
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004705 return &http_err_replies[msgnum];
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004706}
4707
Christopher Faulet40e6b552020-06-25 16:04:50 +02004708/* Produces an HTX message from an http reply. Depending on the http reply type,
4709 * a, errorfile, an raw file or a log-format string is used. On success, it
4710 * returns 0. If an error occurs -1 is returned. If it fails, this function only
4711 * exits. It is the caller responsibility to do the cleanup.
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004712 */
Christopher Fauletae43b6c2020-05-27 15:24:22 +02004713int http_reply_to_htx(struct stream *s, struct htx *htx, struct http_reply *reply)
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004714{
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004715 struct buffer *errmsg;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004716 struct htx_sl *sl;
4717 struct buffer *body = NULL;
4718 const char *status, *reason, *clen, *ctype;
4719 unsigned int slflags;
4720 int ret = 0;
4721
Christopher Faulete29a97e2020-05-14 14:49:25 +02004722 /*
4723 * - HTTP_REPLY_ERRFILES unexpected here. handled as no payload if so
4724 *
4725 * - HTTP_REPLY_INDIRECT: switch on another reply if defined or handled
4726 * as no payload if NULL. the TXN status code is set with the status
4727 * of the original reply.
4728 */
4729
4730 if (reply->type == HTTP_REPLY_INDIRECT) {
4731 if (reply->body.reply)
4732 reply = reply->body.reply;
4733 }
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004734 if (reply->type == HTTP_REPLY_ERRMSG && !reply->body.errmsg) {
4735 /* get default error message */
4736 if (reply == s->txn->http_reply)
4737 s->txn->http_reply = NULL;
4738 reply = http_error_message(s);
4739 if (reply->type == HTTP_REPLY_INDIRECT) {
4740 if (reply->body.reply)
4741 reply = reply->body.reply;
4742 }
4743 }
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004744
4745 if (reply->type == HTTP_REPLY_ERRMSG) {
4746 /* implicit or explicit error message*/
4747 errmsg = reply->body.errmsg;
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004748 if (errmsg && !b_is_null(errmsg)) {
Christopher Faulet20567362020-05-15 14:52:49 +02004749 if (!htx_copy_msg(htx, errmsg))
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004750 goto fail;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004751 }
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004752 }
4753 else {
4754 /* no payload, file or log-format string */
4755 if (reply->type == HTTP_REPLY_RAW) {
4756 /* file */
4757 body = &reply->body.obj;
4758 }
4759 else if (reply->type == HTTP_REPLY_LOGFMT) {
4760 /* log-format string */
4761 body = alloc_trash_chunk();
4762 if (!body)
4763 goto fail_alloc;
4764 body->data = build_logline(s, body->area, body->size, &reply->body.fmt);
4765 }
4766 /* else no payload */
4767
4768 status = ultoa(reply->status);
4769 reason = http_get_reason(reply->status);
4770 slflags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_CLEN);
4771 if (!body || !b_data(body))
4772 slflags |= HTX_SL_F_BODYLESS;
4773 sl = htx_add_stline(htx, HTX_BLK_RES_SL, slflags, ist("HTTP/1.1"), ist(status), ist(reason));
4774 if (!sl)
4775 goto fail;
4776 sl->info.res.status = reply->status;
4777
4778 clen = (body ? ultoa(b_data(body)) : "0");
4779 ctype = reply->ctype;
4780
4781 if (!LIST_ISEMPTY(&reply->hdrs)) {
4782 struct http_reply_hdr *hdr;
4783 struct buffer *value = alloc_trash_chunk();
4784
4785 if (!value)
4786 goto fail;
4787
4788 list_for_each_entry(hdr, &reply->hdrs, list) {
4789 chunk_reset(value);
4790 value->data = build_logline(s, value->area, value->size, &hdr->value);
4791 if (b_data(value) && !htx_add_header(htx, hdr->name, ist2(b_head(value), b_data(value)))) {
4792 free_trash_chunk(value);
4793 goto fail;
4794 }
4795 chunk_reset(value);
4796 }
4797 free_trash_chunk(value);
4798 }
4799
4800 if (!htx_add_header(htx, ist("content-length"), ist(clen)) ||
4801 (body && b_data(body) && ctype && !htx_add_header(htx, ist("content-type"), ist(ctype))) ||
4802 !htx_add_endof(htx, HTX_BLK_EOH) ||
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004803 (body && b_data(body) && !htx_add_data_atonce(htx, ist2(b_head(body), b_data(body)))))
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004804 goto fail;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004805
4806 htx->flags |= HTX_FL_EOM;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004807 }
4808
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004809 leave:
4810 if (reply->type == HTTP_REPLY_LOGFMT)
4811 free_trash_chunk(body);
4812 return ret;
4813
4814 fail_alloc:
4815 if (!(s->flags & SF_ERR_MASK))
4816 s->flags |= SF_ERR_RESOURCE;
Christopher Faulet97e466c2020-05-15 15:12:47 +02004817 /* fall through */
4818 fail:
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004819 ret = -1;
4820 goto leave;
Christopher Faulet97e466c2020-05-15 15:12:47 +02004821}
4822
4823/* Send an http reply to the client. On success, it returns 0. If an error
Christopher Faulet40e6b552020-06-25 16:04:50 +02004824 * occurs -1 is returned and the response channel is truncated, removing this
4825 * way the faulty reply. This function may fail when the reply is formatted
4826 * (http_reply_to_htx) or when the reply is forwarded
4827 * (http_forward_proxy_resp). On the last case, it is because a
4828 * http-after-response rule fails.
Christopher Faulet97e466c2020-05-15 15:12:47 +02004829 */
4830int http_reply_message(struct stream *s, struct http_reply *reply)
4831{
4832 struct channel *res = &s->res;
4833 struct htx *htx = htx_from_buf(&res->buf);
4834
4835 if (s->txn->status == -1)
4836 s->txn->status = reply->status;
4837 channel_htx_truncate(res, htx);
4838
4839 if (http_reply_to_htx(s, htx, reply) == -1)
4840 goto fail;
4841
4842 htx_to_buf(htx, &s->res.buf);
4843 if (!http_forward_proxy_resp(s, 1))
4844 goto fail;
4845 return 0;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004846
4847 fail:
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004848 channel_htx_truncate(res, htx);
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004849 if (!(s->flags & SF_ERR_MASK))
4850 s->flags |= SF_ERR_PRXCOND;
Christopher Faulet97e466c2020-05-15 15:12:47 +02004851 return -1;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004852}
4853
Christopher Faulet50264b42022-03-30 19:39:30 +02004854/* Return the error message corresponding to s->conn_err_type. It is assumed
Christopher Faulet304cc402019-07-15 15:46:28 +02004855 * that the server side is closed. Note that err_type is actually a
4856 * bitmask, where almost only aborts may be cumulated with other
4857 * values. We consider that aborted operations are more important
4858 * than timeouts or errors due to the fact that nobody else in the
4859 * logs might explain incomplete retries. All others should avoid
4860 * being cumulated. It should normally not be possible to have multiple
4861 * aborts at once, but just in case, the first one in sequence is reported.
4862 * Note that connection errors appearing on the second request of a keep-alive
4863 * connection are not reported since this allows the client to retry.
4864 */
Christopher Faulet0eb32c02022-04-04 11:06:31 +02004865void http_return_srv_error(struct stream *s, struct conn_stream *cs)
Christopher Faulet304cc402019-07-15 15:46:28 +02004866{
Christopher Faulet50264b42022-03-30 19:39:30 +02004867 int err_type = s->conn_err_type;
Christopher Faulet304cc402019-07-15 15:46:28 +02004868
4869 /* set s->txn->status for http_error_message(s) */
Christopher Faulet50264b42022-03-30 19:39:30 +02004870 if (err_type & STRM_ET_QUEUE_ABRT) {
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004871 s->txn->status = -1;
Christopher Faulet0eb32c02022-04-04 11:06:31 +02004872 http_server_error(s, cs, SF_ERR_CLICL, SF_FINST_Q, NULL);
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004873 }
Christopher Faulet50264b42022-03-30 19:39:30 +02004874 else if (err_type & STRM_ET_CONN_ABRT) {
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004875 s->txn->status = -1;
Christopher Faulet0eb32c02022-04-04 11:06:31 +02004876 http_server_error(s, cs, SF_ERR_CLICL, SF_FINST_C, NULL);
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004877 }
Christopher Faulet50264b42022-03-30 19:39:30 +02004878 else if (err_type & STRM_ET_QUEUE_TO) {
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004879 s->txn->status = 503;
Christopher Faulet0eb32c02022-04-04 11:06:31 +02004880 http_server_error(s, cs, SF_ERR_SRVTO, SF_FINST_Q,
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004881 http_error_message(s));
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004882 }
Christopher Faulet50264b42022-03-30 19:39:30 +02004883 else if (err_type & STRM_ET_QUEUE_ERR) {
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004884 s->txn->status = 503;
Christopher Faulet0eb32c02022-04-04 11:06:31 +02004885 http_server_error(s, cs, SF_ERR_SRVCL, SF_FINST_Q,
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004886 http_error_message(s));
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004887 }
Christopher Faulet50264b42022-03-30 19:39:30 +02004888 else if (err_type & STRM_ET_CONN_TO) {
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004889 s->txn->status = 503;
Christopher Faulet0eb32c02022-04-04 11:06:31 +02004890 http_server_error(s, cs, SF_ERR_SRVTO, SF_FINST_C,
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004891 (s->txn->flags & TX_NOT_FIRST) ? NULL :
4892 http_error_message(s));
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004893 }
Christopher Faulet50264b42022-03-30 19:39:30 +02004894 else if (err_type & STRM_ET_CONN_ERR) {
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004895 s->txn->status = 503;
Christopher Faulet0eb32c02022-04-04 11:06:31 +02004896 http_server_error(s, cs, SF_ERR_SRVCL, SF_FINST_C,
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004897 (s->flags & SF_SRV_REUSED) ? NULL :
4898 http_error_message(s));
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004899 }
Christopher Faulet50264b42022-03-30 19:39:30 +02004900 else if (err_type & STRM_ET_CONN_RES) {
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004901 s->txn->status = 503;
Christopher Faulet0eb32c02022-04-04 11:06:31 +02004902 http_server_error(s, cs, SF_ERR_RESOURCE, SF_FINST_C,
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004903 (s->txn->flags & TX_NOT_FIRST) ? NULL :
4904 http_error_message(s));
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004905 }
Christopher Faulet50264b42022-03-30 19:39:30 +02004906 else { /* STRM_ET_CONN_OTHER and others */
Christopher Faulet304cc402019-07-15 15:46:28 +02004907 s->txn->status = 500;
Christopher Faulet0eb32c02022-04-04 11:06:31 +02004908 http_server_error(s, cs, SF_ERR_INTERNAL, SF_FINST_C,
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004909 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004910 }
4911}
4912
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004913
Christopher Faulet4a28a532019-03-01 11:19:40 +01004914/* Handle Expect: 100-continue for HTTP/1.1 messages if necessary. It returns 0
4915 * on success and -1 on error.
4916 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004917static int http_handle_expect_hdr(struct stream *s, struct htx *htx, struct http_msg *msg)
Christopher Faulet4a28a532019-03-01 11:19:40 +01004918{
4919 /* If we have HTTP/1.1 message with a body and Expect: 100-continue,
4920 * then we must send an HTTP/1.1 100 Continue intermediate response.
4921 */
4922 if (msg->msg_state == HTTP_MSG_BODY && (msg->flags & HTTP_MSGF_VER_11) &&
4923 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
4924 struct ist hdr = { .ptr = "Expect", .len = 6 };
4925 struct http_hdr_ctx ctx;
4926
4927 ctx.blk = NULL;
4928 /* Expect is allowed in 1.1, look for it */
4929 if (http_find_header(htx, hdr, &ctx, 0) &&
4930 unlikely(isteqi(ctx.value, ist2("100-continue", 12)))) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004931 if (http_reply_100_continue(s) == -1)
Christopher Faulet4a28a532019-03-01 11:19:40 +01004932 return -1;
4933 http_remove_header(htx, &ctx);
4934 }
4935 }
4936 return 0;
4937}
4938
Christopher Faulet23a3c792018-11-28 10:01:23 +01004939/* Send a 100-Continue response to the client. It returns 0 on success and -1
4940 * on error. The response channel is updated accordingly.
4941 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004942static int http_reply_100_continue(struct stream *s)
Christopher Faulet23a3c792018-11-28 10:01:23 +01004943{
4944 struct channel *res = &s->res;
4945 struct htx *htx = htx_from_buf(&res->buf);
4946 struct htx_sl *sl;
4947 unsigned int flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|
4948 HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
Christopher Faulet23a3c792018-11-28 10:01:23 +01004949
4950 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
4951 ist("HTTP/1.1"), ist("100"), ist("Continue"));
4952 if (!sl)
4953 goto fail;
4954 sl->info.res.status = 100;
4955
Christopher Faulet1d5ec092019-06-26 14:23:54 +02004956 if (!htx_add_endof(htx, HTX_BLK_EOH))
Christopher Faulet23a3c792018-11-28 10:01:23 +01004957 goto fail;
4958
Christopher Fauleta72a7e42020-01-28 09:28:11 +01004959 if (!http_forward_proxy_resp(s, 0))
4960 goto fail;
Christopher Faulet23a3c792018-11-28 10:01:23 +01004961 return 0;
4962
4963 fail:
4964 /* If an error occurred, remove the incomplete HTTP response from the
4965 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004966 channel_htx_truncate(res, htx);
Christopher Faulet23a3c792018-11-28 10:01:23 +01004967 return -1;
4968}
4969
Christopher Faulet12c51e22018-11-28 15:59:42 +01004970
Christopher Faulet0f226952018-10-22 09:29:56 +02004971/*
4972 * Capture headers from message <htx> according to header list <cap_hdr>, and
4973 * fill the <cap> pointers appropriately.
4974 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004975static void http_capture_headers(struct htx *htx, char **cap, struct cap_hdr *cap_hdr)
Christopher Faulet0f226952018-10-22 09:29:56 +02004976{
4977 struct cap_hdr *h;
4978 int32_t pos;
4979
Christopher Fauleta3f15502019-05-13 15:27:23 +02004980 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet0f226952018-10-22 09:29:56 +02004981 struct htx_blk *blk = htx_get_blk(htx, pos);
4982 enum htx_blk_type type = htx_get_blk_type(blk);
4983 struct ist n, v;
4984
4985 if (type == HTX_BLK_EOH)
4986 break;
4987 if (type != HTX_BLK_HDR)
4988 continue;
4989
4990 n = htx_get_blk_name(htx, blk);
4991
4992 for (h = cap_hdr; h; h = h->next) {
4993 if (h->namelen && (h->namelen == n.len) &&
4994 (strncasecmp(n.ptr, h->name, h->namelen) == 0)) {
4995 if (cap[h->index] == NULL)
4996 cap[h->index] =
4997 pool_alloc(h->pool);
4998
4999 if (cap[h->index] == NULL) {
5000 ha_alert("HTTP capture : out of memory.\n");
5001 break;
5002 }
5003
5004 v = htx_get_blk_value(htx, blk);
Tim Duesterhus2471f5c2021-11-08 09:05:01 +01005005 v = isttrim(v, h->len);
Christopher Faulet0f226952018-10-22 09:29:56 +02005006
5007 memcpy(cap[h->index], v.ptr, v.len);
5008 cap[h->index][v.len]=0;
5009 }
5010 }
5011 }
5012}
5013
Christopher Faulet0b6bdc52018-10-24 11:05:36 +02005014/* Delete a value in a header between delimiters <from> and <next>. The header
5015 * itself is delimited by <start> and <end> pointers. The number of characters
5016 * displaced is returned, and the pointer to the first delimiter is updated if
5017 * required. The function tries as much as possible to respect the following
5018 * principles :
5019 * - replace <from> delimiter by the <next> one unless <from> points to <start>,
5020 * in which case <next> is simply removed
5021 * - set exactly one space character after the new first delimiter, unless there
5022 * are not enough characters in the block being moved to do so.
5023 * - remove unneeded spaces before the previous delimiter and after the new
5024 * one.
5025 *
5026 * It is the caller's responsibility to ensure that :
5027 * - <from> points to a valid delimiter or <start> ;
5028 * - <next> points to a valid delimiter or <end> ;
5029 * - there are non-space chars before <from>.
5030 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005031static int http_del_hdr_value(char *start, char *end, char **from, char *next)
Christopher Faulet0b6bdc52018-10-24 11:05:36 +02005032{
5033 char *prev = *from;
5034
5035 if (prev == start) {
5036 /* We're removing the first value. eat the semicolon, if <next>
5037 * is lower than <end> */
5038 if (next < end)
5039 next++;
5040
5041 while (next < end && HTTP_IS_SPHT(*next))
5042 next++;
5043 }
5044 else {
5045 /* Remove useless spaces before the old delimiter. */
5046 while (HTTP_IS_SPHT(*(prev-1)))
5047 prev--;
5048 *from = prev;
5049
5050 /* copy the delimiter and if possible a space if we're
5051 * not at the end of the line.
5052 */
5053 if (next < end) {
5054 *prev++ = *next++;
5055 if (prev + 1 < next)
5056 *prev++ = ' ';
5057 while (next < end && HTTP_IS_SPHT(*next))
5058 next++;
5059 }
5060 }
5061 memmove(prev, next, end - next);
5062 return (prev - next);
5063}
5064
Christopher Faulet0f226952018-10-22 09:29:56 +02005065
5066/* Formats the start line of the request (without CRLF) and puts it in <str> and
Joseph Herlantc42c0e92018-11-25 10:43:27 -08005067 * return the written length. The line can be truncated if it exceeds <len>.
Christopher Faulet0f226952018-10-22 09:29:56 +02005068 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005069static size_t http_fmt_req_line(const struct htx_sl *sl, char *str, size_t len)
Christopher Faulet0f226952018-10-22 09:29:56 +02005070{
5071 struct ist dst = ist2(str, 0);
5072
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005073 if (istcat(&dst, htx_sl_req_meth(sl), len) == -1)
Christopher Faulet0f226952018-10-22 09:29:56 +02005074 goto end;
5075 if (dst.len + 1 > len)
5076 goto end;
5077 dst.ptr[dst.len++] = ' ';
5078
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005079 if (istcat(&dst, htx_sl_req_uri(sl), len) == -1)
Christopher Faulet0f226952018-10-22 09:29:56 +02005080 goto end;
5081 if (dst.len + 1 > len)
5082 goto end;
5083 dst.ptr[dst.len++] = ' ';
5084
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005085 istcat(&dst, htx_sl_req_vsn(sl), len);
Christopher Faulet0f226952018-10-22 09:29:56 +02005086 end:
5087 return dst.len;
5088}
5089
5090/*
5091 * Print a debug line with a start line.
5092 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005093static void http_debug_stline(const char *dir, struct stream *s, const struct htx_sl *sl)
Christopher Faulet0f226952018-10-22 09:29:56 +02005094{
5095 struct session *sess = strm_sess(s);
5096 int max;
5097
5098 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
5099 dir,
Willy Tarreau88bc8002021-12-06 07:01:02 +00005100 objt_conn(sess->origin) ? (unsigned short)__objt_conn(sess->origin)->handle.fd : -1,
Christopher Faulet693b23b2022-02-28 09:09:05 +01005101 cs_conn(s->csb) ? (unsigned short)(__cs_conn(s->csb))->handle.fd : -1);
Christopher Faulet0f226952018-10-22 09:29:56 +02005102
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005103 max = HTX_SL_P1_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02005104 UBOUND(max, trash.size - trash.data - 3);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005105 chunk_memcat(&trash, HTX_SL_P1_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02005106 trash.area[trash.data++] = ' ';
5107
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005108 max = HTX_SL_P2_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02005109 UBOUND(max, trash.size - trash.data - 2);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005110 chunk_memcat(&trash, HTX_SL_P2_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02005111 trash.area[trash.data++] = ' ';
5112
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005113 max = HTX_SL_P3_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02005114 UBOUND(max, trash.size - trash.data - 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005115 chunk_memcat(&trash, HTX_SL_P3_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02005116 trash.area[trash.data++] = '\n';
5117
Willy Tarreau2e8ab6b2020-03-14 11:03:20 +01005118 DISGUISE(write(1, trash.area, trash.data));
Christopher Faulet0f226952018-10-22 09:29:56 +02005119}
5120
5121/*
5122 * Print a debug line with a header.
5123 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005124static 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 +02005125{
5126 struct session *sess = strm_sess(s);
5127 int max;
5128
5129 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
5130 dir,
Willy Tarreau88bc8002021-12-06 07:01:02 +00005131 objt_conn(sess->origin) ? (unsigned short)__objt_conn(sess->origin)->handle.fd : -1,
Christopher Faulet693b23b2022-02-28 09:09:05 +01005132 cs_conn(s->csb) ? (unsigned short)(__cs_conn(s->csb))->handle.fd : -1);
Christopher Faulet0f226952018-10-22 09:29:56 +02005133
5134 max = n.len;
5135 UBOUND(max, trash.size - trash.data - 3);
5136 chunk_memcat(&trash, n.ptr, max);
5137 trash.area[trash.data++] = ':';
5138 trash.area[trash.data++] = ' ';
5139
5140 max = v.len;
5141 UBOUND(max, trash.size - trash.data - 1);
5142 chunk_memcat(&trash, v.ptr, max);
5143 trash.area[trash.data++] = '\n';
5144
Willy Tarreau2e8ab6b2020-03-14 11:03:20 +01005145 DISGUISE(write(1, trash.area, trash.data));
Christopher Faulet0f226952018-10-22 09:29:56 +02005146}
5147
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005148void http_txn_reset_req(struct http_txn *txn)
5149{
Christopher Faulet1aea50e2020-01-17 16:03:53 +01005150 txn->req.flags = 0;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005151 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
5152}
5153
5154void http_txn_reset_res(struct http_txn *txn)
5155{
Christopher Faulet1aea50e2020-01-17 16:03:53 +01005156 txn->rsp.flags = 0;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005157 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
5158}
5159
5160/*
Christopher Faulet75f619a2021-03-08 19:12:58 +01005161 * Create and initialize a new HTTP transaction for stream <s>. This should be
5162 * used before processing any new request. It returns the transaction or NLULL
5163 * on error.
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005164 */
Christopher Faulet75f619a2021-03-08 19:12:58 +01005165struct http_txn *http_create_txn(struct stream *s)
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005166{
Christopher Faulet75f619a2021-03-08 19:12:58 +01005167 struct http_txn *txn;
Christopher Faulet95a61e82021-12-22 14:22:03 +01005168 struct conn_stream *cs = s->csf;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005169
Christopher Faulet75f619a2021-03-08 19:12:58 +01005170 txn = pool_alloc(pool_head_http_txn);
5171 if (!txn)
5172 return NULL;
5173 s->txn = txn;
5174
Christopher Faulete9e48202022-03-22 18:13:29 +01005175 txn->flags = ((cs && cs->endp->flags & CS_EP_NOT_FIRST) ? TX_NOT_FIRST : 0);
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005176 txn->status = -1;
Christopher Faulet5cb513a2020-05-13 17:56:56 +02005177 txn->http_reply = NULL;
Christopher Faulete05bf9e2022-03-29 15:23:40 +02005178 txn->l7_buffer = BUF_NULL;
Willy Tarreau8b507582020-02-25 09:35:07 +01005179 write_u32(txn->cache_hash, 0);
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005180
5181 txn->cookie_first_date = 0;
5182 txn->cookie_last_date = 0;
5183
5184 txn->srv_cookie = NULL;
5185 txn->cli_cookie = NULL;
5186 txn->uri = NULL;
5187
5188 http_txn_reset_req(txn);
5189 http_txn_reset_res(txn);
5190
5191 txn->req.chn = &s->req;
5192 txn->rsp.chn = &s->res;
5193
5194 txn->auth.method = HTTP_AUTH_UNKNOWN;
5195
Willy Tarreaub7bfcb32021-08-31 08:13:25 +02005196 vars_init_head(&s->vars_txn, SCOPE_TXN);
5197 vars_init_head(&s->vars_reqres, SCOPE_REQ);
Christopher Faulet75f619a2021-03-08 19:12:58 +01005198
5199 return txn;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005200}
5201
5202/* to be used at the end of a transaction */
Christopher Faulet75f619a2021-03-08 19:12:58 +01005203void http_destroy_txn(struct stream *s)
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005204{
5205 struct http_txn *txn = s->txn;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005206
5207 /* these ones will have been dynamically allocated */
5208 pool_free(pool_head_requri, txn->uri);
5209 pool_free(pool_head_capture, txn->cli_cookie);
5210 pool_free(pool_head_capture, txn->srv_cookie);
Tim Duesterhusa17e6622020-03-05 20:19:02 +01005211 pool_free(pool_head_uniqueid, s->unique_id.ptr);
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005212
Tim Duesterhusa17e6622020-03-05 20:19:02 +01005213 s->unique_id = IST_NULL;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005214 txn->uri = NULL;
5215 txn->srv_cookie = NULL;
5216 txn->cli_cookie = NULL;
5217
Christopher Faulet59399252019-11-07 14:27:52 +01005218 if (!LIST_ISEMPTY(&s->vars_txn.head))
5219 vars_prune(&s->vars_txn, s->sess, s);
5220 if (!LIST_ISEMPTY(&s->vars_reqres.head))
5221 vars_prune(&s->vars_reqres, s->sess, s);
Christopher Faulet75f619a2021-03-08 19:12:58 +01005222
Christopher Faulete05bf9e2022-03-29 15:23:40 +02005223 b_free(&txn->l7_buffer);
5224
Christopher Faulet75f619a2021-03-08 19:12:58 +01005225 pool_free(pool_head_http_txn, txn);
5226 s->txn = NULL;
Christopher Faulet59399252019-11-07 14:27:52 +01005227}
5228
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005229
5230DECLARE_POOL(pool_head_http_txn, "http_txn", sizeof(struct http_txn));
Christopher Faulet0f226952018-10-22 09:29:56 +02005231
Christopher Fauletf4eb75d2018-10-11 15:55:07 +02005232/*
5233 * Local variables:
5234 * c-indent-level: 8
5235 * c-basic-offset: 8
5236 * End:
5237 */