blob: 1029d85234c96ac2232390241701ce9d9cfd2b37 [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 Fauletb8a53712019-12-16 11:29:38 +0100411 goto return_int_err;
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
556 return_int_err:
557 txn->status = 500;
558 if (!(s->flags & SF_ERR_MASK))
559 s->flags |= SF_ERR_INTERNAL;
Willy Tarreau4781b152021-04-06 13:53:36 +0200560 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100561 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau4781b152021-04-06 13:53:36 +0200562 _HA_ATOMIC_INC(&s->be->be_counters.internal_errors);
William Lallemand36119de2021-03-08 15:26:48 +0100563 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200564 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100565 goto return_prx_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200566
567 return_bad_req:
Christopher Faulete0768eb2018-10-03 16:38:02 +0200568 txn->status = 400;
Willy Tarreau4781b152021-04-06 13:53:36 +0200569 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req);
William Lallemand36119de2021-03-08 15:26:48 +0100570 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200571 _HA_ATOMIC_INC(&sess->listener->counters->failed_req);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100572 /* fall through */
573
574 return_prx_err:
575 http_reply_and_close(s, txn->status, http_error_message(s));
576 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200577
578 return_prx_cond:
579 if (!(s->flags & SF_ERR_MASK))
580 s->flags |= SF_ERR_PRXCOND;
581 if (!(s->flags & SF_FINST_MASK))
582 s->flags |= SF_FINST_R;
583
584 req->analysers &= AN_REQ_FLT_END;
585 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100586 DBG_TRACE_DEVEL("leaving on error",
587 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200588 return 0;
589
590 return_prx_yield:
591 channel_dont_connect(req);
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100592 DBG_TRACE_DEVEL("waiting for more data",
593 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200594 return 0;
595}
596
597/* This function performs all the processing enabled for the current request.
598 * It returns 1 if the processing can continue on next analysers, or zero if it
599 * needs more data, encounters an error, or wants to immediately abort the
600 * request. It relies on buffers flags, and updates s->req.analysers.
601 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200602int http_process_request(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200603{
604 struct session *sess = s->sess;
605 struct http_txn *txn = s->txn;
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200606 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200607 struct connection *cli_conn = objt_conn(strm_sess(s)->origin);
608
Christopher Faulet8bebd2f2020-10-06 17:54:56 +0200609 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200610
611 /*
612 * Right now, we know that we have processed the entire headers
613 * and that unwanted requests have been filtered out. We can do
614 * whatever we want with the remaining request. Also, now we
615 * may have separate values for ->fe, ->be.
616 */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100617 htx = htxbuf(&req->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200618
619 /*
620 * If HTTP PROXY is set we simply get remote server address parsing
Willy Tarreau1c8d32b2019-07-18 15:47:45 +0200621 * incoming request.
Christopher Faulete0768eb2018-10-03 16:38:02 +0200622 */
623 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SF_ADDR_SET)) {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100624 struct htx_sl *sl;
625 struct ist uri, path;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200626
Willy Tarreau9b7587a2020-10-15 07:32:10 +0200627 if (!sockaddr_alloc(&s->target_addr, NULL, 0)) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200628 if (!(s->flags & SF_ERR_MASK))
629 s->flags |= SF_ERR_RESOURCE;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100630 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200631 }
Christopher Faulet297fbb42019-05-13 14:41:27 +0200632 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100633 uri = htx_sl_req_uri(sl);
634 path = http_get_path(uri);
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200635
Willy Tarreau1c8d32b2019-07-18 15:47:45 +0200636 if (url2sa(uri.ptr, uri.len - path.len, s->target_addr, NULL) == -1)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200637 goto return_bad_req;
638
Willy Tarreau1c8d32b2019-07-18 15:47:45 +0200639 s->target = &s->be->obj_type;
640 s->flags |= SF_ADDR_SET | SF_ASSIGNED;
641
Christopher Faulete0768eb2018-10-03 16:38:02 +0200642 /* if the path was found, we have to remove everything between
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200643 * uri.ptr and path.ptr (excluded). If it was not found, we need
644 * to replace from all the uri by a single "/".
645 *
Ilya Shipitsin46a030c2020-07-05 16:36:08 +0500646 * Instead of rewriting the whole start line, we just update
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100647 * the star-line URI. Some space will be lost but it should be
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200648 * insignificant.
Christopher Faulete0768eb2018-10-03 16:38:02 +0200649 */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100650 istcpy(&uri, (path.len ? path : ist("/")), uri.len);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200651 }
652
653 /*
654 * 7: Now we can work with the cookies.
655 * Note that doing so might move headers in the request, but
656 * the fields will stay coherent and the URI will not move.
657 * This should only be performed in the backend.
658 */
659 if (s->be->cookie_name || sess->fe->capture_name)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200660 http_manage_client_side_cookies(s, req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200661
Tim Duesterhus2825b4b2020-02-28 15:13:34 +0100662 /* 8: Generate unique ID if a "unique-id-format" is defined.
663 *
664 * A unique ID is generated even when it is not sent to ensure that the ID can make use of
665 * fetches only available in the HTTP request processing stage.
666 */
667 if (!LIST_ISEMPTY(&sess->fe->format_unique_id)) {
Tim Duesterhusa17e6622020-03-05 20:19:02 +0100668 struct ist unique_id = stream_generate_unique_id(s, &sess->fe->format_unique_id);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200669
Tim Duesterhusa17e6622020-03-05 20:19:02 +0100670 if (!isttest(unique_id)) {
Christopher Fauletb8a53712019-12-16 11:29:38 +0100671 if (!(s->flags & SF_ERR_MASK))
672 s->flags |= SF_ERR_RESOURCE;
673 goto return_int_err;
674 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200675
Tim Duesterhus2825b4b2020-02-28 15:13:34 +0100676 /* send unique ID if a "unique-id-header" is defined */
Tim Duesterhus0643b0e2020-03-05 17:56:35 +0100677 if (isttest(sess->fe->header_unique_id) &&
Tim Duesterhusa17e6622020-03-05 20:19:02 +0100678 unlikely(!http_add_header(htx, sess->fe->header_unique_id, s->unique_id)))
Tim Duesterhus2825b4b2020-02-28 15:13:34 +0100679 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200680 }
681
682 /*
683 * 9: add X-Forwarded-For if either the frontend or the backend
684 * asks for it.
685 */
686 if ((sess->fe->options | s->be->options) & PR_O_FWDFOR) {
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200687 struct http_hdr_ctx ctx = { .blk = NULL };
688 struct ist hdr = ist2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : sess->fe->fwdfor_hdr_name,
689 s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : sess->fe->fwdfor_hdr_len);
690
Christopher Faulete0768eb2018-10-03 16:38:02 +0200691 if (!((sess->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200692 http_find_header(htx, hdr, &ctx, 0)) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200693 /* The header is set to be added only if none is present
694 * and we found it, so don't do anything.
695 */
696 }
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200697 else if (cli_conn && conn_get_src(cli_conn) && cli_conn->src->ss_family == AF_INET) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200698 /* Add an X-Forwarded-For header unless the source IP is
699 * in the 'except' network range.
700 */
Christopher Faulet5d1def62021-02-26 09:19:15 +0100701 if (ipcmp2net(cli_conn->src, &sess->fe->except_xff_net) &&
702 ipcmp2net(cli_conn->src, &s->be->except_xff_net)) {
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200703 unsigned char *pn = (unsigned char *)&((struct sockaddr_in *)cli_conn->src)->sin_addr;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200704
705 /* Note: we rely on the backend to get the header name to be used for
706 * x-forwarded-for, because the header is really meant for the backends.
707 * However, if the backend did not specify any option, we have to rely
708 * on the frontend's header name.
709 */
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200710 chunk_printf(&trash, "%d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
711 if (unlikely(!http_add_header(htx, hdr, ist2(trash.area, trash.data))))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100712 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200713 }
714 }
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200715 else if (cli_conn && conn_get_src(cli_conn) && cli_conn->src->ss_family == AF_INET6) {
Christopher Faulet5d1def62021-02-26 09:19:15 +0100716 /* Add an X-Forwarded-For header unless the source IP is
717 * in the 'except' network range.
Christopher Faulete0768eb2018-10-03 16:38:02 +0200718 */
Christopher Faulet5d1def62021-02-26 09:19:15 +0100719 if (ipcmp2net(cli_conn->src, &sess->fe->except_xff_net) &&
720 ipcmp2net(cli_conn->src, &s->be->except_xff_net)) {
721 char pn[INET6_ADDRSTRLEN];
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200722
Christopher Faulet5d1def62021-02-26 09:19:15 +0100723 inet_ntop(AF_INET6,
724 (const void *)&((struct sockaddr_in6 *)(cli_conn->src))->sin6_addr,
725 pn, sizeof(pn));
Christopher Faulete0768eb2018-10-03 16:38:02 +0200726
Christopher Faulet5d1def62021-02-26 09:19:15 +0100727 /* Note: we rely on the backend to get the header name to be used for
728 * x-forwarded-for, because the header is really meant for the backends.
729 * However, if the backend did not specify any option, we have to rely
730 * on the frontend's header name.
731 */
732 chunk_printf(&trash, "%s", pn);
733 if (unlikely(!http_add_header(htx, hdr, ist2(trash.area, trash.data))))
734 goto return_int_err;
735 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200736 }
737 }
738
739 /*
740 * 10: add X-Original-To if either the frontend or the backend
741 * asks for it.
742 */
743 if ((sess->fe->options | s->be->options) & PR_O_ORGTO) {
Christopher Faulet5d1def62021-02-26 09:19:15 +0100744 struct ist hdr = ist2(s->be->orgto_hdr_len ? s->be->orgto_hdr_name : sess->fe->orgto_hdr_name,
745 s->be->orgto_hdr_len ? s->be->orgto_hdr_len : sess->fe->orgto_hdr_len);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200746
Christopher Fauletcccded92021-02-26 12:45:56 +0100747 if (cli_conn && conn_get_dst(cli_conn) && cli_conn->dst->ss_family == AF_INET) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200748 /* Add an X-Original-To header unless the destination IP is
749 * in the 'except' network range.
750 */
Christopher Faulet5d1def62021-02-26 09:19:15 +0100751 if (ipcmp2net(cli_conn->dst, &sess->fe->except_xot_net) &&
752 ipcmp2net(cli_conn->dst, &s->be->except_xot_net)) {
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200753 unsigned char *pn = (unsigned char *)&((struct sockaddr_in *)cli_conn->dst)->sin_addr;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200754
755 /* Note: we rely on the backend to get the header name to be used for
756 * x-original-to, because the header is really meant for the backends.
757 * However, if the backend did not specify any option, we have to rely
758 * on the frontend's header name.
759 */
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200760 chunk_printf(&trash, "%d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
761 if (unlikely(!http_add_header(htx, hdr, ist2(trash.area, trash.data))))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100762 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200763 }
764 }
Christopher Faulet5d1def62021-02-26 09:19:15 +0100765 else if (cli_conn && conn_get_dst(cli_conn) && cli_conn->dst->ss_family == AF_INET6) {
766 /* Add an X-Original-To header unless the source IP is
767 * in the 'except' network range.
768 */
769 if (ipcmp2net(cli_conn->dst, &sess->fe->except_xot_net) &&
770 ipcmp2net(cli_conn->dst, &s->be->except_xot_net)) {
771 char pn[INET6_ADDRSTRLEN];
772
773 inet_ntop(AF_INET6,
774 (const void *)&((struct sockaddr_in6 *)(cli_conn->dst))->sin6_addr,
775 pn, sizeof(pn));
776
777 /* Note: we rely on the backend to get the header name to be used for
778 * x-forwarded-for, because the header is really meant for the backends.
779 * However, if the backend did not specify any option, we have to rely
780 * on the frontend's header name.
781 */
782 chunk_printf(&trash, "%s", pn);
783 if (unlikely(!http_add_header(htx, hdr, ist2(trash.area, trash.data))))
784 goto return_int_err;
785 }
786 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200787 }
788
Christopher Fauletc2ac5e42021-03-08 18:20:09 +0100789 /* Filter the request headers if there are filters attached to the
790 * stream.
791 */
792 if (HAS_FILTERS(s))
793 req->analysers |= AN_REQ_FLT_HTTP_HDRS;
794
Christopher Faulete0768eb2018-10-03 16:38:02 +0200795 /* If we have no server assigned yet and we're balancing on url_param
796 * with a POST request, we may be interested in checking the body for
797 * that parameter. This will be done in another analyser.
798 */
799 if (!(s->flags & (SF_ASSIGNED|SF_DIRECT)) &&
Willy Tarreau089eaa02019-01-14 15:17:46 +0100800 s->txn->meth == HTTP_METH_POST &&
801 (s->be->lbprm.algo & BE_LB_ALGO) == BE_LB_ALGO_PH) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200802 channel_dont_connect(req);
803 req->analysers |= AN_REQ_HTTP_BODY;
804 }
805
806 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
807 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau1a18b542018-12-11 16:37:42 +0100808
Christopher Faulete0768eb2018-10-03 16:38:02 +0200809 /* We expect some data from the client. Unless we know for sure
810 * we already have a full request, we have to re-enable quick-ack
811 * in case we previously disabled it, otherwise we might cause
812 * the client to delay further data.
813 */
William Lallemand36119de2021-03-08 15:26:48 +0100814 if ((sess->listener && (sess->listener->options & LI_O_NOQUICKACK)) && !(htx->flags & HTX_FL_EOM))
Willy Tarreau1a18b542018-12-11 16:37:42 +0100815 conn_set_quickack(cli_conn, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200816
817 /*************************************************************
818 * OK, that's finished for the headers. We have done what we *
819 * could. Let's switch to the DATA state. *
820 ************************************************************/
821 req->analyse_exp = TICK_ETERNITY;
822 req->analysers &= ~an_bit;
823
824 s->logs.tv_request = now;
825 /* OK let's go on with the BODY now */
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100826 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200827 return 1;
828
Christopher Fauletb8a53712019-12-16 11:29:38 +0100829 return_int_err:
830 txn->status = 500;
831 if (!(s->flags & SF_ERR_MASK))
832 s->flags |= SF_ERR_INTERNAL;
Willy Tarreau4781b152021-04-06 13:53:36 +0200833 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100834 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau4781b152021-04-06 13:53:36 +0200835 _HA_ATOMIC_INC(&s->be->be_counters.internal_errors);
William Lallemand36119de2021-03-08 15:26:48 +0100836 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200837 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100838 goto return_prx_cond;
839
Christopher Faulete0768eb2018-10-03 16:38:02 +0200840 return_bad_req: /* let's centralize all bad requests */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200841 txn->status = 400;
Willy Tarreau4781b152021-04-06 13:53:36 +0200842 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req);
William Lallemand36119de2021-03-08 15:26:48 +0100843 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200844 _HA_ATOMIC_INC(&sess->listener->counters->failed_req);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100845 /* fall through */
846
847 return_prx_cond:
848 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +0200849
850 if (!(s->flags & SF_ERR_MASK))
851 s->flags |= SF_ERR_PRXCOND;
852 if (!(s->flags & SF_FINST_MASK))
853 s->flags |= SF_FINST_R;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100854
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100855 DBG_TRACE_DEVEL("leaving on error",
856 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200857 return 0;
858}
859
860/* This function is an analyser which processes the HTTP tarpit. It always
861 * returns zero, at the beginning because it prevents any other processing
862 * from occurring, and at the end because it terminates the request.
863 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200864int http_process_tarpit(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200865{
866 struct http_txn *txn = s->txn;
867
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100868 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, &txn->req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200869 /* This connection is being tarpitted. The CLIENT side has
870 * already set the connect expiration date to the right
871 * timeout. We just have to check that the client is still
872 * there and that the timeout has not expired.
873 */
874 channel_dont_connect(req);
875 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100876 !tick_is_expired(req->analyse_exp, now_ms)) {
Christopher Fauletb02d5f02021-10-29 14:37:07 +0200877 /* Be sure to drain all data from the request channel */
878 channel_htx_erase(req, htxbuf(&req->buf));
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100879 DBG_TRACE_DEVEL("waiting for tarpit timeout expiry",
880 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200881 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100882 }
883
Christopher Faulete0768eb2018-10-03 16:38:02 +0200884
885 /* We will set the queue timer to the time spent, just for
886 * logging purposes. We fake a 500 server error, so that the
887 * attacker will not suspect his connection has been tarpitted.
888 * It will not cause trouble to the logs because we can exclude
889 * the tarpitted connections by filtering on the 'PT' status flags.
890 */
891 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
892
Christopher Faulet8dfeccf2020-05-15 14:16:29 +0200893 http_reply_and_close(s, txn->status, (!(req->flags & CF_READ_ERROR) ? http_error_message(s) : NULL));
Christopher Faulet5cb513a2020-05-13 17:56:56 +0200894
Christopher Faulete0768eb2018-10-03 16:38:02 +0200895 if (!(s->flags & SF_ERR_MASK))
896 s->flags |= SF_ERR_PRXCOND;
897 if (!(s->flags & SF_FINST_MASK))
898 s->flags |= SF_FINST_T;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100899
900 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200901 return 0;
902}
903
904/* This function is an analyser which waits for the HTTP request body. It waits
905 * for either the buffer to be full, or the full advertised contents to have
906 * reached the buffer. It must only be called after the standard HTTP request
907 * processing has occurred, because it expects the request to be parsed and will
908 * look for the Expect header. It may send a 100-Continue interim response. It
909 * takes in input any state starting from HTTP_MSG_BODY and leaves with one of
910 * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it
911 * needs to read more data, or 1 once it has completed its analysis.
912 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200913int http_wait_for_request_body(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200914{
915 struct session *sess = s->sess;
916 struct http_txn *txn = s->txn;
917 struct http_msg *msg = &s->txn->req;
918
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100919 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Fauletf76ebe82018-10-24 11:16:22 +0200920
Christopher Fauletf76ebe82018-10-24 11:16:22 +0200921
Christopher Faulet021a8e42021-03-29 10:46:38 +0200922 switch (http_wait_for_msg_body(s, req, s->be->timeout.httpreq, 0)) {
923 case HTTP_RULE_RES_CONT:
924 goto http_end;
925 case HTTP_RULE_RES_YIELD:
926 goto missing_data_or_waiting;
927 case HTTP_RULE_RES_BADREQ:
Willy Tarreau4236f032019-03-05 10:43:32 +0100928 goto return_bad_req;
Christopher Faulet021a8e42021-03-29 10:46:38 +0200929 case HTTP_RULE_RES_ERROR:
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200930 goto return_int_err;
Christopher Faulet021a8e42021-03-29 10:46:38 +0200931 case HTTP_RULE_RES_ABRT:
Christopher Fauletb8a53712019-12-16 11:29:38 +0100932 goto return_prx_cond;
Christopher Faulet021a8e42021-03-29 10:46:38 +0200933 default:
934 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200935 }
936
937 http_end:
938 /* The situation will not evolve, so let's give up on the analysis. */
939 s->logs.tv_request = now; /* update the request timer to reflect full request */
940 req->analysers &= ~an_bit;
941 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100942 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200943 return 1;
944
Christopher Faulet021a8e42021-03-29 10:46:38 +0200945 missing_data_or_waiting:
946 channel_dont_connect(req);
947 DBG_TRACE_DEVEL("waiting for more data",
948 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
949 return 0;
950
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200951 return_int_err:
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200952 txn->status = 500;
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200953 if (!(s->flags & SF_ERR_MASK))
954 s->flags |= SF_ERR_INTERNAL;
Willy Tarreau4781b152021-04-06 13:53:36 +0200955 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100956 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau4781b152021-04-06 13:53:36 +0200957 _HA_ATOMIC_INC(&s->be->be_counters.internal_errors);
William Lallemand36119de2021-03-08 15:26:48 +0100958 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200959 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Faulet021a8e42021-03-29 10:46:38 +0200960 goto return_prx_err;
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200961
Christopher Faulete0768eb2018-10-03 16:38:02 +0200962 return_bad_req: /* let's centralize all bad requests */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200963 txn->status = 400;
Willy Tarreau4781b152021-04-06 13:53:36 +0200964 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req);
William Lallemand36119de2021-03-08 15:26:48 +0100965 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200966 _HA_ATOMIC_INC(&sess->listener->counters->failed_req);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100967 /* fall through */
968
Christopher Faulet021a8e42021-03-29 10:46:38 +0200969 return_prx_err:
Christopher Fauletb8a53712019-12-16 11:29:38 +0100970 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet021a8e42021-03-29 10:46:38 +0200971 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200972
Christopher Faulet021a8e42021-03-29 10:46:38 +0200973 return_prx_cond:
Christopher Faulete0768eb2018-10-03 16:38:02 +0200974 if (!(s->flags & SF_ERR_MASK))
975 s->flags |= SF_ERR_PRXCOND;
976 if (!(s->flags & SF_FINST_MASK))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100977 s->flags |= (msg->msg_state < HTTP_MSG_DATA ? SF_FINST_R : SF_FINST_D);
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200978
Christopher Faulete0768eb2018-10-03 16:38:02 +0200979 req->analysers &= AN_REQ_FLT_END;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100980 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100981 DBG_TRACE_DEVEL("leaving on error",
982 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200983 return 0;
984}
985
986/* This function is an analyser which forwards request body (including chunk
987 * sizes if any). It is called as soon as we must forward, even if we forward
988 * zero byte. The only situation where it must not be called is when we're in
989 * tunnel mode and we want to forward till the close. It's used both to forward
990 * remaining data and to resync after end of body. It expects the msg_state to
991 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
992 * read more data, or 1 once we can go on with next request or end the stream.
993 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
994 * bytes of pending data + the headers if not already done.
995 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200996int http_request_forward_body(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200997{
998 struct session *sess = s->sess;
999 struct http_txn *txn = s->txn;
Christopher Faulet9768c262018-10-22 09:34:31 +02001000 struct http_msg *msg = &txn->req;
1001 struct htx *htx;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001002 short status = 0;
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001003 int ret;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001004
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001005 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001006
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001007 htx = htxbuf(&req->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001008
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001009 if (htx->flags & HTX_FL_PARSING_ERROR)
1010 goto return_bad_req;
1011 if (htx->flags & HTX_FL_PROCESSING_ERROR)
1012 goto return_int_err;
1013
Christopher Faulete0768eb2018-10-03 16:38:02 +02001014 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
1015 ((req->flags & CF_SHUTW) && (req->to_forward || co_data(req)))) {
1016 /* Output closed while we were sending data. We must abort and
1017 * wake the other side up.
Christopher Fauletf506d962021-04-27 10:56:28 +02001018 *
1019 * If we have finished to send the request and the response is
1020 * still in progress, don't catch write error on the request
1021 * side if it is in fact a read error on the server side.
Christopher Faulete0768eb2018-10-03 16:38:02 +02001022 */
Christopher Fauletf506d962021-04-27 10:56:28 +02001023 if (msg->msg_state == HTTP_MSG_DONE && (s->res.flags & CF_READ_ERROR) && s->res.analysers)
1024 return 0;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001025
Olivier Houchard29cac3c2019-07-12 15:48:58 +02001026 /* Don't abort yet if we had L7 retries activated and it
1027 * was a write error, we may recover.
1028 */
1029 if (!(req->flags & (CF_READ_ERROR | CF_READ_TIMEOUT)) &&
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001030 (s->si[1].flags & SI_FL_L7_RETRY)) {
1031 DBG_TRACE_DEVEL("leaving on L7 retry",
1032 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Olivier Houchard29cac3c2019-07-12 15:48:58 +02001033 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001034 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001035 msg->msg_state = HTTP_MSG_ERROR;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001036 http_end_request(s);
1037 http_end_response(s);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001038 DBG_TRACE_DEVEL("leaving on error",
1039 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001040 return 1;
1041 }
1042
1043 /* Note that we don't have to send 100-continue back because we don't
1044 * need the data to complete our job, and it's up to the server to
1045 * decide whether to return 100, 417 or anything else in return of
1046 * an "Expect: 100-continue" header.
1047 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001048 if (msg->msg_state == HTTP_MSG_BODY)
1049 msg->msg_state = HTTP_MSG_DATA;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001050
Christopher Faulete0768eb2018-10-03 16:38:02 +02001051 /* in most states, we should abort in case of early close */
1052 channel_auto_close(req);
1053
1054 if (req->to_forward) {
Christopher Faulet66af0b22019-03-22 14:54:52 +01001055 if (req->to_forward == CHN_INFINITE_FORWARD) {
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001056 if (req->flags & CF_EOI)
1057 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet66af0b22019-03-22 14:54:52 +01001058 }
1059 else {
1060 /* We can't process the buffer's contents yet */
1061 req->flags |= CF_WAKE_WRITE;
1062 goto missing_data_or_waiting;
1063 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001064 }
1065
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001066 if (msg->msg_state >= HTTP_MSG_ENDING)
1067 goto ending;
1068
1069 if (txn->meth == HTTP_METH_CONNECT) {
1070 msg->msg_state = HTTP_MSG_ENDING;
1071 goto ending;
1072 }
1073
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001074 /* Forward input data. We get it by removing all outgoing data not
1075 * forwarded yet from HTX data size. If there are some data filters, we
1076 * let them decide the amount of data to forward.
Christopher Faulet9768c262018-10-22 09:34:31 +02001077 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001078 if (HAS_REQ_DATA_FILTERS(s)) {
1079 ret = flt_http_payload(s, msg, htx->data);
1080 if (ret < 0)
1081 goto return_bad_req;
Christopher Faulet421e7692019-06-13 11:16:45 +02001082 c_adv(req, ret);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001083 }
1084 else {
Christopher Faulet421e7692019-06-13 11:16:45 +02001085 c_adv(req, htx->data - co_data(req));
Christopher Faulet66af0b22019-03-22 14:54:52 +01001086 if (msg->flags & HTTP_MSGF_XFER_LEN)
1087 channel_htx_forward_forever(req, htx);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001088 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001089
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001090 if (htx->data != co_data(req))
1091 goto missing_data_or_waiting;
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001092
Christopher Faulet9768c262018-10-22 09:34:31 +02001093 /* Check if the end-of-message is reached and if so, switch the message
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001094 * in HTTP_MSG_ENDING state. Then if all data was marked to be
1095 * forwarded, set the state to HTTP_MSG_DONE.
Christopher Faulet9768c262018-10-22 09:34:31 +02001096 */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001097 if (!(htx->flags & HTX_FL_EOM))
Christopher Faulet9768c262018-10-22 09:34:31 +02001098 goto missing_data_or_waiting;
1099
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001100 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet9768c262018-10-22 09:34:31 +02001101
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001102 ending:
Christopher Faulet2151cdd2020-07-22 16:34:59 +02001103 req->flags &= ~CF_EXPECT_MORE; /* no more data are expected */
1104
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001105 /* other states, ENDING...TUNNEL */
1106 if (msg->msg_state >= HTTP_MSG_DONE)
1107 goto done;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001108
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001109 if (HAS_REQ_DATA_FILTERS(s)) {
1110 ret = flt_http_end(s, msg);
1111 if (ret <= 0) {
1112 if (!ret)
1113 goto missing_data_or_waiting;
1114 goto return_bad_req;
1115 }
1116 }
1117
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001118 if (txn->meth == HTTP_METH_CONNECT)
1119 msg->msg_state = HTTP_MSG_TUNNEL;
1120 else {
1121 msg->msg_state = HTTP_MSG_DONE;
1122 req->to_forward = 0;
1123 }
1124
1125 done:
1126 /* we don't want to forward closes on DONE except in tunnel mode. */
1127 if (!(txn->flags & TX_CON_WANT_TUN))
1128 channel_dont_close(req);
1129
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001130 http_end_request(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001131 if (!(req->analysers & an_bit)) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001132 http_end_response(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001133 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
1134 if (req->flags & CF_SHUTW) {
1135 /* request errors are most likely due to the
1136 * server aborting the transfer. */
Christopher Faulet93e02d82019-03-08 14:18:50 +01001137 goto return_srv_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001138 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001139 goto return_bad_req;
1140 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001141 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001142 return 1;
1143 }
1144
1145 /* If "option abortonclose" is set on the backend, we want to monitor
1146 * the client's connection and forward any shutdown notification to the
1147 * server, which will decide whether to close or to go on processing the
1148 * request. We only do that in tunnel mode, and not in other modes since
1149 * it can be abused to exhaust source ports. */
Christopher Faulet769d0e92019-03-22 14:23:18 +01001150 if (s->be->options & PR_O_ABRT_CLOSE) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001151 channel_auto_read(req);
Christopher Fauletc41547b2019-07-16 14:32:23 +02001152 if ((req->flags & (CF_SHUTR|CF_READ_NULL)) && !(txn->flags & TX_CON_WANT_TUN))
Christopher Faulete0768eb2018-10-03 16:38:02 +02001153 s->si[1].flags |= SI_FL_NOLINGER;
1154 channel_auto_close(req);
1155 }
1156 else if (s->txn->meth == HTTP_METH_POST) {
1157 /* POST requests may require to read extra CRLF sent by broken
1158 * browsers and which could cause an RST to be sent upon close
1159 * on some systems (eg: Linux). */
1160 channel_auto_read(req);
1161 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001162 DBG_TRACE_DEVEL("waiting for the end of the HTTP txn",
1163 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001164 return 0;
1165
1166 missing_data_or_waiting:
1167 /* stop waiting for data if the input is closed before the end */
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001168 if (msg->msg_state < HTTP_MSG_ENDING && req->flags & CF_SHUTR)
Christopher Faulet93e02d82019-03-08 14:18:50 +01001169 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001170
1171 waiting:
1172 /* waiting for the last bits to leave the buffer */
1173 if (req->flags & CF_SHUTW)
Christopher Faulet93e02d82019-03-08 14:18:50 +01001174 goto return_srv_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001175
1176 /* When TE: chunked is used, we need to get there again to parse remaining
1177 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
1178 * And when content-length is used, we never want to let the possible
1179 * shutdown be forwarded to the other side, as the state machine will
1180 * take care of it once the client responds. It's also important to
1181 * prevent TIME_WAITs from accumulating on the backend side, and for
1182 * HTTP/2 where the last frame comes with a shutdown.
1183 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001184 if (msg->flags & HTTP_MSGF_XFER_LEN)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001185 channel_dont_close(req);
1186
1187 /* We know that more data are expected, but we couldn't send more that
1188 * what we did. So we always set the CF_EXPECT_MORE flag so that the
1189 * system knows it must not set a PUSH on this first part. Interactive
1190 * modes are already handled by the stream sock layer. We must not do
1191 * this in content-length mode because it could present the MSG_MORE
1192 * flag with the last block of forwarded data, which would cause an
1193 * additional delay to be observed by the receiver.
1194 */
Christopher Faulet2151cdd2020-07-22 16:34:59 +02001195 if (HAS_REQ_DATA_FILTERS(s))
Christopher Faulete0768eb2018-10-03 16:38:02 +02001196 req->flags |= CF_EXPECT_MORE;
1197
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001198 DBG_TRACE_DEVEL("waiting for more data to forward",
1199 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001200 return 0;
1201
Christopher Faulet93e02d82019-03-08 14:18:50 +01001202 return_cli_abort:
Willy Tarreau4781b152021-04-06 13:53:36 +02001203 _HA_ATOMIC_INC(&sess->fe->fe_counters.cli_aborts);
1204 _HA_ATOMIC_INC(&s->be->be_counters.cli_aborts);
William Lallemand36119de2021-03-08 15:26:48 +01001205 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001206 _HA_ATOMIC_INC(&sess->listener->counters->cli_aborts);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001207 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001208 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.cli_aborts);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001209 if (!(s->flags & SF_ERR_MASK))
1210 s->flags |= SF_ERR_CLICL;
1211 status = 400;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001212 goto return_prx_cond;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001213
1214 return_srv_abort:
Willy Tarreau4781b152021-04-06 13:53:36 +02001215 _HA_ATOMIC_INC(&sess->fe->fe_counters.srv_aborts);
1216 _HA_ATOMIC_INC(&s->be->be_counters.srv_aborts);
William Lallemand36119de2021-03-08 15:26:48 +01001217 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001218 _HA_ATOMIC_INC(&sess->listener->counters->srv_aborts);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001219 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001220 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.srv_aborts);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001221 if (!(s->flags & SF_ERR_MASK))
1222 s->flags |= SF_ERR_SRVCL;
1223 status = 502;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001224 goto return_prx_cond;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001225
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001226 return_int_err:
1227 if (!(s->flags & SF_ERR_MASK))
1228 s->flags |= SF_ERR_INTERNAL;
Willy Tarreau4781b152021-04-06 13:53:36 +02001229 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
1230 _HA_ATOMIC_INC(&s->be->be_counters.internal_errors);
William Lallemand36119de2021-03-08 15:26:48 +01001231 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001232 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001233 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001234 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.internal_errors);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001235 status = 500;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001236 goto return_prx_cond;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001237
Christopher Faulet93e02d82019-03-08 14:18:50 +01001238 return_bad_req:
Willy Tarreau4781b152021-04-06 13:53:36 +02001239 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req);
William Lallemand36119de2021-03-08 15:26:48 +01001240 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001241 _HA_ATOMIC_INC(&sess->listener->counters->failed_req);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001242 status = 400;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001243 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001244
Christopher Fauletb8a53712019-12-16 11:29:38 +01001245 return_prx_cond:
Christopher Faulet9768c262018-10-22 09:34:31 +02001246 if (txn->status > 0) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001247 /* Note: we don't send any error if some data were already sent */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001248 http_reply_and_close(s, txn->status, NULL);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001249 } else {
Christopher Faulet93e02d82019-03-08 14:18:50 +01001250 txn->status = status;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001251 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001252 }
Christopher Fauletb8a53712019-12-16 11:29:38 +01001253 if (!(s->flags & SF_ERR_MASK))
1254 s->flags |= SF_ERR_PRXCOND;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001255 if (!(s->flags & SF_FINST_MASK))
1256 s->flags |= ((txn->rsp.msg_state < HTTP_MSG_ERROR) ? SF_FINST_H : SF_FINST_D);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001257 DBG_TRACE_DEVEL("leaving on error ",
1258 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001259 return 0;
1260}
1261
Olivier Houcharda254a372019-04-05 15:30:12 +02001262/* Reset the stream and the backend stream_interface to a situation suitable for attemption connection */
1263/* Returns 0 if we can attempt to retry, -1 otherwise */
1264static __inline int do_l7_retry(struct stream *s, struct stream_interface *si)
1265{
Christopher Faulet5bf85852021-05-21 13:46:14 +02001266 struct channel *req, *res;
1267 int co_data;
Olivier Houcharda254a372019-04-05 15:30:12 +02001268
1269 si->conn_retries--;
1270 if (si->conn_retries < 0)
Christopher Faulet043cdb22021-05-26 10:31:06 +02001271 return -1;
Christopher Faulet5b82cc52020-10-12 15:18:50 +02001272
Christopher Faulete763c8c2021-05-05 18:23:59 +02001273 if (objt_server(s->target)) {
1274 if (s->flags & SF_CURR_SESS) {
1275 s->flags &= ~SF_CURR_SESS;
1276 _HA_ATOMIC_DEC(&__objt_server(s->target)->cur_sess);
1277 }
Willy Tarreau4781b152021-04-06 13:53:36 +02001278 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.retries);
Christopher Faulete763c8c2021-05-05 18:23:59 +02001279 }
Willy Tarreau4781b152021-04-06 13:53:36 +02001280 _HA_ATOMIC_INC(&s->be->be_counters.retries);
Willy Tarreau223995e2019-05-04 10:38:31 +02001281
Christopher Faulet5bf85852021-05-21 13:46:14 +02001282 req = &s->req;
1283 res = &s->res;
Olivier Houcharda254a372019-04-05 15:30:12 +02001284 /* Remove any write error from the request, and read error from the response */
1285 req->flags &= ~(CF_WRITE_ERROR | CF_WRITE_TIMEOUT | CF_SHUTW | CF_SHUTW_NOW);
1286 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 +01001287 res->analysers &= AN_RES_FLT_END;
Olivier Houcharda254a372019-04-05 15:30:12 +02001288 si->flags &= ~(SI_FL_ERR | SI_FL_EXP | SI_FL_RXBLK_SHUT);
Christopher Faulet30aa0da2021-05-05 21:05:09 +02001289 si->err_type = SI_ET_NONE;
1290 s->flags &= ~(SF_ERR_MASK | SF_FINST_MASK);
Olivier Houchard4bd58672019-07-12 16:16:59 +02001291 stream_choose_redispatch(s);
Olivier Houcharda254a372019-04-05 15:30:12 +02001292 si->exp = TICK_ETERNITY;
1293 res->rex = TICK_ETERNITY;
1294 res->to_forward = 0;
1295 res->analyse_exp = TICK_ETERNITY;
1296 res->total = 0;
Olivier Houcharda254a372019-04-05 15:30:12 +02001297 si_release_endpoint(&s->si[1]);
Olivier Houcharda254a372019-04-05 15:30:12 +02001298
Christopher Faulet5bf85852021-05-21 13:46:14 +02001299 b_free(&req->buf);
1300 /* Swap the L7 buffer with the channel buffer */
1301 /* We know we stored the co_data as b_data, so get it there */
1302 co_data = b_data(&si->l7_buffer);
1303 b_set_data(&si->l7_buffer, b_size(&si->l7_buffer));
1304 b_xfer(&req->buf, &si->l7_buffer, b_data(&si->l7_buffer));
1305 co_set_data(req, co_data);
Christopher Faulet5b82cc52020-10-12 15:18:50 +02001306
Ilya Shipitsinacf84592021-02-06 22:29:08 +05001307 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 +02001308
Olivier Houcharda254a372019-04-05 15:30:12 +02001309 b_reset(&res->buf);
1310 co_set_data(res, 0);
1311 return 0;
1312}
1313
Christopher Faulete0768eb2018-10-03 16:38:02 +02001314/* This stream analyser waits for a complete HTTP response. It returns 1 if the
1315 * processing can continue on next analysers, or zero if it either needs more
1316 * data or wants to immediately abort the response (eg: timeout, error, ...). It
1317 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->res.analysers
1318 * when it has nothing left to do, and may remove any analyser when it wants to
1319 * abort.
1320 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001321int http_wait_for_response(struct stream *s, struct channel *rep, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001322{
Christopher Faulet9768c262018-10-22 09:34:31 +02001323 /*
1324 * We will analyze a complete HTTP response to check the its syntax.
1325 *
1326 * Once the start line and all headers are received, we may perform a
1327 * capture of the error (if any), and we will set a few fields. We also
1328 * logging and finally headers capture.
1329 */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001330 struct session *sess = s->sess;
1331 struct http_txn *txn = s->txn;
1332 struct http_msg *msg = &txn->rsp;
Christopher Faulet9768c262018-10-22 09:34:31 +02001333 struct htx *htx;
Olivier Houcharda254a372019-04-05 15:30:12 +02001334 struct stream_interface *si_b = &s->si[1];
Christopher Faulet61608322018-11-23 16:23:45 +01001335 struct connection *srv_conn;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001336 struct htx_sl *sl;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001337 int n;
1338
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001339 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001340
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001341 htx = htxbuf(&rep->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001342
Willy Tarreau4236f032019-03-05 10:43:32 +01001343 /* Parsing errors are caught here */
1344 if (htx->flags & HTX_FL_PARSING_ERROR)
1345 goto return_bad_res;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001346 if (htx->flags & HTX_FL_PROCESSING_ERROR)
1347 goto return_int_err;
Willy Tarreau4236f032019-03-05 10:43:32 +01001348
Christopher Faulete0768eb2018-10-03 16:38:02 +02001349 /*
1350 * Now we quickly check if we have found a full valid response.
1351 * If not so, we check the FD and buffer states before leaving.
1352 * A full response is indicated by the fact that we have seen
1353 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
1354 * responses are checked first.
1355 *
1356 * Depending on whether the client is still there or not, we
1357 * may send an error response back or not. Note that normally
1358 * we should only check for HTTP status there, and check I/O
1359 * errors somewhere else.
1360 */
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001361 next_one:
Christopher Faulet29f17582019-05-23 11:03:26 +02001362 if (unlikely(htx_is_empty(htx) || htx->first == -1)) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001363 /* 1: have we encountered a read error ? */
1364 if (rep->flags & CF_READ_ERROR) {
Olivier Houchard865d8392019-05-03 22:46:27 +02001365 struct connection *conn = NULL;
1366
Olivier Houchard865d8392019-05-03 22:46:27 +02001367 if (objt_cs(s->si[1].end))
Willy Tarreau66182592021-12-06 07:01:02 +00001368 conn = __objt_cs(s->si[1].end)->conn;
Olivier Houchard865d8392019-05-03 22:46:27 +02001369
Christopher Fauletb1875342021-05-26 12:15:37 +02001370 /* Perform a L7 retry because server refuses the early data. */
1371 if ((si_b->flags & SI_FL_L7_RETRY) &&
1372 (s->be->retry_type & PR_RE_EARLY_ERROR) &&
1373 conn && conn->err_code == CO_ER_SSL_EARLY_FAILED &&
1374 do_l7_retry(s, si_b) == 0) {
1375 DBG_TRACE_DEVEL("leaving on L7 retry",
1376 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
1377 return 0;
1378 }
1379
Olivier Houchard6db16992019-05-17 15:40:49 +02001380 if (txn->flags & TX_NOT_FIRST)
1381 goto abort_keep_alive;
1382
Willy Tarreau4781b152021-04-06 13:53:36 +02001383 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001384 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001385 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001386 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001387 }
1388
Christopher Fauletb1875342021-05-26 12:15:37 +02001389 /* if the server refused the early data, just send a 425 */
1390 if (conn && conn->err_code == CO_ER_SSL_EARLY_FAILED)
Olivier Houchard865d8392019-05-03 22:46:27 +02001391 txn->status = 425;
Christopher Fauletb1875342021-05-26 12:15:37 +02001392 else {
1393 txn->status = 502;
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001394 stream_inc_http_fail_ctr(s);
Christopher Fauletb1875342021-05-26 12:15:37 +02001395 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001396
1397 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001398 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001399
1400 if (!(s->flags & SF_ERR_MASK))
1401 s->flags |= SF_ERR_SRVCL;
1402 if (!(s->flags & SF_FINST_MASK))
1403 s->flags |= SF_FINST_H;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001404 DBG_TRACE_DEVEL("leaving on error",
1405 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001406 return 0;
1407 }
1408
Christopher Faulet9768c262018-10-22 09:34:31 +02001409 /* 2: read timeout : return a 504 to the client. */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001410 else if (rep->flags & CF_READ_TIMEOUT) {
Olivier Houcharda254a372019-04-05 15:30:12 +02001411 if ((si_b->flags & SI_FL_L7_RETRY) &&
1412 (s->be->retry_type & PR_RE_TIMEOUT)) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001413 if (co_data(rep) || do_l7_retry(s, si_b) == 0) {
1414 DBG_TRACE_DEVEL("leaving on L7 retry",
1415 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharda254a372019-04-05 15:30:12 +02001416 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001417 }
Olivier Houcharda254a372019-04-05 15:30:12 +02001418 }
Willy Tarreau4781b152021-04-06 13:53:36 +02001419 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001420 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001421 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001422 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001423 }
1424
Christopher Faulete0768eb2018-10-03 16:38:02 +02001425 txn->status = 504;
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001426 stream_inc_http_fail_ctr(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001427 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001428 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001429
1430 if (!(s->flags & SF_ERR_MASK))
1431 s->flags |= SF_ERR_SRVTO;
1432 if (!(s->flags & SF_FINST_MASK))
1433 s->flags |= SF_FINST_H;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001434 DBG_TRACE_DEVEL("leaving on error",
1435 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001436 return 0;
1437 }
1438
Christopher Faulet9768c262018-10-22 09:34:31 +02001439 /* 3: client abort with an abortonclose */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001440 else if ((rep->flags & CF_SHUTR) && ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001441 _HA_ATOMIC_INC(&sess->fe->fe_counters.cli_aborts);
1442 _HA_ATOMIC_INC(&s->be->be_counters.cli_aborts);
William Lallemand36119de2021-03-08 15:26:48 +01001443 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001444 _HA_ATOMIC_INC(&sess->listener->counters->cli_aborts);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001445 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001446 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.cli_aborts);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001447
Christopher Faulete0768eb2018-10-03 16:38:02 +02001448 txn->status = 400;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001449 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001450
1451 if (!(s->flags & SF_ERR_MASK))
1452 s->flags |= SF_ERR_CLICL;
1453 if (!(s->flags & SF_FINST_MASK))
1454 s->flags |= SF_FINST_H;
1455
1456 /* process_stream() will take care of the error */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001457 DBG_TRACE_DEVEL("leaving on error",
1458 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001459 return 0;
1460 }
1461
Christopher Faulet9768c262018-10-22 09:34:31 +02001462 /* 4: close from server, capture the response if the server has started to respond */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001463 else if (rep->flags & CF_SHUTR) {
Olivier Houcharda254a372019-04-05 15:30:12 +02001464 if ((si_b->flags & SI_FL_L7_RETRY) &&
1465 (s->be->retry_type & PR_RE_DISCONNECTED)) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001466 if (co_data(rep) || do_l7_retry(s, si_b) == 0) {
1467 DBG_TRACE_DEVEL("leaving on L7 retry",
1468 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharda254a372019-04-05 15:30:12 +02001469 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001470 }
Olivier Houcharda254a372019-04-05 15:30:12 +02001471 }
1472
Olivier Houchard6db16992019-05-17 15:40:49 +02001473 if (txn->flags & TX_NOT_FIRST)
1474 goto abort_keep_alive;
1475
Willy Tarreau4781b152021-04-06 13:53:36 +02001476 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001477 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001478 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001479 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001480 }
1481
Christopher Faulete0768eb2018-10-03 16:38:02 +02001482 txn->status = 502;
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001483 stream_inc_http_fail_ctr(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001484 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001485 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001486
1487 if (!(s->flags & SF_ERR_MASK))
1488 s->flags |= SF_ERR_SRVCL;
1489 if (!(s->flags & SF_FINST_MASK))
1490 s->flags |= SF_FINST_H;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001491 DBG_TRACE_DEVEL("leaving on error",
1492 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001493 return 0;
1494 }
1495
Christopher Faulet9768c262018-10-22 09:34:31 +02001496 /* 5: write error to client (we don't send any message then) */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001497 else if (rep->flags & CF_WRITE_ERROR) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001498 if (txn->flags & TX_NOT_FIRST)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001499 goto abort_keep_alive;
1500
Willy Tarreau4781b152021-04-06 13:53:36 +02001501 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001502 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001503 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001504 rep->analysers &= AN_RES_FLT_END;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001505
1506 if (!(s->flags & SF_ERR_MASK))
1507 s->flags |= SF_ERR_CLICL;
1508 if (!(s->flags & SF_FINST_MASK))
1509 s->flags |= SF_FINST_H;
1510
1511 /* process_stream() will take care of the error */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001512 DBG_TRACE_DEVEL("leaving on error",
1513 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001514 return 0;
1515 }
1516
1517 channel_dont_close(rep);
1518 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001519 DBG_TRACE_DEVEL("waiting for more data",
1520 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001521 return 0;
1522 }
1523
1524 /* More interesting part now : we know that we have a complete
1525 * response which at least looks like HTTP. We have an indicator
1526 * of each header's length, so we can parse them quickly.
1527 */
Christopher Faulet29f17582019-05-23 11:03:26 +02001528 BUG_ON(htx_get_first_type(htx) != HTX_BLK_RES_SL);
Christopher Faulet297fbb42019-05-13 14:41:27 +02001529 sl = http_get_stline(htx);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001530
Christopher Faulet0f66d552021-05-26 13:14:39 +02001531 /* Perform a L7 retry because of the status code */
1532 if ((si_b->flags & SI_FL_L7_RETRY) &&
1533 l7_status_match(s->be, sl->info.res.status) &&
1534 do_l7_retry(s, si_b) == 0) {
1535 DBG_TRACE_DEVEL("leaving on L7 retry", STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
1536 return 0;
1537 }
1538
1539 /* Now, L7 buffer is useless, it can be released */
1540 b_free(&s->si[1].l7_buffer);
1541
1542 msg->msg_state = HTTP_MSG_BODY;
1543
1544
Christopher Faulet9768c262018-10-22 09:34:31 +02001545 /* 0: we might have to print this header in debug mode */
1546 if (unlikely((global.mode & MODE_DEBUG) &&
1547 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
1548 int32_t pos;
1549
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001550 http_debug_stline("srvrep", s, sl);
Christopher Faulet9768c262018-10-22 09:34:31 +02001551
Christopher Fauleta3f15502019-05-13 15:27:23 +02001552 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001553 struct htx_blk *blk = htx_get_blk(htx, pos);
1554 enum htx_blk_type type = htx_get_blk_type(blk);
1555
1556 if (type == HTX_BLK_EOH)
1557 break;
1558 if (type != HTX_BLK_HDR)
1559 continue;
1560
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001561 http_debug_hdr("srvhdr", s,
1562 htx_get_blk_name(htx, blk),
1563 htx_get_blk_value(htx, blk));
Christopher Faulet9768c262018-10-22 09:34:31 +02001564 }
1565 }
1566
Christopher Faulet03599112018-11-27 11:21:21 +01001567 /* 1: get the status code and the version. Also set HTTP flags */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001568 txn->status = sl->info.res.status;
Christopher Faulet03599112018-11-27 11:21:21 +01001569 if (sl->flags & HTX_SL_F_VER_11)
Christopher Faulet9768c262018-10-22 09:34:31 +02001570 msg->flags |= HTTP_MSGF_VER_11;
Christopher Faulet03599112018-11-27 11:21:21 +01001571 if (sl->flags & HTX_SL_F_XFER_LEN) {
1572 msg->flags |= HTTP_MSGF_XFER_LEN;
Christopher Faulet2a408542020-11-20 14:22:37 +01001573 if (sl->flags & HTX_SL_F_CLEN)
1574 msg->flags |= HTTP_MSGF_CNT_LEN;
1575 else if (sl->flags & HTX_SL_F_CHNK)
1576 msg->flags |= HTTP_MSGF_TE_CHNK;
Christopher Faulet03599112018-11-27 11:21:21 +01001577 }
Christopher Faulet2a408542020-11-20 14:22:37 +01001578 if (sl->flags & HTX_SL_F_BODYLESS)
1579 msg->flags |= HTTP_MSGF_BODYLESS;
Christopher Faulet576c3582021-01-08 15:53:01 +01001580 if (sl->flags & HTX_SL_F_CONN_UPG)
1581 msg->flags |= HTTP_MSGF_CONN_UPG;
Christopher Faulet9768c262018-10-22 09:34:31 +02001582
1583 n = txn->status / 100;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001584 if (n < 1 || n > 5)
1585 n = 0;
Christopher Faulet9768c262018-10-22 09:34:31 +02001586
Christopher Faulete0768eb2018-10-03 16:38:02 +02001587 /* when the client triggers a 4xx from the server, it's most often due
1588 * to a missing object or permission. These events should be tracked
1589 * because if they happen often, it may indicate a brute force or a
1590 * vulnerability scan.
1591 */
1592 if (n == 4)
1593 stream_inc_http_err_ctr(s);
1594
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001595 if (n == 5 && txn->status != 501 && txn->status != 505)
1596 stream_inc_http_fail_ctr(s);
1597
Marcin Deranek3c27dda2020-05-15 18:32:51 +02001598 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001599 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.p.http.rsp[n]);
1600 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.p.http.cum_req);
Marcin Deranek3c27dda2020-05-15 18:32:51 +02001601 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001602
Christopher Faulete0768eb2018-10-03 16:38:02 +02001603 /* Adjust server's health based on status code. Note: status codes 501
1604 * and 505 are triggered on demand by client request, so we must not
1605 * count them as server failures.
1606 */
1607 if (objt_server(s->target)) {
1608 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001609 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_OK);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001610 else
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001611 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_STS);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001612 }
1613
1614 /*
1615 * We may be facing a 100-continue response, or any other informational
1616 * 1xx response which is non-final, in which case this is not the right
1617 * response, and we're waiting for the next one. Let's allow this response
1618 * to go to the client and wait for the next one. There's an exception for
1619 * 101 which is used later in the code to switch protocols.
1620 */
1621 if (txn->status < 200 &&
1622 (txn->status == 100 || txn->status >= 102)) {
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001623 FLT_STRM_CB(s, flt_http_reset(s, msg));
Christopher Faulet421e7692019-06-13 11:16:45 +02001624 htx->first = channel_htx_fwd_headers(rep, htx);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001625 msg->msg_state = HTTP_MSG_RPBEFORE;
Christopher Faulet3499f622019-09-03 15:23:54 +02001626 msg->flags = 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001627 txn->status = 0;
1628 s->logs.t_data = -1; /* was not a response yet */
Christopher Faulet7d518452020-08-31 11:07:07 +02001629 rep->flags |= CF_SEND_DONTWAIT; /* Send ASAP informational messages */
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001630 goto next_one;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001631 }
1632
Christopher Faulet6e6c7b12021-01-08 16:02:05 +01001633 /* A 101-switching-protocols must contains a Connection header with the
1634 * "upgrade" option and the request too. It means both are agree to
1635 * upgrade. It is not so strict because there is no test on the Upgrade
1636 * header content. But it is probably stronger enough for now.
1637 */
1638 if (txn->status == 101 &&
1639 (!(txn->req.flags & HTTP_MSGF_CONN_UPG) || !(txn->rsp.flags & HTTP_MSGF_CONN_UPG)))
1640 goto return_bad_res;
1641
Christopher Faulete0768eb2018-10-03 16:38:02 +02001642 /*
1643 * 2: check for cacheability.
1644 */
1645
1646 switch (txn->status) {
1647 case 200:
1648 case 203:
1649 case 204:
1650 case 206:
1651 case 300:
1652 case 301:
1653 case 404:
1654 case 405:
1655 case 410:
1656 case 414:
1657 case 501:
1658 break;
1659 default:
1660 /* RFC7231#6.1:
1661 * Responses with status codes that are defined as
1662 * cacheable by default (e.g., 200, 203, 204, 206,
1663 * 300, 301, 404, 405, 410, 414, and 501 in this
1664 * specification) can be reused by a cache with
1665 * heuristic expiration unless otherwise indicated
1666 * by the method definition or explicit cache
1667 * controls [RFC7234]; all other status codes are
1668 * not cacheable by default.
1669 */
1670 txn->flags &= ~(TX_CACHEABLE | TX_CACHE_COOK);
1671 break;
1672 }
1673
1674 /*
1675 * 3: we may need to capture headers
1676 */
1677 s->logs.logwait &= ~LW_RESP;
1678 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->res_cap))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001679 http_capture_headers(htx, s->res_cap, sess->fe->rsp_cap);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001680
Christopher Faulet9768c262018-10-22 09:34:31 +02001681 /* Skip parsing if no content length is possible. */
Christopher Fauletc75668e2020-12-07 18:10:32 +01001682 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status >= 200 && txn->status < 300) ||
Christopher Faulete0768eb2018-10-03 16:38:02 +02001683 txn->status == 101)) {
1684 /* Either we've established an explicit tunnel, or we're
1685 * switching the protocol. In both cases, we're very unlikely
1686 * to understand the next protocols. We have to switch to tunnel
1687 * mode, so that we transfer the request and responses then let
1688 * this protocol pass unmodified. When we later implement specific
1689 * parsers for such protocols, we'll want to check the Upgrade
1690 * header which contains information about that protocol for
1691 * responses with status 101 (eg: see RFC2817 about TLS).
1692 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02001693 txn->flags |= TX_CON_WANT_TUN;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001694 }
1695
Christopher Faulet61608322018-11-23 16:23:45 +01001696 /* check for NTML authentication headers in 401 (WWW-Authenticate) and
1697 * 407 (Proxy-Authenticate) responses and set the connection to private
1698 */
1699 srv_conn = cs_conn(objt_cs(s->si[1].end));
1700 if (srv_conn) {
1701 struct ist hdr;
1702 struct http_hdr_ctx ctx;
1703
1704 if (txn->status == 401)
1705 hdr = ist("WWW-Authenticate");
1706 else if (txn->status == 407)
1707 hdr = ist("Proxy-Authenticate");
1708 else
1709 goto end;
1710
1711 ctx.blk = NULL;
1712 while (http_find_header(htx, hdr, &ctx, 0)) {
Willy Tarreauf1dcced2020-05-07 19:27:02 +02001713 /* If www-authenticate contains "Negotiate", "Nego2", or "NTLM",
1714 * possibly followed by blanks and a base64 string, the connection
1715 * is private. Since it's a mess to deal with, we only check for
1716 * values starting with "NTLM" or "Nego". Note that often multiple
1717 * headers are sent by the server there.
1718 */
1719 if ((ctx.value.len >= 4 && strncasecmp(ctx.value.ptr, "Nego", 4) == 0) ||
Willy Tarreau49a1d282020-05-07 19:10:15 +02001720 (ctx.value.len >= 4 && strncasecmp(ctx.value.ptr, "NTLM", 4) == 0)) {
Olivier Houchard250031e2019-05-29 15:01:50 +02001721 sess->flags |= SESS_FL_PREFER_LAST;
Christopher Faulet08016ab2020-07-01 16:10:06 +02001722 conn_set_owner(srv_conn, sess, NULL);
Christopher Faulet21ddc742020-07-01 15:26:14 +02001723 conn_set_private(srv_conn);
Ilya Shipitsin6b79f382020-07-23 00:32:55 +05001724 /* If it fail now, the same will be done in mux->detach() callback */
Christopher Faulet08016ab2020-07-01 16:10:06 +02001725 session_add_conn(srv_conn->owner, srv_conn, srv_conn->target);
Willy Tarreauf1dcced2020-05-07 19:27:02 +02001726 break;
Olivier Houchard250031e2019-05-29 15:01:50 +02001727 }
Christopher Faulet61608322018-11-23 16:23:45 +01001728 }
1729 }
1730
1731 end:
Christopher Faulete0768eb2018-10-03 16:38:02 +02001732 /* we want to have the response time before we start processing it */
1733 s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now);
1734
1735 /* end of job, return OK */
1736 rep->analysers &= ~an_bit;
1737 rep->analyse_exp = TICK_ETERNITY;
1738 channel_auto_close(rep);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001739 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001740 return 1;
1741
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001742 return_int_err:
Willy Tarreau4781b152021-04-06 13:53:36 +02001743 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
1744 _HA_ATOMIC_INC(&s->be->be_counters.internal_errors);
William Lallemand36119de2021-03-08 15:26:48 +01001745 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001746 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Fauletb8a53712019-12-16 11:29:38 +01001747 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001748 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.internal_errors);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001749 txn->status = 500;
1750 if (!(s->flags & SF_ERR_MASK))
1751 s->flags |= SF_ERR_INTERNAL;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001752 goto return_prx_cond;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001753
1754 return_bad_res:
Willy Tarreau4781b152021-04-06 13:53:36 +02001755 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Faulet47365272018-10-31 17:40:50 +01001756 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001757 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001758 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Christopher Faulet47365272018-10-31 17:40:50 +01001759 }
Olivier Houcharde3249a92019-05-03 23:01:47 +02001760 if ((s->be->retry_type & PR_RE_JUNK_REQUEST) &&
Olivier Houchardad26d8d2019-05-10 17:48:28 +02001761 (si_b->flags & SI_FL_L7_RETRY) &&
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001762 do_l7_retry(s, si_b) == 0) {
1763 DBG_TRACE_DEVEL("leaving on L7 retry",
1764 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharde3249a92019-05-03 23:01:47 +02001765 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001766 }
Christopher Faulet47365272018-10-31 17:40:50 +01001767 txn->status = 502;
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001768 stream_inc_http_fail_ctr(s);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001769 /* fall through */
1770
Christopher Fauletb8a53712019-12-16 11:29:38 +01001771 return_prx_cond:
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001772 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet47365272018-10-31 17:40:50 +01001773
1774 if (!(s->flags & SF_ERR_MASK))
1775 s->flags |= SF_ERR_PRXCOND;
1776 if (!(s->flags & SF_FINST_MASK))
1777 s->flags |= SF_FINST_H;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001778
1779 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001780 DBG_TRACE_DEVEL("leaving on error",
1781 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulet47365272018-10-31 17:40:50 +01001782 return 0;
1783
Christopher Faulete0768eb2018-10-03 16:38:02 +02001784 abort_keep_alive:
1785 /* A keep-alive request to the server failed on a network error.
1786 * The client is required to retry. We need to close without returning
1787 * any other information so that the client retries.
1788 */
1789 txn->status = 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001790 s->logs.logwait = 0;
1791 s->logs.level = 0;
1792 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001793 http_reply_and_close(s, txn->status, NULL);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001794 DBG_TRACE_DEVEL("leaving by closing K/A connection",
1795 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001796 return 0;
1797}
1798
1799/* This function performs all the processing enabled for the current response.
1800 * It normally returns 1 unless it wants to break. It relies on buffers flags,
1801 * and updates s->res.analysers. It might make sense to explode it into several
1802 * other functions. It works like process_request (see indications above).
1803 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001804int http_process_res_common(struct stream *s, struct channel *rep, int an_bit, struct proxy *px)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001805{
1806 struct session *sess = s->sess;
1807 struct http_txn *txn = s->txn;
1808 struct http_msg *msg = &txn->rsp;
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001809 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001810 struct proxy *cur_proxy;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001811 enum rule_result ret = HTTP_RULE_RES_CONT;
1812
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001813 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
1814 return 0;
Christopher Faulet9768c262018-10-22 09:34:31 +02001815
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001816 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001817
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001818 htx = htxbuf(&rep->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001819
1820 /* The stats applet needs to adjust the Connection header but we don't
1821 * apply any filter there.
1822 */
1823 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
1824 rep->analysers &= ~an_bit;
1825 rep->analyse_exp = TICK_ETERNITY;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001826 goto end;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001827 }
1828
1829 /*
1830 * We will have to evaluate the filters.
1831 * As opposed to version 1.2, now they will be evaluated in the
1832 * filters order and not in the header order. This means that
1833 * each filter has to be validated among all headers.
1834 *
1835 * Filters are tried with ->be first, then with ->fe if it is
1836 * different from ->be.
1837 *
1838 * Maybe we are in resume condiion. In this case I choose the
1839 * "struct proxy" which contains the rule list matching the resume
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05001840 * pointer. If none of these "struct proxy" match, I initialise
Christopher Faulete0768eb2018-10-03 16:38:02 +02001841 * the process with the first one.
1842 *
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05001843 * In fact, I check only correspondence between the current list
Christopher Faulete0768eb2018-10-03 16:38:02 +02001844 * pointer and the ->fe rule list. If it doesn't match, I initialize
1845 * the loop with the ->be.
1846 */
1847 if (s->current_rule_list == &sess->fe->http_res_rules)
1848 cur_proxy = sess->fe;
1849 else
1850 cur_proxy = s->be;
1851 while (1) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001852 /* evaluate http-response rules */
Christopher Fauletb4c4a972021-11-09 16:33:25 +01001853 if (ret == HTTP_RULE_RES_CONT || ret == HTTP_RULE_RES_STOP) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001854 ret = http_res_get_intercept_rule(cur_proxy, &cur_proxy->http_res_rules, s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001855
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001856 switch (ret) {
1857 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
1858 goto return_prx_yield;
1859
1860 case HTTP_RULE_RES_CONT:
1861 case HTTP_RULE_RES_STOP: /* nothing to do */
1862 break;
1863
1864 case HTTP_RULE_RES_DENY: /* deny or tarpit */
1865 goto deny;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001866
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001867 case HTTP_RULE_RES_ABRT: /* abort request, response already sent */
1868 goto return_prx_cond;
1869
1870 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
Christopher Fauletb8a53712019-12-16 11:29:38 +01001871 goto done;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001872
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001873 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
1874 goto return_bad_res;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001875
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001876 case HTTP_RULE_RES_ERROR: /* failed with a bad request */
1877 goto return_int_err;
1878 }
1879
1880 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001881
Christopher Faulete0768eb2018-10-03 16:38:02 +02001882 /* check whether we're already working on the frontend */
1883 if (cur_proxy == sess->fe)
1884 break;
1885 cur_proxy = sess->fe;
1886 }
1887
Christopher Faulete0768eb2018-10-03 16:38:02 +02001888 /* OK that's all we can do for 1xx responses */
1889 if (unlikely(txn->status < 200 && txn->status != 101))
Christopher Fauletf2824e62018-10-01 12:12:37 +02001890 goto end;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001891
1892 /*
1893 * Now check for a server cookie.
1894 */
1895 if (s->be->cookie_name || sess->fe->capture_name || (s->be->options & PR_O_CHK_CACHE))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001896 http_manage_server_side_cookies(s, rep);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001897
1898 /*
1899 * Check for cache-control or pragma headers if required.
1900 */
1901 if ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001902 http_check_response_for_cacheability(s, rep);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001903
1904 /*
1905 * Add server cookie in the response if needed
1906 */
1907 if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) &&
1908 !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) &&
1909 (!(s->flags & SF_DIRECT) ||
1910 ((s->be->cookie_maxidle || txn->cookie_last_date) &&
1911 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
1912 (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
1913 (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
1914 (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
1915 !(s->flags & SF_IGNORE_PRST)) {
1916 /* the server is known, it's not the one the client requested, or the
1917 * cookie's last seen date needs to be refreshed. We have to
1918 * insert a set-cookie here, except if we want to insert only on POST
1919 * requests and this one isn't. Note that servers which don't have cookies
1920 * (eg: some backup servers) will return a full cookie removal request.
1921 */
Willy Tarreau66182592021-12-06 07:01:02 +00001922 if (!__objt_server(s->target)->cookie) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001923 chunk_printf(&trash,
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001924 "%s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
Christopher Faulete0768eb2018-10-03 16:38:02 +02001925 s->be->cookie_name);
1926 }
1927 else {
Willy Tarreau66182592021-12-06 07:01:02 +00001928 chunk_printf(&trash, "%s=%s", s->be->cookie_name, __objt_server(s->target)->cookie);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001929
1930 if (s->be->cookie_maxidle || s->be->cookie_maxlife) {
1931 /* emit last_date, which is mandatory */
1932 trash.area[trash.data++] = COOKIE_DELIM_DATE;
1933 s30tob64((date.tv_sec+3) >> 2,
1934 trash.area + trash.data);
1935 trash.data += 5;
1936
1937 if (s->be->cookie_maxlife) {
1938 /* emit first_date, which is either the original one or
1939 * the current date.
1940 */
1941 trash.area[trash.data++] = COOKIE_DELIM_DATE;
1942 s30tob64(txn->cookie_first_date ?
1943 txn->cookie_first_date >> 2 :
1944 (date.tv_sec+3) >> 2,
1945 trash.area + trash.data);
1946 trash.data += 5;
1947 }
1948 }
1949 chunk_appendf(&trash, "; path=/");
1950 }
1951
1952 if (s->be->cookie_domain)
1953 chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain);
1954
1955 if (s->be->ck_opts & PR_CK_HTTPONLY)
1956 chunk_appendf(&trash, "; HttpOnly");
1957
1958 if (s->be->ck_opts & PR_CK_SECURE)
1959 chunk_appendf(&trash, "; Secure");
1960
Christopher Faulet2f533902020-01-21 11:06:48 +01001961 if (s->be->cookie_attrs)
1962 chunk_appendf(&trash, "; %s", s->be->cookie_attrs);
1963
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001964 if (unlikely(!http_add_header(htx, ist("Set-Cookie"), ist2(trash.area, trash.data))))
Christopher Fauletb8a53712019-12-16 11:29:38 +01001965 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001966
1967 txn->flags &= ~TX_SCK_MASK;
1968 if (__objt_server(s->target)->cookie && (s->flags & SF_DIRECT))
1969 /* the server did not change, only the date was updated */
1970 txn->flags |= TX_SCK_UPDATED;
1971 else
1972 txn->flags |= TX_SCK_INSERTED;
1973
1974 /* Here, we will tell an eventual cache on the client side that we don't
1975 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
1976 * Some caches understand the correct form: 'no-cache="set-cookie"', but
1977 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
1978 */
1979 if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
1980
1981 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
1982
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001983 if (unlikely(!http_add_header(htx, ist("Cache-control"), ist("private"))))
Christopher Fauletb8a53712019-12-16 11:29:38 +01001984 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001985 }
1986 }
1987
1988 /*
1989 * Check if result will be cacheable with a cookie.
1990 * We'll block the response if security checks have caught
1991 * nasty things such as a cacheable cookie.
1992 */
1993 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
1994 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
1995 (s->be->options & PR_O_CHK_CACHE)) {
1996 /* we're in presence of a cacheable response containing
1997 * a set-cookie header. We'll block it as requested by
1998 * the 'checkcache' option, and send an alert.
1999 */
Christopher Faulete0768eb2018-10-03 16:38:02 +02002000 ha_alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreau66182592021-12-06 07:01:02 +00002001 s->be->id, objt_server(s->target) ? __objt_server(s->target)->id : "<dispatch>");
Christopher Faulete0768eb2018-10-03 16:38:02 +02002002 send_log(s->be, LOG_ALERT,
2003 "Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreau66182592021-12-06 07:01:02 +00002004 s->be->id, objt_server(s->target) ? __objt_server(s->target)->id : "<dispatch>");
Christopher Fauletb8a53712019-12-16 11:29:38 +01002005 goto deny;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002006 }
2007
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002008 end:
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002009 /*
2010 * Evaluate after-response rules before forwarding the response. rules
2011 * from the backend are evaluated first, then one from the frontend if
2012 * it differs.
2013 */
2014 if (!http_eval_after_res_rules(s))
2015 goto return_int_err;
2016
Christopher Fauletc2ac5e42021-03-08 18:20:09 +01002017 /* Filter the response headers if there are filters attached to the
2018 * stream.
2019 */
2020 if (HAS_FILTERS(s))
2021 rep->analysers |= AN_RES_FLT_HTTP_HDRS;
2022
Christopher Faulete0768eb2018-10-03 16:38:02 +02002023 /* Always enter in the body analyzer */
2024 rep->analysers &= ~AN_RES_FLT_XFER_DATA;
2025 rep->analysers |= AN_RES_HTTP_XFER_BODY;
2026
2027 /* if the user wants to log as soon as possible, without counting
2028 * bytes from the server, then this is the right moment. We have
2029 * to temporarily assign bytes_out to log what we currently have.
2030 */
2031 if (!LIST_ISEMPTY(&sess->fe->logformat) && !(s->logs.logwait & LW_BYTES)) {
2032 s->logs.t_close = s->logs.t_data; /* to get a valid end date */
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002033 s->logs.bytes_out = htx->data;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002034 s->do_log(s);
2035 s->logs.bytes_out = 0;
2036 }
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002037
Christopher Fauletb8a53712019-12-16 11:29:38 +01002038 done:
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002039 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002040 rep->analysers &= ~an_bit;
2041 rep->analyse_exp = TICK_ETERNITY;
2042 return 1;
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002043
Christopher Fauletb8a53712019-12-16 11:29:38 +01002044 deny:
Willy Tarreau4781b152021-04-06 13:53:36 +02002045 _HA_ATOMIC_INC(&sess->fe->fe_counters.denied_resp);
2046 _HA_ATOMIC_INC(&s->be->be_counters.denied_resp);
William Lallemand36119de2021-03-08 15:26:48 +01002047 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02002048 _HA_ATOMIC_INC(&sess->listener->counters->denied_resp);
Christopher Fauleta08546b2019-12-16 16:07:34 +01002049 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02002050 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.denied_resp);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002051 goto return_prx_err;
2052
2053 return_int_err:
2054 txn->status = 500;
2055 if (!(s->flags & SF_ERR_MASK))
2056 s->flags |= SF_ERR_INTERNAL;
Willy Tarreau4781b152021-04-06 13:53:36 +02002057 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
2058 _HA_ATOMIC_INC(&s->be->be_counters.internal_errors);
Dragan Dosen84426cd2021-09-21 13:02:09 +02002059 if (sess->listener && sess->listener->counters)
2060 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Fauletcff0f732019-12-16 16:13:44 +01002061 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02002062 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.internal_errors);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002063 goto return_prx_err;
2064
2065 return_bad_res:
2066 txn->status = 502;
Willy Tarreau826f3ab2021-02-10 12:07:15 +01002067 stream_inc_http_fail_ctr(s);
Willy Tarreau4781b152021-04-06 13:53:36 +02002068 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Fauleta20a6532020-02-05 10:16:41 +01002069 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02002070 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Christopher Fauleta20a6532020-02-05 10:16:41 +01002071 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_RSP);
2072 }
Christopher Fauletb8a53712019-12-16 11:29:38 +01002073 /* fall through */
2074
2075 return_prx_err:
2076 http_reply_and_close(s, txn->status, http_error_message(s));
2077 /* fall through */
2078
2079 return_prx_cond:
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002080 s->logs.t_data = -1; /* was not a valid response */
2081 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002082
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002083 if (!(s->flags & SF_ERR_MASK))
2084 s->flags |= SF_ERR_PRXCOND;
2085 if (!(s->flags & SF_FINST_MASK))
2086 s->flags |= SF_FINST_H;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002087
Christopher Faulete58c0002020-03-02 16:21:01 +01002088 rep->analysers &= AN_RES_FLT_END;
2089 s->req.analysers &= AN_REQ_FLT_END;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002090 rep->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002091 DBG_TRACE_DEVEL("leaving on error",
2092 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002093 return 0;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002094
2095 return_prx_yield:
2096 channel_dont_close(rep);
2097 DBG_TRACE_DEVEL("waiting for more data",
2098 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
2099 return 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002100}
2101
2102/* This function is an analyser which forwards response body (including chunk
2103 * sizes if any). It is called as soon as we must forward, even if we forward
2104 * zero byte. The only situation where it must not be called is when we're in
2105 * tunnel mode and we want to forward till the close. It's used both to forward
2106 * remaining data and to resync after end of body. It expects the msg_state to
2107 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
2108 * read more data, or 1 once we can go on with next request or end the stream.
2109 *
2110 * It is capable of compressing response data both in content-length mode and
2111 * in chunked mode. The state machines follows different flows depending on
2112 * whether content-length and chunked modes are used, since there are no
2113 * trailers in content-length :
2114 *
2115 * chk-mode cl-mode
2116 * ,----- BODY -----.
2117 * / \
2118 * V size > 0 V chk-mode
2119 * .--> SIZE -------------> DATA -------------> CRLF
2120 * | | size == 0 | last byte |
2121 * | v final crlf v inspected |
2122 * | TRAILERS -----------> DONE |
2123 * | |
2124 * `----------------------------------------------'
2125 *
2126 * Compression only happens in the DATA state, and must be flushed in final
2127 * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding
2128 * is performed at once on final states for all bytes parsed, or when leaving
2129 * on missing data.
2130 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002131int http_response_forward_body(struct stream *s, struct channel *res, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +02002132{
2133 struct session *sess = s->sess;
2134 struct http_txn *txn = s->txn;
2135 struct http_msg *msg = &s->txn->rsp;
Christopher Faulet9768c262018-10-22 09:34:31 +02002136 struct htx *htx;
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002137 int ret;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002138
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002139 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002140
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002141 htx = htxbuf(&res->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002142
Christopher Fauletb9a92f32019-09-09 10:15:21 +02002143 if (htx->flags & HTX_FL_PARSING_ERROR)
2144 goto return_bad_res;
2145 if (htx->flags & HTX_FL_PROCESSING_ERROR)
2146 goto return_int_err;
2147
Christopher Faulete0768eb2018-10-03 16:38:02 +02002148 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Christopher Fauletf2824e62018-10-01 12:12:37 +02002149 ((res->flags & CF_SHUTW) && (res->to_forward || co_data(res)))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02002150 /* Output closed while we were sending data. We must abort and
2151 * wake the other side up.
2152 */
Christopher Faulete0768eb2018-10-03 16:38:02 +02002153 msg->msg_state = HTTP_MSG_ERROR;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002154 http_end_response(s);
2155 http_end_request(s);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002156 DBG_TRACE_DEVEL("leaving on error",
2157 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002158 return 1;
2159 }
2160
Christopher Faulet9768c262018-10-22 09:34:31 +02002161 if (msg->msg_state == HTTP_MSG_BODY)
2162 msg->msg_state = HTTP_MSG_DATA;
2163
Christopher Faulete0768eb2018-10-03 16:38:02 +02002164 /* in most states, we should abort in case of early close */
2165 channel_auto_close(res);
2166
Christopher Faulete0768eb2018-10-03 16:38:02 +02002167 if (res->to_forward) {
Christopher Faulet66af0b22019-03-22 14:54:52 +01002168 if (res->to_forward == CHN_INFINITE_FORWARD) {
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002169 if (res->flags & CF_EOI)
2170 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet66af0b22019-03-22 14:54:52 +01002171 }
2172 else {
2173 /* We can't process the buffer's contents yet */
2174 res->flags |= CF_WAKE_WRITE;
2175 goto missing_data_or_waiting;
2176 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02002177 }
2178
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002179 if (msg->msg_state >= HTTP_MSG_ENDING)
2180 goto ending;
2181
Christopher Fauletc75668e2020-12-07 18:10:32 +01002182 if ((txn->meth == HTTP_METH_CONNECT && txn->status >= 200 && txn->status < 300) || txn->status == 101 ||
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002183 (!(msg->flags & HTTP_MSGF_XFER_LEN) && !HAS_RSP_DATA_FILTERS(s))) {
2184 msg->msg_state = HTTP_MSG_ENDING;
2185 goto ending;
2186 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002187
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002188 /* Forward input data. We get it by removing all outgoing data not
2189 * forwarded yet from HTX data size. If there are some data filters, we
2190 * let them decide the amount of data to forward.
Christopher Faulet9768c262018-10-22 09:34:31 +02002191 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002192 if (HAS_RSP_DATA_FILTERS(s)) {
2193 ret = flt_http_payload(s, msg, htx->data);
2194 if (ret < 0)
2195 goto return_bad_res;
Christopher Faulet421e7692019-06-13 11:16:45 +02002196 c_adv(res, ret);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002197 }
2198 else {
Christopher Faulet421e7692019-06-13 11:16:45 +02002199 c_adv(res, htx->data - co_data(res));
Christopher Faulet66af0b22019-03-22 14:54:52 +01002200 if (msg->flags & HTTP_MSGF_XFER_LEN)
2201 channel_htx_forward_forever(res, htx);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002202 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002203
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002204 if (htx->data != co_data(res))
2205 goto missing_data_or_waiting;
2206
2207 if (!(msg->flags & HTTP_MSGF_XFER_LEN) && res->flags & CF_SHUTR) {
2208 msg->msg_state = HTTP_MSG_ENDING;
2209 goto ending;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002210 }
2211
Christopher Faulet9768c262018-10-22 09:34:31 +02002212 /* Check if the end-of-message is reached and if so, switch the message
Christopher Fauletd20fdb02019-06-13 16:43:22 +02002213 * in HTTP_MSG_ENDING state. Then if all data was marked to be
2214 * forwarded, set the state to HTTP_MSG_DONE.
Christopher Faulet9768c262018-10-22 09:34:31 +02002215 */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002216 if (!(htx->flags & HTX_FL_EOM))
Christopher Faulet9768c262018-10-22 09:34:31 +02002217 goto missing_data_or_waiting;
2218
Christopher Fauletd20fdb02019-06-13 16:43:22 +02002219 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet9768c262018-10-22 09:34:31 +02002220
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002221 ending:
Christopher Faulet2151cdd2020-07-22 16:34:59 +02002222 res->flags &= ~CF_EXPECT_MORE; /* no more data are expected */
2223
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002224 /* other states, ENDING...TUNNEL */
2225 if (msg->msg_state >= HTTP_MSG_DONE)
2226 goto done;
Christopher Faulet9768c262018-10-22 09:34:31 +02002227
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002228 if (HAS_RSP_DATA_FILTERS(s)) {
2229 ret = flt_http_end(s, msg);
2230 if (ret <= 0) {
2231 if (!ret)
2232 goto missing_data_or_waiting;
2233 goto return_bad_res;
2234 }
2235 }
2236
Christopher Fauletc75668e2020-12-07 18:10:32 +01002237 if ((txn->meth == HTTP_METH_CONNECT && txn->status >= 200 && txn->status < 300) || txn->status == 101 ||
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002238 !(msg->flags & HTTP_MSGF_XFER_LEN)) {
2239 msg->msg_state = HTTP_MSG_TUNNEL;
2240 goto ending;
2241 }
2242 else {
2243 msg->msg_state = HTTP_MSG_DONE;
2244 res->to_forward = 0;
2245 }
2246
2247 done:
2248
2249 channel_dont_close(res);
2250
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002251 http_end_response(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002252 if (!(res->analysers & an_bit)) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002253 http_end_request(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002254 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
2255 if (res->flags & CF_SHUTW) {
2256 /* response errors are most likely due to the
2257 * client aborting the transfer. */
Christopher Faulet93e02d82019-03-08 14:18:50 +01002258 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002259 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02002260 goto return_bad_res;
2261 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002262 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002263 return 1;
2264 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002265 DBG_TRACE_DEVEL("waiting for the end of the HTTP txn",
2266 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002267 return 0;
2268
2269 missing_data_or_waiting:
2270 if (res->flags & CF_SHUTW)
Christopher Faulet93e02d82019-03-08 14:18:50 +01002271 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002272
2273 /* stop waiting for data if the input is closed before the end. If the
2274 * client side was already closed, it means that the client has aborted,
2275 * so we don't want to count this as a server abort. Otherwise it's a
2276 * server abort.
2277 */
Christopher Fauletd20fdb02019-06-13 16:43:22 +02002278 if (msg->msg_state < HTTP_MSG_ENDING && res->flags & CF_SHUTR) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02002279 if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))
Christopher Faulet93e02d82019-03-08 14:18:50 +01002280 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002281 /* If we have some pending data, we continue the processing */
Christopher Faulet93e02d82019-03-08 14:18:50 +01002282 if (htx_is_empty(htx))
2283 goto return_srv_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002284 }
2285
Christopher Faulete0768eb2018-10-03 16:38:02 +02002286 /* When TE: chunked is used, we need to get there again to parse
2287 * remaining chunks even if the server has closed, so we don't want to
Christopher Faulet9768c262018-10-22 09:34:31 +02002288 * set CF_DONTCLOSE. Similarly when there is a content-leng or if there
2289 * are filters registered on the stream, we don't want to forward a
2290 * close
Christopher Faulete0768eb2018-10-03 16:38:02 +02002291 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002292 if ((msg->flags & HTTP_MSGF_XFER_LEN) || HAS_RSP_DATA_FILTERS(s))
Christopher Faulete0768eb2018-10-03 16:38:02 +02002293 channel_dont_close(res);
2294
2295 /* We know that more data are expected, but we couldn't send more that
2296 * what we did. So we always set the CF_EXPECT_MORE flag so that the
2297 * system knows it must not set a PUSH on this first part. Interactive
2298 * modes are already handled by the stream sock layer. We must not do
2299 * this in content-length mode because it could present the MSG_MORE
2300 * flag with the last block of forwarded data, which would cause an
2301 * additional delay to be observed by the receiver.
2302 */
Christopher Faulet2151cdd2020-07-22 16:34:59 +02002303 if (HAS_RSP_DATA_FILTERS(s))
Christopher Faulete0768eb2018-10-03 16:38:02 +02002304 res->flags |= CF_EXPECT_MORE;
2305
2306 /* the stream handler will take care of timeouts and errors */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002307 DBG_TRACE_DEVEL("waiting for more data to forward",
2308 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002309 return 0;
2310
Christopher Faulet93e02d82019-03-08 14:18:50 +01002311 return_srv_abort:
Willy Tarreau4781b152021-04-06 13:53:36 +02002312 _HA_ATOMIC_INC(&sess->fe->fe_counters.srv_aborts);
2313 _HA_ATOMIC_INC(&s->be->be_counters.srv_aborts);
William Lallemand36119de2021-03-08 15:26:48 +01002314 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02002315 _HA_ATOMIC_INC(&sess->listener->counters->srv_aborts);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002316 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02002317 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.srv_aborts);
Willy Tarreau826f3ab2021-02-10 12:07:15 +01002318 stream_inc_http_fail_ctr(s);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002319 if (!(s->flags & SF_ERR_MASK))
2320 s->flags |= SF_ERR_SRVCL;
2321 goto return_error;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002322
Christopher Faulet93e02d82019-03-08 14:18:50 +01002323 return_cli_abort:
Willy Tarreau4781b152021-04-06 13:53:36 +02002324 _HA_ATOMIC_INC(&sess->fe->fe_counters.cli_aborts);
2325 _HA_ATOMIC_INC(&s->be->be_counters.cli_aborts);
William Lallemand36119de2021-03-08 15:26:48 +01002326 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02002327 _HA_ATOMIC_INC(&sess->listener->counters->cli_aborts);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002328 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02002329 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.cli_aborts);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002330 if (!(s->flags & SF_ERR_MASK))
2331 s->flags |= SF_ERR_CLICL;
2332 goto return_error;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002333
Christopher Fauletb9a92f32019-09-09 10:15:21 +02002334 return_int_err:
Willy Tarreau4781b152021-04-06 13:53:36 +02002335 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
2336 _HA_ATOMIC_INC(&s->be->be_counters.internal_errors);
William Lallemand36119de2021-03-08 15:26:48 +01002337 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02002338 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002339 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02002340 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.internal_errors);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02002341 if (!(s->flags & SF_ERR_MASK))
2342 s->flags |= SF_ERR_INTERNAL;
2343 goto return_error;
2344
Christopher Faulet93e02d82019-03-08 14:18:50 +01002345 return_bad_res:
Willy Tarreau4781b152021-04-06 13:53:36 +02002346 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002347 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02002348 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002349 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_RSP);
2350 }
Willy Tarreau826f3ab2021-02-10 12:07:15 +01002351 stream_inc_http_fail_ctr(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002352 if (!(s->flags & SF_ERR_MASK))
Christopher Faulet93e02d82019-03-08 14:18:50 +01002353 s->flags |= SF_ERR_SRVCL;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002354 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +02002355
Christopher Faulet93e02d82019-03-08 14:18:50 +01002356 return_error:
Christopher Faulete0768eb2018-10-03 16:38:02 +02002357 /* don't send any error message as we're in the body */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002358 http_reply_and_close(s, txn->status, NULL);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002359 if (!(s->flags & SF_FINST_MASK))
2360 s->flags |= SF_FINST_D;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002361 DBG_TRACE_DEVEL("leaving on error",
2362 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002363 return 0;
2364}
2365
Christopher Fauletf2824e62018-10-01 12:12:37 +02002366/* Perform an HTTP redirect based on the information in <rule>. The function
Christopher Faulet99daf282018-11-28 22:58:13 +01002367 * returns zero on success, or zero in case of a, irrecoverable error such
Christopher Fauletf2824e62018-10-01 12:12:37 +02002368 * as too large a request to build a valid response.
2369 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002370int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn)
Christopher Fauletf2824e62018-10-01 12:12:37 +02002371{
Christopher Faulet99daf282018-11-28 22:58:13 +01002372 struct channel *req = &s->req;
2373 struct channel *res = &s->res;
2374 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01002375 struct htx_sl *sl;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002376 struct buffer *chunk;
Christopher Faulet99daf282018-11-28 22:58:13 +01002377 struct ist status, reason, location;
2378 unsigned int flags;
Christopher Faulet08e66462019-05-23 16:44:59 +02002379 int close = 0; /* Try to keep the connection alive byt default */
Christopher Fauletf2824e62018-10-01 12:12:37 +02002380
2381 chunk = alloc_trash_chunk();
Christopher Fauletb8a53712019-12-16 11:29:38 +01002382 if (!chunk) {
2383 if (!(s->flags & SF_ERR_MASK))
2384 s->flags |= SF_ERR_RESOURCE;
Christopher Faulet99daf282018-11-28 22:58:13 +01002385 goto fail;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002386 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002387
Christopher Faulet99daf282018-11-28 22:58:13 +01002388 /*
2389 * Create the location
2390 */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002391 htx = htxbuf(&req->buf);
Christopher Fauletf2824e62018-10-01 12:12:37 +02002392 switch(rule->type) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002393 case REDIRECT_TYPE_SCHEME: {
2394 struct http_hdr_ctx ctx;
2395 struct ist path, host;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002396
Christopher Faulet99daf282018-11-28 22:58:13 +01002397 host = ist("");
2398 ctx.blk = NULL;
2399 if (http_find_header(htx, ist("Host"), &ctx, 0))
2400 host = ctx.value;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002401
Christopher Faulet297fbb42019-05-13 14:41:27 +02002402 sl = http_get_stline(htx);
Christopher Faulet99daf282018-11-28 22:58:13 +01002403 path = http_get_path(htx_sl_req_uri(sl));
2404 /* build message using path */
Tim Duesterhused526372020-03-05 17:56:33 +01002405 if (isttest(path)) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002406 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2407 int qs = 0;
2408 while (qs < path.len) {
2409 if (*(path.ptr + qs) == '?') {
2410 path.len = qs;
2411 break;
2412 }
2413 qs++;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002414 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002415 }
2416 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002417 else
2418 path = ist("/");
Christopher Fauletf2824e62018-10-01 12:12:37 +02002419
Christopher Faulet99daf282018-11-28 22:58:13 +01002420 if (rule->rdr_str) { /* this is an old "redirect" rule */
2421 /* add scheme */
2422 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2423 goto fail;
2424 }
2425 else {
2426 /* add scheme with executing log format */
2427 chunk->data += build_logline(s, chunk->area + chunk->data,
2428 chunk->size - chunk->data,
2429 &rule->rdr_fmt);
2430 }
2431 /* add "://" + host + path */
2432 if (!chunk_memcat(chunk, "://", 3) ||
2433 !chunk_memcat(chunk, host.ptr, host.len) ||
2434 !chunk_memcat(chunk, path.ptr, path.len))
2435 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002436
Christopher Faulet99daf282018-11-28 22:58:13 +01002437 /* append a slash at the end of the location if needed and missing */
2438 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
2439 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
2440 if (chunk->data + 1 >= chunk->size)
2441 goto fail;
2442 chunk->area[chunk->data++] = '/';
2443 }
2444 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002445 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002446
Christopher Faulet99daf282018-11-28 22:58:13 +01002447 case REDIRECT_TYPE_PREFIX: {
2448 struct ist path;
2449
Christopher Faulet297fbb42019-05-13 14:41:27 +02002450 sl = http_get_stline(htx);
Christopher Faulet99daf282018-11-28 22:58:13 +01002451 path = http_get_path(htx_sl_req_uri(sl));
2452 /* build message using path */
Tim Duesterhused526372020-03-05 17:56:33 +01002453 if (isttest(path)) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002454 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2455 int qs = 0;
2456 while (qs < path.len) {
2457 if (*(path.ptr + qs) == '?') {
2458 path.len = qs;
2459 break;
2460 }
2461 qs++;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002462 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002463 }
2464 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002465 else
2466 path = ist("/");
Christopher Fauletf2824e62018-10-01 12:12:37 +02002467
Christopher Faulet99daf282018-11-28 22:58:13 +01002468 if (rule->rdr_str) { /* this is an old "redirect" rule */
2469 /* add prefix. Note that if prefix == "/", we don't want to
2470 * add anything, otherwise it makes it hard for the user to
2471 * configure a self-redirection.
2472 */
2473 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
2474 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2475 goto fail;
2476 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002477 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002478 else {
2479 /* add prefix with executing log format */
2480 chunk->data += build_logline(s, chunk->area + chunk->data,
2481 chunk->size - chunk->data,
2482 &rule->rdr_fmt);
2483 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002484
Christopher Faulet99daf282018-11-28 22:58:13 +01002485 /* add path */
2486 if (!chunk_memcat(chunk, path.ptr, path.len))
2487 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002488
Christopher Faulet99daf282018-11-28 22:58:13 +01002489 /* append a slash at the end of the location if needed and missing */
2490 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
2491 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
2492 if (chunk->data + 1 >= chunk->size)
2493 goto fail;
2494 chunk->area[chunk->data++] = '/';
2495 }
2496 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002497 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002498 case REDIRECT_TYPE_LOCATION:
2499 default:
2500 if (rule->rdr_str) { /* this is an old "redirect" rule */
2501 /* add location */
2502 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2503 goto fail;
2504 }
2505 else {
2506 /* add location with executing log format */
2507 chunk->data += build_logline(s, chunk->area + chunk->data,
2508 chunk->size - chunk->data,
2509 &rule->rdr_fmt);
2510 }
2511 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002512 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002513 location = ist2(chunk->area, chunk->data);
2514
2515 /*
2516 * Create the 30x response
2517 */
2518 switch (rule->code) {
2519 case 308:
2520 status = ist("308");
2521 reason = ist("Permanent Redirect");
2522 break;
2523 case 307:
2524 status = ist("307");
2525 reason = ist("Temporary Redirect");
2526 break;
2527 case 303:
2528 status = ist("303");
2529 reason = ist("See Other");
2530 break;
2531 case 301:
2532 status = ist("301");
2533 reason = ist("Moved Permanently");
2534 break;
2535 case 302:
2536 default:
2537 status = ist("302");
2538 reason = ist("Found");
2539 break;
2540 }
2541
Christopher Faulet08e66462019-05-23 16:44:59 +02002542 if (!(txn->req.flags & HTTP_MSGF_BODYLESS) && txn->req.msg_state != HTTP_MSG_DONE)
2543 close = 1;
2544
Christopher Faulet99daf282018-11-28 22:58:13 +01002545 htx = htx_from_buf(&res->buf);
Kevin Zhu96b36392020-01-07 09:42:55 +01002546 /* Trim any possible response */
2547 channel_htx_truncate(&s->res, htx);
Christopher Faulet99daf282018-11-28 22:58:13 +01002548 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
2549 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), status, reason);
2550 if (!sl)
2551 goto fail;
2552 sl->info.res.status = rule->code;
2553 s->txn->status = rule->code;
2554
Christopher Faulet08e66462019-05-23 16:44:59 +02002555 if (close && !htx_add_header(htx, ist("Connection"), ist("close")))
2556 goto fail;
2557
2558 if (!htx_add_header(htx, ist("Content-length"), ist("0")) ||
Christopher Faulet99daf282018-11-28 22:58:13 +01002559 !htx_add_header(htx, ist("Location"), location))
2560 goto fail;
2561
2562 if (rule->code == 302 || rule->code == 303 || rule->code == 307) {
2563 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")))
2564 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002565 }
2566
2567 if (rule->cookie_len) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002568 if (!htx_add_header(htx, ist("Set-Cookie"), ist2(rule->cookie_str, rule->cookie_len)))
2569 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002570 }
2571
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002572 if (!htx_add_endof(htx, HTX_BLK_EOH))
Christopher Faulet99daf282018-11-28 22:58:13 +01002573 goto fail;
2574
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002575 htx->flags |= HTX_FL_EOM;
Kevin Zhu96b36392020-01-07 09:42:55 +01002576 htx_to_buf(htx, &res->buf);
Christopher Fauleta72a7e42020-01-28 09:28:11 +01002577 if (!http_forward_proxy_resp(s, 1))
2578 goto fail;
Christopher Faulet99daf282018-11-28 22:58:13 +01002579
Christopher Faulet60b33a52020-01-28 09:18:10 +01002580 if (rule->flags & REDIRECT_FLAG_FROM_REQ) {
2581 /* let's log the request time */
2582 s->logs.tv_request = now;
Christopher Faulet91322272021-10-04 14:16:46 +02002583 req->analysers &= AN_REQ_FLT_END;
Christopher Faulet99daf282018-11-28 22:58:13 +01002584
Christopher Faulet60b33a52020-01-28 09:18:10 +01002585 if (s->sess->fe == s->be) /* report it if the request was intercepted by the frontend */
Willy Tarreau4781b152021-04-06 13:53:36 +02002586 _HA_ATOMIC_INC(&s->sess->fe->fe_counters.intercepted_req);
Christopher Faulet60b33a52020-01-28 09:18:10 +01002587 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002588
2589 if (!(s->flags & SF_ERR_MASK))
2590 s->flags |= SF_ERR_LOCAL;
2591 if (!(s->flags & SF_FINST_MASK))
Christopher Faulet60b33a52020-01-28 09:18:10 +01002592 s->flags |= ((rule->flags & REDIRECT_FLAG_FROM_REQ) ? SF_FINST_R : SF_FINST_H);
Christopher Fauletf2824e62018-10-01 12:12:37 +02002593
Christopher Faulet99daf282018-11-28 22:58:13 +01002594 free_trash_chunk(chunk);
2595 return 1;
2596
2597 fail:
2598 /* If an error occurred, remove the incomplete HTTP response from the
2599 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01002600 channel_htx_truncate(res, htxbuf(&res->buf));
Christopher Fauletf2824e62018-10-01 12:12:37 +02002601 free_trash_chunk(chunk);
Christopher Faulet99daf282018-11-28 22:58:13 +01002602 return 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002603}
2604
Christopher Faulet79507152022-05-16 11:43:10 +02002605/* This function filters the request header names to only allow [0-9a-zA-Z-]
2606 * characters. Depending on the proxy configuration, headers with a name not
2607 * matching this charset are removed or the request is rejected with a
2608 * 403-Forbidden response if such name are found. It returns HTTP_RULE_RES_CONT
2609 * to continue the request processing or HTTP_RULE_RES_DENY if the request is
2610 * rejected.
2611 */
2612static enum rule_result http_req_restrict_header_names(struct stream *s, struct htx *htx, struct proxy *px)
2613{
2614 struct htx_blk *blk;
2615 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
2616
2617 blk = htx_get_first_blk(htx);
2618 while (blk) {
2619 enum htx_blk_type type = htx_get_blk_type(blk);
2620
2621 if (type == HTX_BLK_HDR) {
2622 struct ist n = htx_get_blk_name(htx, blk);
2623 int i;
2624
2625 for (i = 0; i < istlen(n); i++) {
2626 if (!isalnum((unsigned char)n.ptr[i]) && n.ptr[i] != '-') {
2627 /* Block the request or remove the header */
2628 if (px->options2 & PR_O2_RSTRICT_REQ_HDR_NAMES_BLK)
2629 goto block;
2630 blk = htx_remove_blk(htx, blk);
2631 continue;
2632 }
2633 }
2634 }
2635 if (type == HTX_BLK_EOH)
2636 break;
2637
2638 blk = htx_get_next_blk(htx, blk);
2639 }
2640 out:
2641 return rule_ret;
2642 block:
2643 /* Block the request returning a 403-Forbidden response */
2644 s->txn->status = 403;
2645 rule_ret = HTTP_RULE_RES_DENY;
2646 goto out;
2647}
2648
Christopher Faulet92d34fe2019-12-17 09:20:34 +01002649/* Replace all headers matching the name <name>. The header value is replaced if
2650 * it matches the regex <re>. <str> is used for the replacement. If <full> is
2651 * set to 1, the full-line is matched and replaced. Otherwise, comma-separated
2652 * values are evaluated one by one. It returns 0 on success and -1 on error.
2653 */
2654int http_replace_hdrs(struct stream* s, struct htx *htx, struct ist name,
2655 const char *str, struct my_regex *re, int full)
Christopher Faulet72333522018-10-24 11:25:02 +02002656{
2657 struct http_hdr_ctx ctx;
2658 struct buffer *output = get_trash_chunk();
2659
Christopher Faulet72333522018-10-24 11:25:02 +02002660 ctx.blk = NULL;
Christopher Faulet92d34fe2019-12-17 09:20:34 +01002661 while (http_find_header(htx, name, &ctx, full)) {
Christopher Faulet72333522018-10-24 11:25:02 +02002662 if (!regex_exec_match2(re, ctx.value.ptr, ctx.value.len, MAX_MATCH, pmatch, 0))
2663 continue;
2664
2665 output->data = exp_replace(output->area, output->size, ctx.value.ptr, str, pmatch);
2666 if (output->data == -1)
2667 return -1;
2668 if (!http_replace_header_value(htx, &ctx, ist2(output->area, output->data)))
2669 return -1;
2670 }
2671 return 0;
2672}
2673
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002674/* This function executes one of the set-{method,path,query,uri} actions. It
2675 * takes the string from the variable 'replace' with length 'len', then modifies
2676 * the relevant part of the request line accordingly. Then it updates various
2677 * pointers to the next elements which were moved, and the total buffer length.
2678 * It finds the action to be performed in p[2], previously filled by function
2679 * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal
2680 * error, though this can be revisited when this code is finally exploited.
2681 *
2682 * 'action' can be '0' to replace method, '1' to replace path, '2' to replace
Christopher Faulet312294f2020-09-02 17:17:44 +02002683 * query string, 3 to replace uri or 4 to replace the path+query.
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002684 *
2685 * In query string case, the mark question '?' must be set at the start of the
2686 * string by the caller, event if the replacement query string is empty.
2687 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002688int http_req_replace_stline(int action, const char *replace, int len,
2689 struct proxy *px, struct stream *s)
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002690{
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002691 struct htx *htx = htxbuf(&s->req.buf);
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002692
2693 switch (action) {
2694 case 0: // method
2695 if (!http_replace_req_meth(htx, ist2(replace, len)))
2696 return -1;
2697 break;
2698
2699 case 1: // path
Christopher Fauletb8ce5052020-08-31 16:11:57 +02002700 if (!http_replace_req_path(htx, ist2(replace, len), 0))
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002701 return -1;
2702 break;
2703
2704 case 2: // query
2705 if (!http_replace_req_query(htx, ist2(replace, len)))
2706 return -1;
2707 break;
2708
2709 case 3: // uri
2710 if (!http_replace_req_uri(htx, ist2(replace, len)))
2711 return -1;
2712 break;
2713
Christopher Faulet312294f2020-09-02 17:17:44 +02002714 case 4: // path + query
2715 if (!http_replace_req_path(htx, ist2(replace, len), 1))
2716 return -1;
2717 break;
2718
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002719 default:
2720 return -1;
2721 }
2722 return 0;
2723}
2724
2725/* This function replace the HTTP status code and the associated message. The
Christopher Faulete00d06c2019-12-16 17:18:42 +01002726 * variable <status> contains the new status code. This function never fails. It
2727 * returns 0 in case of success, -1 in case of internal error.
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002728 */
Christopher Faulet96bff762019-12-17 13:46:18 +01002729int http_res_set_status(unsigned int status, struct ist reason, struct stream *s)
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002730{
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002731 struct htx *htx = htxbuf(&s->res.buf);
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002732 char *res;
2733
2734 chunk_reset(&trash);
2735 res = ultoa_o(status, trash.area, trash.size);
2736 trash.data = res - trash.area;
2737
2738 /* Do we have a custom reason format string? */
Tim Duesterhuse296d3e2020-03-05 17:56:31 +01002739 if (!isttest(reason)) {
Christopher Faulet96bff762019-12-17 13:46:18 +01002740 const char *str = http_get_reason(status);
Tim Duesterhusdcf753a2021-03-04 17:31:47 +01002741 reason = ist(str);
Christopher Faulet96bff762019-12-17 13:46:18 +01002742 }
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002743
Christopher Fauletbde2c4c2020-08-31 16:43:34 +02002744 if (!http_replace_res_status(htx, ist2(trash.area, trash.data), reason))
Christopher Faulete00d06c2019-12-16 17:18:42 +01002745 return -1;
2746 return 0;
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002747}
2748
Christopher Faulet3e964192018-10-24 11:39:23 +02002749/* Executes the http-request rules <rules> for stream <s>, proxy <px> and
2750 * transaction <txn>. Returns the verdict of the first rule that prevents
2751 * further processing of the request (auth, deny, ...), and defaults to
2752 * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or
2753 * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT
2754 * on txn->flags if it encounters a tarpit rule. If <deny_status> is not NULL
2755 * and a deny/tarpit rule is matched, it will be filled with this rule's deny
2756 * status.
2757 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002758static enum rule_result http_req_get_intercept_rule(struct proxy *px, struct list *rules,
Christopher Fauletb58f62b2020-01-13 16:40:13 +01002759 struct stream *s)
Christopher Faulet3e964192018-10-24 11:39:23 +02002760{
2761 struct session *sess = strm_sess(s);
2762 struct http_txn *txn = s->txn;
Christopher Faulet3e964192018-10-24 11:39:23 +02002763 struct act_rule *rule;
Christopher Faulet3e964192018-10-24 11:39:23 +02002764 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002765 int act_opts = 0;
Christopher Faulet3e964192018-10-24 11:39:23 +02002766
Christopher Faulet3e964192018-10-24 11:39:23 +02002767 /* If "the current_rule_list" match the executed rule list, we are in
2768 * resume condition. If a resume is needed it is always in the action
2769 * and never in the ACL or converters. In this case, we initialise the
2770 * current rule, and go to the action execution point.
2771 */
2772 if (s->current_rule) {
2773 rule = s->current_rule;
2774 s->current_rule = NULL;
2775 if (s->current_rule_list == rules)
2776 goto resume_execution;
2777 }
2778 s->current_rule_list = rules;
2779
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002780 /* start the ruleset evaluation in strict mode */
2781 txn->req.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01002782
Christopher Faulet3e964192018-10-24 11:39:23 +02002783 list_for_each_entry(rule, rules, list) {
2784 /* check optional condition */
2785 if (rule->cond) {
2786 int ret;
2787
2788 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
2789 ret = acl_pass(ret);
2790
2791 if (rule->cond->pol == ACL_COND_UNLESS)
2792 ret = !ret;
2793
2794 if (!ret) /* condition not matched */
2795 continue;
2796 }
2797
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002798 act_opts |= ACT_OPT_FIRST;
Christopher Faulet3e964192018-10-24 11:39:23 +02002799 resume_execution:
Amaury Denoyelle03517732021-05-07 14:25:01 +02002800 if (rule->kw->flags & KWF_EXPERIMENTAL)
2801 mark_tainted(TAINTED_ACTION_EXP_EXECUTED);
2802
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002803 /* Always call the action function if defined */
2804 if (rule->action_ptr) {
2805 if ((s->req.flags & CF_READ_ERROR) ||
2806 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
2807 (px->options & PR_O_ABRT_CLOSE)))
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002808 act_opts |= ACT_OPT_FINAL;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002809
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002810 switch (rule->action_ptr(rule, px, sess, s, act_opts)) {
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002811 case ACT_RET_CONT:
2812 break;
2813 case ACT_RET_STOP:
2814 rule_ret = HTTP_RULE_RES_STOP;
2815 goto end;
2816 case ACT_RET_YIELD:
2817 s->current_rule = rule;
2818 rule_ret = HTTP_RULE_RES_YIELD;
2819 goto end;
2820 case ACT_RET_ERR:
2821 rule_ret = HTTP_RULE_RES_ERROR;
2822 goto end;
2823 case ACT_RET_DONE:
2824 rule_ret = HTTP_RULE_RES_DONE;
2825 goto end;
2826 case ACT_RET_DENY:
Christopher Fauletb58f62b2020-01-13 16:40:13 +01002827 if (txn->status == -1)
2828 txn->status = 403;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002829 rule_ret = HTTP_RULE_RES_DENY;
2830 goto end;
2831 case ACT_RET_ABRT:
2832 rule_ret = HTTP_RULE_RES_ABRT;
2833 goto end;
2834 case ACT_RET_INV:
2835 rule_ret = HTTP_RULE_RES_BADREQ;
2836 goto end;
2837 }
2838 continue; /* eval the next rule */
2839 }
2840
2841 /* If not action function defined, check for known actions */
Christopher Faulet3e964192018-10-24 11:39:23 +02002842 switch (rule->action) {
2843 case ACT_ACTION_ALLOW:
2844 rule_ret = HTTP_RULE_RES_STOP;
2845 goto end;
2846
2847 case ACT_ACTION_DENY:
Christopher Faulet5cb513a2020-05-13 17:56:56 +02002848 txn->status = rule->arg.http_reply->status;
2849 txn->http_reply = rule->arg.http_reply;
Christopher Faulet3e964192018-10-24 11:39:23 +02002850 rule_ret = HTTP_RULE_RES_DENY;
2851 goto end;
2852
2853 case ACT_HTTP_REQ_TARPIT:
2854 txn->flags |= TX_CLTARPIT;
Christopher Faulet5cb513a2020-05-13 17:56:56 +02002855 txn->status = rule->arg.http_reply->status;
2856 txn->http_reply = rule->arg.http_reply;
Christopher Faulet3e964192018-10-24 11:39:23 +02002857 rule_ret = HTTP_RULE_RES_DENY;
2858 goto end;
2859
Christopher Faulet3e964192018-10-24 11:39:23 +02002860 case ACT_HTTP_REDIR:
Christopher Faulet90d22a82020-03-06 11:18:39 +01002861 rule_ret = HTTP_RULE_RES_ABRT;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002862 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
Christopher Faulet3a26bee2019-12-16 12:47:40 +01002863 rule_ret = HTTP_RULE_RES_ERROR;
Christopher Faulet3e964192018-10-24 11:39:23 +02002864 goto end;
2865
2866 case ACT_HTTP_SET_NICE:
Christopher Faulet96bff762019-12-17 13:46:18 +01002867 s->task->nice = rule->arg.http.i;
Christopher Faulet3e964192018-10-24 11:39:23 +02002868 break;
2869
2870 case ACT_HTTP_SET_TOS:
Christopher Faulet96bff762019-12-17 13:46:18 +01002871 conn_set_tos(objt_conn(sess->origin), rule->arg.http.i);
Christopher Faulet3e964192018-10-24 11:39:23 +02002872 break;
2873
2874 case ACT_HTTP_SET_MARK:
Christopher Faulet96bff762019-12-17 13:46:18 +01002875 conn_set_mark(objt_conn(sess->origin), rule->arg.http.i);
Christopher Faulet3e964192018-10-24 11:39:23 +02002876 break;
2877
2878 case ACT_HTTP_SET_LOGL:
Christopher Faulet96bff762019-12-17 13:46:18 +01002879 s->logs.level = rule->arg.http.i;
Christopher Faulet3e964192018-10-24 11:39:23 +02002880 break;
2881
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002882 /* other flags exists, but normally, they never be matched. */
Christopher Faulet3e964192018-10-24 11:39:23 +02002883 default:
2884 break;
2885 }
2886 }
2887
2888 end:
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002889 /* if the ruleset evaluation is finished reset the strict mode */
Christopher Faulet46f95542019-12-20 10:07:22 +01002890 if (rule_ret != HTTP_RULE_RES_YIELD)
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002891 txn->req.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01002892
Christopher Faulet3e964192018-10-24 11:39:23 +02002893 /* we reached the end of the rules, nothing to report */
2894 return rule_ret;
2895}
2896
2897/* Executes the http-response rules <rules> for stream <s> and proxy <px>. It
2898 * returns one of 5 possible statuses: HTTP_RULE_RES_CONT, HTTP_RULE_RES_STOP,
2899 * HTTP_RULE_RES_DONE, HTTP_RULE_RES_YIELD, or HTTP_RULE_RES_BADREQ. If *CONT
2900 * is returned, the process can continue the evaluation of next rule list. If
2901 * *STOP or *DONE is returned, the process must stop the evaluation. If *BADREQ
2902 * is returned, it means the operation could not be processed and a server error
Christopher Fauleta53abad2020-05-13 08:12:22 +02002903 * must be returned. If *YIELD is returned, the caller must call again the
2904 * function with the same context.
Christopher Faulet3e964192018-10-24 11:39:23 +02002905 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002906static enum rule_result http_res_get_intercept_rule(struct proxy *px, struct list *rules,
2907 struct stream *s)
Christopher Faulet3e964192018-10-24 11:39:23 +02002908{
2909 struct session *sess = strm_sess(s);
2910 struct http_txn *txn = s->txn;
Christopher Faulet3e964192018-10-24 11:39:23 +02002911 struct act_rule *rule;
Christopher Faulet3e964192018-10-24 11:39:23 +02002912 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002913 int act_opts = 0;
Christopher Faulet3e964192018-10-24 11:39:23 +02002914
Christopher Faulet3e964192018-10-24 11:39:23 +02002915 /* If "the current_rule_list" match the executed rule list, we are in
2916 * resume condition. If a resume is needed it is always in the action
2917 * and never in the ACL or converters. In this case, we initialise the
2918 * current rule, and go to the action execution point.
2919 */
2920 if (s->current_rule) {
2921 rule = s->current_rule;
2922 s->current_rule = NULL;
2923 if (s->current_rule_list == rules)
2924 goto resume_execution;
2925 }
2926 s->current_rule_list = rules;
2927
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002928 /* start the ruleset evaluation in strict mode */
2929 txn->rsp.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01002930
Christopher Faulet3e964192018-10-24 11:39:23 +02002931 list_for_each_entry(rule, rules, list) {
2932 /* check optional condition */
2933 if (rule->cond) {
2934 int ret;
2935
2936 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
2937 ret = acl_pass(ret);
2938
2939 if (rule->cond->pol == ACL_COND_UNLESS)
2940 ret = !ret;
2941
2942 if (!ret) /* condition not matched */
2943 continue;
2944 }
2945
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002946 act_opts |= ACT_OPT_FIRST;
Christopher Faulet3e964192018-10-24 11:39:23 +02002947resume_execution:
Amaury Denoyelle03517732021-05-07 14:25:01 +02002948 if (rule->kw->flags & KWF_EXPERIMENTAL)
2949 mark_tainted(TAINTED_ACTION_EXP_EXECUTED);
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002950
2951 /* Always call the action function if defined */
2952 if (rule->action_ptr) {
2953 if ((s->req.flags & CF_READ_ERROR) ||
2954 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
2955 (px->options & PR_O_ABRT_CLOSE)))
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002956 act_opts |= ACT_OPT_FINAL;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002957
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002958 switch (rule->action_ptr(rule, px, sess, s, act_opts)) {
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002959 case ACT_RET_CONT:
2960 break;
2961 case ACT_RET_STOP:
2962 rule_ret = HTTP_RULE_RES_STOP;
2963 goto end;
2964 case ACT_RET_YIELD:
2965 s->current_rule = rule;
2966 rule_ret = HTTP_RULE_RES_YIELD;
2967 goto end;
2968 case ACT_RET_ERR:
2969 rule_ret = HTTP_RULE_RES_ERROR;
2970 goto end;
2971 case ACT_RET_DONE:
2972 rule_ret = HTTP_RULE_RES_DONE;
2973 goto end;
2974 case ACT_RET_DENY:
Christopher Fauletb58f62b2020-01-13 16:40:13 +01002975 if (txn->status == -1)
2976 txn->status = 502;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002977 rule_ret = HTTP_RULE_RES_DENY;
2978 goto end;
2979 case ACT_RET_ABRT:
2980 rule_ret = HTTP_RULE_RES_ABRT;
2981 goto end;
2982 case ACT_RET_INV:
2983 rule_ret = HTTP_RULE_RES_BADREQ;
2984 goto end;
2985 }
2986 continue; /* eval the next rule */
2987 }
2988
2989 /* If not action function defined, check for known actions */
Christopher Faulet3e964192018-10-24 11:39:23 +02002990 switch (rule->action) {
2991 case ACT_ACTION_ALLOW:
2992 rule_ret = HTTP_RULE_RES_STOP; /* "allow" rules are OK */
2993 goto end;
2994
2995 case ACT_ACTION_DENY:
Christopher Faulet5cb513a2020-05-13 17:56:56 +02002996 txn->status = rule->arg.http_reply->status;
2997 txn->http_reply = rule->arg.http_reply;
Christopher Faulet3a26bee2019-12-16 12:47:40 +01002998 rule_ret = HTTP_RULE_RES_DENY;
Christopher Faulet3e964192018-10-24 11:39:23 +02002999 goto end;
3000
3001 case ACT_HTTP_SET_NICE:
Christopher Faulet96bff762019-12-17 13:46:18 +01003002 s->task->nice = rule->arg.http.i;
Christopher Faulet3e964192018-10-24 11:39:23 +02003003 break;
3004
3005 case ACT_HTTP_SET_TOS:
Christopher Faulet96bff762019-12-17 13:46:18 +01003006 conn_set_tos(objt_conn(sess->origin), rule->arg.http.i);
Christopher Faulet3e964192018-10-24 11:39:23 +02003007 break;
3008
3009 case ACT_HTTP_SET_MARK:
Christopher Faulet96bff762019-12-17 13:46:18 +01003010 conn_set_mark(objt_conn(sess->origin), rule->arg.http.i);
Christopher Faulet3e964192018-10-24 11:39:23 +02003011 break;
3012
3013 case ACT_HTTP_SET_LOGL:
Christopher Faulet96bff762019-12-17 13:46:18 +01003014 s->logs.level = rule->arg.http.i;
Christopher Faulet3e964192018-10-24 11:39:23 +02003015 break;
3016
Christopher Faulet3e964192018-10-24 11:39:23 +02003017 case ACT_HTTP_REDIR:
Christopher Faulet49c2a702020-03-06 15:44:37 +01003018 rule_ret = HTTP_RULE_RES_ABRT;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003019 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
Christopher Faulet3a26bee2019-12-16 12:47:40 +01003020 rule_ret = HTTP_RULE_RES_ERROR;
Christopher Faulet3e964192018-10-24 11:39:23 +02003021 goto end;
3022
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01003023 /* other flags exists, but normally, they never be matched. */
Christopher Faulet3e964192018-10-24 11:39:23 +02003024 default:
3025 break;
3026 }
3027 }
3028
3029 end:
Christopher Faulet1aea50e2020-01-17 16:03:53 +01003030 /* if the ruleset evaluation is finished reset the strict mode */
Christopher Faulet46f95542019-12-20 10:07:22 +01003031 if (rule_ret != HTTP_RULE_RES_YIELD)
Christopher Faulet1aea50e2020-01-17 16:03:53 +01003032 txn->rsp.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01003033
Christopher Faulet3e964192018-10-24 11:39:23 +02003034 /* we reached the end of the rules, nothing to report */
3035 return rule_ret;
3036}
3037
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01003038/* Executes backend and frontend http-after-response rules for the stream <s>,
3039 * in that order. it return 1 on success and 0 on error. It is the caller
3040 * responsibility to catch error or ignore it. If it catches it, this function
3041 * may be called a second time, for the internal error.
3042 */
3043int http_eval_after_res_rules(struct stream *s)
3044{
3045 struct session *sess = s->sess;
3046 enum rule_result ret = HTTP_RULE_RES_CONT;
3047
Christopher Faulet507479b2020-05-15 12:29:46 +02003048 /* Eval after-response ruleset only if the reply is not const */
3049 if (s->txn->flags & TX_CONST_REPLY)
3050 goto end;
3051
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01003052 /* prune the request variables if not already done and swap to the response variables. */
3053 if (s->vars_reqres.scope != SCOPE_RES) {
3054 if (!LIST_ISEMPTY(&s->vars_reqres.head))
3055 vars_prune(&s->vars_reqres, s->sess, s);
3056 vars_init(&s->vars_reqres, SCOPE_RES);
3057 }
3058
3059 ret = http_res_get_intercept_rule(s->be, &s->be->http_after_res_rules, s);
Christopher Fauletf5db0fc2021-11-09 17:48:39 +01003060 if ((ret == HTTP_RULE_RES_CONT || ret == HTTP_RULE_RES_STOP) && sess->fe != s->be)
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01003061 ret = http_res_get_intercept_rule(sess->fe, &sess->fe->http_after_res_rules, s);
3062
Christopher Faulet507479b2020-05-15 12:29:46 +02003063 end:
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01003064 /* All other codes than CONTINUE, STOP or DONE are forbidden */
3065 return (ret == HTTP_RULE_RES_CONT || ret == HTTP_RULE_RES_STOP || ret == HTTP_RULE_RES_DONE);
3066}
3067
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003068/*
3069 * Manage client-side cookie. It can impact performance by about 2% so it is
3070 * desirable to call it only when needed. This code is quite complex because
3071 * of the multiple very crappy and ambiguous syntaxes we have to support. it
3072 * highly recommended not to touch this part without a good reason !
3073 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003074static void http_manage_client_side_cookies(struct stream *s, struct channel *req)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003075{
3076 struct session *sess = s->sess;
3077 struct http_txn *txn = s->txn;
3078 struct htx *htx;
3079 struct http_hdr_ctx ctx;
3080 char *hdr_beg, *hdr_end, *del_from;
3081 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
3082 int preserve_hdr;
3083
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003084 htx = htxbuf(&req->buf);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003085 ctx.blk = NULL;
3086 while (http_find_header(htx, ist("Cookie"), &ctx, 1)) {
Olivier Houchardf0f42382019-07-22 17:43:46 +02003087 int is_first = 1;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003088 del_from = NULL; /* nothing to be deleted */
3089 preserve_hdr = 0; /* assume we may kill the whole header */
3090
3091 /* Now look for cookies. Conforming to RFC2109, we have to support
3092 * attributes whose name begin with a '$', and associate them with
3093 * the right cookie, if we want to delete this cookie.
3094 * So there are 3 cases for each cookie read :
3095 * 1) it's a special attribute, beginning with a '$' : ignore it.
3096 * 2) it's a server id cookie that we *MAY* want to delete : save
3097 * some pointers on it (last semi-colon, beginning of cookie...)
3098 * 3) it's an application cookie : we *MAY* have to delete a previous
3099 * "special" cookie.
3100 * At the end of loop, if a "special" cookie remains, we may have to
3101 * remove it. If no application cookie persists in the header, we
3102 * *MUST* delete it.
3103 *
3104 * Note: RFC2965 is unclear about the processing of spaces around
3105 * the equal sign in the ATTR=VALUE form. A careful inspection of
3106 * the RFC explicitly allows spaces before it, and not within the
3107 * tokens (attrs or values). An inspection of RFC2109 allows that
3108 * too but section 10.1.3 lets one think that spaces may be allowed
3109 * after the equal sign too, resulting in some (rare) buggy
3110 * implementations trying to do that. So let's do what servers do.
3111 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
3112 * allowed quoted strings in values, with any possible character
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003113 * after a backslash, including control chars and delimiters, which
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003114 * causes parsing to become ambiguous. Browsers also allow spaces
3115 * within values even without quotes.
3116 *
3117 * We have to keep multiple pointers in order to support cookie
3118 * removal at the beginning, middle or end of header without
3119 * corrupting the header. All of these headers are valid :
3120 *
3121 * hdr_beg hdr_end
3122 * | |
3123 * v |
3124 * NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3 |
3125 * NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3 v
3126 * NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3
3127 * | | | | | | |
3128 * | | | | | | |
3129 * | | | | | | +--> next
3130 * | | | | | +----> val_end
3131 * | | | | +-----------> val_beg
3132 * | | | +--------------> equal
3133 * | | +----------------> att_end
3134 * | +---------------------> att_beg
3135 * +--------------------------> prev
3136 *
3137 */
3138 hdr_beg = ctx.value.ptr;
3139 hdr_end = hdr_beg + ctx.value.len;
3140 for (prev = hdr_beg; prev < hdr_end; prev = next) {
3141 /* Iterate through all cookies on this line */
3142
3143 /* find att_beg */
3144 att_beg = prev;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003145 if (!is_first)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003146 att_beg++;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003147 is_first = 0;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003148
3149 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
3150 att_beg++;
3151
3152 /* find att_end : this is the first character after the last non
3153 * space before the equal. It may be equal to hdr_end.
3154 */
3155 equal = att_end = att_beg;
3156 while (equal < hdr_end) {
3157 if (*equal == '=' || *equal == ',' || *equal == ';')
3158 break;
3159 if (HTTP_IS_SPHT(*equal++))
3160 continue;
3161 att_end = equal;
3162 }
3163
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003164 /* here, <equal> points to '=', a delimiter or the end. <att_end>
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003165 * is between <att_beg> and <equal>, both may be identical.
3166 */
3167 /* look for end of cookie if there is an equal sign */
3168 if (equal < hdr_end && *equal == '=') {
3169 /* look for the beginning of the value */
3170 val_beg = equal + 1;
3171 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
3172 val_beg++;
3173
3174 /* find the end of the value, respecting quotes */
3175 next = http_find_cookie_value_end(val_beg, hdr_end);
3176
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003177 /* make val_end point to the first white space or delimiter after the value */
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003178 val_end = next;
3179 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
3180 val_end--;
3181 }
3182 else
3183 val_beg = val_end = next = equal;
3184
3185 /* We have nothing to do with attributes beginning with
3186 * '$'. However, they will automatically be removed if a
3187 * header before them is removed, since they're supposed
3188 * to be linked together.
3189 */
3190 if (*att_beg == '$')
3191 continue;
3192
3193 /* Ignore cookies with no equal sign */
3194 if (equal == next) {
3195 /* This is not our cookie, so we must preserve it. But if we already
3196 * scheduled another cookie for removal, we cannot remove the
3197 * complete header, but we can remove the previous block itself.
3198 */
3199 preserve_hdr = 1;
3200 if (del_from != NULL) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003201 int delta = http_del_hdr_value(hdr_beg, hdr_end, &del_from, prev);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003202 val_end += delta;
3203 next += delta;
3204 hdr_end += delta;
3205 prev = del_from;
3206 del_from = NULL;
3207 }
3208 continue;
3209 }
3210
3211 /* if there are spaces around the equal sign, we need to
3212 * strip them otherwise we'll get trouble for cookie captures,
3213 * or even for rewrites. Since this happens extremely rarely,
3214 * it does not hurt performance.
3215 */
3216 if (unlikely(att_end != equal || val_beg > equal + 1)) {
3217 int stripped_before = 0;
3218 int stripped_after = 0;
3219
3220 if (att_end != equal) {
3221 memmove(att_end, equal, hdr_end - equal);
3222 stripped_before = (att_end - equal);
3223 equal += stripped_before;
3224 val_beg += stripped_before;
3225 }
3226
3227 if (val_beg > equal + 1) {
3228 memmove(equal + 1, val_beg, hdr_end + stripped_before - val_beg);
3229 stripped_after = (equal + 1) - val_beg;
3230 val_beg += stripped_after;
3231 stripped_before += stripped_after;
3232 }
3233
3234 val_end += stripped_before;
3235 next += stripped_before;
3236 hdr_end += stripped_before;
3237 }
3238 /* now everything is as on the diagram above */
3239
3240 /* First, let's see if we want to capture this cookie. We check
3241 * that we don't already have a client side cookie, because we
3242 * can only capture one. Also as an optimisation, we ignore
3243 * cookies shorter than the declared name.
3244 */
3245 if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL &&
3246 (val_end - att_beg >= sess->fe->capture_namelen) &&
3247 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
3248 int log_len = val_end - att_beg;
3249
3250 if ((txn->cli_cookie = pool_alloc(pool_head_capture)) == NULL) {
3251 ha_alert("HTTP logging : out of memory.\n");
3252 } else {
3253 if (log_len > sess->fe->capture_len)
3254 log_len = sess->fe->capture_len;
3255 memcpy(txn->cli_cookie, att_beg, log_len);
3256 txn->cli_cookie[log_len] = 0;
3257 }
3258 }
3259
3260 /* Persistence cookies in passive, rewrite or insert mode have the
3261 * following form :
3262 *
3263 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
3264 *
3265 * For cookies in prefix mode, the form is :
3266 *
3267 * Cookie: NAME=SRV~VALUE
3268 */
3269 if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
3270 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
3271 struct server *srv = s->be->srv;
3272 char *delim;
3273
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003274 /* if we're in cookie prefix mode, we'll search the delimiter so that we
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003275 * have the server ID between val_beg and delim, and the original cookie between
3276 * delim+1 and val_end. Otherwise, delim==val_end :
3277 *
3278 * hdr_beg
3279 * |
3280 * v
3281 * NAME=SRV; # in all but prefix modes
3282 * NAME=SRV~OPAQUE ; # in prefix mode
3283 * || || | |+-> next
3284 * || || | +--> val_end
3285 * || || +---------> delim
3286 * || |+------------> val_beg
3287 * || +-------------> att_end = equal
3288 * |+-----------------> att_beg
3289 * +------------------> prev
3290 *
3291 */
3292 if (s->be->ck_opts & PR_CK_PFX) {
3293 for (delim = val_beg; delim < val_end; delim++)
3294 if (*delim == COOKIE_DELIM)
3295 break;
3296 }
3297 else {
3298 char *vbar1;
3299 delim = val_end;
3300 /* Now check if the cookie contains a date field, which would
3301 * appear after a vertical bar ('|') just after the server name
3302 * and before the delimiter.
3303 */
3304 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
3305 if (vbar1) {
3306 /* OK, so left of the bar is the server's cookie and
3307 * right is the last seen date. It is a base64 encoded
3308 * 30-bit value representing the UNIX date since the
3309 * epoch in 4-second quantities.
3310 */
3311 int val;
3312 delim = vbar1++;
3313 if (val_end - vbar1 >= 5) {
3314 val = b64tos30(vbar1);
3315 if (val > 0)
3316 txn->cookie_last_date = val << 2;
3317 }
3318 /* look for a second vertical bar */
3319 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
3320 if (vbar1 && (val_end - vbar1 > 5)) {
3321 val = b64tos30(vbar1 + 1);
3322 if (val > 0)
3323 txn->cookie_first_date = val << 2;
3324 }
3325 }
3326 }
3327
3328 /* if the cookie has an expiration date and the proxy wants to check
3329 * it, then we do that now. We first check if the cookie is too old,
3330 * then only if it has expired. We detect strict overflow because the
3331 * time resolution here is not great (4 seconds). Cookies with dates
3332 * in the future are ignored if their offset is beyond one day. This
3333 * allows an admin to fix timezone issues without expiring everyone
3334 * and at the same time avoids keeping unwanted side effects for too
3335 * long.
3336 */
3337 if (txn->cookie_first_date && s->be->cookie_maxlife &&
3338 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) ||
3339 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
3340 txn->flags &= ~TX_CK_MASK;
3341 txn->flags |= TX_CK_OLD;
3342 delim = val_beg; // let's pretend we have not found the cookie
3343 txn->cookie_first_date = 0;
3344 txn->cookie_last_date = 0;
3345 }
3346 else if (txn->cookie_last_date && s->be->cookie_maxidle &&
3347 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) ||
3348 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
3349 txn->flags &= ~TX_CK_MASK;
3350 txn->flags |= TX_CK_EXPIRED;
3351 delim = val_beg; // let's pretend we have not found the cookie
3352 txn->cookie_first_date = 0;
3353 txn->cookie_last_date = 0;
3354 }
3355
3356 /* Here, we'll look for the first running server which supports the cookie.
3357 * This allows to share a same cookie between several servers, for example
3358 * to dedicate backup servers to specific servers only.
3359 * However, to prevent clients from sticking to cookie-less backup server
3360 * when they have incidentely learned an empty cookie, we simply ignore
3361 * empty cookies and mark them as invalid.
3362 * The same behaviour is applied when persistence must be ignored.
3363 */
3364 if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
3365 srv = NULL;
3366
3367 while (srv) {
3368 if (srv->cookie && (srv->cklen == delim - val_beg) &&
3369 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
3370 if ((srv->cur_state != SRV_ST_STOPPED) ||
3371 (s->be->options & PR_O_PERSIST) ||
3372 (s->flags & SF_FORCE_PRST)) {
3373 /* we found the server and we can use it */
3374 txn->flags &= ~TX_CK_MASK;
3375 txn->flags |= (srv->cur_state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
3376 s->flags |= SF_DIRECT | SF_ASSIGNED;
3377 s->target = &srv->obj_type;
3378 break;
3379 } else {
3380 /* we found a server, but it's down,
3381 * mark it as such and go on in case
3382 * another one is available.
3383 */
3384 txn->flags &= ~TX_CK_MASK;
3385 txn->flags |= TX_CK_DOWN;
3386 }
3387 }
3388 srv = srv->next;
3389 }
3390
3391 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
3392 /* no server matched this cookie or we deliberately skipped it */
3393 txn->flags &= ~TX_CK_MASK;
3394 if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
3395 txn->flags |= TX_CK_UNUSED;
3396 else
3397 txn->flags |= TX_CK_INVALID;
3398 }
3399
3400 /* depending on the cookie mode, we may have to either :
3401 * - delete the complete cookie if we're in insert+indirect mode, so that
3402 * the server never sees it ;
3403 * - remove the server id from the cookie value, and tag the cookie as an
Joseph Herlante9d5c722018-11-25 11:00:25 -08003404 * application cookie so that it does not get accidentally removed later,
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003405 * if we're in cookie prefix mode
3406 */
3407 if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
3408 int delta; /* negative */
3409
3410 memmove(val_beg, delim + 1, hdr_end - (delim + 1));
3411 delta = val_beg - (delim + 1);
3412 val_end += delta;
3413 next += delta;
3414 hdr_end += delta;
3415 del_from = NULL;
3416 preserve_hdr = 1; /* we want to keep this cookie */
3417 }
3418 else if (del_from == NULL &&
3419 (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
3420 del_from = prev;
3421 }
3422 }
3423 else {
3424 /* This is not our cookie, so we must preserve it. But if we already
3425 * scheduled another cookie for removal, we cannot remove the
3426 * complete header, but we can remove the previous block itself.
3427 */
3428 preserve_hdr = 1;
3429
3430 if (del_from != NULL) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003431 int delta = http_del_hdr_value(hdr_beg, hdr_end, &del_from, prev);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003432 if (att_beg >= del_from)
3433 att_beg += delta;
3434 if (att_end >= del_from)
3435 att_end += delta;
3436 val_beg += delta;
3437 val_end += delta;
3438 next += delta;
3439 hdr_end += delta;
3440 prev = del_from;
3441 del_from = NULL;
3442 }
3443 }
3444
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003445 } /* for each cookie */
3446
3447
3448 /* There are no more cookies on this line.
3449 * We may still have one (or several) marked for deletion at the
3450 * end of the line. We must do this now in two ways :
3451 * - if some cookies must be preserved, we only delete from the
3452 * mark to the end of line ;
3453 * - if nothing needs to be preserved, simply delete the whole header
3454 */
3455 if (del_from) {
3456 hdr_end = (preserve_hdr ? del_from : hdr_beg);
3457 }
3458 if ((hdr_end - hdr_beg) != ctx.value.len) {
Christopher Faulet3e2638e2019-06-18 09:49:16 +02003459 if (hdr_beg != hdr_end)
3460 htx_change_blk_value_len(htx, ctx.blk, hdr_end - hdr_beg);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003461 else
3462 http_remove_header(htx, &ctx);
3463 }
3464 } /* for each "Cookie header */
3465}
3466
3467/*
3468 * Manage server-side cookies. It can impact performance by about 2% so it is
3469 * desirable to call it only when needed. This function is also used when we
3470 * just need to know if there is a cookie (eg: for check-cache).
3471 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003472static void http_manage_server_side_cookies(struct stream *s, struct channel *res)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003473{
3474 struct session *sess = s->sess;
3475 struct http_txn *txn = s->txn;
3476 struct htx *htx;
3477 struct http_hdr_ctx ctx;
3478 struct server *srv;
3479 char *hdr_beg, *hdr_end;
3480 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau6f7a02a2019-04-15 21:49:49 +02003481 int is_cookie2 = 0;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003482
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003483 htx = htxbuf(&res->buf);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003484
3485 ctx.blk = NULL;
3486 while (1) {
Olivier Houchardf0f42382019-07-22 17:43:46 +02003487 int is_first = 1;
3488
Andrew McDermott86032c32022-02-11 18:26:49 +00003489 if (is_cookie2 || !http_find_header(htx, ist("Set-Cookie"), &ctx, 1)) {
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003490 if (!http_find_header(htx, ist("Set-Cookie2"), &ctx, 1))
3491 break;
3492 is_cookie2 = 1;
3493 }
3494
3495 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
3496 * <prev> points to the colon.
3497 */
3498 txn->flags |= TX_SCK_PRESENT;
3499
3500 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
3501 * check-cache is enabled) and we are not interested in checking
3502 * them. Warning, the cookie capture is declared in the frontend.
3503 */
3504 if (s->be->cookie_name == NULL && sess->fe->capture_name == NULL)
3505 break;
3506
3507 /* OK so now we know we have to process this response cookie.
3508 * The format of the Set-Cookie header is slightly different
3509 * from the format of the Cookie header in that it does not
3510 * support the comma as a cookie delimiter (thus the header
3511 * cannot be folded) because the Expires attribute described in
3512 * the original Netscape's spec may contain an unquoted date
3513 * with a comma inside. We have to live with this because
3514 * many browsers don't support Max-Age and some browsers don't
3515 * support quoted strings. However the Set-Cookie2 header is
3516 * clean.
3517 *
3518 * We have to keep multiple pointers in order to support cookie
3519 * removal at the beginning, middle or end of header without
3520 * corrupting the header (in case of set-cookie2). A special
3521 * pointer, <scav> points to the beginning of the set-cookie-av
3522 * fields after the first semi-colon. The <next> pointer points
3523 * either to the end of line (set-cookie) or next unquoted comma
3524 * (set-cookie2). All of these headers are valid :
3525 *
3526 * hdr_beg hdr_end
3527 * | |
3528 * v |
3529 * NAME1 = VALUE 1 ; Secure; Path="/" |
3530 * NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT v
3531 * NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT
3532 * NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard
3533 * | | | | | | | |
3534 * | | | | | | | +-> next
3535 * | | | | | | +------------> scav
3536 * | | | | | +--------------> val_end
3537 * | | | | +--------------------> val_beg
3538 * | | | +----------------------> equal
3539 * | | +------------------------> att_end
3540 * | +----------------------------> att_beg
3541 * +------------------------------> prev
3542 * -------------------------------> hdr_beg
3543 */
3544 hdr_beg = ctx.value.ptr;
3545 hdr_end = hdr_beg + ctx.value.len;
3546 for (prev = hdr_beg; prev < hdr_end; prev = next) {
3547
3548 /* Iterate through all cookies on this line */
3549
3550 /* find att_beg */
3551 att_beg = prev;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003552 if (!is_first)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003553 att_beg++;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003554 is_first = 0;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003555
3556 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
3557 att_beg++;
3558
3559 /* find att_end : this is the first character after the last non
3560 * space before the equal. It may be equal to hdr_end.
3561 */
3562 equal = att_end = att_beg;
3563
3564 while (equal < hdr_end) {
3565 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
3566 break;
3567 if (HTTP_IS_SPHT(*equal++))
3568 continue;
3569 att_end = equal;
3570 }
3571
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003572 /* here, <equal> points to '=', a delimiter or the end. <att_end>
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003573 * is between <att_beg> and <equal>, both may be identical.
3574 */
3575
3576 /* look for end of cookie if there is an equal sign */
3577 if (equal < hdr_end && *equal == '=') {
3578 /* look for the beginning of the value */
3579 val_beg = equal + 1;
3580 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
3581 val_beg++;
3582
3583 /* find the end of the value, respecting quotes */
3584 next = http_find_cookie_value_end(val_beg, hdr_end);
3585
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003586 /* make val_end point to the first white space or delimiter after the value */
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003587 val_end = next;
3588 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
3589 val_end--;
3590 }
3591 else {
3592 /* <equal> points to next comma, semi-colon or EOL */
3593 val_beg = val_end = next = equal;
3594 }
3595
3596 if (next < hdr_end) {
3597 /* Set-Cookie2 supports multiple cookies, and <next> points to
3598 * a colon or semi-colon before the end. So skip all attr-value
3599 * pairs and look for the next comma. For Set-Cookie, since
3600 * commas are permitted in values, skip to the end.
3601 */
3602 if (is_cookie2)
3603 next = http_find_hdr_value_end(next, hdr_end);
3604 else
3605 next = hdr_end;
3606 }
3607
3608 /* Now everything is as on the diagram above */
3609
3610 /* Ignore cookies with no equal sign */
3611 if (equal == val_end)
3612 continue;
3613
3614 /* If there are spaces around the equal sign, we need to
3615 * strip them otherwise we'll get trouble for cookie captures,
3616 * or even for rewrites. Since this happens extremely rarely,
3617 * it does not hurt performance.
3618 */
3619 if (unlikely(att_end != equal || val_beg > equal + 1)) {
3620 int stripped_before = 0;
3621 int stripped_after = 0;
3622
3623 if (att_end != equal) {
3624 memmove(att_end, equal, hdr_end - equal);
3625 stripped_before = (att_end - equal);
3626 equal += stripped_before;
3627 val_beg += stripped_before;
3628 }
3629
3630 if (val_beg > equal + 1) {
3631 memmove(equal + 1, val_beg, hdr_end + stripped_before - val_beg);
3632 stripped_after = (equal + 1) - val_beg;
3633 val_beg += stripped_after;
3634 stripped_before += stripped_after;
3635 }
3636
3637 val_end += stripped_before;
3638 next += stripped_before;
3639 hdr_end += stripped_before;
3640
Christopher Faulet3e2638e2019-06-18 09:49:16 +02003641 htx_change_blk_value_len(htx, ctx.blk, hdr_end - hdr_beg);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003642 ctx.value.len = hdr_end - hdr_beg;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003643 }
3644
3645 /* First, let's see if we want to capture this cookie. We check
3646 * that we don't already have a server side cookie, because we
3647 * can only capture one. Also as an optimisation, we ignore
3648 * cookies shorter than the declared name.
3649 */
3650 if (sess->fe->capture_name != NULL &&
3651 txn->srv_cookie == NULL &&
3652 (val_end - att_beg >= sess->fe->capture_namelen) &&
3653 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
3654 int log_len = val_end - att_beg;
3655 if ((txn->srv_cookie = pool_alloc(pool_head_capture)) == NULL) {
3656 ha_alert("HTTP logging : out of memory.\n");
3657 }
3658 else {
3659 if (log_len > sess->fe->capture_len)
3660 log_len = sess->fe->capture_len;
3661 memcpy(txn->srv_cookie, att_beg, log_len);
3662 txn->srv_cookie[log_len] = 0;
3663 }
3664 }
3665
3666 srv = objt_server(s->target);
3667 /* now check if we need to process it for persistence */
3668 if (!(s->flags & SF_IGNORE_PRST) &&
3669 (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
3670 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
3671 /* assume passive cookie by default */
3672 txn->flags &= ~TX_SCK_MASK;
3673 txn->flags |= TX_SCK_FOUND;
3674
3675 /* If the cookie is in insert mode on a known server, we'll delete
3676 * this occurrence because we'll insert another one later.
3677 * We'll delete it too if the "indirect" option is set and we're in
3678 * a direct access.
3679 */
3680 if (s->be->ck_opts & PR_CK_PSV) {
3681 /* The "preserve" flag was set, we don't want to touch the
3682 * server's cookie.
3683 */
3684 }
3685 else if ((srv && (s->be->ck_opts & PR_CK_INS)) ||
3686 ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) {
3687 /* this cookie must be deleted */
3688 if (prev == hdr_beg && next == hdr_end) {
3689 /* whole header */
3690 http_remove_header(htx, &ctx);
3691 /* note: while both invalid now, <next> and <hdr_end>
3692 * are still equal, so the for() will stop as expected.
3693 */
3694 } else {
3695 /* just remove the value */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003696 int delta = http_del_hdr_value(hdr_beg, hdr_end, &prev, next);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003697 next = prev;
3698 hdr_end += delta;
3699 }
3700 txn->flags &= ~TX_SCK_MASK;
3701 txn->flags |= TX_SCK_DELETED;
3702 /* and go on with next cookie */
3703 }
3704 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) {
3705 /* replace bytes val_beg->val_end with the cookie name associated
3706 * with this server since we know it.
3707 */
3708 int sliding, delta;
3709
3710 ctx.value = ist2(val_beg, val_end - val_beg);
3711 ctx.lws_before = ctx.lws_after = 0;
3712 http_replace_header_value(htx, &ctx, ist2(srv->cookie, srv->cklen));
3713 delta = srv->cklen - (val_end - val_beg);
3714 sliding = (ctx.value.ptr - val_beg);
3715 hdr_beg += sliding;
3716 val_beg += sliding;
3717 next += sliding + delta;
3718 hdr_end += sliding + delta;
3719
3720 txn->flags &= ~TX_SCK_MASK;
3721 txn->flags |= TX_SCK_REPLACED;
3722 }
3723 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) {
3724 /* insert the cookie name associated with this server
3725 * before existing cookie, and insert a delimiter between them..
3726 */
3727 int sliding, delta;
3728 ctx.value = ist2(val_beg, 0);
3729 ctx.lws_before = ctx.lws_after = 0;
3730 http_replace_header_value(htx, &ctx, ist2(srv->cookie, srv->cklen + 1));
3731 delta = srv->cklen + 1;
3732 sliding = (ctx.value.ptr - val_beg);
3733 hdr_beg += sliding;
3734 val_beg += sliding;
3735 next += sliding + delta;
3736 hdr_end += sliding + delta;
3737
3738 val_beg[srv->cklen] = COOKIE_DELIM;
3739 txn->flags &= ~TX_SCK_MASK;
3740 txn->flags |= TX_SCK_REPLACED;
3741 }
3742 }
3743 /* that's done for this cookie, check the next one on the same
3744 * line when next != hdr_end (only if is_cookie2).
3745 */
3746 }
3747 }
3748}
3749
Christopher Faulet25a02f62018-10-24 12:00:25 +02003750/*
3751 * Parses the Cache-Control and Pragma request header fields to determine if
3752 * the request may be served from the cache and/or if it is cacheable. Updates
3753 * s->txn->flags.
3754 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003755void http_check_request_for_cacheability(struct stream *s, struct channel *req)
Christopher Faulet25a02f62018-10-24 12:00:25 +02003756{
3757 struct http_txn *txn = s->txn;
3758 struct htx *htx;
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003759 struct http_hdr_ctx ctx = { .blk = NULL };
3760 int pragma_found, cc_found;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003761
3762 if ((txn->flags & (TX_CACHEABLE|TX_CACHE_IGNORE)) == TX_CACHE_IGNORE)
3763 return; /* nothing more to do here */
3764
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003765 htx = htxbuf(&req->buf);
Christopher Faulet25a02f62018-10-24 12:00:25 +02003766 pragma_found = cc_found = 0;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003767
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003768 /* Check "pragma" header for HTTP/1.0 compatibility. */
3769 if (http_find_header(htx, ist("pragma"), &ctx, 1)) {
3770 if (isteqi(ctx.value, ist("no-cache"))) {
3771 pragma_found = 1;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003772 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003773 }
Christopher Faulet25a02f62018-10-24 12:00:25 +02003774
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003775 ctx.blk = NULL;
3776 /* Don't use the cache and don't try to store if we found the
3777 * Authorization header */
3778 if (http_find_header(htx, ist("authorization"), &ctx, 1)) {
3779 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3780 txn->flags |= TX_CACHE_IGNORE;
3781 }
Christopher Faulet25a02f62018-10-24 12:00:25 +02003782
Christopher Faulet25a02f62018-10-24 12:00:25 +02003783
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003784 /* Look for "cache-control" header and iterate over all the values
3785 * until we find one that specifies that caching is possible or not. */
3786 ctx.blk = NULL;
3787 while (http_find_header(htx, ist("cache-control"), &ctx, 0)) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003788 cc_found = 1;
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003789 /* We don't check the values after max-age, max-stale nor min-fresh,
3790 * we simply don't use the cache when they're specified. */
3791 if (istmatchi(ctx.value, ist("max-age")) ||
3792 istmatchi(ctx.value, ist("no-cache")) ||
3793 istmatchi(ctx.value, ist("max-stale")) ||
3794 istmatchi(ctx.value, ist("min-fresh"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003795 txn->flags |= TX_CACHE_IGNORE;
3796 continue;
3797 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003798 if (istmatchi(ctx.value, ist("no-store"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003799 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3800 continue;
3801 }
3802 }
3803
3804 /* RFC7234#5.4:
3805 * When the Cache-Control header field is also present and
3806 * understood in a request, Pragma is ignored.
3807 * When the Cache-Control header field is not present in a
3808 * request, caches MUST consider the no-cache request
3809 * pragma-directive as having the same effect as if
3810 * "Cache-Control: no-cache" were present.
3811 */
3812 if (!cc_found && pragma_found)
3813 txn->flags |= TX_CACHE_IGNORE;
3814}
3815
3816/*
3817 * Check if response is cacheable or not. Updates s->txn->flags.
3818 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003819void http_check_response_for_cacheability(struct stream *s, struct channel *res)
Christopher Faulet25a02f62018-10-24 12:00:25 +02003820{
3821 struct http_txn *txn = s->txn;
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003822 struct http_hdr_ctx ctx = { .blk = NULL };
Christopher Faulet25a02f62018-10-24 12:00:25 +02003823 struct htx *htx;
Remi Tricot-Le Bretoncc9bf2e2020-11-12 11:14:41 +01003824 int has_freshness_info = 0;
3825 int has_validator = 0;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003826
3827 if (txn->status < 200) {
3828 /* do not try to cache interim responses! */
3829 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3830 return;
3831 }
3832
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003833 htx = htxbuf(&res->buf);
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003834 /* Check "pragma" header for HTTP/1.0 compatibility. */
3835 if (http_find_header(htx, ist("pragma"), &ctx, 1)) {
3836 if (isteqi(ctx.value, ist("no-cache"))) {
3837 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3838 return;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003839 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003840 }
Christopher Faulet25a02f62018-10-24 12:00:25 +02003841
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003842 /* Look for "cache-control" header and iterate over all the values
3843 * until we find one that specifies that caching is possible or not. */
3844 ctx.blk = NULL;
3845 while (http_find_header(htx, ist("cache-control"), &ctx, 0)) {
3846 if (isteqi(ctx.value, ist("public"))) {
3847 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003848 continue;
3849 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003850 if (isteqi(ctx.value, ist("private")) ||
3851 isteqi(ctx.value, ist("no-cache")) ||
3852 isteqi(ctx.value, ist("no-store")) ||
3853 isteqi(ctx.value, ist("max-age=0")) ||
3854 isteqi(ctx.value, ist("s-maxage=0"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003855 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003856 continue;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003857 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003858 /* We might have a no-cache="set-cookie" form. */
3859 if (istmatchi(ctx.value, ist("no-cache=\"set-cookie"))) {
3860 txn->flags &= ~TX_CACHE_COOK;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003861 continue;
3862 }
Remi Tricot-Le Bretoncc9bf2e2020-11-12 11:14:41 +01003863
3864 if (istmatchi(ctx.value, ist("s-maxage")) ||
3865 istmatchi(ctx.value, ist("max-age"))) {
3866 has_freshness_info = 1;
3867 continue;
3868 }
3869 }
3870
3871 /* If no freshness information could be found in Cache-Control values,
3872 * look for an Expires header. */
3873 if (!has_freshness_info) {
3874 ctx.blk = NULL;
3875 has_freshness_info = http_find_header(htx, ist("expires"), &ctx, 0);
Christopher Faulet25a02f62018-10-24 12:00:25 +02003876 }
Remi Tricot-Le Bretoncc9bf2e2020-11-12 11:14:41 +01003877
3878 /* If no freshness information could be found in Cache-Control or Expires
3879 * values, look for an explicit validator. */
3880 if (!has_freshness_info) {
3881 ctx.blk = NULL;
3882 has_validator = 1;
3883 if (!http_find_header(htx, ist("etag"), &ctx, 0)) {
3884 ctx.blk = NULL;
3885 if (!http_find_header(htx, ist("last-modified"), &ctx, 0))
3886 has_validator = 0;
3887 }
3888 }
3889
3890 /* We won't store an entry that has neither a cache validator nor an
3891 * explicit expiration time, as suggested in RFC 7234#3. */
3892 if (!has_freshness_info && !has_validator)
3893 txn->flags |= TX_CACHE_IGNORE;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003894}
3895
Christopher Faulet377c5a52018-10-24 21:21:30 +02003896/*
3897 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
3898 * for the current backend.
3899 *
3900 * It is assumed that the request is either a HEAD, GET, or POST and that the
3901 * uri_auth field is valid.
3902 *
3903 * Returns 1 if stats should be provided, otherwise 0.
3904 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003905static int http_stats_check_uri(struct stream *s, struct http_txn *txn, struct proxy *backend)
Christopher Faulet377c5a52018-10-24 21:21:30 +02003906{
3907 struct uri_auth *uri_auth = backend->uri_auth;
3908 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003909 struct htx_sl *sl;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003910 struct ist uri;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003911
3912 if (!uri_auth)
3913 return 0;
3914
3915 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
3916 return 0;
3917
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003918 htx = htxbuf(&s->req.buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02003919 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003920 uri = htx_sl_req_uri(sl);
Willy Tarreau1eb3b482019-10-31 15:50:28 +01003921 if (*uri_auth->uri_prefix == '/')
3922 uri = http_get_path(uri);
Christopher Faulet377c5a52018-10-24 21:21:30 +02003923
3924 /* check URI size */
3925 if (uri_auth->uri_len > uri.len)
3926 return 0;
3927
3928 if (memcmp(uri.ptr, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
3929 return 0;
3930
3931 return 1;
3932}
3933
3934/* This function prepares an applet to handle the stats. It can deal with the
3935 * "100-continue" expectation, check that admin rules are met for POST requests,
3936 * and program a response message if something was unexpected. It cannot fail
3937 * and always relies on the stats applet to complete the job. It does not touch
3938 * analysers nor counters, which are left to the caller. It does not touch
3939 * s->target which is supposed to already point to the stats applet. The caller
3940 * is expected to have already assigned an appctx to the stream.
3941 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003942static int http_handle_stats(struct stream *s, struct channel *req)
Christopher Faulet377c5a52018-10-24 21:21:30 +02003943{
3944 struct stats_admin_rule *stats_admin_rule;
3945 struct stream_interface *si = &s->si[1];
3946 struct session *sess = s->sess;
3947 struct http_txn *txn = s->txn;
3948 struct http_msg *msg = &txn->req;
3949 struct uri_auth *uri_auth = s->be->uri_auth;
3950 const char *h, *lookup, *end;
3951 struct appctx *appctx;
3952 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003953 struct htx_sl *sl;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003954
3955 appctx = si_appctx(si);
3956 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
3957 appctx->st1 = appctx->st2 = 0;
3958 appctx->ctx.stats.st_code = STAT_STATUS_INIT;
Willy Tarreau676c29e2019-10-09 10:50:01 +02003959 appctx->ctx.stats.flags |= uri_auth->flags;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003960 appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */
3961 if ((msg->flags & HTTP_MSGF_VER_11) && (txn->meth != HTTP_METH_HEAD))
3962 appctx->ctx.stats.flags |= STAT_CHUNKED;
3963
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003964 htx = htxbuf(&req->buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02003965 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003966 lookup = HTX_SL_REQ_UPTR(sl) + uri_auth->uri_len;
3967 end = HTX_SL_REQ_UPTR(sl) + HTX_SL_REQ_ULEN(sl);
Christopher Faulet377c5a52018-10-24 21:21:30 +02003968
3969 for (h = lookup; h <= end - 3; h++) {
3970 if (memcmp(h, ";up", 3) == 0) {
3971 appctx->ctx.stats.flags |= STAT_HIDE_DOWN;
3972 break;
3973 }
Amaury Denoyelle91e55ea2021-02-25 14:46:08 +01003974 }
3975
3976 for (h = lookup; h <= end - 9; h++) {
3977 if (memcmp(h, ";no-maint", 9) == 0) {
Willy Tarreau3e320362020-10-23 17:28:57 +02003978 appctx->ctx.stats.flags |= STAT_HIDE_MAINT;
3979 break;
3980 }
Christopher Faulet377c5a52018-10-24 21:21:30 +02003981 }
3982
3983 if (uri_auth->refresh) {
3984 for (h = lookup; h <= end - 10; h++) {
3985 if (memcmp(h, ";norefresh", 10) == 0) {
3986 appctx->ctx.stats.flags |= STAT_NO_REFRESH;
3987 break;
3988 }
3989 }
3990 }
3991
3992 for (h = lookup; h <= end - 4; h++) {
3993 if (memcmp(h, ";csv", 4) == 0) {
Christopher Faulet6338a082019-09-09 15:50:54 +02003994 appctx->ctx.stats.flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
Christopher Faulet377c5a52018-10-24 21:21:30 +02003995 break;
3996 }
3997 }
3998
3999 for (h = lookup; h <= end - 6; h++) {
4000 if (memcmp(h, ";typed", 6) == 0) {
Christopher Faulet6338a082019-09-09 15:50:54 +02004001 appctx->ctx.stats.flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
Christopher Faulet377c5a52018-10-24 21:21:30 +02004002 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
4003 break;
4004 }
4005 }
4006
Christopher Faulet6338a082019-09-09 15:50:54 +02004007 for (h = lookup; h <= end - 5; h++) {
4008 if (memcmp(h, ";json", 5) == 0) {
4009 appctx->ctx.stats.flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
4010 appctx->ctx.stats.flags |= STAT_FMT_JSON;
4011 break;
4012 }
4013 }
4014
4015 for (h = lookup; h <= end - 12; h++) {
4016 if (memcmp(h, ";json-schema", 12) == 0) {
4017 appctx->ctx.stats.flags &= ~STAT_FMT_MASK;
4018 appctx->ctx.stats.flags |= STAT_JSON_SCHM;
4019 break;
4020 }
4021 }
4022
Christopher Faulet377c5a52018-10-24 21:21:30 +02004023 for (h = lookup; h <= end - 8; h++) {
4024 if (memcmp(h, ";st=", 4) == 0) {
4025 int i;
4026 h += 4;
4027 appctx->ctx.stats.st_code = STAT_STATUS_UNKN;
4028 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
4029 if (strncmp(stat_status_codes[i], h, 4) == 0) {
4030 appctx->ctx.stats.st_code = i;
4031 break;
4032 }
4033 }
4034 break;
4035 }
4036 }
4037
4038 appctx->ctx.stats.scope_str = 0;
4039 appctx->ctx.stats.scope_len = 0;
4040 for (h = lookup; h <= end - 8; h++) {
4041 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
4042 int itx = 0;
4043 const char *h2;
4044 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
4045 const char *err;
4046
4047 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
4048 h2 = h;
Christopher Fauleted7a0662019-01-14 11:07:34 +01004049 appctx->ctx.stats.scope_str = h2 - HTX_SL_REQ_UPTR(sl);
4050 while (h < end) {
Christopher Faulet377c5a52018-10-24 21:21:30 +02004051 if (*h == ';' || *h == '&' || *h == ' ')
4052 break;
4053 itx++;
4054 h++;
4055 }
4056
4057 if (itx > STAT_SCOPE_TXT_MAXLEN)
4058 itx = STAT_SCOPE_TXT_MAXLEN;
4059 appctx->ctx.stats.scope_len = itx;
4060
4061 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
4062 memcpy(scope_txt, h2, itx);
4063 scope_txt[itx] = '\0';
4064 err = invalid_char(scope_txt);
4065 if (err) {
4066 /* bad char in search text => clear scope */
4067 appctx->ctx.stats.scope_str = 0;
4068 appctx->ctx.stats.scope_len = 0;
4069 }
4070 break;
4071 }
4072 }
4073
4074 /* now check whether we have some admin rules for this request */
4075 list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) {
4076 int ret = 1;
4077
4078 if (stats_admin_rule->cond) {
4079 ret = acl_exec_cond(stats_admin_rule->cond, s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
4080 ret = acl_pass(ret);
4081 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
4082 ret = !ret;
4083 }
4084
4085 if (ret) {
4086 /* no rule, or the rule matches */
4087 appctx->ctx.stats.flags |= STAT_ADMIN;
4088 break;
4089 }
4090 }
4091
Christopher Faulet5d45e382019-02-27 15:15:23 +01004092 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
4093 appctx->st0 = STAT_HTTP_HEAD;
4094 else if (txn->meth == HTTP_METH_POST) {
Christopher Fauletbd9e8422019-08-15 22:26:48 +02004095 if (appctx->ctx.stats.flags & STAT_ADMIN) {
Christopher Faulet377c5a52018-10-24 21:21:30 +02004096 appctx->st0 = STAT_HTTP_POST;
Christopher Fauletbd9e8422019-08-15 22:26:48 +02004097 if (msg->msg_state < HTTP_MSG_DATA)
4098 req->analysers |= AN_REQ_HTTP_BODY;
4099 }
Christopher Faulet377c5a52018-10-24 21:21:30 +02004100 else {
Christopher Faulet5d45e382019-02-27 15:15:23 +01004101 /* POST without admin level */
Christopher Faulet377c5a52018-10-24 21:21:30 +02004102 appctx->ctx.stats.flags &= ~STAT_CHUNKED;
4103 appctx->ctx.stats.st_code = STAT_STATUS_DENY;
4104 appctx->st0 = STAT_HTTP_LAST;
4105 }
4106 }
4107 else {
Christopher Faulet5d45e382019-02-27 15:15:23 +01004108 /* Unsupported method */
4109 appctx->ctx.stats.flags &= ~STAT_CHUNKED;
4110 appctx->ctx.stats.st_code = STAT_STATUS_IVAL;
4111 appctx->st0 = STAT_HTTP_LAST;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004112 }
4113
4114 s->task->nice = -32; /* small boost for HTTP statistics */
4115 return 1;
4116}
4117
Christopher Faulet021a8e42021-03-29 10:46:38 +02004118/* This function waits for the message payload at most <time> milliseconds (may
4119 * be set to TICK_ETERNITY). It stops to wait if at least <bytes> bytes of the
4120 * payload are received (0 means no limit). It returns HTTP_RULE_* depending on
4121 * the result:
4122 *
4123 * - HTTP_RULE_RES_CONT when conditions are met to stop waiting
4124 * - HTTP_RULE_RES_YIELD to wait for more data
Ilya Shipitsinb2be9a12021-04-24 13:25:42 +05004125 * - HTTP_RULE_RES_ABRT when a timeout occurred.
Christopher Faulet021a8e42021-03-29 10:46:38 +02004126 * - HTTP_RULE_RES_BADREQ if a parsing error is raised by lower level
Ilya Shipitsinb2be9a12021-04-24 13:25:42 +05004127 * - HTTP_RULE_RES_ERROR if an internal error occurred
Christopher Faulet021a8e42021-03-29 10:46:38 +02004128 *
Ilya Shipitsinb2be9a12021-04-24 13:25:42 +05004129 * If a timeout occurred, this function is responsible to emit the right response
Christopher Faulet021a8e42021-03-29 10:46:38 +02004130 * to the client, depending on the channel (408 on request side, 504 on response
4131 * side). All other errors must be handled by the caller.
4132 */
4133enum rule_result http_wait_for_msg_body(struct stream *s, struct channel *chn,
4134 unsigned int time, unsigned int bytes)
4135{
4136 struct session *sess = s->sess;
4137 struct http_txn *txn = s->txn;
4138 struct http_msg *msg = ((chn->flags & CF_ISRESP) ? &txn->rsp : &txn->req);
4139 struct htx *htx;
4140 enum rule_result ret = HTTP_RULE_RES_CONT;
4141
4142 htx = htxbuf(&chn->buf);
4143
4144 if (htx->flags & HTX_FL_PARSING_ERROR) {
4145 ret = HTTP_RULE_RES_BADREQ;
4146 goto end;
4147 }
4148 if (htx->flags & HTX_FL_PROCESSING_ERROR) {
4149 ret = HTTP_RULE_RES_ERROR;
4150 goto end;
4151 }
4152
4153 /* Do nothing for bodyless and CONNECT requests */
4154 if (txn->meth == HTTP_METH_CONNECT || (msg->flags & HTTP_MSGF_BODYLESS))
4155 goto end;
4156
4157 if (!(chn->flags & CF_ISRESP) && msg->msg_state < HTTP_MSG_DATA) {
4158 if (http_handle_expect_hdr(s, htx, msg) == -1) {
4159 ret = HTTP_RULE_RES_ERROR;
4160 goto end;
4161 }
4162 }
4163
4164 msg->msg_state = HTTP_MSG_DATA;
4165
4166 /* Now we're in HTTP_MSG_DATA. We just need to know if all data have
4167 * been received or if the buffer is full.
4168 */
Christopher Faulet59df8122021-09-23 14:46:32 +02004169 if ((htx->flags & HTX_FL_EOM) ||
4170 htx_get_tail_type(htx) > HTX_BLK_DATA ||
4171 channel_htx_full(chn, htx, global.tune.maxrewrite) ||
4172 si_rx_blocked_room(chn_prod(chn)))
Christopher Faulet021a8e42021-03-29 10:46:38 +02004173 goto end;
4174
4175 if (bytes) {
4176 struct htx_blk *blk;
4177 unsigned int len = 0;
4178
4179 for (blk = htx_get_first_blk(htx); blk; blk = htx_get_next_blk(htx, blk)) {
4180 if (htx_get_blk_type(blk) != HTX_BLK_DATA)
4181 continue;
4182 len += htx_get_blksz(blk);
4183 if (len >= bytes)
4184 goto end;
4185 }
4186 }
4187
4188 if ((chn->flags & CF_READ_TIMEOUT) || tick_is_expired(chn->analyse_exp, now_ms)) {
4189 if (!(chn->flags & CF_ISRESP))
4190 goto abort_req;
4191 goto abort_res;
4192 }
4193
4194 /* we get here if we need to wait for more data */
4195 if (!(chn->flags & (CF_SHUTR | CF_READ_ERROR))) {
4196 if (!tick_isset(chn->analyse_exp))
4197 chn->analyse_exp = tick_add_ifset(now_ms, time);
4198 ret = HTTP_RULE_RES_YIELD;
4199 }
4200
4201 end:
4202 return ret;
4203
4204 abort_req:
4205 txn->status = 408;
4206 if (!(s->flags & SF_ERR_MASK))
4207 s->flags |= SF_ERR_CLITO;
4208 if (!(s->flags & SF_FINST_MASK))
4209 s->flags |= SF_FINST_D;
Willy Tarreau4781b152021-04-06 13:53:36 +02004210 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req);
Christopher Faulet021a8e42021-03-29 10:46:38 +02004211 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02004212 _HA_ATOMIC_INC(&sess->listener->counters->failed_req);
Christopher Faulet021a8e42021-03-29 10:46:38 +02004213 http_reply_and_close(s, txn->status, http_error_message(s));
4214 ret = HTTP_RULE_RES_ABRT;
4215 goto end;
4216
4217 abort_res:
4218 txn->status = 504;
4219 if (!(s->flags & SF_ERR_MASK))
4220 s->flags |= SF_ERR_SRVTO;
4221 if (!(s->flags & SF_FINST_MASK))
4222 s->flags |= SF_FINST_D;
4223 stream_inc_http_fail_ctr(s);
4224 http_reply_and_close(s, txn->status, http_error_message(s));
4225 ret = HTTP_RULE_RES_ABRT;
4226 goto end;
4227}
4228
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004229void http_perform_server_redirect(struct stream *s, struct stream_interface *si)
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004230{
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004231 struct channel *req = &s->req;
4232 struct channel *res = &s->res;
4233 struct server *srv;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004234 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004235 struct htx_sl *sl;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004236 struct ist path, location;
4237 unsigned int flags;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004238
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004239 /*
4240 * Create the location
4241 */
4242 chunk_reset(&trash);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004243
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004244 /* 1: add the server's prefix */
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004245 /* special prefix "/" means don't change URL */
4246 srv = __objt_server(s->target);
4247 if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') {
4248 if (!chunk_memcat(&trash, srv->rdr_pfx, srv->rdr_len))
4249 return;
4250 }
4251
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004252 /* 2: add the request Path */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004253 htx = htxbuf(&req->buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02004254 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004255 path = http_get_path(htx_sl_req_uri(sl));
Tim Duesterhused526372020-03-05 17:56:33 +01004256 if (!isttest(path))
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004257 return;
4258
4259 if (!chunk_memcat(&trash, path.ptr, path.len))
4260 return;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004261 location = ist2(trash.area, trash.data);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004262
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004263 /*
4264 * Create the 302 respone
4265 */
4266 htx = htx_from_buf(&res->buf);
4267 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
4268 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
4269 ist("HTTP/1.1"), ist("302"), ist("Found"));
4270 if (!sl)
4271 goto fail;
4272 sl->info.res.status = 302;
4273 s->txn->status = 302;
4274
4275 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) ||
4276 !htx_add_header(htx, ist("Connection"), ist("close")) ||
4277 !htx_add_header(htx, ist("Content-length"), ist("0")) ||
4278 !htx_add_header(htx, ist("Location"), location))
4279 goto fail;
4280
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004281 if (!htx_add_endof(htx, HTX_BLK_EOH))
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004282 goto fail;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004283
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004284 htx->flags |= HTX_FL_EOM;
Christopher Fauletc20afb82020-01-24 19:16:26 +01004285 htx_to_buf(htx, &res->buf);
Christopher Fauleta72a7e42020-01-28 09:28:11 +01004286 if (!http_forward_proxy_resp(s, 1))
4287 goto fail;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004288
4289 /* return without error. */
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004290 si_shutr(si);
4291 si_shutw(si);
4292 si->err_type = SI_ET_NONE;
4293 si->state = SI_ST_CLO;
4294
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004295 if (!(s->flags & SF_ERR_MASK))
4296 s->flags |= SF_ERR_LOCAL;
4297 if (!(s->flags & SF_FINST_MASK))
4298 s->flags |= SF_FINST_C;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004299
4300 /* FIXME: we should increase a counter of redirects per server and per backend. */
4301 srv_inc_sess_ctr(srv);
4302 srv_set_sess_last(srv);
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004303 return;
4304
4305 fail:
4306 /* If an error occurred, remove the incomplete HTTP response from the
4307 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004308 channel_htx_truncate(res, htx);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004309}
4310
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05004311/* This function terminates the request because it was completely analyzed or
Christopher Fauletf2824e62018-10-01 12:12:37 +02004312 * because an error was triggered during the body forwarding.
4313 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004314static void http_end_request(struct stream *s)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004315{
4316 struct channel *chn = &s->req;
4317 struct http_txn *txn = s->txn;
4318
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004319 DBG_TRACE_ENTER(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004320
Christopher Fauletb42a8b62018-11-19 21:59:00 +01004321 if (unlikely(txn->req.msg_state == HTTP_MSG_ERROR ||
4322 txn->rsp.msg_state == HTTP_MSG_ERROR)) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004323 channel_abort(chn);
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004324 channel_htx_truncate(chn, htxbuf(&chn->buf));
Christopher Fauletf2824e62018-10-01 12:12:37 +02004325 goto end;
4326 }
4327
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004328 if (unlikely(txn->req.msg_state < HTTP_MSG_DONE)) {
4329 DBG_TRACE_DEVEL("waiting end of the request", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004330 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004331 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004332
4333 if (txn->req.msg_state == HTTP_MSG_DONE) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004334 /* No need to read anymore, the request was completely parsed.
4335 * We can shut the read side unless we want to abort_on_close,
4336 * or we have a POST request. The issue with POST requests is
4337 * that some browsers still send a CRLF after the request, and
4338 * this CRLF must be read so that it does not remain in the kernel
4339 * buffers, otherwise a close could cause an RST on some systems
4340 * (eg: Linux).
4341 */
Christopher Faulet769d0e92019-03-22 14:23:18 +01004342 if (!(s->be->options & PR_O_ABRT_CLOSE) && txn->meth != HTTP_METH_POST)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004343 channel_dont_read(chn);
4344
4345 /* if the server closes the connection, we want to immediately react
4346 * and close the socket to save packets and syscalls.
4347 */
4348 s->si[1].flags |= SI_FL_NOHALF;
4349
4350 /* In any case we've finished parsing the request so we must
4351 * disable Nagle when sending data because 1) we're not going
4352 * to shut this side, and 2) the server is waiting for us to
4353 * send pending data.
4354 */
4355 chn->flags |= CF_NEVER_WAIT;
4356
Christopher Fauletd01ce402019-01-02 17:44:13 +01004357 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
4358 /* The server has not finished to respond, so we
4359 * don't want to move in order not to upset it.
4360 */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004361 DBG_TRACE_DEVEL("waiting end of the response", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletd01ce402019-01-02 17:44:13 +01004362 return;
4363 }
4364
Christopher Fauletf2824e62018-10-01 12:12:37 +02004365 /* When we get here, it means that both the request and the
4366 * response have finished receiving. Depending on the connection
4367 * mode, we'll have to wait for the last bytes to leave in either
4368 * direction, and sometimes for a close to be effective.
4369 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02004370 if (txn->flags & TX_CON_WANT_TUN) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004371 /* Tunnel mode will not have any analyser so it needs to
4372 * poll for reads.
4373 */
4374 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004375 if (b_data(&chn->buf)) {
4376 DBG_TRACE_DEVEL("waiting to flush the request", STRM_EV_HTTP_ANA, s, txn);
Christopher Faulet9768c262018-10-22 09:34:31 +02004377 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004378 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004379 txn->req.msg_state = HTTP_MSG_TUNNEL;
4380 }
4381 else {
4382 /* we're not expecting any new data to come for this
4383 * transaction, so we can close it.
Christopher Faulet9768c262018-10-22 09:34:31 +02004384 *
4385 * However, there is an exception if the response
4386 * length is undefined. In this case, we need to wait
4387 * the close from the server. The response will be
4388 * switched in TUNNEL mode until the end.
Christopher Fauletf2824e62018-10-01 12:12:37 +02004389 */
4390 if (!(txn->rsp.flags & HTTP_MSGF_XFER_LEN) &&
4391 txn->rsp.msg_state != HTTP_MSG_CLOSED)
Christopher Faulet9768c262018-10-22 09:34:31 +02004392 goto check_channel_flags;
Christopher Fauletf2824e62018-10-01 12:12:37 +02004393
4394 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4395 channel_shutr_now(chn);
4396 channel_shutw_now(chn);
4397 }
4398 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004399 goto check_channel_flags;
4400 }
4401
4402 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
4403 http_msg_closing:
4404 /* nothing else to forward, just waiting for the output buffer
4405 * to be empty and for the shutw_now to take effect.
4406 */
4407 if (channel_is_empty(chn)) {
4408 txn->req.msg_state = HTTP_MSG_CLOSED;
4409 goto http_msg_closed;
4410 }
4411 else if (chn->flags & CF_SHUTW) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004412 txn->req.msg_state = HTTP_MSG_ERROR;
4413 goto end;
4414 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004415 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004416 return;
4417 }
4418
4419 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
4420 http_msg_closed:
Christopher Fauletf2824e62018-10-01 12:12:37 +02004421 /* if we don't know whether the server will close, we need to hard close */
4422 if (txn->rsp.flags & HTTP_MSGF_XFER_LEN)
4423 s->si[1].flags |= SI_FL_NOLINGER; /* we want to close ASAP */
Christopher Fauletf2824e62018-10-01 12:12:37 +02004424 /* see above in MSG_DONE why we only do this in these states */
Christopher Faulet769d0e92019-03-22 14:23:18 +01004425 if (!(s->be->options & PR_O_ABRT_CLOSE))
Christopher Fauletf2824e62018-10-01 12:12:37 +02004426 channel_dont_read(chn);
4427 goto end;
4428 }
4429
4430 check_channel_flags:
4431 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4432 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4433 /* if we've just closed an output, let's switch */
4434 txn->req.msg_state = HTTP_MSG_CLOSING;
4435 goto http_msg_closing;
4436 }
4437
4438 end:
4439 chn->analysers &= AN_REQ_FLT_END;
Christopher Faulet198ef8b2020-12-15 13:32:55 +01004440 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
4441 chn->flags |= CF_NEVER_WAIT;
4442 if (HAS_REQ_DATA_FILTERS(s))
Christopher Fauletf2824e62018-10-01 12:12:37 +02004443 chn->analysers |= AN_REQ_FLT_XFER_DATA;
Christopher Faulet198ef8b2020-12-15 13:32:55 +01004444 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004445 channel_auto_close(chn);
4446 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004447 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004448}
4449
4450
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05004451/* This function terminates the response because it was completely analyzed or
Christopher Fauletf2824e62018-10-01 12:12:37 +02004452 * because an error was triggered during the body forwarding.
4453 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004454static void http_end_response(struct stream *s)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004455{
4456 struct channel *chn = &s->res;
4457 struct http_txn *txn = s->txn;
4458
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004459 DBG_TRACE_ENTER(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004460
Christopher Fauletb42a8b62018-11-19 21:59:00 +01004461 if (unlikely(txn->req.msg_state == HTTP_MSG_ERROR ||
4462 txn->rsp.msg_state == HTTP_MSG_ERROR)) {
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004463 channel_htx_truncate(&s->req, htxbuf(&s->req.buf));
Christopher Faulet9768c262018-10-22 09:34:31 +02004464 channel_abort(&s->req);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004465 goto end;
4466 }
4467
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004468 if (unlikely(txn->rsp.msg_state < HTTP_MSG_DONE)) {
4469 DBG_TRACE_DEVEL("waiting end of the response", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004470 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004471 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004472
4473 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
4474 /* In theory, we don't need to read anymore, but we must
4475 * still monitor the server connection for a possible close
4476 * while the request is being uploaded, so we don't disable
4477 * reading.
4478 */
4479 /* channel_dont_read(chn); */
4480
4481 if (txn->req.msg_state < HTTP_MSG_DONE) {
4482 /* The client seems to still be sending data, probably
4483 * because we got an error response during an upload.
4484 * We have the choice of either breaking the connection
4485 * or letting it pass through. Let's do the later.
4486 */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004487 DBG_TRACE_DEVEL("waiting end of the request", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004488 return;
4489 }
4490
4491 /* When we get here, it means that both the request and the
4492 * response have finished receiving. Depending on the connection
4493 * mode, we'll have to wait for the last bytes to leave in either
4494 * direction, and sometimes for a close to be effective.
4495 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02004496 if (txn->flags & TX_CON_WANT_TUN) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004497 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004498 if (b_data(&chn->buf)) {
4499 DBG_TRACE_DEVEL("waiting to flush the respone", STRM_EV_HTTP_ANA, s, txn);
Christopher Faulet9768c262018-10-22 09:34:31 +02004500 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004501 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004502 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
4503 }
4504 else {
4505 /* we're not expecting any new data to come for this
4506 * transaction, so we can close it.
4507 */
4508 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4509 channel_shutr_now(chn);
4510 channel_shutw_now(chn);
4511 }
4512 }
4513 goto check_channel_flags;
4514 }
4515
4516 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
4517 http_msg_closing:
4518 /* nothing else to forward, just waiting for the output buffer
4519 * to be empty and for the shutw_now to take effect.
4520 */
4521 if (channel_is_empty(chn)) {
4522 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4523 goto http_msg_closed;
4524 }
4525 else if (chn->flags & CF_SHUTW) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004526 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau4781b152021-04-06 13:53:36 +02004527 _HA_ATOMIC_INC(&strm_sess(s)->fe->fe_counters.cli_aborts);
4528 _HA_ATOMIC_INC(&s->be->be_counters.cli_aborts);
William Lallemand36119de2021-03-08 15:26:48 +01004529 if (strm_sess(s)->listener && strm_sess(s)->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02004530 _HA_ATOMIC_INC(&strm_sess(s)->listener->counters->cli_aborts);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004531 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02004532 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.cli_aborts);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004533 goto end;
4534 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004535 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004536 return;
4537 }
4538
4539 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
4540 http_msg_closed:
4541 /* drop any pending data */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004542 channel_htx_truncate(&s->req, htxbuf(&s->req.buf));
Christopher Faulet9768c262018-10-22 09:34:31 +02004543 channel_abort(&s->req);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004544 goto end;
4545 }
4546
4547 check_channel_flags:
4548 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4549 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4550 /* if we've just closed an output, let's switch */
4551 txn->rsp.msg_state = HTTP_MSG_CLOSING;
4552 goto http_msg_closing;
4553 }
4554
4555 end:
4556 chn->analysers &= AN_RES_FLT_END;
Christopher Faulet198ef8b2020-12-15 13:32:55 +01004557 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
4558 chn->flags |= CF_NEVER_WAIT;
4559 if (HAS_RSP_DATA_FILTERS(s))
4560 chn->analysers |= AN_RES_FLT_XFER_DATA;
4561 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004562 channel_auto_close(chn);
4563 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004564 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004565}
4566
Christopher Fauletef70e252020-01-28 09:26:19 +01004567/* Forward a response generated by HAProxy (error/redirect/return). This
4568 * function forwards all pending incoming data. If <final> is set to 0, nothing
4569 * more is performed. It is used for 1xx informational messages. Otherwise, the
Christopher Faulet507479b2020-05-15 12:29:46 +02004570 * transaction is terminated and the request is emptied. On success 1 is
Christopher Faulet40e6b552020-06-25 16:04:50 +02004571 * returned. If an error occurred, 0 is returned. If it fails, this function
4572 * only exits. It is the caller responsibility to do the cleanup.
Christopher Fauletef70e252020-01-28 09:26:19 +01004573 */
4574int http_forward_proxy_resp(struct stream *s, int final)
4575{
4576 struct channel *req = &s->req;
4577 struct channel *res = &s->res;
4578 struct htx *htx = htxbuf(&res->buf);
4579 size_t data;
4580
4581 if (final) {
4582 htx->flags |= HTX_FL_PROXY_RESP;
Christopher Faulet507479b2020-05-15 12:29:46 +02004583
Christopher Fauletaab1b672020-11-18 16:44:02 +01004584 if (!htx_is_empty(htx) && !http_eval_after_res_rules(s))
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01004585 return 0;
Christopher Fauletef70e252020-01-28 09:26:19 +01004586
Christopher Fauletd6c48362020-10-19 18:01:38 +02004587 if (s->txn->meth == HTTP_METH_HEAD)
4588 htx_skip_msg_payload(htx);
4589
Christopher Fauletef70e252020-01-28 09:26:19 +01004590 channel_auto_read(req);
4591 channel_abort(req);
4592 channel_auto_close(req);
4593 channel_htx_erase(req, htxbuf(&req->buf));
4594
4595 res->wex = tick_add_ifset(now_ms, res->wto);
4596 channel_auto_read(res);
4597 channel_auto_close(res);
4598 channel_shutr_now(res);
Christopher Faulet1a9db7c2020-06-25 15:36:45 +02004599 res->flags |= CF_EOI; /* The response is terminated, add EOI */
Christopher Faulet42432f32020-11-20 17:43:16 +01004600 htxbuf(&res->buf)->flags |= HTX_FL_EOM; /* no more data are expected */
Christopher Fauletef70e252020-01-28 09:26:19 +01004601 }
Christopher Fauletcf6898c2020-06-25 15:55:11 +02004602 else {
4603 /* Send ASAP informational messages. Rely on CF_EOI for final
4604 * response.
4605 */
4606 res->flags |= CF_SEND_DONTWAIT;
4607 }
Christopher Fauletef70e252020-01-28 09:26:19 +01004608
4609 data = htx->data - co_data(res);
4610 c_adv(res, data);
4611 htx->first = -1;
4612 res->total += data;
4613 return 1;
4614}
4615
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004616void http_server_error(struct stream *s, struct stream_interface *si, int err,
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004617 int finst, struct http_reply *msg)
Christopher Faulet0f226952018-10-22 09:29:56 +02004618{
Christopher Faulet72c7d8d2020-01-27 15:32:25 +01004619 http_reply_and_close(s, s->txn->status, msg);
Christopher Faulet0f226952018-10-22 09:29:56 +02004620 if (!(s->flags & SF_ERR_MASK))
4621 s->flags |= err;
4622 if (!(s->flags & SF_FINST_MASK))
4623 s->flags |= finst;
4624}
4625
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004626void http_reply_and_close(struct stream *s, short status, struct http_reply *msg)
Christopher Faulet0f226952018-10-22 09:29:56 +02004627{
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004628 if (!msg) {
4629 channel_htx_truncate(&s->res, htxbuf(&s->res.buf));
4630 goto end;
4631 }
4632
4633 if (http_reply_message(s, msg) == -1) {
4634 /* On error, return a 500 error message, but don't rewrite it if
Christopher Faulet40e6b552020-06-25 16:04:50 +02004635 * it is already an internal error. If it was already a "const"
4636 * 500 error, just fail.
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004637 */
Christopher Faulet40e6b552020-06-25 16:04:50 +02004638 if (s->txn->status == 500) {
4639 if (s->txn->flags & TX_CONST_REPLY)
4640 goto end;
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004641 s->txn->flags |= TX_CONST_REPLY;
Christopher Faulet40e6b552020-06-25 16:04:50 +02004642 }
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004643 s->txn->status = 500;
4644 s->txn->http_reply = NULL;
4645 return http_reply_and_close(s, s->txn->status, http_error_message(s));
4646 }
4647
4648end:
4649 s->res.wex = tick_add_ifset(now_ms, s->res.wto);
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004650
Christopher Fauletdb8ba102021-09-10 09:17:50 +02004651 /* At this staged, HTTP analysis is finished */
4652 s->req.analysers &= AN_REQ_FLT_END;
4653 s->req.analyse_exp = TICK_ETERNITY;
4654
4655 s->res.analysers &= AN_RES_FLT_END;
4656 s->res.analyse_exp = TICK_ETERNITY;
4657
Christopher Faulet0f226952018-10-22 09:29:56 +02004658 channel_auto_read(&s->req);
4659 channel_abort(&s->req);
4660 channel_auto_close(&s->req);
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004661 channel_htx_erase(&s->req, htxbuf(&s->req.buf));
Christopher Faulet72c7d8d2020-01-27 15:32:25 +01004662 channel_auto_read(&s->res);
4663 channel_auto_close(&s->res);
4664 channel_shutr_now(&s->res);
Christopher Faulet0f226952018-10-22 09:29:56 +02004665}
4666
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004667struct http_reply *http_error_message(struct stream *s)
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004668{
4669 const int msgnum = http_get_status_idx(s->txn->status);
4670
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004671 if (s->txn->http_reply)
4672 return s->txn->http_reply;
4673 else if (s->be->replies[msgnum])
4674 return s->be->replies[msgnum];
4675 else if (strm_fe(s)->replies[msgnum])
4676 return strm_fe(s)->replies[msgnum];
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004677 else
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004678 return &http_err_replies[msgnum];
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004679}
4680
Christopher Faulet40e6b552020-06-25 16:04:50 +02004681/* Produces an HTX message from an http reply. Depending on the http reply type,
4682 * a, errorfile, an raw file or a log-format string is used. On success, it
4683 * returns 0. If an error occurs -1 is returned. If it fails, this function only
4684 * exits. It is the caller responsibility to do the cleanup.
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004685 */
Christopher Fauletae43b6c2020-05-27 15:24:22 +02004686int http_reply_to_htx(struct stream *s, struct htx *htx, struct http_reply *reply)
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004687{
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004688 struct buffer *errmsg;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004689 struct htx_sl *sl;
4690 struct buffer *body = NULL;
4691 const char *status, *reason, *clen, *ctype;
4692 unsigned int slflags;
4693 int ret = 0;
4694
Christopher Faulete29a97e2020-05-14 14:49:25 +02004695 /*
4696 * - HTTP_REPLY_ERRFILES unexpected here. handled as no payload if so
4697 *
4698 * - HTTP_REPLY_INDIRECT: switch on another reply if defined or handled
4699 * as no payload if NULL. the TXN status code is set with the status
4700 * of the original reply.
4701 */
4702
4703 if (reply->type == HTTP_REPLY_INDIRECT) {
4704 if (reply->body.reply)
4705 reply = reply->body.reply;
4706 }
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004707 if (reply->type == HTTP_REPLY_ERRMSG && !reply->body.errmsg) {
4708 /* get default error message */
4709 if (reply == s->txn->http_reply)
4710 s->txn->http_reply = NULL;
4711 reply = http_error_message(s);
4712 if (reply->type == HTTP_REPLY_INDIRECT) {
4713 if (reply->body.reply)
4714 reply = reply->body.reply;
4715 }
4716 }
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004717
4718 if (reply->type == HTTP_REPLY_ERRMSG) {
4719 /* implicit or explicit error message*/
4720 errmsg = reply->body.errmsg;
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004721 if (errmsg && !b_is_null(errmsg)) {
Christopher Faulet20567362020-05-15 14:52:49 +02004722 if (!htx_copy_msg(htx, errmsg))
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004723 goto fail;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004724 }
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004725 }
4726 else {
4727 /* no payload, file or log-format string */
4728 if (reply->type == HTTP_REPLY_RAW) {
4729 /* file */
4730 body = &reply->body.obj;
4731 }
4732 else if (reply->type == HTTP_REPLY_LOGFMT) {
4733 /* log-format string */
4734 body = alloc_trash_chunk();
4735 if (!body)
4736 goto fail_alloc;
4737 body->data = build_logline(s, body->area, body->size, &reply->body.fmt);
4738 }
4739 /* else no payload */
4740
4741 status = ultoa(reply->status);
4742 reason = http_get_reason(reply->status);
4743 slflags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_CLEN);
4744 if (!body || !b_data(body))
4745 slflags |= HTX_SL_F_BODYLESS;
4746 sl = htx_add_stline(htx, HTX_BLK_RES_SL, slflags, ist("HTTP/1.1"), ist(status), ist(reason));
4747 if (!sl)
4748 goto fail;
4749 sl->info.res.status = reply->status;
4750
4751 clen = (body ? ultoa(b_data(body)) : "0");
4752 ctype = reply->ctype;
4753
4754 if (!LIST_ISEMPTY(&reply->hdrs)) {
4755 struct http_reply_hdr *hdr;
4756 struct buffer *value = alloc_trash_chunk();
4757
4758 if (!value)
4759 goto fail;
4760
4761 list_for_each_entry(hdr, &reply->hdrs, list) {
4762 chunk_reset(value);
4763 value->data = build_logline(s, value->area, value->size, &hdr->value);
4764 if (b_data(value) && !htx_add_header(htx, hdr->name, ist2(b_head(value), b_data(value)))) {
4765 free_trash_chunk(value);
4766 goto fail;
4767 }
4768 chunk_reset(value);
4769 }
4770 free_trash_chunk(value);
4771 }
4772
4773 if (!htx_add_header(htx, ist("content-length"), ist(clen)) ||
4774 (body && b_data(body) && ctype && !htx_add_header(htx, ist("content-type"), ist(ctype))) ||
4775 !htx_add_endof(htx, HTX_BLK_EOH) ||
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004776 (body && b_data(body) && !htx_add_data_atonce(htx, ist2(b_head(body), b_data(body)))))
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004777 goto fail;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004778
4779 htx->flags |= HTX_FL_EOM;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004780 }
4781
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004782 leave:
4783 if (reply->type == HTTP_REPLY_LOGFMT)
4784 free_trash_chunk(body);
4785 return ret;
4786
4787 fail_alloc:
4788 if (!(s->flags & SF_ERR_MASK))
4789 s->flags |= SF_ERR_RESOURCE;
Christopher Faulet97e466c2020-05-15 15:12:47 +02004790 /* fall through */
4791 fail:
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004792 ret = -1;
4793 goto leave;
Christopher Faulet97e466c2020-05-15 15:12:47 +02004794}
4795
4796/* Send an http reply to the client. On success, it returns 0. If an error
Christopher Faulet40e6b552020-06-25 16:04:50 +02004797 * occurs -1 is returned and the response channel is truncated, removing this
4798 * way the faulty reply. This function may fail when the reply is formatted
4799 * (http_reply_to_htx) or when the reply is forwarded
4800 * (http_forward_proxy_resp). On the last case, it is because a
4801 * http-after-response rule fails.
Christopher Faulet97e466c2020-05-15 15:12:47 +02004802 */
4803int http_reply_message(struct stream *s, struct http_reply *reply)
4804{
4805 struct channel *res = &s->res;
4806 struct htx *htx = htx_from_buf(&res->buf);
4807
4808 if (s->txn->status == -1)
4809 s->txn->status = reply->status;
4810 channel_htx_truncate(res, htx);
4811
4812 if (http_reply_to_htx(s, htx, reply) == -1)
4813 goto fail;
4814
4815 htx_to_buf(htx, &s->res.buf);
4816 if (!http_forward_proxy_resp(s, 1))
4817 goto fail;
4818 return 0;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004819
4820 fail:
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004821 channel_htx_truncate(res, htx);
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004822 if (!(s->flags & SF_ERR_MASK))
4823 s->flags |= SF_ERR_PRXCOND;
Christopher Faulet97e466c2020-05-15 15:12:47 +02004824 return -1;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004825}
4826
Christopher Faulet304cc402019-07-15 15:46:28 +02004827/* Return the error message corresponding to si->err_type. It is assumed
4828 * that the server side is closed. Note that err_type is actually a
4829 * bitmask, where almost only aborts may be cumulated with other
4830 * values. We consider that aborted operations are more important
4831 * than timeouts or errors due to the fact that nobody else in the
4832 * logs might explain incomplete retries. All others should avoid
4833 * being cumulated. It should normally not be possible to have multiple
4834 * aborts at once, but just in case, the first one in sequence is reported.
4835 * Note that connection errors appearing on the second request of a keep-alive
4836 * connection are not reported since this allows the client to retry.
4837 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004838void http_return_srv_error(struct stream *s, struct stream_interface *si)
Christopher Faulet304cc402019-07-15 15:46:28 +02004839{
4840 int err_type = si->err_type;
4841
4842 /* set s->txn->status for http_error_message(s) */
4843 s->txn->status = 503;
4844
4845 if (err_type & SI_ET_QUEUE_ABRT)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004846 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_Q,
4847 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004848 else if (err_type & SI_ET_CONN_ABRT)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004849 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_C,
4850 (s->txn->flags & TX_NOT_FIRST) ? NULL :
4851 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004852 else if (err_type & SI_ET_QUEUE_TO)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004853 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_Q,
4854 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004855 else if (err_type & SI_ET_QUEUE_ERR)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004856 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_Q,
4857 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004858 else if (err_type & SI_ET_CONN_TO)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004859 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_C,
4860 (s->txn->flags & TX_NOT_FIRST) ? NULL :
4861 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004862 else if (err_type & SI_ET_CONN_ERR)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004863 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_C,
4864 (s->flags & SF_SRV_REUSED) ? NULL :
4865 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004866 else if (err_type & SI_ET_CONN_RES)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004867 http_server_error(s, si, SF_ERR_RESOURCE, SF_FINST_C,
4868 (s->txn->flags & TX_NOT_FIRST) ? NULL :
4869 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004870 else { /* SI_ET_CONN_OTHER and others */
4871 s->txn->status = 500;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004872 http_server_error(s, si, SF_ERR_INTERNAL, SF_FINST_C,
4873 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004874 }
4875}
4876
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004877
Christopher Faulet4a28a532019-03-01 11:19:40 +01004878/* Handle Expect: 100-continue for HTTP/1.1 messages if necessary. It returns 0
4879 * on success and -1 on error.
4880 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004881static int http_handle_expect_hdr(struct stream *s, struct htx *htx, struct http_msg *msg)
Christopher Faulet4a28a532019-03-01 11:19:40 +01004882{
4883 /* If we have HTTP/1.1 message with a body and Expect: 100-continue,
4884 * then we must send an HTTP/1.1 100 Continue intermediate response.
4885 */
4886 if (msg->msg_state == HTTP_MSG_BODY && (msg->flags & HTTP_MSGF_VER_11) &&
4887 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
4888 struct ist hdr = { .ptr = "Expect", .len = 6 };
4889 struct http_hdr_ctx ctx;
4890
4891 ctx.blk = NULL;
4892 /* Expect is allowed in 1.1, look for it */
4893 if (http_find_header(htx, hdr, &ctx, 0) &&
4894 unlikely(isteqi(ctx.value, ist2("100-continue", 12)))) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004895 if (http_reply_100_continue(s) == -1)
Christopher Faulet4a28a532019-03-01 11:19:40 +01004896 return -1;
4897 http_remove_header(htx, &ctx);
4898 }
4899 }
4900 return 0;
4901}
4902
Christopher Faulet23a3c792018-11-28 10:01:23 +01004903/* Send a 100-Continue response to the client. It returns 0 on success and -1
4904 * on error. The response channel is updated accordingly.
4905 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004906static int http_reply_100_continue(struct stream *s)
Christopher Faulet23a3c792018-11-28 10:01:23 +01004907{
4908 struct channel *res = &s->res;
4909 struct htx *htx = htx_from_buf(&res->buf);
4910 struct htx_sl *sl;
4911 unsigned int flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|
4912 HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
Christopher Faulet23a3c792018-11-28 10:01:23 +01004913
4914 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
4915 ist("HTTP/1.1"), ist("100"), ist("Continue"));
4916 if (!sl)
4917 goto fail;
4918 sl->info.res.status = 100;
4919
Christopher Faulet1d5ec092019-06-26 14:23:54 +02004920 if (!htx_add_endof(htx, HTX_BLK_EOH))
Christopher Faulet23a3c792018-11-28 10:01:23 +01004921 goto fail;
4922
Christopher Fauleta72a7e42020-01-28 09:28:11 +01004923 if (!http_forward_proxy_resp(s, 0))
4924 goto fail;
Christopher Faulet23a3c792018-11-28 10:01:23 +01004925 return 0;
4926
4927 fail:
4928 /* If an error occurred, remove the incomplete HTTP response from the
4929 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004930 channel_htx_truncate(res, htx);
Christopher Faulet23a3c792018-11-28 10:01:23 +01004931 return -1;
4932}
4933
Christopher Faulet12c51e22018-11-28 15:59:42 +01004934
Christopher Faulet0f226952018-10-22 09:29:56 +02004935/*
4936 * Capture headers from message <htx> according to header list <cap_hdr>, and
4937 * fill the <cap> pointers appropriately.
4938 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004939static void http_capture_headers(struct htx *htx, char **cap, struct cap_hdr *cap_hdr)
Christopher Faulet0f226952018-10-22 09:29:56 +02004940{
4941 struct cap_hdr *h;
4942 int32_t pos;
4943
Christopher Fauleta3f15502019-05-13 15:27:23 +02004944 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet0f226952018-10-22 09:29:56 +02004945 struct htx_blk *blk = htx_get_blk(htx, pos);
4946 enum htx_blk_type type = htx_get_blk_type(blk);
4947 struct ist n, v;
4948
4949 if (type == HTX_BLK_EOH)
4950 break;
4951 if (type != HTX_BLK_HDR)
4952 continue;
4953
4954 n = htx_get_blk_name(htx, blk);
4955
4956 for (h = cap_hdr; h; h = h->next) {
4957 if (h->namelen && (h->namelen == n.len) &&
4958 (strncasecmp(n.ptr, h->name, h->namelen) == 0)) {
4959 if (cap[h->index] == NULL)
4960 cap[h->index] =
4961 pool_alloc(h->pool);
4962
4963 if (cap[h->index] == NULL) {
4964 ha_alert("HTTP capture : out of memory.\n");
4965 break;
4966 }
4967
4968 v = htx_get_blk_value(htx, blk);
4969 if (v.len > h->len)
4970 v.len = h->len;
4971
4972 memcpy(cap[h->index], v.ptr, v.len);
4973 cap[h->index][v.len]=0;
4974 }
4975 }
4976 }
4977}
4978
Christopher Faulet0b6bdc52018-10-24 11:05:36 +02004979/* Delete a value in a header between delimiters <from> and <next>. The header
4980 * itself is delimited by <start> and <end> pointers. The number of characters
4981 * displaced is returned, and the pointer to the first delimiter is updated if
4982 * required. The function tries as much as possible to respect the following
4983 * principles :
4984 * - replace <from> delimiter by the <next> one unless <from> points to <start>,
4985 * in which case <next> is simply removed
4986 * - set exactly one space character after the new first delimiter, unless there
4987 * are not enough characters in the block being moved to do so.
4988 * - remove unneeded spaces before the previous delimiter and after the new
4989 * one.
4990 *
4991 * It is the caller's responsibility to ensure that :
4992 * - <from> points to a valid delimiter or <start> ;
4993 * - <next> points to a valid delimiter or <end> ;
4994 * - there are non-space chars before <from>.
4995 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004996static int http_del_hdr_value(char *start, char *end, char **from, char *next)
Christopher Faulet0b6bdc52018-10-24 11:05:36 +02004997{
4998 char *prev = *from;
4999
5000 if (prev == start) {
5001 /* We're removing the first value. eat the semicolon, if <next>
5002 * is lower than <end> */
5003 if (next < end)
5004 next++;
5005
5006 while (next < end && HTTP_IS_SPHT(*next))
5007 next++;
5008 }
5009 else {
5010 /* Remove useless spaces before the old delimiter. */
5011 while (HTTP_IS_SPHT(*(prev-1)))
5012 prev--;
5013 *from = prev;
5014
5015 /* copy the delimiter and if possible a space if we're
5016 * not at the end of the line.
5017 */
5018 if (next < end) {
5019 *prev++ = *next++;
5020 if (prev + 1 < next)
5021 *prev++ = ' ';
5022 while (next < end && HTTP_IS_SPHT(*next))
5023 next++;
5024 }
5025 }
5026 memmove(prev, next, end - next);
5027 return (prev - next);
5028}
5029
Christopher Faulet0f226952018-10-22 09:29:56 +02005030
5031/* Formats the start line of the request (without CRLF) and puts it in <str> and
Joseph Herlantc42c0e92018-11-25 10:43:27 -08005032 * return the written length. The line can be truncated if it exceeds <len>.
Christopher Faulet0f226952018-10-22 09:29:56 +02005033 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005034static size_t http_fmt_req_line(const struct htx_sl *sl, char *str, size_t len)
Christopher Faulet0f226952018-10-22 09:29:56 +02005035{
5036 struct ist dst = ist2(str, 0);
5037
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005038 if (istcat(&dst, htx_sl_req_meth(sl), len) == -1)
Christopher Faulet0f226952018-10-22 09:29:56 +02005039 goto end;
5040 if (dst.len + 1 > len)
5041 goto end;
5042 dst.ptr[dst.len++] = ' ';
5043
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005044 if (istcat(&dst, htx_sl_req_uri(sl), len) == -1)
Christopher Faulet0f226952018-10-22 09:29:56 +02005045 goto end;
5046 if (dst.len + 1 > len)
5047 goto end;
5048 dst.ptr[dst.len++] = ' ';
5049
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005050 istcat(&dst, htx_sl_req_vsn(sl), len);
Christopher Faulet0f226952018-10-22 09:29:56 +02005051 end:
5052 return dst.len;
5053}
5054
5055/*
5056 * Print a debug line with a start line.
5057 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005058static void http_debug_stline(const char *dir, struct stream *s, const struct htx_sl *sl)
Christopher Faulet0f226952018-10-22 09:29:56 +02005059{
5060 struct session *sess = strm_sess(s);
5061 int max;
5062
5063 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
5064 dir,
Willy Tarreau66182592021-12-06 07:01:02 +00005065 objt_conn(sess->origin) ? (unsigned short)__objt_conn(sess->origin)->handle.fd : -1,
5066 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 +02005067
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005068 max = HTX_SL_P1_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02005069 UBOUND(max, trash.size - trash.data - 3);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005070 chunk_memcat(&trash, HTX_SL_P1_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02005071 trash.area[trash.data++] = ' ';
5072
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005073 max = HTX_SL_P2_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02005074 UBOUND(max, trash.size - trash.data - 2);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005075 chunk_memcat(&trash, HTX_SL_P2_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02005076 trash.area[trash.data++] = ' ';
5077
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005078 max = HTX_SL_P3_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02005079 UBOUND(max, trash.size - trash.data - 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005080 chunk_memcat(&trash, HTX_SL_P3_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02005081 trash.area[trash.data++] = '\n';
5082
Willy Tarreau2e8ab6b2020-03-14 11:03:20 +01005083 DISGUISE(write(1, trash.area, trash.data));
Christopher Faulet0f226952018-10-22 09:29:56 +02005084}
5085
5086/*
5087 * Print a debug line with a header.
5088 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005089static 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 +02005090{
5091 struct session *sess = strm_sess(s);
5092 int max;
5093
5094 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
5095 dir,
Willy Tarreau66182592021-12-06 07:01:02 +00005096 objt_conn(sess->origin) ? (unsigned short)__objt_conn(sess->origin)->handle.fd : -1,
5097 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 +02005098
5099 max = n.len;
5100 UBOUND(max, trash.size - trash.data - 3);
5101 chunk_memcat(&trash, n.ptr, max);
5102 trash.area[trash.data++] = ':';
5103 trash.area[trash.data++] = ' ';
5104
5105 max = v.len;
5106 UBOUND(max, trash.size - trash.data - 1);
5107 chunk_memcat(&trash, v.ptr, max);
5108 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
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005113/* Allocate a new HTTP transaction for stream <s> unless there is one already.
5114 * In case of allocation failure, everything allocated is freed and NULL is
5115 * returned. Otherwise the new transaction is assigned to the stream and
5116 * returned.
5117 */
5118struct http_txn *http_alloc_txn(struct stream *s)
5119{
5120 struct http_txn *txn = s->txn;
5121
5122 if (txn)
5123 return txn;
5124
5125 txn = pool_alloc(pool_head_http_txn);
5126 if (!txn)
5127 return txn;
5128
5129 s->txn = txn;
5130 return txn;
5131}
5132
5133void http_txn_reset_req(struct http_txn *txn)
5134{
Christopher Faulet1aea50e2020-01-17 16:03:53 +01005135 txn->req.flags = 0;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005136 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
5137}
5138
5139void http_txn_reset_res(struct http_txn *txn)
5140{
Christopher Faulet1aea50e2020-01-17 16:03:53 +01005141 txn->rsp.flags = 0;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005142 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
5143}
5144
5145/*
Christopher Faulet75f619a2021-03-08 19:12:58 +01005146 * Create and initialize a new HTTP transaction for stream <s>. This should be
5147 * used before processing any new request. It returns the transaction or NLULL
5148 * on error.
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005149 */
Christopher Faulet75f619a2021-03-08 19:12:58 +01005150struct http_txn *http_create_txn(struct stream *s)
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005151{
Christopher Faulet75f619a2021-03-08 19:12:58 +01005152 struct http_txn *txn;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005153 struct conn_stream *cs = objt_cs(s->si[0].end);
5154
Christopher Faulet75f619a2021-03-08 19:12:58 +01005155 txn = pool_alloc(pool_head_http_txn);
5156 if (!txn)
5157 return NULL;
5158 s->txn = txn;
5159
Christopher Fauletda831fa2020-10-06 17:58:43 +02005160 txn->flags = ((cs && cs->flags & CS_FL_NOT_FIRST) ? TX_NOT_FIRST : 0);
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005161 txn->status = -1;
Christopher Faulet5cb513a2020-05-13 17:56:56 +02005162 txn->http_reply = NULL;
Willy Tarreau8b507582020-02-25 09:35:07 +01005163 write_u32(txn->cache_hash, 0);
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005164
5165 txn->cookie_first_date = 0;
5166 txn->cookie_last_date = 0;
5167
5168 txn->srv_cookie = NULL;
5169 txn->cli_cookie = NULL;
5170 txn->uri = NULL;
5171
5172 http_txn_reset_req(txn);
5173 http_txn_reset_res(txn);
5174
5175 txn->req.chn = &s->req;
5176 txn->rsp.chn = &s->res;
5177
5178 txn->auth.method = HTTP_AUTH_UNKNOWN;
5179
5180 vars_init(&s->vars_txn, SCOPE_TXN);
5181 vars_init(&s->vars_reqres, SCOPE_REQ);
Christopher Faulet75f619a2021-03-08 19:12:58 +01005182
5183 return txn;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005184}
5185
5186/* to be used at the end of a transaction */
Christopher Faulet75f619a2021-03-08 19:12:58 +01005187void http_destroy_txn(struct stream *s)
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005188{
5189 struct http_txn *txn = s->txn;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005190
5191 /* these ones will have been dynamically allocated */
5192 pool_free(pool_head_requri, txn->uri);
5193 pool_free(pool_head_capture, txn->cli_cookie);
5194 pool_free(pool_head_capture, txn->srv_cookie);
Tim Duesterhusa17e6622020-03-05 20:19:02 +01005195 pool_free(pool_head_uniqueid, s->unique_id.ptr);
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005196
Tim Duesterhusa17e6622020-03-05 20:19:02 +01005197 s->unique_id = IST_NULL;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005198 txn->uri = NULL;
5199 txn->srv_cookie = NULL;
5200 txn->cli_cookie = NULL;
5201
Christopher Faulet59399252019-11-07 14:27:52 +01005202 if (!LIST_ISEMPTY(&s->vars_txn.head))
5203 vars_prune(&s->vars_txn, s->sess, s);
5204 if (!LIST_ISEMPTY(&s->vars_reqres.head))
5205 vars_prune(&s->vars_reqres, s->sess, s);
Christopher Faulet75f619a2021-03-08 19:12:58 +01005206
5207 pool_free(pool_head_http_txn, txn);
5208 s->txn = NULL;
Christopher Faulet59399252019-11-07 14:27:52 +01005209}
5210
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005211
5212DECLARE_POOL(pool_head_http_txn, "http_txn", sizeof(struct http_txn));
Christopher Faulet0f226952018-10-22 09:29:56 +02005213
Christopher Fauletf4eb75d2018-10-11 15:55:07 +02005214__attribute__((constructor))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005215static void __http_protocol_init(void)
Christopher Fauletf4eb75d2018-10-11 15:55:07 +02005216{
5217}
5218
5219
5220/*
5221 * Local variables:
5222 * c-indent-level: 8
5223 * c-basic-offset: 8
5224 * End:
5225 */