blob: 97594c6a284c1d4c4dd6ffb4c210a5be2832f025 [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>
Aurelien DARRAGONb2bb9252022-12-28 15:37:57 +010029#include <haproxy/http_ext.h>
Willy Tarreau16f958c2020-06-03 08:44:35 +020030#include <haproxy/htx.h>
Willy Tarreauaeed4a82020-06-04 22:01:04 +020031#include <haproxy/log.h>
Willy Tarreau6131d6a2020-06-02 16:48:09 +020032#include <haproxy/net_helper.h>
Willy Tarreaua264d962020-06-04 22:29:18 +020033#include <haproxy/proxy.h>
Willy Tarreau7cd8b6e2020-06-02 17:32:26 +020034#include <haproxy/regex.h>
Willy Tarreau5edca2f2022-05-27 09:25:10 +020035#include <haproxy/sc_strm.h>
Willy Tarreau1e56f922020-06-04 23:20:13 +020036#include <haproxy/server-t.h>
Willy Tarreau2eec9b52020-06-04 19:58:55 +020037#include <haproxy/stats.h>
Willy Tarreaucb086c62022-05-27 09:47:12 +020038#include <haproxy/stconn.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020039#include <haproxy/stream.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020040#include <haproxy/trace.h>
Willy Tarreau8c42b8a2020-06-04 19:27:34 +020041#include <haproxy/uri_auth-t.h>
Willy Tarreaua1718922020-06-04 16:25:31 +020042#include <haproxy/vars.h>
Christopher Faulete0768eb2018-10-03 16:38:02 +020043
Christopher Faulete0768eb2018-10-03 16:38:02 +020044
Christopher Fauleteea8fc72019-11-05 16:18:10 +010045#define TRACE_SOURCE &trace_strm
46
Christopher Faulet377c5a52018-10-24 21:21:30 +020047extern const char *stat_status_codes[];
Christopher Fauletf2824e62018-10-01 12:12:37 +020048
Willy Tarreauff882702021-04-10 17:23:00 +020049struct pool_head *pool_head_requri __read_mostly = NULL;
50struct pool_head *pool_head_capture __read_mostly = NULL;
Christopher Fauleta8a46e22019-07-16 14:53:09 +020051
52
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020053static void http_end_request(struct stream *s);
54static void http_end_response(struct stream *s);
Christopher Fauletf2824e62018-10-01 12:12:37 +020055
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020056static void http_capture_headers(struct htx *htx, char **cap, struct cap_hdr *cap_hdr);
57static int http_del_hdr_value(char *start, char *end, char **from, char *next);
58static size_t http_fmt_req_line(const struct htx_sl *sl, char *str, size_t len);
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020059static void http_debug_stline(const char *dir, struct stream *s, const struct htx_sl *sl);
60static 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 +020061
Christopher Fauletd4150ad2021-10-13 15:35:55 +020062static enum rule_result http_req_get_intercept_rule(struct proxy *px, struct list *def_rules, struct list *rules, struct stream *s);
63static 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 +020064static enum rule_result http_req_restrict_header_names(struct stream *s, struct htx *htx, struct proxy *px);
Christopher Faulet3e964192018-10-24 11:39:23 +020065
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020066static void http_manage_client_side_cookies(struct stream *s, struct channel *req);
67static void http_manage_server_side_cookies(struct stream *s, struct channel *res);
Christopher Fauletfcda7c62018-10-24 11:56:22 +020068
Aurelien DARRAGONf6286142023-12-05 17:54:20 +010069static int http_stats_check_uri(struct stream *s, struct http_txn *txn, struct proxy *px);
70static int http_handle_stats(struct stream *s, struct channel *req, struct proxy *px);
Christopher Faulet377c5a52018-10-24 21:21:30 +020071
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020072static int http_handle_expect_hdr(struct stream *s, struct htx *htx, struct http_msg *msg);
73static int http_reply_100_continue(struct stream *s);
Christopher Faulet23a3c792018-11-28 10:01:23 +010074
Christopher Faulete0768eb2018-10-03 16:38:02 +020075/* This stream analyser waits for a complete HTTP request. It returns 1 if the
76 * processing can continue on next analysers, or zero if it either needs more
77 * data or wants to immediately abort the request (eg: timeout, error, ...). It
78 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req.analysers
79 * when it has nothing left to do, and may remove any analyser when it wants to
80 * abort.
81 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020082int http_wait_for_request(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +020083{
Christopher Faulet9768c262018-10-22 09:34:31 +020084
Christopher Faulete0768eb2018-10-03 16:38:02 +020085 /*
Christopher Faulet9768c262018-10-22 09:34:31 +020086 * We will analyze a complete HTTP request to check the its syntax.
Christopher Faulete0768eb2018-10-03 16:38:02 +020087 *
Christopher Faulet9768c262018-10-22 09:34:31 +020088 * Once the start line and all headers are received, we may perform a
89 * capture of the error (if any), and we will set a few fields. We also
90 * check for monitor-uri, logging and finally headers capture.
Christopher Faulete0768eb2018-10-03 16:38:02 +020091 */
Christopher Faulete0768eb2018-10-03 16:38:02 +020092 struct session *sess = s->sess;
93 struct http_txn *txn = s->txn;
94 struct http_msg *msg = &txn->req;
Christopher Faulet9768c262018-10-22 09:34:31 +020095 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +010096 struct htx_sl *sl;
Frédéric Lécaille9969adb2023-01-18 11:52:21 +010097 char http_ver;
98 int len;
Christopher Faulete0768eb2018-10-03 16:38:02 +020099
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100100 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200101
Christopher Fauletda46a0d2021-01-21 17:32:58 +0100102 if (unlikely(!IS_HTX_STRM(s))) {
103 /* It is only possible when a TCP stream is upgrade to HTTP.
104 * There is a transition period during which there is no
105 * data. The stream is still in raw mode and SF_IGNORE flag is
106 * still set. When this happens, the new mux is responsible to
Ilya Shipitsinacf84592021-02-06 22:29:08 +0500107 * handle all errors. Thus we may leave immediately.
Christopher Fauletda46a0d2021-01-21 17:32:58 +0100108 */
109 BUG_ON(!(s->flags & SF_IGNORE) || !c_empty(&s->req));
Christopher Faulet9768c262018-10-22 09:34:31 +0200110
Christopher Faulet97b3a612021-03-15 17:10:12 +0100111 /* Don't connect for now */
112 channel_dont_connect(req);
113
Christopher Faulet0c370ee2023-04-13 16:05:13 +0200114 /* An abort at this stage means we are performing a "destructive"
Christopher Faulet97b3a612021-03-15 17:10:12 +0100115 * HTTP upgrade (TCP>H2). In this case, we can leave.
116 */
Christopher Fauletca5309a2023-04-17 16:17:32 +0200117 if (s->scf->flags & (SC_FL_ABRT_DONE|SC_FL_EOS)) {
Christopher Faulet97b3a612021-03-15 17:10:12 +0100118 s->logs.logwait = 0;
119 s->logs.level = 0;
Christopher Faulet7eb837d2023-04-13 15:22:29 +0200120 stream_abort(s);
Christopher Faulet97b3a612021-03-15 17:10:12 +0100121 req->analysers &= AN_REQ_FLT_END;
122 req->analyse_exp = TICK_ETERNITY;
123 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA, s);
124 return 1;
125 }
Christopher Fauletda46a0d2021-01-21 17:32:58 +0100126 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA, s);
127 return 0;
128 }
129
130 htx = htxbuf(&req->buf);
Frédéric Lécaille9969adb2023-01-18 11:52:21 +0100131 sl = http_get_stline(htx);
132 len = HTX_SL_REQ_VLEN(sl);
133 if (len < 6) {
134 http_ver = 0;
135 }
136 else {
137 char *ptr;
138
139 ptr = HTX_SL_REQ_VPTR(sl);
140 http_ver = ptr[5] - '0';
141 }
Christopher Faulet8bebd2f2020-10-06 17:54:56 +0200142
Willy Tarreau4236f032019-03-05 10:43:32 +0100143 /* Parsing errors are caught here */
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200144 if (htx->flags & (HTX_FL_PARSING_ERROR|HTX_FL_PROCESSING_ERROR)) {
Willy Tarreau4236f032019-03-05 10:43:32 +0100145 stream_inc_http_req_ctr(s);
Frédéric Lécaille9969adb2023-01-18 11:52:21 +0100146 proxy_inc_fe_req_ctr(sess->listener, sess->fe, http_ver);
Christopher Fauletbf7175f2021-02-10 14:58:01 +0100147 if (htx->flags & HTX_FL_PARSING_ERROR) {
148 stream_inc_http_err_ctr(s);
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200149 goto return_bad_req;
Christopher Fauletbf7175f2021-02-10 14:58:01 +0100150 }
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200151 else
152 goto return_int_err;
Willy Tarreau4236f032019-03-05 10:43:32 +0100153 }
154
Christopher Faulete0768eb2018-10-03 16:38:02 +0200155 /* we're speaking HTTP here, so let's speak HTTP to the client */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200156 s->srv_error = http_return_srv_error;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200157
Christopher Faulet9768c262018-10-22 09:34:31 +0200158 msg->msg_state = HTTP_MSG_BODY;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200159 stream_inc_http_req_ctr(s);
Frédéric Lécaille9969adb2023-01-18 11:52:21 +0100160 proxy_inc_fe_req_ctr(sess->listener, sess->fe, http_ver); /* one more valid request for this FE */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200161
Christopher Faulet9768c262018-10-22 09:34:31 +0200162 /* kill the pending keep-alive timeout */
Christopher Faulet9768c262018-10-22 09:34:31 +0200163 req->analyse_exp = TICK_ETERNITY;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200164
Christopher Faulet29f17582019-05-23 11:03:26 +0200165 BUG_ON(htx_get_first_type(htx) != HTX_BLK_REQ_SL);
Christopher Faulet03599112018-11-27 11:21:21 +0100166
Christopher Faulet9768c262018-10-22 09:34:31 +0200167 /* 0: we might have to print this header in debug mode */
168 if (unlikely((global.mode & MODE_DEBUG) &&
169 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
170 int32_t pos;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200171
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200172 http_debug_stline("clireq", s, sl);
Christopher Faulet9768c262018-10-22 09:34:31 +0200173
Christopher Fauleta3f15502019-05-13 15:27:23 +0200174 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet9768c262018-10-22 09:34:31 +0200175 struct htx_blk *blk = htx_get_blk(htx, pos);
176 enum htx_blk_type type = htx_get_blk_type(blk);
177
178 if (type == HTX_BLK_EOH)
179 break;
180 if (type != HTX_BLK_HDR)
181 continue;
182
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200183 http_debug_hdr("clihdr", s,
184 htx_get_blk_name(htx, blk),
185 htx_get_blk_value(htx, blk));
Christopher Faulet9768c262018-10-22 09:34:31 +0200186 }
187 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200188
189 /*
Christopher Faulet03599112018-11-27 11:21:21 +0100190 * 1: identify the method and the version. Also set HTTP flags
Christopher Faulete0768eb2018-10-03 16:38:02 +0200191 */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100192 txn->meth = sl->info.req.meth;
Christopher Faulet03599112018-11-27 11:21:21 +0100193 if (sl->flags & HTX_SL_F_VER_11)
Christopher Faulet9768c262018-10-22 09:34:31 +0200194 msg->flags |= HTTP_MSGF_VER_11;
Christopher Faulet03599112018-11-27 11:21:21 +0100195 msg->flags |= HTTP_MSGF_XFER_LEN;
Christopher Faulet2a408542020-11-20 14:22:37 +0100196 if (sl->flags & HTX_SL_F_CLEN)
197 msg->flags |= HTTP_MSGF_CNT_LEN;
198 else if (sl->flags & HTX_SL_F_CHNK)
199 msg->flags |= HTTP_MSGF_TE_CHNK;
Christopher Fauletb2db4fa2018-11-27 16:51:09 +0100200 if (sl->flags & HTX_SL_F_BODYLESS)
201 msg->flags |= HTTP_MSGF_BODYLESS;
Christopher Faulet576c3582021-01-08 15:53:01 +0100202 if (sl->flags & HTX_SL_F_CONN_UPG)
203 msg->flags |= HTTP_MSGF_CONN_UPG;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200204
205 /* we can make use of server redirect on GET and HEAD */
206 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
207 s->flags |= SF_REDIRECTABLE;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100208 else if (txn->meth == HTTP_METH_OTHER && isteqi(htx_sl_req_meth(sl), ist("PRI"))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200209 /* PRI is reserved for the HTTP/2 preface */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200210 goto return_bad_req;
211 }
212
213 /*
Christopher Faulet6072beb2020-02-18 15:34:58 +0100214 * 2: check if the URI matches the monitor_uri. We have to do this for
215 * every request which gets in, because the monitor-uri is defined by
216 * the frontend. If the monitor-uri starts with a '/', the matching is
217 * done against the request's path. Otherwise, the request's uri is
218 * used. It is a workaround to let HTTP/2 health-checks work as
219 * expected.
Christopher Faulete0768eb2018-10-03 16:38:02 +0200220 */
Tim Duesterhus4b1fcaa2022-03-05 00:52:40 +0100221 if (unlikely(isttest(sess->fe->monitor_uri))) {
222 const struct ist monitor_uri = sess->fe->monitor_uri;
Amaury Denoyellec453f952021-07-06 11:40:12 +0200223 struct http_uri_parser parser = http_uri_parser_init(htx_sl_req_uri(sl));
Christopher Faulete0768eb2018-10-03 16:38:02 +0200224
Amaury Denoyelle5a9bd372021-07-06 11:23:10 +0200225 if ((istptr(monitor_uri)[0] == '/' &&
Amaury Denoyellec453f952021-07-06 11:40:12 +0200226 isteq(http_parse_path(&parser), monitor_uri)) ||
Amaury Denoyelle5a9bd372021-07-06 11:23:10 +0200227 isteq(htx_sl_req_uri(sl), monitor_uri)) {
228 /*
229 * We have found the monitor URI
230 */
231 struct acl_cond *cond;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200232
Amaury Denoyelle5a9bd372021-07-06 11:23:10 +0200233 s->flags |= SF_MONITOR;
234 _HA_ATOMIC_INC(&sess->fe->fe_counters.intercepted_req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200235
Amaury Denoyelle5a9bd372021-07-06 11:23:10 +0200236 /* Check if we want to fail this monitor request or not */
237 list_for_each_entry(cond, &sess->fe->mon_fail_cond, list) {
238 int ret = acl_exec_cond(cond, sess->fe, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200239
Amaury Denoyelle5a9bd372021-07-06 11:23:10 +0200240 ret = acl_pass(ret);
241 if (cond->pol == ACL_COND_UNLESS)
242 ret = !ret;
243
244 if (ret) {
245 /* we fail this request, let's return 503 service unavail */
246 txn->status = 503;
247 if (!(s->flags & SF_ERR_MASK))
248 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
249 goto return_prx_cond;
250 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200251 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200252
Amaury Denoyelle5a9bd372021-07-06 11:23:10 +0200253 /* nothing to fail, let's reply normally */
254 txn->status = 200;
255 if (!(s->flags & SF_ERR_MASK))
256 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
257 goto return_prx_cond;
258 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200259 }
260
261 /*
262 * 3: Maybe we have to copy the original REQURI for the logs ?
263 * Note: we cannot log anymore if the request has been
264 * classified as invalid.
265 */
266 if (unlikely(s->logs.logwait & LW_REQ)) {
267 /* we have a complete HTTP request that we must log */
268 if ((txn->uri = pool_alloc(pool_head_requri)) != NULL) {
Christopher Faulet9768c262018-10-22 09:34:31 +0200269 size_t len;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200270
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200271 len = http_fmt_req_line(sl, txn->uri, global.tune.requri_len - 1);
Christopher Faulet9768c262018-10-22 09:34:31 +0200272 txn->uri[len] = 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200273
274 if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT)))
275 s->do_log(s);
276 } else {
277 ha_alert("HTTP logging : out of memory.\n");
278 }
279 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200280
Christopher Faulete0768eb2018-10-03 16:38:02 +0200281 /* if the frontend has "option http-use-proxy-header", we'll check if
282 * we have what looks like a proxied connection instead of a connection,
283 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
284 * Note that this is *not* RFC-compliant, however browsers and proxies
285 * happen to do that despite being non-standard :-(
286 * We consider that a request not beginning with either '/' or '*' is
287 * a proxied connection, which covers both "scheme://location" and
288 * CONNECT ip:port.
289 */
290 if ((sess->fe->options2 & PR_O2_USE_PXHDR) &&
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100291 *HTX_SL_REQ_UPTR(sl) != '/' && *HTX_SL_REQ_UPTR(sl) != '*')
Christopher Faulete0768eb2018-10-03 16:38:02 +0200292 txn->flags |= TX_USE_PX_CONN;
293
Christopher Faulete0768eb2018-10-03 16:38:02 +0200294 /* 5: we may need to capture headers */
295 if (unlikely((s->logs.logwait & LW_REQHDR) && s->req_cap))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200296 http_capture_headers(htx, s->req_cap, sess->fe->req_cap);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200297
Christopher Faulete0768eb2018-10-03 16:38:02 +0200298 /* we may have to wait for the request's body */
Christopher Faulet9768c262018-10-22 09:34:31 +0200299 if (s->be->options & PR_O_WREQ_BODY)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200300 req->analysers |= AN_REQ_HTTP_BODY;
301
302 /*
303 * RFC7234#4:
304 * A cache MUST write through requests with methods
305 * that are unsafe (Section 4.2.1 of [RFC7231]) to
306 * the origin server; i.e., a cache is not allowed
307 * to generate a reply to such a request before
308 * having forwarded the request and having received
309 * a corresponding response.
310 *
311 * RFC7231#4.2.1:
312 * Of the request methods defined by this
313 * specification, the GET, HEAD, OPTIONS, and TRACE
314 * methods are defined to be safe.
315 */
316 if (likely(txn->meth == HTTP_METH_GET ||
317 txn->meth == HTTP_METH_HEAD ||
318 txn->meth == HTTP_METH_OPTIONS ||
319 txn->meth == HTTP_METH_TRACE))
320 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
321
322 /* end of job, return OK */
323 req->analysers &= ~an_bit;
324 req->analyse_exp = TICK_ETERNITY;
Christopher Faulet9768c262018-10-22 09:34:31 +0200325
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100326 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200327 return 1;
328
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200329 return_int_err:
330 txn->status = 500;
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200331 if (!(s->flags & SF_ERR_MASK))
332 s->flags |= SF_ERR_INTERNAL;
Willy Tarreau4781b152021-04-06 13:53:36 +0200333 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
William Lallemand36119de2021-03-08 15:26:48 +0100334 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200335 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200336 goto return_prx_cond;
337
Christopher Faulete0768eb2018-10-03 16:38:02 +0200338 return_bad_req:
Christopher Faulet9768c262018-10-22 09:34:31 +0200339 txn->status = 400;
Willy Tarreau4781b152021-04-06 13:53:36 +0200340 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req);
William Lallemand36119de2021-03-08 15:26:48 +0100341 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200342 _HA_ATOMIC_INC(&sess->listener->counters->failed_req);
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200343 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200344
345 return_prx_cond:
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200346 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet4a66c942023-01-13 09:43:21 +0100347 http_set_term_flags(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200348
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100349 DBG_TRACE_DEVEL("leaving on error",
350 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200351 return 0;
352}
353
354
355/* This stream analyser runs all HTTP request processing which is common to
356 * frontends and backends, which means blocking ACLs, filters, connection-close,
357 * reqadd, stats and redirects. This is performed for the designated proxy.
358 * It returns 1 if the processing can continue on next analysers, or zero if it
359 * either needs more data or wants to immediately abort the request (eg: deny,
360 * error, ...).
361 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200362int http_process_req_common(struct stream *s, struct channel *req, int an_bit, struct proxy *px)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200363{
Christopher Fauletd4150ad2021-10-13 15:35:55 +0200364 struct list *def_rules, *rules;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200365 struct session *sess = s->sess;
366 struct http_txn *txn = s->txn;
367 struct http_msg *msg = &txn->req;
Christopher Fauletff2759f2018-10-24 11:13:16 +0200368 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200369 struct redirect_rule *rule;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200370 enum rule_result verdict;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200371 struct connection *conn = objt_conn(sess->origin);
372
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100373 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200374
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100375 htx = htxbuf(&req->buf);
Christopher Fauletff2759f2018-10-24 11:13:16 +0200376
Christopher Faulet1907ccc2019-04-29 13:12:02 +0200377 /* just in case we have some per-backend tracking. Only called the first
378 * execution of the analyser. */
Christopher Fauletd4150ad2021-10-13 15:35:55 +0200379 if (!s->current_rule && !s->current_rule_list)
Christopher Faulet1907ccc2019-04-29 13:12:02 +0200380 stream_inc_be_http_req_ctr(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200381
Christopher Fauletd4150ad2021-10-13 15:35:55 +0200382 def_rules = ((px->defpx && (an_bit == AN_REQ_HTTP_PROCESS_FE || px != sess->fe)) ? &px->defpx->http_req_rules : NULL);
383 rules = &px->http_req_rules;
384
Christopher Faulete0768eb2018-10-03 16:38:02 +0200385 /* evaluate http-request rules */
Christopher Fauletd4150ad2021-10-13 15:35:55 +0200386 if ((def_rules && !LIST_ISEMPTY(def_rules)) || !LIST_ISEMPTY(rules)) {
387 verdict = http_req_get_intercept_rule(px, def_rules, rules, s);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200388
389 switch (verdict) {
390 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
391 goto return_prx_yield;
392
393 case HTTP_RULE_RES_CONT:
394 case HTTP_RULE_RES_STOP: /* nothing to do */
395 break;
396
397 case HTTP_RULE_RES_DENY: /* deny or tarpit */
398 if (txn->flags & TX_CLTARPIT)
399 goto tarpit;
400 goto deny;
401
402 case HTTP_RULE_RES_ABRT: /* abort request, response already sent. Eg: auth */
403 goto return_prx_cond;
404
405 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
406 goto done;
407
408 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
409 goto return_bad_req;
Christopher Faulet3a26bee2019-12-16 12:47:40 +0100410
411 case HTTP_RULE_RES_ERROR: /* failed with a bad request */
412 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200413 }
414 }
415
Christopher Faulet18c13d32022-05-16 11:43:10 +0200416 if (px->options2 & (PR_O2_RSTRICT_REQ_HDR_NAMES_BLK|PR_O2_RSTRICT_REQ_HDR_NAMES_DEL)) {
417 verdict = http_req_restrict_header_names(s, htx, px);
418 if (verdict == HTTP_RULE_RES_DENY)
419 goto deny;
420 }
421
Christopher Faulete0768eb2018-10-03 16:38:02 +0200422 if (conn && (conn->flags & CO_FL_EARLY_DATA) &&
Olivier Houchard220a26c2020-01-23 14:57:36 +0100423 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_SSL_WAIT_HS))) {
Christopher Fauletff2759f2018-10-24 11:13:16 +0200424 struct http_hdr_ctx ctx;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200425
Christopher Fauletff2759f2018-10-24 11:13:16 +0200426 ctx.blk = NULL;
427 if (!http_find_header(htx, ist("Early-Data"), &ctx, 0)) {
428 if (unlikely(!http_add_header(htx, ist("Early-Data"), ist("1"))))
Christopher Fauletd649b572022-06-01 17:42:35 +0200429 goto return_fail_rewrite;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200430 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200431 }
432
433 /* OK at this stage, we know that the request was accepted according to
434 * the http-request rules, we can check for the stats. Note that the
435 * URI is detected *before* the req* rules in order not to be affected
436 * by a possible reqrep, while they are processed *after* so that a
437 * reqdeny can still block them. This clearly needs to change in 1.6!
438 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200439 if (!s->target && http_stats_check_uri(s, txn, px)) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200440 s->target = &http_stats_applet.obj_type;
Willy Tarreaua0b58b52022-05-27 08:33:53 +0200441 if (unlikely(!sc_applet_create(s->scb, objt_applet(s->target)))) {
Willy Tarreau69530f52023-04-28 09:16:15 +0200442 s->logs.request_ts = now_ns;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200443 if (!(s->flags & SF_ERR_MASK))
444 s->flags |= SF_ERR_RESOURCE;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100445 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200446 }
447
448 /* parse the whole stats request and extract the relevant information */
Aurelien DARRAGONf6286142023-12-05 17:54:20 +0100449 http_handle_stats(s, req, px);
Christopher Fauletd4150ad2021-10-13 15:35:55 +0200450 verdict = http_req_get_intercept_rule(px, NULL, &px->uri_auth->http_req_rules, s);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200451 /* not all actions implemented: deny, allow, auth */
452
453 if (verdict == HTTP_RULE_RES_DENY) /* stats http-request deny */
454 goto deny;
455
456 if (verdict == HTTP_RULE_RES_ABRT) /* stats auth / stats http-request auth */
457 goto return_prx_cond;
Christopher Faulet3a26bee2019-12-16 12:47:40 +0100458
459 if (verdict == HTTP_RULE_RES_BADREQ) /* failed with a bad request */
460 goto return_bad_req;
461
462 if (verdict == HTTP_RULE_RES_ERROR) /* failed with a bad request */
463 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200464 }
465
Christopher Faulet2571bc62019-03-01 11:44:26 +0100466 /* Proceed with the applets now. */
467 if (unlikely(objt_applet(s->target))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200468 if (sess->fe == s->be) /* report it if the request was intercepted by the frontend */
Willy Tarreau4781b152021-04-06 13:53:36 +0200469 _HA_ATOMIC_INC(&sess->fe->fe_counters.intercepted_req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200470
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200471 if (http_handle_expect_hdr(s, htx, msg) == -1)
Christopher Fauletb8a53712019-12-16 11:29:38 +0100472 goto return_int_err;
Christopher Fauletbcf242a2019-03-01 11:36:26 +0100473
Christopher Faulete0768eb2018-10-03 16:38:02 +0200474 if (!(s->flags & SF_ERR_MASK)) // this is not really an error but it is
475 s->flags |= SF_ERR_LOCAL; // to mark that it comes from the proxy
Christopher Faulet4a66c942023-01-13 09:43:21 +0100476 http_set_term_flags(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200477
Christopher Fauletc2ac5e42021-03-08 18:20:09 +0100478 if (HAS_FILTERS(s))
479 req->analysers |= AN_REQ_FLT_HTTP_HDRS;
480
Christopher Faulete0768eb2018-10-03 16:38:02 +0200481 /* enable the minimally required analyzers to handle keep-alive and compression on the HTTP response */
482 req->analysers &= (AN_REQ_HTTP_BODY | AN_REQ_FLT_HTTP_HDRS | AN_REQ_FLT_END);
483 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
484 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Christopher Fauletbcf242a2019-03-01 11:36:26 +0100485
Christopher Faulet5c281d52023-03-16 15:53:28 +0100486 s->scb->flags |= SC_FL_SND_ASAP;
Christopher Fauletbcf242a2019-03-01 11:36:26 +0100487 s->flags |= SF_ASSIGNED;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200488 goto done;
489 }
490
491 /* check whether we have some ACLs set to redirect this request */
492 list_for_each_entry(rule, &px->redirect_rules, list) {
493 if (rule->cond) {
494 int ret;
495
496 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
497 ret = acl_pass(ret);
498 if (rule->cond->pol == ACL_COND_UNLESS)
499 ret = !ret;
500 if (!ret)
501 continue;
502 }
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200503 if (!http_apply_redirect_rule(rule, s, txn))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100504 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200505 goto done;
506 }
507
508 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
509 * If this happens, then the data will not come immediately, so we must
510 * send all what we have without waiting. Note that due to the small gain
511 * in waiting for the body of the request, it's easier to simply put the
Christopher Faulet5c281d52023-03-16 15:53:28 +0100512 * SC_FL_SND_ASAP flag on the back SC any time. It's a one-shot flag so it
513 * will remove itself once used.
Christopher Faulete0768eb2018-10-03 16:38:02 +0200514 */
Christopher Faulet5c281d52023-03-16 15:53:28 +0100515 s->scb->flags |= SC_FL_SND_ASAP;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200516
517 done: /* done with this analyser, continue with next ones that the calling
518 * points will have set, if any.
519 */
520 req->analyse_exp = TICK_ETERNITY;
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +0500521 done_without_exp: /* done with this analyser, but don't reset the analyse_exp. */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200522 req->analysers &= ~an_bit;
Christopher Fauletd4150ad2021-10-13 15:35:55 +0200523 s->current_rule = s->current_rule_list = NULL;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100524 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200525 return 1;
526
527 tarpit:
528 /* Allow cookie logging
529 */
530 if (s->be->cookie_name || sess->fe->capture_name)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200531 http_manage_client_side_cookies(s, req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200532
533 /* When a connection is tarpitted, we use the tarpit timeout,
534 * which may be the same as the connect timeout if unspecified.
535 * If unset, then set it to zero because we really want it to
536 * eventually expire. We build the tarpit as an analyser.
537 */
Christopher Faulet202c6ce2019-01-07 14:57:35 +0100538 channel_htx_erase(&s->req, htx);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200539
540 /* wipe the request out so that we can drop the connection early
541 * if the client closes first.
542 */
543 channel_dont_connect(req);
544
Christopher Faulete0768eb2018-10-03 16:38:02 +0200545 req->analysers &= AN_REQ_FLT_END; /* remove switching rules etc... */
546 req->analysers |= AN_REQ_HTTP_TARPIT;
547 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
548 if (!req->analyse_exp)
549 req->analyse_exp = tick_add(now_ms, 0);
550 stream_inc_http_err_ctr(s);
Willy Tarreau4781b152021-04-06 13:53:36 +0200551 _HA_ATOMIC_INC(&sess->fe->fe_counters.denied_req);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100552 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau4781b152021-04-06 13:53:36 +0200553 _HA_ATOMIC_INC(&s->be->be_counters.denied_req);
William Lallemand36119de2021-03-08 15:26:48 +0100554 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200555 _HA_ATOMIC_INC(&sess->listener->counters->denied_req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200556 goto done_without_exp;
557
558 deny: /* this request was blocked (denied) */
559
560 /* Allow cookie logging
561 */
562 if (s->be->cookie_name || sess->fe->capture_name)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200563 http_manage_client_side_cookies(s, req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200564
Willy Tarreau69530f52023-04-28 09:16:15 +0200565 s->logs.request_ts = now_ns;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200566 stream_inc_http_err_ctr(s);
Willy Tarreau4781b152021-04-06 13:53:36 +0200567 _HA_ATOMIC_INC(&sess->fe->fe_counters.denied_req);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100568 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau4781b152021-04-06 13:53:36 +0200569 _HA_ATOMIC_INC(&s->be->be_counters.denied_req);
William Lallemand36119de2021-03-08 15:26:48 +0100570 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200571 _HA_ATOMIC_INC(&sess->listener->counters->denied_req);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100572 goto return_prx_err;
573
Christopher Fauletd649b572022-06-01 17:42:35 +0200574 return_fail_rewrite:
575 if (!(s->flags & SF_ERR_MASK))
576 s->flags |= SF_ERR_PRXCOND;
577 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_rewrites);
578 if (s->flags & SF_BE_ASSIGNED)
579 _HA_ATOMIC_INC(&s->be->be_counters.failed_rewrites);
580 if (sess->listener && sess->listener->counters)
581 _HA_ATOMIC_INC(&sess->listener->counters->failed_rewrites);
582 if (objt_server(s->target))
583 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_rewrites);
584 /* fall through */
585
Christopher Fauletb8a53712019-12-16 11:29:38 +0100586 return_int_err:
587 txn->status = 500;
588 if (!(s->flags & SF_ERR_MASK))
589 s->flags |= SF_ERR_INTERNAL;
Willy Tarreau4781b152021-04-06 13:53:36 +0200590 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100591 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau4781b152021-04-06 13:53:36 +0200592 _HA_ATOMIC_INC(&s->be->be_counters.internal_errors);
William Lallemand36119de2021-03-08 15:26:48 +0100593 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200594 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100595 goto return_prx_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200596
597 return_bad_req:
Christopher Faulete0768eb2018-10-03 16:38:02 +0200598 txn->status = 400;
Willy Tarreau4781b152021-04-06 13:53:36 +0200599 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req);
William Lallemand36119de2021-03-08 15:26:48 +0100600 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200601 _HA_ATOMIC_INC(&sess->listener->counters->failed_req);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100602 /* fall through */
603
604 return_prx_err:
605 http_reply_and_close(s, txn->status, http_error_message(s));
606 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200607
608 return_prx_cond:
Christopher Faulet4a66c942023-01-13 09:43:21 +0100609 http_set_term_flags(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200610
611 req->analysers &= AN_REQ_FLT_END;
612 req->analyse_exp = TICK_ETERNITY;
Christopher Fauletd4150ad2021-10-13 15:35:55 +0200613 s->current_rule = s->current_rule_list = NULL;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100614 DBG_TRACE_DEVEL("leaving on error",
615 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200616 return 0;
617
618 return_prx_yield:
619 channel_dont_connect(req);
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100620 DBG_TRACE_DEVEL("waiting for more data",
621 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200622 return 0;
623}
624
625/* This function performs all the processing enabled for the current request.
626 * It returns 1 if the processing can continue on next analysers, or zero if it
627 * needs more data, encounters an error, or wants to immediately abort the
628 * request. It relies on buffers flags, and updates s->req.analysers.
629 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200630int http_process_request(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200631{
632 struct session *sess = s->sess;
633 struct http_txn *txn = s->txn;
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200634 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200635 struct connection *cli_conn = objt_conn(strm_sess(s)->origin);
636
Christopher Faulet8bebd2f2020-10-06 17:54:56 +0200637 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200638
639 /*
640 * Right now, we know that we have processed the entire headers
641 * and that unwanted requests have been filtered out. We can do
642 * whatever we want with the remaining request. Also, now we
643 * may have separate values for ->fe, ->be.
644 */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100645 htx = htxbuf(&req->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200646
647 /*
Christopher Faulete0768eb2018-10-03 16:38:02 +0200648 * 7: Now we can work with the cookies.
649 * Note that doing so might move headers in the request, but
650 * the fields will stay coherent and the URI will not move.
651 * This should only be performed in the backend.
652 */
653 if (s->be->cookie_name || sess->fe->capture_name)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200654 http_manage_client_side_cookies(s, req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200655
Tim Duesterhus2825b4b2020-02-28 15:13:34 +0100656 /* 8: Generate unique ID if a "unique-id-format" is defined.
657 *
658 * A unique ID is generated even when it is not sent to ensure that the ID can make use of
659 * fetches only available in the HTTP request processing stage.
660 */
661 if (!LIST_ISEMPTY(&sess->fe->format_unique_id)) {
Tim Duesterhusa17e6622020-03-05 20:19:02 +0100662 struct ist unique_id = stream_generate_unique_id(s, &sess->fe->format_unique_id);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200663
Tim Duesterhusa17e6622020-03-05 20:19:02 +0100664 if (!isttest(unique_id)) {
Christopher Fauletb8a53712019-12-16 11:29:38 +0100665 if (!(s->flags & SF_ERR_MASK))
666 s->flags |= SF_ERR_RESOURCE;
667 goto return_int_err;
668 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200669
Tim Duesterhus2825b4b2020-02-28 15:13:34 +0100670 /* send unique ID if a "unique-id-header" is defined */
Tim Duesterhus0643b0e2020-03-05 17:56:35 +0100671 if (isttest(sess->fe->header_unique_id) &&
Tim Duesterhus7ad27d42022-05-18 00:22:15 +0200672 unlikely(!http_add_header(htx, sess->fe->header_unique_id, unique_id)))
Christopher Fauletd649b572022-06-01 17:42:35 +0200673 goto return_fail_rewrite;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200674 }
675
Aurelien DARRAGONb2e2ec52023-01-09 11:09:03 +0100676 /* handle http extensions (if configured) */
677 if (unlikely(!http_handle_7239_header(s, req)))
678 goto return_fail_rewrite;
679 if (unlikely(!http_handle_xff_header(s, req)))
680 goto return_fail_rewrite;
681 if (unlikely(!http_handle_xot_header(s, req)))
682 goto return_fail_rewrite;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200683
Christopher Fauletc2ac5e42021-03-08 18:20:09 +0100684 /* Filter the request headers if there are filters attached to the
685 * stream.
686 */
687 if (HAS_FILTERS(s))
688 req->analysers |= AN_REQ_FLT_HTTP_HDRS;
689
Christopher Faulete0768eb2018-10-03 16:38:02 +0200690 /* If we have no server assigned yet and we're balancing on url_param
691 * with a POST request, we may be interested in checking the body for
692 * that parameter. This will be done in another analyser.
693 */
694 if (!(s->flags & (SF_ASSIGNED|SF_DIRECT)) &&
Willy Tarreau089eaa02019-01-14 15:17:46 +0100695 s->txn->meth == HTTP_METH_POST &&
696 (s->be->lbprm.algo & BE_LB_ALGO) == BE_LB_ALGO_PH) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200697 channel_dont_connect(req);
698 req->analysers |= AN_REQ_HTTP_BODY;
699 }
700
701 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
702 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau1a18b542018-12-11 16:37:42 +0100703
Christopher Faulete0768eb2018-10-03 16:38:02 +0200704 /* We expect some data from the client. Unless we know for sure
705 * we already have a full request, we have to re-enable quick-ack
706 * in case we previously disabled it, otherwise we might cause
707 * the client to delay further data.
708 */
Willy Tarreau9bdcf422023-01-12 19:40:42 +0100709 if ((sess->listener && (sess->listener->bind_conf->options & BC_O_NOQUICKACK)) && !(htx->flags & HTX_FL_EOM))
Willy Tarreau1a18b542018-12-11 16:37:42 +0100710 conn_set_quickack(cli_conn, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200711
712 /*************************************************************
713 * OK, that's finished for the headers. We have done what we *
714 * could. Let's switch to the DATA state. *
715 ************************************************************/
716 req->analyse_exp = TICK_ETERNITY;
717 req->analysers &= ~an_bit;
718
Willy Tarreau69530f52023-04-28 09:16:15 +0200719 s->logs.request_ts = now_ns;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200720 /* OK let's go on with the BODY now */
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100721 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200722 return 1;
723
Christopher Fauletd649b572022-06-01 17:42:35 +0200724 return_fail_rewrite:
725 if (!(s->flags & SF_ERR_MASK))
726 s->flags |= SF_ERR_PRXCOND;
727 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_rewrites);
728 if (s->flags & SF_BE_ASSIGNED)
729 _HA_ATOMIC_INC(&s->be->be_counters.failed_rewrites);
730 if (sess->listener && sess->listener->counters)
731 _HA_ATOMIC_INC(&sess->listener->counters->failed_rewrites);
732 if (objt_server(s->target))
733 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_rewrites);
734 /* fall through */
735
Christopher Fauletb8a53712019-12-16 11:29:38 +0100736 return_int_err:
737 txn->status = 500;
738 if (!(s->flags & SF_ERR_MASK))
739 s->flags |= SF_ERR_INTERNAL;
Willy Tarreau4781b152021-04-06 13:53:36 +0200740 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100741 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau4781b152021-04-06 13:53:36 +0200742 _HA_ATOMIC_INC(&s->be->be_counters.internal_errors);
William Lallemand36119de2021-03-08 15:26:48 +0100743 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200744 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100745
Christopher Fauletb8a53712019-12-16 11:29:38 +0100746 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet4a66c942023-01-13 09:43:21 +0100747 http_set_term_flags(s);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100748
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100749 DBG_TRACE_DEVEL("leaving on error",
750 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200751 return 0;
752}
753
754/* This function is an analyser which processes the HTTP tarpit. It always
755 * returns zero, at the beginning because it prevents any other processing
756 * from occurring, and at the end because it terminates the request.
757 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200758int http_process_tarpit(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200759{
760 struct http_txn *txn = s->txn;
761
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100762 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, &txn->req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200763 /* This connection is being tarpitted. The CLIENT side has
764 * already set the connect expiration date to the right
765 * timeout. We just have to check that the client is still
766 * there and that the timeout has not expired.
767 */
768 channel_dont_connect(req);
Christopher Fauletca5309a2023-04-17 16:17:32 +0200769 if (!(s->scf->flags & (SC_FL_ABRT_DONE|SC_FL_EOS)) &&
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100770 !tick_is_expired(req->analyse_exp, now_ms)) {
Christopher Fauletb0c87f12021-10-29 14:37:07 +0200771 /* Be sure to drain all data from the request channel */
772 channel_htx_erase(req, htxbuf(&req->buf));
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100773 DBG_TRACE_DEVEL("waiting for tarpit timeout expiry",
774 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200775 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100776 }
777
Christopher Faulete0768eb2018-10-03 16:38:02 +0200778
779 /* We will set the queue timer to the time spent, just for
780 * logging purposes. We fake a 500 server error, so that the
781 * attacker will not suspect his connection has been tarpitted.
782 * It will not cause trouble to the logs because we can exclude
783 * the tarpitted connections by filtering on the 'PT' status flags.
784 */
Willy Tarreau69530f52023-04-28 09:16:15 +0200785 s->logs.t_queue = ns_to_ms(now_ns - s->logs.accept_ts);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200786
Christopher Fauletd7bac882023-04-14 12:05:55 +0200787 http_reply_and_close(s, txn->status, (!(s->scf->flags & SC_FL_ERROR) ? http_error_message(s) : NULL));
Christopher Faulet4a66c942023-01-13 09:43:21 +0100788 http_set_term_flags(s);
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100789
790 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200791 return 0;
792}
793
794/* This function is an analyser which waits for the HTTP request body. It waits
795 * for either the buffer to be full, or the full advertised contents to have
796 * reached the buffer. It must only be called after the standard HTTP request
797 * processing has occurred, because it expects the request to be parsed and will
798 * look for the Expect header. It may send a 100-Continue interim response. It
Christopher Faulet2954bcc2023-04-05 10:42:03 +0200799 * returns zero if it needs to read more data, or 1 once it has completed its
800 * analysis.
Christopher Faulete0768eb2018-10-03 16:38:02 +0200801 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200802int http_wait_for_request_body(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200803{
804 struct session *sess = s->sess;
805 struct http_txn *txn = s->txn;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200806
Christopher Fauletf4569bb2023-01-13 10:49:31 +0100807 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, &s->txn->req);
Christopher Fauletf76ebe82018-10-24 11:16:22 +0200808
Christopher Fauletf76ebe82018-10-24 11:16:22 +0200809
Christopher Faulet021a8e42021-03-29 10:46:38 +0200810 switch (http_wait_for_msg_body(s, req, s->be->timeout.httpreq, 0)) {
811 case HTTP_RULE_RES_CONT:
812 goto http_end;
813 case HTTP_RULE_RES_YIELD:
814 goto missing_data_or_waiting;
815 case HTTP_RULE_RES_BADREQ:
Willy Tarreau4236f032019-03-05 10:43:32 +0100816 goto return_bad_req;
Christopher Faulet021a8e42021-03-29 10:46:38 +0200817 case HTTP_RULE_RES_ERROR:
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200818 goto return_int_err;
Christopher Faulet021a8e42021-03-29 10:46:38 +0200819 case HTTP_RULE_RES_ABRT:
Christopher Fauletb8a53712019-12-16 11:29:38 +0100820 goto return_prx_cond;
Christopher Faulet021a8e42021-03-29 10:46:38 +0200821 default:
822 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200823 }
824
825 http_end:
826 /* The situation will not evolve, so let's give up on the analysis. */
Willy Tarreau69530f52023-04-28 09:16:15 +0200827 s->logs.request_ts = now_ns; /* update the request timer to reflect full request */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200828 req->analysers &= ~an_bit;
829 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100830 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200831 return 1;
832
Christopher Faulet021a8e42021-03-29 10:46:38 +0200833 missing_data_or_waiting:
834 channel_dont_connect(req);
835 DBG_TRACE_DEVEL("waiting for more data",
836 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
837 return 0;
838
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200839 return_int_err:
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200840 txn->status = 500;
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200841 if (!(s->flags & SF_ERR_MASK))
842 s->flags |= SF_ERR_INTERNAL;
Willy Tarreau4781b152021-04-06 13:53:36 +0200843 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100844 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau4781b152021-04-06 13:53:36 +0200845 _HA_ATOMIC_INC(&s->be->be_counters.internal_errors);
William Lallemand36119de2021-03-08 15:26:48 +0100846 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200847 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Faulet021a8e42021-03-29 10:46:38 +0200848 goto return_prx_err;
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200849
Christopher Faulete0768eb2018-10-03 16:38:02 +0200850 return_bad_req: /* let's centralize all bad requests */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200851 txn->status = 400;
Willy Tarreau4781b152021-04-06 13:53:36 +0200852 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req);
William Lallemand36119de2021-03-08 15:26:48 +0100853 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200854 _HA_ATOMIC_INC(&sess->listener->counters->failed_req);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100855 /* fall through */
856
Christopher Faulet021a8e42021-03-29 10:46:38 +0200857 return_prx_err:
Christopher Fauletb8a53712019-12-16 11:29:38 +0100858 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet021a8e42021-03-29 10:46:38 +0200859 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200860
Christopher Faulet021a8e42021-03-29 10:46:38 +0200861 return_prx_cond:
Christopher Fauletf0d80df2023-01-13 10:20:20 +0100862 http_set_term_flags(s);
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200863
Christopher Faulete0768eb2018-10-03 16:38:02 +0200864 req->analysers &= AN_REQ_FLT_END;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100865 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100866 DBG_TRACE_DEVEL("leaving on error",
867 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200868 return 0;
869}
870
871/* This function is an analyser which forwards request body (including chunk
872 * sizes if any). It is called as soon as we must forward, even if we forward
873 * zero byte. The only situation where it must not be called is when we're in
874 * tunnel mode and we want to forward till the close. It's used both to forward
875 * remaining data and to resync after end of body. It expects the msg_state to
876 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
877 * read more data, or 1 once we can go on with next request or end the stream.
878 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
879 * bytes of pending data + the headers if not already done.
880 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200881int http_request_forward_body(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200882{
883 struct session *sess = s->sess;
884 struct http_txn *txn = s->txn;
Christopher Faulet9768c262018-10-22 09:34:31 +0200885 struct http_msg *msg = &txn->req;
886 struct htx *htx;
Christopher Faulet93e02d82019-03-08 14:18:50 +0100887 short status = 0;
Christopher Fauletaed82cf2018-11-30 22:22:32 +0100888 int ret;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200889
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100890 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200891
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100892 htx = htxbuf(&req->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200893
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200894 if (htx->flags & HTX_FL_PARSING_ERROR)
895 goto return_bad_req;
896 if (htx->flags & HTX_FL_PROCESSING_ERROR)
897 goto return_int_err;
898
Christopher Faulete0768eb2018-10-03 16:38:02 +0200899 /* Note that we don't have to send 100-continue back because we don't
900 * need the data to complete our job, and it's up to the server to
901 * decide whether to return 100, 417 or anything else in return of
902 * an "Expect: 100-continue" header.
903 */
Christopher Faulet9768c262018-10-22 09:34:31 +0200904 if (msg->msg_state == HTTP_MSG_BODY)
905 msg->msg_state = HTTP_MSG_DATA;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200906
Christopher Faulete0768eb2018-10-03 16:38:02 +0200907 /* in most states, we should abort in case of early close */
908 channel_auto_close(req);
909
910 if (req->to_forward) {
Christopher Faulet66af0b22019-03-22 14:54:52 +0100911 if (req->to_forward == CHN_INFINITE_FORWARD) {
Christopher Faulet904763f2023-03-22 14:53:11 +0100912 if (s->scf->flags & SC_FL_EOI)
Christopher Faulet1a3e0272019-11-15 16:31:46 +0100913 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet66af0b22019-03-22 14:54:52 +0100914 }
915 else {
916 /* We can't process the buffer's contents yet */
917 req->flags |= CF_WAKE_WRITE;
918 goto missing_data_or_waiting;
919 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200920 }
921
Christopher Faulet1a3e0272019-11-15 16:31:46 +0100922 if (msg->msg_state >= HTTP_MSG_ENDING)
923 goto ending;
924
925 if (txn->meth == HTTP_METH_CONNECT) {
926 msg->msg_state = HTTP_MSG_ENDING;
927 goto ending;
928 }
929
Christopher Fauletaed82cf2018-11-30 22:22:32 +0100930 /* Forward input data. We get it by removing all outgoing data not
931 * forwarded yet from HTX data size. If there are some data filters, we
932 * let them decide the amount of data to forward.
Christopher Faulet9768c262018-10-22 09:34:31 +0200933 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +0100934 if (HAS_REQ_DATA_FILTERS(s)) {
935 ret = flt_http_payload(s, msg, htx->data);
936 if (ret < 0)
937 goto return_bad_req;
Christopher Faulet421e7692019-06-13 11:16:45 +0200938 c_adv(req, ret);
Christopher Fauletaed82cf2018-11-30 22:22:32 +0100939 }
940 else {
Christopher Faulet421e7692019-06-13 11:16:45 +0200941 c_adv(req, htx->data - co_data(req));
Christopher Faulet2f7c82b2023-02-20 14:06:52 +0100942 if ((global.tune.options & GTUNE_USE_FAST_FWD) && (msg->flags & HTTP_MSGF_XFER_LEN))
Christopher Faulet66af0b22019-03-22 14:54:52 +0100943 channel_htx_forward_forever(req, htx);
Christopher Fauletaed82cf2018-11-30 22:22:32 +0100944 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200945
Christopher Faulet1a3e0272019-11-15 16:31:46 +0100946 if (htx->data != co_data(req))
947 goto missing_data_or_waiting;
Christopher Fauletd20fdb02019-06-13 16:43:22 +0200948
Christopher Faulet9768c262018-10-22 09:34:31 +0200949 /* Check if the end-of-message is reached and if so, switch the message
Christopher Fauletd20fdb02019-06-13 16:43:22 +0200950 * in HTTP_MSG_ENDING state. Then if all data was marked to be
951 * forwarded, set the state to HTTP_MSG_DONE.
Christopher Faulet9768c262018-10-22 09:34:31 +0200952 */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +0100953 if (!(htx->flags & HTX_FL_EOM))
Christopher Faulet9768c262018-10-22 09:34:31 +0200954 goto missing_data_or_waiting;
955
Christopher Fauletd20fdb02019-06-13 16:43:22 +0200956 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet9768c262018-10-22 09:34:31 +0200957
Christopher Faulet1a3e0272019-11-15 16:31:46 +0100958 ending:
Christopher Faulet84d3ef92023-03-17 15:45:58 +0100959 s->scb->flags &= ~SC_FL_SND_EXP_MORE; /* no more data are expected to be send */
Christopher Faulet2151cdd2020-07-22 16:34:59 +0200960
Christopher Faulet1a3e0272019-11-15 16:31:46 +0100961 /* other states, ENDING...TUNNEL */
962 if (msg->msg_state >= HTTP_MSG_DONE)
963 goto done;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200964
Christopher Fauletaed82cf2018-11-30 22:22:32 +0100965 if (HAS_REQ_DATA_FILTERS(s)) {
966 ret = flt_http_end(s, msg);
967 if (ret <= 0) {
968 if (!ret)
969 goto missing_data_or_waiting;
970 goto return_bad_req;
971 }
972 }
973
Christopher Faulet1a3e0272019-11-15 16:31:46 +0100974 if (txn->meth == HTTP_METH_CONNECT)
975 msg->msg_state = HTTP_MSG_TUNNEL;
976 else {
977 msg->msg_state = HTTP_MSG_DONE;
978 req->to_forward = 0;
979 }
980
981 done:
982 /* we don't want to forward closes on DONE except in tunnel mode. */
983 if (!(txn->flags & TX_CON_WANT_TUN))
984 channel_dont_close(req);
985
Christopher Faulet64350bb2023-04-13 16:37:37 +0200986 if ((s->scb->flags & SC_FL_SHUT_DONE) && co_data(req)) {
Christopher Fauletf2b02cf2023-01-13 11:02:28 +0100987 /* request errors are most likely due to the server aborting the
Christopher Fauletbea90bb2023-09-14 11:12:32 +0200988 * transfer.Bit handle server aborts only if there is no
989 * response. Otherwise, let a change to foward the response
990 * first.
991 */
992 if (htx_is_empty(htxbuf(&s->res.buf)))
993 goto return_srv_abort;
Christopher Fauletf2b02cf2023-01-13 11:02:28 +0100994 }
995
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200996 http_end_request(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200997 if (!(req->analysers & an_bit)) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100998 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200999 return 1;
1000 }
1001
1002 /* If "option abortonclose" is set on the backend, we want to monitor
1003 * the client's connection and forward any shutdown notification to the
1004 * server, which will decide whether to close or to go on processing the
1005 * request. We only do that in tunnel mode, and not in other modes since
1006 * it can be abused to exhaust source ports. */
Christopher Faulet769d0e92019-03-22 14:23:18 +01001007 if (s->be->options & PR_O_ABRT_CLOSE) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001008 channel_auto_read(req);
Christopher Fauletca5309a2023-04-17 16:17:32 +02001009 if ((s->scf->flags & (SC_FL_ABRT_DONE|SC_FL_EOS)) && !(txn->flags & TX_CON_WANT_TUN))
Willy Tarreaucb041662022-05-17 19:44:42 +02001010 s->scb->flags |= SC_FL_NOLINGER;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001011 channel_auto_close(req);
1012 }
1013 else if (s->txn->meth == HTTP_METH_POST) {
1014 /* POST requests may require to read extra CRLF sent by broken
1015 * browsers and which could cause an RST to be sent upon close
1016 * on some systems (eg: Linux). */
1017 channel_auto_read(req);
1018 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001019 DBG_TRACE_DEVEL("waiting for the end of the HTTP txn",
1020 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001021 return 0;
1022
1023 missing_data_or_waiting:
1024 /* stop waiting for data if the input is closed before the end */
Christopher Fauletca5309a2023-04-17 16:17:32 +02001025 if (msg->msg_state < HTTP_MSG_ENDING && (s->scf->flags & (SC_FL_ABRT_DONE|SC_FL_EOS)))
Christopher Faulet93e02d82019-03-08 14:18:50 +01001026 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001027
1028 waiting:
1029 /* waiting for the last bits to leave the buffer */
Christopher Fauletbea90bb2023-09-14 11:12:32 +02001030 if (s->scb->flags & SC_FL_SHUT_DONE) {
1031 /* Handle server aborts only if there is no response. Otherwise,
1032 * let a change to foward the response first.
1033 */
1034 if (htx_is_empty(htxbuf(&s->res.buf)))
1035 goto return_srv_abort;
1036 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001037
1038 /* When TE: chunked is used, we need to get there again to parse remaining
1039 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
1040 * And when content-length is used, we never want to let the possible
1041 * shutdown be forwarded to the other side, as the state machine will
1042 * take care of it once the client responds. It's also important to
1043 * prevent TIME_WAITs from accumulating on the backend side, and for
1044 * HTTP/2 where the last frame comes with a shutdown.
1045 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001046 if (msg->flags & HTTP_MSGF_XFER_LEN)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001047 channel_dont_close(req);
1048
1049 /* We know that more data are expected, but we couldn't send more that
Christopher Faulet84d3ef92023-03-17 15:45:58 +01001050 * what we did. So we always set the SC_FL_SND_EXP_MORE flag so that the
Christopher Faulete0768eb2018-10-03 16:38:02 +02001051 * system knows it must not set a PUSH on this first part. Interactive
1052 * modes are already handled by the stream sock layer. We must not do
1053 * this in content-length mode because it could present the MSG_MORE
1054 * flag with the last block of forwarded data, which would cause an
1055 * additional delay to be observed by the receiver.
1056 */
Christopher Faulet2151cdd2020-07-22 16:34:59 +02001057 if (HAS_REQ_DATA_FILTERS(s))
Christopher Faulet84d3ef92023-03-17 15:45:58 +01001058 s->scb->flags |= SC_FL_SND_EXP_MORE;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001059
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001060 DBG_TRACE_DEVEL("waiting for more data to forward",
1061 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001062 return 0;
1063
Christopher Faulet93e02d82019-03-08 14:18:50 +01001064 return_cli_abort:
Willy Tarreau4781b152021-04-06 13:53:36 +02001065 _HA_ATOMIC_INC(&sess->fe->fe_counters.cli_aborts);
1066 _HA_ATOMIC_INC(&s->be->be_counters.cli_aborts);
William Lallemand36119de2021-03-08 15:26:48 +01001067 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001068 _HA_ATOMIC_INC(&sess->listener->counters->cli_aborts);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001069 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001070 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.cli_aborts);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001071 if (!(s->flags & SF_ERR_MASK))
Christopher Fauletf2b02cf2023-01-13 11:02:28 +01001072 s->flags |= ((req->flags & CF_READ_TIMEOUT) ? SF_ERR_CLITO : SF_ERR_CLICL);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001073 status = 400;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001074 goto return_prx_cond;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001075
1076 return_srv_abort:
Willy Tarreau4781b152021-04-06 13:53:36 +02001077 _HA_ATOMIC_INC(&sess->fe->fe_counters.srv_aborts);
1078 _HA_ATOMIC_INC(&s->be->be_counters.srv_aborts);
William Lallemand36119de2021-03-08 15:26:48 +01001079 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001080 _HA_ATOMIC_INC(&sess->listener->counters->srv_aborts);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001081 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001082 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.srv_aborts);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001083 if (!(s->flags & SF_ERR_MASK))
Christopher Fauletf2b02cf2023-01-13 11:02:28 +01001084 s->flags |= ((req->flags & CF_WRITE_TIMEOUT) ? SF_ERR_SRVTO : SF_ERR_SRVCL);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001085 status = 502;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001086 goto return_prx_cond;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001087
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001088 return_int_err:
1089 if (!(s->flags & SF_ERR_MASK))
1090 s->flags |= SF_ERR_INTERNAL;
Willy Tarreau4781b152021-04-06 13:53:36 +02001091 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
1092 _HA_ATOMIC_INC(&s->be->be_counters.internal_errors);
William Lallemand36119de2021-03-08 15:26:48 +01001093 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001094 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001095 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001096 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.internal_errors);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001097 status = 500;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001098 goto return_prx_cond;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001099
Christopher Faulet93e02d82019-03-08 14:18:50 +01001100 return_bad_req:
Willy Tarreau4781b152021-04-06 13:53:36 +02001101 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req);
William Lallemand36119de2021-03-08 15:26:48 +01001102 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001103 _HA_ATOMIC_INC(&sess->listener->counters->failed_req);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001104 status = 400;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001105 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001106
Christopher Fauletb8a53712019-12-16 11:29:38 +01001107 return_prx_cond:
Christopher Faulet9768c262018-10-22 09:34:31 +02001108 if (txn->status > 0) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001109 /* Note: we don't send any error if some data were already sent */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001110 http_reply_and_close(s, txn->status, NULL);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001111 } else {
Christopher Faulet93e02d82019-03-08 14:18:50 +01001112 txn->status = status;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001113 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001114 }
Christopher Fauletf2b02cf2023-01-13 11:02:28 +01001115 http_set_term_flags(s);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001116 DBG_TRACE_DEVEL("leaving on error ",
1117 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001118 return 0;
1119}
1120
Willy Tarreau4596fe22022-05-17 19:07:51 +02001121/* Reset the stream and the backend stream connector to a situation suitable for attemption connection */
Olivier Houcharda254a372019-04-05 15:30:12 +02001122/* Returns 0 if we can attempt to retry, -1 otherwise */
Willy Tarreaub49672d2022-05-27 10:13:37 +02001123static __inline int do_l7_retry(struct stream *s, struct stconn *sc)
Olivier Houcharda254a372019-04-05 15:30:12 +02001124{
Christopher Faulet9f5382e2021-05-21 13:46:14 +02001125 struct channel *req, *res;
1126 int co_data;
Olivier Houcharda254a372019-04-05 15:30:12 +02001127
Christopher Faulet731c8e62022-03-29 16:08:44 +02001128 if (s->conn_retries >= s->be->conn_retries)
Christopher Faulet552601d2021-05-26 10:31:06 +02001129 return -1;
Christopher Faulet41ade742023-03-01 15:45:39 +01001130 s->conn_retries++;
Christopher Faulete763c8c2021-05-05 18:23:59 +02001131 if (objt_server(s->target)) {
1132 if (s->flags & SF_CURR_SESS) {
1133 s->flags &= ~SF_CURR_SESS;
1134 _HA_ATOMIC_DEC(&__objt_server(s->target)->cur_sess);
1135 }
Willy Tarreau4781b152021-04-06 13:53:36 +02001136 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.retries);
Christopher Faulete763c8c2021-05-05 18:23:59 +02001137 }
Willy Tarreau4781b152021-04-06 13:53:36 +02001138 _HA_ATOMIC_INC(&s->be->be_counters.retries);
Willy Tarreau223995e2019-05-04 10:38:31 +02001139
Christopher Faulet9f5382e2021-05-21 13:46:14 +02001140 req = &s->req;
1141 res = &s->res;
Christopher Faulet87633c32023-04-03 18:32:50 +02001142
Olivier Houcharda254a372019-04-05 15:30:12 +02001143 /* Remove any write error from the request, and read error from the response */
Christopher Fauletca5309a2023-04-17 16:17:32 +02001144 s->scf->flags &= ~(SC_FL_EOS|SC_FL_ABRT_DONE|SC_FL_ABRT_WANTED);
Christopher Faulet87633c32023-04-03 18:32:50 +02001145 req->flags &= ~CF_WRITE_TIMEOUT;
1146 res->flags &= ~(CF_READ_TIMEOUT | CF_READ_EVENT);
Christopher Faulet7bf46bb2022-01-04 10:56:03 +01001147 res->analysers &= AN_RES_FLT_END;
Christopher Faulet50264b42022-03-30 19:39:30 +02001148 s->conn_err_type = STRM_ET_NONE;
Christopher Fauletae024ce2022-03-29 19:02:31 +02001149 s->flags &= ~(SF_CONN_EXP | SF_ERR_MASK | SF_FINST_MASK);
1150 s->conn_exp = TICK_ETERNITY;
Olivier Houchard4bd58672019-07-12 16:16:59 +02001151 stream_choose_redispatch(s);
Olivier Houcharda254a372019-04-05 15:30:12 +02001152 res->to_forward = 0;
1153 res->analyse_exp = TICK_ETERNITY;
1154 res->total = 0;
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +01001155
Christopher Fauleta1d14a72023-04-14 10:42:08 +02001156 s->scb->flags &= ~(SC_FL_ERROR|SC_FL_SHUT_DONE|SC_FL_SHUT_WANTED);
Willy Tarreau19c65a92022-05-27 08:49:24 +02001157 if (sc_reset_endp(s->scb) < 0) {
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +01001158 if (!(s->flags & SF_ERR_MASK))
1159 s->flags |= SF_ERR_INTERNAL;
1160 return -1;
1161 }
Olivier Houcharda254a372019-04-05 15:30:12 +02001162
Christopher Faulet9f5382e2021-05-21 13:46:14 +02001163 b_free(&req->buf);
1164 /* Swap the L7 buffer with the channel buffer */
1165 /* We know we stored the co_data as b_data, so get it there */
Christopher Faulete05bf9e2022-03-29 15:23:40 +02001166 co_data = b_data(&s->txn->l7_buffer);
1167 b_set_data(&s->txn->l7_buffer, b_size(&s->txn->l7_buffer));
1168 b_xfer(&req->buf, &s->txn->l7_buffer, b_data(&s->txn->l7_buffer));
Christopher Faulet9f5382e2021-05-21 13:46:14 +02001169 co_set_data(req, co_data);
Christopher Faulet5b82cc52020-10-12 15:18:50 +02001170
Ilya Shipitsinacf84592021-02-06 22:29:08 +05001171 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 +02001172
Olivier Houcharda254a372019-04-05 15:30:12 +02001173 b_reset(&res->buf);
1174 co_set_data(res, 0);
1175 return 0;
1176}
1177
Christopher Faulete0768eb2018-10-03 16:38:02 +02001178/* This stream analyser waits for a complete HTTP response. It returns 1 if the
1179 * processing can continue on next analysers, or zero if it either needs more
1180 * data or wants to immediately abort the response (eg: timeout, error, ...). It
1181 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->res.analysers
1182 * when it has nothing left to do, and may remove any analyser when it wants to
1183 * abort.
1184 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001185int http_wait_for_response(struct stream *s, struct channel *rep, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001186{
Christopher Faulet9768c262018-10-22 09:34:31 +02001187 /*
1188 * We will analyze a complete HTTP response to check the its syntax.
1189 *
1190 * Once the start line and all headers are received, we may perform a
1191 * capture of the error (if any), and we will set a few fields. We also
1192 * logging and finally headers capture.
1193 */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001194 struct session *sess = s->sess;
1195 struct http_txn *txn = s->txn;
1196 struct http_msg *msg = &txn->rsp;
Christopher Faulet9768c262018-10-22 09:34:31 +02001197 struct htx *htx;
Christopher Faulet61608322018-11-23 16:23:45 +01001198 struct connection *srv_conn;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001199 struct htx_sl *sl;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001200 int n;
1201
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001202 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001203
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001204 htx = htxbuf(&rep->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001205
Willy Tarreau4236f032019-03-05 10:43:32 +01001206 /* Parsing errors are caught here */
1207 if (htx->flags & HTX_FL_PARSING_ERROR)
1208 goto return_bad_res;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001209 if (htx->flags & HTX_FL_PROCESSING_ERROR)
1210 goto return_int_err;
Willy Tarreau4236f032019-03-05 10:43:32 +01001211
Christopher Faulete0768eb2018-10-03 16:38:02 +02001212 /*
1213 * Now we quickly check if we have found a full valid response.
1214 * If not so, we check the FD and buffer states before leaving.
1215 * A full response is indicated by the fact that we have seen
1216 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
1217 * responses are checked first.
1218 *
1219 * Depending on whether the client is still there or not, we
1220 * may send an error response back or not. Note that normally
1221 * we should only check for HTTP status there, and check I/O
1222 * errors somewhere else.
1223 */
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001224 next_one:
Christopher Faulet29f17582019-05-23 11:03:26 +02001225 if (unlikely(htx_is_empty(htx) || htx->first == -1)) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001226 /* 1: have we encountered a read error ? */
Christopher Fauletd7bac882023-04-14 12:05:55 +02001227 if (s->scb->flags & SC_FL_ERROR) {
Willy Tarreaufd9417b2022-05-18 16:23:22 +02001228 struct connection *conn = sc_conn(s->scb);
Olivier Houchard865d8392019-05-03 22:46:27 +02001229
Christopher Faulet6f78ac52023-03-01 15:47:18 +01001230
1231 if ((txn->flags & TX_L7_RETRY) &&
1232 (s->be->retry_type & PR_RE_DISCONNECTED) &&
1233 (!conn || conn->err_code != CO_ER_SSL_EARLY_FAILED)) {
1234 if (co_data(rep) || do_l7_retry(s, s->scb) == 0)
1235 return 0;
1236 }
1237
1238 /* Perform a L7 retry on empty response or because server refuses the early data. */
Christopher Faulete05bf9e2022-03-29 15:23:40 +02001239 if ((txn->flags & TX_L7_RETRY) &&
Christopher Fauletd9769232021-05-26 12:15:37 +02001240 (s->be->retry_type & PR_RE_EARLY_ERROR) &&
1241 conn && conn->err_code == CO_ER_SSL_EARLY_FAILED &&
Willy Tarreau7cb9e6c2022-05-17 19:40:40 +02001242 do_l7_retry(s, s->scb) == 0) {
Christopher Fauletd9769232021-05-26 12:15:37 +02001243 DBG_TRACE_DEVEL("leaving on L7 retry",
1244 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
1245 return 0;
1246 }
1247
Christopher Fauletca023882024-04-09 08:19:01 +02001248 if (s->flags & SF_SRV_REUSED)
Olivier Houchard6db16992019-05-17 15:40:49 +02001249 goto abort_keep_alive;
1250
Willy Tarreau4781b152021-04-06 13:53:36 +02001251 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001252 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001253 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001254 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001255 }
1256
Christopher Fauletd9769232021-05-26 12:15:37 +02001257 /* if the server refused the early data, just send a 425 */
1258 if (conn && conn->err_code == CO_ER_SSL_EARLY_FAILED)
Olivier Houchard865d8392019-05-03 22:46:27 +02001259 txn->status = 425;
Christopher Fauletd9769232021-05-26 12:15:37 +02001260 else {
1261 txn->status = 502;
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001262 stream_inc_http_fail_ctr(s);
Christopher Fauletd9769232021-05-26 12:15:37 +02001263 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001264
Willy Tarreaucb041662022-05-17 19:44:42 +02001265 s->scb->flags |= SC_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001266 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001267
1268 if (!(s->flags & SF_ERR_MASK))
1269 s->flags |= SF_ERR_SRVCL;
Christopher Faulet4a66c942023-01-13 09:43:21 +01001270 http_set_term_flags(s);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001271 DBG_TRACE_DEVEL("leaving on error",
1272 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001273 return 0;
1274 }
1275
Christopher Faulet9768c262018-10-22 09:34:31 +02001276 /* 2: read timeout : return a 504 to the client. */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001277 else if (rep->flags & CF_READ_TIMEOUT) {
Christopher Faulete05bf9e2022-03-29 15:23:40 +02001278 if ((txn->flags & TX_L7_RETRY) &&
Olivier Houcharda254a372019-04-05 15:30:12 +02001279 (s->be->retry_type & PR_RE_TIMEOUT)) {
Willy Tarreau7cb9e6c2022-05-17 19:40:40 +02001280 if (co_data(rep) || do_l7_retry(s, s->scb) == 0) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001281 DBG_TRACE_DEVEL("leaving on L7 retry",
1282 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharda254a372019-04-05 15:30:12 +02001283 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001284 }
Olivier Houcharda254a372019-04-05 15:30:12 +02001285 }
Willy Tarreau4781b152021-04-06 13:53:36 +02001286 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001287 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001288 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001289 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001290 }
1291
Christopher Faulete0768eb2018-10-03 16:38:02 +02001292 txn->status = 504;
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001293 stream_inc_http_fail_ctr(s);
Willy Tarreaucb041662022-05-17 19:44:42 +02001294 s->scb->flags |= SC_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001295 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001296
1297 if (!(s->flags & SF_ERR_MASK))
1298 s->flags |= SF_ERR_SRVTO;
Christopher Faulet4a66c942023-01-13 09:43:21 +01001299 http_set_term_flags(s);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001300 DBG_TRACE_DEVEL("leaving on error",
1301 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001302 return 0;
1303 }
1304
Christopher Faulet9768c262018-10-22 09:34:31 +02001305 /* 3: client abort with an abortonclose */
Christopher Fauletca5309a2023-04-17 16:17:32 +02001306 else if ((s->scb->flags & (SC_FL_EOS|SC_FL_ABRT_DONE)) && (s->scb->flags & SC_FL_SHUT_DONE) &&
1307 (s->scf->flags & (SC_FL_EOS|SC_FL_ABRT_DONE))) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001308 _HA_ATOMIC_INC(&sess->fe->fe_counters.cli_aborts);
1309 _HA_ATOMIC_INC(&s->be->be_counters.cli_aborts);
William Lallemand36119de2021-03-08 15:26:48 +01001310 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001311 _HA_ATOMIC_INC(&sess->listener->counters->cli_aborts);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001312 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001313 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.cli_aborts);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001314
Christopher Faulete0768eb2018-10-03 16:38:02 +02001315 txn->status = 400;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001316 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001317
1318 if (!(s->flags & SF_ERR_MASK))
1319 s->flags |= SF_ERR_CLICL;
Christopher Faulet4a66c942023-01-13 09:43:21 +01001320 http_set_term_flags(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001321
1322 /* process_stream() will take care of the error */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001323 DBG_TRACE_DEVEL("leaving on error",
1324 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001325 return 0;
1326 }
1327
Christopher Faulet9768c262018-10-22 09:34:31 +02001328 /* 4: close from server, capture the response if the server has started to respond */
Christopher Fauletca5309a2023-04-17 16:17:32 +02001329 else if (s->scb->flags & (SC_FL_EOS|SC_FL_ABRT_DONE)) {
Christopher Faulete05bf9e2022-03-29 15:23:40 +02001330 if ((txn->flags & TX_L7_RETRY) &&
Olivier Houcharda254a372019-04-05 15:30:12 +02001331 (s->be->retry_type & PR_RE_DISCONNECTED)) {
Willy Tarreau7cb9e6c2022-05-17 19:40:40 +02001332 if (co_data(rep) || do_l7_retry(s, s->scb) == 0) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001333 DBG_TRACE_DEVEL("leaving on L7 retry",
1334 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharda254a372019-04-05 15:30:12 +02001335 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001336 }
Olivier Houcharda254a372019-04-05 15:30:12 +02001337 }
1338
Christopher Fauletca023882024-04-09 08:19:01 +02001339 if (s->flags & SF_SRV_REUSED)
Olivier Houchard6db16992019-05-17 15:40:49 +02001340 goto abort_keep_alive;
1341
Willy Tarreau4781b152021-04-06 13:53:36 +02001342 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001343 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001344 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001345 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001346 }
1347
Christopher Faulete0768eb2018-10-03 16:38:02 +02001348 txn->status = 502;
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001349 stream_inc_http_fail_ctr(s);
Willy Tarreaucb041662022-05-17 19:44:42 +02001350 s->scb->flags |= SC_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001351 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001352
1353 if (!(s->flags & SF_ERR_MASK))
1354 s->flags |= SF_ERR_SRVCL;
Christopher Faulet4a66c942023-01-13 09:43:21 +01001355 http_set_term_flags(s);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001356 DBG_TRACE_DEVEL("leaving on error",
1357 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001358 return 0;
1359 }
1360
Christopher Faulet9768c262018-10-22 09:34:31 +02001361 /* 5: write error to client (we don't send any message then) */
Christopher Faulet2e56a732023-01-26 16:18:09 +01001362 else if (sc_ep_test(s->scf, SE_FL_ERR_PENDING)) {
Christopher Fauletca023882024-04-09 08:19:01 +02001363 if (s->flags & SF_SRV_REUSED)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001364 goto abort_keep_alive;
1365
Willy Tarreau4781b152021-04-06 13:53:36 +02001366 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001367 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001368 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001369 rep->analysers &= AN_RES_FLT_END;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001370
1371 if (!(s->flags & SF_ERR_MASK))
1372 s->flags |= SF_ERR_CLICL;
Christopher Faulet4a66c942023-01-13 09:43:21 +01001373 http_set_term_flags(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001374
1375 /* process_stream() will take care of the error */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001376 DBG_TRACE_DEVEL("leaving on error",
1377 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001378 return 0;
1379 }
1380
1381 channel_dont_close(rep);
Christopher Faulet9a790f62023-03-16 14:40:03 +01001382 s->scb->flags |= SC_FL_RCV_ONCE; /* try to get back here ASAP */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001383 DBG_TRACE_DEVEL("waiting for more data",
1384 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001385 return 0;
1386 }
1387
1388 /* More interesting part now : we know that we have a complete
1389 * response which at least looks like HTTP. We have an indicator
1390 * of each header's length, so we can parse them quickly.
1391 */
Christopher Faulet29f17582019-05-23 11:03:26 +02001392 BUG_ON(htx_get_first_type(htx) != HTX_BLK_RES_SL);
Christopher Faulet297fbb42019-05-13 14:41:27 +02001393 sl = http_get_stline(htx);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001394
Christopher Faulet1f08bff2021-05-26 13:14:39 +02001395 /* Perform a L7 retry because of the status code */
Christopher Faulete05bf9e2022-03-29 15:23:40 +02001396 if ((txn->flags & TX_L7_RETRY) &&
Christopher Faulet1f08bff2021-05-26 13:14:39 +02001397 l7_status_match(s->be, sl->info.res.status) &&
Willy Tarreau7cb9e6c2022-05-17 19:40:40 +02001398 do_l7_retry(s, s->scb) == 0) {
Christopher Faulet1f08bff2021-05-26 13:14:39 +02001399 DBG_TRACE_DEVEL("leaving on L7 retry", STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
1400 return 0;
1401 }
1402
1403 /* Now, L7 buffer is useless, it can be released */
Christopher Faulete05bf9e2022-03-29 15:23:40 +02001404 b_free(&txn->l7_buffer);
Christopher Faulet1f08bff2021-05-26 13:14:39 +02001405
1406 msg->msg_state = HTTP_MSG_BODY;
1407
1408
Christopher Faulet9768c262018-10-22 09:34:31 +02001409 /* 0: we might have to print this header in debug mode */
1410 if (unlikely((global.mode & MODE_DEBUG) &&
1411 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
1412 int32_t pos;
1413
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001414 http_debug_stline("srvrep", s, sl);
Christopher Faulet9768c262018-10-22 09:34:31 +02001415
Christopher Fauleta3f15502019-05-13 15:27:23 +02001416 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001417 struct htx_blk *blk = htx_get_blk(htx, pos);
1418 enum htx_blk_type type = htx_get_blk_type(blk);
1419
1420 if (type == HTX_BLK_EOH)
1421 break;
1422 if (type != HTX_BLK_HDR)
1423 continue;
1424
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001425 http_debug_hdr("srvhdr", s,
1426 htx_get_blk_name(htx, blk),
1427 htx_get_blk_value(htx, blk));
Christopher Faulet9768c262018-10-22 09:34:31 +02001428 }
1429 }
1430
Christopher Faulet03599112018-11-27 11:21:21 +01001431 /* 1: get the status code and the version. Also set HTTP flags */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001432 txn->status = sl->info.res.status;
Christopher Faulet03599112018-11-27 11:21:21 +01001433 if (sl->flags & HTX_SL_F_VER_11)
Christopher Faulet9768c262018-10-22 09:34:31 +02001434 msg->flags |= HTTP_MSGF_VER_11;
Christopher Faulet03599112018-11-27 11:21:21 +01001435 if (sl->flags & HTX_SL_F_XFER_LEN) {
1436 msg->flags |= HTTP_MSGF_XFER_LEN;
Christopher Faulet2a408542020-11-20 14:22:37 +01001437 if (sl->flags & HTX_SL_F_CLEN)
1438 msg->flags |= HTTP_MSGF_CNT_LEN;
1439 else if (sl->flags & HTX_SL_F_CHNK)
1440 msg->flags |= HTTP_MSGF_TE_CHNK;
Christopher Faulet03599112018-11-27 11:21:21 +01001441 }
Christopher Faulet2a408542020-11-20 14:22:37 +01001442 if (sl->flags & HTX_SL_F_BODYLESS)
1443 msg->flags |= HTTP_MSGF_BODYLESS;
Christopher Faulet576c3582021-01-08 15:53:01 +01001444 if (sl->flags & HTX_SL_F_CONN_UPG)
1445 msg->flags |= HTTP_MSGF_CONN_UPG;
Christopher Faulet9768c262018-10-22 09:34:31 +02001446
1447 n = txn->status / 100;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001448 if (n < 1 || n > 5)
1449 n = 0;
Christopher Faulet9768c262018-10-22 09:34:31 +02001450
Christopher Faulete0768eb2018-10-03 16:38:02 +02001451 /* when the client triggers a 4xx from the server, it's most often due
1452 * to a missing object or permission. These events should be tracked
1453 * because if they happen often, it may indicate a brute force or a
1454 * vulnerability scan.
1455 */
1456 if (n == 4)
1457 stream_inc_http_err_ctr(s);
1458
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001459 if (n == 5 && txn->status != 501 && txn->status != 505)
1460 stream_inc_http_fail_ctr(s);
1461
Marcin Deranek3c27dda2020-05-15 18:32:51 +02001462 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001463 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.p.http.rsp[n]);
1464 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.p.http.cum_req);
Marcin Deranek3c27dda2020-05-15 18:32:51 +02001465 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001466
Christopher Faulete0768eb2018-10-03 16:38:02 +02001467 /* Adjust server's health based on status code. Note: status codes 501
1468 * and 505 are triggered on demand by client request, so we must not
1469 * count them as server failures.
1470 */
1471 if (objt_server(s->target)) {
1472 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001473 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_OK);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001474 else
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001475 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_STS);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001476 }
1477
1478 /*
1479 * We may be facing a 100-continue response, or any other informational
1480 * 1xx response which is non-final, in which case this is not the right
1481 * response, and we're waiting for the next one. Let's allow this response
1482 * to go to the client and wait for the next one. There's an exception for
1483 * 101 which is used later in the code to switch protocols.
1484 */
1485 if (txn->status < 200 &&
1486 (txn->status == 100 || txn->status >= 102)) {
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001487 FLT_STRM_CB(s, flt_http_reset(s, msg));
Christopher Faulet421e7692019-06-13 11:16:45 +02001488 htx->first = channel_htx_fwd_headers(rep, htx);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001489 msg->msg_state = HTTP_MSG_RPBEFORE;
Christopher Faulet3499f622019-09-03 15:23:54 +02001490 msg->flags = 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001491 txn->status = 0;
1492 s->logs.t_data = -1; /* was not a response yet */
Christopher Faulet5c281d52023-03-16 15:53:28 +01001493 s->scf->flags |= SC_FL_SND_ASAP; /* Send ASAP informational messages */
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001494 goto next_one;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001495 }
1496
Christopher Faulet6e6c7b12021-01-08 16:02:05 +01001497 /* A 101-switching-protocols must contains a Connection header with the
1498 * "upgrade" option and the request too. It means both are agree to
1499 * upgrade. It is not so strict because there is no test on the Upgrade
1500 * header content. But it is probably stronger enough for now.
1501 */
1502 if (txn->status == 101 &&
1503 (!(txn->req.flags & HTTP_MSGF_CONN_UPG) || !(txn->rsp.flags & HTTP_MSGF_CONN_UPG)))
1504 goto return_bad_res;
1505
Christopher Faulete0768eb2018-10-03 16:38:02 +02001506 /*
1507 * 2: check for cacheability.
1508 */
1509
1510 switch (txn->status) {
1511 case 200:
1512 case 203:
1513 case 204:
1514 case 206:
1515 case 300:
1516 case 301:
1517 case 404:
1518 case 405:
1519 case 410:
1520 case 414:
1521 case 501:
1522 break;
1523 default:
1524 /* RFC7231#6.1:
1525 * Responses with status codes that are defined as
1526 * cacheable by default (e.g., 200, 203, 204, 206,
1527 * 300, 301, 404, 405, 410, 414, and 501 in this
1528 * specification) can be reused by a cache with
1529 * heuristic expiration unless otherwise indicated
1530 * by the method definition or explicit cache
1531 * controls [RFC7234]; all other status codes are
1532 * not cacheable by default.
1533 */
1534 txn->flags &= ~(TX_CACHEABLE | TX_CACHE_COOK);
1535 break;
1536 }
1537
1538 /*
1539 * 3: we may need to capture headers
1540 */
1541 s->logs.logwait &= ~LW_RESP;
1542 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->res_cap))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001543 http_capture_headers(htx, s->res_cap, sess->fe->rsp_cap);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001544
Christopher Faulet9768c262018-10-22 09:34:31 +02001545 /* Skip parsing if no content length is possible. */
Christopher Fauletc75668e2020-12-07 18:10:32 +01001546 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status >= 200 && txn->status < 300) ||
Christopher Faulete0768eb2018-10-03 16:38:02 +02001547 txn->status == 101)) {
1548 /* Either we've established an explicit tunnel, or we're
1549 * switching the protocol. In both cases, we're very unlikely
1550 * to understand the next protocols. We have to switch to tunnel
1551 * mode, so that we transfer the request and responses then let
1552 * this protocol pass unmodified. When we later implement specific
1553 * parsers for such protocols, we'll want to check the Upgrade
1554 * header which contains information about that protocol for
1555 * responses with status 101 (eg: see RFC2817 about TLS).
1556 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02001557 txn->flags |= TX_CON_WANT_TUN;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001558 }
1559
Amaury Denoyelleab6b0742024-03-20 09:25:03 +01001560 /* Check for NTML authentication headers in 401 (WWW-Authenticate) and
1561 * 407 (Proxy-Authenticate) responses and set the connection to
1562 * private.
1563 *
1564 * Note that this is not performed when using a true multiplexer unless
1565 * connection is already attached to the session as nothing prevents it
1566 * from being shared already by several sessions here.
Christopher Faulet61608322018-11-23 16:23:45 +01001567 */
Willy Tarreaufd9417b2022-05-18 16:23:22 +02001568 srv_conn = sc_conn(s->scb);
Amaury Denoyelleab6b0742024-03-20 09:25:03 +01001569 if (srv_conn &&
1570 (LIST_INLIST(&srv_conn->sess_el) || strcmp(srv_conn->mux->name, "H1") == 0)) {
Christopher Faulet61608322018-11-23 16:23:45 +01001571 struct ist hdr;
1572 struct http_hdr_ctx ctx;
1573
1574 if (txn->status == 401)
1575 hdr = ist("WWW-Authenticate");
1576 else if (txn->status == 407)
1577 hdr = ist("Proxy-Authenticate");
1578 else
1579 goto end;
1580
1581 ctx.blk = NULL;
1582 while (http_find_header(htx, hdr, &ctx, 0)) {
Willy Tarreauf1dcced2020-05-07 19:27:02 +02001583 /* If www-authenticate contains "Negotiate", "Nego2", or "NTLM",
1584 * possibly followed by blanks and a base64 string, the connection
1585 * is private. Since it's a mess to deal with, we only check for
1586 * values starting with "NTLM" or "Nego". Note that often multiple
1587 * headers are sent by the server there.
1588 */
1589 if ((ctx.value.len >= 4 && strncasecmp(ctx.value.ptr, "Nego", 4) == 0) ||
Willy Tarreau49a1d282020-05-07 19:10:15 +02001590 (ctx.value.len >= 4 && strncasecmp(ctx.value.ptr, "NTLM", 4) == 0)) {
Olivier Houchard250031e2019-05-29 15:01:50 +02001591 sess->flags |= SESS_FL_PREFER_LAST;
Christopher Faulet08016ab2020-07-01 16:10:06 +02001592 conn_set_owner(srv_conn, sess, NULL);
Christopher Faulet21ddc742020-07-01 15:26:14 +02001593 conn_set_private(srv_conn);
Ilya Shipitsin6b79f382020-07-23 00:32:55 +05001594 /* If it fail now, the same will be done in mux->detach() callback */
Christopher Faulet08016ab2020-07-01 16:10:06 +02001595 session_add_conn(srv_conn->owner, srv_conn, srv_conn->target);
Willy Tarreauf1dcced2020-05-07 19:27:02 +02001596 break;
Olivier Houchard250031e2019-05-29 15:01:50 +02001597 }
Christopher Faulet61608322018-11-23 16:23:45 +01001598 }
1599 }
1600
1601 end:
Christopher Faulete0768eb2018-10-03 16:38:02 +02001602 /* we want to have the response time before we start processing it */
Willy Tarreau69530f52023-04-28 09:16:15 +02001603 s->logs.t_data = ns_to_ms(now_ns - s->logs.accept_ts);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001604
1605 /* end of job, return OK */
1606 rep->analysers &= ~an_bit;
1607 rep->analyse_exp = TICK_ETERNITY;
1608 channel_auto_close(rep);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001609 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001610 return 1;
1611
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001612 return_int_err:
Willy Tarreau4781b152021-04-06 13:53:36 +02001613 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
1614 _HA_ATOMIC_INC(&s->be->be_counters.internal_errors);
William Lallemand36119de2021-03-08 15:26:48 +01001615 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001616 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Fauletb8a53712019-12-16 11:29:38 +01001617 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001618 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.internal_errors);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001619 txn->status = 500;
1620 if (!(s->flags & SF_ERR_MASK))
1621 s->flags |= SF_ERR_INTERNAL;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001622 goto return_prx_cond;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001623
1624 return_bad_res:
Willy Tarreau4781b152021-04-06 13:53:36 +02001625 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Faulet47365272018-10-31 17:40:50 +01001626 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001627 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001628 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Christopher Faulet47365272018-10-31 17:40:50 +01001629 }
Olivier Houcharde3249a92019-05-03 23:01:47 +02001630 if ((s->be->retry_type & PR_RE_JUNK_REQUEST) &&
Christopher Faulete05bf9e2022-03-29 15:23:40 +02001631 (txn->flags & TX_L7_RETRY) &&
Willy Tarreau7cb9e6c2022-05-17 19:40:40 +02001632 do_l7_retry(s, s->scb) == 0) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001633 DBG_TRACE_DEVEL("leaving on L7 retry",
1634 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharde3249a92019-05-03 23:01:47 +02001635 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001636 }
Christopher Faulet47365272018-10-31 17:40:50 +01001637 txn->status = 502;
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001638 stream_inc_http_fail_ctr(s);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001639 /* fall through */
1640
Christopher Fauletb8a53712019-12-16 11:29:38 +01001641 return_prx_cond:
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001642 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet4a66c942023-01-13 09:43:21 +01001643 http_set_term_flags(s);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001644
Willy Tarreaucb041662022-05-17 19:44:42 +02001645 s->scb->flags |= SC_FL_NOLINGER;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001646 DBG_TRACE_DEVEL("leaving on error",
1647 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulet47365272018-10-31 17:40:50 +01001648 return 0;
1649
Christopher Faulete0768eb2018-10-03 16:38:02 +02001650 abort_keep_alive:
1651 /* A keep-alive request to the server failed on a network error.
1652 * The client is required to retry. We need to close without returning
1653 * any other information so that the client retries.
1654 */
1655 txn->status = 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001656 s->logs.logwait = 0;
1657 s->logs.level = 0;
Christopher Faulet84d3ef92023-03-17 15:45:58 +01001658 s->scf->flags &= ~SC_FL_SND_EXP_MORE; /* speed up sending a previous response */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001659 http_reply_and_close(s, txn->status, NULL);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001660 DBG_TRACE_DEVEL("leaving by closing K/A connection",
1661 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001662 return 0;
1663}
1664
1665/* This function performs all the processing enabled for the current response.
1666 * It normally returns 1 unless it wants to break. It relies on buffers flags,
1667 * and updates s->res.analysers. It might make sense to explode it into several
1668 * other functions. It works like process_request (see indications above).
1669 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001670int http_process_res_common(struct stream *s, struct channel *rep, int an_bit, struct proxy *px)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001671{
1672 struct session *sess = s->sess;
1673 struct http_txn *txn = s->txn;
1674 struct http_msg *msg = &txn->rsp;
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001675 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001676 struct proxy *cur_proxy;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001677 enum rule_result ret = HTTP_RULE_RES_CONT;
1678
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001679 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
1680 return 0;
Christopher Faulet9768c262018-10-22 09:34:31 +02001681
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001682 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001683
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001684 htx = htxbuf(&rep->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001685
1686 /* The stats applet needs to adjust the Connection header but we don't
1687 * apply any filter there.
1688 */
1689 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
1690 rep->analysers &= ~an_bit;
1691 rep->analyse_exp = TICK_ETERNITY;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001692 goto end;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001693 }
1694
1695 /*
1696 * We will have to evaluate the filters.
1697 * As opposed to version 1.2, now they will be evaluated in the
1698 * filters order and not in the header order. This means that
1699 * each filter has to be validated among all headers.
1700 *
1701 * Filters are tried with ->be first, then with ->fe if it is
1702 * different from ->be.
1703 *
1704 * Maybe we are in resume condiion. In this case I choose the
1705 * "struct proxy" which contains the rule list matching the resume
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05001706 * pointer. If none of these "struct proxy" match, I initialise
Christopher Faulete0768eb2018-10-03 16:38:02 +02001707 * the process with the first one.
1708 *
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05001709 * In fact, I check only correspondence between the current list
Christopher Faulete0768eb2018-10-03 16:38:02 +02001710 * pointer and the ->fe rule list. If it doesn't match, I initialize
1711 * the loop with the ->be.
1712 */
Christopher Fauletd4150ad2021-10-13 15:35:55 +02001713 if (s->current_rule_list == &sess->fe->http_res_rules ||
1714 (sess->fe->defpx && s->current_rule_list == &sess->fe->defpx->http_res_rules))
Christopher Faulete0768eb2018-10-03 16:38:02 +02001715 cur_proxy = sess->fe;
1716 else
1717 cur_proxy = s->be;
Christopher Fauletd4150ad2021-10-13 15:35:55 +02001718
Christopher Faulete0768eb2018-10-03 16:38:02 +02001719 while (1) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001720 /* evaluate http-response rules */
Christopher Faulet46f46df2021-11-09 16:33:25 +01001721 if (ret == HTTP_RULE_RES_CONT || ret == HTTP_RULE_RES_STOP) {
Christopher Fauletd4150ad2021-10-13 15:35:55 +02001722 struct list *def_rules, *rules;
1723
1724 def_rules = ((cur_proxy->defpx && (cur_proxy == s->be || cur_proxy->defpx != s->be->defpx)) ? &cur_proxy->defpx->http_res_rules : NULL);
1725 rules = &cur_proxy->http_res_rules;
1726
1727 ret = http_res_get_intercept_rule(cur_proxy, def_rules, rules, s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001728
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001729 switch (ret) {
1730 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
1731 goto return_prx_yield;
1732
1733 case HTTP_RULE_RES_CONT:
1734 case HTTP_RULE_RES_STOP: /* nothing to do */
1735 break;
1736
1737 case HTTP_RULE_RES_DENY: /* deny or tarpit */
1738 goto deny;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001739
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001740 case HTTP_RULE_RES_ABRT: /* abort request, response already sent */
1741 goto return_prx_cond;
1742
1743 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
Christopher Fauletb8a53712019-12-16 11:29:38 +01001744 goto done;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001745
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001746 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
1747 goto return_bad_res;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001748
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001749 case HTTP_RULE_RES_ERROR: /* failed with a bad request */
1750 goto return_int_err;
1751 }
1752
1753 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001754
Christopher Faulete0768eb2018-10-03 16:38:02 +02001755 /* check whether we're already working on the frontend */
1756 if (cur_proxy == sess->fe)
1757 break;
1758 cur_proxy = sess->fe;
1759 }
1760
Christopher Faulete0768eb2018-10-03 16:38:02 +02001761 /* OK that's all we can do for 1xx responses */
1762 if (unlikely(txn->status < 200 && txn->status != 101))
Christopher Fauletf2824e62018-10-01 12:12:37 +02001763 goto end;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001764
1765 /*
1766 * Now check for a server cookie.
1767 */
1768 if (s->be->cookie_name || sess->fe->capture_name || (s->be->options & PR_O_CHK_CACHE))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001769 http_manage_server_side_cookies(s, rep);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001770
1771 /*
1772 * Check for cache-control or pragma headers if required.
1773 */
1774 if ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001775 http_check_response_for_cacheability(s, rep);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001776
1777 /*
1778 * Add server cookie in the response if needed
1779 */
1780 if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) &&
1781 !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) &&
1782 (!(s->flags & SF_DIRECT) ||
1783 ((s->be->cookie_maxidle || txn->cookie_last_date) &&
1784 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
1785 (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
1786 (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
1787 (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
1788 !(s->flags & SF_IGNORE_PRST)) {
1789 /* the server is known, it's not the one the client requested, or the
1790 * cookie's last seen date needs to be refreshed. We have to
1791 * insert a set-cookie here, except if we want to insert only on POST
1792 * requests and this one isn't. Note that servers which don't have cookies
1793 * (eg: some backup servers) will return a full cookie removal request.
1794 */
Willy Tarreau88bc8002021-12-06 07:01:02 +00001795 if (!__objt_server(s->target)->cookie) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001796 chunk_printf(&trash,
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001797 "%s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
Christopher Faulete0768eb2018-10-03 16:38:02 +02001798 s->be->cookie_name);
1799 }
1800 else {
Willy Tarreau88bc8002021-12-06 07:01:02 +00001801 chunk_printf(&trash, "%s=%s", s->be->cookie_name, __objt_server(s->target)->cookie);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001802
1803 if (s->be->cookie_maxidle || s->be->cookie_maxlife) {
1804 /* emit last_date, which is mandatory */
1805 trash.area[trash.data++] = COOKIE_DELIM_DATE;
1806 s30tob64((date.tv_sec+3) >> 2,
1807 trash.area + trash.data);
1808 trash.data += 5;
1809
1810 if (s->be->cookie_maxlife) {
1811 /* emit first_date, which is either the original one or
1812 * the current date.
1813 */
1814 trash.area[trash.data++] = COOKIE_DELIM_DATE;
1815 s30tob64(txn->cookie_first_date ?
1816 txn->cookie_first_date >> 2 :
1817 (date.tv_sec+3) >> 2,
1818 trash.area + trash.data);
1819 trash.data += 5;
1820 }
1821 }
1822 chunk_appendf(&trash, "; path=/");
1823 }
1824
1825 if (s->be->cookie_domain)
1826 chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain);
1827
1828 if (s->be->ck_opts & PR_CK_HTTPONLY)
1829 chunk_appendf(&trash, "; HttpOnly");
1830
1831 if (s->be->ck_opts & PR_CK_SECURE)
1832 chunk_appendf(&trash, "; Secure");
1833
Christopher Faulet2f533902020-01-21 11:06:48 +01001834 if (s->be->cookie_attrs)
1835 chunk_appendf(&trash, "; %s", s->be->cookie_attrs);
1836
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001837 if (unlikely(!http_add_header(htx, ist("Set-Cookie"), ist2(trash.area, trash.data))))
Christopher Fauletd649b572022-06-01 17:42:35 +02001838 goto return_fail_rewrite;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001839
1840 txn->flags &= ~TX_SCK_MASK;
1841 if (__objt_server(s->target)->cookie && (s->flags & SF_DIRECT))
1842 /* the server did not change, only the date was updated */
1843 txn->flags |= TX_SCK_UPDATED;
1844 else
1845 txn->flags |= TX_SCK_INSERTED;
1846
1847 /* Here, we will tell an eventual cache on the client side that we don't
1848 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
1849 * Some caches understand the correct form: 'no-cache="set-cookie"', but
1850 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
1851 */
1852 if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
1853
1854 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
1855
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001856 if (unlikely(!http_add_header(htx, ist("Cache-control"), ist("private"))))
Christopher Fauletd649b572022-06-01 17:42:35 +02001857 goto return_fail_rewrite;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001858 }
1859 }
1860
1861 /*
1862 * Check if result will be cacheable with a cookie.
1863 * We'll block the response if security checks have caught
1864 * nasty things such as a cacheable cookie.
1865 */
1866 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
1867 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
1868 (s->be->options & PR_O_CHK_CACHE)) {
1869 /* we're in presence of a cacheable response containing
1870 * a set-cookie header. We'll block it as requested by
1871 * the 'checkcache' option, and send an alert.
1872 */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001873 ha_alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreau88bc8002021-12-06 07:01:02 +00001874 s->be->id, objt_server(s->target) ? __objt_server(s->target)->id : "<dispatch>");
Christopher Faulete0768eb2018-10-03 16:38:02 +02001875 send_log(s->be, LOG_ALERT,
1876 "Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreau88bc8002021-12-06 07:01:02 +00001877 s->be->id, objt_server(s->target) ? __objt_server(s->target)->id : "<dispatch>");
Christopher Fauletb8a53712019-12-16 11:29:38 +01001878 goto deny;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001879 }
1880
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001881 end:
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01001882 /*
1883 * Evaluate after-response rules before forwarding the response. rules
1884 * from the backend are evaluated first, then one from the frontend if
1885 * it differs.
1886 */
1887 if (!http_eval_after_res_rules(s))
1888 goto return_int_err;
1889
Christopher Fauletc2ac5e42021-03-08 18:20:09 +01001890 /* Filter the response headers if there are filters attached to the
1891 * stream.
1892 */
1893 if (HAS_FILTERS(s))
1894 rep->analysers |= AN_RES_FLT_HTTP_HDRS;
1895
Christopher Faulete0768eb2018-10-03 16:38:02 +02001896 /* Always enter in the body analyzer */
1897 rep->analysers &= ~AN_RES_FLT_XFER_DATA;
1898 rep->analysers |= AN_RES_HTTP_XFER_BODY;
1899
1900 /* if the user wants to log as soon as possible, without counting
1901 * bytes from the server, then this is the right moment. We have
1902 * to temporarily assign bytes_out to log what we currently have.
1903 */
1904 if (!LIST_ISEMPTY(&sess->fe->logformat) && !(s->logs.logwait & LW_BYTES)) {
1905 s->logs.t_close = s->logs.t_data; /* to get a valid end date */
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001906 s->logs.bytes_out = htx->data;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001907 s->do_log(s);
1908 s->logs.bytes_out = 0;
1909 }
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001910
Christopher Fauletb8a53712019-12-16 11:29:38 +01001911 done:
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01001912 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletb8a53712019-12-16 11:29:38 +01001913 rep->analysers &= ~an_bit;
1914 rep->analyse_exp = TICK_ETERNITY;
Christopher Fauletd4150ad2021-10-13 15:35:55 +02001915 s->current_rule = s->current_rule_list = NULL;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001916 return 1;
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001917
Christopher Fauletb8a53712019-12-16 11:29:38 +01001918 deny:
Willy Tarreau4781b152021-04-06 13:53:36 +02001919 _HA_ATOMIC_INC(&sess->fe->fe_counters.denied_resp);
1920 _HA_ATOMIC_INC(&s->be->be_counters.denied_resp);
William Lallemand36119de2021-03-08 15:26:48 +01001921 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001922 _HA_ATOMIC_INC(&sess->listener->counters->denied_resp);
Christopher Fauleta08546b2019-12-16 16:07:34 +01001923 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001924 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.denied_resp);
Christopher Fauletb8a53712019-12-16 11:29:38 +01001925 goto return_prx_err;
1926
Christopher Fauletd649b572022-06-01 17:42:35 +02001927 return_fail_rewrite:
1928 if (!(s->flags & SF_ERR_MASK))
1929 s->flags |= SF_ERR_PRXCOND;
1930 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_rewrites);
1931 _HA_ATOMIC_INC(&s->be->be_counters.failed_rewrites);
1932 if (sess->listener && sess->listener->counters)
1933 _HA_ATOMIC_INC(&sess->listener->counters->failed_rewrites);
1934 if (objt_server(s->target))
1935 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_rewrites);
1936 /* fall through */
1937
Christopher Fauletb8a53712019-12-16 11:29:38 +01001938 return_int_err:
1939 txn->status = 500;
1940 if (!(s->flags & SF_ERR_MASK))
1941 s->flags |= SF_ERR_INTERNAL;
Willy Tarreau4781b152021-04-06 13:53:36 +02001942 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
1943 _HA_ATOMIC_INC(&s->be->be_counters.internal_errors);
Dragan Dosen9a006f92021-09-21 13:02:09 +02001944 if (sess->listener && sess->listener->counters)
1945 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001946 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001947 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.internal_errors);
Christopher Fauletb8a53712019-12-16 11:29:38 +01001948 goto return_prx_err;
1949
1950 return_bad_res:
1951 txn->status = 502;
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001952 stream_inc_http_fail_ctr(s);
Willy Tarreau4781b152021-04-06 13:53:36 +02001953 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Fauleta20a6532020-02-05 10:16:41 +01001954 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001955 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Christopher Fauleta20a6532020-02-05 10:16:41 +01001956 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_RSP);
1957 }
Christopher Fauletb8a53712019-12-16 11:29:38 +01001958 /* fall through */
1959
1960 return_prx_err:
1961 http_reply_and_close(s, txn->status, http_error_message(s));
1962 /* fall through */
1963
1964 return_prx_cond:
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001965 s->logs.t_data = -1; /* was not a valid response */
Willy Tarreaucb041662022-05-17 19:44:42 +02001966 s->scb->flags |= SC_FL_NOLINGER;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001967
Christopher Faulet4a66c942023-01-13 09:43:21 +01001968 http_set_term_flags(s);
Christopher Fauletb8a53712019-12-16 11:29:38 +01001969
Christopher Faulete58c0002020-03-02 16:21:01 +01001970 rep->analysers &= AN_RES_FLT_END;
1971 s->req.analysers &= AN_REQ_FLT_END;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001972 rep->analyse_exp = TICK_ETERNITY;
Christopher Fauletd4150ad2021-10-13 15:35:55 +02001973 s->current_rule = s->current_rule_list = NULL;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001974 DBG_TRACE_DEVEL("leaving on error",
1975 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001976 return 0;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001977
1978 return_prx_yield:
1979 channel_dont_close(rep);
1980 DBG_TRACE_DEVEL("waiting for more data",
1981 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
1982 return 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001983}
1984
1985/* This function is an analyser which forwards response body (including chunk
1986 * sizes if any). It is called as soon as we must forward, even if we forward
1987 * zero byte. The only situation where it must not be called is when we're in
1988 * tunnel mode and we want to forward till the close. It's used both to forward
1989 * remaining data and to resync after end of body. It expects the msg_state to
1990 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
1991 * read more data, or 1 once we can go on with next request or end the stream.
1992 *
1993 * It is capable of compressing response data both in content-length mode and
1994 * in chunked mode. The state machines follows different flows depending on
1995 * whether content-length and chunked modes are used, since there are no
1996 * trailers in content-length :
1997 *
1998 * chk-mode cl-mode
1999 * ,----- BODY -----.
2000 * / \
2001 * V size > 0 V chk-mode
2002 * .--> SIZE -------------> DATA -------------> CRLF
2003 * | | size == 0 | last byte |
2004 * | v final crlf v inspected |
2005 * | TRAILERS -----------> DONE |
2006 * | |
2007 * `----------------------------------------------'
2008 *
2009 * Compression only happens in the DATA state, and must be flushed in final
2010 * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding
2011 * is performed at once on final states for all bytes parsed, or when leaving
2012 * on missing data.
2013 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002014int http_response_forward_body(struct stream *s, struct channel *res, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +02002015{
2016 struct session *sess = s->sess;
2017 struct http_txn *txn = s->txn;
2018 struct http_msg *msg = &s->txn->rsp;
Christopher Faulet9768c262018-10-22 09:34:31 +02002019 struct htx *htx;
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002020 int ret;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002021
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002022 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002023
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002024 htx = htxbuf(&res->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002025
Christopher Fauletb9a92f32019-09-09 10:15:21 +02002026 if (htx->flags & HTX_FL_PARSING_ERROR)
2027 goto return_bad_res;
2028 if (htx->flags & HTX_FL_PROCESSING_ERROR)
2029 goto return_int_err;
2030
Christopher Faulet9768c262018-10-22 09:34:31 +02002031 if (msg->msg_state == HTTP_MSG_BODY)
2032 msg->msg_state = HTTP_MSG_DATA;
2033
Christopher Faulete0768eb2018-10-03 16:38:02 +02002034 /* in most states, we should abort in case of early close */
2035 channel_auto_close(res);
2036
Christopher Faulete0768eb2018-10-03 16:38:02 +02002037 if (res->to_forward) {
Christopher Faulet66af0b22019-03-22 14:54:52 +01002038 if (res->to_forward == CHN_INFINITE_FORWARD) {
Christopher Faulet904763f2023-03-22 14:53:11 +01002039 if (s->scb->flags & SC_FL_EOI)
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002040 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet66af0b22019-03-22 14:54:52 +01002041 }
2042 else {
2043 /* We can't process the buffer's contents yet */
2044 res->flags |= CF_WAKE_WRITE;
2045 goto missing_data_or_waiting;
2046 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02002047 }
2048
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002049 if (msg->msg_state >= HTTP_MSG_ENDING)
2050 goto ending;
2051
Christopher Fauletc75668e2020-12-07 18:10:32 +01002052 if ((txn->meth == HTTP_METH_CONNECT && txn->status >= 200 && txn->status < 300) || txn->status == 101 ||
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002053 (!(msg->flags & HTTP_MSGF_XFER_LEN) && !HAS_RSP_DATA_FILTERS(s))) {
2054 msg->msg_state = HTTP_MSG_ENDING;
2055 goto ending;
2056 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002057
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002058 /* Forward input data. We get it by removing all outgoing data not
2059 * forwarded yet from HTX data size. If there are some data filters, we
2060 * let them decide the amount of data to forward.
Christopher Faulet9768c262018-10-22 09:34:31 +02002061 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002062 if (HAS_RSP_DATA_FILTERS(s)) {
2063 ret = flt_http_payload(s, msg, htx->data);
2064 if (ret < 0)
2065 goto return_bad_res;
Christopher Faulet421e7692019-06-13 11:16:45 +02002066 c_adv(res, ret);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002067 }
2068 else {
Christopher Faulet421e7692019-06-13 11:16:45 +02002069 c_adv(res, htx->data - co_data(res));
Christopher Faulet2f7c82b2023-02-20 14:06:52 +01002070 if ((global.tune.options & GTUNE_USE_FAST_FWD) && (msg->flags & HTTP_MSGF_XFER_LEN))
Christopher Faulet66af0b22019-03-22 14:54:52 +01002071 channel_htx_forward_forever(res, htx);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002072 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002073
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002074 if (htx->data != co_data(res))
2075 goto missing_data_or_waiting;
2076
Christopher Fauletca5309a2023-04-17 16:17:32 +02002077 if (!(msg->flags & HTTP_MSGF_XFER_LEN) && (s->scb->flags & (SC_FL_EOS|SC_FL_ABRT_DONE))) {
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002078 msg->msg_state = HTTP_MSG_ENDING;
2079 goto ending;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002080 }
2081
Christopher Faulet9768c262018-10-22 09:34:31 +02002082 /* Check if the end-of-message is reached and if so, switch the message
Christopher Fauletd20fdb02019-06-13 16:43:22 +02002083 * in HTTP_MSG_ENDING state. Then if all data was marked to be
2084 * forwarded, set the state to HTTP_MSG_DONE.
Christopher Faulet9768c262018-10-22 09:34:31 +02002085 */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002086 if (!(htx->flags & HTX_FL_EOM))
Christopher Faulet9768c262018-10-22 09:34:31 +02002087 goto missing_data_or_waiting;
2088
Christopher Fauletd20fdb02019-06-13 16:43:22 +02002089 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet9768c262018-10-22 09:34:31 +02002090
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002091 ending:
Christopher Faulet84d3ef92023-03-17 15:45:58 +01002092 s->scf->flags &= ~SC_FL_SND_EXP_MORE; /* no more data are expected to be sent */
Christopher Faulet2151cdd2020-07-22 16:34:59 +02002093
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002094 /* other states, ENDING...TUNNEL */
2095 if (msg->msg_state >= HTTP_MSG_DONE)
2096 goto done;
Christopher Faulet9768c262018-10-22 09:34:31 +02002097
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002098 if (HAS_RSP_DATA_FILTERS(s)) {
2099 ret = flt_http_end(s, msg);
2100 if (ret <= 0) {
2101 if (!ret)
2102 goto missing_data_or_waiting;
2103 goto return_bad_res;
2104 }
2105 }
2106
Christopher Faulet97047972023-04-17 08:52:10 +02002107 if (!(txn->flags & TX_CON_WANT_TUN) && !(msg->flags & HTTP_MSGF_XFER_LEN)) {
2108 /* One-side tunnel */
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002109 msg->msg_state = HTTP_MSG_TUNNEL;
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002110 }
2111 else {
2112 msg->msg_state = HTTP_MSG_DONE;
2113 res->to_forward = 0;
2114 }
2115
2116 done:
2117
2118 channel_dont_close(res);
2119
Christopher Faulet64350bb2023-04-13 16:37:37 +02002120 if ((s->scf->flags & SC_FL_SHUT_DONE) && co_data(res)) {
Christopher Fauletf2b02cf2023-01-13 11:02:28 +01002121 /* response errors are most likely due to the client aborting
2122 * the transfer. */
2123 goto return_cli_abort;
2124 }
2125
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002126 http_end_response(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002127 if (!(res->analysers & an_bit)) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002128 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002129 return 1;
2130 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002131 DBG_TRACE_DEVEL("waiting for the end of the HTTP txn",
2132 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002133 return 0;
2134
2135 missing_data_or_waiting:
Christopher Faulet64350bb2023-04-13 16:37:37 +02002136 if (s->scf->flags & SC_FL_SHUT_DONE)
Christopher Faulet93e02d82019-03-08 14:18:50 +01002137 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002138
2139 /* stop waiting for data if the input is closed before the end. If the
2140 * client side was already closed, it means that the client has aborted,
2141 * so we don't want to count this as a server abort. Otherwise it's a
2142 * server abort.
2143 */
Christopher Fauletca5309a2023-04-17 16:17:32 +02002144 if (msg->msg_state < HTTP_MSG_ENDING && (s->scb->flags & (SC_FL_EOS|SC_FL_ABRT_DONE))) {
2145 if ((s->scf->flags & (SC_FL_EOS|SC_FL_ABRT_DONE)) &&
Christopher Faulet64350bb2023-04-13 16:37:37 +02002146 (s->scb->flags & SC_FL_SHUT_DONE))
Christopher Faulet93e02d82019-03-08 14:18:50 +01002147 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002148 /* If we have some pending data, we continue the processing */
Christopher Faulet93e02d82019-03-08 14:18:50 +01002149 if (htx_is_empty(htx))
2150 goto return_srv_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002151 }
2152
Christopher Faulete0768eb2018-10-03 16:38:02 +02002153 /* When TE: chunked is used, we need to get there again to parse
2154 * remaining chunks even if the server has closed, so we don't want to
Christopher Faulet9768c262018-10-22 09:34:31 +02002155 * set CF_DONTCLOSE. Similarly when there is a content-leng or if there
2156 * are filters registered on the stream, we don't want to forward a
2157 * close
Christopher Faulete0768eb2018-10-03 16:38:02 +02002158 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002159 if ((msg->flags & HTTP_MSGF_XFER_LEN) || HAS_RSP_DATA_FILTERS(s))
Christopher Faulete0768eb2018-10-03 16:38:02 +02002160 channel_dont_close(res);
2161
2162 /* We know that more data are expected, but we couldn't send more that
Christopher Faulet84d3ef92023-03-17 15:45:58 +01002163 * what we did. So we always set the SC_FL_SND_EXP_MORE flag so that the
Christopher Faulete0768eb2018-10-03 16:38:02 +02002164 * system knows it must not set a PUSH on this first part. Interactive
2165 * modes are already handled by the stream sock layer. We must not do
2166 * this in content-length mode because it could present the MSG_MORE
2167 * flag with the last block of forwarded data, which would cause an
2168 * additional delay to be observed by the receiver.
2169 */
Christopher Faulet2151cdd2020-07-22 16:34:59 +02002170 if (HAS_RSP_DATA_FILTERS(s))
Christopher Faulet84d3ef92023-03-17 15:45:58 +01002171 s->scf->flags |= SC_FL_SND_EXP_MORE;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002172
2173 /* the stream handler will take care of timeouts and errors */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002174 DBG_TRACE_DEVEL("waiting for more data to forward",
2175 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002176 return 0;
2177
Christopher Faulet93e02d82019-03-08 14:18:50 +01002178 return_srv_abort:
Willy Tarreau4781b152021-04-06 13:53:36 +02002179 _HA_ATOMIC_INC(&sess->fe->fe_counters.srv_aborts);
2180 _HA_ATOMIC_INC(&s->be->be_counters.srv_aborts);
William Lallemand36119de2021-03-08 15:26:48 +01002181 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02002182 _HA_ATOMIC_INC(&sess->listener->counters->srv_aborts);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002183 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02002184 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.srv_aborts);
Willy Tarreau826f3ab2021-02-10 12:07:15 +01002185 stream_inc_http_fail_ctr(s);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002186 if (!(s->flags & SF_ERR_MASK))
Christopher Fauletf2b02cf2023-01-13 11:02:28 +01002187 s->flags |= ((res->flags & CF_READ_TIMEOUT) ? SF_ERR_SRVTO : SF_ERR_SRVCL);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002188 goto return_error;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002189
Christopher Faulet93e02d82019-03-08 14:18:50 +01002190 return_cli_abort:
Willy Tarreau4781b152021-04-06 13:53:36 +02002191 _HA_ATOMIC_INC(&sess->fe->fe_counters.cli_aborts);
2192 _HA_ATOMIC_INC(&s->be->be_counters.cli_aborts);
William Lallemand36119de2021-03-08 15:26:48 +01002193 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02002194 _HA_ATOMIC_INC(&sess->listener->counters->cli_aborts);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002195 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02002196 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.cli_aborts);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002197 if (!(s->flags & SF_ERR_MASK))
Christopher Fauletf2b02cf2023-01-13 11:02:28 +01002198 s->flags |= ((res->flags & CF_WRITE_TIMEOUT) ? SF_ERR_CLITO : SF_ERR_CLICL);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002199 goto return_error;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002200
Christopher Fauletb9a92f32019-09-09 10:15:21 +02002201 return_int_err:
Willy Tarreau4781b152021-04-06 13:53:36 +02002202 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
2203 _HA_ATOMIC_INC(&s->be->be_counters.internal_errors);
William Lallemand36119de2021-03-08 15:26:48 +01002204 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02002205 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002206 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02002207 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.internal_errors);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02002208 if (!(s->flags & SF_ERR_MASK))
2209 s->flags |= SF_ERR_INTERNAL;
2210 goto return_error;
2211
Christopher Faulet93e02d82019-03-08 14:18:50 +01002212 return_bad_res:
Willy Tarreau4781b152021-04-06 13:53:36 +02002213 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002214 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02002215 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002216 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_RSP);
2217 }
Willy Tarreau826f3ab2021-02-10 12:07:15 +01002218 stream_inc_http_fail_ctr(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002219 if (!(s->flags & SF_ERR_MASK))
Christopher Faulet93e02d82019-03-08 14:18:50 +01002220 s->flags |= SF_ERR_SRVCL;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002221 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +02002222
Christopher Faulet93e02d82019-03-08 14:18:50 +01002223 return_error:
Christopher Faulete0768eb2018-10-03 16:38:02 +02002224 /* don't send any error message as we're in the body */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002225 http_reply_and_close(s, txn->status, NULL);
Christopher Fauletf2b02cf2023-01-13 11:02:28 +01002226 http_set_term_flags(s);
2227 stream_inc_http_fail_ctr(s);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002228 DBG_TRACE_DEVEL("leaving on error",
2229 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002230 return 0;
2231}
2232
Christopher Fauletf2824e62018-10-01 12:12:37 +02002233/* Perform an HTTP redirect based on the information in <rule>. The function
Willy Tarreaubc1223b2021-09-02 16:54:33 +02002234 * returns zero in case of an irrecoverable error such as too large a request
2235 * to build a valid response, 1 in case of successful redirect (hence the rule
2236 * is final), or 2 if the rule has to be silently skipped.
Christopher Fauletf2824e62018-10-01 12:12:37 +02002237 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002238int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn)
Christopher Fauletf2824e62018-10-01 12:12:37 +02002239{
Christopher Faulet99daf282018-11-28 22:58:13 +01002240 struct channel *req = &s->req;
2241 struct channel *res = &s->res;
2242 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01002243 struct htx_sl *sl;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002244 struct buffer *chunk;
Christopher Faulet99daf282018-11-28 22:58:13 +01002245 struct ist status, reason, location;
2246 unsigned int flags;
Christopher Fauleteab17572022-04-26 20:34:38 +02002247 int ret = 1, close = 0; /* Try to keep the connection alive byt default */
Christopher Fauletf2824e62018-10-01 12:12:37 +02002248
2249 chunk = alloc_trash_chunk();
Christopher Fauletb8a53712019-12-16 11:29:38 +01002250 if (!chunk) {
2251 if (!(s->flags & SF_ERR_MASK))
2252 s->flags |= SF_ERR_RESOURCE;
Christopher Faulet99daf282018-11-28 22:58:13 +01002253 goto fail;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002254 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002255
Christopher Faulet99daf282018-11-28 22:58:13 +01002256 /*
2257 * Create the location
2258 */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002259 htx = htxbuf(&req->buf);
Christopher Fauletf2824e62018-10-01 12:12:37 +02002260 switch(rule->type) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002261 case REDIRECT_TYPE_SCHEME: {
2262 struct http_hdr_ctx ctx;
2263 struct ist path, host;
Amaury Denoyellec453f952021-07-06 11:40:12 +02002264 struct http_uri_parser parser;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002265
Christopher Faulet99daf282018-11-28 22:58:13 +01002266 host = ist("");
2267 ctx.blk = NULL;
2268 if (http_find_header(htx, ist("Host"), &ctx, 0))
2269 host = ctx.value;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002270
Christopher Faulet297fbb42019-05-13 14:41:27 +02002271 sl = http_get_stline(htx);
Amaury Denoyellec453f952021-07-06 11:40:12 +02002272 parser = http_uri_parser_init(htx_sl_req_uri(sl));
2273 path = http_parse_path(&parser);
Christopher Faulet99daf282018-11-28 22:58:13 +01002274 /* build message using path */
Tim Duesterhused526372020-03-05 17:56:33 +01002275 if (isttest(path)) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002276 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2277 int qs = 0;
2278 while (qs < path.len) {
2279 if (*(path.ptr + qs) == '?') {
2280 path.len = qs;
2281 break;
2282 }
2283 qs++;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002284 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002285 }
2286 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002287 else
2288 path = ist("/");
Christopher Fauletf2824e62018-10-01 12:12:37 +02002289
Christopher Faulet99daf282018-11-28 22:58:13 +01002290 if (rule->rdr_str) { /* this is an old "redirect" rule */
2291 /* add scheme */
2292 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2293 goto fail;
2294 }
2295 else {
2296 /* add scheme with executing log format */
2297 chunk->data += build_logline(s, chunk->area + chunk->data,
2298 chunk->size - chunk->data,
2299 &rule->rdr_fmt);
2300 }
2301 /* add "://" + host + path */
2302 if (!chunk_memcat(chunk, "://", 3) ||
2303 !chunk_memcat(chunk, host.ptr, host.len) ||
2304 !chunk_memcat(chunk, path.ptr, path.len))
2305 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002306
Christopher Faulet99daf282018-11-28 22:58:13 +01002307 /* append a slash at the end of the location if needed and missing */
2308 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
2309 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
2310 if (chunk->data + 1 >= chunk->size)
2311 goto fail;
2312 chunk->area[chunk->data++] = '/';
2313 }
2314 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002315 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002316
Christopher Faulet99daf282018-11-28 22:58:13 +01002317 case REDIRECT_TYPE_PREFIX: {
2318 struct ist path;
Amaury Denoyellec453f952021-07-06 11:40:12 +02002319 struct http_uri_parser parser;
Christopher Faulet99daf282018-11-28 22:58:13 +01002320
Christopher Faulet297fbb42019-05-13 14:41:27 +02002321 sl = http_get_stline(htx);
Amaury Denoyellec453f952021-07-06 11:40:12 +02002322 parser = http_uri_parser_init(htx_sl_req_uri(sl));
2323 path = http_parse_path(&parser);
Christopher Faulet99daf282018-11-28 22:58:13 +01002324 /* build message using path */
Tim Duesterhused526372020-03-05 17:56:33 +01002325 if (isttest(path)) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002326 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2327 int qs = 0;
2328 while (qs < path.len) {
2329 if (*(path.ptr + qs) == '?') {
2330 path.len = qs;
2331 break;
2332 }
2333 qs++;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002334 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002335 }
2336 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002337 else
2338 path = ist("/");
Christopher Fauletf2824e62018-10-01 12:12:37 +02002339
Christopher Faulet99daf282018-11-28 22:58:13 +01002340 if (rule->rdr_str) { /* this is an old "redirect" rule */
2341 /* add prefix. Note that if prefix == "/", we don't want to
2342 * add anything, otherwise it makes it hard for the user to
2343 * configure a self-redirection.
2344 */
2345 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
2346 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2347 goto fail;
2348 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002349 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002350 else {
2351 /* add prefix with executing log format */
2352 chunk->data += build_logline(s, chunk->area + chunk->data,
2353 chunk->size - chunk->data,
2354 &rule->rdr_fmt);
2355 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002356
Christopher Faulet99daf282018-11-28 22:58:13 +01002357 /* add path */
2358 if (!chunk_memcat(chunk, path.ptr, path.len))
2359 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002360
Christopher Faulet99daf282018-11-28 22:58:13 +01002361 /* append a slash at the end of the location if needed and missing */
2362 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
2363 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
2364 if (chunk->data + 1 >= chunk->size)
2365 goto fail;
2366 chunk->area[chunk->data++] = '/';
2367 }
2368 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002369 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002370 case REDIRECT_TYPE_LOCATION:
2371 default:
2372 if (rule->rdr_str) { /* this is an old "redirect" rule */
2373 /* add location */
2374 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2375 goto fail;
2376 }
2377 else {
2378 /* add location with executing log format */
Willy Tarreaubc1223b2021-09-02 16:54:33 +02002379 int len = build_logline(s, chunk->area + chunk->data,
2380 chunk->size - chunk->data,
2381 &rule->rdr_fmt);
Christopher Fauleteab17572022-04-26 20:34:38 +02002382 if (!len && rule->flags & REDIRECT_FLAG_IGNORE_EMPTY) {
2383 ret = 2;
2384 goto out;
2385 }
Willy Tarreaubc1223b2021-09-02 16:54:33 +02002386
2387 chunk->data += len;
Christopher Faulet99daf282018-11-28 22:58:13 +01002388 }
2389 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002390 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002391 location = ist2(chunk->area, chunk->data);
2392
2393 /*
2394 * Create the 30x response
2395 */
2396 switch (rule->code) {
2397 case 308:
2398 status = ist("308");
2399 reason = ist("Permanent Redirect");
2400 break;
2401 case 307:
2402 status = ist("307");
2403 reason = ist("Temporary Redirect");
2404 break;
2405 case 303:
2406 status = ist("303");
2407 reason = ist("See Other");
2408 break;
2409 case 301:
2410 status = ist("301");
2411 reason = ist("Moved Permanently");
2412 break;
2413 case 302:
2414 default:
2415 status = ist("302");
2416 reason = ist("Found");
2417 break;
2418 }
2419
Christopher Faulet08e66462019-05-23 16:44:59 +02002420 if (!(txn->req.flags & HTTP_MSGF_BODYLESS) && txn->req.msg_state != HTTP_MSG_DONE)
2421 close = 1;
2422
Christopher Faulet99daf282018-11-28 22:58:13 +01002423 htx = htx_from_buf(&res->buf);
Kevin Zhu96b36392020-01-07 09:42:55 +01002424 /* Trim any possible response */
2425 channel_htx_truncate(&s->res, htx);
Christopher Faulet617ec5c2023-10-17 11:43:43 +02002426 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_CLEN|HTX_SL_F_BODYLESS);
Christopher Faulet99daf282018-11-28 22:58:13 +01002427 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), status, reason);
2428 if (!sl)
2429 goto fail;
2430 sl->info.res.status = rule->code;
2431 s->txn->status = rule->code;
2432
Christopher Faulet08e66462019-05-23 16:44:59 +02002433 if (close && !htx_add_header(htx, ist("Connection"), ist("close")))
2434 goto fail;
2435
2436 if (!htx_add_header(htx, ist("Content-length"), ist("0")) ||
Christopher Faulet99daf282018-11-28 22:58:13 +01002437 !htx_add_header(htx, ist("Location"), location))
2438 goto fail;
2439
2440 if (rule->code == 302 || rule->code == 303 || rule->code == 307) {
2441 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")))
2442 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002443 }
2444
2445 if (rule->cookie_len) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002446 if (!htx_add_header(htx, ist("Set-Cookie"), ist2(rule->cookie_str, rule->cookie_len)))
2447 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002448 }
2449
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002450 if (!htx_add_endof(htx, HTX_BLK_EOH))
Christopher Faulet99daf282018-11-28 22:58:13 +01002451 goto fail;
2452
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002453 htx->flags |= HTX_FL_EOM;
Kevin Zhu96b36392020-01-07 09:42:55 +01002454 htx_to_buf(htx, &res->buf);
Christopher Fauleta72a7e42020-01-28 09:28:11 +01002455 if (!http_forward_proxy_resp(s, 1))
2456 goto fail;
Christopher Faulet99daf282018-11-28 22:58:13 +01002457
Christopher Faulet60b33a52020-01-28 09:18:10 +01002458 if (rule->flags & REDIRECT_FLAG_FROM_REQ) {
2459 /* let's log the request time */
Willy Tarreau69530f52023-04-28 09:16:15 +02002460 s->logs.request_ts = now_ns;
Christopher Fauletd3475882021-10-04 14:16:46 +02002461 req->analysers &= AN_REQ_FLT_END;
Christopher Faulet99daf282018-11-28 22:58:13 +01002462
Christopher Faulet60b33a52020-01-28 09:18:10 +01002463 if (s->sess->fe == s->be) /* report it if the request was intercepted by the frontend */
Willy Tarreau4781b152021-04-06 13:53:36 +02002464 _HA_ATOMIC_INC(&s->sess->fe->fe_counters.intercepted_req);
Christopher Faulet60b33a52020-01-28 09:18:10 +01002465 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002466
2467 if (!(s->flags & SF_ERR_MASK))
2468 s->flags |= SF_ERR_LOCAL;
Christopher Faulet4a66c942023-01-13 09:43:21 +01002469 http_set_term_flags(s);
Christopher Fauletf2824e62018-10-01 12:12:37 +02002470
Christopher Fauleteab17572022-04-26 20:34:38 +02002471 out:
Christopher Faulet99daf282018-11-28 22:58:13 +01002472 free_trash_chunk(chunk);
Christopher Fauleteab17572022-04-26 20:34:38 +02002473 return ret;
Christopher Faulet99daf282018-11-28 22:58:13 +01002474
2475 fail:
2476 /* If an error occurred, remove the incomplete HTTP response from the
2477 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01002478 channel_htx_truncate(res, htxbuf(&res->buf));
Christopher Fauleteab17572022-04-26 20:34:38 +02002479 ret = 0;
2480 goto out;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002481}
2482
Christopher Faulet18c13d32022-05-16 11:43:10 +02002483/* This function filters the request header names to only allow [0-9a-zA-Z-]
2484 * characters. Depending on the proxy configuration, headers with a name not
2485 * matching this charset are removed or the request is rejected with a
2486 * 403-Forbidden response if such name are found. It returns HTTP_RULE_RES_CONT
2487 * to continue the request processing or HTTP_RULE_RES_DENY if the request is
2488 * rejected.
2489 */
2490static enum rule_result http_req_restrict_header_names(struct stream *s, struct htx *htx, struct proxy *px)
2491{
2492 struct htx_blk *blk;
2493 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
2494
2495 blk = htx_get_first_blk(htx);
2496 while (blk) {
2497 enum htx_blk_type type = htx_get_blk_type(blk);
2498
2499 if (type == HTX_BLK_HDR) {
2500 struct ist n = htx_get_blk_name(htx, blk);
Mateusz Malek4b85a962022-08-17 14:22:09 +02002501 int i, end = istlen(n);
Christopher Faulet18c13d32022-05-16 11:43:10 +02002502
Mateusz Malek4b85a962022-08-17 14:22:09 +02002503 for (i = 0; i < end; i++) {
Christopher Faulet18c13d32022-05-16 11:43:10 +02002504 if (!isalnum((unsigned char)n.ptr[i]) && n.ptr[i] != '-') {
Mateusz Malek4b85a962022-08-17 14:22:09 +02002505 break;
Christopher Faulet18c13d32022-05-16 11:43:10 +02002506 }
2507 }
Mateusz Malek4b85a962022-08-17 14:22:09 +02002508
2509 if (i < end) {
2510 /* Disallowed character found - block the request or remove the header */
2511 if (px->options2 & PR_O2_RSTRICT_REQ_HDR_NAMES_BLK)
2512 goto block;
2513 blk = htx_remove_blk(htx, blk);
2514 continue;
2515 }
Christopher Faulet18c13d32022-05-16 11:43:10 +02002516 }
2517 if (type == HTX_BLK_EOH)
2518 break;
2519
2520 blk = htx_get_next_blk(htx, blk);
2521 }
2522 out:
2523 return rule_ret;
2524 block:
2525 /* Block the request returning a 403-Forbidden response */
2526 s->txn->status = 403;
2527 rule_ret = HTTP_RULE_RES_DENY;
2528 goto out;
2529}
2530
Christopher Faulet92d34fe2019-12-17 09:20:34 +01002531/* Replace all headers matching the name <name>. The header value is replaced if
2532 * it matches the regex <re>. <str> is used for the replacement. If <full> is
2533 * set to 1, the full-line is matched and replaced. Otherwise, comma-separated
2534 * values are evaluated one by one. It returns 0 on success and -1 on error.
2535 */
2536int http_replace_hdrs(struct stream* s, struct htx *htx, struct ist name,
2537 const char *str, struct my_regex *re, int full)
Christopher Faulet72333522018-10-24 11:25:02 +02002538{
2539 struct http_hdr_ctx ctx;
Christopher Faulet72333522018-10-24 11:25:02 +02002540
Christopher Faulet72333522018-10-24 11:25:02 +02002541 ctx.blk = NULL;
Christopher Faulet92d34fe2019-12-17 09:20:34 +01002542 while (http_find_header(htx, name, &ctx, full)) {
Christopher Faulet80707f72023-08-04 16:51:11 +02002543 struct buffer *output = get_trash_chunk();
2544
Christopher Faulet72333522018-10-24 11:25:02 +02002545 if (!regex_exec_match2(re, ctx.value.ptr, ctx.value.len, MAX_MATCH, pmatch, 0))
2546 continue;
2547
2548 output->data = exp_replace(output->area, output->size, ctx.value.ptr, str, pmatch);
2549 if (output->data == -1)
2550 return -1;
2551 if (!http_replace_header_value(htx, &ctx, ist2(output->area, output->data)))
2552 return -1;
2553 }
2554 return 0;
2555}
2556
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002557/* This function executes one of the set-{method,path,query,uri} actions. It
2558 * takes the string from the variable 'replace' with length 'len', then modifies
2559 * the relevant part of the request line accordingly. Then it updates various
2560 * pointers to the next elements which were moved, and the total buffer length.
2561 * It finds the action to be performed in p[2], previously filled by function
2562 * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal
2563 * error, though this can be revisited when this code is finally exploited.
2564 *
2565 * 'action' can be '0' to replace method, '1' to replace path, '2' to replace
Christopher Faulet312294f2020-09-02 17:17:44 +02002566 * query string, 3 to replace uri or 4 to replace the path+query.
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002567 *
2568 * In query string case, the mark question '?' must be set at the start of the
2569 * string by the caller, event if the replacement query string is empty.
2570 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002571int http_req_replace_stline(int action, const char *replace, int len,
2572 struct proxy *px, struct stream *s)
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002573{
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002574 struct htx *htx = htxbuf(&s->req.buf);
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002575
2576 switch (action) {
2577 case 0: // method
2578 if (!http_replace_req_meth(htx, ist2(replace, len)))
2579 return -1;
2580 break;
2581
2582 case 1: // path
Christopher Fauletb8ce5052020-08-31 16:11:57 +02002583 if (!http_replace_req_path(htx, ist2(replace, len), 0))
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002584 return -1;
2585 break;
2586
2587 case 2: // query
2588 if (!http_replace_req_query(htx, ist2(replace, len)))
2589 return -1;
2590 break;
2591
2592 case 3: // uri
2593 if (!http_replace_req_uri(htx, ist2(replace, len)))
2594 return -1;
2595 break;
2596
Christopher Faulet312294f2020-09-02 17:17:44 +02002597 case 4: // path + query
2598 if (!http_replace_req_path(htx, ist2(replace, len), 1))
2599 return -1;
2600 break;
2601
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002602 default:
2603 return -1;
2604 }
2605 return 0;
2606}
2607
2608/* This function replace the HTTP status code and the associated message. The
Christopher Faulete00d06c2019-12-16 17:18:42 +01002609 * variable <status> contains the new status code. This function never fails. It
2610 * returns 0 in case of success, -1 in case of internal error.
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002611 */
Christopher Faulet96bff762019-12-17 13:46:18 +01002612int http_res_set_status(unsigned int status, struct ist reason, struct stream *s)
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002613{
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002614 struct htx *htx = htxbuf(&s->res.buf);
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002615 char *res;
2616
2617 chunk_reset(&trash);
2618 res = ultoa_o(status, trash.area, trash.size);
2619 trash.data = res - trash.area;
2620
2621 /* Do we have a custom reason format string? */
Tim Duesterhuse296d3e2020-03-05 17:56:31 +01002622 if (!isttest(reason)) {
Christopher Faulet96bff762019-12-17 13:46:18 +01002623 const char *str = http_get_reason(status);
Tim Duesterhusdcf753a2021-03-04 17:31:47 +01002624 reason = ist(str);
Christopher Faulet96bff762019-12-17 13:46:18 +01002625 }
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002626
Christopher Fauletbde2c4c2020-08-31 16:43:34 +02002627 if (!http_replace_res_status(htx, ist2(trash.area, trash.data), reason))
Christopher Faulete00d06c2019-12-16 17:18:42 +01002628 return -1;
Willy Tarreau640e2532023-01-10 14:50:44 +01002629 s->txn->status = status;
Christopher Faulete00d06c2019-12-16 17:18:42 +01002630 return 0;
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002631}
2632
Christopher Faulet3e964192018-10-24 11:39:23 +02002633/* Executes the http-request rules <rules> for stream <s>, proxy <px> and
2634 * transaction <txn>. Returns the verdict of the first rule that prevents
2635 * further processing of the request (auth, deny, ...), and defaults to
2636 * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or
2637 * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT
2638 * on txn->flags if it encounters a tarpit rule. If <deny_status> is not NULL
2639 * and a deny/tarpit rule is matched, it will be filled with this rule's deny
2640 * status.
2641 */
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002642static enum rule_result http_req_get_intercept_rule(struct proxy *px, struct list *def_rules,
2643 struct list *rules, struct stream *s)
Christopher Faulet3e964192018-10-24 11:39:23 +02002644{
2645 struct session *sess = strm_sess(s);
2646 struct http_txn *txn = s->txn;
Christopher Faulet3e964192018-10-24 11:39:23 +02002647 struct act_rule *rule;
Christopher Faulet3e964192018-10-24 11:39:23 +02002648 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002649 int act_opts = 0;
Christopher Faulet3e964192018-10-24 11:39:23 +02002650
Christopher Faulet3e964192018-10-24 11:39:23 +02002651 /* If "the current_rule_list" match the executed rule list, we are in
2652 * resume condition. If a resume is needed it is always in the action
2653 * and never in the ACL or converters. In this case, we initialise the
2654 * current rule, and go to the action execution point.
2655 */
2656 if (s->current_rule) {
2657 rule = s->current_rule;
2658 s->current_rule = NULL;
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002659 if (s->current_rule_list == rules || (def_rules && s->current_rule_list == def_rules))
Christopher Faulet3e964192018-10-24 11:39:23 +02002660 goto resume_execution;
2661 }
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002662 s->current_rule_list = ((!def_rules || s->current_rule_list == def_rules) ? rules : def_rules);
Christopher Faulet3e964192018-10-24 11:39:23 +02002663
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002664 restart:
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002665 /* start the ruleset evaluation in strict mode */
2666 txn->req.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01002667
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002668 list_for_each_entry(rule, s->current_rule_list, list) {
Christopher Faulet3e964192018-10-24 11:39:23 +02002669 /* check optional condition */
2670 if (rule->cond) {
2671 int ret;
2672
2673 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
2674 ret = acl_pass(ret);
2675
2676 if (rule->cond->pol == ACL_COND_UNLESS)
2677 ret = !ret;
2678
2679 if (!ret) /* condition not matched */
2680 continue;
2681 }
2682
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002683 act_opts |= ACT_OPT_FIRST;
Christopher Faulet3e964192018-10-24 11:39:23 +02002684 resume_execution:
Amaury Denoyelle03517732021-05-07 14:25:01 +02002685 if (rule->kw->flags & KWF_EXPERIMENTAL)
2686 mark_tainted(TAINTED_ACTION_EXP_EXECUTED);
2687
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002688 /* Always call the action function if defined */
2689 if (rule->action_ptr) {
Christopher Fauletd7bac882023-04-14 12:05:55 +02002690 if ((s->scf->flags & SC_FL_ERROR) ||
Christopher Fauletca5309a2023-04-17 16:17:32 +02002691 ((s->scf->flags & (SC_FL_EOS|SC_FL_ABRT_DONE)) &&
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002692 (px->options & PR_O_ABRT_CLOSE)))
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002693 act_opts |= ACT_OPT_FINAL;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002694
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002695 switch (rule->action_ptr(rule, px, sess, s, act_opts)) {
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002696 case ACT_RET_CONT:
2697 break;
2698 case ACT_RET_STOP:
2699 rule_ret = HTTP_RULE_RES_STOP;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002700 s->last_rule_file = rule->conf.file;
2701 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002702 goto end;
2703 case ACT_RET_YIELD:
2704 s->current_rule = rule;
2705 rule_ret = HTTP_RULE_RES_YIELD;
2706 goto end;
2707 case ACT_RET_ERR:
2708 rule_ret = HTTP_RULE_RES_ERROR;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002709 s->last_rule_file = rule->conf.file;
2710 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002711 goto end;
2712 case ACT_RET_DONE:
2713 rule_ret = HTTP_RULE_RES_DONE;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002714 s->last_rule_file = rule->conf.file;
2715 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002716 goto end;
2717 case ACT_RET_DENY:
Christopher Fauletb58f62b2020-01-13 16:40:13 +01002718 if (txn->status == -1)
2719 txn->status = 403;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002720 rule_ret = HTTP_RULE_RES_DENY;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002721 s->last_rule_file = rule->conf.file;
2722 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002723 goto end;
2724 case ACT_RET_ABRT:
2725 rule_ret = HTTP_RULE_RES_ABRT;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002726 s->last_rule_file = rule->conf.file;
2727 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002728 goto end;
2729 case ACT_RET_INV:
2730 rule_ret = HTTP_RULE_RES_BADREQ;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002731 s->last_rule_file = rule->conf.file;
2732 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002733 goto end;
2734 }
2735 continue; /* eval the next rule */
2736 }
2737
2738 /* If not action function defined, check for known actions */
Christopher Faulet3e964192018-10-24 11:39:23 +02002739 switch (rule->action) {
2740 case ACT_ACTION_ALLOW:
2741 rule_ret = HTTP_RULE_RES_STOP;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002742 s->last_rule_file = rule->conf.file;
2743 s->last_rule_line = rule->conf.line;
Christopher Faulet3e964192018-10-24 11:39:23 +02002744 goto end;
2745
2746 case ACT_ACTION_DENY:
Christopher Faulet5cb513a2020-05-13 17:56:56 +02002747 txn->status = rule->arg.http_reply->status;
2748 txn->http_reply = rule->arg.http_reply;
Christopher Faulet3e964192018-10-24 11:39:23 +02002749 rule_ret = HTTP_RULE_RES_DENY;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002750 s->last_rule_file = rule->conf.file;
2751 s->last_rule_line = rule->conf.line;
Christopher Faulet3e964192018-10-24 11:39:23 +02002752 goto end;
2753
2754 case ACT_HTTP_REQ_TARPIT:
2755 txn->flags |= TX_CLTARPIT;
Christopher Faulet5cb513a2020-05-13 17:56:56 +02002756 txn->status = rule->arg.http_reply->status;
2757 txn->http_reply = rule->arg.http_reply;
Christopher Faulet3e964192018-10-24 11:39:23 +02002758 rule_ret = HTTP_RULE_RES_DENY;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002759 s->last_rule_file = rule->conf.file;
2760 s->last_rule_line = rule->conf.line;
Christopher Faulet3e964192018-10-24 11:39:23 +02002761 goto end;
2762
Willy Tarreaubc1223b2021-09-02 16:54:33 +02002763 case ACT_HTTP_REDIR: {
2764 int ret = http_apply_redirect_rule(rule->arg.redir, s, txn);
2765
2766 if (ret == 2) // 2 == skip
2767 break;
2768
2769 rule_ret = ret ? HTTP_RULE_RES_ABRT : HTTP_RULE_RES_ERROR;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002770 s->last_rule_file = rule->conf.file;
2771 s->last_rule_line = rule->conf.line;
Christopher Faulet3e964192018-10-24 11:39:23 +02002772 goto end;
Willy Tarreaubc1223b2021-09-02 16:54:33 +02002773 }
Christopher Faulet3e964192018-10-24 11:39:23 +02002774
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002775 /* other flags exists, but normally, they never be matched. */
Christopher Faulet3e964192018-10-24 11:39:23 +02002776 default:
2777 break;
2778 }
2779 }
2780
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002781 if (def_rules && s->current_rule_list == def_rules) {
2782 s->current_rule_list = rules;
2783 goto restart;
2784 }
2785
Christopher Faulet3e964192018-10-24 11:39:23 +02002786 end:
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002787 /* if the ruleset evaluation is finished reset the strict mode */
Christopher Faulet46f95542019-12-20 10:07:22 +01002788 if (rule_ret != HTTP_RULE_RES_YIELD)
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002789 txn->req.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01002790
Christopher Faulet3e964192018-10-24 11:39:23 +02002791 /* we reached the end of the rules, nothing to report */
2792 return rule_ret;
2793}
2794
2795/* Executes the http-response rules <rules> for stream <s> and proxy <px>. It
2796 * returns one of 5 possible statuses: HTTP_RULE_RES_CONT, HTTP_RULE_RES_STOP,
2797 * HTTP_RULE_RES_DONE, HTTP_RULE_RES_YIELD, or HTTP_RULE_RES_BADREQ. If *CONT
2798 * is returned, the process can continue the evaluation of next rule list. If
2799 * *STOP or *DONE is returned, the process must stop the evaluation. If *BADREQ
2800 * is returned, it means the operation could not be processed and a server error
Christopher Fauleta53abad2020-05-13 08:12:22 +02002801 * must be returned. If *YIELD is returned, the caller must call again the
2802 * function with the same context.
Christopher Faulet3e964192018-10-24 11:39:23 +02002803 */
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002804static enum rule_result http_res_get_intercept_rule(struct proxy *px, struct list *def_rules,
2805 struct list *rules, struct stream *s)
Christopher Faulet3e964192018-10-24 11:39:23 +02002806{
2807 struct session *sess = strm_sess(s);
2808 struct http_txn *txn = s->txn;
Christopher Faulet3e964192018-10-24 11:39:23 +02002809 struct act_rule *rule;
Christopher Faulet3e964192018-10-24 11:39:23 +02002810 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002811 int act_opts = 0;
Christopher Faulet3e964192018-10-24 11:39:23 +02002812
Christopher Faulet3e964192018-10-24 11:39:23 +02002813 /* If "the current_rule_list" match the executed rule list, we are in
2814 * resume condition. If a resume is needed it is always in the action
2815 * and never in the ACL or converters. In this case, we initialise the
2816 * current rule, and go to the action execution point.
2817 */
2818 if (s->current_rule) {
2819 rule = s->current_rule;
2820 s->current_rule = NULL;
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002821 if (s->current_rule_list == rules || (def_rules && s->current_rule_list == def_rules))
Christopher Faulet3e964192018-10-24 11:39:23 +02002822 goto resume_execution;
2823 }
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002824 s->current_rule_list = ((!def_rules || s->current_rule_list == def_rules) ? rules : def_rules);
2825
2826 restart:
Christopher Faulet3e964192018-10-24 11:39:23 +02002827
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002828 /* start the ruleset evaluation in strict mode */
2829 txn->rsp.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01002830
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002831 list_for_each_entry(rule, s->current_rule_list, list) {
Christopher Faulet3e964192018-10-24 11:39:23 +02002832 /* check optional condition */
2833 if (rule->cond) {
2834 int ret;
2835
2836 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
2837 ret = acl_pass(ret);
2838
2839 if (rule->cond->pol == ACL_COND_UNLESS)
2840 ret = !ret;
2841
2842 if (!ret) /* condition not matched */
2843 continue;
2844 }
2845
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002846 act_opts |= ACT_OPT_FIRST;
Christopher Faulet3e964192018-10-24 11:39:23 +02002847resume_execution:
Amaury Denoyelle03517732021-05-07 14:25:01 +02002848 if (rule->kw->flags & KWF_EXPERIMENTAL)
2849 mark_tainted(TAINTED_ACTION_EXP_EXECUTED);
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002850
2851 /* Always call the action function if defined */
2852 if (rule->action_ptr) {
Christopher Fauletd7bac882023-04-14 12:05:55 +02002853 if ((s->scf->flags & SC_FL_ERROR) ||
Christopher Fauletca5309a2023-04-17 16:17:32 +02002854 ((s->scf->flags & (SC_FL_EOS|SC_FL_ABRT_DONE)) &&
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002855 (px->options & PR_O_ABRT_CLOSE)))
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002856 act_opts |= ACT_OPT_FINAL;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002857
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002858 switch (rule->action_ptr(rule, px, sess, s, act_opts)) {
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002859 case ACT_RET_CONT:
2860 break;
2861 case ACT_RET_STOP:
2862 rule_ret = HTTP_RULE_RES_STOP;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002863 s->last_rule_file = rule->conf.file;
2864 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002865 goto end;
2866 case ACT_RET_YIELD:
2867 s->current_rule = rule;
2868 rule_ret = HTTP_RULE_RES_YIELD;
2869 goto end;
2870 case ACT_RET_ERR:
2871 rule_ret = HTTP_RULE_RES_ERROR;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002872 s->last_rule_file = rule->conf.file;
2873 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002874 goto end;
2875 case ACT_RET_DONE:
2876 rule_ret = HTTP_RULE_RES_DONE;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002877 s->last_rule_file = rule->conf.file;
2878 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002879 goto end;
2880 case ACT_RET_DENY:
Christopher Fauletb58f62b2020-01-13 16:40:13 +01002881 if (txn->status == -1)
2882 txn->status = 502;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002883 rule_ret = HTTP_RULE_RES_DENY;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002884 s->last_rule_file = rule->conf.file;
2885 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002886 goto end;
2887 case ACT_RET_ABRT:
2888 rule_ret = HTTP_RULE_RES_ABRT;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002889 s->last_rule_file = rule->conf.file;
2890 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002891 goto end;
2892 case ACT_RET_INV:
2893 rule_ret = HTTP_RULE_RES_BADREQ;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002894 s->last_rule_file = rule->conf.file;
2895 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002896 goto end;
2897 }
2898 continue; /* eval the next rule */
2899 }
2900
2901 /* If not action function defined, check for known actions */
Christopher Faulet3e964192018-10-24 11:39:23 +02002902 switch (rule->action) {
2903 case ACT_ACTION_ALLOW:
2904 rule_ret = HTTP_RULE_RES_STOP; /* "allow" rules are OK */
Willy Tarreauc6dae862022-03-09 17:23:10 +01002905 s->last_rule_file = rule->conf.file;
2906 s->last_rule_line = rule->conf.line;
Christopher Faulet3e964192018-10-24 11:39:23 +02002907 goto end;
2908
2909 case ACT_ACTION_DENY:
Christopher Faulet5cb513a2020-05-13 17:56:56 +02002910 txn->status = rule->arg.http_reply->status;
2911 txn->http_reply = rule->arg.http_reply;
Christopher Faulet3a26bee2019-12-16 12:47:40 +01002912 rule_ret = HTTP_RULE_RES_DENY;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002913 s->last_rule_file = rule->conf.file;
2914 s->last_rule_line = rule->conf.line;
Christopher Faulet3e964192018-10-24 11:39:23 +02002915 goto end;
2916
Willy Tarreaubc1223b2021-09-02 16:54:33 +02002917 case ACT_HTTP_REDIR: {
2918 int ret = http_apply_redirect_rule(rule->arg.redir, s, txn);
Christopher Faulet3e964192018-10-24 11:39:23 +02002919
Willy Tarreaubc1223b2021-09-02 16:54:33 +02002920 if (ret == 2) // 2 == skip
2921 break;
2922
2923 rule_ret = ret ? HTTP_RULE_RES_ABRT : HTTP_RULE_RES_ERROR;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002924 s->last_rule_file = rule->conf.file;
2925 s->last_rule_line = rule->conf.line;
Willy Tarreaubc1223b2021-09-02 16:54:33 +02002926 goto end;
2927 }
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002928 /* other flags exists, but normally, they never be matched. */
Christopher Faulet3e964192018-10-24 11:39:23 +02002929 default:
2930 break;
2931 }
2932 }
2933
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002934 if (def_rules && s->current_rule_list == def_rules) {
2935 s->current_rule_list = rules;
2936 goto restart;
2937 }
2938
Christopher Faulet3e964192018-10-24 11:39:23 +02002939 end:
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002940 /* if the ruleset evaluation is finished reset the strict mode */
Christopher Faulet46f95542019-12-20 10:07:22 +01002941 if (rule_ret != HTTP_RULE_RES_YIELD)
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002942 txn->rsp.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01002943
Christopher Faulet3e964192018-10-24 11:39:23 +02002944 /* we reached the end of the rules, nothing to report */
2945 return rule_ret;
2946}
2947
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002948/* Executes backend and frontend http-after-response rules for the stream <s>,
2949 * in that order. it return 1 on success and 0 on error. It is the caller
2950 * responsibility to catch error or ignore it. If it catches it, this function
2951 * may be called a second time, for the internal error.
2952 */
2953int http_eval_after_res_rules(struct stream *s)
2954{
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002955 struct list *def_rules, *rules;
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002956 struct session *sess = s->sess;
2957 enum rule_result ret = HTTP_RULE_RES_CONT;
2958
Christopher Faulet507479b2020-05-15 12:29:46 +02002959 /* Eval after-response ruleset only if the reply is not const */
2960 if (s->txn->flags & TX_CONST_REPLY)
2961 goto end;
2962
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002963 /* prune the request variables if not already done and swap to the response variables. */
2964 if (s->vars_reqres.scope != SCOPE_RES) {
2965 if (!LIST_ISEMPTY(&s->vars_reqres.head))
2966 vars_prune(&s->vars_reqres, s->sess, s);
Willy Tarreaub7bfcb32021-08-31 08:13:25 +02002967 vars_init_head(&s->vars_reqres, SCOPE_RES);
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002968 }
2969
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002970 def_rules = (s->be->defpx ? &s->be->defpx->http_after_res_rules : NULL);
2971 rules = &s->be->http_after_res_rules;
2972
2973 ret = http_res_get_intercept_rule(s->be, def_rules, rules, s);
Christopher Faulet4c5a5912021-11-09 17:48:39 +01002974 if ((ret == HTTP_RULE_RES_CONT || ret == HTTP_RULE_RES_STOP) && sess->fe != s->be) {
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002975 def_rules = ((sess->fe->defpx && sess->fe->defpx != s->be->defpx) ? &sess->fe->defpx->http_after_res_rules : NULL);
2976 rules = &sess->fe->http_after_res_rules;
2977 ret = http_res_get_intercept_rule(sess->fe, def_rules, rules, s);
2978 }
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002979
Christopher Faulet507479b2020-05-15 12:29:46 +02002980 end:
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002981 /* All other codes than CONTINUE, STOP or DONE are forbidden */
2982 return (ret == HTTP_RULE_RES_CONT || ret == HTTP_RULE_RES_STOP || ret == HTTP_RULE_RES_DONE);
2983}
2984
Christopher Fauletfcda7c62018-10-24 11:56:22 +02002985/*
2986 * Manage client-side cookie. It can impact performance by about 2% so it is
2987 * desirable to call it only when needed. This code is quite complex because
2988 * of the multiple very crappy and ambiguous syntaxes we have to support. it
2989 * highly recommended not to touch this part without a good reason !
2990 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002991static void http_manage_client_side_cookies(struct stream *s, struct channel *req)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02002992{
2993 struct session *sess = s->sess;
2994 struct http_txn *txn = s->txn;
2995 struct htx *htx;
2996 struct http_hdr_ctx ctx;
2997 char *hdr_beg, *hdr_end, *del_from;
2998 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
2999 int preserve_hdr;
3000
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003001 htx = htxbuf(&req->buf);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003002 ctx.blk = NULL;
3003 while (http_find_header(htx, ist("Cookie"), &ctx, 1)) {
Olivier Houchardf0f42382019-07-22 17:43:46 +02003004 int is_first = 1;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003005 del_from = NULL; /* nothing to be deleted */
3006 preserve_hdr = 0; /* assume we may kill the whole header */
3007
3008 /* Now look for cookies. Conforming to RFC2109, we have to support
3009 * attributes whose name begin with a '$', and associate them with
3010 * the right cookie, if we want to delete this cookie.
3011 * So there are 3 cases for each cookie read :
3012 * 1) it's a special attribute, beginning with a '$' : ignore it.
3013 * 2) it's a server id cookie that we *MAY* want to delete : save
3014 * some pointers on it (last semi-colon, beginning of cookie...)
3015 * 3) it's an application cookie : we *MAY* have to delete a previous
3016 * "special" cookie.
3017 * At the end of loop, if a "special" cookie remains, we may have to
3018 * remove it. If no application cookie persists in the header, we
3019 * *MUST* delete it.
3020 *
3021 * Note: RFC2965 is unclear about the processing of spaces around
3022 * the equal sign in the ATTR=VALUE form. A careful inspection of
3023 * the RFC explicitly allows spaces before it, and not within the
3024 * tokens (attrs or values). An inspection of RFC2109 allows that
3025 * too but section 10.1.3 lets one think that spaces may be allowed
3026 * after the equal sign too, resulting in some (rare) buggy
3027 * implementations trying to do that. So let's do what servers do.
3028 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
3029 * allowed quoted strings in values, with any possible character
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003030 * after a backslash, including control chars and delimiters, which
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003031 * causes parsing to become ambiguous. Browsers also allow spaces
3032 * within values even without quotes.
3033 *
3034 * We have to keep multiple pointers in order to support cookie
3035 * removal at the beginning, middle or end of header without
3036 * corrupting the header. All of these headers are valid :
3037 *
3038 * hdr_beg hdr_end
3039 * | |
3040 * v |
3041 * NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3 |
3042 * NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3 v
3043 * NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3
3044 * | | | | | | |
3045 * | | | | | | |
3046 * | | | | | | +--> next
3047 * | | | | | +----> val_end
3048 * | | | | +-----------> val_beg
3049 * | | | +--------------> equal
3050 * | | +----------------> att_end
3051 * | +---------------------> att_beg
3052 * +--------------------------> prev
3053 *
3054 */
3055 hdr_beg = ctx.value.ptr;
3056 hdr_end = hdr_beg + ctx.value.len;
3057 for (prev = hdr_beg; prev < hdr_end; prev = next) {
3058 /* Iterate through all cookies on this line */
3059
3060 /* find att_beg */
3061 att_beg = prev;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003062 if (!is_first)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003063 att_beg++;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003064 is_first = 0;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003065
3066 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
3067 att_beg++;
3068
3069 /* find att_end : this is the first character after the last non
3070 * space before the equal. It may be equal to hdr_end.
3071 */
3072 equal = att_end = att_beg;
3073 while (equal < hdr_end) {
3074 if (*equal == '=' || *equal == ',' || *equal == ';')
3075 break;
3076 if (HTTP_IS_SPHT(*equal++))
3077 continue;
3078 att_end = equal;
3079 }
3080
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003081 /* here, <equal> points to '=', a delimiter or the end. <att_end>
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003082 * is between <att_beg> and <equal>, both may be identical.
3083 */
3084 /* look for end of cookie if there is an equal sign */
3085 if (equal < hdr_end && *equal == '=') {
3086 /* look for the beginning of the value */
3087 val_beg = equal + 1;
3088 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
3089 val_beg++;
3090
3091 /* find the end of the value, respecting quotes */
3092 next = http_find_cookie_value_end(val_beg, hdr_end);
3093
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003094 /* make val_end point to the first white space or delimiter after the value */
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003095 val_end = next;
3096 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
3097 val_end--;
3098 }
3099 else
3100 val_beg = val_end = next = equal;
3101
3102 /* We have nothing to do with attributes beginning with
3103 * '$'. However, they will automatically be removed if a
3104 * header before them is removed, since they're supposed
3105 * to be linked together.
3106 */
3107 if (*att_beg == '$')
3108 continue;
3109
3110 /* Ignore cookies with no equal sign */
3111 if (equal == next) {
3112 /* This is not our cookie, so we must preserve it. But if we already
3113 * scheduled another cookie for removal, we cannot remove the
3114 * complete header, but we can remove the previous block itself.
3115 */
3116 preserve_hdr = 1;
3117 if (del_from != NULL) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003118 int delta = http_del_hdr_value(hdr_beg, hdr_end, &del_from, prev);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003119 val_end += delta;
3120 next += delta;
3121 hdr_end += delta;
3122 prev = del_from;
3123 del_from = NULL;
3124 }
3125 continue;
3126 }
3127
3128 /* if there are spaces around the equal sign, we need to
3129 * strip them otherwise we'll get trouble for cookie captures,
3130 * or even for rewrites. Since this happens extremely rarely,
3131 * it does not hurt performance.
3132 */
3133 if (unlikely(att_end != equal || val_beg > equal + 1)) {
3134 int stripped_before = 0;
3135 int stripped_after = 0;
3136
3137 if (att_end != equal) {
3138 memmove(att_end, equal, hdr_end - equal);
3139 stripped_before = (att_end - equal);
3140 equal += stripped_before;
3141 val_beg += stripped_before;
3142 }
3143
3144 if (val_beg > equal + 1) {
3145 memmove(equal + 1, val_beg, hdr_end + stripped_before - val_beg);
3146 stripped_after = (equal + 1) - val_beg;
3147 val_beg += stripped_after;
3148 stripped_before += stripped_after;
3149 }
3150
3151 val_end += stripped_before;
3152 next += stripped_before;
3153 hdr_end += stripped_before;
3154 }
3155 /* now everything is as on the diagram above */
3156
3157 /* First, let's see if we want to capture this cookie. We check
3158 * that we don't already have a client side cookie, because we
3159 * can only capture one. Also as an optimisation, we ignore
3160 * cookies shorter than the declared name.
3161 */
3162 if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL &&
3163 (val_end - att_beg >= sess->fe->capture_namelen) &&
3164 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
3165 int log_len = val_end - att_beg;
3166
3167 if ((txn->cli_cookie = pool_alloc(pool_head_capture)) == NULL) {
3168 ha_alert("HTTP logging : out of memory.\n");
3169 } else {
3170 if (log_len > sess->fe->capture_len)
3171 log_len = sess->fe->capture_len;
3172 memcpy(txn->cli_cookie, att_beg, log_len);
3173 txn->cli_cookie[log_len] = 0;
3174 }
3175 }
3176
3177 /* Persistence cookies in passive, rewrite or insert mode have the
3178 * following form :
3179 *
3180 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
3181 *
3182 * For cookies in prefix mode, the form is :
3183 *
3184 * Cookie: NAME=SRV~VALUE
3185 */
3186 if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
3187 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
3188 struct server *srv = s->be->srv;
3189 char *delim;
3190
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003191 /* if we're in cookie prefix mode, we'll search the delimiter so that we
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003192 * have the server ID between val_beg and delim, and the original cookie between
3193 * delim+1 and val_end. Otherwise, delim==val_end :
3194 *
3195 * hdr_beg
3196 * |
3197 * v
3198 * NAME=SRV; # in all but prefix modes
3199 * NAME=SRV~OPAQUE ; # in prefix mode
3200 * || || | |+-> next
3201 * || || | +--> val_end
3202 * || || +---------> delim
3203 * || |+------------> val_beg
3204 * || +-------------> att_end = equal
3205 * |+-----------------> att_beg
3206 * +------------------> prev
3207 *
3208 */
3209 if (s->be->ck_opts & PR_CK_PFX) {
3210 for (delim = val_beg; delim < val_end; delim++)
3211 if (*delim == COOKIE_DELIM)
3212 break;
3213 }
3214 else {
3215 char *vbar1;
3216 delim = val_end;
3217 /* Now check if the cookie contains a date field, which would
3218 * appear after a vertical bar ('|') just after the server name
3219 * and before the delimiter.
3220 */
3221 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
3222 if (vbar1) {
3223 /* OK, so left of the bar is the server's cookie and
3224 * right is the last seen date. It is a base64 encoded
3225 * 30-bit value representing the UNIX date since the
3226 * epoch in 4-second quantities.
3227 */
3228 int val;
3229 delim = vbar1++;
3230 if (val_end - vbar1 >= 5) {
3231 val = b64tos30(vbar1);
3232 if (val > 0)
3233 txn->cookie_last_date = val << 2;
3234 }
3235 /* look for a second vertical bar */
3236 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
3237 if (vbar1 && (val_end - vbar1 > 5)) {
3238 val = b64tos30(vbar1 + 1);
3239 if (val > 0)
3240 txn->cookie_first_date = val << 2;
3241 }
3242 }
3243 }
3244
3245 /* if the cookie has an expiration date and the proxy wants to check
3246 * it, then we do that now. We first check if the cookie is too old,
3247 * then only if it has expired. We detect strict overflow because the
3248 * time resolution here is not great (4 seconds). Cookies with dates
3249 * in the future are ignored if their offset is beyond one day. This
3250 * allows an admin to fix timezone issues without expiring everyone
3251 * and at the same time avoids keeping unwanted side effects for too
3252 * long.
3253 */
3254 if (txn->cookie_first_date && s->be->cookie_maxlife &&
3255 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) ||
3256 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
3257 txn->flags &= ~TX_CK_MASK;
3258 txn->flags |= TX_CK_OLD;
3259 delim = val_beg; // let's pretend we have not found the cookie
3260 txn->cookie_first_date = 0;
3261 txn->cookie_last_date = 0;
3262 }
3263 else if (txn->cookie_last_date && s->be->cookie_maxidle &&
3264 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) ||
3265 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
3266 txn->flags &= ~TX_CK_MASK;
3267 txn->flags |= TX_CK_EXPIRED;
3268 delim = val_beg; // let's pretend we have not found the cookie
3269 txn->cookie_first_date = 0;
3270 txn->cookie_last_date = 0;
3271 }
3272
3273 /* Here, we'll look for the first running server which supports the cookie.
3274 * This allows to share a same cookie between several servers, for example
3275 * to dedicate backup servers to specific servers only.
3276 * However, to prevent clients from sticking to cookie-less backup server
3277 * when they have incidentely learned an empty cookie, we simply ignore
3278 * empty cookies and mark them as invalid.
3279 * The same behaviour is applied when persistence must be ignored.
3280 */
3281 if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
3282 srv = NULL;
3283
3284 while (srv) {
3285 if (srv->cookie && (srv->cklen == delim - val_beg) &&
3286 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
3287 if ((srv->cur_state != SRV_ST_STOPPED) ||
3288 (s->be->options & PR_O_PERSIST) ||
3289 (s->flags & SF_FORCE_PRST)) {
3290 /* we found the server and we can use it */
3291 txn->flags &= ~TX_CK_MASK;
3292 txn->flags |= (srv->cur_state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
3293 s->flags |= SF_DIRECT | SF_ASSIGNED;
3294 s->target = &srv->obj_type;
3295 break;
3296 } else {
3297 /* we found a server, but it's down,
3298 * mark it as such and go on in case
3299 * another one is available.
3300 */
3301 txn->flags &= ~TX_CK_MASK;
3302 txn->flags |= TX_CK_DOWN;
3303 }
3304 }
3305 srv = srv->next;
3306 }
3307
3308 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
3309 /* no server matched this cookie or we deliberately skipped it */
3310 txn->flags &= ~TX_CK_MASK;
3311 if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
3312 txn->flags |= TX_CK_UNUSED;
3313 else
3314 txn->flags |= TX_CK_INVALID;
3315 }
3316
3317 /* depending on the cookie mode, we may have to either :
3318 * - delete the complete cookie if we're in insert+indirect mode, so that
3319 * the server never sees it ;
3320 * - remove the server id from the cookie value, and tag the cookie as an
Joseph Herlante9d5c722018-11-25 11:00:25 -08003321 * application cookie so that it does not get accidentally removed later,
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003322 * if we're in cookie prefix mode
3323 */
3324 if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
3325 int delta; /* negative */
3326
3327 memmove(val_beg, delim + 1, hdr_end - (delim + 1));
3328 delta = val_beg - (delim + 1);
3329 val_end += delta;
3330 next += delta;
3331 hdr_end += delta;
3332 del_from = NULL;
3333 preserve_hdr = 1; /* we want to keep this cookie */
3334 }
3335 else if (del_from == NULL &&
3336 (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
3337 del_from = prev;
3338 }
3339 }
3340 else {
3341 /* This is not our cookie, so we must preserve it. But if we already
3342 * scheduled another cookie for removal, we cannot remove the
3343 * complete header, but we can remove the previous block itself.
3344 */
3345 preserve_hdr = 1;
3346
3347 if (del_from != NULL) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003348 int delta = http_del_hdr_value(hdr_beg, hdr_end, &del_from, prev);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003349 if (att_beg >= del_from)
3350 att_beg += delta;
3351 if (att_end >= del_from)
3352 att_end += delta;
3353 val_beg += delta;
3354 val_end += delta;
3355 next += delta;
3356 hdr_end += delta;
3357 prev = del_from;
3358 del_from = NULL;
3359 }
3360 }
3361
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003362 } /* for each cookie */
3363
3364
3365 /* There are no more cookies on this line.
3366 * We may still have one (or several) marked for deletion at the
3367 * end of the line. We must do this now in two ways :
3368 * - if some cookies must be preserved, we only delete from the
3369 * mark to the end of line ;
3370 * - if nothing needs to be preserved, simply delete the whole header
3371 */
3372 if (del_from) {
3373 hdr_end = (preserve_hdr ? del_from : hdr_beg);
3374 }
3375 if ((hdr_end - hdr_beg) != ctx.value.len) {
Christopher Faulet3e2638e2019-06-18 09:49:16 +02003376 if (hdr_beg != hdr_end)
3377 htx_change_blk_value_len(htx, ctx.blk, hdr_end - hdr_beg);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003378 else
3379 http_remove_header(htx, &ctx);
3380 }
3381 } /* for each "Cookie header */
3382}
3383
3384/*
3385 * Manage server-side cookies. It can impact performance by about 2% so it is
3386 * desirable to call it only when needed. This function is also used when we
3387 * just need to know if there is a cookie (eg: for check-cache).
3388 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003389static void http_manage_server_side_cookies(struct stream *s, struct channel *res)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003390{
3391 struct session *sess = s->sess;
3392 struct http_txn *txn = s->txn;
3393 struct htx *htx;
3394 struct http_hdr_ctx ctx;
3395 struct server *srv;
3396 char *hdr_beg, *hdr_end;
3397 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003398
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003399 htx = htxbuf(&res->buf);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003400
3401 ctx.blk = NULL;
Willy Tarreauaa1909e2022-11-14 18:58:35 +01003402 while (http_find_header(htx, ist("Set-Cookie"), &ctx, 1)) {
Olivier Houchardf0f42382019-07-22 17:43:46 +02003403 int is_first = 1;
3404
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003405 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
3406 * <prev> points to the colon.
3407 */
3408 txn->flags |= TX_SCK_PRESENT;
3409
3410 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
3411 * check-cache is enabled) and we are not interested in checking
3412 * them. Warning, the cookie capture is declared in the frontend.
3413 */
3414 if (s->be->cookie_name == NULL && sess->fe->capture_name == NULL)
3415 break;
3416
3417 /* OK so now we know we have to process this response cookie.
3418 * The format of the Set-Cookie header is slightly different
3419 * from the format of the Cookie header in that it does not
3420 * support the comma as a cookie delimiter (thus the header
3421 * cannot be folded) because the Expires attribute described in
3422 * the original Netscape's spec may contain an unquoted date
3423 * with a comma inside. We have to live with this because
3424 * many browsers don't support Max-Age and some browsers don't
3425 * support quoted strings. However the Set-Cookie2 header is
Willy Tarreauaa1909e2022-11-14 18:58:35 +01003426 * clean but basically nobody supports it.
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003427 *
3428 * We have to keep multiple pointers in order to support cookie
3429 * removal at the beginning, middle or end of header without
3430 * corrupting the header (in case of set-cookie2). A special
3431 * pointer, <scav> points to the beginning of the set-cookie-av
3432 * fields after the first semi-colon. The <next> pointer points
3433 * either to the end of line (set-cookie) or next unquoted comma
3434 * (set-cookie2). All of these headers are valid :
3435 *
3436 * hdr_beg hdr_end
3437 * | |
3438 * v |
3439 * NAME1 = VALUE 1 ; Secure; Path="/" |
3440 * NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT v
3441 * NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT
3442 * NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard
3443 * | | | | | | | |
3444 * | | | | | | | +-> next
3445 * | | | | | | +------------> scav
3446 * | | | | | +--------------> val_end
3447 * | | | | +--------------------> val_beg
3448 * | | | +----------------------> equal
3449 * | | +------------------------> att_end
3450 * | +----------------------------> att_beg
3451 * +------------------------------> prev
3452 * -------------------------------> hdr_beg
3453 */
3454 hdr_beg = ctx.value.ptr;
3455 hdr_end = hdr_beg + ctx.value.len;
3456 for (prev = hdr_beg; prev < hdr_end; prev = next) {
3457
3458 /* Iterate through all cookies on this line */
3459
3460 /* find att_beg */
3461 att_beg = prev;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003462 if (!is_first)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003463 att_beg++;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003464 is_first = 0;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003465
3466 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
3467 att_beg++;
3468
3469 /* find att_end : this is the first character after the last non
3470 * space before the equal. It may be equal to hdr_end.
3471 */
3472 equal = att_end = att_beg;
3473
3474 while (equal < hdr_end) {
Willy Tarreauaa1909e2022-11-14 18:58:35 +01003475 if (*equal == '=' || *equal == ';')
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003476 break;
3477 if (HTTP_IS_SPHT(*equal++))
3478 continue;
3479 att_end = equal;
3480 }
3481
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003482 /* here, <equal> points to '=', a delimiter or the end. <att_end>
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003483 * is between <att_beg> and <equal>, both may be identical.
3484 */
3485
3486 /* look for end of cookie if there is an equal sign */
3487 if (equal < hdr_end && *equal == '=') {
3488 /* look for the beginning of the value */
3489 val_beg = equal + 1;
3490 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
3491 val_beg++;
3492
3493 /* find the end of the value, respecting quotes */
3494 next = http_find_cookie_value_end(val_beg, hdr_end);
3495
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003496 /* make val_end point to the first white space or delimiter after the value */
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003497 val_end = next;
3498 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
3499 val_end--;
3500 }
3501 else {
3502 /* <equal> points to next comma, semi-colon or EOL */
3503 val_beg = val_end = next = equal;
3504 }
3505
3506 if (next < hdr_end) {
Willy Tarreauaa1909e2022-11-14 18:58:35 +01003507 /* For Set-Cookie, since commas are permitted
3508 * in values, skip to the end.
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003509 */
Willy Tarreauaa1909e2022-11-14 18:58:35 +01003510 next = hdr_end;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003511 }
3512
3513 /* Now everything is as on the diagram above */
3514
3515 /* Ignore cookies with no equal sign */
3516 if (equal == val_end)
3517 continue;
3518
3519 /* If there are spaces around the equal sign, we need to
3520 * strip them otherwise we'll get trouble for cookie captures,
3521 * or even for rewrites. Since this happens extremely rarely,
3522 * it does not hurt performance.
3523 */
3524 if (unlikely(att_end != equal || val_beg > equal + 1)) {
3525 int stripped_before = 0;
3526 int stripped_after = 0;
3527
3528 if (att_end != equal) {
3529 memmove(att_end, equal, hdr_end - equal);
3530 stripped_before = (att_end - equal);
3531 equal += stripped_before;
3532 val_beg += stripped_before;
3533 }
3534
3535 if (val_beg > equal + 1) {
3536 memmove(equal + 1, val_beg, hdr_end + stripped_before - val_beg);
3537 stripped_after = (equal + 1) - val_beg;
3538 val_beg += stripped_after;
3539 stripped_before += stripped_after;
3540 }
3541
3542 val_end += stripped_before;
3543 next += stripped_before;
3544 hdr_end += stripped_before;
3545
Christopher Faulet3e2638e2019-06-18 09:49:16 +02003546 htx_change_blk_value_len(htx, ctx.blk, hdr_end - hdr_beg);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003547 ctx.value.len = hdr_end - hdr_beg;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003548 }
3549
3550 /* First, let's see if we want to capture this cookie. We check
3551 * that we don't already have a server side cookie, because we
3552 * can only capture one. Also as an optimisation, we ignore
3553 * cookies shorter than the declared name.
3554 */
3555 if (sess->fe->capture_name != NULL &&
3556 txn->srv_cookie == NULL &&
3557 (val_end - att_beg >= sess->fe->capture_namelen) &&
3558 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
3559 int log_len = val_end - att_beg;
3560 if ((txn->srv_cookie = pool_alloc(pool_head_capture)) == NULL) {
3561 ha_alert("HTTP logging : out of memory.\n");
3562 }
3563 else {
3564 if (log_len > sess->fe->capture_len)
3565 log_len = sess->fe->capture_len;
3566 memcpy(txn->srv_cookie, att_beg, log_len);
3567 txn->srv_cookie[log_len] = 0;
3568 }
3569 }
3570
3571 srv = objt_server(s->target);
3572 /* now check if we need to process it for persistence */
3573 if (!(s->flags & SF_IGNORE_PRST) &&
3574 (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
3575 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
3576 /* assume passive cookie by default */
3577 txn->flags &= ~TX_SCK_MASK;
3578 txn->flags |= TX_SCK_FOUND;
3579
3580 /* If the cookie is in insert mode on a known server, we'll delete
3581 * this occurrence because we'll insert another one later.
3582 * We'll delete it too if the "indirect" option is set and we're in
3583 * a direct access.
3584 */
3585 if (s->be->ck_opts & PR_CK_PSV) {
3586 /* The "preserve" flag was set, we don't want to touch the
3587 * server's cookie.
3588 */
3589 }
3590 else if ((srv && (s->be->ck_opts & PR_CK_INS)) ||
3591 ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) {
3592 /* this cookie must be deleted */
3593 if (prev == hdr_beg && next == hdr_end) {
3594 /* whole header */
3595 http_remove_header(htx, &ctx);
3596 /* note: while both invalid now, <next> and <hdr_end>
3597 * are still equal, so the for() will stop as expected.
3598 */
3599 } else {
3600 /* just remove the value */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003601 int delta = http_del_hdr_value(hdr_beg, hdr_end, &prev, next);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003602 next = prev;
3603 hdr_end += delta;
3604 }
3605 txn->flags &= ~TX_SCK_MASK;
3606 txn->flags |= TX_SCK_DELETED;
3607 /* and go on with next cookie */
3608 }
3609 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) {
3610 /* replace bytes val_beg->val_end with the cookie name associated
3611 * with this server since we know it.
3612 */
3613 int sliding, delta;
3614
3615 ctx.value = ist2(val_beg, val_end - val_beg);
3616 ctx.lws_before = ctx.lws_after = 0;
3617 http_replace_header_value(htx, &ctx, ist2(srv->cookie, srv->cklen));
3618 delta = srv->cklen - (val_end - val_beg);
3619 sliding = (ctx.value.ptr - val_beg);
3620 hdr_beg += sliding;
3621 val_beg += sliding;
3622 next += sliding + delta;
3623 hdr_end += sliding + delta;
3624
3625 txn->flags &= ~TX_SCK_MASK;
3626 txn->flags |= TX_SCK_REPLACED;
3627 }
3628 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) {
3629 /* insert the cookie name associated with this server
3630 * before existing cookie, and insert a delimiter between them..
3631 */
3632 int sliding, delta;
3633 ctx.value = ist2(val_beg, 0);
3634 ctx.lws_before = ctx.lws_after = 0;
3635 http_replace_header_value(htx, &ctx, ist2(srv->cookie, srv->cklen + 1));
3636 delta = srv->cklen + 1;
3637 sliding = (ctx.value.ptr - val_beg);
3638 hdr_beg += sliding;
3639 val_beg += sliding;
3640 next += sliding + delta;
3641 hdr_end += sliding + delta;
3642
3643 val_beg[srv->cklen] = COOKIE_DELIM;
3644 txn->flags &= ~TX_SCK_MASK;
3645 txn->flags |= TX_SCK_REPLACED;
3646 }
3647 }
3648 /* that's done for this cookie, check the next one on the same
Willy Tarreauaa1909e2022-11-14 18:58:35 +01003649 * line when next != hdr_end (which should normally not happen
3650 * with set-cookie2 support removed).
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003651 */
3652 }
3653 }
3654}
3655
Christopher Faulet25a02f62018-10-24 12:00:25 +02003656/*
3657 * Parses the Cache-Control and Pragma request header fields to determine if
3658 * the request may be served from the cache and/or if it is cacheable. Updates
3659 * s->txn->flags.
3660 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003661void http_check_request_for_cacheability(struct stream *s, struct channel *req)
Christopher Faulet25a02f62018-10-24 12:00:25 +02003662{
3663 struct http_txn *txn = s->txn;
3664 struct htx *htx;
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003665 struct http_hdr_ctx ctx = { .blk = NULL };
3666 int pragma_found, cc_found;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003667
3668 if ((txn->flags & (TX_CACHEABLE|TX_CACHE_IGNORE)) == TX_CACHE_IGNORE)
3669 return; /* nothing more to do here */
3670
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003671 htx = htxbuf(&req->buf);
Christopher Faulet25a02f62018-10-24 12:00:25 +02003672 pragma_found = cc_found = 0;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003673
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003674 /* Check "pragma" header for HTTP/1.0 compatibility. */
3675 if (http_find_header(htx, ist("pragma"), &ctx, 1)) {
3676 if (isteqi(ctx.value, ist("no-cache"))) {
3677 pragma_found = 1;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003678 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003679 }
Christopher Faulet25a02f62018-10-24 12:00:25 +02003680
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003681 ctx.blk = NULL;
3682 /* Don't use the cache and don't try to store if we found the
3683 * Authorization header */
3684 if (http_find_header(htx, ist("authorization"), &ctx, 1)) {
3685 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3686 txn->flags |= TX_CACHE_IGNORE;
3687 }
Christopher Faulet25a02f62018-10-24 12:00:25 +02003688
Christopher Faulet25a02f62018-10-24 12:00:25 +02003689
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003690 /* Look for "cache-control" header and iterate over all the values
3691 * until we find one that specifies that caching is possible or not. */
3692 ctx.blk = NULL;
3693 while (http_find_header(htx, ist("cache-control"), &ctx, 0)) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003694 cc_found = 1;
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003695 /* We don't check the values after max-age, max-stale nor min-fresh,
3696 * we simply don't use the cache when they're specified. */
3697 if (istmatchi(ctx.value, ist("max-age")) ||
3698 istmatchi(ctx.value, ist("no-cache")) ||
3699 istmatchi(ctx.value, ist("max-stale")) ||
3700 istmatchi(ctx.value, ist("min-fresh"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003701 txn->flags |= TX_CACHE_IGNORE;
3702 continue;
3703 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003704 if (istmatchi(ctx.value, ist("no-store"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003705 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3706 continue;
3707 }
3708 }
3709
3710 /* RFC7234#5.4:
3711 * When the Cache-Control header field is also present and
3712 * understood in a request, Pragma is ignored.
3713 * When the Cache-Control header field is not present in a
3714 * request, caches MUST consider the no-cache request
3715 * pragma-directive as having the same effect as if
3716 * "Cache-Control: no-cache" were present.
3717 */
3718 if (!cc_found && pragma_found)
3719 txn->flags |= TX_CACHE_IGNORE;
3720}
3721
3722/*
3723 * Check if response is cacheable or not. Updates s->txn->flags.
3724 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003725void http_check_response_for_cacheability(struct stream *s, struct channel *res)
Christopher Faulet25a02f62018-10-24 12:00:25 +02003726{
3727 struct http_txn *txn = s->txn;
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003728 struct http_hdr_ctx ctx = { .blk = NULL };
Christopher Faulet25a02f62018-10-24 12:00:25 +02003729 struct htx *htx;
Remi Tricot-Le Bretoncc9bf2e2020-11-12 11:14:41 +01003730 int has_freshness_info = 0;
3731 int has_validator = 0;
Remi Tricot-Le Breton02f47242023-07-04 17:13:28 +02003732 int has_null_maxage = 0;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003733
3734 if (txn->status < 200) {
3735 /* do not try to cache interim responses! */
3736 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3737 return;
3738 }
3739
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003740 htx = htxbuf(&res->buf);
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003741 /* Check "pragma" header for HTTP/1.0 compatibility. */
3742 if (http_find_header(htx, ist("pragma"), &ctx, 1)) {
3743 if (isteqi(ctx.value, ist("no-cache"))) {
3744 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3745 return;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003746 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003747 }
Christopher Faulet25a02f62018-10-24 12:00:25 +02003748
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003749 /* Look for "cache-control" header and iterate over all the values
3750 * until we find one that specifies that caching is possible or not. */
3751 ctx.blk = NULL;
3752 while (http_find_header(htx, ist("cache-control"), &ctx, 0)) {
3753 if (isteqi(ctx.value, ist("public"))) {
3754 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003755 continue;
3756 }
Remi Tricot-Le Breton02f47242023-07-04 17:13:28 +02003757 /* This max-age might be overridden by a s-maxage directive, do
3758 * not unset the TX_CACHEABLE yet. */
3759 if (isteqi(ctx.value, ist("max-age=0"))) {
3760 has_null_maxage = 1;
3761 continue;
3762 }
3763
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003764 if (isteqi(ctx.value, ist("private")) ||
3765 isteqi(ctx.value, ist("no-cache")) ||
3766 isteqi(ctx.value, ist("no-store")) ||
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003767 isteqi(ctx.value, ist("s-maxage=0"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003768 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003769 continue;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003770 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003771 /* We might have a no-cache="set-cookie" form. */
3772 if (istmatchi(ctx.value, ist("no-cache=\"set-cookie"))) {
3773 txn->flags &= ~TX_CACHE_COOK;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003774 continue;
3775 }
Remi Tricot-Le Bretoncc9bf2e2020-11-12 11:14:41 +01003776
Remi Tricot-Le Breton02f47242023-07-04 17:13:28 +02003777 if (istmatchi(ctx.value, ist("s-maxage"))) {
Remi Tricot-Le Bretoncc9bf2e2020-11-12 11:14:41 +01003778 has_freshness_info = 1;
Remi Tricot-Le Breton02f47242023-07-04 17:13:28 +02003779 has_null_maxage = 0; /* The null max-age is overridden, ignore it */
Remi Tricot-Le Bretoncc9bf2e2020-11-12 11:14:41 +01003780 continue;
3781 }
Remi Tricot-Le Breton02f47242023-07-04 17:13:28 +02003782 if (istmatchi(ctx.value, ist("max-age"))) {
3783 has_freshness_info = 1;
3784 continue;
3785 }
3786 }
3787
3788 /* We had a 'max-age=0' directive but no extra s-maxage, do not cache
3789 * the response. */
3790 if (has_null_maxage) {
3791 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Remi Tricot-Le Bretoncc9bf2e2020-11-12 11:14:41 +01003792 }
3793
3794 /* If no freshness information could be found in Cache-Control values,
3795 * look for an Expires header. */
3796 if (!has_freshness_info) {
3797 ctx.blk = NULL;
3798 has_freshness_info = http_find_header(htx, ist("expires"), &ctx, 0);
Christopher Faulet25a02f62018-10-24 12:00:25 +02003799 }
Remi Tricot-Le Bretoncc9bf2e2020-11-12 11:14:41 +01003800
3801 /* If no freshness information could be found in Cache-Control or Expires
3802 * values, look for an explicit validator. */
3803 if (!has_freshness_info) {
3804 ctx.blk = NULL;
3805 has_validator = 1;
3806 if (!http_find_header(htx, ist("etag"), &ctx, 0)) {
3807 ctx.blk = NULL;
3808 if (!http_find_header(htx, ist("last-modified"), &ctx, 0))
3809 has_validator = 0;
3810 }
3811 }
3812
3813 /* We won't store an entry that has neither a cache validator nor an
3814 * explicit expiration time, as suggested in RFC 7234#3. */
3815 if (!has_freshness_info && !has_validator)
Remi Tricot-Le Breton879debe2023-02-21 11:47:17 +01003816 txn->flags &= ~TX_CACHEABLE;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003817}
3818
Christopher Faulet377c5a52018-10-24 21:21:30 +02003819/*
3820 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
Aurelien DARRAGONf6286142023-12-05 17:54:20 +01003821 * for the current proxy.
Christopher Faulet377c5a52018-10-24 21:21:30 +02003822 *
3823 * It is assumed that the request is either a HEAD, GET, or POST and that the
3824 * uri_auth field is valid.
3825 *
3826 * Returns 1 if stats should be provided, otherwise 0.
3827 */
Aurelien DARRAGONf6286142023-12-05 17:54:20 +01003828static int http_stats_check_uri(struct stream *s, struct http_txn *txn, struct proxy *px)
Christopher Faulet377c5a52018-10-24 21:21:30 +02003829{
Aurelien DARRAGONf6286142023-12-05 17:54:20 +01003830 struct uri_auth *uri_auth = px->uri_auth;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003831 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003832 struct htx_sl *sl;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003833 struct ist uri;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003834
3835 if (!uri_auth)
3836 return 0;
3837
3838 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
3839 return 0;
3840
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003841 htx = htxbuf(&s->req.buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02003842 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003843 uri = htx_sl_req_uri(sl);
Amaury Denoyellec453f952021-07-06 11:40:12 +02003844 if (*uri_auth->uri_prefix == '/') {
3845 struct http_uri_parser parser = http_uri_parser_init(uri);
3846 uri = http_parse_path(&parser);
3847 }
Christopher Faulet377c5a52018-10-24 21:21:30 +02003848
3849 /* check URI size */
3850 if (uri_auth->uri_len > uri.len)
3851 return 0;
3852
3853 if (memcmp(uri.ptr, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
3854 return 0;
3855
3856 return 1;
3857}
3858
3859/* This function prepares an applet to handle the stats. It can deal with the
3860 * "100-continue" expectation, check that admin rules are met for POST requests,
3861 * and program a response message if something was unexpected. It cannot fail
3862 * and always relies on the stats applet to complete the job. It does not touch
3863 * analysers nor counters, which are left to the caller. It does not touch
3864 * s->target which is supposed to already point to the stats applet. The caller
3865 * is expected to have already assigned an appctx to the stream.
3866 */
Aurelien DARRAGONf6286142023-12-05 17:54:20 +01003867static int http_handle_stats(struct stream *s, struct channel *req, struct proxy *px)
Christopher Faulet377c5a52018-10-24 21:21:30 +02003868{
3869 struct stats_admin_rule *stats_admin_rule;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003870 struct session *sess = s->sess;
3871 struct http_txn *txn = s->txn;
3872 struct http_msg *msg = &txn->req;
Aurelien DARRAGONf6286142023-12-05 17:54:20 +01003873 struct uri_auth *uri_auth = px->uri_auth;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003874 const char *h, *lookup, *end;
Willy Tarreau8e7c6e62022-05-18 17:58:02 +02003875 struct appctx *appctx = __sc_appctx(s->scb);
Willy Tarreau91cefca2022-05-03 17:08:29 +02003876 struct show_stat_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx));
Christopher Faulet377c5a52018-10-24 21:21:30 +02003877 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003878 struct htx_sl *sl;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003879
Willy Tarreau41f88522022-05-03 18:39:27 +02003880 appctx->st1 = 0;
Willy Tarreau6ef16482022-05-06 18:07:53 +02003881 ctx->state = STAT_STATE_INIT;
Willy Tarreau91cefca2022-05-03 17:08:29 +02003882 ctx->st_code = STAT_STATUS_INIT;
Aurelien DARRAGONf6286142023-12-05 17:54:20 +01003883 ctx->http_px = px;
Willy Tarreau91cefca2022-05-03 17:08:29 +02003884 ctx->flags |= uri_auth->flags;
3885 ctx->flags |= STAT_FMT_HTML; /* assume HTML mode by default */
Christopher Faulet377c5a52018-10-24 21:21:30 +02003886 if ((msg->flags & HTTP_MSGF_VER_11) && (txn->meth != HTTP_METH_HEAD))
Willy Tarreau91cefca2022-05-03 17:08:29 +02003887 ctx->flags |= STAT_CHUNKED;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003888
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003889 htx = htxbuf(&req->buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02003890 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003891 lookup = HTX_SL_REQ_UPTR(sl) + uri_auth->uri_len;
3892 end = HTX_SL_REQ_UPTR(sl) + HTX_SL_REQ_ULEN(sl);
Christopher Faulet377c5a52018-10-24 21:21:30 +02003893
3894 for (h = lookup; h <= end - 3; h++) {
3895 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02003896 ctx->flags |= STAT_HIDE_DOWN;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003897 break;
3898 }
Amaury Denoyelle91e55ea2021-02-25 14:46:08 +01003899 }
3900
3901 for (h = lookup; h <= end - 9; h++) {
3902 if (memcmp(h, ";no-maint", 9) == 0) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02003903 ctx->flags |= STAT_HIDE_MAINT;
Willy Tarreau3e320362020-10-23 17:28:57 +02003904 break;
3905 }
Christopher Faulet377c5a52018-10-24 21:21:30 +02003906 }
3907
3908 if (uri_auth->refresh) {
3909 for (h = lookup; h <= end - 10; h++) {
3910 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02003911 ctx->flags |= STAT_NO_REFRESH;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003912 break;
3913 }
3914 }
3915 }
3916
3917 for (h = lookup; h <= end - 4; h++) {
3918 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02003919 ctx->flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
Christopher Faulet377c5a52018-10-24 21:21:30 +02003920 break;
3921 }
3922 }
3923
3924 for (h = lookup; h <= end - 6; h++) {
3925 if (memcmp(h, ";typed", 6) == 0) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02003926 ctx->flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
3927 ctx->flags |= STAT_FMT_TYPED;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003928 break;
3929 }
3930 }
3931
Christopher Faulet6338a082019-09-09 15:50:54 +02003932 for (h = lookup; h <= end - 5; h++) {
3933 if (memcmp(h, ";json", 5) == 0) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02003934 ctx->flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
3935 ctx->flags |= STAT_FMT_JSON;
Christopher Faulet6338a082019-09-09 15:50:54 +02003936 break;
3937 }
3938 }
3939
3940 for (h = lookup; h <= end - 12; h++) {
3941 if (memcmp(h, ";json-schema", 12) == 0) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02003942 ctx->flags &= ~STAT_FMT_MASK;
3943 ctx->flags |= STAT_JSON_SCHM;
Christopher Faulet6338a082019-09-09 15:50:54 +02003944 break;
3945 }
3946 }
3947
Christopher Faulet377c5a52018-10-24 21:21:30 +02003948 for (h = lookup; h <= end - 8; h++) {
3949 if (memcmp(h, ";st=", 4) == 0) {
3950 int i;
3951 h += 4;
Willy Tarreau91cefca2022-05-03 17:08:29 +02003952 ctx->st_code = STAT_STATUS_UNKN;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003953 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
3954 if (strncmp(stat_status_codes[i], h, 4) == 0) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02003955 ctx->st_code = i;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003956 break;
3957 }
3958 }
3959 break;
3960 }
3961 }
3962
Willy Tarreau91cefca2022-05-03 17:08:29 +02003963 ctx->scope_str = 0;
3964 ctx->scope_len = 0;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003965 for (h = lookup; h <= end - 8; h++) {
3966 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
3967 int itx = 0;
3968 const char *h2;
3969 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
3970 const char *err;
3971
3972 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
3973 h2 = h;
Willy Tarreau91cefca2022-05-03 17:08:29 +02003974 ctx->scope_str = h2 - HTX_SL_REQ_UPTR(sl);
Christopher Fauleted7a0662019-01-14 11:07:34 +01003975 while (h < end) {
Christopher Faulet377c5a52018-10-24 21:21:30 +02003976 if (*h == ';' || *h == '&' || *h == ' ')
3977 break;
3978 itx++;
3979 h++;
3980 }
3981
3982 if (itx > STAT_SCOPE_TXT_MAXLEN)
3983 itx = STAT_SCOPE_TXT_MAXLEN;
Willy Tarreau91cefca2022-05-03 17:08:29 +02003984 ctx->scope_len = itx;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003985
Willy Tarreau91cefca2022-05-03 17:08:29 +02003986 /* scope_txt = search query, ctx->scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Christopher Faulet377c5a52018-10-24 21:21:30 +02003987 memcpy(scope_txt, h2, itx);
3988 scope_txt[itx] = '\0';
3989 err = invalid_char(scope_txt);
3990 if (err) {
3991 /* bad char in search text => clear scope */
Willy Tarreau91cefca2022-05-03 17:08:29 +02003992 ctx->scope_str = 0;
3993 ctx->scope_len = 0;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003994 }
3995 break;
3996 }
3997 }
3998
3999 /* now check whether we have some admin rules for this request */
4000 list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) {
4001 int ret = 1;
4002
4003 if (stats_admin_rule->cond) {
4004 ret = acl_exec_cond(stats_admin_rule->cond, s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
4005 ret = acl_pass(ret);
4006 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
4007 ret = !ret;
4008 }
4009
4010 if (ret) {
4011 /* no rule, or the rule matches */
Willy Tarreau91cefca2022-05-03 17:08:29 +02004012 ctx->flags |= STAT_ADMIN;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004013 break;
4014 }
4015 }
4016
Christopher Faulet5d45e382019-02-27 15:15:23 +01004017 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
4018 appctx->st0 = STAT_HTTP_HEAD;
4019 else if (txn->meth == HTTP_METH_POST) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02004020 if (ctx->flags & STAT_ADMIN) {
Christopher Faulet377c5a52018-10-24 21:21:30 +02004021 appctx->st0 = STAT_HTTP_POST;
Christopher Fauletbd9e8422019-08-15 22:26:48 +02004022 if (msg->msg_state < HTTP_MSG_DATA)
4023 req->analysers |= AN_REQ_HTTP_BODY;
4024 }
Christopher Faulet377c5a52018-10-24 21:21:30 +02004025 else {
Christopher Faulet5d45e382019-02-27 15:15:23 +01004026 /* POST without admin level */
Willy Tarreau91cefca2022-05-03 17:08:29 +02004027 ctx->flags &= ~STAT_CHUNKED;
4028 ctx->st_code = STAT_STATUS_DENY;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004029 appctx->st0 = STAT_HTTP_LAST;
4030 }
4031 }
4032 else {
Christopher Faulet5d45e382019-02-27 15:15:23 +01004033 /* Unsupported method */
Willy Tarreau91cefca2022-05-03 17:08:29 +02004034 ctx->flags &= ~STAT_CHUNKED;
4035 ctx->st_code = STAT_STATUS_IVAL;
Christopher Faulet5d45e382019-02-27 15:15:23 +01004036 appctx->st0 = STAT_HTTP_LAST;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004037 }
4038
4039 s->task->nice = -32; /* small boost for HTTP statistics */
4040 return 1;
4041}
4042
Christopher Faulet021a8e42021-03-29 10:46:38 +02004043/* This function waits for the message payload at most <time> milliseconds (may
4044 * be set to TICK_ETERNITY). It stops to wait if at least <bytes> bytes of the
4045 * payload are received (0 means no limit). It returns HTTP_RULE_* depending on
4046 * the result:
4047 *
4048 * - HTTP_RULE_RES_CONT when conditions are met to stop waiting
4049 * - HTTP_RULE_RES_YIELD to wait for more data
Ilya Shipitsinb2be9a12021-04-24 13:25:42 +05004050 * - HTTP_RULE_RES_ABRT when a timeout occurred.
Christopher Faulet021a8e42021-03-29 10:46:38 +02004051 * - HTTP_RULE_RES_BADREQ if a parsing error is raised by lower level
Ilya Shipitsinb2be9a12021-04-24 13:25:42 +05004052 * - HTTP_RULE_RES_ERROR if an internal error occurred
Christopher Faulet021a8e42021-03-29 10:46:38 +02004053 *
Ilya Shipitsinb2be9a12021-04-24 13:25:42 +05004054 * If a timeout occurred, this function is responsible to emit the right response
Christopher Faulet021a8e42021-03-29 10:46:38 +02004055 * to the client, depending on the channel (408 on request side, 504 on response
4056 * side). All other errors must be handled by the caller.
4057 */
4058enum rule_result http_wait_for_msg_body(struct stream *s, struct channel *chn,
4059 unsigned int time, unsigned int bytes)
4060{
4061 struct session *sess = s->sess;
4062 struct http_txn *txn = s->txn;
4063 struct http_msg *msg = ((chn->flags & CF_ISRESP) ? &txn->rsp : &txn->req);
4064 struct htx *htx;
4065 enum rule_result ret = HTTP_RULE_RES_CONT;
4066
4067 htx = htxbuf(&chn->buf);
4068
4069 if (htx->flags & HTX_FL_PARSING_ERROR) {
4070 ret = HTTP_RULE_RES_BADREQ;
4071 goto end;
4072 }
4073 if (htx->flags & HTX_FL_PROCESSING_ERROR) {
4074 ret = HTTP_RULE_RES_ERROR;
4075 goto end;
4076 }
4077
4078 /* Do nothing for bodyless and CONNECT requests */
4079 if (txn->meth == HTTP_METH_CONNECT || (msg->flags & HTTP_MSGF_BODYLESS))
4080 goto end;
4081
Christopher Fauletffcffa82023-04-05 10:33:31 +02004082 if (!(chn->flags & CF_ISRESP)) {
Christopher Faulet021a8e42021-03-29 10:46:38 +02004083 if (http_handle_expect_hdr(s, htx, msg) == -1) {
4084 ret = HTTP_RULE_RES_ERROR;
4085 goto end;
4086 }
4087 }
4088
Christopher Faulet2954bcc2023-04-05 10:42:03 +02004089 /* Now we're are waiting for the payload. We just need to know if all
4090 * data have been received or if the buffer is full.
Christopher Faulet021a8e42021-03-29 10:46:38 +02004091 */
Christopher Faulet78335962021-09-23 14:46:32 +02004092 if ((htx->flags & HTX_FL_EOM) ||
4093 htx_get_tail_type(htx) > HTX_BLK_DATA ||
4094 channel_htx_full(chn, htx, global.tune.maxrewrite) ||
Willy Tarreau99615ed2022-05-25 07:29:36 +02004095 sc_waiting_room(chn_prod(chn)))
Christopher Faulet021a8e42021-03-29 10:46:38 +02004096 goto end;
4097
4098 if (bytes) {
4099 struct htx_blk *blk;
4100 unsigned int len = 0;
4101
4102 for (blk = htx_get_first_blk(htx); blk; blk = htx_get_next_blk(htx, blk)) {
4103 if (htx_get_blk_type(blk) != HTX_BLK_DATA)
4104 continue;
4105 len += htx_get_blksz(blk);
4106 if (len >= bytes)
4107 goto end;
4108 }
4109 }
4110
4111 if ((chn->flags & CF_READ_TIMEOUT) || tick_is_expired(chn->analyse_exp, now_ms)) {
4112 if (!(chn->flags & CF_ISRESP))
4113 goto abort_req;
4114 goto abort_res;
4115 }
4116
4117 /* we get here if we need to wait for more data */
Christopher Fauletca5309a2023-04-17 16:17:32 +02004118 if (!(chn_prod(chn)->flags & (SC_FL_EOS|SC_FL_ABRT_DONE))) {
Christopher Faulet021a8e42021-03-29 10:46:38 +02004119 if (!tick_isset(chn->analyse_exp))
4120 chn->analyse_exp = tick_add_ifset(now_ms, time);
4121 ret = HTTP_RULE_RES_YIELD;
4122 }
4123
4124 end:
4125 return ret;
4126
Christopher Fauletf0d80df2023-01-13 10:20:20 +01004127 abort:
4128 http_reply_and_close(s, txn->status, http_error_message(s));
4129 ret = HTTP_RULE_RES_ABRT;
4130 goto end;
4131
Christopher Faulet021a8e42021-03-29 10:46:38 +02004132 abort_req:
4133 txn->status = 408;
4134 if (!(s->flags & SF_ERR_MASK))
4135 s->flags |= SF_ERR_CLITO;
Willy Tarreau4781b152021-04-06 13:53:36 +02004136 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req);
Christopher Faulet021a8e42021-03-29 10:46:38 +02004137 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02004138 _HA_ATOMIC_INC(&sess->listener->counters->failed_req);
Christopher Fauletf0d80df2023-01-13 10:20:20 +01004139 goto abort;
Christopher Faulet021a8e42021-03-29 10:46:38 +02004140
4141 abort_res:
4142 txn->status = 504;
4143 if (!(s->flags & SF_ERR_MASK))
4144 s->flags |= SF_ERR_SRVTO;
Christopher Faulet021a8e42021-03-29 10:46:38 +02004145 stream_inc_http_fail_ctr(s);
Christopher Fauletf0d80df2023-01-13 10:20:20 +01004146 goto abort;
Christopher Faulet021a8e42021-03-29 10:46:38 +02004147}
4148
Willy Tarreaub49672d2022-05-27 10:13:37 +02004149void http_perform_server_redirect(struct stream *s, struct stconn *sc)
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004150{
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004151 struct channel *req = &s->req;
4152 struct channel *res = &s->res;
4153 struct server *srv;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004154 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004155 struct htx_sl *sl;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004156 struct ist path, location;
4157 unsigned int flags;
Amaury Denoyellec453f952021-07-06 11:40:12 +02004158 struct http_uri_parser parser;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004159
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004160 /*
4161 * Create the location
4162 */
4163 chunk_reset(&trash);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004164
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004165 /* 1: add the server's prefix */
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004166 /* special prefix "/" means don't change URL */
4167 srv = __objt_server(s->target);
4168 if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') {
4169 if (!chunk_memcat(&trash, srv->rdr_pfx, srv->rdr_len))
4170 return;
4171 }
4172
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004173 /* 2: add the request Path */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004174 htx = htxbuf(&req->buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02004175 sl = http_get_stline(htx);
Amaury Denoyellec453f952021-07-06 11:40:12 +02004176 parser = http_uri_parser_init(htx_sl_req_uri(sl));
4177 path = http_parse_path(&parser);
Tim Duesterhused526372020-03-05 17:56:33 +01004178 if (!isttest(path))
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004179 return;
4180
4181 if (!chunk_memcat(&trash, path.ptr, path.len))
4182 return;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004183 location = ist2(trash.area, trash.data);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004184
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004185 /*
Ilya Shipitsin4a689da2022-10-29 09:34:32 +05004186 * Create the 302 response
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004187 */
4188 htx = htx_from_buf(&res->buf);
Christopher Faulet617ec5c2023-10-17 11:43:43 +02004189 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_CLEN|HTX_SL_F_BODYLESS);
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004190 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
4191 ist("HTTP/1.1"), ist("302"), ist("Found"));
4192 if (!sl)
4193 goto fail;
4194 sl->info.res.status = 302;
4195 s->txn->status = 302;
4196
4197 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) ||
4198 !htx_add_header(htx, ist("Connection"), ist("close")) ||
4199 !htx_add_header(htx, ist("Content-length"), ist("0")) ||
4200 !htx_add_header(htx, ist("Location"), location))
4201 goto fail;
4202
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004203 if (!htx_add_endof(htx, HTX_BLK_EOH))
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004204 goto fail;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004205
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004206 htx->flags |= HTX_FL_EOM;
Christopher Fauletc20afb82020-01-24 19:16:26 +01004207 htx_to_buf(htx, &res->buf);
Christopher Fauleta72a7e42020-01-28 09:28:11 +01004208 if (!http_forward_proxy_resp(s, 1))
4209 goto fail;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004210
4211 /* return without error. */
Christopher Fauletcfc11c02023-04-13 16:10:23 +02004212 sc_abort(sc);
Christopher Fauletb2b1c3a2023-04-13 16:23:48 +02004213 sc_shutdown(sc);
Christopher Faulet50264b42022-03-30 19:39:30 +02004214 s->conn_err_type = STRM_ET_NONE;
Willy Tarreaub49672d2022-05-27 10:13:37 +02004215 sc->state = SC_ST_CLO;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004216
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004217 if (!(s->flags & SF_ERR_MASK))
4218 s->flags |= SF_ERR_LOCAL;
4219 if (!(s->flags & SF_FINST_MASK))
4220 s->flags |= SF_FINST_C;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004221
4222 /* FIXME: we should increase a counter of redirects per server and per backend. */
4223 srv_inc_sess_ctr(srv);
4224 srv_set_sess_last(srv);
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004225 return;
4226
4227 fail:
4228 /* If an error occurred, remove the incomplete HTTP response from the
4229 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004230 channel_htx_truncate(res, htx);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004231}
4232
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05004233/* This function terminates the request because it was completely analyzed or
Christopher Fauletf2824e62018-10-01 12:12:37 +02004234 * because an error was triggered during the body forwarding.
4235 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004236static void http_end_request(struct stream *s)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004237{
4238 struct channel *chn = &s->req;
4239 struct http_txn *txn = s->txn;
4240
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004241 DBG_TRACE_ENTER(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004242
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004243 if (unlikely(txn->req.msg_state < HTTP_MSG_DONE)) {
4244 DBG_TRACE_DEVEL("waiting end of the request", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004245 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004246 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004247
4248 if (txn->req.msg_state == HTTP_MSG_DONE) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004249 /* No need to read anymore, the request was completely parsed.
4250 * We can shut the read side unless we want to abort_on_close,
4251 * or we have a POST request. The issue with POST requests is
4252 * that some browsers still send a CRLF after the request, and
4253 * this CRLF must be read so that it does not remain in the kernel
4254 * buffers, otherwise a close could cause an RST on some systems
4255 * (eg: Linux).
4256 */
Christopher Faulet769d0e92019-03-22 14:23:18 +01004257 if (!(s->be->options & PR_O_ABRT_CLOSE) && txn->meth != HTTP_METH_POST)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004258 channel_dont_read(chn);
4259
4260 /* if the server closes the connection, we want to immediately react
4261 * and close the socket to save packets and syscalls.
4262 */
Willy Tarreaucb041662022-05-17 19:44:42 +02004263 s->scb->flags |= SC_FL_NOHALF;
Christopher Fauletf2824e62018-10-01 12:12:37 +02004264
4265 /* In any case we've finished parsing the request so we must
4266 * disable Nagle when sending data because 1) we're not going
4267 * to shut this side, and 2) the server is waiting for us to
4268 * send pending data.
4269 */
Christopher Faulet68ef2182023-03-17 15:38:18 +01004270 s->scb->flags |= SC_FL_SND_NEVERWAIT;
Christopher Fauletf2824e62018-10-01 12:12:37 +02004271
Christopher Fauletc2fba3f2023-03-01 16:03:17 +01004272 if (txn->rsp.msg_state < HTTP_MSG_BODY ||
4273 (txn->rsp.msg_state < HTTP_MSG_DONE && s->scb->state != SC_ST_CLO)) {
Christopher Fauletaf124362023-02-14 10:48:02 +01004274 /* The server has not finished to respond and the
4275 * backend SC is not closed, so we don't want to move in
4276 * order not to upset it.
Christopher Fauletd01ce402019-01-02 17:44:13 +01004277 */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004278 DBG_TRACE_DEVEL("waiting end of the response", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletd01ce402019-01-02 17:44:13 +01004279 return;
4280 }
4281
Christopher Fauletf2824e62018-10-01 12:12:37 +02004282 /* When we get here, it means that both the request and the
4283 * response have finished receiving. Depending on the connection
4284 * mode, we'll have to wait for the last bytes to leave in either
4285 * direction, and sometimes for a close to be effective.
4286 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02004287 if (txn->flags & TX_CON_WANT_TUN) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004288 /* Tunnel mode will not have any analyser so it needs to
4289 * poll for reads.
4290 */
Christopher Faulet97047972023-04-17 08:52:10 +02004291 channel_auto_read(&s->req);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004292 txn->req.msg_state = HTTP_MSG_TUNNEL;
Christopher Faulet27c17d12023-04-18 11:01:51 +02004293 if (txn->rsp.msg_state != HTTP_MSG_TUNNEL)
4294 s->res.flags |= CF_WAKE_ONCE;
Christopher Fauletf2824e62018-10-01 12:12:37 +02004295 }
4296 else {
4297 /* we're not expecting any new data to come for this
4298 * transaction, so we can close it.
Christopher Faulet9768c262018-10-22 09:34:31 +02004299 *
4300 * However, there is an exception if the response
4301 * length is undefined. In this case, we need to wait
4302 * the close from the server. The response will be
4303 * switched in TUNNEL mode until the end.
Christopher Fauletf2824e62018-10-01 12:12:37 +02004304 */
4305 if (!(txn->rsp.flags & HTTP_MSGF_XFER_LEN) &&
4306 txn->rsp.msg_state != HTTP_MSG_CLOSED)
Christopher Faulet9768c262018-10-22 09:34:31 +02004307 goto check_channel_flags;
Christopher Fauletf2824e62018-10-01 12:12:37 +02004308
Christopher Faulet64350bb2023-04-13 16:37:37 +02004309 if (!(s->scb->flags & (SC_FL_SHUT_DONE|SC_FL_SHUT_WANTED))) {
Christopher Faulet12762f02023-04-13 15:40:10 +02004310 sc_schedule_abort(s->scf);
Christopher Fauletdf7cd712023-04-13 15:56:26 +02004311 sc_schedule_shutdown(s->scb);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004312 }
4313 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004314 goto check_channel_flags;
4315 }
4316
4317 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
4318 http_msg_closing:
4319 /* nothing else to forward, just waiting for the output buffer
Christopher Fauletdf7cd712023-04-13 15:56:26 +02004320 * to be empty and for the shut_wanted to take effect.
Christopher Fauletf2824e62018-10-01 12:12:37 +02004321 */
4322 if (channel_is_empty(chn)) {
4323 txn->req.msg_state = HTTP_MSG_CLOSED;
4324 goto http_msg_closed;
4325 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004326 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004327 return;
4328 }
4329
4330 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
4331 http_msg_closed:
Christopher Fauletf2824e62018-10-01 12:12:37 +02004332 /* if we don't know whether the server will close, we need to hard close */
4333 if (txn->rsp.flags & HTTP_MSGF_XFER_LEN)
Willy Tarreaucb041662022-05-17 19:44:42 +02004334 s->scb->flags |= SC_FL_NOLINGER; /* we want to close ASAP */
Christopher Fauletf2824e62018-10-01 12:12:37 +02004335 /* see above in MSG_DONE why we only do this in these states */
Christopher Faulet769d0e92019-03-22 14:23:18 +01004336 if (!(s->be->options & PR_O_ABRT_CLOSE))
Christopher Fauletf2824e62018-10-01 12:12:37 +02004337 channel_dont_read(chn);
4338 goto end;
4339 }
4340
4341 check_channel_flags:
4342 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
Christopher Faulet64350bb2023-04-13 16:37:37 +02004343 if (s->scb->flags & (SC_FL_SHUT_DONE|SC_FL_SHUT_WANTED)) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004344 /* if we've just closed an output, let's switch */
4345 txn->req.msg_state = HTTP_MSG_CLOSING;
4346 goto http_msg_closing;
4347 }
4348
4349 end:
4350 chn->analysers &= AN_REQ_FLT_END;
Christopher Faulet198ef8b2020-12-15 13:32:55 +01004351 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
Christopher Faulet68ef2182023-03-17 15:38:18 +01004352 s->scb->flags |= SC_FL_SND_NEVERWAIT;
Christopher Faulet198ef8b2020-12-15 13:32:55 +01004353 if (HAS_REQ_DATA_FILTERS(s))
Christopher Fauletf2824e62018-10-01 12:12:37 +02004354 chn->analysers |= AN_REQ_FLT_XFER_DATA;
Christopher Faulet27c17d12023-04-18 11:01:51 +02004355 else
4356 c_adv(chn, htxbuf(&chn->buf)->data - co_data(chn));
Christopher Faulet198ef8b2020-12-15 13:32:55 +01004357 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004358 channel_auto_close(chn);
4359 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004360 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004361}
4362
4363
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05004364/* This function terminates the response because it was completely analyzed or
Christopher Fauletf2824e62018-10-01 12:12:37 +02004365 * because an error was triggered during the body forwarding.
4366 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004367static void http_end_response(struct stream *s)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004368{
4369 struct channel *chn = &s->res;
4370 struct http_txn *txn = s->txn;
4371
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004372 DBG_TRACE_ENTER(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004373
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004374 if (unlikely(txn->rsp.msg_state < HTTP_MSG_DONE)) {
4375 DBG_TRACE_DEVEL("waiting end of the response", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004376 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004377 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004378
4379 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
4380 /* In theory, we don't need to read anymore, but we must
4381 * still monitor the server connection for a possible close
4382 * while the request is being uploaded, so we don't disable
4383 * reading.
4384 */
4385 /* channel_dont_read(chn); */
4386
Christopher Fauletaf124362023-02-14 10:48:02 +01004387 if (txn->req.msg_state < HTTP_MSG_DONE && s->scf->state != SC_ST_CLO) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004388 /* The client seems to still be sending data, probably
4389 * because we got an error response during an upload.
4390 * We have the choice of either breaking the connection
4391 * or letting it pass through. Let's do the later.
4392 */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004393 DBG_TRACE_DEVEL("waiting end of the request", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004394 return;
4395 }
4396
4397 /* When we get here, it means that both the request and the
4398 * response have finished receiving. Depending on the connection
4399 * mode, we'll have to wait for the last bytes to leave in either
4400 * direction, and sometimes for a close to be effective.
4401 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02004402 if (txn->flags & TX_CON_WANT_TUN) {
Christopher Faulet97047972023-04-17 08:52:10 +02004403 channel_auto_read(&s->res);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004404 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Christopher Faulet27c17d12023-04-18 11:01:51 +02004405 if (txn->req.msg_state != HTTP_MSG_TUNNEL)
4406 s->req.flags |= CF_WAKE_ONCE;
Christopher Fauletf2824e62018-10-01 12:12:37 +02004407 }
4408 else {
4409 /* we're not expecting any new data to come for this
4410 * transaction, so we can close it.
4411 */
Christopher Faulet64350bb2023-04-13 16:37:37 +02004412 if (!(s->scf->flags & (SC_FL_SHUT_DONE|SC_FL_SHUT_WANTED))) {
Christopher Faulet12762f02023-04-13 15:40:10 +02004413 sc_schedule_abort(s->scb);
Christopher Fauletdf7cd712023-04-13 15:56:26 +02004414 sc_schedule_shutdown(s->scf);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004415 }
4416 }
4417 goto check_channel_flags;
4418 }
4419
4420 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
4421 http_msg_closing:
4422 /* nothing else to forward, just waiting for the output buffer
Christopher Fauletdf7cd712023-04-13 15:56:26 +02004423 * to be empty and for the shut_wanted to take effect.
Christopher Fauletf2824e62018-10-01 12:12:37 +02004424 */
4425 if (channel_is_empty(chn)) {
4426 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4427 goto http_msg_closed;
4428 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004429 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004430 return;
4431 }
4432
4433 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
4434 http_msg_closed:
4435 /* drop any pending data */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004436 channel_htx_truncate(&s->req, htxbuf(&s->req.buf));
Christopher Faulet9768c262018-10-22 09:34:31 +02004437 channel_abort(&s->req);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004438 goto end;
4439 }
4440
4441 check_channel_flags:
4442 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
Christopher Faulet64350bb2023-04-13 16:37:37 +02004443 if (s->scf->flags & (SC_FL_SHUT_DONE|SC_FL_SHUT_WANTED)) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004444 /* if we've just closed an output, let's switch */
4445 txn->rsp.msg_state = HTTP_MSG_CLOSING;
4446 goto http_msg_closing;
4447 }
4448
4449 end:
4450 chn->analysers &= AN_RES_FLT_END;
Christopher Faulet198ef8b2020-12-15 13:32:55 +01004451 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
Christopher Faulet68ef2182023-03-17 15:38:18 +01004452 s->scf->flags |= SC_FL_SND_NEVERWAIT;
Christopher Faulet198ef8b2020-12-15 13:32:55 +01004453 if (HAS_RSP_DATA_FILTERS(s))
4454 chn->analysers |= AN_RES_FLT_XFER_DATA;
Christopher Faulet27c17d12023-04-18 11:01:51 +02004455 else
4456 c_adv(chn, htxbuf(&chn->buf)->data - co_data(chn));
Christopher Faulet198ef8b2020-12-15 13:32:55 +01004457 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004458 channel_auto_close(chn);
4459 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004460 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004461}
4462
Christopher Fauletef70e252020-01-28 09:26:19 +01004463/* Forward a response generated by HAProxy (error/redirect/return). This
4464 * function forwards all pending incoming data. If <final> is set to 0, nothing
4465 * more is performed. It is used for 1xx informational messages. Otherwise, the
Christopher Faulet507479b2020-05-15 12:29:46 +02004466 * transaction is terminated and the request is emptied. On success 1 is
Christopher Faulet40e6b552020-06-25 16:04:50 +02004467 * returned. If an error occurred, 0 is returned. If it fails, this function
4468 * only exits. It is the caller responsibility to do the cleanup.
Christopher Fauletef70e252020-01-28 09:26:19 +01004469 */
4470int http_forward_proxy_resp(struct stream *s, int final)
4471{
4472 struct channel *req = &s->req;
4473 struct channel *res = &s->res;
4474 struct htx *htx = htxbuf(&res->buf);
4475 size_t data;
4476
4477 if (final) {
4478 htx->flags |= HTX_FL_PROXY_RESP;
Christopher Faulet507479b2020-05-15 12:29:46 +02004479
Christopher Fauletaab1b672020-11-18 16:44:02 +01004480 if (!htx_is_empty(htx) && !http_eval_after_res_rules(s))
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01004481 return 0;
Christopher Fauletef70e252020-01-28 09:26:19 +01004482
Christopher Fauletd6c48362020-10-19 18:01:38 +02004483 if (s->txn->meth == HTTP_METH_HEAD)
4484 htx_skip_msg_payload(htx);
4485
Christopher Fauletef70e252020-01-28 09:26:19 +01004486 channel_auto_read(req);
4487 channel_abort(req);
Christopher Fauletef70e252020-01-28 09:26:19 +01004488 channel_htx_erase(req, htxbuf(&req->buf));
4489
Christopher Fauletef70e252020-01-28 09:26:19 +01004490 channel_auto_read(res);
4491 channel_auto_close(res);
Christopher Faulet12762f02023-04-13 15:40:10 +02004492 sc_schedule_abort(s->scb);
Christopher Faulet904763f2023-03-22 14:53:11 +01004493 s->scb->flags |= SC_FL_EOI; /* The response is terminated, add EOI */
Christopher Faulet42432f32020-11-20 17:43:16 +01004494 htxbuf(&res->buf)->flags |= HTX_FL_EOM; /* no more data are expected */
Christopher Fauletef70e252020-01-28 09:26:19 +01004495 }
Christopher Fauletcf6898c2020-06-25 15:55:11 +02004496 else {
Christopher Faulet904763f2023-03-22 14:53:11 +01004497 /* Send ASAP informational messages. Rely on SC_FL_EOI for final
Christopher Fauletcf6898c2020-06-25 15:55:11 +02004498 * response.
4499 */
Christopher Faulet5c281d52023-03-16 15:53:28 +01004500 s->scf->flags |= SC_FL_SND_ASAP;
Christopher Fauletcf6898c2020-06-25 15:55:11 +02004501 }
Christopher Fauletef70e252020-01-28 09:26:19 +01004502
4503 data = htx->data - co_data(res);
4504 c_adv(res, data);
4505 htx->first = -1;
4506 res->total += data;
4507 return 1;
4508}
4509
Willy Tarreaub49672d2022-05-27 10:13:37 +02004510void http_server_error(struct stream *s, struct stconn *sc, int err,
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004511 int finst, struct http_reply *msg)
Christopher Faulet0f226952018-10-22 09:29:56 +02004512{
Christopher Faulet72c7d8d2020-01-27 15:32:25 +01004513 http_reply_and_close(s, s->txn->status, msg);
Christopher Faulet0f226952018-10-22 09:29:56 +02004514 if (!(s->flags & SF_ERR_MASK))
4515 s->flags |= err;
4516 if (!(s->flags & SF_FINST_MASK))
4517 s->flags |= finst;
4518}
4519
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004520void http_reply_and_close(struct stream *s, short status, struct http_reply *msg)
Christopher Faulet0f226952018-10-22 09:29:56 +02004521{
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004522 if (!msg) {
4523 channel_htx_truncate(&s->res, htxbuf(&s->res.buf));
4524 goto end;
4525 }
4526
4527 if (http_reply_message(s, msg) == -1) {
4528 /* On error, return a 500 error message, but don't rewrite it if
Christopher Faulet40e6b552020-06-25 16:04:50 +02004529 * it is already an internal error. If it was already a "const"
4530 * 500 error, just fail.
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004531 */
Christopher Faulet40e6b552020-06-25 16:04:50 +02004532 if (s->txn->status == 500) {
4533 if (s->txn->flags & TX_CONST_REPLY)
4534 goto end;
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004535 s->txn->flags |= TX_CONST_REPLY;
Christopher Faulet40e6b552020-06-25 16:04:50 +02004536 }
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004537 s->txn->status = 500;
4538 s->txn->http_reply = NULL;
4539 return http_reply_and_close(s, s->txn->status, http_error_message(s));
4540 }
4541
4542end:
Christopher Faulet2d565002021-09-10 09:17:50 +02004543 /* At this staged, HTTP analysis is finished */
4544 s->req.analysers &= AN_REQ_FLT_END;
4545 s->req.analyse_exp = TICK_ETERNITY;
4546
4547 s->res.analysers &= AN_RES_FLT_END;
4548 s->res.analyse_exp = TICK_ETERNITY;
4549
Christopher Faulet0f226952018-10-22 09:29:56 +02004550 channel_auto_read(&s->req);
4551 channel_abort(&s->req);
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004552 channel_htx_erase(&s->req, htxbuf(&s->req.buf));
Christopher Faulet72c7d8d2020-01-27 15:32:25 +01004553 channel_auto_read(&s->res);
4554 channel_auto_close(&s->res);
Christopher Faulet12762f02023-04-13 15:40:10 +02004555 sc_schedule_abort(s->scb);
Christopher Faulet0f226952018-10-22 09:29:56 +02004556}
4557
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004558struct http_reply *http_error_message(struct stream *s)
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004559{
4560 const int msgnum = http_get_status_idx(s->txn->status);
4561
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004562 if (s->txn->http_reply)
4563 return s->txn->http_reply;
4564 else if (s->be->replies[msgnum])
4565 return s->be->replies[msgnum];
4566 else if (strm_fe(s)->replies[msgnum])
4567 return strm_fe(s)->replies[msgnum];
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004568 else
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004569 return &http_err_replies[msgnum];
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004570}
4571
Christopher Faulet40e6b552020-06-25 16:04:50 +02004572/* Produces an HTX message from an http reply. Depending on the http reply type,
4573 * a, errorfile, an raw file or a log-format string is used. On success, it
4574 * returns 0. If an error occurs -1 is returned. If it fails, this function only
4575 * exits. It is the caller responsibility to do the cleanup.
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004576 */
Christopher Fauletae43b6c2020-05-27 15:24:22 +02004577int http_reply_to_htx(struct stream *s, struct htx *htx, struct http_reply *reply)
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004578{
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004579 struct buffer *errmsg;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004580 struct htx_sl *sl;
4581 struct buffer *body = NULL;
4582 const char *status, *reason, *clen, *ctype;
4583 unsigned int slflags;
4584 int ret = 0;
4585
Christopher Faulete29a97e2020-05-14 14:49:25 +02004586 /*
4587 * - HTTP_REPLY_ERRFILES unexpected here. handled as no payload if so
4588 *
4589 * - HTTP_REPLY_INDIRECT: switch on another reply if defined or handled
4590 * as no payload if NULL. the TXN status code is set with the status
4591 * of the original reply.
4592 */
4593
4594 if (reply->type == HTTP_REPLY_INDIRECT) {
4595 if (reply->body.reply)
4596 reply = reply->body.reply;
4597 }
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004598 if (reply->type == HTTP_REPLY_ERRMSG && !reply->body.errmsg) {
4599 /* get default error message */
4600 if (reply == s->txn->http_reply)
4601 s->txn->http_reply = NULL;
4602 reply = http_error_message(s);
4603 if (reply->type == HTTP_REPLY_INDIRECT) {
4604 if (reply->body.reply)
4605 reply = reply->body.reply;
4606 }
4607 }
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004608
4609 if (reply->type == HTTP_REPLY_ERRMSG) {
4610 /* implicit or explicit error message*/
4611 errmsg = reply->body.errmsg;
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004612 if (errmsg && !b_is_null(errmsg)) {
Christopher Faulet20567362020-05-15 14:52:49 +02004613 if (!htx_copy_msg(htx, errmsg))
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004614 goto fail;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004615 }
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004616 }
4617 else {
4618 /* no payload, file or log-format string */
4619 if (reply->type == HTTP_REPLY_RAW) {
4620 /* file */
4621 body = &reply->body.obj;
4622 }
4623 else if (reply->type == HTTP_REPLY_LOGFMT) {
4624 /* log-format string */
4625 body = alloc_trash_chunk();
4626 if (!body)
4627 goto fail_alloc;
4628 body->data = build_logline(s, body->area, body->size, &reply->body.fmt);
4629 }
4630 /* else no payload */
4631
4632 status = ultoa(reply->status);
4633 reason = http_get_reason(reply->status);
4634 slflags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_CLEN);
4635 if (!body || !b_data(body))
4636 slflags |= HTX_SL_F_BODYLESS;
4637 sl = htx_add_stline(htx, HTX_BLK_RES_SL, slflags, ist("HTTP/1.1"), ist(status), ist(reason));
4638 if (!sl)
4639 goto fail;
4640 sl->info.res.status = reply->status;
4641
4642 clen = (body ? ultoa(b_data(body)) : "0");
4643 ctype = reply->ctype;
4644
4645 if (!LIST_ISEMPTY(&reply->hdrs)) {
4646 struct http_reply_hdr *hdr;
4647 struct buffer *value = alloc_trash_chunk();
4648
4649 if (!value)
4650 goto fail;
4651
4652 list_for_each_entry(hdr, &reply->hdrs, list) {
4653 chunk_reset(value);
4654 value->data = build_logline(s, value->area, value->size, &hdr->value);
4655 if (b_data(value) && !htx_add_header(htx, hdr->name, ist2(b_head(value), b_data(value)))) {
4656 free_trash_chunk(value);
4657 goto fail;
4658 }
4659 chunk_reset(value);
4660 }
4661 free_trash_chunk(value);
4662 }
4663
4664 if (!htx_add_header(htx, ist("content-length"), ist(clen)) ||
4665 (body && b_data(body) && ctype && !htx_add_header(htx, ist("content-type"), ist(ctype))) ||
4666 !htx_add_endof(htx, HTX_BLK_EOH) ||
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004667 (body && b_data(body) && !htx_add_data_atonce(htx, ist2(b_head(body), b_data(body)))))
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004668 goto fail;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004669
4670 htx->flags |= HTX_FL_EOM;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004671 }
4672
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004673 leave:
4674 if (reply->type == HTTP_REPLY_LOGFMT)
4675 free_trash_chunk(body);
4676 return ret;
4677
4678 fail_alloc:
4679 if (!(s->flags & SF_ERR_MASK))
4680 s->flags |= SF_ERR_RESOURCE;
Christopher Faulet97e466c2020-05-15 15:12:47 +02004681 /* fall through */
4682 fail:
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004683 ret = -1;
4684 goto leave;
Christopher Faulet97e466c2020-05-15 15:12:47 +02004685}
4686
4687/* Send an http reply to the client. On success, it returns 0. If an error
Christopher Faulet40e6b552020-06-25 16:04:50 +02004688 * occurs -1 is returned and the response channel is truncated, removing this
4689 * way the faulty reply. This function may fail when the reply is formatted
4690 * (http_reply_to_htx) or when the reply is forwarded
4691 * (http_forward_proxy_resp). On the last case, it is because a
4692 * http-after-response rule fails.
Christopher Faulet97e466c2020-05-15 15:12:47 +02004693 */
4694int http_reply_message(struct stream *s, struct http_reply *reply)
4695{
4696 struct channel *res = &s->res;
4697 struct htx *htx = htx_from_buf(&res->buf);
4698
4699 if (s->txn->status == -1)
4700 s->txn->status = reply->status;
4701 channel_htx_truncate(res, htx);
4702
4703 if (http_reply_to_htx(s, htx, reply) == -1)
4704 goto fail;
4705
4706 htx_to_buf(htx, &s->res.buf);
4707 if (!http_forward_proxy_resp(s, 1))
4708 goto fail;
4709 return 0;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004710
4711 fail:
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004712 channel_htx_truncate(res, htx);
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004713 if (!(s->flags & SF_ERR_MASK))
4714 s->flags |= SF_ERR_PRXCOND;
Christopher Faulet97e466c2020-05-15 15:12:47 +02004715 return -1;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004716}
4717
Christopher Faulet50264b42022-03-30 19:39:30 +02004718/* Return the error message corresponding to s->conn_err_type. It is assumed
Christopher Faulet304cc402019-07-15 15:46:28 +02004719 * that the server side is closed. Note that err_type is actually a
4720 * bitmask, where almost only aborts may be cumulated with other
4721 * values. We consider that aborted operations are more important
4722 * than timeouts or errors due to the fact that nobody else in the
4723 * logs might explain incomplete retries. All others should avoid
4724 * being cumulated. It should normally not be possible to have multiple
4725 * aborts at once, but just in case, the first one in sequence is reported.
4726 * Note that connection errors appearing on the second request of a keep-alive
4727 * connection are not reported since this allows the client to retry.
4728 */
Willy Tarreaub49672d2022-05-27 10:13:37 +02004729void http_return_srv_error(struct stream *s, struct stconn *sc)
Christopher Faulet304cc402019-07-15 15:46:28 +02004730{
Christopher Faulet50264b42022-03-30 19:39:30 +02004731 int err_type = s->conn_err_type;
Christopher Faulet304cc402019-07-15 15:46:28 +02004732
4733 /* set s->txn->status for http_error_message(s) */
Christopher Faulet50264b42022-03-30 19:39:30 +02004734 if (err_type & STRM_ET_QUEUE_ABRT) {
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004735 s->txn->status = -1;
Willy Tarreaub49672d2022-05-27 10:13:37 +02004736 http_server_error(s, sc, SF_ERR_CLICL, SF_FINST_Q, NULL);
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004737 }
Christopher Faulet50264b42022-03-30 19:39:30 +02004738 else if (err_type & STRM_ET_CONN_ABRT) {
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004739 s->txn->status = -1;
Willy Tarreaub49672d2022-05-27 10:13:37 +02004740 http_server_error(s, sc, SF_ERR_CLICL, SF_FINST_C, NULL);
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004741 }
Christopher Faulet50264b42022-03-30 19:39:30 +02004742 else if (err_type & STRM_ET_QUEUE_TO) {
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004743 s->txn->status = 503;
Willy Tarreaub49672d2022-05-27 10:13:37 +02004744 http_server_error(s, sc, SF_ERR_SRVTO, SF_FINST_Q,
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004745 http_error_message(s));
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004746 }
Christopher Faulet50264b42022-03-30 19:39:30 +02004747 else if (err_type & STRM_ET_QUEUE_ERR) {
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004748 s->txn->status = 503;
Willy Tarreaub49672d2022-05-27 10:13:37 +02004749 http_server_error(s, sc, SF_ERR_SRVCL, SF_FINST_Q,
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004750 http_error_message(s));
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004751 }
Christopher Faulet50264b42022-03-30 19:39:30 +02004752 else if (err_type & STRM_ET_CONN_TO) {
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004753 s->txn->status = 503;
Willy Tarreaub49672d2022-05-27 10:13:37 +02004754 http_server_error(s, sc, SF_ERR_SRVTO, SF_FINST_C,
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004755 (s->txn->flags & TX_NOT_FIRST) ? NULL :
4756 http_error_message(s));
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004757 }
Christopher Faulet50264b42022-03-30 19:39:30 +02004758 else if (err_type & STRM_ET_CONN_ERR) {
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004759 s->txn->status = 503;
Willy Tarreaub49672d2022-05-27 10:13:37 +02004760 http_server_error(s, sc, SF_ERR_SRVCL, SF_FINST_C,
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004761 (s->flags & SF_SRV_REUSED) ? NULL :
4762 http_error_message(s));
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004763 }
Christopher Faulet50264b42022-03-30 19:39:30 +02004764 else if (err_type & STRM_ET_CONN_RES) {
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004765 s->txn->status = 503;
Willy Tarreaub49672d2022-05-27 10:13:37 +02004766 http_server_error(s, sc, SF_ERR_RESOURCE, SF_FINST_C,
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004767 (s->txn->flags & TX_NOT_FIRST) ? NULL :
4768 http_error_message(s));
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004769 }
Christopher Faulet50264b42022-03-30 19:39:30 +02004770 else { /* STRM_ET_CONN_OTHER and others */
Christopher Faulet304cc402019-07-15 15:46:28 +02004771 s->txn->status = 500;
Willy Tarreaub49672d2022-05-27 10:13:37 +02004772 http_server_error(s, sc, SF_ERR_INTERNAL, SF_FINST_C,
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004773 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004774 }
4775}
4776
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004777
Christopher Faulet4a28a532019-03-01 11:19:40 +01004778/* Handle Expect: 100-continue for HTTP/1.1 messages if necessary. It returns 0
4779 * on success and -1 on error.
4780 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004781static int http_handle_expect_hdr(struct stream *s, struct htx *htx, struct http_msg *msg)
Christopher Faulet4a28a532019-03-01 11:19:40 +01004782{
4783 /* If we have HTTP/1.1 message with a body and Expect: 100-continue,
4784 * then we must send an HTTP/1.1 100 Continue intermediate response.
4785 */
Christopher Fauletffcffa82023-04-05 10:33:31 +02004786 if (!(msg->flags & HTTP_MSGF_EXPECT_CHECKED) &&
4787 (msg->flags & HTTP_MSGF_VER_11) &&
Christopher Faulet4a28a532019-03-01 11:19:40 +01004788 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
4789 struct ist hdr = { .ptr = "Expect", .len = 6 };
4790 struct http_hdr_ctx ctx;
4791
4792 ctx.blk = NULL;
4793 /* Expect is allowed in 1.1, look for it */
4794 if (http_find_header(htx, hdr, &ctx, 0) &&
4795 unlikely(isteqi(ctx.value, ist2("100-continue", 12)))) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004796 if (http_reply_100_continue(s) == -1)
Christopher Faulet4a28a532019-03-01 11:19:40 +01004797 return -1;
4798 http_remove_header(htx, &ctx);
4799 }
4800 }
Christopher Fauletffcffa82023-04-05 10:33:31 +02004801 msg->flags |= HTTP_MSGF_EXPECT_CHECKED;
Christopher Faulet4a28a532019-03-01 11:19:40 +01004802 return 0;
4803}
4804
Christopher Faulet23a3c792018-11-28 10:01:23 +01004805/* Send a 100-Continue response to the client. It returns 0 on success and -1
4806 * on error. The response channel is updated accordingly.
4807 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004808static int http_reply_100_continue(struct stream *s)
Christopher Faulet23a3c792018-11-28 10:01:23 +01004809{
4810 struct channel *res = &s->res;
4811 struct htx *htx = htx_from_buf(&res->buf);
4812 struct htx_sl *sl;
4813 unsigned int flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|
4814 HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
Christopher Faulet23a3c792018-11-28 10:01:23 +01004815
4816 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
4817 ist("HTTP/1.1"), ist("100"), ist("Continue"));
4818 if (!sl)
4819 goto fail;
4820 sl->info.res.status = 100;
4821
Christopher Faulet1d5ec092019-06-26 14:23:54 +02004822 if (!htx_add_endof(htx, HTX_BLK_EOH))
Christopher Faulet23a3c792018-11-28 10:01:23 +01004823 goto fail;
4824
Christopher Fauleta72a7e42020-01-28 09:28:11 +01004825 if (!http_forward_proxy_resp(s, 0))
4826 goto fail;
Christopher Faulet23a3c792018-11-28 10:01:23 +01004827 return 0;
4828
4829 fail:
4830 /* If an error occurred, remove the incomplete HTTP response from the
4831 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004832 channel_htx_truncate(res, htx);
Christopher Faulet23a3c792018-11-28 10:01:23 +01004833 return -1;
4834}
4835
Christopher Faulet12c51e22018-11-28 15:59:42 +01004836
Christopher Faulet0f226952018-10-22 09:29:56 +02004837/*
4838 * Capture headers from message <htx> according to header list <cap_hdr>, and
4839 * fill the <cap> pointers appropriately.
4840 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004841static void http_capture_headers(struct htx *htx, char **cap, struct cap_hdr *cap_hdr)
Christopher Faulet0f226952018-10-22 09:29:56 +02004842{
4843 struct cap_hdr *h;
4844 int32_t pos;
4845
Christopher Fauleta3f15502019-05-13 15:27:23 +02004846 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet0f226952018-10-22 09:29:56 +02004847 struct htx_blk *blk = htx_get_blk(htx, pos);
4848 enum htx_blk_type type = htx_get_blk_type(blk);
4849 struct ist n, v;
4850
4851 if (type == HTX_BLK_EOH)
4852 break;
4853 if (type != HTX_BLK_HDR)
4854 continue;
4855
4856 n = htx_get_blk_name(htx, blk);
4857
4858 for (h = cap_hdr; h; h = h->next) {
4859 if (h->namelen && (h->namelen == n.len) &&
4860 (strncasecmp(n.ptr, h->name, h->namelen) == 0)) {
4861 if (cap[h->index] == NULL)
4862 cap[h->index] =
4863 pool_alloc(h->pool);
4864
4865 if (cap[h->index] == NULL) {
4866 ha_alert("HTTP capture : out of memory.\n");
4867 break;
4868 }
4869
4870 v = htx_get_blk_value(htx, blk);
Tim Duesterhus2471f5c2021-11-08 09:05:01 +01004871 v = isttrim(v, h->len);
Christopher Faulet0f226952018-10-22 09:29:56 +02004872
4873 memcpy(cap[h->index], v.ptr, v.len);
4874 cap[h->index][v.len]=0;
4875 }
4876 }
4877 }
4878}
4879
Christopher Faulet0b6bdc52018-10-24 11:05:36 +02004880/* Delete a value in a header between delimiters <from> and <next>. The header
4881 * itself is delimited by <start> and <end> pointers. The number of characters
4882 * displaced is returned, and the pointer to the first delimiter is updated if
4883 * required. The function tries as much as possible to respect the following
4884 * principles :
4885 * - replace <from> delimiter by the <next> one unless <from> points to <start>,
4886 * in which case <next> is simply removed
4887 * - set exactly one space character after the new first delimiter, unless there
4888 * are not enough characters in the block being moved to do so.
4889 * - remove unneeded spaces before the previous delimiter and after the new
4890 * one.
4891 *
4892 * It is the caller's responsibility to ensure that :
4893 * - <from> points to a valid delimiter or <start> ;
4894 * - <next> points to a valid delimiter or <end> ;
4895 * - there are non-space chars before <from>.
4896 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004897static int http_del_hdr_value(char *start, char *end, char **from, char *next)
Christopher Faulet0b6bdc52018-10-24 11:05:36 +02004898{
4899 char *prev = *from;
4900
4901 if (prev == start) {
4902 /* We're removing the first value. eat the semicolon, if <next>
4903 * is lower than <end> */
4904 if (next < end)
4905 next++;
4906
4907 while (next < end && HTTP_IS_SPHT(*next))
4908 next++;
4909 }
4910 else {
4911 /* Remove useless spaces before the old delimiter. */
4912 while (HTTP_IS_SPHT(*(prev-1)))
4913 prev--;
4914 *from = prev;
4915
4916 /* copy the delimiter and if possible a space if we're
4917 * not at the end of the line.
4918 */
4919 if (next < end) {
4920 *prev++ = *next++;
4921 if (prev + 1 < next)
4922 *prev++ = ' ';
4923 while (next < end && HTTP_IS_SPHT(*next))
4924 next++;
4925 }
4926 }
4927 memmove(prev, next, end - next);
4928 return (prev - next);
4929}
4930
Christopher Faulet0f226952018-10-22 09:29:56 +02004931
4932/* Formats the start line of the request (without CRLF) and puts it in <str> and
Joseph Herlantc42c0e92018-11-25 10:43:27 -08004933 * return the written length. The line can be truncated if it exceeds <len>.
Christopher Faulet0f226952018-10-22 09:29:56 +02004934 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004935static size_t http_fmt_req_line(const struct htx_sl *sl, char *str, size_t len)
Christopher Faulet0f226952018-10-22 09:29:56 +02004936{
4937 struct ist dst = ist2(str, 0);
4938
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004939 if (istcat(&dst, htx_sl_req_meth(sl), len) == -1)
Christopher Faulet0f226952018-10-22 09:29:56 +02004940 goto end;
4941 if (dst.len + 1 > len)
4942 goto end;
4943 dst.ptr[dst.len++] = ' ';
4944
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004945 if (istcat(&dst, htx_sl_req_uri(sl), len) == -1)
Christopher Faulet0f226952018-10-22 09:29:56 +02004946 goto end;
4947 if (dst.len + 1 > len)
4948 goto end;
4949 dst.ptr[dst.len++] = ' ';
4950
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004951 istcat(&dst, htx_sl_req_vsn(sl), len);
Christopher Faulet0f226952018-10-22 09:29:56 +02004952 end:
4953 return dst.len;
4954}
4955
4956/*
4957 * Print a debug line with a start line.
4958 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004959static void http_debug_stline(const char *dir, struct stream *s, const struct htx_sl *sl)
Christopher Faulet0f226952018-10-22 09:29:56 +02004960{
4961 struct session *sess = strm_sess(s);
4962 int max;
4963
4964 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
4965 dir,
Willy Tarreau88bc8002021-12-06 07:01:02 +00004966 objt_conn(sess->origin) ? (unsigned short)__objt_conn(sess->origin)->handle.fd : -1,
Willy Tarreaufd9417b2022-05-18 16:23:22 +02004967 sc_conn(s->scb) ? (unsigned short)(__sc_conn(s->scb))->handle.fd : -1);
Christopher Faulet0f226952018-10-22 09:29:56 +02004968
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004969 max = HTX_SL_P1_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02004970 UBOUND(max, trash.size - trash.data - 3);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004971 chunk_memcat(&trash, HTX_SL_P1_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02004972 trash.area[trash.data++] = ' ';
4973
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004974 max = HTX_SL_P2_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02004975 UBOUND(max, trash.size - trash.data - 2);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004976 chunk_memcat(&trash, HTX_SL_P2_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02004977 trash.area[trash.data++] = ' ';
4978
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004979 max = HTX_SL_P3_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02004980 UBOUND(max, trash.size - trash.data - 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004981 chunk_memcat(&trash, HTX_SL_P3_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02004982 trash.area[trash.data++] = '\n';
4983
Willy Tarreau2e8ab6b2020-03-14 11:03:20 +01004984 DISGUISE(write(1, trash.area, trash.data));
Christopher Faulet0f226952018-10-22 09:29:56 +02004985}
4986
4987/*
4988 * Print a debug line with a header.
4989 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004990static 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 +02004991{
4992 struct session *sess = strm_sess(s);
4993 int max;
4994
4995 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
4996 dir,
Willy Tarreau88bc8002021-12-06 07:01:02 +00004997 objt_conn(sess->origin) ? (unsigned short)__objt_conn(sess->origin)->handle.fd : -1,
Willy Tarreaufd9417b2022-05-18 16:23:22 +02004998 sc_conn(s->scb) ? (unsigned short)(__sc_conn(s->scb))->handle.fd : -1);
Christopher Faulet0f226952018-10-22 09:29:56 +02004999
5000 max = n.len;
5001 UBOUND(max, trash.size - trash.data - 3);
5002 chunk_memcat(&trash, n.ptr, max);
5003 trash.area[trash.data++] = ':';
5004 trash.area[trash.data++] = ' ';
5005
5006 max = v.len;
5007 UBOUND(max, trash.size - trash.data - 1);
5008 chunk_memcat(&trash, v.ptr, max);
5009 trash.area[trash.data++] = '\n';
5010
Willy Tarreau2e8ab6b2020-03-14 11:03:20 +01005011 DISGUISE(write(1, trash.area, trash.data));
Christopher Faulet0f226952018-10-22 09:29:56 +02005012}
5013
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005014void http_txn_reset_req(struct http_txn *txn)
5015{
Christopher Faulet1aea50e2020-01-17 16:03:53 +01005016 txn->req.flags = 0;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005017 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
5018}
5019
5020void http_txn_reset_res(struct http_txn *txn)
5021{
Christopher Faulet1aea50e2020-01-17 16:03:53 +01005022 txn->rsp.flags = 0;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005023 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
5024}
5025
5026/*
Christopher Faulet75f619a2021-03-08 19:12:58 +01005027 * Create and initialize a new HTTP transaction for stream <s>. This should be
5028 * used before processing any new request. It returns the transaction or NLULL
5029 * on error.
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005030 */
Christopher Faulet75f619a2021-03-08 19:12:58 +01005031struct http_txn *http_create_txn(struct stream *s)
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005032{
Christopher Faulet75f619a2021-03-08 19:12:58 +01005033 struct http_txn *txn;
Willy Tarreaub49672d2022-05-27 10:13:37 +02005034 struct stconn *sc = s->scf;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005035
Christopher Faulet75f619a2021-03-08 19:12:58 +01005036 txn = pool_alloc(pool_head_http_txn);
5037 if (!txn)
5038 return NULL;
5039 s->txn = txn;
5040
Christopher Faulet5eb67f52022-06-22 17:12:05 +02005041 txn->meth = HTTP_METH_OTHER;
Willy Tarreaub49672d2022-05-27 10:13:37 +02005042 txn->flags = ((sc && sc_ep_test(sc, SE_FL_NOT_FIRST)) ? TX_NOT_FIRST : 0);
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005043 txn->status = -1;
Christopher Faulet5cb513a2020-05-13 17:56:56 +02005044 txn->http_reply = NULL;
Christopher Faulete05bf9e2022-03-29 15:23:40 +02005045 txn->l7_buffer = BUF_NULL;
Willy Tarreau8b507582020-02-25 09:35:07 +01005046 write_u32(txn->cache_hash, 0);
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005047
5048 txn->cookie_first_date = 0;
5049 txn->cookie_last_date = 0;
5050
5051 txn->srv_cookie = NULL;
5052 txn->cli_cookie = NULL;
5053 txn->uri = NULL;
5054
5055 http_txn_reset_req(txn);
5056 http_txn_reset_res(txn);
5057
5058 txn->req.chn = &s->req;
5059 txn->rsp.chn = &s->res;
5060
5061 txn->auth.method = HTTP_AUTH_UNKNOWN;
5062
Aurelien DARRAGON5ad2b642022-11-18 09:17:29 +01005063 /* here we don't want to re-initialize s->vars_txn and s->vars_reqres
5064 * variable lists, because they were already initialized upon stream
5065 * creation in stream_new(), and thus may already contain some variables
5066 */
Christopher Faulet75f619a2021-03-08 19:12:58 +01005067
5068 return txn;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005069}
5070
5071/* to be used at the end of a transaction */
Christopher Faulet75f619a2021-03-08 19:12:58 +01005072void http_destroy_txn(struct stream *s)
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005073{
5074 struct http_txn *txn = s->txn;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005075
5076 /* these ones will have been dynamically allocated */
5077 pool_free(pool_head_requri, txn->uri);
5078 pool_free(pool_head_capture, txn->cli_cookie);
5079 pool_free(pool_head_capture, txn->srv_cookie);
Tim Duesterhusa17e6622020-03-05 20:19:02 +01005080 pool_free(pool_head_uniqueid, s->unique_id.ptr);
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005081
Tim Duesterhusa17e6622020-03-05 20:19:02 +01005082 s->unique_id = IST_NULL;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005083 txn->uri = NULL;
5084 txn->srv_cookie = NULL;
5085 txn->cli_cookie = NULL;
5086
Christopher Faulet59399252019-11-07 14:27:52 +01005087 if (!LIST_ISEMPTY(&s->vars_txn.head))
5088 vars_prune(&s->vars_txn, s->sess, s);
5089 if (!LIST_ISEMPTY(&s->vars_reqres.head))
5090 vars_prune(&s->vars_reqres, s->sess, s);
Christopher Faulet75f619a2021-03-08 19:12:58 +01005091
Christopher Faulete05bf9e2022-03-29 15:23:40 +02005092 b_free(&txn->l7_buffer);
5093
Christopher Faulet75f619a2021-03-08 19:12:58 +01005094 pool_free(pool_head_http_txn, txn);
5095 s->txn = NULL;
Christopher Faulet59399252019-11-07 14:27:52 +01005096}
5097
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005098
Christopher Faulet71236de2023-01-13 09:06:38 +01005099void http_set_term_flags(struct stream *s)
5100{
5101 if (!(s->flags & SF_ERR_MASK))
5102 s->flags |= SF_ERR_PRXCOND;
5103
5104 if (!(s->flags & SF_FINST_MASK)) {
5105 if (s->scb->state == SC_ST_INI) {
5106 /* Before any connection attempt on the server side, we
5107 * are still in the request analysis. Just take case to
5108 * detect tarpit error
5109 */
5110 if (s->req.analysers & AN_REQ_HTTP_TARPIT)
5111 s->flags |= SF_FINST_T;
5112 else
5113 s->flags |= SF_FINST_R;
5114 }
5115 else if (s->scb->state == SC_ST_QUE)
5116 s->flags |= SF_FINST_Q;
5117 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)) {
5118 if (unlikely(objt_applet(s->target))) {
5119 s->flags |= SF_FINST_R;
5120 }
5121 else
5122 s->flags |= SF_FINST_C;
5123 }
5124 else {
5125 if (s->txn->rsp.msg_state < HTTP_MSG_DATA) {
5126 /* We are still processing the response headers */
5127 s->flags |= SF_FINST_H;
5128 }
Christopher Faulet5b74f992023-01-26 19:02:07 +01005129 // (res == (done|closing|closed)) & (res->flags & shutw)
5130 else if (s->txn->rsp.msg_state >= HTTP_MSG_DONE && s->txn->rsp.msg_state < HTTP_MSG_TUNNEL &&
Christopher Faulet71236de2023-01-13 09:06:38 +01005131 (s->flags & (SF_ERR_CLITO|SF_ERR_CLICL))) {
5132 /* A client error was reported and we are
5133 * transmitting the last block of data
5134 */
5135 s->flags |= SF_FINST_L;
5136 }
5137 else {
5138 /* Otherwise we are in DATA phase on both sides */
5139 s->flags |= SF_FINST_D;
5140 }
5141 }
5142 }
5143}
5144
5145
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005146DECLARE_POOL(pool_head_http_txn, "http_txn", sizeof(struct http_txn));
Christopher Faulet0f226952018-10-22 09:29:56 +02005147
Christopher Fauletf4eb75d2018-10-11 15:55:07 +02005148/*
5149 * Local variables:
5150 * c-indent-level: 8
5151 * c-basic-offset: 8
5152 * End:
5153 */