blob: 7049263ae0f14ed3db5458e810553b08beb6fb78 [file] [log] [blame]
Christopher Fauletf4eb75d2018-10-11 15:55:07 +02001/*
2 * HTTP protocol analyzer
3 *
4 * Copyright (C) 2018 HAProxy Technologies, Christopher Faulet <cfaulet@haproxy.com>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
Willy Tarreaudcc048a2020-06-04 19:11:43 +020013#include <haproxy/acl.h>
Willy Tarreau122eba92020-06-04 10:15:32 +020014#include <haproxy/action-t.h>
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020015#include <haproxy/api.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020016#include <haproxy/backend.h>
Willy Tarreau8d366972020-05-27 16:10:29 +020017#include <haproxy/base64.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020018#include <haproxy/capture-t.h>
Amaury Denoyelle03517732021-05-07 14:25:01 +020019#include <haproxy/cfgparse.h>
Willy Tarreauf1d32c42020-06-04 21:07:02 +020020#include <haproxy/channel.h>
Willy Tarreau4aa573d2020-06-04 18:21:56 +020021#include <haproxy/check.h>
Willy Tarreau7ea393d2020-06-04 18:02:10 +020022#include <haproxy/connection.h>
Willy Tarreau36979d92020-06-05 17:27:29 +020023#include <haproxy/errors.h>
Willy Tarreauc7babd82020-06-04 21:29:29 +020024#include <haproxy/filters.h>
Willy Tarreaucd72d8c2020-06-02 19:11:26 +020025#include <haproxy/http.h>
Willy Tarreauc2b1ff02020-06-04 21:21:03 +020026#include <haproxy/http_ana.h>
Willy Tarreau87735332020-06-04 09:08:41 +020027#include <haproxy/http_htx.h>
Willy Tarreau16f958c2020-06-03 08:44:35 +020028#include <haproxy/htx.h>
Willy Tarreauaeed4a82020-06-04 22:01:04 +020029#include <haproxy/log.h>
Willy Tarreau6131d6a2020-06-02 16:48:09 +020030#include <haproxy/net_helper.h>
Willy Tarreaua264d962020-06-04 22:29:18 +020031#include <haproxy/proxy.h>
Willy Tarreau7cd8b6e2020-06-02 17:32:26 +020032#include <haproxy/regex.h>
Willy Tarreau1e56f922020-06-04 23:20:13 +020033#include <haproxy/server-t.h>
Willy Tarreau2eec9b52020-06-04 19:58:55 +020034#include <haproxy/stats.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020035#include <haproxy/stream.h>
Willy Tarreau5e539c92020-06-04 20:45:39 +020036#include <haproxy/stream_interface.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020037#include <haproxy/trace.h>
Willy Tarreau8c42b8a2020-06-04 19:27:34 +020038#include <haproxy/uri_auth-t.h>
Willy Tarreaua1718922020-06-04 16:25:31 +020039#include <haproxy/vars.h>
Christopher Faulete0768eb2018-10-03 16:38:02 +020040
Christopher Faulete0768eb2018-10-03 16:38:02 +020041
Christopher Fauleteea8fc72019-11-05 16:18:10 +010042#define TRACE_SOURCE &trace_strm
43
Christopher Faulet377c5a52018-10-24 21:21:30 +020044extern const char *stat_status_codes[];
Christopher Fauletf2824e62018-10-01 12:12:37 +020045
Willy Tarreauff882702021-04-10 17:23:00 +020046struct pool_head *pool_head_requri __read_mostly = NULL;
47struct pool_head *pool_head_capture __read_mostly = NULL;
Christopher Fauleta8a46e22019-07-16 14:53:09 +020048
49
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020050static void http_end_request(struct stream *s);
51static void http_end_response(struct stream *s);
Christopher Fauletf2824e62018-10-01 12:12:37 +020052
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020053static void http_capture_headers(struct htx *htx, char **cap, struct cap_hdr *cap_hdr);
54static int http_del_hdr_value(char *start, char *end, char **from, char *next);
55static size_t http_fmt_req_line(const struct htx_sl *sl, char *str, size_t len);
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020056static void http_debug_stline(const char *dir, struct stream *s, const struct htx_sl *sl);
57static void http_debug_hdr(const char *dir, struct stream *s, const struct ist n, const struct ist v);
Christopher Faulet0f226952018-10-22 09:29:56 +020058
Christopher Fauletb58f62b2020-01-13 16:40:13 +010059static enum rule_result http_req_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s);
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020060static enum rule_result http_res_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s);
Christopher Faulet3e964192018-10-24 11:39:23 +020061
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020062static void http_manage_client_side_cookies(struct stream *s, struct channel *req);
63static void http_manage_server_side_cookies(struct stream *s, struct channel *res);
Christopher Fauletfcda7c62018-10-24 11:56:22 +020064
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020065static int http_stats_check_uri(struct stream *s, struct http_txn *txn, struct proxy *backend);
66static int http_handle_stats(struct stream *s, struct channel *req);
Christopher Faulet377c5a52018-10-24 21:21:30 +020067
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020068static int http_handle_expect_hdr(struct stream *s, struct htx *htx, struct http_msg *msg);
69static int http_reply_100_continue(struct stream *s);
Christopher Faulet23a3c792018-11-28 10:01:23 +010070
Christopher Faulete0768eb2018-10-03 16:38:02 +020071/* This stream analyser waits for a complete HTTP request. It returns 1 if the
72 * processing can continue on next analysers, or zero if it either needs more
73 * data or wants to immediately abort the request (eg: timeout, error, ...). It
74 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req.analysers
75 * when it has nothing left to do, and may remove any analyser when it wants to
76 * abort.
77 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020078int http_wait_for_request(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +020079{
Christopher Faulet9768c262018-10-22 09:34:31 +020080
Christopher Faulete0768eb2018-10-03 16:38:02 +020081 /*
Christopher Faulet9768c262018-10-22 09:34:31 +020082 * We will analyze a complete HTTP request to check the its syntax.
Christopher Faulete0768eb2018-10-03 16:38:02 +020083 *
Christopher Faulet9768c262018-10-22 09:34:31 +020084 * Once the start line and all headers are received, we may perform a
85 * capture of the error (if any), and we will set a few fields. We also
86 * check for monitor-uri, logging and finally headers capture.
Christopher Faulete0768eb2018-10-03 16:38:02 +020087 */
Christopher Faulete0768eb2018-10-03 16:38:02 +020088 struct session *sess = s->sess;
89 struct http_txn *txn = s->txn;
90 struct http_msg *msg = &txn->req;
Christopher Faulet9768c262018-10-22 09:34:31 +020091 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +010092 struct htx_sl *sl;
Christopher Faulete0768eb2018-10-03 16:38:02 +020093
Christopher Fauleteea8fc72019-11-05 16:18:10 +010094 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +020095
Christopher Fauletda46a0d2021-01-21 17:32:58 +010096 if (unlikely(!IS_HTX_STRM(s))) {
97 /* It is only possible when a TCP stream is upgrade to HTTP.
98 * There is a transition period during which there is no
99 * data. The stream is still in raw mode and SF_IGNORE flag is
100 * still set. When this happens, the new mux is responsible to
Ilya Shipitsinacf84592021-02-06 22:29:08 +0500101 * handle all errors. Thus we may leave immediately.
Christopher Fauletda46a0d2021-01-21 17:32:58 +0100102 */
103 BUG_ON(!(s->flags & SF_IGNORE) || !c_empty(&s->req));
Christopher Faulet9768c262018-10-22 09:34:31 +0200104
Christopher Faulet97b3a612021-03-15 17:10:12 +0100105 /* Don't connect for now */
106 channel_dont_connect(req);
107
108 /* A SHUTR at this stage means we are performing a "destructive"
109 * HTTP upgrade (TCP>H2). In this case, we can leave.
110 */
111 if (req->flags & CF_SHUTR) {
112 s->logs.logwait = 0;
113 s->logs.level = 0;
114 channel_abort(&s->req);
115 channel_abort(&s->res);
116 req->analysers &= AN_REQ_FLT_END;
117 req->analyse_exp = TICK_ETERNITY;
118 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA, s);
119 return 1;
120 }
Christopher Fauletda46a0d2021-01-21 17:32:58 +0100121 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA, s);
122 return 0;
123 }
124
125 htx = htxbuf(&req->buf);
Christopher Faulet8bebd2f2020-10-06 17:54:56 +0200126
Willy Tarreau4236f032019-03-05 10:43:32 +0100127 /* Parsing errors are caught here */
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200128 if (htx->flags & (HTX_FL_PARSING_ERROR|HTX_FL_PROCESSING_ERROR)) {
Willy Tarreau4236f032019-03-05 10:43:32 +0100129 stream_inc_http_req_ctr(s);
Emeric Brun28976442020-10-07 08:50:09 +0200130 proxy_inc_fe_req_ctr(sess->listener, sess->fe);
Christopher Fauletbf7175f2021-02-10 14:58:01 +0100131 if (htx->flags & HTX_FL_PARSING_ERROR) {
132 stream_inc_http_err_ctr(s);
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200133 goto return_bad_req;
Christopher Fauletbf7175f2021-02-10 14:58:01 +0100134 }
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200135 else
136 goto return_int_err;
Willy Tarreau4236f032019-03-05 10:43:32 +0100137 }
138
Christopher Faulete0768eb2018-10-03 16:38:02 +0200139 /* we're speaking HTTP here, so let's speak HTTP to the client */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200140 s->srv_error = http_return_srv_error;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200141
Christopher Faulet9768c262018-10-22 09:34:31 +0200142 msg->msg_state = HTTP_MSG_BODY;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200143 stream_inc_http_req_ctr(s);
Emeric Brun28976442020-10-07 08:50:09 +0200144 proxy_inc_fe_req_ctr(sess->listener, sess->fe); /* one more valid request for this FE */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200145
Christopher Faulet9768c262018-10-22 09:34:31 +0200146 /* kill the pending keep-alive timeout */
Christopher Faulet9768c262018-10-22 09:34:31 +0200147 req->analyse_exp = TICK_ETERNITY;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200148
Christopher Faulet29f17582019-05-23 11:03:26 +0200149 BUG_ON(htx_get_first_type(htx) != HTX_BLK_REQ_SL);
Christopher Faulet297fbb42019-05-13 14:41:27 +0200150 sl = http_get_stline(htx);
Christopher Faulet03599112018-11-27 11:21:21 +0100151
Christopher Faulet9768c262018-10-22 09:34:31 +0200152 /* 0: we might have to print this header in debug mode */
153 if (unlikely((global.mode & MODE_DEBUG) &&
154 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
155 int32_t pos;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200156
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200157 http_debug_stline("clireq", s, sl);
Christopher Faulet9768c262018-10-22 09:34:31 +0200158
Christopher Fauleta3f15502019-05-13 15:27:23 +0200159 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet9768c262018-10-22 09:34:31 +0200160 struct htx_blk *blk = htx_get_blk(htx, pos);
161 enum htx_blk_type type = htx_get_blk_type(blk);
162
163 if (type == HTX_BLK_EOH)
164 break;
165 if (type != HTX_BLK_HDR)
166 continue;
167
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200168 http_debug_hdr("clihdr", s,
169 htx_get_blk_name(htx, blk),
170 htx_get_blk_value(htx, blk));
Christopher Faulet9768c262018-10-22 09:34:31 +0200171 }
172 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200173
174 /*
Christopher Faulet03599112018-11-27 11:21:21 +0100175 * 1: identify the method and the version. Also set HTTP flags
Christopher Faulete0768eb2018-10-03 16:38:02 +0200176 */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100177 txn->meth = sl->info.req.meth;
Christopher Faulet03599112018-11-27 11:21:21 +0100178 if (sl->flags & HTX_SL_F_VER_11)
Christopher Faulet9768c262018-10-22 09:34:31 +0200179 msg->flags |= HTTP_MSGF_VER_11;
Christopher Faulet03599112018-11-27 11:21:21 +0100180 msg->flags |= HTTP_MSGF_XFER_LEN;
Christopher Faulet2a408542020-11-20 14:22:37 +0100181 if (sl->flags & HTX_SL_F_CLEN)
182 msg->flags |= HTTP_MSGF_CNT_LEN;
183 else if (sl->flags & HTX_SL_F_CHNK)
184 msg->flags |= HTTP_MSGF_TE_CHNK;
Christopher Fauletb2db4fa2018-11-27 16:51:09 +0100185 if (sl->flags & HTX_SL_F_BODYLESS)
186 msg->flags |= HTTP_MSGF_BODYLESS;
Christopher Faulet576c3582021-01-08 15:53:01 +0100187 if (sl->flags & HTX_SL_F_CONN_UPG)
188 msg->flags |= HTTP_MSGF_CONN_UPG;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200189
190 /* we can make use of server redirect on GET and HEAD */
191 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
192 s->flags |= SF_REDIRECTABLE;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100193 else if (txn->meth == HTTP_METH_OTHER && isteqi(htx_sl_req_meth(sl), ist("PRI"))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200194 /* PRI is reserved for the HTTP/2 preface */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200195 goto return_bad_req;
196 }
197
198 /*
Christopher Faulet6072beb2020-02-18 15:34:58 +0100199 * 2: check if the URI matches the monitor_uri. We have to do this for
200 * every request which gets in, because the monitor-uri is defined by
201 * the frontend. If the monitor-uri starts with a '/', the matching is
202 * done against the request's path. Otherwise, the request's uri is
203 * used. It is a workaround to let HTTP/2 health-checks work as
204 * expected.
Christopher Faulete0768eb2018-10-03 16:38:02 +0200205 */
Amaury Denoyelle5a9bd372021-07-06 11:23:10 +0200206 if (unlikely(sess->fe->monitor_uri_len != 0)) {
207 const struct ist monitor_uri = ist2(sess->fe->monitor_uri,
208 sess->fe->monitor_uri_len);
Amaury Denoyellec453f952021-07-06 11:40:12 +0200209 struct http_uri_parser parser = http_uri_parser_init(htx_sl_req_uri(sl));
Christopher Faulete0768eb2018-10-03 16:38:02 +0200210
Amaury Denoyelle5a9bd372021-07-06 11:23:10 +0200211 if ((istptr(monitor_uri)[0] == '/' &&
Amaury Denoyellec453f952021-07-06 11:40:12 +0200212 isteq(http_parse_path(&parser), monitor_uri)) ||
Amaury Denoyelle5a9bd372021-07-06 11:23:10 +0200213 isteq(htx_sl_req_uri(sl), monitor_uri)) {
214 /*
215 * We have found the monitor URI
216 */
217 struct acl_cond *cond;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200218
Amaury Denoyelle5a9bd372021-07-06 11:23:10 +0200219 s->flags |= SF_MONITOR;
220 _HA_ATOMIC_INC(&sess->fe->fe_counters.intercepted_req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200221
Amaury Denoyelle5a9bd372021-07-06 11:23:10 +0200222 /* Check if we want to fail this monitor request or not */
223 list_for_each_entry(cond, &sess->fe->mon_fail_cond, list) {
224 int ret = acl_exec_cond(cond, sess->fe, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200225
Amaury Denoyelle5a9bd372021-07-06 11:23:10 +0200226 ret = acl_pass(ret);
227 if (cond->pol == ACL_COND_UNLESS)
228 ret = !ret;
229
230 if (ret) {
231 /* we fail this request, let's return 503 service unavail */
232 txn->status = 503;
233 if (!(s->flags & SF_ERR_MASK))
234 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
235 goto return_prx_cond;
236 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200237 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200238
Amaury Denoyelle5a9bd372021-07-06 11:23:10 +0200239 /* nothing to fail, let's reply normally */
240 txn->status = 200;
241 if (!(s->flags & SF_ERR_MASK))
242 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
243 goto return_prx_cond;
244 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200245 }
246
247 /*
248 * 3: Maybe we have to copy the original REQURI for the logs ?
249 * Note: we cannot log anymore if the request has been
250 * classified as invalid.
251 */
252 if (unlikely(s->logs.logwait & LW_REQ)) {
253 /* we have a complete HTTP request that we must log */
254 if ((txn->uri = pool_alloc(pool_head_requri)) != NULL) {
Christopher Faulet9768c262018-10-22 09:34:31 +0200255 size_t len;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200256
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200257 len = http_fmt_req_line(sl, txn->uri, global.tune.requri_len - 1);
Christopher Faulet9768c262018-10-22 09:34:31 +0200258 txn->uri[len] = 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200259
260 if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT)))
261 s->do_log(s);
262 } else {
263 ha_alert("HTTP logging : out of memory.\n");
264 }
265 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200266
Christopher Faulete0768eb2018-10-03 16:38:02 +0200267 /* if the frontend has "option http-use-proxy-header", we'll check if
268 * we have what looks like a proxied connection instead of a connection,
269 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
270 * Note that this is *not* RFC-compliant, however browsers and proxies
271 * happen to do that despite being non-standard :-(
272 * We consider that a request not beginning with either '/' or '*' is
273 * a proxied connection, which covers both "scheme://location" and
274 * CONNECT ip:port.
275 */
276 if ((sess->fe->options2 & PR_O2_USE_PXHDR) &&
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100277 *HTX_SL_REQ_UPTR(sl) != '/' && *HTX_SL_REQ_UPTR(sl) != '*')
Christopher Faulete0768eb2018-10-03 16:38:02 +0200278 txn->flags |= TX_USE_PX_CONN;
279
Christopher Faulete0768eb2018-10-03 16:38:02 +0200280 /* 5: we may need to capture headers */
281 if (unlikely((s->logs.logwait & LW_REQHDR) && s->req_cap))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200282 http_capture_headers(htx, s->req_cap, sess->fe->req_cap);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200283
Christopher Faulete0768eb2018-10-03 16:38:02 +0200284 /* we may have to wait for the request's body */
Christopher Faulet9768c262018-10-22 09:34:31 +0200285 if (s->be->options & PR_O_WREQ_BODY)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200286 req->analysers |= AN_REQ_HTTP_BODY;
287
288 /*
289 * RFC7234#4:
290 * A cache MUST write through requests with methods
291 * that are unsafe (Section 4.2.1 of [RFC7231]) to
292 * the origin server; i.e., a cache is not allowed
293 * to generate a reply to such a request before
294 * having forwarded the request and having received
295 * a corresponding response.
296 *
297 * RFC7231#4.2.1:
298 * Of the request methods defined by this
299 * specification, the GET, HEAD, OPTIONS, and TRACE
300 * methods are defined to be safe.
301 */
302 if (likely(txn->meth == HTTP_METH_GET ||
303 txn->meth == HTTP_METH_HEAD ||
304 txn->meth == HTTP_METH_OPTIONS ||
305 txn->meth == HTTP_METH_TRACE))
306 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
307
308 /* end of job, return OK */
309 req->analysers &= ~an_bit;
310 req->analyse_exp = TICK_ETERNITY;
Christopher Faulet9768c262018-10-22 09:34:31 +0200311
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100312 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200313 return 1;
314
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200315 return_int_err:
316 txn->status = 500;
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200317 if (!(s->flags & SF_ERR_MASK))
318 s->flags |= SF_ERR_INTERNAL;
Willy Tarreau4781b152021-04-06 13:53:36 +0200319 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
William Lallemand36119de2021-03-08 15:26:48 +0100320 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200321 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200322 goto return_prx_cond;
323
Christopher Faulete0768eb2018-10-03 16:38:02 +0200324 return_bad_req:
Christopher Faulet9768c262018-10-22 09:34:31 +0200325 txn->status = 400;
Willy Tarreau4781b152021-04-06 13:53:36 +0200326 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req);
William Lallemand36119de2021-03-08 15:26:48 +0100327 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200328 _HA_ATOMIC_INC(&sess->listener->counters->failed_req);
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200329 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200330
331 return_prx_cond:
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200332 http_reply_and_close(s, txn->status, http_error_message(s));
333
Christopher Faulete0768eb2018-10-03 16:38:02 +0200334 if (!(s->flags & SF_ERR_MASK))
335 s->flags |= SF_ERR_PRXCOND;
336 if (!(s->flags & SF_FINST_MASK))
337 s->flags |= SF_FINST_R;
338
339 req->analysers &= AN_REQ_FLT_END;
340 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100341 DBG_TRACE_DEVEL("leaving on error",
342 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200343 return 0;
344}
345
346
347/* This stream analyser runs all HTTP request processing which is common to
348 * frontends and backends, which means blocking ACLs, filters, connection-close,
349 * reqadd, stats and redirects. This is performed for the designated proxy.
350 * It returns 1 if the processing can continue on next analysers, or zero if it
351 * either needs more data or wants to immediately abort the request (eg: deny,
352 * error, ...).
353 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200354int http_process_req_common(struct stream *s, struct channel *req, int an_bit, struct proxy *px)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200355{
356 struct session *sess = s->sess;
357 struct http_txn *txn = s->txn;
358 struct http_msg *msg = &txn->req;
Christopher Fauletff2759f2018-10-24 11:13:16 +0200359 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200360 struct redirect_rule *rule;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200361 enum rule_result verdict;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200362 struct connection *conn = objt_conn(sess->origin);
363
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100364 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200365
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100366 htx = htxbuf(&req->buf);
Christopher Fauletff2759f2018-10-24 11:13:16 +0200367
Christopher Faulet1907ccc2019-04-29 13:12:02 +0200368 /* just in case we have some per-backend tracking. Only called the first
369 * execution of the analyser. */
370 if (!s->current_rule || s->current_rule_list != &px->http_req_rules)
371 stream_inc_be_http_req_ctr(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200372
373 /* evaluate http-request rules */
374 if (!LIST_ISEMPTY(&px->http_req_rules)) {
Christopher Fauletb58f62b2020-01-13 16:40:13 +0100375 verdict = http_req_get_intercept_rule(px, &px->http_req_rules, s);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200376
377 switch (verdict) {
378 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
379 goto return_prx_yield;
380
381 case HTTP_RULE_RES_CONT:
382 case HTTP_RULE_RES_STOP: /* nothing to do */
383 break;
384
385 case HTTP_RULE_RES_DENY: /* deny or tarpit */
386 if (txn->flags & TX_CLTARPIT)
387 goto tarpit;
388 goto deny;
389
390 case HTTP_RULE_RES_ABRT: /* abort request, response already sent. Eg: auth */
391 goto return_prx_cond;
392
393 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
394 goto done;
395
396 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
397 goto return_bad_req;
Christopher Faulet3a26bee2019-12-16 12:47:40 +0100398
399 case HTTP_RULE_RES_ERROR: /* failed with a bad request */
400 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200401 }
402 }
403
404 if (conn && (conn->flags & CO_FL_EARLY_DATA) &&
Olivier Houchard220a26c2020-01-23 14:57:36 +0100405 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_SSL_WAIT_HS))) {
Christopher Fauletff2759f2018-10-24 11:13:16 +0200406 struct http_hdr_ctx ctx;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200407
Christopher Fauletff2759f2018-10-24 11:13:16 +0200408 ctx.blk = NULL;
409 if (!http_find_header(htx, ist("Early-Data"), &ctx, 0)) {
410 if (unlikely(!http_add_header(htx, ist("Early-Data"), ist("1"))))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100411 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200412 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200413 }
414
415 /* OK at this stage, we know that the request was accepted according to
416 * the http-request rules, we can check for the stats. Note that the
417 * URI is detected *before* the req* rules in order not to be affected
418 * by a possible reqrep, while they are processed *after* so that a
419 * reqdeny can still block them. This clearly needs to change in 1.6!
420 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200421 if (!s->target && http_stats_check_uri(s, txn, px)) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200422 s->target = &http_stats_applet.obj_type;
Willy Tarreau14bfe9a2018-12-19 15:19:27 +0100423 if (unlikely(!si_register_handler(&s->si[1], objt_applet(s->target)))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200424 s->logs.tv_request = now;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200425 if (!(s->flags & SF_ERR_MASK))
426 s->flags |= SF_ERR_RESOURCE;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100427 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200428 }
429
430 /* parse the whole stats request and extract the relevant information */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200431 http_handle_stats(s, req);
Christopher Fauletb58f62b2020-01-13 16:40:13 +0100432 verdict = http_req_get_intercept_rule(px, &px->uri_auth->http_req_rules, s);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200433 /* not all actions implemented: deny, allow, auth */
434
435 if (verdict == HTTP_RULE_RES_DENY) /* stats http-request deny */
436 goto deny;
437
438 if (verdict == HTTP_RULE_RES_ABRT) /* stats auth / stats http-request auth */
439 goto return_prx_cond;
Christopher Faulet3a26bee2019-12-16 12:47:40 +0100440
441 if (verdict == HTTP_RULE_RES_BADREQ) /* failed with a bad request */
442 goto return_bad_req;
443
444 if (verdict == HTTP_RULE_RES_ERROR) /* failed with a bad request */
445 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200446 }
447
Christopher Faulet2571bc62019-03-01 11:44:26 +0100448 /* Proceed with the applets now. */
449 if (unlikely(objt_applet(s->target))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200450 if (sess->fe == s->be) /* report it if the request was intercepted by the frontend */
Willy Tarreau4781b152021-04-06 13:53:36 +0200451 _HA_ATOMIC_INC(&sess->fe->fe_counters.intercepted_req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200452
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200453 if (http_handle_expect_hdr(s, htx, msg) == -1)
Christopher Fauletb8a53712019-12-16 11:29:38 +0100454 goto return_int_err;
Christopher Fauletbcf242a2019-03-01 11:36:26 +0100455
Christopher Faulete0768eb2018-10-03 16:38:02 +0200456 if (!(s->flags & SF_ERR_MASK)) // this is not really an error but it is
457 s->flags |= SF_ERR_LOCAL; // to mark that it comes from the proxy
458 if (!(s->flags & SF_FINST_MASK))
459 s->flags |= SF_FINST_R;
460
Christopher Fauletc2ac5e42021-03-08 18:20:09 +0100461 if (HAS_FILTERS(s))
462 req->analysers |= AN_REQ_FLT_HTTP_HDRS;
463
Christopher Faulete0768eb2018-10-03 16:38:02 +0200464 /* enable the minimally required analyzers to handle keep-alive and compression on the HTTP response */
465 req->analysers &= (AN_REQ_HTTP_BODY | AN_REQ_FLT_HTTP_HDRS | AN_REQ_FLT_END);
466 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
467 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Christopher Fauletbcf242a2019-03-01 11:36:26 +0100468
469 req->flags |= CF_SEND_DONTWAIT;
470 s->flags |= SF_ASSIGNED;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200471 goto done;
472 }
473
474 /* check whether we have some ACLs set to redirect this request */
475 list_for_each_entry(rule, &px->redirect_rules, list) {
476 if (rule->cond) {
477 int ret;
478
479 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
480 ret = acl_pass(ret);
481 if (rule->cond->pol == ACL_COND_UNLESS)
482 ret = !ret;
483 if (!ret)
484 continue;
485 }
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200486 if (!http_apply_redirect_rule(rule, s, txn))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100487 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200488 goto done;
489 }
490
491 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
492 * If this happens, then the data will not come immediately, so we must
493 * send all what we have without waiting. Note that due to the small gain
494 * in waiting for the body of the request, it's easier to simply put the
495 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
496 * itself once used.
497 */
498 req->flags |= CF_SEND_DONTWAIT;
499
500 done: /* done with this analyser, continue with next ones that the calling
501 * points will have set, if any.
502 */
503 req->analyse_exp = TICK_ETERNITY;
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +0500504 done_without_exp: /* done with this analyser, but don't reset the analyse_exp. */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200505 req->analysers &= ~an_bit;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100506 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200507 return 1;
508
509 tarpit:
510 /* Allow cookie logging
511 */
512 if (s->be->cookie_name || sess->fe->capture_name)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200513 http_manage_client_side_cookies(s, req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200514
515 /* When a connection is tarpitted, we use the tarpit timeout,
516 * which may be the same as the connect timeout if unspecified.
517 * If unset, then set it to zero because we really want it to
518 * eventually expire. We build the tarpit as an analyser.
519 */
Christopher Faulet202c6ce2019-01-07 14:57:35 +0100520 channel_htx_erase(&s->req, htx);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200521
522 /* wipe the request out so that we can drop the connection early
523 * if the client closes first.
524 */
525 channel_dont_connect(req);
526
Christopher Faulete0768eb2018-10-03 16:38:02 +0200527 req->analysers &= AN_REQ_FLT_END; /* remove switching rules etc... */
528 req->analysers |= AN_REQ_HTTP_TARPIT;
529 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
530 if (!req->analyse_exp)
531 req->analyse_exp = tick_add(now_ms, 0);
532 stream_inc_http_err_ctr(s);
Willy Tarreau4781b152021-04-06 13:53:36 +0200533 _HA_ATOMIC_INC(&sess->fe->fe_counters.denied_req);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100534 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau4781b152021-04-06 13:53:36 +0200535 _HA_ATOMIC_INC(&s->be->be_counters.denied_req);
William Lallemand36119de2021-03-08 15:26:48 +0100536 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200537 _HA_ATOMIC_INC(&sess->listener->counters->denied_req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200538 goto done_without_exp;
539
540 deny: /* this request was blocked (denied) */
541
542 /* Allow cookie logging
543 */
544 if (s->be->cookie_name || sess->fe->capture_name)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200545 http_manage_client_side_cookies(s, req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200546
Christopher Faulete0768eb2018-10-03 16:38:02 +0200547 s->logs.tv_request = now;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200548 stream_inc_http_err_ctr(s);
Willy Tarreau4781b152021-04-06 13:53:36 +0200549 _HA_ATOMIC_INC(&sess->fe->fe_counters.denied_req);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100550 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau4781b152021-04-06 13:53:36 +0200551 _HA_ATOMIC_INC(&s->be->be_counters.denied_req);
William Lallemand36119de2021-03-08 15:26:48 +0100552 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200553 _HA_ATOMIC_INC(&sess->listener->counters->denied_req);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100554 goto return_prx_err;
555
556 return_int_err:
557 txn->status = 500;
558 if (!(s->flags & SF_ERR_MASK))
559 s->flags |= SF_ERR_INTERNAL;
Willy Tarreau4781b152021-04-06 13:53:36 +0200560 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100561 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau4781b152021-04-06 13:53:36 +0200562 _HA_ATOMIC_INC(&s->be->be_counters.internal_errors);
William Lallemand36119de2021-03-08 15:26:48 +0100563 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200564 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100565 goto return_prx_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200566
567 return_bad_req:
Christopher Faulete0768eb2018-10-03 16:38:02 +0200568 txn->status = 400;
Willy Tarreau4781b152021-04-06 13:53:36 +0200569 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req);
William Lallemand36119de2021-03-08 15:26:48 +0100570 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200571 _HA_ATOMIC_INC(&sess->listener->counters->failed_req);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100572 /* fall through */
573
574 return_prx_err:
575 http_reply_and_close(s, txn->status, http_error_message(s));
576 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200577
578 return_prx_cond:
579 if (!(s->flags & SF_ERR_MASK))
580 s->flags |= SF_ERR_PRXCOND;
581 if (!(s->flags & SF_FINST_MASK))
582 s->flags |= SF_FINST_R;
583
584 req->analysers &= AN_REQ_FLT_END;
585 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100586 DBG_TRACE_DEVEL("leaving on error",
587 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200588 return 0;
589
590 return_prx_yield:
591 channel_dont_connect(req);
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100592 DBG_TRACE_DEVEL("waiting for more data",
593 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200594 return 0;
595}
596
597/* This function performs all the processing enabled for the current request.
598 * It returns 1 if the processing can continue on next analysers, or zero if it
599 * needs more data, encounters an error, or wants to immediately abort the
600 * request. It relies on buffers flags, and updates s->req.analysers.
601 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200602int http_process_request(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200603{
604 struct session *sess = s->sess;
605 struct http_txn *txn = s->txn;
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200606 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200607 struct connection *cli_conn = objt_conn(strm_sess(s)->origin);
608
Christopher Faulet8bebd2f2020-10-06 17:54:56 +0200609 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200610
611 /*
612 * Right now, we know that we have processed the entire headers
613 * and that unwanted requests have been filtered out. We can do
614 * whatever we want with the remaining request. Also, now we
615 * may have separate values for ->fe, ->be.
616 */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100617 htx = htxbuf(&req->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200618
619 /*
620 * If HTTP PROXY is set we simply get remote server address parsing
Willy Tarreau1c8d32b2019-07-18 15:47:45 +0200621 * incoming request.
Christopher Faulete0768eb2018-10-03 16:38:02 +0200622 */
623 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SF_ADDR_SET)) {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100624 struct htx_sl *sl;
625 struct ist uri, path;
Amaury Denoyelleb60fb8d2021-07-08 17:27:01 +0200626 struct http_uri_parser parser;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200627
Willy Tarreau9b7587a2020-10-15 07:32:10 +0200628 if (!sockaddr_alloc(&s->target_addr, NULL, 0)) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200629 if (!(s->flags & SF_ERR_MASK))
630 s->flags |= SF_ERR_RESOURCE;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100631 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200632 }
Christopher Faulet297fbb42019-05-13 14:41:27 +0200633 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100634 uri = htx_sl_req_uri(sl);
Amaury Denoyelleb60fb8d2021-07-08 17:27:01 +0200635 parser = http_uri_parser_init(uri);
Amaury Denoyellec453f952021-07-06 11:40:12 +0200636 path = http_parse_path(&parser);
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200637
Willy Tarreau1c8d32b2019-07-18 15:47:45 +0200638 if (url2sa(uri.ptr, uri.len - path.len, s->target_addr, NULL) == -1)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200639 goto return_bad_req;
640
Willy Tarreau1c8d32b2019-07-18 15:47:45 +0200641 s->target = &s->be->obj_type;
642 s->flags |= SF_ADDR_SET | SF_ASSIGNED;
643
Christopher Faulete0768eb2018-10-03 16:38:02 +0200644 /* if the path was found, we have to remove everything between
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200645 * uri.ptr and path.ptr (excluded). If it was not found, we need
646 * to replace from all the uri by a single "/".
647 *
Ilya Shipitsin46a030c2020-07-05 16:36:08 +0500648 * Instead of rewriting the whole start line, we just update
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100649 * the star-line URI. Some space will be lost but it should be
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200650 * insignificant.
Christopher Faulete0768eb2018-10-03 16:38:02 +0200651 */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100652 istcpy(&uri, (path.len ? path : ist("/")), uri.len);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200653 }
654
655 /*
656 * 7: Now we can work with the cookies.
657 * Note that doing so might move headers in the request, but
658 * the fields will stay coherent and the URI will not move.
659 * This should only be performed in the backend.
660 */
661 if (s->be->cookie_name || sess->fe->capture_name)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200662 http_manage_client_side_cookies(s, req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200663
Tim Duesterhus2825b4b2020-02-28 15:13:34 +0100664 /* 8: Generate unique ID if a "unique-id-format" is defined.
665 *
666 * A unique ID is generated even when it is not sent to ensure that the ID can make use of
667 * fetches only available in the HTTP request processing stage.
668 */
669 if (!LIST_ISEMPTY(&sess->fe->format_unique_id)) {
Tim Duesterhusa17e6622020-03-05 20:19:02 +0100670 struct ist unique_id = stream_generate_unique_id(s, &sess->fe->format_unique_id);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200671
Tim Duesterhusa17e6622020-03-05 20:19:02 +0100672 if (!isttest(unique_id)) {
Christopher Fauletb8a53712019-12-16 11:29:38 +0100673 if (!(s->flags & SF_ERR_MASK))
674 s->flags |= SF_ERR_RESOURCE;
675 goto return_int_err;
676 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200677
Tim Duesterhus2825b4b2020-02-28 15:13:34 +0100678 /* send unique ID if a "unique-id-header" is defined */
Tim Duesterhus0643b0e2020-03-05 17:56:35 +0100679 if (isttest(sess->fe->header_unique_id) &&
Tim Duesterhusa17e6622020-03-05 20:19:02 +0100680 unlikely(!http_add_header(htx, sess->fe->header_unique_id, s->unique_id)))
Tim Duesterhus2825b4b2020-02-28 15:13:34 +0100681 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200682 }
683
684 /*
685 * 9: add X-Forwarded-For if either the frontend or the backend
686 * asks for it.
687 */
688 if ((sess->fe->options | s->be->options) & PR_O_FWDFOR) {
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200689 struct http_hdr_ctx ctx = { .blk = NULL };
690 struct ist hdr = ist2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : sess->fe->fwdfor_hdr_name,
691 s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : sess->fe->fwdfor_hdr_len);
692
Christopher Faulete0768eb2018-10-03 16:38:02 +0200693 if (!((sess->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200694 http_find_header(htx, hdr, &ctx, 0)) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200695 /* The header is set to be added only if none is present
696 * and we found it, so don't do anything.
697 */
698 }
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200699 else if (cli_conn && conn_get_src(cli_conn) && cli_conn->src->ss_family == AF_INET) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200700 /* Add an X-Forwarded-For header unless the source IP is
701 * in the 'except' network range.
702 */
Christopher Faulet5d1def62021-02-26 09:19:15 +0100703 if (ipcmp2net(cli_conn->src, &sess->fe->except_xff_net) &&
704 ipcmp2net(cli_conn->src, &s->be->except_xff_net)) {
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200705 unsigned char *pn = (unsigned char *)&((struct sockaddr_in *)cli_conn->src)->sin_addr;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200706
707 /* Note: we rely on the backend to get the header name to be used for
708 * x-forwarded-for, because the header is really meant for the backends.
709 * However, if the backend did not specify any option, we have to rely
710 * on the frontend's header name.
711 */
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200712 chunk_printf(&trash, "%d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
713 if (unlikely(!http_add_header(htx, hdr, ist2(trash.area, trash.data))))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100714 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200715 }
716 }
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200717 else if (cli_conn && conn_get_src(cli_conn) && cli_conn->src->ss_family == AF_INET6) {
Christopher Faulet5d1def62021-02-26 09:19:15 +0100718 /* Add an X-Forwarded-For header unless the source IP is
719 * in the 'except' network range.
Christopher Faulete0768eb2018-10-03 16:38:02 +0200720 */
Christopher Faulet5d1def62021-02-26 09:19:15 +0100721 if (ipcmp2net(cli_conn->src, &sess->fe->except_xff_net) &&
722 ipcmp2net(cli_conn->src, &s->be->except_xff_net)) {
723 char pn[INET6_ADDRSTRLEN];
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200724
Christopher Faulet5d1def62021-02-26 09:19:15 +0100725 inet_ntop(AF_INET6,
726 (const void *)&((struct sockaddr_in6 *)(cli_conn->src))->sin6_addr,
727 pn, sizeof(pn));
Christopher Faulete0768eb2018-10-03 16:38:02 +0200728
Christopher Faulet5d1def62021-02-26 09:19:15 +0100729 /* Note: we rely on the backend to get the header name to be used for
730 * x-forwarded-for, because the header is really meant for the backends.
731 * However, if the backend did not specify any option, we have to rely
732 * on the frontend's header name.
733 */
734 chunk_printf(&trash, "%s", pn);
735 if (unlikely(!http_add_header(htx, hdr, ist2(trash.area, trash.data))))
736 goto return_int_err;
737 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200738 }
739 }
740
741 /*
742 * 10: add X-Original-To if either the frontend or the backend
743 * asks for it.
744 */
745 if ((sess->fe->options | s->be->options) & PR_O_ORGTO) {
Christopher Faulet5d1def62021-02-26 09:19:15 +0100746 struct ist hdr = ist2(s->be->orgto_hdr_len ? s->be->orgto_hdr_name : sess->fe->orgto_hdr_name,
747 s->be->orgto_hdr_len ? s->be->orgto_hdr_len : sess->fe->orgto_hdr_len);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200748
Christopher Fauletcccded92021-02-26 12:45:56 +0100749 if (cli_conn && conn_get_dst(cli_conn) && cli_conn->dst->ss_family == AF_INET) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200750 /* Add an X-Original-To header unless the destination IP is
751 * in the 'except' network range.
752 */
Christopher Faulet5d1def62021-02-26 09:19:15 +0100753 if (ipcmp2net(cli_conn->dst, &sess->fe->except_xot_net) &&
754 ipcmp2net(cli_conn->dst, &s->be->except_xot_net)) {
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200755 unsigned char *pn = (unsigned char *)&((struct sockaddr_in *)cli_conn->dst)->sin_addr;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200756
757 /* Note: we rely on the backend to get the header name to be used for
758 * x-original-to, because the header is really meant for the backends.
759 * However, if the backend did not specify any option, we have to rely
760 * on the frontend's header name.
761 */
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200762 chunk_printf(&trash, "%d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
763 if (unlikely(!http_add_header(htx, hdr, ist2(trash.area, trash.data))))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100764 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200765 }
766 }
Christopher Faulet5d1def62021-02-26 09:19:15 +0100767 else if (cli_conn && conn_get_dst(cli_conn) && cli_conn->dst->ss_family == AF_INET6) {
768 /* Add an X-Original-To header unless the source IP is
769 * in the 'except' network range.
770 */
771 if (ipcmp2net(cli_conn->dst, &sess->fe->except_xot_net) &&
772 ipcmp2net(cli_conn->dst, &s->be->except_xot_net)) {
773 char pn[INET6_ADDRSTRLEN];
774
775 inet_ntop(AF_INET6,
776 (const void *)&((struct sockaddr_in6 *)(cli_conn->dst))->sin6_addr,
777 pn, sizeof(pn));
778
779 /* Note: we rely on the backend to get the header name to be used for
780 * x-forwarded-for, because the header is really meant for the backends.
781 * However, if the backend did not specify any option, we have to rely
782 * on the frontend's header name.
783 */
784 chunk_printf(&trash, "%s", pn);
785 if (unlikely(!http_add_header(htx, hdr, ist2(trash.area, trash.data))))
786 goto return_int_err;
787 }
788 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200789 }
790
Christopher Fauletc2ac5e42021-03-08 18:20:09 +0100791 /* Filter the request headers if there are filters attached to the
792 * stream.
793 */
794 if (HAS_FILTERS(s))
795 req->analysers |= AN_REQ_FLT_HTTP_HDRS;
796
Christopher Faulete0768eb2018-10-03 16:38:02 +0200797 /* If we have no server assigned yet and we're balancing on url_param
798 * with a POST request, we may be interested in checking the body for
799 * that parameter. This will be done in another analyser.
800 */
801 if (!(s->flags & (SF_ASSIGNED|SF_DIRECT)) &&
Willy Tarreau089eaa02019-01-14 15:17:46 +0100802 s->txn->meth == HTTP_METH_POST &&
803 (s->be->lbprm.algo & BE_LB_ALGO) == BE_LB_ALGO_PH) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200804 channel_dont_connect(req);
805 req->analysers |= AN_REQ_HTTP_BODY;
806 }
807
808 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
809 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau1a18b542018-12-11 16:37:42 +0100810
Christopher Faulete0768eb2018-10-03 16:38:02 +0200811 /* We expect some data from the client. Unless we know for sure
812 * we already have a full request, we have to re-enable quick-ack
813 * in case we previously disabled it, otherwise we might cause
814 * the client to delay further data.
815 */
William Lallemand36119de2021-03-08 15:26:48 +0100816 if ((sess->listener && (sess->listener->options & LI_O_NOQUICKACK)) && !(htx->flags & HTX_FL_EOM))
Willy Tarreau1a18b542018-12-11 16:37:42 +0100817 conn_set_quickack(cli_conn, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200818
819 /*************************************************************
820 * OK, that's finished for the headers. We have done what we *
821 * could. Let's switch to the DATA state. *
822 ************************************************************/
823 req->analyse_exp = TICK_ETERNITY;
824 req->analysers &= ~an_bit;
825
826 s->logs.tv_request = now;
827 /* OK let's go on with the BODY now */
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100828 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200829 return 1;
830
Christopher Fauletb8a53712019-12-16 11:29:38 +0100831 return_int_err:
832 txn->status = 500;
833 if (!(s->flags & SF_ERR_MASK))
834 s->flags |= SF_ERR_INTERNAL;
Willy Tarreau4781b152021-04-06 13:53:36 +0200835 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100836 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau4781b152021-04-06 13:53:36 +0200837 _HA_ATOMIC_INC(&s->be->be_counters.internal_errors);
William Lallemand36119de2021-03-08 15:26:48 +0100838 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200839 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100840 goto return_prx_cond;
841
Christopher Faulete0768eb2018-10-03 16:38:02 +0200842 return_bad_req: /* let's centralize all bad requests */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200843 txn->status = 400;
Willy Tarreau4781b152021-04-06 13:53:36 +0200844 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req);
William Lallemand36119de2021-03-08 15:26:48 +0100845 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200846 _HA_ATOMIC_INC(&sess->listener->counters->failed_req);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100847 /* fall through */
848
849 return_prx_cond:
850 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +0200851
852 if (!(s->flags & SF_ERR_MASK))
853 s->flags |= SF_ERR_PRXCOND;
854 if (!(s->flags & SF_FINST_MASK))
855 s->flags |= SF_FINST_R;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100856
857 req->analysers &= AN_REQ_FLT_END;
858 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100859 DBG_TRACE_DEVEL("leaving on error",
860 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200861 return 0;
862}
863
864/* This function is an analyser which processes the HTTP tarpit. It always
865 * returns zero, at the beginning because it prevents any other processing
866 * from occurring, and at the end because it terminates the request.
867 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200868int http_process_tarpit(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200869{
870 struct http_txn *txn = s->txn;
871
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100872 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, &txn->req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200873 /* This connection is being tarpitted. The CLIENT side has
874 * already set the connect expiration date to the right
875 * timeout. We just have to check that the client is still
876 * there and that the timeout has not expired.
877 */
878 channel_dont_connect(req);
879 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100880 !tick_is_expired(req->analyse_exp, now_ms)) {
881 DBG_TRACE_DEVEL("waiting for tarpit timeout expiry",
882 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200883 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100884 }
885
Christopher Faulete0768eb2018-10-03 16:38:02 +0200886
887 /* We will set the queue timer to the time spent, just for
888 * logging purposes. We fake a 500 server error, so that the
889 * attacker will not suspect his connection has been tarpitted.
890 * It will not cause trouble to the logs because we can exclude
891 * the tarpitted connections by filtering on the 'PT' status flags.
892 */
893 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
894
Christopher Faulet8dfeccf2020-05-15 14:16:29 +0200895 http_reply_and_close(s, txn->status, (!(req->flags & CF_READ_ERROR) ? http_error_message(s) : NULL));
Christopher Faulet5cb513a2020-05-13 17:56:56 +0200896
897 end:
Christopher Faulete0768eb2018-10-03 16:38:02 +0200898 req->analysers &= AN_REQ_FLT_END;
899 req->analyse_exp = TICK_ETERNITY;
900
901 if (!(s->flags & SF_ERR_MASK))
902 s->flags |= SF_ERR_PRXCOND;
903 if (!(s->flags & SF_FINST_MASK))
904 s->flags |= SF_FINST_T;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100905
906 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200907 return 0;
908}
909
910/* This function is an analyser which waits for the HTTP request body. It waits
911 * for either the buffer to be full, or the full advertised contents to have
912 * reached the buffer. It must only be called after the standard HTTP request
913 * processing has occurred, because it expects the request to be parsed and will
914 * look for the Expect header. It may send a 100-Continue interim response. It
915 * takes in input any state starting from HTTP_MSG_BODY and leaves with one of
916 * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it
917 * needs to read more data, or 1 once it has completed its analysis.
918 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200919int http_wait_for_request_body(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200920{
921 struct session *sess = s->sess;
922 struct http_txn *txn = s->txn;
923 struct http_msg *msg = &s->txn->req;
924
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100925 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Fauletf76ebe82018-10-24 11:16:22 +0200926
Christopher Fauletf76ebe82018-10-24 11:16:22 +0200927
Christopher Faulet021a8e42021-03-29 10:46:38 +0200928 switch (http_wait_for_msg_body(s, req, s->be->timeout.httpreq, 0)) {
929 case HTTP_RULE_RES_CONT:
930 goto http_end;
931 case HTTP_RULE_RES_YIELD:
932 goto missing_data_or_waiting;
933 case HTTP_RULE_RES_BADREQ:
Willy Tarreau4236f032019-03-05 10:43:32 +0100934 goto return_bad_req;
Christopher Faulet021a8e42021-03-29 10:46:38 +0200935 case HTTP_RULE_RES_ERROR:
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200936 goto return_int_err;
Christopher Faulet021a8e42021-03-29 10:46:38 +0200937 case HTTP_RULE_RES_ABRT:
Christopher Fauletb8a53712019-12-16 11:29:38 +0100938 goto return_prx_cond;
Christopher Faulet021a8e42021-03-29 10:46:38 +0200939 default:
940 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200941 }
942
943 http_end:
944 /* The situation will not evolve, so let's give up on the analysis. */
945 s->logs.tv_request = now; /* update the request timer to reflect full request */
946 req->analysers &= ~an_bit;
947 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100948 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200949 return 1;
950
Christopher Faulet021a8e42021-03-29 10:46:38 +0200951 missing_data_or_waiting:
952 channel_dont_connect(req);
953 DBG_TRACE_DEVEL("waiting for more data",
954 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
955 return 0;
956
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200957 return_int_err:
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200958 txn->status = 500;
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200959 if (!(s->flags & SF_ERR_MASK))
960 s->flags |= SF_ERR_INTERNAL;
Willy Tarreau4781b152021-04-06 13:53:36 +0200961 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100962 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau4781b152021-04-06 13:53:36 +0200963 _HA_ATOMIC_INC(&s->be->be_counters.internal_errors);
William Lallemand36119de2021-03-08 15:26:48 +0100964 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200965 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Faulet021a8e42021-03-29 10:46:38 +0200966 goto return_prx_err;
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200967
Christopher Faulete0768eb2018-10-03 16:38:02 +0200968 return_bad_req: /* let's centralize all bad requests */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200969 txn->status = 400;
Willy Tarreau4781b152021-04-06 13:53:36 +0200970 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req);
William Lallemand36119de2021-03-08 15:26:48 +0100971 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200972 _HA_ATOMIC_INC(&sess->listener->counters->failed_req);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100973 /* fall through */
974
Christopher Faulet021a8e42021-03-29 10:46:38 +0200975 return_prx_err:
Christopher Fauletb8a53712019-12-16 11:29:38 +0100976 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet021a8e42021-03-29 10:46:38 +0200977 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200978
Christopher Faulet021a8e42021-03-29 10:46:38 +0200979 return_prx_cond:
Christopher Faulete0768eb2018-10-03 16:38:02 +0200980 if (!(s->flags & SF_ERR_MASK))
981 s->flags |= SF_ERR_PRXCOND;
982 if (!(s->flags & SF_FINST_MASK))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100983 s->flags |= (msg->msg_state < HTTP_MSG_DATA ? SF_FINST_R : SF_FINST_D);
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200984
Christopher Faulete0768eb2018-10-03 16:38:02 +0200985 req->analysers &= AN_REQ_FLT_END;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100986 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100987 DBG_TRACE_DEVEL("leaving on error",
988 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200989 return 0;
990}
991
992/* This function is an analyser which forwards request body (including chunk
993 * sizes if any). It is called as soon as we must forward, even if we forward
994 * zero byte. The only situation where it must not be called is when we're in
995 * tunnel mode and we want to forward till the close. It's used both to forward
996 * remaining data and to resync after end of body. It expects the msg_state to
997 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
998 * read more data, or 1 once we can go on with next request or end the stream.
999 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
1000 * bytes of pending data + the headers if not already done.
1001 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001002int http_request_forward_body(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001003{
1004 struct session *sess = s->sess;
1005 struct http_txn *txn = s->txn;
Christopher Faulet9768c262018-10-22 09:34:31 +02001006 struct http_msg *msg = &txn->req;
1007 struct htx *htx;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001008 short status = 0;
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001009 int ret;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001010
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001011 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001012
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001013 htx = htxbuf(&req->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001014
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001015 if (htx->flags & HTX_FL_PARSING_ERROR)
1016 goto return_bad_req;
1017 if (htx->flags & HTX_FL_PROCESSING_ERROR)
1018 goto return_int_err;
1019
Christopher Faulete0768eb2018-10-03 16:38:02 +02001020 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
1021 ((req->flags & CF_SHUTW) && (req->to_forward || co_data(req)))) {
1022 /* Output closed while we were sending data. We must abort and
1023 * wake the other side up.
Christopher Fauletf506d962021-04-27 10:56:28 +02001024 *
1025 * If we have finished to send the request and the response is
1026 * still in progress, don't catch write error on the request
1027 * side if it is in fact a read error on the server side.
Christopher Faulete0768eb2018-10-03 16:38:02 +02001028 */
Christopher Fauletf506d962021-04-27 10:56:28 +02001029 if (msg->msg_state == HTTP_MSG_DONE && (s->res.flags & CF_READ_ERROR) && s->res.analysers)
1030 return 0;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001031
Olivier Houchard29cac3c2019-07-12 15:48:58 +02001032 /* Don't abort yet if we had L7 retries activated and it
1033 * was a write error, we may recover.
1034 */
1035 if (!(req->flags & (CF_READ_ERROR | CF_READ_TIMEOUT)) &&
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001036 (s->si[1].flags & SI_FL_L7_RETRY)) {
1037 DBG_TRACE_DEVEL("leaving on L7 retry",
1038 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Olivier Houchard29cac3c2019-07-12 15:48:58 +02001039 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001040 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001041 msg->msg_state = HTTP_MSG_ERROR;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001042 http_end_request(s);
1043 http_end_response(s);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001044 DBG_TRACE_DEVEL("leaving on error",
1045 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001046 return 1;
1047 }
1048
1049 /* Note that we don't have to send 100-continue back because we don't
1050 * need the data to complete our job, and it's up to the server to
1051 * decide whether to return 100, 417 or anything else in return of
1052 * an "Expect: 100-continue" header.
1053 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001054 if (msg->msg_state == HTTP_MSG_BODY)
1055 msg->msg_state = HTTP_MSG_DATA;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001056
Christopher Faulete0768eb2018-10-03 16:38:02 +02001057 /* in most states, we should abort in case of early close */
1058 channel_auto_close(req);
1059
1060 if (req->to_forward) {
Christopher Faulet66af0b22019-03-22 14:54:52 +01001061 if (req->to_forward == CHN_INFINITE_FORWARD) {
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001062 if (req->flags & CF_EOI)
1063 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet66af0b22019-03-22 14:54:52 +01001064 }
1065 else {
1066 /* We can't process the buffer's contents yet */
1067 req->flags |= CF_WAKE_WRITE;
1068 goto missing_data_or_waiting;
1069 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001070 }
1071
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001072 if (msg->msg_state >= HTTP_MSG_ENDING)
1073 goto ending;
1074
1075 if (txn->meth == HTTP_METH_CONNECT) {
1076 msg->msg_state = HTTP_MSG_ENDING;
1077 goto ending;
1078 }
1079
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001080 /* Forward input data. We get it by removing all outgoing data not
1081 * forwarded yet from HTX data size. If there are some data filters, we
1082 * let them decide the amount of data to forward.
Christopher Faulet9768c262018-10-22 09:34:31 +02001083 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001084 if (HAS_REQ_DATA_FILTERS(s)) {
1085 ret = flt_http_payload(s, msg, htx->data);
1086 if (ret < 0)
1087 goto return_bad_req;
Christopher Faulet421e7692019-06-13 11:16:45 +02001088 c_adv(req, ret);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001089 }
1090 else {
Christopher Faulet421e7692019-06-13 11:16:45 +02001091 c_adv(req, htx->data - co_data(req));
Christopher Faulet66af0b22019-03-22 14:54:52 +01001092 if (msg->flags & HTTP_MSGF_XFER_LEN)
1093 channel_htx_forward_forever(req, htx);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001094 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001095
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001096 if (htx->data != co_data(req))
1097 goto missing_data_or_waiting;
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001098
Christopher Faulet9768c262018-10-22 09:34:31 +02001099 /* Check if the end-of-message is reached and if so, switch the message
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001100 * in HTTP_MSG_ENDING state. Then if all data was marked to be
1101 * forwarded, set the state to HTTP_MSG_DONE.
Christopher Faulet9768c262018-10-22 09:34:31 +02001102 */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001103 if (!(htx->flags & HTX_FL_EOM))
Christopher Faulet9768c262018-10-22 09:34:31 +02001104 goto missing_data_or_waiting;
1105
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001106 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet9768c262018-10-22 09:34:31 +02001107
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001108 ending:
Christopher Faulet2151cdd2020-07-22 16:34:59 +02001109 req->flags &= ~CF_EXPECT_MORE; /* no more data are expected */
1110
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001111 /* other states, ENDING...TUNNEL */
1112 if (msg->msg_state >= HTTP_MSG_DONE)
1113 goto done;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001114
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001115 if (HAS_REQ_DATA_FILTERS(s)) {
1116 ret = flt_http_end(s, msg);
1117 if (ret <= 0) {
1118 if (!ret)
1119 goto missing_data_or_waiting;
1120 goto return_bad_req;
1121 }
1122 }
1123
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001124 if (txn->meth == HTTP_METH_CONNECT)
1125 msg->msg_state = HTTP_MSG_TUNNEL;
1126 else {
1127 msg->msg_state = HTTP_MSG_DONE;
1128 req->to_forward = 0;
1129 }
1130
1131 done:
1132 /* we don't want to forward closes on DONE except in tunnel mode. */
1133 if (!(txn->flags & TX_CON_WANT_TUN))
1134 channel_dont_close(req);
1135
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001136 http_end_request(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001137 if (!(req->analysers & an_bit)) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001138 http_end_response(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001139 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
1140 if (req->flags & CF_SHUTW) {
1141 /* request errors are most likely due to the
1142 * server aborting the transfer. */
Christopher Faulet93e02d82019-03-08 14:18:50 +01001143 goto return_srv_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001144 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001145 goto return_bad_req;
1146 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001147 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001148 return 1;
1149 }
1150
1151 /* If "option abortonclose" is set on the backend, we want to monitor
1152 * the client's connection and forward any shutdown notification to the
1153 * server, which will decide whether to close or to go on processing the
1154 * request. We only do that in tunnel mode, and not in other modes since
1155 * it can be abused to exhaust source ports. */
Christopher Faulet769d0e92019-03-22 14:23:18 +01001156 if (s->be->options & PR_O_ABRT_CLOSE) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001157 channel_auto_read(req);
Christopher Fauletc41547b2019-07-16 14:32:23 +02001158 if ((req->flags & (CF_SHUTR|CF_READ_NULL)) && !(txn->flags & TX_CON_WANT_TUN))
Christopher Faulete0768eb2018-10-03 16:38:02 +02001159 s->si[1].flags |= SI_FL_NOLINGER;
1160 channel_auto_close(req);
1161 }
1162 else if (s->txn->meth == HTTP_METH_POST) {
1163 /* POST requests may require to read extra CRLF sent by broken
1164 * browsers and which could cause an RST to be sent upon close
1165 * on some systems (eg: Linux). */
1166 channel_auto_read(req);
1167 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001168 DBG_TRACE_DEVEL("waiting for the end of the HTTP txn",
1169 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001170 return 0;
1171
1172 missing_data_or_waiting:
1173 /* stop waiting for data if the input is closed before the end */
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001174 if (msg->msg_state < HTTP_MSG_ENDING && req->flags & CF_SHUTR)
Christopher Faulet93e02d82019-03-08 14:18:50 +01001175 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001176
1177 waiting:
1178 /* waiting for the last bits to leave the buffer */
1179 if (req->flags & CF_SHUTW)
Christopher Faulet93e02d82019-03-08 14:18:50 +01001180 goto return_srv_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001181
1182 /* When TE: chunked is used, we need to get there again to parse remaining
1183 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
1184 * And when content-length is used, we never want to let the possible
1185 * shutdown be forwarded to the other side, as the state machine will
1186 * take care of it once the client responds. It's also important to
1187 * prevent TIME_WAITs from accumulating on the backend side, and for
1188 * HTTP/2 where the last frame comes with a shutdown.
1189 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001190 if (msg->flags & HTTP_MSGF_XFER_LEN)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001191 channel_dont_close(req);
1192
1193 /* We know that more data are expected, but we couldn't send more that
1194 * what we did. So we always set the CF_EXPECT_MORE flag so that the
1195 * system knows it must not set a PUSH on this first part. Interactive
1196 * modes are already handled by the stream sock layer. We must not do
1197 * this in content-length mode because it could present the MSG_MORE
1198 * flag with the last block of forwarded data, which would cause an
1199 * additional delay to be observed by the receiver.
1200 */
Christopher Faulet2151cdd2020-07-22 16:34:59 +02001201 if (HAS_REQ_DATA_FILTERS(s))
Christopher Faulete0768eb2018-10-03 16:38:02 +02001202 req->flags |= CF_EXPECT_MORE;
1203
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001204 DBG_TRACE_DEVEL("waiting for more data to forward",
1205 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001206 return 0;
1207
Christopher Faulet93e02d82019-03-08 14:18:50 +01001208 return_cli_abort:
Willy Tarreau4781b152021-04-06 13:53:36 +02001209 _HA_ATOMIC_INC(&sess->fe->fe_counters.cli_aborts);
1210 _HA_ATOMIC_INC(&s->be->be_counters.cli_aborts);
William Lallemand36119de2021-03-08 15:26:48 +01001211 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001212 _HA_ATOMIC_INC(&sess->listener->counters->cli_aborts);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001213 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001214 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.cli_aborts);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001215 if (!(s->flags & SF_ERR_MASK))
1216 s->flags |= SF_ERR_CLICL;
1217 status = 400;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001218 goto return_prx_cond;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001219
1220 return_srv_abort:
Willy Tarreau4781b152021-04-06 13:53:36 +02001221 _HA_ATOMIC_INC(&sess->fe->fe_counters.srv_aborts);
1222 _HA_ATOMIC_INC(&s->be->be_counters.srv_aborts);
William Lallemand36119de2021-03-08 15:26:48 +01001223 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001224 _HA_ATOMIC_INC(&sess->listener->counters->srv_aborts);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001225 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001226 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.srv_aborts);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001227 if (!(s->flags & SF_ERR_MASK))
1228 s->flags |= SF_ERR_SRVCL;
1229 status = 502;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001230 goto return_prx_cond;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001231
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001232 return_int_err:
1233 if (!(s->flags & SF_ERR_MASK))
1234 s->flags |= SF_ERR_INTERNAL;
Willy Tarreau4781b152021-04-06 13:53:36 +02001235 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
1236 _HA_ATOMIC_INC(&s->be->be_counters.internal_errors);
William Lallemand36119de2021-03-08 15:26:48 +01001237 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001238 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001239 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001240 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.internal_errors);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001241 status = 500;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001242 goto return_prx_cond;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001243
Christopher Faulet93e02d82019-03-08 14:18:50 +01001244 return_bad_req:
Willy Tarreau4781b152021-04-06 13:53:36 +02001245 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req);
William Lallemand36119de2021-03-08 15:26:48 +01001246 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001247 _HA_ATOMIC_INC(&sess->listener->counters->failed_req);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001248 status = 400;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001249 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001250
Christopher Fauletb8a53712019-12-16 11:29:38 +01001251 return_prx_cond:
Christopher Faulet9768c262018-10-22 09:34:31 +02001252 if (txn->status > 0) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001253 /* Note: we don't send any error if some data were already sent */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001254 http_reply_and_close(s, txn->status, NULL);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001255 } else {
Christopher Faulet93e02d82019-03-08 14:18:50 +01001256 txn->status = status;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001257 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001258 }
1259 req->analysers &= AN_REQ_FLT_END;
1260 s->res.analysers &= AN_RES_FLT_END; /* we're in data phase, we want to abort both directions */
Christopher Fauletb8a53712019-12-16 11:29:38 +01001261 if (!(s->flags & SF_ERR_MASK))
1262 s->flags |= SF_ERR_PRXCOND;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001263 if (!(s->flags & SF_FINST_MASK))
1264 s->flags |= ((txn->rsp.msg_state < HTTP_MSG_ERROR) ? SF_FINST_H : SF_FINST_D);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001265 DBG_TRACE_DEVEL("leaving on error ",
1266 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001267 return 0;
1268}
1269
Olivier Houcharda254a372019-04-05 15:30:12 +02001270/* Reset the stream and the backend stream_interface to a situation suitable for attemption connection */
1271/* Returns 0 if we can attempt to retry, -1 otherwise */
1272static __inline int do_l7_retry(struct stream *s, struct stream_interface *si)
1273{
Christopher Faulet9f5382e2021-05-21 13:46:14 +02001274 struct channel *req, *res;
1275 int co_data;
Olivier Houcharda254a372019-04-05 15:30:12 +02001276
1277 si->conn_retries--;
1278 if (si->conn_retries < 0)
Christopher Faulet552601d2021-05-26 10:31:06 +02001279 return -1;
Christopher Faulet5b82cc52020-10-12 15:18:50 +02001280
Christopher Faulete763c8c2021-05-05 18:23:59 +02001281 if (objt_server(s->target)) {
1282 if (s->flags & SF_CURR_SESS) {
1283 s->flags &= ~SF_CURR_SESS;
1284 _HA_ATOMIC_DEC(&__objt_server(s->target)->cur_sess);
1285 }
Willy Tarreau4781b152021-04-06 13:53:36 +02001286 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.retries);
Christopher Faulete763c8c2021-05-05 18:23:59 +02001287 }
Willy Tarreau4781b152021-04-06 13:53:36 +02001288 _HA_ATOMIC_INC(&s->be->be_counters.retries);
Willy Tarreau223995e2019-05-04 10:38:31 +02001289
Christopher Faulet9f5382e2021-05-21 13:46:14 +02001290 req = &s->req;
1291 res = &s->res;
Olivier Houcharda254a372019-04-05 15:30:12 +02001292 /* Remove any write error from the request, and read error from the response */
1293 req->flags &= ~(CF_WRITE_ERROR | CF_WRITE_TIMEOUT | CF_SHUTW | CF_SHUTW_NOW);
1294 res->flags &= ~(CF_READ_ERROR | CF_READ_TIMEOUT | CF_SHUTR | CF_EOI | CF_READ_NULL | CF_SHUTR_NOW);
1295 res->analysers = 0;
1296 si->flags &= ~(SI_FL_ERR | SI_FL_EXP | SI_FL_RXBLK_SHUT);
Christopher Faulet30aa0da2021-05-05 21:05:09 +02001297 si->err_type = SI_ET_NONE;
1298 s->flags &= ~(SF_ERR_MASK | SF_FINST_MASK);
Olivier Houchard4bd58672019-07-12 16:16:59 +02001299 stream_choose_redispatch(s);
Olivier Houcharda254a372019-04-05 15:30:12 +02001300 si->exp = TICK_ETERNITY;
1301 res->rex = TICK_ETERNITY;
1302 res->to_forward = 0;
1303 res->analyse_exp = TICK_ETERNITY;
1304 res->total = 0;
Olivier Houcharda254a372019-04-05 15:30:12 +02001305 si_release_endpoint(&s->si[1]);
Olivier Houcharda254a372019-04-05 15:30:12 +02001306
Christopher Faulet9f5382e2021-05-21 13:46:14 +02001307 b_free(&req->buf);
1308 /* Swap the L7 buffer with the channel buffer */
1309 /* We know we stored the co_data as b_data, so get it there */
1310 co_data = b_data(&si->l7_buffer);
1311 b_set_data(&si->l7_buffer, b_size(&si->l7_buffer));
1312 b_xfer(&req->buf, &si->l7_buffer, b_data(&si->l7_buffer));
1313 co_set_data(req, co_data);
Christopher Faulet5b82cc52020-10-12 15:18:50 +02001314
Ilya Shipitsinacf84592021-02-06 22:29:08 +05001315 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 +02001316
Olivier Houcharda254a372019-04-05 15:30:12 +02001317 b_reset(&res->buf);
1318 co_set_data(res, 0);
1319 return 0;
1320}
1321
Christopher Faulete0768eb2018-10-03 16:38:02 +02001322/* This stream analyser waits for a complete HTTP response. It returns 1 if the
1323 * processing can continue on next analysers, or zero if it either needs more
1324 * data or wants to immediately abort the response (eg: timeout, error, ...). It
1325 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->res.analysers
1326 * when it has nothing left to do, and may remove any analyser when it wants to
1327 * abort.
1328 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001329int http_wait_for_response(struct stream *s, struct channel *rep, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001330{
Christopher Faulet9768c262018-10-22 09:34:31 +02001331 /*
1332 * We will analyze a complete HTTP response to check the its syntax.
1333 *
1334 * Once the start line and all headers are received, we may perform a
1335 * capture of the error (if any), and we will set a few fields. We also
1336 * logging and finally headers capture.
1337 */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001338 struct session *sess = s->sess;
1339 struct http_txn *txn = s->txn;
1340 struct http_msg *msg = &txn->rsp;
Christopher Faulet9768c262018-10-22 09:34:31 +02001341 struct htx *htx;
Olivier Houcharda254a372019-04-05 15:30:12 +02001342 struct stream_interface *si_b = &s->si[1];
Christopher Faulet61608322018-11-23 16:23:45 +01001343 struct connection *srv_conn;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001344 struct htx_sl *sl;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001345 int n;
1346
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001347 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001348
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001349 htx = htxbuf(&rep->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001350
Willy Tarreau4236f032019-03-05 10:43:32 +01001351 /* Parsing errors are caught here */
1352 if (htx->flags & HTX_FL_PARSING_ERROR)
1353 goto return_bad_res;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001354 if (htx->flags & HTX_FL_PROCESSING_ERROR)
1355 goto return_int_err;
Willy Tarreau4236f032019-03-05 10:43:32 +01001356
Christopher Faulete0768eb2018-10-03 16:38:02 +02001357 /*
1358 * Now we quickly check if we have found a full valid response.
1359 * If not so, we check the FD and buffer states before leaving.
1360 * A full response is indicated by the fact that we have seen
1361 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
1362 * responses are checked first.
1363 *
1364 * Depending on whether the client is still there or not, we
1365 * may send an error response back or not. Note that normally
1366 * we should only check for HTTP status there, and check I/O
1367 * errors somewhere else.
1368 */
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001369 next_one:
Christopher Faulet29f17582019-05-23 11:03:26 +02001370 if (unlikely(htx_is_empty(htx) || htx->first == -1)) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001371 /* 1: have we encountered a read error ? */
1372 if (rep->flags & CF_READ_ERROR) {
Olivier Houchard865d8392019-05-03 22:46:27 +02001373 struct connection *conn = NULL;
1374
Olivier Houchard865d8392019-05-03 22:46:27 +02001375 if (objt_cs(s->si[1].end))
1376 conn = objt_cs(s->si[1].end)->conn;
1377
Christopher Fauletd9769232021-05-26 12:15:37 +02001378 /* Perform a L7 retry because server refuses the early data. */
1379 if ((si_b->flags & SI_FL_L7_RETRY) &&
1380 (s->be->retry_type & PR_RE_EARLY_ERROR) &&
1381 conn && conn->err_code == CO_ER_SSL_EARLY_FAILED &&
1382 do_l7_retry(s, si_b) == 0) {
1383 DBG_TRACE_DEVEL("leaving on L7 retry",
1384 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
1385 return 0;
1386 }
1387
Olivier Houchard6db16992019-05-17 15:40:49 +02001388 if (txn->flags & TX_NOT_FIRST)
1389 goto abort_keep_alive;
1390
Willy Tarreau4781b152021-04-06 13:53:36 +02001391 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001392 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001393 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001394 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001395 }
1396
Christopher Fauletd9769232021-05-26 12:15:37 +02001397 /* if the server refused the early data, just send a 425 */
1398 if (conn && conn->err_code == CO_ER_SSL_EARLY_FAILED)
Olivier Houchard865d8392019-05-03 22:46:27 +02001399 txn->status = 425;
Christopher Fauletd9769232021-05-26 12:15:37 +02001400 else {
1401 txn->status = 502;
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001402 stream_inc_http_fail_ctr(s);
Christopher Fauletd9769232021-05-26 12:15:37 +02001403 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001404
Christopher Fauletd9769232021-05-26 12:15:37 +02001405 rep->analysers &= AN_RES_FLT_END;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001406 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001407 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001408
1409 if (!(s->flags & SF_ERR_MASK))
1410 s->flags |= SF_ERR_SRVCL;
1411 if (!(s->flags & SF_FINST_MASK))
1412 s->flags |= SF_FINST_H;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001413 DBG_TRACE_DEVEL("leaving on error",
1414 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001415 return 0;
1416 }
1417
Christopher Faulet9768c262018-10-22 09:34:31 +02001418 /* 2: read timeout : return a 504 to the client. */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001419 else if (rep->flags & CF_READ_TIMEOUT) {
Olivier Houcharda254a372019-04-05 15:30:12 +02001420 if ((si_b->flags & SI_FL_L7_RETRY) &&
1421 (s->be->retry_type & PR_RE_TIMEOUT)) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001422 if (co_data(rep) || do_l7_retry(s, si_b) == 0) {
1423 DBG_TRACE_DEVEL("leaving on L7 retry",
1424 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharda254a372019-04-05 15:30:12 +02001425 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001426 }
Olivier Houcharda254a372019-04-05 15:30:12 +02001427 }
Willy Tarreau4781b152021-04-06 13:53:36 +02001428 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001429 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001430 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001431 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001432 }
1433
Christopher Faulete0768eb2018-10-03 16:38:02 +02001434 rep->analysers &= AN_RES_FLT_END;
1435 txn->status = 504;
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_SRVTO;
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 /* 3: client abort with an abortonclose */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001450 else if ((rep->flags & CF_SHUTR) && ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001451 _HA_ATOMIC_INC(&sess->fe->fe_counters.cli_aborts);
1452 _HA_ATOMIC_INC(&s->be->be_counters.cli_aborts);
William Lallemand36119de2021-03-08 15:26:48 +01001453 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001454 _HA_ATOMIC_INC(&sess->listener->counters->cli_aborts);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001455 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001456 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.cli_aborts);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001457
1458 rep->analysers &= AN_RES_FLT_END;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001459 txn->status = 400;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001460 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001461
1462 if (!(s->flags & SF_ERR_MASK))
1463 s->flags |= SF_ERR_CLICL;
1464 if (!(s->flags & SF_FINST_MASK))
1465 s->flags |= SF_FINST_H;
1466
1467 /* process_stream() will take care of the error */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001468 DBG_TRACE_DEVEL("leaving on error",
1469 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001470 return 0;
1471 }
1472
Christopher Faulet9768c262018-10-22 09:34:31 +02001473 /* 4: close from server, capture the response if the server has started to respond */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001474 else if (rep->flags & CF_SHUTR) {
Olivier Houcharda254a372019-04-05 15:30:12 +02001475 if ((si_b->flags & SI_FL_L7_RETRY) &&
1476 (s->be->retry_type & PR_RE_DISCONNECTED)) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001477 if (co_data(rep) || do_l7_retry(s, si_b) == 0) {
1478 DBG_TRACE_DEVEL("leaving on L7 retry",
1479 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharda254a372019-04-05 15:30:12 +02001480 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001481 }
Olivier Houcharda254a372019-04-05 15:30:12 +02001482 }
1483
Olivier Houchard6db16992019-05-17 15:40:49 +02001484 if (txn->flags & TX_NOT_FIRST)
1485 goto abort_keep_alive;
1486
Willy Tarreau4781b152021-04-06 13:53:36 +02001487 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001488 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001489 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001490 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001491 }
1492
Christopher Faulete0768eb2018-10-03 16:38:02 +02001493 rep->analysers &= AN_RES_FLT_END;
1494 txn->status = 502;
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001495 stream_inc_http_fail_ctr(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001496 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001497 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001498
1499 if (!(s->flags & SF_ERR_MASK))
1500 s->flags |= SF_ERR_SRVCL;
1501 if (!(s->flags & SF_FINST_MASK))
1502 s->flags |= SF_FINST_H;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001503 DBG_TRACE_DEVEL("leaving on error",
1504 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001505 return 0;
1506 }
1507
Christopher Faulet9768c262018-10-22 09:34:31 +02001508 /* 5: write error to client (we don't send any message then) */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001509 else if (rep->flags & CF_WRITE_ERROR) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001510 if (txn->flags & TX_NOT_FIRST)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001511 goto abort_keep_alive;
1512
Willy Tarreau4781b152021-04-06 13:53:36 +02001513 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001514 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001515 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001516 rep->analysers &= AN_RES_FLT_END;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001517
1518 if (!(s->flags & SF_ERR_MASK))
1519 s->flags |= SF_ERR_CLICL;
1520 if (!(s->flags & SF_FINST_MASK))
1521 s->flags |= SF_FINST_H;
1522
1523 /* process_stream() will take care of the error */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001524 DBG_TRACE_DEVEL("leaving on error",
1525 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001526 return 0;
1527 }
1528
1529 channel_dont_close(rep);
1530 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001531 DBG_TRACE_DEVEL("waiting for more data",
1532 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001533 return 0;
1534 }
1535
1536 /* More interesting part now : we know that we have a complete
1537 * response which at least looks like HTTP. We have an indicator
1538 * of each header's length, so we can parse them quickly.
1539 */
Christopher Faulet29f17582019-05-23 11:03:26 +02001540 BUG_ON(htx_get_first_type(htx) != HTX_BLK_RES_SL);
Christopher Faulet297fbb42019-05-13 14:41:27 +02001541 sl = http_get_stline(htx);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001542
Christopher Faulet1f08bff2021-05-26 13:14:39 +02001543 /* Perform a L7 retry because of the status code */
1544 if ((si_b->flags & SI_FL_L7_RETRY) &&
1545 l7_status_match(s->be, sl->info.res.status) &&
1546 do_l7_retry(s, si_b) == 0) {
1547 DBG_TRACE_DEVEL("leaving on L7 retry", STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
1548 return 0;
1549 }
1550
1551 /* Now, L7 buffer is useless, it can be released */
1552 b_free(&s->si[1].l7_buffer);
1553
1554 msg->msg_state = HTTP_MSG_BODY;
1555
1556
Christopher Faulet9768c262018-10-22 09:34:31 +02001557 /* 0: we might have to print this header in debug mode */
1558 if (unlikely((global.mode & MODE_DEBUG) &&
1559 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
1560 int32_t pos;
1561
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001562 http_debug_stline("srvrep", s, sl);
Christopher Faulet9768c262018-10-22 09:34:31 +02001563
Christopher Fauleta3f15502019-05-13 15:27:23 +02001564 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001565 struct htx_blk *blk = htx_get_blk(htx, pos);
1566 enum htx_blk_type type = htx_get_blk_type(blk);
1567
1568 if (type == HTX_BLK_EOH)
1569 break;
1570 if (type != HTX_BLK_HDR)
1571 continue;
1572
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001573 http_debug_hdr("srvhdr", s,
1574 htx_get_blk_name(htx, blk),
1575 htx_get_blk_value(htx, blk));
Christopher Faulet9768c262018-10-22 09:34:31 +02001576 }
1577 }
1578
Christopher Faulet03599112018-11-27 11:21:21 +01001579 /* 1: get the status code and the version. Also set HTTP flags */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001580 txn->status = sl->info.res.status;
Christopher Faulet03599112018-11-27 11:21:21 +01001581 if (sl->flags & HTX_SL_F_VER_11)
Christopher Faulet9768c262018-10-22 09:34:31 +02001582 msg->flags |= HTTP_MSGF_VER_11;
Christopher Faulet03599112018-11-27 11:21:21 +01001583 if (sl->flags & HTX_SL_F_XFER_LEN) {
1584 msg->flags |= HTTP_MSGF_XFER_LEN;
Christopher Faulet2a408542020-11-20 14:22:37 +01001585 if (sl->flags & HTX_SL_F_CLEN)
1586 msg->flags |= HTTP_MSGF_CNT_LEN;
1587 else if (sl->flags & HTX_SL_F_CHNK)
1588 msg->flags |= HTTP_MSGF_TE_CHNK;
Christopher Faulet03599112018-11-27 11:21:21 +01001589 }
Christopher Faulet2a408542020-11-20 14:22:37 +01001590 if (sl->flags & HTX_SL_F_BODYLESS)
1591 msg->flags |= HTTP_MSGF_BODYLESS;
Christopher Faulet576c3582021-01-08 15:53:01 +01001592 if (sl->flags & HTX_SL_F_CONN_UPG)
1593 msg->flags |= HTTP_MSGF_CONN_UPG;
Christopher Faulet9768c262018-10-22 09:34:31 +02001594
1595 n = txn->status / 100;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001596 if (n < 1 || n > 5)
1597 n = 0;
Christopher Faulet9768c262018-10-22 09:34:31 +02001598
Christopher Faulete0768eb2018-10-03 16:38:02 +02001599 /* when the client triggers a 4xx from the server, it's most often due
1600 * to a missing object or permission. These events should be tracked
1601 * because if they happen often, it may indicate a brute force or a
1602 * vulnerability scan.
1603 */
1604 if (n == 4)
1605 stream_inc_http_err_ctr(s);
1606
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001607 if (n == 5 && txn->status != 501 && txn->status != 505)
1608 stream_inc_http_fail_ctr(s);
1609
Marcin Deranek3c27dda2020-05-15 18:32:51 +02001610 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001611 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.p.http.rsp[n]);
1612 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.p.http.cum_req);
Marcin Deranek3c27dda2020-05-15 18:32:51 +02001613 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001614
Christopher Faulete0768eb2018-10-03 16:38:02 +02001615 /* Adjust server's health based on status code. Note: status codes 501
1616 * and 505 are triggered on demand by client request, so we must not
1617 * count them as server failures.
1618 */
1619 if (objt_server(s->target)) {
1620 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001621 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_OK);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001622 else
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001623 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_STS);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001624 }
1625
1626 /*
1627 * We may be facing a 100-continue response, or any other informational
1628 * 1xx response which is non-final, in which case this is not the right
1629 * response, and we're waiting for the next one. Let's allow this response
1630 * to go to the client and wait for the next one. There's an exception for
1631 * 101 which is used later in the code to switch protocols.
1632 */
1633 if (txn->status < 200 &&
1634 (txn->status == 100 || txn->status >= 102)) {
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001635 FLT_STRM_CB(s, flt_http_reset(s, msg));
Christopher Faulet421e7692019-06-13 11:16:45 +02001636 htx->first = channel_htx_fwd_headers(rep, htx);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001637 msg->msg_state = HTTP_MSG_RPBEFORE;
Christopher Faulet3499f622019-09-03 15:23:54 +02001638 msg->flags = 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001639 txn->status = 0;
1640 s->logs.t_data = -1; /* was not a response yet */
Christopher Faulet7d518452020-08-31 11:07:07 +02001641 rep->flags |= CF_SEND_DONTWAIT; /* Send ASAP informational messages */
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001642 goto next_one;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001643 }
1644
Christopher Faulet6e6c7b12021-01-08 16:02:05 +01001645 /* A 101-switching-protocols must contains a Connection header with the
1646 * "upgrade" option and the request too. It means both are agree to
1647 * upgrade. It is not so strict because there is no test on the Upgrade
1648 * header content. But it is probably stronger enough for now.
1649 */
1650 if (txn->status == 101 &&
1651 (!(txn->req.flags & HTTP_MSGF_CONN_UPG) || !(txn->rsp.flags & HTTP_MSGF_CONN_UPG)))
1652 goto return_bad_res;
1653
Christopher Faulete0768eb2018-10-03 16:38:02 +02001654 /*
1655 * 2: check for cacheability.
1656 */
1657
1658 switch (txn->status) {
1659 case 200:
1660 case 203:
1661 case 204:
1662 case 206:
1663 case 300:
1664 case 301:
1665 case 404:
1666 case 405:
1667 case 410:
1668 case 414:
1669 case 501:
1670 break;
1671 default:
1672 /* RFC7231#6.1:
1673 * Responses with status codes that are defined as
1674 * cacheable by default (e.g., 200, 203, 204, 206,
1675 * 300, 301, 404, 405, 410, 414, and 501 in this
1676 * specification) can be reused by a cache with
1677 * heuristic expiration unless otherwise indicated
1678 * by the method definition or explicit cache
1679 * controls [RFC7234]; all other status codes are
1680 * not cacheable by default.
1681 */
1682 txn->flags &= ~(TX_CACHEABLE | TX_CACHE_COOK);
1683 break;
1684 }
1685
1686 /*
1687 * 3: we may need to capture headers
1688 */
1689 s->logs.logwait &= ~LW_RESP;
1690 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->res_cap))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001691 http_capture_headers(htx, s->res_cap, sess->fe->rsp_cap);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001692
Christopher Faulet9768c262018-10-22 09:34:31 +02001693 /* Skip parsing if no content length is possible. */
Christopher Fauletc75668e2020-12-07 18:10:32 +01001694 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status >= 200 && txn->status < 300) ||
Christopher Faulete0768eb2018-10-03 16:38:02 +02001695 txn->status == 101)) {
1696 /* Either we've established an explicit tunnel, or we're
1697 * switching the protocol. In both cases, we're very unlikely
1698 * to understand the next protocols. We have to switch to tunnel
1699 * mode, so that we transfer the request and responses then let
1700 * this protocol pass unmodified. When we later implement specific
1701 * parsers for such protocols, we'll want to check the Upgrade
1702 * header which contains information about that protocol for
1703 * responses with status 101 (eg: see RFC2817 about TLS).
1704 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02001705 txn->flags |= TX_CON_WANT_TUN;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001706 }
1707
Christopher Faulet61608322018-11-23 16:23:45 +01001708 /* check for NTML authentication headers in 401 (WWW-Authenticate) and
1709 * 407 (Proxy-Authenticate) responses and set the connection to private
1710 */
1711 srv_conn = cs_conn(objt_cs(s->si[1].end));
1712 if (srv_conn) {
1713 struct ist hdr;
1714 struct http_hdr_ctx ctx;
1715
1716 if (txn->status == 401)
1717 hdr = ist("WWW-Authenticate");
1718 else if (txn->status == 407)
1719 hdr = ist("Proxy-Authenticate");
1720 else
1721 goto end;
1722
1723 ctx.blk = NULL;
1724 while (http_find_header(htx, hdr, &ctx, 0)) {
Willy Tarreauf1dcced2020-05-07 19:27:02 +02001725 /* If www-authenticate contains "Negotiate", "Nego2", or "NTLM",
1726 * possibly followed by blanks and a base64 string, the connection
1727 * is private. Since it's a mess to deal with, we only check for
1728 * values starting with "NTLM" or "Nego". Note that often multiple
1729 * headers are sent by the server there.
1730 */
1731 if ((ctx.value.len >= 4 && strncasecmp(ctx.value.ptr, "Nego", 4) == 0) ||
Willy Tarreau49a1d282020-05-07 19:10:15 +02001732 (ctx.value.len >= 4 && strncasecmp(ctx.value.ptr, "NTLM", 4) == 0)) {
Olivier Houchard250031e2019-05-29 15:01:50 +02001733 sess->flags |= SESS_FL_PREFER_LAST;
Christopher Faulet08016ab2020-07-01 16:10:06 +02001734 conn_set_owner(srv_conn, sess, NULL);
Christopher Faulet21ddc742020-07-01 15:26:14 +02001735 conn_set_private(srv_conn);
Ilya Shipitsin6b79f382020-07-23 00:32:55 +05001736 /* If it fail now, the same will be done in mux->detach() callback */
Christopher Faulet08016ab2020-07-01 16:10:06 +02001737 session_add_conn(srv_conn->owner, srv_conn, srv_conn->target);
Willy Tarreauf1dcced2020-05-07 19:27:02 +02001738 break;
Olivier Houchard250031e2019-05-29 15:01:50 +02001739 }
Christopher Faulet61608322018-11-23 16:23:45 +01001740 }
1741 }
1742
1743 end:
Christopher Faulete0768eb2018-10-03 16:38:02 +02001744 /* we want to have the response time before we start processing it */
1745 s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now);
1746
1747 /* end of job, return OK */
1748 rep->analysers &= ~an_bit;
1749 rep->analyse_exp = TICK_ETERNITY;
1750 channel_auto_close(rep);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001751 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001752 return 1;
1753
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001754 return_int_err:
Willy Tarreau4781b152021-04-06 13:53:36 +02001755 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
1756 _HA_ATOMIC_INC(&s->be->be_counters.internal_errors);
William Lallemand36119de2021-03-08 15:26:48 +01001757 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001758 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Fauletb8a53712019-12-16 11:29:38 +01001759 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001760 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.internal_errors);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001761 txn->status = 500;
1762 if (!(s->flags & SF_ERR_MASK))
1763 s->flags |= SF_ERR_INTERNAL;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001764 goto return_prx_cond;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001765
1766 return_bad_res:
Willy Tarreau4781b152021-04-06 13:53:36 +02001767 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Faulet47365272018-10-31 17:40:50 +01001768 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001769 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001770 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Christopher Faulet47365272018-10-31 17:40:50 +01001771 }
Olivier Houcharde3249a92019-05-03 23:01:47 +02001772 if ((s->be->retry_type & PR_RE_JUNK_REQUEST) &&
Olivier Houchardad26d8d2019-05-10 17:48:28 +02001773 (si_b->flags & SI_FL_L7_RETRY) &&
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001774 do_l7_retry(s, si_b) == 0) {
1775 DBG_TRACE_DEVEL("leaving on L7 retry",
1776 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharde3249a92019-05-03 23:01:47 +02001777 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001778 }
Christopher Faulet47365272018-10-31 17:40:50 +01001779 txn->status = 502;
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001780 stream_inc_http_fail_ctr(s);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001781 /* fall through */
1782
Christopher Fauletb8a53712019-12-16 11:29:38 +01001783 return_prx_cond:
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001784 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet47365272018-10-31 17:40:50 +01001785
1786 if (!(s->flags & SF_ERR_MASK))
1787 s->flags |= SF_ERR_PRXCOND;
1788 if (!(s->flags & SF_FINST_MASK))
1789 s->flags |= SF_FINST_H;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001790
1791 s->si[1].flags |= SI_FL_NOLINGER;
1792 rep->analysers &= AN_RES_FLT_END;
Christopher Faulete58c0002020-03-02 16:21:01 +01001793 s->req.analysers &= AN_REQ_FLT_END;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001794 rep->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001795 DBG_TRACE_DEVEL("leaving on error",
1796 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulet47365272018-10-31 17:40:50 +01001797 return 0;
1798
Christopher Faulete0768eb2018-10-03 16:38:02 +02001799 abort_keep_alive:
1800 /* A keep-alive request to the server failed on a network error.
1801 * The client is required to retry. We need to close without returning
1802 * any other information so that the client retries.
1803 */
1804 txn->status = 0;
1805 rep->analysers &= AN_RES_FLT_END;
1806 s->req.analysers &= AN_REQ_FLT_END;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001807 s->logs.logwait = 0;
1808 s->logs.level = 0;
1809 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001810 http_reply_and_close(s, txn->status, NULL);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001811 DBG_TRACE_DEVEL("leaving by closing K/A connection",
1812 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001813 return 0;
1814}
1815
1816/* This function performs all the processing enabled for the current response.
1817 * It normally returns 1 unless it wants to break. It relies on buffers flags,
1818 * and updates s->res.analysers. It might make sense to explode it into several
1819 * other functions. It works like process_request (see indications above).
1820 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001821int http_process_res_common(struct stream *s, struct channel *rep, int an_bit, struct proxy *px)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001822{
1823 struct session *sess = s->sess;
1824 struct http_txn *txn = s->txn;
1825 struct http_msg *msg = &txn->rsp;
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001826 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001827 struct proxy *cur_proxy;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001828 enum rule_result ret = HTTP_RULE_RES_CONT;
1829
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001830 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
1831 return 0;
Christopher Faulet9768c262018-10-22 09:34:31 +02001832
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001833 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001834
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001835 htx = htxbuf(&rep->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001836
1837 /* The stats applet needs to adjust the Connection header but we don't
1838 * apply any filter there.
1839 */
1840 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
1841 rep->analysers &= ~an_bit;
1842 rep->analyse_exp = TICK_ETERNITY;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001843 goto end;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001844 }
1845
1846 /*
1847 * We will have to evaluate the filters.
1848 * As opposed to version 1.2, now they will be evaluated in the
1849 * filters order and not in the header order. This means that
1850 * each filter has to be validated among all headers.
1851 *
1852 * Filters are tried with ->be first, then with ->fe if it is
1853 * different from ->be.
1854 *
1855 * Maybe we are in resume condiion. In this case I choose the
1856 * "struct proxy" which contains the rule list matching the resume
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05001857 * pointer. If none of these "struct proxy" match, I initialise
Christopher Faulete0768eb2018-10-03 16:38:02 +02001858 * the process with the first one.
1859 *
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05001860 * In fact, I check only correspondence between the current list
Christopher Faulete0768eb2018-10-03 16:38:02 +02001861 * pointer and the ->fe rule list. If it doesn't match, I initialize
1862 * the loop with the ->be.
1863 */
1864 if (s->current_rule_list == &sess->fe->http_res_rules)
1865 cur_proxy = sess->fe;
1866 else
1867 cur_proxy = s->be;
1868 while (1) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001869 /* evaluate http-response rules */
1870 if (ret == HTTP_RULE_RES_CONT) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001871 ret = http_res_get_intercept_rule(cur_proxy, &cur_proxy->http_res_rules, s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001872
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001873 switch (ret) {
1874 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
1875 goto return_prx_yield;
1876
1877 case HTTP_RULE_RES_CONT:
1878 case HTTP_RULE_RES_STOP: /* nothing to do */
1879 break;
1880
1881 case HTTP_RULE_RES_DENY: /* deny or tarpit */
1882 goto deny;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001883
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001884 case HTTP_RULE_RES_ABRT: /* abort request, response already sent */
1885 goto return_prx_cond;
1886
1887 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
Christopher Fauletb8a53712019-12-16 11:29:38 +01001888 goto done;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001889
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001890 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
1891 goto return_bad_res;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001892
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001893 case HTTP_RULE_RES_ERROR: /* failed with a bad request */
1894 goto return_int_err;
1895 }
1896
1897 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001898
Christopher Faulete0768eb2018-10-03 16:38:02 +02001899 /* check whether we're already working on the frontend */
1900 if (cur_proxy == sess->fe)
1901 break;
1902 cur_proxy = sess->fe;
1903 }
1904
Christopher Faulete0768eb2018-10-03 16:38:02 +02001905 /* OK that's all we can do for 1xx responses */
1906 if (unlikely(txn->status < 200 && txn->status != 101))
Christopher Fauletf2824e62018-10-01 12:12:37 +02001907 goto end;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001908
1909 /*
1910 * Now check for a server cookie.
1911 */
1912 if (s->be->cookie_name || sess->fe->capture_name || (s->be->options & PR_O_CHK_CACHE))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001913 http_manage_server_side_cookies(s, rep);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001914
1915 /*
1916 * Check for cache-control or pragma headers if required.
1917 */
1918 if ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001919 http_check_response_for_cacheability(s, rep);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001920
1921 /*
1922 * Add server cookie in the response if needed
1923 */
1924 if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) &&
1925 !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) &&
1926 (!(s->flags & SF_DIRECT) ||
1927 ((s->be->cookie_maxidle || txn->cookie_last_date) &&
1928 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
1929 (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
1930 (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
1931 (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
1932 !(s->flags & SF_IGNORE_PRST)) {
1933 /* the server is known, it's not the one the client requested, or the
1934 * cookie's last seen date needs to be refreshed. We have to
1935 * insert a set-cookie here, except if we want to insert only on POST
1936 * requests and this one isn't. Note that servers which don't have cookies
1937 * (eg: some backup servers) will return a full cookie removal request.
1938 */
1939 if (!objt_server(s->target)->cookie) {
1940 chunk_printf(&trash,
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001941 "%s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
Christopher Faulete0768eb2018-10-03 16:38:02 +02001942 s->be->cookie_name);
1943 }
1944 else {
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001945 chunk_printf(&trash, "%s=%s", s->be->cookie_name, objt_server(s->target)->cookie);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001946
1947 if (s->be->cookie_maxidle || s->be->cookie_maxlife) {
1948 /* emit last_date, which is mandatory */
1949 trash.area[trash.data++] = COOKIE_DELIM_DATE;
1950 s30tob64((date.tv_sec+3) >> 2,
1951 trash.area + trash.data);
1952 trash.data += 5;
1953
1954 if (s->be->cookie_maxlife) {
1955 /* emit first_date, which is either the original one or
1956 * the current date.
1957 */
1958 trash.area[trash.data++] = COOKIE_DELIM_DATE;
1959 s30tob64(txn->cookie_first_date ?
1960 txn->cookie_first_date >> 2 :
1961 (date.tv_sec+3) >> 2,
1962 trash.area + trash.data);
1963 trash.data += 5;
1964 }
1965 }
1966 chunk_appendf(&trash, "; path=/");
1967 }
1968
1969 if (s->be->cookie_domain)
1970 chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain);
1971
1972 if (s->be->ck_opts & PR_CK_HTTPONLY)
1973 chunk_appendf(&trash, "; HttpOnly");
1974
1975 if (s->be->ck_opts & PR_CK_SECURE)
1976 chunk_appendf(&trash, "; Secure");
1977
Christopher Faulet2f533902020-01-21 11:06:48 +01001978 if (s->be->cookie_attrs)
1979 chunk_appendf(&trash, "; %s", s->be->cookie_attrs);
1980
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001981 if (unlikely(!http_add_header(htx, ist("Set-Cookie"), ist2(trash.area, trash.data))))
Christopher Fauletb8a53712019-12-16 11:29:38 +01001982 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001983
1984 txn->flags &= ~TX_SCK_MASK;
1985 if (__objt_server(s->target)->cookie && (s->flags & SF_DIRECT))
1986 /* the server did not change, only the date was updated */
1987 txn->flags |= TX_SCK_UPDATED;
1988 else
1989 txn->flags |= TX_SCK_INSERTED;
1990
1991 /* Here, we will tell an eventual cache on the client side that we don't
1992 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
1993 * Some caches understand the correct form: 'no-cache="set-cookie"', but
1994 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
1995 */
1996 if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
1997
1998 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
1999
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002000 if (unlikely(!http_add_header(htx, ist("Cache-control"), ist("private"))))
Christopher Fauletb8a53712019-12-16 11:29:38 +01002001 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002002 }
2003 }
2004
2005 /*
2006 * Check if result will be cacheable with a cookie.
2007 * We'll block the response if security checks have caught
2008 * nasty things such as a cacheable cookie.
2009 */
2010 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
2011 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
2012 (s->be->options & PR_O_CHK_CACHE)) {
2013 /* we're in presence of a cacheable response containing
2014 * a set-cookie header. We'll block it as requested by
2015 * the 'checkcache' option, and send an alert.
2016 */
Christopher Faulete0768eb2018-10-03 16:38:02 +02002017 ha_alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
2018 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
2019 send_log(s->be, LOG_ALERT,
2020 "Blocking cacheable cookie in response from instance %s, server %s.\n",
2021 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
Christopher Fauletb8a53712019-12-16 11:29:38 +01002022 goto deny;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002023 }
2024
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002025 end:
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002026 /*
2027 * Evaluate after-response rules before forwarding the response. rules
2028 * from the backend are evaluated first, then one from the frontend if
2029 * it differs.
2030 */
2031 if (!http_eval_after_res_rules(s))
2032 goto return_int_err;
2033
Christopher Fauletc2ac5e42021-03-08 18:20:09 +01002034 /* Filter the response headers if there are filters attached to the
2035 * stream.
2036 */
2037 if (HAS_FILTERS(s))
2038 rep->analysers |= AN_RES_FLT_HTTP_HDRS;
2039
Christopher Faulete0768eb2018-10-03 16:38:02 +02002040 /* Always enter in the body analyzer */
2041 rep->analysers &= ~AN_RES_FLT_XFER_DATA;
2042 rep->analysers |= AN_RES_HTTP_XFER_BODY;
2043
2044 /* if the user wants to log as soon as possible, without counting
2045 * bytes from the server, then this is the right moment. We have
2046 * to temporarily assign bytes_out to log what we currently have.
2047 */
2048 if (!LIST_ISEMPTY(&sess->fe->logformat) && !(s->logs.logwait & LW_BYTES)) {
2049 s->logs.t_close = s->logs.t_data; /* to get a valid end date */
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002050 s->logs.bytes_out = htx->data;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002051 s->do_log(s);
2052 s->logs.bytes_out = 0;
2053 }
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002054
Christopher Fauletb8a53712019-12-16 11:29:38 +01002055 done:
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002056 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002057 rep->analysers &= ~an_bit;
2058 rep->analyse_exp = TICK_ETERNITY;
2059 return 1;
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002060
Christopher Fauletb8a53712019-12-16 11:29:38 +01002061 deny:
Willy Tarreau4781b152021-04-06 13:53:36 +02002062 _HA_ATOMIC_INC(&sess->fe->fe_counters.denied_resp);
2063 _HA_ATOMIC_INC(&s->be->be_counters.denied_resp);
William Lallemand36119de2021-03-08 15:26:48 +01002064 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02002065 _HA_ATOMIC_INC(&sess->listener->counters->denied_resp);
Christopher Fauleta08546b2019-12-16 16:07:34 +01002066 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02002067 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.denied_resp);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002068 goto return_prx_err;
2069
2070 return_int_err:
2071 txn->status = 500;
2072 if (!(s->flags & SF_ERR_MASK))
2073 s->flags |= SF_ERR_INTERNAL;
Willy Tarreau4781b152021-04-06 13:53:36 +02002074 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
2075 _HA_ATOMIC_INC(&s->be->be_counters.internal_errors);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002076 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02002077 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.internal_errors);
Christopher Fauletcff0f732019-12-16 16:13:44 +01002078 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02002079 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.internal_errors);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002080 goto return_prx_err;
2081
2082 return_bad_res:
2083 txn->status = 502;
Willy Tarreau826f3ab2021-02-10 12:07:15 +01002084 stream_inc_http_fail_ctr(s);
Willy Tarreau4781b152021-04-06 13:53:36 +02002085 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Fauleta20a6532020-02-05 10:16:41 +01002086 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02002087 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Christopher Fauleta20a6532020-02-05 10:16:41 +01002088 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_RSP);
2089 }
Christopher Fauletb8a53712019-12-16 11:29:38 +01002090 /* fall through */
2091
2092 return_prx_err:
2093 http_reply_and_close(s, txn->status, http_error_message(s));
2094 /* fall through */
2095
2096 return_prx_cond:
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002097 s->logs.t_data = -1; /* was not a valid response */
2098 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002099
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002100 if (!(s->flags & SF_ERR_MASK))
2101 s->flags |= SF_ERR_PRXCOND;
2102 if (!(s->flags & SF_FINST_MASK))
2103 s->flags |= SF_FINST_H;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002104
Christopher Faulete58c0002020-03-02 16:21:01 +01002105 rep->analysers &= AN_RES_FLT_END;
2106 s->req.analysers &= AN_REQ_FLT_END;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002107 rep->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002108 DBG_TRACE_DEVEL("leaving on error",
2109 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002110 return 0;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002111
2112 return_prx_yield:
2113 channel_dont_close(rep);
2114 DBG_TRACE_DEVEL("waiting for more data",
2115 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
2116 return 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002117}
2118
2119/* This function is an analyser which forwards response body (including chunk
2120 * sizes if any). It is called as soon as we must forward, even if we forward
2121 * zero byte. The only situation where it must not be called is when we're in
2122 * tunnel mode and we want to forward till the close. It's used both to forward
2123 * remaining data and to resync after end of body. It expects the msg_state to
2124 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
2125 * read more data, or 1 once we can go on with next request or end the stream.
2126 *
2127 * It is capable of compressing response data both in content-length mode and
2128 * in chunked mode. The state machines follows different flows depending on
2129 * whether content-length and chunked modes are used, since there are no
2130 * trailers in content-length :
2131 *
2132 * chk-mode cl-mode
2133 * ,----- BODY -----.
2134 * / \
2135 * V size > 0 V chk-mode
2136 * .--> SIZE -------------> DATA -------------> CRLF
2137 * | | size == 0 | last byte |
2138 * | v final crlf v inspected |
2139 * | TRAILERS -----------> DONE |
2140 * | |
2141 * `----------------------------------------------'
2142 *
2143 * Compression only happens in the DATA state, and must be flushed in final
2144 * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding
2145 * is performed at once on final states for all bytes parsed, or when leaving
2146 * on missing data.
2147 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002148int http_response_forward_body(struct stream *s, struct channel *res, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +02002149{
2150 struct session *sess = s->sess;
2151 struct http_txn *txn = s->txn;
2152 struct http_msg *msg = &s->txn->rsp;
Christopher Faulet9768c262018-10-22 09:34:31 +02002153 struct htx *htx;
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002154 int ret;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002155
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002156 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002157
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002158 htx = htxbuf(&res->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002159
Christopher Fauletb9a92f32019-09-09 10:15:21 +02002160 if (htx->flags & HTX_FL_PARSING_ERROR)
2161 goto return_bad_res;
2162 if (htx->flags & HTX_FL_PROCESSING_ERROR)
2163 goto return_int_err;
2164
Christopher Faulete0768eb2018-10-03 16:38:02 +02002165 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Christopher Fauletf2824e62018-10-01 12:12:37 +02002166 ((res->flags & CF_SHUTW) && (res->to_forward || co_data(res)))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02002167 /* Output closed while we were sending data. We must abort and
2168 * wake the other side up.
2169 */
Christopher Faulete0768eb2018-10-03 16:38:02 +02002170 msg->msg_state = HTTP_MSG_ERROR;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002171 http_end_response(s);
2172 http_end_request(s);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002173 DBG_TRACE_DEVEL("leaving on error",
2174 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002175 return 1;
2176 }
2177
Christopher Faulet9768c262018-10-22 09:34:31 +02002178 if (msg->msg_state == HTTP_MSG_BODY)
2179 msg->msg_state = HTTP_MSG_DATA;
2180
Christopher Faulete0768eb2018-10-03 16:38:02 +02002181 /* in most states, we should abort in case of early close */
2182 channel_auto_close(res);
2183
Christopher Faulete0768eb2018-10-03 16:38:02 +02002184 if (res->to_forward) {
Christopher Faulet66af0b22019-03-22 14:54:52 +01002185 if (res->to_forward == CHN_INFINITE_FORWARD) {
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002186 if (res->flags & CF_EOI)
2187 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet66af0b22019-03-22 14:54:52 +01002188 }
2189 else {
2190 /* We can't process the buffer's contents yet */
2191 res->flags |= CF_WAKE_WRITE;
2192 goto missing_data_or_waiting;
2193 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02002194 }
2195
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002196 if (msg->msg_state >= HTTP_MSG_ENDING)
2197 goto ending;
2198
Christopher Fauletc75668e2020-12-07 18:10:32 +01002199 if ((txn->meth == HTTP_METH_CONNECT && txn->status >= 200 && txn->status < 300) || txn->status == 101 ||
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002200 (!(msg->flags & HTTP_MSGF_XFER_LEN) && !HAS_RSP_DATA_FILTERS(s))) {
2201 msg->msg_state = HTTP_MSG_ENDING;
2202 goto ending;
2203 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002204
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002205 /* Forward input data. We get it by removing all outgoing data not
2206 * forwarded yet from HTX data size. If there are some data filters, we
2207 * let them decide the amount of data to forward.
Christopher Faulet9768c262018-10-22 09:34:31 +02002208 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002209 if (HAS_RSP_DATA_FILTERS(s)) {
2210 ret = flt_http_payload(s, msg, htx->data);
2211 if (ret < 0)
2212 goto return_bad_res;
Christopher Faulet421e7692019-06-13 11:16:45 +02002213 c_adv(res, ret);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002214 }
2215 else {
Christopher Faulet421e7692019-06-13 11:16:45 +02002216 c_adv(res, htx->data - co_data(res));
Christopher Faulet66af0b22019-03-22 14:54:52 +01002217 if (msg->flags & HTTP_MSGF_XFER_LEN)
2218 channel_htx_forward_forever(res, htx);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002219 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002220
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002221 if (htx->data != co_data(res))
2222 goto missing_data_or_waiting;
2223
2224 if (!(msg->flags & HTTP_MSGF_XFER_LEN) && res->flags & CF_SHUTR) {
2225 msg->msg_state = HTTP_MSG_ENDING;
2226 goto ending;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002227 }
2228
Christopher Faulet9768c262018-10-22 09:34:31 +02002229 /* Check if the end-of-message is reached and if so, switch the message
Christopher Fauletd20fdb02019-06-13 16:43:22 +02002230 * in HTTP_MSG_ENDING state. Then if all data was marked to be
2231 * forwarded, set the state to HTTP_MSG_DONE.
Christopher Faulet9768c262018-10-22 09:34:31 +02002232 */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002233 if (!(htx->flags & HTX_FL_EOM))
Christopher Faulet9768c262018-10-22 09:34:31 +02002234 goto missing_data_or_waiting;
2235
Christopher Fauletd20fdb02019-06-13 16:43:22 +02002236 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet9768c262018-10-22 09:34:31 +02002237
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002238 ending:
Christopher Faulet2151cdd2020-07-22 16:34:59 +02002239 res->flags &= ~CF_EXPECT_MORE; /* no more data are expected */
2240
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002241 /* other states, ENDING...TUNNEL */
2242 if (msg->msg_state >= HTTP_MSG_DONE)
2243 goto done;
Christopher Faulet9768c262018-10-22 09:34:31 +02002244
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002245 if (HAS_RSP_DATA_FILTERS(s)) {
2246 ret = flt_http_end(s, msg);
2247 if (ret <= 0) {
2248 if (!ret)
2249 goto missing_data_or_waiting;
2250 goto return_bad_res;
2251 }
2252 }
2253
Christopher Fauletc75668e2020-12-07 18:10:32 +01002254 if ((txn->meth == HTTP_METH_CONNECT && txn->status >= 200 && txn->status < 300) || txn->status == 101 ||
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002255 !(msg->flags & HTTP_MSGF_XFER_LEN)) {
2256 msg->msg_state = HTTP_MSG_TUNNEL;
2257 goto ending;
2258 }
2259 else {
2260 msg->msg_state = HTTP_MSG_DONE;
2261 res->to_forward = 0;
2262 }
2263
2264 done:
2265
2266 channel_dont_close(res);
2267
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002268 http_end_response(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002269 if (!(res->analysers & an_bit)) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002270 http_end_request(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002271 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
2272 if (res->flags & CF_SHUTW) {
2273 /* response errors are most likely due to the
2274 * client aborting the transfer. */
Christopher Faulet93e02d82019-03-08 14:18:50 +01002275 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002276 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02002277 goto return_bad_res;
2278 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002279 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002280 return 1;
2281 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002282 DBG_TRACE_DEVEL("waiting for the end of the HTTP txn",
2283 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002284 return 0;
2285
2286 missing_data_or_waiting:
2287 if (res->flags & CF_SHUTW)
Christopher Faulet93e02d82019-03-08 14:18:50 +01002288 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002289
2290 /* stop waiting for data if the input is closed before the end. If the
2291 * client side was already closed, it means that the client has aborted,
2292 * so we don't want to count this as a server abort. Otherwise it's a
2293 * server abort.
2294 */
Christopher Fauletd20fdb02019-06-13 16:43:22 +02002295 if (msg->msg_state < HTTP_MSG_ENDING && res->flags & CF_SHUTR) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02002296 if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))
Christopher Faulet93e02d82019-03-08 14:18:50 +01002297 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002298 /* If we have some pending data, we continue the processing */
Christopher Faulet93e02d82019-03-08 14:18:50 +01002299 if (htx_is_empty(htx))
2300 goto return_srv_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002301 }
2302
Christopher Faulete0768eb2018-10-03 16:38:02 +02002303 /* When TE: chunked is used, we need to get there again to parse
2304 * remaining chunks even if the server has closed, so we don't want to
Christopher Faulet9768c262018-10-22 09:34:31 +02002305 * set CF_DONTCLOSE. Similarly when there is a content-leng or if there
2306 * are filters registered on the stream, we don't want to forward a
2307 * close
Christopher Faulete0768eb2018-10-03 16:38:02 +02002308 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002309 if ((msg->flags & HTTP_MSGF_XFER_LEN) || HAS_RSP_DATA_FILTERS(s))
Christopher Faulete0768eb2018-10-03 16:38:02 +02002310 channel_dont_close(res);
2311
2312 /* We know that more data are expected, but we couldn't send more that
2313 * what we did. So we always set the CF_EXPECT_MORE flag so that the
2314 * system knows it must not set a PUSH on this first part. Interactive
2315 * modes are already handled by the stream sock layer. We must not do
2316 * this in content-length mode because it could present the MSG_MORE
2317 * flag with the last block of forwarded data, which would cause an
2318 * additional delay to be observed by the receiver.
2319 */
Christopher Faulet2151cdd2020-07-22 16:34:59 +02002320 if (HAS_RSP_DATA_FILTERS(s))
Christopher Faulete0768eb2018-10-03 16:38:02 +02002321 res->flags |= CF_EXPECT_MORE;
2322
2323 /* the stream handler will take care of timeouts and errors */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002324 DBG_TRACE_DEVEL("waiting for more data to forward",
2325 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002326 return 0;
2327
Christopher Faulet93e02d82019-03-08 14:18:50 +01002328 return_srv_abort:
Willy Tarreau4781b152021-04-06 13:53:36 +02002329 _HA_ATOMIC_INC(&sess->fe->fe_counters.srv_aborts);
2330 _HA_ATOMIC_INC(&s->be->be_counters.srv_aborts);
William Lallemand36119de2021-03-08 15:26:48 +01002331 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02002332 _HA_ATOMIC_INC(&sess->listener->counters->srv_aborts);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002333 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02002334 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.srv_aborts);
Willy Tarreau826f3ab2021-02-10 12:07:15 +01002335 stream_inc_http_fail_ctr(s);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002336 if (!(s->flags & SF_ERR_MASK))
2337 s->flags |= SF_ERR_SRVCL;
2338 goto return_error;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002339
Christopher Faulet93e02d82019-03-08 14:18:50 +01002340 return_cli_abort:
Willy Tarreau4781b152021-04-06 13:53:36 +02002341 _HA_ATOMIC_INC(&sess->fe->fe_counters.cli_aborts);
2342 _HA_ATOMIC_INC(&s->be->be_counters.cli_aborts);
William Lallemand36119de2021-03-08 15:26:48 +01002343 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02002344 _HA_ATOMIC_INC(&sess->listener->counters->cli_aborts);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002345 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02002346 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.cli_aborts);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002347 if (!(s->flags & SF_ERR_MASK))
2348 s->flags |= SF_ERR_CLICL;
2349 goto return_error;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002350
Christopher Fauletb9a92f32019-09-09 10:15:21 +02002351 return_int_err:
Willy Tarreau4781b152021-04-06 13:53:36 +02002352 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
2353 _HA_ATOMIC_INC(&s->be->be_counters.internal_errors);
William Lallemand36119de2021-03-08 15:26:48 +01002354 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02002355 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002356 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02002357 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.internal_errors);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02002358 if (!(s->flags & SF_ERR_MASK))
2359 s->flags |= SF_ERR_INTERNAL;
2360 goto return_error;
2361
Christopher Faulet93e02d82019-03-08 14:18:50 +01002362 return_bad_res:
Willy Tarreau4781b152021-04-06 13:53:36 +02002363 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002364 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02002365 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002366 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_RSP);
2367 }
Willy Tarreau826f3ab2021-02-10 12:07:15 +01002368 stream_inc_http_fail_ctr(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002369 if (!(s->flags & SF_ERR_MASK))
Christopher Faulet93e02d82019-03-08 14:18:50 +01002370 s->flags |= SF_ERR_SRVCL;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002371 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +02002372
Christopher Faulet93e02d82019-03-08 14:18:50 +01002373 return_error:
Christopher Faulete0768eb2018-10-03 16:38:02 +02002374 /* don't send any error message as we're in the body */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002375 http_reply_and_close(s, txn->status, NULL);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002376 res->analysers &= AN_RES_FLT_END;
2377 s->req.analysers &= AN_REQ_FLT_END; /* we're in data phase, we want to abort both directions */
Christopher Faulete0768eb2018-10-03 16:38:02 +02002378 if (!(s->flags & SF_FINST_MASK))
2379 s->flags |= SF_FINST_D;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002380 DBG_TRACE_DEVEL("leaving on error",
2381 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002382 return 0;
2383}
2384
Christopher Fauletf2824e62018-10-01 12:12:37 +02002385/* Perform an HTTP redirect based on the information in <rule>. The function
Christopher Faulet99daf282018-11-28 22:58:13 +01002386 * returns zero on success, or zero in case of a, irrecoverable error such
Christopher Fauletf2824e62018-10-01 12:12:37 +02002387 * as too large a request to build a valid response.
2388 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002389int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn)
Christopher Fauletf2824e62018-10-01 12:12:37 +02002390{
Christopher Faulet99daf282018-11-28 22:58:13 +01002391 struct channel *req = &s->req;
2392 struct channel *res = &s->res;
2393 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01002394 struct htx_sl *sl;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002395 struct buffer *chunk;
Christopher Faulet99daf282018-11-28 22:58:13 +01002396 struct ist status, reason, location;
2397 unsigned int flags;
Christopher Faulet08e66462019-05-23 16:44:59 +02002398 int close = 0; /* Try to keep the connection alive byt default */
Christopher Fauletf2824e62018-10-01 12:12:37 +02002399
2400 chunk = alloc_trash_chunk();
Christopher Fauletb8a53712019-12-16 11:29:38 +01002401 if (!chunk) {
2402 if (!(s->flags & SF_ERR_MASK))
2403 s->flags |= SF_ERR_RESOURCE;
Christopher Faulet99daf282018-11-28 22:58:13 +01002404 goto fail;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002405 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002406
Christopher Faulet99daf282018-11-28 22:58:13 +01002407 /*
2408 * Create the location
2409 */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002410 htx = htxbuf(&req->buf);
Christopher Fauletf2824e62018-10-01 12:12:37 +02002411 switch(rule->type) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002412 case REDIRECT_TYPE_SCHEME: {
2413 struct http_hdr_ctx ctx;
2414 struct ist path, host;
Amaury Denoyellec453f952021-07-06 11:40:12 +02002415 struct http_uri_parser parser;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002416
Christopher Faulet99daf282018-11-28 22:58:13 +01002417 host = ist("");
2418 ctx.blk = NULL;
2419 if (http_find_header(htx, ist("Host"), &ctx, 0))
2420 host = ctx.value;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002421
Christopher Faulet297fbb42019-05-13 14:41:27 +02002422 sl = http_get_stline(htx);
Amaury Denoyellec453f952021-07-06 11:40:12 +02002423 parser = http_uri_parser_init(htx_sl_req_uri(sl));
2424 path = http_parse_path(&parser);
Christopher Faulet99daf282018-11-28 22:58:13 +01002425 /* build message using path */
Tim Duesterhused526372020-03-05 17:56:33 +01002426 if (isttest(path)) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002427 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2428 int qs = 0;
2429 while (qs < path.len) {
2430 if (*(path.ptr + qs) == '?') {
2431 path.len = qs;
2432 break;
2433 }
2434 qs++;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002435 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002436 }
2437 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002438 else
2439 path = ist("/");
Christopher Fauletf2824e62018-10-01 12:12:37 +02002440
Christopher Faulet99daf282018-11-28 22:58:13 +01002441 if (rule->rdr_str) { /* this is an old "redirect" rule */
2442 /* add scheme */
2443 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2444 goto fail;
2445 }
2446 else {
2447 /* add scheme with executing log format */
2448 chunk->data += build_logline(s, chunk->area + chunk->data,
2449 chunk->size - chunk->data,
2450 &rule->rdr_fmt);
2451 }
2452 /* add "://" + host + path */
2453 if (!chunk_memcat(chunk, "://", 3) ||
2454 !chunk_memcat(chunk, host.ptr, host.len) ||
2455 !chunk_memcat(chunk, path.ptr, path.len))
2456 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002457
Christopher Faulet99daf282018-11-28 22:58:13 +01002458 /* append a slash at the end of the location if needed and missing */
2459 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
2460 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
2461 if (chunk->data + 1 >= chunk->size)
2462 goto fail;
2463 chunk->area[chunk->data++] = '/';
2464 }
2465 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002466 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002467
Christopher Faulet99daf282018-11-28 22:58:13 +01002468 case REDIRECT_TYPE_PREFIX: {
2469 struct ist path;
Amaury Denoyellec453f952021-07-06 11:40:12 +02002470 struct http_uri_parser parser;
Christopher Faulet99daf282018-11-28 22:58:13 +01002471
Christopher Faulet297fbb42019-05-13 14:41:27 +02002472 sl = http_get_stline(htx);
Amaury Denoyellec453f952021-07-06 11:40:12 +02002473 parser = http_uri_parser_init(htx_sl_req_uri(sl));
2474 path = http_parse_path(&parser);
Christopher Faulet99daf282018-11-28 22:58:13 +01002475 /* build message using path */
Tim Duesterhused526372020-03-05 17:56:33 +01002476 if (isttest(path)) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002477 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2478 int qs = 0;
2479 while (qs < path.len) {
2480 if (*(path.ptr + qs) == '?') {
2481 path.len = qs;
2482 break;
2483 }
2484 qs++;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002485 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002486 }
2487 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002488 else
2489 path = ist("/");
Christopher Fauletf2824e62018-10-01 12:12:37 +02002490
Christopher Faulet99daf282018-11-28 22:58:13 +01002491 if (rule->rdr_str) { /* this is an old "redirect" rule */
2492 /* add prefix. Note that if prefix == "/", we don't want to
2493 * add anything, otherwise it makes it hard for the user to
2494 * configure a self-redirection.
2495 */
2496 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
2497 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2498 goto fail;
2499 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002500 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002501 else {
2502 /* add prefix with executing log format */
2503 chunk->data += build_logline(s, chunk->area + chunk->data,
2504 chunk->size - chunk->data,
2505 &rule->rdr_fmt);
2506 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002507
Christopher Faulet99daf282018-11-28 22:58:13 +01002508 /* add path */
2509 if (!chunk_memcat(chunk, path.ptr, path.len))
2510 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002511
Christopher Faulet99daf282018-11-28 22:58:13 +01002512 /* append a slash at the end of the location if needed and missing */
2513 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
2514 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
2515 if (chunk->data + 1 >= chunk->size)
2516 goto fail;
2517 chunk->area[chunk->data++] = '/';
2518 }
2519 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002520 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002521 case REDIRECT_TYPE_LOCATION:
2522 default:
2523 if (rule->rdr_str) { /* this is an old "redirect" rule */
2524 /* add location */
2525 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2526 goto fail;
2527 }
2528 else {
2529 /* add location with executing log format */
2530 chunk->data += build_logline(s, chunk->area + chunk->data,
2531 chunk->size - chunk->data,
2532 &rule->rdr_fmt);
2533 }
2534 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002535 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002536 location = ist2(chunk->area, chunk->data);
2537
2538 /*
2539 * Create the 30x response
2540 */
2541 switch (rule->code) {
2542 case 308:
2543 status = ist("308");
2544 reason = ist("Permanent Redirect");
2545 break;
2546 case 307:
2547 status = ist("307");
2548 reason = ist("Temporary Redirect");
2549 break;
2550 case 303:
2551 status = ist("303");
2552 reason = ist("See Other");
2553 break;
2554 case 301:
2555 status = ist("301");
2556 reason = ist("Moved Permanently");
2557 break;
2558 case 302:
2559 default:
2560 status = ist("302");
2561 reason = ist("Found");
2562 break;
2563 }
2564
Christopher Faulet08e66462019-05-23 16:44:59 +02002565 if (!(txn->req.flags & HTTP_MSGF_BODYLESS) && txn->req.msg_state != HTTP_MSG_DONE)
2566 close = 1;
2567
Christopher Faulet99daf282018-11-28 22:58:13 +01002568 htx = htx_from_buf(&res->buf);
Kevin Zhu96b36392020-01-07 09:42:55 +01002569 /* Trim any possible response */
2570 channel_htx_truncate(&s->res, htx);
Christopher Faulet99daf282018-11-28 22:58:13 +01002571 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
2572 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), status, reason);
2573 if (!sl)
2574 goto fail;
2575 sl->info.res.status = rule->code;
2576 s->txn->status = rule->code;
2577
Christopher Faulet08e66462019-05-23 16:44:59 +02002578 if (close && !htx_add_header(htx, ist("Connection"), ist("close")))
2579 goto fail;
2580
2581 if (!htx_add_header(htx, ist("Content-length"), ist("0")) ||
Christopher Faulet99daf282018-11-28 22:58:13 +01002582 !htx_add_header(htx, ist("Location"), location))
2583 goto fail;
2584
2585 if (rule->code == 302 || rule->code == 303 || rule->code == 307) {
2586 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")))
2587 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002588 }
2589
2590 if (rule->cookie_len) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002591 if (!htx_add_header(htx, ist("Set-Cookie"), ist2(rule->cookie_str, rule->cookie_len)))
2592 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002593 }
2594
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002595 if (!htx_add_endof(htx, HTX_BLK_EOH))
Christopher Faulet99daf282018-11-28 22:58:13 +01002596 goto fail;
2597
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002598 htx->flags |= HTX_FL_EOM;
Kevin Zhu96b36392020-01-07 09:42:55 +01002599 htx_to_buf(htx, &res->buf);
Christopher Fauleta72a7e42020-01-28 09:28:11 +01002600 if (!http_forward_proxy_resp(s, 1))
2601 goto fail;
Christopher Faulet99daf282018-11-28 22:58:13 +01002602
Christopher Faulet60b33a52020-01-28 09:18:10 +01002603 if (rule->flags & REDIRECT_FLAG_FROM_REQ) {
2604 /* let's log the request time */
2605 s->logs.tv_request = now;
2606 req->analysers &= AN_REQ_FLT_END;
Christopher Faulet99daf282018-11-28 22:58:13 +01002607
Christopher Faulet60b33a52020-01-28 09:18:10 +01002608 if (s->sess->fe == s->be) /* report it if the request was intercepted by the frontend */
Willy Tarreau4781b152021-04-06 13:53:36 +02002609 _HA_ATOMIC_INC(&s->sess->fe->fe_counters.intercepted_req);
Christopher Faulet60b33a52020-01-28 09:18:10 +01002610 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002611
2612 if (!(s->flags & SF_ERR_MASK))
2613 s->flags |= SF_ERR_LOCAL;
2614 if (!(s->flags & SF_FINST_MASK))
Christopher Faulet60b33a52020-01-28 09:18:10 +01002615 s->flags |= ((rule->flags & REDIRECT_FLAG_FROM_REQ) ? SF_FINST_R : SF_FINST_H);
Christopher Fauletf2824e62018-10-01 12:12:37 +02002616
Christopher Faulet99daf282018-11-28 22:58:13 +01002617 free_trash_chunk(chunk);
2618 return 1;
2619
2620 fail:
2621 /* If an error occurred, remove the incomplete HTTP response from the
2622 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01002623 channel_htx_truncate(res, htxbuf(&res->buf));
Christopher Fauletf2824e62018-10-01 12:12:37 +02002624 free_trash_chunk(chunk);
Christopher Faulet99daf282018-11-28 22:58:13 +01002625 return 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002626}
2627
Christopher Faulet92d34fe2019-12-17 09:20:34 +01002628/* Replace all headers matching the name <name>. The header value is replaced if
2629 * it matches the regex <re>. <str> is used for the replacement. If <full> is
2630 * set to 1, the full-line is matched and replaced. Otherwise, comma-separated
2631 * values are evaluated one by one. It returns 0 on success and -1 on error.
2632 */
2633int http_replace_hdrs(struct stream* s, struct htx *htx, struct ist name,
2634 const char *str, struct my_regex *re, int full)
Christopher Faulet72333522018-10-24 11:25:02 +02002635{
2636 struct http_hdr_ctx ctx;
2637 struct buffer *output = get_trash_chunk();
2638
Christopher Faulet72333522018-10-24 11:25:02 +02002639 ctx.blk = NULL;
Christopher Faulet92d34fe2019-12-17 09:20:34 +01002640 while (http_find_header(htx, name, &ctx, full)) {
Christopher Faulet72333522018-10-24 11:25:02 +02002641 if (!regex_exec_match2(re, ctx.value.ptr, ctx.value.len, MAX_MATCH, pmatch, 0))
2642 continue;
2643
2644 output->data = exp_replace(output->area, output->size, ctx.value.ptr, str, pmatch);
2645 if (output->data == -1)
2646 return -1;
2647 if (!http_replace_header_value(htx, &ctx, ist2(output->area, output->data)))
2648 return -1;
2649 }
2650 return 0;
2651}
2652
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002653/* This function executes one of the set-{method,path,query,uri} actions. It
2654 * takes the string from the variable 'replace' with length 'len', then modifies
2655 * the relevant part of the request line accordingly. Then it updates various
2656 * pointers to the next elements which were moved, and the total buffer length.
2657 * It finds the action to be performed in p[2], previously filled by function
2658 * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal
2659 * error, though this can be revisited when this code is finally exploited.
2660 *
2661 * 'action' can be '0' to replace method, '1' to replace path, '2' to replace
Christopher Faulet312294f2020-09-02 17:17:44 +02002662 * query string, 3 to replace uri or 4 to replace the path+query.
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002663 *
2664 * In query string case, the mark question '?' must be set at the start of the
2665 * string by the caller, event if the replacement query string is empty.
2666 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002667int http_req_replace_stline(int action, const char *replace, int len,
2668 struct proxy *px, struct stream *s)
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002669{
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002670 struct htx *htx = htxbuf(&s->req.buf);
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002671
2672 switch (action) {
2673 case 0: // method
2674 if (!http_replace_req_meth(htx, ist2(replace, len)))
2675 return -1;
2676 break;
2677
2678 case 1: // path
Christopher Fauletb8ce5052020-08-31 16:11:57 +02002679 if (!http_replace_req_path(htx, ist2(replace, len), 0))
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002680 return -1;
2681 break;
2682
2683 case 2: // query
2684 if (!http_replace_req_query(htx, ist2(replace, len)))
2685 return -1;
2686 break;
2687
2688 case 3: // uri
2689 if (!http_replace_req_uri(htx, ist2(replace, len)))
2690 return -1;
2691 break;
2692
Christopher Faulet312294f2020-09-02 17:17:44 +02002693 case 4: // path + query
2694 if (!http_replace_req_path(htx, ist2(replace, len), 1))
2695 return -1;
2696 break;
2697
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002698 default:
2699 return -1;
2700 }
2701 return 0;
2702}
2703
2704/* This function replace the HTTP status code and the associated message. The
Christopher Faulete00d06c2019-12-16 17:18:42 +01002705 * variable <status> contains the new status code. This function never fails. It
2706 * returns 0 in case of success, -1 in case of internal error.
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002707 */
Christopher Faulet96bff762019-12-17 13:46:18 +01002708int http_res_set_status(unsigned int status, struct ist reason, struct stream *s)
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002709{
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002710 struct htx *htx = htxbuf(&s->res.buf);
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002711 char *res;
2712
2713 chunk_reset(&trash);
2714 res = ultoa_o(status, trash.area, trash.size);
2715 trash.data = res - trash.area;
2716
2717 /* Do we have a custom reason format string? */
Tim Duesterhuse296d3e2020-03-05 17:56:31 +01002718 if (!isttest(reason)) {
Christopher Faulet96bff762019-12-17 13:46:18 +01002719 const char *str = http_get_reason(status);
Tim Duesterhusdcf753a2021-03-04 17:31:47 +01002720 reason = ist(str);
Christopher Faulet96bff762019-12-17 13:46:18 +01002721 }
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002722
Christopher Fauletbde2c4c2020-08-31 16:43:34 +02002723 if (!http_replace_res_status(htx, ist2(trash.area, trash.data), reason))
Christopher Faulete00d06c2019-12-16 17:18:42 +01002724 return -1;
2725 return 0;
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002726}
2727
Christopher Faulet3e964192018-10-24 11:39:23 +02002728/* Executes the http-request rules <rules> for stream <s>, proxy <px> and
2729 * transaction <txn>. Returns the verdict of the first rule that prevents
2730 * further processing of the request (auth, deny, ...), and defaults to
2731 * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or
2732 * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT
2733 * on txn->flags if it encounters a tarpit rule. If <deny_status> is not NULL
2734 * and a deny/tarpit rule is matched, it will be filled with this rule's deny
2735 * status.
2736 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002737static enum rule_result http_req_get_intercept_rule(struct proxy *px, struct list *rules,
Christopher Fauletb58f62b2020-01-13 16:40:13 +01002738 struct stream *s)
Christopher Faulet3e964192018-10-24 11:39:23 +02002739{
2740 struct session *sess = strm_sess(s);
2741 struct http_txn *txn = s->txn;
Christopher Faulet3e964192018-10-24 11:39:23 +02002742 struct act_rule *rule;
Christopher Faulet3e964192018-10-24 11:39:23 +02002743 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002744 int act_opts = 0;
Christopher Faulet3e964192018-10-24 11:39:23 +02002745
Christopher Faulet3e964192018-10-24 11:39:23 +02002746 /* If "the current_rule_list" match the executed rule list, we are in
2747 * resume condition. If a resume is needed it is always in the action
2748 * and never in the ACL or converters. In this case, we initialise the
2749 * current rule, and go to the action execution point.
2750 */
2751 if (s->current_rule) {
2752 rule = s->current_rule;
2753 s->current_rule = NULL;
2754 if (s->current_rule_list == rules)
2755 goto resume_execution;
2756 }
2757 s->current_rule_list = rules;
2758
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002759 /* start the ruleset evaluation in strict mode */
2760 txn->req.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01002761
Christopher Faulet3e964192018-10-24 11:39:23 +02002762 list_for_each_entry(rule, rules, list) {
2763 /* check optional condition */
2764 if (rule->cond) {
2765 int ret;
2766
2767 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
2768 ret = acl_pass(ret);
2769
2770 if (rule->cond->pol == ACL_COND_UNLESS)
2771 ret = !ret;
2772
2773 if (!ret) /* condition not matched */
2774 continue;
2775 }
2776
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002777 act_opts |= ACT_OPT_FIRST;
Christopher Faulet3e964192018-10-24 11:39:23 +02002778 resume_execution:
Amaury Denoyelle03517732021-05-07 14:25:01 +02002779 if (rule->kw->flags & KWF_EXPERIMENTAL)
2780 mark_tainted(TAINTED_ACTION_EXP_EXECUTED);
2781
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002782 /* Always call the action function if defined */
2783 if (rule->action_ptr) {
2784 if ((s->req.flags & CF_READ_ERROR) ||
2785 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
2786 (px->options & PR_O_ABRT_CLOSE)))
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002787 act_opts |= ACT_OPT_FINAL;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002788
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002789 switch (rule->action_ptr(rule, px, sess, s, act_opts)) {
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002790 case ACT_RET_CONT:
2791 break;
2792 case ACT_RET_STOP:
2793 rule_ret = HTTP_RULE_RES_STOP;
2794 goto end;
2795 case ACT_RET_YIELD:
2796 s->current_rule = rule;
2797 rule_ret = HTTP_RULE_RES_YIELD;
2798 goto end;
2799 case ACT_RET_ERR:
2800 rule_ret = HTTP_RULE_RES_ERROR;
2801 goto end;
2802 case ACT_RET_DONE:
2803 rule_ret = HTTP_RULE_RES_DONE;
2804 goto end;
2805 case ACT_RET_DENY:
Christopher Fauletb58f62b2020-01-13 16:40:13 +01002806 if (txn->status == -1)
2807 txn->status = 403;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002808 rule_ret = HTTP_RULE_RES_DENY;
2809 goto end;
2810 case ACT_RET_ABRT:
2811 rule_ret = HTTP_RULE_RES_ABRT;
2812 goto end;
2813 case ACT_RET_INV:
2814 rule_ret = HTTP_RULE_RES_BADREQ;
2815 goto end;
2816 }
2817 continue; /* eval the next rule */
2818 }
2819
2820 /* If not action function defined, check for known actions */
Christopher Faulet3e964192018-10-24 11:39:23 +02002821 switch (rule->action) {
2822 case ACT_ACTION_ALLOW:
2823 rule_ret = HTTP_RULE_RES_STOP;
2824 goto end;
2825
2826 case ACT_ACTION_DENY:
Christopher Faulet5cb513a2020-05-13 17:56:56 +02002827 txn->status = rule->arg.http_reply->status;
2828 txn->http_reply = rule->arg.http_reply;
Christopher Faulet3e964192018-10-24 11:39:23 +02002829 rule_ret = HTTP_RULE_RES_DENY;
2830 goto end;
2831
2832 case ACT_HTTP_REQ_TARPIT:
2833 txn->flags |= TX_CLTARPIT;
Christopher Faulet5cb513a2020-05-13 17:56:56 +02002834 txn->status = rule->arg.http_reply->status;
2835 txn->http_reply = rule->arg.http_reply;
Christopher Faulet3e964192018-10-24 11:39:23 +02002836 rule_ret = HTTP_RULE_RES_DENY;
2837 goto end;
2838
Christopher Faulet3e964192018-10-24 11:39:23 +02002839 case ACT_HTTP_REDIR:
Christopher Faulet90d22a82020-03-06 11:18:39 +01002840 rule_ret = HTTP_RULE_RES_ABRT;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002841 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
Christopher Faulet3a26bee2019-12-16 12:47:40 +01002842 rule_ret = HTTP_RULE_RES_ERROR;
Christopher Faulet3e964192018-10-24 11:39:23 +02002843 goto end;
2844
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002845 /* other flags exists, but normally, they never be matched. */
Christopher Faulet3e964192018-10-24 11:39:23 +02002846 default:
2847 break;
2848 }
2849 }
2850
2851 end:
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002852 /* if the ruleset evaluation is finished reset the strict mode */
Christopher Faulet46f95542019-12-20 10:07:22 +01002853 if (rule_ret != HTTP_RULE_RES_YIELD)
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002854 txn->req.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01002855
Christopher Faulet3e964192018-10-24 11:39:23 +02002856 /* we reached the end of the rules, nothing to report */
2857 return rule_ret;
2858}
2859
2860/* Executes the http-response rules <rules> for stream <s> and proxy <px>. It
2861 * returns one of 5 possible statuses: HTTP_RULE_RES_CONT, HTTP_RULE_RES_STOP,
2862 * HTTP_RULE_RES_DONE, HTTP_RULE_RES_YIELD, or HTTP_RULE_RES_BADREQ. If *CONT
2863 * is returned, the process can continue the evaluation of next rule list. If
2864 * *STOP or *DONE is returned, the process must stop the evaluation. If *BADREQ
2865 * is returned, it means the operation could not be processed and a server error
Christopher Fauleta53abad2020-05-13 08:12:22 +02002866 * must be returned. If *YIELD is returned, the caller must call again the
2867 * function with the same context.
Christopher Faulet3e964192018-10-24 11:39:23 +02002868 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002869static enum rule_result http_res_get_intercept_rule(struct proxy *px, struct list *rules,
2870 struct stream *s)
Christopher Faulet3e964192018-10-24 11:39:23 +02002871{
2872 struct session *sess = strm_sess(s);
2873 struct http_txn *txn = s->txn;
Christopher Faulet3e964192018-10-24 11:39:23 +02002874 struct act_rule *rule;
Christopher Faulet3e964192018-10-24 11:39:23 +02002875 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002876 int act_opts = 0;
Christopher Faulet3e964192018-10-24 11:39:23 +02002877
Christopher Faulet3e964192018-10-24 11:39:23 +02002878 /* If "the current_rule_list" match the executed rule list, we are in
2879 * resume condition. If a resume is needed it is always in the action
2880 * and never in the ACL or converters. In this case, we initialise the
2881 * current rule, and go to the action execution point.
2882 */
2883 if (s->current_rule) {
2884 rule = s->current_rule;
2885 s->current_rule = NULL;
2886 if (s->current_rule_list == rules)
2887 goto resume_execution;
2888 }
2889 s->current_rule_list = rules;
2890
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002891 /* start the ruleset evaluation in strict mode */
2892 txn->rsp.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01002893
Christopher Faulet3e964192018-10-24 11:39:23 +02002894 list_for_each_entry(rule, rules, list) {
2895 /* check optional condition */
2896 if (rule->cond) {
2897 int ret;
2898
2899 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
2900 ret = acl_pass(ret);
2901
2902 if (rule->cond->pol == ACL_COND_UNLESS)
2903 ret = !ret;
2904
2905 if (!ret) /* condition not matched */
2906 continue;
2907 }
2908
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002909 act_opts |= ACT_OPT_FIRST;
Christopher Faulet3e964192018-10-24 11:39:23 +02002910resume_execution:
Amaury Denoyelle03517732021-05-07 14:25:01 +02002911 if (rule->kw->flags & KWF_EXPERIMENTAL)
2912 mark_tainted(TAINTED_ACTION_EXP_EXECUTED);
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002913
2914 /* Always call the action function if defined */
2915 if (rule->action_ptr) {
2916 if ((s->req.flags & CF_READ_ERROR) ||
2917 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
2918 (px->options & PR_O_ABRT_CLOSE)))
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002919 act_opts |= ACT_OPT_FINAL;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002920
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002921 switch (rule->action_ptr(rule, px, sess, s, act_opts)) {
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002922 case ACT_RET_CONT:
2923 break;
2924 case ACT_RET_STOP:
2925 rule_ret = HTTP_RULE_RES_STOP;
2926 goto end;
2927 case ACT_RET_YIELD:
2928 s->current_rule = rule;
2929 rule_ret = HTTP_RULE_RES_YIELD;
2930 goto end;
2931 case ACT_RET_ERR:
2932 rule_ret = HTTP_RULE_RES_ERROR;
2933 goto end;
2934 case ACT_RET_DONE:
2935 rule_ret = HTTP_RULE_RES_DONE;
2936 goto end;
2937 case ACT_RET_DENY:
Christopher Fauletb58f62b2020-01-13 16:40:13 +01002938 if (txn->status == -1)
2939 txn->status = 502;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002940 rule_ret = HTTP_RULE_RES_DENY;
2941 goto end;
2942 case ACT_RET_ABRT:
2943 rule_ret = HTTP_RULE_RES_ABRT;
2944 goto end;
2945 case ACT_RET_INV:
2946 rule_ret = HTTP_RULE_RES_BADREQ;
2947 goto end;
2948 }
2949 continue; /* eval the next rule */
2950 }
2951
2952 /* If not action function defined, check for known actions */
Christopher Faulet3e964192018-10-24 11:39:23 +02002953 switch (rule->action) {
2954 case ACT_ACTION_ALLOW:
2955 rule_ret = HTTP_RULE_RES_STOP; /* "allow" rules are OK */
2956 goto end;
2957
2958 case ACT_ACTION_DENY:
Christopher Faulet5cb513a2020-05-13 17:56:56 +02002959 txn->status = rule->arg.http_reply->status;
2960 txn->http_reply = rule->arg.http_reply;
Christopher Faulet3a26bee2019-12-16 12:47:40 +01002961 rule_ret = HTTP_RULE_RES_DENY;
Christopher Faulet3e964192018-10-24 11:39:23 +02002962 goto end;
2963
Christopher Faulet3e964192018-10-24 11:39:23 +02002964 case ACT_HTTP_REDIR:
Christopher Faulet49c2a702020-03-06 15:44:37 +01002965 rule_ret = HTTP_RULE_RES_ABRT;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002966 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
Christopher Faulet3a26bee2019-12-16 12:47:40 +01002967 rule_ret = HTTP_RULE_RES_ERROR;
Christopher Faulet3e964192018-10-24 11:39:23 +02002968 goto end;
2969
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002970 /* other flags exists, but normally, they never be matched. */
Christopher Faulet3e964192018-10-24 11:39:23 +02002971 default:
2972 break;
2973 }
2974 }
2975
2976 end:
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002977 /* if the ruleset evaluation is finished reset the strict mode */
Christopher Faulet46f95542019-12-20 10:07:22 +01002978 if (rule_ret != HTTP_RULE_RES_YIELD)
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002979 txn->rsp.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01002980
Christopher Faulet3e964192018-10-24 11:39:23 +02002981 /* we reached the end of the rules, nothing to report */
2982 return rule_ret;
2983}
2984
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002985/* Executes backend and frontend http-after-response rules for the stream <s>,
2986 * in that order. it return 1 on success and 0 on error. It is the caller
2987 * responsibility to catch error or ignore it. If it catches it, this function
2988 * may be called a second time, for the internal error.
2989 */
2990int http_eval_after_res_rules(struct stream *s)
2991{
2992 struct session *sess = s->sess;
2993 enum rule_result ret = HTTP_RULE_RES_CONT;
2994
Christopher Faulet507479b2020-05-15 12:29:46 +02002995 /* Eval after-response ruleset only if the reply is not const */
2996 if (s->txn->flags & TX_CONST_REPLY)
2997 goto end;
2998
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002999 /* prune the request variables if not already done and swap to the response variables. */
3000 if (s->vars_reqres.scope != SCOPE_RES) {
3001 if (!LIST_ISEMPTY(&s->vars_reqres.head))
3002 vars_prune(&s->vars_reqres, s->sess, s);
3003 vars_init(&s->vars_reqres, SCOPE_RES);
3004 }
3005
3006 ret = http_res_get_intercept_rule(s->be, &s->be->http_after_res_rules, s);
3007 if ((ret == HTTP_RULE_RES_CONT || ret == HTTP_RULE_RES_STOP) && sess->fe != s->be)
3008 ret = http_res_get_intercept_rule(sess->fe, &sess->fe->http_after_res_rules, s);
3009
Christopher Faulet507479b2020-05-15 12:29:46 +02003010 end:
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01003011 /* All other codes than CONTINUE, STOP or DONE are forbidden */
3012 return (ret == HTTP_RULE_RES_CONT || ret == HTTP_RULE_RES_STOP || ret == HTTP_RULE_RES_DONE);
3013}
3014
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003015/*
3016 * Manage client-side cookie. It can impact performance by about 2% so it is
3017 * desirable to call it only when needed. This code is quite complex because
3018 * of the multiple very crappy and ambiguous syntaxes we have to support. it
3019 * highly recommended not to touch this part without a good reason !
3020 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003021static void http_manage_client_side_cookies(struct stream *s, struct channel *req)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003022{
3023 struct session *sess = s->sess;
3024 struct http_txn *txn = s->txn;
3025 struct htx *htx;
3026 struct http_hdr_ctx ctx;
3027 char *hdr_beg, *hdr_end, *del_from;
3028 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
3029 int preserve_hdr;
3030
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003031 htx = htxbuf(&req->buf);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003032 ctx.blk = NULL;
3033 while (http_find_header(htx, ist("Cookie"), &ctx, 1)) {
Olivier Houchardf0f42382019-07-22 17:43:46 +02003034 int is_first = 1;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003035 del_from = NULL; /* nothing to be deleted */
3036 preserve_hdr = 0; /* assume we may kill the whole header */
3037
3038 /* Now look for cookies. Conforming to RFC2109, we have to support
3039 * attributes whose name begin with a '$', and associate them with
3040 * the right cookie, if we want to delete this cookie.
3041 * So there are 3 cases for each cookie read :
3042 * 1) it's a special attribute, beginning with a '$' : ignore it.
3043 * 2) it's a server id cookie that we *MAY* want to delete : save
3044 * some pointers on it (last semi-colon, beginning of cookie...)
3045 * 3) it's an application cookie : we *MAY* have to delete a previous
3046 * "special" cookie.
3047 * At the end of loop, if a "special" cookie remains, we may have to
3048 * remove it. If no application cookie persists in the header, we
3049 * *MUST* delete it.
3050 *
3051 * Note: RFC2965 is unclear about the processing of spaces around
3052 * the equal sign in the ATTR=VALUE form. A careful inspection of
3053 * the RFC explicitly allows spaces before it, and not within the
3054 * tokens (attrs or values). An inspection of RFC2109 allows that
3055 * too but section 10.1.3 lets one think that spaces may be allowed
3056 * after the equal sign too, resulting in some (rare) buggy
3057 * implementations trying to do that. So let's do what servers do.
3058 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
3059 * allowed quoted strings in values, with any possible character
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003060 * after a backslash, including control chars and delimiters, which
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003061 * causes parsing to become ambiguous. Browsers also allow spaces
3062 * within values even without quotes.
3063 *
3064 * We have to keep multiple pointers in order to support cookie
3065 * removal at the beginning, middle or end of header without
3066 * corrupting the header. All of these headers are valid :
3067 *
3068 * hdr_beg hdr_end
3069 * | |
3070 * v |
3071 * NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3 |
3072 * NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3 v
3073 * NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3
3074 * | | | | | | |
3075 * | | | | | | |
3076 * | | | | | | +--> next
3077 * | | | | | +----> val_end
3078 * | | | | +-----------> val_beg
3079 * | | | +--------------> equal
3080 * | | +----------------> att_end
3081 * | +---------------------> att_beg
3082 * +--------------------------> prev
3083 *
3084 */
3085 hdr_beg = ctx.value.ptr;
3086 hdr_end = hdr_beg + ctx.value.len;
3087 for (prev = hdr_beg; prev < hdr_end; prev = next) {
3088 /* Iterate through all cookies on this line */
3089
3090 /* find att_beg */
3091 att_beg = prev;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003092 if (!is_first)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003093 att_beg++;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003094 is_first = 0;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003095
3096 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
3097 att_beg++;
3098
3099 /* find att_end : this is the first character after the last non
3100 * space before the equal. It may be equal to hdr_end.
3101 */
3102 equal = att_end = att_beg;
3103 while (equal < hdr_end) {
3104 if (*equal == '=' || *equal == ',' || *equal == ';')
3105 break;
3106 if (HTTP_IS_SPHT(*equal++))
3107 continue;
3108 att_end = equal;
3109 }
3110
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003111 /* here, <equal> points to '=', a delimiter or the end. <att_end>
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003112 * is between <att_beg> and <equal>, both may be identical.
3113 */
3114 /* look for end of cookie if there is an equal sign */
3115 if (equal < hdr_end && *equal == '=') {
3116 /* look for the beginning of the value */
3117 val_beg = equal + 1;
3118 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
3119 val_beg++;
3120
3121 /* find the end of the value, respecting quotes */
3122 next = http_find_cookie_value_end(val_beg, hdr_end);
3123
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003124 /* make val_end point to the first white space or delimiter after the value */
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003125 val_end = next;
3126 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
3127 val_end--;
3128 }
3129 else
3130 val_beg = val_end = next = equal;
3131
3132 /* We have nothing to do with attributes beginning with
3133 * '$'. However, they will automatically be removed if a
3134 * header before them is removed, since they're supposed
3135 * to be linked together.
3136 */
3137 if (*att_beg == '$')
3138 continue;
3139
3140 /* Ignore cookies with no equal sign */
3141 if (equal == next) {
3142 /* This is not our cookie, so we must preserve it. But if we already
3143 * scheduled another cookie for removal, we cannot remove the
3144 * complete header, but we can remove the previous block itself.
3145 */
3146 preserve_hdr = 1;
3147 if (del_from != NULL) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003148 int delta = http_del_hdr_value(hdr_beg, hdr_end, &del_from, prev);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003149 val_end += delta;
3150 next += delta;
3151 hdr_end += delta;
3152 prev = del_from;
3153 del_from = NULL;
3154 }
3155 continue;
3156 }
3157
3158 /* if there are spaces around the equal sign, we need to
3159 * strip them otherwise we'll get trouble for cookie captures,
3160 * or even for rewrites. Since this happens extremely rarely,
3161 * it does not hurt performance.
3162 */
3163 if (unlikely(att_end != equal || val_beg > equal + 1)) {
3164 int stripped_before = 0;
3165 int stripped_after = 0;
3166
3167 if (att_end != equal) {
3168 memmove(att_end, equal, hdr_end - equal);
3169 stripped_before = (att_end - equal);
3170 equal += stripped_before;
3171 val_beg += stripped_before;
3172 }
3173
3174 if (val_beg > equal + 1) {
3175 memmove(equal + 1, val_beg, hdr_end + stripped_before - val_beg);
3176 stripped_after = (equal + 1) - val_beg;
3177 val_beg += stripped_after;
3178 stripped_before += stripped_after;
3179 }
3180
3181 val_end += stripped_before;
3182 next += stripped_before;
3183 hdr_end += stripped_before;
3184 }
3185 /* now everything is as on the diagram above */
3186
3187 /* First, let's see if we want to capture this cookie. We check
3188 * that we don't already have a client side cookie, because we
3189 * can only capture one. Also as an optimisation, we ignore
3190 * cookies shorter than the declared name.
3191 */
3192 if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL &&
3193 (val_end - att_beg >= sess->fe->capture_namelen) &&
3194 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
3195 int log_len = val_end - att_beg;
3196
3197 if ((txn->cli_cookie = pool_alloc(pool_head_capture)) == NULL) {
3198 ha_alert("HTTP logging : out of memory.\n");
3199 } else {
3200 if (log_len > sess->fe->capture_len)
3201 log_len = sess->fe->capture_len;
3202 memcpy(txn->cli_cookie, att_beg, log_len);
3203 txn->cli_cookie[log_len] = 0;
3204 }
3205 }
3206
3207 /* Persistence cookies in passive, rewrite or insert mode have the
3208 * following form :
3209 *
3210 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
3211 *
3212 * For cookies in prefix mode, the form is :
3213 *
3214 * Cookie: NAME=SRV~VALUE
3215 */
3216 if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
3217 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
3218 struct server *srv = s->be->srv;
3219 char *delim;
3220
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003221 /* if we're in cookie prefix mode, we'll search the delimiter so that we
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003222 * have the server ID between val_beg and delim, and the original cookie between
3223 * delim+1 and val_end. Otherwise, delim==val_end :
3224 *
3225 * hdr_beg
3226 * |
3227 * v
3228 * NAME=SRV; # in all but prefix modes
3229 * NAME=SRV~OPAQUE ; # in prefix mode
3230 * || || | |+-> next
3231 * || || | +--> val_end
3232 * || || +---------> delim
3233 * || |+------------> val_beg
3234 * || +-------------> att_end = equal
3235 * |+-----------------> att_beg
3236 * +------------------> prev
3237 *
3238 */
3239 if (s->be->ck_opts & PR_CK_PFX) {
3240 for (delim = val_beg; delim < val_end; delim++)
3241 if (*delim == COOKIE_DELIM)
3242 break;
3243 }
3244 else {
3245 char *vbar1;
3246 delim = val_end;
3247 /* Now check if the cookie contains a date field, which would
3248 * appear after a vertical bar ('|') just after the server name
3249 * and before the delimiter.
3250 */
3251 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
3252 if (vbar1) {
3253 /* OK, so left of the bar is the server's cookie and
3254 * right is the last seen date. It is a base64 encoded
3255 * 30-bit value representing the UNIX date since the
3256 * epoch in 4-second quantities.
3257 */
3258 int val;
3259 delim = vbar1++;
3260 if (val_end - vbar1 >= 5) {
3261 val = b64tos30(vbar1);
3262 if (val > 0)
3263 txn->cookie_last_date = val << 2;
3264 }
3265 /* look for a second vertical bar */
3266 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
3267 if (vbar1 && (val_end - vbar1 > 5)) {
3268 val = b64tos30(vbar1 + 1);
3269 if (val > 0)
3270 txn->cookie_first_date = val << 2;
3271 }
3272 }
3273 }
3274
3275 /* if the cookie has an expiration date and the proxy wants to check
3276 * it, then we do that now. We first check if the cookie is too old,
3277 * then only if it has expired. We detect strict overflow because the
3278 * time resolution here is not great (4 seconds). Cookies with dates
3279 * in the future are ignored if their offset is beyond one day. This
3280 * allows an admin to fix timezone issues without expiring everyone
3281 * and at the same time avoids keeping unwanted side effects for too
3282 * long.
3283 */
3284 if (txn->cookie_first_date && s->be->cookie_maxlife &&
3285 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) ||
3286 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
3287 txn->flags &= ~TX_CK_MASK;
3288 txn->flags |= TX_CK_OLD;
3289 delim = val_beg; // let's pretend we have not found the cookie
3290 txn->cookie_first_date = 0;
3291 txn->cookie_last_date = 0;
3292 }
3293 else if (txn->cookie_last_date && s->be->cookie_maxidle &&
3294 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) ||
3295 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
3296 txn->flags &= ~TX_CK_MASK;
3297 txn->flags |= TX_CK_EXPIRED;
3298 delim = val_beg; // let's pretend we have not found the cookie
3299 txn->cookie_first_date = 0;
3300 txn->cookie_last_date = 0;
3301 }
3302
3303 /* Here, we'll look for the first running server which supports the cookie.
3304 * This allows to share a same cookie between several servers, for example
3305 * to dedicate backup servers to specific servers only.
3306 * However, to prevent clients from sticking to cookie-less backup server
3307 * when they have incidentely learned an empty cookie, we simply ignore
3308 * empty cookies and mark them as invalid.
3309 * The same behaviour is applied when persistence must be ignored.
3310 */
3311 if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
3312 srv = NULL;
3313
3314 while (srv) {
3315 if (srv->cookie && (srv->cklen == delim - val_beg) &&
3316 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
3317 if ((srv->cur_state != SRV_ST_STOPPED) ||
3318 (s->be->options & PR_O_PERSIST) ||
3319 (s->flags & SF_FORCE_PRST)) {
3320 /* we found the server and we can use it */
3321 txn->flags &= ~TX_CK_MASK;
3322 txn->flags |= (srv->cur_state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
3323 s->flags |= SF_DIRECT | SF_ASSIGNED;
3324 s->target = &srv->obj_type;
3325 break;
3326 } else {
3327 /* we found a server, but it's down,
3328 * mark it as such and go on in case
3329 * another one is available.
3330 */
3331 txn->flags &= ~TX_CK_MASK;
3332 txn->flags |= TX_CK_DOWN;
3333 }
3334 }
3335 srv = srv->next;
3336 }
3337
3338 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
3339 /* no server matched this cookie or we deliberately skipped it */
3340 txn->flags &= ~TX_CK_MASK;
3341 if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
3342 txn->flags |= TX_CK_UNUSED;
3343 else
3344 txn->flags |= TX_CK_INVALID;
3345 }
3346
3347 /* depending on the cookie mode, we may have to either :
3348 * - delete the complete cookie if we're in insert+indirect mode, so that
3349 * the server never sees it ;
3350 * - remove the server id from the cookie value, and tag the cookie as an
Joseph Herlante9d5c722018-11-25 11:00:25 -08003351 * application cookie so that it does not get accidentally removed later,
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003352 * if we're in cookie prefix mode
3353 */
3354 if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
3355 int delta; /* negative */
3356
3357 memmove(val_beg, delim + 1, hdr_end - (delim + 1));
3358 delta = val_beg - (delim + 1);
3359 val_end += delta;
3360 next += delta;
3361 hdr_end += delta;
3362 del_from = NULL;
3363 preserve_hdr = 1; /* we want to keep this cookie */
3364 }
3365 else if (del_from == NULL &&
3366 (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
3367 del_from = prev;
3368 }
3369 }
3370 else {
3371 /* This is not our cookie, so we must preserve it. But if we already
3372 * scheduled another cookie for removal, we cannot remove the
3373 * complete header, but we can remove the previous block itself.
3374 */
3375 preserve_hdr = 1;
3376
3377 if (del_from != NULL) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003378 int delta = http_del_hdr_value(hdr_beg, hdr_end, &del_from, prev);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003379 if (att_beg >= del_from)
3380 att_beg += delta;
3381 if (att_end >= del_from)
3382 att_end += delta;
3383 val_beg += delta;
3384 val_end += delta;
3385 next += delta;
3386 hdr_end += delta;
3387 prev = del_from;
3388 del_from = NULL;
3389 }
3390 }
3391
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003392 } /* for each cookie */
3393
3394
3395 /* There are no more cookies on this line.
3396 * We may still have one (or several) marked for deletion at the
3397 * end of the line. We must do this now in two ways :
3398 * - if some cookies must be preserved, we only delete from the
3399 * mark to the end of line ;
3400 * - if nothing needs to be preserved, simply delete the whole header
3401 */
3402 if (del_from) {
3403 hdr_end = (preserve_hdr ? del_from : hdr_beg);
3404 }
3405 if ((hdr_end - hdr_beg) != ctx.value.len) {
Christopher Faulet3e2638e2019-06-18 09:49:16 +02003406 if (hdr_beg != hdr_end)
3407 htx_change_blk_value_len(htx, ctx.blk, hdr_end - hdr_beg);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003408 else
3409 http_remove_header(htx, &ctx);
3410 }
3411 } /* for each "Cookie header */
3412}
3413
3414/*
3415 * Manage server-side cookies. It can impact performance by about 2% so it is
3416 * desirable to call it only when needed. This function is also used when we
3417 * just need to know if there is a cookie (eg: for check-cache).
3418 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003419static void http_manage_server_side_cookies(struct stream *s, struct channel *res)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003420{
3421 struct session *sess = s->sess;
3422 struct http_txn *txn = s->txn;
3423 struct htx *htx;
3424 struct http_hdr_ctx ctx;
3425 struct server *srv;
3426 char *hdr_beg, *hdr_end;
3427 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau6f7a02a2019-04-15 21:49:49 +02003428 int is_cookie2 = 0;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003429
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003430 htx = htxbuf(&res->buf);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003431
3432 ctx.blk = NULL;
3433 while (1) {
Olivier Houchardf0f42382019-07-22 17:43:46 +02003434 int is_first = 1;
3435
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003436 if (!http_find_header(htx, ist("Set-Cookie"), &ctx, 1)) {
3437 if (!http_find_header(htx, ist("Set-Cookie2"), &ctx, 1))
3438 break;
3439 is_cookie2 = 1;
3440 }
3441
3442 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
3443 * <prev> points to the colon.
3444 */
3445 txn->flags |= TX_SCK_PRESENT;
3446
3447 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
3448 * check-cache is enabled) and we are not interested in checking
3449 * them. Warning, the cookie capture is declared in the frontend.
3450 */
3451 if (s->be->cookie_name == NULL && sess->fe->capture_name == NULL)
3452 break;
3453
3454 /* OK so now we know we have to process this response cookie.
3455 * The format of the Set-Cookie header is slightly different
3456 * from the format of the Cookie header in that it does not
3457 * support the comma as a cookie delimiter (thus the header
3458 * cannot be folded) because the Expires attribute described in
3459 * the original Netscape's spec may contain an unquoted date
3460 * with a comma inside. We have to live with this because
3461 * many browsers don't support Max-Age and some browsers don't
3462 * support quoted strings. However the Set-Cookie2 header is
3463 * clean.
3464 *
3465 * We have to keep multiple pointers in order to support cookie
3466 * removal at the beginning, middle or end of header without
3467 * corrupting the header (in case of set-cookie2). A special
3468 * pointer, <scav> points to the beginning of the set-cookie-av
3469 * fields after the first semi-colon. The <next> pointer points
3470 * either to the end of line (set-cookie) or next unquoted comma
3471 * (set-cookie2). All of these headers are valid :
3472 *
3473 * hdr_beg hdr_end
3474 * | |
3475 * v |
3476 * NAME1 = VALUE 1 ; Secure; Path="/" |
3477 * NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT v
3478 * NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT
3479 * NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard
3480 * | | | | | | | |
3481 * | | | | | | | +-> next
3482 * | | | | | | +------------> scav
3483 * | | | | | +--------------> val_end
3484 * | | | | +--------------------> val_beg
3485 * | | | +----------------------> equal
3486 * | | +------------------------> att_end
3487 * | +----------------------------> att_beg
3488 * +------------------------------> prev
3489 * -------------------------------> hdr_beg
3490 */
3491 hdr_beg = ctx.value.ptr;
3492 hdr_end = hdr_beg + ctx.value.len;
3493 for (prev = hdr_beg; prev < hdr_end; prev = next) {
3494
3495 /* Iterate through all cookies on this line */
3496
3497 /* find att_beg */
3498 att_beg = prev;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003499 if (!is_first)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003500 att_beg++;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003501 is_first = 0;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003502
3503 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
3504 att_beg++;
3505
3506 /* find att_end : this is the first character after the last non
3507 * space before the equal. It may be equal to hdr_end.
3508 */
3509 equal = att_end = att_beg;
3510
3511 while (equal < hdr_end) {
3512 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
3513 break;
3514 if (HTTP_IS_SPHT(*equal++))
3515 continue;
3516 att_end = equal;
3517 }
3518
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003519 /* here, <equal> points to '=', a delimiter or the end. <att_end>
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003520 * is between <att_beg> and <equal>, both may be identical.
3521 */
3522
3523 /* look for end of cookie if there is an equal sign */
3524 if (equal < hdr_end && *equal == '=') {
3525 /* look for the beginning of the value */
3526 val_beg = equal + 1;
3527 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
3528 val_beg++;
3529
3530 /* find the end of the value, respecting quotes */
3531 next = http_find_cookie_value_end(val_beg, hdr_end);
3532
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003533 /* make val_end point to the first white space or delimiter after the value */
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003534 val_end = next;
3535 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
3536 val_end--;
3537 }
3538 else {
3539 /* <equal> points to next comma, semi-colon or EOL */
3540 val_beg = val_end = next = equal;
3541 }
3542
3543 if (next < hdr_end) {
3544 /* Set-Cookie2 supports multiple cookies, and <next> points to
3545 * a colon or semi-colon before the end. So skip all attr-value
3546 * pairs and look for the next comma. For Set-Cookie, since
3547 * commas are permitted in values, skip to the end.
3548 */
3549 if (is_cookie2)
3550 next = http_find_hdr_value_end(next, hdr_end);
3551 else
3552 next = hdr_end;
3553 }
3554
3555 /* Now everything is as on the diagram above */
3556
3557 /* Ignore cookies with no equal sign */
3558 if (equal == val_end)
3559 continue;
3560
3561 /* If there are spaces around the equal sign, we need to
3562 * strip them otherwise we'll get trouble for cookie captures,
3563 * or even for rewrites. Since this happens extremely rarely,
3564 * it does not hurt performance.
3565 */
3566 if (unlikely(att_end != equal || val_beg > equal + 1)) {
3567 int stripped_before = 0;
3568 int stripped_after = 0;
3569
3570 if (att_end != equal) {
3571 memmove(att_end, equal, hdr_end - equal);
3572 stripped_before = (att_end - equal);
3573 equal += stripped_before;
3574 val_beg += stripped_before;
3575 }
3576
3577 if (val_beg > equal + 1) {
3578 memmove(equal + 1, val_beg, hdr_end + stripped_before - val_beg);
3579 stripped_after = (equal + 1) - val_beg;
3580 val_beg += stripped_after;
3581 stripped_before += stripped_after;
3582 }
3583
3584 val_end += stripped_before;
3585 next += stripped_before;
3586 hdr_end += stripped_before;
3587
Christopher Faulet3e2638e2019-06-18 09:49:16 +02003588 htx_change_blk_value_len(htx, ctx.blk, hdr_end - hdr_beg);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003589 ctx.value.len = hdr_end - hdr_beg;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003590 }
3591
3592 /* First, let's see if we want to capture this cookie. We check
3593 * that we don't already have a server side cookie, because we
3594 * can only capture one. Also as an optimisation, we ignore
3595 * cookies shorter than the declared name.
3596 */
3597 if (sess->fe->capture_name != NULL &&
3598 txn->srv_cookie == NULL &&
3599 (val_end - att_beg >= sess->fe->capture_namelen) &&
3600 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
3601 int log_len = val_end - att_beg;
3602 if ((txn->srv_cookie = pool_alloc(pool_head_capture)) == NULL) {
3603 ha_alert("HTTP logging : out of memory.\n");
3604 }
3605 else {
3606 if (log_len > sess->fe->capture_len)
3607 log_len = sess->fe->capture_len;
3608 memcpy(txn->srv_cookie, att_beg, log_len);
3609 txn->srv_cookie[log_len] = 0;
3610 }
3611 }
3612
3613 srv = objt_server(s->target);
3614 /* now check if we need to process it for persistence */
3615 if (!(s->flags & SF_IGNORE_PRST) &&
3616 (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
3617 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
3618 /* assume passive cookie by default */
3619 txn->flags &= ~TX_SCK_MASK;
3620 txn->flags |= TX_SCK_FOUND;
3621
3622 /* If the cookie is in insert mode on a known server, we'll delete
3623 * this occurrence because we'll insert another one later.
3624 * We'll delete it too if the "indirect" option is set and we're in
3625 * a direct access.
3626 */
3627 if (s->be->ck_opts & PR_CK_PSV) {
3628 /* The "preserve" flag was set, we don't want to touch the
3629 * server's cookie.
3630 */
3631 }
3632 else if ((srv && (s->be->ck_opts & PR_CK_INS)) ||
3633 ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) {
3634 /* this cookie must be deleted */
3635 if (prev == hdr_beg && next == hdr_end) {
3636 /* whole header */
3637 http_remove_header(htx, &ctx);
3638 /* note: while both invalid now, <next> and <hdr_end>
3639 * are still equal, so the for() will stop as expected.
3640 */
3641 } else {
3642 /* just remove the value */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003643 int delta = http_del_hdr_value(hdr_beg, hdr_end, &prev, next);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003644 next = prev;
3645 hdr_end += delta;
3646 }
3647 txn->flags &= ~TX_SCK_MASK;
3648 txn->flags |= TX_SCK_DELETED;
3649 /* and go on with next cookie */
3650 }
3651 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) {
3652 /* replace bytes val_beg->val_end with the cookie name associated
3653 * with this server since we know it.
3654 */
3655 int sliding, delta;
3656
3657 ctx.value = ist2(val_beg, val_end - val_beg);
3658 ctx.lws_before = ctx.lws_after = 0;
3659 http_replace_header_value(htx, &ctx, ist2(srv->cookie, srv->cklen));
3660 delta = srv->cklen - (val_end - val_beg);
3661 sliding = (ctx.value.ptr - val_beg);
3662 hdr_beg += sliding;
3663 val_beg += sliding;
3664 next += sliding + delta;
3665 hdr_end += sliding + delta;
3666
3667 txn->flags &= ~TX_SCK_MASK;
3668 txn->flags |= TX_SCK_REPLACED;
3669 }
3670 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) {
3671 /* insert the cookie name associated with this server
3672 * before existing cookie, and insert a delimiter between them..
3673 */
3674 int sliding, delta;
3675 ctx.value = ist2(val_beg, 0);
3676 ctx.lws_before = ctx.lws_after = 0;
3677 http_replace_header_value(htx, &ctx, ist2(srv->cookie, srv->cklen + 1));
3678 delta = srv->cklen + 1;
3679 sliding = (ctx.value.ptr - val_beg);
3680 hdr_beg += sliding;
3681 val_beg += sliding;
3682 next += sliding + delta;
3683 hdr_end += sliding + delta;
3684
3685 val_beg[srv->cklen] = COOKIE_DELIM;
3686 txn->flags &= ~TX_SCK_MASK;
3687 txn->flags |= TX_SCK_REPLACED;
3688 }
3689 }
3690 /* that's done for this cookie, check the next one on the same
3691 * line when next != hdr_end (only if is_cookie2).
3692 */
3693 }
3694 }
3695}
3696
Christopher Faulet25a02f62018-10-24 12:00:25 +02003697/*
3698 * Parses the Cache-Control and Pragma request header fields to determine if
3699 * the request may be served from the cache and/or if it is cacheable. Updates
3700 * s->txn->flags.
3701 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003702void http_check_request_for_cacheability(struct stream *s, struct channel *req)
Christopher Faulet25a02f62018-10-24 12:00:25 +02003703{
3704 struct http_txn *txn = s->txn;
3705 struct htx *htx;
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003706 struct http_hdr_ctx ctx = { .blk = NULL };
3707 int pragma_found, cc_found;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003708
3709 if ((txn->flags & (TX_CACHEABLE|TX_CACHE_IGNORE)) == TX_CACHE_IGNORE)
3710 return; /* nothing more to do here */
3711
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003712 htx = htxbuf(&req->buf);
Christopher Faulet25a02f62018-10-24 12:00:25 +02003713 pragma_found = cc_found = 0;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003714
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003715 /* Check "pragma" header for HTTP/1.0 compatibility. */
3716 if (http_find_header(htx, ist("pragma"), &ctx, 1)) {
3717 if (isteqi(ctx.value, ist("no-cache"))) {
3718 pragma_found = 1;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003719 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003720 }
Christopher Faulet25a02f62018-10-24 12:00:25 +02003721
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003722 ctx.blk = NULL;
3723 /* Don't use the cache and don't try to store if we found the
3724 * Authorization header */
3725 if (http_find_header(htx, ist("authorization"), &ctx, 1)) {
3726 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3727 txn->flags |= TX_CACHE_IGNORE;
3728 }
Christopher Faulet25a02f62018-10-24 12:00:25 +02003729
Christopher Faulet25a02f62018-10-24 12:00:25 +02003730
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003731 /* Look for "cache-control" header and iterate over all the values
3732 * until we find one that specifies that caching is possible or not. */
3733 ctx.blk = NULL;
3734 while (http_find_header(htx, ist("cache-control"), &ctx, 0)) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003735 cc_found = 1;
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003736 /* We don't check the values after max-age, max-stale nor min-fresh,
3737 * we simply don't use the cache when they're specified. */
3738 if (istmatchi(ctx.value, ist("max-age")) ||
3739 istmatchi(ctx.value, ist("no-cache")) ||
3740 istmatchi(ctx.value, ist("max-stale")) ||
3741 istmatchi(ctx.value, ist("min-fresh"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003742 txn->flags |= TX_CACHE_IGNORE;
3743 continue;
3744 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003745 if (istmatchi(ctx.value, ist("no-store"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003746 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3747 continue;
3748 }
3749 }
3750
3751 /* RFC7234#5.4:
3752 * When the Cache-Control header field is also present and
3753 * understood in a request, Pragma is ignored.
3754 * When the Cache-Control header field is not present in a
3755 * request, caches MUST consider the no-cache request
3756 * pragma-directive as having the same effect as if
3757 * "Cache-Control: no-cache" were present.
3758 */
3759 if (!cc_found && pragma_found)
3760 txn->flags |= TX_CACHE_IGNORE;
3761}
3762
3763/*
3764 * Check if response is cacheable or not. Updates s->txn->flags.
3765 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003766void http_check_response_for_cacheability(struct stream *s, struct channel *res)
Christopher Faulet25a02f62018-10-24 12:00:25 +02003767{
3768 struct http_txn *txn = s->txn;
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003769 struct http_hdr_ctx ctx = { .blk = NULL };
Christopher Faulet25a02f62018-10-24 12:00:25 +02003770 struct htx *htx;
Remi Tricot-Le Bretoncc9bf2e2020-11-12 11:14:41 +01003771 int has_freshness_info = 0;
3772 int has_validator = 0;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003773
3774 if (txn->status < 200) {
3775 /* do not try to cache interim responses! */
3776 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3777 return;
3778 }
3779
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003780 htx = htxbuf(&res->buf);
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003781 /* Check "pragma" header for HTTP/1.0 compatibility. */
3782 if (http_find_header(htx, ist("pragma"), &ctx, 1)) {
3783 if (isteqi(ctx.value, ist("no-cache"))) {
3784 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3785 return;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003786 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003787 }
Christopher Faulet25a02f62018-10-24 12:00:25 +02003788
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003789 /* Look for "cache-control" header and iterate over all the values
3790 * until we find one that specifies that caching is possible or not. */
3791 ctx.blk = NULL;
3792 while (http_find_header(htx, ist("cache-control"), &ctx, 0)) {
3793 if (isteqi(ctx.value, ist("public"))) {
3794 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003795 continue;
3796 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003797 if (isteqi(ctx.value, ist("private")) ||
3798 isteqi(ctx.value, ist("no-cache")) ||
3799 isteqi(ctx.value, ist("no-store")) ||
3800 isteqi(ctx.value, ist("max-age=0")) ||
3801 isteqi(ctx.value, ist("s-maxage=0"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003802 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003803 continue;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003804 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003805 /* We might have a no-cache="set-cookie" form. */
3806 if (istmatchi(ctx.value, ist("no-cache=\"set-cookie"))) {
3807 txn->flags &= ~TX_CACHE_COOK;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003808 continue;
3809 }
Remi Tricot-Le Bretoncc9bf2e2020-11-12 11:14:41 +01003810
3811 if (istmatchi(ctx.value, ist("s-maxage")) ||
3812 istmatchi(ctx.value, ist("max-age"))) {
3813 has_freshness_info = 1;
3814 continue;
3815 }
3816 }
3817
3818 /* If no freshness information could be found in Cache-Control values,
3819 * look for an Expires header. */
3820 if (!has_freshness_info) {
3821 ctx.blk = NULL;
3822 has_freshness_info = http_find_header(htx, ist("expires"), &ctx, 0);
Christopher Faulet25a02f62018-10-24 12:00:25 +02003823 }
Remi Tricot-Le Bretoncc9bf2e2020-11-12 11:14:41 +01003824
3825 /* If no freshness information could be found in Cache-Control or Expires
3826 * values, look for an explicit validator. */
3827 if (!has_freshness_info) {
3828 ctx.blk = NULL;
3829 has_validator = 1;
3830 if (!http_find_header(htx, ist("etag"), &ctx, 0)) {
3831 ctx.blk = NULL;
3832 if (!http_find_header(htx, ist("last-modified"), &ctx, 0))
3833 has_validator = 0;
3834 }
3835 }
3836
3837 /* We won't store an entry that has neither a cache validator nor an
3838 * explicit expiration time, as suggested in RFC 7234#3. */
3839 if (!has_freshness_info && !has_validator)
3840 txn->flags |= TX_CACHE_IGNORE;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003841}
3842
Christopher Faulet377c5a52018-10-24 21:21:30 +02003843/*
3844 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
3845 * for the current backend.
3846 *
3847 * It is assumed that the request is either a HEAD, GET, or POST and that the
3848 * uri_auth field is valid.
3849 *
3850 * Returns 1 if stats should be provided, otherwise 0.
3851 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003852static int http_stats_check_uri(struct stream *s, struct http_txn *txn, struct proxy *backend)
Christopher Faulet377c5a52018-10-24 21:21:30 +02003853{
3854 struct uri_auth *uri_auth = backend->uri_auth;
3855 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003856 struct htx_sl *sl;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003857 struct ist uri;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003858
3859 if (!uri_auth)
3860 return 0;
3861
3862 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
3863 return 0;
3864
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003865 htx = htxbuf(&s->req.buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02003866 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003867 uri = htx_sl_req_uri(sl);
Amaury Denoyellec453f952021-07-06 11:40:12 +02003868 if (*uri_auth->uri_prefix == '/') {
3869 struct http_uri_parser parser = http_uri_parser_init(uri);
3870 uri = http_parse_path(&parser);
3871 }
Christopher Faulet377c5a52018-10-24 21:21:30 +02003872
3873 /* check URI size */
3874 if (uri_auth->uri_len > uri.len)
3875 return 0;
3876
3877 if (memcmp(uri.ptr, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
3878 return 0;
3879
3880 return 1;
3881}
3882
3883/* This function prepares an applet to handle the stats. It can deal with the
3884 * "100-continue" expectation, check that admin rules are met for POST requests,
3885 * and program a response message if something was unexpected. It cannot fail
3886 * and always relies on the stats applet to complete the job. It does not touch
3887 * analysers nor counters, which are left to the caller. It does not touch
3888 * s->target which is supposed to already point to the stats applet. The caller
3889 * is expected to have already assigned an appctx to the stream.
3890 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003891static int http_handle_stats(struct stream *s, struct channel *req)
Christopher Faulet377c5a52018-10-24 21:21:30 +02003892{
3893 struct stats_admin_rule *stats_admin_rule;
3894 struct stream_interface *si = &s->si[1];
3895 struct session *sess = s->sess;
3896 struct http_txn *txn = s->txn;
3897 struct http_msg *msg = &txn->req;
3898 struct uri_auth *uri_auth = s->be->uri_auth;
3899 const char *h, *lookup, *end;
3900 struct appctx *appctx;
3901 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003902 struct htx_sl *sl;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003903
3904 appctx = si_appctx(si);
3905 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
3906 appctx->st1 = appctx->st2 = 0;
3907 appctx->ctx.stats.st_code = STAT_STATUS_INIT;
Willy Tarreau676c29e2019-10-09 10:50:01 +02003908 appctx->ctx.stats.flags |= uri_auth->flags;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003909 appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */
3910 if ((msg->flags & HTTP_MSGF_VER_11) && (txn->meth != HTTP_METH_HEAD))
3911 appctx->ctx.stats.flags |= STAT_CHUNKED;
3912
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003913 htx = htxbuf(&req->buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02003914 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003915 lookup = HTX_SL_REQ_UPTR(sl) + uri_auth->uri_len;
3916 end = HTX_SL_REQ_UPTR(sl) + HTX_SL_REQ_ULEN(sl);
Christopher Faulet377c5a52018-10-24 21:21:30 +02003917
3918 for (h = lookup; h <= end - 3; h++) {
3919 if (memcmp(h, ";up", 3) == 0) {
3920 appctx->ctx.stats.flags |= STAT_HIDE_DOWN;
3921 break;
3922 }
Amaury Denoyelle91e55ea2021-02-25 14:46:08 +01003923 }
3924
3925 for (h = lookup; h <= end - 9; h++) {
3926 if (memcmp(h, ";no-maint", 9) == 0) {
Willy Tarreau3e320362020-10-23 17:28:57 +02003927 appctx->ctx.stats.flags |= STAT_HIDE_MAINT;
3928 break;
3929 }
Christopher Faulet377c5a52018-10-24 21:21:30 +02003930 }
3931
3932 if (uri_auth->refresh) {
3933 for (h = lookup; h <= end - 10; h++) {
3934 if (memcmp(h, ";norefresh", 10) == 0) {
3935 appctx->ctx.stats.flags |= STAT_NO_REFRESH;
3936 break;
3937 }
3938 }
3939 }
3940
3941 for (h = lookup; h <= end - 4; h++) {
3942 if (memcmp(h, ";csv", 4) == 0) {
Christopher Faulet6338a082019-09-09 15:50:54 +02003943 appctx->ctx.stats.flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
Christopher Faulet377c5a52018-10-24 21:21:30 +02003944 break;
3945 }
3946 }
3947
3948 for (h = lookup; h <= end - 6; h++) {
3949 if (memcmp(h, ";typed", 6) == 0) {
Christopher Faulet6338a082019-09-09 15:50:54 +02003950 appctx->ctx.stats.flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
Christopher Faulet377c5a52018-10-24 21:21:30 +02003951 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
3952 break;
3953 }
3954 }
3955
Christopher Faulet6338a082019-09-09 15:50:54 +02003956 for (h = lookup; h <= end - 5; h++) {
3957 if (memcmp(h, ";json", 5) == 0) {
3958 appctx->ctx.stats.flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
3959 appctx->ctx.stats.flags |= STAT_FMT_JSON;
3960 break;
3961 }
3962 }
3963
3964 for (h = lookup; h <= end - 12; h++) {
3965 if (memcmp(h, ";json-schema", 12) == 0) {
3966 appctx->ctx.stats.flags &= ~STAT_FMT_MASK;
3967 appctx->ctx.stats.flags |= STAT_JSON_SCHM;
3968 break;
3969 }
3970 }
3971
Christopher Faulet377c5a52018-10-24 21:21:30 +02003972 for (h = lookup; h <= end - 8; h++) {
3973 if (memcmp(h, ";st=", 4) == 0) {
3974 int i;
3975 h += 4;
3976 appctx->ctx.stats.st_code = STAT_STATUS_UNKN;
3977 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
3978 if (strncmp(stat_status_codes[i], h, 4) == 0) {
3979 appctx->ctx.stats.st_code = i;
3980 break;
3981 }
3982 }
3983 break;
3984 }
3985 }
3986
3987 appctx->ctx.stats.scope_str = 0;
3988 appctx->ctx.stats.scope_len = 0;
3989 for (h = lookup; h <= end - 8; h++) {
3990 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
3991 int itx = 0;
3992 const char *h2;
3993 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
3994 const char *err;
3995
3996 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
3997 h2 = h;
Christopher Fauleted7a0662019-01-14 11:07:34 +01003998 appctx->ctx.stats.scope_str = h2 - HTX_SL_REQ_UPTR(sl);
3999 while (h < end) {
Christopher Faulet377c5a52018-10-24 21:21:30 +02004000 if (*h == ';' || *h == '&' || *h == ' ')
4001 break;
4002 itx++;
4003 h++;
4004 }
4005
4006 if (itx > STAT_SCOPE_TXT_MAXLEN)
4007 itx = STAT_SCOPE_TXT_MAXLEN;
4008 appctx->ctx.stats.scope_len = itx;
4009
4010 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
4011 memcpy(scope_txt, h2, itx);
4012 scope_txt[itx] = '\0';
4013 err = invalid_char(scope_txt);
4014 if (err) {
4015 /* bad char in search text => clear scope */
4016 appctx->ctx.stats.scope_str = 0;
4017 appctx->ctx.stats.scope_len = 0;
4018 }
4019 break;
4020 }
4021 }
4022
4023 /* now check whether we have some admin rules for this request */
4024 list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) {
4025 int ret = 1;
4026
4027 if (stats_admin_rule->cond) {
4028 ret = acl_exec_cond(stats_admin_rule->cond, s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
4029 ret = acl_pass(ret);
4030 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
4031 ret = !ret;
4032 }
4033
4034 if (ret) {
4035 /* no rule, or the rule matches */
4036 appctx->ctx.stats.flags |= STAT_ADMIN;
4037 break;
4038 }
4039 }
4040
Christopher Faulet5d45e382019-02-27 15:15:23 +01004041 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
4042 appctx->st0 = STAT_HTTP_HEAD;
4043 else if (txn->meth == HTTP_METH_POST) {
Christopher Fauletbd9e8422019-08-15 22:26:48 +02004044 if (appctx->ctx.stats.flags & STAT_ADMIN) {
Christopher Faulet377c5a52018-10-24 21:21:30 +02004045 appctx->st0 = STAT_HTTP_POST;
Christopher Fauletbd9e8422019-08-15 22:26:48 +02004046 if (msg->msg_state < HTTP_MSG_DATA)
4047 req->analysers |= AN_REQ_HTTP_BODY;
4048 }
Christopher Faulet377c5a52018-10-24 21:21:30 +02004049 else {
Christopher Faulet5d45e382019-02-27 15:15:23 +01004050 /* POST without admin level */
Christopher Faulet377c5a52018-10-24 21:21:30 +02004051 appctx->ctx.stats.flags &= ~STAT_CHUNKED;
4052 appctx->ctx.stats.st_code = STAT_STATUS_DENY;
4053 appctx->st0 = STAT_HTTP_LAST;
4054 }
4055 }
4056 else {
Christopher Faulet5d45e382019-02-27 15:15:23 +01004057 /* Unsupported method */
4058 appctx->ctx.stats.flags &= ~STAT_CHUNKED;
4059 appctx->ctx.stats.st_code = STAT_STATUS_IVAL;
4060 appctx->st0 = STAT_HTTP_LAST;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004061 }
4062
4063 s->task->nice = -32; /* small boost for HTTP statistics */
4064 return 1;
4065}
4066
Christopher Faulet021a8e42021-03-29 10:46:38 +02004067/* This function waits for the message payload at most <time> milliseconds (may
4068 * be set to TICK_ETERNITY). It stops to wait if at least <bytes> bytes of the
4069 * payload are received (0 means no limit). It returns HTTP_RULE_* depending on
4070 * the result:
4071 *
4072 * - HTTP_RULE_RES_CONT when conditions are met to stop waiting
4073 * - HTTP_RULE_RES_YIELD to wait for more data
Ilya Shipitsinb2be9a12021-04-24 13:25:42 +05004074 * - HTTP_RULE_RES_ABRT when a timeout occurred.
Christopher Faulet021a8e42021-03-29 10:46:38 +02004075 * - HTTP_RULE_RES_BADREQ if a parsing error is raised by lower level
Ilya Shipitsinb2be9a12021-04-24 13:25:42 +05004076 * - HTTP_RULE_RES_ERROR if an internal error occurred
Christopher Faulet021a8e42021-03-29 10:46:38 +02004077 *
Ilya Shipitsinb2be9a12021-04-24 13:25:42 +05004078 * If a timeout occurred, this function is responsible to emit the right response
Christopher Faulet021a8e42021-03-29 10:46:38 +02004079 * to the client, depending on the channel (408 on request side, 504 on response
4080 * side). All other errors must be handled by the caller.
4081 */
4082enum rule_result http_wait_for_msg_body(struct stream *s, struct channel *chn,
4083 unsigned int time, unsigned int bytes)
4084{
4085 struct session *sess = s->sess;
4086 struct http_txn *txn = s->txn;
4087 struct http_msg *msg = ((chn->flags & CF_ISRESP) ? &txn->rsp : &txn->req);
4088 struct htx *htx;
4089 enum rule_result ret = HTTP_RULE_RES_CONT;
4090
4091 htx = htxbuf(&chn->buf);
4092
4093 if (htx->flags & HTX_FL_PARSING_ERROR) {
4094 ret = HTTP_RULE_RES_BADREQ;
4095 goto end;
4096 }
4097 if (htx->flags & HTX_FL_PROCESSING_ERROR) {
4098 ret = HTTP_RULE_RES_ERROR;
4099 goto end;
4100 }
4101
4102 /* Do nothing for bodyless and CONNECT requests */
4103 if (txn->meth == HTTP_METH_CONNECT || (msg->flags & HTTP_MSGF_BODYLESS))
4104 goto end;
4105
4106 if (!(chn->flags & CF_ISRESP) && msg->msg_state < HTTP_MSG_DATA) {
4107 if (http_handle_expect_hdr(s, htx, msg) == -1) {
4108 ret = HTTP_RULE_RES_ERROR;
4109 goto end;
4110 }
4111 }
4112
4113 msg->msg_state = HTTP_MSG_DATA;
4114
4115 /* Now we're in HTTP_MSG_DATA. We just need to know if all data have
4116 * been received or if the buffer is full.
4117 */
4118 if ((htx->flags & HTX_FL_EOM) || htx_get_tail_type(htx) > HTX_BLK_DATA ||
4119 channel_htx_full(chn, htx, global.tune.maxrewrite))
4120 goto end;
4121
4122 if (bytes) {
4123 struct htx_blk *blk;
4124 unsigned int len = 0;
4125
4126 for (blk = htx_get_first_blk(htx); blk; blk = htx_get_next_blk(htx, blk)) {
4127 if (htx_get_blk_type(blk) != HTX_BLK_DATA)
4128 continue;
4129 len += htx_get_blksz(blk);
4130 if (len >= bytes)
4131 goto end;
4132 }
4133 }
4134
4135 if ((chn->flags & CF_READ_TIMEOUT) || tick_is_expired(chn->analyse_exp, now_ms)) {
4136 if (!(chn->flags & CF_ISRESP))
4137 goto abort_req;
4138 goto abort_res;
4139 }
4140
4141 /* we get here if we need to wait for more data */
4142 if (!(chn->flags & (CF_SHUTR | CF_READ_ERROR))) {
4143 if (!tick_isset(chn->analyse_exp))
4144 chn->analyse_exp = tick_add_ifset(now_ms, time);
4145 ret = HTTP_RULE_RES_YIELD;
4146 }
4147
4148 end:
4149 return ret;
4150
4151 abort_req:
4152 txn->status = 408;
4153 if (!(s->flags & SF_ERR_MASK))
4154 s->flags |= SF_ERR_CLITO;
4155 if (!(s->flags & SF_FINST_MASK))
4156 s->flags |= SF_FINST_D;
Willy Tarreau4781b152021-04-06 13:53:36 +02004157 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req);
Christopher Faulet021a8e42021-03-29 10:46:38 +02004158 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02004159 _HA_ATOMIC_INC(&sess->listener->counters->failed_req);
Christopher Faulet021a8e42021-03-29 10:46:38 +02004160 http_reply_and_close(s, txn->status, http_error_message(s));
4161 ret = HTTP_RULE_RES_ABRT;
4162 goto end;
4163
4164 abort_res:
4165 txn->status = 504;
4166 if (!(s->flags & SF_ERR_MASK))
4167 s->flags |= SF_ERR_SRVTO;
4168 if (!(s->flags & SF_FINST_MASK))
4169 s->flags |= SF_FINST_D;
4170 stream_inc_http_fail_ctr(s);
4171 http_reply_and_close(s, txn->status, http_error_message(s));
4172 ret = HTTP_RULE_RES_ABRT;
4173 goto end;
4174}
4175
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004176void http_perform_server_redirect(struct stream *s, struct stream_interface *si)
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004177{
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004178 struct channel *req = &s->req;
4179 struct channel *res = &s->res;
4180 struct server *srv;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004181 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004182 struct htx_sl *sl;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004183 struct ist path, location;
4184 unsigned int flags;
Amaury Denoyellec453f952021-07-06 11:40:12 +02004185 struct http_uri_parser parser;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004186
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004187 /*
4188 * Create the location
4189 */
4190 chunk_reset(&trash);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004191
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004192 /* 1: add the server's prefix */
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004193 /* special prefix "/" means don't change URL */
4194 srv = __objt_server(s->target);
4195 if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') {
4196 if (!chunk_memcat(&trash, srv->rdr_pfx, srv->rdr_len))
4197 return;
4198 }
4199
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004200 /* 2: add the request Path */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004201 htx = htxbuf(&req->buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02004202 sl = http_get_stline(htx);
Amaury Denoyellec453f952021-07-06 11:40:12 +02004203 parser = http_uri_parser_init(htx_sl_req_uri(sl));
4204 path = http_parse_path(&parser);
Tim Duesterhused526372020-03-05 17:56:33 +01004205 if (!isttest(path))
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004206 return;
4207
4208 if (!chunk_memcat(&trash, path.ptr, path.len))
4209 return;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004210 location = ist2(trash.area, trash.data);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004211
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004212 /*
4213 * Create the 302 respone
4214 */
4215 htx = htx_from_buf(&res->buf);
4216 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
4217 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
4218 ist("HTTP/1.1"), ist("302"), ist("Found"));
4219 if (!sl)
4220 goto fail;
4221 sl->info.res.status = 302;
4222 s->txn->status = 302;
4223
4224 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) ||
4225 !htx_add_header(htx, ist("Connection"), ist("close")) ||
4226 !htx_add_header(htx, ist("Content-length"), ist("0")) ||
4227 !htx_add_header(htx, ist("Location"), location))
4228 goto fail;
4229
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004230 if (!htx_add_endof(htx, HTX_BLK_EOH))
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004231 goto fail;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004232
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004233 htx->flags |= HTX_FL_EOM;
Christopher Fauletc20afb82020-01-24 19:16:26 +01004234 htx_to_buf(htx, &res->buf);
Christopher Fauleta72a7e42020-01-28 09:28:11 +01004235 if (!http_forward_proxy_resp(s, 1))
4236 goto fail;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004237
4238 /* return without error. */
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004239 si_shutr(si);
4240 si_shutw(si);
4241 si->err_type = SI_ET_NONE;
4242 si->state = SI_ST_CLO;
4243
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004244 if (!(s->flags & SF_ERR_MASK))
4245 s->flags |= SF_ERR_LOCAL;
4246 if (!(s->flags & SF_FINST_MASK))
4247 s->flags |= SF_FINST_C;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004248
4249 /* FIXME: we should increase a counter of redirects per server and per backend. */
4250 srv_inc_sess_ctr(srv);
4251 srv_set_sess_last(srv);
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004252 return;
4253
4254 fail:
4255 /* If an error occurred, remove the incomplete HTTP response from the
4256 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004257 channel_htx_truncate(res, htx);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004258}
4259
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05004260/* This function terminates the request because it was completely analyzed or
Christopher Fauletf2824e62018-10-01 12:12:37 +02004261 * because an error was triggered during the body forwarding.
4262 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004263static void http_end_request(struct stream *s)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004264{
4265 struct channel *chn = &s->req;
4266 struct http_txn *txn = s->txn;
4267
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004268 DBG_TRACE_ENTER(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004269
Christopher Fauletb42a8b62018-11-19 21:59:00 +01004270 if (unlikely(txn->req.msg_state == HTTP_MSG_ERROR ||
4271 txn->rsp.msg_state == HTTP_MSG_ERROR)) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004272 channel_abort(chn);
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004273 channel_htx_truncate(chn, htxbuf(&chn->buf));
Christopher Fauletf2824e62018-10-01 12:12:37 +02004274 goto end;
4275 }
4276
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004277 if (unlikely(txn->req.msg_state < HTTP_MSG_DONE)) {
4278 DBG_TRACE_DEVEL("waiting end of the request", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004279 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004280 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004281
4282 if (txn->req.msg_state == HTTP_MSG_DONE) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004283 /* No need to read anymore, the request was completely parsed.
4284 * We can shut the read side unless we want to abort_on_close,
4285 * or we have a POST request. The issue with POST requests is
4286 * that some browsers still send a CRLF after the request, and
4287 * this CRLF must be read so that it does not remain in the kernel
4288 * buffers, otherwise a close could cause an RST on some systems
4289 * (eg: Linux).
4290 */
Christopher Faulet769d0e92019-03-22 14:23:18 +01004291 if (!(s->be->options & PR_O_ABRT_CLOSE) && txn->meth != HTTP_METH_POST)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004292 channel_dont_read(chn);
4293
4294 /* if the server closes the connection, we want to immediately react
4295 * and close the socket to save packets and syscalls.
4296 */
4297 s->si[1].flags |= SI_FL_NOHALF;
4298
4299 /* In any case we've finished parsing the request so we must
4300 * disable Nagle when sending data because 1) we're not going
4301 * to shut this side, and 2) the server is waiting for us to
4302 * send pending data.
4303 */
4304 chn->flags |= CF_NEVER_WAIT;
4305
Christopher Fauletd01ce402019-01-02 17:44:13 +01004306 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
4307 /* The server has not finished to respond, so we
4308 * don't want to move in order not to upset it.
4309 */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004310 DBG_TRACE_DEVEL("waiting end of the response", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletd01ce402019-01-02 17:44:13 +01004311 return;
4312 }
4313
Christopher Fauletf2824e62018-10-01 12:12:37 +02004314 /* When we get here, it means that both the request and the
4315 * response have finished receiving. Depending on the connection
4316 * mode, we'll have to wait for the last bytes to leave in either
4317 * direction, and sometimes for a close to be effective.
4318 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02004319 if (txn->flags & TX_CON_WANT_TUN) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004320 /* Tunnel mode will not have any analyser so it needs to
4321 * poll for reads.
4322 */
4323 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004324 if (b_data(&chn->buf)) {
4325 DBG_TRACE_DEVEL("waiting to flush the request", STRM_EV_HTTP_ANA, s, txn);
Christopher Faulet9768c262018-10-22 09:34:31 +02004326 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004327 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004328 txn->req.msg_state = HTTP_MSG_TUNNEL;
4329 }
4330 else {
4331 /* we're not expecting any new data to come for this
4332 * transaction, so we can close it.
Christopher Faulet9768c262018-10-22 09:34:31 +02004333 *
4334 * However, there is an exception if the response
4335 * length is undefined. In this case, we need to wait
4336 * the close from the server. The response will be
4337 * switched in TUNNEL mode until the end.
Christopher Fauletf2824e62018-10-01 12:12:37 +02004338 */
4339 if (!(txn->rsp.flags & HTTP_MSGF_XFER_LEN) &&
4340 txn->rsp.msg_state != HTTP_MSG_CLOSED)
Christopher Faulet9768c262018-10-22 09:34:31 +02004341 goto check_channel_flags;
Christopher Fauletf2824e62018-10-01 12:12:37 +02004342
4343 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4344 channel_shutr_now(chn);
4345 channel_shutw_now(chn);
4346 }
4347 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004348 goto check_channel_flags;
4349 }
4350
4351 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
4352 http_msg_closing:
4353 /* nothing else to forward, just waiting for the output buffer
4354 * to be empty and for the shutw_now to take effect.
4355 */
4356 if (channel_is_empty(chn)) {
4357 txn->req.msg_state = HTTP_MSG_CLOSED;
4358 goto http_msg_closed;
4359 }
4360 else if (chn->flags & CF_SHUTW) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004361 txn->req.msg_state = HTTP_MSG_ERROR;
4362 goto end;
4363 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004364 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004365 return;
4366 }
4367
4368 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
4369 http_msg_closed:
Christopher Fauletf2824e62018-10-01 12:12:37 +02004370 /* if we don't know whether the server will close, we need to hard close */
4371 if (txn->rsp.flags & HTTP_MSGF_XFER_LEN)
4372 s->si[1].flags |= SI_FL_NOLINGER; /* we want to close ASAP */
Christopher Fauletf2824e62018-10-01 12:12:37 +02004373 /* see above in MSG_DONE why we only do this in these states */
Christopher Faulet769d0e92019-03-22 14:23:18 +01004374 if (!(s->be->options & PR_O_ABRT_CLOSE))
Christopher Fauletf2824e62018-10-01 12:12:37 +02004375 channel_dont_read(chn);
4376 goto end;
4377 }
4378
4379 check_channel_flags:
4380 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4381 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4382 /* if we've just closed an output, let's switch */
4383 txn->req.msg_state = HTTP_MSG_CLOSING;
4384 goto http_msg_closing;
4385 }
4386
4387 end:
4388 chn->analysers &= AN_REQ_FLT_END;
Christopher Faulet198ef8b2020-12-15 13:32:55 +01004389 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
4390 chn->flags |= CF_NEVER_WAIT;
4391 if (HAS_REQ_DATA_FILTERS(s))
Christopher Fauletf2824e62018-10-01 12:12:37 +02004392 chn->analysers |= AN_REQ_FLT_XFER_DATA;
Christopher Faulet198ef8b2020-12-15 13:32:55 +01004393 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004394 channel_auto_close(chn);
4395 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004396 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004397}
4398
4399
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05004400/* This function terminates the response because it was completely analyzed or
Christopher Fauletf2824e62018-10-01 12:12:37 +02004401 * because an error was triggered during the body forwarding.
4402 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004403static void http_end_response(struct stream *s)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004404{
4405 struct channel *chn = &s->res;
4406 struct http_txn *txn = s->txn;
4407
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004408 DBG_TRACE_ENTER(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004409
Christopher Fauletb42a8b62018-11-19 21:59:00 +01004410 if (unlikely(txn->req.msg_state == HTTP_MSG_ERROR ||
4411 txn->rsp.msg_state == HTTP_MSG_ERROR)) {
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004412 channel_htx_truncate(&s->req, htxbuf(&s->req.buf));
Christopher Faulet9768c262018-10-22 09:34:31 +02004413 channel_abort(&s->req);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004414 goto end;
4415 }
4416
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004417 if (unlikely(txn->rsp.msg_state < HTTP_MSG_DONE)) {
4418 DBG_TRACE_DEVEL("waiting end of the response", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004419 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004420 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004421
4422 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
4423 /* In theory, we don't need to read anymore, but we must
4424 * still monitor the server connection for a possible close
4425 * while the request is being uploaded, so we don't disable
4426 * reading.
4427 */
4428 /* channel_dont_read(chn); */
4429
4430 if (txn->req.msg_state < HTTP_MSG_DONE) {
4431 /* The client seems to still be sending data, probably
4432 * because we got an error response during an upload.
4433 * We have the choice of either breaking the connection
4434 * or letting it pass through. Let's do the later.
4435 */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004436 DBG_TRACE_DEVEL("waiting end of the request", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004437 return;
4438 }
4439
4440 /* When we get here, it means that both the request and the
4441 * response have finished receiving. Depending on the connection
4442 * mode, we'll have to wait for the last bytes to leave in either
4443 * direction, and sometimes for a close to be effective.
4444 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02004445 if (txn->flags & TX_CON_WANT_TUN) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004446 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004447 if (b_data(&chn->buf)) {
4448 DBG_TRACE_DEVEL("waiting to flush the respone", STRM_EV_HTTP_ANA, s, txn);
Christopher Faulet9768c262018-10-22 09:34:31 +02004449 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004450 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004451 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
4452 }
4453 else {
4454 /* we're not expecting any new data to come for this
4455 * transaction, so we can close it.
4456 */
4457 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4458 channel_shutr_now(chn);
4459 channel_shutw_now(chn);
4460 }
4461 }
4462 goto check_channel_flags;
4463 }
4464
4465 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
4466 http_msg_closing:
4467 /* nothing else to forward, just waiting for the output buffer
4468 * to be empty and for the shutw_now to take effect.
4469 */
4470 if (channel_is_empty(chn)) {
4471 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4472 goto http_msg_closed;
4473 }
4474 else if (chn->flags & CF_SHUTW) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004475 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau4781b152021-04-06 13:53:36 +02004476 _HA_ATOMIC_INC(&strm_sess(s)->fe->fe_counters.cli_aborts);
4477 _HA_ATOMIC_INC(&s->be->be_counters.cli_aborts);
William Lallemand36119de2021-03-08 15:26:48 +01004478 if (strm_sess(s)->listener && strm_sess(s)->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02004479 _HA_ATOMIC_INC(&strm_sess(s)->listener->counters->cli_aborts);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004480 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02004481 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.cli_aborts);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004482 goto end;
4483 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004484 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004485 return;
4486 }
4487
4488 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
4489 http_msg_closed:
4490 /* drop any pending data */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004491 channel_htx_truncate(&s->req, htxbuf(&s->req.buf));
Christopher Faulet9768c262018-10-22 09:34:31 +02004492 channel_abort(&s->req);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004493 goto end;
4494 }
4495
4496 check_channel_flags:
4497 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4498 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4499 /* if we've just closed an output, let's switch */
4500 txn->rsp.msg_state = HTTP_MSG_CLOSING;
4501 goto http_msg_closing;
4502 }
4503
4504 end:
4505 chn->analysers &= AN_RES_FLT_END;
Christopher Faulet198ef8b2020-12-15 13:32:55 +01004506 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
4507 chn->flags |= CF_NEVER_WAIT;
4508 if (HAS_RSP_DATA_FILTERS(s))
4509 chn->analysers |= AN_RES_FLT_XFER_DATA;
4510 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004511 channel_auto_close(chn);
4512 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004513 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004514}
4515
Christopher Fauletef70e252020-01-28 09:26:19 +01004516/* Forward a response generated by HAProxy (error/redirect/return). This
4517 * function forwards all pending incoming data. If <final> is set to 0, nothing
4518 * more is performed. It is used for 1xx informational messages. Otherwise, the
Christopher Faulet507479b2020-05-15 12:29:46 +02004519 * transaction is terminated and the request is emptied. On success 1 is
Christopher Faulet40e6b552020-06-25 16:04:50 +02004520 * returned. If an error occurred, 0 is returned. If it fails, this function
4521 * only exits. It is the caller responsibility to do the cleanup.
Christopher Fauletef70e252020-01-28 09:26:19 +01004522 */
4523int http_forward_proxy_resp(struct stream *s, int final)
4524{
4525 struct channel *req = &s->req;
4526 struct channel *res = &s->res;
4527 struct htx *htx = htxbuf(&res->buf);
4528 size_t data;
4529
4530 if (final) {
4531 htx->flags |= HTX_FL_PROXY_RESP;
Christopher Faulet507479b2020-05-15 12:29:46 +02004532
Christopher Fauletaab1b672020-11-18 16:44:02 +01004533 if (!htx_is_empty(htx) && !http_eval_after_res_rules(s))
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01004534 return 0;
Christopher Fauletef70e252020-01-28 09:26:19 +01004535
Christopher Fauletd6c48362020-10-19 18:01:38 +02004536 if (s->txn->meth == HTTP_METH_HEAD)
4537 htx_skip_msg_payload(htx);
4538
Christopher Fauletef70e252020-01-28 09:26:19 +01004539 channel_auto_read(req);
4540 channel_abort(req);
4541 channel_auto_close(req);
4542 channel_htx_erase(req, htxbuf(&req->buf));
4543
4544 res->wex = tick_add_ifset(now_ms, res->wto);
4545 channel_auto_read(res);
4546 channel_auto_close(res);
4547 channel_shutr_now(res);
Christopher Faulet1a9db7c2020-06-25 15:36:45 +02004548 res->flags |= CF_EOI; /* The response is terminated, add EOI */
Christopher Faulet42432f32020-11-20 17:43:16 +01004549 htxbuf(&res->buf)->flags |= HTX_FL_EOM; /* no more data are expected */
Christopher Fauletef70e252020-01-28 09:26:19 +01004550 }
Christopher Fauletcf6898c2020-06-25 15:55:11 +02004551 else {
4552 /* Send ASAP informational messages. Rely on CF_EOI for final
4553 * response.
4554 */
4555 res->flags |= CF_SEND_DONTWAIT;
4556 }
Christopher Fauletef70e252020-01-28 09:26:19 +01004557
4558 data = htx->data - co_data(res);
4559 c_adv(res, data);
4560 htx->first = -1;
4561 res->total += data;
4562 return 1;
4563}
4564
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004565void http_server_error(struct stream *s, struct stream_interface *si, int err,
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004566 int finst, struct http_reply *msg)
Christopher Faulet0f226952018-10-22 09:29:56 +02004567{
Christopher Faulet72c7d8d2020-01-27 15:32:25 +01004568 http_reply_and_close(s, s->txn->status, msg);
Christopher Faulet0f226952018-10-22 09:29:56 +02004569 if (!(s->flags & SF_ERR_MASK))
4570 s->flags |= err;
4571 if (!(s->flags & SF_FINST_MASK))
4572 s->flags |= finst;
4573}
4574
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004575void http_reply_and_close(struct stream *s, short status, struct http_reply *msg)
Christopher Faulet0f226952018-10-22 09:29:56 +02004576{
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004577 if (!msg) {
4578 channel_htx_truncate(&s->res, htxbuf(&s->res.buf));
4579 goto end;
4580 }
4581
4582 if (http_reply_message(s, msg) == -1) {
4583 /* On error, return a 500 error message, but don't rewrite it if
Christopher Faulet40e6b552020-06-25 16:04:50 +02004584 * it is already an internal error. If it was already a "const"
4585 * 500 error, just fail.
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004586 */
Christopher Faulet40e6b552020-06-25 16:04:50 +02004587 if (s->txn->status == 500) {
4588 if (s->txn->flags & TX_CONST_REPLY)
4589 goto end;
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004590 s->txn->flags |= TX_CONST_REPLY;
Christopher Faulet40e6b552020-06-25 16:04:50 +02004591 }
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004592 s->txn->status = 500;
4593 s->txn->http_reply = NULL;
4594 return http_reply_and_close(s, s->txn->status, http_error_message(s));
4595 }
4596
4597end:
4598 s->res.wex = tick_add_ifset(now_ms, s->res.wto);
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004599
Christopher Faulet0f226952018-10-22 09:29:56 +02004600 channel_auto_read(&s->req);
4601 channel_abort(&s->req);
4602 channel_auto_close(&s->req);
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004603 channel_htx_erase(&s->req, htxbuf(&s->req.buf));
Christopher Faulet72c7d8d2020-01-27 15:32:25 +01004604 channel_auto_read(&s->res);
4605 channel_auto_close(&s->res);
4606 channel_shutr_now(&s->res);
Christopher Faulet0f226952018-10-22 09:29:56 +02004607}
4608
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004609struct http_reply *http_error_message(struct stream *s)
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004610{
4611 const int msgnum = http_get_status_idx(s->txn->status);
4612
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004613 if (s->txn->http_reply)
4614 return s->txn->http_reply;
4615 else if (s->be->replies[msgnum])
4616 return s->be->replies[msgnum];
4617 else if (strm_fe(s)->replies[msgnum])
4618 return strm_fe(s)->replies[msgnum];
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004619 else
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004620 return &http_err_replies[msgnum];
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004621}
4622
Christopher Faulet40e6b552020-06-25 16:04:50 +02004623/* Produces an HTX message from an http reply. Depending on the http reply type,
4624 * a, errorfile, an raw file or a log-format string is used. On success, it
4625 * returns 0. If an error occurs -1 is returned. If it fails, this function only
4626 * exits. It is the caller responsibility to do the cleanup.
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004627 */
Christopher Fauletae43b6c2020-05-27 15:24:22 +02004628int http_reply_to_htx(struct stream *s, struct htx *htx, struct http_reply *reply)
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004629{
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004630 struct buffer *errmsg;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004631 struct htx_sl *sl;
4632 struct buffer *body = NULL;
4633 const char *status, *reason, *clen, *ctype;
4634 unsigned int slflags;
4635 int ret = 0;
4636
Christopher Faulete29a97e2020-05-14 14:49:25 +02004637 /*
4638 * - HTTP_REPLY_ERRFILES unexpected here. handled as no payload if so
4639 *
4640 * - HTTP_REPLY_INDIRECT: switch on another reply if defined or handled
4641 * as no payload if NULL. the TXN status code is set with the status
4642 * of the original reply.
4643 */
4644
4645 if (reply->type == HTTP_REPLY_INDIRECT) {
4646 if (reply->body.reply)
4647 reply = reply->body.reply;
4648 }
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004649 if (reply->type == HTTP_REPLY_ERRMSG && !reply->body.errmsg) {
4650 /* get default error message */
4651 if (reply == s->txn->http_reply)
4652 s->txn->http_reply = NULL;
4653 reply = http_error_message(s);
4654 if (reply->type == HTTP_REPLY_INDIRECT) {
4655 if (reply->body.reply)
4656 reply = reply->body.reply;
4657 }
4658 }
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004659
4660 if (reply->type == HTTP_REPLY_ERRMSG) {
4661 /* implicit or explicit error message*/
4662 errmsg = reply->body.errmsg;
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004663 if (errmsg && !b_is_null(errmsg)) {
Christopher Faulet20567362020-05-15 14:52:49 +02004664 if (!htx_copy_msg(htx, errmsg))
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004665 goto fail;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004666 }
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004667 }
4668 else {
4669 /* no payload, file or log-format string */
4670 if (reply->type == HTTP_REPLY_RAW) {
4671 /* file */
4672 body = &reply->body.obj;
4673 }
4674 else if (reply->type == HTTP_REPLY_LOGFMT) {
4675 /* log-format string */
4676 body = alloc_trash_chunk();
4677 if (!body)
4678 goto fail_alloc;
4679 body->data = build_logline(s, body->area, body->size, &reply->body.fmt);
4680 }
4681 /* else no payload */
4682
4683 status = ultoa(reply->status);
4684 reason = http_get_reason(reply->status);
4685 slflags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_CLEN);
4686 if (!body || !b_data(body))
4687 slflags |= HTX_SL_F_BODYLESS;
4688 sl = htx_add_stline(htx, HTX_BLK_RES_SL, slflags, ist("HTTP/1.1"), ist(status), ist(reason));
4689 if (!sl)
4690 goto fail;
4691 sl->info.res.status = reply->status;
4692
4693 clen = (body ? ultoa(b_data(body)) : "0");
4694 ctype = reply->ctype;
4695
4696 if (!LIST_ISEMPTY(&reply->hdrs)) {
4697 struct http_reply_hdr *hdr;
4698 struct buffer *value = alloc_trash_chunk();
4699
4700 if (!value)
4701 goto fail;
4702
4703 list_for_each_entry(hdr, &reply->hdrs, list) {
4704 chunk_reset(value);
4705 value->data = build_logline(s, value->area, value->size, &hdr->value);
4706 if (b_data(value) && !htx_add_header(htx, hdr->name, ist2(b_head(value), b_data(value)))) {
4707 free_trash_chunk(value);
4708 goto fail;
4709 }
4710 chunk_reset(value);
4711 }
4712 free_trash_chunk(value);
4713 }
4714
4715 if (!htx_add_header(htx, ist("content-length"), ist(clen)) ||
4716 (body && b_data(body) && ctype && !htx_add_header(htx, ist("content-type"), ist(ctype))) ||
4717 !htx_add_endof(htx, HTX_BLK_EOH) ||
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004718 (body && b_data(body) && !htx_add_data_atonce(htx, ist2(b_head(body), b_data(body)))))
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004719 goto fail;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004720
4721 htx->flags |= HTX_FL_EOM;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004722 }
4723
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004724 leave:
4725 if (reply->type == HTTP_REPLY_LOGFMT)
4726 free_trash_chunk(body);
4727 return ret;
4728
4729 fail_alloc:
4730 if (!(s->flags & SF_ERR_MASK))
4731 s->flags |= SF_ERR_RESOURCE;
Christopher Faulet97e466c2020-05-15 15:12:47 +02004732 /* fall through */
4733 fail:
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004734 ret = -1;
4735 goto leave;
Christopher Faulet97e466c2020-05-15 15:12:47 +02004736}
4737
4738/* Send an http reply to the client. On success, it returns 0. If an error
Christopher Faulet40e6b552020-06-25 16:04:50 +02004739 * occurs -1 is returned and the response channel is truncated, removing this
4740 * way the faulty reply. This function may fail when the reply is formatted
4741 * (http_reply_to_htx) or when the reply is forwarded
4742 * (http_forward_proxy_resp). On the last case, it is because a
4743 * http-after-response rule fails.
Christopher Faulet97e466c2020-05-15 15:12:47 +02004744 */
4745int http_reply_message(struct stream *s, struct http_reply *reply)
4746{
4747 struct channel *res = &s->res;
4748 struct htx *htx = htx_from_buf(&res->buf);
4749
4750 if (s->txn->status == -1)
4751 s->txn->status = reply->status;
4752 channel_htx_truncate(res, htx);
4753
4754 if (http_reply_to_htx(s, htx, reply) == -1)
4755 goto fail;
4756
4757 htx_to_buf(htx, &s->res.buf);
4758 if (!http_forward_proxy_resp(s, 1))
4759 goto fail;
4760 return 0;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004761
4762 fail:
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004763 channel_htx_truncate(res, htx);
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004764 if (!(s->flags & SF_ERR_MASK))
4765 s->flags |= SF_ERR_PRXCOND;
Christopher Faulet97e466c2020-05-15 15:12:47 +02004766 return -1;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004767}
4768
Christopher Faulet304cc402019-07-15 15:46:28 +02004769/* Return the error message corresponding to si->err_type. It is assumed
4770 * that the server side is closed. Note that err_type is actually a
4771 * bitmask, where almost only aborts may be cumulated with other
4772 * values. We consider that aborted operations are more important
4773 * than timeouts or errors due to the fact that nobody else in the
4774 * logs might explain incomplete retries. All others should avoid
4775 * being cumulated. It should normally not be possible to have multiple
4776 * aborts at once, but just in case, the first one in sequence is reported.
4777 * Note that connection errors appearing on the second request of a keep-alive
4778 * connection are not reported since this allows the client to retry.
4779 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004780void http_return_srv_error(struct stream *s, struct stream_interface *si)
Christopher Faulet304cc402019-07-15 15:46:28 +02004781{
4782 int err_type = si->err_type;
4783
4784 /* set s->txn->status for http_error_message(s) */
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004785 if (err_type & SI_ET_QUEUE_ABRT) {
4786 s->txn->status = -1;
4787 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_Q, NULL);
4788 }
4789 else if (err_type & SI_ET_CONN_ABRT) {
4790 s->txn->status = -1;
4791 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_C, NULL);
4792 }
4793 else if (err_type & SI_ET_QUEUE_TO) {
4794 s->txn->status = 503;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004795 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_Q,
4796 http_error_message(s));
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004797 }
4798 else if (err_type & SI_ET_QUEUE_ERR) {
4799 s->txn->status = 503;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004800 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_Q,
4801 http_error_message(s));
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004802 }
4803 else if (err_type & SI_ET_CONN_TO) {
4804 s->txn->status = 503;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004805 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_C,
4806 (s->txn->flags & TX_NOT_FIRST) ? NULL :
4807 http_error_message(s));
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004808 }
4809 else if (err_type & SI_ET_CONN_ERR) {
4810 s->txn->status = 503;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004811 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_C,
4812 (s->flags & SF_SRV_REUSED) ? NULL :
4813 http_error_message(s));
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004814 }
4815 else if (err_type & SI_ET_CONN_RES) {
4816 s->txn->status = 503;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004817 http_server_error(s, si, SF_ERR_RESOURCE, SF_FINST_C,
4818 (s->txn->flags & TX_NOT_FIRST) ? NULL :
4819 http_error_message(s));
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004820 }
Christopher Faulet304cc402019-07-15 15:46:28 +02004821 else { /* SI_ET_CONN_OTHER and others */
4822 s->txn->status = 500;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004823 http_server_error(s, si, SF_ERR_INTERNAL, SF_FINST_C,
4824 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004825 }
4826}
4827
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004828
Christopher Faulet4a28a532019-03-01 11:19:40 +01004829/* Handle Expect: 100-continue for HTTP/1.1 messages if necessary. It returns 0
4830 * on success and -1 on error.
4831 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004832static int http_handle_expect_hdr(struct stream *s, struct htx *htx, struct http_msg *msg)
Christopher Faulet4a28a532019-03-01 11:19:40 +01004833{
4834 /* If we have HTTP/1.1 message with a body and Expect: 100-continue,
4835 * then we must send an HTTP/1.1 100 Continue intermediate response.
4836 */
4837 if (msg->msg_state == HTTP_MSG_BODY && (msg->flags & HTTP_MSGF_VER_11) &&
4838 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
4839 struct ist hdr = { .ptr = "Expect", .len = 6 };
4840 struct http_hdr_ctx ctx;
4841
4842 ctx.blk = NULL;
4843 /* Expect is allowed in 1.1, look for it */
4844 if (http_find_header(htx, hdr, &ctx, 0) &&
4845 unlikely(isteqi(ctx.value, ist2("100-continue", 12)))) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004846 if (http_reply_100_continue(s) == -1)
Christopher Faulet4a28a532019-03-01 11:19:40 +01004847 return -1;
4848 http_remove_header(htx, &ctx);
4849 }
4850 }
4851 return 0;
4852}
4853
Christopher Faulet23a3c792018-11-28 10:01:23 +01004854/* Send a 100-Continue response to the client. It returns 0 on success and -1
4855 * on error. The response channel is updated accordingly.
4856 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004857static int http_reply_100_continue(struct stream *s)
Christopher Faulet23a3c792018-11-28 10:01:23 +01004858{
4859 struct channel *res = &s->res;
4860 struct htx *htx = htx_from_buf(&res->buf);
4861 struct htx_sl *sl;
4862 unsigned int flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|
4863 HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
Christopher Faulet23a3c792018-11-28 10:01:23 +01004864
4865 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
4866 ist("HTTP/1.1"), ist("100"), ist("Continue"));
4867 if (!sl)
4868 goto fail;
4869 sl->info.res.status = 100;
4870
Christopher Faulet1d5ec092019-06-26 14:23:54 +02004871 if (!htx_add_endof(htx, HTX_BLK_EOH))
Christopher Faulet23a3c792018-11-28 10:01:23 +01004872 goto fail;
4873
Christopher Fauleta72a7e42020-01-28 09:28:11 +01004874 if (!http_forward_proxy_resp(s, 0))
4875 goto fail;
Christopher Faulet23a3c792018-11-28 10:01:23 +01004876 return 0;
4877
4878 fail:
4879 /* If an error occurred, remove the incomplete HTTP response from the
4880 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004881 channel_htx_truncate(res, htx);
Christopher Faulet23a3c792018-11-28 10:01:23 +01004882 return -1;
4883}
4884
Christopher Faulet12c51e22018-11-28 15:59:42 +01004885
Christopher Faulet0f226952018-10-22 09:29:56 +02004886/*
4887 * Capture headers from message <htx> according to header list <cap_hdr>, and
4888 * fill the <cap> pointers appropriately.
4889 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004890static void http_capture_headers(struct htx *htx, char **cap, struct cap_hdr *cap_hdr)
Christopher Faulet0f226952018-10-22 09:29:56 +02004891{
4892 struct cap_hdr *h;
4893 int32_t pos;
4894
Christopher Fauleta3f15502019-05-13 15:27:23 +02004895 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet0f226952018-10-22 09:29:56 +02004896 struct htx_blk *blk = htx_get_blk(htx, pos);
4897 enum htx_blk_type type = htx_get_blk_type(blk);
4898 struct ist n, v;
4899
4900 if (type == HTX_BLK_EOH)
4901 break;
4902 if (type != HTX_BLK_HDR)
4903 continue;
4904
4905 n = htx_get_blk_name(htx, blk);
4906
4907 for (h = cap_hdr; h; h = h->next) {
4908 if (h->namelen && (h->namelen == n.len) &&
4909 (strncasecmp(n.ptr, h->name, h->namelen) == 0)) {
4910 if (cap[h->index] == NULL)
4911 cap[h->index] =
4912 pool_alloc(h->pool);
4913
4914 if (cap[h->index] == NULL) {
4915 ha_alert("HTTP capture : out of memory.\n");
4916 break;
4917 }
4918
4919 v = htx_get_blk_value(htx, blk);
4920 if (v.len > h->len)
4921 v.len = h->len;
4922
4923 memcpy(cap[h->index], v.ptr, v.len);
4924 cap[h->index][v.len]=0;
4925 }
4926 }
4927 }
4928}
4929
Christopher Faulet0b6bdc52018-10-24 11:05:36 +02004930/* Delete a value in a header between delimiters <from> and <next>. The header
4931 * itself is delimited by <start> and <end> pointers. The number of characters
4932 * displaced is returned, and the pointer to the first delimiter is updated if
4933 * required. The function tries as much as possible to respect the following
4934 * principles :
4935 * - replace <from> delimiter by the <next> one unless <from> points to <start>,
4936 * in which case <next> is simply removed
4937 * - set exactly one space character after the new first delimiter, unless there
4938 * are not enough characters in the block being moved to do so.
4939 * - remove unneeded spaces before the previous delimiter and after the new
4940 * one.
4941 *
4942 * It is the caller's responsibility to ensure that :
4943 * - <from> points to a valid delimiter or <start> ;
4944 * - <next> points to a valid delimiter or <end> ;
4945 * - there are non-space chars before <from>.
4946 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004947static int http_del_hdr_value(char *start, char *end, char **from, char *next)
Christopher Faulet0b6bdc52018-10-24 11:05:36 +02004948{
4949 char *prev = *from;
4950
4951 if (prev == start) {
4952 /* We're removing the first value. eat the semicolon, if <next>
4953 * is lower than <end> */
4954 if (next < end)
4955 next++;
4956
4957 while (next < end && HTTP_IS_SPHT(*next))
4958 next++;
4959 }
4960 else {
4961 /* Remove useless spaces before the old delimiter. */
4962 while (HTTP_IS_SPHT(*(prev-1)))
4963 prev--;
4964 *from = prev;
4965
4966 /* copy the delimiter and if possible a space if we're
4967 * not at the end of the line.
4968 */
4969 if (next < end) {
4970 *prev++ = *next++;
4971 if (prev + 1 < next)
4972 *prev++ = ' ';
4973 while (next < end && HTTP_IS_SPHT(*next))
4974 next++;
4975 }
4976 }
4977 memmove(prev, next, end - next);
4978 return (prev - next);
4979}
4980
Christopher Faulet0f226952018-10-22 09:29:56 +02004981
4982/* Formats the start line of the request (without CRLF) and puts it in <str> and
Joseph Herlantc42c0e92018-11-25 10:43:27 -08004983 * return the written length. The line can be truncated if it exceeds <len>.
Christopher Faulet0f226952018-10-22 09:29:56 +02004984 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004985static size_t http_fmt_req_line(const struct htx_sl *sl, char *str, size_t len)
Christopher Faulet0f226952018-10-22 09:29:56 +02004986{
4987 struct ist dst = ist2(str, 0);
4988
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004989 if (istcat(&dst, htx_sl_req_meth(sl), len) == -1)
Christopher Faulet0f226952018-10-22 09:29:56 +02004990 goto end;
4991 if (dst.len + 1 > len)
4992 goto end;
4993 dst.ptr[dst.len++] = ' ';
4994
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004995 if (istcat(&dst, htx_sl_req_uri(sl), len) == -1)
Christopher Faulet0f226952018-10-22 09:29:56 +02004996 goto end;
4997 if (dst.len + 1 > len)
4998 goto end;
4999 dst.ptr[dst.len++] = ' ';
5000
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005001 istcat(&dst, htx_sl_req_vsn(sl), len);
Christopher Faulet0f226952018-10-22 09:29:56 +02005002 end:
5003 return dst.len;
5004}
5005
5006/*
5007 * Print a debug line with a start line.
5008 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005009static void http_debug_stline(const char *dir, struct stream *s, const struct htx_sl *sl)
Christopher Faulet0f226952018-10-22 09:29:56 +02005010{
5011 struct session *sess = strm_sess(s);
5012 int max;
5013
5014 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
5015 dir,
5016 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->handle.fd : -1,
5017 objt_cs(s->si[1].end) ? (unsigned short)objt_cs(s->si[1].end)->conn->handle.fd : -1);
5018
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005019 max = HTX_SL_P1_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02005020 UBOUND(max, trash.size - trash.data - 3);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005021 chunk_memcat(&trash, HTX_SL_P1_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02005022 trash.area[trash.data++] = ' ';
5023
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005024 max = HTX_SL_P2_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02005025 UBOUND(max, trash.size - trash.data - 2);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005026 chunk_memcat(&trash, HTX_SL_P2_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02005027 trash.area[trash.data++] = ' ';
5028
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005029 max = HTX_SL_P3_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02005030 UBOUND(max, trash.size - trash.data - 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005031 chunk_memcat(&trash, HTX_SL_P3_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02005032 trash.area[trash.data++] = '\n';
5033
Willy Tarreau2e8ab6b2020-03-14 11:03:20 +01005034 DISGUISE(write(1, trash.area, trash.data));
Christopher Faulet0f226952018-10-22 09:29:56 +02005035}
5036
5037/*
5038 * Print a debug line with a header.
5039 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005040static 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 +02005041{
5042 struct session *sess = strm_sess(s);
5043 int max;
5044
5045 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
5046 dir,
5047 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->handle.fd : -1,
5048 objt_cs(s->si[1].end) ? (unsigned short)objt_cs(s->si[1].end)->conn->handle.fd : -1);
5049
5050 max = n.len;
5051 UBOUND(max, trash.size - trash.data - 3);
5052 chunk_memcat(&trash, n.ptr, max);
5053 trash.area[trash.data++] = ':';
5054 trash.area[trash.data++] = ' ';
5055
5056 max = v.len;
5057 UBOUND(max, trash.size - trash.data - 1);
5058 chunk_memcat(&trash, v.ptr, max);
5059 trash.area[trash.data++] = '\n';
5060
Willy Tarreau2e8ab6b2020-03-14 11:03:20 +01005061 DISGUISE(write(1, trash.area, trash.data));
Christopher Faulet0f226952018-10-22 09:29:56 +02005062}
5063
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005064/* Allocate a new HTTP transaction for stream <s> unless there is one already.
5065 * In case of allocation failure, everything allocated is freed and NULL is
5066 * returned. Otherwise the new transaction is assigned to the stream and
5067 * returned.
5068 */
5069struct http_txn *http_alloc_txn(struct stream *s)
5070{
5071 struct http_txn *txn = s->txn;
5072
5073 if (txn)
5074 return txn;
5075
5076 txn = pool_alloc(pool_head_http_txn);
5077 if (!txn)
5078 return txn;
5079
5080 s->txn = txn;
5081 return txn;
5082}
5083
5084void http_txn_reset_req(struct http_txn *txn)
5085{
Christopher Faulet1aea50e2020-01-17 16:03:53 +01005086 txn->req.flags = 0;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005087 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
5088}
5089
5090void http_txn_reset_res(struct http_txn *txn)
5091{
Christopher Faulet1aea50e2020-01-17 16:03:53 +01005092 txn->rsp.flags = 0;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005093 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
5094}
5095
5096/*
Christopher Faulet75f619a2021-03-08 19:12:58 +01005097 * Create and initialize a new HTTP transaction for stream <s>. This should be
5098 * used before processing any new request. It returns the transaction or NLULL
5099 * on error.
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005100 */
Christopher Faulet75f619a2021-03-08 19:12:58 +01005101struct http_txn *http_create_txn(struct stream *s)
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005102{
Christopher Faulet75f619a2021-03-08 19:12:58 +01005103 struct http_txn *txn;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005104 struct conn_stream *cs = objt_cs(s->si[0].end);
5105
Christopher Faulet75f619a2021-03-08 19:12:58 +01005106 txn = pool_alloc(pool_head_http_txn);
5107 if (!txn)
5108 return NULL;
5109 s->txn = txn;
5110
Christopher Fauletda831fa2020-10-06 17:58:43 +02005111 txn->flags = ((cs && cs->flags & CS_FL_NOT_FIRST) ? TX_NOT_FIRST : 0);
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005112 txn->status = -1;
Christopher Faulet5cb513a2020-05-13 17:56:56 +02005113 txn->http_reply = NULL;
Willy Tarreau8b507582020-02-25 09:35:07 +01005114 write_u32(txn->cache_hash, 0);
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005115
5116 txn->cookie_first_date = 0;
5117 txn->cookie_last_date = 0;
5118
5119 txn->srv_cookie = NULL;
5120 txn->cli_cookie = NULL;
5121 txn->uri = NULL;
5122
5123 http_txn_reset_req(txn);
5124 http_txn_reset_res(txn);
5125
5126 txn->req.chn = &s->req;
5127 txn->rsp.chn = &s->res;
5128
5129 txn->auth.method = HTTP_AUTH_UNKNOWN;
5130
5131 vars_init(&s->vars_txn, SCOPE_TXN);
5132 vars_init(&s->vars_reqres, SCOPE_REQ);
Christopher Faulet75f619a2021-03-08 19:12:58 +01005133
5134 return txn;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005135}
5136
5137/* to be used at the end of a transaction */
Christopher Faulet75f619a2021-03-08 19:12:58 +01005138void http_destroy_txn(struct stream *s)
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005139{
5140 struct http_txn *txn = s->txn;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005141
5142 /* these ones will have been dynamically allocated */
5143 pool_free(pool_head_requri, txn->uri);
5144 pool_free(pool_head_capture, txn->cli_cookie);
5145 pool_free(pool_head_capture, txn->srv_cookie);
Tim Duesterhusa17e6622020-03-05 20:19:02 +01005146 pool_free(pool_head_uniqueid, s->unique_id.ptr);
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005147
Tim Duesterhusa17e6622020-03-05 20:19:02 +01005148 s->unique_id = IST_NULL;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005149 txn->uri = NULL;
5150 txn->srv_cookie = NULL;
5151 txn->cli_cookie = NULL;
5152
Christopher Faulet59399252019-11-07 14:27:52 +01005153 if (!LIST_ISEMPTY(&s->vars_txn.head))
5154 vars_prune(&s->vars_txn, s->sess, s);
5155 if (!LIST_ISEMPTY(&s->vars_reqres.head))
5156 vars_prune(&s->vars_reqres, s->sess, s);
Christopher Faulet75f619a2021-03-08 19:12:58 +01005157
5158 pool_free(pool_head_http_txn, txn);
5159 s->txn = NULL;
Christopher Faulet59399252019-11-07 14:27:52 +01005160}
5161
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005162
5163DECLARE_POOL(pool_head_http_txn, "http_txn", sizeof(struct http_txn));
Christopher Faulet0f226952018-10-22 09:29:56 +02005164
Christopher Fauletf4eb75d2018-10-11 15:55:07 +02005165__attribute__((constructor))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005166static void __http_protocol_init(void)
Christopher Fauletf4eb75d2018-10-11 15:55:07 +02005167{
5168}
5169
5170
5171/*
5172 * Local variables:
5173 * c-indent-level: 8
5174 * c-basic-offset: 8
5175 * End:
5176 */