blob: 7e32fb8ead2bca0417fd598349f4c7cafd550d4c [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
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100339 DBG_TRACE_DEVEL("leaving on error",
340 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200341 return 0;
342}
343
344
345/* This stream analyser runs all HTTP request processing which is common to
346 * frontends and backends, which means blocking ACLs, filters, connection-close,
347 * reqadd, stats and redirects. This is performed for the designated proxy.
348 * It returns 1 if the processing can continue on next analysers, or zero if it
349 * either needs more data or wants to immediately abort the request (eg: deny,
350 * error, ...).
351 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200352int http_process_req_common(struct stream *s, struct channel *req, int an_bit, struct proxy *px)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200353{
354 struct session *sess = s->sess;
355 struct http_txn *txn = s->txn;
356 struct http_msg *msg = &txn->req;
Christopher Fauletff2759f2018-10-24 11:13:16 +0200357 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200358 struct redirect_rule *rule;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200359 enum rule_result verdict;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200360 struct connection *conn = objt_conn(sess->origin);
361
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100362 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200363
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100364 htx = htxbuf(&req->buf);
Christopher Fauletff2759f2018-10-24 11:13:16 +0200365
Christopher Faulet1907ccc2019-04-29 13:12:02 +0200366 /* just in case we have some per-backend tracking. Only called the first
367 * execution of the analyser. */
368 if (!s->current_rule || s->current_rule_list != &px->http_req_rules)
369 stream_inc_be_http_req_ctr(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200370
371 /* evaluate http-request rules */
372 if (!LIST_ISEMPTY(&px->http_req_rules)) {
Christopher Fauletb58f62b2020-01-13 16:40:13 +0100373 verdict = http_req_get_intercept_rule(px, &px->http_req_rules, s);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200374
375 switch (verdict) {
376 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
377 goto return_prx_yield;
378
379 case HTTP_RULE_RES_CONT:
380 case HTTP_RULE_RES_STOP: /* nothing to do */
381 break;
382
383 case HTTP_RULE_RES_DENY: /* deny or tarpit */
384 if (txn->flags & TX_CLTARPIT)
385 goto tarpit;
386 goto deny;
387
388 case HTTP_RULE_RES_ABRT: /* abort request, response already sent. Eg: auth */
389 goto return_prx_cond;
390
391 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
392 goto done;
393
394 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
395 goto return_bad_req;
Christopher Faulet3a26bee2019-12-16 12:47:40 +0100396
397 case HTTP_RULE_RES_ERROR: /* failed with a bad request */
398 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200399 }
400 }
401
402 if (conn && (conn->flags & CO_FL_EARLY_DATA) &&
Olivier Houchard220a26c2020-01-23 14:57:36 +0100403 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_SSL_WAIT_HS))) {
Christopher Fauletff2759f2018-10-24 11:13:16 +0200404 struct http_hdr_ctx ctx;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200405
Christopher Fauletff2759f2018-10-24 11:13:16 +0200406 ctx.blk = NULL;
407 if (!http_find_header(htx, ist("Early-Data"), &ctx, 0)) {
408 if (unlikely(!http_add_header(htx, ist("Early-Data"), ist("1"))))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100409 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200410 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200411 }
412
413 /* OK at this stage, we know that the request was accepted according to
414 * the http-request rules, we can check for the stats. Note that the
415 * URI is detected *before* the req* rules in order not to be affected
416 * by a possible reqrep, while they are processed *after* so that a
417 * reqdeny can still block them. This clearly needs to change in 1.6!
418 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200419 if (!s->target && http_stats_check_uri(s, txn, px)) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200420 s->target = &http_stats_applet.obj_type;
Willy Tarreau14bfe9a2018-12-19 15:19:27 +0100421 if (unlikely(!si_register_handler(&s->si[1], objt_applet(s->target)))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200422 s->logs.tv_request = now;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200423 if (!(s->flags & SF_ERR_MASK))
424 s->flags |= SF_ERR_RESOURCE;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100425 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200426 }
427
428 /* parse the whole stats request and extract the relevant information */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200429 http_handle_stats(s, req);
Christopher Fauletb58f62b2020-01-13 16:40:13 +0100430 verdict = http_req_get_intercept_rule(px, &px->uri_auth->http_req_rules, s);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200431 /* not all actions implemented: deny, allow, auth */
432
433 if (verdict == HTTP_RULE_RES_DENY) /* stats http-request deny */
434 goto deny;
435
436 if (verdict == HTTP_RULE_RES_ABRT) /* stats auth / stats http-request auth */
437 goto return_prx_cond;
Christopher Faulet3a26bee2019-12-16 12:47:40 +0100438
439 if (verdict == HTTP_RULE_RES_BADREQ) /* failed with a bad request */
440 goto return_bad_req;
441
442 if (verdict == HTTP_RULE_RES_ERROR) /* failed with a bad request */
443 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200444 }
445
Christopher Faulet2571bc62019-03-01 11:44:26 +0100446 /* Proceed with the applets now. */
447 if (unlikely(objt_applet(s->target))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200448 if (sess->fe == s->be) /* report it if the request was intercepted by the frontend */
Willy Tarreau4781b152021-04-06 13:53:36 +0200449 _HA_ATOMIC_INC(&sess->fe->fe_counters.intercepted_req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200450
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200451 if (http_handle_expect_hdr(s, htx, msg) == -1)
Christopher Fauletb8a53712019-12-16 11:29:38 +0100452 goto return_int_err;
Christopher Fauletbcf242a2019-03-01 11:36:26 +0100453
Christopher Faulete0768eb2018-10-03 16:38:02 +0200454 if (!(s->flags & SF_ERR_MASK)) // this is not really an error but it is
455 s->flags |= SF_ERR_LOCAL; // to mark that it comes from the proxy
456 if (!(s->flags & SF_FINST_MASK))
457 s->flags |= SF_FINST_R;
458
Christopher Fauletc2ac5e42021-03-08 18:20:09 +0100459 if (HAS_FILTERS(s))
460 req->analysers |= AN_REQ_FLT_HTTP_HDRS;
461
Christopher Faulete0768eb2018-10-03 16:38:02 +0200462 /* enable the minimally required analyzers to handle keep-alive and compression on the HTTP response */
463 req->analysers &= (AN_REQ_HTTP_BODY | AN_REQ_FLT_HTTP_HDRS | AN_REQ_FLT_END);
464 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
465 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Christopher Fauletbcf242a2019-03-01 11:36:26 +0100466
467 req->flags |= CF_SEND_DONTWAIT;
468 s->flags |= SF_ASSIGNED;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200469 goto done;
470 }
471
472 /* check whether we have some ACLs set to redirect this request */
473 list_for_each_entry(rule, &px->redirect_rules, list) {
474 if (rule->cond) {
475 int ret;
476
477 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
478 ret = acl_pass(ret);
479 if (rule->cond->pol == ACL_COND_UNLESS)
480 ret = !ret;
481 if (!ret)
482 continue;
483 }
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200484 if (!http_apply_redirect_rule(rule, s, txn))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100485 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200486 goto done;
487 }
488
489 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
490 * If this happens, then the data will not come immediately, so we must
491 * send all what we have without waiting. Note that due to the small gain
492 * in waiting for the body of the request, it's easier to simply put the
493 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
494 * itself once used.
495 */
496 req->flags |= CF_SEND_DONTWAIT;
497
498 done: /* done with this analyser, continue with next ones that the calling
499 * points will have set, if any.
500 */
501 req->analyse_exp = TICK_ETERNITY;
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +0500502 done_without_exp: /* done with this analyser, but don't reset the analyse_exp. */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200503 req->analysers &= ~an_bit;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100504 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200505 return 1;
506
507 tarpit:
508 /* Allow cookie logging
509 */
510 if (s->be->cookie_name || sess->fe->capture_name)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200511 http_manage_client_side_cookies(s, req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200512
513 /* When a connection is tarpitted, we use the tarpit timeout,
514 * which may be the same as the connect timeout if unspecified.
515 * If unset, then set it to zero because we really want it to
516 * eventually expire. We build the tarpit as an analyser.
517 */
Christopher Faulet202c6ce2019-01-07 14:57:35 +0100518 channel_htx_erase(&s->req, htx);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200519
520 /* wipe the request out so that we can drop the connection early
521 * if the client closes first.
522 */
523 channel_dont_connect(req);
524
Christopher Faulete0768eb2018-10-03 16:38:02 +0200525 req->analysers &= AN_REQ_FLT_END; /* remove switching rules etc... */
526 req->analysers |= AN_REQ_HTTP_TARPIT;
527 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
528 if (!req->analyse_exp)
529 req->analyse_exp = tick_add(now_ms, 0);
530 stream_inc_http_err_ctr(s);
Willy Tarreau4781b152021-04-06 13:53:36 +0200531 _HA_ATOMIC_INC(&sess->fe->fe_counters.denied_req);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100532 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau4781b152021-04-06 13:53:36 +0200533 _HA_ATOMIC_INC(&s->be->be_counters.denied_req);
William Lallemand36119de2021-03-08 15:26:48 +0100534 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200535 _HA_ATOMIC_INC(&sess->listener->counters->denied_req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200536 goto done_without_exp;
537
538 deny: /* this request was blocked (denied) */
539
540 /* Allow cookie logging
541 */
542 if (s->be->cookie_name || sess->fe->capture_name)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200543 http_manage_client_side_cookies(s, req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200544
Christopher Faulete0768eb2018-10-03 16:38:02 +0200545 s->logs.tv_request = now;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200546 stream_inc_http_err_ctr(s);
Willy Tarreau4781b152021-04-06 13:53:36 +0200547 _HA_ATOMIC_INC(&sess->fe->fe_counters.denied_req);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100548 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau4781b152021-04-06 13:53:36 +0200549 _HA_ATOMIC_INC(&s->be->be_counters.denied_req);
William Lallemand36119de2021-03-08 15:26:48 +0100550 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200551 _HA_ATOMIC_INC(&sess->listener->counters->denied_req);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100552 goto return_prx_err;
553
554 return_int_err:
555 txn->status = 500;
556 if (!(s->flags & SF_ERR_MASK))
557 s->flags |= SF_ERR_INTERNAL;
Willy Tarreau4781b152021-04-06 13:53:36 +0200558 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100559 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau4781b152021-04-06 13:53:36 +0200560 _HA_ATOMIC_INC(&s->be->be_counters.internal_errors);
William Lallemand36119de2021-03-08 15:26:48 +0100561 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200562 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100563 goto return_prx_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200564
565 return_bad_req:
Christopher Faulete0768eb2018-10-03 16:38:02 +0200566 txn->status = 400;
Willy Tarreau4781b152021-04-06 13:53:36 +0200567 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req);
William Lallemand36119de2021-03-08 15:26:48 +0100568 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200569 _HA_ATOMIC_INC(&sess->listener->counters->failed_req);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100570 /* fall through */
571
572 return_prx_err:
573 http_reply_and_close(s, txn->status, http_error_message(s));
574 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200575
576 return_prx_cond:
577 if (!(s->flags & SF_ERR_MASK))
578 s->flags |= SF_ERR_PRXCOND;
579 if (!(s->flags & SF_FINST_MASK))
580 s->flags |= SF_FINST_R;
581
582 req->analysers &= AN_REQ_FLT_END;
583 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100584 DBG_TRACE_DEVEL("leaving on error",
585 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200586 return 0;
587
588 return_prx_yield:
589 channel_dont_connect(req);
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100590 DBG_TRACE_DEVEL("waiting for more data",
591 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200592 return 0;
593}
594
595/* This function performs all the processing enabled for the current request.
596 * It returns 1 if the processing can continue on next analysers, or zero if it
597 * needs more data, encounters an error, or wants to immediately abort the
598 * request. It relies on buffers flags, and updates s->req.analysers.
599 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200600int http_process_request(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200601{
602 struct session *sess = s->sess;
603 struct http_txn *txn = s->txn;
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200604 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200605 struct connection *cli_conn = objt_conn(strm_sess(s)->origin);
606
Christopher Faulet8bebd2f2020-10-06 17:54:56 +0200607 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200608
609 /*
610 * Right now, we know that we have processed the entire headers
611 * and that unwanted requests have been filtered out. We can do
612 * whatever we want with the remaining request. Also, now we
613 * may have separate values for ->fe, ->be.
614 */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100615 htx = htxbuf(&req->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200616
617 /*
Christopher Faulete0768eb2018-10-03 16:38:02 +0200618 * 7: Now we can work with the cookies.
619 * Note that doing so might move headers in the request, but
620 * the fields will stay coherent and the URI will not move.
621 * This should only be performed in the backend.
622 */
623 if (s->be->cookie_name || sess->fe->capture_name)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200624 http_manage_client_side_cookies(s, req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200625
Tim Duesterhus2825b4b2020-02-28 15:13:34 +0100626 /* 8: Generate unique ID if a "unique-id-format" is defined.
627 *
628 * A unique ID is generated even when it is not sent to ensure that the ID can make use of
629 * fetches only available in the HTTP request processing stage.
630 */
631 if (!LIST_ISEMPTY(&sess->fe->format_unique_id)) {
Tim Duesterhusa17e6622020-03-05 20:19:02 +0100632 struct ist unique_id = stream_generate_unique_id(s, &sess->fe->format_unique_id);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200633
Tim Duesterhusa17e6622020-03-05 20:19:02 +0100634 if (!isttest(unique_id)) {
Christopher Fauletb8a53712019-12-16 11:29:38 +0100635 if (!(s->flags & SF_ERR_MASK))
636 s->flags |= SF_ERR_RESOURCE;
637 goto return_int_err;
638 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200639
Tim Duesterhus2825b4b2020-02-28 15:13:34 +0100640 /* send unique ID if a "unique-id-header" is defined */
Tim Duesterhus0643b0e2020-03-05 17:56:35 +0100641 if (isttest(sess->fe->header_unique_id) &&
Tim Duesterhusa17e6622020-03-05 20:19:02 +0100642 unlikely(!http_add_header(htx, sess->fe->header_unique_id, s->unique_id)))
Tim Duesterhus2825b4b2020-02-28 15:13:34 +0100643 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200644 }
645
646 /*
647 * 9: add X-Forwarded-For if either the frontend or the backend
648 * asks for it.
649 */
650 if ((sess->fe->options | s->be->options) & PR_O_FWDFOR) {
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200651 struct http_hdr_ctx ctx = { .blk = NULL };
652 struct ist hdr = ist2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : sess->fe->fwdfor_hdr_name,
653 s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : sess->fe->fwdfor_hdr_len);
654
Christopher Faulete0768eb2018-10-03 16:38:02 +0200655 if (!((sess->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200656 http_find_header(htx, hdr, &ctx, 0)) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200657 /* The header is set to be added only if none is present
658 * and we found it, so don't do anything.
659 */
660 }
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200661 else if (cli_conn && conn_get_src(cli_conn) && cli_conn->src->ss_family == AF_INET) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200662 /* Add an X-Forwarded-For header unless the source IP is
663 * in the 'except' network range.
664 */
Christopher Faulet5d1def62021-02-26 09:19:15 +0100665 if (ipcmp2net(cli_conn->src, &sess->fe->except_xff_net) &&
666 ipcmp2net(cli_conn->src, &s->be->except_xff_net)) {
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200667 unsigned char *pn = (unsigned char *)&((struct sockaddr_in *)cli_conn->src)->sin_addr;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200668
669 /* Note: we rely on the backend to get the header name to be used for
670 * x-forwarded-for, because the header is really meant for the backends.
671 * However, if the backend did not specify any option, we have to rely
672 * on the frontend's header name.
673 */
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200674 chunk_printf(&trash, "%d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
675 if (unlikely(!http_add_header(htx, hdr, ist2(trash.area, trash.data))))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100676 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200677 }
678 }
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200679 else if (cli_conn && conn_get_src(cli_conn) && cli_conn->src->ss_family == AF_INET6) {
Christopher Faulet5d1def62021-02-26 09:19:15 +0100680 /* Add an X-Forwarded-For header unless the source IP is
681 * in the 'except' network range.
Christopher Faulete0768eb2018-10-03 16:38:02 +0200682 */
Christopher Faulet5d1def62021-02-26 09:19:15 +0100683 if (ipcmp2net(cli_conn->src, &sess->fe->except_xff_net) &&
684 ipcmp2net(cli_conn->src, &s->be->except_xff_net)) {
685 char pn[INET6_ADDRSTRLEN];
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200686
Christopher Faulet5d1def62021-02-26 09:19:15 +0100687 inet_ntop(AF_INET6,
688 (const void *)&((struct sockaddr_in6 *)(cli_conn->src))->sin6_addr,
689 pn, sizeof(pn));
Christopher Faulete0768eb2018-10-03 16:38:02 +0200690
Christopher Faulet5d1def62021-02-26 09:19:15 +0100691 /* Note: we rely on the backend to get the header name to be used for
692 * x-forwarded-for, because the header is really meant for the backends.
693 * However, if the backend did not specify any option, we have to rely
694 * on the frontend's header name.
695 */
696 chunk_printf(&trash, "%s", pn);
697 if (unlikely(!http_add_header(htx, hdr, ist2(trash.area, trash.data))))
698 goto return_int_err;
699 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200700 }
701 }
702
703 /*
704 * 10: add X-Original-To if either the frontend or the backend
705 * asks for it.
706 */
707 if ((sess->fe->options | s->be->options) & PR_O_ORGTO) {
Christopher Faulet5d1def62021-02-26 09:19:15 +0100708 struct ist hdr = ist2(s->be->orgto_hdr_len ? s->be->orgto_hdr_name : sess->fe->orgto_hdr_name,
709 s->be->orgto_hdr_len ? s->be->orgto_hdr_len : sess->fe->orgto_hdr_len);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200710
Christopher Fauletcccded92021-02-26 12:45:56 +0100711 if (cli_conn && conn_get_dst(cli_conn) && cli_conn->dst->ss_family == AF_INET) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200712 /* Add an X-Original-To header unless the destination IP is
713 * in the 'except' network range.
714 */
Christopher Faulet5d1def62021-02-26 09:19:15 +0100715 if (ipcmp2net(cli_conn->dst, &sess->fe->except_xot_net) &&
716 ipcmp2net(cli_conn->dst, &s->be->except_xot_net)) {
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200717 unsigned char *pn = (unsigned char *)&((struct sockaddr_in *)cli_conn->dst)->sin_addr;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200718
719 /* Note: we rely on the backend to get the header name to be used for
720 * x-original-to, because the header is really meant for the backends.
721 * However, if the backend did not specify any option, we have to rely
722 * on the frontend's header name.
723 */
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200724 chunk_printf(&trash, "%d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
725 if (unlikely(!http_add_header(htx, hdr, ist2(trash.area, trash.data))))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100726 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200727 }
728 }
Christopher Faulet5d1def62021-02-26 09:19:15 +0100729 else if (cli_conn && conn_get_dst(cli_conn) && cli_conn->dst->ss_family == AF_INET6) {
730 /* Add an X-Original-To header unless the source IP is
731 * in the 'except' network range.
732 */
733 if (ipcmp2net(cli_conn->dst, &sess->fe->except_xot_net) &&
734 ipcmp2net(cli_conn->dst, &s->be->except_xot_net)) {
735 char pn[INET6_ADDRSTRLEN];
736
737 inet_ntop(AF_INET6,
738 (const void *)&((struct sockaddr_in6 *)(cli_conn->dst))->sin6_addr,
739 pn, sizeof(pn));
740
741 /* Note: we rely on the backend to get the header name to be used for
742 * x-forwarded-for, because the header is really meant for the backends.
743 * However, if the backend did not specify any option, we have to rely
744 * on the frontend's header name.
745 */
746 chunk_printf(&trash, "%s", pn);
747 if (unlikely(!http_add_header(htx, hdr, ist2(trash.area, trash.data))))
748 goto return_int_err;
749 }
750 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200751 }
752
Christopher Fauletc2ac5e42021-03-08 18:20:09 +0100753 /* Filter the request headers if there are filters attached to the
754 * stream.
755 */
756 if (HAS_FILTERS(s))
757 req->analysers |= AN_REQ_FLT_HTTP_HDRS;
758
Christopher Faulete0768eb2018-10-03 16:38:02 +0200759 /* If we have no server assigned yet and we're balancing on url_param
760 * with a POST request, we may be interested in checking the body for
761 * that parameter. This will be done in another analyser.
762 */
763 if (!(s->flags & (SF_ASSIGNED|SF_DIRECT)) &&
Willy Tarreau089eaa02019-01-14 15:17:46 +0100764 s->txn->meth == HTTP_METH_POST &&
765 (s->be->lbprm.algo & BE_LB_ALGO) == BE_LB_ALGO_PH) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200766 channel_dont_connect(req);
767 req->analysers |= AN_REQ_HTTP_BODY;
768 }
769
770 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
771 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau1a18b542018-12-11 16:37:42 +0100772
Christopher Faulete0768eb2018-10-03 16:38:02 +0200773 /* We expect some data from the client. Unless we know for sure
774 * we already have a full request, we have to re-enable quick-ack
775 * in case we previously disabled it, otherwise we might cause
776 * the client to delay further data.
777 */
William Lallemand36119de2021-03-08 15:26:48 +0100778 if ((sess->listener && (sess->listener->options & LI_O_NOQUICKACK)) && !(htx->flags & HTX_FL_EOM))
Willy Tarreau1a18b542018-12-11 16:37:42 +0100779 conn_set_quickack(cli_conn, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200780
781 /*************************************************************
782 * OK, that's finished for the headers. We have done what we *
783 * could. Let's switch to the DATA state. *
784 ************************************************************/
785 req->analyse_exp = TICK_ETERNITY;
786 req->analysers &= ~an_bit;
787
788 s->logs.tv_request = now;
789 /* OK let's go on with the BODY now */
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100790 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200791 return 1;
792
Christopher Fauletb8a53712019-12-16 11:29:38 +0100793 return_int_err:
794 txn->status = 500;
795 if (!(s->flags & SF_ERR_MASK))
796 s->flags |= SF_ERR_INTERNAL;
Willy Tarreau4781b152021-04-06 13:53:36 +0200797 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100798 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau4781b152021-04-06 13:53:36 +0200799 _HA_ATOMIC_INC(&s->be->be_counters.internal_errors);
William Lallemand36119de2021-03-08 15:26:48 +0100800 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200801 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100802
Christopher Fauletb8a53712019-12-16 11:29:38 +0100803 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +0200804
805 if (!(s->flags & SF_ERR_MASK))
806 s->flags |= SF_ERR_PRXCOND;
807 if (!(s->flags & SF_FINST_MASK))
808 s->flags |= SF_FINST_R;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100809
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100810 DBG_TRACE_DEVEL("leaving on error",
811 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200812 return 0;
813}
814
815/* This function is an analyser which processes the HTTP tarpit. It always
816 * returns zero, at the beginning because it prevents any other processing
817 * from occurring, and at the end because it terminates the request.
818 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200819int http_process_tarpit(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200820{
821 struct http_txn *txn = s->txn;
822
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100823 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, &txn->req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200824 /* This connection is being tarpitted. The CLIENT side has
825 * already set the connect expiration date to the right
826 * timeout. We just have to check that the client is still
827 * there and that the timeout has not expired.
828 */
829 channel_dont_connect(req);
830 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100831 !tick_is_expired(req->analyse_exp, now_ms)) {
832 DBG_TRACE_DEVEL("waiting for tarpit timeout expiry",
833 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200834 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100835 }
836
Christopher Faulete0768eb2018-10-03 16:38:02 +0200837
838 /* We will set the queue timer to the time spent, just for
839 * logging purposes. We fake a 500 server error, so that the
840 * attacker will not suspect his connection has been tarpitted.
841 * It will not cause trouble to the logs because we can exclude
842 * the tarpitted connections by filtering on the 'PT' status flags.
843 */
844 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
845
Christopher Faulet8dfeccf2020-05-15 14:16:29 +0200846 http_reply_and_close(s, txn->status, (!(req->flags & CF_READ_ERROR) ? http_error_message(s) : NULL));
Christopher Faulet5cb513a2020-05-13 17:56:56 +0200847
Christopher Faulete0768eb2018-10-03 16:38:02 +0200848 if (!(s->flags & SF_ERR_MASK))
849 s->flags |= SF_ERR_PRXCOND;
850 if (!(s->flags & SF_FINST_MASK))
851 s->flags |= SF_FINST_T;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100852
853 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200854 return 0;
855}
856
857/* This function is an analyser which waits for the HTTP request body. It waits
858 * for either the buffer to be full, or the full advertised contents to have
859 * reached the buffer. It must only be called after the standard HTTP request
860 * processing has occurred, because it expects the request to be parsed and will
861 * look for the Expect header. It may send a 100-Continue interim response. It
862 * takes in input any state starting from HTTP_MSG_BODY and leaves with one of
863 * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it
864 * needs to read more data, or 1 once it has completed its analysis.
865 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200866int http_wait_for_request_body(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200867{
868 struct session *sess = s->sess;
869 struct http_txn *txn = s->txn;
870 struct http_msg *msg = &s->txn->req;
871
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100872 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Fauletf76ebe82018-10-24 11:16:22 +0200873
Christopher Fauletf76ebe82018-10-24 11:16:22 +0200874
Christopher Faulet021a8e42021-03-29 10:46:38 +0200875 switch (http_wait_for_msg_body(s, req, s->be->timeout.httpreq, 0)) {
876 case HTTP_RULE_RES_CONT:
877 goto http_end;
878 case HTTP_RULE_RES_YIELD:
879 goto missing_data_or_waiting;
880 case HTTP_RULE_RES_BADREQ:
Willy Tarreau4236f032019-03-05 10:43:32 +0100881 goto return_bad_req;
Christopher Faulet021a8e42021-03-29 10:46:38 +0200882 case HTTP_RULE_RES_ERROR:
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200883 goto return_int_err;
Christopher Faulet021a8e42021-03-29 10:46:38 +0200884 case HTTP_RULE_RES_ABRT:
Christopher Fauletb8a53712019-12-16 11:29:38 +0100885 goto return_prx_cond;
Christopher Faulet021a8e42021-03-29 10:46:38 +0200886 default:
887 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200888 }
889
890 http_end:
891 /* The situation will not evolve, so let's give up on the analysis. */
892 s->logs.tv_request = now; /* update the request timer to reflect full request */
893 req->analysers &= ~an_bit;
894 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100895 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200896 return 1;
897
Christopher Faulet021a8e42021-03-29 10:46:38 +0200898 missing_data_or_waiting:
899 channel_dont_connect(req);
900 DBG_TRACE_DEVEL("waiting for more data",
901 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
902 return 0;
903
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200904 return_int_err:
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200905 txn->status = 500;
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200906 if (!(s->flags & SF_ERR_MASK))
907 s->flags |= SF_ERR_INTERNAL;
Willy Tarreau4781b152021-04-06 13:53:36 +0200908 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100909 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau4781b152021-04-06 13:53:36 +0200910 _HA_ATOMIC_INC(&s->be->be_counters.internal_errors);
William Lallemand36119de2021-03-08 15:26:48 +0100911 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200912 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Faulet021a8e42021-03-29 10:46:38 +0200913 goto return_prx_err;
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200914
Christopher Faulete0768eb2018-10-03 16:38:02 +0200915 return_bad_req: /* let's centralize all bad requests */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200916 txn->status = 400;
Willy Tarreau4781b152021-04-06 13:53:36 +0200917 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req);
William Lallemand36119de2021-03-08 15:26:48 +0100918 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200919 _HA_ATOMIC_INC(&sess->listener->counters->failed_req);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100920 /* fall through */
921
Christopher Faulet021a8e42021-03-29 10:46:38 +0200922 return_prx_err:
Christopher Fauletb8a53712019-12-16 11:29:38 +0100923 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet021a8e42021-03-29 10:46:38 +0200924 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200925
Christopher Faulet021a8e42021-03-29 10:46:38 +0200926 return_prx_cond:
Christopher Faulete0768eb2018-10-03 16:38:02 +0200927 if (!(s->flags & SF_ERR_MASK))
928 s->flags |= SF_ERR_PRXCOND;
929 if (!(s->flags & SF_FINST_MASK))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100930 s->flags |= (msg->msg_state < HTTP_MSG_DATA ? SF_FINST_R : SF_FINST_D);
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200931
Christopher Faulete0768eb2018-10-03 16:38:02 +0200932 req->analysers &= AN_REQ_FLT_END;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100933 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100934 DBG_TRACE_DEVEL("leaving on error",
935 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200936 return 0;
937}
938
939/* This function is an analyser which forwards request body (including chunk
940 * sizes if any). It is called as soon as we must forward, even if we forward
941 * zero byte. The only situation where it must not be called is when we're in
942 * tunnel mode and we want to forward till the close. It's used both to forward
943 * remaining data and to resync after end of body. It expects the msg_state to
944 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
945 * read more data, or 1 once we can go on with next request or end the stream.
946 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
947 * bytes of pending data + the headers if not already done.
948 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200949int http_request_forward_body(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200950{
951 struct session *sess = s->sess;
952 struct http_txn *txn = s->txn;
Christopher Faulet9768c262018-10-22 09:34:31 +0200953 struct http_msg *msg = &txn->req;
954 struct htx *htx;
Christopher Faulet93e02d82019-03-08 14:18:50 +0100955 short status = 0;
Christopher Fauletaed82cf2018-11-30 22:22:32 +0100956 int ret;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200957
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100958 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200959
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100960 htx = htxbuf(&req->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200961
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200962 if (htx->flags & HTX_FL_PARSING_ERROR)
963 goto return_bad_req;
964 if (htx->flags & HTX_FL_PROCESSING_ERROR)
965 goto return_int_err;
966
Christopher Faulete0768eb2018-10-03 16:38:02 +0200967 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
968 ((req->flags & CF_SHUTW) && (req->to_forward || co_data(req)))) {
969 /* Output closed while we were sending data. We must abort and
970 * wake the other side up.
Christopher Fauletf506d962021-04-27 10:56:28 +0200971 *
972 * If we have finished to send the request and the response is
973 * still in progress, don't catch write error on the request
974 * side if it is in fact a read error on the server side.
Christopher Faulete0768eb2018-10-03 16:38:02 +0200975 */
Christopher Fauletf506d962021-04-27 10:56:28 +0200976 if (msg->msg_state == HTTP_MSG_DONE && (s->res.flags & CF_READ_ERROR) && s->res.analysers)
977 return 0;
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200978
Olivier Houchard29cac3c2019-07-12 15:48:58 +0200979 /* Don't abort yet if we had L7 retries activated and it
980 * was a write error, we may recover.
981 */
982 if (!(req->flags & (CF_READ_ERROR | CF_READ_TIMEOUT)) &&
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100983 (s->si[1].flags & SI_FL_L7_RETRY)) {
984 DBG_TRACE_DEVEL("leaving on L7 retry",
985 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Olivier Houchard29cac3c2019-07-12 15:48:58 +0200986 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100987 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200988 msg->msg_state = HTTP_MSG_ERROR;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200989 http_end_request(s);
990 http_end_response(s);
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100991 DBG_TRACE_DEVEL("leaving on error",
992 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200993 return 1;
994 }
995
996 /* Note that we don't have to send 100-continue back because we don't
997 * need the data to complete our job, and it's up to the server to
998 * decide whether to return 100, 417 or anything else in return of
999 * an "Expect: 100-continue" header.
1000 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001001 if (msg->msg_state == HTTP_MSG_BODY)
1002 msg->msg_state = HTTP_MSG_DATA;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001003
Christopher Faulete0768eb2018-10-03 16:38:02 +02001004 /* in most states, we should abort in case of early close */
1005 channel_auto_close(req);
1006
1007 if (req->to_forward) {
Christopher Faulet66af0b22019-03-22 14:54:52 +01001008 if (req->to_forward == CHN_INFINITE_FORWARD) {
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001009 if (req->flags & CF_EOI)
1010 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet66af0b22019-03-22 14:54:52 +01001011 }
1012 else {
1013 /* We can't process the buffer's contents yet */
1014 req->flags |= CF_WAKE_WRITE;
1015 goto missing_data_or_waiting;
1016 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001017 }
1018
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001019 if (msg->msg_state >= HTTP_MSG_ENDING)
1020 goto ending;
1021
1022 if (txn->meth == HTTP_METH_CONNECT) {
1023 msg->msg_state = HTTP_MSG_ENDING;
1024 goto ending;
1025 }
1026
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001027 /* Forward input data. We get it by removing all outgoing data not
1028 * forwarded yet from HTX data size. If there are some data filters, we
1029 * let them decide the amount of data to forward.
Christopher Faulet9768c262018-10-22 09:34:31 +02001030 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001031 if (HAS_REQ_DATA_FILTERS(s)) {
1032 ret = flt_http_payload(s, msg, htx->data);
1033 if (ret < 0)
1034 goto return_bad_req;
Christopher Faulet421e7692019-06-13 11:16:45 +02001035 c_adv(req, ret);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001036 }
1037 else {
Christopher Faulet421e7692019-06-13 11:16:45 +02001038 c_adv(req, htx->data - co_data(req));
Christopher Faulet66af0b22019-03-22 14:54:52 +01001039 if (msg->flags & HTTP_MSGF_XFER_LEN)
1040 channel_htx_forward_forever(req, htx);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001041 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001042
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001043 if (htx->data != co_data(req))
1044 goto missing_data_or_waiting;
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001045
Christopher Faulet9768c262018-10-22 09:34:31 +02001046 /* Check if the end-of-message is reached and if so, switch the message
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001047 * in HTTP_MSG_ENDING state. Then if all data was marked to be
1048 * forwarded, set the state to HTTP_MSG_DONE.
Christopher Faulet9768c262018-10-22 09:34:31 +02001049 */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001050 if (!(htx->flags & HTX_FL_EOM))
Christopher Faulet9768c262018-10-22 09:34:31 +02001051 goto missing_data_or_waiting;
1052
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001053 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet9768c262018-10-22 09:34:31 +02001054
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001055 ending:
Christopher Faulet2151cdd2020-07-22 16:34:59 +02001056 req->flags &= ~CF_EXPECT_MORE; /* no more data are expected */
1057
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001058 /* other states, ENDING...TUNNEL */
1059 if (msg->msg_state >= HTTP_MSG_DONE)
1060 goto done;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001061
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001062 if (HAS_REQ_DATA_FILTERS(s)) {
1063 ret = flt_http_end(s, msg);
1064 if (ret <= 0) {
1065 if (!ret)
1066 goto missing_data_or_waiting;
1067 goto return_bad_req;
1068 }
1069 }
1070
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001071 if (txn->meth == HTTP_METH_CONNECT)
1072 msg->msg_state = HTTP_MSG_TUNNEL;
1073 else {
1074 msg->msg_state = HTTP_MSG_DONE;
1075 req->to_forward = 0;
1076 }
1077
1078 done:
1079 /* we don't want to forward closes on DONE except in tunnel mode. */
1080 if (!(txn->flags & TX_CON_WANT_TUN))
1081 channel_dont_close(req);
1082
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001083 http_end_request(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001084 if (!(req->analysers & an_bit)) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001085 http_end_response(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001086 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
1087 if (req->flags & CF_SHUTW) {
1088 /* request errors are most likely due to the
1089 * server aborting the transfer. */
Christopher Faulet93e02d82019-03-08 14:18:50 +01001090 goto return_srv_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001091 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001092 goto return_bad_req;
1093 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001094 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001095 return 1;
1096 }
1097
1098 /* If "option abortonclose" is set on the backend, we want to monitor
1099 * the client's connection and forward any shutdown notification to the
1100 * server, which will decide whether to close or to go on processing the
1101 * request. We only do that in tunnel mode, and not in other modes since
1102 * it can be abused to exhaust source ports. */
Christopher Faulet769d0e92019-03-22 14:23:18 +01001103 if (s->be->options & PR_O_ABRT_CLOSE) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001104 channel_auto_read(req);
Christopher Fauletc41547b2019-07-16 14:32:23 +02001105 if ((req->flags & (CF_SHUTR|CF_READ_NULL)) && !(txn->flags & TX_CON_WANT_TUN))
Christopher Faulete0768eb2018-10-03 16:38:02 +02001106 s->si[1].flags |= SI_FL_NOLINGER;
1107 channel_auto_close(req);
1108 }
1109 else if (s->txn->meth == HTTP_METH_POST) {
1110 /* POST requests may require to read extra CRLF sent by broken
1111 * browsers and which could cause an RST to be sent upon close
1112 * on some systems (eg: Linux). */
1113 channel_auto_read(req);
1114 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001115 DBG_TRACE_DEVEL("waiting for the end of the HTTP txn",
1116 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001117 return 0;
1118
1119 missing_data_or_waiting:
1120 /* stop waiting for data if the input is closed before the end */
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001121 if (msg->msg_state < HTTP_MSG_ENDING && req->flags & CF_SHUTR)
Christopher Faulet93e02d82019-03-08 14:18:50 +01001122 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001123
1124 waiting:
1125 /* waiting for the last bits to leave the buffer */
1126 if (req->flags & CF_SHUTW)
Christopher Faulet93e02d82019-03-08 14:18:50 +01001127 goto return_srv_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001128
1129 /* When TE: chunked is used, we need to get there again to parse remaining
1130 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
1131 * And when content-length is used, we never want to let the possible
1132 * shutdown be forwarded to the other side, as the state machine will
1133 * take care of it once the client responds. It's also important to
1134 * prevent TIME_WAITs from accumulating on the backend side, and for
1135 * HTTP/2 where the last frame comes with a shutdown.
1136 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001137 if (msg->flags & HTTP_MSGF_XFER_LEN)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001138 channel_dont_close(req);
1139
1140 /* We know that more data are expected, but we couldn't send more that
1141 * what we did. So we always set the CF_EXPECT_MORE flag so that the
1142 * system knows it must not set a PUSH on this first part. Interactive
1143 * modes are already handled by the stream sock layer. We must not do
1144 * this in content-length mode because it could present the MSG_MORE
1145 * flag with the last block of forwarded data, which would cause an
1146 * additional delay to be observed by the receiver.
1147 */
Christopher Faulet2151cdd2020-07-22 16:34:59 +02001148 if (HAS_REQ_DATA_FILTERS(s))
Christopher Faulete0768eb2018-10-03 16:38:02 +02001149 req->flags |= CF_EXPECT_MORE;
1150
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001151 DBG_TRACE_DEVEL("waiting for more data to forward",
1152 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001153 return 0;
1154
Christopher Faulet93e02d82019-03-08 14:18:50 +01001155 return_cli_abort:
Willy Tarreau4781b152021-04-06 13:53:36 +02001156 _HA_ATOMIC_INC(&sess->fe->fe_counters.cli_aborts);
1157 _HA_ATOMIC_INC(&s->be->be_counters.cli_aborts);
William Lallemand36119de2021-03-08 15:26:48 +01001158 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001159 _HA_ATOMIC_INC(&sess->listener->counters->cli_aborts);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001160 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001161 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.cli_aborts);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001162 if (!(s->flags & SF_ERR_MASK))
1163 s->flags |= SF_ERR_CLICL;
1164 status = 400;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001165 goto return_prx_cond;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001166
1167 return_srv_abort:
Willy Tarreau4781b152021-04-06 13:53:36 +02001168 _HA_ATOMIC_INC(&sess->fe->fe_counters.srv_aborts);
1169 _HA_ATOMIC_INC(&s->be->be_counters.srv_aborts);
William Lallemand36119de2021-03-08 15:26:48 +01001170 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001171 _HA_ATOMIC_INC(&sess->listener->counters->srv_aborts);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001172 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001173 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.srv_aborts);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001174 if (!(s->flags & SF_ERR_MASK))
1175 s->flags |= SF_ERR_SRVCL;
1176 status = 502;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001177 goto return_prx_cond;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001178
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001179 return_int_err:
1180 if (!(s->flags & SF_ERR_MASK))
1181 s->flags |= SF_ERR_INTERNAL;
Willy Tarreau4781b152021-04-06 13:53:36 +02001182 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
1183 _HA_ATOMIC_INC(&s->be->be_counters.internal_errors);
William Lallemand36119de2021-03-08 15:26:48 +01001184 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001185 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001186 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001187 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.internal_errors);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001188 status = 500;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001189 goto return_prx_cond;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001190
Christopher Faulet93e02d82019-03-08 14:18:50 +01001191 return_bad_req:
Willy Tarreau4781b152021-04-06 13:53:36 +02001192 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req);
William Lallemand36119de2021-03-08 15:26:48 +01001193 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001194 _HA_ATOMIC_INC(&sess->listener->counters->failed_req);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001195 status = 400;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001196 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001197
Christopher Fauletb8a53712019-12-16 11:29:38 +01001198 return_prx_cond:
Christopher Faulet9768c262018-10-22 09:34:31 +02001199 if (txn->status > 0) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001200 /* Note: we don't send any error if some data were already sent */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001201 http_reply_and_close(s, txn->status, NULL);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001202 } else {
Christopher Faulet93e02d82019-03-08 14:18:50 +01001203 txn->status = status;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001204 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001205 }
Christopher Fauletb8a53712019-12-16 11:29:38 +01001206 if (!(s->flags & SF_ERR_MASK))
1207 s->flags |= SF_ERR_PRXCOND;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001208 if (!(s->flags & SF_FINST_MASK))
1209 s->flags |= ((txn->rsp.msg_state < HTTP_MSG_ERROR) ? SF_FINST_H : SF_FINST_D);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001210 DBG_TRACE_DEVEL("leaving on error ",
1211 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001212 return 0;
1213}
1214
Olivier Houcharda254a372019-04-05 15:30:12 +02001215/* Reset the stream and the backend stream_interface to a situation suitable for attemption connection */
1216/* Returns 0 if we can attempt to retry, -1 otherwise */
1217static __inline int do_l7_retry(struct stream *s, struct stream_interface *si)
1218{
Christopher Faulet9f5382e2021-05-21 13:46:14 +02001219 struct channel *req, *res;
1220 int co_data;
Olivier Houcharda254a372019-04-05 15:30:12 +02001221
1222 si->conn_retries--;
1223 if (si->conn_retries < 0)
Christopher Faulet552601d2021-05-26 10:31:06 +02001224 return -1;
Christopher Faulet5b82cc52020-10-12 15:18:50 +02001225
Christopher Faulete763c8c2021-05-05 18:23:59 +02001226 if (objt_server(s->target)) {
1227 if (s->flags & SF_CURR_SESS) {
1228 s->flags &= ~SF_CURR_SESS;
1229 _HA_ATOMIC_DEC(&__objt_server(s->target)->cur_sess);
1230 }
Willy Tarreau4781b152021-04-06 13:53:36 +02001231 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.retries);
Christopher Faulete763c8c2021-05-05 18:23:59 +02001232 }
Willy Tarreau4781b152021-04-06 13:53:36 +02001233 _HA_ATOMIC_INC(&s->be->be_counters.retries);
Willy Tarreau223995e2019-05-04 10:38:31 +02001234
Christopher Faulet9f5382e2021-05-21 13:46:14 +02001235 req = &s->req;
1236 res = &s->res;
Olivier Houcharda254a372019-04-05 15:30:12 +02001237 /* Remove any write error from the request, and read error from the response */
1238 req->flags &= ~(CF_WRITE_ERROR | CF_WRITE_TIMEOUT | CF_SHUTW | CF_SHUTW_NOW);
1239 res->flags &= ~(CF_READ_ERROR | CF_READ_TIMEOUT | CF_SHUTR | CF_EOI | CF_READ_NULL | CF_SHUTR_NOW);
1240 res->analysers = 0;
1241 si->flags &= ~(SI_FL_ERR | SI_FL_EXP | SI_FL_RXBLK_SHUT);
Christopher Faulet30aa0da2021-05-05 21:05:09 +02001242 si->err_type = SI_ET_NONE;
1243 s->flags &= ~(SF_ERR_MASK | SF_FINST_MASK);
Olivier Houchard4bd58672019-07-12 16:16:59 +02001244 stream_choose_redispatch(s);
Olivier Houcharda254a372019-04-05 15:30:12 +02001245 si->exp = TICK_ETERNITY;
1246 res->rex = TICK_ETERNITY;
1247 res->to_forward = 0;
1248 res->analyse_exp = TICK_ETERNITY;
1249 res->total = 0;
Olivier Houcharda254a372019-04-05 15:30:12 +02001250 si_release_endpoint(&s->si[1]);
Olivier Houcharda254a372019-04-05 15:30:12 +02001251
Christopher Faulet9f5382e2021-05-21 13:46:14 +02001252 b_free(&req->buf);
1253 /* Swap the L7 buffer with the channel buffer */
1254 /* We know we stored the co_data as b_data, so get it there */
1255 co_data = b_data(&si->l7_buffer);
1256 b_set_data(&si->l7_buffer, b_size(&si->l7_buffer));
1257 b_xfer(&req->buf, &si->l7_buffer, b_data(&si->l7_buffer));
1258 co_set_data(req, co_data);
Christopher Faulet5b82cc52020-10-12 15:18:50 +02001259
Ilya Shipitsinacf84592021-02-06 22:29:08 +05001260 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 +02001261
Olivier Houcharda254a372019-04-05 15:30:12 +02001262 b_reset(&res->buf);
1263 co_set_data(res, 0);
1264 return 0;
1265}
1266
Christopher Faulete0768eb2018-10-03 16:38:02 +02001267/* This stream analyser waits for a complete HTTP response. It returns 1 if the
1268 * processing can continue on next analysers, or zero if it either needs more
1269 * data or wants to immediately abort the response (eg: timeout, error, ...). It
1270 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->res.analysers
1271 * when it has nothing left to do, and may remove any analyser when it wants to
1272 * abort.
1273 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001274int http_wait_for_response(struct stream *s, struct channel *rep, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001275{
Christopher Faulet9768c262018-10-22 09:34:31 +02001276 /*
1277 * We will analyze a complete HTTP response to check the its syntax.
1278 *
1279 * Once the start line and all headers are received, we may perform a
1280 * capture of the error (if any), and we will set a few fields. We also
1281 * logging and finally headers capture.
1282 */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001283 struct session *sess = s->sess;
1284 struct http_txn *txn = s->txn;
1285 struct http_msg *msg = &txn->rsp;
Christopher Faulet9768c262018-10-22 09:34:31 +02001286 struct htx *htx;
Olivier Houcharda254a372019-04-05 15:30:12 +02001287 struct stream_interface *si_b = &s->si[1];
Christopher Faulet61608322018-11-23 16:23:45 +01001288 struct connection *srv_conn;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001289 struct htx_sl *sl;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001290 int n;
1291
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001292 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001293
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001294 htx = htxbuf(&rep->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001295
Willy Tarreau4236f032019-03-05 10:43:32 +01001296 /* Parsing errors are caught here */
1297 if (htx->flags & HTX_FL_PARSING_ERROR)
1298 goto return_bad_res;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001299 if (htx->flags & HTX_FL_PROCESSING_ERROR)
1300 goto return_int_err;
Willy Tarreau4236f032019-03-05 10:43:32 +01001301
Christopher Faulete0768eb2018-10-03 16:38:02 +02001302 /*
1303 * Now we quickly check if we have found a full valid response.
1304 * If not so, we check the FD and buffer states before leaving.
1305 * A full response is indicated by the fact that we have seen
1306 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
1307 * responses are checked first.
1308 *
1309 * Depending on whether the client is still there or not, we
1310 * may send an error response back or not. Note that normally
1311 * we should only check for HTTP status there, and check I/O
1312 * errors somewhere else.
1313 */
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001314 next_one:
Christopher Faulet29f17582019-05-23 11:03:26 +02001315 if (unlikely(htx_is_empty(htx) || htx->first == -1)) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001316 /* 1: have we encountered a read error ? */
1317 if (rep->flags & CF_READ_ERROR) {
Olivier Houchard865d8392019-05-03 22:46:27 +02001318 struct connection *conn = NULL;
1319
Olivier Houchard865d8392019-05-03 22:46:27 +02001320 if (objt_cs(s->si[1].end))
1321 conn = objt_cs(s->si[1].end)->conn;
1322
Christopher Fauletd9769232021-05-26 12:15:37 +02001323 /* Perform a L7 retry because server refuses the early data. */
1324 if ((si_b->flags & SI_FL_L7_RETRY) &&
1325 (s->be->retry_type & PR_RE_EARLY_ERROR) &&
1326 conn && conn->err_code == CO_ER_SSL_EARLY_FAILED &&
1327 do_l7_retry(s, si_b) == 0) {
1328 DBG_TRACE_DEVEL("leaving on L7 retry",
1329 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
1330 return 0;
1331 }
1332
Olivier Houchard6db16992019-05-17 15:40:49 +02001333 if (txn->flags & TX_NOT_FIRST)
1334 goto abort_keep_alive;
1335
Willy Tarreau4781b152021-04-06 13:53:36 +02001336 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001337 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001338 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001339 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001340 }
1341
Christopher Fauletd9769232021-05-26 12:15:37 +02001342 /* if the server refused the early data, just send a 425 */
1343 if (conn && conn->err_code == CO_ER_SSL_EARLY_FAILED)
Olivier Houchard865d8392019-05-03 22:46:27 +02001344 txn->status = 425;
Christopher Fauletd9769232021-05-26 12:15:37 +02001345 else {
1346 txn->status = 502;
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001347 stream_inc_http_fail_ctr(s);
Christopher Fauletd9769232021-05-26 12:15:37 +02001348 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001349
1350 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001351 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001352
1353 if (!(s->flags & SF_ERR_MASK))
1354 s->flags |= SF_ERR_SRVCL;
1355 if (!(s->flags & SF_FINST_MASK))
1356 s->flags |= SF_FINST_H;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001357 DBG_TRACE_DEVEL("leaving on error",
1358 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001359 return 0;
1360 }
1361
Christopher Faulet9768c262018-10-22 09:34:31 +02001362 /* 2: read timeout : return a 504 to the client. */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001363 else if (rep->flags & CF_READ_TIMEOUT) {
Olivier Houcharda254a372019-04-05 15:30:12 +02001364 if ((si_b->flags & SI_FL_L7_RETRY) &&
1365 (s->be->retry_type & PR_RE_TIMEOUT)) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001366 if (co_data(rep) || do_l7_retry(s, si_b) == 0) {
1367 DBG_TRACE_DEVEL("leaving on L7 retry",
1368 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharda254a372019-04-05 15:30:12 +02001369 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001370 }
Olivier Houcharda254a372019-04-05 15:30:12 +02001371 }
Willy Tarreau4781b152021-04-06 13:53:36 +02001372 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001373 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001374 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001375 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001376 }
1377
Christopher Faulete0768eb2018-10-03 16:38:02 +02001378 txn->status = 504;
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001379 stream_inc_http_fail_ctr(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001380 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001381 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001382
1383 if (!(s->flags & SF_ERR_MASK))
1384 s->flags |= SF_ERR_SRVTO;
1385 if (!(s->flags & SF_FINST_MASK))
1386 s->flags |= SF_FINST_H;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001387 DBG_TRACE_DEVEL("leaving on error",
1388 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001389 return 0;
1390 }
1391
Christopher Faulet9768c262018-10-22 09:34:31 +02001392 /* 3: client abort with an abortonclose */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001393 else if ((rep->flags & CF_SHUTR) && ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001394 _HA_ATOMIC_INC(&sess->fe->fe_counters.cli_aborts);
1395 _HA_ATOMIC_INC(&s->be->be_counters.cli_aborts);
William Lallemand36119de2021-03-08 15:26:48 +01001396 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001397 _HA_ATOMIC_INC(&sess->listener->counters->cli_aborts);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001398 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001399 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.cli_aborts);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001400
Christopher Faulete0768eb2018-10-03 16:38:02 +02001401 txn->status = 400;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001402 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001403
1404 if (!(s->flags & SF_ERR_MASK))
1405 s->flags |= SF_ERR_CLICL;
1406 if (!(s->flags & SF_FINST_MASK))
1407 s->flags |= SF_FINST_H;
1408
1409 /* process_stream() will take care of the error */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001410 DBG_TRACE_DEVEL("leaving on error",
1411 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001412 return 0;
1413 }
1414
Christopher Faulet9768c262018-10-22 09:34:31 +02001415 /* 4: close from server, capture the response if the server has started to respond */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001416 else if (rep->flags & CF_SHUTR) {
Olivier Houcharda254a372019-04-05 15:30:12 +02001417 if ((si_b->flags & SI_FL_L7_RETRY) &&
1418 (s->be->retry_type & PR_RE_DISCONNECTED)) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001419 if (co_data(rep) || do_l7_retry(s, si_b) == 0) {
1420 DBG_TRACE_DEVEL("leaving on L7 retry",
1421 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharda254a372019-04-05 15:30:12 +02001422 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001423 }
Olivier Houcharda254a372019-04-05 15:30:12 +02001424 }
1425
Olivier Houchard6db16992019-05-17 15:40:49 +02001426 if (txn->flags & TX_NOT_FIRST)
1427 goto abort_keep_alive;
1428
Willy Tarreau4781b152021-04-06 13:53:36 +02001429 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001430 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001431 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001432 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001433 }
1434
Christopher Faulete0768eb2018-10-03 16:38:02 +02001435 txn->status = 502;
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001436 stream_inc_http_fail_ctr(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001437 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001438 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001439
1440 if (!(s->flags & SF_ERR_MASK))
1441 s->flags |= SF_ERR_SRVCL;
1442 if (!(s->flags & SF_FINST_MASK))
1443 s->flags |= SF_FINST_H;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001444 DBG_TRACE_DEVEL("leaving on error",
1445 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001446 return 0;
1447 }
1448
Christopher Faulet9768c262018-10-22 09:34:31 +02001449 /* 5: write error to client (we don't send any message then) */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001450 else if (rep->flags & CF_WRITE_ERROR) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001451 if (txn->flags & TX_NOT_FIRST)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001452 goto abort_keep_alive;
1453
Willy Tarreau4781b152021-04-06 13:53:36 +02001454 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001455 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001456 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001457 rep->analysers &= AN_RES_FLT_END;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001458
1459 if (!(s->flags & SF_ERR_MASK))
1460 s->flags |= SF_ERR_CLICL;
1461 if (!(s->flags & SF_FINST_MASK))
1462 s->flags |= SF_FINST_H;
1463
1464 /* process_stream() will take care of the error */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001465 DBG_TRACE_DEVEL("leaving on error",
1466 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001467 return 0;
1468 }
1469
1470 channel_dont_close(rep);
1471 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001472 DBG_TRACE_DEVEL("waiting for more data",
1473 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001474 return 0;
1475 }
1476
1477 /* More interesting part now : we know that we have a complete
1478 * response which at least looks like HTTP. We have an indicator
1479 * of each header's length, so we can parse them quickly.
1480 */
Christopher Faulet29f17582019-05-23 11:03:26 +02001481 BUG_ON(htx_get_first_type(htx) != HTX_BLK_RES_SL);
Christopher Faulet297fbb42019-05-13 14:41:27 +02001482 sl = http_get_stline(htx);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001483
Christopher Faulet1f08bff2021-05-26 13:14:39 +02001484 /* Perform a L7 retry because of the status code */
1485 if ((si_b->flags & SI_FL_L7_RETRY) &&
1486 l7_status_match(s->be, sl->info.res.status) &&
1487 do_l7_retry(s, si_b) == 0) {
1488 DBG_TRACE_DEVEL("leaving on L7 retry", STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
1489 return 0;
1490 }
1491
1492 /* Now, L7 buffer is useless, it can be released */
1493 b_free(&s->si[1].l7_buffer);
1494
1495 msg->msg_state = HTTP_MSG_BODY;
1496
1497
Christopher Faulet9768c262018-10-22 09:34:31 +02001498 /* 0: we might have to print this header in debug mode */
1499 if (unlikely((global.mode & MODE_DEBUG) &&
1500 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
1501 int32_t pos;
1502
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001503 http_debug_stline("srvrep", s, sl);
Christopher Faulet9768c262018-10-22 09:34:31 +02001504
Christopher Fauleta3f15502019-05-13 15:27:23 +02001505 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001506 struct htx_blk *blk = htx_get_blk(htx, pos);
1507 enum htx_blk_type type = htx_get_blk_type(blk);
1508
1509 if (type == HTX_BLK_EOH)
1510 break;
1511 if (type != HTX_BLK_HDR)
1512 continue;
1513
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001514 http_debug_hdr("srvhdr", s,
1515 htx_get_blk_name(htx, blk),
1516 htx_get_blk_value(htx, blk));
Christopher Faulet9768c262018-10-22 09:34:31 +02001517 }
1518 }
1519
Christopher Faulet03599112018-11-27 11:21:21 +01001520 /* 1: get the status code and the version. Also set HTTP flags */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001521 txn->status = sl->info.res.status;
Christopher Faulet03599112018-11-27 11:21:21 +01001522 if (sl->flags & HTX_SL_F_VER_11)
Christopher Faulet9768c262018-10-22 09:34:31 +02001523 msg->flags |= HTTP_MSGF_VER_11;
Christopher Faulet03599112018-11-27 11:21:21 +01001524 if (sl->flags & HTX_SL_F_XFER_LEN) {
1525 msg->flags |= HTTP_MSGF_XFER_LEN;
Christopher Faulet2a408542020-11-20 14:22:37 +01001526 if (sl->flags & HTX_SL_F_CLEN)
1527 msg->flags |= HTTP_MSGF_CNT_LEN;
1528 else if (sl->flags & HTX_SL_F_CHNK)
1529 msg->flags |= HTTP_MSGF_TE_CHNK;
Christopher Faulet03599112018-11-27 11:21:21 +01001530 }
Christopher Faulet2a408542020-11-20 14:22:37 +01001531 if (sl->flags & HTX_SL_F_BODYLESS)
1532 msg->flags |= HTTP_MSGF_BODYLESS;
Christopher Faulet576c3582021-01-08 15:53:01 +01001533 if (sl->flags & HTX_SL_F_CONN_UPG)
1534 msg->flags |= HTTP_MSGF_CONN_UPG;
Christopher Faulet9768c262018-10-22 09:34:31 +02001535
1536 n = txn->status / 100;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001537 if (n < 1 || n > 5)
1538 n = 0;
Christopher Faulet9768c262018-10-22 09:34:31 +02001539
Christopher Faulete0768eb2018-10-03 16:38:02 +02001540 /* when the client triggers a 4xx from the server, it's most often due
1541 * to a missing object or permission. These events should be tracked
1542 * because if they happen often, it may indicate a brute force or a
1543 * vulnerability scan.
1544 */
1545 if (n == 4)
1546 stream_inc_http_err_ctr(s);
1547
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001548 if (n == 5 && txn->status != 501 && txn->status != 505)
1549 stream_inc_http_fail_ctr(s);
1550
Marcin Deranek3c27dda2020-05-15 18:32:51 +02001551 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001552 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.p.http.rsp[n]);
1553 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.p.http.cum_req);
Marcin Deranek3c27dda2020-05-15 18:32:51 +02001554 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001555
Christopher Faulete0768eb2018-10-03 16:38:02 +02001556 /* Adjust server's health based on status code. Note: status codes 501
1557 * and 505 are triggered on demand by client request, so we must not
1558 * count them as server failures.
1559 */
1560 if (objt_server(s->target)) {
1561 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001562 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_OK);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001563 else
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001564 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_STS);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001565 }
1566
1567 /*
1568 * We may be facing a 100-continue response, or any other informational
1569 * 1xx response which is non-final, in which case this is not the right
1570 * response, and we're waiting for the next one. Let's allow this response
1571 * to go to the client and wait for the next one. There's an exception for
1572 * 101 which is used later in the code to switch protocols.
1573 */
1574 if (txn->status < 200 &&
1575 (txn->status == 100 || txn->status >= 102)) {
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001576 FLT_STRM_CB(s, flt_http_reset(s, msg));
Christopher Faulet421e7692019-06-13 11:16:45 +02001577 htx->first = channel_htx_fwd_headers(rep, htx);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001578 msg->msg_state = HTTP_MSG_RPBEFORE;
Christopher Faulet3499f622019-09-03 15:23:54 +02001579 msg->flags = 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001580 txn->status = 0;
1581 s->logs.t_data = -1; /* was not a response yet */
Christopher Faulet7d518452020-08-31 11:07:07 +02001582 rep->flags |= CF_SEND_DONTWAIT; /* Send ASAP informational messages */
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001583 goto next_one;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001584 }
1585
Christopher Faulet6e6c7b12021-01-08 16:02:05 +01001586 /* A 101-switching-protocols must contains a Connection header with the
1587 * "upgrade" option and the request too. It means both are agree to
1588 * upgrade. It is not so strict because there is no test on the Upgrade
1589 * header content. But it is probably stronger enough for now.
1590 */
1591 if (txn->status == 101 &&
1592 (!(txn->req.flags & HTTP_MSGF_CONN_UPG) || !(txn->rsp.flags & HTTP_MSGF_CONN_UPG)))
1593 goto return_bad_res;
1594
Christopher Faulete0768eb2018-10-03 16:38:02 +02001595 /*
1596 * 2: check for cacheability.
1597 */
1598
1599 switch (txn->status) {
1600 case 200:
1601 case 203:
1602 case 204:
1603 case 206:
1604 case 300:
1605 case 301:
1606 case 404:
1607 case 405:
1608 case 410:
1609 case 414:
1610 case 501:
1611 break;
1612 default:
1613 /* RFC7231#6.1:
1614 * Responses with status codes that are defined as
1615 * cacheable by default (e.g., 200, 203, 204, 206,
1616 * 300, 301, 404, 405, 410, 414, and 501 in this
1617 * specification) can be reused by a cache with
1618 * heuristic expiration unless otherwise indicated
1619 * by the method definition or explicit cache
1620 * controls [RFC7234]; all other status codes are
1621 * not cacheable by default.
1622 */
1623 txn->flags &= ~(TX_CACHEABLE | TX_CACHE_COOK);
1624 break;
1625 }
1626
1627 /*
1628 * 3: we may need to capture headers
1629 */
1630 s->logs.logwait &= ~LW_RESP;
1631 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->res_cap))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001632 http_capture_headers(htx, s->res_cap, sess->fe->rsp_cap);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001633
Christopher Faulet9768c262018-10-22 09:34:31 +02001634 /* Skip parsing if no content length is possible. */
Christopher Fauletc75668e2020-12-07 18:10:32 +01001635 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status >= 200 && txn->status < 300) ||
Christopher Faulete0768eb2018-10-03 16:38:02 +02001636 txn->status == 101)) {
1637 /* Either we've established an explicit tunnel, or we're
1638 * switching the protocol. In both cases, we're very unlikely
1639 * to understand the next protocols. We have to switch to tunnel
1640 * mode, so that we transfer the request and responses then let
1641 * this protocol pass unmodified. When we later implement specific
1642 * parsers for such protocols, we'll want to check the Upgrade
1643 * header which contains information about that protocol for
1644 * responses with status 101 (eg: see RFC2817 about TLS).
1645 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02001646 txn->flags |= TX_CON_WANT_TUN;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001647 }
1648
Christopher Faulet61608322018-11-23 16:23:45 +01001649 /* check for NTML authentication headers in 401 (WWW-Authenticate) and
1650 * 407 (Proxy-Authenticate) responses and set the connection to private
1651 */
1652 srv_conn = cs_conn(objt_cs(s->si[1].end));
1653 if (srv_conn) {
1654 struct ist hdr;
1655 struct http_hdr_ctx ctx;
1656
1657 if (txn->status == 401)
1658 hdr = ist("WWW-Authenticate");
1659 else if (txn->status == 407)
1660 hdr = ist("Proxy-Authenticate");
1661 else
1662 goto end;
1663
1664 ctx.blk = NULL;
1665 while (http_find_header(htx, hdr, &ctx, 0)) {
Willy Tarreauf1dcced2020-05-07 19:27:02 +02001666 /* If www-authenticate contains "Negotiate", "Nego2", or "NTLM",
1667 * possibly followed by blanks and a base64 string, the connection
1668 * is private. Since it's a mess to deal with, we only check for
1669 * values starting with "NTLM" or "Nego". Note that often multiple
1670 * headers are sent by the server there.
1671 */
1672 if ((ctx.value.len >= 4 && strncasecmp(ctx.value.ptr, "Nego", 4) == 0) ||
Willy Tarreau49a1d282020-05-07 19:10:15 +02001673 (ctx.value.len >= 4 && strncasecmp(ctx.value.ptr, "NTLM", 4) == 0)) {
Olivier Houchard250031e2019-05-29 15:01:50 +02001674 sess->flags |= SESS_FL_PREFER_LAST;
Christopher Faulet08016ab2020-07-01 16:10:06 +02001675 conn_set_owner(srv_conn, sess, NULL);
Christopher Faulet21ddc742020-07-01 15:26:14 +02001676 conn_set_private(srv_conn);
Ilya Shipitsin6b79f382020-07-23 00:32:55 +05001677 /* If it fail now, the same will be done in mux->detach() callback */
Christopher Faulet08016ab2020-07-01 16:10:06 +02001678 session_add_conn(srv_conn->owner, srv_conn, srv_conn->target);
Willy Tarreauf1dcced2020-05-07 19:27:02 +02001679 break;
Olivier Houchard250031e2019-05-29 15:01:50 +02001680 }
Christopher Faulet61608322018-11-23 16:23:45 +01001681 }
1682 }
1683
1684 end:
Christopher Faulete0768eb2018-10-03 16:38:02 +02001685 /* we want to have the response time before we start processing it */
1686 s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now);
1687
1688 /* end of job, return OK */
1689 rep->analysers &= ~an_bit;
1690 rep->analyse_exp = TICK_ETERNITY;
1691 channel_auto_close(rep);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001692 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001693 return 1;
1694
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001695 return_int_err:
Willy Tarreau4781b152021-04-06 13:53:36 +02001696 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
1697 _HA_ATOMIC_INC(&s->be->be_counters.internal_errors);
William Lallemand36119de2021-03-08 15:26:48 +01001698 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001699 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Fauletb8a53712019-12-16 11:29:38 +01001700 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001701 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.internal_errors);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001702 txn->status = 500;
1703 if (!(s->flags & SF_ERR_MASK))
1704 s->flags |= SF_ERR_INTERNAL;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001705 goto return_prx_cond;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001706
1707 return_bad_res:
Willy Tarreau4781b152021-04-06 13:53:36 +02001708 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Faulet47365272018-10-31 17:40:50 +01001709 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001710 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001711 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Christopher Faulet47365272018-10-31 17:40:50 +01001712 }
Olivier Houcharde3249a92019-05-03 23:01:47 +02001713 if ((s->be->retry_type & PR_RE_JUNK_REQUEST) &&
Olivier Houchardad26d8d2019-05-10 17:48:28 +02001714 (si_b->flags & SI_FL_L7_RETRY) &&
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001715 do_l7_retry(s, si_b) == 0) {
1716 DBG_TRACE_DEVEL("leaving on L7 retry",
1717 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharde3249a92019-05-03 23:01:47 +02001718 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001719 }
Christopher Faulet47365272018-10-31 17:40:50 +01001720 txn->status = 502;
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001721 stream_inc_http_fail_ctr(s);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001722 /* fall through */
1723
Christopher Fauletb8a53712019-12-16 11:29:38 +01001724 return_prx_cond:
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001725 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet47365272018-10-31 17:40:50 +01001726
1727 if (!(s->flags & SF_ERR_MASK))
1728 s->flags |= SF_ERR_PRXCOND;
1729 if (!(s->flags & SF_FINST_MASK))
1730 s->flags |= SF_FINST_H;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001731
1732 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001733 DBG_TRACE_DEVEL("leaving on error",
1734 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulet47365272018-10-31 17:40:50 +01001735 return 0;
1736
Christopher Faulete0768eb2018-10-03 16:38:02 +02001737 abort_keep_alive:
1738 /* A keep-alive request to the server failed on a network error.
1739 * The client is required to retry. We need to close without returning
1740 * any other information so that the client retries.
1741 */
1742 txn->status = 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001743 s->logs.logwait = 0;
1744 s->logs.level = 0;
1745 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001746 http_reply_and_close(s, txn->status, NULL);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001747 DBG_TRACE_DEVEL("leaving by closing K/A connection",
1748 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001749 return 0;
1750}
1751
1752/* This function performs all the processing enabled for the current response.
1753 * It normally returns 1 unless it wants to break. It relies on buffers flags,
1754 * and updates s->res.analysers. It might make sense to explode it into several
1755 * other functions. It works like process_request (see indications above).
1756 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001757int http_process_res_common(struct stream *s, struct channel *rep, int an_bit, struct proxy *px)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001758{
1759 struct session *sess = s->sess;
1760 struct http_txn *txn = s->txn;
1761 struct http_msg *msg = &txn->rsp;
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001762 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001763 struct proxy *cur_proxy;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001764 enum rule_result ret = HTTP_RULE_RES_CONT;
1765
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001766 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
1767 return 0;
Christopher Faulet9768c262018-10-22 09:34:31 +02001768
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001769 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001770
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001771 htx = htxbuf(&rep->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001772
1773 /* The stats applet needs to adjust the Connection header but we don't
1774 * apply any filter there.
1775 */
1776 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
1777 rep->analysers &= ~an_bit;
1778 rep->analyse_exp = TICK_ETERNITY;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001779 goto end;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001780 }
1781
1782 /*
1783 * We will have to evaluate the filters.
1784 * As opposed to version 1.2, now they will be evaluated in the
1785 * filters order and not in the header order. This means that
1786 * each filter has to be validated among all headers.
1787 *
1788 * Filters are tried with ->be first, then with ->fe if it is
1789 * different from ->be.
1790 *
1791 * Maybe we are in resume condiion. In this case I choose the
1792 * "struct proxy" which contains the rule list matching the resume
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05001793 * pointer. If none of these "struct proxy" match, I initialise
Christopher Faulete0768eb2018-10-03 16:38:02 +02001794 * the process with the first one.
1795 *
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05001796 * In fact, I check only correspondence between the current list
Christopher Faulete0768eb2018-10-03 16:38:02 +02001797 * pointer and the ->fe rule list. If it doesn't match, I initialize
1798 * the loop with the ->be.
1799 */
1800 if (s->current_rule_list == &sess->fe->http_res_rules)
1801 cur_proxy = sess->fe;
1802 else
1803 cur_proxy = s->be;
1804 while (1) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001805 /* evaluate http-response rules */
1806 if (ret == HTTP_RULE_RES_CONT) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001807 ret = http_res_get_intercept_rule(cur_proxy, &cur_proxy->http_res_rules, s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001808
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001809 switch (ret) {
1810 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
1811 goto return_prx_yield;
1812
1813 case HTTP_RULE_RES_CONT:
1814 case HTTP_RULE_RES_STOP: /* nothing to do */
1815 break;
1816
1817 case HTTP_RULE_RES_DENY: /* deny or tarpit */
1818 goto deny;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001819
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001820 case HTTP_RULE_RES_ABRT: /* abort request, response already sent */
1821 goto return_prx_cond;
1822
1823 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
Christopher Fauletb8a53712019-12-16 11:29:38 +01001824 goto done;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001825
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001826 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
1827 goto return_bad_res;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001828
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001829 case HTTP_RULE_RES_ERROR: /* failed with a bad request */
1830 goto return_int_err;
1831 }
1832
1833 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001834
Christopher Faulete0768eb2018-10-03 16:38:02 +02001835 /* check whether we're already working on the frontend */
1836 if (cur_proxy == sess->fe)
1837 break;
1838 cur_proxy = sess->fe;
1839 }
1840
Christopher Faulete0768eb2018-10-03 16:38:02 +02001841 /* OK that's all we can do for 1xx responses */
1842 if (unlikely(txn->status < 200 && txn->status != 101))
Christopher Fauletf2824e62018-10-01 12:12:37 +02001843 goto end;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001844
1845 /*
1846 * Now check for a server cookie.
1847 */
1848 if (s->be->cookie_name || sess->fe->capture_name || (s->be->options & PR_O_CHK_CACHE))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001849 http_manage_server_side_cookies(s, rep);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001850
1851 /*
1852 * Check for cache-control or pragma headers if required.
1853 */
1854 if ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001855 http_check_response_for_cacheability(s, rep);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001856
1857 /*
1858 * Add server cookie in the response if needed
1859 */
1860 if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) &&
1861 !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) &&
1862 (!(s->flags & SF_DIRECT) ||
1863 ((s->be->cookie_maxidle || txn->cookie_last_date) &&
1864 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
1865 (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
1866 (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
1867 (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
1868 !(s->flags & SF_IGNORE_PRST)) {
1869 /* the server is known, it's not the one the client requested, or the
1870 * cookie's last seen date needs to be refreshed. We have to
1871 * insert a set-cookie here, except if we want to insert only on POST
1872 * requests and this one isn't. Note that servers which don't have cookies
1873 * (eg: some backup servers) will return a full cookie removal request.
1874 */
1875 if (!objt_server(s->target)->cookie) {
1876 chunk_printf(&trash,
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001877 "%s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
Christopher Faulete0768eb2018-10-03 16:38:02 +02001878 s->be->cookie_name);
1879 }
1880 else {
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001881 chunk_printf(&trash, "%s=%s", s->be->cookie_name, objt_server(s->target)->cookie);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001882
1883 if (s->be->cookie_maxidle || s->be->cookie_maxlife) {
1884 /* emit last_date, which is mandatory */
1885 trash.area[trash.data++] = COOKIE_DELIM_DATE;
1886 s30tob64((date.tv_sec+3) >> 2,
1887 trash.area + trash.data);
1888 trash.data += 5;
1889
1890 if (s->be->cookie_maxlife) {
1891 /* emit first_date, which is either the original one or
1892 * the current date.
1893 */
1894 trash.area[trash.data++] = COOKIE_DELIM_DATE;
1895 s30tob64(txn->cookie_first_date ?
1896 txn->cookie_first_date >> 2 :
1897 (date.tv_sec+3) >> 2,
1898 trash.area + trash.data);
1899 trash.data += 5;
1900 }
1901 }
1902 chunk_appendf(&trash, "; path=/");
1903 }
1904
1905 if (s->be->cookie_domain)
1906 chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain);
1907
1908 if (s->be->ck_opts & PR_CK_HTTPONLY)
1909 chunk_appendf(&trash, "; HttpOnly");
1910
1911 if (s->be->ck_opts & PR_CK_SECURE)
1912 chunk_appendf(&trash, "; Secure");
1913
Christopher Faulet2f533902020-01-21 11:06:48 +01001914 if (s->be->cookie_attrs)
1915 chunk_appendf(&trash, "; %s", s->be->cookie_attrs);
1916
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001917 if (unlikely(!http_add_header(htx, ist("Set-Cookie"), ist2(trash.area, trash.data))))
Christopher Fauletb8a53712019-12-16 11:29:38 +01001918 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001919
1920 txn->flags &= ~TX_SCK_MASK;
1921 if (__objt_server(s->target)->cookie && (s->flags & SF_DIRECT))
1922 /* the server did not change, only the date was updated */
1923 txn->flags |= TX_SCK_UPDATED;
1924 else
1925 txn->flags |= TX_SCK_INSERTED;
1926
1927 /* Here, we will tell an eventual cache on the client side that we don't
1928 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
1929 * Some caches understand the correct form: 'no-cache="set-cookie"', but
1930 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
1931 */
1932 if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
1933
1934 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
1935
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001936 if (unlikely(!http_add_header(htx, ist("Cache-control"), ist("private"))))
Christopher Fauletb8a53712019-12-16 11:29:38 +01001937 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001938 }
1939 }
1940
1941 /*
1942 * Check if result will be cacheable with a cookie.
1943 * We'll block the response if security checks have caught
1944 * nasty things such as a cacheable cookie.
1945 */
1946 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
1947 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
1948 (s->be->options & PR_O_CHK_CACHE)) {
1949 /* we're in presence of a cacheable response containing
1950 * a set-cookie header. We'll block it as requested by
1951 * the 'checkcache' option, and send an alert.
1952 */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001953 ha_alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
1954 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
1955 send_log(s->be, LOG_ALERT,
1956 "Blocking cacheable cookie in response from instance %s, server %s.\n",
1957 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
Christopher Fauletb8a53712019-12-16 11:29:38 +01001958 goto deny;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001959 }
1960
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001961 end:
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01001962 /*
1963 * Evaluate after-response rules before forwarding the response. rules
1964 * from the backend are evaluated first, then one from the frontend if
1965 * it differs.
1966 */
1967 if (!http_eval_after_res_rules(s))
1968 goto return_int_err;
1969
Christopher Fauletc2ac5e42021-03-08 18:20:09 +01001970 /* Filter the response headers if there are filters attached to the
1971 * stream.
1972 */
1973 if (HAS_FILTERS(s))
1974 rep->analysers |= AN_RES_FLT_HTTP_HDRS;
1975
Christopher Faulete0768eb2018-10-03 16:38:02 +02001976 /* Always enter in the body analyzer */
1977 rep->analysers &= ~AN_RES_FLT_XFER_DATA;
1978 rep->analysers |= AN_RES_HTTP_XFER_BODY;
1979
1980 /* if the user wants to log as soon as possible, without counting
1981 * bytes from the server, then this is the right moment. We have
1982 * to temporarily assign bytes_out to log what we currently have.
1983 */
1984 if (!LIST_ISEMPTY(&sess->fe->logformat) && !(s->logs.logwait & LW_BYTES)) {
1985 s->logs.t_close = s->logs.t_data; /* to get a valid end date */
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001986 s->logs.bytes_out = htx->data;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001987 s->do_log(s);
1988 s->logs.bytes_out = 0;
1989 }
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001990
Christopher Fauletb8a53712019-12-16 11:29:38 +01001991 done:
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01001992 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletb8a53712019-12-16 11:29:38 +01001993 rep->analysers &= ~an_bit;
1994 rep->analyse_exp = TICK_ETERNITY;
1995 return 1;
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001996
Christopher Fauletb8a53712019-12-16 11:29:38 +01001997 deny:
Willy Tarreau4781b152021-04-06 13:53:36 +02001998 _HA_ATOMIC_INC(&sess->fe->fe_counters.denied_resp);
1999 _HA_ATOMIC_INC(&s->be->be_counters.denied_resp);
William Lallemand36119de2021-03-08 15:26:48 +01002000 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02002001 _HA_ATOMIC_INC(&sess->listener->counters->denied_resp);
Christopher Fauleta08546b2019-12-16 16:07:34 +01002002 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02002003 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.denied_resp);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002004 goto return_prx_err;
2005
2006 return_int_err:
2007 txn->status = 500;
2008 if (!(s->flags & SF_ERR_MASK))
2009 s->flags |= SF_ERR_INTERNAL;
Willy Tarreau4781b152021-04-06 13:53:36 +02002010 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
2011 _HA_ATOMIC_INC(&s->be->be_counters.internal_errors);
Dragan Dosen9a006f92021-09-21 13:02:09 +02002012 if (sess->listener && sess->listener->counters)
2013 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Fauletcff0f732019-12-16 16:13:44 +01002014 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02002015 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.internal_errors);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002016 goto return_prx_err;
2017
2018 return_bad_res:
2019 txn->status = 502;
Willy Tarreau826f3ab2021-02-10 12:07:15 +01002020 stream_inc_http_fail_ctr(s);
Willy Tarreau4781b152021-04-06 13:53:36 +02002021 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Fauleta20a6532020-02-05 10:16:41 +01002022 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02002023 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Christopher Fauleta20a6532020-02-05 10:16:41 +01002024 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_RSP);
2025 }
Christopher Fauletb8a53712019-12-16 11:29:38 +01002026 /* fall through */
2027
2028 return_prx_err:
2029 http_reply_and_close(s, txn->status, http_error_message(s));
2030 /* fall through */
2031
2032 return_prx_cond:
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002033 s->logs.t_data = -1; /* was not a valid response */
2034 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002035
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002036 if (!(s->flags & SF_ERR_MASK))
2037 s->flags |= SF_ERR_PRXCOND;
2038 if (!(s->flags & SF_FINST_MASK))
2039 s->flags |= SF_FINST_H;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002040
Christopher Faulete58c0002020-03-02 16:21:01 +01002041 rep->analysers &= AN_RES_FLT_END;
2042 s->req.analysers &= AN_REQ_FLT_END;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002043 rep->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002044 DBG_TRACE_DEVEL("leaving on error",
2045 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002046 return 0;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002047
2048 return_prx_yield:
2049 channel_dont_close(rep);
2050 DBG_TRACE_DEVEL("waiting for more data",
2051 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
2052 return 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002053}
2054
2055/* This function is an analyser which forwards response body (including chunk
2056 * sizes if any). It is called as soon as we must forward, even if we forward
2057 * zero byte. The only situation where it must not be called is when we're in
2058 * tunnel mode and we want to forward till the close. It's used both to forward
2059 * remaining data and to resync after end of body. It expects the msg_state to
2060 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
2061 * read more data, or 1 once we can go on with next request or end the stream.
2062 *
2063 * It is capable of compressing response data both in content-length mode and
2064 * in chunked mode. The state machines follows different flows depending on
2065 * whether content-length and chunked modes are used, since there are no
2066 * trailers in content-length :
2067 *
2068 * chk-mode cl-mode
2069 * ,----- BODY -----.
2070 * / \
2071 * V size > 0 V chk-mode
2072 * .--> SIZE -------------> DATA -------------> CRLF
2073 * | | size == 0 | last byte |
2074 * | v final crlf v inspected |
2075 * | TRAILERS -----------> DONE |
2076 * | |
2077 * `----------------------------------------------'
2078 *
2079 * Compression only happens in the DATA state, and must be flushed in final
2080 * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding
2081 * is performed at once on final states for all bytes parsed, or when leaving
2082 * on missing data.
2083 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002084int http_response_forward_body(struct stream *s, struct channel *res, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +02002085{
2086 struct session *sess = s->sess;
2087 struct http_txn *txn = s->txn;
2088 struct http_msg *msg = &s->txn->rsp;
Christopher Faulet9768c262018-10-22 09:34:31 +02002089 struct htx *htx;
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002090 int ret;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002091
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002092 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002093
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002094 htx = htxbuf(&res->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002095
Christopher Fauletb9a92f32019-09-09 10:15:21 +02002096 if (htx->flags & HTX_FL_PARSING_ERROR)
2097 goto return_bad_res;
2098 if (htx->flags & HTX_FL_PROCESSING_ERROR)
2099 goto return_int_err;
2100
Christopher Faulete0768eb2018-10-03 16:38:02 +02002101 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Christopher Fauletf2824e62018-10-01 12:12:37 +02002102 ((res->flags & CF_SHUTW) && (res->to_forward || co_data(res)))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02002103 /* Output closed while we were sending data. We must abort and
2104 * wake the other side up.
2105 */
Christopher Faulete0768eb2018-10-03 16:38:02 +02002106 msg->msg_state = HTTP_MSG_ERROR;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002107 http_end_response(s);
2108 http_end_request(s);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002109 DBG_TRACE_DEVEL("leaving on error",
2110 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002111 return 1;
2112 }
2113
Christopher Faulet9768c262018-10-22 09:34:31 +02002114 if (msg->msg_state == HTTP_MSG_BODY)
2115 msg->msg_state = HTTP_MSG_DATA;
2116
Christopher Faulete0768eb2018-10-03 16:38:02 +02002117 /* in most states, we should abort in case of early close */
2118 channel_auto_close(res);
2119
Christopher Faulete0768eb2018-10-03 16:38:02 +02002120 if (res->to_forward) {
Christopher Faulet66af0b22019-03-22 14:54:52 +01002121 if (res->to_forward == CHN_INFINITE_FORWARD) {
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002122 if (res->flags & CF_EOI)
2123 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet66af0b22019-03-22 14:54:52 +01002124 }
2125 else {
2126 /* We can't process the buffer's contents yet */
2127 res->flags |= CF_WAKE_WRITE;
2128 goto missing_data_or_waiting;
2129 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02002130 }
2131
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002132 if (msg->msg_state >= HTTP_MSG_ENDING)
2133 goto ending;
2134
Christopher Fauletc75668e2020-12-07 18:10:32 +01002135 if ((txn->meth == HTTP_METH_CONNECT && txn->status >= 200 && txn->status < 300) || txn->status == 101 ||
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002136 (!(msg->flags & HTTP_MSGF_XFER_LEN) && !HAS_RSP_DATA_FILTERS(s))) {
2137 msg->msg_state = HTTP_MSG_ENDING;
2138 goto ending;
2139 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002140
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002141 /* Forward input data. We get it by removing all outgoing data not
2142 * forwarded yet from HTX data size. If there are some data filters, we
2143 * let them decide the amount of data to forward.
Christopher Faulet9768c262018-10-22 09:34:31 +02002144 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002145 if (HAS_RSP_DATA_FILTERS(s)) {
2146 ret = flt_http_payload(s, msg, htx->data);
2147 if (ret < 0)
2148 goto return_bad_res;
Christopher Faulet421e7692019-06-13 11:16:45 +02002149 c_adv(res, ret);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002150 }
2151 else {
Christopher Faulet421e7692019-06-13 11:16:45 +02002152 c_adv(res, htx->data - co_data(res));
Christopher Faulet66af0b22019-03-22 14:54:52 +01002153 if (msg->flags & HTTP_MSGF_XFER_LEN)
2154 channel_htx_forward_forever(res, htx);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002155 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002156
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002157 if (htx->data != co_data(res))
2158 goto missing_data_or_waiting;
2159
2160 if (!(msg->flags & HTTP_MSGF_XFER_LEN) && res->flags & CF_SHUTR) {
2161 msg->msg_state = HTTP_MSG_ENDING;
2162 goto ending;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002163 }
2164
Christopher Faulet9768c262018-10-22 09:34:31 +02002165 /* Check if the end-of-message is reached and if so, switch the message
Christopher Fauletd20fdb02019-06-13 16:43:22 +02002166 * in HTTP_MSG_ENDING state. Then if all data was marked to be
2167 * forwarded, set the state to HTTP_MSG_DONE.
Christopher Faulet9768c262018-10-22 09:34:31 +02002168 */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002169 if (!(htx->flags & HTX_FL_EOM))
Christopher Faulet9768c262018-10-22 09:34:31 +02002170 goto missing_data_or_waiting;
2171
Christopher Fauletd20fdb02019-06-13 16:43:22 +02002172 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet9768c262018-10-22 09:34:31 +02002173
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002174 ending:
Christopher Faulet2151cdd2020-07-22 16:34:59 +02002175 res->flags &= ~CF_EXPECT_MORE; /* no more data are expected */
2176
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002177 /* other states, ENDING...TUNNEL */
2178 if (msg->msg_state >= HTTP_MSG_DONE)
2179 goto done;
Christopher Faulet9768c262018-10-22 09:34:31 +02002180
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002181 if (HAS_RSP_DATA_FILTERS(s)) {
2182 ret = flt_http_end(s, msg);
2183 if (ret <= 0) {
2184 if (!ret)
2185 goto missing_data_or_waiting;
2186 goto return_bad_res;
2187 }
2188 }
2189
Christopher Fauletc75668e2020-12-07 18:10:32 +01002190 if ((txn->meth == HTTP_METH_CONNECT && txn->status >= 200 && txn->status < 300) || txn->status == 101 ||
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002191 !(msg->flags & HTTP_MSGF_XFER_LEN)) {
2192 msg->msg_state = HTTP_MSG_TUNNEL;
2193 goto ending;
2194 }
2195 else {
2196 msg->msg_state = HTTP_MSG_DONE;
2197 res->to_forward = 0;
2198 }
2199
2200 done:
2201
2202 channel_dont_close(res);
2203
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002204 http_end_response(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002205 if (!(res->analysers & an_bit)) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002206 http_end_request(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002207 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
2208 if (res->flags & CF_SHUTW) {
2209 /* response errors are most likely due to the
2210 * client aborting the transfer. */
Christopher Faulet93e02d82019-03-08 14:18:50 +01002211 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002212 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02002213 goto return_bad_res;
2214 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002215 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002216 return 1;
2217 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002218 DBG_TRACE_DEVEL("waiting for the end of the HTTP txn",
2219 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002220 return 0;
2221
2222 missing_data_or_waiting:
2223 if (res->flags & CF_SHUTW)
Christopher Faulet93e02d82019-03-08 14:18:50 +01002224 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002225
2226 /* stop waiting for data if the input is closed before the end. If the
2227 * client side was already closed, it means that the client has aborted,
2228 * so we don't want to count this as a server abort. Otherwise it's a
2229 * server abort.
2230 */
Christopher Fauletd20fdb02019-06-13 16:43:22 +02002231 if (msg->msg_state < HTTP_MSG_ENDING && res->flags & CF_SHUTR) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02002232 if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))
Christopher Faulet93e02d82019-03-08 14:18:50 +01002233 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002234 /* If we have some pending data, we continue the processing */
Christopher Faulet93e02d82019-03-08 14:18:50 +01002235 if (htx_is_empty(htx))
2236 goto return_srv_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002237 }
2238
Christopher Faulete0768eb2018-10-03 16:38:02 +02002239 /* When TE: chunked is used, we need to get there again to parse
2240 * remaining chunks even if the server has closed, so we don't want to
Christopher Faulet9768c262018-10-22 09:34:31 +02002241 * set CF_DONTCLOSE. Similarly when there is a content-leng or if there
2242 * are filters registered on the stream, we don't want to forward a
2243 * close
Christopher Faulete0768eb2018-10-03 16:38:02 +02002244 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002245 if ((msg->flags & HTTP_MSGF_XFER_LEN) || HAS_RSP_DATA_FILTERS(s))
Christopher Faulete0768eb2018-10-03 16:38:02 +02002246 channel_dont_close(res);
2247
2248 /* We know that more data are expected, but we couldn't send more that
2249 * what we did. So we always set the CF_EXPECT_MORE flag so that the
2250 * system knows it must not set a PUSH on this first part. Interactive
2251 * modes are already handled by the stream sock layer. We must not do
2252 * this in content-length mode because it could present the MSG_MORE
2253 * flag with the last block of forwarded data, which would cause an
2254 * additional delay to be observed by the receiver.
2255 */
Christopher Faulet2151cdd2020-07-22 16:34:59 +02002256 if (HAS_RSP_DATA_FILTERS(s))
Christopher Faulete0768eb2018-10-03 16:38:02 +02002257 res->flags |= CF_EXPECT_MORE;
2258
2259 /* the stream handler will take care of timeouts and errors */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002260 DBG_TRACE_DEVEL("waiting for more data to forward",
2261 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002262 return 0;
2263
Christopher Faulet93e02d82019-03-08 14:18:50 +01002264 return_srv_abort:
Willy Tarreau4781b152021-04-06 13:53:36 +02002265 _HA_ATOMIC_INC(&sess->fe->fe_counters.srv_aborts);
2266 _HA_ATOMIC_INC(&s->be->be_counters.srv_aborts);
William Lallemand36119de2021-03-08 15:26:48 +01002267 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02002268 _HA_ATOMIC_INC(&sess->listener->counters->srv_aborts);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002269 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02002270 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.srv_aborts);
Willy Tarreau826f3ab2021-02-10 12:07:15 +01002271 stream_inc_http_fail_ctr(s);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002272 if (!(s->flags & SF_ERR_MASK))
2273 s->flags |= SF_ERR_SRVCL;
2274 goto return_error;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002275
Christopher Faulet93e02d82019-03-08 14:18:50 +01002276 return_cli_abort:
Willy Tarreau4781b152021-04-06 13:53:36 +02002277 _HA_ATOMIC_INC(&sess->fe->fe_counters.cli_aborts);
2278 _HA_ATOMIC_INC(&s->be->be_counters.cli_aborts);
William Lallemand36119de2021-03-08 15:26:48 +01002279 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02002280 _HA_ATOMIC_INC(&sess->listener->counters->cli_aborts);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002281 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02002282 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.cli_aborts);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002283 if (!(s->flags & SF_ERR_MASK))
2284 s->flags |= SF_ERR_CLICL;
2285 goto return_error;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002286
Christopher Fauletb9a92f32019-09-09 10:15:21 +02002287 return_int_err:
Willy Tarreau4781b152021-04-06 13:53:36 +02002288 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
2289 _HA_ATOMIC_INC(&s->be->be_counters.internal_errors);
William Lallemand36119de2021-03-08 15:26:48 +01002290 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02002291 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002292 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02002293 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.internal_errors);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02002294 if (!(s->flags & SF_ERR_MASK))
2295 s->flags |= SF_ERR_INTERNAL;
2296 goto return_error;
2297
Christopher Faulet93e02d82019-03-08 14:18:50 +01002298 return_bad_res:
Willy Tarreau4781b152021-04-06 13:53:36 +02002299 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002300 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02002301 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002302 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_RSP);
2303 }
Willy Tarreau826f3ab2021-02-10 12:07:15 +01002304 stream_inc_http_fail_ctr(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002305 if (!(s->flags & SF_ERR_MASK))
Christopher Faulet93e02d82019-03-08 14:18:50 +01002306 s->flags |= SF_ERR_SRVCL;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002307 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +02002308
Christopher Faulet93e02d82019-03-08 14:18:50 +01002309 return_error:
Christopher Faulete0768eb2018-10-03 16:38:02 +02002310 /* don't send any error message as we're in the body */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002311 http_reply_and_close(s, txn->status, NULL);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002312 if (!(s->flags & SF_FINST_MASK))
2313 s->flags |= SF_FINST_D;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002314 DBG_TRACE_DEVEL("leaving on error",
2315 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002316 return 0;
2317}
2318
Christopher Fauletf2824e62018-10-01 12:12:37 +02002319/* Perform an HTTP redirect based on the information in <rule>. The function
Willy Tarreaubc1223b2021-09-02 16:54:33 +02002320 * returns zero in case of an irrecoverable error such as too large a request
2321 * to build a valid response, 1 in case of successful redirect (hence the rule
2322 * is final), or 2 if the rule has to be silently skipped.
Christopher Fauletf2824e62018-10-01 12:12:37 +02002323 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002324int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn)
Christopher Fauletf2824e62018-10-01 12:12:37 +02002325{
Christopher Faulet99daf282018-11-28 22:58:13 +01002326 struct channel *req = &s->req;
2327 struct channel *res = &s->res;
2328 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01002329 struct htx_sl *sl;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002330 struct buffer *chunk;
Christopher Faulet99daf282018-11-28 22:58:13 +01002331 struct ist status, reason, location;
2332 unsigned int flags;
Christopher Faulet08e66462019-05-23 16:44:59 +02002333 int close = 0; /* Try to keep the connection alive byt default */
Christopher Fauletf2824e62018-10-01 12:12:37 +02002334
2335 chunk = alloc_trash_chunk();
Christopher Fauletb8a53712019-12-16 11:29:38 +01002336 if (!chunk) {
2337 if (!(s->flags & SF_ERR_MASK))
2338 s->flags |= SF_ERR_RESOURCE;
Christopher Faulet99daf282018-11-28 22:58:13 +01002339 goto fail;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002340 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002341
Christopher Faulet99daf282018-11-28 22:58:13 +01002342 /*
2343 * Create the location
2344 */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002345 htx = htxbuf(&req->buf);
Christopher Fauletf2824e62018-10-01 12:12:37 +02002346 switch(rule->type) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002347 case REDIRECT_TYPE_SCHEME: {
2348 struct http_hdr_ctx ctx;
2349 struct ist path, host;
Amaury Denoyellec453f952021-07-06 11:40:12 +02002350 struct http_uri_parser parser;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002351
Christopher Faulet99daf282018-11-28 22:58:13 +01002352 host = ist("");
2353 ctx.blk = NULL;
2354 if (http_find_header(htx, ist("Host"), &ctx, 0))
2355 host = ctx.value;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002356
Christopher Faulet297fbb42019-05-13 14:41:27 +02002357 sl = http_get_stline(htx);
Amaury Denoyellec453f952021-07-06 11:40:12 +02002358 parser = http_uri_parser_init(htx_sl_req_uri(sl));
2359 path = http_parse_path(&parser);
Christopher Faulet99daf282018-11-28 22:58:13 +01002360 /* build message using path */
Tim Duesterhused526372020-03-05 17:56:33 +01002361 if (isttest(path)) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002362 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2363 int qs = 0;
2364 while (qs < path.len) {
2365 if (*(path.ptr + qs) == '?') {
2366 path.len = qs;
2367 break;
2368 }
2369 qs++;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002370 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002371 }
2372 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002373 else
2374 path = ist("/");
Christopher Fauletf2824e62018-10-01 12:12:37 +02002375
Christopher Faulet99daf282018-11-28 22:58:13 +01002376 if (rule->rdr_str) { /* this is an old "redirect" rule */
2377 /* add scheme */
2378 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2379 goto fail;
2380 }
2381 else {
2382 /* add scheme with executing log format */
2383 chunk->data += build_logline(s, chunk->area + chunk->data,
2384 chunk->size - chunk->data,
2385 &rule->rdr_fmt);
2386 }
2387 /* add "://" + host + path */
2388 if (!chunk_memcat(chunk, "://", 3) ||
2389 !chunk_memcat(chunk, host.ptr, host.len) ||
2390 !chunk_memcat(chunk, path.ptr, path.len))
2391 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002392
Christopher Faulet99daf282018-11-28 22:58:13 +01002393 /* append a slash at the end of the location if needed and missing */
2394 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
2395 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
2396 if (chunk->data + 1 >= chunk->size)
2397 goto fail;
2398 chunk->area[chunk->data++] = '/';
2399 }
2400 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002401 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002402
Christopher Faulet99daf282018-11-28 22:58:13 +01002403 case REDIRECT_TYPE_PREFIX: {
2404 struct ist path;
Amaury Denoyellec453f952021-07-06 11:40:12 +02002405 struct http_uri_parser parser;
Christopher Faulet99daf282018-11-28 22:58:13 +01002406
Christopher Faulet297fbb42019-05-13 14:41:27 +02002407 sl = http_get_stline(htx);
Amaury Denoyellec453f952021-07-06 11:40:12 +02002408 parser = http_uri_parser_init(htx_sl_req_uri(sl));
2409 path = http_parse_path(&parser);
Christopher Faulet99daf282018-11-28 22:58:13 +01002410 /* build message using path */
Tim Duesterhused526372020-03-05 17:56:33 +01002411 if (isttest(path)) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002412 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2413 int qs = 0;
2414 while (qs < path.len) {
2415 if (*(path.ptr + qs) == '?') {
2416 path.len = qs;
2417 break;
2418 }
2419 qs++;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002420 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002421 }
2422 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002423 else
2424 path = ist("/");
Christopher Fauletf2824e62018-10-01 12:12:37 +02002425
Christopher Faulet99daf282018-11-28 22:58:13 +01002426 if (rule->rdr_str) { /* this is an old "redirect" rule */
2427 /* add prefix. Note that if prefix == "/", we don't want to
2428 * add anything, otherwise it makes it hard for the user to
2429 * configure a self-redirection.
2430 */
2431 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
2432 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2433 goto fail;
2434 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002435 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002436 else {
2437 /* add prefix with executing log format */
2438 chunk->data += build_logline(s, chunk->area + chunk->data,
2439 chunk->size - chunk->data,
2440 &rule->rdr_fmt);
2441 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002442
Christopher Faulet99daf282018-11-28 22:58:13 +01002443 /* add path */
2444 if (!chunk_memcat(chunk, path.ptr, path.len))
2445 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002446
Christopher Faulet99daf282018-11-28 22:58:13 +01002447 /* append a slash at the end of the location if needed and missing */
2448 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
2449 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
2450 if (chunk->data + 1 >= chunk->size)
2451 goto fail;
2452 chunk->area[chunk->data++] = '/';
2453 }
2454 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002455 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002456 case REDIRECT_TYPE_LOCATION:
2457 default:
2458 if (rule->rdr_str) { /* this is an old "redirect" rule */
2459 /* add location */
2460 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2461 goto fail;
2462 }
2463 else {
2464 /* add location with executing log format */
Willy Tarreaubc1223b2021-09-02 16:54:33 +02002465 int len = build_logline(s, chunk->area + chunk->data,
2466 chunk->size - chunk->data,
2467 &rule->rdr_fmt);
2468 if (!len && rule->flags & REDIRECT_FLAG_IGNORE_EMPTY)
2469 return 2;
2470
2471 chunk->data += len;
Christopher Faulet99daf282018-11-28 22:58:13 +01002472 }
2473 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002474 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002475 location = ist2(chunk->area, chunk->data);
2476
2477 /*
2478 * Create the 30x response
2479 */
2480 switch (rule->code) {
2481 case 308:
2482 status = ist("308");
2483 reason = ist("Permanent Redirect");
2484 break;
2485 case 307:
2486 status = ist("307");
2487 reason = ist("Temporary Redirect");
2488 break;
2489 case 303:
2490 status = ist("303");
2491 reason = ist("See Other");
2492 break;
2493 case 301:
2494 status = ist("301");
2495 reason = ist("Moved Permanently");
2496 break;
2497 case 302:
2498 default:
2499 status = ist("302");
2500 reason = ist("Found");
2501 break;
2502 }
2503
Christopher Faulet08e66462019-05-23 16:44:59 +02002504 if (!(txn->req.flags & HTTP_MSGF_BODYLESS) && txn->req.msg_state != HTTP_MSG_DONE)
2505 close = 1;
2506
Christopher Faulet99daf282018-11-28 22:58:13 +01002507 htx = htx_from_buf(&res->buf);
Kevin Zhu96b36392020-01-07 09:42:55 +01002508 /* Trim any possible response */
2509 channel_htx_truncate(&s->res, htx);
Christopher Faulet99daf282018-11-28 22:58:13 +01002510 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
2511 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), status, reason);
2512 if (!sl)
2513 goto fail;
2514 sl->info.res.status = rule->code;
2515 s->txn->status = rule->code;
2516
Christopher Faulet08e66462019-05-23 16:44:59 +02002517 if (close && !htx_add_header(htx, ist("Connection"), ist("close")))
2518 goto fail;
2519
2520 if (!htx_add_header(htx, ist("Content-length"), ist("0")) ||
Christopher Faulet99daf282018-11-28 22:58:13 +01002521 !htx_add_header(htx, ist("Location"), location))
2522 goto fail;
2523
2524 if (rule->code == 302 || rule->code == 303 || rule->code == 307) {
2525 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")))
2526 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002527 }
2528
2529 if (rule->cookie_len) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002530 if (!htx_add_header(htx, ist("Set-Cookie"), ist2(rule->cookie_str, rule->cookie_len)))
2531 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002532 }
2533
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002534 if (!htx_add_endof(htx, HTX_BLK_EOH))
Christopher Faulet99daf282018-11-28 22:58:13 +01002535 goto fail;
2536
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002537 htx->flags |= HTX_FL_EOM;
Kevin Zhu96b36392020-01-07 09:42:55 +01002538 htx_to_buf(htx, &res->buf);
Christopher Fauleta72a7e42020-01-28 09:28:11 +01002539 if (!http_forward_proxy_resp(s, 1))
2540 goto fail;
Christopher Faulet99daf282018-11-28 22:58:13 +01002541
Christopher Faulet60b33a52020-01-28 09:18:10 +01002542 if (rule->flags & REDIRECT_FLAG_FROM_REQ) {
2543 /* let's log the request time */
2544 s->logs.tv_request = now;
Christopher Fauletd3475882021-10-04 14:16:46 +02002545 req->analysers &= AN_REQ_FLT_END;
Christopher Faulet99daf282018-11-28 22:58:13 +01002546
Christopher Faulet60b33a52020-01-28 09:18:10 +01002547 if (s->sess->fe == s->be) /* report it if the request was intercepted by the frontend */
Willy Tarreau4781b152021-04-06 13:53:36 +02002548 _HA_ATOMIC_INC(&s->sess->fe->fe_counters.intercepted_req);
Christopher Faulet60b33a52020-01-28 09:18:10 +01002549 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002550
2551 if (!(s->flags & SF_ERR_MASK))
2552 s->flags |= SF_ERR_LOCAL;
2553 if (!(s->flags & SF_FINST_MASK))
Christopher Faulet60b33a52020-01-28 09:18:10 +01002554 s->flags |= ((rule->flags & REDIRECT_FLAG_FROM_REQ) ? SF_FINST_R : SF_FINST_H);
Christopher Fauletf2824e62018-10-01 12:12:37 +02002555
Christopher Faulet99daf282018-11-28 22:58:13 +01002556 free_trash_chunk(chunk);
2557 return 1;
2558
2559 fail:
2560 /* If an error occurred, remove the incomplete HTTP response from the
2561 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01002562 channel_htx_truncate(res, htxbuf(&res->buf));
Christopher Fauletf2824e62018-10-01 12:12:37 +02002563 free_trash_chunk(chunk);
Christopher Faulet99daf282018-11-28 22:58:13 +01002564 return 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002565}
2566
Christopher Faulet92d34fe2019-12-17 09:20:34 +01002567/* Replace all headers matching the name <name>. The header value is replaced if
2568 * it matches the regex <re>. <str> is used for the replacement. If <full> is
2569 * set to 1, the full-line is matched and replaced. Otherwise, comma-separated
2570 * values are evaluated one by one. It returns 0 on success and -1 on error.
2571 */
2572int http_replace_hdrs(struct stream* s, struct htx *htx, struct ist name,
2573 const char *str, struct my_regex *re, int full)
Christopher Faulet72333522018-10-24 11:25:02 +02002574{
2575 struct http_hdr_ctx ctx;
2576 struct buffer *output = get_trash_chunk();
2577
Christopher Faulet72333522018-10-24 11:25:02 +02002578 ctx.blk = NULL;
Christopher Faulet92d34fe2019-12-17 09:20:34 +01002579 while (http_find_header(htx, name, &ctx, full)) {
Christopher Faulet72333522018-10-24 11:25:02 +02002580 if (!regex_exec_match2(re, ctx.value.ptr, ctx.value.len, MAX_MATCH, pmatch, 0))
2581 continue;
2582
2583 output->data = exp_replace(output->area, output->size, ctx.value.ptr, str, pmatch);
2584 if (output->data == -1)
2585 return -1;
2586 if (!http_replace_header_value(htx, &ctx, ist2(output->area, output->data)))
2587 return -1;
2588 }
2589 return 0;
2590}
2591
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002592/* This function executes one of the set-{method,path,query,uri} actions. It
2593 * takes the string from the variable 'replace' with length 'len', then modifies
2594 * the relevant part of the request line accordingly. Then it updates various
2595 * pointers to the next elements which were moved, and the total buffer length.
2596 * It finds the action to be performed in p[2], previously filled by function
2597 * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal
2598 * error, though this can be revisited when this code is finally exploited.
2599 *
2600 * 'action' can be '0' to replace method, '1' to replace path, '2' to replace
Christopher Faulet312294f2020-09-02 17:17:44 +02002601 * query string, 3 to replace uri or 4 to replace the path+query.
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002602 *
2603 * In query string case, the mark question '?' must be set at the start of the
2604 * string by the caller, event if the replacement query string is empty.
2605 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002606int http_req_replace_stline(int action, const char *replace, int len,
2607 struct proxy *px, struct stream *s)
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002608{
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002609 struct htx *htx = htxbuf(&s->req.buf);
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002610
2611 switch (action) {
2612 case 0: // method
2613 if (!http_replace_req_meth(htx, ist2(replace, len)))
2614 return -1;
2615 break;
2616
2617 case 1: // path
Christopher Fauletb8ce5052020-08-31 16:11:57 +02002618 if (!http_replace_req_path(htx, ist2(replace, len), 0))
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002619 return -1;
2620 break;
2621
2622 case 2: // query
2623 if (!http_replace_req_query(htx, ist2(replace, len)))
2624 return -1;
2625 break;
2626
2627 case 3: // uri
2628 if (!http_replace_req_uri(htx, ist2(replace, len)))
2629 return -1;
2630 break;
2631
Christopher Faulet312294f2020-09-02 17:17:44 +02002632 case 4: // path + query
2633 if (!http_replace_req_path(htx, ist2(replace, len), 1))
2634 return -1;
2635 break;
2636
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002637 default:
2638 return -1;
2639 }
2640 return 0;
2641}
2642
2643/* This function replace the HTTP status code and the associated message. The
Christopher Faulete00d06c2019-12-16 17:18:42 +01002644 * variable <status> contains the new status code. This function never fails. It
2645 * returns 0 in case of success, -1 in case of internal error.
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002646 */
Christopher Faulet96bff762019-12-17 13:46:18 +01002647int http_res_set_status(unsigned int status, struct ist reason, struct stream *s)
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002648{
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002649 struct htx *htx = htxbuf(&s->res.buf);
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002650 char *res;
2651
2652 chunk_reset(&trash);
2653 res = ultoa_o(status, trash.area, trash.size);
2654 trash.data = res - trash.area;
2655
2656 /* Do we have a custom reason format string? */
Tim Duesterhuse296d3e2020-03-05 17:56:31 +01002657 if (!isttest(reason)) {
Christopher Faulet96bff762019-12-17 13:46:18 +01002658 const char *str = http_get_reason(status);
Tim Duesterhusdcf753a2021-03-04 17:31:47 +01002659 reason = ist(str);
Christopher Faulet96bff762019-12-17 13:46:18 +01002660 }
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002661
Christopher Fauletbde2c4c2020-08-31 16:43:34 +02002662 if (!http_replace_res_status(htx, ist2(trash.area, trash.data), reason))
Christopher Faulete00d06c2019-12-16 17:18:42 +01002663 return -1;
2664 return 0;
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002665}
2666
Christopher Faulet3e964192018-10-24 11:39:23 +02002667/* Executes the http-request rules <rules> for stream <s>, proxy <px> and
2668 * transaction <txn>. Returns the verdict of the first rule that prevents
2669 * further processing of the request (auth, deny, ...), and defaults to
2670 * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or
2671 * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT
2672 * on txn->flags if it encounters a tarpit rule. If <deny_status> is not NULL
2673 * and a deny/tarpit rule is matched, it will be filled with this rule's deny
2674 * status.
2675 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002676static enum rule_result http_req_get_intercept_rule(struct proxy *px, struct list *rules,
Christopher Fauletb58f62b2020-01-13 16:40:13 +01002677 struct stream *s)
Christopher Faulet3e964192018-10-24 11:39:23 +02002678{
2679 struct session *sess = strm_sess(s);
2680 struct http_txn *txn = s->txn;
Christopher Faulet3e964192018-10-24 11:39:23 +02002681 struct act_rule *rule;
Christopher Faulet3e964192018-10-24 11:39:23 +02002682 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002683 int act_opts = 0;
Christopher Faulet3e964192018-10-24 11:39:23 +02002684
Christopher Faulet3e964192018-10-24 11:39:23 +02002685 /* If "the current_rule_list" match the executed rule list, we are in
2686 * resume condition. If a resume is needed it is always in the action
2687 * and never in the ACL or converters. In this case, we initialise the
2688 * current rule, and go to the action execution point.
2689 */
2690 if (s->current_rule) {
2691 rule = s->current_rule;
2692 s->current_rule = NULL;
2693 if (s->current_rule_list == rules)
2694 goto resume_execution;
2695 }
2696 s->current_rule_list = rules;
2697
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002698 /* start the ruleset evaluation in strict mode */
2699 txn->req.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01002700
Christopher Faulet3e964192018-10-24 11:39:23 +02002701 list_for_each_entry(rule, rules, list) {
2702 /* check optional condition */
2703 if (rule->cond) {
2704 int ret;
2705
2706 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
2707 ret = acl_pass(ret);
2708
2709 if (rule->cond->pol == ACL_COND_UNLESS)
2710 ret = !ret;
2711
2712 if (!ret) /* condition not matched */
2713 continue;
2714 }
2715
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002716 act_opts |= ACT_OPT_FIRST;
Christopher Faulet3e964192018-10-24 11:39:23 +02002717 resume_execution:
Amaury Denoyelle03517732021-05-07 14:25:01 +02002718 if (rule->kw->flags & KWF_EXPERIMENTAL)
2719 mark_tainted(TAINTED_ACTION_EXP_EXECUTED);
2720
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002721 /* Always call the action function if defined */
2722 if (rule->action_ptr) {
2723 if ((s->req.flags & CF_READ_ERROR) ||
2724 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
2725 (px->options & PR_O_ABRT_CLOSE)))
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002726 act_opts |= ACT_OPT_FINAL;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002727
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002728 switch (rule->action_ptr(rule, px, sess, s, act_opts)) {
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002729 case ACT_RET_CONT:
2730 break;
2731 case ACT_RET_STOP:
2732 rule_ret = HTTP_RULE_RES_STOP;
2733 goto end;
2734 case ACT_RET_YIELD:
2735 s->current_rule = rule;
2736 rule_ret = HTTP_RULE_RES_YIELD;
2737 goto end;
2738 case ACT_RET_ERR:
2739 rule_ret = HTTP_RULE_RES_ERROR;
2740 goto end;
2741 case ACT_RET_DONE:
2742 rule_ret = HTTP_RULE_RES_DONE;
2743 goto end;
2744 case ACT_RET_DENY:
Christopher Fauletb58f62b2020-01-13 16:40:13 +01002745 if (txn->status == -1)
2746 txn->status = 403;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002747 rule_ret = HTTP_RULE_RES_DENY;
2748 goto end;
2749 case ACT_RET_ABRT:
2750 rule_ret = HTTP_RULE_RES_ABRT;
2751 goto end;
2752 case ACT_RET_INV:
2753 rule_ret = HTTP_RULE_RES_BADREQ;
2754 goto end;
2755 }
2756 continue; /* eval the next rule */
2757 }
2758
2759 /* If not action function defined, check for known actions */
Christopher Faulet3e964192018-10-24 11:39:23 +02002760 switch (rule->action) {
2761 case ACT_ACTION_ALLOW:
2762 rule_ret = HTTP_RULE_RES_STOP;
2763 goto end;
2764
2765 case ACT_ACTION_DENY:
Christopher Faulet5cb513a2020-05-13 17:56:56 +02002766 txn->status = rule->arg.http_reply->status;
2767 txn->http_reply = rule->arg.http_reply;
Christopher Faulet3e964192018-10-24 11:39:23 +02002768 rule_ret = HTTP_RULE_RES_DENY;
2769 goto end;
2770
2771 case ACT_HTTP_REQ_TARPIT:
2772 txn->flags |= TX_CLTARPIT;
Christopher Faulet5cb513a2020-05-13 17:56:56 +02002773 txn->status = rule->arg.http_reply->status;
2774 txn->http_reply = rule->arg.http_reply;
Christopher Faulet3e964192018-10-24 11:39:23 +02002775 rule_ret = HTTP_RULE_RES_DENY;
2776 goto end;
2777
Willy Tarreaubc1223b2021-09-02 16:54:33 +02002778 case ACT_HTTP_REDIR: {
2779 int ret = http_apply_redirect_rule(rule->arg.redir, s, txn);
2780
2781 if (ret == 2) // 2 == skip
2782 break;
2783
2784 rule_ret = ret ? HTTP_RULE_RES_ABRT : HTTP_RULE_RES_ERROR;
Christopher Faulet3e964192018-10-24 11:39:23 +02002785 goto end;
Willy Tarreaubc1223b2021-09-02 16:54:33 +02002786 }
Christopher Faulet3e964192018-10-24 11:39:23 +02002787
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002788 /* other flags exists, but normally, they never be matched. */
Christopher Faulet3e964192018-10-24 11:39:23 +02002789 default:
2790 break;
2791 }
2792 }
2793
2794 end:
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002795 /* if the ruleset evaluation is finished reset the strict mode */
Christopher Faulet46f95542019-12-20 10:07:22 +01002796 if (rule_ret != HTTP_RULE_RES_YIELD)
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002797 txn->req.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01002798
Christopher Faulet3e964192018-10-24 11:39:23 +02002799 /* we reached the end of the rules, nothing to report */
2800 return rule_ret;
2801}
2802
2803/* Executes the http-response rules <rules> for stream <s> and proxy <px>. It
2804 * returns one of 5 possible statuses: HTTP_RULE_RES_CONT, HTTP_RULE_RES_STOP,
2805 * HTTP_RULE_RES_DONE, HTTP_RULE_RES_YIELD, or HTTP_RULE_RES_BADREQ. If *CONT
2806 * is returned, the process can continue the evaluation of next rule list. If
2807 * *STOP or *DONE is returned, the process must stop the evaluation. If *BADREQ
2808 * is returned, it means the operation could not be processed and a server error
Christopher Fauleta53abad2020-05-13 08:12:22 +02002809 * must be returned. If *YIELD is returned, the caller must call again the
2810 * function with the same context.
Christopher Faulet3e964192018-10-24 11:39:23 +02002811 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002812static enum rule_result http_res_get_intercept_rule(struct proxy *px, struct list *rules,
2813 struct stream *s)
Christopher Faulet3e964192018-10-24 11:39:23 +02002814{
2815 struct session *sess = strm_sess(s);
2816 struct http_txn *txn = s->txn;
Christopher Faulet3e964192018-10-24 11:39:23 +02002817 struct act_rule *rule;
Christopher Faulet3e964192018-10-24 11:39:23 +02002818 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002819 int act_opts = 0;
Christopher Faulet3e964192018-10-24 11:39:23 +02002820
Christopher Faulet3e964192018-10-24 11:39:23 +02002821 /* If "the current_rule_list" match the executed rule list, we are in
2822 * resume condition. If a resume is needed it is always in the action
2823 * and never in the ACL or converters. In this case, we initialise the
2824 * current rule, and go to the action execution point.
2825 */
2826 if (s->current_rule) {
2827 rule = s->current_rule;
2828 s->current_rule = NULL;
2829 if (s->current_rule_list == rules)
2830 goto resume_execution;
2831 }
2832 s->current_rule_list = rules;
2833
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002834 /* start the ruleset evaluation in strict mode */
2835 txn->rsp.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01002836
Christopher Faulet3e964192018-10-24 11:39:23 +02002837 list_for_each_entry(rule, rules, list) {
2838 /* check optional condition */
2839 if (rule->cond) {
2840 int ret;
2841
2842 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
2843 ret = acl_pass(ret);
2844
2845 if (rule->cond->pol == ACL_COND_UNLESS)
2846 ret = !ret;
2847
2848 if (!ret) /* condition not matched */
2849 continue;
2850 }
2851
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002852 act_opts |= ACT_OPT_FIRST;
Christopher Faulet3e964192018-10-24 11:39:23 +02002853resume_execution:
Amaury Denoyelle03517732021-05-07 14:25:01 +02002854 if (rule->kw->flags & KWF_EXPERIMENTAL)
2855 mark_tainted(TAINTED_ACTION_EXP_EXECUTED);
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002856
2857 /* Always call the action function if defined */
2858 if (rule->action_ptr) {
2859 if ((s->req.flags & CF_READ_ERROR) ||
2860 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
2861 (px->options & PR_O_ABRT_CLOSE)))
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002862 act_opts |= ACT_OPT_FINAL;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002863
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002864 switch (rule->action_ptr(rule, px, sess, s, act_opts)) {
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002865 case ACT_RET_CONT:
2866 break;
2867 case ACT_RET_STOP:
2868 rule_ret = HTTP_RULE_RES_STOP;
2869 goto end;
2870 case ACT_RET_YIELD:
2871 s->current_rule = rule;
2872 rule_ret = HTTP_RULE_RES_YIELD;
2873 goto end;
2874 case ACT_RET_ERR:
2875 rule_ret = HTTP_RULE_RES_ERROR;
2876 goto end;
2877 case ACT_RET_DONE:
2878 rule_ret = HTTP_RULE_RES_DONE;
2879 goto end;
2880 case ACT_RET_DENY:
Christopher Fauletb58f62b2020-01-13 16:40:13 +01002881 if (txn->status == -1)
2882 txn->status = 502;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002883 rule_ret = HTTP_RULE_RES_DENY;
2884 goto end;
2885 case ACT_RET_ABRT:
2886 rule_ret = HTTP_RULE_RES_ABRT;
2887 goto end;
2888 case ACT_RET_INV:
2889 rule_ret = HTTP_RULE_RES_BADREQ;
2890 goto end;
2891 }
2892 continue; /* eval the next rule */
2893 }
2894
2895 /* If not action function defined, check for known actions */
Christopher Faulet3e964192018-10-24 11:39:23 +02002896 switch (rule->action) {
2897 case ACT_ACTION_ALLOW:
2898 rule_ret = HTTP_RULE_RES_STOP; /* "allow" rules are OK */
2899 goto end;
2900
2901 case ACT_ACTION_DENY:
Christopher Faulet5cb513a2020-05-13 17:56:56 +02002902 txn->status = rule->arg.http_reply->status;
2903 txn->http_reply = rule->arg.http_reply;
Christopher Faulet3a26bee2019-12-16 12:47:40 +01002904 rule_ret = HTTP_RULE_RES_DENY;
Christopher Faulet3e964192018-10-24 11:39:23 +02002905 goto end;
2906
Willy Tarreaubc1223b2021-09-02 16:54:33 +02002907 case ACT_HTTP_REDIR: {
2908 int ret = http_apply_redirect_rule(rule->arg.redir, s, txn);
Christopher Faulet3e964192018-10-24 11:39:23 +02002909
Willy Tarreaubc1223b2021-09-02 16:54:33 +02002910 if (ret == 2) // 2 == skip
2911 break;
2912
2913 rule_ret = ret ? HTTP_RULE_RES_ABRT : HTTP_RULE_RES_ERROR;
2914 goto end;
2915 }
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002916 /* other flags exists, but normally, they never be matched. */
Christopher Faulet3e964192018-10-24 11:39:23 +02002917 default:
2918 break;
2919 }
2920 }
2921
2922 end:
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002923 /* if the ruleset evaluation is finished reset the strict mode */
Christopher Faulet46f95542019-12-20 10:07:22 +01002924 if (rule_ret != HTTP_RULE_RES_YIELD)
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002925 txn->rsp.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01002926
Christopher Faulet3e964192018-10-24 11:39:23 +02002927 /* we reached the end of the rules, nothing to report */
2928 return rule_ret;
2929}
2930
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002931/* Executes backend and frontend http-after-response rules for the stream <s>,
2932 * in that order. it return 1 on success and 0 on error. It is the caller
2933 * responsibility to catch error or ignore it. If it catches it, this function
2934 * may be called a second time, for the internal error.
2935 */
2936int http_eval_after_res_rules(struct stream *s)
2937{
2938 struct session *sess = s->sess;
2939 enum rule_result ret = HTTP_RULE_RES_CONT;
2940
Christopher Faulet507479b2020-05-15 12:29:46 +02002941 /* Eval after-response ruleset only if the reply is not const */
2942 if (s->txn->flags & TX_CONST_REPLY)
2943 goto end;
2944
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002945 /* prune the request variables if not already done and swap to the response variables. */
2946 if (s->vars_reqres.scope != SCOPE_RES) {
2947 if (!LIST_ISEMPTY(&s->vars_reqres.head))
2948 vars_prune(&s->vars_reqres, s->sess, s);
Willy Tarreaub7bfcb32021-08-31 08:13:25 +02002949 vars_init_head(&s->vars_reqres, SCOPE_RES);
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002950 }
2951
2952 ret = http_res_get_intercept_rule(s->be, &s->be->http_after_res_rules, s);
2953 if ((ret == HTTP_RULE_RES_CONT || ret == HTTP_RULE_RES_STOP) && sess->fe != s->be)
2954 ret = http_res_get_intercept_rule(sess->fe, &sess->fe->http_after_res_rules, s);
2955
Christopher Faulet507479b2020-05-15 12:29:46 +02002956 end:
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002957 /* All other codes than CONTINUE, STOP or DONE are forbidden */
2958 return (ret == HTTP_RULE_RES_CONT || ret == HTTP_RULE_RES_STOP || ret == HTTP_RULE_RES_DONE);
2959}
2960
Christopher Fauletfcda7c62018-10-24 11:56:22 +02002961/*
2962 * Manage client-side cookie. It can impact performance by about 2% so it is
2963 * desirable to call it only when needed. This code is quite complex because
2964 * of the multiple very crappy and ambiguous syntaxes we have to support. it
2965 * highly recommended not to touch this part without a good reason !
2966 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002967static void http_manage_client_side_cookies(struct stream *s, struct channel *req)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02002968{
2969 struct session *sess = s->sess;
2970 struct http_txn *txn = s->txn;
2971 struct htx *htx;
2972 struct http_hdr_ctx ctx;
2973 char *hdr_beg, *hdr_end, *del_from;
2974 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
2975 int preserve_hdr;
2976
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002977 htx = htxbuf(&req->buf);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02002978 ctx.blk = NULL;
2979 while (http_find_header(htx, ist("Cookie"), &ctx, 1)) {
Olivier Houchardf0f42382019-07-22 17:43:46 +02002980 int is_first = 1;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02002981 del_from = NULL; /* nothing to be deleted */
2982 preserve_hdr = 0; /* assume we may kill the whole header */
2983
2984 /* Now look for cookies. Conforming to RFC2109, we have to support
2985 * attributes whose name begin with a '$', and associate them with
2986 * the right cookie, if we want to delete this cookie.
2987 * So there are 3 cases for each cookie read :
2988 * 1) it's a special attribute, beginning with a '$' : ignore it.
2989 * 2) it's a server id cookie that we *MAY* want to delete : save
2990 * some pointers on it (last semi-colon, beginning of cookie...)
2991 * 3) it's an application cookie : we *MAY* have to delete a previous
2992 * "special" cookie.
2993 * At the end of loop, if a "special" cookie remains, we may have to
2994 * remove it. If no application cookie persists in the header, we
2995 * *MUST* delete it.
2996 *
2997 * Note: RFC2965 is unclear about the processing of spaces around
2998 * the equal sign in the ATTR=VALUE form. A careful inspection of
2999 * the RFC explicitly allows spaces before it, and not within the
3000 * tokens (attrs or values). An inspection of RFC2109 allows that
3001 * too but section 10.1.3 lets one think that spaces may be allowed
3002 * after the equal sign too, resulting in some (rare) buggy
3003 * implementations trying to do that. So let's do what servers do.
3004 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
3005 * allowed quoted strings in values, with any possible character
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003006 * after a backslash, including control chars and delimiters, which
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003007 * causes parsing to become ambiguous. Browsers also allow spaces
3008 * within values even without quotes.
3009 *
3010 * We have to keep multiple pointers in order to support cookie
3011 * removal at the beginning, middle or end of header without
3012 * corrupting the header. All of these headers are valid :
3013 *
3014 * hdr_beg hdr_end
3015 * | |
3016 * v |
3017 * NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3 |
3018 * NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3 v
3019 * NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3
3020 * | | | | | | |
3021 * | | | | | | |
3022 * | | | | | | +--> next
3023 * | | | | | +----> val_end
3024 * | | | | +-----------> val_beg
3025 * | | | +--------------> equal
3026 * | | +----------------> att_end
3027 * | +---------------------> att_beg
3028 * +--------------------------> prev
3029 *
3030 */
3031 hdr_beg = ctx.value.ptr;
3032 hdr_end = hdr_beg + ctx.value.len;
3033 for (prev = hdr_beg; prev < hdr_end; prev = next) {
3034 /* Iterate through all cookies on this line */
3035
3036 /* find att_beg */
3037 att_beg = prev;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003038 if (!is_first)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003039 att_beg++;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003040 is_first = 0;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003041
3042 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
3043 att_beg++;
3044
3045 /* find att_end : this is the first character after the last non
3046 * space before the equal. It may be equal to hdr_end.
3047 */
3048 equal = att_end = att_beg;
3049 while (equal < hdr_end) {
3050 if (*equal == '=' || *equal == ',' || *equal == ';')
3051 break;
3052 if (HTTP_IS_SPHT(*equal++))
3053 continue;
3054 att_end = equal;
3055 }
3056
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003057 /* here, <equal> points to '=', a delimiter or the end. <att_end>
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003058 * is between <att_beg> and <equal>, both may be identical.
3059 */
3060 /* look for end of cookie if there is an equal sign */
3061 if (equal < hdr_end && *equal == '=') {
3062 /* look for the beginning of the value */
3063 val_beg = equal + 1;
3064 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
3065 val_beg++;
3066
3067 /* find the end of the value, respecting quotes */
3068 next = http_find_cookie_value_end(val_beg, hdr_end);
3069
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003070 /* make val_end point to the first white space or delimiter after the value */
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003071 val_end = next;
3072 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
3073 val_end--;
3074 }
3075 else
3076 val_beg = val_end = next = equal;
3077
3078 /* We have nothing to do with attributes beginning with
3079 * '$'. However, they will automatically be removed if a
3080 * header before them is removed, since they're supposed
3081 * to be linked together.
3082 */
3083 if (*att_beg == '$')
3084 continue;
3085
3086 /* Ignore cookies with no equal sign */
3087 if (equal == next) {
3088 /* This is not our cookie, so we must preserve it. But if we already
3089 * scheduled another cookie for removal, we cannot remove the
3090 * complete header, but we can remove the previous block itself.
3091 */
3092 preserve_hdr = 1;
3093 if (del_from != NULL) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003094 int delta = http_del_hdr_value(hdr_beg, hdr_end, &del_from, prev);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003095 val_end += delta;
3096 next += delta;
3097 hdr_end += delta;
3098 prev = del_from;
3099 del_from = NULL;
3100 }
3101 continue;
3102 }
3103
3104 /* if there are spaces around the equal sign, we need to
3105 * strip them otherwise we'll get trouble for cookie captures,
3106 * or even for rewrites. Since this happens extremely rarely,
3107 * it does not hurt performance.
3108 */
3109 if (unlikely(att_end != equal || val_beg > equal + 1)) {
3110 int stripped_before = 0;
3111 int stripped_after = 0;
3112
3113 if (att_end != equal) {
3114 memmove(att_end, equal, hdr_end - equal);
3115 stripped_before = (att_end - equal);
3116 equal += stripped_before;
3117 val_beg += stripped_before;
3118 }
3119
3120 if (val_beg > equal + 1) {
3121 memmove(equal + 1, val_beg, hdr_end + stripped_before - val_beg);
3122 stripped_after = (equal + 1) - val_beg;
3123 val_beg += stripped_after;
3124 stripped_before += stripped_after;
3125 }
3126
3127 val_end += stripped_before;
3128 next += stripped_before;
3129 hdr_end += stripped_before;
3130 }
3131 /* now everything is as on the diagram above */
3132
3133 /* First, let's see if we want to capture this cookie. We check
3134 * that we don't already have a client side cookie, because we
3135 * can only capture one. Also as an optimisation, we ignore
3136 * cookies shorter than the declared name.
3137 */
3138 if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL &&
3139 (val_end - att_beg >= sess->fe->capture_namelen) &&
3140 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
3141 int log_len = val_end - att_beg;
3142
3143 if ((txn->cli_cookie = pool_alloc(pool_head_capture)) == NULL) {
3144 ha_alert("HTTP logging : out of memory.\n");
3145 } else {
3146 if (log_len > sess->fe->capture_len)
3147 log_len = sess->fe->capture_len;
3148 memcpy(txn->cli_cookie, att_beg, log_len);
3149 txn->cli_cookie[log_len] = 0;
3150 }
3151 }
3152
3153 /* Persistence cookies in passive, rewrite or insert mode have the
3154 * following form :
3155 *
3156 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
3157 *
3158 * For cookies in prefix mode, the form is :
3159 *
3160 * Cookie: NAME=SRV~VALUE
3161 */
3162 if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
3163 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
3164 struct server *srv = s->be->srv;
3165 char *delim;
3166
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003167 /* if we're in cookie prefix mode, we'll search the delimiter so that we
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003168 * have the server ID between val_beg and delim, and the original cookie between
3169 * delim+1 and val_end. Otherwise, delim==val_end :
3170 *
3171 * hdr_beg
3172 * |
3173 * v
3174 * NAME=SRV; # in all but prefix modes
3175 * NAME=SRV~OPAQUE ; # in prefix mode
3176 * || || | |+-> next
3177 * || || | +--> val_end
3178 * || || +---------> delim
3179 * || |+------------> val_beg
3180 * || +-------------> att_end = equal
3181 * |+-----------------> att_beg
3182 * +------------------> prev
3183 *
3184 */
3185 if (s->be->ck_opts & PR_CK_PFX) {
3186 for (delim = val_beg; delim < val_end; delim++)
3187 if (*delim == COOKIE_DELIM)
3188 break;
3189 }
3190 else {
3191 char *vbar1;
3192 delim = val_end;
3193 /* Now check if the cookie contains a date field, which would
3194 * appear after a vertical bar ('|') just after the server name
3195 * and before the delimiter.
3196 */
3197 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
3198 if (vbar1) {
3199 /* OK, so left of the bar is the server's cookie and
3200 * right is the last seen date. It is a base64 encoded
3201 * 30-bit value representing the UNIX date since the
3202 * epoch in 4-second quantities.
3203 */
3204 int val;
3205 delim = vbar1++;
3206 if (val_end - vbar1 >= 5) {
3207 val = b64tos30(vbar1);
3208 if (val > 0)
3209 txn->cookie_last_date = val << 2;
3210 }
3211 /* look for a second vertical bar */
3212 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
3213 if (vbar1 && (val_end - vbar1 > 5)) {
3214 val = b64tos30(vbar1 + 1);
3215 if (val > 0)
3216 txn->cookie_first_date = val << 2;
3217 }
3218 }
3219 }
3220
3221 /* if the cookie has an expiration date and the proxy wants to check
3222 * it, then we do that now. We first check if the cookie is too old,
3223 * then only if it has expired. We detect strict overflow because the
3224 * time resolution here is not great (4 seconds). Cookies with dates
3225 * in the future are ignored if their offset is beyond one day. This
3226 * allows an admin to fix timezone issues without expiring everyone
3227 * and at the same time avoids keeping unwanted side effects for too
3228 * long.
3229 */
3230 if (txn->cookie_first_date && s->be->cookie_maxlife &&
3231 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) ||
3232 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
3233 txn->flags &= ~TX_CK_MASK;
3234 txn->flags |= TX_CK_OLD;
3235 delim = val_beg; // let's pretend we have not found the cookie
3236 txn->cookie_first_date = 0;
3237 txn->cookie_last_date = 0;
3238 }
3239 else if (txn->cookie_last_date && s->be->cookie_maxidle &&
3240 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) ||
3241 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
3242 txn->flags &= ~TX_CK_MASK;
3243 txn->flags |= TX_CK_EXPIRED;
3244 delim = val_beg; // let's pretend we have not found the cookie
3245 txn->cookie_first_date = 0;
3246 txn->cookie_last_date = 0;
3247 }
3248
3249 /* Here, we'll look for the first running server which supports the cookie.
3250 * This allows to share a same cookie between several servers, for example
3251 * to dedicate backup servers to specific servers only.
3252 * However, to prevent clients from sticking to cookie-less backup server
3253 * when they have incidentely learned an empty cookie, we simply ignore
3254 * empty cookies and mark them as invalid.
3255 * The same behaviour is applied when persistence must be ignored.
3256 */
3257 if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
3258 srv = NULL;
3259
3260 while (srv) {
3261 if (srv->cookie && (srv->cklen == delim - val_beg) &&
3262 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
3263 if ((srv->cur_state != SRV_ST_STOPPED) ||
3264 (s->be->options & PR_O_PERSIST) ||
3265 (s->flags & SF_FORCE_PRST)) {
3266 /* we found the server and we can use it */
3267 txn->flags &= ~TX_CK_MASK;
3268 txn->flags |= (srv->cur_state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
3269 s->flags |= SF_DIRECT | SF_ASSIGNED;
3270 s->target = &srv->obj_type;
3271 break;
3272 } else {
3273 /* we found a server, but it's down,
3274 * mark it as such and go on in case
3275 * another one is available.
3276 */
3277 txn->flags &= ~TX_CK_MASK;
3278 txn->flags |= TX_CK_DOWN;
3279 }
3280 }
3281 srv = srv->next;
3282 }
3283
3284 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
3285 /* no server matched this cookie or we deliberately skipped it */
3286 txn->flags &= ~TX_CK_MASK;
3287 if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
3288 txn->flags |= TX_CK_UNUSED;
3289 else
3290 txn->flags |= TX_CK_INVALID;
3291 }
3292
3293 /* depending on the cookie mode, we may have to either :
3294 * - delete the complete cookie if we're in insert+indirect mode, so that
3295 * the server never sees it ;
3296 * - remove the server id from the cookie value, and tag the cookie as an
Joseph Herlante9d5c722018-11-25 11:00:25 -08003297 * application cookie so that it does not get accidentally removed later,
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003298 * if we're in cookie prefix mode
3299 */
3300 if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
3301 int delta; /* negative */
3302
3303 memmove(val_beg, delim + 1, hdr_end - (delim + 1));
3304 delta = val_beg - (delim + 1);
3305 val_end += delta;
3306 next += delta;
3307 hdr_end += delta;
3308 del_from = NULL;
3309 preserve_hdr = 1; /* we want to keep this cookie */
3310 }
3311 else if (del_from == NULL &&
3312 (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
3313 del_from = prev;
3314 }
3315 }
3316 else {
3317 /* This is not our cookie, so we must preserve it. But if we already
3318 * scheduled another cookie for removal, we cannot remove the
3319 * complete header, but we can remove the previous block itself.
3320 */
3321 preserve_hdr = 1;
3322
3323 if (del_from != NULL) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003324 int delta = http_del_hdr_value(hdr_beg, hdr_end, &del_from, prev);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003325 if (att_beg >= del_from)
3326 att_beg += delta;
3327 if (att_end >= del_from)
3328 att_end += delta;
3329 val_beg += delta;
3330 val_end += delta;
3331 next += delta;
3332 hdr_end += delta;
3333 prev = del_from;
3334 del_from = NULL;
3335 }
3336 }
3337
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003338 } /* for each cookie */
3339
3340
3341 /* There are no more cookies on this line.
3342 * We may still have one (or several) marked for deletion at the
3343 * end of the line. We must do this now in two ways :
3344 * - if some cookies must be preserved, we only delete from the
3345 * mark to the end of line ;
3346 * - if nothing needs to be preserved, simply delete the whole header
3347 */
3348 if (del_from) {
3349 hdr_end = (preserve_hdr ? del_from : hdr_beg);
3350 }
3351 if ((hdr_end - hdr_beg) != ctx.value.len) {
Christopher Faulet3e2638e2019-06-18 09:49:16 +02003352 if (hdr_beg != hdr_end)
3353 htx_change_blk_value_len(htx, ctx.blk, hdr_end - hdr_beg);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003354 else
3355 http_remove_header(htx, &ctx);
3356 }
3357 } /* for each "Cookie header */
3358}
3359
3360/*
3361 * Manage server-side cookies. It can impact performance by about 2% so it is
3362 * desirable to call it only when needed. This function is also used when we
3363 * just need to know if there is a cookie (eg: for check-cache).
3364 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003365static void http_manage_server_side_cookies(struct stream *s, struct channel *res)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003366{
3367 struct session *sess = s->sess;
3368 struct http_txn *txn = s->txn;
3369 struct htx *htx;
3370 struct http_hdr_ctx ctx;
3371 struct server *srv;
3372 char *hdr_beg, *hdr_end;
3373 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau6f7a02a2019-04-15 21:49:49 +02003374 int is_cookie2 = 0;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003375
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003376 htx = htxbuf(&res->buf);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003377
3378 ctx.blk = NULL;
3379 while (1) {
Olivier Houchardf0f42382019-07-22 17:43:46 +02003380 int is_first = 1;
3381
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003382 if (!http_find_header(htx, ist("Set-Cookie"), &ctx, 1)) {
3383 if (!http_find_header(htx, ist("Set-Cookie2"), &ctx, 1))
3384 break;
3385 is_cookie2 = 1;
3386 }
3387
3388 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
3389 * <prev> points to the colon.
3390 */
3391 txn->flags |= TX_SCK_PRESENT;
3392
3393 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
3394 * check-cache is enabled) and we are not interested in checking
3395 * them. Warning, the cookie capture is declared in the frontend.
3396 */
3397 if (s->be->cookie_name == NULL && sess->fe->capture_name == NULL)
3398 break;
3399
3400 /* OK so now we know we have to process this response cookie.
3401 * The format of the Set-Cookie header is slightly different
3402 * from the format of the Cookie header in that it does not
3403 * support the comma as a cookie delimiter (thus the header
3404 * cannot be folded) because the Expires attribute described in
3405 * the original Netscape's spec may contain an unquoted date
3406 * with a comma inside. We have to live with this because
3407 * many browsers don't support Max-Age and some browsers don't
3408 * support quoted strings. However the Set-Cookie2 header is
3409 * clean.
3410 *
3411 * We have to keep multiple pointers in order to support cookie
3412 * removal at the beginning, middle or end of header without
3413 * corrupting the header (in case of set-cookie2). A special
3414 * pointer, <scav> points to the beginning of the set-cookie-av
3415 * fields after the first semi-colon. The <next> pointer points
3416 * either to the end of line (set-cookie) or next unquoted comma
3417 * (set-cookie2). All of these headers are valid :
3418 *
3419 * hdr_beg hdr_end
3420 * | |
3421 * v |
3422 * NAME1 = VALUE 1 ; Secure; Path="/" |
3423 * NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT v
3424 * NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT
3425 * NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard
3426 * | | | | | | | |
3427 * | | | | | | | +-> next
3428 * | | | | | | +------------> scav
3429 * | | | | | +--------------> val_end
3430 * | | | | +--------------------> val_beg
3431 * | | | +----------------------> equal
3432 * | | +------------------------> att_end
3433 * | +----------------------------> att_beg
3434 * +------------------------------> prev
3435 * -------------------------------> hdr_beg
3436 */
3437 hdr_beg = ctx.value.ptr;
3438 hdr_end = hdr_beg + ctx.value.len;
3439 for (prev = hdr_beg; prev < hdr_end; prev = next) {
3440
3441 /* Iterate through all cookies on this line */
3442
3443 /* find att_beg */
3444 att_beg = prev;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003445 if (!is_first)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003446 att_beg++;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003447 is_first = 0;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003448
3449 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
3450 att_beg++;
3451
3452 /* find att_end : this is the first character after the last non
3453 * space before the equal. It may be equal to hdr_end.
3454 */
3455 equal = att_end = att_beg;
3456
3457 while (equal < hdr_end) {
3458 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
3459 break;
3460 if (HTTP_IS_SPHT(*equal++))
3461 continue;
3462 att_end = equal;
3463 }
3464
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003465 /* here, <equal> points to '=', a delimiter or the end. <att_end>
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003466 * is between <att_beg> and <equal>, both may be identical.
3467 */
3468
3469 /* look for end of cookie if there is an equal sign */
3470 if (equal < hdr_end && *equal == '=') {
3471 /* look for the beginning of the value */
3472 val_beg = equal + 1;
3473 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
3474 val_beg++;
3475
3476 /* find the end of the value, respecting quotes */
3477 next = http_find_cookie_value_end(val_beg, hdr_end);
3478
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003479 /* make val_end point to the first white space or delimiter after the value */
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003480 val_end = next;
3481 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
3482 val_end--;
3483 }
3484 else {
3485 /* <equal> points to next comma, semi-colon or EOL */
3486 val_beg = val_end = next = equal;
3487 }
3488
3489 if (next < hdr_end) {
3490 /* Set-Cookie2 supports multiple cookies, and <next> points to
3491 * a colon or semi-colon before the end. So skip all attr-value
3492 * pairs and look for the next comma. For Set-Cookie, since
3493 * commas are permitted in values, skip to the end.
3494 */
3495 if (is_cookie2)
3496 next = http_find_hdr_value_end(next, hdr_end);
3497 else
3498 next = hdr_end;
3499 }
3500
3501 /* Now everything is as on the diagram above */
3502
3503 /* Ignore cookies with no equal sign */
3504 if (equal == val_end)
3505 continue;
3506
3507 /* If there are spaces around the equal sign, we need to
3508 * strip them otherwise we'll get trouble for cookie captures,
3509 * or even for rewrites. Since this happens extremely rarely,
3510 * it does not hurt performance.
3511 */
3512 if (unlikely(att_end != equal || val_beg > equal + 1)) {
3513 int stripped_before = 0;
3514 int stripped_after = 0;
3515
3516 if (att_end != equal) {
3517 memmove(att_end, equal, hdr_end - equal);
3518 stripped_before = (att_end - equal);
3519 equal += stripped_before;
3520 val_beg += stripped_before;
3521 }
3522
3523 if (val_beg > equal + 1) {
3524 memmove(equal + 1, val_beg, hdr_end + stripped_before - val_beg);
3525 stripped_after = (equal + 1) - val_beg;
3526 val_beg += stripped_after;
3527 stripped_before += stripped_after;
3528 }
3529
3530 val_end += stripped_before;
3531 next += stripped_before;
3532 hdr_end += stripped_before;
3533
Christopher Faulet3e2638e2019-06-18 09:49:16 +02003534 htx_change_blk_value_len(htx, ctx.blk, hdr_end - hdr_beg);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003535 ctx.value.len = hdr_end - hdr_beg;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003536 }
3537
3538 /* First, let's see if we want to capture this cookie. We check
3539 * that we don't already have a server side cookie, because we
3540 * can only capture one. Also as an optimisation, we ignore
3541 * cookies shorter than the declared name.
3542 */
3543 if (sess->fe->capture_name != NULL &&
3544 txn->srv_cookie == NULL &&
3545 (val_end - att_beg >= sess->fe->capture_namelen) &&
3546 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
3547 int log_len = val_end - att_beg;
3548 if ((txn->srv_cookie = pool_alloc(pool_head_capture)) == NULL) {
3549 ha_alert("HTTP logging : out of memory.\n");
3550 }
3551 else {
3552 if (log_len > sess->fe->capture_len)
3553 log_len = sess->fe->capture_len;
3554 memcpy(txn->srv_cookie, att_beg, log_len);
3555 txn->srv_cookie[log_len] = 0;
3556 }
3557 }
3558
3559 srv = objt_server(s->target);
3560 /* now check if we need to process it for persistence */
3561 if (!(s->flags & SF_IGNORE_PRST) &&
3562 (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
3563 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
3564 /* assume passive cookie by default */
3565 txn->flags &= ~TX_SCK_MASK;
3566 txn->flags |= TX_SCK_FOUND;
3567
3568 /* If the cookie is in insert mode on a known server, we'll delete
3569 * this occurrence because we'll insert another one later.
3570 * We'll delete it too if the "indirect" option is set and we're in
3571 * a direct access.
3572 */
3573 if (s->be->ck_opts & PR_CK_PSV) {
3574 /* The "preserve" flag was set, we don't want to touch the
3575 * server's cookie.
3576 */
3577 }
3578 else if ((srv && (s->be->ck_opts & PR_CK_INS)) ||
3579 ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) {
3580 /* this cookie must be deleted */
3581 if (prev == hdr_beg && next == hdr_end) {
3582 /* whole header */
3583 http_remove_header(htx, &ctx);
3584 /* note: while both invalid now, <next> and <hdr_end>
3585 * are still equal, so the for() will stop as expected.
3586 */
3587 } else {
3588 /* just remove the value */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003589 int delta = http_del_hdr_value(hdr_beg, hdr_end, &prev, next);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003590 next = prev;
3591 hdr_end += delta;
3592 }
3593 txn->flags &= ~TX_SCK_MASK;
3594 txn->flags |= TX_SCK_DELETED;
3595 /* and go on with next cookie */
3596 }
3597 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) {
3598 /* replace bytes val_beg->val_end with the cookie name associated
3599 * with this server since we know it.
3600 */
3601 int sliding, delta;
3602
3603 ctx.value = ist2(val_beg, val_end - val_beg);
3604 ctx.lws_before = ctx.lws_after = 0;
3605 http_replace_header_value(htx, &ctx, ist2(srv->cookie, srv->cklen));
3606 delta = srv->cklen - (val_end - val_beg);
3607 sliding = (ctx.value.ptr - val_beg);
3608 hdr_beg += sliding;
3609 val_beg += sliding;
3610 next += sliding + delta;
3611 hdr_end += sliding + delta;
3612
3613 txn->flags &= ~TX_SCK_MASK;
3614 txn->flags |= TX_SCK_REPLACED;
3615 }
3616 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) {
3617 /* insert the cookie name associated with this server
3618 * before existing cookie, and insert a delimiter between them..
3619 */
3620 int sliding, delta;
3621 ctx.value = ist2(val_beg, 0);
3622 ctx.lws_before = ctx.lws_after = 0;
3623 http_replace_header_value(htx, &ctx, ist2(srv->cookie, srv->cklen + 1));
3624 delta = srv->cklen + 1;
3625 sliding = (ctx.value.ptr - val_beg);
3626 hdr_beg += sliding;
3627 val_beg += sliding;
3628 next += sliding + delta;
3629 hdr_end += sliding + delta;
3630
3631 val_beg[srv->cklen] = COOKIE_DELIM;
3632 txn->flags &= ~TX_SCK_MASK;
3633 txn->flags |= TX_SCK_REPLACED;
3634 }
3635 }
3636 /* that's done for this cookie, check the next one on the same
3637 * line when next != hdr_end (only if is_cookie2).
3638 */
3639 }
3640 }
3641}
3642
Christopher Faulet25a02f62018-10-24 12:00:25 +02003643/*
3644 * Parses the Cache-Control and Pragma request header fields to determine if
3645 * the request may be served from the cache and/or if it is cacheable. Updates
3646 * s->txn->flags.
3647 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003648void http_check_request_for_cacheability(struct stream *s, struct channel *req)
Christopher Faulet25a02f62018-10-24 12:00:25 +02003649{
3650 struct http_txn *txn = s->txn;
3651 struct htx *htx;
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003652 struct http_hdr_ctx ctx = { .blk = NULL };
3653 int pragma_found, cc_found;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003654
3655 if ((txn->flags & (TX_CACHEABLE|TX_CACHE_IGNORE)) == TX_CACHE_IGNORE)
3656 return; /* nothing more to do here */
3657
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003658 htx = htxbuf(&req->buf);
Christopher Faulet25a02f62018-10-24 12:00:25 +02003659 pragma_found = cc_found = 0;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003660
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003661 /* Check "pragma" header for HTTP/1.0 compatibility. */
3662 if (http_find_header(htx, ist("pragma"), &ctx, 1)) {
3663 if (isteqi(ctx.value, ist("no-cache"))) {
3664 pragma_found = 1;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003665 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003666 }
Christopher Faulet25a02f62018-10-24 12:00:25 +02003667
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003668 ctx.blk = NULL;
3669 /* Don't use the cache and don't try to store if we found the
3670 * Authorization header */
3671 if (http_find_header(htx, ist("authorization"), &ctx, 1)) {
3672 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3673 txn->flags |= TX_CACHE_IGNORE;
3674 }
Christopher Faulet25a02f62018-10-24 12:00:25 +02003675
Christopher Faulet25a02f62018-10-24 12:00:25 +02003676
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003677 /* Look for "cache-control" header and iterate over all the values
3678 * until we find one that specifies that caching is possible or not. */
3679 ctx.blk = NULL;
3680 while (http_find_header(htx, ist("cache-control"), &ctx, 0)) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003681 cc_found = 1;
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003682 /* We don't check the values after max-age, max-stale nor min-fresh,
3683 * we simply don't use the cache when they're specified. */
3684 if (istmatchi(ctx.value, ist("max-age")) ||
3685 istmatchi(ctx.value, ist("no-cache")) ||
3686 istmatchi(ctx.value, ist("max-stale")) ||
3687 istmatchi(ctx.value, ist("min-fresh"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003688 txn->flags |= TX_CACHE_IGNORE;
3689 continue;
3690 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003691 if (istmatchi(ctx.value, ist("no-store"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003692 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3693 continue;
3694 }
3695 }
3696
3697 /* RFC7234#5.4:
3698 * When the Cache-Control header field is also present and
3699 * understood in a request, Pragma is ignored.
3700 * When the Cache-Control header field is not present in a
3701 * request, caches MUST consider the no-cache request
3702 * pragma-directive as having the same effect as if
3703 * "Cache-Control: no-cache" were present.
3704 */
3705 if (!cc_found && pragma_found)
3706 txn->flags |= TX_CACHE_IGNORE;
3707}
3708
3709/*
3710 * Check if response is cacheable or not. Updates s->txn->flags.
3711 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003712void http_check_response_for_cacheability(struct stream *s, struct channel *res)
Christopher Faulet25a02f62018-10-24 12:00:25 +02003713{
3714 struct http_txn *txn = s->txn;
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003715 struct http_hdr_ctx ctx = { .blk = NULL };
Christopher Faulet25a02f62018-10-24 12:00:25 +02003716 struct htx *htx;
Remi Tricot-Le Bretoncc9bf2e2020-11-12 11:14:41 +01003717 int has_freshness_info = 0;
3718 int has_validator = 0;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003719
3720 if (txn->status < 200) {
3721 /* do not try to cache interim responses! */
3722 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3723 return;
3724 }
3725
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003726 htx = htxbuf(&res->buf);
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003727 /* Check "pragma" header for HTTP/1.0 compatibility. */
3728 if (http_find_header(htx, ist("pragma"), &ctx, 1)) {
3729 if (isteqi(ctx.value, ist("no-cache"))) {
3730 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3731 return;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003732 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003733 }
Christopher Faulet25a02f62018-10-24 12:00:25 +02003734
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003735 /* Look for "cache-control" header and iterate over all the values
3736 * until we find one that specifies that caching is possible or not. */
3737 ctx.blk = NULL;
3738 while (http_find_header(htx, ist("cache-control"), &ctx, 0)) {
3739 if (isteqi(ctx.value, ist("public"))) {
3740 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003741 continue;
3742 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003743 if (isteqi(ctx.value, ist("private")) ||
3744 isteqi(ctx.value, ist("no-cache")) ||
3745 isteqi(ctx.value, ist("no-store")) ||
3746 isteqi(ctx.value, ist("max-age=0")) ||
3747 isteqi(ctx.value, ist("s-maxage=0"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003748 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003749 continue;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003750 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003751 /* We might have a no-cache="set-cookie" form. */
3752 if (istmatchi(ctx.value, ist("no-cache=\"set-cookie"))) {
3753 txn->flags &= ~TX_CACHE_COOK;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003754 continue;
3755 }
Remi Tricot-Le Bretoncc9bf2e2020-11-12 11:14:41 +01003756
3757 if (istmatchi(ctx.value, ist("s-maxage")) ||
3758 istmatchi(ctx.value, ist("max-age"))) {
3759 has_freshness_info = 1;
3760 continue;
3761 }
3762 }
3763
3764 /* If no freshness information could be found in Cache-Control values,
3765 * look for an Expires header. */
3766 if (!has_freshness_info) {
3767 ctx.blk = NULL;
3768 has_freshness_info = http_find_header(htx, ist("expires"), &ctx, 0);
Christopher Faulet25a02f62018-10-24 12:00:25 +02003769 }
Remi Tricot-Le Bretoncc9bf2e2020-11-12 11:14:41 +01003770
3771 /* If no freshness information could be found in Cache-Control or Expires
3772 * values, look for an explicit validator. */
3773 if (!has_freshness_info) {
3774 ctx.blk = NULL;
3775 has_validator = 1;
3776 if (!http_find_header(htx, ist("etag"), &ctx, 0)) {
3777 ctx.blk = NULL;
3778 if (!http_find_header(htx, ist("last-modified"), &ctx, 0))
3779 has_validator = 0;
3780 }
3781 }
3782
3783 /* We won't store an entry that has neither a cache validator nor an
3784 * explicit expiration time, as suggested in RFC 7234#3. */
3785 if (!has_freshness_info && !has_validator)
3786 txn->flags |= TX_CACHE_IGNORE;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003787}
3788
Christopher Faulet377c5a52018-10-24 21:21:30 +02003789/*
3790 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
3791 * for the current backend.
3792 *
3793 * It is assumed that the request is either a HEAD, GET, or POST and that the
3794 * uri_auth field is valid.
3795 *
3796 * Returns 1 if stats should be provided, otherwise 0.
3797 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003798static int http_stats_check_uri(struct stream *s, struct http_txn *txn, struct proxy *backend)
Christopher Faulet377c5a52018-10-24 21:21:30 +02003799{
3800 struct uri_auth *uri_auth = backend->uri_auth;
3801 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003802 struct htx_sl *sl;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003803 struct ist uri;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003804
3805 if (!uri_auth)
3806 return 0;
3807
3808 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
3809 return 0;
3810
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003811 htx = htxbuf(&s->req.buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02003812 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003813 uri = htx_sl_req_uri(sl);
Amaury Denoyellec453f952021-07-06 11:40:12 +02003814 if (*uri_auth->uri_prefix == '/') {
3815 struct http_uri_parser parser = http_uri_parser_init(uri);
3816 uri = http_parse_path(&parser);
3817 }
Christopher Faulet377c5a52018-10-24 21:21:30 +02003818
3819 /* check URI size */
3820 if (uri_auth->uri_len > uri.len)
3821 return 0;
3822
3823 if (memcmp(uri.ptr, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
3824 return 0;
3825
3826 return 1;
3827}
3828
3829/* This function prepares an applet to handle the stats. It can deal with the
3830 * "100-continue" expectation, check that admin rules are met for POST requests,
3831 * and program a response message if something was unexpected. It cannot fail
3832 * and always relies on the stats applet to complete the job. It does not touch
3833 * analysers nor counters, which are left to the caller. It does not touch
3834 * s->target which is supposed to already point to the stats applet. The caller
3835 * is expected to have already assigned an appctx to the stream.
3836 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003837static int http_handle_stats(struct stream *s, struct channel *req)
Christopher Faulet377c5a52018-10-24 21:21:30 +02003838{
3839 struct stats_admin_rule *stats_admin_rule;
3840 struct stream_interface *si = &s->si[1];
3841 struct session *sess = s->sess;
3842 struct http_txn *txn = s->txn;
3843 struct http_msg *msg = &txn->req;
3844 struct uri_auth *uri_auth = s->be->uri_auth;
3845 const char *h, *lookup, *end;
3846 struct appctx *appctx;
3847 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003848 struct htx_sl *sl;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003849
3850 appctx = si_appctx(si);
3851 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
3852 appctx->st1 = appctx->st2 = 0;
3853 appctx->ctx.stats.st_code = STAT_STATUS_INIT;
Willy Tarreau676c29e2019-10-09 10:50:01 +02003854 appctx->ctx.stats.flags |= uri_auth->flags;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003855 appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */
3856 if ((msg->flags & HTTP_MSGF_VER_11) && (txn->meth != HTTP_METH_HEAD))
3857 appctx->ctx.stats.flags |= STAT_CHUNKED;
3858
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003859 htx = htxbuf(&req->buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02003860 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003861 lookup = HTX_SL_REQ_UPTR(sl) + uri_auth->uri_len;
3862 end = HTX_SL_REQ_UPTR(sl) + HTX_SL_REQ_ULEN(sl);
Christopher Faulet377c5a52018-10-24 21:21:30 +02003863
3864 for (h = lookup; h <= end - 3; h++) {
3865 if (memcmp(h, ";up", 3) == 0) {
3866 appctx->ctx.stats.flags |= STAT_HIDE_DOWN;
3867 break;
3868 }
Amaury Denoyelle91e55ea2021-02-25 14:46:08 +01003869 }
3870
3871 for (h = lookup; h <= end - 9; h++) {
3872 if (memcmp(h, ";no-maint", 9) == 0) {
Willy Tarreau3e320362020-10-23 17:28:57 +02003873 appctx->ctx.stats.flags |= STAT_HIDE_MAINT;
3874 break;
3875 }
Christopher Faulet377c5a52018-10-24 21:21:30 +02003876 }
3877
3878 if (uri_auth->refresh) {
3879 for (h = lookup; h <= end - 10; h++) {
3880 if (memcmp(h, ";norefresh", 10) == 0) {
3881 appctx->ctx.stats.flags |= STAT_NO_REFRESH;
3882 break;
3883 }
3884 }
3885 }
3886
3887 for (h = lookup; h <= end - 4; h++) {
3888 if (memcmp(h, ";csv", 4) == 0) {
Christopher Faulet6338a082019-09-09 15:50:54 +02003889 appctx->ctx.stats.flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
Christopher Faulet377c5a52018-10-24 21:21:30 +02003890 break;
3891 }
3892 }
3893
3894 for (h = lookup; h <= end - 6; h++) {
3895 if (memcmp(h, ";typed", 6) == 0) {
Christopher Faulet6338a082019-09-09 15:50:54 +02003896 appctx->ctx.stats.flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
Christopher Faulet377c5a52018-10-24 21:21:30 +02003897 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
3898 break;
3899 }
3900 }
3901
Christopher Faulet6338a082019-09-09 15:50:54 +02003902 for (h = lookup; h <= end - 5; h++) {
3903 if (memcmp(h, ";json", 5) == 0) {
3904 appctx->ctx.stats.flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
3905 appctx->ctx.stats.flags |= STAT_FMT_JSON;
3906 break;
3907 }
3908 }
3909
3910 for (h = lookup; h <= end - 12; h++) {
3911 if (memcmp(h, ";json-schema", 12) == 0) {
3912 appctx->ctx.stats.flags &= ~STAT_FMT_MASK;
3913 appctx->ctx.stats.flags |= STAT_JSON_SCHM;
3914 break;
3915 }
3916 }
3917
Christopher Faulet377c5a52018-10-24 21:21:30 +02003918 for (h = lookup; h <= end - 8; h++) {
3919 if (memcmp(h, ";st=", 4) == 0) {
3920 int i;
3921 h += 4;
3922 appctx->ctx.stats.st_code = STAT_STATUS_UNKN;
3923 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
3924 if (strncmp(stat_status_codes[i], h, 4) == 0) {
3925 appctx->ctx.stats.st_code = i;
3926 break;
3927 }
3928 }
3929 break;
3930 }
3931 }
3932
3933 appctx->ctx.stats.scope_str = 0;
3934 appctx->ctx.stats.scope_len = 0;
3935 for (h = lookup; h <= end - 8; h++) {
3936 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
3937 int itx = 0;
3938 const char *h2;
3939 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
3940 const char *err;
3941
3942 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
3943 h2 = h;
Christopher Fauleted7a0662019-01-14 11:07:34 +01003944 appctx->ctx.stats.scope_str = h2 - HTX_SL_REQ_UPTR(sl);
3945 while (h < end) {
Christopher Faulet377c5a52018-10-24 21:21:30 +02003946 if (*h == ';' || *h == '&' || *h == ' ')
3947 break;
3948 itx++;
3949 h++;
3950 }
3951
3952 if (itx > STAT_SCOPE_TXT_MAXLEN)
3953 itx = STAT_SCOPE_TXT_MAXLEN;
3954 appctx->ctx.stats.scope_len = itx;
3955
3956 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
3957 memcpy(scope_txt, h2, itx);
3958 scope_txt[itx] = '\0';
3959 err = invalid_char(scope_txt);
3960 if (err) {
3961 /* bad char in search text => clear scope */
3962 appctx->ctx.stats.scope_str = 0;
3963 appctx->ctx.stats.scope_len = 0;
3964 }
3965 break;
3966 }
3967 }
3968
3969 /* now check whether we have some admin rules for this request */
3970 list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) {
3971 int ret = 1;
3972
3973 if (stats_admin_rule->cond) {
3974 ret = acl_exec_cond(stats_admin_rule->cond, s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
3975 ret = acl_pass(ret);
3976 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
3977 ret = !ret;
3978 }
3979
3980 if (ret) {
3981 /* no rule, or the rule matches */
3982 appctx->ctx.stats.flags |= STAT_ADMIN;
3983 break;
3984 }
3985 }
3986
Christopher Faulet5d45e382019-02-27 15:15:23 +01003987 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
3988 appctx->st0 = STAT_HTTP_HEAD;
3989 else if (txn->meth == HTTP_METH_POST) {
Christopher Fauletbd9e8422019-08-15 22:26:48 +02003990 if (appctx->ctx.stats.flags & STAT_ADMIN) {
Christopher Faulet377c5a52018-10-24 21:21:30 +02003991 appctx->st0 = STAT_HTTP_POST;
Christopher Fauletbd9e8422019-08-15 22:26:48 +02003992 if (msg->msg_state < HTTP_MSG_DATA)
3993 req->analysers |= AN_REQ_HTTP_BODY;
3994 }
Christopher Faulet377c5a52018-10-24 21:21:30 +02003995 else {
Christopher Faulet5d45e382019-02-27 15:15:23 +01003996 /* POST without admin level */
Christopher Faulet377c5a52018-10-24 21:21:30 +02003997 appctx->ctx.stats.flags &= ~STAT_CHUNKED;
3998 appctx->ctx.stats.st_code = STAT_STATUS_DENY;
3999 appctx->st0 = STAT_HTTP_LAST;
4000 }
4001 }
4002 else {
Christopher Faulet5d45e382019-02-27 15:15:23 +01004003 /* Unsupported method */
4004 appctx->ctx.stats.flags &= ~STAT_CHUNKED;
4005 appctx->ctx.stats.st_code = STAT_STATUS_IVAL;
4006 appctx->st0 = STAT_HTTP_LAST;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004007 }
4008
4009 s->task->nice = -32; /* small boost for HTTP statistics */
4010 return 1;
4011}
4012
Christopher Faulet021a8e42021-03-29 10:46:38 +02004013/* This function waits for the message payload at most <time> milliseconds (may
4014 * be set to TICK_ETERNITY). It stops to wait if at least <bytes> bytes of the
4015 * payload are received (0 means no limit). It returns HTTP_RULE_* depending on
4016 * the result:
4017 *
4018 * - HTTP_RULE_RES_CONT when conditions are met to stop waiting
4019 * - HTTP_RULE_RES_YIELD to wait for more data
Ilya Shipitsinb2be9a12021-04-24 13:25:42 +05004020 * - HTTP_RULE_RES_ABRT when a timeout occurred.
Christopher Faulet021a8e42021-03-29 10:46:38 +02004021 * - HTTP_RULE_RES_BADREQ if a parsing error is raised by lower level
Ilya Shipitsinb2be9a12021-04-24 13:25:42 +05004022 * - HTTP_RULE_RES_ERROR if an internal error occurred
Christopher Faulet021a8e42021-03-29 10:46:38 +02004023 *
Ilya Shipitsinb2be9a12021-04-24 13:25:42 +05004024 * If a timeout occurred, this function is responsible to emit the right response
Christopher Faulet021a8e42021-03-29 10:46:38 +02004025 * to the client, depending on the channel (408 on request side, 504 on response
4026 * side). All other errors must be handled by the caller.
4027 */
4028enum rule_result http_wait_for_msg_body(struct stream *s, struct channel *chn,
4029 unsigned int time, unsigned int bytes)
4030{
4031 struct session *sess = s->sess;
4032 struct http_txn *txn = s->txn;
4033 struct http_msg *msg = ((chn->flags & CF_ISRESP) ? &txn->rsp : &txn->req);
4034 struct htx *htx;
4035 enum rule_result ret = HTTP_RULE_RES_CONT;
4036
4037 htx = htxbuf(&chn->buf);
4038
4039 if (htx->flags & HTX_FL_PARSING_ERROR) {
4040 ret = HTTP_RULE_RES_BADREQ;
4041 goto end;
4042 }
4043 if (htx->flags & HTX_FL_PROCESSING_ERROR) {
4044 ret = HTTP_RULE_RES_ERROR;
4045 goto end;
4046 }
4047
4048 /* Do nothing for bodyless and CONNECT requests */
4049 if (txn->meth == HTTP_METH_CONNECT || (msg->flags & HTTP_MSGF_BODYLESS))
4050 goto end;
4051
4052 if (!(chn->flags & CF_ISRESP) && msg->msg_state < HTTP_MSG_DATA) {
4053 if (http_handle_expect_hdr(s, htx, msg) == -1) {
4054 ret = HTTP_RULE_RES_ERROR;
4055 goto end;
4056 }
4057 }
4058
4059 msg->msg_state = HTTP_MSG_DATA;
4060
4061 /* Now we're in HTTP_MSG_DATA. We just need to know if all data have
4062 * been received or if the buffer is full.
4063 */
Christopher Faulet78335962021-09-23 14:46:32 +02004064 if ((htx->flags & HTX_FL_EOM) ||
4065 htx_get_tail_type(htx) > HTX_BLK_DATA ||
4066 channel_htx_full(chn, htx, global.tune.maxrewrite) ||
4067 si_rx_blocked_room(chn_prod(chn)))
Christopher Faulet021a8e42021-03-29 10:46:38 +02004068 goto end;
4069
4070 if (bytes) {
4071 struct htx_blk *blk;
4072 unsigned int len = 0;
4073
4074 for (blk = htx_get_first_blk(htx); blk; blk = htx_get_next_blk(htx, blk)) {
4075 if (htx_get_blk_type(blk) != HTX_BLK_DATA)
4076 continue;
4077 len += htx_get_blksz(blk);
4078 if (len >= bytes)
4079 goto end;
4080 }
4081 }
4082
4083 if ((chn->flags & CF_READ_TIMEOUT) || tick_is_expired(chn->analyse_exp, now_ms)) {
4084 if (!(chn->flags & CF_ISRESP))
4085 goto abort_req;
4086 goto abort_res;
4087 }
4088
4089 /* we get here if we need to wait for more data */
4090 if (!(chn->flags & (CF_SHUTR | CF_READ_ERROR))) {
4091 if (!tick_isset(chn->analyse_exp))
4092 chn->analyse_exp = tick_add_ifset(now_ms, time);
4093 ret = HTTP_RULE_RES_YIELD;
4094 }
4095
4096 end:
4097 return ret;
4098
4099 abort_req:
4100 txn->status = 408;
4101 if (!(s->flags & SF_ERR_MASK))
4102 s->flags |= SF_ERR_CLITO;
4103 if (!(s->flags & SF_FINST_MASK))
4104 s->flags |= SF_FINST_D;
Willy Tarreau4781b152021-04-06 13:53:36 +02004105 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req);
Christopher Faulet021a8e42021-03-29 10:46:38 +02004106 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02004107 _HA_ATOMIC_INC(&sess->listener->counters->failed_req);
Christopher Faulet021a8e42021-03-29 10:46:38 +02004108 http_reply_and_close(s, txn->status, http_error_message(s));
4109 ret = HTTP_RULE_RES_ABRT;
4110 goto end;
4111
4112 abort_res:
4113 txn->status = 504;
4114 if (!(s->flags & SF_ERR_MASK))
4115 s->flags |= SF_ERR_SRVTO;
4116 if (!(s->flags & SF_FINST_MASK))
4117 s->flags |= SF_FINST_D;
4118 stream_inc_http_fail_ctr(s);
4119 http_reply_and_close(s, txn->status, http_error_message(s));
4120 ret = HTTP_RULE_RES_ABRT;
4121 goto end;
4122}
4123
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004124void http_perform_server_redirect(struct stream *s, struct stream_interface *si)
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004125{
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004126 struct channel *req = &s->req;
4127 struct channel *res = &s->res;
4128 struct server *srv;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004129 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004130 struct htx_sl *sl;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004131 struct ist path, location;
4132 unsigned int flags;
Amaury Denoyellec453f952021-07-06 11:40:12 +02004133 struct http_uri_parser parser;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004134
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004135 /*
4136 * Create the location
4137 */
4138 chunk_reset(&trash);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004139
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004140 /* 1: add the server's prefix */
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004141 /* special prefix "/" means don't change URL */
4142 srv = __objt_server(s->target);
4143 if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') {
4144 if (!chunk_memcat(&trash, srv->rdr_pfx, srv->rdr_len))
4145 return;
4146 }
4147
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004148 /* 2: add the request Path */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004149 htx = htxbuf(&req->buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02004150 sl = http_get_stline(htx);
Amaury Denoyellec453f952021-07-06 11:40:12 +02004151 parser = http_uri_parser_init(htx_sl_req_uri(sl));
4152 path = http_parse_path(&parser);
Tim Duesterhused526372020-03-05 17:56:33 +01004153 if (!isttest(path))
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004154 return;
4155
4156 if (!chunk_memcat(&trash, path.ptr, path.len))
4157 return;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004158 location = ist2(trash.area, trash.data);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004159
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004160 /*
4161 * Create the 302 respone
4162 */
4163 htx = htx_from_buf(&res->buf);
4164 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
4165 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
4166 ist("HTTP/1.1"), ist("302"), ist("Found"));
4167 if (!sl)
4168 goto fail;
4169 sl->info.res.status = 302;
4170 s->txn->status = 302;
4171
4172 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) ||
4173 !htx_add_header(htx, ist("Connection"), ist("close")) ||
4174 !htx_add_header(htx, ist("Content-length"), ist("0")) ||
4175 !htx_add_header(htx, ist("Location"), location))
4176 goto fail;
4177
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004178 if (!htx_add_endof(htx, HTX_BLK_EOH))
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004179 goto fail;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004180
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004181 htx->flags |= HTX_FL_EOM;
Christopher Fauletc20afb82020-01-24 19:16:26 +01004182 htx_to_buf(htx, &res->buf);
Christopher Fauleta72a7e42020-01-28 09:28:11 +01004183 if (!http_forward_proxy_resp(s, 1))
4184 goto fail;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004185
4186 /* return without error. */
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004187 si_shutr(si);
4188 si_shutw(si);
4189 si->err_type = SI_ET_NONE;
4190 si->state = SI_ST_CLO;
4191
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004192 if (!(s->flags & SF_ERR_MASK))
4193 s->flags |= SF_ERR_LOCAL;
4194 if (!(s->flags & SF_FINST_MASK))
4195 s->flags |= SF_FINST_C;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004196
4197 /* FIXME: we should increase a counter of redirects per server and per backend. */
4198 srv_inc_sess_ctr(srv);
4199 srv_set_sess_last(srv);
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004200 return;
4201
4202 fail:
4203 /* If an error occurred, remove the incomplete HTTP response from the
4204 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004205 channel_htx_truncate(res, htx);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004206}
4207
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05004208/* This function terminates the request because it was completely analyzed or
Christopher Fauletf2824e62018-10-01 12:12:37 +02004209 * because an error was triggered during the body forwarding.
4210 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004211static void http_end_request(struct stream *s)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004212{
4213 struct channel *chn = &s->req;
4214 struct http_txn *txn = s->txn;
4215
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004216 DBG_TRACE_ENTER(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004217
Christopher Fauletb42a8b62018-11-19 21:59:00 +01004218 if (unlikely(txn->req.msg_state == HTTP_MSG_ERROR ||
4219 txn->rsp.msg_state == HTTP_MSG_ERROR)) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004220 channel_abort(chn);
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004221 channel_htx_truncate(chn, htxbuf(&chn->buf));
Christopher Fauletf2824e62018-10-01 12:12:37 +02004222 goto end;
4223 }
4224
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004225 if (unlikely(txn->req.msg_state < HTTP_MSG_DONE)) {
4226 DBG_TRACE_DEVEL("waiting end of the request", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004227 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004228 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004229
4230 if (txn->req.msg_state == HTTP_MSG_DONE) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004231 /* No need to read anymore, the request was completely parsed.
4232 * We can shut the read side unless we want to abort_on_close,
4233 * or we have a POST request. The issue with POST requests is
4234 * that some browsers still send a CRLF after the request, and
4235 * this CRLF must be read so that it does not remain in the kernel
4236 * buffers, otherwise a close could cause an RST on some systems
4237 * (eg: Linux).
4238 */
Christopher Faulet769d0e92019-03-22 14:23:18 +01004239 if (!(s->be->options & PR_O_ABRT_CLOSE) && txn->meth != HTTP_METH_POST)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004240 channel_dont_read(chn);
4241
4242 /* if the server closes the connection, we want to immediately react
4243 * and close the socket to save packets and syscalls.
4244 */
4245 s->si[1].flags |= SI_FL_NOHALF;
4246
4247 /* In any case we've finished parsing the request so we must
4248 * disable Nagle when sending data because 1) we're not going
4249 * to shut this side, and 2) the server is waiting for us to
4250 * send pending data.
4251 */
4252 chn->flags |= CF_NEVER_WAIT;
4253
Christopher Fauletd01ce402019-01-02 17:44:13 +01004254 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
4255 /* The server has not finished to respond, so we
4256 * don't want to move in order not to upset it.
4257 */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004258 DBG_TRACE_DEVEL("waiting end of the response", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletd01ce402019-01-02 17:44:13 +01004259 return;
4260 }
4261
Christopher Fauletf2824e62018-10-01 12:12:37 +02004262 /* When we get here, it means that both the request and the
4263 * response have finished receiving. Depending on the connection
4264 * mode, we'll have to wait for the last bytes to leave in either
4265 * direction, and sometimes for a close to be effective.
4266 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02004267 if (txn->flags & TX_CON_WANT_TUN) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004268 /* Tunnel mode will not have any analyser so it needs to
4269 * poll for reads.
4270 */
4271 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004272 if (b_data(&chn->buf)) {
4273 DBG_TRACE_DEVEL("waiting to flush the request", STRM_EV_HTTP_ANA, s, txn);
Christopher Faulet9768c262018-10-22 09:34:31 +02004274 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004275 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004276 txn->req.msg_state = HTTP_MSG_TUNNEL;
4277 }
4278 else {
4279 /* we're not expecting any new data to come for this
4280 * transaction, so we can close it.
Christopher Faulet9768c262018-10-22 09:34:31 +02004281 *
4282 * However, there is an exception if the response
4283 * length is undefined. In this case, we need to wait
4284 * the close from the server. The response will be
4285 * switched in TUNNEL mode until the end.
Christopher Fauletf2824e62018-10-01 12:12:37 +02004286 */
4287 if (!(txn->rsp.flags & HTTP_MSGF_XFER_LEN) &&
4288 txn->rsp.msg_state != HTTP_MSG_CLOSED)
Christopher Faulet9768c262018-10-22 09:34:31 +02004289 goto check_channel_flags;
Christopher Fauletf2824e62018-10-01 12:12:37 +02004290
4291 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4292 channel_shutr_now(chn);
4293 channel_shutw_now(chn);
4294 }
4295 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004296 goto check_channel_flags;
4297 }
4298
4299 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
4300 http_msg_closing:
4301 /* nothing else to forward, just waiting for the output buffer
4302 * to be empty and for the shutw_now to take effect.
4303 */
4304 if (channel_is_empty(chn)) {
4305 txn->req.msg_state = HTTP_MSG_CLOSED;
4306 goto http_msg_closed;
4307 }
4308 else if (chn->flags & CF_SHUTW) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004309 txn->req.msg_state = HTTP_MSG_ERROR;
4310 goto end;
4311 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004312 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004313 return;
4314 }
4315
4316 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
4317 http_msg_closed:
Christopher Fauletf2824e62018-10-01 12:12:37 +02004318 /* if we don't know whether the server will close, we need to hard close */
4319 if (txn->rsp.flags & HTTP_MSGF_XFER_LEN)
4320 s->si[1].flags |= SI_FL_NOLINGER; /* we want to close ASAP */
Christopher Fauletf2824e62018-10-01 12:12:37 +02004321 /* see above in MSG_DONE why we only do this in these states */
Christopher Faulet769d0e92019-03-22 14:23:18 +01004322 if (!(s->be->options & PR_O_ABRT_CLOSE))
Christopher Fauletf2824e62018-10-01 12:12:37 +02004323 channel_dont_read(chn);
4324 goto end;
4325 }
4326
4327 check_channel_flags:
4328 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4329 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4330 /* if we've just closed an output, let's switch */
4331 txn->req.msg_state = HTTP_MSG_CLOSING;
4332 goto http_msg_closing;
4333 }
4334
4335 end:
4336 chn->analysers &= AN_REQ_FLT_END;
Christopher Faulet198ef8b2020-12-15 13:32:55 +01004337 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
4338 chn->flags |= CF_NEVER_WAIT;
4339 if (HAS_REQ_DATA_FILTERS(s))
Christopher Fauletf2824e62018-10-01 12:12:37 +02004340 chn->analysers |= AN_REQ_FLT_XFER_DATA;
Christopher Faulet198ef8b2020-12-15 13:32:55 +01004341 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004342 channel_auto_close(chn);
4343 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004344 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004345}
4346
4347
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05004348/* This function terminates the response because it was completely analyzed or
Christopher Fauletf2824e62018-10-01 12:12:37 +02004349 * because an error was triggered during the body forwarding.
4350 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004351static void http_end_response(struct stream *s)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004352{
4353 struct channel *chn = &s->res;
4354 struct http_txn *txn = s->txn;
4355
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004356 DBG_TRACE_ENTER(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004357
Christopher Fauletb42a8b62018-11-19 21:59:00 +01004358 if (unlikely(txn->req.msg_state == HTTP_MSG_ERROR ||
4359 txn->rsp.msg_state == HTTP_MSG_ERROR)) {
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004360 channel_htx_truncate(&s->req, htxbuf(&s->req.buf));
Christopher Faulet9768c262018-10-22 09:34:31 +02004361 channel_abort(&s->req);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004362 goto end;
4363 }
4364
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004365 if (unlikely(txn->rsp.msg_state < HTTP_MSG_DONE)) {
4366 DBG_TRACE_DEVEL("waiting end of the response", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004367 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004368 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004369
4370 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
4371 /* In theory, we don't need to read anymore, but we must
4372 * still monitor the server connection for a possible close
4373 * while the request is being uploaded, so we don't disable
4374 * reading.
4375 */
4376 /* channel_dont_read(chn); */
4377
4378 if (txn->req.msg_state < HTTP_MSG_DONE) {
4379 /* The client seems to still be sending data, probably
4380 * because we got an error response during an upload.
4381 * We have the choice of either breaking the connection
4382 * or letting it pass through. Let's do the later.
4383 */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004384 DBG_TRACE_DEVEL("waiting end of the request", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004385 return;
4386 }
4387
4388 /* When we get here, it means that both the request and the
4389 * response have finished receiving. Depending on the connection
4390 * mode, we'll have to wait for the last bytes to leave in either
4391 * direction, and sometimes for a close to be effective.
4392 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02004393 if (txn->flags & TX_CON_WANT_TUN) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004394 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004395 if (b_data(&chn->buf)) {
4396 DBG_TRACE_DEVEL("waiting to flush the respone", STRM_EV_HTTP_ANA, s, txn);
Christopher Faulet9768c262018-10-22 09:34:31 +02004397 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004398 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004399 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
4400 }
4401 else {
4402 /* we're not expecting any new data to come for this
4403 * transaction, so we can close it.
4404 */
4405 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4406 channel_shutr_now(chn);
4407 channel_shutw_now(chn);
4408 }
4409 }
4410 goto check_channel_flags;
4411 }
4412
4413 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
4414 http_msg_closing:
4415 /* nothing else to forward, just waiting for the output buffer
4416 * to be empty and for the shutw_now to take effect.
4417 */
4418 if (channel_is_empty(chn)) {
4419 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4420 goto http_msg_closed;
4421 }
4422 else if (chn->flags & CF_SHUTW) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004423 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau4781b152021-04-06 13:53:36 +02004424 _HA_ATOMIC_INC(&strm_sess(s)->fe->fe_counters.cli_aborts);
4425 _HA_ATOMIC_INC(&s->be->be_counters.cli_aborts);
William Lallemand36119de2021-03-08 15:26:48 +01004426 if (strm_sess(s)->listener && strm_sess(s)->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02004427 _HA_ATOMIC_INC(&strm_sess(s)->listener->counters->cli_aborts);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004428 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02004429 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.cli_aborts);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004430 goto end;
4431 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004432 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004433 return;
4434 }
4435
4436 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
4437 http_msg_closed:
4438 /* drop any pending data */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004439 channel_htx_truncate(&s->req, htxbuf(&s->req.buf));
Christopher Faulet9768c262018-10-22 09:34:31 +02004440 channel_abort(&s->req);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004441 goto end;
4442 }
4443
4444 check_channel_flags:
4445 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4446 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4447 /* if we've just closed an output, let's switch */
4448 txn->rsp.msg_state = HTTP_MSG_CLOSING;
4449 goto http_msg_closing;
4450 }
4451
4452 end:
4453 chn->analysers &= AN_RES_FLT_END;
Christopher Faulet198ef8b2020-12-15 13:32:55 +01004454 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
4455 chn->flags |= CF_NEVER_WAIT;
4456 if (HAS_RSP_DATA_FILTERS(s))
4457 chn->analysers |= AN_RES_FLT_XFER_DATA;
4458 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004459 channel_auto_close(chn);
4460 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004461 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004462}
4463
Christopher Fauletef70e252020-01-28 09:26:19 +01004464/* Forward a response generated by HAProxy (error/redirect/return). This
4465 * function forwards all pending incoming data. If <final> is set to 0, nothing
4466 * more is performed. It is used for 1xx informational messages. Otherwise, the
Christopher Faulet507479b2020-05-15 12:29:46 +02004467 * transaction is terminated and the request is emptied. On success 1 is
Christopher Faulet40e6b552020-06-25 16:04:50 +02004468 * returned. If an error occurred, 0 is returned. If it fails, this function
4469 * only exits. It is the caller responsibility to do the cleanup.
Christopher Fauletef70e252020-01-28 09:26:19 +01004470 */
4471int http_forward_proxy_resp(struct stream *s, int final)
4472{
4473 struct channel *req = &s->req;
4474 struct channel *res = &s->res;
4475 struct htx *htx = htxbuf(&res->buf);
4476 size_t data;
4477
4478 if (final) {
4479 htx->flags |= HTX_FL_PROXY_RESP;
Christopher Faulet507479b2020-05-15 12:29:46 +02004480
Christopher Fauletaab1b672020-11-18 16:44:02 +01004481 if (!htx_is_empty(htx) && !http_eval_after_res_rules(s))
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01004482 return 0;
Christopher Fauletef70e252020-01-28 09:26:19 +01004483
Christopher Fauletd6c48362020-10-19 18:01:38 +02004484 if (s->txn->meth == HTTP_METH_HEAD)
4485 htx_skip_msg_payload(htx);
4486
Christopher Fauletef70e252020-01-28 09:26:19 +01004487 channel_auto_read(req);
4488 channel_abort(req);
4489 channel_auto_close(req);
4490 channel_htx_erase(req, htxbuf(&req->buf));
4491
4492 res->wex = tick_add_ifset(now_ms, res->wto);
4493 channel_auto_read(res);
4494 channel_auto_close(res);
4495 channel_shutr_now(res);
Christopher Faulet1a9db7c2020-06-25 15:36:45 +02004496 res->flags |= CF_EOI; /* The response is terminated, add EOI */
Christopher Faulet42432f32020-11-20 17:43:16 +01004497 htxbuf(&res->buf)->flags |= HTX_FL_EOM; /* no more data are expected */
Christopher Fauletef70e252020-01-28 09:26:19 +01004498 }
Christopher Fauletcf6898c2020-06-25 15:55:11 +02004499 else {
4500 /* Send ASAP informational messages. Rely on CF_EOI for final
4501 * response.
4502 */
4503 res->flags |= CF_SEND_DONTWAIT;
4504 }
Christopher Fauletef70e252020-01-28 09:26:19 +01004505
4506 data = htx->data - co_data(res);
4507 c_adv(res, data);
4508 htx->first = -1;
4509 res->total += data;
4510 return 1;
4511}
4512
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004513void http_server_error(struct stream *s, struct stream_interface *si, int err,
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004514 int finst, struct http_reply *msg)
Christopher Faulet0f226952018-10-22 09:29:56 +02004515{
Christopher Faulet72c7d8d2020-01-27 15:32:25 +01004516 http_reply_and_close(s, s->txn->status, msg);
Christopher Faulet0f226952018-10-22 09:29:56 +02004517 if (!(s->flags & SF_ERR_MASK))
4518 s->flags |= err;
4519 if (!(s->flags & SF_FINST_MASK))
4520 s->flags |= finst;
4521}
4522
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004523void http_reply_and_close(struct stream *s, short status, struct http_reply *msg)
Christopher Faulet0f226952018-10-22 09:29:56 +02004524{
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004525 if (!msg) {
4526 channel_htx_truncate(&s->res, htxbuf(&s->res.buf));
4527 goto end;
4528 }
4529
4530 if (http_reply_message(s, msg) == -1) {
4531 /* On error, return a 500 error message, but don't rewrite it if
Christopher Faulet40e6b552020-06-25 16:04:50 +02004532 * it is already an internal error. If it was already a "const"
4533 * 500 error, just fail.
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004534 */
Christopher Faulet40e6b552020-06-25 16:04:50 +02004535 if (s->txn->status == 500) {
4536 if (s->txn->flags & TX_CONST_REPLY)
4537 goto end;
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004538 s->txn->flags |= TX_CONST_REPLY;
Christopher Faulet40e6b552020-06-25 16:04:50 +02004539 }
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004540 s->txn->status = 500;
4541 s->txn->http_reply = NULL;
4542 return http_reply_and_close(s, s->txn->status, http_error_message(s));
4543 }
4544
4545end:
4546 s->res.wex = tick_add_ifset(now_ms, s->res.wto);
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004547
Christopher Faulet2d565002021-09-10 09:17:50 +02004548 /* At this staged, HTTP analysis is finished */
4549 s->req.analysers &= AN_REQ_FLT_END;
4550 s->req.analyse_exp = TICK_ETERNITY;
4551
4552 s->res.analysers &= AN_RES_FLT_END;
4553 s->res.analyse_exp = TICK_ETERNITY;
4554
Christopher Faulet0f226952018-10-22 09:29:56 +02004555 channel_auto_read(&s->req);
4556 channel_abort(&s->req);
4557 channel_auto_close(&s->req);
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004558 channel_htx_erase(&s->req, htxbuf(&s->req.buf));
Christopher Faulet72c7d8d2020-01-27 15:32:25 +01004559 channel_auto_read(&s->res);
4560 channel_auto_close(&s->res);
4561 channel_shutr_now(&s->res);
Christopher Faulet0f226952018-10-22 09:29:56 +02004562}
4563
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004564struct http_reply *http_error_message(struct stream *s)
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004565{
4566 const int msgnum = http_get_status_idx(s->txn->status);
4567
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004568 if (s->txn->http_reply)
4569 return s->txn->http_reply;
4570 else if (s->be->replies[msgnum])
4571 return s->be->replies[msgnum];
4572 else if (strm_fe(s)->replies[msgnum])
4573 return strm_fe(s)->replies[msgnum];
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004574 else
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004575 return &http_err_replies[msgnum];
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004576}
4577
Christopher Faulet40e6b552020-06-25 16:04:50 +02004578/* Produces an HTX message from an http reply. Depending on the http reply type,
4579 * a, errorfile, an raw file or a log-format string is used. On success, it
4580 * returns 0. If an error occurs -1 is returned. If it fails, this function only
4581 * exits. It is the caller responsibility to do the cleanup.
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004582 */
Christopher Fauletae43b6c2020-05-27 15:24:22 +02004583int http_reply_to_htx(struct stream *s, struct htx *htx, struct http_reply *reply)
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004584{
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004585 struct buffer *errmsg;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004586 struct htx_sl *sl;
4587 struct buffer *body = NULL;
4588 const char *status, *reason, *clen, *ctype;
4589 unsigned int slflags;
4590 int ret = 0;
4591
Christopher Faulete29a97e2020-05-14 14:49:25 +02004592 /*
4593 * - HTTP_REPLY_ERRFILES unexpected here. handled as no payload if so
4594 *
4595 * - HTTP_REPLY_INDIRECT: switch on another reply if defined or handled
4596 * as no payload if NULL. the TXN status code is set with the status
4597 * of the original reply.
4598 */
4599
4600 if (reply->type == HTTP_REPLY_INDIRECT) {
4601 if (reply->body.reply)
4602 reply = reply->body.reply;
4603 }
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004604 if (reply->type == HTTP_REPLY_ERRMSG && !reply->body.errmsg) {
4605 /* get default error message */
4606 if (reply == s->txn->http_reply)
4607 s->txn->http_reply = NULL;
4608 reply = http_error_message(s);
4609 if (reply->type == HTTP_REPLY_INDIRECT) {
4610 if (reply->body.reply)
4611 reply = reply->body.reply;
4612 }
4613 }
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004614
4615 if (reply->type == HTTP_REPLY_ERRMSG) {
4616 /* implicit or explicit error message*/
4617 errmsg = reply->body.errmsg;
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004618 if (errmsg && !b_is_null(errmsg)) {
Christopher Faulet20567362020-05-15 14:52:49 +02004619 if (!htx_copy_msg(htx, errmsg))
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004620 goto fail;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004621 }
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004622 }
4623 else {
4624 /* no payload, file or log-format string */
4625 if (reply->type == HTTP_REPLY_RAW) {
4626 /* file */
4627 body = &reply->body.obj;
4628 }
4629 else if (reply->type == HTTP_REPLY_LOGFMT) {
4630 /* log-format string */
4631 body = alloc_trash_chunk();
4632 if (!body)
4633 goto fail_alloc;
4634 body->data = build_logline(s, body->area, body->size, &reply->body.fmt);
4635 }
4636 /* else no payload */
4637
4638 status = ultoa(reply->status);
4639 reason = http_get_reason(reply->status);
4640 slflags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_CLEN);
4641 if (!body || !b_data(body))
4642 slflags |= HTX_SL_F_BODYLESS;
4643 sl = htx_add_stline(htx, HTX_BLK_RES_SL, slflags, ist("HTTP/1.1"), ist(status), ist(reason));
4644 if (!sl)
4645 goto fail;
4646 sl->info.res.status = reply->status;
4647
4648 clen = (body ? ultoa(b_data(body)) : "0");
4649 ctype = reply->ctype;
4650
4651 if (!LIST_ISEMPTY(&reply->hdrs)) {
4652 struct http_reply_hdr *hdr;
4653 struct buffer *value = alloc_trash_chunk();
4654
4655 if (!value)
4656 goto fail;
4657
4658 list_for_each_entry(hdr, &reply->hdrs, list) {
4659 chunk_reset(value);
4660 value->data = build_logline(s, value->area, value->size, &hdr->value);
4661 if (b_data(value) && !htx_add_header(htx, hdr->name, ist2(b_head(value), b_data(value)))) {
4662 free_trash_chunk(value);
4663 goto fail;
4664 }
4665 chunk_reset(value);
4666 }
4667 free_trash_chunk(value);
4668 }
4669
4670 if (!htx_add_header(htx, ist("content-length"), ist(clen)) ||
4671 (body && b_data(body) && ctype && !htx_add_header(htx, ist("content-type"), ist(ctype))) ||
4672 !htx_add_endof(htx, HTX_BLK_EOH) ||
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004673 (body && b_data(body) && !htx_add_data_atonce(htx, ist2(b_head(body), b_data(body)))))
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004674 goto fail;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004675
4676 htx->flags |= HTX_FL_EOM;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004677 }
4678
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004679 leave:
4680 if (reply->type == HTTP_REPLY_LOGFMT)
4681 free_trash_chunk(body);
4682 return ret;
4683
4684 fail_alloc:
4685 if (!(s->flags & SF_ERR_MASK))
4686 s->flags |= SF_ERR_RESOURCE;
Christopher Faulet97e466c2020-05-15 15:12:47 +02004687 /* fall through */
4688 fail:
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004689 ret = -1;
4690 goto leave;
Christopher Faulet97e466c2020-05-15 15:12:47 +02004691}
4692
4693/* Send an http reply to the client. On success, it returns 0. If an error
Christopher Faulet40e6b552020-06-25 16:04:50 +02004694 * occurs -1 is returned and the response channel is truncated, removing this
4695 * way the faulty reply. This function may fail when the reply is formatted
4696 * (http_reply_to_htx) or when the reply is forwarded
4697 * (http_forward_proxy_resp). On the last case, it is because a
4698 * http-after-response rule fails.
Christopher Faulet97e466c2020-05-15 15:12:47 +02004699 */
4700int http_reply_message(struct stream *s, struct http_reply *reply)
4701{
4702 struct channel *res = &s->res;
4703 struct htx *htx = htx_from_buf(&res->buf);
4704
4705 if (s->txn->status == -1)
4706 s->txn->status = reply->status;
4707 channel_htx_truncate(res, htx);
4708
4709 if (http_reply_to_htx(s, htx, reply) == -1)
4710 goto fail;
4711
4712 htx_to_buf(htx, &s->res.buf);
4713 if (!http_forward_proxy_resp(s, 1))
4714 goto fail;
4715 return 0;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004716
4717 fail:
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004718 channel_htx_truncate(res, htx);
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004719 if (!(s->flags & SF_ERR_MASK))
4720 s->flags |= SF_ERR_PRXCOND;
Christopher Faulet97e466c2020-05-15 15:12:47 +02004721 return -1;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004722}
4723
Christopher Faulet304cc402019-07-15 15:46:28 +02004724/* Return the error message corresponding to si->err_type. It is assumed
4725 * that the server side is closed. Note that err_type is actually a
4726 * bitmask, where almost only aborts may be cumulated with other
4727 * values. We consider that aborted operations are more important
4728 * than timeouts or errors due to the fact that nobody else in the
4729 * logs might explain incomplete retries. All others should avoid
4730 * being cumulated. It should normally not be possible to have multiple
4731 * aborts at once, but just in case, the first one in sequence is reported.
4732 * Note that connection errors appearing on the second request of a keep-alive
4733 * connection are not reported since this allows the client to retry.
4734 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004735void http_return_srv_error(struct stream *s, struct stream_interface *si)
Christopher Faulet304cc402019-07-15 15:46:28 +02004736{
4737 int err_type = si->err_type;
4738
4739 /* set s->txn->status for http_error_message(s) */
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004740 if (err_type & SI_ET_QUEUE_ABRT) {
4741 s->txn->status = -1;
4742 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_Q, NULL);
4743 }
4744 else if (err_type & SI_ET_CONN_ABRT) {
4745 s->txn->status = -1;
4746 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_C, NULL);
4747 }
4748 else if (err_type & SI_ET_QUEUE_TO) {
4749 s->txn->status = 503;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004750 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_Q,
4751 http_error_message(s));
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004752 }
4753 else if (err_type & SI_ET_QUEUE_ERR) {
4754 s->txn->status = 503;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004755 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_Q,
4756 http_error_message(s));
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004757 }
4758 else if (err_type & SI_ET_CONN_TO) {
4759 s->txn->status = 503;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004760 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_C,
4761 (s->txn->flags & TX_NOT_FIRST) ? NULL :
4762 http_error_message(s));
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004763 }
4764 else if (err_type & SI_ET_CONN_ERR) {
4765 s->txn->status = 503;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004766 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_C,
4767 (s->flags & SF_SRV_REUSED) ? NULL :
4768 http_error_message(s));
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004769 }
4770 else if (err_type & SI_ET_CONN_RES) {
4771 s->txn->status = 503;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004772 http_server_error(s, si, SF_ERR_RESOURCE, SF_FINST_C,
4773 (s->txn->flags & TX_NOT_FIRST) ? NULL :
4774 http_error_message(s));
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004775 }
Christopher Faulet304cc402019-07-15 15:46:28 +02004776 else { /* SI_ET_CONN_OTHER and others */
4777 s->txn->status = 500;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004778 http_server_error(s, si, SF_ERR_INTERNAL, SF_FINST_C,
4779 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004780 }
4781}
4782
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004783
Christopher Faulet4a28a532019-03-01 11:19:40 +01004784/* Handle Expect: 100-continue for HTTP/1.1 messages if necessary. It returns 0
4785 * on success and -1 on error.
4786 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004787static int http_handle_expect_hdr(struct stream *s, struct htx *htx, struct http_msg *msg)
Christopher Faulet4a28a532019-03-01 11:19:40 +01004788{
4789 /* If we have HTTP/1.1 message with a body and Expect: 100-continue,
4790 * then we must send an HTTP/1.1 100 Continue intermediate response.
4791 */
4792 if (msg->msg_state == HTTP_MSG_BODY && (msg->flags & HTTP_MSGF_VER_11) &&
4793 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
4794 struct ist hdr = { .ptr = "Expect", .len = 6 };
4795 struct http_hdr_ctx ctx;
4796
4797 ctx.blk = NULL;
4798 /* Expect is allowed in 1.1, look for it */
4799 if (http_find_header(htx, hdr, &ctx, 0) &&
4800 unlikely(isteqi(ctx.value, ist2("100-continue", 12)))) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004801 if (http_reply_100_continue(s) == -1)
Christopher Faulet4a28a532019-03-01 11:19:40 +01004802 return -1;
4803 http_remove_header(htx, &ctx);
4804 }
4805 }
4806 return 0;
4807}
4808
Christopher Faulet23a3c792018-11-28 10:01:23 +01004809/* Send a 100-Continue response to the client. It returns 0 on success and -1
4810 * on error. The response channel is updated accordingly.
4811 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004812static int http_reply_100_continue(struct stream *s)
Christopher Faulet23a3c792018-11-28 10:01:23 +01004813{
4814 struct channel *res = &s->res;
4815 struct htx *htx = htx_from_buf(&res->buf);
4816 struct htx_sl *sl;
4817 unsigned int flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|
4818 HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
Christopher Faulet23a3c792018-11-28 10:01:23 +01004819
4820 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
4821 ist("HTTP/1.1"), ist("100"), ist("Continue"));
4822 if (!sl)
4823 goto fail;
4824 sl->info.res.status = 100;
4825
Christopher Faulet1d5ec092019-06-26 14:23:54 +02004826 if (!htx_add_endof(htx, HTX_BLK_EOH))
Christopher Faulet23a3c792018-11-28 10:01:23 +01004827 goto fail;
4828
Christopher Fauleta72a7e42020-01-28 09:28:11 +01004829 if (!http_forward_proxy_resp(s, 0))
4830 goto fail;
Christopher Faulet23a3c792018-11-28 10:01:23 +01004831 return 0;
4832
4833 fail:
4834 /* If an error occurred, remove the incomplete HTTP response from the
4835 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004836 channel_htx_truncate(res, htx);
Christopher Faulet23a3c792018-11-28 10:01:23 +01004837 return -1;
4838}
4839
Christopher Faulet12c51e22018-11-28 15:59:42 +01004840
Christopher Faulet0f226952018-10-22 09:29:56 +02004841/*
4842 * Capture headers from message <htx> according to header list <cap_hdr>, and
4843 * fill the <cap> pointers appropriately.
4844 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004845static void http_capture_headers(struct htx *htx, char **cap, struct cap_hdr *cap_hdr)
Christopher Faulet0f226952018-10-22 09:29:56 +02004846{
4847 struct cap_hdr *h;
4848 int32_t pos;
4849
Christopher Fauleta3f15502019-05-13 15:27:23 +02004850 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet0f226952018-10-22 09:29:56 +02004851 struct htx_blk *blk = htx_get_blk(htx, pos);
4852 enum htx_blk_type type = htx_get_blk_type(blk);
4853 struct ist n, v;
4854
4855 if (type == HTX_BLK_EOH)
4856 break;
4857 if (type != HTX_BLK_HDR)
4858 continue;
4859
4860 n = htx_get_blk_name(htx, blk);
4861
4862 for (h = cap_hdr; h; h = h->next) {
4863 if (h->namelen && (h->namelen == n.len) &&
4864 (strncasecmp(n.ptr, h->name, h->namelen) == 0)) {
4865 if (cap[h->index] == NULL)
4866 cap[h->index] =
4867 pool_alloc(h->pool);
4868
4869 if (cap[h->index] == NULL) {
4870 ha_alert("HTTP capture : out of memory.\n");
4871 break;
4872 }
4873
4874 v = htx_get_blk_value(htx, blk);
4875 if (v.len > h->len)
4876 v.len = h->len;
4877
4878 memcpy(cap[h->index], v.ptr, v.len);
4879 cap[h->index][v.len]=0;
4880 }
4881 }
4882 }
4883}
4884
Christopher Faulet0b6bdc52018-10-24 11:05:36 +02004885/* Delete a value in a header between delimiters <from> and <next>. The header
4886 * itself is delimited by <start> and <end> pointers. The number of characters
4887 * displaced is returned, and the pointer to the first delimiter is updated if
4888 * required. The function tries as much as possible to respect the following
4889 * principles :
4890 * - replace <from> delimiter by the <next> one unless <from> points to <start>,
4891 * in which case <next> is simply removed
4892 * - set exactly one space character after the new first delimiter, unless there
4893 * are not enough characters in the block being moved to do so.
4894 * - remove unneeded spaces before the previous delimiter and after the new
4895 * one.
4896 *
4897 * It is the caller's responsibility to ensure that :
4898 * - <from> points to a valid delimiter or <start> ;
4899 * - <next> points to a valid delimiter or <end> ;
4900 * - there are non-space chars before <from>.
4901 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004902static int http_del_hdr_value(char *start, char *end, char **from, char *next)
Christopher Faulet0b6bdc52018-10-24 11:05:36 +02004903{
4904 char *prev = *from;
4905
4906 if (prev == start) {
4907 /* We're removing the first value. eat the semicolon, if <next>
4908 * is lower than <end> */
4909 if (next < end)
4910 next++;
4911
4912 while (next < end && HTTP_IS_SPHT(*next))
4913 next++;
4914 }
4915 else {
4916 /* Remove useless spaces before the old delimiter. */
4917 while (HTTP_IS_SPHT(*(prev-1)))
4918 prev--;
4919 *from = prev;
4920
4921 /* copy the delimiter and if possible a space if we're
4922 * not at the end of the line.
4923 */
4924 if (next < end) {
4925 *prev++ = *next++;
4926 if (prev + 1 < next)
4927 *prev++ = ' ';
4928 while (next < end && HTTP_IS_SPHT(*next))
4929 next++;
4930 }
4931 }
4932 memmove(prev, next, end - next);
4933 return (prev - next);
4934}
4935
Christopher Faulet0f226952018-10-22 09:29:56 +02004936
4937/* Formats the start line of the request (without CRLF) and puts it in <str> and
Joseph Herlantc42c0e92018-11-25 10:43:27 -08004938 * return the written length. The line can be truncated if it exceeds <len>.
Christopher Faulet0f226952018-10-22 09:29:56 +02004939 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004940static size_t http_fmt_req_line(const struct htx_sl *sl, char *str, size_t len)
Christopher Faulet0f226952018-10-22 09:29:56 +02004941{
4942 struct ist dst = ist2(str, 0);
4943
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004944 if (istcat(&dst, htx_sl_req_meth(sl), len) == -1)
Christopher Faulet0f226952018-10-22 09:29:56 +02004945 goto end;
4946 if (dst.len + 1 > len)
4947 goto end;
4948 dst.ptr[dst.len++] = ' ';
4949
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004950 if (istcat(&dst, htx_sl_req_uri(sl), len) == -1)
Christopher Faulet0f226952018-10-22 09:29:56 +02004951 goto end;
4952 if (dst.len + 1 > len)
4953 goto end;
4954 dst.ptr[dst.len++] = ' ';
4955
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004956 istcat(&dst, htx_sl_req_vsn(sl), len);
Christopher Faulet0f226952018-10-22 09:29:56 +02004957 end:
4958 return dst.len;
4959}
4960
4961/*
4962 * Print a debug line with a start line.
4963 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004964static void http_debug_stline(const char *dir, struct stream *s, const struct htx_sl *sl)
Christopher Faulet0f226952018-10-22 09:29:56 +02004965{
4966 struct session *sess = strm_sess(s);
4967 int max;
4968
4969 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
4970 dir,
4971 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->handle.fd : -1,
4972 objt_cs(s->si[1].end) ? (unsigned short)objt_cs(s->si[1].end)->conn->handle.fd : -1);
4973
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004974 max = HTX_SL_P1_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02004975 UBOUND(max, trash.size - trash.data - 3);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004976 chunk_memcat(&trash, HTX_SL_P1_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02004977 trash.area[trash.data++] = ' ';
4978
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004979 max = HTX_SL_P2_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02004980 UBOUND(max, trash.size - trash.data - 2);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004981 chunk_memcat(&trash, HTX_SL_P2_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02004982 trash.area[trash.data++] = ' ';
4983
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004984 max = HTX_SL_P3_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02004985 UBOUND(max, trash.size - trash.data - 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004986 chunk_memcat(&trash, HTX_SL_P3_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02004987 trash.area[trash.data++] = '\n';
4988
Willy Tarreau2e8ab6b2020-03-14 11:03:20 +01004989 DISGUISE(write(1, trash.area, trash.data));
Christopher Faulet0f226952018-10-22 09:29:56 +02004990}
4991
4992/*
4993 * Print a debug line with a header.
4994 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004995static 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 +02004996{
4997 struct session *sess = strm_sess(s);
4998 int max;
4999
5000 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
5001 dir,
5002 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->handle.fd : -1,
5003 objt_cs(s->si[1].end) ? (unsigned short)objt_cs(s->si[1].end)->conn->handle.fd : -1);
5004
5005 max = n.len;
5006 UBOUND(max, trash.size - trash.data - 3);
5007 chunk_memcat(&trash, n.ptr, max);
5008 trash.area[trash.data++] = ':';
5009 trash.area[trash.data++] = ' ';
5010
5011 max = v.len;
5012 UBOUND(max, trash.size - trash.data - 1);
5013 chunk_memcat(&trash, v.ptr, max);
5014 trash.area[trash.data++] = '\n';
5015
Willy Tarreau2e8ab6b2020-03-14 11:03:20 +01005016 DISGUISE(write(1, trash.area, trash.data));
Christopher Faulet0f226952018-10-22 09:29:56 +02005017}
5018
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005019/* Allocate a new HTTP transaction for stream <s> unless there is one already.
5020 * In case of allocation failure, everything allocated is freed and NULL is
5021 * returned. Otherwise the new transaction is assigned to the stream and
5022 * returned.
5023 */
5024struct http_txn *http_alloc_txn(struct stream *s)
5025{
5026 struct http_txn *txn = s->txn;
5027
5028 if (txn)
5029 return txn;
5030
5031 txn = pool_alloc(pool_head_http_txn);
5032 if (!txn)
5033 return txn;
5034
5035 s->txn = txn;
5036 return txn;
5037}
5038
5039void http_txn_reset_req(struct http_txn *txn)
5040{
Christopher Faulet1aea50e2020-01-17 16:03:53 +01005041 txn->req.flags = 0;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005042 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
5043}
5044
5045void http_txn_reset_res(struct http_txn *txn)
5046{
Christopher Faulet1aea50e2020-01-17 16:03:53 +01005047 txn->rsp.flags = 0;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005048 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
5049}
5050
5051/*
Christopher Faulet75f619a2021-03-08 19:12:58 +01005052 * Create and initialize a new HTTP transaction for stream <s>. This should be
5053 * used before processing any new request. It returns the transaction or NLULL
5054 * on error.
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005055 */
Christopher Faulet75f619a2021-03-08 19:12:58 +01005056struct http_txn *http_create_txn(struct stream *s)
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005057{
Christopher Faulet75f619a2021-03-08 19:12:58 +01005058 struct http_txn *txn;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005059 struct conn_stream *cs = objt_cs(s->si[0].end);
5060
Christopher Faulet75f619a2021-03-08 19:12:58 +01005061 txn = pool_alloc(pool_head_http_txn);
5062 if (!txn)
5063 return NULL;
5064 s->txn = txn;
5065
Christopher Fauletda831fa2020-10-06 17:58:43 +02005066 txn->flags = ((cs && cs->flags & CS_FL_NOT_FIRST) ? TX_NOT_FIRST : 0);
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005067 txn->status = -1;
Christopher Faulet5cb513a2020-05-13 17:56:56 +02005068 txn->http_reply = NULL;
Willy Tarreau8b507582020-02-25 09:35:07 +01005069 write_u32(txn->cache_hash, 0);
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005070
5071 txn->cookie_first_date = 0;
5072 txn->cookie_last_date = 0;
5073
5074 txn->srv_cookie = NULL;
5075 txn->cli_cookie = NULL;
5076 txn->uri = NULL;
5077
5078 http_txn_reset_req(txn);
5079 http_txn_reset_res(txn);
5080
5081 txn->req.chn = &s->req;
5082 txn->rsp.chn = &s->res;
5083
5084 txn->auth.method = HTTP_AUTH_UNKNOWN;
5085
Willy Tarreaub7bfcb32021-08-31 08:13:25 +02005086 vars_init_head(&s->vars_txn, SCOPE_TXN);
5087 vars_init_head(&s->vars_reqres, SCOPE_REQ);
Christopher Faulet75f619a2021-03-08 19:12:58 +01005088
5089 return txn;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005090}
5091
5092/* to be used at the end of a transaction */
Christopher Faulet75f619a2021-03-08 19:12:58 +01005093void http_destroy_txn(struct stream *s)
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005094{
5095 struct http_txn *txn = s->txn;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005096
5097 /* these ones will have been dynamically allocated */
5098 pool_free(pool_head_requri, txn->uri);
5099 pool_free(pool_head_capture, txn->cli_cookie);
5100 pool_free(pool_head_capture, txn->srv_cookie);
Tim Duesterhusa17e6622020-03-05 20:19:02 +01005101 pool_free(pool_head_uniqueid, s->unique_id.ptr);
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005102
Tim Duesterhusa17e6622020-03-05 20:19:02 +01005103 s->unique_id = IST_NULL;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005104 txn->uri = NULL;
5105 txn->srv_cookie = NULL;
5106 txn->cli_cookie = NULL;
5107
Christopher Faulet59399252019-11-07 14:27:52 +01005108 if (!LIST_ISEMPTY(&s->vars_txn.head))
5109 vars_prune(&s->vars_txn, s->sess, s);
5110 if (!LIST_ISEMPTY(&s->vars_reqres.head))
5111 vars_prune(&s->vars_reqres, s->sess, s);
Christopher Faulet75f619a2021-03-08 19:12:58 +01005112
5113 pool_free(pool_head_http_txn, txn);
5114 s->txn = NULL;
Christopher Faulet59399252019-11-07 14:27:52 +01005115}
5116
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005117
5118DECLARE_POOL(pool_head_http_txn, "http_txn", sizeof(struct http_txn));
Christopher Faulet0f226952018-10-22 09:29:56 +02005119
Christopher Fauletf4eb75d2018-10-11 15:55:07 +02005120__attribute__((constructor))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005121static void __http_protocol_init(void)
Christopher Fauletf4eb75d2018-10-11 15:55:07 +02005122{
5123}
5124
5125
5126/*
5127 * Local variables:
5128 * c-indent-level: 8
5129 * c-basic-offset: 8
5130 * End:
5131 */