blob: 021ad753b44ee9ce65d995c0f95db63e418549ce [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);
2648 int i;
2649
2650 for (i = 0; i < istlen(n); i++) {
2651 if (!isalnum((unsigned char)n.ptr[i]) && n.ptr[i] != '-') {
2652 /* Block the request or remove the header */
2653 if (px->options2 & PR_O2_RSTRICT_REQ_HDR_NAMES_BLK)
2654 goto block;
2655 blk = htx_remove_blk(htx, blk);
2656 continue;
2657 }
2658 }
2659 }
2660 if (type == HTX_BLK_EOH)
2661 break;
2662
2663 blk = htx_get_next_blk(htx, blk);
2664 }
2665 out:
2666 return rule_ret;
2667 block:
2668 /* Block the request returning a 403-Forbidden response */
2669 s->txn->status = 403;
2670 rule_ret = HTTP_RULE_RES_DENY;
2671 goto out;
2672}
2673
Christopher Faulet92d34fe2019-12-17 09:20:34 +01002674/* Replace all headers matching the name <name>. The header value is replaced if
2675 * it matches the regex <re>. <str> is used for the replacement. If <full> is
2676 * set to 1, the full-line is matched and replaced. Otherwise, comma-separated
2677 * values are evaluated one by one. It returns 0 on success and -1 on error.
2678 */
2679int http_replace_hdrs(struct stream* s, struct htx *htx, struct ist name,
2680 const char *str, struct my_regex *re, int full)
Christopher Faulet72333522018-10-24 11:25:02 +02002681{
2682 struct http_hdr_ctx ctx;
2683 struct buffer *output = get_trash_chunk();
2684
Christopher Faulet72333522018-10-24 11:25:02 +02002685 ctx.blk = NULL;
Christopher Faulet92d34fe2019-12-17 09:20:34 +01002686 while (http_find_header(htx, name, &ctx, full)) {
Christopher Faulet72333522018-10-24 11:25:02 +02002687 if (!regex_exec_match2(re, ctx.value.ptr, ctx.value.len, MAX_MATCH, pmatch, 0))
2688 continue;
2689
2690 output->data = exp_replace(output->area, output->size, ctx.value.ptr, str, pmatch);
2691 if (output->data == -1)
2692 return -1;
2693 if (!http_replace_header_value(htx, &ctx, ist2(output->area, output->data)))
2694 return -1;
2695 }
2696 return 0;
2697}
2698
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002699/* This function executes one of the set-{method,path,query,uri} actions. It
2700 * takes the string from the variable 'replace' with length 'len', then modifies
2701 * the relevant part of the request line accordingly. Then it updates various
2702 * pointers to the next elements which were moved, and the total buffer length.
2703 * It finds the action to be performed in p[2], previously filled by function
2704 * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal
2705 * error, though this can be revisited when this code is finally exploited.
2706 *
2707 * 'action' can be '0' to replace method, '1' to replace path, '2' to replace
Christopher Faulet312294f2020-09-02 17:17:44 +02002708 * query string, 3 to replace uri or 4 to replace the path+query.
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002709 *
2710 * In query string case, the mark question '?' must be set at the start of the
2711 * string by the caller, event if the replacement query string is empty.
2712 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002713int http_req_replace_stline(int action, const char *replace, int len,
2714 struct proxy *px, struct stream *s)
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002715{
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002716 struct htx *htx = htxbuf(&s->req.buf);
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002717
2718 switch (action) {
2719 case 0: // method
2720 if (!http_replace_req_meth(htx, ist2(replace, len)))
2721 return -1;
2722 break;
2723
2724 case 1: // path
Christopher Fauletb8ce5052020-08-31 16:11:57 +02002725 if (!http_replace_req_path(htx, ist2(replace, len), 0))
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002726 return -1;
2727 break;
2728
2729 case 2: // query
2730 if (!http_replace_req_query(htx, ist2(replace, len)))
2731 return -1;
2732 break;
2733
2734 case 3: // uri
2735 if (!http_replace_req_uri(htx, ist2(replace, len)))
2736 return -1;
2737 break;
2738
Christopher Faulet312294f2020-09-02 17:17:44 +02002739 case 4: // path + query
2740 if (!http_replace_req_path(htx, ist2(replace, len), 1))
2741 return -1;
2742 break;
2743
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002744 default:
2745 return -1;
2746 }
2747 return 0;
2748}
2749
2750/* This function replace the HTTP status code and the associated message. The
Christopher Faulete00d06c2019-12-16 17:18:42 +01002751 * variable <status> contains the new status code. This function never fails. It
2752 * returns 0 in case of success, -1 in case of internal error.
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002753 */
Christopher Faulet96bff762019-12-17 13:46:18 +01002754int http_res_set_status(unsigned int status, struct ist reason, struct stream *s)
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002755{
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002756 struct htx *htx = htxbuf(&s->res.buf);
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002757 char *res;
2758
2759 chunk_reset(&trash);
2760 res = ultoa_o(status, trash.area, trash.size);
2761 trash.data = res - trash.area;
2762
2763 /* Do we have a custom reason format string? */
Tim Duesterhuse296d3e2020-03-05 17:56:31 +01002764 if (!isttest(reason)) {
Christopher Faulet96bff762019-12-17 13:46:18 +01002765 const char *str = http_get_reason(status);
Tim Duesterhusdcf753a2021-03-04 17:31:47 +01002766 reason = ist(str);
Christopher Faulet96bff762019-12-17 13:46:18 +01002767 }
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002768
Christopher Fauletbde2c4c2020-08-31 16:43:34 +02002769 if (!http_replace_res_status(htx, ist2(trash.area, trash.data), reason))
Christopher Faulete00d06c2019-12-16 17:18:42 +01002770 return -1;
2771 return 0;
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002772}
2773
Christopher Faulet3e964192018-10-24 11:39:23 +02002774/* Executes the http-request rules <rules> for stream <s>, proxy <px> and
2775 * transaction <txn>. Returns the verdict of the first rule that prevents
2776 * further processing of the request (auth, deny, ...), and defaults to
2777 * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or
2778 * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT
2779 * on txn->flags if it encounters a tarpit rule. If <deny_status> is not NULL
2780 * and a deny/tarpit rule is matched, it will be filled with this rule's deny
2781 * status.
2782 */
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002783static enum rule_result http_req_get_intercept_rule(struct proxy *px, struct list *def_rules,
2784 struct list *rules, struct stream *s)
Christopher Faulet3e964192018-10-24 11:39:23 +02002785{
2786 struct session *sess = strm_sess(s);
2787 struct http_txn *txn = s->txn;
Christopher Faulet3e964192018-10-24 11:39:23 +02002788 struct act_rule *rule;
Christopher Faulet3e964192018-10-24 11:39:23 +02002789 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002790 int act_opts = 0;
Christopher Faulet3e964192018-10-24 11:39:23 +02002791
Christopher Faulet3e964192018-10-24 11:39:23 +02002792 /* If "the current_rule_list" match the executed rule list, we are in
2793 * resume condition. If a resume is needed it is always in the action
2794 * and never in the ACL or converters. In this case, we initialise the
2795 * current rule, and go to the action execution point.
2796 */
2797 if (s->current_rule) {
2798 rule = s->current_rule;
2799 s->current_rule = NULL;
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002800 if (s->current_rule_list == rules || (def_rules && s->current_rule_list == def_rules))
Christopher Faulet3e964192018-10-24 11:39:23 +02002801 goto resume_execution;
2802 }
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002803 s->current_rule_list = ((!def_rules || s->current_rule_list == def_rules) ? rules : def_rules);
Christopher Faulet3e964192018-10-24 11:39:23 +02002804
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002805 restart:
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002806 /* start the ruleset evaluation in strict mode */
2807 txn->req.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01002808
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002809 list_for_each_entry(rule, s->current_rule_list, list) {
Christopher Faulet3e964192018-10-24 11:39:23 +02002810 /* check optional condition */
2811 if (rule->cond) {
2812 int ret;
2813
2814 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
2815 ret = acl_pass(ret);
2816
2817 if (rule->cond->pol == ACL_COND_UNLESS)
2818 ret = !ret;
2819
2820 if (!ret) /* condition not matched */
2821 continue;
2822 }
2823
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002824 act_opts |= ACT_OPT_FIRST;
Christopher Faulet3e964192018-10-24 11:39:23 +02002825 resume_execution:
Amaury Denoyelle03517732021-05-07 14:25:01 +02002826 if (rule->kw->flags & KWF_EXPERIMENTAL)
2827 mark_tainted(TAINTED_ACTION_EXP_EXECUTED);
2828
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002829 /* Always call the action function if defined */
2830 if (rule->action_ptr) {
2831 if ((s->req.flags & CF_READ_ERROR) ||
2832 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
2833 (px->options & PR_O_ABRT_CLOSE)))
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002834 act_opts |= ACT_OPT_FINAL;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002835
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002836 switch (rule->action_ptr(rule, px, sess, s, act_opts)) {
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002837 case ACT_RET_CONT:
2838 break;
2839 case ACT_RET_STOP:
2840 rule_ret = HTTP_RULE_RES_STOP;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002841 s->last_rule_file = rule->conf.file;
2842 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002843 goto end;
2844 case ACT_RET_YIELD:
2845 s->current_rule = rule;
2846 rule_ret = HTTP_RULE_RES_YIELD;
2847 goto end;
2848 case ACT_RET_ERR:
2849 rule_ret = HTTP_RULE_RES_ERROR;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002850 s->last_rule_file = rule->conf.file;
2851 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002852 goto end;
2853 case ACT_RET_DONE:
2854 rule_ret = HTTP_RULE_RES_DONE;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002855 s->last_rule_file = rule->conf.file;
2856 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002857 goto end;
2858 case ACT_RET_DENY:
Christopher Fauletb58f62b2020-01-13 16:40:13 +01002859 if (txn->status == -1)
2860 txn->status = 403;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002861 rule_ret = HTTP_RULE_RES_DENY;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002862 s->last_rule_file = rule->conf.file;
2863 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002864 goto end;
2865 case ACT_RET_ABRT:
2866 rule_ret = HTTP_RULE_RES_ABRT;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002867 s->last_rule_file = rule->conf.file;
2868 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002869 goto end;
2870 case ACT_RET_INV:
2871 rule_ret = HTTP_RULE_RES_BADREQ;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002872 s->last_rule_file = rule->conf.file;
2873 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002874 goto end;
2875 }
2876 continue; /* eval the next rule */
2877 }
2878
2879 /* If not action function defined, check for known actions */
Christopher Faulet3e964192018-10-24 11:39:23 +02002880 switch (rule->action) {
2881 case ACT_ACTION_ALLOW:
2882 rule_ret = HTTP_RULE_RES_STOP;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002883 s->last_rule_file = rule->conf.file;
2884 s->last_rule_line = rule->conf.line;
Christopher Faulet3e964192018-10-24 11:39:23 +02002885 goto end;
2886
2887 case ACT_ACTION_DENY:
Christopher Faulet5cb513a2020-05-13 17:56:56 +02002888 txn->status = rule->arg.http_reply->status;
2889 txn->http_reply = rule->arg.http_reply;
Christopher Faulet3e964192018-10-24 11:39:23 +02002890 rule_ret = HTTP_RULE_RES_DENY;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002891 s->last_rule_file = rule->conf.file;
2892 s->last_rule_line = rule->conf.line;
Christopher Faulet3e964192018-10-24 11:39:23 +02002893 goto end;
2894
2895 case ACT_HTTP_REQ_TARPIT:
2896 txn->flags |= TX_CLTARPIT;
Christopher Faulet5cb513a2020-05-13 17:56:56 +02002897 txn->status = rule->arg.http_reply->status;
2898 txn->http_reply = rule->arg.http_reply;
Christopher Faulet3e964192018-10-24 11:39:23 +02002899 rule_ret = HTTP_RULE_RES_DENY;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002900 s->last_rule_file = rule->conf.file;
2901 s->last_rule_line = rule->conf.line;
Christopher Faulet3e964192018-10-24 11:39:23 +02002902 goto end;
2903
Willy Tarreaubc1223b2021-09-02 16:54:33 +02002904 case ACT_HTTP_REDIR: {
2905 int ret = http_apply_redirect_rule(rule->arg.redir, s, txn);
2906
2907 if (ret == 2) // 2 == skip
2908 break;
2909
2910 rule_ret = ret ? HTTP_RULE_RES_ABRT : HTTP_RULE_RES_ERROR;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002911 s->last_rule_file = rule->conf.file;
2912 s->last_rule_line = rule->conf.line;
Christopher Faulet3e964192018-10-24 11:39:23 +02002913 goto end;
Willy Tarreaubc1223b2021-09-02 16:54:33 +02002914 }
Christopher Faulet3e964192018-10-24 11:39:23 +02002915
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002916 /* other flags exists, but normally, they never be matched. */
Christopher Faulet3e964192018-10-24 11:39:23 +02002917 default:
2918 break;
2919 }
2920 }
2921
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002922 if (def_rules && s->current_rule_list == def_rules) {
2923 s->current_rule_list = rules;
2924 goto restart;
2925 }
2926
Christopher Faulet3e964192018-10-24 11:39:23 +02002927 end:
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002928 /* if the ruleset evaluation is finished reset the strict mode */
Christopher Faulet46f95542019-12-20 10:07:22 +01002929 if (rule_ret != HTTP_RULE_RES_YIELD)
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002930 txn->req.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01002931
Christopher Faulet3e964192018-10-24 11:39:23 +02002932 /* we reached the end of the rules, nothing to report */
2933 return rule_ret;
2934}
2935
2936/* Executes the http-response rules <rules> for stream <s> and proxy <px>. It
2937 * returns one of 5 possible statuses: HTTP_RULE_RES_CONT, HTTP_RULE_RES_STOP,
2938 * HTTP_RULE_RES_DONE, HTTP_RULE_RES_YIELD, or HTTP_RULE_RES_BADREQ. If *CONT
2939 * is returned, the process can continue the evaluation of next rule list. If
2940 * *STOP or *DONE is returned, the process must stop the evaluation. If *BADREQ
2941 * is returned, it means the operation could not be processed and a server error
Christopher Fauleta53abad2020-05-13 08:12:22 +02002942 * must be returned. If *YIELD is returned, the caller must call again the
2943 * function with the same context.
Christopher Faulet3e964192018-10-24 11:39:23 +02002944 */
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002945static enum rule_result http_res_get_intercept_rule(struct proxy *px, struct list *def_rules,
2946 struct list *rules, struct stream *s)
Christopher Faulet3e964192018-10-24 11:39:23 +02002947{
2948 struct session *sess = strm_sess(s);
2949 struct http_txn *txn = s->txn;
Christopher Faulet3e964192018-10-24 11:39:23 +02002950 struct act_rule *rule;
Christopher Faulet3e964192018-10-24 11:39:23 +02002951 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002952 int act_opts = 0;
Christopher Faulet3e964192018-10-24 11:39:23 +02002953
Christopher Faulet3e964192018-10-24 11:39:23 +02002954 /* If "the current_rule_list" match the executed rule list, we are in
2955 * resume condition. If a resume is needed it is always in the action
2956 * and never in the ACL or converters. In this case, we initialise the
2957 * current rule, and go to the action execution point.
2958 */
2959 if (s->current_rule) {
2960 rule = s->current_rule;
2961 s->current_rule = NULL;
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002962 if (s->current_rule_list == rules || (def_rules && s->current_rule_list == def_rules))
Christopher Faulet3e964192018-10-24 11:39:23 +02002963 goto resume_execution;
2964 }
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002965 s->current_rule_list = ((!def_rules || s->current_rule_list == def_rules) ? rules : def_rules);
2966
2967 restart:
Christopher Faulet3e964192018-10-24 11:39:23 +02002968
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002969 /* start the ruleset evaluation in strict mode */
2970 txn->rsp.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01002971
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002972 list_for_each_entry(rule, s->current_rule_list, list) {
Christopher Faulet3e964192018-10-24 11:39:23 +02002973 /* check optional condition */
2974 if (rule->cond) {
2975 int ret;
2976
2977 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
2978 ret = acl_pass(ret);
2979
2980 if (rule->cond->pol == ACL_COND_UNLESS)
2981 ret = !ret;
2982
2983 if (!ret) /* condition not matched */
2984 continue;
2985 }
2986
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002987 act_opts |= ACT_OPT_FIRST;
Christopher Faulet3e964192018-10-24 11:39:23 +02002988resume_execution:
Amaury Denoyelle03517732021-05-07 14:25:01 +02002989 if (rule->kw->flags & KWF_EXPERIMENTAL)
2990 mark_tainted(TAINTED_ACTION_EXP_EXECUTED);
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002991
2992 /* Always call the action function if defined */
2993 if (rule->action_ptr) {
2994 if ((s->req.flags & CF_READ_ERROR) ||
2995 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
2996 (px->options & PR_O_ABRT_CLOSE)))
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002997 act_opts |= ACT_OPT_FINAL;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002998
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002999 switch (rule->action_ptr(rule, px, sess, s, act_opts)) {
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01003000 case ACT_RET_CONT:
3001 break;
3002 case ACT_RET_STOP:
3003 rule_ret = HTTP_RULE_RES_STOP;
Willy Tarreauc6dae862022-03-09 17:23:10 +01003004 s->last_rule_file = rule->conf.file;
3005 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01003006 goto end;
3007 case ACT_RET_YIELD:
3008 s->current_rule = rule;
3009 rule_ret = HTTP_RULE_RES_YIELD;
3010 goto end;
3011 case ACT_RET_ERR:
3012 rule_ret = HTTP_RULE_RES_ERROR;
Willy Tarreauc6dae862022-03-09 17:23:10 +01003013 s->last_rule_file = rule->conf.file;
3014 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01003015 goto end;
3016 case ACT_RET_DONE:
3017 rule_ret = HTTP_RULE_RES_DONE;
Willy Tarreauc6dae862022-03-09 17:23:10 +01003018 s->last_rule_file = rule->conf.file;
3019 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01003020 goto end;
3021 case ACT_RET_DENY:
Christopher Fauletb58f62b2020-01-13 16:40:13 +01003022 if (txn->status == -1)
3023 txn->status = 502;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01003024 rule_ret = HTTP_RULE_RES_DENY;
Willy Tarreauc6dae862022-03-09 17:23:10 +01003025 s->last_rule_file = rule->conf.file;
3026 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01003027 goto end;
3028 case ACT_RET_ABRT:
3029 rule_ret = HTTP_RULE_RES_ABRT;
Willy Tarreauc6dae862022-03-09 17:23:10 +01003030 s->last_rule_file = rule->conf.file;
3031 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01003032 goto end;
3033 case ACT_RET_INV:
3034 rule_ret = HTTP_RULE_RES_BADREQ;
Willy Tarreauc6dae862022-03-09 17:23:10 +01003035 s->last_rule_file = rule->conf.file;
3036 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01003037 goto end;
3038 }
3039 continue; /* eval the next rule */
3040 }
3041
3042 /* If not action function defined, check for known actions */
Christopher Faulet3e964192018-10-24 11:39:23 +02003043 switch (rule->action) {
3044 case ACT_ACTION_ALLOW:
3045 rule_ret = HTTP_RULE_RES_STOP; /* "allow" rules are OK */
Willy Tarreauc6dae862022-03-09 17:23:10 +01003046 s->last_rule_file = rule->conf.file;
3047 s->last_rule_line = rule->conf.line;
Christopher Faulet3e964192018-10-24 11:39:23 +02003048 goto end;
3049
3050 case ACT_ACTION_DENY:
Christopher Faulet5cb513a2020-05-13 17:56:56 +02003051 txn->status = rule->arg.http_reply->status;
3052 txn->http_reply = rule->arg.http_reply;
Christopher Faulet3a26bee2019-12-16 12:47:40 +01003053 rule_ret = HTTP_RULE_RES_DENY;
Willy Tarreauc6dae862022-03-09 17:23:10 +01003054 s->last_rule_file = rule->conf.file;
3055 s->last_rule_line = rule->conf.line;
Christopher Faulet3e964192018-10-24 11:39:23 +02003056 goto end;
3057
Willy Tarreaubc1223b2021-09-02 16:54:33 +02003058 case ACT_HTTP_REDIR: {
3059 int ret = http_apply_redirect_rule(rule->arg.redir, s, txn);
Christopher Faulet3e964192018-10-24 11:39:23 +02003060
Willy Tarreaubc1223b2021-09-02 16:54:33 +02003061 if (ret == 2) // 2 == skip
3062 break;
3063
3064 rule_ret = ret ? HTTP_RULE_RES_ABRT : HTTP_RULE_RES_ERROR;
Willy Tarreauc6dae862022-03-09 17:23:10 +01003065 s->last_rule_file = rule->conf.file;
3066 s->last_rule_line = rule->conf.line;
Willy Tarreaubc1223b2021-09-02 16:54:33 +02003067 goto end;
3068 }
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01003069 /* other flags exists, but normally, they never be matched. */
Christopher Faulet3e964192018-10-24 11:39:23 +02003070 default:
3071 break;
3072 }
3073 }
3074
Christopher Fauletd4150ad2021-10-13 15:35:55 +02003075 if (def_rules && s->current_rule_list == def_rules) {
3076 s->current_rule_list = rules;
3077 goto restart;
3078 }
3079
Christopher Faulet3e964192018-10-24 11:39:23 +02003080 end:
Christopher Faulet1aea50e2020-01-17 16:03:53 +01003081 /* if the ruleset evaluation is finished reset the strict mode */
Christopher Faulet46f95542019-12-20 10:07:22 +01003082 if (rule_ret != HTTP_RULE_RES_YIELD)
Christopher Faulet1aea50e2020-01-17 16:03:53 +01003083 txn->rsp.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01003084
Christopher Faulet3e964192018-10-24 11:39:23 +02003085 /* we reached the end of the rules, nothing to report */
3086 return rule_ret;
3087}
3088
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01003089/* Executes backend and frontend http-after-response rules for the stream <s>,
3090 * in that order. it return 1 on success and 0 on error. It is the caller
3091 * responsibility to catch error or ignore it. If it catches it, this function
3092 * may be called a second time, for the internal error.
3093 */
3094int http_eval_after_res_rules(struct stream *s)
3095{
Christopher Fauletd4150ad2021-10-13 15:35:55 +02003096 struct list *def_rules, *rules;
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01003097 struct session *sess = s->sess;
3098 enum rule_result ret = HTTP_RULE_RES_CONT;
3099
Christopher Faulet507479b2020-05-15 12:29:46 +02003100 /* Eval after-response ruleset only if the reply is not const */
3101 if (s->txn->flags & TX_CONST_REPLY)
3102 goto end;
3103
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01003104 /* prune the request variables if not already done and swap to the response variables. */
3105 if (s->vars_reqres.scope != SCOPE_RES) {
3106 if (!LIST_ISEMPTY(&s->vars_reqres.head))
3107 vars_prune(&s->vars_reqres, s->sess, s);
Willy Tarreaub7bfcb32021-08-31 08:13:25 +02003108 vars_init_head(&s->vars_reqres, SCOPE_RES);
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01003109 }
3110
Christopher Fauletd4150ad2021-10-13 15:35:55 +02003111 def_rules = (s->be->defpx ? &s->be->defpx->http_after_res_rules : NULL);
3112 rules = &s->be->http_after_res_rules;
3113
3114 ret = http_res_get_intercept_rule(s->be, def_rules, rules, s);
Christopher Faulet4c5a5912021-11-09 17:48:39 +01003115 if ((ret == HTTP_RULE_RES_CONT || ret == HTTP_RULE_RES_STOP) && sess->fe != s->be) {
Christopher Fauletd4150ad2021-10-13 15:35:55 +02003116 def_rules = ((sess->fe->defpx && sess->fe->defpx != s->be->defpx) ? &sess->fe->defpx->http_after_res_rules : NULL);
3117 rules = &sess->fe->http_after_res_rules;
3118 ret = http_res_get_intercept_rule(sess->fe, def_rules, rules, s);
3119 }
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01003120
Christopher Faulet507479b2020-05-15 12:29:46 +02003121 end:
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01003122 /* All other codes than CONTINUE, STOP or DONE are forbidden */
3123 return (ret == HTTP_RULE_RES_CONT || ret == HTTP_RULE_RES_STOP || ret == HTTP_RULE_RES_DONE);
3124}
3125
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003126/*
3127 * Manage client-side cookie. It can impact performance by about 2% so it is
3128 * desirable to call it only when needed. This code is quite complex because
3129 * of the multiple very crappy and ambiguous syntaxes we have to support. it
3130 * highly recommended not to touch this part without a good reason !
3131 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003132static void http_manage_client_side_cookies(struct stream *s, struct channel *req)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003133{
3134 struct session *sess = s->sess;
3135 struct http_txn *txn = s->txn;
3136 struct htx *htx;
3137 struct http_hdr_ctx ctx;
3138 char *hdr_beg, *hdr_end, *del_from;
3139 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
3140 int preserve_hdr;
3141
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003142 htx = htxbuf(&req->buf);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003143 ctx.blk = NULL;
3144 while (http_find_header(htx, ist("Cookie"), &ctx, 1)) {
Olivier Houchardf0f42382019-07-22 17:43:46 +02003145 int is_first = 1;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003146 del_from = NULL; /* nothing to be deleted */
3147 preserve_hdr = 0; /* assume we may kill the whole header */
3148
3149 /* Now look for cookies. Conforming to RFC2109, we have to support
3150 * attributes whose name begin with a '$', and associate them with
3151 * the right cookie, if we want to delete this cookie.
3152 * So there are 3 cases for each cookie read :
3153 * 1) it's a special attribute, beginning with a '$' : ignore it.
3154 * 2) it's a server id cookie that we *MAY* want to delete : save
3155 * some pointers on it (last semi-colon, beginning of cookie...)
3156 * 3) it's an application cookie : we *MAY* have to delete a previous
3157 * "special" cookie.
3158 * At the end of loop, if a "special" cookie remains, we may have to
3159 * remove it. If no application cookie persists in the header, we
3160 * *MUST* delete it.
3161 *
3162 * Note: RFC2965 is unclear about the processing of spaces around
3163 * the equal sign in the ATTR=VALUE form. A careful inspection of
3164 * the RFC explicitly allows spaces before it, and not within the
3165 * tokens (attrs or values). An inspection of RFC2109 allows that
3166 * too but section 10.1.3 lets one think that spaces may be allowed
3167 * after the equal sign too, resulting in some (rare) buggy
3168 * implementations trying to do that. So let's do what servers do.
3169 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
3170 * allowed quoted strings in values, with any possible character
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003171 * after a backslash, including control chars and delimiters, which
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003172 * causes parsing to become ambiguous. Browsers also allow spaces
3173 * within values even without quotes.
3174 *
3175 * We have to keep multiple pointers in order to support cookie
3176 * removal at the beginning, middle or end of header without
3177 * corrupting the header. All of these headers are valid :
3178 *
3179 * hdr_beg hdr_end
3180 * | |
3181 * v |
3182 * NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3 |
3183 * NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3 v
3184 * NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3
3185 * | | | | | | |
3186 * | | | | | | |
3187 * | | | | | | +--> next
3188 * | | | | | +----> val_end
3189 * | | | | +-----------> val_beg
3190 * | | | +--------------> equal
3191 * | | +----------------> att_end
3192 * | +---------------------> att_beg
3193 * +--------------------------> prev
3194 *
3195 */
3196 hdr_beg = ctx.value.ptr;
3197 hdr_end = hdr_beg + ctx.value.len;
3198 for (prev = hdr_beg; prev < hdr_end; prev = next) {
3199 /* Iterate through all cookies on this line */
3200
3201 /* find att_beg */
3202 att_beg = prev;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003203 if (!is_first)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003204 att_beg++;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003205 is_first = 0;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003206
3207 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
3208 att_beg++;
3209
3210 /* find att_end : this is the first character after the last non
3211 * space before the equal. It may be equal to hdr_end.
3212 */
3213 equal = att_end = att_beg;
3214 while (equal < hdr_end) {
3215 if (*equal == '=' || *equal == ',' || *equal == ';')
3216 break;
3217 if (HTTP_IS_SPHT(*equal++))
3218 continue;
3219 att_end = equal;
3220 }
3221
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003222 /* here, <equal> points to '=', a delimiter or the end. <att_end>
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003223 * is between <att_beg> and <equal>, both may be identical.
3224 */
3225 /* look for end of cookie if there is an equal sign */
3226 if (equal < hdr_end && *equal == '=') {
3227 /* look for the beginning of the value */
3228 val_beg = equal + 1;
3229 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
3230 val_beg++;
3231
3232 /* find the end of the value, respecting quotes */
3233 next = http_find_cookie_value_end(val_beg, hdr_end);
3234
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003235 /* make val_end point to the first white space or delimiter after the value */
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003236 val_end = next;
3237 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
3238 val_end--;
3239 }
3240 else
3241 val_beg = val_end = next = equal;
3242
3243 /* We have nothing to do with attributes beginning with
3244 * '$'. However, they will automatically be removed if a
3245 * header before them is removed, since they're supposed
3246 * to be linked together.
3247 */
3248 if (*att_beg == '$')
3249 continue;
3250
3251 /* Ignore cookies with no equal sign */
3252 if (equal == next) {
3253 /* This is not our cookie, so we must preserve it. But if we already
3254 * scheduled another cookie for removal, we cannot remove the
3255 * complete header, but we can remove the previous block itself.
3256 */
3257 preserve_hdr = 1;
3258 if (del_from != NULL) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003259 int delta = http_del_hdr_value(hdr_beg, hdr_end, &del_from, prev);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003260 val_end += delta;
3261 next += delta;
3262 hdr_end += delta;
3263 prev = del_from;
3264 del_from = NULL;
3265 }
3266 continue;
3267 }
3268
3269 /* if there are spaces around the equal sign, we need to
3270 * strip them otherwise we'll get trouble for cookie captures,
3271 * or even for rewrites. Since this happens extremely rarely,
3272 * it does not hurt performance.
3273 */
3274 if (unlikely(att_end != equal || val_beg > equal + 1)) {
3275 int stripped_before = 0;
3276 int stripped_after = 0;
3277
3278 if (att_end != equal) {
3279 memmove(att_end, equal, hdr_end - equal);
3280 stripped_before = (att_end - equal);
3281 equal += stripped_before;
3282 val_beg += stripped_before;
3283 }
3284
3285 if (val_beg > equal + 1) {
3286 memmove(equal + 1, val_beg, hdr_end + stripped_before - val_beg);
3287 stripped_after = (equal + 1) - val_beg;
3288 val_beg += stripped_after;
3289 stripped_before += stripped_after;
3290 }
3291
3292 val_end += stripped_before;
3293 next += stripped_before;
3294 hdr_end += stripped_before;
3295 }
3296 /* now everything is as on the diagram above */
3297
3298 /* First, let's see if we want to capture this cookie. We check
3299 * that we don't already have a client side cookie, because we
3300 * can only capture one. Also as an optimisation, we ignore
3301 * cookies shorter than the declared name.
3302 */
3303 if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL &&
3304 (val_end - att_beg >= sess->fe->capture_namelen) &&
3305 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
3306 int log_len = val_end - att_beg;
3307
3308 if ((txn->cli_cookie = pool_alloc(pool_head_capture)) == NULL) {
3309 ha_alert("HTTP logging : out of memory.\n");
3310 } else {
3311 if (log_len > sess->fe->capture_len)
3312 log_len = sess->fe->capture_len;
3313 memcpy(txn->cli_cookie, att_beg, log_len);
3314 txn->cli_cookie[log_len] = 0;
3315 }
3316 }
3317
3318 /* Persistence cookies in passive, rewrite or insert mode have the
3319 * following form :
3320 *
3321 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
3322 *
3323 * For cookies in prefix mode, the form is :
3324 *
3325 * Cookie: NAME=SRV~VALUE
3326 */
3327 if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
3328 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
3329 struct server *srv = s->be->srv;
3330 char *delim;
3331
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003332 /* if we're in cookie prefix mode, we'll search the delimiter so that we
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003333 * have the server ID between val_beg and delim, and the original cookie between
3334 * delim+1 and val_end. Otherwise, delim==val_end :
3335 *
3336 * hdr_beg
3337 * |
3338 * v
3339 * NAME=SRV; # in all but prefix modes
3340 * NAME=SRV~OPAQUE ; # in prefix mode
3341 * || || | |+-> next
3342 * || || | +--> val_end
3343 * || || +---------> delim
3344 * || |+------------> val_beg
3345 * || +-------------> att_end = equal
3346 * |+-----------------> att_beg
3347 * +------------------> prev
3348 *
3349 */
3350 if (s->be->ck_opts & PR_CK_PFX) {
3351 for (delim = val_beg; delim < val_end; delim++)
3352 if (*delim == COOKIE_DELIM)
3353 break;
3354 }
3355 else {
3356 char *vbar1;
3357 delim = val_end;
3358 /* Now check if the cookie contains a date field, which would
3359 * appear after a vertical bar ('|') just after the server name
3360 * and before the delimiter.
3361 */
3362 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
3363 if (vbar1) {
3364 /* OK, so left of the bar is the server's cookie and
3365 * right is the last seen date. It is a base64 encoded
3366 * 30-bit value representing the UNIX date since the
3367 * epoch in 4-second quantities.
3368 */
3369 int val;
3370 delim = vbar1++;
3371 if (val_end - vbar1 >= 5) {
3372 val = b64tos30(vbar1);
3373 if (val > 0)
3374 txn->cookie_last_date = val << 2;
3375 }
3376 /* look for a second vertical bar */
3377 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
3378 if (vbar1 && (val_end - vbar1 > 5)) {
3379 val = b64tos30(vbar1 + 1);
3380 if (val > 0)
3381 txn->cookie_first_date = val << 2;
3382 }
3383 }
3384 }
3385
3386 /* if the cookie has an expiration date and the proxy wants to check
3387 * it, then we do that now. We first check if the cookie is too old,
3388 * then only if it has expired. We detect strict overflow because the
3389 * time resolution here is not great (4 seconds). Cookies with dates
3390 * in the future are ignored if their offset is beyond one day. This
3391 * allows an admin to fix timezone issues without expiring everyone
3392 * and at the same time avoids keeping unwanted side effects for too
3393 * long.
3394 */
3395 if (txn->cookie_first_date && s->be->cookie_maxlife &&
3396 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) ||
3397 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
3398 txn->flags &= ~TX_CK_MASK;
3399 txn->flags |= TX_CK_OLD;
3400 delim = val_beg; // let's pretend we have not found the cookie
3401 txn->cookie_first_date = 0;
3402 txn->cookie_last_date = 0;
3403 }
3404 else if (txn->cookie_last_date && s->be->cookie_maxidle &&
3405 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) ||
3406 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
3407 txn->flags &= ~TX_CK_MASK;
3408 txn->flags |= TX_CK_EXPIRED;
3409 delim = val_beg; // let's pretend we have not found the cookie
3410 txn->cookie_first_date = 0;
3411 txn->cookie_last_date = 0;
3412 }
3413
3414 /* Here, we'll look for the first running server which supports the cookie.
3415 * This allows to share a same cookie between several servers, for example
3416 * to dedicate backup servers to specific servers only.
3417 * However, to prevent clients from sticking to cookie-less backup server
3418 * when they have incidentely learned an empty cookie, we simply ignore
3419 * empty cookies and mark them as invalid.
3420 * The same behaviour is applied when persistence must be ignored.
3421 */
3422 if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
3423 srv = NULL;
3424
3425 while (srv) {
3426 if (srv->cookie && (srv->cklen == delim - val_beg) &&
3427 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
3428 if ((srv->cur_state != SRV_ST_STOPPED) ||
3429 (s->be->options & PR_O_PERSIST) ||
3430 (s->flags & SF_FORCE_PRST)) {
3431 /* we found the server and we can use it */
3432 txn->flags &= ~TX_CK_MASK;
3433 txn->flags |= (srv->cur_state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
3434 s->flags |= SF_DIRECT | SF_ASSIGNED;
3435 s->target = &srv->obj_type;
3436 break;
3437 } else {
3438 /* we found a server, but it's down,
3439 * mark it as such and go on in case
3440 * another one is available.
3441 */
3442 txn->flags &= ~TX_CK_MASK;
3443 txn->flags |= TX_CK_DOWN;
3444 }
3445 }
3446 srv = srv->next;
3447 }
3448
3449 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
3450 /* no server matched this cookie or we deliberately skipped it */
3451 txn->flags &= ~TX_CK_MASK;
3452 if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
3453 txn->flags |= TX_CK_UNUSED;
3454 else
3455 txn->flags |= TX_CK_INVALID;
3456 }
3457
3458 /* depending on the cookie mode, we may have to either :
3459 * - delete the complete cookie if we're in insert+indirect mode, so that
3460 * the server never sees it ;
3461 * - remove the server id from the cookie value, and tag the cookie as an
Joseph Herlante9d5c722018-11-25 11:00:25 -08003462 * application cookie so that it does not get accidentally removed later,
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003463 * if we're in cookie prefix mode
3464 */
3465 if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
3466 int delta; /* negative */
3467
3468 memmove(val_beg, delim + 1, hdr_end - (delim + 1));
3469 delta = val_beg - (delim + 1);
3470 val_end += delta;
3471 next += delta;
3472 hdr_end += delta;
3473 del_from = NULL;
3474 preserve_hdr = 1; /* we want to keep this cookie */
3475 }
3476 else if (del_from == NULL &&
3477 (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
3478 del_from = prev;
3479 }
3480 }
3481 else {
3482 /* This is not our cookie, so we must preserve it. But if we already
3483 * scheduled another cookie for removal, we cannot remove the
3484 * complete header, but we can remove the previous block itself.
3485 */
3486 preserve_hdr = 1;
3487
3488 if (del_from != NULL) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003489 int delta = http_del_hdr_value(hdr_beg, hdr_end, &del_from, prev);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003490 if (att_beg >= del_from)
3491 att_beg += delta;
3492 if (att_end >= del_from)
3493 att_end += delta;
3494 val_beg += delta;
3495 val_end += delta;
3496 next += delta;
3497 hdr_end += delta;
3498 prev = del_from;
3499 del_from = NULL;
3500 }
3501 }
3502
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003503 } /* for each cookie */
3504
3505
3506 /* There are no more cookies on this line.
3507 * We may still have one (or several) marked for deletion at the
3508 * end of the line. We must do this now in two ways :
3509 * - if some cookies must be preserved, we only delete from the
3510 * mark to the end of line ;
3511 * - if nothing needs to be preserved, simply delete the whole header
3512 */
3513 if (del_from) {
3514 hdr_end = (preserve_hdr ? del_from : hdr_beg);
3515 }
3516 if ((hdr_end - hdr_beg) != ctx.value.len) {
Christopher Faulet3e2638e2019-06-18 09:49:16 +02003517 if (hdr_beg != hdr_end)
3518 htx_change_blk_value_len(htx, ctx.blk, hdr_end - hdr_beg);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003519 else
3520 http_remove_header(htx, &ctx);
3521 }
3522 } /* for each "Cookie header */
3523}
3524
3525/*
3526 * Manage server-side cookies. It can impact performance by about 2% so it is
3527 * desirable to call it only when needed. This function is also used when we
3528 * just need to know if there is a cookie (eg: for check-cache).
3529 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003530static void http_manage_server_side_cookies(struct stream *s, struct channel *res)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003531{
3532 struct session *sess = s->sess;
3533 struct http_txn *txn = s->txn;
3534 struct htx *htx;
3535 struct http_hdr_ctx ctx;
3536 struct server *srv;
3537 char *hdr_beg, *hdr_end;
3538 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau6f7a02a2019-04-15 21:49:49 +02003539 int is_cookie2 = 0;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003540
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003541 htx = htxbuf(&res->buf);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003542
3543 ctx.blk = NULL;
3544 while (1) {
Olivier Houchardf0f42382019-07-22 17:43:46 +02003545 int is_first = 1;
3546
Andrew McDermottbfb15ab2022-02-11 18:26:49 +00003547 if (is_cookie2 || !http_find_header(htx, ist("Set-Cookie"), &ctx, 1)) {
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003548 if (!http_find_header(htx, ist("Set-Cookie2"), &ctx, 1))
3549 break;
3550 is_cookie2 = 1;
3551 }
3552
3553 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
3554 * <prev> points to the colon.
3555 */
3556 txn->flags |= TX_SCK_PRESENT;
3557
3558 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
3559 * check-cache is enabled) and we are not interested in checking
3560 * them. Warning, the cookie capture is declared in the frontend.
3561 */
3562 if (s->be->cookie_name == NULL && sess->fe->capture_name == NULL)
3563 break;
3564
3565 /* OK so now we know we have to process this response cookie.
3566 * The format of the Set-Cookie header is slightly different
3567 * from the format of the Cookie header in that it does not
3568 * support the comma as a cookie delimiter (thus the header
3569 * cannot be folded) because the Expires attribute described in
3570 * the original Netscape's spec may contain an unquoted date
3571 * with a comma inside. We have to live with this because
3572 * many browsers don't support Max-Age and some browsers don't
3573 * support quoted strings. However the Set-Cookie2 header is
3574 * clean.
3575 *
3576 * We have to keep multiple pointers in order to support cookie
3577 * removal at the beginning, middle or end of header without
3578 * corrupting the header (in case of set-cookie2). A special
3579 * pointer, <scav> points to the beginning of the set-cookie-av
3580 * fields after the first semi-colon. The <next> pointer points
3581 * either to the end of line (set-cookie) or next unquoted comma
3582 * (set-cookie2). All of these headers are valid :
3583 *
3584 * hdr_beg hdr_end
3585 * | |
3586 * v |
3587 * NAME1 = VALUE 1 ; Secure; Path="/" |
3588 * NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT v
3589 * NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT
3590 * NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard
3591 * | | | | | | | |
3592 * | | | | | | | +-> next
3593 * | | | | | | +------------> scav
3594 * | | | | | +--------------> val_end
3595 * | | | | +--------------------> val_beg
3596 * | | | +----------------------> equal
3597 * | | +------------------------> att_end
3598 * | +----------------------------> att_beg
3599 * +------------------------------> prev
3600 * -------------------------------> hdr_beg
3601 */
3602 hdr_beg = ctx.value.ptr;
3603 hdr_end = hdr_beg + ctx.value.len;
3604 for (prev = hdr_beg; prev < hdr_end; prev = next) {
3605
3606 /* Iterate through all cookies on this line */
3607
3608 /* find att_beg */
3609 att_beg = prev;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003610 if (!is_first)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003611 att_beg++;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003612 is_first = 0;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003613
3614 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
3615 att_beg++;
3616
3617 /* find att_end : this is the first character after the last non
3618 * space before the equal. It may be equal to hdr_end.
3619 */
3620 equal = att_end = att_beg;
3621
3622 while (equal < hdr_end) {
3623 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
3624 break;
3625 if (HTTP_IS_SPHT(*equal++))
3626 continue;
3627 att_end = equal;
3628 }
3629
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003630 /* here, <equal> points to '=', a delimiter or the end. <att_end>
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003631 * is between <att_beg> and <equal>, both may be identical.
3632 */
3633
3634 /* look for end of cookie if there is an equal sign */
3635 if (equal < hdr_end && *equal == '=') {
3636 /* look for the beginning of the value */
3637 val_beg = equal + 1;
3638 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
3639 val_beg++;
3640
3641 /* find the end of the value, respecting quotes */
3642 next = http_find_cookie_value_end(val_beg, hdr_end);
3643
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003644 /* make val_end point to the first white space or delimiter after the value */
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003645 val_end = next;
3646 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
3647 val_end--;
3648 }
3649 else {
3650 /* <equal> points to next comma, semi-colon or EOL */
3651 val_beg = val_end = next = equal;
3652 }
3653
3654 if (next < hdr_end) {
3655 /* Set-Cookie2 supports multiple cookies, and <next> points to
3656 * a colon or semi-colon before the end. So skip all attr-value
3657 * pairs and look for the next comma. For Set-Cookie, since
3658 * commas are permitted in values, skip to the end.
3659 */
3660 if (is_cookie2)
3661 next = http_find_hdr_value_end(next, hdr_end);
3662 else
3663 next = hdr_end;
3664 }
3665
3666 /* Now everything is as on the diagram above */
3667
3668 /* Ignore cookies with no equal sign */
3669 if (equal == val_end)
3670 continue;
3671
3672 /* If there are spaces around the equal sign, we need to
3673 * strip them otherwise we'll get trouble for cookie captures,
3674 * or even for rewrites. Since this happens extremely rarely,
3675 * it does not hurt performance.
3676 */
3677 if (unlikely(att_end != equal || val_beg > equal + 1)) {
3678 int stripped_before = 0;
3679 int stripped_after = 0;
3680
3681 if (att_end != equal) {
3682 memmove(att_end, equal, hdr_end - equal);
3683 stripped_before = (att_end - equal);
3684 equal += stripped_before;
3685 val_beg += stripped_before;
3686 }
3687
3688 if (val_beg > equal + 1) {
3689 memmove(equal + 1, val_beg, hdr_end + stripped_before - val_beg);
3690 stripped_after = (equal + 1) - val_beg;
3691 val_beg += stripped_after;
3692 stripped_before += stripped_after;
3693 }
3694
3695 val_end += stripped_before;
3696 next += stripped_before;
3697 hdr_end += stripped_before;
3698
Christopher Faulet3e2638e2019-06-18 09:49:16 +02003699 htx_change_blk_value_len(htx, ctx.blk, hdr_end - hdr_beg);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003700 ctx.value.len = hdr_end - hdr_beg;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003701 }
3702
3703 /* First, let's see if we want to capture this cookie. We check
3704 * that we don't already have a server side cookie, because we
3705 * can only capture one. Also as an optimisation, we ignore
3706 * cookies shorter than the declared name.
3707 */
3708 if (sess->fe->capture_name != NULL &&
3709 txn->srv_cookie == NULL &&
3710 (val_end - att_beg >= sess->fe->capture_namelen) &&
3711 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
3712 int log_len = val_end - att_beg;
3713 if ((txn->srv_cookie = pool_alloc(pool_head_capture)) == NULL) {
3714 ha_alert("HTTP logging : out of memory.\n");
3715 }
3716 else {
3717 if (log_len > sess->fe->capture_len)
3718 log_len = sess->fe->capture_len;
3719 memcpy(txn->srv_cookie, att_beg, log_len);
3720 txn->srv_cookie[log_len] = 0;
3721 }
3722 }
3723
3724 srv = objt_server(s->target);
3725 /* now check if we need to process it for persistence */
3726 if (!(s->flags & SF_IGNORE_PRST) &&
3727 (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
3728 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
3729 /* assume passive cookie by default */
3730 txn->flags &= ~TX_SCK_MASK;
3731 txn->flags |= TX_SCK_FOUND;
3732
3733 /* If the cookie is in insert mode on a known server, we'll delete
3734 * this occurrence because we'll insert another one later.
3735 * We'll delete it too if the "indirect" option is set and we're in
3736 * a direct access.
3737 */
3738 if (s->be->ck_opts & PR_CK_PSV) {
3739 /* The "preserve" flag was set, we don't want to touch the
3740 * server's cookie.
3741 */
3742 }
3743 else if ((srv && (s->be->ck_opts & PR_CK_INS)) ||
3744 ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) {
3745 /* this cookie must be deleted */
3746 if (prev == hdr_beg && next == hdr_end) {
3747 /* whole header */
3748 http_remove_header(htx, &ctx);
3749 /* note: while both invalid now, <next> and <hdr_end>
3750 * are still equal, so the for() will stop as expected.
3751 */
3752 } else {
3753 /* just remove the value */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003754 int delta = http_del_hdr_value(hdr_beg, hdr_end, &prev, next);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003755 next = prev;
3756 hdr_end += delta;
3757 }
3758 txn->flags &= ~TX_SCK_MASK;
3759 txn->flags |= TX_SCK_DELETED;
3760 /* and go on with next cookie */
3761 }
3762 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) {
3763 /* replace bytes val_beg->val_end with the cookie name associated
3764 * with this server since we know it.
3765 */
3766 int sliding, delta;
3767
3768 ctx.value = ist2(val_beg, val_end - val_beg);
3769 ctx.lws_before = ctx.lws_after = 0;
3770 http_replace_header_value(htx, &ctx, ist2(srv->cookie, srv->cklen));
3771 delta = srv->cklen - (val_end - val_beg);
3772 sliding = (ctx.value.ptr - val_beg);
3773 hdr_beg += sliding;
3774 val_beg += sliding;
3775 next += sliding + delta;
3776 hdr_end += sliding + delta;
3777
3778 txn->flags &= ~TX_SCK_MASK;
3779 txn->flags |= TX_SCK_REPLACED;
3780 }
3781 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) {
3782 /* insert the cookie name associated with this server
3783 * before existing cookie, and insert a delimiter between them..
3784 */
3785 int sliding, delta;
3786 ctx.value = ist2(val_beg, 0);
3787 ctx.lws_before = ctx.lws_after = 0;
3788 http_replace_header_value(htx, &ctx, ist2(srv->cookie, srv->cklen + 1));
3789 delta = srv->cklen + 1;
3790 sliding = (ctx.value.ptr - val_beg);
3791 hdr_beg += sliding;
3792 val_beg += sliding;
3793 next += sliding + delta;
3794 hdr_end += sliding + delta;
3795
3796 val_beg[srv->cklen] = COOKIE_DELIM;
3797 txn->flags &= ~TX_SCK_MASK;
3798 txn->flags |= TX_SCK_REPLACED;
3799 }
3800 }
3801 /* that's done for this cookie, check the next one on the same
3802 * line when next != hdr_end (only if is_cookie2).
3803 */
3804 }
3805 }
3806}
3807
Christopher Faulet25a02f62018-10-24 12:00:25 +02003808/*
3809 * Parses the Cache-Control and Pragma request header fields to determine if
3810 * the request may be served from the cache and/or if it is cacheable. Updates
3811 * s->txn->flags.
3812 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003813void http_check_request_for_cacheability(struct stream *s, struct channel *req)
Christopher Faulet25a02f62018-10-24 12:00:25 +02003814{
3815 struct http_txn *txn = s->txn;
3816 struct htx *htx;
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003817 struct http_hdr_ctx ctx = { .blk = NULL };
3818 int pragma_found, cc_found;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003819
3820 if ((txn->flags & (TX_CACHEABLE|TX_CACHE_IGNORE)) == TX_CACHE_IGNORE)
3821 return; /* nothing more to do here */
3822
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003823 htx = htxbuf(&req->buf);
Christopher Faulet25a02f62018-10-24 12:00:25 +02003824 pragma_found = cc_found = 0;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003825
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003826 /* Check "pragma" header for HTTP/1.0 compatibility. */
3827 if (http_find_header(htx, ist("pragma"), &ctx, 1)) {
3828 if (isteqi(ctx.value, ist("no-cache"))) {
3829 pragma_found = 1;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003830 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003831 }
Christopher Faulet25a02f62018-10-24 12:00:25 +02003832
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003833 ctx.blk = NULL;
3834 /* Don't use the cache and don't try to store if we found the
3835 * Authorization header */
3836 if (http_find_header(htx, ist("authorization"), &ctx, 1)) {
3837 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3838 txn->flags |= TX_CACHE_IGNORE;
3839 }
Christopher Faulet25a02f62018-10-24 12:00:25 +02003840
Christopher Faulet25a02f62018-10-24 12:00:25 +02003841
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003842 /* Look for "cache-control" header and iterate over all the values
3843 * until we find one that specifies that caching is possible or not. */
3844 ctx.blk = NULL;
3845 while (http_find_header(htx, ist("cache-control"), &ctx, 0)) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003846 cc_found = 1;
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003847 /* We don't check the values after max-age, max-stale nor min-fresh,
3848 * we simply don't use the cache when they're specified. */
3849 if (istmatchi(ctx.value, ist("max-age")) ||
3850 istmatchi(ctx.value, ist("no-cache")) ||
3851 istmatchi(ctx.value, ist("max-stale")) ||
3852 istmatchi(ctx.value, ist("min-fresh"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003853 txn->flags |= TX_CACHE_IGNORE;
3854 continue;
3855 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003856 if (istmatchi(ctx.value, ist("no-store"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003857 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3858 continue;
3859 }
3860 }
3861
3862 /* RFC7234#5.4:
3863 * When the Cache-Control header field is also present and
3864 * understood in a request, Pragma is ignored.
3865 * When the Cache-Control header field is not present in a
3866 * request, caches MUST consider the no-cache request
3867 * pragma-directive as having the same effect as if
3868 * "Cache-Control: no-cache" were present.
3869 */
3870 if (!cc_found && pragma_found)
3871 txn->flags |= TX_CACHE_IGNORE;
3872}
3873
3874/*
3875 * Check if response is cacheable or not. Updates s->txn->flags.
3876 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003877void http_check_response_for_cacheability(struct stream *s, struct channel *res)
Christopher Faulet25a02f62018-10-24 12:00:25 +02003878{
3879 struct http_txn *txn = s->txn;
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003880 struct http_hdr_ctx ctx = { .blk = NULL };
Christopher Faulet25a02f62018-10-24 12:00:25 +02003881 struct htx *htx;
Remi Tricot-Le Bretoncc9bf2e2020-11-12 11:14:41 +01003882 int has_freshness_info = 0;
3883 int has_validator = 0;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003884
3885 if (txn->status < 200) {
3886 /* do not try to cache interim responses! */
3887 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3888 return;
3889 }
3890
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003891 htx = htxbuf(&res->buf);
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003892 /* Check "pragma" header for HTTP/1.0 compatibility. */
3893 if (http_find_header(htx, ist("pragma"), &ctx, 1)) {
3894 if (isteqi(ctx.value, ist("no-cache"))) {
3895 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3896 return;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003897 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003898 }
Christopher Faulet25a02f62018-10-24 12:00:25 +02003899
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003900 /* Look for "cache-control" header and iterate over all the values
3901 * until we find one that specifies that caching is possible or not. */
3902 ctx.blk = NULL;
3903 while (http_find_header(htx, ist("cache-control"), &ctx, 0)) {
3904 if (isteqi(ctx.value, ist("public"))) {
3905 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003906 continue;
3907 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003908 if (isteqi(ctx.value, ist("private")) ||
3909 isteqi(ctx.value, ist("no-cache")) ||
3910 isteqi(ctx.value, ist("no-store")) ||
3911 isteqi(ctx.value, ist("max-age=0")) ||
3912 isteqi(ctx.value, ist("s-maxage=0"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003913 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003914 continue;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003915 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003916 /* We might have a no-cache="set-cookie" form. */
3917 if (istmatchi(ctx.value, ist("no-cache=\"set-cookie"))) {
3918 txn->flags &= ~TX_CACHE_COOK;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003919 continue;
3920 }
Remi Tricot-Le Bretoncc9bf2e2020-11-12 11:14:41 +01003921
3922 if (istmatchi(ctx.value, ist("s-maxage")) ||
3923 istmatchi(ctx.value, ist("max-age"))) {
3924 has_freshness_info = 1;
3925 continue;
3926 }
3927 }
3928
3929 /* If no freshness information could be found in Cache-Control values,
3930 * look for an Expires header. */
3931 if (!has_freshness_info) {
3932 ctx.blk = NULL;
3933 has_freshness_info = http_find_header(htx, ist("expires"), &ctx, 0);
Christopher Faulet25a02f62018-10-24 12:00:25 +02003934 }
Remi Tricot-Le Bretoncc9bf2e2020-11-12 11:14:41 +01003935
3936 /* If no freshness information could be found in Cache-Control or Expires
3937 * values, look for an explicit validator. */
3938 if (!has_freshness_info) {
3939 ctx.blk = NULL;
3940 has_validator = 1;
3941 if (!http_find_header(htx, ist("etag"), &ctx, 0)) {
3942 ctx.blk = NULL;
3943 if (!http_find_header(htx, ist("last-modified"), &ctx, 0))
3944 has_validator = 0;
3945 }
3946 }
3947
3948 /* We won't store an entry that has neither a cache validator nor an
3949 * explicit expiration time, as suggested in RFC 7234#3. */
3950 if (!has_freshness_info && !has_validator)
3951 txn->flags |= TX_CACHE_IGNORE;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003952}
3953
Christopher Faulet377c5a52018-10-24 21:21:30 +02003954/*
3955 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
3956 * for the current backend.
3957 *
3958 * It is assumed that the request is either a HEAD, GET, or POST and that the
3959 * uri_auth field is valid.
3960 *
3961 * Returns 1 if stats should be provided, otherwise 0.
3962 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003963static int http_stats_check_uri(struct stream *s, struct http_txn *txn, struct proxy *backend)
Christopher Faulet377c5a52018-10-24 21:21:30 +02003964{
3965 struct uri_auth *uri_auth = backend->uri_auth;
3966 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003967 struct htx_sl *sl;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003968 struct ist uri;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003969
3970 if (!uri_auth)
3971 return 0;
3972
3973 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
3974 return 0;
3975
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003976 htx = htxbuf(&s->req.buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02003977 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003978 uri = htx_sl_req_uri(sl);
Amaury Denoyellec453f952021-07-06 11:40:12 +02003979 if (*uri_auth->uri_prefix == '/') {
3980 struct http_uri_parser parser = http_uri_parser_init(uri);
3981 uri = http_parse_path(&parser);
3982 }
Christopher Faulet377c5a52018-10-24 21:21:30 +02003983
3984 /* check URI size */
3985 if (uri_auth->uri_len > uri.len)
3986 return 0;
3987
3988 if (memcmp(uri.ptr, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
3989 return 0;
3990
3991 return 1;
3992}
3993
3994/* This function prepares an applet to handle the stats. It can deal with the
3995 * "100-continue" expectation, check that admin rules are met for POST requests,
3996 * and program a response message if something was unexpected. It cannot fail
3997 * and always relies on the stats applet to complete the job. It does not touch
3998 * analysers nor counters, which are left to the caller. It does not touch
3999 * s->target which is supposed to already point to the stats applet. The caller
4000 * is expected to have already assigned an appctx to the stream.
4001 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004002static int http_handle_stats(struct stream *s, struct channel *req)
Christopher Faulet377c5a52018-10-24 21:21:30 +02004003{
4004 struct stats_admin_rule *stats_admin_rule;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004005 struct session *sess = s->sess;
4006 struct http_txn *txn = s->txn;
4007 struct http_msg *msg = &txn->req;
4008 struct uri_auth *uri_auth = s->be->uri_auth;
4009 const char *h, *lookup, *end;
Willy Tarreau8e7c6e62022-05-18 17:58:02 +02004010 struct appctx *appctx = __sc_appctx(s->scb);
Willy Tarreau91cefca2022-05-03 17:08:29 +02004011 struct show_stat_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx));
Christopher Faulet377c5a52018-10-24 21:21:30 +02004012 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004013 struct htx_sl *sl;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004014
Willy Tarreau41f88522022-05-03 18:39:27 +02004015 appctx->st1 = 0;
Willy Tarreau6ef16482022-05-06 18:07:53 +02004016 ctx->state = STAT_STATE_INIT;
Willy Tarreau91cefca2022-05-03 17:08:29 +02004017 ctx->st_code = STAT_STATUS_INIT;
4018 ctx->flags |= uri_auth->flags;
4019 ctx->flags |= STAT_FMT_HTML; /* assume HTML mode by default */
Christopher Faulet377c5a52018-10-24 21:21:30 +02004020 if ((msg->flags & HTTP_MSGF_VER_11) && (txn->meth != HTTP_METH_HEAD))
Willy Tarreau91cefca2022-05-03 17:08:29 +02004021 ctx->flags |= STAT_CHUNKED;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004022
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004023 htx = htxbuf(&req->buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02004024 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004025 lookup = HTX_SL_REQ_UPTR(sl) + uri_auth->uri_len;
4026 end = HTX_SL_REQ_UPTR(sl) + HTX_SL_REQ_ULEN(sl);
Christopher Faulet377c5a52018-10-24 21:21:30 +02004027
4028 for (h = lookup; h <= end - 3; h++) {
4029 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02004030 ctx->flags |= STAT_HIDE_DOWN;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004031 break;
4032 }
Amaury Denoyelle91e55ea2021-02-25 14:46:08 +01004033 }
4034
4035 for (h = lookup; h <= end - 9; h++) {
4036 if (memcmp(h, ";no-maint", 9) == 0) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02004037 ctx->flags |= STAT_HIDE_MAINT;
Willy Tarreau3e320362020-10-23 17:28:57 +02004038 break;
4039 }
Christopher Faulet377c5a52018-10-24 21:21:30 +02004040 }
4041
4042 if (uri_auth->refresh) {
4043 for (h = lookup; h <= end - 10; h++) {
4044 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02004045 ctx->flags |= STAT_NO_REFRESH;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004046 break;
4047 }
4048 }
4049 }
4050
4051 for (h = lookup; h <= end - 4; h++) {
4052 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02004053 ctx->flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
Christopher Faulet377c5a52018-10-24 21:21:30 +02004054 break;
4055 }
4056 }
4057
4058 for (h = lookup; h <= end - 6; h++) {
4059 if (memcmp(h, ";typed", 6) == 0) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02004060 ctx->flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
4061 ctx->flags |= STAT_FMT_TYPED;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004062 break;
4063 }
4064 }
4065
Christopher Faulet6338a082019-09-09 15:50:54 +02004066 for (h = lookup; h <= end - 5; h++) {
4067 if (memcmp(h, ";json", 5) == 0) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02004068 ctx->flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
4069 ctx->flags |= STAT_FMT_JSON;
Christopher Faulet6338a082019-09-09 15:50:54 +02004070 break;
4071 }
4072 }
4073
4074 for (h = lookup; h <= end - 12; h++) {
4075 if (memcmp(h, ";json-schema", 12) == 0) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02004076 ctx->flags &= ~STAT_FMT_MASK;
4077 ctx->flags |= STAT_JSON_SCHM;
Christopher Faulet6338a082019-09-09 15:50:54 +02004078 break;
4079 }
4080 }
4081
Christopher Faulet377c5a52018-10-24 21:21:30 +02004082 for (h = lookup; h <= end - 8; h++) {
4083 if (memcmp(h, ";st=", 4) == 0) {
4084 int i;
4085 h += 4;
Willy Tarreau91cefca2022-05-03 17:08:29 +02004086 ctx->st_code = STAT_STATUS_UNKN;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004087 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
4088 if (strncmp(stat_status_codes[i], h, 4) == 0) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02004089 ctx->st_code = i;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004090 break;
4091 }
4092 }
4093 break;
4094 }
4095 }
4096
Willy Tarreau91cefca2022-05-03 17:08:29 +02004097 ctx->scope_str = 0;
4098 ctx->scope_len = 0;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004099 for (h = lookup; h <= end - 8; h++) {
4100 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
4101 int itx = 0;
4102 const char *h2;
4103 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
4104 const char *err;
4105
4106 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
4107 h2 = h;
Willy Tarreau91cefca2022-05-03 17:08:29 +02004108 ctx->scope_str = h2 - HTX_SL_REQ_UPTR(sl);
Christopher Fauleted7a0662019-01-14 11:07:34 +01004109 while (h < end) {
Christopher Faulet377c5a52018-10-24 21:21:30 +02004110 if (*h == ';' || *h == '&' || *h == ' ')
4111 break;
4112 itx++;
4113 h++;
4114 }
4115
4116 if (itx > STAT_SCOPE_TXT_MAXLEN)
4117 itx = STAT_SCOPE_TXT_MAXLEN;
Willy Tarreau91cefca2022-05-03 17:08:29 +02004118 ctx->scope_len = itx;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004119
Willy Tarreau91cefca2022-05-03 17:08:29 +02004120 /* scope_txt = search query, ctx->scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Christopher Faulet377c5a52018-10-24 21:21:30 +02004121 memcpy(scope_txt, h2, itx);
4122 scope_txt[itx] = '\0';
4123 err = invalid_char(scope_txt);
4124 if (err) {
4125 /* bad char in search text => clear scope */
Willy Tarreau91cefca2022-05-03 17:08:29 +02004126 ctx->scope_str = 0;
4127 ctx->scope_len = 0;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004128 }
4129 break;
4130 }
4131 }
4132
4133 /* now check whether we have some admin rules for this request */
4134 list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) {
4135 int ret = 1;
4136
4137 if (stats_admin_rule->cond) {
4138 ret = acl_exec_cond(stats_admin_rule->cond, s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
4139 ret = acl_pass(ret);
4140 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
4141 ret = !ret;
4142 }
4143
4144 if (ret) {
4145 /* no rule, or the rule matches */
Willy Tarreau91cefca2022-05-03 17:08:29 +02004146 ctx->flags |= STAT_ADMIN;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004147 break;
4148 }
4149 }
4150
Christopher Faulet5d45e382019-02-27 15:15:23 +01004151 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
4152 appctx->st0 = STAT_HTTP_HEAD;
4153 else if (txn->meth == HTTP_METH_POST) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02004154 if (ctx->flags & STAT_ADMIN) {
Christopher Faulet377c5a52018-10-24 21:21:30 +02004155 appctx->st0 = STAT_HTTP_POST;
Christopher Fauletbd9e8422019-08-15 22:26:48 +02004156 if (msg->msg_state < HTTP_MSG_DATA)
4157 req->analysers |= AN_REQ_HTTP_BODY;
4158 }
Christopher Faulet377c5a52018-10-24 21:21:30 +02004159 else {
Christopher Faulet5d45e382019-02-27 15:15:23 +01004160 /* POST without admin level */
Willy Tarreau91cefca2022-05-03 17:08:29 +02004161 ctx->flags &= ~STAT_CHUNKED;
4162 ctx->st_code = STAT_STATUS_DENY;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004163 appctx->st0 = STAT_HTTP_LAST;
4164 }
4165 }
4166 else {
Christopher Faulet5d45e382019-02-27 15:15:23 +01004167 /* Unsupported method */
Willy Tarreau91cefca2022-05-03 17:08:29 +02004168 ctx->flags &= ~STAT_CHUNKED;
4169 ctx->st_code = STAT_STATUS_IVAL;
Christopher Faulet5d45e382019-02-27 15:15:23 +01004170 appctx->st0 = STAT_HTTP_LAST;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004171 }
4172
4173 s->task->nice = -32; /* small boost for HTTP statistics */
4174 return 1;
4175}
4176
Christopher Faulet021a8e42021-03-29 10:46:38 +02004177/* This function waits for the message payload at most <time> milliseconds (may
4178 * be set to TICK_ETERNITY). It stops to wait if at least <bytes> bytes of the
4179 * payload are received (0 means no limit). It returns HTTP_RULE_* depending on
4180 * the result:
4181 *
4182 * - HTTP_RULE_RES_CONT when conditions are met to stop waiting
4183 * - HTTP_RULE_RES_YIELD to wait for more data
Ilya Shipitsinb2be9a12021-04-24 13:25:42 +05004184 * - HTTP_RULE_RES_ABRT when a timeout occurred.
Christopher Faulet021a8e42021-03-29 10:46:38 +02004185 * - HTTP_RULE_RES_BADREQ if a parsing error is raised by lower level
Ilya Shipitsinb2be9a12021-04-24 13:25:42 +05004186 * - HTTP_RULE_RES_ERROR if an internal error occurred
Christopher Faulet021a8e42021-03-29 10:46:38 +02004187 *
Ilya Shipitsinb2be9a12021-04-24 13:25:42 +05004188 * If a timeout occurred, this function is responsible to emit the right response
Christopher Faulet021a8e42021-03-29 10:46:38 +02004189 * to the client, depending on the channel (408 on request side, 504 on response
4190 * side). All other errors must be handled by the caller.
4191 */
4192enum rule_result http_wait_for_msg_body(struct stream *s, struct channel *chn,
4193 unsigned int time, unsigned int bytes)
4194{
4195 struct session *sess = s->sess;
4196 struct http_txn *txn = s->txn;
4197 struct http_msg *msg = ((chn->flags & CF_ISRESP) ? &txn->rsp : &txn->req);
4198 struct htx *htx;
4199 enum rule_result ret = HTTP_RULE_RES_CONT;
4200
4201 htx = htxbuf(&chn->buf);
4202
4203 if (htx->flags & HTX_FL_PARSING_ERROR) {
4204 ret = HTTP_RULE_RES_BADREQ;
4205 goto end;
4206 }
4207 if (htx->flags & HTX_FL_PROCESSING_ERROR) {
4208 ret = HTTP_RULE_RES_ERROR;
4209 goto end;
4210 }
4211
4212 /* Do nothing for bodyless and CONNECT requests */
4213 if (txn->meth == HTTP_METH_CONNECT || (msg->flags & HTTP_MSGF_BODYLESS))
4214 goto end;
4215
4216 if (!(chn->flags & CF_ISRESP) && msg->msg_state < HTTP_MSG_DATA) {
4217 if (http_handle_expect_hdr(s, htx, msg) == -1) {
4218 ret = HTTP_RULE_RES_ERROR;
4219 goto end;
4220 }
4221 }
4222
4223 msg->msg_state = HTTP_MSG_DATA;
4224
4225 /* Now we're in HTTP_MSG_DATA. We just need to know if all data have
4226 * been received or if the buffer is full.
4227 */
Christopher Faulet78335962021-09-23 14:46:32 +02004228 if ((htx->flags & HTX_FL_EOM) ||
4229 htx_get_tail_type(htx) > HTX_BLK_DATA ||
4230 channel_htx_full(chn, htx, global.tune.maxrewrite) ||
Willy Tarreau99615ed2022-05-25 07:29:36 +02004231 sc_waiting_room(chn_prod(chn)))
Christopher Faulet021a8e42021-03-29 10:46:38 +02004232 goto end;
4233
4234 if (bytes) {
4235 struct htx_blk *blk;
4236 unsigned int len = 0;
4237
4238 for (blk = htx_get_first_blk(htx); blk; blk = htx_get_next_blk(htx, blk)) {
4239 if (htx_get_blk_type(blk) != HTX_BLK_DATA)
4240 continue;
4241 len += htx_get_blksz(blk);
4242 if (len >= bytes)
4243 goto end;
4244 }
4245 }
4246
4247 if ((chn->flags & CF_READ_TIMEOUT) || tick_is_expired(chn->analyse_exp, now_ms)) {
4248 if (!(chn->flags & CF_ISRESP))
4249 goto abort_req;
4250 goto abort_res;
4251 }
4252
4253 /* we get here if we need to wait for more data */
4254 if (!(chn->flags & (CF_SHUTR | CF_READ_ERROR))) {
4255 if (!tick_isset(chn->analyse_exp))
4256 chn->analyse_exp = tick_add_ifset(now_ms, time);
4257 ret = HTTP_RULE_RES_YIELD;
4258 }
4259
4260 end:
4261 return ret;
4262
4263 abort_req:
4264 txn->status = 408;
4265 if (!(s->flags & SF_ERR_MASK))
4266 s->flags |= SF_ERR_CLITO;
4267 if (!(s->flags & SF_FINST_MASK))
4268 s->flags |= SF_FINST_D;
Willy Tarreau4781b152021-04-06 13:53:36 +02004269 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req);
Christopher Faulet021a8e42021-03-29 10:46:38 +02004270 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02004271 _HA_ATOMIC_INC(&sess->listener->counters->failed_req);
Christopher Faulet021a8e42021-03-29 10:46:38 +02004272 http_reply_and_close(s, txn->status, http_error_message(s));
4273 ret = HTTP_RULE_RES_ABRT;
4274 goto end;
4275
4276 abort_res:
4277 txn->status = 504;
4278 if (!(s->flags & SF_ERR_MASK))
4279 s->flags |= SF_ERR_SRVTO;
4280 if (!(s->flags & SF_FINST_MASK))
4281 s->flags |= SF_FINST_D;
4282 stream_inc_http_fail_ctr(s);
4283 http_reply_and_close(s, txn->status, http_error_message(s));
4284 ret = HTTP_RULE_RES_ABRT;
4285 goto end;
4286}
4287
Willy Tarreaub49672d2022-05-27 10:13:37 +02004288void http_perform_server_redirect(struct stream *s, struct stconn *sc)
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004289{
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004290 struct channel *req = &s->req;
4291 struct channel *res = &s->res;
4292 struct server *srv;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004293 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004294 struct htx_sl *sl;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004295 struct ist path, location;
4296 unsigned int flags;
Amaury Denoyellec453f952021-07-06 11:40:12 +02004297 struct http_uri_parser parser;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004298
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004299 /*
4300 * Create the location
4301 */
4302 chunk_reset(&trash);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004303
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004304 /* 1: add the server's prefix */
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004305 /* special prefix "/" means don't change URL */
4306 srv = __objt_server(s->target);
4307 if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') {
4308 if (!chunk_memcat(&trash, srv->rdr_pfx, srv->rdr_len))
4309 return;
4310 }
4311
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004312 /* 2: add the request Path */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004313 htx = htxbuf(&req->buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02004314 sl = http_get_stline(htx);
Amaury Denoyellec453f952021-07-06 11:40:12 +02004315 parser = http_uri_parser_init(htx_sl_req_uri(sl));
4316 path = http_parse_path(&parser);
Tim Duesterhused526372020-03-05 17:56:33 +01004317 if (!isttest(path))
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004318 return;
4319
4320 if (!chunk_memcat(&trash, path.ptr, path.len))
4321 return;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004322 location = ist2(trash.area, trash.data);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004323
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004324 /*
4325 * Create the 302 respone
4326 */
4327 htx = htx_from_buf(&res->buf);
4328 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
4329 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
4330 ist("HTTP/1.1"), ist("302"), ist("Found"));
4331 if (!sl)
4332 goto fail;
4333 sl->info.res.status = 302;
4334 s->txn->status = 302;
4335
4336 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) ||
4337 !htx_add_header(htx, ist("Connection"), ist("close")) ||
4338 !htx_add_header(htx, ist("Content-length"), ist("0")) ||
4339 !htx_add_header(htx, ist("Location"), location))
4340 goto fail;
4341
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004342 if (!htx_add_endof(htx, HTX_BLK_EOH))
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004343 goto fail;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004344
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004345 htx->flags |= HTX_FL_EOM;
Christopher Fauletc20afb82020-01-24 19:16:26 +01004346 htx_to_buf(htx, &res->buf);
Christopher Fauleta72a7e42020-01-28 09:28:11 +01004347 if (!http_forward_proxy_resp(s, 1))
4348 goto fail;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004349
4350 /* return without error. */
Willy Tarreaub49672d2022-05-27 10:13:37 +02004351 sc_shutr(sc);
4352 sc_shutw(sc);
Christopher Faulet50264b42022-03-30 19:39:30 +02004353 s->conn_err_type = STRM_ET_NONE;
Willy Tarreaub49672d2022-05-27 10:13:37 +02004354 sc->state = SC_ST_CLO;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004355
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004356 if (!(s->flags & SF_ERR_MASK))
4357 s->flags |= SF_ERR_LOCAL;
4358 if (!(s->flags & SF_FINST_MASK))
4359 s->flags |= SF_FINST_C;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004360
4361 /* FIXME: we should increase a counter of redirects per server and per backend. */
4362 srv_inc_sess_ctr(srv);
4363 srv_set_sess_last(srv);
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004364 return;
4365
4366 fail:
4367 /* If an error occurred, remove the incomplete HTTP response from the
4368 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004369 channel_htx_truncate(res, htx);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004370}
4371
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05004372/* This function terminates the request because it was completely analyzed or
Christopher Fauletf2824e62018-10-01 12:12:37 +02004373 * because an error was triggered during the body forwarding.
4374 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004375static void http_end_request(struct stream *s)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004376{
4377 struct channel *chn = &s->req;
4378 struct http_txn *txn = s->txn;
4379
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004380 DBG_TRACE_ENTER(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004381
Christopher Fauletb42a8b62018-11-19 21:59:00 +01004382 if (unlikely(txn->req.msg_state == HTTP_MSG_ERROR ||
4383 txn->rsp.msg_state == HTTP_MSG_ERROR)) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004384 channel_abort(chn);
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004385 channel_htx_truncate(chn, htxbuf(&chn->buf));
Christopher Fauletf2824e62018-10-01 12:12:37 +02004386 goto end;
4387 }
4388
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004389 if (unlikely(txn->req.msg_state < HTTP_MSG_DONE)) {
4390 DBG_TRACE_DEVEL("waiting end of the request", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004391 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004392 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004393
4394 if (txn->req.msg_state == HTTP_MSG_DONE) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004395 /* No need to read anymore, the request was completely parsed.
4396 * We can shut the read side unless we want to abort_on_close,
4397 * or we have a POST request. The issue with POST requests is
4398 * that some browsers still send a CRLF after the request, and
4399 * this CRLF must be read so that it does not remain in the kernel
4400 * buffers, otherwise a close could cause an RST on some systems
4401 * (eg: Linux).
4402 */
Christopher Faulet769d0e92019-03-22 14:23:18 +01004403 if (!(s->be->options & PR_O_ABRT_CLOSE) && txn->meth != HTTP_METH_POST)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004404 channel_dont_read(chn);
4405
4406 /* if the server closes the connection, we want to immediately react
4407 * and close the socket to save packets and syscalls.
4408 */
Willy Tarreaucb041662022-05-17 19:44:42 +02004409 s->scb->flags |= SC_FL_NOHALF;
Christopher Fauletf2824e62018-10-01 12:12:37 +02004410
4411 /* In any case we've finished parsing the request so we must
4412 * disable Nagle when sending data because 1) we're not going
4413 * to shut this side, and 2) the server is waiting for us to
4414 * send pending data.
4415 */
4416 chn->flags |= CF_NEVER_WAIT;
4417
Christopher Fauletd01ce402019-01-02 17:44:13 +01004418 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
4419 /* The server has not finished to respond, so we
4420 * don't want to move in order not to upset it.
4421 */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004422 DBG_TRACE_DEVEL("waiting end of the response", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletd01ce402019-01-02 17:44:13 +01004423 return;
4424 }
4425
Christopher Fauletf2824e62018-10-01 12:12:37 +02004426 /* When we get here, it means that both the request and the
4427 * response have finished receiving. Depending on the connection
4428 * mode, we'll have to wait for the last bytes to leave in either
4429 * direction, and sometimes for a close to be effective.
4430 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02004431 if (txn->flags & TX_CON_WANT_TUN) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004432 /* Tunnel mode will not have any analyser so it needs to
4433 * poll for reads.
4434 */
4435 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004436 if (b_data(&chn->buf)) {
4437 DBG_TRACE_DEVEL("waiting to flush the request", STRM_EV_HTTP_ANA, s, txn);
Christopher Faulet9768c262018-10-22 09:34:31 +02004438 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004439 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004440 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 Fauleteea8fc72019-11-05 16:18:10 +01004559 if (b_data(&chn->buf)) {
4560 DBG_TRACE_DEVEL("waiting to flush the respone", STRM_EV_HTTP_ANA, s, txn);
Christopher Faulet9768c262018-10-22 09:34:31 +02004561 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004562 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004563 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
4564 }
4565 else {
4566 /* we're not expecting any new data to come for this
4567 * transaction, so we can close it.
4568 */
4569 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4570 channel_shutr_now(chn);
4571 channel_shutw_now(chn);
4572 }
4573 }
4574 goto check_channel_flags;
4575 }
4576
4577 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
4578 http_msg_closing:
4579 /* nothing else to forward, just waiting for the output buffer
4580 * to be empty and for the shutw_now to take effect.
4581 */
4582 if (channel_is_empty(chn)) {
4583 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4584 goto http_msg_closed;
4585 }
4586 else if (chn->flags & CF_SHUTW) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004587 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau4781b152021-04-06 13:53:36 +02004588 _HA_ATOMIC_INC(&strm_sess(s)->fe->fe_counters.cli_aborts);
4589 _HA_ATOMIC_INC(&s->be->be_counters.cli_aborts);
William Lallemand36119de2021-03-08 15:26:48 +01004590 if (strm_sess(s)->listener && strm_sess(s)->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02004591 _HA_ATOMIC_INC(&strm_sess(s)->listener->counters->cli_aborts);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004592 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02004593 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.cli_aborts);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004594 goto end;
4595 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004596 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004597 return;
4598 }
4599
4600 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
4601 http_msg_closed:
4602 /* drop any pending data */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004603 channel_htx_truncate(&s->req, htxbuf(&s->req.buf));
Christopher Faulet9768c262018-10-22 09:34:31 +02004604 channel_abort(&s->req);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004605 goto end;
4606 }
4607
4608 check_channel_flags:
4609 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4610 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4611 /* if we've just closed an output, let's switch */
4612 txn->rsp.msg_state = HTTP_MSG_CLOSING;
4613 goto http_msg_closing;
4614 }
4615
4616 end:
4617 chn->analysers &= AN_RES_FLT_END;
Christopher Faulet198ef8b2020-12-15 13:32:55 +01004618 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
4619 chn->flags |= CF_NEVER_WAIT;
4620 if (HAS_RSP_DATA_FILTERS(s))
4621 chn->analysers |= AN_RES_FLT_XFER_DATA;
4622 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004623 channel_auto_close(chn);
4624 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004625 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004626}
4627
Christopher Fauletef70e252020-01-28 09:26:19 +01004628/* Forward a response generated by HAProxy (error/redirect/return). This
4629 * function forwards all pending incoming data. If <final> is set to 0, nothing
4630 * more is performed. It is used for 1xx informational messages. Otherwise, the
Christopher Faulet507479b2020-05-15 12:29:46 +02004631 * transaction is terminated and the request is emptied. On success 1 is
Christopher Faulet40e6b552020-06-25 16:04:50 +02004632 * returned. If an error occurred, 0 is returned. If it fails, this function
4633 * only exits. It is the caller responsibility to do the cleanup.
Christopher Fauletef70e252020-01-28 09:26:19 +01004634 */
4635int http_forward_proxy_resp(struct stream *s, int final)
4636{
4637 struct channel *req = &s->req;
4638 struct channel *res = &s->res;
4639 struct htx *htx = htxbuf(&res->buf);
4640 size_t data;
4641
4642 if (final) {
4643 htx->flags |= HTX_FL_PROXY_RESP;
Christopher Faulet507479b2020-05-15 12:29:46 +02004644
Christopher Fauletaab1b672020-11-18 16:44:02 +01004645 if (!htx_is_empty(htx) && !http_eval_after_res_rules(s))
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01004646 return 0;
Christopher Fauletef70e252020-01-28 09:26:19 +01004647
Christopher Fauletd6c48362020-10-19 18:01:38 +02004648 if (s->txn->meth == HTTP_METH_HEAD)
4649 htx_skip_msg_payload(htx);
4650
Christopher Fauletef70e252020-01-28 09:26:19 +01004651 channel_auto_read(req);
4652 channel_abort(req);
4653 channel_auto_close(req);
4654 channel_htx_erase(req, htxbuf(&req->buf));
4655
4656 res->wex = tick_add_ifset(now_ms, res->wto);
4657 channel_auto_read(res);
4658 channel_auto_close(res);
4659 channel_shutr_now(res);
Christopher Faulet1a9db7c2020-06-25 15:36:45 +02004660 res->flags |= CF_EOI; /* The response is terminated, add EOI */
Christopher Faulet42432f32020-11-20 17:43:16 +01004661 htxbuf(&res->buf)->flags |= HTX_FL_EOM; /* no more data are expected */
Christopher Fauletef70e252020-01-28 09:26:19 +01004662 }
Christopher Fauletcf6898c2020-06-25 15:55:11 +02004663 else {
4664 /* Send ASAP informational messages. Rely on CF_EOI for final
4665 * response.
4666 */
4667 res->flags |= CF_SEND_DONTWAIT;
4668 }
Christopher Fauletef70e252020-01-28 09:26:19 +01004669
4670 data = htx->data - co_data(res);
4671 c_adv(res, data);
4672 htx->first = -1;
4673 res->total += data;
4674 return 1;
4675}
4676
Willy Tarreaub49672d2022-05-27 10:13:37 +02004677void http_server_error(struct stream *s, struct stconn *sc, int err,
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004678 int finst, struct http_reply *msg)
Christopher Faulet0f226952018-10-22 09:29:56 +02004679{
Christopher Faulet72c7d8d2020-01-27 15:32:25 +01004680 http_reply_and_close(s, s->txn->status, msg);
Christopher Faulet0f226952018-10-22 09:29:56 +02004681 if (!(s->flags & SF_ERR_MASK))
4682 s->flags |= err;
4683 if (!(s->flags & SF_FINST_MASK))
4684 s->flags |= finst;
4685}
4686
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004687void http_reply_and_close(struct stream *s, short status, struct http_reply *msg)
Christopher Faulet0f226952018-10-22 09:29:56 +02004688{
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004689 if (!msg) {
4690 channel_htx_truncate(&s->res, htxbuf(&s->res.buf));
4691 goto end;
4692 }
4693
4694 if (http_reply_message(s, msg) == -1) {
4695 /* On error, return a 500 error message, but don't rewrite it if
Christopher Faulet40e6b552020-06-25 16:04:50 +02004696 * it is already an internal error. If it was already a "const"
4697 * 500 error, just fail.
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004698 */
Christopher Faulet40e6b552020-06-25 16:04:50 +02004699 if (s->txn->status == 500) {
4700 if (s->txn->flags & TX_CONST_REPLY)
4701 goto end;
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004702 s->txn->flags |= TX_CONST_REPLY;
Christopher Faulet40e6b552020-06-25 16:04:50 +02004703 }
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004704 s->txn->status = 500;
4705 s->txn->http_reply = NULL;
4706 return http_reply_and_close(s, s->txn->status, http_error_message(s));
4707 }
4708
4709end:
4710 s->res.wex = tick_add_ifset(now_ms, s->res.wto);
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004711
Christopher Faulet2d565002021-09-10 09:17:50 +02004712 /* At this staged, HTTP analysis is finished */
4713 s->req.analysers &= AN_REQ_FLT_END;
4714 s->req.analyse_exp = TICK_ETERNITY;
4715
4716 s->res.analysers &= AN_RES_FLT_END;
4717 s->res.analyse_exp = TICK_ETERNITY;
4718
Christopher Faulet0f226952018-10-22 09:29:56 +02004719 channel_auto_read(&s->req);
4720 channel_abort(&s->req);
4721 channel_auto_close(&s->req);
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004722 channel_htx_erase(&s->req, htxbuf(&s->req.buf));
Christopher Faulet72c7d8d2020-01-27 15:32:25 +01004723 channel_auto_read(&s->res);
4724 channel_auto_close(&s->res);
4725 channel_shutr_now(&s->res);
Christopher Faulet0f226952018-10-22 09:29:56 +02004726}
4727
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004728struct http_reply *http_error_message(struct stream *s)
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004729{
4730 const int msgnum = http_get_status_idx(s->txn->status);
4731
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004732 if (s->txn->http_reply)
4733 return s->txn->http_reply;
4734 else if (s->be->replies[msgnum])
4735 return s->be->replies[msgnum];
4736 else if (strm_fe(s)->replies[msgnum])
4737 return strm_fe(s)->replies[msgnum];
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004738 else
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004739 return &http_err_replies[msgnum];
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004740}
4741
Christopher Faulet40e6b552020-06-25 16:04:50 +02004742/* Produces an HTX message from an http reply. Depending on the http reply type,
4743 * a, errorfile, an raw file or a log-format string is used. On success, it
4744 * returns 0. If an error occurs -1 is returned. If it fails, this function only
4745 * exits. It is the caller responsibility to do the cleanup.
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004746 */
Christopher Fauletae43b6c2020-05-27 15:24:22 +02004747int http_reply_to_htx(struct stream *s, struct htx *htx, struct http_reply *reply)
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004748{
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004749 struct buffer *errmsg;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004750 struct htx_sl *sl;
4751 struct buffer *body = NULL;
4752 const char *status, *reason, *clen, *ctype;
4753 unsigned int slflags;
4754 int ret = 0;
4755
Christopher Faulete29a97e2020-05-14 14:49:25 +02004756 /*
4757 * - HTTP_REPLY_ERRFILES unexpected here. handled as no payload if so
4758 *
4759 * - HTTP_REPLY_INDIRECT: switch on another reply if defined or handled
4760 * as no payload if NULL. the TXN status code is set with the status
4761 * of the original reply.
4762 */
4763
4764 if (reply->type == HTTP_REPLY_INDIRECT) {
4765 if (reply->body.reply)
4766 reply = reply->body.reply;
4767 }
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004768 if (reply->type == HTTP_REPLY_ERRMSG && !reply->body.errmsg) {
4769 /* get default error message */
4770 if (reply == s->txn->http_reply)
4771 s->txn->http_reply = NULL;
4772 reply = http_error_message(s);
4773 if (reply->type == HTTP_REPLY_INDIRECT) {
4774 if (reply->body.reply)
4775 reply = reply->body.reply;
4776 }
4777 }
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004778
4779 if (reply->type == HTTP_REPLY_ERRMSG) {
4780 /* implicit or explicit error message*/
4781 errmsg = reply->body.errmsg;
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004782 if (errmsg && !b_is_null(errmsg)) {
Christopher Faulet20567362020-05-15 14:52:49 +02004783 if (!htx_copy_msg(htx, errmsg))
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004784 goto fail;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004785 }
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004786 }
4787 else {
4788 /* no payload, file or log-format string */
4789 if (reply->type == HTTP_REPLY_RAW) {
4790 /* file */
4791 body = &reply->body.obj;
4792 }
4793 else if (reply->type == HTTP_REPLY_LOGFMT) {
4794 /* log-format string */
4795 body = alloc_trash_chunk();
4796 if (!body)
4797 goto fail_alloc;
4798 body->data = build_logline(s, body->area, body->size, &reply->body.fmt);
4799 }
4800 /* else no payload */
4801
4802 status = ultoa(reply->status);
4803 reason = http_get_reason(reply->status);
4804 slflags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_CLEN);
4805 if (!body || !b_data(body))
4806 slflags |= HTX_SL_F_BODYLESS;
4807 sl = htx_add_stline(htx, HTX_BLK_RES_SL, slflags, ist("HTTP/1.1"), ist(status), ist(reason));
4808 if (!sl)
4809 goto fail;
4810 sl->info.res.status = reply->status;
4811
4812 clen = (body ? ultoa(b_data(body)) : "0");
4813 ctype = reply->ctype;
4814
4815 if (!LIST_ISEMPTY(&reply->hdrs)) {
4816 struct http_reply_hdr *hdr;
4817 struct buffer *value = alloc_trash_chunk();
4818
4819 if (!value)
4820 goto fail;
4821
4822 list_for_each_entry(hdr, &reply->hdrs, list) {
4823 chunk_reset(value);
4824 value->data = build_logline(s, value->area, value->size, &hdr->value);
4825 if (b_data(value) && !htx_add_header(htx, hdr->name, ist2(b_head(value), b_data(value)))) {
4826 free_trash_chunk(value);
4827 goto fail;
4828 }
4829 chunk_reset(value);
4830 }
4831 free_trash_chunk(value);
4832 }
4833
4834 if (!htx_add_header(htx, ist("content-length"), ist(clen)) ||
4835 (body && b_data(body) && ctype && !htx_add_header(htx, ist("content-type"), ist(ctype))) ||
4836 !htx_add_endof(htx, HTX_BLK_EOH) ||
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004837 (body && b_data(body) && !htx_add_data_atonce(htx, ist2(b_head(body), b_data(body)))))
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004838 goto fail;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004839
4840 htx->flags |= HTX_FL_EOM;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004841 }
4842
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004843 leave:
4844 if (reply->type == HTTP_REPLY_LOGFMT)
4845 free_trash_chunk(body);
4846 return ret;
4847
4848 fail_alloc:
4849 if (!(s->flags & SF_ERR_MASK))
4850 s->flags |= SF_ERR_RESOURCE;
Christopher Faulet97e466c2020-05-15 15:12:47 +02004851 /* fall through */
4852 fail:
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004853 ret = -1;
4854 goto leave;
Christopher Faulet97e466c2020-05-15 15:12:47 +02004855}
4856
4857/* Send an http reply to the client. On success, it returns 0. If an error
Christopher Faulet40e6b552020-06-25 16:04:50 +02004858 * occurs -1 is returned and the response channel is truncated, removing this
4859 * way the faulty reply. This function may fail when the reply is formatted
4860 * (http_reply_to_htx) or when the reply is forwarded
4861 * (http_forward_proxy_resp). On the last case, it is because a
4862 * http-after-response rule fails.
Christopher Faulet97e466c2020-05-15 15:12:47 +02004863 */
4864int http_reply_message(struct stream *s, struct http_reply *reply)
4865{
4866 struct channel *res = &s->res;
4867 struct htx *htx = htx_from_buf(&res->buf);
4868
4869 if (s->txn->status == -1)
4870 s->txn->status = reply->status;
4871 channel_htx_truncate(res, htx);
4872
4873 if (http_reply_to_htx(s, htx, reply) == -1)
4874 goto fail;
4875
4876 htx_to_buf(htx, &s->res.buf);
4877 if (!http_forward_proxy_resp(s, 1))
4878 goto fail;
4879 return 0;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004880
4881 fail:
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004882 channel_htx_truncate(res, htx);
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004883 if (!(s->flags & SF_ERR_MASK))
4884 s->flags |= SF_ERR_PRXCOND;
Christopher Faulet97e466c2020-05-15 15:12:47 +02004885 return -1;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004886}
4887
Christopher Faulet50264b42022-03-30 19:39:30 +02004888/* Return the error message corresponding to s->conn_err_type. It is assumed
Christopher Faulet304cc402019-07-15 15:46:28 +02004889 * that the server side is closed. Note that err_type is actually a
4890 * bitmask, where almost only aborts may be cumulated with other
4891 * values. We consider that aborted operations are more important
4892 * than timeouts or errors due to the fact that nobody else in the
4893 * logs might explain incomplete retries. All others should avoid
4894 * being cumulated. It should normally not be possible to have multiple
4895 * aborts at once, but just in case, the first one in sequence is reported.
4896 * Note that connection errors appearing on the second request of a keep-alive
4897 * connection are not reported since this allows the client to retry.
4898 */
Willy Tarreaub49672d2022-05-27 10:13:37 +02004899void http_return_srv_error(struct stream *s, struct stconn *sc)
Christopher Faulet304cc402019-07-15 15:46:28 +02004900{
Christopher Faulet50264b42022-03-30 19:39:30 +02004901 int err_type = s->conn_err_type;
Christopher Faulet304cc402019-07-15 15:46:28 +02004902
4903 /* set s->txn->status for http_error_message(s) */
Christopher Faulet50264b42022-03-30 19:39:30 +02004904 if (err_type & STRM_ET_QUEUE_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_Q, NULL);
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004907 }
Christopher Faulet50264b42022-03-30 19:39:30 +02004908 else if (err_type & STRM_ET_CONN_ABRT) {
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004909 s->txn->status = -1;
Willy Tarreaub49672d2022-05-27 10:13:37 +02004910 http_server_error(s, sc, SF_ERR_CLICL, SF_FINST_C, NULL);
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004911 }
Christopher Faulet50264b42022-03-30 19:39:30 +02004912 else if (err_type & STRM_ET_QUEUE_TO) {
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004913 s->txn->status = 503;
Willy Tarreaub49672d2022-05-27 10:13:37 +02004914 http_server_error(s, sc, SF_ERR_SRVTO, SF_FINST_Q,
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004915 http_error_message(s));
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004916 }
Christopher Faulet50264b42022-03-30 19:39:30 +02004917 else if (err_type & STRM_ET_QUEUE_ERR) {
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004918 s->txn->status = 503;
Willy Tarreaub49672d2022-05-27 10:13:37 +02004919 http_server_error(s, sc, SF_ERR_SRVCL, SF_FINST_Q,
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004920 http_error_message(s));
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004921 }
Christopher Faulet50264b42022-03-30 19:39:30 +02004922 else if (err_type & STRM_ET_CONN_TO) {
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004923 s->txn->status = 503;
Willy Tarreaub49672d2022-05-27 10:13:37 +02004924 http_server_error(s, sc, SF_ERR_SRVTO, SF_FINST_C,
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004925 (s->txn->flags & TX_NOT_FIRST) ? NULL :
4926 http_error_message(s));
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004927 }
Christopher Faulet50264b42022-03-30 19:39:30 +02004928 else if (err_type & STRM_ET_CONN_ERR) {
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004929 s->txn->status = 503;
Willy Tarreaub49672d2022-05-27 10:13:37 +02004930 http_server_error(s, sc, SF_ERR_SRVCL, SF_FINST_C,
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004931 (s->flags & SF_SRV_REUSED) ? NULL :
4932 http_error_message(s));
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004933 }
Christopher Faulet50264b42022-03-30 19:39:30 +02004934 else if (err_type & STRM_ET_CONN_RES) {
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004935 s->txn->status = 503;
Willy Tarreaub49672d2022-05-27 10:13:37 +02004936 http_server_error(s, sc, SF_ERR_RESOURCE, SF_FINST_C,
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004937 (s->txn->flags & TX_NOT_FIRST) ? NULL :
4938 http_error_message(s));
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004939 }
Christopher Faulet50264b42022-03-30 19:39:30 +02004940 else { /* STRM_ET_CONN_OTHER and others */
Christopher Faulet304cc402019-07-15 15:46:28 +02004941 s->txn->status = 500;
Willy Tarreaub49672d2022-05-27 10:13:37 +02004942 http_server_error(s, sc, SF_ERR_INTERNAL, SF_FINST_C,
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004943 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004944 }
4945}
4946
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004947
Christopher Faulet4a28a532019-03-01 11:19:40 +01004948/* Handle Expect: 100-continue for HTTP/1.1 messages if necessary. It returns 0
4949 * on success and -1 on error.
4950 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004951static int http_handle_expect_hdr(struct stream *s, struct htx *htx, struct http_msg *msg)
Christopher Faulet4a28a532019-03-01 11:19:40 +01004952{
4953 /* If we have HTTP/1.1 message with a body and Expect: 100-continue,
4954 * then we must send an HTTP/1.1 100 Continue intermediate response.
4955 */
4956 if (msg->msg_state == HTTP_MSG_BODY && (msg->flags & HTTP_MSGF_VER_11) &&
4957 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
4958 struct ist hdr = { .ptr = "Expect", .len = 6 };
4959 struct http_hdr_ctx ctx;
4960
4961 ctx.blk = NULL;
4962 /* Expect is allowed in 1.1, look for it */
4963 if (http_find_header(htx, hdr, &ctx, 0) &&
4964 unlikely(isteqi(ctx.value, ist2("100-continue", 12)))) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004965 if (http_reply_100_continue(s) == -1)
Christopher Faulet4a28a532019-03-01 11:19:40 +01004966 return -1;
4967 http_remove_header(htx, &ctx);
4968 }
4969 }
4970 return 0;
4971}
4972
Christopher Faulet23a3c792018-11-28 10:01:23 +01004973/* Send a 100-Continue response to the client. It returns 0 on success and -1
4974 * on error. The response channel is updated accordingly.
4975 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004976static int http_reply_100_continue(struct stream *s)
Christopher Faulet23a3c792018-11-28 10:01:23 +01004977{
4978 struct channel *res = &s->res;
4979 struct htx *htx = htx_from_buf(&res->buf);
4980 struct htx_sl *sl;
4981 unsigned int flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|
4982 HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
Christopher Faulet23a3c792018-11-28 10:01:23 +01004983
4984 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
4985 ist("HTTP/1.1"), ist("100"), ist("Continue"));
4986 if (!sl)
4987 goto fail;
4988 sl->info.res.status = 100;
4989
Christopher Faulet1d5ec092019-06-26 14:23:54 +02004990 if (!htx_add_endof(htx, HTX_BLK_EOH))
Christopher Faulet23a3c792018-11-28 10:01:23 +01004991 goto fail;
4992
Christopher Fauleta72a7e42020-01-28 09:28:11 +01004993 if (!http_forward_proxy_resp(s, 0))
4994 goto fail;
Christopher Faulet23a3c792018-11-28 10:01:23 +01004995 return 0;
4996
4997 fail:
4998 /* If an error occurred, remove the incomplete HTTP response from the
4999 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01005000 channel_htx_truncate(res, htx);
Christopher Faulet23a3c792018-11-28 10:01:23 +01005001 return -1;
5002}
5003
Christopher Faulet12c51e22018-11-28 15:59:42 +01005004
Christopher Faulet0f226952018-10-22 09:29:56 +02005005/*
5006 * Capture headers from message <htx> according to header list <cap_hdr>, and
5007 * fill the <cap> pointers appropriately.
5008 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005009static void http_capture_headers(struct htx *htx, char **cap, struct cap_hdr *cap_hdr)
Christopher Faulet0f226952018-10-22 09:29:56 +02005010{
5011 struct cap_hdr *h;
5012 int32_t pos;
5013
Christopher Fauleta3f15502019-05-13 15:27:23 +02005014 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet0f226952018-10-22 09:29:56 +02005015 struct htx_blk *blk = htx_get_blk(htx, pos);
5016 enum htx_blk_type type = htx_get_blk_type(blk);
5017 struct ist n, v;
5018
5019 if (type == HTX_BLK_EOH)
5020 break;
5021 if (type != HTX_BLK_HDR)
5022 continue;
5023
5024 n = htx_get_blk_name(htx, blk);
5025
5026 for (h = cap_hdr; h; h = h->next) {
5027 if (h->namelen && (h->namelen == n.len) &&
5028 (strncasecmp(n.ptr, h->name, h->namelen) == 0)) {
5029 if (cap[h->index] == NULL)
5030 cap[h->index] =
5031 pool_alloc(h->pool);
5032
5033 if (cap[h->index] == NULL) {
5034 ha_alert("HTTP capture : out of memory.\n");
5035 break;
5036 }
5037
5038 v = htx_get_blk_value(htx, blk);
Tim Duesterhus2471f5c2021-11-08 09:05:01 +01005039 v = isttrim(v, h->len);
Christopher Faulet0f226952018-10-22 09:29:56 +02005040
5041 memcpy(cap[h->index], v.ptr, v.len);
5042 cap[h->index][v.len]=0;
5043 }
5044 }
5045 }
5046}
5047
Christopher Faulet0b6bdc52018-10-24 11:05:36 +02005048/* Delete a value in a header between delimiters <from> and <next>. The header
5049 * itself is delimited by <start> and <end> pointers. The number of characters
5050 * displaced is returned, and the pointer to the first delimiter is updated if
5051 * required. The function tries as much as possible to respect the following
5052 * principles :
5053 * - replace <from> delimiter by the <next> one unless <from> points to <start>,
5054 * in which case <next> is simply removed
5055 * - set exactly one space character after the new first delimiter, unless there
5056 * are not enough characters in the block being moved to do so.
5057 * - remove unneeded spaces before the previous delimiter and after the new
5058 * one.
5059 *
5060 * It is the caller's responsibility to ensure that :
5061 * - <from> points to a valid delimiter or <start> ;
5062 * - <next> points to a valid delimiter or <end> ;
5063 * - there are non-space chars before <from>.
5064 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005065static int http_del_hdr_value(char *start, char *end, char **from, char *next)
Christopher Faulet0b6bdc52018-10-24 11:05:36 +02005066{
5067 char *prev = *from;
5068
5069 if (prev == start) {
5070 /* We're removing the first value. eat the semicolon, if <next>
5071 * is lower than <end> */
5072 if (next < end)
5073 next++;
5074
5075 while (next < end && HTTP_IS_SPHT(*next))
5076 next++;
5077 }
5078 else {
5079 /* Remove useless spaces before the old delimiter. */
5080 while (HTTP_IS_SPHT(*(prev-1)))
5081 prev--;
5082 *from = prev;
5083
5084 /* copy the delimiter and if possible a space if we're
5085 * not at the end of the line.
5086 */
5087 if (next < end) {
5088 *prev++ = *next++;
5089 if (prev + 1 < next)
5090 *prev++ = ' ';
5091 while (next < end && HTTP_IS_SPHT(*next))
5092 next++;
5093 }
5094 }
5095 memmove(prev, next, end - next);
5096 return (prev - next);
5097}
5098
Christopher Faulet0f226952018-10-22 09:29:56 +02005099
5100/* Formats the start line of the request (without CRLF) and puts it in <str> and
Joseph Herlantc42c0e92018-11-25 10:43:27 -08005101 * return the written length. The line can be truncated if it exceeds <len>.
Christopher Faulet0f226952018-10-22 09:29:56 +02005102 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005103static size_t http_fmt_req_line(const struct htx_sl *sl, char *str, size_t len)
Christopher Faulet0f226952018-10-22 09:29:56 +02005104{
5105 struct ist dst = ist2(str, 0);
5106
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005107 if (istcat(&dst, htx_sl_req_meth(sl), len) == -1)
Christopher Faulet0f226952018-10-22 09:29:56 +02005108 goto end;
5109 if (dst.len + 1 > len)
5110 goto end;
5111 dst.ptr[dst.len++] = ' ';
5112
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005113 if (istcat(&dst, htx_sl_req_uri(sl), len) == -1)
Christopher Faulet0f226952018-10-22 09:29:56 +02005114 goto end;
5115 if (dst.len + 1 > len)
5116 goto end;
5117 dst.ptr[dst.len++] = ' ';
5118
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005119 istcat(&dst, htx_sl_req_vsn(sl), len);
Christopher Faulet0f226952018-10-22 09:29:56 +02005120 end:
5121 return dst.len;
5122}
5123
5124/*
5125 * Print a debug line with a start line.
5126 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005127static void http_debug_stline(const char *dir, struct stream *s, const struct htx_sl *sl)
Christopher Faulet0f226952018-10-22 09:29:56 +02005128{
5129 struct session *sess = strm_sess(s);
5130 int max;
5131
5132 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
5133 dir,
Willy Tarreau88bc8002021-12-06 07:01:02 +00005134 objt_conn(sess->origin) ? (unsigned short)__objt_conn(sess->origin)->handle.fd : -1,
Willy Tarreaufd9417b2022-05-18 16:23:22 +02005135 sc_conn(s->scb) ? (unsigned short)(__sc_conn(s->scb))->handle.fd : -1);
Christopher Faulet0f226952018-10-22 09:29:56 +02005136
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005137 max = HTX_SL_P1_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02005138 UBOUND(max, trash.size - trash.data - 3);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005139 chunk_memcat(&trash, HTX_SL_P1_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02005140 trash.area[trash.data++] = ' ';
5141
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005142 max = HTX_SL_P2_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02005143 UBOUND(max, trash.size - trash.data - 2);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005144 chunk_memcat(&trash, HTX_SL_P2_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02005145 trash.area[trash.data++] = ' ';
5146
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005147 max = HTX_SL_P3_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02005148 UBOUND(max, trash.size - trash.data - 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005149 chunk_memcat(&trash, HTX_SL_P3_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02005150 trash.area[trash.data++] = '\n';
5151
Willy Tarreau2e8ab6b2020-03-14 11:03:20 +01005152 DISGUISE(write(1, trash.area, trash.data));
Christopher Faulet0f226952018-10-22 09:29:56 +02005153}
5154
5155/*
5156 * Print a debug line with a header.
5157 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005158static 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 +02005159{
5160 struct session *sess = strm_sess(s);
5161 int max;
5162
5163 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
5164 dir,
Willy Tarreau88bc8002021-12-06 07:01:02 +00005165 objt_conn(sess->origin) ? (unsigned short)__objt_conn(sess->origin)->handle.fd : -1,
Willy Tarreaufd9417b2022-05-18 16:23:22 +02005166 sc_conn(s->scb) ? (unsigned short)(__sc_conn(s->scb))->handle.fd : -1);
Christopher Faulet0f226952018-10-22 09:29:56 +02005167
5168 max = n.len;
5169 UBOUND(max, trash.size - trash.data - 3);
5170 chunk_memcat(&trash, n.ptr, max);
5171 trash.area[trash.data++] = ':';
5172 trash.area[trash.data++] = ' ';
5173
5174 max = v.len;
5175 UBOUND(max, trash.size - trash.data - 1);
5176 chunk_memcat(&trash, v.ptr, max);
5177 trash.area[trash.data++] = '\n';
5178
Willy Tarreau2e8ab6b2020-03-14 11:03:20 +01005179 DISGUISE(write(1, trash.area, trash.data));
Christopher Faulet0f226952018-10-22 09:29:56 +02005180}
5181
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005182void http_txn_reset_req(struct http_txn *txn)
5183{
Christopher Faulet1aea50e2020-01-17 16:03:53 +01005184 txn->req.flags = 0;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005185 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
5186}
5187
5188void http_txn_reset_res(struct http_txn *txn)
5189{
Christopher Faulet1aea50e2020-01-17 16:03:53 +01005190 txn->rsp.flags = 0;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005191 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
5192}
5193
5194/*
Christopher Faulet75f619a2021-03-08 19:12:58 +01005195 * Create and initialize a new HTTP transaction for stream <s>. This should be
5196 * used before processing any new request. It returns the transaction or NLULL
5197 * on error.
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005198 */
Christopher Faulet75f619a2021-03-08 19:12:58 +01005199struct http_txn *http_create_txn(struct stream *s)
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005200{
Christopher Faulet75f619a2021-03-08 19:12:58 +01005201 struct http_txn *txn;
Willy Tarreaub49672d2022-05-27 10:13:37 +02005202 struct stconn *sc = s->scf;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005203
Christopher Faulet75f619a2021-03-08 19:12:58 +01005204 txn = pool_alloc(pool_head_http_txn);
5205 if (!txn)
5206 return NULL;
5207 s->txn = txn;
5208
Christopher Faulet5eb67f52022-06-22 17:12:05 +02005209 txn->meth = HTTP_METH_OTHER;
Willy Tarreaub49672d2022-05-27 10:13:37 +02005210 txn->flags = ((sc && sc_ep_test(sc, SE_FL_NOT_FIRST)) ? TX_NOT_FIRST : 0);
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005211 txn->status = -1;
Christopher Faulet5cb513a2020-05-13 17:56:56 +02005212 txn->http_reply = NULL;
Christopher Faulete05bf9e2022-03-29 15:23:40 +02005213 txn->l7_buffer = BUF_NULL;
Willy Tarreau8b507582020-02-25 09:35:07 +01005214 write_u32(txn->cache_hash, 0);
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005215
5216 txn->cookie_first_date = 0;
5217 txn->cookie_last_date = 0;
5218
5219 txn->srv_cookie = NULL;
5220 txn->cli_cookie = NULL;
5221 txn->uri = NULL;
5222
5223 http_txn_reset_req(txn);
5224 http_txn_reset_res(txn);
5225
5226 txn->req.chn = &s->req;
5227 txn->rsp.chn = &s->res;
5228
5229 txn->auth.method = HTTP_AUTH_UNKNOWN;
5230
Willy Tarreaub7bfcb32021-08-31 08:13:25 +02005231 vars_init_head(&s->vars_txn, SCOPE_TXN);
5232 vars_init_head(&s->vars_reqres, SCOPE_REQ);
Christopher Faulet75f619a2021-03-08 19:12:58 +01005233
5234 return txn;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005235}
5236
5237/* to be used at the end of a transaction */
Christopher Faulet75f619a2021-03-08 19:12:58 +01005238void http_destroy_txn(struct stream *s)
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005239{
5240 struct http_txn *txn = s->txn;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005241
5242 /* these ones will have been dynamically allocated */
5243 pool_free(pool_head_requri, txn->uri);
5244 pool_free(pool_head_capture, txn->cli_cookie);
5245 pool_free(pool_head_capture, txn->srv_cookie);
Tim Duesterhusa17e6622020-03-05 20:19:02 +01005246 pool_free(pool_head_uniqueid, s->unique_id.ptr);
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005247
Tim Duesterhusa17e6622020-03-05 20:19:02 +01005248 s->unique_id = IST_NULL;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005249 txn->uri = NULL;
5250 txn->srv_cookie = NULL;
5251 txn->cli_cookie = NULL;
5252
Christopher Faulet59399252019-11-07 14:27:52 +01005253 if (!LIST_ISEMPTY(&s->vars_txn.head))
5254 vars_prune(&s->vars_txn, s->sess, s);
5255 if (!LIST_ISEMPTY(&s->vars_reqres.head))
5256 vars_prune(&s->vars_reqres, s->sess, s);
Christopher Faulet75f619a2021-03-08 19:12:58 +01005257
Christopher Faulete05bf9e2022-03-29 15:23:40 +02005258 b_free(&txn->l7_buffer);
5259
Christopher Faulet75f619a2021-03-08 19:12:58 +01005260 pool_free(pool_head_http_txn, txn);
5261 s->txn = NULL;
Christopher Faulet59399252019-11-07 14:27:52 +01005262}
5263
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005264
5265DECLARE_POOL(pool_head_http_txn, "http_txn", sizeof(struct http_txn));
Christopher Faulet0f226952018-10-22 09:29:56 +02005266
Christopher Fauletf4eb75d2018-10-11 15:55:07 +02005267/*
5268 * Local variables:
5269 * c-indent-level: 8
5270 * c-basic-offset: 8
5271 * End:
5272 */