blob: 980731dcebf532c29369cb875fe7f890219078a6 [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 */
206 if (unlikely((sess->fe->monitor_uri_len != 0) &&
Christopher Faulet6072beb2020-02-18 15:34:58 +0100207 ((*sess->fe->monitor_uri == '/' && isteq(http_get_path(htx_sl_req_uri(sl)),
208 ist2(sess->fe->monitor_uri, sess->fe->monitor_uri_len))) ||
209 isteq(htx_sl_req_uri(sl), ist2(sess->fe->monitor_uri, sess->fe->monitor_uri_len))))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200210 /*
211 * We have found the monitor URI
212 */
213 struct acl_cond *cond;
214
215 s->flags |= SF_MONITOR;
Willy Tarreau4781b152021-04-06 13:53:36 +0200216 _HA_ATOMIC_INC(&sess->fe->fe_counters.intercepted_req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200217
218 /* Check if we want to fail this monitor request or not */
219 list_for_each_entry(cond, &sess->fe->mon_fail_cond, list) {
220 int ret = acl_exec_cond(cond, sess->fe, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
221
222 ret = acl_pass(ret);
223 if (cond->pol == ACL_COND_UNLESS)
224 ret = !ret;
225
226 if (ret) {
227 /* we fail this request, let's return 503 service unavail */
228 txn->status = 503;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200229 if (!(s->flags & SF_ERR_MASK))
230 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
231 goto return_prx_cond;
232 }
233 }
234
Joseph Herlantc42c0e92018-11-25 10:43:27 -0800235 /* nothing to fail, let's reply normally */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200236 txn->status = 200;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200237 if (!(s->flags & SF_ERR_MASK))
238 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
239 goto return_prx_cond;
240 }
241
242 /*
243 * 3: Maybe we have to copy the original REQURI for the logs ?
244 * Note: we cannot log anymore if the request has been
245 * classified as invalid.
246 */
247 if (unlikely(s->logs.logwait & LW_REQ)) {
248 /* we have a complete HTTP request that we must log */
249 if ((txn->uri = pool_alloc(pool_head_requri)) != NULL) {
Christopher Faulet9768c262018-10-22 09:34:31 +0200250 size_t len;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200251
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200252 len = http_fmt_req_line(sl, txn->uri, global.tune.requri_len - 1);
Christopher Faulet9768c262018-10-22 09:34:31 +0200253 txn->uri[len] = 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200254
255 if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT)))
256 s->do_log(s);
257 } else {
258 ha_alert("HTTP logging : out of memory.\n");
259 }
260 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200261
Christopher Faulete0768eb2018-10-03 16:38:02 +0200262 /* if the frontend has "option http-use-proxy-header", we'll check if
263 * we have what looks like a proxied connection instead of a connection,
264 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
265 * Note that this is *not* RFC-compliant, however browsers and proxies
266 * happen to do that despite being non-standard :-(
267 * We consider that a request not beginning with either '/' or '*' is
268 * a proxied connection, which covers both "scheme://location" and
269 * CONNECT ip:port.
270 */
271 if ((sess->fe->options2 & PR_O2_USE_PXHDR) &&
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100272 *HTX_SL_REQ_UPTR(sl) != '/' && *HTX_SL_REQ_UPTR(sl) != '*')
Christopher Faulete0768eb2018-10-03 16:38:02 +0200273 txn->flags |= TX_USE_PX_CONN;
274
Christopher Faulete0768eb2018-10-03 16:38:02 +0200275 /* 5: we may need to capture headers */
276 if (unlikely((s->logs.logwait & LW_REQHDR) && s->req_cap))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200277 http_capture_headers(htx, s->req_cap, sess->fe->req_cap);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200278
Christopher Faulete0768eb2018-10-03 16:38:02 +0200279 /* we may have to wait for the request's body */
Christopher Faulet9768c262018-10-22 09:34:31 +0200280 if (s->be->options & PR_O_WREQ_BODY)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200281 req->analysers |= AN_REQ_HTTP_BODY;
282
283 /*
284 * RFC7234#4:
285 * A cache MUST write through requests with methods
286 * that are unsafe (Section 4.2.1 of [RFC7231]) to
287 * the origin server; i.e., a cache is not allowed
288 * to generate a reply to such a request before
289 * having forwarded the request and having received
290 * a corresponding response.
291 *
292 * RFC7231#4.2.1:
293 * Of the request methods defined by this
294 * specification, the GET, HEAD, OPTIONS, and TRACE
295 * methods are defined to be safe.
296 */
297 if (likely(txn->meth == HTTP_METH_GET ||
298 txn->meth == HTTP_METH_HEAD ||
299 txn->meth == HTTP_METH_OPTIONS ||
300 txn->meth == HTTP_METH_TRACE))
301 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
302
303 /* end of job, return OK */
304 req->analysers &= ~an_bit;
305 req->analyse_exp = TICK_ETERNITY;
Christopher Faulet9768c262018-10-22 09:34:31 +0200306
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100307 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200308 return 1;
309
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200310 return_int_err:
311 txn->status = 500;
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200312 if (!(s->flags & SF_ERR_MASK))
313 s->flags |= SF_ERR_INTERNAL;
Willy Tarreau4781b152021-04-06 13:53:36 +0200314 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
William Lallemand36119de2021-03-08 15:26:48 +0100315 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200316 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200317 goto return_prx_cond;
318
Christopher Faulete0768eb2018-10-03 16:38:02 +0200319 return_bad_req:
Christopher Faulet9768c262018-10-22 09:34:31 +0200320 txn->status = 400;
Willy Tarreau4781b152021-04-06 13:53:36 +0200321 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req);
William Lallemand36119de2021-03-08 15:26:48 +0100322 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200323 _HA_ATOMIC_INC(&sess->listener->counters->failed_req);
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200324 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200325
326 return_prx_cond:
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200327 http_reply_and_close(s, txn->status, http_error_message(s));
328
Christopher Faulete0768eb2018-10-03 16:38:02 +0200329 if (!(s->flags & SF_ERR_MASK))
330 s->flags |= SF_ERR_PRXCOND;
331 if (!(s->flags & SF_FINST_MASK))
332 s->flags |= SF_FINST_R;
333
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100334 DBG_TRACE_DEVEL("leaving on error",
335 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200336 return 0;
337}
338
339
340/* This stream analyser runs all HTTP request processing which is common to
341 * frontends and backends, which means blocking ACLs, filters, connection-close,
342 * reqadd, stats and redirects. This is performed for the designated proxy.
343 * It returns 1 if the processing can continue on next analysers, or zero if it
344 * either needs more data or wants to immediately abort the request (eg: deny,
345 * error, ...).
346 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200347int http_process_req_common(struct stream *s, struct channel *req, int an_bit, struct proxy *px)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200348{
349 struct session *sess = s->sess;
350 struct http_txn *txn = s->txn;
351 struct http_msg *msg = &txn->req;
Christopher Fauletff2759f2018-10-24 11:13:16 +0200352 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200353 struct redirect_rule *rule;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200354 enum rule_result verdict;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200355 struct connection *conn = objt_conn(sess->origin);
356
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100357 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200358
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100359 htx = htxbuf(&req->buf);
Christopher Fauletff2759f2018-10-24 11:13:16 +0200360
Christopher Faulet1907ccc2019-04-29 13:12:02 +0200361 /* just in case we have some per-backend tracking. Only called the first
362 * execution of the analyser. */
363 if (!s->current_rule || s->current_rule_list != &px->http_req_rules)
364 stream_inc_be_http_req_ctr(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200365
366 /* evaluate http-request rules */
367 if (!LIST_ISEMPTY(&px->http_req_rules)) {
Christopher Fauletb58f62b2020-01-13 16:40:13 +0100368 verdict = http_req_get_intercept_rule(px, &px->http_req_rules, s);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200369
370 switch (verdict) {
371 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
372 goto return_prx_yield;
373
374 case HTTP_RULE_RES_CONT:
375 case HTTP_RULE_RES_STOP: /* nothing to do */
376 break;
377
378 case HTTP_RULE_RES_DENY: /* deny or tarpit */
379 if (txn->flags & TX_CLTARPIT)
380 goto tarpit;
381 goto deny;
382
383 case HTTP_RULE_RES_ABRT: /* abort request, response already sent. Eg: auth */
384 goto return_prx_cond;
385
386 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
387 goto done;
388
389 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
390 goto return_bad_req;
Christopher Faulet3a26bee2019-12-16 12:47:40 +0100391
392 case HTTP_RULE_RES_ERROR: /* failed with a bad request */
393 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200394 }
395 }
396
397 if (conn && (conn->flags & CO_FL_EARLY_DATA) &&
Olivier Houchard220a26c2020-01-23 14:57:36 +0100398 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_SSL_WAIT_HS))) {
Christopher Fauletff2759f2018-10-24 11:13:16 +0200399 struct http_hdr_ctx ctx;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200400
Christopher Fauletff2759f2018-10-24 11:13:16 +0200401 ctx.blk = NULL;
402 if (!http_find_header(htx, ist("Early-Data"), &ctx, 0)) {
403 if (unlikely(!http_add_header(htx, ist("Early-Data"), ist("1"))))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100404 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200405 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200406 }
407
408 /* OK at this stage, we know that the request was accepted according to
409 * the http-request rules, we can check for the stats. Note that the
410 * URI is detected *before* the req* rules in order not to be affected
411 * by a possible reqrep, while they are processed *after* so that a
412 * reqdeny can still block them. This clearly needs to change in 1.6!
413 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200414 if (!s->target && http_stats_check_uri(s, txn, px)) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200415 s->target = &http_stats_applet.obj_type;
Willy Tarreau14bfe9a2018-12-19 15:19:27 +0100416 if (unlikely(!si_register_handler(&s->si[1], objt_applet(s->target)))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200417 s->logs.tv_request = now;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200418 if (!(s->flags & SF_ERR_MASK))
419 s->flags |= SF_ERR_RESOURCE;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100420 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200421 }
422
423 /* parse the whole stats request and extract the relevant information */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200424 http_handle_stats(s, req);
Christopher Fauletb58f62b2020-01-13 16:40:13 +0100425 verdict = http_req_get_intercept_rule(px, &px->uri_auth->http_req_rules, s);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200426 /* not all actions implemented: deny, allow, auth */
427
428 if (verdict == HTTP_RULE_RES_DENY) /* stats http-request deny */
429 goto deny;
430
431 if (verdict == HTTP_RULE_RES_ABRT) /* stats auth / stats http-request auth */
432 goto return_prx_cond;
Christopher Faulet3a26bee2019-12-16 12:47:40 +0100433
434 if (verdict == HTTP_RULE_RES_BADREQ) /* failed with a bad request */
435 goto return_bad_req;
436
437 if (verdict == HTTP_RULE_RES_ERROR) /* failed with a bad request */
438 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200439 }
440
Christopher Faulet2571bc62019-03-01 11:44:26 +0100441 /* Proceed with the applets now. */
442 if (unlikely(objt_applet(s->target))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200443 if (sess->fe == s->be) /* report it if the request was intercepted by the frontend */
Willy Tarreau4781b152021-04-06 13:53:36 +0200444 _HA_ATOMIC_INC(&sess->fe->fe_counters.intercepted_req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200445
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200446 if (http_handle_expect_hdr(s, htx, msg) == -1)
Christopher Fauletb8a53712019-12-16 11:29:38 +0100447 goto return_int_err;
Christopher Fauletbcf242a2019-03-01 11:36:26 +0100448
Christopher Faulete0768eb2018-10-03 16:38:02 +0200449 if (!(s->flags & SF_ERR_MASK)) // this is not really an error but it is
450 s->flags |= SF_ERR_LOCAL; // to mark that it comes from the proxy
451 if (!(s->flags & SF_FINST_MASK))
452 s->flags |= SF_FINST_R;
453
Christopher Fauletc2ac5e42021-03-08 18:20:09 +0100454 if (HAS_FILTERS(s))
455 req->analysers |= AN_REQ_FLT_HTTP_HDRS;
456
Christopher Faulete0768eb2018-10-03 16:38:02 +0200457 /* enable the minimally required analyzers to handle keep-alive and compression on the HTTP response */
458 req->analysers &= (AN_REQ_HTTP_BODY | AN_REQ_FLT_HTTP_HDRS | AN_REQ_FLT_END);
459 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
460 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Christopher Fauletbcf242a2019-03-01 11:36:26 +0100461
462 req->flags |= CF_SEND_DONTWAIT;
463 s->flags |= SF_ASSIGNED;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200464 goto done;
465 }
466
467 /* check whether we have some ACLs set to redirect this request */
468 list_for_each_entry(rule, &px->redirect_rules, list) {
469 if (rule->cond) {
470 int ret;
471
472 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
473 ret = acl_pass(ret);
474 if (rule->cond->pol == ACL_COND_UNLESS)
475 ret = !ret;
476 if (!ret)
477 continue;
478 }
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200479 if (!http_apply_redirect_rule(rule, s, txn))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100480 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200481 goto done;
482 }
483
484 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
485 * If this happens, then the data will not come immediately, so we must
486 * send all what we have without waiting. Note that due to the small gain
487 * in waiting for the body of the request, it's easier to simply put the
488 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
489 * itself once used.
490 */
491 req->flags |= CF_SEND_DONTWAIT;
492
493 done: /* done with this analyser, continue with next ones that the calling
494 * points will have set, if any.
495 */
496 req->analyse_exp = TICK_ETERNITY;
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +0500497 done_without_exp: /* done with this analyser, but don't reset the analyse_exp. */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200498 req->analysers &= ~an_bit;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100499 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200500 return 1;
501
502 tarpit:
503 /* Allow cookie logging
504 */
505 if (s->be->cookie_name || sess->fe->capture_name)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200506 http_manage_client_side_cookies(s, req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200507
508 /* When a connection is tarpitted, we use the tarpit timeout,
509 * which may be the same as the connect timeout if unspecified.
510 * If unset, then set it to zero because we really want it to
511 * eventually expire. We build the tarpit as an analyser.
512 */
Christopher Faulet202c6ce2019-01-07 14:57:35 +0100513 channel_htx_erase(&s->req, htx);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200514
515 /* wipe the request out so that we can drop the connection early
516 * if the client closes first.
517 */
518 channel_dont_connect(req);
519
Christopher Faulete0768eb2018-10-03 16:38:02 +0200520 req->analysers &= AN_REQ_FLT_END; /* remove switching rules etc... */
521 req->analysers |= AN_REQ_HTTP_TARPIT;
522 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
523 if (!req->analyse_exp)
524 req->analyse_exp = tick_add(now_ms, 0);
525 stream_inc_http_err_ctr(s);
Willy Tarreau4781b152021-04-06 13:53:36 +0200526 _HA_ATOMIC_INC(&sess->fe->fe_counters.denied_req);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100527 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau4781b152021-04-06 13:53:36 +0200528 _HA_ATOMIC_INC(&s->be->be_counters.denied_req);
William Lallemand36119de2021-03-08 15:26:48 +0100529 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200530 _HA_ATOMIC_INC(&sess->listener->counters->denied_req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200531 goto done_without_exp;
532
533 deny: /* this request was blocked (denied) */
534
535 /* Allow cookie logging
536 */
537 if (s->be->cookie_name || sess->fe->capture_name)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200538 http_manage_client_side_cookies(s, req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200539
Christopher Faulete0768eb2018-10-03 16:38:02 +0200540 s->logs.tv_request = now;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200541 stream_inc_http_err_ctr(s);
Willy Tarreau4781b152021-04-06 13:53:36 +0200542 _HA_ATOMIC_INC(&sess->fe->fe_counters.denied_req);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100543 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau4781b152021-04-06 13:53:36 +0200544 _HA_ATOMIC_INC(&s->be->be_counters.denied_req);
William Lallemand36119de2021-03-08 15:26:48 +0100545 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200546 _HA_ATOMIC_INC(&sess->listener->counters->denied_req);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100547 goto return_prx_err;
548
549 return_int_err:
550 txn->status = 500;
551 if (!(s->flags & SF_ERR_MASK))
552 s->flags |= SF_ERR_INTERNAL;
Willy Tarreau4781b152021-04-06 13:53:36 +0200553 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100554 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau4781b152021-04-06 13:53:36 +0200555 _HA_ATOMIC_INC(&s->be->be_counters.internal_errors);
William Lallemand36119de2021-03-08 15:26:48 +0100556 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200557 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100558 goto return_prx_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200559
560 return_bad_req:
Christopher Faulete0768eb2018-10-03 16:38:02 +0200561 txn->status = 400;
Willy Tarreau4781b152021-04-06 13:53:36 +0200562 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req);
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->failed_req);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100565 /* fall through */
566
567 return_prx_err:
568 http_reply_and_close(s, txn->status, http_error_message(s));
569 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200570
571 return_prx_cond:
572 if (!(s->flags & SF_ERR_MASK))
573 s->flags |= SF_ERR_PRXCOND;
574 if (!(s->flags & SF_FINST_MASK))
575 s->flags |= SF_FINST_R;
576
577 req->analysers &= AN_REQ_FLT_END;
578 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100579 DBG_TRACE_DEVEL("leaving on error",
580 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200581 return 0;
582
583 return_prx_yield:
584 channel_dont_connect(req);
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100585 DBG_TRACE_DEVEL("waiting for more data",
586 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200587 return 0;
588}
589
590/* This function performs all the processing enabled for the current request.
591 * It returns 1 if the processing can continue on next analysers, or zero if it
592 * needs more data, encounters an error, or wants to immediately abort the
593 * request. It relies on buffers flags, and updates s->req.analysers.
594 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200595int http_process_request(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200596{
597 struct session *sess = s->sess;
598 struct http_txn *txn = s->txn;
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200599 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200600 struct connection *cli_conn = objt_conn(strm_sess(s)->origin);
601
Christopher Faulet8bebd2f2020-10-06 17:54:56 +0200602 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200603
604 /*
605 * Right now, we know that we have processed the entire headers
606 * and that unwanted requests have been filtered out. We can do
607 * whatever we want with the remaining request. Also, now we
608 * may have separate values for ->fe, ->be.
609 */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100610 htx = htxbuf(&req->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200611
612 /*
613 * If HTTP PROXY is set we simply get remote server address parsing
Willy Tarreau1c8d32b2019-07-18 15:47:45 +0200614 * incoming request.
Christopher Faulete0768eb2018-10-03 16:38:02 +0200615 */
616 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SF_ADDR_SET)) {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100617 struct htx_sl *sl;
618 struct ist uri, path;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200619
Willy Tarreau9b7587a2020-10-15 07:32:10 +0200620 if (!sockaddr_alloc(&s->target_addr, NULL, 0)) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200621 if (!(s->flags & SF_ERR_MASK))
622 s->flags |= SF_ERR_RESOURCE;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100623 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200624 }
Christopher Faulet297fbb42019-05-13 14:41:27 +0200625 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100626 uri = htx_sl_req_uri(sl);
627 path = http_get_path(uri);
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200628
Willy Tarreau1c8d32b2019-07-18 15:47:45 +0200629 if (url2sa(uri.ptr, uri.len - path.len, s->target_addr, NULL) == -1)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200630 goto return_bad_req;
631
Willy Tarreau1c8d32b2019-07-18 15:47:45 +0200632 s->target = &s->be->obj_type;
633 s->flags |= SF_ADDR_SET | SF_ASSIGNED;
634
Christopher Faulete0768eb2018-10-03 16:38:02 +0200635 /* if the path was found, we have to remove everything between
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200636 * uri.ptr and path.ptr (excluded). If it was not found, we need
637 * to replace from all the uri by a single "/".
638 *
Ilya Shipitsin46a030c2020-07-05 16:36:08 +0500639 * Instead of rewriting the whole start line, we just update
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100640 * the star-line URI. Some space will be lost but it should be
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200641 * insignificant.
Christopher Faulete0768eb2018-10-03 16:38:02 +0200642 */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100643 istcpy(&uri, (path.len ? path : ist("/")), uri.len);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200644 }
645
646 /*
647 * 7: Now we can work with the cookies.
648 * Note that doing so might move headers in the request, but
649 * the fields will stay coherent and the URI will not move.
650 * This should only be performed in the backend.
651 */
652 if (s->be->cookie_name || sess->fe->capture_name)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200653 http_manage_client_side_cookies(s, req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200654
Tim Duesterhus2825b4b2020-02-28 15:13:34 +0100655 /* 8: Generate unique ID if a "unique-id-format" is defined.
656 *
657 * A unique ID is generated even when it is not sent to ensure that the ID can make use of
658 * fetches only available in the HTTP request processing stage.
659 */
660 if (!LIST_ISEMPTY(&sess->fe->format_unique_id)) {
Tim Duesterhusa17e6622020-03-05 20:19:02 +0100661 struct ist unique_id = stream_generate_unique_id(s, &sess->fe->format_unique_id);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200662
Tim Duesterhusa17e6622020-03-05 20:19:02 +0100663 if (!isttest(unique_id)) {
Christopher Fauletb8a53712019-12-16 11:29:38 +0100664 if (!(s->flags & SF_ERR_MASK))
665 s->flags |= SF_ERR_RESOURCE;
666 goto return_int_err;
667 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200668
Tim Duesterhus2825b4b2020-02-28 15:13:34 +0100669 /* send unique ID if a "unique-id-header" is defined */
Tim Duesterhus0643b0e2020-03-05 17:56:35 +0100670 if (isttest(sess->fe->header_unique_id) &&
Tim Duesterhusa17e6622020-03-05 20:19:02 +0100671 unlikely(!http_add_header(htx, sess->fe->header_unique_id, s->unique_id)))
Tim Duesterhus2825b4b2020-02-28 15:13:34 +0100672 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200673 }
674
675 /*
676 * 9: add X-Forwarded-For if either the frontend or the backend
677 * asks for it.
678 */
679 if ((sess->fe->options | s->be->options) & PR_O_FWDFOR) {
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200680 struct http_hdr_ctx ctx = { .blk = NULL };
681 struct ist hdr = ist2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : sess->fe->fwdfor_hdr_name,
682 s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : sess->fe->fwdfor_hdr_len);
683
Christopher Faulete0768eb2018-10-03 16:38:02 +0200684 if (!((sess->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200685 http_find_header(htx, hdr, &ctx, 0)) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200686 /* The header is set to be added only if none is present
687 * and we found it, so don't do anything.
688 */
689 }
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200690 else if (cli_conn && conn_get_src(cli_conn) && cli_conn->src->ss_family == AF_INET) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200691 /* Add an X-Forwarded-For header unless the source IP is
692 * in the 'except' network range.
693 */
Christopher Faulet5d1def62021-02-26 09:19:15 +0100694 if (ipcmp2net(cli_conn->src, &sess->fe->except_xff_net) &&
695 ipcmp2net(cli_conn->src, &s->be->except_xff_net)) {
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200696 unsigned char *pn = (unsigned char *)&((struct sockaddr_in *)cli_conn->src)->sin_addr;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200697
698 /* Note: we rely on the backend to get the header name to be used for
699 * x-forwarded-for, because the header is really meant for the backends.
700 * However, if the backend did not specify any option, we have to rely
701 * on the frontend's header name.
702 */
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200703 chunk_printf(&trash, "%d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
704 if (unlikely(!http_add_header(htx, hdr, ist2(trash.area, trash.data))))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100705 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200706 }
707 }
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200708 else if (cli_conn && conn_get_src(cli_conn) && cli_conn->src->ss_family == AF_INET6) {
Christopher Faulet5d1def62021-02-26 09:19:15 +0100709 /* Add an X-Forwarded-For header unless the source IP is
710 * in the 'except' network range.
Christopher Faulete0768eb2018-10-03 16:38:02 +0200711 */
Christopher Faulet5d1def62021-02-26 09:19:15 +0100712 if (ipcmp2net(cli_conn->src, &sess->fe->except_xff_net) &&
713 ipcmp2net(cli_conn->src, &s->be->except_xff_net)) {
714 char pn[INET6_ADDRSTRLEN];
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200715
Christopher Faulet5d1def62021-02-26 09:19:15 +0100716 inet_ntop(AF_INET6,
717 (const void *)&((struct sockaddr_in6 *)(cli_conn->src))->sin6_addr,
718 pn, sizeof(pn));
Christopher Faulete0768eb2018-10-03 16:38:02 +0200719
Christopher Faulet5d1def62021-02-26 09:19:15 +0100720 /* Note: we rely on the backend to get the header name to be used for
721 * x-forwarded-for, because the header is really meant for the backends.
722 * However, if the backend did not specify any option, we have to rely
723 * on the frontend's header name.
724 */
725 chunk_printf(&trash, "%s", pn);
726 if (unlikely(!http_add_header(htx, hdr, ist2(trash.area, trash.data))))
727 goto return_int_err;
728 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200729 }
730 }
731
732 /*
733 * 10: add X-Original-To if either the frontend or the backend
734 * asks for it.
735 */
736 if ((sess->fe->options | s->be->options) & PR_O_ORGTO) {
Christopher Faulet5d1def62021-02-26 09:19:15 +0100737 struct ist hdr = ist2(s->be->orgto_hdr_len ? s->be->orgto_hdr_name : sess->fe->orgto_hdr_name,
738 s->be->orgto_hdr_len ? s->be->orgto_hdr_len : sess->fe->orgto_hdr_len);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200739
Christopher Fauletcccded92021-02-26 12:45:56 +0100740 if (cli_conn && conn_get_dst(cli_conn) && cli_conn->dst->ss_family == AF_INET) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200741 /* Add an X-Original-To header unless the destination IP is
742 * in the 'except' network range.
743 */
Christopher Faulet5d1def62021-02-26 09:19:15 +0100744 if (ipcmp2net(cli_conn->dst, &sess->fe->except_xot_net) &&
745 ipcmp2net(cli_conn->dst, &s->be->except_xot_net)) {
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200746 unsigned char *pn = (unsigned char *)&((struct sockaddr_in *)cli_conn->dst)->sin_addr;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200747
748 /* Note: we rely on the backend to get the header name to be used for
749 * x-original-to, because the header is really meant for the backends.
750 * However, if the backend did not specify any option, we have to rely
751 * on the frontend's header name.
752 */
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200753 chunk_printf(&trash, "%d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
754 if (unlikely(!http_add_header(htx, hdr, ist2(trash.area, trash.data))))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100755 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200756 }
757 }
Christopher Faulet5d1def62021-02-26 09:19:15 +0100758 else if (cli_conn && conn_get_dst(cli_conn) && cli_conn->dst->ss_family == AF_INET6) {
759 /* Add an X-Original-To header unless the source IP is
760 * in the 'except' network range.
761 */
762 if (ipcmp2net(cli_conn->dst, &sess->fe->except_xot_net) &&
763 ipcmp2net(cli_conn->dst, &s->be->except_xot_net)) {
764 char pn[INET6_ADDRSTRLEN];
765
766 inet_ntop(AF_INET6,
767 (const void *)&((struct sockaddr_in6 *)(cli_conn->dst))->sin6_addr,
768 pn, sizeof(pn));
769
770 /* Note: we rely on the backend to get the header name to be used for
771 * x-forwarded-for, because the header is really meant for the backends.
772 * However, if the backend did not specify any option, we have to rely
773 * on the frontend's header name.
774 */
775 chunk_printf(&trash, "%s", pn);
776 if (unlikely(!http_add_header(htx, hdr, ist2(trash.area, trash.data))))
777 goto return_int_err;
778 }
779 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200780 }
781
Christopher Fauletc2ac5e42021-03-08 18:20:09 +0100782 /* Filter the request headers if there are filters attached to the
783 * stream.
784 */
785 if (HAS_FILTERS(s))
786 req->analysers |= AN_REQ_FLT_HTTP_HDRS;
787
Christopher Faulete0768eb2018-10-03 16:38:02 +0200788 /* If we have no server assigned yet and we're balancing on url_param
789 * with a POST request, we may be interested in checking the body for
790 * that parameter. This will be done in another analyser.
791 */
792 if (!(s->flags & (SF_ASSIGNED|SF_DIRECT)) &&
Willy Tarreau089eaa02019-01-14 15:17:46 +0100793 s->txn->meth == HTTP_METH_POST &&
794 (s->be->lbprm.algo & BE_LB_ALGO) == BE_LB_ALGO_PH) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200795 channel_dont_connect(req);
796 req->analysers |= AN_REQ_HTTP_BODY;
797 }
798
799 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
800 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau1a18b542018-12-11 16:37:42 +0100801
Christopher Faulete0768eb2018-10-03 16:38:02 +0200802 /* We expect some data from the client. Unless we know for sure
803 * we already have a full request, we have to re-enable quick-ack
804 * in case we previously disabled it, otherwise we might cause
805 * the client to delay further data.
806 */
William Lallemand36119de2021-03-08 15:26:48 +0100807 if ((sess->listener && (sess->listener->options & LI_O_NOQUICKACK)) && !(htx->flags & HTX_FL_EOM))
Willy Tarreau1a18b542018-12-11 16:37:42 +0100808 conn_set_quickack(cli_conn, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200809
810 /*************************************************************
811 * OK, that's finished for the headers. We have done what we *
812 * could. Let's switch to the DATA state. *
813 ************************************************************/
814 req->analyse_exp = TICK_ETERNITY;
815 req->analysers &= ~an_bit;
816
817 s->logs.tv_request = now;
818 /* OK let's go on with the BODY now */
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100819 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200820 return 1;
821
Christopher Fauletb8a53712019-12-16 11:29:38 +0100822 return_int_err:
823 txn->status = 500;
824 if (!(s->flags & SF_ERR_MASK))
825 s->flags |= SF_ERR_INTERNAL;
Willy Tarreau4781b152021-04-06 13:53:36 +0200826 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100827 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau4781b152021-04-06 13:53:36 +0200828 _HA_ATOMIC_INC(&s->be->be_counters.internal_errors);
William Lallemand36119de2021-03-08 15:26:48 +0100829 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200830 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100831 goto return_prx_cond;
832
Christopher Faulete0768eb2018-10-03 16:38:02 +0200833 return_bad_req: /* let's centralize all bad requests */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200834 txn->status = 400;
Willy Tarreau4781b152021-04-06 13:53:36 +0200835 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req);
William Lallemand36119de2021-03-08 15:26:48 +0100836 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200837 _HA_ATOMIC_INC(&sess->listener->counters->failed_req);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100838 /* fall through */
839
840 return_prx_cond:
841 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +0200842
843 if (!(s->flags & SF_ERR_MASK))
844 s->flags |= SF_ERR_PRXCOND;
845 if (!(s->flags & SF_FINST_MASK))
846 s->flags |= SF_FINST_R;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100847
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100848 DBG_TRACE_DEVEL("leaving on error",
849 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200850 return 0;
851}
852
853/* This function is an analyser which processes the HTTP tarpit. It always
854 * returns zero, at the beginning because it prevents any other processing
855 * from occurring, and at the end because it terminates the request.
856 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200857int http_process_tarpit(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200858{
859 struct http_txn *txn = s->txn;
860
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100861 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, &txn->req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200862 /* This connection is being tarpitted. The CLIENT side has
863 * already set the connect expiration date to the right
864 * timeout. We just have to check that the client is still
865 * there and that the timeout has not expired.
866 */
867 channel_dont_connect(req);
868 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100869 !tick_is_expired(req->analyse_exp, now_ms)) {
870 DBG_TRACE_DEVEL("waiting for tarpit timeout expiry",
871 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200872 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100873 }
874
Christopher Faulete0768eb2018-10-03 16:38:02 +0200875
876 /* We will set the queue timer to the time spent, just for
877 * logging purposes. We fake a 500 server error, so that the
878 * attacker will not suspect his connection has been tarpitted.
879 * It will not cause trouble to the logs because we can exclude
880 * the tarpitted connections by filtering on the 'PT' status flags.
881 */
882 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
883
Christopher Faulet8dfeccf2020-05-15 14:16:29 +0200884 http_reply_and_close(s, txn->status, (!(req->flags & CF_READ_ERROR) ? http_error_message(s) : NULL));
Christopher Faulet5cb513a2020-05-13 17:56:56 +0200885
Christopher Faulete0768eb2018-10-03 16:38:02 +0200886 if (!(s->flags & SF_ERR_MASK))
887 s->flags |= SF_ERR_PRXCOND;
888 if (!(s->flags & SF_FINST_MASK))
889 s->flags |= SF_FINST_T;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100890
891 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200892 return 0;
893}
894
895/* This function is an analyser which waits for the HTTP request body. It waits
896 * for either the buffer to be full, or the full advertised contents to have
897 * reached the buffer. It must only be called after the standard HTTP request
898 * processing has occurred, because it expects the request to be parsed and will
899 * look for the Expect header. It may send a 100-Continue interim response. It
900 * takes in input any state starting from HTTP_MSG_BODY and leaves with one of
901 * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it
902 * needs to read more data, or 1 once it has completed its analysis.
903 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200904int http_wait_for_request_body(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200905{
906 struct session *sess = s->sess;
907 struct http_txn *txn = s->txn;
908 struct http_msg *msg = &s->txn->req;
909
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100910 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Fauletf76ebe82018-10-24 11:16:22 +0200911
Christopher Fauletf76ebe82018-10-24 11:16:22 +0200912
Christopher Faulet021a8e42021-03-29 10:46:38 +0200913 switch (http_wait_for_msg_body(s, req, s->be->timeout.httpreq, 0)) {
914 case HTTP_RULE_RES_CONT:
915 goto http_end;
916 case HTTP_RULE_RES_YIELD:
917 goto missing_data_or_waiting;
918 case HTTP_RULE_RES_BADREQ:
Willy Tarreau4236f032019-03-05 10:43:32 +0100919 goto return_bad_req;
Christopher Faulet021a8e42021-03-29 10:46:38 +0200920 case HTTP_RULE_RES_ERROR:
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200921 goto return_int_err;
Christopher Faulet021a8e42021-03-29 10:46:38 +0200922 case HTTP_RULE_RES_ABRT:
Christopher Fauletb8a53712019-12-16 11:29:38 +0100923 goto return_prx_cond;
Christopher Faulet021a8e42021-03-29 10:46:38 +0200924 default:
925 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200926 }
927
928 http_end:
929 /* The situation will not evolve, so let's give up on the analysis. */
930 s->logs.tv_request = now; /* update the request timer to reflect full request */
931 req->analysers &= ~an_bit;
932 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100933 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200934 return 1;
935
Christopher Faulet021a8e42021-03-29 10:46:38 +0200936 missing_data_or_waiting:
937 channel_dont_connect(req);
938 DBG_TRACE_DEVEL("waiting for more data",
939 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
940 return 0;
941
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200942 return_int_err:
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200943 txn->status = 500;
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200944 if (!(s->flags & SF_ERR_MASK))
945 s->flags |= SF_ERR_INTERNAL;
Willy Tarreau4781b152021-04-06 13:53:36 +0200946 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100947 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau4781b152021-04-06 13:53:36 +0200948 _HA_ATOMIC_INC(&s->be->be_counters.internal_errors);
William Lallemand36119de2021-03-08 15:26:48 +0100949 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200950 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Faulet021a8e42021-03-29 10:46:38 +0200951 goto return_prx_err;
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200952
Christopher Faulete0768eb2018-10-03 16:38:02 +0200953 return_bad_req: /* let's centralize all bad requests */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200954 txn->status = 400;
Willy Tarreau4781b152021-04-06 13:53:36 +0200955 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req);
William Lallemand36119de2021-03-08 15:26:48 +0100956 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200957 _HA_ATOMIC_INC(&sess->listener->counters->failed_req);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100958 /* fall through */
959
Christopher Faulet021a8e42021-03-29 10:46:38 +0200960 return_prx_err:
Christopher Fauletb8a53712019-12-16 11:29:38 +0100961 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet021a8e42021-03-29 10:46:38 +0200962 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200963
Christopher Faulet021a8e42021-03-29 10:46:38 +0200964 return_prx_cond:
Christopher Faulete0768eb2018-10-03 16:38:02 +0200965 if (!(s->flags & SF_ERR_MASK))
966 s->flags |= SF_ERR_PRXCOND;
967 if (!(s->flags & SF_FINST_MASK))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100968 s->flags |= (msg->msg_state < HTTP_MSG_DATA ? SF_FINST_R : SF_FINST_D);
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200969
Christopher Faulete0768eb2018-10-03 16:38:02 +0200970 req->analysers &= AN_REQ_FLT_END;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100971 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100972 DBG_TRACE_DEVEL("leaving on error",
973 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200974 return 0;
975}
976
977/* This function is an analyser which forwards request body (including chunk
978 * sizes if any). It is called as soon as we must forward, even if we forward
979 * zero byte. The only situation where it must not be called is when we're in
980 * tunnel mode and we want to forward till the close. It's used both to forward
981 * remaining data and to resync after end of body. It expects the msg_state to
982 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
983 * read more data, or 1 once we can go on with next request or end the stream.
984 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
985 * bytes of pending data + the headers if not already done.
986 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200987int http_request_forward_body(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200988{
989 struct session *sess = s->sess;
990 struct http_txn *txn = s->txn;
Christopher Faulet9768c262018-10-22 09:34:31 +0200991 struct http_msg *msg = &txn->req;
992 struct htx *htx;
Christopher Faulet93e02d82019-03-08 14:18:50 +0100993 short status = 0;
Christopher Fauletaed82cf2018-11-30 22:22:32 +0100994 int ret;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200995
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100996 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200997
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100998 htx = htxbuf(&req->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200999
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001000 if (htx->flags & HTX_FL_PARSING_ERROR)
1001 goto return_bad_req;
1002 if (htx->flags & HTX_FL_PROCESSING_ERROR)
1003 goto return_int_err;
1004
Christopher Faulete0768eb2018-10-03 16:38:02 +02001005 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
1006 ((req->flags & CF_SHUTW) && (req->to_forward || co_data(req)))) {
1007 /* Output closed while we were sending data. We must abort and
1008 * wake the other side up.
Christopher Fauletf506d962021-04-27 10:56:28 +02001009 *
1010 * If we have finished to send the request and the response is
1011 * still in progress, don't catch write error on the request
1012 * side if it is in fact a read error on the server side.
Christopher Faulete0768eb2018-10-03 16:38:02 +02001013 */
Christopher Fauletf506d962021-04-27 10:56:28 +02001014 if (msg->msg_state == HTTP_MSG_DONE && (s->res.flags & CF_READ_ERROR) && s->res.analysers)
1015 return 0;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001016
Olivier Houchard29cac3c2019-07-12 15:48:58 +02001017 /* Don't abort yet if we had L7 retries activated and it
1018 * was a write error, we may recover.
1019 */
1020 if (!(req->flags & (CF_READ_ERROR | CF_READ_TIMEOUT)) &&
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001021 (s->si[1].flags & SI_FL_L7_RETRY)) {
1022 DBG_TRACE_DEVEL("leaving on L7 retry",
1023 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Olivier Houchard29cac3c2019-07-12 15:48:58 +02001024 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001025 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001026 msg->msg_state = HTTP_MSG_ERROR;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001027 http_end_request(s);
1028 http_end_response(s);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001029 DBG_TRACE_DEVEL("leaving on error",
1030 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001031 return 1;
1032 }
1033
1034 /* Note that we don't have to send 100-continue back because we don't
1035 * need the data to complete our job, and it's up to the server to
1036 * decide whether to return 100, 417 or anything else in return of
1037 * an "Expect: 100-continue" header.
1038 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001039 if (msg->msg_state == HTTP_MSG_BODY)
1040 msg->msg_state = HTTP_MSG_DATA;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001041
Christopher Faulete0768eb2018-10-03 16:38:02 +02001042 /* in most states, we should abort in case of early close */
1043 channel_auto_close(req);
1044
1045 if (req->to_forward) {
Christopher Faulet66af0b22019-03-22 14:54:52 +01001046 if (req->to_forward == CHN_INFINITE_FORWARD) {
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001047 if (req->flags & CF_EOI)
1048 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet66af0b22019-03-22 14:54:52 +01001049 }
1050 else {
1051 /* We can't process the buffer's contents yet */
1052 req->flags |= CF_WAKE_WRITE;
1053 goto missing_data_or_waiting;
1054 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001055 }
1056
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001057 if (msg->msg_state >= HTTP_MSG_ENDING)
1058 goto ending;
1059
1060 if (txn->meth == HTTP_METH_CONNECT) {
1061 msg->msg_state = HTTP_MSG_ENDING;
1062 goto ending;
1063 }
1064
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001065 /* Forward input data. We get it by removing all outgoing data not
1066 * forwarded yet from HTX data size. If there are some data filters, we
1067 * let them decide the amount of data to forward.
Christopher Faulet9768c262018-10-22 09:34:31 +02001068 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001069 if (HAS_REQ_DATA_FILTERS(s)) {
1070 ret = flt_http_payload(s, msg, htx->data);
1071 if (ret < 0)
1072 goto return_bad_req;
Christopher Faulet421e7692019-06-13 11:16:45 +02001073 c_adv(req, ret);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001074 }
1075 else {
Christopher Faulet421e7692019-06-13 11:16:45 +02001076 c_adv(req, htx->data - co_data(req));
Christopher Faulet66af0b22019-03-22 14:54:52 +01001077 if (msg->flags & HTTP_MSGF_XFER_LEN)
1078 channel_htx_forward_forever(req, htx);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001079 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001080
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001081 if (htx->data != co_data(req))
1082 goto missing_data_or_waiting;
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001083
Christopher Faulet9768c262018-10-22 09:34:31 +02001084 /* Check if the end-of-message is reached and if so, switch the message
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001085 * in HTTP_MSG_ENDING state. Then if all data was marked to be
1086 * forwarded, set the state to HTTP_MSG_DONE.
Christopher Faulet9768c262018-10-22 09:34:31 +02001087 */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001088 if (!(htx->flags & HTX_FL_EOM))
Christopher Faulet9768c262018-10-22 09:34:31 +02001089 goto missing_data_or_waiting;
1090
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001091 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet9768c262018-10-22 09:34:31 +02001092
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001093 ending:
Christopher Faulet2151cdd2020-07-22 16:34:59 +02001094 req->flags &= ~CF_EXPECT_MORE; /* no more data are expected */
1095
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001096 /* other states, ENDING...TUNNEL */
1097 if (msg->msg_state >= HTTP_MSG_DONE)
1098 goto done;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001099
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001100 if (HAS_REQ_DATA_FILTERS(s)) {
1101 ret = flt_http_end(s, msg);
1102 if (ret <= 0) {
1103 if (!ret)
1104 goto missing_data_or_waiting;
1105 goto return_bad_req;
1106 }
1107 }
1108
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001109 if (txn->meth == HTTP_METH_CONNECT)
1110 msg->msg_state = HTTP_MSG_TUNNEL;
1111 else {
1112 msg->msg_state = HTTP_MSG_DONE;
1113 req->to_forward = 0;
1114 }
1115
1116 done:
1117 /* we don't want to forward closes on DONE except in tunnel mode. */
1118 if (!(txn->flags & TX_CON_WANT_TUN))
1119 channel_dont_close(req);
1120
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001121 http_end_request(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001122 if (!(req->analysers & an_bit)) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001123 http_end_response(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001124 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
1125 if (req->flags & CF_SHUTW) {
1126 /* request errors are most likely due to the
1127 * server aborting the transfer. */
Christopher Faulet93e02d82019-03-08 14:18:50 +01001128 goto return_srv_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001129 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001130 goto return_bad_req;
1131 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001132 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001133 return 1;
1134 }
1135
1136 /* If "option abortonclose" is set on the backend, we want to monitor
1137 * the client's connection and forward any shutdown notification to the
1138 * server, which will decide whether to close or to go on processing the
1139 * request. We only do that in tunnel mode, and not in other modes since
1140 * it can be abused to exhaust source ports. */
Christopher Faulet769d0e92019-03-22 14:23:18 +01001141 if (s->be->options & PR_O_ABRT_CLOSE) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001142 channel_auto_read(req);
Christopher Fauletc41547b2019-07-16 14:32:23 +02001143 if ((req->flags & (CF_SHUTR|CF_READ_NULL)) && !(txn->flags & TX_CON_WANT_TUN))
Christopher Faulete0768eb2018-10-03 16:38:02 +02001144 s->si[1].flags |= SI_FL_NOLINGER;
1145 channel_auto_close(req);
1146 }
1147 else if (s->txn->meth == HTTP_METH_POST) {
1148 /* POST requests may require to read extra CRLF sent by broken
1149 * browsers and which could cause an RST to be sent upon close
1150 * on some systems (eg: Linux). */
1151 channel_auto_read(req);
1152 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001153 DBG_TRACE_DEVEL("waiting for the end of the HTTP txn",
1154 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001155 return 0;
1156
1157 missing_data_or_waiting:
1158 /* stop waiting for data if the input is closed before the end */
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001159 if (msg->msg_state < HTTP_MSG_ENDING && req->flags & CF_SHUTR)
Christopher Faulet93e02d82019-03-08 14:18:50 +01001160 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001161
1162 waiting:
1163 /* waiting for the last bits to leave the buffer */
1164 if (req->flags & CF_SHUTW)
Christopher Faulet93e02d82019-03-08 14:18:50 +01001165 goto return_srv_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001166
1167 /* When TE: chunked is used, we need to get there again to parse remaining
1168 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
1169 * And when content-length is used, we never want to let the possible
1170 * shutdown be forwarded to the other side, as the state machine will
1171 * take care of it once the client responds. It's also important to
1172 * prevent TIME_WAITs from accumulating on the backend side, and for
1173 * HTTP/2 where the last frame comes with a shutdown.
1174 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001175 if (msg->flags & HTTP_MSGF_XFER_LEN)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001176 channel_dont_close(req);
1177
1178 /* We know that more data are expected, but we couldn't send more that
1179 * what we did. So we always set the CF_EXPECT_MORE flag so that the
1180 * system knows it must not set a PUSH on this first part. Interactive
1181 * modes are already handled by the stream sock layer. We must not do
1182 * this in content-length mode because it could present the MSG_MORE
1183 * flag with the last block of forwarded data, which would cause an
1184 * additional delay to be observed by the receiver.
1185 */
Christopher Faulet2151cdd2020-07-22 16:34:59 +02001186 if (HAS_REQ_DATA_FILTERS(s))
Christopher Faulete0768eb2018-10-03 16:38:02 +02001187 req->flags |= CF_EXPECT_MORE;
1188
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001189 DBG_TRACE_DEVEL("waiting for more data to forward",
1190 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001191 return 0;
1192
Christopher Faulet93e02d82019-03-08 14:18:50 +01001193 return_cli_abort:
Willy Tarreau4781b152021-04-06 13:53:36 +02001194 _HA_ATOMIC_INC(&sess->fe->fe_counters.cli_aborts);
1195 _HA_ATOMIC_INC(&s->be->be_counters.cli_aborts);
William Lallemand36119de2021-03-08 15:26:48 +01001196 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001197 _HA_ATOMIC_INC(&sess->listener->counters->cli_aborts);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001198 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001199 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.cli_aborts);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001200 if (!(s->flags & SF_ERR_MASK))
1201 s->flags |= SF_ERR_CLICL;
1202 status = 400;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001203 goto return_prx_cond;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001204
1205 return_srv_abort:
Willy Tarreau4781b152021-04-06 13:53:36 +02001206 _HA_ATOMIC_INC(&sess->fe->fe_counters.srv_aborts);
1207 _HA_ATOMIC_INC(&s->be->be_counters.srv_aborts);
William Lallemand36119de2021-03-08 15:26:48 +01001208 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001209 _HA_ATOMIC_INC(&sess->listener->counters->srv_aborts);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001210 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001211 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.srv_aborts);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001212 if (!(s->flags & SF_ERR_MASK))
1213 s->flags |= SF_ERR_SRVCL;
1214 status = 502;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001215 goto return_prx_cond;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001216
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001217 return_int_err:
1218 if (!(s->flags & SF_ERR_MASK))
1219 s->flags |= SF_ERR_INTERNAL;
Willy Tarreau4781b152021-04-06 13:53:36 +02001220 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
1221 _HA_ATOMIC_INC(&s->be->be_counters.internal_errors);
William Lallemand36119de2021-03-08 15:26:48 +01001222 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001223 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001224 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001225 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.internal_errors);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001226 status = 500;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001227 goto return_prx_cond;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001228
Christopher Faulet93e02d82019-03-08 14:18:50 +01001229 return_bad_req:
Willy Tarreau4781b152021-04-06 13:53:36 +02001230 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req);
William Lallemand36119de2021-03-08 15:26:48 +01001231 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001232 _HA_ATOMIC_INC(&sess->listener->counters->failed_req);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001233 status = 400;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001234 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001235
Christopher Fauletb8a53712019-12-16 11:29:38 +01001236 return_prx_cond:
Christopher Faulet9768c262018-10-22 09:34:31 +02001237 if (txn->status > 0) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001238 /* Note: we don't send any error if some data were already sent */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001239 http_reply_and_close(s, txn->status, NULL);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001240 } else {
Christopher Faulet93e02d82019-03-08 14:18:50 +01001241 txn->status = status;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001242 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001243 }
Christopher Fauletb8a53712019-12-16 11:29:38 +01001244 if (!(s->flags & SF_ERR_MASK))
1245 s->flags |= SF_ERR_PRXCOND;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001246 if (!(s->flags & SF_FINST_MASK))
1247 s->flags |= ((txn->rsp.msg_state < HTTP_MSG_ERROR) ? SF_FINST_H : SF_FINST_D);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001248 DBG_TRACE_DEVEL("leaving on error ",
1249 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001250 return 0;
1251}
1252
Olivier Houcharda254a372019-04-05 15:30:12 +02001253/* Reset the stream and the backend stream_interface to a situation suitable for attemption connection */
1254/* Returns 0 if we can attempt to retry, -1 otherwise */
1255static __inline int do_l7_retry(struct stream *s, struct stream_interface *si)
1256{
Christopher Faulet5bf85852021-05-21 13:46:14 +02001257 struct channel *req, *res;
1258 int co_data;
Olivier Houcharda254a372019-04-05 15:30:12 +02001259
1260 si->conn_retries--;
1261 if (si->conn_retries < 0)
Christopher Faulet043cdb22021-05-26 10:31:06 +02001262 return -1;
Christopher Faulet5b82cc52020-10-12 15:18:50 +02001263
Christopher Faulete763c8c2021-05-05 18:23:59 +02001264 if (objt_server(s->target)) {
1265 if (s->flags & SF_CURR_SESS) {
1266 s->flags &= ~SF_CURR_SESS;
1267 _HA_ATOMIC_DEC(&__objt_server(s->target)->cur_sess);
1268 }
Willy Tarreau4781b152021-04-06 13:53:36 +02001269 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.retries);
Christopher Faulete763c8c2021-05-05 18:23:59 +02001270 }
Willy Tarreau4781b152021-04-06 13:53:36 +02001271 _HA_ATOMIC_INC(&s->be->be_counters.retries);
Willy Tarreau223995e2019-05-04 10:38:31 +02001272
Christopher Faulet5bf85852021-05-21 13:46:14 +02001273 req = &s->req;
1274 res = &s->res;
Olivier Houcharda254a372019-04-05 15:30:12 +02001275 /* Remove any write error from the request, and read error from the response */
1276 req->flags &= ~(CF_WRITE_ERROR | CF_WRITE_TIMEOUT | CF_SHUTW | CF_SHUTW_NOW);
1277 res->flags &= ~(CF_READ_ERROR | CF_READ_TIMEOUT | CF_SHUTR | CF_EOI | CF_READ_NULL | CF_SHUTR_NOW);
1278 res->analysers = 0;
1279 si->flags &= ~(SI_FL_ERR | SI_FL_EXP | SI_FL_RXBLK_SHUT);
Christopher Faulet30aa0da2021-05-05 21:05:09 +02001280 si->err_type = SI_ET_NONE;
1281 s->flags &= ~(SF_ERR_MASK | SF_FINST_MASK);
Olivier Houchard4bd58672019-07-12 16:16:59 +02001282 stream_choose_redispatch(s);
Olivier Houcharda254a372019-04-05 15:30:12 +02001283 si->exp = TICK_ETERNITY;
1284 res->rex = TICK_ETERNITY;
1285 res->to_forward = 0;
1286 res->analyse_exp = TICK_ETERNITY;
1287 res->total = 0;
Olivier Houcharda254a372019-04-05 15:30:12 +02001288 si_release_endpoint(&s->si[1]);
Olivier Houcharda254a372019-04-05 15:30:12 +02001289
Christopher Faulet5bf85852021-05-21 13:46:14 +02001290 b_free(&req->buf);
1291 /* Swap the L7 buffer with the channel buffer */
1292 /* We know we stored the co_data as b_data, so get it there */
1293 co_data = b_data(&si->l7_buffer);
1294 b_set_data(&si->l7_buffer, b_size(&si->l7_buffer));
1295 b_xfer(&req->buf, &si->l7_buffer, b_data(&si->l7_buffer));
1296 co_set_data(req, co_data);
Christopher Faulet5b82cc52020-10-12 15:18:50 +02001297
Ilya Shipitsinacf84592021-02-06 22:29:08 +05001298 DBG_TRACE_DEVEL("perform a L7 retry", STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, s->txn);
Christopher Faulet5bf85852021-05-21 13:46:14 +02001299
Olivier Houcharda254a372019-04-05 15:30:12 +02001300 b_reset(&res->buf);
1301 co_set_data(res, 0);
1302 return 0;
1303}
1304
Christopher Faulete0768eb2018-10-03 16:38:02 +02001305/* This stream analyser waits for a complete HTTP response. It returns 1 if the
1306 * processing can continue on next analysers, or zero if it either needs more
1307 * data or wants to immediately abort the response (eg: timeout, error, ...). It
1308 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->res.analysers
1309 * when it has nothing left to do, and may remove any analyser when it wants to
1310 * abort.
1311 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001312int http_wait_for_response(struct stream *s, struct channel *rep, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001313{
Christopher Faulet9768c262018-10-22 09:34:31 +02001314 /*
1315 * We will analyze a complete HTTP response to check the its syntax.
1316 *
1317 * Once the start line and all headers are received, we may perform a
1318 * capture of the error (if any), and we will set a few fields. We also
1319 * logging and finally headers capture.
1320 */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001321 struct session *sess = s->sess;
1322 struct http_txn *txn = s->txn;
1323 struct http_msg *msg = &txn->rsp;
Christopher Faulet9768c262018-10-22 09:34:31 +02001324 struct htx *htx;
Olivier Houcharda254a372019-04-05 15:30:12 +02001325 struct stream_interface *si_b = &s->si[1];
Christopher Faulet61608322018-11-23 16:23:45 +01001326 struct connection *srv_conn;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001327 struct htx_sl *sl;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001328 int n;
1329
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001330 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001331
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001332 htx = htxbuf(&rep->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001333
Willy Tarreau4236f032019-03-05 10:43:32 +01001334 /* Parsing errors are caught here */
1335 if (htx->flags & HTX_FL_PARSING_ERROR)
1336 goto return_bad_res;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001337 if (htx->flags & HTX_FL_PROCESSING_ERROR)
1338 goto return_int_err;
Willy Tarreau4236f032019-03-05 10:43:32 +01001339
Christopher Faulete0768eb2018-10-03 16:38:02 +02001340 /*
1341 * Now we quickly check if we have found a full valid response.
1342 * If not so, we check the FD and buffer states before leaving.
1343 * A full response is indicated by the fact that we have seen
1344 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
1345 * responses are checked first.
1346 *
1347 * Depending on whether the client is still there or not, we
1348 * may send an error response back or not. Note that normally
1349 * we should only check for HTTP status there, and check I/O
1350 * errors somewhere else.
1351 */
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001352 next_one:
Christopher Faulet29f17582019-05-23 11:03:26 +02001353 if (unlikely(htx_is_empty(htx) || htx->first == -1)) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001354 /* 1: have we encountered a read error ? */
1355 if (rep->flags & CF_READ_ERROR) {
Olivier Houchard865d8392019-05-03 22:46:27 +02001356 struct connection *conn = NULL;
1357
Olivier Houchard865d8392019-05-03 22:46:27 +02001358 if (objt_cs(s->si[1].end))
1359 conn = objt_cs(s->si[1].end)->conn;
1360
Christopher Fauletb1875342021-05-26 12:15:37 +02001361 /* Perform a L7 retry because server refuses the early data. */
1362 if ((si_b->flags & SI_FL_L7_RETRY) &&
1363 (s->be->retry_type & PR_RE_EARLY_ERROR) &&
1364 conn && conn->err_code == CO_ER_SSL_EARLY_FAILED &&
1365 do_l7_retry(s, si_b) == 0) {
1366 DBG_TRACE_DEVEL("leaving on L7 retry",
1367 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
1368 return 0;
1369 }
1370
Olivier Houchard6db16992019-05-17 15:40:49 +02001371 if (txn->flags & TX_NOT_FIRST)
1372 goto abort_keep_alive;
1373
Willy Tarreau4781b152021-04-06 13:53:36 +02001374 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001375 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001376 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001377 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001378 }
1379
Christopher Fauletb1875342021-05-26 12:15:37 +02001380 /* if the server refused the early data, just send a 425 */
1381 if (conn && conn->err_code == CO_ER_SSL_EARLY_FAILED)
Olivier Houchard865d8392019-05-03 22:46:27 +02001382 txn->status = 425;
Christopher Fauletb1875342021-05-26 12:15:37 +02001383 else {
1384 txn->status = 502;
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001385 stream_inc_http_fail_ctr(s);
Christopher Fauletb1875342021-05-26 12:15:37 +02001386 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001387
1388 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001389 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001390
1391 if (!(s->flags & SF_ERR_MASK))
1392 s->flags |= SF_ERR_SRVCL;
1393 if (!(s->flags & SF_FINST_MASK))
1394 s->flags |= SF_FINST_H;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001395 DBG_TRACE_DEVEL("leaving on error",
1396 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001397 return 0;
1398 }
1399
Christopher Faulet9768c262018-10-22 09:34:31 +02001400 /* 2: read timeout : return a 504 to the client. */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001401 else if (rep->flags & CF_READ_TIMEOUT) {
Olivier Houcharda254a372019-04-05 15:30:12 +02001402 if ((si_b->flags & SI_FL_L7_RETRY) &&
1403 (s->be->retry_type & PR_RE_TIMEOUT)) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001404 if (co_data(rep) || do_l7_retry(s, si_b) == 0) {
1405 DBG_TRACE_DEVEL("leaving on L7 retry",
1406 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharda254a372019-04-05 15:30:12 +02001407 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001408 }
Olivier Houcharda254a372019-04-05 15:30:12 +02001409 }
Willy Tarreau4781b152021-04-06 13:53:36 +02001410 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001411 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001412 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001413 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001414 }
1415
Christopher Faulete0768eb2018-10-03 16:38:02 +02001416 txn->status = 504;
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001417 stream_inc_http_fail_ctr(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001418 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001419 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001420
1421 if (!(s->flags & SF_ERR_MASK))
1422 s->flags |= SF_ERR_SRVTO;
1423 if (!(s->flags & SF_FINST_MASK))
1424 s->flags |= SF_FINST_H;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001425 DBG_TRACE_DEVEL("leaving on error",
1426 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001427 return 0;
1428 }
1429
Christopher Faulet9768c262018-10-22 09:34:31 +02001430 /* 3: client abort with an abortonclose */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001431 else if ((rep->flags & CF_SHUTR) && ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001432 _HA_ATOMIC_INC(&sess->fe->fe_counters.cli_aborts);
1433 _HA_ATOMIC_INC(&s->be->be_counters.cli_aborts);
William Lallemand36119de2021-03-08 15:26:48 +01001434 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001435 _HA_ATOMIC_INC(&sess->listener->counters->cli_aborts);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001436 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001437 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.cli_aborts);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001438
Christopher Faulete0768eb2018-10-03 16:38:02 +02001439 txn->status = 400;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001440 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001441
1442 if (!(s->flags & SF_ERR_MASK))
1443 s->flags |= SF_ERR_CLICL;
1444 if (!(s->flags & SF_FINST_MASK))
1445 s->flags |= SF_FINST_H;
1446
1447 /* process_stream() will take care of the error */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001448 DBG_TRACE_DEVEL("leaving on error",
1449 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001450 return 0;
1451 }
1452
Christopher Faulet9768c262018-10-22 09:34:31 +02001453 /* 4: close from server, capture the response if the server has started to respond */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001454 else if (rep->flags & CF_SHUTR) {
Olivier Houcharda254a372019-04-05 15:30:12 +02001455 if ((si_b->flags & SI_FL_L7_RETRY) &&
1456 (s->be->retry_type & PR_RE_DISCONNECTED)) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001457 if (co_data(rep) || do_l7_retry(s, si_b) == 0) {
1458 DBG_TRACE_DEVEL("leaving on L7 retry",
1459 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharda254a372019-04-05 15:30:12 +02001460 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001461 }
Olivier Houcharda254a372019-04-05 15:30:12 +02001462 }
1463
Olivier Houchard6db16992019-05-17 15:40:49 +02001464 if (txn->flags & TX_NOT_FIRST)
1465 goto abort_keep_alive;
1466
Willy Tarreau4781b152021-04-06 13:53:36 +02001467 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001468 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001469 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001470 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001471 }
1472
Christopher Faulete0768eb2018-10-03 16:38:02 +02001473 txn->status = 502;
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001474 stream_inc_http_fail_ctr(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001475 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001476 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001477
1478 if (!(s->flags & SF_ERR_MASK))
1479 s->flags |= SF_ERR_SRVCL;
1480 if (!(s->flags & SF_FINST_MASK))
1481 s->flags |= SF_FINST_H;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001482 DBG_TRACE_DEVEL("leaving on error",
1483 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001484 return 0;
1485 }
1486
Christopher Faulet9768c262018-10-22 09:34:31 +02001487 /* 5: write error to client (we don't send any message then) */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001488 else if (rep->flags & CF_WRITE_ERROR) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001489 if (txn->flags & TX_NOT_FIRST)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001490 goto abort_keep_alive;
1491
Willy Tarreau4781b152021-04-06 13:53:36 +02001492 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001493 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001494 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001495 rep->analysers &= AN_RES_FLT_END;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001496
1497 if (!(s->flags & SF_ERR_MASK))
1498 s->flags |= SF_ERR_CLICL;
1499 if (!(s->flags & SF_FINST_MASK))
1500 s->flags |= SF_FINST_H;
1501
1502 /* process_stream() will take care of the error */
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
1508 channel_dont_close(rep);
1509 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001510 DBG_TRACE_DEVEL("waiting for more data",
1511 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001512 return 0;
1513 }
1514
1515 /* More interesting part now : we know that we have a complete
1516 * response which at least looks like HTTP. We have an indicator
1517 * of each header's length, so we can parse them quickly.
1518 */
Christopher Faulet29f17582019-05-23 11:03:26 +02001519 BUG_ON(htx_get_first_type(htx) != HTX_BLK_RES_SL);
Christopher Faulet297fbb42019-05-13 14:41:27 +02001520 sl = http_get_stline(htx);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001521
Christopher Faulet0f66d552021-05-26 13:14:39 +02001522 /* Perform a L7 retry because of the status code */
1523 if ((si_b->flags & SI_FL_L7_RETRY) &&
1524 l7_status_match(s->be, sl->info.res.status) &&
1525 do_l7_retry(s, si_b) == 0) {
1526 DBG_TRACE_DEVEL("leaving on L7 retry", STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
1527 return 0;
1528 }
1529
1530 /* Now, L7 buffer is useless, it can be released */
1531 b_free(&s->si[1].l7_buffer);
1532
1533 msg->msg_state = HTTP_MSG_BODY;
1534
1535
Christopher Faulet9768c262018-10-22 09:34:31 +02001536 /* 0: we might have to print this header in debug mode */
1537 if (unlikely((global.mode & MODE_DEBUG) &&
1538 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
1539 int32_t pos;
1540
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001541 http_debug_stline("srvrep", s, sl);
Christopher Faulet9768c262018-10-22 09:34:31 +02001542
Christopher Fauleta3f15502019-05-13 15:27:23 +02001543 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001544 struct htx_blk *blk = htx_get_blk(htx, pos);
1545 enum htx_blk_type type = htx_get_blk_type(blk);
1546
1547 if (type == HTX_BLK_EOH)
1548 break;
1549 if (type != HTX_BLK_HDR)
1550 continue;
1551
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001552 http_debug_hdr("srvhdr", s,
1553 htx_get_blk_name(htx, blk),
1554 htx_get_blk_value(htx, blk));
Christopher Faulet9768c262018-10-22 09:34:31 +02001555 }
1556 }
1557
Christopher Faulet03599112018-11-27 11:21:21 +01001558 /* 1: get the status code and the version. Also set HTTP flags */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001559 txn->status = sl->info.res.status;
Christopher Faulet03599112018-11-27 11:21:21 +01001560 if (sl->flags & HTX_SL_F_VER_11)
Christopher Faulet9768c262018-10-22 09:34:31 +02001561 msg->flags |= HTTP_MSGF_VER_11;
Christopher Faulet03599112018-11-27 11:21:21 +01001562 if (sl->flags & HTX_SL_F_XFER_LEN) {
1563 msg->flags |= HTTP_MSGF_XFER_LEN;
Christopher Faulet2a408542020-11-20 14:22:37 +01001564 if (sl->flags & HTX_SL_F_CLEN)
1565 msg->flags |= HTTP_MSGF_CNT_LEN;
1566 else if (sl->flags & HTX_SL_F_CHNK)
1567 msg->flags |= HTTP_MSGF_TE_CHNK;
Christopher Faulet03599112018-11-27 11:21:21 +01001568 }
Christopher Faulet2a408542020-11-20 14:22:37 +01001569 if (sl->flags & HTX_SL_F_BODYLESS)
1570 msg->flags |= HTTP_MSGF_BODYLESS;
Christopher Faulet576c3582021-01-08 15:53:01 +01001571 if (sl->flags & HTX_SL_F_CONN_UPG)
1572 msg->flags |= HTTP_MSGF_CONN_UPG;
Christopher Faulet9768c262018-10-22 09:34:31 +02001573
1574 n = txn->status / 100;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001575 if (n < 1 || n > 5)
1576 n = 0;
Christopher Faulet9768c262018-10-22 09:34:31 +02001577
Christopher Faulete0768eb2018-10-03 16:38:02 +02001578 /* when the client triggers a 4xx from the server, it's most often due
1579 * to a missing object or permission. These events should be tracked
1580 * because if they happen often, it may indicate a brute force or a
1581 * vulnerability scan.
1582 */
1583 if (n == 4)
1584 stream_inc_http_err_ctr(s);
1585
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001586 if (n == 5 && txn->status != 501 && txn->status != 505)
1587 stream_inc_http_fail_ctr(s);
1588
Marcin Deranek3c27dda2020-05-15 18:32:51 +02001589 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001590 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.p.http.rsp[n]);
1591 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.p.http.cum_req);
Marcin Deranek3c27dda2020-05-15 18:32:51 +02001592 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001593
Christopher Faulete0768eb2018-10-03 16:38:02 +02001594 /* Adjust server's health based on status code. Note: status codes 501
1595 * and 505 are triggered on demand by client request, so we must not
1596 * count them as server failures.
1597 */
1598 if (objt_server(s->target)) {
1599 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001600 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_OK);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001601 else
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001602 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_STS);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001603 }
1604
1605 /*
1606 * We may be facing a 100-continue response, or any other informational
1607 * 1xx response which is non-final, in which case this is not the right
1608 * response, and we're waiting for the next one. Let's allow this response
1609 * to go to the client and wait for the next one. There's an exception for
1610 * 101 which is used later in the code to switch protocols.
1611 */
1612 if (txn->status < 200 &&
1613 (txn->status == 100 || txn->status >= 102)) {
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001614 FLT_STRM_CB(s, flt_http_reset(s, msg));
Christopher Faulet421e7692019-06-13 11:16:45 +02001615 htx->first = channel_htx_fwd_headers(rep, htx);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001616 msg->msg_state = HTTP_MSG_RPBEFORE;
Christopher Faulet3499f622019-09-03 15:23:54 +02001617 msg->flags = 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001618 txn->status = 0;
1619 s->logs.t_data = -1; /* was not a response yet */
Christopher Faulet7d518452020-08-31 11:07:07 +02001620 rep->flags |= CF_SEND_DONTWAIT; /* Send ASAP informational messages */
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001621 goto next_one;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001622 }
1623
Christopher Faulet6e6c7b12021-01-08 16:02:05 +01001624 /* A 101-switching-protocols must contains a Connection header with the
1625 * "upgrade" option and the request too. It means both are agree to
1626 * upgrade. It is not so strict because there is no test on the Upgrade
1627 * header content. But it is probably stronger enough for now.
1628 */
1629 if (txn->status == 101 &&
1630 (!(txn->req.flags & HTTP_MSGF_CONN_UPG) || !(txn->rsp.flags & HTTP_MSGF_CONN_UPG)))
1631 goto return_bad_res;
1632
Christopher Faulete0768eb2018-10-03 16:38:02 +02001633 /*
1634 * 2: check for cacheability.
1635 */
1636
1637 switch (txn->status) {
1638 case 200:
1639 case 203:
1640 case 204:
1641 case 206:
1642 case 300:
1643 case 301:
1644 case 404:
1645 case 405:
1646 case 410:
1647 case 414:
1648 case 501:
1649 break;
1650 default:
1651 /* RFC7231#6.1:
1652 * Responses with status codes that are defined as
1653 * cacheable by default (e.g., 200, 203, 204, 206,
1654 * 300, 301, 404, 405, 410, 414, and 501 in this
1655 * specification) can be reused by a cache with
1656 * heuristic expiration unless otherwise indicated
1657 * by the method definition or explicit cache
1658 * controls [RFC7234]; all other status codes are
1659 * not cacheable by default.
1660 */
1661 txn->flags &= ~(TX_CACHEABLE | TX_CACHE_COOK);
1662 break;
1663 }
1664
1665 /*
1666 * 3: we may need to capture headers
1667 */
1668 s->logs.logwait &= ~LW_RESP;
1669 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->res_cap))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001670 http_capture_headers(htx, s->res_cap, sess->fe->rsp_cap);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001671
Christopher Faulet9768c262018-10-22 09:34:31 +02001672 /* Skip parsing if no content length is possible. */
Christopher Fauletc75668e2020-12-07 18:10:32 +01001673 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status >= 200 && txn->status < 300) ||
Christopher Faulete0768eb2018-10-03 16:38:02 +02001674 txn->status == 101)) {
1675 /* Either we've established an explicit tunnel, or we're
1676 * switching the protocol. In both cases, we're very unlikely
1677 * to understand the next protocols. We have to switch to tunnel
1678 * mode, so that we transfer the request and responses then let
1679 * this protocol pass unmodified. When we later implement specific
1680 * parsers for such protocols, we'll want to check the Upgrade
1681 * header which contains information about that protocol for
1682 * responses with status 101 (eg: see RFC2817 about TLS).
1683 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02001684 txn->flags |= TX_CON_WANT_TUN;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001685 }
1686
Christopher Faulet61608322018-11-23 16:23:45 +01001687 /* check for NTML authentication headers in 401 (WWW-Authenticate) and
1688 * 407 (Proxy-Authenticate) responses and set the connection to private
1689 */
1690 srv_conn = cs_conn(objt_cs(s->si[1].end));
1691 if (srv_conn) {
1692 struct ist hdr;
1693 struct http_hdr_ctx ctx;
1694
1695 if (txn->status == 401)
1696 hdr = ist("WWW-Authenticate");
1697 else if (txn->status == 407)
1698 hdr = ist("Proxy-Authenticate");
1699 else
1700 goto end;
1701
1702 ctx.blk = NULL;
1703 while (http_find_header(htx, hdr, &ctx, 0)) {
Willy Tarreauf1dcced2020-05-07 19:27:02 +02001704 /* If www-authenticate contains "Negotiate", "Nego2", or "NTLM",
1705 * possibly followed by blanks and a base64 string, the connection
1706 * is private. Since it's a mess to deal with, we only check for
1707 * values starting with "NTLM" or "Nego". Note that often multiple
1708 * headers are sent by the server there.
1709 */
1710 if ((ctx.value.len >= 4 && strncasecmp(ctx.value.ptr, "Nego", 4) == 0) ||
Willy Tarreau49a1d282020-05-07 19:10:15 +02001711 (ctx.value.len >= 4 && strncasecmp(ctx.value.ptr, "NTLM", 4) == 0)) {
Olivier Houchard250031e2019-05-29 15:01:50 +02001712 sess->flags |= SESS_FL_PREFER_LAST;
Christopher Faulet08016ab2020-07-01 16:10:06 +02001713 conn_set_owner(srv_conn, sess, NULL);
Christopher Faulet21ddc742020-07-01 15:26:14 +02001714 conn_set_private(srv_conn);
Ilya Shipitsin6b79f382020-07-23 00:32:55 +05001715 /* If it fail now, the same will be done in mux->detach() callback */
Christopher Faulet08016ab2020-07-01 16:10:06 +02001716 session_add_conn(srv_conn->owner, srv_conn, srv_conn->target);
Willy Tarreauf1dcced2020-05-07 19:27:02 +02001717 break;
Olivier Houchard250031e2019-05-29 15:01:50 +02001718 }
Christopher Faulet61608322018-11-23 16:23:45 +01001719 }
1720 }
1721
1722 end:
Christopher Faulete0768eb2018-10-03 16:38:02 +02001723 /* we want to have the response time before we start processing it */
1724 s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now);
1725
1726 /* end of job, return OK */
1727 rep->analysers &= ~an_bit;
1728 rep->analyse_exp = TICK_ETERNITY;
1729 channel_auto_close(rep);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001730 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001731 return 1;
1732
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001733 return_int_err:
Willy Tarreau4781b152021-04-06 13:53:36 +02001734 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
1735 _HA_ATOMIC_INC(&s->be->be_counters.internal_errors);
William Lallemand36119de2021-03-08 15:26:48 +01001736 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001737 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Fauletb8a53712019-12-16 11:29:38 +01001738 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001739 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.internal_errors);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001740 txn->status = 500;
1741 if (!(s->flags & SF_ERR_MASK))
1742 s->flags |= SF_ERR_INTERNAL;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001743 goto return_prx_cond;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001744
1745 return_bad_res:
Willy Tarreau4781b152021-04-06 13:53:36 +02001746 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Faulet47365272018-10-31 17:40:50 +01001747 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001748 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001749 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Christopher Faulet47365272018-10-31 17:40:50 +01001750 }
Olivier Houcharde3249a92019-05-03 23:01:47 +02001751 if ((s->be->retry_type & PR_RE_JUNK_REQUEST) &&
Olivier Houchardad26d8d2019-05-10 17:48:28 +02001752 (si_b->flags & SI_FL_L7_RETRY) &&
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001753 do_l7_retry(s, si_b) == 0) {
1754 DBG_TRACE_DEVEL("leaving on L7 retry",
1755 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharde3249a92019-05-03 23:01:47 +02001756 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001757 }
Christopher Faulet47365272018-10-31 17:40:50 +01001758 txn->status = 502;
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001759 stream_inc_http_fail_ctr(s);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001760 /* fall through */
1761
Christopher Fauletb8a53712019-12-16 11:29:38 +01001762 return_prx_cond:
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001763 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet47365272018-10-31 17:40:50 +01001764
1765 if (!(s->flags & SF_ERR_MASK))
1766 s->flags |= SF_ERR_PRXCOND;
1767 if (!(s->flags & SF_FINST_MASK))
1768 s->flags |= SF_FINST_H;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001769
1770 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001771 DBG_TRACE_DEVEL("leaving on error",
1772 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulet47365272018-10-31 17:40:50 +01001773 return 0;
1774
Christopher Faulete0768eb2018-10-03 16:38:02 +02001775 abort_keep_alive:
1776 /* A keep-alive request to the server failed on a network error.
1777 * The client is required to retry. We need to close without returning
1778 * any other information so that the client retries.
1779 */
1780 txn->status = 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001781 s->logs.logwait = 0;
1782 s->logs.level = 0;
1783 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001784 http_reply_and_close(s, txn->status, NULL);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001785 DBG_TRACE_DEVEL("leaving by closing K/A connection",
1786 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001787 return 0;
1788}
1789
1790/* This function performs all the processing enabled for the current response.
1791 * It normally returns 1 unless it wants to break. It relies on buffers flags,
1792 * and updates s->res.analysers. It might make sense to explode it into several
1793 * other functions. It works like process_request (see indications above).
1794 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001795int http_process_res_common(struct stream *s, struct channel *rep, int an_bit, struct proxy *px)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001796{
1797 struct session *sess = s->sess;
1798 struct http_txn *txn = s->txn;
1799 struct http_msg *msg = &txn->rsp;
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001800 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001801 struct proxy *cur_proxy;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001802 enum rule_result ret = HTTP_RULE_RES_CONT;
1803
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001804 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
1805 return 0;
Christopher Faulet9768c262018-10-22 09:34:31 +02001806
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001807 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001808
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001809 htx = htxbuf(&rep->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001810
1811 /* The stats applet needs to adjust the Connection header but we don't
1812 * apply any filter there.
1813 */
1814 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
1815 rep->analysers &= ~an_bit;
1816 rep->analyse_exp = TICK_ETERNITY;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001817 goto end;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001818 }
1819
1820 /*
1821 * We will have to evaluate the filters.
1822 * As opposed to version 1.2, now they will be evaluated in the
1823 * filters order and not in the header order. This means that
1824 * each filter has to be validated among all headers.
1825 *
1826 * Filters are tried with ->be first, then with ->fe if it is
1827 * different from ->be.
1828 *
1829 * Maybe we are in resume condiion. In this case I choose the
1830 * "struct proxy" which contains the rule list matching the resume
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05001831 * pointer. If none of these "struct proxy" match, I initialise
Christopher Faulete0768eb2018-10-03 16:38:02 +02001832 * the process with the first one.
1833 *
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05001834 * In fact, I check only correspondence between the current list
Christopher Faulete0768eb2018-10-03 16:38:02 +02001835 * pointer and the ->fe rule list. If it doesn't match, I initialize
1836 * the loop with the ->be.
1837 */
1838 if (s->current_rule_list == &sess->fe->http_res_rules)
1839 cur_proxy = sess->fe;
1840 else
1841 cur_proxy = s->be;
1842 while (1) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001843 /* evaluate http-response rules */
1844 if (ret == HTTP_RULE_RES_CONT) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001845 ret = http_res_get_intercept_rule(cur_proxy, &cur_proxy->http_res_rules, s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001846
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001847 switch (ret) {
1848 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
1849 goto return_prx_yield;
1850
1851 case HTTP_RULE_RES_CONT:
1852 case HTTP_RULE_RES_STOP: /* nothing to do */
1853 break;
1854
1855 case HTTP_RULE_RES_DENY: /* deny or tarpit */
1856 goto deny;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001857
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001858 case HTTP_RULE_RES_ABRT: /* abort request, response already sent */
1859 goto return_prx_cond;
1860
1861 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
Christopher Fauletb8a53712019-12-16 11:29:38 +01001862 goto done;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001863
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001864 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
1865 goto return_bad_res;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001866
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001867 case HTTP_RULE_RES_ERROR: /* failed with a bad request */
1868 goto return_int_err;
1869 }
1870
1871 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001872
Christopher Faulete0768eb2018-10-03 16:38:02 +02001873 /* check whether we're already working on the frontend */
1874 if (cur_proxy == sess->fe)
1875 break;
1876 cur_proxy = sess->fe;
1877 }
1878
Christopher Faulete0768eb2018-10-03 16:38:02 +02001879 /* OK that's all we can do for 1xx responses */
1880 if (unlikely(txn->status < 200 && txn->status != 101))
Christopher Fauletf2824e62018-10-01 12:12:37 +02001881 goto end;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001882
1883 /*
1884 * Now check for a server cookie.
1885 */
1886 if (s->be->cookie_name || sess->fe->capture_name || (s->be->options & PR_O_CHK_CACHE))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001887 http_manage_server_side_cookies(s, rep);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001888
1889 /*
1890 * Check for cache-control or pragma headers if required.
1891 */
1892 if ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001893 http_check_response_for_cacheability(s, rep);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001894
1895 /*
1896 * Add server cookie in the response if needed
1897 */
1898 if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) &&
1899 !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) &&
1900 (!(s->flags & SF_DIRECT) ||
1901 ((s->be->cookie_maxidle || txn->cookie_last_date) &&
1902 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
1903 (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
1904 (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
1905 (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
1906 !(s->flags & SF_IGNORE_PRST)) {
1907 /* the server is known, it's not the one the client requested, or the
1908 * cookie's last seen date needs to be refreshed. We have to
1909 * insert a set-cookie here, except if we want to insert only on POST
1910 * requests and this one isn't. Note that servers which don't have cookies
1911 * (eg: some backup servers) will return a full cookie removal request.
1912 */
1913 if (!objt_server(s->target)->cookie) {
1914 chunk_printf(&trash,
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001915 "%s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
Christopher Faulete0768eb2018-10-03 16:38:02 +02001916 s->be->cookie_name);
1917 }
1918 else {
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001919 chunk_printf(&trash, "%s=%s", s->be->cookie_name, objt_server(s->target)->cookie);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001920
1921 if (s->be->cookie_maxidle || s->be->cookie_maxlife) {
1922 /* emit last_date, which is mandatory */
1923 trash.area[trash.data++] = COOKIE_DELIM_DATE;
1924 s30tob64((date.tv_sec+3) >> 2,
1925 trash.area + trash.data);
1926 trash.data += 5;
1927
1928 if (s->be->cookie_maxlife) {
1929 /* emit first_date, which is either the original one or
1930 * the current date.
1931 */
1932 trash.area[trash.data++] = COOKIE_DELIM_DATE;
1933 s30tob64(txn->cookie_first_date ?
1934 txn->cookie_first_date >> 2 :
1935 (date.tv_sec+3) >> 2,
1936 trash.area + trash.data);
1937 trash.data += 5;
1938 }
1939 }
1940 chunk_appendf(&trash, "; path=/");
1941 }
1942
1943 if (s->be->cookie_domain)
1944 chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain);
1945
1946 if (s->be->ck_opts & PR_CK_HTTPONLY)
1947 chunk_appendf(&trash, "; HttpOnly");
1948
1949 if (s->be->ck_opts & PR_CK_SECURE)
1950 chunk_appendf(&trash, "; Secure");
1951
Christopher Faulet2f533902020-01-21 11:06:48 +01001952 if (s->be->cookie_attrs)
1953 chunk_appendf(&trash, "; %s", s->be->cookie_attrs);
1954
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001955 if (unlikely(!http_add_header(htx, ist("Set-Cookie"), ist2(trash.area, trash.data))))
Christopher Fauletb8a53712019-12-16 11:29:38 +01001956 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001957
1958 txn->flags &= ~TX_SCK_MASK;
1959 if (__objt_server(s->target)->cookie && (s->flags & SF_DIRECT))
1960 /* the server did not change, only the date was updated */
1961 txn->flags |= TX_SCK_UPDATED;
1962 else
1963 txn->flags |= TX_SCK_INSERTED;
1964
1965 /* Here, we will tell an eventual cache on the client side that we don't
1966 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
1967 * Some caches understand the correct form: 'no-cache="set-cookie"', but
1968 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
1969 */
1970 if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
1971
1972 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
1973
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001974 if (unlikely(!http_add_header(htx, ist("Cache-control"), ist("private"))))
Christopher Fauletb8a53712019-12-16 11:29:38 +01001975 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001976 }
1977 }
1978
1979 /*
1980 * Check if result will be cacheable with a cookie.
1981 * We'll block the response if security checks have caught
1982 * nasty things such as a cacheable cookie.
1983 */
1984 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
1985 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
1986 (s->be->options & PR_O_CHK_CACHE)) {
1987 /* we're in presence of a cacheable response containing
1988 * a set-cookie header. We'll block it as requested by
1989 * the 'checkcache' option, and send an alert.
1990 */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001991 ha_alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
1992 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
1993 send_log(s->be, LOG_ALERT,
1994 "Blocking cacheable cookie in response from instance %s, server %s.\n",
1995 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
Christopher Fauletb8a53712019-12-16 11:29:38 +01001996 goto deny;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001997 }
1998
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001999 end:
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002000 /*
2001 * Evaluate after-response rules before forwarding the response. rules
2002 * from the backend are evaluated first, then one from the frontend if
2003 * it differs.
2004 */
2005 if (!http_eval_after_res_rules(s))
2006 goto return_int_err;
2007
Christopher Fauletc2ac5e42021-03-08 18:20:09 +01002008 /* Filter the response headers if there are filters attached to the
2009 * stream.
2010 */
2011 if (HAS_FILTERS(s))
2012 rep->analysers |= AN_RES_FLT_HTTP_HDRS;
2013
Christopher Faulete0768eb2018-10-03 16:38:02 +02002014 /* Always enter in the body analyzer */
2015 rep->analysers &= ~AN_RES_FLT_XFER_DATA;
2016 rep->analysers |= AN_RES_HTTP_XFER_BODY;
2017
2018 /* if the user wants to log as soon as possible, without counting
2019 * bytes from the server, then this is the right moment. We have
2020 * to temporarily assign bytes_out to log what we currently have.
2021 */
2022 if (!LIST_ISEMPTY(&sess->fe->logformat) && !(s->logs.logwait & LW_BYTES)) {
2023 s->logs.t_close = s->logs.t_data; /* to get a valid end date */
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002024 s->logs.bytes_out = htx->data;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002025 s->do_log(s);
2026 s->logs.bytes_out = 0;
2027 }
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002028
Christopher Fauletb8a53712019-12-16 11:29:38 +01002029 done:
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002030 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002031 rep->analysers &= ~an_bit;
2032 rep->analyse_exp = TICK_ETERNITY;
2033 return 1;
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002034
Christopher Fauletb8a53712019-12-16 11:29:38 +01002035 deny:
Willy Tarreau4781b152021-04-06 13:53:36 +02002036 _HA_ATOMIC_INC(&sess->fe->fe_counters.denied_resp);
2037 _HA_ATOMIC_INC(&s->be->be_counters.denied_resp);
William Lallemand36119de2021-03-08 15:26:48 +01002038 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02002039 _HA_ATOMIC_INC(&sess->listener->counters->denied_resp);
Christopher Fauleta08546b2019-12-16 16:07:34 +01002040 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02002041 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.denied_resp);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002042 goto return_prx_err;
2043
2044 return_int_err:
2045 txn->status = 500;
2046 if (!(s->flags & SF_ERR_MASK))
2047 s->flags |= SF_ERR_INTERNAL;
Willy Tarreau4781b152021-04-06 13:53:36 +02002048 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
2049 _HA_ATOMIC_INC(&s->be->be_counters.internal_errors);
Dragan Dosen84426cd2021-09-21 13:02:09 +02002050 if (sess->listener && sess->listener->counters)
2051 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Fauletcff0f732019-12-16 16:13:44 +01002052 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02002053 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.internal_errors);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002054 goto return_prx_err;
2055
2056 return_bad_res:
2057 txn->status = 502;
Willy Tarreau826f3ab2021-02-10 12:07:15 +01002058 stream_inc_http_fail_ctr(s);
Willy Tarreau4781b152021-04-06 13:53:36 +02002059 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Fauleta20a6532020-02-05 10:16:41 +01002060 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02002061 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Christopher Fauleta20a6532020-02-05 10:16:41 +01002062 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_RSP);
2063 }
Christopher Fauletb8a53712019-12-16 11:29:38 +01002064 /* fall through */
2065
2066 return_prx_err:
2067 http_reply_and_close(s, txn->status, http_error_message(s));
2068 /* fall through */
2069
2070 return_prx_cond:
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002071 s->logs.t_data = -1; /* was not a valid response */
2072 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002073
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002074 if (!(s->flags & SF_ERR_MASK))
2075 s->flags |= SF_ERR_PRXCOND;
2076 if (!(s->flags & SF_FINST_MASK))
2077 s->flags |= SF_FINST_H;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002078
Christopher Faulete58c0002020-03-02 16:21:01 +01002079 rep->analysers &= AN_RES_FLT_END;
2080 s->req.analysers &= AN_REQ_FLT_END;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002081 rep->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002082 DBG_TRACE_DEVEL("leaving on error",
2083 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002084 return 0;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002085
2086 return_prx_yield:
2087 channel_dont_close(rep);
2088 DBG_TRACE_DEVEL("waiting for more data",
2089 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
2090 return 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002091}
2092
2093/* This function is an analyser which forwards response body (including chunk
2094 * sizes if any). It is called as soon as we must forward, even if we forward
2095 * zero byte. The only situation where it must not be called is when we're in
2096 * tunnel mode and we want to forward till the close. It's used both to forward
2097 * remaining data and to resync after end of body. It expects the msg_state to
2098 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
2099 * read more data, or 1 once we can go on with next request or end the stream.
2100 *
2101 * It is capable of compressing response data both in content-length mode and
2102 * in chunked mode. The state machines follows different flows depending on
2103 * whether content-length and chunked modes are used, since there are no
2104 * trailers in content-length :
2105 *
2106 * chk-mode cl-mode
2107 * ,----- BODY -----.
2108 * / \
2109 * V size > 0 V chk-mode
2110 * .--> SIZE -------------> DATA -------------> CRLF
2111 * | | size == 0 | last byte |
2112 * | v final crlf v inspected |
2113 * | TRAILERS -----------> DONE |
2114 * | |
2115 * `----------------------------------------------'
2116 *
2117 * Compression only happens in the DATA state, and must be flushed in final
2118 * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding
2119 * is performed at once on final states for all bytes parsed, or when leaving
2120 * on missing data.
2121 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002122int http_response_forward_body(struct stream *s, struct channel *res, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +02002123{
2124 struct session *sess = s->sess;
2125 struct http_txn *txn = s->txn;
2126 struct http_msg *msg = &s->txn->rsp;
Christopher Faulet9768c262018-10-22 09:34:31 +02002127 struct htx *htx;
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002128 int ret;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002129
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002130 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002131
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002132 htx = htxbuf(&res->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002133
Christopher Fauletb9a92f32019-09-09 10:15:21 +02002134 if (htx->flags & HTX_FL_PARSING_ERROR)
2135 goto return_bad_res;
2136 if (htx->flags & HTX_FL_PROCESSING_ERROR)
2137 goto return_int_err;
2138
Christopher Faulete0768eb2018-10-03 16:38:02 +02002139 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Christopher Fauletf2824e62018-10-01 12:12:37 +02002140 ((res->flags & CF_SHUTW) && (res->to_forward || co_data(res)))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02002141 /* Output closed while we were sending data. We must abort and
2142 * wake the other side up.
2143 */
Christopher Faulete0768eb2018-10-03 16:38:02 +02002144 msg->msg_state = HTTP_MSG_ERROR;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002145 http_end_response(s);
2146 http_end_request(s);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002147 DBG_TRACE_DEVEL("leaving on error",
2148 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002149 return 1;
2150 }
2151
Christopher Faulet9768c262018-10-22 09:34:31 +02002152 if (msg->msg_state == HTTP_MSG_BODY)
2153 msg->msg_state = HTTP_MSG_DATA;
2154
Christopher Faulete0768eb2018-10-03 16:38:02 +02002155 /* in most states, we should abort in case of early close */
2156 channel_auto_close(res);
2157
Christopher Faulete0768eb2018-10-03 16:38:02 +02002158 if (res->to_forward) {
Christopher Faulet66af0b22019-03-22 14:54:52 +01002159 if (res->to_forward == CHN_INFINITE_FORWARD) {
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002160 if (res->flags & CF_EOI)
2161 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet66af0b22019-03-22 14:54:52 +01002162 }
2163 else {
2164 /* We can't process the buffer's contents yet */
2165 res->flags |= CF_WAKE_WRITE;
2166 goto missing_data_or_waiting;
2167 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02002168 }
2169
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002170 if (msg->msg_state >= HTTP_MSG_ENDING)
2171 goto ending;
2172
Christopher Fauletc75668e2020-12-07 18:10:32 +01002173 if ((txn->meth == HTTP_METH_CONNECT && txn->status >= 200 && txn->status < 300) || txn->status == 101 ||
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002174 (!(msg->flags & HTTP_MSGF_XFER_LEN) && !HAS_RSP_DATA_FILTERS(s))) {
2175 msg->msg_state = HTTP_MSG_ENDING;
2176 goto ending;
2177 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002178
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002179 /* Forward input data. We get it by removing all outgoing data not
2180 * forwarded yet from HTX data size. If there are some data filters, we
2181 * let them decide the amount of data to forward.
Christopher Faulet9768c262018-10-22 09:34:31 +02002182 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002183 if (HAS_RSP_DATA_FILTERS(s)) {
2184 ret = flt_http_payload(s, msg, htx->data);
2185 if (ret < 0)
2186 goto return_bad_res;
Christopher Faulet421e7692019-06-13 11:16:45 +02002187 c_adv(res, ret);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002188 }
2189 else {
Christopher Faulet421e7692019-06-13 11:16:45 +02002190 c_adv(res, htx->data - co_data(res));
Christopher Faulet66af0b22019-03-22 14:54:52 +01002191 if (msg->flags & HTTP_MSGF_XFER_LEN)
2192 channel_htx_forward_forever(res, htx);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002193 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002194
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002195 if (htx->data != co_data(res))
2196 goto missing_data_or_waiting;
2197
2198 if (!(msg->flags & HTTP_MSGF_XFER_LEN) && res->flags & CF_SHUTR) {
2199 msg->msg_state = HTTP_MSG_ENDING;
2200 goto ending;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002201 }
2202
Christopher Faulet9768c262018-10-22 09:34:31 +02002203 /* Check if the end-of-message is reached and if so, switch the message
Christopher Fauletd20fdb02019-06-13 16:43:22 +02002204 * in HTTP_MSG_ENDING state. Then if all data was marked to be
2205 * forwarded, set the state to HTTP_MSG_DONE.
Christopher Faulet9768c262018-10-22 09:34:31 +02002206 */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002207 if (!(htx->flags & HTX_FL_EOM))
Christopher Faulet9768c262018-10-22 09:34:31 +02002208 goto missing_data_or_waiting;
2209
Christopher Fauletd20fdb02019-06-13 16:43:22 +02002210 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet9768c262018-10-22 09:34:31 +02002211
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002212 ending:
Christopher Faulet2151cdd2020-07-22 16:34:59 +02002213 res->flags &= ~CF_EXPECT_MORE; /* no more data are expected */
2214
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002215 /* other states, ENDING...TUNNEL */
2216 if (msg->msg_state >= HTTP_MSG_DONE)
2217 goto done;
Christopher Faulet9768c262018-10-22 09:34:31 +02002218
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002219 if (HAS_RSP_DATA_FILTERS(s)) {
2220 ret = flt_http_end(s, msg);
2221 if (ret <= 0) {
2222 if (!ret)
2223 goto missing_data_or_waiting;
2224 goto return_bad_res;
2225 }
2226 }
2227
Christopher Fauletc75668e2020-12-07 18:10:32 +01002228 if ((txn->meth == HTTP_METH_CONNECT && txn->status >= 200 && txn->status < 300) || txn->status == 101 ||
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002229 !(msg->flags & HTTP_MSGF_XFER_LEN)) {
2230 msg->msg_state = HTTP_MSG_TUNNEL;
2231 goto ending;
2232 }
2233 else {
2234 msg->msg_state = HTTP_MSG_DONE;
2235 res->to_forward = 0;
2236 }
2237
2238 done:
2239
2240 channel_dont_close(res);
2241
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002242 http_end_response(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002243 if (!(res->analysers & an_bit)) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002244 http_end_request(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002245 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
2246 if (res->flags & CF_SHUTW) {
2247 /* response errors are most likely due to the
2248 * client aborting the transfer. */
Christopher Faulet93e02d82019-03-08 14:18:50 +01002249 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002250 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02002251 goto return_bad_res;
2252 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002253 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002254 return 1;
2255 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002256 DBG_TRACE_DEVEL("waiting for the end of the HTTP txn",
2257 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002258 return 0;
2259
2260 missing_data_or_waiting:
2261 if (res->flags & CF_SHUTW)
Christopher Faulet93e02d82019-03-08 14:18:50 +01002262 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002263
2264 /* stop waiting for data if the input is closed before the end. If the
2265 * client side was already closed, it means that the client has aborted,
2266 * so we don't want to count this as a server abort. Otherwise it's a
2267 * server abort.
2268 */
Christopher Fauletd20fdb02019-06-13 16:43:22 +02002269 if (msg->msg_state < HTTP_MSG_ENDING && res->flags & CF_SHUTR) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02002270 if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))
Christopher Faulet93e02d82019-03-08 14:18:50 +01002271 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002272 /* If we have some pending data, we continue the processing */
Christopher Faulet93e02d82019-03-08 14:18:50 +01002273 if (htx_is_empty(htx))
2274 goto return_srv_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002275 }
2276
Christopher Faulete0768eb2018-10-03 16:38:02 +02002277 /* When TE: chunked is used, we need to get there again to parse
2278 * remaining chunks even if the server has closed, so we don't want to
Christopher Faulet9768c262018-10-22 09:34:31 +02002279 * set CF_DONTCLOSE. Similarly when there is a content-leng or if there
2280 * are filters registered on the stream, we don't want to forward a
2281 * close
Christopher Faulete0768eb2018-10-03 16:38:02 +02002282 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002283 if ((msg->flags & HTTP_MSGF_XFER_LEN) || HAS_RSP_DATA_FILTERS(s))
Christopher Faulete0768eb2018-10-03 16:38:02 +02002284 channel_dont_close(res);
2285
2286 /* We know that more data are expected, but we couldn't send more that
2287 * what we did. So we always set the CF_EXPECT_MORE flag so that the
2288 * system knows it must not set a PUSH on this first part. Interactive
2289 * modes are already handled by the stream sock layer. We must not do
2290 * this in content-length mode because it could present the MSG_MORE
2291 * flag with the last block of forwarded data, which would cause an
2292 * additional delay to be observed by the receiver.
2293 */
Christopher Faulet2151cdd2020-07-22 16:34:59 +02002294 if (HAS_RSP_DATA_FILTERS(s))
Christopher Faulete0768eb2018-10-03 16:38:02 +02002295 res->flags |= CF_EXPECT_MORE;
2296
2297 /* the stream handler will take care of timeouts and errors */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002298 DBG_TRACE_DEVEL("waiting for more data to forward",
2299 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002300 return 0;
2301
Christopher Faulet93e02d82019-03-08 14:18:50 +01002302 return_srv_abort:
Willy Tarreau4781b152021-04-06 13:53:36 +02002303 _HA_ATOMIC_INC(&sess->fe->fe_counters.srv_aborts);
2304 _HA_ATOMIC_INC(&s->be->be_counters.srv_aborts);
William Lallemand36119de2021-03-08 15:26:48 +01002305 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02002306 _HA_ATOMIC_INC(&sess->listener->counters->srv_aborts);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002307 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02002308 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.srv_aborts);
Willy Tarreau826f3ab2021-02-10 12:07:15 +01002309 stream_inc_http_fail_ctr(s);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002310 if (!(s->flags & SF_ERR_MASK))
2311 s->flags |= SF_ERR_SRVCL;
2312 goto return_error;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002313
Christopher Faulet93e02d82019-03-08 14:18:50 +01002314 return_cli_abort:
Willy Tarreau4781b152021-04-06 13:53:36 +02002315 _HA_ATOMIC_INC(&sess->fe->fe_counters.cli_aborts);
2316 _HA_ATOMIC_INC(&s->be->be_counters.cli_aborts);
William Lallemand36119de2021-03-08 15:26:48 +01002317 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02002318 _HA_ATOMIC_INC(&sess->listener->counters->cli_aborts);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002319 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02002320 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.cli_aborts);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002321 if (!(s->flags & SF_ERR_MASK))
2322 s->flags |= SF_ERR_CLICL;
2323 goto return_error;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002324
Christopher Fauletb9a92f32019-09-09 10:15:21 +02002325 return_int_err:
Willy Tarreau4781b152021-04-06 13:53:36 +02002326 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
2327 _HA_ATOMIC_INC(&s->be->be_counters.internal_errors);
William Lallemand36119de2021-03-08 15:26:48 +01002328 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02002329 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002330 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02002331 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.internal_errors);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02002332 if (!(s->flags & SF_ERR_MASK))
2333 s->flags |= SF_ERR_INTERNAL;
2334 goto return_error;
2335
Christopher Faulet93e02d82019-03-08 14:18:50 +01002336 return_bad_res:
Willy Tarreau4781b152021-04-06 13:53:36 +02002337 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002338 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02002339 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002340 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_RSP);
2341 }
Willy Tarreau826f3ab2021-02-10 12:07:15 +01002342 stream_inc_http_fail_ctr(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002343 if (!(s->flags & SF_ERR_MASK))
Christopher Faulet93e02d82019-03-08 14:18:50 +01002344 s->flags |= SF_ERR_SRVCL;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002345 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +02002346
Christopher Faulet93e02d82019-03-08 14:18:50 +01002347 return_error:
Christopher Faulete0768eb2018-10-03 16:38:02 +02002348 /* don't send any error message as we're in the body */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002349 http_reply_and_close(s, txn->status, NULL);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002350 if (!(s->flags & SF_FINST_MASK))
2351 s->flags |= SF_FINST_D;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002352 DBG_TRACE_DEVEL("leaving on error",
2353 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002354 return 0;
2355}
2356
Christopher Fauletf2824e62018-10-01 12:12:37 +02002357/* Perform an HTTP redirect based on the information in <rule>. The function
Christopher Faulet99daf282018-11-28 22:58:13 +01002358 * returns zero on success, or zero in case of a, irrecoverable error such
Christopher Fauletf2824e62018-10-01 12:12:37 +02002359 * as too large a request to build a valid response.
2360 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002361int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn)
Christopher Fauletf2824e62018-10-01 12:12:37 +02002362{
Christopher Faulet99daf282018-11-28 22:58:13 +01002363 struct channel *req = &s->req;
2364 struct channel *res = &s->res;
2365 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01002366 struct htx_sl *sl;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002367 struct buffer *chunk;
Christopher Faulet99daf282018-11-28 22:58:13 +01002368 struct ist status, reason, location;
2369 unsigned int flags;
Christopher Faulet08e66462019-05-23 16:44:59 +02002370 int close = 0; /* Try to keep the connection alive byt default */
Christopher Fauletf2824e62018-10-01 12:12:37 +02002371
2372 chunk = alloc_trash_chunk();
Christopher Fauletb8a53712019-12-16 11:29:38 +01002373 if (!chunk) {
2374 if (!(s->flags & SF_ERR_MASK))
2375 s->flags |= SF_ERR_RESOURCE;
Christopher Faulet99daf282018-11-28 22:58:13 +01002376 goto fail;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002377 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002378
Christopher Faulet99daf282018-11-28 22:58:13 +01002379 /*
2380 * Create the location
2381 */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002382 htx = htxbuf(&req->buf);
Christopher Fauletf2824e62018-10-01 12:12:37 +02002383 switch(rule->type) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002384 case REDIRECT_TYPE_SCHEME: {
2385 struct http_hdr_ctx ctx;
2386 struct ist path, host;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002387
Christopher Faulet99daf282018-11-28 22:58:13 +01002388 host = ist("");
2389 ctx.blk = NULL;
2390 if (http_find_header(htx, ist("Host"), &ctx, 0))
2391 host = ctx.value;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002392
Christopher Faulet297fbb42019-05-13 14:41:27 +02002393 sl = http_get_stline(htx);
Christopher Faulet99daf282018-11-28 22:58:13 +01002394 path = http_get_path(htx_sl_req_uri(sl));
2395 /* build message using path */
Tim Duesterhused526372020-03-05 17:56:33 +01002396 if (isttest(path)) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002397 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2398 int qs = 0;
2399 while (qs < path.len) {
2400 if (*(path.ptr + qs) == '?') {
2401 path.len = qs;
2402 break;
2403 }
2404 qs++;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002405 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002406 }
2407 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002408 else
2409 path = ist("/");
Christopher Fauletf2824e62018-10-01 12:12:37 +02002410
Christopher Faulet99daf282018-11-28 22:58:13 +01002411 if (rule->rdr_str) { /* this is an old "redirect" rule */
2412 /* add scheme */
2413 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2414 goto fail;
2415 }
2416 else {
2417 /* add scheme with executing log format */
2418 chunk->data += build_logline(s, chunk->area + chunk->data,
2419 chunk->size - chunk->data,
2420 &rule->rdr_fmt);
2421 }
2422 /* add "://" + host + path */
2423 if (!chunk_memcat(chunk, "://", 3) ||
2424 !chunk_memcat(chunk, host.ptr, host.len) ||
2425 !chunk_memcat(chunk, path.ptr, path.len))
2426 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002427
Christopher Faulet99daf282018-11-28 22:58:13 +01002428 /* append a slash at the end of the location if needed and missing */
2429 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
2430 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
2431 if (chunk->data + 1 >= chunk->size)
2432 goto fail;
2433 chunk->area[chunk->data++] = '/';
2434 }
2435 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002436 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002437
Christopher Faulet99daf282018-11-28 22:58:13 +01002438 case REDIRECT_TYPE_PREFIX: {
2439 struct ist path;
2440
Christopher Faulet297fbb42019-05-13 14:41:27 +02002441 sl = http_get_stline(htx);
Christopher Faulet99daf282018-11-28 22:58:13 +01002442 path = http_get_path(htx_sl_req_uri(sl));
2443 /* build message using path */
Tim Duesterhused526372020-03-05 17:56:33 +01002444 if (isttest(path)) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002445 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2446 int qs = 0;
2447 while (qs < path.len) {
2448 if (*(path.ptr + qs) == '?') {
2449 path.len = qs;
2450 break;
2451 }
2452 qs++;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002453 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002454 }
2455 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002456 else
2457 path = ist("/");
Christopher Fauletf2824e62018-10-01 12:12:37 +02002458
Christopher Faulet99daf282018-11-28 22:58:13 +01002459 if (rule->rdr_str) { /* this is an old "redirect" rule */
2460 /* add prefix. Note that if prefix == "/", we don't want to
2461 * add anything, otherwise it makes it hard for the user to
2462 * configure a self-redirection.
2463 */
2464 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
2465 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2466 goto fail;
2467 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002468 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002469 else {
2470 /* add prefix with executing log format */
2471 chunk->data += build_logline(s, chunk->area + chunk->data,
2472 chunk->size - chunk->data,
2473 &rule->rdr_fmt);
2474 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002475
Christopher Faulet99daf282018-11-28 22:58:13 +01002476 /* add path */
2477 if (!chunk_memcat(chunk, path.ptr, path.len))
2478 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002479
Christopher Faulet99daf282018-11-28 22:58:13 +01002480 /* append a slash at the end of the location if needed and missing */
2481 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
2482 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
2483 if (chunk->data + 1 >= chunk->size)
2484 goto fail;
2485 chunk->area[chunk->data++] = '/';
2486 }
2487 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002488 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002489 case REDIRECT_TYPE_LOCATION:
2490 default:
2491 if (rule->rdr_str) { /* this is an old "redirect" rule */
2492 /* add location */
2493 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2494 goto fail;
2495 }
2496 else {
2497 /* add location with executing log format */
2498 chunk->data += build_logline(s, chunk->area + chunk->data,
2499 chunk->size - chunk->data,
2500 &rule->rdr_fmt);
2501 }
2502 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002503 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002504 location = ist2(chunk->area, chunk->data);
2505
2506 /*
2507 * Create the 30x response
2508 */
2509 switch (rule->code) {
2510 case 308:
2511 status = ist("308");
2512 reason = ist("Permanent Redirect");
2513 break;
2514 case 307:
2515 status = ist("307");
2516 reason = ist("Temporary Redirect");
2517 break;
2518 case 303:
2519 status = ist("303");
2520 reason = ist("See Other");
2521 break;
2522 case 301:
2523 status = ist("301");
2524 reason = ist("Moved Permanently");
2525 break;
2526 case 302:
2527 default:
2528 status = ist("302");
2529 reason = ist("Found");
2530 break;
2531 }
2532
Christopher Faulet08e66462019-05-23 16:44:59 +02002533 if (!(txn->req.flags & HTTP_MSGF_BODYLESS) && txn->req.msg_state != HTTP_MSG_DONE)
2534 close = 1;
2535
Christopher Faulet99daf282018-11-28 22:58:13 +01002536 htx = htx_from_buf(&res->buf);
Kevin Zhu96b36392020-01-07 09:42:55 +01002537 /* Trim any possible response */
2538 channel_htx_truncate(&s->res, htx);
Christopher Faulet99daf282018-11-28 22:58:13 +01002539 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
2540 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), status, reason);
2541 if (!sl)
2542 goto fail;
2543 sl->info.res.status = rule->code;
2544 s->txn->status = rule->code;
2545
Christopher Faulet08e66462019-05-23 16:44:59 +02002546 if (close && !htx_add_header(htx, ist("Connection"), ist("close")))
2547 goto fail;
2548
2549 if (!htx_add_header(htx, ist("Content-length"), ist("0")) ||
Christopher Faulet99daf282018-11-28 22:58:13 +01002550 !htx_add_header(htx, ist("Location"), location))
2551 goto fail;
2552
2553 if (rule->code == 302 || rule->code == 303 || rule->code == 307) {
2554 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")))
2555 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002556 }
2557
2558 if (rule->cookie_len) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002559 if (!htx_add_header(htx, ist("Set-Cookie"), ist2(rule->cookie_str, rule->cookie_len)))
2560 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002561 }
2562
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002563 if (!htx_add_endof(htx, HTX_BLK_EOH))
Christopher Faulet99daf282018-11-28 22:58:13 +01002564 goto fail;
2565
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002566 htx->flags |= HTX_FL_EOM;
Kevin Zhu96b36392020-01-07 09:42:55 +01002567 htx_to_buf(htx, &res->buf);
Christopher Fauleta72a7e42020-01-28 09:28:11 +01002568 if (!http_forward_proxy_resp(s, 1))
2569 goto fail;
Christopher Faulet99daf282018-11-28 22:58:13 +01002570
Christopher Faulet60b33a52020-01-28 09:18:10 +01002571 if (rule->flags & REDIRECT_FLAG_FROM_REQ) {
2572 /* let's log the request time */
2573 s->logs.tv_request = now;
Christopher Faulet91322272021-10-04 14:16:46 +02002574 req->analysers &= AN_REQ_FLT_END;
Christopher Faulet99daf282018-11-28 22:58:13 +01002575
Christopher Faulet60b33a52020-01-28 09:18:10 +01002576 if (s->sess->fe == s->be) /* report it if the request was intercepted by the frontend */
Willy Tarreau4781b152021-04-06 13:53:36 +02002577 _HA_ATOMIC_INC(&s->sess->fe->fe_counters.intercepted_req);
Christopher Faulet60b33a52020-01-28 09:18:10 +01002578 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002579
2580 if (!(s->flags & SF_ERR_MASK))
2581 s->flags |= SF_ERR_LOCAL;
2582 if (!(s->flags & SF_FINST_MASK))
Christopher Faulet60b33a52020-01-28 09:18:10 +01002583 s->flags |= ((rule->flags & REDIRECT_FLAG_FROM_REQ) ? SF_FINST_R : SF_FINST_H);
Christopher Fauletf2824e62018-10-01 12:12:37 +02002584
Christopher Faulet99daf282018-11-28 22:58:13 +01002585 free_trash_chunk(chunk);
2586 return 1;
2587
2588 fail:
2589 /* If an error occurred, remove the incomplete HTTP response from the
2590 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01002591 channel_htx_truncate(res, htxbuf(&res->buf));
Christopher Fauletf2824e62018-10-01 12:12:37 +02002592 free_trash_chunk(chunk);
Christopher Faulet99daf282018-11-28 22:58:13 +01002593 return 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002594}
2595
Christopher Faulet92d34fe2019-12-17 09:20:34 +01002596/* Replace all headers matching the name <name>. The header value is replaced if
2597 * it matches the regex <re>. <str> is used for the replacement. If <full> is
2598 * set to 1, the full-line is matched and replaced. Otherwise, comma-separated
2599 * values are evaluated one by one. It returns 0 on success and -1 on error.
2600 */
2601int http_replace_hdrs(struct stream* s, struct htx *htx, struct ist name,
2602 const char *str, struct my_regex *re, int full)
Christopher Faulet72333522018-10-24 11:25:02 +02002603{
2604 struct http_hdr_ctx ctx;
2605 struct buffer *output = get_trash_chunk();
2606
Christopher Faulet72333522018-10-24 11:25:02 +02002607 ctx.blk = NULL;
Christopher Faulet92d34fe2019-12-17 09:20:34 +01002608 while (http_find_header(htx, name, &ctx, full)) {
Christopher Faulet72333522018-10-24 11:25:02 +02002609 if (!regex_exec_match2(re, ctx.value.ptr, ctx.value.len, MAX_MATCH, pmatch, 0))
2610 continue;
2611
2612 output->data = exp_replace(output->area, output->size, ctx.value.ptr, str, pmatch);
2613 if (output->data == -1)
2614 return -1;
2615 if (!http_replace_header_value(htx, &ctx, ist2(output->area, output->data)))
2616 return -1;
2617 }
2618 return 0;
2619}
2620
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002621/* This function executes one of the set-{method,path,query,uri} actions. It
2622 * takes the string from the variable 'replace' with length 'len', then modifies
2623 * the relevant part of the request line accordingly. Then it updates various
2624 * pointers to the next elements which were moved, and the total buffer length.
2625 * It finds the action to be performed in p[2], previously filled by function
2626 * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal
2627 * error, though this can be revisited when this code is finally exploited.
2628 *
2629 * 'action' can be '0' to replace method, '1' to replace path, '2' to replace
Christopher Faulet312294f2020-09-02 17:17:44 +02002630 * query string, 3 to replace uri or 4 to replace the path+query.
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002631 *
2632 * In query string case, the mark question '?' must be set at the start of the
2633 * string by the caller, event if the replacement query string is empty.
2634 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002635int http_req_replace_stline(int action, const char *replace, int len,
2636 struct proxy *px, struct stream *s)
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002637{
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002638 struct htx *htx = htxbuf(&s->req.buf);
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002639
2640 switch (action) {
2641 case 0: // method
2642 if (!http_replace_req_meth(htx, ist2(replace, len)))
2643 return -1;
2644 break;
2645
2646 case 1: // path
Christopher Fauletb8ce5052020-08-31 16:11:57 +02002647 if (!http_replace_req_path(htx, ist2(replace, len), 0))
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002648 return -1;
2649 break;
2650
2651 case 2: // query
2652 if (!http_replace_req_query(htx, ist2(replace, len)))
2653 return -1;
2654 break;
2655
2656 case 3: // uri
2657 if (!http_replace_req_uri(htx, ist2(replace, len)))
2658 return -1;
2659 break;
2660
Christopher Faulet312294f2020-09-02 17:17:44 +02002661 case 4: // path + query
2662 if (!http_replace_req_path(htx, ist2(replace, len), 1))
2663 return -1;
2664 break;
2665
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002666 default:
2667 return -1;
2668 }
2669 return 0;
2670}
2671
2672/* This function replace the HTTP status code and the associated message. The
Christopher Faulete00d06c2019-12-16 17:18:42 +01002673 * variable <status> contains the new status code. This function never fails. It
2674 * returns 0 in case of success, -1 in case of internal error.
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002675 */
Christopher Faulet96bff762019-12-17 13:46:18 +01002676int http_res_set_status(unsigned int status, struct ist reason, struct stream *s)
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002677{
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002678 struct htx *htx = htxbuf(&s->res.buf);
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002679 char *res;
2680
2681 chunk_reset(&trash);
2682 res = ultoa_o(status, trash.area, trash.size);
2683 trash.data = res - trash.area;
2684
2685 /* Do we have a custom reason format string? */
Tim Duesterhuse296d3e2020-03-05 17:56:31 +01002686 if (!isttest(reason)) {
Christopher Faulet96bff762019-12-17 13:46:18 +01002687 const char *str = http_get_reason(status);
Tim Duesterhusdcf753a2021-03-04 17:31:47 +01002688 reason = ist(str);
Christopher Faulet96bff762019-12-17 13:46:18 +01002689 }
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002690
Christopher Fauletbde2c4c2020-08-31 16:43:34 +02002691 if (!http_replace_res_status(htx, ist2(trash.area, trash.data), reason))
Christopher Faulete00d06c2019-12-16 17:18:42 +01002692 return -1;
2693 return 0;
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002694}
2695
Christopher Faulet3e964192018-10-24 11:39:23 +02002696/* Executes the http-request rules <rules> for stream <s>, proxy <px> and
2697 * transaction <txn>. Returns the verdict of the first rule that prevents
2698 * further processing of the request (auth, deny, ...), and defaults to
2699 * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or
2700 * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT
2701 * on txn->flags if it encounters a tarpit rule. If <deny_status> is not NULL
2702 * and a deny/tarpit rule is matched, it will be filled with this rule's deny
2703 * status.
2704 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002705static enum rule_result http_req_get_intercept_rule(struct proxy *px, struct list *rules,
Christopher Fauletb58f62b2020-01-13 16:40:13 +01002706 struct stream *s)
Christopher Faulet3e964192018-10-24 11:39:23 +02002707{
2708 struct session *sess = strm_sess(s);
2709 struct http_txn *txn = s->txn;
Christopher Faulet3e964192018-10-24 11:39:23 +02002710 struct act_rule *rule;
Christopher Faulet3e964192018-10-24 11:39:23 +02002711 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002712 int act_opts = 0;
Christopher Faulet3e964192018-10-24 11:39:23 +02002713
Christopher Faulet3e964192018-10-24 11:39:23 +02002714 /* If "the current_rule_list" match the executed rule list, we are in
2715 * resume condition. If a resume is needed it is always in the action
2716 * and never in the ACL or converters. In this case, we initialise the
2717 * current rule, and go to the action execution point.
2718 */
2719 if (s->current_rule) {
2720 rule = s->current_rule;
2721 s->current_rule = NULL;
2722 if (s->current_rule_list == rules)
2723 goto resume_execution;
2724 }
2725 s->current_rule_list = rules;
2726
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002727 /* start the ruleset evaluation in strict mode */
2728 txn->req.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01002729
Christopher Faulet3e964192018-10-24 11:39:23 +02002730 list_for_each_entry(rule, rules, list) {
2731 /* check optional condition */
2732 if (rule->cond) {
2733 int ret;
2734
2735 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
2736 ret = acl_pass(ret);
2737
2738 if (rule->cond->pol == ACL_COND_UNLESS)
2739 ret = !ret;
2740
2741 if (!ret) /* condition not matched */
2742 continue;
2743 }
2744
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002745 act_opts |= ACT_OPT_FIRST;
Christopher Faulet3e964192018-10-24 11:39:23 +02002746 resume_execution:
Amaury Denoyelle03517732021-05-07 14:25:01 +02002747 if (rule->kw->flags & KWF_EXPERIMENTAL)
2748 mark_tainted(TAINTED_ACTION_EXP_EXECUTED);
2749
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002750 /* Always call the action function if defined */
2751 if (rule->action_ptr) {
2752 if ((s->req.flags & CF_READ_ERROR) ||
2753 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
2754 (px->options & PR_O_ABRT_CLOSE)))
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002755 act_opts |= ACT_OPT_FINAL;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002756
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002757 switch (rule->action_ptr(rule, px, sess, s, act_opts)) {
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002758 case ACT_RET_CONT:
2759 break;
2760 case ACT_RET_STOP:
2761 rule_ret = HTTP_RULE_RES_STOP;
2762 goto end;
2763 case ACT_RET_YIELD:
2764 s->current_rule = rule;
2765 rule_ret = HTTP_RULE_RES_YIELD;
2766 goto end;
2767 case ACT_RET_ERR:
2768 rule_ret = HTTP_RULE_RES_ERROR;
2769 goto end;
2770 case ACT_RET_DONE:
2771 rule_ret = HTTP_RULE_RES_DONE;
2772 goto end;
2773 case ACT_RET_DENY:
Christopher Fauletb58f62b2020-01-13 16:40:13 +01002774 if (txn->status == -1)
2775 txn->status = 403;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002776 rule_ret = HTTP_RULE_RES_DENY;
2777 goto end;
2778 case ACT_RET_ABRT:
2779 rule_ret = HTTP_RULE_RES_ABRT;
2780 goto end;
2781 case ACT_RET_INV:
2782 rule_ret = HTTP_RULE_RES_BADREQ;
2783 goto end;
2784 }
2785 continue; /* eval the next rule */
2786 }
2787
2788 /* If not action function defined, check for known actions */
Christopher Faulet3e964192018-10-24 11:39:23 +02002789 switch (rule->action) {
2790 case ACT_ACTION_ALLOW:
2791 rule_ret = HTTP_RULE_RES_STOP;
2792 goto end;
2793
2794 case ACT_ACTION_DENY:
Christopher Faulet5cb513a2020-05-13 17:56:56 +02002795 txn->status = rule->arg.http_reply->status;
2796 txn->http_reply = rule->arg.http_reply;
Christopher Faulet3e964192018-10-24 11:39:23 +02002797 rule_ret = HTTP_RULE_RES_DENY;
2798 goto end;
2799
2800 case ACT_HTTP_REQ_TARPIT:
2801 txn->flags |= TX_CLTARPIT;
Christopher Faulet5cb513a2020-05-13 17:56:56 +02002802 txn->status = rule->arg.http_reply->status;
2803 txn->http_reply = rule->arg.http_reply;
Christopher Faulet3e964192018-10-24 11:39:23 +02002804 rule_ret = HTTP_RULE_RES_DENY;
2805 goto end;
2806
Christopher Faulet3e964192018-10-24 11:39:23 +02002807 case ACT_HTTP_REDIR:
Christopher Faulet90d22a82020-03-06 11:18:39 +01002808 rule_ret = HTTP_RULE_RES_ABRT;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002809 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
Christopher Faulet3a26bee2019-12-16 12:47:40 +01002810 rule_ret = HTTP_RULE_RES_ERROR;
Christopher Faulet3e964192018-10-24 11:39:23 +02002811 goto end;
2812
2813 case ACT_HTTP_SET_NICE:
Christopher Faulet96bff762019-12-17 13:46:18 +01002814 s->task->nice = rule->arg.http.i;
Christopher Faulet3e964192018-10-24 11:39:23 +02002815 break;
2816
2817 case ACT_HTTP_SET_TOS:
Christopher Faulet96bff762019-12-17 13:46:18 +01002818 conn_set_tos(objt_conn(sess->origin), rule->arg.http.i);
Christopher Faulet3e964192018-10-24 11:39:23 +02002819 break;
2820
2821 case ACT_HTTP_SET_MARK:
Christopher Faulet96bff762019-12-17 13:46:18 +01002822 conn_set_mark(objt_conn(sess->origin), rule->arg.http.i);
Christopher Faulet3e964192018-10-24 11:39:23 +02002823 break;
2824
2825 case ACT_HTTP_SET_LOGL:
Christopher Faulet96bff762019-12-17 13:46:18 +01002826 s->logs.level = rule->arg.http.i;
Christopher Faulet3e964192018-10-24 11:39:23 +02002827 break;
2828
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002829 /* other flags exists, but normally, they never be matched. */
Christopher Faulet3e964192018-10-24 11:39:23 +02002830 default:
2831 break;
2832 }
2833 }
2834
2835 end:
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002836 /* if the ruleset evaluation is finished reset the strict mode */
Christopher Faulet46f95542019-12-20 10:07:22 +01002837 if (rule_ret != HTTP_RULE_RES_YIELD)
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002838 txn->req.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01002839
Christopher Faulet3e964192018-10-24 11:39:23 +02002840 /* we reached the end of the rules, nothing to report */
2841 return rule_ret;
2842}
2843
2844/* Executes the http-response rules <rules> for stream <s> and proxy <px>. It
2845 * returns one of 5 possible statuses: HTTP_RULE_RES_CONT, HTTP_RULE_RES_STOP,
2846 * HTTP_RULE_RES_DONE, HTTP_RULE_RES_YIELD, or HTTP_RULE_RES_BADREQ. If *CONT
2847 * is returned, the process can continue the evaluation of next rule list. If
2848 * *STOP or *DONE is returned, the process must stop the evaluation. If *BADREQ
2849 * is returned, it means the operation could not be processed and a server error
Christopher Fauleta53abad2020-05-13 08:12:22 +02002850 * must be returned. If *YIELD is returned, the caller must call again the
2851 * function with the same context.
Christopher Faulet3e964192018-10-24 11:39:23 +02002852 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002853static enum rule_result http_res_get_intercept_rule(struct proxy *px, struct list *rules,
2854 struct stream *s)
Christopher Faulet3e964192018-10-24 11:39:23 +02002855{
2856 struct session *sess = strm_sess(s);
2857 struct http_txn *txn = s->txn;
Christopher Faulet3e964192018-10-24 11:39:23 +02002858 struct act_rule *rule;
Christopher Faulet3e964192018-10-24 11:39:23 +02002859 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002860 int act_opts = 0;
Christopher Faulet3e964192018-10-24 11:39:23 +02002861
Christopher Faulet3e964192018-10-24 11:39:23 +02002862 /* If "the current_rule_list" match the executed rule list, we are in
2863 * resume condition. If a resume is needed it is always in the action
2864 * and never in the ACL or converters. In this case, we initialise the
2865 * current rule, and go to the action execution point.
2866 */
2867 if (s->current_rule) {
2868 rule = s->current_rule;
2869 s->current_rule = NULL;
2870 if (s->current_rule_list == rules)
2871 goto resume_execution;
2872 }
2873 s->current_rule_list = rules;
2874
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002875 /* start the ruleset evaluation in strict mode */
2876 txn->rsp.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01002877
Christopher Faulet3e964192018-10-24 11:39:23 +02002878 list_for_each_entry(rule, rules, list) {
2879 /* check optional condition */
2880 if (rule->cond) {
2881 int ret;
2882
2883 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
2884 ret = acl_pass(ret);
2885
2886 if (rule->cond->pol == ACL_COND_UNLESS)
2887 ret = !ret;
2888
2889 if (!ret) /* condition not matched */
2890 continue;
2891 }
2892
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002893 act_opts |= ACT_OPT_FIRST;
Christopher Faulet3e964192018-10-24 11:39:23 +02002894resume_execution:
Amaury Denoyelle03517732021-05-07 14:25:01 +02002895 if (rule->kw->flags & KWF_EXPERIMENTAL)
2896 mark_tainted(TAINTED_ACTION_EXP_EXECUTED);
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002897
2898 /* Always call the action function if defined */
2899 if (rule->action_ptr) {
2900 if ((s->req.flags & CF_READ_ERROR) ||
2901 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
2902 (px->options & PR_O_ABRT_CLOSE)))
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002903 act_opts |= ACT_OPT_FINAL;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002904
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002905 switch (rule->action_ptr(rule, px, sess, s, act_opts)) {
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002906 case ACT_RET_CONT:
2907 break;
2908 case ACT_RET_STOP:
2909 rule_ret = HTTP_RULE_RES_STOP;
2910 goto end;
2911 case ACT_RET_YIELD:
2912 s->current_rule = rule;
2913 rule_ret = HTTP_RULE_RES_YIELD;
2914 goto end;
2915 case ACT_RET_ERR:
2916 rule_ret = HTTP_RULE_RES_ERROR;
2917 goto end;
2918 case ACT_RET_DONE:
2919 rule_ret = HTTP_RULE_RES_DONE;
2920 goto end;
2921 case ACT_RET_DENY:
Christopher Fauletb58f62b2020-01-13 16:40:13 +01002922 if (txn->status == -1)
2923 txn->status = 502;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002924 rule_ret = HTTP_RULE_RES_DENY;
2925 goto end;
2926 case ACT_RET_ABRT:
2927 rule_ret = HTTP_RULE_RES_ABRT;
2928 goto end;
2929 case ACT_RET_INV:
2930 rule_ret = HTTP_RULE_RES_BADREQ;
2931 goto end;
2932 }
2933 continue; /* eval the next rule */
2934 }
2935
2936 /* If not action function defined, check for known actions */
Christopher Faulet3e964192018-10-24 11:39:23 +02002937 switch (rule->action) {
2938 case ACT_ACTION_ALLOW:
2939 rule_ret = HTTP_RULE_RES_STOP; /* "allow" rules are OK */
2940 goto end;
2941
2942 case ACT_ACTION_DENY:
Christopher Faulet5cb513a2020-05-13 17:56:56 +02002943 txn->status = rule->arg.http_reply->status;
2944 txn->http_reply = rule->arg.http_reply;
Christopher Faulet3a26bee2019-12-16 12:47:40 +01002945 rule_ret = HTTP_RULE_RES_DENY;
Christopher Faulet3e964192018-10-24 11:39:23 +02002946 goto end;
2947
2948 case ACT_HTTP_SET_NICE:
Christopher Faulet96bff762019-12-17 13:46:18 +01002949 s->task->nice = rule->arg.http.i;
Christopher Faulet3e964192018-10-24 11:39:23 +02002950 break;
2951
2952 case ACT_HTTP_SET_TOS:
Christopher Faulet96bff762019-12-17 13:46:18 +01002953 conn_set_tos(objt_conn(sess->origin), rule->arg.http.i);
Christopher Faulet3e964192018-10-24 11:39:23 +02002954 break;
2955
2956 case ACT_HTTP_SET_MARK:
Christopher Faulet96bff762019-12-17 13:46:18 +01002957 conn_set_mark(objt_conn(sess->origin), rule->arg.http.i);
Christopher Faulet3e964192018-10-24 11:39:23 +02002958 break;
2959
2960 case ACT_HTTP_SET_LOGL:
Christopher Faulet96bff762019-12-17 13:46:18 +01002961 s->logs.level = rule->arg.http.i;
Christopher Faulet3e964192018-10-24 11:39:23 +02002962 break;
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);
Christopher Faulet085ccd42021-10-15 13:51:34 +02003007 if (ret == HTTP_RULE_RES_CONT && sess->fe != s->be)
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01003008 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);
Willy Tarreau1eb3b482019-10-31 15:50:28 +01003868 if (*uri_auth->uri_prefix == '/')
3869 uri = http_get_path(uri);
Christopher Faulet377c5a52018-10-24 21:21:30 +02003870
3871 /* check URI size */
3872 if (uri_auth->uri_len > uri.len)
3873 return 0;
3874
3875 if (memcmp(uri.ptr, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
3876 return 0;
3877
3878 return 1;
3879}
3880
3881/* This function prepares an applet to handle the stats. It can deal with the
3882 * "100-continue" expectation, check that admin rules are met for POST requests,
3883 * and program a response message if something was unexpected. It cannot fail
3884 * and always relies on the stats applet to complete the job. It does not touch
3885 * analysers nor counters, which are left to the caller. It does not touch
3886 * s->target which is supposed to already point to the stats applet. The caller
3887 * is expected to have already assigned an appctx to the stream.
3888 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003889static int http_handle_stats(struct stream *s, struct channel *req)
Christopher Faulet377c5a52018-10-24 21:21:30 +02003890{
3891 struct stats_admin_rule *stats_admin_rule;
3892 struct stream_interface *si = &s->si[1];
3893 struct session *sess = s->sess;
3894 struct http_txn *txn = s->txn;
3895 struct http_msg *msg = &txn->req;
3896 struct uri_auth *uri_auth = s->be->uri_auth;
3897 const char *h, *lookup, *end;
3898 struct appctx *appctx;
3899 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003900 struct htx_sl *sl;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003901
3902 appctx = si_appctx(si);
3903 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
3904 appctx->st1 = appctx->st2 = 0;
3905 appctx->ctx.stats.st_code = STAT_STATUS_INIT;
Willy Tarreau676c29e2019-10-09 10:50:01 +02003906 appctx->ctx.stats.flags |= uri_auth->flags;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003907 appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */
3908 if ((msg->flags & HTTP_MSGF_VER_11) && (txn->meth != HTTP_METH_HEAD))
3909 appctx->ctx.stats.flags |= STAT_CHUNKED;
3910
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003911 htx = htxbuf(&req->buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02003912 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003913 lookup = HTX_SL_REQ_UPTR(sl) + uri_auth->uri_len;
3914 end = HTX_SL_REQ_UPTR(sl) + HTX_SL_REQ_ULEN(sl);
Christopher Faulet377c5a52018-10-24 21:21:30 +02003915
3916 for (h = lookup; h <= end - 3; h++) {
3917 if (memcmp(h, ";up", 3) == 0) {
3918 appctx->ctx.stats.flags |= STAT_HIDE_DOWN;
3919 break;
3920 }
Amaury Denoyelle91e55ea2021-02-25 14:46:08 +01003921 }
3922
3923 for (h = lookup; h <= end - 9; h++) {
3924 if (memcmp(h, ";no-maint", 9) == 0) {
Willy Tarreau3e320362020-10-23 17:28:57 +02003925 appctx->ctx.stats.flags |= STAT_HIDE_MAINT;
3926 break;
3927 }
Christopher Faulet377c5a52018-10-24 21:21:30 +02003928 }
3929
3930 if (uri_auth->refresh) {
3931 for (h = lookup; h <= end - 10; h++) {
3932 if (memcmp(h, ";norefresh", 10) == 0) {
3933 appctx->ctx.stats.flags |= STAT_NO_REFRESH;
3934 break;
3935 }
3936 }
3937 }
3938
3939 for (h = lookup; h <= end - 4; h++) {
3940 if (memcmp(h, ";csv", 4) == 0) {
Christopher Faulet6338a082019-09-09 15:50:54 +02003941 appctx->ctx.stats.flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
Christopher Faulet377c5a52018-10-24 21:21:30 +02003942 break;
3943 }
3944 }
3945
3946 for (h = lookup; h <= end - 6; h++) {
3947 if (memcmp(h, ";typed", 6) == 0) {
Christopher Faulet6338a082019-09-09 15:50:54 +02003948 appctx->ctx.stats.flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
Christopher Faulet377c5a52018-10-24 21:21:30 +02003949 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
3950 break;
3951 }
3952 }
3953
Christopher Faulet6338a082019-09-09 15:50:54 +02003954 for (h = lookup; h <= end - 5; h++) {
3955 if (memcmp(h, ";json", 5) == 0) {
3956 appctx->ctx.stats.flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
3957 appctx->ctx.stats.flags |= STAT_FMT_JSON;
3958 break;
3959 }
3960 }
3961
3962 for (h = lookup; h <= end - 12; h++) {
3963 if (memcmp(h, ";json-schema", 12) == 0) {
3964 appctx->ctx.stats.flags &= ~STAT_FMT_MASK;
3965 appctx->ctx.stats.flags |= STAT_JSON_SCHM;
3966 break;
3967 }
3968 }
3969
Christopher Faulet377c5a52018-10-24 21:21:30 +02003970 for (h = lookup; h <= end - 8; h++) {
3971 if (memcmp(h, ";st=", 4) == 0) {
3972 int i;
3973 h += 4;
3974 appctx->ctx.stats.st_code = STAT_STATUS_UNKN;
3975 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
3976 if (strncmp(stat_status_codes[i], h, 4) == 0) {
3977 appctx->ctx.stats.st_code = i;
3978 break;
3979 }
3980 }
3981 break;
3982 }
3983 }
3984
3985 appctx->ctx.stats.scope_str = 0;
3986 appctx->ctx.stats.scope_len = 0;
3987 for (h = lookup; h <= end - 8; h++) {
3988 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
3989 int itx = 0;
3990 const char *h2;
3991 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
3992 const char *err;
3993
3994 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
3995 h2 = h;
Christopher Fauleted7a0662019-01-14 11:07:34 +01003996 appctx->ctx.stats.scope_str = h2 - HTX_SL_REQ_UPTR(sl);
3997 while (h < end) {
Christopher Faulet377c5a52018-10-24 21:21:30 +02003998 if (*h == ';' || *h == '&' || *h == ' ')
3999 break;
4000 itx++;
4001 h++;
4002 }
4003
4004 if (itx > STAT_SCOPE_TXT_MAXLEN)
4005 itx = STAT_SCOPE_TXT_MAXLEN;
4006 appctx->ctx.stats.scope_len = itx;
4007
4008 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
4009 memcpy(scope_txt, h2, itx);
4010 scope_txt[itx] = '\0';
4011 err = invalid_char(scope_txt);
4012 if (err) {
4013 /* bad char in search text => clear scope */
4014 appctx->ctx.stats.scope_str = 0;
4015 appctx->ctx.stats.scope_len = 0;
4016 }
4017 break;
4018 }
4019 }
4020
4021 /* now check whether we have some admin rules for this request */
4022 list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) {
4023 int ret = 1;
4024
4025 if (stats_admin_rule->cond) {
4026 ret = acl_exec_cond(stats_admin_rule->cond, s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
4027 ret = acl_pass(ret);
4028 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
4029 ret = !ret;
4030 }
4031
4032 if (ret) {
4033 /* no rule, or the rule matches */
4034 appctx->ctx.stats.flags |= STAT_ADMIN;
4035 break;
4036 }
4037 }
4038
Christopher Faulet5d45e382019-02-27 15:15:23 +01004039 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
4040 appctx->st0 = STAT_HTTP_HEAD;
4041 else if (txn->meth == HTTP_METH_POST) {
Christopher Fauletbd9e8422019-08-15 22:26:48 +02004042 if (appctx->ctx.stats.flags & STAT_ADMIN) {
Christopher Faulet377c5a52018-10-24 21:21:30 +02004043 appctx->st0 = STAT_HTTP_POST;
Christopher Fauletbd9e8422019-08-15 22:26:48 +02004044 if (msg->msg_state < HTTP_MSG_DATA)
4045 req->analysers |= AN_REQ_HTTP_BODY;
4046 }
Christopher Faulet377c5a52018-10-24 21:21:30 +02004047 else {
Christopher Faulet5d45e382019-02-27 15:15:23 +01004048 /* POST without admin level */
Christopher Faulet377c5a52018-10-24 21:21:30 +02004049 appctx->ctx.stats.flags &= ~STAT_CHUNKED;
4050 appctx->ctx.stats.st_code = STAT_STATUS_DENY;
4051 appctx->st0 = STAT_HTTP_LAST;
4052 }
4053 }
4054 else {
Christopher Faulet5d45e382019-02-27 15:15:23 +01004055 /* Unsupported method */
4056 appctx->ctx.stats.flags &= ~STAT_CHUNKED;
4057 appctx->ctx.stats.st_code = STAT_STATUS_IVAL;
4058 appctx->st0 = STAT_HTTP_LAST;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004059 }
4060
4061 s->task->nice = -32; /* small boost for HTTP statistics */
4062 return 1;
4063}
4064
Christopher Faulet021a8e42021-03-29 10:46:38 +02004065/* This function waits for the message payload at most <time> milliseconds (may
4066 * be set to TICK_ETERNITY). It stops to wait if at least <bytes> bytes of the
4067 * payload are received (0 means no limit). It returns HTTP_RULE_* depending on
4068 * the result:
4069 *
4070 * - HTTP_RULE_RES_CONT when conditions are met to stop waiting
4071 * - HTTP_RULE_RES_YIELD to wait for more data
Ilya Shipitsinb2be9a12021-04-24 13:25:42 +05004072 * - HTTP_RULE_RES_ABRT when a timeout occurred.
Christopher Faulet021a8e42021-03-29 10:46:38 +02004073 * - HTTP_RULE_RES_BADREQ if a parsing error is raised by lower level
Ilya Shipitsinb2be9a12021-04-24 13:25:42 +05004074 * - HTTP_RULE_RES_ERROR if an internal error occurred
Christopher Faulet021a8e42021-03-29 10:46:38 +02004075 *
Ilya Shipitsinb2be9a12021-04-24 13:25:42 +05004076 * If a timeout occurred, this function is responsible to emit the right response
Christopher Faulet021a8e42021-03-29 10:46:38 +02004077 * to the client, depending on the channel (408 on request side, 504 on response
4078 * side). All other errors must be handled by the caller.
4079 */
4080enum rule_result http_wait_for_msg_body(struct stream *s, struct channel *chn,
4081 unsigned int time, unsigned int bytes)
4082{
4083 struct session *sess = s->sess;
4084 struct http_txn *txn = s->txn;
4085 struct http_msg *msg = ((chn->flags & CF_ISRESP) ? &txn->rsp : &txn->req);
4086 struct htx *htx;
4087 enum rule_result ret = HTTP_RULE_RES_CONT;
4088
4089 htx = htxbuf(&chn->buf);
4090
4091 if (htx->flags & HTX_FL_PARSING_ERROR) {
4092 ret = HTTP_RULE_RES_BADREQ;
4093 goto end;
4094 }
4095 if (htx->flags & HTX_FL_PROCESSING_ERROR) {
4096 ret = HTTP_RULE_RES_ERROR;
4097 goto end;
4098 }
4099
4100 /* Do nothing for bodyless and CONNECT requests */
4101 if (txn->meth == HTTP_METH_CONNECT || (msg->flags & HTTP_MSGF_BODYLESS))
4102 goto end;
4103
4104 if (!(chn->flags & CF_ISRESP) && msg->msg_state < HTTP_MSG_DATA) {
4105 if (http_handle_expect_hdr(s, htx, msg) == -1) {
4106 ret = HTTP_RULE_RES_ERROR;
4107 goto end;
4108 }
4109 }
4110
4111 msg->msg_state = HTTP_MSG_DATA;
4112
4113 /* Now we're in HTTP_MSG_DATA. We just need to know if all data have
4114 * been received or if the buffer is full.
4115 */
Christopher Faulet59df8122021-09-23 14:46:32 +02004116 if ((htx->flags & HTX_FL_EOM) ||
4117 htx_get_tail_type(htx) > HTX_BLK_DATA ||
4118 channel_htx_full(chn, htx, global.tune.maxrewrite) ||
4119 si_rx_blocked_room(chn_prod(chn)))
Christopher Faulet021a8e42021-03-29 10:46:38 +02004120 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;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004185
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004186 /*
4187 * Create the location
4188 */
4189 chunk_reset(&trash);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004190
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004191 /* 1: add the server's prefix */
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004192 /* special prefix "/" means don't change URL */
4193 srv = __objt_server(s->target);
4194 if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') {
4195 if (!chunk_memcat(&trash, srv->rdr_pfx, srv->rdr_len))
4196 return;
4197 }
4198
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004199 /* 2: add the request Path */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004200 htx = htxbuf(&req->buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02004201 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004202 path = http_get_path(htx_sl_req_uri(sl));
Tim Duesterhused526372020-03-05 17:56:33 +01004203 if (!isttest(path))
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004204 return;
4205
4206 if (!chunk_memcat(&trash, path.ptr, path.len))
4207 return;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004208 location = ist2(trash.area, trash.data);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004209
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004210 /*
4211 * Create the 302 respone
4212 */
4213 htx = htx_from_buf(&res->buf);
4214 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
4215 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
4216 ist("HTTP/1.1"), ist("302"), ist("Found"));
4217 if (!sl)
4218 goto fail;
4219 sl->info.res.status = 302;
4220 s->txn->status = 302;
4221
4222 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) ||
4223 !htx_add_header(htx, ist("Connection"), ist("close")) ||
4224 !htx_add_header(htx, ist("Content-length"), ist("0")) ||
4225 !htx_add_header(htx, ist("Location"), location))
4226 goto fail;
4227
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004228 if (!htx_add_endof(htx, HTX_BLK_EOH))
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004229 goto fail;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004230
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004231 htx->flags |= HTX_FL_EOM;
Christopher Fauletc20afb82020-01-24 19:16:26 +01004232 htx_to_buf(htx, &res->buf);
Christopher Fauleta72a7e42020-01-28 09:28:11 +01004233 if (!http_forward_proxy_resp(s, 1))
4234 goto fail;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004235
4236 /* return without error. */
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004237 si_shutr(si);
4238 si_shutw(si);
4239 si->err_type = SI_ET_NONE;
4240 si->state = SI_ST_CLO;
4241
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004242 if (!(s->flags & SF_ERR_MASK))
4243 s->flags |= SF_ERR_LOCAL;
4244 if (!(s->flags & SF_FINST_MASK))
4245 s->flags |= SF_FINST_C;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004246
4247 /* FIXME: we should increase a counter of redirects per server and per backend. */
4248 srv_inc_sess_ctr(srv);
4249 srv_set_sess_last(srv);
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004250 return;
4251
4252 fail:
4253 /* If an error occurred, remove the incomplete HTTP response from the
4254 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004255 channel_htx_truncate(res, htx);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004256}
4257
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05004258/* This function terminates the request because it was completely analyzed or
Christopher Fauletf2824e62018-10-01 12:12:37 +02004259 * because an error was triggered during the body forwarding.
4260 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004261static void http_end_request(struct stream *s)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004262{
4263 struct channel *chn = &s->req;
4264 struct http_txn *txn = s->txn;
4265
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004266 DBG_TRACE_ENTER(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004267
Christopher Fauletb42a8b62018-11-19 21:59:00 +01004268 if (unlikely(txn->req.msg_state == HTTP_MSG_ERROR ||
4269 txn->rsp.msg_state == HTTP_MSG_ERROR)) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004270 channel_abort(chn);
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004271 channel_htx_truncate(chn, htxbuf(&chn->buf));
Christopher Fauletf2824e62018-10-01 12:12:37 +02004272 goto end;
4273 }
4274
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004275 if (unlikely(txn->req.msg_state < HTTP_MSG_DONE)) {
4276 DBG_TRACE_DEVEL("waiting end of the request", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004277 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004278 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004279
4280 if (txn->req.msg_state == HTTP_MSG_DONE) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004281 /* No need to read anymore, the request was completely parsed.
4282 * We can shut the read side unless we want to abort_on_close,
4283 * or we have a POST request. The issue with POST requests is
4284 * that some browsers still send a CRLF after the request, and
4285 * this CRLF must be read so that it does not remain in the kernel
4286 * buffers, otherwise a close could cause an RST on some systems
4287 * (eg: Linux).
4288 */
Christopher Faulet769d0e92019-03-22 14:23:18 +01004289 if (!(s->be->options & PR_O_ABRT_CLOSE) && txn->meth != HTTP_METH_POST)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004290 channel_dont_read(chn);
4291
4292 /* if the server closes the connection, we want to immediately react
4293 * and close the socket to save packets and syscalls.
4294 */
4295 s->si[1].flags |= SI_FL_NOHALF;
4296
4297 /* In any case we've finished parsing the request so we must
4298 * disable Nagle when sending data because 1) we're not going
4299 * to shut this side, and 2) the server is waiting for us to
4300 * send pending data.
4301 */
4302 chn->flags |= CF_NEVER_WAIT;
4303
Christopher Fauletd01ce402019-01-02 17:44:13 +01004304 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
4305 /* The server has not finished to respond, so we
4306 * don't want to move in order not to upset it.
4307 */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004308 DBG_TRACE_DEVEL("waiting end of the response", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletd01ce402019-01-02 17:44:13 +01004309 return;
4310 }
4311
Christopher Fauletf2824e62018-10-01 12:12:37 +02004312 /* When we get here, it means that both the request and the
4313 * response have finished receiving. Depending on the connection
4314 * mode, we'll have to wait for the last bytes to leave in either
4315 * direction, and sometimes for a close to be effective.
4316 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02004317 if (txn->flags & TX_CON_WANT_TUN) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004318 /* Tunnel mode will not have any analyser so it needs to
4319 * poll for reads.
4320 */
4321 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004322 if (b_data(&chn->buf)) {
4323 DBG_TRACE_DEVEL("waiting to flush the request", STRM_EV_HTTP_ANA, s, txn);
Christopher Faulet9768c262018-10-22 09:34:31 +02004324 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004325 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004326 txn->req.msg_state = HTTP_MSG_TUNNEL;
4327 }
4328 else {
4329 /* we're not expecting any new data to come for this
4330 * transaction, so we can close it.
Christopher Faulet9768c262018-10-22 09:34:31 +02004331 *
4332 * However, there is an exception if the response
4333 * length is undefined. In this case, we need to wait
4334 * the close from the server. The response will be
4335 * switched in TUNNEL mode until the end.
Christopher Fauletf2824e62018-10-01 12:12:37 +02004336 */
4337 if (!(txn->rsp.flags & HTTP_MSGF_XFER_LEN) &&
4338 txn->rsp.msg_state != HTTP_MSG_CLOSED)
Christopher Faulet9768c262018-10-22 09:34:31 +02004339 goto check_channel_flags;
Christopher Fauletf2824e62018-10-01 12:12:37 +02004340
4341 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4342 channel_shutr_now(chn);
4343 channel_shutw_now(chn);
4344 }
4345 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004346 goto check_channel_flags;
4347 }
4348
4349 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
4350 http_msg_closing:
4351 /* nothing else to forward, just waiting for the output buffer
4352 * to be empty and for the shutw_now to take effect.
4353 */
4354 if (channel_is_empty(chn)) {
4355 txn->req.msg_state = HTTP_MSG_CLOSED;
4356 goto http_msg_closed;
4357 }
4358 else if (chn->flags & CF_SHUTW) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004359 txn->req.msg_state = HTTP_MSG_ERROR;
4360 goto end;
4361 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004362 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004363 return;
4364 }
4365
4366 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
4367 http_msg_closed:
Christopher Fauletf2824e62018-10-01 12:12:37 +02004368 /* if we don't know whether the server will close, we need to hard close */
4369 if (txn->rsp.flags & HTTP_MSGF_XFER_LEN)
4370 s->si[1].flags |= SI_FL_NOLINGER; /* we want to close ASAP */
Christopher Fauletf2824e62018-10-01 12:12:37 +02004371 /* see above in MSG_DONE why we only do this in these states */
Christopher Faulet769d0e92019-03-22 14:23:18 +01004372 if (!(s->be->options & PR_O_ABRT_CLOSE))
Christopher Fauletf2824e62018-10-01 12:12:37 +02004373 channel_dont_read(chn);
4374 goto end;
4375 }
4376
4377 check_channel_flags:
4378 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4379 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4380 /* if we've just closed an output, let's switch */
4381 txn->req.msg_state = HTTP_MSG_CLOSING;
4382 goto http_msg_closing;
4383 }
4384
4385 end:
4386 chn->analysers &= AN_REQ_FLT_END;
Christopher Faulet198ef8b2020-12-15 13:32:55 +01004387 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
4388 chn->flags |= CF_NEVER_WAIT;
4389 if (HAS_REQ_DATA_FILTERS(s))
Christopher Fauletf2824e62018-10-01 12:12:37 +02004390 chn->analysers |= AN_REQ_FLT_XFER_DATA;
Christopher Faulet198ef8b2020-12-15 13:32:55 +01004391 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004392 channel_auto_close(chn);
4393 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004394 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004395}
4396
4397
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05004398/* This function terminates the response because it was completely analyzed or
Christopher Fauletf2824e62018-10-01 12:12:37 +02004399 * because an error was triggered during the body forwarding.
4400 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004401static void http_end_response(struct stream *s)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004402{
4403 struct channel *chn = &s->res;
4404 struct http_txn *txn = s->txn;
4405
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004406 DBG_TRACE_ENTER(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004407
Christopher Fauletb42a8b62018-11-19 21:59:00 +01004408 if (unlikely(txn->req.msg_state == HTTP_MSG_ERROR ||
4409 txn->rsp.msg_state == HTTP_MSG_ERROR)) {
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004410 channel_htx_truncate(&s->req, htxbuf(&s->req.buf));
Christopher Faulet9768c262018-10-22 09:34:31 +02004411 channel_abort(&s->req);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004412 goto end;
4413 }
4414
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004415 if (unlikely(txn->rsp.msg_state < HTTP_MSG_DONE)) {
4416 DBG_TRACE_DEVEL("waiting end of the response", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004417 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004418 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004419
4420 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
4421 /* In theory, we don't need to read anymore, but we must
4422 * still monitor the server connection for a possible close
4423 * while the request is being uploaded, so we don't disable
4424 * reading.
4425 */
4426 /* channel_dont_read(chn); */
4427
4428 if (txn->req.msg_state < HTTP_MSG_DONE) {
4429 /* The client seems to still be sending data, probably
4430 * because we got an error response during an upload.
4431 * We have the choice of either breaking the connection
4432 * or letting it pass through. Let's do the later.
4433 */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004434 DBG_TRACE_DEVEL("waiting end of the request", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004435 return;
4436 }
4437
4438 /* When we get here, it means that both the request and the
4439 * response have finished receiving. Depending on the connection
4440 * mode, we'll have to wait for the last bytes to leave in either
4441 * direction, and sometimes for a close to be effective.
4442 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02004443 if (txn->flags & TX_CON_WANT_TUN) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004444 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004445 if (b_data(&chn->buf)) {
4446 DBG_TRACE_DEVEL("waiting to flush the respone", STRM_EV_HTTP_ANA, s, txn);
Christopher Faulet9768c262018-10-22 09:34:31 +02004447 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004448 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004449 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
4450 }
4451 else {
4452 /* we're not expecting any new data to come for this
4453 * transaction, so we can close it.
4454 */
4455 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4456 channel_shutr_now(chn);
4457 channel_shutw_now(chn);
4458 }
4459 }
4460 goto check_channel_flags;
4461 }
4462
4463 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
4464 http_msg_closing:
4465 /* nothing else to forward, just waiting for the output buffer
4466 * to be empty and for the shutw_now to take effect.
4467 */
4468 if (channel_is_empty(chn)) {
4469 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4470 goto http_msg_closed;
4471 }
4472 else if (chn->flags & CF_SHUTW) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004473 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau4781b152021-04-06 13:53:36 +02004474 _HA_ATOMIC_INC(&strm_sess(s)->fe->fe_counters.cli_aborts);
4475 _HA_ATOMIC_INC(&s->be->be_counters.cli_aborts);
William Lallemand36119de2021-03-08 15:26:48 +01004476 if (strm_sess(s)->listener && strm_sess(s)->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02004477 _HA_ATOMIC_INC(&strm_sess(s)->listener->counters->cli_aborts);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004478 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02004479 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.cli_aborts);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004480 goto end;
4481 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004482 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004483 return;
4484 }
4485
4486 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
4487 http_msg_closed:
4488 /* drop any pending data */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004489 channel_htx_truncate(&s->req, htxbuf(&s->req.buf));
Christopher Faulet9768c262018-10-22 09:34:31 +02004490 channel_abort(&s->req);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004491 goto end;
4492 }
4493
4494 check_channel_flags:
4495 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4496 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4497 /* if we've just closed an output, let's switch */
4498 txn->rsp.msg_state = HTTP_MSG_CLOSING;
4499 goto http_msg_closing;
4500 }
4501
4502 end:
4503 chn->analysers &= AN_RES_FLT_END;
Christopher Faulet198ef8b2020-12-15 13:32:55 +01004504 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
4505 chn->flags |= CF_NEVER_WAIT;
4506 if (HAS_RSP_DATA_FILTERS(s))
4507 chn->analysers |= AN_RES_FLT_XFER_DATA;
4508 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004509 channel_auto_close(chn);
4510 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004511 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004512}
4513
Christopher Fauletef70e252020-01-28 09:26:19 +01004514/* Forward a response generated by HAProxy (error/redirect/return). This
4515 * function forwards all pending incoming data. If <final> is set to 0, nothing
4516 * more is performed. It is used for 1xx informational messages. Otherwise, the
Christopher Faulet507479b2020-05-15 12:29:46 +02004517 * transaction is terminated and the request is emptied. On success 1 is
Christopher Faulet40e6b552020-06-25 16:04:50 +02004518 * returned. If an error occurred, 0 is returned. If it fails, this function
4519 * only exits. It is the caller responsibility to do the cleanup.
Christopher Fauletef70e252020-01-28 09:26:19 +01004520 */
4521int http_forward_proxy_resp(struct stream *s, int final)
4522{
4523 struct channel *req = &s->req;
4524 struct channel *res = &s->res;
4525 struct htx *htx = htxbuf(&res->buf);
4526 size_t data;
4527
4528 if (final) {
4529 htx->flags |= HTX_FL_PROXY_RESP;
Christopher Faulet507479b2020-05-15 12:29:46 +02004530
Christopher Fauletaab1b672020-11-18 16:44:02 +01004531 if (!htx_is_empty(htx) && !http_eval_after_res_rules(s))
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01004532 return 0;
Christopher Fauletef70e252020-01-28 09:26:19 +01004533
Christopher Fauletd6c48362020-10-19 18:01:38 +02004534 if (s->txn->meth == HTTP_METH_HEAD)
4535 htx_skip_msg_payload(htx);
4536
Christopher Fauletef70e252020-01-28 09:26:19 +01004537 channel_auto_read(req);
4538 channel_abort(req);
4539 channel_auto_close(req);
4540 channel_htx_erase(req, htxbuf(&req->buf));
4541
4542 res->wex = tick_add_ifset(now_ms, res->wto);
4543 channel_auto_read(res);
4544 channel_auto_close(res);
4545 channel_shutr_now(res);
Christopher Faulet1a9db7c2020-06-25 15:36:45 +02004546 res->flags |= CF_EOI; /* The response is terminated, add EOI */
Christopher Faulet42432f32020-11-20 17:43:16 +01004547 htxbuf(&res->buf)->flags |= HTX_FL_EOM; /* no more data are expected */
Christopher Fauletef70e252020-01-28 09:26:19 +01004548 }
Christopher Fauletcf6898c2020-06-25 15:55:11 +02004549 else {
4550 /* Send ASAP informational messages. Rely on CF_EOI for final
4551 * response.
4552 */
4553 res->flags |= CF_SEND_DONTWAIT;
4554 }
Christopher Fauletef70e252020-01-28 09:26:19 +01004555
4556 data = htx->data - co_data(res);
4557 c_adv(res, data);
4558 htx->first = -1;
4559 res->total += data;
4560 return 1;
4561}
4562
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004563void http_server_error(struct stream *s, struct stream_interface *si, int err,
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004564 int finst, struct http_reply *msg)
Christopher Faulet0f226952018-10-22 09:29:56 +02004565{
Christopher Faulet72c7d8d2020-01-27 15:32:25 +01004566 http_reply_and_close(s, s->txn->status, msg);
Christopher Faulet0f226952018-10-22 09:29:56 +02004567 if (!(s->flags & SF_ERR_MASK))
4568 s->flags |= err;
4569 if (!(s->flags & SF_FINST_MASK))
4570 s->flags |= finst;
4571}
4572
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004573void http_reply_and_close(struct stream *s, short status, struct http_reply *msg)
Christopher Faulet0f226952018-10-22 09:29:56 +02004574{
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004575 if (!msg) {
4576 channel_htx_truncate(&s->res, htxbuf(&s->res.buf));
4577 goto end;
4578 }
4579
4580 if (http_reply_message(s, msg) == -1) {
4581 /* On error, return a 500 error message, but don't rewrite it if
Christopher Faulet40e6b552020-06-25 16:04:50 +02004582 * it is already an internal error. If it was already a "const"
4583 * 500 error, just fail.
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004584 */
Christopher Faulet40e6b552020-06-25 16:04:50 +02004585 if (s->txn->status == 500) {
4586 if (s->txn->flags & TX_CONST_REPLY)
4587 goto end;
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004588 s->txn->flags |= TX_CONST_REPLY;
Christopher Faulet40e6b552020-06-25 16:04:50 +02004589 }
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004590 s->txn->status = 500;
4591 s->txn->http_reply = NULL;
4592 return http_reply_and_close(s, s->txn->status, http_error_message(s));
4593 }
4594
4595end:
4596 s->res.wex = tick_add_ifset(now_ms, s->res.wto);
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004597
Christopher Fauletdb8ba102021-09-10 09:17:50 +02004598 /* At this staged, HTTP analysis is finished */
4599 s->req.analysers &= AN_REQ_FLT_END;
4600 s->req.analyse_exp = TICK_ETERNITY;
4601
4602 s->res.analysers &= AN_RES_FLT_END;
4603 s->res.analyse_exp = TICK_ETERNITY;
4604
Christopher Faulet0f226952018-10-22 09:29:56 +02004605 channel_auto_read(&s->req);
4606 channel_abort(&s->req);
4607 channel_auto_close(&s->req);
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004608 channel_htx_erase(&s->req, htxbuf(&s->req.buf));
Christopher Faulet72c7d8d2020-01-27 15:32:25 +01004609 channel_auto_read(&s->res);
4610 channel_auto_close(&s->res);
4611 channel_shutr_now(&s->res);
Christopher Faulet0f226952018-10-22 09:29:56 +02004612}
4613
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004614struct http_reply *http_error_message(struct stream *s)
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004615{
4616 const int msgnum = http_get_status_idx(s->txn->status);
4617
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004618 if (s->txn->http_reply)
4619 return s->txn->http_reply;
4620 else if (s->be->replies[msgnum])
4621 return s->be->replies[msgnum];
4622 else if (strm_fe(s)->replies[msgnum])
4623 return strm_fe(s)->replies[msgnum];
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004624 else
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004625 return &http_err_replies[msgnum];
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004626}
4627
Christopher Faulet40e6b552020-06-25 16:04:50 +02004628/* Produces an HTX message from an http reply. Depending on the http reply type,
4629 * a, errorfile, an raw file or a log-format string is used. On success, it
4630 * returns 0. If an error occurs -1 is returned. If it fails, this function only
4631 * exits. It is the caller responsibility to do the cleanup.
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004632 */
Christopher Fauletae43b6c2020-05-27 15:24:22 +02004633int http_reply_to_htx(struct stream *s, struct htx *htx, struct http_reply *reply)
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004634{
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004635 struct buffer *errmsg;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004636 struct htx_sl *sl;
4637 struct buffer *body = NULL;
4638 const char *status, *reason, *clen, *ctype;
4639 unsigned int slflags;
4640 int ret = 0;
4641
Christopher Faulete29a97e2020-05-14 14:49:25 +02004642 /*
4643 * - HTTP_REPLY_ERRFILES unexpected here. handled as no payload if so
4644 *
4645 * - HTTP_REPLY_INDIRECT: switch on another reply if defined or handled
4646 * as no payload if NULL. the TXN status code is set with the status
4647 * of the original reply.
4648 */
4649
4650 if (reply->type == HTTP_REPLY_INDIRECT) {
4651 if (reply->body.reply)
4652 reply = reply->body.reply;
4653 }
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004654 if (reply->type == HTTP_REPLY_ERRMSG && !reply->body.errmsg) {
4655 /* get default error message */
4656 if (reply == s->txn->http_reply)
4657 s->txn->http_reply = NULL;
4658 reply = http_error_message(s);
4659 if (reply->type == HTTP_REPLY_INDIRECT) {
4660 if (reply->body.reply)
4661 reply = reply->body.reply;
4662 }
4663 }
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004664
4665 if (reply->type == HTTP_REPLY_ERRMSG) {
4666 /* implicit or explicit error message*/
4667 errmsg = reply->body.errmsg;
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004668 if (errmsg && !b_is_null(errmsg)) {
Christopher Faulet20567362020-05-15 14:52:49 +02004669 if (!htx_copy_msg(htx, errmsg))
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004670 goto fail;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004671 }
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004672 }
4673 else {
4674 /* no payload, file or log-format string */
4675 if (reply->type == HTTP_REPLY_RAW) {
4676 /* file */
4677 body = &reply->body.obj;
4678 }
4679 else if (reply->type == HTTP_REPLY_LOGFMT) {
4680 /* log-format string */
4681 body = alloc_trash_chunk();
4682 if (!body)
4683 goto fail_alloc;
4684 body->data = build_logline(s, body->area, body->size, &reply->body.fmt);
4685 }
4686 /* else no payload */
4687
4688 status = ultoa(reply->status);
4689 reason = http_get_reason(reply->status);
4690 slflags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_CLEN);
4691 if (!body || !b_data(body))
4692 slflags |= HTX_SL_F_BODYLESS;
4693 sl = htx_add_stline(htx, HTX_BLK_RES_SL, slflags, ist("HTTP/1.1"), ist(status), ist(reason));
4694 if (!sl)
4695 goto fail;
4696 sl->info.res.status = reply->status;
4697
4698 clen = (body ? ultoa(b_data(body)) : "0");
4699 ctype = reply->ctype;
4700
4701 if (!LIST_ISEMPTY(&reply->hdrs)) {
4702 struct http_reply_hdr *hdr;
4703 struct buffer *value = alloc_trash_chunk();
4704
4705 if (!value)
4706 goto fail;
4707
4708 list_for_each_entry(hdr, &reply->hdrs, list) {
4709 chunk_reset(value);
4710 value->data = build_logline(s, value->area, value->size, &hdr->value);
4711 if (b_data(value) && !htx_add_header(htx, hdr->name, ist2(b_head(value), b_data(value)))) {
4712 free_trash_chunk(value);
4713 goto fail;
4714 }
4715 chunk_reset(value);
4716 }
4717 free_trash_chunk(value);
4718 }
4719
4720 if (!htx_add_header(htx, ist("content-length"), ist(clen)) ||
4721 (body && b_data(body) && ctype && !htx_add_header(htx, ist("content-type"), ist(ctype))) ||
4722 !htx_add_endof(htx, HTX_BLK_EOH) ||
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004723 (body && b_data(body) && !htx_add_data_atonce(htx, ist2(b_head(body), b_data(body)))))
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004724 goto fail;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004725
4726 htx->flags |= HTX_FL_EOM;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004727 }
4728
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004729 leave:
4730 if (reply->type == HTTP_REPLY_LOGFMT)
4731 free_trash_chunk(body);
4732 return ret;
4733
4734 fail_alloc:
4735 if (!(s->flags & SF_ERR_MASK))
4736 s->flags |= SF_ERR_RESOURCE;
Christopher Faulet97e466c2020-05-15 15:12:47 +02004737 /* fall through */
4738 fail:
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004739 ret = -1;
4740 goto leave;
Christopher Faulet97e466c2020-05-15 15:12:47 +02004741}
4742
4743/* Send an http reply to the client. On success, it returns 0. If an error
Christopher Faulet40e6b552020-06-25 16:04:50 +02004744 * occurs -1 is returned and the response channel is truncated, removing this
4745 * way the faulty reply. This function may fail when the reply is formatted
4746 * (http_reply_to_htx) or when the reply is forwarded
4747 * (http_forward_proxy_resp). On the last case, it is because a
4748 * http-after-response rule fails.
Christopher Faulet97e466c2020-05-15 15:12:47 +02004749 */
4750int http_reply_message(struct stream *s, struct http_reply *reply)
4751{
4752 struct channel *res = &s->res;
4753 struct htx *htx = htx_from_buf(&res->buf);
4754
4755 if (s->txn->status == -1)
4756 s->txn->status = reply->status;
4757 channel_htx_truncate(res, htx);
4758
4759 if (http_reply_to_htx(s, htx, reply) == -1)
4760 goto fail;
4761
4762 htx_to_buf(htx, &s->res.buf);
4763 if (!http_forward_proxy_resp(s, 1))
4764 goto fail;
4765 return 0;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004766
4767 fail:
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004768 channel_htx_truncate(res, htx);
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004769 if (!(s->flags & SF_ERR_MASK))
4770 s->flags |= SF_ERR_PRXCOND;
Christopher Faulet97e466c2020-05-15 15:12:47 +02004771 return -1;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004772}
4773
Christopher Faulet304cc402019-07-15 15:46:28 +02004774/* Return the error message corresponding to si->err_type. It is assumed
4775 * that the server side is closed. Note that err_type is actually a
4776 * bitmask, where almost only aborts may be cumulated with other
4777 * values. We consider that aborted operations are more important
4778 * than timeouts or errors due to the fact that nobody else in the
4779 * logs might explain incomplete retries. All others should avoid
4780 * being cumulated. It should normally not be possible to have multiple
4781 * aborts at once, but just in case, the first one in sequence is reported.
4782 * Note that connection errors appearing on the second request of a keep-alive
4783 * connection are not reported since this allows the client to retry.
4784 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004785void http_return_srv_error(struct stream *s, struct stream_interface *si)
Christopher Faulet304cc402019-07-15 15:46:28 +02004786{
4787 int err_type = si->err_type;
4788
4789 /* set s->txn->status for http_error_message(s) */
4790 s->txn->status = 503;
4791
4792 if (err_type & SI_ET_QUEUE_ABRT)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004793 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_Q,
4794 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004795 else if (err_type & SI_ET_CONN_ABRT)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004796 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_C,
4797 (s->txn->flags & TX_NOT_FIRST) ? NULL :
4798 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004799 else if (err_type & SI_ET_QUEUE_TO)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004800 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_Q,
4801 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004802 else if (err_type & SI_ET_QUEUE_ERR)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004803 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_Q,
4804 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004805 else if (err_type & SI_ET_CONN_TO)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004806 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_C,
4807 (s->txn->flags & TX_NOT_FIRST) ? NULL :
4808 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004809 else if (err_type & SI_ET_CONN_ERR)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004810 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_C,
4811 (s->flags & SF_SRV_REUSED) ? NULL :
4812 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004813 else if (err_type & SI_ET_CONN_RES)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004814 http_server_error(s, si, SF_ERR_RESOURCE, SF_FINST_C,
4815 (s->txn->flags & TX_NOT_FIRST) ? NULL :
4816 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004817 else { /* SI_ET_CONN_OTHER and others */
4818 s->txn->status = 500;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004819 http_server_error(s, si, SF_ERR_INTERNAL, SF_FINST_C,
4820 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004821 }
4822}
4823
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004824
Christopher Faulet4a28a532019-03-01 11:19:40 +01004825/* Handle Expect: 100-continue for HTTP/1.1 messages if necessary. It returns 0
4826 * on success and -1 on error.
4827 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004828static int http_handle_expect_hdr(struct stream *s, struct htx *htx, struct http_msg *msg)
Christopher Faulet4a28a532019-03-01 11:19:40 +01004829{
4830 /* If we have HTTP/1.1 message with a body and Expect: 100-continue,
4831 * then we must send an HTTP/1.1 100 Continue intermediate response.
4832 */
4833 if (msg->msg_state == HTTP_MSG_BODY && (msg->flags & HTTP_MSGF_VER_11) &&
4834 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
4835 struct ist hdr = { .ptr = "Expect", .len = 6 };
4836 struct http_hdr_ctx ctx;
4837
4838 ctx.blk = NULL;
4839 /* Expect is allowed in 1.1, look for it */
4840 if (http_find_header(htx, hdr, &ctx, 0) &&
4841 unlikely(isteqi(ctx.value, ist2("100-continue", 12)))) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004842 if (http_reply_100_continue(s) == -1)
Christopher Faulet4a28a532019-03-01 11:19:40 +01004843 return -1;
4844 http_remove_header(htx, &ctx);
4845 }
4846 }
4847 return 0;
4848}
4849
Christopher Faulet23a3c792018-11-28 10:01:23 +01004850/* Send a 100-Continue response to the client. It returns 0 on success and -1
4851 * on error. The response channel is updated accordingly.
4852 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004853static int http_reply_100_continue(struct stream *s)
Christopher Faulet23a3c792018-11-28 10:01:23 +01004854{
4855 struct channel *res = &s->res;
4856 struct htx *htx = htx_from_buf(&res->buf);
4857 struct htx_sl *sl;
4858 unsigned int flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|
4859 HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
Christopher Faulet23a3c792018-11-28 10:01:23 +01004860
4861 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
4862 ist("HTTP/1.1"), ist("100"), ist("Continue"));
4863 if (!sl)
4864 goto fail;
4865 sl->info.res.status = 100;
4866
Christopher Faulet1d5ec092019-06-26 14:23:54 +02004867 if (!htx_add_endof(htx, HTX_BLK_EOH))
Christopher Faulet23a3c792018-11-28 10:01:23 +01004868 goto fail;
4869
Christopher Fauleta72a7e42020-01-28 09:28:11 +01004870 if (!http_forward_proxy_resp(s, 0))
4871 goto fail;
Christopher Faulet23a3c792018-11-28 10:01:23 +01004872 return 0;
4873
4874 fail:
4875 /* If an error occurred, remove the incomplete HTTP response from the
4876 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004877 channel_htx_truncate(res, htx);
Christopher Faulet23a3c792018-11-28 10:01:23 +01004878 return -1;
4879}
4880
Christopher Faulet12c51e22018-11-28 15:59:42 +01004881
Christopher Faulet0f226952018-10-22 09:29:56 +02004882/*
4883 * Capture headers from message <htx> according to header list <cap_hdr>, and
4884 * fill the <cap> pointers appropriately.
4885 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004886static void http_capture_headers(struct htx *htx, char **cap, struct cap_hdr *cap_hdr)
Christopher Faulet0f226952018-10-22 09:29:56 +02004887{
4888 struct cap_hdr *h;
4889 int32_t pos;
4890
Christopher Fauleta3f15502019-05-13 15:27:23 +02004891 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet0f226952018-10-22 09:29:56 +02004892 struct htx_blk *blk = htx_get_blk(htx, pos);
4893 enum htx_blk_type type = htx_get_blk_type(blk);
4894 struct ist n, v;
4895
4896 if (type == HTX_BLK_EOH)
4897 break;
4898 if (type != HTX_BLK_HDR)
4899 continue;
4900
4901 n = htx_get_blk_name(htx, blk);
4902
4903 for (h = cap_hdr; h; h = h->next) {
4904 if (h->namelen && (h->namelen == n.len) &&
4905 (strncasecmp(n.ptr, h->name, h->namelen) == 0)) {
4906 if (cap[h->index] == NULL)
4907 cap[h->index] =
4908 pool_alloc(h->pool);
4909
4910 if (cap[h->index] == NULL) {
4911 ha_alert("HTTP capture : out of memory.\n");
4912 break;
4913 }
4914
4915 v = htx_get_blk_value(htx, blk);
4916 if (v.len > h->len)
4917 v.len = h->len;
4918
4919 memcpy(cap[h->index], v.ptr, v.len);
4920 cap[h->index][v.len]=0;
4921 }
4922 }
4923 }
4924}
4925
Christopher Faulet0b6bdc52018-10-24 11:05:36 +02004926/* Delete a value in a header between delimiters <from> and <next>. The header
4927 * itself is delimited by <start> and <end> pointers. The number of characters
4928 * displaced is returned, and the pointer to the first delimiter is updated if
4929 * required. The function tries as much as possible to respect the following
4930 * principles :
4931 * - replace <from> delimiter by the <next> one unless <from> points to <start>,
4932 * in which case <next> is simply removed
4933 * - set exactly one space character after the new first delimiter, unless there
4934 * are not enough characters in the block being moved to do so.
4935 * - remove unneeded spaces before the previous delimiter and after the new
4936 * one.
4937 *
4938 * It is the caller's responsibility to ensure that :
4939 * - <from> points to a valid delimiter or <start> ;
4940 * - <next> points to a valid delimiter or <end> ;
4941 * - there are non-space chars before <from>.
4942 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004943static int http_del_hdr_value(char *start, char *end, char **from, char *next)
Christopher Faulet0b6bdc52018-10-24 11:05:36 +02004944{
4945 char *prev = *from;
4946
4947 if (prev == start) {
4948 /* We're removing the first value. eat the semicolon, if <next>
4949 * is lower than <end> */
4950 if (next < end)
4951 next++;
4952
4953 while (next < end && HTTP_IS_SPHT(*next))
4954 next++;
4955 }
4956 else {
4957 /* Remove useless spaces before the old delimiter. */
4958 while (HTTP_IS_SPHT(*(prev-1)))
4959 prev--;
4960 *from = prev;
4961
4962 /* copy the delimiter and if possible a space if we're
4963 * not at the end of the line.
4964 */
4965 if (next < end) {
4966 *prev++ = *next++;
4967 if (prev + 1 < next)
4968 *prev++ = ' ';
4969 while (next < end && HTTP_IS_SPHT(*next))
4970 next++;
4971 }
4972 }
4973 memmove(prev, next, end - next);
4974 return (prev - next);
4975}
4976
Christopher Faulet0f226952018-10-22 09:29:56 +02004977
4978/* Formats the start line of the request (without CRLF) and puts it in <str> and
Joseph Herlantc42c0e92018-11-25 10:43:27 -08004979 * return the written length. The line can be truncated if it exceeds <len>.
Christopher Faulet0f226952018-10-22 09:29:56 +02004980 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004981static size_t http_fmt_req_line(const struct htx_sl *sl, char *str, size_t len)
Christopher Faulet0f226952018-10-22 09:29:56 +02004982{
4983 struct ist dst = ist2(str, 0);
4984
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004985 if (istcat(&dst, htx_sl_req_meth(sl), len) == -1)
Christopher Faulet0f226952018-10-22 09:29:56 +02004986 goto end;
4987 if (dst.len + 1 > len)
4988 goto end;
4989 dst.ptr[dst.len++] = ' ';
4990
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004991 if (istcat(&dst, htx_sl_req_uri(sl), len) == -1)
Christopher Faulet0f226952018-10-22 09:29:56 +02004992 goto end;
4993 if (dst.len + 1 > len)
4994 goto end;
4995 dst.ptr[dst.len++] = ' ';
4996
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004997 istcat(&dst, htx_sl_req_vsn(sl), len);
Christopher Faulet0f226952018-10-22 09:29:56 +02004998 end:
4999 return dst.len;
5000}
5001
5002/*
5003 * Print a debug line with a start line.
5004 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005005static void http_debug_stline(const char *dir, struct stream *s, const struct htx_sl *sl)
Christopher Faulet0f226952018-10-22 09:29:56 +02005006{
5007 struct session *sess = strm_sess(s);
5008 int max;
5009
5010 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
5011 dir,
5012 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->handle.fd : -1,
5013 objt_cs(s->si[1].end) ? (unsigned short)objt_cs(s->si[1].end)->conn->handle.fd : -1);
5014
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005015 max = HTX_SL_P1_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02005016 UBOUND(max, trash.size - trash.data - 3);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005017 chunk_memcat(&trash, HTX_SL_P1_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02005018 trash.area[trash.data++] = ' ';
5019
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005020 max = HTX_SL_P2_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02005021 UBOUND(max, trash.size - trash.data - 2);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005022 chunk_memcat(&trash, HTX_SL_P2_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02005023 trash.area[trash.data++] = ' ';
5024
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005025 max = HTX_SL_P3_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02005026 UBOUND(max, trash.size - trash.data - 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005027 chunk_memcat(&trash, HTX_SL_P3_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02005028 trash.area[trash.data++] = '\n';
5029
Willy Tarreau2e8ab6b2020-03-14 11:03:20 +01005030 DISGUISE(write(1, trash.area, trash.data));
Christopher Faulet0f226952018-10-22 09:29:56 +02005031}
5032
5033/*
5034 * Print a debug line with a header.
5035 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005036static 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 +02005037{
5038 struct session *sess = strm_sess(s);
5039 int max;
5040
5041 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
5042 dir,
5043 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->handle.fd : -1,
5044 objt_cs(s->si[1].end) ? (unsigned short)objt_cs(s->si[1].end)->conn->handle.fd : -1);
5045
5046 max = n.len;
5047 UBOUND(max, trash.size - trash.data - 3);
5048 chunk_memcat(&trash, n.ptr, max);
5049 trash.area[trash.data++] = ':';
5050 trash.area[trash.data++] = ' ';
5051
5052 max = v.len;
5053 UBOUND(max, trash.size - trash.data - 1);
5054 chunk_memcat(&trash, v.ptr, max);
5055 trash.area[trash.data++] = '\n';
5056
Willy Tarreau2e8ab6b2020-03-14 11:03:20 +01005057 DISGUISE(write(1, trash.area, trash.data));
Christopher Faulet0f226952018-10-22 09:29:56 +02005058}
5059
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005060/* Allocate a new HTTP transaction for stream <s> unless there is one already.
5061 * In case of allocation failure, everything allocated is freed and NULL is
5062 * returned. Otherwise the new transaction is assigned to the stream and
5063 * returned.
5064 */
5065struct http_txn *http_alloc_txn(struct stream *s)
5066{
5067 struct http_txn *txn = s->txn;
5068
5069 if (txn)
5070 return txn;
5071
5072 txn = pool_alloc(pool_head_http_txn);
5073 if (!txn)
5074 return txn;
5075
5076 s->txn = txn;
5077 return txn;
5078}
5079
5080void http_txn_reset_req(struct http_txn *txn)
5081{
Christopher Faulet1aea50e2020-01-17 16:03:53 +01005082 txn->req.flags = 0;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005083 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
5084}
5085
5086void http_txn_reset_res(struct http_txn *txn)
5087{
Christopher Faulet1aea50e2020-01-17 16:03:53 +01005088 txn->rsp.flags = 0;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005089 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
5090}
5091
5092/*
Christopher Faulet75f619a2021-03-08 19:12:58 +01005093 * Create and initialize a new HTTP transaction for stream <s>. This should be
5094 * used before processing any new request. It returns the transaction or NLULL
5095 * on error.
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005096 */
Christopher Faulet75f619a2021-03-08 19:12:58 +01005097struct http_txn *http_create_txn(struct stream *s)
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005098{
Christopher Faulet75f619a2021-03-08 19:12:58 +01005099 struct http_txn *txn;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005100 struct conn_stream *cs = objt_cs(s->si[0].end);
5101
Christopher Faulet75f619a2021-03-08 19:12:58 +01005102 txn = pool_alloc(pool_head_http_txn);
5103 if (!txn)
5104 return NULL;
5105 s->txn = txn;
5106
Christopher Fauletda831fa2020-10-06 17:58:43 +02005107 txn->flags = ((cs && cs->flags & CS_FL_NOT_FIRST) ? TX_NOT_FIRST : 0);
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005108 txn->status = -1;
Christopher Faulet5cb513a2020-05-13 17:56:56 +02005109 txn->http_reply = NULL;
Willy Tarreau8b507582020-02-25 09:35:07 +01005110 write_u32(txn->cache_hash, 0);
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005111
5112 txn->cookie_first_date = 0;
5113 txn->cookie_last_date = 0;
5114
5115 txn->srv_cookie = NULL;
5116 txn->cli_cookie = NULL;
5117 txn->uri = NULL;
5118
5119 http_txn_reset_req(txn);
5120 http_txn_reset_res(txn);
5121
5122 txn->req.chn = &s->req;
5123 txn->rsp.chn = &s->res;
5124
5125 txn->auth.method = HTTP_AUTH_UNKNOWN;
5126
5127 vars_init(&s->vars_txn, SCOPE_TXN);
5128 vars_init(&s->vars_reqres, SCOPE_REQ);
Christopher Faulet75f619a2021-03-08 19:12:58 +01005129
5130 return txn;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005131}
5132
5133/* to be used at the end of a transaction */
Christopher Faulet75f619a2021-03-08 19:12:58 +01005134void http_destroy_txn(struct stream *s)
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005135{
5136 struct http_txn *txn = s->txn;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005137
5138 /* these ones will have been dynamically allocated */
5139 pool_free(pool_head_requri, txn->uri);
5140 pool_free(pool_head_capture, txn->cli_cookie);
5141 pool_free(pool_head_capture, txn->srv_cookie);
Tim Duesterhusa17e6622020-03-05 20:19:02 +01005142 pool_free(pool_head_uniqueid, s->unique_id.ptr);
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005143
Tim Duesterhusa17e6622020-03-05 20:19:02 +01005144 s->unique_id = IST_NULL;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005145 txn->uri = NULL;
5146 txn->srv_cookie = NULL;
5147 txn->cli_cookie = NULL;
5148
Christopher Faulet59399252019-11-07 14:27:52 +01005149 if (!LIST_ISEMPTY(&s->vars_txn.head))
5150 vars_prune(&s->vars_txn, s->sess, s);
5151 if (!LIST_ISEMPTY(&s->vars_reqres.head))
5152 vars_prune(&s->vars_reqres, s->sess, s);
Christopher Faulet75f619a2021-03-08 19:12:58 +01005153
5154 pool_free(pool_head_http_txn, txn);
5155 s->txn = NULL;
Christopher Faulet59399252019-11-07 14:27:52 +01005156}
5157
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005158
5159DECLARE_POOL(pool_head_http_txn, "http_txn", sizeof(struct http_txn));
Christopher Faulet0f226952018-10-22 09:29:56 +02005160
Christopher Fauletf4eb75d2018-10-11 15:55:07 +02005161__attribute__((constructor))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005162static void __http_protocol_init(void)
Christopher Fauletf4eb75d2018-10-11 15:55:07 +02005163{
5164}
5165
5166
5167/*
5168 * Local variables:
5169 * c-indent-level: 8
5170 * c-basic-offset: 8
5171 * End:
5172 */