blob: 4491ea11469e280e2a0d5a4b73d8348b3a63115a [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 Fauletb58f62b2020-01-13 16:40:13 +010059static enum rule_result http_req_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s);
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020060static enum rule_result http_res_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s);
Christopher Faulet79507152022-05-16 11:43:10 +020061static enum rule_result http_req_restrict_header_names(struct stream *s, struct htx *htx, struct proxy *px);
Christopher Faulet3e964192018-10-24 11:39:23 +020062
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020063static void http_manage_client_side_cookies(struct stream *s, struct channel *req);
64static void http_manage_server_side_cookies(struct stream *s, struct channel *res);
Christopher Fauletfcda7c62018-10-24 11:56:22 +020065
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020066static int http_stats_check_uri(struct stream *s, struct http_txn *txn, struct proxy *backend);
67static int http_handle_stats(struct stream *s, struct channel *req);
Christopher Faulet377c5a52018-10-24 21:21:30 +020068
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020069static int http_handle_expect_hdr(struct stream *s, struct htx *htx, struct http_msg *msg);
70static int http_reply_100_continue(struct stream *s);
Christopher Faulet23a3c792018-11-28 10:01:23 +010071
Christopher Faulete0768eb2018-10-03 16:38:02 +020072/* This stream analyser waits for a complete HTTP request. It returns 1 if the
73 * processing can continue on next analysers, or zero if it either needs more
74 * data or wants to immediately abort the request (eg: timeout, error, ...). It
75 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req.analysers
76 * when it has nothing left to do, and may remove any analyser when it wants to
77 * abort.
78 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020079int http_wait_for_request(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +020080{
Christopher Faulet9768c262018-10-22 09:34:31 +020081
Christopher Faulete0768eb2018-10-03 16:38:02 +020082 /*
Christopher Faulet9768c262018-10-22 09:34:31 +020083 * We will analyze a complete HTTP request to check the its syntax.
Christopher Faulete0768eb2018-10-03 16:38:02 +020084 *
Christopher Faulet9768c262018-10-22 09:34:31 +020085 * Once the start line and all headers are received, we may perform a
86 * capture of the error (if any), and we will set a few fields. We also
87 * check for monitor-uri, logging and finally headers capture.
Christopher Faulete0768eb2018-10-03 16:38:02 +020088 */
Christopher Faulete0768eb2018-10-03 16:38:02 +020089 struct session *sess = s->sess;
90 struct http_txn *txn = s->txn;
91 struct http_msg *msg = &txn->req;
Christopher Faulet9768c262018-10-22 09:34:31 +020092 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +010093 struct htx_sl *sl;
Christopher Faulete0768eb2018-10-03 16:38:02 +020094
Christopher Fauleteea8fc72019-11-05 16:18:10 +010095 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +020096
Christopher Fauletda46a0d2021-01-21 17:32:58 +010097 if (unlikely(!IS_HTX_STRM(s))) {
98 /* It is only possible when a TCP stream is upgrade to HTTP.
99 * There is a transition period during which there is no
100 * data. The stream is still in raw mode and SF_IGNORE flag is
101 * still set. When this happens, the new mux is responsible to
Ilya Shipitsinacf84592021-02-06 22:29:08 +0500102 * handle all errors. Thus we may leave immediately.
Christopher Fauletda46a0d2021-01-21 17:32:58 +0100103 */
104 BUG_ON(!(s->flags & SF_IGNORE) || !c_empty(&s->req));
Christopher Faulet9768c262018-10-22 09:34:31 +0200105
Christopher Faulet97b3a612021-03-15 17:10:12 +0100106 /* Don't connect for now */
107 channel_dont_connect(req);
108
109 /* A SHUTR at this stage means we are performing a "destructive"
110 * HTTP upgrade (TCP>H2). In this case, we can leave.
111 */
112 if (req->flags & CF_SHUTR) {
113 s->logs.logwait = 0;
114 s->logs.level = 0;
115 channel_abort(&s->req);
116 channel_abort(&s->res);
117 req->analysers &= AN_REQ_FLT_END;
118 req->analyse_exp = TICK_ETERNITY;
119 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA, s);
120 return 1;
121 }
Christopher Fauletda46a0d2021-01-21 17:32:58 +0100122 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA, s);
123 return 0;
124 }
125
126 htx = htxbuf(&req->buf);
Christopher Faulet8bebd2f2020-10-06 17:54:56 +0200127
Willy Tarreau4236f032019-03-05 10:43:32 +0100128 /* Parsing errors are caught here */
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200129 if (htx->flags & (HTX_FL_PARSING_ERROR|HTX_FL_PROCESSING_ERROR)) {
Willy Tarreau4236f032019-03-05 10:43:32 +0100130 stream_inc_http_req_ctr(s);
Emeric Brun28976442020-10-07 08:50:09 +0200131 proxy_inc_fe_req_ctr(sess->listener, sess->fe);
Christopher Fauletbf7175f2021-02-10 14:58:01 +0100132 if (htx->flags & HTX_FL_PARSING_ERROR) {
133 stream_inc_http_err_ctr(s);
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200134 goto return_bad_req;
Christopher Fauletbf7175f2021-02-10 14:58:01 +0100135 }
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200136 else
137 goto return_int_err;
Willy Tarreau4236f032019-03-05 10:43:32 +0100138 }
139
Christopher Faulete0768eb2018-10-03 16:38:02 +0200140 /* we're speaking HTTP here, so let's speak HTTP to the client */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200141 s->srv_error = http_return_srv_error;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200142
Christopher Faulet9768c262018-10-22 09:34:31 +0200143 msg->msg_state = HTTP_MSG_BODY;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200144 stream_inc_http_req_ctr(s);
Emeric Brun28976442020-10-07 08:50:09 +0200145 proxy_inc_fe_req_ctr(sess->listener, sess->fe); /* one more valid request for this FE */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200146
Christopher Faulet9768c262018-10-22 09:34:31 +0200147 /* kill the pending keep-alive timeout */
Christopher Faulet9768c262018-10-22 09:34:31 +0200148 req->analyse_exp = TICK_ETERNITY;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200149
Christopher Faulet29f17582019-05-23 11:03:26 +0200150 BUG_ON(htx_get_first_type(htx) != HTX_BLK_REQ_SL);
Christopher Faulet297fbb42019-05-13 14:41:27 +0200151 sl = http_get_stline(htx);
Christopher Faulet03599112018-11-27 11:21:21 +0100152
Christopher Faulet9768c262018-10-22 09:34:31 +0200153 /* 0: we might have to print this header in debug mode */
154 if (unlikely((global.mode & MODE_DEBUG) &&
155 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
156 int32_t pos;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200157
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200158 http_debug_stline("clireq", s, sl);
Christopher Faulet9768c262018-10-22 09:34:31 +0200159
Christopher Fauleta3f15502019-05-13 15:27:23 +0200160 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet9768c262018-10-22 09:34:31 +0200161 struct htx_blk *blk = htx_get_blk(htx, pos);
162 enum htx_blk_type type = htx_get_blk_type(blk);
163
164 if (type == HTX_BLK_EOH)
165 break;
166 if (type != HTX_BLK_HDR)
167 continue;
168
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200169 http_debug_hdr("clihdr", s,
170 htx_get_blk_name(htx, blk),
171 htx_get_blk_value(htx, blk));
Christopher Faulet9768c262018-10-22 09:34:31 +0200172 }
173 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200174
175 /*
Christopher Faulet03599112018-11-27 11:21:21 +0100176 * 1: identify the method and the version. Also set HTTP flags
Christopher Faulete0768eb2018-10-03 16:38:02 +0200177 */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100178 txn->meth = sl->info.req.meth;
Christopher Faulet03599112018-11-27 11:21:21 +0100179 if (sl->flags & HTX_SL_F_VER_11)
Christopher Faulet9768c262018-10-22 09:34:31 +0200180 msg->flags |= HTTP_MSGF_VER_11;
Christopher Faulet03599112018-11-27 11:21:21 +0100181 msg->flags |= HTTP_MSGF_XFER_LEN;
Christopher Faulet2a408542020-11-20 14:22:37 +0100182 if (sl->flags & HTX_SL_F_CLEN)
183 msg->flags |= HTTP_MSGF_CNT_LEN;
184 else if (sl->flags & HTX_SL_F_CHNK)
185 msg->flags |= HTTP_MSGF_TE_CHNK;
Christopher Fauletb2db4fa2018-11-27 16:51:09 +0100186 if (sl->flags & HTX_SL_F_BODYLESS)
187 msg->flags |= HTTP_MSGF_BODYLESS;
Christopher Faulet576c3582021-01-08 15:53:01 +0100188 if (sl->flags & HTX_SL_F_CONN_UPG)
189 msg->flags |= HTTP_MSGF_CONN_UPG;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200190
191 /* we can make use of server redirect on GET and HEAD */
192 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
193 s->flags |= SF_REDIRECTABLE;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100194 else if (txn->meth == HTTP_METH_OTHER && isteqi(htx_sl_req_meth(sl), ist("PRI"))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200195 /* PRI is reserved for the HTTP/2 preface */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200196 goto return_bad_req;
197 }
198
199 /*
Christopher Faulet6072beb2020-02-18 15:34:58 +0100200 * 2: check if the URI matches the monitor_uri. We have to do this for
201 * every request which gets in, because the monitor-uri is defined by
202 * the frontend. If the monitor-uri starts with a '/', the matching is
203 * done against the request's path. Otherwise, the request's uri is
204 * used. It is a workaround to let HTTP/2 health-checks work as
205 * expected.
Christopher Faulete0768eb2018-10-03 16:38:02 +0200206 */
207 if (unlikely((sess->fe->monitor_uri_len != 0) &&
Christopher Faulet6072beb2020-02-18 15:34:58 +0100208 ((*sess->fe->monitor_uri == '/' && isteq(http_get_path(htx_sl_req_uri(sl)),
209 ist2(sess->fe->monitor_uri, sess->fe->monitor_uri_len))) ||
210 isteq(htx_sl_req_uri(sl), ist2(sess->fe->monitor_uri, sess->fe->monitor_uri_len))))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200211 /*
212 * We have found the monitor URI
213 */
214 struct acl_cond *cond;
215
216 s->flags |= SF_MONITOR;
Willy Tarreau4781b152021-04-06 13:53:36 +0200217 _HA_ATOMIC_INC(&sess->fe->fe_counters.intercepted_req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200218
219 /* Check if we want to fail this monitor request or not */
220 list_for_each_entry(cond, &sess->fe->mon_fail_cond, list) {
221 int ret = acl_exec_cond(cond, sess->fe, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
222
223 ret = acl_pass(ret);
224 if (cond->pol == ACL_COND_UNLESS)
225 ret = !ret;
226
227 if (ret) {
228 /* we fail this request, let's return 503 service unavail */
229 txn->status = 503;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200230 if (!(s->flags & SF_ERR_MASK))
231 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
232 goto return_prx_cond;
233 }
234 }
235
Joseph Herlantc42c0e92018-11-25 10:43:27 -0800236 /* nothing to fail, let's reply normally */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200237 txn->status = 200;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200238 if (!(s->flags & SF_ERR_MASK))
239 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
240 goto return_prx_cond;
241 }
242
243 /*
244 * 3: Maybe we have to copy the original REQURI for the logs ?
245 * Note: we cannot log anymore if the request has been
246 * classified as invalid.
247 */
248 if (unlikely(s->logs.logwait & LW_REQ)) {
249 /* we have a complete HTTP request that we must log */
250 if ((txn->uri = pool_alloc(pool_head_requri)) != NULL) {
Christopher Faulet9768c262018-10-22 09:34:31 +0200251 size_t len;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200252
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200253 len = http_fmt_req_line(sl, txn->uri, global.tune.requri_len - 1);
Christopher Faulet9768c262018-10-22 09:34:31 +0200254 txn->uri[len] = 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200255
256 if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT)))
257 s->do_log(s);
258 } else {
259 ha_alert("HTTP logging : out of memory.\n");
260 }
261 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200262
Christopher Faulete0768eb2018-10-03 16:38:02 +0200263 /* if the frontend has "option http-use-proxy-header", we'll check if
264 * we have what looks like a proxied connection instead of a connection,
265 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
266 * Note that this is *not* RFC-compliant, however browsers and proxies
267 * happen to do that despite being non-standard :-(
268 * We consider that a request not beginning with either '/' or '*' is
269 * a proxied connection, which covers both "scheme://location" and
270 * CONNECT ip:port.
271 */
272 if ((sess->fe->options2 & PR_O2_USE_PXHDR) &&
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100273 *HTX_SL_REQ_UPTR(sl) != '/' && *HTX_SL_REQ_UPTR(sl) != '*')
Christopher Faulete0768eb2018-10-03 16:38:02 +0200274 txn->flags |= TX_USE_PX_CONN;
275
Christopher Faulete0768eb2018-10-03 16:38:02 +0200276 /* 5: we may need to capture headers */
277 if (unlikely((s->logs.logwait & LW_REQHDR) && s->req_cap))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200278 http_capture_headers(htx, s->req_cap, sess->fe->req_cap);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200279
Christopher Faulete0768eb2018-10-03 16:38:02 +0200280 /* we may have to wait for the request's body */
Christopher Faulet9768c262018-10-22 09:34:31 +0200281 if (s->be->options & PR_O_WREQ_BODY)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200282 req->analysers |= AN_REQ_HTTP_BODY;
283
284 /*
285 * RFC7234#4:
286 * A cache MUST write through requests with methods
287 * that are unsafe (Section 4.2.1 of [RFC7231]) to
288 * the origin server; i.e., a cache is not allowed
289 * to generate a reply to such a request before
290 * having forwarded the request and having received
291 * a corresponding response.
292 *
293 * RFC7231#4.2.1:
294 * Of the request methods defined by this
295 * specification, the GET, HEAD, OPTIONS, and TRACE
296 * methods are defined to be safe.
297 */
298 if (likely(txn->meth == HTTP_METH_GET ||
299 txn->meth == HTTP_METH_HEAD ||
300 txn->meth == HTTP_METH_OPTIONS ||
301 txn->meth == HTTP_METH_TRACE))
302 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
303
304 /* end of job, return OK */
305 req->analysers &= ~an_bit;
306 req->analyse_exp = TICK_ETERNITY;
Christopher Faulet9768c262018-10-22 09:34:31 +0200307
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100308 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200309 return 1;
310
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200311 return_int_err:
312 txn->status = 500;
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200313 if (!(s->flags & SF_ERR_MASK))
314 s->flags |= SF_ERR_INTERNAL;
Willy Tarreau4781b152021-04-06 13:53:36 +0200315 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
William Lallemand36119de2021-03-08 15:26:48 +0100316 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200317 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200318 goto return_prx_cond;
319
Christopher Faulete0768eb2018-10-03 16:38:02 +0200320 return_bad_req:
Christopher Faulet9768c262018-10-22 09:34:31 +0200321 txn->status = 400;
Willy Tarreau4781b152021-04-06 13:53:36 +0200322 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req);
William Lallemand36119de2021-03-08 15:26:48 +0100323 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200324 _HA_ATOMIC_INC(&sess->listener->counters->failed_req);
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200325 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200326
327 return_prx_cond:
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200328 http_reply_and_close(s, txn->status, http_error_message(s));
329
Christopher Faulete0768eb2018-10-03 16:38:02 +0200330 if (!(s->flags & SF_ERR_MASK))
331 s->flags |= SF_ERR_PRXCOND;
332 if (!(s->flags & SF_FINST_MASK))
333 s->flags |= SF_FINST_R;
334
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100335 DBG_TRACE_DEVEL("leaving on error",
336 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200337 return 0;
338}
339
340
341/* This stream analyser runs all HTTP request processing which is common to
342 * frontends and backends, which means blocking ACLs, filters, connection-close,
343 * reqadd, stats and redirects. This is performed for the designated proxy.
344 * It returns 1 if the processing can continue on next analysers, or zero if it
345 * either needs more data or wants to immediately abort the request (eg: deny,
346 * error, ...).
347 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200348int http_process_req_common(struct stream *s, struct channel *req, int an_bit, struct proxy *px)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200349{
350 struct session *sess = s->sess;
351 struct http_txn *txn = s->txn;
352 struct http_msg *msg = &txn->req;
Christopher Fauletff2759f2018-10-24 11:13:16 +0200353 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200354 struct redirect_rule *rule;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200355 enum rule_result verdict;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200356 struct connection *conn = objt_conn(sess->origin);
357
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100358 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200359
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100360 htx = htxbuf(&req->buf);
Christopher Fauletff2759f2018-10-24 11:13:16 +0200361
Christopher Faulet1907ccc2019-04-29 13:12:02 +0200362 /* just in case we have some per-backend tracking. Only called the first
363 * execution of the analyser. */
364 if (!s->current_rule || s->current_rule_list != &px->http_req_rules)
365 stream_inc_be_http_req_ctr(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200366
367 /* evaluate http-request rules */
368 if (!LIST_ISEMPTY(&px->http_req_rules)) {
Christopher Fauletb58f62b2020-01-13 16:40:13 +0100369 verdict = http_req_get_intercept_rule(px, &px->http_req_rules, s);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200370
371 switch (verdict) {
372 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
373 goto return_prx_yield;
374
375 case HTTP_RULE_RES_CONT:
376 case HTTP_RULE_RES_STOP: /* nothing to do */
377 break;
378
379 case HTTP_RULE_RES_DENY: /* deny or tarpit */
380 if (txn->flags & TX_CLTARPIT)
381 goto tarpit;
382 goto deny;
383
384 case HTTP_RULE_RES_ABRT: /* abort request, response already sent. Eg: auth */
385 goto return_prx_cond;
386
387 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
388 goto done;
389
390 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
391 goto return_bad_req;
Christopher Faulet3a26bee2019-12-16 12:47:40 +0100392
393 case HTTP_RULE_RES_ERROR: /* failed with a bad request */
394 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200395 }
396 }
397
Christopher Faulet79507152022-05-16 11:43:10 +0200398 if (px->options2 & (PR_O2_RSTRICT_REQ_HDR_NAMES_BLK|PR_O2_RSTRICT_REQ_HDR_NAMES_DEL)) {
399 verdict = http_req_restrict_header_names(s, htx, px);
400 if (verdict == HTTP_RULE_RES_DENY)
401 goto deny;
402 }
403
Christopher Faulete0768eb2018-10-03 16:38:02 +0200404 if (conn && (conn->flags & CO_FL_EARLY_DATA) &&
Olivier Houchard220a26c2020-01-23 14:57:36 +0100405 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_SSL_WAIT_HS))) {
Christopher Fauletff2759f2018-10-24 11:13:16 +0200406 struct http_hdr_ctx ctx;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200407
Christopher Fauletff2759f2018-10-24 11:13:16 +0200408 ctx.blk = NULL;
409 if (!http_find_header(htx, ist("Early-Data"), &ctx, 0)) {
410 if (unlikely(!http_add_header(htx, ist("Early-Data"), ist("1"))))
Christopher Faulet028c1642022-06-01 17:42:35 +0200411 goto return_fail_rewrite;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200412 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200413 }
414
415 /* OK at this stage, we know that the request was accepted according to
416 * the http-request rules, we can check for the stats. Note that the
417 * URI is detected *before* the req* rules in order not to be affected
418 * by a possible reqrep, while they are processed *after* so that a
419 * reqdeny can still block them. This clearly needs to change in 1.6!
420 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200421 if (!s->target && http_stats_check_uri(s, txn, px)) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200422 s->target = &http_stats_applet.obj_type;
Willy Tarreau14bfe9a2018-12-19 15:19:27 +0100423 if (unlikely(!si_register_handler(&s->si[1], objt_applet(s->target)))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200424 s->logs.tv_request = now;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200425 if (!(s->flags & SF_ERR_MASK))
426 s->flags |= SF_ERR_RESOURCE;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100427 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200428 }
429
430 /* parse the whole stats request and extract the relevant information */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200431 http_handle_stats(s, req);
Christopher Fauletb58f62b2020-01-13 16:40:13 +0100432 verdict = http_req_get_intercept_rule(px, &px->uri_auth->http_req_rules, s);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200433 /* not all actions implemented: deny, allow, auth */
434
435 if (verdict == HTTP_RULE_RES_DENY) /* stats http-request deny */
436 goto deny;
437
438 if (verdict == HTTP_RULE_RES_ABRT) /* stats auth / stats http-request auth */
439 goto return_prx_cond;
Christopher Faulet3a26bee2019-12-16 12:47:40 +0100440
441 if (verdict == HTTP_RULE_RES_BADREQ) /* failed with a bad request */
442 goto return_bad_req;
443
444 if (verdict == HTTP_RULE_RES_ERROR) /* failed with a bad request */
445 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200446 }
447
Christopher Faulet2571bc62019-03-01 11:44:26 +0100448 /* Proceed with the applets now. */
449 if (unlikely(objt_applet(s->target))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200450 if (sess->fe == s->be) /* report it if the request was intercepted by the frontend */
Willy Tarreau4781b152021-04-06 13:53:36 +0200451 _HA_ATOMIC_INC(&sess->fe->fe_counters.intercepted_req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200452
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200453 if (http_handle_expect_hdr(s, htx, msg) == -1)
Christopher Fauletb8a53712019-12-16 11:29:38 +0100454 goto return_int_err;
Christopher Fauletbcf242a2019-03-01 11:36:26 +0100455
Christopher Faulete0768eb2018-10-03 16:38:02 +0200456 if (!(s->flags & SF_ERR_MASK)) // this is not really an error but it is
457 s->flags |= SF_ERR_LOCAL; // to mark that it comes from the proxy
458 if (!(s->flags & SF_FINST_MASK))
459 s->flags |= SF_FINST_R;
460
Christopher Fauletc2ac5e42021-03-08 18:20:09 +0100461 if (HAS_FILTERS(s))
462 req->analysers |= AN_REQ_FLT_HTTP_HDRS;
463
Christopher Faulete0768eb2018-10-03 16:38:02 +0200464 /* enable the minimally required analyzers to handle keep-alive and compression on the HTTP response */
465 req->analysers &= (AN_REQ_HTTP_BODY | AN_REQ_FLT_HTTP_HDRS | AN_REQ_FLT_END);
466 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
467 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Christopher Fauletbcf242a2019-03-01 11:36:26 +0100468
469 req->flags |= CF_SEND_DONTWAIT;
470 s->flags |= SF_ASSIGNED;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200471 goto done;
472 }
473
474 /* check whether we have some ACLs set to redirect this request */
475 list_for_each_entry(rule, &px->redirect_rules, list) {
476 if (rule->cond) {
477 int ret;
478
479 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
480 ret = acl_pass(ret);
481 if (rule->cond->pol == ACL_COND_UNLESS)
482 ret = !ret;
483 if (!ret)
484 continue;
485 }
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200486 if (!http_apply_redirect_rule(rule, s, txn))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100487 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200488 goto done;
489 }
490
491 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
492 * If this happens, then the data will not come immediately, so we must
493 * send all what we have without waiting. Note that due to the small gain
494 * in waiting for the body of the request, it's easier to simply put the
495 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
496 * itself once used.
497 */
498 req->flags |= CF_SEND_DONTWAIT;
499
500 done: /* done with this analyser, continue with next ones that the calling
501 * points will have set, if any.
502 */
503 req->analyse_exp = TICK_ETERNITY;
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +0500504 done_without_exp: /* done with this analyser, but don't reset the analyse_exp. */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200505 req->analysers &= ~an_bit;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100506 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200507 return 1;
508
509 tarpit:
510 /* Allow cookie logging
511 */
512 if (s->be->cookie_name || sess->fe->capture_name)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200513 http_manage_client_side_cookies(s, req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200514
515 /* When a connection is tarpitted, we use the tarpit timeout,
516 * which may be the same as the connect timeout if unspecified.
517 * If unset, then set it to zero because we really want it to
518 * eventually expire. We build the tarpit as an analyser.
519 */
Christopher Faulet202c6ce2019-01-07 14:57:35 +0100520 channel_htx_erase(&s->req, htx);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200521
522 /* wipe the request out so that we can drop the connection early
523 * if the client closes first.
524 */
525 channel_dont_connect(req);
526
Christopher Faulete0768eb2018-10-03 16:38:02 +0200527 req->analysers &= AN_REQ_FLT_END; /* remove switching rules etc... */
528 req->analysers |= AN_REQ_HTTP_TARPIT;
529 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
530 if (!req->analyse_exp)
531 req->analyse_exp = tick_add(now_ms, 0);
532 stream_inc_http_err_ctr(s);
Willy Tarreau4781b152021-04-06 13:53:36 +0200533 _HA_ATOMIC_INC(&sess->fe->fe_counters.denied_req);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100534 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau4781b152021-04-06 13:53:36 +0200535 _HA_ATOMIC_INC(&s->be->be_counters.denied_req);
William Lallemand36119de2021-03-08 15:26:48 +0100536 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200537 _HA_ATOMIC_INC(&sess->listener->counters->denied_req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200538 goto done_without_exp;
539
540 deny: /* this request was blocked (denied) */
541
542 /* Allow cookie logging
543 */
544 if (s->be->cookie_name || sess->fe->capture_name)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200545 http_manage_client_side_cookies(s, req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200546
Christopher Faulete0768eb2018-10-03 16:38:02 +0200547 s->logs.tv_request = now;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200548 stream_inc_http_err_ctr(s);
Willy Tarreau4781b152021-04-06 13:53:36 +0200549 _HA_ATOMIC_INC(&sess->fe->fe_counters.denied_req);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100550 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau4781b152021-04-06 13:53:36 +0200551 _HA_ATOMIC_INC(&s->be->be_counters.denied_req);
William Lallemand36119de2021-03-08 15:26:48 +0100552 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200553 _HA_ATOMIC_INC(&sess->listener->counters->denied_req);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100554 goto return_prx_err;
555
Christopher Faulet028c1642022-06-01 17:42:35 +0200556 return_fail_rewrite:
557 if (!(s->flags & SF_ERR_MASK))
558 s->flags |= SF_ERR_PRXCOND;
559 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_rewrites);
560 if (s->flags & SF_BE_ASSIGNED)
561 _HA_ATOMIC_INC(&s->be->be_counters.failed_rewrites);
562 if (sess->listener && sess->listener->counters)
563 _HA_ATOMIC_INC(&sess->listener->counters->failed_rewrites);
564 if (objt_server(s->target))
565 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_rewrites);
566 /* fall through */
567
Christopher Fauletb8a53712019-12-16 11:29:38 +0100568 return_int_err:
569 txn->status = 500;
570 if (!(s->flags & SF_ERR_MASK))
571 s->flags |= SF_ERR_INTERNAL;
Willy Tarreau4781b152021-04-06 13:53:36 +0200572 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100573 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau4781b152021-04-06 13:53:36 +0200574 _HA_ATOMIC_INC(&s->be->be_counters.internal_errors);
William Lallemand36119de2021-03-08 15:26:48 +0100575 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200576 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100577 goto return_prx_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200578
579 return_bad_req:
Christopher Faulete0768eb2018-10-03 16:38:02 +0200580 txn->status = 400;
Willy Tarreau4781b152021-04-06 13:53:36 +0200581 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req);
William Lallemand36119de2021-03-08 15:26:48 +0100582 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200583 _HA_ATOMIC_INC(&sess->listener->counters->failed_req);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100584 /* fall through */
585
586 return_prx_err:
587 http_reply_and_close(s, txn->status, http_error_message(s));
588 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200589
590 return_prx_cond:
591 if (!(s->flags & SF_ERR_MASK))
592 s->flags |= SF_ERR_PRXCOND;
593 if (!(s->flags & SF_FINST_MASK))
594 s->flags |= SF_FINST_R;
595
596 req->analysers &= AN_REQ_FLT_END;
597 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100598 DBG_TRACE_DEVEL("leaving on error",
599 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200600 return 0;
601
602 return_prx_yield:
603 channel_dont_connect(req);
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100604 DBG_TRACE_DEVEL("waiting for more data",
605 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200606 return 0;
607}
608
609/* This function performs all the processing enabled for the current request.
610 * It returns 1 if the processing can continue on next analysers, or zero if it
611 * needs more data, encounters an error, or wants to immediately abort the
612 * request. It relies on buffers flags, and updates s->req.analysers.
613 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200614int http_process_request(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200615{
616 struct session *sess = s->sess;
617 struct http_txn *txn = s->txn;
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200618 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200619 struct connection *cli_conn = objt_conn(strm_sess(s)->origin);
620
Christopher Faulet8bebd2f2020-10-06 17:54:56 +0200621 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200622
623 /*
624 * Right now, we know that we have processed the entire headers
625 * and that unwanted requests have been filtered out. We can do
626 * whatever we want with the remaining request. Also, now we
627 * may have separate values for ->fe, ->be.
628 */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100629 htx = htxbuf(&req->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200630
631 /*
632 * If HTTP PROXY is set we simply get remote server address parsing
Willy Tarreau1c8d32b2019-07-18 15:47:45 +0200633 * incoming request.
Christopher Faulete0768eb2018-10-03 16:38:02 +0200634 */
635 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SF_ADDR_SET)) {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100636 struct htx_sl *sl;
637 struct ist uri, path;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200638
Willy Tarreau9b7587a2020-10-15 07:32:10 +0200639 if (!sockaddr_alloc(&s->target_addr, NULL, 0)) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200640 if (!(s->flags & SF_ERR_MASK))
641 s->flags |= SF_ERR_RESOURCE;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100642 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200643 }
Christopher Faulet297fbb42019-05-13 14:41:27 +0200644 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100645 uri = htx_sl_req_uri(sl);
646 path = http_get_path(uri);
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200647
Willy Tarreau1c8d32b2019-07-18 15:47:45 +0200648 if (url2sa(uri.ptr, uri.len - path.len, s->target_addr, NULL) == -1)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200649 goto return_bad_req;
650
Willy Tarreau1c8d32b2019-07-18 15:47:45 +0200651 s->target = &s->be->obj_type;
652 s->flags |= SF_ADDR_SET | SF_ASSIGNED;
653
Christopher Faulete0768eb2018-10-03 16:38:02 +0200654 /* if the path was found, we have to remove everything between
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200655 * uri.ptr and path.ptr (excluded). If it was not found, we need
656 * to replace from all the uri by a single "/".
657 *
Ilya Shipitsin46a030c2020-07-05 16:36:08 +0500658 * Instead of rewriting the whole start line, we just update
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100659 * the star-line URI. Some space will be lost but it should be
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200660 * insignificant.
Christopher Faulete0768eb2018-10-03 16:38:02 +0200661 */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100662 istcpy(&uri, (path.len ? path : ist("/")), uri.len);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200663 }
664
665 /*
666 * 7: Now we can work with the cookies.
667 * Note that doing so might move headers in the request, but
668 * the fields will stay coherent and the URI will not move.
669 * This should only be performed in the backend.
670 */
671 if (s->be->cookie_name || sess->fe->capture_name)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200672 http_manage_client_side_cookies(s, req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200673
Tim Duesterhus2825b4b2020-02-28 15:13:34 +0100674 /* 8: Generate unique ID if a "unique-id-format" is defined.
675 *
676 * A unique ID is generated even when it is not sent to ensure that the ID can make use of
677 * fetches only available in the HTTP request processing stage.
678 */
679 if (!LIST_ISEMPTY(&sess->fe->format_unique_id)) {
Tim Duesterhusa17e6622020-03-05 20:19:02 +0100680 struct ist unique_id = stream_generate_unique_id(s, &sess->fe->format_unique_id);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200681
Tim Duesterhusa17e6622020-03-05 20:19:02 +0100682 if (!isttest(unique_id)) {
Christopher Fauletb8a53712019-12-16 11:29:38 +0100683 if (!(s->flags & SF_ERR_MASK))
684 s->flags |= SF_ERR_RESOURCE;
685 goto return_int_err;
686 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200687
Tim Duesterhus2825b4b2020-02-28 15:13:34 +0100688 /* send unique ID if a "unique-id-header" is defined */
Tim Duesterhus0643b0e2020-03-05 17:56:35 +0100689 if (isttest(sess->fe->header_unique_id) &&
Tim Duesterhusa17e6622020-03-05 20:19:02 +0100690 unlikely(!http_add_header(htx, sess->fe->header_unique_id, s->unique_id)))
Christopher Faulet028c1642022-06-01 17:42:35 +0200691 goto return_fail_rewrite;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200692 }
693
694 /*
695 * 9: add X-Forwarded-For if either the frontend or the backend
696 * asks for it.
697 */
698 if ((sess->fe->options | s->be->options) & PR_O_FWDFOR) {
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200699 struct http_hdr_ctx ctx = { .blk = NULL };
700 struct ist hdr = ist2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : sess->fe->fwdfor_hdr_name,
701 s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : sess->fe->fwdfor_hdr_len);
702
Christopher Faulete0768eb2018-10-03 16:38:02 +0200703 if (!((sess->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200704 http_find_header(htx, hdr, &ctx, 0)) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200705 /* The header is set to be added only if none is present
706 * and we found it, so don't do anything.
707 */
708 }
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200709 else if (cli_conn && conn_get_src(cli_conn) && cli_conn->src->ss_family == AF_INET) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200710 /* Add an X-Forwarded-For header unless the source IP is
711 * in the 'except' network range.
712 */
Christopher Faulet5d1def62021-02-26 09:19:15 +0100713 if (ipcmp2net(cli_conn->src, &sess->fe->except_xff_net) &&
714 ipcmp2net(cli_conn->src, &s->be->except_xff_net)) {
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200715 unsigned char *pn = (unsigned char *)&((struct sockaddr_in *)cli_conn->src)->sin_addr;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200716
717 /* Note: we rely on the backend to get the header name to be used for
718 * x-forwarded-for, because the header is really meant for the backends.
719 * However, if the backend did not specify any option, we have to rely
720 * on the frontend's header name.
721 */
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200722 chunk_printf(&trash, "%d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
723 if (unlikely(!http_add_header(htx, hdr, ist2(trash.area, trash.data))))
Christopher Faulet028c1642022-06-01 17:42:35 +0200724 goto return_fail_rewrite;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200725 }
726 }
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200727 else if (cli_conn && conn_get_src(cli_conn) && cli_conn->src->ss_family == AF_INET6) {
Christopher Faulet5d1def62021-02-26 09:19:15 +0100728 /* Add an X-Forwarded-For header unless the source IP is
729 * in the 'except' network range.
Christopher Faulete0768eb2018-10-03 16:38:02 +0200730 */
Christopher Faulet5d1def62021-02-26 09:19:15 +0100731 if (ipcmp2net(cli_conn->src, &sess->fe->except_xff_net) &&
732 ipcmp2net(cli_conn->src, &s->be->except_xff_net)) {
733 char pn[INET6_ADDRSTRLEN];
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200734
Christopher Faulet5d1def62021-02-26 09:19:15 +0100735 inet_ntop(AF_INET6,
736 (const void *)&((struct sockaddr_in6 *)(cli_conn->src))->sin6_addr,
737 pn, sizeof(pn));
Christopher Faulete0768eb2018-10-03 16:38:02 +0200738
Christopher Faulet5d1def62021-02-26 09:19:15 +0100739 /* Note: we rely on the backend to get the header name to be used for
740 * x-forwarded-for, because the header is really meant for the backends.
741 * However, if the backend did not specify any option, we have to rely
742 * on the frontend's header name.
743 */
744 chunk_printf(&trash, "%s", pn);
745 if (unlikely(!http_add_header(htx, hdr, ist2(trash.area, trash.data))))
Christopher Faulet028c1642022-06-01 17:42:35 +0200746 goto return_fail_rewrite;
Christopher Faulet5d1def62021-02-26 09:19:15 +0100747 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200748 }
749 }
750
751 /*
752 * 10: add X-Original-To if either the frontend or the backend
753 * asks for it.
754 */
755 if ((sess->fe->options | s->be->options) & PR_O_ORGTO) {
Christopher Faulet5d1def62021-02-26 09:19:15 +0100756 struct ist hdr = ist2(s->be->orgto_hdr_len ? s->be->orgto_hdr_name : sess->fe->orgto_hdr_name,
757 s->be->orgto_hdr_len ? s->be->orgto_hdr_len : sess->fe->orgto_hdr_len);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200758
Christopher Fauletcccded92021-02-26 12:45:56 +0100759 if (cli_conn && conn_get_dst(cli_conn) && cli_conn->dst->ss_family == AF_INET) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200760 /* Add an X-Original-To header unless the destination IP is
761 * in the 'except' network range.
762 */
Christopher Faulet5d1def62021-02-26 09:19:15 +0100763 if (ipcmp2net(cli_conn->dst, &sess->fe->except_xot_net) &&
764 ipcmp2net(cli_conn->dst, &s->be->except_xot_net)) {
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200765 unsigned char *pn = (unsigned char *)&((struct sockaddr_in *)cli_conn->dst)->sin_addr;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200766
767 /* Note: we rely on the backend to get the header name to be used for
768 * x-original-to, because the header is really meant for the backends.
769 * However, if the backend did not specify any option, we have to rely
770 * on the frontend's header name.
771 */
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200772 chunk_printf(&trash, "%d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
773 if (unlikely(!http_add_header(htx, hdr, ist2(trash.area, trash.data))))
Christopher Faulet028c1642022-06-01 17:42:35 +0200774 goto return_fail_rewrite;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200775 }
776 }
Christopher Faulet5d1def62021-02-26 09:19:15 +0100777 else if (cli_conn && conn_get_dst(cli_conn) && cli_conn->dst->ss_family == AF_INET6) {
778 /* Add an X-Original-To header unless the source IP is
779 * in the 'except' network range.
780 */
781 if (ipcmp2net(cli_conn->dst, &sess->fe->except_xot_net) &&
782 ipcmp2net(cli_conn->dst, &s->be->except_xot_net)) {
783 char pn[INET6_ADDRSTRLEN];
784
785 inet_ntop(AF_INET6,
786 (const void *)&((struct sockaddr_in6 *)(cli_conn->dst))->sin6_addr,
787 pn, sizeof(pn));
788
789 /* Note: we rely on the backend to get the header name to be used for
790 * x-forwarded-for, because the header is really meant for the backends.
791 * However, if the backend did not specify any option, we have to rely
792 * on the frontend's header name.
793 */
794 chunk_printf(&trash, "%s", pn);
795 if (unlikely(!http_add_header(htx, hdr, ist2(trash.area, trash.data))))
Christopher Faulet028c1642022-06-01 17:42:35 +0200796 goto return_fail_rewrite;
Christopher Faulet5d1def62021-02-26 09:19:15 +0100797 }
798 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200799 }
800
Christopher Fauletc2ac5e42021-03-08 18:20:09 +0100801 /* Filter the request headers if there are filters attached to the
802 * stream.
803 */
804 if (HAS_FILTERS(s))
805 req->analysers |= AN_REQ_FLT_HTTP_HDRS;
806
Christopher Faulete0768eb2018-10-03 16:38:02 +0200807 /* If we have no server assigned yet and we're balancing on url_param
808 * with a POST request, we may be interested in checking the body for
809 * that parameter. This will be done in another analyser.
810 */
811 if (!(s->flags & (SF_ASSIGNED|SF_DIRECT)) &&
Willy Tarreau089eaa02019-01-14 15:17:46 +0100812 s->txn->meth == HTTP_METH_POST &&
813 (s->be->lbprm.algo & BE_LB_ALGO) == BE_LB_ALGO_PH) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200814 channel_dont_connect(req);
815 req->analysers |= AN_REQ_HTTP_BODY;
816 }
817
818 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
819 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau1a18b542018-12-11 16:37:42 +0100820
Christopher Faulete0768eb2018-10-03 16:38:02 +0200821 /* We expect some data from the client. Unless we know for sure
822 * we already have a full request, we have to re-enable quick-ack
823 * in case we previously disabled it, otherwise we might cause
824 * the client to delay further data.
825 */
William Lallemand36119de2021-03-08 15:26:48 +0100826 if ((sess->listener && (sess->listener->options & LI_O_NOQUICKACK)) && !(htx->flags & HTX_FL_EOM))
Willy Tarreau1a18b542018-12-11 16:37:42 +0100827 conn_set_quickack(cli_conn, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200828
829 /*************************************************************
830 * OK, that's finished for the headers. We have done what we *
831 * could. Let's switch to the DATA state. *
832 ************************************************************/
833 req->analyse_exp = TICK_ETERNITY;
834 req->analysers &= ~an_bit;
835
836 s->logs.tv_request = now;
837 /* OK let's go on with the BODY now */
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100838 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200839 return 1;
840
Christopher Faulet028c1642022-06-01 17:42:35 +0200841 return_fail_rewrite:
842 if (!(s->flags & SF_ERR_MASK))
843 s->flags |= SF_ERR_PRXCOND;
844 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_rewrites);
845 if (s->flags & SF_BE_ASSIGNED)
846 _HA_ATOMIC_INC(&s->be->be_counters.failed_rewrites);
847 if (sess->listener && sess->listener->counters)
848 _HA_ATOMIC_INC(&sess->listener->counters->failed_rewrites);
849 if (objt_server(s->target))
850 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_rewrites);
851 /* fall through */
852
Christopher Fauletb8a53712019-12-16 11:29:38 +0100853 return_int_err:
854 txn->status = 500;
855 if (!(s->flags & SF_ERR_MASK))
856 s->flags |= SF_ERR_INTERNAL;
Willy Tarreau4781b152021-04-06 13:53:36 +0200857 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100858 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau4781b152021-04-06 13:53:36 +0200859 _HA_ATOMIC_INC(&s->be->be_counters.internal_errors);
William Lallemand36119de2021-03-08 15:26:48 +0100860 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200861 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100862 goto return_prx_cond;
863
Christopher Faulete0768eb2018-10-03 16:38:02 +0200864 return_bad_req: /* let's centralize all bad requests */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200865 txn->status = 400;
Willy Tarreau4781b152021-04-06 13:53:36 +0200866 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req);
William Lallemand36119de2021-03-08 15:26:48 +0100867 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200868 _HA_ATOMIC_INC(&sess->listener->counters->failed_req);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100869 /* fall through */
870
871 return_prx_cond:
872 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +0200873
874 if (!(s->flags & SF_ERR_MASK))
875 s->flags |= SF_ERR_PRXCOND;
876 if (!(s->flags & SF_FINST_MASK))
877 s->flags |= SF_FINST_R;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100878
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100879 DBG_TRACE_DEVEL("leaving on error",
880 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200881 return 0;
882}
883
884/* This function is an analyser which processes the HTTP tarpit. It always
885 * returns zero, at the beginning because it prevents any other processing
886 * from occurring, and at the end because it terminates the request.
887 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200888int http_process_tarpit(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200889{
890 struct http_txn *txn = s->txn;
891
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100892 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, &txn->req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200893 /* This connection is being tarpitted. The CLIENT side has
894 * already set the connect expiration date to the right
895 * timeout. We just have to check that the client is still
896 * there and that the timeout has not expired.
897 */
898 channel_dont_connect(req);
899 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100900 !tick_is_expired(req->analyse_exp, now_ms)) {
Christopher Fauletb02d5f02021-10-29 14:37:07 +0200901 /* Be sure to drain all data from the request channel */
902 channel_htx_erase(req, htxbuf(&req->buf));
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100903 DBG_TRACE_DEVEL("waiting for tarpit timeout expiry",
904 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200905 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100906 }
907
Christopher Faulete0768eb2018-10-03 16:38:02 +0200908
909 /* We will set the queue timer to the time spent, just for
910 * logging purposes. We fake a 500 server error, so that the
911 * attacker will not suspect his connection has been tarpitted.
912 * It will not cause trouble to the logs because we can exclude
913 * the tarpitted connections by filtering on the 'PT' status flags.
914 */
915 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
916
Christopher Faulet8dfeccf2020-05-15 14:16:29 +0200917 http_reply_and_close(s, txn->status, (!(req->flags & CF_READ_ERROR) ? http_error_message(s) : NULL));
Christopher Faulet5cb513a2020-05-13 17:56:56 +0200918
Christopher Faulete0768eb2018-10-03 16:38:02 +0200919 if (!(s->flags & SF_ERR_MASK))
920 s->flags |= SF_ERR_PRXCOND;
921 if (!(s->flags & SF_FINST_MASK))
922 s->flags |= SF_FINST_T;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100923
924 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200925 return 0;
926}
927
928/* This function is an analyser which waits for the HTTP request body. It waits
929 * for either the buffer to be full, or the full advertised contents to have
930 * reached the buffer. It must only be called after the standard HTTP request
931 * processing has occurred, because it expects the request to be parsed and will
932 * look for the Expect header. It may send a 100-Continue interim response. It
933 * takes in input any state starting from HTTP_MSG_BODY and leaves with one of
934 * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it
935 * needs to read more data, or 1 once it has completed its analysis.
936 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200937int http_wait_for_request_body(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200938{
939 struct session *sess = s->sess;
940 struct http_txn *txn = s->txn;
941 struct http_msg *msg = &s->txn->req;
942
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100943 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Fauletf76ebe82018-10-24 11:16:22 +0200944
Christopher Fauletf76ebe82018-10-24 11:16:22 +0200945
Christopher Faulet021a8e42021-03-29 10:46:38 +0200946 switch (http_wait_for_msg_body(s, req, s->be->timeout.httpreq, 0)) {
947 case HTTP_RULE_RES_CONT:
948 goto http_end;
949 case HTTP_RULE_RES_YIELD:
950 goto missing_data_or_waiting;
951 case HTTP_RULE_RES_BADREQ:
Willy Tarreau4236f032019-03-05 10:43:32 +0100952 goto return_bad_req;
Christopher Faulet021a8e42021-03-29 10:46:38 +0200953 case HTTP_RULE_RES_ERROR:
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200954 goto return_int_err;
Christopher Faulet021a8e42021-03-29 10:46:38 +0200955 case HTTP_RULE_RES_ABRT:
Christopher Fauletb8a53712019-12-16 11:29:38 +0100956 goto return_prx_cond;
Christopher Faulet021a8e42021-03-29 10:46:38 +0200957 default:
958 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200959 }
960
961 http_end:
962 /* The situation will not evolve, so let's give up on the analysis. */
963 s->logs.tv_request = now; /* update the request timer to reflect full request */
964 req->analysers &= ~an_bit;
965 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100966 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200967 return 1;
968
Christopher Faulet021a8e42021-03-29 10:46:38 +0200969 missing_data_or_waiting:
970 channel_dont_connect(req);
971 DBG_TRACE_DEVEL("waiting for more data",
972 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
973 return 0;
974
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200975 return_int_err:
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200976 txn->status = 500;
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200977 if (!(s->flags & SF_ERR_MASK))
978 s->flags |= SF_ERR_INTERNAL;
Willy Tarreau4781b152021-04-06 13:53:36 +0200979 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100980 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau4781b152021-04-06 13:53:36 +0200981 _HA_ATOMIC_INC(&s->be->be_counters.internal_errors);
William Lallemand36119de2021-03-08 15:26:48 +0100982 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200983 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Faulet021a8e42021-03-29 10:46:38 +0200984 goto return_prx_err;
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200985
Christopher Faulete0768eb2018-10-03 16:38:02 +0200986 return_bad_req: /* let's centralize all bad requests */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200987 txn->status = 400;
Willy Tarreau4781b152021-04-06 13:53:36 +0200988 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req);
William Lallemand36119de2021-03-08 15:26:48 +0100989 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200990 _HA_ATOMIC_INC(&sess->listener->counters->failed_req);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100991 /* fall through */
992
Christopher Faulet021a8e42021-03-29 10:46:38 +0200993 return_prx_err:
Christopher Fauletb8a53712019-12-16 11:29:38 +0100994 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet021a8e42021-03-29 10:46:38 +0200995 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200996
Christopher Faulet021a8e42021-03-29 10:46:38 +0200997 return_prx_cond:
Christopher Faulete0768eb2018-10-03 16:38:02 +0200998 if (!(s->flags & SF_ERR_MASK))
999 s->flags |= SF_ERR_PRXCOND;
1000 if (!(s->flags & SF_FINST_MASK))
Christopher Fauletb8a53712019-12-16 11:29:38 +01001001 s->flags |= (msg->msg_state < HTTP_MSG_DATA ? SF_FINST_R : SF_FINST_D);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001002
Christopher Faulete0768eb2018-10-03 16:38:02 +02001003 req->analysers &= AN_REQ_FLT_END;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001004 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001005 DBG_TRACE_DEVEL("leaving on error",
1006 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001007 return 0;
1008}
1009
1010/* This function is an analyser which forwards request body (including chunk
1011 * sizes if any). It is called as soon as we must forward, even if we forward
1012 * zero byte. The only situation where it must not be called is when we're in
1013 * tunnel mode and we want to forward till the close. It's used both to forward
1014 * remaining data and to resync after end of body. It expects the msg_state to
1015 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
1016 * read more data, or 1 once we can go on with next request or end the stream.
1017 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
1018 * bytes of pending data + the headers if not already done.
1019 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001020int http_request_forward_body(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001021{
1022 struct session *sess = s->sess;
1023 struct http_txn *txn = s->txn;
Christopher Faulet9768c262018-10-22 09:34:31 +02001024 struct http_msg *msg = &txn->req;
1025 struct htx *htx;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001026 short status = 0;
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001027 int ret;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001028
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001029 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001030
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001031 htx = htxbuf(&req->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001032
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001033 if (htx->flags & HTX_FL_PARSING_ERROR)
1034 goto return_bad_req;
1035 if (htx->flags & HTX_FL_PROCESSING_ERROR)
1036 goto return_int_err;
1037
Christopher Faulete0768eb2018-10-03 16:38:02 +02001038 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
1039 ((req->flags & CF_SHUTW) && (req->to_forward || co_data(req)))) {
1040 /* Output closed while we were sending data. We must abort and
1041 * wake the other side up.
Christopher Fauletf506d962021-04-27 10:56:28 +02001042 *
1043 * If we have finished to send the request and the response is
1044 * still in progress, don't catch write error on the request
1045 * side if it is in fact a read error on the server side.
Christopher Faulete0768eb2018-10-03 16:38:02 +02001046 */
Christopher Fauletf506d962021-04-27 10:56:28 +02001047 if (msg->msg_state == HTTP_MSG_DONE && (s->res.flags & CF_READ_ERROR) && s->res.analysers)
1048 return 0;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001049
Olivier Houchard29cac3c2019-07-12 15:48:58 +02001050 /* Don't abort yet if we had L7 retries activated and it
1051 * was a write error, we may recover.
1052 */
1053 if (!(req->flags & (CF_READ_ERROR | CF_READ_TIMEOUT)) &&
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001054 (s->si[1].flags & SI_FL_L7_RETRY)) {
1055 DBG_TRACE_DEVEL("leaving on L7 retry",
1056 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Olivier Houchard29cac3c2019-07-12 15:48:58 +02001057 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001058 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001059 msg->msg_state = HTTP_MSG_ERROR;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001060 http_end_request(s);
1061 http_end_response(s);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001062 DBG_TRACE_DEVEL("leaving on error",
1063 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001064 return 1;
1065 }
1066
1067 /* Note that we don't have to send 100-continue back because we don't
1068 * need the data to complete our job, and it's up to the server to
1069 * decide whether to return 100, 417 or anything else in return of
1070 * an "Expect: 100-continue" header.
1071 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001072 if (msg->msg_state == HTTP_MSG_BODY)
1073 msg->msg_state = HTTP_MSG_DATA;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001074
Christopher Faulete0768eb2018-10-03 16:38:02 +02001075 /* in most states, we should abort in case of early close */
1076 channel_auto_close(req);
1077
1078 if (req->to_forward) {
Christopher Faulet66af0b22019-03-22 14:54:52 +01001079 if (req->to_forward == CHN_INFINITE_FORWARD) {
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001080 if (req->flags & CF_EOI)
1081 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet66af0b22019-03-22 14:54:52 +01001082 }
1083 else {
1084 /* We can't process the buffer's contents yet */
1085 req->flags |= CF_WAKE_WRITE;
1086 goto missing_data_or_waiting;
1087 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001088 }
1089
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001090 if (msg->msg_state >= HTTP_MSG_ENDING)
1091 goto ending;
1092
1093 if (txn->meth == HTTP_METH_CONNECT) {
1094 msg->msg_state = HTTP_MSG_ENDING;
1095 goto ending;
1096 }
1097
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001098 /* Forward input data. We get it by removing all outgoing data not
1099 * forwarded yet from HTX data size. If there are some data filters, we
1100 * let them decide the amount of data to forward.
Christopher Faulet9768c262018-10-22 09:34:31 +02001101 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001102 if (HAS_REQ_DATA_FILTERS(s)) {
1103 ret = flt_http_payload(s, msg, htx->data);
1104 if (ret < 0)
1105 goto return_bad_req;
Christopher Faulet421e7692019-06-13 11:16:45 +02001106 c_adv(req, ret);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001107 }
1108 else {
Christopher Faulet421e7692019-06-13 11:16:45 +02001109 c_adv(req, htx->data - co_data(req));
Christopher Faulet66af0b22019-03-22 14:54:52 +01001110 if (msg->flags & HTTP_MSGF_XFER_LEN)
1111 channel_htx_forward_forever(req, htx);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001112 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001113
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001114 if (htx->data != co_data(req))
1115 goto missing_data_or_waiting;
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001116
Christopher Faulet9768c262018-10-22 09:34:31 +02001117 /* Check if the end-of-message is reached and if so, switch the message
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001118 * in HTTP_MSG_ENDING state. Then if all data was marked to be
1119 * forwarded, set the state to HTTP_MSG_DONE.
Christopher Faulet9768c262018-10-22 09:34:31 +02001120 */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001121 if (!(htx->flags & HTX_FL_EOM))
Christopher Faulet9768c262018-10-22 09:34:31 +02001122 goto missing_data_or_waiting;
1123
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001124 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet9768c262018-10-22 09:34:31 +02001125
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001126 ending:
Christopher Faulet2151cdd2020-07-22 16:34:59 +02001127 req->flags &= ~CF_EXPECT_MORE; /* no more data are expected */
1128
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001129 /* other states, ENDING...TUNNEL */
1130 if (msg->msg_state >= HTTP_MSG_DONE)
1131 goto done;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001132
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001133 if (HAS_REQ_DATA_FILTERS(s)) {
1134 ret = flt_http_end(s, msg);
1135 if (ret <= 0) {
1136 if (!ret)
1137 goto missing_data_or_waiting;
1138 goto return_bad_req;
1139 }
1140 }
1141
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001142 if (txn->meth == HTTP_METH_CONNECT)
1143 msg->msg_state = HTTP_MSG_TUNNEL;
1144 else {
1145 msg->msg_state = HTTP_MSG_DONE;
1146 req->to_forward = 0;
1147 }
1148
1149 done:
1150 /* we don't want to forward closes on DONE except in tunnel mode. */
1151 if (!(txn->flags & TX_CON_WANT_TUN))
1152 channel_dont_close(req);
1153
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001154 http_end_request(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001155 if (!(req->analysers & an_bit)) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001156 http_end_response(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001157 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
1158 if (req->flags & CF_SHUTW) {
1159 /* request errors are most likely due to the
1160 * server aborting the transfer. */
Christopher Faulet93e02d82019-03-08 14:18:50 +01001161 goto return_srv_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001162 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001163 goto return_bad_req;
1164 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001165 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001166 return 1;
1167 }
1168
1169 /* If "option abortonclose" is set on the backend, we want to monitor
1170 * the client's connection and forward any shutdown notification to the
1171 * server, which will decide whether to close or to go on processing the
1172 * request. We only do that in tunnel mode, and not in other modes since
1173 * it can be abused to exhaust source ports. */
Christopher Faulet769d0e92019-03-22 14:23:18 +01001174 if (s->be->options & PR_O_ABRT_CLOSE) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001175 channel_auto_read(req);
Christopher Fauletc41547b2019-07-16 14:32:23 +02001176 if ((req->flags & (CF_SHUTR|CF_READ_NULL)) && !(txn->flags & TX_CON_WANT_TUN))
Christopher Faulete0768eb2018-10-03 16:38:02 +02001177 s->si[1].flags |= SI_FL_NOLINGER;
1178 channel_auto_close(req);
1179 }
1180 else if (s->txn->meth == HTTP_METH_POST) {
1181 /* POST requests may require to read extra CRLF sent by broken
1182 * browsers and which could cause an RST to be sent upon close
1183 * on some systems (eg: Linux). */
1184 channel_auto_read(req);
1185 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001186 DBG_TRACE_DEVEL("waiting for the end of the HTTP txn",
1187 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001188 return 0;
1189
1190 missing_data_or_waiting:
1191 /* stop waiting for data if the input is closed before the end */
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001192 if (msg->msg_state < HTTP_MSG_ENDING && req->flags & CF_SHUTR)
Christopher Faulet93e02d82019-03-08 14:18:50 +01001193 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001194
1195 waiting:
1196 /* waiting for the last bits to leave the buffer */
1197 if (req->flags & CF_SHUTW)
Christopher Faulet93e02d82019-03-08 14:18:50 +01001198 goto return_srv_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001199
1200 /* When TE: chunked is used, we need to get there again to parse remaining
1201 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
1202 * And when content-length is used, we never want to let the possible
1203 * shutdown be forwarded to the other side, as the state machine will
1204 * take care of it once the client responds. It's also important to
1205 * prevent TIME_WAITs from accumulating on the backend side, and for
1206 * HTTP/2 where the last frame comes with a shutdown.
1207 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001208 if (msg->flags & HTTP_MSGF_XFER_LEN)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001209 channel_dont_close(req);
1210
1211 /* We know that more data are expected, but we couldn't send more that
1212 * what we did. So we always set the CF_EXPECT_MORE flag so that the
1213 * system knows it must not set a PUSH on this first part. Interactive
1214 * modes are already handled by the stream sock layer. We must not do
1215 * this in content-length mode because it could present the MSG_MORE
1216 * flag with the last block of forwarded data, which would cause an
1217 * additional delay to be observed by the receiver.
1218 */
Christopher Faulet2151cdd2020-07-22 16:34:59 +02001219 if (HAS_REQ_DATA_FILTERS(s))
Christopher Faulete0768eb2018-10-03 16:38:02 +02001220 req->flags |= CF_EXPECT_MORE;
1221
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001222 DBG_TRACE_DEVEL("waiting for more data to forward",
1223 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001224 return 0;
1225
Christopher Faulet93e02d82019-03-08 14:18:50 +01001226 return_cli_abort:
Willy Tarreau4781b152021-04-06 13:53:36 +02001227 _HA_ATOMIC_INC(&sess->fe->fe_counters.cli_aborts);
1228 _HA_ATOMIC_INC(&s->be->be_counters.cli_aborts);
William Lallemand36119de2021-03-08 15:26:48 +01001229 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001230 _HA_ATOMIC_INC(&sess->listener->counters->cli_aborts);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001231 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001232 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.cli_aborts);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001233 if (!(s->flags & SF_ERR_MASK))
1234 s->flags |= SF_ERR_CLICL;
1235 status = 400;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001236 goto return_prx_cond;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001237
1238 return_srv_abort:
Willy Tarreau4781b152021-04-06 13:53:36 +02001239 _HA_ATOMIC_INC(&sess->fe->fe_counters.srv_aborts);
1240 _HA_ATOMIC_INC(&s->be->be_counters.srv_aborts);
William Lallemand36119de2021-03-08 15:26:48 +01001241 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001242 _HA_ATOMIC_INC(&sess->listener->counters->srv_aborts);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001243 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001244 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.srv_aborts);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001245 if (!(s->flags & SF_ERR_MASK))
1246 s->flags |= SF_ERR_SRVCL;
1247 status = 502;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001248 goto return_prx_cond;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001249
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001250 return_int_err:
1251 if (!(s->flags & SF_ERR_MASK))
1252 s->flags |= SF_ERR_INTERNAL;
Willy Tarreau4781b152021-04-06 13:53:36 +02001253 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
1254 _HA_ATOMIC_INC(&s->be->be_counters.internal_errors);
William Lallemand36119de2021-03-08 15:26:48 +01001255 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001256 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001257 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001258 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.internal_errors);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001259 status = 500;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001260 goto return_prx_cond;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001261
Christopher Faulet93e02d82019-03-08 14:18:50 +01001262 return_bad_req:
Willy Tarreau4781b152021-04-06 13:53:36 +02001263 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req);
William Lallemand36119de2021-03-08 15:26:48 +01001264 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001265 _HA_ATOMIC_INC(&sess->listener->counters->failed_req);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001266 status = 400;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001267 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001268
Christopher Fauletb8a53712019-12-16 11:29:38 +01001269 return_prx_cond:
Christopher Faulet9768c262018-10-22 09:34:31 +02001270 if (txn->status > 0) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001271 /* Note: we don't send any error if some data were already sent */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001272 http_reply_and_close(s, txn->status, NULL);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001273 } else {
Christopher Faulet93e02d82019-03-08 14:18:50 +01001274 txn->status = status;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001275 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001276 }
Christopher Fauletb8a53712019-12-16 11:29:38 +01001277 if (!(s->flags & SF_ERR_MASK))
1278 s->flags |= SF_ERR_PRXCOND;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001279 if (!(s->flags & SF_FINST_MASK))
1280 s->flags |= ((txn->rsp.msg_state < HTTP_MSG_ERROR) ? SF_FINST_H : SF_FINST_D);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001281 DBG_TRACE_DEVEL("leaving on error ",
1282 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001283 return 0;
1284}
1285
Olivier Houcharda254a372019-04-05 15:30:12 +02001286/* Reset the stream and the backend stream_interface to a situation suitable for attemption connection */
1287/* Returns 0 if we can attempt to retry, -1 otherwise */
1288static __inline int do_l7_retry(struct stream *s, struct stream_interface *si)
1289{
Christopher Faulet5bf85852021-05-21 13:46:14 +02001290 struct channel *req, *res;
1291 int co_data;
Olivier Houcharda254a372019-04-05 15:30:12 +02001292
1293 si->conn_retries--;
1294 if (si->conn_retries < 0)
Christopher Faulet043cdb22021-05-26 10:31:06 +02001295 return -1;
Christopher Faulet5b82cc52020-10-12 15:18:50 +02001296
Christopher Faulete763c8c2021-05-05 18:23:59 +02001297 if (objt_server(s->target)) {
1298 if (s->flags & SF_CURR_SESS) {
1299 s->flags &= ~SF_CURR_SESS;
1300 _HA_ATOMIC_DEC(&__objt_server(s->target)->cur_sess);
1301 }
Willy Tarreau4781b152021-04-06 13:53:36 +02001302 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.retries);
Christopher Faulete763c8c2021-05-05 18:23:59 +02001303 }
Willy Tarreau4781b152021-04-06 13:53:36 +02001304 _HA_ATOMIC_INC(&s->be->be_counters.retries);
Willy Tarreau223995e2019-05-04 10:38:31 +02001305
Christopher Faulet5bf85852021-05-21 13:46:14 +02001306 req = &s->req;
1307 res = &s->res;
Olivier Houcharda254a372019-04-05 15:30:12 +02001308 /* Remove any write error from the request, and read error from the response */
1309 req->flags &= ~(CF_WRITE_ERROR | CF_WRITE_TIMEOUT | CF_SHUTW | CF_SHUTW_NOW);
1310 res->flags &= ~(CF_READ_ERROR | CF_READ_TIMEOUT | CF_SHUTR | CF_EOI | CF_READ_NULL | CF_SHUTR_NOW);
Christopher Faulet42d4ee12022-01-04 10:56:03 +01001311 res->analysers &= AN_RES_FLT_END;
Olivier Houcharda254a372019-04-05 15:30:12 +02001312 si->flags &= ~(SI_FL_ERR | SI_FL_EXP | SI_FL_RXBLK_SHUT);
Christopher Faulet30aa0da2021-05-05 21:05:09 +02001313 si->err_type = SI_ET_NONE;
1314 s->flags &= ~(SF_ERR_MASK | SF_FINST_MASK);
Olivier Houchard4bd58672019-07-12 16:16:59 +02001315 stream_choose_redispatch(s);
Olivier Houcharda254a372019-04-05 15:30:12 +02001316 si->exp = TICK_ETERNITY;
1317 res->rex = TICK_ETERNITY;
1318 res->to_forward = 0;
1319 res->analyse_exp = TICK_ETERNITY;
1320 res->total = 0;
Olivier Houcharda254a372019-04-05 15:30:12 +02001321 si_release_endpoint(&s->si[1]);
Olivier Houcharda254a372019-04-05 15:30:12 +02001322
Christopher Faulet5bf85852021-05-21 13:46:14 +02001323 b_free(&req->buf);
1324 /* Swap the L7 buffer with the channel buffer */
1325 /* We know we stored the co_data as b_data, so get it there */
1326 co_data = b_data(&si->l7_buffer);
1327 b_set_data(&si->l7_buffer, b_size(&si->l7_buffer));
1328 b_xfer(&req->buf, &si->l7_buffer, b_data(&si->l7_buffer));
1329 co_set_data(req, co_data);
Christopher Faulet5b82cc52020-10-12 15:18:50 +02001330
Ilya Shipitsinacf84592021-02-06 22:29:08 +05001331 DBG_TRACE_DEVEL("perform a L7 retry", STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, s->txn);
Christopher Faulet5bf85852021-05-21 13:46:14 +02001332
Olivier Houcharda254a372019-04-05 15:30:12 +02001333 b_reset(&res->buf);
1334 co_set_data(res, 0);
1335 return 0;
1336}
1337
Christopher Faulete0768eb2018-10-03 16:38:02 +02001338/* This stream analyser waits for a complete HTTP response. It returns 1 if the
1339 * processing can continue on next analysers, or zero if it either needs more
1340 * data or wants to immediately abort the response (eg: timeout, error, ...). It
1341 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->res.analysers
1342 * when it has nothing left to do, and may remove any analyser when it wants to
1343 * abort.
1344 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001345int http_wait_for_response(struct stream *s, struct channel *rep, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001346{
Christopher Faulet9768c262018-10-22 09:34:31 +02001347 /*
1348 * We will analyze a complete HTTP response to check the its syntax.
1349 *
1350 * Once the start line and all headers are received, we may perform a
1351 * capture of the error (if any), and we will set a few fields. We also
1352 * logging and finally headers capture.
1353 */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001354 struct session *sess = s->sess;
1355 struct http_txn *txn = s->txn;
1356 struct http_msg *msg = &txn->rsp;
Christopher Faulet9768c262018-10-22 09:34:31 +02001357 struct htx *htx;
Olivier Houcharda254a372019-04-05 15:30:12 +02001358 struct stream_interface *si_b = &s->si[1];
Christopher Faulet61608322018-11-23 16:23:45 +01001359 struct connection *srv_conn;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001360 struct htx_sl *sl;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001361 int n;
1362
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001363 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001364
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001365 htx = htxbuf(&rep->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001366
Willy Tarreau4236f032019-03-05 10:43:32 +01001367 /* Parsing errors are caught here */
1368 if (htx->flags & HTX_FL_PARSING_ERROR)
1369 goto return_bad_res;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001370 if (htx->flags & HTX_FL_PROCESSING_ERROR)
1371 goto return_int_err;
Willy Tarreau4236f032019-03-05 10:43:32 +01001372
Christopher Faulete0768eb2018-10-03 16:38:02 +02001373 /*
1374 * Now we quickly check if we have found a full valid response.
1375 * If not so, we check the FD and buffer states before leaving.
1376 * A full response is indicated by the fact that we have seen
1377 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
1378 * responses are checked first.
1379 *
1380 * Depending on whether the client is still there or not, we
1381 * may send an error response back or not. Note that normally
1382 * we should only check for HTTP status there, and check I/O
1383 * errors somewhere else.
1384 */
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001385 next_one:
Christopher Faulet29f17582019-05-23 11:03:26 +02001386 if (unlikely(htx_is_empty(htx) || htx->first == -1)) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001387 /* 1: have we encountered a read error ? */
1388 if (rep->flags & CF_READ_ERROR) {
Olivier Houchard865d8392019-05-03 22:46:27 +02001389 struct connection *conn = NULL;
1390
Olivier Houchard865d8392019-05-03 22:46:27 +02001391 if (objt_cs(s->si[1].end))
Willy Tarreau66182592021-12-06 07:01:02 +00001392 conn = __objt_cs(s->si[1].end)->conn;
Olivier Houchard865d8392019-05-03 22:46:27 +02001393
Christopher Fauletb1875342021-05-26 12:15:37 +02001394 /* Perform a L7 retry because server refuses the early data. */
1395 if ((si_b->flags & SI_FL_L7_RETRY) &&
1396 (s->be->retry_type & PR_RE_EARLY_ERROR) &&
1397 conn && conn->err_code == CO_ER_SSL_EARLY_FAILED &&
1398 do_l7_retry(s, si_b) == 0) {
1399 DBG_TRACE_DEVEL("leaving on L7 retry",
1400 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
1401 return 0;
1402 }
1403
Olivier Houchard6db16992019-05-17 15:40:49 +02001404 if (txn->flags & TX_NOT_FIRST)
1405 goto abort_keep_alive;
1406
Willy Tarreau4781b152021-04-06 13:53:36 +02001407 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001408 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001409 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001410 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001411 }
1412
Christopher Fauletb1875342021-05-26 12:15:37 +02001413 /* if the server refused the early data, just send a 425 */
1414 if (conn && conn->err_code == CO_ER_SSL_EARLY_FAILED)
Olivier Houchard865d8392019-05-03 22:46:27 +02001415 txn->status = 425;
Christopher Fauletb1875342021-05-26 12:15:37 +02001416 else {
1417 txn->status = 502;
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001418 stream_inc_http_fail_ctr(s);
Christopher Fauletb1875342021-05-26 12:15:37 +02001419 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001420
1421 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001422 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001423
1424 if (!(s->flags & SF_ERR_MASK))
1425 s->flags |= SF_ERR_SRVCL;
1426 if (!(s->flags & SF_FINST_MASK))
1427 s->flags |= SF_FINST_H;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001428 DBG_TRACE_DEVEL("leaving on error",
1429 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001430 return 0;
1431 }
1432
Christopher Faulet9768c262018-10-22 09:34:31 +02001433 /* 2: read timeout : return a 504 to the client. */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001434 else if (rep->flags & CF_READ_TIMEOUT) {
Olivier Houcharda254a372019-04-05 15:30:12 +02001435 if ((si_b->flags & SI_FL_L7_RETRY) &&
1436 (s->be->retry_type & PR_RE_TIMEOUT)) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001437 if (co_data(rep) || do_l7_retry(s, si_b) == 0) {
1438 DBG_TRACE_DEVEL("leaving on L7 retry",
1439 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharda254a372019-04-05 15:30:12 +02001440 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001441 }
Olivier Houcharda254a372019-04-05 15:30:12 +02001442 }
Willy Tarreau4781b152021-04-06 13:53:36 +02001443 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001444 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001445 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001446 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001447 }
1448
Christopher Faulete0768eb2018-10-03 16:38:02 +02001449 txn->status = 504;
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001450 stream_inc_http_fail_ctr(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001451 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001452 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001453
1454 if (!(s->flags & SF_ERR_MASK))
1455 s->flags |= SF_ERR_SRVTO;
1456 if (!(s->flags & SF_FINST_MASK))
1457 s->flags |= SF_FINST_H;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001458 DBG_TRACE_DEVEL("leaving on error",
1459 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001460 return 0;
1461 }
1462
Christopher Faulet9768c262018-10-22 09:34:31 +02001463 /* 3: client abort with an abortonclose */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001464 else if ((rep->flags & CF_SHUTR) && ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001465 _HA_ATOMIC_INC(&sess->fe->fe_counters.cli_aborts);
1466 _HA_ATOMIC_INC(&s->be->be_counters.cli_aborts);
William Lallemand36119de2021-03-08 15:26:48 +01001467 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001468 _HA_ATOMIC_INC(&sess->listener->counters->cli_aborts);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001469 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001470 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.cli_aborts);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001471
Christopher Faulete0768eb2018-10-03 16:38:02 +02001472 txn->status = 400;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001473 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001474
1475 if (!(s->flags & SF_ERR_MASK))
1476 s->flags |= SF_ERR_CLICL;
1477 if (!(s->flags & SF_FINST_MASK))
1478 s->flags |= SF_FINST_H;
1479
1480 /* process_stream() will take care of the error */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001481 DBG_TRACE_DEVEL("leaving on error",
1482 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001483 return 0;
1484 }
1485
Christopher Faulet9768c262018-10-22 09:34:31 +02001486 /* 4: close from server, capture the response if the server has started to respond */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001487 else if (rep->flags & CF_SHUTR) {
Olivier Houcharda254a372019-04-05 15:30:12 +02001488 if ((si_b->flags & SI_FL_L7_RETRY) &&
1489 (s->be->retry_type & PR_RE_DISCONNECTED)) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001490 if (co_data(rep) || do_l7_retry(s, si_b) == 0) {
1491 DBG_TRACE_DEVEL("leaving on L7 retry",
1492 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharda254a372019-04-05 15:30:12 +02001493 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001494 }
Olivier Houcharda254a372019-04-05 15:30:12 +02001495 }
1496
Olivier Houchard6db16992019-05-17 15:40:49 +02001497 if (txn->flags & TX_NOT_FIRST)
1498 goto abort_keep_alive;
1499
Willy Tarreau4781b152021-04-06 13:53:36 +02001500 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001501 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001502 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001503 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001504 }
1505
Christopher Faulete0768eb2018-10-03 16:38:02 +02001506 txn->status = 502;
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001507 stream_inc_http_fail_ctr(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001508 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001509 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001510
1511 if (!(s->flags & SF_ERR_MASK))
1512 s->flags |= SF_ERR_SRVCL;
1513 if (!(s->flags & SF_FINST_MASK))
1514 s->flags |= SF_FINST_H;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001515 DBG_TRACE_DEVEL("leaving on error",
1516 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001517 return 0;
1518 }
1519
Christopher Faulet9768c262018-10-22 09:34:31 +02001520 /* 5: write error to client (we don't send any message then) */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001521 else if (rep->flags & CF_WRITE_ERROR) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001522 if (txn->flags & TX_NOT_FIRST)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001523 goto abort_keep_alive;
1524
Willy Tarreau4781b152021-04-06 13:53:36 +02001525 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001526 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001527 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001528 rep->analysers &= AN_RES_FLT_END;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001529
1530 if (!(s->flags & SF_ERR_MASK))
1531 s->flags |= SF_ERR_CLICL;
1532 if (!(s->flags & SF_FINST_MASK))
1533 s->flags |= SF_FINST_H;
1534
1535 /* process_stream() will take care of the error */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001536 DBG_TRACE_DEVEL("leaving on error",
1537 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001538 return 0;
1539 }
1540
1541 channel_dont_close(rep);
1542 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001543 DBG_TRACE_DEVEL("waiting for more data",
1544 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001545 return 0;
1546 }
1547
1548 /* More interesting part now : we know that we have a complete
1549 * response which at least looks like HTTP. We have an indicator
1550 * of each header's length, so we can parse them quickly.
1551 */
Christopher Faulet29f17582019-05-23 11:03:26 +02001552 BUG_ON(htx_get_first_type(htx) != HTX_BLK_RES_SL);
Christopher Faulet297fbb42019-05-13 14:41:27 +02001553 sl = http_get_stline(htx);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001554
Christopher Faulet0f66d552021-05-26 13:14:39 +02001555 /* Perform a L7 retry because of the status code */
1556 if ((si_b->flags & SI_FL_L7_RETRY) &&
1557 l7_status_match(s->be, sl->info.res.status) &&
1558 do_l7_retry(s, si_b) == 0) {
1559 DBG_TRACE_DEVEL("leaving on L7 retry", STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
1560 return 0;
1561 }
1562
1563 /* Now, L7 buffer is useless, it can be released */
1564 b_free(&s->si[1].l7_buffer);
1565
1566 msg->msg_state = HTTP_MSG_BODY;
1567
1568
Christopher Faulet9768c262018-10-22 09:34:31 +02001569 /* 0: we might have to print this header in debug mode */
1570 if (unlikely((global.mode & MODE_DEBUG) &&
1571 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
1572 int32_t pos;
1573
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001574 http_debug_stline("srvrep", s, sl);
Christopher Faulet9768c262018-10-22 09:34:31 +02001575
Christopher Fauleta3f15502019-05-13 15:27:23 +02001576 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001577 struct htx_blk *blk = htx_get_blk(htx, pos);
1578 enum htx_blk_type type = htx_get_blk_type(blk);
1579
1580 if (type == HTX_BLK_EOH)
1581 break;
1582 if (type != HTX_BLK_HDR)
1583 continue;
1584
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001585 http_debug_hdr("srvhdr", s,
1586 htx_get_blk_name(htx, blk),
1587 htx_get_blk_value(htx, blk));
Christopher Faulet9768c262018-10-22 09:34:31 +02001588 }
1589 }
1590
Christopher Faulet03599112018-11-27 11:21:21 +01001591 /* 1: get the status code and the version. Also set HTTP flags */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001592 txn->status = sl->info.res.status;
Christopher Faulet03599112018-11-27 11:21:21 +01001593 if (sl->flags & HTX_SL_F_VER_11)
Christopher Faulet9768c262018-10-22 09:34:31 +02001594 msg->flags |= HTTP_MSGF_VER_11;
Christopher Faulet03599112018-11-27 11:21:21 +01001595 if (sl->flags & HTX_SL_F_XFER_LEN) {
1596 msg->flags |= HTTP_MSGF_XFER_LEN;
Christopher Faulet2a408542020-11-20 14:22:37 +01001597 if (sl->flags & HTX_SL_F_CLEN)
1598 msg->flags |= HTTP_MSGF_CNT_LEN;
1599 else if (sl->flags & HTX_SL_F_CHNK)
1600 msg->flags |= HTTP_MSGF_TE_CHNK;
Christopher Faulet03599112018-11-27 11:21:21 +01001601 }
Christopher Faulet2a408542020-11-20 14:22:37 +01001602 if (sl->flags & HTX_SL_F_BODYLESS)
1603 msg->flags |= HTTP_MSGF_BODYLESS;
Christopher Faulet576c3582021-01-08 15:53:01 +01001604 if (sl->flags & HTX_SL_F_CONN_UPG)
1605 msg->flags |= HTTP_MSGF_CONN_UPG;
Christopher Faulet9768c262018-10-22 09:34:31 +02001606
1607 n = txn->status / 100;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001608 if (n < 1 || n > 5)
1609 n = 0;
Christopher Faulet9768c262018-10-22 09:34:31 +02001610
Christopher Faulete0768eb2018-10-03 16:38:02 +02001611 /* when the client triggers a 4xx from the server, it's most often due
1612 * to a missing object or permission. These events should be tracked
1613 * because if they happen often, it may indicate a brute force or a
1614 * vulnerability scan.
1615 */
1616 if (n == 4)
1617 stream_inc_http_err_ctr(s);
1618
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001619 if (n == 5 && txn->status != 501 && txn->status != 505)
1620 stream_inc_http_fail_ctr(s);
1621
Marcin Deranek3c27dda2020-05-15 18:32:51 +02001622 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001623 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.p.http.rsp[n]);
1624 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.p.http.cum_req);
Marcin Deranek3c27dda2020-05-15 18:32:51 +02001625 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001626
Christopher Faulete0768eb2018-10-03 16:38:02 +02001627 /* Adjust server's health based on status code. Note: status codes 501
1628 * and 505 are triggered on demand by client request, so we must not
1629 * count them as server failures.
1630 */
1631 if (objt_server(s->target)) {
1632 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001633 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_OK);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001634 else
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001635 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_STS);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001636 }
1637
1638 /*
1639 * We may be facing a 100-continue response, or any other informational
1640 * 1xx response which is non-final, in which case this is not the right
1641 * response, and we're waiting for the next one. Let's allow this response
1642 * to go to the client and wait for the next one. There's an exception for
1643 * 101 which is used later in the code to switch protocols.
1644 */
1645 if (txn->status < 200 &&
1646 (txn->status == 100 || txn->status >= 102)) {
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001647 FLT_STRM_CB(s, flt_http_reset(s, msg));
Christopher Faulet421e7692019-06-13 11:16:45 +02001648 htx->first = channel_htx_fwd_headers(rep, htx);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001649 msg->msg_state = HTTP_MSG_RPBEFORE;
Christopher Faulet3499f622019-09-03 15:23:54 +02001650 msg->flags = 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001651 txn->status = 0;
1652 s->logs.t_data = -1; /* was not a response yet */
Christopher Faulet7d518452020-08-31 11:07:07 +02001653 rep->flags |= CF_SEND_DONTWAIT; /* Send ASAP informational messages */
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001654 goto next_one;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001655 }
1656
Christopher Faulet6e6c7b12021-01-08 16:02:05 +01001657 /* A 101-switching-protocols must contains a Connection header with the
1658 * "upgrade" option and the request too. It means both are agree to
1659 * upgrade. It is not so strict because there is no test on the Upgrade
1660 * header content. But it is probably stronger enough for now.
1661 */
1662 if (txn->status == 101 &&
1663 (!(txn->req.flags & HTTP_MSGF_CONN_UPG) || !(txn->rsp.flags & HTTP_MSGF_CONN_UPG)))
1664 goto return_bad_res;
1665
Christopher Faulete0768eb2018-10-03 16:38:02 +02001666 /*
1667 * 2: check for cacheability.
1668 */
1669
1670 switch (txn->status) {
1671 case 200:
1672 case 203:
1673 case 204:
1674 case 206:
1675 case 300:
1676 case 301:
1677 case 404:
1678 case 405:
1679 case 410:
1680 case 414:
1681 case 501:
1682 break;
1683 default:
1684 /* RFC7231#6.1:
1685 * Responses with status codes that are defined as
1686 * cacheable by default (e.g., 200, 203, 204, 206,
1687 * 300, 301, 404, 405, 410, 414, and 501 in this
1688 * specification) can be reused by a cache with
1689 * heuristic expiration unless otherwise indicated
1690 * by the method definition or explicit cache
1691 * controls [RFC7234]; all other status codes are
1692 * not cacheable by default.
1693 */
1694 txn->flags &= ~(TX_CACHEABLE | TX_CACHE_COOK);
1695 break;
1696 }
1697
1698 /*
1699 * 3: we may need to capture headers
1700 */
1701 s->logs.logwait &= ~LW_RESP;
1702 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->res_cap))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001703 http_capture_headers(htx, s->res_cap, sess->fe->rsp_cap);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001704
Christopher Faulet9768c262018-10-22 09:34:31 +02001705 /* Skip parsing if no content length is possible. */
Christopher Fauletc75668e2020-12-07 18:10:32 +01001706 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status >= 200 && txn->status < 300) ||
Christopher Faulete0768eb2018-10-03 16:38:02 +02001707 txn->status == 101)) {
1708 /* Either we've established an explicit tunnel, or we're
1709 * switching the protocol. In both cases, we're very unlikely
1710 * to understand the next protocols. We have to switch to tunnel
1711 * mode, so that we transfer the request and responses then let
1712 * this protocol pass unmodified. When we later implement specific
1713 * parsers for such protocols, we'll want to check the Upgrade
1714 * header which contains information about that protocol for
1715 * responses with status 101 (eg: see RFC2817 about TLS).
1716 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02001717 txn->flags |= TX_CON_WANT_TUN;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001718 }
1719
Christopher Faulet61608322018-11-23 16:23:45 +01001720 /* check for NTML authentication headers in 401 (WWW-Authenticate) and
1721 * 407 (Proxy-Authenticate) responses and set the connection to private
1722 */
1723 srv_conn = cs_conn(objt_cs(s->si[1].end));
1724 if (srv_conn) {
1725 struct ist hdr;
1726 struct http_hdr_ctx ctx;
1727
1728 if (txn->status == 401)
1729 hdr = ist("WWW-Authenticate");
1730 else if (txn->status == 407)
1731 hdr = ist("Proxy-Authenticate");
1732 else
1733 goto end;
1734
1735 ctx.blk = NULL;
1736 while (http_find_header(htx, hdr, &ctx, 0)) {
Willy Tarreauf1dcced2020-05-07 19:27:02 +02001737 /* If www-authenticate contains "Negotiate", "Nego2", or "NTLM",
1738 * possibly followed by blanks and a base64 string, the connection
1739 * is private. Since it's a mess to deal with, we only check for
1740 * values starting with "NTLM" or "Nego". Note that often multiple
1741 * headers are sent by the server there.
1742 */
1743 if ((ctx.value.len >= 4 && strncasecmp(ctx.value.ptr, "Nego", 4) == 0) ||
Willy Tarreau49a1d282020-05-07 19:10:15 +02001744 (ctx.value.len >= 4 && strncasecmp(ctx.value.ptr, "NTLM", 4) == 0)) {
Olivier Houchard250031e2019-05-29 15:01:50 +02001745 sess->flags |= SESS_FL_PREFER_LAST;
Christopher Faulet08016ab2020-07-01 16:10:06 +02001746 conn_set_owner(srv_conn, sess, NULL);
Christopher Faulet21ddc742020-07-01 15:26:14 +02001747 conn_set_private(srv_conn);
Ilya Shipitsin6b79f382020-07-23 00:32:55 +05001748 /* If it fail now, the same will be done in mux->detach() callback */
Christopher Faulet08016ab2020-07-01 16:10:06 +02001749 session_add_conn(srv_conn->owner, srv_conn, srv_conn->target);
Willy Tarreauf1dcced2020-05-07 19:27:02 +02001750 break;
Olivier Houchard250031e2019-05-29 15:01:50 +02001751 }
Christopher Faulet61608322018-11-23 16:23:45 +01001752 }
1753 }
1754
1755 end:
Christopher Faulete0768eb2018-10-03 16:38:02 +02001756 /* we want to have the response time before we start processing it */
1757 s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now);
1758
1759 /* end of job, return OK */
1760 rep->analysers &= ~an_bit;
1761 rep->analyse_exp = TICK_ETERNITY;
1762 channel_auto_close(rep);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001763 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001764 return 1;
1765
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001766 return_int_err:
Willy Tarreau4781b152021-04-06 13:53:36 +02001767 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
1768 _HA_ATOMIC_INC(&s->be->be_counters.internal_errors);
William Lallemand36119de2021-03-08 15:26:48 +01001769 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001770 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Fauletb8a53712019-12-16 11:29:38 +01001771 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001772 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.internal_errors);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001773 txn->status = 500;
1774 if (!(s->flags & SF_ERR_MASK))
1775 s->flags |= SF_ERR_INTERNAL;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001776 goto return_prx_cond;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001777
1778 return_bad_res:
Willy Tarreau4781b152021-04-06 13:53:36 +02001779 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Faulet47365272018-10-31 17:40:50 +01001780 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001781 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001782 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Christopher Faulet47365272018-10-31 17:40:50 +01001783 }
Olivier Houcharde3249a92019-05-03 23:01:47 +02001784 if ((s->be->retry_type & PR_RE_JUNK_REQUEST) &&
Olivier Houchardad26d8d2019-05-10 17:48:28 +02001785 (si_b->flags & SI_FL_L7_RETRY) &&
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001786 do_l7_retry(s, si_b) == 0) {
1787 DBG_TRACE_DEVEL("leaving on L7 retry",
1788 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharde3249a92019-05-03 23:01:47 +02001789 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001790 }
Christopher Faulet47365272018-10-31 17:40:50 +01001791 txn->status = 502;
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001792 stream_inc_http_fail_ctr(s);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001793 /* fall through */
1794
Christopher Fauletb8a53712019-12-16 11:29:38 +01001795 return_prx_cond:
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001796 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet47365272018-10-31 17:40:50 +01001797
1798 if (!(s->flags & SF_ERR_MASK))
1799 s->flags |= SF_ERR_PRXCOND;
1800 if (!(s->flags & SF_FINST_MASK))
1801 s->flags |= SF_FINST_H;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001802
1803 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001804 DBG_TRACE_DEVEL("leaving on error",
1805 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulet47365272018-10-31 17:40:50 +01001806 return 0;
1807
Christopher Faulete0768eb2018-10-03 16:38:02 +02001808 abort_keep_alive:
1809 /* A keep-alive request to the server failed on a network error.
1810 * The client is required to retry. We need to close without returning
1811 * any other information so that the client retries.
1812 */
1813 txn->status = 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001814 s->logs.logwait = 0;
1815 s->logs.level = 0;
1816 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001817 http_reply_and_close(s, txn->status, NULL);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001818 DBG_TRACE_DEVEL("leaving by closing K/A connection",
1819 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001820 return 0;
1821}
1822
1823/* This function performs all the processing enabled for the current response.
1824 * It normally returns 1 unless it wants to break. It relies on buffers flags,
1825 * and updates s->res.analysers. It might make sense to explode it into several
1826 * other functions. It works like process_request (see indications above).
1827 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001828int http_process_res_common(struct stream *s, struct channel *rep, int an_bit, struct proxy *px)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001829{
1830 struct session *sess = s->sess;
1831 struct http_txn *txn = s->txn;
1832 struct http_msg *msg = &txn->rsp;
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001833 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001834 struct proxy *cur_proxy;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001835 enum rule_result ret = HTTP_RULE_RES_CONT;
1836
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001837 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
1838 return 0;
Christopher Faulet9768c262018-10-22 09:34:31 +02001839
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001840 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001841
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001842 htx = htxbuf(&rep->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001843
1844 /* The stats applet needs to adjust the Connection header but we don't
1845 * apply any filter there.
1846 */
1847 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
1848 rep->analysers &= ~an_bit;
1849 rep->analyse_exp = TICK_ETERNITY;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001850 goto end;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001851 }
1852
1853 /*
1854 * We will have to evaluate the filters.
1855 * As opposed to version 1.2, now they will be evaluated in the
1856 * filters order and not in the header order. This means that
1857 * each filter has to be validated among all headers.
1858 *
1859 * Filters are tried with ->be first, then with ->fe if it is
1860 * different from ->be.
1861 *
1862 * Maybe we are in resume condiion. In this case I choose the
1863 * "struct proxy" which contains the rule list matching the resume
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05001864 * pointer. If none of these "struct proxy" match, I initialise
Christopher Faulete0768eb2018-10-03 16:38:02 +02001865 * the process with the first one.
1866 *
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05001867 * In fact, I check only correspondence between the current list
Christopher Faulete0768eb2018-10-03 16:38:02 +02001868 * pointer and the ->fe rule list. If it doesn't match, I initialize
1869 * the loop with the ->be.
1870 */
1871 if (s->current_rule_list == &sess->fe->http_res_rules)
1872 cur_proxy = sess->fe;
1873 else
1874 cur_proxy = s->be;
1875 while (1) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001876 /* evaluate http-response rules */
Christopher Fauletb4c4a972021-11-09 16:33:25 +01001877 if (ret == HTTP_RULE_RES_CONT || ret == HTTP_RULE_RES_STOP) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001878 ret = http_res_get_intercept_rule(cur_proxy, &cur_proxy->http_res_rules, s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001879
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001880 switch (ret) {
1881 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
1882 goto return_prx_yield;
1883
1884 case HTTP_RULE_RES_CONT:
1885 case HTTP_RULE_RES_STOP: /* nothing to do */
1886 break;
1887
1888 case HTTP_RULE_RES_DENY: /* deny or tarpit */
1889 goto deny;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001890
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001891 case HTTP_RULE_RES_ABRT: /* abort request, response already sent */
1892 goto return_prx_cond;
1893
1894 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
Christopher Fauletb8a53712019-12-16 11:29:38 +01001895 goto done;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001896
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001897 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
1898 goto return_bad_res;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001899
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001900 case HTTP_RULE_RES_ERROR: /* failed with a bad request */
1901 goto return_int_err;
1902 }
1903
1904 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001905
Christopher Faulete0768eb2018-10-03 16:38:02 +02001906 /* check whether we're already working on the frontend */
1907 if (cur_proxy == sess->fe)
1908 break;
1909 cur_proxy = sess->fe;
1910 }
1911
Christopher Faulete0768eb2018-10-03 16:38:02 +02001912 /* OK that's all we can do for 1xx responses */
1913 if (unlikely(txn->status < 200 && txn->status != 101))
Christopher Fauletf2824e62018-10-01 12:12:37 +02001914 goto end;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001915
1916 /*
1917 * Now check for a server cookie.
1918 */
1919 if (s->be->cookie_name || sess->fe->capture_name || (s->be->options & PR_O_CHK_CACHE))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001920 http_manage_server_side_cookies(s, rep);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001921
1922 /*
1923 * Check for cache-control or pragma headers if required.
1924 */
1925 if ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001926 http_check_response_for_cacheability(s, rep);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001927
1928 /*
1929 * Add server cookie in the response if needed
1930 */
1931 if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) &&
1932 !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) &&
1933 (!(s->flags & SF_DIRECT) ||
1934 ((s->be->cookie_maxidle || txn->cookie_last_date) &&
1935 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
1936 (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
1937 (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
1938 (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
1939 !(s->flags & SF_IGNORE_PRST)) {
1940 /* the server is known, it's not the one the client requested, or the
1941 * cookie's last seen date needs to be refreshed. We have to
1942 * insert a set-cookie here, except if we want to insert only on POST
1943 * requests and this one isn't. Note that servers which don't have cookies
1944 * (eg: some backup servers) will return a full cookie removal request.
1945 */
Willy Tarreau66182592021-12-06 07:01:02 +00001946 if (!__objt_server(s->target)->cookie) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001947 chunk_printf(&trash,
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001948 "%s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
Christopher Faulete0768eb2018-10-03 16:38:02 +02001949 s->be->cookie_name);
1950 }
1951 else {
Willy Tarreau66182592021-12-06 07:01:02 +00001952 chunk_printf(&trash, "%s=%s", s->be->cookie_name, __objt_server(s->target)->cookie);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001953
1954 if (s->be->cookie_maxidle || s->be->cookie_maxlife) {
1955 /* emit last_date, which is mandatory */
1956 trash.area[trash.data++] = COOKIE_DELIM_DATE;
1957 s30tob64((date.tv_sec+3) >> 2,
1958 trash.area + trash.data);
1959 trash.data += 5;
1960
1961 if (s->be->cookie_maxlife) {
1962 /* emit first_date, which is either the original one or
1963 * the current date.
1964 */
1965 trash.area[trash.data++] = COOKIE_DELIM_DATE;
1966 s30tob64(txn->cookie_first_date ?
1967 txn->cookie_first_date >> 2 :
1968 (date.tv_sec+3) >> 2,
1969 trash.area + trash.data);
1970 trash.data += 5;
1971 }
1972 }
1973 chunk_appendf(&trash, "; path=/");
1974 }
1975
1976 if (s->be->cookie_domain)
1977 chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain);
1978
1979 if (s->be->ck_opts & PR_CK_HTTPONLY)
1980 chunk_appendf(&trash, "; HttpOnly");
1981
1982 if (s->be->ck_opts & PR_CK_SECURE)
1983 chunk_appendf(&trash, "; Secure");
1984
Christopher Faulet2f533902020-01-21 11:06:48 +01001985 if (s->be->cookie_attrs)
1986 chunk_appendf(&trash, "; %s", s->be->cookie_attrs);
1987
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001988 if (unlikely(!http_add_header(htx, ist("Set-Cookie"), ist2(trash.area, trash.data))))
Christopher Faulet028c1642022-06-01 17:42:35 +02001989 goto return_fail_rewrite;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001990
1991 txn->flags &= ~TX_SCK_MASK;
1992 if (__objt_server(s->target)->cookie && (s->flags & SF_DIRECT))
1993 /* the server did not change, only the date was updated */
1994 txn->flags |= TX_SCK_UPDATED;
1995 else
1996 txn->flags |= TX_SCK_INSERTED;
1997
1998 /* Here, we will tell an eventual cache on the client side that we don't
1999 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
2000 * Some caches understand the correct form: 'no-cache="set-cookie"', but
2001 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
2002 */
2003 if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
2004
2005 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
2006
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002007 if (unlikely(!http_add_header(htx, ist("Cache-control"), ist("private"))))
Christopher Faulet028c1642022-06-01 17:42:35 +02002008 goto return_fail_rewrite;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002009 }
2010 }
2011
2012 /*
2013 * Check if result will be cacheable with a cookie.
2014 * We'll block the response if security checks have caught
2015 * nasty things such as a cacheable cookie.
2016 */
2017 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
2018 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
2019 (s->be->options & PR_O_CHK_CACHE)) {
2020 /* we're in presence of a cacheable response containing
2021 * a set-cookie header. We'll block it as requested by
2022 * the 'checkcache' option, and send an alert.
2023 */
Christopher Faulete0768eb2018-10-03 16:38:02 +02002024 ha_alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreau66182592021-12-06 07:01:02 +00002025 s->be->id, objt_server(s->target) ? __objt_server(s->target)->id : "<dispatch>");
Christopher Faulete0768eb2018-10-03 16:38:02 +02002026 send_log(s->be, LOG_ALERT,
2027 "Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreau66182592021-12-06 07:01:02 +00002028 s->be->id, objt_server(s->target) ? __objt_server(s->target)->id : "<dispatch>");
Christopher Fauletb8a53712019-12-16 11:29:38 +01002029 goto deny;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002030 }
2031
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002032 end:
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002033 /*
2034 * Evaluate after-response rules before forwarding the response. rules
2035 * from the backend are evaluated first, then one from the frontend if
2036 * it differs.
2037 */
2038 if (!http_eval_after_res_rules(s))
2039 goto return_int_err;
2040
Christopher Fauletc2ac5e42021-03-08 18:20:09 +01002041 /* Filter the response headers if there are filters attached to the
2042 * stream.
2043 */
2044 if (HAS_FILTERS(s))
2045 rep->analysers |= AN_RES_FLT_HTTP_HDRS;
2046
Christopher Faulete0768eb2018-10-03 16:38:02 +02002047 /* Always enter in the body analyzer */
2048 rep->analysers &= ~AN_RES_FLT_XFER_DATA;
2049 rep->analysers |= AN_RES_HTTP_XFER_BODY;
2050
2051 /* if the user wants to log as soon as possible, without counting
2052 * bytes from the server, then this is the right moment. We have
2053 * to temporarily assign bytes_out to log what we currently have.
2054 */
2055 if (!LIST_ISEMPTY(&sess->fe->logformat) && !(s->logs.logwait & LW_BYTES)) {
2056 s->logs.t_close = s->logs.t_data; /* to get a valid end date */
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002057 s->logs.bytes_out = htx->data;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002058 s->do_log(s);
2059 s->logs.bytes_out = 0;
2060 }
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002061
Christopher Fauletb8a53712019-12-16 11:29:38 +01002062 done:
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002063 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002064 rep->analysers &= ~an_bit;
2065 rep->analyse_exp = TICK_ETERNITY;
2066 return 1;
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002067
Christopher Fauletb8a53712019-12-16 11:29:38 +01002068 deny:
Willy Tarreau4781b152021-04-06 13:53:36 +02002069 _HA_ATOMIC_INC(&sess->fe->fe_counters.denied_resp);
2070 _HA_ATOMIC_INC(&s->be->be_counters.denied_resp);
William Lallemand36119de2021-03-08 15:26:48 +01002071 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02002072 _HA_ATOMIC_INC(&sess->listener->counters->denied_resp);
Christopher Fauleta08546b2019-12-16 16:07:34 +01002073 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02002074 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.denied_resp);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002075 goto return_prx_err;
2076
Christopher Faulet028c1642022-06-01 17:42:35 +02002077 return_fail_rewrite:
2078 if (!(s->flags & SF_ERR_MASK))
2079 s->flags |= SF_ERR_PRXCOND;
2080 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_rewrites);
2081 _HA_ATOMIC_INC(&s->be->be_counters.failed_rewrites);
2082 if (sess->listener && sess->listener->counters)
2083 _HA_ATOMIC_INC(&sess->listener->counters->failed_rewrites);
2084 if (objt_server(s->target))
2085 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_rewrites);
2086 /* fall through */
2087
Christopher Fauletb8a53712019-12-16 11:29:38 +01002088 return_int_err:
2089 txn->status = 500;
2090 if (!(s->flags & SF_ERR_MASK))
2091 s->flags |= SF_ERR_INTERNAL;
Willy Tarreau4781b152021-04-06 13:53:36 +02002092 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
2093 _HA_ATOMIC_INC(&s->be->be_counters.internal_errors);
Dragan Dosen84426cd2021-09-21 13:02:09 +02002094 if (sess->listener && sess->listener->counters)
2095 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Fauletcff0f732019-12-16 16:13:44 +01002096 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02002097 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.internal_errors);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002098 goto return_prx_err;
2099
2100 return_bad_res:
2101 txn->status = 502;
Willy Tarreau826f3ab2021-02-10 12:07:15 +01002102 stream_inc_http_fail_ctr(s);
Willy Tarreau4781b152021-04-06 13:53:36 +02002103 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Fauleta20a6532020-02-05 10:16:41 +01002104 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02002105 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Christopher Fauleta20a6532020-02-05 10:16:41 +01002106 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_RSP);
2107 }
Christopher Fauletb8a53712019-12-16 11:29:38 +01002108 /* fall through */
2109
2110 return_prx_err:
2111 http_reply_and_close(s, txn->status, http_error_message(s));
2112 /* fall through */
2113
2114 return_prx_cond:
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002115 s->logs.t_data = -1; /* was not a valid response */
2116 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002117
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002118 if (!(s->flags & SF_ERR_MASK))
2119 s->flags |= SF_ERR_PRXCOND;
2120 if (!(s->flags & SF_FINST_MASK))
2121 s->flags |= SF_FINST_H;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002122
Christopher Faulete58c0002020-03-02 16:21:01 +01002123 rep->analysers &= AN_RES_FLT_END;
2124 s->req.analysers &= AN_REQ_FLT_END;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002125 rep->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002126 DBG_TRACE_DEVEL("leaving on error",
2127 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002128 return 0;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002129
2130 return_prx_yield:
2131 channel_dont_close(rep);
2132 DBG_TRACE_DEVEL("waiting for more data",
2133 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
2134 return 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002135}
2136
2137/* This function is an analyser which forwards response body (including chunk
2138 * sizes if any). It is called as soon as we must forward, even if we forward
2139 * zero byte. The only situation where it must not be called is when we're in
2140 * tunnel mode and we want to forward till the close. It's used both to forward
2141 * remaining data and to resync after end of body. It expects the msg_state to
2142 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
2143 * read more data, or 1 once we can go on with next request or end the stream.
2144 *
2145 * It is capable of compressing response data both in content-length mode and
2146 * in chunked mode. The state machines follows different flows depending on
2147 * whether content-length and chunked modes are used, since there are no
2148 * trailers in content-length :
2149 *
2150 * chk-mode cl-mode
2151 * ,----- BODY -----.
2152 * / \
2153 * V size > 0 V chk-mode
2154 * .--> SIZE -------------> DATA -------------> CRLF
2155 * | | size == 0 | last byte |
2156 * | v final crlf v inspected |
2157 * | TRAILERS -----------> DONE |
2158 * | |
2159 * `----------------------------------------------'
2160 *
2161 * Compression only happens in the DATA state, and must be flushed in final
2162 * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding
2163 * is performed at once on final states for all bytes parsed, or when leaving
2164 * on missing data.
2165 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002166int http_response_forward_body(struct stream *s, struct channel *res, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +02002167{
2168 struct session *sess = s->sess;
2169 struct http_txn *txn = s->txn;
2170 struct http_msg *msg = &s->txn->rsp;
Christopher Faulet9768c262018-10-22 09:34:31 +02002171 struct htx *htx;
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002172 int ret;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002173
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002174 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002175
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002176 htx = htxbuf(&res->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002177
Christopher Fauletb9a92f32019-09-09 10:15:21 +02002178 if (htx->flags & HTX_FL_PARSING_ERROR)
2179 goto return_bad_res;
2180 if (htx->flags & HTX_FL_PROCESSING_ERROR)
2181 goto return_int_err;
2182
Christopher Faulete0768eb2018-10-03 16:38:02 +02002183 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Christopher Fauletf2824e62018-10-01 12:12:37 +02002184 ((res->flags & CF_SHUTW) && (res->to_forward || co_data(res)))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02002185 /* Output closed while we were sending data. We must abort and
2186 * wake the other side up.
2187 */
Christopher Faulete0768eb2018-10-03 16:38:02 +02002188 msg->msg_state = HTTP_MSG_ERROR;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002189 http_end_response(s);
2190 http_end_request(s);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002191 DBG_TRACE_DEVEL("leaving on error",
2192 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002193 return 1;
2194 }
2195
Christopher Faulet9768c262018-10-22 09:34:31 +02002196 if (msg->msg_state == HTTP_MSG_BODY)
2197 msg->msg_state = HTTP_MSG_DATA;
2198
Christopher Faulete0768eb2018-10-03 16:38:02 +02002199 /* in most states, we should abort in case of early close */
2200 channel_auto_close(res);
2201
Christopher Faulete0768eb2018-10-03 16:38:02 +02002202 if (res->to_forward) {
Christopher Faulet66af0b22019-03-22 14:54:52 +01002203 if (res->to_forward == CHN_INFINITE_FORWARD) {
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002204 if (res->flags & CF_EOI)
2205 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet66af0b22019-03-22 14:54:52 +01002206 }
2207 else {
2208 /* We can't process the buffer's contents yet */
2209 res->flags |= CF_WAKE_WRITE;
2210 goto missing_data_or_waiting;
2211 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02002212 }
2213
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002214 if (msg->msg_state >= HTTP_MSG_ENDING)
2215 goto ending;
2216
Christopher Fauletc75668e2020-12-07 18:10:32 +01002217 if ((txn->meth == HTTP_METH_CONNECT && txn->status >= 200 && txn->status < 300) || txn->status == 101 ||
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002218 (!(msg->flags & HTTP_MSGF_XFER_LEN) && !HAS_RSP_DATA_FILTERS(s))) {
2219 msg->msg_state = HTTP_MSG_ENDING;
2220 goto ending;
2221 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002222
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002223 /* Forward input data. We get it by removing all outgoing data not
2224 * forwarded yet from HTX data size. If there are some data filters, we
2225 * let them decide the amount of data to forward.
Christopher Faulet9768c262018-10-22 09:34:31 +02002226 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002227 if (HAS_RSP_DATA_FILTERS(s)) {
2228 ret = flt_http_payload(s, msg, htx->data);
2229 if (ret < 0)
2230 goto return_bad_res;
Christopher Faulet421e7692019-06-13 11:16:45 +02002231 c_adv(res, ret);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002232 }
2233 else {
Christopher Faulet421e7692019-06-13 11:16:45 +02002234 c_adv(res, htx->data - co_data(res));
Christopher Faulet66af0b22019-03-22 14:54:52 +01002235 if (msg->flags & HTTP_MSGF_XFER_LEN)
2236 channel_htx_forward_forever(res, htx);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002237 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002238
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002239 if (htx->data != co_data(res))
2240 goto missing_data_or_waiting;
2241
2242 if (!(msg->flags & HTTP_MSGF_XFER_LEN) && res->flags & CF_SHUTR) {
2243 msg->msg_state = HTTP_MSG_ENDING;
2244 goto ending;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002245 }
2246
Christopher Faulet9768c262018-10-22 09:34:31 +02002247 /* Check if the end-of-message is reached and if so, switch the message
Christopher Fauletd20fdb02019-06-13 16:43:22 +02002248 * in HTTP_MSG_ENDING state. Then if all data was marked to be
2249 * forwarded, set the state to HTTP_MSG_DONE.
Christopher Faulet9768c262018-10-22 09:34:31 +02002250 */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002251 if (!(htx->flags & HTX_FL_EOM))
Christopher Faulet9768c262018-10-22 09:34:31 +02002252 goto missing_data_or_waiting;
2253
Christopher Fauletd20fdb02019-06-13 16:43:22 +02002254 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet9768c262018-10-22 09:34:31 +02002255
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002256 ending:
Christopher Faulet2151cdd2020-07-22 16:34:59 +02002257 res->flags &= ~CF_EXPECT_MORE; /* no more data are expected */
2258
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002259 /* other states, ENDING...TUNNEL */
2260 if (msg->msg_state >= HTTP_MSG_DONE)
2261 goto done;
Christopher Faulet9768c262018-10-22 09:34:31 +02002262
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002263 if (HAS_RSP_DATA_FILTERS(s)) {
2264 ret = flt_http_end(s, msg);
2265 if (ret <= 0) {
2266 if (!ret)
2267 goto missing_data_or_waiting;
2268 goto return_bad_res;
2269 }
2270 }
2271
Christopher Fauletc75668e2020-12-07 18:10:32 +01002272 if ((txn->meth == HTTP_METH_CONNECT && txn->status >= 200 && txn->status < 300) || txn->status == 101 ||
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002273 !(msg->flags & HTTP_MSGF_XFER_LEN)) {
2274 msg->msg_state = HTTP_MSG_TUNNEL;
2275 goto ending;
2276 }
2277 else {
2278 msg->msg_state = HTTP_MSG_DONE;
2279 res->to_forward = 0;
2280 }
2281
2282 done:
2283
2284 channel_dont_close(res);
2285
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002286 http_end_response(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002287 if (!(res->analysers & an_bit)) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002288 http_end_request(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002289 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
2290 if (res->flags & CF_SHUTW) {
2291 /* response errors are most likely due to the
2292 * client aborting the transfer. */
Christopher Faulet93e02d82019-03-08 14:18:50 +01002293 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002294 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02002295 goto return_bad_res;
2296 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002297 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002298 return 1;
2299 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002300 DBG_TRACE_DEVEL("waiting for the end of the HTTP txn",
2301 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002302 return 0;
2303
2304 missing_data_or_waiting:
2305 if (res->flags & CF_SHUTW)
Christopher Faulet93e02d82019-03-08 14:18:50 +01002306 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002307
2308 /* stop waiting for data if the input is closed before the end. If the
2309 * client side was already closed, it means that the client has aborted,
2310 * so we don't want to count this as a server abort. Otherwise it's a
2311 * server abort.
2312 */
Christopher Fauletd20fdb02019-06-13 16:43:22 +02002313 if (msg->msg_state < HTTP_MSG_ENDING && res->flags & CF_SHUTR) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02002314 if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))
Christopher Faulet93e02d82019-03-08 14:18:50 +01002315 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002316 /* If we have some pending data, we continue the processing */
Christopher Faulet93e02d82019-03-08 14:18:50 +01002317 if (htx_is_empty(htx))
2318 goto return_srv_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002319 }
2320
Christopher Faulete0768eb2018-10-03 16:38:02 +02002321 /* When TE: chunked is used, we need to get there again to parse
2322 * remaining chunks even if the server has closed, so we don't want to
Christopher Faulet9768c262018-10-22 09:34:31 +02002323 * set CF_DONTCLOSE. Similarly when there is a content-leng or if there
2324 * are filters registered on the stream, we don't want to forward a
2325 * close
Christopher Faulete0768eb2018-10-03 16:38:02 +02002326 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002327 if ((msg->flags & HTTP_MSGF_XFER_LEN) || HAS_RSP_DATA_FILTERS(s))
Christopher Faulete0768eb2018-10-03 16:38:02 +02002328 channel_dont_close(res);
2329
2330 /* We know that more data are expected, but we couldn't send more that
2331 * what we did. So we always set the CF_EXPECT_MORE flag so that the
2332 * system knows it must not set a PUSH on this first part. Interactive
2333 * modes are already handled by the stream sock layer. We must not do
2334 * this in content-length mode because it could present the MSG_MORE
2335 * flag with the last block of forwarded data, which would cause an
2336 * additional delay to be observed by the receiver.
2337 */
Christopher Faulet2151cdd2020-07-22 16:34:59 +02002338 if (HAS_RSP_DATA_FILTERS(s))
Christopher Faulete0768eb2018-10-03 16:38:02 +02002339 res->flags |= CF_EXPECT_MORE;
2340
2341 /* the stream handler will take care of timeouts and errors */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002342 DBG_TRACE_DEVEL("waiting for more data to forward",
2343 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002344 return 0;
2345
Christopher Faulet93e02d82019-03-08 14:18:50 +01002346 return_srv_abort:
Willy Tarreau4781b152021-04-06 13:53:36 +02002347 _HA_ATOMIC_INC(&sess->fe->fe_counters.srv_aborts);
2348 _HA_ATOMIC_INC(&s->be->be_counters.srv_aborts);
William Lallemand36119de2021-03-08 15:26:48 +01002349 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02002350 _HA_ATOMIC_INC(&sess->listener->counters->srv_aborts);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002351 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02002352 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.srv_aborts);
Willy Tarreau826f3ab2021-02-10 12:07:15 +01002353 stream_inc_http_fail_ctr(s);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002354 if (!(s->flags & SF_ERR_MASK))
2355 s->flags |= SF_ERR_SRVCL;
2356 goto return_error;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002357
Christopher Faulet93e02d82019-03-08 14:18:50 +01002358 return_cli_abort:
Willy Tarreau4781b152021-04-06 13:53:36 +02002359 _HA_ATOMIC_INC(&sess->fe->fe_counters.cli_aborts);
2360 _HA_ATOMIC_INC(&s->be->be_counters.cli_aborts);
William Lallemand36119de2021-03-08 15:26:48 +01002361 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02002362 _HA_ATOMIC_INC(&sess->listener->counters->cli_aborts);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002363 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02002364 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.cli_aborts);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002365 if (!(s->flags & SF_ERR_MASK))
2366 s->flags |= SF_ERR_CLICL;
2367 goto return_error;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002368
Christopher Fauletb9a92f32019-09-09 10:15:21 +02002369 return_int_err:
Willy Tarreau4781b152021-04-06 13:53:36 +02002370 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
2371 _HA_ATOMIC_INC(&s->be->be_counters.internal_errors);
William Lallemand36119de2021-03-08 15:26:48 +01002372 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02002373 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002374 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02002375 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.internal_errors);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02002376 if (!(s->flags & SF_ERR_MASK))
2377 s->flags |= SF_ERR_INTERNAL;
2378 goto return_error;
2379
Christopher Faulet93e02d82019-03-08 14:18:50 +01002380 return_bad_res:
Willy Tarreau4781b152021-04-06 13:53:36 +02002381 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002382 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02002383 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002384 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_RSP);
2385 }
Willy Tarreau826f3ab2021-02-10 12:07:15 +01002386 stream_inc_http_fail_ctr(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002387 if (!(s->flags & SF_ERR_MASK))
Christopher Faulet93e02d82019-03-08 14:18:50 +01002388 s->flags |= SF_ERR_SRVCL;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002389 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +02002390
Christopher Faulet93e02d82019-03-08 14:18:50 +01002391 return_error:
Christopher Faulete0768eb2018-10-03 16:38:02 +02002392 /* don't send any error message as we're in the body */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002393 http_reply_and_close(s, txn->status, NULL);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002394 if (!(s->flags & SF_FINST_MASK))
2395 s->flags |= SF_FINST_D;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002396 DBG_TRACE_DEVEL("leaving on error",
2397 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002398 return 0;
2399}
2400
Christopher Fauletf2824e62018-10-01 12:12:37 +02002401/* Perform an HTTP redirect based on the information in <rule>. The function
Christopher Faulet99daf282018-11-28 22:58:13 +01002402 * returns zero on success, or zero in case of a, irrecoverable error such
Christopher Fauletf2824e62018-10-01 12:12:37 +02002403 * as too large a request to build a valid response.
2404 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002405int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn)
Christopher Fauletf2824e62018-10-01 12:12:37 +02002406{
Christopher Faulet99daf282018-11-28 22:58:13 +01002407 struct channel *req = &s->req;
2408 struct channel *res = &s->res;
2409 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01002410 struct htx_sl *sl;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002411 struct buffer *chunk;
Christopher Faulet99daf282018-11-28 22:58:13 +01002412 struct ist status, reason, location;
2413 unsigned int flags;
Christopher Faulet08e66462019-05-23 16:44:59 +02002414 int close = 0; /* Try to keep the connection alive byt default */
Christopher Fauletf2824e62018-10-01 12:12:37 +02002415
2416 chunk = alloc_trash_chunk();
Christopher Fauletb8a53712019-12-16 11:29:38 +01002417 if (!chunk) {
2418 if (!(s->flags & SF_ERR_MASK))
2419 s->flags |= SF_ERR_RESOURCE;
Christopher Faulet99daf282018-11-28 22:58:13 +01002420 goto fail;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002421 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002422
Christopher Faulet99daf282018-11-28 22:58:13 +01002423 /*
2424 * Create the location
2425 */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002426 htx = htxbuf(&req->buf);
Christopher Fauletf2824e62018-10-01 12:12:37 +02002427 switch(rule->type) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002428 case REDIRECT_TYPE_SCHEME: {
2429 struct http_hdr_ctx ctx;
2430 struct ist path, host;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002431
Christopher Faulet99daf282018-11-28 22:58:13 +01002432 host = ist("");
2433 ctx.blk = NULL;
2434 if (http_find_header(htx, ist("Host"), &ctx, 0))
2435 host = ctx.value;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002436
Christopher Faulet297fbb42019-05-13 14:41:27 +02002437 sl = http_get_stline(htx);
Christopher Faulet99daf282018-11-28 22:58:13 +01002438 path = http_get_path(htx_sl_req_uri(sl));
2439 /* build message using path */
Tim Duesterhused526372020-03-05 17:56:33 +01002440 if (isttest(path)) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002441 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2442 int qs = 0;
2443 while (qs < path.len) {
2444 if (*(path.ptr + qs) == '?') {
2445 path.len = qs;
2446 break;
2447 }
2448 qs++;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002449 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002450 }
2451 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002452 else
2453 path = ist("/");
Christopher Fauletf2824e62018-10-01 12:12:37 +02002454
Christopher Faulet99daf282018-11-28 22:58:13 +01002455 if (rule->rdr_str) { /* this is an old "redirect" rule */
2456 /* add scheme */
2457 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2458 goto fail;
2459 }
2460 else {
2461 /* add scheme with executing log format */
2462 chunk->data += build_logline(s, chunk->area + chunk->data,
2463 chunk->size - chunk->data,
2464 &rule->rdr_fmt);
2465 }
2466 /* add "://" + host + path */
2467 if (!chunk_memcat(chunk, "://", 3) ||
2468 !chunk_memcat(chunk, host.ptr, host.len) ||
2469 !chunk_memcat(chunk, path.ptr, path.len))
2470 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002471
Christopher Faulet99daf282018-11-28 22:58:13 +01002472 /* append a slash at the end of the location if needed and missing */
2473 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
2474 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
2475 if (chunk->data + 1 >= chunk->size)
2476 goto fail;
2477 chunk->area[chunk->data++] = '/';
2478 }
2479 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002480 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002481
Christopher Faulet99daf282018-11-28 22:58:13 +01002482 case REDIRECT_TYPE_PREFIX: {
2483 struct ist path;
2484
Christopher Faulet297fbb42019-05-13 14:41:27 +02002485 sl = http_get_stline(htx);
Christopher Faulet99daf282018-11-28 22:58:13 +01002486 path = http_get_path(htx_sl_req_uri(sl));
2487 /* build message using path */
Tim Duesterhused526372020-03-05 17:56:33 +01002488 if (isttest(path)) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002489 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2490 int qs = 0;
2491 while (qs < path.len) {
2492 if (*(path.ptr + qs) == '?') {
2493 path.len = qs;
2494 break;
2495 }
2496 qs++;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002497 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002498 }
2499 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002500 else
2501 path = ist("/");
Christopher Fauletf2824e62018-10-01 12:12:37 +02002502
Christopher Faulet99daf282018-11-28 22:58:13 +01002503 if (rule->rdr_str) { /* this is an old "redirect" rule */
2504 /* add prefix. Note that if prefix == "/", we don't want to
2505 * add anything, otherwise it makes it hard for the user to
2506 * configure a self-redirection.
2507 */
2508 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
2509 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2510 goto fail;
2511 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002512 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002513 else {
2514 /* add prefix with executing log format */
2515 chunk->data += build_logline(s, chunk->area + chunk->data,
2516 chunk->size - chunk->data,
2517 &rule->rdr_fmt);
2518 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002519
Christopher Faulet99daf282018-11-28 22:58:13 +01002520 /* add path */
2521 if (!chunk_memcat(chunk, path.ptr, path.len))
2522 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002523
Christopher Faulet99daf282018-11-28 22:58:13 +01002524 /* append a slash at the end of the location if needed and missing */
2525 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
2526 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
2527 if (chunk->data + 1 >= chunk->size)
2528 goto fail;
2529 chunk->area[chunk->data++] = '/';
2530 }
2531 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002532 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002533 case REDIRECT_TYPE_LOCATION:
2534 default:
2535 if (rule->rdr_str) { /* this is an old "redirect" rule */
2536 /* add location */
2537 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2538 goto fail;
2539 }
2540 else {
2541 /* add location with executing log format */
2542 chunk->data += build_logline(s, chunk->area + chunk->data,
2543 chunk->size - chunk->data,
2544 &rule->rdr_fmt);
2545 }
2546 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002547 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002548 location = ist2(chunk->area, chunk->data);
2549
2550 /*
2551 * Create the 30x response
2552 */
2553 switch (rule->code) {
2554 case 308:
2555 status = ist("308");
2556 reason = ist("Permanent Redirect");
2557 break;
2558 case 307:
2559 status = ist("307");
2560 reason = ist("Temporary Redirect");
2561 break;
2562 case 303:
2563 status = ist("303");
2564 reason = ist("See Other");
2565 break;
2566 case 301:
2567 status = ist("301");
2568 reason = ist("Moved Permanently");
2569 break;
2570 case 302:
2571 default:
2572 status = ist("302");
2573 reason = ist("Found");
2574 break;
2575 }
2576
Christopher Faulet08e66462019-05-23 16:44:59 +02002577 if (!(txn->req.flags & HTTP_MSGF_BODYLESS) && txn->req.msg_state != HTTP_MSG_DONE)
2578 close = 1;
2579
Christopher Faulet99daf282018-11-28 22:58:13 +01002580 htx = htx_from_buf(&res->buf);
Kevin Zhu96b36392020-01-07 09:42:55 +01002581 /* Trim any possible response */
2582 channel_htx_truncate(&s->res, htx);
Christopher Faulet99daf282018-11-28 22:58:13 +01002583 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
2584 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), status, reason);
2585 if (!sl)
2586 goto fail;
2587 sl->info.res.status = rule->code;
2588 s->txn->status = rule->code;
2589
Christopher Faulet08e66462019-05-23 16:44:59 +02002590 if (close && !htx_add_header(htx, ist("Connection"), ist("close")))
2591 goto fail;
2592
2593 if (!htx_add_header(htx, ist("Content-length"), ist("0")) ||
Christopher Faulet99daf282018-11-28 22:58:13 +01002594 !htx_add_header(htx, ist("Location"), location))
2595 goto fail;
2596
2597 if (rule->code == 302 || rule->code == 303 || rule->code == 307) {
2598 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")))
2599 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002600 }
2601
2602 if (rule->cookie_len) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002603 if (!htx_add_header(htx, ist("Set-Cookie"), ist2(rule->cookie_str, rule->cookie_len)))
2604 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002605 }
2606
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002607 if (!htx_add_endof(htx, HTX_BLK_EOH))
Christopher Faulet99daf282018-11-28 22:58:13 +01002608 goto fail;
2609
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002610 htx->flags |= HTX_FL_EOM;
Kevin Zhu96b36392020-01-07 09:42:55 +01002611 htx_to_buf(htx, &res->buf);
Christopher Fauleta72a7e42020-01-28 09:28:11 +01002612 if (!http_forward_proxy_resp(s, 1))
2613 goto fail;
Christopher Faulet99daf282018-11-28 22:58:13 +01002614
Christopher Faulet60b33a52020-01-28 09:18:10 +01002615 if (rule->flags & REDIRECT_FLAG_FROM_REQ) {
2616 /* let's log the request time */
2617 s->logs.tv_request = now;
Christopher Faulet91322272021-10-04 14:16:46 +02002618 req->analysers &= AN_REQ_FLT_END;
Christopher Faulet99daf282018-11-28 22:58:13 +01002619
Christopher Faulet60b33a52020-01-28 09:18:10 +01002620 if (s->sess->fe == s->be) /* report it if the request was intercepted by the frontend */
Willy Tarreau4781b152021-04-06 13:53:36 +02002621 _HA_ATOMIC_INC(&s->sess->fe->fe_counters.intercepted_req);
Christopher Faulet60b33a52020-01-28 09:18:10 +01002622 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002623
2624 if (!(s->flags & SF_ERR_MASK))
2625 s->flags |= SF_ERR_LOCAL;
2626 if (!(s->flags & SF_FINST_MASK))
Christopher Faulet60b33a52020-01-28 09:18:10 +01002627 s->flags |= ((rule->flags & REDIRECT_FLAG_FROM_REQ) ? SF_FINST_R : SF_FINST_H);
Christopher Fauletf2824e62018-10-01 12:12:37 +02002628
Christopher Faulet99daf282018-11-28 22:58:13 +01002629 free_trash_chunk(chunk);
2630 return 1;
2631
2632 fail:
2633 /* If an error occurred, remove the incomplete HTTP response from the
2634 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01002635 channel_htx_truncate(res, htxbuf(&res->buf));
Christopher Fauletf2824e62018-10-01 12:12:37 +02002636 free_trash_chunk(chunk);
Christopher Faulet99daf282018-11-28 22:58:13 +01002637 return 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002638}
2639
Christopher Faulet79507152022-05-16 11:43:10 +02002640/* This function filters the request header names to only allow [0-9a-zA-Z-]
2641 * characters. Depending on the proxy configuration, headers with a name not
2642 * matching this charset are removed or the request is rejected with a
2643 * 403-Forbidden response if such name are found. It returns HTTP_RULE_RES_CONT
2644 * to continue the request processing or HTTP_RULE_RES_DENY if the request is
2645 * rejected.
2646 */
2647static enum rule_result http_req_restrict_header_names(struct stream *s, struct htx *htx, struct proxy *px)
2648{
2649 struct htx_blk *blk;
2650 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
2651
2652 blk = htx_get_first_blk(htx);
2653 while (blk) {
2654 enum htx_blk_type type = htx_get_blk_type(blk);
2655
2656 if (type == HTX_BLK_HDR) {
2657 struct ist n = htx_get_blk_name(htx, blk);
2658 int i;
2659
2660 for (i = 0; i < istlen(n); i++) {
2661 if (!isalnum((unsigned char)n.ptr[i]) && n.ptr[i] != '-') {
2662 /* Block the request or remove the header */
2663 if (px->options2 & PR_O2_RSTRICT_REQ_HDR_NAMES_BLK)
2664 goto block;
2665 blk = htx_remove_blk(htx, blk);
2666 continue;
2667 }
2668 }
2669 }
2670 if (type == HTX_BLK_EOH)
2671 break;
2672
2673 blk = htx_get_next_blk(htx, blk);
2674 }
2675 out:
2676 return rule_ret;
2677 block:
2678 /* Block the request returning a 403-Forbidden response */
2679 s->txn->status = 403;
2680 rule_ret = HTTP_RULE_RES_DENY;
2681 goto out;
2682}
2683
Christopher Faulet92d34fe2019-12-17 09:20:34 +01002684/* Replace all headers matching the name <name>. The header value is replaced if
2685 * it matches the regex <re>. <str> is used for the replacement. If <full> is
2686 * set to 1, the full-line is matched and replaced. Otherwise, comma-separated
2687 * values are evaluated one by one. It returns 0 on success and -1 on error.
2688 */
2689int http_replace_hdrs(struct stream* s, struct htx *htx, struct ist name,
2690 const char *str, struct my_regex *re, int full)
Christopher Faulet72333522018-10-24 11:25:02 +02002691{
2692 struct http_hdr_ctx ctx;
2693 struct buffer *output = get_trash_chunk();
2694
Christopher Faulet72333522018-10-24 11:25:02 +02002695 ctx.blk = NULL;
Christopher Faulet92d34fe2019-12-17 09:20:34 +01002696 while (http_find_header(htx, name, &ctx, full)) {
Christopher Faulet72333522018-10-24 11:25:02 +02002697 if (!regex_exec_match2(re, ctx.value.ptr, ctx.value.len, MAX_MATCH, pmatch, 0))
2698 continue;
2699
2700 output->data = exp_replace(output->area, output->size, ctx.value.ptr, str, pmatch);
2701 if (output->data == -1)
2702 return -1;
2703 if (!http_replace_header_value(htx, &ctx, ist2(output->area, output->data)))
2704 return -1;
2705 }
2706 return 0;
2707}
2708
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002709/* This function executes one of the set-{method,path,query,uri} actions. It
2710 * takes the string from the variable 'replace' with length 'len', then modifies
2711 * the relevant part of the request line accordingly. Then it updates various
2712 * pointers to the next elements which were moved, and the total buffer length.
2713 * It finds the action to be performed in p[2], previously filled by function
2714 * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal
2715 * error, though this can be revisited when this code is finally exploited.
2716 *
2717 * 'action' can be '0' to replace method, '1' to replace path, '2' to replace
Christopher Faulet312294f2020-09-02 17:17:44 +02002718 * query string, 3 to replace uri or 4 to replace the path+query.
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002719 *
2720 * In query string case, the mark question '?' must be set at the start of the
2721 * string by the caller, event if the replacement query string is empty.
2722 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002723int http_req_replace_stline(int action, const char *replace, int len,
2724 struct proxy *px, struct stream *s)
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002725{
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002726 struct htx *htx = htxbuf(&s->req.buf);
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002727
2728 switch (action) {
2729 case 0: // method
2730 if (!http_replace_req_meth(htx, ist2(replace, len)))
2731 return -1;
2732 break;
2733
2734 case 1: // path
Christopher Fauletb8ce5052020-08-31 16:11:57 +02002735 if (!http_replace_req_path(htx, ist2(replace, len), 0))
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002736 return -1;
2737 break;
2738
2739 case 2: // query
2740 if (!http_replace_req_query(htx, ist2(replace, len)))
2741 return -1;
2742 break;
2743
2744 case 3: // uri
2745 if (!http_replace_req_uri(htx, ist2(replace, len)))
2746 return -1;
2747 break;
2748
Christopher Faulet312294f2020-09-02 17:17:44 +02002749 case 4: // path + query
2750 if (!http_replace_req_path(htx, ist2(replace, len), 1))
2751 return -1;
2752 break;
2753
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002754 default:
2755 return -1;
2756 }
2757 return 0;
2758}
2759
2760/* This function replace the HTTP status code and the associated message. The
Christopher Faulete00d06c2019-12-16 17:18:42 +01002761 * variable <status> contains the new status code. This function never fails. It
2762 * returns 0 in case of success, -1 in case of internal error.
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002763 */
Christopher Faulet96bff762019-12-17 13:46:18 +01002764int http_res_set_status(unsigned int status, struct ist reason, struct stream *s)
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002765{
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002766 struct htx *htx = htxbuf(&s->res.buf);
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002767 char *res;
2768
2769 chunk_reset(&trash);
2770 res = ultoa_o(status, trash.area, trash.size);
2771 trash.data = res - trash.area;
2772
2773 /* Do we have a custom reason format string? */
Tim Duesterhuse296d3e2020-03-05 17:56:31 +01002774 if (!isttest(reason)) {
Christopher Faulet96bff762019-12-17 13:46:18 +01002775 const char *str = http_get_reason(status);
Tim Duesterhusdcf753a2021-03-04 17:31:47 +01002776 reason = ist(str);
Christopher Faulet96bff762019-12-17 13:46:18 +01002777 }
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002778
Christopher Fauletbde2c4c2020-08-31 16:43:34 +02002779 if (!http_replace_res_status(htx, ist2(trash.area, trash.data), reason))
Christopher Faulete00d06c2019-12-16 17:18:42 +01002780 return -1;
2781 return 0;
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002782}
2783
Christopher Faulet3e964192018-10-24 11:39:23 +02002784/* Executes the http-request rules <rules> for stream <s>, proxy <px> and
2785 * transaction <txn>. Returns the verdict of the first rule that prevents
2786 * further processing of the request (auth, deny, ...), and defaults to
2787 * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or
2788 * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT
2789 * on txn->flags if it encounters a tarpit rule. If <deny_status> is not NULL
2790 * and a deny/tarpit rule is matched, it will be filled with this rule's deny
2791 * status.
2792 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002793static enum rule_result http_req_get_intercept_rule(struct proxy *px, struct list *rules,
Christopher Fauletb58f62b2020-01-13 16:40:13 +01002794 struct stream *s)
Christopher Faulet3e964192018-10-24 11:39:23 +02002795{
2796 struct session *sess = strm_sess(s);
2797 struct http_txn *txn = s->txn;
Christopher Faulet3e964192018-10-24 11:39:23 +02002798 struct act_rule *rule;
Christopher Faulet3e964192018-10-24 11:39:23 +02002799 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002800 int act_opts = 0;
Christopher Faulet3e964192018-10-24 11:39:23 +02002801
Christopher Faulet3e964192018-10-24 11:39:23 +02002802 /* If "the current_rule_list" match the executed rule list, we are in
2803 * resume condition. If a resume is needed it is always in the action
2804 * and never in the ACL or converters. In this case, we initialise the
2805 * current rule, and go to the action execution point.
2806 */
2807 if (s->current_rule) {
2808 rule = s->current_rule;
2809 s->current_rule = NULL;
2810 if (s->current_rule_list == rules)
2811 goto resume_execution;
2812 }
2813 s->current_rule_list = rules;
2814
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002815 /* start the ruleset evaluation in strict mode */
2816 txn->req.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01002817
Christopher Faulet3e964192018-10-24 11:39:23 +02002818 list_for_each_entry(rule, rules, list) {
2819 /* check optional condition */
2820 if (rule->cond) {
2821 int ret;
2822
2823 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
2824 ret = acl_pass(ret);
2825
2826 if (rule->cond->pol == ACL_COND_UNLESS)
2827 ret = !ret;
2828
2829 if (!ret) /* condition not matched */
2830 continue;
2831 }
2832
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002833 act_opts |= ACT_OPT_FIRST;
Christopher Faulet3e964192018-10-24 11:39:23 +02002834 resume_execution:
Amaury Denoyelle03517732021-05-07 14:25:01 +02002835 if (rule->kw->flags & KWF_EXPERIMENTAL)
2836 mark_tainted(TAINTED_ACTION_EXP_EXECUTED);
2837
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002838 /* Always call the action function if defined */
2839 if (rule->action_ptr) {
2840 if ((s->req.flags & CF_READ_ERROR) ||
2841 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
2842 (px->options & PR_O_ABRT_CLOSE)))
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002843 act_opts |= ACT_OPT_FINAL;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002844
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002845 switch (rule->action_ptr(rule, px, sess, s, act_opts)) {
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002846 case ACT_RET_CONT:
2847 break;
2848 case ACT_RET_STOP:
2849 rule_ret = HTTP_RULE_RES_STOP;
2850 goto end;
2851 case ACT_RET_YIELD:
2852 s->current_rule = rule;
2853 rule_ret = HTTP_RULE_RES_YIELD;
2854 goto end;
2855 case ACT_RET_ERR:
2856 rule_ret = HTTP_RULE_RES_ERROR;
2857 goto end;
2858 case ACT_RET_DONE:
2859 rule_ret = HTTP_RULE_RES_DONE;
2860 goto end;
2861 case ACT_RET_DENY:
Christopher Fauletb58f62b2020-01-13 16:40:13 +01002862 if (txn->status == -1)
2863 txn->status = 403;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002864 rule_ret = HTTP_RULE_RES_DENY;
2865 goto end;
2866 case ACT_RET_ABRT:
2867 rule_ret = HTTP_RULE_RES_ABRT;
2868 goto end;
2869 case ACT_RET_INV:
2870 rule_ret = HTTP_RULE_RES_BADREQ;
2871 goto end;
2872 }
2873 continue; /* eval the next rule */
2874 }
2875
2876 /* If not action function defined, check for known actions */
Christopher Faulet3e964192018-10-24 11:39:23 +02002877 switch (rule->action) {
2878 case ACT_ACTION_ALLOW:
2879 rule_ret = HTTP_RULE_RES_STOP;
2880 goto end;
2881
2882 case ACT_ACTION_DENY:
Christopher Faulet5cb513a2020-05-13 17:56:56 +02002883 txn->status = rule->arg.http_reply->status;
2884 txn->http_reply = rule->arg.http_reply;
Christopher Faulet3e964192018-10-24 11:39:23 +02002885 rule_ret = HTTP_RULE_RES_DENY;
2886 goto end;
2887
2888 case ACT_HTTP_REQ_TARPIT:
2889 txn->flags |= TX_CLTARPIT;
Christopher Faulet5cb513a2020-05-13 17:56:56 +02002890 txn->status = rule->arg.http_reply->status;
2891 txn->http_reply = rule->arg.http_reply;
Christopher Faulet3e964192018-10-24 11:39:23 +02002892 rule_ret = HTTP_RULE_RES_DENY;
2893 goto end;
2894
Christopher Faulet3e964192018-10-24 11:39:23 +02002895 case ACT_HTTP_REDIR:
Christopher Faulet90d22a82020-03-06 11:18:39 +01002896 rule_ret = HTTP_RULE_RES_ABRT;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002897 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
Christopher Faulet3a26bee2019-12-16 12:47:40 +01002898 rule_ret = HTTP_RULE_RES_ERROR;
Christopher Faulet3e964192018-10-24 11:39:23 +02002899 goto end;
2900
2901 case ACT_HTTP_SET_NICE:
Christopher Faulet96bff762019-12-17 13:46:18 +01002902 s->task->nice = rule->arg.http.i;
Christopher Faulet3e964192018-10-24 11:39:23 +02002903 break;
2904
2905 case ACT_HTTP_SET_TOS:
Christopher Faulet96bff762019-12-17 13:46:18 +01002906 conn_set_tos(objt_conn(sess->origin), rule->arg.http.i);
Christopher Faulet3e964192018-10-24 11:39:23 +02002907 break;
2908
2909 case ACT_HTTP_SET_MARK:
Christopher Faulet96bff762019-12-17 13:46:18 +01002910 conn_set_mark(objt_conn(sess->origin), rule->arg.http.i);
Christopher Faulet3e964192018-10-24 11:39:23 +02002911 break;
2912
2913 case ACT_HTTP_SET_LOGL:
Christopher Faulet96bff762019-12-17 13:46:18 +01002914 s->logs.level = rule->arg.http.i;
Christopher Faulet3e964192018-10-24 11:39:23 +02002915 break;
2916
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002917 /* other flags exists, but normally, they never be matched. */
Christopher Faulet3e964192018-10-24 11:39:23 +02002918 default:
2919 break;
2920 }
2921 }
2922
2923 end:
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002924 /* if the ruleset evaluation is finished reset the strict mode */
Christopher Faulet46f95542019-12-20 10:07:22 +01002925 if (rule_ret != HTTP_RULE_RES_YIELD)
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002926 txn->req.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01002927
Christopher Faulet3e964192018-10-24 11:39:23 +02002928 /* we reached the end of the rules, nothing to report */
2929 return rule_ret;
2930}
2931
2932/* Executes the http-response rules <rules> for stream <s> and proxy <px>. It
2933 * returns one of 5 possible statuses: HTTP_RULE_RES_CONT, HTTP_RULE_RES_STOP,
2934 * HTTP_RULE_RES_DONE, HTTP_RULE_RES_YIELD, or HTTP_RULE_RES_BADREQ. If *CONT
2935 * is returned, the process can continue the evaluation of next rule list. If
2936 * *STOP or *DONE is returned, the process must stop the evaluation. If *BADREQ
2937 * is returned, it means the operation could not be processed and a server error
Christopher Fauleta53abad2020-05-13 08:12:22 +02002938 * must be returned. If *YIELD is returned, the caller must call again the
2939 * function with the same context.
Christopher Faulet3e964192018-10-24 11:39:23 +02002940 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002941static enum rule_result http_res_get_intercept_rule(struct proxy *px, struct list *rules,
2942 struct stream *s)
Christopher Faulet3e964192018-10-24 11:39:23 +02002943{
2944 struct session *sess = strm_sess(s);
2945 struct http_txn *txn = s->txn;
Christopher Faulet3e964192018-10-24 11:39:23 +02002946 struct act_rule *rule;
Christopher Faulet3e964192018-10-24 11:39:23 +02002947 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002948 int act_opts = 0;
Christopher Faulet3e964192018-10-24 11:39:23 +02002949
Christopher Faulet3e964192018-10-24 11:39:23 +02002950 /* If "the current_rule_list" match the executed rule list, we are in
2951 * resume condition. If a resume is needed it is always in the action
2952 * and never in the ACL or converters. In this case, we initialise the
2953 * current rule, and go to the action execution point.
2954 */
2955 if (s->current_rule) {
2956 rule = s->current_rule;
2957 s->current_rule = NULL;
2958 if (s->current_rule_list == rules)
2959 goto resume_execution;
2960 }
2961 s->current_rule_list = rules;
2962
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002963 /* start the ruleset evaluation in strict mode */
2964 txn->rsp.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01002965
Christopher Faulet3e964192018-10-24 11:39:23 +02002966 list_for_each_entry(rule, rules, list) {
2967 /* check optional condition */
2968 if (rule->cond) {
2969 int ret;
2970
2971 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
2972 ret = acl_pass(ret);
2973
2974 if (rule->cond->pol == ACL_COND_UNLESS)
2975 ret = !ret;
2976
2977 if (!ret) /* condition not matched */
2978 continue;
2979 }
2980
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002981 act_opts |= ACT_OPT_FIRST;
Christopher Faulet3e964192018-10-24 11:39:23 +02002982resume_execution:
Amaury Denoyelle03517732021-05-07 14:25:01 +02002983 if (rule->kw->flags & KWF_EXPERIMENTAL)
2984 mark_tainted(TAINTED_ACTION_EXP_EXECUTED);
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002985
2986 /* Always call the action function if defined */
2987 if (rule->action_ptr) {
2988 if ((s->req.flags & CF_READ_ERROR) ||
2989 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
2990 (px->options & PR_O_ABRT_CLOSE)))
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002991 act_opts |= ACT_OPT_FINAL;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002992
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002993 switch (rule->action_ptr(rule, px, sess, s, act_opts)) {
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002994 case ACT_RET_CONT:
2995 break;
2996 case ACT_RET_STOP:
2997 rule_ret = HTTP_RULE_RES_STOP;
2998 goto end;
2999 case ACT_RET_YIELD:
3000 s->current_rule = rule;
3001 rule_ret = HTTP_RULE_RES_YIELD;
3002 goto end;
3003 case ACT_RET_ERR:
3004 rule_ret = HTTP_RULE_RES_ERROR;
3005 goto end;
3006 case ACT_RET_DONE:
3007 rule_ret = HTTP_RULE_RES_DONE;
3008 goto end;
3009 case ACT_RET_DENY:
Christopher Fauletb58f62b2020-01-13 16:40:13 +01003010 if (txn->status == -1)
3011 txn->status = 502;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01003012 rule_ret = HTTP_RULE_RES_DENY;
3013 goto end;
3014 case ACT_RET_ABRT:
3015 rule_ret = HTTP_RULE_RES_ABRT;
3016 goto end;
3017 case ACT_RET_INV:
3018 rule_ret = HTTP_RULE_RES_BADREQ;
3019 goto end;
3020 }
3021 continue; /* eval the next rule */
3022 }
3023
3024 /* If not action function defined, check for known actions */
Christopher Faulet3e964192018-10-24 11:39:23 +02003025 switch (rule->action) {
3026 case ACT_ACTION_ALLOW:
3027 rule_ret = HTTP_RULE_RES_STOP; /* "allow" rules are OK */
3028 goto end;
3029
3030 case ACT_ACTION_DENY:
Christopher Faulet5cb513a2020-05-13 17:56:56 +02003031 txn->status = rule->arg.http_reply->status;
3032 txn->http_reply = rule->arg.http_reply;
Christopher Faulet3a26bee2019-12-16 12:47:40 +01003033 rule_ret = HTTP_RULE_RES_DENY;
Christopher Faulet3e964192018-10-24 11:39:23 +02003034 goto end;
3035
3036 case ACT_HTTP_SET_NICE:
Christopher Faulet96bff762019-12-17 13:46:18 +01003037 s->task->nice = rule->arg.http.i;
Christopher Faulet3e964192018-10-24 11:39:23 +02003038 break;
3039
3040 case ACT_HTTP_SET_TOS:
Christopher Faulet96bff762019-12-17 13:46:18 +01003041 conn_set_tos(objt_conn(sess->origin), rule->arg.http.i);
Christopher Faulet3e964192018-10-24 11:39:23 +02003042 break;
3043
3044 case ACT_HTTP_SET_MARK:
Christopher Faulet96bff762019-12-17 13:46:18 +01003045 conn_set_mark(objt_conn(sess->origin), rule->arg.http.i);
Christopher Faulet3e964192018-10-24 11:39:23 +02003046 break;
3047
3048 case ACT_HTTP_SET_LOGL:
Christopher Faulet96bff762019-12-17 13:46:18 +01003049 s->logs.level = rule->arg.http.i;
Christopher Faulet3e964192018-10-24 11:39:23 +02003050 break;
3051
Christopher Faulet3e964192018-10-24 11:39:23 +02003052 case ACT_HTTP_REDIR:
Christopher Faulet49c2a702020-03-06 15:44:37 +01003053 rule_ret = HTTP_RULE_RES_ABRT;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003054 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
Christopher Faulet3a26bee2019-12-16 12:47:40 +01003055 rule_ret = HTTP_RULE_RES_ERROR;
Christopher Faulet3e964192018-10-24 11:39:23 +02003056 goto end;
3057
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01003058 /* other flags exists, but normally, they never be matched. */
Christopher Faulet3e964192018-10-24 11:39:23 +02003059 default:
3060 break;
3061 }
3062 }
3063
3064 end:
Christopher Faulet1aea50e2020-01-17 16:03:53 +01003065 /* if the ruleset evaluation is finished reset the strict mode */
Christopher Faulet46f95542019-12-20 10:07:22 +01003066 if (rule_ret != HTTP_RULE_RES_YIELD)
Christopher Faulet1aea50e2020-01-17 16:03:53 +01003067 txn->rsp.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01003068
Christopher Faulet3e964192018-10-24 11:39:23 +02003069 /* we reached the end of the rules, nothing to report */
3070 return rule_ret;
3071}
3072
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01003073/* Executes backend and frontend http-after-response rules for the stream <s>,
3074 * in that order. it return 1 on success and 0 on error. It is the caller
3075 * responsibility to catch error or ignore it. If it catches it, this function
3076 * may be called a second time, for the internal error.
3077 */
3078int http_eval_after_res_rules(struct stream *s)
3079{
3080 struct session *sess = s->sess;
3081 enum rule_result ret = HTTP_RULE_RES_CONT;
3082
Christopher Faulet507479b2020-05-15 12:29:46 +02003083 /* Eval after-response ruleset only if the reply is not const */
3084 if (s->txn->flags & TX_CONST_REPLY)
3085 goto end;
3086
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01003087 /* prune the request variables if not already done and swap to the response variables. */
3088 if (s->vars_reqres.scope != SCOPE_RES) {
3089 if (!LIST_ISEMPTY(&s->vars_reqres.head))
3090 vars_prune(&s->vars_reqres, s->sess, s);
3091 vars_init(&s->vars_reqres, SCOPE_RES);
3092 }
3093
3094 ret = http_res_get_intercept_rule(s->be, &s->be->http_after_res_rules, s);
Christopher Fauletf5db0fc2021-11-09 17:48:39 +01003095 if ((ret == HTTP_RULE_RES_CONT || ret == HTTP_RULE_RES_STOP) && sess->fe != s->be)
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01003096 ret = http_res_get_intercept_rule(sess->fe, &sess->fe->http_after_res_rules, s);
3097
Christopher Faulet507479b2020-05-15 12:29:46 +02003098 end:
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01003099 /* All other codes than CONTINUE, STOP or DONE are forbidden */
3100 return (ret == HTTP_RULE_RES_CONT || ret == HTTP_RULE_RES_STOP || ret == HTTP_RULE_RES_DONE);
3101}
3102
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003103/*
3104 * Manage client-side cookie. It can impact performance by about 2% so it is
3105 * desirable to call it only when needed. This code is quite complex because
3106 * of the multiple very crappy and ambiguous syntaxes we have to support. it
3107 * highly recommended not to touch this part without a good reason !
3108 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003109static void http_manage_client_side_cookies(struct stream *s, struct channel *req)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003110{
3111 struct session *sess = s->sess;
3112 struct http_txn *txn = s->txn;
3113 struct htx *htx;
3114 struct http_hdr_ctx ctx;
3115 char *hdr_beg, *hdr_end, *del_from;
3116 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
3117 int preserve_hdr;
3118
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003119 htx = htxbuf(&req->buf);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003120 ctx.blk = NULL;
3121 while (http_find_header(htx, ist("Cookie"), &ctx, 1)) {
Olivier Houchardf0f42382019-07-22 17:43:46 +02003122 int is_first = 1;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003123 del_from = NULL; /* nothing to be deleted */
3124 preserve_hdr = 0; /* assume we may kill the whole header */
3125
3126 /* Now look for cookies. Conforming to RFC2109, we have to support
3127 * attributes whose name begin with a '$', and associate them with
3128 * the right cookie, if we want to delete this cookie.
3129 * So there are 3 cases for each cookie read :
3130 * 1) it's a special attribute, beginning with a '$' : ignore it.
3131 * 2) it's a server id cookie that we *MAY* want to delete : save
3132 * some pointers on it (last semi-colon, beginning of cookie...)
3133 * 3) it's an application cookie : we *MAY* have to delete a previous
3134 * "special" cookie.
3135 * At the end of loop, if a "special" cookie remains, we may have to
3136 * remove it. If no application cookie persists in the header, we
3137 * *MUST* delete it.
3138 *
3139 * Note: RFC2965 is unclear about the processing of spaces around
3140 * the equal sign in the ATTR=VALUE form. A careful inspection of
3141 * the RFC explicitly allows spaces before it, and not within the
3142 * tokens (attrs or values). An inspection of RFC2109 allows that
3143 * too but section 10.1.3 lets one think that spaces may be allowed
3144 * after the equal sign too, resulting in some (rare) buggy
3145 * implementations trying to do that. So let's do what servers do.
3146 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
3147 * allowed quoted strings in values, with any possible character
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003148 * after a backslash, including control chars and delimiters, which
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003149 * causes parsing to become ambiguous. Browsers also allow spaces
3150 * within values even without quotes.
3151 *
3152 * We have to keep multiple pointers in order to support cookie
3153 * removal at the beginning, middle or end of header without
3154 * corrupting the header. All of these headers are valid :
3155 *
3156 * hdr_beg hdr_end
3157 * | |
3158 * v |
3159 * NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3 |
3160 * NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3 v
3161 * NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3
3162 * | | | | | | |
3163 * | | | | | | |
3164 * | | | | | | +--> next
3165 * | | | | | +----> val_end
3166 * | | | | +-----------> val_beg
3167 * | | | +--------------> equal
3168 * | | +----------------> att_end
3169 * | +---------------------> att_beg
3170 * +--------------------------> prev
3171 *
3172 */
3173 hdr_beg = ctx.value.ptr;
3174 hdr_end = hdr_beg + ctx.value.len;
3175 for (prev = hdr_beg; prev < hdr_end; prev = next) {
3176 /* Iterate through all cookies on this line */
3177
3178 /* find att_beg */
3179 att_beg = prev;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003180 if (!is_first)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003181 att_beg++;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003182 is_first = 0;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003183
3184 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
3185 att_beg++;
3186
3187 /* find att_end : this is the first character after the last non
3188 * space before the equal. It may be equal to hdr_end.
3189 */
3190 equal = att_end = att_beg;
3191 while (equal < hdr_end) {
3192 if (*equal == '=' || *equal == ',' || *equal == ';')
3193 break;
3194 if (HTTP_IS_SPHT(*equal++))
3195 continue;
3196 att_end = equal;
3197 }
3198
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003199 /* here, <equal> points to '=', a delimiter or the end. <att_end>
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003200 * is between <att_beg> and <equal>, both may be identical.
3201 */
3202 /* look for end of cookie if there is an equal sign */
3203 if (equal < hdr_end && *equal == '=') {
3204 /* look for the beginning of the value */
3205 val_beg = equal + 1;
3206 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
3207 val_beg++;
3208
3209 /* find the end of the value, respecting quotes */
3210 next = http_find_cookie_value_end(val_beg, hdr_end);
3211
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003212 /* make val_end point to the first white space or delimiter after the value */
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003213 val_end = next;
3214 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
3215 val_end--;
3216 }
3217 else
3218 val_beg = val_end = next = equal;
3219
3220 /* We have nothing to do with attributes beginning with
3221 * '$'. However, they will automatically be removed if a
3222 * header before them is removed, since they're supposed
3223 * to be linked together.
3224 */
3225 if (*att_beg == '$')
3226 continue;
3227
3228 /* Ignore cookies with no equal sign */
3229 if (equal == next) {
3230 /* This is not our cookie, so we must preserve it. But if we already
3231 * scheduled another cookie for removal, we cannot remove the
3232 * complete header, but we can remove the previous block itself.
3233 */
3234 preserve_hdr = 1;
3235 if (del_from != NULL) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003236 int delta = http_del_hdr_value(hdr_beg, hdr_end, &del_from, prev);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003237 val_end += delta;
3238 next += delta;
3239 hdr_end += delta;
3240 prev = del_from;
3241 del_from = NULL;
3242 }
3243 continue;
3244 }
3245
3246 /* if there are spaces around the equal sign, we need to
3247 * strip them otherwise we'll get trouble for cookie captures,
3248 * or even for rewrites. Since this happens extremely rarely,
3249 * it does not hurt performance.
3250 */
3251 if (unlikely(att_end != equal || val_beg > equal + 1)) {
3252 int stripped_before = 0;
3253 int stripped_after = 0;
3254
3255 if (att_end != equal) {
3256 memmove(att_end, equal, hdr_end - equal);
3257 stripped_before = (att_end - equal);
3258 equal += stripped_before;
3259 val_beg += stripped_before;
3260 }
3261
3262 if (val_beg > equal + 1) {
3263 memmove(equal + 1, val_beg, hdr_end + stripped_before - val_beg);
3264 stripped_after = (equal + 1) - val_beg;
3265 val_beg += stripped_after;
3266 stripped_before += stripped_after;
3267 }
3268
3269 val_end += stripped_before;
3270 next += stripped_before;
3271 hdr_end += stripped_before;
3272 }
3273 /* now everything is as on the diagram above */
3274
3275 /* First, let's see if we want to capture this cookie. We check
3276 * that we don't already have a client side cookie, because we
3277 * can only capture one. Also as an optimisation, we ignore
3278 * cookies shorter than the declared name.
3279 */
3280 if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL &&
3281 (val_end - att_beg >= sess->fe->capture_namelen) &&
3282 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
3283 int log_len = val_end - att_beg;
3284
3285 if ((txn->cli_cookie = pool_alloc(pool_head_capture)) == NULL) {
3286 ha_alert("HTTP logging : out of memory.\n");
3287 } else {
3288 if (log_len > sess->fe->capture_len)
3289 log_len = sess->fe->capture_len;
3290 memcpy(txn->cli_cookie, att_beg, log_len);
3291 txn->cli_cookie[log_len] = 0;
3292 }
3293 }
3294
3295 /* Persistence cookies in passive, rewrite or insert mode have the
3296 * following form :
3297 *
3298 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
3299 *
3300 * For cookies in prefix mode, the form is :
3301 *
3302 * Cookie: NAME=SRV~VALUE
3303 */
3304 if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
3305 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
3306 struct server *srv = s->be->srv;
3307 char *delim;
3308
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003309 /* if we're in cookie prefix mode, we'll search the delimiter so that we
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003310 * have the server ID between val_beg and delim, and the original cookie between
3311 * delim+1 and val_end. Otherwise, delim==val_end :
3312 *
3313 * hdr_beg
3314 * |
3315 * v
3316 * NAME=SRV; # in all but prefix modes
3317 * NAME=SRV~OPAQUE ; # in prefix mode
3318 * || || | |+-> next
3319 * || || | +--> val_end
3320 * || || +---------> delim
3321 * || |+------------> val_beg
3322 * || +-------------> att_end = equal
3323 * |+-----------------> att_beg
3324 * +------------------> prev
3325 *
3326 */
3327 if (s->be->ck_opts & PR_CK_PFX) {
3328 for (delim = val_beg; delim < val_end; delim++)
3329 if (*delim == COOKIE_DELIM)
3330 break;
3331 }
3332 else {
3333 char *vbar1;
3334 delim = val_end;
3335 /* Now check if the cookie contains a date field, which would
3336 * appear after a vertical bar ('|') just after the server name
3337 * and before the delimiter.
3338 */
3339 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
3340 if (vbar1) {
3341 /* OK, so left of the bar is the server's cookie and
3342 * right is the last seen date. It is a base64 encoded
3343 * 30-bit value representing the UNIX date since the
3344 * epoch in 4-second quantities.
3345 */
3346 int val;
3347 delim = vbar1++;
3348 if (val_end - vbar1 >= 5) {
3349 val = b64tos30(vbar1);
3350 if (val > 0)
3351 txn->cookie_last_date = val << 2;
3352 }
3353 /* look for a second vertical bar */
3354 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
3355 if (vbar1 && (val_end - vbar1 > 5)) {
3356 val = b64tos30(vbar1 + 1);
3357 if (val > 0)
3358 txn->cookie_first_date = val << 2;
3359 }
3360 }
3361 }
3362
3363 /* if the cookie has an expiration date and the proxy wants to check
3364 * it, then we do that now. We first check if the cookie is too old,
3365 * then only if it has expired. We detect strict overflow because the
3366 * time resolution here is not great (4 seconds). Cookies with dates
3367 * in the future are ignored if their offset is beyond one day. This
3368 * allows an admin to fix timezone issues without expiring everyone
3369 * and at the same time avoids keeping unwanted side effects for too
3370 * long.
3371 */
3372 if (txn->cookie_first_date && s->be->cookie_maxlife &&
3373 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) ||
3374 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
3375 txn->flags &= ~TX_CK_MASK;
3376 txn->flags |= TX_CK_OLD;
3377 delim = val_beg; // let's pretend we have not found the cookie
3378 txn->cookie_first_date = 0;
3379 txn->cookie_last_date = 0;
3380 }
3381 else if (txn->cookie_last_date && s->be->cookie_maxidle &&
3382 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) ||
3383 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
3384 txn->flags &= ~TX_CK_MASK;
3385 txn->flags |= TX_CK_EXPIRED;
3386 delim = val_beg; // let's pretend we have not found the cookie
3387 txn->cookie_first_date = 0;
3388 txn->cookie_last_date = 0;
3389 }
3390
3391 /* Here, we'll look for the first running server which supports the cookie.
3392 * This allows to share a same cookie between several servers, for example
3393 * to dedicate backup servers to specific servers only.
3394 * However, to prevent clients from sticking to cookie-less backup server
3395 * when they have incidentely learned an empty cookie, we simply ignore
3396 * empty cookies and mark them as invalid.
3397 * The same behaviour is applied when persistence must be ignored.
3398 */
3399 if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
3400 srv = NULL;
3401
3402 while (srv) {
3403 if (srv->cookie && (srv->cklen == delim - val_beg) &&
3404 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
3405 if ((srv->cur_state != SRV_ST_STOPPED) ||
3406 (s->be->options & PR_O_PERSIST) ||
3407 (s->flags & SF_FORCE_PRST)) {
3408 /* we found the server and we can use it */
3409 txn->flags &= ~TX_CK_MASK;
3410 txn->flags |= (srv->cur_state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
3411 s->flags |= SF_DIRECT | SF_ASSIGNED;
3412 s->target = &srv->obj_type;
3413 break;
3414 } else {
3415 /* we found a server, but it's down,
3416 * mark it as such and go on in case
3417 * another one is available.
3418 */
3419 txn->flags &= ~TX_CK_MASK;
3420 txn->flags |= TX_CK_DOWN;
3421 }
3422 }
3423 srv = srv->next;
3424 }
3425
3426 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
3427 /* no server matched this cookie or we deliberately skipped it */
3428 txn->flags &= ~TX_CK_MASK;
3429 if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
3430 txn->flags |= TX_CK_UNUSED;
3431 else
3432 txn->flags |= TX_CK_INVALID;
3433 }
3434
3435 /* depending on the cookie mode, we may have to either :
3436 * - delete the complete cookie if we're in insert+indirect mode, so that
3437 * the server never sees it ;
3438 * - remove the server id from the cookie value, and tag the cookie as an
Joseph Herlante9d5c722018-11-25 11:00:25 -08003439 * application cookie so that it does not get accidentally removed later,
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003440 * if we're in cookie prefix mode
3441 */
3442 if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
3443 int delta; /* negative */
3444
3445 memmove(val_beg, delim + 1, hdr_end - (delim + 1));
3446 delta = val_beg - (delim + 1);
3447 val_end += delta;
3448 next += delta;
3449 hdr_end += delta;
3450 del_from = NULL;
3451 preserve_hdr = 1; /* we want to keep this cookie */
3452 }
3453 else if (del_from == NULL &&
3454 (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
3455 del_from = prev;
3456 }
3457 }
3458 else {
3459 /* This is not our cookie, so we must preserve it. But if we already
3460 * scheduled another cookie for removal, we cannot remove the
3461 * complete header, but we can remove the previous block itself.
3462 */
3463 preserve_hdr = 1;
3464
3465 if (del_from != NULL) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003466 int delta = http_del_hdr_value(hdr_beg, hdr_end, &del_from, prev);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003467 if (att_beg >= del_from)
3468 att_beg += delta;
3469 if (att_end >= del_from)
3470 att_end += delta;
3471 val_beg += delta;
3472 val_end += delta;
3473 next += delta;
3474 hdr_end += delta;
3475 prev = del_from;
3476 del_from = NULL;
3477 }
3478 }
3479
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003480 } /* for each cookie */
3481
3482
3483 /* There are no more cookies on this line.
3484 * We may still have one (or several) marked for deletion at the
3485 * end of the line. We must do this now in two ways :
3486 * - if some cookies must be preserved, we only delete from the
3487 * mark to the end of line ;
3488 * - if nothing needs to be preserved, simply delete the whole header
3489 */
3490 if (del_from) {
3491 hdr_end = (preserve_hdr ? del_from : hdr_beg);
3492 }
3493 if ((hdr_end - hdr_beg) != ctx.value.len) {
Christopher Faulet3e2638e2019-06-18 09:49:16 +02003494 if (hdr_beg != hdr_end)
3495 htx_change_blk_value_len(htx, ctx.blk, hdr_end - hdr_beg);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003496 else
3497 http_remove_header(htx, &ctx);
3498 }
3499 } /* for each "Cookie header */
3500}
3501
3502/*
3503 * Manage server-side cookies. It can impact performance by about 2% so it is
3504 * desirable to call it only when needed. This function is also used when we
3505 * just need to know if there is a cookie (eg: for check-cache).
3506 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003507static void http_manage_server_side_cookies(struct stream *s, struct channel *res)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003508{
3509 struct session *sess = s->sess;
3510 struct http_txn *txn = s->txn;
3511 struct htx *htx;
3512 struct http_hdr_ctx ctx;
3513 struct server *srv;
3514 char *hdr_beg, *hdr_end;
3515 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau6f7a02a2019-04-15 21:49:49 +02003516 int is_cookie2 = 0;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003517
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003518 htx = htxbuf(&res->buf);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003519
3520 ctx.blk = NULL;
3521 while (1) {
Olivier Houchardf0f42382019-07-22 17:43:46 +02003522 int is_first = 1;
3523
Andrew McDermott86032c32022-02-11 18:26:49 +00003524 if (is_cookie2 || !http_find_header(htx, ist("Set-Cookie"), &ctx, 1)) {
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003525 if (!http_find_header(htx, ist("Set-Cookie2"), &ctx, 1))
3526 break;
3527 is_cookie2 = 1;
3528 }
3529
3530 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
3531 * <prev> points to the colon.
3532 */
3533 txn->flags |= TX_SCK_PRESENT;
3534
3535 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
3536 * check-cache is enabled) and we are not interested in checking
3537 * them. Warning, the cookie capture is declared in the frontend.
3538 */
3539 if (s->be->cookie_name == NULL && sess->fe->capture_name == NULL)
3540 break;
3541
3542 /* OK so now we know we have to process this response cookie.
3543 * The format of the Set-Cookie header is slightly different
3544 * from the format of the Cookie header in that it does not
3545 * support the comma as a cookie delimiter (thus the header
3546 * cannot be folded) because the Expires attribute described in
3547 * the original Netscape's spec may contain an unquoted date
3548 * with a comma inside. We have to live with this because
3549 * many browsers don't support Max-Age and some browsers don't
3550 * support quoted strings. However the Set-Cookie2 header is
3551 * clean.
3552 *
3553 * We have to keep multiple pointers in order to support cookie
3554 * removal at the beginning, middle or end of header without
3555 * corrupting the header (in case of set-cookie2). A special
3556 * pointer, <scav> points to the beginning of the set-cookie-av
3557 * fields after the first semi-colon. The <next> pointer points
3558 * either to the end of line (set-cookie) or next unquoted comma
3559 * (set-cookie2). All of these headers are valid :
3560 *
3561 * hdr_beg hdr_end
3562 * | |
3563 * v |
3564 * NAME1 = VALUE 1 ; Secure; Path="/" |
3565 * NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT v
3566 * NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT
3567 * NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard
3568 * | | | | | | | |
3569 * | | | | | | | +-> next
3570 * | | | | | | +------------> scav
3571 * | | | | | +--------------> val_end
3572 * | | | | +--------------------> val_beg
3573 * | | | +----------------------> equal
3574 * | | +------------------------> att_end
3575 * | +----------------------------> att_beg
3576 * +------------------------------> prev
3577 * -------------------------------> hdr_beg
3578 */
3579 hdr_beg = ctx.value.ptr;
3580 hdr_end = hdr_beg + ctx.value.len;
3581 for (prev = hdr_beg; prev < hdr_end; prev = next) {
3582
3583 /* Iterate through all cookies on this line */
3584
3585 /* find att_beg */
3586 att_beg = prev;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003587 if (!is_first)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003588 att_beg++;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003589 is_first = 0;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003590
3591 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
3592 att_beg++;
3593
3594 /* find att_end : this is the first character after the last non
3595 * space before the equal. It may be equal to hdr_end.
3596 */
3597 equal = att_end = att_beg;
3598
3599 while (equal < hdr_end) {
3600 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
3601 break;
3602 if (HTTP_IS_SPHT(*equal++))
3603 continue;
3604 att_end = equal;
3605 }
3606
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003607 /* here, <equal> points to '=', a delimiter or the end. <att_end>
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003608 * is between <att_beg> and <equal>, both may be identical.
3609 */
3610
3611 /* look for end of cookie if there is an equal sign */
3612 if (equal < hdr_end && *equal == '=') {
3613 /* look for the beginning of the value */
3614 val_beg = equal + 1;
3615 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
3616 val_beg++;
3617
3618 /* find the end of the value, respecting quotes */
3619 next = http_find_cookie_value_end(val_beg, hdr_end);
3620
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003621 /* make val_end point to the first white space or delimiter after the value */
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003622 val_end = next;
3623 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
3624 val_end--;
3625 }
3626 else {
3627 /* <equal> points to next comma, semi-colon or EOL */
3628 val_beg = val_end = next = equal;
3629 }
3630
3631 if (next < hdr_end) {
3632 /* Set-Cookie2 supports multiple cookies, and <next> points to
3633 * a colon or semi-colon before the end. So skip all attr-value
3634 * pairs and look for the next comma. For Set-Cookie, since
3635 * commas are permitted in values, skip to the end.
3636 */
3637 if (is_cookie2)
3638 next = http_find_hdr_value_end(next, hdr_end);
3639 else
3640 next = hdr_end;
3641 }
3642
3643 /* Now everything is as on the diagram above */
3644
3645 /* Ignore cookies with no equal sign */
3646 if (equal == val_end)
3647 continue;
3648
3649 /* If there are spaces around the equal sign, we need to
3650 * strip them otherwise we'll get trouble for cookie captures,
3651 * or even for rewrites. Since this happens extremely rarely,
3652 * it does not hurt performance.
3653 */
3654 if (unlikely(att_end != equal || val_beg > equal + 1)) {
3655 int stripped_before = 0;
3656 int stripped_after = 0;
3657
3658 if (att_end != equal) {
3659 memmove(att_end, equal, hdr_end - equal);
3660 stripped_before = (att_end - equal);
3661 equal += stripped_before;
3662 val_beg += stripped_before;
3663 }
3664
3665 if (val_beg > equal + 1) {
3666 memmove(equal + 1, val_beg, hdr_end + stripped_before - val_beg);
3667 stripped_after = (equal + 1) - val_beg;
3668 val_beg += stripped_after;
3669 stripped_before += stripped_after;
3670 }
3671
3672 val_end += stripped_before;
3673 next += stripped_before;
3674 hdr_end += stripped_before;
3675
Christopher Faulet3e2638e2019-06-18 09:49:16 +02003676 htx_change_blk_value_len(htx, ctx.blk, hdr_end - hdr_beg);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003677 ctx.value.len = hdr_end - hdr_beg;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003678 }
3679
3680 /* First, let's see if we want to capture this cookie. We check
3681 * that we don't already have a server side cookie, because we
3682 * can only capture one. Also as an optimisation, we ignore
3683 * cookies shorter than the declared name.
3684 */
3685 if (sess->fe->capture_name != NULL &&
3686 txn->srv_cookie == NULL &&
3687 (val_end - att_beg >= sess->fe->capture_namelen) &&
3688 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
3689 int log_len = val_end - att_beg;
3690 if ((txn->srv_cookie = pool_alloc(pool_head_capture)) == NULL) {
3691 ha_alert("HTTP logging : out of memory.\n");
3692 }
3693 else {
3694 if (log_len > sess->fe->capture_len)
3695 log_len = sess->fe->capture_len;
3696 memcpy(txn->srv_cookie, att_beg, log_len);
3697 txn->srv_cookie[log_len] = 0;
3698 }
3699 }
3700
3701 srv = objt_server(s->target);
3702 /* now check if we need to process it for persistence */
3703 if (!(s->flags & SF_IGNORE_PRST) &&
3704 (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
3705 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
3706 /* assume passive cookie by default */
3707 txn->flags &= ~TX_SCK_MASK;
3708 txn->flags |= TX_SCK_FOUND;
3709
3710 /* If the cookie is in insert mode on a known server, we'll delete
3711 * this occurrence because we'll insert another one later.
3712 * We'll delete it too if the "indirect" option is set and we're in
3713 * a direct access.
3714 */
3715 if (s->be->ck_opts & PR_CK_PSV) {
3716 /* The "preserve" flag was set, we don't want to touch the
3717 * server's cookie.
3718 */
3719 }
3720 else if ((srv && (s->be->ck_opts & PR_CK_INS)) ||
3721 ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) {
3722 /* this cookie must be deleted */
3723 if (prev == hdr_beg && next == hdr_end) {
3724 /* whole header */
3725 http_remove_header(htx, &ctx);
3726 /* note: while both invalid now, <next> and <hdr_end>
3727 * are still equal, so the for() will stop as expected.
3728 */
3729 } else {
3730 /* just remove the value */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003731 int delta = http_del_hdr_value(hdr_beg, hdr_end, &prev, next);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003732 next = prev;
3733 hdr_end += delta;
3734 }
3735 txn->flags &= ~TX_SCK_MASK;
3736 txn->flags |= TX_SCK_DELETED;
3737 /* and go on with next cookie */
3738 }
3739 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) {
3740 /* replace bytes val_beg->val_end with the cookie name associated
3741 * with this server since we know it.
3742 */
3743 int sliding, delta;
3744
3745 ctx.value = ist2(val_beg, val_end - val_beg);
3746 ctx.lws_before = ctx.lws_after = 0;
3747 http_replace_header_value(htx, &ctx, ist2(srv->cookie, srv->cklen));
3748 delta = srv->cklen - (val_end - val_beg);
3749 sliding = (ctx.value.ptr - val_beg);
3750 hdr_beg += sliding;
3751 val_beg += sliding;
3752 next += sliding + delta;
3753 hdr_end += sliding + delta;
3754
3755 txn->flags &= ~TX_SCK_MASK;
3756 txn->flags |= TX_SCK_REPLACED;
3757 }
3758 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) {
3759 /* insert the cookie name associated with this server
3760 * before existing cookie, and insert a delimiter between them..
3761 */
3762 int sliding, delta;
3763 ctx.value = ist2(val_beg, 0);
3764 ctx.lws_before = ctx.lws_after = 0;
3765 http_replace_header_value(htx, &ctx, ist2(srv->cookie, srv->cklen + 1));
3766 delta = srv->cklen + 1;
3767 sliding = (ctx.value.ptr - val_beg);
3768 hdr_beg += sliding;
3769 val_beg += sliding;
3770 next += sliding + delta;
3771 hdr_end += sliding + delta;
3772
3773 val_beg[srv->cklen] = COOKIE_DELIM;
3774 txn->flags &= ~TX_SCK_MASK;
3775 txn->flags |= TX_SCK_REPLACED;
3776 }
3777 }
3778 /* that's done for this cookie, check the next one on the same
3779 * line when next != hdr_end (only if is_cookie2).
3780 */
3781 }
3782 }
3783}
3784
Christopher Faulet25a02f62018-10-24 12:00:25 +02003785/*
3786 * Parses the Cache-Control and Pragma request header fields to determine if
3787 * the request may be served from the cache and/or if it is cacheable. Updates
3788 * s->txn->flags.
3789 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003790void http_check_request_for_cacheability(struct stream *s, struct channel *req)
Christopher Faulet25a02f62018-10-24 12:00:25 +02003791{
3792 struct http_txn *txn = s->txn;
3793 struct htx *htx;
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003794 struct http_hdr_ctx ctx = { .blk = NULL };
3795 int pragma_found, cc_found;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003796
3797 if ((txn->flags & (TX_CACHEABLE|TX_CACHE_IGNORE)) == TX_CACHE_IGNORE)
3798 return; /* nothing more to do here */
3799
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003800 htx = htxbuf(&req->buf);
Christopher Faulet25a02f62018-10-24 12:00:25 +02003801 pragma_found = cc_found = 0;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003802
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003803 /* Check "pragma" header for HTTP/1.0 compatibility. */
3804 if (http_find_header(htx, ist("pragma"), &ctx, 1)) {
3805 if (isteqi(ctx.value, ist("no-cache"))) {
3806 pragma_found = 1;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003807 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003808 }
Christopher Faulet25a02f62018-10-24 12:00:25 +02003809
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003810 ctx.blk = NULL;
3811 /* Don't use the cache and don't try to store if we found the
3812 * Authorization header */
3813 if (http_find_header(htx, ist("authorization"), &ctx, 1)) {
3814 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3815 txn->flags |= TX_CACHE_IGNORE;
3816 }
Christopher Faulet25a02f62018-10-24 12:00:25 +02003817
Christopher Faulet25a02f62018-10-24 12:00:25 +02003818
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003819 /* Look for "cache-control" header and iterate over all the values
3820 * until we find one that specifies that caching is possible or not. */
3821 ctx.blk = NULL;
3822 while (http_find_header(htx, ist("cache-control"), &ctx, 0)) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003823 cc_found = 1;
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003824 /* We don't check the values after max-age, max-stale nor min-fresh,
3825 * we simply don't use the cache when they're specified. */
3826 if (istmatchi(ctx.value, ist("max-age")) ||
3827 istmatchi(ctx.value, ist("no-cache")) ||
3828 istmatchi(ctx.value, ist("max-stale")) ||
3829 istmatchi(ctx.value, ist("min-fresh"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003830 txn->flags |= TX_CACHE_IGNORE;
3831 continue;
3832 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003833 if (istmatchi(ctx.value, ist("no-store"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003834 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3835 continue;
3836 }
3837 }
3838
3839 /* RFC7234#5.4:
3840 * When the Cache-Control header field is also present and
3841 * understood in a request, Pragma is ignored.
3842 * When the Cache-Control header field is not present in a
3843 * request, caches MUST consider the no-cache request
3844 * pragma-directive as having the same effect as if
3845 * "Cache-Control: no-cache" were present.
3846 */
3847 if (!cc_found && pragma_found)
3848 txn->flags |= TX_CACHE_IGNORE;
3849}
3850
3851/*
3852 * Check if response is cacheable or not. Updates s->txn->flags.
3853 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003854void http_check_response_for_cacheability(struct stream *s, struct channel *res)
Christopher Faulet25a02f62018-10-24 12:00:25 +02003855{
3856 struct http_txn *txn = s->txn;
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003857 struct http_hdr_ctx ctx = { .blk = NULL };
Christopher Faulet25a02f62018-10-24 12:00:25 +02003858 struct htx *htx;
Remi Tricot-Le Bretoncc9bf2e2020-11-12 11:14:41 +01003859 int has_freshness_info = 0;
3860 int has_validator = 0;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003861
3862 if (txn->status < 200) {
3863 /* do not try to cache interim responses! */
3864 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3865 return;
3866 }
3867
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003868 htx = htxbuf(&res->buf);
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003869 /* Check "pragma" header for HTTP/1.0 compatibility. */
3870 if (http_find_header(htx, ist("pragma"), &ctx, 1)) {
3871 if (isteqi(ctx.value, ist("no-cache"))) {
3872 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3873 return;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003874 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003875 }
Christopher Faulet25a02f62018-10-24 12:00:25 +02003876
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003877 /* Look for "cache-control" header and iterate over all the values
3878 * until we find one that specifies that caching is possible or not. */
3879 ctx.blk = NULL;
3880 while (http_find_header(htx, ist("cache-control"), &ctx, 0)) {
3881 if (isteqi(ctx.value, ist("public"))) {
3882 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003883 continue;
3884 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003885 if (isteqi(ctx.value, ist("private")) ||
3886 isteqi(ctx.value, ist("no-cache")) ||
3887 isteqi(ctx.value, ist("no-store")) ||
3888 isteqi(ctx.value, ist("max-age=0")) ||
3889 isteqi(ctx.value, ist("s-maxage=0"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003890 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003891 continue;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003892 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003893 /* We might have a no-cache="set-cookie" form. */
3894 if (istmatchi(ctx.value, ist("no-cache=\"set-cookie"))) {
3895 txn->flags &= ~TX_CACHE_COOK;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003896 continue;
3897 }
Remi Tricot-Le Bretoncc9bf2e2020-11-12 11:14:41 +01003898
3899 if (istmatchi(ctx.value, ist("s-maxage")) ||
3900 istmatchi(ctx.value, ist("max-age"))) {
3901 has_freshness_info = 1;
3902 continue;
3903 }
3904 }
3905
3906 /* If no freshness information could be found in Cache-Control values,
3907 * look for an Expires header. */
3908 if (!has_freshness_info) {
3909 ctx.blk = NULL;
3910 has_freshness_info = http_find_header(htx, ist("expires"), &ctx, 0);
Christopher Faulet25a02f62018-10-24 12:00:25 +02003911 }
Remi Tricot-Le Bretoncc9bf2e2020-11-12 11:14:41 +01003912
3913 /* If no freshness information could be found in Cache-Control or Expires
3914 * values, look for an explicit validator. */
3915 if (!has_freshness_info) {
3916 ctx.blk = NULL;
3917 has_validator = 1;
3918 if (!http_find_header(htx, ist("etag"), &ctx, 0)) {
3919 ctx.blk = NULL;
3920 if (!http_find_header(htx, ist("last-modified"), &ctx, 0))
3921 has_validator = 0;
3922 }
3923 }
3924
3925 /* We won't store an entry that has neither a cache validator nor an
3926 * explicit expiration time, as suggested in RFC 7234#3. */
3927 if (!has_freshness_info && !has_validator)
3928 txn->flags |= TX_CACHE_IGNORE;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003929}
3930
Christopher Faulet377c5a52018-10-24 21:21:30 +02003931/*
3932 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
3933 * for the current backend.
3934 *
3935 * It is assumed that the request is either a HEAD, GET, or POST and that the
3936 * uri_auth field is valid.
3937 *
3938 * Returns 1 if stats should be provided, otherwise 0.
3939 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003940static int http_stats_check_uri(struct stream *s, struct http_txn *txn, struct proxy *backend)
Christopher Faulet377c5a52018-10-24 21:21:30 +02003941{
3942 struct uri_auth *uri_auth = backend->uri_auth;
3943 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003944 struct htx_sl *sl;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003945 struct ist uri;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003946
3947 if (!uri_auth)
3948 return 0;
3949
3950 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
3951 return 0;
3952
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003953 htx = htxbuf(&s->req.buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02003954 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003955 uri = htx_sl_req_uri(sl);
Willy Tarreau1eb3b482019-10-31 15:50:28 +01003956 if (*uri_auth->uri_prefix == '/')
3957 uri = http_get_path(uri);
Christopher Faulet377c5a52018-10-24 21:21:30 +02003958
3959 /* check URI size */
3960 if (uri_auth->uri_len > uri.len)
3961 return 0;
3962
3963 if (memcmp(uri.ptr, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
3964 return 0;
3965
3966 return 1;
3967}
3968
3969/* This function prepares an applet to handle the stats. It can deal with the
3970 * "100-continue" expectation, check that admin rules are met for POST requests,
3971 * and program a response message if something was unexpected. It cannot fail
3972 * and always relies on the stats applet to complete the job. It does not touch
3973 * analysers nor counters, which are left to the caller. It does not touch
3974 * s->target which is supposed to already point to the stats applet. The caller
3975 * is expected to have already assigned an appctx to the stream.
3976 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003977static int http_handle_stats(struct stream *s, struct channel *req)
Christopher Faulet377c5a52018-10-24 21:21:30 +02003978{
3979 struct stats_admin_rule *stats_admin_rule;
3980 struct stream_interface *si = &s->si[1];
3981 struct session *sess = s->sess;
3982 struct http_txn *txn = s->txn;
3983 struct http_msg *msg = &txn->req;
3984 struct uri_auth *uri_auth = s->be->uri_auth;
3985 const char *h, *lookup, *end;
3986 struct appctx *appctx;
3987 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003988 struct htx_sl *sl;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003989
3990 appctx = si_appctx(si);
3991 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
3992 appctx->st1 = appctx->st2 = 0;
3993 appctx->ctx.stats.st_code = STAT_STATUS_INIT;
Willy Tarreau676c29e2019-10-09 10:50:01 +02003994 appctx->ctx.stats.flags |= uri_auth->flags;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003995 appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */
3996 if ((msg->flags & HTTP_MSGF_VER_11) && (txn->meth != HTTP_METH_HEAD))
3997 appctx->ctx.stats.flags |= STAT_CHUNKED;
3998
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003999 htx = htxbuf(&req->buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02004000 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004001 lookup = HTX_SL_REQ_UPTR(sl) + uri_auth->uri_len;
4002 end = HTX_SL_REQ_UPTR(sl) + HTX_SL_REQ_ULEN(sl);
Christopher Faulet377c5a52018-10-24 21:21:30 +02004003
4004 for (h = lookup; h <= end - 3; h++) {
4005 if (memcmp(h, ";up", 3) == 0) {
4006 appctx->ctx.stats.flags |= STAT_HIDE_DOWN;
4007 break;
4008 }
Amaury Denoyelle91e55ea2021-02-25 14:46:08 +01004009 }
4010
4011 for (h = lookup; h <= end - 9; h++) {
4012 if (memcmp(h, ";no-maint", 9) == 0) {
Willy Tarreau3e320362020-10-23 17:28:57 +02004013 appctx->ctx.stats.flags |= STAT_HIDE_MAINT;
4014 break;
4015 }
Christopher Faulet377c5a52018-10-24 21:21:30 +02004016 }
4017
4018 if (uri_auth->refresh) {
4019 for (h = lookup; h <= end - 10; h++) {
4020 if (memcmp(h, ";norefresh", 10) == 0) {
4021 appctx->ctx.stats.flags |= STAT_NO_REFRESH;
4022 break;
4023 }
4024 }
4025 }
4026
4027 for (h = lookup; h <= end - 4; h++) {
4028 if (memcmp(h, ";csv", 4) == 0) {
Christopher Faulet6338a082019-09-09 15:50:54 +02004029 appctx->ctx.stats.flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
Christopher Faulet377c5a52018-10-24 21:21:30 +02004030 break;
4031 }
4032 }
4033
4034 for (h = lookup; h <= end - 6; h++) {
4035 if (memcmp(h, ";typed", 6) == 0) {
Christopher Faulet6338a082019-09-09 15:50:54 +02004036 appctx->ctx.stats.flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
Christopher Faulet377c5a52018-10-24 21:21:30 +02004037 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
4038 break;
4039 }
4040 }
4041
Christopher Faulet6338a082019-09-09 15:50:54 +02004042 for (h = lookup; h <= end - 5; h++) {
4043 if (memcmp(h, ";json", 5) == 0) {
4044 appctx->ctx.stats.flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
4045 appctx->ctx.stats.flags |= STAT_FMT_JSON;
4046 break;
4047 }
4048 }
4049
4050 for (h = lookup; h <= end - 12; h++) {
4051 if (memcmp(h, ";json-schema", 12) == 0) {
4052 appctx->ctx.stats.flags &= ~STAT_FMT_MASK;
4053 appctx->ctx.stats.flags |= STAT_JSON_SCHM;
4054 break;
4055 }
4056 }
4057
Christopher Faulet377c5a52018-10-24 21:21:30 +02004058 for (h = lookup; h <= end - 8; h++) {
4059 if (memcmp(h, ";st=", 4) == 0) {
4060 int i;
4061 h += 4;
4062 appctx->ctx.stats.st_code = STAT_STATUS_UNKN;
4063 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
4064 if (strncmp(stat_status_codes[i], h, 4) == 0) {
4065 appctx->ctx.stats.st_code = i;
4066 break;
4067 }
4068 }
4069 break;
4070 }
4071 }
4072
4073 appctx->ctx.stats.scope_str = 0;
4074 appctx->ctx.stats.scope_len = 0;
4075 for (h = lookup; h <= end - 8; h++) {
4076 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
4077 int itx = 0;
4078 const char *h2;
4079 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
4080 const char *err;
4081
4082 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
4083 h2 = h;
Christopher Fauleted7a0662019-01-14 11:07:34 +01004084 appctx->ctx.stats.scope_str = h2 - HTX_SL_REQ_UPTR(sl);
4085 while (h < end) {
Christopher Faulet377c5a52018-10-24 21:21:30 +02004086 if (*h == ';' || *h == '&' || *h == ' ')
4087 break;
4088 itx++;
4089 h++;
4090 }
4091
4092 if (itx > STAT_SCOPE_TXT_MAXLEN)
4093 itx = STAT_SCOPE_TXT_MAXLEN;
4094 appctx->ctx.stats.scope_len = itx;
4095
4096 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
4097 memcpy(scope_txt, h2, itx);
4098 scope_txt[itx] = '\0';
4099 err = invalid_char(scope_txt);
4100 if (err) {
4101 /* bad char in search text => clear scope */
4102 appctx->ctx.stats.scope_str = 0;
4103 appctx->ctx.stats.scope_len = 0;
4104 }
4105 break;
4106 }
4107 }
4108
4109 /* now check whether we have some admin rules for this request */
4110 list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) {
4111 int ret = 1;
4112
4113 if (stats_admin_rule->cond) {
4114 ret = acl_exec_cond(stats_admin_rule->cond, s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
4115 ret = acl_pass(ret);
4116 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
4117 ret = !ret;
4118 }
4119
4120 if (ret) {
4121 /* no rule, or the rule matches */
4122 appctx->ctx.stats.flags |= STAT_ADMIN;
4123 break;
4124 }
4125 }
4126
Christopher Faulet5d45e382019-02-27 15:15:23 +01004127 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
4128 appctx->st0 = STAT_HTTP_HEAD;
4129 else if (txn->meth == HTTP_METH_POST) {
Christopher Fauletbd9e8422019-08-15 22:26:48 +02004130 if (appctx->ctx.stats.flags & STAT_ADMIN) {
Christopher Faulet377c5a52018-10-24 21:21:30 +02004131 appctx->st0 = STAT_HTTP_POST;
Christopher Fauletbd9e8422019-08-15 22:26:48 +02004132 if (msg->msg_state < HTTP_MSG_DATA)
4133 req->analysers |= AN_REQ_HTTP_BODY;
4134 }
Christopher Faulet377c5a52018-10-24 21:21:30 +02004135 else {
Christopher Faulet5d45e382019-02-27 15:15:23 +01004136 /* POST without admin level */
Christopher Faulet377c5a52018-10-24 21:21:30 +02004137 appctx->ctx.stats.flags &= ~STAT_CHUNKED;
4138 appctx->ctx.stats.st_code = STAT_STATUS_DENY;
4139 appctx->st0 = STAT_HTTP_LAST;
4140 }
4141 }
4142 else {
Christopher Faulet5d45e382019-02-27 15:15:23 +01004143 /* Unsupported method */
4144 appctx->ctx.stats.flags &= ~STAT_CHUNKED;
4145 appctx->ctx.stats.st_code = STAT_STATUS_IVAL;
4146 appctx->st0 = STAT_HTTP_LAST;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004147 }
4148
4149 s->task->nice = -32; /* small boost for HTTP statistics */
4150 return 1;
4151}
4152
Christopher Faulet021a8e42021-03-29 10:46:38 +02004153/* This function waits for the message payload at most <time> milliseconds (may
4154 * be set to TICK_ETERNITY). It stops to wait if at least <bytes> bytes of the
4155 * payload are received (0 means no limit). It returns HTTP_RULE_* depending on
4156 * the result:
4157 *
4158 * - HTTP_RULE_RES_CONT when conditions are met to stop waiting
4159 * - HTTP_RULE_RES_YIELD to wait for more data
Ilya Shipitsinb2be9a12021-04-24 13:25:42 +05004160 * - HTTP_RULE_RES_ABRT when a timeout occurred.
Christopher Faulet021a8e42021-03-29 10:46:38 +02004161 * - HTTP_RULE_RES_BADREQ if a parsing error is raised by lower level
Ilya Shipitsinb2be9a12021-04-24 13:25:42 +05004162 * - HTTP_RULE_RES_ERROR if an internal error occurred
Christopher Faulet021a8e42021-03-29 10:46:38 +02004163 *
Ilya Shipitsinb2be9a12021-04-24 13:25:42 +05004164 * If a timeout occurred, this function is responsible to emit the right response
Christopher Faulet021a8e42021-03-29 10:46:38 +02004165 * to the client, depending on the channel (408 on request side, 504 on response
4166 * side). All other errors must be handled by the caller.
4167 */
4168enum rule_result http_wait_for_msg_body(struct stream *s, struct channel *chn,
4169 unsigned int time, unsigned int bytes)
4170{
4171 struct session *sess = s->sess;
4172 struct http_txn *txn = s->txn;
4173 struct http_msg *msg = ((chn->flags & CF_ISRESP) ? &txn->rsp : &txn->req);
4174 struct htx *htx;
4175 enum rule_result ret = HTTP_RULE_RES_CONT;
4176
4177 htx = htxbuf(&chn->buf);
4178
4179 if (htx->flags & HTX_FL_PARSING_ERROR) {
4180 ret = HTTP_RULE_RES_BADREQ;
4181 goto end;
4182 }
4183 if (htx->flags & HTX_FL_PROCESSING_ERROR) {
4184 ret = HTTP_RULE_RES_ERROR;
4185 goto end;
4186 }
4187
4188 /* Do nothing for bodyless and CONNECT requests */
4189 if (txn->meth == HTTP_METH_CONNECT || (msg->flags & HTTP_MSGF_BODYLESS))
4190 goto end;
4191
4192 if (!(chn->flags & CF_ISRESP) && msg->msg_state < HTTP_MSG_DATA) {
4193 if (http_handle_expect_hdr(s, htx, msg) == -1) {
4194 ret = HTTP_RULE_RES_ERROR;
4195 goto end;
4196 }
4197 }
4198
4199 msg->msg_state = HTTP_MSG_DATA;
4200
4201 /* Now we're in HTTP_MSG_DATA. We just need to know if all data have
4202 * been received or if the buffer is full.
4203 */
Christopher Faulet59df8122021-09-23 14:46:32 +02004204 if ((htx->flags & HTX_FL_EOM) ||
4205 htx_get_tail_type(htx) > HTX_BLK_DATA ||
4206 channel_htx_full(chn, htx, global.tune.maxrewrite) ||
4207 si_rx_blocked_room(chn_prod(chn)))
Christopher Faulet021a8e42021-03-29 10:46:38 +02004208 goto end;
4209
4210 if (bytes) {
4211 struct htx_blk *blk;
4212 unsigned int len = 0;
4213
4214 for (blk = htx_get_first_blk(htx); blk; blk = htx_get_next_blk(htx, blk)) {
4215 if (htx_get_blk_type(blk) != HTX_BLK_DATA)
4216 continue;
4217 len += htx_get_blksz(blk);
4218 if (len >= bytes)
4219 goto end;
4220 }
4221 }
4222
4223 if ((chn->flags & CF_READ_TIMEOUT) || tick_is_expired(chn->analyse_exp, now_ms)) {
4224 if (!(chn->flags & CF_ISRESP))
4225 goto abort_req;
4226 goto abort_res;
4227 }
4228
4229 /* we get here if we need to wait for more data */
4230 if (!(chn->flags & (CF_SHUTR | CF_READ_ERROR))) {
4231 if (!tick_isset(chn->analyse_exp))
4232 chn->analyse_exp = tick_add_ifset(now_ms, time);
4233 ret = HTTP_RULE_RES_YIELD;
4234 }
4235
4236 end:
4237 return ret;
4238
4239 abort_req:
4240 txn->status = 408;
4241 if (!(s->flags & SF_ERR_MASK))
4242 s->flags |= SF_ERR_CLITO;
4243 if (!(s->flags & SF_FINST_MASK))
4244 s->flags |= SF_FINST_D;
Willy Tarreau4781b152021-04-06 13:53:36 +02004245 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req);
Christopher Faulet021a8e42021-03-29 10:46:38 +02004246 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02004247 _HA_ATOMIC_INC(&sess->listener->counters->failed_req);
Christopher Faulet021a8e42021-03-29 10:46:38 +02004248 http_reply_and_close(s, txn->status, http_error_message(s));
4249 ret = HTTP_RULE_RES_ABRT;
4250 goto end;
4251
4252 abort_res:
4253 txn->status = 504;
4254 if (!(s->flags & SF_ERR_MASK))
4255 s->flags |= SF_ERR_SRVTO;
4256 if (!(s->flags & SF_FINST_MASK))
4257 s->flags |= SF_FINST_D;
4258 stream_inc_http_fail_ctr(s);
4259 http_reply_and_close(s, txn->status, http_error_message(s));
4260 ret = HTTP_RULE_RES_ABRT;
4261 goto end;
4262}
4263
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004264void http_perform_server_redirect(struct stream *s, struct stream_interface *si)
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004265{
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004266 struct channel *req = &s->req;
4267 struct channel *res = &s->res;
4268 struct server *srv;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004269 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004270 struct htx_sl *sl;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004271 struct ist path, location;
4272 unsigned int flags;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004273
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004274 /*
4275 * Create the location
4276 */
4277 chunk_reset(&trash);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004278
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004279 /* 1: add the server's prefix */
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004280 /* special prefix "/" means don't change URL */
4281 srv = __objt_server(s->target);
4282 if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') {
4283 if (!chunk_memcat(&trash, srv->rdr_pfx, srv->rdr_len))
4284 return;
4285 }
4286
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004287 /* 2: add the request Path */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004288 htx = htxbuf(&req->buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02004289 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004290 path = http_get_path(htx_sl_req_uri(sl));
Tim Duesterhused526372020-03-05 17:56:33 +01004291 if (!isttest(path))
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004292 return;
4293
4294 if (!chunk_memcat(&trash, path.ptr, path.len))
4295 return;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004296 location = ist2(trash.area, trash.data);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004297
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004298 /*
4299 * Create the 302 respone
4300 */
4301 htx = htx_from_buf(&res->buf);
4302 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
4303 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
4304 ist("HTTP/1.1"), ist("302"), ist("Found"));
4305 if (!sl)
4306 goto fail;
4307 sl->info.res.status = 302;
4308 s->txn->status = 302;
4309
4310 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) ||
4311 !htx_add_header(htx, ist("Connection"), ist("close")) ||
4312 !htx_add_header(htx, ist("Content-length"), ist("0")) ||
4313 !htx_add_header(htx, ist("Location"), location))
4314 goto fail;
4315
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004316 if (!htx_add_endof(htx, HTX_BLK_EOH))
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004317 goto fail;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004318
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004319 htx->flags |= HTX_FL_EOM;
Christopher Fauletc20afb82020-01-24 19:16:26 +01004320 htx_to_buf(htx, &res->buf);
Christopher Fauleta72a7e42020-01-28 09:28:11 +01004321 if (!http_forward_proxy_resp(s, 1))
4322 goto fail;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004323
4324 /* return without error. */
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004325 si_shutr(si);
4326 si_shutw(si);
4327 si->err_type = SI_ET_NONE;
4328 si->state = SI_ST_CLO;
4329
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004330 if (!(s->flags & SF_ERR_MASK))
4331 s->flags |= SF_ERR_LOCAL;
4332 if (!(s->flags & SF_FINST_MASK))
4333 s->flags |= SF_FINST_C;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004334
4335 /* FIXME: we should increase a counter of redirects per server and per backend. */
4336 srv_inc_sess_ctr(srv);
4337 srv_set_sess_last(srv);
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004338 return;
4339
4340 fail:
4341 /* If an error occurred, remove the incomplete HTTP response from the
4342 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004343 channel_htx_truncate(res, htx);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004344}
4345
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05004346/* This function terminates the request because it was completely analyzed or
Christopher Fauletf2824e62018-10-01 12:12:37 +02004347 * because an error was triggered during the body forwarding.
4348 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004349static void http_end_request(struct stream *s)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004350{
4351 struct channel *chn = &s->req;
4352 struct http_txn *txn = s->txn;
4353
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004354 DBG_TRACE_ENTER(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004355
Christopher Fauletb42a8b62018-11-19 21:59:00 +01004356 if (unlikely(txn->req.msg_state == HTTP_MSG_ERROR ||
4357 txn->rsp.msg_state == HTTP_MSG_ERROR)) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004358 channel_abort(chn);
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004359 channel_htx_truncate(chn, htxbuf(&chn->buf));
Christopher Fauletf2824e62018-10-01 12:12:37 +02004360 goto end;
4361 }
4362
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004363 if (unlikely(txn->req.msg_state < HTTP_MSG_DONE)) {
4364 DBG_TRACE_DEVEL("waiting end of the request", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004365 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004366 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004367
4368 if (txn->req.msg_state == HTTP_MSG_DONE) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004369 /* No need to read anymore, the request was completely parsed.
4370 * We can shut the read side unless we want to abort_on_close,
4371 * or we have a POST request. The issue with POST requests is
4372 * that some browsers still send a CRLF after the request, and
4373 * this CRLF must be read so that it does not remain in the kernel
4374 * buffers, otherwise a close could cause an RST on some systems
4375 * (eg: Linux).
4376 */
Christopher Faulet769d0e92019-03-22 14:23:18 +01004377 if (!(s->be->options & PR_O_ABRT_CLOSE) && txn->meth != HTTP_METH_POST)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004378 channel_dont_read(chn);
4379
4380 /* if the server closes the connection, we want to immediately react
4381 * and close the socket to save packets and syscalls.
4382 */
4383 s->si[1].flags |= SI_FL_NOHALF;
4384
4385 /* In any case we've finished parsing the request so we must
4386 * disable Nagle when sending data because 1) we're not going
4387 * to shut this side, and 2) the server is waiting for us to
4388 * send pending data.
4389 */
4390 chn->flags |= CF_NEVER_WAIT;
4391
Christopher Fauletd01ce402019-01-02 17:44:13 +01004392 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
4393 /* The server has not finished to respond, so we
4394 * don't want to move in order not to upset it.
4395 */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004396 DBG_TRACE_DEVEL("waiting end of the response", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletd01ce402019-01-02 17:44:13 +01004397 return;
4398 }
4399
Christopher Fauletf2824e62018-10-01 12:12:37 +02004400 /* When we get here, it means that both the request and the
4401 * response have finished receiving. Depending on the connection
4402 * mode, we'll have to wait for the last bytes to leave in either
4403 * direction, and sometimes for a close to be effective.
4404 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02004405 if (txn->flags & TX_CON_WANT_TUN) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004406 /* Tunnel mode will not have any analyser so it needs to
4407 * poll for reads.
4408 */
4409 channel_auto_read(chn);
4410 txn->req.msg_state = HTTP_MSG_TUNNEL;
4411 }
4412 else {
4413 /* we're not expecting any new data to come for this
4414 * transaction, so we can close it.
Christopher Faulet9768c262018-10-22 09:34:31 +02004415 *
4416 * However, there is an exception if the response
4417 * length is undefined. In this case, we need to wait
4418 * the close from the server. The response will be
4419 * switched in TUNNEL mode until the end.
Christopher Fauletf2824e62018-10-01 12:12:37 +02004420 */
4421 if (!(txn->rsp.flags & HTTP_MSGF_XFER_LEN) &&
4422 txn->rsp.msg_state != HTTP_MSG_CLOSED)
Christopher Faulet9768c262018-10-22 09:34:31 +02004423 goto check_channel_flags;
Christopher Fauletf2824e62018-10-01 12:12:37 +02004424
4425 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4426 channel_shutr_now(chn);
4427 channel_shutw_now(chn);
4428 }
4429 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004430 goto check_channel_flags;
4431 }
4432
4433 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
4434 http_msg_closing:
4435 /* nothing else to forward, just waiting for the output buffer
4436 * to be empty and for the shutw_now to take effect.
4437 */
4438 if (channel_is_empty(chn)) {
4439 txn->req.msg_state = HTTP_MSG_CLOSED;
4440 goto http_msg_closed;
4441 }
4442 else if (chn->flags & CF_SHUTW) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004443 txn->req.msg_state = HTTP_MSG_ERROR;
4444 goto end;
4445 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004446 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004447 return;
4448 }
4449
4450 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
4451 http_msg_closed:
Christopher Fauletf2824e62018-10-01 12:12:37 +02004452 /* if we don't know whether the server will close, we need to hard close */
4453 if (txn->rsp.flags & HTTP_MSGF_XFER_LEN)
4454 s->si[1].flags |= SI_FL_NOLINGER; /* we want to close ASAP */
Christopher Fauletf2824e62018-10-01 12:12:37 +02004455 /* see above in MSG_DONE why we only do this in these states */
Christopher Faulet769d0e92019-03-22 14:23:18 +01004456 if (!(s->be->options & PR_O_ABRT_CLOSE))
Christopher Fauletf2824e62018-10-01 12:12:37 +02004457 channel_dont_read(chn);
4458 goto end;
4459 }
4460
4461 check_channel_flags:
4462 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4463 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4464 /* if we've just closed an output, let's switch */
4465 txn->req.msg_state = HTTP_MSG_CLOSING;
4466 goto http_msg_closing;
4467 }
4468
4469 end:
4470 chn->analysers &= AN_REQ_FLT_END;
Christopher Faulet198ef8b2020-12-15 13:32:55 +01004471 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
4472 chn->flags |= CF_NEVER_WAIT;
4473 if (HAS_REQ_DATA_FILTERS(s))
Christopher Fauletf2824e62018-10-01 12:12:37 +02004474 chn->analysers |= AN_REQ_FLT_XFER_DATA;
Christopher Faulet198ef8b2020-12-15 13:32:55 +01004475 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004476 channel_auto_close(chn);
4477 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004478 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004479}
4480
4481
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05004482/* This function terminates the response because it was completely analyzed or
Christopher Fauletf2824e62018-10-01 12:12:37 +02004483 * because an error was triggered during the body forwarding.
4484 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004485static void http_end_response(struct stream *s)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004486{
4487 struct channel *chn = &s->res;
4488 struct http_txn *txn = s->txn;
4489
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004490 DBG_TRACE_ENTER(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004491
Christopher Fauletb42a8b62018-11-19 21:59:00 +01004492 if (unlikely(txn->req.msg_state == HTTP_MSG_ERROR ||
4493 txn->rsp.msg_state == HTTP_MSG_ERROR)) {
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004494 channel_htx_truncate(&s->req, htxbuf(&s->req.buf));
Christopher Faulet9768c262018-10-22 09:34:31 +02004495 channel_abort(&s->req);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004496 goto end;
4497 }
4498
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004499 if (unlikely(txn->rsp.msg_state < HTTP_MSG_DONE)) {
4500 DBG_TRACE_DEVEL("waiting end of the response", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004501 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004502 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004503
4504 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
4505 /* In theory, we don't need to read anymore, but we must
4506 * still monitor the server connection for a possible close
4507 * while the request is being uploaded, so we don't disable
4508 * reading.
4509 */
4510 /* channel_dont_read(chn); */
4511
4512 if (txn->req.msg_state < HTTP_MSG_DONE) {
4513 /* The client seems to still be sending data, probably
4514 * because we got an error response during an upload.
4515 * We have the choice of either breaking the connection
4516 * or letting it pass through. Let's do the later.
4517 */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004518 DBG_TRACE_DEVEL("waiting end of the request", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004519 return;
4520 }
4521
4522 /* When we get here, it means that both the request and the
4523 * response have finished receiving. Depending on the connection
4524 * mode, we'll have to wait for the last bytes to leave in either
4525 * direction, and sometimes for a close to be effective.
4526 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02004527 if (txn->flags & TX_CON_WANT_TUN) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004528 channel_auto_read(chn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004529 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
4530 }
4531 else {
4532 /* we're not expecting any new data to come for this
4533 * transaction, so we can close it.
4534 */
4535 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4536 channel_shutr_now(chn);
4537 channel_shutw_now(chn);
4538 }
4539 }
4540 goto check_channel_flags;
4541 }
4542
4543 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
4544 http_msg_closing:
4545 /* nothing else to forward, just waiting for the output buffer
4546 * to be empty and for the shutw_now to take effect.
4547 */
4548 if (channel_is_empty(chn)) {
4549 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4550 goto http_msg_closed;
4551 }
4552 else if (chn->flags & CF_SHUTW) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004553 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau4781b152021-04-06 13:53:36 +02004554 _HA_ATOMIC_INC(&strm_sess(s)->fe->fe_counters.cli_aborts);
4555 _HA_ATOMIC_INC(&s->be->be_counters.cli_aborts);
William Lallemand36119de2021-03-08 15:26:48 +01004556 if (strm_sess(s)->listener && strm_sess(s)->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02004557 _HA_ATOMIC_INC(&strm_sess(s)->listener->counters->cli_aborts);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004558 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02004559 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.cli_aborts);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004560 goto end;
4561 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004562 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004563 return;
4564 }
4565
4566 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
4567 http_msg_closed:
4568 /* drop any pending data */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004569 channel_htx_truncate(&s->req, htxbuf(&s->req.buf));
Christopher Faulet9768c262018-10-22 09:34:31 +02004570 channel_abort(&s->req);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004571 goto end;
4572 }
4573
4574 check_channel_flags:
4575 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4576 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4577 /* if we've just closed an output, let's switch */
4578 txn->rsp.msg_state = HTTP_MSG_CLOSING;
4579 goto http_msg_closing;
4580 }
4581
4582 end:
4583 chn->analysers &= AN_RES_FLT_END;
Christopher Faulet198ef8b2020-12-15 13:32:55 +01004584 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
4585 chn->flags |= CF_NEVER_WAIT;
4586 if (HAS_RSP_DATA_FILTERS(s))
4587 chn->analysers |= AN_RES_FLT_XFER_DATA;
4588 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004589 channel_auto_close(chn);
4590 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004591 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004592}
4593
Christopher Fauletef70e252020-01-28 09:26:19 +01004594/* Forward a response generated by HAProxy (error/redirect/return). This
4595 * function forwards all pending incoming data. If <final> is set to 0, nothing
4596 * more is performed. It is used for 1xx informational messages. Otherwise, the
Christopher Faulet507479b2020-05-15 12:29:46 +02004597 * transaction is terminated and the request is emptied. On success 1 is
Christopher Faulet40e6b552020-06-25 16:04:50 +02004598 * returned. If an error occurred, 0 is returned. If it fails, this function
4599 * only exits. It is the caller responsibility to do the cleanup.
Christopher Fauletef70e252020-01-28 09:26:19 +01004600 */
4601int http_forward_proxy_resp(struct stream *s, int final)
4602{
4603 struct channel *req = &s->req;
4604 struct channel *res = &s->res;
4605 struct htx *htx = htxbuf(&res->buf);
4606 size_t data;
4607
4608 if (final) {
4609 htx->flags |= HTX_FL_PROXY_RESP;
Christopher Faulet507479b2020-05-15 12:29:46 +02004610
Christopher Fauletaab1b672020-11-18 16:44:02 +01004611 if (!htx_is_empty(htx) && !http_eval_after_res_rules(s))
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01004612 return 0;
Christopher Fauletef70e252020-01-28 09:26:19 +01004613
Christopher Fauletd6c48362020-10-19 18:01:38 +02004614 if (s->txn->meth == HTTP_METH_HEAD)
4615 htx_skip_msg_payload(htx);
4616
Christopher Fauletef70e252020-01-28 09:26:19 +01004617 channel_auto_read(req);
4618 channel_abort(req);
4619 channel_auto_close(req);
4620 channel_htx_erase(req, htxbuf(&req->buf));
4621
4622 res->wex = tick_add_ifset(now_ms, res->wto);
4623 channel_auto_read(res);
4624 channel_auto_close(res);
4625 channel_shutr_now(res);
Christopher Faulet1a9db7c2020-06-25 15:36:45 +02004626 res->flags |= CF_EOI; /* The response is terminated, add EOI */
Christopher Faulet42432f32020-11-20 17:43:16 +01004627 htxbuf(&res->buf)->flags |= HTX_FL_EOM; /* no more data are expected */
Christopher Fauletef70e252020-01-28 09:26:19 +01004628 }
Christopher Fauletcf6898c2020-06-25 15:55:11 +02004629 else {
4630 /* Send ASAP informational messages. Rely on CF_EOI for final
4631 * response.
4632 */
4633 res->flags |= CF_SEND_DONTWAIT;
4634 }
Christopher Fauletef70e252020-01-28 09:26:19 +01004635
4636 data = htx->data - co_data(res);
4637 c_adv(res, data);
4638 htx->first = -1;
4639 res->total += data;
4640 return 1;
4641}
4642
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004643void http_server_error(struct stream *s, struct stream_interface *si, int err,
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004644 int finst, struct http_reply *msg)
Christopher Faulet0f226952018-10-22 09:29:56 +02004645{
Christopher Faulet72c7d8d2020-01-27 15:32:25 +01004646 http_reply_and_close(s, s->txn->status, msg);
Christopher Faulet0f226952018-10-22 09:29:56 +02004647 if (!(s->flags & SF_ERR_MASK))
4648 s->flags |= err;
4649 if (!(s->flags & SF_FINST_MASK))
4650 s->flags |= finst;
4651}
4652
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004653void http_reply_and_close(struct stream *s, short status, struct http_reply *msg)
Christopher Faulet0f226952018-10-22 09:29:56 +02004654{
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004655 if (!msg) {
4656 channel_htx_truncate(&s->res, htxbuf(&s->res.buf));
4657 goto end;
4658 }
4659
4660 if (http_reply_message(s, msg) == -1) {
4661 /* On error, return a 500 error message, but don't rewrite it if
Christopher Faulet40e6b552020-06-25 16:04:50 +02004662 * it is already an internal error. If it was already a "const"
4663 * 500 error, just fail.
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004664 */
Christopher Faulet40e6b552020-06-25 16:04:50 +02004665 if (s->txn->status == 500) {
4666 if (s->txn->flags & TX_CONST_REPLY)
4667 goto end;
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004668 s->txn->flags |= TX_CONST_REPLY;
Christopher Faulet40e6b552020-06-25 16:04:50 +02004669 }
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004670 s->txn->status = 500;
4671 s->txn->http_reply = NULL;
4672 return http_reply_and_close(s, s->txn->status, http_error_message(s));
4673 }
4674
4675end:
4676 s->res.wex = tick_add_ifset(now_ms, s->res.wto);
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004677
Christopher Fauletdb8ba102021-09-10 09:17:50 +02004678 /* At this staged, HTTP analysis is finished */
4679 s->req.analysers &= AN_REQ_FLT_END;
4680 s->req.analyse_exp = TICK_ETERNITY;
4681
4682 s->res.analysers &= AN_RES_FLT_END;
4683 s->res.analyse_exp = TICK_ETERNITY;
4684
Christopher Faulet0f226952018-10-22 09:29:56 +02004685 channel_auto_read(&s->req);
4686 channel_abort(&s->req);
4687 channel_auto_close(&s->req);
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004688 channel_htx_erase(&s->req, htxbuf(&s->req.buf));
Christopher Faulet72c7d8d2020-01-27 15:32:25 +01004689 channel_auto_read(&s->res);
4690 channel_auto_close(&s->res);
4691 channel_shutr_now(&s->res);
Christopher Faulet0f226952018-10-22 09:29:56 +02004692}
4693
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004694struct http_reply *http_error_message(struct stream *s)
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004695{
4696 const int msgnum = http_get_status_idx(s->txn->status);
4697
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004698 if (s->txn->http_reply)
4699 return s->txn->http_reply;
4700 else if (s->be->replies[msgnum])
4701 return s->be->replies[msgnum];
4702 else if (strm_fe(s)->replies[msgnum])
4703 return strm_fe(s)->replies[msgnum];
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004704 else
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004705 return &http_err_replies[msgnum];
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004706}
4707
Christopher Faulet40e6b552020-06-25 16:04:50 +02004708/* Produces an HTX message from an http reply. Depending on the http reply type,
4709 * a, errorfile, an raw file or a log-format string is used. On success, it
4710 * returns 0. If an error occurs -1 is returned. If it fails, this function only
4711 * exits. It is the caller responsibility to do the cleanup.
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004712 */
Christopher Fauletae43b6c2020-05-27 15:24:22 +02004713int http_reply_to_htx(struct stream *s, struct htx *htx, struct http_reply *reply)
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004714{
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004715 struct buffer *errmsg;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004716 struct htx_sl *sl;
4717 struct buffer *body = NULL;
4718 const char *status, *reason, *clen, *ctype;
4719 unsigned int slflags;
4720 int ret = 0;
4721
Christopher Faulete29a97e2020-05-14 14:49:25 +02004722 /*
4723 * - HTTP_REPLY_ERRFILES unexpected here. handled as no payload if so
4724 *
4725 * - HTTP_REPLY_INDIRECT: switch on another reply if defined or handled
4726 * as no payload if NULL. the TXN status code is set with the status
4727 * of the original reply.
4728 */
4729
4730 if (reply->type == HTTP_REPLY_INDIRECT) {
4731 if (reply->body.reply)
4732 reply = reply->body.reply;
4733 }
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004734 if (reply->type == HTTP_REPLY_ERRMSG && !reply->body.errmsg) {
4735 /* get default error message */
4736 if (reply == s->txn->http_reply)
4737 s->txn->http_reply = NULL;
4738 reply = http_error_message(s);
4739 if (reply->type == HTTP_REPLY_INDIRECT) {
4740 if (reply->body.reply)
4741 reply = reply->body.reply;
4742 }
4743 }
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004744
4745 if (reply->type == HTTP_REPLY_ERRMSG) {
4746 /* implicit or explicit error message*/
4747 errmsg = reply->body.errmsg;
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004748 if (errmsg && !b_is_null(errmsg)) {
Christopher Faulet20567362020-05-15 14:52:49 +02004749 if (!htx_copy_msg(htx, errmsg))
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004750 goto fail;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004751 }
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004752 }
4753 else {
4754 /* no payload, file or log-format string */
4755 if (reply->type == HTTP_REPLY_RAW) {
4756 /* file */
4757 body = &reply->body.obj;
4758 }
4759 else if (reply->type == HTTP_REPLY_LOGFMT) {
4760 /* log-format string */
4761 body = alloc_trash_chunk();
4762 if (!body)
4763 goto fail_alloc;
4764 body->data = build_logline(s, body->area, body->size, &reply->body.fmt);
4765 }
4766 /* else no payload */
4767
4768 status = ultoa(reply->status);
4769 reason = http_get_reason(reply->status);
4770 slflags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_CLEN);
4771 if (!body || !b_data(body))
4772 slflags |= HTX_SL_F_BODYLESS;
4773 sl = htx_add_stline(htx, HTX_BLK_RES_SL, slflags, ist("HTTP/1.1"), ist(status), ist(reason));
4774 if (!sl)
4775 goto fail;
4776 sl->info.res.status = reply->status;
4777
4778 clen = (body ? ultoa(b_data(body)) : "0");
4779 ctype = reply->ctype;
4780
4781 if (!LIST_ISEMPTY(&reply->hdrs)) {
4782 struct http_reply_hdr *hdr;
4783 struct buffer *value = alloc_trash_chunk();
4784
4785 if (!value)
4786 goto fail;
4787
4788 list_for_each_entry(hdr, &reply->hdrs, list) {
4789 chunk_reset(value);
4790 value->data = build_logline(s, value->area, value->size, &hdr->value);
4791 if (b_data(value) && !htx_add_header(htx, hdr->name, ist2(b_head(value), b_data(value)))) {
4792 free_trash_chunk(value);
4793 goto fail;
4794 }
4795 chunk_reset(value);
4796 }
4797 free_trash_chunk(value);
4798 }
4799
4800 if (!htx_add_header(htx, ist("content-length"), ist(clen)) ||
4801 (body && b_data(body) && ctype && !htx_add_header(htx, ist("content-type"), ist(ctype))) ||
4802 !htx_add_endof(htx, HTX_BLK_EOH) ||
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004803 (body && b_data(body) && !htx_add_data_atonce(htx, ist2(b_head(body), b_data(body)))))
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004804 goto fail;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004805
4806 htx->flags |= HTX_FL_EOM;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004807 }
4808
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004809 leave:
4810 if (reply->type == HTTP_REPLY_LOGFMT)
4811 free_trash_chunk(body);
4812 return ret;
4813
4814 fail_alloc:
4815 if (!(s->flags & SF_ERR_MASK))
4816 s->flags |= SF_ERR_RESOURCE;
Christopher Faulet97e466c2020-05-15 15:12:47 +02004817 /* fall through */
4818 fail:
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004819 ret = -1;
4820 goto leave;
Christopher Faulet97e466c2020-05-15 15:12:47 +02004821}
4822
4823/* Send an http reply to the client. On success, it returns 0. If an error
Christopher Faulet40e6b552020-06-25 16:04:50 +02004824 * occurs -1 is returned and the response channel is truncated, removing this
4825 * way the faulty reply. This function may fail when the reply is formatted
4826 * (http_reply_to_htx) or when the reply is forwarded
4827 * (http_forward_proxy_resp). On the last case, it is because a
4828 * http-after-response rule fails.
Christopher Faulet97e466c2020-05-15 15:12:47 +02004829 */
4830int http_reply_message(struct stream *s, struct http_reply *reply)
4831{
4832 struct channel *res = &s->res;
4833 struct htx *htx = htx_from_buf(&res->buf);
4834
4835 if (s->txn->status == -1)
4836 s->txn->status = reply->status;
4837 channel_htx_truncate(res, htx);
4838
4839 if (http_reply_to_htx(s, htx, reply) == -1)
4840 goto fail;
4841
4842 htx_to_buf(htx, &s->res.buf);
4843 if (!http_forward_proxy_resp(s, 1))
4844 goto fail;
4845 return 0;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004846
4847 fail:
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004848 channel_htx_truncate(res, htx);
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004849 if (!(s->flags & SF_ERR_MASK))
4850 s->flags |= SF_ERR_PRXCOND;
Christopher Faulet97e466c2020-05-15 15:12:47 +02004851 return -1;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004852}
4853
Christopher Faulet304cc402019-07-15 15:46:28 +02004854/* Return the error message corresponding to si->err_type. It is assumed
4855 * that the server side is closed. Note that err_type is actually a
4856 * bitmask, where almost only aborts may be cumulated with other
4857 * values. We consider that aborted operations are more important
4858 * than timeouts or errors due to the fact that nobody else in the
4859 * logs might explain incomplete retries. All others should avoid
4860 * being cumulated. It should normally not be possible to have multiple
4861 * aborts at once, but just in case, the first one in sequence is reported.
4862 * Note that connection errors appearing on the second request of a keep-alive
4863 * connection are not reported since this allows the client to retry.
4864 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004865void http_return_srv_error(struct stream *s, struct stream_interface *si)
Christopher Faulet304cc402019-07-15 15:46:28 +02004866{
4867 int err_type = si->err_type;
4868
4869 /* set s->txn->status for http_error_message(s) */
4870 s->txn->status = 503;
4871
4872 if (err_type & SI_ET_QUEUE_ABRT)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004873 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_Q,
4874 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004875 else if (err_type & SI_ET_CONN_ABRT)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004876 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_C,
4877 (s->txn->flags & TX_NOT_FIRST) ? NULL :
4878 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004879 else if (err_type & SI_ET_QUEUE_TO)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004880 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_Q,
4881 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004882 else if (err_type & SI_ET_QUEUE_ERR)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004883 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_Q,
4884 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004885 else if (err_type & SI_ET_CONN_TO)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004886 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_C,
4887 (s->txn->flags & TX_NOT_FIRST) ? NULL :
4888 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004889 else if (err_type & SI_ET_CONN_ERR)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004890 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_C,
4891 (s->flags & SF_SRV_REUSED) ? NULL :
4892 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004893 else if (err_type & SI_ET_CONN_RES)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004894 http_server_error(s, si, SF_ERR_RESOURCE, SF_FINST_C,
4895 (s->txn->flags & TX_NOT_FIRST) ? NULL :
4896 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004897 else { /* SI_ET_CONN_OTHER and others */
4898 s->txn->status = 500;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004899 http_server_error(s, si, SF_ERR_INTERNAL, SF_FINST_C,
4900 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004901 }
4902}
4903
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004904
Christopher Faulet4a28a532019-03-01 11:19:40 +01004905/* Handle Expect: 100-continue for HTTP/1.1 messages if necessary. It returns 0
4906 * on success and -1 on error.
4907 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004908static int http_handle_expect_hdr(struct stream *s, struct htx *htx, struct http_msg *msg)
Christopher Faulet4a28a532019-03-01 11:19:40 +01004909{
4910 /* If we have HTTP/1.1 message with a body and Expect: 100-continue,
4911 * then we must send an HTTP/1.1 100 Continue intermediate response.
4912 */
4913 if (msg->msg_state == HTTP_MSG_BODY && (msg->flags & HTTP_MSGF_VER_11) &&
4914 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
4915 struct ist hdr = { .ptr = "Expect", .len = 6 };
4916 struct http_hdr_ctx ctx;
4917
4918 ctx.blk = NULL;
4919 /* Expect is allowed in 1.1, look for it */
4920 if (http_find_header(htx, hdr, &ctx, 0) &&
4921 unlikely(isteqi(ctx.value, ist2("100-continue", 12)))) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004922 if (http_reply_100_continue(s) == -1)
Christopher Faulet4a28a532019-03-01 11:19:40 +01004923 return -1;
4924 http_remove_header(htx, &ctx);
4925 }
4926 }
4927 return 0;
4928}
4929
Christopher Faulet23a3c792018-11-28 10:01:23 +01004930/* Send a 100-Continue response to the client. It returns 0 on success and -1
4931 * on error. The response channel is updated accordingly.
4932 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004933static int http_reply_100_continue(struct stream *s)
Christopher Faulet23a3c792018-11-28 10:01:23 +01004934{
4935 struct channel *res = &s->res;
4936 struct htx *htx = htx_from_buf(&res->buf);
4937 struct htx_sl *sl;
4938 unsigned int flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|
4939 HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
Christopher Faulet23a3c792018-11-28 10:01:23 +01004940
4941 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
4942 ist("HTTP/1.1"), ist("100"), ist("Continue"));
4943 if (!sl)
4944 goto fail;
4945 sl->info.res.status = 100;
4946
Christopher Faulet1d5ec092019-06-26 14:23:54 +02004947 if (!htx_add_endof(htx, HTX_BLK_EOH))
Christopher Faulet23a3c792018-11-28 10:01:23 +01004948 goto fail;
4949
Christopher Fauleta72a7e42020-01-28 09:28:11 +01004950 if (!http_forward_proxy_resp(s, 0))
4951 goto fail;
Christopher Faulet23a3c792018-11-28 10:01:23 +01004952 return 0;
4953
4954 fail:
4955 /* If an error occurred, remove the incomplete HTTP response from the
4956 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004957 channel_htx_truncate(res, htx);
Christopher Faulet23a3c792018-11-28 10:01:23 +01004958 return -1;
4959}
4960
Christopher Faulet12c51e22018-11-28 15:59:42 +01004961
Christopher Faulet0f226952018-10-22 09:29:56 +02004962/*
4963 * Capture headers from message <htx> according to header list <cap_hdr>, and
4964 * fill the <cap> pointers appropriately.
4965 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004966static void http_capture_headers(struct htx *htx, char **cap, struct cap_hdr *cap_hdr)
Christopher Faulet0f226952018-10-22 09:29:56 +02004967{
4968 struct cap_hdr *h;
4969 int32_t pos;
4970
Christopher Fauleta3f15502019-05-13 15:27:23 +02004971 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet0f226952018-10-22 09:29:56 +02004972 struct htx_blk *blk = htx_get_blk(htx, pos);
4973 enum htx_blk_type type = htx_get_blk_type(blk);
4974 struct ist n, v;
4975
4976 if (type == HTX_BLK_EOH)
4977 break;
4978 if (type != HTX_BLK_HDR)
4979 continue;
4980
4981 n = htx_get_blk_name(htx, blk);
4982
4983 for (h = cap_hdr; h; h = h->next) {
4984 if (h->namelen && (h->namelen == n.len) &&
4985 (strncasecmp(n.ptr, h->name, h->namelen) == 0)) {
4986 if (cap[h->index] == NULL)
4987 cap[h->index] =
4988 pool_alloc(h->pool);
4989
4990 if (cap[h->index] == NULL) {
4991 ha_alert("HTTP capture : out of memory.\n");
4992 break;
4993 }
4994
4995 v = htx_get_blk_value(htx, blk);
4996 if (v.len > h->len)
4997 v.len = h->len;
4998
4999 memcpy(cap[h->index], v.ptr, v.len);
5000 cap[h->index][v.len]=0;
5001 }
5002 }
5003 }
5004}
5005
Christopher Faulet0b6bdc52018-10-24 11:05:36 +02005006/* Delete a value in a header between delimiters <from> and <next>. The header
5007 * itself is delimited by <start> and <end> pointers. The number of characters
5008 * displaced is returned, and the pointer to the first delimiter is updated if
5009 * required. The function tries as much as possible to respect the following
5010 * principles :
5011 * - replace <from> delimiter by the <next> one unless <from> points to <start>,
5012 * in which case <next> is simply removed
5013 * - set exactly one space character after the new first delimiter, unless there
5014 * are not enough characters in the block being moved to do so.
5015 * - remove unneeded spaces before the previous delimiter and after the new
5016 * one.
5017 *
5018 * It is the caller's responsibility to ensure that :
5019 * - <from> points to a valid delimiter or <start> ;
5020 * - <next> points to a valid delimiter or <end> ;
5021 * - there are non-space chars before <from>.
5022 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005023static int http_del_hdr_value(char *start, char *end, char **from, char *next)
Christopher Faulet0b6bdc52018-10-24 11:05:36 +02005024{
5025 char *prev = *from;
5026
5027 if (prev == start) {
5028 /* We're removing the first value. eat the semicolon, if <next>
5029 * is lower than <end> */
5030 if (next < end)
5031 next++;
5032
5033 while (next < end && HTTP_IS_SPHT(*next))
5034 next++;
5035 }
5036 else {
5037 /* Remove useless spaces before the old delimiter. */
5038 while (HTTP_IS_SPHT(*(prev-1)))
5039 prev--;
5040 *from = prev;
5041
5042 /* copy the delimiter and if possible a space if we're
5043 * not at the end of the line.
5044 */
5045 if (next < end) {
5046 *prev++ = *next++;
5047 if (prev + 1 < next)
5048 *prev++ = ' ';
5049 while (next < end && HTTP_IS_SPHT(*next))
5050 next++;
5051 }
5052 }
5053 memmove(prev, next, end - next);
5054 return (prev - next);
5055}
5056
Christopher Faulet0f226952018-10-22 09:29:56 +02005057
5058/* Formats the start line of the request (without CRLF) and puts it in <str> and
Joseph Herlantc42c0e92018-11-25 10:43:27 -08005059 * return the written length. The line can be truncated if it exceeds <len>.
Christopher Faulet0f226952018-10-22 09:29:56 +02005060 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005061static size_t http_fmt_req_line(const struct htx_sl *sl, char *str, size_t len)
Christopher Faulet0f226952018-10-22 09:29:56 +02005062{
5063 struct ist dst = ist2(str, 0);
5064
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005065 if (istcat(&dst, htx_sl_req_meth(sl), len) == -1)
Christopher Faulet0f226952018-10-22 09:29:56 +02005066 goto end;
5067 if (dst.len + 1 > len)
5068 goto end;
5069 dst.ptr[dst.len++] = ' ';
5070
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005071 if (istcat(&dst, htx_sl_req_uri(sl), len) == -1)
Christopher Faulet0f226952018-10-22 09:29:56 +02005072 goto end;
5073 if (dst.len + 1 > len)
5074 goto end;
5075 dst.ptr[dst.len++] = ' ';
5076
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005077 istcat(&dst, htx_sl_req_vsn(sl), len);
Christopher Faulet0f226952018-10-22 09:29:56 +02005078 end:
5079 return dst.len;
5080}
5081
5082/*
5083 * Print a debug line with a start line.
5084 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005085static void http_debug_stline(const char *dir, struct stream *s, const struct htx_sl *sl)
Christopher Faulet0f226952018-10-22 09:29:56 +02005086{
5087 struct session *sess = strm_sess(s);
5088 int max;
5089
5090 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
5091 dir,
Willy Tarreau66182592021-12-06 07:01:02 +00005092 objt_conn(sess->origin) ? (unsigned short)__objt_conn(sess->origin)->handle.fd : -1,
5093 objt_cs(s->si[1].end) ? (unsigned short)__objt_cs(s->si[1].end)->conn->handle.fd : -1);
Christopher Faulet0f226952018-10-22 09:29:56 +02005094
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005095 max = HTX_SL_P1_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02005096 UBOUND(max, trash.size - trash.data - 3);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005097 chunk_memcat(&trash, HTX_SL_P1_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02005098 trash.area[trash.data++] = ' ';
5099
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005100 max = HTX_SL_P2_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02005101 UBOUND(max, trash.size - trash.data - 2);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005102 chunk_memcat(&trash, HTX_SL_P2_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02005103 trash.area[trash.data++] = ' ';
5104
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005105 max = HTX_SL_P3_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02005106 UBOUND(max, trash.size - trash.data - 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005107 chunk_memcat(&trash, HTX_SL_P3_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02005108 trash.area[trash.data++] = '\n';
5109
Willy Tarreau2e8ab6b2020-03-14 11:03:20 +01005110 DISGUISE(write(1, trash.area, trash.data));
Christopher Faulet0f226952018-10-22 09:29:56 +02005111}
5112
5113/*
5114 * Print a debug line with a header.
5115 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005116static 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 +02005117{
5118 struct session *sess = strm_sess(s);
5119 int max;
5120
5121 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
5122 dir,
Willy Tarreau66182592021-12-06 07:01:02 +00005123 objt_conn(sess->origin) ? (unsigned short)__objt_conn(sess->origin)->handle.fd : -1,
5124 objt_cs(s->si[1].end) ? (unsigned short)__objt_cs(s->si[1].end)->conn->handle.fd : -1);
Christopher Faulet0f226952018-10-22 09:29:56 +02005125
5126 max = n.len;
5127 UBOUND(max, trash.size - trash.data - 3);
5128 chunk_memcat(&trash, n.ptr, max);
5129 trash.area[trash.data++] = ':';
5130 trash.area[trash.data++] = ' ';
5131
5132 max = v.len;
5133 UBOUND(max, trash.size - trash.data - 1);
5134 chunk_memcat(&trash, v.ptr, max);
5135 trash.area[trash.data++] = '\n';
5136
Willy Tarreau2e8ab6b2020-03-14 11:03:20 +01005137 DISGUISE(write(1, trash.area, trash.data));
Christopher Faulet0f226952018-10-22 09:29:56 +02005138}
5139
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005140/* Allocate a new HTTP transaction for stream <s> unless there is one already.
5141 * In case of allocation failure, everything allocated is freed and NULL is
5142 * returned. Otherwise the new transaction is assigned to the stream and
5143 * returned.
5144 */
5145struct http_txn *http_alloc_txn(struct stream *s)
5146{
5147 struct http_txn *txn = s->txn;
5148
5149 if (txn)
5150 return txn;
5151
5152 txn = pool_alloc(pool_head_http_txn);
5153 if (!txn)
5154 return txn;
5155
5156 s->txn = txn;
5157 return txn;
5158}
5159
5160void http_txn_reset_req(struct http_txn *txn)
5161{
Christopher Faulet1aea50e2020-01-17 16:03:53 +01005162 txn->req.flags = 0;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005163 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
5164}
5165
5166void http_txn_reset_res(struct http_txn *txn)
5167{
Christopher Faulet1aea50e2020-01-17 16:03:53 +01005168 txn->rsp.flags = 0;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005169 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
5170}
5171
5172/*
Christopher Faulet75f619a2021-03-08 19:12:58 +01005173 * Create and initialize a new HTTP transaction for stream <s>. This should be
5174 * used before processing any new request. It returns the transaction or NLULL
5175 * on error.
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005176 */
Christopher Faulet75f619a2021-03-08 19:12:58 +01005177struct http_txn *http_create_txn(struct stream *s)
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005178{
Christopher Faulet75f619a2021-03-08 19:12:58 +01005179 struct http_txn *txn;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005180 struct conn_stream *cs = objt_cs(s->si[0].end);
5181
Christopher Faulet75f619a2021-03-08 19:12:58 +01005182 txn = pool_alloc(pool_head_http_txn);
5183 if (!txn)
5184 return NULL;
5185 s->txn = txn;
5186
Christopher Fauletdb7ced02022-06-22 17:12:05 +02005187 txn->meth = HTTP_METH_OTHER;
Christopher Fauletda831fa2020-10-06 17:58:43 +02005188 txn->flags = ((cs && cs->flags & CS_FL_NOT_FIRST) ? TX_NOT_FIRST : 0);
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005189 txn->status = -1;
Christopher Faulet5cb513a2020-05-13 17:56:56 +02005190 txn->http_reply = NULL;
Willy Tarreau8b507582020-02-25 09:35:07 +01005191 write_u32(txn->cache_hash, 0);
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005192
5193 txn->cookie_first_date = 0;
5194 txn->cookie_last_date = 0;
5195
5196 txn->srv_cookie = NULL;
5197 txn->cli_cookie = NULL;
5198 txn->uri = NULL;
5199
5200 http_txn_reset_req(txn);
5201 http_txn_reset_res(txn);
5202
5203 txn->req.chn = &s->req;
5204 txn->rsp.chn = &s->res;
5205
5206 txn->auth.method = HTTP_AUTH_UNKNOWN;
5207
5208 vars_init(&s->vars_txn, SCOPE_TXN);
5209 vars_init(&s->vars_reqres, SCOPE_REQ);
Christopher Faulet75f619a2021-03-08 19:12:58 +01005210
5211 return txn;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005212}
5213
5214/* to be used at the end of a transaction */
Christopher Faulet75f619a2021-03-08 19:12:58 +01005215void http_destroy_txn(struct stream *s)
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005216{
5217 struct http_txn *txn = s->txn;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005218
5219 /* these ones will have been dynamically allocated */
5220 pool_free(pool_head_requri, txn->uri);
5221 pool_free(pool_head_capture, txn->cli_cookie);
5222 pool_free(pool_head_capture, txn->srv_cookie);
Tim Duesterhusa17e6622020-03-05 20:19:02 +01005223 pool_free(pool_head_uniqueid, s->unique_id.ptr);
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005224
Tim Duesterhusa17e6622020-03-05 20:19:02 +01005225 s->unique_id = IST_NULL;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005226 txn->uri = NULL;
5227 txn->srv_cookie = NULL;
5228 txn->cli_cookie = NULL;
5229
Christopher Faulet59399252019-11-07 14:27:52 +01005230 if (!LIST_ISEMPTY(&s->vars_txn.head))
5231 vars_prune(&s->vars_txn, s->sess, s);
5232 if (!LIST_ISEMPTY(&s->vars_reqres.head))
5233 vars_prune(&s->vars_reqres, s->sess, s);
Christopher Faulet75f619a2021-03-08 19:12:58 +01005234
5235 pool_free(pool_head_http_txn, txn);
5236 s->txn = NULL;
Christopher Faulet59399252019-11-07 14:27:52 +01005237}
5238
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005239
5240DECLARE_POOL(pool_head_http_txn, "http_txn", sizeof(struct http_txn));
Christopher Faulet0f226952018-10-22 09:29:56 +02005241
Christopher Fauletf4eb75d2018-10-11 15:55:07 +02005242__attribute__((constructor))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005243static void __http_protocol_init(void)
Christopher Fauletf4eb75d2018-10-11 15:55:07 +02005244{
5245}
5246
5247
5248/*
5249 * Local variables:
5250 * c-indent-level: 8
5251 * c-basic-offset: 8
5252 * End:
5253 */