blob: a06a20a261876ae4c55f108cf053849bacaab999 [file] [log] [blame]
Christopher Fauletf4eb75d2018-10-11 15:55:07 +02001/*
2 * HTTP protocol analyzer
3 *
4 * Copyright (C) 2018 HAProxy Technologies, Christopher Faulet <cfaulet@haproxy.com>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
Willy Tarreaudcc048a2020-06-04 19:11:43 +020013#include <haproxy/acl.h>
Willy Tarreau122eba92020-06-04 10:15:32 +020014#include <haproxy/action-t.h>
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020015#include <haproxy/api.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020016#include <haproxy/backend.h>
Willy Tarreau8d366972020-05-27 16:10:29 +020017#include <haproxy/base64.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020018#include <haproxy/capture-t.h>
Amaury Denoyelle03517732021-05-07 14:25:01 +020019#include <haproxy/cfgparse.h>
Willy Tarreauf1d32c42020-06-04 21:07:02 +020020#include <haproxy/channel.h>
Willy Tarreau4aa573d2020-06-04 18:21:56 +020021#include <haproxy/check.h>
Willy Tarreau7ea393d2020-06-04 18:02:10 +020022#include <haproxy/connection.h>
Willy Tarreau36979d92020-06-05 17:27:29 +020023#include <haproxy/errors.h>
Willy Tarreauc7babd82020-06-04 21:29:29 +020024#include <haproxy/filters.h>
Willy Tarreaucd72d8c2020-06-02 19:11:26 +020025#include <haproxy/http.h>
Willy Tarreauc2b1ff02020-06-04 21:21:03 +020026#include <haproxy/http_ana.h>
Willy Tarreau87735332020-06-04 09:08:41 +020027#include <haproxy/http_htx.h>
Willy Tarreau16f958c2020-06-03 08:44:35 +020028#include <haproxy/htx.h>
Willy Tarreauaeed4a82020-06-04 22:01:04 +020029#include <haproxy/log.h>
Willy Tarreau6131d6a2020-06-02 16:48:09 +020030#include <haproxy/net_helper.h>
Willy Tarreaua264d962020-06-04 22:29:18 +020031#include <haproxy/proxy.h>
Willy Tarreau7cd8b6e2020-06-02 17:32:26 +020032#include <haproxy/regex.h>
Willy Tarreau1e56f922020-06-04 23:20:13 +020033#include <haproxy/server-t.h>
Willy Tarreau2eec9b52020-06-04 19:58:55 +020034#include <haproxy/stats.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020035#include <haproxy/stream.h>
Willy Tarreau5e539c92020-06-04 20:45:39 +020036#include <haproxy/stream_interface.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020037#include <haproxy/trace.h>
Willy Tarreau8c42b8a2020-06-04 19:27:34 +020038#include <haproxy/uri_auth-t.h>
Willy Tarreaua1718922020-06-04 16:25:31 +020039#include <haproxy/vars.h>
Christopher Faulete0768eb2018-10-03 16:38:02 +020040
Christopher Faulete0768eb2018-10-03 16:38:02 +020041
Christopher Fauleteea8fc72019-11-05 16:18:10 +010042#define TRACE_SOURCE &trace_strm
43
Christopher Faulet377c5a52018-10-24 21:21:30 +020044extern const char *stat_status_codes[];
Christopher Fauletf2824e62018-10-01 12:12:37 +020045
Willy Tarreauff882702021-04-10 17:23:00 +020046struct pool_head *pool_head_requri __read_mostly = NULL;
47struct pool_head *pool_head_capture __read_mostly = NULL;
Christopher Fauleta8a46e22019-07-16 14:53:09 +020048
49
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020050static void http_end_request(struct stream *s);
51static void http_end_response(struct stream *s);
Christopher Fauletf2824e62018-10-01 12:12:37 +020052
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020053static void http_capture_headers(struct htx *htx, char **cap, struct cap_hdr *cap_hdr);
54static int http_del_hdr_value(char *start, char *end, char **from, char *next);
55static size_t http_fmt_req_line(const struct htx_sl *sl, char *str, size_t len);
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020056static void http_debug_stline(const char *dir, struct stream *s, const struct htx_sl *sl);
57static void http_debug_hdr(const char *dir, struct stream *s, const struct ist n, const struct ist v);
Christopher Faulet0f226952018-10-22 09:29:56 +020058
Christopher Fauletd4150ad2021-10-13 15:35:55 +020059static enum rule_result http_req_get_intercept_rule(struct proxy *px, struct list *def_rules, struct list *rules, struct stream *s);
60static enum rule_result http_res_get_intercept_rule(struct proxy *px, struct list *def_rules, struct list *rules, struct stream *s);
Christopher Faulet3e964192018-10-24 11:39:23 +020061
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020062static void http_manage_client_side_cookies(struct stream *s, struct channel *req);
63static void http_manage_server_side_cookies(struct stream *s, struct channel *res);
Christopher Fauletfcda7c62018-10-24 11:56:22 +020064
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020065static int http_stats_check_uri(struct stream *s, struct http_txn *txn, struct proxy *backend);
66static int http_handle_stats(struct stream *s, struct channel *req);
Christopher Faulet377c5a52018-10-24 21:21:30 +020067
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020068static int http_handle_expect_hdr(struct stream *s, struct htx *htx, struct http_msg *msg);
69static int http_reply_100_continue(struct stream *s);
Christopher Faulet23a3c792018-11-28 10:01:23 +010070
Christopher Faulete0768eb2018-10-03 16:38:02 +020071/* This stream analyser waits for a complete HTTP request. It returns 1 if the
72 * processing can continue on next analysers, or zero if it either needs more
73 * data or wants to immediately abort the request (eg: timeout, error, ...). It
74 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req.analysers
75 * when it has nothing left to do, and may remove any analyser when it wants to
76 * abort.
77 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020078int http_wait_for_request(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +020079{
Christopher Faulet9768c262018-10-22 09:34:31 +020080
Christopher Faulete0768eb2018-10-03 16:38:02 +020081 /*
Christopher Faulet9768c262018-10-22 09:34:31 +020082 * We will analyze a complete HTTP request to check the its syntax.
Christopher Faulete0768eb2018-10-03 16:38:02 +020083 *
Christopher Faulet9768c262018-10-22 09:34:31 +020084 * Once the start line and all headers are received, we may perform a
85 * capture of the error (if any), and we will set a few fields. We also
86 * check for monitor-uri, logging and finally headers capture.
Christopher Faulete0768eb2018-10-03 16:38:02 +020087 */
Christopher Faulete0768eb2018-10-03 16:38:02 +020088 struct session *sess = s->sess;
89 struct http_txn *txn = s->txn;
90 struct http_msg *msg = &txn->req;
Christopher Faulet9768c262018-10-22 09:34:31 +020091 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +010092 struct htx_sl *sl;
Christopher Faulete0768eb2018-10-03 16:38:02 +020093
Christopher Fauleteea8fc72019-11-05 16:18:10 +010094 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +020095
Christopher Fauletda46a0d2021-01-21 17:32:58 +010096 if (unlikely(!IS_HTX_STRM(s))) {
97 /* It is only possible when a TCP stream is upgrade to HTTP.
98 * There is a transition period during which there is no
99 * data. The stream is still in raw mode and SF_IGNORE flag is
100 * still set. When this happens, the new mux is responsible to
Ilya Shipitsinacf84592021-02-06 22:29:08 +0500101 * handle all errors. Thus we may leave immediately.
Christopher Fauletda46a0d2021-01-21 17:32:58 +0100102 */
103 BUG_ON(!(s->flags & SF_IGNORE) || !c_empty(&s->req));
Christopher Faulet9768c262018-10-22 09:34:31 +0200104
Christopher Faulet97b3a612021-03-15 17:10:12 +0100105 /* Don't connect for now */
106 channel_dont_connect(req);
107
108 /* A SHUTR at this stage means we are performing a "destructive"
109 * HTTP upgrade (TCP>H2). In this case, we can leave.
110 */
111 if (req->flags & CF_SHUTR) {
112 s->logs.logwait = 0;
113 s->logs.level = 0;
114 channel_abort(&s->req);
115 channel_abort(&s->res);
116 req->analysers &= AN_REQ_FLT_END;
117 req->analyse_exp = TICK_ETERNITY;
118 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA, s);
119 return 1;
120 }
Christopher Fauletda46a0d2021-01-21 17:32:58 +0100121 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA, s);
122 return 0;
123 }
124
125 htx = htxbuf(&req->buf);
Christopher Faulet8bebd2f2020-10-06 17:54:56 +0200126
Willy Tarreau4236f032019-03-05 10:43:32 +0100127 /* Parsing errors are caught here */
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200128 if (htx->flags & (HTX_FL_PARSING_ERROR|HTX_FL_PROCESSING_ERROR)) {
Willy Tarreau4236f032019-03-05 10:43:32 +0100129 stream_inc_http_req_ctr(s);
Emeric Brun28976442020-10-07 08:50:09 +0200130 proxy_inc_fe_req_ctr(sess->listener, sess->fe);
Christopher Fauletbf7175f2021-02-10 14:58:01 +0100131 if (htx->flags & HTX_FL_PARSING_ERROR) {
132 stream_inc_http_err_ctr(s);
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200133 goto return_bad_req;
Christopher Fauletbf7175f2021-02-10 14:58:01 +0100134 }
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200135 else
136 goto return_int_err;
Willy Tarreau4236f032019-03-05 10:43:32 +0100137 }
138
Christopher Faulete0768eb2018-10-03 16:38:02 +0200139 /* we're speaking HTTP here, so let's speak HTTP to the client */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200140 s->srv_error = http_return_srv_error;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200141
Christopher Faulet9768c262018-10-22 09:34:31 +0200142 msg->msg_state = HTTP_MSG_BODY;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200143 stream_inc_http_req_ctr(s);
Emeric Brun28976442020-10-07 08:50:09 +0200144 proxy_inc_fe_req_ctr(sess->listener, sess->fe); /* one more valid request for this FE */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200145
Christopher Faulet9768c262018-10-22 09:34:31 +0200146 /* kill the pending keep-alive timeout */
Christopher Faulet9768c262018-10-22 09:34:31 +0200147 req->analyse_exp = TICK_ETERNITY;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200148
Christopher Faulet29f17582019-05-23 11:03:26 +0200149 BUG_ON(htx_get_first_type(htx) != HTX_BLK_REQ_SL);
Christopher Faulet297fbb42019-05-13 14:41:27 +0200150 sl = http_get_stline(htx);
Christopher Faulet03599112018-11-27 11:21:21 +0100151
Christopher Faulet9768c262018-10-22 09:34:31 +0200152 /* 0: we might have to print this header in debug mode */
153 if (unlikely((global.mode & MODE_DEBUG) &&
154 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
155 int32_t pos;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200156
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200157 http_debug_stline("clireq", s, sl);
Christopher Faulet9768c262018-10-22 09:34:31 +0200158
Christopher Fauleta3f15502019-05-13 15:27:23 +0200159 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet9768c262018-10-22 09:34:31 +0200160 struct htx_blk *blk = htx_get_blk(htx, pos);
161 enum htx_blk_type type = htx_get_blk_type(blk);
162
163 if (type == HTX_BLK_EOH)
164 break;
165 if (type != HTX_BLK_HDR)
166 continue;
167
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200168 http_debug_hdr("clihdr", s,
169 htx_get_blk_name(htx, blk),
170 htx_get_blk_value(htx, blk));
Christopher Faulet9768c262018-10-22 09:34:31 +0200171 }
172 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200173
174 /*
Christopher Faulet03599112018-11-27 11:21:21 +0100175 * 1: identify the method and the version. Also set HTTP flags
Christopher Faulete0768eb2018-10-03 16:38:02 +0200176 */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100177 txn->meth = sl->info.req.meth;
Christopher Faulet03599112018-11-27 11:21:21 +0100178 if (sl->flags & HTX_SL_F_VER_11)
Christopher Faulet9768c262018-10-22 09:34:31 +0200179 msg->flags |= HTTP_MSGF_VER_11;
Christopher Faulet03599112018-11-27 11:21:21 +0100180 msg->flags |= HTTP_MSGF_XFER_LEN;
Christopher Faulet2a408542020-11-20 14:22:37 +0100181 if (sl->flags & HTX_SL_F_CLEN)
182 msg->flags |= HTTP_MSGF_CNT_LEN;
183 else if (sl->flags & HTX_SL_F_CHNK)
184 msg->flags |= HTTP_MSGF_TE_CHNK;
Christopher Fauletb2db4fa2018-11-27 16:51:09 +0100185 if (sl->flags & HTX_SL_F_BODYLESS)
186 msg->flags |= HTTP_MSGF_BODYLESS;
Christopher Faulet576c3582021-01-08 15:53:01 +0100187 if (sl->flags & HTX_SL_F_CONN_UPG)
188 msg->flags |= HTTP_MSGF_CONN_UPG;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200189
190 /* we can make use of server redirect on GET and HEAD */
191 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
192 s->flags |= SF_REDIRECTABLE;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100193 else if (txn->meth == HTTP_METH_OTHER && isteqi(htx_sl_req_meth(sl), ist("PRI"))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200194 /* PRI is reserved for the HTTP/2 preface */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200195 goto return_bad_req;
196 }
197
198 /*
Christopher Faulet6072beb2020-02-18 15:34:58 +0100199 * 2: check if the URI matches the monitor_uri. We have to do this for
200 * every request which gets in, because the monitor-uri is defined by
201 * the frontend. If the monitor-uri starts with a '/', the matching is
202 * done against the request's path. Otherwise, the request's uri is
203 * used. It is a workaround to let HTTP/2 health-checks work as
204 * expected.
Christopher Faulete0768eb2018-10-03 16:38:02 +0200205 */
Amaury Denoyelle5a9bd372021-07-06 11:23:10 +0200206 if (unlikely(sess->fe->monitor_uri_len != 0)) {
207 const struct ist monitor_uri = ist2(sess->fe->monitor_uri,
208 sess->fe->monitor_uri_len);
Amaury Denoyellec453f952021-07-06 11:40:12 +0200209 struct http_uri_parser parser = http_uri_parser_init(htx_sl_req_uri(sl));
Christopher Faulete0768eb2018-10-03 16:38:02 +0200210
Amaury Denoyelle5a9bd372021-07-06 11:23:10 +0200211 if ((istptr(monitor_uri)[0] == '/' &&
Amaury Denoyellec453f952021-07-06 11:40:12 +0200212 isteq(http_parse_path(&parser), monitor_uri)) ||
Amaury Denoyelle5a9bd372021-07-06 11:23:10 +0200213 isteq(htx_sl_req_uri(sl), monitor_uri)) {
214 /*
215 * We have found the monitor URI
216 */
217 struct acl_cond *cond;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200218
Amaury Denoyelle5a9bd372021-07-06 11:23:10 +0200219 s->flags |= SF_MONITOR;
220 _HA_ATOMIC_INC(&sess->fe->fe_counters.intercepted_req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200221
Amaury Denoyelle5a9bd372021-07-06 11:23:10 +0200222 /* Check if we want to fail this monitor request or not */
223 list_for_each_entry(cond, &sess->fe->mon_fail_cond, list) {
224 int ret = acl_exec_cond(cond, sess->fe, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200225
Amaury Denoyelle5a9bd372021-07-06 11:23:10 +0200226 ret = acl_pass(ret);
227 if (cond->pol == ACL_COND_UNLESS)
228 ret = !ret;
229
230 if (ret) {
231 /* we fail this request, let's return 503 service unavail */
232 txn->status = 503;
233 if (!(s->flags & SF_ERR_MASK))
234 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
235 goto return_prx_cond;
236 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200237 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200238
Amaury Denoyelle5a9bd372021-07-06 11:23:10 +0200239 /* nothing to fail, let's reply normally */
240 txn->status = 200;
241 if (!(s->flags & SF_ERR_MASK))
242 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
243 goto return_prx_cond;
244 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200245 }
246
247 /*
248 * 3: Maybe we have to copy the original REQURI for the logs ?
249 * Note: we cannot log anymore if the request has been
250 * classified as invalid.
251 */
252 if (unlikely(s->logs.logwait & LW_REQ)) {
253 /* we have a complete HTTP request that we must log */
254 if ((txn->uri = pool_alloc(pool_head_requri)) != NULL) {
Christopher Faulet9768c262018-10-22 09:34:31 +0200255 size_t len;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200256
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200257 len = http_fmt_req_line(sl, txn->uri, global.tune.requri_len - 1);
Christopher Faulet9768c262018-10-22 09:34:31 +0200258 txn->uri[len] = 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200259
260 if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT)))
261 s->do_log(s);
262 } else {
263 ha_alert("HTTP logging : out of memory.\n");
264 }
265 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200266
Christopher Faulete0768eb2018-10-03 16:38:02 +0200267 /* if the frontend has "option http-use-proxy-header", we'll check if
268 * we have what looks like a proxied connection instead of a connection,
269 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
270 * Note that this is *not* RFC-compliant, however browsers and proxies
271 * happen to do that despite being non-standard :-(
272 * We consider that a request not beginning with either '/' or '*' is
273 * a proxied connection, which covers both "scheme://location" and
274 * CONNECT ip:port.
275 */
276 if ((sess->fe->options2 & PR_O2_USE_PXHDR) &&
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100277 *HTX_SL_REQ_UPTR(sl) != '/' && *HTX_SL_REQ_UPTR(sl) != '*')
Christopher Faulete0768eb2018-10-03 16:38:02 +0200278 txn->flags |= TX_USE_PX_CONN;
279
Christopher Faulete0768eb2018-10-03 16:38:02 +0200280 /* 5: we may need to capture headers */
281 if (unlikely((s->logs.logwait & LW_REQHDR) && s->req_cap))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200282 http_capture_headers(htx, s->req_cap, sess->fe->req_cap);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200283
Christopher Faulete0768eb2018-10-03 16:38:02 +0200284 /* we may have to wait for the request's body */
Christopher Faulet9768c262018-10-22 09:34:31 +0200285 if (s->be->options & PR_O_WREQ_BODY)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200286 req->analysers |= AN_REQ_HTTP_BODY;
287
288 /*
289 * RFC7234#4:
290 * A cache MUST write through requests with methods
291 * that are unsafe (Section 4.2.1 of [RFC7231]) to
292 * the origin server; i.e., a cache is not allowed
293 * to generate a reply to such a request before
294 * having forwarded the request and having received
295 * a corresponding response.
296 *
297 * RFC7231#4.2.1:
298 * Of the request methods defined by this
299 * specification, the GET, HEAD, OPTIONS, and TRACE
300 * methods are defined to be safe.
301 */
302 if (likely(txn->meth == HTTP_METH_GET ||
303 txn->meth == HTTP_METH_HEAD ||
304 txn->meth == HTTP_METH_OPTIONS ||
305 txn->meth == HTTP_METH_TRACE))
306 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
307
308 /* end of job, return OK */
309 req->analysers &= ~an_bit;
310 req->analyse_exp = TICK_ETERNITY;
Christopher Faulet9768c262018-10-22 09:34:31 +0200311
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100312 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200313 return 1;
314
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200315 return_int_err:
316 txn->status = 500;
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200317 if (!(s->flags & SF_ERR_MASK))
318 s->flags |= SF_ERR_INTERNAL;
Willy Tarreau4781b152021-04-06 13:53:36 +0200319 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
William Lallemand36119de2021-03-08 15:26:48 +0100320 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200321 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200322 goto return_prx_cond;
323
Christopher Faulete0768eb2018-10-03 16:38:02 +0200324 return_bad_req:
Christopher Faulet9768c262018-10-22 09:34:31 +0200325 txn->status = 400;
Willy Tarreau4781b152021-04-06 13:53:36 +0200326 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req);
William Lallemand36119de2021-03-08 15:26:48 +0100327 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200328 _HA_ATOMIC_INC(&sess->listener->counters->failed_req);
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200329 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200330
331 return_prx_cond:
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200332 http_reply_and_close(s, txn->status, http_error_message(s));
333
Christopher Faulete0768eb2018-10-03 16:38:02 +0200334 if (!(s->flags & SF_ERR_MASK))
335 s->flags |= SF_ERR_PRXCOND;
336 if (!(s->flags & SF_FINST_MASK))
337 s->flags |= SF_FINST_R;
338
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100339 DBG_TRACE_DEVEL("leaving on error",
340 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200341 return 0;
342}
343
344
345/* This stream analyser runs all HTTP request processing which is common to
346 * frontends and backends, which means blocking ACLs, filters, connection-close,
347 * reqadd, stats and redirects. This is performed for the designated proxy.
348 * It returns 1 if the processing can continue on next analysers, or zero if it
349 * either needs more data or wants to immediately abort the request (eg: deny,
350 * error, ...).
351 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200352int http_process_req_common(struct stream *s, struct channel *req, int an_bit, struct proxy *px)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200353{
Christopher Fauletd4150ad2021-10-13 15:35:55 +0200354 struct list *def_rules, *rules;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200355 struct session *sess = s->sess;
356 struct http_txn *txn = s->txn;
357 struct http_msg *msg = &txn->req;
Christopher Fauletff2759f2018-10-24 11:13:16 +0200358 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200359 struct redirect_rule *rule;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200360 enum rule_result verdict;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200361 struct connection *conn = objt_conn(sess->origin);
362
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100363 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200364
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100365 htx = htxbuf(&req->buf);
Christopher Fauletff2759f2018-10-24 11:13:16 +0200366
Christopher Faulet1907ccc2019-04-29 13:12:02 +0200367 /* just in case we have some per-backend tracking. Only called the first
368 * execution of the analyser. */
Christopher Fauletd4150ad2021-10-13 15:35:55 +0200369 if (!s->current_rule && !s->current_rule_list)
Christopher Faulet1907ccc2019-04-29 13:12:02 +0200370 stream_inc_be_http_req_ctr(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200371
Christopher Fauletd4150ad2021-10-13 15:35:55 +0200372 def_rules = ((px->defpx && (an_bit == AN_REQ_HTTP_PROCESS_FE || px != sess->fe)) ? &px->defpx->http_req_rules : NULL);
373 rules = &px->http_req_rules;
374
Christopher Faulete0768eb2018-10-03 16:38:02 +0200375 /* evaluate http-request rules */
Christopher Fauletd4150ad2021-10-13 15:35:55 +0200376 if ((def_rules && !LIST_ISEMPTY(def_rules)) || !LIST_ISEMPTY(rules)) {
377 verdict = http_req_get_intercept_rule(px, def_rules, rules, s);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200378
379 switch (verdict) {
380 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
381 goto return_prx_yield;
382
383 case HTTP_RULE_RES_CONT:
384 case HTTP_RULE_RES_STOP: /* nothing to do */
385 break;
386
387 case HTTP_RULE_RES_DENY: /* deny or tarpit */
388 if (txn->flags & TX_CLTARPIT)
389 goto tarpit;
390 goto deny;
391
392 case HTTP_RULE_RES_ABRT: /* abort request, response already sent. Eg: auth */
393 goto return_prx_cond;
394
395 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
396 goto done;
397
398 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
399 goto return_bad_req;
Christopher Faulet3a26bee2019-12-16 12:47:40 +0100400
401 case HTTP_RULE_RES_ERROR: /* failed with a bad request */
402 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200403 }
404 }
405
406 if (conn && (conn->flags & CO_FL_EARLY_DATA) &&
Olivier Houchard220a26c2020-01-23 14:57:36 +0100407 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_SSL_WAIT_HS))) {
Christopher Fauletff2759f2018-10-24 11:13:16 +0200408 struct http_hdr_ctx ctx;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200409
Christopher Fauletff2759f2018-10-24 11:13:16 +0200410 ctx.blk = NULL;
411 if (!http_find_header(htx, ist("Early-Data"), &ctx, 0)) {
412 if (unlikely(!http_add_header(htx, ist("Early-Data"), ist("1"))))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100413 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200414 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200415 }
416
417 /* OK at this stage, we know that the request was accepted according to
418 * the http-request rules, we can check for the stats. Note that the
419 * URI is detected *before* the req* rules in order not to be affected
420 * by a possible reqrep, while they are processed *after* so that a
421 * reqdeny can still block them. This clearly needs to change in 1.6!
422 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200423 if (!s->target && http_stats_check_uri(s, txn, px)) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200424 s->target = &http_stats_applet.obj_type;
Willy Tarreau14bfe9a2018-12-19 15:19:27 +0100425 if (unlikely(!si_register_handler(&s->si[1], objt_applet(s->target)))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200426 s->logs.tv_request = now;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200427 if (!(s->flags & SF_ERR_MASK))
428 s->flags |= SF_ERR_RESOURCE;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100429 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200430 }
431
432 /* parse the whole stats request and extract the relevant information */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200433 http_handle_stats(s, req);
Christopher Fauletd4150ad2021-10-13 15:35:55 +0200434 verdict = http_req_get_intercept_rule(px, NULL, &px->uri_auth->http_req_rules, s);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200435 /* not all actions implemented: deny, allow, auth */
436
437 if (verdict == HTTP_RULE_RES_DENY) /* stats http-request deny */
438 goto deny;
439
440 if (verdict == HTTP_RULE_RES_ABRT) /* stats auth / stats http-request auth */
441 goto return_prx_cond;
Christopher Faulet3a26bee2019-12-16 12:47:40 +0100442
443 if (verdict == HTTP_RULE_RES_BADREQ) /* failed with a bad request */
444 goto return_bad_req;
445
446 if (verdict == HTTP_RULE_RES_ERROR) /* failed with a bad request */
447 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200448 }
449
Christopher Faulet2571bc62019-03-01 11:44:26 +0100450 /* Proceed with the applets now. */
451 if (unlikely(objt_applet(s->target))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200452 if (sess->fe == s->be) /* report it if the request was intercepted by the frontend */
Willy Tarreau4781b152021-04-06 13:53:36 +0200453 _HA_ATOMIC_INC(&sess->fe->fe_counters.intercepted_req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200454
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200455 if (http_handle_expect_hdr(s, htx, msg) == -1)
Christopher Fauletb8a53712019-12-16 11:29:38 +0100456 goto return_int_err;
Christopher Fauletbcf242a2019-03-01 11:36:26 +0100457
Christopher Faulete0768eb2018-10-03 16:38:02 +0200458 if (!(s->flags & SF_ERR_MASK)) // this is not really an error but it is
459 s->flags |= SF_ERR_LOCAL; // to mark that it comes from the proxy
460 if (!(s->flags & SF_FINST_MASK))
461 s->flags |= SF_FINST_R;
462
Christopher Fauletc2ac5e42021-03-08 18:20:09 +0100463 if (HAS_FILTERS(s))
464 req->analysers |= AN_REQ_FLT_HTTP_HDRS;
465
Christopher Faulete0768eb2018-10-03 16:38:02 +0200466 /* enable the minimally required analyzers to handle keep-alive and compression on the HTTP response */
467 req->analysers &= (AN_REQ_HTTP_BODY | AN_REQ_FLT_HTTP_HDRS | AN_REQ_FLT_END);
468 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
469 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Christopher Fauletbcf242a2019-03-01 11:36:26 +0100470
471 req->flags |= CF_SEND_DONTWAIT;
472 s->flags |= SF_ASSIGNED;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200473 goto done;
474 }
475
476 /* check whether we have some ACLs set to redirect this request */
477 list_for_each_entry(rule, &px->redirect_rules, list) {
478 if (rule->cond) {
479 int ret;
480
481 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
482 ret = acl_pass(ret);
483 if (rule->cond->pol == ACL_COND_UNLESS)
484 ret = !ret;
485 if (!ret)
486 continue;
487 }
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200488 if (!http_apply_redirect_rule(rule, s, txn))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100489 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200490 goto done;
491 }
492
493 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
494 * If this happens, then the data will not come immediately, so we must
495 * send all what we have without waiting. Note that due to the small gain
496 * in waiting for the body of the request, it's easier to simply put the
497 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
498 * itself once used.
499 */
500 req->flags |= CF_SEND_DONTWAIT;
501
502 done: /* done with this analyser, continue with next ones that the calling
503 * points will have set, if any.
504 */
505 req->analyse_exp = TICK_ETERNITY;
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +0500506 done_without_exp: /* done with this analyser, but don't reset the analyse_exp. */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200507 req->analysers &= ~an_bit;
Christopher Fauletd4150ad2021-10-13 15:35:55 +0200508 s->current_rule = s->current_rule_list = NULL;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100509 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200510 return 1;
511
512 tarpit:
513 /* Allow cookie logging
514 */
515 if (s->be->cookie_name || sess->fe->capture_name)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200516 http_manage_client_side_cookies(s, req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200517
518 /* When a connection is tarpitted, we use the tarpit timeout,
519 * which may be the same as the connect timeout if unspecified.
520 * If unset, then set it to zero because we really want it to
521 * eventually expire. We build the tarpit as an analyser.
522 */
Christopher Faulet202c6ce2019-01-07 14:57:35 +0100523 channel_htx_erase(&s->req, htx);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200524
525 /* wipe the request out so that we can drop the connection early
526 * if the client closes first.
527 */
528 channel_dont_connect(req);
529
Christopher Faulete0768eb2018-10-03 16:38:02 +0200530 req->analysers &= AN_REQ_FLT_END; /* remove switching rules etc... */
531 req->analysers |= AN_REQ_HTTP_TARPIT;
532 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
533 if (!req->analyse_exp)
534 req->analyse_exp = tick_add(now_ms, 0);
535 stream_inc_http_err_ctr(s);
Willy Tarreau4781b152021-04-06 13:53:36 +0200536 _HA_ATOMIC_INC(&sess->fe->fe_counters.denied_req);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100537 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau4781b152021-04-06 13:53:36 +0200538 _HA_ATOMIC_INC(&s->be->be_counters.denied_req);
William Lallemand36119de2021-03-08 15:26:48 +0100539 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200540 _HA_ATOMIC_INC(&sess->listener->counters->denied_req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200541 goto done_without_exp;
542
543 deny: /* this request was blocked (denied) */
544
545 /* Allow cookie logging
546 */
547 if (s->be->cookie_name || sess->fe->capture_name)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200548 http_manage_client_side_cookies(s, req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200549
Christopher Faulete0768eb2018-10-03 16:38:02 +0200550 s->logs.tv_request = now;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200551 stream_inc_http_err_ctr(s);
Willy Tarreau4781b152021-04-06 13:53:36 +0200552 _HA_ATOMIC_INC(&sess->fe->fe_counters.denied_req);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100553 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau4781b152021-04-06 13:53:36 +0200554 _HA_ATOMIC_INC(&s->be->be_counters.denied_req);
William Lallemand36119de2021-03-08 15:26:48 +0100555 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200556 _HA_ATOMIC_INC(&sess->listener->counters->denied_req);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100557 goto return_prx_err;
558
559 return_int_err:
560 txn->status = 500;
561 if (!(s->flags & SF_ERR_MASK))
562 s->flags |= SF_ERR_INTERNAL;
Willy Tarreau4781b152021-04-06 13:53:36 +0200563 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100564 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau4781b152021-04-06 13:53:36 +0200565 _HA_ATOMIC_INC(&s->be->be_counters.internal_errors);
William Lallemand36119de2021-03-08 15:26:48 +0100566 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200567 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100568 goto return_prx_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200569
570 return_bad_req:
Christopher Faulete0768eb2018-10-03 16:38:02 +0200571 txn->status = 400;
Willy Tarreau4781b152021-04-06 13:53:36 +0200572 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req);
William Lallemand36119de2021-03-08 15:26:48 +0100573 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200574 _HA_ATOMIC_INC(&sess->listener->counters->failed_req);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100575 /* fall through */
576
577 return_prx_err:
578 http_reply_and_close(s, txn->status, http_error_message(s));
579 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200580
581 return_prx_cond:
582 if (!(s->flags & SF_ERR_MASK))
583 s->flags |= SF_ERR_PRXCOND;
584 if (!(s->flags & SF_FINST_MASK))
585 s->flags |= SF_FINST_R;
586
587 req->analysers &= AN_REQ_FLT_END;
588 req->analyse_exp = TICK_ETERNITY;
Christopher Fauletd4150ad2021-10-13 15:35:55 +0200589 s->current_rule = s->current_rule_list = NULL;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100590 DBG_TRACE_DEVEL("leaving on error",
591 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200592 return 0;
593
594 return_prx_yield:
595 channel_dont_connect(req);
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100596 DBG_TRACE_DEVEL("waiting for more data",
597 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200598 return 0;
599}
600
601/* This function performs all the processing enabled for the current request.
602 * It returns 1 if the processing can continue on next analysers, or zero if it
603 * needs more data, encounters an error, or wants to immediately abort the
604 * request. It relies on buffers flags, and updates s->req.analysers.
605 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200606int http_process_request(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200607{
608 struct session *sess = s->sess;
609 struct http_txn *txn = s->txn;
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200610 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200611 struct connection *cli_conn = objt_conn(strm_sess(s)->origin);
612
Christopher Faulet8bebd2f2020-10-06 17:54:56 +0200613 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200614
615 /*
616 * Right now, we know that we have processed the entire headers
617 * and that unwanted requests have been filtered out. We can do
618 * whatever we want with the remaining request. Also, now we
619 * may have separate values for ->fe, ->be.
620 */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100621 htx = htxbuf(&req->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200622
623 /*
Christopher Faulete0768eb2018-10-03 16:38:02 +0200624 * 7: Now we can work with the cookies.
625 * Note that doing so might move headers in the request, but
626 * the fields will stay coherent and the URI will not move.
627 * This should only be performed in the backend.
628 */
629 if (s->be->cookie_name || sess->fe->capture_name)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200630 http_manage_client_side_cookies(s, req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200631
Tim Duesterhus2825b4b2020-02-28 15:13:34 +0100632 /* 8: Generate unique ID if a "unique-id-format" is defined.
633 *
634 * A unique ID is generated even when it is not sent to ensure that the ID can make use of
635 * fetches only available in the HTTP request processing stage.
636 */
637 if (!LIST_ISEMPTY(&sess->fe->format_unique_id)) {
Tim Duesterhusa17e6622020-03-05 20:19:02 +0100638 struct ist unique_id = stream_generate_unique_id(s, &sess->fe->format_unique_id);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200639
Tim Duesterhusa17e6622020-03-05 20:19:02 +0100640 if (!isttest(unique_id)) {
Christopher Fauletb8a53712019-12-16 11:29:38 +0100641 if (!(s->flags & SF_ERR_MASK))
642 s->flags |= SF_ERR_RESOURCE;
643 goto return_int_err;
644 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200645
Tim Duesterhus2825b4b2020-02-28 15:13:34 +0100646 /* send unique ID if a "unique-id-header" is defined */
Tim Duesterhus0643b0e2020-03-05 17:56:35 +0100647 if (isttest(sess->fe->header_unique_id) &&
Tim Duesterhusa17e6622020-03-05 20:19:02 +0100648 unlikely(!http_add_header(htx, sess->fe->header_unique_id, s->unique_id)))
Tim Duesterhus2825b4b2020-02-28 15:13:34 +0100649 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200650 }
651
652 /*
653 * 9: add X-Forwarded-For if either the frontend or the backend
654 * asks for it.
655 */
656 if ((sess->fe->options | s->be->options) & PR_O_FWDFOR) {
Christopher Faulet8a104ba2021-10-25 07:41:30 +0200657 const struct sockaddr_storage *src = si_src(&s->si[0]);
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200658 struct http_hdr_ctx ctx = { .blk = NULL };
659 struct ist hdr = ist2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : sess->fe->fwdfor_hdr_name,
660 s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : sess->fe->fwdfor_hdr_len);
661
Christopher Faulete0768eb2018-10-03 16:38:02 +0200662 if (!((sess->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200663 http_find_header(htx, hdr, &ctx, 0)) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200664 /* The header is set to be added only if none is present
665 * and we found it, so don't do anything.
666 */
667 }
Christopher Faulet8a104ba2021-10-25 07:41:30 +0200668 else if (src && src->ss_family == AF_INET) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200669 /* Add an X-Forwarded-For header unless the source IP is
670 * in the 'except' network range.
671 */
Christopher Faulet8a104ba2021-10-25 07:41:30 +0200672 if (ipcmp2net(src, &sess->fe->except_xff_net) &&
673 ipcmp2net(src, &s->be->except_xff_net)) {
674 unsigned char *pn = (unsigned char *)&((struct sockaddr_in *)src)->sin_addr;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200675
676 /* Note: we rely on the backend to get the header name to be used for
677 * x-forwarded-for, because the header is really meant for the backends.
678 * However, if the backend did not specify any option, we have to rely
679 * on the frontend's header name.
680 */
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200681 chunk_printf(&trash, "%d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
682 if (unlikely(!http_add_header(htx, hdr, ist2(trash.area, trash.data))))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100683 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200684 }
685 }
Christopher Faulet8a104ba2021-10-25 07:41:30 +0200686 else if (src && src->ss_family == AF_INET6) {
Christopher Faulet5d1def62021-02-26 09:19:15 +0100687 /* Add an X-Forwarded-For header unless the source IP is
688 * in the 'except' network range.
Christopher Faulete0768eb2018-10-03 16:38:02 +0200689 */
Christopher Faulet8a104ba2021-10-25 07:41:30 +0200690 if (ipcmp2net(src, &sess->fe->except_xff_net) &&
691 ipcmp2net(src, &s->be->except_xff_net)) {
Christopher Faulet5d1def62021-02-26 09:19:15 +0100692 char pn[INET6_ADDRSTRLEN];
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200693
Christopher Faulet5d1def62021-02-26 09:19:15 +0100694 inet_ntop(AF_INET6,
Christopher Faulet8a104ba2021-10-25 07:41:30 +0200695 (const void *)&((struct sockaddr_in6 *)(src))->sin6_addr,
Christopher Faulet5d1def62021-02-26 09:19:15 +0100696 pn, sizeof(pn));
Christopher Faulete0768eb2018-10-03 16:38:02 +0200697
Christopher Faulet5d1def62021-02-26 09:19:15 +0100698 /* Note: we rely on the backend to get the header name to be used for
699 * x-forwarded-for, because the header is really meant for the backends.
700 * However, if the backend did not specify any option, we have to rely
701 * on the frontend's header name.
702 */
703 chunk_printf(&trash, "%s", pn);
704 if (unlikely(!http_add_header(htx, hdr, ist2(trash.area, trash.data))))
705 goto return_int_err;
706 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200707 }
708 }
709
710 /*
711 * 10: add X-Original-To if either the frontend or the backend
712 * asks for it.
713 */
714 if ((sess->fe->options | s->be->options) & PR_O_ORGTO) {
Christopher Faulet8a104ba2021-10-25 07:41:30 +0200715 const struct sockaddr_storage *dst = si_dst(&s->si[0]);
Christopher Faulet5d1def62021-02-26 09:19:15 +0100716 struct ist hdr = ist2(s->be->orgto_hdr_len ? s->be->orgto_hdr_name : sess->fe->orgto_hdr_name,
717 s->be->orgto_hdr_len ? s->be->orgto_hdr_len : sess->fe->orgto_hdr_len);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200718
Christopher Faulet8a104ba2021-10-25 07:41:30 +0200719 if (dst && dst->ss_family == AF_INET) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200720 /* Add an X-Original-To header unless the destination IP is
721 * in the 'except' network range.
722 */
Christopher Faulet8a104ba2021-10-25 07:41:30 +0200723 if (ipcmp2net(dst, &sess->fe->except_xot_net) &&
724 ipcmp2net(dst, &s->be->except_xot_net)) {
725 unsigned char *pn = (unsigned char *)&((struct sockaddr_in *)dst)->sin_addr;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200726
727 /* Note: we rely on the backend to get the header name to be used for
728 * x-original-to, 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 */
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200732 chunk_printf(&trash, "%d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
733 if (unlikely(!http_add_header(htx, hdr, ist2(trash.area, trash.data))))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100734 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200735 }
736 }
Christopher Faulet8a104ba2021-10-25 07:41:30 +0200737 else if (dst && dst->ss_family == AF_INET6) {
Christopher Faulet5d1def62021-02-26 09:19:15 +0100738 /* Add an X-Original-To header unless the source IP is
739 * in the 'except' network range.
740 */
Christopher Faulet8a104ba2021-10-25 07:41:30 +0200741 if (ipcmp2net(dst, &sess->fe->except_xot_net) &&
742 ipcmp2net(dst, &s->be->except_xot_net)) {
Christopher Faulet5d1def62021-02-26 09:19:15 +0100743 char pn[INET6_ADDRSTRLEN];
744
745 inet_ntop(AF_INET6,
Christopher Faulet8a104ba2021-10-25 07:41:30 +0200746 (const void *)&((struct sockaddr_in6 *)dst)->sin6_addr,
Christopher Faulet5d1def62021-02-26 09:19:15 +0100747 pn, sizeof(pn));
748
749 /* Note: we rely on the backend to get the header name to be used for
750 * x-forwarded-for, because the header is really meant for the backends.
751 * However, if the backend did not specify any option, we have to rely
752 * on the frontend's header name.
753 */
754 chunk_printf(&trash, "%s", pn);
755 if (unlikely(!http_add_header(htx, hdr, ist2(trash.area, trash.data))))
756 goto return_int_err;
757 }
758 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200759 }
760
Christopher Fauletc2ac5e42021-03-08 18:20:09 +0100761 /* Filter the request headers if there are filters attached to the
762 * stream.
763 */
764 if (HAS_FILTERS(s))
765 req->analysers |= AN_REQ_FLT_HTTP_HDRS;
766
Christopher Faulete0768eb2018-10-03 16:38:02 +0200767 /* If we have no server assigned yet and we're balancing on url_param
768 * with a POST request, we may be interested in checking the body for
769 * that parameter. This will be done in another analyser.
770 */
771 if (!(s->flags & (SF_ASSIGNED|SF_DIRECT)) &&
Willy Tarreau089eaa02019-01-14 15:17:46 +0100772 s->txn->meth == HTTP_METH_POST &&
773 (s->be->lbprm.algo & BE_LB_ALGO) == BE_LB_ALGO_PH) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200774 channel_dont_connect(req);
775 req->analysers |= AN_REQ_HTTP_BODY;
776 }
777
778 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
779 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau1a18b542018-12-11 16:37:42 +0100780
Christopher Faulete0768eb2018-10-03 16:38:02 +0200781 /* We expect some data from the client. Unless we know for sure
782 * we already have a full request, we have to re-enable quick-ack
783 * in case we previously disabled it, otherwise we might cause
784 * the client to delay further data.
785 */
William Lallemand36119de2021-03-08 15:26:48 +0100786 if ((sess->listener && (sess->listener->options & LI_O_NOQUICKACK)) && !(htx->flags & HTX_FL_EOM))
Willy Tarreau1a18b542018-12-11 16:37:42 +0100787 conn_set_quickack(cli_conn, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200788
789 /*************************************************************
790 * OK, that's finished for the headers. We have done what we *
791 * could. Let's switch to the DATA state. *
792 ************************************************************/
793 req->analyse_exp = TICK_ETERNITY;
794 req->analysers &= ~an_bit;
795
796 s->logs.tv_request = now;
797 /* OK let's go on with the BODY now */
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100798 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200799 return 1;
800
Christopher Fauletb8a53712019-12-16 11:29:38 +0100801 return_int_err:
802 txn->status = 500;
803 if (!(s->flags & SF_ERR_MASK))
804 s->flags |= SF_ERR_INTERNAL;
Willy Tarreau4781b152021-04-06 13:53:36 +0200805 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100806 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau4781b152021-04-06 13:53:36 +0200807 _HA_ATOMIC_INC(&s->be->be_counters.internal_errors);
William Lallemand36119de2021-03-08 15:26:48 +0100808 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200809 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100810
Christopher Fauletb8a53712019-12-16 11:29:38 +0100811 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +0200812
813 if (!(s->flags & SF_ERR_MASK))
814 s->flags |= SF_ERR_PRXCOND;
815 if (!(s->flags & SF_FINST_MASK))
816 s->flags |= SF_FINST_R;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100817
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100818 DBG_TRACE_DEVEL("leaving on error",
819 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200820 return 0;
821}
822
823/* This function is an analyser which processes the HTTP tarpit. It always
824 * returns zero, at the beginning because it prevents any other processing
825 * from occurring, and at the end because it terminates the request.
826 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200827int http_process_tarpit(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200828{
829 struct http_txn *txn = s->txn;
830
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100831 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, &txn->req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200832 /* This connection is being tarpitted. The CLIENT side has
833 * already set the connect expiration date to the right
834 * timeout. We just have to check that the client is still
835 * there and that the timeout has not expired.
836 */
837 channel_dont_connect(req);
838 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100839 !tick_is_expired(req->analyse_exp, now_ms)) {
840 DBG_TRACE_DEVEL("waiting for tarpit timeout expiry",
841 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200842 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100843 }
844
Christopher Faulete0768eb2018-10-03 16:38:02 +0200845
846 /* We will set the queue timer to the time spent, just for
847 * logging purposes. We fake a 500 server error, so that the
848 * attacker will not suspect his connection has been tarpitted.
849 * It will not cause trouble to the logs because we can exclude
850 * the tarpitted connections by filtering on the 'PT' status flags.
851 */
852 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
853
Christopher Faulet8dfeccf2020-05-15 14:16:29 +0200854 http_reply_and_close(s, txn->status, (!(req->flags & CF_READ_ERROR) ? http_error_message(s) : NULL));
Christopher Faulet5cb513a2020-05-13 17:56:56 +0200855
Christopher Faulete0768eb2018-10-03 16:38:02 +0200856 if (!(s->flags & SF_ERR_MASK))
857 s->flags |= SF_ERR_PRXCOND;
858 if (!(s->flags & SF_FINST_MASK))
859 s->flags |= SF_FINST_T;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100860
861 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200862 return 0;
863}
864
865/* This function is an analyser which waits for the HTTP request body. It waits
866 * for either the buffer to be full, or the full advertised contents to have
867 * reached the buffer. It must only be called after the standard HTTP request
868 * processing has occurred, because it expects the request to be parsed and will
869 * look for the Expect header. It may send a 100-Continue interim response. It
870 * takes in input any state starting from HTTP_MSG_BODY and leaves with one of
871 * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it
872 * needs to read more data, or 1 once it has completed its analysis.
873 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200874int http_wait_for_request_body(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200875{
876 struct session *sess = s->sess;
877 struct http_txn *txn = s->txn;
878 struct http_msg *msg = &s->txn->req;
879
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100880 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Fauletf76ebe82018-10-24 11:16:22 +0200881
Christopher Fauletf76ebe82018-10-24 11:16:22 +0200882
Christopher Faulet021a8e42021-03-29 10:46:38 +0200883 switch (http_wait_for_msg_body(s, req, s->be->timeout.httpreq, 0)) {
884 case HTTP_RULE_RES_CONT:
885 goto http_end;
886 case HTTP_RULE_RES_YIELD:
887 goto missing_data_or_waiting;
888 case HTTP_RULE_RES_BADREQ:
Willy Tarreau4236f032019-03-05 10:43:32 +0100889 goto return_bad_req;
Christopher Faulet021a8e42021-03-29 10:46:38 +0200890 case HTTP_RULE_RES_ERROR:
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200891 goto return_int_err;
Christopher Faulet021a8e42021-03-29 10:46:38 +0200892 case HTTP_RULE_RES_ABRT:
Christopher Fauletb8a53712019-12-16 11:29:38 +0100893 goto return_prx_cond;
Christopher Faulet021a8e42021-03-29 10:46:38 +0200894 default:
895 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200896 }
897
898 http_end:
899 /* The situation will not evolve, so let's give up on the analysis. */
900 s->logs.tv_request = now; /* update the request timer to reflect full request */
901 req->analysers &= ~an_bit;
902 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100903 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200904 return 1;
905
Christopher Faulet021a8e42021-03-29 10:46:38 +0200906 missing_data_or_waiting:
907 channel_dont_connect(req);
908 DBG_TRACE_DEVEL("waiting for more data",
909 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
910 return 0;
911
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200912 return_int_err:
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200913 txn->status = 500;
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200914 if (!(s->flags & SF_ERR_MASK))
915 s->flags |= SF_ERR_INTERNAL;
Willy Tarreau4781b152021-04-06 13:53:36 +0200916 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100917 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau4781b152021-04-06 13:53:36 +0200918 _HA_ATOMIC_INC(&s->be->be_counters.internal_errors);
William Lallemand36119de2021-03-08 15:26:48 +0100919 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200920 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Faulet021a8e42021-03-29 10:46:38 +0200921 goto return_prx_err;
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200922
Christopher Faulete0768eb2018-10-03 16:38:02 +0200923 return_bad_req: /* let's centralize all bad requests */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200924 txn->status = 400;
Willy Tarreau4781b152021-04-06 13:53:36 +0200925 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req);
William Lallemand36119de2021-03-08 15:26:48 +0100926 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200927 _HA_ATOMIC_INC(&sess->listener->counters->failed_req);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100928 /* fall through */
929
Christopher Faulet021a8e42021-03-29 10:46:38 +0200930 return_prx_err:
Christopher Fauletb8a53712019-12-16 11:29:38 +0100931 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet021a8e42021-03-29 10:46:38 +0200932 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200933
Christopher Faulet021a8e42021-03-29 10:46:38 +0200934 return_prx_cond:
Christopher Faulete0768eb2018-10-03 16:38:02 +0200935 if (!(s->flags & SF_ERR_MASK))
936 s->flags |= SF_ERR_PRXCOND;
937 if (!(s->flags & SF_FINST_MASK))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100938 s->flags |= (msg->msg_state < HTTP_MSG_DATA ? SF_FINST_R : SF_FINST_D);
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200939
Christopher Faulete0768eb2018-10-03 16:38:02 +0200940 req->analysers &= AN_REQ_FLT_END;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100941 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100942 DBG_TRACE_DEVEL("leaving on error",
943 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200944 return 0;
945}
946
947/* This function is an analyser which forwards request body (including chunk
948 * sizes if any). It is called as soon as we must forward, even if we forward
949 * zero byte. The only situation where it must not be called is when we're in
950 * tunnel mode and we want to forward till the close. It's used both to forward
951 * remaining data and to resync after end of body. It expects the msg_state to
952 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
953 * read more data, or 1 once we can go on with next request or end the stream.
954 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
955 * bytes of pending data + the headers if not already done.
956 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200957int http_request_forward_body(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200958{
959 struct session *sess = s->sess;
960 struct http_txn *txn = s->txn;
Christopher Faulet9768c262018-10-22 09:34:31 +0200961 struct http_msg *msg = &txn->req;
962 struct htx *htx;
Christopher Faulet93e02d82019-03-08 14:18:50 +0100963 short status = 0;
Christopher Fauletaed82cf2018-11-30 22:22:32 +0100964 int ret;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200965
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100966 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200967
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100968 htx = htxbuf(&req->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200969
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200970 if (htx->flags & HTX_FL_PARSING_ERROR)
971 goto return_bad_req;
972 if (htx->flags & HTX_FL_PROCESSING_ERROR)
973 goto return_int_err;
974
Christopher Faulete0768eb2018-10-03 16:38:02 +0200975 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
976 ((req->flags & CF_SHUTW) && (req->to_forward || co_data(req)))) {
977 /* Output closed while we were sending data. We must abort and
978 * wake the other side up.
Christopher Fauletf506d962021-04-27 10:56:28 +0200979 *
980 * If we have finished to send the request and the response is
981 * still in progress, don't catch write error on the request
982 * side if it is in fact a read error on the server side.
Christopher Faulete0768eb2018-10-03 16:38:02 +0200983 */
Christopher Fauletf506d962021-04-27 10:56:28 +0200984 if (msg->msg_state == HTTP_MSG_DONE && (s->res.flags & CF_READ_ERROR) && s->res.analysers)
985 return 0;
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200986
Olivier Houchard29cac3c2019-07-12 15:48:58 +0200987 /* Don't abort yet if we had L7 retries activated and it
988 * was a write error, we may recover.
989 */
990 if (!(req->flags & (CF_READ_ERROR | CF_READ_TIMEOUT)) &&
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100991 (s->si[1].flags & SI_FL_L7_RETRY)) {
992 DBG_TRACE_DEVEL("leaving on L7 retry",
993 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Olivier Houchard29cac3c2019-07-12 15:48:58 +0200994 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100995 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200996 msg->msg_state = HTTP_MSG_ERROR;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200997 http_end_request(s);
998 http_end_response(s);
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100999 DBG_TRACE_DEVEL("leaving on error",
1000 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001001 return 1;
1002 }
1003
1004 /* Note that we don't have to send 100-continue back because we don't
1005 * need the data to complete our job, and it's up to the server to
1006 * decide whether to return 100, 417 or anything else in return of
1007 * an "Expect: 100-continue" header.
1008 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001009 if (msg->msg_state == HTTP_MSG_BODY)
1010 msg->msg_state = HTTP_MSG_DATA;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001011
Christopher Faulete0768eb2018-10-03 16:38:02 +02001012 /* in most states, we should abort in case of early close */
1013 channel_auto_close(req);
1014
1015 if (req->to_forward) {
Christopher Faulet66af0b22019-03-22 14:54:52 +01001016 if (req->to_forward == CHN_INFINITE_FORWARD) {
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001017 if (req->flags & CF_EOI)
1018 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet66af0b22019-03-22 14:54:52 +01001019 }
1020 else {
1021 /* We can't process the buffer's contents yet */
1022 req->flags |= CF_WAKE_WRITE;
1023 goto missing_data_or_waiting;
1024 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001025 }
1026
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001027 if (msg->msg_state >= HTTP_MSG_ENDING)
1028 goto ending;
1029
1030 if (txn->meth == HTTP_METH_CONNECT) {
1031 msg->msg_state = HTTP_MSG_ENDING;
1032 goto ending;
1033 }
1034
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001035 /* Forward input data. We get it by removing all outgoing data not
1036 * forwarded yet from HTX data size. If there are some data filters, we
1037 * let them decide the amount of data to forward.
Christopher Faulet9768c262018-10-22 09:34:31 +02001038 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001039 if (HAS_REQ_DATA_FILTERS(s)) {
1040 ret = flt_http_payload(s, msg, htx->data);
1041 if (ret < 0)
1042 goto return_bad_req;
Christopher Faulet421e7692019-06-13 11:16:45 +02001043 c_adv(req, ret);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001044 }
1045 else {
Christopher Faulet421e7692019-06-13 11:16:45 +02001046 c_adv(req, htx->data - co_data(req));
Christopher Faulet66af0b22019-03-22 14:54:52 +01001047 if (msg->flags & HTTP_MSGF_XFER_LEN)
1048 channel_htx_forward_forever(req, htx);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001049 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001050
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001051 if (htx->data != co_data(req))
1052 goto missing_data_or_waiting;
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001053
Christopher Faulet9768c262018-10-22 09:34:31 +02001054 /* Check if the end-of-message is reached and if so, switch the message
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001055 * in HTTP_MSG_ENDING state. Then if all data was marked to be
1056 * forwarded, set the state to HTTP_MSG_DONE.
Christopher Faulet9768c262018-10-22 09:34:31 +02001057 */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001058 if (!(htx->flags & HTX_FL_EOM))
Christopher Faulet9768c262018-10-22 09:34:31 +02001059 goto missing_data_or_waiting;
1060
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001061 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet9768c262018-10-22 09:34:31 +02001062
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001063 ending:
Christopher Faulet2151cdd2020-07-22 16:34:59 +02001064 req->flags &= ~CF_EXPECT_MORE; /* no more data are expected */
1065
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001066 /* other states, ENDING...TUNNEL */
1067 if (msg->msg_state >= HTTP_MSG_DONE)
1068 goto done;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001069
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001070 if (HAS_REQ_DATA_FILTERS(s)) {
1071 ret = flt_http_end(s, msg);
1072 if (ret <= 0) {
1073 if (!ret)
1074 goto missing_data_or_waiting;
1075 goto return_bad_req;
1076 }
1077 }
1078
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001079 if (txn->meth == HTTP_METH_CONNECT)
1080 msg->msg_state = HTTP_MSG_TUNNEL;
1081 else {
1082 msg->msg_state = HTTP_MSG_DONE;
1083 req->to_forward = 0;
1084 }
1085
1086 done:
1087 /* we don't want to forward closes on DONE except in tunnel mode. */
1088 if (!(txn->flags & TX_CON_WANT_TUN))
1089 channel_dont_close(req);
1090
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001091 http_end_request(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001092 if (!(req->analysers & an_bit)) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001093 http_end_response(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001094 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
1095 if (req->flags & CF_SHUTW) {
1096 /* request errors are most likely due to the
1097 * server aborting the transfer. */
Christopher Faulet93e02d82019-03-08 14:18:50 +01001098 goto return_srv_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001099 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001100 goto return_bad_req;
1101 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001102 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001103 return 1;
1104 }
1105
1106 /* If "option abortonclose" is set on the backend, we want to monitor
1107 * the client's connection and forward any shutdown notification to the
1108 * server, which will decide whether to close or to go on processing the
1109 * request. We only do that in tunnel mode, and not in other modes since
1110 * it can be abused to exhaust source ports. */
Christopher Faulet769d0e92019-03-22 14:23:18 +01001111 if (s->be->options & PR_O_ABRT_CLOSE) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001112 channel_auto_read(req);
Christopher Fauletc41547b2019-07-16 14:32:23 +02001113 if ((req->flags & (CF_SHUTR|CF_READ_NULL)) && !(txn->flags & TX_CON_WANT_TUN))
Christopher Faulete0768eb2018-10-03 16:38:02 +02001114 s->si[1].flags |= SI_FL_NOLINGER;
1115 channel_auto_close(req);
1116 }
1117 else if (s->txn->meth == HTTP_METH_POST) {
1118 /* POST requests may require to read extra CRLF sent by broken
1119 * browsers and which could cause an RST to be sent upon close
1120 * on some systems (eg: Linux). */
1121 channel_auto_read(req);
1122 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001123 DBG_TRACE_DEVEL("waiting for the end of the HTTP txn",
1124 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001125 return 0;
1126
1127 missing_data_or_waiting:
1128 /* stop waiting for data if the input is closed before the end */
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001129 if (msg->msg_state < HTTP_MSG_ENDING && req->flags & CF_SHUTR)
Christopher Faulet93e02d82019-03-08 14:18:50 +01001130 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001131
1132 waiting:
1133 /* waiting for the last bits to leave the buffer */
1134 if (req->flags & CF_SHUTW)
Christopher Faulet93e02d82019-03-08 14:18:50 +01001135 goto return_srv_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001136
1137 /* When TE: chunked is used, we need to get there again to parse remaining
1138 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
1139 * And when content-length is used, we never want to let the possible
1140 * shutdown be forwarded to the other side, as the state machine will
1141 * take care of it once the client responds. It's also important to
1142 * prevent TIME_WAITs from accumulating on the backend side, and for
1143 * HTTP/2 where the last frame comes with a shutdown.
1144 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001145 if (msg->flags & HTTP_MSGF_XFER_LEN)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001146 channel_dont_close(req);
1147
1148 /* We know that more data are expected, but we couldn't send more that
1149 * what we did. So we always set the CF_EXPECT_MORE flag so that the
1150 * system knows it must not set a PUSH on this first part. Interactive
1151 * modes are already handled by the stream sock layer. We must not do
1152 * this in content-length mode because it could present the MSG_MORE
1153 * flag with the last block of forwarded data, which would cause an
1154 * additional delay to be observed by the receiver.
1155 */
Christopher Faulet2151cdd2020-07-22 16:34:59 +02001156 if (HAS_REQ_DATA_FILTERS(s))
Christopher Faulete0768eb2018-10-03 16:38:02 +02001157 req->flags |= CF_EXPECT_MORE;
1158
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001159 DBG_TRACE_DEVEL("waiting for more data to forward",
1160 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001161 return 0;
1162
Christopher Faulet93e02d82019-03-08 14:18:50 +01001163 return_cli_abort:
Willy Tarreau4781b152021-04-06 13:53:36 +02001164 _HA_ATOMIC_INC(&sess->fe->fe_counters.cli_aborts);
1165 _HA_ATOMIC_INC(&s->be->be_counters.cli_aborts);
William Lallemand36119de2021-03-08 15:26:48 +01001166 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001167 _HA_ATOMIC_INC(&sess->listener->counters->cli_aborts);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001168 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001169 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.cli_aborts);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001170 if (!(s->flags & SF_ERR_MASK))
1171 s->flags |= SF_ERR_CLICL;
1172 status = 400;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001173 goto return_prx_cond;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001174
1175 return_srv_abort:
Willy Tarreau4781b152021-04-06 13:53:36 +02001176 _HA_ATOMIC_INC(&sess->fe->fe_counters.srv_aborts);
1177 _HA_ATOMIC_INC(&s->be->be_counters.srv_aborts);
William Lallemand36119de2021-03-08 15:26:48 +01001178 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001179 _HA_ATOMIC_INC(&sess->listener->counters->srv_aborts);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001180 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001181 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.srv_aborts);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001182 if (!(s->flags & SF_ERR_MASK))
1183 s->flags |= SF_ERR_SRVCL;
1184 status = 502;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001185 goto return_prx_cond;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001186
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001187 return_int_err:
1188 if (!(s->flags & SF_ERR_MASK))
1189 s->flags |= SF_ERR_INTERNAL;
Willy Tarreau4781b152021-04-06 13:53:36 +02001190 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
1191 _HA_ATOMIC_INC(&s->be->be_counters.internal_errors);
William Lallemand36119de2021-03-08 15:26:48 +01001192 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001193 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001194 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001195 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.internal_errors);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001196 status = 500;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001197 goto return_prx_cond;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001198
Christopher Faulet93e02d82019-03-08 14:18:50 +01001199 return_bad_req:
Willy Tarreau4781b152021-04-06 13:53:36 +02001200 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req);
William Lallemand36119de2021-03-08 15:26:48 +01001201 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001202 _HA_ATOMIC_INC(&sess->listener->counters->failed_req);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001203 status = 400;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001204 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001205
Christopher Fauletb8a53712019-12-16 11:29:38 +01001206 return_prx_cond:
Christopher Faulet9768c262018-10-22 09:34:31 +02001207 if (txn->status > 0) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001208 /* Note: we don't send any error if some data were already sent */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001209 http_reply_and_close(s, txn->status, NULL);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001210 } else {
Christopher Faulet93e02d82019-03-08 14:18:50 +01001211 txn->status = status;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001212 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001213 }
Christopher Fauletb8a53712019-12-16 11:29:38 +01001214 if (!(s->flags & SF_ERR_MASK))
1215 s->flags |= SF_ERR_PRXCOND;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001216 if (!(s->flags & SF_FINST_MASK))
1217 s->flags |= ((txn->rsp.msg_state < HTTP_MSG_ERROR) ? SF_FINST_H : SF_FINST_D);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001218 DBG_TRACE_DEVEL("leaving on error ",
1219 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001220 return 0;
1221}
1222
Olivier Houcharda254a372019-04-05 15:30:12 +02001223/* Reset the stream and the backend stream_interface to a situation suitable for attemption connection */
1224/* Returns 0 if we can attempt to retry, -1 otherwise */
1225static __inline int do_l7_retry(struct stream *s, struct stream_interface *si)
1226{
Christopher Faulet9f5382e2021-05-21 13:46:14 +02001227 struct channel *req, *res;
1228 int co_data;
Olivier Houcharda254a372019-04-05 15:30:12 +02001229
1230 si->conn_retries--;
1231 if (si->conn_retries < 0)
Christopher Faulet552601d2021-05-26 10:31:06 +02001232 return -1;
Christopher Faulet5b82cc52020-10-12 15:18:50 +02001233
Christopher Faulete763c8c2021-05-05 18:23:59 +02001234 if (objt_server(s->target)) {
1235 if (s->flags & SF_CURR_SESS) {
1236 s->flags &= ~SF_CURR_SESS;
1237 _HA_ATOMIC_DEC(&__objt_server(s->target)->cur_sess);
1238 }
Willy Tarreau4781b152021-04-06 13:53:36 +02001239 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.retries);
Christopher Faulete763c8c2021-05-05 18:23:59 +02001240 }
Willy Tarreau4781b152021-04-06 13:53:36 +02001241 _HA_ATOMIC_INC(&s->be->be_counters.retries);
Willy Tarreau223995e2019-05-04 10:38:31 +02001242
Christopher Faulet9f5382e2021-05-21 13:46:14 +02001243 req = &s->req;
1244 res = &s->res;
Olivier Houcharda254a372019-04-05 15:30:12 +02001245 /* Remove any write error from the request, and read error from the response */
1246 req->flags &= ~(CF_WRITE_ERROR | CF_WRITE_TIMEOUT | CF_SHUTW | CF_SHUTW_NOW);
1247 res->flags &= ~(CF_READ_ERROR | CF_READ_TIMEOUT | CF_SHUTR | CF_EOI | CF_READ_NULL | CF_SHUTR_NOW);
1248 res->analysers = 0;
1249 si->flags &= ~(SI_FL_ERR | SI_FL_EXP | SI_FL_RXBLK_SHUT);
Christopher Faulet30aa0da2021-05-05 21:05:09 +02001250 si->err_type = SI_ET_NONE;
1251 s->flags &= ~(SF_ERR_MASK | SF_FINST_MASK);
Olivier Houchard4bd58672019-07-12 16:16:59 +02001252 stream_choose_redispatch(s);
Olivier Houcharda254a372019-04-05 15:30:12 +02001253 si->exp = TICK_ETERNITY;
1254 res->rex = TICK_ETERNITY;
1255 res->to_forward = 0;
1256 res->analyse_exp = TICK_ETERNITY;
1257 res->total = 0;
Olivier Houcharda254a372019-04-05 15:30:12 +02001258 si_release_endpoint(&s->si[1]);
Olivier Houcharda254a372019-04-05 15:30:12 +02001259
Christopher Faulet9f5382e2021-05-21 13:46:14 +02001260 b_free(&req->buf);
1261 /* Swap the L7 buffer with the channel buffer */
1262 /* We know we stored the co_data as b_data, so get it there */
1263 co_data = b_data(&si->l7_buffer);
1264 b_set_data(&si->l7_buffer, b_size(&si->l7_buffer));
1265 b_xfer(&req->buf, &si->l7_buffer, b_data(&si->l7_buffer));
1266 co_set_data(req, co_data);
Christopher Faulet5b82cc52020-10-12 15:18:50 +02001267
Ilya Shipitsinacf84592021-02-06 22:29:08 +05001268 DBG_TRACE_DEVEL("perform a L7 retry", STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, s->txn);
Christopher Faulet9f5382e2021-05-21 13:46:14 +02001269
Olivier Houcharda254a372019-04-05 15:30:12 +02001270 b_reset(&res->buf);
1271 co_set_data(res, 0);
1272 return 0;
1273}
1274
Christopher Faulete0768eb2018-10-03 16:38:02 +02001275/* This stream analyser waits for a complete HTTP response. It returns 1 if the
1276 * processing can continue on next analysers, or zero if it either needs more
1277 * data or wants to immediately abort the response (eg: timeout, error, ...). It
1278 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->res.analysers
1279 * when it has nothing left to do, and may remove any analyser when it wants to
1280 * abort.
1281 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001282int http_wait_for_response(struct stream *s, struct channel *rep, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001283{
Christopher Faulet9768c262018-10-22 09:34:31 +02001284 /*
1285 * We will analyze a complete HTTP response to check the its syntax.
1286 *
1287 * Once the start line and all headers are received, we may perform a
1288 * capture of the error (if any), and we will set a few fields. We also
1289 * logging and finally headers capture.
1290 */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001291 struct session *sess = s->sess;
1292 struct http_txn *txn = s->txn;
1293 struct http_msg *msg = &txn->rsp;
Christopher Faulet9768c262018-10-22 09:34:31 +02001294 struct htx *htx;
Olivier Houcharda254a372019-04-05 15:30:12 +02001295 struct stream_interface *si_b = &s->si[1];
Christopher Faulet61608322018-11-23 16:23:45 +01001296 struct connection *srv_conn;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001297 struct htx_sl *sl;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001298 int n;
1299
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001300 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001301
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001302 htx = htxbuf(&rep->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001303
Willy Tarreau4236f032019-03-05 10:43:32 +01001304 /* Parsing errors are caught here */
1305 if (htx->flags & HTX_FL_PARSING_ERROR)
1306 goto return_bad_res;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001307 if (htx->flags & HTX_FL_PROCESSING_ERROR)
1308 goto return_int_err;
Willy Tarreau4236f032019-03-05 10:43:32 +01001309
Christopher Faulete0768eb2018-10-03 16:38:02 +02001310 /*
1311 * Now we quickly check if we have found a full valid response.
1312 * If not so, we check the FD and buffer states before leaving.
1313 * A full response is indicated by the fact that we have seen
1314 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
1315 * responses are checked first.
1316 *
1317 * Depending on whether the client is still there or not, we
1318 * may send an error response back or not. Note that normally
1319 * we should only check for HTTP status there, and check I/O
1320 * errors somewhere else.
1321 */
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001322 next_one:
Christopher Faulet29f17582019-05-23 11:03:26 +02001323 if (unlikely(htx_is_empty(htx) || htx->first == -1)) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001324 /* 1: have we encountered a read error ? */
1325 if (rep->flags & CF_READ_ERROR) {
Olivier Houchard865d8392019-05-03 22:46:27 +02001326 struct connection *conn = NULL;
1327
Olivier Houchard865d8392019-05-03 22:46:27 +02001328 if (objt_cs(s->si[1].end))
1329 conn = objt_cs(s->si[1].end)->conn;
1330
Christopher Fauletd9769232021-05-26 12:15:37 +02001331 /* Perform a L7 retry because server refuses the early data. */
1332 if ((si_b->flags & SI_FL_L7_RETRY) &&
1333 (s->be->retry_type & PR_RE_EARLY_ERROR) &&
1334 conn && conn->err_code == CO_ER_SSL_EARLY_FAILED &&
1335 do_l7_retry(s, si_b) == 0) {
1336 DBG_TRACE_DEVEL("leaving on L7 retry",
1337 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
1338 return 0;
1339 }
1340
Olivier Houchard6db16992019-05-17 15:40:49 +02001341 if (txn->flags & TX_NOT_FIRST)
1342 goto abort_keep_alive;
1343
Willy Tarreau4781b152021-04-06 13:53:36 +02001344 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001345 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001346 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001347 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001348 }
1349
Christopher Fauletd9769232021-05-26 12:15:37 +02001350 /* if the server refused the early data, just send a 425 */
1351 if (conn && conn->err_code == CO_ER_SSL_EARLY_FAILED)
Olivier Houchard865d8392019-05-03 22:46:27 +02001352 txn->status = 425;
Christopher Fauletd9769232021-05-26 12:15:37 +02001353 else {
1354 txn->status = 502;
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001355 stream_inc_http_fail_ctr(s);
Christopher Fauletd9769232021-05-26 12:15:37 +02001356 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001357
1358 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001359 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001360
1361 if (!(s->flags & SF_ERR_MASK))
1362 s->flags |= SF_ERR_SRVCL;
1363 if (!(s->flags & SF_FINST_MASK))
1364 s->flags |= SF_FINST_H;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001365 DBG_TRACE_DEVEL("leaving on error",
1366 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001367 return 0;
1368 }
1369
Christopher Faulet9768c262018-10-22 09:34:31 +02001370 /* 2: read timeout : return a 504 to the client. */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001371 else if (rep->flags & CF_READ_TIMEOUT) {
Olivier Houcharda254a372019-04-05 15:30:12 +02001372 if ((si_b->flags & SI_FL_L7_RETRY) &&
1373 (s->be->retry_type & PR_RE_TIMEOUT)) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001374 if (co_data(rep) || 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);
Olivier Houcharda254a372019-04-05 15:30:12 +02001377 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001378 }
Olivier Houcharda254a372019-04-05 15:30:12 +02001379 }
Willy Tarreau4781b152021-04-06 13:53:36 +02001380 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001381 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001382 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001383 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001384 }
1385
Christopher Faulete0768eb2018-10-03 16:38:02 +02001386 txn->status = 504;
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001387 stream_inc_http_fail_ctr(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001388 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001389 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001390
1391 if (!(s->flags & SF_ERR_MASK))
1392 s->flags |= SF_ERR_SRVTO;
1393 if (!(s->flags & SF_FINST_MASK))
1394 s->flags |= SF_FINST_H;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001395 DBG_TRACE_DEVEL("leaving on error",
1396 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001397 return 0;
1398 }
1399
Christopher Faulet9768c262018-10-22 09:34:31 +02001400 /* 3: client abort with an abortonclose */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001401 else if ((rep->flags & CF_SHUTR) && ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001402 _HA_ATOMIC_INC(&sess->fe->fe_counters.cli_aborts);
1403 _HA_ATOMIC_INC(&s->be->be_counters.cli_aborts);
William Lallemand36119de2021-03-08 15:26:48 +01001404 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001405 _HA_ATOMIC_INC(&sess->listener->counters->cli_aborts);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001406 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001407 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.cli_aborts);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001408
Christopher Faulete0768eb2018-10-03 16:38:02 +02001409 txn->status = 400;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001410 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001411
1412 if (!(s->flags & SF_ERR_MASK))
1413 s->flags |= SF_ERR_CLICL;
1414 if (!(s->flags & SF_FINST_MASK))
1415 s->flags |= SF_FINST_H;
1416
1417 /* process_stream() will take care of the error */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001418 DBG_TRACE_DEVEL("leaving on error",
1419 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001420 return 0;
1421 }
1422
Christopher Faulet9768c262018-10-22 09:34:31 +02001423 /* 4: close from server, capture the response if the server has started to respond */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001424 else if (rep->flags & CF_SHUTR) {
Olivier Houcharda254a372019-04-05 15:30:12 +02001425 if ((si_b->flags & SI_FL_L7_RETRY) &&
1426 (s->be->retry_type & PR_RE_DISCONNECTED)) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001427 if (co_data(rep) || do_l7_retry(s, si_b) == 0) {
1428 DBG_TRACE_DEVEL("leaving on L7 retry",
1429 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharda254a372019-04-05 15:30:12 +02001430 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001431 }
Olivier Houcharda254a372019-04-05 15:30:12 +02001432 }
1433
Olivier Houchard6db16992019-05-17 15:40:49 +02001434 if (txn->flags & TX_NOT_FIRST)
1435 goto abort_keep_alive;
1436
Willy Tarreau4781b152021-04-06 13:53:36 +02001437 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001438 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001439 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001440 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001441 }
1442
Christopher Faulete0768eb2018-10-03 16:38:02 +02001443 txn->status = 502;
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001444 stream_inc_http_fail_ctr(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001445 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001446 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001447
1448 if (!(s->flags & SF_ERR_MASK))
1449 s->flags |= SF_ERR_SRVCL;
1450 if (!(s->flags & SF_FINST_MASK))
1451 s->flags |= SF_FINST_H;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001452 DBG_TRACE_DEVEL("leaving on error",
1453 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001454 return 0;
1455 }
1456
Christopher Faulet9768c262018-10-22 09:34:31 +02001457 /* 5: write error to client (we don't send any message then) */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001458 else if (rep->flags & CF_WRITE_ERROR) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001459 if (txn->flags & TX_NOT_FIRST)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001460 goto abort_keep_alive;
1461
Willy Tarreau4781b152021-04-06 13:53:36 +02001462 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001463 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001464 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001465 rep->analysers &= AN_RES_FLT_END;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001466
1467 if (!(s->flags & SF_ERR_MASK))
1468 s->flags |= SF_ERR_CLICL;
1469 if (!(s->flags & SF_FINST_MASK))
1470 s->flags |= SF_FINST_H;
1471
1472 /* process_stream() will take care of the error */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001473 DBG_TRACE_DEVEL("leaving on error",
1474 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001475 return 0;
1476 }
1477
1478 channel_dont_close(rep);
1479 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001480 DBG_TRACE_DEVEL("waiting for more data",
1481 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001482 return 0;
1483 }
1484
1485 /* More interesting part now : we know that we have a complete
1486 * response which at least looks like HTTP. We have an indicator
1487 * of each header's length, so we can parse them quickly.
1488 */
Christopher Faulet29f17582019-05-23 11:03:26 +02001489 BUG_ON(htx_get_first_type(htx) != HTX_BLK_RES_SL);
Christopher Faulet297fbb42019-05-13 14:41:27 +02001490 sl = http_get_stline(htx);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001491
Christopher Faulet1f08bff2021-05-26 13:14:39 +02001492 /* Perform a L7 retry because of the status code */
1493 if ((si_b->flags & SI_FL_L7_RETRY) &&
1494 l7_status_match(s->be, sl->info.res.status) &&
1495 do_l7_retry(s, si_b) == 0) {
1496 DBG_TRACE_DEVEL("leaving on L7 retry", STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
1497 return 0;
1498 }
1499
1500 /* Now, L7 buffer is useless, it can be released */
1501 b_free(&s->si[1].l7_buffer);
1502
1503 msg->msg_state = HTTP_MSG_BODY;
1504
1505
Christopher Faulet9768c262018-10-22 09:34:31 +02001506 /* 0: we might have to print this header in debug mode */
1507 if (unlikely((global.mode & MODE_DEBUG) &&
1508 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
1509 int32_t pos;
1510
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001511 http_debug_stline("srvrep", s, sl);
Christopher Faulet9768c262018-10-22 09:34:31 +02001512
Christopher Fauleta3f15502019-05-13 15:27:23 +02001513 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001514 struct htx_blk *blk = htx_get_blk(htx, pos);
1515 enum htx_blk_type type = htx_get_blk_type(blk);
1516
1517 if (type == HTX_BLK_EOH)
1518 break;
1519 if (type != HTX_BLK_HDR)
1520 continue;
1521
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001522 http_debug_hdr("srvhdr", s,
1523 htx_get_blk_name(htx, blk),
1524 htx_get_blk_value(htx, blk));
Christopher Faulet9768c262018-10-22 09:34:31 +02001525 }
1526 }
1527
Christopher Faulet03599112018-11-27 11:21:21 +01001528 /* 1: get the status code and the version. Also set HTTP flags */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001529 txn->status = sl->info.res.status;
Christopher Faulet03599112018-11-27 11:21:21 +01001530 if (sl->flags & HTX_SL_F_VER_11)
Christopher Faulet9768c262018-10-22 09:34:31 +02001531 msg->flags |= HTTP_MSGF_VER_11;
Christopher Faulet03599112018-11-27 11:21:21 +01001532 if (sl->flags & HTX_SL_F_XFER_LEN) {
1533 msg->flags |= HTTP_MSGF_XFER_LEN;
Christopher Faulet2a408542020-11-20 14:22:37 +01001534 if (sl->flags & HTX_SL_F_CLEN)
1535 msg->flags |= HTTP_MSGF_CNT_LEN;
1536 else if (sl->flags & HTX_SL_F_CHNK)
1537 msg->flags |= HTTP_MSGF_TE_CHNK;
Christopher Faulet03599112018-11-27 11:21:21 +01001538 }
Christopher Faulet2a408542020-11-20 14:22:37 +01001539 if (sl->flags & HTX_SL_F_BODYLESS)
1540 msg->flags |= HTTP_MSGF_BODYLESS;
Christopher Faulet576c3582021-01-08 15:53:01 +01001541 if (sl->flags & HTX_SL_F_CONN_UPG)
1542 msg->flags |= HTTP_MSGF_CONN_UPG;
Christopher Faulet9768c262018-10-22 09:34:31 +02001543
1544 n = txn->status / 100;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001545 if (n < 1 || n > 5)
1546 n = 0;
Christopher Faulet9768c262018-10-22 09:34:31 +02001547
Christopher Faulete0768eb2018-10-03 16:38:02 +02001548 /* when the client triggers a 4xx from the server, it's most often due
1549 * to a missing object or permission. These events should be tracked
1550 * because if they happen often, it may indicate a brute force or a
1551 * vulnerability scan.
1552 */
1553 if (n == 4)
1554 stream_inc_http_err_ctr(s);
1555
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001556 if (n == 5 && txn->status != 501 && txn->status != 505)
1557 stream_inc_http_fail_ctr(s);
1558
Marcin Deranek3c27dda2020-05-15 18:32:51 +02001559 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001560 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.p.http.rsp[n]);
1561 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.p.http.cum_req);
Marcin Deranek3c27dda2020-05-15 18:32:51 +02001562 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001563
Christopher Faulete0768eb2018-10-03 16:38:02 +02001564 /* Adjust server's health based on status code. Note: status codes 501
1565 * and 505 are triggered on demand by client request, so we must not
1566 * count them as server failures.
1567 */
1568 if (objt_server(s->target)) {
1569 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001570 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_OK);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001571 else
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001572 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_STS);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001573 }
1574
1575 /*
1576 * We may be facing a 100-continue response, or any other informational
1577 * 1xx response which is non-final, in which case this is not the right
1578 * response, and we're waiting for the next one. Let's allow this response
1579 * to go to the client and wait for the next one. There's an exception for
1580 * 101 which is used later in the code to switch protocols.
1581 */
1582 if (txn->status < 200 &&
1583 (txn->status == 100 || txn->status >= 102)) {
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001584 FLT_STRM_CB(s, flt_http_reset(s, msg));
Christopher Faulet421e7692019-06-13 11:16:45 +02001585 htx->first = channel_htx_fwd_headers(rep, htx);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001586 msg->msg_state = HTTP_MSG_RPBEFORE;
Christopher Faulet3499f622019-09-03 15:23:54 +02001587 msg->flags = 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001588 txn->status = 0;
1589 s->logs.t_data = -1; /* was not a response yet */
Christopher Faulet7d518452020-08-31 11:07:07 +02001590 rep->flags |= CF_SEND_DONTWAIT; /* Send ASAP informational messages */
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001591 goto next_one;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001592 }
1593
Christopher Faulet6e6c7b12021-01-08 16:02:05 +01001594 /* A 101-switching-protocols must contains a Connection header with the
1595 * "upgrade" option and the request too. It means both are agree to
1596 * upgrade. It is not so strict because there is no test on the Upgrade
1597 * header content. But it is probably stronger enough for now.
1598 */
1599 if (txn->status == 101 &&
1600 (!(txn->req.flags & HTTP_MSGF_CONN_UPG) || !(txn->rsp.flags & HTTP_MSGF_CONN_UPG)))
1601 goto return_bad_res;
1602
Christopher Faulete0768eb2018-10-03 16:38:02 +02001603 /*
1604 * 2: check for cacheability.
1605 */
1606
1607 switch (txn->status) {
1608 case 200:
1609 case 203:
1610 case 204:
1611 case 206:
1612 case 300:
1613 case 301:
1614 case 404:
1615 case 405:
1616 case 410:
1617 case 414:
1618 case 501:
1619 break;
1620 default:
1621 /* RFC7231#6.1:
1622 * Responses with status codes that are defined as
1623 * cacheable by default (e.g., 200, 203, 204, 206,
1624 * 300, 301, 404, 405, 410, 414, and 501 in this
1625 * specification) can be reused by a cache with
1626 * heuristic expiration unless otherwise indicated
1627 * by the method definition or explicit cache
1628 * controls [RFC7234]; all other status codes are
1629 * not cacheable by default.
1630 */
1631 txn->flags &= ~(TX_CACHEABLE | TX_CACHE_COOK);
1632 break;
1633 }
1634
1635 /*
1636 * 3: we may need to capture headers
1637 */
1638 s->logs.logwait &= ~LW_RESP;
1639 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->res_cap))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001640 http_capture_headers(htx, s->res_cap, sess->fe->rsp_cap);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001641
Christopher Faulet9768c262018-10-22 09:34:31 +02001642 /* Skip parsing if no content length is possible. */
Christopher Fauletc75668e2020-12-07 18:10:32 +01001643 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status >= 200 && txn->status < 300) ||
Christopher Faulete0768eb2018-10-03 16:38:02 +02001644 txn->status == 101)) {
1645 /* Either we've established an explicit tunnel, or we're
1646 * switching the protocol. In both cases, we're very unlikely
1647 * to understand the next protocols. We have to switch to tunnel
1648 * mode, so that we transfer the request and responses then let
1649 * this protocol pass unmodified. When we later implement specific
1650 * parsers for such protocols, we'll want to check the Upgrade
1651 * header which contains information about that protocol for
1652 * responses with status 101 (eg: see RFC2817 about TLS).
1653 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02001654 txn->flags |= TX_CON_WANT_TUN;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001655 }
1656
Christopher Faulet61608322018-11-23 16:23:45 +01001657 /* check for NTML authentication headers in 401 (WWW-Authenticate) and
1658 * 407 (Proxy-Authenticate) responses and set the connection to private
1659 */
1660 srv_conn = cs_conn(objt_cs(s->si[1].end));
1661 if (srv_conn) {
1662 struct ist hdr;
1663 struct http_hdr_ctx ctx;
1664
1665 if (txn->status == 401)
1666 hdr = ist("WWW-Authenticate");
1667 else if (txn->status == 407)
1668 hdr = ist("Proxy-Authenticate");
1669 else
1670 goto end;
1671
1672 ctx.blk = NULL;
1673 while (http_find_header(htx, hdr, &ctx, 0)) {
Willy Tarreauf1dcced2020-05-07 19:27:02 +02001674 /* If www-authenticate contains "Negotiate", "Nego2", or "NTLM",
1675 * possibly followed by blanks and a base64 string, the connection
1676 * is private. Since it's a mess to deal with, we only check for
1677 * values starting with "NTLM" or "Nego". Note that often multiple
1678 * headers are sent by the server there.
1679 */
1680 if ((ctx.value.len >= 4 && strncasecmp(ctx.value.ptr, "Nego", 4) == 0) ||
Willy Tarreau49a1d282020-05-07 19:10:15 +02001681 (ctx.value.len >= 4 && strncasecmp(ctx.value.ptr, "NTLM", 4) == 0)) {
Olivier Houchard250031e2019-05-29 15:01:50 +02001682 sess->flags |= SESS_FL_PREFER_LAST;
Christopher Faulet08016ab2020-07-01 16:10:06 +02001683 conn_set_owner(srv_conn, sess, NULL);
Christopher Faulet21ddc742020-07-01 15:26:14 +02001684 conn_set_private(srv_conn);
Ilya Shipitsin6b79f382020-07-23 00:32:55 +05001685 /* If it fail now, the same will be done in mux->detach() callback */
Christopher Faulet08016ab2020-07-01 16:10:06 +02001686 session_add_conn(srv_conn->owner, srv_conn, srv_conn->target);
Willy Tarreauf1dcced2020-05-07 19:27:02 +02001687 break;
Olivier Houchard250031e2019-05-29 15:01:50 +02001688 }
Christopher Faulet61608322018-11-23 16:23:45 +01001689 }
1690 }
1691
1692 end:
Christopher Faulete0768eb2018-10-03 16:38:02 +02001693 /* we want to have the response time before we start processing it */
1694 s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now);
1695
1696 /* end of job, return OK */
1697 rep->analysers &= ~an_bit;
1698 rep->analyse_exp = TICK_ETERNITY;
1699 channel_auto_close(rep);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001700 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001701 return 1;
1702
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001703 return_int_err:
Willy Tarreau4781b152021-04-06 13:53:36 +02001704 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
1705 _HA_ATOMIC_INC(&s->be->be_counters.internal_errors);
William Lallemand36119de2021-03-08 15:26:48 +01001706 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001707 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Fauletb8a53712019-12-16 11:29:38 +01001708 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001709 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.internal_errors);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001710 txn->status = 500;
1711 if (!(s->flags & SF_ERR_MASK))
1712 s->flags |= SF_ERR_INTERNAL;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001713 goto return_prx_cond;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001714
1715 return_bad_res:
Willy Tarreau4781b152021-04-06 13:53:36 +02001716 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Faulet47365272018-10-31 17:40:50 +01001717 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001718 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001719 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Christopher Faulet47365272018-10-31 17:40:50 +01001720 }
Olivier Houcharde3249a92019-05-03 23:01:47 +02001721 if ((s->be->retry_type & PR_RE_JUNK_REQUEST) &&
Olivier Houchardad26d8d2019-05-10 17:48:28 +02001722 (si_b->flags & SI_FL_L7_RETRY) &&
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001723 do_l7_retry(s, si_b) == 0) {
1724 DBG_TRACE_DEVEL("leaving on L7 retry",
1725 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharde3249a92019-05-03 23:01:47 +02001726 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001727 }
Christopher Faulet47365272018-10-31 17:40:50 +01001728 txn->status = 502;
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001729 stream_inc_http_fail_ctr(s);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001730 /* fall through */
1731
Christopher Fauletb8a53712019-12-16 11:29:38 +01001732 return_prx_cond:
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001733 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet47365272018-10-31 17:40:50 +01001734
1735 if (!(s->flags & SF_ERR_MASK))
1736 s->flags |= SF_ERR_PRXCOND;
1737 if (!(s->flags & SF_FINST_MASK))
1738 s->flags |= SF_FINST_H;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001739
1740 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001741 DBG_TRACE_DEVEL("leaving on error",
1742 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulet47365272018-10-31 17:40:50 +01001743 return 0;
1744
Christopher Faulete0768eb2018-10-03 16:38:02 +02001745 abort_keep_alive:
1746 /* A keep-alive request to the server failed on a network error.
1747 * The client is required to retry. We need to close without returning
1748 * any other information so that the client retries.
1749 */
1750 txn->status = 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001751 s->logs.logwait = 0;
1752 s->logs.level = 0;
1753 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001754 http_reply_and_close(s, txn->status, NULL);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001755 DBG_TRACE_DEVEL("leaving by closing K/A connection",
1756 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001757 return 0;
1758}
1759
1760/* This function performs all the processing enabled for the current response.
1761 * It normally returns 1 unless it wants to break. It relies on buffers flags,
1762 * and updates s->res.analysers. It might make sense to explode it into several
1763 * other functions. It works like process_request (see indications above).
1764 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001765int http_process_res_common(struct stream *s, struct channel *rep, int an_bit, struct proxy *px)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001766{
1767 struct session *sess = s->sess;
1768 struct http_txn *txn = s->txn;
1769 struct http_msg *msg = &txn->rsp;
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001770 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001771 struct proxy *cur_proxy;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001772 enum rule_result ret = HTTP_RULE_RES_CONT;
1773
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001774 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
1775 return 0;
Christopher Faulet9768c262018-10-22 09:34:31 +02001776
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001777 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001778
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001779 htx = htxbuf(&rep->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001780
1781 /* The stats applet needs to adjust the Connection header but we don't
1782 * apply any filter there.
1783 */
1784 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
1785 rep->analysers &= ~an_bit;
1786 rep->analyse_exp = TICK_ETERNITY;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001787 goto end;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001788 }
1789
1790 /*
1791 * We will have to evaluate the filters.
1792 * As opposed to version 1.2, now they will be evaluated in the
1793 * filters order and not in the header order. This means that
1794 * each filter has to be validated among all headers.
1795 *
1796 * Filters are tried with ->be first, then with ->fe if it is
1797 * different from ->be.
1798 *
1799 * Maybe we are in resume condiion. In this case I choose the
1800 * "struct proxy" which contains the rule list matching the resume
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05001801 * pointer. If none of these "struct proxy" match, I initialise
Christopher Faulete0768eb2018-10-03 16:38:02 +02001802 * the process with the first one.
1803 *
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05001804 * In fact, I check only correspondence between the current list
Christopher Faulete0768eb2018-10-03 16:38:02 +02001805 * pointer and the ->fe rule list. If it doesn't match, I initialize
1806 * the loop with the ->be.
1807 */
Christopher Fauletd4150ad2021-10-13 15:35:55 +02001808 if (s->current_rule_list == &sess->fe->http_res_rules ||
1809 (sess->fe->defpx && s->current_rule_list == &sess->fe->defpx->http_res_rules))
Christopher Faulete0768eb2018-10-03 16:38:02 +02001810 cur_proxy = sess->fe;
1811 else
1812 cur_proxy = s->be;
Christopher Fauletd4150ad2021-10-13 15:35:55 +02001813
Christopher Faulete0768eb2018-10-03 16:38:02 +02001814 while (1) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001815 /* evaluate http-response rules */
1816 if (ret == HTTP_RULE_RES_CONT) {
Christopher Fauletd4150ad2021-10-13 15:35:55 +02001817 struct list *def_rules, *rules;
1818
1819 def_rules = ((cur_proxy->defpx && (cur_proxy == s->be || cur_proxy->defpx != s->be->defpx)) ? &cur_proxy->defpx->http_res_rules : NULL);
1820 rules = &cur_proxy->http_res_rules;
1821
1822 ret = http_res_get_intercept_rule(cur_proxy, def_rules, rules, s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001823
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001824 switch (ret) {
1825 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
1826 goto return_prx_yield;
1827
1828 case HTTP_RULE_RES_CONT:
1829 case HTTP_RULE_RES_STOP: /* nothing to do */
1830 break;
1831
1832 case HTTP_RULE_RES_DENY: /* deny or tarpit */
1833 goto deny;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001834
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001835 case HTTP_RULE_RES_ABRT: /* abort request, response already sent */
1836 goto return_prx_cond;
1837
1838 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
Christopher Fauletb8a53712019-12-16 11:29:38 +01001839 goto done;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001840
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001841 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
1842 goto return_bad_res;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001843
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001844 case HTTP_RULE_RES_ERROR: /* failed with a bad request */
1845 goto return_int_err;
1846 }
1847
1848 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001849
Christopher Faulete0768eb2018-10-03 16:38:02 +02001850 /* check whether we're already working on the frontend */
1851 if (cur_proxy == sess->fe)
1852 break;
1853 cur_proxy = sess->fe;
1854 }
1855
Christopher Faulete0768eb2018-10-03 16:38:02 +02001856 /* OK that's all we can do for 1xx responses */
1857 if (unlikely(txn->status < 200 && txn->status != 101))
Christopher Fauletf2824e62018-10-01 12:12:37 +02001858 goto end;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001859
1860 /*
1861 * Now check for a server cookie.
1862 */
1863 if (s->be->cookie_name || sess->fe->capture_name || (s->be->options & PR_O_CHK_CACHE))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001864 http_manage_server_side_cookies(s, rep);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001865
1866 /*
1867 * Check for cache-control or pragma headers if required.
1868 */
1869 if ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001870 http_check_response_for_cacheability(s, rep);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001871
1872 /*
1873 * Add server cookie in the response if needed
1874 */
1875 if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) &&
1876 !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) &&
1877 (!(s->flags & SF_DIRECT) ||
1878 ((s->be->cookie_maxidle || txn->cookie_last_date) &&
1879 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
1880 (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
1881 (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
1882 (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
1883 !(s->flags & SF_IGNORE_PRST)) {
1884 /* the server is known, it's not the one the client requested, or the
1885 * cookie's last seen date needs to be refreshed. We have to
1886 * insert a set-cookie here, except if we want to insert only on POST
1887 * requests and this one isn't. Note that servers which don't have cookies
1888 * (eg: some backup servers) will return a full cookie removal request.
1889 */
1890 if (!objt_server(s->target)->cookie) {
1891 chunk_printf(&trash,
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001892 "%s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
Christopher Faulete0768eb2018-10-03 16:38:02 +02001893 s->be->cookie_name);
1894 }
1895 else {
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001896 chunk_printf(&trash, "%s=%s", s->be->cookie_name, objt_server(s->target)->cookie);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001897
1898 if (s->be->cookie_maxidle || s->be->cookie_maxlife) {
1899 /* emit last_date, which is mandatory */
1900 trash.area[trash.data++] = COOKIE_DELIM_DATE;
1901 s30tob64((date.tv_sec+3) >> 2,
1902 trash.area + trash.data);
1903 trash.data += 5;
1904
1905 if (s->be->cookie_maxlife) {
1906 /* emit first_date, which is either the original one or
1907 * the current date.
1908 */
1909 trash.area[trash.data++] = COOKIE_DELIM_DATE;
1910 s30tob64(txn->cookie_first_date ?
1911 txn->cookie_first_date >> 2 :
1912 (date.tv_sec+3) >> 2,
1913 trash.area + trash.data);
1914 trash.data += 5;
1915 }
1916 }
1917 chunk_appendf(&trash, "; path=/");
1918 }
1919
1920 if (s->be->cookie_domain)
1921 chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain);
1922
1923 if (s->be->ck_opts & PR_CK_HTTPONLY)
1924 chunk_appendf(&trash, "; HttpOnly");
1925
1926 if (s->be->ck_opts & PR_CK_SECURE)
1927 chunk_appendf(&trash, "; Secure");
1928
Christopher Faulet2f533902020-01-21 11:06:48 +01001929 if (s->be->cookie_attrs)
1930 chunk_appendf(&trash, "; %s", s->be->cookie_attrs);
1931
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001932 if (unlikely(!http_add_header(htx, ist("Set-Cookie"), ist2(trash.area, trash.data))))
Christopher Fauletb8a53712019-12-16 11:29:38 +01001933 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001934
1935 txn->flags &= ~TX_SCK_MASK;
1936 if (__objt_server(s->target)->cookie && (s->flags & SF_DIRECT))
1937 /* the server did not change, only the date was updated */
1938 txn->flags |= TX_SCK_UPDATED;
1939 else
1940 txn->flags |= TX_SCK_INSERTED;
1941
1942 /* Here, we will tell an eventual cache on the client side that we don't
1943 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
1944 * Some caches understand the correct form: 'no-cache="set-cookie"', but
1945 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
1946 */
1947 if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
1948
1949 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
1950
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001951 if (unlikely(!http_add_header(htx, ist("Cache-control"), ist("private"))))
Christopher Fauletb8a53712019-12-16 11:29:38 +01001952 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001953 }
1954 }
1955
1956 /*
1957 * Check if result will be cacheable with a cookie.
1958 * We'll block the response if security checks have caught
1959 * nasty things such as a cacheable cookie.
1960 */
1961 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
1962 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
1963 (s->be->options & PR_O_CHK_CACHE)) {
1964 /* we're in presence of a cacheable response containing
1965 * a set-cookie header. We'll block it as requested by
1966 * the 'checkcache' option, and send an alert.
1967 */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001968 ha_alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
1969 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
1970 send_log(s->be, LOG_ALERT,
1971 "Blocking cacheable cookie in response from instance %s, server %s.\n",
1972 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
Christopher Fauletb8a53712019-12-16 11:29:38 +01001973 goto deny;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001974 }
1975
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001976 end:
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01001977 /*
1978 * Evaluate after-response rules before forwarding the response. rules
1979 * from the backend are evaluated first, then one from the frontend if
1980 * it differs.
1981 */
1982 if (!http_eval_after_res_rules(s))
1983 goto return_int_err;
1984
Christopher Fauletc2ac5e42021-03-08 18:20:09 +01001985 /* Filter the response headers if there are filters attached to the
1986 * stream.
1987 */
1988 if (HAS_FILTERS(s))
1989 rep->analysers |= AN_RES_FLT_HTTP_HDRS;
1990
Christopher Faulete0768eb2018-10-03 16:38:02 +02001991 /* Always enter in the body analyzer */
1992 rep->analysers &= ~AN_RES_FLT_XFER_DATA;
1993 rep->analysers |= AN_RES_HTTP_XFER_BODY;
1994
1995 /* if the user wants to log as soon as possible, without counting
1996 * bytes from the server, then this is the right moment. We have
1997 * to temporarily assign bytes_out to log what we currently have.
1998 */
1999 if (!LIST_ISEMPTY(&sess->fe->logformat) && !(s->logs.logwait & LW_BYTES)) {
2000 s->logs.t_close = s->logs.t_data; /* to get a valid end date */
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002001 s->logs.bytes_out = htx->data;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002002 s->do_log(s);
2003 s->logs.bytes_out = 0;
2004 }
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002005
Christopher Fauletb8a53712019-12-16 11:29:38 +01002006 done:
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002007 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002008 rep->analysers &= ~an_bit;
2009 rep->analyse_exp = TICK_ETERNITY;
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002010 s->current_rule = s->current_rule_list = NULL;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002011 return 1;
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002012
Christopher Fauletb8a53712019-12-16 11:29:38 +01002013 deny:
Willy Tarreau4781b152021-04-06 13:53:36 +02002014 _HA_ATOMIC_INC(&sess->fe->fe_counters.denied_resp);
2015 _HA_ATOMIC_INC(&s->be->be_counters.denied_resp);
William Lallemand36119de2021-03-08 15:26:48 +01002016 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02002017 _HA_ATOMIC_INC(&sess->listener->counters->denied_resp);
Christopher Fauleta08546b2019-12-16 16:07:34 +01002018 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02002019 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.denied_resp);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002020 goto return_prx_err;
2021
2022 return_int_err:
2023 txn->status = 500;
2024 if (!(s->flags & SF_ERR_MASK))
2025 s->flags |= SF_ERR_INTERNAL;
Willy Tarreau4781b152021-04-06 13:53:36 +02002026 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
2027 _HA_ATOMIC_INC(&s->be->be_counters.internal_errors);
Dragan Dosen9a006f92021-09-21 13:02:09 +02002028 if (sess->listener && sess->listener->counters)
2029 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Fauletcff0f732019-12-16 16:13:44 +01002030 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02002031 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.internal_errors);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002032 goto return_prx_err;
2033
2034 return_bad_res:
2035 txn->status = 502;
Willy Tarreau826f3ab2021-02-10 12:07:15 +01002036 stream_inc_http_fail_ctr(s);
Willy Tarreau4781b152021-04-06 13:53:36 +02002037 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Fauleta20a6532020-02-05 10:16:41 +01002038 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02002039 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Christopher Fauleta20a6532020-02-05 10:16:41 +01002040 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_RSP);
2041 }
Christopher Fauletb8a53712019-12-16 11:29:38 +01002042 /* fall through */
2043
2044 return_prx_err:
2045 http_reply_and_close(s, txn->status, http_error_message(s));
2046 /* fall through */
2047
2048 return_prx_cond:
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002049 s->logs.t_data = -1; /* was not a valid response */
2050 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002051
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002052 if (!(s->flags & SF_ERR_MASK))
2053 s->flags |= SF_ERR_PRXCOND;
2054 if (!(s->flags & SF_FINST_MASK))
2055 s->flags |= SF_FINST_H;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002056
Christopher Faulete58c0002020-03-02 16:21:01 +01002057 rep->analysers &= AN_RES_FLT_END;
2058 s->req.analysers &= AN_REQ_FLT_END;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002059 rep->analyse_exp = TICK_ETERNITY;
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002060 s->current_rule = s->current_rule_list = NULL;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002061 DBG_TRACE_DEVEL("leaving on error",
2062 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002063 return 0;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002064
2065 return_prx_yield:
2066 channel_dont_close(rep);
2067 DBG_TRACE_DEVEL("waiting for more data",
2068 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
2069 return 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002070}
2071
2072/* This function is an analyser which forwards response body (including chunk
2073 * sizes if any). It is called as soon as we must forward, even if we forward
2074 * zero byte. The only situation where it must not be called is when we're in
2075 * tunnel mode and we want to forward till the close. It's used both to forward
2076 * remaining data and to resync after end of body. It expects the msg_state to
2077 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
2078 * read more data, or 1 once we can go on with next request or end the stream.
2079 *
2080 * It is capable of compressing response data both in content-length mode and
2081 * in chunked mode. The state machines follows different flows depending on
2082 * whether content-length and chunked modes are used, since there are no
2083 * trailers in content-length :
2084 *
2085 * chk-mode cl-mode
2086 * ,----- BODY -----.
2087 * / \
2088 * V size > 0 V chk-mode
2089 * .--> SIZE -------------> DATA -------------> CRLF
2090 * | | size == 0 | last byte |
2091 * | v final crlf v inspected |
2092 * | TRAILERS -----------> DONE |
2093 * | |
2094 * `----------------------------------------------'
2095 *
2096 * Compression only happens in the DATA state, and must be flushed in final
2097 * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding
2098 * is performed at once on final states for all bytes parsed, or when leaving
2099 * on missing data.
2100 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002101int http_response_forward_body(struct stream *s, struct channel *res, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +02002102{
2103 struct session *sess = s->sess;
2104 struct http_txn *txn = s->txn;
2105 struct http_msg *msg = &s->txn->rsp;
Christopher Faulet9768c262018-10-22 09:34:31 +02002106 struct htx *htx;
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002107 int ret;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002108
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002109 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002110
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002111 htx = htxbuf(&res->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002112
Christopher Fauletb9a92f32019-09-09 10:15:21 +02002113 if (htx->flags & HTX_FL_PARSING_ERROR)
2114 goto return_bad_res;
2115 if (htx->flags & HTX_FL_PROCESSING_ERROR)
2116 goto return_int_err;
2117
Christopher Faulete0768eb2018-10-03 16:38:02 +02002118 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Christopher Fauletf2824e62018-10-01 12:12:37 +02002119 ((res->flags & CF_SHUTW) && (res->to_forward || co_data(res)))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02002120 /* Output closed while we were sending data. We must abort and
2121 * wake the other side up.
2122 */
Christopher Faulete0768eb2018-10-03 16:38:02 +02002123 msg->msg_state = HTTP_MSG_ERROR;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002124 http_end_response(s);
2125 http_end_request(s);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002126 DBG_TRACE_DEVEL("leaving on error",
2127 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002128 return 1;
2129 }
2130
Christopher Faulet9768c262018-10-22 09:34:31 +02002131 if (msg->msg_state == HTTP_MSG_BODY)
2132 msg->msg_state = HTTP_MSG_DATA;
2133
Christopher Faulete0768eb2018-10-03 16:38:02 +02002134 /* in most states, we should abort in case of early close */
2135 channel_auto_close(res);
2136
Christopher Faulete0768eb2018-10-03 16:38:02 +02002137 if (res->to_forward) {
Christopher Faulet66af0b22019-03-22 14:54:52 +01002138 if (res->to_forward == CHN_INFINITE_FORWARD) {
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002139 if (res->flags & CF_EOI)
2140 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet66af0b22019-03-22 14:54:52 +01002141 }
2142 else {
2143 /* We can't process the buffer's contents yet */
2144 res->flags |= CF_WAKE_WRITE;
2145 goto missing_data_or_waiting;
2146 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02002147 }
2148
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002149 if (msg->msg_state >= HTTP_MSG_ENDING)
2150 goto ending;
2151
Christopher Fauletc75668e2020-12-07 18:10:32 +01002152 if ((txn->meth == HTTP_METH_CONNECT && txn->status >= 200 && txn->status < 300) || txn->status == 101 ||
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002153 (!(msg->flags & HTTP_MSGF_XFER_LEN) && !HAS_RSP_DATA_FILTERS(s))) {
2154 msg->msg_state = HTTP_MSG_ENDING;
2155 goto ending;
2156 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002157
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002158 /* Forward input data. We get it by removing all outgoing data not
2159 * forwarded yet from HTX data size. If there are some data filters, we
2160 * let them decide the amount of data to forward.
Christopher Faulet9768c262018-10-22 09:34:31 +02002161 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002162 if (HAS_RSP_DATA_FILTERS(s)) {
2163 ret = flt_http_payload(s, msg, htx->data);
2164 if (ret < 0)
2165 goto return_bad_res;
Christopher Faulet421e7692019-06-13 11:16:45 +02002166 c_adv(res, ret);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002167 }
2168 else {
Christopher Faulet421e7692019-06-13 11:16:45 +02002169 c_adv(res, htx->data - co_data(res));
Christopher Faulet66af0b22019-03-22 14:54:52 +01002170 if (msg->flags & HTTP_MSGF_XFER_LEN)
2171 channel_htx_forward_forever(res, htx);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002172 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002173
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002174 if (htx->data != co_data(res))
2175 goto missing_data_or_waiting;
2176
2177 if (!(msg->flags & HTTP_MSGF_XFER_LEN) && res->flags & CF_SHUTR) {
2178 msg->msg_state = HTTP_MSG_ENDING;
2179 goto ending;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002180 }
2181
Christopher Faulet9768c262018-10-22 09:34:31 +02002182 /* Check if the end-of-message is reached and if so, switch the message
Christopher Fauletd20fdb02019-06-13 16:43:22 +02002183 * in HTTP_MSG_ENDING state. Then if all data was marked to be
2184 * forwarded, set the state to HTTP_MSG_DONE.
Christopher Faulet9768c262018-10-22 09:34:31 +02002185 */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002186 if (!(htx->flags & HTX_FL_EOM))
Christopher Faulet9768c262018-10-22 09:34:31 +02002187 goto missing_data_or_waiting;
2188
Christopher Fauletd20fdb02019-06-13 16:43:22 +02002189 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet9768c262018-10-22 09:34:31 +02002190
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002191 ending:
Christopher Faulet2151cdd2020-07-22 16:34:59 +02002192 res->flags &= ~CF_EXPECT_MORE; /* no more data are expected */
2193
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002194 /* other states, ENDING...TUNNEL */
2195 if (msg->msg_state >= HTTP_MSG_DONE)
2196 goto done;
Christopher Faulet9768c262018-10-22 09:34:31 +02002197
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002198 if (HAS_RSP_DATA_FILTERS(s)) {
2199 ret = flt_http_end(s, msg);
2200 if (ret <= 0) {
2201 if (!ret)
2202 goto missing_data_or_waiting;
2203 goto return_bad_res;
2204 }
2205 }
2206
Christopher Fauletc75668e2020-12-07 18:10:32 +01002207 if ((txn->meth == HTTP_METH_CONNECT && txn->status >= 200 && txn->status < 300) || txn->status == 101 ||
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002208 !(msg->flags & HTTP_MSGF_XFER_LEN)) {
2209 msg->msg_state = HTTP_MSG_TUNNEL;
2210 goto ending;
2211 }
2212 else {
2213 msg->msg_state = HTTP_MSG_DONE;
2214 res->to_forward = 0;
2215 }
2216
2217 done:
2218
2219 channel_dont_close(res);
2220
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002221 http_end_response(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002222 if (!(res->analysers & an_bit)) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002223 http_end_request(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002224 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
2225 if (res->flags & CF_SHUTW) {
2226 /* response errors are most likely due to the
2227 * client aborting the transfer. */
Christopher Faulet93e02d82019-03-08 14:18:50 +01002228 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002229 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02002230 goto return_bad_res;
2231 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002232 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002233 return 1;
2234 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002235 DBG_TRACE_DEVEL("waiting for the end of the HTTP txn",
2236 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002237 return 0;
2238
2239 missing_data_or_waiting:
2240 if (res->flags & CF_SHUTW)
Christopher Faulet93e02d82019-03-08 14:18:50 +01002241 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002242
2243 /* stop waiting for data if the input is closed before the end. If the
2244 * client side was already closed, it means that the client has aborted,
2245 * so we don't want to count this as a server abort. Otherwise it's a
2246 * server abort.
2247 */
Christopher Fauletd20fdb02019-06-13 16:43:22 +02002248 if (msg->msg_state < HTTP_MSG_ENDING && res->flags & CF_SHUTR) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02002249 if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))
Christopher Faulet93e02d82019-03-08 14:18:50 +01002250 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002251 /* If we have some pending data, we continue the processing */
Christopher Faulet93e02d82019-03-08 14:18:50 +01002252 if (htx_is_empty(htx))
2253 goto return_srv_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002254 }
2255
Christopher Faulete0768eb2018-10-03 16:38:02 +02002256 /* When TE: chunked is used, we need to get there again to parse
2257 * remaining chunks even if the server has closed, so we don't want to
Christopher Faulet9768c262018-10-22 09:34:31 +02002258 * set CF_DONTCLOSE. Similarly when there is a content-leng or if there
2259 * are filters registered on the stream, we don't want to forward a
2260 * close
Christopher Faulete0768eb2018-10-03 16:38:02 +02002261 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002262 if ((msg->flags & HTTP_MSGF_XFER_LEN) || HAS_RSP_DATA_FILTERS(s))
Christopher Faulete0768eb2018-10-03 16:38:02 +02002263 channel_dont_close(res);
2264
2265 /* We know that more data are expected, but we couldn't send more that
2266 * what we did. So we always set the CF_EXPECT_MORE flag so that the
2267 * system knows it must not set a PUSH on this first part. Interactive
2268 * modes are already handled by the stream sock layer. We must not do
2269 * this in content-length mode because it could present the MSG_MORE
2270 * flag with the last block of forwarded data, which would cause an
2271 * additional delay to be observed by the receiver.
2272 */
Christopher Faulet2151cdd2020-07-22 16:34:59 +02002273 if (HAS_RSP_DATA_FILTERS(s))
Christopher Faulete0768eb2018-10-03 16:38:02 +02002274 res->flags |= CF_EXPECT_MORE;
2275
2276 /* the stream handler will take care of timeouts and errors */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002277 DBG_TRACE_DEVEL("waiting for more data to forward",
2278 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002279 return 0;
2280
Christopher Faulet93e02d82019-03-08 14:18:50 +01002281 return_srv_abort:
Willy Tarreau4781b152021-04-06 13:53:36 +02002282 _HA_ATOMIC_INC(&sess->fe->fe_counters.srv_aborts);
2283 _HA_ATOMIC_INC(&s->be->be_counters.srv_aborts);
William Lallemand36119de2021-03-08 15:26:48 +01002284 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02002285 _HA_ATOMIC_INC(&sess->listener->counters->srv_aborts);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002286 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02002287 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.srv_aborts);
Willy Tarreau826f3ab2021-02-10 12:07:15 +01002288 stream_inc_http_fail_ctr(s);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002289 if (!(s->flags & SF_ERR_MASK))
2290 s->flags |= SF_ERR_SRVCL;
2291 goto return_error;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002292
Christopher Faulet93e02d82019-03-08 14:18:50 +01002293 return_cli_abort:
Willy Tarreau4781b152021-04-06 13:53:36 +02002294 _HA_ATOMIC_INC(&sess->fe->fe_counters.cli_aborts);
2295 _HA_ATOMIC_INC(&s->be->be_counters.cli_aborts);
William Lallemand36119de2021-03-08 15:26:48 +01002296 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02002297 _HA_ATOMIC_INC(&sess->listener->counters->cli_aborts);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002298 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02002299 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.cli_aborts);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002300 if (!(s->flags & SF_ERR_MASK))
2301 s->flags |= SF_ERR_CLICL;
2302 goto return_error;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002303
Christopher Fauletb9a92f32019-09-09 10:15:21 +02002304 return_int_err:
Willy Tarreau4781b152021-04-06 13:53:36 +02002305 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
2306 _HA_ATOMIC_INC(&s->be->be_counters.internal_errors);
William Lallemand36119de2021-03-08 15:26:48 +01002307 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02002308 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002309 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02002310 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.internal_errors);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02002311 if (!(s->flags & SF_ERR_MASK))
2312 s->flags |= SF_ERR_INTERNAL;
2313 goto return_error;
2314
Christopher Faulet93e02d82019-03-08 14:18:50 +01002315 return_bad_res:
Willy Tarreau4781b152021-04-06 13:53:36 +02002316 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002317 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02002318 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002319 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_RSP);
2320 }
Willy Tarreau826f3ab2021-02-10 12:07:15 +01002321 stream_inc_http_fail_ctr(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002322 if (!(s->flags & SF_ERR_MASK))
Christopher Faulet93e02d82019-03-08 14:18:50 +01002323 s->flags |= SF_ERR_SRVCL;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002324 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +02002325
Christopher Faulet93e02d82019-03-08 14:18:50 +01002326 return_error:
Christopher Faulete0768eb2018-10-03 16:38:02 +02002327 /* don't send any error message as we're in the body */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002328 http_reply_and_close(s, txn->status, NULL);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002329 if (!(s->flags & SF_FINST_MASK))
2330 s->flags |= SF_FINST_D;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002331 DBG_TRACE_DEVEL("leaving on error",
2332 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002333 return 0;
2334}
2335
Christopher Fauletf2824e62018-10-01 12:12:37 +02002336/* Perform an HTTP redirect based on the information in <rule>. The function
Willy Tarreaubc1223b2021-09-02 16:54:33 +02002337 * returns zero in case of an irrecoverable error such as too large a request
2338 * to build a valid response, 1 in case of successful redirect (hence the rule
2339 * is final), or 2 if the rule has to be silently skipped.
Christopher Fauletf2824e62018-10-01 12:12:37 +02002340 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002341int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn)
Christopher Fauletf2824e62018-10-01 12:12:37 +02002342{
Christopher Faulet99daf282018-11-28 22:58:13 +01002343 struct channel *req = &s->req;
2344 struct channel *res = &s->res;
2345 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01002346 struct htx_sl *sl;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002347 struct buffer *chunk;
Christopher Faulet99daf282018-11-28 22:58:13 +01002348 struct ist status, reason, location;
2349 unsigned int flags;
Christopher Faulet08e66462019-05-23 16:44:59 +02002350 int close = 0; /* Try to keep the connection alive byt default */
Christopher Fauletf2824e62018-10-01 12:12:37 +02002351
2352 chunk = alloc_trash_chunk();
Christopher Fauletb8a53712019-12-16 11:29:38 +01002353 if (!chunk) {
2354 if (!(s->flags & SF_ERR_MASK))
2355 s->flags |= SF_ERR_RESOURCE;
Christopher Faulet99daf282018-11-28 22:58:13 +01002356 goto fail;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002357 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002358
Christopher Faulet99daf282018-11-28 22:58:13 +01002359 /*
2360 * Create the location
2361 */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002362 htx = htxbuf(&req->buf);
Christopher Fauletf2824e62018-10-01 12:12:37 +02002363 switch(rule->type) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002364 case REDIRECT_TYPE_SCHEME: {
2365 struct http_hdr_ctx ctx;
2366 struct ist path, host;
Amaury Denoyellec453f952021-07-06 11:40:12 +02002367 struct http_uri_parser parser;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002368
Christopher Faulet99daf282018-11-28 22:58:13 +01002369 host = ist("");
2370 ctx.blk = NULL;
2371 if (http_find_header(htx, ist("Host"), &ctx, 0))
2372 host = ctx.value;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002373
Christopher Faulet297fbb42019-05-13 14:41:27 +02002374 sl = http_get_stline(htx);
Amaury Denoyellec453f952021-07-06 11:40:12 +02002375 parser = http_uri_parser_init(htx_sl_req_uri(sl));
2376 path = http_parse_path(&parser);
Christopher Faulet99daf282018-11-28 22:58:13 +01002377 /* build message using path */
Tim Duesterhused526372020-03-05 17:56:33 +01002378 if (isttest(path)) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002379 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2380 int qs = 0;
2381 while (qs < path.len) {
2382 if (*(path.ptr + qs) == '?') {
2383 path.len = qs;
2384 break;
2385 }
2386 qs++;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002387 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002388 }
2389 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002390 else
2391 path = ist("/");
Christopher Fauletf2824e62018-10-01 12:12:37 +02002392
Christopher Faulet99daf282018-11-28 22:58:13 +01002393 if (rule->rdr_str) { /* this is an old "redirect" rule */
2394 /* add scheme */
2395 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2396 goto fail;
2397 }
2398 else {
2399 /* add scheme with executing log format */
2400 chunk->data += build_logline(s, chunk->area + chunk->data,
2401 chunk->size - chunk->data,
2402 &rule->rdr_fmt);
2403 }
2404 /* add "://" + host + path */
2405 if (!chunk_memcat(chunk, "://", 3) ||
2406 !chunk_memcat(chunk, host.ptr, host.len) ||
2407 !chunk_memcat(chunk, path.ptr, path.len))
2408 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002409
Christopher Faulet99daf282018-11-28 22:58:13 +01002410 /* append a slash at the end of the location if needed and missing */
2411 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
2412 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
2413 if (chunk->data + 1 >= chunk->size)
2414 goto fail;
2415 chunk->area[chunk->data++] = '/';
2416 }
2417 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002418 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002419
Christopher Faulet99daf282018-11-28 22:58:13 +01002420 case REDIRECT_TYPE_PREFIX: {
2421 struct ist path;
Amaury Denoyellec453f952021-07-06 11:40:12 +02002422 struct http_uri_parser parser;
Christopher Faulet99daf282018-11-28 22:58:13 +01002423
Christopher Faulet297fbb42019-05-13 14:41:27 +02002424 sl = http_get_stline(htx);
Amaury Denoyellec453f952021-07-06 11:40:12 +02002425 parser = http_uri_parser_init(htx_sl_req_uri(sl));
2426 path = http_parse_path(&parser);
Christopher Faulet99daf282018-11-28 22:58:13 +01002427 /* build message using path */
Tim Duesterhused526372020-03-05 17:56:33 +01002428 if (isttest(path)) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002429 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2430 int qs = 0;
2431 while (qs < path.len) {
2432 if (*(path.ptr + qs) == '?') {
2433 path.len = qs;
2434 break;
2435 }
2436 qs++;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002437 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002438 }
2439 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002440 else
2441 path = ist("/");
Christopher Fauletf2824e62018-10-01 12:12:37 +02002442
Christopher Faulet99daf282018-11-28 22:58:13 +01002443 if (rule->rdr_str) { /* this is an old "redirect" rule */
2444 /* add prefix. Note that if prefix == "/", we don't want to
2445 * add anything, otherwise it makes it hard for the user to
2446 * configure a self-redirection.
2447 */
2448 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
2449 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2450 goto fail;
2451 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002452 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002453 else {
2454 /* add prefix with executing log format */
2455 chunk->data += build_logline(s, chunk->area + chunk->data,
2456 chunk->size - chunk->data,
2457 &rule->rdr_fmt);
2458 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002459
Christopher Faulet99daf282018-11-28 22:58:13 +01002460 /* add path */
2461 if (!chunk_memcat(chunk, path.ptr, path.len))
2462 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002463
Christopher Faulet99daf282018-11-28 22:58:13 +01002464 /* append a slash at the end of the location if needed and missing */
2465 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
2466 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
2467 if (chunk->data + 1 >= chunk->size)
2468 goto fail;
2469 chunk->area[chunk->data++] = '/';
2470 }
2471 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002472 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002473 case REDIRECT_TYPE_LOCATION:
2474 default:
2475 if (rule->rdr_str) { /* this is an old "redirect" rule */
2476 /* add location */
2477 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2478 goto fail;
2479 }
2480 else {
2481 /* add location with executing log format */
Willy Tarreaubc1223b2021-09-02 16:54:33 +02002482 int len = build_logline(s, chunk->area + chunk->data,
2483 chunk->size - chunk->data,
2484 &rule->rdr_fmt);
2485 if (!len && rule->flags & REDIRECT_FLAG_IGNORE_EMPTY)
2486 return 2;
2487
2488 chunk->data += len;
Christopher Faulet99daf282018-11-28 22:58:13 +01002489 }
2490 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002491 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002492 location = ist2(chunk->area, chunk->data);
2493
2494 /*
2495 * Create the 30x response
2496 */
2497 switch (rule->code) {
2498 case 308:
2499 status = ist("308");
2500 reason = ist("Permanent Redirect");
2501 break;
2502 case 307:
2503 status = ist("307");
2504 reason = ist("Temporary Redirect");
2505 break;
2506 case 303:
2507 status = ist("303");
2508 reason = ist("See Other");
2509 break;
2510 case 301:
2511 status = ist("301");
2512 reason = ist("Moved Permanently");
2513 break;
2514 case 302:
2515 default:
2516 status = ist("302");
2517 reason = ist("Found");
2518 break;
2519 }
2520
Christopher Faulet08e66462019-05-23 16:44:59 +02002521 if (!(txn->req.flags & HTTP_MSGF_BODYLESS) && txn->req.msg_state != HTTP_MSG_DONE)
2522 close = 1;
2523
Christopher Faulet99daf282018-11-28 22:58:13 +01002524 htx = htx_from_buf(&res->buf);
Kevin Zhu96b36392020-01-07 09:42:55 +01002525 /* Trim any possible response */
2526 channel_htx_truncate(&s->res, htx);
Christopher Faulet99daf282018-11-28 22:58:13 +01002527 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
2528 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), status, reason);
2529 if (!sl)
2530 goto fail;
2531 sl->info.res.status = rule->code;
2532 s->txn->status = rule->code;
2533
Christopher Faulet08e66462019-05-23 16:44:59 +02002534 if (close && !htx_add_header(htx, ist("Connection"), ist("close")))
2535 goto fail;
2536
2537 if (!htx_add_header(htx, ist("Content-length"), ist("0")) ||
Christopher Faulet99daf282018-11-28 22:58:13 +01002538 !htx_add_header(htx, ist("Location"), location))
2539 goto fail;
2540
2541 if (rule->code == 302 || rule->code == 303 || rule->code == 307) {
2542 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")))
2543 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002544 }
2545
2546 if (rule->cookie_len) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002547 if (!htx_add_header(htx, ist("Set-Cookie"), ist2(rule->cookie_str, rule->cookie_len)))
2548 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002549 }
2550
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002551 if (!htx_add_endof(htx, HTX_BLK_EOH))
Christopher Faulet99daf282018-11-28 22:58:13 +01002552 goto fail;
2553
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002554 htx->flags |= HTX_FL_EOM;
Kevin Zhu96b36392020-01-07 09:42:55 +01002555 htx_to_buf(htx, &res->buf);
Christopher Fauleta72a7e42020-01-28 09:28:11 +01002556 if (!http_forward_proxy_resp(s, 1))
2557 goto fail;
Christopher Faulet99daf282018-11-28 22:58:13 +01002558
Christopher Faulet60b33a52020-01-28 09:18:10 +01002559 if (rule->flags & REDIRECT_FLAG_FROM_REQ) {
2560 /* let's log the request time */
2561 s->logs.tv_request = now;
Christopher Fauletd3475882021-10-04 14:16:46 +02002562 req->analysers &= AN_REQ_FLT_END;
Christopher Faulet99daf282018-11-28 22:58:13 +01002563
Christopher Faulet60b33a52020-01-28 09:18:10 +01002564 if (s->sess->fe == s->be) /* report it if the request was intercepted by the frontend */
Willy Tarreau4781b152021-04-06 13:53:36 +02002565 _HA_ATOMIC_INC(&s->sess->fe->fe_counters.intercepted_req);
Christopher Faulet60b33a52020-01-28 09:18:10 +01002566 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002567
2568 if (!(s->flags & SF_ERR_MASK))
2569 s->flags |= SF_ERR_LOCAL;
2570 if (!(s->flags & SF_FINST_MASK))
Christopher Faulet60b33a52020-01-28 09:18:10 +01002571 s->flags |= ((rule->flags & REDIRECT_FLAG_FROM_REQ) ? SF_FINST_R : SF_FINST_H);
Christopher Fauletf2824e62018-10-01 12:12:37 +02002572
Christopher Faulet99daf282018-11-28 22:58:13 +01002573 free_trash_chunk(chunk);
2574 return 1;
2575
2576 fail:
2577 /* If an error occurred, remove the incomplete HTTP response from the
2578 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01002579 channel_htx_truncate(res, htxbuf(&res->buf));
Christopher Fauletf2824e62018-10-01 12:12:37 +02002580 free_trash_chunk(chunk);
Christopher Faulet99daf282018-11-28 22:58:13 +01002581 return 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002582}
2583
Christopher Faulet92d34fe2019-12-17 09:20:34 +01002584/* Replace all headers matching the name <name>. The header value is replaced if
2585 * it matches the regex <re>. <str> is used for the replacement. If <full> is
2586 * set to 1, the full-line is matched and replaced. Otherwise, comma-separated
2587 * values are evaluated one by one. It returns 0 on success and -1 on error.
2588 */
2589int http_replace_hdrs(struct stream* s, struct htx *htx, struct ist name,
2590 const char *str, struct my_regex *re, int full)
Christopher Faulet72333522018-10-24 11:25:02 +02002591{
2592 struct http_hdr_ctx ctx;
2593 struct buffer *output = get_trash_chunk();
2594
Christopher Faulet72333522018-10-24 11:25:02 +02002595 ctx.blk = NULL;
Christopher Faulet92d34fe2019-12-17 09:20:34 +01002596 while (http_find_header(htx, name, &ctx, full)) {
Christopher Faulet72333522018-10-24 11:25:02 +02002597 if (!regex_exec_match2(re, ctx.value.ptr, ctx.value.len, MAX_MATCH, pmatch, 0))
2598 continue;
2599
2600 output->data = exp_replace(output->area, output->size, ctx.value.ptr, str, pmatch);
2601 if (output->data == -1)
2602 return -1;
2603 if (!http_replace_header_value(htx, &ctx, ist2(output->area, output->data)))
2604 return -1;
2605 }
2606 return 0;
2607}
2608
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002609/* This function executes one of the set-{method,path,query,uri} actions. It
2610 * takes the string from the variable 'replace' with length 'len', then modifies
2611 * the relevant part of the request line accordingly. Then it updates various
2612 * pointers to the next elements which were moved, and the total buffer length.
2613 * It finds the action to be performed in p[2], previously filled by function
2614 * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal
2615 * error, though this can be revisited when this code is finally exploited.
2616 *
2617 * 'action' can be '0' to replace method, '1' to replace path, '2' to replace
Christopher Faulet312294f2020-09-02 17:17:44 +02002618 * query string, 3 to replace uri or 4 to replace the path+query.
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002619 *
2620 * In query string case, the mark question '?' must be set at the start of the
2621 * string by the caller, event if the replacement query string is empty.
2622 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002623int http_req_replace_stline(int action, const char *replace, int len,
2624 struct proxy *px, struct stream *s)
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002625{
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002626 struct htx *htx = htxbuf(&s->req.buf);
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002627
2628 switch (action) {
2629 case 0: // method
2630 if (!http_replace_req_meth(htx, ist2(replace, len)))
2631 return -1;
2632 break;
2633
2634 case 1: // path
Christopher Fauletb8ce5052020-08-31 16:11:57 +02002635 if (!http_replace_req_path(htx, ist2(replace, len), 0))
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002636 return -1;
2637 break;
2638
2639 case 2: // query
2640 if (!http_replace_req_query(htx, ist2(replace, len)))
2641 return -1;
2642 break;
2643
2644 case 3: // uri
2645 if (!http_replace_req_uri(htx, ist2(replace, len)))
2646 return -1;
2647 break;
2648
Christopher Faulet312294f2020-09-02 17:17:44 +02002649 case 4: // path + query
2650 if (!http_replace_req_path(htx, ist2(replace, len), 1))
2651 return -1;
2652 break;
2653
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002654 default:
2655 return -1;
2656 }
2657 return 0;
2658}
2659
2660/* This function replace the HTTP status code and the associated message. The
Christopher Faulete00d06c2019-12-16 17:18:42 +01002661 * variable <status> contains the new status code. This function never fails. It
2662 * returns 0 in case of success, -1 in case of internal error.
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002663 */
Christopher Faulet96bff762019-12-17 13:46:18 +01002664int http_res_set_status(unsigned int status, struct ist reason, struct stream *s)
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002665{
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002666 struct htx *htx = htxbuf(&s->res.buf);
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002667 char *res;
2668
2669 chunk_reset(&trash);
2670 res = ultoa_o(status, trash.area, trash.size);
2671 trash.data = res - trash.area;
2672
2673 /* Do we have a custom reason format string? */
Tim Duesterhuse296d3e2020-03-05 17:56:31 +01002674 if (!isttest(reason)) {
Christopher Faulet96bff762019-12-17 13:46:18 +01002675 const char *str = http_get_reason(status);
Tim Duesterhusdcf753a2021-03-04 17:31:47 +01002676 reason = ist(str);
Christopher Faulet96bff762019-12-17 13:46:18 +01002677 }
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002678
Christopher Fauletbde2c4c2020-08-31 16:43:34 +02002679 if (!http_replace_res_status(htx, ist2(trash.area, trash.data), reason))
Christopher Faulete00d06c2019-12-16 17:18:42 +01002680 return -1;
2681 return 0;
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002682}
2683
Christopher Faulet3e964192018-10-24 11:39:23 +02002684/* Executes the http-request rules <rules> for stream <s>, proxy <px> and
2685 * transaction <txn>. Returns the verdict of the first rule that prevents
2686 * further processing of the request (auth, deny, ...), and defaults to
2687 * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or
2688 * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT
2689 * on txn->flags if it encounters a tarpit rule. If <deny_status> is not NULL
2690 * and a deny/tarpit rule is matched, it will be filled with this rule's deny
2691 * status.
2692 */
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002693static enum rule_result http_req_get_intercept_rule(struct proxy *px, struct list *def_rules,
2694 struct list *rules, struct stream *s)
Christopher Faulet3e964192018-10-24 11:39:23 +02002695{
2696 struct session *sess = strm_sess(s);
2697 struct http_txn *txn = s->txn;
Christopher Faulet3e964192018-10-24 11:39:23 +02002698 struct act_rule *rule;
Christopher Faulet3e964192018-10-24 11:39:23 +02002699 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002700 int act_opts = 0;
Christopher Faulet3e964192018-10-24 11:39:23 +02002701
Christopher Faulet3e964192018-10-24 11:39:23 +02002702 /* If "the current_rule_list" match the executed rule list, we are in
2703 * resume condition. If a resume is needed it is always in the action
2704 * and never in the ACL or converters. In this case, we initialise the
2705 * current rule, and go to the action execution point.
2706 */
2707 if (s->current_rule) {
2708 rule = s->current_rule;
2709 s->current_rule = NULL;
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002710 if (s->current_rule_list == rules || (def_rules && s->current_rule_list == def_rules))
Christopher Faulet3e964192018-10-24 11:39:23 +02002711 goto resume_execution;
2712 }
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002713 s->current_rule_list = ((!def_rules || s->current_rule_list == def_rules) ? rules : def_rules);
Christopher Faulet3e964192018-10-24 11:39:23 +02002714
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002715 restart:
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002716 /* start the ruleset evaluation in strict mode */
2717 txn->req.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01002718
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002719 list_for_each_entry(rule, s->current_rule_list, list) {
Christopher Faulet3e964192018-10-24 11:39:23 +02002720 /* check optional condition */
2721 if (rule->cond) {
2722 int ret;
2723
2724 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
2725 ret = acl_pass(ret);
2726
2727 if (rule->cond->pol == ACL_COND_UNLESS)
2728 ret = !ret;
2729
2730 if (!ret) /* condition not matched */
2731 continue;
2732 }
2733
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002734 act_opts |= ACT_OPT_FIRST;
Christopher Faulet3e964192018-10-24 11:39:23 +02002735 resume_execution:
Amaury Denoyelle03517732021-05-07 14:25:01 +02002736 if (rule->kw->flags & KWF_EXPERIMENTAL)
2737 mark_tainted(TAINTED_ACTION_EXP_EXECUTED);
2738
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002739 /* Always call the action function if defined */
2740 if (rule->action_ptr) {
2741 if ((s->req.flags & CF_READ_ERROR) ||
2742 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
2743 (px->options & PR_O_ABRT_CLOSE)))
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002744 act_opts |= ACT_OPT_FINAL;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002745
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002746 switch (rule->action_ptr(rule, px, sess, s, act_opts)) {
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002747 case ACT_RET_CONT:
2748 break;
2749 case ACT_RET_STOP:
2750 rule_ret = HTTP_RULE_RES_STOP;
2751 goto end;
2752 case ACT_RET_YIELD:
2753 s->current_rule = rule;
2754 rule_ret = HTTP_RULE_RES_YIELD;
2755 goto end;
2756 case ACT_RET_ERR:
2757 rule_ret = HTTP_RULE_RES_ERROR;
2758 goto end;
2759 case ACT_RET_DONE:
2760 rule_ret = HTTP_RULE_RES_DONE;
2761 goto end;
2762 case ACT_RET_DENY:
Christopher Fauletb58f62b2020-01-13 16:40:13 +01002763 if (txn->status == -1)
2764 txn->status = 403;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002765 rule_ret = HTTP_RULE_RES_DENY;
2766 goto end;
2767 case ACT_RET_ABRT:
2768 rule_ret = HTTP_RULE_RES_ABRT;
2769 goto end;
2770 case ACT_RET_INV:
2771 rule_ret = HTTP_RULE_RES_BADREQ;
2772 goto end;
2773 }
2774 continue; /* eval the next rule */
2775 }
2776
2777 /* If not action function defined, check for known actions */
Christopher Faulet3e964192018-10-24 11:39:23 +02002778 switch (rule->action) {
2779 case ACT_ACTION_ALLOW:
2780 rule_ret = HTTP_RULE_RES_STOP;
2781 goto end;
2782
2783 case ACT_ACTION_DENY:
Christopher Faulet5cb513a2020-05-13 17:56:56 +02002784 txn->status = rule->arg.http_reply->status;
2785 txn->http_reply = rule->arg.http_reply;
Christopher Faulet3e964192018-10-24 11:39:23 +02002786 rule_ret = HTTP_RULE_RES_DENY;
2787 goto end;
2788
2789 case ACT_HTTP_REQ_TARPIT:
2790 txn->flags |= TX_CLTARPIT;
Christopher Faulet5cb513a2020-05-13 17:56:56 +02002791 txn->status = rule->arg.http_reply->status;
2792 txn->http_reply = rule->arg.http_reply;
Christopher Faulet3e964192018-10-24 11:39:23 +02002793 rule_ret = HTTP_RULE_RES_DENY;
2794 goto end;
2795
Willy Tarreaubc1223b2021-09-02 16:54:33 +02002796 case ACT_HTTP_REDIR: {
2797 int ret = http_apply_redirect_rule(rule->arg.redir, s, txn);
2798
2799 if (ret == 2) // 2 == skip
2800 break;
2801
2802 rule_ret = ret ? HTTP_RULE_RES_ABRT : HTTP_RULE_RES_ERROR;
Christopher Faulet3e964192018-10-24 11:39:23 +02002803 goto end;
Willy Tarreaubc1223b2021-09-02 16:54:33 +02002804 }
Christopher Faulet3e964192018-10-24 11:39:23 +02002805
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002806 /* other flags exists, but normally, they never be matched. */
Christopher Faulet3e964192018-10-24 11:39:23 +02002807 default:
2808 break;
2809 }
2810 }
2811
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002812 if (def_rules && s->current_rule_list == def_rules) {
2813 s->current_rule_list = rules;
2814 goto restart;
2815 }
2816
Christopher Faulet3e964192018-10-24 11:39:23 +02002817 end:
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002818 /* if the ruleset evaluation is finished reset the strict mode */
Christopher Faulet46f95542019-12-20 10:07:22 +01002819 if (rule_ret != HTTP_RULE_RES_YIELD)
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002820 txn->req.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01002821
Christopher Faulet3e964192018-10-24 11:39:23 +02002822 /* we reached the end of the rules, nothing to report */
2823 return rule_ret;
2824}
2825
2826/* Executes the http-response rules <rules> for stream <s> and proxy <px>. It
2827 * returns one of 5 possible statuses: HTTP_RULE_RES_CONT, HTTP_RULE_RES_STOP,
2828 * HTTP_RULE_RES_DONE, HTTP_RULE_RES_YIELD, or HTTP_RULE_RES_BADREQ. If *CONT
2829 * is returned, the process can continue the evaluation of next rule list. If
2830 * *STOP or *DONE is returned, the process must stop the evaluation. If *BADREQ
2831 * is returned, it means the operation could not be processed and a server error
Christopher Fauleta53abad2020-05-13 08:12:22 +02002832 * must be returned. If *YIELD is returned, the caller must call again the
2833 * function with the same context.
Christopher Faulet3e964192018-10-24 11:39:23 +02002834 */
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002835static enum rule_result http_res_get_intercept_rule(struct proxy *px, struct list *def_rules,
2836 struct list *rules, struct stream *s)
Christopher Faulet3e964192018-10-24 11:39:23 +02002837{
2838 struct session *sess = strm_sess(s);
2839 struct http_txn *txn = s->txn;
Christopher Faulet3e964192018-10-24 11:39:23 +02002840 struct act_rule *rule;
Christopher Faulet3e964192018-10-24 11:39:23 +02002841 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002842 int act_opts = 0;
Christopher Faulet3e964192018-10-24 11:39:23 +02002843
Christopher Faulet3e964192018-10-24 11:39:23 +02002844 /* If "the current_rule_list" match the executed rule list, we are in
2845 * resume condition. If a resume is needed it is always in the action
2846 * and never in the ACL or converters. In this case, we initialise the
2847 * current rule, and go to the action execution point.
2848 */
2849 if (s->current_rule) {
2850 rule = s->current_rule;
2851 s->current_rule = NULL;
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002852 if (s->current_rule_list == rules || (def_rules && s->current_rule_list == def_rules))
Christopher Faulet3e964192018-10-24 11:39:23 +02002853 goto resume_execution;
2854 }
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002855 s->current_rule_list = ((!def_rules || s->current_rule_list == def_rules) ? rules : def_rules);
2856
2857 restart:
Christopher Faulet3e964192018-10-24 11:39:23 +02002858
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002859 /* start the ruleset evaluation in strict mode */
2860 txn->rsp.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01002861
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002862 list_for_each_entry(rule, s->current_rule_list, list) {
Christopher Faulet3e964192018-10-24 11:39:23 +02002863 /* check optional condition */
2864 if (rule->cond) {
2865 int ret;
2866
2867 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
2868 ret = acl_pass(ret);
2869
2870 if (rule->cond->pol == ACL_COND_UNLESS)
2871 ret = !ret;
2872
2873 if (!ret) /* condition not matched */
2874 continue;
2875 }
2876
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002877 act_opts |= ACT_OPT_FIRST;
Christopher Faulet3e964192018-10-24 11:39:23 +02002878resume_execution:
Amaury Denoyelle03517732021-05-07 14:25:01 +02002879 if (rule->kw->flags & KWF_EXPERIMENTAL)
2880 mark_tainted(TAINTED_ACTION_EXP_EXECUTED);
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002881
2882 /* Always call the action function if defined */
2883 if (rule->action_ptr) {
2884 if ((s->req.flags & CF_READ_ERROR) ||
2885 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
2886 (px->options & PR_O_ABRT_CLOSE)))
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002887 act_opts |= ACT_OPT_FINAL;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002888
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002889 switch (rule->action_ptr(rule, px, sess, s, act_opts)) {
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002890 case ACT_RET_CONT:
2891 break;
2892 case ACT_RET_STOP:
2893 rule_ret = HTTP_RULE_RES_STOP;
2894 goto end;
2895 case ACT_RET_YIELD:
2896 s->current_rule = rule;
2897 rule_ret = HTTP_RULE_RES_YIELD;
2898 goto end;
2899 case ACT_RET_ERR:
2900 rule_ret = HTTP_RULE_RES_ERROR;
2901 goto end;
2902 case ACT_RET_DONE:
2903 rule_ret = HTTP_RULE_RES_DONE;
2904 goto end;
2905 case ACT_RET_DENY:
Christopher Fauletb58f62b2020-01-13 16:40:13 +01002906 if (txn->status == -1)
2907 txn->status = 502;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002908 rule_ret = HTTP_RULE_RES_DENY;
2909 goto end;
2910 case ACT_RET_ABRT:
2911 rule_ret = HTTP_RULE_RES_ABRT;
2912 goto end;
2913 case ACT_RET_INV:
2914 rule_ret = HTTP_RULE_RES_BADREQ;
2915 goto end;
2916 }
2917 continue; /* eval the next rule */
2918 }
2919
2920 /* If not action function defined, check for known actions */
Christopher Faulet3e964192018-10-24 11:39:23 +02002921 switch (rule->action) {
2922 case ACT_ACTION_ALLOW:
2923 rule_ret = HTTP_RULE_RES_STOP; /* "allow" rules are OK */
2924 goto end;
2925
2926 case ACT_ACTION_DENY:
Christopher Faulet5cb513a2020-05-13 17:56:56 +02002927 txn->status = rule->arg.http_reply->status;
2928 txn->http_reply = rule->arg.http_reply;
Christopher Faulet3a26bee2019-12-16 12:47:40 +01002929 rule_ret = HTTP_RULE_RES_DENY;
Christopher Faulet3e964192018-10-24 11:39:23 +02002930 goto end;
2931
Willy Tarreaubc1223b2021-09-02 16:54:33 +02002932 case ACT_HTTP_REDIR: {
2933 int ret = http_apply_redirect_rule(rule->arg.redir, s, txn);
Christopher Faulet3e964192018-10-24 11:39:23 +02002934
Willy Tarreaubc1223b2021-09-02 16:54:33 +02002935 if (ret == 2) // 2 == skip
2936 break;
2937
2938 rule_ret = ret ? HTTP_RULE_RES_ABRT : HTTP_RULE_RES_ERROR;
2939 goto end;
2940 }
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002941 /* other flags exists, but normally, they never be matched. */
Christopher Faulet3e964192018-10-24 11:39:23 +02002942 default:
2943 break;
2944 }
2945 }
2946
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002947 if (def_rules && s->current_rule_list == def_rules) {
2948 s->current_rule_list = rules;
2949 goto restart;
2950 }
2951
Christopher Faulet3e964192018-10-24 11:39:23 +02002952 end:
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002953 /* if the ruleset evaluation is finished reset the strict mode */
Christopher Faulet46f95542019-12-20 10:07:22 +01002954 if (rule_ret != HTTP_RULE_RES_YIELD)
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002955 txn->rsp.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01002956
Christopher Faulet3e964192018-10-24 11:39:23 +02002957 /* we reached the end of the rules, nothing to report */
2958 return rule_ret;
2959}
2960
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002961/* Executes backend and frontend http-after-response rules for the stream <s>,
2962 * in that order. it return 1 on success and 0 on error. It is the caller
2963 * responsibility to catch error or ignore it. If it catches it, this function
2964 * may be called a second time, for the internal error.
2965 */
2966int http_eval_after_res_rules(struct stream *s)
2967{
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002968 struct list *def_rules, *rules;
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002969 struct session *sess = s->sess;
2970 enum rule_result ret = HTTP_RULE_RES_CONT;
2971
Christopher Faulet507479b2020-05-15 12:29:46 +02002972 /* Eval after-response ruleset only if the reply is not const */
2973 if (s->txn->flags & TX_CONST_REPLY)
2974 goto end;
2975
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002976 /* prune the request variables if not already done and swap to the response variables. */
2977 if (s->vars_reqres.scope != SCOPE_RES) {
2978 if (!LIST_ISEMPTY(&s->vars_reqres.head))
2979 vars_prune(&s->vars_reqres, s->sess, s);
Willy Tarreaub7bfcb32021-08-31 08:13:25 +02002980 vars_init_head(&s->vars_reqres, SCOPE_RES);
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002981 }
2982
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002983
2984 def_rules = (s->be->defpx ? &s->be->defpx->http_after_res_rules : NULL);
2985 rules = &s->be->http_after_res_rules;
2986
2987 ret = http_res_get_intercept_rule(s->be, def_rules, rules, s);
2988 if (ret == HTTP_RULE_RES_CONT && sess->fe != s->be) {
2989 def_rules = ((sess->fe->defpx && sess->fe->defpx != s->be->defpx) ? &sess->fe->defpx->http_after_res_rules : NULL);
2990 rules = &sess->fe->http_after_res_rules;
2991 ret = http_res_get_intercept_rule(sess->fe, def_rules, rules, s);
2992 }
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002993
Christopher Faulet507479b2020-05-15 12:29:46 +02002994 end:
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002995 /* All other codes than CONTINUE, STOP or DONE are forbidden */
2996 return (ret == HTTP_RULE_RES_CONT || ret == HTTP_RULE_RES_STOP || ret == HTTP_RULE_RES_DONE);
2997}
2998
Christopher Fauletfcda7c62018-10-24 11:56:22 +02002999/*
3000 * Manage client-side cookie. It can impact performance by about 2% so it is
3001 * desirable to call it only when needed. This code is quite complex because
3002 * of the multiple very crappy and ambiguous syntaxes we have to support. it
3003 * highly recommended not to touch this part without a good reason !
3004 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003005static void http_manage_client_side_cookies(struct stream *s, struct channel *req)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003006{
3007 struct session *sess = s->sess;
3008 struct http_txn *txn = s->txn;
3009 struct htx *htx;
3010 struct http_hdr_ctx ctx;
3011 char *hdr_beg, *hdr_end, *del_from;
3012 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
3013 int preserve_hdr;
3014
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003015 htx = htxbuf(&req->buf);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003016 ctx.blk = NULL;
3017 while (http_find_header(htx, ist("Cookie"), &ctx, 1)) {
Olivier Houchardf0f42382019-07-22 17:43:46 +02003018 int is_first = 1;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003019 del_from = NULL; /* nothing to be deleted */
3020 preserve_hdr = 0; /* assume we may kill the whole header */
3021
3022 /* Now look for cookies. Conforming to RFC2109, we have to support
3023 * attributes whose name begin with a '$', and associate them with
3024 * the right cookie, if we want to delete this cookie.
3025 * So there are 3 cases for each cookie read :
3026 * 1) it's a special attribute, beginning with a '$' : ignore it.
3027 * 2) it's a server id cookie that we *MAY* want to delete : save
3028 * some pointers on it (last semi-colon, beginning of cookie...)
3029 * 3) it's an application cookie : we *MAY* have to delete a previous
3030 * "special" cookie.
3031 * At the end of loop, if a "special" cookie remains, we may have to
3032 * remove it. If no application cookie persists in the header, we
3033 * *MUST* delete it.
3034 *
3035 * Note: RFC2965 is unclear about the processing of spaces around
3036 * the equal sign in the ATTR=VALUE form. A careful inspection of
3037 * the RFC explicitly allows spaces before it, and not within the
3038 * tokens (attrs or values). An inspection of RFC2109 allows that
3039 * too but section 10.1.3 lets one think that spaces may be allowed
3040 * after the equal sign too, resulting in some (rare) buggy
3041 * implementations trying to do that. So let's do what servers do.
3042 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
3043 * allowed quoted strings in values, with any possible character
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003044 * after a backslash, including control chars and delimiters, which
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003045 * causes parsing to become ambiguous. Browsers also allow spaces
3046 * within values even without quotes.
3047 *
3048 * We have to keep multiple pointers in order to support cookie
3049 * removal at the beginning, middle or end of header without
3050 * corrupting the header. All of these headers are valid :
3051 *
3052 * hdr_beg hdr_end
3053 * | |
3054 * v |
3055 * NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3 |
3056 * NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3 v
3057 * NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3
3058 * | | | | | | |
3059 * | | | | | | |
3060 * | | | | | | +--> next
3061 * | | | | | +----> val_end
3062 * | | | | +-----------> val_beg
3063 * | | | +--------------> equal
3064 * | | +----------------> att_end
3065 * | +---------------------> att_beg
3066 * +--------------------------> prev
3067 *
3068 */
3069 hdr_beg = ctx.value.ptr;
3070 hdr_end = hdr_beg + ctx.value.len;
3071 for (prev = hdr_beg; prev < hdr_end; prev = next) {
3072 /* Iterate through all cookies on this line */
3073
3074 /* find att_beg */
3075 att_beg = prev;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003076 if (!is_first)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003077 att_beg++;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003078 is_first = 0;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003079
3080 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
3081 att_beg++;
3082
3083 /* find att_end : this is the first character after the last non
3084 * space before the equal. It may be equal to hdr_end.
3085 */
3086 equal = att_end = att_beg;
3087 while (equal < hdr_end) {
3088 if (*equal == '=' || *equal == ',' || *equal == ';')
3089 break;
3090 if (HTTP_IS_SPHT(*equal++))
3091 continue;
3092 att_end = equal;
3093 }
3094
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003095 /* here, <equal> points to '=', a delimiter or the end. <att_end>
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003096 * is between <att_beg> and <equal>, both may be identical.
3097 */
3098 /* look for end of cookie if there is an equal sign */
3099 if (equal < hdr_end && *equal == '=') {
3100 /* look for the beginning of the value */
3101 val_beg = equal + 1;
3102 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
3103 val_beg++;
3104
3105 /* find the end of the value, respecting quotes */
3106 next = http_find_cookie_value_end(val_beg, hdr_end);
3107
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003108 /* make val_end point to the first white space or delimiter after the value */
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003109 val_end = next;
3110 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
3111 val_end--;
3112 }
3113 else
3114 val_beg = val_end = next = equal;
3115
3116 /* We have nothing to do with attributes beginning with
3117 * '$'. However, they will automatically be removed if a
3118 * header before them is removed, since they're supposed
3119 * to be linked together.
3120 */
3121 if (*att_beg == '$')
3122 continue;
3123
3124 /* Ignore cookies with no equal sign */
3125 if (equal == next) {
3126 /* This is not our cookie, so we must preserve it. But if we already
3127 * scheduled another cookie for removal, we cannot remove the
3128 * complete header, but we can remove the previous block itself.
3129 */
3130 preserve_hdr = 1;
3131 if (del_from != NULL) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003132 int delta = http_del_hdr_value(hdr_beg, hdr_end, &del_from, prev);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003133 val_end += delta;
3134 next += delta;
3135 hdr_end += delta;
3136 prev = del_from;
3137 del_from = NULL;
3138 }
3139 continue;
3140 }
3141
3142 /* if there are spaces around the equal sign, we need to
3143 * strip them otherwise we'll get trouble for cookie captures,
3144 * or even for rewrites. Since this happens extremely rarely,
3145 * it does not hurt performance.
3146 */
3147 if (unlikely(att_end != equal || val_beg > equal + 1)) {
3148 int stripped_before = 0;
3149 int stripped_after = 0;
3150
3151 if (att_end != equal) {
3152 memmove(att_end, equal, hdr_end - equal);
3153 stripped_before = (att_end - equal);
3154 equal += stripped_before;
3155 val_beg += stripped_before;
3156 }
3157
3158 if (val_beg > equal + 1) {
3159 memmove(equal + 1, val_beg, hdr_end + stripped_before - val_beg);
3160 stripped_after = (equal + 1) - val_beg;
3161 val_beg += stripped_after;
3162 stripped_before += stripped_after;
3163 }
3164
3165 val_end += stripped_before;
3166 next += stripped_before;
3167 hdr_end += stripped_before;
3168 }
3169 /* now everything is as on the diagram above */
3170
3171 /* First, let's see if we want to capture this cookie. We check
3172 * that we don't already have a client side cookie, because we
3173 * can only capture one. Also as an optimisation, we ignore
3174 * cookies shorter than the declared name.
3175 */
3176 if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL &&
3177 (val_end - att_beg >= sess->fe->capture_namelen) &&
3178 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
3179 int log_len = val_end - att_beg;
3180
3181 if ((txn->cli_cookie = pool_alloc(pool_head_capture)) == NULL) {
3182 ha_alert("HTTP logging : out of memory.\n");
3183 } else {
3184 if (log_len > sess->fe->capture_len)
3185 log_len = sess->fe->capture_len;
3186 memcpy(txn->cli_cookie, att_beg, log_len);
3187 txn->cli_cookie[log_len] = 0;
3188 }
3189 }
3190
3191 /* Persistence cookies in passive, rewrite or insert mode have the
3192 * following form :
3193 *
3194 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
3195 *
3196 * For cookies in prefix mode, the form is :
3197 *
3198 * Cookie: NAME=SRV~VALUE
3199 */
3200 if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
3201 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
3202 struct server *srv = s->be->srv;
3203 char *delim;
3204
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003205 /* if we're in cookie prefix mode, we'll search the delimiter so that we
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003206 * have the server ID between val_beg and delim, and the original cookie between
3207 * delim+1 and val_end. Otherwise, delim==val_end :
3208 *
3209 * hdr_beg
3210 * |
3211 * v
3212 * NAME=SRV; # in all but prefix modes
3213 * NAME=SRV~OPAQUE ; # in prefix mode
3214 * || || | |+-> next
3215 * || || | +--> val_end
3216 * || || +---------> delim
3217 * || |+------------> val_beg
3218 * || +-------------> att_end = equal
3219 * |+-----------------> att_beg
3220 * +------------------> prev
3221 *
3222 */
3223 if (s->be->ck_opts & PR_CK_PFX) {
3224 for (delim = val_beg; delim < val_end; delim++)
3225 if (*delim == COOKIE_DELIM)
3226 break;
3227 }
3228 else {
3229 char *vbar1;
3230 delim = val_end;
3231 /* Now check if the cookie contains a date field, which would
3232 * appear after a vertical bar ('|') just after the server name
3233 * and before the delimiter.
3234 */
3235 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
3236 if (vbar1) {
3237 /* OK, so left of the bar is the server's cookie and
3238 * right is the last seen date. It is a base64 encoded
3239 * 30-bit value representing the UNIX date since the
3240 * epoch in 4-second quantities.
3241 */
3242 int val;
3243 delim = vbar1++;
3244 if (val_end - vbar1 >= 5) {
3245 val = b64tos30(vbar1);
3246 if (val > 0)
3247 txn->cookie_last_date = val << 2;
3248 }
3249 /* look for a second vertical bar */
3250 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
3251 if (vbar1 && (val_end - vbar1 > 5)) {
3252 val = b64tos30(vbar1 + 1);
3253 if (val > 0)
3254 txn->cookie_first_date = val << 2;
3255 }
3256 }
3257 }
3258
3259 /* if the cookie has an expiration date and the proxy wants to check
3260 * it, then we do that now. We first check if the cookie is too old,
3261 * then only if it has expired. We detect strict overflow because the
3262 * time resolution here is not great (4 seconds). Cookies with dates
3263 * in the future are ignored if their offset is beyond one day. This
3264 * allows an admin to fix timezone issues without expiring everyone
3265 * and at the same time avoids keeping unwanted side effects for too
3266 * long.
3267 */
3268 if (txn->cookie_first_date && s->be->cookie_maxlife &&
3269 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) ||
3270 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
3271 txn->flags &= ~TX_CK_MASK;
3272 txn->flags |= TX_CK_OLD;
3273 delim = val_beg; // let's pretend we have not found the cookie
3274 txn->cookie_first_date = 0;
3275 txn->cookie_last_date = 0;
3276 }
3277 else if (txn->cookie_last_date && s->be->cookie_maxidle &&
3278 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) ||
3279 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
3280 txn->flags &= ~TX_CK_MASK;
3281 txn->flags |= TX_CK_EXPIRED;
3282 delim = val_beg; // let's pretend we have not found the cookie
3283 txn->cookie_first_date = 0;
3284 txn->cookie_last_date = 0;
3285 }
3286
3287 /* Here, we'll look for the first running server which supports the cookie.
3288 * This allows to share a same cookie between several servers, for example
3289 * to dedicate backup servers to specific servers only.
3290 * However, to prevent clients from sticking to cookie-less backup server
3291 * when they have incidentely learned an empty cookie, we simply ignore
3292 * empty cookies and mark them as invalid.
3293 * The same behaviour is applied when persistence must be ignored.
3294 */
3295 if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
3296 srv = NULL;
3297
3298 while (srv) {
3299 if (srv->cookie && (srv->cklen == delim - val_beg) &&
3300 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
3301 if ((srv->cur_state != SRV_ST_STOPPED) ||
3302 (s->be->options & PR_O_PERSIST) ||
3303 (s->flags & SF_FORCE_PRST)) {
3304 /* we found the server and we can use it */
3305 txn->flags &= ~TX_CK_MASK;
3306 txn->flags |= (srv->cur_state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
3307 s->flags |= SF_DIRECT | SF_ASSIGNED;
3308 s->target = &srv->obj_type;
3309 break;
3310 } else {
3311 /* we found a server, but it's down,
3312 * mark it as such and go on in case
3313 * another one is available.
3314 */
3315 txn->flags &= ~TX_CK_MASK;
3316 txn->flags |= TX_CK_DOWN;
3317 }
3318 }
3319 srv = srv->next;
3320 }
3321
3322 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
3323 /* no server matched this cookie or we deliberately skipped it */
3324 txn->flags &= ~TX_CK_MASK;
3325 if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
3326 txn->flags |= TX_CK_UNUSED;
3327 else
3328 txn->flags |= TX_CK_INVALID;
3329 }
3330
3331 /* depending on the cookie mode, we may have to either :
3332 * - delete the complete cookie if we're in insert+indirect mode, so that
3333 * the server never sees it ;
3334 * - remove the server id from the cookie value, and tag the cookie as an
Joseph Herlante9d5c722018-11-25 11:00:25 -08003335 * application cookie so that it does not get accidentally removed later,
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003336 * if we're in cookie prefix mode
3337 */
3338 if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
3339 int delta; /* negative */
3340
3341 memmove(val_beg, delim + 1, hdr_end - (delim + 1));
3342 delta = val_beg - (delim + 1);
3343 val_end += delta;
3344 next += delta;
3345 hdr_end += delta;
3346 del_from = NULL;
3347 preserve_hdr = 1; /* we want to keep this cookie */
3348 }
3349 else if (del_from == NULL &&
3350 (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
3351 del_from = prev;
3352 }
3353 }
3354 else {
3355 /* This is not our cookie, so we must preserve it. But if we already
3356 * scheduled another cookie for removal, we cannot remove the
3357 * complete header, but we can remove the previous block itself.
3358 */
3359 preserve_hdr = 1;
3360
3361 if (del_from != NULL) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003362 int delta = http_del_hdr_value(hdr_beg, hdr_end, &del_from, prev);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003363 if (att_beg >= del_from)
3364 att_beg += delta;
3365 if (att_end >= del_from)
3366 att_end += delta;
3367 val_beg += delta;
3368 val_end += delta;
3369 next += delta;
3370 hdr_end += delta;
3371 prev = del_from;
3372 del_from = NULL;
3373 }
3374 }
3375
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003376 } /* for each cookie */
3377
3378
3379 /* There are no more cookies on this line.
3380 * We may still have one (or several) marked for deletion at the
3381 * end of the line. We must do this now in two ways :
3382 * - if some cookies must be preserved, we only delete from the
3383 * mark to the end of line ;
3384 * - if nothing needs to be preserved, simply delete the whole header
3385 */
3386 if (del_from) {
3387 hdr_end = (preserve_hdr ? del_from : hdr_beg);
3388 }
3389 if ((hdr_end - hdr_beg) != ctx.value.len) {
Christopher Faulet3e2638e2019-06-18 09:49:16 +02003390 if (hdr_beg != hdr_end)
3391 htx_change_blk_value_len(htx, ctx.blk, hdr_end - hdr_beg);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003392 else
3393 http_remove_header(htx, &ctx);
3394 }
3395 } /* for each "Cookie header */
3396}
3397
3398/*
3399 * Manage server-side cookies. It can impact performance by about 2% so it is
3400 * desirable to call it only when needed. This function is also used when we
3401 * just need to know if there is a cookie (eg: for check-cache).
3402 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003403static void http_manage_server_side_cookies(struct stream *s, struct channel *res)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003404{
3405 struct session *sess = s->sess;
3406 struct http_txn *txn = s->txn;
3407 struct htx *htx;
3408 struct http_hdr_ctx ctx;
3409 struct server *srv;
3410 char *hdr_beg, *hdr_end;
3411 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau6f7a02a2019-04-15 21:49:49 +02003412 int is_cookie2 = 0;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003413
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003414 htx = htxbuf(&res->buf);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003415
3416 ctx.blk = NULL;
3417 while (1) {
Olivier Houchardf0f42382019-07-22 17:43:46 +02003418 int is_first = 1;
3419
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003420 if (!http_find_header(htx, ist("Set-Cookie"), &ctx, 1)) {
3421 if (!http_find_header(htx, ist("Set-Cookie2"), &ctx, 1))
3422 break;
3423 is_cookie2 = 1;
3424 }
3425
3426 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
3427 * <prev> points to the colon.
3428 */
3429 txn->flags |= TX_SCK_PRESENT;
3430
3431 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
3432 * check-cache is enabled) and we are not interested in checking
3433 * them. Warning, the cookie capture is declared in the frontend.
3434 */
3435 if (s->be->cookie_name == NULL && sess->fe->capture_name == NULL)
3436 break;
3437
3438 /* OK so now we know we have to process this response cookie.
3439 * The format of the Set-Cookie header is slightly different
3440 * from the format of the Cookie header in that it does not
3441 * support the comma as a cookie delimiter (thus the header
3442 * cannot be folded) because the Expires attribute described in
3443 * the original Netscape's spec may contain an unquoted date
3444 * with a comma inside. We have to live with this because
3445 * many browsers don't support Max-Age and some browsers don't
3446 * support quoted strings. However the Set-Cookie2 header is
3447 * clean.
3448 *
3449 * We have to keep multiple pointers in order to support cookie
3450 * removal at the beginning, middle or end of header without
3451 * corrupting the header (in case of set-cookie2). A special
3452 * pointer, <scav> points to the beginning of the set-cookie-av
3453 * fields after the first semi-colon. The <next> pointer points
3454 * either to the end of line (set-cookie) or next unquoted comma
3455 * (set-cookie2). All of these headers are valid :
3456 *
3457 * hdr_beg hdr_end
3458 * | |
3459 * v |
3460 * NAME1 = VALUE 1 ; Secure; Path="/" |
3461 * NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT v
3462 * NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT
3463 * NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard
3464 * | | | | | | | |
3465 * | | | | | | | +-> next
3466 * | | | | | | +------------> scav
3467 * | | | | | +--------------> val_end
3468 * | | | | +--------------------> val_beg
3469 * | | | +----------------------> equal
3470 * | | +------------------------> att_end
3471 * | +----------------------------> att_beg
3472 * +------------------------------> prev
3473 * -------------------------------> hdr_beg
3474 */
3475 hdr_beg = ctx.value.ptr;
3476 hdr_end = hdr_beg + ctx.value.len;
3477 for (prev = hdr_beg; prev < hdr_end; prev = next) {
3478
3479 /* Iterate through all cookies on this line */
3480
3481 /* find att_beg */
3482 att_beg = prev;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003483 if (!is_first)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003484 att_beg++;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003485 is_first = 0;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003486
3487 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
3488 att_beg++;
3489
3490 /* find att_end : this is the first character after the last non
3491 * space before the equal. It may be equal to hdr_end.
3492 */
3493 equal = att_end = att_beg;
3494
3495 while (equal < hdr_end) {
3496 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
3497 break;
3498 if (HTTP_IS_SPHT(*equal++))
3499 continue;
3500 att_end = equal;
3501 }
3502
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003503 /* here, <equal> points to '=', a delimiter or the end. <att_end>
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003504 * is between <att_beg> and <equal>, both may be identical.
3505 */
3506
3507 /* look for end of cookie if there is an equal sign */
3508 if (equal < hdr_end && *equal == '=') {
3509 /* look for the beginning of the value */
3510 val_beg = equal + 1;
3511 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
3512 val_beg++;
3513
3514 /* find the end of the value, respecting quotes */
3515 next = http_find_cookie_value_end(val_beg, hdr_end);
3516
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003517 /* make val_end point to the first white space or delimiter after the value */
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003518 val_end = next;
3519 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
3520 val_end--;
3521 }
3522 else {
3523 /* <equal> points to next comma, semi-colon or EOL */
3524 val_beg = val_end = next = equal;
3525 }
3526
3527 if (next < hdr_end) {
3528 /* Set-Cookie2 supports multiple cookies, and <next> points to
3529 * a colon or semi-colon before the end. So skip all attr-value
3530 * pairs and look for the next comma. For Set-Cookie, since
3531 * commas are permitted in values, skip to the end.
3532 */
3533 if (is_cookie2)
3534 next = http_find_hdr_value_end(next, hdr_end);
3535 else
3536 next = hdr_end;
3537 }
3538
3539 /* Now everything is as on the diagram above */
3540
3541 /* Ignore cookies with no equal sign */
3542 if (equal == val_end)
3543 continue;
3544
3545 /* If there are spaces around the equal sign, we need to
3546 * strip them otherwise we'll get trouble for cookie captures,
3547 * or even for rewrites. Since this happens extremely rarely,
3548 * it does not hurt performance.
3549 */
3550 if (unlikely(att_end != equal || val_beg > equal + 1)) {
3551 int stripped_before = 0;
3552 int stripped_after = 0;
3553
3554 if (att_end != equal) {
3555 memmove(att_end, equal, hdr_end - equal);
3556 stripped_before = (att_end - equal);
3557 equal += stripped_before;
3558 val_beg += stripped_before;
3559 }
3560
3561 if (val_beg > equal + 1) {
3562 memmove(equal + 1, val_beg, hdr_end + stripped_before - val_beg);
3563 stripped_after = (equal + 1) - val_beg;
3564 val_beg += stripped_after;
3565 stripped_before += stripped_after;
3566 }
3567
3568 val_end += stripped_before;
3569 next += stripped_before;
3570 hdr_end += stripped_before;
3571
Christopher Faulet3e2638e2019-06-18 09:49:16 +02003572 htx_change_blk_value_len(htx, ctx.blk, hdr_end - hdr_beg);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003573 ctx.value.len = hdr_end - hdr_beg;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003574 }
3575
3576 /* First, let's see if we want to capture this cookie. We check
3577 * that we don't already have a server side cookie, because we
3578 * can only capture one. Also as an optimisation, we ignore
3579 * cookies shorter than the declared name.
3580 */
3581 if (sess->fe->capture_name != NULL &&
3582 txn->srv_cookie == NULL &&
3583 (val_end - att_beg >= sess->fe->capture_namelen) &&
3584 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
3585 int log_len = val_end - att_beg;
3586 if ((txn->srv_cookie = pool_alloc(pool_head_capture)) == NULL) {
3587 ha_alert("HTTP logging : out of memory.\n");
3588 }
3589 else {
3590 if (log_len > sess->fe->capture_len)
3591 log_len = sess->fe->capture_len;
3592 memcpy(txn->srv_cookie, att_beg, log_len);
3593 txn->srv_cookie[log_len] = 0;
3594 }
3595 }
3596
3597 srv = objt_server(s->target);
3598 /* now check if we need to process it for persistence */
3599 if (!(s->flags & SF_IGNORE_PRST) &&
3600 (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
3601 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
3602 /* assume passive cookie by default */
3603 txn->flags &= ~TX_SCK_MASK;
3604 txn->flags |= TX_SCK_FOUND;
3605
3606 /* If the cookie is in insert mode on a known server, we'll delete
3607 * this occurrence because we'll insert another one later.
3608 * We'll delete it too if the "indirect" option is set and we're in
3609 * a direct access.
3610 */
3611 if (s->be->ck_opts & PR_CK_PSV) {
3612 /* The "preserve" flag was set, we don't want to touch the
3613 * server's cookie.
3614 */
3615 }
3616 else if ((srv && (s->be->ck_opts & PR_CK_INS)) ||
3617 ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) {
3618 /* this cookie must be deleted */
3619 if (prev == hdr_beg && next == hdr_end) {
3620 /* whole header */
3621 http_remove_header(htx, &ctx);
3622 /* note: while both invalid now, <next> and <hdr_end>
3623 * are still equal, so the for() will stop as expected.
3624 */
3625 } else {
3626 /* just remove the value */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003627 int delta = http_del_hdr_value(hdr_beg, hdr_end, &prev, next);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003628 next = prev;
3629 hdr_end += delta;
3630 }
3631 txn->flags &= ~TX_SCK_MASK;
3632 txn->flags |= TX_SCK_DELETED;
3633 /* and go on with next cookie */
3634 }
3635 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) {
3636 /* replace bytes val_beg->val_end with the cookie name associated
3637 * with this server since we know it.
3638 */
3639 int sliding, delta;
3640
3641 ctx.value = ist2(val_beg, val_end - val_beg);
3642 ctx.lws_before = ctx.lws_after = 0;
3643 http_replace_header_value(htx, &ctx, ist2(srv->cookie, srv->cklen));
3644 delta = srv->cklen - (val_end - val_beg);
3645 sliding = (ctx.value.ptr - val_beg);
3646 hdr_beg += sliding;
3647 val_beg += sliding;
3648 next += sliding + delta;
3649 hdr_end += sliding + delta;
3650
3651 txn->flags &= ~TX_SCK_MASK;
3652 txn->flags |= TX_SCK_REPLACED;
3653 }
3654 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) {
3655 /* insert the cookie name associated with this server
3656 * before existing cookie, and insert a delimiter between them..
3657 */
3658 int sliding, delta;
3659 ctx.value = ist2(val_beg, 0);
3660 ctx.lws_before = ctx.lws_after = 0;
3661 http_replace_header_value(htx, &ctx, ist2(srv->cookie, srv->cklen + 1));
3662 delta = srv->cklen + 1;
3663 sliding = (ctx.value.ptr - val_beg);
3664 hdr_beg += sliding;
3665 val_beg += sliding;
3666 next += sliding + delta;
3667 hdr_end += sliding + delta;
3668
3669 val_beg[srv->cklen] = COOKIE_DELIM;
3670 txn->flags &= ~TX_SCK_MASK;
3671 txn->flags |= TX_SCK_REPLACED;
3672 }
3673 }
3674 /* that's done for this cookie, check the next one on the same
3675 * line when next != hdr_end (only if is_cookie2).
3676 */
3677 }
3678 }
3679}
3680
Christopher Faulet25a02f62018-10-24 12:00:25 +02003681/*
3682 * Parses the Cache-Control and Pragma request header fields to determine if
3683 * the request may be served from the cache and/or if it is cacheable. Updates
3684 * s->txn->flags.
3685 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003686void http_check_request_for_cacheability(struct stream *s, struct channel *req)
Christopher Faulet25a02f62018-10-24 12:00:25 +02003687{
3688 struct http_txn *txn = s->txn;
3689 struct htx *htx;
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003690 struct http_hdr_ctx ctx = { .blk = NULL };
3691 int pragma_found, cc_found;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003692
3693 if ((txn->flags & (TX_CACHEABLE|TX_CACHE_IGNORE)) == TX_CACHE_IGNORE)
3694 return; /* nothing more to do here */
3695
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003696 htx = htxbuf(&req->buf);
Christopher Faulet25a02f62018-10-24 12:00:25 +02003697 pragma_found = cc_found = 0;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003698
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003699 /* Check "pragma" header for HTTP/1.0 compatibility. */
3700 if (http_find_header(htx, ist("pragma"), &ctx, 1)) {
3701 if (isteqi(ctx.value, ist("no-cache"))) {
3702 pragma_found = 1;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003703 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003704 }
Christopher Faulet25a02f62018-10-24 12:00:25 +02003705
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003706 ctx.blk = NULL;
3707 /* Don't use the cache and don't try to store if we found the
3708 * Authorization header */
3709 if (http_find_header(htx, ist("authorization"), &ctx, 1)) {
3710 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3711 txn->flags |= TX_CACHE_IGNORE;
3712 }
Christopher Faulet25a02f62018-10-24 12:00:25 +02003713
Christopher Faulet25a02f62018-10-24 12:00:25 +02003714
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003715 /* Look for "cache-control" header and iterate over all the values
3716 * until we find one that specifies that caching is possible or not. */
3717 ctx.blk = NULL;
3718 while (http_find_header(htx, ist("cache-control"), &ctx, 0)) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003719 cc_found = 1;
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003720 /* We don't check the values after max-age, max-stale nor min-fresh,
3721 * we simply don't use the cache when they're specified. */
3722 if (istmatchi(ctx.value, ist("max-age")) ||
3723 istmatchi(ctx.value, ist("no-cache")) ||
3724 istmatchi(ctx.value, ist("max-stale")) ||
3725 istmatchi(ctx.value, ist("min-fresh"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003726 txn->flags |= TX_CACHE_IGNORE;
3727 continue;
3728 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003729 if (istmatchi(ctx.value, ist("no-store"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003730 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3731 continue;
3732 }
3733 }
3734
3735 /* RFC7234#5.4:
3736 * When the Cache-Control header field is also present and
3737 * understood in a request, Pragma is ignored.
3738 * When the Cache-Control header field is not present in a
3739 * request, caches MUST consider the no-cache request
3740 * pragma-directive as having the same effect as if
3741 * "Cache-Control: no-cache" were present.
3742 */
3743 if (!cc_found && pragma_found)
3744 txn->flags |= TX_CACHE_IGNORE;
3745}
3746
3747/*
3748 * Check if response is cacheable or not. Updates s->txn->flags.
3749 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003750void http_check_response_for_cacheability(struct stream *s, struct channel *res)
Christopher Faulet25a02f62018-10-24 12:00:25 +02003751{
3752 struct http_txn *txn = s->txn;
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003753 struct http_hdr_ctx ctx = { .blk = NULL };
Christopher Faulet25a02f62018-10-24 12:00:25 +02003754 struct htx *htx;
Remi Tricot-Le Bretoncc9bf2e2020-11-12 11:14:41 +01003755 int has_freshness_info = 0;
3756 int has_validator = 0;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003757
3758 if (txn->status < 200) {
3759 /* do not try to cache interim responses! */
3760 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3761 return;
3762 }
3763
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003764 htx = htxbuf(&res->buf);
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003765 /* Check "pragma" header for HTTP/1.0 compatibility. */
3766 if (http_find_header(htx, ist("pragma"), &ctx, 1)) {
3767 if (isteqi(ctx.value, ist("no-cache"))) {
3768 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3769 return;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003770 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003771 }
Christopher Faulet25a02f62018-10-24 12:00:25 +02003772
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003773 /* Look for "cache-control" header and iterate over all the values
3774 * until we find one that specifies that caching is possible or not. */
3775 ctx.blk = NULL;
3776 while (http_find_header(htx, ist("cache-control"), &ctx, 0)) {
3777 if (isteqi(ctx.value, ist("public"))) {
3778 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003779 continue;
3780 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003781 if (isteqi(ctx.value, ist("private")) ||
3782 isteqi(ctx.value, ist("no-cache")) ||
3783 isteqi(ctx.value, ist("no-store")) ||
3784 isteqi(ctx.value, ist("max-age=0")) ||
3785 isteqi(ctx.value, ist("s-maxage=0"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003786 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003787 continue;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003788 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003789 /* We might have a no-cache="set-cookie" form. */
3790 if (istmatchi(ctx.value, ist("no-cache=\"set-cookie"))) {
3791 txn->flags &= ~TX_CACHE_COOK;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003792 continue;
3793 }
Remi Tricot-Le Bretoncc9bf2e2020-11-12 11:14:41 +01003794
3795 if (istmatchi(ctx.value, ist("s-maxage")) ||
3796 istmatchi(ctx.value, ist("max-age"))) {
3797 has_freshness_info = 1;
3798 continue;
3799 }
3800 }
3801
3802 /* If no freshness information could be found in Cache-Control values,
3803 * look for an Expires header. */
3804 if (!has_freshness_info) {
3805 ctx.blk = NULL;
3806 has_freshness_info = http_find_header(htx, ist("expires"), &ctx, 0);
Christopher Faulet25a02f62018-10-24 12:00:25 +02003807 }
Remi Tricot-Le Bretoncc9bf2e2020-11-12 11:14:41 +01003808
3809 /* If no freshness information could be found in Cache-Control or Expires
3810 * values, look for an explicit validator. */
3811 if (!has_freshness_info) {
3812 ctx.blk = NULL;
3813 has_validator = 1;
3814 if (!http_find_header(htx, ist("etag"), &ctx, 0)) {
3815 ctx.blk = NULL;
3816 if (!http_find_header(htx, ist("last-modified"), &ctx, 0))
3817 has_validator = 0;
3818 }
3819 }
3820
3821 /* We won't store an entry that has neither a cache validator nor an
3822 * explicit expiration time, as suggested in RFC 7234#3. */
3823 if (!has_freshness_info && !has_validator)
3824 txn->flags |= TX_CACHE_IGNORE;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003825}
3826
Christopher Faulet377c5a52018-10-24 21:21:30 +02003827/*
3828 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
3829 * for the current backend.
3830 *
3831 * It is assumed that the request is either a HEAD, GET, or POST and that the
3832 * uri_auth field is valid.
3833 *
3834 * Returns 1 if stats should be provided, otherwise 0.
3835 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003836static int http_stats_check_uri(struct stream *s, struct http_txn *txn, struct proxy *backend)
Christopher Faulet377c5a52018-10-24 21:21:30 +02003837{
3838 struct uri_auth *uri_auth = backend->uri_auth;
3839 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003840 struct htx_sl *sl;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003841 struct ist uri;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003842
3843 if (!uri_auth)
3844 return 0;
3845
3846 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
3847 return 0;
3848
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003849 htx = htxbuf(&s->req.buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02003850 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003851 uri = htx_sl_req_uri(sl);
Amaury Denoyellec453f952021-07-06 11:40:12 +02003852 if (*uri_auth->uri_prefix == '/') {
3853 struct http_uri_parser parser = http_uri_parser_init(uri);
3854 uri = http_parse_path(&parser);
3855 }
Christopher Faulet377c5a52018-10-24 21:21:30 +02003856
3857 /* check URI size */
3858 if (uri_auth->uri_len > uri.len)
3859 return 0;
3860
3861 if (memcmp(uri.ptr, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
3862 return 0;
3863
3864 return 1;
3865}
3866
3867/* This function prepares an applet to handle the stats. It can deal with the
3868 * "100-continue" expectation, check that admin rules are met for POST requests,
3869 * and program a response message if something was unexpected. It cannot fail
3870 * and always relies on the stats applet to complete the job. It does not touch
3871 * analysers nor counters, which are left to the caller. It does not touch
3872 * s->target which is supposed to already point to the stats applet. The caller
3873 * is expected to have already assigned an appctx to the stream.
3874 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003875static int http_handle_stats(struct stream *s, struct channel *req)
Christopher Faulet377c5a52018-10-24 21:21:30 +02003876{
3877 struct stats_admin_rule *stats_admin_rule;
3878 struct stream_interface *si = &s->si[1];
3879 struct session *sess = s->sess;
3880 struct http_txn *txn = s->txn;
3881 struct http_msg *msg = &txn->req;
3882 struct uri_auth *uri_auth = s->be->uri_auth;
3883 const char *h, *lookup, *end;
3884 struct appctx *appctx;
3885 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003886 struct htx_sl *sl;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003887
3888 appctx = si_appctx(si);
3889 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
3890 appctx->st1 = appctx->st2 = 0;
3891 appctx->ctx.stats.st_code = STAT_STATUS_INIT;
Willy Tarreau676c29e2019-10-09 10:50:01 +02003892 appctx->ctx.stats.flags |= uri_auth->flags;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003893 appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */
3894 if ((msg->flags & HTTP_MSGF_VER_11) && (txn->meth != HTTP_METH_HEAD))
3895 appctx->ctx.stats.flags |= STAT_CHUNKED;
3896
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003897 htx = htxbuf(&req->buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02003898 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003899 lookup = HTX_SL_REQ_UPTR(sl) + uri_auth->uri_len;
3900 end = HTX_SL_REQ_UPTR(sl) + HTX_SL_REQ_ULEN(sl);
Christopher Faulet377c5a52018-10-24 21:21:30 +02003901
3902 for (h = lookup; h <= end - 3; h++) {
3903 if (memcmp(h, ";up", 3) == 0) {
3904 appctx->ctx.stats.flags |= STAT_HIDE_DOWN;
3905 break;
3906 }
Amaury Denoyelle91e55ea2021-02-25 14:46:08 +01003907 }
3908
3909 for (h = lookup; h <= end - 9; h++) {
3910 if (memcmp(h, ";no-maint", 9) == 0) {
Willy Tarreau3e320362020-10-23 17:28:57 +02003911 appctx->ctx.stats.flags |= STAT_HIDE_MAINT;
3912 break;
3913 }
Christopher Faulet377c5a52018-10-24 21:21:30 +02003914 }
3915
3916 if (uri_auth->refresh) {
3917 for (h = lookup; h <= end - 10; h++) {
3918 if (memcmp(h, ";norefresh", 10) == 0) {
3919 appctx->ctx.stats.flags |= STAT_NO_REFRESH;
3920 break;
3921 }
3922 }
3923 }
3924
3925 for (h = lookup; h <= end - 4; h++) {
3926 if (memcmp(h, ";csv", 4) == 0) {
Christopher Faulet6338a082019-09-09 15:50:54 +02003927 appctx->ctx.stats.flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
Christopher Faulet377c5a52018-10-24 21:21:30 +02003928 break;
3929 }
3930 }
3931
3932 for (h = lookup; h <= end - 6; h++) {
3933 if (memcmp(h, ";typed", 6) == 0) {
Christopher Faulet6338a082019-09-09 15:50:54 +02003934 appctx->ctx.stats.flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
Christopher Faulet377c5a52018-10-24 21:21:30 +02003935 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
3936 break;
3937 }
3938 }
3939
Christopher Faulet6338a082019-09-09 15:50:54 +02003940 for (h = lookup; h <= end - 5; h++) {
3941 if (memcmp(h, ";json", 5) == 0) {
3942 appctx->ctx.stats.flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
3943 appctx->ctx.stats.flags |= STAT_FMT_JSON;
3944 break;
3945 }
3946 }
3947
3948 for (h = lookup; h <= end - 12; h++) {
3949 if (memcmp(h, ";json-schema", 12) == 0) {
3950 appctx->ctx.stats.flags &= ~STAT_FMT_MASK;
3951 appctx->ctx.stats.flags |= STAT_JSON_SCHM;
3952 break;
3953 }
3954 }
3955
Christopher Faulet377c5a52018-10-24 21:21:30 +02003956 for (h = lookup; h <= end - 8; h++) {
3957 if (memcmp(h, ";st=", 4) == 0) {
3958 int i;
3959 h += 4;
3960 appctx->ctx.stats.st_code = STAT_STATUS_UNKN;
3961 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
3962 if (strncmp(stat_status_codes[i], h, 4) == 0) {
3963 appctx->ctx.stats.st_code = i;
3964 break;
3965 }
3966 }
3967 break;
3968 }
3969 }
3970
3971 appctx->ctx.stats.scope_str = 0;
3972 appctx->ctx.stats.scope_len = 0;
3973 for (h = lookup; h <= end - 8; h++) {
3974 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
3975 int itx = 0;
3976 const char *h2;
3977 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
3978 const char *err;
3979
3980 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
3981 h2 = h;
Christopher Fauleted7a0662019-01-14 11:07:34 +01003982 appctx->ctx.stats.scope_str = h2 - HTX_SL_REQ_UPTR(sl);
3983 while (h < end) {
Christopher Faulet377c5a52018-10-24 21:21:30 +02003984 if (*h == ';' || *h == '&' || *h == ' ')
3985 break;
3986 itx++;
3987 h++;
3988 }
3989
3990 if (itx > STAT_SCOPE_TXT_MAXLEN)
3991 itx = STAT_SCOPE_TXT_MAXLEN;
3992 appctx->ctx.stats.scope_len = itx;
3993
3994 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
3995 memcpy(scope_txt, h2, itx);
3996 scope_txt[itx] = '\0';
3997 err = invalid_char(scope_txt);
3998 if (err) {
3999 /* bad char in search text => clear scope */
4000 appctx->ctx.stats.scope_str = 0;
4001 appctx->ctx.stats.scope_len = 0;
4002 }
4003 break;
4004 }
4005 }
4006
4007 /* now check whether we have some admin rules for this request */
4008 list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) {
4009 int ret = 1;
4010
4011 if (stats_admin_rule->cond) {
4012 ret = acl_exec_cond(stats_admin_rule->cond, s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
4013 ret = acl_pass(ret);
4014 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
4015 ret = !ret;
4016 }
4017
4018 if (ret) {
4019 /* no rule, or the rule matches */
4020 appctx->ctx.stats.flags |= STAT_ADMIN;
4021 break;
4022 }
4023 }
4024
Christopher Faulet5d45e382019-02-27 15:15:23 +01004025 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
4026 appctx->st0 = STAT_HTTP_HEAD;
4027 else if (txn->meth == HTTP_METH_POST) {
Christopher Fauletbd9e8422019-08-15 22:26:48 +02004028 if (appctx->ctx.stats.flags & STAT_ADMIN) {
Christopher Faulet377c5a52018-10-24 21:21:30 +02004029 appctx->st0 = STAT_HTTP_POST;
Christopher Fauletbd9e8422019-08-15 22:26:48 +02004030 if (msg->msg_state < HTTP_MSG_DATA)
4031 req->analysers |= AN_REQ_HTTP_BODY;
4032 }
Christopher Faulet377c5a52018-10-24 21:21:30 +02004033 else {
Christopher Faulet5d45e382019-02-27 15:15:23 +01004034 /* POST without admin level */
Christopher Faulet377c5a52018-10-24 21:21:30 +02004035 appctx->ctx.stats.flags &= ~STAT_CHUNKED;
4036 appctx->ctx.stats.st_code = STAT_STATUS_DENY;
4037 appctx->st0 = STAT_HTTP_LAST;
4038 }
4039 }
4040 else {
Christopher Faulet5d45e382019-02-27 15:15:23 +01004041 /* Unsupported method */
4042 appctx->ctx.stats.flags &= ~STAT_CHUNKED;
4043 appctx->ctx.stats.st_code = STAT_STATUS_IVAL;
4044 appctx->st0 = STAT_HTTP_LAST;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004045 }
4046
4047 s->task->nice = -32; /* small boost for HTTP statistics */
4048 return 1;
4049}
4050
Christopher Faulet021a8e42021-03-29 10:46:38 +02004051/* This function waits for the message payload at most <time> milliseconds (may
4052 * be set to TICK_ETERNITY). It stops to wait if at least <bytes> bytes of the
4053 * payload are received (0 means no limit). It returns HTTP_RULE_* depending on
4054 * the result:
4055 *
4056 * - HTTP_RULE_RES_CONT when conditions are met to stop waiting
4057 * - HTTP_RULE_RES_YIELD to wait for more data
Ilya Shipitsinb2be9a12021-04-24 13:25:42 +05004058 * - HTTP_RULE_RES_ABRT when a timeout occurred.
Christopher Faulet021a8e42021-03-29 10:46:38 +02004059 * - HTTP_RULE_RES_BADREQ if a parsing error is raised by lower level
Ilya Shipitsinb2be9a12021-04-24 13:25:42 +05004060 * - HTTP_RULE_RES_ERROR if an internal error occurred
Christopher Faulet021a8e42021-03-29 10:46:38 +02004061 *
Ilya Shipitsinb2be9a12021-04-24 13:25:42 +05004062 * If a timeout occurred, this function is responsible to emit the right response
Christopher Faulet021a8e42021-03-29 10:46:38 +02004063 * to the client, depending on the channel (408 on request side, 504 on response
4064 * side). All other errors must be handled by the caller.
4065 */
4066enum rule_result http_wait_for_msg_body(struct stream *s, struct channel *chn,
4067 unsigned int time, unsigned int bytes)
4068{
4069 struct session *sess = s->sess;
4070 struct http_txn *txn = s->txn;
4071 struct http_msg *msg = ((chn->flags & CF_ISRESP) ? &txn->rsp : &txn->req);
4072 struct htx *htx;
4073 enum rule_result ret = HTTP_RULE_RES_CONT;
4074
4075 htx = htxbuf(&chn->buf);
4076
4077 if (htx->flags & HTX_FL_PARSING_ERROR) {
4078 ret = HTTP_RULE_RES_BADREQ;
4079 goto end;
4080 }
4081 if (htx->flags & HTX_FL_PROCESSING_ERROR) {
4082 ret = HTTP_RULE_RES_ERROR;
4083 goto end;
4084 }
4085
4086 /* Do nothing for bodyless and CONNECT requests */
4087 if (txn->meth == HTTP_METH_CONNECT || (msg->flags & HTTP_MSGF_BODYLESS))
4088 goto end;
4089
4090 if (!(chn->flags & CF_ISRESP) && msg->msg_state < HTTP_MSG_DATA) {
4091 if (http_handle_expect_hdr(s, htx, msg) == -1) {
4092 ret = HTTP_RULE_RES_ERROR;
4093 goto end;
4094 }
4095 }
4096
4097 msg->msg_state = HTTP_MSG_DATA;
4098
4099 /* Now we're in HTTP_MSG_DATA. We just need to know if all data have
4100 * been received or if the buffer is full.
4101 */
Christopher Faulet78335962021-09-23 14:46:32 +02004102 if ((htx->flags & HTX_FL_EOM) ||
4103 htx_get_tail_type(htx) > HTX_BLK_DATA ||
4104 channel_htx_full(chn, htx, global.tune.maxrewrite) ||
4105 si_rx_blocked_room(chn_prod(chn)))
Christopher Faulet021a8e42021-03-29 10:46:38 +02004106 goto end;
4107
4108 if (bytes) {
4109 struct htx_blk *blk;
4110 unsigned int len = 0;
4111
4112 for (blk = htx_get_first_blk(htx); blk; blk = htx_get_next_blk(htx, blk)) {
4113 if (htx_get_blk_type(blk) != HTX_BLK_DATA)
4114 continue;
4115 len += htx_get_blksz(blk);
4116 if (len >= bytes)
4117 goto end;
4118 }
4119 }
4120
4121 if ((chn->flags & CF_READ_TIMEOUT) || tick_is_expired(chn->analyse_exp, now_ms)) {
4122 if (!(chn->flags & CF_ISRESP))
4123 goto abort_req;
4124 goto abort_res;
4125 }
4126
4127 /* we get here if we need to wait for more data */
4128 if (!(chn->flags & (CF_SHUTR | CF_READ_ERROR))) {
4129 if (!tick_isset(chn->analyse_exp))
4130 chn->analyse_exp = tick_add_ifset(now_ms, time);
4131 ret = HTTP_RULE_RES_YIELD;
4132 }
4133
4134 end:
4135 return ret;
4136
4137 abort_req:
4138 txn->status = 408;
4139 if (!(s->flags & SF_ERR_MASK))
4140 s->flags |= SF_ERR_CLITO;
4141 if (!(s->flags & SF_FINST_MASK))
4142 s->flags |= SF_FINST_D;
Willy Tarreau4781b152021-04-06 13:53:36 +02004143 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req);
Christopher Faulet021a8e42021-03-29 10:46:38 +02004144 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02004145 _HA_ATOMIC_INC(&sess->listener->counters->failed_req);
Christopher Faulet021a8e42021-03-29 10:46:38 +02004146 http_reply_and_close(s, txn->status, http_error_message(s));
4147 ret = HTTP_RULE_RES_ABRT;
4148 goto end;
4149
4150 abort_res:
4151 txn->status = 504;
4152 if (!(s->flags & SF_ERR_MASK))
4153 s->flags |= SF_ERR_SRVTO;
4154 if (!(s->flags & SF_FINST_MASK))
4155 s->flags |= SF_FINST_D;
4156 stream_inc_http_fail_ctr(s);
4157 http_reply_and_close(s, txn->status, http_error_message(s));
4158 ret = HTTP_RULE_RES_ABRT;
4159 goto end;
4160}
4161
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004162void http_perform_server_redirect(struct stream *s, struct stream_interface *si)
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004163{
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004164 struct channel *req = &s->req;
4165 struct channel *res = &s->res;
4166 struct server *srv;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004167 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004168 struct htx_sl *sl;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004169 struct ist path, location;
4170 unsigned int flags;
Amaury Denoyellec453f952021-07-06 11:40:12 +02004171 struct http_uri_parser parser;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004172
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004173 /*
4174 * Create the location
4175 */
4176 chunk_reset(&trash);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004177
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004178 /* 1: add the server's prefix */
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004179 /* special prefix "/" means don't change URL */
4180 srv = __objt_server(s->target);
4181 if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') {
4182 if (!chunk_memcat(&trash, srv->rdr_pfx, srv->rdr_len))
4183 return;
4184 }
4185
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004186 /* 2: add the request Path */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004187 htx = htxbuf(&req->buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02004188 sl = http_get_stline(htx);
Amaury Denoyellec453f952021-07-06 11:40:12 +02004189 parser = http_uri_parser_init(htx_sl_req_uri(sl));
4190 path = http_parse_path(&parser);
Tim Duesterhused526372020-03-05 17:56:33 +01004191 if (!isttest(path))
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004192 return;
4193
4194 if (!chunk_memcat(&trash, path.ptr, path.len))
4195 return;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004196 location = ist2(trash.area, trash.data);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004197
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004198 /*
4199 * Create the 302 respone
4200 */
4201 htx = htx_from_buf(&res->buf);
4202 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
4203 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
4204 ist("HTTP/1.1"), ist("302"), ist("Found"));
4205 if (!sl)
4206 goto fail;
4207 sl->info.res.status = 302;
4208 s->txn->status = 302;
4209
4210 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) ||
4211 !htx_add_header(htx, ist("Connection"), ist("close")) ||
4212 !htx_add_header(htx, ist("Content-length"), ist("0")) ||
4213 !htx_add_header(htx, ist("Location"), location))
4214 goto fail;
4215
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004216 if (!htx_add_endof(htx, HTX_BLK_EOH))
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004217 goto fail;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004218
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004219 htx->flags |= HTX_FL_EOM;
Christopher Fauletc20afb82020-01-24 19:16:26 +01004220 htx_to_buf(htx, &res->buf);
Christopher Fauleta72a7e42020-01-28 09:28:11 +01004221 if (!http_forward_proxy_resp(s, 1))
4222 goto fail;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004223
4224 /* return without error. */
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004225 si_shutr(si);
4226 si_shutw(si);
4227 si->err_type = SI_ET_NONE;
4228 si->state = SI_ST_CLO;
4229
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004230 if (!(s->flags & SF_ERR_MASK))
4231 s->flags |= SF_ERR_LOCAL;
4232 if (!(s->flags & SF_FINST_MASK))
4233 s->flags |= SF_FINST_C;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004234
4235 /* FIXME: we should increase a counter of redirects per server and per backend. */
4236 srv_inc_sess_ctr(srv);
4237 srv_set_sess_last(srv);
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004238 return;
4239
4240 fail:
4241 /* If an error occurred, remove the incomplete HTTP response from the
4242 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004243 channel_htx_truncate(res, htx);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004244}
4245
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05004246/* This function terminates the request because it was completely analyzed or
Christopher Fauletf2824e62018-10-01 12:12:37 +02004247 * because an error was triggered during the body forwarding.
4248 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004249static void http_end_request(struct stream *s)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004250{
4251 struct channel *chn = &s->req;
4252 struct http_txn *txn = s->txn;
4253
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004254 DBG_TRACE_ENTER(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004255
Christopher Fauletb42a8b62018-11-19 21:59:00 +01004256 if (unlikely(txn->req.msg_state == HTTP_MSG_ERROR ||
4257 txn->rsp.msg_state == HTTP_MSG_ERROR)) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004258 channel_abort(chn);
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004259 channel_htx_truncate(chn, htxbuf(&chn->buf));
Christopher Fauletf2824e62018-10-01 12:12:37 +02004260 goto end;
4261 }
4262
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004263 if (unlikely(txn->req.msg_state < HTTP_MSG_DONE)) {
4264 DBG_TRACE_DEVEL("waiting end of the request", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004265 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004266 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004267
4268 if (txn->req.msg_state == HTTP_MSG_DONE) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004269 /* No need to read anymore, the request was completely parsed.
4270 * We can shut the read side unless we want to abort_on_close,
4271 * or we have a POST request. The issue with POST requests is
4272 * that some browsers still send a CRLF after the request, and
4273 * this CRLF must be read so that it does not remain in the kernel
4274 * buffers, otherwise a close could cause an RST on some systems
4275 * (eg: Linux).
4276 */
Christopher Faulet769d0e92019-03-22 14:23:18 +01004277 if (!(s->be->options & PR_O_ABRT_CLOSE) && txn->meth != HTTP_METH_POST)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004278 channel_dont_read(chn);
4279
4280 /* if the server closes the connection, we want to immediately react
4281 * and close the socket to save packets and syscalls.
4282 */
4283 s->si[1].flags |= SI_FL_NOHALF;
4284
4285 /* In any case we've finished parsing the request so we must
4286 * disable Nagle when sending data because 1) we're not going
4287 * to shut this side, and 2) the server is waiting for us to
4288 * send pending data.
4289 */
4290 chn->flags |= CF_NEVER_WAIT;
4291
Christopher Fauletd01ce402019-01-02 17:44:13 +01004292 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
4293 /* The server has not finished to respond, so we
4294 * don't want to move in order not to upset it.
4295 */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004296 DBG_TRACE_DEVEL("waiting end of the response", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletd01ce402019-01-02 17:44:13 +01004297 return;
4298 }
4299
Christopher Fauletf2824e62018-10-01 12:12:37 +02004300 /* When we get here, it means that both the request and the
4301 * response have finished receiving. Depending on the connection
4302 * mode, we'll have to wait for the last bytes to leave in either
4303 * direction, and sometimes for a close to be effective.
4304 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02004305 if (txn->flags & TX_CON_WANT_TUN) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004306 /* Tunnel mode will not have any analyser so it needs to
4307 * poll for reads.
4308 */
4309 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004310 if (b_data(&chn->buf)) {
4311 DBG_TRACE_DEVEL("waiting to flush the request", STRM_EV_HTTP_ANA, s, txn);
Christopher Faulet9768c262018-10-22 09:34:31 +02004312 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004313 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004314 txn->req.msg_state = HTTP_MSG_TUNNEL;
4315 }
4316 else {
4317 /* we're not expecting any new data to come for this
4318 * transaction, so we can close it.
Christopher Faulet9768c262018-10-22 09:34:31 +02004319 *
4320 * However, there is an exception if the response
4321 * length is undefined. In this case, we need to wait
4322 * the close from the server. The response will be
4323 * switched in TUNNEL mode until the end.
Christopher Fauletf2824e62018-10-01 12:12:37 +02004324 */
4325 if (!(txn->rsp.flags & HTTP_MSGF_XFER_LEN) &&
4326 txn->rsp.msg_state != HTTP_MSG_CLOSED)
Christopher Faulet9768c262018-10-22 09:34:31 +02004327 goto check_channel_flags;
Christopher Fauletf2824e62018-10-01 12:12:37 +02004328
4329 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4330 channel_shutr_now(chn);
4331 channel_shutw_now(chn);
4332 }
4333 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004334 goto check_channel_flags;
4335 }
4336
4337 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
4338 http_msg_closing:
4339 /* nothing else to forward, just waiting for the output buffer
4340 * to be empty and for the shutw_now to take effect.
4341 */
4342 if (channel_is_empty(chn)) {
4343 txn->req.msg_state = HTTP_MSG_CLOSED;
4344 goto http_msg_closed;
4345 }
4346 else if (chn->flags & CF_SHUTW) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004347 txn->req.msg_state = HTTP_MSG_ERROR;
4348 goto end;
4349 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004350 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004351 return;
4352 }
4353
4354 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
4355 http_msg_closed:
Christopher Fauletf2824e62018-10-01 12:12:37 +02004356 /* if we don't know whether the server will close, we need to hard close */
4357 if (txn->rsp.flags & HTTP_MSGF_XFER_LEN)
4358 s->si[1].flags |= SI_FL_NOLINGER; /* we want to close ASAP */
Christopher Fauletf2824e62018-10-01 12:12:37 +02004359 /* see above in MSG_DONE why we only do this in these states */
Christopher Faulet769d0e92019-03-22 14:23:18 +01004360 if (!(s->be->options & PR_O_ABRT_CLOSE))
Christopher Fauletf2824e62018-10-01 12:12:37 +02004361 channel_dont_read(chn);
4362 goto end;
4363 }
4364
4365 check_channel_flags:
4366 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4367 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4368 /* if we've just closed an output, let's switch */
4369 txn->req.msg_state = HTTP_MSG_CLOSING;
4370 goto http_msg_closing;
4371 }
4372
4373 end:
4374 chn->analysers &= AN_REQ_FLT_END;
Christopher Faulet198ef8b2020-12-15 13:32:55 +01004375 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
4376 chn->flags |= CF_NEVER_WAIT;
4377 if (HAS_REQ_DATA_FILTERS(s))
Christopher Fauletf2824e62018-10-01 12:12:37 +02004378 chn->analysers |= AN_REQ_FLT_XFER_DATA;
Christopher Faulet198ef8b2020-12-15 13:32:55 +01004379 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004380 channel_auto_close(chn);
4381 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004382 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004383}
4384
4385
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05004386/* This function terminates the response because it was completely analyzed or
Christopher Fauletf2824e62018-10-01 12:12:37 +02004387 * because an error was triggered during the body forwarding.
4388 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004389static void http_end_response(struct stream *s)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004390{
4391 struct channel *chn = &s->res;
4392 struct http_txn *txn = s->txn;
4393
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004394 DBG_TRACE_ENTER(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004395
Christopher Fauletb42a8b62018-11-19 21:59:00 +01004396 if (unlikely(txn->req.msg_state == HTTP_MSG_ERROR ||
4397 txn->rsp.msg_state == HTTP_MSG_ERROR)) {
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004398 channel_htx_truncate(&s->req, htxbuf(&s->req.buf));
Christopher Faulet9768c262018-10-22 09:34:31 +02004399 channel_abort(&s->req);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004400 goto end;
4401 }
4402
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004403 if (unlikely(txn->rsp.msg_state < HTTP_MSG_DONE)) {
4404 DBG_TRACE_DEVEL("waiting end of the response", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004405 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004406 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004407
4408 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
4409 /* In theory, we don't need to read anymore, but we must
4410 * still monitor the server connection for a possible close
4411 * while the request is being uploaded, so we don't disable
4412 * reading.
4413 */
4414 /* channel_dont_read(chn); */
4415
4416 if (txn->req.msg_state < HTTP_MSG_DONE) {
4417 /* The client seems to still be sending data, probably
4418 * because we got an error response during an upload.
4419 * We have the choice of either breaking the connection
4420 * or letting it pass through. Let's do the later.
4421 */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004422 DBG_TRACE_DEVEL("waiting end of the request", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004423 return;
4424 }
4425
4426 /* When we get here, it means that both the request and the
4427 * response have finished receiving. Depending on the connection
4428 * mode, we'll have to wait for the last bytes to leave in either
4429 * direction, and sometimes for a close to be effective.
4430 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02004431 if (txn->flags & TX_CON_WANT_TUN) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004432 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004433 if (b_data(&chn->buf)) {
4434 DBG_TRACE_DEVEL("waiting to flush the respone", STRM_EV_HTTP_ANA, s, txn);
Christopher Faulet9768c262018-10-22 09:34:31 +02004435 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004436 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004437 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
4438 }
4439 else {
4440 /* we're not expecting any new data to come for this
4441 * transaction, so we can close it.
4442 */
4443 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4444 channel_shutr_now(chn);
4445 channel_shutw_now(chn);
4446 }
4447 }
4448 goto check_channel_flags;
4449 }
4450
4451 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
4452 http_msg_closing:
4453 /* nothing else to forward, just waiting for the output buffer
4454 * to be empty and for the shutw_now to take effect.
4455 */
4456 if (channel_is_empty(chn)) {
4457 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4458 goto http_msg_closed;
4459 }
4460 else if (chn->flags & CF_SHUTW) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004461 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau4781b152021-04-06 13:53:36 +02004462 _HA_ATOMIC_INC(&strm_sess(s)->fe->fe_counters.cli_aborts);
4463 _HA_ATOMIC_INC(&s->be->be_counters.cli_aborts);
William Lallemand36119de2021-03-08 15:26:48 +01004464 if (strm_sess(s)->listener && strm_sess(s)->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02004465 _HA_ATOMIC_INC(&strm_sess(s)->listener->counters->cli_aborts);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004466 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02004467 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.cli_aborts);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004468 goto end;
4469 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004470 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004471 return;
4472 }
4473
4474 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
4475 http_msg_closed:
4476 /* drop any pending data */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004477 channel_htx_truncate(&s->req, htxbuf(&s->req.buf));
Christopher Faulet9768c262018-10-22 09:34:31 +02004478 channel_abort(&s->req);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004479 goto end;
4480 }
4481
4482 check_channel_flags:
4483 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4484 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4485 /* if we've just closed an output, let's switch */
4486 txn->rsp.msg_state = HTTP_MSG_CLOSING;
4487 goto http_msg_closing;
4488 }
4489
4490 end:
4491 chn->analysers &= AN_RES_FLT_END;
Christopher Faulet198ef8b2020-12-15 13:32:55 +01004492 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
4493 chn->flags |= CF_NEVER_WAIT;
4494 if (HAS_RSP_DATA_FILTERS(s))
4495 chn->analysers |= AN_RES_FLT_XFER_DATA;
4496 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004497 channel_auto_close(chn);
4498 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004499 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004500}
4501
Christopher Fauletef70e252020-01-28 09:26:19 +01004502/* Forward a response generated by HAProxy (error/redirect/return). This
4503 * function forwards all pending incoming data. If <final> is set to 0, nothing
4504 * more is performed. It is used for 1xx informational messages. Otherwise, the
Christopher Faulet507479b2020-05-15 12:29:46 +02004505 * transaction is terminated and the request is emptied. On success 1 is
Christopher Faulet40e6b552020-06-25 16:04:50 +02004506 * returned. If an error occurred, 0 is returned. If it fails, this function
4507 * only exits. It is the caller responsibility to do the cleanup.
Christopher Fauletef70e252020-01-28 09:26:19 +01004508 */
4509int http_forward_proxy_resp(struct stream *s, int final)
4510{
4511 struct channel *req = &s->req;
4512 struct channel *res = &s->res;
4513 struct htx *htx = htxbuf(&res->buf);
4514 size_t data;
4515
4516 if (final) {
4517 htx->flags |= HTX_FL_PROXY_RESP;
Christopher Faulet507479b2020-05-15 12:29:46 +02004518
Christopher Fauletaab1b672020-11-18 16:44:02 +01004519 if (!htx_is_empty(htx) && !http_eval_after_res_rules(s))
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01004520 return 0;
Christopher Fauletef70e252020-01-28 09:26:19 +01004521
Christopher Fauletd6c48362020-10-19 18:01:38 +02004522 if (s->txn->meth == HTTP_METH_HEAD)
4523 htx_skip_msg_payload(htx);
4524
Christopher Fauletef70e252020-01-28 09:26:19 +01004525 channel_auto_read(req);
4526 channel_abort(req);
4527 channel_auto_close(req);
4528 channel_htx_erase(req, htxbuf(&req->buf));
4529
4530 res->wex = tick_add_ifset(now_ms, res->wto);
4531 channel_auto_read(res);
4532 channel_auto_close(res);
4533 channel_shutr_now(res);
Christopher Faulet1a9db7c2020-06-25 15:36:45 +02004534 res->flags |= CF_EOI; /* The response is terminated, add EOI */
Christopher Faulet42432f32020-11-20 17:43:16 +01004535 htxbuf(&res->buf)->flags |= HTX_FL_EOM; /* no more data are expected */
Christopher Fauletef70e252020-01-28 09:26:19 +01004536 }
Christopher Fauletcf6898c2020-06-25 15:55:11 +02004537 else {
4538 /* Send ASAP informational messages. Rely on CF_EOI for final
4539 * response.
4540 */
4541 res->flags |= CF_SEND_DONTWAIT;
4542 }
Christopher Fauletef70e252020-01-28 09:26:19 +01004543
4544 data = htx->data - co_data(res);
4545 c_adv(res, data);
4546 htx->first = -1;
4547 res->total += data;
4548 return 1;
4549}
4550
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004551void http_server_error(struct stream *s, struct stream_interface *si, int err,
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004552 int finst, struct http_reply *msg)
Christopher Faulet0f226952018-10-22 09:29:56 +02004553{
Christopher Faulet72c7d8d2020-01-27 15:32:25 +01004554 http_reply_and_close(s, s->txn->status, msg);
Christopher Faulet0f226952018-10-22 09:29:56 +02004555 if (!(s->flags & SF_ERR_MASK))
4556 s->flags |= err;
4557 if (!(s->flags & SF_FINST_MASK))
4558 s->flags |= finst;
4559}
4560
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004561void http_reply_and_close(struct stream *s, short status, struct http_reply *msg)
Christopher Faulet0f226952018-10-22 09:29:56 +02004562{
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004563 if (!msg) {
4564 channel_htx_truncate(&s->res, htxbuf(&s->res.buf));
4565 goto end;
4566 }
4567
4568 if (http_reply_message(s, msg) == -1) {
4569 /* On error, return a 500 error message, but don't rewrite it if
Christopher Faulet40e6b552020-06-25 16:04:50 +02004570 * it is already an internal error. If it was already a "const"
4571 * 500 error, just fail.
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004572 */
Christopher Faulet40e6b552020-06-25 16:04:50 +02004573 if (s->txn->status == 500) {
4574 if (s->txn->flags & TX_CONST_REPLY)
4575 goto end;
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004576 s->txn->flags |= TX_CONST_REPLY;
Christopher Faulet40e6b552020-06-25 16:04:50 +02004577 }
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004578 s->txn->status = 500;
4579 s->txn->http_reply = NULL;
4580 return http_reply_and_close(s, s->txn->status, http_error_message(s));
4581 }
4582
4583end:
4584 s->res.wex = tick_add_ifset(now_ms, s->res.wto);
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004585
Christopher Faulet2d565002021-09-10 09:17:50 +02004586 /* At this staged, HTTP analysis is finished */
4587 s->req.analysers &= AN_REQ_FLT_END;
4588 s->req.analyse_exp = TICK_ETERNITY;
4589
4590 s->res.analysers &= AN_RES_FLT_END;
4591 s->res.analyse_exp = TICK_ETERNITY;
4592
Christopher Faulet0f226952018-10-22 09:29:56 +02004593 channel_auto_read(&s->req);
4594 channel_abort(&s->req);
4595 channel_auto_close(&s->req);
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004596 channel_htx_erase(&s->req, htxbuf(&s->req.buf));
Christopher Faulet72c7d8d2020-01-27 15:32:25 +01004597 channel_auto_read(&s->res);
4598 channel_auto_close(&s->res);
4599 channel_shutr_now(&s->res);
Christopher Faulet0f226952018-10-22 09:29:56 +02004600}
4601
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004602struct http_reply *http_error_message(struct stream *s)
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004603{
4604 const int msgnum = http_get_status_idx(s->txn->status);
4605
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004606 if (s->txn->http_reply)
4607 return s->txn->http_reply;
4608 else if (s->be->replies[msgnum])
4609 return s->be->replies[msgnum];
4610 else if (strm_fe(s)->replies[msgnum])
4611 return strm_fe(s)->replies[msgnum];
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004612 else
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004613 return &http_err_replies[msgnum];
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004614}
4615
Christopher Faulet40e6b552020-06-25 16:04:50 +02004616/* Produces an HTX message from an http reply. Depending on the http reply type,
4617 * a, errorfile, an raw file or a log-format string is used. On success, it
4618 * returns 0. If an error occurs -1 is returned. If it fails, this function only
4619 * exits. It is the caller responsibility to do the cleanup.
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004620 */
Christopher Fauletae43b6c2020-05-27 15:24:22 +02004621int http_reply_to_htx(struct stream *s, struct htx *htx, struct http_reply *reply)
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004622{
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004623 struct buffer *errmsg;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004624 struct htx_sl *sl;
4625 struct buffer *body = NULL;
4626 const char *status, *reason, *clen, *ctype;
4627 unsigned int slflags;
4628 int ret = 0;
4629
Christopher Faulete29a97e2020-05-14 14:49:25 +02004630 /*
4631 * - HTTP_REPLY_ERRFILES unexpected here. handled as no payload if so
4632 *
4633 * - HTTP_REPLY_INDIRECT: switch on another reply if defined or handled
4634 * as no payload if NULL. the TXN status code is set with the status
4635 * of the original reply.
4636 */
4637
4638 if (reply->type == HTTP_REPLY_INDIRECT) {
4639 if (reply->body.reply)
4640 reply = reply->body.reply;
4641 }
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004642 if (reply->type == HTTP_REPLY_ERRMSG && !reply->body.errmsg) {
4643 /* get default error message */
4644 if (reply == s->txn->http_reply)
4645 s->txn->http_reply = NULL;
4646 reply = http_error_message(s);
4647 if (reply->type == HTTP_REPLY_INDIRECT) {
4648 if (reply->body.reply)
4649 reply = reply->body.reply;
4650 }
4651 }
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004652
4653 if (reply->type == HTTP_REPLY_ERRMSG) {
4654 /* implicit or explicit error message*/
4655 errmsg = reply->body.errmsg;
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004656 if (errmsg && !b_is_null(errmsg)) {
Christopher Faulet20567362020-05-15 14:52:49 +02004657 if (!htx_copy_msg(htx, errmsg))
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004658 goto fail;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004659 }
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004660 }
4661 else {
4662 /* no payload, file or log-format string */
4663 if (reply->type == HTTP_REPLY_RAW) {
4664 /* file */
4665 body = &reply->body.obj;
4666 }
4667 else if (reply->type == HTTP_REPLY_LOGFMT) {
4668 /* log-format string */
4669 body = alloc_trash_chunk();
4670 if (!body)
4671 goto fail_alloc;
4672 body->data = build_logline(s, body->area, body->size, &reply->body.fmt);
4673 }
4674 /* else no payload */
4675
4676 status = ultoa(reply->status);
4677 reason = http_get_reason(reply->status);
4678 slflags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_CLEN);
4679 if (!body || !b_data(body))
4680 slflags |= HTX_SL_F_BODYLESS;
4681 sl = htx_add_stline(htx, HTX_BLK_RES_SL, slflags, ist("HTTP/1.1"), ist(status), ist(reason));
4682 if (!sl)
4683 goto fail;
4684 sl->info.res.status = reply->status;
4685
4686 clen = (body ? ultoa(b_data(body)) : "0");
4687 ctype = reply->ctype;
4688
4689 if (!LIST_ISEMPTY(&reply->hdrs)) {
4690 struct http_reply_hdr *hdr;
4691 struct buffer *value = alloc_trash_chunk();
4692
4693 if (!value)
4694 goto fail;
4695
4696 list_for_each_entry(hdr, &reply->hdrs, list) {
4697 chunk_reset(value);
4698 value->data = build_logline(s, value->area, value->size, &hdr->value);
4699 if (b_data(value) && !htx_add_header(htx, hdr->name, ist2(b_head(value), b_data(value)))) {
4700 free_trash_chunk(value);
4701 goto fail;
4702 }
4703 chunk_reset(value);
4704 }
4705 free_trash_chunk(value);
4706 }
4707
4708 if (!htx_add_header(htx, ist("content-length"), ist(clen)) ||
4709 (body && b_data(body) && ctype && !htx_add_header(htx, ist("content-type"), ist(ctype))) ||
4710 !htx_add_endof(htx, HTX_BLK_EOH) ||
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004711 (body && b_data(body) && !htx_add_data_atonce(htx, ist2(b_head(body), b_data(body)))))
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004712 goto fail;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004713
4714 htx->flags |= HTX_FL_EOM;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004715 }
4716
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004717 leave:
4718 if (reply->type == HTTP_REPLY_LOGFMT)
4719 free_trash_chunk(body);
4720 return ret;
4721
4722 fail_alloc:
4723 if (!(s->flags & SF_ERR_MASK))
4724 s->flags |= SF_ERR_RESOURCE;
Christopher Faulet97e466c2020-05-15 15:12:47 +02004725 /* fall through */
4726 fail:
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004727 ret = -1;
4728 goto leave;
Christopher Faulet97e466c2020-05-15 15:12:47 +02004729}
4730
4731/* Send an http reply to the client. On success, it returns 0. If an error
Christopher Faulet40e6b552020-06-25 16:04:50 +02004732 * occurs -1 is returned and the response channel is truncated, removing this
4733 * way the faulty reply. This function may fail when the reply is formatted
4734 * (http_reply_to_htx) or when the reply is forwarded
4735 * (http_forward_proxy_resp). On the last case, it is because a
4736 * http-after-response rule fails.
Christopher Faulet97e466c2020-05-15 15:12:47 +02004737 */
4738int http_reply_message(struct stream *s, struct http_reply *reply)
4739{
4740 struct channel *res = &s->res;
4741 struct htx *htx = htx_from_buf(&res->buf);
4742
4743 if (s->txn->status == -1)
4744 s->txn->status = reply->status;
4745 channel_htx_truncate(res, htx);
4746
4747 if (http_reply_to_htx(s, htx, reply) == -1)
4748 goto fail;
4749
4750 htx_to_buf(htx, &s->res.buf);
4751 if (!http_forward_proxy_resp(s, 1))
4752 goto fail;
4753 return 0;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004754
4755 fail:
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004756 channel_htx_truncate(res, htx);
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004757 if (!(s->flags & SF_ERR_MASK))
4758 s->flags |= SF_ERR_PRXCOND;
Christopher Faulet97e466c2020-05-15 15:12:47 +02004759 return -1;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004760}
4761
Christopher Faulet304cc402019-07-15 15:46:28 +02004762/* Return the error message corresponding to si->err_type. It is assumed
4763 * that the server side is closed. Note that err_type is actually a
4764 * bitmask, where almost only aborts may be cumulated with other
4765 * values. We consider that aborted operations are more important
4766 * than timeouts or errors due to the fact that nobody else in the
4767 * logs might explain incomplete retries. All others should avoid
4768 * being cumulated. It should normally not be possible to have multiple
4769 * aborts at once, but just in case, the first one in sequence is reported.
4770 * Note that connection errors appearing on the second request of a keep-alive
4771 * connection are not reported since this allows the client to retry.
4772 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004773void http_return_srv_error(struct stream *s, struct stream_interface *si)
Christopher Faulet304cc402019-07-15 15:46:28 +02004774{
4775 int err_type = si->err_type;
4776
4777 /* set s->txn->status for http_error_message(s) */
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004778 if (err_type & SI_ET_QUEUE_ABRT) {
4779 s->txn->status = -1;
4780 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_Q, NULL);
4781 }
4782 else if (err_type & SI_ET_CONN_ABRT) {
4783 s->txn->status = -1;
4784 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_C, NULL);
4785 }
4786 else if (err_type & SI_ET_QUEUE_TO) {
4787 s->txn->status = 503;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004788 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_Q,
4789 http_error_message(s));
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004790 }
4791 else if (err_type & SI_ET_QUEUE_ERR) {
4792 s->txn->status = 503;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004793 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_Q,
4794 http_error_message(s));
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004795 }
4796 else if (err_type & SI_ET_CONN_TO) {
4797 s->txn->status = 503;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004798 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_C,
4799 (s->txn->flags & TX_NOT_FIRST) ? NULL :
4800 http_error_message(s));
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004801 }
4802 else if (err_type & SI_ET_CONN_ERR) {
4803 s->txn->status = 503;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004804 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_C,
4805 (s->flags & SF_SRV_REUSED) ? NULL :
4806 http_error_message(s));
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004807 }
4808 else if (err_type & SI_ET_CONN_RES) {
4809 s->txn->status = 503;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004810 http_server_error(s, si, SF_ERR_RESOURCE, SF_FINST_C,
4811 (s->txn->flags & TX_NOT_FIRST) ? NULL :
4812 http_error_message(s));
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004813 }
Christopher Faulet304cc402019-07-15 15:46:28 +02004814 else { /* SI_ET_CONN_OTHER and others */
4815 s->txn->status = 500;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004816 http_server_error(s, si, SF_ERR_INTERNAL, SF_FINST_C,
4817 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004818 }
4819}
4820
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004821
Christopher Faulet4a28a532019-03-01 11:19:40 +01004822/* Handle Expect: 100-continue for HTTP/1.1 messages if necessary. It returns 0
4823 * on success and -1 on error.
4824 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004825static int http_handle_expect_hdr(struct stream *s, struct htx *htx, struct http_msg *msg)
Christopher Faulet4a28a532019-03-01 11:19:40 +01004826{
4827 /* If we have HTTP/1.1 message with a body and Expect: 100-continue,
4828 * then we must send an HTTP/1.1 100 Continue intermediate response.
4829 */
4830 if (msg->msg_state == HTTP_MSG_BODY && (msg->flags & HTTP_MSGF_VER_11) &&
4831 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
4832 struct ist hdr = { .ptr = "Expect", .len = 6 };
4833 struct http_hdr_ctx ctx;
4834
4835 ctx.blk = NULL;
4836 /* Expect is allowed in 1.1, look for it */
4837 if (http_find_header(htx, hdr, &ctx, 0) &&
4838 unlikely(isteqi(ctx.value, ist2("100-continue", 12)))) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004839 if (http_reply_100_continue(s) == -1)
Christopher Faulet4a28a532019-03-01 11:19:40 +01004840 return -1;
4841 http_remove_header(htx, &ctx);
4842 }
4843 }
4844 return 0;
4845}
4846
Christopher Faulet23a3c792018-11-28 10:01:23 +01004847/* Send a 100-Continue response to the client. It returns 0 on success and -1
4848 * on error. The response channel is updated accordingly.
4849 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004850static int http_reply_100_continue(struct stream *s)
Christopher Faulet23a3c792018-11-28 10:01:23 +01004851{
4852 struct channel *res = &s->res;
4853 struct htx *htx = htx_from_buf(&res->buf);
4854 struct htx_sl *sl;
4855 unsigned int flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|
4856 HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
Christopher Faulet23a3c792018-11-28 10:01:23 +01004857
4858 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
4859 ist("HTTP/1.1"), ist("100"), ist("Continue"));
4860 if (!sl)
4861 goto fail;
4862 sl->info.res.status = 100;
4863
Christopher Faulet1d5ec092019-06-26 14:23:54 +02004864 if (!htx_add_endof(htx, HTX_BLK_EOH))
Christopher Faulet23a3c792018-11-28 10:01:23 +01004865 goto fail;
4866
Christopher Fauleta72a7e42020-01-28 09:28:11 +01004867 if (!http_forward_proxy_resp(s, 0))
4868 goto fail;
Christopher Faulet23a3c792018-11-28 10:01:23 +01004869 return 0;
4870
4871 fail:
4872 /* If an error occurred, remove the incomplete HTTP response from the
4873 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004874 channel_htx_truncate(res, htx);
Christopher Faulet23a3c792018-11-28 10:01:23 +01004875 return -1;
4876}
4877
Christopher Faulet12c51e22018-11-28 15:59:42 +01004878
Christopher Faulet0f226952018-10-22 09:29:56 +02004879/*
4880 * Capture headers from message <htx> according to header list <cap_hdr>, and
4881 * fill the <cap> pointers appropriately.
4882 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004883static void http_capture_headers(struct htx *htx, char **cap, struct cap_hdr *cap_hdr)
Christopher Faulet0f226952018-10-22 09:29:56 +02004884{
4885 struct cap_hdr *h;
4886 int32_t pos;
4887
Christopher Fauleta3f15502019-05-13 15:27:23 +02004888 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet0f226952018-10-22 09:29:56 +02004889 struct htx_blk *blk = htx_get_blk(htx, pos);
4890 enum htx_blk_type type = htx_get_blk_type(blk);
4891 struct ist n, v;
4892
4893 if (type == HTX_BLK_EOH)
4894 break;
4895 if (type != HTX_BLK_HDR)
4896 continue;
4897
4898 n = htx_get_blk_name(htx, blk);
4899
4900 for (h = cap_hdr; h; h = h->next) {
4901 if (h->namelen && (h->namelen == n.len) &&
4902 (strncasecmp(n.ptr, h->name, h->namelen) == 0)) {
4903 if (cap[h->index] == NULL)
4904 cap[h->index] =
4905 pool_alloc(h->pool);
4906
4907 if (cap[h->index] == NULL) {
4908 ha_alert("HTTP capture : out of memory.\n");
4909 break;
4910 }
4911
4912 v = htx_get_blk_value(htx, blk);
4913 if (v.len > h->len)
4914 v.len = h->len;
4915
4916 memcpy(cap[h->index], v.ptr, v.len);
4917 cap[h->index][v.len]=0;
4918 }
4919 }
4920 }
4921}
4922
Christopher Faulet0b6bdc52018-10-24 11:05:36 +02004923/* Delete a value in a header between delimiters <from> and <next>. The header
4924 * itself is delimited by <start> and <end> pointers. The number of characters
4925 * displaced is returned, and the pointer to the first delimiter is updated if
4926 * required. The function tries as much as possible to respect the following
4927 * principles :
4928 * - replace <from> delimiter by the <next> one unless <from> points to <start>,
4929 * in which case <next> is simply removed
4930 * - set exactly one space character after the new first delimiter, unless there
4931 * are not enough characters in the block being moved to do so.
4932 * - remove unneeded spaces before the previous delimiter and after the new
4933 * one.
4934 *
4935 * It is the caller's responsibility to ensure that :
4936 * - <from> points to a valid delimiter or <start> ;
4937 * - <next> points to a valid delimiter or <end> ;
4938 * - there are non-space chars before <from>.
4939 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004940static int http_del_hdr_value(char *start, char *end, char **from, char *next)
Christopher Faulet0b6bdc52018-10-24 11:05:36 +02004941{
4942 char *prev = *from;
4943
4944 if (prev == start) {
4945 /* We're removing the first value. eat the semicolon, if <next>
4946 * is lower than <end> */
4947 if (next < end)
4948 next++;
4949
4950 while (next < end && HTTP_IS_SPHT(*next))
4951 next++;
4952 }
4953 else {
4954 /* Remove useless spaces before the old delimiter. */
4955 while (HTTP_IS_SPHT(*(prev-1)))
4956 prev--;
4957 *from = prev;
4958
4959 /* copy the delimiter and if possible a space if we're
4960 * not at the end of the line.
4961 */
4962 if (next < end) {
4963 *prev++ = *next++;
4964 if (prev + 1 < next)
4965 *prev++ = ' ';
4966 while (next < end && HTTP_IS_SPHT(*next))
4967 next++;
4968 }
4969 }
4970 memmove(prev, next, end - next);
4971 return (prev - next);
4972}
4973
Christopher Faulet0f226952018-10-22 09:29:56 +02004974
4975/* Formats the start line of the request (without CRLF) and puts it in <str> and
Joseph Herlantc42c0e92018-11-25 10:43:27 -08004976 * return the written length. The line can be truncated if it exceeds <len>.
Christopher Faulet0f226952018-10-22 09:29:56 +02004977 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004978static size_t http_fmt_req_line(const struct htx_sl *sl, char *str, size_t len)
Christopher Faulet0f226952018-10-22 09:29:56 +02004979{
4980 struct ist dst = ist2(str, 0);
4981
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004982 if (istcat(&dst, htx_sl_req_meth(sl), len) == -1)
Christopher Faulet0f226952018-10-22 09:29:56 +02004983 goto end;
4984 if (dst.len + 1 > len)
4985 goto end;
4986 dst.ptr[dst.len++] = ' ';
4987
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004988 if (istcat(&dst, htx_sl_req_uri(sl), len) == -1)
Christopher Faulet0f226952018-10-22 09:29:56 +02004989 goto end;
4990 if (dst.len + 1 > len)
4991 goto end;
4992 dst.ptr[dst.len++] = ' ';
4993
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004994 istcat(&dst, htx_sl_req_vsn(sl), len);
Christopher Faulet0f226952018-10-22 09:29:56 +02004995 end:
4996 return dst.len;
4997}
4998
4999/*
5000 * Print a debug line with a start line.
5001 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005002static void http_debug_stline(const char *dir, struct stream *s, const struct htx_sl *sl)
Christopher Faulet0f226952018-10-22 09:29:56 +02005003{
5004 struct session *sess = strm_sess(s);
5005 int max;
5006
5007 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
5008 dir,
5009 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->handle.fd : -1,
5010 objt_cs(s->si[1].end) ? (unsigned short)objt_cs(s->si[1].end)->conn->handle.fd : -1);
5011
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005012 max = HTX_SL_P1_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02005013 UBOUND(max, trash.size - trash.data - 3);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005014 chunk_memcat(&trash, HTX_SL_P1_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02005015 trash.area[trash.data++] = ' ';
5016
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005017 max = HTX_SL_P2_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02005018 UBOUND(max, trash.size - trash.data - 2);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005019 chunk_memcat(&trash, HTX_SL_P2_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02005020 trash.area[trash.data++] = ' ';
5021
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005022 max = HTX_SL_P3_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02005023 UBOUND(max, trash.size - trash.data - 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005024 chunk_memcat(&trash, HTX_SL_P3_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02005025 trash.area[trash.data++] = '\n';
5026
Willy Tarreau2e8ab6b2020-03-14 11:03:20 +01005027 DISGUISE(write(1, trash.area, trash.data));
Christopher Faulet0f226952018-10-22 09:29:56 +02005028}
5029
5030/*
5031 * Print a debug line with a header.
5032 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005033static 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 +02005034{
5035 struct session *sess = strm_sess(s);
5036 int max;
5037
5038 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
5039 dir,
5040 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->handle.fd : -1,
5041 objt_cs(s->si[1].end) ? (unsigned short)objt_cs(s->si[1].end)->conn->handle.fd : -1);
5042
5043 max = n.len;
5044 UBOUND(max, trash.size - trash.data - 3);
5045 chunk_memcat(&trash, n.ptr, max);
5046 trash.area[trash.data++] = ':';
5047 trash.area[trash.data++] = ' ';
5048
5049 max = v.len;
5050 UBOUND(max, trash.size - trash.data - 1);
5051 chunk_memcat(&trash, v.ptr, max);
5052 trash.area[trash.data++] = '\n';
5053
Willy Tarreau2e8ab6b2020-03-14 11:03:20 +01005054 DISGUISE(write(1, trash.area, trash.data));
Christopher Faulet0f226952018-10-22 09:29:56 +02005055}
5056
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005057/* Allocate a new HTTP transaction for stream <s> unless there is one already.
5058 * In case of allocation failure, everything allocated is freed and NULL is
5059 * returned. Otherwise the new transaction is assigned to the stream and
5060 * returned.
5061 */
5062struct http_txn *http_alloc_txn(struct stream *s)
5063{
5064 struct http_txn *txn = s->txn;
5065
5066 if (txn)
5067 return txn;
5068
5069 txn = pool_alloc(pool_head_http_txn);
5070 if (!txn)
5071 return txn;
5072
5073 s->txn = txn;
5074 return txn;
5075}
5076
5077void http_txn_reset_req(struct http_txn *txn)
5078{
Christopher Faulet1aea50e2020-01-17 16:03:53 +01005079 txn->req.flags = 0;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005080 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
5081}
5082
5083void http_txn_reset_res(struct http_txn *txn)
5084{
Christopher Faulet1aea50e2020-01-17 16:03:53 +01005085 txn->rsp.flags = 0;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005086 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
5087}
5088
5089/*
Christopher Faulet75f619a2021-03-08 19:12:58 +01005090 * Create and initialize a new HTTP transaction for stream <s>. This should be
5091 * used before processing any new request. It returns the transaction or NLULL
5092 * on error.
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005093 */
Christopher Faulet75f619a2021-03-08 19:12:58 +01005094struct http_txn *http_create_txn(struct stream *s)
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005095{
Christopher Faulet75f619a2021-03-08 19:12:58 +01005096 struct http_txn *txn;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005097 struct conn_stream *cs = objt_cs(s->si[0].end);
5098
Christopher Faulet75f619a2021-03-08 19:12:58 +01005099 txn = pool_alloc(pool_head_http_txn);
5100 if (!txn)
5101 return NULL;
5102 s->txn = txn;
5103
Christopher Fauletda831fa2020-10-06 17:58:43 +02005104 txn->flags = ((cs && cs->flags & CS_FL_NOT_FIRST) ? TX_NOT_FIRST : 0);
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005105 txn->status = -1;
Christopher Faulet5cb513a2020-05-13 17:56:56 +02005106 txn->http_reply = NULL;
Willy Tarreau8b507582020-02-25 09:35:07 +01005107 write_u32(txn->cache_hash, 0);
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005108
5109 txn->cookie_first_date = 0;
5110 txn->cookie_last_date = 0;
5111
5112 txn->srv_cookie = NULL;
5113 txn->cli_cookie = NULL;
5114 txn->uri = NULL;
5115
5116 http_txn_reset_req(txn);
5117 http_txn_reset_res(txn);
5118
5119 txn->req.chn = &s->req;
5120 txn->rsp.chn = &s->res;
5121
5122 txn->auth.method = HTTP_AUTH_UNKNOWN;
5123
Willy Tarreaub7bfcb32021-08-31 08:13:25 +02005124 vars_init_head(&s->vars_txn, SCOPE_TXN);
5125 vars_init_head(&s->vars_reqres, SCOPE_REQ);
Christopher Faulet75f619a2021-03-08 19:12:58 +01005126
5127 return txn;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005128}
5129
5130/* to be used at the end of a transaction */
Christopher Faulet75f619a2021-03-08 19:12:58 +01005131void http_destroy_txn(struct stream *s)
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005132{
5133 struct http_txn *txn = s->txn;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005134
5135 /* these ones will have been dynamically allocated */
5136 pool_free(pool_head_requri, txn->uri);
5137 pool_free(pool_head_capture, txn->cli_cookie);
5138 pool_free(pool_head_capture, txn->srv_cookie);
Tim Duesterhusa17e6622020-03-05 20:19:02 +01005139 pool_free(pool_head_uniqueid, s->unique_id.ptr);
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005140
Tim Duesterhusa17e6622020-03-05 20:19:02 +01005141 s->unique_id = IST_NULL;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005142 txn->uri = NULL;
5143 txn->srv_cookie = NULL;
5144 txn->cli_cookie = NULL;
5145
Christopher Faulet59399252019-11-07 14:27:52 +01005146 if (!LIST_ISEMPTY(&s->vars_txn.head))
5147 vars_prune(&s->vars_txn, s->sess, s);
5148 if (!LIST_ISEMPTY(&s->vars_reqres.head))
5149 vars_prune(&s->vars_reqres, s->sess, s);
Christopher Faulet75f619a2021-03-08 19:12:58 +01005150
5151 pool_free(pool_head_http_txn, txn);
5152 s->txn = NULL;
Christopher Faulet59399252019-11-07 14:27:52 +01005153}
5154
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005155
5156DECLARE_POOL(pool_head_http_txn, "http_txn", sizeof(struct http_txn));
Christopher Faulet0f226952018-10-22 09:29:56 +02005157
Christopher Fauletf4eb75d2018-10-11 15:55:07 +02005158__attribute__((constructor))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005159static void __http_protocol_init(void)
Christopher Fauletf4eb75d2018-10-11 15:55:07 +02005160{
5161}
5162
5163
5164/*
5165 * Local variables:
5166 * c-indent-level: 8
5167 * c-basic-offset: 8
5168 * End:
5169 */