blob: 1d9423af48d12e63f9e126861e5a03dae4044609 [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 /* Note that we don't have to send 100-continue back because we don't
988 * need the data to complete our job, and it's up to the server to
989 * decide whether to return 100, 417 or anything else in return of
990 * an "Expect: 100-continue" header.
991 */
Christopher Faulet9768c262018-10-22 09:34:31 +0200992 if (msg->msg_state == HTTP_MSG_BODY)
993 msg->msg_state = HTTP_MSG_DATA;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200994
Christopher Faulete0768eb2018-10-03 16:38:02 +0200995 /* in most states, we should abort in case of early close */
996 channel_auto_close(req);
997
998 if (req->to_forward) {
Christopher Faulet66af0b22019-03-22 14:54:52 +0100999 if (req->to_forward == CHN_INFINITE_FORWARD) {
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001000 if (req->flags & CF_EOI)
1001 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet66af0b22019-03-22 14:54:52 +01001002 }
1003 else {
1004 /* We can't process the buffer's contents yet */
1005 req->flags |= CF_WAKE_WRITE;
1006 goto missing_data_or_waiting;
1007 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001008 }
1009
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001010 if (msg->msg_state >= HTTP_MSG_ENDING)
1011 goto ending;
1012
1013 if (txn->meth == HTTP_METH_CONNECT) {
1014 msg->msg_state = HTTP_MSG_ENDING;
1015 goto ending;
1016 }
1017
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001018 /* Forward input data. We get it by removing all outgoing data not
1019 * forwarded yet from HTX data size. If there are some data filters, we
1020 * let them decide the amount of data to forward.
Christopher Faulet9768c262018-10-22 09:34:31 +02001021 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001022 if (HAS_REQ_DATA_FILTERS(s)) {
1023 ret = flt_http_payload(s, msg, htx->data);
1024 if (ret < 0)
1025 goto return_bad_req;
Christopher Faulet421e7692019-06-13 11:16:45 +02001026 c_adv(req, ret);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001027 }
1028 else {
Christopher Faulet421e7692019-06-13 11:16:45 +02001029 c_adv(req, htx->data - co_data(req));
Christopher Faulet66af0b22019-03-22 14:54:52 +01001030 if (msg->flags & HTTP_MSGF_XFER_LEN)
1031 channel_htx_forward_forever(req, htx);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001032 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001033
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001034 if (htx->data != co_data(req))
1035 goto missing_data_or_waiting;
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001036
Christopher Faulet9768c262018-10-22 09:34:31 +02001037 /* Check if the end-of-message is reached and if so, switch the message
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001038 * in HTTP_MSG_ENDING state. Then if all data was marked to be
1039 * forwarded, set the state to HTTP_MSG_DONE.
Christopher Faulet9768c262018-10-22 09:34:31 +02001040 */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001041 if (!(htx->flags & HTX_FL_EOM))
Christopher Faulet9768c262018-10-22 09:34:31 +02001042 goto missing_data_or_waiting;
1043
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001044 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet9768c262018-10-22 09:34:31 +02001045
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001046 ending:
Christopher Faulet2151cdd2020-07-22 16:34:59 +02001047 req->flags &= ~CF_EXPECT_MORE; /* no more data are expected */
1048
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001049 /* other states, ENDING...TUNNEL */
1050 if (msg->msg_state >= HTTP_MSG_DONE)
1051 goto done;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001052
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001053 if (HAS_REQ_DATA_FILTERS(s)) {
1054 ret = flt_http_end(s, msg);
1055 if (ret <= 0) {
1056 if (!ret)
1057 goto missing_data_or_waiting;
1058 goto return_bad_req;
1059 }
1060 }
1061
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001062 if (txn->meth == HTTP_METH_CONNECT)
1063 msg->msg_state = HTTP_MSG_TUNNEL;
1064 else {
1065 msg->msg_state = HTTP_MSG_DONE;
1066 req->to_forward = 0;
1067 }
1068
1069 done:
1070 /* we don't want to forward closes on DONE except in tunnel mode. */
1071 if (!(txn->flags & TX_CON_WANT_TUN))
1072 channel_dont_close(req);
1073
Christopher Fauletf2b02cf2023-01-13 11:02:28 +01001074 if ((req->flags & CF_SHUTW) && co_data(req)) {
1075 /* request errors are most likely due to the server aborting the
1076 * transfer. */
1077 goto return_srv_abort;
1078 }
1079
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001080 http_end_request(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001081 if (!(req->analysers & an_bit)) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001082 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001083 return 1;
1084 }
1085
1086 /* If "option abortonclose" is set on the backend, we want to monitor
1087 * the client's connection and forward any shutdown notification to the
1088 * server, which will decide whether to close or to go on processing the
1089 * request. We only do that in tunnel mode, and not in other modes since
1090 * it can be abused to exhaust source ports. */
Christopher Faulet769d0e92019-03-22 14:23:18 +01001091 if (s->be->options & PR_O_ABRT_CLOSE) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001092 channel_auto_read(req);
Christopher Faulet446d8032022-12-12 07:53:18 +01001093 if ((req->flags & CF_SHUTR) && !(txn->flags & TX_CON_WANT_TUN))
Willy Tarreaucb041662022-05-17 19:44:42 +02001094 s->scb->flags |= SC_FL_NOLINGER;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001095 channel_auto_close(req);
1096 }
1097 else if (s->txn->meth == HTTP_METH_POST) {
1098 /* POST requests may require to read extra CRLF sent by broken
1099 * browsers and which could cause an RST to be sent upon close
1100 * on some systems (eg: Linux). */
1101 channel_auto_read(req);
1102 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001103 DBG_TRACE_DEVEL("waiting for the end of the HTTP txn",
1104 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001105 return 0;
1106
1107 missing_data_or_waiting:
1108 /* stop waiting for data if the input is closed before the end */
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001109 if (msg->msg_state < HTTP_MSG_ENDING && req->flags & CF_SHUTR)
Christopher Faulet93e02d82019-03-08 14:18:50 +01001110 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001111
1112 waiting:
1113 /* waiting for the last bits to leave the buffer */
1114 if (req->flags & CF_SHUTW)
Christopher Faulet93e02d82019-03-08 14:18:50 +01001115 goto return_srv_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001116
1117 /* When TE: chunked is used, we need to get there again to parse remaining
1118 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
1119 * And when content-length is used, we never want to let the possible
1120 * shutdown be forwarded to the other side, as the state machine will
1121 * take care of it once the client responds. It's also important to
1122 * prevent TIME_WAITs from accumulating on the backend side, and for
1123 * HTTP/2 where the last frame comes with a shutdown.
1124 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001125 if (msg->flags & HTTP_MSGF_XFER_LEN)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001126 channel_dont_close(req);
1127
1128 /* We know that more data are expected, but we couldn't send more that
1129 * what we did. So we always set the CF_EXPECT_MORE flag so that the
1130 * system knows it must not set a PUSH on this first part. Interactive
1131 * modes are already handled by the stream sock layer. We must not do
1132 * this in content-length mode because it could present the MSG_MORE
1133 * flag with the last block of forwarded data, which would cause an
1134 * additional delay to be observed by the receiver.
1135 */
Christopher Faulet2151cdd2020-07-22 16:34:59 +02001136 if (HAS_REQ_DATA_FILTERS(s))
Christopher Faulete0768eb2018-10-03 16:38:02 +02001137 req->flags |= CF_EXPECT_MORE;
1138
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001139 DBG_TRACE_DEVEL("waiting for more data to forward",
1140 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001141 return 0;
1142
Christopher Faulet93e02d82019-03-08 14:18:50 +01001143 return_cli_abort:
Willy Tarreau4781b152021-04-06 13:53:36 +02001144 _HA_ATOMIC_INC(&sess->fe->fe_counters.cli_aborts);
1145 _HA_ATOMIC_INC(&s->be->be_counters.cli_aborts);
William Lallemand36119de2021-03-08 15:26:48 +01001146 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001147 _HA_ATOMIC_INC(&sess->listener->counters->cli_aborts);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001148 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001149 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.cli_aborts);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001150 if (!(s->flags & SF_ERR_MASK))
Christopher Fauletf2b02cf2023-01-13 11:02:28 +01001151 s->flags |= ((req->flags & CF_READ_TIMEOUT) ? SF_ERR_CLITO : SF_ERR_CLICL);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001152 status = 400;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001153 goto return_prx_cond;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001154
1155 return_srv_abort:
Willy Tarreau4781b152021-04-06 13:53:36 +02001156 _HA_ATOMIC_INC(&sess->fe->fe_counters.srv_aborts);
1157 _HA_ATOMIC_INC(&s->be->be_counters.srv_aborts);
William Lallemand36119de2021-03-08 15:26:48 +01001158 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001159 _HA_ATOMIC_INC(&sess->listener->counters->srv_aborts);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001160 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001161 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.srv_aborts);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001162 if (!(s->flags & SF_ERR_MASK))
Christopher Fauletf2b02cf2023-01-13 11:02:28 +01001163 s->flags |= ((req->flags & CF_WRITE_TIMEOUT) ? SF_ERR_SRVTO : SF_ERR_SRVCL);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001164 status = 502;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001165 goto return_prx_cond;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001166
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001167 return_int_err:
1168 if (!(s->flags & SF_ERR_MASK))
1169 s->flags |= SF_ERR_INTERNAL;
Willy Tarreau4781b152021-04-06 13:53:36 +02001170 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
1171 _HA_ATOMIC_INC(&s->be->be_counters.internal_errors);
William Lallemand36119de2021-03-08 15:26:48 +01001172 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001173 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001174 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001175 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.internal_errors);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001176 status = 500;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001177 goto return_prx_cond;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001178
Christopher Faulet93e02d82019-03-08 14:18:50 +01001179 return_bad_req:
Willy Tarreau4781b152021-04-06 13:53:36 +02001180 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req);
William Lallemand36119de2021-03-08 15:26:48 +01001181 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001182 _HA_ATOMIC_INC(&sess->listener->counters->failed_req);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001183 status = 400;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001184 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001185
Christopher Fauletb8a53712019-12-16 11:29:38 +01001186 return_prx_cond:
Christopher Faulet9768c262018-10-22 09:34:31 +02001187 if (txn->status > 0) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001188 /* Note: we don't send any error if some data were already sent */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001189 http_reply_and_close(s, txn->status, NULL);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001190 } else {
Christopher Faulet93e02d82019-03-08 14:18:50 +01001191 txn->status = status;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001192 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001193 }
Christopher Fauletf2b02cf2023-01-13 11:02:28 +01001194 http_set_term_flags(s);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001195 DBG_TRACE_DEVEL("leaving on error ",
1196 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001197 return 0;
1198}
1199
Willy Tarreau4596fe22022-05-17 19:07:51 +02001200/* Reset the stream and the backend stream connector to a situation suitable for attemption connection */
Olivier Houcharda254a372019-04-05 15:30:12 +02001201/* Returns 0 if we can attempt to retry, -1 otherwise */
Willy Tarreaub49672d2022-05-27 10:13:37 +02001202static __inline int do_l7_retry(struct stream *s, struct stconn *sc)
Olivier Houcharda254a372019-04-05 15:30:12 +02001203{
Christopher Faulet9f5382e2021-05-21 13:46:14 +02001204 struct channel *req, *res;
1205 int co_data;
Olivier Houcharda254a372019-04-05 15:30:12 +02001206
Christopher Faulet731c8e62022-03-29 16:08:44 +02001207 s->conn_retries++;
1208 if (s->conn_retries >= s->be->conn_retries)
Christopher Faulet552601d2021-05-26 10:31:06 +02001209 return -1;
Christopher Faulet5b82cc52020-10-12 15:18:50 +02001210
Christopher Faulete763c8c2021-05-05 18:23:59 +02001211 if (objt_server(s->target)) {
1212 if (s->flags & SF_CURR_SESS) {
1213 s->flags &= ~SF_CURR_SESS;
1214 _HA_ATOMIC_DEC(&__objt_server(s->target)->cur_sess);
1215 }
Willy Tarreau4781b152021-04-06 13:53:36 +02001216 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.retries);
Christopher Faulete763c8c2021-05-05 18:23:59 +02001217 }
Willy Tarreau4781b152021-04-06 13:53:36 +02001218 _HA_ATOMIC_INC(&s->be->be_counters.retries);
Willy Tarreau223995e2019-05-04 10:38:31 +02001219
Christopher Faulet9f5382e2021-05-21 13:46:14 +02001220 req = &s->req;
1221 res = &s->res;
Olivier Houcharda254a372019-04-05 15:30:12 +02001222 /* Remove any write error from the request, and read error from the response */
1223 req->flags &= ~(CF_WRITE_ERROR | CF_WRITE_TIMEOUT | CF_SHUTW | CF_SHUTW_NOW);
Christopher Faulet6e1bbc42022-12-12 08:08:15 +01001224 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 +01001225 res->analysers &= AN_RES_FLT_END;
Christopher Faulet50264b42022-03-30 19:39:30 +02001226 s->conn_err_type = STRM_ET_NONE;
Christopher Fauletae024ce2022-03-29 19:02:31 +02001227 s->flags &= ~(SF_CONN_EXP | SF_ERR_MASK | SF_FINST_MASK);
1228 s->conn_exp = TICK_ETERNITY;
Olivier Houchard4bd58672019-07-12 16:16:59 +02001229 stream_choose_redispatch(s);
Olivier Houcharda254a372019-04-05 15:30:12 +02001230 res->rex = TICK_ETERNITY;
1231 res->to_forward = 0;
1232 res->analyse_exp = TICK_ETERNITY;
1233 res->total = 0;
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +01001234
Willy Tarreau19c65a92022-05-27 08:49:24 +02001235 if (sc_reset_endp(s->scb) < 0) {
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +01001236 if (!(s->flags & SF_ERR_MASK))
1237 s->flags |= SF_ERR_INTERNAL;
1238 return -1;
1239 }
Olivier Houcharda254a372019-04-05 15:30:12 +02001240
Christopher Faulet9f5382e2021-05-21 13:46:14 +02001241 b_free(&req->buf);
1242 /* Swap the L7 buffer with the channel buffer */
1243 /* We know we stored the co_data as b_data, so get it there */
Christopher Faulete05bf9e2022-03-29 15:23:40 +02001244 co_data = b_data(&s->txn->l7_buffer);
1245 b_set_data(&s->txn->l7_buffer, b_size(&s->txn->l7_buffer));
1246 b_xfer(&req->buf, &s->txn->l7_buffer, b_data(&s->txn->l7_buffer));
Christopher Faulet9f5382e2021-05-21 13:46:14 +02001247 co_set_data(req, co_data);
Christopher Faulet5b82cc52020-10-12 15:18:50 +02001248
Ilya Shipitsinacf84592021-02-06 22:29:08 +05001249 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 +02001250
Olivier Houcharda254a372019-04-05 15:30:12 +02001251 b_reset(&res->buf);
1252 co_set_data(res, 0);
1253 return 0;
1254}
1255
Christopher Faulete0768eb2018-10-03 16:38:02 +02001256/* This stream analyser waits for a complete HTTP response. It returns 1 if the
1257 * processing can continue on next analysers, or zero if it either needs more
1258 * data or wants to immediately abort the response (eg: timeout, error, ...). It
1259 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->res.analysers
1260 * when it has nothing left to do, and may remove any analyser when it wants to
1261 * abort.
1262 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001263int http_wait_for_response(struct stream *s, struct channel *rep, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001264{
Christopher Faulet9768c262018-10-22 09:34:31 +02001265 /*
1266 * We will analyze a complete HTTP response to check the its syntax.
1267 *
1268 * Once the start line and all headers are received, we may perform a
1269 * capture of the error (if any), and we will set a few fields. We also
1270 * logging and finally headers capture.
1271 */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001272 struct session *sess = s->sess;
1273 struct http_txn *txn = s->txn;
1274 struct http_msg *msg = &txn->rsp;
Christopher Faulet9768c262018-10-22 09:34:31 +02001275 struct htx *htx;
Christopher Faulet61608322018-11-23 16:23:45 +01001276 struct connection *srv_conn;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001277 struct htx_sl *sl;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001278 int n;
1279
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001280 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001281
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001282 htx = htxbuf(&rep->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001283
Willy Tarreau4236f032019-03-05 10:43:32 +01001284 /* Parsing errors are caught here */
1285 if (htx->flags & HTX_FL_PARSING_ERROR)
1286 goto return_bad_res;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001287 if (htx->flags & HTX_FL_PROCESSING_ERROR)
1288 goto return_int_err;
Willy Tarreau4236f032019-03-05 10:43:32 +01001289
Christopher Faulete0768eb2018-10-03 16:38:02 +02001290 /*
1291 * Now we quickly check if we have found a full valid response.
1292 * If not so, we check the FD and buffer states before leaving.
1293 * A full response is indicated by the fact that we have seen
1294 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
1295 * responses are checked first.
1296 *
1297 * Depending on whether the client is still there or not, we
1298 * may send an error response back or not. Note that normally
1299 * we should only check for HTTP status there, and check I/O
1300 * errors somewhere else.
1301 */
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001302 next_one:
Christopher Faulet29f17582019-05-23 11:03:26 +02001303 if (unlikely(htx_is_empty(htx) || htx->first == -1)) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001304 /* 1: have we encountered a read error ? */
1305 if (rep->flags & CF_READ_ERROR) {
Willy Tarreaufd9417b2022-05-18 16:23:22 +02001306 struct connection *conn = sc_conn(s->scb);
Olivier Houchard865d8392019-05-03 22:46:27 +02001307
Christopher Fauletd9769232021-05-26 12:15:37 +02001308 /* Perform a L7 retry because server refuses the early data. */
Christopher Faulete05bf9e2022-03-29 15:23:40 +02001309 if ((txn->flags & TX_L7_RETRY) &&
Christopher Fauletd9769232021-05-26 12:15:37 +02001310 (s->be->retry_type & PR_RE_EARLY_ERROR) &&
1311 conn && conn->err_code == CO_ER_SSL_EARLY_FAILED &&
Willy Tarreau7cb9e6c2022-05-17 19:40:40 +02001312 do_l7_retry(s, s->scb) == 0) {
Christopher Fauletd9769232021-05-26 12:15:37 +02001313 DBG_TRACE_DEVEL("leaving on L7 retry",
1314 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
1315 return 0;
1316 }
1317
Olivier Houchard6db16992019-05-17 15:40:49 +02001318 if (txn->flags & TX_NOT_FIRST)
1319 goto abort_keep_alive;
1320
Willy Tarreau4781b152021-04-06 13:53:36 +02001321 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001322 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001323 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001324 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001325 }
1326
Christopher Fauletd9769232021-05-26 12:15:37 +02001327 /* if the server refused the early data, just send a 425 */
1328 if (conn && conn->err_code == CO_ER_SSL_EARLY_FAILED)
Olivier Houchard865d8392019-05-03 22:46:27 +02001329 txn->status = 425;
Christopher Fauletd9769232021-05-26 12:15:37 +02001330 else {
1331 txn->status = 502;
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001332 stream_inc_http_fail_ctr(s);
Christopher Fauletd9769232021-05-26 12:15:37 +02001333 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001334
Willy Tarreaucb041662022-05-17 19:44:42 +02001335 s->scb->flags |= SC_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001336 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001337
1338 if (!(s->flags & SF_ERR_MASK))
1339 s->flags |= SF_ERR_SRVCL;
Christopher Faulet4a66c942023-01-13 09:43:21 +01001340 http_set_term_flags(s);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001341 DBG_TRACE_DEVEL("leaving on error",
1342 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001343 return 0;
1344 }
1345
Christopher Faulet9768c262018-10-22 09:34:31 +02001346 /* 2: read timeout : return a 504 to the client. */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001347 else if (rep->flags & CF_READ_TIMEOUT) {
Christopher Faulete05bf9e2022-03-29 15:23:40 +02001348 if ((txn->flags & TX_L7_RETRY) &&
Olivier Houcharda254a372019-04-05 15:30:12 +02001349 (s->be->retry_type & PR_RE_TIMEOUT)) {
Willy Tarreau7cb9e6c2022-05-17 19:40:40 +02001350 if (co_data(rep) || do_l7_retry(s, s->scb) == 0) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001351 DBG_TRACE_DEVEL("leaving on L7 retry",
1352 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharda254a372019-04-05 15:30:12 +02001353 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001354 }
Olivier Houcharda254a372019-04-05 15:30:12 +02001355 }
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_TIMEOUT);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001360 }
1361
Christopher Faulete0768eb2018-10-03 16:38:02 +02001362 txn->status = 504;
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001363 stream_inc_http_fail_ctr(s);
Willy Tarreaucb041662022-05-17 19:44:42 +02001364 s->scb->flags |= SC_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001365 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001366
1367 if (!(s->flags & SF_ERR_MASK))
1368 s->flags |= SF_ERR_SRVTO;
Christopher Faulet4a66c942023-01-13 09:43:21 +01001369 http_set_term_flags(s);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001370 DBG_TRACE_DEVEL("leaving on error",
1371 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001372 return 0;
1373 }
1374
Christopher Faulet9768c262018-10-22 09:34:31 +02001375 /* 3: client abort with an abortonclose */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001376 else if ((rep->flags & CF_SHUTR) && ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001377 _HA_ATOMIC_INC(&sess->fe->fe_counters.cli_aborts);
1378 _HA_ATOMIC_INC(&s->be->be_counters.cli_aborts);
William Lallemand36119de2021-03-08 15:26:48 +01001379 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001380 _HA_ATOMIC_INC(&sess->listener->counters->cli_aborts);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001381 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001382 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.cli_aborts);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001383
Christopher Faulete0768eb2018-10-03 16:38:02 +02001384 txn->status = 400;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001385 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001386
1387 if (!(s->flags & SF_ERR_MASK))
1388 s->flags |= SF_ERR_CLICL;
Christopher Faulet4a66c942023-01-13 09:43:21 +01001389 http_set_term_flags(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001390
1391 /* process_stream() will take care of the error */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001392 DBG_TRACE_DEVEL("leaving on error",
1393 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001394 return 0;
1395 }
1396
Christopher Faulet9768c262018-10-22 09:34:31 +02001397 /* 4: close from server, capture the response if the server has started to respond */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001398 else if (rep->flags & CF_SHUTR) {
Christopher Faulete05bf9e2022-03-29 15:23:40 +02001399 if ((txn->flags & TX_L7_RETRY) &&
Olivier Houcharda254a372019-04-05 15:30:12 +02001400 (s->be->retry_type & PR_RE_DISCONNECTED)) {
Willy Tarreau7cb9e6c2022-05-17 19:40:40 +02001401 if (co_data(rep) || do_l7_retry(s, s->scb) == 0) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001402 DBG_TRACE_DEVEL("leaving on L7 retry",
1403 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharda254a372019-04-05 15:30:12 +02001404 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001405 }
Olivier Houcharda254a372019-04-05 15:30:12 +02001406 }
1407
Olivier Houchard6db16992019-05-17 15:40:49 +02001408 if (txn->flags & TX_NOT_FIRST)
1409 goto abort_keep_alive;
1410
Willy Tarreau4781b152021-04-06 13:53:36 +02001411 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001412 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001413 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001414 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001415 }
1416
Christopher Faulete0768eb2018-10-03 16:38:02 +02001417 txn->status = 502;
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001418 stream_inc_http_fail_ctr(s);
Willy Tarreaucb041662022-05-17 19:44:42 +02001419 s->scb->flags |= SC_FL_NOLINGER;
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_SRVCL;
Christopher Faulet4a66c942023-01-13 09:43:21 +01001424 http_set_term_flags(s);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001425 DBG_TRACE_DEVEL("leaving on error",
1426 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001427 return 0;
1428 }
1429
Christopher Faulet9768c262018-10-22 09:34:31 +02001430 /* 5: write error to client (we don't send any message then) */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001431 else if (rep->flags & CF_WRITE_ERROR) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001432 if (txn->flags & TX_NOT_FIRST)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001433 goto abort_keep_alive;
1434
Willy Tarreau4781b152021-04-06 13:53:36 +02001435 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001436 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001437 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001438 rep->analysers &= AN_RES_FLT_END;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001439
1440 if (!(s->flags & SF_ERR_MASK))
1441 s->flags |= SF_ERR_CLICL;
Christopher Faulet4a66c942023-01-13 09:43:21 +01001442 http_set_term_flags(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001443
1444 /* process_stream() will take care of the error */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001445 DBG_TRACE_DEVEL("leaving on error",
1446 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001447 return 0;
1448 }
1449
1450 channel_dont_close(rep);
1451 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001452 DBG_TRACE_DEVEL("waiting for more data",
1453 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001454 return 0;
1455 }
1456
1457 /* More interesting part now : we know that we have a complete
1458 * response which at least looks like HTTP. We have an indicator
1459 * of each header's length, so we can parse them quickly.
1460 */
Christopher Faulet29f17582019-05-23 11:03:26 +02001461 BUG_ON(htx_get_first_type(htx) != HTX_BLK_RES_SL);
Christopher Faulet297fbb42019-05-13 14:41:27 +02001462 sl = http_get_stline(htx);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001463
Christopher Faulet1f08bff2021-05-26 13:14:39 +02001464 /* Perform a L7 retry because of the status code */
Christopher Faulete05bf9e2022-03-29 15:23:40 +02001465 if ((txn->flags & TX_L7_RETRY) &&
Christopher Faulet1f08bff2021-05-26 13:14:39 +02001466 l7_status_match(s->be, sl->info.res.status) &&
Willy Tarreau7cb9e6c2022-05-17 19:40:40 +02001467 do_l7_retry(s, s->scb) == 0) {
Christopher Faulet1f08bff2021-05-26 13:14:39 +02001468 DBG_TRACE_DEVEL("leaving on L7 retry", STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
1469 return 0;
1470 }
1471
1472 /* Now, L7 buffer is useless, it can be released */
Christopher Faulete05bf9e2022-03-29 15:23:40 +02001473 b_free(&txn->l7_buffer);
Christopher Faulet1f08bff2021-05-26 13:14:39 +02001474
1475 msg->msg_state = HTTP_MSG_BODY;
1476
1477
Christopher Faulet9768c262018-10-22 09:34:31 +02001478 /* 0: we might have to print this header in debug mode */
1479 if (unlikely((global.mode & MODE_DEBUG) &&
1480 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
1481 int32_t pos;
1482
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001483 http_debug_stline("srvrep", s, sl);
Christopher Faulet9768c262018-10-22 09:34:31 +02001484
Christopher Fauleta3f15502019-05-13 15:27:23 +02001485 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001486 struct htx_blk *blk = htx_get_blk(htx, pos);
1487 enum htx_blk_type type = htx_get_blk_type(blk);
1488
1489 if (type == HTX_BLK_EOH)
1490 break;
1491 if (type != HTX_BLK_HDR)
1492 continue;
1493
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001494 http_debug_hdr("srvhdr", s,
1495 htx_get_blk_name(htx, blk),
1496 htx_get_blk_value(htx, blk));
Christopher Faulet9768c262018-10-22 09:34:31 +02001497 }
1498 }
1499
Christopher Faulet03599112018-11-27 11:21:21 +01001500 /* 1: get the status code and the version. Also set HTTP flags */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001501 txn->status = sl->info.res.status;
Christopher Faulet03599112018-11-27 11:21:21 +01001502 if (sl->flags & HTX_SL_F_VER_11)
Christopher Faulet9768c262018-10-22 09:34:31 +02001503 msg->flags |= HTTP_MSGF_VER_11;
Christopher Faulet03599112018-11-27 11:21:21 +01001504 if (sl->flags & HTX_SL_F_XFER_LEN) {
1505 msg->flags |= HTTP_MSGF_XFER_LEN;
Christopher Faulet2a408542020-11-20 14:22:37 +01001506 if (sl->flags & HTX_SL_F_CLEN)
1507 msg->flags |= HTTP_MSGF_CNT_LEN;
1508 else if (sl->flags & HTX_SL_F_CHNK)
1509 msg->flags |= HTTP_MSGF_TE_CHNK;
Christopher Faulet03599112018-11-27 11:21:21 +01001510 }
Christopher Faulet2a408542020-11-20 14:22:37 +01001511 if (sl->flags & HTX_SL_F_BODYLESS)
1512 msg->flags |= HTTP_MSGF_BODYLESS;
Christopher Faulet576c3582021-01-08 15:53:01 +01001513 if (sl->flags & HTX_SL_F_CONN_UPG)
1514 msg->flags |= HTTP_MSGF_CONN_UPG;
Christopher Faulet9768c262018-10-22 09:34:31 +02001515
1516 n = txn->status / 100;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001517 if (n < 1 || n > 5)
1518 n = 0;
Christopher Faulet9768c262018-10-22 09:34:31 +02001519
Christopher Faulete0768eb2018-10-03 16:38:02 +02001520 /* when the client triggers a 4xx from the server, it's most often due
1521 * to a missing object or permission. These events should be tracked
1522 * because if they happen often, it may indicate a brute force or a
1523 * vulnerability scan.
1524 */
1525 if (n == 4)
1526 stream_inc_http_err_ctr(s);
1527
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001528 if (n == 5 && txn->status != 501 && txn->status != 505)
1529 stream_inc_http_fail_ctr(s);
1530
Marcin Deranek3c27dda2020-05-15 18:32:51 +02001531 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001532 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.p.http.rsp[n]);
1533 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.p.http.cum_req);
Marcin Deranek3c27dda2020-05-15 18:32:51 +02001534 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001535
Christopher Faulete0768eb2018-10-03 16:38:02 +02001536 /* Adjust server's health based on status code. Note: status codes 501
1537 * and 505 are triggered on demand by client request, so we must not
1538 * count them as server failures.
1539 */
1540 if (objt_server(s->target)) {
1541 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001542 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_OK);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001543 else
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001544 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_STS);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001545 }
1546
1547 /*
1548 * We may be facing a 100-continue response, or any other informational
1549 * 1xx response which is non-final, in which case this is not the right
1550 * response, and we're waiting for the next one. Let's allow this response
1551 * to go to the client and wait for the next one. There's an exception for
1552 * 101 which is used later in the code to switch protocols.
1553 */
1554 if (txn->status < 200 &&
1555 (txn->status == 100 || txn->status >= 102)) {
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001556 FLT_STRM_CB(s, flt_http_reset(s, msg));
Christopher Faulet421e7692019-06-13 11:16:45 +02001557 htx->first = channel_htx_fwd_headers(rep, htx);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001558 msg->msg_state = HTTP_MSG_RPBEFORE;
Christopher Faulet3499f622019-09-03 15:23:54 +02001559 msg->flags = 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001560 txn->status = 0;
1561 s->logs.t_data = -1; /* was not a response yet */
Christopher Faulet7d518452020-08-31 11:07:07 +02001562 rep->flags |= CF_SEND_DONTWAIT; /* Send ASAP informational messages */
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001563 goto next_one;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001564 }
1565
Christopher Faulet6e6c7b12021-01-08 16:02:05 +01001566 /* A 101-switching-protocols must contains a Connection header with the
1567 * "upgrade" option and the request too. It means both are agree to
1568 * upgrade. It is not so strict because there is no test on the Upgrade
1569 * header content. But it is probably stronger enough for now.
1570 */
1571 if (txn->status == 101 &&
1572 (!(txn->req.flags & HTTP_MSGF_CONN_UPG) || !(txn->rsp.flags & HTTP_MSGF_CONN_UPG)))
1573 goto return_bad_res;
1574
Christopher Faulete0768eb2018-10-03 16:38:02 +02001575 /*
1576 * 2: check for cacheability.
1577 */
1578
1579 switch (txn->status) {
1580 case 200:
1581 case 203:
1582 case 204:
1583 case 206:
1584 case 300:
1585 case 301:
1586 case 404:
1587 case 405:
1588 case 410:
1589 case 414:
1590 case 501:
1591 break;
1592 default:
1593 /* RFC7231#6.1:
1594 * Responses with status codes that are defined as
1595 * cacheable by default (e.g., 200, 203, 204, 206,
1596 * 300, 301, 404, 405, 410, 414, and 501 in this
1597 * specification) can be reused by a cache with
1598 * heuristic expiration unless otherwise indicated
1599 * by the method definition or explicit cache
1600 * controls [RFC7234]; all other status codes are
1601 * not cacheable by default.
1602 */
1603 txn->flags &= ~(TX_CACHEABLE | TX_CACHE_COOK);
1604 break;
1605 }
1606
1607 /*
1608 * 3: we may need to capture headers
1609 */
1610 s->logs.logwait &= ~LW_RESP;
1611 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->res_cap))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001612 http_capture_headers(htx, s->res_cap, sess->fe->rsp_cap);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001613
Christopher Faulet9768c262018-10-22 09:34:31 +02001614 /* Skip parsing if no content length is possible. */
Christopher Fauletc75668e2020-12-07 18:10:32 +01001615 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status >= 200 && txn->status < 300) ||
Christopher Faulete0768eb2018-10-03 16:38:02 +02001616 txn->status == 101)) {
1617 /* Either we've established an explicit tunnel, or we're
1618 * switching the protocol. In both cases, we're very unlikely
1619 * to understand the next protocols. We have to switch to tunnel
1620 * mode, so that we transfer the request and responses then let
1621 * this protocol pass unmodified. When we later implement specific
1622 * parsers for such protocols, we'll want to check the Upgrade
1623 * header which contains information about that protocol for
1624 * responses with status 101 (eg: see RFC2817 about TLS).
1625 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02001626 txn->flags |= TX_CON_WANT_TUN;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001627 }
1628
Christopher Faulet61608322018-11-23 16:23:45 +01001629 /* check for NTML authentication headers in 401 (WWW-Authenticate) and
1630 * 407 (Proxy-Authenticate) responses and set the connection to private
1631 */
Willy Tarreaufd9417b2022-05-18 16:23:22 +02001632 srv_conn = sc_conn(s->scb);
Christopher Faulet61608322018-11-23 16:23:45 +01001633 if (srv_conn) {
1634 struct ist hdr;
1635 struct http_hdr_ctx ctx;
1636
1637 if (txn->status == 401)
1638 hdr = ist("WWW-Authenticate");
1639 else if (txn->status == 407)
1640 hdr = ist("Proxy-Authenticate");
1641 else
1642 goto end;
1643
1644 ctx.blk = NULL;
1645 while (http_find_header(htx, hdr, &ctx, 0)) {
Willy Tarreauf1dcced2020-05-07 19:27:02 +02001646 /* If www-authenticate contains "Negotiate", "Nego2", or "NTLM",
1647 * possibly followed by blanks and a base64 string, the connection
1648 * is private. Since it's a mess to deal with, we only check for
1649 * values starting with "NTLM" or "Nego". Note that often multiple
1650 * headers are sent by the server there.
1651 */
1652 if ((ctx.value.len >= 4 && strncasecmp(ctx.value.ptr, "Nego", 4) == 0) ||
Willy Tarreau49a1d282020-05-07 19:10:15 +02001653 (ctx.value.len >= 4 && strncasecmp(ctx.value.ptr, "NTLM", 4) == 0)) {
Olivier Houchard250031e2019-05-29 15:01:50 +02001654 sess->flags |= SESS_FL_PREFER_LAST;
Christopher Faulet08016ab2020-07-01 16:10:06 +02001655 conn_set_owner(srv_conn, sess, NULL);
Christopher Faulet21ddc742020-07-01 15:26:14 +02001656 conn_set_private(srv_conn);
Ilya Shipitsin6b79f382020-07-23 00:32:55 +05001657 /* If it fail now, the same will be done in mux->detach() callback */
Christopher Faulet08016ab2020-07-01 16:10:06 +02001658 session_add_conn(srv_conn->owner, srv_conn, srv_conn->target);
Willy Tarreauf1dcced2020-05-07 19:27:02 +02001659 break;
Olivier Houchard250031e2019-05-29 15:01:50 +02001660 }
Christopher Faulet61608322018-11-23 16:23:45 +01001661 }
1662 }
1663
1664 end:
Christopher Faulete0768eb2018-10-03 16:38:02 +02001665 /* we want to have the response time before we start processing it */
1666 s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now);
1667
1668 /* end of job, return OK */
1669 rep->analysers &= ~an_bit;
1670 rep->analyse_exp = TICK_ETERNITY;
1671 channel_auto_close(rep);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001672 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001673 return 1;
1674
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001675 return_int_err:
Willy Tarreau4781b152021-04-06 13:53:36 +02001676 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
1677 _HA_ATOMIC_INC(&s->be->be_counters.internal_errors);
William Lallemand36119de2021-03-08 15:26:48 +01001678 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001679 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Fauletb8a53712019-12-16 11:29:38 +01001680 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001681 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.internal_errors);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001682 txn->status = 500;
1683 if (!(s->flags & SF_ERR_MASK))
1684 s->flags |= SF_ERR_INTERNAL;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001685 goto return_prx_cond;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001686
1687 return_bad_res:
Willy Tarreau4781b152021-04-06 13:53:36 +02001688 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Faulet47365272018-10-31 17:40:50 +01001689 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001690 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001691 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Christopher Faulet47365272018-10-31 17:40:50 +01001692 }
Olivier Houcharde3249a92019-05-03 23:01:47 +02001693 if ((s->be->retry_type & PR_RE_JUNK_REQUEST) &&
Christopher Faulete05bf9e2022-03-29 15:23:40 +02001694 (txn->flags & TX_L7_RETRY) &&
Willy Tarreau7cb9e6c2022-05-17 19:40:40 +02001695 do_l7_retry(s, s->scb) == 0) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001696 DBG_TRACE_DEVEL("leaving on L7 retry",
1697 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharde3249a92019-05-03 23:01:47 +02001698 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001699 }
Christopher Faulet47365272018-10-31 17:40:50 +01001700 txn->status = 502;
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001701 stream_inc_http_fail_ctr(s);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001702 /* fall through */
1703
Christopher Fauletb8a53712019-12-16 11:29:38 +01001704 return_prx_cond:
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001705 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet4a66c942023-01-13 09:43:21 +01001706 http_set_term_flags(s);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001707
Willy Tarreaucb041662022-05-17 19:44:42 +02001708 s->scb->flags |= SC_FL_NOLINGER;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001709 DBG_TRACE_DEVEL("leaving on error",
1710 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulet47365272018-10-31 17:40:50 +01001711 return 0;
1712
Christopher Faulete0768eb2018-10-03 16:38:02 +02001713 abort_keep_alive:
1714 /* A keep-alive request to the server failed on a network error.
1715 * The client is required to retry. We need to close without returning
1716 * any other information so that the client retries.
1717 */
1718 txn->status = 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001719 s->logs.logwait = 0;
1720 s->logs.level = 0;
1721 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001722 http_reply_and_close(s, txn->status, NULL);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001723 DBG_TRACE_DEVEL("leaving by closing K/A connection",
1724 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001725 return 0;
1726}
1727
1728/* This function performs all the processing enabled for the current response.
1729 * It normally returns 1 unless it wants to break. It relies on buffers flags,
1730 * and updates s->res.analysers. It might make sense to explode it into several
1731 * other functions. It works like process_request (see indications above).
1732 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001733int http_process_res_common(struct stream *s, struct channel *rep, int an_bit, struct proxy *px)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001734{
1735 struct session *sess = s->sess;
1736 struct http_txn *txn = s->txn;
1737 struct http_msg *msg = &txn->rsp;
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001738 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001739 struct proxy *cur_proxy;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001740 enum rule_result ret = HTTP_RULE_RES_CONT;
1741
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001742 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
1743 return 0;
Christopher Faulet9768c262018-10-22 09:34:31 +02001744
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001745 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001746
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001747 htx = htxbuf(&rep->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001748
1749 /* The stats applet needs to adjust the Connection header but we don't
1750 * apply any filter there.
1751 */
1752 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
1753 rep->analysers &= ~an_bit;
1754 rep->analyse_exp = TICK_ETERNITY;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001755 goto end;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001756 }
1757
1758 /*
1759 * We will have to evaluate the filters.
1760 * As opposed to version 1.2, now they will be evaluated in the
1761 * filters order and not in the header order. This means that
1762 * each filter has to be validated among all headers.
1763 *
1764 * Filters are tried with ->be first, then with ->fe if it is
1765 * different from ->be.
1766 *
1767 * Maybe we are in resume condiion. In this case I choose the
1768 * "struct proxy" which contains the rule list matching the resume
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05001769 * pointer. If none of these "struct proxy" match, I initialise
Christopher Faulete0768eb2018-10-03 16:38:02 +02001770 * the process with the first one.
1771 *
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05001772 * In fact, I check only correspondence between the current list
Christopher Faulete0768eb2018-10-03 16:38:02 +02001773 * pointer and the ->fe rule list. If it doesn't match, I initialize
1774 * the loop with the ->be.
1775 */
Christopher Fauletd4150ad2021-10-13 15:35:55 +02001776 if (s->current_rule_list == &sess->fe->http_res_rules ||
1777 (sess->fe->defpx && s->current_rule_list == &sess->fe->defpx->http_res_rules))
Christopher Faulete0768eb2018-10-03 16:38:02 +02001778 cur_proxy = sess->fe;
1779 else
1780 cur_proxy = s->be;
Christopher Fauletd4150ad2021-10-13 15:35:55 +02001781
Christopher Faulete0768eb2018-10-03 16:38:02 +02001782 while (1) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001783 /* evaluate http-response rules */
Christopher Faulet46f46df2021-11-09 16:33:25 +01001784 if (ret == HTTP_RULE_RES_CONT || ret == HTTP_RULE_RES_STOP) {
Christopher Fauletd4150ad2021-10-13 15:35:55 +02001785 struct list *def_rules, *rules;
1786
1787 def_rules = ((cur_proxy->defpx && (cur_proxy == s->be || cur_proxy->defpx != s->be->defpx)) ? &cur_proxy->defpx->http_res_rules : NULL);
1788 rules = &cur_proxy->http_res_rules;
1789
1790 ret = http_res_get_intercept_rule(cur_proxy, def_rules, rules, s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001791
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001792 switch (ret) {
1793 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
1794 goto return_prx_yield;
1795
1796 case HTTP_RULE_RES_CONT:
1797 case HTTP_RULE_RES_STOP: /* nothing to do */
1798 break;
1799
1800 case HTTP_RULE_RES_DENY: /* deny or tarpit */
1801 goto deny;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001802
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001803 case HTTP_RULE_RES_ABRT: /* abort request, response already sent */
1804 goto return_prx_cond;
1805
1806 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
Christopher Fauletb8a53712019-12-16 11:29:38 +01001807 goto done;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001808
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001809 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
1810 goto return_bad_res;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001811
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001812 case HTTP_RULE_RES_ERROR: /* failed with a bad request */
1813 goto return_int_err;
1814 }
1815
1816 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001817
Christopher Faulete0768eb2018-10-03 16:38:02 +02001818 /* check whether we're already working on the frontend */
1819 if (cur_proxy == sess->fe)
1820 break;
1821 cur_proxy = sess->fe;
1822 }
1823
Christopher Faulete0768eb2018-10-03 16:38:02 +02001824 /* OK that's all we can do for 1xx responses */
1825 if (unlikely(txn->status < 200 && txn->status != 101))
Christopher Fauletf2824e62018-10-01 12:12:37 +02001826 goto end;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001827
1828 /*
1829 * Now check for a server cookie.
1830 */
1831 if (s->be->cookie_name || sess->fe->capture_name || (s->be->options & PR_O_CHK_CACHE))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001832 http_manage_server_side_cookies(s, rep);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001833
1834 /*
1835 * Check for cache-control or pragma headers if required.
1836 */
1837 if ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001838 http_check_response_for_cacheability(s, rep);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001839
1840 /*
1841 * Add server cookie in the response if needed
1842 */
1843 if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) &&
1844 !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) &&
1845 (!(s->flags & SF_DIRECT) ||
1846 ((s->be->cookie_maxidle || txn->cookie_last_date) &&
1847 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
1848 (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
1849 (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
1850 (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
1851 !(s->flags & SF_IGNORE_PRST)) {
1852 /* the server is known, it's not the one the client requested, or the
1853 * cookie's last seen date needs to be refreshed. We have to
1854 * insert a set-cookie here, except if we want to insert only on POST
1855 * requests and this one isn't. Note that servers which don't have cookies
1856 * (eg: some backup servers) will return a full cookie removal request.
1857 */
Willy Tarreau88bc8002021-12-06 07:01:02 +00001858 if (!__objt_server(s->target)->cookie) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001859 chunk_printf(&trash,
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001860 "%s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
Christopher Faulete0768eb2018-10-03 16:38:02 +02001861 s->be->cookie_name);
1862 }
1863 else {
Willy Tarreau88bc8002021-12-06 07:01:02 +00001864 chunk_printf(&trash, "%s=%s", s->be->cookie_name, __objt_server(s->target)->cookie);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001865
1866 if (s->be->cookie_maxidle || s->be->cookie_maxlife) {
1867 /* emit last_date, which is mandatory */
1868 trash.area[trash.data++] = COOKIE_DELIM_DATE;
1869 s30tob64((date.tv_sec+3) >> 2,
1870 trash.area + trash.data);
1871 trash.data += 5;
1872
1873 if (s->be->cookie_maxlife) {
1874 /* emit first_date, which is either the original one or
1875 * the current date.
1876 */
1877 trash.area[trash.data++] = COOKIE_DELIM_DATE;
1878 s30tob64(txn->cookie_first_date ?
1879 txn->cookie_first_date >> 2 :
1880 (date.tv_sec+3) >> 2,
1881 trash.area + trash.data);
1882 trash.data += 5;
1883 }
1884 }
1885 chunk_appendf(&trash, "; path=/");
1886 }
1887
1888 if (s->be->cookie_domain)
1889 chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain);
1890
1891 if (s->be->ck_opts & PR_CK_HTTPONLY)
1892 chunk_appendf(&trash, "; HttpOnly");
1893
1894 if (s->be->ck_opts & PR_CK_SECURE)
1895 chunk_appendf(&trash, "; Secure");
1896
Christopher Faulet2f533902020-01-21 11:06:48 +01001897 if (s->be->cookie_attrs)
1898 chunk_appendf(&trash, "; %s", s->be->cookie_attrs);
1899
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001900 if (unlikely(!http_add_header(htx, ist("Set-Cookie"), ist2(trash.area, trash.data))))
Christopher Fauletd649b572022-06-01 17:42:35 +02001901 goto return_fail_rewrite;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001902
1903 txn->flags &= ~TX_SCK_MASK;
1904 if (__objt_server(s->target)->cookie && (s->flags & SF_DIRECT))
1905 /* the server did not change, only the date was updated */
1906 txn->flags |= TX_SCK_UPDATED;
1907 else
1908 txn->flags |= TX_SCK_INSERTED;
1909
1910 /* Here, we will tell an eventual cache on the client side that we don't
1911 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
1912 * Some caches understand the correct form: 'no-cache="set-cookie"', but
1913 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
1914 */
1915 if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
1916
1917 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
1918
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001919 if (unlikely(!http_add_header(htx, ist("Cache-control"), ist("private"))))
Christopher Fauletd649b572022-06-01 17:42:35 +02001920 goto return_fail_rewrite;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001921 }
1922 }
1923
1924 /*
1925 * Check if result will be cacheable with a cookie.
1926 * We'll block the response if security checks have caught
1927 * nasty things such as a cacheable cookie.
1928 */
1929 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
1930 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
1931 (s->be->options & PR_O_CHK_CACHE)) {
1932 /* we're in presence of a cacheable response containing
1933 * a set-cookie header. We'll block it as requested by
1934 * the 'checkcache' option, and send an alert.
1935 */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001936 ha_alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreau88bc8002021-12-06 07:01:02 +00001937 s->be->id, objt_server(s->target) ? __objt_server(s->target)->id : "<dispatch>");
Christopher Faulete0768eb2018-10-03 16:38:02 +02001938 send_log(s->be, LOG_ALERT,
1939 "Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreau88bc8002021-12-06 07:01:02 +00001940 s->be->id, objt_server(s->target) ? __objt_server(s->target)->id : "<dispatch>");
Christopher Fauletb8a53712019-12-16 11:29:38 +01001941 goto deny;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001942 }
1943
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001944 end:
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01001945 /*
1946 * Evaluate after-response rules before forwarding the response. rules
1947 * from the backend are evaluated first, then one from the frontend if
1948 * it differs.
1949 */
1950 if (!http_eval_after_res_rules(s))
1951 goto return_int_err;
1952
Christopher Fauletc2ac5e42021-03-08 18:20:09 +01001953 /* Filter the response headers if there are filters attached to the
1954 * stream.
1955 */
1956 if (HAS_FILTERS(s))
1957 rep->analysers |= AN_RES_FLT_HTTP_HDRS;
1958
Christopher Faulete0768eb2018-10-03 16:38:02 +02001959 /* Always enter in the body analyzer */
1960 rep->analysers &= ~AN_RES_FLT_XFER_DATA;
1961 rep->analysers |= AN_RES_HTTP_XFER_BODY;
1962
1963 /* if the user wants to log as soon as possible, without counting
1964 * bytes from the server, then this is the right moment. We have
1965 * to temporarily assign bytes_out to log what we currently have.
1966 */
1967 if (!LIST_ISEMPTY(&sess->fe->logformat) && !(s->logs.logwait & LW_BYTES)) {
1968 s->logs.t_close = s->logs.t_data; /* to get a valid end date */
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001969 s->logs.bytes_out = htx->data;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001970 s->do_log(s);
1971 s->logs.bytes_out = 0;
1972 }
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001973
Christopher Fauletb8a53712019-12-16 11:29:38 +01001974 done:
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01001975 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletb8a53712019-12-16 11:29:38 +01001976 rep->analysers &= ~an_bit;
1977 rep->analyse_exp = TICK_ETERNITY;
Christopher Fauletd4150ad2021-10-13 15:35:55 +02001978 s->current_rule = s->current_rule_list = NULL;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001979 return 1;
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001980
Christopher Fauletb8a53712019-12-16 11:29:38 +01001981 deny:
Willy Tarreau4781b152021-04-06 13:53:36 +02001982 _HA_ATOMIC_INC(&sess->fe->fe_counters.denied_resp);
1983 _HA_ATOMIC_INC(&s->be->be_counters.denied_resp);
William Lallemand36119de2021-03-08 15:26:48 +01001984 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001985 _HA_ATOMIC_INC(&sess->listener->counters->denied_resp);
Christopher Fauleta08546b2019-12-16 16:07:34 +01001986 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001987 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.denied_resp);
Christopher Fauletb8a53712019-12-16 11:29:38 +01001988 goto return_prx_err;
1989
Christopher Fauletd649b572022-06-01 17:42:35 +02001990 return_fail_rewrite:
1991 if (!(s->flags & SF_ERR_MASK))
1992 s->flags |= SF_ERR_PRXCOND;
1993 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_rewrites);
1994 _HA_ATOMIC_INC(&s->be->be_counters.failed_rewrites);
1995 if (sess->listener && sess->listener->counters)
1996 _HA_ATOMIC_INC(&sess->listener->counters->failed_rewrites);
1997 if (objt_server(s->target))
1998 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_rewrites);
1999 /* fall through */
2000
Christopher Fauletb8a53712019-12-16 11:29:38 +01002001 return_int_err:
2002 txn->status = 500;
2003 if (!(s->flags & SF_ERR_MASK))
2004 s->flags |= SF_ERR_INTERNAL;
Willy Tarreau4781b152021-04-06 13:53:36 +02002005 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
2006 _HA_ATOMIC_INC(&s->be->be_counters.internal_errors);
Dragan Dosen9a006f92021-09-21 13:02:09 +02002007 if (sess->listener && sess->listener->counters)
2008 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Fauletcff0f732019-12-16 16:13:44 +01002009 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02002010 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.internal_errors);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002011 goto return_prx_err;
2012
2013 return_bad_res:
2014 txn->status = 502;
Willy Tarreau826f3ab2021-02-10 12:07:15 +01002015 stream_inc_http_fail_ctr(s);
Willy Tarreau4781b152021-04-06 13:53:36 +02002016 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Fauleta20a6532020-02-05 10:16:41 +01002017 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02002018 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Christopher Fauleta20a6532020-02-05 10:16:41 +01002019 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_RSP);
2020 }
Christopher Fauletb8a53712019-12-16 11:29:38 +01002021 /* fall through */
2022
2023 return_prx_err:
2024 http_reply_and_close(s, txn->status, http_error_message(s));
2025 /* fall through */
2026
2027 return_prx_cond:
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002028 s->logs.t_data = -1; /* was not a valid response */
Willy Tarreaucb041662022-05-17 19:44:42 +02002029 s->scb->flags |= SC_FL_NOLINGER;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002030
Christopher Faulet4a66c942023-01-13 09:43:21 +01002031 http_set_term_flags(s);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002032
Christopher Faulete58c0002020-03-02 16:21:01 +01002033 rep->analysers &= AN_RES_FLT_END;
2034 s->req.analysers &= AN_REQ_FLT_END;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002035 rep->analyse_exp = TICK_ETERNITY;
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002036 s->current_rule = s->current_rule_list = NULL;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002037 DBG_TRACE_DEVEL("leaving on error",
2038 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002039 return 0;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002040
2041 return_prx_yield:
2042 channel_dont_close(rep);
2043 DBG_TRACE_DEVEL("waiting for more data",
2044 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
2045 return 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002046}
2047
2048/* This function is an analyser which forwards response body (including chunk
2049 * sizes if any). It is called as soon as we must forward, even if we forward
2050 * zero byte. The only situation where it must not be called is when we're in
2051 * tunnel mode and we want to forward till the close. It's used both to forward
2052 * remaining data and to resync after end of body. It expects the msg_state to
2053 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
2054 * read more data, or 1 once we can go on with next request or end the stream.
2055 *
2056 * It is capable of compressing response data both in content-length mode and
2057 * in chunked mode. The state machines follows different flows depending on
2058 * whether content-length and chunked modes are used, since there are no
2059 * trailers in content-length :
2060 *
2061 * chk-mode cl-mode
2062 * ,----- BODY -----.
2063 * / \
2064 * V size > 0 V chk-mode
2065 * .--> SIZE -------------> DATA -------------> CRLF
2066 * | | size == 0 | last byte |
2067 * | v final crlf v inspected |
2068 * | TRAILERS -----------> DONE |
2069 * | |
2070 * `----------------------------------------------'
2071 *
2072 * Compression only happens in the DATA state, and must be flushed in final
2073 * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding
2074 * is performed at once on final states for all bytes parsed, or when leaving
2075 * on missing data.
2076 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002077int http_response_forward_body(struct stream *s, struct channel *res, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +02002078{
2079 struct session *sess = s->sess;
2080 struct http_txn *txn = s->txn;
2081 struct http_msg *msg = &s->txn->rsp;
Christopher Faulet9768c262018-10-22 09:34:31 +02002082 struct htx *htx;
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002083 int ret;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002084
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002085 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002086
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002087 htx = htxbuf(&res->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002088
Christopher Fauletb9a92f32019-09-09 10:15:21 +02002089 if (htx->flags & HTX_FL_PARSING_ERROR)
2090 goto return_bad_res;
2091 if (htx->flags & HTX_FL_PROCESSING_ERROR)
2092 goto return_int_err;
2093
Christopher Faulet9768c262018-10-22 09:34:31 +02002094 if (msg->msg_state == HTTP_MSG_BODY)
2095 msg->msg_state = HTTP_MSG_DATA;
2096
Christopher Faulete0768eb2018-10-03 16:38:02 +02002097 /* in most states, we should abort in case of early close */
2098 channel_auto_close(res);
2099
Christopher Faulete0768eb2018-10-03 16:38:02 +02002100 if (res->to_forward) {
Christopher Faulet66af0b22019-03-22 14:54:52 +01002101 if (res->to_forward == CHN_INFINITE_FORWARD) {
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002102 if (res->flags & CF_EOI)
2103 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet66af0b22019-03-22 14:54:52 +01002104 }
2105 else {
2106 /* We can't process the buffer's contents yet */
2107 res->flags |= CF_WAKE_WRITE;
2108 goto missing_data_or_waiting;
2109 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02002110 }
2111
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002112 if (msg->msg_state >= HTTP_MSG_ENDING)
2113 goto ending;
2114
Christopher Fauletc75668e2020-12-07 18:10:32 +01002115 if ((txn->meth == HTTP_METH_CONNECT && txn->status >= 200 && txn->status < 300) || txn->status == 101 ||
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002116 (!(msg->flags & HTTP_MSGF_XFER_LEN) && !HAS_RSP_DATA_FILTERS(s))) {
2117 msg->msg_state = HTTP_MSG_ENDING;
2118 goto ending;
2119 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002120
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002121 /* Forward input data. We get it by removing all outgoing data not
2122 * forwarded yet from HTX data size. If there are some data filters, we
2123 * let them decide the amount of data to forward.
Christopher Faulet9768c262018-10-22 09:34:31 +02002124 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002125 if (HAS_RSP_DATA_FILTERS(s)) {
2126 ret = flt_http_payload(s, msg, htx->data);
2127 if (ret < 0)
2128 goto return_bad_res;
Christopher Faulet421e7692019-06-13 11:16:45 +02002129 c_adv(res, ret);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002130 }
2131 else {
Christopher Faulet421e7692019-06-13 11:16:45 +02002132 c_adv(res, htx->data - co_data(res));
Christopher Faulet66af0b22019-03-22 14:54:52 +01002133 if (msg->flags & HTTP_MSGF_XFER_LEN)
2134 channel_htx_forward_forever(res, htx);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002135 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002136
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002137 if (htx->data != co_data(res))
2138 goto missing_data_or_waiting;
2139
2140 if (!(msg->flags & HTTP_MSGF_XFER_LEN) && res->flags & CF_SHUTR) {
2141 msg->msg_state = HTTP_MSG_ENDING;
2142 goto ending;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002143 }
2144
Christopher Faulet9768c262018-10-22 09:34:31 +02002145 /* Check if the end-of-message is reached and if so, switch the message
Christopher Fauletd20fdb02019-06-13 16:43:22 +02002146 * in HTTP_MSG_ENDING state. Then if all data was marked to be
2147 * forwarded, set the state to HTTP_MSG_DONE.
Christopher Faulet9768c262018-10-22 09:34:31 +02002148 */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002149 if (!(htx->flags & HTX_FL_EOM))
Christopher Faulet9768c262018-10-22 09:34:31 +02002150 goto missing_data_or_waiting;
2151
Christopher Fauletd20fdb02019-06-13 16:43:22 +02002152 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet9768c262018-10-22 09:34:31 +02002153
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002154 ending:
Christopher Faulet2151cdd2020-07-22 16:34:59 +02002155 res->flags &= ~CF_EXPECT_MORE; /* no more data are expected */
2156
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002157 /* other states, ENDING...TUNNEL */
2158 if (msg->msg_state >= HTTP_MSG_DONE)
2159 goto done;
Christopher Faulet9768c262018-10-22 09:34:31 +02002160
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002161 if (HAS_RSP_DATA_FILTERS(s)) {
2162 ret = flt_http_end(s, msg);
2163 if (ret <= 0) {
2164 if (!ret)
2165 goto missing_data_or_waiting;
2166 goto return_bad_res;
2167 }
2168 }
2169
Christopher Fauletc75668e2020-12-07 18:10:32 +01002170 if ((txn->meth == HTTP_METH_CONNECT && txn->status >= 200 && txn->status < 300) || txn->status == 101 ||
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002171 !(msg->flags & HTTP_MSGF_XFER_LEN)) {
2172 msg->msg_state = HTTP_MSG_TUNNEL;
2173 goto ending;
2174 }
2175 else {
2176 msg->msg_state = HTTP_MSG_DONE;
2177 res->to_forward = 0;
2178 }
2179
2180 done:
2181
2182 channel_dont_close(res);
2183
Christopher Fauletf2b02cf2023-01-13 11:02:28 +01002184 if ((res->flags & CF_SHUTW) && co_data(res)) {
2185 /* response errors are most likely due to the client aborting
2186 * the transfer. */
2187 goto return_cli_abort;
2188 }
2189
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002190 http_end_response(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002191 if (!(res->analysers & an_bit)) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002192 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002193 return 1;
2194 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002195 DBG_TRACE_DEVEL("waiting for the end of the HTTP txn",
2196 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002197 return 0;
2198
2199 missing_data_or_waiting:
2200 if (res->flags & CF_SHUTW)
Christopher Faulet93e02d82019-03-08 14:18:50 +01002201 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002202
2203 /* stop waiting for data if the input is closed before the end. If the
2204 * client side was already closed, it means that the client has aborted,
2205 * so we don't want to count this as a server abort. Otherwise it's a
2206 * server abort.
2207 */
Christopher Fauletd20fdb02019-06-13 16:43:22 +02002208 if (msg->msg_state < HTTP_MSG_ENDING && res->flags & CF_SHUTR) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02002209 if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))
Christopher Faulet93e02d82019-03-08 14:18:50 +01002210 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002211 /* If we have some pending data, we continue the processing */
Christopher Faulet93e02d82019-03-08 14:18:50 +01002212 if (htx_is_empty(htx))
2213 goto return_srv_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002214 }
2215
Christopher Faulete0768eb2018-10-03 16:38:02 +02002216 /* When TE: chunked is used, we need to get there again to parse
2217 * remaining chunks even if the server has closed, so we don't want to
Christopher Faulet9768c262018-10-22 09:34:31 +02002218 * set CF_DONTCLOSE. Similarly when there is a content-leng or if there
2219 * are filters registered on the stream, we don't want to forward a
2220 * close
Christopher Faulete0768eb2018-10-03 16:38:02 +02002221 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002222 if ((msg->flags & HTTP_MSGF_XFER_LEN) || HAS_RSP_DATA_FILTERS(s))
Christopher Faulete0768eb2018-10-03 16:38:02 +02002223 channel_dont_close(res);
2224
2225 /* We know that more data are expected, but we couldn't send more that
2226 * what we did. So we always set the CF_EXPECT_MORE flag so that the
2227 * system knows it must not set a PUSH on this first part. Interactive
2228 * modes are already handled by the stream sock layer. We must not do
2229 * this in content-length mode because it could present the MSG_MORE
2230 * flag with the last block of forwarded data, which would cause an
2231 * additional delay to be observed by the receiver.
2232 */
Christopher Faulet2151cdd2020-07-22 16:34:59 +02002233 if (HAS_RSP_DATA_FILTERS(s))
Christopher Faulete0768eb2018-10-03 16:38:02 +02002234 res->flags |= CF_EXPECT_MORE;
2235
2236 /* the stream handler will take care of timeouts and errors */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002237 DBG_TRACE_DEVEL("waiting for more data to forward",
2238 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002239 return 0;
2240
Christopher Faulet93e02d82019-03-08 14:18:50 +01002241 return_srv_abort:
Willy Tarreau4781b152021-04-06 13:53:36 +02002242 _HA_ATOMIC_INC(&sess->fe->fe_counters.srv_aborts);
2243 _HA_ATOMIC_INC(&s->be->be_counters.srv_aborts);
William Lallemand36119de2021-03-08 15:26:48 +01002244 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02002245 _HA_ATOMIC_INC(&sess->listener->counters->srv_aborts);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002246 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02002247 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.srv_aborts);
Willy Tarreau826f3ab2021-02-10 12:07:15 +01002248 stream_inc_http_fail_ctr(s);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002249 if (!(s->flags & SF_ERR_MASK))
Christopher Fauletf2b02cf2023-01-13 11:02:28 +01002250 s->flags |= ((res->flags & CF_READ_TIMEOUT) ? SF_ERR_SRVTO : SF_ERR_SRVCL);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002251 goto return_error;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002252
Christopher Faulet93e02d82019-03-08 14:18:50 +01002253 return_cli_abort:
Willy Tarreau4781b152021-04-06 13:53:36 +02002254 _HA_ATOMIC_INC(&sess->fe->fe_counters.cli_aborts);
2255 _HA_ATOMIC_INC(&s->be->be_counters.cli_aborts);
William Lallemand36119de2021-03-08 15:26:48 +01002256 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02002257 _HA_ATOMIC_INC(&sess->listener->counters->cli_aborts);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002258 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02002259 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.cli_aborts);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002260 if (!(s->flags & SF_ERR_MASK))
Christopher Fauletf2b02cf2023-01-13 11:02:28 +01002261 s->flags |= ((res->flags & CF_WRITE_TIMEOUT) ? SF_ERR_CLITO : SF_ERR_CLICL);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002262 goto return_error;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002263
Christopher Fauletb9a92f32019-09-09 10:15:21 +02002264 return_int_err:
Willy Tarreau4781b152021-04-06 13:53:36 +02002265 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
2266 _HA_ATOMIC_INC(&s->be->be_counters.internal_errors);
William Lallemand36119de2021-03-08 15:26:48 +01002267 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02002268 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002269 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02002270 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.internal_errors);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02002271 if (!(s->flags & SF_ERR_MASK))
2272 s->flags |= SF_ERR_INTERNAL;
2273 goto return_error;
2274
Christopher Faulet93e02d82019-03-08 14:18:50 +01002275 return_bad_res:
Willy Tarreau4781b152021-04-06 13:53:36 +02002276 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002277 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02002278 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002279 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_RSP);
2280 }
Willy Tarreau826f3ab2021-02-10 12:07:15 +01002281 stream_inc_http_fail_ctr(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002282 if (!(s->flags & SF_ERR_MASK))
Christopher Faulet93e02d82019-03-08 14:18:50 +01002283 s->flags |= SF_ERR_SRVCL;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002284 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +02002285
Christopher Faulet93e02d82019-03-08 14:18:50 +01002286 return_error:
Christopher Faulete0768eb2018-10-03 16:38:02 +02002287 /* don't send any error message as we're in the body */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002288 http_reply_and_close(s, txn->status, NULL);
Christopher Fauletf2b02cf2023-01-13 11:02:28 +01002289 http_set_term_flags(s);
2290 stream_inc_http_fail_ctr(s);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002291 DBG_TRACE_DEVEL("leaving on error",
2292 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002293 return 0;
2294}
2295
Christopher Fauletf2824e62018-10-01 12:12:37 +02002296/* Perform an HTTP redirect based on the information in <rule>. The function
Willy Tarreaubc1223b2021-09-02 16:54:33 +02002297 * returns zero in case of an irrecoverable error such as too large a request
2298 * to build a valid response, 1 in case of successful redirect (hence the rule
2299 * is final), or 2 if the rule has to be silently skipped.
Christopher Fauletf2824e62018-10-01 12:12:37 +02002300 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002301int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn)
Christopher Fauletf2824e62018-10-01 12:12:37 +02002302{
Christopher Faulet99daf282018-11-28 22:58:13 +01002303 struct channel *req = &s->req;
2304 struct channel *res = &s->res;
2305 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01002306 struct htx_sl *sl;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002307 struct buffer *chunk;
Christopher Faulet99daf282018-11-28 22:58:13 +01002308 struct ist status, reason, location;
2309 unsigned int flags;
Christopher Fauleteab17572022-04-26 20:34:38 +02002310 int ret = 1, close = 0; /* Try to keep the connection alive byt default */
Christopher Fauletf2824e62018-10-01 12:12:37 +02002311
2312 chunk = alloc_trash_chunk();
Christopher Fauletb8a53712019-12-16 11:29:38 +01002313 if (!chunk) {
2314 if (!(s->flags & SF_ERR_MASK))
2315 s->flags |= SF_ERR_RESOURCE;
Christopher Faulet99daf282018-11-28 22:58:13 +01002316 goto fail;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002317 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002318
Christopher Faulet99daf282018-11-28 22:58:13 +01002319 /*
2320 * Create the location
2321 */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002322 htx = htxbuf(&req->buf);
Christopher Fauletf2824e62018-10-01 12:12:37 +02002323 switch(rule->type) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002324 case REDIRECT_TYPE_SCHEME: {
2325 struct http_hdr_ctx ctx;
2326 struct ist path, host;
Amaury Denoyellec453f952021-07-06 11:40:12 +02002327 struct http_uri_parser parser;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002328
Christopher Faulet99daf282018-11-28 22:58:13 +01002329 host = ist("");
2330 ctx.blk = NULL;
2331 if (http_find_header(htx, ist("Host"), &ctx, 0))
2332 host = ctx.value;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002333
Christopher Faulet297fbb42019-05-13 14:41:27 +02002334 sl = http_get_stline(htx);
Amaury Denoyellec453f952021-07-06 11:40:12 +02002335 parser = http_uri_parser_init(htx_sl_req_uri(sl));
2336 path = http_parse_path(&parser);
Christopher Faulet99daf282018-11-28 22:58:13 +01002337 /* build message using path */
Tim Duesterhused526372020-03-05 17:56:33 +01002338 if (isttest(path)) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002339 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2340 int qs = 0;
2341 while (qs < path.len) {
2342 if (*(path.ptr + qs) == '?') {
2343 path.len = qs;
2344 break;
2345 }
2346 qs++;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002347 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002348 }
2349 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002350 else
2351 path = ist("/");
Christopher Fauletf2824e62018-10-01 12:12:37 +02002352
Christopher Faulet99daf282018-11-28 22:58:13 +01002353 if (rule->rdr_str) { /* this is an old "redirect" rule */
2354 /* add scheme */
2355 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2356 goto fail;
2357 }
2358 else {
2359 /* add scheme with executing log format */
2360 chunk->data += build_logline(s, chunk->area + chunk->data,
2361 chunk->size - chunk->data,
2362 &rule->rdr_fmt);
2363 }
2364 /* add "://" + host + path */
2365 if (!chunk_memcat(chunk, "://", 3) ||
2366 !chunk_memcat(chunk, host.ptr, host.len) ||
2367 !chunk_memcat(chunk, path.ptr, path.len))
2368 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002369
Christopher Faulet99daf282018-11-28 22:58:13 +01002370 /* append a slash at the end of the location if needed and missing */
2371 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
2372 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
2373 if (chunk->data + 1 >= chunk->size)
2374 goto fail;
2375 chunk->area[chunk->data++] = '/';
2376 }
2377 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002378 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002379
Christopher Faulet99daf282018-11-28 22:58:13 +01002380 case REDIRECT_TYPE_PREFIX: {
2381 struct ist path;
Amaury Denoyellec453f952021-07-06 11:40:12 +02002382 struct http_uri_parser parser;
Christopher Faulet99daf282018-11-28 22:58:13 +01002383
Christopher Faulet297fbb42019-05-13 14:41:27 +02002384 sl = http_get_stline(htx);
Amaury Denoyellec453f952021-07-06 11:40:12 +02002385 parser = http_uri_parser_init(htx_sl_req_uri(sl));
2386 path = http_parse_path(&parser);
Christopher Faulet99daf282018-11-28 22:58:13 +01002387 /* build message using path */
Tim Duesterhused526372020-03-05 17:56:33 +01002388 if (isttest(path)) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002389 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2390 int qs = 0;
2391 while (qs < path.len) {
2392 if (*(path.ptr + qs) == '?') {
2393 path.len = qs;
2394 break;
2395 }
2396 qs++;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002397 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002398 }
2399 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002400 else
2401 path = ist("/");
Christopher Fauletf2824e62018-10-01 12:12:37 +02002402
Christopher Faulet99daf282018-11-28 22:58:13 +01002403 if (rule->rdr_str) { /* this is an old "redirect" rule */
2404 /* add prefix. Note that if prefix == "/", we don't want to
2405 * add anything, otherwise it makes it hard for the user to
2406 * configure a self-redirection.
2407 */
2408 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
2409 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2410 goto fail;
2411 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002412 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002413 else {
2414 /* add prefix with executing log format */
2415 chunk->data += build_logline(s, chunk->area + chunk->data,
2416 chunk->size - chunk->data,
2417 &rule->rdr_fmt);
2418 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002419
Christopher Faulet99daf282018-11-28 22:58:13 +01002420 /* add path */
2421 if (!chunk_memcat(chunk, path.ptr, path.len))
2422 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002423
Christopher Faulet99daf282018-11-28 22:58:13 +01002424 /* append a slash at the end of the location if needed and missing */
2425 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
2426 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
2427 if (chunk->data + 1 >= chunk->size)
2428 goto fail;
2429 chunk->area[chunk->data++] = '/';
2430 }
2431 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002432 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002433 case REDIRECT_TYPE_LOCATION:
2434 default:
2435 if (rule->rdr_str) { /* this is an old "redirect" rule */
2436 /* add location */
2437 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2438 goto fail;
2439 }
2440 else {
2441 /* add location with executing log format */
Willy Tarreaubc1223b2021-09-02 16:54:33 +02002442 int len = build_logline(s, chunk->area + chunk->data,
2443 chunk->size - chunk->data,
2444 &rule->rdr_fmt);
Christopher Fauleteab17572022-04-26 20:34:38 +02002445 if (!len && rule->flags & REDIRECT_FLAG_IGNORE_EMPTY) {
2446 ret = 2;
2447 goto out;
2448 }
Willy Tarreaubc1223b2021-09-02 16:54:33 +02002449
2450 chunk->data += len;
Christopher Faulet99daf282018-11-28 22:58:13 +01002451 }
2452 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002453 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002454 location = ist2(chunk->area, chunk->data);
2455
2456 /*
2457 * Create the 30x response
2458 */
2459 switch (rule->code) {
2460 case 308:
2461 status = ist("308");
2462 reason = ist("Permanent Redirect");
2463 break;
2464 case 307:
2465 status = ist("307");
2466 reason = ist("Temporary Redirect");
2467 break;
2468 case 303:
2469 status = ist("303");
2470 reason = ist("See Other");
2471 break;
2472 case 301:
2473 status = ist("301");
2474 reason = ist("Moved Permanently");
2475 break;
2476 case 302:
2477 default:
2478 status = ist("302");
2479 reason = ist("Found");
2480 break;
2481 }
2482
Christopher Faulet08e66462019-05-23 16:44:59 +02002483 if (!(txn->req.flags & HTTP_MSGF_BODYLESS) && txn->req.msg_state != HTTP_MSG_DONE)
2484 close = 1;
2485
Christopher Faulet99daf282018-11-28 22:58:13 +01002486 htx = htx_from_buf(&res->buf);
Kevin Zhu96b36392020-01-07 09:42:55 +01002487 /* Trim any possible response */
2488 channel_htx_truncate(&s->res, htx);
Christopher Faulet99daf282018-11-28 22:58:13 +01002489 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
2490 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), status, reason);
2491 if (!sl)
2492 goto fail;
2493 sl->info.res.status = rule->code;
2494 s->txn->status = rule->code;
2495
Christopher Faulet08e66462019-05-23 16:44:59 +02002496 if (close && !htx_add_header(htx, ist("Connection"), ist("close")))
2497 goto fail;
2498
2499 if (!htx_add_header(htx, ist("Content-length"), ist("0")) ||
Christopher Faulet99daf282018-11-28 22:58:13 +01002500 !htx_add_header(htx, ist("Location"), location))
2501 goto fail;
2502
2503 if (rule->code == 302 || rule->code == 303 || rule->code == 307) {
2504 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")))
2505 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002506 }
2507
2508 if (rule->cookie_len) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002509 if (!htx_add_header(htx, ist("Set-Cookie"), ist2(rule->cookie_str, rule->cookie_len)))
2510 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002511 }
2512
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002513 if (!htx_add_endof(htx, HTX_BLK_EOH))
Christopher Faulet99daf282018-11-28 22:58:13 +01002514 goto fail;
2515
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002516 htx->flags |= HTX_FL_EOM;
Kevin Zhu96b36392020-01-07 09:42:55 +01002517 htx_to_buf(htx, &res->buf);
Christopher Fauleta72a7e42020-01-28 09:28:11 +01002518 if (!http_forward_proxy_resp(s, 1))
2519 goto fail;
Christopher Faulet99daf282018-11-28 22:58:13 +01002520
Christopher Faulet60b33a52020-01-28 09:18:10 +01002521 if (rule->flags & REDIRECT_FLAG_FROM_REQ) {
2522 /* let's log the request time */
2523 s->logs.tv_request = now;
Christopher Fauletd3475882021-10-04 14:16:46 +02002524 req->analysers &= AN_REQ_FLT_END;
Christopher Faulet99daf282018-11-28 22:58:13 +01002525
Christopher Faulet60b33a52020-01-28 09:18:10 +01002526 if (s->sess->fe == s->be) /* report it if the request was intercepted by the frontend */
Willy Tarreau4781b152021-04-06 13:53:36 +02002527 _HA_ATOMIC_INC(&s->sess->fe->fe_counters.intercepted_req);
Christopher Faulet60b33a52020-01-28 09:18:10 +01002528 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002529
2530 if (!(s->flags & SF_ERR_MASK))
2531 s->flags |= SF_ERR_LOCAL;
Christopher Faulet4a66c942023-01-13 09:43:21 +01002532 http_set_term_flags(s);
Christopher Fauletf2824e62018-10-01 12:12:37 +02002533
Christopher Fauleteab17572022-04-26 20:34:38 +02002534 out:
Christopher Faulet99daf282018-11-28 22:58:13 +01002535 free_trash_chunk(chunk);
Christopher Fauleteab17572022-04-26 20:34:38 +02002536 return ret;
Christopher Faulet99daf282018-11-28 22:58:13 +01002537
2538 fail:
2539 /* If an error occurred, remove the incomplete HTTP response from the
2540 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01002541 channel_htx_truncate(res, htxbuf(&res->buf));
Christopher Fauleteab17572022-04-26 20:34:38 +02002542 ret = 0;
2543 goto out;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002544}
2545
Christopher Faulet18c13d32022-05-16 11:43:10 +02002546/* This function filters the request header names to only allow [0-9a-zA-Z-]
2547 * characters. Depending on the proxy configuration, headers with a name not
2548 * matching this charset are removed or the request is rejected with a
2549 * 403-Forbidden response if such name are found. It returns HTTP_RULE_RES_CONT
2550 * to continue the request processing or HTTP_RULE_RES_DENY if the request is
2551 * rejected.
2552 */
2553static enum rule_result http_req_restrict_header_names(struct stream *s, struct htx *htx, struct proxy *px)
2554{
2555 struct htx_blk *blk;
2556 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
2557
2558 blk = htx_get_first_blk(htx);
2559 while (blk) {
2560 enum htx_blk_type type = htx_get_blk_type(blk);
2561
2562 if (type == HTX_BLK_HDR) {
2563 struct ist n = htx_get_blk_name(htx, blk);
Mateusz Malek4b85a962022-08-17 14:22:09 +02002564 int i, end = istlen(n);
Christopher Faulet18c13d32022-05-16 11:43:10 +02002565
Mateusz Malek4b85a962022-08-17 14:22:09 +02002566 for (i = 0; i < end; i++) {
Christopher Faulet18c13d32022-05-16 11:43:10 +02002567 if (!isalnum((unsigned char)n.ptr[i]) && n.ptr[i] != '-') {
Mateusz Malek4b85a962022-08-17 14:22:09 +02002568 break;
Christopher Faulet18c13d32022-05-16 11:43:10 +02002569 }
2570 }
Mateusz Malek4b85a962022-08-17 14:22:09 +02002571
2572 if (i < end) {
2573 /* Disallowed character found - block the request or remove the header */
2574 if (px->options2 & PR_O2_RSTRICT_REQ_HDR_NAMES_BLK)
2575 goto block;
2576 blk = htx_remove_blk(htx, blk);
2577 continue;
2578 }
Christopher Faulet18c13d32022-05-16 11:43:10 +02002579 }
2580 if (type == HTX_BLK_EOH)
2581 break;
2582
2583 blk = htx_get_next_blk(htx, blk);
2584 }
2585 out:
2586 return rule_ret;
2587 block:
2588 /* Block the request returning a 403-Forbidden response */
2589 s->txn->status = 403;
2590 rule_ret = HTTP_RULE_RES_DENY;
2591 goto out;
2592}
2593
Christopher Faulet92d34fe2019-12-17 09:20:34 +01002594/* Replace all headers matching the name <name>. The header value is replaced if
2595 * it matches the regex <re>. <str> is used for the replacement. If <full> is
2596 * set to 1, the full-line is matched and replaced. Otherwise, comma-separated
2597 * values are evaluated one by one. It returns 0 on success and -1 on error.
2598 */
2599int http_replace_hdrs(struct stream* s, struct htx *htx, struct ist name,
2600 const char *str, struct my_regex *re, int full)
Christopher Faulet72333522018-10-24 11:25:02 +02002601{
2602 struct http_hdr_ctx ctx;
2603 struct buffer *output = get_trash_chunk();
2604
Christopher Faulet72333522018-10-24 11:25:02 +02002605 ctx.blk = NULL;
Christopher Faulet92d34fe2019-12-17 09:20:34 +01002606 while (http_find_header(htx, name, &ctx, full)) {
Christopher Faulet72333522018-10-24 11:25:02 +02002607 if (!regex_exec_match2(re, ctx.value.ptr, ctx.value.len, MAX_MATCH, pmatch, 0))
2608 continue;
2609
2610 output->data = exp_replace(output->area, output->size, ctx.value.ptr, str, pmatch);
2611 if (output->data == -1)
2612 return -1;
2613 if (!http_replace_header_value(htx, &ctx, ist2(output->area, output->data)))
2614 return -1;
2615 }
2616 return 0;
2617}
2618
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002619/* This function executes one of the set-{method,path,query,uri} actions. It
2620 * takes the string from the variable 'replace' with length 'len', then modifies
2621 * the relevant part of the request line accordingly. Then it updates various
2622 * pointers to the next elements which were moved, and the total buffer length.
2623 * It finds the action to be performed in p[2], previously filled by function
2624 * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal
2625 * error, though this can be revisited when this code is finally exploited.
2626 *
2627 * 'action' can be '0' to replace method, '1' to replace path, '2' to replace
Christopher Faulet312294f2020-09-02 17:17:44 +02002628 * query string, 3 to replace uri or 4 to replace the path+query.
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002629 *
2630 * In query string case, the mark question '?' must be set at the start of the
2631 * string by the caller, event if the replacement query string is empty.
2632 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002633int http_req_replace_stline(int action, const char *replace, int len,
2634 struct proxy *px, struct stream *s)
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002635{
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002636 struct htx *htx = htxbuf(&s->req.buf);
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002637
2638 switch (action) {
2639 case 0: // method
2640 if (!http_replace_req_meth(htx, ist2(replace, len)))
2641 return -1;
2642 break;
2643
2644 case 1: // path
Christopher Fauletb8ce5052020-08-31 16:11:57 +02002645 if (!http_replace_req_path(htx, ist2(replace, len), 0))
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002646 return -1;
2647 break;
2648
2649 case 2: // query
2650 if (!http_replace_req_query(htx, ist2(replace, len)))
2651 return -1;
2652 break;
2653
2654 case 3: // uri
2655 if (!http_replace_req_uri(htx, ist2(replace, len)))
2656 return -1;
2657 break;
2658
Christopher Faulet312294f2020-09-02 17:17:44 +02002659 case 4: // path + query
2660 if (!http_replace_req_path(htx, ist2(replace, len), 1))
2661 return -1;
2662 break;
2663
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002664 default:
2665 return -1;
2666 }
2667 return 0;
2668}
2669
2670/* This function replace the HTTP status code and the associated message. The
Christopher Faulete00d06c2019-12-16 17:18:42 +01002671 * variable <status> contains the new status code. This function never fails. It
2672 * returns 0 in case of success, -1 in case of internal error.
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002673 */
Christopher Faulet96bff762019-12-17 13:46:18 +01002674int http_res_set_status(unsigned int status, struct ist reason, struct stream *s)
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002675{
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002676 struct htx *htx = htxbuf(&s->res.buf);
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002677 char *res;
2678
2679 chunk_reset(&trash);
2680 res = ultoa_o(status, trash.area, trash.size);
2681 trash.data = res - trash.area;
2682
2683 /* Do we have a custom reason format string? */
Tim Duesterhuse296d3e2020-03-05 17:56:31 +01002684 if (!isttest(reason)) {
Christopher Faulet96bff762019-12-17 13:46:18 +01002685 const char *str = http_get_reason(status);
Tim Duesterhusdcf753a2021-03-04 17:31:47 +01002686 reason = ist(str);
Christopher Faulet96bff762019-12-17 13:46:18 +01002687 }
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002688
Christopher Fauletbde2c4c2020-08-31 16:43:34 +02002689 if (!http_replace_res_status(htx, ist2(trash.area, trash.data), reason))
Christopher Faulete00d06c2019-12-16 17:18:42 +01002690 return -1;
Willy Tarreau640e2532023-01-10 14:50:44 +01002691 s->txn->status = status;
Christopher Faulete00d06c2019-12-16 17:18:42 +01002692 return 0;
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002693}
2694
Christopher Faulet3e964192018-10-24 11:39:23 +02002695/* Executes the http-request rules <rules> for stream <s>, proxy <px> and
2696 * transaction <txn>. Returns the verdict of the first rule that prevents
2697 * further processing of the request (auth, deny, ...), and defaults to
2698 * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or
2699 * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT
2700 * on txn->flags if it encounters a tarpit rule. If <deny_status> is not NULL
2701 * and a deny/tarpit rule is matched, it will be filled with this rule's deny
2702 * status.
2703 */
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002704static enum rule_result http_req_get_intercept_rule(struct proxy *px, struct list *def_rules,
2705 struct list *rules, struct stream *s)
Christopher Faulet3e964192018-10-24 11:39:23 +02002706{
2707 struct session *sess = strm_sess(s);
2708 struct http_txn *txn = s->txn;
Christopher Faulet3e964192018-10-24 11:39:23 +02002709 struct act_rule *rule;
Christopher Faulet3e964192018-10-24 11:39:23 +02002710 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002711 int act_opts = 0;
Christopher Faulet3e964192018-10-24 11:39:23 +02002712
Christopher Faulet3e964192018-10-24 11:39:23 +02002713 /* If "the current_rule_list" match the executed rule list, we are in
2714 * resume condition. If a resume is needed it is always in the action
2715 * and never in the ACL or converters. In this case, we initialise the
2716 * current rule, and go to the action execution point.
2717 */
2718 if (s->current_rule) {
2719 rule = s->current_rule;
2720 s->current_rule = NULL;
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002721 if (s->current_rule_list == rules || (def_rules && s->current_rule_list == def_rules))
Christopher Faulet3e964192018-10-24 11:39:23 +02002722 goto resume_execution;
2723 }
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002724 s->current_rule_list = ((!def_rules || s->current_rule_list == def_rules) ? rules : def_rules);
Christopher Faulet3e964192018-10-24 11:39:23 +02002725
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002726 restart:
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002727 /* start the ruleset evaluation in strict mode */
2728 txn->req.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01002729
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002730 list_for_each_entry(rule, s->current_rule_list, list) {
Christopher Faulet3e964192018-10-24 11:39:23 +02002731 /* check optional condition */
2732 if (rule->cond) {
2733 int ret;
2734
2735 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
2736 ret = acl_pass(ret);
2737
2738 if (rule->cond->pol == ACL_COND_UNLESS)
2739 ret = !ret;
2740
2741 if (!ret) /* condition not matched */
2742 continue;
2743 }
2744
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002745 act_opts |= ACT_OPT_FIRST;
Christopher Faulet3e964192018-10-24 11:39:23 +02002746 resume_execution:
Amaury Denoyelle03517732021-05-07 14:25:01 +02002747 if (rule->kw->flags & KWF_EXPERIMENTAL)
2748 mark_tainted(TAINTED_ACTION_EXP_EXECUTED);
2749
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002750 /* Always call the action function if defined */
2751 if (rule->action_ptr) {
2752 if ((s->req.flags & CF_READ_ERROR) ||
Christopher Faulet446d8032022-12-12 07:53:18 +01002753 ((s->req.flags & CF_SHUTR) &&
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002754 (px->options & PR_O_ABRT_CLOSE)))
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002755 act_opts |= ACT_OPT_FINAL;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002756
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002757 switch (rule->action_ptr(rule, px, sess, s, act_opts)) {
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002758 case ACT_RET_CONT:
2759 break;
2760 case ACT_RET_STOP:
2761 rule_ret = HTTP_RULE_RES_STOP;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002762 s->last_rule_file = rule->conf.file;
2763 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002764 goto end;
2765 case ACT_RET_YIELD:
2766 s->current_rule = rule;
2767 rule_ret = HTTP_RULE_RES_YIELD;
2768 goto end;
2769 case ACT_RET_ERR:
2770 rule_ret = HTTP_RULE_RES_ERROR;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002771 s->last_rule_file = rule->conf.file;
2772 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002773 goto end;
2774 case ACT_RET_DONE:
2775 rule_ret = HTTP_RULE_RES_DONE;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002776 s->last_rule_file = rule->conf.file;
2777 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002778 goto end;
2779 case ACT_RET_DENY:
Christopher Fauletb58f62b2020-01-13 16:40:13 +01002780 if (txn->status == -1)
2781 txn->status = 403;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002782 rule_ret = HTTP_RULE_RES_DENY;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002783 s->last_rule_file = rule->conf.file;
2784 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002785 goto end;
2786 case ACT_RET_ABRT:
2787 rule_ret = HTTP_RULE_RES_ABRT;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002788 s->last_rule_file = rule->conf.file;
2789 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002790 goto end;
2791 case ACT_RET_INV:
2792 rule_ret = HTTP_RULE_RES_BADREQ;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002793 s->last_rule_file = rule->conf.file;
2794 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002795 goto end;
2796 }
2797 continue; /* eval the next rule */
2798 }
2799
2800 /* If not action function defined, check for known actions */
Christopher Faulet3e964192018-10-24 11:39:23 +02002801 switch (rule->action) {
2802 case ACT_ACTION_ALLOW:
2803 rule_ret = HTTP_RULE_RES_STOP;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002804 s->last_rule_file = rule->conf.file;
2805 s->last_rule_line = rule->conf.line;
Christopher Faulet3e964192018-10-24 11:39:23 +02002806 goto end;
2807
2808 case ACT_ACTION_DENY:
Christopher Faulet5cb513a2020-05-13 17:56:56 +02002809 txn->status = rule->arg.http_reply->status;
2810 txn->http_reply = rule->arg.http_reply;
Christopher Faulet3e964192018-10-24 11:39:23 +02002811 rule_ret = HTTP_RULE_RES_DENY;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002812 s->last_rule_file = rule->conf.file;
2813 s->last_rule_line = rule->conf.line;
Christopher Faulet3e964192018-10-24 11:39:23 +02002814 goto end;
2815
2816 case ACT_HTTP_REQ_TARPIT:
2817 txn->flags |= TX_CLTARPIT;
Christopher Faulet5cb513a2020-05-13 17:56:56 +02002818 txn->status = rule->arg.http_reply->status;
2819 txn->http_reply = rule->arg.http_reply;
Christopher Faulet3e964192018-10-24 11:39:23 +02002820 rule_ret = HTTP_RULE_RES_DENY;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002821 s->last_rule_file = rule->conf.file;
2822 s->last_rule_line = rule->conf.line;
Christopher Faulet3e964192018-10-24 11:39:23 +02002823 goto end;
2824
Willy Tarreaubc1223b2021-09-02 16:54:33 +02002825 case ACT_HTTP_REDIR: {
2826 int ret = http_apply_redirect_rule(rule->arg.redir, s, txn);
2827
2828 if (ret == 2) // 2 == skip
2829 break;
2830
2831 rule_ret = ret ? HTTP_RULE_RES_ABRT : HTTP_RULE_RES_ERROR;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002832 s->last_rule_file = rule->conf.file;
2833 s->last_rule_line = rule->conf.line;
Christopher Faulet3e964192018-10-24 11:39:23 +02002834 goto end;
Willy Tarreaubc1223b2021-09-02 16:54:33 +02002835 }
Christopher Faulet3e964192018-10-24 11:39:23 +02002836
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002837 /* other flags exists, but normally, they never be matched. */
Christopher Faulet3e964192018-10-24 11:39:23 +02002838 default:
2839 break;
2840 }
2841 }
2842
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002843 if (def_rules && s->current_rule_list == def_rules) {
2844 s->current_rule_list = rules;
2845 goto restart;
2846 }
2847
Christopher Faulet3e964192018-10-24 11:39:23 +02002848 end:
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002849 /* if the ruleset evaluation is finished reset the strict mode */
Christopher Faulet46f95542019-12-20 10:07:22 +01002850 if (rule_ret != HTTP_RULE_RES_YIELD)
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002851 txn->req.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01002852
Christopher Faulet3e964192018-10-24 11:39:23 +02002853 /* we reached the end of the rules, nothing to report */
2854 return rule_ret;
2855}
2856
2857/* Executes the http-response rules <rules> for stream <s> and proxy <px>. It
2858 * returns one of 5 possible statuses: HTTP_RULE_RES_CONT, HTTP_RULE_RES_STOP,
2859 * HTTP_RULE_RES_DONE, HTTP_RULE_RES_YIELD, or HTTP_RULE_RES_BADREQ. If *CONT
2860 * is returned, the process can continue the evaluation of next rule list. If
2861 * *STOP or *DONE is returned, the process must stop the evaluation. If *BADREQ
2862 * is returned, it means the operation could not be processed and a server error
Christopher Fauleta53abad2020-05-13 08:12:22 +02002863 * must be returned. If *YIELD is returned, the caller must call again the
2864 * function with the same context.
Christopher Faulet3e964192018-10-24 11:39:23 +02002865 */
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002866static enum rule_result http_res_get_intercept_rule(struct proxy *px, struct list *def_rules,
2867 struct list *rules, struct stream *s)
Christopher Faulet3e964192018-10-24 11:39:23 +02002868{
2869 struct session *sess = strm_sess(s);
2870 struct http_txn *txn = s->txn;
Christopher Faulet3e964192018-10-24 11:39:23 +02002871 struct act_rule *rule;
Christopher Faulet3e964192018-10-24 11:39:23 +02002872 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002873 int act_opts = 0;
Christopher Faulet3e964192018-10-24 11:39:23 +02002874
Christopher Faulet3e964192018-10-24 11:39:23 +02002875 /* If "the current_rule_list" match the executed rule list, we are in
2876 * resume condition. If a resume is needed it is always in the action
2877 * and never in the ACL or converters. In this case, we initialise the
2878 * current rule, and go to the action execution point.
2879 */
2880 if (s->current_rule) {
2881 rule = s->current_rule;
2882 s->current_rule = NULL;
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002883 if (s->current_rule_list == rules || (def_rules && s->current_rule_list == def_rules))
Christopher Faulet3e964192018-10-24 11:39:23 +02002884 goto resume_execution;
2885 }
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002886 s->current_rule_list = ((!def_rules || s->current_rule_list == def_rules) ? rules : def_rules);
2887
2888 restart:
Christopher Faulet3e964192018-10-24 11:39:23 +02002889
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002890 /* start the ruleset evaluation in strict mode */
2891 txn->rsp.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01002892
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002893 list_for_each_entry(rule, s->current_rule_list, list) {
Christopher Faulet3e964192018-10-24 11:39:23 +02002894 /* check optional condition */
2895 if (rule->cond) {
2896 int ret;
2897
2898 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
2899 ret = acl_pass(ret);
2900
2901 if (rule->cond->pol == ACL_COND_UNLESS)
2902 ret = !ret;
2903
2904 if (!ret) /* condition not matched */
2905 continue;
2906 }
2907
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002908 act_opts |= ACT_OPT_FIRST;
Christopher Faulet3e964192018-10-24 11:39:23 +02002909resume_execution:
Amaury Denoyelle03517732021-05-07 14:25:01 +02002910 if (rule->kw->flags & KWF_EXPERIMENTAL)
2911 mark_tainted(TAINTED_ACTION_EXP_EXECUTED);
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002912
2913 /* Always call the action function if defined */
2914 if (rule->action_ptr) {
2915 if ((s->req.flags & CF_READ_ERROR) ||
Christopher Faulet446d8032022-12-12 07:53:18 +01002916 ((s->req.flags & CF_SHUTR) &&
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002917 (px->options & PR_O_ABRT_CLOSE)))
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002918 act_opts |= ACT_OPT_FINAL;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002919
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002920 switch (rule->action_ptr(rule, px, sess, s, act_opts)) {
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002921 case ACT_RET_CONT:
2922 break;
2923 case ACT_RET_STOP:
2924 rule_ret = HTTP_RULE_RES_STOP;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002925 s->last_rule_file = rule->conf.file;
2926 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002927 goto end;
2928 case ACT_RET_YIELD:
2929 s->current_rule = rule;
2930 rule_ret = HTTP_RULE_RES_YIELD;
2931 goto end;
2932 case ACT_RET_ERR:
2933 rule_ret = HTTP_RULE_RES_ERROR;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002934 s->last_rule_file = rule->conf.file;
2935 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002936 goto end;
2937 case ACT_RET_DONE:
2938 rule_ret = HTTP_RULE_RES_DONE;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002939 s->last_rule_file = rule->conf.file;
2940 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002941 goto end;
2942 case ACT_RET_DENY:
Christopher Fauletb58f62b2020-01-13 16:40:13 +01002943 if (txn->status == -1)
2944 txn->status = 502;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002945 rule_ret = HTTP_RULE_RES_DENY;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002946 s->last_rule_file = rule->conf.file;
2947 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002948 goto end;
2949 case ACT_RET_ABRT:
2950 rule_ret = HTTP_RULE_RES_ABRT;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002951 s->last_rule_file = rule->conf.file;
2952 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002953 goto end;
2954 case ACT_RET_INV:
2955 rule_ret = HTTP_RULE_RES_BADREQ;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002956 s->last_rule_file = rule->conf.file;
2957 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002958 goto end;
2959 }
2960 continue; /* eval the next rule */
2961 }
2962
2963 /* If not action function defined, check for known actions */
Christopher Faulet3e964192018-10-24 11:39:23 +02002964 switch (rule->action) {
2965 case ACT_ACTION_ALLOW:
2966 rule_ret = HTTP_RULE_RES_STOP; /* "allow" rules are OK */
Willy Tarreauc6dae862022-03-09 17:23:10 +01002967 s->last_rule_file = rule->conf.file;
2968 s->last_rule_line = rule->conf.line;
Christopher Faulet3e964192018-10-24 11:39:23 +02002969 goto end;
2970
2971 case ACT_ACTION_DENY:
Christopher Faulet5cb513a2020-05-13 17:56:56 +02002972 txn->status = rule->arg.http_reply->status;
2973 txn->http_reply = rule->arg.http_reply;
Christopher Faulet3a26bee2019-12-16 12:47:40 +01002974 rule_ret = HTTP_RULE_RES_DENY;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002975 s->last_rule_file = rule->conf.file;
2976 s->last_rule_line = rule->conf.line;
Christopher Faulet3e964192018-10-24 11:39:23 +02002977 goto end;
2978
Willy Tarreaubc1223b2021-09-02 16:54:33 +02002979 case ACT_HTTP_REDIR: {
2980 int ret = http_apply_redirect_rule(rule->arg.redir, s, txn);
Christopher Faulet3e964192018-10-24 11:39:23 +02002981
Willy Tarreaubc1223b2021-09-02 16:54:33 +02002982 if (ret == 2) // 2 == skip
2983 break;
2984
2985 rule_ret = ret ? HTTP_RULE_RES_ABRT : HTTP_RULE_RES_ERROR;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002986 s->last_rule_file = rule->conf.file;
2987 s->last_rule_line = rule->conf.line;
Willy Tarreaubc1223b2021-09-02 16:54:33 +02002988 goto end;
2989 }
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002990 /* other flags exists, but normally, they never be matched. */
Christopher Faulet3e964192018-10-24 11:39:23 +02002991 default:
2992 break;
2993 }
2994 }
2995
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002996 if (def_rules && s->current_rule_list == def_rules) {
2997 s->current_rule_list = rules;
2998 goto restart;
2999 }
3000
Christopher Faulet3e964192018-10-24 11:39:23 +02003001 end:
Christopher Faulet1aea50e2020-01-17 16:03:53 +01003002 /* if the ruleset evaluation is finished reset the strict mode */
Christopher Faulet46f95542019-12-20 10:07:22 +01003003 if (rule_ret != HTTP_RULE_RES_YIELD)
Christopher Faulet1aea50e2020-01-17 16:03:53 +01003004 txn->rsp.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01003005
Christopher Faulet3e964192018-10-24 11:39:23 +02003006 /* we reached the end of the rules, nothing to report */
3007 return rule_ret;
3008}
3009
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01003010/* Executes backend and frontend http-after-response rules for the stream <s>,
3011 * in that order. it return 1 on success and 0 on error. It is the caller
3012 * responsibility to catch error or ignore it. If it catches it, this function
3013 * may be called a second time, for the internal error.
3014 */
3015int http_eval_after_res_rules(struct stream *s)
3016{
Christopher Fauletd4150ad2021-10-13 15:35:55 +02003017 struct list *def_rules, *rules;
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01003018 struct session *sess = s->sess;
3019 enum rule_result ret = HTTP_RULE_RES_CONT;
3020
Christopher Faulet507479b2020-05-15 12:29:46 +02003021 /* Eval after-response ruleset only if the reply is not const */
3022 if (s->txn->flags & TX_CONST_REPLY)
3023 goto end;
3024
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01003025 /* prune the request variables if not already done and swap to the response variables. */
3026 if (s->vars_reqres.scope != SCOPE_RES) {
3027 if (!LIST_ISEMPTY(&s->vars_reqres.head))
3028 vars_prune(&s->vars_reqres, s->sess, s);
Willy Tarreaub7bfcb32021-08-31 08:13:25 +02003029 vars_init_head(&s->vars_reqres, SCOPE_RES);
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01003030 }
3031
Christopher Fauletd4150ad2021-10-13 15:35:55 +02003032 def_rules = (s->be->defpx ? &s->be->defpx->http_after_res_rules : NULL);
3033 rules = &s->be->http_after_res_rules;
3034
3035 ret = http_res_get_intercept_rule(s->be, def_rules, rules, s);
Christopher Faulet4c5a5912021-11-09 17:48:39 +01003036 if ((ret == HTTP_RULE_RES_CONT || ret == HTTP_RULE_RES_STOP) && sess->fe != s->be) {
Christopher Fauletd4150ad2021-10-13 15:35:55 +02003037 def_rules = ((sess->fe->defpx && sess->fe->defpx != s->be->defpx) ? &sess->fe->defpx->http_after_res_rules : NULL);
3038 rules = &sess->fe->http_after_res_rules;
3039 ret = http_res_get_intercept_rule(sess->fe, def_rules, rules, s);
3040 }
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01003041
Christopher Faulet507479b2020-05-15 12:29:46 +02003042 end:
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01003043 /* All other codes than CONTINUE, STOP or DONE are forbidden */
3044 return (ret == HTTP_RULE_RES_CONT || ret == HTTP_RULE_RES_STOP || ret == HTTP_RULE_RES_DONE);
3045}
3046
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003047/*
3048 * Manage client-side cookie. It can impact performance by about 2% so it is
3049 * desirable to call it only when needed. This code is quite complex because
3050 * of the multiple very crappy and ambiguous syntaxes we have to support. it
3051 * highly recommended not to touch this part without a good reason !
3052 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003053static void http_manage_client_side_cookies(struct stream *s, struct channel *req)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003054{
3055 struct session *sess = s->sess;
3056 struct http_txn *txn = s->txn;
3057 struct htx *htx;
3058 struct http_hdr_ctx ctx;
3059 char *hdr_beg, *hdr_end, *del_from;
3060 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
3061 int preserve_hdr;
3062
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003063 htx = htxbuf(&req->buf);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003064 ctx.blk = NULL;
3065 while (http_find_header(htx, ist("Cookie"), &ctx, 1)) {
Olivier Houchardf0f42382019-07-22 17:43:46 +02003066 int is_first = 1;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003067 del_from = NULL; /* nothing to be deleted */
3068 preserve_hdr = 0; /* assume we may kill the whole header */
3069
3070 /* Now look for cookies. Conforming to RFC2109, we have to support
3071 * attributes whose name begin with a '$', and associate them with
3072 * the right cookie, if we want to delete this cookie.
3073 * So there are 3 cases for each cookie read :
3074 * 1) it's a special attribute, beginning with a '$' : ignore it.
3075 * 2) it's a server id cookie that we *MAY* want to delete : save
3076 * some pointers on it (last semi-colon, beginning of cookie...)
3077 * 3) it's an application cookie : we *MAY* have to delete a previous
3078 * "special" cookie.
3079 * At the end of loop, if a "special" cookie remains, we may have to
3080 * remove it. If no application cookie persists in the header, we
3081 * *MUST* delete it.
3082 *
3083 * Note: RFC2965 is unclear about the processing of spaces around
3084 * the equal sign in the ATTR=VALUE form. A careful inspection of
3085 * the RFC explicitly allows spaces before it, and not within the
3086 * tokens (attrs or values). An inspection of RFC2109 allows that
3087 * too but section 10.1.3 lets one think that spaces may be allowed
3088 * after the equal sign too, resulting in some (rare) buggy
3089 * implementations trying to do that. So let's do what servers do.
3090 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
3091 * allowed quoted strings in values, with any possible character
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003092 * after a backslash, including control chars and delimiters, which
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003093 * causes parsing to become ambiguous. Browsers also allow spaces
3094 * within values even without quotes.
3095 *
3096 * We have to keep multiple pointers in order to support cookie
3097 * removal at the beginning, middle or end of header without
3098 * corrupting the header. All of these headers are valid :
3099 *
3100 * hdr_beg hdr_end
3101 * | |
3102 * v |
3103 * NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3 |
3104 * NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3 v
3105 * NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3
3106 * | | | | | | |
3107 * | | | | | | |
3108 * | | | | | | +--> next
3109 * | | | | | +----> val_end
3110 * | | | | +-----------> val_beg
3111 * | | | +--------------> equal
3112 * | | +----------------> att_end
3113 * | +---------------------> att_beg
3114 * +--------------------------> prev
3115 *
3116 */
3117 hdr_beg = ctx.value.ptr;
3118 hdr_end = hdr_beg + ctx.value.len;
3119 for (prev = hdr_beg; prev < hdr_end; prev = next) {
3120 /* Iterate through all cookies on this line */
3121
3122 /* find att_beg */
3123 att_beg = prev;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003124 if (!is_first)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003125 att_beg++;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003126 is_first = 0;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003127
3128 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
3129 att_beg++;
3130
3131 /* find att_end : this is the first character after the last non
3132 * space before the equal. It may be equal to hdr_end.
3133 */
3134 equal = att_end = att_beg;
3135 while (equal < hdr_end) {
3136 if (*equal == '=' || *equal == ',' || *equal == ';')
3137 break;
3138 if (HTTP_IS_SPHT(*equal++))
3139 continue;
3140 att_end = equal;
3141 }
3142
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003143 /* here, <equal> points to '=', a delimiter or the end. <att_end>
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003144 * is between <att_beg> and <equal>, both may be identical.
3145 */
3146 /* look for end of cookie if there is an equal sign */
3147 if (equal < hdr_end && *equal == '=') {
3148 /* look for the beginning of the value */
3149 val_beg = equal + 1;
3150 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
3151 val_beg++;
3152
3153 /* find the end of the value, respecting quotes */
3154 next = http_find_cookie_value_end(val_beg, hdr_end);
3155
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003156 /* make val_end point to the first white space or delimiter after the value */
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003157 val_end = next;
3158 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
3159 val_end--;
3160 }
3161 else
3162 val_beg = val_end = next = equal;
3163
3164 /* We have nothing to do with attributes beginning with
3165 * '$'. However, they will automatically be removed if a
3166 * header before them is removed, since they're supposed
3167 * to be linked together.
3168 */
3169 if (*att_beg == '$')
3170 continue;
3171
3172 /* Ignore cookies with no equal sign */
3173 if (equal == next) {
3174 /* This is not our cookie, so we must preserve it. But if we already
3175 * scheduled another cookie for removal, we cannot remove the
3176 * complete header, but we can remove the previous block itself.
3177 */
3178 preserve_hdr = 1;
3179 if (del_from != NULL) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003180 int delta = http_del_hdr_value(hdr_beg, hdr_end, &del_from, prev);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003181 val_end += delta;
3182 next += delta;
3183 hdr_end += delta;
3184 prev = del_from;
3185 del_from = NULL;
3186 }
3187 continue;
3188 }
3189
3190 /* if there are spaces around the equal sign, we need to
3191 * strip them otherwise we'll get trouble for cookie captures,
3192 * or even for rewrites. Since this happens extremely rarely,
3193 * it does not hurt performance.
3194 */
3195 if (unlikely(att_end != equal || val_beg > equal + 1)) {
3196 int stripped_before = 0;
3197 int stripped_after = 0;
3198
3199 if (att_end != equal) {
3200 memmove(att_end, equal, hdr_end - equal);
3201 stripped_before = (att_end - equal);
3202 equal += stripped_before;
3203 val_beg += stripped_before;
3204 }
3205
3206 if (val_beg > equal + 1) {
3207 memmove(equal + 1, val_beg, hdr_end + stripped_before - val_beg);
3208 stripped_after = (equal + 1) - val_beg;
3209 val_beg += stripped_after;
3210 stripped_before += stripped_after;
3211 }
3212
3213 val_end += stripped_before;
3214 next += stripped_before;
3215 hdr_end += stripped_before;
3216 }
3217 /* now everything is as on the diagram above */
3218
3219 /* First, let's see if we want to capture this cookie. We check
3220 * that we don't already have a client side cookie, because we
3221 * can only capture one. Also as an optimisation, we ignore
3222 * cookies shorter than the declared name.
3223 */
3224 if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL &&
3225 (val_end - att_beg >= sess->fe->capture_namelen) &&
3226 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
3227 int log_len = val_end - att_beg;
3228
3229 if ((txn->cli_cookie = pool_alloc(pool_head_capture)) == NULL) {
3230 ha_alert("HTTP logging : out of memory.\n");
3231 } else {
3232 if (log_len > sess->fe->capture_len)
3233 log_len = sess->fe->capture_len;
3234 memcpy(txn->cli_cookie, att_beg, log_len);
3235 txn->cli_cookie[log_len] = 0;
3236 }
3237 }
3238
3239 /* Persistence cookies in passive, rewrite or insert mode have the
3240 * following form :
3241 *
3242 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
3243 *
3244 * For cookies in prefix mode, the form is :
3245 *
3246 * Cookie: NAME=SRV~VALUE
3247 */
3248 if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
3249 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
3250 struct server *srv = s->be->srv;
3251 char *delim;
3252
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003253 /* if we're in cookie prefix mode, we'll search the delimiter so that we
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003254 * have the server ID between val_beg and delim, and the original cookie between
3255 * delim+1 and val_end. Otherwise, delim==val_end :
3256 *
3257 * hdr_beg
3258 * |
3259 * v
3260 * NAME=SRV; # in all but prefix modes
3261 * NAME=SRV~OPAQUE ; # in prefix mode
3262 * || || | |+-> next
3263 * || || | +--> val_end
3264 * || || +---------> delim
3265 * || |+------------> val_beg
3266 * || +-------------> att_end = equal
3267 * |+-----------------> att_beg
3268 * +------------------> prev
3269 *
3270 */
3271 if (s->be->ck_opts & PR_CK_PFX) {
3272 for (delim = val_beg; delim < val_end; delim++)
3273 if (*delim == COOKIE_DELIM)
3274 break;
3275 }
3276 else {
3277 char *vbar1;
3278 delim = val_end;
3279 /* Now check if the cookie contains a date field, which would
3280 * appear after a vertical bar ('|') just after the server name
3281 * and before the delimiter.
3282 */
3283 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
3284 if (vbar1) {
3285 /* OK, so left of the bar is the server's cookie and
3286 * right is the last seen date. It is a base64 encoded
3287 * 30-bit value representing the UNIX date since the
3288 * epoch in 4-second quantities.
3289 */
3290 int val;
3291 delim = vbar1++;
3292 if (val_end - vbar1 >= 5) {
3293 val = b64tos30(vbar1);
3294 if (val > 0)
3295 txn->cookie_last_date = val << 2;
3296 }
3297 /* look for a second vertical bar */
3298 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
3299 if (vbar1 && (val_end - vbar1 > 5)) {
3300 val = b64tos30(vbar1 + 1);
3301 if (val > 0)
3302 txn->cookie_first_date = val << 2;
3303 }
3304 }
3305 }
3306
3307 /* if the cookie has an expiration date and the proxy wants to check
3308 * it, then we do that now. We first check if the cookie is too old,
3309 * then only if it has expired. We detect strict overflow because the
3310 * time resolution here is not great (4 seconds). Cookies with dates
3311 * in the future are ignored if their offset is beyond one day. This
3312 * allows an admin to fix timezone issues without expiring everyone
3313 * and at the same time avoids keeping unwanted side effects for too
3314 * long.
3315 */
3316 if (txn->cookie_first_date && s->be->cookie_maxlife &&
3317 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) ||
3318 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
3319 txn->flags &= ~TX_CK_MASK;
3320 txn->flags |= TX_CK_OLD;
3321 delim = val_beg; // let's pretend we have not found the cookie
3322 txn->cookie_first_date = 0;
3323 txn->cookie_last_date = 0;
3324 }
3325 else if (txn->cookie_last_date && s->be->cookie_maxidle &&
3326 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) ||
3327 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
3328 txn->flags &= ~TX_CK_MASK;
3329 txn->flags |= TX_CK_EXPIRED;
3330 delim = val_beg; // let's pretend we have not found the cookie
3331 txn->cookie_first_date = 0;
3332 txn->cookie_last_date = 0;
3333 }
3334
3335 /* Here, we'll look for the first running server which supports the cookie.
3336 * This allows to share a same cookie between several servers, for example
3337 * to dedicate backup servers to specific servers only.
3338 * However, to prevent clients from sticking to cookie-less backup server
3339 * when they have incidentely learned an empty cookie, we simply ignore
3340 * empty cookies and mark them as invalid.
3341 * The same behaviour is applied when persistence must be ignored.
3342 */
3343 if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
3344 srv = NULL;
3345
3346 while (srv) {
3347 if (srv->cookie && (srv->cklen == delim - val_beg) &&
3348 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
3349 if ((srv->cur_state != SRV_ST_STOPPED) ||
3350 (s->be->options & PR_O_PERSIST) ||
3351 (s->flags & SF_FORCE_PRST)) {
3352 /* we found the server and we can use it */
3353 txn->flags &= ~TX_CK_MASK;
3354 txn->flags |= (srv->cur_state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
3355 s->flags |= SF_DIRECT | SF_ASSIGNED;
3356 s->target = &srv->obj_type;
3357 break;
3358 } else {
3359 /* we found a server, but it's down,
3360 * mark it as such and go on in case
3361 * another one is available.
3362 */
3363 txn->flags &= ~TX_CK_MASK;
3364 txn->flags |= TX_CK_DOWN;
3365 }
3366 }
3367 srv = srv->next;
3368 }
3369
3370 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
3371 /* no server matched this cookie or we deliberately skipped it */
3372 txn->flags &= ~TX_CK_MASK;
3373 if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
3374 txn->flags |= TX_CK_UNUSED;
3375 else
3376 txn->flags |= TX_CK_INVALID;
3377 }
3378
3379 /* depending on the cookie mode, we may have to either :
3380 * - delete the complete cookie if we're in insert+indirect mode, so that
3381 * the server never sees it ;
3382 * - remove the server id from the cookie value, and tag the cookie as an
Joseph Herlante9d5c722018-11-25 11:00:25 -08003383 * application cookie so that it does not get accidentally removed later,
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003384 * if we're in cookie prefix mode
3385 */
3386 if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
3387 int delta; /* negative */
3388
3389 memmove(val_beg, delim + 1, hdr_end - (delim + 1));
3390 delta = val_beg - (delim + 1);
3391 val_end += delta;
3392 next += delta;
3393 hdr_end += delta;
3394 del_from = NULL;
3395 preserve_hdr = 1; /* we want to keep this cookie */
3396 }
3397 else if (del_from == NULL &&
3398 (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
3399 del_from = prev;
3400 }
3401 }
3402 else {
3403 /* This is not our cookie, so we must preserve it. But if we already
3404 * scheduled another cookie for removal, we cannot remove the
3405 * complete header, but we can remove the previous block itself.
3406 */
3407 preserve_hdr = 1;
3408
3409 if (del_from != NULL) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003410 int delta = http_del_hdr_value(hdr_beg, hdr_end, &del_from, prev);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003411 if (att_beg >= del_from)
3412 att_beg += delta;
3413 if (att_end >= del_from)
3414 att_end += delta;
3415 val_beg += delta;
3416 val_end += delta;
3417 next += delta;
3418 hdr_end += delta;
3419 prev = del_from;
3420 del_from = NULL;
3421 }
3422 }
3423
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003424 } /* for each cookie */
3425
3426
3427 /* There are no more cookies on this line.
3428 * We may still have one (or several) marked for deletion at the
3429 * end of the line. We must do this now in two ways :
3430 * - if some cookies must be preserved, we only delete from the
3431 * mark to the end of line ;
3432 * - if nothing needs to be preserved, simply delete the whole header
3433 */
3434 if (del_from) {
3435 hdr_end = (preserve_hdr ? del_from : hdr_beg);
3436 }
3437 if ((hdr_end - hdr_beg) != ctx.value.len) {
Christopher Faulet3e2638e2019-06-18 09:49:16 +02003438 if (hdr_beg != hdr_end)
3439 htx_change_blk_value_len(htx, ctx.blk, hdr_end - hdr_beg);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003440 else
3441 http_remove_header(htx, &ctx);
3442 }
3443 } /* for each "Cookie header */
3444}
3445
3446/*
3447 * Manage server-side cookies. It can impact performance by about 2% so it is
3448 * desirable to call it only when needed. This function is also used when we
3449 * just need to know if there is a cookie (eg: for check-cache).
3450 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003451static void http_manage_server_side_cookies(struct stream *s, struct channel *res)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003452{
3453 struct session *sess = s->sess;
3454 struct http_txn *txn = s->txn;
3455 struct htx *htx;
3456 struct http_hdr_ctx ctx;
3457 struct server *srv;
3458 char *hdr_beg, *hdr_end;
3459 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003460
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003461 htx = htxbuf(&res->buf);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003462
3463 ctx.blk = NULL;
Willy Tarreauaa1909e2022-11-14 18:58:35 +01003464 while (http_find_header(htx, ist("Set-Cookie"), &ctx, 1)) {
Olivier Houchardf0f42382019-07-22 17:43:46 +02003465 int is_first = 1;
3466
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003467 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
3468 * <prev> points to the colon.
3469 */
3470 txn->flags |= TX_SCK_PRESENT;
3471
3472 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
3473 * check-cache is enabled) and we are not interested in checking
3474 * them. Warning, the cookie capture is declared in the frontend.
3475 */
3476 if (s->be->cookie_name == NULL && sess->fe->capture_name == NULL)
3477 break;
3478
3479 /* OK so now we know we have to process this response cookie.
3480 * The format of the Set-Cookie header is slightly different
3481 * from the format of the Cookie header in that it does not
3482 * support the comma as a cookie delimiter (thus the header
3483 * cannot be folded) because the Expires attribute described in
3484 * the original Netscape's spec may contain an unquoted date
3485 * with a comma inside. We have to live with this because
3486 * many browsers don't support Max-Age and some browsers don't
3487 * support quoted strings. However the Set-Cookie2 header is
Willy Tarreauaa1909e2022-11-14 18:58:35 +01003488 * clean but basically nobody supports it.
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003489 *
3490 * We have to keep multiple pointers in order to support cookie
3491 * removal at the beginning, middle or end of header without
3492 * corrupting the header (in case of set-cookie2). A special
3493 * pointer, <scav> points to the beginning of the set-cookie-av
3494 * fields after the first semi-colon. The <next> pointer points
3495 * either to the end of line (set-cookie) or next unquoted comma
3496 * (set-cookie2). All of these headers are valid :
3497 *
3498 * hdr_beg hdr_end
3499 * | |
3500 * v |
3501 * NAME1 = VALUE 1 ; Secure; Path="/" |
3502 * NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT v
3503 * NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT
3504 * NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard
3505 * | | | | | | | |
3506 * | | | | | | | +-> next
3507 * | | | | | | +------------> scav
3508 * | | | | | +--------------> val_end
3509 * | | | | +--------------------> val_beg
3510 * | | | +----------------------> equal
3511 * | | +------------------------> att_end
3512 * | +----------------------------> att_beg
3513 * +------------------------------> prev
3514 * -------------------------------> hdr_beg
3515 */
3516 hdr_beg = ctx.value.ptr;
3517 hdr_end = hdr_beg + ctx.value.len;
3518 for (prev = hdr_beg; prev < hdr_end; prev = next) {
3519
3520 /* Iterate through all cookies on this line */
3521
3522 /* find att_beg */
3523 att_beg = prev;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003524 if (!is_first)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003525 att_beg++;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003526 is_first = 0;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003527
3528 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
3529 att_beg++;
3530
3531 /* find att_end : this is the first character after the last non
3532 * space before the equal. It may be equal to hdr_end.
3533 */
3534 equal = att_end = att_beg;
3535
3536 while (equal < hdr_end) {
Willy Tarreauaa1909e2022-11-14 18:58:35 +01003537 if (*equal == '=' || *equal == ';')
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003538 break;
3539 if (HTTP_IS_SPHT(*equal++))
3540 continue;
3541 att_end = equal;
3542 }
3543
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003544 /* here, <equal> points to '=', a delimiter or the end. <att_end>
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003545 * is between <att_beg> and <equal>, both may be identical.
3546 */
3547
3548 /* look for end of cookie if there is an equal sign */
3549 if (equal < hdr_end && *equal == '=') {
3550 /* look for the beginning of the value */
3551 val_beg = equal + 1;
3552 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
3553 val_beg++;
3554
3555 /* find the end of the value, respecting quotes */
3556 next = http_find_cookie_value_end(val_beg, hdr_end);
3557
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003558 /* make val_end point to the first white space or delimiter after the value */
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003559 val_end = next;
3560 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
3561 val_end--;
3562 }
3563 else {
3564 /* <equal> points to next comma, semi-colon or EOL */
3565 val_beg = val_end = next = equal;
3566 }
3567
3568 if (next < hdr_end) {
Willy Tarreauaa1909e2022-11-14 18:58:35 +01003569 /* For Set-Cookie, since commas are permitted
3570 * in values, skip to the end.
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003571 */
Willy Tarreauaa1909e2022-11-14 18:58:35 +01003572 next = hdr_end;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003573 }
3574
3575 /* Now everything is as on the diagram above */
3576
3577 /* Ignore cookies with no equal sign */
3578 if (equal == val_end)
3579 continue;
3580
3581 /* If there are spaces around the equal sign, we need to
3582 * strip them otherwise we'll get trouble for cookie captures,
3583 * or even for rewrites. Since this happens extremely rarely,
3584 * it does not hurt performance.
3585 */
3586 if (unlikely(att_end != equal || val_beg > equal + 1)) {
3587 int stripped_before = 0;
3588 int stripped_after = 0;
3589
3590 if (att_end != equal) {
3591 memmove(att_end, equal, hdr_end - equal);
3592 stripped_before = (att_end - equal);
3593 equal += stripped_before;
3594 val_beg += stripped_before;
3595 }
3596
3597 if (val_beg > equal + 1) {
3598 memmove(equal + 1, val_beg, hdr_end + stripped_before - val_beg);
3599 stripped_after = (equal + 1) - val_beg;
3600 val_beg += stripped_after;
3601 stripped_before += stripped_after;
3602 }
3603
3604 val_end += stripped_before;
3605 next += stripped_before;
3606 hdr_end += stripped_before;
3607
Christopher Faulet3e2638e2019-06-18 09:49:16 +02003608 htx_change_blk_value_len(htx, ctx.blk, hdr_end - hdr_beg);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003609 ctx.value.len = hdr_end - hdr_beg;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003610 }
3611
3612 /* First, let's see if we want to capture this cookie. We check
3613 * that we don't already have a server side cookie, because we
3614 * can only capture one. Also as an optimisation, we ignore
3615 * cookies shorter than the declared name.
3616 */
3617 if (sess->fe->capture_name != NULL &&
3618 txn->srv_cookie == NULL &&
3619 (val_end - att_beg >= sess->fe->capture_namelen) &&
3620 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
3621 int log_len = val_end - att_beg;
3622 if ((txn->srv_cookie = pool_alloc(pool_head_capture)) == NULL) {
3623 ha_alert("HTTP logging : out of memory.\n");
3624 }
3625 else {
3626 if (log_len > sess->fe->capture_len)
3627 log_len = sess->fe->capture_len;
3628 memcpy(txn->srv_cookie, att_beg, log_len);
3629 txn->srv_cookie[log_len] = 0;
3630 }
3631 }
3632
3633 srv = objt_server(s->target);
3634 /* now check if we need to process it for persistence */
3635 if (!(s->flags & SF_IGNORE_PRST) &&
3636 (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
3637 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
3638 /* assume passive cookie by default */
3639 txn->flags &= ~TX_SCK_MASK;
3640 txn->flags |= TX_SCK_FOUND;
3641
3642 /* If the cookie is in insert mode on a known server, we'll delete
3643 * this occurrence because we'll insert another one later.
3644 * We'll delete it too if the "indirect" option is set and we're in
3645 * a direct access.
3646 */
3647 if (s->be->ck_opts & PR_CK_PSV) {
3648 /* The "preserve" flag was set, we don't want to touch the
3649 * server's cookie.
3650 */
3651 }
3652 else if ((srv && (s->be->ck_opts & PR_CK_INS)) ||
3653 ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) {
3654 /* this cookie must be deleted */
3655 if (prev == hdr_beg && next == hdr_end) {
3656 /* whole header */
3657 http_remove_header(htx, &ctx);
3658 /* note: while both invalid now, <next> and <hdr_end>
3659 * are still equal, so the for() will stop as expected.
3660 */
3661 } else {
3662 /* just remove the value */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003663 int delta = http_del_hdr_value(hdr_beg, hdr_end, &prev, next);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003664 next = prev;
3665 hdr_end += delta;
3666 }
3667 txn->flags &= ~TX_SCK_MASK;
3668 txn->flags |= TX_SCK_DELETED;
3669 /* and go on with next cookie */
3670 }
3671 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) {
3672 /* replace bytes val_beg->val_end with the cookie name associated
3673 * with this server since we know it.
3674 */
3675 int sliding, delta;
3676
3677 ctx.value = ist2(val_beg, val_end - val_beg);
3678 ctx.lws_before = ctx.lws_after = 0;
3679 http_replace_header_value(htx, &ctx, ist2(srv->cookie, srv->cklen));
3680 delta = srv->cklen - (val_end - val_beg);
3681 sliding = (ctx.value.ptr - val_beg);
3682 hdr_beg += sliding;
3683 val_beg += sliding;
3684 next += sliding + delta;
3685 hdr_end += sliding + delta;
3686
3687 txn->flags &= ~TX_SCK_MASK;
3688 txn->flags |= TX_SCK_REPLACED;
3689 }
3690 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) {
3691 /* insert the cookie name associated with this server
3692 * before existing cookie, and insert a delimiter between them..
3693 */
3694 int sliding, delta;
3695 ctx.value = ist2(val_beg, 0);
3696 ctx.lws_before = ctx.lws_after = 0;
3697 http_replace_header_value(htx, &ctx, ist2(srv->cookie, srv->cklen + 1));
3698 delta = srv->cklen + 1;
3699 sliding = (ctx.value.ptr - val_beg);
3700 hdr_beg += sliding;
3701 val_beg += sliding;
3702 next += sliding + delta;
3703 hdr_end += sliding + delta;
3704
3705 val_beg[srv->cklen] = COOKIE_DELIM;
3706 txn->flags &= ~TX_SCK_MASK;
3707 txn->flags |= TX_SCK_REPLACED;
3708 }
3709 }
3710 /* that's done for this cookie, check the next one on the same
Willy Tarreauaa1909e2022-11-14 18:58:35 +01003711 * line when next != hdr_end (which should normally not happen
3712 * with set-cookie2 support removed).
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003713 */
3714 }
3715 }
3716}
3717
Christopher Faulet25a02f62018-10-24 12:00:25 +02003718/*
3719 * Parses the Cache-Control and Pragma request header fields to determine if
3720 * the request may be served from the cache and/or if it is cacheable. Updates
3721 * s->txn->flags.
3722 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003723void http_check_request_for_cacheability(struct stream *s, struct channel *req)
Christopher Faulet25a02f62018-10-24 12:00:25 +02003724{
3725 struct http_txn *txn = s->txn;
3726 struct htx *htx;
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003727 struct http_hdr_ctx ctx = { .blk = NULL };
3728 int pragma_found, cc_found;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003729
3730 if ((txn->flags & (TX_CACHEABLE|TX_CACHE_IGNORE)) == TX_CACHE_IGNORE)
3731 return; /* nothing more to do here */
3732
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003733 htx = htxbuf(&req->buf);
Christopher Faulet25a02f62018-10-24 12:00:25 +02003734 pragma_found = cc_found = 0;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003735
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003736 /* Check "pragma" header for HTTP/1.0 compatibility. */
3737 if (http_find_header(htx, ist("pragma"), &ctx, 1)) {
3738 if (isteqi(ctx.value, ist("no-cache"))) {
3739 pragma_found = 1;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003740 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003741 }
Christopher Faulet25a02f62018-10-24 12:00:25 +02003742
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003743 ctx.blk = NULL;
3744 /* Don't use the cache and don't try to store if we found the
3745 * Authorization header */
3746 if (http_find_header(htx, ist("authorization"), &ctx, 1)) {
3747 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3748 txn->flags |= TX_CACHE_IGNORE;
3749 }
Christopher Faulet25a02f62018-10-24 12:00:25 +02003750
Christopher Faulet25a02f62018-10-24 12:00:25 +02003751
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003752 /* Look for "cache-control" header and iterate over all the values
3753 * until we find one that specifies that caching is possible or not. */
3754 ctx.blk = NULL;
3755 while (http_find_header(htx, ist("cache-control"), &ctx, 0)) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003756 cc_found = 1;
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003757 /* We don't check the values after max-age, max-stale nor min-fresh,
3758 * we simply don't use the cache when they're specified. */
3759 if (istmatchi(ctx.value, ist("max-age")) ||
3760 istmatchi(ctx.value, ist("no-cache")) ||
3761 istmatchi(ctx.value, ist("max-stale")) ||
3762 istmatchi(ctx.value, ist("min-fresh"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003763 txn->flags |= TX_CACHE_IGNORE;
3764 continue;
3765 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003766 if (istmatchi(ctx.value, ist("no-store"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003767 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3768 continue;
3769 }
3770 }
3771
3772 /* RFC7234#5.4:
3773 * When the Cache-Control header field is also present and
3774 * understood in a request, Pragma is ignored.
3775 * When the Cache-Control header field is not present in a
3776 * request, caches MUST consider the no-cache request
3777 * pragma-directive as having the same effect as if
3778 * "Cache-Control: no-cache" were present.
3779 */
3780 if (!cc_found && pragma_found)
3781 txn->flags |= TX_CACHE_IGNORE;
3782}
3783
3784/*
3785 * Check if response is cacheable or not. Updates s->txn->flags.
3786 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003787void http_check_response_for_cacheability(struct stream *s, struct channel *res)
Christopher Faulet25a02f62018-10-24 12:00:25 +02003788{
3789 struct http_txn *txn = s->txn;
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003790 struct http_hdr_ctx ctx = { .blk = NULL };
Christopher Faulet25a02f62018-10-24 12:00:25 +02003791 struct htx *htx;
Remi Tricot-Le Bretoncc9bf2e2020-11-12 11:14:41 +01003792 int has_freshness_info = 0;
3793 int has_validator = 0;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003794
3795 if (txn->status < 200) {
3796 /* do not try to cache interim responses! */
3797 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3798 return;
3799 }
3800
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003801 htx = htxbuf(&res->buf);
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003802 /* Check "pragma" header for HTTP/1.0 compatibility. */
3803 if (http_find_header(htx, ist("pragma"), &ctx, 1)) {
3804 if (isteqi(ctx.value, ist("no-cache"))) {
3805 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3806 return;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003807 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003808 }
Christopher Faulet25a02f62018-10-24 12:00:25 +02003809
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003810 /* Look for "cache-control" header and iterate over all the values
3811 * until we find one that specifies that caching is possible or not. */
3812 ctx.blk = NULL;
3813 while (http_find_header(htx, ist("cache-control"), &ctx, 0)) {
3814 if (isteqi(ctx.value, ist("public"))) {
3815 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003816 continue;
3817 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003818 if (isteqi(ctx.value, ist("private")) ||
3819 isteqi(ctx.value, ist("no-cache")) ||
3820 isteqi(ctx.value, ist("no-store")) ||
3821 isteqi(ctx.value, ist("max-age=0")) ||
3822 isteqi(ctx.value, ist("s-maxage=0"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003823 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003824 continue;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003825 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003826 /* We might have a no-cache="set-cookie" form. */
3827 if (istmatchi(ctx.value, ist("no-cache=\"set-cookie"))) {
3828 txn->flags &= ~TX_CACHE_COOK;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003829 continue;
3830 }
Remi Tricot-Le Bretoncc9bf2e2020-11-12 11:14:41 +01003831
3832 if (istmatchi(ctx.value, ist("s-maxage")) ||
3833 istmatchi(ctx.value, ist("max-age"))) {
3834 has_freshness_info = 1;
3835 continue;
3836 }
3837 }
3838
3839 /* If no freshness information could be found in Cache-Control values,
3840 * look for an Expires header. */
3841 if (!has_freshness_info) {
3842 ctx.blk = NULL;
3843 has_freshness_info = http_find_header(htx, ist("expires"), &ctx, 0);
Christopher Faulet25a02f62018-10-24 12:00:25 +02003844 }
Remi Tricot-Le Bretoncc9bf2e2020-11-12 11:14:41 +01003845
3846 /* If no freshness information could be found in Cache-Control or Expires
3847 * values, look for an explicit validator. */
3848 if (!has_freshness_info) {
3849 ctx.blk = NULL;
3850 has_validator = 1;
3851 if (!http_find_header(htx, ist("etag"), &ctx, 0)) {
3852 ctx.blk = NULL;
3853 if (!http_find_header(htx, ist("last-modified"), &ctx, 0))
3854 has_validator = 0;
3855 }
3856 }
3857
3858 /* We won't store an entry that has neither a cache validator nor an
3859 * explicit expiration time, as suggested in RFC 7234#3. */
3860 if (!has_freshness_info && !has_validator)
3861 txn->flags |= TX_CACHE_IGNORE;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003862}
3863
Christopher Faulet377c5a52018-10-24 21:21:30 +02003864/*
3865 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
3866 * for the current backend.
3867 *
3868 * It is assumed that the request is either a HEAD, GET, or POST and that the
3869 * uri_auth field is valid.
3870 *
3871 * Returns 1 if stats should be provided, otherwise 0.
3872 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003873static int http_stats_check_uri(struct stream *s, struct http_txn *txn, struct proxy *backend)
Christopher Faulet377c5a52018-10-24 21:21:30 +02003874{
3875 struct uri_auth *uri_auth = backend->uri_auth;
3876 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003877 struct htx_sl *sl;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003878 struct ist uri;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003879
3880 if (!uri_auth)
3881 return 0;
3882
3883 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
3884 return 0;
3885
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003886 htx = htxbuf(&s->req.buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02003887 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003888 uri = htx_sl_req_uri(sl);
Amaury Denoyellec453f952021-07-06 11:40:12 +02003889 if (*uri_auth->uri_prefix == '/') {
3890 struct http_uri_parser parser = http_uri_parser_init(uri);
3891 uri = http_parse_path(&parser);
3892 }
Christopher Faulet377c5a52018-10-24 21:21:30 +02003893
3894 /* check URI size */
3895 if (uri_auth->uri_len > uri.len)
3896 return 0;
3897
3898 if (memcmp(uri.ptr, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
3899 return 0;
3900
3901 return 1;
3902}
3903
3904/* This function prepares an applet to handle the stats. It can deal with the
3905 * "100-continue" expectation, check that admin rules are met for POST requests,
3906 * and program a response message if something was unexpected. It cannot fail
3907 * and always relies on the stats applet to complete the job. It does not touch
3908 * analysers nor counters, which are left to the caller. It does not touch
3909 * s->target which is supposed to already point to the stats applet. The caller
3910 * is expected to have already assigned an appctx to the stream.
3911 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003912static int http_handle_stats(struct stream *s, struct channel *req)
Christopher Faulet377c5a52018-10-24 21:21:30 +02003913{
3914 struct stats_admin_rule *stats_admin_rule;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003915 struct session *sess = s->sess;
3916 struct http_txn *txn = s->txn;
3917 struct http_msg *msg = &txn->req;
3918 struct uri_auth *uri_auth = s->be->uri_auth;
3919 const char *h, *lookup, *end;
Willy Tarreau8e7c6e62022-05-18 17:58:02 +02003920 struct appctx *appctx = __sc_appctx(s->scb);
Willy Tarreau91cefca2022-05-03 17:08:29 +02003921 struct show_stat_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx));
Christopher Faulet377c5a52018-10-24 21:21:30 +02003922 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003923 struct htx_sl *sl;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003924
Willy Tarreau41f88522022-05-03 18:39:27 +02003925 appctx->st1 = 0;
Willy Tarreau6ef16482022-05-06 18:07:53 +02003926 ctx->state = STAT_STATE_INIT;
Willy Tarreau91cefca2022-05-03 17:08:29 +02003927 ctx->st_code = STAT_STATUS_INIT;
3928 ctx->flags |= uri_auth->flags;
3929 ctx->flags |= STAT_FMT_HTML; /* assume HTML mode by default */
Christopher Faulet377c5a52018-10-24 21:21:30 +02003930 if ((msg->flags & HTTP_MSGF_VER_11) && (txn->meth != HTTP_METH_HEAD))
Willy Tarreau91cefca2022-05-03 17:08:29 +02003931 ctx->flags |= STAT_CHUNKED;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003932
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003933 htx = htxbuf(&req->buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02003934 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003935 lookup = HTX_SL_REQ_UPTR(sl) + uri_auth->uri_len;
3936 end = HTX_SL_REQ_UPTR(sl) + HTX_SL_REQ_ULEN(sl);
Christopher Faulet377c5a52018-10-24 21:21:30 +02003937
3938 for (h = lookup; h <= end - 3; h++) {
3939 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02003940 ctx->flags |= STAT_HIDE_DOWN;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003941 break;
3942 }
Amaury Denoyelle91e55ea2021-02-25 14:46:08 +01003943 }
3944
3945 for (h = lookup; h <= end - 9; h++) {
3946 if (memcmp(h, ";no-maint", 9) == 0) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02003947 ctx->flags |= STAT_HIDE_MAINT;
Willy Tarreau3e320362020-10-23 17:28:57 +02003948 break;
3949 }
Christopher Faulet377c5a52018-10-24 21:21:30 +02003950 }
3951
3952 if (uri_auth->refresh) {
3953 for (h = lookup; h <= end - 10; h++) {
3954 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02003955 ctx->flags |= STAT_NO_REFRESH;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003956 break;
3957 }
3958 }
3959 }
3960
3961 for (h = lookup; h <= end - 4; h++) {
3962 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02003963 ctx->flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
Christopher Faulet377c5a52018-10-24 21:21:30 +02003964 break;
3965 }
3966 }
3967
3968 for (h = lookup; h <= end - 6; h++) {
3969 if (memcmp(h, ";typed", 6) == 0) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02003970 ctx->flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
3971 ctx->flags |= STAT_FMT_TYPED;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003972 break;
3973 }
3974 }
3975
Christopher Faulet6338a082019-09-09 15:50:54 +02003976 for (h = lookup; h <= end - 5; h++) {
3977 if (memcmp(h, ";json", 5) == 0) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02003978 ctx->flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
3979 ctx->flags |= STAT_FMT_JSON;
Christopher Faulet6338a082019-09-09 15:50:54 +02003980 break;
3981 }
3982 }
3983
3984 for (h = lookup; h <= end - 12; h++) {
3985 if (memcmp(h, ";json-schema", 12) == 0) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02003986 ctx->flags &= ~STAT_FMT_MASK;
3987 ctx->flags |= STAT_JSON_SCHM;
Christopher Faulet6338a082019-09-09 15:50:54 +02003988 break;
3989 }
3990 }
3991
Christopher Faulet377c5a52018-10-24 21:21:30 +02003992 for (h = lookup; h <= end - 8; h++) {
3993 if (memcmp(h, ";st=", 4) == 0) {
3994 int i;
3995 h += 4;
Willy Tarreau91cefca2022-05-03 17:08:29 +02003996 ctx->st_code = STAT_STATUS_UNKN;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003997 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
3998 if (strncmp(stat_status_codes[i], h, 4) == 0) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02003999 ctx->st_code = i;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004000 break;
4001 }
4002 }
4003 break;
4004 }
4005 }
4006
Willy Tarreau91cefca2022-05-03 17:08:29 +02004007 ctx->scope_str = 0;
4008 ctx->scope_len = 0;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004009 for (h = lookup; h <= end - 8; h++) {
4010 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
4011 int itx = 0;
4012 const char *h2;
4013 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
4014 const char *err;
4015
4016 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
4017 h2 = h;
Willy Tarreau91cefca2022-05-03 17:08:29 +02004018 ctx->scope_str = h2 - HTX_SL_REQ_UPTR(sl);
Christopher Fauleted7a0662019-01-14 11:07:34 +01004019 while (h < end) {
Christopher Faulet377c5a52018-10-24 21:21:30 +02004020 if (*h == ';' || *h == '&' || *h == ' ')
4021 break;
4022 itx++;
4023 h++;
4024 }
4025
4026 if (itx > STAT_SCOPE_TXT_MAXLEN)
4027 itx = STAT_SCOPE_TXT_MAXLEN;
Willy Tarreau91cefca2022-05-03 17:08:29 +02004028 ctx->scope_len = itx;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004029
Willy Tarreau91cefca2022-05-03 17:08:29 +02004030 /* scope_txt = search query, ctx->scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Christopher Faulet377c5a52018-10-24 21:21:30 +02004031 memcpy(scope_txt, h2, itx);
4032 scope_txt[itx] = '\0';
4033 err = invalid_char(scope_txt);
4034 if (err) {
4035 /* bad char in search text => clear scope */
Willy Tarreau91cefca2022-05-03 17:08:29 +02004036 ctx->scope_str = 0;
4037 ctx->scope_len = 0;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004038 }
4039 break;
4040 }
4041 }
4042
4043 /* now check whether we have some admin rules for this request */
4044 list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) {
4045 int ret = 1;
4046
4047 if (stats_admin_rule->cond) {
4048 ret = acl_exec_cond(stats_admin_rule->cond, s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
4049 ret = acl_pass(ret);
4050 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
4051 ret = !ret;
4052 }
4053
4054 if (ret) {
4055 /* no rule, or the rule matches */
Willy Tarreau91cefca2022-05-03 17:08:29 +02004056 ctx->flags |= STAT_ADMIN;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004057 break;
4058 }
4059 }
4060
Christopher Faulet5d45e382019-02-27 15:15:23 +01004061 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
4062 appctx->st0 = STAT_HTTP_HEAD;
4063 else if (txn->meth == HTTP_METH_POST) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02004064 if (ctx->flags & STAT_ADMIN) {
Christopher Faulet377c5a52018-10-24 21:21:30 +02004065 appctx->st0 = STAT_HTTP_POST;
Christopher Fauletbd9e8422019-08-15 22:26:48 +02004066 if (msg->msg_state < HTTP_MSG_DATA)
4067 req->analysers |= AN_REQ_HTTP_BODY;
4068 }
Christopher Faulet377c5a52018-10-24 21:21:30 +02004069 else {
Christopher Faulet5d45e382019-02-27 15:15:23 +01004070 /* POST without admin level */
Willy Tarreau91cefca2022-05-03 17:08:29 +02004071 ctx->flags &= ~STAT_CHUNKED;
4072 ctx->st_code = STAT_STATUS_DENY;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004073 appctx->st0 = STAT_HTTP_LAST;
4074 }
4075 }
4076 else {
Christopher Faulet5d45e382019-02-27 15:15:23 +01004077 /* Unsupported method */
Willy Tarreau91cefca2022-05-03 17:08:29 +02004078 ctx->flags &= ~STAT_CHUNKED;
4079 ctx->st_code = STAT_STATUS_IVAL;
Christopher Faulet5d45e382019-02-27 15:15:23 +01004080 appctx->st0 = STAT_HTTP_LAST;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004081 }
4082
4083 s->task->nice = -32; /* small boost for HTTP statistics */
4084 return 1;
4085}
4086
Christopher Faulet021a8e42021-03-29 10:46:38 +02004087/* This function waits for the message payload at most <time> milliseconds (may
4088 * be set to TICK_ETERNITY). It stops to wait if at least <bytes> bytes of the
4089 * payload are received (0 means no limit). It returns HTTP_RULE_* depending on
4090 * the result:
4091 *
4092 * - HTTP_RULE_RES_CONT when conditions are met to stop waiting
4093 * - HTTP_RULE_RES_YIELD to wait for more data
Ilya Shipitsinb2be9a12021-04-24 13:25:42 +05004094 * - HTTP_RULE_RES_ABRT when a timeout occurred.
Christopher Faulet021a8e42021-03-29 10:46:38 +02004095 * - HTTP_RULE_RES_BADREQ if a parsing error is raised by lower level
Ilya Shipitsinb2be9a12021-04-24 13:25:42 +05004096 * - HTTP_RULE_RES_ERROR if an internal error occurred
Christopher Faulet021a8e42021-03-29 10:46:38 +02004097 *
Ilya Shipitsinb2be9a12021-04-24 13:25:42 +05004098 * If a timeout occurred, this function is responsible to emit the right response
Christopher Faulet021a8e42021-03-29 10:46:38 +02004099 * to the client, depending on the channel (408 on request side, 504 on response
4100 * side). All other errors must be handled by the caller.
4101 */
4102enum rule_result http_wait_for_msg_body(struct stream *s, struct channel *chn,
4103 unsigned int time, unsigned int bytes)
4104{
4105 struct session *sess = s->sess;
4106 struct http_txn *txn = s->txn;
4107 struct http_msg *msg = ((chn->flags & CF_ISRESP) ? &txn->rsp : &txn->req);
4108 struct htx *htx;
4109 enum rule_result ret = HTTP_RULE_RES_CONT;
4110
4111 htx = htxbuf(&chn->buf);
4112
4113 if (htx->flags & HTX_FL_PARSING_ERROR) {
4114 ret = HTTP_RULE_RES_BADREQ;
4115 goto end;
4116 }
4117 if (htx->flags & HTX_FL_PROCESSING_ERROR) {
4118 ret = HTTP_RULE_RES_ERROR;
4119 goto end;
4120 }
4121
4122 /* Do nothing for bodyless and CONNECT requests */
4123 if (txn->meth == HTTP_METH_CONNECT || (msg->flags & HTTP_MSGF_BODYLESS))
4124 goto end;
4125
4126 if (!(chn->flags & CF_ISRESP) && msg->msg_state < HTTP_MSG_DATA) {
4127 if (http_handle_expect_hdr(s, htx, msg) == -1) {
4128 ret = HTTP_RULE_RES_ERROR;
4129 goto end;
4130 }
4131 }
4132
4133 msg->msg_state = HTTP_MSG_DATA;
4134
4135 /* Now we're in HTTP_MSG_DATA. We just need to know if all data have
4136 * been received or if the buffer is full.
4137 */
Christopher Faulet78335962021-09-23 14:46:32 +02004138 if ((htx->flags & HTX_FL_EOM) ||
4139 htx_get_tail_type(htx) > HTX_BLK_DATA ||
4140 channel_htx_full(chn, htx, global.tune.maxrewrite) ||
Willy Tarreau99615ed2022-05-25 07:29:36 +02004141 sc_waiting_room(chn_prod(chn)))
Christopher Faulet021a8e42021-03-29 10:46:38 +02004142 goto end;
4143
4144 if (bytes) {
4145 struct htx_blk *blk;
4146 unsigned int len = 0;
4147
4148 for (blk = htx_get_first_blk(htx); blk; blk = htx_get_next_blk(htx, blk)) {
4149 if (htx_get_blk_type(blk) != HTX_BLK_DATA)
4150 continue;
4151 len += htx_get_blksz(blk);
4152 if (len >= bytes)
4153 goto end;
4154 }
4155 }
4156
4157 if ((chn->flags & CF_READ_TIMEOUT) || tick_is_expired(chn->analyse_exp, now_ms)) {
4158 if (!(chn->flags & CF_ISRESP))
4159 goto abort_req;
4160 goto abort_res;
4161 }
4162
4163 /* we get here if we need to wait for more data */
Christopher Faulet4b490b72023-01-04 11:55:15 +01004164 if (!(chn->flags & CF_SHUTR)) {
Christopher Faulet021a8e42021-03-29 10:46:38 +02004165 if (!tick_isset(chn->analyse_exp))
4166 chn->analyse_exp = tick_add_ifset(now_ms, time);
4167 ret = HTTP_RULE_RES_YIELD;
4168 }
4169
4170 end:
4171 return ret;
4172
Christopher Fauletf0d80df2023-01-13 10:20:20 +01004173 abort:
4174 http_reply_and_close(s, txn->status, http_error_message(s));
4175 ret = HTTP_RULE_RES_ABRT;
4176 goto end;
4177
Christopher Faulet021a8e42021-03-29 10:46:38 +02004178 abort_req:
4179 txn->status = 408;
4180 if (!(s->flags & SF_ERR_MASK))
4181 s->flags |= SF_ERR_CLITO;
Willy Tarreau4781b152021-04-06 13:53:36 +02004182 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req);
Christopher Faulet021a8e42021-03-29 10:46:38 +02004183 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02004184 _HA_ATOMIC_INC(&sess->listener->counters->failed_req);
Christopher Fauletf0d80df2023-01-13 10:20:20 +01004185 goto abort;
Christopher Faulet021a8e42021-03-29 10:46:38 +02004186
4187 abort_res:
4188 txn->status = 504;
4189 if (!(s->flags & SF_ERR_MASK))
4190 s->flags |= SF_ERR_SRVTO;
Christopher Faulet021a8e42021-03-29 10:46:38 +02004191 stream_inc_http_fail_ctr(s);
Christopher Fauletf0d80df2023-01-13 10:20:20 +01004192 goto abort;
Christopher Faulet021a8e42021-03-29 10:46:38 +02004193}
4194
Willy Tarreaub49672d2022-05-27 10:13:37 +02004195void http_perform_server_redirect(struct stream *s, struct stconn *sc)
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004196{
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004197 struct channel *req = &s->req;
4198 struct channel *res = &s->res;
4199 struct server *srv;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004200 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004201 struct htx_sl *sl;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004202 struct ist path, location;
4203 unsigned int flags;
Amaury Denoyellec453f952021-07-06 11:40:12 +02004204 struct http_uri_parser parser;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004205
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004206 /*
4207 * Create the location
4208 */
4209 chunk_reset(&trash);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004210
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004211 /* 1: add the server's prefix */
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004212 /* special prefix "/" means don't change URL */
4213 srv = __objt_server(s->target);
4214 if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') {
4215 if (!chunk_memcat(&trash, srv->rdr_pfx, srv->rdr_len))
4216 return;
4217 }
4218
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004219 /* 2: add the request Path */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004220 htx = htxbuf(&req->buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02004221 sl = http_get_stline(htx);
Amaury Denoyellec453f952021-07-06 11:40:12 +02004222 parser = http_uri_parser_init(htx_sl_req_uri(sl));
4223 path = http_parse_path(&parser);
Tim Duesterhused526372020-03-05 17:56:33 +01004224 if (!isttest(path))
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004225 return;
4226
4227 if (!chunk_memcat(&trash, path.ptr, path.len))
4228 return;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004229 location = ist2(trash.area, trash.data);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004230
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004231 /*
Ilya Shipitsin4a689da2022-10-29 09:34:32 +05004232 * Create the 302 response
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004233 */
4234 htx = htx_from_buf(&res->buf);
4235 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
4236 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
4237 ist("HTTP/1.1"), ist("302"), ist("Found"));
4238 if (!sl)
4239 goto fail;
4240 sl->info.res.status = 302;
4241 s->txn->status = 302;
4242
4243 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) ||
4244 !htx_add_header(htx, ist("Connection"), ist("close")) ||
4245 !htx_add_header(htx, ist("Content-length"), ist("0")) ||
4246 !htx_add_header(htx, ist("Location"), location))
4247 goto fail;
4248
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004249 if (!htx_add_endof(htx, HTX_BLK_EOH))
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004250 goto fail;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004251
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004252 htx->flags |= HTX_FL_EOM;
Christopher Fauletc20afb82020-01-24 19:16:26 +01004253 htx_to_buf(htx, &res->buf);
Christopher Fauleta72a7e42020-01-28 09:28:11 +01004254 if (!http_forward_proxy_resp(s, 1))
4255 goto fail;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004256
4257 /* return without error. */
Willy Tarreaub49672d2022-05-27 10:13:37 +02004258 sc_shutr(sc);
4259 sc_shutw(sc);
Christopher Faulet50264b42022-03-30 19:39:30 +02004260 s->conn_err_type = STRM_ET_NONE;
Willy Tarreaub49672d2022-05-27 10:13:37 +02004261 sc->state = SC_ST_CLO;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004262
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004263 if (!(s->flags & SF_ERR_MASK))
4264 s->flags |= SF_ERR_LOCAL;
4265 if (!(s->flags & SF_FINST_MASK))
4266 s->flags |= SF_FINST_C;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004267
4268 /* FIXME: we should increase a counter of redirects per server and per backend. */
4269 srv_inc_sess_ctr(srv);
4270 srv_set_sess_last(srv);
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004271 return;
4272
4273 fail:
4274 /* If an error occurred, remove the incomplete HTTP response from the
4275 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004276 channel_htx_truncate(res, htx);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004277}
4278
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05004279/* This function terminates the request because it was completely analyzed or
Christopher Fauletf2824e62018-10-01 12:12:37 +02004280 * because an error was triggered during the body forwarding.
4281 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004282static void http_end_request(struct stream *s)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004283{
4284 struct channel *chn = &s->req;
4285 struct http_txn *txn = s->txn;
4286
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004287 DBG_TRACE_ENTER(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004288
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004289 if (unlikely(txn->req.msg_state < HTTP_MSG_DONE)) {
4290 DBG_TRACE_DEVEL("waiting end of the request", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004291 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004292 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004293
4294 if (txn->req.msg_state == HTTP_MSG_DONE) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004295 /* No need to read anymore, the request was completely parsed.
4296 * We can shut the read side unless we want to abort_on_close,
4297 * or we have a POST request. The issue with POST requests is
4298 * that some browsers still send a CRLF after the request, and
4299 * this CRLF must be read so that it does not remain in the kernel
4300 * buffers, otherwise a close could cause an RST on some systems
4301 * (eg: Linux).
4302 */
Christopher Faulet769d0e92019-03-22 14:23:18 +01004303 if (!(s->be->options & PR_O_ABRT_CLOSE) && txn->meth != HTTP_METH_POST)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004304 channel_dont_read(chn);
4305
4306 /* if the server closes the connection, we want to immediately react
4307 * and close the socket to save packets and syscalls.
4308 */
Willy Tarreaucb041662022-05-17 19:44:42 +02004309 s->scb->flags |= SC_FL_NOHALF;
Christopher Fauletf2824e62018-10-01 12:12:37 +02004310
4311 /* In any case we've finished parsing the request so we must
4312 * disable Nagle when sending data because 1) we're not going
4313 * to shut this side, and 2) the server is waiting for us to
4314 * send pending data.
4315 */
4316 chn->flags |= CF_NEVER_WAIT;
4317
Christopher Fauletd01ce402019-01-02 17:44:13 +01004318 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
4319 /* The server has not finished to respond, so we
4320 * don't want to move in order not to upset it.
4321 */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004322 DBG_TRACE_DEVEL("waiting end of the response", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletd01ce402019-01-02 17:44:13 +01004323 return;
4324 }
4325
Christopher Fauletf2824e62018-10-01 12:12:37 +02004326 /* When we get here, it means that both the request and the
4327 * response have finished receiving. Depending on the connection
4328 * mode, we'll have to wait for the last bytes to leave in either
4329 * direction, and sometimes for a close to be effective.
4330 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02004331 if (txn->flags & TX_CON_WANT_TUN) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004332 /* Tunnel mode will not have any analyser so it needs to
4333 * poll for reads.
4334 */
4335 channel_auto_read(chn);
4336 txn->req.msg_state = HTTP_MSG_TUNNEL;
4337 }
4338 else {
4339 /* we're not expecting any new data to come for this
4340 * transaction, so we can close it.
Christopher Faulet9768c262018-10-22 09:34:31 +02004341 *
4342 * However, there is an exception if the response
4343 * length is undefined. In this case, we need to wait
4344 * the close from the server. The response will be
4345 * switched in TUNNEL mode until the end.
Christopher Fauletf2824e62018-10-01 12:12:37 +02004346 */
4347 if (!(txn->rsp.flags & HTTP_MSGF_XFER_LEN) &&
4348 txn->rsp.msg_state != HTTP_MSG_CLOSED)
Christopher Faulet9768c262018-10-22 09:34:31 +02004349 goto check_channel_flags;
Christopher Fauletf2824e62018-10-01 12:12:37 +02004350
4351 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4352 channel_shutr_now(chn);
4353 channel_shutw_now(chn);
4354 }
4355 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004356 goto check_channel_flags;
4357 }
4358
4359 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
4360 http_msg_closing:
4361 /* nothing else to forward, just waiting for the output buffer
4362 * to be empty and for the shutw_now to take effect.
4363 */
4364 if (channel_is_empty(chn)) {
4365 txn->req.msg_state = HTTP_MSG_CLOSED;
4366 goto http_msg_closed;
4367 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004368 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004369 return;
4370 }
4371
4372 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
4373 http_msg_closed:
Christopher Fauletf2824e62018-10-01 12:12:37 +02004374 /* if we don't know whether the server will close, we need to hard close */
4375 if (txn->rsp.flags & HTTP_MSGF_XFER_LEN)
Willy Tarreaucb041662022-05-17 19:44:42 +02004376 s->scb->flags |= SC_FL_NOLINGER; /* we want to close ASAP */
Christopher Fauletf2824e62018-10-01 12:12:37 +02004377 /* see above in MSG_DONE why we only do this in these states */
Christopher Faulet769d0e92019-03-22 14:23:18 +01004378 if (!(s->be->options & PR_O_ABRT_CLOSE))
Christopher Fauletf2824e62018-10-01 12:12:37 +02004379 channel_dont_read(chn);
4380 goto end;
4381 }
4382
4383 check_channel_flags:
4384 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4385 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4386 /* if we've just closed an output, let's switch */
4387 txn->req.msg_state = HTTP_MSG_CLOSING;
4388 goto http_msg_closing;
4389 }
4390
4391 end:
4392 chn->analysers &= AN_REQ_FLT_END;
Christopher Faulet198ef8b2020-12-15 13:32:55 +01004393 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
4394 chn->flags |= CF_NEVER_WAIT;
4395 if (HAS_REQ_DATA_FILTERS(s))
Christopher Fauletf2824e62018-10-01 12:12:37 +02004396 chn->analysers |= AN_REQ_FLT_XFER_DATA;
Christopher Faulet198ef8b2020-12-15 13:32:55 +01004397 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004398 channel_auto_close(chn);
4399 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004400 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004401}
4402
4403
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05004404/* This function terminates the response because it was completely analyzed or
Christopher Fauletf2824e62018-10-01 12:12:37 +02004405 * because an error was triggered during the body forwarding.
4406 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004407static void http_end_response(struct stream *s)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004408{
4409 struct channel *chn = &s->res;
4410 struct http_txn *txn = s->txn;
4411
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004412 DBG_TRACE_ENTER(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004413
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004414 if (unlikely(txn->rsp.msg_state < HTTP_MSG_DONE)) {
4415 DBG_TRACE_DEVEL("waiting end of the response", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004416 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004417 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004418
4419 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
4420 /* In theory, we don't need to read anymore, but we must
4421 * still monitor the server connection for a possible close
4422 * while the request is being uploaded, so we don't disable
4423 * reading.
4424 */
4425 /* channel_dont_read(chn); */
4426
4427 if (txn->req.msg_state < HTTP_MSG_DONE) {
4428 /* The client seems to still be sending data, probably
4429 * because we got an error response during an upload.
4430 * We have the choice of either breaking the connection
4431 * or letting it pass through. Let's do the later.
4432 */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004433 DBG_TRACE_DEVEL("waiting end of the request", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004434 return;
4435 }
4436
4437 /* When we get here, it means that both the request and the
4438 * response have finished receiving. Depending on the connection
4439 * mode, we'll have to wait for the last bytes to leave in either
4440 * direction, and sometimes for a close to be effective.
4441 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02004442 if (txn->flags & TX_CON_WANT_TUN) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004443 channel_auto_read(chn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004444 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
4445 }
4446 else {
4447 /* we're not expecting any new data to come for this
4448 * transaction, so we can close it.
4449 */
4450 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4451 channel_shutr_now(chn);
4452 channel_shutw_now(chn);
4453 }
4454 }
4455 goto check_channel_flags;
4456 }
4457
4458 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
4459 http_msg_closing:
4460 /* nothing else to forward, just waiting for the output buffer
4461 * to be empty and for the shutw_now to take effect.
4462 */
4463 if (channel_is_empty(chn)) {
4464 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4465 goto http_msg_closed;
4466 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004467 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004468 return;
4469 }
4470
4471 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
4472 http_msg_closed:
4473 /* drop any pending data */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004474 channel_htx_truncate(&s->req, htxbuf(&s->req.buf));
Christopher Faulet9768c262018-10-22 09:34:31 +02004475 channel_abort(&s->req);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004476 goto end;
4477 }
4478
4479 check_channel_flags:
4480 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4481 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4482 /* if we've just closed an output, let's switch */
4483 txn->rsp.msg_state = HTTP_MSG_CLOSING;
4484 goto http_msg_closing;
4485 }
4486
4487 end:
4488 chn->analysers &= AN_RES_FLT_END;
Christopher Faulet198ef8b2020-12-15 13:32:55 +01004489 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
4490 chn->flags |= CF_NEVER_WAIT;
4491 if (HAS_RSP_DATA_FILTERS(s))
4492 chn->analysers |= AN_RES_FLT_XFER_DATA;
4493 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004494 channel_auto_close(chn);
4495 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004496 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004497}
4498
Christopher Fauletef70e252020-01-28 09:26:19 +01004499/* Forward a response generated by HAProxy (error/redirect/return). This
4500 * function forwards all pending incoming data. If <final> is set to 0, nothing
4501 * more is performed. It is used for 1xx informational messages. Otherwise, the
Christopher Faulet507479b2020-05-15 12:29:46 +02004502 * transaction is terminated and the request is emptied. On success 1 is
Christopher Faulet40e6b552020-06-25 16:04:50 +02004503 * returned. If an error occurred, 0 is returned. If it fails, this function
4504 * only exits. It is the caller responsibility to do the cleanup.
Christopher Fauletef70e252020-01-28 09:26:19 +01004505 */
4506int http_forward_proxy_resp(struct stream *s, int final)
4507{
4508 struct channel *req = &s->req;
4509 struct channel *res = &s->res;
4510 struct htx *htx = htxbuf(&res->buf);
4511 size_t data;
4512
4513 if (final) {
4514 htx->flags |= HTX_FL_PROXY_RESP;
Christopher Faulet507479b2020-05-15 12:29:46 +02004515
Christopher Fauletaab1b672020-11-18 16:44:02 +01004516 if (!htx_is_empty(htx) && !http_eval_after_res_rules(s))
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01004517 return 0;
Christopher Fauletef70e252020-01-28 09:26:19 +01004518
Christopher Fauletd6c48362020-10-19 18:01:38 +02004519 if (s->txn->meth == HTTP_METH_HEAD)
4520 htx_skip_msg_payload(htx);
4521
Christopher Fauletef70e252020-01-28 09:26:19 +01004522 channel_auto_read(req);
4523 channel_abort(req);
4524 channel_auto_close(req);
4525 channel_htx_erase(req, htxbuf(&req->buf));
4526
4527 res->wex = tick_add_ifset(now_ms, res->wto);
4528 channel_auto_read(res);
4529 channel_auto_close(res);
4530 channel_shutr_now(res);
Christopher Faulet1a9db7c2020-06-25 15:36:45 +02004531 res->flags |= CF_EOI; /* The response is terminated, add EOI */
Christopher Faulet42432f32020-11-20 17:43:16 +01004532 htxbuf(&res->buf)->flags |= HTX_FL_EOM; /* no more data are expected */
Christopher Fauletef70e252020-01-28 09:26:19 +01004533 }
Christopher Fauletcf6898c2020-06-25 15:55:11 +02004534 else {
4535 /* Send ASAP informational messages. Rely on CF_EOI for final
4536 * response.
4537 */
4538 res->flags |= CF_SEND_DONTWAIT;
4539 }
Christopher Fauletef70e252020-01-28 09:26:19 +01004540
4541 data = htx->data - co_data(res);
4542 c_adv(res, data);
4543 htx->first = -1;
4544 res->total += data;
4545 return 1;
4546}
4547
Willy Tarreaub49672d2022-05-27 10:13:37 +02004548void http_server_error(struct stream *s, struct stconn *sc, int err,
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004549 int finst, struct http_reply *msg)
Christopher Faulet0f226952018-10-22 09:29:56 +02004550{
Christopher Faulet72c7d8d2020-01-27 15:32:25 +01004551 http_reply_and_close(s, s->txn->status, msg);
Christopher Faulet0f226952018-10-22 09:29:56 +02004552 if (!(s->flags & SF_ERR_MASK))
4553 s->flags |= err;
4554 if (!(s->flags & SF_FINST_MASK))
4555 s->flags |= finst;
4556}
4557
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004558void http_reply_and_close(struct stream *s, short status, struct http_reply *msg)
Christopher Faulet0f226952018-10-22 09:29:56 +02004559{
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004560 if (!msg) {
4561 channel_htx_truncate(&s->res, htxbuf(&s->res.buf));
4562 goto end;
4563 }
4564
4565 if (http_reply_message(s, msg) == -1) {
4566 /* On error, return a 500 error message, but don't rewrite it if
Christopher Faulet40e6b552020-06-25 16:04:50 +02004567 * it is already an internal error. If it was already a "const"
4568 * 500 error, just fail.
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004569 */
Christopher Faulet40e6b552020-06-25 16:04:50 +02004570 if (s->txn->status == 500) {
4571 if (s->txn->flags & TX_CONST_REPLY)
4572 goto end;
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004573 s->txn->flags |= TX_CONST_REPLY;
Christopher Faulet40e6b552020-06-25 16:04:50 +02004574 }
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004575 s->txn->status = 500;
4576 s->txn->http_reply = NULL;
4577 return http_reply_and_close(s, s->txn->status, http_error_message(s));
4578 }
4579
4580end:
4581 s->res.wex = tick_add_ifset(now_ms, s->res.wto);
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004582
Christopher Faulet2d565002021-09-10 09:17:50 +02004583 /* At this staged, HTTP analysis is finished */
4584 s->req.analysers &= AN_REQ_FLT_END;
4585 s->req.analyse_exp = TICK_ETERNITY;
4586
4587 s->res.analysers &= AN_RES_FLT_END;
4588 s->res.analyse_exp = TICK_ETERNITY;
4589
Christopher Faulet0f226952018-10-22 09:29:56 +02004590 channel_auto_read(&s->req);
4591 channel_abort(&s->req);
4592 channel_auto_close(&s->req);
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004593 channel_htx_erase(&s->req, htxbuf(&s->req.buf));
Christopher Faulet72c7d8d2020-01-27 15:32:25 +01004594 channel_auto_read(&s->res);
4595 channel_auto_close(&s->res);
4596 channel_shutr_now(&s->res);
Christopher Faulet0f226952018-10-22 09:29:56 +02004597}
4598
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004599struct http_reply *http_error_message(struct stream *s)
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004600{
4601 const int msgnum = http_get_status_idx(s->txn->status);
4602
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004603 if (s->txn->http_reply)
4604 return s->txn->http_reply;
4605 else if (s->be->replies[msgnum])
4606 return s->be->replies[msgnum];
4607 else if (strm_fe(s)->replies[msgnum])
4608 return strm_fe(s)->replies[msgnum];
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004609 else
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004610 return &http_err_replies[msgnum];
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004611}
4612
Christopher Faulet40e6b552020-06-25 16:04:50 +02004613/* Produces an HTX message from an http reply. Depending on the http reply type,
4614 * a, errorfile, an raw file or a log-format string is used. On success, it
4615 * returns 0. If an error occurs -1 is returned. If it fails, this function only
4616 * exits. It is the caller responsibility to do the cleanup.
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004617 */
Christopher Fauletae43b6c2020-05-27 15:24:22 +02004618int http_reply_to_htx(struct stream *s, struct htx *htx, struct http_reply *reply)
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004619{
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004620 struct buffer *errmsg;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004621 struct htx_sl *sl;
4622 struct buffer *body = NULL;
4623 const char *status, *reason, *clen, *ctype;
4624 unsigned int slflags;
4625 int ret = 0;
4626
Christopher Faulete29a97e2020-05-14 14:49:25 +02004627 /*
4628 * - HTTP_REPLY_ERRFILES unexpected here. handled as no payload if so
4629 *
4630 * - HTTP_REPLY_INDIRECT: switch on another reply if defined or handled
4631 * as no payload if NULL. the TXN status code is set with the status
4632 * of the original reply.
4633 */
4634
4635 if (reply->type == HTTP_REPLY_INDIRECT) {
4636 if (reply->body.reply)
4637 reply = reply->body.reply;
4638 }
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004639 if (reply->type == HTTP_REPLY_ERRMSG && !reply->body.errmsg) {
4640 /* get default error message */
4641 if (reply == s->txn->http_reply)
4642 s->txn->http_reply = NULL;
4643 reply = http_error_message(s);
4644 if (reply->type == HTTP_REPLY_INDIRECT) {
4645 if (reply->body.reply)
4646 reply = reply->body.reply;
4647 }
4648 }
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004649
4650 if (reply->type == HTTP_REPLY_ERRMSG) {
4651 /* implicit or explicit error message*/
4652 errmsg = reply->body.errmsg;
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004653 if (errmsg && !b_is_null(errmsg)) {
Christopher Faulet20567362020-05-15 14:52:49 +02004654 if (!htx_copy_msg(htx, errmsg))
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004655 goto fail;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004656 }
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004657 }
4658 else {
4659 /* no payload, file or log-format string */
4660 if (reply->type == HTTP_REPLY_RAW) {
4661 /* file */
4662 body = &reply->body.obj;
4663 }
4664 else if (reply->type == HTTP_REPLY_LOGFMT) {
4665 /* log-format string */
4666 body = alloc_trash_chunk();
4667 if (!body)
4668 goto fail_alloc;
4669 body->data = build_logline(s, body->area, body->size, &reply->body.fmt);
4670 }
4671 /* else no payload */
4672
4673 status = ultoa(reply->status);
4674 reason = http_get_reason(reply->status);
4675 slflags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_CLEN);
4676 if (!body || !b_data(body))
4677 slflags |= HTX_SL_F_BODYLESS;
4678 sl = htx_add_stline(htx, HTX_BLK_RES_SL, slflags, ist("HTTP/1.1"), ist(status), ist(reason));
4679 if (!sl)
4680 goto fail;
4681 sl->info.res.status = reply->status;
4682
4683 clen = (body ? ultoa(b_data(body)) : "0");
4684 ctype = reply->ctype;
4685
4686 if (!LIST_ISEMPTY(&reply->hdrs)) {
4687 struct http_reply_hdr *hdr;
4688 struct buffer *value = alloc_trash_chunk();
4689
4690 if (!value)
4691 goto fail;
4692
4693 list_for_each_entry(hdr, &reply->hdrs, list) {
4694 chunk_reset(value);
4695 value->data = build_logline(s, value->area, value->size, &hdr->value);
4696 if (b_data(value) && !htx_add_header(htx, hdr->name, ist2(b_head(value), b_data(value)))) {
4697 free_trash_chunk(value);
4698 goto fail;
4699 }
4700 chunk_reset(value);
4701 }
4702 free_trash_chunk(value);
4703 }
4704
4705 if (!htx_add_header(htx, ist("content-length"), ist(clen)) ||
4706 (body && b_data(body) && ctype && !htx_add_header(htx, ist("content-type"), ist(ctype))) ||
4707 !htx_add_endof(htx, HTX_BLK_EOH) ||
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004708 (body && b_data(body) && !htx_add_data_atonce(htx, ist2(b_head(body), b_data(body)))))
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004709 goto fail;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004710
4711 htx->flags |= HTX_FL_EOM;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004712 }
4713
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004714 leave:
4715 if (reply->type == HTTP_REPLY_LOGFMT)
4716 free_trash_chunk(body);
4717 return ret;
4718
4719 fail_alloc:
4720 if (!(s->flags & SF_ERR_MASK))
4721 s->flags |= SF_ERR_RESOURCE;
Christopher Faulet97e466c2020-05-15 15:12:47 +02004722 /* fall through */
4723 fail:
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004724 ret = -1;
4725 goto leave;
Christopher Faulet97e466c2020-05-15 15:12:47 +02004726}
4727
4728/* Send an http reply to the client. On success, it returns 0. If an error
Christopher Faulet40e6b552020-06-25 16:04:50 +02004729 * occurs -1 is returned and the response channel is truncated, removing this
4730 * way the faulty reply. This function may fail when the reply is formatted
4731 * (http_reply_to_htx) or when the reply is forwarded
4732 * (http_forward_proxy_resp). On the last case, it is because a
4733 * http-after-response rule fails.
Christopher Faulet97e466c2020-05-15 15:12:47 +02004734 */
4735int http_reply_message(struct stream *s, struct http_reply *reply)
4736{
4737 struct channel *res = &s->res;
4738 struct htx *htx = htx_from_buf(&res->buf);
4739
4740 if (s->txn->status == -1)
4741 s->txn->status = reply->status;
4742 channel_htx_truncate(res, htx);
4743
4744 if (http_reply_to_htx(s, htx, reply) == -1)
4745 goto fail;
4746
4747 htx_to_buf(htx, &s->res.buf);
4748 if (!http_forward_proxy_resp(s, 1))
4749 goto fail;
4750 return 0;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004751
4752 fail:
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004753 channel_htx_truncate(res, htx);
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004754 if (!(s->flags & SF_ERR_MASK))
4755 s->flags |= SF_ERR_PRXCOND;
Christopher Faulet97e466c2020-05-15 15:12:47 +02004756 return -1;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004757}
4758
Christopher Faulet50264b42022-03-30 19:39:30 +02004759/* Return the error message corresponding to s->conn_err_type. It is assumed
Christopher Faulet304cc402019-07-15 15:46:28 +02004760 * that the server side is closed. Note that err_type is actually a
4761 * bitmask, where almost only aborts may be cumulated with other
4762 * values. We consider that aborted operations are more important
4763 * than timeouts or errors due to the fact that nobody else in the
4764 * logs might explain incomplete retries. All others should avoid
4765 * being cumulated. It should normally not be possible to have multiple
4766 * aborts at once, but just in case, the first one in sequence is reported.
4767 * Note that connection errors appearing on the second request of a keep-alive
4768 * connection are not reported since this allows the client to retry.
4769 */
Willy Tarreaub49672d2022-05-27 10:13:37 +02004770void http_return_srv_error(struct stream *s, struct stconn *sc)
Christopher Faulet304cc402019-07-15 15:46:28 +02004771{
Christopher Faulet50264b42022-03-30 19:39:30 +02004772 int err_type = s->conn_err_type;
Christopher Faulet304cc402019-07-15 15:46:28 +02004773
4774 /* set s->txn->status for http_error_message(s) */
Christopher Faulet50264b42022-03-30 19:39:30 +02004775 if (err_type & STRM_ET_QUEUE_ABRT) {
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004776 s->txn->status = -1;
Willy Tarreaub49672d2022-05-27 10:13:37 +02004777 http_server_error(s, sc, SF_ERR_CLICL, SF_FINST_Q, NULL);
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004778 }
Christopher Faulet50264b42022-03-30 19:39:30 +02004779 else if (err_type & STRM_ET_CONN_ABRT) {
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004780 s->txn->status = -1;
Willy Tarreaub49672d2022-05-27 10:13:37 +02004781 http_server_error(s, sc, SF_ERR_CLICL, SF_FINST_C, NULL);
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004782 }
Christopher Faulet50264b42022-03-30 19:39:30 +02004783 else if (err_type & STRM_ET_QUEUE_TO) {
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004784 s->txn->status = 503;
Willy Tarreaub49672d2022-05-27 10:13:37 +02004785 http_server_error(s, sc, SF_ERR_SRVTO, SF_FINST_Q,
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004786 http_error_message(s));
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004787 }
Christopher Faulet50264b42022-03-30 19:39:30 +02004788 else if (err_type & STRM_ET_QUEUE_ERR) {
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004789 s->txn->status = 503;
Willy Tarreaub49672d2022-05-27 10:13:37 +02004790 http_server_error(s, sc, SF_ERR_SRVCL, SF_FINST_Q,
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004791 http_error_message(s));
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004792 }
Christopher Faulet50264b42022-03-30 19:39:30 +02004793 else if (err_type & STRM_ET_CONN_TO) {
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004794 s->txn->status = 503;
Willy Tarreaub49672d2022-05-27 10:13:37 +02004795 http_server_error(s, sc, SF_ERR_SRVTO, SF_FINST_C,
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004796 (s->txn->flags & TX_NOT_FIRST) ? NULL :
4797 http_error_message(s));
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004798 }
Christopher Faulet50264b42022-03-30 19:39:30 +02004799 else if (err_type & STRM_ET_CONN_ERR) {
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004800 s->txn->status = 503;
Willy Tarreaub49672d2022-05-27 10:13:37 +02004801 http_server_error(s, sc, SF_ERR_SRVCL, SF_FINST_C,
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004802 (s->flags & SF_SRV_REUSED) ? NULL :
4803 http_error_message(s));
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004804 }
Christopher Faulet50264b42022-03-30 19:39:30 +02004805 else if (err_type & STRM_ET_CONN_RES) {
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004806 s->txn->status = 503;
Willy Tarreaub49672d2022-05-27 10:13:37 +02004807 http_server_error(s, sc, SF_ERR_RESOURCE, SF_FINST_C,
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004808 (s->txn->flags & TX_NOT_FIRST) ? NULL :
4809 http_error_message(s));
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004810 }
Christopher Faulet50264b42022-03-30 19:39:30 +02004811 else { /* STRM_ET_CONN_OTHER and others */
Christopher Faulet304cc402019-07-15 15:46:28 +02004812 s->txn->status = 500;
Willy Tarreaub49672d2022-05-27 10:13:37 +02004813 http_server_error(s, sc, SF_ERR_INTERNAL, SF_FINST_C,
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004814 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004815 }
4816}
4817
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004818
Christopher Faulet4a28a532019-03-01 11:19:40 +01004819/* Handle Expect: 100-continue for HTTP/1.1 messages if necessary. It returns 0
4820 * on success and -1 on error.
4821 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004822static int http_handle_expect_hdr(struct stream *s, struct htx *htx, struct http_msg *msg)
Christopher Faulet4a28a532019-03-01 11:19:40 +01004823{
4824 /* If we have HTTP/1.1 message with a body and Expect: 100-continue,
4825 * then we must send an HTTP/1.1 100 Continue intermediate response.
4826 */
4827 if (msg->msg_state == HTTP_MSG_BODY && (msg->flags & HTTP_MSGF_VER_11) &&
4828 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
4829 struct ist hdr = { .ptr = "Expect", .len = 6 };
4830 struct http_hdr_ctx ctx;
4831
4832 ctx.blk = NULL;
4833 /* Expect is allowed in 1.1, look for it */
4834 if (http_find_header(htx, hdr, &ctx, 0) &&
4835 unlikely(isteqi(ctx.value, ist2("100-continue", 12)))) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004836 if (http_reply_100_continue(s) == -1)
Christopher Faulet4a28a532019-03-01 11:19:40 +01004837 return -1;
4838 http_remove_header(htx, &ctx);
4839 }
4840 }
4841 return 0;
4842}
4843
Christopher Faulet23a3c792018-11-28 10:01:23 +01004844/* Send a 100-Continue response to the client. It returns 0 on success and -1
4845 * on error. The response channel is updated accordingly.
4846 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004847static int http_reply_100_continue(struct stream *s)
Christopher Faulet23a3c792018-11-28 10:01:23 +01004848{
4849 struct channel *res = &s->res;
4850 struct htx *htx = htx_from_buf(&res->buf);
4851 struct htx_sl *sl;
4852 unsigned int flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|
4853 HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
Christopher Faulet23a3c792018-11-28 10:01:23 +01004854
4855 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
4856 ist("HTTP/1.1"), ist("100"), ist("Continue"));
4857 if (!sl)
4858 goto fail;
4859 sl->info.res.status = 100;
4860
Christopher Faulet1d5ec092019-06-26 14:23:54 +02004861 if (!htx_add_endof(htx, HTX_BLK_EOH))
Christopher Faulet23a3c792018-11-28 10:01:23 +01004862 goto fail;
4863
Christopher Fauleta72a7e42020-01-28 09:28:11 +01004864 if (!http_forward_proxy_resp(s, 0))
4865 goto fail;
Christopher Faulet23a3c792018-11-28 10:01:23 +01004866 return 0;
4867
4868 fail:
4869 /* If an error occurred, remove the incomplete HTTP response from the
4870 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004871 channel_htx_truncate(res, htx);
Christopher Faulet23a3c792018-11-28 10:01:23 +01004872 return -1;
4873}
4874
Christopher Faulet12c51e22018-11-28 15:59:42 +01004875
Christopher Faulet0f226952018-10-22 09:29:56 +02004876/*
4877 * Capture headers from message <htx> according to header list <cap_hdr>, and
4878 * fill the <cap> pointers appropriately.
4879 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004880static void http_capture_headers(struct htx *htx, char **cap, struct cap_hdr *cap_hdr)
Christopher Faulet0f226952018-10-22 09:29:56 +02004881{
4882 struct cap_hdr *h;
4883 int32_t pos;
4884
Christopher Fauleta3f15502019-05-13 15:27:23 +02004885 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet0f226952018-10-22 09:29:56 +02004886 struct htx_blk *blk = htx_get_blk(htx, pos);
4887 enum htx_blk_type type = htx_get_blk_type(blk);
4888 struct ist n, v;
4889
4890 if (type == HTX_BLK_EOH)
4891 break;
4892 if (type != HTX_BLK_HDR)
4893 continue;
4894
4895 n = htx_get_blk_name(htx, blk);
4896
4897 for (h = cap_hdr; h; h = h->next) {
4898 if (h->namelen && (h->namelen == n.len) &&
4899 (strncasecmp(n.ptr, h->name, h->namelen) == 0)) {
4900 if (cap[h->index] == NULL)
4901 cap[h->index] =
4902 pool_alloc(h->pool);
4903
4904 if (cap[h->index] == NULL) {
4905 ha_alert("HTTP capture : out of memory.\n");
4906 break;
4907 }
4908
4909 v = htx_get_blk_value(htx, blk);
Tim Duesterhus2471f5c2021-11-08 09:05:01 +01004910 v = isttrim(v, h->len);
Christopher Faulet0f226952018-10-22 09:29:56 +02004911
4912 memcpy(cap[h->index], v.ptr, v.len);
4913 cap[h->index][v.len]=0;
4914 }
4915 }
4916 }
4917}
4918
Christopher Faulet0b6bdc52018-10-24 11:05:36 +02004919/* Delete a value in a header between delimiters <from> and <next>. The header
4920 * itself is delimited by <start> and <end> pointers. The number of characters
4921 * displaced is returned, and the pointer to the first delimiter is updated if
4922 * required. The function tries as much as possible to respect the following
4923 * principles :
4924 * - replace <from> delimiter by the <next> one unless <from> points to <start>,
4925 * in which case <next> is simply removed
4926 * - set exactly one space character after the new first delimiter, unless there
4927 * are not enough characters in the block being moved to do so.
4928 * - remove unneeded spaces before the previous delimiter and after the new
4929 * one.
4930 *
4931 * It is the caller's responsibility to ensure that :
4932 * - <from> points to a valid delimiter or <start> ;
4933 * - <next> points to a valid delimiter or <end> ;
4934 * - there are non-space chars before <from>.
4935 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004936static int http_del_hdr_value(char *start, char *end, char **from, char *next)
Christopher Faulet0b6bdc52018-10-24 11:05:36 +02004937{
4938 char *prev = *from;
4939
4940 if (prev == start) {
4941 /* We're removing the first value. eat the semicolon, if <next>
4942 * is lower than <end> */
4943 if (next < end)
4944 next++;
4945
4946 while (next < end && HTTP_IS_SPHT(*next))
4947 next++;
4948 }
4949 else {
4950 /* Remove useless spaces before the old delimiter. */
4951 while (HTTP_IS_SPHT(*(prev-1)))
4952 prev--;
4953 *from = prev;
4954
4955 /* copy the delimiter and if possible a space if we're
4956 * not at the end of the line.
4957 */
4958 if (next < end) {
4959 *prev++ = *next++;
4960 if (prev + 1 < next)
4961 *prev++ = ' ';
4962 while (next < end && HTTP_IS_SPHT(*next))
4963 next++;
4964 }
4965 }
4966 memmove(prev, next, end - next);
4967 return (prev - next);
4968}
4969
Christopher Faulet0f226952018-10-22 09:29:56 +02004970
4971/* Formats the start line of the request (without CRLF) and puts it in <str> and
Joseph Herlantc42c0e92018-11-25 10:43:27 -08004972 * return the written length. The line can be truncated if it exceeds <len>.
Christopher Faulet0f226952018-10-22 09:29:56 +02004973 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004974static size_t http_fmt_req_line(const struct htx_sl *sl, char *str, size_t len)
Christopher Faulet0f226952018-10-22 09:29:56 +02004975{
4976 struct ist dst = ist2(str, 0);
4977
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004978 if (istcat(&dst, htx_sl_req_meth(sl), len) == -1)
Christopher Faulet0f226952018-10-22 09:29:56 +02004979 goto end;
4980 if (dst.len + 1 > len)
4981 goto end;
4982 dst.ptr[dst.len++] = ' ';
4983
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004984 if (istcat(&dst, htx_sl_req_uri(sl), len) == -1)
Christopher Faulet0f226952018-10-22 09:29:56 +02004985 goto end;
4986 if (dst.len + 1 > len)
4987 goto end;
4988 dst.ptr[dst.len++] = ' ';
4989
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004990 istcat(&dst, htx_sl_req_vsn(sl), len);
Christopher Faulet0f226952018-10-22 09:29:56 +02004991 end:
4992 return dst.len;
4993}
4994
4995/*
4996 * Print a debug line with a start line.
4997 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004998static void http_debug_stline(const char *dir, struct stream *s, const struct htx_sl *sl)
Christopher Faulet0f226952018-10-22 09:29:56 +02004999{
5000 struct session *sess = strm_sess(s);
5001 int max;
5002
5003 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
5004 dir,
Willy Tarreau88bc8002021-12-06 07:01:02 +00005005 objt_conn(sess->origin) ? (unsigned short)__objt_conn(sess->origin)->handle.fd : -1,
Willy Tarreaufd9417b2022-05-18 16:23:22 +02005006 sc_conn(s->scb) ? (unsigned short)(__sc_conn(s->scb))->handle.fd : -1);
Christopher Faulet0f226952018-10-22 09:29:56 +02005007
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005008 max = HTX_SL_P1_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02005009 UBOUND(max, trash.size - trash.data - 3);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005010 chunk_memcat(&trash, HTX_SL_P1_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02005011 trash.area[trash.data++] = ' ';
5012
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005013 max = HTX_SL_P2_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02005014 UBOUND(max, trash.size - trash.data - 2);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005015 chunk_memcat(&trash, HTX_SL_P2_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02005016 trash.area[trash.data++] = ' ';
5017
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005018 max = HTX_SL_P3_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02005019 UBOUND(max, trash.size - trash.data - 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005020 chunk_memcat(&trash, HTX_SL_P3_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02005021 trash.area[trash.data++] = '\n';
5022
Willy Tarreau2e8ab6b2020-03-14 11:03:20 +01005023 DISGUISE(write(1, trash.area, trash.data));
Christopher Faulet0f226952018-10-22 09:29:56 +02005024}
5025
5026/*
5027 * Print a debug line with a header.
5028 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005029static 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 +02005030{
5031 struct session *sess = strm_sess(s);
5032 int max;
5033
5034 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
5035 dir,
Willy Tarreau88bc8002021-12-06 07:01:02 +00005036 objt_conn(sess->origin) ? (unsigned short)__objt_conn(sess->origin)->handle.fd : -1,
Willy Tarreaufd9417b2022-05-18 16:23:22 +02005037 sc_conn(s->scb) ? (unsigned short)(__sc_conn(s->scb))->handle.fd : -1);
Christopher Faulet0f226952018-10-22 09:29:56 +02005038
5039 max = n.len;
5040 UBOUND(max, trash.size - trash.data - 3);
5041 chunk_memcat(&trash, n.ptr, max);
5042 trash.area[trash.data++] = ':';
5043 trash.area[trash.data++] = ' ';
5044
5045 max = v.len;
5046 UBOUND(max, trash.size - trash.data - 1);
5047 chunk_memcat(&trash, v.ptr, max);
5048 trash.area[trash.data++] = '\n';
5049
Willy Tarreau2e8ab6b2020-03-14 11:03:20 +01005050 DISGUISE(write(1, trash.area, trash.data));
Christopher Faulet0f226952018-10-22 09:29:56 +02005051}
5052
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005053void http_txn_reset_req(struct http_txn *txn)
5054{
Christopher Faulet1aea50e2020-01-17 16:03:53 +01005055 txn->req.flags = 0;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005056 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
5057}
5058
5059void http_txn_reset_res(struct http_txn *txn)
5060{
Christopher Faulet1aea50e2020-01-17 16:03:53 +01005061 txn->rsp.flags = 0;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005062 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
5063}
5064
5065/*
Christopher Faulet75f619a2021-03-08 19:12:58 +01005066 * Create and initialize a new HTTP transaction for stream <s>. This should be
5067 * used before processing any new request. It returns the transaction or NLULL
5068 * on error.
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005069 */
Christopher Faulet75f619a2021-03-08 19:12:58 +01005070struct http_txn *http_create_txn(struct stream *s)
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005071{
Christopher Faulet75f619a2021-03-08 19:12:58 +01005072 struct http_txn *txn;
Willy Tarreaub49672d2022-05-27 10:13:37 +02005073 struct stconn *sc = s->scf;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005074
Christopher Faulet75f619a2021-03-08 19:12:58 +01005075 txn = pool_alloc(pool_head_http_txn);
5076 if (!txn)
5077 return NULL;
5078 s->txn = txn;
5079
Christopher Faulet5eb67f52022-06-22 17:12:05 +02005080 txn->meth = HTTP_METH_OTHER;
Willy Tarreaub49672d2022-05-27 10:13:37 +02005081 txn->flags = ((sc && sc_ep_test(sc, SE_FL_NOT_FIRST)) ? TX_NOT_FIRST : 0);
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005082 txn->status = -1;
Christopher Faulet5cb513a2020-05-13 17:56:56 +02005083 txn->http_reply = NULL;
Christopher Faulete05bf9e2022-03-29 15:23:40 +02005084 txn->l7_buffer = BUF_NULL;
Willy Tarreau8b507582020-02-25 09:35:07 +01005085 write_u32(txn->cache_hash, 0);
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005086
5087 txn->cookie_first_date = 0;
5088 txn->cookie_last_date = 0;
5089
5090 txn->srv_cookie = NULL;
5091 txn->cli_cookie = NULL;
5092 txn->uri = NULL;
5093
5094 http_txn_reset_req(txn);
5095 http_txn_reset_res(txn);
5096
5097 txn->req.chn = &s->req;
5098 txn->rsp.chn = &s->res;
5099
5100 txn->auth.method = HTTP_AUTH_UNKNOWN;
5101
Aurelien DARRAGON5ad2b642022-11-18 09:17:29 +01005102 /* here we don't want to re-initialize s->vars_txn and s->vars_reqres
5103 * variable lists, because they were already initialized upon stream
5104 * creation in stream_new(), and thus may already contain some variables
5105 */
Christopher Faulet75f619a2021-03-08 19:12:58 +01005106
5107 return txn;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005108}
5109
5110/* to be used at the end of a transaction */
Christopher Faulet75f619a2021-03-08 19:12:58 +01005111void http_destroy_txn(struct stream *s)
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005112{
5113 struct http_txn *txn = s->txn;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005114
5115 /* these ones will have been dynamically allocated */
5116 pool_free(pool_head_requri, txn->uri);
5117 pool_free(pool_head_capture, txn->cli_cookie);
5118 pool_free(pool_head_capture, txn->srv_cookie);
Tim Duesterhusa17e6622020-03-05 20:19:02 +01005119 pool_free(pool_head_uniqueid, s->unique_id.ptr);
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005120
Tim Duesterhusa17e6622020-03-05 20:19:02 +01005121 s->unique_id = IST_NULL;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005122 txn->uri = NULL;
5123 txn->srv_cookie = NULL;
5124 txn->cli_cookie = NULL;
5125
Christopher Faulet59399252019-11-07 14:27:52 +01005126 if (!LIST_ISEMPTY(&s->vars_txn.head))
5127 vars_prune(&s->vars_txn, s->sess, s);
5128 if (!LIST_ISEMPTY(&s->vars_reqres.head))
5129 vars_prune(&s->vars_reqres, s->sess, s);
Christopher Faulet75f619a2021-03-08 19:12:58 +01005130
Christopher Faulete05bf9e2022-03-29 15:23:40 +02005131 b_free(&txn->l7_buffer);
5132
Christopher Faulet75f619a2021-03-08 19:12:58 +01005133 pool_free(pool_head_http_txn, txn);
5134 s->txn = NULL;
Christopher Faulet59399252019-11-07 14:27:52 +01005135}
5136
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005137
Christopher Faulet71236de2023-01-13 09:06:38 +01005138void http_set_term_flags(struct stream *s)
5139{
5140 if (!(s->flags & SF_ERR_MASK))
5141 s->flags |= SF_ERR_PRXCOND;
5142
5143 if (!(s->flags & SF_FINST_MASK)) {
5144 if (s->scb->state == SC_ST_INI) {
5145 /* Before any connection attempt on the server side, we
5146 * are still in the request analysis. Just take case to
5147 * detect tarpit error
5148 */
5149 if (s->req.analysers & AN_REQ_HTTP_TARPIT)
5150 s->flags |= SF_FINST_T;
5151 else
5152 s->flags |= SF_FINST_R;
5153 }
5154 else if (s->scb->state == SC_ST_QUE)
5155 s->flags |= SF_FINST_Q;
5156 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)) {
5157 if (unlikely(objt_applet(s->target))) {
5158 s->flags |= SF_FINST_R;
5159 }
5160 else
5161 s->flags |= SF_FINST_C;
5162 }
5163 else {
5164 if (s->txn->rsp.msg_state < HTTP_MSG_DATA) {
5165 /* We are still processing the response headers */
5166 s->flags |= SF_FINST_H;
5167 }
5168 // (res >= done) & (res->flags & shutw)
5169 else if (s->txn->rsp.msg_state >= HTTP_MSG_DONE &&
5170 (s->flags & (SF_ERR_CLITO|SF_ERR_CLICL))) {
5171 /* A client error was reported and we are
5172 * transmitting the last block of data
5173 */
5174 s->flags |= SF_FINST_L;
5175 }
5176 else {
5177 /* Otherwise we are in DATA phase on both sides */
5178 s->flags |= SF_FINST_D;
5179 }
5180 }
5181 }
5182}
5183
5184
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005185DECLARE_POOL(pool_head_http_txn, "http_txn", sizeof(struct http_txn));
Christopher Faulet0f226952018-10-22 09:29:56 +02005186
Christopher Fauletf4eb75d2018-10-11 15:55:07 +02005187/*
5188 * Local variables:
5189 * c-indent-level: 8
5190 * c-basic-offset: 8
5191 * End:
5192 */