blob: 12dd48768bb78f3ff1b5bfc7f2e1377bc88db590 [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));
Christopher Faulet4a66c942023-01-13 09:43:21 +0100335 http_set_term_flags(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200336
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100337 DBG_TRACE_DEVEL("leaving on error",
338 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200339 return 0;
340}
341
342
343/* This stream analyser runs all HTTP request processing which is common to
344 * frontends and backends, which means blocking ACLs, filters, connection-close,
345 * reqadd, stats and redirects. This is performed for the designated proxy.
346 * It returns 1 if the processing can continue on next analysers, or zero if it
347 * either needs more data or wants to immediately abort the request (eg: deny,
348 * error, ...).
349 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200350int http_process_req_common(struct stream *s, struct channel *req, int an_bit, struct proxy *px)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200351{
Christopher Fauletd4150ad2021-10-13 15:35:55 +0200352 struct list *def_rules, *rules;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200353 struct session *sess = s->sess;
354 struct http_txn *txn = s->txn;
355 struct http_msg *msg = &txn->req;
Christopher Fauletff2759f2018-10-24 11:13:16 +0200356 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200357 struct redirect_rule *rule;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200358 enum rule_result verdict;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200359 struct connection *conn = objt_conn(sess->origin);
360
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100361 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200362
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100363 htx = htxbuf(&req->buf);
Christopher Fauletff2759f2018-10-24 11:13:16 +0200364
Christopher Faulet1907ccc2019-04-29 13:12:02 +0200365 /* just in case we have some per-backend tracking. Only called the first
366 * execution of the analyser. */
Christopher Fauletd4150ad2021-10-13 15:35:55 +0200367 if (!s->current_rule && !s->current_rule_list)
Christopher Faulet1907ccc2019-04-29 13:12:02 +0200368 stream_inc_be_http_req_ctr(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200369
Christopher Fauletd4150ad2021-10-13 15:35:55 +0200370 def_rules = ((px->defpx && (an_bit == AN_REQ_HTTP_PROCESS_FE || px != sess->fe)) ? &px->defpx->http_req_rules : NULL);
371 rules = &px->http_req_rules;
372
Christopher Faulete0768eb2018-10-03 16:38:02 +0200373 /* evaluate http-request rules */
Christopher Fauletd4150ad2021-10-13 15:35:55 +0200374 if ((def_rules && !LIST_ISEMPTY(def_rules)) || !LIST_ISEMPTY(rules)) {
375 verdict = http_req_get_intercept_rule(px, def_rules, rules, s);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200376
377 switch (verdict) {
378 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
379 goto return_prx_yield;
380
381 case HTTP_RULE_RES_CONT:
382 case HTTP_RULE_RES_STOP: /* nothing to do */
383 break;
384
385 case HTTP_RULE_RES_DENY: /* deny or tarpit */
386 if (txn->flags & TX_CLTARPIT)
387 goto tarpit;
388 goto deny;
389
390 case HTTP_RULE_RES_ABRT: /* abort request, response already sent. Eg: auth */
391 goto return_prx_cond;
392
393 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
394 goto done;
395
396 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
397 goto return_bad_req;
Christopher Faulet3a26bee2019-12-16 12:47:40 +0100398
399 case HTTP_RULE_RES_ERROR: /* failed with a bad request */
400 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200401 }
402 }
403
Christopher Faulet18c13d32022-05-16 11:43:10 +0200404 if (px->options2 & (PR_O2_RSTRICT_REQ_HDR_NAMES_BLK|PR_O2_RSTRICT_REQ_HDR_NAMES_DEL)) {
405 verdict = http_req_restrict_header_names(s, htx, px);
406 if (verdict == HTTP_RULE_RES_DENY)
407 goto deny;
408 }
409
Christopher Faulete0768eb2018-10-03 16:38:02 +0200410 if (conn && (conn->flags & CO_FL_EARLY_DATA) &&
Olivier Houchard220a26c2020-01-23 14:57:36 +0100411 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_SSL_WAIT_HS))) {
Christopher Fauletff2759f2018-10-24 11:13:16 +0200412 struct http_hdr_ctx ctx;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200413
Christopher Fauletff2759f2018-10-24 11:13:16 +0200414 ctx.blk = NULL;
415 if (!http_find_header(htx, ist("Early-Data"), &ctx, 0)) {
416 if (unlikely(!http_add_header(htx, ist("Early-Data"), ist("1"))))
Christopher Fauletd649b572022-06-01 17:42:35 +0200417 goto return_fail_rewrite;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200418 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200419 }
420
421 /* OK at this stage, we know that the request was accepted according to
422 * the http-request rules, we can check for the stats. Note that the
423 * URI is detected *before* the req* rules in order not to be affected
424 * by a possible reqrep, while they are processed *after* so that a
425 * reqdeny can still block them. This clearly needs to change in 1.6!
426 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200427 if (!s->target && http_stats_check_uri(s, txn, px)) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200428 s->target = &http_stats_applet.obj_type;
Willy Tarreaua0b58b52022-05-27 08:33:53 +0200429 if (unlikely(!sc_applet_create(s->scb, objt_applet(s->target)))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200430 s->logs.tv_request = now;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200431 if (!(s->flags & SF_ERR_MASK))
432 s->flags |= SF_ERR_RESOURCE;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100433 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200434 }
435
436 /* parse the whole stats request and extract the relevant information */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200437 http_handle_stats(s, req);
Christopher Fauletd4150ad2021-10-13 15:35:55 +0200438 verdict = http_req_get_intercept_rule(px, NULL, &px->uri_auth->http_req_rules, s);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200439 /* not all actions implemented: deny, allow, auth */
440
441 if (verdict == HTTP_RULE_RES_DENY) /* stats http-request deny */
442 goto deny;
443
444 if (verdict == HTTP_RULE_RES_ABRT) /* stats auth / stats http-request auth */
445 goto return_prx_cond;
Christopher Faulet3a26bee2019-12-16 12:47:40 +0100446
447 if (verdict == HTTP_RULE_RES_BADREQ) /* failed with a bad request */
448 goto return_bad_req;
449
450 if (verdict == HTTP_RULE_RES_ERROR) /* failed with a bad request */
451 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200452 }
453
Christopher Faulet2571bc62019-03-01 11:44:26 +0100454 /* Proceed with the applets now. */
455 if (unlikely(objt_applet(s->target))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200456 if (sess->fe == s->be) /* report it if the request was intercepted by the frontend */
Willy Tarreau4781b152021-04-06 13:53:36 +0200457 _HA_ATOMIC_INC(&sess->fe->fe_counters.intercepted_req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200458
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200459 if (http_handle_expect_hdr(s, htx, msg) == -1)
Christopher Fauletb8a53712019-12-16 11:29:38 +0100460 goto return_int_err;
Christopher Fauletbcf242a2019-03-01 11:36:26 +0100461
Christopher Faulete0768eb2018-10-03 16:38:02 +0200462 if (!(s->flags & SF_ERR_MASK)) // this is not really an error but it is
463 s->flags |= SF_ERR_LOCAL; // to mark that it comes from the proxy
Christopher Faulet4a66c942023-01-13 09:43:21 +0100464 http_set_term_flags(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200465
Christopher Fauletc2ac5e42021-03-08 18:20:09 +0100466 if (HAS_FILTERS(s))
467 req->analysers |= AN_REQ_FLT_HTTP_HDRS;
468
Christopher Faulete0768eb2018-10-03 16:38:02 +0200469 /* enable the minimally required analyzers to handle keep-alive and compression on the HTTP response */
470 req->analysers &= (AN_REQ_HTTP_BODY | AN_REQ_FLT_HTTP_HDRS | AN_REQ_FLT_END);
471 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
472 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Christopher Fauletbcf242a2019-03-01 11:36:26 +0100473
474 req->flags |= CF_SEND_DONTWAIT;
475 s->flags |= SF_ASSIGNED;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200476 goto done;
477 }
478
479 /* check whether we have some ACLs set to redirect this request */
480 list_for_each_entry(rule, &px->redirect_rules, list) {
481 if (rule->cond) {
482 int ret;
483
484 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
485 ret = acl_pass(ret);
486 if (rule->cond->pol == ACL_COND_UNLESS)
487 ret = !ret;
488 if (!ret)
489 continue;
490 }
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200491 if (!http_apply_redirect_rule(rule, s, txn))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100492 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200493 goto done;
494 }
495
496 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
497 * If this happens, then the data will not come immediately, so we must
498 * send all what we have without waiting. Note that due to the small gain
499 * in waiting for the body of the request, it's easier to simply put the
500 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
501 * itself once used.
502 */
503 req->flags |= CF_SEND_DONTWAIT;
504
505 done: /* done with this analyser, continue with next ones that the calling
506 * points will have set, if any.
507 */
508 req->analyse_exp = TICK_ETERNITY;
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +0500509 done_without_exp: /* done with this analyser, but don't reset the analyse_exp. */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200510 req->analysers &= ~an_bit;
Christopher Fauletd4150ad2021-10-13 15:35:55 +0200511 s->current_rule = s->current_rule_list = NULL;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100512 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200513 return 1;
514
515 tarpit:
516 /* Allow cookie logging
517 */
518 if (s->be->cookie_name || sess->fe->capture_name)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200519 http_manage_client_side_cookies(s, req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200520
521 /* When a connection is tarpitted, we use the tarpit timeout,
522 * which may be the same as the connect timeout if unspecified.
523 * If unset, then set it to zero because we really want it to
524 * eventually expire. We build the tarpit as an analyser.
525 */
Christopher Faulet202c6ce2019-01-07 14:57:35 +0100526 channel_htx_erase(&s->req, htx);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200527
528 /* wipe the request out so that we can drop the connection early
529 * if the client closes first.
530 */
531 channel_dont_connect(req);
532
Christopher Faulete0768eb2018-10-03 16:38:02 +0200533 req->analysers &= AN_REQ_FLT_END; /* remove switching rules etc... */
534 req->analysers |= AN_REQ_HTTP_TARPIT;
535 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
536 if (!req->analyse_exp)
537 req->analyse_exp = tick_add(now_ms, 0);
538 stream_inc_http_err_ctr(s);
Willy Tarreau4781b152021-04-06 13:53:36 +0200539 _HA_ATOMIC_INC(&sess->fe->fe_counters.denied_req);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100540 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau4781b152021-04-06 13:53:36 +0200541 _HA_ATOMIC_INC(&s->be->be_counters.denied_req);
William Lallemand36119de2021-03-08 15:26:48 +0100542 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200543 _HA_ATOMIC_INC(&sess->listener->counters->denied_req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200544 goto done_without_exp;
545
546 deny: /* this request was blocked (denied) */
547
548 /* Allow cookie logging
549 */
550 if (s->be->cookie_name || sess->fe->capture_name)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200551 http_manage_client_side_cookies(s, req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200552
Christopher Faulete0768eb2018-10-03 16:38:02 +0200553 s->logs.tv_request = now;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200554 stream_inc_http_err_ctr(s);
Willy Tarreau4781b152021-04-06 13:53:36 +0200555 _HA_ATOMIC_INC(&sess->fe->fe_counters.denied_req);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100556 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau4781b152021-04-06 13:53:36 +0200557 _HA_ATOMIC_INC(&s->be->be_counters.denied_req);
William Lallemand36119de2021-03-08 15:26:48 +0100558 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200559 _HA_ATOMIC_INC(&sess->listener->counters->denied_req);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100560 goto return_prx_err;
561
Christopher Fauletd649b572022-06-01 17:42:35 +0200562 return_fail_rewrite:
563 if (!(s->flags & SF_ERR_MASK))
564 s->flags |= SF_ERR_PRXCOND;
565 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_rewrites);
566 if (s->flags & SF_BE_ASSIGNED)
567 _HA_ATOMIC_INC(&s->be->be_counters.failed_rewrites);
568 if (sess->listener && sess->listener->counters)
569 _HA_ATOMIC_INC(&sess->listener->counters->failed_rewrites);
570 if (objt_server(s->target))
571 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_rewrites);
572 /* fall through */
573
Christopher Fauletb8a53712019-12-16 11:29:38 +0100574 return_int_err:
575 txn->status = 500;
576 if (!(s->flags & SF_ERR_MASK))
577 s->flags |= SF_ERR_INTERNAL;
Willy Tarreau4781b152021-04-06 13:53:36 +0200578 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100579 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau4781b152021-04-06 13:53:36 +0200580 _HA_ATOMIC_INC(&s->be->be_counters.internal_errors);
William Lallemand36119de2021-03-08 15:26:48 +0100581 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200582 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100583 goto return_prx_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200584
585 return_bad_req:
Christopher Faulete0768eb2018-10-03 16:38:02 +0200586 txn->status = 400;
Willy Tarreau4781b152021-04-06 13:53:36 +0200587 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req);
William Lallemand36119de2021-03-08 15:26:48 +0100588 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200589 _HA_ATOMIC_INC(&sess->listener->counters->failed_req);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100590 /* fall through */
591
592 return_prx_err:
593 http_reply_and_close(s, txn->status, http_error_message(s));
594 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200595
596 return_prx_cond:
Christopher Faulet4a66c942023-01-13 09:43:21 +0100597 http_set_term_flags(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200598
599 req->analysers &= AN_REQ_FLT_END;
600 req->analyse_exp = TICK_ETERNITY;
Christopher Fauletd4150ad2021-10-13 15:35:55 +0200601 s->current_rule = s->current_rule_list = NULL;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100602 DBG_TRACE_DEVEL("leaving on error",
603 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200604 return 0;
605
606 return_prx_yield:
607 channel_dont_connect(req);
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100608 DBG_TRACE_DEVEL("waiting for more data",
609 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200610 return 0;
611}
612
613/* This function performs all the processing enabled for the current request.
614 * It returns 1 if the processing can continue on next analysers, or zero if it
615 * needs more data, encounters an error, or wants to immediately abort the
616 * request. It relies on buffers flags, and updates s->req.analysers.
617 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200618int http_process_request(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200619{
620 struct session *sess = s->sess;
621 struct http_txn *txn = s->txn;
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200622 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200623 struct connection *cli_conn = objt_conn(strm_sess(s)->origin);
624
Christopher Faulet8bebd2f2020-10-06 17:54:56 +0200625 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200626
627 /*
628 * Right now, we know that we have processed the entire headers
629 * and that unwanted requests have been filtered out. We can do
630 * whatever we want with the remaining request. Also, now we
631 * may have separate values for ->fe, ->be.
632 */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100633 htx = htxbuf(&req->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200634
635 /*
Christopher Faulete0768eb2018-10-03 16:38:02 +0200636 * 7: Now we can work with the cookies.
637 * Note that doing so might move headers in the request, but
638 * the fields will stay coherent and the URI will not move.
639 * This should only be performed in the backend.
640 */
641 if (s->be->cookie_name || sess->fe->capture_name)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200642 http_manage_client_side_cookies(s, req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200643
Tim Duesterhus2825b4b2020-02-28 15:13:34 +0100644 /* 8: Generate unique ID if a "unique-id-format" is defined.
645 *
646 * A unique ID is generated even when it is not sent to ensure that the ID can make use of
647 * fetches only available in the HTTP request processing stage.
648 */
649 if (!LIST_ISEMPTY(&sess->fe->format_unique_id)) {
Tim Duesterhusa17e6622020-03-05 20:19:02 +0100650 struct ist unique_id = stream_generate_unique_id(s, &sess->fe->format_unique_id);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200651
Tim Duesterhusa17e6622020-03-05 20:19:02 +0100652 if (!isttest(unique_id)) {
Christopher Fauletb8a53712019-12-16 11:29:38 +0100653 if (!(s->flags & SF_ERR_MASK))
654 s->flags |= SF_ERR_RESOURCE;
655 goto return_int_err;
656 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200657
Tim Duesterhus2825b4b2020-02-28 15:13:34 +0100658 /* send unique ID if a "unique-id-header" is defined */
Tim Duesterhus0643b0e2020-03-05 17:56:35 +0100659 if (isttest(sess->fe->header_unique_id) &&
Tim Duesterhus7ad27d42022-05-18 00:22:15 +0200660 unlikely(!http_add_header(htx, sess->fe->header_unique_id, unique_id)))
Christopher Fauletd649b572022-06-01 17:42:35 +0200661 goto return_fail_rewrite;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200662 }
663
664 /*
665 * 9: add X-Forwarded-For if either the frontend or the backend
666 * asks for it.
667 */
668 if ((sess->fe->options | s->be->options) & PR_O_FWDFOR) {
Willy Tarreaud68ff012022-05-27 08:57:21 +0200669 const struct sockaddr_storage *src = sc_src(s->scf);
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200670 struct http_hdr_ctx ctx = { .blk = NULL };
Tim Duesterhusb50ab842022-03-05 00:52:41 +0100671 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 +0200672
Christopher Faulete0768eb2018-10-03 16:38:02 +0200673 if (!((sess->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200674 http_find_header(htx, hdr, &ctx, 0)) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200675 /* The header is set to be added only if none is present
676 * and we found it, so don't do anything.
677 */
678 }
Christopher Faulet8a104ba2021-10-25 07:41:30 +0200679 else if (src && src->ss_family == AF_INET) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200680 /* Add an X-Forwarded-For header unless the source IP is
681 * in the 'except' network range.
682 */
Christopher Faulet8a104ba2021-10-25 07:41:30 +0200683 if (ipcmp2net(src, &sess->fe->except_xff_net) &&
684 ipcmp2net(src, &s->be->except_xff_net)) {
685 unsigned char *pn = (unsigned char *)&((struct sockaddr_in *)src)->sin_addr;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200686
687 /* Note: we rely on the backend to get the header name to be used for
688 * x-forwarded-for, because the header is really meant for the backends.
689 * However, if the backend did not specify any option, we have to rely
690 * on the frontend's header name.
691 */
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200692 chunk_printf(&trash, "%d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
693 if (unlikely(!http_add_header(htx, hdr, ist2(trash.area, trash.data))))
Christopher Fauletd649b572022-06-01 17:42:35 +0200694 goto return_fail_rewrite;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200695 }
696 }
Christopher Faulet8a104ba2021-10-25 07:41:30 +0200697 else if (src && src->ss_family == AF_INET6) {
Christopher Faulet5d1def62021-02-26 09:19:15 +0100698 /* Add an X-Forwarded-For header unless the source IP is
699 * in the 'except' network range.
Christopher Faulete0768eb2018-10-03 16:38:02 +0200700 */
Christopher Faulet8a104ba2021-10-25 07:41:30 +0200701 if (ipcmp2net(src, &sess->fe->except_xff_net) &&
702 ipcmp2net(src, &s->be->except_xff_net)) {
Christopher Faulet5d1def62021-02-26 09:19:15 +0100703 char pn[INET6_ADDRSTRLEN];
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200704
Christopher Faulet5d1def62021-02-26 09:19:15 +0100705 inet_ntop(AF_INET6,
Christopher Faulet8a104ba2021-10-25 07:41:30 +0200706 (const void *)&((struct sockaddr_in6 *)(src))->sin6_addr,
Christopher Faulet5d1def62021-02-26 09:19:15 +0100707 pn, sizeof(pn));
Christopher Faulete0768eb2018-10-03 16:38:02 +0200708
Christopher Faulet5d1def62021-02-26 09:19:15 +0100709 /* Note: we rely on the backend to get the header name to be used for
710 * x-forwarded-for, because the header is really meant for the backends.
711 * However, if the backend did not specify any option, we have to rely
712 * on the frontend's header name.
713 */
714 chunk_printf(&trash, "%s", pn);
715 if (unlikely(!http_add_header(htx, hdr, ist2(trash.area, trash.data))))
Christopher Fauletd649b572022-06-01 17:42:35 +0200716 goto return_fail_rewrite;
Christopher Faulet5d1def62021-02-26 09:19:15 +0100717 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200718 }
719 }
720
721 /*
722 * 10: add X-Original-To if either the frontend or the backend
723 * asks for it.
724 */
725 if ((sess->fe->options | s->be->options) & PR_O_ORGTO) {
Willy Tarreaud68ff012022-05-27 08:57:21 +0200726 const struct sockaddr_storage *dst = sc_dst(s->scf);
Tim Duesterhuse502c3e2022-03-05 00:52:42 +0100727 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 +0200728
Christopher Faulet8a104ba2021-10-25 07:41:30 +0200729 if (dst && dst->ss_family == AF_INET) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200730 /* Add an X-Original-To header unless the destination IP is
731 * in the 'except' network range.
732 */
Christopher Faulet8a104ba2021-10-25 07:41:30 +0200733 if (ipcmp2net(dst, &sess->fe->except_xot_net) &&
734 ipcmp2net(dst, &s->be->except_xot_net)) {
735 unsigned char *pn = (unsigned char *)&((struct sockaddr_in *)dst)->sin_addr;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200736
737 /* Note: we rely on the backend to get the header name to be used for
738 * x-original-to, because the header is really meant for the backends.
739 * However, if the backend did not specify any option, we have to rely
740 * on the frontend's header name.
741 */
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200742 chunk_printf(&trash, "%d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
743 if (unlikely(!http_add_header(htx, hdr, ist2(trash.area, trash.data))))
Christopher Fauletd649b572022-06-01 17:42:35 +0200744 goto return_fail_rewrite;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200745 }
746 }
Christopher Faulet8a104ba2021-10-25 07:41:30 +0200747 else if (dst && dst->ss_family == AF_INET6) {
Christopher Faulet5d1def62021-02-26 09:19:15 +0100748 /* Add an X-Original-To header unless the source IP is
749 * in the 'except' network range.
750 */
Christopher Faulet8a104ba2021-10-25 07:41:30 +0200751 if (ipcmp2net(dst, &sess->fe->except_xot_net) &&
752 ipcmp2net(dst, &s->be->except_xot_net)) {
Christopher Faulet5d1def62021-02-26 09:19:15 +0100753 char pn[INET6_ADDRSTRLEN];
754
755 inet_ntop(AF_INET6,
Christopher Faulet8a104ba2021-10-25 07:41:30 +0200756 (const void *)&((struct sockaddr_in6 *)dst)->sin6_addr,
Christopher Faulet5d1def62021-02-26 09:19:15 +0100757 pn, sizeof(pn));
758
759 /* Note: we rely on the backend to get the header name to be used for
760 * x-forwarded-for, because the header is really meant for the backends.
761 * However, if the backend did not specify any option, we have to rely
762 * on the frontend's header name.
763 */
764 chunk_printf(&trash, "%s", pn);
765 if (unlikely(!http_add_header(htx, hdr, ist2(trash.area, trash.data))))
Christopher Fauletd649b572022-06-01 17:42:35 +0200766 goto return_fail_rewrite;
Christopher Faulet5d1def62021-02-26 09:19:15 +0100767 }
768 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200769 }
770
Christopher Fauletc2ac5e42021-03-08 18:20:09 +0100771 /* Filter the request headers if there are filters attached to the
772 * stream.
773 */
774 if (HAS_FILTERS(s))
775 req->analysers |= AN_REQ_FLT_HTTP_HDRS;
776
Christopher Faulete0768eb2018-10-03 16:38:02 +0200777 /* If we have no server assigned yet and we're balancing on url_param
778 * with a POST request, we may be interested in checking the body for
779 * that parameter. This will be done in another analyser.
780 */
781 if (!(s->flags & (SF_ASSIGNED|SF_DIRECT)) &&
Willy Tarreau089eaa02019-01-14 15:17:46 +0100782 s->txn->meth == HTTP_METH_POST &&
783 (s->be->lbprm.algo & BE_LB_ALGO) == BE_LB_ALGO_PH) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200784 channel_dont_connect(req);
785 req->analysers |= AN_REQ_HTTP_BODY;
786 }
787
788 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
789 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau1a18b542018-12-11 16:37:42 +0100790
Christopher Faulete0768eb2018-10-03 16:38:02 +0200791 /* We expect some data from the client. Unless we know for sure
792 * we already have a full request, we have to re-enable quick-ack
793 * in case we previously disabled it, otherwise we might cause
794 * the client to delay further data.
795 */
William Lallemand36119de2021-03-08 15:26:48 +0100796 if ((sess->listener && (sess->listener->options & LI_O_NOQUICKACK)) && !(htx->flags & HTX_FL_EOM))
Willy Tarreau1a18b542018-12-11 16:37:42 +0100797 conn_set_quickack(cli_conn, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200798
799 /*************************************************************
800 * OK, that's finished for the headers. We have done what we *
801 * could. Let's switch to the DATA state. *
802 ************************************************************/
803 req->analyse_exp = TICK_ETERNITY;
804 req->analysers &= ~an_bit;
805
806 s->logs.tv_request = now;
807 /* OK let's go on with the BODY now */
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100808 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200809 return 1;
810
Christopher Fauletd649b572022-06-01 17:42:35 +0200811 return_fail_rewrite:
812 if (!(s->flags & SF_ERR_MASK))
813 s->flags |= SF_ERR_PRXCOND;
814 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_rewrites);
815 if (s->flags & SF_BE_ASSIGNED)
816 _HA_ATOMIC_INC(&s->be->be_counters.failed_rewrites);
817 if (sess->listener && sess->listener->counters)
818 _HA_ATOMIC_INC(&sess->listener->counters->failed_rewrites);
819 if (objt_server(s->target))
820 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_rewrites);
821 /* fall through */
822
Christopher Fauletb8a53712019-12-16 11:29:38 +0100823 return_int_err:
824 txn->status = 500;
825 if (!(s->flags & SF_ERR_MASK))
826 s->flags |= SF_ERR_INTERNAL;
Willy Tarreau4781b152021-04-06 13:53:36 +0200827 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100828 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau4781b152021-04-06 13:53:36 +0200829 _HA_ATOMIC_INC(&s->be->be_counters.internal_errors);
William Lallemand36119de2021-03-08 15:26:48 +0100830 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200831 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100832
Christopher Fauletb8a53712019-12-16 11:29:38 +0100833 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet4a66c942023-01-13 09:43:21 +0100834 http_set_term_flags(s);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100835
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100836 DBG_TRACE_DEVEL("leaving on error",
837 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200838 return 0;
839}
840
841/* This function is an analyser which processes the HTTP tarpit. It always
842 * returns zero, at the beginning because it prevents any other processing
843 * from occurring, and at the end because it terminates the request.
844 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200845int http_process_tarpit(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200846{
847 struct http_txn *txn = s->txn;
848
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100849 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, &txn->req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200850 /* This connection is being tarpitted. The CLIENT side has
851 * already set the connect expiration date to the right
852 * timeout. We just have to check that the client is still
853 * there and that the timeout has not expired.
854 */
855 channel_dont_connect(req);
Christopher Faulet4b490b72023-01-04 11:55:15 +0100856 if (!(req->flags & CF_SHUTR) &&
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100857 !tick_is_expired(req->analyse_exp, now_ms)) {
Christopher Fauletb0c87f12021-10-29 14:37:07 +0200858 /* Be sure to drain all data from the request channel */
859 channel_htx_erase(req, htxbuf(&req->buf));
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100860 DBG_TRACE_DEVEL("waiting for tarpit timeout expiry",
861 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200862 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100863 }
864
Christopher Faulete0768eb2018-10-03 16:38:02 +0200865
866 /* We will set the queue timer to the time spent, just for
867 * logging purposes. We fake a 500 server error, so that the
868 * attacker will not suspect his connection has been tarpitted.
869 * It will not cause trouble to the logs because we can exclude
870 * the tarpitted connections by filtering on the 'PT' status flags.
871 */
872 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
873
Christopher Faulet8dfeccf2020-05-15 14:16:29 +0200874 http_reply_and_close(s, txn->status, (!(req->flags & CF_READ_ERROR) ? http_error_message(s) : NULL));
Christopher Faulet4a66c942023-01-13 09:43:21 +0100875 http_set_term_flags(s);
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100876
877 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200878 return 0;
879}
880
881/* This function is an analyser which waits for the HTTP request body. It waits
882 * for either the buffer to be full, or the full advertised contents to have
883 * reached the buffer. It must only be called after the standard HTTP request
884 * processing has occurred, because it expects the request to be parsed and will
885 * look for the Expect header. It may send a 100-Continue interim response. It
886 * takes in input any state starting from HTTP_MSG_BODY and leaves with one of
887 * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it
888 * needs to read more data, or 1 once it has completed its analysis.
889 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200890int http_wait_for_request_body(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200891{
892 struct session *sess = s->sess;
893 struct http_txn *txn = s->txn;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200894
Christopher Fauletf4569bb2023-01-13 10:49:31 +0100895 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, &s->txn->req);
Christopher Fauletf76ebe82018-10-24 11:16:22 +0200896
Christopher Fauletf76ebe82018-10-24 11:16:22 +0200897
Christopher Faulet021a8e42021-03-29 10:46:38 +0200898 switch (http_wait_for_msg_body(s, req, s->be->timeout.httpreq, 0)) {
899 case HTTP_RULE_RES_CONT:
900 goto http_end;
901 case HTTP_RULE_RES_YIELD:
902 goto missing_data_or_waiting;
903 case HTTP_RULE_RES_BADREQ:
Willy Tarreau4236f032019-03-05 10:43:32 +0100904 goto return_bad_req;
Christopher Faulet021a8e42021-03-29 10:46:38 +0200905 case HTTP_RULE_RES_ERROR:
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200906 goto return_int_err;
Christopher Faulet021a8e42021-03-29 10:46:38 +0200907 case HTTP_RULE_RES_ABRT:
Christopher Fauletb8a53712019-12-16 11:29:38 +0100908 goto return_prx_cond;
Christopher Faulet021a8e42021-03-29 10:46:38 +0200909 default:
910 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200911 }
912
913 http_end:
914 /* The situation will not evolve, so let's give up on the analysis. */
915 s->logs.tv_request = now; /* update the request timer to reflect full request */
916 req->analysers &= ~an_bit;
917 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100918 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200919 return 1;
920
Christopher Faulet021a8e42021-03-29 10:46:38 +0200921 missing_data_or_waiting:
922 channel_dont_connect(req);
923 DBG_TRACE_DEVEL("waiting for more data",
924 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
925 return 0;
926
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200927 return_int_err:
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200928 txn->status = 500;
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200929 if (!(s->flags & SF_ERR_MASK))
930 s->flags |= SF_ERR_INTERNAL;
Willy Tarreau4781b152021-04-06 13:53:36 +0200931 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100932 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau4781b152021-04-06 13:53:36 +0200933 _HA_ATOMIC_INC(&s->be->be_counters.internal_errors);
William Lallemand36119de2021-03-08 15:26:48 +0100934 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200935 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Faulet021a8e42021-03-29 10:46:38 +0200936 goto return_prx_err;
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200937
Christopher Faulete0768eb2018-10-03 16:38:02 +0200938 return_bad_req: /* let's centralize all bad requests */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200939 txn->status = 400;
Willy Tarreau4781b152021-04-06 13:53:36 +0200940 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req);
William Lallemand36119de2021-03-08 15:26:48 +0100941 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200942 _HA_ATOMIC_INC(&sess->listener->counters->failed_req);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100943 /* fall through */
944
Christopher Faulet021a8e42021-03-29 10:46:38 +0200945 return_prx_err:
Christopher Fauletb8a53712019-12-16 11:29:38 +0100946 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet021a8e42021-03-29 10:46:38 +0200947 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200948
Christopher Faulet021a8e42021-03-29 10:46:38 +0200949 return_prx_cond:
Christopher Fauletf0d80df2023-01-13 10:20:20 +0100950 http_set_term_flags(s);
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200951
Christopher Faulete0768eb2018-10-03 16:38:02 +0200952 req->analysers &= AN_REQ_FLT_END;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100953 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100954 DBG_TRACE_DEVEL("leaving on error",
955 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200956 return 0;
957}
958
959/* This function is an analyser which forwards request body (including chunk
960 * sizes if any). It is called as soon as we must forward, even if we forward
961 * zero byte. The only situation where it must not be called is when we're in
962 * tunnel mode and we want to forward till the close. It's used both to forward
963 * remaining data and to resync after end of body. It expects the msg_state to
964 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
965 * read more data, or 1 once we can go on with next request or end the stream.
966 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
967 * bytes of pending data + the headers if not already done.
968 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200969int http_request_forward_body(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200970{
971 struct session *sess = s->sess;
972 struct http_txn *txn = s->txn;
Christopher Faulet9768c262018-10-22 09:34:31 +0200973 struct http_msg *msg = &txn->req;
974 struct htx *htx;
Christopher Faulet93e02d82019-03-08 14:18:50 +0100975 short status = 0;
Christopher Fauletaed82cf2018-11-30 22:22:32 +0100976 int ret;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200977
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100978 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200979
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100980 htx = htxbuf(&req->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200981
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200982 if (htx->flags & HTX_FL_PARSING_ERROR)
983 goto return_bad_req;
984 if (htx->flags & HTX_FL_PROCESSING_ERROR)
985 goto return_int_err;
986
Christopher Faulete0768eb2018-10-03 16:38:02 +0200987 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
988 ((req->flags & CF_SHUTW) && (req->to_forward || co_data(req)))) {
989 /* Output closed while we were sending data. We must abort and
990 * wake the other side up.
Christopher Fauletf506d962021-04-27 10:56:28 +0200991 *
992 * If we have finished to send the request and the response is
993 * still in progress, don't catch write error on the request
994 * side if it is in fact a read error on the server side.
Christopher Faulete0768eb2018-10-03 16:38:02 +0200995 */
Christopher Fauletf506d962021-04-27 10:56:28 +0200996 if (msg->msg_state == HTTP_MSG_DONE && (s->res.flags & CF_READ_ERROR) && s->res.analysers)
997 return 0;
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200998
Olivier Houchard29cac3c2019-07-12 15:48:58 +0200999 /* Don't abort yet if we had L7 retries activated and it
1000 * was a write error, we may recover.
1001 */
1002 if (!(req->flags & (CF_READ_ERROR | CF_READ_TIMEOUT)) &&
Christopher Faulete05bf9e2022-03-29 15:23:40 +02001003 (txn->flags & TX_L7_RETRY)) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001004 DBG_TRACE_DEVEL("leaving on L7 retry",
1005 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Olivier Houchard29cac3c2019-07-12 15:48:58 +02001006 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001007 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001008 msg->msg_state = HTTP_MSG_ERROR;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001009 http_end_request(s);
1010 http_end_response(s);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001011 DBG_TRACE_DEVEL("leaving on error",
1012 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001013 return 1;
1014 }
1015
1016 /* Note that we don't have to send 100-continue back because we don't
1017 * need the data to complete our job, and it's up to the server to
1018 * decide whether to return 100, 417 or anything else in return of
1019 * an "Expect: 100-continue" header.
1020 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001021 if (msg->msg_state == HTTP_MSG_BODY)
1022 msg->msg_state = HTTP_MSG_DATA;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001023
Christopher Faulete0768eb2018-10-03 16:38:02 +02001024 /* in most states, we should abort in case of early close */
1025 channel_auto_close(req);
1026
1027 if (req->to_forward) {
Christopher Faulet66af0b22019-03-22 14:54:52 +01001028 if (req->to_forward == CHN_INFINITE_FORWARD) {
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001029 if (req->flags & CF_EOI)
1030 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet66af0b22019-03-22 14:54:52 +01001031 }
1032 else {
1033 /* We can't process the buffer's contents yet */
1034 req->flags |= CF_WAKE_WRITE;
1035 goto missing_data_or_waiting;
1036 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001037 }
1038
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001039 if (msg->msg_state >= HTTP_MSG_ENDING)
1040 goto ending;
1041
1042 if (txn->meth == HTTP_METH_CONNECT) {
1043 msg->msg_state = HTTP_MSG_ENDING;
1044 goto ending;
1045 }
1046
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001047 /* Forward input data. We get it by removing all outgoing data not
1048 * forwarded yet from HTX data size. If there are some data filters, we
1049 * let them decide the amount of data to forward.
Christopher Faulet9768c262018-10-22 09:34:31 +02001050 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001051 if (HAS_REQ_DATA_FILTERS(s)) {
1052 ret = flt_http_payload(s, msg, htx->data);
1053 if (ret < 0)
1054 goto return_bad_req;
Christopher Faulet421e7692019-06-13 11:16:45 +02001055 c_adv(req, ret);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001056 }
1057 else {
Christopher Faulet421e7692019-06-13 11:16:45 +02001058 c_adv(req, htx->data - co_data(req));
Christopher Faulet66af0b22019-03-22 14:54:52 +01001059 if (msg->flags & HTTP_MSGF_XFER_LEN)
1060 channel_htx_forward_forever(req, htx);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001061 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001062
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001063 if (htx->data != co_data(req))
1064 goto missing_data_or_waiting;
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001065
Christopher Faulet9768c262018-10-22 09:34:31 +02001066 /* Check if the end-of-message is reached and if so, switch the message
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001067 * in HTTP_MSG_ENDING state. Then if all data was marked to be
1068 * forwarded, set the state to HTTP_MSG_DONE.
Christopher Faulet9768c262018-10-22 09:34:31 +02001069 */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001070 if (!(htx->flags & HTX_FL_EOM))
Christopher Faulet9768c262018-10-22 09:34:31 +02001071 goto missing_data_or_waiting;
1072
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001073 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet9768c262018-10-22 09:34:31 +02001074
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001075 ending:
Christopher Faulet2151cdd2020-07-22 16:34:59 +02001076 req->flags &= ~CF_EXPECT_MORE; /* no more data are expected */
1077
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001078 /* other states, ENDING...TUNNEL */
1079 if (msg->msg_state >= HTTP_MSG_DONE)
1080 goto done;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001081
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001082 if (HAS_REQ_DATA_FILTERS(s)) {
1083 ret = flt_http_end(s, msg);
1084 if (ret <= 0) {
1085 if (!ret)
1086 goto missing_data_or_waiting;
1087 goto return_bad_req;
1088 }
1089 }
1090
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001091 if (txn->meth == HTTP_METH_CONNECT)
1092 msg->msg_state = HTTP_MSG_TUNNEL;
1093 else {
1094 msg->msg_state = HTTP_MSG_DONE;
1095 req->to_forward = 0;
1096 }
1097
1098 done:
1099 /* we don't want to forward closes on DONE except in tunnel mode. */
1100 if (!(txn->flags & TX_CON_WANT_TUN))
1101 channel_dont_close(req);
1102
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001103 http_end_request(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001104 if (!(req->analysers & an_bit)) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001105 http_end_response(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001106 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
1107 if (req->flags & CF_SHUTW) {
1108 /* request errors are most likely due to the
1109 * server aborting the transfer. */
Christopher Faulet93e02d82019-03-08 14:18:50 +01001110 goto return_srv_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001111 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001112 goto return_bad_req;
1113 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001114 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001115 return 1;
1116 }
1117
1118 /* If "option abortonclose" is set on the backend, we want to monitor
1119 * the client's connection and forward any shutdown notification to the
1120 * server, which will decide whether to close or to go on processing the
1121 * request. We only do that in tunnel mode, and not in other modes since
1122 * it can be abused to exhaust source ports. */
Christopher Faulet769d0e92019-03-22 14:23:18 +01001123 if (s->be->options & PR_O_ABRT_CLOSE) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001124 channel_auto_read(req);
Christopher Faulet446d8032022-12-12 07:53:18 +01001125 if ((req->flags & CF_SHUTR) && !(txn->flags & TX_CON_WANT_TUN))
Willy Tarreaucb041662022-05-17 19:44:42 +02001126 s->scb->flags |= SC_FL_NOLINGER;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001127 channel_auto_close(req);
1128 }
1129 else if (s->txn->meth == HTTP_METH_POST) {
1130 /* POST requests may require to read extra CRLF sent by broken
1131 * browsers and which could cause an RST to be sent upon close
1132 * on some systems (eg: Linux). */
1133 channel_auto_read(req);
1134 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001135 DBG_TRACE_DEVEL("waiting for the end of the HTTP txn",
1136 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001137 return 0;
1138
1139 missing_data_or_waiting:
1140 /* stop waiting for data if the input is closed before the end */
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001141 if (msg->msg_state < HTTP_MSG_ENDING && req->flags & CF_SHUTR)
Christopher Faulet93e02d82019-03-08 14:18:50 +01001142 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001143
1144 waiting:
1145 /* waiting for the last bits to leave the buffer */
1146 if (req->flags & CF_SHUTW)
Christopher Faulet93e02d82019-03-08 14:18:50 +01001147 goto return_srv_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001148
1149 /* When TE: chunked is used, we need to get there again to parse remaining
1150 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
1151 * And when content-length is used, we never want to let the possible
1152 * shutdown be forwarded to the other side, as the state machine will
1153 * take care of it once the client responds. It's also important to
1154 * prevent TIME_WAITs from accumulating on the backend side, and for
1155 * HTTP/2 where the last frame comes with a shutdown.
1156 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001157 if (msg->flags & HTTP_MSGF_XFER_LEN)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001158 channel_dont_close(req);
1159
1160 /* We know that more data are expected, but we couldn't send more that
1161 * what we did. So we always set the CF_EXPECT_MORE flag so that the
1162 * system knows it must not set a PUSH on this first part. Interactive
1163 * modes are already handled by the stream sock layer. We must not do
1164 * this in content-length mode because it could present the MSG_MORE
1165 * flag with the last block of forwarded data, which would cause an
1166 * additional delay to be observed by the receiver.
1167 */
Christopher Faulet2151cdd2020-07-22 16:34:59 +02001168 if (HAS_REQ_DATA_FILTERS(s))
Christopher Faulete0768eb2018-10-03 16:38:02 +02001169 req->flags |= CF_EXPECT_MORE;
1170
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001171 DBG_TRACE_DEVEL("waiting for more data to forward",
1172 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001173 return 0;
1174
Christopher Faulet93e02d82019-03-08 14:18:50 +01001175 return_cli_abort:
Willy Tarreau4781b152021-04-06 13:53:36 +02001176 _HA_ATOMIC_INC(&sess->fe->fe_counters.cli_aborts);
1177 _HA_ATOMIC_INC(&s->be->be_counters.cli_aborts);
William Lallemand36119de2021-03-08 15:26:48 +01001178 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001179 _HA_ATOMIC_INC(&sess->listener->counters->cli_aborts);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001180 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001181 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.cli_aborts);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001182 if (!(s->flags & SF_ERR_MASK))
1183 s->flags |= SF_ERR_CLICL;
1184 status = 400;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001185 goto return_prx_cond;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001186
1187 return_srv_abort:
Willy Tarreau4781b152021-04-06 13:53:36 +02001188 _HA_ATOMIC_INC(&sess->fe->fe_counters.srv_aborts);
1189 _HA_ATOMIC_INC(&s->be->be_counters.srv_aborts);
William Lallemand36119de2021-03-08 15:26:48 +01001190 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001191 _HA_ATOMIC_INC(&sess->listener->counters->srv_aborts);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001192 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001193 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.srv_aborts);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001194 if (!(s->flags & SF_ERR_MASK))
1195 s->flags |= SF_ERR_SRVCL;
1196 status = 502;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001197 goto return_prx_cond;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001198
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001199 return_int_err:
1200 if (!(s->flags & SF_ERR_MASK))
1201 s->flags |= SF_ERR_INTERNAL;
Willy Tarreau4781b152021-04-06 13:53:36 +02001202 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
1203 _HA_ATOMIC_INC(&s->be->be_counters.internal_errors);
William Lallemand36119de2021-03-08 15:26:48 +01001204 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001205 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001206 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001207 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.internal_errors);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001208 status = 500;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001209 goto return_prx_cond;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001210
Christopher Faulet93e02d82019-03-08 14:18:50 +01001211 return_bad_req:
Willy Tarreau4781b152021-04-06 13:53:36 +02001212 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req);
William Lallemand36119de2021-03-08 15:26:48 +01001213 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001214 _HA_ATOMIC_INC(&sess->listener->counters->failed_req);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001215 status = 400;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001216 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001217
Christopher Fauletb8a53712019-12-16 11:29:38 +01001218 return_prx_cond:
Christopher Faulet9768c262018-10-22 09:34:31 +02001219 if (txn->status > 0) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001220 /* Note: we don't send any error if some data were already sent */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001221 http_reply_and_close(s, txn->status, NULL);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001222 } else {
Christopher Faulet93e02d82019-03-08 14:18:50 +01001223 txn->status = status;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001224 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001225 }
Christopher Fauletb8a53712019-12-16 11:29:38 +01001226 if (!(s->flags & SF_ERR_MASK))
1227 s->flags |= SF_ERR_PRXCOND;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001228 if (!(s->flags & SF_FINST_MASK))
1229 s->flags |= ((txn->rsp.msg_state < HTTP_MSG_ERROR) ? SF_FINST_H : SF_FINST_D);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001230 DBG_TRACE_DEVEL("leaving on error ",
1231 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001232 return 0;
1233}
1234
Willy Tarreau4596fe22022-05-17 19:07:51 +02001235/* Reset the stream and the backend stream connector to a situation suitable for attemption connection */
Olivier Houcharda254a372019-04-05 15:30:12 +02001236/* Returns 0 if we can attempt to retry, -1 otherwise */
Willy Tarreaub49672d2022-05-27 10:13:37 +02001237static __inline int do_l7_retry(struct stream *s, struct stconn *sc)
Olivier Houcharda254a372019-04-05 15:30:12 +02001238{
Christopher Faulet9f5382e2021-05-21 13:46:14 +02001239 struct channel *req, *res;
1240 int co_data;
Olivier Houcharda254a372019-04-05 15:30:12 +02001241
Christopher Faulet731c8e62022-03-29 16:08:44 +02001242 s->conn_retries++;
1243 if (s->conn_retries >= s->be->conn_retries)
Christopher Faulet552601d2021-05-26 10:31:06 +02001244 return -1;
Christopher Faulet5b82cc52020-10-12 15:18:50 +02001245
Christopher Faulete763c8c2021-05-05 18:23:59 +02001246 if (objt_server(s->target)) {
1247 if (s->flags & SF_CURR_SESS) {
1248 s->flags &= ~SF_CURR_SESS;
1249 _HA_ATOMIC_DEC(&__objt_server(s->target)->cur_sess);
1250 }
Willy Tarreau4781b152021-04-06 13:53:36 +02001251 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.retries);
Christopher Faulete763c8c2021-05-05 18:23:59 +02001252 }
Willy Tarreau4781b152021-04-06 13:53:36 +02001253 _HA_ATOMIC_INC(&s->be->be_counters.retries);
Willy Tarreau223995e2019-05-04 10:38:31 +02001254
Christopher Faulet9f5382e2021-05-21 13:46:14 +02001255 req = &s->req;
1256 res = &s->res;
Olivier Houcharda254a372019-04-05 15:30:12 +02001257 /* Remove any write error from the request, and read error from the response */
1258 req->flags &= ~(CF_WRITE_ERROR | CF_WRITE_TIMEOUT | CF_SHUTW | CF_SHUTW_NOW);
Christopher Faulet6e1bbc42022-12-12 08:08:15 +01001259 res->flags &= ~(CF_READ_ERROR | CF_READ_TIMEOUT | CF_SHUTR | CF_EOI | CF_READ_EVENT | CF_SHUTR_NOW);
Christopher Faulet7bf46bb2022-01-04 10:56:03 +01001260 res->analysers &= AN_RES_FLT_END;
Christopher Faulet50264b42022-03-30 19:39:30 +02001261 s->conn_err_type = STRM_ET_NONE;
Christopher Fauletae024ce2022-03-29 19:02:31 +02001262 s->flags &= ~(SF_CONN_EXP | SF_ERR_MASK | SF_FINST_MASK);
1263 s->conn_exp = TICK_ETERNITY;
Olivier Houchard4bd58672019-07-12 16:16:59 +02001264 stream_choose_redispatch(s);
Olivier Houcharda254a372019-04-05 15:30:12 +02001265 res->rex = TICK_ETERNITY;
1266 res->to_forward = 0;
1267 res->analyse_exp = TICK_ETERNITY;
1268 res->total = 0;
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +01001269
Willy Tarreau19c65a92022-05-27 08:49:24 +02001270 if (sc_reset_endp(s->scb) < 0) {
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +01001271 if (!(s->flags & SF_ERR_MASK))
1272 s->flags |= SF_ERR_INTERNAL;
1273 return -1;
1274 }
Olivier Houcharda254a372019-04-05 15:30:12 +02001275
Christopher Faulet9f5382e2021-05-21 13:46:14 +02001276 b_free(&req->buf);
1277 /* Swap the L7 buffer with the channel buffer */
1278 /* We know we stored the co_data as b_data, so get it there */
Christopher Faulete05bf9e2022-03-29 15:23:40 +02001279 co_data = b_data(&s->txn->l7_buffer);
1280 b_set_data(&s->txn->l7_buffer, b_size(&s->txn->l7_buffer));
1281 b_xfer(&req->buf, &s->txn->l7_buffer, b_data(&s->txn->l7_buffer));
Christopher Faulet9f5382e2021-05-21 13:46:14 +02001282 co_set_data(req, co_data);
Christopher Faulet5b82cc52020-10-12 15:18:50 +02001283
Ilya Shipitsinacf84592021-02-06 22:29:08 +05001284 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 +02001285
Olivier Houcharda254a372019-04-05 15:30:12 +02001286 b_reset(&res->buf);
1287 co_set_data(res, 0);
1288 return 0;
1289}
1290
Christopher Faulete0768eb2018-10-03 16:38:02 +02001291/* This stream analyser waits for a complete HTTP response. It returns 1 if the
1292 * processing can continue on next analysers, or zero if it either needs more
1293 * data or wants to immediately abort the response (eg: timeout, error, ...). It
1294 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->res.analysers
1295 * when it has nothing left to do, and may remove any analyser when it wants to
1296 * abort.
1297 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001298int http_wait_for_response(struct stream *s, struct channel *rep, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001299{
Christopher Faulet9768c262018-10-22 09:34:31 +02001300 /*
1301 * We will analyze a complete HTTP response to check the its syntax.
1302 *
1303 * Once the start line and all headers are received, we may perform a
1304 * capture of the error (if any), and we will set a few fields. We also
1305 * logging and finally headers capture.
1306 */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001307 struct session *sess = s->sess;
1308 struct http_txn *txn = s->txn;
1309 struct http_msg *msg = &txn->rsp;
Christopher Faulet9768c262018-10-22 09:34:31 +02001310 struct htx *htx;
Christopher Faulet61608322018-11-23 16:23:45 +01001311 struct connection *srv_conn;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001312 struct htx_sl *sl;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001313 int n;
1314
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001315 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001316
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001317 htx = htxbuf(&rep->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001318
Willy Tarreau4236f032019-03-05 10:43:32 +01001319 /* Parsing errors are caught here */
1320 if (htx->flags & HTX_FL_PARSING_ERROR)
1321 goto return_bad_res;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001322 if (htx->flags & HTX_FL_PROCESSING_ERROR)
1323 goto return_int_err;
Willy Tarreau4236f032019-03-05 10:43:32 +01001324
Christopher Faulete0768eb2018-10-03 16:38:02 +02001325 /*
1326 * Now we quickly check if we have found a full valid response.
1327 * If not so, we check the FD and buffer states before leaving.
1328 * A full response is indicated by the fact that we have seen
1329 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
1330 * responses are checked first.
1331 *
1332 * Depending on whether the client is still there or not, we
1333 * may send an error response back or not. Note that normally
1334 * we should only check for HTTP status there, and check I/O
1335 * errors somewhere else.
1336 */
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001337 next_one:
Christopher Faulet29f17582019-05-23 11:03:26 +02001338 if (unlikely(htx_is_empty(htx) || htx->first == -1)) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001339 /* 1: have we encountered a read error ? */
1340 if (rep->flags & CF_READ_ERROR) {
Willy Tarreaufd9417b2022-05-18 16:23:22 +02001341 struct connection *conn = sc_conn(s->scb);
Olivier Houchard865d8392019-05-03 22:46:27 +02001342
Christopher Fauletd9769232021-05-26 12:15:37 +02001343 /* Perform a L7 retry because server refuses the early data. */
Christopher Faulete05bf9e2022-03-29 15:23:40 +02001344 if ((txn->flags & TX_L7_RETRY) &&
Christopher Fauletd9769232021-05-26 12:15:37 +02001345 (s->be->retry_type & PR_RE_EARLY_ERROR) &&
1346 conn && conn->err_code == CO_ER_SSL_EARLY_FAILED &&
Willy Tarreau7cb9e6c2022-05-17 19:40:40 +02001347 do_l7_retry(s, s->scb) == 0) {
Christopher Fauletd9769232021-05-26 12:15:37 +02001348 DBG_TRACE_DEVEL("leaving on L7 retry",
1349 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
1350 return 0;
1351 }
1352
Olivier Houchard6db16992019-05-17 15:40:49 +02001353 if (txn->flags & TX_NOT_FIRST)
1354 goto abort_keep_alive;
1355
Willy Tarreau4781b152021-04-06 13:53:36 +02001356 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001357 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001358 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001359 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001360 }
1361
Christopher Fauletd9769232021-05-26 12:15:37 +02001362 /* if the server refused the early data, just send a 425 */
1363 if (conn && conn->err_code == CO_ER_SSL_EARLY_FAILED)
Olivier Houchard865d8392019-05-03 22:46:27 +02001364 txn->status = 425;
Christopher Fauletd9769232021-05-26 12:15:37 +02001365 else {
1366 txn->status = 502;
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001367 stream_inc_http_fail_ctr(s);
Christopher Fauletd9769232021-05-26 12:15:37 +02001368 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001369
Willy Tarreaucb041662022-05-17 19:44:42 +02001370 s->scb->flags |= SC_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001371 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001372
1373 if (!(s->flags & SF_ERR_MASK))
1374 s->flags |= SF_ERR_SRVCL;
Christopher Faulet4a66c942023-01-13 09:43:21 +01001375 http_set_term_flags(s);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001376 DBG_TRACE_DEVEL("leaving on error",
1377 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001378 return 0;
1379 }
1380
Christopher Faulet9768c262018-10-22 09:34:31 +02001381 /* 2: read timeout : return a 504 to the client. */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001382 else if (rep->flags & CF_READ_TIMEOUT) {
Christopher Faulete05bf9e2022-03-29 15:23:40 +02001383 if ((txn->flags & TX_L7_RETRY) &&
Olivier Houcharda254a372019-04-05 15:30:12 +02001384 (s->be->retry_type & PR_RE_TIMEOUT)) {
Willy Tarreau7cb9e6c2022-05-17 19:40:40 +02001385 if (co_data(rep) || do_l7_retry(s, s->scb) == 0) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001386 DBG_TRACE_DEVEL("leaving on L7 retry",
1387 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharda254a372019-04-05 15:30:12 +02001388 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001389 }
Olivier Houcharda254a372019-04-05 15:30:12 +02001390 }
Willy Tarreau4781b152021-04-06 13:53:36 +02001391 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001392 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001393 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001394 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001395 }
1396
Christopher Faulete0768eb2018-10-03 16:38:02 +02001397 txn->status = 504;
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001398 stream_inc_http_fail_ctr(s);
Willy Tarreaucb041662022-05-17 19:44:42 +02001399 s->scb->flags |= SC_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001400 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001401
1402 if (!(s->flags & SF_ERR_MASK))
1403 s->flags |= SF_ERR_SRVTO;
Christopher Faulet4a66c942023-01-13 09:43:21 +01001404 http_set_term_flags(s);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001405 DBG_TRACE_DEVEL("leaving on error",
1406 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001407 return 0;
1408 }
1409
Christopher Faulet9768c262018-10-22 09:34:31 +02001410 /* 3: client abort with an abortonclose */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001411 else if ((rep->flags & CF_SHUTR) && ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001412 _HA_ATOMIC_INC(&sess->fe->fe_counters.cli_aborts);
1413 _HA_ATOMIC_INC(&s->be->be_counters.cli_aborts);
William Lallemand36119de2021-03-08 15:26:48 +01001414 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001415 _HA_ATOMIC_INC(&sess->listener->counters->cli_aborts);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001416 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001417 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.cli_aborts);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001418
Christopher Faulete0768eb2018-10-03 16:38:02 +02001419 txn->status = 400;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001420 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001421
1422 if (!(s->flags & SF_ERR_MASK))
1423 s->flags |= SF_ERR_CLICL;
Christopher Faulet4a66c942023-01-13 09:43:21 +01001424 http_set_term_flags(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001425
1426 /* process_stream() will take care of the error */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001427 DBG_TRACE_DEVEL("leaving on error",
1428 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001429 return 0;
1430 }
1431
Christopher Faulet9768c262018-10-22 09:34:31 +02001432 /* 4: close from server, capture the response if the server has started to respond */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001433 else if (rep->flags & CF_SHUTR) {
Christopher Faulete05bf9e2022-03-29 15:23:40 +02001434 if ((txn->flags & TX_L7_RETRY) &&
Olivier Houcharda254a372019-04-05 15:30:12 +02001435 (s->be->retry_type & PR_RE_DISCONNECTED)) {
Willy Tarreau7cb9e6c2022-05-17 19:40:40 +02001436 if (co_data(rep) || do_l7_retry(s, s->scb) == 0) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001437 DBG_TRACE_DEVEL("leaving on L7 retry",
1438 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharda254a372019-04-05 15:30:12 +02001439 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001440 }
Olivier Houcharda254a372019-04-05 15:30:12 +02001441 }
1442
Olivier Houchard6db16992019-05-17 15:40:49 +02001443 if (txn->flags & TX_NOT_FIRST)
1444 goto abort_keep_alive;
1445
Willy Tarreau4781b152021-04-06 13:53:36 +02001446 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001447 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001448 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001449 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001450 }
1451
Christopher Faulete0768eb2018-10-03 16:38:02 +02001452 txn->status = 502;
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001453 stream_inc_http_fail_ctr(s);
Willy Tarreaucb041662022-05-17 19:44:42 +02001454 s->scb->flags |= SC_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001455 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001456
1457 if (!(s->flags & SF_ERR_MASK))
1458 s->flags |= SF_ERR_SRVCL;
Christopher Faulet4a66c942023-01-13 09:43:21 +01001459 http_set_term_flags(s);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001460 DBG_TRACE_DEVEL("leaving on error",
1461 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001462 return 0;
1463 }
1464
Christopher Faulet9768c262018-10-22 09:34:31 +02001465 /* 5: write error to client (we don't send any message then) */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001466 else if (rep->flags & CF_WRITE_ERROR) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001467 if (txn->flags & TX_NOT_FIRST)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001468 goto abort_keep_alive;
1469
Willy Tarreau4781b152021-04-06 13:53:36 +02001470 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001471 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001472 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001473 rep->analysers &= AN_RES_FLT_END;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001474
1475 if (!(s->flags & SF_ERR_MASK))
1476 s->flags |= SF_ERR_CLICL;
Christopher Faulet4a66c942023-01-13 09:43:21 +01001477 http_set_term_flags(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001478
1479 /* process_stream() will take care of the error */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001480 DBG_TRACE_DEVEL("leaving on error",
1481 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001482 return 0;
1483 }
1484
1485 channel_dont_close(rep);
1486 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001487 DBG_TRACE_DEVEL("waiting for more data",
1488 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001489 return 0;
1490 }
1491
1492 /* More interesting part now : we know that we have a complete
1493 * response which at least looks like HTTP. We have an indicator
1494 * of each header's length, so we can parse them quickly.
1495 */
Christopher Faulet29f17582019-05-23 11:03:26 +02001496 BUG_ON(htx_get_first_type(htx) != HTX_BLK_RES_SL);
Christopher Faulet297fbb42019-05-13 14:41:27 +02001497 sl = http_get_stline(htx);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001498
Christopher Faulet1f08bff2021-05-26 13:14:39 +02001499 /* Perform a L7 retry because of the status code */
Christopher Faulete05bf9e2022-03-29 15:23:40 +02001500 if ((txn->flags & TX_L7_RETRY) &&
Christopher Faulet1f08bff2021-05-26 13:14:39 +02001501 l7_status_match(s->be, sl->info.res.status) &&
Willy Tarreau7cb9e6c2022-05-17 19:40:40 +02001502 do_l7_retry(s, s->scb) == 0) {
Christopher Faulet1f08bff2021-05-26 13:14:39 +02001503 DBG_TRACE_DEVEL("leaving on L7 retry", STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
1504 return 0;
1505 }
1506
1507 /* Now, L7 buffer is useless, it can be released */
Christopher Faulete05bf9e2022-03-29 15:23:40 +02001508 b_free(&txn->l7_buffer);
Christopher Faulet1f08bff2021-05-26 13:14:39 +02001509
1510 msg->msg_state = HTTP_MSG_BODY;
1511
1512
Christopher Faulet9768c262018-10-22 09:34:31 +02001513 /* 0: we might have to print this header in debug mode */
1514 if (unlikely((global.mode & MODE_DEBUG) &&
1515 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
1516 int32_t pos;
1517
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001518 http_debug_stline("srvrep", s, sl);
Christopher Faulet9768c262018-10-22 09:34:31 +02001519
Christopher Fauleta3f15502019-05-13 15:27:23 +02001520 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001521 struct htx_blk *blk = htx_get_blk(htx, pos);
1522 enum htx_blk_type type = htx_get_blk_type(blk);
1523
1524 if (type == HTX_BLK_EOH)
1525 break;
1526 if (type != HTX_BLK_HDR)
1527 continue;
1528
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001529 http_debug_hdr("srvhdr", s,
1530 htx_get_blk_name(htx, blk),
1531 htx_get_blk_value(htx, blk));
Christopher Faulet9768c262018-10-22 09:34:31 +02001532 }
1533 }
1534
Christopher Faulet03599112018-11-27 11:21:21 +01001535 /* 1: get the status code and the version. Also set HTTP flags */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001536 txn->status = sl->info.res.status;
Christopher Faulet03599112018-11-27 11:21:21 +01001537 if (sl->flags & HTX_SL_F_VER_11)
Christopher Faulet9768c262018-10-22 09:34:31 +02001538 msg->flags |= HTTP_MSGF_VER_11;
Christopher Faulet03599112018-11-27 11:21:21 +01001539 if (sl->flags & HTX_SL_F_XFER_LEN) {
1540 msg->flags |= HTTP_MSGF_XFER_LEN;
Christopher Faulet2a408542020-11-20 14:22:37 +01001541 if (sl->flags & HTX_SL_F_CLEN)
1542 msg->flags |= HTTP_MSGF_CNT_LEN;
1543 else if (sl->flags & HTX_SL_F_CHNK)
1544 msg->flags |= HTTP_MSGF_TE_CHNK;
Christopher Faulet03599112018-11-27 11:21:21 +01001545 }
Christopher Faulet2a408542020-11-20 14:22:37 +01001546 if (sl->flags & HTX_SL_F_BODYLESS)
1547 msg->flags |= HTTP_MSGF_BODYLESS;
Christopher Faulet576c3582021-01-08 15:53:01 +01001548 if (sl->flags & HTX_SL_F_CONN_UPG)
1549 msg->flags |= HTTP_MSGF_CONN_UPG;
Christopher Faulet9768c262018-10-22 09:34:31 +02001550
1551 n = txn->status / 100;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001552 if (n < 1 || n > 5)
1553 n = 0;
Christopher Faulet9768c262018-10-22 09:34:31 +02001554
Christopher Faulete0768eb2018-10-03 16:38:02 +02001555 /* when the client triggers a 4xx from the server, it's most often due
1556 * to a missing object or permission. These events should be tracked
1557 * because if they happen often, it may indicate a brute force or a
1558 * vulnerability scan.
1559 */
1560 if (n == 4)
1561 stream_inc_http_err_ctr(s);
1562
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001563 if (n == 5 && txn->status != 501 && txn->status != 505)
1564 stream_inc_http_fail_ctr(s);
1565
Marcin Deranek3c27dda2020-05-15 18:32:51 +02001566 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001567 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.p.http.rsp[n]);
1568 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.p.http.cum_req);
Marcin Deranek3c27dda2020-05-15 18:32:51 +02001569 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001570
Christopher Faulete0768eb2018-10-03 16:38:02 +02001571 /* Adjust server's health based on status code. Note: status codes 501
1572 * and 505 are triggered on demand by client request, so we must not
1573 * count them as server failures.
1574 */
1575 if (objt_server(s->target)) {
1576 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001577 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_OK);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001578 else
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001579 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_STS);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001580 }
1581
1582 /*
1583 * We may be facing a 100-continue response, or any other informational
1584 * 1xx response which is non-final, in which case this is not the right
1585 * response, and we're waiting for the next one. Let's allow this response
1586 * to go to the client and wait for the next one. There's an exception for
1587 * 101 which is used later in the code to switch protocols.
1588 */
1589 if (txn->status < 200 &&
1590 (txn->status == 100 || txn->status >= 102)) {
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001591 FLT_STRM_CB(s, flt_http_reset(s, msg));
Christopher Faulet421e7692019-06-13 11:16:45 +02001592 htx->first = channel_htx_fwd_headers(rep, htx);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001593 msg->msg_state = HTTP_MSG_RPBEFORE;
Christopher Faulet3499f622019-09-03 15:23:54 +02001594 msg->flags = 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001595 txn->status = 0;
1596 s->logs.t_data = -1; /* was not a response yet */
Christopher Faulet7d518452020-08-31 11:07:07 +02001597 rep->flags |= CF_SEND_DONTWAIT; /* Send ASAP informational messages */
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001598 goto next_one;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001599 }
1600
Christopher Faulet6e6c7b12021-01-08 16:02:05 +01001601 /* A 101-switching-protocols must contains a Connection header with the
1602 * "upgrade" option and the request too. It means both are agree to
1603 * upgrade. It is not so strict because there is no test on the Upgrade
1604 * header content. But it is probably stronger enough for now.
1605 */
1606 if (txn->status == 101 &&
1607 (!(txn->req.flags & HTTP_MSGF_CONN_UPG) || !(txn->rsp.flags & HTTP_MSGF_CONN_UPG)))
1608 goto return_bad_res;
1609
Christopher Faulete0768eb2018-10-03 16:38:02 +02001610 /*
1611 * 2: check for cacheability.
1612 */
1613
1614 switch (txn->status) {
1615 case 200:
1616 case 203:
1617 case 204:
1618 case 206:
1619 case 300:
1620 case 301:
1621 case 404:
1622 case 405:
1623 case 410:
1624 case 414:
1625 case 501:
1626 break;
1627 default:
1628 /* RFC7231#6.1:
1629 * Responses with status codes that are defined as
1630 * cacheable by default (e.g., 200, 203, 204, 206,
1631 * 300, 301, 404, 405, 410, 414, and 501 in this
1632 * specification) can be reused by a cache with
1633 * heuristic expiration unless otherwise indicated
1634 * by the method definition or explicit cache
1635 * controls [RFC7234]; all other status codes are
1636 * not cacheable by default.
1637 */
1638 txn->flags &= ~(TX_CACHEABLE | TX_CACHE_COOK);
1639 break;
1640 }
1641
1642 /*
1643 * 3: we may need to capture headers
1644 */
1645 s->logs.logwait &= ~LW_RESP;
1646 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->res_cap))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001647 http_capture_headers(htx, s->res_cap, sess->fe->rsp_cap);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001648
Christopher Faulet9768c262018-10-22 09:34:31 +02001649 /* Skip parsing if no content length is possible. */
Christopher Fauletc75668e2020-12-07 18:10:32 +01001650 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status >= 200 && txn->status < 300) ||
Christopher Faulete0768eb2018-10-03 16:38:02 +02001651 txn->status == 101)) {
1652 /* Either we've established an explicit tunnel, or we're
1653 * switching the protocol. In both cases, we're very unlikely
1654 * to understand the next protocols. We have to switch to tunnel
1655 * mode, so that we transfer the request and responses then let
1656 * this protocol pass unmodified. When we later implement specific
1657 * parsers for such protocols, we'll want to check the Upgrade
1658 * header which contains information about that protocol for
1659 * responses with status 101 (eg: see RFC2817 about TLS).
1660 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02001661 txn->flags |= TX_CON_WANT_TUN;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001662 }
1663
Christopher Faulet61608322018-11-23 16:23:45 +01001664 /* check for NTML authentication headers in 401 (WWW-Authenticate) and
1665 * 407 (Proxy-Authenticate) responses and set the connection to private
1666 */
Willy Tarreaufd9417b2022-05-18 16:23:22 +02001667 srv_conn = sc_conn(s->scb);
Christopher Faulet61608322018-11-23 16:23:45 +01001668 if (srv_conn) {
1669 struct ist hdr;
1670 struct http_hdr_ctx ctx;
1671
1672 if (txn->status == 401)
1673 hdr = ist("WWW-Authenticate");
1674 else if (txn->status == 407)
1675 hdr = ist("Proxy-Authenticate");
1676 else
1677 goto end;
1678
1679 ctx.blk = NULL;
1680 while (http_find_header(htx, hdr, &ctx, 0)) {
Willy Tarreauf1dcced2020-05-07 19:27:02 +02001681 /* If www-authenticate contains "Negotiate", "Nego2", or "NTLM",
1682 * possibly followed by blanks and a base64 string, the connection
1683 * is private. Since it's a mess to deal with, we only check for
1684 * values starting with "NTLM" or "Nego". Note that often multiple
1685 * headers are sent by the server there.
1686 */
1687 if ((ctx.value.len >= 4 && strncasecmp(ctx.value.ptr, "Nego", 4) == 0) ||
Willy Tarreau49a1d282020-05-07 19:10:15 +02001688 (ctx.value.len >= 4 && strncasecmp(ctx.value.ptr, "NTLM", 4) == 0)) {
Olivier Houchard250031e2019-05-29 15:01:50 +02001689 sess->flags |= SESS_FL_PREFER_LAST;
Christopher Faulet08016ab2020-07-01 16:10:06 +02001690 conn_set_owner(srv_conn, sess, NULL);
Christopher Faulet21ddc742020-07-01 15:26:14 +02001691 conn_set_private(srv_conn);
Ilya Shipitsin6b79f382020-07-23 00:32:55 +05001692 /* If it fail now, the same will be done in mux->detach() callback */
Christopher Faulet08016ab2020-07-01 16:10:06 +02001693 session_add_conn(srv_conn->owner, srv_conn, srv_conn->target);
Willy Tarreauf1dcced2020-05-07 19:27:02 +02001694 break;
Olivier Houchard250031e2019-05-29 15:01:50 +02001695 }
Christopher Faulet61608322018-11-23 16:23:45 +01001696 }
1697 }
1698
1699 end:
Christopher Faulete0768eb2018-10-03 16:38:02 +02001700 /* we want to have the response time before we start processing it */
1701 s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now);
1702
1703 /* end of job, return OK */
1704 rep->analysers &= ~an_bit;
1705 rep->analyse_exp = TICK_ETERNITY;
1706 channel_auto_close(rep);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001707 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001708 return 1;
1709
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001710 return_int_err:
Willy Tarreau4781b152021-04-06 13:53:36 +02001711 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
1712 _HA_ATOMIC_INC(&s->be->be_counters.internal_errors);
William Lallemand36119de2021-03-08 15:26:48 +01001713 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001714 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Fauletb8a53712019-12-16 11:29:38 +01001715 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001716 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.internal_errors);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001717 txn->status = 500;
1718 if (!(s->flags & SF_ERR_MASK))
1719 s->flags |= SF_ERR_INTERNAL;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001720 goto return_prx_cond;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001721
1722 return_bad_res:
Willy Tarreau4781b152021-04-06 13:53:36 +02001723 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Faulet47365272018-10-31 17:40:50 +01001724 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001725 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001726 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Christopher Faulet47365272018-10-31 17:40:50 +01001727 }
Olivier Houcharde3249a92019-05-03 23:01:47 +02001728 if ((s->be->retry_type & PR_RE_JUNK_REQUEST) &&
Christopher Faulete05bf9e2022-03-29 15:23:40 +02001729 (txn->flags & TX_L7_RETRY) &&
Willy Tarreau7cb9e6c2022-05-17 19:40:40 +02001730 do_l7_retry(s, s->scb) == 0) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001731 DBG_TRACE_DEVEL("leaving on L7 retry",
1732 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharde3249a92019-05-03 23:01:47 +02001733 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001734 }
Christopher Faulet47365272018-10-31 17:40:50 +01001735 txn->status = 502;
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001736 stream_inc_http_fail_ctr(s);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001737 /* fall through */
1738
Christopher Fauletb8a53712019-12-16 11:29:38 +01001739 return_prx_cond:
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001740 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet4a66c942023-01-13 09:43:21 +01001741 http_set_term_flags(s);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001742
Willy Tarreaucb041662022-05-17 19:44:42 +02001743 s->scb->flags |= SC_FL_NOLINGER;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001744 DBG_TRACE_DEVEL("leaving on error",
1745 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulet47365272018-10-31 17:40:50 +01001746 return 0;
1747
Christopher Faulete0768eb2018-10-03 16:38:02 +02001748 abort_keep_alive:
1749 /* A keep-alive request to the server failed on a network error.
1750 * The client is required to retry. We need to close without returning
1751 * any other information so that the client retries.
1752 */
1753 txn->status = 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001754 s->logs.logwait = 0;
1755 s->logs.level = 0;
1756 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001757 http_reply_and_close(s, txn->status, NULL);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001758 DBG_TRACE_DEVEL("leaving by closing K/A connection",
1759 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001760 return 0;
1761}
1762
1763/* This function performs all the processing enabled for the current response.
1764 * It normally returns 1 unless it wants to break. It relies on buffers flags,
1765 * and updates s->res.analysers. It might make sense to explode it into several
1766 * other functions. It works like process_request (see indications above).
1767 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001768int http_process_res_common(struct stream *s, struct channel *rep, int an_bit, struct proxy *px)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001769{
1770 struct session *sess = s->sess;
1771 struct http_txn *txn = s->txn;
1772 struct http_msg *msg = &txn->rsp;
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001773 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001774 struct proxy *cur_proxy;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001775 enum rule_result ret = HTTP_RULE_RES_CONT;
1776
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001777 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
1778 return 0;
Christopher Faulet9768c262018-10-22 09:34:31 +02001779
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001780 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001781
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001782 htx = htxbuf(&rep->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001783
1784 /* The stats applet needs to adjust the Connection header but we don't
1785 * apply any filter there.
1786 */
1787 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
1788 rep->analysers &= ~an_bit;
1789 rep->analyse_exp = TICK_ETERNITY;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001790 goto end;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001791 }
1792
1793 /*
1794 * We will have to evaluate the filters.
1795 * As opposed to version 1.2, now they will be evaluated in the
1796 * filters order and not in the header order. This means that
1797 * each filter has to be validated among all headers.
1798 *
1799 * Filters are tried with ->be first, then with ->fe if it is
1800 * different from ->be.
1801 *
1802 * Maybe we are in resume condiion. In this case I choose the
1803 * "struct proxy" which contains the rule list matching the resume
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05001804 * pointer. If none of these "struct proxy" match, I initialise
Christopher Faulete0768eb2018-10-03 16:38:02 +02001805 * the process with the first one.
1806 *
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05001807 * In fact, I check only correspondence between the current list
Christopher Faulete0768eb2018-10-03 16:38:02 +02001808 * pointer and the ->fe rule list. If it doesn't match, I initialize
1809 * the loop with the ->be.
1810 */
Christopher Fauletd4150ad2021-10-13 15:35:55 +02001811 if (s->current_rule_list == &sess->fe->http_res_rules ||
1812 (sess->fe->defpx && s->current_rule_list == &sess->fe->defpx->http_res_rules))
Christopher Faulete0768eb2018-10-03 16:38:02 +02001813 cur_proxy = sess->fe;
1814 else
1815 cur_proxy = s->be;
Christopher Fauletd4150ad2021-10-13 15:35:55 +02001816
Christopher Faulete0768eb2018-10-03 16:38:02 +02001817 while (1) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001818 /* evaluate http-response rules */
Christopher Faulet46f46df2021-11-09 16:33:25 +01001819 if (ret == HTTP_RULE_RES_CONT || ret == HTTP_RULE_RES_STOP) {
Christopher Fauletd4150ad2021-10-13 15:35:55 +02001820 struct list *def_rules, *rules;
1821
1822 def_rules = ((cur_proxy->defpx && (cur_proxy == s->be || cur_proxy->defpx != s->be->defpx)) ? &cur_proxy->defpx->http_res_rules : NULL);
1823 rules = &cur_proxy->http_res_rules;
1824
1825 ret = http_res_get_intercept_rule(cur_proxy, def_rules, rules, s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001826
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001827 switch (ret) {
1828 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
1829 goto return_prx_yield;
1830
1831 case HTTP_RULE_RES_CONT:
1832 case HTTP_RULE_RES_STOP: /* nothing to do */
1833 break;
1834
1835 case HTTP_RULE_RES_DENY: /* deny or tarpit */
1836 goto deny;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001837
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001838 case HTTP_RULE_RES_ABRT: /* abort request, response already sent */
1839 goto return_prx_cond;
1840
1841 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
Christopher Fauletb8a53712019-12-16 11:29:38 +01001842 goto done;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001843
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001844 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
1845 goto return_bad_res;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001846
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001847 case HTTP_RULE_RES_ERROR: /* failed with a bad request */
1848 goto return_int_err;
1849 }
1850
1851 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001852
Christopher Faulete0768eb2018-10-03 16:38:02 +02001853 /* check whether we're already working on the frontend */
1854 if (cur_proxy == sess->fe)
1855 break;
1856 cur_proxy = sess->fe;
1857 }
1858
Christopher Faulete0768eb2018-10-03 16:38:02 +02001859 /* OK that's all we can do for 1xx responses */
1860 if (unlikely(txn->status < 200 && txn->status != 101))
Christopher Fauletf2824e62018-10-01 12:12:37 +02001861 goto end;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001862
1863 /*
1864 * Now check for a server cookie.
1865 */
1866 if (s->be->cookie_name || sess->fe->capture_name || (s->be->options & PR_O_CHK_CACHE))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001867 http_manage_server_side_cookies(s, rep);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001868
1869 /*
1870 * Check for cache-control or pragma headers if required.
1871 */
1872 if ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001873 http_check_response_for_cacheability(s, rep);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001874
1875 /*
1876 * Add server cookie in the response if needed
1877 */
1878 if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) &&
1879 !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) &&
1880 (!(s->flags & SF_DIRECT) ||
1881 ((s->be->cookie_maxidle || txn->cookie_last_date) &&
1882 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
1883 (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
1884 (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
1885 (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
1886 !(s->flags & SF_IGNORE_PRST)) {
1887 /* the server is known, it's not the one the client requested, or the
1888 * cookie's last seen date needs to be refreshed. We have to
1889 * insert a set-cookie here, except if we want to insert only on POST
1890 * requests and this one isn't. Note that servers which don't have cookies
1891 * (eg: some backup servers) will return a full cookie removal request.
1892 */
Willy Tarreau88bc8002021-12-06 07:01:02 +00001893 if (!__objt_server(s->target)->cookie) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001894 chunk_printf(&trash,
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001895 "%s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
Christopher Faulete0768eb2018-10-03 16:38:02 +02001896 s->be->cookie_name);
1897 }
1898 else {
Willy Tarreau88bc8002021-12-06 07:01:02 +00001899 chunk_printf(&trash, "%s=%s", s->be->cookie_name, __objt_server(s->target)->cookie);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001900
1901 if (s->be->cookie_maxidle || s->be->cookie_maxlife) {
1902 /* emit last_date, which is mandatory */
1903 trash.area[trash.data++] = COOKIE_DELIM_DATE;
1904 s30tob64((date.tv_sec+3) >> 2,
1905 trash.area + trash.data);
1906 trash.data += 5;
1907
1908 if (s->be->cookie_maxlife) {
1909 /* emit first_date, which is either the original one or
1910 * the current date.
1911 */
1912 trash.area[trash.data++] = COOKIE_DELIM_DATE;
1913 s30tob64(txn->cookie_first_date ?
1914 txn->cookie_first_date >> 2 :
1915 (date.tv_sec+3) >> 2,
1916 trash.area + trash.data);
1917 trash.data += 5;
1918 }
1919 }
1920 chunk_appendf(&trash, "; path=/");
1921 }
1922
1923 if (s->be->cookie_domain)
1924 chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain);
1925
1926 if (s->be->ck_opts & PR_CK_HTTPONLY)
1927 chunk_appendf(&trash, "; HttpOnly");
1928
1929 if (s->be->ck_opts & PR_CK_SECURE)
1930 chunk_appendf(&trash, "; Secure");
1931
Christopher Faulet2f533902020-01-21 11:06:48 +01001932 if (s->be->cookie_attrs)
1933 chunk_appendf(&trash, "; %s", s->be->cookie_attrs);
1934
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001935 if (unlikely(!http_add_header(htx, ist("Set-Cookie"), ist2(trash.area, trash.data))))
Christopher Fauletd649b572022-06-01 17:42:35 +02001936 goto return_fail_rewrite;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001937
1938 txn->flags &= ~TX_SCK_MASK;
1939 if (__objt_server(s->target)->cookie && (s->flags & SF_DIRECT))
1940 /* the server did not change, only the date was updated */
1941 txn->flags |= TX_SCK_UPDATED;
1942 else
1943 txn->flags |= TX_SCK_INSERTED;
1944
1945 /* Here, we will tell an eventual cache on the client side that we don't
1946 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
1947 * Some caches understand the correct form: 'no-cache="set-cookie"', but
1948 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
1949 */
1950 if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
1951
1952 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
1953
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001954 if (unlikely(!http_add_header(htx, ist("Cache-control"), ist("private"))))
Christopher Fauletd649b572022-06-01 17:42:35 +02001955 goto return_fail_rewrite;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001956 }
1957 }
1958
1959 /*
1960 * Check if result will be cacheable with a cookie.
1961 * We'll block the response if security checks have caught
1962 * nasty things such as a cacheable cookie.
1963 */
1964 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
1965 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
1966 (s->be->options & PR_O_CHK_CACHE)) {
1967 /* we're in presence of a cacheable response containing
1968 * a set-cookie header. We'll block it as requested by
1969 * the 'checkcache' option, and send an alert.
1970 */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001971 ha_alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreau88bc8002021-12-06 07:01:02 +00001972 s->be->id, objt_server(s->target) ? __objt_server(s->target)->id : "<dispatch>");
Christopher Faulete0768eb2018-10-03 16:38:02 +02001973 send_log(s->be, LOG_ALERT,
1974 "Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreau88bc8002021-12-06 07:01:02 +00001975 s->be->id, objt_server(s->target) ? __objt_server(s->target)->id : "<dispatch>");
Christopher Fauletb8a53712019-12-16 11:29:38 +01001976 goto deny;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001977 }
1978
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001979 end:
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01001980 /*
1981 * Evaluate after-response rules before forwarding the response. rules
1982 * from the backend are evaluated first, then one from the frontend if
1983 * it differs.
1984 */
1985 if (!http_eval_after_res_rules(s))
1986 goto return_int_err;
1987
Christopher Fauletc2ac5e42021-03-08 18:20:09 +01001988 /* Filter the response headers if there are filters attached to the
1989 * stream.
1990 */
1991 if (HAS_FILTERS(s))
1992 rep->analysers |= AN_RES_FLT_HTTP_HDRS;
1993
Christopher Faulete0768eb2018-10-03 16:38:02 +02001994 /* Always enter in the body analyzer */
1995 rep->analysers &= ~AN_RES_FLT_XFER_DATA;
1996 rep->analysers |= AN_RES_HTTP_XFER_BODY;
1997
1998 /* if the user wants to log as soon as possible, without counting
1999 * bytes from the server, then this is the right moment. We have
2000 * to temporarily assign bytes_out to log what we currently have.
2001 */
2002 if (!LIST_ISEMPTY(&sess->fe->logformat) && !(s->logs.logwait & LW_BYTES)) {
2003 s->logs.t_close = s->logs.t_data; /* to get a valid end date */
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002004 s->logs.bytes_out = htx->data;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002005 s->do_log(s);
2006 s->logs.bytes_out = 0;
2007 }
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002008
Christopher Fauletb8a53712019-12-16 11:29:38 +01002009 done:
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002010 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002011 rep->analysers &= ~an_bit;
2012 rep->analyse_exp = TICK_ETERNITY;
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002013 s->current_rule = s->current_rule_list = NULL;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002014 return 1;
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002015
Christopher Fauletb8a53712019-12-16 11:29:38 +01002016 deny:
Willy Tarreau4781b152021-04-06 13:53:36 +02002017 _HA_ATOMIC_INC(&sess->fe->fe_counters.denied_resp);
2018 _HA_ATOMIC_INC(&s->be->be_counters.denied_resp);
William Lallemand36119de2021-03-08 15:26:48 +01002019 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02002020 _HA_ATOMIC_INC(&sess->listener->counters->denied_resp);
Christopher Fauleta08546b2019-12-16 16:07:34 +01002021 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02002022 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.denied_resp);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002023 goto return_prx_err;
2024
Christopher Fauletd649b572022-06-01 17:42:35 +02002025 return_fail_rewrite:
2026 if (!(s->flags & SF_ERR_MASK))
2027 s->flags |= SF_ERR_PRXCOND;
2028 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_rewrites);
2029 _HA_ATOMIC_INC(&s->be->be_counters.failed_rewrites);
2030 if (sess->listener && sess->listener->counters)
2031 _HA_ATOMIC_INC(&sess->listener->counters->failed_rewrites);
2032 if (objt_server(s->target))
2033 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_rewrites);
2034 /* fall through */
2035
Christopher Fauletb8a53712019-12-16 11:29:38 +01002036 return_int_err:
2037 txn->status = 500;
2038 if (!(s->flags & SF_ERR_MASK))
2039 s->flags |= SF_ERR_INTERNAL;
Willy Tarreau4781b152021-04-06 13:53:36 +02002040 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
2041 _HA_ATOMIC_INC(&s->be->be_counters.internal_errors);
Dragan Dosen9a006f92021-09-21 13:02:09 +02002042 if (sess->listener && sess->listener->counters)
2043 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Fauletcff0f732019-12-16 16:13:44 +01002044 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02002045 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.internal_errors);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002046 goto return_prx_err;
2047
2048 return_bad_res:
2049 txn->status = 502;
Willy Tarreau826f3ab2021-02-10 12:07:15 +01002050 stream_inc_http_fail_ctr(s);
Willy Tarreau4781b152021-04-06 13:53:36 +02002051 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Fauleta20a6532020-02-05 10:16:41 +01002052 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02002053 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Christopher Fauleta20a6532020-02-05 10:16:41 +01002054 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_RSP);
2055 }
Christopher Fauletb8a53712019-12-16 11:29:38 +01002056 /* fall through */
2057
2058 return_prx_err:
2059 http_reply_and_close(s, txn->status, http_error_message(s));
2060 /* fall through */
2061
2062 return_prx_cond:
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002063 s->logs.t_data = -1; /* was not a valid response */
Willy Tarreaucb041662022-05-17 19:44:42 +02002064 s->scb->flags |= SC_FL_NOLINGER;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002065
Christopher Faulet4a66c942023-01-13 09:43:21 +01002066 http_set_term_flags(s);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002067
Christopher Faulete58c0002020-03-02 16:21:01 +01002068 rep->analysers &= AN_RES_FLT_END;
2069 s->req.analysers &= AN_REQ_FLT_END;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002070 rep->analyse_exp = TICK_ETERNITY;
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002071 s->current_rule = s->current_rule_list = NULL;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002072 DBG_TRACE_DEVEL("leaving on error",
2073 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002074 return 0;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002075
2076 return_prx_yield:
2077 channel_dont_close(rep);
2078 DBG_TRACE_DEVEL("waiting for more data",
2079 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
2080 return 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002081}
2082
2083/* This function is an analyser which forwards response body (including chunk
2084 * sizes if any). It is called as soon as we must forward, even if we forward
2085 * zero byte. The only situation where it must not be called is when we're in
2086 * tunnel mode and we want to forward till the close. It's used both to forward
2087 * remaining data and to resync after end of body. It expects the msg_state to
2088 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
2089 * read more data, or 1 once we can go on with next request or end the stream.
2090 *
2091 * It is capable of compressing response data both in content-length mode and
2092 * in chunked mode. The state machines follows different flows depending on
2093 * whether content-length and chunked modes are used, since there are no
2094 * trailers in content-length :
2095 *
2096 * chk-mode cl-mode
2097 * ,----- BODY -----.
2098 * / \
2099 * V size > 0 V chk-mode
2100 * .--> SIZE -------------> DATA -------------> CRLF
2101 * | | size == 0 | last byte |
2102 * | v final crlf v inspected |
2103 * | TRAILERS -----------> DONE |
2104 * | |
2105 * `----------------------------------------------'
2106 *
2107 * Compression only happens in the DATA state, and must be flushed in final
2108 * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding
2109 * is performed at once on final states for all bytes parsed, or when leaving
2110 * on missing data.
2111 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002112int http_response_forward_body(struct stream *s, struct channel *res, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +02002113{
2114 struct session *sess = s->sess;
2115 struct http_txn *txn = s->txn;
2116 struct http_msg *msg = &s->txn->rsp;
Christopher Faulet9768c262018-10-22 09:34:31 +02002117 struct htx *htx;
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002118 int ret;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002119
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002120 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002121
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002122 htx = htxbuf(&res->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002123
Christopher Fauletb9a92f32019-09-09 10:15:21 +02002124 if (htx->flags & HTX_FL_PARSING_ERROR)
2125 goto return_bad_res;
2126 if (htx->flags & HTX_FL_PROCESSING_ERROR)
2127 goto return_int_err;
2128
Christopher Faulete0768eb2018-10-03 16:38:02 +02002129 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Christopher Fauletf2824e62018-10-01 12:12:37 +02002130 ((res->flags & CF_SHUTW) && (res->to_forward || co_data(res)))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02002131 /* Output closed while we were sending data. We must abort and
2132 * wake the other side up.
2133 */
Christopher Faulete0768eb2018-10-03 16:38:02 +02002134 msg->msg_state = HTTP_MSG_ERROR;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002135 http_end_response(s);
2136 http_end_request(s);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002137 DBG_TRACE_DEVEL("leaving on error",
2138 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002139 return 1;
2140 }
2141
Christopher Faulet9768c262018-10-22 09:34:31 +02002142 if (msg->msg_state == HTTP_MSG_BODY)
2143 msg->msg_state = HTTP_MSG_DATA;
2144
Christopher Faulete0768eb2018-10-03 16:38:02 +02002145 /* in most states, we should abort in case of early close */
2146 channel_auto_close(res);
2147
Christopher Faulete0768eb2018-10-03 16:38:02 +02002148 if (res->to_forward) {
Christopher Faulet66af0b22019-03-22 14:54:52 +01002149 if (res->to_forward == CHN_INFINITE_FORWARD) {
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002150 if (res->flags & CF_EOI)
2151 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet66af0b22019-03-22 14:54:52 +01002152 }
2153 else {
2154 /* We can't process the buffer's contents yet */
2155 res->flags |= CF_WAKE_WRITE;
2156 goto missing_data_or_waiting;
2157 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02002158 }
2159
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002160 if (msg->msg_state >= HTTP_MSG_ENDING)
2161 goto ending;
2162
Christopher Fauletc75668e2020-12-07 18:10:32 +01002163 if ((txn->meth == HTTP_METH_CONNECT && txn->status >= 200 && txn->status < 300) || txn->status == 101 ||
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002164 (!(msg->flags & HTTP_MSGF_XFER_LEN) && !HAS_RSP_DATA_FILTERS(s))) {
2165 msg->msg_state = HTTP_MSG_ENDING;
2166 goto ending;
2167 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002168
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002169 /* Forward input data. We get it by removing all outgoing data not
2170 * forwarded yet from HTX data size. If there are some data filters, we
2171 * let them decide the amount of data to forward.
Christopher Faulet9768c262018-10-22 09:34:31 +02002172 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002173 if (HAS_RSP_DATA_FILTERS(s)) {
2174 ret = flt_http_payload(s, msg, htx->data);
2175 if (ret < 0)
2176 goto return_bad_res;
Christopher Faulet421e7692019-06-13 11:16:45 +02002177 c_adv(res, ret);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002178 }
2179 else {
Christopher Faulet421e7692019-06-13 11:16:45 +02002180 c_adv(res, htx->data - co_data(res));
Christopher Faulet66af0b22019-03-22 14:54:52 +01002181 if (msg->flags & HTTP_MSGF_XFER_LEN)
2182 channel_htx_forward_forever(res, htx);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002183 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002184
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002185 if (htx->data != co_data(res))
2186 goto missing_data_or_waiting;
2187
2188 if (!(msg->flags & HTTP_MSGF_XFER_LEN) && res->flags & CF_SHUTR) {
2189 msg->msg_state = HTTP_MSG_ENDING;
2190 goto ending;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002191 }
2192
Christopher Faulet9768c262018-10-22 09:34:31 +02002193 /* Check if the end-of-message is reached and if so, switch the message
Christopher Fauletd20fdb02019-06-13 16:43:22 +02002194 * in HTTP_MSG_ENDING state. Then if all data was marked to be
2195 * forwarded, set the state to HTTP_MSG_DONE.
Christopher Faulet9768c262018-10-22 09:34:31 +02002196 */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002197 if (!(htx->flags & HTX_FL_EOM))
Christopher Faulet9768c262018-10-22 09:34:31 +02002198 goto missing_data_or_waiting;
2199
Christopher Fauletd20fdb02019-06-13 16:43:22 +02002200 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet9768c262018-10-22 09:34:31 +02002201
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002202 ending:
Christopher Faulet2151cdd2020-07-22 16:34:59 +02002203 res->flags &= ~CF_EXPECT_MORE; /* no more data are expected */
2204
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002205 /* other states, ENDING...TUNNEL */
2206 if (msg->msg_state >= HTTP_MSG_DONE)
2207 goto done;
Christopher Faulet9768c262018-10-22 09:34:31 +02002208
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002209 if (HAS_RSP_DATA_FILTERS(s)) {
2210 ret = flt_http_end(s, msg);
2211 if (ret <= 0) {
2212 if (!ret)
2213 goto missing_data_or_waiting;
2214 goto return_bad_res;
2215 }
2216 }
2217
Christopher Fauletc75668e2020-12-07 18:10:32 +01002218 if ((txn->meth == HTTP_METH_CONNECT && txn->status >= 200 && txn->status < 300) || txn->status == 101 ||
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002219 !(msg->flags & HTTP_MSGF_XFER_LEN)) {
2220 msg->msg_state = HTTP_MSG_TUNNEL;
2221 goto ending;
2222 }
2223 else {
2224 msg->msg_state = HTTP_MSG_DONE;
2225 res->to_forward = 0;
2226 }
2227
2228 done:
2229
2230 channel_dont_close(res);
2231
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002232 http_end_response(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002233 if (!(res->analysers & an_bit)) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002234 http_end_request(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002235 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
2236 if (res->flags & CF_SHUTW) {
2237 /* response errors are most likely due to the
2238 * client aborting the transfer. */
Christopher Faulet93e02d82019-03-08 14:18:50 +01002239 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002240 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02002241 goto return_bad_res;
2242 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002243 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002244 return 1;
2245 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002246 DBG_TRACE_DEVEL("waiting for the end of the HTTP txn",
2247 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002248 return 0;
2249
2250 missing_data_or_waiting:
2251 if (res->flags & CF_SHUTW)
Christopher Faulet93e02d82019-03-08 14:18:50 +01002252 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002253
2254 /* stop waiting for data if the input is closed before the end. If the
2255 * client side was already closed, it means that the client has aborted,
2256 * so we don't want to count this as a server abort. Otherwise it's a
2257 * server abort.
2258 */
Christopher Fauletd20fdb02019-06-13 16:43:22 +02002259 if (msg->msg_state < HTTP_MSG_ENDING && res->flags & CF_SHUTR) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02002260 if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))
Christopher Faulet93e02d82019-03-08 14:18:50 +01002261 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002262 /* If we have some pending data, we continue the processing */
Christopher Faulet93e02d82019-03-08 14:18:50 +01002263 if (htx_is_empty(htx))
2264 goto return_srv_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002265 }
2266
Christopher Faulete0768eb2018-10-03 16:38:02 +02002267 /* When TE: chunked is used, we need to get there again to parse
2268 * remaining chunks even if the server has closed, so we don't want to
Christopher Faulet9768c262018-10-22 09:34:31 +02002269 * set CF_DONTCLOSE. Similarly when there is a content-leng or if there
2270 * are filters registered on the stream, we don't want to forward a
2271 * close
Christopher Faulete0768eb2018-10-03 16:38:02 +02002272 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002273 if ((msg->flags & HTTP_MSGF_XFER_LEN) || HAS_RSP_DATA_FILTERS(s))
Christopher Faulete0768eb2018-10-03 16:38:02 +02002274 channel_dont_close(res);
2275
2276 /* We know that more data are expected, but we couldn't send more that
2277 * what we did. So we always set the CF_EXPECT_MORE flag so that the
2278 * system knows it must not set a PUSH on this first part. Interactive
2279 * modes are already handled by the stream sock layer. We must not do
2280 * this in content-length mode because it could present the MSG_MORE
2281 * flag with the last block of forwarded data, which would cause an
2282 * additional delay to be observed by the receiver.
2283 */
Christopher Faulet2151cdd2020-07-22 16:34:59 +02002284 if (HAS_RSP_DATA_FILTERS(s))
Christopher Faulete0768eb2018-10-03 16:38:02 +02002285 res->flags |= CF_EXPECT_MORE;
2286
2287 /* the stream handler will take care of timeouts and errors */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002288 DBG_TRACE_DEVEL("waiting for more data to forward",
2289 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002290 return 0;
2291
Christopher Faulet93e02d82019-03-08 14:18:50 +01002292 return_srv_abort:
Willy Tarreau4781b152021-04-06 13:53:36 +02002293 _HA_ATOMIC_INC(&sess->fe->fe_counters.srv_aborts);
2294 _HA_ATOMIC_INC(&s->be->be_counters.srv_aborts);
William Lallemand36119de2021-03-08 15:26:48 +01002295 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02002296 _HA_ATOMIC_INC(&sess->listener->counters->srv_aborts);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002297 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02002298 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.srv_aborts);
Willy Tarreau826f3ab2021-02-10 12:07:15 +01002299 stream_inc_http_fail_ctr(s);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002300 if (!(s->flags & SF_ERR_MASK))
2301 s->flags |= SF_ERR_SRVCL;
2302 goto return_error;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002303
Christopher Faulet93e02d82019-03-08 14:18:50 +01002304 return_cli_abort:
Willy Tarreau4781b152021-04-06 13:53:36 +02002305 _HA_ATOMIC_INC(&sess->fe->fe_counters.cli_aborts);
2306 _HA_ATOMIC_INC(&s->be->be_counters.cli_aborts);
William Lallemand36119de2021-03-08 15:26:48 +01002307 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02002308 _HA_ATOMIC_INC(&sess->listener->counters->cli_aborts);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002309 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02002310 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.cli_aborts);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002311 if (!(s->flags & SF_ERR_MASK))
2312 s->flags |= SF_ERR_CLICL;
2313 goto return_error;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002314
Christopher Fauletb9a92f32019-09-09 10:15:21 +02002315 return_int_err:
Willy Tarreau4781b152021-04-06 13:53:36 +02002316 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
2317 _HA_ATOMIC_INC(&s->be->be_counters.internal_errors);
William Lallemand36119de2021-03-08 15:26:48 +01002318 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02002319 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002320 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02002321 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.internal_errors);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02002322 if (!(s->flags & SF_ERR_MASK))
2323 s->flags |= SF_ERR_INTERNAL;
2324 goto return_error;
2325
Christopher Faulet93e02d82019-03-08 14:18:50 +01002326 return_bad_res:
Willy Tarreau4781b152021-04-06 13:53:36 +02002327 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002328 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02002329 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002330 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_RSP);
2331 }
Willy Tarreau826f3ab2021-02-10 12:07:15 +01002332 stream_inc_http_fail_ctr(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002333 if (!(s->flags & SF_ERR_MASK))
Christopher Faulet93e02d82019-03-08 14:18:50 +01002334 s->flags |= SF_ERR_SRVCL;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002335 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +02002336
Christopher Faulet93e02d82019-03-08 14:18:50 +01002337 return_error:
Christopher Faulete0768eb2018-10-03 16:38:02 +02002338 /* don't send any error message as we're in the body */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002339 http_reply_and_close(s, txn->status, NULL);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002340 if (!(s->flags & SF_FINST_MASK))
2341 s->flags |= SF_FINST_D;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002342 DBG_TRACE_DEVEL("leaving on error",
2343 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002344 return 0;
2345}
2346
Christopher Fauletf2824e62018-10-01 12:12:37 +02002347/* Perform an HTTP redirect based on the information in <rule>. The function
Willy Tarreaubc1223b2021-09-02 16:54:33 +02002348 * returns zero in case of an irrecoverable error such as too large a request
2349 * to build a valid response, 1 in case of successful redirect (hence the rule
2350 * is final), or 2 if the rule has to be silently skipped.
Christopher Fauletf2824e62018-10-01 12:12:37 +02002351 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002352int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn)
Christopher Fauletf2824e62018-10-01 12:12:37 +02002353{
Christopher Faulet99daf282018-11-28 22:58:13 +01002354 struct channel *req = &s->req;
2355 struct channel *res = &s->res;
2356 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01002357 struct htx_sl *sl;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002358 struct buffer *chunk;
Christopher Faulet99daf282018-11-28 22:58:13 +01002359 struct ist status, reason, location;
2360 unsigned int flags;
Christopher Fauleteab17572022-04-26 20:34:38 +02002361 int ret = 1, close = 0; /* Try to keep the connection alive byt default */
Christopher Fauletf2824e62018-10-01 12:12:37 +02002362
2363 chunk = alloc_trash_chunk();
Christopher Fauletb8a53712019-12-16 11:29:38 +01002364 if (!chunk) {
2365 if (!(s->flags & SF_ERR_MASK))
2366 s->flags |= SF_ERR_RESOURCE;
Christopher Faulet99daf282018-11-28 22:58:13 +01002367 goto fail;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002368 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002369
Christopher Faulet99daf282018-11-28 22:58:13 +01002370 /*
2371 * Create the location
2372 */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002373 htx = htxbuf(&req->buf);
Christopher Fauletf2824e62018-10-01 12:12:37 +02002374 switch(rule->type) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002375 case REDIRECT_TYPE_SCHEME: {
2376 struct http_hdr_ctx ctx;
2377 struct ist path, host;
Amaury Denoyellec453f952021-07-06 11:40:12 +02002378 struct http_uri_parser parser;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002379
Christopher Faulet99daf282018-11-28 22:58:13 +01002380 host = ist("");
2381 ctx.blk = NULL;
2382 if (http_find_header(htx, ist("Host"), &ctx, 0))
2383 host = ctx.value;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002384
Christopher Faulet297fbb42019-05-13 14:41:27 +02002385 sl = http_get_stline(htx);
Amaury Denoyellec453f952021-07-06 11:40:12 +02002386 parser = http_uri_parser_init(htx_sl_req_uri(sl));
2387 path = http_parse_path(&parser);
Christopher Faulet99daf282018-11-28 22:58:13 +01002388 /* build message using path */
Tim Duesterhused526372020-03-05 17:56:33 +01002389 if (isttest(path)) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002390 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2391 int qs = 0;
2392 while (qs < path.len) {
2393 if (*(path.ptr + qs) == '?') {
2394 path.len = qs;
2395 break;
2396 }
2397 qs++;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002398 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002399 }
2400 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002401 else
2402 path = ist("/");
Christopher Fauletf2824e62018-10-01 12:12:37 +02002403
Christopher Faulet99daf282018-11-28 22:58:13 +01002404 if (rule->rdr_str) { /* this is an old "redirect" rule */
2405 /* add scheme */
2406 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2407 goto fail;
2408 }
2409 else {
2410 /* add scheme with executing log format */
2411 chunk->data += build_logline(s, chunk->area + chunk->data,
2412 chunk->size - chunk->data,
2413 &rule->rdr_fmt);
2414 }
2415 /* add "://" + host + path */
2416 if (!chunk_memcat(chunk, "://", 3) ||
2417 !chunk_memcat(chunk, host.ptr, host.len) ||
2418 !chunk_memcat(chunk, path.ptr, path.len))
2419 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002420
Christopher Faulet99daf282018-11-28 22:58:13 +01002421 /* append a slash at the end of the location if needed and missing */
2422 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
2423 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
2424 if (chunk->data + 1 >= chunk->size)
2425 goto fail;
2426 chunk->area[chunk->data++] = '/';
2427 }
2428 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002429 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002430
Christopher Faulet99daf282018-11-28 22:58:13 +01002431 case REDIRECT_TYPE_PREFIX: {
2432 struct ist path;
Amaury Denoyellec453f952021-07-06 11:40:12 +02002433 struct http_uri_parser parser;
Christopher Faulet99daf282018-11-28 22:58:13 +01002434
Christopher Faulet297fbb42019-05-13 14:41:27 +02002435 sl = http_get_stline(htx);
Amaury Denoyellec453f952021-07-06 11:40:12 +02002436 parser = http_uri_parser_init(htx_sl_req_uri(sl));
2437 path = http_parse_path(&parser);
Christopher Faulet99daf282018-11-28 22:58:13 +01002438 /* build message using path */
Tim Duesterhused526372020-03-05 17:56:33 +01002439 if (isttest(path)) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002440 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2441 int qs = 0;
2442 while (qs < path.len) {
2443 if (*(path.ptr + qs) == '?') {
2444 path.len = qs;
2445 break;
2446 }
2447 qs++;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002448 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002449 }
2450 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002451 else
2452 path = ist("/");
Christopher Fauletf2824e62018-10-01 12:12:37 +02002453
Christopher Faulet99daf282018-11-28 22:58:13 +01002454 if (rule->rdr_str) { /* this is an old "redirect" rule */
2455 /* add prefix. Note that if prefix == "/", we don't want to
2456 * add anything, otherwise it makes it hard for the user to
2457 * configure a self-redirection.
2458 */
2459 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
2460 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2461 goto fail;
2462 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002463 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002464 else {
2465 /* add prefix with executing log format */
2466 chunk->data += build_logline(s, chunk->area + chunk->data,
2467 chunk->size - chunk->data,
2468 &rule->rdr_fmt);
2469 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002470
Christopher Faulet99daf282018-11-28 22:58:13 +01002471 /* add path */
2472 if (!chunk_memcat(chunk, path.ptr, path.len))
2473 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002474
Christopher Faulet99daf282018-11-28 22:58:13 +01002475 /* append a slash at the end of the location if needed and missing */
2476 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
2477 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
2478 if (chunk->data + 1 >= chunk->size)
2479 goto fail;
2480 chunk->area[chunk->data++] = '/';
2481 }
2482 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002483 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002484 case REDIRECT_TYPE_LOCATION:
2485 default:
2486 if (rule->rdr_str) { /* this is an old "redirect" rule */
2487 /* add location */
2488 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2489 goto fail;
2490 }
2491 else {
2492 /* add location with executing log format */
Willy Tarreaubc1223b2021-09-02 16:54:33 +02002493 int len = build_logline(s, chunk->area + chunk->data,
2494 chunk->size - chunk->data,
2495 &rule->rdr_fmt);
Christopher Fauleteab17572022-04-26 20:34:38 +02002496 if (!len && rule->flags & REDIRECT_FLAG_IGNORE_EMPTY) {
2497 ret = 2;
2498 goto out;
2499 }
Willy Tarreaubc1223b2021-09-02 16:54:33 +02002500
2501 chunk->data += len;
Christopher Faulet99daf282018-11-28 22:58:13 +01002502 }
2503 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002504 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002505 location = ist2(chunk->area, chunk->data);
2506
2507 /*
2508 * Create the 30x response
2509 */
2510 switch (rule->code) {
2511 case 308:
2512 status = ist("308");
2513 reason = ist("Permanent Redirect");
2514 break;
2515 case 307:
2516 status = ist("307");
2517 reason = ist("Temporary Redirect");
2518 break;
2519 case 303:
2520 status = ist("303");
2521 reason = ist("See Other");
2522 break;
2523 case 301:
2524 status = ist("301");
2525 reason = ist("Moved Permanently");
2526 break;
2527 case 302:
2528 default:
2529 status = ist("302");
2530 reason = ist("Found");
2531 break;
2532 }
2533
Christopher Faulet08e66462019-05-23 16:44:59 +02002534 if (!(txn->req.flags & HTTP_MSGF_BODYLESS) && txn->req.msg_state != HTTP_MSG_DONE)
2535 close = 1;
2536
Christopher Faulet99daf282018-11-28 22:58:13 +01002537 htx = htx_from_buf(&res->buf);
Kevin Zhu96b36392020-01-07 09:42:55 +01002538 /* Trim any possible response */
2539 channel_htx_truncate(&s->res, htx);
Christopher Faulet99daf282018-11-28 22:58:13 +01002540 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
2541 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), status, reason);
2542 if (!sl)
2543 goto fail;
2544 sl->info.res.status = rule->code;
2545 s->txn->status = rule->code;
2546
Christopher Faulet08e66462019-05-23 16:44:59 +02002547 if (close && !htx_add_header(htx, ist("Connection"), ist("close")))
2548 goto fail;
2549
2550 if (!htx_add_header(htx, ist("Content-length"), ist("0")) ||
Christopher Faulet99daf282018-11-28 22:58:13 +01002551 !htx_add_header(htx, ist("Location"), location))
2552 goto fail;
2553
2554 if (rule->code == 302 || rule->code == 303 || rule->code == 307) {
2555 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")))
2556 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002557 }
2558
2559 if (rule->cookie_len) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002560 if (!htx_add_header(htx, ist("Set-Cookie"), ist2(rule->cookie_str, rule->cookie_len)))
2561 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002562 }
2563
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002564 if (!htx_add_endof(htx, HTX_BLK_EOH))
Christopher Faulet99daf282018-11-28 22:58:13 +01002565 goto fail;
2566
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002567 htx->flags |= HTX_FL_EOM;
Kevin Zhu96b36392020-01-07 09:42:55 +01002568 htx_to_buf(htx, &res->buf);
Christopher Fauleta72a7e42020-01-28 09:28:11 +01002569 if (!http_forward_proxy_resp(s, 1))
2570 goto fail;
Christopher Faulet99daf282018-11-28 22:58:13 +01002571
Christopher Faulet60b33a52020-01-28 09:18:10 +01002572 if (rule->flags & REDIRECT_FLAG_FROM_REQ) {
2573 /* let's log the request time */
2574 s->logs.tv_request = now;
Christopher Fauletd3475882021-10-04 14:16:46 +02002575 req->analysers &= AN_REQ_FLT_END;
Christopher Faulet99daf282018-11-28 22:58:13 +01002576
Christopher Faulet60b33a52020-01-28 09:18:10 +01002577 if (s->sess->fe == s->be) /* report it if the request was intercepted by the frontend */
Willy Tarreau4781b152021-04-06 13:53:36 +02002578 _HA_ATOMIC_INC(&s->sess->fe->fe_counters.intercepted_req);
Christopher Faulet60b33a52020-01-28 09:18:10 +01002579 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002580
2581 if (!(s->flags & SF_ERR_MASK))
2582 s->flags |= SF_ERR_LOCAL;
Christopher Faulet4a66c942023-01-13 09:43:21 +01002583 http_set_term_flags(s);
Christopher Fauletf2824e62018-10-01 12:12:37 +02002584
Christopher Fauleteab17572022-04-26 20:34:38 +02002585 out:
Christopher Faulet99daf282018-11-28 22:58:13 +01002586 free_trash_chunk(chunk);
Christopher Fauleteab17572022-04-26 20:34:38 +02002587 return ret;
Christopher Faulet99daf282018-11-28 22:58:13 +01002588
2589 fail:
2590 /* If an error occurred, remove the incomplete HTTP response from the
2591 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01002592 channel_htx_truncate(res, htxbuf(&res->buf));
Christopher Fauleteab17572022-04-26 20:34:38 +02002593 ret = 0;
2594 goto out;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002595}
2596
Christopher Faulet18c13d32022-05-16 11:43:10 +02002597/* This function filters the request header names to only allow [0-9a-zA-Z-]
2598 * characters. Depending on the proxy configuration, headers with a name not
2599 * matching this charset are removed or the request is rejected with a
2600 * 403-Forbidden response if such name are found. It returns HTTP_RULE_RES_CONT
2601 * to continue the request processing or HTTP_RULE_RES_DENY if the request is
2602 * rejected.
2603 */
2604static enum rule_result http_req_restrict_header_names(struct stream *s, struct htx *htx, struct proxy *px)
2605{
2606 struct htx_blk *blk;
2607 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
2608
2609 blk = htx_get_first_blk(htx);
2610 while (blk) {
2611 enum htx_blk_type type = htx_get_blk_type(blk);
2612
2613 if (type == HTX_BLK_HDR) {
2614 struct ist n = htx_get_blk_name(htx, blk);
Mateusz Malek4b85a962022-08-17 14:22:09 +02002615 int i, end = istlen(n);
Christopher Faulet18c13d32022-05-16 11:43:10 +02002616
Mateusz Malek4b85a962022-08-17 14:22:09 +02002617 for (i = 0; i < end; i++) {
Christopher Faulet18c13d32022-05-16 11:43:10 +02002618 if (!isalnum((unsigned char)n.ptr[i]) && n.ptr[i] != '-') {
Mateusz Malek4b85a962022-08-17 14:22:09 +02002619 break;
Christopher Faulet18c13d32022-05-16 11:43:10 +02002620 }
2621 }
Mateusz Malek4b85a962022-08-17 14:22:09 +02002622
2623 if (i < end) {
2624 /* Disallowed character found - block the request or remove the header */
2625 if (px->options2 & PR_O2_RSTRICT_REQ_HDR_NAMES_BLK)
2626 goto block;
2627 blk = htx_remove_blk(htx, blk);
2628 continue;
2629 }
Christopher Faulet18c13d32022-05-16 11:43:10 +02002630 }
2631 if (type == HTX_BLK_EOH)
2632 break;
2633
2634 blk = htx_get_next_blk(htx, blk);
2635 }
2636 out:
2637 return rule_ret;
2638 block:
2639 /* Block the request returning a 403-Forbidden response */
2640 s->txn->status = 403;
2641 rule_ret = HTTP_RULE_RES_DENY;
2642 goto out;
2643}
2644
Christopher Faulet92d34fe2019-12-17 09:20:34 +01002645/* Replace all headers matching the name <name>. The header value is replaced if
2646 * it matches the regex <re>. <str> is used for the replacement. If <full> is
2647 * set to 1, the full-line is matched and replaced. Otherwise, comma-separated
2648 * values are evaluated one by one. It returns 0 on success and -1 on error.
2649 */
2650int http_replace_hdrs(struct stream* s, struct htx *htx, struct ist name,
2651 const char *str, struct my_regex *re, int full)
Christopher Faulet72333522018-10-24 11:25:02 +02002652{
2653 struct http_hdr_ctx ctx;
2654 struct buffer *output = get_trash_chunk();
2655
Christopher Faulet72333522018-10-24 11:25:02 +02002656 ctx.blk = NULL;
Christopher Faulet92d34fe2019-12-17 09:20:34 +01002657 while (http_find_header(htx, name, &ctx, full)) {
Christopher Faulet72333522018-10-24 11:25:02 +02002658 if (!regex_exec_match2(re, ctx.value.ptr, ctx.value.len, MAX_MATCH, pmatch, 0))
2659 continue;
2660
2661 output->data = exp_replace(output->area, output->size, ctx.value.ptr, str, pmatch);
2662 if (output->data == -1)
2663 return -1;
2664 if (!http_replace_header_value(htx, &ctx, ist2(output->area, output->data)))
2665 return -1;
2666 }
2667 return 0;
2668}
2669
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002670/* This function executes one of the set-{method,path,query,uri} actions. It
2671 * takes the string from the variable 'replace' with length 'len', then modifies
2672 * the relevant part of the request line accordingly. Then it updates various
2673 * pointers to the next elements which were moved, and the total buffer length.
2674 * It finds the action to be performed in p[2], previously filled by function
2675 * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal
2676 * error, though this can be revisited when this code is finally exploited.
2677 *
2678 * 'action' can be '0' to replace method, '1' to replace path, '2' to replace
Christopher Faulet312294f2020-09-02 17:17:44 +02002679 * query string, 3 to replace uri or 4 to replace the path+query.
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002680 *
2681 * In query string case, the mark question '?' must be set at the start of the
2682 * string by the caller, event if the replacement query string is empty.
2683 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002684int http_req_replace_stline(int action, const char *replace, int len,
2685 struct proxy *px, struct stream *s)
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002686{
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002687 struct htx *htx = htxbuf(&s->req.buf);
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002688
2689 switch (action) {
2690 case 0: // method
2691 if (!http_replace_req_meth(htx, ist2(replace, len)))
2692 return -1;
2693 break;
2694
2695 case 1: // path
Christopher Fauletb8ce5052020-08-31 16:11:57 +02002696 if (!http_replace_req_path(htx, ist2(replace, len), 0))
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002697 return -1;
2698 break;
2699
2700 case 2: // query
2701 if (!http_replace_req_query(htx, ist2(replace, len)))
2702 return -1;
2703 break;
2704
2705 case 3: // uri
2706 if (!http_replace_req_uri(htx, ist2(replace, len)))
2707 return -1;
2708 break;
2709
Christopher Faulet312294f2020-09-02 17:17:44 +02002710 case 4: // path + query
2711 if (!http_replace_req_path(htx, ist2(replace, len), 1))
2712 return -1;
2713 break;
2714
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002715 default:
2716 return -1;
2717 }
2718 return 0;
2719}
2720
2721/* This function replace the HTTP status code and the associated message. The
Christopher Faulete00d06c2019-12-16 17:18:42 +01002722 * variable <status> contains the new status code. This function never fails. It
2723 * returns 0 in case of success, -1 in case of internal error.
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002724 */
Christopher Faulet96bff762019-12-17 13:46:18 +01002725int http_res_set_status(unsigned int status, struct ist reason, struct stream *s)
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002726{
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002727 struct htx *htx = htxbuf(&s->res.buf);
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002728 char *res;
2729
2730 chunk_reset(&trash);
2731 res = ultoa_o(status, trash.area, trash.size);
2732 trash.data = res - trash.area;
2733
2734 /* Do we have a custom reason format string? */
Tim Duesterhuse296d3e2020-03-05 17:56:31 +01002735 if (!isttest(reason)) {
Christopher Faulet96bff762019-12-17 13:46:18 +01002736 const char *str = http_get_reason(status);
Tim Duesterhusdcf753a2021-03-04 17:31:47 +01002737 reason = ist(str);
Christopher Faulet96bff762019-12-17 13:46:18 +01002738 }
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002739
Christopher Fauletbde2c4c2020-08-31 16:43:34 +02002740 if (!http_replace_res_status(htx, ist2(trash.area, trash.data), reason))
Christopher Faulete00d06c2019-12-16 17:18:42 +01002741 return -1;
2742 return 0;
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002743}
2744
Christopher Faulet3e964192018-10-24 11:39:23 +02002745/* Executes the http-request rules <rules> for stream <s>, proxy <px> and
2746 * transaction <txn>. Returns the verdict of the first rule that prevents
2747 * further processing of the request (auth, deny, ...), and defaults to
2748 * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or
2749 * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT
2750 * on txn->flags if it encounters a tarpit rule. If <deny_status> is not NULL
2751 * and a deny/tarpit rule is matched, it will be filled with this rule's deny
2752 * status.
2753 */
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002754static enum rule_result http_req_get_intercept_rule(struct proxy *px, struct list *def_rules,
2755 struct list *rules, struct stream *s)
Christopher Faulet3e964192018-10-24 11:39:23 +02002756{
2757 struct session *sess = strm_sess(s);
2758 struct http_txn *txn = s->txn;
Christopher Faulet3e964192018-10-24 11:39:23 +02002759 struct act_rule *rule;
Christopher Faulet3e964192018-10-24 11:39:23 +02002760 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002761 int act_opts = 0;
Christopher Faulet3e964192018-10-24 11:39:23 +02002762
Christopher Faulet3e964192018-10-24 11:39:23 +02002763 /* If "the current_rule_list" match the executed rule list, we are in
2764 * resume condition. If a resume is needed it is always in the action
2765 * and never in the ACL or converters. In this case, we initialise the
2766 * current rule, and go to the action execution point.
2767 */
2768 if (s->current_rule) {
2769 rule = s->current_rule;
2770 s->current_rule = NULL;
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002771 if (s->current_rule_list == rules || (def_rules && s->current_rule_list == def_rules))
Christopher Faulet3e964192018-10-24 11:39:23 +02002772 goto resume_execution;
2773 }
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002774 s->current_rule_list = ((!def_rules || s->current_rule_list == def_rules) ? rules : def_rules);
Christopher Faulet3e964192018-10-24 11:39:23 +02002775
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002776 restart:
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002777 /* start the ruleset evaluation in strict mode */
2778 txn->req.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01002779
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002780 list_for_each_entry(rule, s->current_rule_list, list) {
Christopher Faulet3e964192018-10-24 11:39:23 +02002781 /* check optional condition */
2782 if (rule->cond) {
2783 int ret;
2784
2785 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
2786 ret = acl_pass(ret);
2787
2788 if (rule->cond->pol == ACL_COND_UNLESS)
2789 ret = !ret;
2790
2791 if (!ret) /* condition not matched */
2792 continue;
2793 }
2794
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002795 act_opts |= ACT_OPT_FIRST;
Christopher Faulet3e964192018-10-24 11:39:23 +02002796 resume_execution:
Amaury Denoyelle03517732021-05-07 14:25:01 +02002797 if (rule->kw->flags & KWF_EXPERIMENTAL)
2798 mark_tainted(TAINTED_ACTION_EXP_EXECUTED);
2799
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002800 /* Always call the action function if defined */
2801 if (rule->action_ptr) {
2802 if ((s->req.flags & CF_READ_ERROR) ||
Christopher Faulet446d8032022-12-12 07:53:18 +01002803 ((s->req.flags & CF_SHUTR) &&
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002804 (px->options & PR_O_ABRT_CLOSE)))
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002805 act_opts |= ACT_OPT_FINAL;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002806
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002807 switch (rule->action_ptr(rule, px, sess, s, act_opts)) {
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002808 case ACT_RET_CONT:
2809 break;
2810 case ACT_RET_STOP:
2811 rule_ret = HTTP_RULE_RES_STOP;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002812 s->last_rule_file = rule->conf.file;
2813 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002814 goto end;
2815 case ACT_RET_YIELD:
2816 s->current_rule = rule;
2817 rule_ret = HTTP_RULE_RES_YIELD;
2818 goto end;
2819 case ACT_RET_ERR:
2820 rule_ret = HTTP_RULE_RES_ERROR;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002821 s->last_rule_file = rule->conf.file;
2822 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002823 goto end;
2824 case ACT_RET_DONE:
2825 rule_ret = HTTP_RULE_RES_DONE;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002826 s->last_rule_file = rule->conf.file;
2827 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002828 goto end;
2829 case ACT_RET_DENY:
Christopher Fauletb58f62b2020-01-13 16:40:13 +01002830 if (txn->status == -1)
2831 txn->status = 403;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002832 rule_ret = HTTP_RULE_RES_DENY;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002833 s->last_rule_file = rule->conf.file;
2834 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002835 goto end;
2836 case ACT_RET_ABRT:
2837 rule_ret = HTTP_RULE_RES_ABRT;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002838 s->last_rule_file = rule->conf.file;
2839 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002840 goto end;
2841 case ACT_RET_INV:
2842 rule_ret = HTTP_RULE_RES_BADREQ;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002843 s->last_rule_file = rule->conf.file;
2844 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002845 goto end;
2846 }
2847 continue; /* eval the next rule */
2848 }
2849
2850 /* If not action function defined, check for known actions */
Christopher Faulet3e964192018-10-24 11:39:23 +02002851 switch (rule->action) {
2852 case ACT_ACTION_ALLOW:
2853 rule_ret = HTTP_RULE_RES_STOP;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002854 s->last_rule_file = rule->conf.file;
2855 s->last_rule_line = rule->conf.line;
Christopher Faulet3e964192018-10-24 11:39:23 +02002856 goto end;
2857
2858 case ACT_ACTION_DENY:
Christopher Faulet5cb513a2020-05-13 17:56:56 +02002859 txn->status = rule->arg.http_reply->status;
2860 txn->http_reply = rule->arg.http_reply;
Christopher Faulet3e964192018-10-24 11:39:23 +02002861 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 Faulet3e964192018-10-24 11:39:23 +02002864 goto end;
2865
2866 case ACT_HTTP_REQ_TARPIT:
2867 txn->flags |= TX_CLTARPIT;
Christopher Faulet5cb513a2020-05-13 17:56:56 +02002868 txn->status = rule->arg.http_reply->status;
2869 txn->http_reply = rule->arg.http_reply;
Christopher Faulet3e964192018-10-24 11:39:23 +02002870 rule_ret = HTTP_RULE_RES_DENY;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002871 s->last_rule_file = rule->conf.file;
2872 s->last_rule_line = rule->conf.line;
Christopher Faulet3e964192018-10-24 11:39:23 +02002873 goto end;
2874
Willy Tarreaubc1223b2021-09-02 16:54:33 +02002875 case ACT_HTTP_REDIR: {
2876 int ret = http_apply_redirect_rule(rule->arg.redir, s, txn);
2877
2878 if (ret == 2) // 2 == skip
2879 break;
2880
2881 rule_ret = ret ? HTTP_RULE_RES_ABRT : HTTP_RULE_RES_ERROR;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002882 s->last_rule_file = rule->conf.file;
2883 s->last_rule_line = rule->conf.line;
Christopher Faulet3e964192018-10-24 11:39:23 +02002884 goto end;
Willy Tarreaubc1223b2021-09-02 16:54:33 +02002885 }
Christopher Faulet3e964192018-10-24 11:39:23 +02002886
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002887 /* other flags exists, but normally, they never be matched. */
Christopher Faulet3e964192018-10-24 11:39:23 +02002888 default:
2889 break;
2890 }
2891 }
2892
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002893 if (def_rules && s->current_rule_list == def_rules) {
2894 s->current_rule_list = rules;
2895 goto restart;
2896 }
2897
Christopher Faulet3e964192018-10-24 11:39:23 +02002898 end:
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002899 /* if the ruleset evaluation is finished reset the strict mode */
Christopher Faulet46f95542019-12-20 10:07:22 +01002900 if (rule_ret != HTTP_RULE_RES_YIELD)
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002901 txn->req.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01002902
Christopher Faulet3e964192018-10-24 11:39:23 +02002903 /* we reached the end of the rules, nothing to report */
2904 return rule_ret;
2905}
2906
2907/* Executes the http-response rules <rules> for stream <s> and proxy <px>. It
2908 * returns one of 5 possible statuses: HTTP_RULE_RES_CONT, HTTP_RULE_RES_STOP,
2909 * HTTP_RULE_RES_DONE, HTTP_RULE_RES_YIELD, or HTTP_RULE_RES_BADREQ. If *CONT
2910 * is returned, the process can continue the evaluation of next rule list. If
2911 * *STOP or *DONE is returned, the process must stop the evaluation. If *BADREQ
2912 * is returned, it means the operation could not be processed and a server error
Christopher Fauleta53abad2020-05-13 08:12:22 +02002913 * must be returned. If *YIELD is returned, the caller must call again the
2914 * function with the same context.
Christopher Faulet3e964192018-10-24 11:39:23 +02002915 */
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002916static enum rule_result http_res_get_intercept_rule(struct proxy *px, struct list *def_rules,
2917 struct list *rules, struct stream *s)
Christopher Faulet3e964192018-10-24 11:39:23 +02002918{
2919 struct session *sess = strm_sess(s);
2920 struct http_txn *txn = s->txn;
Christopher Faulet3e964192018-10-24 11:39:23 +02002921 struct act_rule *rule;
Christopher Faulet3e964192018-10-24 11:39:23 +02002922 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002923 int act_opts = 0;
Christopher Faulet3e964192018-10-24 11:39:23 +02002924
Christopher Faulet3e964192018-10-24 11:39:23 +02002925 /* If "the current_rule_list" match the executed rule list, we are in
2926 * resume condition. If a resume is needed it is always in the action
2927 * and never in the ACL or converters. In this case, we initialise the
2928 * current rule, and go to the action execution point.
2929 */
2930 if (s->current_rule) {
2931 rule = s->current_rule;
2932 s->current_rule = NULL;
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002933 if (s->current_rule_list == rules || (def_rules && s->current_rule_list == def_rules))
Christopher Faulet3e964192018-10-24 11:39:23 +02002934 goto resume_execution;
2935 }
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002936 s->current_rule_list = ((!def_rules || s->current_rule_list == def_rules) ? rules : def_rules);
2937
2938 restart:
Christopher Faulet3e964192018-10-24 11:39:23 +02002939
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002940 /* start the ruleset evaluation in strict mode */
2941 txn->rsp.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01002942
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002943 list_for_each_entry(rule, s->current_rule_list, list) {
Christopher Faulet3e964192018-10-24 11:39:23 +02002944 /* check optional condition */
2945 if (rule->cond) {
2946 int ret;
2947
2948 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
2949 ret = acl_pass(ret);
2950
2951 if (rule->cond->pol == ACL_COND_UNLESS)
2952 ret = !ret;
2953
2954 if (!ret) /* condition not matched */
2955 continue;
2956 }
2957
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002958 act_opts |= ACT_OPT_FIRST;
Christopher Faulet3e964192018-10-24 11:39:23 +02002959resume_execution:
Amaury Denoyelle03517732021-05-07 14:25:01 +02002960 if (rule->kw->flags & KWF_EXPERIMENTAL)
2961 mark_tainted(TAINTED_ACTION_EXP_EXECUTED);
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002962
2963 /* Always call the action function if defined */
2964 if (rule->action_ptr) {
2965 if ((s->req.flags & CF_READ_ERROR) ||
Christopher Faulet446d8032022-12-12 07:53:18 +01002966 ((s->req.flags & CF_SHUTR) &&
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002967 (px->options & PR_O_ABRT_CLOSE)))
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002968 act_opts |= ACT_OPT_FINAL;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002969
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002970 switch (rule->action_ptr(rule, px, sess, s, act_opts)) {
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002971 case ACT_RET_CONT:
2972 break;
2973 case ACT_RET_STOP:
2974 rule_ret = HTTP_RULE_RES_STOP;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002975 s->last_rule_file = rule->conf.file;
2976 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002977 goto end;
2978 case ACT_RET_YIELD:
2979 s->current_rule = rule;
2980 rule_ret = HTTP_RULE_RES_YIELD;
2981 goto end;
2982 case ACT_RET_ERR:
2983 rule_ret = HTTP_RULE_RES_ERROR;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002984 s->last_rule_file = rule->conf.file;
2985 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002986 goto end;
2987 case ACT_RET_DONE:
2988 rule_ret = HTTP_RULE_RES_DONE;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002989 s->last_rule_file = rule->conf.file;
2990 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002991 goto end;
2992 case ACT_RET_DENY:
Christopher Fauletb58f62b2020-01-13 16:40:13 +01002993 if (txn->status == -1)
2994 txn->status = 502;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002995 rule_ret = HTTP_RULE_RES_DENY;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002996 s->last_rule_file = rule->conf.file;
2997 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002998 goto end;
2999 case ACT_RET_ABRT:
3000 rule_ret = HTTP_RULE_RES_ABRT;
Willy Tarreauc6dae862022-03-09 17:23:10 +01003001 s->last_rule_file = rule->conf.file;
3002 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01003003 goto end;
3004 case ACT_RET_INV:
3005 rule_ret = HTTP_RULE_RES_BADREQ;
Willy Tarreauc6dae862022-03-09 17:23:10 +01003006 s->last_rule_file = rule->conf.file;
3007 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01003008 goto end;
3009 }
3010 continue; /* eval the next rule */
3011 }
3012
3013 /* If not action function defined, check for known actions */
Christopher Faulet3e964192018-10-24 11:39:23 +02003014 switch (rule->action) {
3015 case ACT_ACTION_ALLOW:
3016 rule_ret = HTTP_RULE_RES_STOP; /* "allow" rules are OK */
Willy Tarreauc6dae862022-03-09 17:23:10 +01003017 s->last_rule_file = rule->conf.file;
3018 s->last_rule_line = rule->conf.line;
Christopher Faulet3e964192018-10-24 11:39:23 +02003019 goto end;
3020
3021 case ACT_ACTION_DENY:
Christopher Faulet5cb513a2020-05-13 17:56:56 +02003022 txn->status = rule->arg.http_reply->status;
3023 txn->http_reply = rule->arg.http_reply;
Christopher Faulet3a26bee2019-12-16 12:47:40 +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 Faulet3e964192018-10-24 11:39:23 +02003027 goto end;
3028
Willy Tarreaubc1223b2021-09-02 16:54:33 +02003029 case ACT_HTTP_REDIR: {
3030 int ret = http_apply_redirect_rule(rule->arg.redir, s, txn);
Christopher Faulet3e964192018-10-24 11:39:23 +02003031
Willy Tarreaubc1223b2021-09-02 16:54:33 +02003032 if (ret == 2) // 2 == skip
3033 break;
3034
3035 rule_ret = ret ? HTTP_RULE_RES_ABRT : HTTP_RULE_RES_ERROR;
Willy Tarreauc6dae862022-03-09 17:23:10 +01003036 s->last_rule_file = rule->conf.file;
3037 s->last_rule_line = rule->conf.line;
Willy Tarreaubc1223b2021-09-02 16:54:33 +02003038 goto end;
3039 }
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01003040 /* other flags exists, but normally, they never be matched. */
Christopher Faulet3e964192018-10-24 11:39:23 +02003041 default:
3042 break;
3043 }
3044 }
3045
Christopher Fauletd4150ad2021-10-13 15:35:55 +02003046 if (def_rules && s->current_rule_list == def_rules) {
3047 s->current_rule_list = rules;
3048 goto restart;
3049 }
3050
Christopher Faulet3e964192018-10-24 11:39:23 +02003051 end:
Christopher Faulet1aea50e2020-01-17 16:03:53 +01003052 /* if the ruleset evaluation is finished reset the strict mode */
Christopher Faulet46f95542019-12-20 10:07:22 +01003053 if (rule_ret != HTTP_RULE_RES_YIELD)
Christopher Faulet1aea50e2020-01-17 16:03:53 +01003054 txn->rsp.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01003055
Christopher Faulet3e964192018-10-24 11:39:23 +02003056 /* we reached the end of the rules, nothing to report */
3057 return rule_ret;
3058}
3059
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01003060/* Executes backend and frontend http-after-response rules for the stream <s>,
3061 * in that order. it return 1 on success and 0 on error. It is the caller
3062 * responsibility to catch error or ignore it. If it catches it, this function
3063 * may be called a second time, for the internal error.
3064 */
3065int http_eval_after_res_rules(struct stream *s)
3066{
Christopher Fauletd4150ad2021-10-13 15:35:55 +02003067 struct list *def_rules, *rules;
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01003068 struct session *sess = s->sess;
3069 enum rule_result ret = HTTP_RULE_RES_CONT;
3070
Christopher Faulet507479b2020-05-15 12:29:46 +02003071 /* Eval after-response ruleset only if the reply is not const */
3072 if (s->txn->flags & TX_CONST_REPLY)
3073 goto end;
3074
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01003075 /* prune the request variables if not already done and swap to the response variables. */
3076 if (s->vars_reqres.scope != SCOPE_RES) {
3077 if (!LIST_ISEMPTY(&s->vars_reqres.head))
3078 vars_prune(&s->vars_reqres, s->sess, s);
Willy Tarreaub7bfcb32021-08-31 08:13:25 +02003079 vars_init_head(&s->vars_reqres, SCOPE_RES);
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01003080 }
3081
Christopher Fauletd4150ad2021-10-13 15:35:55 +02003082 def_rules = (s->be->defpx ? &s->be->defpx->http_after_res_rules : NULL);
3083 rules = &s->be->http_after_res_rules;
3084
3085 ret = http_res_get_intercept_rule(s->be, def_rules, rules, s);
Christopher Faulet4c5a5912021-11-09 17:48:39 +01003086 if ((ret == HTTP_RULE_RES_CONT || ret == HTTP_RULE_RES_STOP) && sess->fe != s->be) {
Christopher Fauletd4150ad2021-10-13 15:35:55 +02003087 def_rules = ((sess->fe->defpx && sess->fe->defpx != s->be->defpx) ? &sess->fe->defpx->http_after_res_rules : NULL);
3088 rules = &sess->fe->http_after_res_rules;
3089 ret = http_res_get_intercept_rule(sess->fe, def_rules, rules, s);
3090 }
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01003091
Christopher Faulet507479b2020-05-15 12:29:46 +02003092 end:
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01003093 /* All other codes than CONTINUE, STOP or DONE are forbidden */
3094 return (ret == HTTP_RULE_RES_CONT || ret == HTTP_RULE_RES_STOP || ret == HTTP_RULE_RES_DONE);
3095}
3096
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003097/*
3098 * Manage client-side cookie. It can impact performance by about 2% so it is
3099 * desirable to call it only when needed. This code is quite complex because
3100 * of the multiple very crappy and ambiguous syntaxes we have to support. it
3101 * highly recommended not to touch this part without a good reason !
3102 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003103static void http_manage_client_side_cookies(struct stream *s, struct channel *req)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003104{
3105 struct session *sess = s->sess;
3106 struct http_txn *txn = s->txn;
3107 struct htx *htx;
3108 struct http_hdr_ctx ctx;
3109 char *hdr_beg, *hdr_end, *del_from;
3110 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
3111 int preserve_hdr;
3112
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003113 htx = htxbuf(&req->buf);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003114 ctx.blk = NULL;
3115 while (http_find_header(htx, ist("Cookie"), &ctx, 1)) {
Olivier Houchardf0f42382019-07-22 17:43:46 +02003116 int is_first = 1;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003117 del_from = NULL; /* nothing to be deleted */
3118 preserve_hdr = 0; /* assume we may kill the whole header */
3119
3120 /* Now look for cookies. Conforming to RFC2109, we have to support
3121 * attributes whose name begin with a '$', and associate them with
3122 * the right cookie, if we want to delete this cookie.
3123 * So there are 3 cases for each cookie read :
3124 * 1) it's a special attribute, beginning with a '$' : ignore it.
3125 * 2) it's a server id cookie that we *MAY* want to delete : save
3126 * some pointers on it (last semi-colon, beginning of cookie...)
3127 * 3) it's an application cookie : we *MAY* have to delete a previous
3128 * "special" cookie.
3129 * At the end of loop, if a "special" cookie remains, we may have to
3130 * remove it. If no application cookie persists in the header, we
3131 * *MUST* delete it.
3132 *
3133 * Note: RFC2965 is unclear about the processing of spaces around
3134 * the equal sign in the ATTR=VALUE form. A careful inspection of
3135 * the RFC explicitly allows spaces before it, and not within the
3136 * tokens (attrs or values). An inspection of RFC2109 allows that
3137 * too but section 10.1.3 lets one think that spaces may be allowed
3138 * after the equal sign too, resulting in some (rare) buggy
3139 * implementations trying to do that. So let's do what servers do.
3140 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
3141 * allowed quoted strings in values, with any possible character
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003142 * after a backslash, including control chars and delimiters, which
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003143 * causes parsing to become ambiguous. Browsers also allow spaces
3144 * within values even without quotes.
3145 *
3146 * We have to keep multiple pointers in order to support cookie
3147 * removal at the beginning, middle or end of header without
3148 * corrupting the header. All of these headers are valid :
3149 *
3150 * hdr_beg hdr_end
3151 * | |
3152 * v |
3153 * NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3 |
3154 * NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3 v
3155 * NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3
3156 * | | | | | | |
3157 * | | | | | | |
3158 * | | | | | | +--> next
3159 * | | | | | +----> val_end
3160 * | | | | +-----------> val_beg
3161 * | | | +--------------> equal
3162 * | | +----------------> att_end
3163 * | +---------------------> att_beg
3164 * +--------------------------> prev
3165 *
3166 */
3167 hdr_beg = ctx.value.ptr;
3168 hdr_end = hdr_beg + ctx.value.len;
3169 for (prev = hdr_beg; prev < hdr_end; prev = next) {
3170 /* Iterate through all cookies on this line */
3171
3172 /* find att_beg */
3173 att_beg = prev;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003174 if (!is_first)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003175 att_beg++;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003176 is_first = 0;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003177
3178 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
3179 att_beg++;
3180
3181 /* find att_end : this is the first character after the last non
3182 * space before the equal. It may be equal to hdr_end.
3183 */
3184 equal = att_end = att_beg;
3185 while (equal < hdr_end) {
3186 if (*equal == '=' || *equal == ',' || *equal == ';')
3187 break;
3188 if (HTTP_IS_SPHT(*equal++))
3189 continue;
3190 att_end = equal;
3191 }
3192
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003193 /* here, <equal> points to '=', a delimiter or the end. <att_end>
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003194 * is between <att_beg> and <equal>, both may be identical.
3195 */
3196 /* look for end of cookie if there is an equal sign */
3197 if (equal < hdr_end && *equal == '=') {
3198 /* look for the beginning of the value */
3199 val_beg = equal + 1;
3200 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
3201 val_beg++;
3202
3203 /* find the end of the value, respecting quotes */
3204 next = http_find_cookie_value_end(val_beg, hdr_end);
3205
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003206 /* make val_end point to the first white space or delimiter after the value */
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003207 val_end = next;
3208 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
3209 val_end--;
3210 }
3211 else
3212 val_beg = val_end = next = equal;
3213
3214 /* We have nothing to do with attributes beginning with
3215 * '$'. However, they will automatically be removed if a
3216 * header before them is removed, since they're supposed
3217 * to be linked together.
3218 */
3219 if (*att_beg == '$')
3220 continue;
3221
3222 /* Ignore cookies with no equal sign */
3223 if (equal == next) {
3224 /* This is not our cookie, so we must preserve it. But if we already
3225 * scheduled another cookie for removal, we cannot remove the
3226 * complete header, but we can remove the previous block itself.
3227 */
3228 preserve_hdr = 1;
3229 if (del_from != NULL) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003230 int delta = http_del_hdr_value(hdr_beg, hdr_end, &del_from, prev);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003231 val_end += delta;
3232 next += delta;
3233 hdr_end += delta;
3234 prev = del_from;
3235 del_from = NULL;
3236 }
3237 continue;
3238 }
3239
3240 /* if there are spaces around the equal sign, we need to
3241 * strip them otherwise we'll get trouble for cookie captures,
3242 * or even for rewrites. Since this happens extremely rarely,
3243 * it does not hurt performance.
3244 */
3245 if (unlikely(att_end != equal || val_beg > equal + 1)) {
3246 int stripped_before = 0;
3247 int stripped_after = 0;
3248
3249 if (att_end != equal) {
3250 memmove(att_end, equal, hdr_end - equal);
3251 stripped_before = (att_end - equal);
3252 equal += stripped_before;
3253 val_beg += stripped_before;
3254 }
3255
3256 if (val_beg > equal + 1) {
3257 memmove(equal + 1, val_beg, hdr_end + stripped_before - val_beg);
3258 stripped_after = (equal + 1) - val_beg;
3259 val_beg += stripped_after;
3260 stripped_before += stripped_after;
3261 }
3262
3263 val_end += stripped_before;
3264 next += stripped_before;
3265 hdr_end += stripped_before;
3266 }
3267 /* now everything is as on the diagram above */
3268
3269 /* First, let's see if we want to capture this cookie. We check
3270 * that we don't already have a client side cookie, because we
3271 * can only capture one. Also as an optimisation, we ignore
3272 * cookies shorter than the declared name.
3273 */
3274 if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL &&
3275 (val_end - att_beg >= sess->fe->capture_namelen) &&
3276 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
3277 int log_len = val_end - att_beg;
3278
3279 if ((txn->cli_cookie = pool_alloc(pool_head_capture)) == NULL) {
3280 ha_alert("HTTP logging : out of memory.\n");
3281 } else {
3282 if (log_len > sess->fe->capture_len)
3283 log_len = sess->fe->capture_len;
3284 memcpy(txn->cli_cookie, att_beg, log_len);
3285 txn->cli_cookie[log_len] = 0;
3286 }
3287 }
3288
3289 /* Persistence cookies in passive, rewrite or insert mode have the
3290 * following form :
3291 *
3292 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
3293 *
3294 * For cookies in prefix mode, the form is :
3295 *
3296 * Cookie: NAME=SRV~VALUE
3297 */
3298 if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
3299 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
3300 struct server *srv = s->be->srv;
3301 char *delim;
3302
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003303 /* if we're in cookie prefix mode, we'll search the delimiter so that we
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003304 * have the server ID between val_beg and delim, and the original cookie between
3305 * delim+1 and val_end. Otherwise, delim==val_end :
3306 *
3307 * hdr_beg
3308 * |
3309 * v
3310 * NAME=SRV; # in all but prefix modes
3311 * NAME=SRV~OPAQUE ; # in prefix mode
3312 * || || | |+-> next
3313 * || || | +--> val_end
3314 * || || +---------> delim
3315 * || |+------------> val_beg
3316 * || +-------------> att_end = equal
3317 * |+-----------------> att_beg
3318 * +------------------> prev
3319 *
3320 */
3321 if (s->be->ck_opts & PR_CK_PFX) {
3322 for (delim = val_beg; delim < val_end; delim++)
3323 if (*delim == COOKIE_DELIM)
3324 break;
3325 }
3326 else {
3327 char *vbar1;
3328 delim = val_end;
3329 /* Now check if the cookie contains a date field, which would
3330 * appear after a vertical bar ('|') just after the server name
3331 * and before the delimiter.
3332 */
3333 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
3334 if (vbar1) {
3335 /* OK, so left of the bar is the server's cookie and
3336 * right is the last seen date. It is a base64 encoded
3337 * 30-bit value representing the UNIX date since the
3338 * epoch in 4-second quantities.
3339 */
3340 int val;
3341 delim = vbar1++;
3342 if (val_end - vbar1 >= 5) {
3343 val = b64tos30(vbar1);
3344 if (val > 0)
3345 txn->cookie_last_date = val << 2;
3346 }
3347 /* look for a second vertical bar */
3348 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
3349 if (vbar1 && (val_end - vbar1 > 5)) {
3350 val = b64tos30(vbar1 + 1);
3351 if (val > 0)
3352 txn->cookie_first_date = val << 2;
3353 }
3354 }
3355 }
3356
3357 /* if the cookie has an expiration date and the proxy wants to check
3358 * it, then we do that now. We first check if the cookie is too old,
3359 * then only if it has expired. We detect strict overflow because the
3360 * time resolution here is not great (4 seconds). Cookies with dates
3361 * in the future are ignored if their offset is beyond one day. This
3362 * allows an admin to fix timezone issues without expiring everyone
3363 * and at the same time avoids keeping unwanted side effects for too
3364 * long.
3365 */
3366 if (txn->cookie_first_date && s->be->cookie_maxlife &&
3367 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) ||
3368 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
3369 txn->flags &= ~TX_CK_MASK;
3370 txn->flags |= TX_CK_OLD;
3371 delim = val_beg; // let's pretend we have not found the cookie
3372 txn->cookie_first_date = 0;
3373 txn->cookie_last_date = 0;
3374 }
3375 else if (txn->cookie_last_date && s->be->cookie_maxidle &&
3376 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) ||
3377 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
3378 txn->flags &= ~TX_CK_MASK;
3379 txn->flags |= TX_CK_EXPIRED;
3380 delim = val_beg; // let's pretend we have not found the cookie
3381 txn->cookie_first_date = 0;
3382 txn->cookie_last_date = 0;
3383 }
3384
3385 /* Here, we'll look for the first running server which supports the cookie.
3386 * This allows to share a same cookie between several servers, for example
3387 * to dedicate backup servers to specific servers only.
3388 * However, to prevent clients from sticking to cookie-less backup server
3389 * when they have incidentely learned an empty cookie, we simply ignore
3390 * empty cookies and mark them as invalid.
3391 * The same behaviour is applied when persistence must be ignored.
3392 */
3393 if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
3394 srv = NULL;
3395
3396 while (srv) {
3397 if (srv->cookie && (srv->cklen == delim - val_beg) &&
3398 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
3399 if ((srv->cur_state != SRV_ST_STOPPED) ||
3400 (s->be->options & PR_O_PERSIST) ||
3401 (s->flags & SF_FORCE_PRST)) {
3402 /* we found the server and we can use it */
3403 txn->flags &= ~TX_CK_MASK;
3404 txn->flags |= (srv->cur_state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
3405 s->flags |= SF_DIRECT | SF_ASSIGNED;
3406 s->target = &srv->obj_type;
3407 break;
3408 } else {
3409 /* we found a server, but it's down,
3410 * mark it as such and go on in case
3411 * another one is available.
3412 */
3413 txn->flags &= ~TX_CK_MASK;
3414 txn->flags |= TX_CK_DOWN;
3415 }
3416 }
3417 srv = srv->next;
3418 }
3419
3420 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
3421 /* no server matched this cookie or we deliberately skipped it */
3422 txn->flags &= ~TX_CK_MASK;
3423 if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
3424 txn->flags |= TX_CK_UNUSED;
3425 else
3426 txn->flags |= TX_CK_INVALID;
3427 }
3428
3429 /* depending on the cookie mode, we may have to either :
3430 * - delete the complete cookie if we're in insert+indirect mode, so that
3431 * the server never sees it ;
3432 * - remove the server id from the cookie value, and tag the cookie as an
Joseph Herlante9d5c722018-11-25 11:00:25 -08003433 * application cookie so that it does not get accidentally removed later,
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003434 * if we're in cookie prefix mode
3435 */
3436 if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
3437 int delta; /* negative */
3438
3439 memmove(val_beg, delim + 1, hdr_end - (delim + 1));
3440 delta = val_beg - (delim + 1);
3441 val_end += delta;
3442 next += delta;
3443 hdr_end += delta;
3444 del_from = NULL;
3445 preserve_hdr = 1; /* we want to keep this cookie */
3446 }
3447 else if (del_from == NULL &&
3448 (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
3449 del_from = prev;
3450 }
3451 }
3452 else {
3453 /* This is not our cookie, so we must preserve it. But if we already
3454 * scheduled another cookie for removal, we cannot remove the
3455 * complete header, but we can remove the previous block itself.
3456 */
3457 preserve_hdr = 1;
3458
3459 if (del_from != NULL) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003460 int delta = http_del_hdr_value(hdr_beg, hdr_end, &del_from, prev);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003461 if (att_beg >= del_from)
3462 att_beg += delta;
3463 if (att_end >= del_from)
3464 att_end += delta;
3465 val_beg += delta;
3466 val_end += delta;
3467 next += delta;
3468 hdr_end += delta;
3469 prev = del_from;
3470 del_from = NULL;
3471 }
3472 }
3473
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003474 } /* for each cookie */
3475
3476
3477 /* There are no more cookies on this line.
3478 * We may still have one (or several) marked for deletion at the
3479 * end of the line. We must do this now in two ways :
3480 * - if some cookies must be preserved, we only delete from the
3481 * mark to the end of line ;
3482 * - if nothing needs to be preserved, simply delete the whole header
3483 */
3484 if (del_from) {
3485 hdr_end = (preserve_hdr ? del_from : hdr_beg);
3486 }
3487 if ((hdr_end - hdr_beg) != ctx.value.len) {
Christopher Faulet3e2638e2019-06-18 09:49:16 +02003488 if (hdr_beg != hdr_end)
3489 htx_change_blk_value_len(htx, ctx.blk, hdr_end - hdr_beg);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003490 else
3491 http_remove_header(htx, &ctx);
3492 }
3493 } /* for each "Cookie header */
3494}
3495
3496/*
3497 * Manage server-side cookies. It can impact performance by about 2% so it is
3498 * desirable to call it only when needed. This function is also used when we
3499 * just need to know if there is a cookie (eg: for check-cache).
3500 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003501static void http_manage_server_side_cookies(struct stream *s, struct channel *res)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003502{
3503 struct session *sess = s->sess;
3504 struct http_txn *txn = s->txn;
3505 struct htx *htx;
3506 struct http_hdr_ctx ctx;
3507 struct server *srv;
3508 char *hdr_beg, *hdr_end;
3509 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003510
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003511 htx = htxbuf(&res->buf);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003512
3513 ctx.blk = NULL;
Willy Tarreauaa1909e2022-11-14 18:58:35 +01003514 while (http_find_header(htx, ist("Set-Cookie"), &ctx, 1)) {
Olivier Houchardf0f42382019-07-22 17:43:46 +02003515 int is_first = 1;
3516
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003517 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
3518 * <prev> points to the colon.
3519 */
3520 txn->flags |= TX_SCK_PRESENT;
3521
3522 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
3523 * check-cache is enabled) and we are not interested in checking
3524 * them. Warning, the cookie capture is declared in the frontend.
3525 */
3526 if (s->be->cookie_name == NULL && sess->fe->capture_name == NULL)
3527 break;
3528
3529 /* OK so now we know we have to process this response cookie.
3530 * The format of the Set-Cookie header is slightly different
3531 * from the format of the Cookie header in that it does not
3532 * support the comma as a cookie delimiter (thus the header
3533 * cannot be folded) because the Expires attribute described in
3534 * the original Netscape's spec may contain an unquoted date
3535 * with a comma inside. We have to live with this because
3536 * many browsers don't support Max-Age and some browsers don't
3537 * support quoted strings. However the Set-Cookie2 header is
Willy Tarreauaa1909e2022-11-14 18:58:35 +01003538 * clean but basically nobody supports it.
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003539 *
3540 * We have to keep multiple pointers in order to support cookie
3541 * removal at the beginning, middle or end of header without
3542 * corrupting the header (in case of set-cookie2). A special
3543 * pointer, <scav> points to the beginning of the set-cookie-av
3544 * fields after the first semi-colon. The <next> pointer points
3545 * either to the end of line (set-cookie) or next unquoted comma
3546 * (set-cookie2). All of these headers are valid :
3547 *
3548 * hdr_beg hdr_end
3549 * | |
3550 * v |
3551 * NAME1 = VALUE 1 ; Secure; Path="/" |
3552 * NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT v
3553 * NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT
3554 * NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard
3555 * | | | | | | | |
3556 * | | | | | | | +-> next
3557 * | | | | | | +------------> scav
3558 * | | | | | +--------------> val_end
3559 * | | | | +--------------------> val_beg
3560 * | | | +----------------------> equal
3561 * | | +------------------------> att_end
3562 * | +----------------------------> att_beg
3563 * +------------------------------> prev
3564 * -------------------------------> hdr_beg
3565 */
3566 hdr_beg = ctx.value.ptr;
3567 hdr_end = hdr_beg + ctx.value.len;
3568 for (prev = hdr_beg; prev < hdr_end; prev = next) {
3569
3570 /* Iterate through all cookies on this line */
3571
3572 /* find att_beg */
3573 att_beg = prev;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003574 if (!is_first)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003575 att_beg++;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003576 is_first = 0;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003577
3578 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
3579 att_beg++;
3580
3581 /* find att_end : this is the first character after the last non
3582 * space before the equal. It may be equal to hdr_end.
3583 */
3584 equal = att_end = att_beg;
3585
3586 while (equal < hdr_end) {
Willy Tarreauaa1909e2022-11-14 18:58:35 +01003587 if (*equal == '=' || *equal == ';')
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003588 break;
3589 if (HTTP_IS_SPHT(*equal++))
3590 continue;
3591 att_end = equal;
3592 }
3593
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003594 /* here, <equal> points to '=', a delimiter or the end. <att_end>
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003595 * is between <att_beg> and <equal>, both may be identical.
3596 */
3597
3598 /* look for end of cookie if there is an equal sign */
3599 if (equal < hdr_end && *equal == '=') {
3600 /* look for the beginning of the value */
3601 val_beg = equal + 1;
3602 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
3603 val_beg++;
3604
3605 /* find the end of the value, respecting quotes */
3606 next = http_find_cookie_value_end(val_beg, hdr_end);
3607
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003608 /* make val_end point to the first white space or delimiter after the value */
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003609 val_end = next;
3610 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
3611 val_end--;
3612 }
3613 else {
3614 /* <equal> points to next comma, semi-colon or EOL */
3615 val_beg = val_end = next = equal;
3616 }
3617
3618 if (next < hdr_end) {
Willy Tarreauaa1909e2022-11-14 18:58:35 +01003619 /* For Set-Cookie, since commas are permitted
3620 * in values, skip to the end.
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003621 */
Willy Tarreauaa1909e2022-11-14 18:58:35 +01003622 next = hdr_end;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003623 }
3624
3625 /* Now everything is as on the diagram above */
3626
3627 /* Ignore cookies with no equal sign */
3628 if (equal == val_end)
3629 continue;
3630
3631 /* If there are spaces around the equal sign, we need to
3632 * strip them otherwise we'll get trouble for cookie captures,
3633 * or even for rewrites. Since this happens extremely rarely,
3634 * it does not hurt performance.
3635 */
3636 if (unlikely(att_end != equal || val_beg > equal + 1)) {
3637 int stripped_before = 0;
3638 int stripped_after = 0;
3639
3640 if (att_end != equal) {
3641 memmove(att_end, equal, hdr_end - equal);
3642 stripped_before = (att_end - equal);
3643 equal += stripped_before;
3644 val_beg += stripped_before;
3645 }
3646
3647 if (val_beg > equal + 1) {
3648 memmove(equal + 1, val_beg, hdr_end + stripped_before - val_beg);
3649 stripped_after = (equal + 1) - val_beg;
3650 val_beg += stripped_after;
3651 stripped_before += stripped_after;
3652 }
3653
3654 val_end += stripped_before;
3655 next += stripped_before;
3656 hdr_end += stripped_before;
3657
Christopher Faulet3e2638e2019-06-18 09:49:16 +02003658 htx_change_blk_value_len(htx, ctx.blk, hdr_end - hdr_beg);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003659 ctx.value.len = hdr_end - hdr_beg;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003660 }
3661
3662 /* First, let's see if we want to capture this cookie. We check
3663 * that we don't already have a server side cookie, because we
3664 * can only capture one. Also as an optimisation, we ignore
3665 * cookies shorter than the declared name.
3666 */
3667 if (sess->fe->capture_name != NULL &&
3668 txn->srv_cookie == NULL &&
3669 (val_end - att_beg >= sess->fe->capture_namelen) &&
3670 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
3671 int log_len = val_end - att_beg;
3672 if ((txn->srv_cookie = pool_alloc(pool_head_capture)) == NULL) {
3673 ha_alert("HTTP logging : out of memory.\n");
3674 }
3675 else {
3676 if (log_len > sess->fe->capture_len)
3677 log_len = sess->fe->capture_len;
3678 memcpy(txn->srv_cookie, att_beg, log_len);
3679 txn->srv_cookie[log_len] = 0;
3680 }
3681 }
3682
3683 srv = objt_server(s->target);
3684 /* now check if we need to process it for persistence */
3685 if (!(s->flags & SF_IGNORE_PRST) &&
3686 (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
3687 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
3688 /* assume passive cookie by default */
3689 txn->flags &= ~TX_SCK_MASK;
3690 txn->flags |= TX_SCK_FOUND;
3691
3692 /* If the cookie is in insert mode on a known server, we'll delete
3693 * this occurrence because we'll insert another one later.
3694 * We'll delete it too if the "indirect" option is set and we're in
3695 * a direct access.
3696 */
3697 if (s->be->ck_opts & PR_CK_PSV) {
3698 /* The "preserve" flag was set, we don't want to touch the
3699 * server's cookie.
3700 */
3701 }
3702 else if ((srv && (s->be->ck_opts & PR_CK_INS)) ||
3703 ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) {
3704 /* this cookie must be deleted */
3705 if (prev == hdr_beg && next == hdr_end) {
3706 /* whole header */
3707 http_remove_header(htx, &ctx);
3708 /* note: while both invalid now, <next> and <hdr_end>
3709 * are still equal, so the for() will stop as expected.
3710 */
3711 } else {
3712 /* just remove the value */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003713 int delta = http_del_hdr_value(hdr_beg, hdr_end, &prev, next);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003714 next = prev;
3715 hdr_end += delta;
3716 }
3717 txn->flags &= ~TX_SCK_MASK;
3718 txn->flags |= TX_SCK_DELETED;
3719 /* and go on with next cookie */
3720 }
3721 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) {
3722 /* replace bytes val_beg->val_end with the cookie name associated
3723 * with this server since we know it.
3724 */
3725 int sliding, delta;
3726
3727 ctx.value = ist2(val_beg, val_end - val_beg);
3728 ctx.lws_before = ctx.lws_after = 0;
3729 http_replace_header_value(htx, &ctx, ist2(srv->cookie, srv->cklen));
3730 delta = srv->cklen - (val_end - val_beg);
3731 sliding = (ctx.value.ptr - val_beg);
3732 hdr_beg += sliding;
3733 val_beg += sliding;
3734 next += sliding + delta;
3735 hdr_end += sliding + delta;
3736
3737 txn->flags &= ~TX_SCK_MASK;
3738 txn->flags |= TX_SCK_REPLACED;
3739 }
3740 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) {
3741 /* insert the cookie name associated with this server
3742 * before existing cookie, and insert a delimiter between them..
3743 */
3744 int sliding, delta;
3745 ctx.value = ist2(val_beg, 0);
3746 ctx.lws_before = ctx.lws_after = 0;
3747 http_replace_header_value(htx, &ctx, ist2(srv->cookie, srv->cklen + 1));
3748 delta = srv->cklen + 1;
3749 sliding = (ctx.value.ptr - val_beg);
3750 hdr_beg += sliding;
3751 val_beg += sliding;
3752 next += sliding + delta;
3753 hdr_end += sliding + delta;
3754
3755 val_beg[srv->cklen] = COOKIE_DELIM;
3756 txn->flags &= ~TX_SCK_MASK;
3757 txn->flags |= TX_SCK_REPLACED;
3758 }
3759 }
3760 /* that's done for this cookie, check the next one on the same
Willy Tarreauaa1909e2022-11-14 18:58:35 +01003761 * line when next != hdr_end (which should normally not happen
3762 * with set-cookie2 support removed).
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003763 */
3764 }
3765 }
3766}
3767
Christopher Faulet25a02f62018-10-24 12:00:25 +02003768/*
3769 * Parses the Cache-Control and Pragma request header fields to determine if
3770 * the request may be served from the cache and/or if it is cacheable. Updates
3771 * s->txn->flags.
3772 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003773void http_check_request_for_cacheability(struct stream *s, struct channel *req)
Christopher Faulet25a02f62018-10-24 12:00:25 +02003774{
3775 struct http_txn *txn = s->txn;
3776 struct htx *htx;
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003777 struct http_hdr_ctx ctx = { .blk = NULL };
3778 int pragma_found, cc_found;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003779
3780 if ((txn->flags & (TX_CACHEABLE|TX_CACHE_IGNORE)) == TX_CACHE_IGNORE)
3781 return; /* nothing more to do here */
3782
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003783 htx = htxbuf(&req->buf);
Christopher Faulet25a02f62018-10-24 12:00:25 +02003784 pragma_found = cc_found = 0;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003785
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003786 /* Check "pragma" header for HTTP/1.0 compatibility. */
3787 if (http_find_header(htx, ist("pragma"), &ctx, 1)) {
3788 if (isteqi(ctx.value, ist("no-cache"))) {
3789 pragma_found = 1;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003790 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003791 }
Christopher Faulet25a02f62018-10-24 12:00:25 +02003792
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003793 ctx.blk = NULL;
3794 /* Don't use the cache and don't try to store if we found the
3795 * Authorization header */
3796 if (http_find_header(htx, ist("authorization"), &ctx, 1)) {
3797 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3798 txn->flags |= TX_CACHE_IGNORE;
3799 }
Christopher Faulet25a02f62018-10-24 12:00:25 +02003800
Christopher Faulet25a02f62018-10-24 12:00:25 +02003801
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003802 /* Look for "cache-control" header and iterate over all the values
3803 * until we find one that specifies that caching is possible or not. */
3804 ctx.blk = NULL;
3805 while (http_find_header(htx, ist("cache-control"), &ctx, 0)) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003806 cc_found = 1;
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003807 /* We don't check the values after max-age, max-stale nor min-fresh,
3808 * we simply don't use the cache when they're specified. */
3809 if (istmatchi(ctx.value, ist("max-age")) ||
3810 istmatchi(ctx.value, ist("no-cache")) ||
3811 istmatchi(ctx.value, ist("max-stale")) ||
3812 istmatchi(ctx.value, ist("min-fresh"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003813 txn->flags |= TX_CACHE_IGNORE;
3814 continue;
3815 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003816 if (istmatchi(ctx.value, ist("no-store"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003817 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3818 continue;
3819 }
3820 }
3821
3822 /* RFC7234#5.4:
3823 * When the Cache-Control header field is also present and
3824 * understood in a request, Pragma is ignored.
3825 * When the Cache-Control header field is not present in a
3826 * request, caches MUST consider the no-cache request
3827 * pragma-directive as having the same effect as if
3828 * "Cache-Control: no-cache" were present.
3829 */
3830 if (!cc_found && pragma_found)
3831 txn->flags |= TX_CACHE_IGNORE;
3832}
3833
3834/*
3835 * Check if response is cacheable or not. Updates s->txn->flags.
3836 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003837void http_check_response_for_cacheability(struct stream *s, struct channel *res)
Christopher Faulet25a02f62018-10-24 12:00:25 +02003838{
3839 struct http_txn *txn = s->txn;
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003840 struct http_hdr_ctx ctx = { .blk = NULL };
Christopher Faulet25a02f62018-10-24 12:00:25 +02003841 struct htx *htx;
Remi Tricot-Le Bretoncc9bf2e2020-11-12 11:14:41 +01003842 int has_freshness_info = 0;
3843 int has_validator = 0;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003844
3845 if (txn->status < 200) {
3846 /* do not try to cache interim responses! */
3847 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3848 return;
3849 }
3850
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003851 htx = htxbuf(&res->buf);
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003852 /* Check "pragma" header for HTTP/1.0 compatibility. */
3853 if (http_find_header(htx, ist("pragma"), &ctx, 1)) {
3854 if (isteqi(ctx.value, ist("no-cache"))) {
3855 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3856 return;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003857 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003858 }
Christopher Faulet25a02f62018-10-24 12:00:25 +02003859
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003860 /* Look for "cache-control" header and iterate over all the values
3861 * until we find one that specifies that caching is possible or not. */
3862 ctx.blk = NULL;
3863 while (http_find_header(htx, ist("cache-control"), &ctx, 0)) {
3864 if (isteqi(ctx.value, ist("public"))) {
3865 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003866 continue;
3867 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003868 if (isteqi(ctx.value, ist("private")) ||
3869 isteqi(ctx.value, ist("no-cache")) ||
3870 isteqi(ctx.value, ist("no-store")) ||
3871 isteqi(ctx.value, ist("max-age=0")) ||
3872 isteqi(ctx.value, ist("s-maxage=0"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003873 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003874 continue;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003875 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003876 /* We might have a no-cache="set-cookie" form. */
3877 if (istmatchi(ctx.value, ist("no-cache=\"set-cookie"))) {
3878 txn->flags &= ~TX_CACHE_COOK;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003879 continue;
3880 }
Remi Tricot-Le Bretoncc9bf2e2020-11-12 11:14:41 +01003881
3882 if (istmatchi(ctx.value, ist("s-maxage")) ||
3883 istmatchi(ctx.value, ist("max-age"))) {
3884 has_freshness_info = 1;
3885 continue;
3886 }
3887 }
3888
3889 /* If no freshness information could be found in Cache-Control values,
3890 * look for an Expires header. */
3891 if (!has_freshness_info) {
3892 ctx.blk = NULL;
3893 has_freshness_info = http_find_header(htx, ist("expires"), &ctx, 0);
Christopher Faulet25a02f62018-10-24 12:00:25 +02003894 }
Remi Tricot-Le Bretoncc9bf2e2020-11-12 11:14:41 +01003895
3896 /* If no freshness information could be found in Cache-Control or Expires
3897 * values, look for an explicit validator. */
3898 if (!has_freshness_info) {
3899 ctx.blk = NULL;
3900 has_validator = 1;
3901 if (!http_find_header(htx, ist("etag"), &ctx, 0)) {
3902 ctx.blk = NULL;
3903 if (!http_find_header(htx, ist("last-modified"), &ctx, 0))
3904 has_validator = 0;
3905 }
3906 }
3907
3908 /* We won't store an entry that has neither a cache validator nor an
3909 * explicit expiration time, as suggested in RFC 7234#3. */
3910 if (!has_freshness_info && !has_validator)
3911 txn->flags |= TX_CACHE_IGNORE;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003912}
3913
Christopher Faulet377c5a52018-10-24 21:21:30 +02003914/*
3915 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
3916 * for the current backend.
3917 *
3918 * It is assumed that the request is either a HEAD, GET, or POST and that the
3919 * uri_auth field is valid.
3920 *
3921 * Returns 1 if stats should be provided, otherwise 0.
3922 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003923static int http_stats_check_uri(struct stream *s, struct http_txn *txn, struct proxy *backend)
Christopher Faulet377c5a52018-10-24 21:21:30 +02003924{
3925 struct uri_auth *uri_auth = backend->uri_auth;
3926 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003927 struct htx_sl *sl;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003928 struct ist uri;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003929
3930 if (!uri_auth)
3931 return 0;
3932
3933 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
3934 return 0;
3935
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003936 htx = htxbuf(&s->req.buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02003937 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003938 uri = htx_sl_req_uri(sl);
Amaury Denoyellec453f952021-07-06 11:40:12 +02003939 if (*uri_auth->uri_prefix == '/') {
3940 struct http_uri_parser parser = http_uri_parser_init(uri);
3941 uri = http_parse_path(&parser);
3942 }
Christopher Faulet377c5a52018-10-24 21:21:30 +02003943
3944 /* check URI size */
3945 if (uri_auth->uri_len > uri.len)
3946 return 0;
3947
3948 if (memcmp(uri.ptr, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
3949 return 0;
3950
3951 return 1;
3952}
3953
3954/* This function prepares an applet to handle the stats. It can deal with the
3955 * "100-continue" expectation, check that admin rules are met for POST requests,
3956 * and program a response message if something was unexpected. It cannot fail
3957 * and always relies on the stats applet to complete the job. It does not touch
3958 * analysers nor counters, which are left to the caller. It does not touch
3959 * s->target which is supposed to already point to the stats applet. The caller
3960 * is expected to have already assigned an appctx to the stream.
3961 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003962static int http_handle_stats(struct stream *s, struct channel *req)
Christopher Faulet377c5a52018-10-24 21:21:30 +02003963{
3964 struct stats_admin_rule *stats_admin_rule;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003965 struct session *sess = s->sess;
3966 struct http_txn *txn = s->txn;
3967 struct http_msg *msg = &txn->req;
3968 struct uri_auth *uri_auth = s->be->uri_auth;
3969 const char *h, *lookup, *end;
Willy Tarreau8e7c6e62022-05-18 17:58:02 +02003970 struct appctx *appctx = __sc_appctx(s->scb);
Willy Tarreau91cefca2022-05-03 17:08:29 +02003971 struct show_stat_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx));
Christopher Faulet377c5a52018-10-24 21:21:30 +02003972 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003973 struct htx_sl *sl;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003974
Willy Tarreau41f88522022-05-03 18:39:27 +02003975 appctx->st1 = 0;
Willy Tarreau6ef16482022-05-06 18:07:53 +02003976 ctx->state = STAT_STATE_INIT;
Willy Tarreau91cefca2022-05-03 17:08:29 +02003977 ctx->st_code = STAT_STATUS_INIT;
3978 ctx->flags |= uri_auth->flags;
3979 ctx->flags |= STAT_FMT_HTML; /* assume HTML mode by default */
Christopher Faulet377c5a52018-10-24 21:21:30 +02003980 if ((msg->flags & HTTP_MSGF_VER_11) && (txn->meth != HTTP_METH_HEAD))
Willy Tarreau91cefca2022-05-03 17:08:29 +02003981 ctx->flags |= STAT_CHUNKED;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003982
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003983 htx = htxbuf(&req->buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02003984 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003985 lookup = HTX_SL_REQ_UPTR(sl) + uri_auth->uri_len;
3986 end = HTX_SL_REQ_UPTR(sl) + HTX_SL_REQ_ULEN(sl);
Christopher Faulet377c5a52018-10-24 21:21:30 +02003987
3988 for (h = lookup; h <= end - 3; h++) {
3989 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02003990 ctx->flags |= STAT_HIDE_DOWN;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003991 break;
3992 }
Amaury Denoyelle91e55ea2021-02-25 14:46:08 +01003993 }
3994
3995 for (h = lookup; h <= end - 9; h++) {
3996 if (memcmp(h, ";no-maint", 9) == 0) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02003997 ctx->flags |= STAT_HIDE_MAINT;
Willy Tarreau3e320362020-10-23 17:28:57 +02003998 break;
3999 }
Christopher Faulet377c5a52018-10-24 21:21:30 +02004000 }
4001
4002 if (uri_auth->refresh) {
4003 for (h = lookup; h <= end - 10; h++) {
4004 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02004005 ctx->flags |= STAT_NO_REFRESH;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004006 break;
4007 }
4008 }
4009 }
4010
4011 for (h = lookup; h <= end - 4; h++) {
4012 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02004013 ctx->flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
Christopher Faulet377c5a52018-10-24 21:21:30 +02004014 break;
4015 }
4016 }
4017
4018 for (h = lookup; h <= end - 6; h++) {
4019 if (memcmp(h, ";typed", 6) == 0) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02004020 ctx->flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
4021 ctx->flags |= STAT_FMT_TYPED;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004022 break;
4023 }
4024 }
4025
Christopher Faulet6338a082019-09-09 15:50:54 +02004026 for (h = lookup; h <= end - 5; h++) {
4027 if (memcmp(h, ";json", 5) == 0) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02004028 ctx->flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
4029 ctx->flags |= STAT_FMT_JSON;
Christopher Faulet6338a082019-09-09 15:50:54 +02004030 break;
4031 }
4032 }
4033
4034 for (h = lookup; h <= end - 12; h++) {
4035 if (memcmp(h, ";json-schema", 12) == 0) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02004036 ctx->flags &= ~STAT_FMT_MASK;
4037 ctx->flags |= STAT_JSON_SCHM;
Christopher Faulet6338a082019-09-09 15:50:54 +02004038 break;
4039 }
4040 }
4041
Christopher Faulet377c5a52018-10-24 21:21:30 +02004042 for (h = lookup; h <= end - 8; h++) {
4043 if (memcmp(h, ";st=", 4) == 0) {
4044 int i;
4045 h += 4;
Willy Tarreau91cefca2022-05-03 17:08:29 +02004046 ctx->st_code = STAT_STATUS_UNKN;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004047 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
4048 if (strncmp(stat_status_codes[i], h, 4) == 0) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02004049 ctx->st_code = i;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004050 break;
4051 }
4052 }
4053 break;
4054 }
4055 }
4056
Willy Tarreau91cefca2022-05-03 17:08:29 +02004057 ctx->scope_str = 0;
4058 ctx->scope_len = 0;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004059 for (h = lookup; h <= end - 8; h++) {
4060 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
4061 int itx = 0;
4062 const char *h2;
4063 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
4064 const char *err;
4065
4066 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
4067 h2 = h;
Willy Tarreau91cefca2022-05-03 17:08:29 +02004068 ctx->scope_str = h2 - HTX_SL_REQ_UPTR(sl);
Christopher Fauleted7a0662019-01-14 11:07:34 +01004069 while (h < end) {
Christopher Faulet377c5a52018-10-24 21:21:30 +02004070 if (*h == ';' || *h == '&' || *h == ' ')
4071 break;
4072 itx++;
4073 h++;
4074 }
4075
4076 if (itx > STAT_SCOPE_TXT_MAXLEN)
4077 itx = STAT_SCOPE_TXT_MAXLEN;
Willy Tarreau91cefca2022-05-03 17:08:29 +02004078 ctx->scope_len = itx;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004079
Willy Tarreau91cefca2022-05-03 17:08:29 +02004080 /* scope_txt = search query, ctx->scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Christopher Faulet377c5a52018-10-24 21:21:30 +02004081 memcpy(scope_txt, h2, itx);
4082 scope_txt[itx] = '\0';
4083 err = invalid_char(scope_txt);
4084 if (err) {
4085 /* bad char in search text => clear scope */
Willy Tarreau91cefca2022-05-03 17:08:29 +02004086 ctx->scope_str = 0;
4087 ctx->scope_len = 0;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004088 }
4089 break;
4090 }
4091 }
4092
4093 /* now check whether we have some admin rules for this request */
4094 list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) {
4095 int ret = 1;
4096
4097 if (stats_admin_rule->cond) {
4098 ret = acl_exec_cond(stats_admin_rule->cond, s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
4099 ret = acl_pass(ret);
4100 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
4101 ret = !ret;
4102 }
4103
4104 if (ret) {
4105 /* no rule, or the rule matches */
Willy Tarreau91cefca2022-05-03 17:08:29 +02004106 ctx->flags |= STAT_ADMIN;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004107 break;
4108 }
4109 }
4110
Christopher Faulet5d45e382019-02-27 15:15:23 +01004111 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
4112 appctx->st0 = STAT_HTTP_HEAD;
4113 else if (txn->meth == HTTP_METH_POST) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02004114 if (ctx->flags & STAT_ADMIN) {
Christopher Faulet377c5a52018-10-24 21:21:30 +02004115 appctx->st0 = STAT_HTTP_POST;
Christopher Fauletbd9e8422019-08-15 22:26:48 +02004116 if (msg->msg_state < HTTP_MSG_DATA)
4117 req->analysers |= AN_REQ_HTTP_BODY;
4118 }
Christopher Faulet377c5a52018-10-24 21:21:30 +02004119 else {
Christopher Faulet5d45e382019-02-27 15:15:23 +01004120 /* POST without admin level */
Willy Tarreau91cefca2022-05-03 17:08:29 +02004121 ctx->flags &= ~STAT_CHUNKED;
4122 ctx->st_code = STAT_STATUS_DENY;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004123 appctx->st0 = STAT_HTTP_LAST;
4124 }
4125 }
4126 else {
Christopher Faulet5d45e382019-02-27 15:15:23 +01004127 /* Unsupported method */
Willy Tarreau91cefca2022-05-03 17:08:29 +02004128 ctx->flags &= ~STAT_CHUNKED;
4129 ctx->st_code = STAT_STATUS_IVAL;
Christopher Faulet5d45e382019-02-27 15:15:23 +01004130 appctx->st0 = STAT_HTTP_LAST;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004131 }
4132
4133 s->task->nice = -32; /* small boost for HTTP statistics */
4134 return 1;
4135}
4136
Christopher Faulet021a8e42021-03-29 10:46:38 +02004137/* This function waits for the message payload at most <time> milliseconds (may
4138 * be set to TICK_ETERNITY). It stops to wait if at least <bytes> bytes of the
4139 * payload are received (0 means no limit). It returns HTTP_RULE_* depending on
4140 * the result:
4141 *
4142 * - HTTP_RULE_RES_CONT when conditions are met to stop waiting
4143 * - HTTP_RULE_RES_YIELD to wait for more data
Ilya Shipitsinb2be9a12021-04-24 13:25:42 +05004144 * - HTTP_RULE_RES_ABRT when a timeout occurred.
Christopher Faulet021a8e42021-03-29 10:46:38 +02004145 * - HTTP_RULE_RES_BADREQ if a parsing error is raised by lower level
Ilya Shipitsinb2be9a12021-04-24 13:25:42 +05004146 * - HTTP_RULE_RES_ERROR if an internal error occurred
Christopher Faulet021a8e42021-03-29 10:46:38 +02004147 *
Ilya Shipitsinb2be9a12021-04-24 13:25:42 +05004148 * If a timeout occurred, this function is responsible to emit the right response
Christopher Faulet021a8e42021-03-29 10:46:38 +02004149 * to the client, depending on the channel (408 on request side, 504 on response
4150 * side). All other errors must be handled by the caller.
4151 */
4152enum rule_result http_wait_for_msg_body(struct stream *s, struct channel *chn,
4153 unsigned int time, unsigned int bytes)
4154{
4155 struct session *sess = s->sess;
4156 struct http_txn *txn = s->txn;
4157 struct http_msg *msg = ((chn->flags & CF_ISRESP) ? &txn->rsp : &txn->req);
4158 struct htx *htx;
4159 enum rule_result ret = HTTP_RULE_RES_CONT;
4160
4161 htx = htxbuf(&chn->buf);
4162
4163 if (htx->flags & HTX_FL_PARSING_ERROR) {
4164 ret = HTTP_RULE_RES_BADREQ;
4165 goto end;
4166 }
4167 if (htx->flags & HTX_FL_PROCESSING_ERROR) {
4168 ret = HTTP_RULE_RES_ERROR;
4169 goto end;
4170 }
4171
4172 /* Do nothing for bodyless and CONNECT requests */
4173 if (txn->meth == HTTP_METH_CONNECT || (msg->flags & HTTP_MSGF_BODYLESS))
4174 goto end;
4175
4176 if (!(chn->flags & CF_ISRESP) && msg->msg_state < HTTP_MSG_DATA) {
4177 if (http_handle_expect_hdr(s, htx, msg) == -1) {
4178 ret = HTTP_RULE_RES_ERROR;
4179 goto end;
4180 }
4181 }
4182
4183 msg->msg_state = HTTP_MSG_DATA;
4184
4185 /* Now we're in HTTP_MSG_DATA. We just need to know if all data have
4186 * been received or if the buffer is full.
4187 */
Christopher Faulet78335962021-09-23 14:46:32 +02004188 if ((htx->flags & HTX_FL_EOM) ||
4189 htx_get_tail_type(htx) > HTX_BLK_DATA ||
4190 channel_htx_full(chn, htx, global.tune.maxrewrite) ||
Willy Tarreau99615ed2022-05-25 07:29:36 +02004191 sc_waiting_room(chn_prod(chn)))
Christopher Faulet021a8e42021-03-29 10:46:38 +02004192 goto end;
4193
4194 if (bytes) {
4195 struct htx_blk *blk;
4196 unsigned int len = 0;
4197
4198 for (blk = htx_get_first_blk(htx); blk; blk = htx_get_next_blk(htx, blk)) {
4199 if (htx_get_blk_type(blk) != HTX_BLK_DATA)
4200 continue;
4201 len += htx_get_blksz(blk);
4202 if (len >= bytes)
4203 goto end;
4204 }
4205 }
4206
4207 if ((chn->flags & CF_READ_TIMEOUT) || tick_is_expired(chn->analyse_exp, now_ms)) {
4208 if (!(chn->flags & CF_ISRESP))
4209 goto abort_req;
4210 goto abort_res;
4211 }
4212
4213 /* we get here if we need to wait for more data */
Christopher Faulet4b490b72023-01-04 11:55:15 +01004214 if (!(chn->flags & CF_SHUTR)) {
Christopher Faulet021a8e42021-03-29 10:46:38 +02004215 if (!tick_isset(chn->analyse_exp))
4216 chn->analyse_exp = tick_add_ifset(now_ms, time);
4217 ret = HTTP_RULE_RES_YIELD;
4218 }
4219
4220 end:
4221 return ret;
4222
Christopher Fauletf0d80df2023-01-13 10:20:20 +01004223 abort:
4224 http_reply_and_close(s, txn->status, http_error_message(s));
4225 ret = HTTP_RULE_RES_ABRT;
4226 goto end;
4227
Christopher Faulet021a8e42021-03-29 10:46:38 +02004228 abort_req:
4229 txn->status = 408;
4230 if (!(s->flags & SF_ERR_MASK))
4231 s->flags |= SF_ERR_CLITO;
Willy Tarreau4781b152021-04-06 13:53:36 +02004232 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req);
Christopher Faulet021a8e42021-03-29 10:46:38 +02004233 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02004234 _HA_ATOMIC_INC(&sess->listener->counters->failed_req);
Christopher Fauletf0d80df2023-01-13 10:20:20 +01004235 goto abort;
Christopher Faulet021a8e42021-03-29 10:46:38 +02004236
4237 abort_res:
4238 txn->status = 504;
4239 if (!(s->flags & SF_ERR_MASK))
4240 s->flags |= SF_ERR_SRVTO;
Christopher Faulet021a8e42021-03-29 10:46:38 +02004241 stream_inc_http_fail_ctr(s);
Christopher Fauletf0d80df2023-01-13 10:20:20 +01004242 goto abort;
Christopher Faulet021a8e42021-03-29 10:46:38 +02004243}
4244
Willy Tarreaub49672d2022-05-27 10:13:37 +02004245void http_perform_server_redirect(struct stream *s, struct stconn *sc)
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004246{
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004247 struct channel *req = &s->req;
4248 struct channel *res = &s->res;
4249 struct server *srv;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004250 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004251 struct htx_sl *sl;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004252 struct ist path, location;
4253 unsigned int flags;
Amaury Denoyellec453f952021-07-06 11:40:12 +02004254 struct http_uri_parser parser;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004255
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004256 /*
4257 * Create the location
4258 */
4259 chunk_reset(&trash);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004260
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004261 /* 1: add the server's prefix */
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004262 /* special prefix "/" means don't change URL */
4263 srv = __objt_server(s->target);
4264 if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') {
4265 if (!chunk_memcat(&trash, srv->rdr_pfx, srv->rdr_len))
4266 return;
4267 }
4268
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004269 /* 2: add the request Path */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004270 htx = htxbuf(&req->buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02004271 sl = http_get_stline(htx);
Amaury Denoyellec453f952021-07-06 11:40:12 +02004272 parser = http_uri_parser_init(htx_sl_req_uri(sl));
4273 path = http_parse_path(&parser);
Tim Duesterhused526372020-03-05 17:56:33 +01004274 if (!isttest(path))
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004275 return;
4276
4277 if (!chunk_memcat(&trash, path.ptr, path.len))
4278 return;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004279 location = ist2(trash.area, trash.data);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004280
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004281 /*
Ilya Shipitsin4a689da2022-10-29 09:34:32 +05004282 * Create the 302 response
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004283 */
4284 htx = htx_from_buf(&res->buf);
4285 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
4286 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
4287 ist("HTTP/1.1"), ist("302"), ist("Found"));
4288 if (!sl)
4289 goto fail;
4290 sl->info.res.status = 302;
4291 s->txn->status = 302;
4292
4293 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) ||
4294 !htx_add_header(htx, ist("Connection"), ist("close")) ||
4295 !htx_add_header(htx, ist("Content-length"), ist("0")) ||
4296 !htx_add_header(htx, ist("Location"), location))
4297 goto fail;
4298
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004299 if (!htx_add_endof(htx, HTX_BLK_EOH))
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004300 goto fail;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004301
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004302 htx->flags |= HTX_FL_EOM;
Christopher Fauletc20afb82020-01-24 19:16:26 +01004303 htx_to_buf(htx, &res->buf);
Christopher Fauleta72a7e42020-01-28 09:28:11 +01004304 if (!http_forward_proxy_resp(s, 1))
4305 goto fail;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004306
4307 /* return without error. */
Willy Tarreaub49672d2022-05-27 10:13:37 +02004308 sc_shutr(sc);
4309 sc_shutw(sc);
Christopher Faulet50264b42022-03-30 19:39:30 +02004310 s->conn_err_type = STRM_ET_NONE;
Willy Tarreaub49672d2022-05-27 10:13:37 +02004311 sc->state = SC_ST_CLO;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004312
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004313 if (!(s->flags & SF_ERR_MASK))
4314 s->flags |= SF_ERR_LOCAL;
4315 if (!(s->flags & SF_FINST_MASK))
4316 s->flags |= SF_FINST_C;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004317
4318 /* FIXME: we should increase a counter of redirects per server and per backend. */
4319 srv_inc_sess_ctr(srv);
4320 srv_set_sess_last(srv);
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004321 return;
4322
4323 fail:
4324 /* If an error occurred, remove the incomplete HTTP response from the
4325 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004326 channel_htx_truncate(res, htx);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004327}
4328
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05004329/* This function terminates the request because it was completely analyzed or
Christopher Fauletf2824e62018-10-01 12:12:37 +02004330 * because an error was triggered during the body forwarding.
4331 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004332static void http_end_request(struct stream *s)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004333{
4334 struct channel *chn = &s->req;
4335 struct http_txn *txn = s->txn;
4336
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004337 DBG_TRACE_ENTER(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004338
Christopher Fauletb42a8b62018-11-19 21:59:00 +01004339 if (unlikely(txn->req.msg_state == HTTP_MSG_ERROR ||
4340 txn->rsp.msg_state == HTTP_MSG_ERROR)) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004341 channel_abort(chn);
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004342 channel_htx_truncate(chn, htxbuf(&chn->buf));
Christopher Fauletf2824e62018-10-01 12:12:37 +02004343 goto end;
4344 }
4345
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004346 if (unlikely(txn->req.msg_state < HTTP_MSG_DONE)) {
4347 DBG_TRACE_DEVEL("waiting end of the request", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004348 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004349 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004350
4351 if (txn->req.msg_state == HTTP_MSG_DONE) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004352 /* No need to read anymore, the request was completely parsed.
4353 * We can shut the read side unless we want to abort_on_close,
4354 * or we have a POST request. The issue with POST requests is
4355 * that some browsers still send a CRLF after the request, and
4356 * this CRLF must be read so that it does not remain in the kernel
4357 * buffers, otherwise a close could cause an RST on some systems
4358 * (eg: Linux).
4359 */
Christopher Faulet769d0e92019-03-22 14:23:18 +01004360 if (!(s->be->options & PR_O_ABRT_CLOSE) && txn->meth != HTTP_METH_POST)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004361 channel_dont_read(chn);
4362
4363 /* if the server closes the connection, we want to immediately react
4364 * and close the socket to save packets and syscalls.
4365 */
Willy Tarreaucb041662022-05-17 19:44:42 +02004366 s->scb->flags |= SC_FL_NOHALF;
Christopher Fauletf2824e62018-10-01 12:12:37 +02004367
4368 /* In any case we've finished parsing the request so we must
4369 * disable Nagle when sending data because 1) we're not going
4370 * to shut this side, and 2) the server is waiting for us to
4371 * send pending data.
4372 */
4373 chn->flags |= CF_NEVER_WAIT;
4374
Christopher Fauletd01ce402019-01-02 17:44:13 +01004375 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
4376 /* The server has not finished to respond, so we
4377 * don't want to move in order not to upset it.
4378 */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004379 DBG_TRACE_DEVEL("waiting end of the response", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletd01ce402019-01-02 17:44:13 +01004380 return;
4381 }
4382
Christopher Fauletf2824e62018-10-01 12:12:37 +02004383 /* When we get here, it means that both the request and the
4384 * response have finished receiving. Depending on the connection
4385 * mode, we'll have to wait for the last bytes to leave in either
4386 * direction, and sometimes for a close to be effective.
4387 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02004388 if (txn->flags & TX_CON_WANT_TUN) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004389 /* Tunnel mode will not have any analyser so it needs to
4390 * poll for reads.
4391 */
4392 channel_auto_read(chn);
4393 txn->req.msg_state = HTTP_MSG_TUNNEL;
4394 }
4395 else {
4396 /* we're not expecting any new data to come for this
4397 * transaction, so we can close it.
Christopher Faulet9768c262018-10-22 09:34:31 +02004398 *
4399 * However, there is an exception if the response
4400 * length is undefined. In this case, we need to wait
4401 * the close from the server. The response will be
4402 * switched in TUNNEL mode until the end.
Christopher Fauletf2824e62018-10-01 12:12:37 +02004403 */
4404 if (!(txn->rsp.flags & HTTP_MSGF_XFER_LEN) &&
4405 txn->rsp.msg_state != HTTP_MSG_CLOSED)
Christopher Faulet9768c262018-10-22 09:34:31 +02004406 goto check_channel_flags;
Christopher Fauletf2824e62018-10-01 12:12:37 +02004407
4408 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4409 channel_shutr_now(chn);
4410 channel_shutw_now(chn);
4411 }
4412 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004413 goto check_channel_flags;
4414 }
4415
4416 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
4417 http_msg_closing:
4418 /* nothing else to forward, just waiting for the output buffer
4419 * to be empty and for the shutw_now to take effect.
4420 */
4421 if (channel_is_empty(chn)) {
4422 txn->req.msg_state = HTTP_MSG_CLOSED;
4423 goto http_msg_closed;
4424 }
4425 else if (chn->flags & CF_SHUTW) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004426 txn->req.msg_state = HTTP_MSG_ERROR;
4427 goto end;
4428 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004429 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004430 return;
4431 }
4432
4433 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
4434 http_msg_closed:
Christopher Fauletf2824e62018-10-01 12:12:37 +02004435 /* if we don't know whether the server will close, we need to hard close */
4436 if (txn->rsp.flags & HTTP_MSGF_XFER_LEN)
Willy Tarreaucb041662022-05-17 19:44:42 +02004437 s->scb->flags |= SC_FL_NOLINGER; /* we want to close ASAP */
Christopher Fauletf2824e62018-10-01 12:12:37 +02004438 /* see above in MSG_DONE why we only do this in these states */
Christopher Faulet769d0e92019-03-22 14:23:18 +01004439 if (!(s->be->options & PR_O_ABRT_CLOSE))
Christopher Fauletf2824e62018-10-01 12:12:37 +02004440 channel_dont_read(chn);
4441 goto end;
4442 }
4443
4444 check_channel_flags:
4445 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4446 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4447 /* if we've just closed an output, let's switch */
4448 txn->req.msg_state = HTTP_MSG_CLOSING;
4449 goto http_msg_closing;
4450 }
4451
4452 end:
4453 chn->analysers &= AN_REQ_FLT_END;
Christopher Faulet198ef8b2020-12-15 13:32:55 +01004454 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
4455 chn->flags |= CF_NEVER_WAIT;
4456 if (HAS_REQ_DATA_FILTERS(s))
Christopher Fauletf2824e62018-10-01 12:12:37 +02004457 chn->analysers |= AN_REQ_FLT_XFER_DATA;
Christopher Faulet198ef8b2020-12-15 13:32:55 +01004458 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004459 channel_auto_close(chn);
4460 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004461 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004462}
4463
4464
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05004465/* This function terminates the response because it was completely analyzed or
Christopher Fauletf2824e62018-10-01 12:12:37 +02004466 * because an error was triggered during the body forwarding.
4467 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004468static void http_end_response(struct stream *s)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004469{
4470 struct channel *chn = &s->res;
4471 struct http_txn *txn = s->txn;
4472
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004473 DBG_TRACE_ENTER(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004474
Christopher Fauletb42a8b62018-11-19 21:59:00 +01004475 if (unlikely(txn->req.msg_state == HTTP_MSG_ERROR ||
4476 txn->rsp.msg_state == HTTP_MSG_ERROR)) {
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004477 channel_htx_truncate(&s->req, htxbuf(&s->req.buf));
Christopher Faulet9768c262018-10-22 09:34:31 +02004478 channel_abort(&s->req);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004479 goto end;
4480 }
4481
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004482 if (unlikely(txn->rsp.msg_state < HTTP_MSG_DONE)) {
4483 DBG_TRACE_DEVEL("waiting end of the response", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004484 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004485 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004486
4487 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
4488 /* In theory, we don't need to read anymore, but we must
4489 * still monitor the server connection for a possible close
4490 * while the request is being uploaded, so we don't disable
4491 * reading.
4492 */
4493 /* channel_dont_read(chn); */
4494
4495 if (txn->req.msg_state < HTTP_MSG_DONE) {
4496 /* The client seems to still be sending data, probably
4497 * because we got an error response during an upload.
4498 * We have the choice of either breaking the connection
4499 * or letting it pass through. Let's do the later.
4500 */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004501 DBG_TRACE_DEVEL("waiting end of the request", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004502 return;
4503 }
4504
4505 /* When we get here, it means that both the request and the
4506 * response have finished receiving. Depending on the connection
4507 * mode, we'll have to wait for the last bytes to leave in either
4508 * direction, and sometimes for a close to be effective.
4509 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02004510 if (txn->flags & TX_CON_WANT_TUN) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004511 channel_auto_read(chn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004512 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
4513 }
4514 else {
4515 /* we're not expecting any new data to come for this
4516 * transaction, so we can close it.
4517 */
4518 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4519 channel_shutr_now(chn);
4520 channel_shutw_now(chn);
4521 }
4522 }
4523 goto check_channel_flags;
4524 }
4525
4526 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
4527 http_msg_closing:
4528 /* nothing else to forward, just waiting for the output buffer
4529 * to be empty and for the shutw_now to take effect.
4530 */
4531 if (channel_is_empty(chn)) {
4532 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4533 goto http_msg_closed;
4534 }
4535 else if (chn->flags & CF_SHUTW) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004536 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau4781b152021-04-06 13:53:36 +02004537 _HA_ATOMIC_INC(&strm_sess(s)->fe->fe_counters.cli_aborts);
4538 _HA_ATOMIC_INC(&s->be->be_counters.cli_aborts);
William Lallemand36119de2021-03-08 15:26:48 +01004539 if (strm_sess(s)->listener && strm_sess(s)->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02004540 _HA_ATOMIC_INC(&strm_sess(s)->listener->counters->cli_aborts);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004541 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02004542 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.cli_aborts);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004543 goto end;
4544 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004545 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004546 return;
4547 }
4548
4549 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
4550 http_msg_closed:
4551 /* drop any pending data */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004552 channel_htx_truncate(&s->req, htxbuf(&s->req.buf));
Christopher Faulet9768c262018-10-22 09:34:31 +02004553 channel_abort(&s->req);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004554 goto end;
4555 }
4556
4557 check_channel_flags:
4558 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4559 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4560 /* if we've just closed an output, let's switch */
4561 txn->rsp.msg_state = HTTP_MSG_CLOSING;
4562 goto http_msg_closing;
4563 }
4564
4565 end:
4566 chn->analysers &= AN_RES_FLT_END;
Christopher Faulet198ef8b2020-12-15 13:32:55 +01004567 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
4568 chn->flags |= CF_NEVER_WAIT;
4569 if (HAS_RSP_DATA_FILTERS(s))
4570 chn->analysers |= AN_RES_FLT_XFER_DATA;
4571 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004572 channel_auto_close(chn);
4573 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004574 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004575}
4576
Christopher Fauletef70e252020-01-28 09:26:19 +01004577/* Forward a response generated by HAProxy (error/redirect/return). This
4578 * function forwards all pending incoming data. If <final> is set to 0, nothing
4579 * more is performed. It is used for 1xx informational messages. Otherwise, the
Christopher Faulet507479b2020-05-15 12:29:46 +02004580 * transaction is terminated and the request is emptied. On success 1 is
Christopher Faulet40e6b552020-06-25 16:04:50 +02004581 * returned. If an error occurred, 0 is returned. If it fails, this function
4582 * only exits. It is the caller responsibility to do the cleanup.
Christopher Fauletef70e252020-01-28 09:26:19 +01004583 */
4584int http_forward_proxy_resp(struct stream *s, int final)
4585{
4586 struct channel *req = &s->req;
4587 struct channel *res = &s->res;
4588 struct htx *htx = htxbuf(&res->buf);
4589 size_t data;
4590
4591 if (final) {
4592 htx->flags |= HTX_FL_PROXY_RESP;
Christopher Faulet507479b2020-05-15 12:29:46 +02004593
Christopher Fauletaab1b672020-11-18 16:44:02 +01004594 if (!htx_is_empty(htx) && !http_eval_after_res_rules(s))
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01004595 return 0;
Christopher Fauletef70e252020-01-28 09:26:19 +01004596
Christopher Fauletd6c48362020-10-19 18:01:38 +02004597 if (s->txn->meth == HTTP_METH_HEAD)
4598 htx_skip_msg_payload(htx);
4599
Christopher Fauletef70e252020-01-28 09:26:19 +01004600 channel_auto_read(req);
4601 channel_abort(req);
4602 channel_auto_close(req);
4603 channel_htx_erase(req, htxbuf(&req->buf));
4604
4605 res->wex = tick_add_ifset(now_ms, res->wto);
4606 channel_auto_read(res);
4607 channel_auto_close(res);
4608 channel_shutr_now(res);
Christopher Faulet1a9db7c2020-06-25 15:36:45 +02004609 res->flags |= CF_EOI; /* The response is terminated, add EOI */
Christopher Faulet42432f32020-11-20 17:43:16 +01004610 htxbuf(&res->buf)->flags |= HTX_FL_EOM; /* no more data are expected */
Christopher Fauletef70e252020-01-28 09:26:19 +01004611 }
Christopher Fauletcf6898c2020-06-25 15:55:11 +02004612 else {
4613 /* Send ASAP informational messages. Rely on CF_EOI for final
4614 * response.
4615 */
4616 res->flags |= CF_SEND_DONTWAIT;
4617 }
Christopher Fauletef70e252020-01-28 09:26:19 +01004618
4619 data = htx->data - co_data(res);
4620 c_adv(res, data);
4621 htx->first = -1;
4622 res->total += data;
4623 return 1;
4624}
4625
Willy Tarreaub49672d2022-05-27 10:13:37 +02004626void http_server_error(struct stream *s, struct stconn *sc, int err,
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004627 int finst, struct http_reply *msg)
Christopher Faulet0f226952018-10-22 09:29:56 +02004628{
Christopher Faulet72c7d8d2020-01-27 15:32:25 +01004629 http_reply_and_close(s, s->txn->status, msg);
Christopher Faulet0f226952018-10-22 09:29:56 +02004630 if (!(s->flags & SF_ERR_MASK))
4631 s->flags |= err;
4632 if (!(s->flags & SF_FINST_MASK))
4633 s->flags |= finst;
4634}
4635
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004636void http_reply_and_close(struct stream *s, short status, struct http_reply *msg)
Christopher Faulet0f226952018-10-22 09:29:56 +02004637{
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004638 if (!msg) {
4639 channel_htx_truncate(&s->res, htxbuf(&s->res.buf));
4640 goto end;
4641 }
4642
4643 if (http_reply_message(s, msg) == -1) {
4644 /* On error, return a 500 error message, but don't rewrite it if
Christopher Faulet40e6b552020-06-25 16:04:50 +02004645 * it is already an internal error. If it was already a "const"
4646 * 500 error, just fail.
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004647 */
Christopher Faulet40e6b552020-06-25 16:04:50 +02004648 if (s->txn->status == 500) {
4649 if (s->txn->flags & TX_CONST_REPLY)
4650 goto end;
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004651 s->txn->flags |= TX_CONST_REPLY;
Christopher Faulet40e6b552020-06-25 16:04:50 +02004652 }
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004653 s->txn->status = 500;
4654 s->txn->http_reply = NULL;
4655 return http_reply_and_close(s, s->txn->status, http_error_message(s));
4656 }
4657
4658end:
4659 s->res.wex = tick_add_ifset(now_ms, s->res.wto);
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004660
Christopher Faulet2d565002021-09-10 09:17:50 +02004661 /* At this staged, HTTP analysis is finished */
4662 s->req.analysers &= AN_REQ_FLT_END;
4663 s->req.analyse_exp = TICK_ETERNITY;
4664
4665 s->res.analysers &= AN_RES_FLT_END;
4666 s->res.analyse_exp = TICK_ETERNITY;
4667
Christopher Faulet0f226952018-10-22 09:29:56 +02004668 channel_auto_read(&s->req);
4669 channel_abort(&s->req);
4670 channel_auto_close(&s->req);
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004671 channel_htx_erase(&s->req, htxbuf(&s->req.buf));
Christopher Faulet72c7d8d2020-01-27 15:32:25 +01004672 channel_auto_read(&s->res);
4673 channel_auto_close(&s->res);
4674 channel_shutr_now(&s->res);
Christopher Faulet0f226952018-10-22 09:29:56 +02004675}
4676
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004677struct http_reply *http_error_message(struct stream *s)
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004678{
4679 const int msgnum = http_get_status_idx(s->txn->status);
4680
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004681 if (s->txn->http_reply)
4682 return s->txn->http_reply;
4683 else if (s->be->replies[msgnum])
4684 return s->be->replies[msgnum];
4685 else if (strm_fe(s)->replies[msgnum])
4686 return strm_fe(s)->replies[msgnum];
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004687 else
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004688 return &http_err_replies[msgnum];
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004689}
4690
Christopher Faulet40e6b552020-06-25 16:04:50 +02004691/* Produces an HTX message from an http reply. Depending on the http reply type,
4692 * a, errorfile, an raw file or a log-format string is used. On success, it
4693 * returns 0. If an error occurs -1 is returned. If it fails, this function only
4694 * exits. It is the caller responsibility to do the cleanup.
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004695 */
Christopher Fauletae43b6c2020-05-27 15:24:22 +02004696int http_reply_to_htx(struct stream *s, struct htx *htx, struct http_reply *reply)
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004697{
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004698 struct buffer *errmsg;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004699 struct htx_sl *sl;
4700 struct buffer *body = NULL;
4701 const char *status, *reason, *clen, *ctype;
4702 unsigned int slflags;
4703 int ret = 0;
4704
Christopher Faulete29a97e2020-05-14 14:49:25 +02004705 /*
4706 * - HTTP_REPLY_ERRFILES unexpected here. handled as no payload if so
4707 *
4708 * - HTTP_REPLY_INDIRECT: switch on another reply if defined or handled
4709 * as no payload if NULL. the TXN status code is set with the status
4710 * of the original reply.
4711 */
4712
4713 if (reply->type == HTTP_REPLY_INDIRECT) {
4714 if (reply->body.reply)
4715 reply = reply->body.reply;
4716 }
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004717 if (reply->type == HTTP_REPLY_ERRMSG && !reply->body.errmsg) {
4718 /* get default error message */
4719 if (reply == s->txn->http_reply)
4720 s->txn->http_reply = NULL;
4721 reply = http_error_message(s);
4722 if (reply->type == HTTP_REPLY_INDIRECT) {
4723 if (reply->body.reply)
4724 reply = reply->body.reply;
4725 }
4726 }
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004727
4728 if (reply->type == HTTP_REPLY_ERRMSG) {
4729 /* implicit or explicit error message*/
4730 errmsg = reply->body.errmsg;
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004731 if (errmsg && !b_is_null(errmsg)) {
Christopher Faulet20567362020-05-15 14:52:49 +02004732 if (!htx_copy_msg(htx, errmsg))
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004733 goto fail;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004734 }
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004735 }
4736 else {
4737 /* no payload, file or log-format string */
4738 if (reply->type == HTTP_REPLY_RAW) {
4739 /* file */
4740 body = &reply->body.obj;
4741 }
4742 else if (reply->type == HTTP_REPLY_LOGFMT) {
4743 /* log-format string */
4744 body = alloc_trash_chunk();
4745 if (!body)
4746 goto fail_alloc;
4747 body->data = build_logline(s, body->area, body->size, &reply->body.fmt);
4748 }
4749 /* else no payload */
4750
4751 status = ultoa(reply->status);
4752 reason = http_get_reason(reply->status);
4753 slflags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_CLEN);
4754 if (!body || !b_data(body))
4755 slflags |= HTX_SL_F_BODYLESS;
4756 sl = htx_add_stline(htx, HTX_BLK_RES_SL, slflags, ist("HTTP/1.1"), ist(status), ist(reason));
4757 if (!sl)
4758 goto fail;
4759 sl->info.res.status = reply->status;
4760
4761 clen = (body ? ultoa(b_data(body)) : "0");
4762 ctype = reply->ctype;
4763
4764 if (!LIST_ISEMPTY(&reply->hdrs)) {
4765 struct http_reply_hdr *hdr;
4766 struct buffer *value = alloc_trash_chunk();
4767
4768 if (!value)
4769 goto fail;
4770
4771 list_for_each_entry(hdr, &reply->hdrs, list) {
4772 chunk_reset(value);
4773 value->data = build_logline(s, value->area, value->size, &hdr->value);
4774 if (b_data(value) && !htx_add_header(htx, hdr->name, ist2(b_head(value), b_data(value)))) {
4775 free_trash_chunk(value);
4776 goto fail;
4777 }
4778 chunk_reset(value);
4779 }
4780 free_trash_chunk(value);
4781 }
4782
4783 if (!htx_add_header(htx, ist("content-length"), ist(clen)) ||
4784 (body && b_data(body) && ctype && !htx_add_header(htx, ist("content-type"), ist(ctype))) ||
4785 !htx_add_endof(htx, HTX_BLK_EOH) ||
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004786 (body && b_data(body) && !htx_add_data_atonce(htx, ist2(b_head(body), b_data(body)))))
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004787 goto fail;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004788
4789 htx->flags |= HTX_FL_EOM;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004790 }
4791
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004792 leave:
4793 if (reply->type == HTTP_REPLY_LOGFMT)
4794 free_trash_chunk(body);
4795 return ret;
4796
4797 fail_alloc:
4798 if (!(s->flags & SF_ERR_MASK))
4799 s->flags |= SF_ERR_RESOURCE;
Christopher Faulet97e466c2020-05-15 15:12:47 +02004800 /* fall through */
4801 fail:
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004802 ret = -1;
4803 goto leave;
Christopher Faulet97e466c2020-05-15 15:12:47 +02004804}
4805
4806/* Send an http reply to the client. On success, it returns 0. If an error
Christopher Faulet40e6b552020-06-25 16:04:50 +02004807 * occurs -1 is returned and the response channel is truncated, removing this
4808 * way the faulty reply. This function may fail when the reply is formatted
4809 * (http_reply_to_htx) or when the reply is forwarded
4810 * (http_forward_proxy_resp). On the last case, it is because a
4811 * http-after-response rule fails.
Christopher Faulet97e466c2020-05-15 15:12:47 +02004812 */
4813int http_reply_message(struct stream *s, struct http_reply *reply)
4814{
4815 struct channel *res = &s->res;
4816 struct htx *htx = htx_from_buf(&res->buf);
4817
4818 if (s->txn->status == -1)
4819 s->txn->status = reply->status;
4820 channel_htx_truncate(res, htx);
4821
4822 if (http_reply_to_htx(s, htx, reply) == -1)
4823 goto fail;
4824
4825 htx_to_buf(htx, &s->res.buf);
4826 if (!http_forward_proxy_resp(s, 1))
4827 goto fail;
4828 return 0;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004829
4830 fail:
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004831 channel_htx_truncate(res, htx);
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004832 if (!(s->flags & SF_ERR_MASK))
4833 s->flags |= SF_ERR_PRXCOND;
Christopher Faulet97e466c2020-05-15 15:12:47 +02004834 return -1;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004835}
4836
Christopher Faulet50264b42022-03-30 19:39:30 +02004837/* Return the error message corresponding to s->conn_err_type. It is assumed
Christopher Faulet304cc402019-07-15 15:46:28 +02004838 * that the server side is closed. Note that err_type is actually a
4839 * bitmask, where almost only aborts may be cumulated with other
4840 * values. We consider that aborted operations are more important
4841 * than timeouts or errors due to the fact that nobody else in the
4842 * logs might explain incomplete retries. All others should avoid
4843 * being cumulated. It should normally not be possible to have multiple
4844 * aborts at once, but just in case, the first one in sequence is reported.
4845 * Note that connection errors appearing on the second request of a keep-alive
4846 * connection are not reported since this allows the client to retry.
4847 */
Willy Tarreaub49672d2022-05-27 10:13:37 +02004848void http_return_srv_error(struct stream *s, struct stconn *sc)
Christopher Faulet304cc402019-07-15 15:46:28 +02004849{
Christopher Faulet50264b42022-03-30 19:39:30 +02004850 int err_type = s->conn_err_type;
Christopher Faulet304cc402019-07-15 15:46:28 +02004851
4852 /* set s->txn->status for http_error_message(s) */
Christopher Faulet50264b42022-03-30 19:39:30 +02004853 if (err_type & STRM_ET_QUEUE_ABRT) {
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004854 s->txn->status = -1;
Willy Tarreaub49672d2022-05-27 10:13:37 +02004855 http_server_error(s, sc, SF_ERR_CLICL, SF_FINST_Q, NULL);
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004856 }
Christopher Faulet50264b42022-03-30 19:39:30 +02004857 else if (err_type & STRM_ET_CONN_ABRT) {
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004858 s->txn->status = -1;
Willy Tarreaub49672d2022-05-27 10:13:37 +02004859 http_server_error(s, sc, SF_ERR_CLICL, SF_FINST_C, NULL);
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004860 }
Christopher Faulet50264b42022-03-30 19:39:30 +02004861 else if (err_type & STRM_ET_QUEUE_TO) {
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004862 s->txn->status = 503;
Willy Tarreaub49672d2022-05-27 10:13:37 +02004863 http_server_error(s, sc, SF_ERR_SRVTO, SF_FINST_Q,
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004864 http_error_message(s));
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004865 }
Christopher Faulet50264b42022-03-30 19:39:30 +02004866 else if (err_type & STRM_ET_QUEUE_ERR) {
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004867 s->txn->status = 503;
Willy Tarreaub49672d2022-05-27 10:13:37 +02004868 http_server_error(s, sc, SF_ERR_SRVCL, SF_FINST_Q,
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004869 http_error_message(s));
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004870 }
Christopher Faulet50264b42022-03-30 19:39:30 +02004871 else if (err_type & STRM_ET_CONN_TO) {
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004872 s->txn->status = 503;
Willy Tarreaub49672d2022-05-27 10:13:37 +02004873 http_server_error(s, sc, SF_ERR_SRVTO, SF_FINST_C,
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004874 (s->txn->flags & TX_NOT_FIRST) ? NULL :
4875 http_error_message(s));
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004876 }
Christopher Faulet50264b42022-03-30 19:39:30 +02004877 else if (err_type & STRM_ET_CONN_ERR) {
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004878 s->txn->status = 503;
Willy Tarreaub49672d2022-05-27 10:13:37 +02004879 http_server_error(s, sc, SF_ERR_SRVCL, SF_FINST_C,
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004880 (s->flags & SF_SRV_REUSED) ? NULL :
4881 http_error_message(s));
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004882 }
Christopher Faulet50264b42022-03-30 19:39:30 +02004883 else if (err_type & STRM_ET_CONN_RES) {
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004884 s->txn->status = 503;
Willy Tarreaub49672d2022-05-27 10:13:37 +02004885 http_server_error(s, sc, SF_ERR_RESOURCE, SF_FINST_C,
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004886 (s->txn->flags & TX_NOT_FIRST) ? NULL :
4887 http_error_message(s));
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004888 }
Christopher Faulet50264b42022-03-30 19:39:30 +02004889 else { /* STRM_ET_CONN_OTHER and others */
Christopher Faulet304cc402019-07-15 15:46:28 +02004890 s->txn->status = 500;
Willy Tarreaub49672d2022-05-27 10:13:37 +02004891 http_server_error(s, sc, SF_ERR_INTERNAL, SF_FINST_C,
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004892 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004893 }
4894}
4895
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004896
Christopher Faulet4a28a532019-03-01 11:19:40 +01004897/* Handle Expect: 100-continue for HTTP/1.1 messages if necessary. It returns 0
4898 * on success and -1 on error.
4899 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004900static int http_handle_expect_hdr(struct stream *s, struct htx *htx, struct http_msg *msg)
Christopher Faulet4a28a532019-03-01 11:19:40 +01004901{
4902 /* If we have HTTP/1.1 message with a body and Expect: 100-continue,
4903 * then we must send an HTTP/1.1 100 Continue intermediate response.
4904 */
4905 if (msg->msg_state == HTTP_MSG_BODY && (msg->flags & HTTP_MSGF_VER_11) &&
4906 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
4907 struct ist hdr = { .ptr = "Expect", .len = 6 };
4908 struct http_hdr_ctx ctx;
4909
4910 ctx.blk = NULL;
4911 /* Expect is allowed in 1.1, look for it */
4912 if (http_find_header(htx, hdr, &ctx, 0) &&
4913 unlikely(isteqi(ctx.value, ist2("100-continue", 12)))) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004914 if (http_reply_100_continue(s) == -1)
Christopher Faulet4a28a532019-03-01 11:19:40 +01004915 return -1;
4916 http_remove_header(htx, &ctx);
4917 }
4918 }
4919 return 0;
4920}
4921
Christopher Faulet23a3c792018-11-28 10:01:23 +01004922/* Send a 100-Continue response to the client. It returns 0 on success and -1
4923 * on error. The response channel is updated accordingly.
4924 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004925static int http_reply_100_continue(struct stream *s)
Christopher Faulet23a3c792018-11-28 10:01:23 +01004926{
4927 struct channel *res = &s->res;
4928 struct htx *htx = htx_from_buf(&res->buf);
4929 struct htx_sl *sl;
4930 unsigned int flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|
4931 HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
Christopher Faulet23a3c792018-11-28 10:01:23 +01004932
4933 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
4934 ist("HTTP/1.1"), ist("100"), ist("Continue"));
4935 if (!sl)
4936 goto fail;
4937 sl->info.res.status = 100;
4938
Christopher Faulet1d5ec092019-06-26 14:23:54 +02004939 if (!htx_add_endof(htx, HTX_BLK_EOH))
Christopher Faulet23a3c792018-11-28 10:01:23 +01004940 goto fail;
4941
Christopher Fauleta72a7e42020-01-28 09:28:11 +01004942 if (!http_forward_proxy_resp(s, 0))
4943 goto fail;
Christopher Faulet23a3c792018-11-28 10:01:23 +01004944 return 0;
4945
4946 fail:
4947 /* If an error occurred, remove the incomplete HTTP response from the
4948 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004949 channel_htx_truncate(res, htx);
Christopher Faulet23a3c792018-11-28 10:01:23 +01004950 return -1;
4951}
4952
Christopher Faulet12c51e22018-11-28 15:59:42 +01004953
Christopher Faulet0f226952018-10-22 09:29:56 +02004954/*
4955 * Capture headers from message <htx> according to header list <cap_hdr>, and
4956 * fill the <cap> pointers appropriately.
4957 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004958static void http_capture_headers(struct htx *htx, char **cap, struct cap_hdr *cap_hdr)
Christopher Faulet0f226952018-10-22 09:29:56 +02004959{
4960 struct cap_hdr *h;
4961 int32_t pos;
4962
Christopher Fauleta3f15502019-05-13 15:27:23 +02004963 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet0f226952018-10-22 09:29:56 +02004964 struct htx_blk *blk = htx_get_blk(htx, pos);
4965 enum htx_blk_type type = htx_get_blk_type(blk);
4966 struct ist n, v;
4967
4968 if (type == HTX_BLK_EOH)
4969 break;
4970 if (type != HTX_BLK_HDR)
4971 continue;
4972
4973 n = htx_get_blk_name(htx, blk);
4974
4975 for (h = cap_hdr; h; h = h->next) {
4976 if (h->namelen && (h->namelen == n.len) &&
4977 (strncasecmp(n.ptr, h->name, h->namelen) == 0)) {
4978 if (cap[h->index] == NULL)
4979 cap[h->index] =
4980 pool_alloc(h->pool);
4981
4982 if (cap[h->index] == NULL) {
4983 ha_alert("HTTP capture : out of memory.\n");
4984 break;
4985 }
4986
4987 v = htx_get_blk_value(htx, blk);
Tim Duesterhus2471f5c2021-11-08 09:05:01 +01004988 v = isttrim(v, h->len);
Christopher Faulet0f226952018-10-22 09:29:56 +02004989
4990 memcpy(cap[h->index], v.ptr, v.len);
4991 cap[h->index][v.len]=0;
4992 }
4993 }
4994 }
4995}
4996
Christopher Faulet0b6bdc52018-10-24 11:05:36 +02004997/* Delete a value in a header between delimiters <from> and <next>. The header
4998 * itself is delimited by <start> and <end> pointers. The number of characters
4999 * displaced is returned, and the pointer to the first delimiter is updated if
5000 * required. The function tries as much as possible to respect the following
5001 * principles :
5002 * - replace <from> delimiter by the <next> one unless <from> points to <start>,
5003 * in which case <next> is simply removed
5004 * - set exactly one space character after the new first delimiter, unless there
5005 * are not enough characters in the block being moved to do so.
5006 * - remove unneeded spaces before the previous delimiter and after the new
5007 * one.
5008 *
5009 * It is the caller's responsibility to ensure that :
5010 * - <from> points to a valid delimiter or <start> ;
5011 * - <next> points to a valid delimiter or <end> ;
5012 * - there are non-space chars before <from>.
5013 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005014static int http_del_hdr_value(char *start, char *end, char **from, char *next)
Christopher Faulet0b6bdc52018-10-24 11:05:36 +02005015{
5016 char *prev = *from;
5017
5018 if (prev == start) {
5019 /* We're removing the first value. eat the semicolon, if <next>
5020 * is lower than <end> */
5021 if (next < end)
5022 next++;
5023
5024 while (next < end && HTTP_IS_SPHT(*next))
5025 next++;
5026 }
5027 else {
5028 /* Remove useless spaces before the old delimiter. */
5029 while (HTTP_IS_SPHT(*(prev-1)))
5030 prev--;
5031 *from = prev;
5032
5033 /* copy the delimiter and if possible a space if we're
5034 * not at the end of the line.
5035 */
5036 if (next < end) {
5037 *prev++ = *next++;
5038 if (prev + 1 < next)
5039 *prev++ = ' ';
5040 while (next < end && HTTP_IS_SPHT(*next))
5041 next++;
5042 }
5043 }
5044 memmove(prev, next, end - next);
5045 return (prev - next);
5046}
5047
Christopher Faulet0f226952018-10-22 09:29:56 +02005048
5049/* Formats the start line of the request (without CRLF) and puts it in <str> and
Joseph Herlantc42c0e92018-11-25 10:43:27 -08005050 * return the written length. The line can be truncated if it exceeds <len>.
Christopher Faulet0f226952018-10-22 09:29:56 +02005051 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005052static size_t http_fmt_req_line(const struct htx_sl *sl, char *str, size_t len)
Christopher Faulet0f226952018-10-22 09:29:56 +02005053{
5054 struct ist dst = ist2(str, 0);
5055
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005056 if (istcat(&dst, htx_sl_req_meth(sl), len) == -1)
Christopher Faulet0f226952018-10-22 09:29:56 +02005057 goto end;
5058 if (dst.len + 1 > len)
5059 goto end;
5060 dst.ptr[dst.len++] = ' ';
5061
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005062 if (istcat(&dst, htx_sl_req_uri(sl), len) == -1)
Christopher Faulet0f226952018-10-22 09:29:56 +02005063 goto end;
5064 if (dst.len + 1 > len)
5065 goto end;
5066 dst.ptr[dst.len++] = ' ';
5067
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005068 istcat(&dst, htx_sl_req_vsn(sl), len);
Christopher Faulet0f226952018-10-22 09:29:56 +02005069 end:
5070 return dst.len;
5071}
5072
5073/*
5074 * Print a debug line with a start line.
5075 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005076static void http_debug_stline(const char *dir, struct stream *s, const struct htx_sl *sl)
Christopher Faulet0f226952018-10-22 09:29:56 +02005077{
5078 struct session *sess = strm_sess(s);
5079 int max;
5080
5081 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
5082 dir,
Willy Tarreau88bc8002021-12-06 07:01:02 +00005083 objt_conn(sess->origin) ? (unsigned short)__objt_conn(sess->origin)->handle.fd : -1,
Willy Tarreaufd9417b2022-05-18 16:23:22 +02005084 sc_conn(s->scb) ? (unsigned short)(__sc_conn(s->scb))->handle.fd : -1);
Christopher Faulet0f226952018-10-22 09:29:56 +02005085
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005086 max = HTX_SL_P1_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02005087 UBOUND(max, trash.size - trash.data - 3);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005088 chunk_memcat(&trash, HTX_SL_P1_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02005089 trash.area[trash.data++] = ' ';
5090
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005091 max = HTX_SL_P2_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02005092 UBOUND(max, trash.size - trash.data - 2);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005093 chunk_memcat(&trash, HTX_SL_P2_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02005094 trash.area[trash.data++] = ' ';
5095
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005096 max = HTX_SL_P3_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02005097 UBOUND(max, trash.size - trash.data - 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005098 chunk_memcat(&trash, HTX_SL_P3_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02005099 trash.area[trash.data++] = '\n';
5100
Willy Tarreau2e8ab6b2020-03-14 11:03:20 +01005101 DISGUISE(write(1, trash.area, trash.data));
Christopher Faulet0f226952018-10-22 09:29:56 +02005102}
5103
5104/*
5105 * Print a debug line with a header.
5106 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005107static 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 +02005108{
5109 struct session *sess = strm_sess(s);
5110 int max;
5111
5112 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
5113 dir,
Willy Tarreau88bc8002021-12-06 07:01:02 +00005114 objt_conn(sess->origin) ? (unsigned short)__objt_conn(sess->origin)->handle.fd : -1,
Willy Tarreaufd9417b2022-05-18 16:23:22 +02005115 sc_conn(s->scb) ? (unsigned short)(__sc_conn(s->scb))->handle.fd : -1);
Christopher Faulet0f226952018-10-22 09:29:56 +02005116
5117 max = n.len;
5118 UBOUND(max, trash.size - trash.data - 3);
5119 chunk_memcat(&trash, n.ptr, max);
5120 trash.area[trash.data++] = ':';
5121 trash.area[trash.data++] = ' ';
5122
5123 max = v.len;
5124 UBOUND(max, trash.size - trash.data - 1);
5125 chunk_memcat(&trash, v.ptr, max);
5126 trash.area[trash.data++] = '\n';
5127
Willy Tarreau2e8ab6b2020-03-14 11:03:20 +01005128 DISGUISE(write(1, trash.area, trash.data));
Christopher Faulet0f226952018-10-22 09:29:56 +02005129}
5130
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005131void http_txn_reset_req(struct http_txn *txn)
5132{
Christopher Faulet1aea50e2020-01-17 16:03:53 +01005133 txn->req.flags = 0;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005134 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
5135}
5136
5137void http_txn_reset_res(struct http_txn *txn)
5138{
Christopher Faulet1aea50e2020-01-17 16:03:53 +01005139 txn->rsp.flags = 0;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005140 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
5141}
5142
5143/*
Christopher Faulet75f619a2021-03-08 19:12:58 +01005144 * Create and initialize a new HTTP transaction for stream <s>. This should be
5145 * used before processing any new request. It returns the transaction or NLULL
5146 * on error.
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005147 */
Christopher Faulet75f619a2021-03-08 19:12:58 +01005148struct http_txn *http_create_txn(struct stream *s)
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005149{
Christopher Faulet75f619a2021-03-08 19:12:58 +01005150 struct http_txn *txn;
Willy Tarreaub49672d2022-05-27 10:13:37 +02005151 struct stconn *sc = s->scf;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005152
Christopher Faulet75f619a2021-03-08 19:12:58 +01005153 txn = pool_alloc(pool_head_http_txn);
5154 if (!txn)
5155 return NULL;
5156 s->txn = txn;
5157
Christopher Faulet5eb67f52022-06-22 17:12:05 +02005158 txn->meth = HTTP_METH_OTHER;
Willy Tarreaub49672d2022-05-27 10:13:37 +02005159 txn->flags = ((sc && sc_ep_test(sc, SE_FL_NOT_FIRST)) ? TX_NOT_FIRST : 0);
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005160 txn->status = -1;
Christopher Faulet5cb513a2020-05-13 17:56:56 +02005161 txn->http_reply = NULL;
Christopher Faulete05bf9e2022-03-29 15:23:40 +02005162 txn->l7_buffer = BUF_NULL;
Willy Tarreau8b507582020-02-25 09:35:07 +01005163 write_u32(txn->cache_hash, 0);
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005164
5165 txn->cookie_first_date = 0;
5166 txn->cookie_last_date = 0;
5167
5168 txn->srv_cookie = NULL;
5169 txn->cli_cookie = NULL;
5170 txn->uri = NULL;
5171
5172 http_txn_reset_req(txn);
5173 http_txn_reset_res(txn);
5174
5175 txn->req.chn = &s->req;
5176 txn->rsp.chn = &s->res;
5177
5178 txn->auth.method = HTTP_AUTH_UNKNOWN;
5179
Aurelien DARRAGON5ad2b642022-11-18 09:17:29 +01005180 /* here we don't want to re-initialize s->vars_txn and s->vars_reqres
5181 * variable lists, because they were already initialized upon stream
5182 * creation in stream_new(), and thus may already contain some variables
5183 */
Christopher Faulet75f619a2021-03-08 19:12:58 +01005184
5185 return txn;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005186}
5187
5188/* to be used at the end of a transaction */
Christopher Faulet75f619a2021-03-08 19:12:58 +01005189void http_destroy_txn(struct stream *s)
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005190{
5191 struct http_txn *txn = s->txn;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005192
5193 /* these ones will have been dynamically allocated */
5194 pool_free(pool_head_requri, txn->uri);
5195 pool_free(pool_head_capture, txn->cli_cookie);
5196 pool_free(pool_head_capture, txn->srv_cookie);
Tim Duesterhusa17e6622020-03-05 20:19:02 +01005197 pool_free(pool_head_uniqueid, s->unique_id.ptr);
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005198
Tim Duesterhusa17e6622020-03-05 20:19:02 +01005199 s->unique_id = IST_NULL;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005200 txn->uri = NULL;
5201 txn->srv_cookie = NULL;
5202 txn->cli_cookie = NULL;
5203
Christopher Faulet59399252019-11-07 14:27:52 +01005204 if (!LIST_ISEMPTY(&s->vars_txn.head))
5205 vars_prune(&s->vars_txn, s->sess, s);
5206 if (!LIST_ISEMPTY(&s->vars_reqres.head))
5207 vars_prune(&s->vars_reqres, s->sess, s);
Christopher Faulet75f619a2021-03-08 19:12:58 +01005208
Christopher Faulete05bf9e2022-03-29 15:23:40 +02005209 b_free(&txn->l7_buffer);
5210
Christopher Faulet75f619a2021-03-08 19:12:58 +01005211 pool_free(pool_head_http_txn, txn);
5212 s->txn = NULL;
Christopher Faulet59399252019-11-07 14:27:52 +01005213}
5214
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005215
Christopher Faulet71236de2023-01-13 09:06:38 +01005216void http_set_term_flags(struct stream *s)
5217{
5218 if (!(s->flags & SF_ERR_MASK))
5219 s->flags |= SF_ERR_PRXCOND;
5220
5221 if (!(s->flags & SF_FINST_MASK)) {
5222 if (s->scb->state == SC_ST_INI) {
5223 /* Before any connection attempt on the server side, we
5224 * are still in the request analysis. Just take case to
5225 * detect tarpit error
5226 */
5227 if (s->req.analysers & AN_REQ_HTTP_TARPIT)
5228 s->flags |= SF_FINST_T;
5229 else
5230 s->flags |= SF_FINST_R;
5231 }
5232 else if (s->scb->state == SC_ST_QUE)
5233 s->flags |= SF_FINST_Q;
5234 else if (sc_state_in(s->scb->state, SC_SB_REQ|SC_SB_TAR|SC_SB_ASS|SC_SB_CON|SC_SB_CER|SC_SB_RDY)) {
5235 if (unlikely(objt_applet(s->target))) {
5236 s->flags |= SF_FINST_R;
5237 }
5238 else
5239 s->flags |= SF_FINST_C;
5240 }
5241 else {
5242 if (s->txn->rsp.msg_state < HTTP_MSG_DATA) {
5243 /* We are still processing the response headers */
5244 s->flags |= SF_FINST_H;
5245 }
5246 // (res >= done) & (res->flags & shutw)
5247 else if (s->txn->rsp.msg_state >= HTTP_MSG_DONE &&
5248 (s->flags & (SF_ERR_CLITO|SF_ERR_CLICL))) {
5249 /* A client error was reported and we are
5250 * transmitting the last block of data
5251 */
5252 s->flags |= SF_FINST_L;
5253 }
5254 else {
5255 /* Otherwise we are in DATA phase on both sides */
5256 s->flags |= SF_FINST_D;
5257 }
5258 }
5259 }
5260}
5261
5262
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005263DECLARE_POOL(pool_head_http_txn, "http_txn", sizeof(struct http_txn));
Christopher Faulet0f226952018-10-22 09:29:56 +02005264
Christopher Fauletf4eb75d2018-10-11 15:55:07 +02005265/*
5266 * Local variables:
5267 * c-indent-level: 8
5268 * c-basic-offset: 8
5269 * End:
5270 */