blob: 8c477708fe69a40d0066249d55722b851453916e [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 Tarreaub2551052020-06-09 09:07:15 +020016#include <haproxy/backend.h>
Willy Tarreau8d366972020-05-27 16:10:29 +020017#include <haproxy/base64.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020018#include <haproxy/capture-t.h>
Amaury Denoyelle03517732021-05-07 14:25:01 +020019#include <haproxy/cfgparse.h>
Willy Tarreauf1d32c42020-06-04 21:07:02 +020020#include <haproxy/channel.h>
Willy Tarreau4aa573d2020-06-04 18:21:56 +020021#include <haproxy/check.h>
Willy Tarreau7ea393d2020-06-04 18:02:10 +020022#include <haproxy/connection.h>
Willy Tarreau36979d92020-06-05 17:27:29 +020023#include <haproxy/errors.h>
Willy Tarreauc7babd82020-06-04 21:29:29 +020024#include <haproxy/filters.h>
Willy Tarreaucd72d8c2020-06-02 19:11:26 +020025#include <haproxy/http.h>
Willy Tarreauc2b1ff02020-06-04 21:21:03 +020026#include <haproxy/http_ana.h>
Willy Tarreau87735332020-06-04 09:08:41 +020027#include <haproxy/http_htx.h>
Willy Tarreau16f958c2020-06-03 08:44:35 +020028#include <haproxy/htx.h>
Willy Tarreauaeed4a82020-06-04 22:01:04 +020029#include <haproxy/log.h>
Willy Tarreau6131d6a2020-06-02 16:48:09 +020030#include <haproxy/net_helper.h>
Willy Tarreaua264d962020-06-04 22:29:18 +020031#include <haproxy/proxy.h>
Willy Tarreau7cd8b6e2020-06-02 17:32:26 +020032#include <haproxy/regex.h>
Willy Tarreau1e56f922020-06-04 23:20:13 +020033#include <haproxy/server-t.h>
Willy Tarreau2eec9b52020-06-04 19:58:55 +020034#include <haproxy/stats.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020035#include <haproxy/stream.h>
Willy Tarreau5e539c92020-06-04 20:45:39 +020036#include <haproxy/stream_interface.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020037#include <haproxy/trace.h>
Willy Tarreau8c42b8a2020-06-04 19:27:34 +020038#include <haproxy/uri_auth-t.h>
Willy Tarreaua1718922020-06-04 16:25:31 +020039#include <haproxy/vars.h>
Christopher Faulete0768eb2018-10-03 16:38:02 +020040
Christopher Faulete0768eb2018-10-03 16:38:02 +020041
Christopher Fauleteea8fc72019-11-05 16:18:10 +010042#define TRACE_SOURCE &trace_strm
43
Christopher Faulet377c5a52018-10-24 21:21:30 +020044extern const char *stat_status_codes[];
Christopher Fauletf2824e62018-10-01 12:12:37 +020045
Willy Tarreauff882702021-04-10 17:23:00 +020046struct pool_head *pool_head_requri __read_mostly = NULL;
47struct pool_head *pool_head_capture __read_mostly = NULL;
Christopher Fauleta8a46e22019-07-16 14:53:09 +020048
49
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020050static void http_end_request(struct stream *s);
51static void http_end_response(struct stream *s);
Christopher Fauletf2824e62018-10-01 12:12:37 +020052
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020053static void http_capture_headers(struct htx *htx, char **cap, struct cap_hdr *cap_hdr);
54static int http_del_hdr_value(char *start, char *end, char **from, char *next);
55static size_t http_fmt_req_line(const struct htx_sl *sl, char *str, size_t len);
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020056static void http_debug_stline(const char *dir, struct stream *s, const struct htx_sl *sl);
57static 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 +020058
Christopher Fauletd4150ad2021-10-13 15:35:55 +020059static enum rule_result http_req_get_intercept_rule(struct proxy *px, struct list *def_rules, struct list *rules, struct stream *s);
60static enum rule_result http_res_get_intercept_rule(struct proxy *px, struct list *def_rules, struct list *rules, struct stream *s);
Christopher Faulet3e964192018-10-24 11:39:23 +020061
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020062static void http_manage_client_side_cookies(struct stream *s, struct channel *req);
63static void http_manage_server_side_cookies(struct stream *s, struct channel *res);
Christopher Fauletfcda7c62018-10-24 11:56:22 +020064
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020065static int http_stats_check_uri(struct stream *s, struct http_txn *txn, struct proxy *backend);
66static int http_handle_stats(struct stream *s, struct channel *req);
Christopher Faulet377c5a52018-10-24 21:21:30 +020067
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020068static int http_handle_expect_hdr(struct stream *s, struct htx *htx, struct http_msg *msg);
69static int http_reply_100_continue(struct stream *s);
Christopher Faulet23a3c792018-11-28 10:01:23 +010070
Christopher Faulete0768eb2018-10-03 16:38:02 +020071/* This stream analyser waits for a complete HTTP request. It returns 1 if the
72 * processing can continue on next analysers, or zero if it either needs more
73 * data or wants to immediately abort the request (eg: timeout, error, ...). It
74 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req.analysers
75 * when it has nothing left to do, and may remove any analyser when it wants to
76 * abort.
77 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020078int http_wait_for_request(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +020079{
Christopher Faulet9768c262018-10-22 09:34:31 +020080
Christopher Faulete0768eb2018-10-03 16:38:02 +020081 /*
Christopher Faulet9768c262018-10-22 09:34:31 +020082 * We will analyze a complete HTTP request to check the its syntax.
Christopher Faulete0768eb2018-10-03 16:38:02 +020083 *
Christopher Faulet9768c262018-10-22 09:34:31 +020084 * Once the start line and all headers are received, we may perform a
85 * capture of the error (if any), and we will set a few fields. We also
86 * check for monitor-uri, logging and finally headers capture.
Christopher Faulete0768eb2018-10-03 16:38:02 +020087 */
Christopher Faulete0768eb2018-10-03 16:38:02 +020088 struct session *sess = s->sess;
89 struct http_txn *txn = s->txn;
90 struct http_msg *msg = &txn->req;
Christopher Faulet9768c262018-10-22 09:34:31 +020091 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +010092 struct htx_sl *sl;
Christopher Faulete0768eb2018-10-03 16:38:02 +020093
Christopher Fauleteea8fc72019-11-05 16:18:10 +010094 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +020095
Christopher Fauletda46a0d2021-01-21 17:32:58 +010096 if (unlikely(!IS_HTX_STRM(s))) {
97 /* It is only possible when a TCP stream is upgrade to HTTP.
98 * There is a transition period during which there is no
99 * data. The stream is still in raw mode and SF_IGNORE flag is
100 * still set. When this happens, the new mux is responsible to
Ilya Shipitsinacf84592021-02-06 22:29:08 +0500101 * handle all errors. Thus we may leave immediately.
Christopher Fauletda46a0d2021-01-21 17:32:58 +0100102 */
103 BUG_ON(!(s->flags & SF_IGNORE) || !c_empty(&s->req));
Christopher Faulet9768c262018-10-22 09:34:31 +0200104
Christopher Faulet97b3a612021-03-15 17:10:12 +0100105 /* Don't connect for now */
106 channel_dont_connect(req);
107
108 /* A SHUTR at this stage means we are performing a "destructive"
109 * HTTP upgrade (TCP>H2). In this case, we can leave.
110 */
111 if (req->flags & CF_SHUTR) {
112 s->logs.logwait = 0;
113 s->logs.level = 0;
114 channel_abort(&s->req);
115 channel_abort(&s->res);
116 req->analysers &= AN_REQ_FLT_END;
117 req->analyse_exp = TICK_ETERNITY;
118 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA, s);
119 return 1;
120 }
Christopher Fauletda46a0d2021-01-21 17:32:58 +0100121 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA, s);
122 return 0;
123 }
124
125 htx = htxbuf(&req->buf);
Christopher Faulet8bebd2f2020-10-06 17:54:56 +0200126
Willy Tarreau4236f032019-03-05 10:43:32 +0100127 /* Parsing errors are caught here */
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200128 if (htx->flags & (HTX_FL_PARSING_ERROR|HTX_FL_PROCESSING_ERROR)) {
Willy Tarreau4236f032019-03-05 10:43:32 +0100129 stream_inc_http_req_ctr(s);
Emeric Brun28976442020-10-07 08:50:09 +0200130 proxy_inc_fe_req_ctr(sess->listener, sess->fe);
Christopher Fauletbf7175f2021-02-10 14:58:01 +0100131 if (htx->flags & HTX_FL_PARSING_ERROR) {
132 stream_inc_http_err_ctr(s);
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200133 goto return_bad_req;
Christopher Fauletbf7175f2021-02-10 14:58:01 +0100134 }
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200135 else
136 goto return_int_err;
Willy Tarreau4236f032019-03-05 10:43:32 +0100137 }
138
Christopher Faulete0768eb2018-10-03 16:38:02 +0200139 /* we're speaking HTTP here, so let's speak HTTP to the client */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200140 s->srv_error = http_return_srv_error;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200141
Christopher Faulet9768c262018-10-22 09:34:31 +0200142 msg->msg_state = HTTP_MSG_BODY;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200143 stream_inc_http_req_ctr(s);
Emeric Brun28976442020-10-07 08:50:09 +0200144 proxy_inc_fe_req_ctr(sess->listener, sess->fe); /* one more valid request for this FE */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200145
Christopher Faulet9768c262018-10-22 09:34:31 +0200146 /* kill the pending keep-alive timeout */
Christopher Faulet9768c262018-10-22 09:34:31 +0200147 req->analyse_exp = TICK_ETERNITY;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200148
Christopher Faulet29f17582019-05-23 11:03:26 +0200149 BUG_ON(htx_get_first_type(htx) != HTX_BLK_REQ_SL);
Christopher Faulet297fbb42019-05-13 14:41:27 +0200150 sl = http_get_stline(htx);
Christopher Faulet03599112018-11-27 11:21:21 +0100151
Christopher Faulet9768c262018-10-22 09:34:31 +0200152 /* 0: we might have to print this header in debug mode */
153 if (unlikely((global.mode & MODE_DEBUG) &&
154 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
155 int32_t pos;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200156
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200157 http_debug_stline("clireq", s, sl);
Christopher Faulet9768c262018-10-22 09:34:31 +0200158
Christopher Fauleta3f15502019-05-13 15:27:23 +0200159 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet9768c262018-10-22 09:34:31 +0200160 struct htx_blk *blk = htx_get_blk(htx, pos);
161 enum htx_blk_type type = htx_get_blk_type(blk);
162
163 if (type == HTX_BLK_EOH)
164 break;
165 if (type != HTX_BLK_HDR)
166 continue;
167
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200168 http_debug_hdr("clihdr", s,
169 htx_get_blk_name(htx, blk),
170 htx_get_blk_value(htx, blk));
Christopher Faulet9768c262018-10-22 09:34:31 +0200171 }
172 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200173
174 /*
Christopher Faulet03599112018-11-27 11:21:21 +0100175 * 1: identify the method and the version. Also set HTTP flags
Christopher Faulete0768eb2018-10-03 16:38:02 +0200176 */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100177 txn->meth = sl->info.req.meth;
Christopher Faulet03599112018-11-27 11:21:21 +0100178 if (sl->flags & HTX_SL_F_VER_11)
Christopher Faulet9768c262018-10-22 09:34:31 +0200179 msg->flags |= HTTP_MSGF_VER_11;
Christopher Faulet03599112018-11-27 11:21:21 +0100180 msg->flags |= HTTP_MSGF_XFER_LEN;
Christopher Faulet2a408542020-11-20 14:22:37 +0100181 if (sl->flags & HTX_SL_F_CLEN)
182 msg->flags |= HTTP_MSGF_CNT_LEN;
183 else if (sl->flags & HTX_SL_F_CHNK)
184 msg->flags |= HTTP_MSGF_TE_CHNK;
Christopher Fauletb2db4fa2018-11-27 16:51:09 +0100185 if (sl->flags & HTX_SL_F_BODYLESS)
186 msg->flags |= HTTP_MSGF_BODYLESS;
Christopher Faulet576c3582021-01-08 15:53:01 +0100187 if (sl->flags & HTX_SL_F_CONN_UPG)
188 msg->flags |= HTTP_MSGF_CONN_UPG;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200189
190 /* we can make use of server redirect on GET and HEAD */
191 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
192 s->flags |= SF_REDIRECTABLE;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100193 else if (txn->meth == HTTP_METH_OTHER && isteqi(htx_sl_req_meth(sl), ist("PRI"))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200194 /* PRI is reserved for the HTTP/2 preface */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200195 goto return_bad_req;
196 }
197
198 /*
Christopher Faulet6072beb2020-02-18 15:34:58 +0100199 * 2: check if the URI matches the monitor_uri. We have to do this for
200 * every request which gets in, because the monitor-uri is defined by
201 * the frontend. If the monitor-uri starts with a '/', the matching is
202 * done against the request's path. Otherwise, the request's uri is
203 * used. It is a workaround to let HTTP/2 health-checks work as
204 * expected.
Christopher Faulete0768eb2018-10-03 16:38:02 +0200205 */
Tim Duesterhus4b1fcaa2022-03-05 00:52:40 +0100206 if (unlikely(isttest(sess->fe->monitor_uri))) {
207 const struct ist monitor_uri = sess->fe->monitor_uri;
Amaury Denoyellec453f952021-07-06 11:40:12 +0200208 struct http_uri_parser parser = http_uri_parser_init(htx_sl_req_uri(sl));
Christopher Faulete0768eb2018-10-03 16:38:02 +0200209
Amaury Denoyelle5a9bd372021-07-06 11:23:10 +0200210 if ((istptr(monitor_uri)[0] == '/' &&
Amaury Denoyellec453f952021-07-06 11:40:12 +0200211 isteq(http_parse_path(&parser), monitor_uri)) ||
Amaury Denoyelle5a9bd372021-07-06 11:23:10 +0200212 isteq(htx_sl_req_uri(sl), monitor_uri)) {
213 /*
214 * We have found the monitor URI
215 */
216 struct acl_cond *cond;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200217
Amaury Denoyelle5a9bd372021-07-06 11:23:10 +0200218 s->flags |= SF_MONITOR;
219 _HA_ATOMIC_INC(&sess->fe->fe_counters.intercepted_req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200220
Amaury Denoyelle5a9bd372021-07-06 11:23:10 +0200221 /* Check if we want to fail this monitor request or not */
222 list_for_each_entry(cond, &sess->fe->mon_fail_cond, list) {
223 int ret = acl_exec_cond(cond, sess->fe, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200224
Amaury Denoyelle5a9bd372021-07-06 11:23:10 +0200225 ret = acl_pass(ret);
226 if (cond->pol == ACL_COND_UNLESS)
227 ret = !ret;
228
229 if (ret) {
230 /* we fail this request, let's return 503 service unavail */
231 txn->status = 503;
232 if (!(s->flags & SF_ERR_MASK))
233 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
234 goto return_prx_cond;
235 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200236 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200237
Amaury Denoyelle5a9bd372021-07-06 11:23:10 +0200238 /* nothing to fail, let's reply normally */
239 txn->status = 200;
240 if (!(s->flags & SF_ERR_MASK))
241 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
242 goto return_prx_cond;
243 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200244 }
245
246 /*
247 * 3: Maybe we have to copy the original REQURI for the logs ?
248 * Note: we cannot log anymore if the request has been
249 * classified as invalid.
250 */
251 if (unlikely(s->logs.logwait & LW_REQ)) {
252 /* we have a complete HTTP request that we must log */
253 if ((txn->uri = pool_alloc(pool_head_requri)) != NULL) {
Christopher Faulet9768c262018-10-22 09:34:31 +0200254 size_t len;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200255
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200256 len = http_fmt_req_line(sl, txn->uri, global.tune.requri_len - 1);
Christopher Faulet9768c262018-10-22 09:34:31 +0200257 txn->uri[len] = 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200258
259 if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT)))
260 s->do_log(s);
261 } else {
262 ha_alert("HTTP logging : out of memory.\n");
263 }
264 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200265
Christopher Faulete0768eb2018-10-03 16:38:02 +0200266 /* if the frontend has "option http-use-proxy-header", we'll check if
267 * we have what looks like a proxied connection instead of a connection,
268 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
269 * Note that this is *not* RFC-compliant, however browsers and proxies
270 * happen to do that despite being non-standard :-(
271 * We consider that a request not beginning with either '/' or '*' is
272 * a proxied connection, which covers both "scheme://location" and
273 * CONNECT ip:port.
274 */
275 if ((sess->fe->options2 & PR_O2_USE_PXHDR) &&
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100276 *HTX_SL_REQ_UPTR(sl) != '/' && *HTX_SL_REQ_UPTR(sl) != '*')
Christopher Faulete0768eb2018-10-03 16:38:02 +0200277 txn->flags |= TX_USE_PX_CONN;
278
Christopher Faulete0768eb2018-10-03 16:38:02 +0200279 /* 5: we may need to capture headers */
280 if (unlikely((s->logs.logwait & LW_REQHDR) && s->req_cap))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200281 http_capture_headers(htx, s->req_cap, sess->fe->req_cap);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200282
Christopher Faulete0768eb2018-10-03 16:38:02 +0200283 /* we may have to wait for the request's body */
Christopher Faulet9768c262018-10-22 09:34:31 +0200284 if (s->be->options & PR_O_WREQ_BODY)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200285 req->analysers |= AN_REQ_HTTP_BODY;
286
287 /*
288 * RFC7234#4:
289 * A cache MUST write through requests with methods
290 * that are unsafe (Section 4.2.1 of [RFC7231]) to
291 * the origin server; i.e., a cache is not allowed
292 * to generate a reply to such a request before
293 * having forwarded the request and having received
294 * a corresponding response.
295 *
296 * RFC7231#4.2.1:
297 * Of the request methods defined by this
298 * specification, the GET, HEAD, OPTIONS, and TRACE
299 * methods are defined to be safe.
300 */
301 if (likely(txn->meth == HTTP_METH_GET ||
302 txn->meth == HTTP_METH_HEAD ||
303 txn->meth == HTTP_METH_OPTIONS ||
304 txn->meth == HTTP_METH_TRACE))
305 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
306
307 /* end of job, return OK */
308 req->analysers &= ~an_bit;
309 req->analyse_exp = TICK_ETERNITY;
Christopher Faulet9768c262018-10-22 09:34:31 +0200310
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100311 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200312 return 1;
313
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200314 return_int_err:
315 txn->status = 500;
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200316 if (!(s->flags & SF_ERR_MASK))
317 s->flags |= SF_ERR_INTERNAL;
Willy Tarreau4781b152021-04-06 13:53:36 +0200318 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
William Lallemand36119de2021-03-08 15:26:48 +0100319 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200320 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200321 goto return_prx_cond;
322
Christopher Faulete0768eb2018-10-03 16:38:02 +0200323 return_bad_req:
Christopher Faulet9768c262018-10-22 09:34:31 +0200324 txn->status = 400;
Willy Tarreau4781b152021-04-06 13:53:36 +0200325 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req);
William Lallemand36119de2021-03-08 15:26:48 +0100326 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200327 _HA_ATOMIC_INC(&sess->listener->counters->failed_req);
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200328 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200329
330 return_prx_cond:
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200331 http_reply_and_close(s, txn->status, http_error_message(s));
332
Christopher Faulete0768eb2018-10-03 16:38:02 +0200333 if (!(s->flags & SF_ERR_MASK))
334 s->flags |= SF_ERR_PRXCOND;
335 if (!(s->flags & SF_FINST_MASK))
336 s->flags |= SF_FINST_R;
337
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100338 DBG_TRACE_DEVEL("leaving on error",
339 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200340 return 0;
341}
342
343
344/* This stream analyser runs all HTTP request processing which is common to
345 * frontends and backends, which means blocking ACLs, filters, connection-close,
346 * reqadd, stats and redirects. This is performed for the designated proxy.
347 * It returns 1 if the processing can continue on next analysers, or zero if it
348 * either needs more data or wants to immediately abort the request (eg: deny,
349 * error, ...).
350 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200351int http_process_req_common(struct stream *s, struct channel *req, int an_bit, struct proxy *px)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200352{
Christopher Fauletd4150ad2021-10-13 15:35:55 +0200353 struct list *def_rules, *rules;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200354 struct session *sess = s->sess;
355 struct http_txn *txn = s->txn;
356 struct http_msg *msg = &txn->req;
Christopher Fauletff2759f2018-10-24 11:13:16 +0200357 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200358 struct redirect_rule *rule;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200359 enum rule_result verdict;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200360 struct connection *conn = objt_conn(sess->origin);
361
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100362 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200363
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100364 htx = htxbuf(&req->buf);
Christopher Fauletff2759f2018-10-24 11:13:16 +0200365
Christopher Faulet1907ccc2019-04-29 13:12:02 +0200366 /* just in case we have some per-backend tracking. Only called the first
367 * execution of the analyser. */
Christopher Fauletd4150ad2021-10-13 15:35:55 +0200368 if (!s->current_rule && !s->current_rule_list)
Christopher Faulet1907ccc2019-04-29 13:12:02 +0200369 stream_inc_be_http_req_ctr(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200370
Christopher Fauletd4150ad2021-10-13 15:35:55 +0200371 def_rules = ((px->defpx && (an_bit == AN_REQ_HTTP_PROCESS_FE || px != sess->fe)) ? &px->defpx->http_req_rules : NULL);
372 rules = &px->http_req_rules;
373
Christopher Faulete0768eb2018-10-03 16:38:02 +0200374 /* evaluate http-request rules */
Christopher Fauletd4150ad2021-10-13 15:35:55 +0200375 if ((def_rules && !LIST_ISEMPTY(def_rules)) || !LIST_ISEMPTY(rules)) {
376 verdict = http_req_get_intercept_rule(px, def_rules, rules, s);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200377
378 switch (verdict) {
379 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
380 goto return_prx_yield;
381
382 case HTTP_RULE_RES_CONT:
383 case HTTP_RULE_RES_STOP: /* nothing to do */
384 break;
385
386 case HTTP_RULE_RES_DENY: /* deny or tarpit */
387 if (txn->flags & TX_CLTARPIT)
388 goto tarpit;
389 goto deny;
390
391 case HTTP_RULE_RES_ABRT: /* abort request, response already sent. Eg: auth */
392 goto return_prx_cond;
393
394 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
395 goto done;
396
397 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
398 goto return_bad_req;
Christopher Faulet3a26bee2019-12-16 12:47:40 +0100399
400 case HTTP_RULE_RES_ERROR: /* failed with a bad request */
401 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200402 }
403 }
404
405 if (conn && (conn->flags & CO_FL_EARLY_DATA) &&
Olivier Houchard220a26c2020-01-23 14:57:36 +0100406 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_SSL_WAIT_HS))) {
Christopher Fauletff2759f2018-10-24 11:13:16 +0200407 struct http_hdr_ctx ctx;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200408
Christopher Fauletff2759f2018-10-24 11:13:16 +0200409 ctx.blk = NULL;
410 if (!http_find_header(htx, ist("Early-Data"), &ctx, 0)) {
411 if (unlikely(!http_add_header(htx, ist("Early-Data"), ist("1"))))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100412 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200413 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200414 }
415
416 /* OK at this stage, we know that the request was accepted according to
417 * the http-request rules, we can check for the stats. Note that the
418 * URI is detected *before* the req* rules in order not to be affected
419 * by a possible reqrep, while they are processed *after* so that a
420 * reqdeny can still block them. This clearly needs to change in 1.6!
421 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200422 if (!s->target && http_stats_check_uri(s, txn, px)) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200423 s->target = &http_stats_applet.obj_type;
Christopher Fauleta6294472021-12-23 13:25:57 +0100424 if (unlikely(!si_register_handler(cs_si(s->csb), objt_applet(s->target)))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200425 s->logs.tv_request = now;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200426 if (!(s->flags & SF_ERR_MASK))
427 s->flags |= SF_ERR_RESOURCE;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100428 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200429 }
430
431 /* parse the whole stats request and extract the relevant information */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200432 http_handle_stats(s, req);
Christopher Fauletd4150ad2021-10-13 15:35:55 +0200433 verdict = http_req_get_intercept_rule(px, NULL, &px->uri_auth->http_req_rules, s);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200434 /* not all actions implemented: deny, allow, auth */
435
436 if (verdict == HTTP_RULE_RES_DENY) /* stats http-request deny */
437 goto deny;
438
439 if (verdict == HTTP_RULE_RES_ABRT) /* stats auth / stats http-request auth */
440 goto return_prx_cond;
Christopher Faulet3a26bee2019-12-16 12:47:40 +0100441
442 if (verdict == HTTP_RULE_RES_BADREQ) /* failed with a bad request */
443 goto return_bad_req;
444
445 if (verdict == HTTP_RULE_RES_ERROR) /* failed with a bad request */
446 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200447 }
448
Christopher Faulet2571bc62019-03-01 11:44:26 +0100449 /* Proceed with the applets now. */
450 if (unlikely(objt_applet(s->target))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200451 if (sess->fe == s->be) /* report it if the request was intercepted by the frontend */
Willy Tarreau4781b152021-04-06 13:53:36 +0200452 _HA_ATOMIC_INC(&sess->fe->fe_counters.intercepted_req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200453
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200454 if (http_handle_expect_hdr(s, htx, msg) == -1)
Christopher Fauletb8a53712019-12-16 11:29:38 +0100455 goto return_int_err;
Christopher Fauletbcf242a2019-03-01 11:36:26 +0100456
Christopher Faulete0768eb2018-10-03 16:38:02 +0200457 if (!(s->flags & SF_ERR_MASK)) // this is not really an error but it is
458 s->flags |= SF_ERR_LOCAL; // to mark that it comes from the proxy
459 if (!(s->flags & SF_FINST_MASK))
460 s->flags |= SF_FINST_R;
461
Christopher Fauletc2ac5e42021-03-08 18:20:09 +0100462 if (HAS_FILTERS(s))
463 req->analysers |= AN_REQ_FLT_HTTP_HDRS;
464
Christopher Faulete0768eb2018-10-03 16:38:02 +0200465 /* enable the minimally required analyzers to handle keep-alive and compression on the HTTP response */
466 req->analysers &= (AN_REQ_HTTP_BODY | AN_REQ_FLT_HTTP_HDRS | AN_REQ_FLT_END);
467 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
468 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Christopher Fauletbcf242a2019-03-01 11:36:26 +0100469
470 req->flags |= CF_SEND_DONTWAIT;
471 s->flags |= SF_ASSIGNED;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200472 goto done;
473 }
474
475 /* check whether we have some ACLs set to redirect this request */
476 list_for_each_entry(rule, &px->redirect_rules, list) {
477 if (rule->cond) {
478 int ret;
479
480 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
481 ret = acl_pass(ret);
482 if (rule->cond->pol == ACL_COND_UNLESS)
483 ret = !ret;
484 if (!ret)
485 continue;
486 }
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200487 if (!http_apply_redirect_rule(rule, s, txn))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100488 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200489 goto done;
490 }
491
492 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
493 * If this happens, then the data will not come immediately, so we must
494 * send all what we have without waiting. Note that due to the small gain
495 * in waiting for the body of the request, it's easier to simply put the
496 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
497 * itself once used.
498 */
499 req->flags |= CF_SEND_DONTWAIT;
500
501 done: /* done with this analyser, continue with next ones that the calling
502 * points will have set, if any.
503 */
504 req->analyse_exp = TICK_ETERNITY;
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +0500505 done_without_exp: /* done with this analyser, but don't reset the analyse_exp. */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200506 req->analysers &= ~an_bit;
Christopher Fauletd4150ad2021-10-13 15:35:55 +0200507 s->current_rule = s->current_rule_list = NULL;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100508 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200509 return 1;
510
511 tarpit:
512 /* Allow cookie logging
513 */
514 if (s->be->cookie_name || sess->fe->capture_name)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200515 http_manage_client_side_cookies(s, req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200516
517 /* When a connection is tarpitted, we use the tarpit timeout,
518 * which may be the same as the connect timeout if unspecified.
519 * If unset, then set it to zero because we really want it to
520 * eventually expire. We build the tarpit as an analyser.
521 */
Christopher Faulet202c6ce2019-01-07 14:57:35 +0100522 channel_htx_erase(&s->req, htx);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200523
524 /* wipe the request out so that we can drop the connection early
525 * if the client closes first.
526 */
527 channel_dont_connect(req);
528
Christopher Faulete0768eb2018-10-03 16:38:02 +0200529 req->analysers &= AN_REQ_FLT_END; /* remove switching rules etc... */
530 req->analysers |= AN_REQ_HTTP_TARPIT;
531 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
532 if (!req->analyse_exp)
533 req->analyse_exp = tick_add(now_ms, 0);
534 stream_inc_http_err_ctr(s);
Willy Tarreau4781b152021-04-06 13:53:36 +0200535 _HA_ATOMIC_INC(&sess->fe->fe_counters.denied_req);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100536 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau4781b152021-04-06 13:53:36 +0200537 _HA_ATOMIC_INC(&s->be->be_counters.denied_req);
William Lallemand36119de2021-03-08 15:26:48 +0100538 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200539 _HA_ATOMIC_INC(&sess->listener->counters->denied_req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200540 goto done_without_exp;
541
542 deny: /* this request was blocked (denied) */
543
544 /* Allow cookie logging
545 */
546 if (s->be->cookie_name || sess->fe->capture_name)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200547 http_manage_client_side_cookies(s, req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200548
Christopher Faulete0768eb2018-10-03 16:38:02 +0200549 s->logs.tv_request = now;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200550 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 Fauletb8a53712019-12-16 11:29:38 +0100556 goto return_prx_err;
557
558 return_int_err:
559 txn->status = 500;
560 if (!(s->flags & SF_ERR_MASK))
561 s->flags |= SF_ERR_INTERNAL;
Willy Tarreau4781b152021-04-06 13:53:36 +0200562 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100563 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau4781b152021-04-06 13:53:36 +0200564 _HA_ATOMIC_INC(&s->be->be_counters.internal_errors);
William Lallemand36119de2021-03-08 15:26:48 +0100565 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200566 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100567 goto return_prx_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200568
569 return_bad_req:
Christopher Faulete0768eb2018-10-03 16:38:02 +0200570 txn->status = 400;
Willy Tarreau4781b152021-04-06 13:53:36 +0200571 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req);
William Lallemand36119de2021-03-08 15:26:48 +0100572 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200573 _HA_ATOMIC_INC(&sess->listener->counters->failed_req);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100574 /* fall through */
575
576 return_prx_err:
577 http_reply_and_close(s, txn->status, http_error_message(s));
578 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200579
580 return_prx_cond:
581 if (!(s->flags & SF_ERR_MASK))
582 s->flags |= SF_ERR_PRXCOND;
583 if (!(s->flags & SF_FINST_MASK))
584 s->flags |= SF_FINST_R;
585
586 req->analysers &= AN_REQ_FLT_END;
587 req->analyse_exp = TICK_ETERNITY;
Christopher Fauletd4150ad2021-10-13 15:35:55 +0200588 s->current_rule = s->current_rule_list = NULL;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100589 DBG_TRACE_DEVEL("leaving on error",
590 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200591 return 0;
592
593 return_prx_yield:
594 channel_dont_connect(req);
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100595 DBG_TRACE_DEVEL("waiting for more data",
596 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200597 return 0;
598}
599
600/* This function performs all the processing enabled for the current request.
601 * It returns 1 if the processing can continue on next analysers, or zero if it
602 * needs more data, encounters an error, or wants to immediately abort the
603 * request. It relies on buffers flags, and updates s->req.analysers.
604 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200605int http_process_request(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200606{
607 struct session *sess = s->sess;
608 struct http_txn *txn = s->txn;
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200609 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200610 struct connection *cli_conn = objt_conn(strm_sess(s)->origin);
611
Christopher Faulet8bebd2f2020-10-06 17:54:56 +0200612 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200613
614 /*
615 * Right now, we know that we have processed the entire headers
616 * and that unwanted requests have been filtered out. We can do
617 * whatever we want with the remaining request. Also, now we
618 * may have separate values for ->fe, ->be.
619 */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100620 htx = htxbuf(&req->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200621
622 /*
Christopher Faulete0768eb2018-10-03 16:38:02 +0200623 * 7: Now we can work with the cookies.
624 * Note that doing so might move headers in the request, but
625 * the fields will stay coherent and the URI will not move.
626 * This should only be performed in the backend.
627 */
628 if (s->be->cookie_name || sess->fe->capture_name)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200629 http_manage_client_side_cookies(s, req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200630
Tim Duesterhus2825b4b2020-02-28 15:13:34 +0100631 /* 8: Generate unique ID if a "unique-id-format" is defined.
632 *
633 * A unique ID is generated even when it is not sent to ensure that the ID can make use of
634 * fetches only available in the HTTP request processing stage.
635 */
636 if (!LIST_ISEMPTY(&sess->fe->format_unique_id)) {
Tim Duesterhusa17e6622020-03-05 20:19:02 +0100637 struct ist unique_id = stream_generate_unique_id(s, &sess->fe->format_unique_id);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200638
Tim Duesterhusa17e6622020-03-05 20:19:02 +0100639 if (!isttest(unique_id)) {
Christopher Fauletb8a53712019-12-16 11:29:38 +0100640 if (!(s->flags & SF_ERR_MASK))
641 s->flags |= SF_ERR_RESOURCE;
642 goto return_int_err;
643 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200644
Tim Duesterhus2825b4b2020-02-28 15:13:34 +0100645 /* send unique ID if a "unique-id-header" is defined */
Tim Duesterhus0643b0e2020-03-05 17:56:35 +0100646 if (isttest(sess->fe->header_unique_id) &&
Tim Duesterhusa17e6622020-03-05 20:19:02 +0100647 unlikely(!http_add_header(htx, sess->fe->header_unique_id, s->unique_id)))
Tim Duesterhus2825b4b2020-02-28 15:13:34 +0100648 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200649 }
650
651 /*
652 * 9: add X-Forwarded-For if either the frontend or the backend
653 * asks for it.
654 */
655 if ((sess->fe->options | s->be->options) & PR_O_FWDFOR) {
Christopher Fauleta6294472021-12-23 13:25:57 +0100656 const struct sockaddr_storage *src = si_src(cs_si(s->csf));
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200657 struct http_hdr_ctx ctx = { .blk = NULL };
Tim Duesterhusb50ab842022-03-05 00:52:41 +0100658 struct ist hdr = isttest(s->be->fwdfor_hdr_name) ? s->be->fwdfor_hdr_name : sess->fe->fwdfor_hdr_name;
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200659
Christopher Faulete0768eb2018-10-03 16:38:02 +0200660 if (!((sess->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200661 http_find_header(htx, hdr, &ctx, 0)) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200662 /* The header is set to be added only if none is present
663 * and we found it, so don't do anything.
664 */
665 }
Christopher Faulet8a104ba2021-10-25 07:41:30 +0200666 else if (src && src->ss_family == AF_INET) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200667 /* Add an X-Forwarded-For header unless the source IP is
668 * in the 'except' network range.
669 */
Christopher Faulet8a104ba2021-10-25 07:41:30 +0200670 if (ipcmp2net(src, &sess->fe->except_xff_net) &&
671 ipcmp2net(src, &s->be->except_xff_net)) {
672 unsigned char *pn = (unsigned char *)&((struct sockaddr_in *)src)->sin_addr;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200673
674 /* Note: we rely on the backend to get the header name to be used for
675 * x-forwarded-for, because the header is really meant for the backends.
676 * However, if the backend did not specify any option, we have to rely
677 * on the frontend's header name.
678 */
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200679 chunk_printf(&trash, "%d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
680 if (unlikely(!http_add_header(htx, hdr, ist2(trash.area, trash.data))))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100681 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200682 }
683 }
Christopher Faulet8a104ba2021-10-25 07:41:30 +0200684 else if (src && src->ss_family == AF_INET6) {
Christopher Faulet5d1def62021-02-26 09:19:15 +0100685 /* Add an X-Forwarded-For header unless the source IP is
686 * in the 'except' network range.
Christopher Faulete0768eb2018-10-03 16:38:02 +0200687 */
Christopher Faulet8a104ba2021-10-25 07:41:30 +0200688 if (ipcmp2net(src, &sess->fe->except_xff_net) &&
689 ipcmp2net(src, &s->be->except_xff_net)) {
Christopher Faulet5d1def62021-02-26 09:19:15 +0100690 char pn[INET6_ADDRSTRLEN];
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200691
Christopher Faulet5d1def62021-02-26 09:19:15 +0100692 inet_ntop(AF_INET6,
Christopher Faulet8a104ba2021-10-25 07:41:30 +0200693 (const void *)&((struct sockaddr_in6 *)(src))->sin6_addr,
Christopher Faulet5d1def62021-02-26 09:19:15 +0100694 pn, sizeof(pn));
Christopher Faulete0768eb2018-10-03 16:38:02 +0200695
Christopher Faulet5d1def62021-02-26 09:19:15 +0100696 /* Note: we rely on the backend to get the header name to be used for
697 * x-forwarded-for, because the header is really meant for the backends.
698 * However, if the backend did not specify any option, we have to rely
699 * on the frontend's header name.
700 */
701 chunk_printf(&trash, "%s", pn);
702 if (unlikely(!http_add_header(htx, hdr, ist2(trash.area, trash.data))))
703 goto return_int_err;
704 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200705 }
706 }
707
708 /*
709 * 10: add X-Original-To if either the frontend or the backend
710 * asks for it.
711 */
712 if ((sess->fe->options | s->be->options) & PR_O_ORGTO) {
Christopher Fauleta6294472021-12-23 13:25:57 +0100713 const struct sockaddr_storage *dst = si_dst(cs_si(s->csf));
Tim Duesterhuse502c3e2022-03-05 00:52:42 +0100714 struct ist hdr = isttest(s->be->orgto_hdr_name) ? s->be->orgto_hdr_name : sess->fe->orgto_hdr_name;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200715
Christopher Faulet8a104ba2021-10-25 07:41:30 +0200716 if (dst && dst->ss_family == AF_INET) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200717 /* Add an X-Original-To header unless the destination IP is
718 * in the 'except' network range.
719 */
Christopher Faulet8a104ba2021-10-25 07:41:30 +0200720 if (ipcmp2net(dst, &sess->fe->except_xot_net) &&
721 ipcmp2net(dst, &s->be->except_xot_net)) {
722 unsigned char *pn = (unsigned char *)&((struct sockaddr_in *)dst)->sin_addr;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200723
724 /* Note: we rely on the backend to get the header name to be used for
725 * x-original-to, because the header is really meant for the backends.
726 * However, if the backend did not specify any option, we have to rely
727 * on the frontend's header name.
728 */
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200729 chunk_printf(&trash, "%d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
730 if (unlikely(!http_add_header(htx, hdr, ist2(trash.area, trash.data))))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100731 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200732 }
733 }
Christopher Faulet8a104ba2021-10-25 07:41:30 +0200734 else if (dst && dst->ss_family == AF_INET6) {
Christopher Faulet5d1def62021-02-26 09:19:15 +0100735 /* Add an X-Original-To header unless the source IP is
736 * in the 'except' network range.
737 */
Christopher Faulet8a104ba2021-10-25 07:41:30 +0200738 if (ipcmp2net(dst, &sess->fe->except_xot_net) &&
739 ipcmp2net(dst, &s->be->except_xot_net)) {
Christopher Faulet5d1def62021-02-26 09:19:15 +0100740 char pn[INET6_ADDRSTRLEN];
741
742 inet_ntop(AF_INET6,
Christopher Faulet8a104ba2021-10-25 07:41:30 +0200743 (const void *)&((struct sockaddr_in6 *)dst)->sin6_addr,
Christopher Faulet5d1def62021-02-26 09:19:15 +0100744 pn, sizeof(pn));
745
746 /* Note: we rely on the backend to get the header name to be used for
747 * x-forwarded-for, because the header is really meant for the backends.
748 * However, if the backend did not specify any option, we have to rely
749 * on the frontend's header name.
750 */
751 chunk_printf(&trash, "%s", pn);
752 if (unlikely(!http_add_header(htx, hdr, ist2(trash.area, trash.data))))
753 goto return_int_err;
754 }
755 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200756 }
757
Christopher Fauletc2ac5e42021-03-08 18:20:09 +0100758 /* Filter the request headers if there are filters attached to the
759 * stream.
760 */
761 if (HAS_FILTERS(s))
762 req->analysers |= AN_REQ_FLT_HTTP_HDRS;
763
Christopher Faulete0768eb2018-10-03 16:38:02 +0200764 /* If we have no server assigned yet and we're balancing on url_param
765 * with a POST request, we may be interested in checking the body for
766 * that parameter. This will be done in another analyser.
767 */
768 if (!(s->flags & (SF_ASSIGNED|SF_DIRECT)) &&
Willy Tarreau089eaa02019-01-14 15:17:46 +0100769 s->txn->meth == HTTP_METH_POST &&
770 (s->be->lbprm.algo & BE_LB_ALGO) == BE_LB_ALGO_PH) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200771 channel_dont_connect(req);
772 req->analysers |= AN_REQ_HTTP_BODY;
773 }
774
775 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
776 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau1a18b542018-12-11 16:37:42 +0100777
Christopher Faulete0768eb2018-10-03 16:38:02 +0200778 /* We expect some data from the client. Unless we know for sure
779 * we already have a full request, we have to re-enable quick-ack
780 * in case we previously disabled it, otherwise we might cause
781 * the client to delay further data.
782 */
William Lallemand36119de2021-03-08 15:26:48 +0100783 if ((sess->listener && (sess->listener->options & LI_O_NOQUICKACK)) && !(htx->flags & HTX_FL_EOM))
Willy Tarreau1a18b542018-12-11 16:37:42 +0100784 conn_set_quickack(cli_conn, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200785
786 /*************************************************************
787 * OK, that's finished for the headers. We have done what we *
788 * could. Let's switch to the DATA state. *
789 ************************************************************/
790 req->analyse_exp = TICK_ETERNITY;
791 req->analysers &= ~an_bit;
792
793 s->logs.tv_request = now;
794 /* OK let's go on with the BODY now */
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100795 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200796 return 1;
797
Christopher Fauletb8a53712019-12-16 11:29:38 +0100798 return_int_err:
799 txn->status = 500;
800 if (!(s->flags & SF_ERR_MASK))
801 s->flags |= SF_ERR_INTERNAL;
Willy Tarreau4781b152021-04-06 13:53:36 +0200802 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100803 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau4781b152021-04-06 13:53:36 +0200804 _HA_ATOMIC_INC(&s->be->be_counters.internal_errors);
William Lallemand36119de2021-03-08 15:26:48 +0100805 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200806 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100807
Christopher Fauletb8a53712019-12-16 11:29:38 +0100808 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +0200809
810 if (!(s->flags & SF_ERR_MASK))
811 s->flags |= SF_ERR_PRXCOND;
812 if (!(s->flags & SF_FINST_MASK))
813 s->flags |= SF_FINST_R;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100814
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100815 DBG_TRACE_DEVEL("leaving on error",
816 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200817 return 0;
818}
819
820/* This function is an analyser which processes the HTTP tarpit. It always
821 * returns zero, at the beginning because it prevents any other processing
822 * from occurring, and at the end because it terminates the request.
823 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200824int http_process_tarpit(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200825{
826 struct http_txn *txn = s->txn;
827
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100828 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, &txn->req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200829 /* This connection is being tarpitted. The CLIENT side has
830 * already set the connect expiration date to the right
831 * timeout. We just have to check that the client is still
832 * there and that the timeout has not expired.
833 */
834 channel_dont_connect(req);
835 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100836 !tick_is_expired(req->analyse_exp, now_ms)) {
Christopher Fauletb0c87f12021-10-29 14:37:07 +0200837 /* Be sure to drain all data from the request channel */
838 channel_htx_erase(req, htxbuf(&req->buf));
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100839 DBG_TRACE_DEVEL("waiting for tarpit timeout expiry",
840 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200841 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100842 }
843
Christopher Faulete0768eb2018-10-03 16:38:02 +0200844
845 /* We will set the queue timer to the time spent, just for
846 * logging purposes. We fake a 500 server error, so that the
847 * attacker will not suspect his connection has been tarpitted.
848 * It will not cause trouble to the logs because we can exclude
849 * the tarpitted connections by filtering on the 'PT' status flags.
850 */
851 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
852
Christopher Faulet8dfeccf2020-05-15 14:16:29 +0200853 http_reply_and_close(s, txn->status, (!(req->flags & CF_READ_ERROR) ? http_error_message(s) : NULL));
Christopher Faulet5cb513a2020-05-13 17:56:56 +0200854
Christopher Faulete0768eb2018-10-03 16:38:02 +0200855 if (!(s->flags & SF_ERR_MASK))
856 s->flags |= SF_ERR_PRXCOND;
857 if (!(s->flags & SF_FINST_MASK))
858 s->flags |= SF_FINST_T;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100859
860 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200861 return 0;
862}
863
864/* This function is an analyser which waits for the HTTP request body. It waits
865 * for either the buffer to be full, or the full advertised contents to have
866 * reached the buffer. It must only be called after the standard HTTP request
867 * processing has occurred, because it expects the request to be parsed and will
868 * look for the Expect header. It may send a 100-Continue interim response. It
869 * takes in input any state starting from HTTP_MSG_BODY and leaves with one of
870 * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it
871 * needs to read more data, or 1 once it has completed its analysis.
872 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200873int http_wait_for_request_body(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200874{
875 struct session *sess = s->sess;
876 struct http_txn *txn = s->txn;
877 struct http_msg *msg = &s->txn->req;
878
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100879 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Fauletf76ebe82018-10-24 11:16:22 +0200880
Christopher Fauletf76ebe82018-10-24 11:16:22 +0200881
Christopher Faulet021a8e42021-03-29 10:46:38 +0200882 switch (http_wait_for_msg_body(s, req, s->be->timeout.httpreq, 0)) {
883 case HTTP_RULE_RES_CONT:
884 goto http_end;
885 case HTTP_RULE_RES_YIELD:
886 goto missing_data_or_waiting;
887 case HTTP_RULE_RES_BADREQ:
Willy Tarreau4236f032019-03-05 10:43:32 +0100888 goto return_bad_req;
Christopher Faulet021a8e42021-03-29 10:46:38 +0200889 case HTTP_RULE_RES_ERROR:
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200890 goto return_int_err;
Christopher Faulet021a8e42021-03-29 10:46:38 +0200891 case HTTP_RULE_RES_ABRT:
Christopher Fauletb8a53712019-12-16 11:29:38 +0100892 goto return_prx_cond;
Christopher Faulet021a8e42021-03-29 10:46:38 +0200893 default:
894 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200895 }
896
897 http_end:
898 /* The situation will not evolve, so let's give up on the analysis. */
899 s->logs.tv_request = now; /* update the request timer to reflect full request */
900 req->analysers &= ~an_bit;
901 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100902 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200903 return 1;
904
Christopher Faulet021a8e42021-03-29 10:46:38 +0200905 missing_data_or_waiting:
906 channel_dont_connect(req);
907 DBG_TRACE_DEVEL("waiting for more data",
908 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
909 return 0;
910
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200911 return_int_err:
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200912 txn->status = 500;
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200913 if (!(s->flags & SF_ERR_MASK))
914 s->flags |= SF_ERR_INTERNAL;
Willy Tarreau4781b152021-04-06 13:53:36 +0200915 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100916 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau4781b152021-04-06 13:53:36 +0200917 _HA_ATOMIC_INC(&s->be->be_counters.internal_errors);
William Lallemand36119de2021-03-08 15:26:48 +0100918 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200919 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Faulet021a8e42021-03-29 10:46:38 +0200920 goto return_prx_err;
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200921
Christopher Faulete0768eb2018-10-03 16:38:02 +0200922 return_bad_req: /* let's centralize all bad requests */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200923 txn->status = 400;
Willy Tarreau4781b152021-04-06 13:53:36 +0200924 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req);
William Lallemand36119de2021-03-08 15:26:48 +0100925 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200926 _HA_ATOMIC_INC(&sess->listener->counters->failed_req);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100927 /* fall through */
928
Christopher Faulet021a8e42021-03-29 10:46:38 +0200929 return_prx_err:
Christopher Fauletb8a53712019-12-16 11:29:38 +0100930 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet021a8e42021-03-29 10:46:38 +0200931 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200932
Christopher Faulet021a8e42021-03-29 10:46:38 +0200933 return_prx_cond:
Christopher Faulete0768eb2018-10-03 16:38:02 +0200934 if (!(s->flags & SF_ERR_MASK))
935 s->flags |= SF_ERR_PRXCOND;
936 if (!(s->flags & SF_FINST_MASK))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100937 s->flags |= (msg->msg_state < HTTP_MSG_DATA ? SF_FINST_R : SF_FINST_D);
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200938
Christopher Faulete0768eb2018-10-03 16:38:02 +0200939 req->analysers &= AN_REQ_FLT_END;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100940 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100941 DBG_TRACE_DEVEL("leaving on error",
942 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200943 return 0;
944}
945
946/* This function is an analyser which forwards request body (including chunk
947 * sizes if any). It is called as soon as we must forward, even if we forward
948 * zero byte. The only situation where it must not be called is when we're in
949 * tunnel mode and we want to forward till the close. It's used both to forward
950 * remaining data and to resync after end of body. It expects the msg_state to
951 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
952 * read more data, or 1 once we can go on with next request or end the stream.
953 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
954 * bytes of pending data + the headers if not already done.
955 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200956int http_request_forward_body(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200957{
958 struct session *sess = s->sess;
959 struct http_txn *txn = s->txn;
Christopher Faulet9768c262018-10-22 09:34:31 +0200960 struct http_msg *msg = &txn->req;
961 struct htx *htx;
Christopher Faulet93e02d82019-03-08 14:18:50 +0100962 short status = 0;
Christopher Fauletaed82cf2018-11-30 22:22:32 +0100963 int ret;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200964
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100965 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200966
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100967 htx = htxbuf(&req->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200968
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200969 if (htx->flags & HTX_FL_PARSING_ERROR)
970 goto return_bad_req;
971 if (htx->flags & HTX_FL_PROCESSING_ERROR)
972 goto return_int_err;
973
Christopher Faulete0768eb2018-10-03 16:38:02 +0200974 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
975 ((req->flags & CF_SHUTW) && (req->to_forward || co_data(req)))) {
976 /* Output closed while we were sending data. We must abort and
977 * wake the other side up.
Christopher Fauletf506d962021-04-27 10:56:28 +0200978 *
979 * If we have finished to send the request and the response is
980 * still in progress, don't catch write error on the request
981 * side if it is in fact a read error on the server side.
Christopher Faulete0768eb2018-10-03 16:38:02 +0200982 */
Christopher Fauletf506d962021-04-27 10:56:28 +0200983 if (msg->msg_state == HTTP_MSG_DONE && (s->res.flags & CF_READ_ERROR) && s->res.analysers)
984 return 0;
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200985
Olivier Houchard29cac3c2019-07-12 15:48:58 +0200986 /* Don't abort yet if we had L7 retries activated and it
987 * was a write error, we may recover.
988 */
989 if (!(req->flags & (CF_READ_ERROR | CF_READ_TIMEOUT)) &&
Christopher Fauleta6294472021-12-23 13:25:57 +0100990 (cs_si(s->csb)->flags & SI_FL_L7_RETRY)) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100991 DBG_TRACE_DEVEL("leaving on L7 retry",
992 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Olivier Houchard29cac3c2019-07-12 15:48:58 +0200993 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100994 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200995 msg->msg_state = HTTP_MSG_ERROR;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200996 http_end_request(s);
997 http_end_response(s);
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100998 DBG_TRACE_DEVEL("leaving on error",
999 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001000 return 1;
1001 }
1002
1003 /* Note that we don't have to send 100-continue back because we don't
1004 * need the data to complete our job, and it's up to the server to
1005 * decide whether to return 100, 417 or anything else in return of
1006 * an "Expect: 100-continue" header.
1007 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001008 if (msg->msg_state == HTTP_MSG_BODY)
1009 msg->msg_state = HTTP_MSG_DATA;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001010
Christopher Faulete0768eb2018-10-03 16:38:02 +02001011 /* in most states, we should abort in case of early close */
1012 channel_auto_close(req);
1013
1014 if (req->to_forward) {
Christopher Faulet66af0b22019-03-22 14:54:52 +01001015 if (req->to_forward == CHN_INFINITE_FORWARD) {
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001016 if (req->flags & CF_EOI)
1017 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet66af0b22019-03-22 14:54:52 +01001018 }
1019 else {
1020 /* We can't process the buffer's contents yet */
1021 req->flags |= CF_WAKE_WRITE;
1022 goto missing_data_or_waiting;
1023 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001024 }
1025
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001026 if (msg->msg_state >= HTTP_MSG_ENDING)
1027 goto ending;
1028
1029 if (txn->meth == HTTP_METH_CONNECT) {
1030 msg->msg_state = HTTP_MSG_ENDING;
1031 goto ending;
1032 }
1033
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001034 /* Forward input data. We get it by removing all outgoing data not
1035 * forwarded yet from HTX data size. If there are some data filters, we
1036 * let them decide the amount of data to forward.
Christopher Faulet9768c262018-10-22 09:34:31 +02001037 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001038 if (HAS_REQ_DATA_FILTERS(s)) {
1039 ret = flt_http_payload(s, msg, htx->data);
1040 if (ret < 0)
1041 goto return_bad_req;
Christopher Faulet421e7692019-06-13 11:16:45 +02001042 c_adv(req, ret);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001043 }
1044 else {
Christopher Faulet421e7692019-06-13 11:16:45 +02001045 c_adv(req, htx->data - co_data(req));
Christopher Faulet66af0b22019-03-22 14:54:52 +01001046 if (msg->flags & HTTP_MSGF_XFER_LEN)
1047 channel_htx_forward_forever(req, htx);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001048 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001049
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001050 if (htx->data != co_data(req))
1051 goto missing_data_or_waiting;
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001052
Christopher Faulet9768c262018-10-22 09:34:31 +02001053 /* Check if the end-of-message is reached and if so, switch the message
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001054 * in HTTP_MSG_ENDING state. Then if all data was marked to be
1055 * forwarded, set the state to HTTP_MSG_DONE.
Christopher Faulet9768c262018-10-22 09:34:31 +02001056 */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001057 if (!(htx->flags & HTX_FL_EOM))
Christopher Faulet9768c262018-10-22 09:34:31 +02001058 goto missing_data_or_waiting;
1059
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001060 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet9768c262018-10-22 09:34:31 +02001061
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001062 ending:
Christopher Faulet2151cdd2020-07-22 16:34:59 +02001063 req->flags &= ~CF_EXPECT_MORE; /* no more data are expected */
1064
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001065 /* other states, ENDING...TUNNEL */
1066 if (msg->msg_state >= HTTP_MSG_DONE)
1067 goto done;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001068
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001069 if (HAS_REQ_DATA_FILTERS(s)) {
1070 ret = flt_http_end(s, msg);
1071 if (ret <= 0) {
1072 if (!ret)
1073 goto missing_data_or_waiting;
1074 goto return_bad_req;
1075 }
1076 }
1077
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001078 if (txn->meth == HTTP_METH_CONNECT)
1079 msg->msg_state = HTTP_MSG_TUNNEL;
1080 else {
1081 msg->msg_state = HTTP_MSG_DONE;
1082 req->to_forward = 0;
1083 }
1084
1085 done:
1086 /* we don't want to forward closes on DONE except in tunnel mode. */
1087 if (!(txn->flags & TX_CON_WANT_TUN))
1088 channel_dont_close(req);
1089
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001090 http_end_request(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001091 if (!(req->analysers & an_bit)) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001092 http_end_response(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001093 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
1094 if (req->flags & CF_SHUTW) {
1095 /* request errors are most likely due to the
1096 * server aborting the transfer. */
Christopher Faulet93e02d82019-03-08 14:18:50 +01001097 goto return_srv_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001098 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001099 goto return_bad_req;
1100 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001101 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001102 return 1;
1103 }
1104
1105 /* If "option abortonclose" is set on the backend, we want to monitor
1106 * the client's connection and forward any shutdown notification to the
1107 * server, which will decide whether to close or to go on processing the
1108 * request. We only do that in tunnel mode, and not in other modes since
1109 * it can be abused to exhaust source ports. */
Christopher Faulet769d0e92019-03-22 14:23:18 +01001110 if (s->be->options & PR_O_ABRT_CLOSE) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001111 channel_auto_read(req);
Christopher Fauletc41547b2019-07-16 14:32:23 +02001112 if ((req->flags & (CF_SHUTR|CF_READ_NULL)) && !(txn->flags & TX_CON_WANT_TUN))
Christopher Fauleta6294472021-12-23 13:25:57 +01001113 cs_si(s->csb)->flags |= SI_FL_NOLINGER;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001114 channel_auto_close(req);
1115 }
1116 else if (s->txn->meth == HTTP_METH_POST) {
1117 /* POST requests may require to read extra CRLF sent by broken
1118 * browsers and which could cause an RST to be sent upon close
1119 * on some systems (eg: Linux). */
1120 channel_auto_read(req);
1121 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001122 DBG_TRACE_DEVEL("waiting for the end of the HTTP txn",
1123 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001124 return 0;
1125
1126 missing_data_or_waiting:
1127 /* stop waiting for data if the input is closed before the end */
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001128 if (msg->msg_state < HTTP_MSG_ENDING && req->flags & CF_SHUTR)
Christopher Faulet93e02d82019-03-08 14:18:50 +01001129 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001130
1131 waiting:
1132 /* waiting for the last bits to leave the buffer */
1133 if (req->flags & CF_SHUTW)
Christopher Faulet93e02d82019-03-08 14:18:50 +01001134 goto return_srv_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001135
1136 /* When TE: chunked is used, we need to get there again to parse remaining
1137 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
1138 * And when content-length is used, we never want to let the possible
1139 * shutdown be forwarded to the other side, as the state machine will
1140 * take care of it once the client responds. It's also important to
1141 * prevent TIME_WAITs from accumulating on the backend side, and for
1142 * HTTP/2 where the last frame comes with a shutdown.
1143 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001144 if (msg->flags & HTTP_MSGF_XFER_LEN)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001145 channel_dont_close(req);
1146
1147 /* We know that more data are expected, but we couldn't send more that
1148 * what we did. So we always set the CF_EXPECT_MORE flag so that the
1149 * system knows it must not set a PUSH on this first part. Interactive
1150 * modes are already handled by the stream sock layer. We must not do
1151 * this in content-length mode because it could present the MSG_MORE
1152 * flag with the last block of forwarded data, which would cause an
1153 * additional delay to be observed by the receiver.
1154 */
Christopher Faulet2151cdd2020-07-22 16:34:59 +02001155 if (HAS_REQ_DATA_FILTERS(s))
Christopher Faulete0768eb2018-10-03 16:38:02 +02001156 req->flags |= CF_EXPECT_MORE;
1157
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001158 DBG_TRACE_DEVEL("waiting for more data to forward",
1159 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001160 return 0;
1161
Christopher Faulet93e02d82019-03-08 14:18:50 +01001162 return_cli_abort:
Willy Tarreau4781b152021-04-06 13:53:36 +02001163 _HA_ATOMIC_INC(&sess->fe->fe_counters.cli_aborts);
1164 _HA_ATOMIC_INC(&s->be->be_counters.cli_aborts);
William Lallemand36119de2021-03-08 15:26:48 +01001165 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001166 _HA_ATOMIC_INC(&sess->listener->counters->cli_aborts);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001167 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001168 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.cli_aborts);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001169 if (!(s->flags & SF_ERR_MASK))
1170 s->flags |= SF_ERR_CLICL;
1171 status = 400;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001172 goto return_prx_cond;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001173
1174 return_srv_abort:
Willy Tarreau4781b152021-04-06 13:53:36 +02001175 _HA_ATOMIC_INC(&sess->fe->fe_counters.srv_aborts);
1176 _HA_ATOMIC_INC(&s->be->be_counters.srv_aborts);
William Lallemand36119de2021-03-08 15:26:48 +01001177 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001178 _HA_ATOMIC_INC(&sess->listener->counters->srv_aborts);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001179 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001180 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.srv_aborts);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001181 if (!(s->flags & SF_ERR_MASK))
1182 s->flags |= SF_ERR_SRVCL;
1183 status = 502;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001184 goto return_prx_cond;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001185
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001186 return_int_err:
1187 if (!(s->flags & SF_ERR_MASK))
1188 s->flags |= SF_ERR_INTERNAL;
Willy Tarreau4781b152021-04-06 13:53:36 +02001189 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
1190 _HA_ATOMIC_INC(&s->be->be_counters.internal_errors);
William Lallemand36119de2021-03-08 15:26:48 +01001191 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001192 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001193 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001194 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.internal_errors);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001195 status = 500;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001196 goto return_prx_cond;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001197
Christopher Faulet93e02d82019-03-08 14:18:50 +01001198 return_bad_req:
Willy Tarreau4781b152021-04-06 13:53:36 +02001199 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req);
William Lallemand36119de2021-03-08 15:26:48 +01001200 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001201 _HA_ATOMIC_INC(&sess->listener->counters->failed_req);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001202 status = 400;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001203 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001204
Christopher Fauletb8a53712019-12-16 11:29:38 +01001205 return_prx_cond:
Christopher Faulet9768c262018-10-22 09:34:31 +02001206 if (txn->status > 0) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001207 /* Note: we don't send any error if some data were already sent */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001208 http_reply_and_close(s, txn->status, NULL);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001209 } else {
Christopher Faulet93e02d82019-03-08 14:18:50 +01001210 txn->status = status;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001211 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001212 }
Christopher Fauletb8a53712019-12-16 11:29:38 +01001213 if (!(s->flags & SF_ERR_MASK))
1214 s->flags |= SF_ERR_PRXCOND;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001215 if (!(s->flags & SF_FINST_MASK))
1216 s->flags |= ((txn->rsp.msg_state < HTTP_MSG_ERROR) ? SF_FINST_H : SF_FINST_D);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001217 DBG_TRACE_DEVEL("leaving on error ",
1218 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001219 return 0;
1220}
1221
Olivier Houcharda254a372019-04-05 15:30:12 +02001222/* Reset the stream and the backend stream_interface to a situation suitable for attemption connection */
1223/* Returns 0 if we can attempt to retry, -1 otherwise */
1224static __inline int do_l7_retry(struct stream *s, struct stream_interface *si)
1225{
Christopher Faulet9f5382e2021-05-21 13:46:14 +02001226 struct channel *req, *res;
1227 int co_data;
Olivier Houcharda254a372019-04-05 15:30:12 +02001228
1229 si->conn_retries--;
1230 if (si->conn_retries < 0)
Christopher Faulet552601d2021-05-26 10:31:06 +02001231 return -1;
Christopher Faulet5b82cc52020-10-12 15:18:50 +02001232
Christopher Faulete763c8c2021-05-05 18:23:59 +02001233 if (objt_server(s->target)) {
1234 if (s->flags & SF_CURR_SESS) {
1235 s->flags &= ~SF_CURR_SESS;
1236 _HA_ATOMIC_DEC(&__objt_server(s->target)->cur_sess);
1237 }
Willy Tarreau4781b152021-04-06 13:53:36 +02001238 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.retries);
Christopher Faulete763c8c2021-05-05 18:23:59 +02001239 }
Willy Tarreau4781b152021-04-06 13:53:36 +02001240 _HA_ATOMIC_INC(&s->be->be_counters.retries);
Willy Tarreau223995e2019-05-04 10:38:31 +02001241
Christopher Faulet9f5382e2021-05-21 13:46:14 +02001242 req = &s->req;
1243 res = &s->res;
Olivier Houcharda254a372019-04-05 15:30:12 +02001244 /* Remove any write error from the request, and read error from the response */
1245 req->flags &= ~(CF_WRITE_ERROR | CF_WRITE_TIMEOUT | CF_SHUTW | CF_SHUTW_NOW);
1246 res->flags &= ~(CF_READ_ERROR | CF_READ_TIMEOUT | CF_SHUTR | CF_EOI | CF_READ_NULL | CF_SHUTR_NOW);
Christopher Faulet7bf46bb2022-01-04 10:56:03 +01001247 res->analysers &= AN_RES_FLT_END;
Olivier Houcharda254a372019-04-05 15:30:12 +02001248 si->flags &= ~(SI_FL_ERR | SI_FL_EXP | SI_FL_RXBLK_SHUT);
Christopher Faulet30aa0da2021-05-05 21:05:09 +02001249 si->err_type = SI_ET_NONE;
1250 s->flags &= ~(SF_ERR_MASK | SF_FINST_MASK);
Olivier Houchard4bd58672019-07-12 16:16:59 +02001251 stream_choose_redispatch(s);
Olivier Houcharda254a372019-04-05 15:30:12 +02001252 si->exp = TICK_ETERNITY;
1253 res->rex = TICK_ETERNITY;
1254 res->to_forward = 0;
1255 res->analyse_exp = TICK_ETERNITY;
1256 res->total = 0;
Christopher Fauletcda94ac2021-12-23 17:28:17 +01001257 cs_detach_endp(s->csb);
Olivier Houcharda254a372019-04-05 15:30:12 +02001258
Christopher Faulet9f5382e2021-05-21 13:46:14 +02001259 b_free(&req->buf);
1260 /* Swap the L7 buffer with the channel buffer */
1261 /* We know we stored the co_data as b_data, so get it there */
1262 co_data = b_data(&si->l7_buffer);
1263 b_set_data(&si->l7_buffer, b_size(&si->l7_buffer));
1264 b_xfer(&req->buf, &si->l7_buffer, b_data(&si->l7_buffer));
1265 co_set_data(req, co_data);
Christopher Faulet5b82cc52020-10-12 15:18:50 +02001266
Ilya Shipitsinacf84592021-02-06 22:29:08 +05001267 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 +02001268
Olivier Houcharda254a372019-04-05 15:30:12 +02001269 b_reset(&res->buf);
1270 co_set_data(res, 0);
1271 return 0;
1272}
1273
Christopher Faulete0768eb2018-10-03 16:38:02 +02001274/* This stream analyser waits for a complete HTTP response. It returns 1 if the
1275 * processing can continue on next analysers, or zero if it either needs more
1276 * data or wants to immediately abort the response (eg: timeout, error, ...). It
1277 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->res.analysers
1278 * when it has nothing left to do, and may remove any analyser when it wants to
1279 * abort.
1280 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001281int http_wait_for_response(struct stream *s, struct channel *rep, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001282{
Christopher Faulet9768c262018-10-22 09:34:31 +02001283 /*
1284 * We will analyze a complete HTTP response to check the its syntax.
1285 *
1286 * Once the start line and all headers are received, we may perform a
1287 * capture of the error (if any), and we will set a few fields. We also
1288 * logging and finally headers capture.
1289 */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001290 struct session *sess = s->sess;
1291 struct http_txn *txn = s->txn;
1292 struct http_msg *msg = &txn->rsp;
Christopher Faulet9768c262018-10-22 09:34:31 +02001293 struct htx *htx;
Christopher Fauleta6294472021-12-23 13:25:57 +01001294 struct stream_interface *si_b = cs_si(s->csb);
Christopher Faulet61608322018-11-23 16:23:45 +01001295 struct connection *srv_conn;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001296 struct htx_sl *sl;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001297 int n;
1298
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001299 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001300
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001301 htx = htxbuf(&rep->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001302
Willy Tarreau4236f032019-03-05 10:43:32 +01001303 /* Parsing errors are caught here */
1304 if (htx->flags & HTX_FL_PARSING_ERROR)
1305 goto return_bad_res;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001306 if (htx->flags & HTX_FL_PROCESSING_ERROR)
1307 goto return_int_err;
Willy Tarreau4236f032019-03-05 10:43:32 +01001308
Christopher Faulete0768eb2018-10-03 16:38:02 +02001309 /*
1310 * Now we quickly check if we have found a full valid response.
1311 * If not so, we check the FD and buffer states before leaving.
1312 * A full response is indicated by the fact that we have seen
1313 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
1314 * responses are checked first.
1315 *
1316 * Depending on whether the client is still there or not, we
1317 * may send an error response back or not. Note that normally
1318 * we should only check for HTTP status there, and check I/O
1319 * errors somewhere else.
1320 */
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001321 next_one:
Christopher Faulet29f17582019-05-23 11:03:26 +02001322 if (unlikely(htx_is_empty(htx) || htx->first == -1)) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001323 /* 1: have we encountered a read error ? */
1324 if (rep->flags & CF_READ_ERROR) {
Christopher Faulet95a61e82021-12-22 14:22:03 +01001325 struct connection *conn = cs_conn(s->csb);
Olivier Houchard865d8392019-05-03 22:46:27 +02001326
Christopher Fauletd9769232021-05-26 12:15:37 +02001327 /* Perform a L7 retry because server refuses the early data. */
1328 if ((si_b->flags & SI_FL_L7_RETRY) &&
1329 (s->be->retry_type & PR_RE_EARLY_ERROR) &&
1330 conn && conn->err_code == CO_ER_SSL_EARLY_FAILED &&
1331 do_l7_retry(s, si_b) == 0) {
1332 DBG_TRACE_DEVEL("leaving on L7 retry",
1333 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
1334 return 0;
1335 }
1336
Olivier Houchard6db16992019-05-17 15:40:49 +02001337 if (txn->flags & TX_NOT_FIRST)
1338 goto abort_keep_alive;
1339
Willy Tarreau4781b152021-04-06 13:53:36 +02001340 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001341 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001342 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001343 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001344 }
1345
Christopher Fauletd9769232021-05-26 12:15:37 +02001346 /* if the server refused the early data, just send a 425 */
1347 if (conn && conn->err_code == CO_ER_SSL_EARLY_FAILED)
Olivier Houchard865d8392019-05-03 22:46:27 +02001348 txn->status = 425;
Christopher Fauletd9769232021-05-26 12:15:37 +02001349 else {
1350 txn->status = 502;
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001351 stream_inc_http_fail_ctr(s);
Christopher Fauletd9769232021-05-26 12:15:37 +02001352 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001353
Christopher Fauleta6294472021-12-23 13:25:57 +01001354 si_b->flags |= SI_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001355 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001356
1357 if (!(s->flags & SF_ERR_MASK))
1358 s->flags |= SF_ERR_SRVCL;
1359 if (!(s->flags & SF_FINST_MASK))
1360 s->flags |= SF_FINST_H;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001361 DBG_TRACE_DEVEL("leaving on error",
1362 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001363 return 0;
1364 }
1365
Christopher Faulet9768c262018-10-22 09:34:31 +02001366 /* 2: read timeout : return a 504 to the client. */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001367 else if (rep->flags & CF_READ_TIMEOUT) {
Olivier Houcharda254a372019-04-05 15:30:12 +02001368 if ((si_b->flags & SI_FL_L7_RETRY) &&
1369 (s->be->retry_type & PR_RE_TIMEOUT)) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001370 if (co_data(rep) || do_l7_retry(s, si_b) == 0) {
1371 DBG_TRACE_DEVEL("leaving on L7 retry",
1372 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharda254a372019-04-05 15:30:12 +02001373 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001374 }
Olivier Houcharda254a372019-04-05 15:30:12 +02001375 }
Willy Tarreau4781b152021-04-06 13:53:36 +02001376 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001377 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001378 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001379 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001380 }
1381
Christopher Faulete0768eb2018-10-03 16:38:02 +02001382 txn->status = 504;
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001383 stream_inc_http_fail_ctr(s);
Christopher Fauleta6294472021-12-23 13:25:57 +01001384 si_b->flags |= SI_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001385 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001386
1387 if (!(s->flags & SF_ERR_MASK))
1388 s->flags |= SF_ERR_SRVTO;
1389 if (!(s->flags & SF_FINST_MASK))
1390 s->flags |= SF_FINST_H;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001391 DBG_TRACE_DEVEL("leaving on error",
1392 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001393 return 0;
1394 }
1395
Christopher Faulet9768c262018-10-22 09:34:31 +02001396 /* 3: client abort with an abortonclose */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001397 else if ((rep->flags & CF_SHUTR) && ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001398 _HA_ATOMIC_INC(&sess->fe->fe_counters.cli_aborts);
1399 _HA_ATOMIC_INC(&s->be->be_counters.cli_aborts);
William Lallemand36119de2021-03-08 15:26:48 +01001400 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001401 _HA_ATOMIC_INC(&sess->listener->counters->cli_aborts);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001402 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001403 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.cli_aborts);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001404
Christopher Faulete0768eb2018-10-03 16:38:02 +02001405 txn->status = 400;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001406 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001407
1408 if (!(s->flags & SF_ERR_MASK))
1409 s->flags |= SF_ERR_CLICL;
1410 if (!(s->flags & SF_FINST_MASK))
1411 s->flags |= SF_FINST_H;
1412
1413 /* process_stream() will take care of the error */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001414 DBG_TRACE_DEVEL("leaving on error",
1415 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001416 return 0;
1417 }
1418
Christopher Faulet9768c262018-10-22 09:34:31 +02001419 /* 4: close from server, capture the response if the server has started to respond */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001420 else if (rep->flags & CF_SHUTR) {
Olivier Houcharda254a372019-04-05 15:30:12 +02001421 if ((si_b->flags & SI_FL_L7_RETRY) &&
1422 (s->be->retry_type & PR_RE_DISCONNECTED)) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001423 if (co_data(rep) || do_l7_retry(s, si_b) == 0) {
1424 DBG_TRACE_DEVEL("leaving on L7 retry",
1425 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharda254a372019-04-05 15:30:12 +02001426 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001427 }
Olivier Houcharda254a372019-04-05 15:30:12 +02001428 }
1429
Olivier Houchard6db16992019-05-17 15:40:49 +02001430 if (txn->flags & TX_NOT_FIRST)
1431 goto abort_keep_alive;
1432
Willy Tarreau4781b152021-04-06 13:53:36 +02001433 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001434 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001435 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001436 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001437 }
1438
Christopher Faulete0768eb2018-10-03 16:38:02 +02001439 txn->status = 502;
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001440 stream_inc_http_fail_ctr(s);
Christopher Fauleta6294472021-12-23 13:25:57 +01001441 si_b->flags |= SI_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001442 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001443
1444 if (!(s->flags & SF_ERR_MASK))
1445 s->flags |= SF_ERR_SRVCL;
1446 if (!(s->flags & SF_FINST_MASK))
1447 s->flags |= SF_FINST_H;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001448 DBG_TRACE_DEVEL("leaving on error",
1449 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001450 return 0;
1451 }
1452
Christopher Faulet9768c262018-10-22 09:34:31 +02001453 /* 5: write error to client (we don't send any message then) */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001454 else if (rep->flags & CF_WRITE_ERROR) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001455 if (txn->flags & TX_NOT_FIRST)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001456 goto abort_keep_alive;
1457
Willy Tarreau4781b152021-04-06 13:53:36 +02001458 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001459 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001460 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001461 rep->analysers &= AN_RES_FLT_END;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001462
1463 if (!(s->flags & SF_ERR_MASK))
1464 s->flags |= SF_ERR_CLICL;
1465 if (!(s->flags & SF_FINST_MASK))
1466 s->flags |= SF_FINST_H;
1467
1468 /* process_stream() will take care of the error */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001469 DBG_TRACE_DEVEL("leaving on error",
1470 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001471 return 0;
1472 }
1473
1474 channel_dont_close(rep);
1475 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001476 DBG_TRACE_DEVEL("waiting for more data",
1477 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001478 return 0;
1479 }
1480
1481 /* More interesting part now : we know that we have a complete
1482 * response which at least looks like HTTP. We have an indicator
1483 * of each header's length, so we can parse them quickly.
1484 */
Christopher Faulet29f17582019-05-23 11:03:26 +02001485 BUG_ON(htx_get_first_type(htx) != HTX_BLK_RES_SL);
Christopher Faulet297fbb42019-05-13 14:41:27 +02001486 sl = http_get_stline(htx);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001487
Christopher Faulet1f08bff2021-05-26 13:14:39 +02001488 /* Perform a L7 retry because of the status code */
1489 if ((si_b->flags & SI_FL_L7_RETRY) &&
1490 l7_status_match(s->be, sl->info.res.status) &&
1491 do_l7_retry(s, si_b) == 0) {
1492 DBG_TRACE_DEVEL("leaving on L7 retry", STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
1493 return 0;
1494 }
1495
1496 /* Now, L7 buffer is useless, it can be released */
Christopher Fauleta6294472021-12-23 13:25:57 +01001497 b_free(&(cs_si(s->csb)->l7_buffer));
Christopher Faulet1f08bff2021-05-26 13:14:39 +02001498
1499 msg->msg_state = HTTP_MSG_BODY;
1500
1501
Christopher Faulet9768c262018-10-22 09:34:31 +02001502 /* 0: we might have to print this header in debug mode */
1503 if (unlikely((global.mode & MODE_DEBUG) &&
1504 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
1505 int32_t pos;
1506
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001507 http_debug_stline("srvrep", s, sl);
Christopher Faulet9768c262018-10-22 09:34:31 +02001508
Christopher Fauleta3f15502019-05-13 15:27:23 +02001509 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001510 struct htx_blk *blk = htx_get_blk(htx, pos);
1511 enum htx_blk_type type = htx_get_blk_type(blk);
1512
1513 if (type == HTX_BLK_EOH)
1514 break;
1515 if (type != HTX_BLK_HDR)
1516 continue;
1517
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001518 http_debug_hdr("srvhdr", s,
1519 htx_get_blk_name(htx, blk),
1520 htx_get_blk_value(htx, blk));
Christopher Faulet9768c262018-10-22 09:34:31 +02001521 }
1522 }
1523
Christopher Faulet03599112018-11-27 11:21:21 +01001524 /* 1: get the status code and the version. Also set HTTP flags */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001525 txn->status = sl->info.res.status;
Christopher Faulet03599112018-11-27 11:21:21 +01001526 if (sl->flags & HTX_SL_F_VER_11)
Christopher Faulet9768c262018-10-22 09:34:31 +02001527 msg->flags |= HTTP_MSGF_VER_11;
Christopher Faulet03599112018-11-27 11:21:21 +01001528 if (sl->flags & HTX_SL_F_XFER_LEN) {
1529 msg->flags |= HTTP_MSGF_XFER_LEN;
Christopher Faulet2a408542020-11-20 14:22:37 +01001530 if (sl->flags & HTX_SL_F_CLEN)
1531 msg->flags |= HTTP_MSGF_CNT_LEN;
1532 else if (sl->flags & HTX_SL_F_CHNK)
1533 msg->flags |= HTTP_MSGF_TE_CHNK;
Christopher Faulet03599112018-11-27 11:21:21 +01001534 }
Christopher Faulet2a408542020-11-20 14:22:37 +01001535 if (sl->flags & HTX_SL_F_BODYLESS)
1536 msg->flags |= HTTP_MSGF_BODYLESS;
Christopher Faulet576c3582021-01-08 15:53:01 +01001537 if (sl->flags & HTX_SL_F_CONN_UPG)
1538 msg->flags |= HTTP_MSGF_CONN_UPG;
Christopher Faulet9768c262018-10-22 09:34:31 +02001539
1540 n = txn->status / 100;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001541 if (n < 1 || n > 5)
1542 n = 0;
Christopher Faulet9768c262018-10-22 09:34:31 +02001543
Christopher Faulete0768eb2018-10-03 16:38:02 +02001544 /* when the client triggers a 4xx from the server, it's most often due
1545 * to a missing object or permission. These events should be tracked
1546 * because if they happen often, it may indicate a brute force or a
1547 * vulnerability scan.
1548 */
1549 if (n == 4)
1550 stream_inc_http_err_ctr(s);
1551
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001552 if (n == 5 && txn->status != 501 && txn->status != 505)
1553 stream_inc_http_fail_ctr(s);
1554
Marcin Deranek3c27dda2020-05-15 18:32:51 +02001555 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001556 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.p.http.rsp[n]);
1557 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.p.http.cum_req);
Marcin Deranek3c27dda2020-05-15 18:32:51 +02001558 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001559
Christopher Faulete0768eb2018-10-03 16:38:02 +02001560 /* Adjust server's health based on status code. Note: status codes 501
1561 * and 505 are triggered on demand by client request, so we must not
1562 * count them as server failures.
1563 */
1564 if (objt_server(s->target)) {
1565 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001566 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_OK);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001567 else
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001568 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_STS);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001569 }
1570
1571 /*
1572 * We may be facing a 100-continue response, or any other informational
1573 * 1xx response which is non-final, in which case this is not the right
1574 * response, and we're waiting for the next one. Let's allow this response
1575 * to go to the client and wait for the next one. There's an exception for
1576 * 101 which is used later in the code to switch protocols.
1577 */
1578 if (txn->status < 200 &&
1579 (txn->status == 100 || txn->status >= 102)) {
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001580 FLT_STRM_CB(s, flt_http_reset(s, msg));
Christopher Faulet421e7692019-06-13 11:16:45 +02001581 htx->first = channel_htx_fwd_headers(rep, htx);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001582 msg->msg_state = HTTP_MSG_RPBEFORE;
Christopher Faulet3499f622019-09-03 15:23:54 +02001583 msg->flags = 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001584 txn->status = 0;
1585 s->logs.t_data = -1; /* was not a response yet */
Christopher Faulet7d518452020-08-31 11:07:07 +02001586 rep->flags |= CF_SEND_DONTWAIT; /* Send ASAP informational messages */
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001587 goto next_one;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001588 }
1589
Christopher Faulet6e6c7b12021-01-08 16:02:05 +01001590 /* A 101-switching-protocols must contains a Connection header with the
1591 * "upgrade" option and the request too. It means both are agree to
1592 * upgrade. It is not so strict because there is no test on the Upgrade
1593 * header content. But it is probably stronger enough for now.
1594 */
1595 if (txn->status == 101 &&
1596 (!(txn->req.flags & HTTP_MSGF_CONN_UPG) || !(txn->rsp.flags & HTTP_MSGF_CONN_UPG)))
1597 goto return_bad_res;
1598
Christopher Faulete0768eb2018-10-03 16:38:02 +02001599 /*
1600 * 2: check for cacheability.
1601 */
1602
1603 switch (txn->status) {
1604 case 200:
1605 case 203:
1606 case 204:
1607 case 206:
1608 case 300:
1609 case 301:
1610 case 404:
1611 case 405:
1612 case 410:
1613 case 414:
1614 case 501:
1615 break;
1616 default:
1617 /* RFC7231#6.1:
1618 * Responses with status codes that are defined as
1619 * cacheable by default (e.g., 200, 203, 204, 206,
1620 * 300, 301, 404, 405, 410, 414, and 501 in this
1621 * specification) can be reused by a cache with
1622 * heuristic expiration unless otherwise indicated
1623 * by the method definition or explicit cache
1624 * controls [RFC7234]; all other status codes are
1625 * not cacheable by default.
1626 */
1627 txn->flags &= ~(TX_CACHEABLE | TX_CACHE_COOK);
1628 break;
1629 }
1630
1631 /*
1632 * 3: we may need to capture headers
1633 */
1634 s->logs.logwait &= ~LW_RESP;
1635 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->res_cap))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001636 http_capture_headers(htx, s->res_cap, sess->fe->rsp_cap);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001637
Christopher Faulet9768c262018-10-22 09:34:31 +02001638 /* Skip parsing if no content length is possible. */
Christopher Fauletc75668e2020-12-07 18:10:32 +01001639 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status >= 200 && txn->status < 300) ||
Christopher Faulete0768eb2018-10-03 16:38:02 +02001640 txn->status == 101)) {
1641 /* Either we've established an explicit tunnel, or we're
1642 * switching the protocol. In both cases, we're very unlikely
1643 * to understand the next protocols. We have to switch to tunnel
1644 * mode, so that we transfer the request and responses then let
1645 * this protocol pass unmodified. When we later implement specific
1646 * parsers for such protocols, we'll want to check the Upgrade
1647 * header which contains information about that protocol for
1648 * responses with status 101 (eg: see RFC2817 about TLS).
1649 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02001650 txn->flags |= TX_CON_WANT_TUN;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001651 }
1652
Christopher Faulet61608322018-11-23 16:23:45 +01001653 /* check for NTML authentication headers in 401 (WWW-Authenticate) and
1654 * 407 (Proxy-Authenticate) responses and set the connection to private
1655 */
Christopher Faulet95a61e82021-12-22 14:22:03 +01001656 srv_conn = cs_conn(s->csb);
Christopher Faulet61608322018-11-23 16:23:45 +01001657 if (srv_conn) {
1658 struct ist hdr;
1659 struct http_hdr_ctx ctx;
1660
1661 if (txn->status == 401)
1662 hdr = ist("WWW-Authenticate");
1663 else if (txn->status == 407)
1664 hdr = ist("Proxy-Authenticate");
1665 else
1666 goto end;
1667
1668 ctx.blk = NULL;
1669 while (http_find_header(htx, hdr, &ctx, 0)) {
Willy Tarreauf1dcced2020-05-07 19:27:02 +02001670 /* If www-authenticate contains "Negotiate", "Nego2", or "NTLM",
1671 * possibly followed by blanks and a base64 string, the connection
1672 * is private. Since it's a mess to deal with, we only check for
1673 * values starting with "NTLM" or "Nego". Note that often multiple
1674 * headers are sent by the server there.
1675 */
1676 if ((ctx.value.len >= 4 && strncasecmp(ctx.value.ptr, "Nego", 4) == 0) ||
Willy Tarreau49a1d282020-05-07 19:10:15 +02001677 (ctx.value.len >= 4 && strncasecmp(ctx.value.ptr, "NTLM", 4) == 0)) {
Olivier Houchard250031e2019-05-29 15:01:50 +02001678 sess->flags |= SESS_FL_PREFER_LAST;
Christopher Faulet08016ab2020-07-01 16:10:06 +02001679 conn_set_owner(srv_conn, sess, NULL);
Christopher Faulet21ddc742020-07-01 15:26:14 +02001680 conn_set_private(srv_conn);
Ilya Shipitsin6b79f382020-07-23 00:32:55 +05001681 /* If it fail now, the same will be done in mux->detach() callback */
Christopher Faulet08016ab2020-07-01 16:10:06 +02001682 session_add_conn(srv_conn->owner, srv_conn, srv_conn->target);
Willy Tarreauf1dcced2020-05-07 19:27:02 +02001683 break;
Olivier Houchard250031e2019-05-29 15:01:50 +02001684 }
Christopher Faulet61608322018-11-23 16:23:45 +01001685 }
1686 }
1687
1688 end:
Christopher Faulete0768eb2018-10-03 16:38:02 +02001689 /* we want to have the response time before we start processing it */
1690 s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now);
1691
1692 /* end of job, return OK */
1693 rep->analysers &= ~an_bit;
1694 rep->analyse_exp = TICK_ETERNITY;
1695 channel_auto_close(rep);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001696 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001697 return 1;
1698
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001699 return_int_err:
Willy Tarreau4781b152021-04-06 13:53:36 +02001700 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
1701 _HA_ATOMIC_INC(&s->be->be_counters.internal_errors);
William Lallemand36119de2021-03-08 15:26:48 +01001702 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001703 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Fauletb8a53712019-12-16 11:29:38 +01001704 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001705 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.internal_errors);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001706 txn->status = 500;
1707 if (!(s->flags & SF_ERR_MASK))
1708 s->flags |= SF_ERR_INTERNAL;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001709 goto return_prx_cond;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001710
1711 return_bad_res:
Willy Tarreau4781b152021-04-06 13:53:36 +02001712 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Faulet47365272018-10-31 17:40:50 +01001713 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001714 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001715 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Christopher Faulet47365272018-10-31 17:40:50 +01001716 }
Olivier Houcharde3249a92019-05-03 23:01:47 +02001717 if ((s->be->retry_type & PR_RE_JUNK_REQUEST) &&
Olivier Houchardad26d8d2019-05-10 17:48:28 +02001718 (si_b->flags & SI_FL_L7_RETRY) &&
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001719 do_l7_retry(s, si_b) == 0) {
1720 DBG_TRACE_DEVEL("leaving on L7 retry",
1721 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharde3249a92019-05-03 23:01:47 +02001722 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001723 }
Christopher Faulet47365272018-10-31 17:40:50 +01001724 txn->status = 502;
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001725 stream_inc_http_fail_ctr(s);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001726 /* fall through */
1727
Christopher Fauletb8a53712019-12-16 11:29:38 +01001728 return_prx_cond:
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001729 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet47365272018-10-31 17:40:50 +01001730
1731 if (!(s->flags & SF_ERR_MASK))
1732 s->flags |= SF_ERR_PRXCOND;
1733 if (!(s->flags & SF_FINST_MASK))
1734 s->flags |= SF_FINST_H;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001735
Christopher Fauleta6294472021-12-23 13:25:57 +01001736 si_b->flags |= SI_FL_NOLINGER;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001737 DBG_TRACE_DEVEL("leaving on error",
1738 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulet47365272018-10-31 17:40:50 +01001739 return 0;
1740
Christopher Faulete0768eb2018-10-03 16:38:02 +02001741 abort_keep_alive:
1742 /* A keep-alive request to the server failed on a network error.
1743 * The client is required to retry. We need to close without returning
1744 * any other information so that the client retries.
1745 */
1746 txn->status = 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001747 s->logs.logwait = 0;
1748 s->logs.level = 0;
1749 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001750 http_reply_and_close(s, txn->status, NULL);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001751 DBG_TRACE_DEVEL("leaving by closing K/A connection",
1752 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001753 return 0;
1754}
1755
1756/* This function performs all the processing enabled for the current response.
1757 * It normally returns 1 unless it wants to break. It relies on buffers flags,
1758 * and updates s->res.analysers. It might make sense to explode it into several
1759 * other functions. It works like process_request (see indications above).
1760 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001761int http_process_res_common(struct stream *s, struct channel *rep, int an_bit, struct proxy *px)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001762{
1763 struct session *sess = s->sess;
1764 struct http_txn *txn = s->txn;
1765 struct http_msg *msg = &txn->rsp;
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001766 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001767 struct proxy *cur_proxy;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001768 enum rule_result ret = HTTP_RULE_RES_CONT;
1769
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001770 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
1771 return 0;
Christopher Faulet9768c262018-10-22 09:34:31 +02001772
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001773 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001774
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001775 htx = htxbuf(&rep->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001776
1777 /* The stats applet needs to adjust the Connection header but we don't
1778 * apply any filter there.
1779 */
1780 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
1781 rep->analysers &= ~an_bit;
1782 rep->analyse_exp = TICK_ETERNITY;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001783 goto end;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001784 }
1785
1786 /*
1787 * We will have to evaluate the filters.
1788 * As opposed to version 1.2, now they will be evaluated in the
1789 * filters order and not in the header order. This means that
1790 * each filter has to be validated among all headers.
1791 *
1792 * Filters are tried with ->be first, then with ->fe if it is
1793 * different from ->be.
1794 *
1795 * Maybe we are in resume condiion. In this case I choose the
1796 * "struct proxy" which contains the rule list matching the resume
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05001797 * pointer. If none of these "struct proxy" match, I initialise
Christopher Faulete0768eb2018-10-03 16:38:02 +02001798 * the process with the first one.
1799 *
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05001800 * In fact, I check only correspondence between the current list
Christopher Faulete0768eb2018-10-03 16:38:02 +02001801 * pointer and the ->fe rule list. If it doesn't match, I initialize
1802 * the loop with the ->be.
1803 */
Christopher Fauletd4150ad2021-10-13 15:35:55 +02001804 if (s->current_rule_list == &sess->fe->http_res_rules ||
1805 (sess->fe->defpx && s->current_rule_list == &sess->fe->defpx->http_res_rules))
Christopher Faulete0768eb2018-10-03 16:38:02 +02001806 cur_proxy = sess->fe;
1807 else
1808 cur_proxy = s->be;
Christopher Fauletd4150ad2021-10-13 15:35:55 +02001809
Christopher Faulete0768eb2018-10-03 16:38:02 +02001810 while (1) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001811 /* evaluate http-response rules */
Christopher Faulet46f46df2021-11-09 16:33:25 +01001812 if (ret == HTTP_RULE_RES_CONT || ret == HTTP_RULE_RES_STOP) {
Christopher Fauletd4150ad2021-10-13 15:35:55 +02001813 struct list *def_rules, *rules;
1814
1815 def_rules = ((cur_proxy->defpx && (cur_proxy == s->be || cur_proxy->defpx != s->be->defpx)) ? &cur_proxy->defpx->http_res_rules : NULL);
1816 rules = &cur_proxy->http_res_rules;
1817
1818 ret = http_res_get_intercept_rule(cur_proxy, def_rules, rules, s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001819
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001820 switch (ret) {
1821 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
1822 goto return_prx_yield;
1823
1824 case HTTP_RULE_RES_CONT:
1825 case HTTP_RULE_RES_STOP: /* nothing to do */
1826 break;
1827
1828 case HTTP_RULE_RES_DENY: /* deny or tarpit */
1829 goto deny;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001830
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001831 case HTTP_RULE_RES_ABRT: /* abort request, response already sent */
1832 goto return_prx_cond;
1833
1834 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
Christopher Fauletb8a53712019-12-16 11:29:38 +01001835 goto done;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001836
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001837 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
1838 goto return_bad_res;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001839
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001840 case HTTP_RULE_RES_ERROR: /* failed with a bad request */
1841 goto return_int_err;
1842 }
1843
1844 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001845
Christopher Faulete0768eb2018-10-03 16:38:02 +02001846 /* check whether we're already working on the frontend */
1847 if (cur_proxy == sess->fe)
1848 break;
1849 cur_proxy = sess->fe;
1850 }
1851
Christopher Faulete0768eb2018-10-03 16:38:02 +02001852 /* OK that's all we can do for 1xx responses */
1853 if (unlikely(txn->status < 200 && txn->status != 101))
Christopher Fauletf2824e62018-10-01 12:12:37 +02001854 goto end;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001855
1856 /*
1857 * Now check for a server cookie.
1858 */
1859 if (s->be->cookie_name || sess->fe->capture_name || (s->be->options & PR_O_CHK_CACHE))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001860 http_manage_server_side_cookies(s, rep);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001861
1862 /*
1863 * Check for cache-control or pragma headers if required.
1864 */
1865 if ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001866 http_check_response_for_cacheability(s, rep);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001867
1868 /*
1869 * Add server cookie in the response if needed
1870 */
1871 if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) &&
1872 !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) &&
1873 (!(s->flags & SF_DIRECT) ||
1874 ((s->be->cookie_maxidle || txn->cookie_last_date) &&
1875 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
1876 (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
1877 (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
1878 (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
1879 !(s->flags & SF_IGNORE_PRST)) {
1880 /* the server is known, it's not the one the client requested, or the
1881 * cookie's last seen date needs to be refreshed. We have to
1882 * insert a set-cookie here, except if we want to insert only on POST
1883 * requests and this one isn't. Note that servers which don't have cookies
1884 * (eg: some backup servers) will return a full cookie removal request.
1885 */
Willy Tarreau88bc8002021-12-06 07:01:02 +00001886 if (!__objt_server(s->target)->cookie) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001887 chunk_printf(&trash,
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001888 "%s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
Christopher Faulete0768eb2018-10-03 16:38:02 +02001889 s->be->cookie_name);
1890 }
1891 else {
Willy Tarreau88bc8002021-12-06 07:01:02 +00001892 chunk_printf(&trash, "%s=%s", s->be->cookie_name, __objt_server(s->target)->cookie);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001893
1894 if (s->be->cookie_maxidle || s->be->cookie_maxlife) {
1895 /* emit last_date, which is mandatory */
1896 trash.area[trash.data++] = COOKIE_DELIM_DATE;
1897 s30tob64((date.tv_sec+3) >> 2,
1898 trash.area + trash.data);
1899 trash.data += 5;
1900
1901 if (s->be->cookie_maxlife) {
1902 /* emit first_date, which is either the original one or
1903 * the current date.
1904 */
1905 trash.area[trash.data++] = COOKIE_DELIM_DATE;
1906 s30tob64(txn->cookie_first_date ?
1907 txn->cookie_first_date >> 2 :
1908 (date.tv_sec+3) >> 2,
1909 trash.area + trash.data);
1910 trash.data += 5;
1911 }
1912 }
1913 chunk_appendf(&trash, "; path=/");
1914 }
1915
1916 if (s->be->cookie_domain)
1917 chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain);
1918
1919 if (s->be->ck_opts & PR_CK_HTTPONLY)
1920 chunk_appendf(&trash, "; HttpOnly");
1921
1922 if (s->be->ck_opts & PR_CK_SECURE)
1923 chunk_appendf(&trash, "; Secure");
1924
Christopher Faulet2f533902020-01-21 11:06:48 +01001925 if (s->be->cookie_attrs)
1926 chunk_appendf(&trash, "; %s", s->be->cookie_attrs);
1927
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001928 if (unlikely(!http_add_header(htx, ist("Set-Cookie"), ist2(trash.area, trash.data))))
Christopher Fauletb8a53712019-12-16 11:29:38 +01001929 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001930
1931 txn->flags &= ~TX_SCK_MASK;
1932 if (__objt_server(s->target)->cookie && (s->flags & SF_DIRECT))
1933 /* the server did not change, only the date was updated */
1934 txn->flags |= TX_SCK_UPDATED;
1935 else
1936 txn->flags |= TX_SCK_INSERTED;
1937
1938 /* Here, we will tell an eventual cache on the client side that we don't
1939 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
1940 * Some caches understand the correct form: 'no-cache="set-cookie"', but
1941 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
1942 */
1943 if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
1944
1945 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
1946
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001947 if (unlikely(!http_add_header(htx, ist("Cache-control"), ist("private"))))
Christopher Fauletb8a53712019-12-16 11:29:38 +01001948 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001949 }
1950 }
1951
1952 /*
1953 * Check if result will be cacheable with a cookie.
1954 * We'll block the response if security checks have caught
1955 * nasty things such as a cacheable cookie.
1956 */
1957 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
1958 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
1959 (s->be->options & PR_O_CHK_CACHE)) {
1960 /* we're in presence of a cacheable response containing
1961 * a set-cookie header. We'll block it as requested by
1962 * the 'checkcache' option, and send an alert.
1963 */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001964 ha_alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreau88bc8002021-12-06 07:01:02 +00001965 s->be->id, objt_server(s->target) ? __objt_server(s->target)->id : "<dispatch>");
Christopher Faulete0768eb2018-10-03 16:38:02 +02001966 send_log(s->be, LOG_ALERT,
1967 "Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreau88bc8002021-12-06 07:01:02 +00001968 s->be->id, objt_server(s->target) ? __objt_server(s->target)->id : "<dispatch>");
Christopher Fauletb8a53712019-12-16 11:29:38 +01001969 goto deny;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001970 }
1971
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001972 end:
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01001973 /*
1974 * Evaluate after-response rules before forwarding the response. rules
1975 * from the backend are evaluated first, then one from the frontend if
1976 * it differs.
1977 */
1978 if (!http_eval_after_res_rules(s))
1979 goto return_int_err;
1980
Christopher Fauletc2ac5e42021-03-08 18:20:09 +01001981 /* Filter the response headers if there are filters attached to the
1982 * stream.
1983 */
1984 if (HAS_FILTERS(s))
1985 rep->analysers |= AN_RES_FLT_HTTP_HDRS;
1986
Christopher Faulete0768eb2018-10-03 16:38:02 +02001987 /* Always enter in the body analyzer */
1988 rep->analysers &= ~AN_RES_FLT_XFER_DATA;
1989 rep->analysers |= AN_RES_HTTP_XFER_BODY;
1990
1991 /* if the user wants to log as soon as possible, without counting
1992 * bytes from the server, then this is the right moment. We have
1993 * to temporarily assign bytes_out to log what we currently have.
1994 */
1995 if (!LIST_ISEMPTY(&sess->fe->logformat) && !(s->logs.logwait & LW_BYTES)) {
1996 s->logs.t_close = s->logs.t_data; /* to get a valid end date */
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001997 s->logs.bytes_out = htx->data;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001998 s->do_log(s);
1999 s->logs.bytes_out = 0;
2000 }
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002001
Christopher Fauletb8a53712019-12-16 11:29:38 +01002002 done:
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002003 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002004 rep->analysers &= ~an_bit;
2005 rep->analyse_exp = TICK_ETERNITY;
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002006 s->current_rule = s->current_rule_list = NULL;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002007 return 1;
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002008
Christopher Fauletb8a53712019-12-16 11:29:38 +01002009 deny:
Willy Tarreau4781b152021-04-06 13:53:36 +02002010 _HA_ATOMIC_INC(&sess->fe->fe_counters.denied_resp);
2011 _HA_ATOMIC_INC(&s->be->be_counters.denied_resp);
William Lallemand36119de2021-03-08 15:26:48 +01002012 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02002013 _HA_ATOMIC_INC(&sess->listener->counters->denied_resp);
Christopher Fauleta08546b2019-12-16 16:07:34 +01002014 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02002015 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.denied_resp);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002016 goto return_prx_err;
2017
2018 return_int_err:
2019 txn->status = 500;
2020 if (!(s->flags & SF_ERR_MASK))
2021 s->flags |= SF_ERR_INTERNAL;
Willy Tarreau4781b152021-04-06 13:53:36 +02002022 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
2023 _HA_ATOMIC_INC(&s->be->be_counters.internal_errors);
Dragan Dosen9a006f92021-09-21 13:02:09 +02002024 if (sess->listener && sess->listener->counters)
2025 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Fauletcff0f732019-12-16 16:13:44 +01002026 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02002027 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.internal_errors);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002028 goto return_prx_err;
2029
2030 return_bad_res:
2031 txn->status = 502;
Willy Tarreau826f3ab2021-02-10 12:07:15 +01002032 stream_inc_http_fail_ctr(s);
Willy Tarreau4781b152021-04-06 13:53:36 +02002033 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Fauleta20a6532020-02-05 10:16:41 +01002034 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02002035 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Christopher Fauleta20a6532020-02-05 10:16:41 +01002036 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_RSP);
2037 }
Christopher Fauletb8a53712019-12-16 11:29:38 +01002038 /* fall through */
2039
2040 return_prx_err:
2041 http_reply_and_close(s, txn->status, http_error_message(s));
2042 /* fall through */
2043
2044 return_prx_cond:
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002045 s->logs.t_data = -1; /* was not a valid response */
Christopher Fauleta6294472021-12-23 13:25:57 +01002046 cs_si(s->csb)->flags |= SI_FL_NOLINGER;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002047
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002048 if (!(s->flags & SF_ERR_MASK))
2049 s->flags |= SF_ERR_PRXCOND;
2050 if (!(s->flags & SF_FINST_MASK))
2051 s->flags |= SF_FINST_H;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002052
Christopher Faulete58c0002020-03-02 16:21:01 +01002053 rep->analysers &= AN_RES_FLT_END;
2054 s->req.analysers &= AN_REQ_FLT_END;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002055 rep->analyse_exp = TICK_ETERNITY;
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002056 s->current_rule = s->current_rule_list = NULL;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002057 DBG_TRACE_DEVEL("leaving on error",
2058 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002059 return 0;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002060
2061 return_prx_yield:
2062 channel_dont_close(rep);
2063 DBG_TRACE_DEVEL("waiting for more data",
2064 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
2065 return 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002066}
2067
2068/* This function is an analyser which forwards response body (including chunk
2069 * sizes if any). It is called as soon as we must forward, even if we forward
2070 * zero byte. The only situation where it must not be called is when we're in
2071 * tunnel mode and we want to forward till the close. It's used both to forward
2072 * remaining data and to resync after end of body. It expects the msg_state to
2073 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
2074 * read more data, or 1 once we can go on with next request or end the stream.
2075 *
2076 * It is capable of compressing response data both in content-length mode and
2077 * in chunked mode. The state machines follows different flows depending on
2078 * whether content-length and chunked modes are used, since there are no
2079 * trailers in content-length :
2080 *
2081 * chk-mode cl-mode
2082 * ,----- BODY -----.
2083 * / \
2084 * V size > 0 V chk-mode
2085 * .--> SIZE -------------> DATA -------------> CRLF
2086 * | | size == 0 | last byte |
2087 * | v final crlf v inspected |
2088 * | TRAILERS -----------> DONE |
2089 * | |
2090 * `----------------------------------------------'
2091 *
2092 * Compression only happens in the DATA state, and must be flushed in final
2093 * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding
2094 * is performed at once on final states for all bytes parsed, or when leaving
2095 * on missing data.
2096 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002097int http_response_forward_body(struct stream *s, struct channel *res, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +02002098{
2099 struct session *sess = s->sess;
2100 struct http_txn *txn = s->txn;
2101 struct http_msg *msg = &s->txn->rsp;
Christopher Faulet9768c262018-10-22 09:34:31 +02002102 struct htx *htx;
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002103 int ret;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002104
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002105 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002106
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002107 htx = htxbuf(&res->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002108
Christopher Fauletb9a92f32019-09-09 10:15:21 +02002109 if (htx->flags & HTX_FL_PARSING_ERROR)
2110 goto return_bad_res;
2111 if (htx->flags & HTX_FL_PROCESSING_ERROR)
2112 goto return_int_err;
2113
Christopher Faulete0768eb2018-10-03 16:38:02 +02002114 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Christopher Fauletf2824e62018-10-01 12:12:37 +02002115 ((res->flags & CF_SHUTW) && (res->to_forward || co_data(res)))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02002116 /* Output closed while we were sending data. We must abort and
2117 * wake the other side up.
2118 */
Christopher Faulete0768eb2018-10-03 16:38:02 +02002119 msg->msg_state = HTTP_MSG_ERROR;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002120 http_end_response(s);
2121 http_end_request(s);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002122 DBG_TRACE_DEVEL("leaving on error",
2123 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002124 return 1;
2125 }
2126
Christopher Faulet9768c262018-10-22 09:34:31 +02002127 if (msg->msg_state == HTTP_MSG_BODY)
2128 msg->msg_state = HTTP_MSG_DATA;
2129
Christopher Faulete0768eb2018-10-03 16:38:02 +02002130 /* in most states, we should abort in case of early close */
2131 channel_auto_close(res);
2132
Christopher Faulete0768eb2018-10-03 16:38:02 +02002133 if (res->to_forward) {
Christopher Faulet66af0b22019-03-22 14:54:52 +01002134 if (res->to_forward == CHN_INFINITE_FORWARD) {
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002135 if (res->flags & CF_EOI)
2136 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet66af0b22019-03-22 14:54:52 +01002137 }
2138 else {
2139 /* We can't process the buffer's contents yet */
2140 res->flags |= CF_WAKE_WRITE;
2141 goto missing_data_or_waiting;
2142 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02002143 }
2144
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002145 if (msg->msg_state >= HTTP_MSG_ENDING)
2146 goto ending;
2147
Christopher Fauletc75668e2020-12-07 18:10:32 +01002148 if ((txn->meth == HTTP_METH_CONNECT && txn->status >= 200 && txn->status < 300) || txn->status == 101 ||
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002149 (!(msg->flags & HTTP_MSGF_XFER_LEN) && !HAS_RSP_DATA_FILTERS(s))) {
2150 msg->msg_state = HTTP_MSG_ENDING;
2151 goto ending;
2152 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002153
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002154 /* Forward input data. We get it by removing all outgoing data not
2155 * forwarded yet from HTX data size. If there are some data filters, we
2156 * let them decide the amount of data to forward.
Christopher Faulet9768c262018-10-22 09:34:31 +02002157 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002158 if (HAS_RSP_DATA_FILTERS(s)) {
2159 ret = flt_http_payload(s, msg, htx->data);
2160 if (ret < 0)
2161 goto return_bad_res;
Christopher Faulet421e7692019-06-13 11:16:45 +02002162 c_adv(res, ret);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002163 }
2164 else {
Christopher Faulet421e7692019-06-13 11:16:45 +02002165 c_adv(res, htx->data - co_data(res));
Christopher Faulet66af0b22019-03-22 14:54:52 +01002166 if (msg->flags & HTTP_MSGF_XFER_LEN)
2167 channel_htx_forward_forever(res, htx);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002168 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002169
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002170 if (htx->data != co_data(res))
2171 goto missing_data_or_waiting;
2172
2173 if (!(msg->flags & HTTP_MSGF_XFER_LEN) && res->flags & CF_SHUTR) {
2174 msg->msg_state = HTTP_MSG_ENDING;
2175 goto ending;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002176 }
2177
Christopher Faulet9768c262018-10-22 09:34:31 +02002178 /* Check if the end-of-message is reached and if so, switch the message
Christopher Fauletd20fdb02019-06-13 16:43:22 +02002179 * in HTTP_MSG_ENDING state. Then if all data was marked to be
2180 * forwarded, set the state to HTTP_MSG_DONE.
Christopher Faulet9768c262018-10-22 09:34:31 +02002181 */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002182 if (!(htx->flags & HTX_FL_EOM))
Christopher Faulet9768c262018-10-22 09:34:31 +02002183 goto missing_data_or_waiting;
2184
Christopher Fauletd20fdb02019-06-13 16:43:22 +02002185 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet9768c262018-10-22 09:34:31 +02002186
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002187 ending:
Christopher Faulet2151cdd2020-07-22 16:34:59 +02002188 res->flags &= ~CF_EXPECT_MORE; /* no more data are expected */
2189
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002190 /* other states, ENDING...TUNNEL */
2191 if (msg->msg_state >= HTTP_MSG_DONE)
2192 goto done;
Christopher Faulet9768c262018-10-22 09:34:31 +02002193
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002194 if (HAS_RSP_DATA_FILTERS(s)) {
2195 ret = flt_http_end(s, msg);
2196 if (ret <= 0) {
2197 if (!ret)
2198 goto missing_data_or_waiting;
2199 goto return_bad_res;
2200 }
2201 }
2202
Christopher Fauletc75668e2020-12-07 18:10:32 +01002203 if ((txn->meth == HTTP_METH_CONNECT && txn->status >= 200 && txn->status < 300) || txn->status == 101 ||
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002204 !(msg->flags & HTTP_MSGF_XFER_LEN)) {
2205 msg->msg_state = HTTP_MSG_TUNNEL;
2206 goto ending;
2207 }
2208 else {
2209 msg->msg_state = HTTP_MSG_DONE;
2210 res->to_forward = 0;
2211 }
2212
2213 done:
2214
2215 channel_dont_close(res);
2216
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002217 http_end_response(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002218 if (!(res->analysers & an_bit)) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002219 http_end_request(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002220 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
2221 if (res->flags & CF_SHUTW) {
2222 /* response errors are most likely due to the
2223 * client aborting the transfer. */
Christopher Faulet93e02d82019-03-08 14:18:50 +01002224 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002225 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02002226 goto return_bad_res;
2227 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002228 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002229 return 1;
2230 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002231 DBG_TRACE_DEVEL("waiting for the end of the HTTP txn",
2232 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002233 return 0;
2234
2235 missing_data_or_waiting:
2236 if (res->flags & CF_SHUTW)
Christopher Faulet93e02d82019-03-08 14:18:50 +01002237 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002238
2239 /* stop waiting for data if the input is closed before the end. If the
2240 * client side was already closed, it means that the client has aborted,
2241 * so we don't want to count this as a server abort. Otherwise it's a
2242 * server abort.
2243 */
Christopher Fauletd20fdb02019-06-13 16:43:22 +02002244 if (msg->msg_state < HTTP_MSG_ENDING && res->flags & CF_SHUTR) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02002245 if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))
Christopher Faulet93e02d82019-03-08 14:18:50 +01002246 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002247 /* If we have some pending data, we continue the processing */
Christopher Faulet93e02d82019-03-08 14:18:50 +01002248 if (htx_is_empty(htx))
2249 goto return_srv_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002250 }
2251
Christopher Faulete0768eb2018-10-03 16:38:02 +02002252 /* When TE: chunked is used, we need to get there again to parse
2253 * remaining chunks even if the server has closed, so we don't want to
Christopher Faulet9768c262018-10-22 09:34:31 +02002254 * set CF_DONTCLOSE. Similarly when there is a content-leng or if there
2255 * are filters registered on the stream, we don't want to forward a
2256 * close
Christopher Faulete0768eb2018-10-03 16:38:02 +02002257 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002258 if ((msg->flags & HTTP_MSGF_XFER_LEN) || HAS_RSP_DATA_FILTERS(s))
Christopher Faulete0768eb2018-10-03 16:38:02 +02002259 channel_dont_close(res);
2260
2261 /* We know that more data are expected, but we couldn't send more that
2262 * what we did. So we always set the CF_EXPECT_MORE flag so that the
2263 * system knows it must not set a PUSH on this first part. Interactive
2264 * modes are already handled by the stream sock layer. We must not do
2265 * this in content-length mode because it could present the MSG_MORE
2266 * flag with the last block of forwarded data, which would cause an
2267 * additional delay to be observed by the receiver.
2268 */
Christopher Faulet2151cdd2020-07-22 16:34:59 +02002269 if (HAS_RSP_DATA_FILTERS(s))
Christopher Faulete0768eb2018-10-03 16:38:02 +02002270 res->flags |= CF_EXPECT_MORE;
2271
2272 /* the stream handler will take care of timeouts and errors */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002273 DBG_TRACE_DEVEL("waiting for more data to forward",
2274 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002275 return 0;
2276
Christopher Faulet93e02d82019-03-08 14:18:50 +01002277 return_srv_abort:
Willy Tarreau4781b152021-04-06 13:53:36 +02002278 _HA_ATOMIC_INC(&sess->fe->fe_counters.srv_aborts);
2279 _HA_ATOMIC_INC(&s->be->be_counters.srv_aborts);
William Lallemand36119de2021-03-08 15:26:48 +01002280 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02002281 _HA_ATOMIC_INC(&sess->listener->counters->srv_aborts);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002282 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02002283 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.srv_aborts);
Willy Tarreau826f3ab2021-02-10 12:07:15 +01002284 stream_inc_http_fail_ctr(s);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002285 if (!(s->flags & SF_ERR_MASK))
2286 s->flags |= SF_ERR_SRVCL;
2287 goto return_error;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002288
Christopher Faulet93e02d82019-03-08 14:18:50 +01002289 return_cli_abort:
Willy Tarreau4781b152021-04-06 13:53:36 +02002290 _HA_ATOMIC_INC(&sess->fe->fe_counters.cli_aborts);
2291 _HA_ATOMIC_INC(&s->be->be_counters.cli_aborts);
William Lallemand36119de2021-03-08 15:26:48 +01002292 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02002293 _HA_ATOMIC_INC(&sess->listener->counters->cli_aborts);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002294 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02002295 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.cli_aborts);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002296 if (!(s->flags & SF_ERR_MASK))
2297 s->flags |= SF_ERR_CLICL;
2298 goto return_error;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002299
Christopher Fauletb9a92f32019-09-09 10:15:21 +02002300 return_int_err:
Willy Tarreau4781b152021-04-06 13:53:36 +02002301 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
2302 _HA_ATOMIC_INC(&s->be->be_counters.internal_errors);
William Lallemand36119de2021-03-08 15:26:48 +01002303 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02002304 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002305 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02002306 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.internal_errors);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02002307 if (!(s->flags & SF_ERR_MASK))
2308 s->flags |= SF_ERR_INTERNAL;
2309 goto return_error;
2310
Christopher Faulet93e02d82019-03-08 14:18:50 +01002311 return_bad_res:
Willy Tarreau4781b152021-04-06 13:53:36 +02002312 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002313 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02002314 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002315 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_RSP);
2316 }
Willy Tarreau826f3ab2021-02-10 12:07:15 +01002317 stream_inc_http_fail_ctr(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002318 if (!(s->flags & SF_ERR_MASK))
Christopher Faulet93e02d82019-03-08 14:18:50 +01002319 s->flags |= SF_ERR_SRVCL;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002320 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +02002321
Christopher Faulet93e02d82019-03-08 14:18:50 +01002322 return_error:
Christopher Faulete0768eb2018-10-03 16:38:02 +02002323 /* don't send any error message as we're in the body */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002324 http_reply_and_close(s, txn->status, NULL);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002325 if (!(s->flags & SF_FINST_MASK))
2326 s->flags |= SF_FINST_D;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002327 DBG_TRACE_DEVEL("leaving on error",
2328 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002329 return 0;
2330}
2331
Christopher Fauletf2824e62018-10-01 12:12:37 +02002332/* Perform an HTTP redirect based on the information in <rule>. The function
Willy Tarreaubc1223b2021-09-02 16:54:33 +02002333 * returns zero in case of an irrecoverable error such as too large a request
2334 * to build a valid response, 1 in case of successful redirect (hence the rule
2335 * is final), or 2 if the rule has to be silently skipped.
Christopher Fauletf2824e62018-10-01 12:12:37 +02002336 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002337int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn)
Christopher Fauletf2824e62018-10-01 12:12:37 +02002338{
Christopher Faulet99daf282018-11-28 22:58:13 +01002339 struct channel *req = &s->req;
2340 struct channel *res = &s->res;
2341 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01002342 struct htx_sl *sl;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002343 struct buffer *chunk;
Christopher Faulet99daf282018-11-28 22:58:13 +01002344 struct ist status, reason, location;
2345 unsigned int flags;
Christopher Faulet08e66462019-05-23 16:44:59 +02002346 int close = 0; /* Try to keep the connection alive byt default */
Christopher Fauletf2824e62018-10-01 12:12:37 +02002347
2348 chunk = alloc_trash_chunk();
Christopher Fauletb8a53712019-12-16 11:29:38 +01002349 if (!chunk) {
2350 if (!(s->flags & SF_ERR_MASK))
2351 s->flags |= SF_ERR_RESOURCE;
Christopher Faulet99daf282018-11-28 22:58:13 +01002352 goto fail;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002353 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002354
Christopher Faulet99daf282018-11-28 22:58:13 +01002355 /*
2356 * Create the location
2357 */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002358 htx = htxbuf(&req->buf);
Christopher Fauletf2824e62018-10-01 12:12:37 +02002359 switch(rule->type) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002360 case REDIRECT_TYPE_SCHEME: {
2361 struct http_hdr_ctx ctx;
2362 struct ist path, host;
Amaury Denoyellec453f952021-07-06 11:40:12 +02002363 struct http_uri_parser parser;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002364
Christopher Faulet99daf282018-11-28 22:58:13 +01002365 host = ist("");
2366 ctx.blk = NULL;
2367 if (http_find_header(htx, ist("Host"), &ctx, 0))
2368 host = ctx.value;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002369
Christopher Faulet297fbb42019-05-13 14:41:27 +02002370 sl = http_get_stline(htx);
Amaury Denoyellec453f952021-07-06 11:40:12 +02002371 parser = http_uri_parser_init(htx_sl_req_uri(sl));
2372 path = http_parse_path(&parser);
Christopher Faulet99daf282018-11-28 22:58:13 +01002373 /* build message using path */
Tim Duesterhused526372020-03-05 17:56:33 +01002374 if (isttest(path)) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002375 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2376 int qs = 0;
2377 while (qs < path.len) {
2378 if (*(path.ptr + qs) == '?') {
2379 path.len = qs;
2380 break;
2381 }
2382 qs++;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002383 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002384 }
2385 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002386 else
2387 path = ist("/");
Christopher Fauletf2824e62018-10-01 12:12:37 +02002388
Christopher Faulet99daf282018-11-28 22:58:13 +01002389 if (rule->rdr_str) { /* this is an old "redirect" rule */
2390 /* add scheme */
2391 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2392 goto fail;
2393 }
2394 else {
2395 /* add scheme with executing log format */
2396 chunk->data += build_logline(s, chunk->area + chunk->data,
2397 chunk->size - chunk->data,
2398 &rule->rdr_fmt);
2399 }
2400 /* add "://" + host + path */
2401 if (!chunk_memcat(chunk, "://", 3) ||
2402 !chunk_memcat(chunk, host.ptr, host.len) ||
2403 !chunk_memcat(chunk, path.ptr, path.len))
2404 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002405
Christopher Faulet99daf282018-11-28 22:58:13 +01002406 /* append a slash at the end of the location if needed and missing */
2407 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
2408 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
2409 if (chunk->data + 1 >= chunk->size)
2410 goto fail;
2411 chunk->area[chunk->data++] = '/';
2412 }
2413 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002414 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002415
Christopher Faulet99daf282018-11-28 22:58:13 +01002416 case REDIRECT_TYPE_PREFIX: {
2417 struct ist path;
Amaury Denoyellec453f952021-07-06 11:40:12 +02002418 struct http_uri_parser parser;
Christopher Faulet99daf282018-11-28 22:58:13 +01002419
Christopher Faulet297fbb42019-05-13 14:41:27 +02002420 sl = http_get_stline(htx);
Amaury Denoyellec453f952021-07-06 11:40:12 +02002421 parser = http_uri_parser_init(htx_sl_req_uri(sl));
2422 path = http_parse_path(&parser);
Christopher Faulet99daf282018-11-28 22:58:13 +01002423 /* build message using path */
Tim Duesterhused526372020-03-05 17:56:33 +01002424 if (isttest(path)) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002425 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2426 int qs = 0;
2427 while (qs < path.len) {
2428 if (*(path.ptr + qs) == '?') {
2429 path.len = qs;
2430 break;
2431 }
2432 qs++;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002433 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002434 }
2435 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002436 else
2437 path = ist("/");
Christopher Fauletf2824e62018-10-01 12:12:37 +02002438
Christopher Faulet99daf282018-11-28 22:58:13 +01002439 if (rule->rdr_str) { /* this is an old "redirect" rule */
2440 /* add prefix. Note that if prefix == "/", we don't want to
2441 * add anything, otherwise it makes it hard for the user to
2442 * configure a self-redirection.
2443 */
2444 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
2445 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2446 goto fail;
2447 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002448 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002449 else {
2450 /* add prefix with executing log format */
2451 chunk->data += build_logline(s, chunk->area + chunk->data,
2452 chunk->size - chunk->data,
2453 &rule->rdr_fmt);
2454 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002455
Christopher Faulet99daf282018-11-28 22:58:13 +01002456 /* add path */
2457 if (!chunk_memcat(chunk, path.ptr, path.len))
2458 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002459
Christopher Faulet99daf282018-11-28 22:58:13 +01002460 /* append a slash at the end of the location if needed and missing */
2461 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
2462 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
2463 if (chunk->data + 1 >= chunk->size)
2464 goto fail;
2465 chunk->area[chunk->data++] = '/';
2466 }
2467 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002468 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002469 case REDIRECT_TYPE_LOCATION:
2470 default:
2471 if (rule->rdr_str) { /* this is an old "redirect" rule */
2472 /* add location */
2473 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2474 goto fail;
2475 }
2476 else {
2477 /* add location with executing log format */
Willy Tarreaubc1223b2021-09-02 16:54:33 +02002478 int len = build_logline(s, chunk->area + chunk->data,
2479 chunk->size - chunk->data,
2480 &rule->rdr_fmt);
2481 if (!len && rule->flags & REDIRECT_FLAG_IGNORE_EMPTY)
2482 return 2;
2483
2484 chunk->data += len;
Christopher Faulet99daf282018-11-28 22:58:13 +01002485 }
2486 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002487 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002488 location = ist2(chunk->area, chunk->data);
2489
2490 /*
2491 * Create the 30x response
2492 */
2493 switch (rule->code) {
2494 case 308:
2495 status = ist("308");
2496 reason = ist("Permanent Redirect");
2497 break;
2498 case 307:
2499 status = ist("307");
2500 reason = ist("Temporary Redirect");
2501 break;
2502 case 303:
2503 status = ist("303");
2504 reason = ist("See Other");
2505 break;
2506 case 301:
2507 status = ist("301");
2508 reason = ist("Moved Permanently");
2509 break;
2510 case 302:
2511 default:
2512 status = ist("302");
2513 reason = ist("Found");
2514 break;
2515 }
2516
Christopher Faulet08e66462019-05-23 16:44:59 +02002517 if (!(txn->req.flags & HTTP_MSGF_BODYLESS) && txn->req.msg_state != HTTP_MSG_DONE)
2518 close = 1;
2519
Christopher Faulet99daf282018-11-28 22:58:13 +01002520 htx = htx_from_buf(&res->buf);
Kevin Zhu96b36392020-01-07 09:42:55 +01002521 /* Trim any possible response */
2522 channel_htx_truncate(&s->res, htx);
Christopher Faulet99daf282018-11-28 22:58:13 +01002523 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
2524 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), status, reason);
2525 if (!sl)
2526 goto fail;
2527 sl->info.res.status = rule->code;
2528 s->txn->status = rule->code;
2529
Christopher Faulet08e66462019-05-23 16:44:59 +02002530 if (close && !htx_add_header(htx, ist("Connection"), ist("close")))
2531 goto fail;
2532
2533 if (!htx_add_header(htx, ist("Content-length"), ist("0")) ||
Christopher Faulet99daf282018-11-28 22:58:13 +01002534 !htx_add_header(htx, ist("Location"), location))
2535 goto fail;
2536
2537 if (rule->code == 302 || rule->code == 303 || rule->code == 307) {
2538 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")))
2539 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002540 }
2541
2542 if (rule->cookie_len) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002543 if (!htx_add_header(htx, ist("Set-Cookie"), ist2(rule->cookie_str, rule->cookie_len)))
2544 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002545 }
2546
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002547 if (!htx_add_endof(htx, HTX_BLK_EOH))
Christopher Faulet99daf282018-11-28 22:58:13 +01002548 goto fail;
2549
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002550 htx->flags |= HTX_FL_EOM;
Kevin Zhu96b36392020-01-07 09:42:55 +01002551 htx_to_buf(htx, &res->buf);
Christopher Fauleta72a7e42020-01-28 09:28:11 +01002552 if (!http_forward_proxy_resp(s, 1))
2553 goto fail;
Christopher Faulet99daf282018-11-28 22:58:13 +01002554
Christopher Faulet60b33a52020-01-28 09:18:10 +01002555 if (rule->flags & REDIRECT_FLAG_FROM_REQ) {
2556 /* let's log the request time */
2557 s->logs.tv_request = now;
Christopher Fauletd3475882021-10-04 14:16:46 +02002558 req->analysers &= AN_REQ_FLT_END;
Christopher Faulet99daf282018-11-28 22:58:13 +01002559
Christopher Faulet60b33a52020-01-28 09:18:10 +01002560 if (s->sess->fe == s->be) /* report it if the request was intercepted by the frontend */
Willy Tarreau4781b152021-04-06 13:53:36 +02002561 _HA_ATOMIC_INC(&s->sess->fe->fe_counters.intercepted_req);
Christopher Faulet60b33a52020-01-28 09:18:10 +01002562 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002563
2564 if (!(s->flags & SF_ERR_MASK))
2565 s->flags |= SF_ERR_LOCAL;
2566 if (!(s->flags & SF_FINST_MASK))
Christopher Faulet60b33a52020-01-28 09:18:10 +01002567 s->flags |= ((rule->flags & REDIRECT_FLAG_FROM_REQ) ? SF_FINST_R : SF_FINST_H);
Christopher Fauletf2824e62018-10-01 12:12:37 +02002568
Christopher Faulet99daf282018-11-28 22:58:13 +01002569 free_trash_chunk(chunk);
2570 return 1;
2571
2572 fail:
2573 /* If an error occurred, remove the incomplete HTTP response from the
2574 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01002575 channel_htx_truncate(res, htxbuf(&res->buf));
Christopher Fauletf2824e62018-10-01 12:12:37 +02002576 free_trash_chunk(chunk);
Christopher Faulet99daf282018-11-28 22:58:13 +01002577 return 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002578}
2579
Christopher Faulet92d34fe2019-12-17 09:20:34 +01002580/* Replace all headers matching the name <name>. The header value is replaced if
2581 * it matches the regex <re>. <str> is used for the replacement. If <full> is
2582 * set to 1, the full-line is matched and replaced. Otherwise, comma-separated
2583 * values are evaluated one by one. It returns 0 on success and -1 on error.
2584 */
2585int http_replace_hdrs(struct stream* s, struct htx *htx, struct ist name,
2586 const char *str, struct my_regex *re, int full)
Christopher Faulet72333522018-10-24 11:25:02 +02002587{
2588 struct http_hdr_ctx ctx;
2589 struct buffer *output = get_trash_chunk();
2590
Christopher Faulet72333522018-10-24 11:25:02 +02002591 ctx.blk = NULL;
Christopher Faulet92d34fe2019-12-17 09:20:34 +01002592 while (http_find_header(htx, name, &ctx, full)) {
Christopher Faulet72333522018-10-24 11:25:02 +02002593 if (!regex_exec_match2(re, ctx.value.ptr, ctx.value.len, MAX_MATCH, pmatch, 0))
2594 continue;
2595
2596 output->data = exp_replace(output->area, output->size, ctx.value.ptr, str, pmatch);
2597 if (output->data == -1)
2598 return -1;
2599 if (!http_replace_header_value(htx, &ctx, ist2(output->area, output->data)))
2600 return -1;
2601 }
2602 return 0;
2603}
2604
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002605/* This function executes one of the set-{method,path,query,uri} actions. It
2606 * takes the string from the variable 'replace' with length 'len', then modifies
2607 * the relevant part of the request line accordingly. Then it updates various
2608 * pointers to the next elements which were moved, and the total buffer length.
2609 * It finds the action to be performed in p[2], previously filled by function
2610 * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal
2611 * error, though this can be revisited when this code is finally exploited.
2612 *
2613 * 'action' can be '0' to replace method, '1' to replace path, '2' to replace
Christopher Faulet312294f2020-09-02 17:17:44 +02002614 * query string, 3 to replace uri or 4 to replace the path+query.
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002615 *
2616 * In query string case, the mark question '?' must be set at the start of the
2617 * string by the caller, event if the replacement query string is empty.
2618 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002619int http_req_replace_stline(int action, const char *replace, int len,
2620 struct proxy *px, struct stream *s)
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002621{
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002622 struct htx *htx = htxbuf(&s->req.buf);
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002623
2624 switch (action) {
2625 case 0: // method
2626 if (!http_replace_req_meth(htx, ist2(replace, len)))
2627 return -1;
2628 break;
2629
2630 case 1: // path
Christopher Fauletb8ce5052020-08-31 16:11:57 +02002631 if (!http_replace_req_path(htx, ist2(replace, len), 0))
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002632 return -1;
2633 break;
2634
2635 case 2: // query
2636 if (!http_replace_req_query(htx, ist2(replace, len)))
2637 return -1;
2638 break;
2639
2640 case 3: // uri
2641 if (!http_replace_req_uri(htx, ist2(replace, len)))
2642 return -1;
2643 break;
2644
Christopher Faulet312294f2020-09-02 17:17:44 +02002645 case 4: // path + query
2646 if (!http_replace_req_path(htx, ist2(replace, len), 1))
2647 return -1;
2648 break;
2649
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002650 default:
2651 return -1;
2652 }
2653 return 0;
2654}
2655
2656/* This function replace the HTTP status code and the associated message. The
Christopher Faulete00d06c2019-12-16 17:18:42 +01002657 * variable <status> contains the new status code. This function never fails. It
2658 * returns 0 in case of success, -1 in case of internal error.
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002659 */
Christopher Faulet96bff762019-12-17 13:46:18 +01002660int http_res_set_status(unsigned int status, struct ist reason, struct stream *s)
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002661{
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002662 struct htx *htx = htxbuf(&s->res.buf);
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002663 char *res;
2664
2665 chunk_reset(&trash);
2666 res = ultoa_o(status, trash.area, trash.size);
2667 trash.data = res - trash.area;
2668
2669 /* Do we have a custom reason format string? */
Tim Duesterhuse296d3e2020-03-05 17:56:31 +01002670 if (!isttest(reason)) {
Christopher Faulet96bff762019-12-17 13:46:18 +01002671 const char *str = http_get_reason(status);
Tim Duesterhusdcf753a2021-03-04 17:31:47 +01002672 reason = ist(str);
Christopher Faulet96bff762019-12-17 13:46:18 +01002673 }
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002674
Christopher Fauletbde2c4c2020-08-31 16:43:34 +02002675 if (!http_replace_res_status(htx, ist2(trash.area, trash.data), reason))
Christopher Faulete00d06c2019-12-16 17:18:42 +01002676 return -1;
2677 return 0;
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002678}
2679
Christopher Faulet3e964192018-10-24 11:39:23 +02002680/* Executes the http-request rules <rules> for stream <s>, proxy <px> and
2681 * transaction <txn>. Returns the verdict of the first rule that prevents
2682 * further processing of the request (auth, deny, ...), and defaults to
2683 * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or
2684 * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT
2685 * on txn->flags if it encounters a tarpit rule. If <deny_status> is not NULL
2686 * and a deny/tarpit rule is matched, it will be filled with this rule's deny
2687 * status.
2688 */
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002689static enum rule_result http_req_get_intercept_rule(struct proxy *px, struct list *def_rules,
2690 struct list *rules, struct stream *s)
Christopher Faulet3e964192018-10-24 11:39:23 +02002691{
2692 struct session *sess = strm_sess(s);
2693 struct http_txn *txn = s->txn;
Christopher Faulet3e964192018-10-24 11:39:23 +02002694 struct act_rule *rule;
Christopher Faulet3e964192018-10-24 11:39:23 +02002695 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002696 int act_opts = 0;
Christopher Faulet3e964192018-10-24 11:39:23 +02002697
Christopher Faulet3e964192018-10-24 11:39:23 +02002698 /* If "the current_rule_list" match the executed rule list, we are in
2699 * resume condition. If a resume is needed it is always in the action
2700 * and never in the ACL or converters. In this case, we initialise the
2701 * current rule, and go to the action execution point.
2702 */
2703 if (s->current_rule) {
2704 rule = s->current_rule;
2705 s->current_rule = NULL;
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002706 if (s->current_rule_list == rules || (def_rules && s->current_rule_list == def_rules))
Christopher Faulet3e964192018-10-24 11:39:23 +02002707 goto resume_execution;
2708 }
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002709 s->current_rule_list = ((!def_rules || s->current_rule_list == def_rules) ? rules : def_rules);
Christopher Faulet3e964192018-10-24 11:39:23 +02002710
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002711 restart:
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002712 /* start the ruleset evaluation in strict mode */
2713 txn->req.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01002714
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002715 list_for_each_entry(rule, s->current_rule_list, list) {
Christopher Faulet3e964192018-10-24 11:39:23 +02002716 /* check optional condition */
2717 if (rule->cond) {
2718 int ret;
2719
2720 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
2721 ret = acl_pass(ret);
2722
2723 if (rule->cond->pol == ACL_COND_UNLESS)
2724 ret = !ret;
2725
2726 if (!ret) /* condition not matched */
2727 continue;
2728 }
2729
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002730 act_opts |= ACT_OPT_FIRST;
Christopher Faulet3e964192018-10-24 11:39:23 +02002731 resume_execution:
Amaury Denoyelle03517732021-05-07 14:25:01 +02002732 if (rule->kw->flags & KWF_EXPERIMENTAL)
2733 mark_tainted(TAINTED_ACTION_EXP_EXECUTED);
2734
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002735 /* Always call the action function if defined */
2736 if (rule->action_ptr) {
2737 if ((s->req.flags & CF_READ_ERROR) ||
2738 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
2739 (px->options & PR_O_ABRT_CLOSE)))
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002740 act_opts |= ACT_OPT_FINAL;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002741
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002742 switch (rule->action_ptr(rule, px, sess, s, act_opts)) {
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002743 case ACT_RET_CONT:
2744 break;
2745 case ACT_RET_STOP:
2746 rule_ret = HTTP_RULE_RES_STOP;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002747 s->last_rule_file = rule->conf.file;
2748 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002749 goto end;
2750 case ACT_RET_YIELD:
2751 s->current_rule = rule;
2752 rule_ret = HTTP_RULE_RES_YIELD;
2753 goto end;
2754 case ACT_RET_ERR:
2755 rule_ret = HTTP_RULE_RES_ERROR;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002756 s->last_rule_file = rule->conf.file;
2757 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002758 goto end;
2759 case ACT_RET_DONE:
2760 rule_ret = HTTP_RULE_RES_DONE;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002761 s->last_rule_file = rule->conf.file;
2762 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002763 goto end;
2764 case ACT_RET_DENY:
Christopher Fauletb58f62b2020-01-13 16:40:13 +01002765 if (txn->status == -1)
2766 txn->status = 403;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002767 rule_ret = HTTP_RULE_RES_DENY;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002768 s->last_rule_file = rule->conf.file;
2769 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002770 goto end;
2771 case ACT_RET_ABRT:
2772 rule_ret = HTTP_RULE_RES_ABRT;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002773 s->last_rule_file = rule->conf.file;
2774 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002775 goto end;
2776 case ACT_RET_INV:
2777 rule_ret = HTTP_RULE_RES_BADREQ;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002778 s->last_rule_file = rule->conf.file;
2779 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002780 goto end;
2781 }
2782 continue; /* eval the next rule */
2783 }
2784
2785 /* If not action function defined, check for known actions */
Christopher Faulet3e964192018-10-24 11:39:23 +02002786 switch (rule->action) {
2787 case ACT_ACTION_ALLOW:
2788 rule_ret = HTTP_RULE_RES_STOP;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002789 s->last_rule_file = rule->conf.file;
2790 s->last_rule_line = rule->conf.line;
Christopher Faulet3e964192018-10-24 11:39:23 +02002791 goto end;
2792
2793 case ACT_ACTION_DENY:
Christopher Faulet5cb513a2020-05-13 17:56:56 +02002794 txn->status = rule->arg.http_reply->status;
2795 txn->http_reply = rule->arg.http_reply;
Christopher Faulet3e964192018-10-24 11:39:23 +02002796 rule_ret = HTTP_RULE_RES_DENY;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002797 s->last_rule_file = rule->conf.file;
2798 s->last_rule_line = rule->conf.line;
Christopher Faulet3e964192018-10-24 11:39:23 +02002799 goto end;
2800
2801 case ACT_HTTP_REQ_TARPIT:
2802 txn->flags |= TX_CLTARPIT;
Christopher Faulet5cb513a2020-05-13 17:56:56 +02002803 txn->status = rule->arg.http_reply->status;
2804 txn->http_reply = rule->arg.http_reply;
Christopher Faulet3e964192018-10-24 11:39:23 +02002805 rule_ret = HTTP_RULE_RES_DENY;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002806 s->last_rule_file = rule->conf.file;
2807 s->last_rule_line = rule->conf.line;
Christopher Faulet3e964192018-10-24 11:39:23 +02002808 goto end;
2809
Willy Tarreaubc1223b2021-09-02 16:54:33 +02002810 case ACT_HTTP_REDIR: {
2811 int ret = http_apply_redirect_rule(rule->arg.redir, s, txn);
2812
2813 if (ret == 2) // 2 == skip
2814 break;
2815
2816 rule_ret = ret ? HTTP_RULE_RES_ABRT : HTTP_RULE_RES_ERROR;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002817 s->last_rule_file = rule->conf.file;
2818 s->last_rule_line = rule->conf.line;
Christopher Faulet3e964192018-10-24 11:39:23 +02002819 goto end;
Willy Tarreaubc1223b2021-09-02 16:54:33 +02002820 }
Christopher Faulet3e964192018-10-24 11:39:23 +02002821
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002822 /* other flags exists, but normally, they never be matched. */
Christopher Faulet3e964192018-10-24 11:39:23 +02002823 default:
2824 break;
2825 }
2826 }
2827
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002828 if (def_rules && s->current_rule_list == def_rules) {
2829 s->current_rule_list = rules;
2830 goto restart;
2831 }
2832
Christopher Faulet3e964192018-10-24 11:39:23 +02002833 end:
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002834 /* if the ruleset evaluation is finished reset the strict mode */
Christopher Faulet46f95542019-12-20 10:07:22 +01002835 if (rule_ret != HTTP_RULE_RES_YIELD)
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002836 txn->req.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01002837
Christopher Faulet3e964192018-10-24 11:39:23 +02002838 /* we reached the end of the rules, nothing to report */
2839 return rule_ret;
2840}
2841
2842/* Executes the http-response rules <rules> for stream <s> and proxy <px>. It
2843 * returns one of 5 possible statuses: HTTP_RULE_RES_CONT, HTTP_RULE_RES_STOP,
2844 * HTTP_RULE_RES_DONE, HTTP_RULE_RES_YIELD, or HTTP_RULE_RES_BADREQ. If *CONT
2845 * is returned, the process can continue the evaluation of next rule list. If
2846 * *STOP or *DONE is returned, the process must stop the evaluation. If *BADREQ
2847 * is returned, it means the operation could not be processed and a server error
Christopher Fauleta53abad2020-05-13 08:12:22 +02002848 * must be returned. If *YIELD is returned, the caller must call again the
2849 * function with the same context.
Christopher Faulet3e964192018-10-24 11:39:23 +02002850 */
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002851static enum rule_result http_res_get_intercept_rule(struct proxy *px, struct list *def_rules,
2852 struct list *rules, struct stream *s)
Christopher Faulet3e964192018-10-24 11:39:23 +02002853{
2854 struct session *sess = strm_sess(s);
2855 struct http_txn *txn = s->txn;
Christopher Faulet3e964192018-10-24 11:39:23 +02002856 struct act_rule *rule;
Christopher Faulet3e964192018-10-24 11:39:23 +02002857 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002858 int act_opts = 0;
Christopher Faulet3e964192018-10-24 11:39:23 +02002859
Christopher Faulet3e964192018-10-24 11:39:23 +02002860 /* If "the current_rule_list" match the executed rule list, we are in
2861 * resume condition. If a resume is needed it is always in the action
2862 * and never in the ACL or converters. In this case, we initialise the
2863 * current rule, and go to the action execution point.
2864 */
2865 if (s->current_rule) {
2866 rule = s->current_rule;
2867 s->current_rule = NULL;
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002868 if (s->current_rule_list == rules || (def_rules && s->current_rule_list == def_rules))
Christopher Faulet3e964192018-10-24 11:39:23 +02002869 goto resume_execution;
2870 }
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002871 s->current_rule_list = ((!def_rules || s->current_rule_list == def_rules) ? rules : def_rules);
2872
2873 restart:
Christopher Faulet3e964192018-10-24 11:39:23 +02002874
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002875 /* start the ruleset evaluation in strict mode */
2876 txn->rsp.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01002877
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002878 list_for_each_entry(rule, s->current_rule_list, list) {
Christopher Faulet3e964192018-10-24 11:39:23 +02002879 /* check optional condition */
2880 if (rule->cond) {
2881 int ret;
2882
2883 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
2884 ret = acl_pass(ret);
2885
2886 if (rule->cond->pol == ACL_COND_UNLESS)
2887 ret = !ret;
2888
2889 if (!ret) /* condition not matched */
2890 continue;
2891 }
2892
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002893 act_opts |= ACT_OPT_FIRST;
Christopher Faulet3e964192018-10-24 11:39:23 +02002894resume_execution:
Amaury Denoyelle03517732021-05-07 14:25:01 +02002895 if (rule->kw->flags & KWF_EXPERIMENTAL)
2896 mark_tainted(TAINTED_ACTION_EXP_EXECUTED);
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002897
2898 /* Always call the action function if defined */
2899 if (rule->action_ptr) {
2900 if ((s->req.flags & CF_READ_ERROR) ||
2901 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
2902 (px->options & PR_O_ABRT_CLOSE)))
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002903 act_opts |= ACT_OPT_FINAL;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002904
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002905 switch (rule->action_ptr(rule, px, sess, s, act_opts)) {
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002906 case ACT_RET_CONT:
2907 break;
2908 case ACT_RET_STOP:
2909 rule_ret = HTTP_RULE_RES_STOP;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002910 s->last_rule_file = rule->conf.file;
2911 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002912 goto end;
2913 case ACT_RET_YIELD:
2914 s->current_rule = rule;
2915 rule_ret = HTTP_RULE_RES_YIELD;
2916 goto end;
2917 case ACT_RET_ERR:
2918 rule_ret = HTTP_RULE_RES_ERROR;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002919 s->last_rule_file = rule->conf.file;
2920 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002921 goto end;
2922 case ACT_RET_DONE:
2923 rule_ret = HTTP_RULE_RES_DONE;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002924 s->last_rule_file = rule->conf.file;
2925 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002926 goto end;
2927 case ACT_RET_DENY:
Christopher Fauletb58f62b2020-01-13 16:40:13 +01002928 if (txn->status == -1)
2929 txn->status = 502;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002930 rule_ret = HTTP_RULE_RES_DENY;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002931 s->last_rule_file = rule->conf.file;
2932 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002933 goto end;
2934 case ACT_RET_ABRT:
2935 rule_ret = HTTP_RULE_RES_ABRT;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002936 s->last_rule_file = rule->conf.file;
2937 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002938 goto end;
2939 case ACT_RET_INV:
2940 rule_ret = HTTP_RULE_RES_BADREQ;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002941 s->last_rule_file = rule->conf.file;
2942 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002943 goto end;
2944 }
2945 continue; /* eval the next rule */
2946 }
2947
2948 /* If not action function defined, check for known actions */
Christopher Faulet3e964192018-10-24 11:39:23 +02002949 switch (rule->action) {
2950 case ACT_ACTION_ALLOW:
2951 rule_ret = HTTP_RULE_RES_STOP; /* "allow" rules are OK */
Willy Tarreauc6dae862022-03-09 17:23:10 +01002952 s->last_rule_file = rule->conf.file;
2953 s->last_rule_line = rule->conf.line;
Christopher Faulet3e964192018-10-24 11:39:23 +02002954 goto end;
2955
2956 case ACT_ACTION_DENY:
Christopher Faulet5cb513a2020-05-13 17:56:56 +02002957 txn->status = rule->arg.http_reply->status;
2958 txn->http_reply = rule->arg.http_reply;
Christopher Faulet3a26bee2019-12-16 12:47:40 +01002959 rule_ret = HTTP_RULE_RES_DENY;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002960 s->last_rule_file = rule->conf.file;
2961 s->last_rule_line = rule->conf.line;
Christopher Faulet3e964192018-10-24 11:39:23 +02002962 goto end;
2963
Willy Tarreaubc1223b2021-09-02 16:54:33 +02002964 case ACT_HTTP_REDIR: {
2965 int ret = http_apply_redirect_rule(rule->arg.redir, s, txn);
Christopher Faulet3e964192018-10-24 11:39:23 +02002966
Willy Tarreaubc1223b2021-09-02 16:54:33 +02002967 if (ret == 2) // 2 == skip
2968 break;
2969
2970 rule_ret = ret ? HTTP_RULE_RES_ABRT : HTTP_RULE_RES_ERROR;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002971 s->last_rule_file = rule->conf.file;
2972 s->last_rule_line = rule->conf.line;
Willy Tarreaubc1223b2021-09-02 16:54:33 +02002973 goto end;
2974 }
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002975 /* other flags exists, but normally, they never be matched. */
Christopher Faulet3e964192018-10-24 11:39:23 +02002976 default:
2977 break;
2978 }
2979 }
2980
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002981 if (def_rules && s->current_rule_list == def_rules) {
2982 s->current_rule_list = rules;
2983 goto restart;
2984 }
2985
Christopher Faulet3e964192018-10-24 11:39:23 +02002986 end:
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002987 /* if the ruleset evaluation is finished reset the strict mode */
Christopher Faulet46f95542019-12-20 10:07:22 +01002988 if (rule_ret != HTTP_RULE_RES_YIELD)
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002989 txn->rsp.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01002990
Christopher Faulet3e964192018-10-24 11:39:23 +02002991 /* we reached the end of the rules, nothing to report */
2992 return rule_ret;
2993}
2994
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002995/* Executes backend and frontend http-after-response rules for the stream <s>,
2996 * in that order. it return 1 on success and 0 on error. It is the caller
2997 * responsibility to catch error or ignore it. If it catches it, this function
2998 * may be called a second time, for the internal error.
2999 */
3000int http_eval_after_res_rules(struct stream *s)
3001{
Christopher Fauletd4150ad2021-10-13 15:35:55 +02003002 struct list *def_rules, *rules;
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01003003 struct session *sess = s->sess;
3004 enum rule_result ret = HTTP_RULE_RES_CONT;
3005
Christopher Faulet507479b2020-05-15 12:29:46 +02003006 /* Eval after-response ruleset only if the reply is not const */
3007 if (s->txn->flags & TX_CONST_REPLY)
3008 goto end;
3009
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01003010 /* prune the request variables if not already done and swap to the response variables. */
3011 if (s->vars_reqres.scope != SCOPE_RES) {
3012 if (!LIST_ISEMPTY(&s->vars_reqres.head))
3013 vars_prune(&s->vars_reqres, s->sess, s);
Willy Tarreaub7bfcb32021-08-31 08:13:25 +02003014 vars_init_head(&s->vars_reqres, SCOPE_RES);
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01003015 }
3016
Christopher Fauletd4150ad2021-10-13 15:35:55 +02003017 def_rules = (s->be->defpx ? &s->be->defpx->http_after_res_rules : NULL);
3018 rules = &s->be->http_after_res_rules;
3019
3020 ret = http_res_get_intercept_rule(s->be, def_rules, rules, s);
Christopher Faulet4c5a5912021-11-09 17:48:39 +01003021 if ((ret == HTTP_RULE_RES_CONT || ret == HTTP_RULE_RES_STOP) && sess->fe != s->be) {
Christopher Fauletd4150ad2021-10-13 15:35:55 +02003022 def_rules = ((sess->fe->defpx && sess->fe->defpx != s->be->defpx) ? &sess->fe->defpx->http_after_res_rules : NULL);
3023 rules = &sess->fe->http_after_res_rules;
3024 ret = http_res_get_intercept_rule(sess->fe, def_rules, rules, s);
3025 }
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01003026
Christopher Faulet507479b2020-05-15 12:29:46 +02003027 end:
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01003028 /* All other codes than CONTINUE, STOP or DONE are forbidden */
3029 return (ret == HTTP_RULE_RES_CONT || ret == HTTP_RULE_RES_STOP || ret == HTTP_RULE_RES_DONE);
3030}
3031
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003032/*
3033 * Manage client-side cookie. It can impact performance by about 2% so it is
3034 * desirable to call it only when needed. This code is quite complex because
3035 * of the multiple very crappy and ambiguous syntaxes we have to support. it
3036 * highly recommended not to touch this part without a good reason !
3037 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003038static void http_manage_client_side_cookies(struct stream *s, struct channel *req)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003039{
3040 struct session *sess = s->sess;
3041 struct http_txn *txn = s->txn;
3042 struct htx *htx;
3043 struct http_hdr_ctx ctx;
3044 char *hdr_beg, *hdr_end, *del_from;
3045 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
3046 int preserve_hdr;
3047
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003048 htx = htxbuf(&req->buf);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003049 ctx.blk = NULL;
3050 while (http_find_header(htx, ist("Cookie"), &ctx, 1)) {
Olivier Houchardf0f42382019-07-22 17:43:46 +02003051 int is_first = 1;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003052 del_from = NULL; /* nothing to be deleted */
3053 preserve_hdr = 0; /* assume we may kill the whole header */
3054
3055 /* Now look for cookies. Conforming to RFC2109, we have to support
3056 * attributes whose name begin with a '$', and associate them with
3057 * the right cookie, if we want to delete this cookie.
3058 * So there are 3 cases for each cookie read :
3059 * 1) it's a special attribute, beginning with a '$' : ignore it.
3060 * 2) it's a server id cookie that we *MAY* want to delete : save
3061 * some pointers on it (last semi-colon, beginning of cookie...)
3062 * 3) it's an application cookie : we *MAY* have to delete a previous
3063 * "special" cookie.
3064 * At the end of loop, if a "special" cookie remains, we may have to
3065 * remove it. If no application cookie persists in the header, we
3066 * *MUST* delete it.
3067 *
3068 * Note: RFC2965 is unclear about the processing of spaces around
3069 * the equal sign in the ATTR=VALUE form. A careful inspection of
3070 * the RFC explicitly allows spaces before it, and not within the
3071 * tokens (attrs or values). An inspection of RFC2109 allows that
3072 * too but section 10.1.3 lets one think that spaces may be allowed
3073 * after the equal sign too, resulting in some (rare) buggy
3074 * implementations trying to do that. So let's do what servers do.
3075 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
3076 * allowed quoted strings in values, with any possible character
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003077 * after a backslash, including control chars and delimiters, which
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003078 * causes parsing to become ambiguous. Browsers also allow spaces
3079 * within values even without quotes.
3080 *
3081 * We have to keep multiple pointers in order to support cookie
3082 * removal at the beginning, middle or end of header without
3083 * corrupting the header. All of these headers are valid :
3084 *
3085 * hdr_beg hdr_end
3086 * | |
3087 * v |
3088 * NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3 |
3089 * NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3 v
3090 * NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3
3091 * | | | | | | |
3092 * | | | | | | |
3093 * | | | | | | +--> next
3094 * | | | | | +----> val_end
3095 * | | | | +-----------> val_beg
3096 * | | | +--------------> equal
3097 * | | +----------------> att_end
3098 * | +---------------------> att_beg
3099 * +--------------------------> prev
3100 *
3101 */
3102 hdr_beg = ctx.value.ptr;
3103 hdr_end = hdr_beg + ctx.value.len;
3104 for (prev = hdr_beg; prev < hdr_end; prev = next) {
3105 /* Iterate through all cookies on this line */
3106
3107 /* find att_beg */
3108 att_beg = prev;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003109 if (!is_first)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003110 att_beg++;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003111 is_first = 0;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003112
3113 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
3114 att_beg++;
3115
3116 /* find att_end : this is the first character after the last non
3117 * space before the equal. It may be equal to hdr_end.
3118 */
3119 equal = att_end = att_beg;
3120 while (equal < hdr_end) {
3121 if (*equal == '=' || *equal == ',' || *equal == ';')
3122 break;
3123 if (HTTP_IS_SPHT(*equal++))
3124 continue;
3125 att_end = equal;
3126 }
3127
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003128 /* here, <equal> points to '=', a delimiter or the end. <att_end>
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003129 * is between <att_beg> and <equal>, both may be identical.
3130 */
3131 /* look for end of cookie if there is an equal sign */
3132 if (equal < hdr_end && *equal == '=') {
3133 /* look for the beginning of the value */
3134 val_beg = equal + 1;
3135 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
3136 val_beg++;
3137
3138 /* find the end of the value, respecting quotes */
3139 next = http_find_cookie_value_end(val_beg, hdr_end);
3140
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003141 /* make val_end point to the first white space or delimiter after the value */
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003142 val_end = next;
3143 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
3144 val_end--;
3145 }
3146 else
3147 val_beg = val_end = next = equal;
3148
3149 /* We have nothing to do with attributes beginning with
3150 * '$'. However, they will automatically be removed if a
3151 * header before them is removed, since they're supposed
3152 * to be linked together.
3153 */
3154 if (*att_beg == '$')
3155 continue;
3156
3157 /* Ignore cookies with no equal sign */
3158 if (equal == next) {
3159 /* This is not our cookie, so we must preserve it. But if we already
3160 * scheduled another cookie for removal, we cannot remove the
3161 * complete header, but we can remove the previous block itself.
3162 */
3163 preserve_hdr = 1;
3164 if (del_from != NULL) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003165 int delta = http_del_hdr_value(hdr_beg, hdr_end, &del_from, prev);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003166 val_end += delta;
3167 next += delta;
3168 hdr_end += delta;
3169 prev = del_from;
3170 del_from = NULL;
3171 }
3172 continue;
3173 }
3174
3175 /* if there are spaces around the equal sign, we need to
3176 * strip them otherwise we'll get trouble for cookie captures,
3177 * or even for rewrites. Since this happens extremely rarely,
3178 * it does not hurt performance.
3179 */
3180 if (unlikely(att_end != equal || val_beg > equal + 1)) {
3181 int stripped_before = 0;
3182 int stripped_after = 0;
3183
3184 if (att_end != equal) {
3185 memmove(att_end, equal, hdr_end - equal);
3186 stripped_before = (att_end - equal);
3187 equal += stripped_before;
3188 val_beg += stripped_before;
3189 }
3190
3191 if (val_beg > equal + 1) {
3192 memmove(equal + 1, val_beg, hdr_end + stripped_before - val_beg);
3193 stripped_after = (equal + 1) - val_beg;
3194 val_beg += stripped_after;
3195 stripped_before += stripped_after;
3196 }
3197
3198 val_end += stripped_before;
3199 next += stripped_before;
3200 hdr_end += stripped_before;
3201 }
3202 /* now everything is as on the diagram above */
3203
3204 /* First, let's see if we want to capture this cookie. We check
3205 * that we don't already have a client side cookie, because we
3206 * can only capture one. Also as an optimisation, we ignore
3207 * cookies shorter than the declared name.
3208 */
3209 if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL &&
3210 (val_end - att_beg >= sess->fe->capture_namelen) &&
3211 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
3212 int log_len = val_end - att_beg;
3213
3214 if ((txn->cli_cookie = pool_alloc(pool_head_capture)) == NULL) {
3215 ha_alert("HTTP logging : out of memory.\n");
3216 } else {
3217 if (log_len > sess->fe->capture_len)
3218 log_len = sess->fe->capture_len;
3219 memcpy(txn->cli_cookie, att_beg, log_len);
3220 txn->cli_cookie[log_len] = 0;
3221 }
3222 }
3223
3224 /* Persistence cookies in passive, rewrite or insert mode have the
3225 * following form :
3226 *
3227 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
3228 *
3229 * For cookies in prefix mode, the form is :
3230 *
3231 * Cookie: NAME=SRV~VALUE
3232 */
3233 if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
3234 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
3235 struct server *srv = s->be->srv;
3236 char *delim;
3237
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003238 /* if we're in cookie prefix mode, we'll search the delimiter so that we
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003239 * have the server ID between val_beg and delim, and the original cookie between
3240 * delim+1 and val_end. Otherwise, delim==val_end :
3241 *
3242 * hdr_beg
3243 * |
3244 * v
3245 * NAME=SRV; # in all but prefix modes
3246 * NAME=SRV~OPAQUE ; # in prefix mode
3247 * || || | |+-> next
3248 * || || | +--> val_end
3249 * || || +---------> delim
3250 * || |+------------> val_beg
3251 * || +-------------> att_end = equal
3252 * |+-----------------> att_beg
3253 * +------------------> prev
3254 *
3255 */
3256 if (s->be->ck_opts & PR_CK_PFX) {
3257 for (delim = val_beg; delim < val_end; delim++)
3258 if (*delim == COOKIE_DELIM)
3259 break;
3260 }
3261 else {
3262 char *vbar1;
3263 delim = val_end;
3264 /* Now check if the cookie contains a date field, which would
3265 * appear after a vertical bar ('|') just after the server name
3266 * and before the delimiter.
3267 */
3268 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
3269 if (vbar1) {
3270 /* OK, so left of the bar is the server's cookie and
3271 * right is the last seen date. It is a base64 encoded
3272 * 30-bit value representing the UNIX date since the
3273 * epoch in 4-second quantities.
3274 */
3275 int val;
3276 delim = vbar1++;
3277 if (val_end - vbar1 >= 5) {
3278 val = b64tos30(vbar1);
3279 if (val > 0)
3280 txn->cookie_last_date = val << 2;
3281 }
3282 /* look for a second vertical bar */
3283 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
3284 if (vbar1 && (val_end - vbar1 > 5)) {
3285 val = b64tos30(vbar1 + 1);
3286 if (val > 0)
3287 txn->cookie_first_date = val << 2;
3288 }
3289 }
3290 }
3291
3292 /* if the cookie has an expiration date and the proxy wants to check
3293 * it, then we do that now. We first check if the cookie is too old,
3294 * then only if it has expired. We detect strict overflow because the
3295 * time resolution here is not great (4 seconds). Cookies with dates
3296 * in the future are ignored if their offset is beyond one day. This
3297 * allows an admin to fix timezone issues without expiring everyone
3298 * and at the same time avoids keeping unwanted side effects for too
3299 * long.
3300 */
3301 if (txn->cookie_first_date && s->be->cookie_maxlife &&
3302 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) ||
3303 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
3304 txn->flags &= ~TX_CK_MASK;
3305 txn->flags |= TX_CK_OLD;
3306 delim = val_beg; // let's pretend we have not found the cookie
3307 txn->cookie_first_date = 0;
3308 txn->cookie_last_date = 0;
3309 }
3310 else if (txn->cookie_last_date && s->be->cookie_maxidle &&
3311 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) ||
3312 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
3313 txn->flags &= ~TX_CK_MASK;
3314 txn->flags |= TX_CK_EXPIRED;
3315 delim = val_beg; // let's pretend we have not found the cookie
3316 txn->cookie_first_date = 0;
3317 txn->cookie_last_date = 0;
3318 }
3319
3320 /* Here, we'll look for the first running server which supports the cookie.
3321 * This allows to share a same cookie between several servers, for example
3322 * to dedicate backup servers to specific servers only.
3323 * However, to prevent clients from sticking to cookie-less backup server
3324 * when they have incidentely learned an empty cookie, we simply ignore
3325 * empty cookies and mark them as invalid.
3326 * The same behaviour is applied when persistence must be ignored.
3327 */
3328 if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
3329 srv = NULL;
3330
3331 while (srv) {
3332 if (srv->cookie && (srv->cklen == delim - val_beg) &&
3333 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
3334 if ((srv->cur_state != SRV_ST_STOPPED) ||
3335 (s->be->options & PR_O_PERSIST) ||
3336 (s->flags & SF_FORCE_PRST)) {
3337 /* we found the server and we can use it */
3338 txn->flags &= ~TX_CK_MASK;
3339 txn->flags |= (srv->cur_state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
3340 s->flags |= SF_DIRECT | SF_ASSIGNED;
3341 s->target = &srv->obj_type;
3342 break;
3343 } else {
3344 /* we found a server, but it's down,
3345 * mark it as such and go on in case
3346 * another one is available.
3347 */
3348 txn->flags &= ~TX_CK_MASK;
3349 txn->flags |= TX_CK_DOWN;
3350 }
3351 }
3352 srv = srv->next;
3353 }
3354
3355 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
3356 /* no server matched this cookie or we deliberately skipped it */
3357 txn->flags &= ~TX_CK_MASK;
3358 if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
3359 txn->flags |= TX_CK_UNUSED;
3360 else
3361 txn->flags |= TX_CK_INVALID;
3362 }
3363
3364 /* depending on the cookie mode, we may have to either :
3365 * - delete the complete cookie if we're in insert+indirect mode, so that
3366 * the server never sees it ;
3367 * - remove the server id from the cookie value, and tag the cookie as an
Joseph Herlante9d5c722018-11-25 11:00:25 -08003368 * application cookie so that it does not get accidentally removed later,
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003369 * if we're in cookie prefix mode
3370 */
3371 if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
3372 int delta; /* negative */
3373
3374 memmove(val_beg, delim + 1, hdr_end - (delim + 1));
3375 delta = val_beg - (delim + 1);
3376 val_end += delta;
3377 next += delta;
3378 hdr_end += delta;
3379 del_from = NULL;
3380 preserve_hdr = 1; /* we want to keep this cookie */
3381 }
3382 else if (del_from == NULL &&
3383 (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
3384 del_from = prev;
3385 }
3386 }
3387 else {
3388 /* This is not our cookie, so we must preserve it. But if we already
3389 * scheduled another cookie for removal, we cannot remove the
3390 * complete header, but we can remove the previous block itself.
3391 */
3392 preserve_hdr = 1;
3393
3394 if (del_from != NULL) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003395 int delta = http_del_hdr_value(hdr_beg, hdr_end, &del_from, prev);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003396 if (att_beg >= del_from)
3397 att_beg += delta;
3398 if (att_end >= del_from)
3399 att_end += delta;
3400 val_beg += delta;
3401 val_end += delta;
3402 next += delta;
3403 hdr_end += delta;
3404 prev = del_from;
3405 del_from = NULL;
3406 }
3407 }
3408
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003409 } /* for each cookie */
3410
3411
3412 /* There are no more cookies on this line.
3413 * We may still have one (or several) marked for deletion at the
3414 * end of the line. We must do this now in two ways :
3415 * - if some cookies must be preserved, we only delete from the
3416 * mark to the end of line ;
3417 * - if nothing needs to be preserved, simply delete the whole header
3418 */
3419 if (del_from) {
3420 hdr_end = (preserve_hdr ? del_from : hdr_beg);
3421 }
3422 if ((hdr_end - hdr_beg) != ctx.value.len) {
Christopher Faulet3e2638e2019-06-18 09:49:16 +02003423 if (hdr_beg != hdr_end)
3424 htx_change_blk_value_len(htx, ctx.blk, hdr_end - hdr_beg);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003425 else
3426 http_remove_header(htx, &ctx);
3427 }
3428 } /* for each "Cookie header */
3429}
3430
3431/*
3432 * Manage server-side cookies. It can impact performance by about 2% so it is
3433 * desirable to call it only when needed. This function is also used when we
3434 * just need to know if there is a cookie (eg: for check-cache).
3435 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003436static void http_manage_server_side_cookies(struct stream *s, struct channel *res)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003437{
3438 struct session *sess = s->sess;
3439 struct http_txn *txn = s->txn;
3440 struct htx *htx;
3441 struct http_hdr_ctx ctx;
3442 struct server *srv;
3443 char *hdr_beg, *hdr_end;
3444 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau6f7a02a2019-04-15 21:49:49 +02003445 int is_cookie2 = 0;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003446
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003447 htx = htxbuf(&res->buf);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003448
3449 ctx.blk = NULL;
3450 while (1) {
Olivier Houchardf0f42382019-07-22 17:43:46 +02003451 int is_first = 1;
3452
Andrew McDermottbfb15ab2022-02-11 18:26:49 +00003453 if (is_cookie2 || !http_find_header(htx, ist("Set-Cookie"), &ctx, 1)) {
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003454 if (!http_find_header(htx, ist("Set-Cookie2"), &ctx, 1))
3455 break;
3456 is_cookie2 = 1;
3457 }
3458
3459 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
3460 * <prev> points to the colon.
3461 */
3462 txn->flags |= TX_SCK_PRESENT;
3463
3464 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
3465 * check-cache is enabled) and we are not interested in checking
3466 * them. Warning, the cookie capture is declared in the frontend.
3467 */
3468 if (s->be->cookie_name == NULL && sess->fe->capture_name == NULL)
3469 break;
3470
3471 /* OK so now we know we have to process this response cookie.
3472 * The format of the Set-Cookie header is slightly different
3473 * from the format of the Cookie header in that it does not
3474 * support the comma as a cookie delimiter (thus the header
3475 * cannot be folded) because the Expires attribute described in
3476 * the original Netscape's spec may contain an unquoted date
3477 * with a comma inside. We have to live with this because
3478 * many browsers don't support Max-Age and some browsers don't
3479 * support quoted strings. However the Set-Cookie2 header is
3480 * clean.
3481 *
3482 * We have to keep multiple pointers in order to support cookie
3483 * removal at the beginning, middle or end of header without
3484 * corrupting the header (in case of set-cookie2). A special
3485 * pointer, <scav> points to the beginning of the set-cookie-av
3486 * fields after the first semi-colon. The <next> pointer points
3487 * either to the end of line (set-cookie) or next unquoted comma
3488 * (set-cookie2). All of these headers are valid :
3489 *
3490 * hdr_beg hdr_end
3491 * | |
3492 * v |
3493 * NAME1 = VALUE 1 ; Secure; Path="/" |
3494 * NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT v
3495 * NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT
3496 * NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard
3497 * | | | | | | | |
3498 * | | | | | | | +-> next
3499 * | | | | | | +------------> scav
3500 * | | | | | +--------------> val_end
3501 * | | | | +--------------------> val_beg
3502 * | | | +----------------------> equal
3503 * | | +------------------------> att_end
3504 * | +----------------------------> att_beg
3505 * +------------------------------> prev
3506 * -------------------------------> hdr_beg
3507 */
3508 hdr_beg = ctx.value.ptr;
3509 hdr_end = hdr_beg + ctx.value.len;
3510 for (prev = hdr_beg; prev < hdr_end; prev = next) {
3511
3512 /* Iterate through all cookies on this line */
3513
3514 /* find att_beg */
3515 att_beg = prev;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003516 if (!is_first)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003517 att_beg++;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003518 is_first = 0;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003519
3520 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
3521 att_beg++;
3522
3523 /* find att_end : this is the first character after the last non
3524 * space before the equal. It may be equal to hdr_end.
3525 */
3526 equal = att_end = att_beg;
3527
3528 while (equal < hdr_end) {
3529 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
3530 break;
3531 if (HTTP_IS_SPHT(*equal++))
3532 continue;
3533 att_end = equal;
3534 }
3535
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003536 /* here, <equal> points to '=', a delimiter or the end. <att_end>
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003537 * is between <att_beg> and <equal>, both may be identical.
3538 */
3539
3540 /* look for end of cookie if there is an equal sign */
3541 if (equal < hdr_end && *equal == '=') {
3542 /* look for the beginning of the value */
3543 val_beg = equal + 1;
3544 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
3545 val_beg++;
3546
3547 /* find the end of the value, respecting quotes */
3548 next = http_find_cookie_value_end(val_beg, hdr_end);
3549
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003550 /* make val_end point to the first white space or delimiter after the value */
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003551 val_end = next;
3552 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
3553 val_end--;
3554 }
3555 else {
3556 /* <equal> points to next comma, semi-colon or EOL */
3557 val_beg = val_end = next = equal;
3558 }
3559
3560 if (next < hdr_end) {
3561 /* Set-Cookie2 supports multiple cookies, and <next> points to
3562 * a colon or semi-colon before the end. So skip all attr-value
3563 * pairs and look for the next comma. For Set-Cookie, since
3564 * commas are permitted in values, skip to the end.
3565 */
3566 if (is_cookie2)
3567 next = http_find_hdr_value_end(next, hdr_end);
3568 else
3569 next = hdr_end;
3570 }
3571
3572 /* Now everything is as on the diagram above */
3573
3574 /* Ignore cookies with no equal sign */
3575 if (equal == val_end)
3576 continue;
3577
3578 /* If there are spaces around the equal sign, we need to
3579 * strip them otherwise we'll get trouble for cookie captures,
3580 * or even for rewrites. Since this happens extremely rarely,
3581 * it does not hurt performance.
3582 */
3583 if (unlikely(att_end != equal || val_beg > equal + 1)) {
3584 int stripped_before = 0;
3585 int stripped_after = 0;
3586
3587 if (att_end != equal) {
3588 memmove(att_end, equal, hdr_end - equal);
3589 stripped_before = (att_end - equal);
3590 equal += stripped_before;
3591 val_beg += stripped_before;
3592 }
3593
3594 if (val_beg > equal + 1) {
3595 memmove(equal + 1, val_beg, hdr_end + stripped_before - val_beg);
3596 stripped_after = (equal + 1) - val_beg;
3597 val_beg += stripped_after;
3598 stripped_before += stripped_after;
3599 }
3600
3601 val_end += stripped_before;
3602 next += stripped_before;
3603 hdr_end += stripped_before;
3604
Christopher Faulet3e2638e2019-06-18 09:49:16 +02003605 htx_change_blk_value_len(htx, ctx.blk, hdr_end - hdr_beg);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003606 ctx.value.len = hdr_end - hdr_beg;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003607 }
3608
3609 /* First, let's see if we want to capture this cookie. We check
3610 * that we don't already have a server side cookie, because we
3611 * can only capture one. Also as an optimisation, we ignore
3612 * cookies shorter than the declared name.
3613 */
3614 if (sess->fe->capture_name != NULL &&
3615 txn->srv_cookie == NULL &&
3616 (val_end - att_beg >= sess->fe->capture_namelen) &&
3617 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
3618 int log_len = val_end - att_beg;
3619 if ((txn->srv_cookie = pool_alloc(pool_head_capture)) == NULL) {
3620 ha_alert("HTTP logging : out of memory.\n");
3621 }
3622 else {
3623 if (log_len > sess->fe->capture_len)
3624 log_len = sess->fe->capture_len;
3625 memcpy(txn->srv_cookie, att_beg, log_len);
3626 txn->srv_cookie[log_len] = 0;
3627 }
3628 }
3629
3630 srv = objt_server(s->target);
3631 /* now check if we need to process it for persistence */
3632 if (!(s->flags & SF_IGNORE_PRST) &&
3633 (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
3634 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
3635 /* assume passive cookie by default */
3636 txn->flags &= ~TX_SCK_MASK;
3637 txn->flags |= TX_SCK_FOUND;
3638
3639 /* If the cookie is in insert mode on a known server, we'll delete
3640 * this occurrence because we'll insert another one later.
3641 * We'll delete it too if the "indirect" option is set and we're in
3642 * a direct access.
3643 */
3644 if (s->be->ck_opts & PR_CK_PSV) {
3645 /* The "preserve" flag was set, we don't want to touch the
3646 * server's cookie.
3647 */
3648 }
3649 else if ((srv && (s->be->ck_opts & PR_CK_INS)) ||
3650 ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) {
3651 /* this cookie must be deleted */
3652 if (prev == hdr_beg && next == hdr_end) {
3653 /* whole header */
3654 http_remove_header(htx, &ctx);
3655 /* note: while both invalid now, <next> and <hdr_end>
3656 * are still equal, so the for() will stop as expected.
3657 */
3658 } else {
3659 /* just remove the value */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003660 int delta = http_del_hdr_value(hdr_beg, hdr_end, &prev, next);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003661 next = prev;
3662 hdr_end += delta;
3663 }
3664 txn->flags &= ~TX_SCK_MASK;
3665 txn->flags |= TX_SCK_DELETED;
3666 /* and go on with next cookie */
3667 }
3668 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) {
3669 /* replace bytes val_beg->val_end with the cookie name associated
3670 * with this server since we know it.
3671 */
3672 int sliding, delta;
3673
3674 ctx.value = ist2(val_beg, val_end - val_beg);
3675 ctx.lws_before = ctx.lws_after = 0;
3676 http_replace_header_value(htx, &ctx, ist2(srv->cookie, srv->cklen));
3677 delta = srv->cklen - (val_end - val_beg);
3678 sliding = (ctx.value.ptr - val_beg);
3679 hdr_beg += sliding;
3680 val_beg += sliding;
3681 next += sliding + delta;
3682 hdr_end += sliding + delta;
3683
3684 txn->flags &= ~TX_SCK_MASK;
3685 txn->flags |= TX_SCK_REPLACED;
3686 }
3687 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) {
3688 /* insert the cookie name associated with this server
3689 * before existing cookie, and insert a delimiter between them..
3690 */
3691 int sliding, delta;
3692 ctx.value = ist2(val_beg, 0);
3693 ctx.lws_before = ctx.lws_after = 0;
3694 http_replace_header_value(htx, &ctx, ist2(srv->cookie, srv->cklen + 1));
3695 delta = srv->cklen + 1;
3696 sliding = (ctx.value.ptr - val_beg);
3697 hdr_beg += sliding;
3698 val_beg += sliding;
3699 next += sliding + delta;
3700 hdr_end += sliding + delta;
3701
3702 val_beg[srv->cklen] = COOKIE_DELIM;
3703 txn->flags &= ~TX_SCK_MASK;
3704 txn->flags |= TX_SCK_REPLACED;
3705 }
3706 }
3707 /* that's done for this cookie, check the next one on the same
3708 * line when next != hdr_end (only if is_cookie2).
3709 */
3710 }
3711 }
3712}
3713
Christopher Faulet25a02f62018-10-24 12:00:25 +02003714/*
3715 * Parses the Cache-Control and Pragma request header fields to determine if
3716 * the request may be served from the cache and/or if it is cacheable. Updates
3717 * s->txn->flags.
3718 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003719void http_check_request_for_cacheability(struct stream *s, struct channel *req)
Christopher Faulet25a02f62018-10-24 12:00:25 +02003720{
3721 struct http_txn *txn = s->txn;
3722 struct htx *htx;
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003723 struct http_hdr_ctx ctx = { .blk = NULL };
3724 int pragma_found, cc_found;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003725
3726 if ((txn->flags & (TX_CACHEABLE|TX_CACHE_IGNORE)) == TX_CACHE_IGNORE)
3727 return; /* nothing more to do here */
3728
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003729 htx = htxbuf(&req->buf);
Christopher Faulet25a02f62018-10-24 12:00:25 +02003730 pragma_found = cc_found = 0;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003731
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003732 /* Check "pragma" header for HTTP/1.0 compatibility. */
3733 if (http_find_header(htx, ist("pragma"), &ctx, 1)) {
3734 if (isteqi(ctx.value, ist("no-cache"))) {
3735 pragma_found = 1;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003736 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003737 }
Christopher Faulet25a02f62018-10-24 12:00:25 +02003738
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003739 ctx.blk = NULL;
3740 /* Don't use the cache and don't try to store if we found the
3741 * Authorization header */
3742 if (http_find_header(htx, ist("authorization"), &ctx, 1)) {
3743 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3744 txn->flags |= TX_CACHE_IGNORE;
3745 }
Christopher Faulet25a02f62018-10-24 12:00:25 +02003746
Christopher Faulet25a02f62018-10-24 12:00:25 +02003747
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003748 /* Look for "cache-control" header and iterate over all the values
3749 * until we find one that specifies that caching is possible or not. */
3750 ctx.blk = NULL;
3751 while (http_find_header(htx, ist("cache-control"), &ctx, 0)) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003752 cc_found = 1;
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003753 /* We don't check the values after max-age, max-stale nor min-fresh,
3754 * we simply don't use the cache when they're specified. */
3755 if (istmatchi(ctx.value, ist("max-age")) ||
3756 istmatchi(ctx.value, ist("no-cache")) ||
3757 istmatchi(ctx.value, ist("max-stale")) ||
3758 istmatchi(ctx.value, ist("min-fresh"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003759 txn->flags |= TX_CACHE_IGNORE;
3760 continue;
3761 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003762 if (istmatchi(ctx.value, ist("no-store"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003763 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3764 continue;
3765 }
3766 }
3767
3768 /* RFC7234#5.4:
3769 * When the Cache-Control header field is also present and
3770 * understood in a request, Pragma is ignored.
3771 * When the Cache-Control header field is not present in a
3772 * request, caches MUST consider the no-cache request
3773 * pragma-directive as having the same effect as if
3774 * "Cache-Control: no-cache" were present.
3775 */
3776 if (!cc_found && pragma_found)
3777 txn->flags |= TX_CACHE_IGNORE;
3778}
3779
3780/*
3781 * Check if response is cacheable or not. Updates s->txn->flags.
3782 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003783void http_check_response_for_cacheability(struct stream *s, struct channel *res)
Christopher Faulet25a02f62018-10-24 12:00:25 +02003784{
3785 struct http_txn *txn = s->txn;
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003786 struct http_hdr_ctx ctx = { .blk = NULL };
Christopher Faulet25a02f62018-10-24 12:00:25 +02003787 struct htx *htx;
Remi Tricot-Le Bretoncc9bf2e2020-11-12 11:14:41 +01003788 int has_freshness_info = 0;
3789 int has_validator = 0;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003790
3791 if (txn->status < 200) {
3792 /* do not try to cache interim responses! */
3793 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3794 return;
3795 }
3796
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003797 htx = htxbuf(&res->buf);
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003798 /* Check "pragma" header for HTTP/1.0 compatibility. */
3799 if (http_find_header(htx, ist("pragma"), &ctx, 1)) {
3800 if (isteqi(ctx.value, ist("no-cache"))) {
3801 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3802 return;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003803 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003804 }
Christopher Faulet25a02f62018-10-24 12:00:25 +02003805
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003806 /* Look for "cache-control" header and iterate over all the values
3807 * until we find one that specifies that caching is possible or not. */
3808 ctx.blk = NULL;
3809 while (http_find_header(htx, ist("cache-control"), &ctx, 0)) {
3810 if (isteqi(ctx.value, ist("public"))) {
3811 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003812 continue;
3813 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003814 if (isteqi(ctx.value, ist("private")) ||
3815 isteqi(ctx.value, ist("no-cache")) ||
3816 isteqi(ctx.value, ist("no-store")) ||
3817 isteqi(ctx.value, ist("max-age=0")) ||
3818 isteqi(ctx.value, ist("s-maxage=0"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003819 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003820 continue;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003821 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003822 /* We might have a no-cache="set-cookie" form. */
3823 if (istmatchi(ctx.value, ist("no-cache=\"set-cookie"))) {
3824 txn->flags &= ~TX_CACHE_COOK;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003825 continue;
3826 }
Remi Tricot-Le Bretoncc9bf2e2020-11-12 11:14:41 +01003827
3828 if (istmatchi(ctx.value, ist("s-maxage")) ||
3829 istmatchi(ctx.value, ist("max-age"))) {
3830 has_freshness_info = 1;
3831 continue;
3832 }
3833 }
3834
3835 /* If no freshness information could be found in Cache-Control values,
3836 * look for an Expires header. */
3837 if (!has_freshness_info) {
3838 ctx.blk = NULL;
3839 has_freshness_info = http_find_header(htx, ist("expires"), &ctx, 0);
Christopher Faulet25a02f62018-10-24 12:00:25 +02003840 }
Remi Tricot-Le Bretoncc9bf2e2020-11-12 11:14:41 +01003841
3842 /* If no freshness information could be found in Cache-Control or Expires
3843 * values, look for an explicit validator. */
3844 if (!has_freshness_info) {
3845 ctx.blk = NULL;
3846 has_validator = 1;
3847 if (!http_find_header(htx, ist("etag"), &ctx, 0)) {
3848 ctx.blk = NULL;
3849 if (!http_find_header(htx, ist("last-modified"), &ctx, 0))
3850 has_validator = 0;
3851 }
3852 }
3853
3854 /* We won't store an entry that has neither a cache validator nor an
3855 * explicit expiration time, as suggested in RFC 7234#3. */
3856 if (!has_freshness_info && !has_validator)
3857 txn->flags |= TX_CACHE_IGNORE;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003858}
3859
Christopher Faulet377c5a52018-10-24 21:21:30 +02003860/*
3861 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
3862 * for the current backend.
3863 *
3864 * It is assumed that the request is either a HEAD, GET, or POST and that the
3865 * uri_auth field is valid.
3866 *
3867 * Returns 1 if stats should be provided, otherwise 0.
3868 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003869static int http_stats_check_uri(struct stream *s, struct http_txn *txn, struct proxy *backend)
Christopher Faulet377c5a52018-10-24 21:21:30 +02003870{
3871 struct uri_auth *uri_auth = backend->uri_auth;
3872 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003873 struct htx_sl *sl;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003874 struct ist uri;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003875
3876 if (!uri_auth)
3877 return 0;
3878
3879 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
3880 return 0;
3881
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003882 htx = htxbuf(&s->req.buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02003883 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003884 uri = htx_sl_req_uri(sl);
Amaury Denoyellec453f952021-07-06 11:40:12 +02003885 if (*uri_auth->uri_prefix == '/') {
3886 struct http_uri_parser parser = http_uri_parser_init(uri);
3887 uri = http_parse_path(&parser);
3888 }
Christopher Faulet377c5a52018-10-24 21:21:30 +02003889
3890 /* check URI size */
3891 if (uri_auth->uri_len > uri.len)
3892 return 0;
3893
3894 if (memcmp(uri.ptr, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
3895 return 0;
3896
3897 return 1;
3898}
3899
3900/* This function prepares an applet to handle the stats. It can deal with the
3901 * "100-continue" expectation, check that admin rules are met for POST requests,
3902 * and program a response message if something was unexpected. It cannot fail
3903 * and always relies on the stats applet to complete the job. It does not touch
3904 * analysers nor counters, which are left to the caller. It does not touch
3905 * s->target which is supposed to already point to the stats applet. The caller
3906 * is expected to have already assigned an appctx to the stream.
3907 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003908static int http_handle_stats(struct stream *s, struct channel *req)
Christopher Faulet377c5a52018-10-24 21:21:30 +02003909{
3910 struct stats_admin_rule *stats_admin_rule;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003911 struct session *sess = s->sess;
3912 struct http_txn *txn = s->txn;
3913 struct http_msg *msg = &txn->req;
3914 struct uri_auth *uri_auth = s->be->uri_auth;
3915 const char *h, *lookup, *end;
Christopher Faulet693b23b2022-02-28 09:09:05 +01003916 struct appctx *appctx = __cs_appctx(s->csb);
Christopher Faulet377c5a52018-10-24 21:21:30 +02003917 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003918 struct htx_sl *sl;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003919
Christopher Faulet377c5a52018-10-24 21:21:30 +02003920 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
3921 appctx->st1 = appctx->st2 = 0;
3922 appctx->ctx.stats.st_code = STAT_STATUS_INIT;
Willy Tarreau676c29e2019-10-09 10:50:01 +02003923 appctx->ctx.stats.flags |= uri_auth->flags;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003924 appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */
3925 if ((msg->flags & HTTP_MSGF_VER_11) && (txn->meth != HTTP_METH_HEAD))
3926 appctx->ctx.stats.flags |= STAT_CHUNKED;
3927
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003928 htx = htxbuf(&req->buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02003929 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003930 lookup = HTX_SL_REQ_UPTR(sl) + uri_auth->uri_len;
3931 end = HTX_SL_REQ_UPTR(sl) + HTX_SL_REQ_ULEN(sl);
Christopher Faulet377c5a52018-10-24 21:21:30 +02003932
3933 for (h = lookup; h <= end - 3; h++) {
3934 if (memcmp(h, ";up", 3) == 0) {
3935 appctx->ctx.stats.flags |= STAT_HIDE_DOWN;
3936 break;
3937 }
Amaury Denoyelle91e55ea2021-02-25 14:46:08 +01003938 }
3939
3940 for (h = lookup; h <= end - 9; h++) {
3941 if (memcmp(h, ";no-maint", 9) == 0) {
Willy Tarreau3e320362020-10-23 17:28:57 +02003942 appctx->ctx.stats.flags |= STAT_HIDE_MAINT;
3943 break;
3944 }
Christopher Faulet377c5a52018-10-24 21:21:30 +02003945 }
3946
3947 if (uri_auth->refresh) {
3948 for (h = lookup; h <= end - 10; h++) {
3949 if (memcmp(h, ";norefresh", 10) == 0) {
3950 appctx->ctx.stats.flags |= STAT_NO_REFRESH;
3951 break;
3952 }
3953 }
3954 }
3955
3956 for (h = lookup; h <= end - 4; h++) {
3957 if (memcmp(h, ";csv", 4) == 0) {
Christopher Faulet6338a082019-09-09 15:50:54 +02003958 appctx->ctx.stats.flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
Christopher Faulet377c5a52018-10-24 21:21:30 +02003959 break;
3960 }
3961 }
3962
3963 for (h = lookup; h <= end - 6; h++) {
3964 if (memcmp(h, ";typed", 6) == 0) {
Christopher Faulet6338a082019-09-09 15:50:54 +02003965 appctx->ctx.stats.flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
Christopher Faulet377c5a52018-10-24 21:21:30 +02003966 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
3967 break;
3968 }
3969 }
3970
Christopher Faulet6338a082019-09-09 15:50:54 +02003971 for (h = lookup; h <= end - 5; h++) {
3972 if (memcmp(h, ";json", 5) == 0) {
3973 appctx->ctx.stats.flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
3974 appctx->ctx.stats.flags |= STAT_FMT_JSON;
3975 break;
3976 }
3977 }
3978
3979 for (h = lookup; h <= end - 12; h++) {
3980 if (memcmp(h, ";json-schema", 12) == 0) {
3981 appctx->ctx.stats.flags &= ~STAT_FMT_MASK;
3982 appctx->ctx.stats.flags |= STAT_JSON_SCHM;
3983 break;
3984 }
3985 }
3986
Christopher Faulet377c5a52018-10-24 21:21:30 +02003987 for (h = lookup; h <= end - 8; h++) {
3988 if (memcmp(h, ";st=", 4) == 0) {
3989 int i;
3990 h += 4;
3991 appctx->ctx.stats.st_code = STAT_STATUS_UNKN;
3992 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
3993 if (strncmp(stat_status_codes[i], h, 4) == 0) {
3994 appctx->ctx.stats.st_code = i;
3995 break;
3996 }
3997 }
3998 break;
3999 }
4000 }
4001
4002 appctx->ctx.stats.scope_str = 0;
4003 appctx->ctx.stats.scope_len = 0;
4004 for (h = lookup; h <= end - 8; h++) {
4005 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
4006 int itx = 0;
4007 const char *h2;
4008 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
4009 const char *err;
4010
4011 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
4012 h2 = h;
Christopher Fauleted7a0662019-01-14 11:07:34 +01004013 appctx->ctx.stats.scope_str = h2 - HTX_SL_REQ_UPTR(sl);
4014 while (h < end) {
Christopher Faulet377c5a52018-10-24 21:21:30 +02004015 if (*h == ';' || *h == '&' || *h == ' ')
4016 break;
4017 itx++;
4018 h++;
4019 }
4020
4021 if (itx > STAT_SCOPE_TXT_MAXLEN)
4022 itx = STAT_SCOPE_TXT_MAXLEN;
4023 appctx->ctx.stats.scope_len = itx;
4024
4025 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
4026 memcpy(scope_txt, h2, itx);
4027 scope_txt[itx] = '\0';
4028 err = invalid_char(scope_txt);
4029 if (err) {
4030 /* bad char in search text => clear scope */
4031 appctx->ctx.stats.scope_str = 0;
4032 appctx->ctx.stats.scope_len = 0;
4033 }
4034 break;
4035 }
4036 }
4037
4038 /* now check whether we have some admin rules for this request */
4039 list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) {
4040 int ret = 1;
4041
4042 if (stats_admin_rule->cond) {
4043 ret = acl_exec_cond(stats_admin_rule->cond, s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
4044 ret = acl_pass(ret);
4045 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
4046 ret = !ret;
4047 }
4048
4049 if (ret) {
4050 /* no rule, or the rule matches */
4051 appctx->ctx.stats.flags |= STAT_ADMIN;
4052 break;
4053 }
4054 }
4055
Christopher Faulet5d45e382019-02-27 15:15:23 +01004056 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
4057 appctx->st0 = STAT_HTTP_HEAD;
4058 else if (txn->meth == HTTP_METH_POST) {
Christopher Fauletbd9e8422019-08-15 22:26:48 +02004059 if (appctx->ctx.stats.flags & STAT_ADMIN) {
Christopher Faulet377c5a52018-10-24 21:21:30 +02004060 appctx->st0 = STAT_HTTP_POST;
Christopher Fauletbd9e8422019-08-15 22:26:48 +02004061 if (msg->msg_state < HTTP_MSG_DATA)
4062 req->analysers |= AN_REQ_HTTP_BODY;
4063 }
Christopher Faulet377c5a52018-10-24 21:21:30 +02004064 else {
Christopher Faulet5d45e382019-02-27 15:15:23 +01004065 /* POST without admin level */
Christopher Faulet377c5a52018-10-24 21:21:30 +02004066 appctx->ctx.stats.flags &= ~STAT_CHUNKED;
4067 appctx->ctx.stats.st_code = STAT_STATUS_DENY;
4068 appctx->st0 = STAT_HTTP_LAST;
4069 }
4070 }
4071 else {
Christopher Faulet5d45e382019-02-27 15:15:23 +01004072 /* Unsupported method */
4073 appctx->ctx.stats.flags &= ~STAT_CHUNKED;
4074 appctx->ctx.stats.st_code = STAT_STATUS_IVAL;
4075 appctx->st0 = STAT_HTTP_LAST;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004076 }
4077
4078 s->task->nice = -32; /* small boost for HTTP statistics */
4079 return 1;
4080}
4081
Christopher Faulet021a8e42021-03-29 10:46:38 +02004082/* This function waits for the message payload at most <time> milliseconds (may
4083 * be set to TICK_ETERNITY). It stops to wait if at least <bytes> bytes of the
4084 * payload are received (0 means no limit). It returns HTTP_RULE_* depending on
4085 * the result:
4086 *
4087 * - HTTP_RULE_RES_CONT when conditions are met to stop waiting
4088 * - HTTP_RULE_RES_YIELD to wait for more data
Ilya Shipitsinb2be9a12021-04-24 13:25:42 +05004089 * - HTTP_RULE_RES_ABRT when a timeout occurred.
Christopher Faulet021a8e42021-03-29 10:46:38 +02004090 * - HTTP_RULE_RES_BADREQ if a parsing error is raised by lower level
Ilya Shipitsinb2be9a12021-04-24 13:25:42 +05004091 * - HTTP_RULE_RES_ERROR if an internal error occurred
Christopher Faulet021a8e42021-03-29 10:46:38 +02004092 *
Ilya Shipitsinb2be9a12021-04-24 13:25:42 +05004093 * If a timeout occurred, this function is responsible to emit the right response
Christopher Faulet021a8e42021-03-29 10:46:38 +02004094 * to the client, depending on the channel (408 on request side, 504 on response
4095 * side). All other errors must be handled by the caller.
4096 */
4097enum rule_result http_wait_for_msg_body(struct stream *s, struct channel *chn,
4098 unsigned int time, unsigned int bytes)
4099{
4100 struct session *sess = s->sess;
4101 struct http_txn *txn = s->txn;
4102 struct http_msg *msg = ((chn->flags & CF_ISRESP) ? &txn->rsp : &txn->req);
4103 struct htx *htx;
4104 enum rule_result ret = HTTP_RULE_RES_CONT;
4105
4106 htx = htxbuf(&chn->buf);
4107
4108 if (htx->flags & HTX_FL_PARSING_ERROR) {
4109 ret = HTTP_RULE_RES_BADREQ;
4110 goto end;
4111 }
4112 if (htx->flags & HTX_FL_PROCESSING_ERROR) {
4113 ret = HTTP_RULE_RES_ERROR;
4114 goto end;
4115 }
4116
4117 /* Do nothing for bodyless and CONNECT requests */
4118 if (txn->meth == HTTP_METH_CONNECT || (msg->flags & HTTP_MSGF_BODYLESS))
4119 goto end;
4120
4121 if (!(chn->flags & CF_ISRESP) && msg->msg_state < HTTP_MSG_DATA) {
4122 if (http_handle_expect_hdr(s, htx, msg) == -1) {
4123 ret = HTTP_RULE_RES_ERROR;
4124 goto end;
4125 }
4126 }
4127
4128 msg->msg_state = HTTP_MSG_DATA;
4129
4130 /* Now we're in HTTP_MSG_DATA. We just need to know if all data have
4131 * been received or if the buffer is full.
4132 */
Christopher Faulet78335962021-09-23 14:46:32 +02004133 if ((htx->flags & HTX_FL_EOM) ||
4134 htx_get_tail_type(htx) > HTX_BLK_DATA ||
4135 channel_htx_full(chn, htx, global.tune.maxrewrite) ||
4136 si_rx_blocked_room(chn_prod(chn)))
Christopher Faulet021a8e42021-03-29 10:46:38 +02004137 goto end;
4138
4139 if (bytes) {
4140 struct htx_blk *blk;
4141 unsigned int len = 0;
4142
4143 for (blk = htx_get_first_blk(htx); blk; blk = htx_get_next_blk(htx, blk)) {
4144 if (htx_get_blk_type(blk) != HTX_BLK_DATA)
4145 continue;
4146 len += htx_get_blksz(blk);
4147 if (len >= bytes)
4148 goto end;
4149 }
4150 }
4151
4152 if ((chn->flags & CF_READ_TIMEOUT) || tick_is_expired(chn->analyse_exp, now_ms)) {
4153 if (!(chn->flags & CF_ISRESP))
4154 goto abort_req;
4155 goto abort_res;
4156 }
4157
4158 /* we get here if we need to wait for more data */
4159 if (!(chn->flags & (CF_SHUTR | CF_READ_ERROR))) {
4160 if (!tick_isset(chn->analyse_exp))
4161 chn->analyse_exp = tick_add_ifset(now_ms, time);
4162 ret = HTTP_RULE_RES_YIELD;
4163 }
4164
4165 end:
4166 return ret;
4167
4168 abort_req:
4169 txn->status = 408;
4170 if (!(s->flags & SF_ERR_MASK))
4171 s->flags |= SF_ERR_CLITO;
4172 if (!(s->flags & SF_FINST_MASK))
4173 s->flags |= SF_FINST_D;
Willy Tarreau4781b152021-04-06 13:53:36 +02004174 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req);
Christopher Faulet021a8e42021-03-29 10:46:38 +02004175 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02004176 _HA_ATOMIC_INC(&sess->listener->counters->failed_req);
Christopher Faulet021a8e42021-03-29 10:46:38 +02004177 http_reply_and_close(s, txn->status, http_error_message(s));
4178 ret = HTTP_RULE_RES_ABRT;
4179 goto end;
4180
4181 abort_res:
4182 txn->status = 504;
4183 if (!(s->flags & SF_ERR_MASK))
4184 s->flags |= SF_ERR_SRVTO;
4185 if (!(s->flags & SF_FINST_MASK))
4186 s->flags |= SF_FINST_D;
4187 stream_inc_http_fail_ctr(s);
4188 http_reply_and_close(s, txn->status, http_error_message(s));
4189 ret = HTTP_RULE_RES_ABRT;
4190 goto end;
4191}
4192
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004193void http_perform_server_redirect(struct stream *s, struct stream_interface *si)
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004194{
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004195 struct channel *req = &s->req;
4196 struct channel *res = &s->res;
4197 struct server *srv;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004198 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004199 struct htx_sl *sl;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004200 struct ist path, location;
4201 unsigned int flags;
Amaury Denoyellec453f952021-07-06 11:40:12 +02004202 struct http_uri_parser parser;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004203
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004204 /*
4205 * Create the location
4206 */
4207 chunk_reset(&trash);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004208
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004209 /* 1: add the server's prefix */
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004210 /* special prefix "/" means don't change URL */
4211 srv = __objt_server(s->target);
4212 if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') {
4213 if (!chunk_memcat(&trash, srv->rdr_pfx, srv->rdr_len))
4214 return;
4215 }
4216
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004217 /* 2: add the request Path */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004218 htx = htxbuf(&req->buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02004219 sl = http_get_stline(htx);
Amaury Denoyellec453f952021-07-06 11:40:12 +02004220 parser = http_uri_parser_init(htx_sl_req_uri(sl));
4221 path = http_parse_path(&parser);
Tim Duesterhused526372020-03-05 17:56:33 +01004222 if (!isttest(path))
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004223 return;
4224
4225 if (!chunk_memcat(&trash, path.ptr, path.len))
4226 return;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004227 location = ist2(trash.area, trash.data);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004228
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004229 /*
4230 * Create the 302 respone
4231 */
4232 htx = htx_from_buf(&res->buf);
4233 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
4234 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
4235 ist("HTTP/1.1"), ist("302"), ist("Found"));
4236 if (!sl)
4237 goto fail;
4238 sl->info.res.status = 302;
4239 s->txn->status = 302;
4240
4241 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) ||
4242 !htx_add_header(htx, ist("Connection"), ist("close")) ||
4243 !htx_add_header(htx, ist("Content-length"), ist("0")) ||
4244 !htx_add_header(htx, ist("Location"), location))
4245 goto fail;
4246
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004247 if (!htx_add_endof(htx, HTX_BLK_EOH))
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004248 goto fail;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004249
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004250 htx->flags |= HTX_FL_EOM;
Christopher Fauletc20afb82020-01-24 19:16:26 +01004251 htx_to_buf(htx, &res->buf);
Christopher Fauleta72a7e42020-01-28 09:28:11 +01004252 if (!http_forward_proxy_resp(s, 1))
4253 goto fail;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004254
4255 /* return without error. */
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004256 si_shutr(si);
4257 si_shutw(si);
4258 si->err_type = SI_ET_NONE;
4259 si->state = SI_ST_CLO;
4260
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004261 if (!(s->flags & SF_ERR_MASK))
4262 s->flags |= SF_ERR_LOCAL;
4263 if (!(s->flags & SF_FINST_MASK))
4264 s->flags |= SF_FINST_C;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004265
4266 /* FIXME: we should increase a counter of redirects per server and per backend. */
4267 srv_inc_sess_ctr(srv);
4268 srv_set_sess_last(srv);
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004269 return;
4270
4271 fail:
4272 /* If an error occurred, remove the incomplete HTTP response from the
4273 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004274 channel_htx_truncate(res, htx);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004275}
4276
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05004277/* This function terminates the request because it was completely analyzed or
Christopher Fauletf2824e62018-10-01 12:12:37 +02004278 * because an error was triggered during the body forwarding.
4279 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004280static void http_end_request(struct stream *s)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004281{
4282 struct channel *chn = &s->req;
4283 struct http_txn *txn = s->txn;
4284
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004285 DBG_TRACE_ENTER(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004286
Christopher Fauletb42a8b62018-11-19 21:59:00 +01004287 if (unlikely(txn->req.msg_state == HTTP_MSG_ERROR ||
4288 txn->rsp.msg_state == HTTP_MSG_ERROR)) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004289 channel_abort(chn);
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004290 channel_htx_truncate(chn, htxbuf(&chn->buf));
Christopher Fauletf2824e62018-10-01 12:12:37 +02004291 goto end;
4292 }
4293
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004294 if (unlikely(txn->req.msg_state < HTTP_MSG_DONE)) {
4295 DBG_TRACE_DEVEL("waiting end of the request", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004296 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004297 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004298
4299 if (txn->req.msg_state == HTTP_MSG_DONE) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004300 /* No need to read anymore, the request was completely parsed.
4301 * We can shut the read side unless we want to abort_on_close,
4302 * or we have a POST request. The issue with POST requests is
4303 * that some browsers still send a CRLF after the request, and
4304 * this CRLF must be read so that it does not remain in the kernel
4305 * buffers, otherwise a close could cause an RST on some systems
4306 * (eg: Linux).
4307 */
Christopher Faulet769d0e92019-03-22 14:23:18 +01004308 if (!(s->be->options & PR_O_ABRT_CLOSE) && txn->meth != HTTP_METH_POST)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004309 channel_dont_read(chn);
4310
4311 /* if the server closes the connection, we want to immediately react
4312 * and close the socket to save packets and syscalls.
4313 */
Christopher Fauleta6294472021-12-23 13:25:57 +01004314 cs_si(s->csb)->flags |= SI_FL_NOHALF;
Christopher Fauletf2824e62018-10-01 12:12:37 +02004315
4316 /* In any case we've finished parsing the request so we must
4317 * disable Nagle when sending data because 1) we're not going
4318 * to shut this side, and 2) the server is waiting for us to
4319 * send pending data.
4320 */
4321 chn->flags |= CF_NEVER_WAIT;
4322
Christopher Fauletd01ce402019-01-02 17:44:13 +01004323 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
4324 /* The server has not finished to respond, so we
4325 * don't want to move in order not to upset it.
4326 */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004327 DBG_TRACE_DEVEL("waiting end of the response", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletd01ce402019-01-02 17:44:13 +01004328 return;
4329 }
4330
Christopher Fauletf2824e62018-10-01 12:12:37 +02004331 /* When we get here, it means that both the request and the
4332 * response have finished receiving. Depending on the connection
4333 * mode, we'll have to wait for the last bytes to leave in either
4334 * direction, and sometimes for a close to be effective.
4335 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02004336 if (txn->flags & TX_CON_WANT_TUN) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004337 /* Tunnel mode will not have any analyser so it needs to
4338 * poll for reads.
4339 */
4340 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004341 if (b_data(&chn->buf)) {
4342 DBG_TRACE_DEVEL("waiting to flush the request", STRM_EV_HTTP_ANA, s, txn);
Christopher Faulet9768c262018-10-22 09:34:31 +02004343 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004344 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004345 txn->req.msg_state = HTTP_MSG_TUNNEL;
4346 }
4347 else {
4348 /* we're not expecting any new data to come for this
4349 * transaction, so we can close it.
Christopher Faulet9768c262018-10-22 09:34:31 +02004350 *
4351 * However, there is an exception if the response
4352 * length is undefined. In this case, we need to wait
4353 * the close from the server. The response will be
4354 * switched in TUNNEL mode until the end.
Christopher Fauletf2824e62018-10-01 12:12:37 +02004355 */
4356 if (!(txn->rsp.flags & HTTP_MSGF_XFER_LEN) &&
4357 txn->rsp.msg_state != HTTP_MSG_CLOSED)
Christopher Faulet9768c262018-10-22 09:34:31 +02004358 goto check_channel_flags;
Christopher Fauletf2824e62018-10-01 12:12:37 +02004359
4360 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4361 channel_shutr_now(chn);
4362 channel_shutw_now(chn);
4363 }
4364 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004365 goto check_channel_flags;
4366 }
4367
4368 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
4369 http_msg_closing:
4370 /* nothing else to forward, just waiting for the output buffer
4371 * to be empty and for the shutw_now to take effect.
4372 */
4373 if (channel_is_empty(chn)) {
4374 txn->req.msg_state = HTTP_MSG_CLOSED;
4375 goto http_msg_closed;
4376 }
4377 else if (chn->flags & CF_SHUTW) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004378 txn->req.msg_state = HTTP_MSG_ERROR;
4379 goto end;
4380 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004381 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004382 return;
4383 }
4384
4385 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
4386 http_msg_closed:
Christopher Fauletf2824e62018-10-01 12:12:37 +02004387 /* if we don't know whether the server will close, we need to hard close */
4388 if (txn->rsp.flags & HTTP_MSGF_XFER_LEN)
Christopher Fauleta6294472021-12-23 13:25:57 +01004389 cs_si(s->csb)->flags |= SI_FL_NOLINGER; /* we want to close ASAP */
Christopher Fauletf2824e62018-10-01 12:12:37 +02004390 /* see above in MSG_DONE why we only do this in these states */
Christopher Faulet769d0e92019-03-22 14:23:18 +01004391 if (!(s->be->options & PR_O_ABRT_CLOSE))
Christopher Fauletf2824e62018-10-01 12:12:37 +02004392 channel_dont_read(chn);
4393 goto end;
4394 }
4395
4396 check_channel_flags:
4397 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4398 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4399 /* if we've just closed an output, let's switch */
4400 txn->req.msg_state = HTTP_MSG_CLOSING;
4401 goto http_msg_closing;
4402 }
4403
4404 end:
4405 chn->analysers &= AN_REQ_FLT_END;
Christopher Faulet198ef8b2020-12-15 13:32:55 +01004406 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
4407 chn->flags |= CF_NEVER_WAIT;
4408 if (HAS_REQ_DATA_FILTERS(s))
Christopher Fauletf2824e62018-10-01 12:12:37 +02004409 chn->analysers |= AN_REQ_FLT_XFER_DATA;
Christopher Faulet198ef8b2020-12-15 13:32:55 +01004410 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004411 channel_auto_close(chn);
4412 channel_auto_read(chn);
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}
4415
4416
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05004417/* This function terminates the response because it was completely analyzed or
Christopher Fauletf2824e62018-10-01 12:12:37 +02004418 * because an error was triggered during the body forwarding.
4419 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004420static void http_end_response(struct stream *s)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004421{
4422 struct channel *chn = &s->res;
4423 struct http_txn *txn = s->txn;
4424
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004425 DBG_TRACE_ENTER(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004426
Christopher Fauletb42a8b62018-11-19 21:59:00 +01004427 if (unlikely(txn->req.msg_state == HTTP_MSG_ERROR ||
4428 txn->rsp.msg_state == HTTP_MSG_ERROR)) {
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004429 channel_htx_truncate(&s->req, htxbuf(&s->req.buf));
Christopher Faulet9768c262018-10-22 09:34:31 +02004430 channel_abort(&s->req);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004431 goto end;
4432 }
4433
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004434 if (unlikely(txn->rsp.msg_state < HTTP_MSG_DONE)) {
4435 DBG_TRACE_DEVEL("waiting end of the response", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004436 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004437 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004438
4439 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
4440 /* In theory, we don't need to read anymore, but we must
4441 * still monitor the server connection for a possible close
4442 * while the request is being uploaded, so we don't disable
4443 * reading.
4444 */
4445 /* channel_dont_read(chn); */
4446
4447 if (txn->req.msg_state < HTTP_MSG_DONE) {
4448 /* The client seems to still be sending data, probably
4449 * because we got an error response during an upload.
4450 * We have the choice of either breaking the connection
4451 * or letting it pass through. Let's do the later.
4452 */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004453 DBG_TRACE_DEVEL("waiting end of the request", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004454 return;
4455 }
4456
4457 /* When we get here, it means that both the request and the
4458 * response have finished receiving. Depending on the connection
4459 * mode, we'll have to wait for the last bytes to leave in either
4460 * direction, and sometimes for a close to be effective.
4461 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02004462 if (txn->flags & TX_CON_WANT_TUN) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004463 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004464 if (b_data(&chn->buf)) {
4465 DBG_TRACE_DEVEL("waiting to flush the respone", STRM_EV_HTTP_ANA, s, txn);
Christopher Faulet9768c262018-10-22 09:34:31 +02004466 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004467 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004468 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
4469 }
4470 else {
4471 /* we're not expecting any new data to come for this
4472 * transaction, so we can close it.
4473 */
4474 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4475 channel_shutr_now(chn);
4476 channel_shutw_now(chn);
4477 }
4478 }
4479 goto check_channel_flags;
4480 }
4481
4482 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
4483 http_msg_closing:
4484 /* nothing else to forward, just waiting for the output buffer
4485 * to be empty and for the shutw_now to take effect.
4486 */
4487 if (channel_is_empty(chn)) {
4488 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4489 goto http_msg_closed;
4490 }
4491 else if (chn->flags & CF_SHUTW) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004492 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau4781b152021-04-06 13:53:36 +02004493 _HA_ATOMIC_INC(&strm_sess(s)->fe->fe_counters.cli_aborts);
4494 _HA_ATOMIC_INC(&s->be->be_counters.cli_aborts);
William Lallemand36119de2021-03-08 15:26:48 +01004495 if (strm_sess(s)->listener && strm_sess(s)->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02004496 _HA_ATOMIC_INC(&strm_sess(s)->listener->counters->cli_aborts);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004497 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02004498 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.cli_aborts);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004499 goto end;
4500 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004501 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004502 return;
4503 }
4504
4505 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
4506 http_msg_closed:
4507 /* drop any pending data */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004508 channel_htx_truncate(&s->req, htxbuf(&s->req.buf));
Christopher Faulet9768c262018-10-22 09:34:31 +02004509 channel_abort(&s->req);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004510 goto end;
4511 }
4512
4513 check_channel_flags:
4514 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4515 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4516 /* if we've just closed an output, let's switch */
4517 txn->rsp.msg_state = HTTP_MSG_CLOSING;
4518 goto http_msg_closing;
4519 }
4520
4521 end:
4522 chn->analysers &= AN_RES_FLT_END;
Christopher Faulet198ef8b2020-12-15 13:32:55 +01004523 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
4524 chn->flags |= CF_NEVER_WAIT;
4525 if (HAS_RSP_DATA_FILTERS(s))
4526 chn->analysers |= AN_RES_FLT_XFER_DATA;
4527 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004528 channel_auto_close(chn);
4529 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004530 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004531}
4532
Christopher Fauletef70e252020-01-28 09:26:19 +01004533/* Forward a response generated by HAProxy (error/redirect/return). This
4534 * function forwards all pending incoming data. If <final> is set to 0, nothing
4535 * more is performed. It is used for 1xx informational messages. Otherwise, the
Christopher Faulet507479b2020-05-15 12:29:46 +02004536 * transaction is terminated and the request is emptied. On success 1 is
Christopher Faulet40e6b552020-06-25 16:04:50 +02004537 * returned. If an error occurred, 0 is returned. If it fails, this function
4538 * only exits. It is the caller responsibility to do the cleanup.
Christopher Fauletef70e252020-01-28 09:26:19 +01004539 */
4540int http_forward_proxy_resp(struct stream *s, int final)
4541{
4542 struct channel *req = &s->req;
4543 struct channel *res = &s->res;
4544 struct htx *htx = htxbuf(&res->buf);
4545 size_t data;
4546
4547 if (final) {
4548 htx->flags |= HTX_FL_PROXY_RESP;
Christopher Faulet507479b2020-05-15 12:29:46 +02004549
Christopher Fauletaab1b672020-11-18 16:44:02 +01004550 if (!htx_is_empty(htx) && !http_eval_after_res_rules(s))
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01004551 return 0;
Christopher Fauletef70e252020-01-28 09:26:19 +01004552
Christopher Fauletd6c48362020-10-19 18:01:38 +02004553 if (s->txn->meth == HTTP_METH_HEAD)
4554 htx_skip_msg_payload(htx);
4555
Christopher Fauletef70e252020-01-28 09:26:19 +01004556 channel_auto_read(req);
4557 channel_abort(req);
4558 channel_auto_close(req);
4559 channel_htx_erase(req, htxbuf(&req->buf));
4560
4561 res->wex = tick_add_ifset(now_ms, res->wto);
4562 channel_auto_read(res);
4563 channel_auto_close(res);
4564 channel_shutr_now(res);
Christopher Faulet1a9db7c2020-06-25 15:36:45 +02004565 res->flags |= CF_EOI; /* The response is terminated, add EOI */
Christopher Faulet42432f32020-11-20 17:43:16 +01004566 htxbuf(&res->buf)->flags |= HTX_FL_EOM; /* no more data are expected */
Christopher Fauletef70e252020-01-28 09:26:19 +01004567 }
Christopher Fauletcf6898c2020-06-25 15:55:11 +02004568 else {
4569 /* Send ASAP informational messages. Rely on CF_EOI for final
4570 * response.
4571 */
4572 res->flags |= CF_SEND_DONTWAIT;
4573 }
Christopher Fauletef70e252020-01-28 09:26:19 +01004574
4575 data = htx->data - co_data(res);
4576 c_adv(res, data);
4577 htx->first = -1;
4578 res->total += data;
4579 return 1;
4580}
4581
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004582void http_server_error(struct stream *s, struct stream_interface *si, int err,
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004583 int finst, struct http_reply *msg)
Christopher Faulet0f226952018-10-22 09:29:56 +02004584{
Christopher Faulet72c7d8d2020-01-27 15:32:25 +01004585 http_reply_and_close(s, s->txn->status, msg);
Christopher Faulet0f226952018-10-22 09:29:56 +02004586 if (!(s->flags & SF_ERR_MASK))
4587 s->flags |= err;
4588 if (!(s->flags & SF_FINST_MASK))
4589 s->flags |= finst;
4590}
4591
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004592void http_reply_and_close(struct stream *s, short status, struct http_reply *msg)
Christopher Faulet0f226952018-10-22 09:29:56 +02004593{
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004594 if (!msg) {
4595 channel_htx_truncate(&s->res, htxbuf(&s->res.buf));
4596 goto end;
4597 }
4598
4599 if (http_reply_message(s, msg) == -1) {
4600 /* On error, return a 500 error message, but don't rewrite it if
Christopher Faulet40e6b552020-06-25 16:04:50 +02004601 * it is already an internal error. If it was already a "const"
4602 * 500 error, just fail.
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004603 */
Christopher Faulet40e6b552020-06-25 16:04:50 +02004604 if (s->txn->status == 500) {
4605 if (s->txn->flags & TX_CONST_REPLY)
4606 goto end;
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004607 s->txn->flags |= TX_CONST_REPLY;
Christopher Faulet40e6b552020-06-25 16:04:50 +02004608 }
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004609 s->txn->status = 500;
4610 s->txn->http_reply = NULL;
4611 return http_reply_and_close(s, s->txn->status, http_error_message(s));
4612 }
4613
4614end:
4615 s->res.wex = tick_add_ifset(now_ms, s->res.wto);
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004616
Christopher Faulet2d565002021-09-10 09:17:50 +02004617 /* At this staged, HTTP analysis is finished */
4618 s->req.analysers &= AN_REQ_FLT_END;
4619 s->req.analyse_exp = TICK_ETERNITY;
4620
4621 s->res.analysers &= AN_RES_FLT_END;
4622 s->res.analyse_exp = TICK_ETERNITY;
4623
Christopher Faulet0f226952018-10-22 09:29:56 +02004624 channel_auto_read(&s->req);
4625 channel_abort(&s->req);
4626 channel_auto_close(&s->req);
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004627 channel_htx_erase(&s->req, htxbuf(&s->req.buf));
Christopher Faulet72c7d8d2020-01-27 15:32:25 +01004628 channel_auto_read(&s->res);
4629 channel_auto_close(&s->res);
4630 channel_shutr_now(&s->res);
Christopher Faulet0f226952018-10-22 09:29:56 +02004631}
4632
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004633struct http_reply *http_error_message(struct stream *s)
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004634{
4635 const int msgnum = http_get_status_idx(s->txn->status);
4636
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004637 if (s->txn->http_reply)
4638 return s->txn->http_reply;
4639 else if (s->be->replies[msgnum])
4640 return s->be->replies[msgnum];
4641 else if (strm_fe(s)->replies[msgnum])
4642 return strm_fe(s)->replies[msgnum];
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004643 else
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004644 return &http_err_replies[msgnum];
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004645}
4646
Christopher Faulet40e6b552020-06-25 16:04:50 +02004647/* Produces an HTX message from an http reply. Depending on the http reply type,
4648 * a, errorfile, an raw file or a log-format string is used. On success, it
4649 * returns 0. If an error occurs -1 is returned. If it fails, this function only
4650 * exits. It is the caller responsibility to do the cleanup.
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004651 */
Christopher Fauletae43b6c2020-05-27 15:24:22 +02004652int http_reply_to_htx(struct stream *s, struct htx *htx, struct http_reply *reply)
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004653{
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004654 struct buffer *errmsg;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004655 struct htx_sl *sl;
4656 struct buffer *body = NULL;
4657 const char *status, *reason, *clen, *ctype;
4658 unsigned int slflags;
4659 int ret = 0;
4660
Christopher Faulete29a97e2020-05-14 14:49:25 +02004661 /*
4662 * - HTTP_REPLY_ERRFILES unexpected here. handled as no payload if so
4663 *
4664 * - HTTP_REPLY_INDIRECT: switch on another reply if defined or handled
4665 * as no payload if NULL. the TXN status code is set with the status
4666 * of the original reply.
4667 */
4668
4669 if (reply->type == HTTP_REPLY_INDIRECT) {
4670 if (reply->body.reply)
4671 reply = reply->body.reply;
4672 }
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004673 if (reply->type == HTTP_REPLY_ERRMSG && !reply->body.errmsg) {
4674 /* get default error message */
4675 if (reply == s->txn->http_reply)
4676 s->txn->http_reply = NULL;
4677 reply = http_error_message(s);
4678 if (reply->type == HTTP_REPLY_INDIRECT) {
4679 if (reply->body.reply)
4680 reply = reply->body.reply;
4681 }
4682 }
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004683
4684 if (reply->type == HTTP_REPLY_ERRMSG) {
4685 /* implicit or explicit error message*/
4686 errmsg = reply->body.errmsg;
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004687 if (errmsg && !b_is_null(errmsg)) {
Christopher Faulet20567362020-05-15 14:52:49 +02004688 if (!htx_copy_msg(htx, errmsg))
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004689 goto fail;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004690 }
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004691 }
4692 else {
4693 /* no payload, file or log-format string */
4694 if (reply->type == HTTP_REPLY_RAW) {
4695 /* file */
4696 body = &reply->body.obj;
4697 }
4698 else if (reply->type == HTTP_REPLY_LOGFMT) {
4699 /* log-format string */
4700 body = alloc_trash_chunk();
4701 if (!body)
4702 goto fail_alloc;
4703 body->data = build_logline(s, body->area, body->size, &reply->body.fmt);
4704 }
4705 /* else no payload */
4706
4707 status = ultoa(reply->status);
4708 reason = http_get_reason(reply->status);
4709 slflags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_CLEN);
4710 if (!body || !b_data(body))
4711 slflags |= HTX_SL_F_BODYLESS;
4712 sl = htx_add_stline(htx, HTX_BLK_RES_SL, slflags, ist("HTTP/1.1"), ist(status), ist(reason));
4713 if (!sl)
4714 goto fail;
4715 sl->info.res.status = reply->status;
4716
4717 clen = (body ? ultoa(b_data(body)) : "0");
4718 ctype = reply->ctype;
4719
4720 if (!LIST_ISEMPTY(&reply->hdrs)) {
4721 struct http_reply_hdr *hdr;
4722 struct buffer *value = alloc_trash_chunk();
4723
4724 if (!value)
4725 goto fail;
4726
4727 list_for_each_entry(hdr, &reply->hdrs, list) {
4728 chunk_reset(value);
4729 value->data = build_logline(s, value->area, value->size, &hdr->value);
4730 if (b_data(value) && !htx_add_header(htx, hdr->name, ist2(b_head(value), b_data(value)))) {
4731 free_trash_chunk(value);
4732 goto fail;
4733 }
4734 chunk_reset(value);
4735 }
4736 free_trash_chunk(value);
4737 }
4738
4739 if (!htx_add_header(htx, ist("content-length"), ist(clen)) ||
4740 (body && b_data(body) && ctype && !htx_add_header(htx, ist("content-type"), ist(ctype))) ||
4741 !htx_add_endof(htx, HTX_BLK_EOH) ||
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004742 (body && b_data(body) && !htx_add_data_atonce(htx, ist2(b_head(body), b_data(body)))))
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004743 goto fail;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004744
4745 htx->flags |= HTX_FL_EOM;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004746 }
4747
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004748 leave:
4749 if (reply->type == HTTP_REPLY_LOGFMT)
4750 free_trash_chunk(body);
4751 return ret;
4752
4753 fail_alloc:
4754 if (!(s->flags & SF_ERR_MASK))
4755 s->flags |= SF_ERR_RESOURCE;
Christopher Faulet97e466c2020-05-15 15:12:47 +02004756 /* fall through */
4757 fail:
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004758 ret = -1;
4759 goto leave;
Christopher Faulet97e466c2020-05-15 15:12:47 +02004760}
4761
4762/* Send an http reply to the client. On success, it returns 0. If an error
Christopher Faulet40e6b552020-06-25 16:04:50 +02004763 * occurs -1 is returned and the response channel is truncated, removing this
4764 * way the faulty reply. This function may fail when the reply is formatted
4765 * (http_reply_to_htx) or when the reply is forwarded
4766 * (http_forward_proxy_resp). On the last case, it is because a
4767 * http-after-response rule fails.
Christopher Faulet97e466c2020-05-15 15:12:47 +02004768 */
4769int http_reply_message(struct stream *s, struct http_reply *reply)
4770{
4771 struct channel *res = &s->res;
4772 struct htx *htx = htx_from_buf(&res->buf);
4773
4774 if (s->txn->status == -1)
4775 s->txn->status = reply->status;
4776 channel_htx_truncate(res, htx);
4777
4778 if (http_reply_to_htx(s, htx, reply) == -1)
4779 goto fail;
4780
4781 htx_to_buf(htx, &s->res.buf);
4782 if (!http_forward_proxy_resp(s, 1))
4783 goto fail;
4784 return 0;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004785
4786 fail:
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004787 channel_htx_truncate(res, htx);
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004788 if (!(s->flags & SF_ERR_MASK))
4789 s->flags |= SF_ERR_PRXCOND;
Christopher Faulet97e466c2020-05-15 15:12:47 +02004790 return -1;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004791}
4792
Christopher Faulet304cc402019-07-15 15:46:28 +02004793/* Return the error message corresponding to si->err_type. It is assumed
4794 * that the server side is closed. Note that err_type is actually a
4795 * bitmask, where almost only aborts may be cumulated with other
4796 * values. We consider that aborted operations are more important
4797 * than timeouts or errors due to the fact that nobody else in the
4798 * logs might explain incomplete retries. All others should avoid
4799 * being cumulated. It should normally not be possible to have multiple
4800 * aborts at once, but just in case, the first one in sequence is reported.
4801 * Note that connection errors appearing on the second request of a keep-alive
4802 * connection are not reported since this allows the client to retry.
4803 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004804void http_return_srv_error(struct stream *s, struct stream_interface *si)
Christopher Faulet304cc402019-07-15 15:46:28 +02004805{
4806 int err_type = si->err_type;
4807
4808 /* set s->txn->status for http_error_message(s) */
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004809 if (err_type & SI_ET_QUEUE_ABRT) {
4810 s->txn->status = -1;
4811 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_Q, NULL);
4812 }
4813 else if (err_type & SI_ET_CONN_ABRT) {
4814 s->txn->status = -1;
4815 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_C, NULL);
4816 }
4817 else if (err_type & SI_ET_QUEUE_TO) {
4818 s->txn->status = 503;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004819 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_Q,
4820 http_error_message(s));
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004821 }
4822 else if (err_type & SI_ET_QUEUE_ERR) {
4823 s->txn->status = 503;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004824 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_Q,
4825 http_error_message(s));
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004826 }
4827 else if (err_type & SI_ET_CONN_TO) {
4828 s->txn->status = 503;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004829 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_C,
4830 (s->txn->flags & TX_NOT_FIRST) ? NULL :
4831 http_error_message(s));
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004832 }
4833 else if (err_type & SI_ET_CONN_ERR) {
4834 s->txn->status = 503;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004835 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_C,
4836 (s->flags & SF_SRV_REUSED) ? NULL :
4837 http_error_message(s));
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004838 }
4839 else if (err_type & SI_ET_CONN_RES) {
4840 s->txn->status = 503;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004841 http_server_error(s, si, SF_ERR_RESOURCE, SF_FINST_C,
4842 (s->txn->flags & TX_NOT_FIRST) ? NULL :
4843 http_error_message(s));
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004844 }
Christopher Faulet304cc402019-07-15 15:46:28 +02004845 else { /* SI_ET_CONN_OTHER and others */
4846 s->txn->status = 500;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004847 http_server_error(s, si, SF_ERR_INTERNAL, SF_FINST_C,
4848 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004849 }
4850}
4851
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004852
Christopher Faulet4a28a532019-03-01 11:19:40 +01004853/* Handle Expect: 100-continue for HTTP/1.1 messages if necessary. It returns 0
4854 * on success and -1 on error.
4855 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004856static int http_handle_expect_hdr(struct stream *s, struct htx *htx, struct http_msg *msg)
Christopher Faulet4a28a532019-03-01 11:19:40 +01004857{
4858 /* If we have HTTP/1.1 message with a body and Expect: 100-continue,
4859 * then we must send an HTTP/1.1 100 Continue intermediate response.
4860 */
4861 if (msg->msg_state == HTTP_MSG_BODY && (msg->flags & HTTP_MSGF_VER_11) &&
4862 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
4863 struct ist hdr = { .ptr = "Expect", .len = 6 };
4864 struct http_hdr_ctx ctx;
4865
4866 ctx.blk = NULL;
4867 /* Expect is allowed in 1.1, look for it */
4868 if (http_find_header(htx, hdr, &ctx, 0) &&
4869 unlikely(isteqi(ctx.value, ist2("100-continue", 12)))) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004870 if (http_reply_100_continue(s) == -1)
Christopher Faulet4a28a532019-03-01 11:19:40 +01004871 return -1;
4872 http_remove_header(htx, &ctx);
4873 }
4874 }
4875 return 0;
4876}
4877
Christopher Faulet23a3c792018-11-28 10:01:23 +01004878/* Send a 100-Continue response to the client. It returns 0 on success and -1
4879 * on error. The response channel is updated accordingly.
4880 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004881static int http_reply_100_continue(struct stream *s)
Christopher Faulet23a3c792018-11-28 10:01:23 +01004882{
4883 struct channel *res = &s->res;
4884 struct htx *htx = htx_from_buf(&res->buf);
4885 struct htx_sl *sl;
4886 unsigned int flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|
4887 HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
Christopher Faulet23a3c792018-11-28 10:01:23 +01004888
4889 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
4890 ist("HTTP/1.1"), ist("100"), ist("Continue"));
4891 if (!sl)
4892 goto fail;
4893 sl->info.res.status = 100;
4894
Christopher Faulet1d5ec092019-06-26 14:23:54 +02004895 if (!htx_add_endof(htx, HTX_BLK_EOH))
Christopher Faulet23a3c792018-11-28 10:01:23 +01004896 goto fail;
4897
Christopher Fauleta72a7e42020-01-28 09:28:11 +01004898 if (!http_forward_proxy_resp(s, 0))
4899 goto fail;
Christopher Faulet23a3c792018-11-28 10:01:23 +01004900 return 0;
4901
4902 fail:
4903 /* If an error occurred, remove the incomplete HTTP response from the
4904 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004905 channel_htx_truncate(res, htx);
Christopher Faulet23a3c792018-11-28 10:01:23 +01004906 return -1;
4907}
4908
Christopher Faulet12c51e22018-11-28 15:59:42 +01004909
Christopher Faulet0f226952018-10-22 09:29:56 +02004910/*
4911 * Capture headers from message <htx> according to header list <cap_hdr>, and
4912 * fill the <cap> pointers appropriately.
4913 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004914static void http_capture_headers(struct htx *htx, char **cap, struct cap_hdr *cap_hdr)
Christopher Faulet0f226952018-10-22 09:29:56 +02004915{
4916 struct cap_hdr *h;
4917 int32_t pos;
4918
Christopher Fauleta3f15502019-05-13 15:27:23 +02004919 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet0f226952018-10-22 09:29:56 +02004920 struct htx_blk *blk = htx_get_blk(htx, pos);
4921 enum htx_blk_type type = htx_get_blk_type(blk);
4922 struct ist n, v;
4923
4924 if (type == HTX_BLK_EOH)
4925 break;
4926 if (type != HTX_BLK_HDR)
4927 continue;
4928
4929 n = htx_get_blk_name(htx, blk);
4930
4931 for (h = cap_hdr; h; h = h->next) {
4932 if (h->namelen && (h->namelen == n.len) &&
4933 (strncasecmp(n.ptr, h->name, h->namelen) == 0)) {
4934 if (cap[h->index] == NULL)
4935 cap[h->index] =
4936 pool_alloc(h->pool);
4937
4938 if (cap[h->index] == NULL) {
4939 ha_alert("HTTP capture : out of memory.\n");
4940 break;
4941 }
4942
4943 v = htx_get_blk_value(htx, blk);
Tim Duesterhus2471f5c2021-11-08 09:05:01 +01004944 v = isttrim(v, h->len);
Christopher Faulet0f226952018-10-22 09:29:56 +02004945
4946 memcpy(cap[h->index], v.ptr, v.len);
4947 cap[h->index][v.len]=0;
4948 }
4949 }
4950 }
4951}
4952
Christopher Faulet0b6bdc52018-10-24 11:05:36 +02004953/* Delete a value in a header between delimiters <from> and <next>. The header
4954 * itself is delimited by <start> and <end> pointers. The number of characters
4955 * displaced is returned, and the pointer to the first delimiter is updated if
4956 * required. The function tries as much as possible to respect the following
4957 * principles :
4958 * - replace <from> delimiter by the <next> one unless <from> points to <start>,
4959 * in which case <next> is simply removed
4960 * - set exactly one space character after the new first delimiter, unless there
4961 * are not enough characters in the block being moved to do so.
4962 * - remove unneeded spaces before the previous delimiter and after the new
4963 * one.
4964 *
4965 * It is the caller's responsibility to ensure that :
4966 * - <from> points to a valid delimiter or <start> ;
4967 * - <next> points to a valid delimiter or <end> ;
4968 * - there are non-space chars before <from>.
4969 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004970static int http_del_hdr_value(char *start, char *end, char **from, char *next)
Christopher Faulet0b6bdc52018-10-24 11:05:36 +02004971{
4972 char *prev = *from;
4973
4974 if (prev == start) {
4975 /* We're removing the first value. eat the semicolon, if <next>
4976 * is lower than <end> */
4977 if (next < end)
4978 next++;
4979
4980 while (next < end && HTTP_IS_SPHT(*next))
4981 next++;
4982 }
4983 else {
4984 /* Remove useless spaces before the old delimiter. */
4985 while (HTTP_IS_SPHT(*(prev-1)))
4986 prev--;
4987 *from = prev;
4988
4989 /* copy the delimiter and if possible a space if we're
4990 * not at the end of the line.
4991 */
4992 if (next < end) {
4993 *prev++ = *next++;
4994 if (prev + 1 < next)
4995 *prev++ = ' ';
4996 while (next < end && HTTP_IS_SPHT(*next))
4997 next++;
4998 }
4999 }
5000 memmove(prev, next, end - next);
5001 return (prev - next);
5002}
5003
Christopher Faulet0f226952018-10-22 09:29:56 +02005004
5005/* Formats the start line of the request (without CRLF) and puts it in <str> and
Joseph Herlantc42c0e92018-11-25 10:43:27 -08005006 * return the written length. The line can be truncated if it exceeds <len>.
Christopher Faulet0f226952018-10-22 09:29:56 +02005007 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005008static size_t http_fmt_req_line(const struct htx_sl *sl, char *str, size_t len)
Christopher Faulet0f226952018-10-22 09:29:56 +02005009{
5010 struct ist dst = ist2(str, 0);
5011
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005012 if (istcat(&dst, htx_sl_req_meth(sl), len) == -1)
Christopher Faulet0f226952018-10-22 09:29:56 +02005013 goto end;
5014 if (dst.len + 1 > len)
5015 goto end;
5016 dst.ptr[dst.len++] = ' ';
5017
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005018 if (istcat(&dst, htx_sl_req_uri(sl), len) == -1)
Christopher Faulet0f226952018-10-22 09:29:56 +02005019 goto end;
5020 if (dst.len + 1 > len)
5021 goto end;
5022 dst.ptr[dst.len++] = ' ';
5023
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005024 istcat(&dst, htx_sl_req_vsn(sl), len);
Christopher Faulet0f226952018-10-22 09:29:56 +02005025 end:
5026 return dst.len;
5027}
5028
5029/*
5030 * Print a debug line with a start line.
5031 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005032static void http_debug_stline(const char *dir, struct stream *s, const struct htx_sl *sl)
Christopher Faulet0f226952018-10-22 09:29:56 +02005033{
5034 struct session *sess = strm_sess(s);
5035 int max;
5036
5037 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
5038 dir,
Willy Tarreau88bc8002021-12-06 07:01:02 +00005039 objt_conn(sess->origin) ? (unsigned short)__objt_conn(sess->origin)->handle.fd : -1,
Christopher Faulet693b23b2022-02-28 09:09:05 +01005040 cs_conn(s->csb) ? (unsigned short)(__cs_conn(s->csb))->handle.fd : -1);
Christopher Faulet0f226952018-10-22 09:29:56 +02005041
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005042 max = HTX_SL_P1_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02005043 UBOUND(max, trash.size - trash.data - 3);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005044 chunk_memcat(&trash, HTX_SL_P1_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02005045 trash.area[trash.data++] = ' ';
5046
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005047 max = HTX_SL_P2_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02005048 UBOUND(max, trash.size - trash.data - 2);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005049 chunk_memcat(&trash, HTX_SL_P2_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02005050 trash.area[trash.data++] = ' ';
5051
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005052 max = HTX_SL_P3_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02005053 UBOUND(max, trash.size - trash.data - 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005054 chunk_memcat(&trash, HTX_SL_P3_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02005055 trash.area[trash.data++] = '\n';
5056
Willy Tarreau2e8ab6b2020-03-14 11:03:20 +01005057 DISGUISE(write(1, trash.area, trash.data));
Christopher Faulet0f226952018-10-22 09:29:56 +02005058}
5059
5060/*
5061 * Print a debug line with a header.
5062 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005063static 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 +02005064{
5065 struct session *sess = strm_sess(s);
5066 int max;
5067
5068 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
5069 dir,
Willy Tarreau88bc8002021-12-06 07:01:02 +00005070 objt_conn(sess->origin) ? (unsigned short)__objt_conn(sess->origin)->handle.fd : -1,
Christopher Faulet693b23b2022-02-28 09:09:05 +01005071 cs_conn(s->csb) ? (unsigned short)(__cs_conn(s->csb))->handle.fd : -1);
Christopher Faulet0f226952018-10-22 09:29:56 +02005072
5073 max = n.len;
5074 UBOUND(max, trash.size - trash.data - 3);
5075 chunk_memcat(&trash, n.ptr, max);
5076 trash.area[trash.data++] = ':';
5077 trash.area[trash.data++] = ' ';
5078
5079 max = v.len;
5080 UBOUND(max, trash.size - trash.data - 1);
5081 chunk_memcat(&trash, v.ptr, max);
5082 trash.area[trash.data++] = '\n';
5083
Willy Tarreau2e8ab6b2020-03-14 11:03:20 +01005084 DISGUISE(write(1, trash.area, trash.data));
Christopher Faulet0f226952018-10-22 09:29:56 +02005085}
5086
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005087/* Allocate a new HTTP transaction for stream <s> unless there is one already.
5088 * In case of allocation failure, everything allocated is freed and NULL is
5089 * returned. Otherwise the new transaction is assigned to the stream and
5090 * returned.
5091 */
5092struct http_txn *http_alloc_txn(struct stream *s)
5093{
5094 struct http_txn *txn = s->txn;
5095
5096 if (txn)
5097 return txn;
5098
5099 txn = pool_alloc(pool_head_http_txn);
5100 if (!txn)
5101 return txn;
5102
5103 s->txn = txn;
5104 return txn;
5105}
5106
5107void http_txn_reset_req(struct http_txn *txn)
5108{
Christopher Faulet1aea50e2020-01-17 16:03:53 +01005109 txn->req.flags = 0;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005110 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
5111}
5112
5113void http_txn_reset_res(struct http_txn *txn)
5114{
Christopher Faulet1aea50e2020-01-17 16:03:53 +01005115 txn->rsp.flags = 0;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005116 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
5117}
5118
5119/*
Christopher Faulet75f619a2021-03-08 19:12:58 +01005120 * Create and initialize a new HTTP transaction for stream <s>. This should be
5121 * used before processing any new request. It returns the transaction or NLULL
5122 * on error.
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005123 */
Christopher Faulet75f619a2021-03-08 19:12:58 +01005124struct http_txn *http_create_txn(struct stream *s)
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005125{
Christopher Faulet75f619a2021-03-08 19:12:58 +01005126 struct http_txn *txn;
Christopher Faulet95a61e82021-12-22 14:22:03 +01005127 struct conn_stream *cs = s->csf;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005128
Christopher Faulet75f619a2021-03-08 19:12:58 +01005129 txn = pool_alloc(pool_head_http_txn);
5130 if (!txn)
5131 return NULL;
5132 s->txn = txn;
5133
Christopher Faulete9e48202022-03-22 18:13:29 +01005134 txn->flags = ((cs && cs->endp->flags & CS_EP_NOT_FIRST) ? TX_NOT_FIRST : 0);
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005135 txn->status = -1;
Christopher Faulet5cb513a2020-05-13 17:56:56 +02005136 txn->http_reply = NULL;
Willy Tarreau8b507582020-02-25 09:35:07 +01005137 write_u32(txn->cache_hash, 0);
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005138
5139 txn->cookie_first_date = 0;
5140 txn->cookie_last_date = 0;
5141
5142 txn->srv_cookie = NULL;
5143 txn->cli_cookie = NULL;
5144 txn->uri = NULL;
5145
5146 http_txn_reset_req(txn);
5147 http_txn_reset_res(txn);
5148
5149 txn->req.chn = &s->req;
5150 txn->rsp.chn = &s->res;
5151
5152 txn->auth.method = HTTP_AUTH_UNKNOWN;
5153
Willy Tarreaub7bfcb32021-08-31 08:13:25 +02005154 vars_init_head(&s->vars_txn, SCOPE_TXN);
5155 vars_init_head(&s->vars_reqres, SCOPE_REQ);
Christopher Faulet75f619a2021-03-08 19:12:58 +01005156
5157 return txn;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005158}
5159
5160/* to be used at the end of a transaction */
Christopher Faulet75f619a2021-03-08 19:12:58 +01005161void http_destroy_txn(struct stream *s)
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005162{
5163 struct http_txn *txn = s->txn;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005164
5165 /* these ones will have been dynamically allocated */
5166 pool_free(pool_head_requri, txn->uri);
5167 pool_free(pool_head_capture, txn->cli_cookie);
5168 pool_free(pool_head_capture, txn->srv_cookie);
Tim Duesterhusa17e6622020-03-05 20:19:02 +01005169 pool_free(pool_head_uniqueid, s->unique_id.ptr);
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005170
Tim Duesterhusa17e6622020-03-05 20:19:02 +01005171 s->unique_id = IST_NULL;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005172 txn->uri = NULL;
5173 txn->srv_cookie = NULL;
5174 txn->cli_cookie = NULL;
5175
Christopher Faulet59399252019-11-07 14:27:52 +01005176 if (!LIST_ISEMPTY(&s->vars_txn.head))
5177 vars_prune(&s->vars_txn, s->sess, s);
5178 if (!LIST_ISEMPTY(&s->vars_reqres.head))
5179 vars_prune(&s->vars_reqres, s->sess, s);
Christopher Faulet75f619a2021-03-08 19:12:58 +01005180
5181 pool_free(pool_head_http_txn, txn);
5182 s->txn = NULL;
Christopher Faulet59399252019-11-07 14:27:52 +01005183}
5184
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005185
5186DECLARE_POOL(pool_head_http_txn, "http_txn", sizeof(struct http_txn));
Christopher Faulet0f226952018-10-22 09:29:56 +02005187
Christopher Fauletf4eb75d2018-10-11 15:55:07 +02005188__attribute__((constructor))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005189static void __http_protocol_init(void)
Christopher Fauletf4eb75d2018-10-11 15:55:07 +02005190{
5191}
5192
5193
5194/*
5195 * Local variables:
5196 * c-indent-level: 8
5197 * c-basic-offset: 8
5198 * End:
5199 */