blob: 7a0417486d25ac3ecb64c5d3a1d933dcf436b708 [file] [log] [blame]
Christopher Fauletf4eb75d2018-10-11 15:55:07 +02001/*
2 * HTTP protocol analyzer
3 *
4 * Copyright (C) 2018 HAProxy Technologies, Christopher Faulet <cfaulet@haproxy.com>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
Willy Tarreaudcc048a2020-06-04 19:11:43 +020013#include <haproxy/acl.h>
Willy Tarreau122eba92020-06-04 10:15:32 +020014#include <haproxy/action-t.h>
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020015#include <haproxy/api.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020016#include <haproxy/backend.h>
Willy Tarreau8d366972020-05-27 16:10:29 +020017#include <haproxy/base64.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020018#include <haproxy/capture-t.h>
Amaury Denoyelle03517732021-05-07 14:25:01 +020019#include <haproxy/cfgparse.h>
Willy Tarreauf1d32c42020-06-04 21:07:02 +020020#include <haproxy/channel.h>
Willy Tarreau4aa573d2020-06-04 18:21:56 +020021#include <haproxy/check.h>
Willy Tarreau7ea393d2020-06-04 18:02:10 +020022#include <haproxy/connection.h>
Willy Tarreau36979d92020-06-05 17:27:29 +020023#include <haproxy/errors.h>
Willy Tarreauc7babd82020-06-04 21:29:29 +020024#include <haproxy/filters.h>
Willy Tarreaucd72d8c2020-06-02 19:11:26 +020025#include <haproxy/http.h>
Willy Tarreauc2b1ff02020-06-04 21:21:03 +020026#include <haproxy/http_ana.h>
Willy Tarreau87735332020-06-04 09:08:41 +020027#include <haproxy/http_htx.h>
Willy Tarreau16f958c2020-06-03 08:44:35 +020028#include <haproxy/htx.h>
Willy Tarreauaeed4a82020-06-04 22:01:04 +020029#include <haproxy/log.h>
Willy Tarreau6131d6a2020-06-02 16:48:09 +020030#include <haproxy/net_helper.h>
Willy Tarreaua264d962020-06-04 22:29:18 +020031#include <haproxy/proxy.h>
Willy Tarreau7cd8b6e2020-06-02 17:32:26 +020032#include <haproxy/regex.h>
Willy Tarreau1e56f922020-06-04 23:20:13 +020033#include <haproxy/server-t.h>
Willy Tarreau2eec9b52020-06-04 19:58:55 +020034#include <haproxy/stats.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020035#include <haproxy/stream.h>
Willy Tarreau5e539c92020-06-04 20:45:39 +020036#include <haproxy/stream_interface.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020037#include <haproxy/trace.h>
Willy Tarreau8c42b8a2020-06-04 19:27:34 +020038#include <haproxy/uri_auth-t.h>
Willy Tarreaua1718922020-06-04 16:25:31 +020039#include <haproxy/vars.h>
Christopher Faulete0768eb2018-10-03 16:38:02 +020040
Christopher Faulete0768eb2018-10-03 16:38:02 +020041
Christopher Fauleteea8fc72019-11-05 16:18:10 +010042#define TRACE_SOURCE &trace_strm
43
Christopher Faulet377c5a52018-10-24 21:21:30 +020044extern const char *stat_status_codes[];
Christopher Fauletf2824e62018-10-01 12:12:37 +020045
Willy Tarreauff882702021-04-10 17:23:00 +020046struct pool_head *pool_head_requri __read_mostly = NULL;
47struct pool_head *pool_head_capture __read_mostly = NULL;
Christopher Fauleta8a46e22019-07-16 14:53:09 +020048
49
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020050static void http_end_request(struct stream *s);
51static void http_end_response(struct stream *s);
Christopher Fauletf2824e62018-10-01 12:12:37 +020052
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020053static void http_capture_headers(struct htx *htx, char **cap, struct cap_hdr *cap_hdr);
54static int http_del_hdr_value(char *start, char *end, char **from, char *next);
55static size_t http_fmt_req_line(const struct htx_sl *sl, char *str, size_t len);
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020056static void http_debug_stline(const char *dir, struct stream *s, const struct htx_sl *sl);
57static void http_debug_hdr(const char *dir, struct stream *s, const struct ist n, const struct ist v);
Christopher Faulet0f226952018-10-22 09:29:56 +020058
Christopher Fauletb58f62b2020-01-13 16:40:13 +010059static enum rule_result http_req_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s);
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020060static enum rule_result http_res_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s);
Christopher 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
339 req->analysers &= AN_REQ_FLT_END;
340 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100341 DBG_TRACE_DEVEL("leaving on error",
342 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200343 return 0;
344}
345
346
347/* This stream analyser runs all HTTP request processing which is common to
348 * frontends and backends, which means blocking ACLs, filters, connection-close,
349 * reqadd, stats and redirects. This is performed for the designated proxy.
350 * It returns 1 if the processing can continue on next analysers, or zero if it
351 * either needs more data or wants to immediately abort the request (eg: deny,
352 * error, ...).
353 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200354int http_process_req_common(struct stream *s, struct channel *req, int an_bit, struct proxy *px)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200355{
356 struct session *sess = s->sess;
357 struct http_txn *txn = s->txn;
358 struct http_msg *msg = &txn->req;
Christopher Fauletff2759f2018-10-24 11:13:16 +0200359 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200360 struct redirect_rule *rule;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200361 enum rule_result verdict;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200362 struct connection *conn = objt_conn(sess->origin);
363
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100364 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200365
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100366 htx = htxbuf(&req->buf);
Christopher Fauletff2759f2018-10-24 11:13:16 +0200367
Christopher Faulet1907ccc2019-04-29 13:12:02 +0200368 /* just in case we have some per-backend tracking. Only called the first
369 * execution of the analyser. */
370 if (!s->current_rule || s->current_rule_list != &px->http_req_rules)
371 stream_inc_be_http_req_ctr(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200372
373 /* evaluate http-request rules */
374 if (!LIST_ISEMPTY(&px->http_req_rules)) {
Christopher Fauletb58f62b2020-01-13 16:40:13 +0100375 verdict = http_req_get_intercept_rule(px, &px->http_req_rules, s);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200376
377 switch (verdict) {
378 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
379 goto return_prx_yield;
380
381 case HTTP_RULE_RES_CONT:
382 case HTTP_RULE_RES_STOP: /* nothing to do */
383 break;
384
385 case HTTP_RULE_RES_DENY: /* deny or tarpit */
386 if (txn->flags & TX_CLTARPIT)
387 goto tarpit;
388 goto deny;
389
390 case HTTP_RULE_RES_ABRT: /* abort request, response already sent. Eg: auth */
391 goto return_prx_cond;
392
393 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
394 goto done;
395
396 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
397 goto return_bad_req;
Christopher Faulet3a26bee2019-12-16 12:47:40 +0100398
399 case HTTP_RULE_RES_ERROR: /* failed with a bad request */
400 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200401 }
402 }
403
404 if (conn && (conn->flags & CO_FL_EARLY_DATA) &&
Olivier Houchard220a26c2020-01-23 14:57:36 +0100405 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_SSL_WAIT_HS))) {
Christopher Fauletff2759f2018-10-24 11:13:16 +0200406 struct http_hdr_ctx ctx;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200407
Christopher Fauletff2759f2018-10-24 11:13:16 +0200408 ctx.blk = NULL;
409 if (!http_find_header(htx, ist("Early-Data"), &ctx, 0)) {
410 if (unlikely(!http_add_header(htx, ist("Early-Data"), ist("1"))))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100411 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200412 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200413 }
414
415 /* OK at this stage, we know that the request was accepted according to
416 * the http-request rules, we can check for the stats. Note that the
417 * URI is detected *before* the req* rules in order not to be affected
418 * by a possible reqrep, while they are processed *after* so that a
419 * reqdeny can still block them. This clearly needs to change in 1.6!
420 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200421 if (!s->target && http_stats_check_uri(s, txn, px)) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200422 s->target = &http_stats_applet.obj_type;
Willy Tarreau14bfe9a2018-12-19 15:19:27 +0100423 if (unlikely(!si_register_handler(&s->si[1], objt_applet(s->target)))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200424 s->logs.tv_request = now;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200425 if (!(s->flags & SF_ERR_MASK))
426 s->flags |= SF_ERR_RESOURCE;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100427 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200428 }
429
430 /* parse the whole stats request and extract the relevant information */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200431 http_handle_stats(s, req);
Christopher Fauletb58f62b2020-01-13 16:40:13 +0100432 verdict = http_req_get_intercept_rule(px, &px->uri_auth->http_req_rules, s);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200433 /* not all actions implemented: deny, allow, auth */
434
435 if (verdict == HTTP_RULE_RES_DENY) /* stats http-request deny */
436 goto deny;
437
438 if (verdict == HTTP_RULE_RES_ABRT) /* stats auth / stats http-request auth */
439 goto return_prx_cond;
Christopher Faulet3a26bee2019-12-16 12:47:40 +0100440
441 if (verdict == HTTP_RULE_RES_BADREQ) /* failed with a bad request */
442 goto return_bad_req;
443
444 if (verdict == HTTP_RULE_RES_ERROR) /* failed with a bad request */
445 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200446 }
447
Christopher Faulet2571bc62019-03-01 11:44:26 +0100448 /* Proceed with the applets now. */
449 if (unlikely(objt_applet(s->target))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200450 if (sess->fe == s->be) /* report it if the request was intercepted by the frontend */
Willy Tarreau4781b152021-04-06 13:53:36 +0200451 _HA_ATOMIC_INC(&sess->fe->fe_counters.intercepted_req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200452
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200453 if (http_handle_expect_hdr(s, htx, msg) == -1)
Christopher Fauletb8a53712019-12-16 11:29:38 +0100454 goto return_int_err;
Christopher Fauletbcf242a2019-03-01 11:36:26 +0100455
Christopher Faulete0768eb2018-10-03 16:38:02 +0200456 if (!(s->flags & SF_ERR_MASK)) // this is not really an error but it is
457 s->flags |= SF_ERR_LOCAL; // to mark that it comes from the proxy
458 if (!(s->flags & SF_FINST_MASK))
459 s->flags |= SF_FINST_R;
460
Christopher Fauletc2ac5e42021-03-08 18:20:09 +0100461 if (HAS_FILTERS(s))
462 req->analysers |= AN_REQ_FLT_HTTP_HDRS;
463
Christopher Faulete0768eb2018-10-03 16:38:02 +0200464 /* enable the minimally required analyzers to handle keep-alive and compression on the HTTP response */
465 req->analysers &= (AN_REQ_HTTP_BODY | AN_REQ_FLT_HTTP_HDRS | AN_REQ_FLT_END);
466 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
467 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Christopher Fauletbcf242a2019-03-01 11:36:26 +0100468
469 req->flags |= CF_SEND_DONTWAIT;
470 s->flags |= SF_ASSIGNED;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200471 goto done;
472 }
473
474 /* check whether we have some ACLs set to redirect this request */
475 list_for_each_entry(rule, &px->redirect_rules, list) {
476 if (rule->cond) {
477 int ret;
478
479 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
480 ret = acl_pass(ret);
481 if (rule->cond->pol == ACL_COND_UNLESS)
482 ret = !ret;
483 if (!ret)
484 continue;
485 }
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200486 if (!http_apply_redirect_rule(rule, s, txn))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100487 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200488 goto done;
489 }
490
491 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
492 * If this happens, then the data will not come immediately, so we must
493 * send all what we have without waiting. Note that due to the small gain
494 * in waiting for the body of the request, it's easier to simply put the
495 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
496 * itself once used.
497 */
498 req->flags |= CF_SEND_DONTWAIT;
499
500 done: /* done with this analyser, continue with next ones that the calling
501 * points will have set, if any.
502 */
503 req->analyse_exp = TICK_ETERNITY;
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +0500504 done_without_exp: /* done with this analyser, but don't reset the analyse_exp. */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200505 req->analysers &= ~an_bit;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100506 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200507 return 1;
508
509 tarpit:
510 /* Allow cookie logging
511 */
512 if (s->be->cookie_name || sess->fe->capture_name)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200513 http_manage_client_side_cookies(s, req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200514
515 /* When a connection is tarpitted, we use the tarpit timeout,
516 * which may be the same as the connect timeout if unspecified.
517 * If unset, then set it to zero because we really want it to
518 * eventually expire. We build the tarpit as an analyser.
519 */
Christopher Faulet202c6ce2019-01-07 14:57:35 +0100520 channel_htx_erase(&s->req, htx);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200521
522 /* wipe the request out so that we can drop the connection early
523 * if the client closes first.
524 */
525 channel_dont_connect(req);
526
Christopher Faulete0768eb2018-10-03 16:38:02 +0200527 req->analysers &= AN_REQ_FLT_END; /* remove switching rules etc... */
528 req->analysers |= AN_REQ_HTTP_TARPIT;
529 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
530 if (!req->analyse_exp)
531 req->analyse_exp = tick_add(now_ms, 0);
532 stream_inc_http_err_ctr(s);
Willy Tarreau4781b152021-04-06 13:53:36 +0200533 _HA_ATOMIC_INC(&sess->fe->fe_counters.denied_req);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100534 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau4781b152021-04-06 13:53:36 +0200535 _HA_ATOMIC_INC(&s->be->be_counters.denied_req);
William Lallemand36119de2021-03-08 15:26:48 +0100536 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200537 _HA_ATOMIC_INC(&sess->listener->counters->denied_req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200538 goto done_without_exp;
539
540 deny: /* this request was blocked (denied) */
541
542 /* Allow cookie logging
543 */
544 if (s->be->cookie_name || sess->fe->capture_name)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200545 http_manage_client_side_cookies(s, req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200546
Christopher Faulete0768eb2018-10-03 16:38:02 +0200547 s->logs.tv_request = now;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200548 stream_inc_http_err_ctr(s);
Willy Tarreau4781b152021-04-06 13:53:36 +0200549 _HA_ATOMIC_INC(&sess->fe->fe_counters.denied_req);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100550 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau4781b152021-04-06 13:53:36 +0200551 _HA_ATOMIC_INC(&s->be->be_counters.denied_req);
William Lallemand36119de2021-03-08 15:26:48 +0100552 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200553 _HA_ATOMIC_INC(&sess->listener->counters->denied_req);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100554 goto return_prx_err;
555
556 return_int_err:
557 txn->status = 500;
558 if (!(s->flags & SF_ERR_MASK))
559 s->flags |= SF_ERR_INTERNAL;
Willy Tarreau4781b152021-04-06 13:53:36 +0200560 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100561 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau4781b152021-04-06 13:53:36 +0200562 _HA_ATOMIC_INC(&s->be->be_counters.internal_errors);
William Lallemand36119de2021-03-08 15:26:48 +0100563 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200564 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100565 goto return_prx_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200566
567 return_bad_req:
Christopher Faulete0768eb2018-10-03 16:38:02 +0200568 txn->status = 400;
Willy Tarreau4781b152021-04-06 13:53:36 +0200569 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req);
William Lallemand36119de2021-03-08 15:26:48 +0100570 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200571 _HA_ATOMIC_INC(&sess->listener->counters->failed_req);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100572 /* fall through */
573
574 return_prx_err:
575 http_reply_and_close(s, txn->status, http_error_message(s));
576 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200577
578 return_prx_cond:
579 if (!(s->flags & SF_ERR_MASK))
580 s->flags |= SF_ERR_PRXCOND;
581 if (!(s->flags & SF_FINST_MASK))
582 s->flags |= SF_FINST_R;
583
584 req->analysers &= AN_REQ_FLT_END;
585 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100586 DBG_TRACE_DEVEL("leaving on error",
587 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200588 return 0;
589
590 return_prx_yield:
591 channel_dont_connect(req);
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100592 DBG_TRACE_DEVEL("waiting for more data",
593 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200594 return 0;
595}
596
597/* This function performs all the processing enabled for the current request.
598 * It returns 1 if the processing can continue on next analysers, or zero if it
599 * needs more data, encounters an error, or wants to immediately abort the
600 * request. It relies on buffers flags, and updates s->req.analysers.
601 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200602int http_process_request(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200603{
604 struct session *sess = s->sess;
605 struct http_txn *txn = s->txn;
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200606 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200607 struct connection *cli_conn = objt_conn(strm_sess(s)->origin);
608
Christopher Faulet8bebd2f2020-10-06 17:54:56 +0200609 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200610
611 /*
612 * Right now, we know that we have processed the entire headers
613 * and that unwanted requests have been filtered out. We can do
614 * whatever we want with the remaining request. Also, now we
615 * may have separate values for ->fe, ->be.
616 */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100617 htx = htxbuf(&req->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200618
619 /*
Christopher Faulete0768eb2018-10-03 16:38:02 +0200620 * 7: Now we can work with the cookies.
621 * Note that doing so might move headers in the request, but
622 * the fields will stay coherent and the URI will not move.
623 * This should only be performed in the backend.
624 */
625 if (s->be->cookie_name || sess->fe->capture_name)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200626 http_manage_client_side_cookies(s, req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200627
Tim Duesterhus2825b4b2020-02-28 15:13:34 +0100628 /* 8: Generate unique ID if a "unique-id-format" is defined.
629 *
630 * A unique ID is generated even when it is not sent to ensure that the ID can make use of
631 * fetches only available in the HTTP request processing stage.
632 */
633 if (!LIST_ISEMPTY(&sess->fe->format_unique_id)) {
Tim Duesterhusa17e6622020-03-05 20:19:02 +0100634 struct ist unique_id = stream_generate_unique_id(s, &sess->fe->format_unique_id);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200635
Tim Duesterhusa17e6622020-03-05 20:19:02 +0100636 if (!isttest(unique_id)) {
Christopher Fauletb8a53712019-12-16 11:29:38 +0100637 if (!(s->flags & SF_ERR_MASK))
638 s->flags |= SF_ERR_RESOURCE;
639 goto return_int_err;
640 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200641
Tim Duesterhus2825b4b2020-02-28 15:13:34 +0100642 /* send unique ID if a "unique-id-header" is defined */
Tim Duesterhus0643b0e2020-03-05 17:56:35 +0100643 if (isttest(sess->fe->header_unique_id) &&
Tim Duesterhusa17e6622020-03-05 20:19:02 +0100644 unlikely(!http_add_header(htx, sess->fe->header_unique_id, s->unique_id)))
Tim Duesterhus2825b4b2020-02-28 15:13:34 +0100645 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200646 }
647
648 /*
649 * 9: add X-Forwarded-For if either the frontend or the backend
650 * asks for it.
651 */
652 if ((sess->fe->options | s->be->options) & PR_O_FWDFOR) {
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200653 struct http_hdr_ctx ctx = { .blk = NULL };
654 struct ist hdr = ist2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : sess->fe->fwdfor_hdr_name,
655 s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : sess->fe->fwdfor_hdr_len);
656
Christopher Faulete0768eb2018-10-03 16:38:02 +0200657 if (!((sess->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200658 http_find_header(htx, hdr, &ctx, 0)) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200659 /* The header is set to be added only if none is present
660 * and we found it, so don't do anything.
661 */
662 }
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200663 else if (cli_conn && conn_get_src(cli_conn) && cli_conn->src->ss_family == AF_INET) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200664 /* Add an X-Forwarded-For header unless the source IP is
665 * in the 'except' network range.
666 */
Christopher Faulet5d1def62021-02-26 09:19:15 +0100667 if (ipcmp2net(cli_conn->src, &sess->fe->except_xff_net) &&
668 ipcmp2net(cli_conn->src, &s->be->except_xff_net)) {
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200669 unsigned char *pn = (unsigned char *)&((struct sockaddr_in *)cli_conn->src)->sin_addr;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200670
671 /* Note: we rely on the backend to get the header name to be used for
672 * x-forwarded-for, because the header is really meant for the backends.
673 * However, if the backend did not specify any option, we have to rely
674 * on the frontend's header name.
675 */
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200676 chunk_printf(&trash, "%d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
677 if (unlikely(!http_add_header(htx, hdr, ist2(trash.area, trash.data))))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100678 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200679 }
680 }
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200681 else if (cli_conn && conn_get_src(cli_conn) && cli_conn->src->ss_family == AF_INET6) {
Christopher Faulet5d1def62021-02-26 09:19:15 +0100682 /* Add an X-Forwarded-For header unless the source IP is
683 * in the 'except' network range.
Christopher Faulete0768eb2018-10-03 16:38:02 +0200684 */
Christopher Faulet5d1def62021-02-26 09:19:15 +0100685 if (ipcmp2net(cli_conn->src, &sess->fe->except_xff_net) &&
686 ipcmp2net(cli_conn->src, &s->be->except_xff_net)) {
687 char pn[INET6_ADDRSTRLEN];
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200688
Christopher Faulet5d1def62021-02-26 09:19:15 +0100689 inet_ntop(AF_INET6,
690 (const void *)&((struct sockaddr_in6 *)(cli_conn->src))->sin6_addr,
691 pn, sizeof(pn));
Christopher Faulete0768eb2018-10-03 16:38:02 +0200692
Christopher Faulet5d1def62021-02-26 09:19:15 +0100693 /* Note: we rely on the backend to get the header name to be used for
694 * x-forwarded-for, because the header is really meant for the backends.
695 * However, if the backend did not specify any option, we have to rely
696 * on the frontend's header name.
697 */
698 chunk_printf(&trash, "%s", pn);
699 if (unlikely(!http_add_header(htx, hdr, ist2(trash.area, trash.data))))
700 goto return_int_err;
701 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200702 }
703 }
704
705 /*
706 * 10: add X-Original-To if either the frontend or the backend
707 * asks for it.
708 */
709 if ((sess->fe->options | s->be->options) & PR_O_ORGTO) {
Christopher Faulet5d1def62021-02-26 09:19:15 +0100710 struct ist hdr = ist2(s->be->orgto_hdr_len ? s->be->orgto_hdr_name : sess->fe->orgto_hdr_name,
711 s->be->orgto_hdr_len ? s->be->orgto_hdr_len : sess->fe->orgto_hdr_len);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200712
Christopher Fauletcccded92021-02-26 12:45:56 +0100713 if (cli_conn && conn_get_dst(cli_conn) && cli_conn->dst->ss_family == AF_INET) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200714 /* Add an X-Original-To header unless the destination IP is
715 * in the 'except' network range.
716 */
Christopher Faulet5d1def62021-02-26 09:19:15 +0100717 if (ipcmp2net(cli_conn->dst, &sess->fe->except_xot_net) &&
718 ipcmp2net(cli_conn->dst, &s->be->except_xot_net)) {
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200719 unsigned char *pn = (unsigned char *)&((struct sockaddr_in *)cli_conn->dst)->sin_addr;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200720
721 /* Note: we rely on the backend to get the header name to be used for
722 * x-original-to, because the header is really meant for the backends.
723 * However, if the backend did not specify any option, we have to rely
724 * on the frontend's header name.
725 */
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200726 chunk_printf(&trash, "%d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
727 if (unlikely(!http_add_header(htx, hdr, ist2(trash.area, trash.data))))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100728 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200729 }
730 }
Christopher Faulet5d1def62021-02-26 09:19:15 +0100731 else if (cli_conn && conn_get_dst(cli_conn) && cli_conn->dst->ss_family == AF_INET6) {
732 /* Add an X-Original-To header unless the source IP is
733 * in the 'except' network range.
734 */
735 if (ipcmp2net(cli_conn->dst, &sess->fe->except_xot_net) &&
736 ipcmp2net(cli_conn->dst, &s->be->except_xot_net)) {
737 char pn[INET6_ADDRSTRLEN];
738
739 inet_ntop(AF_INET6,
740 (const void *)&((struct sockaddr_in6 *)(cli_conn->dst))->sin6_addr,
741 pn, sizeof(pn));
742
743 /* Note: we rely on the backend to get the header name to be used for
744 * x-forwarded-for, because the header is really meant for the backends.
745 * However, if the backend did not specify any option, we have to rely
746 * on the frontend's header name.
747 */
748 chunk_printf(&trash, "%s", pn);
749 if (unlikely(!http_add_header(htx, hdr, ist2(trash.area, trash.data))))
750 goto return_int_err;
751 }
752 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200753 }
754
Christopher Fauletc2ac5e42021-03-08 18:20:09 +0100755 /* Filter the request headers if there are filters attached to the
756 * stream.
757 */
758 if (HAS_FILTERS(s))
759 req->analysers |= AN_REQ_FLT_HTTP_HDRS;
760
Christopher Faulete0768eb2018-10-03 16:38:02 +0200761 /* If we have no server assigned yet and we're balancing on url_param
762 * with a POST request, we may be interested in checking the body for
763 * that parameter. This will be done in another analyser.
764 */
765 if (!(s->flags & (SF_ASSIGNED|SF_DIRECT)) &&
Willy Tarreau089eaa02019-01-14 15:17:46 +0100766 s->txn->meth == HTTP_METH_POST &&
767 (s->be->lbprm.algo & BE_LB_ALGO) == BE_LB_ALGO_PH) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200768 channel_dont_connect(req);
769 req->analysers |= AN_REQ_HTTP_BODY;
770 }
771
772 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
773 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau1a18b542018-12-11 16:37:42 +0100774
Christopher Faulete0768eb2018-10-03 16:38:02 +0200775 /* We expect some data from the client. Unless we know for sure
776 * we already have a full request, we have to re-enable quick-ack
777 * in case we previously disabled it, otherwise we might cause
778 * the client to delay further data.
779 */
William Lallemand36119de2021-03-08 15:26:48 +0100780 if ((sess->listener && (sess->listener->options & LI_O_NOQUICKACK)) && !(htx->flags & HTX_FL_EOM))
Willy Tarreau1a18b542018-12-11 16:37:42 +0100781 conn_set_quickack(cli_conn, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200782
783 /*************************************************************
784 * OK, that's finished for the headers. We have done what we *
785 * could. Let's switch to the DATA state. *
786 ************************************************************/
787 req->analyse_exp = TICK_ETERNITY;
788 req->analysers &= ~an_bit;
789
790 s->logs.tv_request = now;
791 /* OK let's go on with the BODY now */
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100792 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200793 return 1;
794
Christopher Fauletb8a53712019-12-16 11:29:38 +0100795 return_int_err:
796 txn->status = 500;
797 if (!(s->flags & SF_ERR_MASK))
798 s->flags |= SF_ERR_INTERNAL;
Willy Tarreau4781b152021-04-06 13:53:36 +0200799 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100800 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau4781b152021-04-06 13:53:36 +0200801 _HA_ATOMIC_INC(&s->be->be_counters.internal_errors);
William Lallemand36119de2021-03-08 15:26:48 +0100802 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200803 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100804
Christopher Fauletb8a53712019-12-16 11:29:38 +0100805 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +0200806
807 if (!(s->flags & SF_ERR_MASK))
808 s->flags |= SF_ERR_PRXCOND;
809 if (!(s->flags & SF_FINST_MASK))
810 s->flags |= SF_FINST_R;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100811
812 req->analysers &= AN_REQ_FLT_END;
813 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100814 DBG_TRACE_DEVEL("leaving on error",
815 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200816 return 0;
817}
818
819/* This function is an analyser which processes the HTTP tarpit. It always
820 * returns zero, at the beginning because it prevents any other processing
821 * from occurring, and at the end because it terminates the request.
822 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200823int http_process_tarpit(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200824{
825 struct http_txn *txn = s->txn;
826
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100827 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, &txn->req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200828 /* This connection is being tarpitted. The CLIENT side has
829 * already set the connect expiration date to the right
830 * timeout. We just have to check that the client is still
831 * there and that the timeout has not expired.
832 */
833 channel_dont_connect(req);
834 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100835 !tick_is_expired(req->analyse_exp, now_ms)) {
836 DBG_TRACE_DEVEL("waiting for tarpit timeout expiry",
837 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200838 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100839 }
840
Christopher Faulete0768eb2018-10-03 16:38:02 +0200841
842 /* We will set the queue timer to the time spent, just for
843 * logging purposes. We fake a 500 server error, so that the
844 * attacker will not suspect his connection has been tarpitted.
845 * It will not cause trouble to the logs because we can exclude
846 * the tarpitted connections by filtering on the 'PT' status flags.
847 */
848 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
849
Christopher Faulet8dfeccf2020-05-15 14:16:29 +0200850 http_reply_and_close(s, txn->status, (!(req->flags & CF_READ_ERROR) ? http_error_message(s) : NULL));
Christopher Faulet5cb513a2020-05-13 17:56:56 +0200851
852 end:
Christopher Faulete0768eb2018-10-03 16:38:02 +0200853 req->analysers &= AN_REQ_FLT_END;
854 req->analyse_exp = TICK_ETERNITY;
855
856 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 }
1214 req->analysers &= AN_REQ_FLT_END;
1215 s->res.analysers &= AN_RES_FLT_END; /* we're in data phase, we want to abort both directions */
Christopher Fauletb8a53712019-12-16 11:29:38 +01001216 if (!(s->flags & SF_ERR_MASK))
1217 s->flags |= SF_ERR_PRXCOND;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001218 if (!(s->flags & SF_FINST_MASK))
1219 s->flags |= ((txn->rsp.msg_state < HTTP_MSG_ERROR) ? SF_FINST_H : SF_FINST_D);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001220 DBG_TRACE_DEVEL("leaving on error ",
1221 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001222 return 0;
1223}
1224
Olivier Houcharda254a372019-04-05 15:30:12 +02001225/* Reset the stream and the backend stream_interface to a situation suitable for attemption connection */
1226/* Returns 0 if we can attempt to retry, -1 otherwise */
1227static __inline int do_l7_retry(struct stream *s, struct stream_interface *si)
1228{
Christopher Faulet9f5382e2021-05-21 13:46:14 +02001229 struct channel *req, *res;
1230 int co_data;
Olivier Houcharda254a372019-04-05 15:30:12 +02001231
1232 si->conn_retries--;
1233 if (si->conn_retries < 0)
Christopher Faulet552601d2021-05-26 10:31:06 +02001234 return -1;
Christopher Faulet5b82cc52020-10-12 15:18:50 +02001235
Christopher Faulete763c8c2021-05-05 18:23:59 +02001236 if (objt_server(s->target)) {
1237 if (s->flags & SF_CURR_SESS) {
1238 s->flags &= ~SF_CURR_SESS;
1239 _HA_ATOMIC_DEC(&__objt_server(s->target)->cur_sess);
1240 }
Willy Tarreau4781b152021-04-06 13:53:36 +02001241 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.retries);
Christopher Faulete763c8c2021-05-05 18:23:59 +02001242 }
Willy Tarreau4781b152021-04-06 13:53:36 +02001243 _HA_ATOMIC_INC(&s->be->be_counters.retries);
Willy Tarreau223995e2019-05-04 10:38:31 +02001244
Christopher Faulet9f5382e2021-05-21 13:46:14 +02001245 req = &s->req;
1246 res = &s->res;
Olivier Houcharda254a372019-04-05 15:30:12 +02001247 /* Remove any write error from the request, and read error from the response */
1248 req->flags &= ~(CF_WRITE_ERROR | CF_WRITE_TIMEOUT | CF_SHUTW | CF_SHUTW_NOW);
1249 res->flags &= ~(CF_READ_ERROR | CF_READ_TIMEOUT | CF_SHUTR | CF_EOI | CF_READ_NULL | CF_SHUTR_NOW);
1250 res->analysers = 0;
1251 si->flags &= ~(SI_FL_ERR | SI_FL_EXP | SI_FL_RXBLK_SHUT);
Christopher Faulet30aa0da2021-05-05 21:05:09 +02001252 si->err_type = SI_ET_NONE;
1253 s->flags &= ~(SF_ERR_MASK | SF_FINST_MASK);
Olivier Houchard4bd58672019-07-12 16:16:59 +02001254 stream_choose_redispatch(s);
Olivier Houcharda254a372019-04-05 15:30:12 +02001255 si->exp = TICK_ETERNITY;
1256 res->rex = TICK_ETERNITY;
1257 res->to_forward = 0;
1258 res->analyse_exp = TICK_ETERNITY;
1259 res->total = 0;
Olivier Houcharda254a372019-04-05 15:30:12 +02001260 si_release_endpoint(&s->si[1]);
Olivier Houcharda254a372019-04-05 15:30:12 +02001261
Christopher Faulet9f5382e2021-05-21 13:46:14 +02001262 b_free(&req->buf);
1263 /* Swap the L7 buffer with the channel buffer */
1264 /* We know we stored the co_data as b_data, so get it there */
1265 co_data = b_data(&si->l7_buffer);
1266 b_set_data(&si->l7_buffer, b_size(&si->l7_buffer));
1267 b_xfer(&req->buf, &si->l7_buffer, b_data(&si->l7_buffer));
1268 co_set_data(req, co_data);
Christopher Faulet5b82cc52020-10-12 15:18:50 +02001269
Ilya Shipitsinacf84592021-02-06 22:29:08 +05001270 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 +02001271
Olivier Houcharda254a372019-04-05 15:30:12 +02001272 b_reset(&res->buf);
1273 co_set_data(res, 0);
1274 return 0;
1275}
1276
Christopher Faulete0768eb2018-10-03 16:38:02 +02001277/* This stream analyser waits for a complete HTTP response. It returns 1 if the
1278 * processing can continue on next analysers, or zero if it either needs more
1279 * data or wants to immediately abort the response (eg: timeout, error, ...). It
1280 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->res.analysers
1281 * when it has nothing left to do, and may remove any analyser when it wants to
1282 * abort.
1283 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001284int http_wait_for_response(struct stream *s, struct channel *rep, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001285{
Christopher Faulet9768c262018-10-22 09:34:31 +02001286 /*
1287 * We will analyze a complete HTTP response to check the its syntax.
1288 *
1289 * Once the start line and all headers are received, we may perform a
1290 * capture of the error (if any), and we will set a few fields. We also
1291 * logging and finally headers capture.
1292 */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001293 struct session *sess = s->sess;
1294 struct http_txn *txn = s->txn;
1295 struct http_msg *msg = &txn->rsp;
Christopher Faulet9768c262018-10-22 09:34:31 +02001296 struct htx *htx;
Olivier Houcharda254a372019-04-05 15:30:12 +02001297 struct stream_interface *si_b = &s->si[1];
Christopher Faulet61608322018-11-23 16:23:45 +01001298 struct connection *srv_conn;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001299 struct htx_sl *sl;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001300 int n;
1301
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001302 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001303
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001304 htx = htxbuf(&rep->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001305
Willy Tarreau4236f032019-03-05 10:43:32 +01001306 /* Parsing errors are caught here */
1307 if (htx->flags & HTX_FL_PARSING_ERROR)
1308 goto return_bad_res;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001309 if (htx->flags & HTX_FL_PROCESSING_ERROR)
1310 goto return_int_err;
Willy Tarreau4236f032019-03-05 10:43:32 +01001311
Christopher Faulete0768eb2018-10-03 16:38:02 +02001312 /*
1313 * Now we quickly check if we have found a full valid response.
1314 * If not so, we check the FD and buffer states before leaving.
1315 * A full response is indicated by the fact that we have seen
1316 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
1317 * responses are checked first.
1318 *
1319 * Depending on whether the client is still there or not, we
1320 * may send an error response back or not. Note that normally
1321 * we should only check for HTTP status there, and check I/O
1322 * errors somewhere else.
1323 */
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001324 next_one:
Christopher Faulet29f17582019-05-23 11:03:26 +02001325 if (unlikely(htx_is_empty(htx) || htx->first == -1)) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001326 /* 1: have we encountered a read error ? */
1327 if (rep->flags & CF_READ_ERROR) {
Olivier Houchard865d8392019-05-03 22:46:27 +02001328 struct connection *conn = NULL;
1329
Olivier Houchard865d8392019-05-03 22:46:27 +02001330 if (objt_cs(s->si[1].end))
1331 conn = objt_cs(s->si[1].end)->conn;
1332
Christopher Fauletd9769232021-05-26 12:15:37 +02001333 /* Perform a L7 retry because server refuses the early data. */
1334 if ((si_b->flags & SI_FL_L7_RETRY) &&
1335 (s->be->retry_type & PR_RE_EARLY_ERROR) &&
1336 conn && conn->err_code == CO_ER_SSL_EARLY_FAILED &&
1337 do_l7_retry(s, si_b) == 0) {
1338 DBG_TRACE_DEVEL("leaving on L7 retry",
1339 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
1340 return 0;
1341 }
1342
Olivier Houchard6db16992019-05-17 15:40:49 +02001343 if (txn->flags & TX_NOT_FIRST)
1344 goto abort_keep_alive;
1345
Willy Tarreau4781b152021-04-06 13:53:36 +02001346 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001347 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001348 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001349 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001350 }
1351
Christopher Fauletd9769232021-05-26 12:15:37 +02001352 /* if the server refused the early data, just send a 425 */
1353 if (conn && conn->err_code == CO_ER_SSL_EARLY_FAILED)
Olivier Houchard865d8392019-05-03 22:46:27 +02001354 txn->status = 425;
Christopher Fauletd9769232021-05-26 12:15:37 +02001355 else {
1356 txn->status = 502;
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001357 stream_inc_http_fail_ctr(s);
Christopher Fauletd9769232021-05-26 12:15:37 +02001358 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001359
Christopher Fauletd9769232021-05-26 12:15:37 +02001360 rep->analysers &= AN_RES_FLT_END;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001361 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001362 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001363
1364 if (!(s->flags & SF_ERR_MASK))
1365 s->flags |= SF_ERR_SRVCL;
1366 if (!(s->flags & SF_FINST_MASK))
1367 s->flags |= SF_FINST_H;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001368 DBG_TRACE_DEVEL("leaving on error",
1369 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001370 return 0;
1371 }
1372
Christopher Faulet9768c262018-10-22 09:34:31 +02001373 /* 2: read timeout : return a 504 to the client. */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001374 else if (rep->flags & CF_READ_TIMEOUT) {
Olivier Houcharda254a372019-04-05 15:30:12 +02001375 if ((si_b->flags & SI_FL_L7_RETRY) &&
1376 (s->be->retry_type & PR_RE_TIMEOUT)) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001377 if (co_data(rep) || do_l7_retry(s, si_b) == 0) {
1378 DBG_TRACE_DEVEL("leaving on L7 retry",
1379 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharda254a372019-04-05 15:30:12 +02001380 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001381 }
Olivier Houcharda254a372019-04-05 15:30:12 +02001382 }
Willy Tarreau4781b152021-04-06 13:53:36 +02001383 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001384 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001385 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001386 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001387 }
1388
Christopher Faulete0768eb2018-10-03 16:38:02 +02001389 rep->analysers &= AN_RES_FLT_END;
1390 txn->status = 504;
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001391 stream_inc_http_fail_ctr(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001392 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001393 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001394
1395 if (!(s->flags & SF_ERR_MASK))
1396 s->flags |= SF_ERR_SRVTO;
1397 if (!(s->flags & SF_FINST_MASK))
1398 s->flags |= SF_FINST_H;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001399 DBG_TRACE_DEVEL("leaving on error",
1400 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001401 return 0;
1402 }
1403
Christopher Faulet9768c262018-10-22 09:34:31 +02001404 /* 3: client abort with an abortonclose */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001405 else if ((rep->flags & CF_SHUTR) && ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001406 _HA_ATOMIC_INC(&sess->fe->fe_counters.cli_aborts);
1407 _HA_ATOMIC_INC(&s->be->be_counters.cli_aborts);
William Lallemand36119de2021-03-08 15:26:48 +01001408 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001409 _HA_ATOMIC_INC(&sess->listener->counters->cli_aborts);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001410 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001411 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.cli_aborts);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001412
1413 rep->analysers &= AN_RES_FLT_END;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001414 txn->status = 400;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001415 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001416
1417 if (!(s->flags & SF_ERR_MASK))
1418 s->flags |= SF_ERR_CLICL;
1419 if (!(s->flags & SF_FINST_MASK))
1420 s->flags |= SF_FINST_H;
1421
1422 /* process_stream() will take care of the error */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001423 DBG_TRACE_DEVEL("leaving on error",
1424 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001425 return 0;
1426 }
1427
Christopher Faulet9768c262018-10-22 09:34:31 +02001428 /* 4: close from server, capture the response if the server has started to respond */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001429 else if (rep->flags & CF_SHUTR) {
Olivier Houcharda254a372019-04-05 15:30:12 +02001430 if ((si_b->flags & SI_FL_L7_RETRY) &&
1431 (s->be->retry_type & PR_RE_DISCONNECTED)) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001432 if (co_data(rep) || do_l7_retry(s, si_b) == 0) {
1433 DBG_TRACE_DEVEL("leaving on L7 retry",
1434 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharda254a372019-04-05 15:30:12 +02001435 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001436 }
Olivier Houcharda254a372019-04-05 15:30:12 +02001437 }
1438
Olivier Houchard6db16992019-05-17 15:40:49 +02001439 if (txn->flags & TX_NOT_FIRST)
1440 goto abort_keep_alive;
1441
Willy Tarreau4781b152021-04-06 13:53:36 +02001442 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001443 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001444 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001445 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001446 }
1447
Christopher Faulete0768eb2018-10-03 16:38:02 +02001448 rep->analysers &= AN_RES_FLT_END;
1449 txn->status = 502;
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001450 stream_inc_http_fail_ctr(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001451 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001452 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001453
1454 if (!(s->flags & SF_ERR_MASK))
1455 s->flags |= SF_ERR_SRVCL;
1456 if (!(s->flags & SF_FINST_MASK))
1457 s->flags |= SF_FINST_H;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001458 DBG_TRACE_DEVEL("leaving on error",
1459 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001460 return 0;
1461 }
1462
Christopher Faulet9768c262018-10-22 09:34:31 +02001463 /* 5: write error to client (we don't send any message then) */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001464 else if (rep->flags & CF_WRITE_ERROR) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001465 if (txn->flags & TX_NOT_FIRST)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001466 goto abort_keep_alive;
1467
Willy Tarreau4781b152021-04-06 13:53:36 +02001468 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001469 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001470 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001471 rep->analysers &= AN_RES_FLT_END;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001472
1473 if (!(s->flags & SF_ERR_MASK))
1474 s->flags |= SF_ERR_CLICL;
1475 if (!(s->flags & SF_FINST_MASK))
1476 s->flags |= SF_FINST_H;
1477
1478 /* process_stream() will take care of the error */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001479 DBG_TRACE_DEVEL("leaving on error",
1480 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001481 return 0;
1482 }
1483
1484 channel_dont_close(rep);
1485 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001486 DBG_TRACE_DEVEL("waiting for more data",
1487 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001488 return 0;
1489 }
1490
1491 /* More interesting part now : we know that we have a complete
1492 * response which at least looks like HTTP. We have an indicator
1493 * of each header's length, so we can parse them quickly.
1494 */
Christopher Faulet29f17582019-05-23 11:03:26 +02001495 BUG_ON(htx_get_first_type(htx) != HTX_BLK_RES_SL);
Christopher Faulet297fbb42019-05-13 14:41:27 +02001496 sl = http_get_stline(htx);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001497
Christopher Faulet1f08bff2021-05-26 13:14:39 +02001498 /* Perform a L7 retry because of the status code */
1499 if ((si_b->flags & SI_FL_L7_RETRY) &&
1500 l7_status_match(s->be, sl->info.res.status) &&
1501 do_l7_retry(s, si_b) == 0) {
1502 DBG_TRACE_DEVEL("leaving on L7 retry", STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
1503 return 0;
1504 }
1505
1506 /* Now, L7 buffer is useless, it can be released */
1507 b_free(&s->si[1].l7_buffer);
1508
1509 msg->msg_state = HTTP_MSG_BODY;
1510
1511
Christopher Faulet9768c262018-10-22 09:34:31 +02001512 /* 0: we might have to print this header in debug mode */
1513 if (unlikely((global.mode & MODE_DEBUG) &&
1514 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
1515 int32_t pos;
1516
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001517 http_debug_stline("srvrep", s, sl);
Christopher Faulet9768c262018-10-22 09:34:31 +02001518
Christopher Fauleta3f15502019-05-13 15:27:23 +02001519 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001520 struct htx_blk *blk = htx_get_blk(htx, pos);
1521 enum htx_blk_type type = htx_get_blk_type(blk);
1522
1523 if (type == HTX_BLK_EOH)
1524 break;
1525 if (type != HTX_BLK_HDR)
1526 continue;
1527
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001528 http_debug_hdr("srvhdr", s,
1529 htx_get_blk_name(htx, blk),
1530 htx_get_blk_value(htx, blk));
Christopher Faulet9768c262018-10-22 09:34:31 +02001531 }
1532 }
1533
Christopher Faulet03599112018-11-27 11:21:21 +01001534 /* 1: get the status code and the version. Also set HTTP flags */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001535 txn->status = sl->info.res.status;
Christopher Faulet03599112018-11-27 11:21:21 +01001536 if (sl->flags & HTX_SL_F_VER_11)
Christopher Faulet9768c262018-10-22 09:34:31 +02001537 msg->flags |= HTTP_MSGF_VER_11;
Christopher Faulet03599112018-11-27 11:21:21 +01001538 if (sl->flags & HTX_SL_F_XFER_LEN) {
1539 msg->flags |= HTTP_MSGF_XFER_LEN;
Christopher Faulet2a408542020-11-20 14:22:37 +01001540 if (sl->flags & HTX_SL_F_CLEN)
1541 msg->flags |= HTTP_MSGF_CNT_LEN;
1542 else if (sl->flags & HTX_SL_F_CHNK)
1543 msg->flags |= HTTP_MSGF_TE_CHNK;
Christopher Faulet03599112018-11-27 11:21:21 +01001544 }
Christopher Faulet2a408542020-11-20 14:22:37 +01001545 if (sl->flags & HTX_SL_F_BODYLESS)
1546 msg->flags |= HTTP_MSGF_BODYLESS;
Christopher Faulet576c3582021-01-08 15:53:01 +01001547 if (sl->flags & HTX_SL_F_CONN_UPG)
1548 msg->flags |= HTTP_MSGF_CONN_UPG;
Christopher Faulet9768c262018-10-22 09:34:31 +02001549
1550 n = txn->status / 100;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001551 if (n < 1 || n > 5)
1552 n = 0;
Christopher Faulet9768c262018-10-22 09:34:31 +02001553
Christopher Faulete0768eb2018-10-03 16:38:02 +02001554 /* when the client triggers a 4xx from the server, it's most often due
1555 * to a missing object or permission. These events should be tracked
1556 * because if they happen often, it may indicate a brute force or a
1557 * vulnerability scan.
1558 */
1559 if (n == 4)
1560 stream_inc_http_err_ctr(s);
1561
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001562 if (n == 5 && txn->status != 501 && txn->status != 505)
1563 stream_inc_http_fail_ctr(s);
1564
Marcin Deranek3c27dda2020-05-15 18:32:51 +02001565 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001566 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.p.http.rsp[n]);
1567 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.p.http.cum_req);
Marcin Deranek3c27dda2020-05-15 18:32:51 +02001568 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001569
Christopher Faulete0768eb2018-10-03 16:38:02 +02001570 /* Adjust server's health based on status code. Note: status codes 501
1571 * and 505 are triggered on demand by client request, so we must not
1572 * count them as server failures.
1573 */
1574 if (objt_server(s->target)) {
1575 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001576 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_OK);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001577 else
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001578 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_STS);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001579 }
1580
1581 /*
1582 * We may be facing a 100-continue response, or any other informational
1583 * 1xx response which is non-final, in which case this is not the right
1584 * response, and we're waiting for the next one. Let's allow this response
1585 * to go to the client and wait for the next one. There's an exception for
1586 * 101 which is used later in the code to switch protocols.
1587 */
1588 if (txn->status < 200 &&
1589 (txn->status == 100 || txn->status >= 102)) {
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001590 FLT_STRM_CB(s, flt_http_reset(s, msg));
Christopher Faulet421e7692019-06-13 11:16:45 +02001591 htx->first = channel_htx_fwd_headers(rep, htx);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001592 msg->msg_state = HTTP_MSG_RPBEFORE;
Christopher Faulet3499f622019-09-03 15:23:54 +02001593 msg->flags = 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001594 txn->status = 0;
1595 s->logs.t_data = -1; /* was not a response yet */
Christopher Faulet7d518452020-08-31 11:07:07 +02001596 rep->flags |= CF_SEND_DONTWAIT; /* Send ASAP informational messages */
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001597 goto next_one;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001598 }
1599
Christopher Faulet6e6c7b12021-01-08 16:02:05 +01001600 /* A 101-switching-protocols must contains a Connection header with the
1601 * "upgrade" option and the request too. It means both are agree to
1602 * upgrade. It is not so strict because there is no test on the Upgrade
1603 * header content. But it is probably stronger enough for now.
1604 */
1605 if (txn->status == 101 &&
1606 (!(txn->req.flags & HTTP_MSGF_CONN_UPG) || !(txn->rsp.flags & HTTP_MSGF_CONN_UPG)))
1607 goto return_bad_res;
1608
Christopher Faulete0768eb2018-10-03 16:38:02 +02001609 /*
1610 * 2: check for cacheability.
1611 */
1612
1613 switch (txn->status) {
1614 case 200:
1615 case 203:
1616 case 204:
1617 case 206:
1618 case 300:
1619 case 301:
1620 case 404:
1621 case 405:
1622 case 410:
1623 case 414:
1624 case 501:
1625 break;
1626 default:
1627 /* RFC7231#6.1:
1628 * Responses with status codes that are defined as
1629 * cacheable by default (e.g., 200, 203, 204, 206,
1630 * 300, 301, 404, 405, 410, 414, and 501 in this
1631 * specification) can be reused by a cache with
1632 * heuristic expiration unless otherwise indicated
1633 * by the method definition or explicit cache
1634 * controls [RFC7234]; all other status codes are
1635 * not cacheable by default.
1636 */
1637 txn->flags &= ~(TX_CACHEABLE | TX_CACHE_COOK);
1638 break;
1639 }
1640
1641 /*
1642 * 3: we may need to capture headers
1643 */
1644 s->logs.logwait &= ~LW_RESP;
1645 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->res_cap))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001646 http_capture_headers(htx, s->res_cap, sess->fe->rsp_cap);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001647
Christopher Faulet9768c262018-10-22 09:34:31 +02001648 /* Skip parsing if no content length is possible. */
Christopher Fauletc75668e2020-12-07 18:10:32 +01001649 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status >= 200 && txn->status < 300) ||
Christopher Faulete0768eb2018-10-03 16:38:02 +02001650 txn->status == 101)) {
1651 /* Either we've established an explicit tunnel, or we're
1652 * switching the protocol. In both cases, we're very unlikely
1653 * to understand the next protocols. We have to switch to tunnel
1654 * mode, so that we transfer the request and responses then let
1655 * this protocol pass unmodified. When we later implement specific
1656 * parsers for such protocols, we'll want to check the Upgrade
1657 * header which contains information about that protocol for
1658 * responses with status 101 (eg: see RFC2817 about TLS).
1659 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02001660 txn->flags |= TX_CON_WANT_TUN;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001661 }
1662
Christopher Faulet61608322018-11-23 16:23:45 +01001663 /* check for NTML authentication headers in 401 (WWW-Authenticate) and
1664 * 407 (Proxy-Authenticate) responses and set the connection to private
1665 */
1666 srv_conn = cs_conn(objt_cs(s->si[1].end));
1667 if (srv_conn) {
1668 struct ist hdr;
1669 struct http_hdr_ctx ctx;
1670
1671 if (txn->status == 401)
1672 hdr = ist("WWW-Authenticate");
1673 else if (txn->status == 407)
1674 hdr = ist("Proxy-Authenticate");
1675 else
1676 goto end;
1677
1678 ctx.blk = NULL;
1679 while (http_find_header(htx, hdr, &ctx, 0)) {
Willy Tarreauf1dcced2020-05-07 19:27:02 +02001680 /* If www-authenticate contains "Negotiate", "Nego2", or "NTLM",
1681 * possibly followed by blanks and a base64 string, the connection
1682 * is private. Since it's a mess to deal with, we only check for
1683 * values starting with "NTLM" or "Nego". Note that often multiple
1684 * headers are sent by the server there.
1685 */
1686 if ((ctx.value.len >= 4 && strncasecmp(ctx.value.ptr, "Nego", 4) == 0) ||
Willy Tarreau49a1d282020-05-07 19:10:15 +02001687 (ctx.value.len >= 4 && strncasecmp(ctx.value.ptr, "NTLM", 4) == 0)) {
Olivier Houchard250031e2019-05-29 15:01:50 +02001688 sess->flags |= SESS_FL_PREFER_LAST;
Christopher Faulet08016ab2020-07-01 16:10:06 +02001689 conn_set_owner(srv_conn, sess, NULL);
Christopher Faulet21ddc742020-07-01 15:26:14 +02001690 conn_set_private(srv_conn);
Ilya Shipitsin6b79f382020-07-23 00:32:55 +05001691 /* If it fail now, the same will be done in mux->detach() callback */
Christopher Faulet08016ab2020-07-01 16:10:06 +02001692 session_add_conn(srv_conn->owner, srv_conn, srv_conn->target);
Willy Tarreauf1dcced2020-05-07 19:27:02 +02001693 break;
Olivier Houchard250031e2019-05-29 15:01:50 +02001694 }
Christopher Faulet61608322018-11-23 16:23:45 +01001695 }
1696 }
1697
1698 end:
Christopher Faulete0768eb2018-10-03 16:38:02 +02001699 /* we want to have the response time before we start processing it */
1700 s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now);
1701
1702 /* end of job, return OK */
1703 rep->analysers &= ~an_bit;
1704 rep->analyse_exp = TICK_ETERNITY;
1705 channel_auto_close(rep);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001706 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001707 return 1;
1708
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001709 return_int_err:
Willy Tarreau4781b152021-04-06 13:53:36 +02001710 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
1711 _HA_ATOMIC_INC(&s->be->be_counters.internal_errors);
William Lallemand36119de2021-03-08 15:26:48 +01001712 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001713 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Fauletb8a53712019-12-16 11:29:38 +01001714 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001715 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.internal_errors);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001716 txn->status = 500;
1717 if (!(s->flags & SF_ERR_MASK))
1718 s->flags |= SF_ERR_INTERNAL;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001719 goto return_prx_cond;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001720
1721 return_bad_res:
Willy Tarreau4781b152021-04-06 13:53:36 +02001722 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Faulet47365272018-10-31 17:40:50 +01001723 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001724 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001725 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Christopher Faulet47365272018-10-31 17:40:50 +01001726 }
Olivier Houcharde3249a92019-05-03 23:01:47 +02001727 if ((s->be->retry_type & PR_RE_JUNK_REQUEST) &&
Olivier Houchardad26d8d2019-05-10 17:48:28 +02001728 (si_b->flags & SI_FL_L7_RETRY) &&
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001729 do_l7_retry(s, si_b) == 0) {
1730 DBG_TRACE_DEVEL("leaving on L7 retry",
1731 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharde3249a92019-05-03 23:01:47 +02001732 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001733 }
Christopher Faulet47365272018-10-31 17:40:50 +01001734 txn->status = 502;
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001735 stream_inc_http_fail_ctr(s);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001736 /* fall through */
1737
Christopher Fauletb8a53712019-12-16 11:29:38 +01001738 return_prx_cond:
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001739 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet47365272018-10-31 17:40:50 +01001740
1741 if (!(s->flags & SF_ERR_MASK))
1742 s->flags |= SF_ERR_PRXCOND;
1743 if (!(s->flags & SF_FINST_MASK))
1744 s->flags |= SF_FINST_H;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001745
1746 s->si[1].flags |= SI_FL_NOLINGER;
1747 rep->analysers &= AN_RES_FLT_END;
Christopher Faulete58c0002020-03-02 16:21:01 +01001748 s->req.analysers &= AN_REQ_FLT_END;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001749 rep->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001750 DBG_TRACE_DEVEL("leaving on error",
1751 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulet47365272018-10-31 17:40:50 +01001752 return 0;
1753
Christopher Faulete0768eb2018-10-03 16:38:02 +02001754 abort_keep_alive:
1755 /* A keep-alive request to the server failed on a network error.
1756 * The client is required to retry. We need to close without returning
1757 * any other information so that the client retries.
1758 */
1759 txn->status = 0;
1760 rep->analysers &= AN_RES_FLT_END;
1761 s->req.analysers &= AN_REQ_FLT_END;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001762 s->logs.logwait = 0;
1763 s->logs.level = 0;
1764 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001765 http_reply_and_close(s, txn->status, NULL);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001766 DBG_TRACE_DEVEL("leaving by closing K/A connection",
1767 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001768 return 0;
1769}
1770
1771/* This function performs all the processing enabled for the current response.
1772 * It normally returns 1 unless it wants to break. It relies on buffers flags,
1773 * and updates s->res.analysers. It might make sense to explode it into several
1774 * other functions. It works like process_request (see indications above).
1775 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001776int http_process_res_common(struct stream *s, struct channel *rep, int an_bit, struct proxy *px)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001777{
1778 struct session *sess = s->sess;
1779 struct http_txn *txn = s->txn;
1780 struct http_msg *msg = &txn->rsp;
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001781 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001782 struct proxy *cur_proxy;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001783 enum rule_result ret = HTTP_RULE_RES_CONT;
1784
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001785 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
1786 return 0;
Christopher Faulet9768c262018-10-22 09:34:31 +02001787
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001788 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001789
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001790 htx = htxbuf(&rep->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001791
1792 /* The stats applet needs to adjust the Connection header but we don't
1793 * apply any filter there.
1794 */
1795 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
1796 rep->analysers &= ~an_bit;
1797 rep->analyse_exp = TICK_ETERNITY;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001798 goto end;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001799 }
1800
1801 /*
1802 * We will have to evaluate the filters.
1803 * As opposed to version 1.2, now they will be evaluated in the
1804 * filters order and not in the header order. This means that
1805 * each filter has to be validated among all headers.
1806 *
1807 * Filters are tried with ->be first, then with ->fe if it is
1808 * different from ->be.
1809 *
1810 * Maybe we are in resume condiion. In this case I choose the
1811 * "struct proxy" which contains the rule list matching the resume
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05001812 * pointer. If none of these "struct proxy" match, I initialise
Christopher Faulete0768eb2018-10-03 16:38:02 +02001813 * the process with the first one.
1814 *
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05001815 * In fact, I check only correspondence between the current list
Christopher Faulete0768eb2018-10-03 16:38:02 +02001816 * pointer and the ->fe rule list. If it doesn't match, I initialize
1817 * the loop with the ->be.
1818 */
1819 if (s->current_rule_list == &sess->fe->http_res_rules)
1820 cur_proxy = sess->fe;
1821 else
1822 cur_proxy = s->be;
1823 while (1) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001824 /* evaluate http-response rules */
1825 if (ret == HTTP_RULE_RES_CONT) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001826 ret = http_res_get_intercept_rule(cur_proxy, &cur_proxy->http_res_rules, s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001827
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001828 switch (ret) {
1829 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
1830 goto return_prx_yield;
1831
1832 case HTTP_RULE_RES_CONT:
1833 case HTTP_RULE_RES_STOP: /* nothing to do */
1834 break;
1835
1836 case HTTP_RULE_RES_DENY: /* deny or tarpit */
1837 goto deny;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001838
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001839 case HTTP_RULE_RES_ABRT: /* abort request, response already sent */
1840 goto return_prx_cond;
1841
1842 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
Christopher Fauletb8a53712019-12-16 11:29:38 +01001843 goto done;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001844
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001845 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
1846 goto return_bad_res;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001847
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001848 case HTTP_RULE_RES_ERROR: /* failed with a bad request */
1849 goto return_int_err;
1850 }
1851
1852 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001853
Christopher Faulete0768eb2018-10-03 16:38:02 +02001854 /* check whether we're already working on the frontend */
1855 if (cur_proxy == sess->fe)
1856 break;
1857 cur_proxy = sess->fe;
1858 }
1859
Christopher Faulete0768eb2018-10-03 16:38:02 +02001860 /* OK that's all we can do for 1xx responses */
1861 if (unlikely(txn->status < 200 && txn->status != 101))
Christopher Fauletf2824e62018-10-01 12:12:37 +02001862 goto end;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001863
1864 /*
1865 * Now check for a server cookie.
1866 */
1867 if (s->be->cookie_name || sess->fe->capture_name || (s->be->options & PR_O_CHK_CACHE))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001868 http_manage_server_side_cookies(s, rep);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001869
1870 /*
1871 * Check for cache-control or pragma headers if required.
1872 */
1873 if ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001874 http_check_response_for_cacheability(s, rep);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001875
1876 /*
1877 * Add server cookie in the response if needed
1878 */
1879 if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) &&
1880 !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) &&
1881 (!(s->flags & SF_DIRECT) ||
1882 ((s->be->cookie_maxidle || txn->cookie_last_date) &&
1883 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
1884 (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
1885 (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
1886 (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
1887 !(s->flags & SF_IGNORE_PRST)) {
1888 /* the server is known, it's not the one the client requested, or the
1889 * cookie's last seen date needs to be refreshed. We have to
1890 * insert a set-cookie here, except if we want to insert only on POST
1891 * requests and this one isn't. Note that servers which don't have cookies
1892 * (eg: some backup servers) will return a full cookie removal request.
1893 */
1894 if (!objt_server(s->target)->cookie) {
1895 chunk_printf(&trash,
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001896 "%s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
Christopher Faulete0768eb2018-10-03 16:38:02 +02001897 s->be->cookie_name);
1898 }
1899 else {
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001900 chunk_printf(&trash, "%s=%s", s->be->cookie_name, objt_server(s->target)->cookie);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001901
1902 if (s->be->cookie_maxidle || s->be->cookie_maxlife) {
1903 /* emit last_date, which is mandatory */
1904 trash.area[trash.data++] = COOKIE_DELIM_DATE;
1905 s30tob64((date.tv_sec+3) >> 2,
1906 trash.area + trash.data);
1907 trash.data += 5;
1908
1909 if (s->be->cookie_maxlife) {
1910 /* emit first_date, which is either the original one or
1911 * the current date.
1912 */
1913 trash.area[trash.data++] = COOKIE_DELIM_DATE;
1914 s30tob64(txn->cookie_first_date ?
1915 txn->cookie_first_date >> 2 :
1916 (date.tv_sec+3) >> 2,
1917 trash.area + trash.data);
1918 trash.data += 5;
1919 }
1920 }
1921 chunk_appendf(&trash, "; path=/");
1922 }
1923
1924 if (s->be->cookie_domain)
1925 chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain);
1926
1927 if (s->be->ck_opts & PR_CK_HTTPONLY)
1928 chunk_appendf(&trash, "; HttpOnly");
1929
1930 if (s->be->ck_opts & PR_CK_SECURE)
1931 chunk_appendf(&trash, "; Secure");
1932
Christopher Faulet2f533902020-01-21 11:06:48 +01001933 if (s->be->cookie_attrs)
1934 chunk_appendf(&trash, "; %s", s->be->cookie_attrs);
1935
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001936 if (unlikely(!http_add_header(htx, ist("Set-Cookie"), ist2(trash.area, trash.data))))
Christopher Fauletb8a53712019-12-16 11:29:38 +01001937 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001938
1939 txn->flags &= ~TX_SCK_MASK;
1940 if (__objt_server(s->target)->cookie && (s->flags & SF_DIRECT))
1941 /* the server did not change, only the date was updated */
1942 txn->flags |= TX_SCK_UPDATED;
1943 else
1944 txn->flags |= TX_SCK_INSERTED;
1945
1946 /* Here, we will tell an eventual cache on the client side that we don't
1947 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
1948 * Some caches understand the correct form: 'no-cache="set-cookie"', but
1949 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
1950 */
1951 if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
1952
1953 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
1954
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001955 if (unlikely(!http_add_header(htx, ist("Cache-control"), ist("private"))))
Christopher Fauletb8a53712019-12-16 11:29:38 +01001956 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001957 }
1958 }
1959
1960 /*
1961 * Check if result will be cacheable with a cookie.
1962 * We'll block the response if security checks have caught
1963 * nasty things such as a cacheable cookie.
1964 */
1965 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
1966 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
1967 (s->be->options & PR_O_CHK_CACHE)) {
1968 /* we're in presence of a cacheable response containing
1969 * a set-cookie header. We'll block it as requested by
1970 * the 'checkcache' option, and send an alert.
1971 */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001972 ha_alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
1973 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
1974 send_log(s->be, LOG_ALERT,
1975 "Blocking cacheable cookie in response from instance %s, server %s.\n",
1976 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
Christopher Fauletb8a53712019-12-16 11:29:38 +01001977 goto deny;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001978 }
1979
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001980 end:
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01001981 /*
1982 * Evaluate after-response rules before forwarding the response. rules
1983 * from the backend are evaluated first, then one from the frontend if
1984 * it differs.
1985 */
1986 if (!http_eval_after_res_rules(s))
1987 goto return_int_err;
1988
Christopher Fauletc2ac5e42021-03-08 18:20:09 +01001989 /* Filter the response headers if there are filters attached to the
1990 * stream.
1991 */
1992 if (HAS_FILTERS(s))
1993 rep->analysers |= AN_RES_FLT_HTTP_HDRS;
1994
Christopher Faulete0768eb2018-10-03 16:38:02 +02001995 /* Always enter in the body analyzer */
1996 rep->analysers &= ~AN_RES_FLT_XFER_DATA;
1997 rep->analysers |= AN_RES_HTTP_XFER_BODY;
1998
1999 /* if the user wants to log as soon as possible, without counting
2000 * bytes from the server, then this is the right moment. We have
2001 * to temporarily assign bytes_out to log what we currently have.
2002 */
2003 if (!LIST_ISEMPTY(&sess->fe->logformat) && !(s->logs.logwait & LW_BYTES)) {
2004 s->logs.t_close = s->logs.t_data; /* to get a valid end date */
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002005 s->logs.bytes_out = htx->data;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002006 s->do_log(s);
2007 s->logs.bytes_out = 0;
2008 }
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002009
Christopher Fauletb8a53712019-12-16 11:29:38 +01002010 done:
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002011 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002012 rep->analysers &= ~an_bit;
2013 rep->analyse_exp = TICK_ETERNITY;
2014 return 1;
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002015
Christopher Fauletb8a53712019-12-16 11:29:38 +01002016 deny:
Willy Tarreau4781b152021-04-06 13:53:36 +02002017 _HA_ATOMIC_INC(&sess->fe->fe_counters.denied_resp);
2018 _HA_ATOMIC_INC(&s->be->be_counters.denied_resp);
William Lallemand36119de2021-03-08 15:26:48 +01002019 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02002020 _HA_ATOMIC_INC(&sess->listener->counters->denied_resp);
Christopher Fauleta08546b2019-12-16 16:07:34 +01002021 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02002022 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.denied_resp);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002023 goto return_prx_err;
2024
2025 return_int_err:
2026 txn->status = 500;
2027 if (!(s->flags & SF_ERR_MASK))
2028 s->flags |= SF_ERR_INTERNAL;
Willy Tarreau4781b152021-04-06 13:53:36 +02002029 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
2030 _HA_ATOMIC_INC(&s->be->be_counters.internal_errors);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002031 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02002032 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.internal_errors);
Christopher Fauletcff0f732019-12-16 16:13:44 +01002033 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02002034 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.internal_errors);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002035 goto return_prx_err;
2036
2037 return_bad_res:
2038 txn->status = 502;
Willy Tarreau826f3ab2021-02-10 12:07:15 +01002039 stream_inc_http_fail_ctr(s);
Willy Tarreau4781b152021-04-06 13:53:36 +02002040 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Fauleta20a6532020-02-05 10:16:41 +01002041 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02002042 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Christopher Fauleta20a6532020-02-05 10:16:41 +01002043 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_RSP);
2044 }
Christopher Fauletb8a53712019-12-16 11:29:38 +01002045 /* fall through */
2046
2047 return_prx_err:
2048 http_reply_and_close(s, txn->status, http_error_message(s));
2049 /* fall through */
2050
2051 return_prx_cond:
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002052 s->logs.t_data = -1; /* was not a valid response */
2053 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002054
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002055 if (!(s->flags & SF_ERR_MASK))
2056 s->flags |= SF_ERR_PRXCOND;
2057 if (!(s->flags & SF_FINST_MASK))
2058 s->flags |= SF_FINST_H;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002059
Christopher Faulete58c0002020-03-02 16:21:01 +01002060 rep->analysers &= AN_RES_FLT_END;
2061 s->req.analysers &= AN_REQ_FLT_END;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002062 rep->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002063 DBG_TRACE_DEVEL("leaving on error",
2064 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002065 return 0;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002066
2067 return_prx_yield:
2068 channel_dont_close(rep);
2069 DBG_TRACE_DEVEL("waiting for more data",
2070 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
2071 return 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002072}
2073
2074/* This function is an analyser which forwards response body (including chunk
2075 * sizes if any). It is called as soon as we must forward, even if we forward
2076 * zero byte. The only situation where it must not be called is when we're in
2077 * tunnel mode and we want to forward till the close. It's used both to forward
2078 * remaining data and to resync after end of body. It expects the msg_state to
2079 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
2080 * read more data, or 1 once we can go on with next request or end the stream.
2081 *
2082 * It is capable of compressing response data both in content-length mode and
2083 * in chunked mode. The state machines follows different flows depending on
2084 * whether content-length and chunked modes are used, since there are no
2085 * trailers in content-length :
2086 *
2087 * chk-mode cl-mode
2088 * ,----- BODY -----.
2089 * / \
2090 * V size > 0 V chk-mode
2091 * .--> SIZE -------------> DATA -------------> CRLF
2092 * | | size == 0 | last byte |
2093 * | v final crlf v inspected |
2094 * | TRAILERS -----------> DONE |
2095 * | |
2096 * `----------------------------------------------'
2097 *
2098 * Compression only happens in the DATA state, and must be flushed in final
2099 * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding
2100 * is performed at once on final states for all bytes parsed, or when leaving
2101 * on missing data.
2102 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002103int http_response_forward_body(struct stream *s, struct channel *res, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +02002104{
2105 struct session *sess = s->sess;
2106 struct http_txn *txn = s->txn;
2107 struct http_msg *msg = &s->txn->rsp;
Christopher Faulet9768c262018-10-22 09:34:31 +02002108 struct htx *htx;
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002109 int ret;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002110
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002111 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002112
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002113 htx = htxbuf(&res->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002114
Christopher Fauletb9a92f32019-09-09 10:15:21 +02002115 if (htx->flags & HTX_FL_PARSING_ERROR)
2116 goto return_bad_res;
2117 if (htx->flags & HTX_FL_PROCESSING_ERROR)
2118 goto return_int_err;
2119
Christopher Faulete0768eb2018-10-03 16:38:02 +02002120 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Christopher Fauletf2824e62018-10-01 12:12:37 +02002121 ((res->flags & CF_SHUTW) && (res->to_forward || co_data(res)))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02002122 /* Output closed while we were sending data. We must abort and
2123 * wake the other side up.
2124 */
Christopher Faulete0768eb2018-10-03 16:38:02 +02002125 msg->msg_state = HTTP_MSG_ERROR;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002126 http_end_response(s);
2127 http_end_request(s);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002128 DBG_TRACE_DEVEL("leaving on error",
2129 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002130 return 1;
2131 }
2132
Christopher Faulet9768c262018-10-22 09:34:31 +02002133 if (msg->msg_state == HTTP_MSG_BODY)
2134 msg->msg_state = HTTP_MSG_DATA;
2135
Christopher Faulete0768eb2018-10-03 16:38:02 +02002136 /* in most states, we should abort in case of early close */
2137 channel_auto_close(res);
2138
Christopher Faulete0768eb2018-10-03 16:38:02 +02002139 if (res->to_forward) {
Christopher Faulet66af0b22019-03-22 14:54:52 +01002140 if (res->to_forward == CHN_INFINITE_FORWARD) {
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002141 if (res->flags & CF_EOI)
2142 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet66af0b22019-03-22 14:54:52 +01002143 }
2144 else {
2145 /* We can't process the buffer's contents yet */
2146 res->flags |= CF_WAKE_WRITE;
2147 goto missing_data_or_waiting;
2148 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02002149 }
2150
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002151 if (msg->msg_state >= HTTP_MSG_ENDING)
2152 goto ending;
2153
Christopher Fauletc75668e2020-12-07 18:10:32 +01002154 if ((txn->meth == HTTP_METH_CONNECT && txn->status >= 200 && txn->status < 300) || txn->status == 101 ||
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002155 (!(msg->flags & HTTP_MSGF_XFER_LEN) && !HAS_RSP_DATA_FILTERS(s))) {
2156 msg->msg_state = HTTP_MSG_ENDING;
2157 goto ending;
2158 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002159
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002160 /* Forward input data. We get it by removing all outgoing data not
2161 * forwarded yet from HTX data size. If there are some data filters, we
2162 * let them decide the amount of data to forward.
Christopher Faulet9768c262018-10-22 09:34:31 +02002163 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002164 if (HAS_RSP_DATA_FILTERS(s)) {
2165 ret = flt_http_payload(s, msg, htx->data);
2166 if (ret < 0)
2167 goto return_bad_res;
Christopher Faulet421e7692019-06-13 11:16:45 +02002168 c_adv(res, ret);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002169 }
2170 else {
Christopher Faulet421e7692019-06-13 11:16:45 +02002171 c_adv(res, htx->data - co_data(res));
Christopher Faulet66af0b22019-03-22 14:54:52 +01002172 if (msg->flags & HTTP_MSGF_XFER_LEN)
2173 channel_htx_forward_forever(res, htx);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002174 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002175
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002176 if (htx->data != co_data(res))
2177 goto missing_data_or_waiting;
2178
2179 if (!(msg->flags & HTTP_MSGF_XFER_LEN) && res->flags & CF_SHUTR) {
2180 msg->msg_state = HTTP_MSG_ENDING;
2181 goto ending;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002182 }
2183
Christopher Faulet9768c262018-10-22 09:34:31 +02002184 /* Check if the end-of-message is reached and if so, switch the message
Christopher Fauletd20fdb02019-06-13 16:43:22 +02002185 * in HTTP_MSG_ENDING state. Then if all data was marked to be
2186 * forwarded, set the state to HTTP_MSG_DONE.
Christopher Faulet9768c262018-10-22 09:34:31 +02002187 */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002188 if (!(htx->flags & HTX_FL_EOM))
Christopher Faulet9768c262018-10-22 09:34:31 +02002189 goto missing_data_or_waiting;
2190
Christopher Fauletd20fdb02019-06-13 16:43:22 +02002191 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet9768c262018-10-22 09:34:31 +02002192
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002193 ending:
Christopher Faulet2151cdd2020-07-22 16:34:59 +02002194 res->flags &= ~CF_EXPECT_MORE; /* no more data are expected */
2195
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002196 /* other states, ENDING...TUNNEL */
2197 if (msg->msg_state >= HTTP_MSG_DONE)
2198 goto done;
Christopher Faulet9768c262018-10-22 09:34:31 +02002199
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002200 if (HAS_RSP_DATA_FILTERS(s)) {
2201 ret = flt_http_end(s, msg);
2202 if (ret <= 0) {
2203 if (!ret)
2204 goto missing_data_or_waiting;
2205 goto return_bad_res;
2206 }
2207 }
2208
Christopher Fauletc75668e2020-12-07 18:10:32 +01002209 if ((txn->meth == HTTP_METH_CONNECT && txn->status >= 200 && txn->status < 300) || txn->status == 101 ||
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002210 !(msg->flags & HTTP_MSGF_XFER_LEN)) {
2211 msg->msg_state = HTTP_MSG_TUNNEL;
2212 goto ending;
2213 }
2214 else {
2215 msg->msg_state = HTTP_MSG_DONE;
2216 res->to_forward = 0;
2217 }
2218
2219 done:
2220
2221 channel_dont_close(res);
2222
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002223 http_end_response(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002224 if (!(res->analysers & an_bit)) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002225 http_end_request(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002226 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
2227 if (res->flags & CF_SHUTW) {
2228 /* response errors are most likely due to the
2229 * client aborting the transfer. */
Christopher Faulet93e02d82019-03-08 14:18:50 +01002230 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002231 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02002232 goto return_bad_res;
2233 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002234 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002235 return 1;
2236 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002237 DBG_TRACE_DEVEL("waiting for the end of the HTTP txn",
2238 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002239 return 0;
2240
2241 missing_data_or_waiting:
2242 if (res->flags & CF_SHUTW)
Christopher Faulet93e02d82019-03-08 14:18:50 +01002243 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002244
2245 /* stop waiting for data if the input is closed before the end. If the
2246 * client side was already closed, it means that the client has aborted,
2247 * so we don't want to count this as a server abort. Otherwise it's a
2248 * server abort.
2249 */
Christopher Fauletd20fdb02019-06-13 16:43:22 +02002250 if (msg->msg_state < HTTP_MSG_ENDING && res->flags & CF_SHUTR) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02002251 if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))
Christopher Faulet93e02d82019-03-08 14:18:50 +01002252 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002253 /* If we have some pending data, we continue the processing */
Christopher Faulet93e02d82019-03-08 14:18:50 +01002254 if (htx_is_empty(htx))
2255 goto return_srv_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002256 }
2257
Christopher Faulete0768eb2018-10-03 16:38:02 +02002258 /* When TE: chunked is used, we need to get there again to parse
2259 * remaining chunks even if the server has closed, so we don't want to
Christopher Faulet9768c262018-10-22 09:34:31 +02002260 * set CF_DONTCLOSE. Similarly when there is a content-leng or if there
2261 * are filters registered on the stream, we don't want to forward a
2262 * close
Christopher Faulete0768eb2018-10-03 16:38:02 +02002263 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002264 if ((msg->flags & HTTP_MSGF_XFER_LEN) || HAS_RSP_DATA_FILTERS(s))
Christopher Faulete0768eb2018-10-03 16:38:02 +02002265 channel_dont_close(res);
2266
2267 /* We know that more data are expected, but we couldn't send more that
2268 * what we did. So we always set the CF_EXPECT_MORE flag so that the
2269 * system knows it must not set a PUSH on this first part. Interactive
2270 * modes are already handled by the stream sock layer. We must not do
2271 * this in content-length mode because it could present the MSG_MORE
2272 * flag with the last block of forwarded data, which would cause an
2273 * additional delay to be observed by the receiver.
2274 */
Christopher Faulet2151cdd2020-07-22 16:34:59 +02002275 if (HAS_RSP_DATA_FILTERS(s))
Christopher Faulete0768eb2018-10-03 16:38:02 +02002276 res->flags |= CF_EXPECT_MORE;
2277
2278 /* the stream handler will take care of timeouts and errors */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002279 DBG_TRACE_DEVEL("waiting for more data to forward",
2280 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002281 return 0;
2282
Christopher Faulet93e02d82019-03-08 14:18:50 +01002283 return_srv_abort:
Willy Tarreau4781b152021-04-06 13:53:36 +02002284 _HA_ATOMIC_INC(&sess->fe->fe_counters.srv_aborts);
2285 _HA_ATOMIC_INC(&s->be->be_counters.srv_aborts);
William Lallemand36119de2021-03-08 15:26:48 +01002286 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02002287 _HA_ATOMIC_INC(&sess->listener->counters->srv_aborts);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002288 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02002289 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.srv_aborts);
Willy Tarreau826f3ab2021-02-10 12:07:15 +01002290 stream_inc_http_fail_ctr(s);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002291 if (!(s->flags & SF_ERR_MASK))
2292 s->flags |= SF_ERR_SRVCL;
2293 goto return_error;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002294
Christopher Faulet93e02d82019-03-08 14:18:50 +01002295 return_cli_abort:
Willy Tarreau4781b152021-04-06 13:53:36 +02002296 _HA_ATOMIC_INC(&sess->fe->fe_counters.cli_aborts);
2297 _HA_ATOMIC_INC(&s->be->be_counters.cli_aborts);
William Lallemand36119de2021-03-08 15:26:48 +01002298 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02002299 _HA_ATOMIC_INC(&sess->listener->counters->cli_aborts);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002300 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02002301 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.cli_aborts);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002302 if (!(s->flags & SF_ERR_MASK))
2303 s->flags |= SF_ERR_CLICL;
2304 goto return_error;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002305
Christopher Fauletb9a92f32019-09-09 10:15:21 +02002306 return_int_err:
Willy Tarreau4781b152021-04-06 13:53:36 +02002307 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
2308 _HA_ATOMIC_INC(&s->be->be_counters.internal_errors);
William Lallemand36119de2021-03-08 15:26:48 +01002309 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02002310 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002311 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02002312 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.internal_errors);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02002313 if (!(s->flags & SF_ERR_MASK))
2314 s->flags |= SF_ERR_INTERNAL;
2315 goto return_error;
2316
Christopher Faulet93e02d82019-03-08 14:18:50 +01002317 return_bad_res:
Willy Tarreau4781b152021-04-06 13:53:36 +02002318 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002319 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02002320 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002321 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_RSP);
2322 }
Willy Tarreau826f3ab2021-02-10 12:07:15 +01002323 stream_inc_http_fail_ctr(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002324 if (!(s->flags & SF_ERR_MASK))
Christopher Faulet93e02d82019-03-08 14:18:50 +01002325 s->flags |= SF_ERR_SRVCL;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002326 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +02002327
Christopher Faulet93e02d82019-03-08 14:18:50 +01002328 return_error:
Christopher Faulete0768eb2018-10-03 16:38:02 +02002329 /* don't send any error message as we're in the body */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002330 http_reply_and_close(s, txn->status, NULL);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002331 res->analysers &= AN_RES_FLT_END;
2332 s->req.analysers &= AN_REQ_FLT_END; /* we're in data phase, we want to abort both directions */
Christopher Faulete0768eb2018-10-03 16:38:02 +02002333 if (!(s->flags & SF_FINST_MASK))
2334 s->flags |= SF_FINST_D;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002335 DBG_TRACE_DEVEL("leaving on error",
2336 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002337 return 0;
2338}
2339
Christopher Fauletf2824e62018-10-01 12:12:37 +02002340/* Perform an HTTP redirect based on the information in <rule>. The function
Willy Tarreaubc1223b2021-09-02 16:54:33 +02002341 * returns zero in case of an irrecoverable error such as too large a request
2342 * to build a valid response, 1 in case of successful redirect (hence the rule
2343 * is final), or 2 if the rule has to be silently skipped.
Christopher Fauletf2824e62018-10-01 12:12:37 +02002344 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002345int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn)
Christopher Fauletf2824e62018-10-01 12:12:37 +02002346{
Christopher Faulet99daf282018-11-28 22:58:13 +01002347 struct channel *req = &s->req;
2348 struct channel *res = &s->res;
2349 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01002350 struct htx_sl *sl;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002351 struct buffer *chunk;
Christopher Faulet99daf282018-11-28 22:58:13 +01002352 struct ist status, reason, location;
2353 unsigned int flags;
Christopher Faulet08e66462019-05-23 16:44:59 +02002354 int close = 0; /* Try to keep the connection alive byt default */
Christopher Fauletf2824e62018-10-01 12:12:37 +02002355
2356 chunk = alloc_trash_chunk();
Christopher Fauletb8a53712019-12-16 11:29:38 +01002357 if (!chunk) {
2358 if (!(s->flags & SF_ERR_MASK))
2359 s->flags |= SF_ERR_RESOURCE;
Christopher Faulet99daf282018-11-28 22:58:13 +01002360 goto fail;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002361 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002362
Christopher Faulet99daf282018-11-28 22:58:13 +01002363 /*
2364 * Create the location
2365 */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002366 htx = htxbuf(&req->buf);
Christopher Fauletf2824e62018-10-01 12:12:37 +02002367 switch(rule->type) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002368 case REDIRECT_TYPE_SCHEME: {
2369 struct http_hdr_ctx ctx;
2370 struct ist path, host;
Amaury Denoyellec453f952021-07-06 11:40:12 +02002371 struct http_uri_parser parser;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002372
Christopher Faulet99daf282018-11-28 22:58:13 +01002373 host = ist("");
2374 ctx.blk = NULL;
2375 if (http_find_header(htx, ist("Host"), &ctx, 0))
2376 host = ctx.value;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002377
Christopher Faulet297fbb42019-05-13 14:41:27 +02002378 sl = http_get_stline(htx);
Amaury Denoyellec453f952021-07-06 11:40:12 +02002379 parser = http_uri_parser_init(htx_sl_req_uri(sl));
2380 path = http_parse_path(&parser);
Christopher Faulet99daf282018-11-28 22:58:13 +01002381 /* build message using path */
Tim Duesterhused526372020-03-05 17:56:33 +01002382 if (isttest(path)) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002383 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2384 int qs = 0;
2385 while (qs < path.len) {
2386 if (*(path.ptr + qs) == '?') {
2387 path.len = qs;
2388 break;
2389 }
2390 qs++;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002391 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002392 }
2393 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002394 else
2395 path = ist("/");
Christopher Fauletf2824e62018-10-01 12:12:37 +02002396
Christopher Faulet99daf282018-11-28 22:58:13 +01002397 if (rule->rdr_str) { /* this is an old "redirect" rule */
2398 /* add scheme */
2399 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2400 goto fail;
2401 }
2402 else {
2403 /* add scheme with executing log format */
2404 chunk->data += build_logline(s, chunk->area + chunk->data,
2405 chunk->size - chunk->data,
2406 &rule->rdr_fmt);
2407 }
2408 /* add "://" + host + path */
2409 if (!chunk_memcat(chunk, "://", 3) ||
2410 !chunk_memcat(chunk, host.ptr, host.len) ||
2411 !chunk_memcat(chunk, path.ptr, path.len))
2412 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002413
Christopher Faulet99daf282018-11-28 22:58:13 +01002414 /* append a slash at the end of the location if needed and missing */
2415 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
2416 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
2417 if (chunk->data + 1 >= chunk->size)
2418 goto fail;
2419 chunk->area[chunk->data++] = '/';
2420 }
2421 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002422 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002423
Christopher Faulet99daf282018-11-28 22:58:13 +01002424 case REDIRECT_TYPE_PREFIX: {
2425 struct ist path;
Amaury Denoyellec453f952021-07-06 11:40:12 +02002426 struct http_uri_parser parser;
Christopher Faulet99daf282018-11-28 22:58:13 +01002427
Christopher Faulet297fbb42019-05-13 14:41:27 +02002428 sl = http_get_stline(htx);
Amaury Denoyellec453f952021-07-06 11:40:12 +02002429 parser = http_uri_parser_init(htx_sl_req_uri(sl));
2430 path = http_parse_path(&parser);
Christopher Faulet99daf282018-11-28 22:58:13 +01002431 /* build message using path */
Tim Duesterhused526372020-03-05 17:56:33 +01002432 if (isttest(path)) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002433 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2434 int qs = 0;
2435 while (qs < path.len) {
2436 if (*(path.ptr + qs) == '?') {
2437 path.len = qs;
2438 break;
2439 }
2440 qs++;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002441 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002442 }
2443 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002444 else
2445 path = ist("/");
Christopher Fauletf2824e62018-10-01 12:12:37 +02002446
Christopher Faulet99daf282018-11-28 22:58:13 +01002447 if (rule->rdr_str) { /* this is an old "redirect" rule */
2448 /* add prefix. Note that if prefix == "/", we don't want to
2449 * add anything, otherwise it makes it hard for the user to
2450 * configure a self-redirection.
2451 */
2452 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
2453 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2454 goto fail;
2455 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002456 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002457 else {
2458 /* add prefix with executing log format */
2459 chunk->data += build_logline(s, chunk->area + chunk->data,
2460 chunk->size - chunk->data,
2461 &rule->rdr_fmt);
2462 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002463
Christopher Faulet99daf282018-11-28 22:58:13 +01002464 /* add path */
2465 if (!chunk_memcat(chunk, path.ptr, path.len))
2466 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002467
Christopher Faulet99daf282018-11-28 22:58:13 +01002468 /* append a slash at the end of the location if needed and missing */
2469 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
2470 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
2471 if (chunk->data + 1 >= chunk->size)
2472 goto fail;
2473 chunk->area[chunk->data++] = '/';
2474 }
2475 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002476 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002477 case REDIRECT_TYPE_LOCATION:
2478 default:
2479 if (rule->rdr_str) { /* this is an old "redirect" rule */
2480 /* add location */
2481 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2482 goto fail;
2483 }
2484 else {
2485 /* add location with executing log format */
Willy Tarreaubc1223b2021-09-02 16:54:33 +02002486 int len = build_logline(s, chunk->area + chunk->data,
2487 chunk->size - chunk->data,
2488 &rule->rdr_fmt);
2489 if (!len && rule->flags & REDIRECT_FLAG_IGNORE_EMPTY)
2490 return 2;
2491
2492 chunk->data += len;
Christopher Faulet99daf282018-11-28 22:58:13 +01002493 }
2494 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002495 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002496 location = ist2(chunk->area, chunk->data);
2497
2498 /*
2499 * Create the 30x response
2500 */
2501 switch (rule->code) {
2502 case 308:
2503 status = ist("308");
2504 reason = ist("Permanent Redirect");
2505 break;
2506 case 307:
2507 status = ist("307");
2508 reason = ist("Temporary Redirect");
2509 break;
2510 case 303:
2511 status = ist("303");
2512 reason = ist("See Other");
2513 break;
2514 case 301:
2515 status = ist("301");
2516 reason = ist("Moved Permanently");
2517 break;
2518 case 302:
2519 default:
2520 status = ist("302");
2521 reason = ist("Found");
2522 break;
2523 }
2524
Christopher Faulet08e66462019-05-23 16:44:59 +02002525 if (!(txn->req.flags & HTTP_MSGF_BODYLESS) && txn->req.msg_state != HTTP_MSG_DONE)
2526 close = 1;
2527
Christopher Faulet99daf282018-11-28 22:58:13 +01002528 htx = htx_from_buf(&res->buf);
Kevin Zhu96b36392020-01-07 09:42:55 +01002529 /* Trim any possible response */
2530 channel_htx_truncate(&s->res, htx);
Christopher Faulet99daf282018-11-28 22:58:13 +01002531 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
2532 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), status, reason);
2533 if (!sl)
2534 goto fail;
2535 sl->info.res.status = rule->code;
2536 s->txn->status = rule->code;
2537
Christopher Faulet08e66462019-05-23 16:44:59 +02002538 if (close && !htx_add_header(htx, ist("Connection"), ist("close")))
2539 goto fail;
2540
2541 if (!htx_add_header(htx, ist("Content-length"), ist("0")) ||
Christopher Faulet99daf282018-11-28 22:58:13 +01002542 !htx_add_header(htx, ist("Location"), location))
2543 goto fail;
2544
2545 if (rule->code == 302 || rule->code == 303 || rule->code == 307) {
2546 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")))
2547 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002548 }
2549
2550 if (rule->cookie_len) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002551 if (!htx_add_header(htx, ist("Set-Cookie"), ist2(rule->cookie_str, rule->cookie_len)))
2552 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002553 }
2554
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002555 if (!htx_add_endof(htx, HTX_BLK_EOH))
Christopher Faulet99daf282018-11-28 22:58:13 +01002556 goto fail;
2557
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002558 htx->flags |= HTX_FL_EOM;
Kevin Zhu96b36392020-01-07 09:42:55 +01002559 htx_to_buf(htx, &res->buf);
Christopher Fauleta72a7e42020-01-28 09:28:11 +01002560 if (!http_forward_proxy_resp(s, 1))
2561 goto fail;
Christopher Faulet99daf282018-11-28 22:58:13 +01002562
Christopher Faulet60b33a52020-01-28 09:18:10 +01002563 if (rule->flags & REDIRECT_FLAG_FROM_REQ) {
2564 /* let's log the request time */
2565 s->logs.tv_request = now;
2566 req->analysers &= AN_REQ_FLT_END;
Christopher Faulet99daf282018-11-28 22:58:13 +01002567
Christopher Faulet60b33a52020-01-28 09:18:10 +01002568 if (s->sess->fe == s->be) /* report it if the request was intercepted by the frontend */
Willy Tarreau4781b152021-04-06 13:53:36 +02002569 _HA_ATOMIC_INC(&s->sess->fe->fe_counters.intercepted_req);
Christopher Faulet60b33a52020-01-28 09:18:10 +01002570 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002571
2572 if (!(s->flags & SF_ERR_MASK))
2573 s->flags |= SF_ERR_LOCAL;
2574 if (!(s->flags & SF_FINST_MASK))
Christopher Faulet60b33a52020-01-28 09:18:10 +01002575 s->flags |= ((rule->flags & REDIRECT_FLAG_FROM_REQ) ? SF_FINST_R : SF_FINST_H);
Christopher Fauletf2824e62018-10-01 12:12:37 +02002576
Christopher Faulet99daf282018-11-28 22:58:13 +01002577 free_trash_chunk(chunk);
2578 return 1;
2579
2580 fail:
2581 /* If an error occurred, remove the incomplete HTTP response from the
2582 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01002583 channel_htx_truncate(res, htxbuf(&res->buf));
Christopher Fauletf2824e62018-10-01 12:12:37 +02002584 free_trash_chunk(chunk);
Christopher Faulet99daf282018-11-28 22:58:13 +01002585 return 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002586}
2587
Christopher Faulet92d34fe2019-12-17 09:20:34 +01002588/* Replace all headers matching the name <name>. The header value is replaced if
2589 * it matches the regex <re>. <str> is used for the replacement. If <full> is
2590 * set to 1, the full-line is matched and replaced. Otherwise, comma-separated
2591 * values are evaluated one by one. It returns 0 on success and -1 on error.
2592 */
2593int http_replace_hdrs(struct stream* s, struct htx *htx, struct ist name,
2594 const char *str, struct my_regex *re, int full)
Christopher Faulet72333522018-10-24 11:25:02 +02002595{
2596 struct http_hdr_ctx ctx;
2597 struct buffer *output = get_trash_chunk();
2598
Christopher Faulet72333522018-10-24 11:25:02 +02002599 ctx.blk = NULL;
Christopher Faulet92d34fe2019-12-17 09:20:34 +01002600 while (http_find_header(htx, name, &ctx, full)) {
Christopher Faulet72333522018-10-24 11:25:02 +02002601 if (!regex_exec_match2(re, ctx.value.ptr, ctx.value.len, MAX_MATCH, pmatch, 0))
2602 continue;
2603
2604 output->data = exp_replace(output->area, output->size, ctx.value.ptr, str, pmatch);
2605 if (output->data == -1)
2606 return -1;
2607 if (!http_replace_header_value(htx, &ctx, ist2(output->area, output->data)))
2608 return -1;
2609 }
2610 return 0;
2611}
2612
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002613/* This function executes one of the set-{method,path,query,uri} actions. It
2614 * takes the string from the variable 'replace' with length 'len', then modifies
2615 * the relevant part of the request line accordingly. Then it updates various
2616 * pointers to the next elements which were moved, and the total buffer length.
2617 * It finds the action to be performed in p[2], previously filled by function
2618 * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal
2619 * error, though this can be revisited when this code is finally exploited.
2620 *
2621 * 'action' can be '0' to replace method, '1' to replace path, '2' to replace
Christopher Faulet312294f2020-09-02 17:17:44 +02002622 * query string, 3 to replace uri or 4 to replace the path+query.
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002623 *
2624 * In query string case, the mark question '?' must be set at the start of the
2625 * string by the caller, event if the replacement query string is empty.
2626 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002627int http_req_replace_stline(int action, const char *replace, int len,
2628 struct proxy *px, struct stream *s)
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002629{
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002630 struct htx *htx = htxbuf(&s->req.buf);
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002631
2632 switch (action) {
2633 case 0: // method
2634 if (!http_replace_req_meth(htx, ist2(replace, len)))
2635 return -1;
2636 break;
2637
2638 case 1: // path
Christopher Fauletb8ce5052020-08-31 16:11:57 +02002639 if (!http_replace_req_path(htx, ist2(replace, len), 0))
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002640 return -1;
2641 break;
2642
2643 case 2: // query
2644 if (!http_replace_req_query(htx, ist2(replace, len)))
2645 return -1;
2646 break;
2647
2648 case 3: // uri
2649 if (!http_replace_req_uri(htx, ist2(replace, len)))
2650 return -1;
2651 break;
2652
Christopher Faulet312294f2020-09-02 17:17:44 +02002653 case 4: // path + query
2654 if (!http_replace_req_path(htx, ist2(replace, len), 1))
2655 return -1;
2656 break;
2657
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002658 default:
2659 return -1;
2660 }
2661 return 0;
2662}
2663
2664/* This function replace the HTTP status code and the associated message. The
Christopher Faulete00d06c2019-12-16 17:18:42 +01002665 * variable <status> contains the new status code. This function never fails. It
2666 * returns 0 in case of success, -1 in case of internal error.
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002667 */
Christopher Faulet96bff762019-12-17 13:46:18 +01002668int http_res_set_status(unsigned int status, struct ist reason, struct stream *s)
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002669{
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002670 struct htx *htx = htxbuf(&s->res.buf);
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002671 char *res;
2672
2673 chunk_reset(&trash);
2674 res = ultoa_o(status, trash.area, trash.size);
2675 trash.data = res - trash.area;
2676
2677 /* Do we have a custom reason format string? */
Tim Duesterhuse296d3e2020-03-05 17:56:31 +01002678 if (!isttest(reason)) {
Christopher Faulet96bff762019-12-17 13:46:18 +01002679 const char *str = http_get_reason(status);
Tim Duesterhusdcf753a2021-03-04 17:31:47 +01002680 reason = ist(str);
Christopher Faulet96bff762019-12-17 13:46:18 +01002681 }
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002682
Christopher Fauletbde2c4c2020-08-31 16:43:34 +02002683 if (!http_replace_res_status(htx, ist2(trash.area, trash.data), reason))
Christopher Faulete00d06c2019-12-16 17:18:42 +01002684 return -1;
2685 return 0;
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002686}
2687
Christopher Faulet3e964192018-10-24 11:39:23 +02002688/* Executes the http-request rules <rules> for stream <s>, proxy <px> and
2689 * transaction <txn>. Returns the verdict of the first rule that prevents
2690 * further processing of the request (auth, deny, ...), and defaults to
2691 * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or
2692 * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT
2693 * on txn->flags if it encounters a tarpit rule. If <deny_status> is not NULL
2694 * and a deny/tarpit rule is matched, it will be filled with this rule's deny
2695 * status.
2696 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002697static enum rule_result http_req_get_intercept_rule(struct proxy *px, struct list *rules,
Christopher Fauletb58f62b2020-01-13 16:40:13 +01002698 struct stream *s)
Christopher Faulet3e964192018-10-24 11:39:23 +02002699{
2700 struct session *sess = strm_sess(s);
2701 struct http_txn *txn = s->txn;
Christopher Faulet3e964192018-10-24 11:39:23 +02002702 struct act_rule *rule;
Christopher Faulet3e964192018-10-24 11:39:23 +02002703 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002704 int act_opts = 0;
Christopher Faulet3e964192018-10-24 11:39:23 +02002705
Christopher Faulet3e964192018-10-24 11:39:23 +02002706 /* If "the current_rule_list" match the executed rule list, we are in
2707 * resume condition. If a resume is needed it is always in the action
2708 * and never in the ACL or converters. In this case, we initialise the
2709 * current rule, and go to the action execution point.
2710 */
2711 if (s->current_rule) {
2712 rule = s->current_rule;
2713 s->current_rule = NULL;
2714 if (s->current_rule_list == rules)
2715 goto resume_execution;
2716 }
2717 s->current_rule_list = rules;
2718
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002719 /* start the ruleset evaluation in strict mode */
2720 txn->req.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01002721
Christopher Faulet3e964192018-10-24 11:39:23 +02002722 list_for_each_entry(rule, rules, list) {
2723 /* check optional condition */
2724 if (rule->cond) {
2725 int ret;
2726
2727 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
2728 ret = acl_pass(ret);
2729
2730 if (rule->cond->pol == ACL_COND_UNLESS)
2731 ret = !ret;
2732
2733 if (!ret) /* condition not matched */
2734 continue;
2735 }
2736
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002737 act_opts |= ACT_OPT_FIRST;
Christopher Faulet3e964192018-10-24 11:39:23 +02002738 resume_execution:
Amaury Denoyelle03517732021-05-07 14:25:01 +02002739 if (rule->kw->flags & KWF_EXPERIMENTAL)
2740 mark_tainted(TAINTED_ACTION_EXP_EXECUTED);
2741
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002742 /* Always call the action function if defined */
2743 if (rule->action_ptr) {
2744 if ((s->req.flags & CF_READ_ERROR) ||
2745 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
2746 (px->options & PR_O_ABRT_CLOSE)))
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002747 act_opts |= ACT_OPT_FINAL;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002748
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002749 switch (rule->action_ptr(rule, px, sess, s, act_opts)) {
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002750 case ACT_RET_CONT:
2751 break;
2752 case ACT_RET_STOP:
2753 rule_ret = HTTP_RULE_RES_STOP;
2754 goto end;
2755 case ACT_RET_YIELD:
2756 s->current_rule = rule;
2757 rule_ret = HTTP_RULE_RES_YIELD;
2758 goto end;
2759 case ACT_RET_ERR:
2760 rule_ret = HTTP_RULE_RES_ERROR;
2761 goto end;
2762 case ACT_RET_DONE:
2763 rule_ret = HTTP_RULE_RES_DONE;
2764 goto end;
2765 case ACT_RET_DENY:
Christopher Fauletb58f62b2020-01-13 16:40:13 +01002766 if (txn->status == -1)
2767 txn->status = 403;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002768 rule_ret = HTTP_RULE_RES_DENY;
2769 goto end;
2770 case ACT_RET_ABRT:
2771 rule_ret = HTTP_RULE_RES_ABRT;
2772 goto end;
2773 case ACT_RET_INV:
2774 rule_ret = HTTP_RULE_RES_BADREQ;
2775 goto end;
2776 }
2777 continue; /* eval the next rule */
2778 }
2779
2780 /* If not action function defined, check for known actions */
Christopher Faulet3e964192018-10-24 11:39:23 +02002781 switch (rule->action) {
2782 case ACT_ACTION_ALLOW:
2783 rule_ret = HTTP_RULE_RES_STOP;
2784 goto end;
2785
2786 case ACT_ACTION_DENY:
Christopher Faulet5cb513a2020-05-13 17:56:56 +02002787 txn->status = rule->arg.http_reply->status;
2788 txn->http_reply = rule->arg.http_reply;
Christopher Faulet3e964192018-10-24 11:39:23 +02002789 rule_ret = HTTP_RULE_RES_DENY;
2790 goto end;
2791
2792 case ACT_HTTP_REQ_TARPIT:
2793 txn->flags |= TX_CLTARPIT;
Christopher Faulet5cb513a2020-05-13 17:56:56 +02002794 txn->status = rule->arg.http_reply->status;
2795 txn->http_reply = rule->arg.http_reply;
Christopher Faulet3e964192018-10-24 11:39:23 +02002796 rule_ret = HTTP_RULE_RES_DENY;
2797 goto end;
2798
Willy Tarreaubc1223b2021-09-02 16:54:33 +02002799 case ACT_HTTP_REDIR: {
2800 int ret = http_apply_redirect_rule(rule->arg.redir, s, txn);
2801
2802 if (ret == 2) // 2 == skip
2803 break;
2804
2805 rule_ret = ret ? HTTP_RULE_RES_ABRT : HTTP_RULE_RES_ERROR;
Christopher Faulet3e964192018-10-24 11:39:23 +02002806 goto end;
Willy Tarreaubc1223b2021-09-02 16:54:33 +02002807 }
Christopher Faulet3e964192018-10-24 11:39:23 +02002808
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002809 /* other flags exists, but normally, they never be matched. */
Christopher Faulet3e964192018-10-24 11:39:23 +02002810 default:
2811 break;
2812 }
2813 }
2814
2815 end:
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002816 /* if the ruleset evaluation is finished reset the strict mode */
Christopher Faulet46f95542019-12-20 10:07:22 +01002817 if (rule_ret != HTTP_RULE_RES_YIELD)
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002818 txn->req.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01002819
Christopher Faulet3e964192018-10-24 11:39:23 +02002820 /* we reached the end of the rules, nothing to report */
2821 return rule_ret;
2822}
2823
2824/* Executes the http-response rules <rules> for stream <s> and proxy <px>. It
2825 * returns one of 5 possible statuses: HTTP_RULE_RES_CONT, HTTP_RULE_RES_STOP,
2826 * HTTP_RULE_RES_DONE, HTTP_RULE_RES_YIELD, or HTTP_RULE_RES_BADREQ. If *CONT
2827 * is returned, the process can continue the evaluation of next rule list. If
2828 * *STOP or *DONE is returned, the process must stop the evaluation. If *BADREQ
2829 * is returned, it means the operation could not be processed and a server error
Christopher Fauleta53abad2020-05-13 08:12:22 +02002830 * must be returned. If *YIELD is returned, the caller must call again the
2831 * function with the same context.
Christopher Faulet3e964192018-10-24 11:39:23 +02002832 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002833static enum rule_result http_res_get_intercept_rule(struct proxy *px, struct list *rules,
2834 struct stream *s)
Christopher Faulet3e964192018-10-24 11:39:23 +02002835{
2836 struct session *sess = strm_sess(s);
2837 struct http_txn *txn = s->txn;
Christopher Faulet3e964192018-10-24 11:39:23 +02002838 struct act_rule *rule;
Christopher Faulet3e964192018-10-24 11:39:23 +02002839 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002840 int act_opts = 0;
Christopher Faulet3e964192018-10-24 11:39:23 +02002841
Christopher Faulet3e964192018-10-24 11:39:23 +02002842 /* If "the current_rule_list" match the executed rule list, we are in
2843 * resume condition. If a resume is needed it is always in the action
2844 * and never in the ACL or converters. In this case, we initialise the
2845 * current rule, and go to the action execution point.
2846 */
2847 if (s->current_rule) {
2848 rule = s->current_rule;
2849 s->current_rule = NULL;
2850 if (s->current_rule_list == rules)
2851 goto resume_execution;
2852 }
2853 s->current_rule_list = rules;
2854
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002855 /* start the ruleset evaluation in strict mode */
2856 txn->rsp.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01002857
Christopher Faulet3e964192018-10-24 11:39:23 +02002858 list_for_each_entry(rule, rules, list) {
2859 /* check optional condition */
2860 if (rule->cond) {
2861 int ret;
2862
2863 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
2864 ret = acl_pass(ret);
2865
2866 if (rule->cond->pol == ACL_COND_UNLESS)
2867 ret = !ret;
2868
2869 if (!ret) /* condition not matched */
2870 continue;
2871 }
2872
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002873 act_opts |= ACT_OPT_FIRST;
Christopher Faulet3e964192018-10-24 11:39:23 +02002874resume_execution:
Amaury Denoyelle03517732021-05-07 14:25:01 +02002875 if (rule->kw->flags & KWF_EXPERIMENTAL)
2876 mark_tainted(TAINTED_ACTION_EXP_EXECUTED);
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002877
2878 /* Always call the action function if defined */
2879 if (rule->action_ptr) {
2880 if ((s->req.flags & CF_READ_ERROR) ||
2881 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
2882 (px->options & PR_O_ABRT_CLOSE)))
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002883 act_opts |= ACT_OPT_FINAL;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002884
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002885 switch (rule->action_ptr(rule, px, sess, s, act_opts)) {
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002886 case ACT_RET_CONT:
2887 break;
2888 case ACT_RET_STOP:
2889 rule_ret = HTTP_RULE_RES_STOP;
2890 goto end;
2891 case ACT_RET_YIELD:
2892 s->current_rule = rule;
2893 rule_ret = HTTP_RULE_RES_YIELD;
2894 goto end;
2895 case ACT_RET_ERR:
2896 rule_ret = HTTP_RULE_RES_ERROR;
2897 goto end;
2898 case ACT_RET_DONE:
2899 rule_ret = HTTP_RULE_RES_DONE;
2900 goto end;
2901 case ACT_RET_DENY:
Christopher Fauletb58f62b2020-01-13 16:40:13 +01002902 if (txn->status == -1)
2903 txn->status = 502;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002904 rule_ret = HTTP_RULE_RES_DENY;
2905 goto end;
2906 case ACT_RET_ABRT:
2907 rule_ret = HTTP_RULE_RES_ABRT;
2908 goto end;
2909 case ACT_RET_INV:
2910 rule_ret = HTTP_RULE_RES_BADREQ;
2911 goto end;
2912 }
2913 continue; /* eval the next rule */
2914 }
2915
2916 /* If not action function defined, check for known actions */
Christopher Faulet3e964192018-10-24 11:39:23 +02002917 switch (rule->action) {
2918 case ACT_ACTION_ALLOW:
2919 rule_ret = HTTP_RULE_RES_STOP; /* "allow" rules are OK */
2920 goto end;
2921
2922 case ACT_ACTION_DENY:
Christopher Faulet5cb513a2020-05-13 17:56:56 +02002923 txn->status = rule->arg.http_reply->status;
2924 txn->http_reply = rule->arg.http_reply;
Christopher Faulet3a26bee2019-12-16 12:47:40 +01002925 rule_ret = HTTP_RULE_RES_DENY;
Christopher Faulet3e964192018-10-24 11:39:23 +02002926 goto end;
2927
Willy Tarreaubc1223b2021-09-02 16:54:33 +02002928 case ACT_HTTP_REDIR: {
2929 int ret = http_apply_redirect_rule(rule->arg.redir, s, txn);
Christopher Faulet3e964192018-10-24 11:39:23 +02002930
Willy Tarreaubc1223b2021-09-02 16:54:33 +02002931 if (ret == 2) // 2 == skip
2932 break;
2933
2934 rule_ret = ret ? HTTP_RULE_RES_ABRT : HTTP_RULE_RES_ERROR;
2935 goto end;
2936 }
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002937 /* other flags exists, but normally, they never be matched. */
Christopher Faulet3e964192018-10-24 11:39:23 +02002938 default:
2939 break;
2940 }
2941 }
2942
2943 end:
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002944 /* if the ruleset evaluation is finished reset the strict mode */
Christopher Faulet46f95542019-12-20 10:07:22 +01002945 if (rule_ret != HTTP_RULE_RES_YIELD)
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002946 txn->rsp.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01002947
Christopher Faulet3e964192018-10-24 11:39:23 +02002948 /* we reached the end of the rules, nothing to report */
2949 return rule_ret;
2950}
2951
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002952/* Executes backend and frontend http-after-response rules for the stream <s>,
2953 * in that order. it return 1 on success and 0 on error. It is the caller
2954 * responsibility to catch error or ignore it. If it catches it, this function
2955 * may be called a second time, for the internal error.
2956 */
2957int http_eval_after_res_rules(struct stream *s)
2958{
2959 struct session *sess = s->sess;
2960 enum rule_result ret = HTTP_RULE_RES_CONT;
2961
Christopher Faulet507479b2020-05-15 12:29:46 +02002962 /* Eval after-response ruleset only if the reply is not const */
2963 if (s->txn->flags & TX_CONST_REPLY)
2964 goto end;
2965
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002966 /* prune the request variables if not already done and swap to the response variables. */
2967 if (s->vars_reqres.scope != SCOPE_RES) {
2968 if (!LIST_ISEMPTY(&s->vars_reqres.head))
2969 vars_prune(&s->vars_reqres, s->sess, s);
2970 vars_init(&s->vars_reqres, SCOPE_RES);
2971 }
2972
2973 ret = http_res_get_intercept_rule(s->be, &s->be->http_after_res_rules, s);
2974 if ((ret == HTTP_RULE_RES_CONT || ret == HTTP_RULE_RES_STOP) && sess->fe != s->be)
2975 ret = http_res_get_intercept_rule(sess->fe, &sess->fe->http_after_res_rules, s);
2976
Christopher Faulet507479b2020-05-15 12:29:46 +02002977 end:
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002978 /* All other codes than CONTINUE, STOP or DONE are forbidden */
2979 return (ret == HTTP_RULE_RES_CONT || ret == HTTP_RULE_RES_STOP || ret == HTTP_RULE_RES_DONE);
2980}
2981
Christopher Fauletfcda7c62018-10-24 11:56:22 +02002982/*
2983 * Manage client-side cookie. It can impact performance by about 2% so it is
2984 * desirable to call it only when needed. This code is quite complex because
2985 * of the multiple very crappy and ambiguous syntaxes we have to support. it
2986 * highly recommended not to touch this part without a good reason !
2987 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002988static void http_manage_client_side_cookies(struct stream *s, struct channel *req)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02002989{
2990 struct session *sess = s->sess;
2991 struct http_txn *txn = s->txn;
2992 struct htx *htx;
2993 struct http_hdr_ctx ctx;
2994 char *hdr_beg, *hdr_end, *del_from;
2995 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
2996 int preserve_hdr;
2997
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002998 htx = htxbuf(&req->buf);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02002999 ctx.blk = NULL;
3000 while (http_find_header(htx, ist("Cookie"), &ctx, 1)) {
Olivier Houchardf0f42382019-07-22 17:43:46 +02003001 int is_first = 1;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003002 del_from = NULL; /* nothing to be deleted */
3003 preserve_hdr = 0; /* assume we may kill the whole header */
3004
3005 /* Now look for cookies. Conforming to RFC2109, we have to support
3006 * attributes whose name begin with a '$', and associate them with
3007 * the right cookie, if we want to delete this cookie.
3008 * So there are 3 cases for each cookie read :
3009 * 1) it's a special attribute, beginning with a '$' : ignore it.
3010 * 2) it's a server id cookie that we *MAY* want to delete : save
3011 * some pointers on it (last semi-colon, beginning of cookie...)
3012 * 3) it's an application cookie : we *MAY* have to delete a previous
3013 * "special" cookie.
3014 * At the end of loop, if a "special" cookie remains, we may have to
3015 * remove it. If no application cookie persists in the header, we
3016 * *MUST* delete it.
3017 *
3018 * Note: RFC2965 is unclear about the processing of spaces around
3019 * the equal sign in the ATTR=VALUE form. A careful inspection of
3020 * the RFC explicitly allows spaces before it, and not within the
3021 * tokens (attrs or values). An inspection of RFC2109 allows that
3022 * too but section 10.1.3 lets one think that spaces may be allowed
3023 * after the equal sign too, resulting in some (rare) buggy
3024 * implementations trying to do that. So let's do what servers do.
3025 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
3026 * allowed quoted strings in values, with any possible character
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003027 * after a backslash, including control chars and delimiters, which
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003028 * causes parsing to become ambiguous. Browsers also allow spaces
3029 * within values even without quotes.
3030 *
3031 * We have to keep multiple pointers in order to support cookie
3032 * removal at the beginning, middle or end of header without
3033 * corrupting the header. All of these headers are valid :
3034 *
3035 * hdr_beg hdr_end
3036 * | |
3037 * v |
3038 * NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3 |
3039 * NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3 v
3040 * NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3
3041 * | | | | | | |
3042 * | | | | | | |
3043 * | | | | | | +--> next
3044 * | | | | | +----> val_end
3045 * | | | | +-----------> val_beg
3046 * | | | +--------------> equal
3047 * | | +----------------> att_end
3048 * | +---------------------> att_beg
3049 * +--------------------------> prev
3050 *
3051 */
3052 hdr_beg = ctx.value.ptr;
3053 hdr_end = hdr_beg + ctx.value.len;
3054 for (prev = hdr_beg; prev < hdr_end; prev = next) {
3055 /* Iterate through all cookies on this line */
3056
3057 /* find att_beg */
3058 att_beg = prev;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003059 if (!is_first)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003060 att_beg++;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003061 is_first = 0;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003062
3063 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
3064 att_beg++;
3065
3066 /* find att_end : this is the first character after the last non
3067 * space before the equal. It may be equal to hdr_end.
3068 */
3069 equal = att_end = att_beg;
3070 while (equal < hdr_end) {
3071 if (*equal == '=' || *equal == ',' || *equal == ';')
3072 break;
3073 if (HTTP_IS_SPHT(*equal++))
3074 continue;
3075 att_end = equal;
3076 }
3077
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003078 /* here, <equal> points to '=', a delimiter or the end. <att_end>
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003079 * is between <att_beg> and <equal>, both may be identical.
3080 */
3081 /* look for end of cookie if there is an equal sign */
3082 if (equal < hdr_end && *equal == '=') {
3083 /* look for the beginning of the value */
3084 val_beg = equal + 1;
3085 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
3086 val_beg++;
3087
3088 /* find the end of the value, respecting quotes */
3089 next = http_find_cookie_value_end(val_beg, hdr_end);
3090
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003091 /* make val_end point to the first white space or delimiter after the value */
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003092 val_end = next;
3093 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
3094 val_end--;
3095 }
3096 else
3097 val_beg = val_end = next = equal;
3098
3099 /* We have nothing to do with attributes beginning with
3100 * '$'. However, they will automatically be removed if a
3101 * header before them is removed, since they're supposed
3102 * to be linked together.
3103 */
3104 if (*att_beg == '$')
3105 continue;
3106
3107 /* Ignore cookies with no equal sign */
3108 if (equal == next) {
3109 /* This is not our cookie, so we must preserve it. But if we already
3110 * scheduled another cookie for removal, we cannot remove the
3111 * complete header, but we can remove the previous block itself.
3112 */
3113 preserve_hdr = 1;
3114 if (del_from != NULL) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003115 int delta = http_del_hdr_value(hdr_beg, hdr_end, &del_from, prev);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003116 val_end += delta;
3117 next += delta;
3118 hdr_end += delta;
3119 prev = del_from;
3120 del_from = NULL;
3121 }
3122 continue;
3123 }
3124
3125 /* if there are spaces around the equal sign, we need to
3126 * strip them otherwise we'll get trouble for cookie captures,
3127 * or even for rewrites. Since this happens extremely rarely,
3128 * it does not hurt performance.
3129 */
3130 if (unlikely(att_end != equal || val_beg > equal + 1)) {
3131 int stripped_before = 0;
3132 int stripped_after = 0;
3133
3134 if (att_end != equal) {
3135 memmove(att_end, equal, hdr_end - equal);
3136 stripped_before = (att_end - equal);
3137 equal += stripped_before;
3138 val_beg += stripped_before;
3139 }
3140
3141 if (val_beg > equal + 1) {
3142 memmove(equal + 1, val_beg, hdr_end + stripped_before - val_beg);
3143 stripped_after = (equal + 1) - val_beg;
3144 val_beg += stripped_after;
3145 stripped_before += stripped_after;
3146 }
3147
3148 val_end += stripped_before;
3149 next += stripped_before;
3150 hdr_end += stripped_before;
3151 }
3152 /* now everything is as on the diagram above */
3153
3154 /* First, let's see if we want to capture this cookie. We check
3155 * that we don't already have a client side cookie, because we
3156 * can only capture one. Also as an optimisation, we ignore
3157 * cookies shorter than the declared name.
3158 */
3159 if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL &&
3160 (val_end - att_beg >= sess->fe->capture_namelen) &&
3161 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
3162 int log_len = val_end - att_beg;
3163
3164 if ((txn->cli_cookie = pool_alloc(pool_head_capture)) == NULL) {
3165 ha_alert("HTTP logging : out of memory.\n");
3166 } else {
3167 if (log_len > sess->fe->capture_len)
3168 log_len = sess->fe->capture_len;
3169 memcpy(txn->cli_cookie, att_beg, log_len);
3170 txn->cli_cookie[log_len] = 0;
3171 }
3172 }
3173
3174 /* Persistence cookies in passive, rewrite or insert mode have the
3175 * following form :
3176 *
3177 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
3178 *
3179 * For cookies in prefix mode, the form is :
3180 *
3181 * Cookie: NAME=SRV~VALUE
3182 */
3183 if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
3184 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
3185 struct server *srv = s->be->srv;
3186 char *delim;
3187
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003188 /* if we're in cookie prefix mode, we'll search the delimiter so that we
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003189 * have the server ID between val_beg and delim, and the original cookie between
3190 * delim+1 and val_end. Otherwise, delim==val_end :
3191 *
3192 * hdr_beg
3193 * |
3194 * v
3195 * NAME=SRV; # in all but prefix modes
3196 * NAME=SRV~OPAQUE ; # in prefix mode
3197 * || || | |+-> next
3198 * || || | +--> val_end
3199 * || || +---------> delim
3200 * || |+------------> val_beg
3201 * || +-------------> att_end = equal
3202 * |+-----------------> att_beg
3203 * +------------------> prev
3204 *
3205 */
3206 if (s->be->ck_opts & PR_CK_PFX) {
3207 for (delim = val_beg; delim < val_end; delim++)
3208 if (*delim == COOKIE_DELIM)
3209 break;
3210 }
3211 else {
3212 char *vbar1;
3213 delim = val_end;
3214 /* Now check if the cookie contains a date field, which would
3215 * appear after a vertical bar ('|') just after the server name
3216 * and before the delimiter.
3217 */
3218 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
3219 if (vbar1) {
3220 /* OK, so left of the bar is the server's cookie and
3221 * right is the last seen date. It is a base64 encoded
3222 * 30-bit value representing the UNIX date since the
3223 * epoch in 4-second quantities.
3224 */
3225 int val;
3226 delim = vbar1++;
3227 if (val_end - vbar1 >= 5) {
3228 val = b64tos30(vbar1);
3229 if (val > 0)
3230 txn->cookie_last_date = val << 2;
3231 }
3232 /* look for a second vertical bar */
3233 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
3234 if (vbar1 && (val_end - vbar1 > 5)) {
3235 val = b64tos30(vbar1 + 1);
3236 if (val > 0)
3237 txn->cookie_first_date = val << 2;
3238 }
3239 }
3240 }
3241
3242 /* if the cookie has an expiration date and the proxy wants to check
3243 * it, then we do that now. We first check if the cookie is too old,
3244 * then only if it has expired. We detect strict overflow because the
3245 * time resolution here is not great (4 seconds). Cookies with dates
3246 * in the future are ignored if their offset is beyond one day. This
3247 * allows an admin to fix timezone issues without expiring everyone
3248 * and at the same time avoids keeping unwanted side effects for too
3249 * long.
3250 */
3251 if (txn->cookie_first_date && s->be->cookie_maxlife &&
3252 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) ||
3253 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
3254 txn->flags &= ~TX_CK_MASK;
3255 txn->flags |= TX_CK_OLD;
3256 delim = val_beg; // let's pretend we have not found the cookie
3257 txn->cookie_first_date = 0;
3258 txn->cookie_last_date = 0;
3259 }
3260 else if (txn->cookie_last_date && s->be->cookie_maxidle &&
3261 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) ||
3262 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
3263 txn->flags &= ~TX_CK_MASK;
3264 txn->flags |= TX_CK_EXPIRED;
3265 delim = val_beg; // let's pretend we have not found the cookie
3266 txn->cookie_first_date = 0;
3267 txn->cookie_last_date = 0;
3268 }
3269
3270 /* Here, we'll look for the first running server which supports the cookie.
3271 * This allows to share a same cookie between several servers, for example
3272 * to dedicate backup servers to specific servers only.
3273 * However, to prevent clients from sticking to cookie-less backup server
3274 * when they have incidentely learned an empty cookie, we simply ignore
3275 * empty cookies and mark them as invalid.
3276 * The same behaviour is applied when persistence must be ignored.
3277 */
3278 if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
3279 srv = NULL;
3280
3281 while (srv) {
3282 if (srv->cookie && (srv->cklen == delim - val_beg) &&
3283 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
3284 if ((srv->cur_state != SRV_ST_STOPPED) ||
3285 (s->be->options & PR_O_PERSIST) ||
3286 (s->flags & SF_FORCE_PRST)) {
3287 /* we found the server and we can use it */
3288 txn->flags &= ~TX_CK_MASK;
3289 txn->flags |= (srv->cur_state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
3290 s->flags |= SF_DIRECT | SF_ASSIGNED;
3291 s->target = &srv->obj_type;
3292 break;
3293 } else {
3294 /* we found a server, but it's down,
3295 * mark it as such and go on in case
3296 * another one is available.
3297 */
3298 txn->flags &= ~TX_CK_MASK;
3299 txn->flags |= TX_CK_DOWN;
3300 }
3301 }
3302 srv = srv->next;
3303 }
3304
3305 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
3306 /* no server matched this cookie or we deliberately skipped it */
3307 txn->flags &= ~TX_CK_MASK;
3308 if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
3309 txn->flags |= TX_CK_UNUSED;
3310 else
3311 txn->flags |= TX_CK_INVALID;
3312 }
3313
3314 /* depending on the cookie mode, we may have to either :
3315 * - delete the complete cookie if we're in insert+indirect mode, so that
3316 * the server never sees it ;
3317 * - remove the server id from the cookie value, and tag the cookie as an
Joseph Herlante9d5c722018-11-25 11:00:25 -08003318 * application cookie so that it does not get accidentally removed later,
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003319 * if we're in cookie prefix mode
3320 */
3321 if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
3322 int delta; /* negative */
3323
3324 memmove(val_beg, delim + 1, hdr_end - (delim + 1));
3325 delta = val_beg - (delim + 1);
3326 val_end += delta;
3327 next += delta;
3328 hdr_end += delta;
3329 del_from = NULL;
3330 preserve_hdr = 1; /* we want to keep this cookie */
3331 }
3332 else if (del_from == NULL &&
3333 (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
3334 del_from = prev;
3335 }
3336 }
3337 else {
3338 /* This is not our cookie, so we must preserve it. But if we already
3339 * scheduled another cookie for removal, we cannot remove the
3340 * complete header, but we can remove the previous block itself.
3341 */
3342 preserve_hdr = 1;
3343
3344 if (del_from != NULL) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003345 int delta = http_del_hdr_value(hdr_beg, hdr_end, &del_from, prev);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003346 if (att_beg >= del_from)
3347 att_beg += delta;
3348 if (att_end >= del_from)
3349 att_end += delta;
3350 val_beg += delta;
3351 val_end += delta;
3352 next += delta;
3353 hdr_end += delta;
3354 prev = del_from;
3355 del_from = NULL;
3356 }
3357 }
3358
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003359 } /* for each cookie */
3360
3361
3362 /* There are no more cookies on this line.
3363 * We may still have one (or several) marked for deletion at the
3364 * end of the line. We must do this now in two ways :
3365 * - if some cookies must be preserved, we only delete from the
3366 * mark to the end of line ;
3367 * - if nothing needs to be preserved, simply delete the whole header
3368 */
3369 if (del_from) {
3370 hdr_end = (preserve_hdr ? del_from : hdr_beg);
3371 }
3372 if ((hdr_end - hdr_beg) != ctx.value.len) {
Christopher Faulet3e2638e2019-06-18 09:49:16 +02003373 if (hdr_beg != hdr_end)
3374 htx_change_blk_value_len(htx, ctx.blk, hdr_end - hdr_beg);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003375 else
3376 http_remove_header(htx, &ctx);
3377 }
3378 } /* for each "Cookie header */
3379}
3380
3381/*
3382 * Manage server-side cookies. It can impact performance by about 2% so it is
3383 * desirable to call it only when needed. This function is also used when we
3384 * just need to know if there is a cookie (eg: for check-cache).
3385 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003386static void http_manage_server_side_cookies(struct stream *s, struct channel *res)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003387{
3388 struct session *sess = s->sess;
3389 struct http_txn *txn = s->txn;
3390 struct htx *htx;
3391 struct http_hdr_ctx ctx;
3392 struct server *srv;
3393 char *hdr_beg, *hdr_end;
3394 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau6f7a02a2019-04-15 21:49:49 +02003395 int is_cookie2 = 0;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003396
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003397 htx = htxbuf(&res->buf);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003398
3399 ctx.blk = NULL;
3400 while (1) {
Olivier Houchardf0f42382019-07-22 17:43:46 +02003401 int is_first = 1;
3402
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003403 if (!http_find_header(htx, ist("Set-Cookie"), &ctx, 1)) {
3404 if (!http_find_header(htx, ist("Set-Cookie2"), &ctx, 1))
3405 break;
3406 is_cookie2 = 1;
3407 }
3408
3409 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
3410 * <prev> points to the colon.
3411 */
3412 txn->flags |= TX_SCK_PRESENT;
3413
3414 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
3415 * check-cache is enabled) and we are not interested in checking
3416 * them. Warning, the cookie capture is declared in the frontend.
3417 */
3418 if (s->be->cookie_name == NULL && sess->fe->capture_name == NULL)
3419 break;
3420
3421 /* OK so now we know we have to process this response cookie.
3422 * The format of the Set-Cookie header is slightly different
3423 * from the format of the Cookie header in that it does not
3424 * support the comma as a cookie delimiter (thus the header
3425 * cannot be folded) because the Expires attribute described in
3426 * the original Netscape's spec may contain an unquoted date
3427 * with a comma inside. We have to live with this because
3428 * many browsers don't support Max-Age and some browsers don't
3429 * support quoted strings. However the Set-Cookie2 header is
3430 * clean.
3431 *
3432 * We have to keep multiple pointers in order to support cookie
3433 * removal at the beginning, middle or end of header without
3434 * corrupting the header (in case of set-cookie2). A special
3435 * pointer, <scav> points to the beginning of the set-cookie-av
3436 * fields after the first semi-colon. The <next> pointer points
3437 * either to the end of line (set-cookie) or next unquoted comma
3438 * (set-cookie2). All of these headers are valid :
3439 *
3440 * hdr_beg hdr_end
3441 * | |
3442 * v |
3443 * NAME1 = VALUE 1 ; Secure; Path="/" |
3444 * NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT v
3445 * NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT
3446 * NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard
3447 * | | | | | | | |
3448 * | | | | | | | +-> next
3449 * | | | | | | +------------> scav
3450 * | | | | | +--------------> val_end
3451 * | | | | +--------------------> val_beg
3452 * | | | +----------------------> equal
3453 * | | +------------------------> att_end
3454 * | +----------------------------> att_beg
3455 * +------------------------------> prev
3456 * -------------------------------> hdr_beg
3457 */
3458 hdr_beg = ctx.value.ptr;
3459 hdr_end = hdr_beg + ctx.value.len;
3460 for (prev = hdr_beg; prev < hdr_end; prev = next) {
3461
3462 /* Iterate through all cookies on this line */
3463
3464 /* find att_beg */
3465 att_beg = prev;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003466 if (!is_first)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003467 att_beg++;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003468 is_first = 0;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003469
3470 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
3471 att_beg++;
3472
3473 /* find att_end : this is the first character after the last non
3474 * space before the equal. It may be equal to hdr_end.
3475 */
3476 equal = att_end = att_beg;
3477
3478 while (equal < hdr_end) {
3479 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
3480 break;
3481 if (HTTP_IS_SPHT(*equal++))
3482 continue;
3483 att_end = equal;
3484 }
3485
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003486 /* here, <equal> points to '=', a delimiter or the end. <att_end>
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003487 * is between <att_beg> and <equal>, both may be identical.
3488 */
3489
3490 /* look for end of cookie if there is an equal sign */
3491 if (equal < hdr_end && *equal == '=') {
3492 /* look for the beginning of the value */
3493 val_beg = equal + 1;
3494 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
3495 val_beg++;
3496
3497 /* find the end of the value, respecting quotes */
3498 next = http_find_cookie_value_end(val_beg, hdr_end);
3499
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003500 /* make val_end point to the first white space or delimiter after the value */
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003501 val_end = next;
3502 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
3503 val_end--;
3504 }
3505 else {
3506 /* <equal> points to next comma, semi-colon or EOL */
3507 val_beg = val_end = next = equal;
3508 }
3509
3510 if (next < hdr_end) {
3511 /* Set-Cookie2 supports multiple cookies, and <next> points to
3512 * a colon or semi-colon before the end. So skip all attr-value
3513 * pairs and look for the next comma. For Set-Cookie, since
3514 * commas are permitted in values, skip to the end.
3515 */
3516 if (is_cookie2)
3517 next = http_find_hdr_value_end(next, hdr_end);
3518 else
3519 next = hdr_end;
3520 }
3521
3522 /* Now everything is as on the diagram above */
3523
3524 /* Ignore cookies with no equal sign */
3525 if (equal == val_end)
3526 continue;
3527
3528 /* If there are spaces around the equal sign, we need to
3529 * strip them otherwise we'll get trouble for cookie captures,
3530 * or even for rewrites. Since this happens extremely rarely,
3531 * it does not hurt performance.
3532 */
3533 if (unlikely(att_end != equal || val_beg > equal + 1)) {
3534 int stripped_before = 0;
3535 int stripped_after = 0;
3536
3537 if (att_end != equal) {
3538 memmove(att_end, equal, hdr_end - equal);
3539 stripped_before = (att_end - equal);
3540 equal += stripped_before;
3541 val_beg += stripped_before;
3542 }
3543
3544 if (val_beg > equal + 1) {
3545 memmove(equal + 1, val_beg, hdr_end + stripped_before - val_beg);
3546 stripped_after = (equal + 1) - val_beg;
3547 val_beg += stripped_after;
3548 stripped_before += stripped_after;
3549 }
3550
3551 val_end += stripped_before;
3552 next += stripped_before;
3553 hdr_end += stripped_before;
3554
Christopher Faulet3e2638e2019-06-18 09:49:16 +02003555 htx_change_blk_value_len(htx, ctx.blk, hdr_end - hdr_beg);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003556 ctx.value.len = hdr_end - hdr_beg;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003557 }
3558
3559 /* First, let's see if we want to capture this cookie. We check
3560 * that we don't already have a server side cookie, because we
3561 * can only capture one. Also as an optimisation, we ignore
3562 * cookies shorter than the declared name.
3563 */
3564 if (sess->fe->capture_name != NULL &&
3565 txn->srv_cookie == NULL &&
3566 (val_end - att_beg >= sess->fe->capture_namelen) &&
3567 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
3568 int log_len = val_end - att_beg;
3569 if ((txn->srv_cookie = pool_alloc(pool_head_capture)) == NULL) {
3570 ha_alert("HTTP logging : out of memory.\n");
3571 }
3572 else {
3573 if (log_len > sess->fe->capture_len)
3574 log_len = sess->fe->capture_len;
3575 memcpy(txn->srv_cookie, att_beg, log_len);
3576 txn->srv_cookie[log_len] = 0;
3577 }
3578 }
3579
3580 srv = objt_server(s->target);
3581 /* now check if we need to process it for persistence */
3582 if (!(s->flags & SF_IGNORE_PRST) &&
3583 (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
3584 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
3585 /* assume passive cookie by default */
3586 txn->flags &= ~TX_SCK_MASK;
3587 txn->flags |= TX_SCK_FOUND;
3588
3589 /* If the cookie is in insert mode on a known server, we'll delete
3590 * this occurrence because we'll insert another one later.
3591 * We'll delete it too if the "indirect" option is set and we're in
3592 * a direct access.
3593 */
3594 if (s->be->ck_opts & PR_CK_PSV) {
3595 /* The "preserve" flag was set, we don't want to touch the
3596 * server's cookie.
3597 */
3598 }
3599 else if ((srv && (s->be->ck_opts & PR_CK_INS)) ||
3600 ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) {
3601 /* this cookie must be deleted */
3602 if (prev == hdr_beg && next == hdr_end) {
3603 /* whole header */
3604 http_remove_header(htx, &ctx);
3605 /* note: while both invalid now, <next> and <hdr_end>
3606 * are still equal, so the for() will stop as expected.
3607 */
3608 } else {
3609 /* just remove the value */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003610 int delta = http_del_hdr_value(hdr_beg, hdr_end, &prev, next);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003611 next = prev;
3612 hdr_end += delta;
3613 }
3614 txn->flags &= ~TX_SCK_MASK;
3615 txn->flags |= TX_SCK_DELETED;
3616 /* and go on with next cookie */
3617 }
3618 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) {
3619 /* replace bytes val_beg->val_end with the cookie name associated
3620 * with this server since we know it.
3621 */
3622 int sliding, delta;
3623
3624 ctx.value = ist2(val_beg, val_end - val_beg);
3625 ctx.lws_before = ctx.lws_after = 0;
3626 http_replace_header_value(htx, &ctx, ist2(srv->cookie, srv->cklen));
3627 delta = srv->cklen - (val_end - val_beg);
3628 sliding = (ctx.value.ptr - val_beg);
3629 hdr_beg += sliding;
3630 val_beg += sliding;
3631 next += sliding + delta;
3632 hdr_end += sliding + delta;
3633
3634 txn->flags &= ~TX_SCK_MASK;
3635 txn->flags |= TX_SCK_REPLACED;
3636 }
3637 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) {
3638 /* insert the cookie name associated with this server
3639 * before existing cookie, and insert a delimiter between them..
3640 */
3641 int sliding, delta;
3642 ctx.value = ist2(val_beg, 0);
3643 ctx.lws_before = ctx.lws_after = 0;
3644 http_replace_header_value(htx, &ctx, ist2(srv->cookie, srv->cklen + 1));
3645 delta = srv->cklen + 1;
3646 sliding = (ctx.value.ptr - val_beg);
3647 hdr_beg += sliding;
3648 val_beg += sliding;
3649 next += sliding + delta;
3650 hdr_end += sliding + delta;
3651
3652 val_beg[srv->cklen] = COOKIE_DELIM;
3653 txn->flags &= ~TX_SCK_MASK;
3654 txn->flags |= TX_SCK_REPLACED;
3655 }
3656 }
3657 /* that's done for this cookie, check the next one on the same
3658 * line when next != hdr_end (only if is_cookie2).
3659 */
3660 }
3661 }
3662}
3663
Christopher Faulet25a02f62018-10-24 12:00:25 +02003664/*
3665 * Parses the Cache-Control and Pragma request header fields to determine if
3666 * the request may be served from the cache and/or if it is cacheable. Updates
3667 * s->txn->flags.
3668 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003669void http_check_request_for_cacheability(struct stream *s, struct channel *req)
Christopher Faulet25a02f62018-10-24 12:00:25 +02003670{
3671 struct http_txn *txn = s->txn;
3672 struct htx *htx;
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003673 struct http_hdr_ctx ctx = { .blk = NULL };
3674 int pragma_found, cc_found;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003675
3676 if ((txn->flags & (TX_CACHEABLE|TX_CACHE_IGNORE)) == TX_CACHE_IGNORE)
3677 return; /* nothing more to do here */
3678
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003679 htx = htxbuf(&req->buf);
Christopher Faulet25a02f62018-10-24 12:00:25 +02003680 pragma_found = cc_found = 0;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003681
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003682 /* Check "pragma" header for HTTP/1.0 compatibility. */
3683 if (http_find_header(htx, ist("pragma"), &ctx, 1)) {
3684 if (isteqi(ctx.value, ist("no-cache"))) {
3685 pragma_found = 1;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003686 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003687 }
Christopher Faulet25a02f62018-10-24 12:00:25 +02003688
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003689 ctx.blk = NULL;
3690 /* Don't use the cache and don't try to store if we found the
3691 * Authorization header */
3692 if (http_find_header(htx, ist("authorization"), &ctx, 1)) {
3693 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3694 txn->flags |= TX_CACHE_IGNORE;
3695 }
Christopher Faulet25a02f62018-10-24 12:00:25 +02003696
Christopher Faulet25a02f62018-10-24 12:00:25 +02003697
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003698 /* Look for "cache-control" header and iterate over all the values
3699 * until we find one that specifies that caching is possible or not. */
3700 ctx.blk = NULL;
3701 while (http_find_header(htx, ist("cache-control"), &ctx, 0)) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003702 cc_found = 1;
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003703 /* We don't check the values after max-age, max-stale nor min-fresh,
3704 * we simply don't use the cache when they're specified. */
3705 if (istmatchi(ctx.value, ist("max-age")) ||
3706 istmatchi(ctx.value, ist("no-cache")) ||
3707 istmatchi(ctx.value, ist("max-stale")) ||
3708 istmatchi(ctx.value, ist("min-fresh"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003709 txn->flags |= TX_CACHE_IGNORE;
3710 continue;
3711 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003712 if (istmatchi(ctx.value, ist("no-store"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003713 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3714 continue;
3715 }
3716 }
3717
3718 /* RFC7234#5.4:
3719 * When the Cache-Control header field is also present and
3720 * understood in a request, Pragma is ignored.
3721 * When the Cache-Control header field is not present in a
3722 * request, caches MUST consider the no-cache request
3723 * pragma-directive as having the same effect as if
3724 * "Cache-Control: no-cache" were present.
3725 */
3726 if (!cc_found && pragma_found)
3727 txn->flags |= TX_CACHE_IGNORE;
3728}
3729
3730/*
3731 * Check if response is cacheable or not. Updates s->txn->flags.
3732 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003733void http_check_response_for_cacheability(struct stream *s, struct channel *res)
Christopher Faulet25a02f62018-10-24 12:00:25 +02003734{
3735 struct http_txn *txn = s->txn;
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003736 struct http_hdr_ctx ctx = { .blk = NULL };
Christopher Faulet25a02f62018-10-24 12:00:25 +02003737 struct htx *htx;
Remi Tricot-Le Bretoncc9bf2e2020-11-12 11:14:41 +01003738 int has_freshness_info = 0;
3739 int has_validator = 0;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003740
3741 if (txn->status < 200) {
3742 /* do not try to cache interim responses! */
3743 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3744 return;
3745 }
3746
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003747 htx = htxbuf(&res->buf);
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003748 /* Check "pragma" header for HTTP/1.0 compatibility. */
3749 if (http_find_header(htx, ist("pragma"), &ctx, 1)) {
3750 if (isteqi(ctx.value, ist("no-cache"))) {
3751 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3752 return;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003753 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003754 }
Christopher Faulet25a02f62018-10-24 12:00:25 +02003755
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003756 /* Look for "cache-control" header and iterate over all the values
3757 * until we find one that specifies that caching is possible or not. */
3758 ctx.blk = NULL;
3759 while (http_find_header(htx, ist("cache-control"), &ctx, 0)) {
3760 if (isteqi(ctx.value, ist("public"))) {
3761 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003762 continue;
3763 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003764 if (isteqi(ctx.value, ist("private")) ||
3765 isteqi(ctx.value, ist("no-cache")) ||
3766 isteqi(ctx.value, ist("no-store")) ||
3767 isteqi(ctx.value, ist("max-age=0")) ||
3768 isteqi(ctx.value, ist("s-maxage=0"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003769 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003770 continue;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003771 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003772 /* We might have a no-cache="set-cookie" form. */
3773 if (istmatchi(ctx.value, ist("no-cache=\"set-cookie"))) {
3774 txn->flags &= ~TX_CACHE_COOK;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003775 continue;
3776 }
Remi Tricot-Le Bretoncc9bf2e2020-11-12 11:14:41 +01003777
3778 if (istmatchi(ctx.value, ist("s-maxage")) ||
3779 istmatchi(ctx.value, ist("max-age"))) {
3780 has_freshness_info = 1;
3781 continue;
3782 }
3783 }
3784
3785 /* If no freshness information could be found in Cache-Control values,
3786 * look for an Expires header. */
3787 if (!has_freshness_info) {
3788 ctx.blk = NULL;
3789 has_freshness_info = http_find_header(htx, ist("expires"), &ctx, 0);
Christopher Faulet25a02f62018-10-24 12:00:25 +02003790 }
Remi Tricot-Le Bretoncc9bf2e2020-11-12 11:14:41 +01003791
3792 /* If no freshness information could be found in Cache-Control or Expires
3793 * values, look for an explicit validator. */
3794 if (!has_freshness_info) {
3795 ctx.blk = NULL;
3796 has_validator = 1;
3797 if (!http_find_header(htx, ist("etag"), &ctx, 0)) {
3798 ctx.blk = NULL;
3799 if (!http_find_header(htx, ist("last-modified"), &ctx, 0))
3800 has_validator = 0;
3801 }
3802 }
3803
3804 /* We won't store an entry that has neither a cache validator nor an
3805 * explicit expiration time, as suggested in RFC 7234#3. */
3806 if (!has_freshness_info && !has_validator)
3807 txn->flags |= TX_CACHE_IGNORE;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003808}
3809
Christopher Faulet377c5a52018-10-24 21:21:30 +02003810/*
3811 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
3812 * for the current backend.
3813 *
3814 * It is assumed that the request is either a HEAD, GET, or POST and that the
3815 * uri_auth field is valid.
3816 *
3817 * Returns 1 if stats should be provided, otherwise 0.
3818 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003819static int http_stats_check_uri(struct stream *s, struct http_txn *txn, struct proxy *backend)
Christopher Faulet377c5a52018-10-24 21:21:30 +02003820{
3821 struct uri_auth *uri_auth = backend->uri_auth;
3822 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003823 struct htx_sl *sl;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003824 struct ist uri;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003825
3826 if (!uri_auth)
3827 return 0;
3828
3829 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
3830 return 0;
3831
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003832 htx = htxbuf(&s->req.buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02003833 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003834 uri = htx_sl_req_uri(sl);
Amaury Denoyellec453f952021-07-06 11:40:12 +02003835 if (*uri_auth->uri_prefix == '/') {
3836 struct http_uri_parser parser = http_uri_parser_init(uri);
3837 uri = http_parse_path(&parser);
3838 }
Christopher Faulet377c5a52018-10-24 21:21:30 +02003839
3840 /* check URI size */
3841 if (uri_auth->uri_len > uri.len)
3842 return 0;
3843
3844 if (memcmp(uri.ptr, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
3845 return 0;
3846
3847 return 1;
3848}
3849
3850/* This function prepares an applet to handle the stats. It can deal with the
3851 * "100-continue" expectation, check that admin rules are met for POST requests,
3852 * and program a response message if something was unexpected. It cannot fail
3853 * and always relies on the stats applet to complete the job. It does not touch
3854 * analysers nor counters, which are left to the caller. It does not touch
3855 * s->target which is supposed to already point to the stats applet. The caller
3856 * is expected to have already assigned an appctx to the stream.
3857 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003858static int http_handle_stats(struct stream *s, struct channel *req)
Christopher Faulet377c5a52018-10-24 21:21:30 +02003859{
3860 struct stats_admin_rule *stats_admin_rule;
3861 struct stream_interface *si = &s->si[1];
3862 struct session *sess = s->sess;
3863 struct http_txn *txn = s->txn;
3864 struct http_msg *msg = &txn->req;
3865 struct uri_auth *uri_auth = s->be->uri_auth;
3866 const char *h, *lookup, *end;
3867 struct appctx *appctx;
3868 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003869 struct htx_sl *sl;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003870
3871 appctx = si_appctx(si);
3872 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
3873 appctx->st1 = appctx->st2 = 0;
3874 appctx->ctx.stats.st_code = STAT_STATUS_INIT;
Willy Tarreau676c29e2019-10-09 10:50:01 +02003875 appctx->ctx.stats.flags |= uri_auth->flags;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003876 appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */
3877 if ((msg->flags & HTTP_MSGF_VER_11) && (txn->meth != HTTP_METH_HEAD))
3878 appctx->ctx.stats.flags |= STAT_CHUNKED;
3879
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003880 htx = htxbuf(&req->buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02003881 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003882 lookup = HTX_SL_REQ_UPTR(sl) + uri_auth->uri_len;
3883 end = HTX_SL_REQ_UPTR(sl) + HTX_SL_REQ_ULEN(sl);
Christopher Faulet377c5a52018-10-24 21:21:30 +02003884
3885 for (h = lookup; h <= end - 3; h++) {
3886 if (memcmp(h, ";up", 3) == 0) {
3887 appctx->ctx.stats.flags |= STAT_HIDE_DOWN;
3888 break;
3889 }
Amaury Denoyelle91e55ea2021-02-25 14:46:08 +01003890 }
3891
3892 for (h = lookup; h <= end - 9; h++) {
3893 if (memcmp(h, ";no-maint", 9) == 0) {
Willy Tarreau3e320362020-10-23 17:28:57 +02003894 appctx->ctx.stats.flags |= STAT_HIDE_MAINT;
3895 break;
3896 }
Christopher Faulet377c5a52018-10-24 21:21:30 +02003897 }
3898
3899 if (uri_auth->refresh) {
3900 for (h = lookup; h <= end - 10; h++) {
3901 if (memcmp(h, ";norefresh", 10) == 0) {
3902 appctx->ctx.stats.flags |= STAT_NO_REFRESH;
3903 break;
3904 }
3905 }
3906 }
3907
3908 for (h = lookup; h <= end - 4; h++) {
3909 if (memcmp(h, ";csv", 4) == 0) {
Christopher Faulet6338a082019-09-09 15:50:54 +02003910 appctx->ctx.stats.flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
Christopher Faulet377c5a52018-10-24 21:21:30 +02003911 break;
3912 }
3913 }
3914
3915 for (h = lookup; h <= end - 6; h++) {
3916 if (memcmp(h, ";typed", 6) == 0) {
Christopher Faulet6338a082019-09-09 15:50:54 +02003917 appctx->ctx.stats.flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
Christopher Faulet377c5a52018-10-24 21:21:30 +02003918 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
3919 break;
3920 }
3921 }
3922
Christopher Faulet6338a082019-09-09 15:50:54 +02003923 for (h = lookup; h <= end - 5; h++) {
3924 if (memcmp(h, ";json", 5) == 0) {
3925 appctx->ctx.stats.flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
3926 appctx->ctx.stats.flags |= STAT_FMT_JSON;
3927 break;
3928 }
3929 }
3930
3931 for (h = lookup; h <= end - 12; h++) {
3932 if (memcmp(h, ";json-schema", 12) == 0) {
3933 appctx->ctx.stats.flags &= ~STAT_FMT_MASK;
3934 appctx->ctx.stats.flags |= STAT_JSON_SCHM;
3935 break;
3936 }
3937 }
3938
Christopher Faulet377c5a52018-10-24 21:21:30 +02003939 for (h = lookup; h <= end - 8; h++) {
3940 if (memcmp(h, ";st=", 4) == 0) {
3941 int i;
3942 h += 4;
3943 appctx->ctx.stats.st_code = STAT_STATUS_UNKN;
3944 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
3945 if (strncmp(stat_status_codes[i], h, 4) == 0) {
3946 appctx->ctx.stats.st_code = i;
3947 break;
3948 }
3949 }
3950 break;
3951 }
3952 }
3953
3954 appctx->ctx.stats.scope_str = 0;
3955 appctx->ctx.stats.scope_len = 0;
3956 for (h = lookup; h <= end - 8; h++) {
3957 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
3958 int itx = 0;
3959 const char *h2;
3960 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
3961 const char *err;
3962
3963 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
3964 h2 = h;
Christopher Fauleted7a0662019-01-14 11:07:34 +01003965 appctx->ctx.stats.scope_str = h2 - HTX_SL_REQ_UPTR(sl);
3966 while (h < end) {
Christopher Faulet377c5a52018-10-24 21:21:30 +02003967 if (*h == ';' || *h == '&' || *h == ' ')
3968 break;
3969 itx++;
3970 h++;
3971 }
3972
3973 if (itx > STAT_SCOPE_TXT_MAXLEN)
3974 itx = STAT_SCOPE_TXT_MAXLEN;
3975 appctx->ctx.stats.scope_len = itx;
3976
3977 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
3978 memcpy(scope_txt, h2, itx);
3979 scope_txt[itx] = '\0';
3980 err = invalid_char(scope_txt);
3981 if (err) {
3982 /* bad char in search text => clear scope */
3983 appctx->ctx.stats.scope_str = 0;
3984 appctx->ctx.stats.scope_len = 0;
3985 }
3986 break;
3987 }
3988 }
3989
3990 /* now check whether we have some admin rules for this request */
3991 list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) {
3992 int ret = 1;
3993
3994 if (stats_admin_rule->cond) {
3995 ret = acl_exec_cond(stats_admin_rule->cond, s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
3996 ret = acl_pass(ret);
3997 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
3998 ret = !ret;
3999 }
4000
4001 if (ret) {
4002 /* no rule, or the rule matches */
4003 appctx->ctx.stats.flags |= STAT_ADMIN;
4004 break;
4005 }
4006 }
4007
Christopher Faulet5d45e382019-02-27 15:15:23 +01004008 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
4009 appctx->st0 = STAT_HTTP_HEAD;
4010 else if (txn->meth == HTTP_METH_POST) {
Christopher Fauletbd9e8422019-08-15 22:26:48 +02004011 if (appctx->ctx.stats.flags & STAT_ADMIN) {
Christopher Faulet377c5a52018-10-24 21:21:30 +02004012 appctx->st0 = STAT_HTTP_POST;
Christopher Fauletbd9e8422019-08-15 22:26:48 +02004013 if (msg->msg_state < HTTP_MSG_DATA)
4014 req->analysers |= AN_REQ_HTTP_BODY;
4015 }
Christopher Faulet377c5a52018-10-24 21:21:30 +02004016 else {
Christopher Faulet5d45e382019-02-27 15:15:23 +01004017 /* POST without admin level */
Christopher Faulet377c5a52018-10-24 21:21:30 +02004018 appctx->ctx.stats.flags &= ~STAT_CHUNKED;
4019 appctx->ctx.stats.st_code = STAT_STATUS_DENY;
4020 appctx->st0 = STAT_HTTP_LAST;
4021 }
4022 }
4023 else {
Christopher Faulet5d45e382019-02-27 15:15:23 +01004024 /* Unsupported method */
4025 appctx->ctx.stats.flags &= ~STAT_CHUNKED;
4026 appctx->ctx.stats.st_code = STAT_STATUS_IVAL;
4027 appctx->st0 = STAT_HTTP_LAST;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004028 }
4029
4030 s->task->nice = -32; /* small boost for HTTP statistics */
4031 return 1;
4032}
4033
Christopher Faulet021a8e42021-03-29 10:46:38 +02004034/* This function waits for the message payload at most <time> milliseconds (may
4035 * be set to TICK_ETERNITY). It stops to wait if at least <bytes> bytes of the
4036 * payload are received (0 means no limit). It returns HTTP_RULE_* depending on
4037 * the result:
4038 *
4039 * - HTTP_RULE_RES_CONT when conditions are met to stop waiting
4040 * - HTTP_RULE_RES_YIELD to wait for more data
Ilya Shipitsinb2be9a12021-04-24 13:25:42 +05004041 * - HTTP_RULE_RES_ABRT when a timeout occurred.
Christopher Faulet021a8e42021-03-29 10:46:38 +02004042 * - HTTP_RULE_RES_BADREQ if a parsing error is raised by lower level
Ilya Shipitsinb2be9a12021-04-24 13:25:42 +05004043 * - HTTP_RULE_RES_ERROR if an internal error occurred
Christopher Faulet021a8e42021-03-29 10:46:38 +02004044 *
Ilya Shipitsinb2be9a12021-04-24 13:25:42 +05004045 * If a timeout occurred, this function is responsible to emit the right response
Christopher Faulet021a8e42021-03-29 10:46:38 +02004046 * to the client, depending on the channel (408 on request side, 504 on response
4047 * side). All other errors must be handled by the caller.
4048 */
4049enum rule_result http_wait_for_msg_body(struct stream *s, struct channel *chn,
4050 unsigned int time, unsigned int bytes)
4051{
4052 struct session *sess = s->sess;
4053 struct http_txn *txn = s->txn;
4054 struct http_msg *msg = ((chn->flags & CF_ISRESP) ? &txn->rsp : &txn->req);
4055 struct htx *htx;
4056 enum rule_result ret = HTTP_RULE_RES_CONT;
4057
4058 htx = htxbuf(&chn->buf);
4059
4060 if (htx->flags & HTX_FL_PARSING_ERROR) {
4061 ret = HTTP_RULE_RES_BADREQ;
4062 goto end;
4063 }
4064 if (htx->flags & HTX_FL_PROCESSING_ERROR) {
4065 ret = HTTP_RULE_RES_ERROR;
4066 goto end;
4067 }
4068
4069 /* Do nothing for bodyless and CONNECT requests */
4070 if (txn->meth == HTTP_METH_CONNECT || (msg->flags & HTTP_MSGF_BODYLESS))
4071 goto end;
4072
4073 if (!(chn->flags & CF_ISRESP) && msg->msg_state < HTTP_MSG_DATA) {
4074 if (http_handle_expect_hdr(s, htx, msg) == -1) {
4075 ret = HTTP_RULE_RES_ERROR;
4076 goto end;
4077 }
4078 }
4079
4080 msg->msg_state = HTTP_MSG_DATA;
4081
4082 /* Now we're in HTTP_MSG_DATA. We just need to know if all data have
4083 * been received or if the buffer is full.
4084 */
4085 if ((htx->flags & HTX_FL_EOM) || htx_get_tail_type(htx) > HTX_BLK_DATA ||
4086 channel_htx_full(chn, htx, global.tune.maxrewrite))
4087 goto end;
4088
4089 if (bytes) {
4090 struct htx_blk *blk;
4091 unsigned int len = 0;
4092
4093 for (blk = htx_get_first_blk(htx); blk; blk = htx_get_next_blk(htx, blk)) {
4094 if (htx_get_blk_type(blk) != HTX_BLK_DATA)
4095 continue;
4096 len += htx_get_blksz(blk);
4097 if (len >= bytes)
4098 goto end;
4099 }
4100 }
4101
4102 if ((chn->flags & CF_READ_TIMEOUT) || tick_is_expired(chn->analyse_exp, now_ms)) {
4103 if (!(chn->flags & CF_ISRESP))
4104 goto abort_req;
4105 goto abort_res;
4106 }
4107
4108 /* we get here if we need to wait for more data */
4109 if (!(chn->flags & (CF_SHUTR | CF_READ_ERROR))) {
4110 if (!tick_isset(chn->analyse_exp))
4111 chn->analyse_exp = tick_add_ifset(now_ms, time);
4112 ret = HTTP_RULE_RES_YIELD;
4113 }
4114
4115 end:
4116 return ret;
4117
4118 abort_req:
4119 txn->status = 408;
4120 if (!(s->flags & SF_ERR_MASK))
4121 s->flags |= SF_ERR_CLITO;
4122 if (!(s->flags & SF_FINST_MASK))
4123 s->flags |= SF_FINST_D;
Willy Tarreau4781b152021-04-06 13:53:36 +02004124 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req);
Christopher Faulet021a8e42021-03-29 10:46:38 +02004125 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02004126 _HA_ATOMIC_INC(&sess->listener->counters->failed_req);
Christopher Faulet021a8e42021-03-29 10:46:38 +02004127 http_reply_and_close(s, txn->status, http_error_message(s));
4128 ret = HTTP_RULE_RES_ABRT;
4129 goto end;
4130
4131 abort_res:
4132 txn->status = 504;
4133 if (!(s->flags & SF_ERR_MASK))
4134 s->flags |= SF_ERR_SRVTO;
4135 if (!(s->flags & SF_FINST_MASK))
4136 s->flags |= SF_FINST_D;
4137 stream_inc_http_fail_ctr(s);
4138 http_reply_and_close(s, txn->status, http_error_message(s));
4139 ret = HTTP_RULE_RES_ABRT;
4140 goto end;
4141}
4142
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004143void http_perform_server_redirect(struct stream *s, struct stream_interface *si)
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004144{
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004145 struct channel *req = &s->req;
4146 struct channel *res = &s->res;
4147 struct server *srv;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004148 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004149 struct htx_sl *sl;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004150 struct ist path, location;
4151 unsigned int flags;
Amaury Denoyellec453f952021-07-06 11:40:12 +02004152 struct http_uri_parser parser;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004153
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004154 /*
4155 * Create the location
4156 */
4157 chunk_reset(&trash);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004158
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004159 /* 1: add the server's prefix */
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004160 /* special prefix "/" means don't change URL */
4161 srv = __objt_server(s->target);
4162 if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') {
4163 if (!chunk_memcat(&trash, srv->rdr_pfx, srv->rdr_len))
4164 return;
4165 }
4166
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004167 /* 2: add the request Path */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004168 htx = htxbuf(&req->buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02004169 sl = http_get_stline(htx);
Amaury Denoyellec453f952021-07-06 11:40:12 +02004170 parser = http_uri_parser_init(htx_sl_req_uri(sl));
4171 path = http_parse_path(&parser);
Tim Duesterhused526372020-03-05 17:56:33 +01004172 if (!isttest(path))
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004173 return;
4174
4175 if (!chunk_memcat(&trash, path.ptr, path.len))
4176 return;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004177 location = ist2(trash.area, trash.data);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004178
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004179 /*
4180 * Create the 302 respone
4181 */
4182 htx = htx_from_buf(&res->buf);
4183 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
4184 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
4185 ist("HTTP/1.1"), ist("302"), ist("Found"));
4186 if (!sl)
4187 goto fail;
4188 sl->info.res.status = 302;
4189 s->txn->status = 302;
4190
4191 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) ||
4192 !htx_add_header(htx, ist("Connection"), ist("close")) ||
4193 !htx_add_header(htx, ist("Content-length"), ist("0")) ||
4194 !htx_add_header(htx, ist("Location"), location))
4195 goto fail;
4196
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004197 if (!htx_add_endof(htx, HTX_BLK_EOH))
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004198 goto fail;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004199
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004200 htx->flags |= HTX_FL_EOM;
Christopher Fauletc20afb82020-01-24 19:16:26 +01004201 htx_to_buf(htx, &res->buf);
Christopher Fauleta72a7e42020-01-28 09:28:11 +01004202 if (!http_forward_proxy_resp(s, 1))
4203 goto fail;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004204
4205 /* return without error. */
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004206 si_shutr(si);
4207 si_shutw(si);
4208 si->err_type = SI_ET_NONE;
4209 si->state = SI_ST_CLO;
4210
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004211 if (!(s->flags & SF_ERR_MASK))
4212 s->flags |= SF_ERR_LOCAL;
4213 if (!(s->flags & SF_FINST_MASK))
4214 s->flags |= SF_FINST_C;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004215
4216 /* FIXME: we should increase a counter of redirects per server and per backend. */
4217 srv_inc_sess_ctr(srv);
4218 srv_set_sess_last(srv);
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004219 return;
4220
4221 fail:
4222 /* If an error occurred, remove the incomplete HTTP response from the
4223 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004224 channel_htx_truncate(res, htx);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004225}
4226
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05004227/* This function terminates the request because it was completely analyzed or
Christopher Fauletf2824e62018-10-01 12:12:37 +02004228 * because an error was triggered during the body forwarding.
4229 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004230static void http_end_request(struct stream *s)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004231{
4232 struct channel *chn = &s->req;
4233 struct http_txn *txn = s->txn;
4234
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004235 DBG_TRACE_ENTER(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004236
Christopher Fauletb42a8b62018-11-19 21:59:00 +01004237 if (unlikely(txn->req.msg_state == HTTP_MSG_ERROR ||
4238 txn->rsp.msg_state == HTTP_MSG_ERROR)) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004239 channel_abort(chn);
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004240 channel_htx_truncate(chn, htxbuf(&chn->buf));
Christopher Fauletf2824e62018-10-01 12:12:37 +02004241 goto end;
4242 }
4243
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004244 if (unlikely(txn->req.msg_state < HTTP_MSG_DONE)) {
4245 DBG_TRACE_DEVEL("waiting end of the request", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004246 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004247 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004248
4249 if (txn->req.msg_state == HTTP_MSG_DONE) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004250 /* No need to read anymore, the request was completely parsed.
4251 * We can shut the read side unless we want to abort_on_close,
4252 * or we have a POST request. The issue with POST requests is
4253 * that some browsers still send a CRLF after the request, and
4254 * this CRLF must be read so that it does not remain in the kernel
4255 * buffers, otherwise a close could cause an RST on some systems
4256 * (eg: Linux).
4257 */
Christopher Faulet769d0e92019-03-22 14:23:18 +01004258 if (!(s->be->options & PR_O_ABRT_CLOSE) && txn->meth != HTTP_METH_POST)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004259 channel_dont_read(chn);
4260
4261 /* if the server closes the connection, we want to immediately react
4262 * and close the socket to save packets and syscalls.
4263 */
4264 s->si[1].flags |= SI_FL_NOHALF;
4265
4266 /* In any case we've finished parsing the request so we must
4267 * disable Nagle when sending data because 1) we're not going
4268 * to shut this side, and 2) the server is waiting for us to
4269 * send pending data.
4270 */
4271 chn->flags |= CF_NEVER_WAIT;
4272
Christopher Fauletd01ce402019-01-02 17:44:13 +01004273 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
4274 /* The server has not finished to respond, so we
4275 * don't want to move in order not to upset it.
4276 */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004277 DBG_TRACE_DEVEL("waiting end of the response", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletd01ce402019-01-02 17:44:13 +01004278 return;
4279 }
4280
Christopher Fauletf2824e62018-10-01 12:12:37 +02004281 /* When we get here, it means that both the request and the
4282 * response have finished receiving. Depending on the connection
4283 * mode, we'll have to wait for the last bytes to leave in either
4284 * direction, and sometimes for a close to be effective.
4285 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02004286 if (txn->flags & TX_CON_WANT_TUN) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004287 /* Tunnel mode will not have any analyser so it needs to
4288 * poll for reads.
4289 */
4290 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004291 if (b_data(&chn->buf)) {
4292 DBG_TRACE_DEVEL("waiting to flush the request", STRM_EV_HTTP_ANA, s, txn);
Christopher Faulet9768c262018-10-22 09:34:31 +02004293 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004294 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004295 txn->req.msg_state = HTTP_MSG_TUNNEL;
4296 }
4297 else {
4298 /* we're not expecting any new data to come for this
4299 * transaction, so we can close it.
Christopher Faulet9768c262018-10-22 09:34:31 +02004300 *
4301 * However, there is an exception if the response
4302 * length is undefined. In this case, we need to wait
4303 * the close from the server. The response will be
4304 * switched in TUNNEL mode until the end.
Christopher Fauletf2824e62018-10-01 12:12:37 +02004305 */
4306 if (!(txn->rsp.flags & HTTP_MSGF_XFER_LEN) &&
4307 txn->rsp.msg_state != HTTP_MSG_CLOSED)
Christopher Faulet9768c262018-10-22 09:34:31 +02004308 goto check_channel_flags;
Christopher Fauletf2824e62018-10-01 12:12:37 +02004309
4310 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4311 channel_shutr_now(chn);
4312 channel_shutw_now(chn);
4313 }
4314 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004315 goto check_channel_flags;
4316 }
4317
4318 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
4319 http_msg_closing:
4320 /* nothing else to forward, just waiting for the output buffer
4321 * to be empty and for the shutw_now to take effect.
4322 */
4323 if (channel_is_empty(chn)) {
4324 txn->req.msg_state = HTTP_MSG_CLOSED;
4325 goto http_msg_closed;
4326 }
4327 else if (chn->flags & CF_SHUTW) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004328 txn->req.msg_state = HTTP_MSG_ERROR;
4329 goto end;
4330 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004331 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004332 return;
4333 }
4334
4335 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
4336 http_msg_closed:
Christopher Fauletf2824e62018-10-01 12:12:37 +02004337 /* if we don't know whether the server will close, we need to hard close */
4338 if (txn->rsp.flags & HTTP_MSGF_XFER_LEN)
4339 s->si[1].flags |= SI_FL_NOLINGER; /* we want to close ASAP */
Christopher Fauletf2824e62018-10-01 12:12:37 +02004340 /* see above in MSG_DONE why we only do this in these states */
Christopher Faulet769d0e92019-03-22 14:23:18 +01004341 if (!(s->be->options & PR_O_ABRT_CLOSE))
Christopher Fauletf2824e62018-10-01 12:12:37 +02004342 channel_dont_read(chn);
4343 goto end;
4344 }
4345
4346 check_channel_flags:
4347 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4348 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4349 /* if we've just closed an output, let's switch */
4350 txn->req.msg_state = HTTP_MSG_CLOSING;
4351 goto http_msg_closing;
4352 }
4353
4354 end:
4355 chn->analysers &= AN_REQ_FLT_END;
Christopher Faulet198ef8b2020-12-15 13:32:55 +01004356 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
4357 chn->flags |= CF_NEVER_WAIT;
4358 if (HAS_REQ_DATA_FILTERS(s))
Christopher Fauletf2824e62018-10-01 12:12:37 +02004359 chn->analysers |= AN_REQ_FLT_XFER_DATA;
Christopher Faulet198ef8b2020-12-15 13:32:55 +01004360 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004361 channel_auto_close(chn);
4362 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004363 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004364}
4365
4366
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05004367/* This function terminates the response because it was completely analyzed or
Christopher Fauletf2824e62018-10-01 12:12:37 +02004368 * because an error was triggered during the body forwarding.
4369 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004370static void http_end_response(struct stream *s)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004371{
4372 struct channel *chn = &s->res;
4373 struct http_txn *txn = s->txn;
4374
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004375 DBG_TRACE_ENTER(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004376
Christopher Fauletb42a8b62018-11-19 21:59:00 +01004377 if (unlikely(txn->req.msg_state == HTTP_MSG_ERROR ||
4378 txn->rsp.msg_state == HTTP_MSG_ERROR)) {
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004379 channel_htx_truncate(&s->req, htxbuf(&s->req.buf));
Christopher Faulet9768c262018-10-22 09:34:31 +02004380 channel_abort(&s->req);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004381 goto end;
4382 }
4383
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004384 if (unlikely(txn->rsp.msg_state < HTTP_MSG_DONE)) {
4385 DBG_TRACE_DEVEL("waiting end of the response", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004386 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004387 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004388
4389 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
4390 /* In theory, we don't need to read anymore, but we must
4391 * still monitor the server connection for a possible close
4392 * while the request is being uploaded, so we don't disable
4393 * reading.
4394 */
4395 /* channel_dont_read(chn); */
4396
4397 if (txn->req.msg_state < HTTP_MSG_DONE) {
4398 /* The client seems to still be sending data, probably
4399 * because we got an error response during an upload.
4400 * We have the choice of either breaking the connection
4401 * or letting it pass through. Let's do the later.
4402 */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004403 DBG_TRACE_DEVEL("waiting end of the request", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004404 return;
4405 }
4406
4407 /* When we get here, it means that both the request and the
4408 * response have finished receiving. Depending on the connection
4409 * mode, we'll have to wait for the last bytes to leave in either
4410 * direction, and sometimes for a close to be effective.
4411 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02004412 if (txn->flags & TX_CON_WANT_TUN) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004413 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004414 if (b_data(&chn->buf)) {
4415 DBG_TRACE_DEVEL("waiting to flush the respone", STRM_EV_HTTP_ANA, s, txn);
Christopher Faulet9768c262018-10-22 09:34:31 +02004416 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004417 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004418 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
4419 }
4420 else {
4421 /* we're not expecting any new data to come for this
4422 * transaction, so we can close it.
4423 */
4424 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4425 channel_shutr_now(chn);
4426 channel_shutw_now(chn);
4427 }
4428 }
4429 goto check_channel_flags;
4430 }
4431
4432 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
4433 http_msg_closing:
4434 /* nothing else to forward, just waiting for the output buffer
4435 * to be empty and for the shutw_now to take effect.
4436 */
4437 if (channel_is_empty(chn)) {
4438 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4439 goto http_msg_closed;
4440 }
4441 else if (chn->flags & CF_SHUTW) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004442 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau4781b152021-04-06 13:53:36 +02004443 _HA_ATOMIC_INC(&strm_sess(s)->fe->fe_counters.cli_aborts);
4444 _HA_ATOMIC_INC(&s->be->be_counters.cli_aborts);
William Lallemand36119de2021-03-08 15:26:48 +01004445 if (strm_sess(s)->listener && strm_sess(s)->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02004446 _HA_ATOMIC_INC(&strm_sess(s)->listener->counters->cli_aborts);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004447 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02004448 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.cli_aborts);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004449 goto end;
4450 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004451 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004452 return;
4453 }
4454
4455 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
4456 http_msg_closed:
4457 /* drop any pending data */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004458 channel_htx_truncate(&s->req, htxbuf(&s->req.buf));
Christopher Faulet9768c262018-10-22 09:34:31 +02004459 channel_abort(&s->req);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004460 goto end;
4461 }
4462
4463 check_channel_flags:
4464 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4465 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4466 /* if we've just closed an output, let's switch */
4467 txn->rsp.msg_state = HTTP_MSG_CLOSING;
4468 goto http_msg_closing;
4469 }
4470
4471 end:
4472 chn->analysers &= AN_RES_FLT_END;
Christopher Faulet198ef8b2020-12-15 13:32:55 +01004473 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
4474 chn->flags |= CF_NEVER_WAIT;
4475 if (HAS_RSP_DATA_FILTERS(s))
4476 chn->analysers |= AN_RES_FLT_XFER_DATA;
4477 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004478 channel_auto_close(chn);
4479 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004480 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004481}
4482
Christopher Fauletef70e252020-01-28 09:26:19 +01004483/* Forward a response generated by HAProxy (error/redirect/return). This
4484 * function forwards all pending incoming data. If <final> is set to 0, nothing
4485 * more is performed. It is used for 1xx informational messages. Otherwise, the
Christopher Faulet507479b2020-05-15 12:29:46 +02004486 * transaction is terminated and the request is emptied. On success 1 is
Christopher Faulet40e6b552020-06-25 16:04:50 +02004487 * returned. If an error occurred, 0 is returned. If it fails, this function
4488 * only exits. It is the caller responsibility to do the cleanup.
Christopher Fauletef70e252020-01-28 09:26:19 +01004489 */
4490int http_forward_proxy_resp(struct stream *s, int final)
4491{
4492 struct channel *req = &s->req;
4493 struct channel *res = &s->res;
4494 struct htx *htx = htxbuf(&res->buf);
4495 size_t data;
4496
4497 if (final) {
4498 htx->flags |= HTX_FL_PROXY_RESP;
Christopher Faulet507479b2020-05-15 12:29:46 +02004499
Christopher Fauletaab1b672020-11-18 16:44:02 +01004500 if (!htx_is_empty(htx) && !http_eval_after_res_rules(s))
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01004501 return 0;
Christopher Fauletef70e252020-01-28 09:26:19 +01004502
Christopher Fauletd6c48362020-10-19 18:01:38 +02004503 if (s->txn->meth == HTTP_METH_HEAD)
4504 htx_skip_msg_payload(htx);
4505
Christopher Fauletef70e252020-01-28 09:26:19 +01004506 channel_auto_read(req);
4507 channel_abort(req);
4508 channel_auto_close(req);
4509 channel_htx_erase(req, htxbuf(&req->buf));
4510
4511 res->wex = tick_add_ifset(now_ms, res->wto);
4512 channel_auto_read(res);
4513 channel_auto_close(res);
4514 channel_shutr_now(res);
Christopher Faulet1a9db7c2020-06-25 15:36:45 +02004515 res->flags |= CF_EOI; /* The response is terminated, add EOI */
Christopher Faulet42432f32020-11-20 17:43:16 +01004516 htxbuf(&res->buf)->flags |= HTX_FL_EOM; /* no more data are expected */
Christopher Fauletef70e252020-01-28 09:26:19 +01004517 }
Christopher Fauletcf6898c2020-06-25 15:55:11 +02004518 else {
4519 /* Send ASAP informational messages. Rely on CF_EOI for final
4520 * response.
4521 */
4522 res->flags |= CF_SEND_DONTWAIT;
4523 }
Christopher Fauletef70e252020-01-28 09:26:19 +01004524
4525 data = htx->data - co_data(res);
4526 c_adv(res, data);
4527 htx->first = -1;
4528 res->total += data;
4529 return 1;
4530}
4531
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004532void http_server_error(struct stream *s, struct stream_interface *si, int err,
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004533 int finst, struct http_reply *msg)
Christopher Faulet0f226952018-10-22 09:29:56 +02004534{
Christopher Faulet72c7d8d2020-01-27 15:32:25 +01004535 http_reply_and_close(s, s->txn->status, msg);
Christopher Faulet0f226952018-10-22 09:29:56 +02004536 if (!(s->flags & SF_ERR_MASK))
4537 s->flags |= err;
4538 if (!(s->flags & SF_FINST_MASK))
4539 s->flags |= finst;
4540}
4541
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004542void http_reply_and_close(struct stream *s, short status, struct http_reply *msg)
Christopher Faulet0f226952018-10-22 09:29:56 +02004543{
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004544 if (!msg) {
4545 channel_htx_truncate(&s->res, htxbuf(&s->res.buf));
4546 goto end;
4547 }
4548
4549 if (http_reply_message(s, msg) == -1) {
4550 /* On error, return a 500 error message, but don't rewrite it if
Christopher Faulet40e6b552020-06-25 16:04:50 +02004551 * it is already an internal error. If it was already a "const"
4552 * 500 error, just fail.
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004553 */
Christopher Faulet40e6b552020-06-25 16:04:50 +02004554 if (s->txn->status == 500) {
4555 if (s->txn->flags & TX_CONST_REPLY)
4556 goto end;
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004557 s->txn->flags |= TX_CONST_REPLY;
Christopher Faulet40e6b552020-06-25 16:04:50 +02004558 }
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004559 s->txn->status = 500;
4560 s->txn->http_reply = NULL;
4561 return http_reply_and_close(s, s->txn->status, http_error_message(s));
4562 }
4563
4564end:
4565 s->res.wex = tick_add_ifset(now_ms, s->res.wto);
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004566
Christopher Faulet0f226952018-10-22 09:29:56 +02004567 channel_auto_read(&s->req);
4568 channel_abort(&s->req);
4569 channel_auto_close(&s->req);
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004570 channel_htx_erase(&s->req, htxbuf(&s->req.buf));
Christopher Faulet72c7d8d2020-01-27 15:32:25 +01004571 channel_auto_read(&s->res);
4572 channel_auto_close(&s->res);
4573 channel_shutr_now(&s->res);
Christopher Faulet0f226952018-10-22 09:29:56 +02004574}
4575
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004576struct http_reply *http_error_message(struct stream *s)
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004577{
4578 const int msgnum = http_get_status_idx(s->txn->status);
4579
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004580 if (s->txn->http_reply)
4581 return s->txn->http_reply;
4582 else if (s->be->replies[msgnum])
4583 return s->be->replies[msgnum];
4584 else if (strm_fe(s)->replies[msgnum])
4585 return strm_fe(s)->replies[msgnum];
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004586 else
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004587 return &http_err_replies[msgnum];
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004588}
4589
Christopher Faulet40e6b552020-06-25 16:04:50 +02004590/* Produces an HTX message from an http reply. Depending on the http reply type,
4591 * a, errorfile, an raw file or a log-format string is used. On success, it
4592 * returns 0. If an error occurs -1 is returned. If it fails, this function only
4593 * exits. It is the caller responsibility to do the cleanup.
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004594 */
Christopher Fauletae43b6c2020-05-27 15:24:22 +02004595int http_reply_to_htx(struct stream *s, struct htx *htx, struct http_reply *reply)
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004596{
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004597 struct buffer *errmsg;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004598 struct htx_sl *sl;
4599 struct buffer *body = NULL;
4600 const char *status, *reason, *clen, *ctype;
4601 unsigned int slflags;
4602 int ret = 0;
4603
Christopher Faulete29a97e2020-05-14 14:49:25 +02004604 /*
4605 * - HTTP_REPLY_ERRFILES unexpected here. handled as no payload if so
4606 *
4607 * - HTTP_REPLY_INDIRECT: switch on another reply if defined or handled
4608 * as no payload if NULL. the TXN status code is set with the status
4609 * of the original reply.
4610 */
4611
4612 if (reply->type == HTTP_REPLY_INDIRECT) {
4613 if (reply->body.reply)
4614 reply = reply->body.reply;
4615 }
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004616 if (reply->type == HTTP_REPLY_ERRMSG && !reply->body.errmsg) {
4617 /* get default error message */
4618 if (reply == s->txn->http_reply)
4619 s->txn->http_reply = NULL;
4620 reply = http_error_message(s);
4621 if (reply->type == HTTP_REPLY_INDIRECT) {
4622 if (reply->body.reply)
4623 reply = reply->body.reply;
4624 }
4625 }
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004626
4627 if (reply->type == HTTP_REPLY_ERRMSG) {
4628 /* implicit or explicit error message*/
4629 errmsg = reply->body.errmsg;
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004630 if (errmsg && !b_is_null(errmsg)) {
Christopher Faulet20567362020-05-15 14:52:49 +02004631 if (!htx_copy_msg(htx, errmsg))
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004632 goto fail;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004633 }
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004634 }
4635 else {
4636 /* no payload, file or log-format string */
4637 if (reply->type == HTTP_REPLY_RAW) {
4638 /* file */
4639 body = &reply->body.obj;
4640 }
4641 else if (reply->type == HTTP_REPLY_LOGFMT) {
4642 /* log-format string */
4643 body = alloc_trash_chunk();
4644 if (!body)
4645 goto fail_alloc;
4646 body->data = build_logline(s, body->area, body->size, &reply->body.fmt);
4647 }
4648 /* else no payload */
4649
4650 status = ultoa(reply->status);
4651 reason = http_get_reason(reply->status);
4652 slflags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_CLEN);
4653 if (!body || !b_data(body))
4654 slflags |= HTX_SL_F_BODYLESS;
4655 sl = htx_add_stline(htx, HTX_BLK_RES_SL, slflags, ist("HTTP/1.1"), ist(status), ist(reason));
4656 if (!sl)
4657 goto fail;
4658 sl->info.res.status = reply->status;
4659
4660 clen = (body ? ultoa(b_data(body)) : "0");
4661 ctype = reply->ctype;
4662
4663 if (!LIST_ISEMPTY(&reply->hdrs)) {
4664 struct http_reply_hdr *hdr;
4665 struct buffer *value = alloc_trash_chunk();
4666
4667 if (!value)
4668 goto fail;
4669
4670 list_for_each_entry(hdr, &reply->hdrs, list) {
4671 chunk_reset(value);
4672 value->data = build_logline(s, value->area, value->size, &hdr->value);
4673 if (b_data(value) && !htx_add_header(htx, hdr->name, ist2(b_head(value), b_data(value)))) {
4674 free_trash_chunk(value);
4675 goto fail;
4676 }
4677 chunk_reset(value);
4678 }
4679 free_trash_chunk(value);
4680 }
4681
4682 if (!htx_add_header(htx, ist("content-length"), ist(clen)) ||
4683 (body && b_data(body) && ctype && !htx_add_header(htx, ist("content-type"), ist(ctype))) ||
4684 !htx_add_endof(htx, HTX_BLK_EOH) ||
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004685 (body && b_data(body) && !htx_add_data_atonce(htx, ist2(b_head(body), b_data(body)))))
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004686 goto fail;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004687
4688 htx->flags |= HTX_FL_EOM;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004689 }
4690
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004691 leave:
4692 if (reply->type == HTTP_REPLY_LOGFMT)
4693 free_trash_chunk(body);
4694 return ret;
4695
4696 fail_alloc:
4697 if (!(s->flags & SF_ERR_MASK))
4698 s->flags |= SF_ERR_RESOURCE;
Christopher Faulet97e466c2020-05-15 15:12:47 +02004699 /* fall through */
4700 fail:
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004701 ret = -1;
4702 goto leave;
Christopher Faulet97e466c2020-05-15 15:12:47 +02004703}
4704
4705/* Send an http reply to the client. On success, it returns 0. If an error
Christopher Faulet40e6b552020-06-25 16:04:50 +02004706 * occurs -1 is returned and the response channel is truncated, removing this
4707 * way the faulty reply. This function may fail when the reply is formatted
4708 * (http_reply_to_htx) or when the reply is forwarded
4709 * (http_forward_proxy_resp). On the last case, it is because a
4710 * http-after-response rule fails.
Christopher Faulet97e466c2020-05-15 15:12:47 +02004711 */
4712int http_reply_message(struct stream *s, struct http_reply *reply)
4713{
4714 struct channel *res = &s->res;
4715 struct htx *htx = htx_from_buf(&res->buf);
4716
4717 if (s->txn->status == -1)
4718 s->txn->status = reply->status;
4719 channel_htx_truncate(res, htx);
4720
4721 if (http_reply_to_htx(s, htx, reply) == -1)
4722 goto fail;
4723
4724 htx_to_buf(htx, &s->res.buf);
4725 if (!http_forward_proxy_resp(s, 1))
4726 goto fail;
4727 return 0;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004728
4729 fail:
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004730 channel_htx_truncate(res, htx);
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004731 if (!(s->flags & SF_ERR_MASK))
4732 s->flags |= SF_ERR_PRXCOND;
Christopher Faulet97e466c2020-05-15 15:12:47 +02004733 return -1;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004734}
4735
Christopher Faulet304cc402019-07-15 15:46:28 +02004736/* Return the error message corresponding to si->err_type. It is assumed
4737 * that the server side is closed. Note that err_type is actually a
4738 * bitmask, where almost only aborts may be cumulated with other
4739 * values. We consider that aborted operations are more important
4740 * than timeouts or errors due to the fact that nobody else in the
4741 * logs might explain incomplete retries. All others should avoid
4742 * being cumulated. It should normally not be possible to have multiple
4743 * aborts at once, but just in case, the first one in sequence is reported.
4744 * Note that connection errors appearing on the second request of a keep-alive
4745 * connection are not reported since this allows the client to retry.
4746 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004747void http_return_srv_error(struct stream *s, struct stream_interface *si)
Christopher Faulet304cc402019-07-15 15:46:28 +02004748{
4749 int err_type = si->err_type;
4750
4751 /* set s->txn->status for http_error_message(s) */
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004752 if (err_type & SI_ET_QUEUE_ABRT) {
4753 s->txn->status = -1;
4754 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_Q, NULL);
4755 }
4756 else if (err_type & SI_ET_CONN_ABRT) {
4757 s->txn->status = -1;
4758 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_C, NULL);
4759 }
4760 else if (err_type & SI_ET_QUEUE_TO) {
4761 s->txn->status = 503;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004762 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_Q,
4763 http_error_message(s));
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004764 }
4765 else if (err_type & SI_ET_QUEUE_ERR) {
4766 s->txn->status = 503;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004767 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_Q,
4768 http_error_message(s));
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004769 }
4770 else if (err_type & SI_ET_CONN_TO) {
4771 s->txn->status = 503;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004772 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_C,
4773 (s->txn->flags & TX_NOT_FIRST) ? NULL :
4774 http_error_message(s));
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004775 }
4776 else if (err_type & SI_ET_CONN_ERR) {
4777 s->txn->status = 503;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004778 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_C,
4779 (s->flags & SF_SRV_REUSED) ? NULL :
4780 http_error_message(s));
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004781 }
4782 else if (err_type & SI_ET_CONN_RES) {
4783 s->txn->status = 503;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004784 http_server_error(s, si, SF_ERR_RESOURCE, SF_FINST_C,
4785 (s->txn->flags & TX_NOT_FIRST) ? NULL :
4786 http_error_message(s));
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004787 }
Christopher Faulet304cc402019-07-15 15:46:28 +02004788 else { /* SI_ET_CONN_OTHER and others */
4789 s->txn->status = 500;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004790 http_server_error(s, si, SF_ERR_INTERNAL, SF_FINST_C,
4791 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004792 }
4793}
4794
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004795
Christopher Faulet4a28a532019-03-01 11:19:40 +01004796/* Handle Expect: 100-continue for HTTP/1.1 messages if necessary. It returns 0
4797 * on success and -1 on error.
4798 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004799static int http_handle_expect_hdr(struct stream *s, struct htx *htx, struct http_msg *msg)
Christopher Faulet4a28a532019-03-01 11:19:40 +01004800{
4801 /* If we have HTTP/1.1 message with a body and Expect: 100-continue,
4802 * then we must send an HTTP/1.1 100 Continue intermediate response.
4803 */
4804 if (msg->msg_state == HTTP_MSG_BODY && (msg->flags & HTTP_MSGF_VER_11) &&
4805 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
4806 struct ist hdr = { .ptr = "Expect", .len = 6 };
4807 struct http_hdr_ctx ctx;
4808
4809 ctx.blk = NULL;
4810 /* Expect is allowed in 1.1, look for it */
4811 if (http_find_header(htx, hdr, &ctx, 0) &&
4812 unlikely(isteqi(ctx.value, ist2("100-continue", 12)))) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004813 if (http_reply_100_continue(s) == -1)
Christopher Faulet4a28a532019-03-01 11:19:40 +01004814 return -1;
4815 http_remove_header(htx, &ctx);
4816 }
4817 }
4818 return 0;
4819}
4820
Christopher Faulet23a3c792018-11-28 10:01:23 +01004821/* Send a 100-Continue response to the client. It returns 0 on success and -1
4822 * on error. The response channel is updated accordingly.
4823 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004824static int http_reply_100_continue(struct stream *s)
Christopher Faulet23a3c792018-11-28 10:01:23 +01004825{
4826 struct channel *res = &s->res;
4827 struct htx *htx = htx_from_buf(&res->buf);
4828 struct htx_sl *sl;
4829 unsigned int flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|
4830 HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
Christopher Faulet23a3c792018-11-28 10:01:23 +01004831
4832 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
4833 ist("HTTP/1.1"), ist("100"), ist("Continue"));
4834 if (!sl)
4835 goto fail;
4836 sl->info.res.status = 100;
4837
Christopher Faulet1d5ec092019-06-26 14:23:54 +02004838 if (!htx_add_endof(htx, HTX_BLK_EOH))
Christopher Faulet23a3c792018-11-28 10:01:23 +01004839 goto fail;
4840
Christopher Fauleta72a7e42020-01-28 09:28:11 +01004841 if (!http_forward_proxy_resp(s, 0))
4842 goto fail;
Christopher Faulet23a3c792018-11-28 10:01:23 +01004843 return 0;
4844
4845 fail:
4846 /* If an error occurred, remove the incomplete HTTP response from the
4847 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004848 channel_htx_truncate(res, htx);
Christopher Faulet23a3c792018-11-28 10:01:23 +01004849 return -1;
4850}
4851
Christopher Faulet12c51e22018-11-28 15:59:42 +01004852
Christopher Faulet0f226952018-10-22 09:29:56 +02004853/*
4854 * Capture headers from message <htx> according to header list <cap_hdr>, and
4855 * fill the <cap> pointers appropriately.
4856 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004857static void http_capture_headers(struct htx *htx, char **cap, struct cap_hdr *cap_hdr)
Christopher Faulet0f226952018-10-22 09:29:56 +02004858{
4859 struct cap_hdr *h;
4860 int32_t pos;
4861
Christopher Fauleta3f15502019-05-13 15:27:23 +02004862 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet0f226952018-10-22 09:29:56 +02004863 struct htx_blk *blk = htx_get_blk(htx, pos);
4864 enum htx_blk_type type = htx_get_blk_type(blk);
4865 struct ist n, v;
4866
4867 if (type == HTX_BLK_EOH)
4868 break;
4869 if (type != HTX_BLK_HDR)
4870 continue;
4871
4872 n = htx_get_blk_name(htx, blk);
4873
4874 for (h = cap_hdr; h; h = h->next) {
4875 if (h->namelen && (h->namelen == n.len) &&
4876 (strncasecmp(n.ptr, h->name, h->namelen) == 0)) {
4877 if (cap[h->index] == NULL)
4878 cap[h->index] =
4879 pool_alloc(h->pool);
4880
4881 if (cap[h->index] == NULL) {
4882 ha_alert("HTTP capture : out of memory.\n");
4883 break;
4884 }
4885
4886 v = htx_get_blk_value(htx, blk);
4887 if (v.len > h->len)
4888 v.len = h->len;
4889
4890 memcpy(cap[h->index], v.ptr, v.len);
4891 cap[h->index][v.len]=0;
4892 }
4893 }
4894 }
4895}
4896
Christopher Faulet0b6bdc52018-10-24 11:05:36 +02004897/* Delete a value in a header between delimiters <from> and <next>. The header
4898 * itself is delimited by <start> and <end> pointers. The number of characters
4899 * displaced is returned, and the pointer to the first delimiter is updated if
4900 * required. The function tries as much as possible to respect the following
4901 * principles :
4902 * - replace <from> delimiter by the <next> one unless <from> points to <start>,
4903 * in which case <next> is simply removed
4904 * - set exactly one space character after the new first delimiter, unless there
4905 * are not enough characters in the block being moved to do so.
4906 * - remove unneeded spaces before the previous delimiter and after the new
4907 * one.
4908 *
4909 * It is the caller's responsibility to ensure that :
4910 * - <from> points to a valid delimiter or <start> ;
4911 * - <next> points to a valid delimiter or <end> ;
4912 * - there are non-space chars before <from>.
4913 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004914static int http_del_hdr_value(char *start, char *end, char **from, char *next)
Christopher Faulet0b6bdc52018-10-24 11:05:36 +02004915{
4916 char *prev = *from;
4917
4918 if (prev == start) {
4919 /* We're removing the first value. eat the semicolon, if <next>
4920 * is lower than <end> */
4921 if (next < end)
4922 next++;
4923
4924 while (next < end && HTTP_IS_SPHT(*next))
4925 next++;
4926 }
4927 else {
4928 /* Remove useless spaces before the old delimiter. */
4929 while (HTTP_IS_SPHT(*(prev-1)))
4930 prev--;
4931 *from = prev;
4932
4933 /* copy the delimiter and if possible a space if we're
4934 * not at the end of the line.
4935 */
4936 if (next < end) {
4937 *prev++ = *next++;
4938 if (prev + 1 < next)
4939 *prev++ = ' ';
4940 while (next < end && HTTP_IS_SPHT(*next))
4941 next++;
4942 }
4943 }
4944 memmove(prev, next, end - next);
4945 return (prev - next);
4946}
4947
Christopher Faulet0f226952018-10-22 09:29:56 +02004948
4949/* Formats the start line of the request (without CRLF) and puts it in <str> and
Joseph Herlantc42c0e92018-11-25 10:43:27 -08004950 * return the written length. The line can be truncated if it exceeds <len>.
Christopher Faulet0f226952018-10-22 09:29:56 +02004951 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004952static size_t http_fmt_req_line(const struct htx_sl *sl, char *str, size_t len)
Christopher Faulet0f226952018-10-22 09:29:56 +02004953{
4954 struct ist dst = ist2(str, 0);
4955
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004956 if (istcat(&dst, htx_sl_req_meth(sl), len) == -1)
Christopher Faulet0f226952018-10-22 09:29:56 +02004957 goto end;
4958 if (dst.len + 1 > len)
4959 goto end;
4960 dst.ptr[dst.len++] = ' ';
4961
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004962 if (istcat(&dst, htx_sl_req_uri(sl), len) == -1)
Christopher Faulet0f226952018-10-22 09:29:56 +02004963 goto end;
4964 if (dst.len + 1 > len)
4965 goto end;
4966 dst.ptr[dst.len++] = ' ';
4967
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004968 istcat(&dst, htx_sl_req_vsn(sl), len);
Christopher Faulet0f226952018-10-22 09:29:56 +02004969 end:
4970 return dst.len;
4971}
4972
4973/*
4974 * Print a debug line with a start line.
4975 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004976static void http_debug_stline(const char *dir, struct stream *s, const struct htx_sl *sl)
Christopher Faulet0f226952018-10-22 09:29:56 +02004977{
4978 struct session *sess = strm_sess(s);
4979 int max;
4980
4981 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
4982 dir,
4983 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->handle.fd : -1,
4984 objt_cs(s->si[1].end) ? (unsigned short)objt_cs(s->si[1].end)->conn->handle.fd : -1);
4985
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004986 max = HTX_SL_P1_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02004987 UBOUND(max, trash.size - trash.data - 3);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004988 chunk_memcat(&trash, HTX_SL_P1_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02004989 trash.area[trash.data++] = ' ';
4990
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004991 max = HTX_SL_P2_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02004992 UBOUND(max, trash.size - trash.data - 2);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004993 chunk_memcat(&trash, HTX_SL_P2_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02004994 trash.area[trash.data++] = ' ';
4995
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004996 max = HTX_SL_P3_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02004997 UBOUND(max, trash.size - trash.data - 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004998 chunk_memcat(&trash, HTX_SL_P3_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02004999 trash.area[trash.data++] = '\n';
5000
Willy Tarreau2e8ab6b2020-03-14 11:03:20 +01005001 DISGUISE(write(1, trash.area, trash.data));
Christopher Faulet0f226952018-10-22 09:29:56 +02005002}
5003
5004/*
5005 * Print a debug line with a header.
5006 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005007static 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 +02005008{
5009 struct session *sess = strm_sess(s);
5010 int max;
5011
5012 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
5013 dir,
5014 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->handle.fd : -1,
5015 objt_cs(s->si[1].end) ? (unsigned short)objt_cs(s->si[1].end)->conn->handle.fd : -1);
5016
5017 max = n.len;
5018 UBOUND(max, trash.size - trash.data - 3);
5019 chunk_memcat(&trash, n.ptr, max);
5020 trash.area[trash.data++] = ':';
5021 trash.area[trash.data++] = ' ';
5022
5023 max = v.len;
5024 UBOUND(max, trash.size - trash.data - 1);
5025 chunk_memcat(&trash, v.ptr, max);
5026 trash.area[trash.data++] = '\n';
5027
Willy Tarreau2e8ab6b2020-03-14 11:03:20 +01005028 DISGUISE(write(1, trash.area, trash.data));
Christopher Faulet0f226952018-10-22 09:29:56 +02005029}
5030
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005031/* Allocate a new HTTP transaction for stream <s> unless there is one already.
5032 * In case of allocation failure, everything allocated is freed and NULL is
5033 * returned. Otherwise the new transaction is assigned to the stream and
5034 * returned.
5035 */
5036struct http_txn *http_alloc_txn(struct stream *s)
5037{
5038 struct http_txn *txn = s->txn;
5039
5040 if (txn)
5041 return txn;
5042
5043 txn = pool_alloc(pool_head_http_txn);
5044 if (!txn)
5045 return txn;
5046
5047 s->txn = txn;
5048 return txn;
5049}
5050
5051void http_txn_reset_req(struct http_txn *txn)
5052{
Christopher Faulet1aea50e2020-01-17 16:03:53 +01005053 txn->req.flags = 0;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005054 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
5055}
5056
5057void http_txn_reset_res(struct http_txn *txn)
5058{
Christopher Faulet1aea50e2020-01-17 16:03:53 +01005059 txn->rsp.flags = 0;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005060 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
5061}
5062
5063/*
Christopher Faulet75f619a2021-03-08 19:12:58 +01005064 * Create and initialize a new HTTP transaction for stream <s>. This should be
5065 * used before processing any new request. It returns the transaction or NLULL
5066 * on error.
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005067 */
Christopher Faulet75f619a2021-03-08 19:12:58 +01005068struct http_txn *http_create_txn(struct stream *s)
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005069{
Christopher Faulet75f619a2021-03-08 19:12:58 +01005070 struct http_txn *txn;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005071 struct conn_stream *cs = objt_cs(s->si[0].end);
5072
Christopher Faulet75f619a2021-03-08 19:12:58 +01005073 txn = pool_alloc(pool_head_http_txn);
5074 if (!txn)
5075 return NULL;
5076 s->txn = txn;
5077
Christopher Fauletda831fa2020-10-06 17:58:43 +02005078 txn->flags = ((cs && cs->flags & CS_FL_NOT_FIRST) ? TX_NOT_FIRST : 0);
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005079 txn->status = -1;
Christopher Faulet5cb513a2020-05-13 17:56:56 +02005080 txn->http_reply = NULL;
Willy Tarreau8b507582020-02-25 09:35:07 +01005081 write_u32(txn->cache_hash, 0);
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005082
5083 txn->cookie_first_date = 0;
5084 txn->cookie_last_date = 0;
5085
5086 txn->srv_cookie = NULL;
5087 txn->cli_cookie = NULL;
5088 txn->uri = NULL;
5089
5090 http_txn_reset_req(txn);
5091 http_txn_reset_res(txn);
5092
5093 txn->req.chn = &s->req;
5094 txn->rsp.chn = &s->res;
5095
5096 txn->auth.method = HTTP_AUTH_UNKNOWN;
5097
5098 vars_init(&s->vars_txn, SCOPE_TXN);
5099 vars_init(&s->vars_reqres, SCOPE_REQ);
Christopher Faulet75f619a2021-03-08 19:12:58 +01005100
5101 return txn;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005102}
5103
5104/* to be used at the end of a transaction */
Christopher Faulet75f619a2021-03-08 19:12:58 +01005105void http_destroy_txn(struct stream *s)
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005106{
5107 struct http_txn *txn = s->txn;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005108
5109 /* these ones will have been dynamically allocated */
5110 pool_free(pool_head_requri, txn->uri);
5111 pool_free(pool_head_capture, txn->cli_cookie);
5112 pool_free(pool_head_capture, txn->srv_cookie);
Tim Duesterhusa17e6622020-03-05 20:19:02 +01005113 pool_free(pool_head_uniqueid, s->unique_id.ptr);
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005114
Tim Duesterhusa17e6622020-03-05 20:19:02 +01005115 s->unique_id = IST_NULL;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005116 txn->uri = NULL;
5117 txn->srv_cookie = NULL;
5118 txn->cli_cookie = NULL;
5119
Christopher Faulet59399252019-11-07 14:27:52 +01005120 if (!LIST_ISEMPTY(&s->vars_txn.head))
5121 vars_prune(&s->vars_txn, s->sess, s);
5122 if (!LIST_ISEMPTY(&s->vars_reqres.head))
5123 vars_prune(&s->vars_reqres, s->sess, s);
Christopher Faulet75f619a2021-03-08 19:12:58 +01005124
5125 pool_free(pool_head_http_txn, txn);
5126 s->txn = NULL;
Christopher Faulet59399252019-11-07 14:27:52 +01005127}
5128
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005129
5130DECLARE_POOL(pool_head_http_txn, "http_txn", sizeof(struct http_txn));
Christopher Faulet0f226952018-10-22 09:29:56 +02005131
Christopher Fauletf4eb75d2018-10-11 15:55:07 +02005132__attribute__((constructor))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005133static void __http_protocol_init(void)
Christopher Fauletf4eb75d2018-10-11 15:55:07 +02005134{
5135}
5136
5137
5138/*
5139 * Local variables:
5140 * c-indent-level: 8
5141 * c-basic-offset: 8
5142 * End:
5143 */