blob: 096d78d868df2b3658ad4e5ce37fde7ce3d9049a [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);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002050 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02002051 _HA_ATOMIC_INC(&__objt_server(s->target)->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 Faulet99daf282018-11-28 22:58:13 +01002574
Christopher Faulet60b33a52020-01-28 09:18:10 +01002575 if (s->sess->fe == s->be) /* report it if the request was intercepted by the frontend */
Willy Tarreau4781b152021-04-06 13:53:36 +02002576 _HA_ATOMIC_INC(&s->sess->fe->fe_counters.intercepted_req);
Christopher Faulet60b33a52020-01-28 09:18:10 +01002577 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002578
2579 if (!(s->flags & SF_ERR_MASK))
2580 s->flags |= SF_ERR_LOCAL;
2581 if (!(s->flags & SF_FINST_MASK))
Christopher Faulet60b33a52020-01-28 09:18:10 +01002582 s->flags |= ((rule->flags & REDIRECT_FLAG_FROM_REQ) ? SF_FINST_R : SF_FINST_H);
Christopher Fauletf2824e62018-10-01 12:12:37 +02002583
Christopher Faulet99daf282018-11-28 22:58:13 +01002584 free_trash_chunk(chunk);
2585 return 1;
2586
2587 fail:
2588 /* If an error occurred, remove the incomplete HTTP response from the
2589 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01002590 channel_htx_truncate(res, htxbuf(&res->buf));
Christopher Fauletf2824e62018-10-01 12:12:37 +02002591 free_trash_chunk(chunk);
Christopher Faulet99daf282018-11-28 22:58:13 +01002592 return 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002593}
2594
Christopher Faulet92d34fe2019-12-17 09:20:34 +01002595/* Replace all headers matching the name <name>. The header value is replaced if
2596 * it matches the regex <re>. <str> is used for the replacement. If <full> is
2597 * set to 1, the full-line is matched and replaced. Otherwise, comma-separated
2598 * values are evaluated one by one. It returns 0 on success and -1 on error.
2599 */
2600int http_replace_hdrs(struct stream* s, struct htx *htx, struct ist name,
2601 const char *str, struct my_regex *re, int full)
Christopher Faulet72333522018-10-24 11:25:02 +02002602{
2603 struct http_hdr_ctx ctx;
2604 struct buffer *output = get_trash_chunk();
2605
Christopher Faulet72333522018-10-24 11:25:02 +02002606 ctx.blk = NULL;
Christopher Faulet92d34fe2019-12-17 09:20:34 +01002607 while (http_find_header(htx, name, &ctx, full)) {
Christopher Faulet72333522018-10-24 11:25:02 +02002608 if (!regex_exec_match2(re, ctx.value.ptr, ctx.value.len, MAX_MATCH, pmatch, 0))
2609 continue;
2610
2611 output->data = exp_replace(output->area, output->size, ctx.value.ptr, str, pmatch);
2612 if (output->data == -1)
2613 return -1;
2614 if (!http_replace_header_value(htx, &ctx, ist2(output->area, output->data)))
2615 return -1;
2616 }
2617 return 0;
2618}
2619
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002620/* This function executes one of the set-{method,path,query,uri} actions. It
2621 * takes the string from the variable 'replace' with length 'len', then modifies
2622 * the relevant part of the request line accordingly. Then it updates various
2623 * pointers to the next elements which were moved, and the total buffer length.
2624 * It finds the action to be performed in p[2], previously filled by function
2625 * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal
2626 * error, though this can be revisited when this code is finally exploited.
2627 *
2628 * 'action' can be '0' to replace method, '1' to replace path, '2' to replace
Christopher Faulet312294f2020-09-02 17:17:44 +02002629 * query string, 3 to replace uri or 4 to replace the path+query.
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002630 *
2631 * In query string case, the mark question '?' must be set at the start of the
2632 * string by the caller, event if the replacement query string is empty.
2633 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002634int http_req_replace_stline(int action, const char *replace, int len,
2635 struct proxy *px, struct stream *s)
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002636{
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002637 struct htx *htx = htxbuf(&s->req.buf);
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002638
2639 switch (action) {
2640 case 0: // method
2641 if (!http_replace_req_meth(htx, ist2(replace, len)))
2642 return -1;
2643 break;
2644
2645 case 1: // path
Christopher Fauletb8ce5052020-08-31 16:11:57 +02002646 if (!http_replace_req_path(htx, ist2(replace, len), 0))
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002647 return -1;
2648 break;
2649
2650 case 2: // query
2651 if (!http_replace_req_query(htx, ist2(replace, len)))
2652 return -1;
2653 break;
2654
2655 case 3: // uri
2656 if (!http_replace_req_uri(htx, ist2(replace, len)))
2657 return -1;
2658 break;
2659
Christopher Faulet312294f2020-09-02 17:17:44 +02002660 case 4: // path + query
2661 if (!http_replace_req_path(htx, ist2(replace, len), 1))
2662 return -1;
2663 break;
2664
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002665 default:
2666 return -1;
2667 }
2668 return 0;
2669}
2670
2671/* This function replace the HTTP status code and the associated message. The
Christopher Faulete00d06c2019-12-16 17:18:42 +01002672 * variable <status> contains the new status code. This function never fails. It
2673 * returns 0 in case of success, -1 in case of internal error.
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002674 */
Christopher Faulet96bff762019-12-17 13:46:18 +01002675int http_res_set_status(unsigned int status, struct ist reason, struct stream *s)
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002676{
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002677 struct htx *htx = htxbuf(&s->res.buf);
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002678 char *res;
2679
2680 chunk_reset(&trash);
2681 res = ultoa_o(status, trash.area, trash.size);
2682 trash.data = res - trash.area;
2683
2684 /* Do we have a custom reason format string? */
Tim Duesterhuse296d3e2020-03-05 17:56:31 +01002685 if (!isttest(reason)) {
Christopher Faulet96bff762019-12-17 13:46:18 +01002686 const char *str = http_get_reason(status);
Tim Duesterhusdcf753a2021-03-04 17:31:47 +01002687 reason = ist(str);
Christopher Faulet96bff762019-12-17 13:46:18 +01002688 }
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002689
Christopher Fauletbde2c4c2020-08-31 16:43:34 +02002690 if (!http_replace_res_status(htx, ist2(trash.area, trash.data), reason))
Christopher Faulete00d06c2019-12-16 17:18:42 +01002691 return -1;
2692 return 0;
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002693}
2694
Christopher Faulet3e964192018-10-24 11:39:23 +02002695/* Executes the http-request rules <rules> for stream <s>, proxy <px> and
2696 * transaction <txn>. Returns the verdict of the first rule that prevents
2697 * further processing of the request (auth, deny, ...), and defaults to
2698 * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or
2699 * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT
2700 * on txn->flags if it encounters a tarpit rule. If <deny_status> is not NULL
2701 * and a deny/tarpit rule is matched, it will be filled with this rule's deny
2702 * status.
2703 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002704static enum rule_result http_req_get_intercept_rule(struct proxy *px, struct list *rules,
Christopher Fauletb58f62b2020-01-13 16:40:13 +01002705 struct stream *s)
Christopher Faulet3e964192018-10-24 11:39:23 +02002706{
2707 struct session *sess = strm_sess(s);
2708 struct http_txn *txn = s->txn;
Christopher Faulet3e964192018-10-24 11:39:23 +02002709 struct act_rule *rule;
Christopher Faulet3e964192018-10-24 11:39:23 +02002710 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002711 int act_opts = 0;
Christopher Faulet3e964192018-10-24 11:39:23 +02002712
Christopher Faulet3e964192018-10-24 11:39:23 +02002713 /* If "the current_rule_list" match the executed rule list, we are in
2714 * resume condition. If a resume is needed it is always in the action
2715 * and never in the ACL or converters. In this case, we initialise the
2716 * current rule, and go to the action execution point.
2717 */
2718 if (s->current_rule) {
2719 rule = s->current_rule;
2720 s->current_rule = NULL;
2721 if (s->current_rule_list == rules)
2722 goto resume_execution;
2723 }
2724 s->current_rule_list = rules;
2725
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002726 /* start the ruleset evaluation in strict mode */
2727 txn->req.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01002728
Christopher Faulet3e964192018-10-24 11:39:23 +02002729 list_for_each_entry(rule, rules, list) {
2730 /* check optional condition */
2731 if (rule->cond) {
2732 int ret;
2733
2734 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
2735 ret = acl_pass(ret);
2736
2737 if (rule->cond->pol == ACL_COND_UNLESS)
2738 ret = !ret;
2739
2740 if (!ret) /* condition not matched */
2741 continue;
2742 }
2743
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002744 act_opts |= ACT_OPT_FIRST;
Christopher Faulet3e964192018-10-24 11:39:23 +02002745 resume_execution:
Amaury Denoyelle03517732021-05-07 14:25:01 +02002746 if (rule->kw->flags & KWF_EXPERIMENTAL)
2747 mark_tainted(TAINTED_ACTION_EXP_EXECUTED);
2748
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002749 /* Always call the action function if defined */
2750 if (rule->action_ptr) {
2751 if ((s->req.flags & CF_READ_ERROR) ||
2752 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
2753 (px->options & PR_O_ABRT_CLOSE)))
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002754 act_opts |= ACT_OPT_FINAL;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002755
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002756 switch (rule->action_ptr(rule, px, sess, s, act_opts)) {
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002757 case ACT_RET_CONT:
2758 break;
2759 case ACT_RET_STOP:
2760 rule_ret = HTTP_RULE_RES_STOP;
2761 goto end;
2762 case ACT_RET_YIELD:
2763 s->current_rule = rule;
2764 rule_ret = HTTP_RULE_RES_YIELD;
2765 goto end;
2766 case ACT_RET_ERR:
2767 rule_ret = HTTP_RULE_RES_ERROR;
2768 goto end;
2769 case ACT_RET_DONE:
2770 rule_ret = HTTP_RULE_RES_DONE;
2771 goto end;
2772 case ACT_RET_DENY:
Christopher Fauletb58f62b2020-01-13 16:40:13 +01002773 if (txn->status == -1)
2774 txn->status = 403;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002775 rule_ret = HTTP_RULE_RES_DENY;
2776 goto end;
2777 case ACT_RET_ABRT:
2778 rule_ret = HTTP_RULE_RES_ABRT;
2779 goto end;
2780 case ACT_RET_INV:
2781 rule_ret = HTTP_RULE_RES_BADREQ;
2782 goto end;
2783 }
2784 continue; /* eval the next rule */
2785 }
2786
2787 /* If not action function defined, check for known actions */
Christopher Faulet3e964192018-10-24 11:39:23 +02002788 switch (rule->action) {
2789 case ACT_ACTION_ALLOW:
2790 rule_ret = HTTP_RULE_RES_STOP;
2791 goto end;
2792
2793 case ACT_ACTION_DENY:
Christopher Faulet5cb513a2020-05-13 17:56:56 +02002794 txn->status = rule->arg.http_reply->status;
2795 txn->http_reply = rule->arg.http_reply;
Christopher Faulet3e964192018-10-24 11:39:23 +02002796 rule_ret = HTTP_RULE_RES_DENY;
2797 goto end;
2798
2799 case ACT_HTTP_REQ_TARPIT:
2800 txn->flags |= TX_CLTARPIT;
Christopher Faulet5cb513a2020-05-13 17:56:56 +02002801 txn->status = rule->arg.http_reply->status;
2802 txn->http_reply = rule->arg.http_reply;
Christopher Faulet3e964192018-10-24 11:39:23 +02002803 rule_ret = HTTP_RULE_RES_DENY;
2804 goto end;
2805
Christopher Faulet3e964192018-10-24 11:39:23 +02002806 case ACT_HTTP_REDIR:
Christopher Faulet90d22a82020-03-06 11:18:39 +01002807 rule_ret = HTTP_RULE_RES_ABRT;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002808 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
Christopher Faulet3a26bee2019-12-16 12:47:40 +01002809 rule_ret = HTTP_RULE_RES_ERROR;
Christopher Faulet3e964192018-10-24 11:39:23 +02002810 goto end;
2811
2812 case ACT_HTTP_SET_NICE:
Christopher Faulet96bff762019-12-17 13:46:18 +01002813 s->task->nice = rule->arg.http.i;
Christopher Faulet3e964192018-10-24 11:39:23 +02002814 break;
2815
2816 case ACT_HTTP_SET_TOS:
Christopher Faulet96bff762019-12-17 13:46:18 +01002817 conn_set_tos(objt_conn(sess->origin), rule->arg.http.i);
Christopher Faulet3e964192018-10-24 11:39:23 +02002818 break;
2819
2820 case ACT_HTTP_SET_MARK:
Christopher Faulet96bff762019-12-17 13:46:18 +01002821 conn_set_mark(objt_conn(sess->origin), rule->arg.http.i);
Christopher Faulet3e964192018-10-24 11:39:23 +02002822 break;
2823
2824 case ACT_HTTP_SET_LOGL:
Christopher Faulet96bff762019-12-17 13:46:18 +01002825 s->logs.level = rule->arg.http.i;
Christopher Faulet3e964192018-10-24 11:39:23 +02002826 break;
2827
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002828 /* other flags exists, but normally, they never be matched. */
Christopher Faulet3e964192018-10-24 11:39:23 +02002829 default:
2830 break;
2831 }
2832 }
2833
2834 end:
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002835 /* if the ruleset evaluation is finished reset the strict mode */
Christopher Faulet46f95542019-12-20 10:07:22 +01002836 if (rule_ret != HTTP_RULE_RES_YIELD)
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002837 txn->req.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01002838
Christopher Faulet3e964192018-10-24 11:39:23 +02002839 /* we reached the end of the rules, nothing to report */
2840 return rule_ret;
2841}
2842
2843/* Executes the http-response rules <rules> for stream <s> and proxy <px>. It
2844 * returns one of 5 possible statuses: HTTP_RULE_RES_CONT, HTTP_RULE_RES_STOP,
2845 * HTTP_RULE_RES_DONE, HTTP_RULE_RES_YIELD, or HTTP_RULE_RES_BADREQ. If *CONT
2846 * is returned, the process can continue the evaluation of next rule list. If
2847 * *STOP or *DONE is returned, the process must stop the evaluation. If *BADREQ
2848 * is returned, it means the operation could not be processed and a server error
Christopher Fauleta53abad2020-05-13 08:12:22 +02002849 * must be returned. If *YIELD is returned, the caller must call again the
2850 * function with the same context.
Christopher Faulet3e964192018-10-24 11:39:23 +02002851 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002852static enum rule_result http_res_get_intercept_rule(struct proxy *px, struct list *rules,
2853 struct stream *s)
Christopher Faulet3e964192018-10-24 11:39:23 +02002854{
2855 struct session *sess = strm_sess(s);
2856 struct http_txn *txn = s->txn;
Christopher Faulet3e964192018-10-24 11:39:23 +02002857 struct act_rule *rule;
Christopher Faulet3e964192018-10-24 11:39:23 +02002858 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002859 int act_opts = 0;
Christopher Faulet3e964192018-10-24 11:39:23 +02002860
Christopher Faulet3e964192018-10-24 11:39:23 +02002861 /* If "the current_rule_list" match the executed rule list, we are in
2862 * resume condition. If a resume is needed it is always in the action
2863 * and never in the ACL or converters. In this case, we initialise the
2864 * current rule, and go to the action execution point.
2865 */
2866 if (s->current_rule) {
2867 rule = s->current_rule;
2868 s->current_rule = NULL;
2869 if (s->current_rule_list == rules)
2870 goto resume_execution;
2871 }
2872 s->current_rule_list = rules;
2873
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002874 /* start the ruleset evaluation in strict mode */
2875 txn->rsp.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01002876
Christopher Faulet3e964192018-10-24 11:39:23 +02002877 list_for_each_entry(rule, rules, list) {
2878 /* check optional condition */
2879 if (rule->cond) {
2880 int ret;
2881
2882 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
2883 ret = acl_pass(ret);
2884
2885 if (rule->cond->pol == ACL_COND_UNLESS)
2886 ret = !ret;
2887
2888 if (!ret) /* condition not matched */
2889 continue;
2890 }
2891
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002892 act_opts |= ACT_OPT_FIRST;
Christopher Faulet3e964192018-10-24 11:39:23 +02002893resume_execution:
Amaury Denoyelle03517732021-05-07 14:25:01 +02002894 if (rule->kw->flags & KWF_EXPERIMENTAL)
2895 mark_tainted(TAINTED_ACTION_EXP_EXECUTED);
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002896
2897 /* Always call the action function if defined */
2898 if (rule->action_ptr) {
2899 if ((s->req.flags & CF_READ_ERROR) ||
2900 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
2901 (px->options & PR_O_ABRT_CLOSE)))
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002902 act_opts |= ACT_OPT_FINAL;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002903
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002904 switch (rule->action_ptr(rule, px, sess, s, act_opts)) {
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002905 case ACT_RET_CONT:
2906 break;
2907 case ACT_RET_STOP:
2908 rule_ret = HTTP_RULE_RES_STOP;
2909 goto end;
2910 case ACT_RET_YIELD:
2911 s->current_rule = rule;
2912 rule_ret = HTTP_RULE_RES_YIELD;
2913 goto end;
2914 case ACT_RET_ERR:
2915 rule_ret = HTTP_RULE_RES_ERROR;
2916 goto end;
2917 case ACT_RET_DONE:
2918 rule_ret = HTTP_RULE_RES_DONE;
2919 goto end;
2920 case ACT_RET_DENY:
Christopher Fauletb58f62b2020-01-13 16:40:13 +01002921 if (txn->status == -1)
2922 txn->status = 502;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002923 rule_ret = HTTP_RULE_RES_DENY;
2924 goto end;
2925 case ACT_RET_ABRT:
2926 rule_ret = HTTP_RULE_RES_ABRT;
2927 goto end;
2928 case ACT_RET_INV:
2929 rule_ret = HTTP_RULE_RES_BADREQ;
2930 goto end;
2931 }
2932 continue; /* eval the next rule */
2933 }
2934
2935 /* If not action function defined, check for known actions */
Christopher Faulet3e964192018-10-24 11:39:23 +02002936 switch (rule->action) {
2937 case ACT_ACTION_ALLOW:
2938 rule_ret = HTTP_RULE_RES_STOP; /* "allow" rules are OK */
2939 goto end;
2940
2941 case ACT_ACTION_DENY:
Christopher Faulet5cb513a2020-05-13 17:56:56 +02002942 txn->status = rule->arg.http_reply->status;
2943 txn->http_reply = rule->arg.http_reply;
Christopher Faulet3a26bee2019-12-16 12:47:40 +01002944 rule_ret = HTTP_RULE_RES_DENY;
Christopher Faulet3e964192018-10-24 11:39:23 +02002945 goto end;
2946
2947 case ACT_HTTP_SET_NICE:
Christopher Faulet96bff762019-12-17 13:46:18 +01002948 s->task->nice = rule->arg.http.i;
Christopher Faulet3e964192018-10-24 11:39:23 +02002949 break;
2950
2951 case ACT_HTTP_SET_TOS:
Christopher Faulet96bff762019-12-17 13:46:18 +01002952 conn_set_tos(objt_conn(sess->origin), rule->arg.http.i);
Christopher Faulet3e964192018-10-24 11:39:23 +02002953 break;
2954
2955 case ACT_HTTP_SET_MARK:
Christopher Faulet96bff762019-12-17 13:46:18 +01002956 conn_set_mark(objt_conn(sess->origin), rule->arg.http.i);
Christopher Faulet3e964192018-10-24 11:39:23 +02002957 break;
2958
2959 case ACT_HTTP_SET_LOGL:
Christopher Faulet96bff762019-12-17 13:46:18 +01002960 s->logs.level = rule->arg.http.i;
Christopher Faulet3e964192018-10-24 11:39:23 +02002961 break;
2962
Christopher Faulet3e964192018-10-24 11:39:23 +02002963 case ACT_HTTP_REDIR:
Christopher Faulet49c2a702020-03-06 15:44:37 +01002964 rule_ret = HTTP_RULE_RES_ABRT;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002965 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
Christopher Faulet3a26bee2019-12-16 12:47:40 +01002966 rule_ret = HTTP_RULE_RES_ERROR;
Christopher Faulet3e964192018-10-24 11:39:23 +02002967 goto end;
2968
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002969 /* other flags exists, but normally, they never be matched. */
Christopher Faulet3e964192018-10-24 11:39:23 +02002970 default:
2971 break;
2972 }
2973 }
2974
2975 end:
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002976 /* if the ruleset evaluation is finished reset the strict mode */
Christopher Faulet46f95542019-12-20 10:07:22 +01002977 if (rule_ret != HTTP_RULE_RES_YIELD)
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002978 txn->rsp.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01002979
Christopher Faulet3e964192018-10-24 11:39:23 +02002980 /* we reached the end of the rules, nothing to report */
2981 return rule_ret;
2982}
2983
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002984/* Executes backend and frontend http-after-response rules for the stream <s>,
2985 * in that order. it return 1 on success and 0 on error. It is the caller
2986 * responsibility to catch error or ignore it. If it catches it, this function
2987 * may be called a second time, for the internal error.
2988 */
2989int http_eval_after_res_rules(struct stream *s)
2990{
2991 struct session *sess = s->sess;
2992 enum rule_result ret = HTTP_RULE_RES_CONT;
2993
Christopher Faulet507479b2020-05-15 12:29:46 +02002994 /* Eval after-response ruleset only if the reply is not const */
2995 if (s->txn->flags & TX_CONST_REPLY)
2996 goto end;
2997
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002998 /* prune the request variables if not already done and swap to the response variables. */
2999 if (s->vars_reqres.scope != SCOPE_RES) {
3000 if (!LIST_ISEMPTY(&s->vars_reqres.head))
3001 vars_prune(&s->vars_reqres, s->sess, s);
3002 vars_init(&s->vars_reqres, SCOPE_RES);
3003 }
3004
3005 ret = http_res_get_intercept_rule(s->be, &s->be->http_after_res_rules, s);
3006 if ((ret == HTTP_RULE_RES_CONT || ret == HTTP_RULE_RES_STOP) && sess->fe != s->be)
3007 ret = http_res_get_intercept_rule(sess->fe, &sess->fe->http_after_res_rules, s);
3008
Christopher Faulet507479b2020-05-15 12:29:46 +02003009 end:
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01003010 /* All other codes than CONTINUE, STOP or DONE are forbidden */
3011 return (ret == HTTP_RULE_RES_CONT || ret == HTTP_RULE_RES_STOP || ret == HTTP_RULE_RES_DONE);
3012}
3013
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003014/*
3015 * Manage client-side cookie. It can impact performance by about 2% so it is
3016 * desirable to call it only when needed. This code is quite complex because
3017 * of the multiple very crappy and ambiguous syntaxes we have to support. it
3018 * highly recommended not to touch this part without a good reason !
3019 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003020static void http_manage_client_side_cookies(struct stream *s, struct channel *req)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003021{
3022 struct session *sess = s->sess;
3023 struct http_txn *txn = s->txn;
3024 struct htx *htx;
3025 struct http_hdr_ctx ctx;
3026 char *hdr_beg, *hdr_end, *del_from;
3027 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
3028 int preserve_hdr;
3029
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003030 htx = htxbuf(&req->buf);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003031 ctx.blk = NULL;
3032 while (http_find_header(htx, ist("Cookie"), &ctx, 1)) {
Olivier Houchardf0f42382019-07-22 17:43:46 +02003033 int is_first = 1;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003034 del_from = NULL; /* nothing to be deleted */
3035 preserve_hdr = 0; /* assume we may kill the whole header */
3036
3037 /* Now look for cookies. Conforming to RFC2109, we have to support
3038 * attributes whose name begin with a '$', and associate them with
3039 * the right cookie, if we want to delete this cookie.
3040 * So there are 3 cases for each cookie read :
3041 * 1) it's a special attribute, beginning with a '$' : ignore it.
3042 * 2) it's a server id cookie that we *MAY* want to delete : save
3043 * some pointers on it (last semi-colon, beginning of cookie...)
3044 * 3) it's an application cookie : we *MAY* have to delete a previous
3045 * "special" cookie.
3046 * At the end of loop, if a "special" cookie remains, we may have to
3047 * remove it. If no application cookie persists in the header, we
3048 * *MUST* delete it.
3049 *
3050 * Note: RFC2965 is unclear about the processing of spaces around
3051 * the equal sign in the ATTR=VALUE form. A careful inspection of
3052 * the RFC explicitly allows spaces before it, and not within the
3053 * tokens (attrs or values). An inspection of RFC2109 allows that
3054 * too but section 10.1.3 lets one think that spaces may be allowed
3055 * after the equal sign too, resulting in some (rare) buggy
3056 * implementations trying to do that. So let's do what servers do.
3057 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
3058 * allowed quoted strings in values, with any possible character
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003059 * after a backslash, including control chars and delimiters, which
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003060 * causes parsing to become ambiguous. Browsers also allow spaces
3061 * within values even without quotes.
3062 *
3063 * We have to keep multiple pointers in order to support cookie
3064 * removal at the beginning, middle or end of header without
3065 * corrupting the header. All of these headers are valid :
3066 *
3067 * hdr_beg hdr_end
3068 * | |
3069 * v |
3070 * NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3 |
3071 * NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3 v
3072 * NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3
3073 * | | | | | | |
3074 * | | | | | | |
3075 * | | | | | | +--> next
3076 * | | | | | +----> val_end
3077 * | | | | +-----------> val_beg
3078 * | | | +--------------> equal
3079 * | | +----------------> att_end
3080 * | +---------------------> att_beg
3081 * +--------------------------> prev
3082 *
3083 */
3084 hdr_beg = ctx.value.ptr;
3085 hdr_end = hdr_beg + ctx.value.len;
3086 for (prev = hdr_beg; prev < hdr_end; prev = next) {
3087 /* Iterate through all cookies on this line */
3088
3089 /* find att_beg */
3090 att_beg = prev;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003091 if (!is_first)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003092 att_beg++;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003093 is_first = 0;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003094
3095 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
3096 att_beg++;
3097
3098 /* find att_end : this is the first character after the last non
3099 * space before the equal. It may be equal to hdr_end.
3100 */
3101 equal = att_end = att_beg;
3102 while (equal < hdr_end) {
3103 if (*equal == '=' || *equal == ',' || *equal == ';')
3104 break;
3105 if (HTTP_IS_SPHT(*equal++))
3106 continue;
3107 att_end = equal;
3108 }
3109
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003110 /* here, <equal> points to '=', a delimiter or the end. <att_end>
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003111 * is between <att_beg> and <equal>, both may be identical.
3112 */
3113 /* look for end of cookie if there is an equal sign */
3114 if (equal < hdr_end && *equal == '=') {
3115 /* look for the beginning of the value */
3116 val_beg = equal + 1;
3117 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
3118 val_beg++;
3119
3120 /* find the end of the value, respecting quotes */
3121 next = http_find_cookie_value_end(val_beg, hdr_end);
3122
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003123 /* make val_end point to the first white space or delimiter after the value */
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003124 val_end = next;
3125 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
3126 val_end--;
3127 }
3128 else
3129 val_beg = val_end = next = equal;
3130
3131 /* We have nothing to do with attributes beginning with
3132 * '$'. However, they will automatically be removed if a
3133 * header before them is removed, since they're supposed
3134 * to be linked together.
3135 */
3136 if (*att_beg == '$')
3137 continue;
3138
3139 /* Ignore cookies with no equal sign */
3140 if (equal == next) {
3141 /* This is not our cookie, so we must preserve it. But if we already
3142 * scheduled another cookie for removal, we cannot remove the
3143 * complete header, but we can remove the previous block itself.
3144 */
3145 preserve_hdr = 1;
3146 if (del_from != NULL) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003147 int delta = http_del_hdr_value(hdr_beg, hdr_end, &del_from, prev);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003148 val_end += delta;
3149 next += delta;
3150 hdr_end += delta;
3151 prev = del_from;
3152 del_from = NULL;
3153 }
3154 continue;
3155 }
3156
3157 /* if there are spaces around the equal sign, we need to
3158 * strip them otherwise we'll get trouble for cookie captures,
3159 * or even for rewrites. Since this happens extremely rarely,
3160 * it does not hurt performance.
3161 */
3162 if (unlikely(att_end != equal || val_beg > equal + 1)) {
3163 int stripped_before = 0;
3164 int stripped_after = 0;
3165
3166 if (att_end != equal) {
3167 memmove(att_end, equal, hdr_end - equal);
3168 stripped_before = (att_end - equal);
3169 equal += stripped_before;
3170 val_beg += stripped_before;
3171 }
3172
3173 if (val_beg > equal + 1) {
3174 memmove(equal + 1, val_beg, hdr_end + stripped_before - val_beg);
3175 stripped_after = (equal + 1) - val_beg;
3176 val_beg += stripped_after;
3177 stripped_before += stripped_after;
3178 }
3179
3180 val_end += stripped_before;
3181 next += stripped_before;
3182 hdr_end += stripped_before;
3183 }
3184 /* now everything is as on the diagram above */
3185
3186 /* First, let's see if we want to capture this cookie. We check
3187 * that we don't already have a client side cookie, because we
3188 * can only capture one. Also as an optimisation, we ignore
3189 * cookies shorter than the declared name.
3190 */
3191 if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL &&
3192 (val_end - att_beg >= sess->fe->capture_namelen) &&
3193 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
3194 int log_len = val_end - att_beg;
3195
3196 if ((txn->cli_cookie = pool_alloc(pool_head_capture)) == NULL) {
3197 ha_alert("HTTP logging : out of memory.\n");
3198 } else {
3199 if (log_len > sess->fe->capture_len)
3200 log_len = sess->fe->capture_len;
3201 memcpy(txn->cli_cookie, att_beg, log_len);
3202 txn->cli_cookie[log_len] = 0;
3203 }
3204 }
3205
3206 /* Persistence cookies in passive, rewrite or insert mode have the
3207 * following form :
3208 *
3209 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
3210 *
3211 * For cookies in prefix mode, the form is :
3212 *
3213 * Cookie: NAME=SRV~VALUE
3214 */
3215 if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
3216 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
3217 struct server *srv = s->be->srv;
3218 char *delim;
3219
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003220 /* if we're in cookie prefix mode, we'll search the delimiter so that we
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003221 * have the server ID between val_beg and delim, and the original cookie between
3222 * delim+1 and val_end. Otherwise, delim==val_end :
3223 *
3224 * hdr_beg
3225 * |
3226 * v
3227 * NAME=SRV; # in all but prefix modes
3228 * NAME=SRV~OPAQUE ; # in prefix mode
3229 * || || | |+-> next
3230 * || || | +--> val_end
3231 * || || +---------> delim
3232 * || |+------------> val_beg
3233 * || +-------------> att_end = equal
3234 * |+-----------------> att_beg
3235 * +------------------> prev
3236 *
3237 */
3238 if (s->be->ck_opts & PR_CK_PFX) {
3239 for (delim = val_beg; delim < val_end; delim++)
3240 if (*delim == COOKIE_DELIM)
3241 break;
3242 }
3243 else {
3244 char *vbar1;
3245 delim = val_end;
3246 /* Now check if the cookie contains a date field, which would
3247 * appear after a vertical bar ('|') just after the server name
3248 * and before the delimiter.
3249 */
3250 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
3251 if (vbar1) {
3252 /* OK, so left of the bar is the server's cookie and
3253 * right is the last seen date. It is a base64 encoded
3254 * 30-bit value representing the UNIX date since the
3255 * epoch in 4-second quantities.
3256 */
3257 int val;
3258 delim = vbar1++;
3259 if (val_end - vbar1 >= 5) {
3260 val = b64tos30(vbar1);
3261 if (val > 0)
3262 txn->cookie_last_date = val << 2;
3263 }
3264 /* look for a second vertical bar */
3265 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
3266 if (vbar1 && (val_end - vbar1 > 5)) {
3267 val = b64tos30(vbar1 + 1);
3268 if (val > 0)
3269 txn->cookie_first_date = val << 2;
3270 }
3271 }
3272 }
3273
3274 /* if the cookie has an expiration date and the proxy wants to check
3275 * it, then we do that now. We first check if the cookie is too old,
3276 * then only if it has expired. We detect strict overflow because the
3277 * time resolution here is not great (4 seconds). Cookies with dates
3278 * in the future are ignored if their offset is beyond one day. This
3279 * allows an admin to fix timezone issues without expiring everyone
3280 * and at the same time avoids keeping unwanted side effects for too
3281 * long.
3282 */
3283 if (txn->cookie_first_date && s->be->cookie_maxlife &&
3284 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) ||
3285 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
3286 txn->flags &= ~TX_CK_MASK;
3287 txn->flags |= TX_CK_OLD;
3288 delim = val_beg; // let's pretend we have not found the cookie
3289 txn->cookie_first_date = 0;
3290 txn->cookie_last_date = 0;
3291 }
3292 else if (txn->cookie_last_date && s->be->cookie_maxidle &&
3293 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) ||
3294 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
3295 txn->flags &= ~TX_CK_MASK;
3296 txn->flags |= TX_CK_EXPIRED;
3297 delim = val_beg; // let's pretend we have not found the cookie
3298 txn->cookie_first_date = 0;
3299 txn->cookie_last_date = 0;
3300 }
3301
3302 /* Here, we'll look for the first running server which supports the cookie.
3303 * This allows to share a same cookie between several servers, for example
3304 * to dedicate backup servers to specific servers only.
3305 * However, to prevent clients from sticking to cookie-less backup server
3306 * when they have incidentely learned an empty cookie, we simply ignore
3307 * empty cookies and mark them as invalid.
3308 * The same behaviour is applied when persistence must be ignored.
3309 */
3310 if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
3311 srv = NULL;
3312
3313 while (srv) {
3314 if (srv->cookie && (srv->cklen == delim - val_beg) &&
3315 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
3316 if ((srv->cur_state != SRV_ST_STOPPED) ||
3317 (s->be->options & PR_O_PERSIST) ||
3318 (s->flags & SF_FORCE_PRST)) {
3319 /* we found the server and we can use it */
3320 txn->flags &= ~TX_CK_MASK;
3321 txn->flags |= (srv->cur_state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
3322 s->flags |= SF_DIRECT | SF_ASSIGNED;
3323 s->target = &srv->obj_type;
3324 break;
3325 } else {
3326 /* we found a server, but it's down,
3327 * mark it as such and go on in case
3328 * another one is available.
3329 */
3330 txn->flags &= ~TX_CK_MASK;
3331 txn->flags |= TX_CK_DOWN;
3332 }
3333 }
3334 srv = srv->next;
3335 }
3336
3337 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
3338 /* no server matched this cookie or we deliberately skipped it */
3339 txn->flags &= ~TX_CK_MASK;
3340 if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
3341 txn->flags |= TX_CK_UNUSED;
3342 else
3343 txn->flags |= TX_CK_INVALID;
3344 }
3345
3346 /* depending on the cookie mode, we may have to either :
3347 * - delete the complete cookie if we're in insert+indirect mode, so that
3348 * the server never sees it ;
3349 * - remove the server id from the cookie value, and tag the cookie as an
Joseph Herlante9d5c722018-11-25 11:00:25 -08003350 * application cookie so that it does not get accidentally removed later,
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003351 * if we're in cookie prefix mode
3352 */
3353 if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
3354 int delta; /* negative */
3355
3356 memmove(val_beg, delim + 1, hdr_end - (delim + 1));
3357 delta = val_beg - (delim + 1);
3358 val_end += delta;
3359 next += delta;
3360 hdr_end += delta;
3361 del_from = NULL;
3362 preserve_hdr = 1; /* we want to keep this cookie */
3363 }
3364 else if (del_from == NULL &&
3365 (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
3366 del_from = prev;
3367 }
3368 }
3369 else {
3370 /* This is not our cookie, so we must preserve it. But if we already
3371 * scheduled another cookie for removal, we cannot remove the
3372 * complete header, but we can remove the previous block itself.
3373 */
3374 preserve_hdr = 1;
3375
3376 if (del_from != NULL) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003377 int delta = http_del_hdr_value(hdr_beg, hdr_end, &del_from, prev);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003378 if (att_beg >= del_from)
3379 att_beg += delta;
3380 if (att_end >= del_from)
3381 att_end += delta;
3382 val_beg += delta;
3383 val_end += delta;
3384 next += delta;
3385 hdr_end += delta;
3386 prev = del_from;
3387 del_from = NULL;
3388 }
3389 }
3390
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003391 } /* for each cookie */
3392
3393
3394 /* There are no more cookies on this line.
3395 * We may still have one (or several) marked for deletion at the
3396 * end of the line. We must do this now in two ways :
3397 * - if some cookies must be preserved, we only delete from the
3398 * mark to the end of line ;
3399 * - if nothing needs to be preserved, simply delete the whole header
3400 */
3401 if (del_from) {
3402 hdr_end = (preserve_hdr ? del_from : hdr_beg);
3403 }
3404 if ((hdr_end - hdr_beg) != ctx.value.len) {
Christopher Faulet3e2638e2019-06-18 09:49:16 +02003405 if (hdr_beg != hdr_end)
3406 htx_change_blk_value_len(htx, ctx.blk, hdr_end - hdr_beg);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003407 else
3408 http_remove_header(htx, &ctx);
3409 }
3410 } /* for each "Cookie header */
3411}
3412
3413/*
3414 * Manage server-side cookies. It can impact performance by about 2% so it is
3415 * desirable to call it only when needed. This function is also used when we
3416 * just need to know if there is a cookie (eg: for check-cache).
3417 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003418static void http_manage_server_side_cookies(struct stream *s, struct channel *res)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003419{
3420 struct session *sess = s->sess;
3421 struct http_txn *txn = s->txn;
3422 struct htx *htx;
3423 struct http_hdr_ctx ctx;
3424 struct server *srv;
3425 char *hdr_beg, *hdr_end;
3426 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau6f7a02a2019-04-15 21:49:49 +02003427 int is_cookie2 = 0;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003428
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003429 htx = htxbuf(&res->buf);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003430
3431 ctx.blk = NULL;
3432 while (1) {
Olivier Houchardf0f42382019-07-22 17:43:46 +02003433 int is_first = 1;
3434
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003435 if (!http_find_header(htx, ist("Set-Cookie"), &ctx, 1)) {
3436 if (!http_find_header(htx, ist("Set-Cookie2"), &ctx, 1))
3437 break;
3438 is_cookie2 = 1;
3439 }
3440
3441 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
3442 * <prev> points to the colon.
3443 */
3444 txn->flags |= TX_SCK_PRESENT;
3445
3446 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
3447 * check-cache is enabled) and we are not interested in checking
3448 * them. Warning, the cookie capture is declared in the frontend.
3449 */
3450 if (s->be->cookie_name == NULL && sess->fe->capture_name == NULL)
3451 break;
3452
3453 /* OK so now we know we have to process this response cookie.
3454 * The format of the Set-Cookie header is slightly different
3455 * from the format of the Cookie header in that it does not
3456 * support the comma as a cookie delimiter (thus the header
3457 * cannot be folded) because the Expires attribute described in
3458 * the original Netscape's spec may contain an unquoted date
3459 * with a comma inside. We have to live with this because
3460 * many browsers don't support Max-Age and some browsers don't
3461 * support quoted strings. However the Set-Cookie2 header is
3462 * clean.
3463 *
3464 * We have to keep multiple pointers in order to support cookie
3465 * removal at the beginning, middle or end of header without
3466 * corrupting the header (in case of set-cookie2). A special
3467 * pointer, <scav> points to the beginning of the set-cookie-av
3468 * fields after the first semi-colon. The <next> pointer points
3469 * either to the end of line (set-cookie) or next unquoted comma
3470 * (set-cookie2). All of these headers are valid :
3471 *
3472 * hdr_beg hdr_end
3473 * | |
3474 * v |
3475 * NAME1 = VALUE 1 ; Secure; Path="/" |
3476 * NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT v
3477 * NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT
3478 * NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard
3479 * | | | | | | | |
3480 * | | | | | | | +-> next
3481 * | | | | | | +------------> scav
3482 * | | | | | +--------------> val_end
3483 * | | | | +--------------------> val_beg
3484 * | | | +----------------------> equal
3485 * | | +------------------------> att_end
3486 * | +----------------------------> att_beg
3487 * +------------------------------> prev
3488 * -------------------------------> hdr_beg
3489 */
3490 hdr_beg = ctx.value.ptr;
3491 hdr_end = hdr_beg + ctx.value.len;
3492 for (prev = hdr_beg; prev < hdr_end; prev = next) {
3493
3494 /* Iterate through all cookies on this line */
3495
3496 /* find att_beg */
3497 att_beg = prev;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003498 if (!is_first)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003499 att_beg++;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003500 is_first = 0;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003501
3502 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
3503 att_beg++;
3504
3505 /* find att_end : this is the first character after the last non
3506 * space before the equal. It may be equal to hdr_end.
3507 */
3508 equal = att_end = att_beg;
3509
3510 while (equal < hdr_end) {
3511 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
3512 break;
3513 if (HTTP_IS_SPHT(*equal++))
3514 continue;
3515 att_end = equal;
3516 }
3517
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003518 /* here, <equal> points to '=', a delimiter or the end. <att_end>
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003519 * is between <att_beg> and <equal>, both may be identical.
3520 */
3521
3522 /* look for end of cookie if there is an equal sign */
3523 if (equal < hdr_end && *equal == '=') {
3524 /* look for the beginning of the value */
3525 val_beg = equal + 1;
3526 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
3527 val_beg++;
3528
3529 /* find the end of the value, respecting quotes */
3530 next = http_find_cookie_value_end(val_beg, hdr_end);
3531
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003532 /* make val_end point to the first white space or delimiter after the value */
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003533 val_end = next;
3534 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
3535 val_end--;
3536 }
3537 else {
3538 /* <equal> points to next comma, semi-colon or EOL */
3539 val_beg = val_end = next = equal;
3540 }
3541
3542 if (next < hdr_end) {
3543 /* Set-Cookie2 supports multiple cookies, and <next> points to
3544 * a colon or semi-colon before the end. So skip all attr-value
3545 * pairs and look for the next comma. For Set-Cookie, since
3546 * commas are permitted in values, skip to the end.
3547 */
3548 if (is_cookie2)
3549 next = http_find_hdr_value_end(next, hdr_end);
3550 else
3551 next = hdr_end;
3552 }
3553
3554 /* Now everything is as on the diagram above */
3555
3556 /* Ignore cookies with no equal sign */
3557 if (equal == val_end)
3558 continue;
3559
3560 /* If there are spaces around the equal sign, we need to
3561 * strip them otherwise we'll get trouble for cookie captures,
3562 * or even for rewrites. Since this happens extremely rarely,
3563 * it does not hurt performance.
3564 */
3565 if (unlikely(att_end != equal || val_beg > equal + 1)) {
3566 int stripped_before = 0;
3567 int stripped_after = 0;
3568
3569 if (att_end != equal) {
3570 memmove(att_end, equal, hdr_end - equal);
3571 stripped_before = (att_end - equal);
3572 equal += stripped_before;
3573 val_beg += stripped_before;
3574 }
3575
3576 if (val_beg > equal + 1) {
3577 memmove(equal + 1, val_beg, hdr_end + stripped_before - val_beg);
3578 stripped_after = (equal + 1) - val_beg;
3579 val_beg += stripped_after;
3580 stripped_before += stripped_after;
3581 }
3582
3583 val_end += stripped_before;
3584 next += stripped_before;
3585 hdr_end += stripped_before;
3586
Christopher Faulet3e2638e2019-06-18 09:49:16 +02003587 htx_change_blk_value_len(htx, ctx.blk, hdr_end - hdr_beg);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003588 ctx.value.len = hdr_end - hdr_beg;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003589 }
3590
3591 /* First, let's see if we want to capture this cookie. We check
3592 * that we don't already have a server side cookie, because we
3593 * can only capture one. Also as an optimisation, we ignore
3594 * cookies shorter than the declared name.
3595 */
3596 if (sess->fe->capture_name != NULL &&
3597 txn->srv_cookie == NULL &&
3598 (val_end - att_beg >= sess->fe->capture_namelen) &&
3599 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
3600 int log_len = val_end - att_beg;
3601 if ((txn->srv_cookie = pool_alloc(pool_head_capture)) == NULL) {
3602 ha_alert("HTTP logging : out of memory.\n");
3603 }
3604 else {
3605 if (log_len > sess->fe->capture_len)
3606 log_len = sess->fe->capture_len;
3607 memcpy(txn->srv_cookie, att_beg, log_len);
3608 txn->srv_cookie[log_len] = 0;
3609 }
3610 }
3611
3612 srv = objt_server(s->target);
3613 /* now check if we need to process it for persistence */
3614 if (!(s->flags & SF_IGNORE_PRST) &&
3615 (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
3616 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
3617 /* assume passive cookie by default */
3618 txn->flags &= ~TX_SCK_MASK;
3619 txn->flags |= TX_SCK_FOUND;
3620
3621 /* If the cookie is in insert mode on a known server, we'll delete
3622 * this occurrence because we'll insert another one later.
3623 * We'll delete it too if the "indirect" option is set and we're in
3624 * a direct access.
3625 */
3626 if (s->be->ck_opts & PR_CK_PSV) {
3627 /* The "preserve" flag was set, we don't want to touch the
3628 * server's cookie.
3629 */
3630 }
3631 else if ((srv && (s->be->ck_opts & PR_CK_INS)) ||
3632 ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) {
3633 /* this cookie must be deleted */
3634 if (prev == hdr_beg && next == hdr_end) {
3635 /* whole header */
3636 http_remove_header(htx, &ctx);
3637 /* note: while both invalid now, <next> and <hdr_end>
3638 * are still equal, so the for() will stop as expected.
3639 */
3640 } else {
3641 /* just remove the value */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003642 int delta = http_del_hdr_value(hdr_beg, hdr_end, &prev, next);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003643 next = prev;
3644 hdr_end += delta;
3645 }
3646 txn->flags &= ~TX_SCK_MASK;
3647 txn->flags |= TX_SCK_DELETED;
3648 /* and go on with next cookie */
3649 }
3650 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) {
3651 /* replace bytes val_beg->val_end with the cookie name associated
3652 * with this server since we know it.
3653 */
3654 int sliding, delta;
3655
3656 ctx.value = ist2(val_beg, val_end - val_beg);
3657 ctx.lws_before = ctx.lws_after = 0;
3658 http_replace_header_value(htx, &ctx, ist2(srv->cookie, srv->cklen));
3659 delta = srv->cklen - (val_end - val_beg);
3660 sliding = (ctx.value.ptr - val_beg);
3661 hdr_beg += sliding;
3662 val_beg += sliding;
3663 next += sliding + delta;
3664 hdr_end += sliding + delta;
3665
3666 txn->flags &= ~TX_SCK_MASK;
3667 txn->flags |= TX_SCK_REPLACED;
3668 }
3669 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) {
3670 /* insert the cookie name associated with this server
3671 * before existing cookie, and insert a delimiter between them..
3672 */
3673 int sliding, delta;
3674 ctx.value = ist2(val_beg, 0);
3675 ctx.lws_before = ctx.lws_after = 0;
3676 http_replace_header_value(htx, &ctx, ist2(srv->cookie, srv->cklen + 1));
3677 delta = srv->cklen + 1;
3678 sliding = (ctx.value.ptr - val_beg);
3679 hdr_beg += sliding;
3680 val_beg += sliding;
3681 next += sliding + delta;
3682 hdr_end += sliding + delta;
3683
3684 val_beg[srv->cklen] = COOKIE_DELIM;
3685 txn->flags &= ~TX_SCK_MASK;
3686 txn->flags |= TX_SCK_REPLACED;
3687 }
3688 }
3689 /* that's done for this cookie, check the next one on the same
3690 * line when next != hdr_end (only if is_cookie2).
3691 */
3692 }
3693 }
3694}
3695
Christopher Faulet25a02f62018-10-24 12:00:25 +02003696/*
3697 * Parses the Cache-Control and Pragma request header fields to determine if
3698 * the request may be served from the cache and/or if it is cacheable. Updates
3699 * s->txn->flags.
3700 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003701void http_check_request_for_cacheability(struct stream *s, struct channel *req)
Christopher Faulet25a02f62018-10-24 12:00:25 +02003702{
3703 struct http_txn *txn = s->txn;
3704 struct htx *htx;
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003705 struct http_hdr_ctx ctx = { .blk = NULL };
3706 int pragma_found, cc_found;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003707
3708 if ((txn->flags & (TX_CACHEABLE|TX_CACHE_IGNORE)) == TX_CACHE_IGNORE)
3709 return; /* nothing more to do here */
3710
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003711 htx = htxbuf(&req->buf);
Christopher Faulet25a02f62018-10-24 12:00:25 +02003712 pragma_found = cc_found = 0;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003713
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003714 /* Check "pragma" header for HTTP/1.0 compatibility. */
3715 if (http_find_header(htx, ist("pragma"), &ctx, 1)) {
3716 if (isteqi(ctx.value, ist("no-cache"))) {
3717 pragma_found = 1;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003718 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003719 }
Christopher Faulet25a02f62018-10-24 12:00:25 +02003720
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003721 ctx.blk = NULL;
3722 /* Don't use the cache and don't try to store if we found the
3723 * Authorization header */
3724 if (http_find_header(htx, ist("authorization"), &ctx, 1)) {
3725 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3726 txn->flags |= TX_CACHE_IGNORE;
3727 }
Christopher Faulet25a02f62018-10-24 12:00:25 +02003728
Christopher Faulet25a02f62018-10-24 12:00:25 +02003729
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003730 /* Look for "cache-control" header and iterate over all the values
3731 * until we find one that specifies that caching is possible or not. */
3732 ctx.blk = NULL;
3733 while (http_find_header(htx, ist("cache-control"), &ctx, 0)) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003734 cc_found = 1;
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003735 /* We don't check the values after max-age, max-stale nor min-fresh,
3736 * we simply don't use the cache when they're specified. */
3737 if (istmatchi(ctx.value, ist("max-age")) ||
3738 istmatchi(ctx.value, ist("no-cache")) ||
3739 istmatchi(ctx.value, ist("max-stale")) ||
3740 istmatchi(ctx.value, ist("min-fresh"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003741 txn->flags |= TX_CACHE_IGNORE;
3742 continue;
3743 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003744 if (istmatchi(ctx.value, ist("no-store"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003745 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3746 continue;
3747 }
3748 }
3749
3750 /* RFC7234#5.4:
3751 * When the Cache-Control header field is also present and
3752 * understood in a request, Pragma is ignored.
3753 * When the Cache-Control header field is not present in a
3754 * request, caches MUST consider the no-cache request
3755 * pragma-directive as having the same effect as if
3756 * "Cache-Control: no-cache" were present.
3757 */
3758 if (!cc_found && pragma_found)
3759 txn->flags |= TX_CACHE_IGNORE;
3760}
3761
3762/*
3763 * Check if response is cacheable or not. Updates s->txn->flags.
3764 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003765void http_check_response_for_cacheability(struct stream *s, struct channel *res)
Christopher Faulet25a02f62018-10-24 12:00:25 +02003766{
3767 struct http_txn *txn = s->txn;
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003768 struct http_hdr_ctx ctx = { .blk = NULL };
Christopher Faulet25a02f62018-10-24 12:00:25 +02003769 struct htx *htx;
Remi Tricot-Le Bretoncc9bf2e2020-11-12 11:14:41 +01003770 int has_freshness_info = 0;
3771 int has_validator = 0;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003772
3773 if (txn->status < 200) {
3774 /* do not try to cache interim responses! */
3775 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3776 return;
3777 }
3778
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003779 htx = htxbuf(&res->buf);
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003780 /* Check "pragma" header for HTTP/1.0 compatibility. */
3781 if (http_find_header(htx, ist("pragma"), &ctx, 1)) {
3782 if (isteqi(ctx.value, ist("no-cache"))) {
3783 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3784 return;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003785 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003786 }
Christopher Faulet25a02f62018-10-24 12:00:25 +02003787
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003788 /* Look for "cache-control" header and iterate over all the values
3789 * until we find one that specifies that caching is possible or not. */
3790 ctx.blk = NULL;
3791 while (http_find_header(htx, ist("cache-control"), &ctx, 0)) {
3792 if (isteqi(ctx.value, ist("public"))) {
3793 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003794 continue;
3795 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003796 if (isteqi(ctx.value, ist("private")) ||
3797 isteqi(ctx.value, ist("no-cache")) ||
3798 isteqi(ctx.value, ist("no-store")) ||
3799 isteqi(ctx.value, ist("max-age=0")) ||
3800 isteqi(ctx.value, ist("s-maxage=0"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003801 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003802 continue;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003803 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003804 /* We might have a no-cache="set-cookie" form. */
3805 if (istmatchi(ctx.value, ist("no-cache=\"set-cookie"))) {
3806 txn->flags &= ~TX_CACHE_COOK;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003807 continue;
3808 }
Remi Tricot-Le Bretoncc9bf2e2020-11-12 11:14:41 +01003809
3810 if (istmatchi(ctx.value, ist("s-maxage")) ||
3811 istmatchi(ctx.value, ist("max-age"))) {
3812 has_freshness_info = 1;
3813 continue;
3814 }
3815 }
3816
3817 /* If no freshness information could be found in Cache-Control values,
3818 * look for an Expires header. */
3819 if (!has_freshness_info) {
3820 ctx.blk = NULL;
3821 has_freshness_info = http_find_header(htx, ist("expires"), &ctx, 0);
Christopher Faulet25a02f62018-10-24 12:00:25 +02003822 }
Remi Tricot-Le Bretoncc9bf2e2020-11-12 11:14:41 +01003823
3824 /* If no freshness information could be found in Cache-Control or Expires
3825 * values, look for an explicit validator. */
3826 if (!has_freshness_info) {
3827 ctx.blk = NULL;
3828 has_validator = 1;
3829 if (!http_find_header(htx, ist("etag"), &ctx, 0)) {
3830 ctx.blk = NULL;
3831 if (!http_find_header(htx, ist("last-modified"), &ctx, 0))
3832 has_validator = 0;
3833 }
3834 }
3835
3836 /* We won't store an entry that has neither a cache validator nor an
3837 * explicit expiration time, as suggested in RFC 7234#3. */
3838 if (!has_freshness_info && !has_validator)
3839 txn->flags |= TX_CACHE_IGNORE;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003840}
3841
Christopher Faulet377c5a52018-10-24 21:21:30 +02003842/*
3843 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
3844 * for the current backend.
3845 *
3846 * It is assumed that the request is either a HEAD, GET, or POST and that the
3847 * uri_auth field is valid.
3848 *
3849 * Returns 1 if stats should be provided, otherwise 0.
3850 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003851static int http_stats_check_uri(struct stream *s, struct http_txn *txn, struct proxy *backend)
Christopher Faulet377c5a52018-10-24 21:21:30 +02003852{
3853 struct uri_auth *uri_auth = backend->uri_auth;
3854 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003855 struct htx_sl *sl;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003856 struct ist uri;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003857
3858 if (!uri_auth)
3859 return 0;
3860
3861 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
3862 return 0;
3863
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003864 htx = htxbuf(&s->req.buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02003865 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003866 uri = htx_sl_req_uri(sl);
Willy Tarreau1eb3b482019-10-31 15:50:28 +01003867 if (*uri_auth->uri_prefix == '/')
3868 uri = http_get_path(uri);
Christopher Faulet377c5a52018-10-24 21:21:30 +02003869
3870 /* check URI size */
3871 if (uri_auth->uri_len > uri.len)
3872 return 0;
3873
3874 if (memcmp(uri.ptr, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
3875 return 0;
3876
3877 return 1;
3878}
3879
3880/* This function prepares an applet to handle the stats. It can deal with the
3881 * "100-continue" expectation, check that admin rules are met for POST requests,
3882 * and program a response message if something was unexpected. It cannot fail
3883 * and always relies on the stats applet to complete the job. It does not touch
3884 * analysers nor counters, which are left to the caller. It does not touch
3885 * s->target which is supposed to already point to the stats applet. The caller
3886 * is expected to have already assigned an appctx to the stream.
3887 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003888static int http_handle_stats(struct stream *s, struct channel *req)
Christopher Faulet377c5a52018-10-24 21:21:30 +02003889{
3890 struct stats_admin_rule *stats_admin_rule;
3891 struct stream_interface *si = &s->si[1];
3892 struct session *sess = s->sess;
3893 struct http_txn *txn = s->txn;
3894 struct http_msg *msg = &txn->req;
3895 struct uri_auth *uri_auth = s->be->uri_auth;
3896 const char *h, *lookup, *end;
3897 struct appctx *appctx;
3898 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003899 struct htx_sl *sl;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003900
3901 appctx = si_appctx(si);
3902 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
3903 appctx->st1 = appctx->st2 = 0;
3904 appctx->ctx.stats.st_code = STAT_STATUS_INIT;
Willy Tarreau676c29e2019-10-09 10:50:01 +02003905 appctx->ctx.stats.flags |= uri_auth->flags;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003906 appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */
3907 if ((msg->flags & HTTP_MSGF_VER_11) && (txn->meth != HTTP_METH_HEAD))
3908 appctx->ctx.stats.flags |= STAT_CHUNKED;
3909
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003910 htx = htxbuf(&req->buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02003911 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003912 lookup = HTX_SL_REQ_UPTR(sl) + uri_auth->uri_len;
3913 end = HTX_SL_REQ_UPTR(sl) + HTX_SL_REQ_ULEN(sl);
Christopher Faulet377c5a52018-10-24 21:21:30 +02003914
3915 for (h = lookup; h <= end - 3; h++) {
3916 if (memcmp(h, ";up", 3) == 0) {
3917 appctx->ctx.stats.flags |= STAT_HIDE_DOWN;
3918 break;
3919 }
Amaury Denoyelle91e55ea2021-02-25 14:46:08 +01003920 }
3921
3922 for (h = lookup; h <= end - 9; h++) {
3923 if (memcmp(h, ";no-maint", 9) == 0) {
Willy Tarreau3e320362020-10-23 17:28:57 +02003924 appctx->ctx.stats.flags |= STAT_HIDE_MAINT;
3925 break;
3926 }
Christopher Faulet377c5a52018-10-24 21:21:30 +02003927 }
3928
3929 if (uri_auth->refresh) {
3930 for (h = lookup; h <= end - 10; h++) {
3931 if (memcmp(h, ";norefresh", 10) == 0) {
3932 appctx->ctx.stats.flags |= STAT_NO_REFRESH;
3933 break;
3934 }
3935 }
3936 }
3937
3938 for (h = lookup; h <= end - 4; h++) {
3939 if (memcmp(h, ";csv", 4) == 0) {
Christopher Faulet6338a082019-09-09 15:50:54 +02003940 appctx->ctx.stats.flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
Christopher Faulet377c5a52018-10-24 21:21:30 +02003941 break;
3942 }
3943 }
3944
3945 for (h = lookup; h <= end - 6; h++) {
3946 if (memcmp(h, ";typed", 6) == 0) {
Christopher Faulet6338a082019-09-09 15:50:54 +02003947 appctx->ctx.stats.flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
Christopher Faulet377c5a52018-10-24 21:21:30 +02003948 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
3949 break;
3950 }
3951 }
3952
Christopher Faulet6338a082019-09-09 15:50:54 +02003953 for (h = lookup; h <= end - 5; h++) {
3954 if (memcmp(h, ";json", 5) == 0) {
3955 appctx->ctx.stats.flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
3956 appctx->ctx.stats.flags |= STAT_FMT_JSON;
3957 break;
3958 }
3959 }
3960
3961 for (h = lookup; h <= end - 12; h++) {
3962 if (memcmp(h, ";json-schema", 12) == 0) {
3963 appctx->ctx.stats.flags &= ~STAT_FMT_MASK;
3964 appctx->ctx.stats.flags |= STAT_JSON_SCHM;
3965 break;
3966 }
3967 }
3968
Christopher Faulet377c5a52018-10-24 21:21:30 +02003969 for (h = lookup; h <= end - 8; h++) {
3970 if (memcmp(h, ";st=", 4) == 0) {
3971 int i;
3972 h += 4;
3973 appctx->ctx.stats.st_code = STAT_STATUS_UNKN;
3974 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
3975 if (strncmp(stat_status_codes[i], h, 4) == 0) {
3976 appctx->ctx.stats.st_code = i;
3977 break;
3978 }
3979 }
3980 break;
3981 }
3982 }
3983
3984 appctx->ctx.stats.scope_str = 0;
3985 appctx->ctx.stats.scope_len = 0;
3986 for (h = lookup; h <= end - 8; h++) {
3987 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
3988 int itx = 0;
3989 const char *h2;
3990 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
3991 const char *err;
3992
3993 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
3994 h2 = h;
Christopher Fauleted7a0662019-01-14 11:07:34 +01003995 appctx->ctx.stats.scope_str = h2 - HTX_SL_REQ_UPTR(sl);
3996 while (h < end) {
Christopher Faulet377c5a52018-10-24 21:21:30 +02003997 if (*h == ';' || *h == '&' || *h == ' ')
3998 break;
3999 itx++;
4000 h++;
4001 }
4002
4003 if (itx > STAT_SCOPE_TXT_MAXLEN)
4004 itx = STAT_SCOPE_TXT_MAXLEN;
4005 appctx->ctx.stats.scope_len = itx;
4006
4007 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
4008 memcpy(scope_txt, h2, itx);
4009 scope_txt[itx] = '\0';
4010 err = invalid_char(scope_txt);
4011 if (err) {
4012 /* bad char in search text => clear scope */
4013 appctx->ctx.stats.scope_str = 0;
4014 appctx->ctx.stats.scope_len = 0;
4015 }
4016 break;
4017 }
4018 }
4019
4020 /* now check whether we have some admin rules for this request */
4021 list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) {
4022 int ret = 1;
4023
4024 if (stats_admin_rule->cond) {
4025 ret = acl_exec_cond(stats_admin_rule->cond, s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
4026 ret = acl_pass(ret);
4027 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
4028 ret = !ret;
4029 }
4030
4031 if (ret) {
4032 /* no rule, or the rule matches */
4033 appctx->ctx.stats.flags |= STAT_ADMIN;
4034 break;
4035 }
4036 }
4037
Christopher Faulet5d45e382019-02-27 15:15:23 +01004038 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
4039 appctx->st0 = STAT_HTTP_HEAD;
4040 else if (txn->meth == HTTP_METH_POST) {
Christopher Fauletbd9e8422019-08-15 22:26:48 +02004041 if (appctx->ctx.stats.flags & STAT_ADMIN) {
Christopher Faulet377c5a52018-10-24 21:21:30 +02004042 appctx->st0 = STAT_HTTP_POST;
Christopher Fauletbd9e8422019-08-15 22:26:48 +02004043 if (msg->msg_state < HTTP_MSG_DATA)
4044 req->analysers |= AN_REQ_HTTP_BODY;
4045 }
Christopher Faulet377c5a52018-10-24 21:21:30 +02004046 else {
Christopher Faulet5d45e382019-02-27 15:15:23 +01004047 /* POST without admin level */
Christopher Faulet377c5a52018-10-24 21:21:30 +02004048 appctx->ctx.stats.flags &= ~STAT_CHUNKED;
4049 appctx->ctx.stats.st_code = STAT_STATUS_DENY;
4050 appctx->st0 = STAT_HTTP_LAST;
4051 }
4052 }
4053 else {
Christopher Faulet5d45e382019-02-27 15:15:23 +01004054 /* Unsupported method */
4055 appctx->ctx.stats.flags &= ~STAT_CHUNKED;
4056 appctx->ctx.stats.st_code = STAT_STATUS_IVAL;
4057 appctx->st0 = STAT_HTTP_LAST;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004058 }
4059
4060 s->task->nice = -32; /* small boost for HTTP statistics */
4061 return 1;
4062}
4063
Christopher Faulet021a8e42021-03-29 10:46:38 +02004064/* This function waits for the message payload at most <time> milliseconds (may
4065 * be set to TICK_ETERNITY). It stops to wait if at least <bytes> bytes of the
4066 * payload are received (0 means no limit). It returns HTTP_RULE_* depending on
4067 * the result:
4068 *
4069 * - HTTP_RULE_RES_CONT when conditions are met to stop waiting
4070 * - HTTP_RULE_RES_YIELD to wait for more data
Ilya Shipitsinb2be9a12021-04-24 13:25:42 +05004071 * - HTTP_RULE_RES_ABRT when a timeout occurred.
Christopher Faulet021a8e42021-03-29 10:46:38 +02004072 * - HTTP_RULE_RES_BADREQ if a parsing error is raised by lower level
Ilya Shipitsinb2be9a12021-04-24 13:25:42 +05004073 * - HTTP_RULE_RES_ERROR if an internal error occurred
Christopher Faulet021a8e42021-03-29 10:46:38 +02004074 *
Ilya Shipitsinb2be9a12021-04-24 13:25:42 +05004075 * If a timeout occurred, this function is responsible to emit the right response
Christopher Faulet021a8e42021-03-29 10:46:38 +02004076 * to the client, depending on the channel (408 on request side, 504 on response
4077 * side). All other errors must be handled by the caller.
4078 */
4079enum rule_result http_wait_for_msg_body(struct stream *s, struct channel *chn,
4080 unsigned int time, unsigned int bytes)
4081{
4082 struct session *sess = s->sess;
4083 struct http_txn *txn = s->txn;
4084 struct http_msg *msg = ((chn->flags & CF_ISRESP) ? &txn->rsp : &txn->req);
4085 struct htx *htx;
4086 enum rule_result ret = HTTP_RULE_RES_CONT;
4087
4088 htx = htxbuf(&chn->buf);
4089
4090 if (htx->flags & HTX_FL_PARSING_ERROR) {
4091 ret = HTTP_RULE_RES_BADREQ;
4092 goto end;
4093 }
4094 if (htx->flags & HTX_FL_PROCESSING_ERROR) {
4095 ret = HTTP_RULE_RES_ERROR;
4096 goto end;
4097 }
4098
4099 /* Do nothing for bodyless and CONNECT requests */
4100 if (txn->meth == HTTP_METH_CONNECT || (msg->flags & HTTP_MSGF_BODYLESS))
4101 goto end;
4102
4103 if (!(chn->flags & CF_ISRESP) && msg->msg_state < HTTP_MSG_DATA) {
4104 if (http_handle_expect_hdr(s, htx, msg) == -1) {
4105 ret = HTTP_RULE_RES_ERROR;
4106 goto end;
4107 }
4108 }
4109
4110 msg->msg_state = HTTP_MSG_DATA;
4111
4112 /* Now we're in HTTP_MSG_DATA. We just need to know if all data have
4113 * been received or if the buffer is full.
4114 */
4115 if ((htx->flags & HTX_FL_EOM) || htx_get_tail_type(htx) > HTX_BLK_DATA ||
4116 channel_htx_full(chn, htx, global.tune.maxrewrite))
4117 goto end;
4118
4119 if (bytes) {
4120 struct htx_blk *blk;
4121 unsigned int len = 0;
4122
4123 for (blk = htx_get_first_blk(htx); blk; blk = htx_get_next_blk(htx, blk)) {
4124 if (htx_get_blk_type(blk) != HTX_BLK_DATA)
4125 continue;
4126 len += htx_get_blksz(blk);
4127 if (len >= bytes)
4128 goto end;
4129 }
4130 }
4131
4132 if ((chn->flags & CF_READ_TIMEOUT) || tick_is_expired(chn->analyse_exp, now_ms)) {
4133 if (!(chn->flags & CF_ISRESP))
4134 goto abort_req;
4135 goto abort_res;
4136 }
4137
4138 /* we get here if we need to wait for more data */
4139 if (!(chn->flags & (CF_SHUTR | CF_READ_ERROR))) {
4140 if (!tick_isset(chn->analyse_exp))
4141 chn->analyse_exp = tick_add_ifset(now_ms, time);
4142 ret = HTTP_RULE_RES_YIELD;
4143 }
4144
4145 end:
4146 return ret;
4147
4148 abort_req:
4149 txn->status = 408;
4150 if (!(s->flags & SF_ERR_MASK))
4151 s->flags |= SF_ERR_CLITO;
4152 if (!(s->flags & SF_FINST_MASK))
4153 s->flags |= SF_FINST_D;
Willy Tarreau4781b152021-04-06 13:53:36 +02004154 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req);
Christopher Faulet021a8e42021-03-29 10:46:38 +02004155 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02004156 _HA_ATOMIC_INC(&sess->listener->counters->failed_req);
Christopher Faulet021a8e42021-03-29 10:46:38 +02004157 http_reply_and_close(s, txn->status, http_error_message(s));
4158 ret = HTTP_RULE_RES_ABRT;
4159 goto end;
4160
4161 abort_res:
4162 txn->status = 504;
4163 if (!(s->flags & SF_ERR_MASK))
4164 s->flags |= SF_ERR_SRVTO;
4165 if (!(s->flags & SF_FINST_MASK))
4166 s->flags |= SF_FINST_D;
4167 stream_inc_http_fail_ctr(s);
4168 http_reply_and_close(s, txn->status, http_error_message(s));
4169 ret = HTTP_RULE_RES_ABRT;
4170 goto end;
4171}
4172
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004173void http_perform_server_redirect(struct stream *s, struct stream_interface *si)
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004174{
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004175 struct channel *req = &s->req;
4176 struct channel *res = &s->res;
4177 struct server *srv;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004178 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004179 struct htx_sl *sl;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004180 struct ist path, location;
4181 unsigned int flags;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004182
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004183 /*
4184 * Create the location
4185 */
4186 chunk_reset(&trash);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004187
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004188 /* 1: add the server's prefix */
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004189 /* special prefix "/" means don't change URL */
4190 srv = __objt_server(s->target);
4191 if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') {
4192 if (!chunk_memcat(&trash, srv->rdr_pfx, srv->rdr_len))
4193 return;
4194 }
4195
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004196 /* 2: add the request Path */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004197 htx = htxbuf(&req->buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02004198 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004199 path = http_get_path(htx_sl_req_uri(sl));
Tim Duesterhused526372020-03-05 17:56:33 +01004200 if (!isttest(path))
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004201 return;
4202
4203 if (!chunk_memcat(&trash, path.ptr, path.len))
4204 return;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004205 location = ist2(trash.area, trash.data);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004206
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004207 /*
4208 * Create the 302 respone
4209 */
4210 htx = htx_from_buf(&res->buf);
4211 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
4212 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
4213 ist("HTTP/1.1"), ist("302"), ist("Found"));
4214 if (!sl)
4215 goto fail;
4216 sl->info.res.status = 302;
4217 s->txn->status = 302;
4218
4219 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) ||
4220 !htx_add_header(htx, ist("Connection"), ist("close")) ||
4221 !htx_add_header(htx, ist("Content-length"), ist("0")) ||
4222 !htx_add_header(htx, ist("Location"), location))
4223 goto fail;
4224
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004225 if (!htx_add_endof(htx, HTX_BLK_EOH))
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004226 goto fail;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004227
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004228 htx->flags |= HTX_FL_EOM;
Christopher Fauletc20afb82020-01-24 19:16:26 +01004229 htx_to_buf(htx, &res->buf);
Christopher Fauleta72a7e42020-01-28 09:28:11 +01004230 if (!http_forward_proxy_resp(s, 1))
4231 goto fail;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004232
4233 /* return without error. */
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004234 si_shutr(si);
4235 si_shutw(si);
4236 si->err_type = SI_ET_NONE;
4237 si->state = SI_ST_CLO;
4238
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004239 if (!(s->flags & SF_ERR_MASK))
4240 s->flags |= SF_ERR_LOCAL;
4241 if (!(s->flags & SF_FINST_MASK))
4242 s->flags |= SF_FINST_C;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004243
4244 /* FIXME: we should increase a counter of redirects per server and per backend. */
4245 srv_inc_sess_ctr(srv);
4246 srv_set_sess_last(srv);
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004247 return;
4248
4249 fail:
4250 /* If an error occurred, remove the incomplete HTTP response from the
4251 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004252 channel_htx_truncate(res, htx);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004253}
4254
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05004255/* This function terminates the request because it was completely analyzed or
Christopher Fauletf2824e62018-10-01 12:12:37 +02004256 * because an error was triggered during the body forwarding.
4257 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004258static void http_end_request(struct stream *s)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004259{
4260 struct channel *chn = &s->req;
4261 struct http_txn *txn = s->txn;
4262
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004263 DBG_TRACE_ENTER(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004264
Christopher Fauletb42a8b62018-11-19 21:59:00 +01004265 if (unlikely(txn->req.msg_state == HTTP_MSG_ERROR ||
4266 txn->rsp.msg_state == HTTP_MSG_ERROR)) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004267 channel_abort(chn);
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004268 channel_htx_truncate(chn, htxbuf(&chn->buf));
Christopher Fauletf2824e62018-10-01 12:12:37 +02004269 goto end;
4270 }
4271
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004272 if (unlikely(txn->req.msg_state < HTTP_MSG_DONE)) {
4273 DBG_TRACE_DEVEL("waiting end of the request", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004274 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004275 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004276
4277 if (txn->req.msg_state == HTTP_MSG_DONE) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004278 /* No need to read anymore, the request was completely parsed.
4279 * We can shut the read side unless we want to abort_on_close,
4280 * or we have a POST request. The issue with POST requests is
4281 * that some browsers still send a CRLF after the request, and
4282 * this CRLF must be read so that it does not remain in the kernel
4283 * buffers, otherwise a close could cause an RST on some systems
4284 * (eg: Linux).
4285 */
Christopher Faulet769d0e92019-03-22 14:23:18 +01004286 if (!(s->be->options & PR_O_ABRT_CLOSE) && txn->meth != HTTP_METH_POST)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004287 channel_dont_read(chn);
4288
4289 /* if the server closes the connection, we want to immediately react
4290 * and close the socket to save packets and syscalls.
4291 */
4292 s->si[1].flags |= SI_FL_NOHALF;
4293
4294 /* In any case we've finished parsing the request so we must
4295 * disable Nagle when sending data because 1) we're not going
4296 * to shut this side, and 2) the server is waiting for us to
4297 * send pending data.
4298 */
4299 chn->flags |= CF_NEVER_WAIT;
4300
Christopher Fauletd01ce402019-01-02 17:44:13 +01004301 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
4302 /* The server has not finished to respond, so we
4303 * don't want to move in order not to upset it.
4304 */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004305 DBG_TRACE_DEVEL("waiting end of the response", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletd01ce402019-01-02 17:44:13 +01004306 return;
4307 }
4308
Christopher Fauletf2824e62018-10-01 12:12:37 +02004309 /* When we get here, it means that both the request and the
4310 * response have finished receiving. Depending on the connection
4311 * mode, we'll have to wait for the last bytes to leave in either
4312 * direction, and sometimes for a close to be effective.
4313 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02004314 if (txn->flags & TX_CON_WANT_TUN) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004315 /* Tunnel mode will not have any analyser so it needs to
4316 * poll for reads.
4317 */
4318 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004319 if (b_data(&chn->buf)) {
4320 DBG_TRACE_DEVEL("waiting to flush the request", STRM_EV_HTTP_ANA, s, txn);
Christopher Faulet9768c262018-10-22 09:34:31 +02004321 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004322 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004323 txn->req.msg_state = HTTP_MSG_TUNNEL;
4324 }
4325 else {
4326 /* we're not expecting any new data to come for this
4327 * transaction, so we can close it.
Christopher Faulet9768c262018-10-22 09:34:31 +02004328 *
4329 * However, there is an exception if the response
4330 * length is undefined. In this case, we need to wait
4331 * the close from the server. The response will be
4332 * switched in TUNNEL mode until the end.
Christopher Fauletf2824e62018-10-01 12:12:37 +02004333 */
4334 if (!(txn->rsp.flags & HTTP_MSGF_XFER_LEN) &&
4335 txn->rsp.msg_state != HTTP_MSG_CLOSED)
Christopher Faulet9768c262018-10-22 09:34:31 +02004336 goto check_channel_flags;
Christopher Fauletf2824e62018-10-01 12:12:37 +02004337
4338 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4339 channel_shutr_now(chn);
4340 channel_shutw_now(chn);
4341 }
4342 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004343 goto check_channel_flags;
4344 }
4345
4346 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
4347 http_msg_closing:
4348 /* nothing else to forward, just waiting for the output buffer
4349 * to be empty and for the shutw_now to take effect.
4350 */
4351 if (channel_is_empty(chn)) {
4352 txn->req.msg_state = HTTP_MSG_CLOSED;
4353 goto http_msg_closed;
4354 }
4355 else if (chn->flags & CF_SHUTW) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004356 txn->req.msg_state = HTTP_MSG_ERROR;
4357 goto end;
4358 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004359 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004360 return;
4361 }
4362
4363 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
4364 http_msg_closed:
Christopher Fauletf2824e62018-10-01 12:12:37 +02004365 /* if we don't know whether the server will close, we need to hard close */
4366 if (txn->rsp.flags & HTTP_MSGF_XFER_LEN)
4367 s->si[1].flags |= SI_FL_NOLINGER; /* we want to close ASAP */
Christopher Fauletf2824e62018-10-01 12:12:37 +02004368 /* see above in MSG_DONE why we only do this in these states */
Christopher Faulet769d0e92019-03-22 14:23:18 +01004369 if (!(s->be->options & PR_O_ABRT_CLOSE))
Christopher Fauletf2824e62018-10-01 12:12:37 +02004370 channel_dont_read(chn);
4371 goto end;
4372 }
4373
4374 check_channel_flags:
4375 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4376 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4377 /* if we've just closed an output, let's switch */
4378 txn->req.msg_state = HTTP_MSG_CLOSING;
4379 goto http_msg_closing;
4380 }
4381
4382 end:
4383 chn->analysers &= AN_REQ_FLT_END;
Christopher Faulet198ef8b2020-12-15 13:32:55 +01004384 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
4385 chn->flags |= CF_NEVER_WAIT;
4386 if (HAS_REQ_DATA_FILTERS(s))
Christopher Fauletf2824e62018-10-01 12:12:37 +02004387 chn->analysers |= AN_REQ_FLT_XFER_DATA;
Christopher Faulet198ef8b2020-12-15 13:32:55 +01004388 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004389 channel_auto_close(chn);
4390 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004391 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004392}
4393
4394
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05004395/* This function terminates the response because it was completely analyzed or
Christopher Fauletf2824e62018-10-01 12:12:37 +02004396 * because an error was triggered during the body forwarding.
4397 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004398static void http_end_response(struct stream *s)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004399{
4400 struct channel *chn = &s->res;
4401 struct http_txn *txn = s->txn;
4402
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004403 DBG_TRACE_ENTER(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004404
Christopher Fauletb42a8b62018-11-19 21:59:00 +01004405 if (unlikely(txn->req.msg_state == HTTP_MSG_ERROR ||
4406 txn->rsp.msg_state == HTTP_MSG_ERROR)) {
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004407 channel_htx_truncate(&s->req, htxbuf(&s->req.buf));
Christopher Faulet9768c262018-10-22 09:34:31 +02004408 channel_abort(&s->req);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004409 goto end;
4410 }
4411
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004412 if (unlikely(txn->rsp.msg_state < HTTP_MSG_DONE)) {
4413 DBG_TRACE_DEVEL("waiting end of the response", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004414 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004415 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004416
4417 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
4418 /* In theory, we don't need to read anymore, but we must
4419 * still monitor the server connection for a possible close
4420 * while the request is being uploaded, so we don't disable
4421 * reading.
4422 */
4423 /* channel_dont_read(chn); */
4424
4425 if (txn->req.msg_state < HTTP_MSG_DONE) {
4426 /* The client seems to still be sending data, probably
4427 * because we got an error response during an upload.
4428 * We have the choice of either breaking the connection
4429 * or letting it pass through. Let's do the later.
4430 */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004431 DBG_TRACE_DEVEL("waiting end of the request", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004432 return;
4433 }
4434
4435 /* When we get here, it means that both the request and the
4436 * response have finished receiving. Depending on the connection
4437 * mode, we'll have to wait for the last bytes to leave in either
4438 * direction, and sometimes for a close to be effective.
4439 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02004440 if (txn->flags & TX_CON_WANT_TUN) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004441 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004442 if (b_data(&chn->buf)) {
4443 DBG_TRACE_DEVEL("waiting to flush the respone", STRM_EV_HTTP_ANA, s, txn);
Christopher Faulet9768c262018-10-22 09:34:31 +02004444 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004445 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004446 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
4447 }
4448 else {
4449 /* we're not expecting any new data to come for this
4450 * transaction, so we can close it.
4451 */
4452 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4453 channel_shutr_now(chn);
4454 channel_shutw_now(chn);
4455 }
4456 }
4457 goto check_channel_flags;
4458 }
4459
4460 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
4461 http_msg_closing:
4462 /* nothing else to forward, just waiting for the output buffer
4463 * to be empty and for the shutw_now to take effect.
4464 */
4465 if (channel_is_empty(chn)) {
4466 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4467 goto http_msg_closed;
4468 }
4469 else if (chn->flags & CF_SHUTW) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004470 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau4781b152021-04-06 13:53:36 +02004471 _HA_ATOMIC_INC(&strm_sess(s)->fe->fe_counters.cli_aborts);
4472 _HA_ATOMIC_INC(&s->be->be_counters.cli_aborts);
William Lallemand36119de2021-03-08 15:26:48 +01004473 if (strm_sess(s)->listener && strm_sess(s)->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02004474 _HA_ATOMIC_INC(&strm_sess(s)->listener->counters->cli_aborts);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004475 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02004476 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.cli_aborts);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004477 goto end;
4478 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004479 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004480 return;
4481 }
4482
4483 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
4484 http_msg_closed:
4485 /* drop any pending data */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004486 channel_htx_truncate(&s->req, htxbuf(&s->req.buf));
Christopher Faulet9768c262018-10-22 09:34:31 +02004487 channel_abort(&s->req);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004488 goto end;
4489 }
4490
4491 check_channel_flags:
4492 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4493 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4494 /* if we've just closed an output, let's switch */
4495 txn->rsp.msg_state = HTTP_MSG_CLOSING;
4496 goto http_msg_closing;
4497 }
4498
4499 end:
4500 chn->analysers &= AN_RES_FLT_END;
Christopher Faulet198ef8b2020-12-15 13:32:55 +01004501 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
4502 chn->flags |= CF_NEVER_WAIT;
4503 if (HAS_RSP_DATA_FILTERS(s))
4504 chn->analysers |= AN_RES_FLT_XFER_DATA;
4505 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004506 channel_auto_close(chn);
4507 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004508 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004509}
4510
Christopher Fauletef70e252020-01-28 09:26:19 +01004511/* Forward a response generated by HAProxy (error/redirect/return). This
4512 * function forwards all pending incoming data. If <final> is set to 0, nothing
4513 * more is performed. It is used for 1xx informational messages. Otherwise, the
Christopher Faulet507479b2020-05-15 12:29:46 +02004514 * transaction is terminated and the request is emptied. On success 1 is
Christopher Faulet40e6b552020-06-25 16:04:50 +02004515 * returned. If an error occurred, 0 is returned. If it fails, this function
4516 * only exits. It is the caller responsibility to do the cleanup.
Christopher Fauletef70e252020-01-28 09:26:19 +01004517 */
4518int http_forward_proxy_resp(struct stream *s, int final)
4519{
4520 struct channel *req = &s->req;
4521 struct channel *res = &s->res;
4522 struct htx *htx = htxbuf(&res->buf);
4523 size_t data;
4524
4525 if (final) {
4526 htx->flags |= HTX_FL_PROXY_RESP;
Christopher Faulet507479b2020-05-15 12:29:46 +02004527
Christopher Fauletaab1b672020-11-18 16:44:02 +01004528 if (!htx_is_empty(htx) && !http_eval_after_res_rules(s))
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01004529 return 0;
Christopher Fauletef70e252020-01-28 09:26:19 +01004530
Christopher Fauletd6c48362020-10-19 18:01:38 +02004531 if (s->txn->meth == HTTP_METH_HEAD)
4532 htx_skip_msg_payload(htx);
4533
Christopher Fauletef70e252020-01-28 09:26:19 +01004534 channel_auto_read(req);
4535 channel_abort(req);
4536 channel_auto_close(req);
4537 channel_htx_erase(req, htxbuf(&req->buf));
4538
4539 res->wex = tick_add_ifset(now_ms, res->wto);
4540 channel_auto_read(res);
4541 channel_auto_close(res);
4542 channel_shutr_now(res);
Christopher Faulet1a9db7c2020-06-25 15:36:45 +02004543 res->flags |= CF_EOI; /* The response is terminated, add EOI */
Christopher Faulet42432f32020-11-20 17:43:16 +01004544 htxbuf(&res->buf)->flags |= HTX_FL_EOM; /* no more data are expected */
Christopher Fauletef70e252020-01-28 09:26:19 +01004545 }
Christopher Fauletcf6898c2020-06-25 15:55:11 +02004546 else {
4547 /* Send ASAP informational messages. Rely on CF_EOI for final
4548 * response.
4549 */
4550 res->flags |= CF_SEND_DONTWAIT;
4551 }
Christopher Fauletef70e252020-01-28 09:26:19 +01004552
4553 data = htx->data - co_data(res);
4554 c_adv(res, data);
4555 htx->first = -1;
4556 res->total += data;
4557 return 1;
4558}
4559
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004560void http_server_error(struct stream *s, struct stream_interface *si, int err,
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004561 int finst, struct http_reply *msg)
Christopher Faulet0f226952018-10-22 09:29:56 +02004562{
Christopher Faulet72c7d8d2020-01-27 15:32:25 +01004563 http_reply_and_close(s, s->txn->status, msg);
Christopher Faulet0f226952018-10-22 09:29:56 +02004564 if (!(s->flags & SF_ERR_MASK))
4565 s->flags |= err;
4566 if (!(s->flags & SF_FINST_MASK))
4567 s->flags |= finst;
4568}
4569
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004570void http_reply_and_close(struct stream *s, short status, struct http_reply *msg)
Christopher Faulet0f226952018-10-22 09:29:56 +02004571{
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004572 if (!msg) {
4573 channel_htx_truncate(&s->res, htxbuf(&s->res.buf));
4574 goto end;
4575 }
4576
4577 if (http_reply_message(s, msg) == -1) {
4578 /* On error, return a 500 error message, but don't rewrite it if
Christopher Faulet40e6b552020-06-25 16:04:50 +02004579 * it is already an internal error. If it was already a "const"
4580 * 500 error, just fail.
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004581 */
Christopher Faulet40e6b552020-06-25 16:04:50 +02004582 if (s->txn->status == 500) {
4583 if (s->txn->flags & TX_CONST_REPLY)
4584 goto end;
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004585 s->txn->flags |= TX_CONST_REPLY;
Christopher Faulet40e6b552020-06-25 16:04:50 +02004586 }
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004587 s->txn->status = 500;
4588 s->txn->http_reply = NULL;
4589 return http_reply_and_close(s, s->txn->status, http_error_message(s));
4590 }
4591
4592end:
4593 s->res.wex = tick_add_ifset(now_ms, s->res.wto);
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004594
Christopher Fauletdb8ba102021-09-10 09:17:50 +02004595 /* At this staged, HTTP analysis is finished */
4596 s->req.analysers &= AN_REQ_FLT_END;
4597 s->req.analyse_exp = TICK_ETERNITY;
4598
4599 s->res.analysers &= AN_RES_FLT_END;
4600 s->res.analyse_exp = TICK_ETERNITY;
4601
Christopher Faulet0f226952018-10-22 09:29:56 +02004602 channel_auto_read(&s->req);
4603 channel_abort(&s->req);
4604 channel_auto_close(&s->req);
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004605 channel_htx_erase(&s->req, htxbuf(&s->req.buf));
Christopher Faulet72c7d8d2020-01-27 15:32:25 +01004606 channel_auto_read(&s->res);
4607 channel_auto_close(&s->res);
4608 channel_shutr_now(&s->res);
Christopher Faulet0f226952018-10-22 09:29:56 +02004609}
4610
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004611struct http_reply *http_error_message(struct stream *s)
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004612{
4613 const int msgnum = http_get_status_idx(s->txn->status);
4614
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004615 if (s->txn->http_reply)
4616 return s->txn->http_reply;
4617 else if (s->be->replies[msgnum])
4618 return s->be->replies[msgnum];
4619 else if (strm_fe(s)->replies[msgnum])
4620 return strm_fe(s)->replies[msgnum];
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004621 else
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004622 return &http_err_replies[msgnum];
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004623}
4624
Christopher Faulet40e6b552020-06-25 16:04:50 +02004625/* Produces an HTX message from an http reply. Depending on the http reply type,
4626 * a, errorfile, an raw file or a log-format string is used. On success, it
4627 * returns 0. If an error occurs -1 is returned. If it fails, this function only
4628 * exits. It is the caller responsibility to do the cleanup.
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004629 */
Christopher Fauletae43b6c2020-05-27 15:24:22 +02004630int http_reply_to_htx(struct stream *s, struct htx *htx, struct http_reply *reply)
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004631{
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004632 struct buffer *errmsg;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004633 struct htx_sl *sl;
4634 struct buffer *body = NULL;
4635 const char *status, *reason, *clen, *ctype;
4636 unsigned int slflags;
4637 int ret = 0;
4638
Christopher Faulete29a97e2020-05-14 14:49:25 +02004639 /*
4640 * - HTTP_REPLY_ERRFILES unexpected here. handled as no payload if so
4641 *
4642 * - HTTP_REPLY_INDIRECT: switch on another reply if defined or handled
4643 * as no payload if NULL. the TXN status code is set with the status
4644 * of the original reply.
4645 */
4646
4647 if (reply->type == HTTP_REPLY_INDIRECT) {
4648 if (reply->body.reply)
4649 reply = reply->body.reply;
4650 }
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004651 if (reply->type == HTTP_REPLY_ERRMSG && !reply->body.errmsg) {
4652 /* get default error message */
4653 if (reply == s->txn->http_reply)
4654 s->txn->http_reply = NULL;
4655 reply = http_error_message(s);
4656 if (reply->type == HTTP_REPLY_INDIRECT) {
4657 if (reply->body.reply)
4658 reply = reply->body.reply;
4659 }
4660 }
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004661
4662 if (reply->type == HTTP_REPLY_ERRMSG) {
4663 /* implicit or explicit error message*/
4664 errmsg = reply->body.errmsg;
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004665 if (errmsg && !b_is_null(errmsg)) {
Christopher Faulet20567362020-05-15 14:52:49 +02004666 if (!htx_copy_msg(htx, errmsg))
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004667 goto fail;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004668 }
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004669 }
4670 else {
4671 /* no payload, file or log-format string */
4672 if (reply->type == HTTP_REPLY_RAW) {
4673 /* file */
4674 body = &reply->body.obj;
4675 }
4676 else if (reply->type == HTTP_REPLY_LOGFMT) {
4677 /* log-format string */
4678 body = alloc_trash_chunk();
4679 if (!body)
4680 goto fail_alloc;
4681 body->data = build_logline(s, body->area, body->size, &reply->body.fmt);
4682 }
4683 /* else no payload */
4684
4685 status = ultoa(reply->status);
4686 reason = http_get_reason(reply->status);
4687 slflags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_CLEN);
4688 if (!body || !b_data(body))
4689 slflags |= HTX_SL_F_BODYLESS;
4690 sl = htx_add_stline(htx, HTX_BLK_RES_SL, slflags, ist("HTTP/1.1"), ist(status), ist(reason));
4691 if (!sl)
4692 goto fail;
4693 sl->info.res.status = reply->status;
4694
4695 clen = (body ? ultoa(b_data(body)) : "0");
4696 ctype = reply->ctype;
4697
4698 if (!LIST_ISEMPTY(&reply->hdrs)) {
4699 struct http_reply_hdr *hdr;
4700 struct buffer *value = alloc_trash_chunk();
4701
4702 if (!value)
4703 goto fail;
4704
4705 list_for_each_entry(hdr, &reply->hdrs, list) {
4706 chunk_reset(value);
4707 value->data = build_logline(s, value->area, value->size, &hdr->value);
4708 if (b_data(value) && !htx_add_header(htx, hdr->name, ist2(b_head(value), b_data(value)))) {
4709 free_trash_chunk(value);
4710 goto fail;
4711 }
4712 chunk_reset(value);
4713 }
4714 free_trash_chunk(value);
4715 }
4716
4717 if (!htx_add_header(htx, ist("content-length"), ist(clen)) ||
4718 (body && b_data(body) && ctype && !htx_add_header(htx, ist("content-type"), ist(ctype))) ||
4719 !htx_add_endof(htx, HTX_BLK_EOH) ||
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004720 (body && b_data(body) && !htx_add_data_atonce(htx, ist2(b_head(body), b_data(body)))))
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004721 goto fail;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004722
4723 htx->flags |= HTX_FL_EOM;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004724 }
4725
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004726 leave:
4727 if (reply->type == HTTP_REPLY_LOGFMT)
4728 free_trash_chunk(body);
4729 return ret;
4730
4731 fail_alloc:
4732 if (!(s->flags & SF_ERR_MASK))
4733 s->flags |= SF_ERR_RESOURCE;
Christopher Faulet97e466c2020-05-15 15:12:47 +02004734 /* fall through */
4735 fail:
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004736 ret = -1;
4737 goto leave;
Christopher Faulet97e466c2020-05-15 15:12:47 +02004738}
4739
4740/* Send an http reply to the client. On success, it returns 0. If an error
Christopher Faulet40e6b552020-06-25 16:04:50 +02004741 * occurs -1 is returned and the response channel is truncated, removing this
4742 * way the faulty reply. This function may fail when the reply is formatted
4743 * (http_reply_to_htx) or when the reply is forwarded
4744 * (http_forward_proxy_resp). On the last case, it is because a
4745 * http-after-response rule fails.
Christopher Faulet97e466c2020-05-15 15:12:47 +02004746 */
4747int http_reply_message(struct stream *s, struct http_reply *reply)
4748{
4749 struct channel *res = &s->res;
4750 struct htx *htx = htx_from_buf(&res->buf);
4751
4752 if (s->txn->status == -1)
4753 s->txn->status = reply->status;
4754 channel_htx_truncate(res, htx);
4755
4756 if (http_reply_to_htx(s, htx, reply) == -1)
4757 goto fail;
4758
4759 htx_to_buf(htx, &s->res.buf);
4760 if (!http_forward_proxy_resp(s, 1))
4761 goto fail;
4762 return 0;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004763
4764 fail:
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004765 channel_htx_truncate(res, htx);
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004766 if (!(s->flags & SF_ERR_MASK))
4767 s->flags |= SF_ERR_PRXCOND;
Christopher Faulet97e466c2020-05-15 15:12:47 +02004768 return -1;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004769}
4770
Christopher Faulet304cc402019-07-15 15:46:28 +02004771/* Return the error message corresponding to si->err_type. It is assumed
4772 * that the server side is closed. Note that err_type is actually a
4773 * bitmask, where almost only aborts may be cumulated with other
4774 * values. We consider that aborted operations are more important
4775 * than timeouts or errors due to the fact that nobody else in the
4776 * logs might explain incomplete retries. All others should avoid
4777 * being cumulated. It should normally not be possible to have multiple
4778 * aborts at once, but just in case, the first one in sequence is reported.
4779 * Note that connection errors appearing on the second request of a keep-alive
4780 * connection are not reported since this allows the client to retry.
4781 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004782void http_return_srv_error(struct stream *s, struct stream_interface *si)
Christopher Faulet304cc402019-07-15 15:46:28 +02004783{
4784 int err_type = si->err_type;
4785
4786 /* set s->txn->status for http_error_message(s) */
4787 s->txn->status = 503;
4788
4789 if (err_type & SI_ET_QUEUE_ABRT)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004790 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_Q,
4791 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004792 else if (err_type & SI_ET_CONN_ABRT)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004793 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_C,
4794 (s->txn->flags & TX_NOT_FIRST) ? NULL :
4795 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004796 else if (err_type & SI_ET_QUEUE_TO)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004797 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_Q,
4798 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004799 else if (err_type & SI_ET_QUEUE_ERR)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004800 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_Q,
4801 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004802 else if (err_type & SI_ET_CONN_TO)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004803 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_C,
4804 (s->txn->flags & TX_NOT_FIRST) ? NULL :
4805 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004806 else if (err_type & SI_ET_CONN_ERR)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004807 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_C,
4808 (s->flags & SF_SRV_REUSED) ? NULL :
4809 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004810 else if (err_type & SI_ET_CONN_RES)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004811 http_server_error(s, si, SF_ERR_RESOURCE, SF_FINST_C,
4812 (s->txn->flags & TX_NOT_FIRST) ? NULL :
4813 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004814 else { /* SI_ET_CONN_OTHER and others */
4815 s->txn->status = 500;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004816 http_server_error(s, si, SF_ERR_INTERNAL, SF_FINST_C,
4817 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004818 }
4819}
4820
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004821
Christopher Faulet4a28a532019-03-01 11:19:40 +01004822/* Handle Expect: 100-continue for HTTP/1.1 messages if necessary. It returns 0
4823 * on success and -1 on error.
4824 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004825static int http_handle_expect_hdr(struct stream *s, struct htx *htx, struct http_msg *msg)
Christopher Faulet4a28a532019-03-01 11:19:40 +01004826{
4827 /* If we have HTTP/1.1 message with a body and Expect: 100-continue,
4828 * then we must send an HTTP/1.1 100 Continue intermediate response.
4829 */
4830 if (msg->msg_state == HTTP_MSG_BODY && (msg->flags & HTTP_MSGF_VER_11) &&
4831 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
4832 struct ist hdr = { .ptr = "Expect", .len = 6 };
4833 struct http_hdr_ctx ctx;
4834
4835 ctx.blk = NULL;
4836 /* Expect is allowed in 1.1, look for it */
4837 if (http_find_header(htx, hdr, &ctx, 0) &&
4838 unlikely(isteqi(ctx.value, ist2("100-continue", 12)))) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004839 if (http_reply_100_continue(s) == -1)
Christopher Faulet4a28a532019-03-01 11:19:40 +01004840 return -1;
4841 http_remove_header(htx, &ctx);
4842 }
4843 }
4844 return 0;
4845}
4846
Christopher Faulet23a3c792018-11-28 10:01:23 +01004847/* Send a 100-Continue response to the client. It returns 0 on success and -1
4848 * on error. The response channel is updated accordingly.
4849 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004850static int http_reply_100_continue(struct stream *s)
Christopher Faulet23a3c792018-11-28 10:01:23 +01004851{
4852 struct channel *res = &s->res;
4853 struct htx *htx = htx_from_buf(&res->buf);
4854 struct htx_sl *sl;
4855 unsigned int flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|
4856 HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
Christopher Faulet23a3c792018-11-28 10:01:23 +01004857
4858 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
4859 ist("HTTP/1.1"), ist("100"), ist("Continue"));
4860 if (!sl)
4861 goto fail;
4862 sl->info.res.status = 100;
4863
Christopher Faulet1d5ec092019-06-26 14:23:54 +02004864 if (!htx_add_endof(htx, HTX_BLK_EOH))
Christopher Faulet23a3c792018-11-28 10:01:23 +01004865 goto fail;
4866
Christopher Fauleta72a7e42020-01-28 09:28:11 +01004867 if (!http_forward_proxy_resp(s, 0))
4868 goto fail;
Christopher Faulet23a3c792018-11-28 10:01:23 +01004869 return 0;
4870
4871 fail:
4872 /* If an error occurred, remove the incomplete HTTP response from the
4873 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004874 channel_htx_truncate(res, htx);
Christopher Faulet23a3c792018-11-28 10:01:23 +01004875 return -1;
4876}
4877
Christopher Faulet12c51e22018-11-28 15:59:42 +01004878
Christopher Faulet0f226952018-10-22 09:29:56 +02004879/*
4880 * Capture headers from message <htx> according to header list <cap_hdr>, and
4881 * fill the <cap> pointers appropriately.
4882 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004883static void http_capture_headers(struct htx *htx, char **cap, struct cap_hdr *cap_hdr)
Christopher Faulet0f226952018-10-22 09:29:56 +02004884{
4885 struct cap_hdr *h;
4886 int32_t pos;
4887
Christopher Fauleta3f15502019-05-13 15:27:23 +02004888 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet0f226952018-10-22 09:29:56 +02004889 struct htx_blk *blk = htx_get_blk(htx, pos);
4890 enum htx_blk_type type = htx_get_blk_type(blk);
4891 struct ist n, v;
4892
4893 if (type == HTX_BLK_EOH)
4894 break;
4895 if (type != HTX_BLK_HDR)
4896 continue;
4897
4898 n = htx_get_blk_name(htx, blk);
4899
4900 for (h = cap_hdr; h; h = h->next) {
4901 if (h->namelen && (h->namelen == n.len) &&
4902 (strncasecmp(n.ptr, h->name, h->namelen) == 0)) {
4903 if (cap[h->index] == NULL)
4904 cap[h->index] =
4905 pool_alloc(h->pool);
4906
4907 if (cap[h->index] == NULL) {
4908 ha_alert("HTTP capture : out of memory.\n");
4909 break;
4910 }
4911
4912 v = htx_get_blk_value(htx, blk);
4913 if (v.len > h->len)
4914 v.len = h->len;
4915
4916 memcpy(cap[h->index], v.ptr, v.len);
4917 cap[h->index][v.len]=0;
4918 }
4919 }
4920 }
4921}
4922
Christopher Faulet0b6bdc52018-10-24 11:05:36 +02004923/* Delete a value in a header between delimiters <from> and <next>. The header
4924 * itself is delimited by <start> and <end> pointers. The number of characters
4925 * displaced is returned, and the pointer to the first delimiter is updated if
4926 * required. The function tries as much as possible to respect the following
4927 * principles :
4928 * - replace <from> delimiter by the <next> one unless <from> points to <start>,
4929 * in which case <next> is simply removed
4930 * - set exactly one space character after the new first delimiter, unless there
4931 * are not enough characters in the block being moved to do so.
4932 * - remove unneeded spaces before the previous delimiter and after the new
4933 * one.
4934 *
4935 * It is the caller's responsibility to ensure that :
4936 * - <from> points to a valid delimiter or <start> ;
4937 * - <next> points to a valid delimiter or <end> ;
4938 * - there are non-space chars before <from>.
4939 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004940static int http_del_hdr_value(char *start, char *end, char **from, char *next)
Christopher Faulet0b6bdc52018-10-24 11:05:36 +02004941{
4942 char *prev = *from;
4943
4944 if (prev == start) {
4945 /* We're removing the first value. eat the semicolon, if <next>
4946 * is lower than <end> */
4947 if (next < end)
4948 next++;
4949
4950 while (next < end && HTTP_IS_SPHT(*next))
4951 next++;
4952 }
4953 else {
4954 /* Remove useless spaces before the old delimiter. */
4955 while (HTTP_IS_SPHT(*(prev-1)))
4956 prev--;
4957 *from = prev;
4958
4959 /* copy the delimiter and if possible a space if we're
4960 * not at the end of the line.
4961 */
4962 if (next < end) {
4963 *prev++ = *next++;
4964 if (prev + 1 < next)
4965 *prev++ = ' ';
4966 while (next < end && HTTP_IS_SPHT(*next))
4967 next++;
4968 }
4969 }
4970 memmove(prev, next, end - next);
4971 return (prev - next);
4972}
4973
Christopher Faulet0f226952018-10-22 09:29:56 +02004974
4975/* Formats the start line of the request (without CRLF) and puts it in <str> and
Joseph Herlantc42c0e92018-11-25 10:43:27 -08004976 * return the written length. The line can be truncated if it exceeds <len>.
Christopher Faulet0f226952018-10-22 09:29:56 +02004977 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004978static size_t http_fmt_req_line(const struct htx_sl *sl, char *str, size_t len)
Christopher Faulet0f226952018-10-22 09:29:56 +02004979{
4980 struct ist dst = ist2(str, 0);
4981
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004982 if (istcat(&dst, htx_sl_req_meth(sl), len) == -1)
Christopher Faulet0f226952018-10-22 09:29:56 +02004983 goto end;
4984 if (dst.len + 1 > len)
4985 goto end;
4986 dst.ptr[dst.len++] = ' ';
4987
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004988 if (istcat(&dst, htx_sl_req_uri(sl), len) == -1)
Christopher Faulet0f226952018-10-22 09:29:56 +02004989 goto end;
4990 if (dst.len + 1 > len)
4991 goto end;
4992 dst.ptr[dst.len++] = ' ';
4993
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004994 istcat(&dst, htx_sl_req_vsn(sl), len);
Christopher Faulet0f226952018-10-22 09:29:56 +02004995 end:
4996 return dst.len;
4997}
4998
4999/*
5000 * Print a debug line with a start line.
5001 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005002static void http_debug_stline(const char *dir, struct stream *s, const struct htx_sl *sl)
Christopher Faulet0f226952018-10-22 09:29:56 +02005003{
5004 struct session *sess = strm_sess(s);
5005 int max;
5006
5007 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
5008 dir,
5009 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->handle.fd : -1,
5010 objt_cs(s->si[1].end) ? (unsigned short)objt_cs(s->si[1].end)->conn->handle.fd : -1);
5011
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005012 max = HTX_SL_P1_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02005013 UBOUND(max, trash.size - trash.data - 3);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005014 chunk_memcat(&trash, HTX_SL_P1_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02005015 trash.area[trash.data++] = ' ';
5016
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005017 max = HTX_SL_P2_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02005018 UBOUND(max, trash.size - trash.data - 2);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005019 chunk_memcat(&trash, HTX_SL_P2_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02005020 trash.area[trash.data++] = ' ';
5021
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005022 max = HTX_SL_P3_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02005023 UBOUND(max, trash.size - trash.data - 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005024 chunk_memcat(&trash, HTX_SL_P3_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02005025 trash.area[trash.data++] = '\n';
5026
Willy Tarreau2e8ab6b2020-03-14 11:03:20 +01005027 DISGUISE(write(1, trash.area, trash.data));
Christopher Faulet0f226952018-10-22 09:29:56 +02005028}
5029
5030/*
5031 * Print a debug line with a header.
5032 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005033static void http_debug_hdr(const char *dir, struct stream *s, const struct ist n, const struct ist v)
Christopher Faulet0f226952018-10-22 09:29:56 +02005034{
5035 struct session *sess = strm_sess(s);
5036 int max;
5037
5038 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
5039 dir,
5040 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->handle.fd : -1,
5041 objt_cs(s->si[1].end) ? (unsigned short)objt_cs(s->si[1].end)->conn->handle.fd : -1);
5042
5043 max = n.len;
5044 UBOUND(max, trash.size - trash.data - 3);
5045 chunk_memcat(&trash, n.ptr, max);
5046 trash.area[trash.data++] = ':';
5047 trash.area[trash.data++] = ' ';
5048
5049 max = v.len;
5050 UBOUND(max, trash.size - trash.data - 1);
5051 chunk_memcat(&trash, v.ptr, max);
5052 trash.area[trash.data++] = '\n';
5053
Willy Tarreau2e8ab6b2020-03-14 11:03:20 +01005054 DISGUISE(write(1, trash.area, trash.data));
Christopher Faulet0f226952018-10-22 09:29:56 +02005055}
5056
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005057/* Allocate a new HTTP transaction for stream <s> unless there is one already.
5058 * In case of allocation failure, everything allocated is freed and NULL is
5059 * returned. Otherwise the new transaction is assigned to the stream and
5060 * returned.
5061 */
5062struct http_txn *http_alloc_txn(struct stream *s)
5063{
5064 struct http_txn *txn = s->txn;
5065
5066 if (txn)
5067 return txn;
5068
5069 txn = pool_alloc(pool_head_http_txn);
5070 if (!txn)
5071 return txn;
5072
5073 s->txn = txn;
5074 return txn;
5075}
5076
5077void http_txn_reset_req(struct http_txn *txn)
5078{
Christopher Faulet1aea50e2020-01-17 16:03:53 +01005079 txn->req.flags = 0;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005080 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
5081}
5082
5083void http_txn_reset_res(struct http_txn *txn)
5084{
Christopher Faulet1aea50e2020-01-17 16:03:53 +01005085 txn->rsp.flags = 0;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005086 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
5087}
5088
5089/*
Christopher Faulet75f619a2021-03-08 19:12:58 +01005090 * Create and initialize a new HTTP transaction for stream <s>. This should be
5091 * used before processing any new request. It returns the transaction or NLULL
5092 * on error.
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005093 */
Christopher Faulet75f619a2021-03-08 19:12:58 +01005094struct http_txn *http_create_txn(struct stream *s)
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005095{
Christopher Faulet75f619a2021-03-08 19:12:58 +01005096 struct http_txn *txn;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005097 struct conn_stream *cs = objt_cs(s->si[0].end);
5098
Christopher Faulet75f619a2021-03-08 19:12:58 +01005099 txn = pool_alloc(pool_head_http_txn);
5100 if (!txn)
5101 return NULL;
5102 s->txn = txn;
5103
Christopher Fauletda831fa2020-10-06 17:58:43 +02005104 txn->flags = ((cs && cs->flags & CS_FL_NOT_FIRST) ? TX_NOT_FIRST : 0);
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005105 txn->status = -1;
Christopher Faulet5cb513a2020-05-13 17:56:56 +02005106 txn->http_reply = NULL;
Willy Tarreau8b507582020-02-25 09:35:07 +01005107 write_u32(txn->cache_hash, 0);
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005108
5109 txn->cookie_first_date = 0;
5110 txn->cookie_last_date = 0;
5111
5112 txn->srv_cookie = NULL;
5113 txn->cli_cookie = NULL;
5114 txn->uri = NULL;
5115
5116 http_txn_reset_req(txn);
5117 http_txn_reset_res(txn);
5118
5119 txn->req.chn = &s->req;
5120 txn->rsp.chn = &s->res;
5121
5122 txn->auth.method = HTTP_AUTH_UNKNOWN;
5123
5124 vars_init(&s->vars_txn, SCOPE_TXN);
5125 vars_init(&s->vars_reqres, SCOPE_REQ);
Christopher Faulet75f619a2021-03-08 19:12:58 +01005126
5127 return txn;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005128}
5129
5130/* to be used at the end of a transaction */
Christopher Faulet75f619a2021-03-08 19:12:58 +01005131void http_destroy_txn(struct stream *s)
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005132{
5133 struct http_txn *txn = s->txn;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005134
5135 /* these ones will have been dynamically allocated */
5136 pool_free(pool_head_requri, txn->uri);
5137 pool_free(pool_head_capture, txn->cli_cookie);
5138 pool_free(pool_head_capture, txn->srv_cookie);
Tim Duesterhusa17e6622020-03-05 20:19:02 +01005139 pool_free(pool_head_uniqueid, s->unique_id.ptr);
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005140
Tim Duesterhusa17e6622020-03-05 20:19:02 +01005141 s->unique_id = IST_NULL;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005142 txn->uri = NULL;
5143 txn->srv_cookie = NULL;
5144 txn->cli_cookie = NULL;
5145
Christopher Faulet59399252019-11-07 14:27:52 +01005146 if (!LIST_ISEMPTY(&s->vars_txn.head))
5147 vars_prune(&s->vars_txn, s->sess, s);
5148 if (!LIST_ISEMPTY(&s->vars_reqres.head))
5149 vars_prune(&s->vars_reqres, s->sess, s);
Christopher Faulet75f619a2021-03-08 19:12:58 +01005150
5151 pool_free(pool_head_http_txn, txn);
5152 s->txn = NULL;
Christopher Faulet59399252019-11-07 14:27:52 +01005153}
5154
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005155
5156DECLARE_POOL(pool_head_http_txn, "http_txn", sizeof(struct http_txn));
Christopher Faulet0f226952018-10-22 09:29:56 +02005157
Christopher Fauletf4eb75d2018-10-11 15:55:07 +02005158__attribute__((constructor))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005159static void __http_protocol_init(void)
Christopher Fauletf4eb75d2018-10-11 15:55:07 +02005160{
5161}
5162
5163
5164/*
5165 * Local variables:
5166 * c-indent-level: 8
5167 * c-basic-offset: 8
5168 * End:
5169 */