blob: 2b2cfdc56103f313d766143f9016d91200065092 [file] [log] [blame]
Christopher Fauletf4eb75d2018-10-11 15:55:07 +02001/*
2 * HTTP protocol analyzer
3 *
4 * Copyright (C) 2018 HAProxy Technologies, Christopher Faulet <cfaulet@haproxy.com>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
Willy Tarreaudcc048a2020-06-04 19:11:43 +020013#include <haproxy/acl.h>
Willy Tarreau122eba92020-06-04 10:15:32 +020014#include <haproxy/action-t.h>
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020015#include <haproxy/api.h>
Willy Tarreau91cefca2022-05-03 17:08:29 +020016#include <haproxy/applet.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020017#include <haproxy/backend.h>
Willy Tarreau8d366972020-05-27 16:10:29 +020018#include <haproxy/base64.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020019#include <haproxy/capture-t.h>
Amaury Denoyelle03517732021-05-07 14:25:01 +020020#include <haproxy/cfgparse.h>
Willy Tarreauf1d32c42020-06-04 21:07:02 +020021#include <haproxy/channel.h>
Willy Tarreau4aa573d2020-06-04 18:21:56 +020022#include <haproxy/check.h>
Willy Tarreau7ea393d2020-06-04 18:02:10 +020023#include <haproxy/connection.h>
Willy Tarreau36979d92020-06-05 17:27:29 +020024#include <haproxy/errors.h>
Willy Tarreauc7babd82020-06-04 21:29:29 +020025#include <haproxy/filters.h>
Willy Tarreaucd72d8c2020-06-02 19:11:26 +020026#include <haproxy/http.h>
Willy Tarreauc2b1ff02020-06-04 21:21:03 +020027#include <haproxy/http_ana.h>
Willy Tarreau87735332020-06-04 09:08:41 +020028#include <haproxy/http_htx.h>
Willy Tarreau16f958c2020-06-03 08:44:35 +020029#include <haproxy/htx.h>
Willy Tarreauaeed4a82020-06-04 22:01:04 +020030#include <haproxy/log.h>
Willy Tarreau6131d6a2020-06-02 16:48:09 +020031#include <haproxy/net_helper.h>
Willy Tarreaua264d962020-06-04 22:29:18 +020032#include <haproxy/proxy.h>
Willy Tarreau7cd8b6e2020-06-02 17:32:26 +020033#include <haproxy/regex.h>
Willy Tarreau5edca2f2022-05-27 09:25:10 +020034#include <haproxy/sc_strm.h>
Willy Tarreau1e56f922020-06-04 23:20:13 +020035#include <haproxy/server-t.h>
Willy Tarreau2eec9b52020-06-04 19:58:55 +020036#include <haproxy/stats.h>
Willy Tarreaucb086c62022-05-27 09:47:12 +020037#include <haproxy/stconn.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020038#include <haproxy/stream.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020039#include <haproxy/trace.h>
Willy Tarreau8c42b8a2020-06-04 19:27:34 +020040#include <haproxy/uri_auth-t.h>
Willy Tarreaua1718922020-06-04 16:25:31 +020041#include <haproxy/vars.h>
Christopher Faulete0768eb2018-10-03 16:38:02 +020042
Christopher Faulete0768eb2018-10-03 16:38:02 +020043
Christopher Fauleteea8fc72019-11-05 16:18:10 +010044#define TRACE_SOURCE &trace_strm
45
Christopher Faulet377c5a52018-10-24 21:21:30 +020046extern const char *stat_status_codes[];
Christopher Fauletf2824e62018-10-01 12:12:37 +020047
Willy Tarreauff882702021-04-10 17:23:00 +020048struct pool_head *pool_head_requri __read_mostly = NULL;
49struct pool_head *pool_head_capture __read_mostly = NULL;
Christopher Fauleta8a46e22019-07-16 14:53:09 +020050
51
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020052static void http_end_request(struct stream *s);
53static void http_end_response(struct stream *s);
Christopher Fauletf2824e62018-10-01 12:12:37 +020054
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020055static void http_capture_headers(struct htx *htx, char **cap, struct cap_hdr *cap_hdr);
56static int http_del_hdr_value(char *start, char *end, char **from, char *next);
57static size_t http_fmt_req_line(const struct htx_sl *sl, char *str, size_t len);
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020058static void http_debug_stline(const char *dir, struct stream *s, const struct htx_sl *sl);
59static void http_debug_hdr(const char *dir, struct stream *s, const struct ist n, const struct ist v);
Christopher Faulet0f226952018-10-22 09:29:56 +020060
Christopher Fauletd4150ad2021-10-13 15:35:55 +020061static enum rule_result http_req_get_intercept_rule(struct proxy *px, struct list *def_rules, struct list *rules, struct stream *s);
62static enum rule_result http_res_get_intercept_rule(struct proxy *px, struct list *def_rules, struct list *rules, struct stream *s);
Christopher Faulet18c13d32022-05-16 11:43:10 +020063static enum rule_result http_req_restrict_header_names(struct stream *s, struct htx *htx, struct proxy *px);
Christopher Faulet3e964192018-10-24 11:39:23 +020064
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020065static void http_manage_client_side_cookies(struct stream *s, struct channel *req);
66static void http_manage_server_side_cookies(struct stream *s, struct channel *res);
Christopher Fauletfcda7c62018-10-24 11:56:22 +020067
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020068static int http_stats_check_uri(struct stream *s, struct http_txn *txn, struct proxy *backend);
69static int http_handle_stats(struct stream *s, struct channel *req);
Christopher Faulet377c5a52018-10-24 21:21:30 +020070
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020071static int http_handle_expect_hdr(struct stream *s, struct htx *htx, struct http_msg *msg);
72static int http_reply_100_continue(struct stream *s);
Christopher Faulet23a3c792018-11-28 10:01:23 +010073
Christopher Faulete0768eb2018-10-03 16:38:02 +020074/* This stream analyser waits for a complete HTTP request. It returns 1 if the
75 * processing can continue on next analysers, or zero if it either needs more
76 * data or wants to immediately abort the request (eg: timeout, error, ...). It
77 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req.analysers
78 * when it has nothing left to do, and may remove any analyser when it wants to
79 * abort.
80 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020081int http_wait_for_request(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +020082{
Christopher Faulet9768c262018-10-22 09:34:31 +020083
Christopher Faulete0768eb2018-10-03 16:38:02 +020084 /*
Christopher Faulet9768c262018-10-22 09:34:31 +020085 * We will analyze a complete HTTP request to check the its syntax.
Christopher Faulete0768eb2018-10-03 16:38:02 +020086 *
Christopher Faulet9768c262018-10-22 09:34:31 +020087 * Once the start line and all headers are received, we may perform a
88 * capture of the error (if any), and we will set a few fields. We also
89 * check for monitor-uri, logging and finally headers capture.
Christopher Faulete0768eb2018-10-03 16:38:02 +020090 */
Christopher Faulete0768eb2018-10-03 16:38:02 +020091 struct session *sess = s->sess;
92 struct http_txn *txn = s->txn;
93 struct http_msg *msg = &txn->req;
Christopher Faulet9768c262018-10-22 09:34:31 +020094 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +010095 struct htx_sl *sl;
Christopher Faulete0768eb2018-10-03 16:38:02 +020096
Christopher Fauleteea8fc72019-11-05 16:18:10 +010097 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +020098
Christopher Fauletda46a0d2021-01-21 17:32:58 +010099 if (unlikely(!IS_HTX_STRM(s))) {
100 /* It is only possible when a TCP stream is upgrade to HTTP.
101 * There is a transition period during which there is no
102 * data. The stream is still in raw mode and SF_IGNORE flag is
103 * still set. When this happens, the new mux is responsible to
Ilya Shipitsinacf84592021-02-06 22:29:08 +0500104 * handle all errors. Thus we may leave immediately.
Christopher Fauletda46a0d2021-01-21 17:32:58 +0100105 */
106 BUG_ON(!(s->flags & SF_IGNORE) || !c_empty(&s->req));
Christopher Faulet9768c262018-10-22 09:34:31 +0200107
Christopher Faulet97b3a612021-03-15 17:10:12 +0100108 /* Don't connect for now */
109 channel_dont_connect(req);
110
111 /* A SHUTR at this stage means we are performing a "destructive"
112 * HTTP upgrade (TCP>H2). In this case, we can leave.
113 */
114 if (req->flags & CF_SHUTR) {
115 s->logs.logwait = 0;
116 s->logs.level = 0;
117 channel_abort(&s->req);
118 channel_abort(&s->res);
119 req->analysers &= AN_REQ_FLT_END;
120 req->analyse_exp = TICK_ETERNITY;
121 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA, s);
122 return 1;
123 }
Christopher Fauletda46a0d2021-01-21 17:32:58 +0100124 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA, s);
125 return 0;
126 }
127
128 htx = htxbuf(&req->buf);
Christopher Faulet8bebd2f2020-10-06 17:54:56 +0200129
Willy Tarreau4236f032019-03-05 10:43:32 +0100130 /* Parsing errors are caught here */
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200131 if (htx->flags & (HTX_FL_PARSING_ERROR|HTX_FL_PROCESSING_ERROR)) {
Willy Tarreau4236f032019-03-05 10:43:32 +0100132 stream_inc_http_req_ctr(s);
Emeric Brun28976442020-10-07 08:50:09 +0200133 proxy_inc_fe_req_ctr(sess->listener, sess->fe);
Christopher Fauletbf7175f2021-02-10 14:58:01 +0100134 if (htx->flags & HTX_FL_PARSING_ERROR) {
135 stream_inc_http_err_ctr(s);
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200136 goto return_bad_req;
Christopher Fauletbf7175f2021-02-10 14:58:01 +0100137 }
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200138 else
139 goto return_int_err;
Willy Tarreau4236f032019-03-05 10:43:32 +0100140 }
141
Christopher Faulete0768eb2018-10-03 16:38:02 +0200142 /* we're speaking HTTP here, so let's speak HTTP to the client */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200143 s->srv_error = http_return_srv_error;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200144
Christopher Faulet9768c262018-10-22 09:34:31 +0200145 msg->msg_state = HTTP_MSG_BODY;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200146 stream_inc_http_req_ctr(s);
Emeric Brun28976442020-10-07 08:50:09 +0200147 proxy_inc_fe_req_ctr(sess->listener, sess->fe); /* one more valid request for this FE */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200148
Christopher Faulet9768c262018-10-22 09:34:31 +0200149 /* kill the pending keep-alive timeout */
Christopher Faulet9768c262018-10-22 09:34:31 +0200150 req->analyse_exp = TICK_ETERNITY;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200151
Christopher Faulet29f17582019-05-23 11:03:26 +0200152 BUG_ON(htx_get_first_type(htx) != HTX_BLK_REQ_SL);
Christopher Faulet297fbb42019-05-13 14:41:27 +0200153 sl = http_get_stline(htx);
Christopher Faulet03599112018-11-27 11:21:21 +0100154
Christopher Faulet9768c262018-10-22 09:34:31 +0200155 /* 0: we might have to print this header in debug mode */
156 if (unlikely((global.mode & MODE_DEBUG) &&
157 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
158 int32_t pos;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200159
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200160 http_debug_stline("clireq", s, sl);
Christopher Faulet9768c262018-10-22 09:34:31 +0200161
Christopher Fauleta3f15502019-05-13 15:27:23 +0200162 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet9768c262018-10-22 09:34:31 +0200163 struct htx_blk *blk = htx_get_blk(htx, pos);
164 enum htx_blk_type type = htx_get_blk_type(blk);
165
166 if (type == HTX_BLK_EOH)
167 break;
168 if (type != HTX_BLK_HDR)
169 continue;
170
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200171 http_debug_hdr("clihdr", s,
172 htx_get_blk_name(htx, blk),
173 htx_get_blk_value(htx, blk));
Christopher Faulet9768c262018-10-22 09:34:31 +0200174 }
175 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200176
177 /*
Christopher Faulet03599112018-11-27 11:21:21 +0100178 * 1: identify the method and the version. Also set HTTP flags
Christopher Faulete0768eb2018-10-03 16:38:02 +0200179 */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100180 txn->meth = sl->info.req.meth;
Christopher Faulet03599112018-11-27 11:21:21 +0100181 if (sl->flags & HTX_SL_F_VER_11)
Christopher Faulet9768c262018-10-22 09:34:31 +0200182 msg->flags |= HTTP_MSGF_VER_11;
Christopher Faulet03599112018-11-27 11:21:21 +0100183 msg->flags |= HTTP_MSGF_XFER_LEN;
Christopher Faulet2a408542020-11-20 14:22:37 +0100184 if (sl->flags & HTX_SL_F_CLEN)
185 msg->flags |= HTTP_MSGF_CNT_LEN;
186 else if (sl->flags & HTX_SL_F_CHNK)
187 msg->flags |= HTTP_MSGF_TE_CHNK;
Christopher Fauletb2db4fa2018-11-27 16:51:09 +0100188 if (sl->flags & HTX_SL_F_BODYLESS)
189 msg->flags |= HTTP_MSGF_BODYLESS;
Christopher Faulet576c3582021-01-08 15:53:01 +0100190 if (sl->flags & HTX_SL_F_CONN_UPG)
191 msg->flags |= HTTP_MSGF_CONN_UPG;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200192
193 /* we can make use of server redirect on GET and HEAD */
194 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
195 s->flags |= SF_REDIRECTABLE;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100196 else if (txn->meth == HTTP_METH_OTHER && isteqi(htx_sl_req_meth(sl), ist("PRI"))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200197 /* PRI is reserved for the HTTP/2 preface */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200198 goto return_bad_req;
199 }
200
201 /*
Christopher Faulet6072beb2020-02-18 15:34:58 +0100202 * 2: check if the URI matches the monitor_uri. We have to do this for
203 * every request which gets in, because the monitor-uri is defined by
204 * the frontend. If the monitor-uri starts with a '/', the matching is
205 * done against the request's path. Otherwise, the request's uri is
206 * used. It is a workaround to let HTTP/2 health-checks work as
207 * expected.
Christopher Faulete0768eb2018-10-03 16:38:02 +0200208 */
Tim Duesterhus4b1fcaa2022-03-05 00:52:40 +0100209 if (unlikely(isttest(sess->fe->monitor_uri))) {
210 const struct ist monitor_uri = sess->fe->monitor_uri;
Amaury Denoyellec453f952021-07-06 11:40:12 +0200211 struct http_uri_parser parser = http_uri_parser_init(htx_sl_req_uri(sl));
Christopher Faulete0768eb2018-10-03 16:38:02 +0200212
Amaury Denoyelle5a9bd372021-07-06 11:23:10 +0200213 if ((istptr(monitor_uri)[0] == '/' &&
Amaury Denoyellec453f952021-07-06 11:40:12 +0200214 isteq(http_parse_path(&parser), monitor_uri)) ||
Amaury Denoyelle5a9bd372021-07-06 11:23:10 +0200215 isteq(htx_sl_req_uri(sl), monitor_uri)) {
216 /*
217 * We have found the monitor URI
218 */
219 struct acl_cond *cond;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200220
Amaury Denoyelle5a9bd372021-07-06 11:23:10 +0200221 s->flags |= SF_MONITOR;
222 _HA_ATOMIC_INC(&sess->fe->fe_counters.intercepted_req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200223
Amaury Denoyelle5a9bd372021-07-06 11:23:10 +0200224 /* Check if we want to fail this monitor request or not */
225 list_for_each_entry(cond, &sess->fe->mon_fail_cond, list) {
226 int ret = acl_exec_cond(cond, sess->fe, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200227
Amaury Denoyelle5a9bd372021-07-06 11:23:10 +0200228 ret = acl_pass(ret);
229 if (cond->pol == ACL_COND_UNLESS)
230 ret = !ret;
231
232 if (ret) {
233 /* we fail this request, let's return 503 service unavail */
234 txn->status = 503;
235 if (!(s->flags & SF_ERR_MASK))
236 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
237 goto return_prx_cond;
238 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200239 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200240
Amaury Denoyelle5a9bd372021-07-06 11:23:10 +0200241 /* nothing to fail, let's reply normally */
242 txn->status = 200;
243 if (!(s->flags & SF_ERR_MASK))
244 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
245 goto return_prx_cond;
246 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200247 }
248
249 /*
250 * 3: Maybe we have to copy the original REQURI for the logs ?
251 * Note: we cannot log anymore if the request has been
252 * classified as invalid.
253 */
254 if (unlikely(s->logs.logwait & LW_REQ)) {
255 /* we have a complete HTTP request that we must log */
256 if ((txn->uri = pool_alloc(pool_head_requri)) != NULL) {
Christopher Faulet9768c262018-10-22 09:34:31 +0200257 size_t len;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200258
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200259 len = http_fmt_req_line(sl, txn->uri, global.tune.requri_len - 1);
Christopher Faulet9768c262018-10-22 09:34:31 +0200260 txn->uri[len] = 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200261
262 if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT)))
263 s->do_log(s);
264 } else {
265 ha_alert("HTTP logging : out of memory.\n");
266 }
267 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200268
Christopher Faulete0768eb2018-10-03 16:38:02 +0200269 /* if the frontend has "option http-use-proxy-header", we'll check if
270 * we have what looks like a proxied connection instead of a connection,
271 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
272 * Note that this is *not* RFC-compliant, however browsers and proxies
273 * happen to do that despite being non-standard :-(
274 * We consider that a request not beginning with either '/' or '*' is
275 * a proxied connection, which covers both "scheme://location" and
276 * CONNECT ip:port.
277 */
278 if ((sess->fe->options2 & PR_O2_USE_PXHDR) &&
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100279 *HTX_SL_REQ_UPTR(sl) != '/' && *HTX_SL_REQ_UPTR(sl) != '*')
Christopher Faulete0768eb2018-10-03 16:38:02 +0200280 txn->flags |= TX_USE_PX_CONN;
281
Christopher Faulete0768eb2018-10-03 16:38:02 +0200282 /* 5: we may need to capture headers */
283 if (unlikely((s->logs.logwait & LW_REQHDR) && s->req_cap))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200284 http_capture_headers(htx, s->req_cap, sess->fe->req_cap);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200285
Christopher Faulete0768eb2018-10-03 16:38:02 +0200286 /* we may have to wait for the request's body */
Christopher Faulet9768c262018-10-22 09:34:31 +0200287 if (s->be->options & PR_O_WREQ_BODY)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200288 req->analysers |= AN_REQ_HTTP_BODY;
289
290 /*
291 * RFC7234#4:
292 * A cache MUST write through requests with methods
293 * that are unsafe (Section 4.2.1 of [RFC7231]) to
294 * the origin server; i.e., a cache is not allowed
295 * to generate a reply to such a request before
296 * having forwarded the request and having received
297 * a corresponding response.
298 *
299 * RFC7231#4.2.1:
300 * Of the request methods defined by this
301 * specification, the GET, HEAD, OPTIONS, and TRACE
302 * methods are defined to be safe.
303 */
304 if (likely(txn->meth == HTTP_METH_GET ||
305 txn->meth == HTTP_METH_HEAD ||
306 txn->meth == HTTP_METH_OPTIONS ||
307 txn->meth == HTTP_METH_TRACE))
308 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
309
310 /* end of job, return OK */
311 req->analysers &= ~an_bit;
312 req->analyse_exp = TICK_ETERNITY;
Christopher Faulet9768c262018-10-22 09:34:31 +0200313
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100314 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200315 return 1;
316
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200317 return_int_err:
318 txn->status = 500;
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200319 if (!(s->flags & SF_ERR_MASK))
320 s->flags |= SF_ERR_INTERNAL;
Willy Tarreau4781b152021-04-06 13:53:36 +0200321 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
William Lallemand36119de2021-03-08 15:26:48 +0100322 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200323 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200324 goto return_prx_cond;
325
Christopher Faulete0768eb2018-10-03 16:38:02 +0200326 return_bad_req:
Christopher Faulet9768c262018-10-22 09:34:31 +0200327 txn->status = 400;
Willy Tarreau4781b152021-04-06 13:53:36 +0200328 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req);
William Lallemand36119de2021-03-08 15:26:48 +0100329 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200330 _HA_ATOMIC_INC(&sess->listener->counters->failed_req);
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200331 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200332
333 return_prx_cond:
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200334 http_reply_and_close(s, txn->status, http_error_message(s));
335
Christopher Faulete0768eb2018-10-03 16:38:02 +0200336 if (!(s->flags & SF_ERR_MASK))
337 s->flags |= SF_ERR_PRXCOND;
338 if (!(s->flags & SF_FINST_MASK))
339 s->flags |= SF_FINST_R;
340
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100341 DBG_TRACE_DEVEL("leaving on error",
342 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200343 return 0;
344}
345
346
347/* This stream analyser runs all HTTP request processing which is common to
348 * frontends and backends, which means blocking ACLs, filters, connection-close,
349 * reqadd, stats and redirects. This is performed for the designated proxy.
350 * It returns 1 if the processing can continue on next analysers, or zero if it
351 * either needs more data or wants to immediately abort the request (eg: deny,
352 * error, ...).
353 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200354int http_process_req_common(struct stream *s, struct channel *req, int an_bit, struct proxy *px)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200355{
Christopher Fauletd4150ad2021-10-13 15:35:55 +0200356 struct list *def_rules, *rules;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200357 struct session *sess = s->sess;
358 struct http_txn *txn = s->txn;
359 struct http_msg *msg = &txn->req;
Christopher Fauletff2759f2018-10-24 11:13:16 +0200360 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200361 struct redirect_rule *rule;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200362 enum rule_result verdict;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200363 struct connection *conn = objt_conn(sess->origin);
364
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100365 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200366
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100367 htx = htxbuf(&req->buf);
Christopher Fauletff2759f2018-10-24 11:13:16 +0200368
Christopher Faulet1907ccc2019-04-29 13:12:02 +0200369 /* just in case we have some per-backend tracking. Only called the first
370 * execution of the analyser. */
Christopher Fauletd4150ad2021-10-13 15:35:55 +0200371 if (!s->current_rule && !s->current_rule_list)
Christopher Faulet1907ccc2019-04-29 13:12:02 +0200372 stream_inc_be_http_req_ctr(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200373
Christopher Fauletd4150ad2021-10-13 15:35:55 +0200374 def_rules = ((px->defpx && (an_bit == AN_REQ_HTTP_PROCESS_FE || px != sess->fe)) ? &px->defpx->http_req_rules : NULL);
375 rules = &px->http_req_rules;
376
Christopher Faulete0768eb2018-10-03 16:38:02 +0200377 /* evaluate http-request rules */
Christopher Fauletd4150ad2021-10-13 15:35:55 +0200378 if ((def_rules && !LIST_ISEMPTY(def_rules)) || !LIST_ISEMPTY(rules)) {
379 verdict = http_req_get_intercept_rule(px, def_rules, rules, s);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200380
381 switch (verdict) {
382 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
383 goto return_prx_yield;
384
385 case HTTP_RULE_RES_CONT:
386 case HTTP_RULE_RES_STOP: /* nothing to do */
387 break;
388
389 case HTTP_RULE_RES_DENY: /* deny or tarpit */
390 if (txn->flags & TX_CLTARPIT)
391 goto tarpit;
392 goto deny;
393
394 case HTTP_RULE_RES_ABRT: /* abort request, response already sent. Eg: auth */
395 goto return_prx_cond;
396
397 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
398 goto done;
399
400 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
401 goto return_bad_req;
Christopher Faulet3a26bee2019-12-16 12:47:40 +0100402
403 case HTTP_RULE_RES_ERROR: /* failed with a bad request */
404 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200405 }
406 }
407
Christopher Faulet18c13d32022-05-16 11:43:10 +0200408 if (px->options2 & (PR_O2_RSTRICT_REQ_HDR_NAMES_BLK|PR_O2_RSTRICT_REQ_HDR_NAMES_DEL)) {
409 verdict = http_req_restrict_header_names(s, htx, px);
410 if (verdict == HTTP_RULE_RES_DENY)
411 goto deny;
412 }
413
Christopher Faulete0768eb2018-10-03 16:38:02 +0200414 if (conn && (conn->flags & CO_FL_EARLY_DATA) &&
Olivier Houchard220a26c2020-01-23 14:57:36 +0100415 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_SSL_WAIT_HS))) {
Christopher Fauletff2759f2018-10-24 11:13:16 +0200416 struct http_hdr_ctx ctx;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200417
Christopher Fauletff2759f2018-10-24 11:13:16 +0200418 ctx.blk = NULL;
419 if (!http_find_header(htx, ist("Early-Data"), &ctx, 0)) {
420 if (unlikely(!http_add_header(htx, ist("Early-Data"), ist("1"))))
Christopher Fauletd649b572022-06-01 17:42:35 +0200421 goto return_fail_rewrite;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200422 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200423 }
424
425 /* OK at this stage, we know that the request was accepted according to
426 * the http-request rules, we can check for the stats. Note that the
427 * URI is detected *before* the req* rules in order not to be affected
428 * by a possible reqrep, while they are processed *after* so that a
429 * reqdeny can still block them. This clearly needs to change in 1.6!
430 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200431 if (!s->target && http_stats_check_uri(s, txn, px)) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200432 s->target = &http_stats_applet.obj_type;
Willy Tarreaua0b58b52022-05-27 08:33:53 +0200433 if (unlikely(!sc_applet_create(s->scb, objt_applet(s->target)))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200434 s->logs.tv_request = now;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200435 if (!(s->flags & SF_ERR_MASK))
436 s->flags |= SF_ERR_RESOURCE;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100437 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200438 }
439
440 /* parse the whole stats request and extract the relevant information */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200441 http_handle_stats(s, req);
Christopher Fauletd4150ad2021-10-13 15:35:55 +0200442 verdict = http_req_get_intercept_rule(px, NULL, &px->uri_auth->http_req_rules, s);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200443 /* not all actions implemented: deny, allow, auth */
444
445 if (verdict == HTTP_RULE_RES_DENY) /* stats http-request deny */
446 goto deny;
447
448 if (verdict == HTTP_RULE_RES_ABRT) /* stats auth / stats http-request auth */
449 goto return_prx_cond;
Christopher Faulet3a26bee2019-12-16 12:47:40 +0100450
451 if (verdict == HTTP_RULE_RES_BADREQ) /* failed with a bad request */
452 goto return_bad_req;
453
454 if (verdict == HTTP_RULE_RES_ERROR) /* failed with a bad request */
455 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200456 }
457
Christopher Faulet2571bc62019-03-01 11:44:26 +0100458 /* Proceed with the applets now. */
459 if (unlikely(objt_applet(s->target))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200460 if (sess->fe == s->be) /* report it if the request was intercepted by the frontend */
Willy Tarreau4781b152021-04-06 13:53:36 +0200461 _HA_ATOMIC_INC(&sess->fe->fe_counters.intercepted_req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200462
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200463 if (http_handle_expect_hdr(s, htx, msg) == -1)
Christopher Fauletb8a53712019-12-16 11:29:38 +0100464 goto return_int_err;
Christopher Fauletbcf242a2019-03-01 11:36:26 +0100465
Christopher Faulete0768eb2018-10-03 16:38:02 +0200466 if (!(s->flags & SF_ERR_MASK)) // this is not really an error but it is
467 s->flags |= SF_ERR_LOCAL; // to mark that it comes from the proxy
468 if (!(s->flags & SF_FINST_MASK))
469 s->flags |= SF_FINST_R;
470
Christopher Fauletc2ac5e42021-03-08 18:20:09 +0100471 if (HAS_FILTERS(s))
472 req->analysers |= AN_REQ_FLT_HTTP_HDRS;
473
Christopher Faulete0768eb2018-10-03 16:38:02 +0200474 /* enable the minimally required analyzers to handle keep-alive and compression on the HTTP response */
475 req->analysers &= (AN_REQ_HTTP_BODY | AN_REQ_FLT_HTTP_HDRS | AN_REQ_FLT_END);
476 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
477 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Christopher Fauletbcf242a2019-03-01 11:36:26 +0100478
479 req->flags |= CF_SEND_DONTWAIT;
480 s->flags |= SF_ASSIGNED;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200481 goto done;
482 }
483
484 /* check whether we have some ACLs set to redirect this request */
485 list_for_each_entry(rule, &px->redirect_rules, list) {
486 if (rule->cond) {
487 int ret;
488
489 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
490 ret = acl_pass(ret);
491 if (rule->cond->pol == ACL_COND_UNLESS)
492 ret = !ret;
493 if (!ret)
494 continue;
495 }
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200496 if (!http_apply_redirect_rule(rule, s, txn))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100497 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200498 goto done;
499 }
500
501 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
502 * If this happens, then the data will not come immediately, so we must
503 * send all what we have without waiting. Note that due to the small gain
504 * in waiting for the body of the request, it's easier to simply put the
505 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
506 * itself once used.
507 */
508 req->flags |= CF_SEND_DONTWAIT;
509
510 done: /* done with this analyser, continue with next ones that the calling
511 * points will have set, if any.
512 */
513 req->analyse_exp = TICK_ETERNITY;
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +0500514 done_without_exp: /* done with this analyser, but don't reset the analyse_exp. */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200515 req->analysers &= ~an_bit;
Christopher Fauletd4150ad2021-10-13 15:35:55 +0200516 s->current_rule = s->current_rule_list = NULL;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100517 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200518 return 1;
519
520 tarpit:
521 /* Allow cookie logging
522 */
523 if (s->be->cookie_name || sess->fe->capture_name)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200524 http_manage_client_side_cookies(s, req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200525
526 /* When a connection is tarpitted, we use the tarpit timeout,
527 * which may be the same as the connect timeout if unspecified.
528 * If unset, then set it to zero because we really want it to
529 * eventually expire. We build the tarpit as an analyser.
530 */
Christopher Faulet202c6ce2019-01-07 14:57:35 +0100531 channel_htx_erase(&s->req, htx);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200532
533 /* wipe the request out so that we can drop the connection early
534 * if the client closes first.
535 */
536 channel_dont_connect(req);
537
Christopher Faulete0768eb2018-10-03 16:38:02 +0200538 req->analysers &= AN_REQ_FLT_END; /* remove switching rules etc... */
539 req->analysers |= AN_REQ_HTTP_TARPIT;
540 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
541 if (!req->analyse_exp)
542 req->analyse_exp = tick_add(now_ms, 0);
543 stream_inc_http_err_ctr(s);
Willy Tarreau4781b152021-04-06 13:53:36 +0200544 _HA_ATOMIC_INC(&sess->fe->fe_counters.denied_req);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100545 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau4781b152021-04-06 13:53:36 +0200546 _HA_ATOMIC_INC(&s->be->be_counters.denied_req);
William Lallemand36119de2021-03-08 15:26:48 +0100547 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200548 _HA_ATOMIC_INC(&sess->listener->counters->denied_req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200549 goto done_without_exp;
550
551 deny: /* this request was blocked (denied) */
552
553 /* Allow cookie logging
554 */
555 if (s->be->cookie_name || sess->fe->capture_name)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200556 http_manage_client_side_cookies(s, req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200557
Christopher Faulete0768eb2018-10-03 16:38:02 +0200558 s->logs.tv_request = now;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200559 stream_inc_http_err_ctr(s);
Willy Tarreau4781b152021-04-06 13:53:36 +0200560 _HA_ATOMIC_INC(&sess->fe->fe_counters.denied_req);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100561 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau4781b152021-04-06 13:53:36 +0200562 _HA_ATOMIC_INC(&s->be->be_counters.denied_req);
William Lallemand36119de2021-03-08 15:26:48 +0100563 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200564 _HA_ATOMIC_INC(&sess->listener->counters->denied_req);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100565 goto return_prx_err;
566
Christopher Fauletd649b572022-06-01 17:42:35 +0200567 return_fail_rewrite:
568 if (!(s->flags & SF_ERR_MASK))
569 s->flags |= SF_ERR_PRXCOND;
570 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_rewrites);
571 if (s->flags & SF_BE_ASSIGNED)
572 _HA_ATOMIC_INC(&s->be->be_counters.failed_rewrites);
573 if (sess->listener && sess->listener->counters)
574 _HA_ATOMIC_INC(&sess->listener->counters->failed_rewrites);
575 if (objt_server(s->target))
576 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_rewrites);
577 /* fall through */
578
Christopher Fauletb8a53712019-12-16 11:29:38 +0100579 return_int_err:
580 txn->status = 500;
581 if (!(s->flags & SF_ERR_MASK))
582 s->flags |= SF_ERR_INTERNAL;
Willy Tarreau4781b152021-04-06 13:53:36 +0200583 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100584 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau4781b152021-04-06 13:53:36 +0200585 _HA_ATOMIC_INC(&s->be->be_counters.internal_errors);
William Lallemand36119de2021-03-08 15:26:48 +0100586 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200587 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100588 goto return_prx_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200589
590 return_bad_req:
Christopher Faulete0768eb2018-10-03 16:38:02 +0200591 txn->status = 400;
Willy Tarreau4781b152021-04-06 13:53:36 +0200592 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req);
William Lallemand36119de2021-03-08 15:26:48 +0100593 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200594 _HA_ATOMIC_INC(&sess->listener->counters->failed_req);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100595 /* fall through */
596
597 return_prx_err:
598 http_reply_and_close(s, txn->status, http_error_message(s));
599 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200600
601 return_prx_cond:
602 if (!(s->flags & SF_ERR_MASK))
603 s->flags |= SF_ERR_PRXCOND;
604 if (!(s->flags & SF_FINST_MASK))
605 s->flags |= SF_FINST_R;
606
607 req->analysers &= AN_REQ_FLT_END;
608 req->analyse_exp = TICK_ETERNITY;
Christopher Fauletd4150ad2021-10-13 15:35:55 +0200609 s->current_rule = s->current_rule_list = NULL;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100610 DBG_TRACE_DEVEL("leaving on error",
611 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200612 return 0;
613
614 return_prx_yield:
615 channel_dont_connect(req);
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100616 DBG_TRACE_DEVEL("waiting for more data",
617 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200618 return 0;
619}
620
621/* This function performs all the processing enabled for the current request.
622 * It returns 1 if the processing can continue on next analysers, or zero if it
623 * needs more data, encounters an error, or wants to immediately abort the
624 * request. It relies on buffers flags, and updates s->req.analysers.
625 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200626int http_process_request(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200627{
628 struct session *sess = s->sess;
629 struct http_txn *txn = s->txn;
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200630 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200631 struct connection *cli_conn = objt_conn(strm_sess(s)->origin);
632
Christopher Faulet8bebd2f2020-10-06 17:54:56 +0200633 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200634
635 /*
636 * Right now, we know that we have processed the entire headers
637 * and that unwanted requests have been filtered out. We can do
638 * whatever we want with the remaining request. Also, now we
639 * may have separate values for ->fe, ->be.
640 */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100641 htx = htxbuf(&req->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200642
643 /*
Christopher Faulete0768eb2018-10-03 16:38:02 +0200644 * 7: Now we can work with the cookies.
645 * Note that doing so might move headers in the request, but
646 * the fields will stay coherent and the URI will not move.
647 * This should only be performed in the backend.
648 */
649 if (s->be->cookie_name || sess->fe->capture_name)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200650 http_manage_client_side_cookies(s, req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200651
Tim Duesterhus2825b4b2020-02-28 15:13:34 +0100652 /* 8: Generate unique ID if a "unique-id-format" is defined.
653 *
654 * A unique ID is generated even when it is not sent to ensure that the ID can make use of
655 * fetches only available in the HTTP request processing stage.
656 */
657 if (!LIST_ISEMPTY(&sess->fe->format_unique_id)) {
Tim Duesterhusa17e6622020-03-05 20:19:02 +0100658 struct ist unique_id = stream_generate_unique_id(s, &sess->fe->format_unique_id);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200659
Tim Duesterhusa17e6622020-03-05 20:19:02 +0100660 if (!isttest(unique_id)) {
Christopher Fauletb8a53712019-12-16 11:29:38 +0100661 if (!(s->flags & SF_ERR_MASK))
662 s->flags |= SF_ERR_RESOURCE;
663 goto return_int_err;
664 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200665
Tim Duesterhus2825b4b2020-02-28 15:13:34 +0100666 /* send unique ID if a "unique-id-header" is defined */
Tim Duesterhus0643b0e2020-03-05 17:56:35 +0100667 if (isttest(sess->fe->header_unique_id) &&
Tim Duesterhus7ad27d42022-05-18 00:22:15 +0200668 unlikely(!http_add_header(htx, sess->fe->header_unique_id, unique_id)))
Christopher Fauletd649b572022-06-01 17:42:35 +0200669 goto return_fail_rewrite;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200670 }
671
672 /*
673 * 9: add X-Forwarded-For if either the frontend or the backend
674 * asks for it.
675 */
676 if ((sess->fe->options | s->be->options) & PR_O_FWDFOR) {
Willy Tarreaud68ff012022-05-27 08:57:21 +0200677 const struct sockaddr_storage *src = sc_src(s->scf);
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200678 struct http_hdr_ctx ctx = { .blk = NULL };
Tim Duesterhusb50ab842022-03-05 00:52:41 +0100679 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 +0200680
Christopher Faulete0768eb2018-10-03 16:38:02 +0200681 if (!((sess->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200682 http_find_header(htx, hdr, &ctx, 0)) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200683 /* The header is set to be added only if none is present
684 * and we found it, so don't do anything.
685 */
686 }
Christopher Faulet8a104ba2021-10-25 07:41:30 +0200687 else if (src && src->ss_family == AF_INET) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200688 /* Add an X-Forwarded-For header unless the source IP is
689 * in the 'except' network range.
690 */
Christopher Faulet8a104ba2021-10-25 07:41:30 +0200691 if (ipcmp2net(src, &sess->fe->except_xff_net) &&
692 ipcmp2net(src, &s->be->except_xff_net)) {
693 unsigned char *pn = (unsigned char *)&((struct sockaddr_in *)src)->sin_addr;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200694
695 /* Note: we rely on the backend to get the header name to be used for
696 * x-forwarded-for, because the header is really meant for the backends.
697 * However, if the backend did not specify any option, we have to rely
698 * on the frontend's header name.
699 */
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200700 chunk_printf(&trash, "%d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
701 if (unlikely(!http_add_header(htx, hdr, ist2(trash.area, trash.data))))
Christopher Fauletd649b572022-06-01 17:42:35 +0200702 goto return_fail_rewrite;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200703 }
704 }
Christopher Faulet8a104ba2021-10-25 07:41:30 +0200705 else if (src && src->ss_family == AF_INET6) {
Christopher Faulet5d1def62021-02-26 09:19:15 +0100706 /* Add an X-Forwarded-For header unless the source IP is
707 * in the 'except' network range.
Christopher Faulete0768eb2018-10-03 16:38:02 +0200708 */
Christopher Faulet8a104ba2021-10-25 07:41:30 +0200709 if (ipcmp2net(src, &sess->fe->except_xff_net) &&
710 ipcmp2net(src, &s->be->except_xff_net)) {
Christopher Faulet5d1def62021-02-26 09:19:15 +0100711 char pn[INET6_ADDRSTRLEN];
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200712
Christopher Faulet5d1def62021-02-26 09:19:15 +0100713 inet_ntop(AF_INET6,
Christopher Faulet8a104ba2021-10-25 07:41:30 +0200714 (const void *)&((struct sockaddr_in6 *)(src))->sin6_addr,
Christopher Faulet5d1def62021-02-26 09:19:15 +0100715 pn, sizeof(pn));
Christopher Faulete0768eb2018-10-03 16:38:02 +0200716
Christopher Faulet5d1def62021-02-26 09:19:15 +0100717 /* Note: we rely on the backend to get the header name to be used for
718 * x-forwarded-for, because the header is really meant for the backends.
719 * However, if the backend did not specify any option, we have to rely
720 * on the frontend's header name.
721 */
722 chunk_printf(&trash, "%s", pn);
723 if (unlikely(!http_add_header(htx, hdr, ist2(trash.area, trash.data))))
Christopher Fauletd649b572022-06-01 17:42:35 +0200724 goto return_fail_rewrite;
Christopher Faulet5d1def62021-02-26 09:19:15 +0100725 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200726 }
727 }
728
729 /*
730 * 10: add X-Original-To if either the frontend or the backend
731 * asks for it.
732 */
733 if ((sess->fe->options | s->be->options) & PR_O_ORGTO) {
Willy Tarreaud68ff012022-05-27 08:57:21 +0200734 const struct sockaddr_storage *dst = sc_dst(s->scf);
Tim Duesterhuse502c3e2022-03-05 00:52:42 +0100735 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 +0200736
Christopher Faulet8a104ba2021-10-25 07:41:30 +0200737 if (dst && dst->ss_family == AF_INET) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200738 /* Add an X-Original-To header unless the destination IP is
739 * in the 'except' network range.
740 */
Christopher Faulet8a104ba2021-10-25 07:41:30 +0200741 if (ipcmp2net(dst, &sess->fe->except_xot_net) &&
742 ipcmp2net(dst, &s->be->except_xot_net)) {
743 unsigned char *pn = (unsigned char *)&((struct sockaddr_in *)dst)->sin_addr;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200744
745 /* Note: we rely on the backend to get the header name to be used for
746 * x-original-to, because the header is really meant for the backends.
747 * However, if the backend did not specify any option, we have to rely
748 * on the frontend's header name.
749 */
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200750 chunk_printf(&trash, "%d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
751 if (unlikely(!http_add_header(htx, hdr, ist2(trash.area, trash.data))))
Christopher Fauletd649b572022-06-01 17:42:35 +0200752 goto return_fail_rewrite;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200753 }
754 }
Christopher Faulet8a104ba2021-10-25 07:41:30 +0200755 else if (dst && dst->ss_family == AF_INET6) {
Christopher Faulet5d1def62021-02-26 09:19:15 +0100756 /* Add an X-Original-To header unless the source IP is
757 * in the 'except' network range.
758 */
Christopher Faulet8a104ba2021-10-25 07:41:30 +0200759 if (ipcmp2net(dst, &sess->fe->except_xot_net) &&
760 ipcmp2net(dst, &s->be->except_xot_net)) {
Christopher Faulet5d1def62021-02-26 09:19:15 +0100761 char pn[INET6_ADDRSTRLEN];
762
763 inet_ntop(AF_INET6,
Christopher Faulet8a104ba2021-10-25 07:41:30 +0200764 (const void *)&((struct sockaddr_in6 *)dst)->sin6_addr,
Christopher Faulet5d1def62021-02-26 09:19:15 +0100765 pn, sizeof(pn));
766
767 /* Note: we rely on the backend to get the header name to be used for
768 * x-forwarded-for, because the header is really meant for the backends.
769 * However, if the backend did not specify any option, we have to rely
770 * on the frontend's header name.
771 */
772 chunk_printf(&trash, "%s", pn);
773 if (unlikely(!http_add_header(htx, hdr, ist2(trash.area, trash.data))))
Christopher Fauletd649b572022-06-01 17:42:35 +0200774 goto return_fail_rewrite;
Christopher Faulet5d1def62021-02-26 09:19:15 +0100775 }
776 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200777 }
778
Christopher Fauletc2ac5e42021-03-08 18:20:09 +0100779 /* Filter the request headers if there are filters attached to the
780 * stream.
781 */
782 if (HAS_FILTERS(s))
783 req->analysers |= AN_REQ_FLT_HTTP_HDRS;
784
Christopher Faulete0768eb2018-10-03 16:38:02 +0200785 /* If we have no server assigned yet and we're balancing on url_param
786 * with a POST request, we may be interested in checking the body for
787 * that parameter. This will be done in another analyser.
788 */
789 if (!(s->flags & (SF_ASSIGNED|SF_DIRECT)) &&
Willy Tarreau089eaa02019-01-14 15:17:46 +0100790 s->txn->meth == HTTP_METH_POST &&
791 (s->be->lbprm.algo & BE_LB_ALGO) == BE_LB_ALGO_PH) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200792 channel_dont_connect(req);
793 req->analysers |= AN_REQ_HTTP_BODY;
794 }
795
796 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
797 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau1a18b542018-12-11 16:37:42 +0100798
Christopher Faulete0768eb2018-10-03 16:38:02 +0200799 /* We expect some data from the client. Unless we know for sure
800 * we already have a full request, we have to re-enable quick-ack
801 * in case we previously disabled it, otherwise we might cause
802 * the client to delay further data.
803 */
William Lallemand36119de2021-03-08 15:26:48 +0100804 if ((sess->listener && (sess->listener->options & LI_O_NOQUICKACK)) && !(htx->flags & HTX_FL_EOM))
Willy Tarreau1a18b542018-12-11 16:37:42 +0100805 conn_set_quickack(cli_conn, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200806
807 /*************************************************************
808 * OK, that's finished for the headers. We have done what we *
809 * could. Let's switch to the DATA state. *
810 ************************************************************/
811 req->analyse_exp = TICK_ETERNITY;
812 req->analysers &= ~an_bit;
813
814 s->logs.tv_request = now;
815 /* OK let's go on with the BODY now */
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100816 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200817 return 1;
818
Christopher Fauletd649b572022-06-01 17:42:35 +0200819 return_fail_rewrite:
820 if (!(s->flags & SF_ERR_MASK))
821 s->flags |= SF_ERR_PRXCOND;
822 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_rewrites);
823 if (s->flags & SF_BE_ASSIGNED)
824 _HA_ATOMIC_INC(&s->be->be_counters.failed_rewrites);
825 if (sess->listener && sess->listener->counters)
826 _HA_ATOMIC_INC(&sess->listener->counters->failed_rewrites);
827 if (objt_server(s->target))
828 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_rewrites);
829 /* fall through */
830
Christopher Fauletb8a53712019-12-16 11:29:38 +0100831 return_int_err:
832 txn->status = 500;
833 if (!(s->flags & SF_ERR_MASK))
834 s->flags |= SF_ERR_INTERNAL;
Willy Tarreau4781b152021-04-06 13:53:36 +0200835 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100836 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau4781b152021-04-06 13:53:36 +0200837 _HA_ATOMIC_INC(&s->be->be_counters.internal_errors);
William Lallemand36119de2021-03-08 15:26:48 +0100838 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200839 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100840
Christopher Fauletb8a53712019-12-16 11:29:38 +0100841 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +0200842
843 if (!(s->flags & SF_ERR_MASK))
844 s->flags |= SF_ERR_PRXCOND;
845 if (!(s->flags & SF_FINST_MASK))
846 s->flags |= SF_FINST_R;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100847
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100848 DBG_TRACE_DEVEL("leaving on error",
849 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200850 return 0;
851}
852
853/* This function is an analyser which processes the HTTP tarpit. It always
854 * returns zero, at the beginning because it prevents any other processing
855 * from occurring, and at the end because it terminates the request.
856 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200857int http_process_tarpit(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200858{
859 struct http_txn *txn = s->txn;
860
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100861 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, &txn->req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200862 /* This connection is being tarpitted. The CLIENT side has
863 * already set the connect expiration date to the right
864 * timeout. We just have to check that the client is still
865 * there and that the timeout has not expired.
866 */
867 channel_dont_connect(req);
868 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100869 !tick_is_expired(req->analyse_exp, now_ms)) {
Christopher Fauletb0c87f12021-10-29 14:37:07 +0200870 /* Be sure to drain all data from the request channel */
871 channel_htx_erase(req, htxbuf(&req->buf));
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100872 DBG_TRACE_DEVEL("waiting for tarpit timeout expiry",
873 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200874 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100875 }
876
Christopher Faulete0768eb2018-10-03 16:38:02 +0200877
878 /* We will set the queue timer to the time spent, just for
879 * logging purposes. We fake a 500 server error, so that the
880 * attacker will not suspect his connection has been tarpitted.
881 * It will not cause trouble to the logs because we can exclude
882 * the tarpitted connections by filtering on the 'PT' status flags.
883 */
884 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
885
Christopher Faulet8dfeccf2020-05-15 14:16:29 +0200886 http_reply_and_close(s, txn->status, (!(req->flags & CF_READ_ERROR) ? http_error_message(s) : NULL));
Christopher Faulet5cb513a2020-05-13 17:56:56 +0200887
Christopher Faulete0768eb2018-10-03 16:38:02 +0200888 if (!(s->flags & SF_ERR_MASK))
889 s->flags |= SF_ERR_PRXCOND;
890 if (!(s->flags & SF_FINST_MASK))
891 s->flags |= SF_FINST_T;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100892
893 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200894 return 0;
895}
896
897/* This function is an analyser which waits for the HTTP request body. It waits
898 * for either the buffer to be full, or the full advertised contents to have
899 * reached the buffer. It must only be called after the standard HTTP request
900 * processing has occurred, because it expects the request to be parsed and will
901 * look for the Expect header. It may send a 100-Continue interim response. It
902 * takes in input any state starting from HTTP_MSG_BODY and leaves with one of
903 * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it
904 * needs to read more data, or 1 once it has completed its analysis.
905 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200906int http_wait_for_request_body(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200907{
908 struct session *sess = s->sess;
909 struct http_txn *txn = s->txn;
910 struct http_msg *msg = &s->txn->req;
911
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100912 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Fauletf76ebe82018-10-24 11:16:22 +0200913
Christopher Fauletf76ebe82018-10-24 11:16:22 +0200914
Christopher Faulet021a8e42021-03-29 10:46:38 +0200915 switch (http_wait_for_msg_body(s, req, s->be->timeout.httpreq, 0)) {
916 case HTTP_RULE_RES_CONT:
917 goto http_end;
918 case HTTP_RULE_RES_YIELD:
919 goto missing_data_or_waiting;
920 case HTTP_RULE_RES_BADREQ:
Willy Tarreau4236f032019-03-05 10:43:32 +0100921 goto return_bad_req;
Christopher Faulet021a8e42021-03-29 10:46:38 +0200922 case HTTP_RULE_RES_ERROR:
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200923 goto return_int_err;
Christopher Faulet021a8e42021-03-29 10:46:38 +0200924 case HTTP_RULE_RES_ABRT:
Christopher Fauletb8a53712019-12-16 11:29:38 +0100925 goto return_prx_cond;
Christopher Faulet021a8e42021-03-29 10:46:38 +0200926 default:
927 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200928 }
929
930 http_end:
931 /* The situation will not evolve, so let's give up on the analysis. */
932 s->logs.tv_request = now; /* update the request timer to reflect full request */
933 req->analysers &= ~an_bit;
934 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100935 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200936 return 1;
937
Christopher Faulet021a8e42021-03-29 10:46:38 +0200938 missing_data_or_waiting:
939 channel_dont_connect(req);
940 DBG_TRACE_DEVEL("waiting for more data",
941 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
942 return 0;
943
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200944 return_int_err:
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200945 txn->status = 500;
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200946 if (!(s->flags & SF_ERR_MASK))
947 s->flags |= SF_ERR_INTERNAL;
Willy Tarreau4781b152021-04-06 13:53:36 +0200948 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100949 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau4781b152021-04-06 13:53:36 +0200950 _HA_ATOMIC_INC(&s->be->be_counters.internal_errors);
William Lallemand36119de2021-03-08 15:26:48 +0100951 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200952 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Faulet021a8e42021-03-29 10:46:38 +0200953 goto return_prx_err;
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200954
Christopher Faulete0768eb2018-10-03 16:38:02 +0200955 return_bad_req: /* let's centralize all bad requests */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200956 txn->status = 400;
Willy Tarreau4781b152021-04-06 13:53:36 +0200957 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req);
William Lallemand36119de2021-03-08 15:26:48 +0100958 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200959 _HA_ATOMIC_INC(&sess->listener->counters->failed_req);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100960 /* fall through */
961
Christopher Faulet021a8e42021-03-29 10:46:38 +0200962 return_prx_err:
Christopher Fauletb8a53712019-12-16 11:29:38 +0100963 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet021a8e42021-03-29 10:46:38 +0200964 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200965
Christopher Faulet021a8e42021-03-29 10:46:38 +0200966 return_prx_cond:
Christopher Faulete0768eb2018-10-03 16:38:02 +0200967 if (!(s->flags & SF_ERR_MASK))
968 s->flags |= SF_ERR_PRXCOND;
969 if (!(s->flags & SF_FINST_MASK))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100970 s->flags |= (msg->msg_state < HTTP_MSG_DATA ? SF_FINST_R : SF_FINST_D);
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200971
Christopher Faulete0768eb2018-10-03 16:38:02 +0200972 req->analysers &= AN_REQ_FLT_END;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100973 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100974 DBG_TRACE_DEVEL("leaving on error",
975 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200976 return 0;
977}
978
979/* This function is an analyser which forwards request body (including chunk
980 * sizes if any). It is called as soon as we must forward, even if we forward
981 * zero byte. The only situation where it must not be called is when we're in
982 * tunnel mode and we want to forward till the close. It's used both to forward
983 * remaining data and to resync after end of body. It expects the msg_state to
984 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
985 * read more data, or 1 once we can go on with next request or end the stream.
986 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
987 * bytes of pending data + the headers if not already done.
988 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200989int http_request_forward_body(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200990{
991 struct session *sess = s->sess;
992 struct http_txn *txn = s->txn;
Christopher Faulet9768c262018-10-22 09:34:31 +0200993 struct http_msg *msg = &txn->req;
994 struct htx *htx;
Christopher Faulet93e02d82019-03-08 14:18:50 +0100995 short status = 0;
Christopher Fauletaed82cf2018-11-30 22:22:32 +0100996 int ret;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200997
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100998 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200999
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001000 htx = htxbuf(&req->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001001
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001002 if (htx->flags & HTX_FL_PARSING_ERROR)
1003 goto return_bad_req;
1004 if (htx->flags & HTX_FL_PROCESSING_ERROR)
1005 goto return_int_err;
1006
Christopher Faulete0768eb2018-10-03 16:38:02 +02001007 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
1008 ((req->flags & CF_SHUTW) && (req->to_forward || co_data(req)))) {
1009 /* Output closed while we were sending data. We must abort and
1010 * wake the other side up.
Christopher Fauletf506d962021-04-27 10:56:28 +02001011 *
1012 * If we have finished to send the request and the response is
1013 * still in progress, don't catch write error on the request
1014 * side if it is in fact a read error on the server side.
Christopher Faulete0768eb2018-10-03 16:38:02 +02001015 */
Christopher Fauletf506d962021-04-27 10:56:28 +02001016 if (msg->msg_state == HTTP_MSG_DONE && (s->res.flags & CF_READ_ERROR) && s->res.analysers)
1017 return 0;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001018
Olivier Houchard29cac3c2019-07-12 15:48:58 +02001019 /* Don't abort yet if we had L7 retries activated and it
1020 * was a write error, we may recover.
1021 */
1022 if (!(req->flags & (CF_READ_ERROR | CF_READ_TIMEOUT)) &&
Christopher Faulete05bf9e2022-03-29 15:23:40 +02001023 (txn->flags & TX_L7_RETRY)) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001024 DBG_TRACE_DEVEL("leaving on L7 retry",
1025 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Olivier Houchard29cac3c2019-07-12 15:48:58 +02001026 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001027 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001028 msg->msg_state = HTTP_MSG_ERROR;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001029 http_end_request(s);
1030 http_end_response(s);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001031 DBG_TRACE_DEVEL("leaving on error",
1032 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001033 return 1;
1034 }
1035
1036 /* Note that we don't have to send 100-continue back because we don't
1037 * need the data to complete our job, and it's up to the server to
1038 * decide whether to return 100, 417 or anything else in return of
1039 * an "Expect: 100-continue" header.
1040 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001041 if (msg->msg_state == HTTP_MSG_BODY)
1042 msg->msg_state = HTTP_MSG_DATA;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001043
Christopher Faulete0768eb2018-10-03 16:38:02 +02001044 /* in most states, we should abort in case of early close */
1045 channel_auto_close(req);
1046
1047 if (req->to_forward) {
Christopher Faulet66af0b22019-03-22 14:54:52 +01001048 if (req->to_forward == CHN_INFINITE_FORWARD) {
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001049 if (req->flags & CF_EOI)
1050 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet66af0b22019-03-22 14:54:52 +01001051 }
1052 else {
1053 /* We can't process the buffer's contents yet */
1054 req->flags |= CF_WAKE_WRITE;
1055 goto missing_data_or_waiting;
1056 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001057 }
1058
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001059 if (msg->msg_state >= HTTP_MSG_ENDING)
1060 goto ending;
1061
1062 if (txn->meth == HTTP_METH_CONNECT) {
1063 msg->msg_state = HTTP_MSG_ENDING;
1064 goto ending;
1065 }
1066
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001067 /* Forward input data. We get it by removing all outgoing data not
1068 * forwarded yet from HTX data size. If there are some data filters, we
1069 * let them decide the amount of data to forward.
Christopher Faulet9768c262018-10-22 09:34:31 +02001070 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001071 if (HAS_REQ_DATA_FILTERS(s)) {
1072 ret = flt_http_payload(s, msg, htx->data);
1073 if (ret < 0)
1074 goto return_bad_req;
Christopher Faulet421e7692019-06-13 11:16:45 +02001075 c_adv(req, ret);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001076 }
1077 else {
Christopher Faulet421e7692019-06-13 11:16:45 +02001078 c_adv(req, htx->data - co_data(req));
Christopher Faulet66af0b22019-03-22 14:54:52 +01001079 if (msg->flags & HTTP_MSGF_XFER_LEN)
1080 channel_htx_forward_forever(req, htx);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001081 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001082
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001083 if (htx->data != co_data(req))
1084 goto missing_data_or_waiting;
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001085
Christopher Faulet9768c262018-10-22 09:34:31 +02001086 /* Check if the end-of-message is reached and if so, switch the message
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001087 * in HTTP_MSG_ENDING state. Then if all data was marked to be
1088 * forwarded, set the state to HTTP_MSG_DONE.
Christopher Faulet9768c262018-10-22 09:34:31 +02001089 */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001090 if (!(htx->flags & HTX_FL_EOM))
Christopher Faulet9768c262018-10-22 09:34:31 +02001091 goto missing_data_or_waiting;
1092
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001093 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet9768c262018-10-22 09:34:31 +02001094
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001095 ending:
Christopher Faulet2151cdd2020-07-22 16:34:59 +02001096 req->flags &= ~CF_EXPECT_MORE; /* no more data are expected */
1097
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001098 /* other states, ENDING...TUNNEL */
1099 if (msg->msg_state >= HTTP_MSG_DONE)
1100 goto done;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001101
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001102 if (HAS_REQ_DATA_FILTERS(s)) {
1103 ret = flt_http_end(s, msg);
1104 if (ret <= 0) {
1105 if (!ret)
1106 goto missing_data_or_waiting;
1107 goto return_bad_req;
1108 }
1109 }
1110
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001111 if (txn->meth == HTTP_METH_CONNECT)
1112 msg->msg_state = HTTP_MSG_TUNNEL;
1113 else {
1114 msg->msg_state = HTTP_MSG_DONE;
1115 req->to_forward = 0;
1116 }
1117
1118 done:
1119 /* we don't want to forward closes on DONE except in tunnel mode. */
1120 if (!(txn->flags & TX_CON_WANT_TUN))
1121 channel_dont_close(req);
1122
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001123 http_end_request(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001124 if (!(req->analysers & an_bit)) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001125 http_end_response(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001126 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
1127 if (req->flags & CF_SHUTW) {
1128 /* request errors are most likely due to the
1129 * server aborting the transfer. */
Christopher Faulet93e02d82019-03-08 14:18:50 +01001130 goto return_srv_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001131 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001132 goto return_bad_req;
1133 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001134 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001135 return 1;
1136 }
1137
1138 /* If "option abortonclose" is set on the backend, we want to monitor
1139 * the client's connection and forward any shutdown notification to the
1140 * server, which will decide whether to close or to go on processing the
1141 * request. We only do that in tunnel mode, and not in other modes since
1142 * it can be abused to exhaust source ports. */
Christopher Faulet769d0e92019-03-22 14:23:18 +01001143 if (s->be->options & PR_O_ABRT_CLOSE) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001144 channel_auto_read(req);
Christopher Fauletc41547b2019-07-16 14:32:23 +02001145 if ((req->flags & (CF_SHUTR|CF_READ_NULL)) && !(txn->flags & TX_CON_WANT_TUN))
Willy Tarreaucb041662022-05-17 19:44:42 +02001146 s->scb->flags |= SC_FL_NOLINGER;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001147 channel_auto_close(req);
1148 }
1149 else if (s->txn->meth == HTTP_METH_POST) {
1150 /* POST requests may require to read extra CRLF sent by broken
1151 * browsers and which could cause an RST to be sent upon close
1152 * on some systems (eg: Linux). */
1153 channel_auto_read(req);
1154 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001155 DBG_TRACE_DEVEL("waiting for the end of the HTTP txn",
1156 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001157 return 0;
1158
1159 missing_data_or_waiting:
1160 /* stop waiting for data if the input is closed before the end */
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001161 if (msg->msg_state < HTTP_MSG_ENDING && req->flags & CF_SHUTR)
Christopher Faulet93e02d82019-03-08 14:18:50 +01001162 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001163
1164 waiting:
1165 /* waiting for the last bits to leave the buffer */
1166 if (req->flags & CF_SHUTW)
Christopher Faulet93e02d82019-03-08 14:18:50 +01001167 goto return_srv_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001168
1169 /* When TE: chunked is used, we need to get there again to parse remaining
1170 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
1171 * And when content-length is used, we never want to let the possible
1172 * shutdown be forwarded to the other side, as the state machine will
1173 * take care of it once the client responds. It's also important to
1174 * prevent TIME_WAITs from accumulating on the backend side, and for
1175 * HTTP/2 where the last frame comes with a shutdown.
1176 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001177 if (msg->flags & HTTP_MSGF_XFER_LEN)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001178 channel_dont_close(req);
1179
1180 /* We know that more data are expected, but we couldn't send more that
1181 * what we did. So we always set the CF_EXPECT_MORE flag so that the
1182 * system knows it must not set a PUSH on this first part. Interactive
1183 * modes are already handled by the stream sock layer. We must not do
1184 * this in content-length mode because it could present the MSG_MORE
1185 * flag with the last block of forwarded data, which would cause an
1186 * additional delay to be observed by the receiver.
1187 */
Christopher Faulet2151cdd2020-07-22 16:34:59 +02001188 if (HAS_REQ_DATA_FILTERS(s))
Christopher Faulete0768eb2018-10-03 16:38:02 +02001189 req->flags |= CF_EXPECT_MORE;
1190
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001191 DBG_TRACE_DEVEL("waiting for more data to forward",
1192 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001193 return 0;
1194
Christopher Faulet93e02d82019-03-08 14:18:50 +01001195 return_cli_abort:
Willy Tarreau4781b152021-04-06 13:53:36 +02001196 _HA_ATOMIC_INC(&sess->fe->fe_counters.cli_aborts);
1197 _HA_ATOMIC_INC(&s->be->be_counters.cli_aborts);
William Lallemand36119de2021-03-08 15:26:48 +01001198 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001199 _HA_ATOMIC_INC(&sess->listener->counters->cli_aborts);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001200 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001201 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.cli_aborts);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001202 if (!(s->flags & SF_ERR_MASK))
1203 s->flags |= SF_ERR_CLICL;
1204 status = 400;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001205 goto return_prx_cond;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001206
1207 return_srv_abort:
Willy Tarreau4781b152021-04-06 13:53:36 +02001208 _HA_ATOMIC_INC(&sess->fe->fe_counters.srv_aborts);
1209 _HA_ATOMIC_INC(&s->be->be_counters.srv_aborts);
William Lallemand36119de2021-03-08 15:26:48 +01001210 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001211 _HA_ATOMIC_INC(&sess->listener->counters->srv_aborts);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001212 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001213 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.srv_aborts);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001214 if (!(s->flags & SF_ERR_MASK))
1215 s->flags |= SF_ERR_SRVCL;
1216 status = 502;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001217 goto return_prx_cond;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001218
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001219 return_int_err:
1220 if (!(s->flags & SF_ERR_MASK))
1221 s->flags |= SF_ERR_INTERNAL;
Willy Tarreau4781b152021-04-06 13:53:36 +02001222 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
1223 _HA_ATOMIC_INC(&s->be->be_counters.internal_errors);
William Lallemand36119de2021-03-08 15:26:48 +01001224 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001225 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001226 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001227 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.internal_errors);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001228 status = 500;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001229 goto return_prx_cond;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001230
Christopher Faulet93e02d82019-03-08 14:18:50 +01001231 return_bad_req:
Willy Tarreau4781b152021-04-06 13:53:36 +02001232 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req);
William Lallemand36119de2021-03-08 15:26:48 +01001233 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001234 _HA_ATOMIC_INC(&sess->listener->counters->failed_req);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001235 status = 400;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001236 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001237
Christopher Fauletb8a53712019-12-16 11:29:38 +01001238 return_prx_cond:
Christopher Faulet9768c262018-10-22 09:34:31 +02001239 if (txn->status > 0) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001240 /* Note: we don't send any error if some data were already sent */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001241 http_reply_and_close(s, txn->status, NULL);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001242 } else {
Christopher Faulet93e02d82019-03-08 14:18:50 +01001243 txn->status = status;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001244 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001245 }
Christopher Fauletb8a53712019-12-16 11:29:38 +01001246 if (!(s->flags & SF_ERR_MASK))
1247 s->flags |= SF_ERR_PRXCOND;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001248 if (!(s->flags & SF_FINST_MASK))
1249 s->flags |= ((txn->rsp.msg_state < HTTP_MSG_ERROR) ? SF_FINST_H : SF_FINST_D);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001250 DBG_TRACE_DEVEL("leaving on error ",
1251 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001252 return 0;
1253}
1254
Willy Tarreau4596fe22022-05-17 19:07:51 +02001255/* Reset the stream and the backend stream connector to a situation suitable for attemption connection */
Olivier Houcharda254a372019-04-05 15:30:12 +02001256/* Returns 0 if we can attempt to retry, -1 otherwise */
Willy Tarreaub49672d2022-05-27 10:13:37 +02001257static __inline int do_l7_retry(struct stream *s, struct stconn *sc)
Olivier Houcharda254a372019-04-05 15:30:12 +02001258{
Christopher Faulet9f5382e2021-05-21 13:46:14 +02001259 struct channel *req, *res;
1260 int co_data;
Olivier Houcharda254a372019-04-05 15:30:12 +02001261
Christopher Faulet731c8e62022-03-29 16:08:44 +02001262 s->conn_retries++;
1263 if (s->conn_retries >= s->be->conn_retries)
Christopher Faulet552601d2021-05-26 10:31:06 +02001264 return -1;
Christopher Faulet5b82cc52020-10-12 15:18:50 +02001265
Christopher Faulete763c8c2021-05-05 18:23:59 +02001266 if (objt_server(s->target)) {
1267 if (s->flags & SF_CURR_SESS) {
1268 s->flags &= ~SF_CURR_SESS;
1269 _HA_ATOMIC_DEC(&__objt_server(s->target)->cur_sess);
1270 }
Willy Tarreau4781b152021-04-06 13:53:36 +02001271 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.retries);
Christopher Faulete763c8c2021-05-05 18:23:59 +02001272 }
Willy Tarreau4781b152021-04-06 13:53:36 +02001273 _HA_ATOMIC_INC(&s->be->be_counters.retries);
Willy Tarreau223995e2019-05-04 10:38:31 +02001274
Christopher Faulet9f5382e2021-05-21 13:46:14 +02001275 req = &s->req;
1276 res = &s->res;
Olivier Houcharda254a372019-04-05 15:30:12 +02001277 /* Remove any write error from the request, and read error from the response */
1278 req->flags &= ~(CF_WRITE_ERROR | CF_WRITE_TIMEOUT | CF_SHUTW | CF_SHUTW_NOW);
1279 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 +01001280 res->analysers &= AN_RES_FLT_END;
Christopher Faulet50264b42022-03-30 19:39:30 +02001281 s->conn_err_type = STRM_ET_NONE;
Christopher Fauletae024ce2022-03-29 19:02:31 +02001282 s->flags &= ~(SF_CONN_EXP | SF_ERR_MASK | SF_FINST_MASK);
1283 s->conn_exp = TICK_ETERNITY;
Olivier Houchard4bd58672019-07-12 16:16:59 +02001284 stream_choose_redispatch(s);
Olivier Houcharda254a372019-04-05 15:30:12 +02001285 res->rex = TICK_ETERNITY;
1286 res->to_forward = 0;
1287 res->analyse_exp = TICK_ETERNITY;
1288 res->total = 0;
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +01001289
Willy Tarreau19c65a92022-05-27 08:49:24 +02001290 if (sc_reset_endp(s->scb) < 0) {
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +01001291 if (!(s->flags & SF_ERR_MASK))
1292 s->flags |= SF_ERR_INTERNAL;
1293 return -1;
1294 }
Olivier Houcharda254a372019-04-05 15:30:12 +02001295
Christopher Faulet9f5382e2021-05-21 13:46:14 +02001296 b_free(&req->buf);
1297 /* Swap the L7 buffer with the channel buffer */
1298 /* We know we stored the co_data as b_data, so get it there */
Christopher Faulete05bf9e2022-03-29 15:23:40 +02001299 co_data = b_data(&s->txn->l7_buffer);
1300 b_set_data(&s->txn->l7_buffer, b_size(&s->txn->l7_buffer));
1301 b_xfer(&req->buf, &s->txn->l7_buffer, b_data(&s->txn->l7_buffer));
Christopher Faulet9f5382e2021-05-21 13:46:14 +02001302 co_set_data(req, co_data);
Christopher Faulet5b82cc52020-10-12 15:18:50 +02001303
Ilya Shipitsinacf84592021-02-06 22:29:08 +05001304 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 +02001305
Olivier Houcharda254a372019-04-05 15:30:12 +02001306 b_reset(&res->buf);
1307 co_set_data(res, 0);
1308 return 0;
1309}
1310
Christopher Faulete0768eb2018-10-03 16:38:02 +02001311/* This stream analyser waits for a complete HTTP response. It returns 1 if the
1312 * processing can continue on next analysers, or zero if it either needs more
1313 * data or wants to immediately abort the response (eg: timeout, error, ...). It
1314 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->res.analysers
1315 * when it has nothing left to do, and may remove any analyser when it wants to
1316 * abort.
1317 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001318int http_wait_for_response(struct stream *s, struct channel *rep, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001319{
Christopher Faulet9768c262018-10-22 09:34:31 +02001320 /*
1321 * We will analyze a complete HTTP response to check the its syntax.
1322 *
1323 * Once the start line and all headers are received, we may perform a
1324 * capture of the error (if any), and we will set a few fields. We also
1325 * logging and finally headers capture.
1326 */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001327 struct session *sess = s->sess;
1328 struct http_txn *txn = s->txn;
1329 struct http_msg *msg = &txn->rsp;
Christopher Faulet9768c262018-10-22 09:34:31 +02001330 struct htx *htx;
Christopher Faulet61608322018-11-23 16:23:45 +01001331 struct connection *srv_conn;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001332 struct htx_sl *sl;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001333 int n;
1334
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001335 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001336
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001337 htx = htxbuf(&rep->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001338
Willy Tarreau4236f032019-03-05 10:43:32 +01001339 /* Parsing errors are caught here */
1340 if (htx->flags & HTX_FL_PARSING_ERROR)
1341 goto return_bad_res;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001342 if (htx->flags & HTX_FL_PROCESSING_ERROR)
1343 goto return_int_err;
Willy Tarreau4236f032019-03-05 10:43:32 +01001344
Christopher Faulete0768eb2018-10-03 16:38:02 +02001345 /*
1346 * Now we quickly check if we have found a full valid response.
1347 * If not so, we check the FD and buffer states before leaving.
1348 * A full response is indicated by the fact that we have seen
1349 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
1350 * responses are checked first.
1351 *
1352 * Depending on whether the client is still there or not, we
1353 * may send an error response back or not. Note that normally
1354 * we should only check for HTTP status there, and check I/O
1355 * errors somewhere else.
1356 */
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001357 next_one:
Christopher Faulet29f17582019-05-23 11:03:26 +02001358 if (unlikely(htx_is_empty(htx) || htx->first == -1)) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001359 /* 1: have we encountered a read error ? */
1360 if (rep->flags & CF_READ_ERROR) {
Willy Tarreaufd9417b2022-05-18 16:23:22 +02001361 struct connection *conn = sc_conn(s->scb);
Olivier Houchard865d8392019-05-03 22:46:27 +02001362
Christopher Fauletd9769232021-05-26 12:15:37 +02001363 /* Perform a L7 retry because server refuses the early data. */
Christopher Faulete05bf9e2022-03-29 15:23:40 +02001364 if ((txn->flags & TX_L7_RETRY) &&
Christopher Fauletd9769232021-05-26 12:15:37 +02001365 (s->be->retry_type & PR_RE_EARLY_ERROR) &&
1366 conn && conn->err_code == CO_ER_SSL_EARLY_FAILED &&
Willy Tarreau7cb9e6c2022-05-17 19:40:40 +02001367 do_l7_retry(s, s->scb) == 0) {
Christopher Fauletd9769232021-05-26 12:15:37 +02001368 DBG_TRACE_DEVEL("leaving on L7 retry",
1369 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
1370 return 0;
1371 }
1372
Olivier Houchard6db16992019-05-17 15:40:49 +02001373 if (txn->flags & TX_NOT_FIRST)
1374 goto abort_keep_alive;
1375
Willy Tarreau4781b152021-04-06 13:53:36 +02001376 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001377 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001378 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001379 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001380 }
1381
Christopher Fauletd9769232021-05-26 12:15:37 +02001382 /* if the server refused the early data, just send a 425 */
1383 if (conn && conn->err_code == CO_ER_SSL_EARLY_FAILED)
Olivier Houchard865d8392019-05-03 22:46:27 +02001384 txn->status = 425;
Christopher Fauletd9769232021-05-26 12:15:37 +02001385 else {
1386 txn->status = 502;
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001387 stream_inc_http_fail_ctr(s);
Christopher Fauletd9769232021-05-26 12:15:37 +02001388 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001389
Willy Tarreaucb041662022-05-17 19:44:42 +02001390 s->scb->flags |= SC_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001391 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001392
1393 if (!(s->flags & SF_ERR_MASK))
1394 s->flags |= SF_ERR_SRVCL;
1395 if (!(s->flags & SF_FINST_MASK))
1396 s->flags |= SF_FINST_H;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001397 DBG_TRACE_DEVEL("leaving on error",
1398 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001399 return 0;
1400 }
1401
Christopher Faulet9768c262018-10-22 09:34:31 +02001402 /* 2: read timeout : return a 504 to the client. */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001403 else if (rep->flags & CF_READ_TIMEOUT) {
Christopher Faulete05bf9e2022-03-29 15:23:40 +02001404 if ((txn->flags & TX_L7_RETRY) &&
Olivier Houcharda254a372019-04-05 15:30:12 +02001405 (s->be->retry_type & PR_RE_TIMEOUT)) {
Willy Tarreau7cb9e6c2022-05-17 19:40:40 +02001406 if (co_data(rep) || do_l7_retry(s, s->scb) == 0) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001407 DBG_TRACE_DEVEL("leaving on L7 retry",
1408 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharda254a372019-04-05 15:30:12 +02001409 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001410 }
Olivier Houcharda254a372019-04-05 15:30:12 +02001411 }
Willy Tarreau4781b152021-04-06 13:53:36 +02001412 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001413 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001414 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001415 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001416 }
1417
Christopher Faulete0768eb2018-10-03 16:38:02 +02001418 txn->status = 504;
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001419 stream_inc_http_fail_ctr(s);
Willy Tarreaucb041662022-05-17 19:44:42 +02001420 s->scb->flags |= SC_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001421 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001422
1423 if (!(s->flags & SF_ERR_MASK))
1424 s->flags |= SF_ERR_SRVTO;
1425 if (!(s->flags & SF_FINST_MASK))
1426 s->flags |= SF_FINST_H;
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 /* 3: client abort with an abortonclose */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001433 else if ((rep->flags & CF_SHUTR) && ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001434 _HA_ATOMIC_INC(&sess->fe->fe_counters.cli_aborts);
1435 _HA_ATOMIC_INC(&s->be->be_counters.cli_aborts);
William Lallemand36119de2021-03-08 15:26:48 +01001436 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001437 _HA_ATOMIC_INC(&sess->listener->counters->cli_aborts);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001438 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001439 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.cli_aborts);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001440
Christopher Faulete0768eb2018-10-03 16:38:02 +02001441 txn->status = 400;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001442 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001443
1444 if (!(s->flags & SF_ERR_MASK))
1445 s->flags |= SF_ERR_CLICL;
1446 if (!(s->flags & SF_FINST_MASK))
1447 s->flags |= SF_FINST_H;
1448
1449 /* process_stream() will take care of the error */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001450 DBG_TRACE_DEVEL("leaving on error",
1451 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001452 return 0;
1453 }
1454
Christopher Faulet9768c262018-10-22 09:34:31 +02001455 /* 4: close from server, capture the response if the server has started to respond */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001456 else if (rep->flags & CF_SHUTR) {
Christopher Faulete05bf9e2022-03-29 15:23:40 +02001457 if ((txn->flags & TX_L7_RETRY) &&
Olivier Houcharda254a372019-04-05 15:30:12 +02001458 (s->be->retry_type & PR_RE_DISCONNECTED)) {
Willy Tarreau7cb9e6c2022-05-17 19:40:40 +02001459 if (co_data(rep) || do_l7_retry(s, s->scb) == 0) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001460 DBG_TRACE_DEVEL("leaving on L7 retry",
1461 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharda254a372019-04-05 15:30:12 +02001462 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001463 }
Olivier Houcharda254a372019-04-05 15:30:12 +02001464 }
1465
Olivier Houchard6db16992019-05-17 15:40:49 +02001466 if (txn->flags & TX_NOT_FIRST)
1467 goto abort_keep_alive;
1468
Willy Tarreau4781b152021-04-06 13:53:36 +02001469 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001470 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001471 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001472 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001473 }
1474
Christopher Faulete0768eb2018-10-03 16:38:02 +02001475 txn->status = 502;
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001476 stream_inc_http_fail_ctr(s);
Willy Tarreaucb041662022-05-17 19:44:42 +02001477 s->scb->flags |= SC_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001478 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001479
1480 if (!(s->flags & SF_ERR_MASK))
1481 s->flags |= SF_ERR_SRVCL;
1482 if (!(s->flags & SF_FINST_MASK))
1483 s->flags |= SF_FINST_H;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001484 DBG_TRACE_DEVEL("leaving on error",
1485 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001486 return 0;
1487 }
1488
Christopher Faulet9768c262018-10-22 09:34:31 +02001489 /* 5: write error to client (we don't send any message then) */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001490 else if (rep->flags & CF_WRITE_ERROR) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001491 if (txn->flags & TX_NOT_FIRST)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001492 goto abort_keep_alive;
1493
Willy Tarreau4781b152021-04-06 13:53:36 +02001494 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001495 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001496 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001497 rep->analysers &= AN_RES_FLT_END;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001498
1499 if (!(s->flags & SF_ERR_MASK))
1500 s->flags |= SF_ERR_CLICL;
1501 if (!(s->flags & SF_FINST_MASK))
1502 s->flags |= SF_FINST_H;
1503
1504 /* process_stream() will take care of the error */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001505 DBG_TRACE_DEVEL("leaving on error",
1506 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001507 return 0;
1508 }
1509
1510 channel_dont_close(rep);
1511 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001512 DBG_TRACE_DEVEL("waiting for more data",
1513 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001514 return 0;
1515 }
1516
1517 /* More interesting part now : we know that we have a complete
1518 * response which at least looks like HTTP. We have an indicator
1519 * of each header's length, so we can parse them quickly.
1520 */
Christopher Faulet29f17582019-05-23 11:03:26 +02001521 BUG_ON(htx_get_first_type(htx) != HTX_BLK_RES_SL);
Christopher Faulet297fbb42019-05-13 14:41:27 +02001522 sl = http_get_stline(htx);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001523
Christopher Faulet1f08bff2021-05-26 13:14:39 +02001524 /* Perform a L7 retry because of the status code */
Christopher Faulete05bf9e2022-03-29 15:23:40 +02001525 if ((txn->flags & TX_L7_RETRY) &&
Christopher Faulet1f08bff2021-05-26 13:14:39 +02001526 l7_status_match(s->be, sl->info.res.status) &&
Willy Tarreau7cb9e6c2022-05-17 19:40:40 +02001527 do_l7_retry(s, s->scb) == 0) {
Christopher Faulet1f08bff2021-05-26 13:14:39 +02001528 DBG_TRACE_DEVEL("leaving on L7 retry", STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
1529 return 0;
1530 }
1531
1532 /* Now, L7 buffer is useless, it can be released */
Christopher Faulete05bf9e2022-03-29 15:23:40 +02001533 b_free(&txn->l7_buffer);
Christopher Faulet1f08bff2021-05-26 13:14:39 +02001534
1535 msg->msg_state = HTTP_MSG_BODY;
1536
1537
Christopher Faulet9768c262018-10-22 09:34:31 +02001538 /* 0: we might have to print this header in debug mode */
1539 if (unlikely((global.mode & MODE_DEBUG) &&
1540 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
1541 int32_t pos;
1542
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001543 http_debug_stline("srvrep", s, sl);
Christopher Faulet9768c262018-10-22 09:34:31 +02001544
Christopher Fauleta3f15502019-05-13 15:27:23 +02001545 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001546 struct htx_blk *blk = htx_get_blk(htx, pos);
1547 enum htx_blk_type type = htx_get_blk_type(blk);
1548
1549 if (type == HTX_BLK_EOH)
1550 break;
1551 if (type != HTX_BLK_HDR)
1552 continue;
1553
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001554 http_debug_hdr("srvhdr", s,
1555 htx_get_blk_name(htx, blk),
1556 htx_get_blk_value(htx, blk));
Christopher Faulet9768c262018-10-22 09:34:31 +02001557 }
1558 }
1559
Christopher Faulet03599112018-11-27 11:21:21 +01001560 /* 1: get the status code and the version. Also set HTTP flags */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001561 txn->status = sl->info.res.status;
Christopher Faulet03599112018-11-27 11:21:21 +01001562 if (sl->flags & HTX_SL_F_VER_11)
Christopher Faulet9768c262018-10-22 09:34:31 +02001563 msg->flags |= HTTP_MSGF_VER_11;
Christopher Faulet03599112018-11-27 11:21:21 +01001564 if (sl->flags & HTX_SL_F_XFER_LEN) {
1565 msg->flags |= HTTP_MSGF_XFER_LEN;
Christopher Faulet2a408542020-11-20 14:22:37 +01001566 if (sl->flags & HTX_SL_F_CLEN)
1567 msg->flags |= HTTP_MSGF_CNT_LEN;
1568 else if (sl->flags & HTX_SL_F_CHNK)
1569 msg->flags |= HTTP_MSGF_TE_CHNK;
Christopher Faulet03599112018-11-27 11:21:21 +01001570 }
Christopher Faulet2a408542020-11-20 14:22:37 +01001571 if (sl->flags & HTX_SL_F_BODYLESS)
1572 msg->flags |= HTTP_MSGF_BODYLESS;
Christopher Faulet576c3582021-01-08 15:53:01 +01001573 if (sl->flags & HTX_SL_F_CONN_UPG)
1574 msg->flags |= HTTP_MSGF_CONN_UPG;
Christopher Faulet9768c262018-10-22 09:34:31 +02001575
1576 n = txn->status / 100;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001577 if (n < 1 || n > 5)
1578 n = 0;
Christopher Faulet9768c262018-10-22 09:34:31 +02001579
Christopher Faulete0768eb2018-10-03 16:38:02 +02001580 /* when the client triggers a 4xx from the server, it's most often due
1581 * to a missing object or permission. These events should be tracked
1582 * because if they happen often, it may indicate a brute force or a
1583 * vulnerability scan.
1584 */
1585 if (n == 4)
1586 stream_inc_http_err_ctr(s);
1587
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001588 if (n == 5 && txn->status != 501 && txn->status != 505)
1589 stream_inc_http_fail_ctr(s);
1590
Marcin Deranek3c27dda2020-05-15 18:32:51 +02001591 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001592 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.p.http.rsp[n]);
1593 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.p.http.cum_req);
Marcin Deranek3c27dda2020-05-15 18:32:51 +02001594 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001595
Christopher Faulete0768eb2018-10-03 16:38:02 +02001596 /* Adjust server's health based on status code. Note: status codes 501
1597 * and 505 are triggered on demand by client request, so we must not
1598 * count them as server failures.
1599 */
1600 if (objt_server(s->target)) {
1601 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001602 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_OK);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001603 else
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001604 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_STS);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001605 }
1606
1607 /*
1608 * We may be facing a 100-continue response, or any other informational
1609 * 1xx response which is non-final, in which case this is not the right
1610 * response, and we're waiting for the next one. Let's allow this response
1611 * to go to the client and wait for the next one. There's an exception for
1612 * 101 which is used later in the code to switch protocols.
1613 */
1614 if (txn->status < 200 &&
1615 (txn->status == 100 || txn->status >= 102)) {
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001616 FLT_STRM_CB(s, flt_http_reset(s, msg));
Christopher Faulet421e7692019-06-13 11:16:45 +02001617 htx->first = channel_htx_fwd_headers(rep, htx);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001618 msg->msg_state = HTTP_MSG_RPBEFORE;
Christopher Faulet3499f622019-09-03 15:23:54 +02001619 msg->flags = 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001620 txn->status = 0;
1621 s->logs.t_data = -1; /* was not a response yet */
Christopher Faulet7d518452020-08-31 11:07:07 +02001622 rep->flags |= CF_SEND_DONTWAIT; /* Send ASAP informational messages */
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001623 goto next_one;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001624 }
1625
Christopher Faulet6e6c7b12021-01-08 16:02:05 +01001626 /* A 101-switching-protocols must contains a Connection header with the
1627 * "upgrade" option and the request too. It means both are agree to
1628 * upgrade. It is not so strict because there is no test on the Upgrade
1629 * header content. But it is probably stronger enough for now.
1630 */
1631 if (txn->status == 101 &&
1632 (!(txn->req.flags & HTTP_MSGF_CONN_UPG) || !(txn->rsp.flags & HTTP_MSGF_CONN_UPG)))
1633 goto return_bad_res;
1634
Christopher Faulete0768eb2018-10-03 16:38:02 +02001635 /*
1636 * 2: check for cacheability.
1637 */
1638
1639 switch (txn->status) {
1640 case 200:
1641 case 203:
1642 case 204:
1643 case 206:
1644 case 300:
1645 case 301:
1646 case 404:
1647 case 405:
1648 case 410:
1649 case 414:
1650 case 501:
1651 break;
1652 default:
1653 /* RFC7231#6.1:
1654 * Responses with status codes that are defined as
1655 * cacheable by default (e.g., 200, 203, 204, 206,
1656 * 300, 301, 404, 405, 410, 414, and 501 in this
1657 * specification) can be reused by a cache with
1658 * heuristic expiration unless otherwise indicated
1659 * by the method definition or explicit cache
1660 * controls [RFC7234]; all other status codes are
1661 * not cacheable by default.
1662 */
1663 txn->flags &= ~(TX_CACHEABLE | TX_CACHE_COOK);
1664 break;
1665 }
1666
1667 /*
1668 * 3: we may need to capture headers
1669 */
1670 s->logs.logwait &= ~LW_RESP;
1671 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->res_cap))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001672 http_capture_headers(htx, s->res_cap, sess->fe->rsp_cap);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001673
Christopher Faulet9768c262018-10-22 09:34:31 +02001674 /* Skip parsing if no content length is possible. */
Christopher Fauletc75668e2020-12-07 18:10:32 +01001675 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status >= 200 && txn->status < 300) ||
Christopher Faulete0768eb2018-10-03 16:38:02 +02001676 txn->status == 101)) {
1677 /* Either we've established an explicit tunnel, or we're
1678 * switching the protocol. In both cases, we're very unlikely
1679 * to understand the next protocols. We have to switch to tunnel
1680 * mode, so that we transfer the request and responses then let
1681 * this protocol pass unmodified. When we later implement specific
1682 * parsers for such protocols, we'll want to check the Upgrade
1683 * header which contains information about that protocol for
1684 * responses with status 101 (eg: see RFC2817 about TLS).
1685 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02001686 txn->flags |= TX_CON_WANT_TUN;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001687 }
1688
Christopher Faulet61608322018-11-23 16:23:45 +01001689 /* check for NTML authentication headers in 401 (WWW-Authenticate) and
1690 * 407 (Proxy-Authenticate) responses and set the connection to private
1691 */
Willy Tarreaufd9417b2022-05-18 16:23:22 +02001692 srv_conn = sc_conn(s->scb);
Christopher Faulet61608322018-11-23 16:23:45 +01001693 if (srv_conn) {
1694 struct ist hdr;
1695 struct http_hdr_ctx ctx;
1696
1697 if (txn->status == 401)
1698 hdr = ist("WWW-Authenticate");
1699 else if (txn->status == 407)
1700 hdr = ist("Proxy-Authenticate");
1701 else
1702 goto end;
1703
1704 ctx.blk = NULL;
1705 while (http_find_header(htx, hdr, &ctx, 0)) {
Willy Tarreauf1dcced2020-05-07 19:27:02 +02001706 /* If www-authenticate contains "Negotiate", "Nego2", or "NTLM",
1707 * possibly followed by blanks and a base64 string, the connection
1708 * is private. Since it's a mess to deal with, we only check for
1709 * values starting with "NTLM" or "Nego". Note that often multiple
1710 * headers are sent by the server there.
1711 */
1712 if ((ctx.value.len >= 4 && strncasecmp(ctx.value.ptr, "Nego", 4) == 0) ||
Willy Tarreau49a1d282020-05-07 19:10:15 +02001713 (ctx.value.len >= 4 && strncasecmp(ctx.value.ptr, "NTLM", 4) == 0)) {
Olivier Houchard250031e2019-05-29 15:01:50 +02001714 sess->flags |= SESS_FL_PREFER_LAST;
Christopher Faulet08016ab2020-07-01 16:10:06 +02001715 conn_set_owner(srv_conn, sess, NULL);
Christopher Faulet21ddc742020-07-01 15:26:14 +02001716 conn_set_private(srv_conn);
Ilya Shipitsin6b79f382020-07-23 00:32:55 +05001717 /* If it fail now, the same will be done in mux->detach() callback */
Christopher Faulet08016ab2020-07-01 16:10:06 +02001718 session_add_conn(srv_conn->owner, srv_conn, srv_conn->target);
Willy Tarreauf1dcced2020-05-07 19:27:02 +02001719 break;
Olivier Houchard250031e2019-05-29 15:01:50 +02001720 }
Christopher Faulet61608322018-11-23 16:23:45 +01001721 }
1722 }
1723
1724 end:
Christopher Faulete0768eb2018-10-03 16:38:02 +02001725 /* we want to have the response time before we start processing it */
1726 s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now);
1727
1728 /* end of job, return OK */
1729 rep->analysers &= ~an_bit;
1730 rep->analyse_exp = TICK_ETERNITY;
1731 channel_auto_close(rep);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001732 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001733 return 1;
1734
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001735 return_int_err:
Willy Tarreau4781b152021-04-06 13:53:36 +02001736 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
1737 _HA_ATOMIC_INC(&s->be->be_counters.internal_errors);
William Lallemand36119de2021-03-08 15:26:48 +01001738 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001739 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Fauletb8a53712019-12-16 11:29:38 +01001740 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001741 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.internal_errors);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001742 txn->status = 500;
1743 if (!(s->flags & SF_ERR_MASK))
1744 s->flags |= SF_ERR_INTERNAL;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001745 goto return_prx_cond;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001746
1747 return_bad_res:
Willy Tarreau4781b152021-04-06 13:53:36 +02001748 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Faulet47365272018-10-31 17:40:50 +01001749 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001750 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001751 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Christopher Faulet47365272018-10-31 17:40:50 +01001752 }
Olivier Houcharde3249a92019-05-03 23:01:47 +02001753 if ((s->be->retry_type & PR_RE_JUNK_REQUEST) &&
Christopher Faulete05bf9e2022-03-29 15:23:40 +02001754 (txn->flags & TX_L7_RETRY) &&
Willy Tarreau7cb9e6c2022-05-17 19:40:40 +02001755 do_l7_retry(s, s->scb) == 0) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001756 DBG_TRACE_DEVEL("leaving on L7 retry",
1757 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharde3249a92019-05-03 23:01:47 +02001758 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001759 }
Christopher Faulet47365272018-10-31 17:40:50 +01001760 txn->status = 502;
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001761 stream_inc_http_fail_ctr(s);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001762 /* fall through */
1763
Christopher Fauletb8a53712019-12-16 11:29:38 +01001764 return_prx_cond:
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001765 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet47365272018-10-31 17:40:50 +01001766
1767 if (!(s->flags & SF_ERR_MASK))
1768 s->flags |= SF_ERR_PRXCOND;
1769 if (!(s->flags & SF_FINST_MASK))
1770 s->flags |= SF_FINST_H;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001771
Willy Tarreaucb041662022-05-17 19:44:42 +02001772 s->scb->flags |= SC_FL_NOLINGER;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001773 DBG_TRACE_DEVEL("leaving on error",
1774 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulet47365272018-10-31 17:40:50 +01001775 return 0;
1776
Christopher Faulete0768eb2018-10-03 16:38:02 +02001777 abort_keep_alive:
1778 /* A keep-alive request to the server failed on a network error.
1779 * The client is required to retry. We need to close without returning
1780 * any other information so that the client retries.
1781 */
1782 txn->status = 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001783 s->logs.logwait = 0;
1784 s->logs.level = 0;
1785 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001786 http_reply_and_close(s, txn->status, NULL);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001787 DBG_TRACE_DEVEL("leaving by closing K/A connection",
1788 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001789 return 0;
1790}
1791
1792/* This function performs all the processing enabled for the current response.
1793 * It normally returns 1 unless it wants to break. It relies on buffers flags,
1794 * and updates s->res.analysers. It might make sense to explode it into several
1795 * other functions. It works like process_request (see indications above).
1796 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001797int http_process_res_common(struct stream *s, struct channel *rep, int an_bit, struct proxy *px)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001798{
1799 struct session *sess = s->sess;
1800 struct http_txn *txn = s->txn;
1801 struct http_msg *msg = &txn->rsp;
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001802 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001803 struct proxy *cur_proxy;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001804 enum rule_result ret = HTTP_RULE_RES_CONT;
1805
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001806 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
1807 return 0;
Christopher Faulet9768c262018-10-22 09:34:31 +02001808
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001809 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001810
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001811 htx = htxbuf(&rep->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001812
1813 /* The stats applet needs to adjust the Connection header but we don't
1814 * apply any filter there.
1815 */
1816 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
1817 rep->analysers &= ~an_bit;
1818 rep->analyse_exp = TICK_ETERNITY;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001819 goto end;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001820 }
1821
1822 /*
1823 * We will have to evaluate the filters.
1824 * As opposed to version 1.2, now they will be evaluated in the
1825 * filters order and not in the header order. This means that
1826 * each filter has to be validated among all headers.
1827 *
1828 * Filters are tried with ->be first, then with ->fe if it is
1829 * different from ->be.
1830 *
1831 * Maybe we are in resume condiion. In this case I choose the
1832 * "struct proxy" which contains the rule list matching the resume
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05001833 * pointer. If none of these "struct proxy" match, I initialise
Christopher Faulete0768eb2018-10-03 16:38:02 +02001834 * the process with the first one.
1835 *
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05001836 * In fact, I check only correspondence between the current list
Christopher Faulete0768eb2018-10-03 16:38:02 +02001837 * pointer and the ->fe rule list. If it doesn't match, I initialize
1838 * the loop with the ->be.
1839 */
Christopher Fauletd4150ad2021-10-13 15:35:55 +02001840 if (s->current_rule_list == &sess->fe->http_res_rules ||
1841 (sess->fe->defpx && s->current_rule_list == &sess->fe->defpx->http_res_rules))
Christopher Faulete0768eb2018-10-03 16:38:02 +02001842 cur_proxy = sess->fe;
1843 else
1844 cur_proxy = s->be;
Christopher Fauletd4150ad2021-10-13 15:35:55 +02001845
Christopher Faulete0768eb2018-10-03 16:38:02 +02001846 while (1) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001847 /* evaluate http-response rules */
Christopher Faulet46f46df2021-11-09 16:33:25 +01001848 if (ret == HTTP_RULE_RES_CONT || ret == HTTP_RULE_RES_STOP) {
Christopher Fauletd4150ad2021-10-13 15:35:55 +02001849 struct list *def_rules, *rules;
1850
1851 def_rules = ((cur_proxy->defpx && (cur_proxy == s->be || cur_proxy->defpx != s->be->defpx)) ? &cur_proxy->defpx->http_res_rules : NULL);
1852 rules = &cur_proxy->http_res_rules;
1853
1854 ret = http_res_get_intercept_rule(cur_proxy, def_rules, rules, s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001855
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001856 switch (ret) {
1857 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
1858 goto return_prx_yield;
1859
1860 case HTTP_RULE_RES_CONT:
1861 case HTTP_RULE_RES_STOP: /* nothing to do */
1862 break;
1863
1864 case HTTP_RULE_RES_DENY: /* deny or tarpit */
1865 goto deny;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001866
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001867 case HTTP_RULE_RES_ABRT: /* abort request, response already sent */
1868 goto return_prx_cond;
1869
1870 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
Christopher Fauletb8a53712019-12-16 11:29:38 +01001871 goto done;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001872
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001873 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
1874 goto return_bad_res;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001875
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001876 case HTTP_RULE_RES_ERROR: /* failed with a bad request */
1877 goto return_int_err;
1878 }
1879
1880 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001881
Christopher Faulete0768eb2018-10-03 16:38:02 +02001882 /* check whether we're already working on the frontend */
1883 if (cur_proxy == sess->fe)
1884 break;
1885 cur_proxy = sess->fe;
1886 }
1887
Christopher Faulete0768eb2018-10-03 16:38:02 +02001888 /* OK that's all we can do for 1xx responses */
1889 if (unlikely(txn->status < 200 && txn->status != 101))
Christopher Fauletf2824e62018-10-01 12:12:37 +02001890 goto end;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001891
1892 /*
1893 * Now check for a server cookie.
1894 */
1895 if (s->be->cookie_name || sess->fe->capture_name || (s->be->options & PR_O_CHK_CACHE))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001896 http_manage_server_side_cookies(s, rep);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001897
1898 /*
1899 * Check for cache-control or pragma headers if required.
1900 */
1901 if ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001902 http_check_response_for_cacheability(s, rep);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001903
1904 /*
1905 * Add server cookie in the response if needed
1906 */
1907 if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) &&
1908 !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) &&
1909 (!(s->flags & SF_DIRECT) ||
1910 ((s->be->cookie_maxidle || txn->cookie_last_date) &&
1911 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
1912 (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
1913 (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
1914 (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
1915 !(s->flags & SF_IGNORE_PRST)) {
1916 /* the server is known, it's not the one the client requested, or the
1917 * cookie's last seen date needs to be refreshed. We have to
1918 * insert a set-cookie here, except if we want to insert only on POST
1919 * requests and this one isn't. Note that servers which don't have cookies
1920 * (eg: some backup servers) will return a full cookie removal request.
1921 */
Willy Tarreau88bc8002021-12-06 07:01:02 +00001922 if (!__objt_server(s->target)->cookie) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001923 chunk_printf(&trash,
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001924 "%s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
Christopher Faulete0768eb2018-10-03 16:38:02 +02001925 s->be->cookie_name);
1926 }
1927 else {
Willy Tarreau88bc8002021-12-06 07:01:02 +00001928 chunk_printf(&trash, "%s=%s", s->be->cookie_name, __objt_server(s->target)->cookie);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001929
1930 if (s->be->cookie_maxidle || s->be->cookie_maxlife) {
1931 /* emit last_date, which is mandatory */
1932 trash.area[trash.data++] = COOKIE_DELIM_DATE;
1933 s30tob64((date.tv_sec+3) >> 2,
1934 trash.area + trash.data);
1935 trash.data += 5;
1936
1937 if (s->be->cookie_maxlife) {
1938 /* emit first_date, which is either the original one or
1939 * the current date.
1940 */
1941 trash.area[trash.data++] = COOKIE_DELIM_DATE;
1942 s30tob64(txn->cookie_first_date ?
1943 txn->cookie_first_date >> 2 :
1944 (date.tv_sec+3) >> 2,
1945 trash.area + trash.data);
1946 trash.data += 5;
1947 }
1948 }
1949 chunk_appendf(&trash, "; path=/");
1950 }
1951
1952 if (s->be->cookie_domain)
1953 chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain);
1954
1955 if (s->be->ck_opts & PR_CK_HTTPONLY)
1956 chunk_appendf(&trash, "; HttpOnly");
1957
1958 if (s->be->ck_opts & PR_CK_SECURE)
1959 chunk_appendf(&trash, "; Secure");
1960
Christopher Faulet2f533902020-01-21 11:06:48 +01001961 if (s->be->cookie_attrs)
1962 chunk_appendf(&trash, "; %s", s->be->cookie_attrs);
1963
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001964 if (unlikely(!http_add_header(htx, ist("Set-Cookie"), ist2(trash.area, trash.data))))
Christopher Fauletd649b572022-06-01 17:42:35 +02001965 goto return_fail_rewrite;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001966
1967 txn->flags &= ~TX_SCK_MASK;
1968 if (__objt_server(s->target)->cookie && (s->flags & SF_DIRECT))
1969 /* the server did not change, only the date was updated */
1970 txn->flags |= TX_SCK_UPDATED;
1971 else
1972 txn->flags |= TX_SCK_INSERTED;
1973
1974 /* Here, we will tell an eventual cache on the client side that we don't
1975 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
1976 * Some caches understand the correct form: 'no-cache="set-cookie"', but
1977 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
1978 */
1979 if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
1980
1981 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
1982
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001983 if (unlikely(!http_add_header(htx, ist("Cache-control"), ist("private"))))
Christopher Fauletd649b572022-06-01 17:42:35 +02001984 goto return_fail_rewrite;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001985 }
1986 }
1987
1988 /*
1989 * Check if result will be cacheable with a cookie.
1990 * We'll block the response if security checks have caught
1991 * nasty things such as a cacheable cookie.
1992 */
1993 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
1994 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
1995 (s->be->options & PR_O_CHK_CACHE)) {
1996 /* we're in presence of a cacheable response containing
1997 * a set-cookie header. We'll block it as requested by
1998 * the 'checkcache' option, and send an alert.
1999 */
Christopher Faulete0768eb2018-10-03 16:38:02 +02002000 ha_alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreau88bc8002021-12-06 07:01:02 +00002001 s->be->id, objt_server(s->target) ? __objt_server(s->target)->id : "<dispatch>");
Christopher Faulete0768eb2018-10-03 16:38:02 +02002002 send_log(s->be, LOG_ALERT,
2003 "Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreau88bc8002021-12-06 07:01:02 +00002004 s->be->id, objt_server(s->target) ? __objt_server(s->target)->id : "<dispatch>");
Christopher Fauletb8a53712019-12-16 11:29:38 +01002005 goto deny;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002006 }
2007
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002008 end:
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002009 /*
2010 * Evaluate after-response rules before forwarding the response. rules
2011 * from the backend are evaluated first, then one from the frontend if
2012 * it differs.
2013 */
2014 if (!http_eval_after_res_rules(s))
2015 goto return_int_err;
2016
Christopher Fauletc2ac5e42021-03-08 18:20:09 +01002017 /* Filter the response headers if there are filters attached to the
2018 * stream.
2019 */
2020 if (HAS_FILTERS(s))
2021 rep->analysers |= AN_RES_FLT_HTTP_HDRS;
2022
Christopher Faulete0768eb2018-10-03 16:38:02 +02002023 /* Always enter in the body analyzer */
2024 rep->analysers &= ~AN_RES_FLT_XFER_DATA;
2025 rep->analysers |= AN_RES_HTTP_XFER_BODY;
2026
2027 /* if the user wants to log as soon as possible, without counting
2028 * bytes from the server, then this is the right moment. We have
2029 * to temporarily assign bytes_out to log what we currently have.
2030 */
2031 if (!LIST_ISEMPTY(&sess->fe->logformat) && !(s->logs.logwait & LW_BYTES)) {
2032 s->logs.t_close = s->logs.t_data; /* to get a valid end date */
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002033 s->logs.bytes_out = htx->data;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002034 s->do_log(s);
2035 s->logs.bytes_out = 0;
2036 }
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002037
Christopher Fauletb8a53712019-12-16 11:29:38 +01002038 done:
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002039 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002040 rep->analysers &= ~an_bit;
2041 rep->analyse_exp = TICK_ETERNITY;
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002042 s->current_rule = s->current_rule_list = NULL;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002043 return 1;
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002044
Christopher Fauletb8a53712019-12-16 11:29:38 +01002045 deny:
Willy Tarreau4781b152021-04-06 13:53:36 +02002046 _HA_ATOMIC_INC(&sess->fe->fe_counters.denied_resp);
2047 _HA_ATOMIC_INC(&s->be->be_counters.denied_resp);
William Lallemand36119de2021-03-08 15:26:48 +01002048 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02002049 _HA_ATOMIC_INC(&sess->listener->counters->denied_resp);
Christopher Fauleta08546b2019-12-16 16:07:34 +01002050 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02002051 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.denied_resp);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002052 goto return_prx_err;
2053
Christopher Fauletd649b572022-06-01 17:42:35 +02002054 return_fail_rewrite:
2055 if (!(s->flags & SF_ERR_MASK))
2056 s->flags |= SF_ERR_PRXCOND;
2057 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_rewrites);
2058 _HA_ATOMIC_INC(&s->be->be_counters.failed_rewrites);
2059 if (sess->listener && sess->listener->counters)
2060 _HA_ATOMIC_INC(&sess->listener->counters->failed_rewrites);
2061 if (objt_server(s->target))
2062 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_rewrites);
2063 /* fall through */
2064
Christopher Fauletb8a53712019-12-16 11:29:38 +01002065 return_int_err:
2066 txn->status = 500;
2067 if (!(s->flags & SF_ERR_MASK))
2068 s->flags |= SF_ERR_INTERNAL;
Willy Tarreau4781b152021-04-06 13:53:36 +02002069 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
2070 _HA_ATOMIC_INC(&s->be->be_counters.internal_errors);
Dragan Dosen9a006f92021-09-21 13:02:09 +02002071 if (sess->listener && sess->listener->counters)
2072 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Fauletcff0f732019-12-16 16:13:44 +01002073 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02002074 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.internal_errors);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002075 goto return_prx_err;
2076
2077 return_bad_res:
2078 txn->status = 502;
Willy Tarreau826f3ab2021-02-10 12:07:15 +01002079 stream_inc_http_fail_ctr(s);
Willy Tarreau4781b152021-04-06 13:53:36 +02002080 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Fauleta20a6532020-02-05 10:16:41 +01002081 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02002082 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Christopher Fauleta20a6532020-02-05 10:16:41 +01002083 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_RSP);
2084 }
Christopher Fauletb8a53712019-12-16 11:29:38 +01002085 /* fall through */
2086
2087 return_prx_err:
2088 http_reply_and_close(s, txn->status, http_error_message(s));
2089 /* fall through */
2090
2091 return_prx_cond:
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002092 s->logs.t_data = -1; /* was not a valid response */
Willy Tarreaucb041662022-05-17 19:44:42 +02002093 s->scb->flags |= SC_FL_NOLINGER;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002094
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002095 if (!(s->flags & SF_ERR_MASK))
2096 s->flags |= SF_ERR_PRXCOND;
2097 if (!(s->flags & SF_FINST_MASK))
2098 s->flags |= SF_FINST_H;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002099
Christopher Faulete58c0002020-03-02 16:21:01 +01002100 rep->analysers &= AN_RES_FLT_END;
2101 s->req.analysers &= AN_REQ_FLT_END;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002102 rep->analyse_exp = TICK_ETERNITY;
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002103 s->current_rule = s->current_rule_list = NULL;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002104 DBG_TRACE_DEVEL("leaving on error",
2105 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002106 return 0;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002107
2108 return_prx_yield:
2109 channel_dont_close(rep);
2110 DBG_TRACE_DEVEL("waiting for more data",
2111 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
2112 return 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002113}
2114
2115/* This function is an analyser which forwards response body (including chunk
2116 * sizes if any). It is called as soon as we must forward, even if we forward
2117 * zero byte. The only situation where it must not be called is when we're in
2118 * tunnel mode and we want to forward till the close. It's used both to forward
2119 * remaining data and to resync after end of body. It expects the msg_state to
2120 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
2121 * read more data, or 1 once we can go on with next request or end the stream.
2122 *
2123 * It is capable of compressing response data both in content-length mode and
2124 * in chunked mode. The state machines follows different flows depending on
2125 * whether content-length and chunked modes are used, since there are no
2126 * trailers in content-length :
2127 *
2128 * chk-mode cl-mode
2129 * ,----- BODY -----.
2130 * / \
2131 * V size > 0 V chk-mode
2132 * .--> SIZE -------------> DATA -------------> CRLF
2133 * | | size == 0 | last byte |
2134 * | v final crlf v inspected |
2135 * | TRAILERS -----------> DONE |
2136 * | |
2137 * `----------------------------------------------'
2138 *
2139 * Compression only happens in the DATA state, and must be flushed in final
2140 * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding
2141 * is performed at once on final states for all bytes parsed, or when leaving
2142 * on missing data.
2143 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002144int http_response_forward_body(struct stream *s, struct channel *res, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +02002145{
2146 struct session *sess = s->sess;
2147 struct http_txn *txn = s->txn;
2148 struct http_msg *msg = &s->txn->rsp;
Christopher Faulet9768c262018-10-22 09:34:31 +02002149 struct htx *htx;
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002150 int ret;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002151
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002152 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002153
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002154 htx = htxbuf(&res->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002155
Christopher Fauletb9a92f32019-09-09 10:15:21 +02002156 if (htx->flags & HTX_FL_PARSING_ERROR)
2157 goto return_bad_res;
2158 if (htx->flags & HTX_FL_PROCESSING_ERROR)
2159 goto return_int_err;
2160
Christopher Faulete0768eb2018-10-03 16:38:02 +02002161 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Christopher Fauletf2824e62018-10-01 12:12:37 +02002162 ((res->flags & CF_SHUTW) && (res->to_forward || co_data(res)))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02002163 /* Output closed while we were sending data. We must abort and
2164 * wake the other side up.
2165 */
Christopher Faulete0768eb2018-10-03 16:38:02 +02002166 msg->msg_state = HTTP_MSG_ERROR;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002167 http_end_response(s);
2168 http_end_request(s);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002169 DBG_TRACE_DEVEL("leaving on error",
2170 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002171 return 1;
2172 }
2173
Christopher Faulet9768c262018-10-22 09:34:31 +02002174 if (msg->msg_state == HTTP_MSG_BODY)
2175 msg->msg_state = HTTP_MSG_DATA;
2176
Christopher Faulete0768eb2018-10-03 16:38:02 +02002177 /* in most states, we should abort in case of early close */
2178 channel_auto_close(res);
2179
Christopher Faulete0768eb2018-10-03 16:38:02 +02002180 if (res->to_forward) {
Christopher Faulet66af0b22019-03-22 14:54:52 +01002181 if (res->to_forward == CHN_INFINITE_FORWARD) {
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002182 if (res->flags & CF_EOI)
2183 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet66af0b22019-03-22 14:54:52 +01002184 }
2185 else {
2186 /* We can't process the buffer's contents yet */
2187 res->flags |= CF_WAKE_WRITE;
2188 goto missing_data_or_waiting;
2189 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02002190 }
2191
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002192 if (msg->msg_state >= HTTP_MSG_ENDING)
2193 goto ending;
2194
Christopher Fauletc75668e2020-12-07 18:10:32 +01002195 if ((txn->meth == HTTP_METH_CONNECT && txn->status >= 200 && txn->status < 300) || txn->status == 101 ||
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002196 (!(msg->flags & HTTP_MSGF_XFER_LEN) && !HAS_RSP_DATA_FILTERS(s))) {
2197 msg->msg_state = HTTP_MSG_ENDING;
2198 goto ending;
2199 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002200
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002201 /* Forward input data. We get it by removing all outgoing data not
2202 * forwarded yet from HTX data size. If there are some data filters, we
2203 * let them decide the amount of data to forward.
Christopher Faulet9768c262018-10-22 09:34:31 +02002204 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002205 if (HAS_RSP_DATA_FILTERS(s)) {
2206 ret = flt_http_payload(s, msg, htx->data);
2207 if (ret < 0)
2208 goto return_bad_res;
Christopher Faulet421e7692019-06-13 11:16:45 +02002209 c_adv(res, ret);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002210 }
2211 else {
Christopher Faulet421e7692019-06-13 11:16:45 +02002212 c_adv(res, htx->data - co_data(res));
Christopher Faulet66af0b22019-03-22 14:54:52 +01002213 if (msg->flags & HTTP_MSGF_XFER_LEN)
2214 channel_htx_forward_forever(res, htx);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002215 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002216
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002217 if (htx->data != co_data(res))
2218 goto missing_data_or_waiting;
2219
2220 if (!(msg->flags & HTTP_MSGF_XFER_LEN) && res->flags & CF_SHUTR) {
2221 msg->msg_state = HTTP_MSG_ENDING;
2222 goto ending;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002223 }
2224
Christopher Faulet9768c262018-10-22 09:34:31 +02002225 /* Check if the end-of-message is reached and if so, switch the message
Christopher Fauletd20fdb02019-06-13 16:43:22 +02002226 * in HTTP_MSG_ENDING state. Then if all data was marked to be
2227 * forwarded, set the state to HTTP_MSG_DONE.
Christopher Faulet9768c262018-10-22 09:34:31 +02002228 */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002229 if (!(htx->flags & HTX_FL_EOM))
Christopher Faulet9768c262018-10-22 09:34:31 +02002230 goto missing_data_or_waiting;
2231
Christopher Fauletd20fdb02019-06-13 16:43:22 +02002232 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet9768c262018-10-22 09:34:31 +02002233
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002234 ending:
Christopher Faulet2151cdd2020-07-22 16:34:59 +02002235 res->flags &= ~CF_EXPECT_MORE; /* no more data are expected */
2236
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002237 /* other states, ENDING...TUNNEL */
2238 if (msg->msg_state >= HTTP_MSG_DONE)
2239 goto done;
Christopher Faulet9768c262018-10-22 09:34:31 +02002240
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002241 if (HAS_RSP_DATA_FILTERS(s)) {
2242 ret = flt_http_end(s, msg);
2243 if (ret <= 0) {
2244 if (!ret)
2245 goto missing_data_or_waiting;
2246 goto return_bad_res;
2247 }
2248 }
2249
Christopher Fauletc75668e2020-12-07 18:10:32 +01002250 if ((txn->meth == HTTP_METH_CONNECT && txn->status >= 200 && txn->status < 300) || txn->status == 101 ||
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002251 !(msg->flags & HTTP_MSGF_XFER_LEN)) {
2252 msg->msg_state = HTTP_MSG_TUNNEL;
2253 goto ending;
2254 }
2255 else {
2256 msg->msg_state = HTTP_MSG_DONE;
2257 res->to_forward = 0;
2258 }
2259
2260 done:
2261
2262 channel_dont_close(res);
2263
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002264 http_end_response(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002265 if (!(res->analysers & an_bit)) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002266 http_end_request(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002267 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
2268 if (res->flags & CF_SHUTW) {
2269 /* response errors are most likely due to the
2270 * client aborting the transfer. */
Christopher Faulet93e02d82019-03-08 14:18:50 +01002271 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002272 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02002273 goto return_bad_res;
2274 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002275 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002276 return 1;
2277 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002278 DBG_TRACE_DEVEL("waiting for the end of the HTTP txn",
2279 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002280 return 0;
2281
2282 missing_data_or_waiting:
2283 if (res->flags & CF_SHUTW)
Christopher Faulet93e02d82019-03-08 14:18:50 +01002284 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002285
2286 /* stop waiting for data if the input is closed before the end. If the
2287 * client side was already closed, it means that the client has aborted,
2288 * so we don't want to count this as a server abort. Otherwise it's a
2289 * server abort.
2290 */
Christopher Fauletd20fdb02019-06-13 16:43:22 +02002291 if (msg->msg_state < HTTP_MSG_ENDING && res->flags & CF_SHUTR) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02002292 if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))
Christopher Faulet93e02d82019-03-08 14:18:50 +01002293 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002294 /* If we have some pending data, we continue the processing */
Christopher Faulet93e02d82019-03-08 14:18:50 +01002295 if (htx_is_empty(htx))
2296 goto return_srv_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002297 }
2298
Christopher Faulete0768eb2018-10-03 16:38:02 +02002299 /* When TE: chunked is used, we need to get there again to parse
2300 * remaining chunks even if the server has closed, so we don't want to
Christopher Faulet9768c262018-10-22 09:34:31 +02002301 * set CF_DONTCLOSE. Similarly when there is a content-leng or if there
2302 * are filters registered on the stream, we don't want to forward a
2303 * close
Christopher Faulete0768eb2018-10-03 16:38:02 +02002304 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002305 if ((msg->flags & HTTP_MSGF_XFER_LEN) || HAS_RSP_DATA_FILTERS(s))
Christopher Faulete0768eb2018-10-03 16:38:02 +02002306 channel_dont_close(res);
2307
2308 /* We know that more data are expected, but we couldn't send more that
2309 * what we did. So we always set the CF_EXPECT_MORE flag so that the
2310 * system knows it must not set a PUSH on this first part. Interactive
2311 * modes are already handled by the stream sock layer. We must not do
2312 * this in content-length mode because it could present the MSG_MORE
2313 * flag with the last block of forwarded data, which would cause an
2314 * additional delay to be observed by the receiver.
2315 */
Christopher Faulet2151cdd2020-07-22 16:34:59 +02002316 if (HAS_RSP_DATA_FILTERS(s))
Christopher Faulete0768eb2018-10-03 16:38:02 +02002317 res->flags |= CF_EXPECT_MORE;
2318
2319 /* the stream handler will take care of timeouts and errors */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002320 DBG_TRACE_DEVEL("waiting for more data to forward",
2321 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002322 return 0;
2323
Christopher Faulet93e02d82019-03-08 14:18:50 +01002324 return_srv_abort:
Willy Tarreau4781b152021-04-06 13:53:36 +02002325 _HA_ATOMIC_INC(&sess->fe->fe_counters.srv_aborts);
2326 _HA_ATOMIC_INC(&s->be->be_counters.srv_aborts);
William Lallemand36119de2021-03-08 15:26:48 +01002327 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02002328 _HA_ATOMIC_INC(&sess->listener->counters->srv_aborts);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002329 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02002330 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.srv_aborts);
Willy Tarreau826f3ab2021-02-10 12:07:15 +01002331 stream_inc_http_fail_ctr(s);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002332 if (!(s->flags & SF_ERR_MASK))
2333 s->flags |= SF_ERR_SRVCL;
2334 goto return_error;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002335
Christopher Faulet93e02d82019-03-08 14:18:50 +01002336 return_cli_abort:
Willy Tarreau4781b152021-04-06 13:53:36 +02002337 _HA_ATOMIC_INC(&sess->fe->fe_counters.cli_aborts);
2338 _HA_ATOMIC_INC(&s->be->be_counters.cli_aborts);
William Lallemand36119de2021-03-08 15:26:48 +01002339 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02002340 _HA_ATOMIC_INC(&sess->listener->counters->cli_aborts);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002341 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02002342 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.cli_aborts);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002343 if (!(s->flags & SF_ERR_MASK))
2344 s->flags |= SF_ERR_CLICL;
2345 goto return_error;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002346
Christopher Fauletb9a92f32019-09-09 10:15:21 +02002347 return_int_err:
Willy Tarreau4781b152021-04-06 13:53:36 +02002348 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
2349 _HA_ATOMIC_INC(&s->be->be_counters.internal_errors);
William Lallemand36119de2021-03-08 15:26:48 +01002350 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02002351 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002352 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02002353 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.internal_errors);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02002354 if (!(s->flags & SF_ERR_MASK))
2355 s->flags |= SF_ERR_INTERNAL;
2356 goto return_error;
2357
Christopher Faulet93e02d82019-03-08 14:18:50 +01002358 return_bad_res:
Willy Tarreau4781b152021-04-06 13:53:36 +02002359 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002360 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02002361 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002362 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_RSP);
2363 }
Willy Tarreau826f3ab2021-02-10 12:07:15 +01002364 stream_inc_http_fail_ctr(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002365 if (!(s->flags & SF_ERR_MASK))
Christopher Faulet93e02d82019-03-08 14:18:50 +01002366 s->flags |= SF_ERR_SRVCL;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002367 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +02002368
Christopher Faulet93e02d82019-03-08 14:18:50 +01002369 return_error:
Christopher Faulete0768eb2018-10-03 16:38:02 +02002370 /* don't send any error message as we're in the body */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002371 http_reply_and_close(s, txn->status, NULL);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002372 if (!(s->flags & SF_FINST_MASK))
2373 s->flags |= SF_FINST_D;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002374 DBG_TRACE_DEVEL("leaving on error",
2375 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002376 return 0;
2377}
2378
Christopher Fauletf2824e62018-10-01 12:12:37 +02002379/* Perform an HTTP redirect based on the information in <rule>. The function
Willy Tarreaubc1223b2021-09-02 16:54:33 +02002380 * returns zero in case of an irrecoverable error such as too large a request
2381 * to build a valid response, 1 in case of successful redirect (hence the rule
2382 * is final), or 2 if the rule has to be silently skipped.
Christopher Fauletf2824e62018-10-01 12:12:37 +02002383 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002384int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn)
Christopher Fauletf2824e62018-10-01 12:12:37 +02002385{
Christopher Faulet99daf282018-11-28 22:58:13 +01002386 struct channel *req = &s->req;
2387 struct channel *res = &s->res;
2388 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01002389 struct htx_sl *sl;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002390 struct buffer *chunk;
Christopher Faulet99daf282018-11-28 22:58:13 +01002391 struct ist status, reason, location;
2392 unsigned int flags;
Christopher Fauleteab17572022-04-26 20:34:38 +02002393 int ret = 1, close = 0; /* Try to keep the connection alive byt default */
Christopher Fauletf2824e62018-10-01 12:12:37 +02002394
2395 chunk = alloc_trash_chunk();
Christopher Fauletb8a53712019-12-16 11:29:38 +01002396 if (!chunk) {
2397 if (!(s->flags & SF_ERR_MASK))
2398 s->flags |= SF_ERR_RESOURCE;
Christopher Faulet99daf282018-11-28 22:58:13 +01002399 goto fail;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002400 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002401
Christopher Faulet99daf282018-11-28 22:58:13 +01002402 /*
2403 * Create the location
2404 */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002405 htx = htxbuf(&req->buf);
Christopher Fauletf2824e62018-10-01 12:12:37 +02002406 switch(rule->type) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002407 case REDIRECT_TYPE_SCHEME: {
2408 struct http_hdr_ctx ctx;
2409 struct ist path, host;
Amaury Denoyellec453f952021-07-06 11:40:12 +02002410 struct http_uri_parser parser;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002411
Christopher Faulet99daf282018-11-28 22:58:13 +01002412 host = ist("");
2413 ctx.blk = NULL;
2414 if (http_find_header(htx, ist("Host"), &ctx, 0))
2415 host = ctx.value;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002416
Christopher Faulet297fbb42019-05-13 14:41:27 +02002417 sl = http_get_stline(htx);
Amaury Denoyellec453f952021-07-06 11:40:12 +02002418 parser = http_uri_parser_init(htx_sl_req_uri(sl));
2419 path = http_parse_path(&parser);
Christopher Faulet99daf282018-11-28 22:58:13 +01002420 /* build message using path */
Tim Duesterhused526372020-03-05 17:56:33 +01002421 if (isttest(path)) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002422 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2423 int qs = 0;
2424 while (qs < path.len) {
2425 if (*(path.ptr + qs) == '?') {
2426 path.len = qs;
2427 break;
2428 }
2429 qs++;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002430 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002431 }
2432 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002433 else
2434 path = ist("/");
Christopher Fauletf2824e62018-10-01 12:12:37 +02002435
Christopher Faulet99daf282018-11-28 22:58:13 +01002436 if (rule->rdr_str) { /* this is an old "redirect" rule */
2437 /* add scheme */
2438 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2439 goto fail;
2440 }
2441 else {
2442 /* add scheme with executing log format */
2443 chunk->data += build_logline(s, chunk->area + chunk->data,
2444 chunk->size - chunk->data,
2445 &rule->rdr_fmt);
2446 }
2447 /* add "://" + host + path */
2448 if (!chunk_memcat(chunk, "://", 3) ||
2449 !chunk_memcat(chunk, host.ptr, host.len) ||
2450 !chunk_memcat(chunk, path.ptr, path.len))
2451 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002452
Christopher Faulet99daf282018-11-28 22:58:13 +01002453 /* append a slash at the end of the location if needed and missing */
2454 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
2455 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
2456 if (chunk->data + 1 >= chunk->size)
2457 goto fail;
2458 chunk->area[chunk->data++] = '/';
2459 }
2460 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002461 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002462
Christopher Faulet99daf282018-11-28 22:58:13 +01002463 case REDIRECT_TYPE_PREFIX: {
2464 struct ist path;
Amaury Denoyellec453f952021-07-06 11:40:12 +02002465 struct http_uri_parser parser;
Christopher Faulet99daf282018-11-28 22:58:13 +01002466
Christopher Faulet297fbb42019-05-13 14:41:27 +02002467 sl = http_get_stline(htx);
Amaury Denoyellec453f952021-07-06 11:40:12 +02002468 parser = http_uri_parser_init(htx_sl_req_uri(sl));
2469 path = http_parse_path(&parser);
Christopher Faulet99daf282018-11-28 22:58:13 +01002470 /* build message using path */
Tim Duesterhused526372020-03-05 17:56:33 +01002471 if (isttest(path)) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002472 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2473 int qs = 0;
2474 while (qs < path.len) {
2475 if (*(path.ptr + qs) == '?') {
2476 path.len = qs;
2477 break;
2478 }
2479 qs++;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002480 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002481 }
2482 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002483 else
2484 path = ist("/");
Christopher Fauletf2824e62018-10-01 12:12:37 +02002485
Christopher Faulet99daf282018-11-28 22:58:13 +01002486 if (rule->rdr_str) { /* this is an old "redirect" rule */
2487 /* add prefix. Note that if prefix == "/", we don't want to
2488 * add anything, otherwise it makes it hard for the user to
2489 * configure a self-redirection.
2490 */
2491 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
2492 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2493 goto fail;
2494 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002495 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002496 else {
2497 /* add prefix with executing log format */
2498 chunk->data += build_logline(s, chunk->area + chunk->data,
2499 chunk->size - chunk->data,
2500 &rule->rdr_fmt);
2501 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002502
Christopher Faulet99daf282018-11-28 22:58:13 +01002503 /* add path */
2504 if (!chunk_memcat(chunk, path.ptr, path.len))
2505 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002506
Christopher Faulet99daf282018-11-28 22:58:13 +01002507 /* append a slash at the end of the location if needed and missing */
2508 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
2509 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
2510 if (chunk->data + 1 >= chunk->size)
2511 goto fail;
2512 chunk->area[chunk->data++] = '/';
2513 }
2514 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002515 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002516 case REDIRECT_TYPE_LOCATION:
2517 default:
2518 if (rule->rdr_str) { /* this is an old "redirect" rule */
2519 /* add location */
2520 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2521 goto fail;
2522 }
2523 else {
2524 /* add location with executing log format */
Willy Tarreaubc1223b2021-09-02 16:54:33 +02002525 int len = build_logline(s, chunk->area + chunk->data,
2526 chunk->size - chunk->data,
2527 &rule->rdr_fmt);
Christopher Fauleteab17572022-04-26 20:34:38 +02002528 if (!len && rule->flags & REDIRECT_FLAG_IGNORE_EMPTY) {
2529 ret = 2;
2530 goto out;
2531 }
Willy Tarreaubc1223b2021-09-02 16:54:33 +02002532
2533 chunk->data += len;
Christopher Faulet99daf282018-11-28 22:58:13 +01002534 }
2535 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002536 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002537 location = ist2(chunk->area, chunk->data);
2538
2539 /*
2540 * Create the 30x response
2541 */
2542 switch (rule->code) {
2543 case 308:
2544 status = ist("308");
2545 reason = ist("Permanent Redirect");
2546 break;
2547 case 307:
2548 status = ist("307");
2549 reason = ist("Temporary Redirect");
2550 break;
2551 case 303:
2552 status = ist("303");
2553 reason = ist("See Other");
2554 break;
2555 case 301:
2556 status = ist("301");
2557 reason = ist("Moved Permanently");
2558 break;
2559 case 302:
2560 default:
2561 status = ist("302");
2562 reason = ist("Found");
2563 break;
2564 }
2565
Christopher Faulet08e66462019-05-23 16:44:59 +02002566 if (!(txn->req.flags & HTTP_MSGF_BODYLESS) && txn->req.msg_state != HTTP_MSG_DONE)
2567 close = 1;
2568
Christopher Faulet99daf282018-11-28 22:58:13 +01002569 htx = htx_from_buf(&res->buf);
Kevin Zhu96b36392020-01-07 09:42:55 +01002570 /* Trim any possible response */
2571 channel_htx_truncate(&s->res, htx);
Christopher Faulet99daf282018-11-28 22:58:13 +01002572 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
2573 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), status, reason);
2574 if (!sl)
2575 goto fail;
2576 sl->info.res.status = rule->code;
2577 s->txn->status = rule->code;
2578
Christopher Faulet08e66462019-05-23 16:44:59 +02002579 if (close && !htx_add_header(htx, ist("Connection"), ist("close")))
2580 goto fail;
2581
2582 if (!htx_add_header(htx, ist("Content-length"), ist("0")) ||
Christopher Faulet99daf282018-11-28 22:58:13 +01002583 !htx_add_header(htx, ist("Location"), location))
2584 goto fail;
2585
2586 if (rule->code == 302 || rule->code == 303 || rule->code == 307) {
2587 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")))
2588 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002589 }
2590
2591 if (rule->cookie_len) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002592 if (!htx_add_header(htx, ist("Set-Cookie"), ist2(rule->cookie_str, rule->cookie_len)))
2593 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002594 }
2595
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002596 if (!htx_add_endof(htx, HTX_BLK_EOH))
Christopher Faulet99daf282018-11-28 22:58:13 +01002597 goto fail;
2598
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002599 htx->flags |= HTX_FL_EOM;
Kevin Zhu96b36392020-01-07 09:42:55 +01002600 htx_to_buf(htx, &res->buf);
Christopher Fauleta72a7e42020-01-28 09:28:11 +01002601 if (!http_forward_proxy_resp(s, 1))
2602 goto fail;
Christopher Faulet99daf282018-11-28 22:58:13 +01002603
Christopher Faulet60b33a52020-01-28 09:18:10 +01002604 if (rule->flags & REDIRECT_FLAG_FROM_REQ) {
2605 /* let's log the request time */
2606 s->logs.tv_request = now;
Christopher Fauletd3475882021-10-04 14:16:46 +02002607 req->analysers &= AN_REQ_FLT_END;
Christopher Faulet99daf282018-11-28 22:58:13 +01002608
Christopher Faulet60b33a52020-01-28 09:18:10 +01002609 if (s->sess->fe == s->be) /* report it if the request was intercepted by the frontend */
Willy Tarreau4781b152021-04-06 13:53:36 +02002610 _HA_ATOMIC_INC(&s->sess->fe->fe_counters.intercepted_req);
Christopher Faulet60b33a52020-01-28 09:18:10 +01002611 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002612
2613 if (!(s->flags & SF_ERR_MASK))
2614 s->flags |= SF_ERR_LOCAL;
2615 if (!(s->flags & SF_FINST_MASK))
Christopher Faulet60b33a52020-01-28 09:18:10 +01002616 s->flags |= ((rule->flags & REDIRECT_FLAG_FROM_REQ) ? SF_FINST_R : SF_FINST_H);
Christopher Fauletf2824e62018-10-01 12:12:37 +02002617
Christopher Fauleteab17572022-04-26 20:34:38 +02002618 out:
Christopher Faulet99daf282018-11-28 22:58:13 +01002619 free_trash_chunk(chunk);
Christopher Fauleteab17572022-04-26 20:34:38 +02002620 return ret;
Christopher Faulet99daf282018-11-28 22:58:13 +01002621
2622 fail:
2623 /* If an error occurred, remove the incomplete HTTP response from the
2624 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01002625 channel_htx_truncate(res, htxbuf(&res->buf));
Christopher Fauleteab17572022-04-26 20:34:38 +02002626 ret = 0;
2627 goto out;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002628}
2629
Christopher Faulet18c13d32022-05-16 11:43:10 +02002630/* This function filters the request header names to only allow [0-9a-zA-Z-]
2631 * characters. Depending on the proxy configuration, headers with a name not
2632 * matching this charset are removed or the request is rejected with a
2633 * 403-Forbidden response if such name are found. It returns HTTP_RULE_RES_CONT
2634 * to continue the request processing or HTTP_RULE_RES_DENY if the request is
2635 * rejected.
2636 */
2637static enum rule_result http_req_restrict_header_names(struct stream *s, struct htx *htx, struct proxy *px)
2638{
2639 struct htx_blk *blk;
2640 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
2641
2642 blk = htx_get_first_blk(htx);
2643 while (blk) {
2644 enum htx_blk_type type = htx_get_blk_type(blk);
2645
2646 if (type == HTX_BLK_HDR) {
2647 struct ist n = htx_get_blk_name(htx, blk);
Mateusz Malek4b85a962022-08-17 14:22:09 +02002648 int i, end = istlen(n);
Christopher Faulet18c13d32022-05-16 11:43:10 +02002649
Mateusz Malek4b85a962022-08-17 14:22:09 +02002650 for (i = 0; i < end; i++) {
Christopher Faulet18c13d32022-05-16 11:43:10 +02002651 if (!isalnum((unsigned char)n.ptr[i]) && n.ptr[i] != '-') {
Mateusz Malek4b85a962022-08-17 14:22:09 +02002652 break;
Christopher Faulet18c13d32022-05-16 11:43:10 +02002653 }
2654 }
Mateusz Malek4b85a962022-08-17 14:22:09 +02002655
2656 if (i < end) {
2657 /* Disallowed character found - block the request or remove the header */
2658 if (px->options2 & PR_O2_RSTRICT_REQ_HDR_NAMES_BLK)
2659 goto block;
2660 blk = htx_remove_blk(htx, blk);
2661 continue;
2662 }
Christopher Faulet18c13d32022-05-16 11:43:10 +02002663 }
2664 if (type == HTX_BLK_EOH)
2665 break;
2666
2667 blk = htx_get_next_blk(htx, blk);
2668 }
2669 out:
2670 return rule_ret;
2671 block:
2672 /* Block the request returning a 403-Forbidden response */
2673 s->txn->status = 403;
2674 rule_ret = HTTP_RULE_RES_DENY;
2675 goto out;
2676}
2677
Christopher Faulet92d34fe2019-12-17 09:20:34 +01002678/* Replace all headers matching the name <name>. The header value is replaced if
2679 * it matches the regex <re>. <str> is used for the replacement. If <full> is
2680 * set to 1, the full-line is matched and replaced. Otherwise, comma-separated
2681 * values are evaluated one by one. It returns 0 on success and -1 on error.
2682 */
2683int http_replace_hdrs(struct stream* s, struct htx *htx, struct ist name,
2684 const char *str, struct my_regex *re, int full)
Christopher Faulet72333522018-10-24 11:25:02 +02002685{
2686 struct http_hdr_ctx ctx;
2687 struct buffer *output = get_trash_chunk();
2688
Christopher Faulet72333522018-10-24 11:25:02 +02002689 ctx.blk = NULL;
Christopher Faulet92d34fe2019-12-17 09:20:34 +01002690 while (http_find_header(htx, name, &ctx, full)) {
Christopher Faulet72333522018-10-24 11:25:02 +02002691 if (!regex_exec_match2(re, ctx.value.ptr, ctx.value.len, MAX_MATCH, pmatch, 0))
2692 continue;
2693
2694 output->data = exp_replace(output->area, output->size, ctx.value.ptr, str, pmatch);
2695 if (output->data == -1)
2696 return -1;
2697 if (!http_replace_header_value(htx, &ctx, ist2(output->area, output->data)))
2698 return -1;
2699 }
2700 return 0;
2701}
2702
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002703/* This function executes one of the set-{method,path,query,uri} actions. It
2704 * takes the string from the variable 'replace' with length 'len', then modifies
2705 * the relevant part of the request line accordingly. Then it updates various
2706 * pointers to the next elements which were moved, and the total buffer length.
2707 * It finds the action to be performed in p[2], previously filled by function
2708 * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal
2709 * error, though this can be revisited when this code is finally exploited.
2710 *
2711 * 'action' can be '0' to replace method, '1' to replace path, '2' to replace
Christopher Faulet312294f2020-09-02 17:17:44 +02002712 * query string, 3 to replace uri or 4 to replace the path+query.
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002713 *
2714 * In query string case, the mark question '?' must be set at the start of the
2715 * string by the caller, event if the replacement query string is empty.
2716 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002717int http_req_replace_stline(int action, const char *replace, int len,
2718 struct proxy *px, struct stream *s)
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002719{
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002720 struct htx *htx = htxbuf(&s->req.buf);
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002721
2722 switch (action) {
2723 case 0: // method
2724 if (!http_replace_req_meth(htx, ist2(replace, len)))
2725 return -1;
2726 break;
2727
2728 case 1: // path
Christopher Fauletb8ce5052020-08-31 16:11:57 +02002729 if (!http_replace_req_path(htx, ist2(replace, len), 0))
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002730 return -1;
2731 break;
2732
2733 case 2: // query
2734 if (!http_replace_req_query(htx, ist2(replace, len)))
2735 return -1;
2736 break;
2737
2738 case 3: // uri
2739 if (!http_replace_req_uri(htx, ist2(replace, len)))
2740 return -1;
2741 break;
2742
Christopher Faulet312294f2020-09-02 17:17:44 +02002743 case 4: // path + query
2744 if (!http_replace_req_path(htx, ist2(replace, len), 1))
2745 return -1;
2746 break;
2747
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002748 default:
2749 return -1;
2750 }
2751 return 0;
2752}
2753
2754/* This function replace the HTTP status code and the associated message. The
Christopher Faulete00d06c2019-12-16 17:18:42 +01002755 * variable <status> contains the new status code. This function never fails. It
2756 * returns 0 in case of success, -1 in case of internal error.
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002757 */
Christopher Faulet96bff762019-12-17 13:46:18 +01002758int http_res_set_status(unsigned int status, struct ist reason, struct stream *s)
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002759{
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002760 struct htx *htx = htxbuf(&s->res.buf);
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002761 char *res;
2762
2763 chunk_reset(&trash);
2764 res = ultoa_o(status, trash.area, trash.size);
2765 trash.data = res - trash.area;
2766
2767 /* Do we have a custom reason format string? */
Tim Duesterhuse296d3e2020-03-05 17:56:31 +01002768 if (!isttest(reason)) {
Christopher Faulet96bff762019-12-17 13:46:18 +01002769 const char *str = http_get_reason(status);
Tim Duesterhusdcf753a2021-03-04 17:31:47 +01002770 reason = ist(str);
Christopher Faulet96bff762019-12-17 13:46:18 +01002771 }
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002772
Christopher Fauletbde2c4c2020-08-31 16:43:34 +02002773 if (!http_replace_res_status(htx, ist2(trash.area, trash.data), reason))
Christopher Faulete00d06c2019-12-16 17:18:42 +01002774 return -1;
2775 return 0;
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002776}
2777
Christopher Faulet3e964192018-10-24 11:39:23 +02002778/* Executes the http-request rules <rules> for stream <s>, proxy <px> and
2779 * transaction <txn>. Returns the verdict of the first rule that prevents
2780 * further processing of the request (auth, deny, ...), and defaults to
2781 * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or
2782 * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT
2783 * on txn->flags if it encounters a tarpit rule. If <deny_status> is not NULL
2784 * and a deny/tarpit rule is matched, it will be filled with this rule's deny
2785 * status.
2786 */
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002787static enum rule_result http_req_get_intercept_rule(struct proxy *px, struct list *def_rules,
2788 struct list *rules, struct stream *s)
Christopher Faulet3e964192018-10-24 11:39:23 +02002789{
2790 struct session *sess = strm_sess(s);
2791 struct http_txn *txn = s->txn;
Christopher Faulet3e964192018-10-24 11:39:23 +02002792 struct act_rule *rule;
Christopher Faulet3e964192018-10-24 11:39:23 +02002793 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002794 int act_opts = 0;
Christopher Faulet3e964192018-10-24 11:39:23 +02002795
Christopher Faulet3e964192018-10-24 11:39:23 +02002796 /* If "the current_rule_list" match the executed rule list, we are in
2797 * resume condition. If a resume is needed it is always in the action
2798 * and never in the ACL or converters. In this case, we initialise the
2799 * current rule, and go to the action execution point.
2800 */
2801 if (s->current_rule) {
2802 rule = s->current_rule;
2803 s->current_rule = NULL;
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002804 if (s->current_rule_list == rules || (def_rules && s->current_rule_list == def_rules))
Christopher Faulet3e964192018-10-24 11:39:23 +02002805 goto resume_execution;
2806 }
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002807 s->current_rule_list = ((!def_rules || s->current_rule_list == def_rules) ? rules : def_rules);
Christopher Faulet3e964192018-10-24 11:39:23 +02002808
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002809 restart:
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002810 /* start the ruleset evaluation in strict mode */
2811 txn->req.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01002812
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002813 list_for_each_entry(rule, s->current_rule_list, list) {
Christopher Faulet3e964192018-10-24 11:39:23 +02002814 /* check optional condition */
2815 if (rule->cond) {
2816 int ret;
2817
2818 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
2819 ret = acl_pass(ret);
2820
2821 if (rule->cond->pol == ACL_COND_UNLESS)
2822 ret = !ret;
2823
2824 if (!ret) /* condition not matched */
2825 continue;
2826 }
2827
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002828 act_opts |= ACT_OPT_FIRST;
Christopher Faulet3e964192018-10-24 11:39:23 +02002829 resume_execution:
Amaury Denoyelle03517732021-05-07 14:25:01 +02002830 if (rule->kw->flags & KWF_EXPERIMENTAL)
2831 mark_tainted(TAINTED_ACTION_EXP_EXECUTED);
2832
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002833 /* Always call the action function if defined */
2834 if (rule->action_ptr) {
2835 if ((s->req.flags & CF_READ_ERROR) ||
2836 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
2837 (px->options & PR_O_ABRT_CLOSE)))
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002838 act_opts |= ACT_OPT_FINAL;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002839
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002840 switch (rule->action_ptr(rule, px, sess, s, act_opts)) {
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002841 case ACT_RET_CONT:
2842 break;
2843 case ACT_RET_STOP:
2844 rule_ret = HTTP_RULE_RES_STOP;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002845 s->last_rule_file = rule->conf.file;
2846 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002847 goto end;
2848 case ACT_RET_YIELD:
2849 s->current_rule = rule;
2850 rule_ret = HTTP_RULE_RES_YIELD;
2851 goto end;
2852 case ACT_RET_ERR:
2853 rule_ret = HTTP_RULE_RES_ERROR;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002854 s->last_rule_file = rule->conf.file;
2855 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002856 goto end;
2857 case ACT_RET_DONE:
2858 rule_ret = HTTP_RULE_RES_DONE;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002859 s->last_rule_file = rule->conf.file;
2860 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002861 goto end;
2862 case ACT_RET_DENY:
Christopher Fauletb58f62b2020-01-13 16:40:13 +01002863 if (txn->status == -1)
2864 txn->status = 403;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002865 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 Fauletcd26e8a2019-12-18 11:13:39 +01002868 goto end;
2869 case ACT_RET_ABRT:
2870 rule_ret = HTTP_RULE_RES_ABRT;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002871 s->last_rule_file = rule->conf.file;
2872 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002873 goto end;
2874 case ACT_RET_INV:
2875 rule_ret = HTTP_RULE_RES_BADREQ;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002876 s->last_rule_file = rule->conf.file;
2877 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002878 goto end;
2879 }
2880 continue; /* eval the next rule */
2881 }
2882
2883 /* If not action function defined, check for known actions */
Christopher Faulet3e964192018-10-24 11:39:23 +02002884 switch (rule->action) {
2885 case ACT_ACTION_ALLOW:
2886 rule_ret = HTTP_RULE_RES_STOP;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002887 s->last_rule_file = rule->conf.file;
2888 s->last_rule_line = rule->conf.line;
Christopher Faulet3e964192018-10-24 11:39:23 +02002889 goto end;
2890
2891 case ACT_ACTION_DENY:
Christopher Faulet5cb513a2020-05-13 17:56:56 +02002892 txn->status = rule->arg.http_reply->status;
2893 txn->http_reply = rule->arg.http_reply;
Christopher Faulet3e964192018-10-24 11:39:23 +02002894 rule_ret = HTTP_RULE_RES_DENY;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002895 s->last_rule_file = rule->conf.file;
2896 s->last_rule_line = rule->conf.line;
Christopher Faulet3e964192018-10-24 11:39:23 +02002897 goto end;
2898
2899 case ACT_HTTP_REQ_TARPIT:
2900 txn->flags |= TX_CLTARPIT;
Christopher Faulet5cb513a2020-05-13 17:56:56 +02002901 txn->status = rule->arg.http_reply->status;
2902 txn->http_reply = rule->arg.http_reply;
Christopher Faulet3e964192018-10-24 11:39:23 +02002903 rule_ret = HTTP_RULE_RES_DENY;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002904 s->last_rule_file = rule->conf.file;
2905 s->last_rule_line = rule->conf.line;
Christopher Faulet3e964192018-10-24 11:39:23 +02002906 goto end;
2907
Willy Tarreaubc1223b2021-09-02 16:54:33 +02002908 case ACT_HTTP_REDIR: {
2909 int ret = http_apply_redirect_rule(rule->arg.redir, s, txn);
2910
2911 if (ret == 2) // 2 == skip
2912 break;
2913
2914 rule_ret = ret ? HTTP_RULE_RES_ABRT : HTTP_RULE_RES_ERROR;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002915 s->last_rule_file = rule->conf.file;
2916 s->last_rule_line = rule->conf.line;
Christopher Faulet3e964192018-10-24 11:39:23 +02002917 goto end;
Willy Tarreaubc1223b2021-09-02 16:54:33 +02002918 }
Christopher Faulet3e964192018-10-24 11:39:23 +02002919
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002920 /* other flags exists, but normally, they never be matched. */
Christopher Faulet3e964192018-10-24 11:39:23 +02002921 default:
2922 break;
2923 }
2924 }
2925
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002926 if (def_rules && s->current_rule_list == def_rules) {
2927 s->current_rule_list = rules;
2928 goto restart;
2929 }
2930
Christopher Faulet3e964192018-10-24 11:39:23 +02002931 end:
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002932 /* if the ruleset evaluation is finished reset the strict mode */
Christopher Faulet46f95542019-12-20 10:07:22 +01002933 if (rule_ret != HTTP_RULE_RES_YIELD)
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002934 txn->req.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01002935
Christopher Faulet3e964192018-10-24 11:39:23 +02002936 /* we reached the end of the rules, nothing to report */
2937 return rule_ret;
2938}
2939
2940/* Executes the http-response rules <rules> for stream <s> and proxy <px>. It
2941 * returns one of 5 possible statuses: HTTP_RULE_RES_CONT, HTTP_RULE_RES_STOP,
2942 * HTTP_RULE_RES_DONE, HTTP_RULE_RES_YIELD, or HTTP_RULE_RES_BADREQ. If *CONT
2943 * is returned, the process can continue the evaluation of next rule list. If
2944 * *STOP or *DONE is returned, the process must stop the evaluation. If *BADREQ
2945 * is returned, it means the operation could not be processed and a server error
Christopher Fauleta53abad2020-05-13 08:12:22 +02002946 * must be returned. If *YIELD is returned, the caller must call again the
2947 * function with the same context.
Christopher Faulet3e964192018-10-24 11:39:23 +02002948 */
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002949static enum rule_result http_res_get_intercept_rule(struct proxy *px, struct list *def_rules,
2950 struct list *rules, struct stream *s)
Christopher Faulet3e964192018-10-24 11:39:23 +02002951{
2952 struct session *sess = strm_sess(s);
2953 struct http_txn *txn = s->txn;
Christopher Faulet3e964192018-10-24 11:39:23 +02002954 struct act_rule *rule;
Christopher Faulet3e964192018-10-24 11:39:23 +02002955 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002956 int act_opts = 0;
Christopher Faulet3e964192018-10-24 11:39:23 +02002957
Christopher Faulet3e964192018-10-24 11:39:23 +02002958 /* If "the current_rule_list" match the executed rule list, we are in
2959 * resume condition. If a resume is needed it is always in the action
2960 * and never in the ACL or converters. In this case, we initialise the
2961 * current rule, and go to the action execution point.
2962 */
2963 if (s->current_rule) {
2964 rule = s->current_rule;
2965 s->current_rule = NULL;
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002966 if (s->current_rule_list == rules || (def_rules && s->current_rule_list == def_rules))
Christopher Faulet3e964192018-10-24 11:39:23 +02002967 goto resume_execution;
2968 }
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002969 s->current_rule_list = ((!def_rules || s->current_rule_list == def_rules) ? rules : def_rules);
2970
2971 restart:
Christopher Faulet3e964192018-10-24 11:39:23 +02002972
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002973 /* start the ruleset evaluation in strict mode */
2974 txn->rsp.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01002975
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002976 list_for_each_entry(rule, s->current_rule_list, list) {
Christopher Faulet3e964192018-10-24 11:39:23 +02002977 /* check optional condition */
2978 if (rule->cond) {
2979 int ret;
2980
2981 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
2982 ret = acl_pass(ret);
2983
2984 if (rule->cond->pol == ACL_COND_UNLESS)
2985 ret = !ret;
2986
2987 if (!ret) /* condition not matched */
2988 continue;
2989 }
2990
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002991 act_opts |= ACT_OPT_FIRST;
Christopher Faulet3e964192018-10-24 11:39:23 +02002992resume_execution:
Amaury Denoyelle03517732021-05-07 14:25:01 +02002993 if (rule->kw->flags & KWF_EXPERIMENTAL)
2994 mark_tainted(TAINTED_ACTION_EXP_EXECUTED);
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002995
2996 /* Always call the action function if defined */
2997 if (rule->action_ptr) {
2998 if ((s->req.flags & CF_READ_ERROR) ||
2999 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
3000 (px->options & PR_O_ABRT_CLOSE)))
Christopher Faulet105ba6c2019-12-18 14:41:51 +01003001 act_opts |= ACT_OPT_FINAL;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01003002
Christopher Faulet105ba6c2019-12-18 14:41:51 +01003003 switch (rule->action_ptr(rule, px, sess, s, act_opts)) {
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01003004 case ACT_RET_CONT:
3005 break;
3006 case ACT_RET_STOP:
3007 rule_ret = HTTP_RULE_RES_STOP;
Willy Tarreauc6dae862022-03-09 17:23:10 +01003008 s->last_rule_file = rule->conf.file;
3009 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01003010 goto end;
3011 case ACT_RET_YIELD:
3012 s->current_rule = rule;
3013 rule_ret = HTTP_RULE_RES_YIELD;
3014 goto end;
3015 case ACT_RET_ERR:
3016 rule_ret = HTTP_RULE_RES_ERROR;
Willy Tarreauc6dae862022-03-09 17:23:10 +01003017 s->last_rule_file = rule->conf.file;
3018 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01003019 goto end;
3020 case ACT_RET_DONE:
3021 rule_ret = HTTP_RULE_RES_DONE;
Willy Tarreauc6dae862022-03-09 17:23:10 +01003022 s->last_rule_file = rule->conf.file;
3023 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01003024 goto end;
3025 case ACT_RET_DENY:
Christopher Fauletb58f62b2020-01-13 16:40:13 +01003026 if (txn->status == -1)
3027 txn->status = 502;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01003028 rule_ret = HTTP_RULE_RES_DENY;
Willy Tarreauc6dae862022-03-09 17:23:10 +01003029 s->last_rule_file = rule->conf.file;
3030 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01003031 goto end;
3032 case ACT_RET_ABRT:
3033 rule_ret = HTTP_RULE_RES_ABRT;
Willy Tarreauc6dae862022-03-09 17:23:10 +01003034 s->last_rule_file = rule->conf.file;
3035 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01003036 goto end;
3037 case ACT_RET_INV:
3038 rule_ret = HTTP_RULE_RES_BADREQ;
Willy Tarreauc6dae862022-03-09 17:23:10 +01003039 s->last_rule_file = rule->conf.file;
3040 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01003041 goto end;
3042 }
3043 continue; /* eval the next rule */
3044 }
3045
3046 /* If not action function defined, check for known actions */
Christopher Faulet3e964192018-10-24 11:39:23 +02003047 switch (rule->action) {
3048 case ACT_ACTION_ALLOW:
3049 rule_ret = HTTP_RULE_RES_STOP; /* "allow" rules are OK */
Willy Tarreauc6dae862022-03-09 17:23:10 +01003050 s->last_rule_file = rule->conf.file;
3051 s->last_rule_line = rule->conf.line;
Christopher Faulet3e964192018-10-24 11:39:23 +02003052 goto end;
3053
3054 case ACT_ACTION_DENY:
Christopher Faulet5cb513a2020-05-13 17:56:56 +02003055 txn->status = rule->arg.http_reply->status;
3056 txn->http_reply = rule->arg.http_reply;
Christopher Faulet3a26bee2019-12-16 12:47:40 +01003057 rule_ret = HTTP_RULE_RES_DENY;
Willy Tarreauc6dae862022-03-09 17:23:10 +01003058 s->last_rule_file = rule->conf.file;
3059 s->last_rule_line = rule->conf.line;
Christopher Faulet3e964192018-10-24 11:39:23 +02003060 goto end;
3061
Willy Tarreaubc1223b2021-09-02 16:54:33 +02003062 case ACT_HTTP_REDIR: {
3063 int ret = http_apply_redirect_rule(rule->arg.redir, s, txn);
Christopher Faulet3e964192018-10-24 11:39:23 +02003064
Willy Tarreaubc1223b2021-09-02 16:54:33 +02003065 if (ret == 2) // 2 == skip
3066 break;
3067
3068 rule_ret = ret ? HTTP_RULE_RES_ABRT : HTTP_RULE_RES_ERROR;
Willy Tarreauc6dae862022-03-09 17:23:10 +01003069 s->last_rule_file = rule->conf.file;
3070 s->last_rule_line = rule->conf.line;
Willy Tarreaubc1223b2021-09-02 16:54:33 +02003071 goto end;
3072 }
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01003073 /* other flags exists, but normally, they never be matched. */
Christopher Faulet3e964192018-10-24 11:39:23 +02003074 default:
3075 break;
3076 }
3077 }
3078
Christopher Fauletd4150ad2021-10-13 15:35:55 +02003079 if (def_rules && s->current_rule_list == def_rules) {
3080 s->current_rule_list = rules;
3081 goto restart;
3082 }
3083
Christopher Faulet3e964192018-10-24 11:39:23 +02003084 end:
Christopher Faulet1aea50e2020-01-17 16:03:53 +01003085 /* if the ruleset evaluation is finished reset the strict mode */
Christopher Faulet46f95542019-12-20 10:07:22 +01003086 if (rule_ret != HTTP_RULE_RES_YIELD)
Christopher Faulet1aea50e2020-01-17 16:03:53 +01003087 txn->rsp.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01003088
Christopher Faulet3e964192018-10-24 11:39:23 +02003089 /* we reached the end of the rules, nothing to report */
3090 return rule_ret;
3091}
3092
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01003093/* Executes backend and frontend http-after-response rules for the stream <s>,
3094 * in that order. it return 1 on success and 0 on error. It is the caller
3095 * responsibility to catch error or ignore it. If it catches it, this function
3096 * may be called a second time, for the internal error.
3097 */
3098int http_eval_after_res_rules(struct stream *s)
3099{
Christopher Fauletd4150ad2021-10-13 15:35:55 +02003100 struct list *def_rules, *rules;
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01003101 struct session *sess = s->sess;
3102 enum rule_result ret = HTTP_RULE_RES_CONT;
3103
Christopher Faulet507479b2020-05-15 12:29:46 +02003104 /* Eval after-response ruleset only if the reply is not const */
3105 if (s->txn->flags & TX_CONST_REPLY)
3106 goto end;
3107
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01003108 /* prune the request variables if not already done and swap to the response variables. */
3109 if (s->vars_reqres.scope != SCOPE_RES) {
3110 if (!LIST_ISEMPTY(&s->vars_reqres.head))
3111 vars_prune(&s->vars_reqres, s->sess, s);
Willy Tarreaub7bfcb32021-08-31 08:13:25 +02003112 vars_init_head(&s->vars_reqres, SCOPE_RES);
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01003113 }
3114
Christopher Fauletd4150ad2021-10-13 15:35:55 +02003115 def_rules = (s->be->defpx ? &s->be->defpx->http_after_res_rules : NULL);
3116 rules = &s->be->http_after_res_rules;
3117
3118 ret = http_res_get_intercept_rule(s->be, def_rules, rules, s);
Christopher Faulet4c5a5912021-11-09 17:48:39 +01003119 if ((ret == HTTP_RULE_RES_CONT || ret == HTTP_RULE_RES_STOP) && sess->fe != s->be) {
Christopher Fauletd4150ad2021-10-13 15:35:55 +02003120 def_rules = ((sess->fe->defpx && sess->fe->defpx != s->be->defpx) ? &sess->fe->defpx->http_after_res_rules : NULL);
3121 rules = &sess->fe->http_after_res_rules;
3122 ret = http_res_get_intercept_rule(sess->fe, def_rules, rules, s);
3123 }
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01003124
Christopher Faulet507479b2020-05-15 12:29:46 +02003125 end:
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01003126 /* All other codes than CONTINUE, STOP or DONE are forbidden */
3127 return (ret == HTTP_RULE_RES_CONT || ret == HTTP_RULE_RES_STOP || ret == HTTP_RULE_RES_DONE);
3128}
3129
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003130/*
3131 * Manage client-side cookie. It can impact performance by about 2% so it is
3132 * desirable to call it only when needed. This code is quite complex because
3133 * of the multiple very crappy and ambiguous syntaxes we have to support. it
3134 * highly recommended not to touch this part without a good reason !
3135 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003136static void http_manage_client_side_cookies(struct stream *s, struct channel *req)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003137{
3138 struct session *sess = s->sess;
3139 struct http_txn *txn = s->txn;
3140 struct htx *htx;
3141 struct http_hdr_ctx ctx;
3142 char *hdr_beg, *hdr_end, *del_from;
3143 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
3144 int preserve_hdr;
3145
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003146 htx = htxbuf(&req->buf);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003147 ctx.blk = NULL;
3148 while (http_find_header(htx, ist("Cookie"), &ctx, 1)) {
Olivier Houchardf0f42382019-07-22 17:43:46 +02003149 int is_first = 1;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003150 del_from = NULL; /* nothing to be deleted */
3151 preserve_hdr = 0; /* assume we may kill the whole header */
3152
3153 /* Now look for cookies. Conforming to RFC2109, we have to support
3154 * attributes whose name begin with a '$', and associate them with
3155 * the right cookie, if we want to delete this cookie.
3156 * So there are 3 cases for each cookie read :
3157 * 1) it's a special attribute, beginning with a '$' : ignore it.
3158 * 2) it's a server id cookie that we *MAY* want to delete : save
3159 * some pointers on it (last semi-colon, beginning of cookie...)
3160 * 3) it's an application cookie : we *MAY* have to delete a previous
3161 * "special" cookie.
3162 * At the end of loop, if a "special" cookie remains, we may have to
3163 * remove it. If no application cookie persists in the header, we
3164 * *MUST* delete it.
3165 *
3166 * Note: RFC2965 is unclear about the processing of spaces around
3167 * the equal sign in the ATTR=VALUE form. A careful inspection of
3168 * the RFC explicitly allows spaces before it, and not within the
3169 * tokens (attrs or values). An inspection of RFC2109 allows that
3170 * too but section 10.1.3 lets one think that spaces may be allowed
3171 * after the equal sign too, resulting in some (rare) buggy
3172 * implementations trying to do that. So let's do what servers do.
3173 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
3174 * allowed quoted strings in values, with any possible character
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003175 * after a backslash, including control chars and delimiters, which
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003176 * causes parsing to become ambiguous. Browsers also allow spaces
3177 * within values even without quotes.
3178 *
3179 * We have to keep multiple pointers in order to support cookie
3180 * removal at the beginning, middle or end of header without
3181 * corrupting the header. All of these headers are valid :
3182 *
3183 * hdr_beg hdr_end
3184 * | |
3185 * v |
3186 * NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3 |
3187 * NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3 v
3188 * NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3
3189 * | | | | | | |
3190 * | | | | | | |
3191 * | | | | | | +--> next
3192 * | | | | | +----> val_end
3193 * | | | | +-----------> val_beg
3194 * | | | +--------------> equal
3195 * | | +----------------> att_end
3196 * | +---------------------> att_beg
3197 * +--------------------------> prev
3198 *
3199 */
3200 hdr_beg = ctx.value.ptr;
3201 hdr_end = hdr_beg + ctx.value.len;
3202 for (prev = hdr_beg; prev < hdr_end; prev = next) {
3203 /* Iterate through all cookies on this line */
3204
3205 /* find att_beg */
3206 att_beg = prev;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003207 if (!is_first)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003208 att_beg++;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003209 is_first = 0;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003210
3211 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
3212 att_beg++;
3213
3214 /* find att_end : this is the first character after the last non
3215 * space before the equal. It may be equal to hdr_end.
3216 */
3217 equal = att_end = att_beg;
3218 while (equal < hdr_end) {
3219 if (*equal == '=' || *equal == ',' || *equal == ';')
3220 break;
3221 if (HTTP_IS_SPHT(*equal++))
3222 continue;
3223 att_end = equal;
3224 }
3225
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003226 /* here, <equal> points to '=', a delimiter or the end. <att_end>
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003227 * is between <att_beg> and <equal>, both may be identical.
3228 */
3229 /* look for end of cookie if there is an equal sign */
3230 if (equal < hdr_end && *equal == '=') {
3231 /* look for the beginning of the value */
3232 val_beg = equal + 1;
3233 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
3234 val_beg++;
3235
3236 /* find the end of the value, respecting quotes */
3237 next = http_find_cookie_value_end(val_beg, hdr_end);
3238
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003239 /* make val_end point to the first white space or delimiter after the value */
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003240 val_end = next;
3241 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
3242 val_end--;
3243 }
3244 else
3245 val_beg = val_end = next = equal;
3246
3247 /* We have nothing to do with attributes beginning with
3248 * '$'. However, they will automatically be removed if a
3249 * header before them is removed, since they're supposed
3250 * to be linked together.
3251 */
3252 if (*att_beg == '$')
3253 continue;
3254
3255 /* Ignore cookies with no equal sign */
3256 if (equal == next) {
3257 /* This is not our cookie, so we must preserve it. But if we already
3258 * scheduled another cookie for removal, we cannot remove the
3259 * complete header, but we can remove the previous block itself.
3260 */
3261 preserve_hdr = 1;
3262 if (del_from != NULL) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003263 int delta = http_del_hdr_value(hdr_beg, hdr_end, &del_from, prev);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003264 val_end += delta;
3265 next += delta;
3266 hdr_end += delta;
3267 prev = del_from;
3268 del_from = NULL;
3269 }
3270 continue;
3271 }
3272
3273 /* if there are spaces around the equal sign, we need to
3274 * strip them otherwise we'll get trouble for cookie captures,
3275 * or even for rewrites. Since this happens extremely rarely,
3276 * it does not hurt performance.
3277 */
3278 if (unlikely(att_end != equal || val_beg > equal + 1)) {
3279 int stripped_before = 0;
3280 int stripped_after = 0;
3281
3282 if (att_end != equal) {
3283 memmove(att_end, equal, hdr_end - equal);
3284 stripped_before = (att_end - equal);
3285 equal += stripped_before;
3286 val_beg += stripped_before;
3287 }
3288
3289 if (val_beg > equal + 1) {
3290 memmove(equal + 1, val_beg, hdr_end + stripped_before - val_beg);
3291 stripped_after = (equal + 1) - val_beg;
3292 val_beg += stripped_after;
3293 stripped_before += stripped_after;
3294 }
3295
3296 val_end += stripped_before;
3297 next += stripped_before;
3298 hdr_end += stripped_before;
3299 }
3300 /* now everything is as on the diagram above */
3301
3302 /* First, let's see if we want to capture this cookie. We check
3303 * that we don't already have a client side cookie, because we
3304 * can only capture one. Also as an optimisation, we ignore
3305 * cookies shorter than the declared name.
3306 */
3307 if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL &&
3308 (val_end - att_beg >= sess->fe->capture_namelen) &&
3309 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
3310 int log_len = val_end - att_beg;
3311
3312 if ((txn->cli_cookie = pool_alloc(pool_head_capture)) == NULL) {
3313 ha_alert("HTTP logging : out of memory.\n");
3314 } else {
3315 if (log_len > sess->fe->capture_len)
3316 log_len = sess->fe->capture_len;
3317 memcpy(txn->cli_cookie, att_beg, log_len);
3318 txn->cli_cookie[log_len] = 0;
3319 }
3320 }
3321
3322 /* Persistence cookies in passive, rewrite or insert mode have the
3323 * following form :
3324 *
3325 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
3326 *
3327 * For cookies in prefix mode, the form is :
3328 *
3329 * Cookie: NAME=SRV~VALUE
3330 */
3331 if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
3332 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
3333 struct server *srv = s->be->srv;
3334 char *delim;
3335
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003336 /* if we're in cookie prefix mode, we'll search the delimiter so that we
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003337 * have the server ID between val_beg and delim, and the original cookie between
3338 * delim+1 and val_end. Otherwise, delim==val_end :
3339 *
3340 * hdr_beg
3341 * |
3342 * v
3343 * NAME=SRV; # in all but prefix modes
3344 * NAME=SRV~OPAQUE ; # in prefix mode
3345 * || || | |+-> next
3346 * || || | +--> val_end
3347 * || || +---------> delim
3348 * || |+------------> val_beg
3349 * || +-------------> att_end = equal
3350 * |+-----------------> att_beg
3351 * +------------------> prev
3352 *
3353 */
3354 if (s->be->ck_opts & PR_CK_PFX) {
3355 for (delim = val_beg; delim < val_end; delim++)
3356 if (*delim == COOKIE_DELIM)
3357 break;
3358 }
3359 else {
3360 char *vbar1;
3361 delim = val_end;
3362 /* Now check if the cookie contains a date field, which would
3363 * appear after a vertical bar ('|') just after the server name
3364 * and before the delimiter.
3365 */
3366 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
3367 if (vbar1) {
3368 /* OK, so left of the bar is the server's cookie and
3369 * right is the last seen date. It is a base64 encoded
3370 * 30-bit value representing the UNIX date since the
3371 * epoch in 4-second quantities.
3372 */
3373 int val;
3374 delim = vbar1++;
3375 if (val_end - vbar1 >= 5) {
3376 val = b64tos30(vbar1);
3377 if (val > 0)
3378 txn->cookie_last_date = val << 2;
3379 }
3380 /* look for a second vertical bar */
3381 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
3382 if (vbar1 && (val_end - vbar1 > 5)) {
3383 val = b64tos30(vbar1 + 1);
3384 if (val > 0)
3385 txn->cookie_first_date = val << 2;
3386 }
3387 }
3388 }
3389
3390 /* if the cookie has an expiration date and the proxy wants to check
3391 * it, then we do that now. We first check if the cookie is too old,
3392 * then only if it has expired. We detect strict overflow because the
3393 * time resolution here is not great (4 seconds). Cookies with dates
3394 * in the future are ignored if their offset is beyond one day. This
3395 * allows an admin to fix timezone issues without expiring everyone
3396 * and at the same time avoids keeping unwanted side effects for too
3397 * long.
3398 */
3399 if (txn->cookie_first_date && s->be->cookie_maxlife &&
3400 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) ||
3401 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
3402 txn->flags &= ~TX_CK_MASK;
3403 txn->flags |= TX_CK_OLD;
3404 delim = val_beg; // let's pretend we have not found the cookie
3405 txn->cookie_first_date = 0;
3406 txn->cookie_last_date = 0;
3407 }
3408 else if (txn->cookie_last_date && s->be->cookie_maxidle &&
3409 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) ||
3410 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
3411 txn->flags &= ~TX_CK_MASK;
3412 txn->flags |= TX_CK_EXPIRED;
3413 delim = val_beg; // let's pretend we have not found the cookie
3414 txn->cookie_first_date = 0;
3415 txn->cookie_last_date = 0;
3416 }
3417
3418 /* Here, we'll look for the first running server which supports the cookie.
3419 * This allows to share a same cookie between several servers, for example
3420 * to dedicate backup servers to specific servers only.
3421 * However, to prevent clients from sticking to cookie-less backup server
3422 * when they have incidentely learned an empty cookie, we simply ignore
3423 * empty cookies and mark them as invalid.
3424 * The same behaviour is applied when persistence must be ignored.
3425 */
3426 if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
3427 srv = NULL;
3428
3429 while (srv) {
3430 if (srv->cookie && (srv->cklen == delim - val_beg) &&
3431 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
3432 if ((srv->cur_state != SRV_ST_STOPPED) ||
3433 (s->be->options & PR_O_PERSIST) ||
3434 (s->flags & SF_FORCE_PRST)) {
3435 /* we found the server and we can use it */
3436 txn->flags &= ~TX_CK_MASK;
3437 txn->flags |= (srv->cur_state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
3438 s->flags |= SF_DIRECT | SF_ASSIGNED;
3439 s->target = &srv->obj_type;
3440 break;
3441 } else {
3442 /* we found a server, but it's down,
3443 * mark it as such and go on in case
3444 * another one is available.
3445 */
3446 txn->flags &= ~TX_CK_MASK;
3447 txn->flags |= TX_CK_DOWN;
3448 }
3449 }
3450 srv = srv->next;
3451 }
3452
3453 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
3454 /* no server matched this cookie or we deliberately skipped it */
3455 txn->flags &= ~TX_CK_MASK;
3456 if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
3457 txn->flags |= TX_CK_UNUSED;
3458 else
3459 txn->flags |= TX_CK_INVALID;
3460 }
3461
3462 /* depending on the cookie mode, we may have to either :
3463 * - delete the complete cookie if we're in insert+indirect mode, so that
3464 * the server never sees it ;
3465 * - remove the server id from the cookie value, and tag the cookie as an
Joseph Herlante9d5c722018-11-25 11:00:25 -08003466 * application cookie so that it does not get accidentally removed later,
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003467 * if we're in cookie prefix mode
3468 */
3469 if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
3470 int delta; /* negative */
3471
3472 memmove(val_beg, delim + 1, hdr_end - (delim + 1));
3473 delta = val_beg - (delim + 1);
3474 val_end += delta;
3475 next += delta;
3476 hdr_end += delta;
3477 del_from = NULL;
3478 preserve_hdr = 1; /* we want to keep this cookie */
3479 }
3480 else if (del_from == NULL &&
3481 (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
3482 del_from = prev;
3483 }
3484 }
3485 else {
3486 /* This is not our cookie, so we must preserve it. But if we already
3487 * scheduled another cookie for removal, we cannot remove the
3488 * complete header, but we can remove the previous block itself.
3489 */
3490 preserve_hdr = 1;
3491
3492 if (del_from != NULL) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003493 int delta = http_del_hdr_value(hdr_beg, hdr_end, &del_from, prev);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003494 if (att_beg >= del_from)
3495 att_beg += delta;
3496 if (att_end >= del_from)
3497 att_end += delta;
3498 val_beg += delta;
3499 val_end += delta;
3500 next += delta;
3501 hdr_end += delta;
3502 prev = del_from;
3503 del_from = NULL;
3504 }
3505 }
3506
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003507 } /* for each cookie */
3508
3509
3510 /* There are no more cookies on this line.
3511 * We may still have one (or several) marked for deletion at the
3512 * end of the line. We must do this now in two ways :
3513 * - if some cookies must be preserved, we only delete from the
3514 * mark to the end of line ;
3515 * - if nothing needs to be preserved, simply delete the whole header
3516 */
3517 if (del_from) {
3518 hdr_end = (preserve_hdr ? del_from : hdr_beg);
3519 }
3520 if ((hdr_end - hdr_beg) != ctx.value.len) {
Christopher Faulet3e2638e2019-06-18 09:49:16 +02003521 if (hdr_beg != hdr_end)
3522 htx_change_blk_value_len(htx, ctx.blk, hdr_end - hdr_beg);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003523 else
3524 http_remove_header(htx, &ctx);
3525 }
3526 } /* for each "Cookie header */
3527}
3528
3529/*
3530 * Manage server-side cookies. It can impact performance by about 2% so it is
3531 * desirable to call it only when needed. This function is also used when we
3532 * just need to know if there is a cookie (eg: for check-cache).
3533 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003534static void http_manage_server_side_cookies(struct stream *s, struct channel *res)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003535{
3536 struct session *sess = s->sess;
3537 struct http_txn *txn = s->txn;
3538 struct htx *htx;
3539 struct http_hdr_ctx ctx;
3540 struct server *srv;
3541 char *hdr_beg, *hdr_end;
3542 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau6f7a02a2019-04-15 21:49:49 +02003543 int is_cookie2 = 0;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003544
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003545 htx = htxbuf(&res->buf);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003546
3547 ctx.blk = NULL;
3548 while (1) {
Olivier Houchardf0f42382019-07-22 17:43:46 +02003549 int is_first = 1;
3550
Andrew McDermottbfb15ab2022-02-11 18:26:49 +00003551 if (is_cookie2 || !http_find_header(htx, ist("Set-Cookie"), &ctx, 1)) {
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003552 if (!http_find_header(htx, ist("Set-Cookie2"), &ctx, 1))
3553 break;
3554 is_cookie2 = 1;
3555 }
3556
3557 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
3558 * <prev> points to the colon.
3559 */
3560 txn->flags |= TX_SCK_PRESENT;
3561
3562 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
3563 * check-cache is enabled) and we are not interested in checking
3564 * them. Warning, the cookie capture is declared in the frontend.
3565 */
3566 if (s->be->cookie_name == NULL && sess->fe->capture_name == NULL)
3567 break;
3568
3569 /* OK so now we know we have to process this response cookie.
3570 * The format of the Set-Cookie header is slightly different
3571 * from the format of the Cookie header in that it does not
3572 * support the comma as a cookie delimiter (thus the header
3573 * cannot be folded) because the Expires attribute described in
3574 * the original Netscape's spec may contain an unquoted date
3575 * with a comma inside. We have to live with this because
3576 * many browsers don't support Max-Age and some browsers don't
3577 * support quoted strings. However the Set-Cookie2 header is
3578 * clean.
3579 *
3580 * We have to keep multiple pointers in order to support cookie
3581 * removal at the beginning, middle or end of header without
3582 * corrupting the header (in case of set-cookie2). A special
3583 * pointer, <scav> points to the beginning of the set-cookie-av
3584 * fields after the first semi-colon. The <next> pointer points
3585 * either to the end of line (set-cookie) or next unquoted comma
3586 * (set-cookie2). All of these headers are valid :
3587 *
3588 * hdr_beg hdr_end
3589 * | |
3590 * v |
3591 * NAME1 = VALUE 1 ; Secure; Path="/" |
3592 * NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT v
3593 * NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT
3594 * NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard
3595 * | | | | | | | |
3596 * | | | | | | | +-> next
3597 * | | | | | | +------------> scav
3598 * | | | | | +--------------> val_end
3599 * | | | | +--------------------> val_beg
3600 * | | | +----------------------> equal
3601 * | | +------------------------> att_end
3602 * | +----------------------------> att_beg
3603 * +------------------------------> prev
3604 * -------------------------------> hdr_beg
3605 */
3606 hdr_beg = ctx.value.ptr;
3607 hdr_end = hdr_beg + ctx.value.len;
3608 for (prev = hdr_beg; prev < hdr_end; prev = next) {
3609
3610 /* Iterate through all cookies on this line */
3611
3612 /* find att_beg */
3613 att_beg = prev;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003614 if (!is_first)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003615 att_beg++;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003616 is_first = 0;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003617
3618 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
3619 att_beg++;
3620
3621 /* find att_end : this is the first character after the last non
3622 * space before the equal. It may be equal to hdr_end.
3623 */
3624 equal = att_end = att_beg;
3625
3626 while (equal < hdr_end) {
3627 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
3628 break;
3629 if (HTTP_IS_SPHT(*equal++))
3630 continue;
3631 att_end = equal;
3632 }
3633
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003634 /* here, <equal> points to '=', a delimiter or the end. <att_end>
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003635 * is between <att_beg> and <equal>, both may be identical.
3636 */
3637
3638 /* look for end of cookie if there is an equal sign */
3639 if (equal < hdr_end && *equal == '=') {
3640 /* look for the beginning of the value */
3641 val_beg = equal + 1;
3642 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
3643 val_beg++;
3644
3645 /* find the end of the value, respecting quotes */
3646 next = http_find_cookie_value_end(val_beg, hdr_end);
3647
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003648 /* make val_end point to the first white space or delimiter after the value */
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003649 val_end = next;
3650 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
3651 val_end--;
3652 }
3653 else {
3654 /* <equal> points to next comma, semi-colon or EOL */
3655 val_beg = val_end = next = equal;
3656 }
3657
3658 if (next < hdr_end) {
3659 /* Set-Cookie2 supports multiple cookies, and <next> points to
3660 * a colon or semi-colon before the end. So skip all attr-value
3661 * pairs and look for the next comma. For Set-Cookie, since
3662 * commas are permitted in values, skip to the end.
3663 */
3664 if (is_cookie2)
3665 next = http_find_hdr_value_end(next, hdr_end);
3666 else
3667 next = hdr_end;
3668 }
3669
3670 /* Now everything is as on the diagram above */
3671
3672 /* Ignore cookies with no equal sign */
3673 if (equal == val_end)
3674 continue;
3675
3676 /* If there are spaces around the equal sign, we need to
3677 * strip them otherwise we'll get trouble for cookie captures,
3678 * or even for rewrites. Since this happens extremely rarely,
3679 * it does not hurt performance.
3680 */
3681 if (unlikely(att_end != equal || val_beg > equal + 1)) {
3682 int stripped_before = 0;
3683 int stripped_after = 0;
3684
3685 if (att_end != equal) {
3686 memmove(att_end, equal, hdr_end - equal);
3687 stripped_before = (att_end - equal);
3688 equal += stripped_before;
3689 val_beg += stripped_before;
3690 }
3691
3692 if (val_beg > equal + 1) {
3693 memmove(equal + 1, val_beg, hdr_end + stripped_before - val_beg);
3694 stripped_after = (equal + 1) - val_beg;
3695 val_beg += stripped_after;
3696 stripped_before += stripped_after;
3697 }
3698
3699 val_end += stripped_before;
3700 next += stripped_before;
3701 hdr_end += stripped_before;
3702
Christopher Faulet3e2638e2019-06-18 09:49:16 +02003703 htx_change_blk_value_len(htx, ctx.blk, hdr_end - hdr_beg);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003704 ctx.value.len = hdr_end - hdr_beg;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003705 }
3706
3707 /* First, let's see if we want to capture this cookie. We check
3708 * that we don't already have a server side cookie, because we
3709 * can only capture one. Also as an optimisation, we ignore
3710 * cookies shorter than the declared name.
3711 */
3712 if (sess->fe->capture_name != NULL &&
3713 txn->srv_cookie == NULL &&
3714 (val_end - att_beg >= sess->fe->capture_namelen) &&
3715 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
3716 int log_len = val_end - att_beg;
3717 if ((txn->srv_cookie = pool_alloc(pool_head_capture)) == NULL) {
3718 ha_alert("HTTP logging : out of memory.\n");
3719 }
3720 else {
3721 if (log_len > sess->fe->capture_len)
3722 log_len = sess->fe->capture_len;
3723 memcpy(txn->srv_cookie, att_beg, log_len);
3724 txn->srv_cookie[log_len] = 0;
3725 }
3726 }
3727
3728 srv = objt_server(s->target);
3729 /* now check if we need to process it for persistence */
3730 if (!(s->flags & SF_IGNORE_PRST) &&
3731 (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
3732 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
3733 /* assume passive cookie by default */
3734 txn->flags &= ~TX_SCK_MASK;
3735 txn->flags |= TX_SCK_FOUND;
3736
3737 /* If the cookie is in insert mode on a known server, we'll delete
3738 * this occurrence because we'll insert another one later.
3739 * We'll delete it too if the "indirect" option is set and we're in
3740 * a direct access.
3741 */
3742 if (s->be->ck_opts & PR_CK_PSV) {
3743 /* The "preserve" flag was set, we don't want to touch the
3744 * server's cookie.
3745 */
3746 }
3747 else if ((srv && (s->be->ck_opts & PR_CK_INS)) ||
3748 ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) {
3749 /* this cookie must be deleted */
3750 if (prev == hdr_beg && next == hdr_end) {
3751 /* whole header */
3752 http_remove_header(htx, &ctx);
3753 /* note: while both invalid now, <next> and <hdr_end>
3754 * are still equal, so the for() will stop as expected.
3755 */
3756 } else {
3757 /* just remove the value */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003758 int delta = http_del_hdr_value(hdr_beg, hdr_end, &prev, next);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003759 next = prev;
3760 hdr_end += delta;
3761 }
3762 txn->flags &= ~TX_SCK_MASK;
3763 txn->flags |= TX_SCK_DELETED;
3764 /* and go on with next cookie */
3765 }
3766 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) {
3767 /* replace bytes val_beg->val_end with the cookie name associated
3768 * with this server since we know it.
3769 */
3770 int sliding, delta;
3771
3772 ctx.value = ist2(val_beg, val_end - val_beg);
3773 ctx.lws_before = ctx.lws_after = 0;
3774 http_replace_header_value(htx, &ctx, ist2(srv->cookie, srv->cklen));
3775 delta = srv->cklen - (val_end - val_beg);
3776 sliding = (ctx.value.ptr - val_beg);
3777 hdr_beg += sliding;
3778 val_beg += sliding;
3779 next += sliding + delta;
3780 hdr_end += sliding + delta;
3781
3782 txn->flags &= ~TX_SCK_MASK;
3783 txn->flags |= TX_SCK_REPLACED;
3784 }
3785 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) {
3786 /* insert the cookie name associated with this server
3787 * before existing cookie, and insert a delimiter between them..
3788 */
3789 int sliding, delta;
3790 ctx.value = ist2(val_beg, 0);
3791 ctx.lws_before = ctx.lws_after = 0;
3792 http_replace_header_value(htx, &ctx, ist2(srv->cookie, srv->cklen + 1));
3793 delta = srv->cklen + 1;
3794 sliding = (ctx.value.ptr - val_beg);
3795 hdr_beg += sliding;
3796 val_beg += sliding;
3797 next += sliding + delta;
3798 hdr_end += sliding + delta;
3799
3800 val_beg[srv->cklen] = COOKIE_DELIM;
3801 txn->flags &= ~TX_SCK_MASK;
3802 txn->flags |= TX_SCK_REPLACED;
3803 }
3804 }
3805 /* that's done for this cookie, check the next one on the same
3806 * line when next != hdr_end (only if is_cookie2).
3807 */
3808 }
3809 }
3810}
3811
Christopher Faulet25a02f62018-10-24 12:00:25 +02003812/*
3813 * Parses the Cache-Control and Pragma request header fields to determine if
3814 * the request may be served from the cache and/or if it is cacheable. Updates
3815 * s->txn->flags.
3816 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003817void http_check_request_for_cacheability(struct stream *s, struct channel *req)
Christopher Faulet25a02f62018-10-24 12:00:25 +02003818{
3819 struct http_txn *txn = s->txn;
3820 struct htx *htx;
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003821 struct http_hdr_ctx ctx = { .blk = NULL };
3822 int pragma_found, cc_found;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003823
3824 if ((txn->flags & (TX_CACHEABLE|TX_CACHE_IGNORE)) == TX_CACHE_IGNORE)
3825 return; /* nothing more to do here */
3826
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003827 htx = htxbuf(&req->buf);
Christopher Faulet25a02f62018-10-24 12:00:25 +02003828 pragma_found = cc_found = 0;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003829
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003830 /* Check "pragma" header for HTTP/1.0 compatibility. */
3831 if (http_find_header(htx, ist("pragma"), &ctx, 1)) {
3832 if (isteqi(ctx.value, ist("no-cache"))) {
3833 pragma_found = 1;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003834 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003835 }
Christopher Faulet25a02f62018-10-24 12:00:25 +02003836
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003837 ctx.blk = NULL;
3838 /* Don't use the cache and don't try to store if we found the
3839 * Authorization header */
3840 if (http_find_header(htx, ist("authorization"), &ctx, 1)) {
3841 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3842 txn->flags |= TX_CACHE_IGNORE;
3843 }
Christopher Faulet25a02f62018-10-24 12:00:25 +02003844
Christopher Faulet25a02f62018-10-24 12:00:25 +02003845
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003846 /* Look for "cache-control" header and iterate over all the values
3847 * until we find one that specifies that caching is possible or not. */
3848 ctx.blk = NULL;
3849 while (http_find_header(htx, ist("cache-control"), &ctx, 0)) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003850 cc_found = 1;
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003851 /* We don't check the values after max-age, max-stale nor min-fresh,
3852 * we simply don't use the cache when they're specified. */
3853 if (istmatchi(ctx.value, ist("max-age")) ||
3854 istmatchi(ctx.value, ist("no-cache")) ||
3855 istmatchi(ctx.value, ist("max-stale")) ||
3856 istmatchi(ctx.value, ist("min-fresh"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003857 txn->flags |= TX_CACHE_IGNORE;
3858 continue;
3859 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003860 if (istmatchi(ctx.value, ist("no-store"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003861 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3862 continue;
3863 }
3864 }
3865
3866 /* RFC7234#5.4:
3867 * When the Cache-Control header field is also present and
3868 * understood in a request, Pragma is ignored.
3869 * When the Cache-Control header field is not present in a
3870 * request, caches MUST consider the no-cache request
3871 * pragma-directive as having the same effect as if
3872 * "Cache-Control: no-cache" were present.
3873 */
3874 if (!cc_found && pragma_found)
3875 txn->flags |= TX_CACHE_IGNORE;
3876}
3877
3878/*
3879 * Check if response is cacheable or not. Updates s->txn->flags.
3880 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003881void http_check_response_for_cacheability(struct stream *s, struct channel *res)
Christopher Faulet25a02f62018-10-24 12:00:25 +02003882{
3883 struct http_txn *txn = s->txn;
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003884 struct http_hdr_ctx ctx = { .blk = NULL };
Christopher Faulet25a02f62018-10-24 12:00:25 +02003885 struct htx *htx;
Remi Tricot-Le Bretoncc9bf2e2020-11-12 11:14:41 +01003886 int has_freshness_info = 0;
3887 int has_validator = 0;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003888
3889 if (txn->status < 200) {
3890 /* do not try to cache interim responses! */
3891 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3892 return;
3893 }
3894
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003895 htx = htxbuf(&res->buf);
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003896 /* Check "pragma" header for HTTP/1.0 compatibility. */
3897 if (http_find_header(htx, ist("pragma"), &ctx, 1)) {
3898 if (isteqi(ctx.value, ist("no-cache"))) {
3899 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3900 return;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003901 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003902 }
Christopher Faulet25a02f62018-10-24 12:00:25 +02003903
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003904 /* Look for "cache-control" header and iterate over all the values
3905 * until we find one that specifies that caching is possible or not. */
3906 ctx.blk = NULL;
3907 while (http_find_header(htx, ist("cache-control"), &ctx, 0)) {
3908 if (isteqi(ctx.value, ist("public"))) {
3909 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003910 continue;
3911 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003912 if (isteqi(ctx.value, ist("private")) ||
3913 isteqi(ctx.value, ist("no-cache")) ||
3914 isteqi(ctx.value, ist("no-store")) ||
3915 isteqi(ctx.value, ist("max-age=0")) ||
3916 isteqi(ctx.value, ist("s-maxage=0"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003917 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003918 continue;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003919 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003920 /* We might have a no-cache="set-cookie" form. */
3921 if (istmatchi(ctx.value, ist("no-cache=\"set-cookie"))) {
3922 txn->flags &= ~TX_CACHE_COOK;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003923 continue;
3924 }
Remi Tricot-Le Bretoncc9bf2e2020-11-12 11:14:41 +01003925
3926 if (istmatchi(ctx.value, ist("s-maxage")) ||
3927 istmatchi(ctx.value, ist("max-age"))) {
3928 has_freshness_info = 1;
3929 continue;
3930 }
3931 }
3932
3933 /* If no freshness information could be found in Cache-Control values,
3934 * look for an Expires header. */
3935 if (!has_freshness_info) {
3936 ctx.blk = NULL;
3937 has_freshness_info = http_find_header(htx, ist("expires"), &ctx, 0);
Christopher Faulet25a02f62018-10-24 12:00:25 +02003938 }
Remi Tricot-Le Bretoncc9bf2e2020-11-12 11:14:41 +01003939
3940 /* If no freshness information could be found in Cache-Control or Expires
3941 * values, look for an explicit validator. */
3942 if (!has_freshness_info) {
3943 ctx.blk = NULL;
3944 has_validator = 1;
3945 if (!http_find_header(htx, ist("etag"), &ctx, 0)) {
3946 ctx.blk = NULL;
3947 if (!http_find_header(htx, ist("last-modified"), &ctx, 0))
3948 has_validator = 0;
3949 }
3950 }
3951
3952 /* We won't store an entry that has neither a cache validator nor an
3953 * explicit expiration time, as suggested in RFC 7234#3. */
3954 if (!has_freshness_info && !has_validator)
3955 txn->flags |= TX_CACHE_IGNORE;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003956}
3957
Christopher Faulet377c5a52018-10-24 21:21:30 +02003958/*
3959 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
3960 * for the current backend.
3961 *
3962 * It is assumed that the request is either a HEAD, GET, or POST and that the
3963 * uri_auth field is valid.
3964 *
3965 * Returns 1 if stats should be provided, otherwise 0.
3966 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003967static int http_stats_check_uri(struct stream *s, struct http_txn *txn, struct proxy *backend)
Christopher Faulet377c5a52018-10-24 21:21:30 +02003968{
3969 struct uri_auth *uri_auth = backend->uri_auth;
3970 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003971 struct htx_sl *sl;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003972 struct ist uri;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003973
3974 if (!uri_auth)
3975 return 0;
3976
3977 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
3978 return 0;
3979
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003980 htx = htxbuf(&s->req.buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02003981 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003982 uri = htx_sl_req_uri(sl);
Amaury Denoyellec453f952021-07-06 11:40:12 +02003983 if (*uri_auth->uri_prefix == '/') {
3984 struct http_uri_parser parser = http_uri_parser_init(uri);
3985 uri = http_parse_path(&parser);
3986 }
Christopher Faulet377c5a52018-10-24 21:21:30 +02003987
3988 /* check URI size */
3989 if (uri_auth->uri_len > uri.len)
3990 return 0;
3991
3992 if (memcmp(uri.ptr, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
3993 return 0;
3994
3995 return 1;
3996}
3997
3998/* This function prepares an applet to handle the stats. It can deal with the
3999 * "100-continue" expectation, check that admin rules are met for POST requests,
4000 * and program a response message if something was unexpected. It cannot fail
4001 * and always relies on the stats applet to complete the job. It does not touch
4002 * analysers nor counters, which are left to the caller. It does not touch
4003 * s->target which is supposed to already point to the stats applet. The caller
4004 * is expected to have already assigned an appctx to the stream.
4005 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004006static int http_handle_stats(struct stream *s, struct channel *req)
Christopher Faulet377c5a52018-10-24 21:21:30 +02004007{
4008 struct stats_admin_rule *stats_admin_rule;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004009 struct session *sess = s->sess;
4010 struct http_txn *txn = s->txn;
4011 struct http_msg *msg = &txn->req;
4012 struct uri_auth *uri_auth = s->be->uri_auth;
4013 const char *h, *lookup, *end;
Willy Tarreau8e7c6e62022-05-18 17:58:02 +02004014 struct appctx *appctx = __sc_appctx(s->scb);
Willy Tarreau91cefca2022-05-03 17:08:29 +02004015 struct show_stat_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx));
Christopher Faulet377c5a52018-10-24 21:21:30 +02004016 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004017 struct htx_sl *sl;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004018
Willy Tarreau41f88522022-05-03 18:39:27 +02004019 appctx->st1 = 0;
Willy Tarreau6ef16482022-05-06 18:07:53 +02004020 ctx->state = STAT_STATE_INIT;
Willy Tarreau91cefca2022-05-03 17:08:29 +02004021 ctx->st_code = STAT_STATUS_INIT;
4022 ctx->flags |= uri_auth->flags;
4023 ctx->flags |= STAT_FMT_HTML; /* assume HTML mode by default */
Christopher Faulet377c5a52018-10-24 21:21:30 +02004024 if ((msg->flags & HTTP_MSGF_VER_11) && (txn->meth != HTTP_METH_HEAD))
Willy Tarreau91cefca2022-05-03 17:08:29 +02004025 ctx->flags |= STAT_CHUNKED;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004026
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004027 htx = htxbuf(&req->buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02004028 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004029 lookup = HTX_SL_REQ_UPTR(sl) + uri_auth->uri_len;
4030 end = HTX_SL_REQ_UPTR(sl) + HTX_SL_REQ_ULEN(sl);
Christopher Faulet377c5a52018-10-24 21:21:30 +02004031
4032 for (h = lookup; h <= end - 3; h++) {
4033 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02004034 ctx->flags |= STAT_HIDE_DOWN;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004035 break;
4036 }
Amaury Denoyelle91e55ea2021-02-25 14:46:08 +01004037 }
4038
4039 for (h = lookup; h <= end - 9; h++) {
4040 if (memcmp(h, ";no-maint", 9) == 0) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02004041 ctx->flags |= STAT_HIDE_MAINT;
Willy Tarreau3e320362020-10-23 17:28:57 +02004042 break;
4043 }
Christopher Faulet377c5a52018-10-24 21:21:30 +02004044 }
4045
4046 if (uri_auth->refresh) {
4047 for (h = lookup; h <= end - 10; h++) {
4048 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02004049 ctx->flags |= STAT_NO_REFRESH;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004050 break;
4051 }
4052 }
4053 }
4054
4055 for (h = lookup; h <= end - 4; h++) {
4056 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02004057 ctx->flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
Christopher Faulet377c5a52018-10-24 21:21:30 +02004058 break;
4059 }
4060 }
4061
4062 for (h = lookup; h <= end - 6; h++) {
4063 if (memcmp(h, ";typed", 6) == 0) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02004064 ctx->flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
4065 ctx->flags |= STAT_FMT_TYPED;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004066 break;
4067 }
4068 }
4069
Christopher Faulet6338a082019-09-09 15:50:54 +02004070 for (h = lookup; h <= end - 5; h++) {
4071 if (memcmp(h, ";json", 5) == 0) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02004072 ctx->flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
4073 ctx->flags |= STAT_FMT_JSON;
Christopher Faulet6338a082019-09-09 15:50:54 +02004074 break;
4075 }
4076 }
4077
4078 for (h = lookup; h <= end - 12; h++) {
4079 if (memcmp(h, ";json-schema", 12) == 0) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02004080 ctx->flags &= ~STAT_FMT_MASK;
4081 ctx->flags |= STAT_JSON_SCHM;
Christopher Faulet6338a082019-09-09 15:50:54 +02004082 break;
4083 }
4084 }
4085
Christopher Faulet377c5a52018-10-24 21:21:30 +02004086 for (h = lookup; h <= end - 8; h++) {
4087 if (memcmp(h, ";st=", 4) == 0) {
4088 int i;
4089 h += 4;
Willy Tarreau91cefca2022-05-03 17:08:29 +02004090 ctx->st_code = STAT_STATUS_UNKN;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004091 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
4092 if (strncmp(stat_status_codes[i], h, 4) == 0) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02004093 ctx->st_code = i;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004094 break;
4095 }
4096 }
4097 break;
4098 }
4099 }
4100
Willy Tarreau91cefca2022-05-03 17:08:29 +02004101 ctx->scope_str = 0;
4102 ctx->scope_len = 0;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004103 for (h = lookup; h <= end - 8; h++) {
4104 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
4105 int itx = 0;
4106 const char *h2;
4107 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
4108 const char *err;
4109
4110 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
4111 h2 = h;
Willy Tarreau91cefca2022-05-03 17:08:29 +02004112 ctx->scope_str = h2 - HTX_SL_REQ_UPTR(sl);
Christopher Fauleted7a0662019-01-14 11:07:34 +01004113 while (h < end) {
Christopher Faulet377c5a52018-10-24 21:21:30 +02004114 if (*h == ';' || *h == '&' || *h == ' ')
4115 break;
4116 itx++;
4117 h++;
4118 }
4119
4120 if (itx > STAT_SCOPE_TXT_MAXLEN)
4121 itx = STAT_SCOPE_TXT_MAXLEN;
Willy Tarreau91cefca2022-05-03 17:08:29 +02004122 ctx->scope_len = itx;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004123
Willy Tarreau91cefca2022-05-03 17:08:29 +02004124 /* scope_txt = search query, ctx->scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Christopher Faulet377c5a52018-10-24 21:21:30 +02004125 memcpy(scope_txt, h2, itx);
4126 scope_txt[itx] = '\0';
4127 err = invalid_char(scope_txt);
4128 if (err) {
4129 /* bad char in search text => clear scope */
Willy Tarreau91cefca2022-05-03 17:08:29 +02004130 ctx->scope_str = 0;
4131 ctx->scope_len = 0;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004132 }
4133 break;
4134 }
4135 }
4136
4137 /* now check whether we have some admin rules for this request */
4138 list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) {
4139 int ret = 1;
4140
4141 if (stats_admin_rule->cond) {
4142 ret = acl_exec_cond(stats_admin_rule->cond, s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
4143 ret = acl_pass(ret);
4144 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
4145 ret = !ret;
4146 }
4147
4148 if (ret) {
4149 /* no rule, or the rule matches */
Willy Tarreau91cefca2022-05-03 17:08:29 +02004150 ctx->flags |= STAT_ADMIN;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004151 break;
4152 }
4153 }
4154
Christopher Faulet5d45e382019-02-27 15:15:23 +01004155 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
4156 appctx->st0 = STAT_HTTP_HEAD;
4157 else if (txn->meth == HTTP_METH_POST) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02004158 if (ctx->flags & STAT_ADMIN) {
Christopher Faulet377c5a52018-10-24 21:21:30 +02004159 appctx->st0 = STAT_HTTP_POST;
Christopher Fauletbd9e8422019-08-15 22:26:48 +02004160 if (msg->msg_state < HTTP_MSG_DATA)
4161 req->analysers |= AN_REQ_HTTP_BODY;
4162 }
Christopher Faulet377c5a52018-10-24 21:21:30 +02004163 else {
Christopher Faulet5d45e382019-02-27 15:15:23 +01004164 /* POST without admin level */
Willy Tarreau91cefca2022-05-03 17:08:29 +02004165 ctx->flags &= ~STAT_CHUNKED;
4166 ctx->st_code = STAT_STATUS_DENY;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004167 appctx->st0 = STAT_HTTP_LAST;
4168 }
4169 }
4170 else {
Christopher Faulet5d45e382019-02-27 15:15:23 +01004171 /* Unsupported method */
Willy Tarreau91cefca2022-05-03 17:08:29 +02004172 ctx->flags &= ~STAT_CHUNKED;
4173 ctx->st_code = STAT_STATUS_IVAL;
Christopher Faulet5d45e382019-02-27 15:15:23 +01004174 appctx->st0 = STAT_HTTP_LAST;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004175 }
4176
4177 s->task->nice = -32; /* small boost for HTTP statistics */
4178 return 1;
4179}
4180
Christopher Faulet021a8e42021-03-29 10:46:38 +02004181/* This function waits for the message payload at most <time> milliseconds (may
4182 * be set to TICK_ETERNITY). It stops to wait if at least <bytes> bytes of the
4183 * payload are received (0 means no limit). It returns HTTP_RULE_* depending on
4184 * the result:
4185 *
4186 * - HTTP_RULE_RES_CONT when conditions are met to stop waiting
4187 * - HTTP_RULE_RES_YIELD to wait for more data
Ilya Shipitsinb2be9a12021-04-24 13:25:42 +05004188 * - HTTP_RULE_RES_ABRT when a timeout occurred.
Christopher Faulet021a8e42021-03-29 10:46:38 +02004189 * - HTTP_RULE_RES_BADREQ if a parsing error is raised by lower level
Ilya Shipitsinb2be9a12021-04-24 13:25:42 +05004190 * - HTTP_RULE_RES_ERROR if an internal error occurred
Christopher Faulet021a8e42021-03-29 10:46:38 +02004191 *
Ilya Shipitsinb2be9a12021-04-24 13:25:42 +05004192 * If a timeout occurred, this function is responsible to emit the right response
Christopher Faulet021a8e42021-03-29 10:46:38 +02004193 * to the client, depending on the channel (408 on request side, 504 on response
4194 * side). All other errors must be handled by the caller.
4195 */
4196enum rule_result http_wait_for_msg_body(struct stream *s, struct channel *chn,
4197 unsigned int time, unsigned int bytes)
4198{
4199 struct session *sess = s->sess;
4200 struct http_txn *txn = s->txn;
4201 struct http_msg *msg = ((chn->flags & CF_ISRESP) ? &txn->rsp : &txn->req);
4202 struct htx *htx;
4203 enum rule_result ret = HTTP_RULE_RES_CONT;
4204
4205 htx = htxbuf(&chn->buf);
4206
4207 if (htx->flags & HTX_FL_PARSING_ERROR) {
4208 ret = HTTP_RULE_RES_BADREQ;
4209 goto end;
4210 }
4211 if (htx->flags & HTX_FL_PROCESSING_ERROR) {
4212 ret = HTTP_RULE_RES_ERROR;
4213 goto end;
4214 }
4215
4216 /* Do nothing for bodyless and CONNECT requests */
4217 if (txn->meth == HTTP_METH_CONNECT || (msg->flags & HTTP_MSGF_BODYLESS))
4218 goto end;
4219
4220 if (!(chn->flags & CF_ISRESP) && msg->msg_state < HTTP_MSG_DATA) {
4221 if (http_handle_expect_hdr(s, htx, msg) == -1) {
4222 ret = HTTP_RULE_RES_ERROR;
4223 goto end;
4224 }
4225 }
4226
4227 msg->msg_state = HTTP_MSG_DATA;
4228
4229 /* Now we're in HTTP_MSG_DATA. We just need to know if all data have
4230 * been received or if the buffer is full.
4231 */
Christopher Faulet78335962021-09-23 14:46:32 +02004232 if ((htx->flags & HTX_FL_EOM) ||
4233 htx_get_tail_type(htx) > HTX_BLK_DATA ||
4234 channel_htx_full(chn, htx, global.tune.maxrewrite) ||
Willy Tarreau99615ed2022-05-25 07:29:36 +02004235 sc_waiting_room(chn_prod(chn)))
Christopher Faulet021a8e42021-03-29 10:46:38 +02004236 goto end;
4237
4238 if (bytes) {
4239 struct htx_blk *blk;
4240 unsigned int len = 0;
4241
4242 for (blk = htx_get_first_blk(htx); blk; blk = htx_get_next_blk(htx, blk)) {
4243 if (htx_get_blk_type(blk) != HTX_BLK_DATA)
4244 continue;
4245 len += htx_get_blksz(blk);
4246 if (len >= bytes)
4247 goto end;
4248 }
4249 }
4250
4251 if ((chn->flags & CF_READ_TIMEOUT) || tick_is_expired(chn->analyse_exp, now_ms)) {
4252 if (!(chn->flags & CF_ISRESP))
4253 goto abort_req;
4254 goto abort_res;
4255 }
4256
4257 /* we get here if we need to wait for more data */
4258 if (!(chn->flags & (CF_SHUTR | CF_READ_ERROR))) {
4259 if (!tick_isset(chn->analyse_exp))
4260 chn->analyse_exp = tick_add_ifset(now_ms, time);
4261 ret = HTTP_RULE_RES_YIELD;
4262 }
4263
4264 end:
4265 return ret;
4266
4267 abort_req:
4268 txn->status = 408;
4269 if (!(s->flags & SF_ERR_MASK))
4270 s->flags |= SF_ERR_CLITO;
4271 if (!(s->flags & SF_FINST_MASK))
4272 s->flags |= SF_FINST_D;
Willy Tarreau4781b152021-04-06 13:53:36 +02004273 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req);
Christopher Faulet021a8e42021-03-29 10:46:38 +02004274 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02004275 _HA_ATOMIC_INC(&sess->listener->counters->failed_req);
Christopher Faulet021a8e42021-03-29 10:46:38 +02004276 http_reply_and_close(s, txn->status, http_error_message(s));
4277 ret = HTTP_RULE_RES_ABRT;
4278 goto end;
4279
4280 abort_res:
4281 txn->status = 504;
4282 if (!(s->flags & SF_ERR_MASK))
4283 s->flags |= SF_ERR_SRVTO;
4284 if (!(s->flags & SF_FINST_MASK))
4285 s->flags |= SF_FINST_D;
4286 stream_inc_http_fail_ctr(s);
4287 http_reply_and_close(s, txn->status, http_error_message(s));
4288 ret = HTTP_RULE_RES_ABRT;
4289 goto end;
4290}
4291
Willy Tarreaub49672d2022-05-27 10:13:37 +02004292void http_perform_server_redirect(struct stream *s, struct stconn *sc)
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004293{
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004294 struct channel *req = &s->req;
4295 struct channel *res = &s->res;
4296 struct server *srv;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004297 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004298 struct htx_sl *sl;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004299 struct ist path, location;
4300 unsigned int flags;
Amaury Denoyellec453f952021-07-06 11:40:12 +02004301 struct http_uri_parser parser;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004302
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004303 /*
4304 * Create the location
4305 */
4306 chunk_reset(&trash);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004307
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004308 /* 1: add the server's prefix */
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004309 /* special prefix "/" means don't change URL */
4310 srv = __objt_server(s->target);
4311 if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') {
4312 if (!chunk_memcat(&trash, srv->rdr_pfx, srv->rdr_len))
4313 return;
4314 }
4315
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004316 /* 2: add the request Path */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004317 htx = htxbuf(&req->buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02004318 sl = http_get_stline(htx);
Amaury Denoyellec453f952021-07-06 11:40:12 +02004319 parser = http_uri_parser_init(htx_sl_req_uri(sl));
4320 path = http_parse_path(&parser);
Tim Duesterhused526372020-03-05 17:56:33 +01004321 if (!isttest(path))
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004322 return;
4323
4324 if (!chunk_memcat(&trash, path.ptr, path.len))
4325 return;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004326 location = ist2(trash.area, trash.data);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004327
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004328 /*
4329 * Create the 302 respone
4330 */
4331 htx = htx_from_buf(&res->buf);
4332 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
4333 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
4334 ist("HTTP/1.1"), ist("302"), ist("Found"));
4335 if (!sl)
4336 goto fail;
4337 sl->info.res.status = 302;
4338 s->txn->status = 302;
4339
4340 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) ||
4341 !htx_add_header(htx, ist("Connection"), ist("close")) ||
4342 !htx_add_header(htx, ist("Content-length"), ist("0")) ||
4343 !htx_add_header(htx, ist("Location"), location))
4344 goto fail;
4345
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004346 if (!htx_add_endof(htx, HTX_BLK_EOH))
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004347 goto fail;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004348
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004349 htx->flags |= HTX_FL_EOM;
Christopher Fauletc20afb82020-01-24 19:16:26 +01004350 htx_to_buf(htx, &res->buf);
Christopher Fauleta72a7e42020-01-28 09:28:11 +01004351 if (!http_forward_proxy_resp(s, 1))
4352 goto fail;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004353
4354 /* return without error. */
Willy Tarreaub49672d2022-05-27 10:13:37 +02004355 sc_shutr(sc);
4356 sc_shutw(sc);
Christopher Faulet50264b42022-03-30 19:39:30 +02004357 s->conn_err_type = STRM_ET_NONE;
Willy Tarreaub49672d2022-05-27 10:13:37 +02004358 sc->state = SC_ST_CLO;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004359
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004360 if (!(s->flags & SF_ERR_MASK))
4361 s->flags |= SF_ERR_LOCAL;
4362 if (!(s->flags & SF_FINST_MASK))
4363 s->flags |= SF_FINST_C;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004364
4365 /* FIXME: we should increase a counter of redirects per server and per backend. */
4366 srv_inc_sess_ctr(srv);
4367 srv_set_sess_last(srv);
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004368 return;
4369
4370 fail:
4371 /* If an error occurred, remove the incomplete HTTP response from the
4372 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004373 channel_htx_truncate(res, htx);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004374}
4375
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05004376/* This function terminates the request because it was completely analyzed or
Christopher Fauletf2824e62018-10-01 12:12:37 +02004377 * because an error was triggered during the body forwarding.
4378 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004379static void http_end_request(struct stream *s)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004380{
4381 struct channel *chn = &s->req;
4382 struct http_txn *txn = s->txn;
4383
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004384 DBG_TRACE_ENTER(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004385
Christopher Fauletb42a8b62018-11-19 21:59:00 +01004386 if (unlikely(txn->req.msg_state == HTTP_MSG_ERROR ||
4387 txn->rsp.msg_state == HTTP_MSG_ERROR)) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004388 channel_abort(chn);
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004389 channel_htx_truncate(chn, htxbuf(&chn->buf));
Christopher Fauletf2824e62018-10-01 12:12:37 +02004390 goto end;
4391 }
4392
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004393 if (unlikely(txn->req.msg_state < HTTP_MSG_DONE)) {
4394 DBG_TRACE_DEVEL("waiting end of the request", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004395 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004396 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004397
4398 if (txn->req.msg_state == HTTP_MSG_DONE) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004399 /* No need to read anymore, the request was completely parsed.
4400 * We can shut the read side unless we want to abort_on_close,
4401 * or we have a POST request. The issue with POST requests is
4402 * that some browsers still send a CRLF after the request, and
4403 * this CRLF must be read so that it does not remain in the kernel
4404 * buffers, otherwise a close could cause an RST on some systems
4405 * (eg: Linux).
4406 */
Christopher Faulet769d0e92019-03-22 14:23:18 +01004407 if (!(s->be->options & PR_O_ABRT_CLOSE) && txn->meth != HTTP_METH_POST)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004408 channel_dont_read(chn);
4409
4410 /* if the server closes the connection, we want to immediately react
4411 * and close the socket to save packets and syscalls.
4412 */
Willy Tarreaucb041662022-05-17 19:44:42 +02004413 s->scb->flags |= SC_FL_NOHALF;
Christopher Fauletf2824e62018-10-01 12:12:37 +02004414
4415 /* In any case we've finished parsing the request so we must
4416 * disable Nagle when sending data because 1) we're not going
4417 * to shut this side, and 2) the server is waiting for us to
4418 * send pending data.
4419 */
4420 chn->flags |= CF_NEVER_WAIT;
4421
Christopher Fauletd01ce402019-01-02 17:44:13 +01004422 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
4423 /* The server has not finished to respond, so we
4424 * don't want to move in order not to upset it.
4425 */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004426 DBG_TRACE_DEVEL("waiting end of the response", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletd01ce402019-01-02 17:44:13 +01004427 return;
4428 }
4429
Christopher Fauletf2824e62018-10-01 12:12:37 +02004430 /* When we get here, it means that both the request and the
4431 * response have finished receiving. Depending on the connection
4432 * mode, we'll have to wait for the last bytes to leave in either
4433 * direction, and sometimes for a close to be effective.
4434 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02004435 if (txn->flags & TX_CON_WANT_TUN) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004436 /* Tunnel mode will not have any analyser so it needs to
4437 * poll for reads.
4438 */
4439 channel_auto_read(chn);
4440 txn->req.msg_state = HTTP_MSG_TUNNEL;
4441 }
4442 else {
4443 /* we're not expecting any new data to come for this
4444 * transaction, so we can close it.
Christopher Faulet9768c262018-10-22 09:34:31 +02004445 *
4446 * However, there is an exception if the response
4447 * length is undefined. In this case, we need to wait
4448 * the close from the server. The response will be
4449 * switched in TUNNEL mode until the end.
Christopher Fauletf2824e62018-10-01 12:12:37 +02004450 */
4451 if (!(txn->rsp.flags & HTTP_MSGF_XFER_LEN) &&
4452 txn->rsp.msg_state != HTTP_MSG_CLOSED)
Christopher Faulet9768c262018-10-22 09:34:31 +02004453 goto check_channel_flags;
Christopher Fauletf2824e62018-10-01 12:12:37 +02004454
4455 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4456 channel_shutr_now(chn);
4457 channel_shutw_now(chn);
4458 }
4459 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004460 goto check_channel_flags;
4461 }
4462
4463 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
4464 http_msg_closing:
4465 /* nothing else to forward, just waiting for the output buffer
4466 * to be empty and for the shutw_now to take effect.
4467 */
4468 if (channel_is_empty(chn)) {
4469 txn->req.msg_state = HTTP_MSG_CLOSED;
4470 goto http_msg_closed;
4471 }
4472 else if (chn->flags & CF_SHUTW) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004473 txn->req.msg_state = HTTP_MSG_ERROR;
4474 goto end;
4475 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004476 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004477 return;
4478 }
4479
4480 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
4481 http_msg_closed:
Christopher Fauletf2824e62018-10-01 12:12:37 +02004482 /* if we don't know whether the server will close, we need to hard close */
4483 if (txn->rsp.flags & HTTP_MSGF_XFER_LEN)
Willy Tarreaucb041662022-05-17 19:44:42 +02004484 s->scb->flags |= SC_FL_NOLINGER; /* we want to close ASAP */
Christopher Fauletf2824e62018-10-01 12:12:37 +02004485 /* see above in MSG_DONE why we only do this in these states */
Christopher Faulet769d0e92019-03-22 14:23:18 +01004486 if (!(s->be->options & PR_O_ABRT_CLOSE))
Christopher Fauletf2824e62018-10-01 12:12:37 +02004487 channel_dont_read(chn);
4488 goto end;
4489 }
4490
4491 check_channel_flags:
4492 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4493 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4494 /* if we've just closed an output, let's switch */
4495 txn->req.msg_state = HTTP_MSG_CLOSING;
4496 goto http_msg_closing;
4497 }
4498
4499 end:
4500 chn->analysers &= AN_REQ_FLT_END;
Christopher Faulet198ef8b2020-12-15 13:32:55 +01004501 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
4502 chn->flags |= CF_NEVER_WAIT;
4503 if (HAS_REQ_DATA_FILTERS(s))
Christopher Fauletf2824e62018-10-01 12:12:37 +02004504 chn->analysers |= AN_REQ_FLT_XFER_DATA;
Christopher Faulet198ef8b2020-12-15 13:32:55 +01004505 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004506 channel_auto_close(chn);
4507 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004508 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004509}
4510
4511
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05004512/* This function terminates the response because it was completely analyzed or
Christopher Fauletf2824e62018-10-01 12:12:37 +02004513 * because an error was triggered during the body forwarding.
4514 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004515static void http_end_response(struct stream *s)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004516{
4517 struct channel *chn = &s->res;
4518 struct http_txn *txn = s->txn;
4519
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004520 DBG_TRACE_ENTER(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004521
Christopher Fauletb42a8b62018-11-19 21:59:00 +01004522 if (unlikely(txn->req.msg_state == HTTP_MSG_ERROR ||
4523 txn->rsp.msg_state == HTTP_MSG_ERROR)) {
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004524 channel_htx_truncate(&s->req, htxbuf(&s->req.buf));
Christopher Faulet9768c262018-10-22 09:34:31 +02004525 channel_abort(&s->req);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004526 goto end;
4527 }
4528
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004529 if (unlikely(txn->rsp.msg_state < HTTP_MSG_DONE)) {
4530 DBG_TRACE_DEVEL("waiting end of the response", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004531 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004532 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004533
4534 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
4535 /* In theory, we don't need to read anymore, but we must
4536 * still monitor the server connection for a possible close
4537 * while the request is being uploaded, so we don't disable
4538 * reading.
4539 */
4540 /* channel_dont_read(chn); */
4541
4542 if (txn->req.msg_state < HTTP_MSG_DONE) {
4543 /* The client seems to still be sending data, probably
4544 * because we got an error response during an upload.
4545 * We have the choice of either breaking the connection
4546 * or letting it pass through. Let's do the later.
4547 */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004548 DBG_TRACE_DEVEL("waiting end of the request", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004549 return;
4550 }
4551
4552 /* When we get here, it means that both the request and the
4553 * response have finished receiving. Depending on the connection
4554 * mode, we'll have to wait for the last bytes to leave in either
4555 * direction, and sometimes for a close to be effective.
4556 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02004557 if (txn->flags & TX_CON_WANT_TUN) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004558 channel_auto_read(chn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004559 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
4560 }
4561 else {
4562 /* we're not expecting any new data to come for this
4563 * transaction, so we can close it.
4564 */
4565 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4566 channel_shutr_now(chn);
4567 channel_shutw_now(chn);
4568 }
4569 }
4570 goto check_channel_flags;
4571 }
4572
4573 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
4574 http_msg_closing:
4575 /* nothing else to forward, just waiting for the output buffer
4576 * to be empty and for the shutw_now to take effect.
4577 */
4578 if (channel_is_empty(chn)) {
4579 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4580 goto http_msg_closed;
4581 }
4582 else if (chn->flags & CF_SHUTW) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004583 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau4781b152021-04-06 13:53:36 +02004584 _HA_ATOMIC_INC(&strm_sess(s)->fe->fe_counters.cli_aborts);
4585 _HA_ATOMIC_INC(&s->be->be_counters.cli_aborts);
William Lallemand36119de2021-03-08 15:26:48 +01004586 if (strm_sess(s)->listener && strm_sess(s)->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02004587 _HA_ATOMIC_INC(&strm_sess(s)->listener->counters->cli_aborts);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004588 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02004589 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.cli_aborts);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004590 goto end;
4591 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004592 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004593 return;
4594 }
4595
4596 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
4597 http_msg_closed:
4598 /* drop any pending data */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004599 channel_htx_truncate(&s->req, htxbuf(&s->req.buf));
Christopher Faulet9768c262018-10-22 09:34:31 +02004600 channel_abort(&s->req);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004601 goto end;
4602 }
4603
4604 check_channel_flags:
4605 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4606 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4607 /* if we've just closed an output, let's switch */
4608 txn->rsp.msg_state = HTTP_MSG_CLOSING;
4609 goto http_msg_closing;
4610 }
4611
4612 end:
4613 chn->analysers &= AN_RES_FLT_END;
Christopher Faulet198ef8b2020-12-15 13:32:55 +01004614 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
4615 chn->flags |= CF_NEVER_WAIT;
4616 if (HAS_RSP_DATA_FILTERS(s))
4617 chn->analysers |= AN_RES_FLT_XFER_DATA;
4618 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004619 channel_auto_close(chn);
4620 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004621 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004622}
4623
Christopher Fauletef70e252020-01-28 09:26:19 +01004624/* Forward a response generated by HAProxy (error/redirect/return). This
4625 * function forwards all pending incoming data. If <final> is set to 0, nothing
4626 * more is performed. It is used for 1xx informational messages. Otherwise, the
Christopher Faulet507479b2020-05-15 12:29:46 +02004627 * transaction is terminated and the request is emptied. On success 1 is
Christopher Faulet40e6b552020-06-25 16:04:50 +02004628 * returned. If an error occurred, 0 is returned. If it fails, this function
4629 * only exits. It is the caller responsibility to do the cleanup.
Christopher Fauletef70e252020-01-28 09:26:19 +01004630 */
4631int http_forward_proxy_resp(struct stream *s, int final)
4632{
4633 struct channel *req = &s->req;
4634 struct channel *res = &s->res;
4635 struct htx *htx = htxbuf(&res->buf);
4636 size_t data;
4637
4638 if (final) {
4639 htx->flags |= HTX_FL_PROXY_RESP;
Christopher Faulet507479b2020-05-15 12:29:46 +02004640
Christopher Fauletaab1b672020-11-18 16:44:02 +01004641 if (!htx_is_empty(htx) && !http_eval_after_res_rules(s))
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01004642 return 0;
Christopher Fauletef70e252020-01-28 09:26:19 +01004643
Christopher Fauletd6c48362020-10-19 18:01:38 +02004644 if (s->txn->meth == HTTP_METH_HEAD)
4645 htx_skip_msg_payload(htx);
4646
Christopher Fauletef70e252020-01-28 09:26:19 +01004647 channel_auto_read(req);
4648 channel_abort(req);
4649 channel_auto_close(req);
4650 channel_htx_erase(req, htxbuf(&req->buf));
4651
4652 res->wex = tick_add_ifset(now_ms, res->wto);
4653 channel_auto_read(res);
4654 channel_auto_close(res);
4655 channel_shutr_now(res);
Christopher Faulet1a9db7c2020-06-25 15:36:45 +02004656 res->flags |= CF_EOI; /* The response is terminated, add EOI */
Christopher Faulet42432f32020-11-20 17:43:16 +01004657 htxbuf(&res->buf)->flags |= HTX_FL_EOM; /* no more data are expected */
Christopher Fauletef70e252020-01-28 09:26:19 +01004658 }
Christopher Fauletcf6898c2020-06-25 15:55:11 +02004659 else {
4660 /* Send ASAP informational messages. Rely on CF_EOI for final
4661 * response.
4662 */
4663 res->flags |= CF_SEND_DONTWAIT;
4664 }
Christopher Fauletef70e252020-01-28 09:26:19 +01004665
4666 data = htx->data - co_data(res);
4667 c_adv(res, data);
4668 htx->first = -1;
4669 res->total += data;
4670 return 1;
4671}
4672
Willy Tarreaub49672d2022-05-27 10:13:37 +02004673void http_server_error(struct stream *s, struct stconn *sc, int err,
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004674 int finst, struct http_reply *msg)
Christopher Faulet0f226952018-10-22 09:29:56 +02004675{
Christopher Faulet72c7d8d2020-01-27 15:32:25 +01004676 http_reply_and_close(s, s->txn->status, msg);
Christopher Faulet0f226952018-10-22 09:29:56 +02004677 if (!(s->flags & SF_ERR_MASK))
4678 s->flags |= err;
4679 if (!(s->flags & SF_FINST_MASK))
4680 s->flags |= finst;
4681}
4682
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004683void http_reply_and_close(struct stream *s, short status, struct http_reply *msg)
Christopher Faulet0f226952018-10-22 09:29:56 +02004684{
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004685 if (!msg) {
4686 channel_htx_truncate(&s->res, htxbuf(&s->res.buf));
4687 goto end;
4688 }
4689
4690 if (http_reply_message(s, msg) == -1) {
4691 /* On error, return a 500 error message, but don't rewrite it if
Christopher Faulet40e6b552020-06-25 16:04:50 +02004692 * it is already an internal error. If it was already a "const"
4693 * 500 error, just fail.
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004694 */
Christopher Faulet40e6b552020-06-25 16:04:50 +02004695 if (s->txn->status == 500) {
4696 if (s->txn->flags & TX_CONST_REPLY)
4697 goto end;
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004698 s->txn->flags |= TX_CONST_REPLY;
Christopher Faulet40e6b552020-06-25 16:04:50 +02004699 }
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004700 s->txn->status = 500;
4701 s->txn->http_reply = NULL;
4702 return http_reply_and_close(s, s->txn->status, http_error_message(s));
4703 }
4704
4705end:
4706 s->res.wex = tick_add_ifset(now_ms, s->res.wto);
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004707
Christopher Faulet2d565002021-09-10 09:17:50 +02004708 /* At this staged, HTTP analysis is finished */
4709 s->req.analysers &= AN_REQ_FLT_END;
4710 s->req.analyse_exp = TICK_ETERNITY;
4711
4712 s->res.analysers &= AN_RES_FLT_END;
4713 s->res.analyse_exp = TICK_ETERNITY;
4714
Christopher Faulet0f226952018-10-22 09:29:56 +02004715 channel_auto_read(&s->req);
4716 channel_abort(&s->req);
4717 channel_auto_close(&s->req);
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004718 channel_htx_erase(&s->req, htxbuf(&s->req.buf));
Christopher Faulet72c7d8d2020-01-27 15:32:25 +01004719 channel_auto_read(&s->res);
4720 channel_auto_close(&s->res);
4721 channel_shutr_now(&s->res);
Christopher Faulet0f226952018-10-22 09:29:56 +02004722}
4723
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004724struct http_reply *http_error_message(struct stream *s)
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004725{
4726 const int msgnum = http_get_status_idx(s->txn->status);
4727
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004728 if (s->txn->http_reply)
4729 return s->txn->http_reply;
4730 else if (s->be->replies[msgnum])
4731 return s->be->replies[msgnum];
4732 else if (strm_fe(s)->replies[msgnum])
4733 return strm_fe(s)->replies[msgnum];
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004734 else
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004735 return &http_err_replies[msgnum];
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004736}
4737
Christopher Faulet40e6b552020-06-25 16:04:50 +02004738/* Produces an HTX message from an http reply. Depending on the http reply type,
4739 * a, errorfile, an raw file or a log-format string is used. On success, it
4740 * returns 0. If an error occurs -1 is returned. If it fails, this function only
4741 * exits. It is the caller responsibility to do the cleanup.
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004742 */
Christopher Fauletae43b6c2020-05-27 15:24:22 +02004743int http_reply_to_htx(struct stream *s, struct htx *htx, struct http_reply *reply)
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004744{
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004745 struct buffer *errmsg;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004746 struct htx_sl *sl;
4747 struct buffer *body = NULL;
4748 const char *status, *reason, *clen, *ctype;
4749 unsigned int slflags;
4750 int ret = 0;
4751
Christopher Faulete29a97e2020-05-14 14:49:25 +02004752 /*
4753 * - HTTP_REPLY_ERRFILES unexpected here. handled as no payload if so
4754 *
4755 * - HTTP_REPLY_INDIRECT: switch on another reply if defined or handled
4756 * as no payload if NULL. the TXN status code is set with the status
4757 * of the original reply.
4758 */
4759
4760 if (reply->type == HTTP_REPLY_INDIRECT) {
4761 if (reply->body.reply)
4762 reply = reply->body.reply;
4763 }
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004764 if (reply->type == HTTP_REPLY_ERRMSG && !reply->body.errmsg) {
4765 /* get default error message */
4766 if (reply == s->txn->http_reply)
4767 s->txn->http_reply = NULL;
4768 reply = http_error_message(s);
4769 if (reply->type == HTTP_REPLY_INDIRECT) {
4770 if (reply->body.reply)
4771 reply = reply->body.reply;
4772 }
4773 }
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004774
4775 if (reply->type == HTTP_REPLY_ERRMSG) {
4776 /* implicit or explicit error message*/
4777 errmsg = reply->body.errmsg;
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004778 if (errmsg && !b_is_null(errmsg)) {
Christopher Faulet20567362020-05-15 14:52:49 +02004779 if (!htx_copy_msg(htx, errmsg))
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004780 goto fail;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004781 }
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004782 }
4783 else {
4784 /* no payload, file or log-format string */
4785 if (reply->type == HTTP_REPLY_RAW) {
4786 /* file */
4787 body = &reply->body.obj;
4788 }
4789 else if (reply->type == HTTP_REPLY_LOGFMT) {
4790 /* log-format string */
4791 body = alloc_trash_chunk();
4792 if (!body)
4793 goto fail_alloc;
4794 body->data = build_logline(s, body->area, body->size, &reply->body.fmt);
4795 }
4796 /* else no payload */
4797
4798 status = ultoa(reply->status);
4799 reason = http_get_reason(reply->status);
4800 slflags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_CLEN);
4801 if (!body || !b_data(body))
4802 slflags |= HTX_SL_F_BODYLESS;
4803 sl = htx_add_stline(htx, HTX_BLK_RES_SL, slflags, ist("HTTP/1.1"), ist(status), ist(reason));
4804 if (!sl)
4805 goto fail;
4806 sl->info.res.status = reply->status;
4807
4808 clen = (body ? ultoa(b_data(body)) : "0");
4809 ctype = reply->ctype;
4810
4811 if (!LIST_ISEMPTY(&reply->hdrs)) {
4812 struct http_reply_hdr *hdr;
4813 struct buffer *value = alloc_trash_chunk();
4814
4815 if (!value)
4816 goto fail;
4817
4818 list_for_each_entry(hdr, &reply->hdrs, list) {
4819 chunk_reset(value);
4820 value->data = build_logline(s, value->area, value->size, &hdr->value);
4821 if (b_data(value) && !htx_add_header(htx, hdr->name, ist2(b_head(value), b_data(value)))) {
4822 free_trash_chunk(value);
4823 goto fail;
4824 }
4825 chunk_reset(value);
4826 }
4827 free_trash_chunk(value);
4828 }
4829
4830 if (!htx_add_header(htx, ist("content-length"), ist(clen)) ||
4831 (body && b_data(body) && ctype && !htx_add_header(htx, ist("content-type"), ist(ctype))) ||
4832 !htx_add_endof(htx, HTX_BLK_EOH) ||
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004833 (body && b_data(body) && !htx_add_data_atonce(htx, ist2(b_head(body), b_data(body)))))
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004834 goto fail;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004835
4836 htx->flags |= HTX_FL_EOM;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004837 }
4838
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004839 leave:
4840 if (reply->type == HTTP_REPLY_LOGFMT)
4841 free_trash_chunk(body);
4842 return ret;
4843
4844 fail_alloc:
4845 if (!(s->flags & SF_ERR_MASK))
4846 s->flags |= SF_ERR_RESOURCE;
Christopher Faulet97e466c2020-05-15 15:12:47 +02004847 /* fall through */
4848 fail:
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004849 ret = -1;
4850 goto leave;
Christopher Faulet97e466c2020-05-15 15:12:47 +02004851}
4852
4853/* Send an http reply to the client. On success, it returns 0. If an error
Christopher Faulet40e6b552020-06-25 16:04:50 +02004854 * occurs -1 is returned and the response channel is truncated, removing this
4855 * way the faulty reply. This function may fail when the reply is formatted
4856 * (http_reply_to_htx) or when the reply is forwarded
4857 * (http_forward_proxy_resp). On the last case, it is because a
4858 * http-after-response rule fails.
Christopher Faulet97e466c2020-05-15 15:12:47 +02004859 */
4860int http_reply_message(struct stream *s, struct http_reply *reply)
4861{
4862 struct channel *res = &s->res;
4863 struct htx *htx = htx_from_buf(&res->buf);
4864
4865 if (s->txn->status == -1)
4866 s->txn->status = reply->status;
4867 channel_htx_truncate(res, htx);
4868
4869 if (http_reply_to_htx(s, htx, reply) == -1)
4870 goto fail;
4871
4872 htx_to_buf(htx, &s->res.buf);
4873 if (!http_forward_proxy_resp(s, 1))
4874 goto fail;
4875 return 0;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004876
4877 fail:
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004878 channel_htx_truncate(res, htx);
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004879 if (!(s->flags & SF_ERR_MASK))
4880 s->flags |= SF_ERR_PRXCOND;
Christopher Faulet97e466c2020-05-15 15:12:47 +02004881 return -1;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004882}
4883
Christopher Faulet50264b42022-03-30 19:39:30 +02004884/* Return the error message corresponding to s->conn_err_type. It is assumed
Christopher Faulet304cc402019-07-15 15:46:28 +02004885 * that the server side is closed. Note that err_type is actually a
4886 * bitmask, where almost only aborts may be cumulated with other
4887 * values. We consider that aborted operations are more important
4888 * than timeouts or errors due to the fact that nobody else in the
4889 * logs might explain incomplete retries. All others should avoid
4890 * being cumulated. It should normally not be possible to have multiple
4891 * aborts at once, but just in case, the first one in sequence is reported.
4892 * Note that connection errors appearing on the second request of a keep-alive
4893 * connection are not reported since this allows the client to retry.
4894 */
Willy Tarreaub49672d2022-05-27 10:13:37 +02004895void http_return_srv_error(struct stream *s, struct stconn *sc)
Christopher Faulet304cc402019-07-15 15:46:28 +02004896{
Christopher Faulet50264b42022-03-30 19:39:30 +02004897 int err_type = s->conn_err_type;
Christopher Faulet304cc402019-07-15 15:46:28 +02004898
4899 /* set s->txn->status for http_error_message(s) */
Christopher Faulet50264b42022-03-30 19:39:30 +02004900 if (err_type & STRM_ET_QUEUE_ABRT) {
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004901 s->txn->status = -1;
Willy Tarreaub49672d2022-05-27 10:13:37 +02004902 http_server_error(s, sc, SF_ERR_CLICL, SF_FINST_Q, NULL);
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004903 }
Christopher Faulet50264b42022-03-30 19:39:30 +02004904 else if (err_type & STRM_ET_CONN_ABRT) {
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004905 s->txn->status = -1;
Willy Tarreaub49672d2022-05-27 10:13:37 +02004906 http_server_error(s, sc, SF_ERR_CLICL, SF_FINST_C, NULL);
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004907 }
Christopher Faulet50264b42022-03-30 19:39:30 +02004908 else if (err_type & STRM_ET_QUEUE_TO) {
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004909 s->txn->status = 503;
Willy Tarreaub49672d2022-05-27 10:13:37 +02004910 http_server_error(s, sc, SF_ERR_SRVTO, SF_FINST_Q,
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004911 http_error_message(s));
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004912 }
Christopher Faulet50264b42022-03-30 19:39:30 +02004913 else if (err_type & STRM_ET_QUEUE_ERR) {
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004914 s->txn->status = 503;
Willy Tarreaub49672d2022-05-27 10:13:37 +02004915 http_server_error(s, sc, SF_ERR_SRVCL, SF_FINST_Q,
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004916 http_error_message(s));
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004917 }
Christopher Faulet50264b42022-03-30 19:39:30 +02004918 else if (err_type & STRM_ET_CONN_TO) {
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004919 s->txn->status = 503;
Willy Tarreaub49672d2022-05-27 10:13:37 +02004920 http_server_error(s, sc, SF_ERR_SRVTO, SF_FINST_C,
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004921 (s->txn->flags & TX_NOT_FIRST) ? NULL :
4922 http_error_message(s));
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004923 }
Christopher Faulet50264b42022-03-30 19:39:30 +02004924 else if (err_type & STRM_ET_CONN_ERR) {
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004925 s->txn->status = 503;
Willy Tarreaub49672d2022-05-27 10:13:37 +02004926 http_server_error(s, sc, SF_ERR_SRVCL, SF_FINST_C,
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004927 (s->flags & SF_SRV_REUSED) ? NULL :
4928 http_error_message(s));
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004929 }
Christopher Faulet50264b42022-03-30 19:39:30 +02004930 else if (err_type & STRM_ET_CONN_RES) {
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004931 s->txn->status = 503;
Willy Tarreaub49672d2022-05-27 10:13:37 +02004932 http_server_error(s, sc, SF_ERR_RESOURCE, SF_FINST_C,
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004933 (s->txn->flags & TX_NOT_FIRST) ? NULL :
4934 http_error_message(s));
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004935 }
Christopher Faulet50264b42022-03-30 19:39:30 +02004936 else { /* STRM_ET_CONN_OTHER and others */
Christopher Faulet304cc402019-07-15 15:46:28 +02004937 s->txn->status = 500;
Willy Tarreaub49672d2022-05-27 10:13:37 +02004938 http_server_error(s, sc, SF_ERR_INTERNAL, SF_FINST_C,
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004939 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004940 }
4941}
4942
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004943
Christopher Faulet4a28a532019-03-01 11:19:40 +01004944/* Handle Expect: 100-continue for HTTP/1.1 messages if necessary. It returns 0
4945 * on success and -1 on error.
4946 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004947static int http_handle_expect_hdr(struct stream *s, struct htx *htx, struct http_msg *msg)
Christopher Faulet4a28a532019-03-01 11:19:40 +01004948{
4949 /* If we have HTTP/1.1 message with a body and Expect: 100-continue,
4950 * then we must send an HTTP/1.1 100 Continue intermediate response.
4951 */
4952 if (msg->msg_state == HTTP_MSG_BODY && (msg->flags & HTTP_MSGF_VER_11) &&
4953 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
4954 struct ist hdr = { .ptr = "Expect", .len = 6 };
4955 struct http_hdr_ctx ctx;
4956
4957 ctx.blk = NULL;
4958 /* Expect is allowed in 1.1, look for it */
4959 if (http_find_header(htx, hdr, &ctx, 0) &&
4960 unlikely(isteqi(ctx.value, ist2("100-continue", 12)))) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004961 if (http_reply_100_continue(s) == -1)
Christopher Faulet4a28a532019-03-01 11:19:40 +01004962 return -1;
4963 http_remove_header(htx, &ctx);
4964 }
4965 }
4966 return 0;
4967}
4968
Christopher Faulet23a3c792018-11-28 10:01:23 +01004969/* Send a 100-Continue response to the client. It returns 0 on success and -1
4970 * on error. The response channel is updated accordingly.
4971 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004972static int http_reply_100_continue(struct stream *s)
Christopher Faulet23a3c792018-11-28 10:01:23 +01004973{
4974 struct channel *res = &s->res;
4975 struct htx *htx = htx_from_buf(&res->buf);
4976 struct htx_sl *sl;
4977 unsigned int flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|
4978 HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
Christopher Faulet23a3c792018-11-28 10:01:23 +01004979
4980 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
4981 ist("HTTP/1.1"), ist("100"), ist("Continue"));
4982 if (!sl)
4983 goto fail;
4984 sl->info.res.status = 100;
4985
Christopher Faulet1d5ec092019-06-26 14:23:54 +02004986 if (!htx_add_endof(htx, HTX_BLK_EOH))
Christopher Faulet23a3c792018-11-28 10:01:23 +01004987 goto fail;
4988
Christopher Fauleta72a7e42020-01-28 09:28:11 +01004989 if (!http_forward_proxy_resp(s, 0))
4990 goto fail;
Christopher Faulet23a3c792018-11-28 10:01:23 +01004991 return 0;
4992
4993 fail:
4994 /* If an error occurred, remove the incomplete HTTP response from the
4995 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004996 channel_htx_truncate(res, htx);
Christopher Faulet23a3c792018-11-28 10:01:23 +01004997 return -1;
4998}
4999
Christopher Faulet12c51e22018-11-28 15:59:42 +01005000
Christopher Faulet0f226952018-10-22 09:29:56 +02005001/*
5002 * Capture headers from message <htx> according to header list <cap_hdr>, and
5003 * fill the <cap> pointers appropriately.
5004 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005005static void http_capture_headers(struct htx *htx, char **cap, struct cap_hdr *cap_hdr)
Christopher Faulet0f226952018-10-22 09:29:56 +02005006{
5007 struct cap_hdr *h;
5008 int32_t pos;
5009
Christopher Fauleta3f15502019-05-13 15:27:23 +02005010 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet0f226952018-10-22 09:29:56 +02005011 struct htx_blk *blk = htx_get_blk(htx, pos);
5012 enum htx_blk_type type = htx_get_blk_type(blk);
5013 struct ist n, v;
5014
5015 if (type == HTX_BLK_EOH)
5016 break;
5017 if (type != HTX_BLK_HDR)
5018 continue;
5019
5020 n = htx_get_blk_name(htx, blk);
5021
5022 for (h = cap_hdr; h; h = h->next) {
5023 if (h->namelen && (h->namelen == n.len) &&
5024 (strncasecmp(n.ptr, h->name, h->namelen) == 0)) {
5025 if (cap[h->index] == NULL)
5026 cap[h->index] =
5027 pool_alloc(h->pool);
5028
5029 if (cap[h->index] == NULL) {
5030 ha_alert("HTTP capture : out of memory.\n");
5031 break;
5032 }
5033
5034 v = htx_get_blk_value(htx, blk);
Tim Duesterhus2471f5c2021-11-08 09:05:01 +01005035 v = isttrim(v, h->len);
Christopher Faulet0f226952018-10-22 09:29:56 +02005036
5037 memcpy(cap[h->index], v.ptr, v.len);
5038 cap[h->index][v.len]=0;
5039 }
5040 }
5041 }
5042}
5043
Christopher Faulet0b6bdc52018-10-24 11:05:36 +02005044/* Delete a value in a header between delimiters <from> and <next>. The header
5045 * itself is delimited by <start> and <end> pointers. The number of characters
5046 * displaced is returned, and the pointer to the first delimiter is updated if
5047 * required. The function tries as much as possible to respect the following
5048 * principles :
5049 * - replace <from> delimiter by the <next> one unless <from> points to <start>,
5050 * in which case <next> is simply removed
5051 * - set exactly one space character after the new first delimiter, unless there
5052 * are not enough characters in the block being moved to do so.
5053 * - remove unneeded spaces before the previous delimiter and after the new
5054 * one.
5055 *
5056 * It is the caller's responsibility to ensure that :
5057 * - <from> points to a valid delimiter or <start> ;
5058 * - <next> points to a valid delimiter or <end> ;
5059 * - there are non-space chars before <from>.
5060 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005061static int http_del_hdr_value(char *start, char *end, char **from, char *next)
Christopher Faulet0b6bdc52018-10-24 11:05:36 +02005062{
5063 char *prev = *from;
5064
5065 if (prev == start) {
5066 /* We're removing the first value. eat the semicolon, if <next>
5067 * is lower than <end> */
5068 if (next < end)
5069 next++;
5070
5071 while (next < end && HTTP_IS_SPHT(*next))
5072 next++;
5073 }
5074 else {
5075 /* Remove useless spaces before the old delimiter. */
5076 while (HTTP_IS_SPHT(*(prev-1)))
5077 prev--;
5078 *from = prev;
5079
5080 /* copy the delimiter and if possible a space if we're
5081 * not at the end of the line.
5082 */
5083 if (next < end) {
5084 *prev++ = *next++;
5085 if (prev + 1 < next)
5086 *prev++ = ' ';
5087 while (next < end && HTTP_IS_SPHT(*next))
5088 next++;
5089 }
5090 }
5091 memmove(prev, next, end - next);
5092 return (prev - next);
5093}
5094
Christopher Faulet0f226952018-10-22 09:29:56 +02005095
5096/* Formats the start line of the request (without CRLF) and puts it in <str> and
Joseph Herlantc42c0e92018-11-25 10:43:27 -08005097 * return the written length. The line can be truncated if it exceeds <len>.
Christopher Faulet0f226952018-10-22 09:29:56 +02005098 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005099static size_t http_fmt_req_line(const struct htx_sl *sl, char *str, size_t len)
Christopher Faulet0f226952018-10-22 09:29:56 +02005100{
5101 struct ist dst = ist2(str, 0);
5102
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005103 if (istcat(&dst, htx_sl_req_meth(sl), len) == -1)
Christopher Faulet0f226952018-10-22 09:29:56 +02005104 goto end;
5105 if (dst.len + 1 > len)
5106 goto end;
5107 dst.ptr[dst.len++] = ' ';
5108
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005109 if (istcat(&dst, htx_sl_req_uri(sl), len) == -1)
Christopher Faulet0f226952018-10-22 09:29:56 +02005110 goto end;
5111 if (dst.len + 1 > len)
5112 goto end;
5113 dst.ptr[dst.len++] = ' ';
5114
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005115 istcat(&dst, htx_sl_req_vsn(sl), len);
Christopher Faulet0f226952018-10-22 09:29:56 +02005116 end:
5117 return dst.len;
5118}
5119
5120/*
5121 * Print a debug line with a start line.
5122 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005123static void http_debug_stline(const char *dir, struct stream *s, const struct htx_sl *sl)
Christopher Faulet0f226952018-10-22 09:29:56 +02005124{
5125 struct session *sess = strm_sess(s);
5126 int max;
5127
5128 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
5129 dir,
Willy Tarreau88bc8002021-12-06 07:01:02 +00005130 objt_conn(sess->origin) ? (unsigned short)__objt_conn(sess->origin)->handle.fd : -1,
Willy Tarreaufd9417b2022-05-18 16:23:22 +02005131 sc_conn(s->scb) ? (unsigned short)(__sc_conn(s->scb))->handle.fd : -1);
Christopher Faulet0f226952018-10-22 09:29:56 +02005132
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005133 max = HTX_SL_P1_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02005134 UBOUND(max, trash.size - trash.data - 3);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005135 chunk_memcat(&trash, HTX_SL_P1_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02005136 trash.area[trash.data++] = ' ';
5137
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005138 max = HTX_SL_P2_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02005139 UBOUND(max, trash.size - trash.data - 2);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005140 chunk_memcat(&trash, HTX_SL_P2_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02005141 trash.area[trash.data++] = ' ';
5142
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005143 max = HTX_SL_P3_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02005144 UBOUND(max, trash.size - trash.data - 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005145 chunk_memcat(&trash, HTX_SL_P3_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02005146 trash.area[trash.data++] = '\n';
5147
Willy Tarreau2e8ab6b2020-03-14 11:03:20 +01005148 DISGUISE(write(1, trash.area, trash.data));
Christopher Faulet0f226952018-10-22 09:29:56 +02005149}
5150
5151/*
5152 * Print a debug line with a header.
5153 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005154static 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 +02005155{
5156 struct session *sess = strm_sess(s);
5157 int max;
5158
5159 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
5160 dir,
Willy Tarreau88bc8002021-12-06 07:01:02 +00005161 objt_conn(sess->origin) ? (unsigned short)__objt_conn(sess->origin)->handle.fd : -1,
Willy Tarreaufd9417b2022-05-18 16:23:22 +02005162 sc_conn(s->scb) ? (unsigned short)(__sc_conn(s->scb))->handle.fd : -1);
Christopher Faulet0f226952018-10-22 09:29:56 +02005163
5164 max = n.len;
5165 UBOUND(max, trash.size - trash.data - 3);
5166 chunk_memcat(&trash, n.ptr, max);
5167 trash.area[trash.data++] = ':';
5168 trash.area[trash.data++] = ' ';
5169
5170 max = v.len;
5171 UBOUND(max, trash.size - trash.data - 1);
5172 chunk_memcat(&trash, v.ptr, max);
5173 trash.area[trash.data++] = '\n';
5174
Willy Tarreau2e8ab6b2020-03-14 11:03:20 +01005175 DISGUISE(write(1, trash.area, trash.data));
Christopher Faulet0f226952018-10-22 09:29:56 +02005176}
5177
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005178void http_txn_reset_req(struct http_txn *txn)
5179{
Christopher Faulet1aea50e2020-01-17 16:03:53 +01005180 txn->req.flags = 0;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005181 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
5182}
5183
5184void http_txn_reset_res(struct http_txn *txn)
5185{
Christopher Faulet1aea50e2020-01-17 16:03:53 +01005186 txn->rsp.flags = 0;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005187 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
5188}
5189
5190/*
Christopher Faulet75f619a2021-03-08 19:12:58 +01005191 * Create and initialize a new HTTP transaction for stream <s>. This should be
5192 * used before processing any new request. It returns the transaction or NLULL
5193 * on error.
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005194 */
Christopher Faulet75f619a2021-03-08 19:12:58 +01005195struct http_txn *http_create_txn(struct stream *s)
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005196{
Christopher Faulet75f619a2021-03-08 19:12:58 +01005197 struct http_txn *txn;
Willy Tarreaub49672d2022-05-27 10:13:37 +02005198 struct stconn *sc = s->scf;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005199
Christopher Faulet75f619a2021-03-08 19:12:58 +01005200 txn = pool_alloc(pool_head_http_txn);
5201 if (!txn)
5202 return NULL;
5203 s->txn = txn;
5204
Christopher Faulet5eb67f52022-06-22 17:12:05 +02005205 txn->meth = HTTP_METH_OTHER;
Willy Tarreaub49672d2022-05-27 10:13:37 +02005206 txn->flags = ((sc && sc_ep_test(sc, SE_FL_NOT_FIRST)) ? TX_NOT_FIRST : 0);
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005207 txn->status = -1;
Christopher Faulet5cb513a2020-05-13 17:56:56 +02005208 txn->http_reply = NULL;
Christopher Faulete05bf9e2022-03-29 15:23:40 +02005209 txn->l7_buffer = BUF_NULL;
Willy Tarreau8b507582020-02-25 09:35:07 +01005210 write_u32(txn->cache_hash, 0);
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005211
5212 txn->cookie_first_date = 0;
5213 txn->cookie_last_date = 0;
5214
5215 txn->srv_cookie = NULL;
5216 txn->cli_cookie = NULL;
5217 txn->uri = NULL;
5218
5219 http_txn_reset_req(txn);
5220 http_txn_reset_res(txn);
5221
5222 txn->req.chn = &s->req;
5223 txn->rsp.chn = &s->res;
5224
5225 txn->auth.method = HTTP_AUTH_UNKNOWN;
5226
Willy Tarreaub7bfcb32021-08-31 08:13:25 +02005227 vars_init_head(&s->vars_txn, SCOPE_TXN);
5228 vars_init_head(&s->vars_reqres, SCOPE_REQ);
Christopher Faulet75f619a2021-03-08 19:12:58 +01005229
5230 return txn;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005231}
5232
5233/* to be used at the end of a transaction */
Christopher Faulet75f619a2021-03-08 19:12:58 +01005234void http_destroy_txn(struct stream *s)
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005235{
5236 struct http_txn *txn = s->txn;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005237
5238 /* these ones will have been dynamically allocated */
5239 pool_free(pool_head_requri, txn->uri);
5240 pool_free(pool_head_capture, txn->cli_cookie);
5241 pool_free(pool_head_capture, txn->srv_cookie);
Tim Duesterhusa17e6622020-03-05 20:19:02 +01005242 pool_free(pool_head_uniqueid, s->unique_id.ptr);
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005243
Tim Duesterhusa17e6622020-03-05 20:19:02 +01005244 s->unique_id = IST_NULL;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005245 txn->uri = NULL;
5246 txn->srv_cookie = NULL;
5247 txn->cli_cookie = NULL;
5248
Christopher Faulet59399252019-11-07 14:27:52 +01005249 if (!LIST_ISEMPTY(&s->vars_txn.head))
5250 vars_prune(&s->vars_txn, s->sess, s);
5251 if (!LIST_ISEMPTY(&s->vars_reqres.head))
5252 vars_prune(&s->vars_reqres, s->sess, s);
Christopher Faulet75f619a2021-03-08 19:12:58 +01005253
Christopher Faulete05bf9e2022-03-29 15:23:40 +02005254 b_free(&txn->l7_buffer);
5255
Christopher Faulet75f619a2021-03-08 19:12:58 +01005256 pool_free(pool_head_http_txn, txn);
5257 s->txn = NULL;
Christopher Faulet59399252019-11-07 14:27:52 +01005258}
5259
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005260
5261DECLARE_POOL(pool_head_http_txn, "http_txn", sizeof(struct http_txn));
Christopher Faulet0f226952018-10-22 09:29:56 +02005262
Christopher Fauletf4eb75d2018-10-11 15:55:07 +02005263/*
5264 * Local variables:
5265 * c-indent-level: 8
5266 * c-basic-offset: 8
5267 * End:
5268 */