blob: 6b2d7f0f3e715921a4c98881495b1b7d90a3b32e [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
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020069static int http_stats_check_uri(struct stream *s, struct http_txn *txn, struct proxy *backend);
70static int http_handle_stats(struct stream *s, struct channel *req);
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 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200449 http_handle_stats(s, req);
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
988 * transfer. */
989 goto return_srv_abort;
990 }
991
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200992 http_end_request(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200993 if (!(req->analysers & an_bit)) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100994 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200995 return 1;
996 }
997
998 /* If "option abortonclose" is set on the backend, we want to monitor
999 * the client's connection and forward any shutdown notification to the
1000 * server, which will decide whether to close or to go on processing the
1001 * request. We only do that in tunnel mode, and not in other modes since
1002 * it can be abused to exhaust source ports. */
Christopher Faulet769d0e92019-03-22 14:23:18 +01001003 if (s->be->options & PR_O_ABRT_CLOSE) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001004 channel_auto_read(req);
Christopher Fauletca5309a2023-04-17 16:17:32 +02001005 if ((s->scf->flags & (SC_FL_ABRT_DONE|SC_FL_EOS)) && !(txn->flags & TX_CON_WANT_TUN))
Willy Tarreaucb041662022-05-17 19:44:42 +02001006 s->scb->flags |= SC_FL_NOLINGER;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001007 channel_auto_close(req);
1008 }
1009 else if (s->txn->meth == HTTP_METH_POST) {
1010 /* POST requests may require to read extra CRLF sent by broken
1011 * browsers and which could cause an RST to be sent upon close
1012 * on some systems (eg: Linux). */
1013 channel_auto_read(req);
1014 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001015 DBG_TRACE_DEVEL("waiting for the end of the HTTP txn",
1016 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001017 return 0;
1018
1019 missing_data_or_waiting:
1020 /* stop waiting for data if the input is closed before the end */
Christopher Fauletca5309a2023-04-17 16:17:32 +02001021 if (msg->msg_state < HTTP_MSG_ENDING && (s->scf->flags & (SC_FL_ABRT_DONE|SC_FL_EOS)))
Christopher Faulet93e02d82019-03-08 14:18:50 +01001022 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001023
1024 waiting:
1025 /* waiting for the last bits to leave the buffer */
Christopher Faulet64350bb2023-04-13 16:37:37 +02001026 if (s->scb->flags & SC_FL_SHUT_DONE)
Christopher Faulet93e02d82019-03-08 14:18:50 +01001027 goto return_srv_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001028
1029 /* When TE: chunked is used, we need to get there again to parse remaining
1030 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
1031 * And when content-length is used, we never want to let the possible
1032 * shutdown be forwarded to the other side, as the state machine will
1033 * take care of it once the client responds. It's also important to
1034 * prevent TIME_WAITs from accumulating on the backend side, and for
1035 * HTTP/2 where the last frame comes with a shutdown.
1036 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001037 if (msg->flags & HTTP_MSGF_XFER_LEN)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001038 channel_dont_close(req);
1039
1040 /* We know that more data are expected, but we couldn't send more that
Christopher Faulet84d3ef92023-03-17 15:45:58 +01001041 * what we did. So we always set the SC_FL_SND_EXP_MORE flag so that the
Christopher Faulete0768eb2018-10-03 16:38:02 +02001042 * system knows it must not set a PUSH on this first part. Interactive
1043 * modes are already handled by the stream sock layer. We must not do
1044 * this in content-length mode because it could present the MSG_MORE
1045 * flag with the last block of forwarded data, which would cause an
1046 * additional delay to be observed by the receiver.
1047 */
Christopher Faulet2151cdd2020-07-22 16:34:59 +02001048 if (HAS_REQ_DATA_FILTERS(s))
Christopher Faulet84d3ef92023-03-17 15:45:58 +01001049 s->scb->flags |= SC_FL_SND_EXP_MORE;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001050
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001051 DBG_TRACE_DEVEL("waiting for more data to forward",
1052 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001053 return 0;
1054
Christopher Faulet93e02d82019-03-08 14:18:50 +01001055 return_cli_abort:
Willy Tarreau4781b152021-04-06 13:53:36 +02001056 _HA_ATOMIC_INC(&sess->fe->fe_counters.cli_aborts);
1057 _HA_ATOMIC_INC(&s->be->be_counters.cli_aborts);
William Lallemand36119de2021-03-08 15:26:48 +01001058 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001059 _HA_ATOMIC_INC(&sess->listener->counters->cli_aborts);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001060 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001061 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.cli_aborts);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001062 if (!(s->flags & SF_ERR_MASK))
Christopher Fauletf2b02cf2023-01-13 11:02:28 +01001063 s->flags |= ((req->flags & CF_READ_TIMEOUT) ? SF_ERR_CLITO : SF_ERR_CLICL);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001064 status = 400;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001065 goto return_prx_cond;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001066
1067 return_srv_abort:
Willy Tarreau4781b152021-04-06 13:53:36 +02001068 _HA_ATOMIC_INC(&sess->fe->fe_counters.srv_aborts);
1069 _HA_ATOMIC_INC(&s->be->be_counters.srv_aborts);
William Lallemand36119de2021-03-08 15:26:48 +01001070 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001071 _HA_ATOMIC_INC(&sess->listener->counters->srv_aborts);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001072 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001073 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.srv_aborts);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001074 if (!(s->flags & SF_ERR_MASK))
Christopher Fauletf2b02cf2023-01-13 11:02:28 +01001075 s->flags |= ((req->flags & CF_WRITE_TIMEOUT) ? SF_ERR_SRVTO : SF_ERR_SRVCL);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001076 status = 502;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001077 goto return_prx_cond;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001078
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001079 return_int_err:
1080 if (!(s->flags & SF_ERR_MASK))
1081 s->flags |= SF_ERR_INTERNAL;
Willy Tarreau4781b152021-04-06 13:53:36 +02001082 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
1083 _HA_ATOMIC_INC(&s->be->be_counters.internal_errors);
William Lallemand36119de2021-03-08 15:26:48 +01001084 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001085 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001086 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001087 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.internal_errors);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001088 status = 500;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001089 goto return_prx_cond;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001090
Christopher Faulet93e02d82019-03-08 14:18:50 +01001091 return_bad_req:
Willy Tarreau4781b152021-04-06 13:53:36 +02001092 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req);
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->failed_req);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001095 status = 400;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001096 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001097
Christopher Fauletb8a53712019-12-16 11:29:38 +01001098 return_prx_cond:
Christopher Faulet9768c262018-10-22 09:34:31 +02001099 if (txn->status > 0) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001100 /* Note: we don't send any error if some data were already sent */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001101 http_reply_and_close(s, txn->status, NULL);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001102 } else {
Christopher Faulet93e02d82019-03-08 14:18:50 +01001103 txn->status = status;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001104 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001105 }
Christopher Fauletf2b02cf2023-01-13 11:02:28 +01001106 http_set_term_flags(s);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001107 DBG_TRACE_DEVEL("leaving on error ",
1108 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001109 return 0;
1110}
1111
Willy Tarreau4596fe22022-05-17 19:07:51 +02001112/* Reset the stream and the backend stream connector to a situation suitable for attemption connection */
Olivier Houcharda254a372019-04-05 15:30:12 +02001113/* Returns 0 if we can attempt to retry, -1 otherwise */
Willy Tarreaub49672d2022-05-27 10:13:37 +02001114static __inline int do_l7_retry(struct stream *s, struct stconn *sc)
Olivier Houcharda254a372019-04-05 15:30:12 +02001115{
Christopher Faulet9f5382e2021-05-21 13:46:14 +02001116 struct channel *req, *res;
1117 int co_data;
Olivier Houcharda254a372019-04-05 15:30:12 +02001118
Christopher Faulet731c8e62022-03-29 16:08:44 +02001119 if (s->conn_retries >= s->be->conn_retries)
Christopher Faulet552601d2021-05-26 10:31:06 +02001120 return -1;
Christopher Faulet41ade742023-03-01 15:45:39 +01001121 s->conn_retries++;
Christopher Faulete763c8c2021-05-05 18:23:59 +02001122 if (objt_server(s->target)) {
1123 if (s->flags & SF_CURR_SESS) {
1124 s->flags &= ~SF_CURR_SESS;
1125 _HA_ATOMIC_DEC(&__objt_server(s->target)->cur_sess);
1126 }
Willy Tarreau4781b152021-04-06 13:53:36 +02001127 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.retries);
Christopher Faulete763c8c2021-05-05 18:23:59 +02001128 }
Willy Tarreau4781b152021-04-06 13:53:36 +02001129 _HA_ATOMIC_INC(&s->be->be_counters.retries);
Willy Tarreau223995e2019-05-04 10:38:31 +02001130
Christopher Faulet9f5382e2021-05-21 13:46:14 +02001131 req = &s->req;
1132 res = &s->res;
Christopher Faulet87633c32023-04-03 18:32:50 +02001133
Olivier Houcharda254a372019-04-05 15:30:12 +02001134 /* Remove any write error from the request, and read error from the response */
Christopher Fauletca5309a2023-04-17 16:17:32 +02001135 s->scf->flags &= ~(SC_FL_EOS|SC_FL_ABRT_DONE|SC_FL_ABRT_WANTED);
Christopher Faulet87633c32023-04-03 18:32:50 +02001136 req->flags &= ~CF_WRITE_TIMEOUT;
1137 res->flags &= ~(CF_READ_TIMEOUT | CF_READ_EVENT);
Christopher Faulet7bf46bb2022-01-04 10:56:03 +01001138 res->analysers &= AN_RES_FLT_END;
Christopher Faulet50264b42022-03-30 19:39:30 +02001139 s->conn_err_type = STRM_ET_NONE;
Christopher Fauletae024ce2022-03-29 19:02:31 +02001140 s->flags &= ~(SF_CONN_EXP | SF_ERR_MASK | SF_FINST_MASK);
1141 s->conn_exp = TICK_ETERNITY;
Olivier Houchard4bd58672019-07-12 16:16:59 +02001142 stream_choose_redispatch(s);
Olivier Houcharda254a372019-04-05 15:30:12 +02001143 res->to_forward = 0;
1144 res->analyse_exp = TICK_ETERNITY;
1145 res->total = 0;
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +01001146
Christopher Fauleta1d14a72023-04-14 10:42:08 +02001147 s->scb->flags &= ~(SC_FL_ERROR|SC_FL_SHUT_DONE|SC_FL_SHUT_WANTED);
Willy Tarreau19c65a92022-05-27 08:49:24 +02001148 if (sc_reset_endp(s->scb) < 0) {
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +01001149 if (!(s->flags & SF_ERR_MASK))
1150 s->flags |= SF_ERR_INTERNAL;
1151 return -1;
1152 }
Olivier Houcharda254a372019-04-05 15:30:12 +02001153
Christopher Faulet9f5382e2021-05-21 13:46:14 +02001154 b_free(&req->buf);
1155 /* Swap the L7 buffer with the channel buffer */
1156 /* We know we stored the co_data as b_data, so get it there */
Christopher Faulete05bf9e2022-03-29 15:23:40 +02001157 co_data = b_data(&s->txn->l7_buffer);
1158 b_set_data(&s->txn->l7_buffer, b_size(&s->txn->l7_buffer));
1159 b_xfer(&req->buf, &s->txn->l7_buffer, b_data(&s->txn->l7_buffer));
Christopher Faulet9f5382e2021-05-21 13:46:14 +02001160 co_set_data(req, co_data);
Christopher Faulet5b82cc52020-10-12 15:18:50 +02001161
Ilya Shipitsinacf84592021-02-06 22:29:08 +05001162 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 +02001163
Olivier Houcharda254a372019-04-05 15:30:12 +02001164 b_reset(&res->buf);
1165 co_set_data(res, 0);
1166 return 0;
1167}
1168
Christopher Faulete0768eb2018-10-03 16:38:02 +02001169/* This stream analyser waits for a complete HTTP response. It returns 1 if the
1170 * processing can continue on next analysers, or zero if it either needs more
1171 * data or wants to immediately abort the response (eg: timeout, error, ...). It
1172 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->res.analysers
1173 * when it has nothing left to do, and may remove any analyser when it wants to
1174 * abort.
1175 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001176int http_wait_for_response(struct stream *s, struct channel *rep, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001177{
Christopher Faulet9768c262018-10-22 09:34:31 +02001178 /*
1179 * We will analyze a complete HTTP response to check the its syntax.
1180 *
1181 * Once the start line and all headers are received, we may perform a
1182 * capture of the error (if any), and we will set a few fields. We also
1183 * logging and finally headers capture.
1184 */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001185 struct session *sess = s->sess;
1186 struct http_txn *txn = s->txn;
1187 struct http_msg *msg = &txn->rsp;
Christopher Faulet9768c262018-10-22 09:34:31 +02001188 struct htx *htx;
Christopher Faulet61608322018-11-23 16:23:45 +01001189 struct connection *srv_conn;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001190 struct htx_sl *sl;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001191 int n;
1192
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001193 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001194
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001195 htx = htxbuf(&rep->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001196
Willy Tarreau4236f032019-03-05 10:43:32 +01001197 /* Parsing errors are caught here */
1198 if (htx->flags & HTX_FL_PARSING_ERROR)
1199 goto return_bad_res;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001200 if (htx->flags & HTX_FL_PROCESSING_ERROR)
1201 goto return_int_err;
Willy Tarreau4236f032019-03-05 10:43:32 +01001202
Christopher Faulete0768eb2018-10-03 16:38:02 +02001203 /*
1204 * Now we quickly check if we have found a full valid response.
1205 * If not so, we check the FD and buffer states before leaving.
1206 * A full response is indicated by the fact that we have seen
1207 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
1208 * responses are checked first.
1209 *
1210 * Depending on whether the client is still there or not, we
1211 * may send an error response back or not. Note that normally
1212 * we should only check for HTTP status there, and check I/O
1213 * errors somewhere else.
1214 */
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001215 next_one:
Christopher Faulet29f17582019-05-23 11:03:26 +02001216 if (unlikely(htx_is_empty(htx) || htx->first == -1)) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001217 /* 1: have we encountered a read error ? */
Christopher Fauletd7bac882023-04-14 12:05:55 +02001218 if (s->scb->flags & SC_FL_ERROR) {
Willy Tarreaufd9417b2022-05-18 16:23:22 +02001219 struct connection *conn = sc_conn(s->scb);
Olivier Houchard865d8392019-05-03 22:46:27 +02001220
Christopher Faulet6f78ac52023-03-01 15:47:18 +01001221
1222 if ((txn->flags & TX_L7_RETRY) &&
1223 (s->be->retry_type & PR_RE_DISCONNECTED) &&
1224 (!conn || conn->err_code != CO_ER_SSL_EARLY_FAILED)) {
1225 if (co_data(rep) || do_l7_retry(s, s->scb) == 0)
1226 return 0;
1227 }
1228
1229 /* Perform a L7 retry on empty response or because server refuses the early data. */
Christopher Faulete05bf9e2022-03-29 15:23:40 +02001230 if ((txn->flags & TX_L7_RETRY) &&
Christopher Fauletd9769232021-05-26 12:15:37 +02001231 (s->be->retry_type & PR_RE_EARLY_ERROR) &&
1232 conn && conn->err_code == CO_ER_SSL_EARLY_FAILED &&
Willy Tarreau7cb9e6c2022-05-17 19:40:40 +02001233 do_l7_retry(s, s->scb) == 0) {
Christopher Fauletd9769232021-05-26 12:15:37 +02001234 DBG_TRACE_DEVEL("leaving on L7 retry",
1235 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
1236 return 0;
1237 }
1238
Olivier Houchard6db16992019-05-17 15:40:49 +02001239 if (txn->flags & TX_NOT_FIRST)
1240 goto abort_keep_alive;
1241
Willy Tarreau4781b152021-04-06 13:53:36 +02001242 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001243 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001244 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001245 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001246 }
1247
Christopher Fauletd9769232021-05-26 12:15:37 +02001248 /* if the server refused the early data, just send a 425 */
1249 if (conn && conn->err_code == CO_ER_SSL_EARLY_FAILED)
Olivier Houchard865d8392019-05-03 22:46:27 +02001250 txn->status = 425;
Christopher Fauletd9769232021-05-26 12:15:37 +02001251 else {
1252 txn->status = 502;
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001253 stream_inc_http_fail_ctr(s);
Christopher Fauletd9769232021-05-26 12:15:37 +02001254 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001255
Willy Tarreaucb041662022-05-17 19:44:42 +02001256 s->scb->flags |= SC_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001257 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001258
1259 if (!(s->flags & SF_ERR_MASK))
1260 s->flags |= SF_ERR_SRVCL;
Christopher Faulet4a66c942023-01-13 09:43:21 +01001261 http_set_term_flags(s);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001262 DBG_TRACE_DEVEL("leaving on error",
1263 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001264 return 0;
1265 }
1266
Christopher Faulet9768c262018-10-22 09:34:31 +02001267 /* 2: read timeout : return a 504 to the client. */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001268 else if (rep->flags & CF_READ_TIMEOUT) {
Christopher Faulete05bf9e2022-03-29 15:23:40 +02001269 if ((txn->flags & TX_L7_RETRY) &&
Olivier Houcharda254a372019-04-05 15:30:12 +02001270 (s->be->retry_type & PR_RE_TIMEOUT)) {
Willy Tarreau7cb9e6c2022-05-17 19:40:40 +02001271 if (co_data(rep) || do_l7_retry(s, s->scb) == 0) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001272 DBG_TRACE_DEVEL("leaving on L7 retry",
1273 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharda254a372019-04-05 15:30:12 +02001274 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001275 }
Olivier Houcharda254a372019-04-05 15:30:12 +02001276 }
Willy Tarreau4781b152021-04-06 13:53:36 +02001277 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001278 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001279 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001280 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001281 }
1282
Christopher Faulete0768eb2018-10-03 16:38:02 +02001283 txn->status = 504;
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001284 stream_inc_http_fail_ctr(s);
Willy Tarreaucb041662022-05-17 19:44:42 +02001285 s->scb->flags |= SC_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001286 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001287
1288 if (!(s->flags & SF_ERR_MASK))
1289 s->flags |= SF_ERR_SRVTO;
Christopher Faulet4a66c942023-01-13 09:43:21 +01001290 http_set_term_flags(s);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001291 DBG_TRACE_DEVEL("leaving on error",
1292 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001293 return 0;
1294 }
1295
Christopher Faulet9768c262018-10-22 09:34:31 +02001296 /* 3: client abort with an abortonclose */
Christopher Fauletca5309a2023-04-17 16:17:32 +02001297 else if ((s->scb->flags & (SC_FL_EOS|SC_FL_ABRT_DONE)) && (s->scb->flags & SC_FL_SHUT_DONE) &&
1298 (s->scf->flags & (SC_FL_EOS|SC_FL_ABRT_DONE))) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001299 _HA_ATOMIC_INC(&sess->fe->fe_counters.cli_aborts);
1300 _HA_ATOMIC_INC(&s->be->be_counters.cli_aborts);
William Lallemand36119de2021-03-08 15:26:48 +01001301 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001302 _HA_ATOMIC_INC(&sess->listener->counters->cli_aborts);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001303 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001304 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.cli_aborts);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001305
Christopher Faulete0768eb2018-10-03 16:38:02 +02001306 txn->status = 400;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001307 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001308
1309 if (!(s->flags & SF_ERR_MASK))
1310 s->flags |= SF_ERR_CLICL;
Christopher Faulet4a66c942023-01-13 09:43:21 +01001311 http_set_term_flags(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001312
1313 /* process_stream() will take care of the error */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001314 DBG_TRACE_DEVEL("leaving on error",
1315 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001316 return 0;
1317 }
1318
Christopher Faulet9768c262018-10-22 09:34:31 +02001319 /* 4: close from server, capture the response if the server has started to respond */
Christopher Fauletca5309a2023-04-17 16:17:32 +02001320 else if (s->scb->flags & (SC_FL_EOS|SC_FL_ABRT_DONE)) {
Christopher Faulete05bf9e2022-03-29 15:23:40 +02001321 if ((txn->flags & TX_L7_RETRY) &&
Olivier Houcharda254a372019-04-05 15:30:12 +02001322 (s->be->retry_type & PR_RE_DISCONNECTED)) {
Willy Tarreau7cb9e6c2022-05-17 19:40:40 +02001323 if (co_data(rep) || do_l7_retry(s, s->scb) == 0) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001324 DBG_TRACE_DEVEL("leaving on L7 retry",
1325 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharda254a372019-04-05 15:30:12 +02001326 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001327 }
Olivier Houcharda254a372019-04-05 15:30:12 +02001328 }
1329
Olivier Houchard6db16992019-05-17 15:40:49 +02001330 if (txn->flags & TX_NOT_FIRST)
1331 goto abort_keep_alive;
1332
Willy Tarreau4781b152021-04-06 13:53:36 +02001333 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001334 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001335 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001336 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001337 }
1338
Christopher Faulete0768eb2018-10-03 16:38:02 +02001339 txn->status = 502;
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001340 stream_inc_http_fail_ctr(s);
Willy Tarreaucb041662022-05-17 19:44:42 +02001341 s->scb->flags |= SC_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001342 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001343
1344 if (!(s->flags & SF_ERR_MASK))
1345 s->flags |= SF_ERR_SRVCL;
Christopher Faulet4a66c942023-01-13 09:43:21 +01001346 http_set_term_flags(s);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001347 DBG_TRACE_DEVEL("leaving on error",
1348 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001349 return 0;
1350 }
1351
Christopher Faulet9768c262018-10-22 09:34:31 +02001352 /* 5: write error to client (we don't send any message then) */
Christopher Faulet2e56a732023-01-26 16:18:09 +01001353 else if (sc_ep_test(s->scf, SE_FL_ERR_PENDING)) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001354 if (txn->flags & TX_NOT_FIRST)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001355 goto abort_keep_alive;
1356
Willy Tarreau4781b152021-04-06 13:53:36 +02001357 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001358 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001359 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001360 rep->analysers &= AN_RES_FLT_END;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001361
1362 if (!(s->flags & SF_ERR_MASK))
1363 s->flags |= SF_ERR_CLICL;
Christopher Faulet4a66c942023-01-13 09:43:21 +01001364 http_set_term_flags(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001365
1366 /* process_stream() will take care of the error */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001367 DBG_TRACE_DEVEL("leaving on error",
1368 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001369 return 0;
1370 }
1371
1372 channel_dont_close(rep);
Christopher Faulet9a790f62023-03-16 14:40:03 +01001373 s->scb->flags |= SC_FL_RCV_ONCE; /* try to get back here ASAP */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001374 DBG_TRACE_DEVEL("waiting for more data",
1375 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001376 return 0;
1377 }
1378
1379 /* More interesting part now : we know that we have a complete
1380 * response which at least looks like HTTP. We have an indicator
1381 * of each header's length, so we can parse them quickly.
1382 */
Christopher Faulet29f17582019-05-23 11:03:26 +02001383 BUG_ON(htx_get_first_type(htx) != HTX_BLK_RES_SL);
Christopher Faulet297fbb42019-05-13 14:41:27 +02001384 sl = http_get_stline(htx);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001385
Christopher Faulet1f08bff2021-05-26 13:14:39 +02001386 /* Perform a L7 retry because of the status code */
Christopher Faulete05bf9e2022-03-29 15:23:40 +02001387 if ((txn->flags & TX_L7_RETRY) &&
Christopher Faulet1f08bff2021-05-26 13:14:39 +02001388 l7_status_match(s->be, sl->info.res.status) &&
Willy Tarreau7cb9e6c2022-05-17 19:40:40 +02001389 do_l7_retry(s, s->scb) == 0) {
Christopher Faulet1f08bff2021-05-26 13:14:39 +02001390 DBG_TRACE_DEVEL("leaving on L7 retry", STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
1391 return 0;
1392 }
1393
1394 /* Now, L7 buffer is useless, it can be released */
Christopher Faulete05bf9e2022-03-29 15:23:40 +02001395 b_free(&txn->l7_buffer);
Christopher Faulet1f08bff2021-05-26 13:14:39 +02001396
1397 msg->msg_state = HTTP_MSG_BODY;
1398
1399
Christopher Faulet9768c262018-10-22 09:34:31 +02001400 /* 0: we might have to print this header in debug mode */
1401 if (unlikely((global.mode & MODE_DEBUG) &&
1402 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
1403 int32_t pos;
1404
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001405 http_debug_stline("srvrep", s, sl);
Christopher Faulet9768c262018-10-22 09:34:31 +02001406
Christopher Fauleta3f15502019-05-13 15:27:23 +02001407 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001408 struct htx_blk *blk = htx_get_blk(htx, pos);
1409 enum htx_blk_type type = htx_get_blk_type(blk);
1410
1411 if (type == HTX_BLK_EOH)
1412 break;
1413 if (type != HTX_BLK_HDR)
1414 continue;
1415
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001416 http_debug_hdr("srvhdr", s,
1417 htx_get_blk_name(htx, blk),
1418 htx_get_blk_value(htx, blk));
Christopher Faulet9768c262018-10-22 09:34:31 +02001419 }
1420 }
1421
Christopher Faulet03599112018-11-27 11:21:21 +01001422 /* 1: get the status code and the version. Also set HTTP flags */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001423 txn->status = sl->info.res.status;
Christopher Faulet03599112018-11-27 11:21:21 +01001424 if (sl->flags & HTX_SL_F_VER_11)
Christopher Faulet9768c262018-10-22 09:34:31 +02001425 msg->flags |= HTTP_MSGF_VER_11;
Christopher Faulet03599112018-11-27 11:21:21 +01001426 if (sl->flags & HTX_SL_F_XFER_LEN) {
1427 msg->flags |= HTTP_MSGF_XFER_LEN;
Christopher Faulet2a408542020-11-20 14:22:37 +01001428 if (sl->flags & HTX_SL_F_CLEN)
1429 msg->flags |= HTTP_MSGF_CNT_LEN;
1430 else if (sl->flags & HTX_SL_F_CHNK)
1431 msg->flags |= HTTP_MSGF_TE_CHNK;
Christopher Faulet03599112018-11-27 11:21:21 +01001432 }
Christopher Faulet2a408542020-11-20 14:22:37 +01001433 if (sl->flags & HTX_SL_F_BODYLESS)
1434 msg->flags |= HTTP_MSGF_BODYLESS;
Christopher Faulet576c3582021-01-08 15:53:01 +01001435 if (sl->flags & HTX_SL_F_CONN_UPG)
1436 msg->flags |= HTTP_MSGF_CONN_UPG;
Christopher Faulet9768c262018-10-22 09:34:31 +02001437
1438 n = txn->status / 100;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001439 if (n < 1 || n > 5)
1440 n = 0;
Christopher Faulet9768c262018-10-22 09:34:31 +02001441
Christopher Faulete0768eb2018-10-03 16:38:02 +02001442 /* when the client triggers a 4xx from the server, it's most often due
1443 * to a missing object or permission. These events should be tracked
1444 * because if they happen often, it may indicate a brute force or a
1445 * vulnerability scan.
1446 */
1447 if (n == 4)
1448 stream_inc_http_err_ctr(s);
1449
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001450 if (n == 5 && txn->status != 501 && txn->status != 505)
1451 stream_inc_http_fail_ctr(s);
1452
Marcin Deranek3c27dda2020-05-15 18:32:51 +02001453 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001454 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.p.http.rsp[n]);
1455 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.p.http.cum_req);
Marcin Deranek3c27dda2020-05-15 18:32:51 +02001456 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001457
Christopher Faulete0768eb2018-10-03 16:38:02 +02001458 /* Adjust server's health based on status code. Note: status codes 501
1459 * and 505 are triggered on demand by client request, so we must not
1460 * count them as server failures.
1461 */
1462 if (objt_server(s->target)) {
1463 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001464 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_OK);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001465 else
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001466 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_STS);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001467 }
1468
1469 /*
1470 * We may be facing a 100-continue response, or any other informational
1471 * 1xx response which is non-final, in which case this is not the right
1472 * response, and we're waiting for the next one. Let's allow this response
1473 * to go to the client and wait for the next one. There's an exception for
1474 * 101 which is used later in the code to switch protocols.
1475 */
1476 if (txn->status < 200 &&
1477 (txn->status == 100 || txn->status >= 102)) {
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001478 FLT_STRM_CB(s, flt_http_reset(s, msg));
Christopher Faulet421e7692019-06-13 11:16:45 +02001479 htx->first = channel_htx_fwd_headers(rep, htx);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001480 msg->msg_state = HTTP_MSG_RPBEFORE;
Christopher Faulet3499f622019-09-03 15:23:54 +02001481 msg->flags = 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001482 txn->status = 0;
1483 s->logs.t_data = -1; /* was not a response yet */
Christopher Faulet5c281d52023-03-16 15:53:28 +01001484 s->scf->flags |= SC_FL_SND_ASAP; /* Send ASAP informational messages */
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001485 goto next_one;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001486 }
1487
Christopher Faulet6e6c7b12021-01-08 16:02:05 +01001488 /* A 101-switching-protocols must contains a Connection header with the
1489 * "upgrade" option and the request too. It means both are agree to
1490 * upgrade. It is not so strict because there is no test on the Upgrade
1491 * header content. But it is probably stronger enough for now.
1492 */
1493 if (txn->status == 101 &&
1494 (!(txn->req.flags & HTTP_MSGF_CONN_UPG) || !(txn->rsp.flags & HTTP_MSGF_CONN_UPG)))
1495 goto return_bad_res;
1496
Christopher Faulete0768eb2018-10-03 16:38:02 +02001497 /*
1498 * 2: check for cacheability.
1499 */
1500
1501 switch (txn->status) {
1502 case 200:
1503 case 203:
1504 case 204:
1505 case 206:
1506 case 300:
1507 case 301:
1508 case 404:
1509 case 405:
1510 case 410:
1511 case 414:
1512 case 501:
1513 break;
1514 default:
1515 /* RFC7231#6.1:
1516 * Responses with status codes that are defined as
1517 * cacheable by default (e.g., 200, 203, 204, 206,
1518 * 300, 301, 404, 405, 410, 414, and 501 in this
1519 * specification) can be reused by a cache with
1520 * heuristic expiration unless otherwise indicated
1521 * by the method definition or explicit cache
1522 * controls [RFC7234]; all other status codes are
1523 * not cacheable by default.
1524 */
1525 txn->flags &= ~(TX_CACHEABLE | TX_CACHE_COOK);
1526 break;
1527 }
1528
1529 /*
1530 * 3: we may need to capture headers
1531 */
1532 s->logs.logwait &= ~LW_RESP;
1533 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->res_cap))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001534 http_capture_headers(htx, s->res_cap, sess->fe->rsp_cap);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001535
Christopher Faulet9768c262018-10-22 09:34:31 +02001536 /* Skip parsing if no content length is possible. */
Christopher Fauletc75668e2020-12-07 18:10:32 +01001537 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status >= 200 && txn->status < 300) ||
Christopher Faulete0768eb2018-10-03 16:38:02 +02001538 txn->status == 101)) {
1539 /* Either we've established an explicit tunnel, or we're
1540 * switching the protocol. In both cases, we're very unlikely
1541 * to understand the next protocols. We have to switch to tunnel
1542 * mode, so that we transfer the request and responses then let
1543 * this protocol pass unmodified. When we later implement specific
1544 * parsers for such protocols, we'll want to check the Upgrade
1545 * header which contains information about that protocol for
1546 * responses with status 101 (eg: see RFC2817 about TLS).
1547 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02001548 txn->flags |= TX_CON_WANT_TUN;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001549 }
1550
Christopher Faulet61608322018-11-23 16:23:45 +01001551 /* check for NTML authentication headers in 401 (WWW-Authenticate) and
1552 * 407 (Proxy-Authenticate) responses and set the connection to private
1553 */
Willy Tarreaufd9417b2022-05-18 16:23:22 +02001554 srv_conn = sc_conn(s->scb);
Christopher Faulet61608322018-11-23 16:23:45 +01001555 if (srv_conn) {
1556 struct ist hdr;
1557 struct http_hdr_ctx ctx;
1558
1559 if (txn->status == 401)
1560 hdr = ist("WWW-Authenticate");
1561 else if (txn->status == 407)
1562 hdr = ist("Proxy-Authenticate");
1563 else
1564 goto end;
1565
1566 ctx.blk = NULL;
1567 while (http_find_header(htx, hdr, &ctx, 0)) {
Willy Tarreauf1dcced2020-05-07 19:27:02 +02001568 /* If www-authenticate contains "Negotiate", "Nego2", or "NTLM",
1569 * possibly followed by blanks and a base64 string, the connection
1570 * is private. Since it's a mess to deal with, we only check for
1571 * values starting with "NTLM" or "Nego". Note that often multiple
1572 * headers are sent by the server there.
1573 */
1574 if ((ctx.value.len >= 4 && strncasecmp(ctx.value.ptr, "Nego", 4) == 0) ||
Willy Tarreau49a1d282020-05-07 19:10:15 +02001575 (ctx.value.len >= 4 && strncasecmp(ctx.value.ptr, "NTLM", 4) == 0)) {
Olivier Houchard250031e2019-05-29 15:01:50 +02001576 sess->flags |= SESS_FL_PREFER_LAST;
Christopher Faulet08016ab2020-07-01 16:10:06 +02001577 conn_set_owner(srv_conn, sess, NULL);
Christopher Faulet21ddc742020-07-01 15:26:14 +02001578 conn_set_private(srv_conn);
Ilya Shipitsin6b79f382020-07-23 00:32:55 +05001579 /* If it fail now, the same will be done in mux->detach() callback */
Christopher Faulet08016ab2020-07-01 16:10:06 +02001580 session_add_conn(srv_conn->owner, srv_conn, srv_conn->target);
Willy Tarreauf1dcced2020-05-07 19:27:02 +02001581 break;
Olivier Houchard250031e2019-05-29 15:01:50 +02001582 }
Christopher Faulet61608322018-11-23 16:23:45 +01001583 }
1584 }
1585
1586 end:
Christopher Faulete0768eb2018-10-03 16:38:02 +02001587 /* we want to have the response time before we start processing it */
Willy Tarreau69530f52023-04-28 09:16:15 +02001588 s->logs.t_data = ns_to_ms(now_ns - s->logs.accept_ts);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001589
1590 /* end of job, return OK */
1591 rep->analysers &= ~an_bit;
1592 rep->analyse_exp = TICK_ETERNITY;
1593 channel_auto_close(rep);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001594 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001595 return 1;
1596
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001597 return_int_err:
Willy Tarreau4781b152021-04-06 13:53:36 +02001598 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
1599 _HA_ATOMIC_INC(&s->be->be_counters.internal_errors);
William Lallemand36119de2021-03-08 15:26:48 +01001600 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001601 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Fauletb8a53712019-12-16 11:29:38 +01001602 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001603 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.internal_errors);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001604 txn->status = 500;
1605 if (!(s->flags & SF_ERR_MASK))
1606 s->flags |= SF_ERR_INTERNAL;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001607 goto return_prx_cond;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001608
1609 return_bad_res:
Willy Tarreau4781b152021-04-06 13:53:36 +02001610 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Faulet47365272018-10-31 17:40:50 +01001611 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001612 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001613 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Christopher Faulet47365272018-10-31 17:40:50 +01001614 }
Olivier Houcharde3249a92019-05-03 23:01:47 +02001615 if ((s->be->retry_type & PR_RE_JUNK_REQUEST) &&
Christopher Faulete05bf9e2022-03-29 15:23:40 +02001616 (txn->flags & TX_L7_RETRY) &&
Willy Tarreau7cb9e6c2022-05-17 19:40:40 +02001617 do_l7_retry(s, s->scb) == 0) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001618 DBG_TRACE_DEVEL("leaving on L7 retry",
1619 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharde3249a92019-05-03 23:01:47 +02001620 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001621 }
Christopher Faulet47365272018-10-31 17:40:50 +01001622 txn->status = 502;
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001623 stream_inc_http_fail_ctr(s);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001624 /* fall through */
1625
Christopher Fauletb8a53712019-12-16 11:29:38 +01001626 return_prx_cond:
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001627 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet4a66c942023-01-13 09:43:21 +01001628 http_set_term_flags(s);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001629
Willy Tarreaucb041662022-05-17 19:44:42 +02001630 s->scb->flags |= SC_FL_NOLINGER;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001631 DBG_TRACE_DEVEL("leaving on error",
1632 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulet47365272018-10-31 17:40:50 +01001633 return 0;
1634
Christopher Faulete0768eb2018-10-03 16:38:02 +02001635 abort_keep_alive:
1636 /* A keep-alive request to the server failed on a network error.
1637 * The client is required to retry. We need to close without returning
1638 * any other information so that the client retries.
1639 */
1640 txn->status = 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001641 s->logs.logwait = 0;
1642 s->logs.level = 0;
Christopher Faulet84d3ef92023-03-17 15:45:58 +01001643 s->scf->flags &= ~SC_FL_SND_EXP_MORE; /* speed up sending a previous response */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001644 http_reply_and_close(s, txn->status, NULL);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001645 DBG_TRACE_DEVEL("leaving by closing K/A connection",
1646 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001647 return 0;
1648}
1649
1650/* This function performs all the processing enabled for the current response.
1651 * It normally returns 1 unless it wants to break. It relies on buffers flags,
1652 * and updates s->res.analysers. It might make sense to explode it into several
1653 * other functions. It works like process_request (see indications above).
1654 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001655int http_process_res_common(struct stream *s, struct channel *rep, int an_bit, struct proxy *px)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001656{
1657 struct session *sess = s->sess;
1658 struct http_txn *txn = s->txn;
1659 struct http_msg *msg = &txn->rsp;
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001660 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001661 struct proxy *cur_proxy;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001662 enum rule_result ret = HTTP_RULE_RES_CONT;
1663
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001664 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
1665 return 0;
Christopher Faulet9768c262018-10-22 09:34:31 +02001666
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001667 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001668
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001669 htx = htxbuf(&rep->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001670
1671 /* The stats applet needs to adjust the Connection header but we don't
1672 * apply any filter there.
1673 */
1674 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
1675 rep->analysers &= ~an_bit;
1676 rep->analyse_exp = TICK_ETERNITY;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001677 goto end;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001678 }
1679
1680 /*
1681 * We will have to evaluate the filters.
1682 * As opposed to version 1.2, now they will be evaluated in the
1683 * filters order and not in the header order. This means that
1684 * each filter has to be validated among all headers.
1685 *
1686 * Filters are tried with ->be first, then with ->fe if it is
1687 * different from ->be.
1688 *
1689 * Maybe we are in resume condiion. In this case I choose the
1690 * "struct proxy" which contains the rule list matching the resume
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05001691 * pointer. If none of these "struct proxy" match, I initialise
Christopher Faulete0768eb2018-10-03 16:38:02 +02001692 * the process with the first one.
1693 *
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05001694 * In fact, I check only correspondence between the current list
Christopher Faulete0768eb2018-10-03 16:38:02 +02001695 * pointer and the ->fe rule list. If it doesn't match, I initialize
1696 * the loop with the ->be.
1697 */
Christopher Fauletd4150ad2021-10-13 15:35:55 +02001698 if (s->current_rule_list == &sess->fe->http_res_rules ||
1699 (sess->fe->defpx && s->current_rule_list == &sess->fe->defpx->http_res_rules))
Christopher Faulete0768eb2018-10-03 16:38:02 +02001700 cur_proxy = sess->fe;
1701 else
1702 cur_proxy = s->be;
Christopher Fauletd4150ad2021-10-13 15:35:55 +02001703
Christopher Faulete0768eb2018-10-03 16:38:02 +02001704 while (1) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001705 /* evaluate http-response rules */
Christopher Faulet46f46df2021-11-09 16:33:25 +01001706 if (ret == HTTP_RULE_RES_CONT || ret == HTTP_RULE_RES_STOP) {
Christopher Fauletd4150ad2021-10-13 15:35:55 +02001707 struct list *def_rules, *rules;
1708
1709 def_rules = ((cur_proxy->defpx && (cur_proxy == s->be || cur_proxy->defpx != s->be->defpx)) ? &cur_proxy->defpx->http_res_rules : NULL);
1710 rules = &cur_proxy->http_res_rules;
1711
1712 ret = http_res_get_intercept_rule(cur_proxy, def_rules, rules, s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001713
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001714 switch (ret) {
1715 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
1716 goto return_prx_yield;
1717
1718 case HTTP_RULE_RES_CONT:
1719 case HTTP_RULE_RES_STOP: /* nothing to do */
1720 break;
1721
1722 case HTTP_RULE_RES_DENY: /* deny or tarpit */
1723 goto deny;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001724
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001725 case HTTP_RULE_RES_ABRT: /* abort request, response already sent */
1726 goto return_prx_cond;
1727
1728 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
Christopher Fauletb8a53712019-12-16 11:29:38 +01001729 goto done;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001730
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001731 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
1732 goto return_bad_res;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001733
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001734 case HTTP_RULE_RES_ERROR: /* failed with a bad request */
1735 goto return_int_err;
1736 }
1737
1738 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001739
Christopher Faulete0768eb2018-10-03 16:38:02 +02001740 /* check whether we're already working on the frontend */
1741 if (cur_proxy == sess->fe)
1742 break;
1743 cur_proxy = sess->fe;
1744 }
1745
Christopher Faulete0768eb2018-10-03 16:38:02 +02001746 /* OK that's all we can do for 1xx responses */
1747 if (unlikely(txn->status < 200 && txn->status != 101))
Christopher Fauletf2824e62018-10-01 12:12:37 +02001748 goto end;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001749
1750 /*
1751 * Now check for a server cookie.
1752 */
1753 if (s->be->cookie_name || sess->fe->capture_name || (s->be->options & PR_O_CHK_CACHE))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001754 http_manage_server_side_cookies(s, rep);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001755
1756 /*
1757 * Check for cache-control or pragma headers if required.
1758 */
1759 if ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001760 http_check_response_for_cacheability(s, rep);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001761
1762 /*
1763 * Add server cookie in the response if needed
1764 */
1765 if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) &&
1766 !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) &&
1767 (!(s->flags & SF_DIRECT) ||
1768 ((s->be->cookie_maxidle || txn->cookie_last_date) &&
1769 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
1770 (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
1771 (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
1772 (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
1773 !(s->flags & SF_IGNORE_PRST)) {
1774 /* the server is known, it's not the one the client requested, or the
1775 * cookie's last seen date needs to be refreshed. We have to
1776 * insert a set-cookie here, except if we want to insert only on POST
1777 * requests and this one isn't. Note that servers which don't have cookies
1778 * (eg: some backup servers) will return a full cookie removal request.
1779 */
Willy Tarreau88bc8002021-12-06 07:01:02 +00001780 if (!__objt_server(s->target)->cookie) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001781 chunk_printf(&trash,
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001782 "%s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
Christopher Faulete0768eb2018-10-03 16:38:02 +02001783 s->be->cookie_name);
1784 }
1785 else {
Willy Tarreau88bc8002021-12-06 07:01:02 +00001786 chunk_printf(&trash, "%s=%s", s->be->cookie_name, __objt_server(s->target)->cookie);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001787
1788 if (s->be->cookie_maxidle || s->be->cookie_maxlife) {
1789 /* emit last_date, which is mandatory */
1790 trash.area[trash.data++] = COOKIE_DELIM_DATE;
1791 s30tob64((date.tv_sec+3) >> 2,
1792 trash.area + trash.data);
1793 trash.data += 5;
1794
1795 if (s->be->cookie_maxlife) {
1796 /* emit first_date, which is either the original one or
1797 * the current date.
1798 */
1799 trash.area[trash.data++] = COOKIE_DELIM_DATE;
1800 s30tob64(txn->cookie_first_date ?
1801 txn->cookie_first_date >> 2 :
1802 (date.tv_sec+3) >> 2,
1803 trash.area + trash.data);
1804 trash.data += 5;
1805 }
1806 }
1807 chunk_appendf(&trash, "; path=/");
1808 }
1809
1810 if (s->be->cookie_domain)
1811 chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain);
1812
1813 if (s->be->ck_opts & PR_CK_HTTPONLY)
1814 chunk_appendf(&trash, "; HttpOnly");
1815
1816 if (s->be->ck_opts & PR_CK_SECURE)
1817 chunk_appendf(&trash, "; Secure");
1818
Christopher Faulet2f533902020-01-21 11:06:48 +01001819 if (s->be->cookie_attrs)
1820 chunk_appendf(&trash, "; %s", s->be->cookie_attrs);
1821
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001822 if (unlikely(!http_add_header(htx, ist("Set-Cookie"), ist2(trash.area, trash.data))))
Christopher Fauletd649b572022-06-01 17:42:35 +02001823 goto return_fail_rewrite;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001824
1825 txn->flags &= ~TX_SCK_MASK;
1826 if (__objt_server(s->target)->cookie && (s->flags & SF_DIRECT))
1827 /* the server did not change, only the date was updated */
1828 txn->flags |= TX_SCK_UPDATED;
1829 else
1830 txn->flags |= TX_SCK_INSERTED;
1831
1832 /* Here, we will tell an eventual cache on the client side that we don't
1833 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
1834 * Some caches understand the correct form: 'no-cache="set-cookie"', but
1835 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
1836 */
1837 if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
1838
1839 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
1840
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001841 if (unlikely(!http_add_header(htx, ist("Cache-control"), ist("private"))))
Christopher Fauletd649b572022-06-01 17:42:35 +02001842 goto return_fail_rewrite;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001843 }
1844 }
1845
1846 /*
1847 * Check if result will be cacheable with a cookie.
1848 * We'll block the response if security checks have caught
1849 * nasty things such as a cacheable cookie.
1850 */
1851 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
1852 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
1853 (s->be->options & PR_O_CHK_CACHE)) {
1854 /* we're in presence of a cacheable response containing
1855 * a set-cookie header. We'll block it as requested by
1856 * the 'checkcache' option, and send an alert.
1857 */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001858 ha_alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreau88bc8002021-12-06 07:01:02 +00001859 s->be->id, objt_server(s->target) ? __objt_server(s->target)->id : "<dispatch>");
Christopher Faulete0768eb2018-10-03 16:38:02 +02001860 send_log(s->be, LOG_ALERT,
1861 "Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreau88bc8002021-12-06 07:01:02 +00001862 s->be->id, objt_server(s->target) ? __objt_server(s->target)->id : "<dispatch>");
Christopher Fauletb8a53712019-12-16 11:29:38 +01001863 goto deny;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001864 }
1865
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001866 end:
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01001867 /*
1868 * Evaluate after-response rules before forwarding the response. rules
1869 * from the backend are evaluated first, then one from the frontend if
1870 * it differs.
1871 */
1872 if (!http_eval_after_res_rules(s))
1873 goto return_int_err;
1874
Christopher Fauletc2ac5e42021-03-08 18:20:09 +01001875 /* Filter the response headers if there are filters attached to the
1876 * stream.
1877 */
1878 if (HAS_FILTERS(s))
1879 rep->analysers |= AN_RES_FLT_HTTP_HDRS;
1880
Christopher Faulete0768eb2018-10-03 16:38:02 +02001881 /* Always enter in the body analyzer */
1882 rep->analysers &= ~AN_RES_FLT_XFER_DATA;
1883 rep->analysers |= AN_RES_HTTP_XFER_BODY;
1884
1885 /* if the user wants to log as soon as possible, without counting
1886 * bytes from the server, then this is the right moment. We have
1887 * to temporarily assign bytes_out to log what we currently have.
1888 */
1889 if (!LIST_ISEMPTY(&sess->fe->logformat) && !(s->logs.logwait & LW_BYTES)) {
1890 s->logs.t_close = s->logs.t_data; /* to get a valid end date */
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001891 s->logs.bytes_out = htx->data;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001892 s->do_log(s);
1893 s->logs.bytes_out = 0;
1894 }
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001895
Christopher Fauletb8a53712019-12-16 11:29:38 +01001896 done:
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01001897 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletb8a53712019-12-16 11:29:38 +01001898 rep->analysers &= ~an_bit;
1899 rep->analyse_exp = TICK_ETERNITY;
Christopher Fauletd4150ad2021-10-13 15:35:55 +02001900 s->current_rule = s->current_rule_list = NULL;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001901 return 1;
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001902
Christopher Fauletb8a53712019-12-16 11:29:38 +01001903 deny:
Willy Tarreau4781b152021-04-06 13:53:36 +02001904 _HA_ATOMIC_INC(&sess->fe->fe_counters.denied_resp);
1905 _HA_ATOMIC_INC(&s->be->be_counters.denied_resp);
William Lallemand36119de2021-03-08 15:26:48 +01001906 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001907 _HA_ATOMIC_INC(&sess->listener->counters->denied_resp);
Christopher Fauleta08546b2019-12-16 16:07:34 +01001908 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001909 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.denied_resp);
Christopher Fauletb8a53712019-12-16 11:29:38 +01001910 goto return_prx_err;
1911
Christopher Fauletd649b572022-06-01 17:42:35 +02001912 return_fail_rewrite:
1913 if (!(s->flags & SF_ERR_MASK))
1914 s->flags |= SF_ERR_PRXCOND;
1915 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_rewrites);
1916 _HA_ATOMIC_INC(&s->be->be_counters.failed_rewrites);
1917 if (sess->listener && sess->listener->counters)
1918 _HA_ATOMIC_INC(&sess->listener->counters->failed_rewrites);
1919 if (objt_server(s->target))
1920 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_rewrites);
1921 /* fall through */
1922
Christopher Fauletb8a53712019-12-16 11:29:38 +01001923 return_int_err:
1924 txn->status = 500;
1925 if (!(s->flags & SF_ERR_MASK))
1926 s->flags |= SF_ERR_INTERNAL;
Willy Tarreau4781b152021-04-06 13:53:36 +02001927 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
1928 _HA_ATOMIC_INC(&s->be->be_counters.internal_errors);
Dragan Dosen9a006f92021-09-21 13:02:09 +02001929 if (sess->listener && sess->listener->counters)
1930 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001931 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001932 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.internal_errors);
Christopher Fauletb8a53712019-12-16 11:29:38 +01001933 goto return_prx_err;
1934
1935 return_bad_res:
1936 txn->status = 502;
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001937 stream_inc_http_fail_ctr(s);
Willy Tarreau4781b152021-04-06 13:53:36 +02001938 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Fauleta20a6532020-02-05 10:16:41 +01001939 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001940 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Christopher Fauleta20a6532020-02-05 10:16:41 +01001941 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_RSP);
1942 }
Christopher Fauletb8a53712019-12-16 11:29:38 +01001943 /* fall through */
1944
1945 return_prx_err:
1946 http_reply_and_close(s, txn->status, http_error_message(s));
1947 /* fall through */
1948
1949 return_prx_cond:
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001950 s->logs.t_data = -1; /* was not a valid response */
Willy Tarreaucb041662022-05-17 19:44:42 +02001951 s->scb->flags |= SC_FL_NOLINGER;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001952
Christopher Faulet4a66c942023-01-13 09:43:21 +01001953 http_set_term_flags(s);
Christopher Fauletb8a53712019-12-16 11:29:38 +01001954
Christopher Faulete58c0002020-03-02 16:21:01 +01001955 rep->analysers &= AN_RES_FLT_END;
1956 s->req.analysers &= AN_REQ_FLT_END;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001957 rep->analyse_exp = TICK_ETERNITY;
Christopher Fauletd4150ad2021-10-13 15:35:55 +02001958 s->current_rule = s->current_rule_list = NULL;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001959 DBG_TRACE_DEVEL("leaving on error",
1960 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001961 return 0;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001962
1963 return_prx_yield:
1964 channel_dont_close(rep);
1965 DBG_TRACE_DEVEL("waiting for more data",
1966 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
1967 return 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001968}
1969
1970/* This function is an analyser which forwards response body (including chunk
1971 * sizes if any). It is called as soon as we must forward, even if we forward
1972 * zero byte. The only situation where it must not be called is when we're in
1973 * tunnel mode and we want to forward till the close. It's used both to forward
1974 * remaining data and to resync after end of body. It expects the msg_state to
1975 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
1976 * read more data, or 1 once we can go on with next request or end the stream.
1977 *
1978 * It is capable of compressing response data both in content-length mode and
1979 * in chunked mode. The state machines follows different flows depending on
1980 * whether content-length and chunked modes are used, since there are no
1981 * trailers in content-length :
1982 *
1983 * chk-mode cl-mode
1984 * ,----- BODY -----.
1985 * / \
1986 * V size > 0 V chk-mode
1987 * .--> SIZE -------------> DATA -------------> CRLF
1988 * | | size == 0 | last byte |
1989 * | v final crlf v inspected |
1990 * | TRAILERS -----------> DONE |
1991 * | |
1992 * `----------------------------------------------'
1993 *
1994 * Compression only happens in the DATA state, and must be flushed in final
1995 * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding
1996 * is performed at once on final states for all bytes parsed, or when leaving
1997 * on missing data.
1998 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001999int http_response_forward_body(struct stream *s, struct channel *res, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +02002000{
2001 struct session *sess = s->sess;
2002 struct http_txn *txn = s->txn;
2003 struct http_msg *msg = &s->txn->rsp;
Christopher Faulet9768c262018-10-22 09:34:31 +02002004 struct htx *htx;
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002005 int ret;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002006
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002007 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002008
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002009 htx = htxbuf(&res->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002010
Christopher Fauletb9a92f32019-09-09 10:15:21 +02002011 if (htx->flags & HTX_FL_PARSING_ERROR)
2012 goto return_bad_res;
2013 if (htx->flags & HTX_FL_PROCESSING_ERROR)
2014 goto return_int_err;
2015
Christopher Faulet9768c262018-10-22 09:34:31 +02002016 if (msg->msg_state == HTTP_MSG_BODY)
2017 msg->msg_state = HTTP_MSG_DATA;
2018
Christopher Faulete0768eb2018-10-03 16:38:02 +02002019 /* in most states, we should abort in case of early close */
2020 channel_auto_close(res);
2021
Christopher Faulete0768eb2018-10-03 16:38:02 +02002022 if (res->to_forward) {
Christopher Faulet66af0b22019-03-22 14:54:52 +01002023 if (res->to_forward == CHN_INFINITE_FORWARD) {
Christopher Faulet904763f2023-03-22 14:53:11 +01002024 if (s->scb->flags & SC_FL_EOI)
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002025 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet66af0b22019-03-22 14:54:52 +01002026 }
2027 else {
2028 /* We can't process the buffer's contents yet */
2029 res->flags |= CF_WAKE_WRITE;
2030 goto missing_data_or_waiting;
2031 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02002032 }
2033
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002034 if (msg->msg_state >= HTTP_MSG_ENDING)
2035 goto ending;
2036
Christopher Fauletc75668e2020-12-07 18:10:32 +01002037 if ((txn->meth == HTTP_METH_CONNECT && txn->status >= 200 && txn->status < 300) || txn->status == 101 ||
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002038 (!(msg->flags & HTTP_MSGF_XFER_LEN) && !HAS_RSP_DATA_FILTERS(s))) {
2039 msg->msg_state = HTTP_MSG_ENDING;
2040 goto ending;
2041 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002042
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002043 /* Forward input data. We get it by removing all outgoing data not
2044 * forwarded yet from HTX data size. If there are some data filters, we
2045 * let them decide the amount of data to forward.
Christopher Faulet9768c262018-10-22 09:34:31 +02002046 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002047 if (HAS_RSP_DATA_FILTERS(s)) {
2048 ret = flt_http_payload(s, msg, htx->data);
2049 if (ret < 0)
2050 goto return_bad_res;
Christopher Faulet421e7692019-06-13 11:16:45 +02002051 c_adv(res, ret);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002052 }
2053 else {
Christopher Faulet421e7692019-06-13 11:16:45 +02002054 c_adv(res, htx->data - co_data(res));
Christopher Faulet2f7c82b2023-02-20 14:06:52 +01002055 if ((global.tune.options & GTUNE_USE_FAST_FWD) && (msg->flags & HTTP_MSGF_XFER_LEN))
Christopher Faulet66af0b22019-03-22 14:54:52 +01002056 channel_htx_forward_forever(res, htx);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002057 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002058
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002059 if (htx->data != co_data(res))
2060 goto missing_data_or_waiting;
2061
Christopher Fauletca5309a2023-04-17 16:17:32 +02002062 if (!(msg->flags & HTTP_MSGF_XFER_LEN) && (s->scb->flags & (SC_FL_EOS|SC_FL_ABRT_DONE))) {
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002063 msg->msg_state = HTTP_MSG_ENDING;
2064 goto ending;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002065 }
2066
Christopher Faulet9768c262018-10-22 09:34:31 +02002067 /* Check if the end-of-message is reached and if so, switch the message
Christopher Fauletd20fdb02019-06-13 16:43:22 +02002068 * in HTTP_MSG_ENDING state. Then if all data was marked to be
2069 * forwarded, set the state to HTTP_MSG_DONE.
Christopher Faulet9768c262018-10-22 09:34:31 +02002070 */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002071 if (!(htx->flags & HTX_FL_EOM))
Christopher Faulet9768c262018-10-22 09:34:31 +02002072 goto missing_data_or_waiting;
2073
Christopher Fauletd20fdb02019-06-13 16:43:22 +02002074 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet9768c262018-10-22 09:34:31 +02002075
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002076 ending:
Christopher Faulet84d3ef92023-03-17 15:45:58 +01002077 s->scf->flags &= ~SC_FL_SND_EXP_MORE; /* no more data are expected to be sent */
Christopher Faulet2151cdd2020-07-22 16:34:59 +02002078
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002079 /* other states, ENDING...TUNNEL */
2080 if (msg->msg_state >= HTTP_MSG_DONE)
2081 goto done;
Christopher Faulet9768c262018-10-22 09:34:31 +02002082
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002083 if (HAS_RSP_DATA_FILTERS(s)) {
2084 ret = flt_http_end(s, msg);
2085 if (ret <= 0) {
2086 if (!ret)
2087 goto missing_data_or_waiting;
2088 goto return_bad_res;
2089 }
2090 }
2091
Christopher Faulet97047972023-04-17 08:52:10 +02002092 if (!(txn->flags & TX_CON_WANT_TUN) && !(msg->flags & HTTP_MSGF_XFER_LEN)) {
2093 /* One-side tunnel */
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002094 msg->msg_state = HTTP_MSG_TUNNEL;
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002095 }
2096 else {
2097 msg->msg_state = HTTP_MSG_DONE;
2098 res->to_forward = 0;
2099 }
2100
2101 done:
2102
2103 channel_dont_close(res);
2104
Christopher Faulet64350bb2023-04-13 16:37:37 +02002105 if ((s->scf->flags & SC_FL_SHUT_DONE) && co_data(res)) {
Christopher Fauletf2b02cf2023-01-13 11:02:28 +01002106 /* response errors are most likely due to the client aborting
2107 * the transfer. */
2108 goto return_cli_abort;
2109 }
2110
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002111 http_end_response(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002112 if (!(res->analysers & an_bit)) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002113 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002114 return 1;
2115 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002116 DBG_TRACE_DEVEL("waiting for the end of the HTTP txn",
2117 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002118 return 0;
2119
2120 missing_data_or_waiting:
Christopher Faulet64350bb2023-04-13 16:37:37 +02002121 if (s->scf->flags & SC_FL_SHUT_DONE)
Christopher Faulet93e02d82019-03-08 14:18:50 +01002122 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002123
2124 /* stop waiting for data if the input is closed before the end. If the
2125 * client side was already closed, it means that the client has aborted,
2126 * so we don't want to count this as a server abort. Otherwise it's a
2127 * server abort.
2128 */
Christopher Fauletca5309a2023-04-17 16:17:32 +02002129 if (msg->msg_state < HTTP_MSG_ENDING && (s->scb->flags & (SC_FL_EOS|SC_FL_ABRT_DONE))) {
2130 if ((s->scf->flags & (SC_FL_EOS|SC_FL_ABRT_DONE)) &&
Christopher Faulet64350bb2023-04-13 16:37:37 +02002131 (s->scb->flags & SC_FL_SHUT_DONE))
Christopher Faulet93e02d82019-03-08 14:18:50 +01002132 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002133 /* If we have some pending data, we continue the processing */
Christopher Faulet93e02d82019-03-08 14:18:50 +01002134 if (htx_is_empty(htx))
2135 goto return_srv_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002136 }
2137
Christopher Faulete0768eb2018-10-03 16:38:02 +02002138 /* When TE: chunked is used, we need to get there again to parse
2139 * remaining chunks even if the server has closed, so we don't want to
Christopher Faulet9768c262018-10-22 09:34:31 +02002140 * set CF_DONTCLOSE. Similarly when there is a content-leng or if there
2141 * are filters registered on the stream, we don't want to forward a
2142 * close
Christopher Faulete0768eb2018-10-03 16:38:02 +02002143 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002144 if ((msg->flags & HTTP_MSGF_XFER_LEN) || HAS_RSP_DATA_FILTERS(s))
Christopher Faulete0768eb2018-10-03 16:38:02 +02002145 channel_dont_close(res);
2146
2147 /* We know that more data are expected, but we couldn't send more that
Christopher Faulet84d3ef92023-03-17 15:45:58 +01002148 * what we did. So we always set the SC_FL_SND_EXP_MORE flag so that the
Christopher Faulete0768eb2018-10-03 16:38:02 +02002149 * system knows it must not set a PUSH on this first part. Interactive
2150 * modes are already handled by the stream sock layer. We must not do
2151 * this in content-length mode because it could present the MSG_MORE
2152 * flag with the last block of forwarded data, which would cause an
2153 * additional delay to be observed by the receiver.
2154 */
Christopher Faulet2151cdd2020-07-22 16:34:59 +02002155 if (HAS_RSP_DATA_FILTERS(s))
Christopher Faulet84d3ef92023-03-17 15:45:58 +01002156 s->scf->flags |= SC_FL_SND_EXP_MORE;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002157
2158 /* the stream handler will take care of timeouts and errors */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002159 DBG_TRACE_DEVEL("waiting for more data to forward",
2160 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002161 return 0;
2162
Christopher Faulet93e02d82019-03-08 14:18:50 +01002163 return_srv_abort:
Willy Tarreau4781b152021-04-06 13:53:36 +02002164 _HA_ATOMIC_INC(&sess->fe->fe_counters.srv_aborts);
2165 _HA_ATOMIC_INC(&s->be->be_counters.srv_aborts);
William Lallemand36119de2021-03-08 15:26:48 +01002166 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02002167 _HA_ATOMIC_INC(&sess->listener->counters->srv_aborts);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002168 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02002169 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.srv_aborts);
Willy Tarreau826f3ab2021-02-10 12:07:15 +01002170 stream_inc_http_fail_ctr(s);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002171 if (!(s->flags & SF_ERR_MASK))
Christopher Fauletf2b02cf2023-01-13 11:02:28 +01002172 s->flags |= ((res->flags & CF_READ_TIMEOUT) ? SF_ERR_SRVTO : SF_ERR_SRVCL);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002173 goto return_error;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002174
Christopher Faulet93e02d82019-03-08 14:18:50 +01002175 return_cli_abort:
Willy Tarreau4781b152021-04-06 13:53:36 +02002176 _HA_ATOMIC_INC(&sess->fe->fe_counters.cli_aborts);
2177 _HA_ATOMIC_INC(&s->be->be_counters.cli_aborts);
William Lallemand36119de2021-03-08 15:26:48 +01002178 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02002179 _HA_ATOMIC_INC(&sess->listener->counters->cli_aborts);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002180 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02002181 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.cli_aborts);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002182 if (!(s->flags & SF_ERR_MASK))
Christopher Fauletf2b02cf2023-01-13 11:02:28 +01002183 s->flags |= ((res->flags & CF_WRITE_TIMEOUT) ? SF_ERR_CLITO : SF_ERR_CLICL);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002184 goto return_error;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002185
Christopher Fauletb9a92f32019-09-09 10:15:21 +02002186 return_int_err:
Willy Tarreau4781b152021-04-06 13:53:36 +02002187 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
2188 _HA_ATOMIC_INC(&s->be->be_counters.internal_errors);
William Lallemand36119de2021-03-08 15:26:48 +01002189 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02002190 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002191 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02002192 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.internal_errors);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02002193 if (!(s->flags & SF_ERR_MASK))
2194 s->flags |= SF_ERR_INTERNAL;
2195 goto return_error;
2196
Christopher Faulet93e02d82019-03-08 14:18:50 +01002197 return_bad_res:
Willy Tarreau4781b152021-04-06 13:53:36 +02002198 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002199 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02002200 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002201 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_RSP);
2202 }
Willy Tarreau826f3ab2021-02-10 12:07:15 +01002203 stream_inc_http_fail_ctr(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002204 if (!(s->flags & SF_ERR_MASK))
Christopher Faulet93e02d82019-03-08 14:18:50 +01002205 s->flags |= SF_ERR_SRVCL;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002206 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +02002207
Christopher Faulet93e02d82019-03-08 14:18:50 +01002208 return_error:
Christopher Faulete0768eb2018-10-03 16:38:02 +02002209 /* don't send any error message as we're in the body */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002210 http_reply_and_close(s, txn->status, NULL);
Christopher Fauletf2b02cf2023-01-13 11:02:28 +01002211 http_set_term_flags(s);
2212 stream_inc_http_fail_ctr(s);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002213 DBG_TRACE_DEVEL("leaving on error",
2214 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002215 return 0;
2216}
2217
Christopher Fauletf2824e62018-10-01 12:12:37 +02002218/* Perform an HTTP redirect based on the information in <rule>. The function
Willy Tarreaubc1223b2021-09-02 16:54:33 +02002219 * returns zero in case of an irrecoverable error such as too large a request
2220 * to build a valid response, 1 in case of successful redirect (hence the rule
2221 * is final), or 2 if the rule has to be silently skipped.
Christopher Fauletf2824e62018-10-01 12:12:37 +02002222 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002223int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn)
Christopher Fauletf2824e62018-10-01 12:12:37 +02002224{
Christopher Faulet99daf282018-11-28 22:58:13 +01002225 struct channel *req = &s->req;
2226 struct channel *res = &s->res;
2227 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01002228 struct htx_sl *sl;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002229 struct buffer *chunk;
Christopher Faulet99daf282018-11-28 22:58:13 +01002230 struct ist status, reason, location;
2231 unsigned int flags;
Christopher Fauleteab17572022-04-26 20:34:38 +02002232 int ret = 1, close = 0; /* Try to keep the connection alive byt default */
Christopher Fauletf2824e62018-10-01 12:12:37 +02002233
2234 chunk = alloc_trash_chunk();
Christopher Fauletb8a53712019-12-16 11:29:38 +01002235 if (!chunk) {
2236 if (!(s->flags & SF_ERR_MASK))
2237 s->flags |= SF_ERR_RESOURCE;
Christopher Faulet99daf282018-11-28 22:58:13 +01002238 goto fail;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002239 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002240
Christopher Faulet99daf282018-11-28 22:58:13 +01002241 /*
2242 * Create the location
2243 */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002244 htx = htxbuf(&req->buf);
Christopher Fauletf2824e62018-10-01 12:12:37 +02002245 switch(rule->type) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002246 case REDIRECT_TYPE_SCHEME: {
2247 struct http_hdr_ctx ctx;
2248 struct ist path, host;
Amaury Denoyellec453f952021-07-06 11:40:12 +02002249 struct http_uri_parser parser;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002250
Christopher Faulet99daf282018-11-28 22:58:13 +01002251 host = ist("");
2252 ctx.blk = NULL;
2253 if (http_find_header(htx, ist("Host"), &ctx, 0))
2254 host = ctx.value;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002255
Christopher Faulet297fbb42019-05-13 14:41:27 +02002256 sl = http_get_stline(htx);
Amaury Denoyellec453f952021-07-06 11:40:12 +02002257 parser = http_uri_parser_init(htx_sl_req_uri(sl));
2258 path = http_parse_path(&parser);
Christopher Faulet99daf282018-11-28 22:58:13 +01002259 /* build message using path */
Tim Duesterhused526372020-03-05 17:56:33 +01002260 if (isttest(path)) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002261 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2262 int qs = 0;
2263 while (qs < path.len) {
2264 if (*(path.ptr + qs) == '?') {
2265 path.len = qs;
2266 break;
2267 }
2268 qs++;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002269 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002270 }
2271 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002272 else
2273 path = ist("/");
Christopher Fauletf2824e62018-10-01 12:12:37 +02002274
Christopher Faulet99daf282018-11-28 22:58:13 +01002275 if (rule->rdr_str) { /* this is an old "redirect" rule */
2276 /* add scheme */
2277 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2278 goto fail;
2279 }
2280 else {
2281 /* add scheme with executing log format */
2282 chunk->data += build_logline(s, chunk->area + chunk->data,
2283 chunk->size - chunk->data,
2284 &rule->rdr_fmt);
2285 }
2286 /* add "://" + host + path */
2287 if (!chunk_memcat(chunk, "://", 3) ||
2288 !chunk_memcat(chunk, host.ptr, host.len) ||
2289 !chunk_memcat(chunk, path.ptr, path.len))
2290 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002291
Christopher Faulet99daf282018-11-28 22:58:13 +01002292 /* append a slash at the end of the location if needed and missing */
2293 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
2294 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
2295 if (chunk->data + 1 >= chunk->size)
2296 goto fail;
2297 chunk->area[chunk->data++] = '/';
2298 }
2299 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002300 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002301
Christopher Faulet99daf282018-11-28 22:58:13 +01002302 case REDIRECT_TYPE_PREFIX: {
2303 struct ist path;
Amaury Denoyellec453f952021-07-06 11:40:12 +02002304 struct http_uri_parser parser;
Christopher Faulet99daf282018-11-28 22:58:13 +01002305
Christopher Faulet297fbb42019-05-13 14:41:27 +02002306 sl = http_get_stline(htx);
Amaury Denoyellec453f952021-07-06 11:40:12 +02002307 parser = http_uri_parser_init(htx_sl_req_uri(sl));
2308 path = http_parse_path(&parser);
Christopher Faulet99daf282018-11-28 22:58:13 +01002309 /* build message using path */
Tim Duesterhused526372020-03-05 17:56:33 +01002310 if (isttest(path)) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002311 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2312 int qs = 0;
2313 while (qs < path.len) {
2314 if (*(path.ptr + qs) == '?') {
2315 path.len = qs;
2316 break;
2317 }
2318 qs++;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002319 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002320 }
2321 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002322 else
2323 path = ist("/");
Christopher Fauletf2824e62018-10-01 12:12:37 +02002324
Christopher Faulet99daf282018-11-28 22:58:13 +01002325 if (rule->rdr_str) { /* this is an old "redirect" rule */
2326 /* add prefix. Note that if prefix == "/", we don't want to
2327 * add anything, otherwise it makes it hard for the user to
2328 * configure a self-redirection.
2329 */
2330 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
2331 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2332 goto fail;
2333 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002334 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002335 else {
2336 /* add prefix with executing log format */
2337 chunk->data += build_logline(s, chunk->area + chunk->data,
2338 chunk->size - chunk->data,
2339 &rule->rdr_fmt);
2340 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002341
Christopher Faulet99daf282018-11-28 22:58:13 +01002342 /* add path */
2343 if (!chunk_memcat(chunk, path.ptr, path.len))
2344 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002345
Christopher Faulet99daf282018-11-28 22:58:13 +01002346 /* append a slash at the end of the location if needed and missing */
2347 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
2348 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
2349 if (chunk->data + 1 >= chunk->size)
2350 goto fail;
2351 chunk->area[chunk->data++] = '/';
2352 }
2353 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002354 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002355 case REDIRECT_TYPE_LOCATION:
2356 default:
2357 if (rule->rdr_str) { /* this is an old "redirect" rule */
2358 /* add location */
2359 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2360 goto fail;
2361 }
2362 else {
2363 /* add location with executing log format */
Willy Tarreaubc1223b2021-09-02 16:54:33 +02002364 int len = build_logline(s, chunk->area + chunk->data,
2365 chunk->size - chunk->data,
2366 &rule->rdr_fmt);
Christopher Fauleteab17572022-04-26 20:34:38 +02002367 if (!len && rule->flags & REDIRECT_FLAG_IGNORE_EMPTY) {
2368 ret = 2;
2369 goto out;
2370 }
Willy Tarreaubc1223b2021-09-02 16:54:33 +02002371
2372 chunk->data += len;
Christopher Faulet99daf282018-11-28 22:58:13 +01002373 }
2374 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002375 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002376 location = ist2(chunk->area, chunk->data);
2377
2378 /*
2379 * Create the 30x response
2380 */
2381 switch (rule->code) {
2382 case 308:
2383 status = ist("308");
2384 reason = ist("Permanent Redirect");
2385 break;
2386 case 307:
2387 status = ist("307");
2388 reason = ist("Temporary Redirect");
2389 break;
2390 case 303:
2391 status = ist("303");
2392 reason = ist("See Other");
2393 break;
2394 case 301:
2395 status = ist("301");
2396 reason = ist("Moved Permanently");
2397 break;
2398 case 302:
2399 default:
2400 status = ist("302");
2401 reason = ist("Found");
2402 break;
2403 }
2404
Christopher Faulet08e66462019-05-23 16:44:59 +02002405 if (!(txn->req.flags & HTTP_MSGF_BODYLESS) && txn->req.msg_state != HTTP_MSG_DONE)
2406 close = 1;
2407
Christopher Faulet99daf282018-11-28 22:58:13 +01002408 htx = htx_from_buf(&res->buf);
Kevin Zhu96b36392020-01-07 09:42:55 +01002409 /* Trim any possible response */
2410 channel_htx_truncate(&s->res, htx);
Christopher Faulet99daf282018-11-28 22:58:13 +01002411 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
2412 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), status, reason);
2413 if (!sl)
2414 goto fail;
2415 sl->info.res.status = rule->code;
2416 s->txn->status = rule->code;
2417
Christopher Faulet08e66462019-05-23 16:44:59 +02002418 if (close && !htx_add_header(htx, ist("Connection"), ist("close")))
2419 goto fail;
2420
2421 if (!htx_add_header(htx, ist("Content-length"), ist("0")) ||
Christopher Faulet99daf282018-11-28 22:58:13 +01002422 !htx_add_header(htx, ist("Location"), location))
2423 goto fail;
2424
2425 if (rule->code == 302 || rule->code == 303 || rule->code == 307) {
2426 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")))
2427 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002428 }
2429
2430 if (rule->cookie_len) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002431 if (!htx_add_header(htx, ist("Set-Cookie"), ist2(rule->cookie_str, rule->cookie_len)))
2432 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002433 }
2434
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002435 if (!htx_add_endof(htx, HTX_BLK_EOH))
Christopher Faulet99daf282018-11-28 22:58:13 +01002436 goto fail;
2437
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002438 htx->flags |= HTX_FL_EOM;
Kevin Zhu96b36392020-01-07 09:42:55 +01002439 htx_to_buf(htx, &res->buf);
Christopher Fauleta72a7e42020-01-28 09:28:11 +01002440 if (!http_forward_proxy_resp(s, 1))
2441 goto fail;
Christopher Faulet99daf282018-11-28 22:58:13 +01002442
Christopher Faulet60b33a52020-01-28 09:18:10 +01002443 if (rule->flags & REDIRECT_FLAG_FROM_REQ) {
2444 /* let's log the request time */
Willy Tarreau69530f52023-04-28 09:16:15 +02002445 s->logs.request_ts = now_ns;
Christopher Fauletd3475882021-10-04 14:16:46 +02002446 req->analysers &= AN_REQ_FLT_END;
Christopher Faulet99daf282018-11-28 22:58:13 +01002447
Christopher Faulet60b33a52020-01-28 09:18:10 +01002448 if (s->sess->fe == s->be) /* report it if the request was intercepted by the frontend */
Willy Tarreau4781b152021-04-06 13:53:36 +02002449 _HA_ATOMIC_INC(&s->sess->fe->fe_counters.intercepted_req);
Christopher Faulet60b33a52020-01-28 09:18:10 +01002450 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002451
2452 if (!(s->flags & SF_ERR_MASK))
2453 s->flags |= SF_ERR_LOCAL;
Christopher Faulet4a66c942023-01-13 09:43:21 +01002454 http_set_term_flags(s);
Christopher Fauletf2824e62018-10-01 12:12:37 +02002455
Christopher Fauleteab17572022-04-26 20:34:38 +02002456 out:
Christopher Faulet99daf282018-11-28 22:58:13 +01002457 free_trash_chunk(chunk);
Christopher Fauleteab17572022-04-26 20:34:38 +02002458 return ret;
Christopher Faulet99daf282018-11-28 22:58:13 +01002459
2460 fail:
2461 /* If an error occurred, remove the incomplete HTTP response from the
2462 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01002463 channel_htx_truncate(res, htxbuf(&res->buf));
Christopher Fauleteab17572022-04-26 20:34:38 +02002464 ret = 0;
2465 goto out;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002466}
2467
Christopher Faulet18c13d32022-05-16 11:43:10 +02002468/* This function filters the request header names to only allow [0-9a-zA-Z-]
2469 * characters. Depending on the proxy configuration, headers with a name not
2470 * matching this charset are removed or the request is rejected with a
2471 * 403-Forbidden response if such name are found. It returns HTTP_RULE_RES_CONT
2472 * to continue the request processing or HTTP_RULE_RES_DENY if the request is
2473 * rejected.
2474 */
2475static enum rule_result http_req_restrict_header_names(struct stream *s, struct htx *htx, struct proxy *px)
2476{
2477 struct htx_blk *blk;
2478 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
2479
2480 blk = htx_get_first_blk(htx);
2481 while (blk) {
2482 enum htx_blk_type type = htx_get_blk_type(blk);
2483
2484 if (type == HTX_BLK_HDR) {
2485 struct ist n = htx_get_blk_name(htx, blk);
Mateusz Malek4b85a962022-08-17 14:22:09 +02002486 int i, end = istlen(n);
Christopher Faulet18c13d32022-05-16 11:43:10 +02002487
Mateusz Malek4b85a962022-08-17 14:22:09 +02002488 for (i = 0; i < end; i++) {
Christopher Faulet18c13d32022-05-16 11:43:10 +02002489 if (!isalnum((unsigned char)n.ptr[i]) && n.ptr[i] != '-') {
Mateusz Malek4b85a962022-08-17 14:22:09 +02002490 break;
Christopher Faulet18c13d32022-05-16 11:43:10 +02002491 }
2492 }
Mateusz Malek4b85a962022-08-17 14:22:09 +02002493
2494 if (i < end) {
2495 /* Disallowed character found - block the request or remove the header */
2496 if (px->options2 & PR_O2_RSTRICT_REQ_HDR_NAMES_BLK)
2497 goto block;
2498 blk = htx_remove_blk(htx, blk);
2499 continue;
2500 }
Christopher Faulet18c13d32022-05-16 11:43:10 +02002501 }
2502 if (type == HTX_BLK_EOH)
2503 break;
2504
2505 blk = htx_get_next_blk(htx, blk);
2506 }
2507 out:
2508 return rule_ret;
2509 block:
2510 /* Block the request returning a 403-Forbidden response */
2511 s->txn->status = 403;
2512 rule_ret = HTTP_RULE_RES_DENY;
2513 goto out;
2514}
2515
Christopher Faulet92d34fe2019-12-17 09:20:34 +01002516/* Replace all headers matching the name <name>. The header value is replaced if
2517 * it matches the regex <re>. <str> is used for the replacement. If <full> is
2518 * set to 1, the full-line is matched and replaced. Otherwise, comma-separated
2519 * values are evaluated one by one. It returns 0 on success and -1 on error.
2520 */
2521int http_replace_hdrs(struct stream* s, struct htx *htx, struct ist name,
2522 const char *str, struct my_regex *re, int full)
Christopher Faulet72333522018-10-24 11:25:02 +02002523{
2524 struct http_hdr_ctx ctx;
Christopher Faulet72333522018-10-24 11:25:02 +02002525
Christopher Faulet72333522018-10-24 11:25:02 +02002526 ctx.blk = NULL;
Christopher Faulet92d34fe2019-12-17 09:20:34 +01002527 while (http_find_header(htx, name, &ctx, full)) {
Christopher Faulet80707f72023-08-04 16:51:11 +02002528 struct buffer *output = get_trash_chunk();
2529
Christopher Faulet72333522018-10-24 11:25:02 +02002530 if (!regex_exec_match2(re, ctx.value.ptr, ctx.value.len, MAX_MATCH, pmatch, 0))
2531 continue;
2532
2533 output->data = exp_replace(output->area, output->size, ctx.value.ptr, str, pmatch);
2534 if (output->data == -1)
2535 return -1;
2536 if (!http_replace_header_value(htx, &ctx, ist2(output->area, output->data)))
2537 return -1;
2538 }
2539 return 0;
2540}
2541
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002542/* This function executes one of the set-{method,path,query,uri} actions. It
2543 * takes the string from the variable 'replace' with length 'len', then modifies
2544 * the relevant part of the request line accordingly. Then it updates various
2545 * pointers to the next elements which were moved, and the total buffer length.
2546 * It finds the action to be performed in p[2], previously filled by function
2547 * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal
2548 * error, though this can be revisited when this code is finally exploited.
2549 *
2550 * 'action' can be '0' to replace method, '1' to replace path, '2' to replace
Christopher Faulet312294f2020-09-02 17:17:44 +02002551 * query string, 3 to replace uri or 4 to replace the path+query.
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002552 *
2553 * In query string case, the mark question '?' must be set at the start of the
2554 * string by the caller, event if the replacement query string is empty.
2555 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002556int http_req_replace_stline(int action, const char *replace, int len,
2557 struct proxy *px, struct stream *s)
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002558{
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002559 struct htx *htx = htxbuf(&s->req.buf);
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002560
2561 switch (action) {
2562 case 0: // method
2563 if (!http_replace_req_meth(htx, ist2(replace, len)))
2564 return -1;
2565 break;
2566
2567 case 1: // path
Christopher Fauletb8ce5052020-08-31 16:11:57 +02002568 if (!http_replace_req_path(htx, ist2(replace, len), 0))
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002569 return -1;
2570 break;
2571
2572 case 2: // query
2573 if (!http_replace_req_query(htx, ist2(replace, len)))
2574 return -1;
2575 break;
2576
2577 case 3: // uri
2578 if (!http_replace_req_uri(htx, ist2(replace, len)))
2579 return -1;
2580 break;
2581
Christopher Faulet312294f2020-09-02 17:17:44 +02002582 case 4: // path + query
2583 if (!http_replace_req_path(htx, ist2(replace, len), 1))
2584 return -1;
2585 break;
2586
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002587 default:
2588 return -1;
2589 }
2590 return 0;
2591}
2592
2593/* This function replace the HTTP status code and the associated message. The
Christopher Faulete00d06c2019-12-16 17:18:42 +01002594 * variable <status> contains the new status code. This function never fails. It
2595 * returns 0 in case of success, -1 in case of internal error.
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002596 */
Christopher Faulet96bff762019-12-17 13:46:18 +01002597int http_res_set_status(unsigned int status, struct ist reason, struct stream *s)
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002598{
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002599 struct htx *htx = htxbuf(&s->res.buf);
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002600 char *res;
2601
2602 chunk_reset(&trash);
2603 res = ultoa_o(status, trash.area, trash.size);
2604 trash.data = res - trash.area;
2605
2606 /* Do we have a custom reason format string? */
Tim Duesterhuse296d3e2020-03-05 17:56:31 +01002607 if (!isttest(reason)) {
Christopher Faulet96bff762019-12-17 13:46:18 +01002608 const char *str = http_get_reason(status);
Tim Duesterhusdcf753a2021-03-04 17:31:47 +01002609 reason = ist(str);
Christopher Faulet96bff762019-12-17 13:46:18 +01002610 }
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002611
Christopher Fauletbde2c4c2020-08-31 16:43:34 +02002612 if (!http_replace_res_status(htx, ist2(trash.area, trash.data), reason))
Christopher Faulete00d06c2019-12-16 17:18:42 +01002613 return -1;
Willy Tarreau640e2532023-01-10 14:50:44 +01002614 s->txn->status = status;
Christopher Faulete00d06c2019-12-16 17:18:42 +01002615 return 0;
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002616}
2617
Christopher Faulet3e964192018-10-24 11:39:23 +02002618/* Executes the http-request rules <rules> for stream <s>, proxy <px> and
2619 * transaction <txn>. Returns the verdict of the first rule that prevents
2620 * further processing of the request (auth, deny, ...), and defaults to
2621 * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or
2622 * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT
2623 * on txn->flags if it encounters a tarpit rule. If <deny_status> is not NULL
2624 * and a deny/tarpit rule is matched, it will be filled with this rule's deny
2625 * status.
2626 */
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002627static enum rule_result http_req_get_intercept_rule(struct proxy *px, struct list *def_rules,
2628 struct list *rules, struct stream *s)
Christopher Faulet3e964192018-10-24 11:39:23 +02002629{
2630 struct session *sess = strm_sess(s);
2631 struct http_txn *txn = s->txn;
Christopher Faulet3e964192018-10-24 11:39:23 +02002632 struct act_rule *rule;
Christopher Faulet3e964192018-10-24 11:39:23 +02002633 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002634 int act_opts = 0;
Christopher Faulet3e964192018-10-24 11:39:23 +02002635
Christopher Faulet3e964192018-10-24 11:39:23 +02002636 /* If "the current_rule_list" match the executed rule list, we are in
2637 * resume condition. If a resume is needed it is always in the action
2638 * and never in the ACL or converters. In this case, we initialise the
2639 * current rule, and go to the action execution point.
2640 */
2641 if (s->current_rule) {
2642 rule = s->current_rule;
2643 s->current_rule = NULL;
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002644 if (s->current_rule_list == rules || (def_rules && s->current_rule_list == def_rules))
Christopher Faulet3e964192018-10-24 11:39:23 +02002645 goto resume_execution;
2646 }
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002647 s->current_rule_list = ((!def_rules || s->current_rule_list == def_rules) ? rules : def_rules);
Christopher Faulet3e964192018-10-24 11:39:23 +02002648
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002649 restart:
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002650 /* start the ruleset evaluation in strict mode */
2651 txn->req.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01002652
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002653 list_for_each_entry(rule, s->current_rule_list, list) {
Christopher Faulet3e964192018-10-24 11:39:23 +02002654 /* check optional condition */
2655 if (rule->cond) {
2656 int ret;
2657
2658 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
2659 ret = acl_pass(ret);
2660
2661 if (rule->cond->pol == ACL_COND_UNLESS)
2662 ret = !ret;
2663
2664 if (!ret) /* condition not matched */
2665 continue;
2666 }
2667
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002668 act_opts |= ACT_OPT_FIRST;
Christopher Faulet3e964192018-10-24 11:39:23 +02002669 resume_execution:
Amaury Denoyelle03517732021-05-07 14:25:01 +02002670 if (rule->kw->flags & KWF_EXPERIMENTAL)
2671 mark_tainted(TAINTED_ACTION_EXP_EXECUTED);
2672
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002673 /* Always call the action function if defined */
2674 if (rule->action_ptr) {
Christopher Fauletd7bac882023-04-14 12:05:55 +02002675 if ((s->scf->flags & SC_FL_ERROR) ||
Christopher Fauletca5309a2023-04-17 16:17:32 +02002676 ((s->scf->flags & (SC_FL_EOS|SC_FL_ABRT_DONE)) &&
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002677 (px->options & PR_O_ABRT_CLOSE)))
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002678 act_opts |= ACT_OPT_FINAL;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002679
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002680 switch (rule->action_ptr(rule, px, sess, s, act_opts)) {
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002681 case ACT_RET_CONT:
2682 break;
2683 case ACT_RET_STOP:
2684 rule_ret = HTTP_RULE_RES_STOP;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002685 s->last_rule_file = rule->conf.file;
2686 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002687 goto end;
2688 case ACT_RET_YIELD:
2689 s->current_rule = rule;
2690 rule_ret = HTTP_RULE_RES_YIELD;
2691 goto end;
2692 case ACT_RET_ERR:
2693 rule_ret = HTTP_RULE_RES_ERROR;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002694 s->last_rule_file = rule->conf.file;
2695 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002696 goto end;
2697 case ACT_RET_DONE:
2698 rule_ret = HTTP_RULE_RES_DONE;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002699 s->last_rule_file = rule->conf.file;
2700 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002701 goto end;
2702 case ACT_RET_DENY:
Christopher Fauletb58f62b2020-01-13 16:40:13 +01002703 if (txn->status == -1)
2704 txn->status = 403;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002705 rule_ret = HTTP_RULE_RES_DENY;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002706 s->last_rule_file = rule->conf.file;
2707 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002708 goto end;
2709 case ACT_RET_ABRT:
2710 rule_ret = HTTP_RULE_RES_ABRT;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002711 s->last_rule_file = rule->conf.file;
2712 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002713 goto end;
2714 case ACT_RET_INV:
2715 rule_ret = HTTP_RULE_RES_BADREQ;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002716 s->last_rule_file = rule->conf.file;
2717 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002718 goto end;
2719 }
2720 continue; /* eval the next rule */
2721 }
2722
2723 /* If not action function defined, check for known actions */
Christopher Faulet3e964192018-10-24 11:39:23 +02002724 switch (rule->action) {
2725 case ACT_ACTION_ALLOW:
2726 rule_ret = HTTP_RULE_RES_STOP;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002727 s->last_rule_file = rule->conf.file;
2728 s->last_rule_line = rule->conf.line;
Christopher Faulet3e964192018-10-24 11:39:23 +02002729 goto end;
2730
2731 case ACT_ACTION_DENY:
Christopher Faulet5cb513a2020-05-13 17:56:56 +02002732 txn->status = rule->arg.http_reply->status;
2733 txn->http_reply = rule->arg.http_reply;
Christopher Faulet3e964192018-10-24 11:39:23 +02002734 rule_ret = HTTP_RULE_RES_DENY;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002735 s->last_rule_file = rule->conf.file;
2736 s->last_rule_line = rule->conf.line;
Christopher Faulet3e964192018-10-24 11:39:23 +02002737 goto end;
2738
2739 case ACT_HTTP_REQ_TARPIT:
2740 txn->flags |= TX_CLTARPIT;
Christopher Faulet5cb513a2020-05-13 17:56:56 +02002741 txn->status = rule->arg.http_reply->status;
2742 txn->http_reply = rule->arg.http_reply;
Christopher Faulet3e964192018-10-24 11:39:23 +02002743 rule_ret = HTTP_RULE_RES_DENY;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002744 s->last_rule_file = rule->conf.file;
2745 s->last_rule_line = rule->conf.line;
Christopher Faulet3e964192018-10-24 11:39:23 +02002746 goto end;
2747
Willy Tarreaubc1223b2021-09-02 16:54:33 +02002748 case ACT_HTTP_REDIR: {
2749 int ret = http_apply_redirect_rule(rule->arg.redir, s, txn);
2750
2751 if (ret == 2) // 2 == skip
2752 break;
2753
2754 rule_ret = ret ? HTTP_RULE_RES_ABRT : HTTP_RULE_RES_ERROR;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002755 s->last_rule_file = rule->conf.file;
2756 s->last_rule_line = rule->conf.line;
Christopher Faulet3e964192018-10-24 11:39:23 +02002757 goto end;
Willy Tarreaubc1223b2021-09-02 16:54:33 +02002758 }
Christopher Faulet3e964192018-10-24 11:39:23 +02002759
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002760 /* other flags exists, but normally, they never be matched. */
Christopher Faulet3e964192018-10-24 11:39:23 +02002761 default:
2762 break;
2763 }
2764 }
2765
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002766 if (def_rules && s->current_rule_list == def_rules) {
2767 s->current_rule_list = rules;
2768 goto restart;
2769 }
2770
Christopher Faulet3e964192018-10-24 11:39:23 +02002771 end:
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002772 /* if the ruleset evaluation is finished reset the strict mode */
Christopher Faulet46f95542019-12-20 10:07:22 +01002773 if (rule_ret != HTTP_RULE_RES_YIELD)
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002774 txn->req.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01002775
Christopher Faulet3e964192018-10-24 11:39:23 +02002776 /* we reached the end of the rules, nothing to report */
2777 return rule_ret;
2778}
2779
2780/* Executes the http-response rules <rules> for stream <s> and proxy <px>. It
2781 * returns one of 5 possible statuses: HTTP_RULE_RES_CONT, HTTP_RULE_RES_STOP,
2782 * HTTP_RULE_RES_DONE, HTTP_RULE_RES_YIELD, or HTTP_RULE_RES_BADREQ. If *CONT
2783 * is returned, the process can continue the evaluation of next rule list. If
2784 * *STOP or *DONE is returned, the process must stop the evaluation. If *BADREQ
2785 * is returned, it means the operation could not be processed and a server error
Christopher Fauleta53abad2020-05-13 08:12:22 +02002786 * must be returned. If *YIELD is returned, the caller must call again the
2787 * function with the same context.
Christopher Faulet3e964192018-10-24 11:39:23 +02002788 */
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002789static enum rule_result http_res_get_intercept_rule(struct proxy *px, struct list *def_rules,
2790 struct list *rules, struct stream *s)
Christopher Faulet3e964192018-10-24 11:39:23 +02002791{
2792 struct session *sess = strm_sess(s);
2793 struct http_txn *txn = s->txn;
Christopher Faulet3e964192018-10-24 11:39:23 +02002794 struct act_rule *rule;
Christopher Faulet3e964192018-10-24 11:39:23 +02002795 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002796 int act_opts = 0;
Christopher Faulet3e964192018-10-24 11:39:23 +02002797
Christopher Faulet3e964192018-10-24 11:39:23 +02002798 /* If "the current_rule_list" match the executed rule list, we are in
2799 * resume condition. If a resume is needed it is always in the action
2800 * and never in the ACL or converters. In this case, we initialise the
2801 * current rule, and go to the action execution point.
2802 */
2803 if (s->current_rule) {
2804 rule = s->current_rule;
2805 s->current_rule = NULL;
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002806 if (s->current_rule_list == rules || (def_rules && s->current_rule_list == def_rules))
Christopher Faulet3e964192018-10-24 11:39:23 +02002807 goto resume_execution;
2808 }
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002809 s->current_rule_list = ((!def_rules || s->current_rule_list == def_rules) ? rules : def_rules);
2810
2811 restart:
Christopher Faulet3e964192018-10-24 11:39:23 +02002812
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002813 /* start the ruleset evaluation in strict mode */
2814 txn->rsp.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01002815
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002816 list_for_each_entry(rule, s->current_rule_list, list) {
Christopher Faulet3e964192018-10-24 11:39:23 +02002817 /* check optional condition */
2818 if (rule->cond) {
2819 int ret;
2820
2821 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
2822 ret = acl_pass(ret);
2823
2824 if (rule->cond->pol == ACL_COND_UNLESS)
2825 ret = !ret;
2826
2827 if (!ret) /* condition not matched */
2828 continue;
2829 }
2830
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002831 act_opts |= ACT_OPT_FIRST;
Christopher Faulet3e964192018-10-24 11:39:23 +02002832resume_execution:
Amaury Denoyelle03517732021-05-07 14:25:01 +02002833 if (rule->kw->flags & KWF_EXPERIMENTAL)
2834 mark_tainted(TAINTED_ACTION_EXP_EXECUTED);
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002835
2836 /* Always call the action function if defined */
2837 if (rule->action_ptr) {
Christopher Fauletd7bac882023-04-14 12:05:55 +02002838 if ((s->scf->flags & SC_FL_ERROR) ||
Christopher Fauletca5309a2023-04-17 16:17:32 +02002839 ((s->scf->flags & (SC_FL_EOS|SC_FL_ABRT_DONE)) &&
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002840 (px->options & PR_O_ABRT_CLOSE)))
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002841 act_opts |= ACT_OPT_FINAL;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002842
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002843 switch (rule->action_ptr(rule, px, sess, s, act_opts)) {
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002844 case ACT_RET_CONT:
2845 break;
2846 case ACT_RET_STOP:
2847 rule_ret = HTTP_RULE_RES_STOP;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002848 s->last_rule_file = rule->conf.file;
2849 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002850 goto end;
2851 case ACT_RET_YIELD:
2852 s->current_rule = rule;
2853 rule_ret = HTTP_RULE_RES_YIELD;
2854 goto end;
2855 case ACT_RET_ERR:
2856 rule_ret = HTTP_RULE_RES_ERROR;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002857 s->last_rule_file = rule->conf.file;
2858 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002859 goto end;
2860 case ACT_RET_DONE:
2861 rule_ret = HTTP_RULE_RES_DONE;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002862 s->last_rule_file = rule->conf.file;
2863 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002864 goto end;
2865 case ACT_RET_DENY:
Christopher Fauletb58f62b2020-01-13 16:40:13 +01002866 if (txn->status == -1)
2867 txn->status = 502;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002868 rule_ret = HTTP_RULE_RES_DENY;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002869 s->last_rule_file = rule->conf.file;
2870 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002871 goto end;
2872 case ACT_RET_ABRT:
2873 rule_ret = HTTP_RULE_RES_ABRT;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002874 s->last_rule_file = rule->conf.file;
2875 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002876 goto end;
2877 case ACT_RET_INV:
2878 rule_ret = HTTP_RULE_RES_BADREQ;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002879 s->last_rule_file = rule->conf.file;
2880 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002881 goto end;
2882 }
2883 continue; /* eval the next rule */
2884 }
2885
2886 /* If not action function defined, check for known actions */
Christopher Faulet3e964192018-10-24 11:39:23 +02002887 switch (rule->action) {
2888 case ACT_ACTION_ALLOW:
2889 rule_ret = HTTP_RULE_RES_STOP; /* "allow" rules are OK */
Willy Tarreauc6dae862022-03-09 17:23:10 +01002890 s->last_rule_file = rule->conf.file;
2891 s->last_rule_line = rule->conf.line;
Christopher Faulet3e964192018-10-24 11:39:23 +02002892 goto end;
2893
2894 case ACT_ACTION_DENY:
Christopher Faulet5cb513a2020-05-13 17:56:56 +02002895 txn->status = rule->arg.http_reply->status;
2896 txn->http_reply = rule->arg.http_reply;
Christopher Faulet3a26bee2019-12-16 12:47:40 +01002897 rule_ret = HTTP_RULE_RES_DENY;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002898 s->last_rule_file = rule->conf.file;
2899 s->last_rule_line = rule->conf.line;
Christopher Faulet3e964192018-10-24 11:39:23 +02002900 goto end;
2901
Willy Tarreaubc1223b2021-09-02 16:54:33 +02002902 case ACT_HTTP_REDIR: {
2903 int ret = http_apply_redirect_rule(rule->arg.redir, s, txn);
Christopher Faulet3e964192018-10-24 11:39:23 +02002904
Willy Tarreaubc1223b2021-09-02 16:54:33 +02002905 if (ret == 2) // 2 == skip
2906 break;
2907
2908 rule_ret = ret ? HTTP_RULE_RES_ABRT : HTTP_RULE_RES_ERROR;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002909 s->last_rule_file = rule->conf.file;
2910 s->last_rule_line = rule->conf.line;
Willy Tarreaubc1223b2021-09-02 16:54:33 +02002911 goto end;
2912 }
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002913 /* other flags exists, but normally, they never be matched. */
Christopher Faulet3e964192018-10-24 11:39:23 +02002914 default:
2915 break;
2916 }
2917 }
2918
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002919 if (def_rules && s->current_rule_list == def_rules) {
2920 s->current_rule_list = rules;
2921 goto restart;
2922 }
2923
Christopher Faulet3e964192018-10-24 11:39:23 +02002924 end:
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002925 /* if the ruleset evaluation is finished reset the strict mode */
Christopher Faulet46f95542019-12-20 10:07:22 +01002926 if (rule_ret != HTTP_RULE_RES_YIELD)
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002927 txn->rsp.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01002928
Christopher Faulet3e964192018-10-24 11:39:23 +02002929 /* we reached the end of the rules, nothing to report */
2930 return rule_ret;
2931}
2932
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002933/* Executes backend and frontend http-after-response rules for the stream <s>,
2934 * in that order. it return 1 on success and 0 on error. It is the caller
2935 * responsibility to catch error or ignore it. If it catches it, this function
2936 * may be called a second time, for the internal error.
2937 */
2938int http_eval_after_res_rules(struct stream *s)
2939{
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002940 struct list *def_rules, *rules;
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002941 struct session *sess = s->sess;
2942 enum rule_result ret = HTTP_RULE_RES_CONT;
2943
Christopher Faulet507479b2020-05-15 12:29:46 +02002944 /* Eval after-response ruleset only if the reply is not const */
2945 if (s->txn->flags & TX_CONST_REPLY)
2946 goto end;
2947
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002948 /* prune the request variables if not already done and swap to the response variables. */
2949 if (s->vars_reqres.scope != SCOPE_RES) {
2950 if (!LIST_ISEMPTY(&s->vars_reqres.head))
2951 vars_prune(&s->vars_reqres, s->sess, s);
Willy Tarreaub7bfcb32021-08-31 08:13:25 +02002952 vars_init_head(&s->vars_reqres, SCOPE_RES);
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002953 }
2954
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002955 def_rules = (s->be->defpx ? &s->be->defpx->http_after_res_rules : NULL);
2956 rules = &s->be->http_after_res_rules;
2957
2958 ret = http_res_get_intercept_rule(s->be, def_rules, rules, s);
Christopher Faulet4c5a5912021-11-09 17:48:39 +01002959 if ((ret == HTTP_RULE_RES_CONT || ret == HTTP_RULE_RES_STOP) && sess->fe != s->be) {
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002960 def_rules = ((sess->fe->defpx && sess->fe->defpx != s->be->defpx) ? &sess->fe->defpx->http_after_res_rules : NULL);
2961 rules = &sess->fe->http_after_res_rules;
2962 ret = http_res_get_intercept_rule(sess->fe, def_rules, rules, s);
2963 }
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002964
Christopher Faulet507479b2020-05-15 12:29:46 +02002965 end:
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002966 /* All other codes than CONTINUE, STOP or DONE are forbidden */
2967 return (ret == HTTP_RULE_RES_CONT || ret == HTTP_RULE_RES_STOP || ret == HTTP_RULE_RES_DONE);
2968}
2969
Christopher Fauletfcda7c62018-10-24 11:56:22 +02002970/*
2971 * Manage client-side cookie. It can impact performance by about 2% so it is
2972 * desirable to call it only when needed. This code is quite complex because
2973 * of the multiple very crappy and ambiguous syntaxes we have to support. it
2974 * highly recommended not to touch this part without a good reason !
2975 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002976static void http_manage_client_side_cookies(struct stream *s, struct channel *req)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02002977{
2978 struct session *sess = s->sess;
2979 struct http_txn *txn = s->txn;
2980 struct htx *htx;
2981 struct http_hdr_ctx ctx;
2982 char *hdr_beg, *hdr_end, *del_from;
2983 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
2984 int preserve_hdr;
2985
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002986 htx = htxbuf(&req->buf);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02002987 ctx.blk = NULL;
2988 while (http_find_header(htx, ist("Cookie"), &ctx, 1)) {
Olivier Houchardf0f42382019-07-22 17:43:46 +02002989 int is_first = 1;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02002990 del_from = NULL; /* nothing to be deleted */
2991 preserve_hdr = 0; /* assume we may kill the whole header */
2992
2993 /* Now look for cookies. Conforming to RFC2109, we have to support
2994 * attributes whose name begin with a '$', and associate them with
2995 * the right cookie, if we want to delete this cookie.
2996 * So there are 3 cases for each cookie read :
2997 * 1) it's a special attribute, beginning with a '$' : ignore it.
2998 * 2) it's a server id cookie that we *MAY* want to delete : save
2999 * some pointers on it (last semi-colon, beginning of cookie...)
3000 * 3) it's an application cookie : we *MAY* have to delete a previous
3001 * "special" cookie.
3002 * At the end of loop, if a "special" cookie remains, we may have to
3003 * remove it. If no application cookie persists in the header, we
3004 * *MUST* delete it.
3005 *
3006 * Note: RFC2965 is unclear about the processing of spaces around
3007 * the equal sign in the ATTR=VALUE form. A careful inspection of
3008 * the RFC explicitly allows spaces before it, and not within the
3009 * tokens (attrs or values). An inspection of RFC2109 allows that
3010 * too but section 10.1.3 lets one think that spaces may be allowed
3011 * after the equal sign too, resulting in some (rare) buggy
3012 * implementations trying to do that. So let's do what servers do.
3013 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
3014 * allowed quoted strings in values, with any possible character
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003015 * after a backslash, including control chars and delimiters, which
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003016 * causes parsing to become ambiguous. Browsers also allow spaces
3017 * within values even without quotes.
3018 *
3019 * We have to keep multiple pointers in order to support cookie
3020 * removal at the beginning, middle or end of header without
3021 * corrupting the header. All of these headers are valid :
3022 *
3023 * hdr_beg hdr_end
3024 * | |
3025 * v |
3026 * NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3 |
3027 * NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3 v
3028 * NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3
3029 * | | | | | | |
3030 * | | | | | | |
3031 * | | | | | | +--> next
3032 * | | | | | +----> val_end
3033 * | | | | +-----------> val_beg
3034 * | | | +--------------> equal
3035 * | | +----------------> att_end
3036 * | +---------------------> att_beg
3037 * +--------------------------> prev
3038 *
3039 */
3040 hdr_beg = ctx.value.ptr;
3041 hdr_end = hdr_beg + ctx.value.len;
3042 for (prev = hdr_beg; prev < hdr_end; prev = next) {
3043 /* Iterate through all cookies on this line */
3044
3045 /* find att_beg */
3046 att_beg = prev;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003047 if (!is_first)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003048 att_beg++;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003049 is_first = 0;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003050
3051 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
3052 att_beg++;
3053
3054 /* find att_end : this is the first character after the last non
3055 * space before the equal. It may be equal to hdr_end.
3056 */
3057 equal = att_end = att_beg;
3058 while (equal < hdr_end) {
3059 if (*equal == '=' || *equal == ',' || *equal == ';')
3060 break;
3061 if (HTTP_IS_SPHT(*equal++))
3062 continue;
3063 att_end = equal;
3064 }
3065
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003066 /* here, <equal> points to '=', a delimiter or the end. <att_end>
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003067 * is between <att_beg> and <equal>, both may be identical.
3068 */
3069 /* look for end of cookie if there is an equal sign */
3070 if (equal < hdr_end && *equal == '=') {
3071 /* look for the beginning of the value */
3072 val_beg = equal + 1;
3073 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
3074 val_beg++;
3075
3076 /* find the end of the value, respecting quotes */
3077 next = http_find_cookie_value_end(val_beg, hdr_end);
3078
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003079 /* make val_end point to the first white space or delimiter after the value */
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003080 val_end = next;
3081 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
3082 val_end--;
3083 }
3084 else
3085 val_beg = val_end = next = equal;
3086
3087 /* We have nothing to do with attributes beginning with
3088 * '$'. However, they will automatically be removed if a
3089 * header before them is removed, since they're supposed
3090 * to be linked together.
3091 */
3092 if (*att_beg == '$')
3093 continue;
3094
3095 /* Ignore cookies with no equal sign */
3096 if (equal == next) {
3097 /* This is not our cookie, so we must preserve it. But if we already
3098 * scheduled another cookie for removal, we cannot remove the
3099 * complete header, but we can remove the previous block itself.
3100 */
3101 preserve_hdr = 1;
3102 if (del_from != NULL) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003103 int delta = http_del_hdr_value(hdr_beg, hdr_end, &del_from, prev);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003104 val_end += delta;
3105 next += delta;
3106 hdr_end += delta;
3107 prev = del_from;
3108 del_from = NULL;
3109 }
3110 continue;
3111 }
3112
3113 /* if there are spaces around the equal sign, we need to
3114 * strip them otherwise we'll get trouble for cookie captures,
3115 * or even for rewrites. Since this happens extremely rarely,
3116 * it does not hurt performance.
3117 */
3118 if (unlikely(att_end != equal || val_beg > equal + 1)) {
3119 int stripped_before = 0;
3120 int stripped_after = 0;
3121
3122 if (att_end != equal) {
3123 memmove(att_end, equal, hdr_end - equal);
3124 stripped_before = (att_end - equal);
3125 equal += stripped_before;
3126 val_beg += stripped_before;
3127 }
3128
3129 if (val_beg > equal + 1) {
3130 memmove(equal + 1, val_beg, hdr_end + stripped_before - val_beg);
3131 stripped_after = (equal + 1) - val_beg;
3132 val_beg += stripped_after;
3133 stripped_before += stripped_after;
3134 }
3135
3136 val_end += stripped_before;
3137 next += stripped_before;
3138 hdr_end += stripped_before;
3139 }
3140 /* now everything is as on the diagram above */
3141
3142 /* First, let's see if we want to capture this cookie. We check
3143 * that we don't already have a client side cookie, because we
3144 * can only capture one. Also as an optimisation, we ignore
3145 * cookies shorter than the declared name.
3146 */
3147 if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL &&
3148 (val_end - att_beg >= sess->fe->capture_namelen) &&
3149 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
3150 int log_len = val_end - att_beg;
3151
3152 if ((txn->cli_cookie = pool_alloc(pool_head_capture)) == NULL) {
3153 ha_alert("HTTP logging : out of memory.\n");
3154 } else {
3155 if (log_len > sess->fe->capture_len)
3156 log_len = sess->fe->capture_len;
3157 memcpy(txn->cli_cookie, att_beg, log_len);
3158 txn->cli_cookie[log_len] = 0;
3159 }
3160 }
3161
3162 /* Persistence cookies in passive, rewrite or insert mode have the
3163 * following form :
3164 *
3165 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
3166 *
3167 * For cookies in prefix mode, the form is :
3168 *
3169 * Cookie: NAME=SRV~VALUE
3170 */
3171 if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
3172 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
3173 struct server *srv = s->be->srv;
3174 char *delim;
3175
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003176 /* if we're in cookie prefix mode, we'll search the delimiter so that we
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003177 * have the server ID between val_beg and delim, and the original cookie between
3178 * delim+1 and val_end. Otherwise, delim==val_end :
3179 *
3180 * hdr_beg
3181 * |
3182 * v
3183 * NAME=SRV; # in all but prefix modes
3184 * NAME=SRV~OPAQUE ; # in prefix mode
3185 * || || | |+-> next
3186 * || || | +--> val_end
3187 * || || +---------> delim
3188 * || |+------------> val_beg
3189 * || +-------------> att_end = equal
3190 * |+-----------------> att_beg
3191 * +------------------> prev
3192 *
3193 */
3194 if (s->be->ck_opts & PR_CK_PFX) {
3195 for (delim = val_beg; delim < val_end; delim++)
3196 if (*delim == COOKIE_DELIM)
3197 break;
3198 }
3199 else {
3200 char *vbar1;
3201 delim = val_end;
3202 /* Now check if the cookie contains a date field, which would
3203 * appear after a vertical bar ('|') just after the server name
3204 * and before the delimiter.
3205 */
3206 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
3207 if (vbar1) {
3208 /* OK, so left of the bar is the server's cookie and
3209 * right is the last seen date. It is a base64 encoded
3210 * 30-bit value representing the UNIX date since the
3211 * epoch in 4-second quantities.
3212 */
3213 int val;
3214 delim = vbar1++;
3215 if (val_end - vbar1 >= 5) {
3216 val = b64tos30(vbar1);
3217 if (val > 0)
3218 txn->cookie_last_date = val << 2;
3219 }
3220 /* look for a second vertical bar */
3221 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
3222 if (vbar1 && (val_end - vbar1 > 5)) {
3223 val = b64tos30(vbar1 + 1);
3224 if (val > 0)
3225 txn->cookie_first_date = val << 2;
3226 }
3227 }
3228 }
3229
3230 /* if the cookie has an expiration date and the proxy wants to check
3231 * it, then we do that now. We first check if the cookie is too old,
3232 * then only if it has expired. We detect strict overflow because the
3233 * time resolution here is not great (4 seconds). Cookies with dates
3234 * in the future are ignored if their offset is beyond one day. This
3235 * allows an admin to fix timezone issues without expiring everyone
3236 * and at the same time avoids keeping unwanted side effects for too
3237 * long.
3238 */
3239 if (txn->cookie_first_date && s->be->cookie_maxlife &&
3240 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) ||
3241 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
3242 txn->flags &= ~TX_CK_MASK;
3243 txn->flags |= TX_CK_OLD;
3244 delim = val_beg; // let's pretend we have not found the cookie
3245 txn->cookie_first_date = 0;
3246 txn->cookie_last_date = 0;
3247 }
3248 else if (txn->cookie_last_date && s->be->cookie_maxidle &&
3249 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) ||
3250 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
3251 txn->flags &= ~TX_CK_MASK;
3252 txn->flags |= TX_CK_EXPIRED;
3253 delim = val_beg; // let's pretend we have not found the cookie
3254 txn->cookie_first_date = 0;
3255 txn->cookie_last_date = 0;
3256 }
3257
3258 /* Here, we'll look for the first running server which supports the cookie.
3259 * This allows to share a same cookie between several servers, for example
3260 * to dedicate backup servers to specific servers only.
3261 * However, to prevent clients from sticking to cookie-less backup server
3262 * when they have incidentely learned an empty cookie, we simply ignore
3263 * empty cookies and mark them as invalid.
3264 * The same behaviour is applied when persistence must be ignored.
3265 */
3266 if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
3267 srv = NULL;
3268
3269 while (srv) {
3270 if (srv->cookie && (srv->cklen == delim - val_beg) &&
3271 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
3272 if ((srv->cur_state != SRV_ST_STOPPED) ||
3273 (s->be->options & PR_O_PERSIST) ||
3274 (s->flags & SF_FORCE_PRST)) {
3275 /* we found the server and we can use it */
3276 txn->flags &= ~TX_CK_MASK;
3277 txn->flags |= (srv->cur_state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
3278 s->flags |= SF_DIRECT | SF_ASSIGNED;
3279 s->target = &srv->obj_type;
3280 break;
3281 } else {
3282 /* we found a server, but it's down,
3283 * mark it as such and go on in case
3284 * another one is available.
3285 */
3286 txn->flags &= ~TX_CK_MASK;
3287 txn->flags |= TX_CK_DOWN;
3288 }
3289 }
3290 srv = srv->next;
3291 }
3292
3293 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
3294 /* no server matched this cookie or we deliberately skipped it */
3295 txn->flags &= ~TX_CK_MASK;
3296 if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
3297 txn->flags |= TX_CK_UNUSED;
3298 else
3299 txn->flags |= TX_CK_INVALID;
3300 }
3301
3302 /* depending on the cookie mode, we may have to either :
3303 * - delete the complete cookie if we're in insert+indirect mode, so that
3304 * the server never sees it ;
3305 * - remove the server id from the cookie value, and tag the cookie as an
Joseph Herlante9d5c722018-11-25 11:00:25 -08003306 * application cookie so that it does not get accidentally removed later,
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003307 * if we're in cookie prefix mode
3308 */
3309 if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
3310 int delta; /* negative */
3311
3312 memmove(val_beg, delim + 1, hdr_end - (delim + 1));
3313 delta = val_beg - (delim + 1);
3314 val_end += delta;
3315 next += delta;
3316 hdr_end += delta;
3317 del_from = NULL;
3318 preserve_hdr = 1; /* we want to keep this cookie */
3319 }
3320 else if (del_from == NULL &&
3321 (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
3322 del_from = prev;
3323 }
3324 }
3325 else {
3326 /* This is not our cookie, so we must preserve it. But if we already
3327 * scheduled another cookie for removal, we cannot remove the
3328 * complete header, but we can remove the previous block itself.
3329 */
3330 preserve_hdr = 1;
3331
3332 if (del_from != NULL) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003333 int delta = http_del_hdr_value(hdr_beg, hdr_end, &del_from, prev);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003334 if (att_beg >= del_from)
3335 att_beg += delta;
3336 if (att_end >= del_from)
3337 att_end += delta;
3338 val_beg += delta;
3339 val_end += delta;
3340 next += delta;
3341 hdr_end += delta;
3342 prev = del_from;
3343 del_from = NULL;
3344 }
3345 }
3346
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003347 } /* for each cookie */
3348
3349
3350 /* There are no more cookies on this line.
3351 * We may still have one (or several) marked for deletion at the
3352 * end of the line. We must do this now in two ways :
3353 * - if some cookies must be preserved, we only delete from the
3354 * mark to the end of line ;
3355 * - if nothing needs to be preserved, simply delete the whole header
3356 */
3357 if (del_from) {
3358 hdr_end = (preserve_hdr ? del_from : hdr_beg);
3359 }
3360 if ((hdr_end - hdr_beg) != ctx.value.len) {
Christopher Faulet3e2638e2019-06-18 09:49:16 +02003361 if (hdr_beg != hdr_end)
3362 htx_change_blk_value_len(htx, ctx.blk, hdr_end - hdr_beg);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003363 else
3364 http_remove_header(htx, &ctx);
3365 }
3366 } /* for each "Cookie header */
3367}
3368
3369/*
3370 * Manage server-side cookies. It can impact performance by about 2% so it is
3371 * desirable to call it only when needed. This function is also used when we
3372 * just need to know if there is a cookie (eg: for check-cache).
3373 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003374static void http_manage_server_side_cookies(struct stream *s, struct channel *res)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003375{
3376 struct session *sess = s->sess;
3377 struct http_txn *txn = s->txn;
3378 struct htx *htx;
3379 struct http_hdr_ctx ctx;
3380 struct server *srv;
3381 char *hdr_beg, *hdr_end;
3382 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003383
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003384 htx = htxbuf(&res->buf);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003385
3386 ctx.blk = NULL;
Willy Tarreauaa1909e2022-11-14 18:58:35 +01003387 while (http_find_header(htx, ist("Set-Cookie"), &ctx, 1)) {
Olivier Houchardf0f42382019-07-22 17:43:46 +02003388 int is_first = 1;
3389
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003390 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
3391 * <prev> points to the colon.
3392 */
3393 txn->flags |= TX_SCK_PRESENT;
3394
3395 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
3396 * check-cache is enabled) and we are not interested in checking
3397 * them. Warning, the cookie capture is declared in the frontend.
3398 */
3399 if (s->be->cookie_name == NULL && sess->fe->capture_name == NULL)
3400 break;
3401
3402 /* OK so now we know we have to process this response cookie.
3403 * The format of the Set-Cookie header is slightly different
3404 * from the format of the Cookie header in that it does not
3405 * support the comma as a cookie delimiter (thus the header
3406 * cannot be folded) because the Expires attribute described in
3407 * the original Netscape's spec may contain an unquoted date
3408 * with a comma inside. We have to live with this because
3409 * many browsers don't support Max-Age and some browsers don't
3410 * support quoted strings. However the Set-Cookie2 header is
Willy Tarreauaa1909e2022-11-14 18:58:35 +01003411 * clean but basically nobody supports it.
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003412 *
3413 * We have to keep multiple pointers in order to support cookie
3414 * removal at the beginning, middle or end of header without
3415 * corrupting the header (in case of set-cookie2). A special
3416 * pointer, <scav> points to the beginning of the set-cookie-av
3417 * fields after the first semi-colon. The <next> pointer points
3418 * either to the end of line (set-cookie) or next unquoted comma
3419 * (set-cookie2). All of these headers are valid :
3420 *
3421 * hdr_beg hdr_end
3422 * | |
3423 * v |
3424 * NAME1 = VALUE 1 ; Secure; Path="/" |
3425 * NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT v
3426 * NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT
3427 * NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard
3428 * | | | | | | | |
3429 * | | | | | | | +-> next
3430 * | | | | | | +------------> scav
3431 * | | | | | +--------------> val_end
3432 * | | | | +--------------------> val_beg
3433 * | | | +----------------------> equal
3434 * | | +------------------------> att_end
3435 * | +----------------------------> att_beg
3436 * +------------------------------> prev
3437 * -------------------------------> hdr_beg
3438 */
3439 hdr_beg = ctx.value.ptr;
3440 hdr_end = hdr_beg + ctx.value.len;
3441 for (prev = hdr_beg; prev < hdr_end; prev = next) {
3442
3443 /* Iterate through all cookies on this line */
3444
3445 /* find att_beg */
3446 att_beg = prev;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003447 if (!is_first)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003448 att_beg++;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003449 is_first = 0;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003450
3451 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
3452 att_beg++;
3453
3454 /* find att_end : this is the first character after the last non
3455 * space before the equal. It may be equal to hdr_end.
3456 */
3457 equal = att_end = att_beg;
3458
3459 while (equal < hdr_end) {
Willy Tarreauaa1909e2022-11-14 18:58:35 +01003460 if (*equal == '=' || *equal == ';')
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003461 break;
3462 if (HTTP_IS_SPHT(*equal++))
3463 continue;
3464 att_end = equal;
3465 }
3466
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003467 /* here, <equal> points to '=', a delimiter or the end. <att_end>
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003468 * is between <att_beg> and <equal>, both may be identical.
3469 */
3470
3471 /* look for end of cookie if there is an equal sign */
3472 if (equal < hdr_end && *equal == '=') {
3473 /* look for the beginning of the value */
3474 val_beg = equal + 1;
3475 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
3476 val_beg++;
3477
3478 /* find the end of the value, respecting quotes */
3479 next = http_find_cookie_value_end(val_beg, hdr_end);
3480
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003481 /* make val_end point to the first white space or delimiter after the value */
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003482 val_end = next;
3483 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
3484 val_end--;
3485 }
3486 else {
3487 /* <equal> points to next comma, semi-colon or EOL */
3488 val_beg = val_end = next = equal;
3489 }
3490
3491 if (next < hdr_end) {
Willy Tarreauaa1909e2022-11-14 18:58:35 +01003492 /* For Set-Cookie, since commas are permitted
3493 * in values, skip to the end.
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003494 */
Willy Tarreauaa1909e2022-11-14 18:58:35 +01003495 next = hdr_end;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003496 }
3497
3498 /* Now everything is as on the diagram above */
3499
3500 /* Ignore cookies with no equal sign */
3501 if (equal == val_end)
3502 continue;
3503
3504 /* If there are spaces around the equal sign, we need to
3505 * strip them otherwise we'll get trouble for cookie captures,
3506 * or even for rewrites. Since this happens extremely rarely,
3507 * it does not hurt performance.
3508 */
3509 if (unlikely(att_end != equal || val_beg > equal + 1)) {
3510 int stripped_before = 0;
3511 int stripped_after = 0;
3512
3513 if (att_end != equal) {
3514 memmove(att_end, equal, hdr_end - equal);
3515 stripped_before = (att_end - equal);
3516 equal += stripped_before;
3517 val_beg += stripped_before;
3518 }
3519
3520 if (val_beg > equal + 1) {
3521 memmove(equal + 1, val_beg, hdr_end + stripped_before - val_beg);
3522 stripped_after = (equal + 1) - val_beg;
3523 val_beg += stripped_after;
3524 stripped_before += stripped_after;
3525 }
3526
3527 val_end += stripped_before;
3528 next += stripped_before;
3529 hdr_end += stripped_before;
3530
Christopher Faulet3e2638e2019-06-18 09:49:16 +02003531 htx_change_blk_value_len(htx, ctx.blk, hdr_end - hdr_beg);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003532 ctx.value.len = hdr_end - hdr_beg;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003533 }
3534
3535 /* First, let's see if we want to capture this cookie. We check
3536 * that we don't already have a server side cookie, because we
3537 * can only capture one. Also as an optimisation, we ignore
3538 * cookies shorter than the declared name.
3539 */
3540 if (sess->fe->capture_name != NULL &&
3541 txn->srv_cookie == NULL &&
3542 (val_end - att_beg >= sess->fe->capture_namelen) &&
3543 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
3544 int log_len = val_end - att_beg;
3545 if ((txn->srv_cookie = pool_alloc(pool_head_capture)) == NULL) {
3546 ha_alert("HTTP logging : out of memory.\n");
3547 }
3548 else {
3549 if (log_len > sess->fe->capture_len)
3550 log_len = sess->fe->capture_len;
3551 memcpy(txn->srv_cookie, att_beg, log_len);
3552 txn->srv_cookie[log_len] = 0;
3553 }
3554 }
3555
3556 srv = objt_server(s->target);
3557 /* now check if we need to process it for persistence */
3558 if (!(s->flags & SF_IGNORE_PRST) &&
3559 (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
3560 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
3561 /* assume passive cookie by default */
3562 txn->flags &= ~TX_SCK_MASK;
3563 txn->flags |= TX_SCK_FOUND;
3564
3565 /* If the cookie is in insert mode on a known server, we'll delete
3566 * this occurrence because we'll insert another one later.
3567 * We'll delete it too if the "indirect" option is set and we're in
3568 * a direct access.
3569 */
3570 if (s->be->ck_opts & PR_CK_PSV) {
3571 /* The "preserve" flag was set, we don't want to touch the
3572 * server's cookie.
3573 */
3574 }
3575 else if ((srv && (s->be->ck_opts & PR_CK_INS)) ||
3576 ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) {
3577 /* this cookie must be deleted */
3578 if (prev == hdr_beg && next == hdr_end) {
3579 /* whole header */
3580 http_remove_header(htx, &ctx);
3581 /* note: while both invalid now, <next> and <hdr_end>
3582 * are still equal, so the for() will stop as expected.
3583 */
3584 } else {
3585 /* just remove the value */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003586 int delta = http_del_hdr_value(hdr_beg, hdr_end, &prev, next);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003587 next = prev;
3588 hdr_end += delta;
3589 }
3590 txn->flags &= ~TX_SCK_MASK;
3591 txn->flags |= TX_SCK_DELETED;
3592 /* and go on with next cookie */
3593 }
3594 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) {
3595 /* replace bytes val_beg->val_end with the cookie name associated
3596 * with this server since we know it.
3597 */
3598 int sliding, delta;
3599
3600 ctx.value = ist2(val_beg, val_end - val_beg);
3601 ctx.lws_before = ctx.lws_after = 0;
3602 http_replace_header_value(htx, &ctx, ist2(srv->cookie, srv->cklen));
3603 delta = srv->cklen - (val_end - val_beg);
3604 sliding = (ctx.value.ptr - val_beg);
3605 hdr_beg += sliding;
3606 val_beg += sliding;
3607 next += sliding + delta;
3608 hdr_end += sliding + delta;
3609
3610 txn->flags &= ~TX_SCK_MASK;
3611 txn->flags |= TX_SCK_REPLACED;
3612 }
3613 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) {
3614 /* insert the cookie name associated with this server
3615 * before existing cookie, and insert a delimiter between them..
3616 */
3617 int sliding, delta;
3618 ctx.value = ist2(val_beg, 0);
3619 ctx.lws_before = ctx.lws_after = 0;
3620 http_replace_header_value(htx, &ctx, ist2(srv->cookie, srv->cklen + 1));
3621 delta = srv->cklen + 1;
3622 sliding = (ctx.value.ptr - val_beg);
3623 hdr_beg += sliding;
3624 val_beg += sliding;
3625 next += sliding + delta;
3626 hdr_end += sliding + delta;
3627
3628 val_beg[srv->cklen] = COOKIE_DELIM;
3629 txn->flags &= ~TX_SCK_MASK;
3630 txn->flags |= TX_SCK_REPLACED;
3631 }
3632 }
3633 /* that's done for this cookie, check the next one on the same
Willy Tarreauaa1909e2022-11-14 18:58:35 +01003634 * line when next != hdr_end (which should normally not happen
3635 * with set-cookie2 support removed).
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003636 */
3637 }
3638 }
3639}
3640
Christopher Faulet25a02f62018-10-24 12:00:25 +02003641/*
3642 * Parses the Cache-Control and Pragma request header fields to determine if
3643 * the request may be served from the cache and/or if it is cacheable. Updates
3644 * s->txn->flags.
3645 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003646void http_check_request_for_cacheability(struct stream *s, struct channel *req)
Christopher Faulet25a02f62018-10-24 12:00:25 +02003647{
3648 struct http_txn *txn = s->txn;
3649 struct htx *htx;
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003650 struct http_hdr_ctx ctx = { .blk = NULL };
3651 int pragma_found, cc_found;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003652
3653 if ((txn->flags & (TX_CACHEABLE|TX_CACHE_IGNORE)) == TX_CACHE_IGNORE)
3654 return; /* nothing more to do here */
3655
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003656 htx = htxbuf(&req->buf);
Christopher Faulet25a02f62018-10-24 12:00:25 +02003657 pragma_found = cc_found = 0;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003658
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003659 /* Check "pragma" header for HTTP/1.0 compatibility. */
3660 if (http_find_header(htx, ist("pragma"), &ctx, 1)) {
3661 if (isteqi(ctx.value, ist("no-cache"))) {
3662 pragma_found = 1;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003663 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003664 }
Christopher Faulet25a02f62018-10-24 12:00:25 +02003665
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003666 ctx.blk = NULL;
3667 /* Don't use the cache and don't try to store if we found the
3668 * Authorization header */
3669 if (http_find_header(htx, ist("authorization"), &ctx, 1)) {
3670 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3671 txn->flags |= TX_CACHE_IGNORE;
3672 }
Christopher Faulet25a02f62018-10-24 12:00:25 +02003673
Christopher Faulet25a02f62018-10-24 12:00:25 +02003674
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003675 /* Look for "cache-control" header and iterate over all the values
3676 * until we find one that specifies that caching is possible or not. */
3677 ctx.blk = NULL;
3678 while (http_find_header(htx, ist("cache-control"), &ctx, 0)) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003679 cc_found = 1;
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003680 /* We don't check the values after max-age, max-stale nor min-fresh,
3681 * we simply don't use the cache when they're specified. */
3682 if (istmatchi(ctx.value, ist("max-age")) ||
3683 istmatchi(ctx.value, ist("no-cache")) ||
3684 istmatchi(ctx.value, ist("max-stale")) ||
3685 istmatchi(ctx.value, ist("min-fresh"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003686 txn->flags |= TX_CACHE_IGNORE;
3687 continue;
3688 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003689 if (istmatchi(ctx.value, ist("no-store"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003690 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3691 continue;
3692 }
3693 }
3694
3695 /* RFC7234#5.4:
3696 * When the Cache-Control header field is also present and
3697 * understood in a request, Pragma is ignored.
3698 * When the Cache-Control header field is not present in a
3699 * request, caches MUST consider the no-cache request
3700 * pragma-directive as having the same effect as if
3701 * "Cache-Control: no-cache" were present.
3702 */
3703 if (!cc_found && pragma_found)
3704 txn->flags |= TX_CACHE_IGNORE;
3705}
3706
3707/*
3708 * Check if response is cacheable or not. Updates s->txn->flags.
3709 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003710void http_check_response_for_cacheability(struct stream *s, struct channel *res)
Christopher Faulet25a02f62018-10-24 12:00:25 +02003711{
3712 struct http_txn *txn = s->txn;
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003713 struct http_hdr_ctx ctx = { .blk = NULL };
Christopher Faulet25a02f62018-10-24 12:00:25 +02003714 struct htx *htx;
Remi Tricot-Le Bretoncc9bf2e2020-11-12 11:14:41 +01003715 int has_freshness_info = 0;
3716 int has_validator = 0;
Remi Tricot-Le Breton02f47242023-07-04 17:13:28 +02003717 int has_null_maxage = 0;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003718
3719 if (txn->status < 200) {
3720 /* do not try to cache interim responses! */
3721 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3722 return;
3723 }
3724
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003725 htx = htxbuf(&res->buf);
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003726 /* Check "pragma" header for HTTP/1.0 compatibility. */
3727 if (http_find_header(htx, ist("pragma"), &ctx, 1)) {
3728 if (isteqi(ctx.value, ist("no-cache"))) {
3729 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3730 return;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003731 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003732 }
Christopher Faulet25a02f62018-10-24 12:00:25 +02003733
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003734 /* Look for "cache-control" header and iterate over all the values
3735 * until we find one that specifies that caching is possible or not. */
3736 ctx.blk = NULL;
3737 while (http_find_header(htx, ist("cache-control"), &ctx, 0)) {
3738 if (isteqi(ctx.value, ist("public"))) {
3739 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003740 continue;
3741 }
Remi Tricot-Le Breton02f47242023-07-04 17:13:28 +02003742 /* This max-age might be overridden by a s-maxage directive, do
3743 * not unset the TX_CACHEABLE yet. */
3744 if (isteqi(ctx.value, ist("max-age=0"))) {
3745 has_null_maxage = 1;
3746 continue;
3747 }
3748
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003749 if (isteqi(ctx.value, ist("private")) ||
3750 isteqi(ctx.value, ist("no-cache")) ||
3751 isteqi(ctx.value, ist("no-store")) ||
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003752 isteqi(ctx.value, ist("s-maxage=0"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003753 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003754 continue;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003755 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003756 /* We might have a no-cache="set-cookie" form. */
3757 if (istmatchi(ctx.value, ist("no-cache=\"set-cookie"))) {
3758 txn->flags &= ~TX_CACHE_COOK;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003759 continue;
3760 }
Remi Tricot-Le Bretoncc9bf2e2020-11-12 11:14:41 +01003761
Remi Tricot-Le Breton02f47242023-07-04 17:13:28 +02003762 if (istmatchi(ctx.value, ist("s-maxage"))) {
Remi Tricot-Le Bretoncc9bf2e2020-11-12 11:14:41 +01003763 has_freshness_info = 1;
Remi Tricot-Le Breton02f47242023-07-04 17:13:28 +02003764 has_null_maxage = 0; /* The null max-age is overridden, ignore it */
Remi Tricot-Le Bretoncc9bf2e2020-11-12 11:14:41 +01003765 continue;
3766 }
Remi Tricot-Le Breton02f47242023-07-04 17:13:28 +02003767 if (istmatchi(ctx.value, ist("max-age"))) {
3768 has_freshness_info = 1;
3769 continue;
3770 }
3771 }
3772
3773 /* We had a 'max-age=0' directive but no extra s-maxage, do not cache
3774 * the response. */
3775 if (has_null_maxage) {
3776 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Remi Tricot-Le Bretoncc9bf2e2020-11-12 11:14:41 +01003777 }
3778
3779 /* If no freshness information could be found in Cache-Control values,
3780 * look for an Expires header. */
3781 if (!has_freshness_info) {
3782 ctx.blk = NULL;
3783 has_freshness_info = http_find_header(htx, ist("expires"), &ctx, 0);
Christopher Faulet25a02f62018-10-24 12:00:25 +02003784 }
Remi Tricot-Le Bretoncc9bf2e2020-11-12 11:14:41 +01003785
3786 /* If no freshness information could be found in Cache-Control or Expires
3787 * values, look for an explicit validator. */
3788 if (!has_freshness_info) {
3789 ctx.blk = NULL;
3790 has_validator = 1;
3791 if (!http_find_header(htx, ist("etag"), &ctx, 0)) {
3792 ctx.blk = NULL;
3793 if (!http_find_header(htx, ist("last-modified"), &ctx, 0))
3794 has_validator = 0;
3795 }
3796 }
3797
3798 /* We won't store an entry that has neither a cache validator nor an
3799 * explicit expiration time, as suggested in RFC 7234#3. */
3800 if (!has_freshness_info && !has_validator)
Remi Tricot-Le Breton879debe2023-02-21 11:47:17 +01003801 txn->flags &= ~TX_CACHEABLE;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003802}
3803
Christopher Faulet377c5a52018-10-24 21:21:30 +02003804/*
3805 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
3806 * for the current backend.
3807 *
3808 * It is assumed that the request is either a HEAD, GET, or POST and that the
3809 * uri_auth field is valid.
3810 *
3811 * Returns 1 if stats should be provided, otherwise 0.
3812 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003813static int http_stats_check_uri(struct stream *s, struct http_txn *txn, struct proxy *backend)
Christopher Faulet377c5a52018-10-24 21:21:30 +02003814{
3815 struct uri_auth *uri_auth = backend->uri_auth;
3816 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003817 struct htx_sl *sl;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003818 struct ist uri;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003819
3820 if (!uri_auth)
3821 return 0;
3822
3823 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
3824 return 0;
3825
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003826 htx = htxbuf(&s->req.buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02003827 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003828 uri = htx_sl_req_uri(sl);
Amaury Denoyellec453f952021-07-06 11:40:12 +02003829 if (*uri_auth->uri_prefix == '/') {
3830 struct http_uri_parser parser = http_uri_parser_init(uri);
3831 uri = http_parse_path(&parser);
3832 }
Christopher Faulet377c5a52018-10-24 21:21:30 +02003833
3834 /* check URI size */
3835 if (uri_auth->uri_len > uri.len)
3836 return 0;
3837
3838 if (memcmp(uri.ptr, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
3839 return 0;
3840
3841 return 1;
3842}
3843
3844/* This function prepares an applet to handle the stats. It can deal with the
3845 * "100-continue" expectation, check that admin rules are met for POST requests,
3846 * and program a response message if something was unexpected. It cannot fail
3847 * and always relies on the stats applet to complete the job. It does not touch
3848 * analysers nor counters, which are left to the caller. It does not touch
3849 * s->target which is supposed to already point to the stats applet. The caller
3850 * is expected to have already assigned an appctx to the stream.
3851 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003852static int http_handle_stats(struct stream *s, struct channel *req)
Christopher Faulet377c5a52018-10-24 21:21:30 +02003853{
3854 struct stats_admin_rule *stats_admin_rule;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003855 struct session *sess = s->sess;
3856 struct http_txn *txn = s->txn;
3857 struct http_msg *msg = &txn->req;
3858 struct uri_auth *uri_auth = s->be->uri_auth;
3859 const char *h, *lookup, *end;
Willy Tarreau8e7c6e62022-05-18 17:58:02 +02003860 struct appctx *appctx = __sc_appctx(s->scb);
Willy Tarreau91cefca2022-05-03 17:08:29 +02003861 struct show_stat_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx));
Christopher Faulet377c5a52018-10-24 21:21:30 +02003862 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003863 struct htx_sl *sl;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003864
Willy Tarreau41f88522022-05-03 18:39:27 +02003865 appctx->st1 = 0;
Willy Tarreau6ef16482022-05-06 18:07:53 +02003866 ctx->state = STAT_STATE_INIT;
Willy Tarreau91cefca2022-05-03 17:08:29 +02003867 ctx->st_code = STAT_STATUS_INIT;
3868 ctx->flags |= uri_auth->flags;
3869 ctx->flags |= STAT_FMT_HTML; /* assume HTML mode by default */
Christopher Faulet377c5a52018-10-24 21:21:30 +02003870 if ((msg->flags & HTTP_MSGF_VER_11) && (txn->meth != HTTP_METH_HEAD))
Willy Tarreau91cefca2022-05-03 17:08:29 +02003871 ctx->flags |= STAT_CHUNKED;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003872
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003873 htx = htxbuf(&req->buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02003874 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003875 lookup = HTX_SL_REQ_UPTR(sl) + uri_auth->uri_len;
3876 end = HTX_SL_REQ_UPTR(sl) + HTX_SL_REQ_ULEN(sl);
Christopher Faulet377c5a52018-10-24 21:21:30 +02003877
3878 for (h = lookup; h <= end - 3; h++) {
3879 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02003880 ctx->flags |= STAT_HIDE_DOWN;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003881 break;
3882 }
Amaury Denoyelle91e55ea2021-02-25 14:46:08 +01003883 }
3884
3885 for (h = lookup; h <= end - 9; h++) {
3886 if (memcmp(h, ";no-maint", 9) == 0) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02003887 ctx->flags |= STAT_HIDE_MAINT;
Willy Tarreau3e320362020-10-23 17:28:57 +02003888 break;
3889 }
Christopher Faulet377c5a52018-10-24 21:21:30 +02003890 }
3891
3892 if (uri_auth->refresh) {
3893 for (h = lookup; h <= end - 10; h++) {
3894 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02003895 ctx->flags |= STAT_NO_REFRESH;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003896 break;
3897 }
3898 }
3899 }
3900
3901 for (h = lookup; h <= end - 4; h++) {
3902 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02003903 ctx->flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
Christopher Faulet377c5a52018-10-24 21:21:30 +02003904 break;
3905 }
3906 }
3907
3908 for (h = lookup; h <= end - 6; h++) {
3909 if (memcmp(h, ";typed", 6) == 0) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02003910 ctx->flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
3911 ctx->flags |= STAT_FMT_TYPED;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003912 break;
3913 }
3914 }
3915
Christopher Faulet6338a082019-09-09 15:50:54 +02003916 for (h = lookup; h <= end - 5; h++) {
3917 if (memcmp(h, ";json", 5) == 0) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02003918 ctx->flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
3919 ctx->flags |= STAT_FMT_JSON;
Christopher Faulet6338a082019-09-09 15:50:54 +02003920 break;
3921 }
3922 }
3923
3924 for (h = lookup; h <= end - 12; h++) {
3925 if (memcmp(h, ";json-schema", 12) == 0) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02003926 ctx->flags &= ~STAT_FMT_MASK;
3927 ctx->flags |= STAT_JSON_SCHM;
Christopher Faulet6338a082019-09-09 15:50:54 +02003928 break;
3929 }
3930 }
3931
Christopher Faulet377c5a52018-10-24 21:21:30 +02003932 for (h = lookup; h <= end - 8; h++) {
3933 if (memcmp(h, ";st=", 4) == 0) {
3934 int i;
3935 h += 4;
Willy Tarreau91cefca2022-05-03 17:08:29 +02003936 ctx->st_code = STAT_STATUS_UNKN;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003937 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
3938 if (strncmp(stat_status_codes[i], h, 4) == 0) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02003939 ctx->st_code = i;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003940 break;
3941 }
3942 }
3943 break;
3944 }
3945 }
3946
Willy Tarreau91cefca2022-05-03 17:08:29 +02003947 ctx->scope_str = 0;
3948 ctx->scope_len = 0;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003949 for (h = lookup; h <= end - 8; h++) {
3950 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
3951 int itx = 0;
3952 const char *h2;
3953 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
3954 const char *err;
3955
3956 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
3957 h2 = h;
Willy Tarreau91cefca2022-05-03 17:08:29 +02003958 ctx->scope_str = h2 - HTX_SL_REQ_UPTR(sl);
Christopher Fauleted7a0662019-01-14 11:07:34 +01003959 while (h < end) {
Christopher Faulet377c5a52018-10-24 21:21:30 +02003960 if (*h == ';' || *h == '&' || *h == ' ')
3961 break;
3962 itx++;
3963 h++;
3964 }
3965
3966 if (itx > STAT_SCOPE_TXT_MAXLEN)
3967 itx = STAT_SCOPE_TXT_MAXLEN;
Willy Tarreau91cefca2022-05-03 17:08:29 +02003968 ctx->scope_len = itx;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003969
Willy Tarreau91cefca2022-05-03 17:08:29 +02003970 /* scope_txt = search query, ctx->scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Christopher Faulet377c5a52018-10-24 21:21:30 +02003971 memcpy(scope_txt, h2, itx);
3972 scope_txt[itx] = '\0';
3973 err = invalid_char(scope_txt);
3974 if (err) {
3975 /* bad char in search text => clear scope */
Willy Tarreau91cefca2022-05-03 17:08:29 +02003976 ctx->scope_str = 0;
3977 ctx->scope_len = 0;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003978 }
3979 break;
3980 }
3981 }
3982
3983 /* now check whether we have some admin rules for this request */
3984 list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) {
3985 int ret = 1;
3986
3987 if (stats_admin_rule->cond) {
3988 ret = acl_exec_cond(stats_admin_rule->cond, s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
3989 ret = acl_pass(ret);
3990 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
3991 ret = !ret;
3992 }
3993
3994 if (ret) {
3995 /* no rule, or the rule matches */
Willy Tarreau91cefca2022-05-03 17:08:29 +02003996 ctx->flags |= STAT_ADMIN;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003997 break;
3998 }
3999 }
4000
Christopher Faulet5d45e382019-02-27 15:15:23 +01004001 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
4002 appctx->st0 = STAT_HTTP_HEAD;
4003 else if (txn->meth == HTTP_METH_POST) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02004004 if (ctx->flags & STAT_ADMIN) {
Christopher Faulet377c5a52018-10-24 21:21:30 +02004005 appctx->st0 = STAT_HTTP_POST;
Christopher Fauletbd9e8422019-08-15 22:26:48 +02004006 if (msg->msg_state < HTTP_MSG_DATA)
4007 req->analysers |= AN_REQ_HTTP_BODY;
4008 }
Christopher Faulet377c5a52018-10-24 21:21:30 +02004009 else {
Christopher Faulet5d45e382019-02-27 15:15:23 +01004010 /* POST without admin level */
Willy Tarreau91cefca2022-05-03 17:08:29 +02004011 ctx->flags &= ~STAT_CHUNKED;
4012 ctx->st_code = STAT_STATUS_DENY;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004013 appctx->st0 = STAT_HTTP_LAST;
4014 }
4015 }
4016 else {
Christopher Faulet5d45e382019-02-27 15:15:23 +01004017 /* Unsupported method */
Willy Tarreau91cefca2022-05-03 17:08:29 +02004018 ctx->flags &= ~STAT_CHUNKED;
4019 ctx->st_code = STAT_STATUS_IVAL;
Christopher Faulet5d45e382019-02-27 15:15:23 +01004020 appctx->st0 = STAT_HTTP_LAST;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004021 }
4022
4023 s->task->nice = -32; /* small boost for HTTP statistics */
4024 return 1;
4025}
4026
Christopher Faulet021a8e42021-03-29 10:46:38 +02004027/* This function waits for the message payload at most <time> milliseconds (may
4028 * be set to TICK_ETERNITY). It stops to wait if at least <bytes> bytes of the
4029 * payload are received (0 means no limit). It returns HTTP_RULE_* depending on
4030 * the result:
4031 *
4032 * - HTTP_RULE_RES_CONT when conditions are met to stop waiting
4033 * - HTTP_RULE_RES_YIELD to wait for more data
Ilya Shipitsinb2be9a12021-04-24 13:25:42 +05004034 * - HTTP_RULE_RES_ABRT when a timeout occurred.
Christopher Faulet021a8e42021-03-29 10:46:38 +02004035 * - HTTP_RULE_RES_BADREQ if a parsing error is raised by lower level
Ilya Shipitsinb2be9a12021-04-24 13:25:42 +05004036 * - HTTP_RULE_RES_ERROR if an internal error occurred
Christopher Faulet021a8e42021-03-29 10:46:38 +02004037 *
Ilya Shipitsinb2be9a12021-04-24 13:25:42 +05004038 * If a timeout occurred, this function is responsible to emit the right response
Christopher Faulet021a8e42021-03-29 10:46:38 +02004039 * to the client, depending on the channel (408 on request side, 504 on response
4040 * side). All other errors must be handled by the caller.
4041 */
4042enum rule_result http_wait_for_msg_body(struct stream *s, struct channel *chn,
4043 unsigned int time, unsigned int bytes)
4044{
4045 struct session *sess = s->sess;
4046 struct http_txn *txn = s->txn;
4047 struct http_msg *msg = ((chn->flags & CF_ISRESP) ? &txn->rsp : &txn->req);
4048 struct htx *htx;
4049 enum rule_result ret = HTTP_RULE_RES_CONT;
4050
4051 htx = htxbuf(&chn->buf);
4052
4053 if (htx->flags & HTX_FL_PARSING_ERROR) {
4054 ret = HTTP_RULE_RES_BADREQ;
4055 goto end;
4056 }
4057 if (htx->flags & HTX_FL_PROCESSING_ERROR) {
4058 ret = HTTP_RULE_RES_ERROR;
4059 goto end;
4060 }
4061
4062 /* Do nothing for bodyless and CONNECT requests */
4063 if (txn->meth == HTTP_METH_CONNECT || (msg->flags & HTTP_MSGF_BODYLESS))
4064 goto end;
4065
Christopher Fauletffcffa82023-04-05 10:33:31 +02004066 if (!(chn->flags & CF_ISRESP)) {
Christopher Faulet021a8e42021-03-29 10:46:38 +02004067 if (http_handle_expect_hdr(s, htx, msg) == -1) {
4068 ret = HTTP_RULE_RES_ERROR;
4069 goto end;
4070 }
4071 }
4072
Christopher Faulet2954bcc2023-04-05 10:42:03 +02004073 /* Now we're are waiting for the payload. We just need to know if all
4074 * data have been received or if the buffer is full.
Christopher Faulet021a8e42021-03-29 10:46:38 +02004075 */
Christopher Faulet78335962021-09-23 14:46:32 +02004076 if ((htx->flags & HTX_FL_EOM) ||
4077 htx_get_tail_type(htx) > HTX_BLK_DATA ||
4078 channel_htx_full(chn, htx, global.tune.maxrewrite) ||
Willy Tarreau99615ed2022-05-25 07:29:36 +02004079 sc_waiting_room(chn_prod(chn)))
Christopher Faulet021a8e42021-03-29 10:46:38 +02004080 goto end;
4081
4082 if (bytes) {
4083 struct htx_blk *blk;
4084 unsigned int len = 0;
4085
4086 for (blk = htx_get_first_blk(htx); blk; blk = htx_get_next_blk(htx, blk)) {
4087 if (htx_get_blk_type(blk) != HTX_BLK_DATA)
4088 continue;
4089 len += htx_get_blksz(blk);
4090 if (len >= bytes)
4091 goto end;
4092 }
4093 }
4094
4095 if ((chn->flags & CF_READ_TIMEOUT) || tick_is_expired(chn->analyse_exp, now_ms)) {
4096 if (!(chn->flags & CF_ISRESP))
4097 goto abort_req;
4098 goto abort_res;
4099 }
4100
4101 /* we get here if we need to wait for more data */
Christopher Fauletca5309a2023-04-17 16:17:32 +02004102 if (!(chn_prod(chn)->flags & (SC_FL_EOS|SC_FL_ABRT_DONE))) {
Christopher Faulet021a8e42021-03-29 10:46:38 +02004103 if (!tick_isset(chn->analyse_exp))
4104 chn->analyse_exp = tick_add_ifset(now_ms, time);
4105 ret = HTTP_RULE_RES_YIELD;
4106 }
4107
4108 end:
4109 return ret;
4110
Christopher Fauletf0d80df2023-01-13 10:20:20 +01004111 abort:
4112 http_reply_and_close(s, txn->status, http_error_message(s));
4113 ret = HTTP_RULE_RES_ABRT;
4114 goto end;
4115
Christopher Faulet021a8e42021-03-29 10:46:38 +02004116 abort_req:
4117 txn->status = 408;
4118 if (!(s->flags & SF_ERR_MASK))
4119 s->flags |= SF_ERR_CLITO;
Willy Tarreau4781b152021-04-06 13:53:36 +02004120 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req);
Christopher Faulet021a8e42021-03-29 10:46:38 +02004121 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02004122 _HA_ATOMIC_INC(&sess->listener->counters->failed_req);
Christopher Fauletf0d80df2023-01-13 10:20:20 +01004123 goto abort;
Christopher Faulet021a8e42021-03-29 10:46:38 +02004124
4125 abort_res:
4126 txn->status = 504;
4127 if (!(s->flags & SF_ERR_MASK))
4128 s->flags |= SF_ERR_SRVTO;
Christopher Faulet021a8e42021-03-29 10:46:38 +02004129 stream_inc_http_fail_ctr(s);
Christopher Fauletf0d80df2023-01-13 10:20:20 +01004130 goto abort;
Christopher Faulet021a8e42021-03-29 10:46:38 +02004131}
4132
Willy Tarreaub49672d2022-05-27 10:13:37 +02004133void http_perform_server_redirect(struct stream *s, struct stconn *sc)
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004134{
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004135 struct channel *req = &s->req;
4136 struct channel *res = &s->res;
4137 struct server *srv;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004138 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004139 struct htx_sl *sl;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004140 struct ist path, location;
4141 unsigned int flags;
Amaury Denoyellec453f952021-07-06 11:40:12 +02004142 struct http_uri_parser parser;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004143
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004144 /*
4145 * Create the location
4146 */
4147 chunk_reset(&trash);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004148
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004149 /* 1: add the server's prefix */
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004150 /* special prefix "/" means don't change URL */
4151 srv = __objt_server(s->target);
4152 if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') {
4153 if (!chunk_memcat(&trash, srv->rdr_pfx, srv->rdr_len))
4154 return;
4155 }
4156
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004157 /* 2: add the request Path */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004158 htx = htxbuf(&req->buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02004159 sl = http_get_stline(htx);
Amaury Denoyellec453f952021-07-06 11:40:12 +02004160 parser = http_uri_parser_init(htx_sl_req_uri(sl));
4161 path = http_parse_path(&parser);
Tim Duesterhused526372020-03-05 17:56:33 +01004162 if (!isttest(path))
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004163 return;
4164
4165 if (!chunk_memcat(&trash, path.ptr, path.len))
4166 return;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004167 location = ist2(trash.area, trash.data);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004168
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004169 /*
Ilya Shipitsin4a689da2022-10-29 09:34:32 +05004170 * Create the 302 response
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004171 */
4172 htx = htx_from_buf(&res->buf);
4173 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
4174 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
4175 ist("HTTP/1.1"), ist("302"), ist("Found"));
4176 if (!sl)
4177 goto fail;
4178 sl->info.res.status = 302;
4179 s->txn->status = 302;
4180
4181 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) ||
4182 !htx_add_header(htx, ist("Connection"), ist("close")) ||
4183 !htx_add_header(htx, ist("Content-length"), ist("0")) ||
4184 !htx_add_header(htx, ist("Location"), location))
4185 goto fail;
4186
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004187 if (!htx_add_endof(htx, HTX_BLK_EOH))
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004188 goto fail;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004189
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004190 htx->flags |= HTX_FL_EOM;
Christopher Fauletc20afb82020-01-24 19:16:26 +01004191 htx_to_buf(htx, &res->buf);
Christopher Fauleta72a7e42020-01-28 09:28:11 +01004192 if (!http_forward_proxy_resp(s, 1))
4193 goto fail;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004194
4195 /* return without error. */
Christopher Fauletcfc11c02023-04-13 16:10:23 +02004196 sc_abort(sc);
Christopher Fauletb2b1c3a2023-04-13 16:23:48 +02004197 sc_shutdown(sc);
Christopher Faulet50264b42022-03-30 19:39:30 +02004198 s->conn_err_type = STRM_ET_NONE;
Willy Tarreaub49672d2022-05-27 10:13:37 +02004199 sc->state = SC_ST_CLO;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004200
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004201 if (!(s->flags & SF_ERR_MASK))
4202 s->flags |= SF_ERR_LOCAL;
4203 if (!(s->flags & SF_FINST_MASK))
4204 s->flags |= SF_FINST_C;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004205
4206 /* FIXME: we should increase a counter of redirects per server and per backend. */
4207 srv_inc_sess_ctr(srv);
4208 srv_set_sess_last(srv);
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004209 return;
4210
4211 fail:
4212 /* If an error occurred, remove the incomplete HTTP response from the
4213 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004214 channel_htx_truncate(res, htx);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004215}
4216
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05004217/* This function terminates the request because it was completely analyzed or
Christopher Fauletf2824e62018-10-01 12:12:37 +02004218 * because an error was triggered during the body forwarding.
4219 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004220static void http_end_request(struct stream *s)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004221{
4222 struct channel *chn = &s->req;
4223 struct http_txn *txn = s->txn;
4224
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004225 DBG_TRACE_ENTER(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004226
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004227 if (unlikely(txn->req.msg_state < HTTP_MSG_DONE)) {
4228 DBG_TRACE_DEVEL("waiting end of the request", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004229 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004230 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004231
4232 if (txn->req.msg_state == HTTP_MSG_DONE) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004233 /* No need to read anymore, the request was completely parsed.
4234 * We can shut the read side unless we want to abort_on_close,
4235 * or we have a POST request. The issue with POST requests is
4236 * that some browsers still send a CRLF after the request, and
4237 * this CRLF must be read so that it does not remain in the kernel
4238 * buffers, otherwise a close could cause an RST on some systems
4239 * (eg: Linux).
4240 */
Christopher Faulet769d0e92019-03-22 14:23:18 +01004241 if (!(s->be->options & PR_O_ABRT_CLOSE) && txn->meth != HTTP_METH_POST)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004242 channel_dont_read(chn);
4243
4244 /* if the server closes the connection, we want to immediately react
4245 * and close the socket to save packets and syscalls.
4246 */
Willy Tarreaucb041662022-05-17 19:44:42 +02004247 s->scb->flags |= SC_FL_NOHALF;
Christopher Fauletf2824e62018-10-01 12:12:37 +02004248
4249 /* In any case we've finished parsing the request so we must
4250 * disable Nagle when sending data because 1) we're not going
4251 * to shut this side, and 2) the server is waiting for us to
4252 * send pending data.
4253 */
Christopher Faulet68ef2182023-03-17 15:38:18 +01004254 s->scb->flags |= SC_FL_SND_NEVERWAIT;
Christopher Fauletf2824e62018-10-01 12:12:37 +02004255
Christopher Fauletc2fba3f2023-03-01 16:03:17 +01004256 if (txn->rsp.msg_state < HTTP_MSG_BODY ||
4257 (txn->rsp.msg_state < HTTP_MSG_DONE && s->scb->state != SC_ST_CLO)) {
Christopher Fauletaf124362023-02-14 10:48:02 +01004258 /* The server has not finished to respond and the
4259 * backend SC is not closed, so we don't want to move in
4260 * order not to upset it.
Christopher Fauletd01ce402019-01-02 17:44:13 +01004261 */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004262 DBG_TRACE_DEVEL("waiting end of the response", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletd01ce402019-01-02 17:44:13 +01004263 return;
4264 }
4265
Christopher Fauletf2824e62018-10-01 12:12:37 +02004266 /* When we get here, it means that both the request and the
4267 * response have finished receiving. Depending on the connection
4268 * mode, we'll have to wait for the last bytes to leave in either
4269 * direction, and sometimes for a close to be effective.
4270 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02004271 if (txn->flags & TX_CON_WANT_TUN) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004272 /* Tunnel mode will not have any analyser so it needs to
4273 * poll for reads.
4274 */
Christopher Faulet97047972023-04-17 08:52:10 +02004275 channel_auto_read(&s->req);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004276 txn->req.msg_state = HTTP_MSG_TUNNEL;
Christopher Faulet27c17d12023-04-18 11:01:51 +02004277 if (txn->rsp.msg_state != HTTP_MSG_TUNNEL)
4278 s->res.flags |= CF_WAKE_ONCE;
Christopher Fauletf2824e62018-10-01 12:12:37 +02004279 }
4280 else {
4281 /* we're not expecting any new data to come for this
4282 * transaction, so we can close it.
Christopher Faulet9768c262018-10-22 09:34:31 +02004283 *
4284 * However, there is an exception if the response
4285 * length is undefined. In this case, we need to wait
4286 * the close from the server. The response will be
4287 * switched in TUNNEL mode until the end.
Christopher Fauletf2824e62018-10-01 12:12:37 +02004288 */
4289 if (!(txn->rsp.flags & HTTP_MSGF_XFER_LEN) &&
4290 txn->rsp.msg_state != HTTP_MSG_CLOSED)
Christopher Faulet9768c262018-10-22 09:34:31 +02004291 goto check_channel_flags;
Christopher Fauletf2824e62018-10-01 12:12:37 +02004292
Christopher Faulet64350bb2023-04-13 16:37:37 +02004293 if (!(s->scb->flags & (SC_FL_SHUT_DONE|SC_FL_SHUT_WANTED))) {
Christopher Faulet12762f02023-04-13 15:40:10 +02004294 sc_schedule_abort(s->scf);
Christopher Fauletdf7cd712023-04-13 15:56:26 +02004295 sc_schedule_shutdown(s->scb);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004296 }
4297 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004298 goto check_channel_flags;
4299 }
4300
4301 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
4302 http_msg_closing:
4303 /* nothing else to forward, just waiting for the output buffer
Christopher Fauletdf7cd712023-04-13 15:56:26 +02004304 * to be empty and for the shut_wanted to take effect.
Christopher Fauletf2824e62018-10-01 12:12:37 +02004305 */
4306 if (channel_is_empty(chn)) {
4307 txn->req.msg_state = HTTP_MSG_CLOSED;
4308 goto http_msg_closed;
4309 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004310 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004311 return;
4312 }
4313
4314 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
4315 http_msg_closed:
Christopher Fauletf2824e62018-10-01 12:12:37 +02004316 /* if we don't know whether the server will close, we need to hard close */
4317 if (txn->rsp.flags & HTTP_MSGF_XFER_LEN)
Willy Tarreaucb041662022-05-17 19:44:42 +02004318 s->scb->flags |= SC_FL_NOLINGER; /* we want to close ASAP */
Christopher Fauletf2824e62018-10-01 12:12:37 +02004319 /* see above in MSG_DONE why we only do this in these states */
Christopher Faulet769d0e92019-03-22 14:23:18 +01004320 if (!(s->be->options & PR_O_ABRT_CLOSE))
Christopher Fauletf2824e62018-10-01 12:12:37 +02004321 channel_dont_read(chn);
4322 goto end;
4323 }
4324
4325 check_channel_flags:
4326 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
Christopher Faulet64350bb2023-04-13 16:37:37 +02004327 if (s->scb->flags & (SC_FL_SHUT_DONE|SC_FL_SHUT_WANTED)) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004328 /* if we've just closed an output, let's switch */
4329 txn->req.msg_state = HTTP_MSG_CLOSING;
4330 goto http_msg_closing;
4331 }
4332
4333 end:
4334 chn->analysers &= AN_REQ_FLT_END;
Christopher Faulet198ef8b2020-12-15 13:32:55 +01004335 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
Christopher Faulet68ef2182023-03-17 15:38:18 +01004336 s->scb->flags |= SC_FL_SND_NEVERWAIT;
Christopher Faulet198ef8b2020-12-15 13:32:55 +01004337 if (HAS_REQ_DATA_FILTERS(s))
Christopher Fauletf2824e62018-10-01 12:12:37 +02004338 chn->analysers |= AN_REQ_FLT_XFER_DATA;
Christopher Faulet27c17d12023-04-18 11:01:51 +02004339 else
4340 c_adv(chn, htxbuf(&chn->buf)->data - co_data(chn));
Christopher Faulet198ef8b2020-12-15 13:32:55 +01004341 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004342 channel_auto_close(chn);
4343 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004344 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004345}
4346
4347
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05004348/* This function terminates the response because it was completely analyzed or
Christopher Fauletf2824e62018-10-01 12:12:37 +02004349 * because an error was triggered during the body forwarding.
4350 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004351static void http_end_response(struct stream *s)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004352{
4353 struct channel *chn = &s->res;
4354 struct http_txn *txn = s->txn;
4355
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004356 DBG_TRACE_ENTER(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004357
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004358 if (unlikely(txn->rsp.msg_state < HTTP_MSG_DONE)) {
4359 DBG_TRACE_DEVEL("waiting end of the response", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004360 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004361 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004362
4363 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
4364 /* In theory, we don't need to read anymore, but we must
4365 * still monitor the server connection for a possible close
4366 * while the request is being uploaded, so we don't disable
4367 * reading.
4368 */
4369 /* channel_dont_read(chn); */
4370
Christopher Fauletaf124362023-02-14 10:48:02 +01004371 if (txn->req.msg_state < HTTP_MSG_DONE && s->scf->state != SC_ST_CLO) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004372 /* The client seems to still be sending data, probably
4373 * because we got an error response during an upload.
4374 * We have the choice of either breaking the connection
4375 * or letting it pass through. Let's do the later.
4376 */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004377 DBG_TRACE_DEVEL("waiting end of the request", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004378 return;
4379 }
4380
4381 /* When we get here, it means that both the request and the
4382 * response have finished receiving. Depending on the connection
4383 * mode, we'll have to wait for the last bytes to leave in either
4384 * direction, and sometimes for a close to be effective.
4385 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02004386 if (txn->flags & TX_CON_WANT_TUN) {
Christopher Faulet97047972023-04-17 08:52:10 +02004387 channel_auto_read(&s->res);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004388 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Christopher Faulet27c17d12023-04-18 11:01:51 +02004389 if (txn->req.msg_state != HTTP_MSG_TUNNEL)
4390 s->req.flags |= CF_WAKE_ONCE;
Christopher Fauletf2824e62018-10-01 12:12:37 +02004391 }
4392 else {
4393 /* we're not expecting any new data to come for this
4394 * transaction, so we can close it.
4395 */
Christopher Faulet64350bb2023-04-13 16:37:37 +02004396 if (!(s->scf->flags & (SC_FL_SHUT_DONE|SC_FL_SHUT_WANTED))) {
Christopher Faulet12762f02023-04-13 15:40:10 +02004397 sc_schedule_abort(s->scb);
Christopher Fauletdf7cd712023-04-13 15:56:26 +02004398 sc_schedule_shutdown(s->scf);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004399 }
4400 }
4401 goto check_channel_flags;
4402 }
4403
4404 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
4405 http_msg_closing:
4406 /* nothing else to forward, just waiting for the output buffer
Christopher Fauletdf7cd712023-04-13 15:56:26 +02004407 * to be empty and for the shut_wanted to take effect.
Christopher Fauletf2824e62018-10-01 12:12:37 +02004408 */
4409 if (channel_is_empty(chn)) {
4410 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4411 goto http_msg_closed;
4412 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004413 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004414 return;
4415 }
4416
4417 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
4418 http_msg_closed:
4419 /* drop any pending data */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004420 channel_htx_truncate(&s->req, htxbuf(&s->req.buf));
Christopher Faulet9768c262018-10-22 09:34:31 +02004421 channel_abort(&s->req);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004422 goto end;
4423 }
4424
4425 check_channel_flags:
4426 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
Christopher Faulet64350bb2023-04-13 16:37:37 +02004427 if (s->scf->flags & (SC_FL_SHUT_DONE|SC_FL_SHUT_WANTED)) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004428 /* if we've just closed an output, let's switch */
4429 txn->rsp.msg_state = HTTP_MSG_CLOSING;
4430 goto http_msg_closing;
4431 }
4432
4433 end:
4434 chn->analysers &= AN_RES_FLT_END;
Christopher Faulet198ef8b2020-12-15 13:32:55 +01004435 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
Christopher Faulet68ef2182023-03-17 15:38:18 +01004436 s->scf->flags |= SC_FL_SND_NEVERWAIT;
Christopher Faulet198ef8b2020-12-15 13:32:55 +01004437 if (HAS_RSP_DATA_FILTERS(s))
4438 chn->analysers |= AN_RES_FLT_XFER_DATA;
Christopher Faulet27c17d12023-04-18 11:01:51 +02004439 else
4440 c_adv(chn, htxbuf(&chn->buf)->data - co_data(chn));
Christopher Faulet198ef8b2020-12-15 13:32:55 +01004441 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004442 channel_auto_close(chn);
4443 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004444 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004445}
4446
Christopher Fauletef70e252020-01-28 09:26:19 +01004447/* Forward a response generated by HAProxy (error/redirect/return). This
4448 * function forwards all pending incoming data. If <final> is set to 0, nothing
4449 * more is performed. It is used for 1xx informational messages. Otherwise, the
Christopher Faulet507479b2020-05-15 12:29:46 +02004450 * transaction is terminated and the request is emptied. On success 1 is
Christopher Faulet40e6b552020-06-25 16:04:50 +02004451 * returned. If an error occurred, 0 is returned. If it fails, this function
4452 * only exits. It is the caller responsibility to do the cleanup.
Christopher Fauletef70e252020-01-28 09:26:19 +01004453 */
4454int http_forward_proxy_resp(struct stream *s, int final)
4455{
4456 struct channel *req = &s->req;
4457 struct channel *res = &s->res;
4458 struct htx *htx = htxbuf(&res->buf);
4459 size_t data;
4460
4461 if (final) {
4462 htx->flags |= HTX_FL_PROXY_RESP;
Christopher Faulet507479b2020-05-15 12:29:46 +02004463
Christopher Fauletaab1b672020-11-18 16:44:02 +01004464 if (!htx_is_empty(htx) && !http_eval_after_res_rules(s))
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01004465 return 0;
Christopher Fauletef70e252020-01-28 09:26:19 +01004466
Christopher Fauletd6c48362020-10-19 18:01:38 +02004467 if (s->txn->meth == HTTP_METH_HEAD)
4468 htx_skip_msg_payload(htx);
4469
Christopher Fauletef70e252020-01-28 09:26:19 +01004470 channel_auto_read(req);
4471 channel_abort(req);
Christopher Fauletef70e252020-01-28 09:26:19 +01004472 channel_htx_erase(req, htxbuf(&req->buf));
4473
Christopher Fauletef70e252020-01-28 09:26:19 +01004474 channel_auto_read(res);
4475 channel_auto_close(res);
Christopher Faulet12762f02023-04-13 15:40:10 +02004476 sc_schedule_abort(s->scb);
Christopher Faulet904763f2023-03-22 14:53:11 +01004477 s->scb->flags |= SC_FL_EOI; /* The response is terminated, add EOI */
Christopher Faulet42432f32020-11-20 17:43:16 +01004478 htxbuf(&res->buf)->flags |= HTX_FL_EOM; /* no more data are expected */
Christopher Fauletef70e252020-01-28 09:26:19 +01004479 }
Christopher Fauletcf6898c2020-06-25 15:55:11 +02004480 else {
Christopher Faulet904763f2023-03-22 14:53:11 +01004481 /* Send ASAP informational messages. Rely on SC_FL_EOI for final
Christopher Fauletcf6898c2020-06-25 15:55:11 +02004482 * response.
4483 */
Christopher Faulet5c281d52023-03-16 15:53:28 +01004484 s->scf->flags |= SC_FL_SND_ASAP;
Christopher Fauletcf6898c2020-06-25 15:55:11 +02004485 }
Christopher Fauletef70e252020-01-28 09:26:19 +01004486
4487 data = htx->data - co_data(res);
4488 c_adv(res, data);
4489 htx->first = -1;
4490 res->total += data;
4491 return 1;
4492}
4493
Willy Tarreaub49672d2022-05-27 10:13:37 +02004494void http_server_error(struct stream *s, struct stconn *sc, int err,
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004495 int finst, struct http_reply *msg)
Christopher Faulet0f226952018-10-22 09:29:56 +02004496{
Christopher Faulet72c7d8d2020-01-27 15:32:25 +01004497 http_reply_and_close(s, s->txn->status, msg);
Christopher Faulet0f226952018-10-22 09:29:56 +02004498 if (!(s->flags & SF_ERR_MASK))
4499 s->flags |= err;
4500 if (!(s->flags & SF_FINST_MASK))
4501 s->flags |= finst;
4502}
4503
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004504void http_reply_and_close(struct stream *s, short status, struct http_reply *msg)
Christopher Faulet0f226952018-10-22 09:29:56 +02004505{
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004506 if (!msg) {
4507 channel_htx_truncate(&s->res, htxbuf(&s->res.buf));
4508 goto end;
4509 }
4510
4511 if (http_reply_message(s, msg) == -1) {
4512 /* On error, return a 500 error message, but don't rewrite it if
Christopher Faulet40e6b552020-06-25 16:04:50 +02004513 * it is already an internal error. If it was already a "const"
4514 * 500 error, just fail.
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004515 */
Christopher Faulet40e6b552020-06-25 16:04:50 +02004516 if (s->txn->status == 500) {
4517 if (s->txn->flags & TX_CONST_REPLY)
4518 goto end;
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004519 s->txn->flags |= TX_CONST_REPLY;
Christopher Faulet40e6b552020-06-25 16:04:50 +02004520 }
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004521 s->txn->status = 500;
4522 s->txn->http_reply = NULL;
4523 return http_reply_and_close(s, s->txn->status, http_error_message(s));
4524 }
4525
4526end:
Christopher Faulet2d565002021-09-10 09:17:50 +02004527 /* At this staged, HTTP analysis is finished */
4528 s->req.analysers &= AN_REQ_FLT_END;
4529 s->req.analyse_exp = TICK_ETERNITY;
4530
4531 s->res.analysers &= AN_RES_FLT_END;
4532 s->res.analyse_exp = TICK_ETERNITY;
4533
Christopher Faulet0f226952018-10-22 09:29:56 +02004534 channel_auto_read(&s->req);
4535 channel_abort(&s->req);
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004536 channel_htx_erase(&s->req, htxbuf(&s->req.buf));
Christopher Faulet72c7d8d2020-01-27 15:32:25 +01004537 channel_auto_read(&s->res);
4538 channel_auto_close(&s->res);
Christopher Faulet12762f02023-04-13 15:40:10 +02004539 sc_schedule_abort(s->scb);
Christopher Faulet0f226952018-10-22 09:29:56 +02004540}
4541
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004542struct http_reply *http_error_message(struct stream *s)
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004543{
4544 const int msgnum = http_get_status_idx(s->txn->status);
4545
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004546 if (s->txn->http_reply)
4547 return s->txn->http_reply;
4548 else if (s->be->replies[msgnum])
4549 return s->be->replies[msgnum];
4550 else if (strm_fe(s)->replies[msgnum])
4551 return strm_fe(s)->replies[msgnum];
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004552 else
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004553 return &http_err_replies[msgnum];
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004554}
4555
Christopher Faulet40e6b552020-06-25 16:04:50 +02004556/* Produces an HTX message from an http reply. Depending on the http reply type,
4557 * a, errorfile, an raw file or a log-format string is used. On success, it
4558 * returns 0. If an error occurs -1 is returned. If it fails, this function only
4559 * exits. It is the caller responsibility to do the cleanup.
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004560 */
Christopher Fauletae43b6c2020-05-27 15:24:22 +02004561int http_reply_to_htx(struct stream *s, struct htx *htx, struct http_reply *reply)
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004562{
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004563 struct buffer *errmsg;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004564 struct htx_sl *sl;
4565 struct buffer *body = NULL;
4566 const char *status, *reason, *clen, *ctype;
4567 unsigned int slflags;
4568 int ret = 0;
4569
Christopher Faulete29a97e2020-05-14 14:49:25 +02004570 /*
4571 * - HTTP_REPLY_ERRFILES unexpected here. handled as no payload if so
4572 *
4573 * - HTTP_REPLY_INDIRECT: switch on another reply if defined or handled
4574 * as no payload if NULL. the TXN status code is set with the status
4575 * of the original reply.
4576 */
4577
4578 if (reply->type == HTTP_REPLY_INDIRECT) {
4579 if (reply->body.reply)
4580 reply = reply->body.reply;
4581 }
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004582 if (reply->type == HTTP_REPLY_ERRMSG && !reply->body.errmsg) {
4583 /* get default error message */
4584 if (reply == s->txn->http_reply)
4585 s->txn->http_reply = NULL;
4586 reply = http_error_message(s);
4587 if (reply->type == HTTP_REPLY_INDIRECT) {
4588 if (reply->body.reply)
4589 reply = reply->body.reply;
4590 }
4591 }
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004592
4593 if (reply->type == HTTP_REPLY_ERRMSG) {
4594 /* implicit or explicit error message*/
4595 errmsg = reply->body.errmsg;
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004596 if (errmsg && !b_is_null(errmsg)) {
Christopher Faulet20567362020-05-15 14:52:49 +02004597 if (!htx_copy_msg(htx, errmsg))
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004598 goto fail;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004599 }
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004600 }
4601 else {
4602 /* no payload, file or log-format string */
4603 if (reply->type == HTTP_REPLY_RAW) {
4604 /* file */
4605 body = &reply->body.obj;
4606 }
4607 else if (reply->type == HTTP_REPLY_LOGFMT) {
4608 /* log-format string */
4609 body = alloc_trash_chunk();
4610 if (!body)
4611 goto fail_alloc;
4612 body->data = build_logline(s, body->area, body->size, &reply->body.fmt);
4613 }
4614 /* else no payload */
4615
4616 status = ultoa(reply->status);
4617 reason = http_get_reason(reply->status);
4618 slflags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_CLEN);
4619 if (!body || !b_data(body))
4620 slflags |= HTX_SL_F_BODYLESS;
4621 sl = htx_add_stline(htx, HTX_BLK_RES_SL, slflags, ist("HTTP/1.1"), ist(status), ist(reason));
4622 if (!sl)
4623 goto fail;
4624 sl->info.res.status = reply->status;
4625
4626 clen = (body ? ultoa(b_data(body)) : "0");
4627 ctype = reply->ctype;
4628
4629 if (!LIST_ISEMPTY(&reply->hdrs)) {
4630 struct http_reply_hdr *hdr;
4631 struct buffer *value = alloc_trash_chunk();
4632
4633 if (!value)
4634 goto fail;
4635
4636 list_for_each_entry(hdr, &reply->hdrs, list) {
4637 chunk_reset(value);
4638 value->data = build_logline(s, value->area, value->size, &hdr->value);
4639 if (b_data(value) && !htx_add_header(htx, hdr->name, ist2(b_head(value), b_data(value)))) {
4640 free_trash_chunk(value);
4641 goto fail;
4642 }
4643 chunk_reset(value);
4644 }
4645 free_trash_chunk(value);
4646 }
4647
4648 if (!htx_add_header(htx, ist("content-length"), ist(clen)) ||
4649 (body && b_data(body) && ctype && !htx_add_header(htx, ist("content-type"), ist(ctype))) ||
4650 !htx_add_endof(htx, HTX_BLK_EOH) ||
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004651 (body && b_data(body) && !htx_add_data_atonce(htx, ist2(b_head(body), b_data(body)))))
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004652 goto fail;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004653
4654 htx->flags |= HTX_FL_EOM;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004655 }
4656
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004657 leave:
4658 if (reply->type == HTTP_REPLY_LOGFMT)
4659 free_trash_chunk(body);
4660 return ret;
4661
4662 fail_alloc:
4663 if (!(s->flags & SF_ERR_MASK))
4664 s->flags |= SF_ERR_RESOURCE;
Christopher Faulet97e466c2020-05-15 15:12:47 +02004665 /* fall through */
4666 fail:
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004667 ret = -1;
4668 goto leave;
Christopher Faulet97e466c2020-05-15 15:12:47 +02004669}
4670
4671/* Send an http reply to the client. On success, it returns 0. If an error
Christopher Faulet40e6b552020-06-25 16:04:50 +02004672 * occurs -1 is returned and the response channel is truncated, removing this
4673 * way the faulty reply. This function may fail when the reply is formatted
4674 * (http_reply_to_htx) or when the reply is forwarded
4675 * (http_forward_proxy_resp). On the last case, it is because a
4676 * http-after-response rule fails.
Christopher Faulet97e466c2020-05-15 15:12:47 +02004677 */
4678int http_reply_message(struct stream *s, struct http_reply *reply)
4679{
4680 struct channel *res = &s->res;
4681 struct htx *htx = htx_from_buf(&res->buf);
4682
4683 if (s->txn->status == -1)
4684 s->txn->status = reply->status;
4685 channel_htx_truncate(res, htx);
4686
4687 if (http_reply_to_htx(s, htx, reply) == -1)
4688 goto fail;
4689
4690 htx_to_buf(htx, &s->res.buf);
4691 if (!http_forward_proxy_resp(s, 1))
4692 goto fail;
4693 return 0;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004694
4695 fail:
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004696 channel_htx_truncate(res, htx);
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004697 if (!(s->flags & SF_ERR_MASK))
4698 s->flags |= SF_ERR_PRXCOND;
Christopher Faulet97e466c2020-05-15 15:12:47 +02004699 return -1;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004700}
4701
Christopher Faulet50264b42022-03-30 19:39:30 +02004702/* Return the error message corresponding to s->conn_err_type. It is assumed
Christopher Faulet304cc402019-07-15 15:46:28 +02004703 * that the server side is closed. Note that err_type is actually a
4704 * bitmask, where almost only aborts may be cumulated with other
4705 * values. We consider that aborted operations are more important
4706 * than timeouts or errors due to the fact that nobody else in the
4707 * logs might explain incomplete retries. All others should avoid
4708 * being cumulated. It should normally not be possible to have multiple
4709 * aborts at once, but just in case, the first one in sequence is reported.
4710 * Note that connection errors appearing on the second request of a keep-alive
4711 * connection are not reported since this allows the client to retry.
4712 */
Willy Tarreaub49672d2022-05-27 10:13:37 +02004713void http_return_srv_error(struct stream *s, struct stconn *sc)
Christopher Faulet304cc402019-07-15 15:46:28 +02004714{
Christopher Faulet50264b42022-03-30 19:39:30 +02004715 int err_type = s->conn_err_type;
Christopher Faulet304cc402019-07-15 15:46:28 +02004716
4717 /* set s->txn->status for http_error_message(s) */
Christopher Faulet50264b42022-03-30 19:39:30 +02004718 if (err_type & STRM_ET_QUEUE_ABRT) {
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004719 s->txn->status = -1;
Willy Tarreaub49672d2022-05-27 10:13:37 +02004720 http_server_error(s, sc, SF_ERR_CLICL, SF_FINST_Q, NULL);
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004721 }
Christopher Faulet50264b42022-03-30 19:39:30 +02004722 else if (err_type & STRM_ET_CONN_ABRT) {
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004723 s->txn->status = -1;
Willy Tarreaub49672d2022-05-27 10:13:37 +02004724 http_server_error(s, sc, SF_ERR_CLICL, SF_FINST_C, NULL);
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004725 }
Christopher Faulet50264b42022-03-30 19:39:30 +02004726 else if (err_type & STRM_ET_QUEUE_TO) {
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004727 s->txn->status = 503;
Willy Tarreaub49672d2022-05-27 10:13:37 +02004728 http_server_error(s, sc, SF_ERR_SRVTO, SF_FINST_Q,
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004729 http_error_message(s));
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004730 }
Christopher Faulet50264b42022-03-30 19:39:30 +02004731 else if (err_type & STRM_ET_QUEUE_ERR) {
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004732 s->txn->status = 503;
Willy Tarreaub49672d2022-05-27 10:13:37 +02004733 http_server_error(s, sc, SF_ERR_SRVCL, SF_FINST_Q,
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004734 http_error_message(s));
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004735 }
Christopher Faulet50264b42022-03-30 19:39:30 +02004736 else if (err_type & STRM_ET_CONN_TO) {
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004737 s->txn->status = 503;
Willy Tarreaub49672d2022-05-27 10:13:37 +02004738 http_server_error(s, sc, SF_ERR_SRVTO, SF_FINST_C,
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004739 (s->txn->flags & TX_NOT_FIRST) ? NULL :
4740 http_error_message(s));
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004741 }
Christopher Faulet50264b42022-03-30 19:39:30 +02004742 else if (err_type & STRM_ET_CONN_ERR) {
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_SRVCL, SF_FINST_C,
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004745 (s->flags & SF_SRV_REUSED) ? NULL :
4746 http_error_message(s));
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004747 }
Christopher Faulet50264b42022-03-30 19:39:30 +02004748 else if (err_type & STRM_ET_CONN_RES) {
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004749 s->txn->status = 503;
Willy Tarreaub49672d2022-05-27 10:13:37 +02004750 http_server_error(s, sc, SF_ERR_RESOURCE, SF_FINST_C,
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004751 (s->txn->flags & TX_NOT_FIRST) ? NULL :
4752 http_error_message(s));
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004753 }
Christopher Faulet50264b42022-03-30 19:39:30 +02004754 else { /* STRM_ET_CONN_OTHER and others */
Christopher Faulet304cc402019-07-15 15:46:28 +02004755 s->txn->status = 500;
Willy Tarreaub49672d2022-05-27 10:13:37 +02004756 http_server_error(s, sc, SF_ERR_INTERNAL, SF_FINST_C,
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004757 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004758 }
4759}
4760
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004761
Christopher Faulet4a28a532019-03-01 11:19:40 +01004762/* Handle Expect: 100-continue for HTTP/1.1 messages if necessary. It returns 0
4763 * on success and -1 on error.
4764 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004765static int http_handle_expect_hdr(struct stream *s, struct htx *htx, struct http_msg *msg)
Christopher Faulet4a28a532019-03-01 11:19:40 +01004766{
4767 /* If we have HTTP/1.1 message with a body and Expect: 100-continue,
4768 * then we must send an HTTP/1.1 100 Continue intermediate response.
4769 */
Christopher Fauletffcffa82023-04-05 10:33:31 +02004770 if (!(msg->flags & HTTP_MSGF_EXPECT_CHECKED) &&
4771 (msg->flags & HTTP_MSGF_VER_11) &&
Christopher Faulet4a28a532019-03-01 11:19:40 +01004772 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
4773 struct ist hdr = { .ptr = "Expect", .len = 6 };
4774 struct http_hdr_ctx ctx;
4775
4776 ctx.blk = NULL;
4777 /* Expect is allowed in 1.1, look for it */
4778 if (http_find_header(htx, hdr, &ctx, 0) &&
4779 unlikely(isteqi(ctx.value, ist2("100-continue", 12)))) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004780 if (http_reply_100_continue(s) == -1)
Christopher Faulet4a28a532019-03-01 11:19:40 +01004781 return -1;
4782 http_remove_header(htx, &ctx);
4783 }
4784 }
Christopher Fauletffcffa82023-04-05 10:33:31 +02004785 msg->flags |= HTTP_MSGF_EXPECT_CHECKED;
Christopher Faulet4a28a532019-03-01 11:19:40 +01004786 return 0;
4787}
4788
Christopher Faulet23a3c792018-11-28 10:01:23 +01004789/* Send a 100-Continue response to the client. It returns 0 on success and -1
4790 * on error. The response channel is updated accordingly.
4791 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004792static int http_reply_100_continue(struct stream *s)
Christopher Faulet23a3c792018-11-28 10:01:23 +01004793{
4794 struct channel *res = &s->res;
4795 struct htx *htx = htx_from_buf(&res->buf);
4796 struct htx_sl *sl;
4797 unsigned int flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|
4798 HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
Christopher Faulet23a3c792018-11-28 10:01:23 +01004799
4800 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
4801 ist("HTTP/1.1"), ist("100"), ist("Continue"));
4802 if (!sl)
4803 goto fail;
4804 sl->info.res.status = 100;
4805
Christopher Faulet1d5ec092019-06-26 14:23:54 +02004806 if (!htx_add_endof(htx, HTX_BLK_EOH))
Christopher Faulet23a3c792018-11-28 10:01:23 +01004807 goto fail;
4808
Christopher Fauleta72a7e42020-01-28 09:28:11 +01004809 if (!http_forward_proxy_resp(s, 0))
4810 goto fail;
Christopher Faulet23a3c792018-11-28 10:01:23 +01004811 return 0;
4812
4813 fail:
4814 /* If an error occurred, remove the incomplete HTTP response from the
4815 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004816 channel_htx_truncate(res, htx);
Christopher Faulet23a3c792018-11-28 10:01:23 +01004817 return -1;
4818}
4819
Christopher Faulet12c51e22018-11-28 15:59:42 +01004820
Christopher Faulet0f226952018-10-22 09:29:56 +02004821/*
4822 * Capture headers from message <htx> according to header list <cap_hdr>, and
4823 * fill the <cap> pointers appropriately.
4824 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004825static void http_capture_headers(struct htx *htx, char **cap, struct cap_hdr *cap_hdr)
Christopher Faulet0f226952018-10-22 09:29:56 +02004826{
4827 struct cap_hdr *h;
4828 int32_t pos;
4829
Christopher Fauleta3f15502019-05-13 15:27:23 +02004830 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet0f226952018-10-22 09:29:56 +02004831 struct htx_blk *blk = htx_get_blk(htx, pos);
4832 enum htx_blk_type type = htx_get_blk_type(blk);
4833 struct ist n, v;
4834
4835 if (type == HTX_BLK_EOH)
4836 break;
4837 if (type != HTX_BLK_HDR)
4838 continue;
4839
4840 n = htx_get_blk_name(htx, blk);
4841
4842 for (h = cap_hdr; h; h = h->next) {
4843 if (h->namelen && (h->namelen == n.len) &&
4844 (strncasecmp(n.ptr, h->name, h->namelen) == 0)) {
4845 if (cap[h->index] == NULL)
4846 cap[h->index] =
4847 pool_alloc(h->pool);
4848
4849 if (cap[h->index] == NULL) {
4850 ha_alert("HTTP capture : out of memory.\n");
4851 break;
4852 }
4853
4854 v = htx_get_blk_value(htx, blk);
Tim Duesterhus2471f5c2021-11-08 09:05:01 +01004855 v = isttrim(v, h->len);
Christopher Faulet0f226952018-10-22 09:29:56 +02004856
4857 memcpy(cap[h->index], v.ptr, v.len);
4858 cap[h->index][v.len]=0;
4859 }
4860 }
4861 }
4862}
4863
Christopher Faulet0b6bdc52018-10-24 11:05:36 +02004864/* Delete a value in a header between delimiters <from> and <next>. The header
4865 * itself is delimited by <start> and <end> pointers. The number of characters
4866 * displaced is returned, and the pointer to the first delimiter is updated if
4867 * required. The function tries as much as possible to respect the following
4868 * principles :
4869 * - replace <from> delimiter by the <next> one unless <from> points to <start>,
4870 * in which case <next> is simply removed
4871 * - set exactly one space character after the new first delimiter, unless there
4872 * are not enough characters in the block being moved to do so.
4873 * - remove unneeded spaces before the previous delimiter and after the new
4874 * one.
4875 *
4876 * It is the caller's responsibility to ensure that :
4877 * - <from> points to a valid delimiter or <start> ;
4878 * - <next> points to a valid delimiter or <end> ;
4879 * - there are non-space chars before <from>.
4880 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004881static int http_del_hdr_value(char *start, char *end, char **from, char *next)
Christopher Faulet0b6bdc52018-10-24 11:05:36 +02004882{
4883 char *prev = *from;
4884
4885 if (prev == start) {
4886 /* We're removing the first value. eat the semicolon, if <next>
4887 * is lower than <end> */
4888 if (next < end)
4889 next++;
4890
4891 while (next < end && HTTP_IS_SPHT(*next))
4892 next++;
4893 }
4894 else {
4895 /* Remove useless spaces before the old delimiter. */
4896 while (HTTP_IS_SPHT(*(prev-1)))
4897 prev--;
4898 *from = prev;
4899
4900 /* copy the delimiter and if possible a space if we're
4901 * not at the end of the line.
4902 */
4903 if (next < end) {
4904 *prev++ = *next++;
4905 if (prev + 1 < next)
4906 *prev++ = ' ';
4907 while (next < end && HTTP_IS_SPHT(*next))
4908 next++;
4909 }
4910 }
4911 memmove(prev, next, end - next);
4912 return (prev - next);
4913}
4914
Christopher Faulet0f226952018-10-22 09:29:56 +02004915
4916/* Formats the start line of the request (without CRLF) and puts it in <str> and
Joseph Herlantc42c0e92018-11-25 10:43:27 -08004917 * return the written length. The line can be truncated if it exceeds <len>.
Christopher Faulet0f226952018-10-22 09:29:56 +02004918 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004919static size_t http_fmt_req_line(const struct htx_sl *sl, char *str, size_t len)
Christopher Faulet0f226952018-10-22 09:29:56 +02004920{
4921 struct ist dst = ist2(str, 0);
4922
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004923 if (istcat(&dst, htx_sl_req_meth(sl), len) == -1)
Christopher Faulet0f226952018-10-22 09:29:56 +02004924 goto end;
4925 if (dst.len + 1 > len)
4926 goto end;
4927 dst.ptr[dst.len++] = ' ';
4928
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004929 if (istcat(&dst, htx_sl_req_uri(sl), len) == -1)
Christopher Faulet0f226952018-10-22 09:29:56 +02004930 goto end;
4931 if (dst.len + 1 > len)
4932 goto end;
4933 dst.ptr[dst.len++] = ' ';
4934
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004935 istcat(&dst, htx_sl_req_vsn(sl), len);
Christopher Faulet0f226952018-10-22 09:29:56 +02004936 end:
4937 return dst.len;
4938}
4939
4940/*
4941 * Print a debug line with a start line.
4942 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004943static void http_debug_stline(const char *dir, struct stream *s, const struct htx_sl *sl)
Christopher Faulet0f226952018-10-22 09:29:56 +02004944{
4945 struct session *sess = strm_sess(s);
4946 int max;
4947
4948 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
4949 dir,
Willy Tarreau88bc8002021-12-06 07:01:02 +00004950 objt_conn(sess->origin) ? (unsigned short)__objt_conn(sess->origin)->handle.fd : -1,
Willy Tarreaufd9417b2022-05-18 16:23:22 +02004951 sc_conn(s->scb) ? (unsigned short)(__sc_conn(s->scb))->handle.fd : -1);
Christopher Faulet0f226952018-10-22 09:29:56 +02004952
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004953 max = HTX_SL_P1_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02004954 UBOUND(max, trash.size - trash.data - 3);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004955 chunk_memcat(&trash, HTX_SL_P1_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02004956 trash.area[trash.data++] = ' ';
4957
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004958 max = HTX_SL_P2_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02004959 UBOUND(max, trash.size - trash.data - 2);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004960 chunk_memcat(&trash, HTX_SL_P2_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02004961 trash.area[trash.data++] = ' ';
4962
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004963 max = HTX_SL_P3_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02004964 UBOUND(max, trash.size - trash.data - 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004965 chunk_memcat(&trash, HTX_SL_P3_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02004966 trash.area[trash.data++] = '\n';
4967
Willy Tarreau2e8ab6b2020-03-14 11:03:20 +01004968 DISGUISE(write(1, trash.area, trash.data));
Christopher Faulet0f226952018-10-22 09:29:56 +02004969}
4970
4971/*
4972 * Print a debug line with a header.
4973 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004974static 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 +02004975{
4976 struct session *sess = strm_sess(s);
4977 int max;
4978
4979 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
4980 dir,
Willy Tarreau88bc8002021-12-06 07:01:02 +00004981 objt_conn(sess->origin) ? (unsigned short)__objt_conn(sess->origin)->handle.fd : -1,
Willy Tarreaufd9417b2022-05-18 16:23:22 +02004982 sc_conn(s->scb) ? (unsigned short)(__sc_conn(s->scb))->handle.fd : -1);
Christopher Faulet0f226952018-10-22 09:29:56 +02004983
4984 max = n.len;
4985 UBOUND(max, trash.size - trash.data - 3);
4986 chunk_memcat(&trash, n.ptr, max);
4987 trash.area[trash.data++] = ':';
4988 trash.area[trash.data++] = ' ';
4989
4990 max = v.len;
4991 UBOUND(max, trash.size - trash.data - 1);
4992 chunk_memcat(&trash, v.ptr, max);
4993 trash.area[trash.data++] = '\n';
4994
Willy Tarreau2e8ab6b2020-03-14 11:03:20 +01004995 DISGUISE(write(1, trash.area, trash.data));
Christopher Faulet0f226952018-10-22 09:29:56 +02004996}
4997
Christopher Fauleta8a46e22019-07-16 14:53:09 +02004998void http_txn_reset_req(struct http_txn *txn)
4999{
Christopher Faulet1aea50e2020-01-17 16:03:53 +01005000 txn->req.flags = 0;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005001 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
5002}
5003
5004void http_txn_reset_res(struct http_txn *txn)
5005{
Christopher Faulet1aea50e2020-01-17 16:03:53 +01005006 txn->rsp.flags = 0;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005007 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
5008}
5009
5010/*
Christopher Faulet75f619a2021-03-08 19:12:58 +01005011 * Create and initialize a new HTTP transaction for stream <s>. This should be
5012 * used before processing any new request. It returns the transaction or NLULL
5013 * on error.
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005014 */
Christopher Faulet75f619a2021-03-08 19:12:58 +01005015struct http_txn *http_create_txn(struct stream *s)
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005016{
Christopher Faulet75f619a2021-03-08 19:12:58 +01005017 struct http_txn *txn;
Willy Tarreaub49672d2022-05-27 10:13:37 +02005018 struct stconn *sc = s->scf;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005019
Christopher Faulet75f619a2021-03-08 19:12:58 +01005020 txn = pool_alloc(pool_head_http_txn);
5021 if (!txn)
5022 return NULL;
5023 s->txn = txn;
5024
Christopher Faulet5eb67f52022-06-22 17:12:05 +02005025 txn->meth = HTTP_METH_OTHER;
Willy Tarreaub49672d2022-05-27 10:13:37 +02005026 txn->flags = ((sc && sc_ep_test(sc, SE_FL_NOT_FIRST)) ? TX_NOT_FIRST : 0);
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005027 txn->status = -1;
Christopher Faulet5cb513a2020-05-13 17:56:56 +02005028 txn->http_reply = NULL;
Christopher Faulete05bf9e2022-03-29 15:23:40 +02005029 txn->l7_buffer = BUF_NULL;
Willy Tarreau8b507582020-02-25 09:35:07 +01005030 write_u32(txn->cache_hash, 0);
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005031
5032 txn->cookie_first_date = 0;
5033 txn->cookie_last_date = 0;
5034
5035 txn->srv_cookie = NULL;
5036 txn->cli_cookie = NULL;
5037 txn->uri = NULL;
5038
5039 http_txn_reset_req(txn);
5040 http_txn_reset_res(txn);
5041
5042 txn->req.chn = &s->req;
5043 txn->rsp.chn = &s->res;
5044
5045 txn->auth.method = HTTP_AUTH_UNKNOWN;
5046
Aurelien DARRAGON5ad2b642022-11-18 09:17:29 +01005047 /* here we don't want to re-initialize s->vars_txn and s->vars_reqres
5048 * variable lists, because they were already initialized upon stream
5049 * creation in stream_new(), and thus may already contain some variables
5050 */
Christopher Faulet75f619a2021-03-08 19:12:58 +01005051
5052 return txn;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005053}
5054
5055/* to be used at the end of a transaction */
Christopher Faulet75f619a2021-03-08 19:12:58 +01005056void http_destroy_txn(struct stream *s)
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005057{
5058 struct http_txn *txn = s->txn;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005059
5060 /* these ones will have been dynamically allocated */
5061 pool_free(pool_head_requri, txn->uri);
5062 pool_free(pool_head_capture, txn->cli_cookie);
5063 pool_free(pool_head_capture, txn->srv_cookie);
Tim Duesterhusa17e6622020-03-05 20:19:02 +01005064 pool_free(pool_head_uniqueid, s->unique_id.ptr);
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005065
Tim Duesterhusa17e6622020-03-05 20:19:02 +01005066 s->unique_id = IST_NULL;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005067 txn->uri = NULL;
5068 txn->srv_cookie = NULL;
5069 txn->cli_cookie = NULL;
5070
Christopher Faulet59399252019-11-07 14:27:52 +01005071 if (!LIST_ISEMPTY(&s->vars_txn.head))
5072 vars_prune(&s->vars_txn, s->sess, s);
5073 if (!LIST_ISEMPTY(&s->vars_reqres.head))
5074 vars_prune(&s->vars_reqres, s->sess, s);
Christopher Faulet75f619a2021-03-08 19:12:58 +01005075
Christopher Faulete05bf9e2022-03-29 15:23:40 +02005076 b_free(&txn->l7_buffer);
5077
Christopher Faulet75f619a2021-03-08 19:12:58 +01005078 pool_free(pool_head_http_txn, txn);
5079 s->txn = NULL;
Christopher Faulet59399252019-11-07 14:27:52 +01005080}
5081
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005082
Christopher Faulet71236de2023-01-13 09:06:38 +01005083void http_set_term_flags(struct stream *s)
5084{
5085 if (!(s->flags & SF_ERR_MASK))
5086 s->flags |= SF_ERR_PRXCOND;
5087
5088 if (!(s->flags & SF_FINST_MASK)) {
5089 if (s->scb->state == SC_ST_INI) {
5090 /* Before any connection attempt on the server side, we
5091 * are still in the request analysis. Just take case to
5092 * detect tarpit error
5093 */
5094 if (s->req.analysers & AN_REQ_HTTP_TARPIT)
5095 s->flags |= SF_FINST_T;
5096 else
5097 s->flags |= SF_FINST_R;
5098 }
5099 else if (s->scb->state == SC_ST_QUE)
5100 s->flags |= SF_FINST_Q;
5101 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)) {
5102 if (unlikely(objt_applet(s->target))) {
5103 s->flags |= SF_FINST_R;
5104 }
5105 else
5106 s->flags |= SF_FINST_C;
5107 }
5108 else {
5109 if (s->txn->rsp.msg_state < HTTP_MSG_DATA) {
5110 /* We are still processing the response headers */
5111 s->flags |= SF_FINST_H;
5112 }
Christopher Faulet5b74f992023-01-26 19:02:07 +01005113 // (res == (done|closing|closed)) & (res->flags & shutw)
5114 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 +01005115 (s->flags & (SF_ERR_CLITO|SF_ERR_CLICL))) {
5116 /* A client error was reported and we are
5117 * transmitting the last block of data
5118 */
5119 s->flags |= SF_FINST_L;
5120 }
5121 else {
5122 /* Otherwise we are in DATA phase on both sides */
5123 s->flags |= SF_FINST_D;
5124 }
5125 }
5126 }
5127}
5128
5129
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005130DECLARE_POOL(pool_head_http_txn, "http_txn", sizeof(struct http_txn));
Christopher Faulet0f226952018-10-22 09:29:56 +02005131
Christopher Fauletf4eb75d2018-10-11 15:55:07 +02005132/*
5133 * Local variables:
5134 * c-indent-level: 8
5135 * c-basic-offset: 8
5136 * End:
5137 */