blob: bcc321ba572ba6aacac5039c48aae7916a12aacb [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)) {
Christopher Fauletb02d5f02021-10-29 14:37:07 +0200870 /* Be sure to drain all data from the request channel */
871 channel_htx_erase(req, htxbuf(&req->buf));
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100872 DBG_TRACE_DEVEL("waiting for tarpit timeout expiry",
873 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200874 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100875 }
876
Christopher Faulete0768eb2018-10-03 16:38:02 +0200877
878 /* We will set the queue timer to the time spent, just for
879 * logging purposes. We fake a 500 server error, so that the
880 * attacker will not suspect his connection has been tarpitted.
881 * It will not cause trouble to the logs because we can exclude
882 * the tarpitted connections by filtering on the 'PT' status flags.
883 */
884 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
885
Christopher Faulet8dfeccf2020-05-15 14:16:29 +0200886 http_reply_and_close(s, txn->status, (!(req->flags & CF_READ_ERROR) ? http_error_message(s) : NULL));
Christopher Faulet5cb513a2020-05-13 17:56:56 +0200887
Christopher Faulete0768eb2018-10-03 16:38:02 +0200888 if (!(s->flags & SF_ERR_MASK))
889 s->flags |= SF_ERR_PRXCOND;
890 if (!(s->flags & SF_FINST_MASK))
891 s->flags |= SF_FINST_T;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100892
893 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200894 return 0;
895}
896
897/* This function is an analyser which waits for the HTTP request body. It waits
898 * for either the buffer to be full, or the full advertised contents to have
899 * reached the buffer. It must only be called after the standard HTTP request
900 * processing has occurred, because it expects the request to be parsed and will
901 * look for the Expect header. It may send a 100-Continue interim response. It
902 * takes in input any state starting from HTTP_MSG_BODY and leaves with one of
903 * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it
904 * needs to read more data, or 1 once it has completed its analysis.
905 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200906int http_wait_for_request_body(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200907{
908 struct session *sess = s->sess;
909 struct http_txn *txn = s->txn;
910 struct http_msg *msg = &s->txn->req;
911
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100912 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Fauletf76ebe82018-10-24 11:16:22 +0200913
Christopher Fauletf76ebe82018-10-24 11:16:22 +0200914
Christopher Faulet021a8e42021-03-29 10:46:38 +0200915 switch (http_wait_for_msg_body(s, req, s->be->timeout.httpreq, 0)) {
916 case HTTP_RULE_RES_CONT:
917 goto http_end;
918 case HTTP_RULE_RES_YIELD:
919 goto missing_data_or_waiting;
920 case HTTP_RULE_RES_BADREQ:
Willy Tarreau4236f032019-03-05 10:43:32 +0100921 goto return_bad_req;
Christopher Faulet021a8e42021-03-29 10:46:38 +0200922 case HTTP_RULE_RES_ERROR:
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200923 goto return_int_err;
Christopher Faulet021a8e42021-03-29 10:46:38 +0200924 case HTTP_RULE_RES_ABRT:
Christopher Fauletb8a53712019-12-16 11:29:38 +0100925 goto return_prx_cond;
Christopher Faulet021a8e42021-03-29 10:46:38 +0200926 default:
927 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200928 }
929
930 http_end:
931 /* The situation will not evolve, so let's give up on the analysis. */
932 s->logs.tv_request = now; /* update the request timer to reflect full request */
933 req->analysers &= ~an_bit;
934 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100935 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200936 return 1;
937
Christopher Faulet021a8e42021-03-29 10:46:38 +0200938 missing_data_or_waiting:
939 channel_dont_connect(req);
940 DBG_TRACE_DEVEL("waiting for more data",
941 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
942 return 0;
943
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200944 return_int_err:
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200945 txn->status = 500;
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200946 if (!(s->flags & SF_ERR_MASK))
947 s->flags |= SF_ERR_INTERNAL;
Willy Tarreau4781b152021-04-06 13:53:36 +0200948 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100949 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau4781b152021-04-06 13:53:36 +0200950 _HA_ATOMIC_INC(&s->be->be_counters.internal_errors);
William Lallemand36119de2021-03-08 15:26:48 +0100951 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200952 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Faulet021a8e42021-03-29 10:46:38 +0200953 goto return_prx_err;
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200954
Christopher Faulete0768eb2018-10-03 16:38:02 +0200955 return_bad_req: /* let's centralize all bad requests */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200956 txn->status = 400;
Willy Tarreau4781b152021-04-06 13:53:36 +0200957 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req);
William Lallemand36119de2021-03-08 15:26:48 +0100958 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200959 _HA_ATOMIC_INC(&sess->listener->counters->failed_req);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100960 /* fall through */
961
Christopher Faulet021a8e42021-03-29 10:46:38 +0200962 return_prx_err:
Christopher Fauletb8a53712019-12-16 11:29:38 +0100963 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet021a8e42021-03-29 10:46:38 +0200964 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200965
Christopher Faulet021a8e42021-03-29 10:46:38 +0200966 return_prx_cond:
Christopher Faulete0768eb2018-10-03 16:38:02 +0200967 if (!(s->flags & SF_ERR_MASK))
968 s->flags |= SF_ERR_PRXCOND;
969 if (!(s->flags & SF_FINST_MASK))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100970 s->flags |= (msg->msg_state < HTTP_MSG_DATA ? SF_FINST_R : SF_FINST_D);
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200971
Christopher Faulete0768eb2018-10-03 16:38:02 +0200972 req->analysers &= AN_REQ_FLT_END;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100973 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100974 DBG_TRACE_DEVEL("leaving on error",
975 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200976 return 0;
977}
978
979/* This function is an analyser which forwards request body (including chunk
980 * sizes if any). It is called as soon as we must forward, even if we forward
981 * zero byte. The only situation where it must not be called is when we're in
982 * tunnel mode and we want to forward till the close. It's used both to forward
983 * remaining data and to resync after end of body. It expects the msg_state to
984 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
985 * read more data, or 1 once we can go on with next request or end the stream.
986 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
987 * bytes of pending data + the headers if not already done.
988 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200989int http_request_forward_body(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200990{
991 struct session *sess = s->sess;
992 struct http_txn *txn = s->txn;
Christopher Faulet9768c262018-10-22 09:34:31 +0200993 struct http_msg *msg = &txn->req;
994 struct htx *htx;
Christopher Faulet93e02d82019-03-08 14:18:50 +0100995 short status = 0;
Christopher Fauletaed82cf2018-11-30 22:22:32 +0100996 int ret;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200997
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100998 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200999
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001000 htx = htxbuf(&req->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001001
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001002 if (htx->flags & HTX_FL_PARSING_ERROR)
1003 goto return_bad_req;
1004 if (htx->flags & HTX_FL_PROCESSING_ERROR)
1005 goto return_int_err;
1006
Christopher Faulete0768eb2018-10-03 16:38:02 +02001007 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
1008 ((req->flags & CF_SHUTW) && (req->to_forward || co_data(req)))) {
1009 /* Output closed while we were sending data. We must abort and
1010 * wake the other side up.
Christopher Fauletf506d962021-04-27 10:56:28 +02001011 *
1012 * If we have finished to send the request and the response is
1013 * still in progress, don't catch write error on the request
1014 * side if it is in fact a read error on the server side.
Christopher Faulete0768eb2018-10-03 16:38:02 +02001015 */
Christopher Fauletf506d962021-04-27 10:56:28 +02001016 if (msg->msg_state == HTTP_MSG_DONE && (s->res.flags & CF_READ_ERROR) && s->res.analysers)
1017 return 0;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001018
Olivier Houchard29cac3c2019-07-12 15:48:58 +02001019 /* Don't abort yet if we had L7 retries activated and it
1020 * was a write error, we may recover.
1021 */
1022 if (!(req->flags & (CF_READ_ERROR | CF_READ_TIMEOUT)) &&
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001023 (s->si[1].flags & SI_FL_L7_RETRY)) {
1024 DBG_TRACE_DEVEL("leaving on L7 retry",
1025 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Olivier Houchard29cac3c2019-07-12 15:48:58 +02001026 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001027 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001028 msg->msg_state = HTTP_MSG_ERROR;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001029 http_end_request(s);
1030 http_end_response(s);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001031 DBG_TRACE_DEVEL("leaving on error",
1032 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001033 return 1;
1034 }
1035
1036 /* Note that we don't have to send 100-continue back because we don't
1037 * need the data to complete our job, and it's up to the server to
1038 * decide whether to return 100, 417 or anything else in return of
1039 * an "Expect: 100-continue" header.
1040 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001041 if (msg->msg_state == HTTP_MSG_BODY)
1042 msg->msg_state = HTTP_MSG_DATA;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001043
Christopher Faulete0768eb2018-10-03 16:38:02 +02001044 /* in most states, we should abort in case of early close */
1045 channel_auto_close(req);
1046
1047 if (req->to_forward) {
Christopher Faulet66af0b22019-03-22 14:54:52 +01001048 if (req->to_forward == CHN_INFINITE_FORWARD) {
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001049 if (req->flags & CF_EOI)
1050 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet66af0b22019-03-22 14:54:52 +01001051 }
1052 else {
1053 /* We can't process the buffer's contents yet */
1054 req->flags |= CF_WAKE_WRITE;
1055 goto missing_data_or_waiting;
1056 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001057 }
1058
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001059 if (msg->msg_state >= HTTP_MSG_ENDING)
1060 goto ending;
1061
1062 if (txn->meth == HTTP_METH_CONNECT) {
1063 msg->msg_state = HTTP_MSG_ENDING;
1064 goto ending;
1065 }
1066
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001067 /* Forward input data. We get it by removing all outgoing data not
1068 * forwarded yet from HTX data size. If there are some data filters, we
1069 * let them decide the amount of data to forward.
Christopher Faulet9768c262018-10-22 09:34:31 +02001070 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001071 if (HAS_REQ_DATA_FILTERS(s)) {
1072 ret = flt_http_payload(s, msg, htx->data);
1073 if (ret < 0)
1074 goto return_bad_req;
Christopher Faulet421e7692019-06-13 11:16:45 +02001075 c_adv(req, ret);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001076 }
1077 else {
Christopher Faulet421e7692019-06-13 11:16:45 +02001078 c_adv(req, htx->data - co_data(req));
Christopher Faulet66af0b22019-03-22 14:54:52 +01001079 if (msg->flags & HTTP_MSGF_XFER_LEN)
1080 channel_htx_forward_forever(req, htx);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001081 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001082
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001083 if (htx->data != co_data(req))
1084 goto missing_data_or_waiting;
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001085
Christopher Faulet9768c262018-10-22 09:34:31 +02001086 /* Check if the end-of-message is reached and if so, switch the message
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001087 * in HTTP_MSG_ENDING state. Then if all data was marked to be
1088 * forwarded, set the state to HTTP_MSG_DONE.
Christopher Faulet9768c262018-10-22 09:34:31 +02001089 */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001090 if (!(htx->flags & HTX_FL_EOM))
Christopher Faulet9768c262018-10-22 09:34:31 +02001091 goto missing_data_or_waiting;
1092
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001093 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet9768c262018-10-22 09:34:31 +02001094
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001095 ending:
Christopher Faulet2151cdd2020-07-22 16:34:59 +02001096 req->flags &= ~CF_EXPECT_MORE; /* no more data are expected */
1097
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001098 /* other states, ENDING...TUNNEL */
1099 if (msg->msg_state >= HTTP_MSG_DONE)
1100 goto done;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001101
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001102 if (HAS_REQ_DATA_FILTERS(s)) {
1103 ret = flt_http_end(s, msg);
1104 if (ret <= 0) {
1105 if (!ret)
1106 goto missing_data_or_waiting;
1107 goto return_bad_req;
1108 }
1109 }
1110
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001111 if (txn->meth == HTTP_METH_CONNECT)
1112 msg->msg_state = HTTP_MSG_TUNNEL;
1113 else {
1114 msg->msg_state = HTTP_MSG_DONE;
1115 req->to_forward = 0;
1116 }
1117
1118 done:
1119 /* we don't want to forward closes on DONE except in tunnel mode. */
1120 if (!(txn->flags & TX_CON_WANT_TUN))
1121 channel_dont_close(req);
1122
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001123 http_end_request(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001124 if (!(req->analysers & an_bit)) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001125 http_end_response(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001126 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
1127 if (req->flags & CF_SHUTW) {
1128 /* request errors are most likely due to the
1129 * server aborting the transfer. */
Christopher Faulet93e02d82019-03-08 14:18:50 +01001130 goto return_srv_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001131 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001132 goto return_bad_req;
1133 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001134 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001135 return 1;
1136 }
1137
1138 /* If "option abortonclose" is set on the backend, we want to monitor
1139 * the client's connection and forward any shutdown notification to the
1140 * server, which will decide whether to close or to go on processing the
1141 * request. We only do that in tunnel mode, and not in other modes since
1142 * it can be abused to exhaust source ports. */
Christopher Faulet769d0e92019-03-22 14:23:18 +01001143 if (s->be->options & PR_O_ABRT_CLOSE) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001144 channel_auto_read(req);
Christopher Fauletc41547b2019-07-16 14:32:23 +02001145 if ((req->flags & (CF_SHUTR|CF_READ_NULL)) && !(txn->flags & TX_CON_WANT_TUN))
Christopher Faulete0768eb2018-10-03 16:38:02 +02001146 s->si[1].flags |= SI_FL_NOLINGER;
1147 channel_auto_close(req);
1148 }
1149 else if (s->txn->meth == HTTP_METH_POST) {
1150 /* POST requests may require to read extra CRLF sent by broken
1151 * browsers and which could cause an RST to be sent upon close
1152 * on some systems (eg: Linux). */
1153 channel_auto_read(req);
1154 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001155 DBG_TRACE_DEVEL("waiting for the end of the HTTP txn",
1156 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001157 return 0;
1158
1159 missing_data_or_waiting:
1160 /* stop waiting for data if the input is closed before the end */
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001161 if (msg->msg_state < HTTP_MSG_ENDING && req->flags & CF_SHUTR)
Christopher Faulet93e02d82019-03-08 14:18:50 +01001162 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001163
1164 waiting:
1165 /* waiting for the last bits to leave the buffer */
1166 if (req->flags & CF_SHUTW)
Christopher Faulet93e02d82019-03-08 14:18:50 +01001167 goto return_srv_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001168
1169 /* When TE: chunked is used, we need to get there again to parse remaining
1170 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
1171 * And when content-length is used, we never want to let the possible
1172 * shutdown be forwarded to the other side, as the state machine will
1173 * take care of it once the client responds. It's also important to
1174 * prevent TIME_WAITs from accumulating on the backend side, and for
1175 * HTTP/2 where the last frame comes with a shutdown.
1176 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001177 if (msg->flags & HTTP_MSGF_XFER_LEN)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001178 channel_dont_close(req);
1179
1180 /* We know that more data are expected, but we couldn't send more that
1181 * what we did. So we always set the CF_EXPECT_MORE flag so that the
1182 * system knows it must not set a PUSH on this first part. Interactive
1183 * modes are already handled by the stream sock layer. We must not do
1184 * this in content-length mode because it could present the MSG_MORE
1185 * flag with the last block of forwarded data, which would cause an
1186 * additional delay to be observed by the receiver.
1187 */
Christopher Faulet2151cdd2020-07-22 16:34:59 +02001188 if (HAS_REQ_DATA_FILTERS(s))
Christopher Faulete0768eb2018-10-03 16:38:02 +02001189 req->flags |= CF_EXPECT_MORE;
1190
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001191 DBG_TRACE_DEVEL("waiting for more data to forward",
1192 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001193 return 0;
1194
Christopher Faulet93e02d82019-03-08 14:18:50 +01001195 return_cli_abort:
Willy Tarreau4781b152021-04-06 13:53:36 +02001196 _HA_ATOMIC_INC(&sess->fe->fe_counters.cli_aborts);
1197 _HA_ATOMIC_INC(&s->be->be_counters.cli_aborts);
William Lallemand36119de2021-03-08 15:26:48 +01001198 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001199 _HA_ATOMIC_INC(&sess->listener->counters->cli_aborts);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001200 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001201 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.cli_aborts);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001202 if (!(s->flags & SF_ERR_MASK))
1203 s->flags |= SF_ERR_CLICL;
1204 status = 400;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001205 goto return_prx_cond;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001206
1207 return_srv_abort:
Willy Tarreau4781b152021-04-06 13:53:36 +02001208 _HA_ATOMIC_INC(&sess->fe->fe_counters.srv_aborts);
1209 _HA_ATOMIC_INC(&s->be->be_counters.srv_aborts);
William Lallemand36119de2021-03-08 15:26:48 +01001210 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001211 _HA_ATOMIC_INC(&sess->listener->counters->srv_aborts);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001212 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001213 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.srv_aborts);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001214 if (!(s->flags & SF_ERR_MASK))
1215 s->flags |= SF_ERR_SRVCL;
1216 status = 502;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001217 goto return_prx_cond;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001218
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001219 return_int_err:
1220 if (!(s->flags & SF_ERR_MASK))
1221 s->flags |= SF_ERR_INTERNAL;
Willy Tarreau4781b152021-04-06 13:53:36 +02001222 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
1223 _HA_ATOMIC_INC(&s->be->be_counters.internal_errors);
William Lallemand36119de2021-03-08 15:26:48 +01001224 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001225 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001226 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001227 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.internal_errors);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001228 status = 500;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001229 goto return_prx_cond;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001230
Christopher Faulet93e02d82019-03-08 14:18:50 +01001231 return_bad_req:
Willy Tarreau4781b152021-04-06 13:53:36 +02001232 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req);
William Lallemand36119de2021-03-08 15:26:48 +01001233 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001234 _HA_ATOMIC_INC(&sess->listener->counters->failed_req);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001235 status = 400;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001236 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001237
Christopher Fauletb8a53712019-12-16 11:29:38 +01001238 return_prx_cond:
Christopher Faulet9768c262018-10-22 09:34:31 +02001239 if (txn->status > 0) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001240 /* Note: we don't send any error if some data were already sent */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001241 http_reply_and_close(s, txn->status, NULL);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001242 } else {
Christopher Faulet93e02d82019-03-08 14:18:50 +01001243 txn->status = status;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001244 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001245 }
Christopher Fauletb8a53712019-12-16 11:29:38 +01001246 if (!(s->flags & SF_ERR_MASK))
1247 s->flags |= SF_ERR_PRXCOND;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001248 if (!(s->flags & SF_FINST_MASK))
1249 s->flags |= ((txn->rsp.msg_state < HTTP_MSG_ERROR) ? SF_FINST_H : SF_FINST_D);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001250 DBG_TRACE_DEVEL("leaving on error ",
1251 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001252 return 0;
1253}
1254
Olivier Houcharda254a372019-04-05 15:30:12 +02001255/* Reset the stream and the backend stream_interface to a situation suitable for attemption connection */
1256/* Returns 0 if we can attempt to retry, -1 otherwise */
1257static __inline int do_l7_retry(struct stream *s, struct stream_interface *si)
1258{
Christopher Faulet5bf85852021-05-21 13:46:14 +02001259 struct channel *req, *res;
1260 int co_data;
Olivier Houcharda254a372019-04-05 15:30:12 +02001261
1262 si->conn_retries--;
1263 if (si->conn_retries < 0)
Christopher Faulet043cdb22021-05-26 10:31:06 +02001264 return -1;
Christopher Faulet5b82cc52020-10-12 15:18:50 +02001265
Christopher Faulete763c8c2021-05-05 18:23:59 +02001266 if (objt_server(s->target)) {
1267 if (s->flags & SF_CURR_SESS) {
1268 s->flags &= ~SF_CURR_SESS;
1269 _HA_ATOMIC_DEC(&__objt_server(s->target)->cur_sess);
1270 }
Willy Tarreau4781b152021-04-06 13:53:36 +02001271 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.retries);
Christopher Faulete763c8c2021-05-05 18:23:59 +02001272 }
Willy Tarreau4781b152021-04-06 13:53:36 +02001273 _HA_ATOMIC_INC(&s->be->be_counters.retries);
Willy Tarreau223995e2019-05-04 10:38:31 +02001274
Christopher Faulet5bf85852021-05-21 13:46:14 +02001275 req = &s->req;
1276 res = &s->res;
Olivier Houcharda254a372019-04-05 15:30:12 +02001277 /* Remove any write error from the request, and read error from the response */
1278 req->flags &= ~(CF_WRITE_ERROR | CF_WRITE_TIMEOUT | CF_SHUTW | CF_SHUTW_NOW);
1279 res->flags &= ~(CF_READ_ERROR | CF_READ_TIMEOUT | CF_SHUTR | CF_EOI | CF_READ_NULL | CF_SHUTR_NOW);
Christopher Faulet42d4ee12022-01-04 10:56:03 +01001280 res->analysers &= AN_RES_FLT_END;
Olivier Houcharda254a372019-04-05 15:30:12 +02001281 si->flags &= ~(SI_FL_ERR | SI_FL_EXP | SI_FL_RXBLK_SHUT);
Christopher Faulet30aa0da2021-05-05 21:05:09 +02001282 si->err_type = SI_ET_NONE;
1283 s->flags &= ~(SF_ERR_MASK | SF_FINST_MASK);
Olivier Houchard4bd58672019-07-12 16:16:59 +02001284 stream_choose_redispatch(s);
Olivier Houcharda254a372019-04-05 15:30:12 +02001285 si->exp = TICK_ETERNITY;
1286 res->rex = TICK_ETERNITY;
1287 res->to_forward = 0;
1288 res->analyse_exp = TICK_ETERNITY;
1289 res->total = 0;
Olivier Houcharda254a372019-04-05 15:30:12 +02001290 si_release_endpoint(&s->si[1]);
Olivier Houcharda254a372019-04-05 15:30:12 +02001291
Christopher Faulet5bf85852021-05-21 13:46:14 +02001292 b_free(&req->buf);
1293 /* Swap the L7 buffer with the channel buffer */
1294 /* We know we stored the co_data as b_data, so get it there */
1295 co_data = b_data(&si->l7_buffer);
1296 b_set_data(&si->l7_buffer, b_size(&si->l7_buffer));
1297 b_xfer(&req->buf, &si->l7_buffer, b_data(&si->l7_buffer));
1298 co_set_data(req, co_data);
Christopher Faulet5b82cc52020-10-12 15:18:50 +02001299
Ilya Shipitsinacf84592021-02-06 22:29:08 +05001300 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 +02001301
Olivier Houcharda254a372019-04-05 15:30:12 +02001302 b_reset(&res->buf);
1303 co_set_data(res, 0);
1304 return 0;
1305}
1306
Christopher Faulete0768eb2018-10-03 16:38:02 +02001307/* This stream analyser waits for a complete HTTP response. It returns 1 if the
1308 * processing can continue on next analysers, or zero if it either needs more
1309 * data or wants to immediately abort the response (eg: timeout, error, ...). It
1310 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->res.analysers
1311 * when it has nothing left to do, and may remove any analyser when it wants to
1312 * abort.
1313 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001314int http_wait_for_response(struct stream *s, struct channel *rep, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001315{
Christopher Faulet9768c262018-10-22 09:34:31 +02001316 /*
1317 * We will analyze a complete HTTP response to check the its syntax.
1318 *
1319 * Once the start line and all headers are received, we may perform a
1320 * capture of the error (if any), and we will set a few fields. We also
1321 * logging and finally headers capture.
1322 */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001323 struct session *sess = s->sess;
1324 struct http_txn *txn = s->txn;
1325 struct http_msg *msg = &txn->rsp;
Christopher Faulet9768c262018-10-22 09:34:31 +02001326 struct htx *htx;
Olivier Houcharda254a372019-04-05 15:30:12 +02001327 struct stream_interface *si_b = &s->si[1];
Christopher Faulet61608322018-11-23 16:23:45 +01001328 struct connection *srv_conn;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001329 struct htx_sl *sl;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001330 int n;
1331
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001332 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001333
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001334 htx = htxbuf(&rep->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001335
Willy Tarreau4236f032019-03-05 10:43:32 +01001336 /* Parsing errors are caught here */
1337 if (htx->flags & HTX_FL_PARSING_ERROR)
1338 goto return_bad_res;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001339 if (htx->flags & HTX_FL_PROCESSING_ERROR)
1340 goto return_int_err;
Willy Tarreau4236f032019-03-05 10:43:32 +01001341
Christopher Faulete0768eb2018-10-03 16:38:02 +02001342 /*
1343 * Now we quickly check if we have found a full valid response.
1344 * If not so, we check the FD and buffer states before leaving.
1345 * A full response is indicated by the fact that we have seen
1346 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
1347 * responses are checked first.
1348 *
1349 * Depending on whether the client is still there or not, we
1350 * may send an error response back or not. Note that normally
1351 * we should only check for HTTP status there, and check I/O
1352 * errors somewhere else.
1353 */
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001354 next_one:
Christopher Faulet29f17582019-05-23 11:03:26 +02001355 if (unlikely(htx_is_empty(htx) || htx->first == -1)) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001356 /* 1: have we encountered a read error ? */
1357 if (rep->flags & CF_READ_ERROR) {
Olivier Houchard865d8392019-05-03 22:46:27 +02001358 struct connection *conn = NULL;
1359
Olivier Houchard865d8392019-05-03 22:46:27 +02001360 if (objt_cs(s->si[1].end))
Willy Tarreau66182592021-12-06 07:01:02 +00001361 conn = __objt_cs(s->si[1].end)->conn;
Olivier Houchard865d8392019-05-03 22:46:27 +02001362
Christopher Fauletb1875342021-05-26 12:15:37 +02001363 /* Perform a L7 retry because server refuses the early data. */
1364 if ((si_b->flags & SI_FL_L7_RETRY) &&
1365 (s->be->retry_type & PR_RE_EARLY_ERROR) &&
1366 conn && conn->err_code == CO_ER_SSL_EARLY_FAILED &&
1367 do_l7_retry(s, si_b) == 0) {
1368 DBG_TRACE_DEVEL("leaving on L7 retry",
1369 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
1370 return 0;
1371 }
1372
Olivier Houchard6db16992019-05-17 15:40:49 +02001373 if (txn->flags & TX_NOT_FIRST)
1374 goto abort_keep_alive;
1375
Willy Tarreau4781b152021-04-06 13:53:36 +02001376 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001377 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001378 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001379 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001380 }
1381
Christopher Fauletb1875342021-05-26 12:15:37 +02001382 /* if the server refused the early data, just send a 425 */
1383 if (conn && conn->err_code == CO_ER_SSL_EARLY_FAILED)
Olivier Houchard865d8392019-05-03 22:46:27 +02001384 txn->status = 425;
Christopher Fauletb1875342021-05-26 12:15:37 +02001385 else {
1386 txn->status = 502;
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001387 stream_inc_http_fail_ctr(s);
Christopher Fauletb1875342021-05-26 12:15:37 +02001388 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001389
1390 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001391 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001392
1393 if (!(s->flags & SF_ERR_MASK))
1394 s->flags |= SF_ERR_SRVCL;
1395 if (!(s->flags & SF_FINST_MASK))
1396 s->flags |= SF_FINST_H;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001397 DBG_TRACE_DEVEL("leaving on error",
1398 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001399 return 0;
1400 }
1401
Christopher Faulet9768c262018-10-22 09:34:31 +02001402 /* 2: read timeout : return a 504 to the client. */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001403 else if (rep->flags & CF_READ_TIMEOUT) {
Olivier Houcharda254a372019-04-05 15:30:12 +02001404 if ((si_b->flags & SI_FL_L7_RETRY) &&
1405 (s->be->retry_type & PR_RE_TIMEOUT)) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001406 if (co_data(rep) || do_l7_retry(s, si_b) == 0) {
1407 DBG_TRACE_DEVEL("leaving on L7 retry",
1408 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharda254a372019-04-05 15:30:12 +02001409 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001410 }
Olivier Houcharda254a372019-04-05 15:30:12 +02001411 }
Willy Tarreau4781b152021-04-06 13:53:36 +02001412 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001413 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001414 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001415 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001416 }
1417
Christopher Faulete0768eb2018-10-03 16:38:02 +02001418 txn->status = 504;
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001419 stream_inc_http_fail_ctr(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001420 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001421 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001422
1423 if (!(s->flags & SF_ERR_MASK))
1424 s->flags |= SF_ERR_SRVTO;
1425 if (!(s->flags & SF_FINST_MASK))
1426 s->flags |= SF_FINST_H;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001427 DBG_TRACE_DEVEL("leaving on error",
1428 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001429 return 0;
1430 }
1431
Christopher Faulet9768c262018-10-22 09:34:31 +02001432 /* 3: client abort with an abortonclose */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001433 else if ((rep->flags & CF_SHUTR) && ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001434 _HA_ATOMIC_INC(&sess->fe->fe_counters.cli_aborts);
1435 _HA_ATOMIC_INC(&s->be->be_counters.cli_aborts);
William Lallemand36119de2021-03-08 15:26:48 +01001436 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001437 _HA_ATOMIC_INC(&sess->listener->counters->cli_aborts);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001438 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001439 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.cli_aborts);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001440
Christopher Faulete0768eb2018-10-03 16:38:02 +02001441 txn->status = 400;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001442 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001443
1444 if (!(s->flags & SF_ERR_MASK))
1445 s->flags |= SF_ERR_CLICL;
1446 if (!(s->flags & SF_FINST_MASK))
1447 s->flags |= SF_FINST_H;
1448
1449 /* process_stream() will take care of the error */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001450 DBG_TRACE_DEVEL("leaving on error",
1451 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001452 return 0;
1453 }
1454
Christopher Faulet9768c262018-10-22 09:34:31 +02001455 /* 4: close from server, capture the response if the server has started to respond */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001456 else if (rep->flags & CF_SHUTR) {
Olivier Houcharda254a372019-04-05 15:30:12 +02001457 if ((si_b->flags & SI_FL_L7_RETRY) &&
1458 (s->be->retry_type & PR_RE_DISCONNECTED)) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001459 if (co_data(rep) || do_l7_retry(s, si_b) == 0) {
1460 DBG_TRACE_DEVEL("leaving on L7 retry",
1461 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharda254a372019-04-05 15:30:12 +02001462 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001463 }
Olivier Houcharda254a372019-04-05 15:30:12 +02001464 }
1465
Olivier Houchard6db16992019-05-17 15:40:49 +02001466 if (txn->flags & TX_NOT_FIRST)
1467 goto abort_keep_alive;
1468
Willy Tarreau4781b152021-04-06 13:53:36 +02001469 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001470 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001471 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001472 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001473 }
1474
Christopher Faulete0768eb2018-10-03 16:38:02 +02001475 txn->status = 502;
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001476 stream_inc_http_fail_ctr(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001477 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001478 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001479
1480 if (!(s->flags & SF_ERR_MASK))
1481 s->flags |= SF_ERR_SRVCL;
1482 if (!(s->flags & SF_FINST_MASK))
1483 s->flags |= SF_FINST_H;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001484 DBG_TRACE_DEVEL("leaving on error",
1485 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001486 return 0;
1487 }
1488
Christopher Faulet9768c262018-10-22 09:34:31 +02001489 /* 5: write error to client (we don't send any message then) */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001490 else if (rep->flags & CF_WRITE_ERROR) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001491 if (txn->flags & TX_NOT_FIRST)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001492 goto abort_keep_alive;
1493
Willy Tarreau4781b152021-04-06 13:53:36 +02001494 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001495 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001496 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001497 rep->analysers &= AN_RES_FLT_END;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001498
1499 if (!(s->flags & SF_ERR_MASK))
1500 s->flags |= SF_ERR_CLICL;
1501 if (!(s->flags & SF_FINST_MASK))
1502 s->flags |= SF_FINST_H;
1503
1504 /* process_stream() will take care of the error */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001505 DBG_TRACE_DEVEL("leaving on error",
1506 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001507 return 0;
1508 }
1509
1510 channel_dont_close(rep);
1511 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001512 DBG_TRACE_DEVEL("waiting for more data",
1513 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001514 return 0;
1515 }
1516
1517 /* More interesting part now : we know that we have a complete
1518 * response which at least looks like HTTP. We have an indicator
1519 * of each header's length, so we can parse them quickly.
1520 */
Christopher Faulet29f17582019-05-23 11:03:26 +02001521 BUG_ON(htx_get_first_type(htx) != HTX_BLK_RES_SL);
Christopher Faulet297fbb42019-05-13 14:41:27 +02001522 sl = http_get_stline(htx);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001523
Christopher Faulet0f66d552021-05-26 13:14:39 +02001524 /* Perform a L7 retry because of the status code */
1525 if ((si_b->flags & SI_FL_L7_RETRY) &&
1526 l7_status_match(s->be, sl->info.res.status) &&
1527 do_l7_retry(s, si_b) == 0) {
1528 DBG_TRACE_DEVEL("leaving on L7 retry", STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
1529 return 0;
1530 }
1531
1532 /* Now, L7 buffer is useless, it can be released */
1533 b_free(&s->si[1].l7_buffer);
1534
1535 msg->msg_state = HTTP_MSG_BODY;
1536
1537
Christopher Faulet9768c262018-10-22 09:34:31 +02001538 /* 0: we might have to print this header in debug mode */
1539 if (unlikely((global.mode & MODE_DEBUG) &&
1540 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
1541 int32_t pos;
1542
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001543 http_debug_stline("srvrep", s, sl);
Christopher Faulet9768c262018-10-22 09:34:31 +02001544
Christopher Fauleta3f15502019-05-13 15:27:23 +02001545 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001546 struct htx_blk *blk = htx_get_blk(htx, pos);
1547 enum htx_blk_type type = htx_get_blk_type(blk);
1548
1549 if (type == HTX_BLK_EOH)
1550 break;
1551 if (type != HTX_BLK_HDR)
1552 continue;
1553
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001554 http_debug_hdr("srvhdr", s,
1555 htx_get_blk_name(htx, blk),
1556 htx_get_blk_value(htx, blk));
Christopher Faulet9768c262018-10-22 09:34:31 +02001557 }
1558 }
1559
Christopher Faulet03599112018-11-27 11:21:21 +01001560 /* 1: get the status code and the version. Also set HTTP flags */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001561 txn->status = sl->info.res.status;
Christopher Faulet03599112018-11-27 11:21:21 +01001562 if (sl->flags & HTX_SL_F_VER_11)
Christopher Faulet9768c262018-10-22 09:34:31 +02001563 msg->flags |= HTTP_MSGF_VER_11;
Christopher Faulet03599112018-11-27 11:21:21 +01001564 if (sl->flags & HTX_SL_F_XFER_LEN) {
1565 msg->flags |= HTTP_MSGF_XFER_LEN;
Christopher Faulet2a408542020-11-20 14:22:37 +01001566 if (sl->flags & HTX_SL_F_CLEN)
1567 msg->flags |= HTTP_MSGF_CNT_LEN;
1568 else if (sl->flags & HTX_SL_F_CHNK)
1569 msg->flags |= HTTP_MSGF_TE_CHNK;
Christopher Faulet03599112018-11-27 11:21:21 +01001570 }
Christopher Faulet2a408542020-11-20 14:22:37 +01001571 if (sl->flags & HTX_SL_F_BODYLESS)
1572 msg->flags |= HTTP_MSGF_BODYLESS;
Christopher Faulet576c3582021-01-08 15:53:01 +01001573 if (sl->flags & HTX_SL_F_CONN_UPG)
1574 msg->flags |= HTTP_MSGF_CONN_UPG;
Christopher Faulet9768c262018-10-22 09:34:31 +02001575
1576 n = txn->status / 100;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001577 if (n < 1 || n > 5)
1578 n = 0;
Christopher Faulet9768c262018-10-22 09:34:31 +02001579
Christopher Faulete0768eb2018-10-03 16:38:02 +02001580 /* when the client triggers a 4xx from the server, it's most often due
1581 * to a missing object or permission. These events should be tracked
1582 * because if they happen often, it may indicate a brute force or a
1583 * vulnerability scan.
1584 */
1585 if (n == 4)
1586 stream_inc_http_err_ctr(s);
1587
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001588 if (n == 5 && txn->status != 501 && txn->status != 505)
1589 stream_inc_http_fail_ctr(s);
1590
Marcin Deranek3c27dda2020-05-15 18:32:51 +02001591 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001592 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.p.http.rsp[n]);
1593 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.p.http.cum_req);
Marcin Deranek3c27dda2020-05-15 18:32:51 +02001594 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001595
Christopher Faulete0768eb2018-10-03 16:38:02 +02001596 /* Adjust server's health based on status code. Note: status codes 501
1597 * and 505 are triggered on demand by client request, so we must not
1598 * count them as server failures.
1599 */
1600 if (objt_server(s->target)) {
1601 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001602 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_OK);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001603 else
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001604 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_STS);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001605 }
1606
1607 /*
1608 * We may be facing a 100-continue response, or any other informational
1609 * 1xx response which is non-final, in which case this is not the right
1610 * response, and we're waiting for the next one. Let's allow this response
1611 * to go to the client and wait for the next one. There's an exception for
1612 * 101 which is used later in the code to switch protocols.
1613 */
1614 if (txn->status < 200 &&
1615 (txn->status == 100 || txn->status >= 102)) {
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001616 FLT_STRM_CB(s, flt_http_reset(s, msg));
Christopher Faulet421e7692019-06-13 11:16:45 +02001617 htx->first = channel_htx_fwd_headers(rep, htx);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001618 msg->msg_state = HTTP_MSG_RPBEFORE;
Christopher Faulet3499f622019-09-03 15:23:54 +02001619 msg->flags = 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001620 txn->status = 0;
1621 s->logs.t_data = -1; /* was not a response yet */
Christopher Faulet7d518452020-08-31 11:07:07 +02001622 rep->flags |= CF_SEND_DONTWAIT; /* Send ASAP informational messages */
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001623 goto next_one;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001624 }
1625
Christopher Faulet6e6c7b12021-01-08 16:02:05 +01001626 /* A 101-switching-protocols must contains a Connection header with the
1627 * "upgrade" option and the request too. It means both are agree to
1628 * upgrade. It is not so strict because there is no test on the Upgrade
1629 * header content. But it is probably stronger enough for now.
1630 */
1631 if (txn->status == 101 &&
1632 (!(txn->req.flags & HTTP_MSGF_CONN_UPG) || !(txn->rsp.flags & HTTP_MSGF_CONN_UPG)))
1633 goto return_bad_res;
1634
Christopher Faulete0768eb2018-10-03 16:38:02 +02001635 /*
1636 * 2: check for cacheability.
1637 */
1638
1639 switch (txn->status) {
1640 case 200:
1641 case 203:
1642 case 204:
1643 case 206:
1644 case 300:
1645 case 301:
1646 case 404:
1647 case 405:
1648 case 410:
1649 case 414:
1650 case 501:
1651 break;
1652 default:
1653 /* RFC7231#6.1:
1654 * Responses with status codes that are defined as
1655 * cacheable by default (e.g., 200, 203, 204, 206,
1656 * 300, 301, 404, 405, 410, 414, and 501 in this
1657 * specification) can be reused by a cache with
1658 * heuristic expiration unless otherwise indicated
1659 * by the method definition or explicit cache
1660 * controls [RFC7234]; all other status codes are
1661 * not cacheable by default.
1662 */
1663 txn->flags &= ~(TX_CACHEABLE | TX_CACHE_COOK);
1664 break;
1665 }
1666
1667 /*
1668 * 3: we may need to capture headers
1669 */
1670 s->logs.logwait &= ~LW_RESP;
1671 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->res_cap))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001672 http_capture_headers(htx, s->res_cap, sess->fe->rsp_cap);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001673
Christopher Faulet9768c262018-10-22 09:34:31 +02001674 /* Skip parsing if no content length is possible. */
Christopher Fauletc75668e2020-12-07 18:10:32 +01001675 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status >= 200 && txn->status < 300) ||
Christopher Faulete0768eb2018-10-03 16:38:02 +02001676 txn->status == 101)) {
1677 /* Either we've established an explicit tunnel, or we're
1678 * switching the protocol. In both cases, we're very unlikely
1679 * to understand the next protocols. We have to switch to tunnel
1680 * mode, so that we transfer the request and responses then let
1681 * this protocol pass unmodified. When we later implement specific
1682 * parsers for such protocols, we'll want to check the Upgrade
1683 * header which contains information about that protocol for
1684 * responses with status 101 (eg: see RFC2817 about TLS).
1685 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02001686 txn->flags |= TX_CON_WANT_TUN;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001687 }
1688
Christopher Faulet61608322018-11-23 16:23:45 +01001689 /* check for NTML authentication headers in 401 (WWW-Authenticate) and
1690 * 407 (Proxy-Authenticate) responses and set the connection to private
1691 */
1692 srv_conn = cs_conn(objt_cs(s->si[1].end));
1693 if (srv_conn) {
1694 struct ist hdr;
1695 struct http_hdr_ctx ctx;
1696
1697 if (txn->status == 401)
1698 hdr = ist("WWW-Authenticate");
1699 else if (txn->status == 407)
1700 hdr = ist("Proxy-Authenticate");
1701 else
1702 goto end;
1703
1704 ctx.blk = NULL;
1705 while (http_find_header(htx, hdr, &ctx, 0)) {
Willy Tarreauf1dcced2020-05-07 19:27:02 +02001706 /* If www-authenticate contains "Negotiate", "Nego2", or "NTLM",
1707 * possibly followed by blanks and a base64 string, the connection
1708 * is private. Since it's a mess to deal with, we only check for
1709 * values starting with "NTLM" or "Nego". Note that often multiple
1710 * headers are sent by the server there.
1711 */
1712 if ((ctx.value.len >= 4 && strncasecmp(ctx.value.ptr, "Nego", 4) == 0) ||
Willy Tarreau49a1d282020-05-07 19:10:15 +02001713 (ctx.value.len >= 4 && strncasecmp(ctx.value.ptr, "NTLM", 4) == 0)) {
Olivier Houchard250031e2019-05-29 15:01:50 +02001714 sess->flags |= SESS_FL_PREFER_LAST;
Christopher Faulet08016ab2020-07-01 16:10:06 +02001715 conn_set_owner(srv_conn, sess, NULL);
Christopher Faulet21ddc742020-07-01 15:26:14 +02001716 conn_set_private(srv_conn);
Ilya Shipitsin6b79f382020-07-23 00:32:55 +05001717 /* If it fail now, the same will be done in mux->detach() callback */
Christopher Faulet08016ab2020-07-01 16:10:06 +02001718 session_add_conn(srv_conn->owner, srv_conn, srv_conn->target);
Willy Tarreauf1dcced2020-05-07 19:27:02 +02001719 break;
Olivier Houchard250031e2019-05-29 15:01:50 +02001720 }
Christopher Faulet61608322018-11-23 16:23:45 +01001721 }
1722 }
1723
1724 end:
Christopher Faulete0768eb2018-10-03 16:38:02 +02001725 /* we want to have the response time before we start processing it */
1726 s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now);
1727
1728 /* end of job, return OK */
1729 rep->analysers &= ~an_bit;
1730 rep->analyse_exp = TICK_ETERNITY;
1731 channel_auto_close(rep);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001732 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001733 return 1;
1734
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001735 return_int_err:
Willy Tarreau4781b152021-04-06 13:53:36 +02001736 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
1737 _HA_ATOMIC_INC(&s->be->be_counters.internal_errors);
William Lallemand36119de2021-03-08 15:26:48 +01001738 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001739 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Fauletb8a53712019-12-16 11:29:38 +01001740 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001741 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.internal_errors);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001742 txn->status = 500;
1743 if (!(s->flags & SF_ERR_MASK))
1744 s->flags |= SF_ERR_INTERNAL;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001745 goto return_prx_cond;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001746
1747 return_bad_res:
Willy Tarreau4781b152021-04-06 13:53:36 +02001748 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Faulet47365272018-10-31 17:40:50 +01001749 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001750 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001751 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Christopher Faulet47365272018-10-31 17:40:50 +01001752 }
Olivier Houcharde3249a92019-05-03 23:01:47 +02001753 if ((s->be->retry_type & PR_RE_JUNK_REQUEST) &&
Olivier Houchardad26d8d2019-05-10 17:48:28 +02001754 (si_b->flags & SI_FL_L7_RETRY) &&
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001755 do_l7_retry(s, si_b) == 0) {
1756 DBG_TRACE_DEVEL("leaving on L7 retry",
1757 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharde3249a92019-05-03 23:01:47 +02001758 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001759 }
Christopher Faulet47365272018-10-31 17:40:50 +01001760 txn->status = 502;
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001761 stream_inc_http_fail_ctr(s);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001762 /* fall through */
1763
Christopher Fauletb8a53712019-12-16 11:29:38 +01001764 return_prx_cond:
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001765 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet47365272018-10-31 17:40:50 +01001766
1767 if (!(s->flags & SF_ERR_MASK))
1768 s->flags |= SF_ERR_PRXCOND;
1769 if (!(s->flags & SF_FINST_MASK))
1770 s->flags |= SF_FINST_H;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001771
1772 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001773 DBG_TRACE_DEVEL("leaving on error",
1774 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulet47365272018-10-31 17:40:50 +01001775 return 0;
1776
Christopher Faulete0768eb2018-10-03 16:38:02 +02001777 abort_keep_alive:
1778 /* A keep-alive request to the server failed on a network error.
1779 * The client is required to retry. We need to close without returning
1780 * any other information so that the client retries.
1781 */
1782 txn->status = 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001783 s->logs.logwait = 0;
1784 s->logs.level = 0;
1785 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001786 http_reply_and_close(s, txn->status, NULL);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001787 DBG_TRACE_DEVEL("leaving by closing K/A connection",
1788 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001789 return 0;
1790}
1791
1792/* This function performs all the processing enabled for the current response.
1793 * It normally returns 1 unless it wants to break. It relies on buffers flags,
1794 * and updates s->res.analysers. It might make sense to explode it into several
1795 * other functions. It works like process_request (see indications above).
1796 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001797int http_process_res_common(struct stream *s, struct channel *rep, int an_bit, struct proxy *px)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001798{
1799 struct session *sess = s->sess;
1800 struct http_txn *txn = s->txn;
1801 struct http_msg *msg = &txn->rsp;
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001802 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001803 struct proxy *cur_proxy;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001804 enum rule_result ret = HTTP_RULE_RES_CONT;
1805
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001806 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
1807 return 0;
Christopher Faulet9768c262018-10-22 09:34:31 +02001808
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001809 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001810
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001811 htx = htxbuf(&rep->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001812
1813 /* The stats applet needs to adjust the Connection header but we don't
1814 * apply any filter there.
1815 */
1816 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
1817 rep->analysers &= ~an_bit;
1818 rep->analyse_exp = TICK_ETERNITY;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001819 goto end;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001820 }
1821
1822 /*
1823 * We will have to evaluate the filters.
1824 * As opposed to version 1.2, now they will be evaluated in the
1825 * filters order and not in the header order. This means that
1826 * each filter has to be validated among all headers.
1827 *
1828 * Filters are tried with ->be first, then with ->fe if it is
1829 * different from ->be.
1830 *
1831 * Maybe we are in resume condiion. In this case I choose the
1832 * "struct proxy" which contains the rule list matching the resume
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05001833 * pointer. If none of these "struct proxy" match, I initialise
Christopher Faulete0768eb2018-10-03 16:38:02 +02001834 * the process with the first one.
1835 *
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05001836 * In fact, I check only correspondence between the current list
Christopher Faulete0768eb2018-10-03 16:38:02 +02001837 * pointer and the ->fe rule list. If it doesn't match, I initialize
1838 * the loop with the ->be.
1839 */
1840 if (s->current_rule_list == &sess->fe->http_res_rules)
1841 cur_proxy = sess->fe;
1842 else
1843 cur_proxy = s->be;
1844 while (1) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001845 /* evaluate http-response rules */
Christopher Fauletb4c4a972021-11-09 16:33:25 +01001846 if (ret == HTTP_RULE_RES_CONT || ret == HTTP_RULE_RES_STOP) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001847 ret = http_res_get_intercept_rule(cur_proxy, &cur_proxy->http_res_rules, s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001848
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001849 switch (ret) {
1850 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
1851 goto return_prx_yield;
1852
1853 case HTTP_RULE_RES_CONT:
1854 case HTTP_RULE_RES_STOP: /* nothing to do */
1855 break;
1856
1857 case HTTP_RULE_RES_DENY: /* deny or tarpit */
1858 goto deny;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001859
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001860 case HTTP_RULE_RES_ABRT: /* abort request, response already sent */
1861 goto return_prx_cond;
1862
1863 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
Christopher Fauletb8a53712019-12-16 11:29:38 +01001864 goto done;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001865
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001866 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
1867 goto return_bad_res;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001868
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001869 case HTTP_RULE_RES_ERROR: /* failed with a bad request */
1870 goto return_int_err;
1871 }
1872
1873 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001874
Christopher Faulete0768eb2018-10-03 16:38:02 +02001875 /* check whether we're already working on the frontend */
1876 if (cur_proxy == sess->fe)
1877 break;
1878 cur_proxy = sess->fe;
1879 }
1880
Christopher Faulete0768eb2018-10-03 16:38:02 +02001881 /* OK that's all we can do for 1xx responses */
1882 if (unlikely(txn->status < 200 && txn->status != 101))
Christopher Fauletf2824e62018-10-01 12:12:37 +02001883 goto end;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001884
1885 /*
1886 * Now check for a server cookie.
1887 */
1888 if (s->be->cookie_name || sess->fe->capture_name || (s->be->options & PR_O_CHK_CACHE))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001889 http_manage_server_side_cookies(s, rep);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001890
1891 /*
1892 * Check for cache-control or pragma headers if required.
1893 */
1894 if ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001895 http_check_response_for_cacheability(s, rep);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001896
1897 /*
1898 * Add server cookie in the response if needed
1899 */
1900 if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) &&
1901 !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) &&
1902 (!(s->flags & SF_DIRECT) ||
1903 ((s->be->cookie_maxidle || txn->cookie_last_date) &&
1904 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
1905 (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
1906 (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
1907 (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
1908 !(s->flags & SF_IGNORE_PRST)) {
1909 /* the server is known, it's not the one the client requested, or the
1910 * cookie's last seen date needs to be refreshed. We have to
1911 * insert a set-cookie here, except if we want to insert only on POST
1912 * requests and this one isn't. Note that servers which don't have cookies
1913 * (eg: some backup servers) will return a full cookie removal request.
1914 */
Willy Tarreau66182592021-12-06 07:01:02 +00001915 if (!__objt_server(s->target)->cookie) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001916 chunk_printf(&trash,
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001917 "%s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
Christopher Faulete0768eb2018-10-03 16:38:02 +02001918 s->be->cookie_name);
1919 }
1920 else {
Willy Tarreau66182592021-12-06 07:01:02 +00001921 chunk_printf(&trash, "%s=%s", s->be->cookie_name, __objt_server(s->target)->cookie);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001922
1923 if (s->be->cookie_maxidle || s->be->cookie_maxlife) {
1924 /* emit last_date, which is mandatory */
1925 trash.area[trash.data++] = COOKIE_DELIM_DATE;
1926 s30tob64((date.tv_sec+3) >> 2,
1927 trash.area + trash.data);
1928 trash.data += 5;
1929
1930 if (s->be->cookie_maxlife) {
1931 /* emit first_date, which is either the original one or
1932 * the current date.
1933 */
1934 trash.area[trash.data++] = COOKIE_DELIM_DATE;
1935 s30tob64(txn->cookie_first_date ?
1936 txn->cookie_first_date >> 2 :
1937 (date.tv_sec+3) >> 2,
1938 trash.area + trash.data);
1939 trash.data += 5;
1940 }
1941 }
1942 chunk_appendf(&trash, "; path=/");
1943 }
1944
1945 if (s->be->cookie_domain)
1946 chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain);
1947
1948 if (s->be->ck_opts & PR_CK_HTTPONLY)
1949 chunk_appendf(&trash, "; HttpOnly");
1950
1951 if (s->be->ck_opts & PR_CK_SECURE)
1952 chunk_appendf(&trash, "; Secure");
1953
Christopher Faulet2f533902020-01-21 11:06:48 +01001954 if (s->be->cookie_attrs)
1955 chunk_appendf(&trash, "; %s", s->be->cookie_attrs);
1956
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001957 if (unlikely(!http_add_header(htx, ist("Set-Cookie"), ist2(trash.area, trash.data))))
Christopher Fauletb8a53712019-12-16 11:29:38 +01001958 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001959
1960 txn->flags &= ~TX_SCK_MASK;
1961 if (__objt_server(s->target)->cookie && (s->flags & SF_DIRECT))
1962 /* the server did not change, only the date was updated */
1963 txn->flags |= TX_SCK_UPDATED;
1964 else
1965 txn->flags |= TX_SCK_INSERTED;
1966
1967 /* Here, we will tell an eventual cache on the client side that we don't
1968 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
1969 * Some caches understand the correct form: 'no-cache="set-cookie"', but
1970 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
1971 */
1972 if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
1973
1974 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
1975
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001976 if (unlikely(!http_add_header(htx, ist("Cache-control"), ist("private"))))
Christopher Fauletb8a53712019-12-16 11:29:38 +01001977 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001978 }
1979 }
1980
1981 /*
1982 * Check if result will be cacheable with a cookie.
1983 * We'll block the response if security checks have caught
1984 * nasty things such as a cacheable cookie.
1985 */
1986 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
1987 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
1988 (s->be->options & PR_O_CHK_CACHE)) {
1989 /* we're in presence of a cacheable response containing
1990 * a set-cookie header. We'll block it as requested by
1991 * the 'checkcache' option, and send an alert.
1992 */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001993 ha_alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreau66182592021-12-06 07:01:02 +00001994 s->be->id, objt_server(s->target) ? __objt_server(s->target)->id : "<dispatch>");
Christopher Faulete0768eb2018-10-03 16:38:02 +02001995 send_log(s->be, LOG_ALERT,
1996 "Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreau66182592021-12-06 07:01:02 +00001997 s->be->id, objt_server(s->target) ? __objt_server(s->target)->id : "<dispatch>");
Christopher Fauletb8a53712019-12-16 11:29:38 +01001998 goto deny;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001999 }
2000
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002001 end:
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002002 /*
2003 * Evaluate after-response rules before forwarding the response. rules
2004 * from the backend are evaluated first, then one from the frontend if
2005 * it differs.
2006 */
2007 if (!http_eval_after_res_rules(s))
2008 goto return_int_err;
2009
Christopher Fauletc2ac5e42021-03-08 18:20:09 +01002010 /* Filter the response headers if there are filters attached to the
2011 * stream.
2012 */
2013 if (HAS_FILTERS(s))
2014 rep->analysers |= AN_RES_FLT_HTTP_HDRS;
2015
Christopher Faulete0768eb2018-10-03 16:38:02 +02002016 /* Always enter in the body analyzer */
2017 rep->analysers &= ~AN_RES_FLT_XFER_DATA;
2018 rep->analysers |= AN_RES_HTTP_XFER_BODY;
2019
2020 /* if the user wants to log as soon as possible, without counting
2021 * bytes from the server, then this is the right moment. We have
2022 * to temporarily assign bytes_out to log what we currently have.
2023 */
2024 if (!LIST_ISEMPTY(&sess->fe->logformat) && !(s->logs.logwait & LW_BYTES)) {
2025 s->logs.t_close = s->logs.t_data; /* to get a valid end date */
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002026 s->logs.bytes_out = htx->data;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002027 s->do_log(s);
2028 s->logs.bytes_out = 0;
2029 }
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002030
Christopher Fauletb8a53712019-12-16 11:29:38 +01002031 done:
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002032 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002033 rep->analysers &= ~an_bit;
2034 rep->analyse_exp = TICK_ETERNITY;
2035 return 1;
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002036
Christopher Fauletb8a53712019-12-16 11:29:38 +01002037 deny:
Willy Tarreau4781b152021-04-06 13:53:36 +02002038 _HA_ATOMIC_INC(&sess->fe->fe_counters.denied_resp);
2039 _HA_ATOMIC_INC(&s->be->be_counters.denied_resp);
William Lallemand36119de2021-03-08 15:26:48 +01002040 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02002041 _HA_ATOMIC_INC(&sess->listener->counters->denied_resp);
Christopher Fauleta08546b2019-12-16 16:07:34 +01002042 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02002043 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.denied_resp);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002044 goto return_prx_err;
2045
2046 return_int_err:
2047 txn->status = 500;
2048 if (!(s->flags & SF_ERR_MASK))
2049 s->flags |= SF_ERR_INTERNAL;
Willy Tarreau4781b152021-04-06 13:53:36 +02002050 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
2051 _HA_ATOMIC_INC(&s->be->be_counters.internal_errors);
Dragan Dosen84426cd2021-09-21 13:02:09 +02002052 if (sess->listener && sess->listener->counters)
2053 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Fauletcff0f732019-12-16 16:13:44 +01002054 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02002055 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.internal_errors);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002056 goto return_prx_err;
2057
2058 return_bad_res:
2059 txn->status = 502;
Willy Tarreau826f3ab2021-02-10 12:07:15 +01002060 stream_inc_http_fail_ctr(s);
Willy Tarreau4781b152021-04-06 13:53:36 +02002061 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Fauleta20a6532020-02-05 10:16:41 +01002062 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02002063 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Christopher Fauleta20a6532020-02-05 10:16:41 +01002064 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_RSP);
2065 }
Christopher Fauletb8a53712019-12-16 11:29:38 +01002066 /* fall through */
2067
2068 return_prx_err:
2069 http_reply_and_close(s, txn->status, http_error_message(s));
2070 /* fall through */
2071
2072 return_prx_cond:
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002073 s->logs.t_data = -1; /* was not a valid response */
2074 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002075
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002076 if (!(s->flags & SF_ERR_MASK))
2077 s->flags |= SF_ERR_PRXCOND;
2078 if (!(s->flags & SF_FINST_MASK))
2079 s->flags |= SF_FINST_H;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002080
Christopher Faulete58c0002020-03-02 16:21:01 +01002081 rep->analysers &= AN_RES_FLT_END;
2082 s->req.analysers &= AN_REQ_FLT_END;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002083 rep->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002084 DBG_TRACE_DEVEL("leaving on error",
2085 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002086 return 0;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002087
2088 return_prx_yield:
2089 channel_dont_close(rep);
2090 DBG_TRACE_DEVEL("waiting for more data",
2091 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
2092 return 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002093}
2094
2095/* This function is an analyser which forwards response body (including chunk
2096 * sizes if any). It is called as soon as we must forward, even if we forward
2097 * zero byte. The only situation where it must not be called is when we're in
2098 * tunnel mode and we want to forward till the close. It's used both to forward
2099 * remaining data and to resync after end of body. It expects the msg_state to
2100 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
2101 * read more data, or 1 once we can go on with next request or end the stream.
2102 *
2103 * It is capable of compressing response data both in content-length mode and
2104 * in chunked mode. The state machines follows different flows depending on
2105 * whether content-length and chunked modes are used, since there are no
2106 * trailers in content-length :
2107 *
2108 * chk-mode cl-mode
2109 * ,----- BODY -----.
2110 * / \
2111 * V size > 0 V chk-mode
2112 * .--> SIZE -------------> DATA -------------> CRLF
2113 * | | size == 0 | last byte |
2114 * | v final crlf v inspected |
2115 * | TRAILERS -----------> DONE |
2116 * | |
2117 * `----------------------------------------------'
2118 *
2119 * Compression only happens in the DATA state, and must be flushed in final
2120 * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding
2121 * is performed at once on final states for all bytes parsed, or when leaving
2122 * on missing data.
2123 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002124int http_response_forward_body(struct stream *s, struct channel *res, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +02002125{
2126 struct session *sess = s->sess;
2127 struct http_txn *txn = s->txn;
2128 struct http_msg *msg = &s->txn->rsp;
Christopher Faulet9768c262018-10-22 09:34:31 +02002129 struct htx *htx;
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002130 int ret;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002131
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002132 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002133
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002134 htx = htxbuf(&res->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002135
Christopher Fauletb9a92f32019-09-09 10:15:21 +02002136 if (htx->flags & HTX_FL_PARSING_ERROR)
2137 goto return_bad_res;
2138 if (htx->flags & HTX_FL_PROCESSING_ERROR)
2139 goto return_int_err;
2140
Christopher Faulete0768eb2018-10-03 16:38:02 +02002141 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Christopher Fauletf2824e62018-10-01 12:12:37 +02002142 ((res->flags & CF_SHUTW) && (res->to_forward || co_data(res)))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02002143 /* Output closed while we were sending data. We must abort and
2144 * wake the other side up.
2145 */
Christopher Faulete0768eb2018-10-03 16:38:02 +02002146 msg->msg_state = HTTP_MSG_ERROR;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002147 http_end_response(s);
2148 http_end_request(s);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002149 DBG_TRACE_DEVEL("leaving on error",
2150 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002151 return 1;
2152 }
2153
Christopher Faulet9768c262018-10-22 09:34:31 +02002154 if (msg->msg_state == HTTP_MSG_BODY)
2155 msg->msg_state = HTTP_MSG_DATA;
2156
Christopher Faulete0768eb2018-10-03 16:38:02 +02002157 /* in most states, we should abort in case of early close */
2158 channel_auto_close(res);
2159
Christopher Faulete0768eb2018-10-03 16:38:02 +02002160 if (res->to_forward) {
Christopher Faulet66af0b22019-03-22 14:54:52 +01002161 if (res->to_forward == CHN_INFINITE_FORWARD) {
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002162 if (res->flags & CF_EOI)
2163 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet66af0b22019-03-22 14:54:52 +01002164 }
2165 else {
2166 /* We can't process the buffer's contents yet */
2167 res->flags |= CF_WAKE_WRITE;
2168 goto missing_data_or_waiting;
2169 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02002170 }
2171
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002172 if (msg->msg_state >= HTTP_MSG_ENDING)
2173 goto ending;
2174
Christopher Fauletc75668e2020-12-07 18:10:32 +01002175 if ((txn->meth == HTTP_METH_CONNECT && txn->status >= 200 && txn->status < 300) || txn->status == 101 ||
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002176 (!(msg->flags & HTTP_MSGF_XFER_LEN) && !HAS_RSP_DATA_FILTERS(s))) {
2177 msg->msg_state = HTTP_MSG_ENDING;
2178 goto ending;
2179 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002180
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002181 /* Forward input data. We get it by removing all outgoing data not
2182 * forwarded yet from HTX data size. If there are some data filters, we
2183 * let them decide the amount of data to forward.
Christopher Faulet9768c262018-10-22 09:34:31 +02002184 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002185 if (HAS_RSP_DATA_FILTERS(s)) {
2186 ret = flt_http_payload(s, msg, htx->data);
2187 if (ret < 0)
2188 goto return_bad_res;
Christopher Faulet421e7692019-06-13 11:16:45 +02002189 c_adv(res, ret);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002190 }
2191 else {
Christopher Faulet421e7692019-06-13 11:16:45 +02002192 c_adv(res, htx->data - co_data(res));
Christopher Faulet66af0b22019-03-22 14:54:52 +01002193 if (msg->flags & HTTP_MSGF_XFER_LEN)
2194 channel_htx_forward_forever(res, htx);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002195 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002196
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002197 if (htx->data != co_data(res))
2198 goto missing_data_or_waiting;
2199
2200 if (!(msg->flags & HTTP_MSGF_XFER_LEN) && res->flags & CF_SHUTR) {
2201 msg->msg_state = HTTP_MSG_ENDING;
2202 goto ending;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002203 }
2204
Christopher Faulet9768c262018-10-22 09:34:31 +02002205 /* Check if the end-of-message is reached and if so, switch the message
Christopher Fauletd20fdb02019-06-13 16:43:22 +02002206 * in HTTP_MSG_ENDING state. Then if all data was marked to be
2207 * forwarded, set the state to HTTP_MSG_DONE.
Christopher Faulet9768c262018-10-22 09:34:31 +02002208 */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002209 if (!(htx->flags & HTX_FL_EOM))
Christopher Faulet9768c262018-10-22 09:34:31 +02002210 goto missing_data_or_waiting;
2211
Christopher Fauletd20fdb02019-06-13 16:43:22 +02002212 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet9768c262018-10-22 09:34:31 +02002213
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002214 ending:
Christopher Faulet2151cdd2020-07-22 16:34:59 +02002215 res->flags &= ~CF_EXPECT_MORE; /* no more data are expected */
2216
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002217 /* other states, ENDING...TUNNEL */
2218 if (msg->msg_state >= HTTP_MSG_DONE)
2219 goto done;
Christopher Faulet9768c262018-10-22 09:34:31 +02002220
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002221 if (HAS_RSP_DATA_FILTERS(s)) {
2222 ret = flt_http_end(s, msg);
2223 if (ret <= 0) {
2224 if (!ret)
2225 goto missing_data_or_waiting;
2226 goto return_bad_res;
2227 }
2228 }
2229
Christopher Fauletc75668e2020-12-07 18:10:32 +01002230 if ((txn->meth == HTTP_METH_CONNECT && txn->status >= 200 && txn->status < 300) || txn->status == 101 ||
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002231 !(msg->flags & HTTP_MSGF_XFER_LEN)) {
2232 msg->msg_state = HTTP_MSG_TUNNEL;
2233 goto ending;
2234 }
2235 else {
2236 msg->msg_state = HTTP_MSG_DONE;
2237 res->to_forward = 0;
2238 }
2239
2240 done:
2241
2242 channel_dont_close(res);
2243
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002244 http_end_response(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002245 if (!(res->analysers & an_bit)) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002246 http_end_request(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002247 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
2248 if (res->flags & CF_SHUTW) {
2249 /* response errors are most likely due to the
2250 * client aborting the transfer. */
Christopher Faulet93e02d82019-03-08 14:18:50 +01002251 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002252 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02002253 goto return_bad_res;
2254 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002255 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002256 return 1;
2257 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002258 DBG_TRACE_DEVEL("waiting for the end of the HTTP txn",
2259 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002260 return 0;
2261
2262 missing_data_or_waiting:
2263 if (res->flags & CF_SHUTW)
Christopher Faulet93e02d82019-03-08 14:18:50 +01002264 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002265
2266 /* stop waiting for data if the input is closed before the end. If the
2267 * client side was already closed, it means that the client has aborted,
2268 * so we don't want to count this as a server abort. Otherwise it's a
2269 * server abort.
2270 */
Christopher Fauletd20fdb02019-06-13 16:43:22 +02002271 if (msg->msg_state < HTTP_MSG_ENDING && res->flags & CF_SHUTR) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02002272 if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))
Christopher Faulet93e02d82019-03-08 14:18:50 +01002273 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002274 /* If we have some pending data, we continue the processing */
Christopher Faulet93e02d82019-03-08 14:18:50 +01002275 if (htx_is_empty(htx))
2276 goto return_srv_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002277 }
2278
Christopher Faulete0768eb2018-10-03 16:38:02 +02002279 /* When TE: chunked is used, we need to get there again to parse
2280 * remaining chunks even if the server has closed, so we don't want to
Christopher Faulet9768c262018-10-22 09:34:31 +02002281 * set CF_DONTCLOSE. Similarly when there is a content-leng or if there
2282 * are filters registered on the stream, we don't want to forward a
2283 * close
Christopher Faulete0768eb2018-10-03 16:38:02 +02002284 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002285 if ((msg->flags & HTTP_MSGF_XFER_LEN) || HAS_RSP_DATA_FILTERS(s))
Christopher Faulete0768eb2018-10-03 16:38:02 +02002286 channel_dont_close(res);
2287
2288 /* We know that more data are expected, but we couldn't send more that
2289 * what we did. So we always set the CF_EXPECT_MORE flag so that the
2290 * system knows it must not set a PUSH on this first part. Interactive
2291 * modes are already handled by the stream sock layer. We must not do
2292 * this in content-length mode because it could present the MSG_MORE
2293 * flag with the last block of forwarded data, which would cause an
2294 * additional delay to be observed by the receiver.
2295 */
Christopher Faulet2151cdd2020-07-22 16:34:59 +02002296 if (HAS_RSP_DATA_FILTERS(s))
Christopher Faulete0768eb2018-10-03 16:38:02 +02002297 res->flags |= CF_EXPECT_MORE;
2298
2299 /* the stream handler will take care of timeouts and errors */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002300 DBG_TRACE_DEVEL("waiting for more data to forward",
2301 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002302 return 0;
2303
Christopher Faulet93e02d82019-03-08 14:18:50 +01002304 return_srv_abort:
Willy Tarreau4781b152021-04-06 13:53:36 +02002305 _HA_ATOMIC_INC(&sess->fe->fe_counters.srv_aborts);
2306 _HA_ATOMIC_INC(&s->be->be_counters.srv_aborts);
William Lallemand36119de2021-03-08 15:26:48 +01002307 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02002308 _HA_ATOMIC_INC(&sess->listener->counters->srv_aborts);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002309 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02002310 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.srv_aborts);
Willy Tarreau826f3ab2021-02-10 12:07:15 +01002311 stream_inc_http_fail_ctr(s);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002312 if (!(s->flags & SF_ERR_MASK))
2313 s->flags |= SF_ERR_SRVCL;
2314 goto return_error;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002315
Christopher Faulet93e02d82019-03-08 14:18:50 +01002316 return_cli_abort:
Willy Tarreau4781b152021-04-06 13:53:36 +02002317 _HA_ATOMIC_INC(&sess->fe->fe_counters.cli_aborts);
2318 _HA_ATOMIC_INC(&s->be->be_counters.cli_aborts);
William Lallemand36119de2021-03-08 15:26:48 +01002319 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02002320 _HA_ATOMIC_INC(&sess->listener->counters->cli_aborts);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002321 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02002322 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.cli_aborts);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002323 if (!(s->flags & SF_ERR_MASK))
2324 s->flags |= SF_ERR_CLICL;
2325 goto return_error;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002326
Christopher Fauletb9a92f32019-09-09 10:15:21 +02002327 return_int_err:
Willy Tarreau4781b152021-04-06 13:53:36 +02002328 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
2329 _HA_ATOMIC_INC(&s->be->be_counters.internal_errors);
William Lallemand36119de2021-03-08 15:26:48 +01002330 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02002331 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002332 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02002333 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.internal_errors);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02002334 if (!(s->flags & SF_ERR_MASK))
2335 s->flags |= SF_ERR_INTERNAL;
2336 goto return_error;
2337
Christopher Faulet93e02d82019-03-08 14:18:50 +01002338 return_bad_res:
Willy Tarreau4781b152021-04-06 13:53:36 +02002339 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002340 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02002341 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002342 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_RSP);
2343 }
Willy Tarreau826f3ab2021-02-10 12:07:15 +01002344 stream_inc_http_fail_ctr(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002345 if (!(s->flags & SF_ERR_MASK))
Christopher Faulet93e02d82019-03-08 14:18:50 +01002346 s->flags |= SF_ERR_SRVCL;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002347 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +02002348
Christopher Faulet93e02d82019-03-08 14:18:50 +01002349 return_error:
Christopher Faulete0768eb2018-10-03 16:38:02 +02002350 /* don't send any error message as we're in the body */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002351 http_reply_and_close(s, txn->status, NULL);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002352 if (!(s->flags & SF_FINST_MASK))
2353 s->flags |= SF_FINST_D;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002354 DBG_TRACE_DEVEL("leaving on error",
2355 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002356 return 0;
2357}
2358
Christopher Fauletf2824e62018-10-01 12:12:37 +02002359/* Perform an HTTP redirect based on the information in <rule>. The function
Christopher Faulet99daf282018-11-28 22:58:13 +01002360 * returns zero on success, or zero in case of a, irrecoverable error such
Christopher Fauletf2824e62018-10-01 12:12:37 +02002361 * as too large a request to build a valid response.
2362 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002363int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn)
Christopher Fauletf2824e62018-10-01 12:12:37 +02002364{
Christopher Faulet99daf282018-11-28 22:58:13 +01002365 struct channel *req = &s->req;
2366 struct channel *res = &s->res;
2367 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01002368 struct htx_sl *sl;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002369 struct buffer *chunk;
Christopher Faulet99daf282018-11-28 22:58:13 +01002370 struct ist status, reason, location;
2371 unsigned int flags;
Christopher Faulet08e66462019-05-23 16:44:59 +02002372 int close = 0; /* Try to keep the connection alive byt default */
Christopher Fauletf2824e62018-10-01 12:12:37 +02002373
2374 chunk = alloc_trash_chunk();
Christopher Fauletb8a53712019-12-16 11:29:38 +01002375 if (!chunk) {
2376 if (!(s->flags & SF_ERR_MASK))
2377 s->flags |= SF_ERR_RESOURCE;
Christopher Faulet99daf282018-11-28 22:58:13 +01002378 goto fail;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002379 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002380
Christopher Faulet99daf282018-11-28 22:58:13 +01002381 /*
2382 * Create the location
2383 */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002384 htx = htxbuf(&req->buf);
Christopher Fauletf2824e62018-10-01 12:12:37 +02002385 switch(rule->type) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002386 case REDIRECT_TYPE_SCHEME: {
2387 struct http_hdr_ctx ctx;
2388 struct ist path, host;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002389
Christopher Faulet99daf282018-11-28 22:58:13 +01002390 host = ist("");
2391 ctx.blk = NULL;
2392 if (http_find_header(htx, ist("Host"), &ctx, 0))
2393 host = ctx.value;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002394
Christopher Faulet297fbb42019-05-13 14:41:27 +02002395 sl = http_get_stline(htx);
Christopher Faulet99daf282018-11-28 22:58:13 +01002396 path = http_get_path(htx_sl_req_uri(sl));
2397 /* build message using path */
Tim Duesterhused526372020-03-05 17:56:33 +01002398 if (isttest(path)) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002399 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2400 int qs = 0;
2401 while (qs < path.len) {
2402 if (*(path.ptr + qs) == '?') {
2403 path.len = qs;
2404 break;
2405 }
2406 qs++;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002407 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002408 }
2409 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002410 else
2411 path = ist("/");
Christopher Fauletf2824e62018-10-01 12:12:37 +02002412
Christopher Faulet99daf282018-11-28 22:58:13 +01002413 if (rule->rdr_str) { /* this is an old "redirect" rule */
2414 /* add scheme */
2415 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2416 goto fail;
2417 }
2418 else {
2419 /* add scheme with executing log format */
2420 chunk->data += build_logline(s, chunk->area + chunk->data,
2421 chunk->size - chunk->data,
2422 &rule->rdr_fmt);
2423 }
2424 /* add "://" + host + path */
2425 if (!chunk_memcat(chunk, "://", 3) ||
2426 !chunk_memcat(chunk, host.ptr, host.len) ||
2427 !chunk_memcat(chunk, path.ptr, path.len))
2428 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002429
Christopher Faulet99daf282018-11-28 22:58:13 +01002430 /* append a slash at the end of the location if needed and missing */
2431 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
2432 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
2433 if (chunk->data + 1 >= chunk->size)
2434 goto fail;
2435 chunk->area[chunk->data++] = '/';
2436 }
2437 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002438 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002439
Christopher Faulet99daf282018-11-28 22:58:13 +01002440 case REDIRECT_TYPE_PREFIX: {
2441 struct ist path;
2442
Christopher Faulet297fbb42019-05-13 14:41:27 +02002443 sl = http_get_stline(htx);
Christopher Faulet99daf282018-11-28 22:58:13 +01002444 path = http_get_path(htx_sl_req_uri(sl));
2445 /* build message using path */
Tim Duesterhused526372020-03-05 17:56:33 +01002446 if (isttest(path)) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002447 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2448 int qs = 0;
2449 while (qs < path.len) {
2450 if (*(path.ptr + qs) == '?') {
2451 path.len = qs;
2452 break;
2453 }
2454 qs++;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002455 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002456 }
2457 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002458 else
2459 path = ist("/");
Christopher Fauletf2824e62018-10-01 12:12:37 +02002460
Christopher Faulet99daf282018-11-28 22:58:13 +01002461 if (rule->rdr_str) { /* this is an old "redirect" rule */
2462 /* add prefix. Note that if prefix == "/", we don't want to
2463 * add anything, otherwise it makes it hard for the user to
2464 * configure a self-redirection.
2465 */
2466 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
2467 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2468 goto fail;
2469 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002470 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002471 else {
2472 /* add prefix with executing log format */
2473 chunk->data += build_logline(s, chunk->area + chunk->data,
2474 chunk->size - chunk->data,
2475 &rule->rdr_fmt);
2476 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002477
Christopher Faulet99daf282018-11-28 22:58:13 +01002478 /* add path */
2479 if (!chunk_memcat(chunk, path.ptr, path.len))
2480 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002481
Christopher Faulet99daf282018-11-28 22:58:13 +01002482 /* append a slash at the end of the location if needed and missing */
2483 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
2484 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
2485 if (chunk->data + 1 >= chunk->size)
2486 goto fail;
2487 chunk->area[chunk->data++] = '/';
2488 }
2489 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002490 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002491 case REDIRECT_TYPE_LOCATION:
2492 default:
2493 if (rule->rdr_str) { /* this is an old "redirect" rule */
2494 /* add location */
2495 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2496 goto fail;
2497 }
2498 else {
2499 /* add location with executing log format */
2500 chunk->data += build_logline(s, chunk->area + chunk->data,
2501 chunk->size - chunk->data,
2502 &rule->rdr_fmt);
2503 }
2504 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002505 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002506 location = ist2(chunk->area, chunk->data);
2507
2508 /*
2509 * Create the 30x response
2510 */
2511 switch (rule->code) {
2512 case 308:
2513 status = ist("308");
2514 reason = ist("Permanent Redirect");
2515 break;
2516 case 307:
2517 status = ist("307");
2518 reason = ist("Temporary Redirect");
2519 break;
2520 case 303:
2521 status = ist("303");
2522 reason = ist("See Other");
2523 break;
2524 case 301:
2525 status = ist("301");
2526 reason = ist("Moved Permanently");
2527 break;
2528 case 302:
2529 default:
2530 status = ist("302");
2531 reason = ist("Found");
2532 break;
2533 }
2534
Christopher Faulet08e66462019-05-23 16:44:59 +02002535 if (!(txn->req.flags & HTTP_MSGF_BODYLESS) && txn->req.msg_state != HTTP_MSG_DONE)
2536 close = 1;
2537
Christopher Faulet99daf282018-11-28 22:58:13 +01002538 htx = htx_from_buf(&res->buf);
Kevin Zhu96b36392020-01-07 09:42:55 +01002539 /* Trim any possible response */
2540 channel_htx_truncate(&s->res, htx);
Christopher Faulet99daf282018-11-28 22:58:13 +01002541 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
2542 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), status, reason);
2543 if (!sl)
2544 goto fail;
2545 sl->info.res.status = rule->code;
2546 s->txn->status = rule->code;
2547
Christopher Faulet08e66462019-05-23 16:44:59 +02002548 if (close && !htx_add_header(htx, ist("Connection"), ist("close")))
2549 goto fail;
2550
2551 if (!htx_add_header(htx, ist("Content-length"), ist("0")) ||
Christopher Faulet99daf282018-11-28 22:58:13 +01002552 !htx_add_header(htx, ist("Location"), location))
2553 goto fail;
2554
2555 if (rule->code == 302 || rule->code == 303 || rule->code == 307) {
2556 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")))
2557 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002558 }
2559
2560 if (rule->cookie_len) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002561 if (!htx_add_header(htx, ist("Set-Cookie"), ist2(rule->cookie_str, rule->cookie_len)))
2562 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002563 }
2564
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002565 if (!htx_add_endof(htx, HTX_BLK_EOH))
Christopher Faulet99daf282018-11-28 22:58:13 +01002566 goto fail;
2567
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002568 htx->flags |= HTX_FL_EOM;
Kevin Zhu96b36392020-01-07 09:42:55 +01002569 htx_to_buf(htx, &res->buf);
Christopher Fauleta72a7e42020-01-28 09:28:11 +01002570 if (!http_forward_proxy_resp(s, 1))
2571 goto fail;
Christopher Faulet99daf282018-11-28 22:58:13 +01002572
Christopher Faulet60b33a52020-01-28 09:18:10 +01002573 if (rule->flags & REDIRECT_FLAG_FROM_REQ) {
2574 /* let's log the request time */
2575 s->logs.tv_request = now;
Christopher Faulet91322272021-10-04 14:16:46 +02002576 req->analysers &= AN_REQ_FLT_END;
Christopher Faulet99daf282018-11-28 22:58:13 +01002577
Christopher Faulet60b33a52020-01-28 09:18:10 +01002578 if (s->sess->fe == s->be) /* report it if the request was intercepted by the frontend */
Willy Tarreau4781b152021-04-06 13:53:36 +02002579 _HA_ATOMIC_INC(&s->sess->fe->fe_counters.intercepted_req);
Christopher Faulet60b33a52020-01-28 09:18:10 +01002580 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002581
2582 if (!(s->flags & SF_ERR_MASK))
2583 s->flags |= SF_ERR_LOCAL;
2584 if (!(s->flags & SF_FINST_MASK))
Christopher Faulet60b33a52020-01-28 09:18:10 +01002585 s->flags |= ((rule->flags & REDIRECT_FLAG_FROM_REQ) ? SF_FINST_R : SF_FINST_H);
Christopher Fauletf2824e62018-10-01 12:12:37 +02002586
Christopher Faulet99daf282018-11-28 22:58:13 +01002587 free_trash_chunk(chunk);
2588 return 1;
2589
2590 fail:
2591 /* If an error occurred, remove the incomplete HTTP response from the
2592 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01002593 channel_htx_truncate(res, htxbuf(&res->buf));
Christopher Fauletf2824e62018-10-01 12:12:37 +02002594 free_trash_chunk(chunk);
Christopher Faulet99daf282018-11-28 22:58:13 +01002595 return 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002596}
2597
Christopher Faulet92d34fe2019-12-17 09:20:34 +01002598/* Replace all headers matching the name <name>. The header value is replaced if
2599 * it matches the regex <re>. <str> is used for the replacement. If <full> is
2600 * set to 1, the full-line is matched and replaced. Otherwise, comma-separated
2601 * values are evaluated one by one. It returns 0 on success and -1 on error.
2602 */
2603int http_replace_hdrs(struct stream* s, struct htx *htx, struct ist name,
2604 const char *str, struct my_regex *re, int full)
Christopher Faulet72333522018-10-24 11:25:02 +02002605{
2606 struct http_hdr_ctx ctx;
2607 struct buffer *output = get_trash_chunk();
2608
Christopher Faulet72333522018-10-24 11:25:02 +02002609 ctx.blk = NULL;
Christopher Faulet92d34fe2019-12-17 09:20:34 +01002610 while (http_find_header(htx, name, &ctx, full)) {
Christopher Faulet72333522018-10-24 11:25:02 +02002611 if (!regex_exec_match2(re, ctx.value.ptr, ctx.value.len, MAX_MATCH, pmatch, 0))
2612 continue;
2613
2614 output->data = exp_replace(output->area, output->size, ctx.value.ptr, str, pmatch);
2615 if (output->data == -1)
2616 return -1;
2617 if (!http_replace_header_value(htx, &ctx, ist2(output->area, output->data)))
2618 return -1;
2619 }
2620 return 0;
2621}
2622
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002623/* This function executes one of the set-{method,path,query,uri} actions. It
2624 * takes the string from the variable 'replace' with length 'len', then modifies
2625 * the relevant part of the request line accordingly. Then it updates various
2626 * pointers to the next elements which were moved, and the total buffer length.
2627 * It finds the action to be performed in p[2], previously filled by function
2628 * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal
2629 * error, though this can be revisited when this code is finally exploited.
2630 *
2631 * 'action' can be '0' to replace method, '1' to replace path, '2' to replace
Christopher Faulet312294f2020-09-02 17:17:44 +02002632 * query string, 3 to replace uri or 4 to replace the path+query.
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002633 *
2634 * In query string case, the mark question '?' must be set at the start of the
2635 * string by the caller, event if the replacement query string is empty.
2636 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002637int http_req_replace_stline(int action, const char *replace, int len,
2638 struct proxy *px, struct stream *s)
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002639{
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002640 struct htx *htx = htxbuf(&s->req.buf);
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002641
2642 switch (action) {
2643 case 0: // method
2644 if (!http_replace_req_meth(htx, ist2(replace, len)))
2645 return -1;
2646 break;
2647
2648 case 1: // path
Christopher Fauletb8ce5052020-08-31 16:11:57 +02002649 if (!http_replace_req_path(htx, ist2(replace, len), 0))
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002650 return -1;
2651 break;
2652
2653 case 2: // query
2654 if (!http_replace_req_query(htx, ist2(replace, len)))
2655 return -1;
2656 break;
2657
2658 case 3: // uri
2659 if (!http_replace_req_uri(htx, ist2(replace, len)))
2660 return -1;
2661 break;
2662
Christopher Faulet312294f2020-09-02 17:17:44 +02002663 case 4: // path + query
2664 if (!http_replace_req_path(htx, ist2(replace, len), 1))
2665 return -1;
2666 break;
2667
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002668 default:
2669 return -1;
2670 }
2671 return 0;
2672}
2673
2674/* This function replace the HTTP status code and the associated message. The
Christopher Faulete00d06c2019-12-16 17:18:42 +01002675 * variable <status> contains the new status code. This function never fails. It
2676 * returns 0 in case of success, -1 in case of internal error.
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002677 */
Christopher Faulet96bff762019-12-17 13:46:18 +01002678int http_res_set_status(unsigned int status, struct ist reason, struct stream *s)
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002679{
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002680 struct htx *htx = htxbuf(&s->res.buf);
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002681 char *res;
2682
2683 chunk_reset(&trash);
2684 res = ultoa_o(status, trash.area, trash.size);
2685 trash.data = res - trash.area;
2686
2687 /* Do we have a custom reason format string? */
Tim Duesterhuse296d3e2020-03-05 17:56:31 +01002688 if (!isttest(reason)) {
Christopher Faulet96bff762019-12-17 13:46:18 +01002689 const char *str = http_get_reason(status);
Tim Duesterhusdcf753a2021-03-04 17:31:47 +01002690 reason = ist(str);
Christopher Faulet96bff762019-12-17 13:46:18 +01002691 }
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002692
Christopher Fauletbde2c4c2020-08-31 16:43:34 +02002693 if (!http_replace_res_status(htx, ist2(trash.area, trash.data), reason))
Christopher Faulete00d06c2019-12-16 17:18:42 +01002694 return -1;
2695 return 0;
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002696}
2697
Christopher Faulet3e964192018-10-24 11:39:23 +02002698/* Executes the http-request rules <rules> for stream <s>, proxy <px> and
2699 * transaction <txn>. Returns the verdict of the first rule that prevents
2700 * further processing of the request (auth, deny, ...), and defaults to
2701 * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or
2702 * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT
2703 * on txn->flags if it encounters a tarpit rule. If <deny_status> is not NULL
2704 * and a deny/tarpit rule is matched, it will be filled with this rule's deny
2705 * status.
2706 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002707static enum rule_result http_req_get_intercept_rule(struct proxy *px, struct list *rules,
Christopher Fauletb58f62b2020-01-13 16:40:13 +01002708 struct stream *s)
Christopher Faulet3e964192018-10-24 11:39:23 +02002709{
2710 struct session *sess = strm_sess(s);
2711 struct http_txn *txn = s->txn;
Christopher Faulet3e964192018-10-24 11:39:23 +02002712 struct act_rule *rule;
Christopher Faulet3e964192018-10-24 11:39:23 +02002713 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002714 int act_opts = 0;
Christopher Faulet3e964192018-10-24 11:39:23 +02002715
Christopher Faulet3e964192018-10-24 11:39:23 +02002716 /* If "the current_rule_list" match the executed rule list, we are in
2717 * resume condition. If a resume is needed it is always in the action
2718 * and never in the ACL or converters. In this case, we initialise the
2719 * current rule, and go to the action execution point.
2720 */
2721 if (s->current_rule) {
2722 rule = s->current_rule;
2723 s->current_rule = NULL;
2724 if (s->current_rule_list == rules)
2725 goto resume_execution;
2726 }
2727 s->current_rule_list = rules;
2728
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002729 /* start the ruleset evaluation in strict mode */
2730 txn->req.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01002731
Christopher Faulet3e964192018-10-24 11:39:23 +02002732 list_for_each_entry(rule, rules, list) {
2733 /* check optional condition */
2734 if (rule->cond) {
2735 int ret;
2736
2737 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
2738 ret = acl_pass(ret);
2739
2740 if (rule->cond->pol == ACL_COND_UNLESS)
2741 ret = !ret;
2742
2743 if (!ret) /* condition not matched */
2744 continue;
2745 }
2746
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002747 act_opts |= ACT_OPT_FIRST;
Christopher Faulet3e964192018-10-24 11:39:23 +02002748 resume_execution:
Amaury Denoyelle03517732021-05-07 14:25:01 +02002749 if (rule->kw->flags & KWF_EXPERIMENTAL)
2750 mark_tainted(TAINTED_ACTION_EXP_EXECUTED);
2751
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002752 /* Always call the action function if defined */
2753 if (rule->action_ptr) {
2754 if ((s->req.flags & CF_READ_ERROR) ||
2755 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
2756 (px->options & PR_O_ABRT_CLOSE)))
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002757 act_opts |= ACT_OPT_FINAL;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002758
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002759 switch (rule->action_ptr(rule, px, sess, s, act_opts)) {
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002760 case ACT_RET_CONT:
2761 break;
2762 case ACT_RET_STOP:
2763 rule_ret = HTTP_RULE_RES_STOP;
2764 goto end;
2765 case ACT_RET_YIELD:
2766 s->current_rule = rule;
2767 rule_ret = HTTP_RULE_RES_YIELD;
2768 goto end;
2769 case ACT_RET_ERR:
2770 rule_ret = HTTP_RULE_RES_ERROR;
2771 goto end;
2772 case ACT_RET_DONE:
2773 rule_ret = HTTP_RULE_RES_DONE;
2774 goto end;
2775 case ACT_RET_DENY:
Christopher Fauletb58f62b2020-01-13 16:40:13 +01002776 if (txn->status == -1)
2777 txn->status = 403;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002778 rule_ret = HTTP_RULE_RES_DENY;
2779 goto end;
2780 case ACT_RET_ABRT:
2781 rule_ret = HTTP_RULE_RES_ABRT;
2782 goto end;
2783 case ACT_RET_INV:
2784 rule_ret = HTTP_RULE_RES_BADREQ;
2785 goto end;
2786 }
2787 continue; /* eval the next rule */
2788 }
2789
2790 /* If not action function defined, check for known actions */
Christopher Faulet3e964192018-10-24 11:39:23 +02002791 switch (rule->action) {
2792 case ACT_ACTION_ALLOW:
2793 rule_ret = HTTP_RULE_RES_STOP;
2794 goto end;
2795
2796 case ACT_ACTION_DENY:
Christopher Faulet5cb513a2020-05-13 17:56:56 +02002797 txn->status = rule->arg.http_reply->status;
2798 txn->http_reply = rule->arg.http_reply;
Christopher Faulet3e964192018-10-24 11:39:23 +02002799 rule_ret = HTTP_RULE_RES_DENY;
2800 goto end;
2801
2802 case ACT_HTTP_REQ_TARPIT:
2803 txn->flags |= TX_CLTARPIT;
Christopher Faulet5cb513a2020-05-13 17:56:56 +02002804 txn->status = rule->arg.http_reply->status;
2805 txn->http_reply = rule->arg.http_reply;
Christopher Faulet3e964192018-10-24 11:39:23 +02002806 rule_ret = HTTP_RULE_RES_DENY;
2807 goto end;
2808
Christopher Faulet3e964192018-10-24 11:39:23 +02002809 case ACT_HTTP_REDIR:
Christopher Faulet90d22a82020-03-06 11:18:39 +01002810 rule_ret = HTTP_RULE_RES_ABRT;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002811 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
Christopher Faulet3a26bee2019-12-16 12:47:40 +01002812 rule_ret = HTTP_RULE_RES_ERROR;
Christopher Faulet3e964192018-10-24 11:39:23 +02002813 goto end;
2814
2815 case ACT_HTTP_SET_NICE:
Christopher Faulet96bff762019-12-17 13:46:18 +01002816 s->task->nice = rule->arg.http.i;
Christopher Faulet3e964192018-10-24 11:39:23 +02002817 break;
2818
2819 case ACT_HTTP_SET_TOS:
Christopher Faulet96bff762019-12-17 13:46:18 +01002820 conn_set_tos(objt_conn(sess->origin), rule->arg.http.i);
Christopher Faulet3e964192018-10-24 11:39:23 +02002821 break;
2822
2823 case ACT_HTTP_SET_MARK:
Christopher Faulet96bff762019-12-17 13:46:18 +01002824 conn_set_mark(objt_conn(sess->origin), rule->arg.http.i);
Christopher Faulet3e964192018-10-24 11:39:23 +02002825 break;
2826
2827 case ACT_HTTP_SET_LOGL:
Christopher Faulet96bff762019-12-17 13:46:18 +01002828 s->logs.level = rule->arg.http.i;
Christopher Faulet3e964192018-10-24 11:39:23 +02002829 break;
2830
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002831 /* other flags exists, but normally, they never be matched. */
Christopher Faulet3e964192018-10-24 11:39:23 +02002832 default:
2833 break;
2834 }
2835 }
2836
2837 end:
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002838 /* if the ruleset evaluation is finished reset the strict mode */
Christopher Faulet46f95542019-12-20 10:07:22 +01002839 if (rule_ret != HTTP_RULE_RES_YIELD)
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002840 txn->req.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01002841
Christopher Faulet3e964192018-10-24 11:39:23 +02002842 /* we reached the end of the rules, nothing to report */
2843 return rule_ret;
2844}
2845
2846/* Executes the http-response rules <rules> for stream <s> and proxy <px>. It
2847 * returns one of 5 possible statuses: HTTP_RULE_RES_CONT, HTTP_RULE_RES_STOP,
2848 * HTTP_RULE_RES_DONE, HTTP_RULE_RES_YIELD, or HTTP_RULE_RES_BADREQ. If *CONT
2849 * is returned, the process can continue the evaluation of next rule list. If
2850 * *STOP or *DONE is returned, the process must stop the evaluation. If *BADREQ
2851 * is returned, it means the operation could not be processed and a server error
Christopher Fauleta53abad2020-05-13 08:12:22 +02002852 * must be returned. If *YIELD is returned, the caller must call again the
2853 * function with the same context.
Christopher Faulet3e964192018-10-24 11:39:23 +02002854 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002855static enum rule_result http_res_get_intercept_rule(struct proxy *px, struct list *rules,
2856 struct stream *s)
Christopher Faulet3e964192018-10-24 11:39:23 +02002857{
2858 struct session *sess = strm_sess(s);
2859 struct http_txn *txn = s->txn;
Christopher Faulet3e964192018-10-24 11:39:23 +02002860 struct act_rule *rule;
Christopher Faulet3e964192018-10-24 11:39:23 +02002861 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002862 int act_opts = 0;
Christopher Faulet3e964192018-10-24 11:39:23 +02002863
Christopher Faulet3e964192018-10-24 11:39:23 +02002864 /* If "the current_rule_list" match the executed rule list, we are in
2865 * resume condition. If a resume is needed it is always in the action
2866 * and never in the ACL or converters. In this case, we initialise the
2867 * current rule, and go to the action execution point.
2868 */
2869 if (s->current_rule) {
2870 rule = s->current_rule;
2871 s->current_rule = NULL;
2872 if (s->current_rule_list == rules)
2873 goto resume_execution;
2874 }
2875 s->current_rule_list = rules;
2876
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002877 /* start the ruleset evaluation in strict mode */
2878 txn->rsp.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01002879
Christopher Faulet3e964192018-10-24 11:39:23 +02002880 list_for_each_entry(rule, rules, list) {
2881 /* check optional condition */
2882 if (rule->cond) {
2883 int ret;
2884
2885 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
2886 ret = acl_pass(ret);
2887
2888 if (rule->cond->pol == ACL_COND_UNLESS)
2889 ret = !ret;
2890
2891 if (!ret) /* condition not matched */
2892 continue;
2893 }
2894
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002895 act_opts |= ACT_OPT_FIRST;
Christopher Faulet3e964192018-10-24 11:39:23 +02002896resume_execution:
Amaury Denoyelle03517732021-05-07 14:25:01 +02002897 if (rule->kw->flags & KWF_EXPERIMENTAL)
2898 mark_tainted(TAINTED_ACTION_EXP_EXECUTED);
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002899
2900 /* Always call the action function if defined */
2901 if (rule->action_ptr) {
2902 if ((s->req.flags & CF_READ_ERROR) ||
2903 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
2904 (px->options & PR_O_ABRT_CLOSE)))
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002905 act_opts |= ACT_OPT_FINAL;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002906
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002907 switch (rule->action_ptr(rule, px, sess, s, act_opts)) {
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002908 case ACT_RET_CONT:
2909 break;
2910 case ACT_RET_STOP:
2911 rule_ret = HTTP_RULE_RES_STOP;
2912 goto end;
2913 case ACT_RET_YIELD:
2914 s->current_rule = rule;
2915 rule_ret = HTTP_RULE_RES_YIELD;
2916 goto end;
2917 case ACT_RET_ERR:
2918 rule_ret = HTTP_RULE_RES_ERROR;
2919 goto end;
2920 case ACT_RET_DONE:
2921 rule_ret = HTTP_RULE_RES_DONE;
2922 goto end;
2923 case ACT_RET_DENY:
Christopher Fauletb58f62b2020-01-13 16:40:13 +01002924 if (txn->status == -1)
2925 txn->status = 502;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002926 rule_ret = HTTP_RULE_RES_DENY;
2927 goto end;
2928 case ACT_RET_ABRT:
2929 rule_ret = HTTP_RULE_RES_ABRT;
2930 goto end;
2931 case ACT_RET_INV:
2932 rule_ret = HTTP_RULE_RES_BADREQ;
2933 goto end;
2934 }
2935 continue; /* eval the next rule */
2936 }
2937
2938 /* If not action function defined, check for known actions */
Christopher Faulet3e964192018-10-24 11:39:23 +02002939 switch (rule->action) {
2940 case ACT_ACTION_ALLOW:
2941 rule_ret = HTTP_RULE_RES_STOP; /* "allow" rules are OK */
2942 goto end;
2943
2944 case ACT_ACTION_DENY:
Christopher Faulet5cb513a2020-05-13 17:56:56 +02002945 txn->status = rule->arg.http_reply->status;
2946 txn->http_reply = rule->arg.http_reply;
Christopher Faulet3a26bee2019-12-16 12:47:40 +01002947 rule_ret = HTTP_RULE_RES_DENY;
Christopher Faulet3e964192018-10-24 11:39:23 +02002948 goto end;
2949
2950 case ACT_HTTP_SET_NICE:
Christopher Faulet96bff762019-12-17 13:46:18 +01002951 s->task->nice = rule->arg.http.i;
Christopher Faulet3e964192018-10-24 11:39:23 +02002952 break;
2953
2954 case ACT_HTTP_SET_TOS:
Christopher Faulet96bff762019-12-17 13:46:18 +01002955 conn_set_tos(objt_conn(sess->origin), rule->arg.http.i);
Christopher Faulet3e964192018-10-24 11:39:23 +02002956 break;
2957
2958 case ACT_HTTP_SET_MARK:
Christopher Faulet96bff762019-12-17 13:46:18 +01002959 conn_set_mark(objt_conn(sess->origin), rule->arg.http.i);
Christopher Faulet3e964192018-10-24 11:39:23 +02002960 break;
2961
2962 case ACT_HTTP_SET_LOGL:
Christopher Faulet96bff762019-12-17 13:46:18 +01002963 s->logs.level = rule->arg.http.i;
Christopher Faulet3e964192018-10-24 11:39:23 +02002964 break;
2965
Christopher Faulet3e964192018-10-24 11:39:23 +02002966 case ACT_HTTP_REDIR:
Christopher Faulet49c2a702020-03-06 15:44:37 +01002967 rule_ret = HTTP_RULE_RES_ABRT;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002968 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
Christopher Faulet3a26bee2019-12-16 12:47:40 +01002969 rule_ret = HTTP_RULE_RES_ERROR;
Christopher Faulet3e964192018-10-24 11:39:23 +02002970 goto end;
2971
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002972 /* other flags exists, but normally, they never be matched. */
Christopher Faulet3e964192018-10-24 11:39:23 +02002973 default:
2974 break;
2975 }
2976 }
2977
2978 end:
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002979 /* if the ruleset evaluation is finished reset the strict mode */
Christopher Faulet46f95542019-12-20 10:07:22 +01002980 if (rule_ret != HTTP_RULE_RES_YIELD)
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002981 txn->rsp.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01002982
Christopher Faulet3e964192018-10-24 11:39:23 +02002983 /* we reached the end of the rules, nothing to report */
2984 return rule_ret;
2985}
2986
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002987/* Executes backend and frontend http-after-response rules for the stream <s>,
2988 * in that order. it return 1 on success and 0 on error. It is the caller
2989 * responsibility to catch error or ignore it. If it catches it, this function
2990 * may be called a second time, for the internal error.
2991 */
2992int http_eval_after_res_rules(struct stream *s)
2993{
2994 struct session *sess = s->sess;
2995 enum rule_result ret = HTTP_RULE_RES_CONT;
2996
Christopher Faulet507479b2020-05-15 12:29:46 +02002997 /* Eval after-response ruleset only if the reply is not const */
2998 if (s->txn->flags & TX_CONST_REPLY)
2999 goto end;
3000
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01003001 /* prune the request variables if not already done and swap to the response variables. */
3002 if (s->vars_reqres.scope != SCOPE_RES) {
3003 if (!LIST_ISEMPTY(&s->vars_reqres.head))
3004 vars_prune(&s->vars_reqres, s->sess, s);
3005 vars_init(&s->vars_reqres, SCOPE_RES);
3006 }
3007
3008 ret = http_res_get_intercept_rule(s->be, &s->be->http_after_res_rules, s);
Christopher Fauletf5db0fc2021-11-09 17:48:39 +01003009 if ((ret == HTTP_RULE_RES_CONT || ret == HTTP_RULE_RES_STOP) && sess->fe != s->be)
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01003010 ret = http_res_get_intercept_rule(sess->fe, &sess->fe->http_after_res_rules, s);
3011
Christopher Faulet507479b2020-05-15 12:29:46 +02003012 end:
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01003013 /* All other codes than CONTINUE, STOP or DONE are forbidden */
3014 return (ret == HTTP_RULE_RES_CONT || ret == HTTP_RULE_RES_STOP || ret == HTTP_RULE_RES_DONE);
3015}
3016
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003017/*
3018 * Manage client-side cookie. It can impact performance by about 2% so it is
3019 * desirable to call it only when needed. This code is quite complex because
3020 * of the multiple very crappy and ambiguous syntaxes we have to support. it
3021 * highly recommended not to touch this part without a good reason !
3022 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003023static void http_manage_client_side_cookies(struct stream *s, struct channel *req)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003024{
3025 struct session *sess = s->sess;
3026 struct http_txn *txn = s->txn;
3027 struct htx *htx;
3028 struct http_hdr_ctx ctx;
3029 char *hdr_beg, *hdr_end, *del_from;
3030 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
3031 int preserve_hdr;
3032
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003033 htx = htxbuf(&req->buf);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003034 ctx.blk = NULL;
3035 while (http_find_header(htx, ist("Cookie"), &ctx, 1)) {
Olivier Houchardf0f42382019-07-22 17:43:46 +02003036 int is_first = 1;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003037 del_from = NULL; /* nothing to be deleted */
3038 preserve_hdr = 0; /* assume we may kill the whole header */
3039
3040 /* Now look for cookies. Conforming to RFC2109, we have to support
3041 * attributes whose name begin with a '$', and associate them with
3042 * the right cookie, if we want to delete this cookie.
3043 * So there are 3 cases for each cookie read :
3044 * 1) it's a special attribute, beginning with a '$' : ignore it.
3045 * 2) it's a server id cookie that we *MAY* want to delete : save
3046 * some pointers on it (last semi-colon, beginning of cookie...)
3047 * 3) it's an application cookie : we *MAY* have to delete a previous
3048 * "special" cookie.
3049 * At the end of loop, if a "special" cookie remains, we may have to
3050 * remove it. If no application cookie persists in the header, we
3051 * *MUST* delete it.
3052 *
3053 * Note: RFC2965 is unclear about the processing of spaces around
3054 * the equal sign in the ATTR=VALUE form. A careful inspection of
3055 * the RFC explicitly allows spaces before it, and not within the
3056 * tokens (attrs or values). An inspection of RFC2109 allows that
3057 * too but section 10.1.3 lets one think that spaces may be allowed
3058 * after the equal sign too, resulting in some (rare) buggy
3059 * implementations trying to do that. So let's do what servers do.
3060 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
3061 * allowed quoted strings in values, with any possible character
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003062 * after a backslash, including control chars and delimiters, which
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003063 * causes parsing to become ambiguous. Browsers also allow spaces
3064 * within values even without quotes.
3065 *
3066 * We have to keep multiple pointers in order to support cookie
3067 * removal at the beginning, middle or end of header without
3068 * corrupting the header. All of these headers are valid :
3069 *
3070 * hdr_beg hdr_end
3071 * | |
3072 * v |
3073 * NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3 |
3074 * NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3 v
3075 * NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3
3076 * | | | | | | |
3077 * | | | | | | |
3078 * | | | | | | +--> next
3079 * | | | | | +----> val_end
3080 * | | | | +-----------> val_beg
3081 * | | | +--------------> equal
3082 * | | +----------------> att_end
3083 * | +---------------------> att_beg
3084 * +--------------------------> prev
3085 *
3086 */
3087 hdr_beg = ctx.value.ptr;
3088 hdr_end = hdr_beg + ctx.value.len;
3089 for (prev = hdr_beg; prev < hdr_end; prev = next) {
3090 /* Iterate through all cookies on this line */
3091
3092 /* find att_beg */
3093 att_beg = prev;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003094 if (!is_first)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003095 att_beg++;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003096 is_first = 0;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003097
3098 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
3099 att_beg++;
3100
3101 /* find att_end : this is the first character after the last non
3102 * space before the equal. It may be equal to hdr_end.
3103 */
3104 equal = att_end = att_beg;
3105 while (equal < hdr_end) {
3106 if (*equal == '=' || *equal == ',' || *equal == ';')
3107 break;
3108 if (HTTP_IS_SPHT(*equal++))
3109 continue;
3110 att_end = equal;
3111 }
3112
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003113 /* here, <equal> points to '=', a delimiter or the end. <att_end>
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003114 * is between <att_beg> and <equal>, both may be identical.
3115 */
3116 /* look for end of cookie if there is an equal sign */
3117 if (equal < hdr_end && *equal == '=') {
3118 /* look for the beginning of the value */
3119 val_beg = equal + 1;
3120 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
3121 val_beg++;
3122
3123 /* find the end of the value, respecting quotes */
3124 next = http_find_cookie_value_end(val_beg, hdr_end);
3125
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003126 /* make val_end point to the first white space or delimiter after the value */
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003127 val_end = next;
3128 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
3129 val_end--;
3130 }
3131 else
3132 val_beg = val_end = next = equal;
3133
3134 /* We have nothing to do with attributes beginning with
3135 * '$'. However, they will automatically be removed if a
3136 * header before them is removed, since they're supposed
3137 * to be linked together.
3138 */
3139 if (*att_beg == '$')
3140 continue;
3141
3142 /* Ignore cookies with no equal sign */
3143 if (equal == next) {
3144 /* This is not our cookie, so we must preserve it. But if we already
3145 * scheduled another cookie for removal, we cannot remove the
3146 * complete header, but we can remove the previous block itself.
3147 */
3148 preserve_hdr = 1;
3149 if (del_from != NULL) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003150 int delta = http_del_hdr_value(hdr_beg, hdr_end, &del_from, prev);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003151 val_end += delta;
3152 next += delta;
3153 hdr_end += delta;
3154 prev = del_from;
3155 del_from = NULL;
3156 }
3157 continue;
3158 }
3159
3160 /* if there are spaces around the equal sign, we need to
3161 * strip them otherwise we'll get trouble for cookie captures,
3162 * or even for rewrites. Since this happens extremely rarely,
3163 * it does not hurt performance.
3164 */
3165 if (unlikely(att_end != equal || val_beg > equal + 1)) {
3166 int stripped_before = 0;
3167 int stripped_after = 0;
3168
3169 if (att_end != equal) {
3170 memmove(att_end, equal, hdr_end - equal);
3171 stripped_before = (att_end - equal);
3172 equal += stripped_before;
3173 val_beg += stripped_before;
3174 }
3175
3176 if (val_beg > equal + 1) {
3177 memmove(equal + 1, val_beg, hdr_end + stripped_before - val_beg);
3178 stripped_after = (equal + 1) - val_beg;
3179 val_beg += stripped_after;
3180 stripped_before += stripped_after;
3181 }
3182
3183 val_end += stripped_before;
3184 next += stripped_before;
3185 hdr_end += stripped_before;
3186 }
3187 /* now everything is as on the diagram above */
3188
3189 /* First, let's see if we want to capture this cookie. We check
3190 * that we don't already have a client side cookie, because we
3191 * can only capture one. Also as an optimisation, we ignore
3192 * cookies shorter than the declared name.
3193 */
3194 if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL &&
3195 (val_end - att_beg >= sess->fe->capture_namelen) &&
3196 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
3197 int log_len = val_end - att_beg;
3198
3199 if ((txn->cli_cookie = pool_alloc(pool_head_capture)) == NULL) {
3200 ha_alert("HTTP logging : out of memory.\n");
3201 } else {
3202 if (log_len > sess->fe->capture_len)
3203 log_len = sess->fe->capture_len;
3204 memcpy(txn->cli_cookie, att_beg, log_len);
3205 txn->cli_cookie[log_len] = 0;
3206 }
3207 }
3208
3209 /* Persistence cookies in passive, rewrite or insert mode have the
3210 * following form :
3211 *
3212 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
3213 *
3214 * For cookies in prefix mode, the form is :
3215 *
3216 * Cookie: NAME=SRV~VALUE
3217 */
3218 if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
3219 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
3220 struct server *srv = s->be->srv;
3221 char *delim;
3222
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003223 /* if we're in cookie prefix mode, we'll search the delimiter so that we
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003224 * have the server ID between val_beg and delim, and the original cookie between
3225 * delim+1 and val_end. Otherwise, delim==val_end :
3226 *
3227 * hdr_beg
3228 * |
3229 * v
3230 * NAME=SRV; # in all but prefix modes
3231 * NAME=SRV~OPAQUE ; # in prefix mode
3232 * || || | |+-> next
3233 * || || | +--> val_end
3234 * || || +---------> delim
3235 * || |+------------> val_beg
3236 * || +-------------> att_end = equal
3237 * |+-----------------> att_beg
3238 * +------------------> prev
3239 *
3240 */
3241 if (s->be->ck_opts & PR_CK_PFX) {
3242 for (delim = val_beg; delim < val_end; delim++)
3243 if (*delim == COOKIE_DELIM)
3244 break;
3245 }
3246 else {
3247 char *vbar1;
3248 delim = val_end;
3249 /* Now check if the cookie contains a date field, which would
3250 * appear after a vertical bar ('|') just after the server name
3251 * and before the delimiter.
3252 */
3253 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
3254 if (vbar1) {
3255 /* OK, so left of the bar is the server's cookie and
3256 * right is the last seen date. It is a base64 encoded
3257 * 30-bit value representing the UNIX date since the
3258 * epoch in 4-second quantities.
3259 */
3260 int val;
3261 delim = vbar1++;
3262 if (val_end - vbar1 >= 5) {
3263 val = b64tos30(vbar1);
3264 if (val > 0)
3265 txn->cookie_last_date = val << 2;
3266 }
3267 /* look for a second vertical bar */
3268 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
3269 if (vbar1 && (val_end - vbar1 > 5)) {
3270 val = b64tos30(vbar1 + 1);
3271 if (val > 0)
3272 txn->cookie_first_date = val << 2;
3273 }
3274 }
3275 }
3276
3277 /* if the cookie has an expiration date and the proxy wants to check
3278 * it, then we do that now. We first check if the cookie is too old,
3279 * then only if it has expired. We detect strict overflow because the
3280 * time resolution here is not great (4 seconds). Cookies with dates
3281 * in the future are ignored if their offset is beyond one day. This
3282 * allows an admin to fix timezone issues without expiring everyone
3283 * and at the same time avoids keeping unwanted side effects for too
3284 * long.
3285 */
3286 if (txn->cookie_first_date && s->be->cookie_maxlife &&
3287 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) ||
3288 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
3289 txn->flags &= ~TX_CK_MASK;
3290 txn->flags |= TX_CK_OLD;
3291 delim = val_beg; // let's pretend we have not found the cookie
3292 txn->cookie_first_date = 0;
3293 txn->cookie_last_date = 0;
3294 }
3295 else if (txn->cookie_last_date && s->be->cookie_maxidle &&
3296 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) ||
3297 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
3298 txn->flags &= ~TX_CK_MASK;
3299 txn->flags |= TX_CK_EXPIRED;
3300 delim = val_beg; // let's pretend we have not found the cookie
3301 txn->cookie_first_date = 0;
3302 txn->cookie_last_date = 0;
3303 }
3304
3305 /* Here, we'll look for the first running server which supports the cookie.
3306 * This allows to share a same cookie between several servers, for example
3307 * to dedicate backup servers to specific servers only.
3308 * However, to prevent clients from sticking to cookie-less backup server
3309 * when they have incidentely learned an empty cookie, we simply ignore
3310 * empty cookies and mark them as invalid.
3311 * The same behaviour is applied when persistence must be ignored.
3312 */
3313 if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
3314 srv = NULL;
3315
3316 while (srv) {
3317 if (srv->cookie && (srv->cklen == delim - val_beg) &&
3318 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
3319 if ((srv->cur_state != SRV_ST_STOPPED) ||
3320 (s->be->options & PR_O_PERSIST) ||
3321 (s->flags & SF_FORCE_PRST)) {
3322 /* we found the server and we can use it */
3323 txn->flags &= ~TX_CK_MASK;
3324 txn->flags |= (srv->cur_state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
3325 s->flags |= SF_DIRECT | SF_ASSIGNED;
3326 s->target = &srv->obj_type;
3327 break;
3328 } else {
3329 /* we found a server, but it's down,
3330 * mark it as such and go on in case
3331 * another one is available.
3332 */
3333 txn->flags &= ~TX_CK_MASK;
3334 txn->flags |= TX_CK_DOWN;
3335 }
3336 }
3337 srv = srv->next;
3338 }
3339
3340 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
3341 /* no server matched this cookie or we deliberately skipped it */
3342 txn->flags &= ~TX_CK_MASK;
3343 if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
3344 txn->flags |= TX_CK_UNUSED;
3345 else
3346 txn->flags |= TX_CK_INVALID;
3347 }
3348
3349 /* depending on the cookie mode, we may have to either :
3350 * - delete the complete cookie if we're in insert+indirect mode, so that
3351 * the server never sees it ;
3352 * - remove the server id from the cookie value, and tag the cookie as an
Joseph Herlante9d5c722018-11-25 11:00:25 -08003353 * application cookie so that it does not get accidentally removed later,
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003354 * if we're in cookie prefix mode
3355 */
3356 if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
3357 int delta; /* negative */
3358
3359 memmove(val_beg, delim + 1, hdr_end - (delim + 1));
3360 delta = val_beg - (delim + 1);
3361 val_end += delta;
3362 next += delta;
3363 hdr_end += delta;
3364 del_from = NULL;
3365 preserve_hdr = 1; /* we want to keep this cookie */
3366 }
3367 else if (del_from == NULL &&
3368 (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
3369 del_from = prev;
3370 }
3371 }
3372 else {
3373 /* This is not our cookie, so we must preserve it. But if we already
3374 * scheduled another cookie for removal, we cannot remove the
3375 * complete header, but we can remove the previous block itself.
3376 */
3377 preserve_hdr = 1;
3378
3379 if (del_from != NULL) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003380 int delta = http_del_hdr_value(hdr_beg, hdr_end, &del_from, prev);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003381 if (att_beg >= del_from)
3382 att_beg += delta;
3383 if (att_end >= del_from)
3384 att_end += delta;
3385 val_beg += delta;
3386 val_end += delta;
3387 next += delta;
3388 hdr_end += delta;
3389 prev = del_from;
3390 del_from = NULL;
3391 }
3392 }
3393
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003394 } /* for each cookie */
3395
3396
3397 /* There are no more cookies on this line.
3398 * We may still have one (or several) marked for deletion at the
3399 * end of the line. We must do this now in two ways :
3400 * - if some cookies must be preserved, we only delete from the
3401 * mark to the end of line ;
3402 * - if nothing needs to be preserved, simply delete the whole header
3403 */
3404 if (del_from) {
3405 hdr_end = (preserve_hdr ? del_from : hdr_beg);
3406 }
3407 if ((hdr_end - hdr_beg) != ctx.value.len) {
Christopher Faulet3e2638e2019-06-18 09:49:16 +02003408 if (hdr_beg != hdr_end)
3409 htx_change_blk_value_len(htx, ctx.blk, hdr_end - hdr_beg);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003410 else
3411 http_remove_header(htx, &ctx);
3412 }
3413 } /* for each "Cookie header */
3414}
3415
3416/*
3417 * Manage server-side cookies. It can impact performance by about 2% so it is
3418 * desirable to call it only when needed. This function is also used when we
3419 * just need to know if there is a cookie (eg: for check-cache).
3420 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003421static void http_manage_server_side_cookies(struct stream *s, struct channel *res)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003422{
3423 struct session *sess = s->sess;
3424 struct http_txn *txn = s->txn;
3425 struct htx *htx;
3426 struct http_hdr_ctx ctx;
3427 struct server *srv;
3428 char *hdr_beg, *hdr_end;
3429 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau6f7a02a2019-04-15 21:49:49 +02003430 int is_cookie2 = 0;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003431
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003432 htx = htxbuf(&res->buf);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003433
3434 ctx.blk = NULL;
3435 while (1) {
Olivier Houchardf0f42382019-07-22 17:43:46 +02003436 int is_first = 1;
3437
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003438 if (!http_find_header(htx, ist("Set-Cookie"), &ctx, 1)) {
3439 if (!http_find_header(htx, ist("Set-Cookie2"), &ctx, 1))
3440 break;
3441 is_cookie2 = 1;
3442 }
3443
3444 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
3445 * <prev> points to the colon.
3446 */
3447 txn->flags |= TX_SCK_PRESENT;
3448
3449 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
3450 * check-cache is enabled) and we are not interested in checking
3451 * them. Warning, the cookie capture is declared in the frontend.
3452 */
3453 if (s->be->cookie_name == NULL && sess->fe->capture_name == NULL)
3454 break;
3455
3456 /* OK so now we know we have to process this response cookie.
3457 * The format of the Set-Cookie header is slightly different
3458 * from the format of the Cookie header in that it does not
3459 * support the comma as a cookie delimiter (thus the header
3460 * cannot be folded) because the Expires attribute described in
3461 * the original Netscape's spec may contain an unquoted date
3462 * with a comma inside. We have to live with this because
3463 * many browsers don't support Max-Age and some browsers don't
3464 * support quoted strings. However the Set-Cookie2 header is
3465 * clean.
3466 *
3467 * We have to keep multiple pointers in order to support cookie
3468 * removal at the beginning, middle or end of header without
3469 * corrupting the header (in case of set-cookie2). A special
3470 * pointer, <scav> points to the beginning of the set-cookie-av
3471 * fields after the first semi-colon. The <next> pointer points
3472 * either to the end of line (set-cookie) or next unquoted comma
3473 * (set-cookie2). All of these headers are valid :
3474 *
3475 * hdr_beg hdr_end
3476 * | |
3477 * v |
3478 * NAME1 = VALUE 1 ; Secure; Path="/" |
3479 * NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT v
3480 * NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT
3481 * NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard
3482 * | | | | | | | |
3483 * | | | | | | | +-> next
3484 * | | | | | | +------------> scav
3485 * | | | | | +--------------> val_end
3486 * | | | | +--------------------> val_beg
3487 * | | | +----------------------> equal
3488 * | | +------------------------> att_end
3489 * | +----------------------------> att_beg
3490 * +------------------------------> prev
3491 * -------------------------------> hdr_beg
3492 */
3493 hdr_beg = ctx.value.ptr;
3494 hdr_end = hdr_beg + ctx.value.len;
3495 for (prev = hdr_beg; prev < hdr_end; prev = next) {
3496
3497 /* Iterate through all cookies on this line */
3498
3499 /* find att_beg */
3500 att_beg = prev;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003501 if (!is_first)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003502 att_beg++;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003503 is_first = 0;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003504
3505 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
3506 att_beg++;
3507
3508 /* find att_end : this is the first character after the last non
3509 * space before the equal. It may be equal to hdr_end.
3510 */
3511 equal = att_end = att_beg;
3512
3513 while (equal < hdr_end) {
3514 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
3515 break;
3516 if (HTTP_IS_SPHT(*equal++))
3517 continue;
3518 att_end = equal;
3519 }
3520
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003521 /* here, <equal> points to '=', a delimiter or the end. <att_end>
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003522 * is between <att_beg> and <equal>, both may be identical.
3523 */
3524
3525 /* look for end of cookie if there is an equal sign */
3526 if (equal < hdr_end && *equal == '=') {
3527 /* look for the beginning of the value */
3528 val_beg = equal + 1;
3529 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
3530 val_beg++;
3531
3532 /* find the end of the value, respecting quotes */
3533 next = http_find_cookie_value_end(val_beg, hdr_end);
3534
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003535 /* make val_end point to the first white space or delimiter after the value */
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003536 val_end = next;
3537 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
3538 val_end--;
3539 }
3540 else {
3541 /* <equal> points to next comma, semi-colon or EOL */
3542 val_beg = val_end = next = equal;
3543 }
3544
3545 if (next < hdr_end) {
3546 /* Set-Cookie2 supports multiple cookies, and <next> points to
3547 * a colon or semi-colon before the end. So skip all attr-value
3548 * pairs and look for the next comma. For Set-Cookie, since
3549 * commas are permitted in values, skip to the end.
3550 */
3551 if (is_cookie2)
3552 next = http_find_hdr_value_end(next, hdr_end);
3553 else
3554 next = hdr_end;
3555 }
3556
3557 /* Now everything is as on the diagram above */
3558
3559 /* Ignore cookies with no equal sign */
3560 if (equal == val_end)
3561 continue;
3562
3563 /* If there are spaces around the equal sign, we need to
3564 * strip them otherwise we'll get trouble for cookie captures,
3565 * or even for rewrites. Since this happens extremely rarely,
3566 * it does not hurt performance.
3567 */
3568 if (unlikely(att_end != equal || val_beg > equal + 1)) {
3569 int stripped_before = 0;
3570 int stripped_after = 0;
3571
3572 if (att_end != equal) {
3573 memmove(att_end, equal, hdr_end - equal);
3574 stripped_before = (att_end - equal);
3575 equal += stripped_before;
3576 val_beg += stripped_before;
3577 }
3578
3579 if (val_beg > equal + 1) {
3580 memmove(equal + 1, val_beg, hdr_end + stripped_before - val_beg);
3581 stripped_after = (equal + 1) - val_beg;
3582 val_beg += stripped_after;
3583 stripped_before += stripped_after;
3584 }
3585
3586 val_end += stripped_before;
3587 next += stripped_before;
3588 hdr_end += stripped_before;
3589
Christopher Faulet3e2638e2019-06-18 09:49:16 +02003590 htx_change_blk_value_len(htx, ctx.blk, hdr_end - hdr_beg);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003591 ctx.value.len = hdr_end - hdr_beg;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003592 }
3593
3594 /* First, let's see if we want to capture this cookie. We check
3595 * that we don't already have a server side cookie, because we
3596 * can only capture one. Also as an optimisation, we ignore
3597 * cookies shorter than the declared name.
3598 */
3599 if (sess->fe->capture_name != NULL &&
3600 txn->srv_cookie == NULL &&
3601 (val_end - att_beg >= sess->fe->capture_namelen) &&
3602 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
3603 int log_len = val_end - att_beg;
3604 if ((txn->srv_cookie = pool_alloc(pool_head_capture)) == NULL) {
3605 ha_alert("HTTP logging : out of memory.\n");
3606 }
3607 else {
3608 if (log_len > sess->fe->capture_len)
3609 log_len = sess->fe->capture_len;
3610 memcpy(txn->srv_cookie, att_beg, log_len);
3611 txn->srv_cookie[log_len] = 0;
3612 }
3613 }
3614
3615 srv = objt_server(s->target);
3616 /* now check if we need to process it for persistence */
3617 if (!(s->flags & SF_IGNORE_PRST) &&
3618 (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
3619 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
3620 /* assume passive cookie by default */
3621 txn->flags &= ~TX_SCK_MASK;
3622 txn->flags |= TX_SCK_FOUND;
3623
3624 /* If the cookie is in insert mode on a known server, we'll delete
3625 * this occurrence because we'll insert another one later.
3626 * We'll delete it too if the "indirect" option is set and we're in
3627 * a direct access.
3628 */
3629 if (s->be->ck_opts & PR_CK_PSV) {
3630 /* The "preserve" flag was set, we don't want to touch the
3631 * server's cookie.
3632 */
3633 }
3634 else if ((srv && (s->be->ck_opts & PR_CK_INS)) ||
3635 ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) {
3636 /* this cookie must be deleted */
3637 if (prev == hdr_beg && next == hdr_end) {
3638 /* whole header */
3639 http_remove_header(htx, &ctx);
3640 /* note: while both invalid now, <next> and <hdr_end>
3641 * are still equal, so the for() will stop as expected.
3642 */
3643 } else {
3644 /* just remove the value */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003645 int delta = http_del_hdr_value(hdr_beg, hdr_end, &prev, next);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003646 next = prev;
3647 hdr_end += delta;
3648 }
3649 txn->flags &= ~TX_SCK_MASK;
3650 txn->flags |= TX_SCK_DELETED;
3651 /* and go on with next cookie */
3652 }
3653 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) {
3654 /* replace bytes val_beg->val_end with the cookie name associated
3655 * with this server since we know it.
3656 */
3657 int sliding, delta;
3658
3659 ctx.value = ist2(val_beg, val_end - val_beg);
3660 ctx.lws_before = ctx.lws_after = 0;
3661 http_replace_header_value(htx, &ctx, ist2(srv->cookie, srv->cklen));
3662 delta = srv->cklen - (val_end - val_beg);
3663 sliding = (ctx.value.ptr - val_beg);
3664 hdr_beg += sliding;
3665 val_beg += sliding;
3666 next += sliding + delta;
3667 hdr_end += sliding + delta;
3668
3669 txn->flags &= ~TX_SCK_MASK;
3670 txn->flags |= TX_SCK_REPLACED;
3671 }
3672 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) {
3673 /* insert the cookie name associated with this server
3674 * before existing cookie, and insert a delimiter between them..
3675 */
3676 int sliding, delta;
3677 ctx.value = ist2(val_beg, 0);
3678 ctx.lws_before = ctx.lws_after = 0;
3679 http_replace_header_value(htx, &ctx, ist2(srv->cookie, srv->cklen + 1));
3680 delta = srv->cklen + 1;
3681 sliding = (ctx.value.ptr - val_beg);
3682 hdr_beg += sliding;
3683 val_beg += sliding;
3684 next += sliding + delta;
3685 hdr_end += sliding + delta;
3686
3687 val_beg[srv->cklen] = COOKIE_DELIM;
3688 txn->flags &= ~TX_SCK_MASK;
3689 txn->flags |= TX_SCK_REPLACED;
3690 }
3691 }
3692 /* that's done for this cookie, check the next one on the same
3693 * line when next != hdr_end (only if is_cookie2).
3694 */
3695 }
3696 }
3697}
3698
Christopher Faulet25a02f62018-10-24 12:00:25 +02003699/*
3700 * Parses the Cache-Control and Pragma request header fields to determine if
3701 * the request may be served from the cache and/or if it is cacheable. Updates
3702 * s->txn->flags.
3703 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003704void http_check_request_for_cacheability(struct stream *s, struct channel *req)
Christopher Faulet25a02f62018-10-24 12:00:25 +02003705{
3706 struct http_txn *txn = s->txn;
3707 struct htx *htx;
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003708 struct http_hdr_ctx ctx = { .blk = NULL };
3709 int pragma_found, cc_found;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003710
3711 if ((txn->flags & (TX_CACHEABLE|TX_CACHE_IGNORE)) == TX_CACHE_IGNORE)
3712 return; /* nothing more to do here */
3713
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003714 htx = htxbuf(&req->buf);
Christopher Faulet25a02f62018-10-24 12:00:25 +02003715 pragma_found = cc_found = 0;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003716
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003717 /* Check "pragma" header for HTTP/1.0 compatibility. */
3718 if (http_find_header(htx, ist("pragma"), &ctx, 1)) {
3719 if (isteqi(ctx.value, ist("no-cache"))) {
3720 pragma_found = 1;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003721 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003722 }
Christopher Faulet25a02f62018-10-24 12:00:25 +02003723
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003724 ctx.blk = NULL;
3725 /* Don't use the cache and don't try to store if we found the
3726 * Authorization header */
3727 if (http_find_header(htx, ist("authorization"), &ctx, 1)) {
3728 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3729 txn->flags |= TX_CACHE_IGNORE;
3730 }
Christopher Faulet25a02f62018-10-24 12:00:25 +02003731
Christopher Faulet25a02f62018-10-24 12:00:25 +02003732
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003733 /* Look for "cache-control" header and iterate over all the values
3734 * until we find one that specifies that caching is possible or not. */
3735 ctx.blk = NULL;
3736 while (http_find_header(htx, ist("cache-control"), &ctx, 0)) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003737 cc_found = 1;
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003738 /* We don't check the values after max-age, max-stale nor min-fresh,
3739 * we simply don't use the cache when they're specified. */
3740 if (istmatchi(ctx.value, ist("max-age")) ||
3741 istmatchi(ctx.value, ist("no-cache")) ||
3742 istmatchi(ctx.value, ist("max-stale")) ||
3743 istmatchi(ctx.value, ist("min-fresh"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003744 txn->flags |= TX_CACHE_IGNORE;
3745 continue;
3746 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003747 if (istmatchi(ctx.value, ist("no-store"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003748 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3749 continue;
3750 }
3751 }
3752
3753 /* RFC7234#5.4:
3754 * When the Cache-Control header field is also present and
3755 * understood in a request, Pragma is ignored.
3756 * When the Cache-Control header field is not present in a
3757 * request, caches MUST consider the no-cache request
3758 * pragma-directive as having the same effect as if
3759 * "Cache-Control: no-cache" were present.
3760 */
3761 if (!cc_found && pragma_found)
3762 txn->flags |= TX_CACHE_IGNORE;
3763}
3764
3765/*
3766 * Check if response is cacheable or not. Updates s->txn->flags.
3767 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003768void http_check_response_for_cacheability(struct stream *s, struct channel *res)
Christopher Faulet25a02f62018-10-24 12:00:25 +02003769{
3770 struct http_txn *txn = s->txn;
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003771 struct http_hdr_ctx ctx = { .blk = NULL };
Christopher Faulet25a02f62018-10-24 12:00:25 +02003772 struct htx *htx;
Remi Tricot-Le Bretoncc9bf2e2020-11-12 11:14:41 +01003773 int has_freshness_info = 0;
3774 int has_validator = 0;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003775
3776 if (txn->status < 200) {
3777 /* do not try to cache interim responses! */
3778 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3779 return;
3780 }
3781
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003782 htx = htxbuf(&res->buf);
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003783 /* Check "pragma" header for HTTP/1.0 compatibility. */
3784 if (http_find_header(htx, ist("pragma"), &ctx, 1)) {
3785 if (isteqi(ctx.value, ist("no-cache"))) {
3786 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3787 return;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003788 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003789 }
Christopher Faulet25a02f62018-10-24 12:00:25 +02003790
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003791 /* Look for "cache-control" header and iterate over all the values
3792 * until we find one that specifies that caching is possible or not. */
3793 ctx.blk = NULL;
3794 while (http_find_header(htx, ist("cache-control"), &ctx, 0)) {
3795 if (isteqi(ctx.value, ist("public"))) {
3796 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003797 continue;
3798 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003799 if (isteqi(ctx.value, ist("private")) ||
3800 isteqi(ctx.value, ist("no-cache")) ||
3801 isteqi(ctx.value, ist("no-store")) ||
3802 isteqi(ctx.value, ist("max-age=0")) ||
3803 isteqi(ctx.value, ist("s-maxage=0"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003804 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003805 continue;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003806 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003807 /* We might have a no-cache="set-cookie" form. */
3808 if (istmatchi(ctx.value, ist("no-cache=\"set-cookie"))) {
3809 txn->flags &= ~TX_CACHE_COOK;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003810 continue;
3811 }
Remi Tricot-Le Bretoncc9bf2e2020-11-12 11:14:41 +01003812
3813 if (istmatchi(ctx.value, ist("s-maxage")) ||
3814 istmatchi(ctx.value, ist("max-age"))) {
3815 has_freshness_info = 1;
3816 continue;
3817 }
3818 }
3819
3820 /* If no freshness information could be found in Cache-Control values,
3821 * look for an Expires header. */
3822 if (!has_freshness_info) {
3823 ctx.blk = NULL;
3824 has_freshness_info = http_find_header(htx, ist("expires"), &ctx, 0);
Christopher Faulet25a02f62018-10-24 12:00:25 +02003825 }
Remi Tricot-Le Bretoncc9bf2e2020-11-12 11:14:41 +01003826
3827 /* If no freshness information could be found in Cache-Control or Expires
3828 * values, look for an explicit validator. */
3829 if (!has_freshness_info) {
3830 ctx.blk = NULL;
3831 has_validator = 1;
3832 if (!http_find_header(htx, ist("etag"), &ctx, 0)) {
3833 ctx.blk = NULL;
3834 if (!http_find_header(htx, ist("last-modified"), &ctx, 0))
3835 has_validator = 0;
3836 }
3837 }
3838
3839 /* We won't store an entry that has neither a cache validator nor an
3840 * explicit expiration time, as suggested in RFC 7234#3. */
3841 if (!has_freshness_info && !has_validator)
3842 txn->flags |= TX_CACHE_IGNORE;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003843}
3844
Christopher Faulet377c5a52018-10-24 21:21:30 +02003845/*
3846 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
3847 * for the current backend.
3848 *
3849 * It is assumed that the request is either a HEAD, GET, or POST and that the
3850 * uri_auth field is valid.
3851 *
3852 * Returns 1 if stats should be provided, otherwise 0.
3853 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003854static int http_stats_check_uri(struct stream *s, struct http_txn *txn, struct proxy *backend)
Christopher Faulet377c5a52018-10-24 21:21:30 +02003855{
3856 struct uri_auth *uri_auth = backend->uri_auth;
3857 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003858 struct htx_sl *sl;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003859 struct ist uri;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003860
3861 if (!uri_auth)
3862 return 0;
3863
3864 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
3865 return 0;
3866
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003867 htx = htxbuf(&s->req.buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02003868 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003869 uri = htx_sl_req_uri(sl);
Willy Tarreau1eb3b482019-10-31 15:50:28 +01003870 if (*uri_auth->uri_prefix == '/')
3871 uri = http_get_path(uri);
Christopher Faulet377c5a52018-10-24 21:21:30 +02003872
3873 /* check URI size */
3874 if (uri_auth->uri_len > uri.len)
3875 return 0;
3876
3877 if (memcmp(uri.ptr, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
3878 return 0;
3879
3880 return 1;
3881}
3882
3883/* This function prepares an applet to handle the stats. It can deal with the
3884 * "100-continue" expectation, check that admin rules are met for POST requests,
3885 * and program a response message if something was unexpected. It cannot fail
3886 * and always relies on the stats applet to complete the job. It does not touch
3887 * analysers nor counters, which are left to the caller. It does not touch
3888 * s->target which is supposed to already point to the stats applet. The caller
3889 * is expected to have already assigned an appctx to the stream.
3890 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003891static int http_handle_stats(struct stream *s, struct channel *req)
Christopher Faulet377c5a52018-10-24 21:21:30 +02003892{
3893 struct stats_admin_rule *stats_admin_rule;
3894 struct stream_interface *si = &s->si[1];
3895 struct session *sess = s->sess;
3896 struct http_txn *txn = s->txn;
3897 struct http_msg *msg = &txn->req;
3898 struct uri_auth *uri_auth = s->be->uri_auth;
3899 const char *h, *lookup, *end;
3900 struct appctx *appctx;
3901 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003902 struct htx_sl *sl;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003903
3904 appctx = si_appctx(si);
3905 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
3906 appctx->st1 = appctx->st2 = 0;
3907 appctx->ctx.stats.st_code = STAT_STATUS_INIT;
Willy Tarreau676c29e2019-10-09 10:50:01 +02003908 appctx->ctx.stats.flags |= uri_auth->flags;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003909 appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */
3910 if ((msg->flags & HTTP_MSGF_VER_11) && (txn->meth != HTTP_METH_HEAD))
3911 appctx->ctx.stats.flags |= STAT_CHUNKED;
3912
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003913 htx = htxbuf(&req->buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02003914 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003915 lookup = HTX_SL_REQ_UPTR(sl) + uri_auth->uri_len;
3916 end = HTX_SL_REQ_UPTR(sl) + HTX_SL_REQ_ULEN(sl);
Christopher Faulet377c5a52018-10-24 21:21:30 +02003917
3918 for (h = lookup; h <= end - 3; h++) {
3919 if (memcmp(h, ";up", 3) == 0) {
3920 appctx->ctx.stats.flags |= STAT_HIDE_DOWN;
3921 break;
3922 }
Amaury Denoyelle91e55ea2021-02-25 14:46:08 +01003923 }
3924
3925 for (h = lookup; h <= end - 9; h++) {
3926 if (memcmp(h, ";no-maint", 9) == 0) {
Willy Tarreau3e320362020-10-23 17:28:57 +02003927 appctx->ctx.stats.flags |= STAT_HIDE_MAINT;
3928 break;
3929 }
Christopher Faulet377c5a52018-10-24 21:21:30 +02003930 }
3931
3932 if (uri_auth->refresh) {
3933 for (h = lookup; h <= end - 10; h++) {
3934 if (memcmp(h, ";norefresh", 10) == 0) {
3935 appctx->ctx.stats.flags |= STAT_NO_REFRESH;
3936 break;
3937 }
3938 }
3939 }
3940
3941 for (h = lookup; h <= end - 4; h++) {
3942 if (memcmp(h, ";csv", 4) == 0) {
Christopher Faulet6338a082019-09-09 15:50:54 +02003943 appctx->ctx.stats.flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
Christopher Faulet377c5a52018-10-24 21:21:30 +02003944 break;
3945 }
3946 }
3947
3948 for (h = lookup; h <= end - 6; h++) {
3949 if (memcmp(h, ";typed", 6) == 0) {
Christopher Faulet6338a082019-09-09 15:50:54 +02003950 appctx->ctx.stats.flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
Christopher Faulet377c5a52018-10-24 21:21:30 +02003951 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
3952 break;
3953 }
3954 }
3955
Christopher Faulet6338a082019-09-09 15:50:54 +02003956 for (h = lookup; h <= end - 5; h++) {
3957 if (memcmp(h, ";json", 5) == 0) {
3958 appctx->ctx.stats.flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
3959 appctx->ctx.stats.flags |= STAT_FMT_JSON;
3960 break;
3961 }
3962 }
3963
3964 for (h = lookup; h <= end - 12; h++) {
3965 if (memcmp(h, ";json-schema", 12) == 0) {
3966 appctx->ctx.stats.flags &= ~STAT_FMT_MASK;
3967 appctx->ctx.stats.flags |= STAT_JSON_SCHM;
3968 break;
3969 }
3970 }
3971
Christopher Faulet377c5a52018-10-24 21:21:30 +02003972 for (h = lookup; h <= end - 8; h++) {
3973 if (memcmp(h, ";st=", 4) == 0) {
3974 int i;
3975 h += 4;
3976 appctx->ctx.stats.st_code = STAT_STATUS_UNKN;
3977 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
3978 if (strncmp(stat_status_codes[i], h, 4) == 0) {
3979 appctx->ctx.stats.st_code = i;
3980 break;
3981 }
3982 }
3983 break;
3984 }
3985 }
3986
3987 appctx->ctx.stats.scope_str = 0;
3988 appctx->ctx.stats.scope_len = 0;
3989 for (h = lookup; h <= end - 8; h++) {
3990 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
3991 int itx = 0;
3992 const char *h2;
3993 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
3994 const char *err;
3995
3996 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
3997 h2 = h;
Christopher Fauleted7a0662019-01-14 11:07:34 +01003998 appctx->ctx.stats.scope_str = h2 - HTX_SL_REQ_UPTR(sl);
3999 while (h < end) {
Christopher Faulet377c5a52018-10-24 21:21:30 +02004000 if (*h == ';' || *h == '&' || *h == ' ')
4001 break;
4002 itx++;
4003 h++;
4004 }
4005
4006 if (itx > STAT_SCOPE_TXT_MAXLEN)
4007 itx = STAT_SCOPE_TXT_MAXLEN;
4008 appctx->ctx.stats.scope_len = itx;
4009
4010 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
4011 memcpy(scope_txt, h2, itx);
4012 scope_txt[itx] = '\0';
4013 err = invalid_char(scope_txt);
4014 if (err) {
4015 /* bad char in search text => clear scope */
4016 appctx->ctx.stats.scope_str = 0;
4017 appctx->ctx.stats.scope_len = 0;
4018 }
4019 break;
4020 }
4021 }
4022
4023 /* now check whether we have some admin rules for this request */
4024 list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) {
4025 int ret = 1;
4026
4027 if (stats_admin_rule->cond) {
4028 ret = acl_exec_cond(stats_admin_rule->cond, s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
4029 ret = acl_pass(ret);
4030 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
4031 ret = !ret;
4032 }
4033
4034 if (ret) {
4035 /* no rule, or the rule matches */
4036 appctx->ctx.stats.flags |= STAT_ADMIN;
4037 break;
4038 }
4039 }
4040
Christopher Faulet5d45e382019-02-27 15:15:23 +01004041 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
4042 appctx->st0 = STAT_HTTP_HEAD;
4043 else if (txn->meth == HTTP_METH_POST) {
Christopher Fauletbd9e8422019-08-15 22:26:48 +02004044 if (appctx->ctx.stats.flags & STAT_ADMIN) {
Christopher Faulet377c5a52018-10-24 21:21:30 +02004045 appctx->st0 = STAT_HTTP_POST;
Christopher Fauletbd9e8422019-08-15 22:26:48 +02004046 if (msg->msg_state < HTTP_MSG_DATA)
4047 req->analysers |= AN_REQ_HTTP_BODY;
4048 }
Christopher Faulet377c5a52018-10-24 21:21:30 +02004049 else {
Christopher Faulet5d45e382019-02-27 15:15:23 +01004050 /* POST without admin level */
Christopher Faulet377c5a52018-10-24 21:21:30 +02004051 appctx->ctx.stats.flags &= ~STAT_CHUNKED;
4052 appctx->ctx.stats.st_code = STAT_STATUS_DENY;
4053 appctx->st0 = STAT_HTTP_LAST;
4054 }
4055 }
4056 else {
Christopher Faulet5d45e382019-02-27 15:15:23 +01004057 /* Unsupported method */
4058 appctx->ctx.stats.flags &= ~STAT_CHUNKED;
4059 appctx->ctx.stats.st_code = STAT_STATUS_IVAL;
4060 appctx->st0 = STAT_HTTP_LAST;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004061 }
4062
4063 s->task->nice = -32; /* small boost for HTTP statistics */
4064 return 1;
4065}
4066
Christopher Faulet021a8e42021-03-29 10:46:38 +02004067/* This function waits for the message payload at most <time> milliseconds (may
4068 * be set to TICK_ETERNITY). It stops to wait if at least <bytes> bytes of the
4069 * payload are received (0 means no limit). It returns HTTP_RULE_* depending on
4070 * the result:
4071 *
4072 * - HTTP_RULE_RES_CONT when conditions are met to stop waiting
4073 * - HTTP_RULE_RES_YIELD to wait for more data
Ilya Shipitsinb2be9a12021-04-24 13:25:42 +05004074 * - HTTP_RULE_RES_ABRT when a timeout occurred.
Christopher Faulet021a8e42021-03-29 10:46:38 +02004075 * - HTTP_RULE_RES_BADREQ if a parsing error is raised by lower level
Ilya Shipitsinb2be9a12021-04-24 13:25:42 +05004076 * - HTTP_RULE_RES_ERROR if an internal error occurred
Christopher Faulet021a8e42021-03-29 10:46:38 +02004077 *
Ilya Shipitsinb2be9a12021-04-24 13:25:42 +05004078 * If a timeout occurred, this function is responsible to emit the right response
Christopher Faulet021a8e42021-03-29 10:46:38 +02004079 * to the client, depending on the channel (408 on request side, 504 on response
4080 * side). All other errors must be handled by the caller.
4081 */
4082enum rule_result http_wait_for_msg_body(struct stream *s, struct channel *chn,
4083 unsigned int time, unsigned int bytes)
4084{
4085 struct session *sess = s->sess;
4086 struct http_txn *txn = s->txn;
4087 struct http_msg *msg = ((chn->flags & CF_ISRESP) ? &txn->rsp : &txn->req);
4088 struct htx *htx;
4089 enum rule_result ret = HTTP_RULE_RES_CONT;
4090
4091 htx = htxbuf(&chn->buf);
4092
4093 if (htx->flags & HTX_FL_PARSING_ERROR) {
4094 ret = HTTP_RULE_RES_BADREQ;
4095 goto end;
4096 }
4097 if (htx->flags & HTX_FL_PROCESSING_ERROR) {
4098 ret = HTTP_RULE_RES_ERROR;
4099 goto end;
4100 }
4101
4102 /* Do nothing for bodyless and CONNECT requests */
4103 if (txn->meth == HTTP_METH_CONNECT || (msg->flags & HTTP_MSGF_BODYLESS))
4104 goto end;
4105
4106 if (!(chn->flags & CF_ISRESP) && msg->msg_state < HTTP_MSG_DATA) {
4107 if (http_handle_expect_hdr(s, htx, msg) == -1) {
4108 ret = HTTP_RULE_RES_ERROR;
4109 goto end;
4110 }
4111 }
4112
4113 msg->msg_state = HTTP_MSG_DATA;
4114
4115 /* Now we're in HTTP_MSG_DATA. We just need to know if all data have
4116 * been received or if the buffer is full.
4117 */
Christopher Faulet59df8122021-09-23 14:46:32 +02004118 if ((htx->flags & HTX_FL_EOM) ||
4119 htx_get_tail_type(htx) > HTX_BLK_DATA ||
4120 channel_htx_full(chn, htx, global.tune.maxrewrite) ||
4121 si_rx_blocked_room(chn_prod(chn)))
Christopher Faulet021a8e42021-03-29 10:46:38 +02004122 goto end;
4123
4124 if (bytes) {
4125 struct htx_blk *blk;
4126 unsigned int len = 0;
4127
4128 for (blk = htx_get_first_blk(htx); blk; blk = htx_get_next_blk(htx, blk)) {
4129 if (htx_get_blk_type(blk) != HTX_BLK_DATA)
4130 continue;
4131 len += htx_get_blksz(blk);
4132 if (len >= bytes)
4133 goto end;
4134 }
4135 }
4136
4137 if ((chn->flags & CF_READ_TIMEOUT) || tick_is_expired(chn->analyse_exp, now_ms)) {
4138 if (!(chn->flags & CF_ISRESP))
4139 goto abort_req;
4140 goto abort_res;
4141 }
4142
4143 /* we get here if we need to wait for more data */
4144 if (!(chn->flags & (CF_SHUTR | CF_READ_ERROR))) {
4145 if (!tick_isset(chn->analyse_exp))
4146 chn->analyse_exp = tick_add_ifset(now_ms, time);
4147 ret = HTTP_RULE_RES_YIELD;
4148 }
4149
4150 end:
4151 return ret;
4152
4153 abort_req:
4154 txn->status = 408;
4155 if (!(s->flags & SF_ERR_MASK))
4156 s->flags |= SF_ERR_CLITO;
4157 if (!(s->flags & SF_FINST_MASK))
4158 s->flags |= SF_FINST_D;
Willy Tarreau4781b152021-04-06 13:53:36 +02004159 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req);
Christopher Faulet021a8e42021-03-29 10:46:38 +02004160 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02004161 _HA_ATOMIC_INC(&sess->listener->counters->failed_req);
Christopher Faulet021a8e42021-03-29 10:46:38 +02004162 http_reply_and_close(s, txn->status, http_error_message(s));
4163 ret = HTTP_RULE_RES_ABRT;
4164 goto end;
4165
4166 abort_res:
4167 txn->status = 504;
4168 if (!(s->flags & SF_ERR_MASK))
4169 s->flags |= SF_ERR_SRVTO;
4170 if (!(s->flags & SF_FINST_MASK))
4171 s->flags |= SF_FINST_D;
4172 stream_inc_http_fail_ctr(s);
4173 http_reply_and_close(s, txn->status, http_error_message(s));
4174 ret = HTTP_RULE_RES_ABRT;
4175 goto end;
4176}
4177
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004178void http_perform_server_redirect(struct stream *s, struct stream_interface *si)
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004179{
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004180 struct channel *req = &s->req;
4181 struct channel *res = &s->res;
4182 struct server *srv;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004183 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004184 struct htx_sl *sl;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004185 struct ist path, location;
4186 unsigned int flags;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004187
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004188 /*
4189 * Create the location
4190 */
4191 chunk_reset(&trash);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004192
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004193 /* 1: add the server's prefix */
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004194 /* special prefix "/" means don't change URL */
4195 srv = __objt_server(s->target);
4196 if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') {
4197 if (!chunk_memcat(&trash, srv->rdr_pfx, srv->rdr_len))
4198 return;
4199 }
4200
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004201 /* 2: add the request Path */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004202 htx = htxbuf(&req->buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02004203 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004204 path = http_get_path(htx_sl_req_uri(sl));
Tim Duesterhused526372020-03-05 17:56:33 +01004205 if (!isttest(path))
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004206 return;
4207
4208 if (!chunk_memcat(&trash, path.ptr, path.len))
4209 return;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004210 location = ist2(trash.area, trash.data);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004211
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004212 /*
4213 * Create the 302 respone
4214 */
4215 htx = htx_from_buf(&res->buf);
4216 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
4217 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
4218 ist("HTTP/1.1"), ist("302"), ist("Found"));
4219 if (!sl)
4220 goto fail;
4221 sl->info.res.status = 302;
4222 s->txn->status = 302;
4223
4224 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) ||
4225 !htx_add_header(htx, ist("Connection"), ist("close")) ||
4226 !htx_add_header(htx, ist("Content-length"), ist("0")) ||
4227 !htx_add_header(htx, ist("Location"), location))
4228 goto fail;
4229
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004230 if (!htx_add_endof(htx, HTX_BLK_EOH))
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004231 goto fail;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004232
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004233 htx->flags |= HTX_FL_EOM;
Christopher Fauletc20afb82020-01-24 19:16:26 +01004234 htx_to_buf(htx, &res->buf);
Christopher Fauleta72a7e42020-01-28 09:28:11 +01004235 if (!http_forward_proxy_resp(s, 1))
4236 goto fail;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004237
4238 /* return without error. */
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004239 si_shutr(si);
4240 si_shutw(si);
4241 si->err_type = SI_ET_NONE;
4242 si->state = SI_ST_CLO;
4243
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004244 if (!(s->flags & SF_ERR_MASK))
4245 s->flags |= SF_ERR_LOCAL;
4246 if (!(s->flags & SF_FINST_MASK))
4247 s->flags |= SF_FINST_C;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004248
4249 /* FIXME: we should increase a counter of redirects per server and per backend. */
4250 srv_inc_sess_ctr(srv);
4251 srv_set_sess_last(srv);
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004252 return;
4253
4254 fail:
4255 /* If an error occurred, remove the incomplete HTTP response from the
4256 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004257 channel_htx_truncate(res, htx);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004258}
4259
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05004260/* This function terminates the request because it was completely analyzed or
Christopher Fauletf2824e62018-10-01 12:12:37 +02004261 * because an error was triggered during the body forwarding.
4262 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004263static void http_end_request(struct stream *s)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004264{
4265 struct channel *chn = &s->req;
4266 struct http_txn *txn = s->txn;
4267
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004268 DBG_TRACE_ENTER(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004269
Christopher Fauletb42a8b62018-11-19 21:59:00 +01004270 if (unlikely(txn->req.msg_state == HTTP_MSG_ERROR ||
4271 txn->rsp.msg_state == HTTP_MSG_ERROR)) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004272 channel_abort(chn);
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004273 channel_htx_truncate(chn, htxbuf(&chn->buf));
Christopher Fauletf2824e62018-10-01 12:12:37 +02004274 goto end;
4275 }
4276
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004277 if (unlikely(txn->req.msg_state < HTTP_MSG_DONE)) {
4278 DBG_TRACE_DEVEL("waiting end of the request", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004279 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004280 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004281
4282 if (txn->req.msg_state == HTTP_MSG_DONE) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004283 /* No need to read anymore, the request was completely parsed.
4284 * We can shut the read side unless we want to abort_on_close,
4285 * or we have a POST request. The issue with POST requests is
4286 * that some browsers still send a CRLF after the request, and
4287 * this CRLF must be read so that it does not remain in the kernel
4288 * buffers, otherwise a close could cause an RST on some systems
4289 * (eg: Linux).
4290 */
Christopher Faulet769d0e92019-03-22 14:23:18 +01004291 if (!(s->be->options & PR_O_ABRT_CLOSE) && txn->meth != HTTP_METH_POST)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004292 channel_dont_read(chn);
4293
4294 /* if the server closes the connection, we want to immediately react
4295 * and close the socket to save packets and syscalls.
4296 */
4297 s->si[1].flags |= SI_FL_NOHALF;
4298
4299 /* In any case we've finished parsing the request so we must
4300 * disable Nagle when sending data because 1) we're not going
4301 * to shut this side, and 2) the server is waiting for us to
4302 * send pending data.
4303 */
4304 chn->flags |= CF_NEVER_WAIT;
4305
Christopher Fauletd01ce402019-01-02 17:44:13 +01004306 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
4307 /* The server has not finished to respond, so we
4308 * don't want to move in order not to upset it.
4309 */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004310 DBG_TRACE_DEVEL("waiting end of the response", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletd01ce402019-01-02 17:44:13 +01004311 return;
4312 }
4313
Christopher Fauletf2824e62018-10-01 12:12:37 +02004314 /* When we get here, it means that both the request and the
4315 * response have finished receiving. Depending on the connection
4316 * mode, we'll have to wait for the last bytes to leave in either
4317 * direction, and sometimes for a close to be effective.
4318 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02004319 if (txn->flags & TX_CON_WANT_TUN) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004320 /* Tunnel mode will not have any analyser so it needs to
4321 * poll for reads.
4322 */
4323 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004324 if (b_data(&chn->buf)) {
4325 DBG_TRACE_DEVEL("waiting to flush the request", STRM_EV_HTTP_ANA, s, txn);
Christopher Faulet9768c262018-10-22 09:34:31 +02004326 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004327 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004328 txn->req.msg_state = HTTP_MSG_TUNNEL;
4329 }
4330 else {
4331 /* we're not expecting any new data to come for this
4332 * transaction, so we can close it.
Christopher Faulet9768c262018-10-22 09:34:31 +02004333 *
4334 * However, there is an exception if the response
4335 * length is undefined. In this case, we need to wait
4336 * the close from the server. The response will be
4337 * switched in TUNNEL mode until the end.
Christopher Fauletf2824e62018-10-01 12:12:37 +02004338 */
4339 if (!(txn->rsp.flags & HTTP_MSGF_XFER_LEN) &&
4340 txn->rsp.msg_state != HTTP_MSG_CLOSED)
Christopher Faulet9768c262018-10-22 09:34:31 +02004341 goto check_channel_flags;
Christopher Fauletf2824e62018-10-01 12:12:37 +02004342
4343 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4344 channel_shutr_now(chn);
4345 channel_shutw_now(chn);
4346 }
4347 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004348 goto check_channel_flags;
4349 }
4350
4351 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
4352 http_msg_closing:
4353 /* nothing else to forward, just waiting for the output buffer
4354 * to be empty and for the shutw_now to take effect.
4355 */
4356 if (channel_is_empty(chn)) {
4357 txn->req.msg_state = HTTP_MSG_CLOSED;
4358 goto http_msg_closed;
4359 }
4360 else if (chn->flags & CF_SHUTW) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004361 txn->req.msg_state = HTTP_MSG_ERROR;
4362 goto end;
4363 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004364 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004365 return;
4366 }
4367
4368 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
4369 http_msg_closed:
Christopher Fauletf2824e62018-10-01 12:12:37 +02004370 /* if we don't know whether the server will close, we need to hard close */
4371 if (txn->rsp.flags & HTTP_MSGF_XFER_LEN)
4372 s->si[1].flags |= SI_FL_NOLINGER; /* we want to close ASAP */
Christopher Fauletf2824e62018-10-01 12:12:37 +02004373 /* see above in MSG_DONE why we only do this in these states */
Christopher Faulet769d0e92019-03-22 14:23:18 +01004374 if (!(s->be->options & PR_O_ABRT_CLOSE))
Christopher Fauletf2824e62018-10-01 12:12:37 +02004375 channel_dont_read(chn);
4376 goto end;
4377 }
4378
4379 check_channel_flags:
4380 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4381 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4382 /* if we've just closed an output, let's switch */
4383 txn->req.msg_state = HTTP_MSG_CLOSING;
4384 goto http_msg_closing;
4385 }
4386
4387 end:
4388 chn->analysers &= AN_REQ_FLT_END;
Christopher Faulet198ef8b2020-12-15 13:32:55 +01004389 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
4390 chn->flags |= CF_NEVER_WAIT;
4391 if (HAS_REQ_DATA_FILTERS(s))
Christopher Fauletf2824e62018-10-01 12:12:37 +02004392 chn->analysers |= AN_REQ_FLT_XFER_DATA;
Christopher Faulet198ef8b2020-12-15 13:32:55 +01004393 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004394 channel_auto_close(chn);
4395 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004396 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004397}
4398
4399
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05004400/* This function terminates the response because it was completely analyzed or
Christopher Fauletf2824e62018-10-01 12:12:37 +02004401 * because an error was triggered during the body forwarding.
4402 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004403static void http_end_response(struct stream *s)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004404{
4405 struct channel *chn = &s->res;
4406 struct http_txn *txn = s->txn;
4407
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004408 DBG_TRACE_ENTER(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004409
Christopher Fauletb42a8b62018-11-19 21:59:00 +01004410 if (unlikely(txn->req.msg_state == HTTP_MSG_ERROR ||
4411 txn->rsp.msg_state == HTTP_MSG_ERROR)) {
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004412 channel_htx_truncate(&s->req, htxbuf(&s->req.buf));
Christopher Faulet9768c262018-10-22 09:34:31 +02004413 channel_abort(&s->req);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004414 goto end;
4415 }
4416
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004417 if (unlikely(txn->rsp.msg_state < HTTP_MSG_DONE)) {
4418 DBG_TRACE_DEVEL("waiting end of the response", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004419 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004420 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004421
4422 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
4423 /* In theory, we don't need to read anymore, but we must
4424 * still monitor the server connection for a possible close
4425 * while the request is being uploaded, so we don't disable
4426 * reading.
4427 */
4428 /* channel_dont_read(chn); */
4429
4430 if (txn->req.msg_state < HTTP_MSG_DONE) {
4431 /* The client seems to still be sending data, probably
4432 * because we got an error response during an upload.
4433 * We have the choice of either breaking the connection
4434 * or letting it pass through. Let's do the later.
4435 */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004436 DBG_TRACE_DEVEL("waiting end of the request", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004437 return;
4438 }
4439
4440 /* When we get here, it means that both the request and the
4441 * response have finished receiving. Depending on the connection
4442 * mode, we'll have to wait for the last bytes to leave in either
4443 * direction, and sometimes for a close to be effective.
4444 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02004445 if (txn->flags & TX_CON_WANT_TUN) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004446 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004447 if (b_data(&chn->buf)) {
4448 DBG_TRACE_DEVEL("waiting to flush the respone", STRM_EV_HTTP_ANA, s, txn);
Christopher Faulet9768c262018-10-22 09:34:31 +02004449 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004450 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004451 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
4452 }
4453 else {
4454 /* we're not expecting any new data to come for this
4455 * transaction, so we can close it.
4456 */
4457 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4458 channel_shutr_now(chn);
4459 channel_shutw_now(chn);
4460 }
4461 }
4462 goto check_channel_flags;
4463 }
4464
4465 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
4466 http_msg_closing:
4467 /* nothing else to forward, just waiting for the output buffer
4468 * to be empty and for the shutw_now to take effect.
4469 */
4470 if (channel_is_empty(chn)) {
4471 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4472 goto http_msg_closed;
4473 }
4474 else if (chn->flags & CF_SHUTW) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004475 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau4781b152021-04-06 13:53:36 +02004476 _HA_ATOMIC_INC(&strm_sess(s)->fe->fe_counters.cli_aborts);
4477 _HA_ATOMIC_INC(&s->be->be_counters.cli_aborts);
William Lallemand36119de2021-03-08 15:26:48 +01004478 if (strm_sess(s)->listener && strm_sess(s)->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02004479 _HA_ATOMIC_INC(&strm_sess(s)->listener->counters->cli_aborts);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004480 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02004481 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.cli_aborts);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004482 goto end;
4483 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004484 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004485 return;
4486 }
4487
4488 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
4489 http_msg_closed:
4490 /* drop any pending data */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004491 channel_htx_truncate(&s->req, htxbuf(&s->req.buf));
Christopher Faulet9768c262018-10-22 09:34:31 +02004492 channel_abort(&s->req);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004493 goto end;
4494 }
4495
4496 check_channel_flags:
4497 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4498 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4499 /* if we've just closed an output, let's switch */
4500 txn->rsp.msg_state = HTTP_MSG_CLOSING;
4501 goto http_msg_closing;
4502 }
4503
4504 end:
4505 chn->analysers &= AN_RES_FLT_END;
Christopher Faulet198ef8b2020-12-15 13:32:55 +01004506 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
4507 chn->flags |= CF_NEVER_WAIT;
4508 if (HAS_RSP_DATA_FILTERS(s))
4509 chn->analysers |= AN_RES_FLT_XFER_DATA;
4510 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004511 channel_auto_close(chn);
4512 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004513 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004514}
4515
Christopher Fauletef70e252020-01-28 09:26:19 +01004516/* Forward a response generated by HAProxy (error/redirect/return). This
4517 * function forwards all pending incoming data. If <final> is set to 0, nothing
4518 * more is performed. It is used for 1xx informational messages. Otherwise, the
Christopher Faulet507479b2020-05-15 12:29:46 +02004519 * transaction is terminated and the request is emptied. On success 1 is
Christopher Faulet40e6b552020-06-25 16:04:50 +02004520 * returned. If an error occurred, 0 is returned. If it fails, this function
4521 * only exits. It is the caller responsibility to do the cleanup.
Christopher Fauletef70e252020-01-28 09:26:19 +01004522 */
4523int http_forward_proxy_resp(struct stream *s, int final)
4524{
4525 struct channel *req = &s->req;
4526 struct channel *res = &s->res;
4527 struct htx *htx = htxbuf(&res->buf);
4528 size_t data;
4529
4530 if (final) {
4531 htx->flags |= HTX_FL_PROXY_RESP;
Christopher Faulet507479b2020-05-15 12:29:46 +02004532
Christopher Fauletaab1b672020-11-18 16:44:02 +01004533 if (!htx_is_empty(htx) && !http_eval_after_res_rules(s))
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01004534 return 0;
Christopher Fauletef70e252020-01-28 09:26:19 +01004535
Christopher Fauletd6c48362020-10-19 18:01:38 +02004536 if (s->txn->meth == HTTP_METH_HEAD)
4537 htx_skip_msg_payload(htx);
4538
Christopher Fauletef70e252020-01-28 09:26:19 +01004539 channel_auto_read(req);
4540 channel_abort(req);
4541 channel_auto_close(req);
4542 channel_htx_erase(req, htxbuf(&req->buf));
4543
4544 res->wex = tick_add_ifset(now_ms, res->wto);
4545 channel_auto_read(res);
4546 channel_auto_close(res);
4547 channel_shutr_now(res);
Christopher Faulet1a9db7c2020-06-25 15:36:45 +02004548 res->flags |= CF_EOI; /* The response is terminated, add EOI */
Christopher Faulet42432f32020-11-20 17:43:16 +01004549 htxbuf(&res->buf)->flags |= HTX_FL_EOM; /* no more data are expected */
Christopher Fauletef70e252020-01-28 09:26:19 +01004550 }
Christopher Fauletcf6898c2020-06-25 15:55:11 +02004551 else {
4552 /* Send ASAP informational messages. Rely on CF_EOI for final
4553 * response.
4554 */
4555 res->flags |= CF_SEND_DONTWAIT;
4556 }
Christopher Fauletef70e252020-01-28 09:26:19 +01004557
4558 data = htx->data - co_data(res);
4559 c_adv(res, data);
4560 htx->first = -1;
4561 res->total += data;
4562 return 1;
4563}
4564
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004565void http_server_error(struct stream *s, struct stream_interface *si, int err,
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004566 int finst, struct http_reply *msg)
Christopher Faulet0f226952018-10-22 09:29:56 +02004567{
Christopher Faulet72c7d8d2020-01-27 15:32:25 +01004568 http_reply_and_close(s, s->txn->status, msg);
Christopher Faulet0f226952018-10-22 09:29:56 +02004569 if (!(s->flags & SF_ERR_MASK))
4570 s->flags |= err;
4571 if (!(s->flags & SF_FINST_MASK))
4572 s->flags |= finst;
4573}
4574
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004575void http_reply_and_close(struct stream *s, short status, struct http_reply *msg)
Christopher Faulet0f226952018-10-22 09:29:56 +02004576{
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004577 if (!msg) {
4578 channel_htx_truncate(&s->res, htxbuf(&s->res.buf));
4579 goto end;
4580 }
4581
4582 if (http_reply_message(s, msg) == -1) {
4583 /* On error, return a 500 error message, but don't rewrite it if
Christopher Faulet40e6b552020-06-25 16:04:50 +02004584 * it is already an internal error. If it was already a "const"
4585 * 500 error, just fail.
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004586 */
Christopher Faulet40e6b552020-06-25 16:04:50 +02004587 if (s->txn->status == 500) {
4588 if (s->txn->flags & TX_CONST_REPLY)
4589 goto end;
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004590 s->txn->flags |= TX_CONST_REPLY;
Christopher Faulet40e6b552020-06-25 16:04:50 +02004591 }
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004592 s->txn->status = 500;
4593 s->txn->http_reply = NULL;
4594 return http_reply_and_close(s, s->txn->status, http_error_message(s));
4595 }
4596
4597end:
4598 s->res.wex = tick_add_ifset(now_ms, s->res.wto);
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004599
Christopher Fauletdb8ba102021-09-10 09:17:50 +02004600 /* At this staged, HTTP analysis is finished */
4601 s->req.analysers &= AN_REQ_FLT_END;
4602 s->req.analyse_exp = TICK_ETERNITY;
4603
4604 s->res.analysers &= AN_RES_FLT_END;
4605 s->res.analyse_exp = TICK_ETERNITY;
4606
Christopher Faulet0f226952018-10-22 09:29:56 +02004607 channel_auto_read(&s->req);
4608 channel_abort(&s->req);
4609 channel_auto_close(&s->req);
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004610 channel_htx_erase(&s->req, htxbuf(&s->req.buf));
Christopher Faulet72c7d8d2020-01-27 15:32:25 +01004611 channel_auto_read(&s->res);
4612 channel_auto_close(&s->res);
4613 channel_shutr_now(&s->res);
Christopher Faulet0f226952018-10-22 09:29:56 +02004614}
4615
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004616struct http_reply *http_error_message(struct stream *s)
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004617{
4618 const int msgnum = http_get_status_idx(s->txn->status);
4619
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004620 if (s->txn->http_reply)
4621 return s->txn->http_reply;
4622 else if (s->be->replies[msgnum])
4623 return s->be->replies[msgnum];
4624 else if (strm_fe(s)->replies[msgnum])
4625 return strm_fe(s)->replies[msgnum];
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004626 else
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004627 return &http_err_replies[msgnum];
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004628}
4629
Christopher Faulet40e6b552020-06-25 16:04:50 +02004630/* Produces an HTX message from an http reply. Depending on the http reply type,
4631 * a, errorfile, an raw file or a log-format string is used. On success, it
4632 * returns 0. If an error occurs -1 is returned. If it fails, this function only
4633 * exits. It is the caller responsibility to do the cleanup.
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004634 */
Christopher Fauletae43b6c2020-05-27 15:24:22 +02004635int http_reply_to_htx(struct stream *s, struct htx *htx, struct http_reply *reply)
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004636{
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004637 struct buffer *errmsg;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004638 struct htx_sl *sl;
4639 struct buffer *body = NULL;
4640 const char *status, *reason, *clen, *ctype;
4641 unsigned int slflags;
4642 int ret = 0;
4643
Christopher Faulete29a97e2020-05-14 14:49:25 +02004644 /*
4645 * - HTTP_REPLY_ERRFILES unexpected here. handled as no payload if so
4646 *
4647 * - HTTP_REPLY_INDIRECT: switch on another reply if defined or handled
4648 * as no payload if NULL. the TXN status code is set with the status
4649 * of the original reply.
4650 */
4651
4652 if (reply->type == HTTP_REPLY_INDIRECT) {
4653 if (reply->body.reply)
4654 reply = reply->body.reply;
4655 }
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004656 if (reply->type == HTTP_REPLY_ERRMSG && !reply->body.errmsg) {
4657 /* get default error message */
4658 if (reply == s->txn->http_reply)
4659 s->txn->http_reply = NULL;
4660 reply = http_error_message(s);
4661 if (reply->type == HTTP_REPLY_INDIRECT) {
4662 if (reply->body.reply)
4663 reply = reply->body.reply;
4664 }
4665 }
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004666
4667 if (reply->type == HTTP_REPLY_ERRMSG) {
4668 /* implicit or explicit error message*/
4669 errmsg = reply->body.errmsg;
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004670 if (errmsg && !b_is_null(errmsg)) {
Christopher Faulet20567362020-05-15 14:52:49 +02004671 if (!htx_copy_msg(htx, errmsg))
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004672 goto fail;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004673 }
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004674 }
4675 else {
4676 /* no payload, file or log-format string */
4677 if (reply->type == HTTP_REPLY_RAW) {
4678 /* file */
4679 body = &reply->body.obj;
4680 }
4681 else if (reply->type == HTTP_REPLY_LOGFMT) {
4682 /* log-format string */
4683 body = alloc_trash_chunk();
4684 if (!body)
4685 goto fail_alloc;
4686 body->data = build_logline(s, body->area, body->size, &reply->body.fmt);
4687 }
4688 /* else no payload */
4689
4690 status = ultoa(reply->status);
4691 reason = http_get_reason(reply->status);
4692 slflags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_CLEN);
4693 if (!body || !b_data(body))
4694 slflags |= HTX_SL_F_BODYLESS;
4695 sl = htx_add_stline(htx, HTX_BLK_RES_SL, slflags, ist("HTTP/1.1"), ist(status), ist(reason));
4696 if (!sl)
4697 goto fail;
4698 sl->info.res.status = reply->status;
4699
4700 clen = (body ? ultoa(b_data(body)) : "0");
4701 ctype = reply->ctype;
4702
4703 if (!LIST_ISEMPTY(&reply->hdrs)) {
4704 struct http_reply_hdr *hdr;
4705 struct buffer *value = alloc_trash_chunk();
4706
4707 if (!value)
4708 goto fail;
4709
4710 list_for_each_entry(hdr, &reply->hdrs, list) {
4711 chunk_reset(value);
4712 value->data = build_logline(s, value->area, value->size, &hdr->value);
4713 if (b_data(value) && !htx_add_header(htx, hdr->name, ist2(b_head(value), b_data(value)))) {
4714 free_trash_chunk(value);
4715 goto fail;
4716 }
4717 chunk_reset(value);
4718 }
4719 free_trash_chunk(value);
4720 }
4721
4722 if (!htx_add_header(htx, ist("content-length"), ist(clen)) ||
4723 (body && b_data(body) && ctype && !htx_add_header(htx, ist("content-type"), ist(ctype))) ||
4724 !htx_add_endof(htx, HTX_BLK_EOH) ||
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004725 (body && b_data(body) && !htx_add_data_atonce(htx, ist2(b_head(body), b_data(body)))))
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004726 goto fail;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004727
4728 htx->flags |= HTX_FL_EOM;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004729 }
4730
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004731 leave:
4732 if (reply->type == HTTP_REPLY_LOGFMT)
4733 free_trash_chunk(body);
4734 return ret;
4735
4736 fail_alloc:
4737 if (!(s->flags & SF_ERR_MASK))
4738 s->flags |= SF_ERR_RESOURCE;
Christopher Faulet97e466c2020-05-15 15:12:47 +02004739 /* fall through */
4740 fail:
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004741 ret = -1;
4742 goto leave;
Christopher Faulet97e466c2020-05-15 15:12:47 +02004743}
4744
4745/* Send an http reply to the client. On success, it returns 0. If an error
Christopher Faulet40e6b552020-06-25 16:04:50 +02004746 * occurs -1 is returned and the response channel is truncated, removing this
4747 * way the faulty reply. This function may fail when the reply is formatted
4748 * (http_reply_to_htx) or when the reply is forwarded
4749 * (http_forward_proxy_resp). On the last case, it is because a
4750 * http-after-response rule fails.
Christopher Faulet97e466c2020-05-15 15:12:47 +02004751 */
4752int http_reply_message(struct stream *s, struct http_reply *reply)
4753{
4754 struct channel *res = &s->res;
4755 struct htx *htx = htx_from_buf(&res->buf);
4756
4757 if (s->txn->status == -1)
4758 s->txn->status = reply->status;
4759 channel_htx_truncate(res, htx);
4760
4761 if (http_reply_to_htx(s, htx, reply) == -1)
4762 goto fail;
4763
4764 htx_to_buf(htx, &s->res.buf);
4765 if (!http_forward_proxy_resp(s, 1))
4766 goto fail;
4767 return 0;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004768
4769 fail:
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004770 channel_htx_truncate(res, htx);
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004771 if (!(s->flags & SF_ERR_MASK))
4772 s->flags |= SF_ERR_PRXCOND;
Christopher Faulet97e466c2020-05-15 15:12:47 +02004773 return -1;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004774}
4775
Christopher Faulet304cc402019-07-15 15:46:28 +02004776/* Return the error message corresponding to si->err_type. It is assumed
4777 * that the server side is closed. Note that err_type is actually a
4778 * bitmask, where almost only aborts may be cumulated with other
4779 * values. We consider that aborted operations are more important
4780 * than timeouts or errors due to the fact that nobody else in the
4781 * logs might explain incomplete retries. All others should avoid
4782 * being cumulated. It should normally not be possible to have multiple
4783 * aborts at once, but just in case, the first one in sequence is reported.
4784 * Note that connection errors appearing on the second request of a keep-alive
4785 * connection are not reported since this allows the client to retry.
4786 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004787void http_return_srv_error(struct stream *s, struct stream_interface *si)
Christopher Faulet304cc402019-07-15 15:46:28 +02004788{
4789 int err_type = si->err_type;
4790
4791 /* set s->txn->status for http_error_message(s) */
4792 s->txn->status = 503;
4793
4794 if (err_type & SI_ET_QUEUE_ABRT)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004795 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_Q,
4796 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004797 else if (err_type & SI_ET_CONN_ABRT)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004798 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_C,
4799 (s->txn->flags & TX_NOT_FIRST) ? NULL :
4800 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004801 else if (err_type & SI_ET_QUEUE_TO)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004802 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_Q,
4803 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004804 else if (err_type & SI_ET_QUEUE_ERR)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004805 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_Q,
4806 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004807 else if (err_type & SI_ET_CONN_TO)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004808 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_C,
4809 (s->txn->flags & TX_NOT_FIRST) ? NULL :
4810 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004811 else if (err_type & SI_ET_CONN_ERR)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004812 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_C,
4813 (s->flags & SF_SRV_REUSED) ? NULL :
4814 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004815 else if (err_type & SI_ET_CONN_RES)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004816 http_server_error(s, si, SF_ERR_RESOURCE, SF_FINST_C,
4817 (s->txn->flags & TX_NOT_FIRST) ? NULL :
4818 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004819 else { /* SI_ET_CONN_OTHER and others */
4820 s->txn->status = 500;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004821 http_server_error(s, si, SF_ERR_INTERNAL, SF_FINST_C,
4822 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004823 }
4824}
4825
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004826
Christopher Faulet4a28a532019-03-01 11:19:40 +01004827/* Handle Expect: 100-continue for HTTP/1.1 messages if necessary. It returns 0
4828 * on success and -1 on error.
4829 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004830static int http_handle_expect_hdr(struct stream *s, struct htx *htx, struct http_msg *msg)
Christopher Faulet4a28a532019-03-01 11:19:40 +01004831{
4832 /* If we have HTTP/1.1 message with a body and Expect: 100-continue,
4833 * then we must send an HTTP/1.1 100 Continue intermediate response.
4834 */
4835 if (msg->msg_state == HTTP_MSG_BODY && (msg->flags & HTTP_MSGF_VER_11) &&
4836 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
4837 struct ist hdr = { .ptr = "Expect", .len = 6 };
4838 struct http_hdr_ctx ctx;
4839
4840 ctx.blk = NULL;
4841 /* Expect is allowed in 1.1, look for it */
4842 if (http_find_header(htx, hdr, &ctx, 0) &&
4843 unlikely(isteqi(ctx.value, ist2("100-continue", 12)))) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004844 if (http_reply_100_continue(s) == -1)
Christopher Faulet4a28a532019-03-01 11:19:40 +01004845 return -1;
4846 http_remove_header(htx, &ctx);
4847 }
4848 }
4849 return 0;
4850}
4851
Christopher Faulet23a3c792018-11-28 10:01:23 +01004852/* Send a 100-Continue response to the client. It returns 0 on success and -1
4853 * on error. The response channel is updated accordingly.
4854 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004855static int http_reply_100_continue(struct stream *s)
Christopher Faulet23a3c792018-11-28 10:01:23 +01004856{
4857 struct channel *res = &s->res;
4858 struct htx *htx = htx_from_buf(&res->buf);
4859 struct htx_sl *sl;
4860 unsigned int flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|
4861 HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
Christopher Faulet23a3c792018-11-28 10:01:23 +01004862
4863 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
4864 ist("HTTP/1.1"), ist("100"), ist("Continue"));
4865 if (!sl)
4866 goto fail;
4867 sl->info.res.status = 100;
4868
Christopher Faulet1d5ec092019-06-26 14:23:54 +02004869 if (!htx_add_endof(htx, HTX_BLK_EOH))
Christopher Faulet23a3c792018-11-28 10:01:23 +01004870 goto fail;
4871
Christopher Fauleta72a7e42020-01-28 09:28:11 +01004872 if (!http_forward_proxy_resp(s, 0))
4873 goto fail;
Christopher Faulet23a3c792018-11-28 10:01:23 +01004874 return 0;
4875
4876 fail:
4877 /* If an error occurred, remove the incomplete HTTP response from the
4878 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004879 channel_htx_truncate(res, htx);
Christopher Faulet23a3c792018-11-28 10:01:23 +01004880 return -1;
4881}
4882
Christopher Faulet12c51e22018-11-28 15:59:42 +01004883
Christopher Faulet0f226952018-10-22 09:29:56 +02004884/*
4885 * Capture headers from message <htx> according to header list <cap_hdr>, and
4886 * fill the <cap> pointers appropriately.
4887 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004888static void http_capture_headers(struct htx *htx, char **cap, struct cap_hdr *cap_hdr)
Christopher Faulet0f226952018-10-22 09:29:56 +02004889{
4890 struct cap_hdr *h;
4891 int32_t pos;
4892
Christopher Fauleta3f15502019-05-13 15:27:23 +02004893 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet0f226952018-10-22 09:29:56 +02004894 struct htx_blk *blk = htx_get_blk(htx, pos);
4895 enum htx_blk_type type = htx_get_blk_type(blk);
4896 struct ist n, v;
4897
4898 if (type == HTX_BLK_EOH)
4899 break;
4900 if (type != HTX_BLK_HDR)
4901 continue;
4902
4903 n = htx_get_blk_name(htx, blk);
4904
4905 for (h = cap_hdr; h; h = h->next) {
4906 if (h->namelen && (h->namelen == n.len) &&
4907 (strncasecmp(n.ptr, h->name, h->namelen) == 0)) {
4908 if (cap[h->index] == NULL)
4909 cap[h->index] =
4910 pool_alloc(h->pool);
4911
4912 if (cap[h->index] == NULL) {
4913 ha_alert("HTTP capture : out of memory.\n");
4914 break;
4915 }
4916
4917 v = htx_get_blk_value(htx, blk);
4918 if (v.len > h->len)
4919 v.len = h->len;
4920
4921 memcpy(cap[h->index], v.ptr, v.len);
4922 cap[h->index][v.len]=0;
4923 }
4924 }
4925 }
4926}
4927
Christopher Faulet0b6bdc52018-10-24 11:05:36 +02004928/* Delete a value in a header between delimiters <from> and <next>. The header
4929 * itself is delimited by <start> and <end> pointers. The number of characters
4930 * displaced is returned, and the pointer to the first delimiter is updated if
4931 * required. The function tries as much as possible to respect the following
4932 * principles :
4933 * - replace <from> delimiter by the <next> one unless <from> points to <start>,
4934 * in which case <next> is simply removed
4935 * - set exactly one space character after the new first delimiter, unless there
4936 * are not enough characters in the block being moved to do so.
4937 * - remove unneeded spaces before the previous delimiter and after the new
4938 * one.
4939 *
4940 * It is the caller's responsibility to ensure that :
4941 * - <from> points to a valid delimiter or <start> ;
4942 * - <next> points to a valid delimiter or <end> ;
4943 * - there are non-space chars before <from>.
4944 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004945static int http_del_hdr_value(char *start, char *end, char **from, char *next)
Christopher Faulet0b6bdc52018-10-24 11:05:36 +02004946{
4947 char *prev = *from;
4948
4949 if (prev == start) {
4950 /* We're removing the first value. eat the semicolon, if <next>
4951 * is lower than <end> */
4952 if (next < end)
4953 next++;
4954
4955 while (next < end && HTTP_IS_SPHT(*next))
4956 next++;
4957 }
4958 else {
4959 /* Remove useless spaces before the old delimiter. */
4960 while (HTTP_IS_SPHT(*(prev-1)))
4961 prev--;
4962 *from = prev;
4963
4964 /* copy the delimiter and if possible a space if we're
4965 * not at the end of the line.
4966 */
4967 if (next < end) {
4968 *prev++ = *next++;
4969 if (prev + 1 < next)
4970 *prev++ = ' ';
4971 while (next < end && HTTP_IS_SPHT(*next))
4972 next++;
4973 }
4974 }
4975 memmove(prev, next, end - next);
4976 return (prev - next);
4977}
4978
Christopher Faulet0f226952018-10-22 09:29:56 +02004979
4980/* Formats the start line of the request (without CRLF) and puts it in <str> and
Joseph Herlantc42c0e92018-11-25 10:43:27 -08004981 * return the written length. The line can be truncated if it exceeds <len>.
Christopher Faulet0f226952018-10-22 09:29:56 +02004982 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004983static size_t http_fmt_req_line(const struct htx_sl *sl, char *str, size_t len)
Christopher Faulet0f226952018-10-22 09:29:56 +02004984{
4985 struct ist dst = ist2(str, 0);
4986
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004987 if (istcat(&dst, htx_sl_req_meth(sl), len) == -1)
Christopher Faulet0f226952018-10-22 09:29:56 +02004988 goto end;
4989 if (dst.len + 1 > len)
4990 goto end;
4991 dst.ptr[dst.len++] = ' ';
4992
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004993 if (istcat(&dst, htx_sl_req_uri(sl), len) == -1)
Christopher Faulet0f226952018-10-22 09:29:56 +02004994 goto end;
4995 if (dst.len + 1 > len)
4996 goto end;
4997 dst.ptr[dst.len++] = ' ';
4998
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004999 istcat(&dst, htx_sl_req_vsn(sl), len);
Christopher Faulet0f226952018-10-22 09:29:56 +02005000 end:
5001 return dst.len;
5002}
5003
5004/*
5005 * Print a debug line with a start line.
5006 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005007static void http_debug_stline(const char *dir, struct stream *s, const struct htx_sl *sl)
Christopher Faulet0f226952018-10-22 09:29:56 +02005008{
5009 struct session *sess = strm_sess(s);
5010 int max;
5011
5012 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
5013 dir,
Willy Tarreau66182592021-12-06 07:01:02 +00005014 objt_conn(sess->origin) ? (unsigned short)__objt_conn(sess->origin)->handle.fd : -1,
5015 objt_cs(s->si[1].end) ? (unsigned short)__objt_cs(s->si[1].end)->conn->handle.fd : -1);
Christopher Faulet0f226952018-10-22 09:29:56 +02005016
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005017 max = HTX_SL_P1_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02005018 UBOUND(max, trash.size - trash.data - 3);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005019 chunk_memcat(&trash, HTX_SL_P1_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_P2_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02005023 UBOUND(max, trash.size - trash.data - 2);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005024 chunk_memcat(&trash, HTX_SL_P2_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02005025 trash.area[trash.data++] = ' ';
5026
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005027 max = HTX_SL_P3_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02005028 UBOUND(max, trash.size - trash.data - 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005029 chunk_memcat(&trash, HTX_SL_P3_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02005030 trash.area[trash.data++] = '\n';
5031
Willy Tarreau2e8ab6b2020-03-14 11:03:20 +01005032 DISGUISE(write(1, trash.area, trash.data));
Christopher Faulet0f226952018-10-22 09:29:56 +02005033}
5034
5035/*
5036 * Print a debug line with a header.
5037 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005038static 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 +02005039{
5040 struct session *sess = strm_sess(s);
5041 int max;
5042
5043 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
5044 dir,
Willy Tarreau66182592021-12-06 07:01:02 +00005045 objt_conn(sess->origin) ? (unsigned short)__objt_conn(sess->origin)->handle.fd : -1,
5046 objt_cs(s->si[1].end) ? (unsigned short)__objt_cs(s->si[1].end)->conn->handle.fd : -1);
Christopher Faulet0f226952018-10-22 09:29:56 +02005047
5048 max = n.len;
5049 UBOUND(max, trash.size - trash.data - 3);
5050 chunk_memcat(&trash, n.ptr, max);
5051 trash.area[trash.data++] = ':';
5052 trash.area[trash.data++] = ' ';
5053
5054 max = v.len;
5055 UBOUND(max, trash.size - trash.data - 1);
5056 chunk_memcat(&trash, v.ptr, max);
5057 trash.area[trash.data++] = '\n';
5058
Willy Tarreau2e8ab6b2020-03-14 11:03:20 +01005059 DISGUISE(write(1, trash.area, trash.data));
Christopher Faulet0f226952018-10-22 09:29:56 +02005060}
5061
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005062/* Allocate a new HTTP transaction for stream <s> unless there is one already.
5063 * In case of allocation failure, everything allocated is freed and NULL is
5064 * returned. Otherwise the new transaction is assigned to the stream and
5065 * returned.
5066 */
5067struct http_txn *http_alloc_txn(struct stream *s)
5068{
5069 struct http_txn *txn = s->txn;
5070
5071 if (txn)
5072 return txn;
5073
5074 txn = pool_alloc(pool_head_http_txn);
5075 if (!txn)
5076 return txn;
5077
5078 s->txn = txn;
5079 return txn;
5080}
5081
5082void http_txn_reset_req(struct http_txn *txn)
5083{
Christopher Faulet1aea50e2020-01-17 16:03:53 +01005084 txn->req.flags = 0;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005085 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
5086}
5087
5088void http_txn_reset_res(struct http_txn *txn)
5089{
Christopher Faulet1aea50e2020-01-17 16:03:53 +01005090 txn->rsp.flags = 0;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005091 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
5092}
5093
5094/*
Christopher Faulet75f619a2021-03-08 19:12:58 +01005095 * Create and initialize a new HTTP transaction for stream <s>. This should be
5096 * used before processing any new request. It returns the transaction or NLULL
5097 * on error.
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005098 */
Christopher Faulet75f619a2021-03-08 19:12:58 +01005099struct http_txn *http_create_txn(struct stream *s)
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005100{
Christopher Faulet75f619a2021-03-08 19:12:58 +01005101 struct http_txn *txn;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005102 struct conn_stream *cs = objt_cs(s->si[0].end);
5103
Christopher Faulet75f619a2021-03-08 19:12:58 +01005104 txn = pool_alloc(pool_head_http_txn);
5105 if (!txn)
5106 return NULL;
5107 s->txn = txn;
5108
Christopher Fauletda831fa2020-10-06 17:58:43 +02005109 txn->flags = ((cs && cs->flags & CS_FL_NOT_FIRST) ? TX_NOT_FIRST : 0);
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005110 txn->status = -1;
Christopher Faulet5cb513a2020-05-13 17:56:56 +02005111 txn->http_reply = NULL;
Willy Tarreau8b507582020-02-25 09:35:07 +01005112 write_u32(txn->cache_hash, 0);
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005113
5114 txn->cookie_first_date = 0;
5115 txn->cookie_last_date = 0;
5116
5117 txn->srv_cookie = NULL;
5118 txn->cli_cookie = NULL;
5119 txn->uri = NULL;
5120
5121 http_txn_reset_req(txn);
5122 http_txn_reset_res(txn);
5123
5124 txn->req.chn = &s->req;
5125 txn->rsp.chn = &s->res;
5126
5127 txn->auth.method = HTTP_AUTH_UNKNOWN;
5128
5129 vars_init(&s->vars_txn, SCOPE_TXN);
5130 vars_init(&s->vars_reqres, SCOPE_REQ);
Christopher Faulet75f619a2021-03-08 19:12:58 +01005131
5132 return txn;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005133}
5134
5135/* to be used at the end of a transaction */
Christopher Faulet75f619a2021-03-08 19:12:58 +01005136void http_destroy_txn(struct stream *s)
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005137{
5138 struct http_txn *txn = s->txn;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005139
5140 /* these ones will have been dynamically allocated */
5141 pool_free(pool_head_requri, txn->uri);
5142 pool_free(pool_head_capture, txn->cli_cookie);
5143 pool_free(pool_head_capture, txn->srv_cookie);
Tim Duesterhusa17e6622020-03-05 20:19:02 +01005144 pool_free(pool_head_uniqueid, s->unique_id.ptr);
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005145
Tim Duesterhusa17e6622020-03-05 20:19:02 +01005146 s->unique_id = IST_NULL;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005147 txn->uri = NULL;
5148 txn->srv_cookie = NULL;
5149 txn->cli_cookie = NULL;
5150
Christopher Faulet59399252019-11-07 14:27:52 +01005151 if (!LIST_ISEMPTY(&s->vars_txn.head))
5152 vars_prune(&s->vars_txn, s->sess, s);
5153 if (!LIST_ISEMPTY(&s->vars_reqres.head))
5154 vars_prune(&s->vars_reqres, s->sess, s);
Christopher Faulet75f619a2021-03-08 19:12:58 +01005155
5156 pool_free(pool_head_http_txn, txn);
5157 s->txn = NULL;
Christopher Faulet59399252019-11-07 14:27:52 +01005158}
5159
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005160
5161DECLARE_POOL(pool_head_http_txn, "http_txn", sizeof(struct http_txn));
Christopher Faulet0f226952018-10-22 09:29:56 +02005162
Christopher Fauletf4eb75d2018-10-11 15:55:07 +02005163__attribute__((constructor))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005164static void __http_protocol_init(void)
Christopher Fauletf4eb75d2018-10-11 15:55:07 +02005165{
5166}
5167
5168
5169/*
5170 * Local variables:
5171 * c-indent-level: 8
5172 * c-basic-offset: 8
5173 * End:
5174 */