blob: 0a1af6d887a9d7aa31a7abbc7d3be7098086fc6a [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
334 req->analysers &= AN_REQ_FLT_END;
335 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100336 DBG_TRACE_DEVEL("leaving on error",
337 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200338 return 0;
339}
340
341
342/* This stream analyser runs all HTTP request processing which is common to
343 * frontends and backends, which means blocking ACLs, filters, connection-close,
344 * reqadd, stats and redirects. This is performed for the designated proxy.
345 * It returns 1 if the processing can continue on next analysers, or zero if it
346 * either needs more data or wants to immediately abort the request (eg: deny,
347 * error, ...).
348 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200349int http_process_req_common(struct stream *s, struct channel *req, int an_bit, struct proxy *px)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200350{
351 struct session *sess = s->sess;
352 struct http_txn *txn = s->txn;
353 struct http_msg *msg = &txn->req;
Christopher Fauletff2759f2018-10-24 11:13:16 +0200354 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200355 struct redirect_rule *rule;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200356 enum rule_result verdict;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200357 struct connection *conn = objt_conn(sess->origin);
358
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100359 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200360
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100361 htx = htxbuf(&req->buf);
Christopher Fauletff2759f2018-10-24 11:13:16 +0200362
Christopher Faulet1907ccc2019-04-29 13:12:02 +0200363 /* just in case we have some per-backend tracking. Only called the first
364 * execution of the analyser. */
365 if (!s->current_rule || s->current_rule_list != &px->http_req_rules)
366 stream_inc_be_http_req_ctr(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200367
368 /* evaluate http-request rules */
369 if (!LIST_ISEMPTY(&px->http_req_rules)) {
Christopher Fauletb58f62b2020-01-13 16:40:13 +0100370 verdict = http_req_get_intercept_rule(px, &px->http_req_rules, s);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200371
372 switch (verdict) {
373 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
374 goto return_prx_yield;
375
376 case HTTP_RULE_RES_CONT:
377 case HTTP_RULE_RES_STOP: /* nothing to do */
378 break;
379
380 case HTTP_RULE_RES_DENY: /* deny or tarpit */
381 if (txn->flags & TX_CLTARPIT)
382 goto tarpit;
383 goto deny;
384
385 case HTTP_RULE_RES_ABRT: /* abort request, response already sent. Eg: auth */
386 goto return_prx_cond;
387
388 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
389 goto done;
390
391 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
392 goto return_bad_req;
Christopher Faulet3a26bee2019-12-16 12:47:40 +0100393
394 case HTTP_RULE_RES_ERROR: /* failed with a bad request */
395 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200396 }
397 }
398
399 if (conn && (conn->flags & CO_FL_EARLY_DATA) &&
Olivier Houchard220a26c2020-01-23 14:57:36 +0100400 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_SSL_WAIT_HS))) {
Christopher Fauletff2759f2018-10-24 11:13:16 +0200401 struct http_hdr_ctx ctx;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200402
Christopher Fauletff2759f2018-10-24 11:13:16 +0200403 ctx.blk = NULL;
404 if (!http_find_header(htx, ist("Early-Data"), &ctx, 0)) {
405 if (unlikely(!http_add_header(htx, ist("Early-Data"), ist("1"))))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100406 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200407 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200408 }
409
410 /* OK at this stage, we know that the request was accepted according to
411 * the http-request rules, we can check for the stats. Note that the
412 * URI is detected *before* the req* rules in order not to be affected
413 * by a possible reqrep, while they are processed *after* so that a
414 * reqdeny can still block them. This clearly needs to change in 1.6!
415 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200416 if (!s->target && http_stats_check_uri(s, txn, px)) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200417 s->target = &http_stats_applet.obj_type;
Willy Tarreau14bfe9a2018-12-19 15:19:27 +0100418 if (unlikely(!si_register_handler(&s->si[1], objt_applet(s->target)))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200419 s->logs.tv_request = now;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200420 if (!(s->flags & SF_ERR_MASK))
421 s->flags |= SF_ERR_RESOURCE;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100422 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200423 }
424
425 /* parse the whole stats request and extract the relevant information */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200426 http_handle_stats(s, req);
Christopher Fauletb58f62b2020-01-13 16:40:13 +0100427 verdict = http_req_get_intercept_rule(px, &px->uri_auth->http_req_rules, s);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200428 /* not all actions implemented: deny, allow, auth */
429
430 if (verdict == HTTP_RULE_RES_DENY) /* stats http-request deny */
431 goto deny;
432
433 if (verdict == HTTP_RULE_RES_ABRT) /* stats auth / stats http-request auth */
434 goto return_prx_cond;
Christopher Faulet3a26bee2019-12-16 12:47:40 +0100435
436 if (verdict == HTTP_RULE_RES_BADREQ) /* failed with a bad request */
437 goto return_bad_req;
438
439 if (verdict == HTTP_RULE_RES_ERROR) /* failed with a bad request */
440 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200441 }
442
Christopher Faulet2571bc62019-03-01 11:44:26 +0100443 /* Proceed with the applets now. */
444 if (unlikely(objt_applet(s->target))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200445 if (sess->fe == s->be) /* report it if the request was intercepted by the frontend */
Willy Tarreau4781b152021-04-06 13:53:36 +0200446 _HA_ATOMIC_INC(&sess->fe->fe_counters.intercepted_req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200447
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200448 if (http_handle_expect_hdr(s, htx, msg) == -1)
Christopher Fauletb8a53712019-12-16 11:29:38 +0100449 goto return_int_err;
Christopher Fauletbcf242a2019-03-01 11:36:26 +0100450
Christopher Faulete0768eb2018-10-03 16:38:02 +0200451 if (!(s->flags & SF_ERR_MASK)) // this is not really an error but it is
452 s->flags |= SF_ERR_LOCAL; // to mark that it comes from the proxy
453 if (!(s->flags & SF_FINST_MASK))
454 s->flags |= SF_FINST_R;
455
Christopher Fauletc2ac5e42021-03-08 18:20:09 +0100456 if (HAS_FILTERS(s))
457 req->analysers |= AN_REQ_FLT_HTTP_HDRS;
458
Christopher Faulete0768eb2018-10-03 16:38:02 +0200459 /* enable the minimally required analyzers to handle keep-alive and compression on the HTTP response */
460 req->analysers &= (AN_REQ_HTTP_BODY | AN_REQ_FLT_HTTP_HDRS | AN_REQ_FLT_END);
461 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
462 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Christopher Fauletbcf242a2019-03-01 11:36:26 +0100463
464 req->flags |= CF_SEND_DONTWAIT;
465 s->flags |= SF_ASSIGNED;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200466 goto done;
467 }
468
469 /* check whether we have some ACLs set to redirect this request */
470 list_for_each_entry(rule, &px->redirect_rules, list) {
471 if (rule->cond) {
472 int ret;
473
474 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
475 ret = acl_pass(ret);
476 if (rule->cond->pol == ACL_COND_UNLESS)
477 ret = !ret;
478 if (!ret)
479 continue;
480 }
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200481 if (!http_apply_redirect_rule(rule, s, txn))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100482 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200483 goto done;
484 }
485
486 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
487 * If this happens, then the data will not come immediately, so we must
488 * send all what we have without waiting. Note that due to the small gain
489 * in waiting for the body of the request, it's easier to simply put the
490 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
491 * itself once used.
492 */
493 req->flags |= CF_SEND_DONTWAIT;
494
495 done: /* done with this analyser, continue with next ones that the calling
496 * points will have set, if any.
497 */
498 req->analyse_exp = TICK_ETERNITY;
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +0500499 done_without_exp: /* done with this analyser, but don't reset the analyse_exp. */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200500 req->analysers &= ~an_bit;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100501 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200502 return 1;
503
504 tarpit:
505 /* Allow cookie logging
506 */
507 if (s->be->cookie_name || sess->fe->capture_name)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200508 http_manage_client_side_cookies(s, req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200509
510 /* When a connection is tarpitted, we use the tarpit timeout,
511 * which may be the same as the connect timeout if unspecified.
512 * If unset, then set it to zero because we really want it to
513 * eventually expire. We build the tarpit as an analyser.
514 */
Christopher Faulet202c6ce2019-01-07 14:57:35 +0100515 channel_htx_erase(&s->req, htx);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200516
517 /* wipe the request out so that we can drop the connection early
518 * if the client closes first.
519 */
520 channel_dont_connect(req);
521
Christopher Faulete0768eb2018-10-03 16:38:02 +0200522 req->analysers &= AN_REQ_FLT_END; /* remove switching rules etc... */
523 req->analysers |= AN_REQ_HTTP_TARPIT;
524 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
525 if (!req->analyse_exp)
526 req->analyse_exp = tick_add(now_ms, 0);
527 stream_inc_http_err_ctr(s);
Willy Tarreau4781b152021-04-06 13:53:36 +0200528 _HA_ATOMIC_INC(&sess->fe->fe_counters.denied_req);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100529 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau4781b152021-04-06 13:53:36 +0200530 _HA_ATOMIC_INC(&s->be->be_counters.denied_req);
William Lallemand36119de2021-03-08 15:26:48 +0100531 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200532 _HA_ATOMIC_INC(&sess->listener->counters->denied_req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200533 goto done_without_exp;
534
535 deny: /* this request was blocked (denied) */
536
537 /* Allow cookie logging
538 */
539 if (s->be->cookie_name || sess->fe->capture_name)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200540 http_manage_client_side_cookies(s, req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200541
Christopher Faulete0768eb2018-10-03 16:38:02 +0200542 s->logs.tv_request = now;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200543 stream_inc_http_err_ctr(s);
Willy Tarreau4781b152021-04-06 13:53:36 +0200544 _HA_ATOMIC_INC(&sess->fe->fe_counters.denied_req);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100545 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau4781b152021-04-06 13:53:36 +0200546 _HA_ATOMIC_INC(&s->be->be_counters.denied_req);
William Lallemand36119de2021-03-08 15:26:48 +0100547 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200548 _HA_ATOMIC_INC(&sess->listener->counters->denied_req);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100549 goto return_prx_err;
550
551 return_int_err:
552 txn->status = 500;
553 if (!(s->flags & SF_ERR_MASK))
554 s->flags |= SF_ERR_INTERNAL;
Willy Tarreau4781b152021-04-06 13:53:36 +0200555 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100556 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau4781b152021-04-06 13:53:36 +0200557 _HA_ATOMIC_INC(&s->be->be_counters.internal_errors);
William Lallemand36119de2021-03-08 15:26:48 +0100558 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200559 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100560 goto return_prx_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200561
562 return_bad_req:
Christopher Faulete0768eb2018-10-03 16:38:02 +0200563 txn->status = 400;
Willy Tarreau4781b152021-04-06 13:53:36 +0200564 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req);
William Lallemand36119de2021-03-08 15:26:48 +0100565 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200566 _HA_ATOMIC_INC(&sess->listener->counters->failed_req);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100567 /* fall through */
568
569 return_prx_err:
570 http_reply_and_close(s, txn->status, http_error_message(s));
571 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200572
573 return_prx_cond:
574 if (!(s->flags & SF_ERR_MASK))
575 s->flags |= SF_ERR_PRXCOND;
576 if (!(s->flags & SF_FINST_MASK))
577 s->flags |= SF_FINST_R;
578
579 req->analysers &= AN_REQ_FLT_END;
580 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100581 DBG_TRACE_DEVEL("leaving on error",
582 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200583 return 0;
584
585 return_prx_yield:
586 channel_dont_connect(req);
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100587 DBG_TRACE_DEVEL("waiting for more data",
588 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200589 return 0;
590}
591
592/* This function performs all the processing enabled for the current request.
593 * It returns 1 if the processing can continue on next analysers, or zero if it
594 * needs more data, encounters an error, or wants to immediately abort the
595 * request. It relies on buffers flags, and updates s->req.analysers.
596 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200597int http_process_request(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200598{
599 struct session *sess = s->sess;
600 struct http_txn *txn = s->txn;
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200601 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200602 struct connection *cli_conn = objt_conn(strm_sess(s)->origin);
603
Christopher Faulet8bebd2f2020-10-06 17:54:56 +0200604 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200605
606 /*
607 * Right now, we know that we have processed the entire headers
608 * and that unwanted requests have been filtered out. We can do
609 * whatever we want with the remaining request. Also, now we
610 * may have separate values for ->fe, ->be.
611 */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100612 htx = htxbuf(&req->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200613
614 /*
615 * If HTTP PROXY is set we simply get remote server address parsing
Willy Tarreau1c8d32b2019-07-18 15:47:45 +0200616 * incoming request.
Christopher Faulete0768eb2018-10-03 16:38:02 +0200617 */
618 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SF_ADDR_SET)) {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100619 struct htx_sl *sl;
620 struct ist uri, path;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200621
Willy Tarreau9b7587a2020-10-15 07:32:10 +0200622 if (!sockaddr_alloc(&s->target_addr, NULL, 0)) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200623 if (!(s->flags & SF_ERR_MASK))
624 s->flags |= SF_ERR_RESOURCE;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100625 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200626 }
Christopher Faulet297fbb42019-05-13 14:41:27 +0200627 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100628 uri = htx_sl_req_uri(sl);
629 path = http_get_path(uri);
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200630
Willy Tarreau1c8d32b2019-07-18 15:47:45 +0200631 if (url2sa(uri.ptr, uri.len - path.len, s->target_addr, NULL) == -1)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200632 goto return_bad_req;
633
Willy Tarreau1c8d32b2019-07-18 15:47:45 +0200634 s->target = &s->be->obj_type;
635 s->flags |= SF_ADDR_SET | SF_ASSIGNED;
636
Christopher Faulete0768eb2018-10-03 16:38:02 +0200637 /* if the path was found, we have to remove everything between
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200638 * uri.ptr and path.ptr (excluded). If it was not found, we need
639 * to replace from all the uri by a single "/".
640 *
Ilya Shipitsin46a030c2020-07-05 16:36:08 +0500641 * Instead of rewriting the whole start line, we just update
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100642 * the star-line URI. Some space will be lost but it should be
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200643 * insignificant.
Christopher Faulete0768eb2018-10-03 16:38:02 +0200644 */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100645 istcpy(&uri, (path.len ? path : ist("/")), uri.len);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200646 }
647
648 /*
649 * 7: Now we can work with the cookies.
650 * Note that doing so might move headers in the request, but
651 * the fields will stay coherent and the URI will not move.
652 * This should only be performed in the backend.
653 */
654 if (s->be->cookie_name || sess->fe->capture_name)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200655 http_manage_client_side_cookies(s, req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200656
Tim Duesterhus2825b4b2020-02-28 15:13:34 +0100657 /* 8: Generate unique ID if a "unique-id-format" is defined.
658 *
659 * A unique ID is generated even when it is not sent to ensure that the ID can make use of
660 * fetches only available in the HTTP request processing stage.
661 */
662 if (!LIST_ISEMPTY(&sess->fe->format_unique_id)) {
Tim Duesterhusa17e6622020-03-05 20:19:02 +0100663 struct ist unique_id = stream_generate_unique_id(s, &sess->fe->format_unique_id);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200664
Tim Duesterhusa17e6622020-03-05 20:19:02 +0100665 if (!isttest(unique_id)) {
Christopher Fauletb8a53712019-12-16 11:29:38 +0100666 if (!(s->flags & SF_ERR_MASK))
667 s->flags |= SF_ERR_RESOURCE;
668 goto return_int_err;
669 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200670
Tim Duesterhus2825b4b2020-02-28 15:13:34 +0100671 /* send unique ID if a "unique-id-header" is defined */
Tim Duesterhus0643b0e2020-03-05 17:56:35 +0100672 if (isttest(sess->fe->header_unique_id) &&
Tim Duesterhusa17e6622020-03-05 20:19:02 +0100673 unlikely(!http_add_header(htx, sess->fe->header_unique_id, s->unique_id)))
Tim Duesterhus2825b4b2020-02-28 15:13:34 +0100674 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200675 }
676
677 /*
678 * 9: add X-Forwarded-For if either the frontend or the backend
679 * asks for it.
680 */
681 if ((sess->fe->options | s->be->options) & PR_O_FWDFOR) {
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200682 struct http_hdr_ctx ctx = { .blk = NULL };
683 struct ist hdr = ist2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : sess->fe->fwdfor_hdr_name,
684 s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : sess->fe->fwdfor_hdr_len);
685
Christopher Faulete0768eb2018-10-03 16:38:02 +0200686 if (!((sess->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200687 http_find_header(htx, hdr, &ctx, 0)) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200688 /* The header is set to be added only if none is present
689 * and we found it, so don't do anything.
690 */
691 }
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200692 else if (cli_conn && conn_get_src(cli_conn) && cli_conn->src->ss_family == AF_INET) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200693 /* Add an X-Forwarded-For header unless the source IP is
694 * in the 'except' network range.
695 */
Christopher Faulet5d1def62021-02-26 09:19:15 +0100696 if (ipcmp2net(cli_conn->src, &sess->fe->except_xff_net) &&
697 ipcmp2net(cli_conn->src, &s->be->except_xff_net)) {
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200698 unsigned char *pn = (unsigned char *)&((struct sockaddr_in *)cli_conn->src)->sin_addr;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200699
700 /* Note: we rely on the backend to get the header name to be used for
701 * x-forwarded-for, because the header is really meant for the backends.
702 * However, if the backend did not specify any option, we have to rely
703 * on the frontend's header name.
704 */
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200705 chunk_printf(&trash, "%d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
706 if (unlikely(!http_add_header(htx, hdr, ist2(trash.area, trash.data))))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100707 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200708 }
709 }
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200710 else if (cli_conn && conn_get_src(cli_conn) && cli_conn->src->ss_family == AF_INET6) {
Christopher Faulet5d1def62021-02-26 09:19:15 +0100711 /* Add an X-Forwarded-For header unless the source IP is
712 * in the 'except' network range.
Christopher Faulete0768eb2018-10-03 16:38:02 +0200713 */
Christopher Faulet5d1def62021-02-26 09:19:15 +0100714 if (ipcmp2net(cli_conn->src, &sess->fe->except_xff_net) &&
715 ipcmp2net(cli_conn->src, &s->be->except_xff_net)) {
716 char pn[INET6_ADDRSTRLEN];
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200717
Christopher Faulet5d1def62021-02-26 09:19:15 +0100718 inet_ntop(AF_INET6,
719 (const void *)&((struct sockaddr_in6 *)(cli_conn->src))->sin6_addr,
720 pn, sizeof(pn));
Christopher Faulete0768eb2018-10-03 16:38:02 +0200721
Christopher Faulet5d1def62021-02-26 09:19:15 +0100722 /* Note: we rely on the backend to get the header name to be used for
723 * x-forwarded-for, because the header is really meant for the backends.
724 * However, if the backend did not specify any option, we have to rely
725 * on the frontend's header name.
726 */
727 chunk_printf(&trash, "%s", pn);
728 if (unlikely(!http_add_header(htx, hdr, ist2(trash.area, trash.data))))
729 goto return_int_err;
730 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200731 }
732 }
733
734 /*
735 * 10: add X-Original-To if either the frontend or the backend
736 * asks for it.
737 */
738 if ((sess->fe->options | s->be->options) & PR_O_ORGTO) {
Christopher Faulet5d1def62021-02-26 09:19:15 +0100739 struct ist hdr = ist2(s->be->orgto_hdr_len ? s->be->orgto_hdr_name : sess->fe->orgto_hdr_name,
740 s->be->orgto_hdr_len ? s->be->orgto_hdr_len : sess->fe->orgto_hdr_len);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200741
Christopher Fauletcccded92021-02-26 12:45:56 +0100742 if (cli_conn && conn_get_dst(cli_conn) && cli_conn->dst->ss_family == AF_INET) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200743 /* Add an X-Original-To header unless the destination IP is
744 * in the 'except' network range.
745 */
Christopher Faulet5d1def62021-02-26 09:19:15 +0100746 if (ipcmp2net(cli_conn->dst, &sess->fe->except_xot_net) &&
747 ipcmp2net(cli_conn->dst, &s->be->except_xot_net)) {
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200748 unsigned char *pn = (unsigned char *)&((struct sockaddr_in *)cli_conn->dst)->sin_addr;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200749
750 /* Note: we rely on the backend to get the header name to be used for
751 * x-original-to, because the header is really meant for the backends.
752 * However, if the backend did not specify any option, we have to rely
753 * on the frontend's header name.
754 */
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200755 chunk_printf(&trash, "%d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
756 if (unlikely(!http_add_header(htx, hdr, ist2(trash.area, trash.data))))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100757 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200758 }
759 }
Christopher Faulet5d1def62021-02-26 09:19:15 +0100760 else if (cli_conn && conn_get_dst(cli_conn) && cli_conn->dst->ss_family == AF_INET6) {
761 /* Add an X-Original-To header unless the source IP is
762 * in the 'except' network range.
763 */
764 if (ipcmp2net(cli_conn->dst, &sess->fe->except_xot_net) &&
765 ipcmp2net(cli_conn->dst, &s->be->except_xot_net)) {
766 char pn[INET6_ADDRSTRLEN];
767
768 inet_ntop(AF_INET6,
769 (const void *)&((struct sockaddr_in6 *)(cli_conn->dst))->sin6_addr,
770 pn, sizeof(pn));
771
772 /* Note: we rely on the backend to get the header name to be used for
773 * x-forwarded-for, because the header is really meant for the backends.
774 * However, if the backend did not specify any option, we have to rely
775 * on the frontend's header name.
776 */
777 chunk_printf(&trash, "%s", pn);
778 if (unlikely(!http_add_header(htx, hdr, ist2(trash.area, trash.data))))
779 goto return_int_err;
780 }
781 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200782 }
783
Christopher Fauletc2ac5e42021-03-08 18:20:09 +0100784 /* Filter the request headers if there are filters attached to the
785 * stream.
786 */
787 if (HAS_FILTERS(s))
788 req->analysers |= AN_REQ_FLT_HTTP_HDRS;
789
Christopher Faulete0768eb2018-10-03 16:38:02 +0200790 /* If we have no server assigned yet and we're balancing on url_param
791 * with a POST request, we may be interested in checking the body for
792 * that parameter. This will be done in another analyser.
793 */
794 if (!(s->flags & (SF_ASSIGNED|SF_DIRECT)) &&
Willy Tarreau089eaa02019-01-14 15:17:46 +0100795 s->txn->meth == HTTP_METH_POST &&
796 (s->be->lbprm.algo & BE_LB_ALGO) == BE_LB_ALGO_PH) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200797 channel_dont_connect(req);
798 req->analysers |= AN_REQ_HTTP_BODY;
799 }
800
801 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
802 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau1a18b542018-12-11 16:37:42 +0100803
Christopher Faulete0768eb2018-10-03 16:38:02 +0200804 /* We expect some data from the client. Unless we know for sure
805 * we already have a full request, we have to re-enable quick-ack
806 * in case we previously disabled it, otherwise we might cause
807 * the client to delay further data.
808 */
William Lallemand36119de2021-03-08 15:26:48 +0100809 if ((sess->listener && (sess->listener->options & LI_O_NOQUICKACK)) && !(htx->flags & HTX_FL_EOM))
Willy Tarreau1a18b542018-12-11 16:37:42 +0100810 conn_set_quickack(cli_conn, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200811
812 /*************************************************************
813 * OK, that's finished for the headers. We have done what we *
814 * could. Let's switch to the DATA state. *
815 ************************************************************/
816 req->analyse_exp = TICK_ETERNITY;
817 req->analysers &= ~an_bit;
818
819 s->logs.tv_request = now;
820 /* OK let's go on with the BODY now */
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100821 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200822 return 1;
823
Christopher Fauletb8a53712019-12-16 11:29:38 +0100824 return_int_err:
825 txn->status = 500;
826 if (!(s->flags & SF_ERR_MASK))
827 s->flags |= SF_ERR_INTERNAL;
Willy Tarreau4781b152021-04-06 13:53:36 +0200828 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100829 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau4781b152021-04-06 13:53:36 +0200830 _HA_ATOMIC_INC(&s->be->be_counters.internal_errors);
William Lallemand36119de2021-03-08 15:26:48 +0100831 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200832 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100833 goto return_prx_cond;
834
Christopher Faulete0768eb2018-10-03 16:38:02 +0200835 return_bad_req: /* let's centralize all bad requests */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200836 txn->status = 400;
Willy Tarreau4781b152021-04-06 13:53:36 +0200837 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req);
William Lallemand36119de2021-03-08 15:26:48 +0100838 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200839 _HA_ATOMIC_INC(&sess->listener->counters->failed_req);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100840 /* fall through */
841
842 return_prx_cond:
843 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +0200844
845 if (!(s->flags & SF_ERR_MASK))
846 s->flags |= SF_ERR_PRXCOND;
847 if (!(s->flags & SF_FINST_MASK))
848 s->flags |= SF_FINST_R;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100849
850 req->analysers &= AN_REQ_FLT_END;
851 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100852 DBG_TRACE_DEVEL("leaving on error",
853 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200854 return 0;
855}
856
857/* This function is an analyser which processes the HTTP tarpit. It always
858 * returns zero, at the beginning because it prevents any other processing
859 * from occurring, and at the end because it terminates the request.
860 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200861int http_process_tarpit(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200862{
863 struct http_txn *txn = s->txn;
864
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100865 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, &txn->req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200866 /* This connection is being tarpitted. The CLIENT side has
867 * already set the connect expiration date to the right
868 * timeout. We just have to check that the client is still
869 * there and that the timeout has not expired.
870 */
871 channel_dont_connect(req);
872 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100873 !tick_is_expired(req->analyse_exp, now_ms)) {
874 DBG_TRACE_DEVEL("waiting for tarpit timeout expiry",
875 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200876 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100877 }
878
Christopher Faulete0768eb2018-10-03 16:38:02 +0200879
880 /* We will set the queue timer to the time spent, just for
881 * logging purposes. We fake a 500 server error, so that the
882 * attacker will not suspect his connection has been tarpitted.
883 * It will not cause trouble to the logs because we can exclude
884 * the tarpitted connections by filtering on the 'PT' status flags.
885 */
886 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
887
Christopher Faulet8dfeccf2020-05-15 14:16:29 +0200888 http_reply_and_close(s, txn->status, (!(req->flags & CF_READ_ERROR) ? http_error_message(s) : NULL));
Christopher Faulet5cb513a2020-05-13 17:56:56 +0200889
890 end:
Christopher Faulete0768eb2018-10-03 16:38:02 +0200891 req->analysers &= AN_REQ_FLT_END;
892 req->analyse_exp = TICK_ETERNITY;
893
894 if (!(s->flags & SF_ERR_MASK))
895 s->flags |= SF_ERR_PRXCOND;
896 if (!(s->flags & SF_FINST_MASK))
897 s->flags |= SF_FINST_T;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100898
899 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200900 return 0;
901}
902
903/* This function is an analyser which waits for the HTTP request body. It waits
904 * for either the buffer to be full, or the full advertised contents to have
905 * reached the buffer. It must only be called after the standard HTTP request
906 * processing has occurred, because it expects the request to be parsed and will
907 * look for the Expect header. It may send a 100-Continue interim response. It
908 * takes in input any state starting from HTTP_MSG_BODY and leaves with one of
909 * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it
910 * needs to read more data, or 1 once it has completed its analysis.
911 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200912int http_wait_for_request_body(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200913{
914 struct session *sess = s->sess;
915 struct http_txn *txn = s->txn;
916 struct http_msg *msg = &s->txn->req;
917
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100918 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Fauletf76ebe82018-10-24 11:16:22 +0200919
Christopher Fauletf76ebe82018-10-24 11:16:22 +0200920
Christopher Faulet021a8e42021-03-29 10:46:38 +0200921 switch (http_wait_for_msg_body(s, req, s->be->timeout.httpreq, 0)) {
922 case HTTP_RULE_RES_CONT:
923 goto http_end;
924 case HTTP_RULE_RES_YIELD:
925 goto missing_data_or_waiting;
926 case HTTP_RULE_RES_BADREQ:
Willy Tarreau4236f032019-03-05 10:43:32 +0100927 goto return_bad_req;
Christopher Faulet021a8e42021-03-29 10:46:38 +0200928 case HTTP_RULE_RES_ERROR:
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200929 goto return_int_err;
Christopher Faulet021a8e42021-03-29 10:46:38 +0200930 case HTTP_RULE_RES_ABRT:
Christopher Fauletb8a53712019-12-16 11:29:38 +0100931 goto return_prx_cond;
Christopher Faulet021a8e42021-03-29 10:46:38 +0200932 default:
933 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200934 }
935
936 http_end:
937 /* The situation will not evolve, so let's give up on the analysis. */
938 s->logs.tv_request = now; /* update the request timer to reflect full request */
939 req->analysers &= ~an_bit;
940 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100941 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200942 return 1;
943
Christopher Faulet021a8e42021-03-29 10:46:38 +0200944 missing_data_or_waiting:
945 channel_dont_connect(req);
946 DBG_TRACE_DEVEL("waiting for more data",
947 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
948 return 0;
949
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200950 return_int_err:
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200951 txn->status = 500;
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200952 if (!(s->flags & SF_ERR_MASK))
953 s->flags |= SF_ERR_INTERNAL;
Willy Tarreau4781b152021-04-06 13:53:36 +0200954 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100955 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau4781b152021-04-06 13:53:36 +0200956 _HA_ATOMIC_INC(&s->be->be_counters.internal_errors);
William Lallemand36119de2021-03-08 15:26:48 +0100957 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200958 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Faulet021a8e42021-03-29 10:46:38 +0200959 goto return_prx_err;
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200960
Christopher Faulete0768eb2018-10-03 16:38:02 +0200961 return_bad_req: /* let's centralize all bad requests */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200962 txn->status = 400;
Willy Tarreau4781b152021-04-06 13:53:36 +0200963 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req);
William Lallemand36119de2021-03-08 15:26:48 +0100964 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200965 _HA_ATOMIC_INC(&sess->listener->counters->failed_req);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100966 /* fall through */
967
Christopher Faulet021a8e42021-03-29 10:46:38 +0200968 return_prx_err:
Christopher Fauletb8a53712019-12-16 11:29:38 +0100969 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet021a8e42021-03-29 10:46:38 +0200970 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200971
Christopher Faulet021a8e42021-03-29 10:46:38 +0200972 return_prx_cond:
Christopher Faulete0768eb2018-10-03 16:38:02 +0200973 if (!(s->flags & SF_ERR_MASK))
974 s->flags |= SF_ERR_PRXCOND;
975 if (!(s->flags & SF_FINST_MASK))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100976 s->flags |= (msg->msg_state < HTTP_MSG_DATA ? SF_FINST_R : SF_FINST_D);
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200977
Christopher Faulete0768eb2018-10-03 16:38:02 +0200978 req->analysers &= AN_REQ_FLT_END;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100979 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100980 DBG_TRACE_DEVEL("leaving on error",
981 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200982 return 0;
983}
984
985/* This function is an analyser which forwards request body (including chunk
986 * sizes if any). It is called as soon as we must forward, even if we forward
987 * zero byte. The only situation where it must not be called is when we're in
988 * tunnel mode and we want to forward till the close. It's used both to forward
989 * remaining data and to resync after end of body. It expects the msg_state to
990 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
991 * read more data, or 1 once we can go on with next request or end the stream.
992 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
993 * bytes of pending data + the headers if not already done.
994 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200995int http_request_forward_body(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200996{
997 struct session *sess = s->sess;
998 struct http_txn *txn = s->txn;
Christopher Faulet9768c262018-10-22 09:34:31 +0200999 struct http_msg *msg = &txn->req;
1000 struct htx *htx;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001001 short status = 0;
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001002 int ret;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001003
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001004 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001005
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001006 htx = htxbuf(&req->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001007
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001008 if (htx->flags & HTX_FL_PARSING_ERROR)
1009 goto return_bad_req;
1010 if (htx->flags & HTX_FL_PROCESSING_ERROR)
1011 goto return_int_err;
1012
Christopher Faulete0768eb2018-10-03 16:38:02 +02001013 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
1014 ((req->flags & CF_SHUTW) && (req->to_forward || co_data(req)))) {
1015 /* Output closed while we were sending data. We must abort and
1016 * wake the other side up.
Christopher Fauletf506d962021-04-27 10:56:28 +02001017 *
1018 * If we have finished to send the request and the response is
1019 * still in progress, don't catch write error on the request
1020 * side if it is in fact a read error on the server side.
Christopher Faulete0768eb2018-10-03 16:38:02 +02001021 */
Christopher Fauletf506d962021-04-27 10:56:28 +02001022 if (msg->msg_state == HTTP_MSG_DONE && (s->res.flags & CF_READ_ERROR) && s->res.analysers)
1023 return 0;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001024
Olivier Houchard29cac3c2019-07-12 15:48:58 +02001025 /* Don't abort yet if we had L7 retries activated and it
1026 * was a write error, we may recover.
1027 */
1028 if (!(req->flags & (CF_READ_ERROR | CF_READ_TIMEOUT)) &&
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001029 (s->si[1].flags & SI_FL_L7_RETRY)) {
1030 DBG_TRACE_DEVEL("leaving on L7 retry",
1031 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Olivier Houchard29cac3c2019-07-12 15:48:58 +02001032 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001033 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001034 msg->msg_state = HTTP_MSG_ERROR;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001035 http_end_request(s);
1036 http_end_response(s);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001037 DBG_TRACE_DEVEL("leaving on error",
1038 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001039 return 1;
1040 }
1041
1042 /* Note that we don't have to send 100-continue back because we don't
1043 * need the data to complete our job, and it's up to the server to
1044 * decide whether to return 100, 417 or anything else in return of
1045 * an "Expect: 100-continue" header.
1046 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001047 if (msg->msg_state == HTTP_MSG_BODY)
1048 msg->msg_state = HTTP_MSG_DATA;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001049
Christopher Faulete0768eb2018-10-03 16:38:02 +02001050 /* in most states, we should abort in case of early close */
1051 channel_auto_close(req);
1052
1053 if (req->to_forward) {
Christopher Faulet66af0b22019-03-22 14:54:52 +01001054 if (req->to_forward == CHN_INFINITE_FORWARD) {
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001055 if (req->flags & CF_EOI)
1056 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet66af0b22019-03-22 14:54:52 +01001057 }
1058 else {
1059 /* We can't process the buffer's contents yet */
1060 req->flags |= CF_WAKE_WRITE;
1061 goto missing_data_or_waiting;
1062 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001063 }
1064
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001065 if (msg->msg_state >= HTTP_MSG_ENDING)
1066 goto ending;
1067
1068 if (txn->meth == HTTP_METH_CONNECT) {
1069 msg->msg_state = HTTP_MSG_ENDING;
1070 goto ending;
1071 }
1072
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001073 /* Forward input data. We get it by removing all outgoing data not
1074 * forwarded yet from HTX data size. If there are some data filters, we
1075 * let them decide the amount of data to forward.
Christopher Faulet9768c262018-10-22 09:34:31 +02001076 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001077 if (HAS_REQ_DATA_FILTERS(s)) {
1078 ret = flt_http_payload(s, msg, htx->data);
1079 if (ret < 0)
1080 goto return_bad_req;
Christopher Faulet421e7692019-06-13 11:16:45 +02001081 c_adv(req, ret);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001082 }
1083 else {
Christopher Faulet421e7692019-06-13 11:16:45 +02001084 c_adv(req, htx->data - co_data(req));
Christopher Faulet66af0b22019-03-22 14:54:52 +01001085 if (msg->flags & HTTP_MSGF_XFER_LEN)
1086 channel_htx_forward_forever(req, htx);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001087 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001088
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001089 if (htx->data != co_data(req))
1090 goto missing_data_or_waiting;
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001091
Christopher Faulet9768c262018-10-22 09:34:31 +02001092 /* Check if the end-of-message is reached and if so, switch the message
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001093 * in HTTP_MSG_ENDING state. Then if all data was marked to be
1094 * forwarded, set the state to HTTP_MSG_DONE.
Christopher Faulet9768c262018-10-22 09:34:31 +02001095 */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001096 if (!(htx->flags & HTX_FL_EOM))
Christopher Faulet9768c262018-10-22 09:34:31 +02001097 goto missing_data_or_waiting;
1098
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001099 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet9768c262018-10-22 09:34:31 +02001100
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001101 ending:
Christopher Faulet2151cdd2020-07-22 16:34:59 +02001102 req->flags &= ~CF_EXPECT_MORE; /* no more data are expected */
1103
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001104 /* other states, ENDING...TUNNEL */
1105 if (msg->msg_state >= HTTP_MSG_DONE)
1106 goto done;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001107
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001108 if (HAS_REQ_DATA_FILTERS(s)) {
1109 ret = flt_http_end(s, msg);
1110 if (ret <= 0) {
1111 if (!ret)
1112 goto missing_data_or_waiting;
1113 goto return_bad_req;
1114 }
1115 }
1116
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001117 if (txn->meth == HTTP_METH_CONNECT)
1118 msg->msg_state = HTTP_MSG_TUNNEL;
1119 else {
1120 msg->msg_state = HTTP_MSG_DONE;
1121 req->to_forward = 0;
1122 }
1123
1124 done:
1125 /* we don't want to forward closes on DONE except in tunnel mode. */
1126 if (!(txn->flags & TX_CON_WANT_TUN))
1127 channel_dont_close(req);
1128
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001129 http_end_request(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001130 if (!(req->analysers & an_bit)) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001131 http_end_response(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001132 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
1133 if (req->flags & CF_SHUTW) {
1134 /* request errors are most likely due to the
1135 * server aborting the transfer. */
Christopher Faulet93e02d82019-03-08 14:18:50 +01001136 goto return_srv_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001137 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001138 goto return_bad_req;
1139 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001140 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001141 return 1;
1142 }
1143
1144 /* If "option abortonclose" is set on the backend, we want to monitor
1145 * the client's connection and forward any shutdown notification to the
1146 * server, which will decide whether to close or to go on processing the
1147 * request. We only do that in tunnel mode, and not in other modes since
1148 * it can be abused to exhaust source ports. */
Christopher Faulet769d0e92019-03-22 14:23:18 +01001149 if (s->be->options & PR_O_ABRT_CLOSE) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001150 channel_auto_read(req);
Christopher Fauletc41547b2019-07-16 14:32:23 +02001151 if ((req->flags & (CF_SHUTR|CF_READ_NULL)) && !(txn->flags & TX_CON_WANT_TUN))
Christopher Faulete0768eb2018-10-03 16:38:02 +02001152 s->si[1].flags |= SI_FL_NOLINGER;
1153 channel_auto_close(req);
1154 }
1155 else if (s->txn->meth == HTTP_METH_POST) {
1156 /* POST requests may require to read extra CRLF sent by broken
1157 * browsers and which could cause an RST to be sent upon close
1158 * on some systems (eg: Linux). */
1159 channel_auto_read(req);
1160 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001161 DBG_TRACE_DEVEL("waiting for the end of the HTTP txn",
1162 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001163 return 0;
1164
1165 missing_data_or_waiting:
1166 /* stop waiting for data if the input is closed before the end */
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001167 if (msg->msg_state < HTTP_MSG_ENDING && req->flags & CF_SHUTR)
Christopher Faulet93e02d82019-03-08 14:18:50 +01001168 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001169
1170 waiting:
1171 /* waiting for the last bits to leave the buffer */
1172 if (req->flags & CF_SHUTW)
Christopher Faulet93e02d82019-03-08 14:18:50 +01001173 goto return_srv_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001174
1175 /* When TE: chunked is used, we need to get there again to parse remaining
1176 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
1177 * And when content-length is used, we never want to let the possible
1178 * shutdown be forwarded to the other side, as the state machine will
1179 * take care of it once the client responds. It's also important to
1180 * prevent TIME_WAITs from accumulating on the backend side, and for
1181 * HTTP/2 where the last frame comes with a shutdown.
1182 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001183 if (msg->flags & HTTP_MSGF_XFER_LEN)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001184 channel_dont_close(req);
1185
1186 /* We know that more data are expected, but we couldn't send more that
1187 * what we did. So we always set the CF_EXPECT_MORE flag so that the
1188 * system knows it must not set a PUSH on this first part. Interactive
1189 * modes are already handled by the stream sock layer. We must not do
1190 * this in content-length mode because it could present the MSG_MORE
1191 * flag with the last block of forwarded data, which would cause an
1192 * additional delay to be observed by the receiver.
1193 */
Christopher Faulet2151cdd2020-07-22 16:34:59 +02001194 if (HAS_REQ_DATA_FILTERS(s))
Christopher Faulete0768eb2018-10-03 16:38:02 +02001195 req->flags |= CF_EXPECT_MORE;
1196
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001197 DBG_TRACE_DEVEL("waiting for more data to forward",
1198 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001199 return 0;
1200
Christopher Faulet93e02d82019-03-08 14:18:50 +01001201 return_cli_abort:
Willy Tarreau4781b152021-04-06 13:53:36 +02001202 _HA_ATOMIC_INC(&sess->fe->fe_counters.cli_aborts);
1203 _HA_ATOMIC_INC(&s->be->be_counters.cli_aborts);
William Lallemand36119de2021-03-08 15:26:48 +01001204 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001205 _HA_ATOMIC_INC(&sess->listener->counters->cli_aborts);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001206 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001207 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.cli_aborts);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001208 if (!(s->flags & SF_ERR_MASK))
1209 s->flags |= SF_ERR_CLICL;
1210 status = 400;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001211 goto return_prx_cond;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001212
1213 return_srv_abort:
Willy Tarreau4781b152021-04-06 13:53:36 +02001214 _HA_ATOMIC_INC(&sess->fe->fe_counters.srv_aborts);
1215 _HA_ATOMIC_INC(&s->be->be_counters.srv_aborts);
William Lallemand36119de2021-03-08 15:26:48 +01001216 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001217 _HA_ATOMIC_INC(&sess->listener->counters->srv_aborts);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001218 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001219 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.srv_aborts);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001220 if (!(s->flags & SF_ERR_MASK))
1221 s->flags |= SF_ERR_SRVCL;
1222 status = 502;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001223 goto return_prx_cond;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001224
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001225 return_int_err:
1226 if (!(s->flags & SF_ERR_MASK))
1227 s->flags |= SF_ERR_INTERNAL;
Willy Tarreau4781b152021-04-06 13:53:36 +02001228 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
1229 _HA_ATOMIC_INC(&s->be->be_counters.internal_errors);
William Lallemand36119de2021-03-08 15:26:48 +01001230 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001231 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001232 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001233 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.internal_errors);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001234 status = 500;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001235 goto return_prx_cond;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001236
Christopher Faulet93e02d82019-03-08 14:18:50 +01001237 return_bad_req:
Willy Tarreau4781b152021-04-06 13:53:36 +02001238 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req);
William Lallemand36119de2021-03-08 15:26:48 +01001239 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001240 _HA_ATOMIC_INC(&sess->listener->counters->failed_req);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001241 status = 400;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001242 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001243
Christopher Fauletb8a53712019-12-16 11:29:38 +01001244 return_prx_cond:
Christopher Faulet9768c262018-10-22 09:34:31 +02001245 if (txn->status > 0) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001246 /* Note: we don't send any error if some data were already sent */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001247 http_reply_and_close(s, txn->status, NULL);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001248 } else {
Christopher Faulet93e02d82019-03-08 14:18:50 +01001249 txn->status = status;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001250 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001251 }
1252 req->analysers &= AN_REQ_FLT_END;
1253 s->res.analysers &= AN_RES_FLT_END; /* we're in data phase, we want to abort both directions */
Christopher Fauletb8a53712019-12-16 11:29:38 +01001254 if (!(s->flags & SF_ERR_MASK))
1255 s->flags |= SF_ERR_PRXCOND;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001256 if (!(s->flags & SF_FINST_MASK))
1257 s->flags |= ((txn->rsp.msg_state < HTTP_MSG_ERROR) ? SF_FINST_H : SF_FINST_D);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001258 DBG_TRACE_DEVEL("leaving on error ",
1259 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001260 return 0;
1261}
1262
Olivier Houcharda254a372019-04-05 15:30:12 +02001263/* Reset the stream and the backend stream_interface to a situation suitable for attemption connection */
1264/* Returns 0 if we can attempt to retry, -1 otherwise */
1265static __inline int do_l7_retry(struct stream *s, struct stream_interface *si)
1266{
Christopher Faulet5bf85852021-05-21 13:46:14 +02001267 struct channel *req, *res;
1268 int co_data;
Olivier Houcharda254a372019-04-05 15:30:12 +02001269
1270 si->conn_retries--;
1271 if (si->conn_retries < 0)
Christopher Faulet043cdb22021-05-26 10:31:06 +02001272 return -1;
Christopher Faulet5b82cc52020-10-12 15:18:50 +02001273
Christopher Faulete763c8c2021-05-05 18:23:59 +02001274 if (objt_server(s->target)) {
1275 if (s->flags & SF_CURR_SESS) {
1276 s->flags &= ~SF_CURR_SESS;
1277 _HA_ATOMIC_DEC(&__objt_server(s->target)->cur_sess);
1278 }
Willy Tarreau4781b152021-04-06 13:53:36 +02001279 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.retries);
Christopher Faulete763c8c2021-05-05 18:23:59 +02001280 }
Willy Tarreau4781b152021-04-06 13:53:36 +02001281 _HA_ATOMIC_INC(&s->be->be_counters.retries);
Willy Tarreau223995e2019-05-04 10:38:31 +02001282
Christopher Faulet5bf85852021-05-21 13:46:14 +02001283 req = &s->req;
1284 res = &s->res;
Olivier Houcharda254a372019-04-05 15:30:12 +02001285 /* Remove any write error from the request, and read error from the response */
1286 req->flags &= ~(CF_WRITE_ERROR | CF_WRITE_TIMEOUT | CF_SHUTW | CF_SHUTW_NOW);
1287 res->flags &= ~(CF_READ_ERROR | CF_READ_TIMEOUT | CF_SHUTR | CF_EOI | CF_READ_NULL | CF_SHUTR_NOW);
1288 res->analysers = 0;
1289 si->flags &= ~(SI_FL_ERR | SI_FL_EXP | SI_FL_RXBLK_SHUT);
Christopher Faulet30aa0da2021-05-05 21:05:09 +02001290 si->err_type = SI_ET_NONE;
1291 s->flags &= ~(SF_ERR_MASK | SF_FINST_MASK);
Olivier Houchard4bd58672019-07-12 16:16:59 +02001292 stream_choose_redispatch(s);
Olivier Houcharda254a372019-04-05 15:30:12 +02001293 si->exp = TICK_ETERNITY;
1294 res->rex = TICK_ETERNITY;
1295 res->to_forward = 0;
1296 res->analyse_exp = TICK_ETERNITY;
1297 res->total = 0;
Olivier Houcharda254a372019-04-05 15:30:12 +02001298 si_release_endpoint(&s->si[1]);
Olivier Houcharda254a372019-04-05 15:30:12 +02001299
Christopher Faulet5bf85852021-05-21 13:46:14 +02001300 b_free(&req->buf);
1301 /* Swap the L7 buffer with the channel buffer */
1302 /* We know we stored the co_data as b_data, so get it there */
1303 co_data = b_data(&si->l7_buffer);
1304 b_set_data(&si->l7_buffer, b_size(&si->l7_buffer));
1305 b_xfer(&req->buf, &si->l7_buffer, b_data(&si->l7_buffer));
1306 co_set_data(req, co_data);
Christopher Faulet5b82cc52020-10-12 15:18:50 +02001307
Ilya Shipitsinacf84592021-02-06 22:29:08 +05001308 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 +02001309
Olivier Houcharda254a372019-04-05 15:30:12 +02001310 b_reset(&res->buf);
1311 co_set_data(res, 0);
1312 return 0;
1313}
1314
Christopher Faulete0768eb2018-10-03 16:38:02 +02001315/* This stream analyser waits for a complete HTTP response. It returns 1 if the
1316 * processing can continue on next analysers, or zero if it either needs more
1317 * data or wants to immediately abort the response (eg: timeout, error, ...). It
1318 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->res.analysers
1319 * when it has nothing left to do, and may remove any analyser when it wants to
1320 * abort.
1321 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001322int http_wait_for_response(struct stream *s, struct channel *rep, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001323{
Christopher Faulet9768c262018-10-22 09:34:31 +02001324 /*
1325 * We will analyze a complete HTTP response to check the its syntax.
1326 *
1327 * Once the start line and all headers are received, we may perform a
1328 * capture of the error (if any), and we will set a few fields. We also
1329 * logging and finally headers capture.
1330 */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001331 struct session *sess = s->sess;
1332 struct http_txn *txn = s->txn;
1333 struct http_msg *msg = &txn->rsp;
Christopher Faulet9768c262018-10-22 09:34:31 +02001334 struct htx *htx;
Olivier Houcharda254a372019-04-05 15:30:12 +02001335 struct stream_interface *si_b = &s->si[1];
Christopher Faulet61608322018-11-23 16:23:45 +01001336 struct connection *srv_conn;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001337 struct htx_sl *sl;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001338 int n;
1339
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001340 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001341
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001342 htx = htxbuf(&rep->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001343
Willy Tarreau4236f032019-03-05 10:43:32 +01001344 /* Parsing errors are caught here */
1345 if (htx->flags & HTX_FL_PARSING_ERROR)
1346 goto return_bad_res;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001347 if (htx->flags & HTX_FL_PROCESSING_ERROR)
1348 goto return_int_err;
Willy Tarreau4236f032019-03-05 10:43:32 +01001349
Christopher Faulete0768eb2018-10-03 16:38:02 +02001350 /*
1351 * Now we quickly check if we have found a full valid response.
1352 * If not so, we check the FD and buffer states before leaving.
1353 * A full response is indicated by the fact that we have seen
1354 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
1355 * responses are checked first.
1356 *
1357 * Depending on whether the client is still there or not, we
1358 * may send an error response back or not. Note that normally
1359 * we should only check for HTTP status there, and check I/O
1360 * errors somewhere else.
1361 */
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001362 next_one:
Christopher Faulet29f17582019-05-23 11:03:26 +02001363 if (unlikely(htx_is_empty(htx) || htx->first == -1)) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001364 /* 1: have we encountered a read error ? */
1365 if (rep->flags & CF_READ_ERROR) {
Olivier Houchard865d8392019-05-03 22:46:27 +02001366 struct connection *conn = NULL;
1367
Olivier Houchard865d8392019-05-03 22:46:27 +02001368 if (objt_cs(s->si[1].end))
1369 conn = objt_cs(s->si[1].end)->conn;
1370
Christopher Fauletb1875342021-05-26 12:15:37 +02001371 /* Perform a L7 retry because server refuses the early data. */
1372 if ((si_b->flags & SI_FL_L7_RETRY) &&
1373 (s->be->retry_type & PR_RE_EARLY_ERROR) &&
1374 conn && conn->err_code == CO_ER_SSL_EARLY_FAILED &&
1375 do_l7_retry(s, si_b) == 0) {
1376 DBG_TRACE_DEVEL("leaving on L7 retry",
1377 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
1378 return 0;
1379 }
1380
Olivier Houchard6db16992019-05-17 15:40:49 +02001381 if (txn->flags & TX_NOT_FIRST)
1382 goto abort_keep_alive;
1383
Willy Tarreau4781b152021-04-06 13:53:36 +02001384 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001385 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001386 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001387 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001388 }
1389
Christopher Fauletb1875342021-05-26 12:15:37 +02001390 /* if the server refused the early data, just send a 425 */
1391 if (conn && conn->err_code == CO_ER_SSL_EARLY_FAILED)
Olivier Houchard865d8392019-05-03 22:46:27 +02001392 txn->status = 425;
Christopher Fauletb1875342021-05-26 12:15:37 +02001393 else {
1394 txn->status = 502;
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001395 stream_inc_http_fail_ctr(s);
Christopher Fauletb1875342021-05-26 12:15:37 +02001396 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001397
Christopher Fauletb1875342021-05-26 12:15:37 +02001398 rep->analysers &= AN_RES_FLT_END;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001399 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001400 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001401
1402 if (!(s->flags & SF_ERR_MASK))
1403 s->flags |= SF_ERR_SRVCL;
1404 if (!(s->flags & SF_FINST_MASK))
1405 s->flags |= SF_FINST_H;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001406 DBG_TRACE_DEVEL("leaving on error",
1407 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001408 return 0;
1409 }
1410
Christopher Faulet9768c262018-10-22 09:34:31 +02001411 /* 2: read timeout : return a 504 to the client. */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001412 else if (rep->flags & CF_READ_TIMEOUT) {
Olivier Houcharda254a372019-04-05 15:30:12 +02001413 if ((si_b->flags & SI_FL_L7_RETRY) &&
1414 (s->be->retry_type & PR_RE_TIMEOUT)) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001415 if (co_data(rep) || do_l7_retry(s, si_b) == 0) {
1416 DBG_TRACE_DEVEL("leaving on L7 retry",
1417 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharda254a372019-04-05 15:30:12 +02001418 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001419 }
Olivier Houcharda254a372019-04-05 15:30:12 +02001420 }
Willy Tarreau4781b152021-04-06 13:53:36 +02001421 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001422 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001423 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001424 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001425 }
1426
Christopher Faulete0768eb2018-10-03 16:38:02 +02001427 rep->analysers &= AN_RES_FLT_END;
1428 txn->status = 504;
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001429 stream_inc_http_fail_ctr(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001430 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001431 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001432
1433 if (!(s->flags & SF_ERR_MASK))
1434 s->flags |= SF_ERR_SRVTO;
1435 if (!(s->flags & SF_FINST_MASK))
1436 s->flags |= SF_FINST_H;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001437 DBG_TRACE_DEVEL("leaving on error",
1438 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001439 return 0;
1440 }
1441
Christopher Faulet9768c262018-10-22 09:34:31 +02001442 /* 3: client abort with an abortonclose */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001443 else if ((rep->flags & CF_SHUTR) && ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001444 _HA_ATOMIC_INC(&sess->fe->fe_counters.cli_aborts);
1445 _HA_ATOMIC_INC(&s->be->be_counters.cli_aborts);
William Lallemand36119de2021-03-08 15:26:48 +01001446 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001447 _HA_ATOMIC_INC(&sess->listener->counters->cli_aborts);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001448 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001449 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.cli_aborts);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001450
1451 rep->analysers &= AN_RES_FLT_END;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001452 txn->status = 400;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001453 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001454
1455 if (!(s->flags & SF_ERR_MASK))
1456 s->flags |= SF_ERR_CLICL;
1457 if (!(s->flags & SF_FINST_MASK))
1458 s->flags |= SF_FINST_H;
1459
1460 /* process_stream() will take care of the error */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001461 DBG_TRACE_DEVEL("leaving on error",
1462 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001463 return 0;
1464 }
1465
Christopher Faulet9768c262018-10-22 09:34:31 +02001466 /* 4: close from server, capture the response if the server has started to respond */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001467 else if (rep->flags & CF_SHUTR) {
Olivier Houcharda254a372019-04-05 15:30:12 +02001468 if ((si_b->flags & SI_FL_L7_RETRY) &&
1469 (s->be->retry_type & PR_RE_DISCONNECTED)) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001470 if (co_data(rep) || do_l7_retry(s, si_b) == 0) {
1471 DBG_TRACE_DEVEL("leaving on L7 retry",
1472 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharda254a372019-04-05 15:30:12 +02001473 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001474 }
Olivier Houcharda254a372019-04-05 15:30:12 +02001475 }
1476
Olivier Houchard6db16992019-05-17 15:40:49 +02001477 if (txn->flags & TX_NOT_FIRST)
1478 goto abort_keep_alive;
1479
Willy Tarreau4781b152021-04-06 13:53:36 +02001480 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001481 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001482 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001483 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001484 }
1485
Christopher Faulete0768eb2018-10-03 16:38:02 +02001486 rep->analysers &= AN_RES_FLT_END;
1487 txn->status = 502;
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001488 stream_inc_http_fail_ctr(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001489 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001490 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001491
1492 if (!(s->flags & SF_ERR_MASK))
1493 s->flags |= SF_ERR_SRVCL;
1494 if (!(s->flags & SF_FINST_MASK))
1495 s->flags |= SF_FINST_H;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001496 DBG_TRACE_DEVEL("leaving on error",
1497 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001498 return 0;
1499 }
1500
Christopher Faulet9768c262018-10-22 09:34:31 +02001501 /* 5: write error to client (we don't send any message then) */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001502 else if (rep->flags & CF_WRITE_ERROR) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001503 if (txn->flags & TX_NOT_FIRST)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001504 goto abort_keep_alive;
1505
Willy Tarreau4781b152021-04-06 13:53:36 +02001506 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001507 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001508 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001509 rep->analysers &= AN_RES_FLT_END;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001510
1511 if (!(s->flags & SF_ERR_MASK))
1512 s->flags |= SF_ERR_CLICL;
1513 if (!(s->flags & SF_FINST_MASK))
1514 s->flags |= SF_FINST_H;
1515
1516 /* process_stream() will take care of the error */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001517 DBG_TRACE_DEVEL("leaving on error",
1518 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001519 return 0;
1520 }
1521
1522 channel_dont_close(rep);
1523 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001524 DBG_TRACE_DEVEL("waiting for more data",
1525 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001526 return 0;
1527 }
1528
1529 /* More interesting part now : we know that we have a complete
1530 * response which at least looks like HTTP. We have an indicator
1531 * of each header's length, so we can parse them quickly.
1532 */
Christopher Faulet29f17582019-05-23 11:03:26 +02001533 BUG_ON(htx_get_first_type(htx) != HTX_BLK_RES_SL);
Christopher Faulet297fbb42019-05-13 14:41:27 +02001534 sl = http_get_stline(htx);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001535
Christopher Faulet0f66d552021-05-26 13:14:39 +02001536 /* Perform a L7 retry because of the status code */
1537 if ((si_b->flags & SI_FL_L7_RETRY) &&
1538 l7_status_match(s->be, sl->info.res.status) &&
1539 do_l7_retry(s, si_b) == 0) {
1540 DBG_TRACE_DEVEL("leaving on L7 retry", STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
1541 return 0;
1542 }
1543
1544 /* Now, L7 buffer is useless, it can be released */
1545 b_free(&s->si[1].l7_buffer);
1546
1547 msg->msg_state = HTTP_MSG_BODY;
1548
1549
Christopher Faulet9768c262018-10-22 09:34:31 +02001550 /* 0: we might have to print this header in debug mode */
1551 if (unlikely((global.mode & MODE_DEBUG) &&
1552 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
1553 int32_t pos;
1554
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001555 http_debug_stline("srvrep", s, sl);
Christopher Faulet9768c262018-10-22 09:34:31 +02001556
Christopher Fauleta3f15502019-05-13 15:27:23 +02001557 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001558 struct htx_blk *blk = htx_get_blk(htx, pos);
1559 enum htx_blk_type type = htx_get_blk_type(blk);
1560
1561 if (type == HTX_BLK_EOH)
1562 break;
1563 if (type != HTX_BLK_HDR)
1564 continue;
1565
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001566 http_debug_hdr("srvhdr", s,
1567 htx_get_blk_name(htx, blk),
1568 htx_get_blk_value(htx, blk));
Christopher Faulet9768c262018-10-22 09:34:31 +02001569 }
1570 }
1571
Christopher Faulet03599112018-11-27 11:21:21 +01001572 /* 1: get the status code and the version. Also set HTTP flags */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001573 txn->status = sl->info.res.status;
Christopher Faulet03599112018-11-27 11:21:21 +01001574 if (sl->flags & HTX_SL_F_VER_11)
Christopher Faulet9768c262018-10-22 09:34:31 +02001575 msg->flags |= HTTP_MSGF_VER_11;
Christopher Faulet03599112018-11-27 11:21:21 +01001576 if (sl->flags & HTX_SL_F_XFER_LEN) {
1577 msg->flags |= HTTP_MSGF_XFER_LEN;
Christopher Faulet2a408542020-11-20 14:22:37 +01001578 if (sl->flags & HTX_SL_F_CLEN)
1579 msg->flags |= HTTP_MSGF_CNT_LEN;
1580 else if (sl->flags & HTX_SL_F_CHNK)
1581 msg->flags |= HTTP_MSGF_TE_CHNK;
Christopher Faulet03599112018-11-27 11:21:21 +01001582 }
Christopher Faulet2a408542020-11-20 14:22:37 +01001583 if (sl->flags & HTX_SL_F_BODYLESS)
1584 msg->flags |= HTTP_MSGF_BODYLESS;
Christopher Faulet576c3582021-01-08 15:53:01 +01001585 if (sl->flags & HTX_SL_F_CONN_UPG)
1586 msg->flags |= HTTP_MSGF_CONN_UPG;
Christopher Faulet9768c262018-10-22 09:34:31 +02001587
1588 n = txn->status / 100;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001589 if (n < 1 || n > 5)
1590 n = 0;
Christopher Faulet9768c262018-10-22 09:34:31 +02001591
Christopher Faulete0768eb2018-10-03 16:38:02 +02001592 /* when the client triggers a 4xx from the server, it's most often due
1593 * to a missing object or permission. These events should be tracked
1594 * because if they happen often, it may indicate a brute force or a
1595 * vulnerability scan.
1596 */
1597 if (n == 4)
1598 stream_inc_http_err_ctr(s);
1599
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001600 if (n == 5 && txn->status != 501 && txn->status != 505)
1601 stream_inc_http_fail_ctr(s);
1602
Marcin Deranek3c27dda2020-05-15 18:32:51 +02001603 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001604 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.p.http.rsp[n]);
1605 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.p.http.cum_req);
Marcin Deranek3c27dda2020-05-15 18:32:51 +02001606 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001607
Christopher Faulete0768eb2018-10-03 16:38:02 +02001608 /* Adjust server's health based on status code. Note: status codes 501
1609 * and 505 are triggered on demand by client request, so we must not
1610 * count them as server failures.
1611 */
1612 if (objt_server(s->target)) {
1613 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001614 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_OK);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001615 else
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001616 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_STS);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001617 }
1618
1619 /*
1620 * We may be facing a 100-continue response, or any other informational
1621 * 1xx response which is non-final, in which case this is not the right
1622 * response, and we're waiting for the next one. Let's allow this response
1623 * to go to the client and wait for the next one. There's an exception for
1624 * 101 which is used later in the code to switch protocols.
1625 */
1626 if (txn->status < 200 &&
1627 (txn->status == 100 || txn->status >= 102)) {
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001628 FLT_STRM_CB(s, flt_http_reset(s, msg));
Christopher Faulet421e7692019-06-13 11:16:45 +02001629 htx->first = channel_htx_fwd_headers(rep, htx);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001630 msg->msg_state = HTTP_MSG_RPBEFORE;
Christopher Faulet3499f622019-09-03 15:23:54 +02001631 msg->flags = 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001632 txn->status = 0;
1633 s->logs.t_data = -1; /* was not a response yet */
Christopher Faulet7d518452020-08-31 11:07:07 +02001634 rep->flags |= CF_SEND_DONTWAIT; /* Send ASAP informational messages */
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001635 goto next_one;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001636 }
1637
Christopher Faulet6e6c7b12021-01-08 16:02:05 +01001638 /* A 101-switching-protocols must contains a Connection header with the
1639 * "upgrade" option and the request too. It means both are agree to
1640 * upgrade. It is not so strict because there is no test on the Upgrade
1641 * header content. But it is probably stronger enough for now.
1642 */
1643 if (txn->status == 101 &&
1644 (!(txn->req.flags & HTTP_MSGF_CONN_UPG) || !(txn->rsp.flags & HTTP_MSGF_CONN_UPG)))
1645 goto return_bad_res;
1646
Christopher Faulete0768eb2018-10-03 16:38:02 +02001647 /*
1648 * 2: check for cacheability.
1649 */
1650
1651 switch (txn->status) {
1652 case 200:
1653 case 203:
1654 case 204:
1655 case 206:
1656 case 300:
1657 case 301:
1658 case 404:
1659 case 405:
1660 case 410:
1661 case 414:
1662 case 501:
1663 break;
1664 default:
1665 /* RFC7231#6.1:
1666 * Responses with status codes that are defined as
1667 * cacheable by default (e.g., 200, 203, 204, 206,
1668 * 300, 301, 404, 405, 410, 414, and 501 in this
1669 * specification) can be reused by a cache with
1670 * heuristic expiration unless otherwise indicated
1671 * by the method definition or explicit cache
1672 * controls [RFC7234]; all other status codes are
1673 * not cacheable by default.
1674 */
1675 txn->flags &= ~(TX_CACHEABLE | TX_CACHE_COOK);
1676 break;
1677 }
1678
1679 /*
1680 * 3: we may need to capture headers
1681 */
1682 s->logs.logwait &= ~LW_RESP;
1683 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->res_cap))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001684 http_capture_headers(htx, s->res_cap, sess->fe->rsp_cap);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001685
Christopher Faulet9768c262018-10-22 09:34:31 +02001686 /* Skip parsing if no content length is possible. */
Christopher Fauletc75668e2020-12-07 18:10:32 +01001687 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status >= 200 && txn->status < 300) ||
Christopher Faulete0768eb2018-10-03 16:38:02 +02001688 txn->status == 101)) {
1689 /* Either we've established an explicit tunnel, or we're
1690 * switching the protocol. In both cases, we're very unlikely
1691 * to understand the next protocols. We have to switch to tunnel
1692 * mode, so that we transfer the request and responses then let
1693 * this protocol pass unmodified. When we later implement specific
1694 * parsers for such protocols, we'll want to check the Upgrade
1695 * header which contains information about that protocol for
1696 * responses with status 101 (eg: see RFC2817 about TLS).
1697 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02001698 txn->flags |= TX_CON_WANT_TUN;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001699 }
1700
Christopher Faulet61608322018-11-23 16:23:45 +01001701 /* check for NTML authentication headers in 401 (WWW-Authenticate) and
1702 * 407 (Proxy-Authenticate) responses and set the connection to private
1703 */
1704 srv_conn = cs_conn(objt_cs(s->si[1].end));
1705 if (srv_conn) {
1706 struct ist hdr;
1707 struct http_hdr_ctx ctx;
1708
1709 if (txn->status == 401)
1710 hdr = ist("WWW-Authenticate");
1711 else if (txn->status == 407)
1712 hdr = ist("Proxy-Authenticate");
1713 else
1714 goto end;
1715
1716 ctx.blk = NULL;
1717 while (http_find_header(htx, hdr, &ctx, 0)) {
Willy Tarreauf1dcced2020-05-07 19:27:02 +02001718 /* If www-authenticate contains "Negotiate", "Nego2", or "NTLM",
1719 * possibly followed by blanks and a base64 string, the connection
1720 * is private. Since it's a mess to deal with, we only check for
1721 * values starting with "NTLM" or "Nego". Note that often multiple
1722 * headers are sent by the server there.
1723 */
1724 if ((ctx.value.len >= 4 && strncasecmp(ctx.value.ptr, "Nego", 4) == 0) ||
Willy Tarreau49a1d282020-05-07 19:10:15 +02001725 (ctx.value.len >= 4 && strncasecmp(ctx.value.ptr, "NTLM", 4) == 0)) {
Olivier Houchard250031e2019-05-29 15:01:50 +02001726 sess->flags |= SESS_FL_PREFER_LAST;
Christopher Faulet08016ab2020-07-01 16:10:06 +02001727 conn_set_owner(srv_conn, sess, NULL);
Christopher Faulet21ddc742020-07-01 15:26:14 +02001728 conn_set_private(srv_conn);
Ilya Shipitsin6b79f382020-07-23 00:32:55 +05001729 /* If it fail now, the same will be done in mux->detach() callback */
Christopher Faulet08016ab2020-07-01 16:10:06 +02001730 session_add_conn(srv_conn->owner, srv_conn, srv_conn->target);
Willy Tarreauf1dcced2020-05-07 19:27:02 +02001731 break;
Olivier Houchard250031e2019-05-29 15:01:50 +02001732 }
Christopher Faulet61608322018-11-23 16:23:45 +01001733 }
1734 }
1735
1736 end:
Christopher Faulete0768eb2018-10-03 16:38:02 +02001737 /* we want to have the response time before we start processing it */
1738 s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now);
1739
1740 /* end of job, return OK */
1741 rep->analysers &= ~an_bit;
1742 rep->analyse_exp = TICK_ETERNITY;
1743 channel_auto_close(rep);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001744 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001745 return 1;
1746
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001747 return_int_err:
Willy Tarreau4781b152021-04-06 13:53:36 +02001748 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
1749 _HA_ATOMIC_INC(&s->be->be_counters.internal_errors);
William Lallemand36119de2021-03-08 15:26:48 +01001750 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001751 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Fauletb8a53712019-12-16 11:29:38 +01001752 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001753 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.internal_errors);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001754 txn->status = 500;
1755 if (!(s->flags & SF_ERR_MASK))
1756 s->flags |= SF_ERR_INTERNAL;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001757 goto return_prx_cond;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001758
1759 return_bad_res:
Willy Tarreau4781b152021-04-06 13:53:36 +02001760 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Faulet47365272018-10-31 17:40:50 +01001761 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001762 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001763 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Christopher Faulet47365272018-10-31 17:40:50 +01001764 }
Olivier Houcharde3249a92019-05-03 23:01:47 +02001765 if ((s->be->retry_type & PR_RE_JUNK_REQUEST) &&
Olivier Houchardad26d8d2019-05-10 17:48:28 +02001766 (si_b->flags & SI_FL_L7_RETRY) &&
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001767 do_l7_retry(s, si_b) == 0) {
1768 DBG_TRACE_DEVEL("leaving on L7 retry",
1769 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharde3249a92019-05-03 23:01:47 +02001770 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001771 }
Christopher Faulet47365272018-10-31 17:40:50 +01001772 txn->status = 502;
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001773 stream_inc_http_fail_ctr(s);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001774 /* fall through */
1775
Christopher Fauletb8a53712019-12-16 11:29:38 +01001776 return_prx_cond:
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001777 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet47365272018-10-31 17:40:50 +01001778
1779 if (!(s->flags & SF_ERR_MASK))
1780 s->flags |= SF_ERR_PRXCOND;
1781 if (!(s->flags & SF_FINST_MASK))
1782 s->flags |= SF_FINST_H;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001783
1784 s->si[1].flags |= SI_FL_NOLINGER;
1785 rep->analysers &= AN_RES_FLT_END;
Christopher Faulete58c0002020-03-02 16:21:01 +01001786 s->req.analysers &= AN_REQ_FLT_END;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001787 rep->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001788 DBG_TRACE_DEVEL("leaving on error",
1789 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulet47365272018-10-31 17:40:50 +01001790 return 0;
1791
Christopher Faulete0768eb2018-10-03 16:38:02 +02001792 abort_keep_alive:
1793 /* A keep-alive request to the server failed on a network error.
1794 * The client is required to retry. We need to close without returning
1795 * any other information so that the client retries.
1796 */
1797 txn->status = 0;
1798 rep->analysers &= AN_RES_FLT_END;
1799 s->req.analysers &= AN_REQ_FLT_END;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001800 s->logs.logwait = 0;
1801 s->logs.level = 0;
1802 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001803 http_reply_and_close(s, txn->status, NULL);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001804 DBG_TRACE_DEVEL("leaving by closing K/A connection",
1805 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001806 return 0;
1807}
1808
1809/* This function performs all the processing enabled for the current response.
1810 * It normally returns 1 unless it wants to break. It relies on buffers flags,
1811 * and updates s->res.analysers. It might make sense to explode it into several
1812 * other functions. It works like process_request (see indications above).
1813 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001814int http_process_res_common(struct stream *s, struct channel *rep, int an_bit, struct proxy *px)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001815{
1816 struct session *sess = s->sess;
1817 struct http_txn *txn = s->txn;
1818 struct http_msg *msg = &txn->rsp;
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001819 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001820 struct proxy *cur_proxy;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001821 enum rule_result ret = HTTP_RULE_RES_CONT;
1822
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001823 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
1824 return 0;
Christopher Faulet9768c262018-10-22 09:34:31 +02001825
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001826 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001827
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001828 htx = htxbuf(&rep->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001829
1830 /* The stats applet needs to adjust the Connection header but we don't
1831 * apply any filter there.
1832 */
1833 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
1834 rep->analysers &= ~an_bit;
1835 rep->analyse_exp = TICK_ETERNITY;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001836 goto end;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001837 }
1838
1839 /*
1840 * We will have to evaluate the filters.
1841 * As opposed to version 1.2, now they will be evaluated in the
1842 * filters order and not in the header order. This means that
1843 * each filter has to be validated among all headers.
1844 *
1845 * Filters are tried with ->be first, then with ->fe if it is
1846 * different from ->be.
1847 *
1848 * Maybe we are in resume condiion. In this case I choose the
1849 * "struct proxy" which contains the rule list matching the resume
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05001850 * pointer. If none of these "struct proxy" match, I initialise
Christopher Faulete0768eb2018-10-03 16:38:02 +02001851 * the process with the first one.
1852 *
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05001853 * In fact, I check only correspondence between the current list
Christopher Faulete0768eb2018-10-03 16:38:02 +02001854 * pointer and the ->fe rule list. If it doesn't match, I initialize
1855 * the loop with the ->be.
1856 */
1857 if (s->current_rule_list == &sess->fe->http_res_rules)
1858 cur_proxy = sess->fe;
1859 else
1860 cur_proxy = s->be;
1861 while (1) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001862 /* evaluate http-response rules */
1863 if (ret == HTTP_RULE_RES_CONT) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001864 ret = http_res_get_intercept_rule(cur_proxy, &cur_proxy->http_res_rules, s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001865
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001866 switch (ret) {
1867 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
1868 goto return_prx_yield;
1869
1870 case HTTP_RULE_RES_CONT:
1871 case HTTP_RULE_RES_STOP: /* nothing to do */
1872 break;
1873
1874 case HTTP_RULE_RES_DENY: /* deny or tarpit */
1875 goto deny;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001876
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001877 case HTTP_RULE_RES_ABRT: /* abort request, response already sent */
1878 goto return_prx_cond;
1879
1880 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
Christopher Fauletb8a53712019-12-16 11:29:38 +01001881 goto done;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001882
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001883 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
1884 goto return_bad_res;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001885
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001886 case HTTP_RULE_RES_ERROR: /* failed with a bad request */
1887 goto return_int_err;
1888 }
1889
1890 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001891
Christopher Faulete0768eb2018-10-03 16:38:02 +02001892 /* check whether we're already working on the frontend */
1893 if (cur_proxy == sess->fe)
1894 break;
1895 cur_proxy = sess->fe;
1896 }
1897
Christopher Faulete0768eb2018-10-03 16:38:02 +02001898 /* OK that's all we can do for 1xx responses */
1899 if (unlikely(txn->status < 200 && txn->status != 101))
Christopher Fauletf2824e62018-10-01 12:12:37 +02001900 goto end;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001901
1902 /*
1903 * Now check for a server cookie.
1904 */
1905 if (s->be->cookie_name || sess->fe->capture_name || (s->be->options & PR_O_CHK_CACHE))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001906 http_manage_server_side_cookies(s, rep);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001907
1908 /*
1909 * Check for cache-control or pragma headers if required.
1910 */
1911 if ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001912 http_check_response_for_cacheability(s, rep);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001913
1914 /*
1915 * Add server cookie in the response if needed
1916 */
1917 if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) &&
1918 !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) &&
1919 (!(s->flags & SF_DIRECT) ||
1920 ((s->be->cookie_maxidle || txn->cookie_last_date) &&
1921 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
1922 (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
1923 (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
1924 (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
1925 !(s->flags & SF_IGNORE_PRST)) {
1926 /* the server is known, it's not the one the client requested, or the
1927 * cookie's last seen date needs to be refreshed. We have to
1928 * insert a set-cookie here, except if we want to insert only on POST
1929 * requests and this one isn't. Note that servers which don't have cookies
1930 * (eg: some backup servers) will return a full cookie removal request.
1931 */
1932 if (!objt_server(s->target)->cookie) {
1933 chunk_printf(&trash,
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001934 "%s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
Christopher Faulete0768eb2018-10-03 16:38:02 +02001935 s->be->cookie_name);
1936 }
1937 else {
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001938 chunk_printf(&trash, "%s=%s", s->be->cookie_name, objt_server(s->target)->cookie);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001939
1940 if (s->be->cookie_maxidle || s->be->cookie_maxlife) {
1941 /* emit last_date, which is mandatory */
1942 trash.area[trash.data++] = COOKIE_DELIM_DATE;
1943 s30tob64((date.tv_sec+3) >> 2,
1944 trash.area + trash.data);
1945 trash.data += 5;
1946
1947 if (s->be->cookie_maxlife) {
1948 /* emit first_date, which is either the original one or
1949 * the current date.
1950 */
1951 trash.area[trash.data++] = COOKIE_DELIM_DATE;
1952 s30tob64(txn->cookie_first_date ?
1953 txn->cookie_first_date >> 2 :
1954 (date.tv_sec+3) >> 2,
1955 trash.area + trash.data);
1956 trash.data += 5;
1957 }
1958 }
1959 chunk_appendf(&trash, "; path=/");
1960 }
1961
1962 if (s->be->cookie_domain)
1963 chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain);
1964
1965 if (s->be->ck_opts & PR_CK_HTTPONLY)
1966 chunk_appendf(&trash, "; HttpOnly");
1967
1968 if (s->be->ck_opts & PR_CK_SECURE)
1969 chunk_appendf(&trash, "; Secure");
1970
Christopher Faulet2f533902020-01-21 11:06:48 +01001971 if (s->be->cookie_attrs)
1972 chunk_appendf(&trash, "; %s", s->be->cookie_attrs);
1973
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001974 if (unlikely(!http_add_header(htx, ist("Set-Cookie"), ist2(trash.area, trash.data))))
Christopher Fauletb8a53712019-12-16 11:29:38 +01001975 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001976
1977 txn->flags &= ~TX_SCK_MASK;
1978 if (__objt_server(s->target)->cookie && (s->flags & SF_DIRECT))
1979 /* the server did not change, only the date was updated */
1980 txn->flags |= TX_SCK_UPDATED;
1981 else
1982 txn->flags |= TX_SCK_INSERTED;
1983
1984 /* Here, we will tell an eventual cache on the client side that we don't
1985 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
1986 * Some caches understand the correct form: 'no-cache="set-cookie"', but
1987 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
1988 */
1989 if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
1990
1991 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
1992
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001993 if (unlikely(!http_add_header(htx, ist("Cache-control"), ist("private"))))
Christopher Fauletb8a53712019-12-16 11:29:38 +01001994 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001995 }
1996 }
1997
1998 /*
1999 * Check if result will be cacheable with a cookie.
2000 * We'll block the response if security checks have caught
2001 * nasty things such as a cacheable cookie.
2002 */
2003 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
2004 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
2005 (s->be->options & PR_O_CHK_CACHE)) {
2006 /* we're in presence of a cacheable response containing
2007 * a set-cookie header. We'll block it as requested by
2008 * the 'checkcache' option, and send an alert.
2009 */
Christopher Faulete0768eb2018-10-03 16:38:02 +02002010 ha_alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
2011 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
2012 send_log(s->be, LOG_ALERT,
2013 "Blocking cacheable cookie in response from instance %s, server %s.\n",
2014 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
Christopher Fauletb8a53712019-12-16 11:29:38 +01002015 goto deny;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002016 }
2017
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002018 end:
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002019 /*
2020 * Evaluate after-response rules before forwarding the response. rules
2021 * from the backend are evaluated first, then one from the frontend if
2022 * it differs.
2023 */
2024 if (!http_eval_after_res_rules(s))
2025 goto return_int_err;
2026
Christopher Fauletc2ac5e42021-03-08 18:20:09 +01002027 /* Filter the response headers if there are filters attached to the
2028 * stream.
2029 */
2030 if (HAS_FILTERS(s))
2031 rep->analysers |= AN_RES_FLT_HTTP_HDRS;
2032
Christopher Faulete0768eb2018-10-03 16:38:02 +02002033 /* Always enter in the body analyzer */
2034 rep->analysers &= ~AN_RES_FLT_XFER_DATA;
2035 rep->analysers |= AN_RES_HTTP_XFER_BODY;
2036
2037 /* if the user wants to log as soon as possible, without counting
2038 * bytes from the server, then this is the right moment. We have
2039 * to temporarily assign bytes_out to log what we currently have.
2040 */
2041 if (!LIST_ISEMPTY(&sess->fe->logformat) && !(s->logs.logwait & LW_BYTES)) {
2042 s->logs.t_close = s->logs.t_data; /* to get a valid end date */
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002043 s->logs.bytes_out = htx->data;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002044 s->do_log(s);
2045 s->logs.bytes_out = 0;
2046 }
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002047
Christopher Fauletb8a53712019-12-16 11:29:38 +01002048 done:
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002049 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002050 rep->analysers &= ~an_bit;
2051 rep->analyse_exp = TICK_ETERNITY;
2052 return 1;
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002053
Christopher Fauletb8a53712019-12-16 11:29:38 +01002054 deny:
Willy Tarreau4781b152021-04-06 13:53:36 +02002055 _HA_ATOMIC_INC(&sess->fe->fe_counters.denied_resp);
2056 _HA_ATOMIC_INC(&s->be->be_counters.denied_resp);
William Lallemand36119de2021-03-08 15:26:48 +01002057 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02002058 _HA_ATOMIC_INC(&sess->listener->counters->denied_resp);
Christopher Fauleta08546b2019-12-16 16:07:34 +01002059 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02002060 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.denied_resp);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002061 goto return_prx_err;
2062
2063 return_int_err:
2064 txn->status = 500;
2065 if (!(s->flags & SF_ERR_MASK))
2066 s->flags |= SF_ERR_INTERNAL;
Willy Tarreau4781b152021-04-06 13:53:36 +02002067 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
2068 _HA_ATOMIC_INC(&s->be->be_counters.internal_errors);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002069 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02002070 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.internal_errors);
Christopher Fauletcff0f732019-12-16 16:13:44 +01002071 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02002072 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.internal_errors);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002073 goto return_prx_err;
2074
2075 return_bad_res:
2076 txn->status = 502;
Willy Tarreau826f3ab2021-02-10 12:07:15 +01002077 stream_inc_http_fail_ctr(s);
Willy Tarreau4781b152021-04-06 13:53:36 +02002078 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Fauleta20a6532020-02-05 10:16:41 +01002079 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02002080 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Christopher Fauleta20a6532020-02-05 10:16:41 +01002081 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_RSP);
2082 }
Christopher Fauletb8a53712019-12-16 11:29:38 +01002083 /* fall through */
2084
2085 return_prx_err:
2086 http_reply_and_close(s, txn->status, http_error_message(s));
2087 /* fall through */
2088
2089 return_prx_cond:
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002090 s->logs.t_data = -1; /* was not a valid response */
2091 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002092
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002093 if (!(s->flags & SF_ERR_MASK))
2094 s->flags |= SF_ERR_PRXCOND;
2095 if (!(s->flags & SF_FINST_MASK))
2096 s->flags |= SF_FINST_H;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002097
Christopher Faulete58c0002020-03-02 16:21:01 +01002098 rep->analysers &= AN_RES_FLT_END;
2099 s->req.analysers &= AN_REQ_FLT_END;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002100 rep->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002101 DBG_TRACE_DEVEL("leaving on error",
2102 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002103 return 0;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002104
2105 return_prx_yield:
2106 channel_dont_close(rep);
2107 DBG_TRACE_DEVEL("waiting for more data",
2108 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
2109 return 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002110}
2111
2112/* This function is an analyser which forwards response body (including chunk
2113 * sizes if any). It is called as soon as we must forward, even if we forward
2114 * zero byte. The only situation where it must not be called is when we're in
2115 * tunnel mode and we want to forward till the close. It's used both to forward
2116 * remaining data and to resync after end of body. It expects the msg_state to
2117 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
2118 * read more data, or 1 once we can go on with next request or end the stream.
2119 *
2120 * It is capable of compressing response data both in content-length mode and
2121 * in chunked mode. The state machines follows different flows depending on
2122 * whether content-length and chunked modes are used, since there are no
2123 * trailers in content-length :
2124 *
2125 * chk-mode cl-mode
2126 * ,----- BODY -----.
2127 * / \
2128 * V size > 0 V chk-mode
2129 * .--> SIZE -------------> DATA -------------> CRLF
2130 * | | size == 0 | last byte |
2131 * | v final crlf v inspected |
2132 * | TRAILERS -----------> DONE |
2133 * | |
2134 * `----------------------------------------------'
2135 *
2136 * Compression only happens in the DATA state, and must be flushed in final
2137 * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding
2138 * is performed at once on final states for all bytes parsed, or when leaving
2139 * on missing data.
2140 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002141int http_response_forward_body(struct stream *s, struct channel *res, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +02002142{
2143 struct session *sess = s->sess;
2144 struct http_txn *txn = s->txn;
2145 struct http_msg *msg = &s->txn->rsp;
Christopher Faulet9768c262018-10-22 09:34:31 +02002146 struct htx *htx;
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002147 int ret;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002148
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002149 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002150
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002151 htx = htxbuf(&res->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002152
Christopher Fauletb9a92f32019-09-09 10:15:21 +02002153 if (htx->flags & HTX_FL_PARSING_ERROR)
2154 goto return_bad_res;
2155 if (htx->flags & HTX_FL_PROCESSING_ERROR)
2156 goto return_int_err;
2157
Christopher Faulete0768eb2018-10-03 16:38:02 +02002158 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Christopher Fauletf2824e62018-10-01 12:12:37 +02002159 ((res->flags & CF_SHUTW) && (res->to_forward || co_data(res)))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02002160 /* Output closed while we were sending data. We must abort and
2161 * wake the other side up.
2162 */
Christopher Faulete0768eb2018-10-03 16:38:02 +02002163 msg->msg_state = HTTP_MSG_ERROR;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002164 http_end_response(s);
2165 http_end_request(s);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002166 DBG_TRACE_DEVEL("leaving on error",
2167 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002168 return 1;
2169 }
2170
Christopher Faulet9768c262018-10-22 09:34:31 +02002171 if (msg->msg_state == HTTP_MSG_BODY)
2172 msg->msg_state = HTTP_MSG_DATA;
2173
Christopher Faulete0768eb2018-10-03 16:38:02 +02002174 /* in most states, we should abort in case of early close */
2175 channel_auto_close(res);
2176
Christopher Faulete0768eb2018-10-03 16:38:02 +02002177 if (res->to_forward) {
Christopher Faulet66af0b22019-03-22 14:54:52 +01002178 if (res->to_forward == CHN_INFINITE_FORWARD) {
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002179 if (res->flags & CF_EOI)
2180 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet66af0b22019-03-22 14:54:52 +01002181 }
2182 else {
2183 /* We can't process the buffer's contents yet */
2184 res->flags |= CF_WAKE_WRITE;
2185 goto missing_data_or_waiting;
2186 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02002187 }
2188
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002189 if (msg->msg_state >= HTTP_MSG_ENDING)
2190 goto ending;
2191
Christopher Fauletc75668e2020-12-07 18:10:32 +01002192 if ((txn->meth == HTTP_METH_CONNECT && txn->status >= 200 && txn->status < 300) || txn->status == 101 ||
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002193 (!(msg->flags & HTTP_MSGF_XFER_LEN) && !HAS_RSP_DATA_FILTERS(s))) {
2194 msg->msg_state = HTTP_MSG_ENDING;
2195 goto ending;
2196 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002197
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002198 /* Forward input data. We get it by removing all outgoing data not
2199 * forwarded yet from HTX data size. If there are some data filters, we
2200 * let them decide the amount of data to forward.
Christopher Faulet9768c262018-10-22 09:34:31 +02002201 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002202 if (HAS_RSP_DATA_FILTERS(s)) {
2203 ret = flt_http_payload(s, msg, htx->data);
2204 if (ret < 0)
2205 goto return_bad_res;
Christopher Faulet421e7692019-06-13 11:16:45 +02002206 c_adv(res, ret);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002207 }
2208 else {
Christopher Faulet421e7692019-06-13 11:16:45 +02002209 c_adv(res, htx->data - co_data(res));
Christopher Faulet66af0b22019-03-22 14:54:52 +01002210 if (msg->flags & HTTP_MSGF_XFER_LEN)
2211 channel_htx_forward_forever(res, htx);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002212 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002213
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002214 if (htx->data != co_data(res))
2215 goto missing_data_or_waiting;
2216
2217 if (!(msg->flags & HTTP_MSGF_XFER_LEN) && res->flags & CF_SHUTR) {
2218 msg->msg_state = HTTP_MSG_ENDING;
2219 goto ending;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002220 }
2221
Christopher Faulet9768c262018-10-22 09:34:31 +02002222 /* Check if the end-of-message is reached and if so, switch the message
Christopher Fauletd20fdb02019-06-13 16:43:22 +02002223 * in HTTP_MSG_ENDING state. Then if all data was marked to be
2224 * forwarded, set the state to HTTP_MSG_DONE.
Christopher Faulet9768c262018-10-22 09:34:31 +02002225 */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002226 if (!(htx->flags & HTX_FL_EOM))
Christopher Faulet9768c262018-10-22 09:34:31 +02002227 goto missing_data_or_waiting;
2228
Christopher Fauletd20fdb02019-06-13 16:43:22 +02002229 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet9768c262018-10-22 09:34:31 +02002230
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002231 ending:
Christopher Faulet2151cdd2020-07-22 16:34:59 +02002232 res->flags &= ~CF_EXPECT_MORE; /* no more data are expected */
2233
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002234 /* other states, ENDING...TUNNEL */
2235 if (msg->msg_state >= HTTP_MSG_DONE)
2236 goto done;
Christopher Faulet9768c262018-10-22 09:34:31 +02002237
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002238 if (HAS_RSP_DATA_FILTERS(s)) {
2239 ret = flt_http_end(s, msg);
2240 if (ret <= 0) {
2241 if (!ret)
2242 goto missing_data_or_waiting;
2243 goto return_bad_res;
2244 }
2245 }
2246
Christopher Fauletc75668e2020-12-07 18:10:32 +01002247 if ((txn->meth == HTTP_METH_CONNECT && txn->status >= 200 && txn->status < 300) || txn->status == 101 ||
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002248 !(msg->flags & HTTP_MSGF_XFER_LEN)) {
2249 msg->msg_state = HTTP_MSG_TUNNEL;
2250 goto ending;
2251 }
2252 else {
2253 msg->msg_state = HTTP_MSG_DONE;
2254 res->to_forward = 0;
2255 }
2256
2257 done:
2258
2259 channel_dont_close(res);
2260
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002261 http_end_response(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002262 if (!(res->analysers & an_bit)) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002263 http_end_request(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002264 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
2265 if (res->flags & CF_SHUTW) {
2266 /* response errors are most likely due to the
2267 * client aborting the transfer. */
Christopher Faulet93e02d82019-03-08 14:18:50 +01002268 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002269 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02002270 goto return_bad_res;
2271 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002272 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002273 return 1;
2274 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002275 DBG_TRACE_DEVEL("waiting for the end of the HTTP txn",
2276 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002277 return 0;
2278
2279 missing_data_or_waiting:
2280 if (res->flags & CF_SHUTW)
Christopher Faulet93e02d82019-03-08 14:18:50 +01002281 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002282
2283 /* stop waiting for data if the input is closed before the end. If the
2284 * client side was already closed, it means that the client has aborted,
2285 * so we don't want to count this as a server abort. Otherwise it's a
2286 * server abort.
2287 */
Christopher Fauletd20fdb02019-06-13 16:43:22 +02002288 if (msg->msg_state < HTTP_MSG_ENDING && res->flags & CF_SHUTR) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02002289 if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))
Christopher Faulet93e02d82019-03-08 14:18:50 +01002290 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002291 /* If we have some pending data, we continue the processing */
Christopher Faulet93e02d82019-03-08 14:18:50 +01002292 if (htx_is_empty(htx))
2293 goto return_srv_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002294 }
2295
Christopher Faulete0768eb2018-10-03 16:38:02 +02002296 /* When TE: chunked is used, we need to get there again to parse
2297 * remaining chunks even if the server has closed, so we don't want to
Christopher Faulet9768c262018-10-22 09:34:31 +02002298 * set CF_DONTCLOSE. Similarly when there is a content-leng or if there
2299 * are filters registered on the stream, we don't want to forward a
2300 * close
Christopher Faulete0768eb2018-10-03 16:38:02 +02002301 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002302 if ((msg->flags & HTTP_MSGF_XFER_LEN) || HAS_RSP_DATA_FILTERS(s))
Christopher Faulete0768eb2018-10-03 16:38:02 +02002303 channel_dont_close(res);
2304
2305 /* We know that more data are expected, but we couldn't send more that
2306 * what we did. So we always set the CF_EXPECT_MORE flag so that the
2307 * system knows it must not set a PUSH on this first part. Interactive
2308 * modes are already handled by the stream sock layer. We must not do
2309 * this in content-length mode because it could present the MSG_MORE
2310 * flag with the last block of forwarded data, which would cause an
2311 * additional delay to be observed by the receiver.
2312 */
Christopher Faulet2151cdd2020-07-22 16:34:59 +02002313 if (HAS_RSP_DATA_FILTERS(s))
Christopher Faulete0768eb2018-10-03 16:38:02 +02002314 res->flags |= CF_EXPECT_MORE;
2315
2316 /* the stream handler will take care of timeouts and errors */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002317 DBG_TRACE_DEVEL("waiting for more data to forward",
2318 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002319 return 0;
2320
Christopher Faulet93e02d82019-03-08 14:18:50 +01002321 return_srv_abort:
Willy Tarreau4781b152021-04-06 13:53:36 +02002322 _HA_ATOMIC_INC(&sess->fe->fe_counters.srv_aborts);
2323 _HA_ATOMIC_INC(&s->be->be_counters.srv_aborts);
William Lallemand36119de2021-03-08 15:26:48 +01002324 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02002325 _HA_ATOMIC_INC(&sess->listener->counters->srv_aborts);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002326 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02002327 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.srv_aborts);
Willy Tarreau826f3ab2021-02-10 12:07:15 +01002328 stream_inc_http_fail_ctr(s);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002329 if (!(s->flags & SF_ERR_MASK))
2330 s->flags |= SF_ERR_SRVCL;
2331 goto return_error;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002332
Christopher Faulet93e02d82019-03-08 14:18:50 +01002333 return_cli_abort:
Willy Tarreau4781b152021-04-06 13:53:36 +02002334 _HA_ATOMIC_INC(&sess->fe->fe_counters.cli_aborts);
2335 _HA_ATOMIC_INC(&s->be->be_counters.cli_aborts);
William Lallemand36119de2021-03-08 15:26:48 +01002336 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02002337 _HA_ATOMIC_INC(&sess->listener->counters->cli_aborts);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002338 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02002339 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.cli_aborts);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002340 if (!(s->flags & SF_ERR_MASK))
2341 s->flags |= SF_ERR_CLICL;
2342 goto return_error;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002343
Christopher Fauletb9a92f32019-09-09 10:15:21 +02002344 return_int_err:
Willy Tarreau4781b152021-04-06 13:53:36 +02002345 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
2346 _HA_ATOMIC_INC(&s->be->be_counters.internal_errors);
William Lallemand36119de2021-03-08 15:26:48 +01002347 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02002348 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002349 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02002350 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.internal_errors);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02002351 if (!(s->flags & SF_ERR_MASK))
2352 s->flags |= SF_ERR_INTERNAL;
2353 goto return_error;
2354
Christopher Faulet93e02d82019-03-08 14:18:50 +01002355 return_bad_res:
Willy Tarreau4781b152021-04-06 13:53:36 +02002356 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002357 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02002358 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002359 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_RSP);
2360 }
Willy Tarreau826f3ab2021-02-10 12:07:15 +01002361 stream_inc_http_fail_ctr(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002362 if (!(s->flags & SF_ERR_MASK))
Christopher Faulet93e02d82019-03-08 14:18:50 +01002363 s->flags |= SF_ERR_SRVCL;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002364 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +02002365
Christopher Faulet93e02d82019-03-08 14:18:50 +01002366 return_error:
Christopher Faulete0768eb2018-10-03 16:38:02 +02002367 /* don't send any error message as we're in the body */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002368 http_reply_and_close(s, txn->status, NULL);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002369 res->analysers &= AN_RES_FLT_END;
2370 s->req.analysers &= AN_REQ_FLT_END; /* we're in data phase, we want to abort both directions */
Christopher Faulete0768eb2018-10-03 16:38:02 +02002371 if (!(s->flags & SF_FINST_MASK))
2372 s->flags |= SF_FINST_D;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002373 DBG_TRACE_DEVEL("leaving on error",
2374 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002375 return 0;
2376}
2377
Christopher Fauletf2824e62018-10-01 12:12:37 +02002378/* Perform an HTTP redirect based on the information in <rule>. The function
Christopher Faulet99daf282018-11-28 22:58:13 +01002379 * returns zero on success, or zero in case of a, irrecoverable error such
Christopher Fauletf2824e62018-10-01 12:12:37 +02002380 * as too large a request to build a valid response.
2381 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002382int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn)
Christopher Fauletf2824e62018-10-01 12:12:37 +02002383{
Christopher Faulet99daf282018-11-28 22:58:13 +01002384 struct channel *req = &s->req;
2385 struct channel *res = &s->res;
2386 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01002387 struct htx_sl *sl;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002388 struct buffer *chunk;
Christopher Faulet99daf282018-11-28 22:58:13 +01002389 struct ist status, reason, location;
2390 unsigned int flags;
Christopher Faulet08e66462019-05-23 16:44:59 +02002391 int close = 0; /* Try to keep the connection alive byt default */
Christopher Fauletf2824e62018-10-01 12:12:37 +02002392
2393 chunk = alloc_trash_chunk();
Christopher Fauletb8a53712019-12-16 11:29:38 +01002394 if (!chunk) {
2395 if (!(s->flags & SF_ERR_MASK))
2396 s->flags |= SF_ERR_RESOURCE;
Christopher Faulet99daf282018-11-28 22:58:13 +01002397 goto fail;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002398 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002399
Christopher Faulet99daf282018-11-28 22:58:13 +01002400 /*
2401 * Create the location
2402 */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002403 htx = htxbuf(&req->buf);
Christopher Fauletf2824e62018-10-01 12:12:37 +02002404 switch(rule->type) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002405 case REDIRECT_TYPE_SCHEME: {
2406 struct http_hdr_ctx ctx;
2407 struct ist path, host;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002408
Christopher Faulet99daf282018-11-28 22:58:13 +01002409 host = ist("");
2410 ctx.blk = NULL;
2411 if (http_find_header(htx, ist("Host"), &ctx, 0))
2412 host = ctx.value;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002413
Christopher Faulet297fbb42019-05-13 14:41:27 +02002414 sl = http_get_stline(htx);
Christopher Faulet99daf282018-11-28 22:58:13 +01002415 path = http_get_path(htx_sl_req_uri(sl));
2416 /* build message using path */
Tim Duesterhused526372020-03-05 17:56:33 +01002417 if (isttest(path)) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002418 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2419 int qs = 0;
2420 while (qs < path.len) {
2421 if (*(path.ptr + qs) == '?') {
2422 path.len = qs;
2423 break;
2424 }
2425 qs++;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002426 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002427 }
2428 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002429 else
2430 path = ist("/");
Christopher Fauletf2824e62018-10-01 12:12:37 +02002431
Christopher Faulet99daf282018-11-28 22:58:13 +01002432 if (rule->rdr_str) { /* this is an old "redirect" rule */
2433 /* add scheme */
2434 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2435 goto fail;
2436 }
2437 else {
2438 /* add scheme with executing log format */
2439 chunk->data += build_logline(s, chunk->area + chunk->data,
2440 chunk->size - chunk->data,
2441 &rule->rdr_fmt);
2442 }
2443 /* add "://" + host + path */
2444 if (!chunk_memcat(chunk, "://", 3) ||
2445 !chunk_memcat(chunk, host.ptr, host.len) ||
2446 !chunk_memcat(chunk, path.ptr, path.len))
2447 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002448
Christopher Faulet99daf282018-11-28 22:58:13 +01002449 /* append a slash at the end of the location if needed and missing */
2450 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
2451 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
2452 if (chunk->data + 1 >= chunk->size)
2453 goto fail;
2454 chunk->area[chunk->data++] = '/';
2455 }
2456 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002457 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002458
Christopher Faulet99daf282018-11-28 22:58:13 +01002459 case REDIRECT_TYPE_PREFIX: {
2460 struct ist path;
2461
Christopher Faulet297fbb42019-05-13 14:41:27 +02002462 sl = http_get_stline(htx);
Christopher Faulet99daf282018-11-28 22:58:13 +01002463 path = http_get_path(htx_sl_req_uri(sl));
2464 /* build message using path */
Tim Duesterhused526372020-03-05 17:56:33 +01002465 if (isttest(path)) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002466 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2467 int qs = 0;
2468 while (qs < path.len) {
2469 if (*(path.ptr + qs) == '?') {
2470 path.len = qs;
2471 break;
2472 }
2473 qs++;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002474 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002475 }
2476 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002477 else
2478 path = ist("/");
Christopher Fauletf2824e62018-10-01 12:12:37 +02002479
Christopher Faulet99daf282018-11-28 22:58:13 +01002480 if (rule->rdr_str) { /* this is an old "redirect" rule */
2481 /* add prefix. Note that if prefix == "/", we don't want to
2482 * add anything, otherwise it makes it hard for the user to
2483 * configure a self-redirection.
2484 */
2485 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
2486 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2487 goto fail;
2488 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002489 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002490 else {
2491 /* add prefix with executing log format */
2492 chunk->data += build_logline(s, chunk->area + chunk->data,
2493 chunk->size - chunk->data,
2494 &rule->rdr_fmt);
2495 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002496
Christopher Faulet99daf282018-11-28 22:58:13 +01002497 /* add path */
2498 if (!chunk_memcat(chunk, path.ptr, path.len))
2499 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002500
Christopher Faulet99daf282018-11-28 22:58:13 +01002501 /* append a slash at the end of the location if needed and missing */
2502 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
2503 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
2504 if (chunk->data + 1 >= chunk->size)
2505 goto fail;
2506 chunk->area[chunk->data++] = '/';
2507 }
2508 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002509 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002510 case REDIRECT_TYPE_LOCATION:
2511 default:
2512 if (rule->rdr_str) { /* this is an old "redirect" rule */
2513 /* add location */
2514 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2515 goto fail;
2516 }
2517 else {
2518 /* add location with executing log format */
2519 chunk->data += build_logline(s, chunk->area + chunk->data,
2520 chunk->size - chunk->data,
2521 &rule->rdr_fmt);
2522 }
2523 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002524 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002525 location = ist2(chunk->area, chunk->data);
2526
2527 /*
2528 * Create the 30x response
2529 */
2530 switch (rule->code) {
2531 case 308:
2532 status = ist("308");
2533 reason = ist("Permanent Redirect");
2534 break;
2535 case 307:
2536 status = ist("307");
2537 reason = ist("Temporary Redirect");
2538 break;
2539 case 303:
2540 status = ist("303");
2541 reason = ist("See Other");
2542 break;
2543 case 301:
2544 status = ist("301");
2545 reason = ist("Moved Permanently");
2546 break;
2547 case 302:
2548 default:
2549 status = ist("302");
2550 reason = ist("Found");
2551 break;
2552 }
2553
Christopher Faulet08e66462019-05-23 16:44:59 +02002554 if (!(txn->req.flags & HTTP_MSGF_BODYLESS) && txn->req.msg_state != HTTP_MSG_DONE)
2555 close = 1;
2556
Christopher Faulet99daf282018-11-28 22:58:13 +01002557 htx = htx_from_buf(&res->buf);
Kevin Zhu96b36392020-01-07 09:42:55 +01002558 /* Trim any possible response */
2559 channel_htx_truncate(&s->res, htx);
Christopher Faulet99daf282018-11-28 22:58:13 +01002560 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
2561 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), status, reason);
2562 if (!sl)
2563 goto fail;
2564 sl->info.res.status = rule->code;
2565 s->txn->status = rule->code;
2566
Christopher Faulet08e66462019-05-23 16:44:59 +02002567 if (close && !htx_add_header(htx, ist("Connection"), ist("close")))
2568 goto fail;
2569
2570 if (!htx_add_header(htx, ist("Content-length"), ist("0")) ||
Christopher Faulet99daf282018-11-28 22:58:13 +01002571 !htx_add_header(htx, ist("Location"), location))
2572 goto fail;
2573
2574 if (rule->code == 302 || rule->code == 303 || rule->code == 307) {
2575 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")))
2576 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002577 }
2578
2579 if (rule->cookie_len) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002580 if (!htx_add_header(htx, ist("Set-Cookie"), ist2(rule->cookie_str, rule->cookie_len)))
2581 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002582 }
2583
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002584 if (!htx_add_endof(htx, HTX_BLK_EOH))
Christopher Faulet99daf282018-11-28 22:58:13 +01002585 goto fail;
2586
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002587 htx->flags |= HTX_FL_EOM;
Kevin Zhu96b36392020-01-07 09:42:55 +01002588 htx_to_buf(htx, &res->buf);
Christopher Fauleta72a7e42020-01-28 09:28:11 +01002589 if (!http_forward_proxy_resp(s, 1))
2590 goto fail;
Christopher Faulet99daf282018-11-28 22:58:13 +01002591
Christopher Faulet60b33a52020-01-28 09:18:10 +01002592 if (rule->flags & REDIRECT_FLAG_FROM_REQ) {
2593 /* let's log the request time */
2594 s->logs.tv_request = now;
2595 req->analysers &= AN_REQ_FLT_END;
Christopher Faulet99daf282018-11-28 22:58:13 +01002596
Christopher Faulet60b33a52020-01-28 09:18:10 +01002597 if (s->sess->fe == s->be) /* report it if the request was intercepted by the frontend */
Willy Tarreau4781b152021-04-06 13:53:36 +02002598 _HA_ATOMIC_INC(&s->sess->fe->fe_counters.intercepted_req);
Christopher Faulet60b33a52020-01-28 09:18:10 +01002599 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002600
2601 if (!(s->flags & SF_ERR_MASK))
2602 s->flags |= SF_ERR_LOCAL;
2603 if (!(s->flags & SF_FINST_MASK))
Christopher Faulet60b33a52020-01-28 09:18:10 +01002604 s->flags |= ((rule->flags & REDIRECT_FLAG_FROM_REQ) ? SF_FINST_R : SF_FINST_H);
Christopher Fauletf2824e62018-10-01 12:12:37 +02002605
Christopher Faulet99daf282018-11-28 22:58:13 +01002606 free_trash_chunk(chunk);
2607 return 1;
2608
2609 fail:
2610 /* If an error occurred, remove the incomplete HTTP response from the
2611 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01002612 channel_htx_truncate(res, htxbuf(&res->buf));
Christopher Fauletf2824e62018-10-01 12:12:37 +02002613 free_trash_chunk(chunk);
Christopher Faulet99daf282018-11-28 22:58:13 +01002614 return 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002615}
2616
Christopher Faulet92d34fe2019-12-17 09:20:34 +01002617/* Replace all headers matching the name <name>. The header value is replaced if
2618 * it matches the regex <re>. <str> is used for the replacement. If <full> is
2619 * set to 1, the full-line is matched and replaced. Otherwise, comma-separated
2620 * values are evaluated one by one. It returns 0 on success and -1 on error.
2621 */
2622int http_replace_hdrs(struct stream* s, struct htx *htx, struct ist name,
2623 const char *str, struct my_regex *re, int full)
Christopher Faulet72333522018-10-24 11:25:02 +02002624{
2625 struct http_hdr_ctx ctx;
2626 struct buffer *output = get_trash_chunk();
2627
Christopher Faulet72333522018-10-24 11:25:02 +02002628 ctx.blk = NULL;
Christopher Faulet92d34fe2019-12-17 09:20:34 +01002629 while (http_find_header(htx, name, &ctx, full)) {
Christopher Faulet72333522018-10-24 11:25:02 +02002630 if (!regex_exec_match2(re, ctx.value.ptr, ctx.value.len, MAX_MATCH, pmatch, 0))
2631 continue;
2632
2633 output->data = exp_replace(output->area, output->size, ctx.value.ptr, str, pmatch);
2634 if (output->data == -1)
2635 return -1;
2636 if (!http_replace_header_value(htx, &ctx, ist2(output->area, output->data)))
2637 return -1;
2638 }
2639 return 0;
2640}
2641
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002642/* This function executes one of the set-{method,path,query,uri} actions. It
2643 * takes the string from the variable 'replace' with length 'len', then modifies
2644 * the relevant part of the request line accordingly. Then it updates various
2645 * pointers to the next elements which were moved, and the total buffer length.
2646 * It finds the action to be performed in p[2], previously filled by function
2647 * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal
2648 * error, though this can be revisited when this code is finally exploited.
2649 *
2650 * 'action' can be '0' to replace method, '1' to replace path, '2' to replace
Christopher Faulet312294f2020-09-02 17:17:44 +02002651 * query string, 3 to replace uri or 4 to replace the path+query.
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002652 *
2653 * In query string case, the mark question '?' must be set at the start of the
2654 * string by the caller, event if the replacement query string is empty.
2655 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002656int http_req_replace_stline(int action, const char *replace, int len,
2657 struct proxy *px, struct stream *s)
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002658{
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002659 struct htx *htx = htxbuf(&s->req.buf);
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002660
2661 switch (action) {
2662 case 0: // method
2663 if (!http_replace_req_meth(htx, ist2(replace, len)))
2664 return -1;
2665 break;
2666
2667 case 1: // path
Christopher Fauletb8ce5052020-08-31 16:11:57 +02002668 if (!http_replace_req_path(htx, ist2(replace, len), 0))
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002669 return -1;
2670 break;
2671
2672 case 2: // query
2673 if (!http_replace_req_query(htx, ist2(replace, len)))
2674 return -1;
2675 break;
2676
2677 case 3: // uri
2678 if (!http_replace_req_uri(htx, ist2(replace, len)))
2679 return -1;
2680 break;
2681
Christopher Faulet312294f2020-09-02 17:17:44 +02002682 case 4: // path + query
2683 if (!http_replace_req_path(htx, ist2(replace, len), 1))
2684 return -1;
2685 break;
2686
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002687 default:
2688 return -1;
2689 }
2690 return 0;
2691}
2692
2693/* This function replace the HTTP status code and the associated message. The
Christopher Faulete00d06c2019-12-16 17:18:42 +01002694 * variable <status> contains the new status code. This function never fails. It
2695 * returns 0 in case of success, -1 in case of internal error.
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002696 */
Christopher Faulet96bff762019-12-17 13:46:18 +01002697int http_res_set_status(unsigned int status, struct ist reason, struct stream *s)
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002698{
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002699 struct htx *htx = htxbuf(&s->res.buf);
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002700 char *res;
2701
2702 chunk_reset(&trash);
2703 res = ultoa_o(status, trash.area, trash.size);
2704 trash.data = res - trash.area;
2705
2706 /* Do we have a custom reason format string? */
Tim Duesterhuse296d3e2020-03-05 17:56:31 +01002707 if (!isttest(reason)) {
Christopher Faulet96bff762019-12-17 13:46:18 +01002708 const char *str = http_get_reason(status);
Tim Duesterhusdcf753a2021-03-04 17:31:47 +01002709 reason = ist(str);
Christopher Faulet96bff762019-12-17 13:46:18 +01002710 }
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002711
Christopher Fauletbde2c4c2020-08-31 16:43:34 +02002712 if (!http_replace_res_status(htx, ist2(trash.area, trash.data), reason))
Christopher Faulete00d06c2019-12-16 17:18:42 +01002713 return -1;
2714 return 0;
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002715}
2716
Christopher Faulet3e964192018-10-24 11:39:23 +02002717/* Executes the http-request rules <rules> for stream <s>, proxy <px> and
2718 * transaction <txn>. Returns the verdict of the first rule that prevents
2719 * further processing of the request (auth, deny, ...), and defaults to
2720 * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or
2721 * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT
2722 * on txn->flags if it encounters a tarpit rule. If <deny_status> is not NULL
2723 * and a deny/tarpit rule is matched, it will be filled with this rule's deny
2724 * status.
2725 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002726static enum rule_result http_req_get_intercept_rule(struct proxy *px, struct list *rules,
Christopher Fauletb58f62b2020-01-13 16:40:13 +01002727 struct stream *s)
Christopher Faulet3e964192018-10-24 11:39:23 +02002728{
2729 struct session *sess = strm_sess(s);
2730 struct http_txn *txn = s->txn;
Christopher Faulet3e964192018-10-24 11:39:23 +02002731 struct act_rule *rule;
Christopher Faulet3e964192018-10-24 11:39:23 +02002732 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002733 int act_opts = 0;
Christopher Faulet3e964192018-10-24 11:39:23 +02002734
Christopher Faulet3e964192018-10-24 11:39:23 +02002735 /* If "the current_rule_list" match the executed rule list, we are in
2736 * resume condition. If a resume is needed it is always in the action
2737 * and never in the ACL or converters. In this case, we initialise the
2738 * current rule, and go to the action execution point.
2739 */
2740 if (s->current_rule) {
2741 rule = s->current_rule;
2742 s->current_rule = NULL;
2743 if (s->current_rule_list == rules)
2744 goto resume_execution;
2745 }
2746 s->current_rule_list = rules;
2747
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002748 /* start the ruleset evaluation in strict mode */
2749 txn->req.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01002750
Christopher Faulet3e964192018-10-24 11:39:23 +02002751 list_for_each_entry(rule, rules, list) {
2752 /* check optional condition */
2753 if (rule->cond) {
2754 int ret;
2755
2756 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
2757 ret = acl_pass(ret);
2758
2759 if (rule->cond->pol == ACL_COND_UNLESS)
2760 ret = !ret;
2761
2762 if (!ret) /* condition not matched */
2763 continue;
2764 }
2765
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002766 act_opts |= ACT_OPT_FIRST;
Christopher Faulet3e964192018-10-24 11:39:23 +02002767 resume_execution:
Amaury Denoyelle03517732021-05-07 14:25:01 +02002768 if (rule->kw->flags & KWF_EXPERIMENTAL)
2769 mark_tainted(TAINTED_ACTION_EXP_EXECUTED);
2770
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002771 /* Always call the action function if defined */
2772 if (rule->action_ptr) {
2773 if ((s->req.flags & CF_READ_ERROR) ||
2774 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
2775 (px->options & PR_O_ABRT_CLOSE)))
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002776 act_opts |= ACT_OPT_FINAL;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002777
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002778 switch (rule->action_ptr(rule, px, sess, s, act_opts)) {
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002779 case ACT_RET_CONT:
2780 break;
2781 case ACT_RET_STOP:
2782 rule_ret = HTTP_RULE_RES_STOP;
2783 goto end;
2784 case ACT_RET_YIELD:
2785 s->current_rule = rule;
2786 rule_ret = HTTP_RULE_RES_YIELD;
2787 goto end;
2788 case ACT_RET_ERR:
2789 rule_ret = HTTP_RULE_RES_ERROR;
2790 goto end;
2791 case ACT_RET_DONE:
2792 rule_ret = HTTP_RULE_RES_DONE;
2793 goto end;
2794 case ACT_RET_DENY:
Christopher Fauletb58f62b2020-01-13 16:40:13 +01002795 if (txn->status == -1)
2796 txn->status = 403;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002797 rule_ret = HTTP_RULE_RES_DENY;
2798 goto end;
2799 case ACT_RET_ABRT:
2800 rule_ret = HTTP_RULE_RES_ABRT;
2801 goto end;
2802 case ACT_RET_INV:
2803 rule_ret = HTTP_RULE_RES_BADREQ;
2804 goto end;
2805 }
2806 continue; /* eval the next rule */
2807 }
2808
2809 /* If not action function defined, check for known actions */
Christopher Faulet3e964192018-10-24 11:39:23 +02002810 switch (rule->action) {
2811 case ACT_ACTION_ALLOW:
2812 rule_ret = HTTP_RULE_RES_STOP;
2813 goto end;
2814
2815 case ACT_ACTION_DENY:
Christopher Faulet5cb513a2020-05-13 17:56:56 +02002816 txn->status = rule->arg.http_reply->status;
2817 txn->http_reply = rule->arg.http_reply;
Christopher Faulet3e964192018-10-24 11:39:23 +02002818 rule_ret = HTTP_RULE_RES_DENY;
2819 goto end;
2820
2821 case ACT_HTTP_REQ_TARPIT:
2822 txn->flags |= TX_CLTARPIT;
Christopher Faulet5cb513a2020-05-13 17:56:56 +02002823 txn->status = rule->arg.http_reply->status;
2824 txn->http_reply = rule->arg.http_reply;
Christopher Faulet3e964192018-10-24 11:39:23 +02002825 rule_ret = HTTP_RULE_RES_DENY;
2826 goto end;
2827
Christopher Faulet3e964192018-10-24 11:39:23 +02002828 case ACT_HTTP_REDIR:
Christopher Faulet90d22a82020-03-06 11:18:39 +01002829 rule_ret = HTTP_RULE_RES_ABRT;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002830 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
Christopher Faulet3a26bee2019-12-16 12:47:40 +01002831 rule_ret = HTTP_RULE_RES_ERROR;
Christopher Faulet3e964192018-10-24 11:39:23 +02002832 goto end;
2833
2834 case ACT_HTTP_SET_NICE:
Christopher Faulet96bff762019-12-17 13:46:18 +01002835 s->task->nice = rule->arg.http.i;
Christopher Faulet3e964192018-10-24 11:39:23 +02002836 break;
2837
2838 case ACT_HTTP_SET_TOS:
Christopher Faulet96bff762019-12-17 13:46:18 +01002839 conn_set_tos(objt_conn(sess->origin), rule->arg.http.i);
Christopher Faulet3e964192018-10-24 11:39:23 +02002840 break;
2841
2842 case ACT_HTTP_SET_MARK:
Christopher Faulet96bff762019-12-17 13:46:18 +01002843 conn_set_mark(objt_conn(sess->origin), rule->arg.http.i);
Christopher Faulet3e964192018-10-24 11:39:23 +02002844 break;
2845
2846 case ACT_HTTP_SET_LOGL:
Christopher Faulet96bff762019-12-17 13:46:18 +01002847 s->logs.level = rule->arg.http.i;
Christopher Faulet3e964192018-10-24 11:39:23 +02002848 break;
2849
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002850 /* other flags exists, but normally, they never be matched. */
Christopher Faulet3e964192018-10-24 11:39:23 +02002851 default:
2852 break;
2853 }
2854 }
2855
2856 end:
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002857 /* if the ruleset evaluation is finished reset the strict mode */
Christopher Faulet46f95542019-12-20 10:07:22 +01002858 if (rule_ret != HTTP_RULE_RES_YIELD)
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002859 txn->req.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01002860
Christopher Faulet3e964192018-10-24 11:39:23 +02002861 /* we reached the end of the rules, nothing to report */
2862 return rule_ret;
2863}
2864
2865/* Executes the http-response rules <rules> for stream <s> and proxy <px>. It
2866 * returns one of 5 possible statuses: HTTP_RULE_RES_CONT, HTTP_RULE_RES_STOP,
2867 * HTTP_RULE_RES_DONE, HTTP_RULE_RES_YIELD, or HTTP_RULE_RES_BADREQ. If *CONT
2868 * is returned, the process can continue the evaluation of next rule list. If
2869 * *STOP or *DONE is returned, the process must stop the evaluation. If *BADREQ
2870 * is returned, it means the operation could not be processed and a server error
Christopher Fauleta53abad2020-05-13 08:12:22 +02002871 * must be returned. If *YIELD is returned, the caller must call again the
2872 * function with the same context.
Christopher Faulet3e964192018-10-24 11:39:23 +02002873 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002874static enum rule_result http_res_get_intercept_rule(struct proxy *px, struct list *rules,
2875 struct stream *s)
Christopher Faulet3e964192018-10-24 11:39:23 +02002876{
2877 struct session *sess = strm_sess(s);
2878 struct http_txn *txn = s->txn;
Christopher Faulet3e964192018-10-24 11:39:23 +02002879 struct act_rule *rule;
Christopher Faulet3e964192018-10-24 11:39:23 +02002880 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002881 int act_opts = 0;
Christopher Faulet3e964192018-10-24 11:39:23 +02002882
Christopher Faulet3e964192018-10-24 11:39:23 +02002883 /* If "the current_rule_list" match the executed rule list, we are in
2884 * resume condition. If a resume is needed it is always in the action
2885 * and never in the ACL or converters. In this case, we initialise the
2886 * current rule, and go to the action execution point.
2887 */
2888 if (s->current_rule) {
2889 rule = s->current_rule;
2890 s->current_rule = NULL;
2891 if (s->current_rule_list == rules)
2892 goto resume_execution;
2893 }
2894 s->current_rule_list = rules;
2895
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002896 /* start the ruleset evaluation in strict mode */
2897 txn->rsp.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01002898
Christopher Faulet3e964192018-10-24 11:39:23 +02002899 list_for_each_entry(rule, rules, list) {
2900 /* check optional condition */
2901 if (rule->cond) {
2902 int ret;
2903
2904 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
2905 ret = acl_pass(ret);
2906
2907 if (rule->cond->pol == ACL_COND_UNLESS)
2908 ret = !ret;
2909
2910 if (!ret) /* condition not matched */
2911 continue;
2912 }
2913
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002914 act_opts |= ACT_OPT_FIRST;
Christopher Faulet3e964192018-10-24 11:39:23 +02002915resume_execution:
Amaury Denoyelle03517732021-05-07 14:25:01 +02002916 if (rule->kw->flags & KWF_EXPERIMENTAL)
2917 mark_tainted(TAINTED_ACTION_EXP_EXECUTED);
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002918
2919 /* Always call the action function if defined */
2920 if (rule->action_ptr) {
2921 if ((s->req.flags & CF_READ_ERROR) ||
2922 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
2923 (px->options & PR_O_ABRT_CLOSE)))
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002924 act_opts |= ACT_OPT_FINAL;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002925
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002926 switch (rule->action_ptr(rule, px, sess, s, act_opts)) {
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002927 case ACT_RET_CONT:
2928 break;
2929 case ACT_RET_STOP:
2930 rule_ret = HTTP_RULE_RES_STOP;
2931 goto end;
2932 case ACT_RET_YIELD:
2933 s->current_rule = rule;
2934 rule_ret = HTTP_RULE_RES_YIELD;
2935 goto end;
2936 case ACT_RET_ERR:
2937 rule_ret = HTTP_RULE_RES_ERROR;
2938 goto end;
2939 case ACT_RET_DONE:
2940 rule_ret = HTTP_RULE_RES_DONE;
2941 goto end;
2942 case ACT_RET_DENY:
Christopher Fauletb58f62b2020-01-13 16:40:13 +01002943 if (txn->status == -1)
2944 txn->status = 502;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002945 rule_ret = HTTP_RULE_RES_DENY;
2946 goto end;
2947 case ACT_RET_ABRT:
2948 rule_ret = HTTP_RULE_RES_ABRT;
2949 goto end;
2950 case ACT_RET_INV:
2951 rule_ret = HTTP_RULE_RES_BADREQ;
2952 goto end;
2953 }
2954 continue; /* eval the next rule */
2955 }
2956
2957 /* If not action function defined, check for known actions */
Christopher Faulet3e964192018-10-24 11:39:23 +02002958 switch (rule->action) {
2959 case ACT_ACTION_ALLOW:
2960 rule_ret = HTTP_RULE_RES_STOP; /* "allow" rules are OK */
2961 goto end;
2962
2963 case ACT_ACTION_DENY:
Christopher Faulet5cb513a2020-05-13 17:56:56 +02002964 txn->status = rule->arg.http_reply->status;
2965 txn->http_reply = rule->arg.http_reply;
Christopher Faulet3a26bee2019-12-16 12:47:40 +01002966 rule_ret = HTTP_RULE_RES_DENY;
Christopher Faulet3e964192018-10-24 11:39:23 +02002967 goto end;
2968
2969 case ACT_HTTP_SET_NICE:
Christopher Faulet96bff762019-12-17 13:46:18 +01002970 s->task->nice = rule->arg.http.i;
Christopher Faulet3e964192018-10-24 11:39:23 +02002971 break;
2972
2973 case ACT_HTTP_SET_TOS:
Christopher Faulet96bff762019-12-17 13:46:18 +01002974 conn_set_tos(objt_conn(sess->origin), rule->arg.http.i);
Christopher Faulet3e964192018-10-24 11:39:23 +02002975 break;
2976
2977 case ACT_HTTP_SET_MARK:
Christopher Faulet96bff762019-12-17 13:46:18 +01002978 conn_set_mark(objt_conn(sess->origin), rule->arg.http.i);
Christopher Faulet3e964192018-10-24 11:39:23 +02002979 break;
2980
2981 case ACT_HTTP_SET_LOGL:
Christopher Faulet96bff762019-12-17 13:46:18 +01002982 s->logs.level = rule->arg.http.i;
Christopher Faulet3e964192018-10-24 11:39:23 +02002983 break;
2984
Christopher Faulet3e964192018-10-24 11:39:23 +02002985 case ACT_HTTP_REDIR:
Christopher Faulet49c2a702020-03-06 15:44:37 +01002986 rule_ret = HTTP_RULE_RES_ABRT;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002987 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
Christopher Faulet3a26bee2019-12-16 12:47:40 +01002988 rule_ret = HTTP_RULE_RES_ERROR;
Christopher Faulet3e964192018-10-24 11:39:23 +02002989 goto end;
2990
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002991 /* other flags exists, but normally, they never be matched. */
Christopher Faulet3e964192018-10-24 11:39:23 +02002992 default:
2993 break;
2994 }
2995 }
2996
2997 end:
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002998 /* if the ruleset evaluation is finished reset the strict mode */
Christopher Faulet46f95542019-12-20 10:07:22 +01002999 if (rule_ret != HTTP_RULE_RES_YIELD)
Christopher Faulet1aea50e2020-01-17 16:03:53 +01003000 txn->rsp.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01003001
Christopher Faulet3e964192018-10-24 11:39:23 +02003002 /* we reached the end of the rules, nothing to report */
3003 return rule_ret;
3004}
3005
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01003006/* Executes backend and frontend http-after-response rules for the stream <s>,
3007 * in that order. it return 1 on success and 0 on error. It is the caller
3008 * responsibility to catch error or ignore it. If it catches it, this function
3009 * may be called a second time, for the internal error.
3010 */
3011int http_eval_after_res_rules(struct stream *s)
3012{
3013 struct session *sess = s->sess;
3014 enum rule_result ret = HTTP_RULE_RES_CONT;
3015
Christopher Faulet507479b2020-05-15 12:29:46 +02003016 /* Eval after-response ruleset only if the reply is not const */
3017 if (s->txn->flags & TX_CONST_REPLY)
3018 goto end;
3019
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01003020 /* prune the request variables if not already done and swap to the response variables. */
3021 if (s->vars_reqres.scope != SCOPE_RES) {
3022 if (!LIST_ISEMPTY(&s->vars_reqres.head))
3023 vars_prune(&s->vars_reqres, s->sess, s);
3024 vars_init(&s->vars_reqres, SCOPE_RES);
3025 }
3026
3027 ret = http_res_get_intercept_rule(s->be, &s->be->http_after_res_rules, s);
3028 if ((ret == HTTP_RULE_RES_CONT || ret == HTTP_RULE_RES_STOP) && sess->fe != s->be)
3029 ret = http_res_get_intercept_rule(sess->fe, &sess->fe->http_after_res_rules, s);
3030
Christopher Faulet507479b2020-05-15 12:29:46 +02003031 end:
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01003032 /* All other codes than CONTINUE, STOP or DONE are forbidden */
3033 return (ret == HTTP_RULE_RES_CONT || ret == HTTP_RULE_RES_STOP || ret == HTTP_RULE_RES_DONE);
3034}
3035
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003036/*
3037 * Manage client-side cookie. It can impact performance by about 2% so it is
3038 * desirable to call it only when needed. This code is quite complex because
3039 * of the multiple very crappy and ambiguous syntaxes we have to support. it
3040 * highly recommended not to touch this part without a good reason !
3041 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003042static void http_manage_client_side_cookies(struct stream *s, struct channel *req)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003043{
3044 struct session *sess = s->sess;
3045 struct http_txn *txn = s->txn;
3046 struct htx *htx;
3047 struct http_hdr_ctx ctx;
3048 char *hdr_beg, *hdr_end, *del_from;
3049 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
3050 int preserve_hdr;
3051
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003052 htx = htxbuf(&req->buf);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003053 ctx.blk = NULL;
3054 while (http_find_header(htx, ist("Cookie"), &ctx, 1)) {
Olivier Houchardf0f42382019-07-22 17:43:46 +02003055 int is_first = 1;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003056 del_from = NULL; /* nothing to be deleted */
3057 preserve_hdr = 0; /* assume we may kill the whole header */
3058
3059 /* Now look for cookies. Conforming to RFC2109, we have to support
3060 * attributes whose name begin with a '$', and associate them with
3061 * the right cookie, if we want to delete this cookie.
3062 * So there are 3 cases for each cookie read :
3063 * 1) it's a special attribute, beginning with a '$' : ignore it.
3064 * 2) it's a server id cookie that we *MAY* want to delete : save
3065 * some pointers on it (last semi-colon, beginning of cookie...)
3066 * 3) it's an application cookie : we *MAY* have to delete a previous
3067 * "special" cookie.
3068 * At the end of loop, if a "special" cookie remains, we may have to
3069 * remove it. If no application cookie persists in the header, we
3070 * *MUST* delete it.
3071 *
3072 * Note: RFC2965 is unclear about the processing of spaces around
3073 * the equal sign in the ATTR=VALUE form. A careful inspection of
3074 * the RFC explicitly allows spaces before it, and not within the
3075 * tokens (attrs or values). An inspection of RFC2109 allows that
3076 * too but section 10.1.3 lets one think that spaces may be allowed
3077 * after the equal sign too, resulting in some (rare) buggy
3078 * implementations trying to do that. So let's do what servers do.
3079 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
3080 * allowed quoted strings in values, with any possible character
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003081 * after a backslash, including control chars and delimiters, which
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003082 * causes parsing to become ambiguous. Browsers also allow spaces
3083 * within values even without quotes.
3084 *
3085 * We have to keep multiple pointers in order to support cookie
3086 * removal at the beginning, middle or end of header without
3087 * corrupting the header. All of these headers are valid :
3088 *
3089 * hdr_beg hdr_end
3090 * | |
3091 * v |
3092 * NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3 |
3093 * NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3 v
3094 * NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3
3095 * | | | | | | |
3096 * | | | | | | |
3097 * | | | | | | +--> next
3098 * | | | | | +----> val_end
3099 * | | | | +-----------> val_beg
3100 * | | | +--------------> equal
3101 * | | +----------------> att_end
3102 * | +---------------------> att_beg
3103 * +--------------------------> prev
3104 *
3105 */
3106 hdr_beg = ctx.value.ptr;
3107 hdr_end = hdr_beg + ctx.value.len;
3108 for (prev = hdr_beg; prev < hdr_end; prev = next) {
3109 /* Iterate through all cookies on this line */
3110
3111 /* find att_beg */
3112 att_beg = prev;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003113 if (!is_first)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003114 att_beg++;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003115 is_first = 0;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003116
3117 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
3118 att_beg++;
3119
3120 /* find att_end : this is the first character after the last non
3121 * space before the equal. It may be equal to hdr_end.
3122 */
3123 equal = att_end = att_beg;
3124 while (equal < hdr_end) {
3125 if (*equal == '=' || *equal == ',' || *equal == ';')
3126 break;
3127 if (HTTP_IS_SPHT(*equal++))
3128 continue;
3129 att_end = equal;
3130 }
3131
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003132 /* here, <equal> points to '=', a delimiter or the end. <att_end>
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003133 * is between <att_beg> and <equal>, both may be identical.
3134 */
3135 /* look for end of cookie if there is an equal sign */
3136 if (equal < hdr_end && *equal == '=') {
3137 /* look for the beginning of the value */
3138 val_beg = equal + 1;
3139 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
3140 val_beg++;
3141
3142 /* find the end of the value, respecting quotes */
3143 next = http_find_cookie_value_end(val_beg, hdr_end);
3144
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003145 /* make val_end point to the first white space or delimiter after the value */
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003146 val_end = next;
3147 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
3148 val_end--;
3149 }
3150 else
3151 val_beg = val_end = next = equal;
3152
3153 /* We have nothing to do with attributes beginning with
3154 * '$'. However, they will automatically be removed if a
3155 * header before them is removed, since they're supposed
3156 * to be linked together.
3157 */
3158 if (*att_beg == '$')
3159 continue;
3160
3161 /* Ignore cookies with no equal sign */
3162 if (equal == next) {
3163 /* This is not our cookie, so we must preserve it. But if we already
3164 * scheduled another cookie for removal, we cannot remove the
3165 * complete header, but we can remove the previous block itself.
3166 */
3167 preserve_hdr = 1;
3168 if (del_from != NULL) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003169 int delta = http_del_hdr_value(hdr_beg, hdr_end, &del_from, prev);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003170 val_end += delta;
3171 next += delta;
3172 hdr_end += delta;
3173 prev = del_from;
3174 del_from = NULL;
3175 }
3176 continue;
3177 }
3178
3179 /* if there are spaces around the equal sign, we need to
3180 * strip them otherwise we'll get trouble for cookie captures,
3181 * or even for rewrites. Since this happens extremely rarely,
3182 * it does not hurt performance.
3183 */
3184 if (unlikely(att_end != equal || val_beg > equal + 1)) {
3185 int stripped_before = 0;
3186 int stripped_after = 0;
3187
3188 if (att_end != equal) {
3189 memmove(att_end, equal, hdr_end - equal);
3190 stripped_before = (att_end - equal);
3191 equal += stripped_before;
3192 val_beg += stripped_before;
3193 }
3194
3195 if (val_beg > equal + 1) {
3196 memmove(equal + 1, val_beg, hdr_end + stripped_before - val_beg);
3197 stripped_after = (equal + 1) - val_beg;
3198 val_beg += stripped_after;
3199 stripped_before += stripped_after;
3200 }
3201
3202 val_end += stripped_before;
3203 next += stripped_before;
3204 hdr_end += stripped_before;
3205 }
3206 /* now everything is as on the diagram above */
3207
3208 /* First, let's see if we want to capture this cookie. We check
3209 * that we don't already have a client side cookie, because we
3210 * can only capture one. Also as an optimisation, we ignore
3211 * cookies shorter than the declared name.
3212 */
3213 if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL &&
3214 (val_end - att_beg >= sess->fe->capture_namelen) &&
3215 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
3216 int log_len = val_end - att_beg;
3217
3218 if ((txn->cli_cookie = pool_alloc(pool_head_capture)) == NULL) {
3219 ha_alert("HTTP logging : out of memory.\n");
3220 } else {
3221 if (log_len > sess->fe->capture_len)
3222 log_len = sess->fe->capture_len;
3223 memcpy(txn->cli_cookie, att_beg, log_len);
3224 txn->cli_cookie[log_len] = 0;
3225 }
3226 }
3227
3228 /* Persistence cookies in passive, rewrite or insert mode have the
3229 * following form :
3230 *
3231 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
3232 *
3233 * For cookies in prefix mode, the form is :
3234 *
3235 * Cookie: NAME=SRV~VALUE
3236 */
3237 if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
3238 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
3239 struct server *srv = s->be->srv;
3240 char *delim;
3241
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003242 /* if we're in cookie prefix mode, we'll search the delimiter so that we
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003243 * have the server ID between val_beg and delim, and the original cookie between
3244 * delim+1 and val_end. Otherwise, delim==val_end :
3245 *
3246 * hdr_beg
3247 * |
3248 * v
3249 * NAME=SRV; # in all but prefix modes
3250 * NAME=SRV~OPAQUE ; # in prefix mode
3251 * || || | |+-> next
3252 * || || | +--> val_end
3253 * || || +---------> delim
3254 * || |+------------> val_beg
3255 * || +-------------> att_end = equal
3256 * |+-----------------> att_beg
3257 * +------------------> prev
3258 *
3259 */
3260 if (s->be->ck_opts & PR_CK_PFX) {
3261 for (delim = val_beg; delim < val_end; delim++)
3262 if (*delim == COOKIE_DELIM)
3263 break;
3264 }
3265 else {
3266 char *vbar1;
3267 delim = val_end;
3268 /* Now check if the cookie contains a date field, which would
3269 * appear after a vertical bar ('|') just after the server name
3270 * and before the delimiter.
3271 */
3272 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
3273 if (vbar1) {
3274 /* OK, so left of the bar is the server's cookie and
3275 * right is the last seen date. It is a base64 encoded
3276 * 30-bit value representing the UNIX date since the
3277 * epoch in 4-second quantities.
3278 */
3279 int val;
3280 delim = vbar1++;
3281 if (val_end - vbar1 >= 5) {
3282 val = b64tos30(vbar1);
3283 if (val > 0)
3284 txn->cookie_last_date = val << 2;
3285 }
3286 /* look for a second vertical bar */
3287 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
3288 if (vbar1 && (val_end - vbar1 > 5)) {
3289 val = b64tos30(vbar1 + 1);
3290 if (val > 0)
3291 txn->cookie_first_date = val << 2;
3292 }
3293 }
3294 }
3295
3296 /* if the cookie has an expiration date and the proxy wants to check
3297 * it, then we do that now. We first check if the cookie is too old,
3298 * then only if it has expired. We detect strict overflow because the
3299 * time resolution here is not great (4 seconds). Cookies with dates
3300 * in the future are ignored if their offset is beyond one day. This
3301 * allows an admin to fix timezone issues without expiring everyone
3302 * and at the same time avoids keeping unwanted side effects for too
3303 * long.
3304 */
3305 if (txn->cookie_first_date && s->be->cookie_maxlife &&
3306 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) ||
3307 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
3308 txn->flags &= ~TX_CK_MASK;
3309 txn->flags |= TX_CK_OLD;
3310 delim = val_beg; // let's pretend we have not found the cookie
3311 txn->cookie_first_date = 0;
3312 txn->cookie_last_date = 0;
3313 }
3314 else if (txn->cookie_last_date && s->be->cookie_maxidle &&
3315 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) ||
3316 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
3317 txn->flags &= ~TX_CK_MASK;
3318 txn->flags |= TX_CK_EXPIRED;
3319 delim = val_beg; // let's pretend we have not found the cookie
3320 txn->cookie_first_date = 0;
3321 txn->cookie_last_date = 0;
3322 }
3323
3324 /* Here, we'll look for the first running server which supports the cookie.
3325 * This allows to share a same cookie between several servers, for example
3326 * to dedicate backup servers to specific servers only.
3327 * However, to prevent clients from sticking to cookie-less backup server
3328 * when they have incidentely learned an empty cookie, we simply ignore
3329 * empty cookies and mark them as invalid.
3330 * The same behaviour is applied when persistence must be ignored.
3331 */
3332 if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
3333 srv = NULL;
3334
3335 while (srv) {
3336 if (srv->cookie && (srv->cklen == delim - val_beg) &&
3337 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
3338 if ((srv->cur_state != SRV_ST_STOPPED) ||
3339 (s->be->options & PR_O_PERSIST) ||
3340 (s->flags & SF_FORCE_PRST)) {
3341 /* we found the server and we can use it */
3342 txn->flags &= ~TX_CK_MASK;
3343 txn->flags |= (srv->cur_state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
3344 s->flags |= SF_DIRECT | SF_ASSIGNED;
3345 s->target = &srv->obj_type;
3346 break;
3347 } else {
3348 /* we found a server, but it's down,
3349 * mark it as such and go on in case
3350 * another one is available.
3351 */
3352 txn->flags &= ~TX_CK_MASK;
3353 txn->flags |= TX_CK_DOWN;
3354 }
3355 }
3356 srv = srv->next;
3357 }
3358
3359 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
3360 /* no server matched this cookie or we deliberately skipped it */
3361 txn->flags &= ~TX_CK_MASK;
3362 if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
3363 txn->flags |= TX_CK_UNUSED;
3364 else
3365 txn->flags |= TX_CK_INVALID;
3366 }
3367
3368 /* depending on the cookie mode, we may have to either :
3369 * - delete the complete cookie if we're in insert+indirect mode, so that
3370 * the server never sees it ;
3371 * - remove the server id from the cookie value, and tag the cookie as an
Joseph Herlante9d5c722018-11-25 11:00:25 -08003372 * application cookie so that it does not get accidentally removed later,
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003373 * if we're in cookie prefix mode
3374 */
3375 if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
3376 int delta; /* negative */
3377
3378 memmove(val_beg, delim + 1, hdr_end - (delim + 1));
3379 delta = val_beg - (delim + 1);
3380 val_end += delta;
3381 next += delta;
3382 hdr_end += delta;
3383 del_from = NULL;
3384 preserve_hdr = 1; /* we want to keep this cookie */
3385 }
3386 else if (del_from == NULL &&
3387 (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
3388 del_from = prev;
3389 }
3390 }
3391 else {
3392 /* This is not our cookie, so we must preserve it. But if we already
3393 * scheduled another cookie for removal, we cannot remove the
3394 * complete header, but we can remove the previous block itself.
3395 */
3396 preserve_hdr = 1;
3397
3398 if (del_from != NULL) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003399 int delta = http_del_hdr_value(hdr_beg, hdr_end, &del_from, prev);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003400 if (att_beg >= del_from)
3401 att_beg += delta;
3402 if (att_end >= del_from)
3403 att_end += delta;
3404 val_beg += delta;
3405 val_end += delta;
3406 next += delta;
3407 hdr_end += delta;
3408 prev = del_from;
3409 del_from = NULL;
3410 }
3411 }
3412
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003413 } /* for each cookie */
3414
3415
3416 /* There are no more cookies on this line.
3417 * We may still have one (or several) marked for deletion at the
3418 * end of the line. We must do this now in two ways :
3419 * - if some cookies must be preserved, we only delete from the
3420 * mark to the end of line ;
3421 * - if nothing needs to be preserved, simply delete the whole header
3422 */
3423 if (del_from) {
3424 hdr_end = (preserve_hdr ? del_from : hdr_beg);
3425 }
3426 if ((hdr_end - hdr_beg) != ctx.value.len) {
Christopher Faulet3e2638e2019-06-18 09:49:16 +02003427 if (hdr_beg != hdr_end)
3428 htx_change_blk_value_len(htx, ctx.blk, hdr_end - hdr_beg);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003429 else
3430 http_remove_header(htx, &ctx);
3431 }
3432 } /* for each "Cookie header */
3433}
3434
3435/*
3436 * Manage server-side cookies. It can impact performance by about 2% so it is
3437 * desirable to call it only when needed. This function is also used when we
3438 * just need to know if there is a cookie (eg: for check-cache).
3439 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003440static void http_manage_server_side_cookies(struct stream *s, struct channel *res)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003441{
3442 struct session *sess = s->sess;
3443 struct http_txn *txn = s->txn;
3444 struct htx *htx;
3445 struct http_hdr_ctx ctx;
3446 struct server *srv;
3447 char *hdr_beg, *hdr_end;
3448 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau6f7a02a2019-04-15 21:49:49 +02003449 int is_cookie2 = 0;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003450
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003451 htx = htxbuf(&res->buf);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003452
3453 ctx.blk = NULL;
3454 while (1) {
Olivier Houchardf0f42382019-07-22 17:43:46 +02003455 int is_first = 1;
3456
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003457 if (!http_find_header(htx, ist("Set-Cookie"), &ctx, 1)) {
3458 if (!http_find_header(htx, ist("Set-Cookie2"), &ctx, 1))
3459 break;
3460 is_cookie2 = 1;
3461 }
3462
3463 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
3464 * <prev> points to the colon.
3465 */
3466 txn->flags |= TX_SCK_PRESENT;
3467
3468 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
3469 * check-cache is enabled) and we are not interested in checking
3470 * them. Warning, the cookie capture is declared in the frontend.
3471 */
3472 if (s->be->cookie_name == NULL && sess->fe->capture_name == NULL)
3473 break;
3474
3475 /* OK so now we know we have to process this response cookie.
3476 * The format of the Set-Cookie header is slightly different
3477 * from the format of the Cookie header in that it does not
3478 * support the comma as a cookie delimiter (thus the header
3479 * cannot be folded) because the Expires attribute described in
3480 * the original Netscape's spec may contain an unquoted date
3481 * with a comma inside. We have to live with this because
3482 * many browsers don't support Max-Age and some browsers don't
3483 * support quoted strings. However the Set-Cookie2 header is
3484 * clean.
3485 *
3486 * We have to keep multiple pointers in order to support cookie
3487 * removal at the beginning, middle or end of header without
3488 * corrupting the header (in case of set-cookie2). A special
3489 * pointer, <scav> points to the beginning of the set-cookie-av
3490 * fields after the first semi-colon. The <next> pointer points
3491 * either to the end of line (set-cookie) or next unquoted comma
3492 * (set-cookie2). All of these headers are valid :
3493 *
3494 * hdr_beg hdr_end
3495 * | |
3496 * v |
3497 * NAME1 = VALUE 1 ; Secure; Path="/" |
3498 * NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT v
3499 * NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT
3500 * NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard
3501 * | | | | | | | |
3502 * | | | | | | | +-> next
3503 * | | | | | | +------------> scav
3504 * | | | | | +--------------> val_end
3505 * | | | | +--------------------> val_beg
3506 * | | | +----------------------> equal
3507 * | | +------------------------> att_end
3508 * | +----------------------------> att_beg
3509 * +------------------------------> prev
3510 * -------------------------------> hdr_beg
3511 */
3512 hdr_beg = ctx.value.ptr;
3513 hdr_end = hdr_beg + ctx.value.len;
3514 for (prev = hdr_beg; prev < hdr_end; prev = next) {
3515
3516 /* Iterate through all cookies on this line */
3517
3518 /* find att_beg */
3519 att_beg = prev;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003520 if (!is_first)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003521 att_beg++;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003522 is_first = 0;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003523
3524 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
3525 att_beg++;
3526
3527 /* find att_end : this is the first character after the last non
3528 * space before the equal. It may be equal to hdr_end.
3529 */
3530 equal = att_end = att_beg;
3531
3532 while (equal < hdr_end) {
3533 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
3534 break;
3535 if (HTTP_IS_SPHT(*equal++))
3536 continue;
3537 att_end = equal;
3538 }
3539
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003540 /* here, <equal> points to '=', a delimiter or the end. <att_end>
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003541 * is between <att_beg> and <equal>, both may be identical.
3542 */
3543
3544 /* look for end of cookie if there is an equal sign */
3545 if (equal < hdr_end && *equal == '=') {
3546 /* look for the beginning of the value */
3547 val_beg = equal + 1;
3548 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
3549 val_beg++;
3550
3551 /* find the end of the value, respecting quotes */
3552 next = http_find_cookie_value_end(val_beg, hdr_end);
3553
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003554 /* make val_end point to the first white space or delimiter after the value */
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003555 val_end = next;
3556 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
3557 val_end--;
3558 }
3559 else {
3560 /* <equal> points to next comma, semi-colon or EOL */
3561 val_beg = val_end = next = equal;
3562 }
3563
3564 if (next < hdr_end) {
3565 /* Set-Cookie2 supports multiple cookies, and <next> points to
3566 * a colon or semi-colon before the end. So skip all attr-value
3567 * pairs and look for the next comma. For Set-Cookie, since
3568 * commas are permitted in values, skip to the end.
3569 */
3570 if (is_cookie2)
3571 next = http_find_hdr_value_end(next, hdr_end);
3572 else
3573 next = hdr_end;
3574 }
3575
3576 /* Now everything is as on the diagram above */
3577
3578 /* Ignore cookies with no equal sign */
3579 if (equal == val_end)
3580 continue;
3581
3582 /* If there are spaces around the equal sign, we need to
3583 * strip them otherwise we'll get trouble for cookie captures,
3584 * or even for rewrites. Since this happens extremely rarely,
3585 * it does not hurt performance.
3586 */
3587 if (unlikely(att_end != equal || val_beg > equal + 1)) {
3588 int stripped_before = 0;
3589 int stripped_after = 0;
3590
3591 if (att_end != equal) {
3592 memmove(att_end, equal, hdr_end - equal);
3593 stripped_before = (att_end - equal);
3594 equal += stripped_before;
3595 val_beg += stripped_before;
3596 }
3597
3598 if (val_beg > equal + 1) {
3599 memmove(equal + 1, val_beg, hdr_end + stripped_before - val_beg);
3600 stripped_after = (equal + 1) - val_beg;
3601 val_beg += stripped_after;
3602 stripped_before += stripped_after;
3603 }
3604
3605 val_end += stripped_before;
3606 next += stripped_before;
3607 hdr_end += stripped_before;
3608
Christopher Faulet3e2638e2019-06-18 09:49:16 +02003609 htx_change_blk_value_len(htx, ctx.blk, hdr_end - hdr_beg);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003610 ctx.value.len = hdr_end - hdr_beg;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003611 }
3612
3613 /* First, let's see if we want to capture this cookie. We check
3614 * that we don't already have a server side cookie, because we
3615 * can only capture one. Also as an optimisation, we ignore
3616 * cookies shorter than the declared name.
3617 */
3618 if (sess->fe->capture_name != NULL &&
3619 txn->srv_cookie == NULL &&
3620 (val_end - att_beg >= sess->fe->capture_namelen) &&
3621 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
3622 int log_len = val_end - att_beg;
3623 if ((txn->srv_cookie = pool_alloc(pool_head_capture)) == NULL) {
3624 ha_alert("HTTP logging : out of memory.\n");
3625 }
3626 else {
3627 if (log_len > sess->fe->capture_len)
3628 log_len = sess->fe->capture_len;
3629 memcpy(txn->srv_cookie, att_beg, log_len);
3630 txn->srv_cookie[log_len] = 0;
3631 }
3632 }
3633
3634 srv = objt_server(s->target);
3635 /* now check if we need to process it for persistence */
3636 if (!(s->flags & SF_IGNORE_PRST) &&
3637 (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
3638 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
3639 /* assume passive cookie by default */
3640 txn->flags &= ~TX_SCK_MASK;
3641 txn->flags |= TX_SCK_FOUND;
3642
3643 /* If the cookie is in insert mode on a known server, we'll delete
3644 * this occurrence because we'll insert another one later.
3645 * We'll delete it too if the "indirect" option is set and we're in
3646 * a direct access.
3647 */
3648 if (s->be->ck_opts & PR_CK_PSV) {
3649 /* The "preserve" flag was set, we don't want to touch the
3650 * server's cookie.
3651 */
3652 }
3653 else if ((srv && (s->be->ck_opts & PR_CK_INS)) ||
3654 ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) {
3655 /* this cookie must be deleted */
3656 if (prev == hdr_beg && next == hdr_end) {
3657 /* whole header */
3658 http_remove_header(htx, &ctx);
3659 /* note: while both invalid now, <next> and <hdr_end>
3660 * are still equal, so the for() will stop as expected.
3661 */
3662 } else {
3663 /* just remove the value */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003664 int delta = http_del_hdr_value(hdr_beg, hdr_end, &prev, next);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003665 next = prev;
3666 hdr_end += delta;
3667 }
3668 txn->flags &= ~TX_SCK_MASK;
3669 txn->flags |= TX_SCK_DELETED;
3670 /* and go on with next cookie */
3671 }
3672 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) {
3673 /* replace bytes val_beg->val_end with the cookie name associated
3674 * with this server since we know it.
3675 */
3676 int sliding, delta;
3677
3678 ctx.value = ist2(val_beg, val_end - val_beg);
3679 ctx.lws_before = ctx.lws_after = 0;
3680 http_replace_header_value(htx, &ctx, ist2(srv->cookie, srv->cklen));
3681 delta = srv->cklen - (val_end - val_beg);
3682 sliding = (ctx.value.ptr - val_beg);
3683 hdr_beg += sliding;
3684 val_beg += sliding;
3685 next += sliding + delta;
3686 hdr_end += sliding + delta;
3687
3688 txn->flags &= ~TX_SCK_MASK;
3689 txn->flags |= TX_SCK_REPLACED;
3690 }
3691 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) {
3692 /* insert the cookie name associated with this server
3693 * before existing cookie, and insert a delimiter between them..
3694 */
3695 int sliding, delta;
3696 ctx.value = ist2(val_beg, 0);
3697 ctx.lws_before = ctx.lws_after = 0;
3698 http_replace_header_value(htx, &ctx, ist2(srv->cookie, srv->cklen + 1));
3699 delta = srv->cklen + 1;
3700 sliding = (ctx.value.ptr - val_beg);
3701 hdr_beg += sliding;
3702 val_beg += sliding;
3703 next += sliding + delta;
3704 hdr_end += sliding + delta;
3705
3706 val_beg[srv->cklen] = COOKIE_DELIM;
3707 txn->flags &= ~TX_SCK_MASK;
3708 txn->flags |= TX_SCK_REPLACED;
3709 }
3710 }
3711 /* that's done for this cookie, check the next one on the same
3712 * line when next != hdr_end (only if is_cookie2).
3713 */
3714 }
3715 }
3716}
3717
Christopher Faulet25a02f62018-10-24 12:00:25 +02003718/*
3719 * Parses the Cache-Control and Pragma request header fields to determine if
3720 * the request may be served from the cache and/or if it is cacheable. Updates
3721 * s->txn->flags.
3722 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003723void http_check_request_for_cacheability(struct stream *s, struct channel *req)
Christopher Faulet25a02f62018-10-24 12:00:25 +02003724{
3725 struct http_txn *txn = s->txn;
3726 struct htx *htx;
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003727 struct http_hdr_ctx ctx = { .blk = NULL };
3728 int pragma_found, cc_found;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003729
3730 if ((txn->flags & (TX_CACHEABLE|TX_CACHE_IGNORE)) == TX_CACHE_IGNORE)
3731 return; /* nothing more to do here */
3732
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003733 htx = htxbuf(&req->buf);
Christopher Faulet25a02f62018-10-24 12:00:25 +02003734 pragma_found = cc_found = 0;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003735
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003736 /* Check "pragma" header for HTTP/1.0 compatibility. */
3737 if (http_find_header(htx, ist("pragma"), &ctx, 1)) {
3738 if (isteqi(ctx.value, ist("no-cache"))) {
3739 pragma_found = 1;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003740 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003741 }
Christopher Faulet25a02f62018-10-24 12:00:25 +02003742
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003743 ctx.blk = NULL;
3744 /* Don't use the cache and don't try to store if we found the
3745 * Authorization header */
3746 if (http_find_header(htx, ist("authorization"), &ctx, 1)) {
3747 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3748 txn->flags |= TX_CACHE_IGNORE;
3749 }
Christopher Faulet25a02f62018-10-24 12:00:25 +02003750
Christopher Faulet25a02f62018-10-24 12:00:25 +02003751
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003752 /* Look for "cache-control" header and iterate over all the values
3753 * until we find one that specifies that caching is possible or not. */
3754 ctx.blk = NULL;
3755 while (http_find_header(htx, ist("cache-control"), &ctx, 0)) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003756 cc_found = 1;
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003757 /* We don't check the values after max-age, max-stale nor min-fresh,
3758 * we simply don't use the cache when they're specified. */
3759 if (istmatchi(ctx.value, ist("max-age")) ||
3760 istmatchi(ctx.value, ist("no-cache")) ||
3761 istmatchi(ctx.value, ist("max-stale")) ||
3762 istmatchi(ctx.value, ist("min-fresh"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003763 txn->flags |= TX_CACHE_IGNORE;
3764 continue;
3765 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003766 if (istmatchi(ctx.value, ist("no-store"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003767 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3768 continue;
3769 }
3770 }
3771
3772 /* RFC7234#5.4:
3773 * When the Cache-Control header field is also present and
3774 * understood in a request, Pragma is ignored.
3775 * When the Cache-Control header field is not present in a
3776 * request, caches MUST consider the no-cache request
3777 * pragma-directive as having the same effect as if
3778 * "Cache-Control: no-cache" were present.
3779 */
3780 if (!cc_found && pragma_found)
3781 txn->flags |= TX_CACHE_IGNORE;
3782}
3783
3784/*
3785 * Check if response is cacheable or not. Updates s->txn->flags.
3786 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003787void http_check_response_for_cacheability(struct stream *s, struct channel *res)
Christopher Faulet25a02f62018-10-24 12:00:25 +02003788{
3789 struct http_txn *txn = s->txn;
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003790 struct http_hdr_ctx ctx = { .blk = NULL };
Christopher Faulet25a02f62018-10-24 12:00:25 +02003791 struct htx *htx;
Remi Tricot-Le Bretoncc9bf2e2020-11-12 11:14:41 +01003792 int has_freshness_info = 0;
3793 int has_validator = 0;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003794
3795 if (txn->status < 200) {
3796 /* do not try to cache interim responses! */
3797 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3798 return;
3799 }
3800
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003801 htx = htxbuf(&res->buf);
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003802 /* Check "pragma" header for HTTP/1.0 compatibility. */
3803 if (http_find_header(htx, ist("pragma"), &ctx, 1)) {
3804 if (isteqi(ctx.value, ist("no-cache"))) {
3805 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3806 return;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003807 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003808 }
Christopher Faulet25a02f62018-10-24 12:00:25 +02003809
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003810 /* Look for "cache-control" header and iterate over all the values
3811 * until we find one that specifies that caching is possible or not. */
3812 ctx.blk = NULL;
3813 while (http_find_header(htx, ist("cache-control"), &ctx, 0)) {
3814 if (isteqi(ctx.value, ist("public"))) {
3815 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003816 continue;
3817 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003818 if (isteqi(ctx.value, ist("private")) ||
3819 isteqi(ctx.value, ist("no-cache")) ||
3820 isteqi(ctx.value, ist("no-store")) ||
3821 isteqi(ctx.value, ist("max-age=0")) ||
3822 isteqi(ctx.value, ist("s-maxage=0"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003823 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003824 continue;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003825 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003826 /* We might have a no-cache="set-cookie" form. */
3827 if (istmatchi(ctx.value, ist("no-cache=\"set-cookie"))) {
3828 txn->flags &= ~TX_CACHE_COOK;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003829 continue;
3830 }
Remi Tricot-Le Bretoncc9bf2e2020-11-12 11:14:41 +01003831
3832 if (istmatchi(ctx.value, ist("s-maxage")) ||
3833 istmatchi(ctx.value, ist("max-age"))) {
3834 has_freshness_info = 1;
3835 continue;
3836 }
3837 }
3838
3839 /* If no freshness information could be found in Cache-Control values,
3840 * look for an Expires header. */
3841 if (!has_freshness_info) {
3842 ctx.blk = NULL;
3843 has_freshness_info = http_find_header(htx, ist("expires"), &ctx, 0);
Christopher Faulet25a02f62018-10-24 12:00:25 +02003844 }
Remi Tricot-Le Bretoncc9bf2e2020-11-12 11:14:41 +01003845
3846 /* If no freshness information could be found in Cache-Control or Expires
3847 * values, look for an explicit validator. */
3848 if (!has_freshness_info) {
3849 ctx.blk = NULL;
3850 has_validator = 1;
3851 if (!http_find_header(htx, ist("etag"), &ctx, 0)) {
3852 ctx.blk = NULL;
3853 if (!http_find_header(htx, ist("last-modified"), &ctx, 0))
3854 has_validator = 0;
3855 }
3856 }
3857
3858 /* We won't store an entry that has neither a cache validator nor an
3859 * explicit expiration time, as suggested in RFC 7234#3. */
3860 if (!has_freshness_info && !has_validator)
3861 txn->flags |= TX_CACHE_IGNORE;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003862}
3863
Christopher Faulet377c5a52018-10-24 21:21:30 +02003864/*
3865 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
3866 * for the current backend.
3867 *
3868 * It is assumed that the request is either a HEAD, GET, or POST and that the
3869 * uri_auth field is valid.
3870 *
3871 * Returns 1 if stats should be provided, otherwise 0.
3872 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003873static int http_stats_check_uri(struct stream *s, struct http_txn *txn, struct proxy *backend)
Christopher Faulet377c5a52018-10-24 21:21:30 +02003874{
3875 struct uri_auth *uri_auth = backend->uri_auth;
3876 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003877 struct htx_sl *sl;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003878 struct ist uri;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003879
3880 if (!uri_auth)
3881 return 0;
3882
3883 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
3884 return 0;
3885
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003886 htx = htxbuf(&s->req.buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02003887 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003888 uri = htx_sl_req_uri(sl);
Willy Tarreau1eb3b482019-10-31 15:50:28 +01003889 if (*uri_auth->uri_prefix == '/')
3890 uri = http_get_path(uri);
Christopher Faulet377c5a52018-10-24 21:21:30 +02003891
3892 /* check URI size */
3893 if (uri_auth->uri_len > uri.len)
3894 return 0;
3895
3896 if (memcmp(uri.ptr, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
3897 return 0;
3898
3899 return 1;
3900}
3901
3902/* This function prepares an applet to handle the stats. It can deal with the
3903 * "100-continue" expectation, check that admin rules are met for POST requests,
3904 * and program a response message if something was unexpected. It cannot fail
3905 * and always relies on the stats applet to complete the job. It does not touch
3906 * analysers nor counters, which are left to the caller. It does not touch
3907 * s->target which is supposed to already point to the stats applet. The caller
3908 * is expected to have already assigned an appctx to the stream.
3909 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003910static int http_handle_stats(struct stream *s, struct channel *req)
Christopher Faulet377c5a52018-10-24 21:21:30 +02003911{
3912 struct stats_admin_rule *stats_admin_rule;
3913 struct stream_interface *si = &s->si[1];
3914 struct session *sess = s->sess;
3915 struct http_txn *txn = s->txn;
3916 struct http_msg *msg = &txn->req;
3917 struct uri_auth *uri_auth = s->be->uri_auth;
3918 const char *h, *lookup, *end;
3919 struct appctx *appctx;
3920 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003921 struct htx_sl *sl;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003922
3923 appctx = si_appctx(si);
3924 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
3925 appctx->st1 = appctx->st2 = 0;
3926 appctx->ctx.stats.st_code = STAT_STATUS_INIT;
Willy Tarreau676c29e2019-10-09 10:50:01 +02003927 appctx->ctx.stats.flags |= uri_auth->flags;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003928 appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */
3929 if ((msg->flags & HTTP_MSGF_VER_11) && (txn->meth != HTTP_METH_HEAD))
3930 appctx->ctx.stats.flags |= STAT_CHUNKED;
3931
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003932 htx = htxbuf(&req->buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02003933 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003934 lookup = HTX_SL_REQ_UPTR(sl) + uri_auth->uri_len;
3935 end = HTX_SL_REQ_UPTR(sl) + HTX_SL_REQ_ULEN(sl);
Christopher Faulet377c5a52018-10-24 21:21:30 +02003936
3937 for (h = lookup; h <= end - 3; h++) {
3938 if (memcmp(h, ";up", 3) == 0) {
3939 appctx->ctx.stats.flags |= STAT_HIDE_DOWN;
3940 break;
3941 }
Amaury Denoyelle91e55ea2021-02-25 14:46:08 +01003942 }
3943
3944 for (h = lookup; h <= end - 9; h++) {
3945 if (memcmp(h, ";no-maint", 9) == 0) {
Willy Tarreau3e320362020-10-23 17:28:57 +02003946 appctx->ctx.stats.flags |= STAT_HIDE_MAINT;
3947 break;
3948 }
Christopher Faulet377c5a52018-10-24 21:21:30 +02003949 }
3950
3951 if (uri_auth->refresh) {
3952 for (h = lookup; h <= end - 10; h++) {
3953 if (memcmp(h, ";norefresh", 10) == 0) {
3954 appctx->ctx.stats.flags |= STAT_NO_REFRESH;
3955 break;
3956 }
3957 }
3958 }
3959
3960 for (h = lookup; h <= end - 4; h++) {
3961 if (memcmp(h, ";csv", 4) == 0) {
Christopher Faulet6338a082019-09-09 15:50:54 +02003962 appctx->ctx.stats.flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
Christopher Faulet377c5a52018-10-24 21:21:30 +02003963 break;
3964 }
3965 }
3966
3967 for (h = lookup; h <= end - 6; h++) {
3968 if (memcmp(h, ";typed", 6) == 0) {
Christopher Faulet6338a082019-09-09 15:50:54 +02003969 appctx->ctx.stats.flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
Christopher Faulet377c5a52018-10-24 21:21:30 +02003970 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
3971 break;
3972 }
3973 }
3974
Christopher Faulet6338a082019-09-09 15:50:54 +02003975 for (h = lookup; h <= end - 5; h++) {
3976 if (memcmp(h, ";json", 5) == 0) {
3977 appctx->ctx.stats.flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
3978 appctx->ctx.stats.flags |= STAT_FMT_JSON;
3979 break;
3980 }
3981 }
3982
3983 for (h = lookup; h <= end - 12; h++) {
3984 if (memcmp(h, ";json-schema", 12) == 0) {
3985 appctx->ctx.stats.flags &= ~STAT_FMT_MASK;
3986 appctx->ctx.stats.flags |= STAT_JSON_SCHM;
3987 break;
3988 }
3989 }
3990
Christopher Faulet377c5a52018-10-24 21:21:30 +02003991 for (h = lookup; h <= end - 8; h++) {
3992 if (memcmp(h, ";st=", 4) == 0) {
3993 int i;
3994 h += 4;
3995 appctx->ctx.stats.st_code = STAT_STATUS_UNKN;
3996 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
3997 if (strncmp(stat_status_codes[i], h, 4) == 0) {
3998 appctx->ctx.stats.st_code = i;
3999 break;
4000 }
4001 }
4002 break;
4003 }
4004 }
4005
4006 appctx->ctx.stats.scope_str = 0;
4007 appctx->ctx.stats.scope_len = 0;
4008 for (h = lookup; h <= end - 8; h++) {
4009 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
4010 int itx = 0;
4011 const char *h2;
4012 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
4013 const char *err;
4014
4015 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
4016 h2 = h;
Christopher Fauleted7a0662019-01-14 11:07:34 +01004017 appctx->ctx.stats.scope_str = h2 - HTX_SL_REQ_UPTR(sl);
4018 while (h < end) {
Christopher Faulet377c5a52018-10-24 21:21:30 +02004019 if (*h == ';' || *h == '&' || *h == ' ')
4020 break;
4021 itx++;
4022 h++;
4023 }
4024
4025 if (itx > STAT_SCOPE_TXT_MAXLEN)
4026 itx = STAT_SCOPE_TXT_MAXLEN;
4027 appctx->ctx.stats.scope_len = itx;
4028
4029 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
4030 memcpy(scope_txt, h2, itx);
4031 scope_txt[itx] = '\0';
4032 err = invalid_char(scope_txt);
4033 if (err) {
4034 /* bad char in search text => clear scope */
4035 appctx->ctx.stats.scope_str = 0;
4036 appctx->ctx.stats.scope_len = 0;
4037 }
4038 break;
4039 }
4040 }
4041
4042 /* now check whether we have some admin rules for this request */
4043 list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) {
4044 int ret = 1;
4045
4046 if (stats_admin_rule->cond) {
4047 ret = acl_exec_cond(stats_admin_rule->cond, s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
4048 ret = acl_pass(ret);
4049 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
4050 ret = !ret;
4051 }
4052
4053 if (ret) {
4054 /* no rule, or the rule matches */
4055 appctx->ctx.stats.flags |= STAT_ADMIN;
4056 break;
4057 }
4058 }
4059
Christopher Faulet5d45e382019-02-27 15:15:23 +01004060 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
4061 appctx->st0 = STAT_HTTP_HEAD;
4062 else if (txn->meth == HTTP_METH_POST) {
Christopher Fauletbd9e8422019-08-15 22:26:48 +02004063 if (appctx->ctx.stats.flags & STAT_ADMIN) {
Christopher Faulet377c5a52018-10-24 21:21:30 +02004064 appctx->st0 = STAT_HTTP_POST;
Christopher Fauletbd9e8422019-08-15 22:26:48 +02004065 if (msg->msg_state < HTTP_MSG_DATA)
4066 req->analysers |= AN_REQ_HTTP_BODY;
4067 }
Christopher Faulet377c5a52018-10-24 21:21:30 +02004068 else {
Christopher Faulet5d45e382019-02-27 15:15:23 +01004069 /* POST without admin level */
Christopher Faulet377c5a52018-10-24 21:21:30 +02004070 appctx->ctx.stats.flags &= ~STAT_CHUNKED;
4071 appctx->ctx.stats.st_code = STAT_STATUS_DENY;
4072 appctx->st0 = STAT_HTTP_LAST;
4073 }
4074 }
4075 else {
Christopher Faulet5d45e382019-02-27 15:15:23 +01004076 /* Unsupported method */
4077 appctx->ctx.stats.flags &= ~STAT_CHUNKED;
4078 appctx->ctx.stats.st_code = STAT_STATUS_IVAL;
4079 appctx->st0 = STAT_HTTP_LAST;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004080 }
4081
4082 s->task->nice = -32; /* small boost for HTTP statistics */
4083 return 1;
4084}
4085
Christopher Faulet021a8e42021-03-29 10:46:38 +02004086/* This function waits for the message payload at most <time> milliseconds (may
4087 * be set to TICK_ETERNITY). It stops to wait if at least <bytes> bytes of the
4088 * payload are received (0 means no limit). It returns HTTP_RULE_* depending on
4089 * the result:
4090 *
4091 * - HTTP_RULE_RES_CONT when conditions are met to stop waiting
4092 * - HTTP_RULE_RES_YIELD to wait for more data
Ilya Shipitsinb2be9a12021-04-24 13:25:42 +05004093 * - HTTP_RULE_RES_ABRT when a timeout occurred.
Christopher Faulet021a8e42021-03-29 10:46:38 +02004094 * - HTTP_RULE_RES_BADREQ if a parsing error is raised by lower level
Ilya Shipitsinb2be9a12021-04-24 13:25:42 +05004095 * - HTTP_RULE_RES_ERROR if an internal error occurred
Christopher Faulet021a8e42021-03-29 10:46:38 +02004096 *
Ilya Shipitsinb2be9a12021-04-24 13:25:42 +05004097 * If a timeout occurred, this function is responsible to emit the right response
Christopher Faulet021a8e42021-03-29 10:46:38 +02004098 * to the client, depending on the channel (408 on request side, 504 on response
4099 * side). All other errors must be handled by the caller.
4100 */
4101enum rule_result http_wait_for_msg_body(struct stream *s, struct channel *chn,
4102 unsigned int time, unsigned int bytes)
4103{
4104 struct session *sess = s->sess;
4105 struct http_txn *txn = s->txn;
4106 struct http_msg *msg = ((chn->flags & CF_ISRESP) ? &txn->rsp : &txn->req);
4107 struct htx *htx;
4108 enum rule_result ret = HTTP_RULE_RES_CONT;
4109
4110 htx = htxbuf(&chn->buf);
4111
4112 if (htx->flags & HTX_FL_PARSING_ERROR) {
4113 ret = HTTP_RULE_RES_BADREQ;
4114 goto end;
4115 }
4116 if (htx->flags & HTX_FL_PROCESSING_ERROR) {
4117 ret = HTTP_RULE_RES_ERROR;
4118 goto end;
4119 }
4120
4121 /* Do nothing for bodyless and CONNECT requests */
4122 if (txn->meth == HTTP_METH_CONNECT || (msg->flags & HTTP_MSGF_BODYLESS))
4123 goto end;
4124
4125 if (!(chn->flags & CF_ISRESP) && msg->msg_state < HTTP_MSG_DATA) {
4126 if (http_handle_expect_hdr(s, htx, msg) == -1) {
4127 ret = HTTP_RULE_RES_ERROR;
4128 goto end;
4129 }
4130 }
4131
4132 msg->msg_state = HTTP_MSG_DATA;
4133
4134 /* Now we're in HTTP_MSG_DATA. We just need to know if all data have
4135 * been received or if the buffer is full.
4136 */
4137 if ((htx->flags & HTX_FL_EOM) || htx_get_tail_type(htx) > HTX_BLK_DATA ||
4138 channel_htx_full(chn, htx, global.tune.maxrewrite))
4139 goto end;
4140
4141 if (bytes) {
4142 struct htx_blk *blk;
4143 unsigned int len = 0;
4144
4145 for (blk = htx_get_first_blk(htx); blk; blk = htx_get_next_blk(htx, blk)) {
4146 if (htx_get_blk_type(blk) != HTX_BLK_DATA)
4147 continue;
4148 len += htx_get_blksz(blk);
4149 if (len >= bytes)
4150 goto end;
4151 }
4152 }
4153
4154 if ((chn->flags & CF_READ_TIMEOUT) || tick_is_expired(chn->analyse_exp, now_ms)) {
4155 if (!(chn->flags & CF_ISRESP))
4156 goto abort_req;
4157 goto abort_res;
4158 }
4159
4160 /* we get here if we need to wait for more data */
4161 if (!(chn->flags & (CF_SHUTR | CF_READ_ERROR))) {
4162 if (!tick_isset(chn->analyse_exp))
4163 chn->analyse_exp = tick_add_ifset(now_ms, time);
4164 ret = HTTP_RULE_RES_YIELD;
4165 }
4166
4167 end:
4168 return ret;
4169
4170 abort_req:
4171 txn->status = 408;
4172 if (!(s->flags & SF_ERR_MASK))
4173 s->flags |= SF_ERR_CLITO;
4174 if (!(s->flags & SF_FINST_MASK))
4175 s->flags |= SF_FINST_D;
Willy Tarreau4781b152021-04-06 13:53:36 +02004176 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req);
Christopher Faulet021a8e42021-03-29 10:46:38 +02004177 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02004178 _HA_ATOMIC_INC(&sess->listener->counters->failed_req);
Christopher Faulet021a8e42021-03-29 10:46:38 +02004179 http_reply_and_close(s, txn->status, http_error_message(s));
4180 ret = HTTP_RULE_RES_ABRT;
4181 goto end;
4182
4183 abort_res:
4184 txn->status = 504;
4185 if (!(s->flags & SF_ERR_MASK))
4186 s->flags |= SF_ERR_SRVTO;
4187 if (!(s->flags & SF_FINST_MASK))
4188 s->flags |= SF_FINST_D;
4189 stream_inc_http_fail_ctr(s);
4190 http_reply_and_close(s, txn->status, http_error_message(s));
4191 ret = HTTP_RULE_RES_ABRT;
4192 goto end;
4193}
4194
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004195void http_perform_server_redirect(struct stream *s, struct stream_interface *si)
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004196{
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004197 struct channel *req = &s->req;
4198 struct channel *res = &s->res;
4199 struct server *srv;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004200 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004201 struct htx_sl *sl;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004202 struct ist path, location;
4203 unsigned int flags;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004204
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004205 /*
4206 * Create the location
4207 */
4208 chunk_reset(&trash);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004209
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004210 /* 1: add the server's prefix */
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004211 /* special prefix "/" means don't change URL */
4212 srv = __objt_server(s->target);
4213 if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') {
4214 if (!chunk_memcat(&trash, srv->rdr_pfx, srv->rdr_len))
4215 return;
4216 }
4217
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004218 /* 2: add the request Path */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004219 htx = htxbuf(&req->buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02004220 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004221 path = http_get_path(htx_sl_req_uri(sl));
Tim Duesterhused526372020-03-05 17:56:33 +01004222 if (!isttest(path))
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004223 return;
4224
4225 if (!chunk_memcat(&trash, path.ptr, path.len))
4226 return;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004227 location = ist2(trash.area, trash.data);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004228
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004229 /*
4230 * Create the 302 respone
4231 */
4232 htx = htx_from_buf(&res->buf);
4233 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
4234 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
4235 ist("HTTP/1.1"), ist("302"), ist("Found"));
4236 if (!sl)
4237 goto fail;
4238 sl->info.res.status = 302;
4239 s->txn->status = 302;
4240
4241 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) ||
4242 !htx_add_header(htx, ist("Connection"), ist("close")) ||
4243 !htx_add_header(htx, ist("Content-length"), ist("0")) ||
4244 !htx_add_header(htx, ist("Location"), location))
4245 goto fail;
4246
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004247 if (!htx_add_endof(htx, HTX_BLK_EOH))
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004248 goto fail;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004249
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004250 htx->flags |= HTX_FL_EOM;
Christopher Fauletc20afb82020-01-24 19:16:26 +01004251 htx_to_buf(htx, &res->buf);
Christopher Fauleta72a7e42020-01-28 09:28:11 +01004252 if (!http_forward_proxy_resp(s, 1))
4253 goto fail;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004254
4255 /* return without error. */
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004256 si_shutr(si);
4257 si_shutw(si);
4258 si->err_type = SI_ET_NONE;
4259 si->state = SI_ST_CLO;
4260
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004261 if (!(s->flags & SF_ERR_MASK))
4262 s->flags |= SF_ERR_LOCAL;
4263 if (!(s->flags & SF_FINST_MASK))
4264 s->flags |= SF_FINST_C;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004265
4266 /* FIXME: we should increase a counter of redirects per server and per backend. */
4267 srv_inc_sess_ctr(srv);
4268 srv_set_sess_last(srv);
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004269 return;
4270
4271 fail:
4272 /* If an error occurred, remove the incomplete HTTP response from the
4273 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004274 channel_htx_truncate(res, htx);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004275}
4276
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05004277/* This function terminates the request because it was completely analyzed or
Christopher Fauletf2824e62018-10-01 12:12:37 +02004278 * because an error was triggered during the body forwarding.
4279 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004280static void http_end_request(struct stream *s)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004281{
4282 struct channel *chn = &s->req;
4283 struct http_txn *txn = s->txn;
4284
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004285 DBG_TRACE_ENTER(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004286
Christopher Fauletb42a8b62018-11-19 21:59:00 +01004287 if (unlikely(txn->req.msg_state == HTTP_MSG_ERROR ||
4288 txn->rsp.msg_state == HTTP_MSG_ERROR)) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004289 channel_abort(chn);
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004290 channel_htx_truncate(chn, htxbuf(&chn->buf));
Christopher Fauletf2824e62018-10-01 12:12:37 +02004291 goto end;
4292 }
4293
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004294 if (unlikely(txn->req.msg_state < HTTP_MSG_DONE)) {
4295 DBG_TRACE_DEVEL("waiting end of the request", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004296 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004297 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004298
4299 if (txn->req.msg_state == HTTP_MSG_DONE) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004300 /* No need to read anymore, the request was completely parsed.
4301 * We can shut the read side unless we want to abort_on_close,
4302 * or we have a POST request. The issue with POST requests is
4303 * that some browsers still send a CRLF after the request, and
4304 * this CRLF must be read so that it does not remain in the kernel
4305 * buffers, otherwise a close could cause an RST on some systems
4306 * (eg: Linux).
4307 */
Christopher Faulet769d0e92019-03-22 14:23:18 +01004308 if (!(s->be->options & PR_O_ABRT_CLOSE) && txn->meth != HTTP_METH_POST)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004309 channel_dont_read(chn);
4310
4311 /* if the server closes the connection, we want to immediately react
4312 * and close the socket to save packets and syscalls.
4313 */
4314 s->si[1].flags |= SI_FL_NOHALF;
4315
4316 /* In any case we've finished parsing the request so we must
4317 * disable Nagle when sending data because 1) we're not going
4318 * to shut this side, and 2) the server is waiting for us to
4319 * send pending data.
4320 */
4321 chn->flags |= CF_NEVER_WAIT;
4322
Christopher Fauletd01ce402019-01-02 17:44:13 +01004323 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
4324 /* The server has not finished to respond, so we
4325 * don't want to move in order not to upset it.
4326 */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004327 DBG_TRACE_DEVEL("waiting end of the response", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletd01ce402019-01-02 17:44:13 +01004328 return;
4329 }
4330
Christopher Fauletf2824e62018-10-01 12:12:37 +02004331 /* When we get here, it means that both the request and the
4332 * response have finished receiving. Depending on the connection
4333 * mode, we'll have to wait for the last bytes to leave in either
4334 * direction, and sometimes for a close to be effective.
4335 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02004336 if (txn->flags & TX_CON_WANT_TUN) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004337 /* Tunnel mode will not have any analyser so it needs to
4338 * poll for reads.
4339 */
4340 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004341 if (b_data(&chn->buf)) {
4342 DBG_TRACE_DEVEL("waiting to flush the request", STRM_EV_HTTP_ANA, s, txn);
Christopher Faulet9768c262018-10-22 09:34:31 +02004343 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004344 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004345 txn->req.msg_state = HTTP_MSG_TUNNEL;
4346 }
4347 else {
4348 /* we're not expecting any new data to come for this
4349 * transaction, so we can close it.
Christopher Faulet9768c262018-10-22 09:34:31 +02004350 *
4351 * However, there is an exception if the response
4352 * length is undefined. In this case, we need to wait
4353 * the close from the server. The response will be
4354 * switched in TUNNEL mode until the end.
Christopher Fauletf2824e62018-10-01 12:12:37 +02004355 */
4356 if (!(txn->rsp.flags & HTTP_MSGF_XFER_LEN) &&
4357 txn->rsp.msg_state != HTTP_MSG_CLOSED)
Christopher Faulet9768c262018-10-22 09:34:31 +02004358 goto check_channel_flags;
Christopher Fauletf2824e62018-10-01 12:12:37 +02004359
4360 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4361 channel_shutr_now(chn);
4362 channel_shutw_now(chn);
4363 }
4364 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004365 goto check_channel_flags;
4366 }
4367
4368 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
4369 http_msg_closing:
4370 /* nothing else to forward, just waiting for the output buffer
4371 * to be empty and for the shutw_now to take effect.
4372 */
4373 if (channel_is_empty(chn)) {
4374 txn->req.msg_state = HTTP_MSG_CLOSED;
4375 goto http_msg_closed;
4376 }
4377 else if (chn->flags & CF_SHUTW) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004378 txn->req.msg_state = HTTP_MSG_ERROR;
4379 goto end;
4380 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004381 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004382 return;
4383 }
4384
4385 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
4386 http_msg_closed:
Christopher Fauletf2824e62018-10-01 12:12:37 +02004387 /* if we don't know whether the server will close, we need to hard close */
4388 if (txn->rsp.flags & HTTP_MSGF_XFER_LEN)
4389 s->si[1].flags |= SI_FL_NOLINGER; /* we want to close ASAP */
Christopher Fauletf2824e62018-10-01 12:12:37 +02004390 /* see above in MSG_DONE why we only do this in these states */
Christopher Faulet769d0e92019-03-22 14:23:18 +01004391 if (!(s->be->options & PR_O_ABRT_CLOSE))
Christopher Fauletf2824e62018-10-01 12:12:37 +02004392 channel_dont_read(chn);
4393 goto end;
4394 }
4395
4396 check_channel_flags:
4397 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4398 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4399 /* if we've just closed an output, let's switch */
4400 txn->req.msg_state = HTTP_MSG_CLOSING;
4401 goto http_msg_closing;
4402 }
4403
4404 end:
4405 chn->analysers &= AN_REQ_FLT_END;
Christopher Faulet198ef8b2020-12-15 13:32:55 +01004406 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
4407 chn->flags |= CF_NEVER_WAIT;
4408 if (HAS_REQ_DATA_FILTERS(s))
Christopher Fauletf2824e62018-10-01 12:12:37 +02004409 chn->analysers |= AN_REQ_FLT_XFER_DATA;
Christopher Faulet198ef8b2020-12-15 13:32:55 +01004410 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004411 channel_auto_close(chn);
4412 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004413 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004414}
4415
4416
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05004417/* This function terminates the response because it was completely analyzed or
Christopher Fauletf2824e62018-10-01 12:12:37 +02004418 * because an error was triggered during the body forwarding.
4419 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004420static void http_end_response(struct stream *s)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004421{
4422 struct channel *chn = &s->res;
4423 struct http_txn *txn = s->txn;
4424
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004425 DBG_TRACE_ENTER(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004426
Christopher Fauletb42a8b62018-11-19 21:59:00 +01004427 if (unlikely(txn->req.msg_state == HTTP_MSG_ERROR ||
4428 txn->rsp.msg_state == HTTP_MSG_ERROR)) {
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004429 channel_htx_truncate(&s->req, htxbuf(&s->req.buf));
Christopher Faulet9768c262018-10-22 09:34:31 +02004430 channel_abort(&s->req);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004431 goto end;
4432 }
4433
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004434 if (unlikely(txn->rsp.msg_state < HTTP_MSG_DONE)) {
4435 DBG_TRACE_DEVEL("waiting end of the response", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004436 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004437 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004438
4439 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
4440 /* In theory, we don't need to read anymore, but we must
4441 * still monitor the server connection for a possible close
4442 * while the request is being uploaded, so we don't disable
4443 * reading.
4444 */
4445 /* channel_dont_read(chn); */
4446
4447 if (txn->req.msg_state < HTTP_MSG_DONE) {
4448 /* The client seems to still be sending data, probably
4449 * because we got an error response during an upload.
4450 * We have the choice of either breaking the connection
4451 * or letting it pass through. Let's do the later.
4452 */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004453 DBG_TRACE_DEVEL("waiting end of the request", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004454 return;
4455 }
4456
4457 /* When we get here, it means that both the request and the
4458 * response have finished receiving. Depending on the connection
4459 * mode, we'll have to wait for the last bytes to leave in either
4460 * direction, and sometimes for a close to be effective.
4461 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02004462 if (txn->flags & TX_CON_WANT_TUN) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004463 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004464 if (b_data(&chn->buf)) {
4465 DBG_TRACE_DEVEL("waiting to flush the respone", STRM_EV_HTTP_ANA, s, txn);
Christopher Faulet9768c262018-10-22 09:34:31 +02004466 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004467 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004468 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
4469 }
4470 else {
4471 /* we're not expecting any new data to come for this
4472 * transaction, so we can close it.
4473 */
4474 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4475 channel_shutr_now(chn);
4476 channel_shutw_now(chn);
4477 }
4478 }
4479 goto check_channel_flags;
4480 }
4481
4482 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
4483 http_msg_closing:
4484 /* nothing else to forward, just waiting for the output buffer
4485 * to be empty and for the shutw_now to take effect.
4486 */
4487 if (channel_is_empty(chn)) {
4488 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4489 goto http_msg_closed;
4490 }
4491 else if (chn->flags & CF_SHUTW) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004492 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau4781b152021-04-06 13:53:36 +02004493 _HA_ATOMIC_INC(&strm_sess(s)->fe->fe_counters.cli_aborts);
4494 _HA_ATOMIC_INC(&s->be->be_counters.cli_aborts);
William Lallemand36119de2021-03-08 15:26:48 +01004495 if (strm_sess(s)->listener && strm_sess(s)->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02004496 _HA_ATOMIC_INC(&strm_sess(s)->listener->counters->cli_aborts);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004497 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02004498 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.cli_aborts);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004499 goto end;
4500 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004501 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004502 return;
4503 }
4504
4505 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
4506 http_msg_closed:
4507 /* drop any pending data */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004508 channel_htx_truncate(&s->req, htxbuf(&s->req.buf));
Christopher Faulet9768c262018-10-22 09:34:31 +02004509 channel_abort(&s->req);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004510 goto end;
4511 }
4512
4513 check_channel_flags:
4514 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4515 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4516 /* if we've just closed an output, let's switch */
4517 txn->rsp.msg_state = HTTP_MSG_CLOSING;
4518 goto http_msg_closing;
4519 }
4520
4521 end:
4522 chn->analysers &= AN_RES_FLT_END;
Christopher Faulet198ef8b2020-12-15 13:32:55 +01004523 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
4524 chn->flags |= CF_NEVER_WAIT;
4525 if (HAS_RSP_DATA_FILTERS(s))
4526 chn->analysers |= AN_RES_FLT_XFER_DATA;
4527 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004528 channel_auto_close(chn);
4529 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004530 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004531}
4532
Christopher Fauletef70e252020-01-28 09:26:19 +01004533/* Forward a response generated by HAProxy (error/redirect/return). This
4534 * function forwards all pending incoming data. If <final> is set to 0, nothing
4535 * more is performed. It is used for 1xx informational messages. Otherwise, the
Christopher Faulet507479b2020-05-15 12:29:46 +02004536 * transaction is terminated and the request is emptied. On success 1 is
Christopher Faulet40e6b552020-06-25 16:04:50 +02004537 * returned. If an error occurred, 0 is returned. If it fails, this function
4538 * only exits. It is the caller responsibility to do the cleanup.
Christopher Fauletef70e252020-01-28 09:26:19 +01004539 */
4540int http_forward_proxy_resp(struct stream *s, int final)
4541{
4542 struct channel *req = &s->req;
4543 struct channel *res = &s->res;
4544 struct htx *htx = htxbuf(&res->buf);
4545 size_t data;
4546
4547 if (final) {
4548 htx->flags |= HTX_FL_PROXY_RESP;
Christopher Faulet507479b2020-05-15 12:29:46 +02004549
Christopher Fauletaab1b672020-11-18 16:44:02 +01004550 if (!htx_is_empty(htx) && !http_eval_after_res_rules(s))
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01004551 return 0;
Christopher Fauletef70e252020-01-28 09:26:19 +01004552
Christopher Fauletd6c48362020-10-19 18:01:38 +02004553 if (s->txn->meth == HTTP_METH_HEAD)
4554 htx_skip_msg_payload(htx);
4555
Christopher Fauletef70e252020-01-28 09:26:19 +01004556 channel_auto_read(req);
4557 channel_abort(req);
4558 channel_auto_close(req);
4559 channel_htx_erase(req, htxbuf(&req->buf));
4560
4561 res->wex = tick_add_ifset(now_ms, res->wto);
4562 channel_auto_read(res);
4563 channel_auto_close(res);
4564 channel_shutr_now(res);
Christopher Faulet1a9db7c2020-06-25 15:36:45 +02004565 res->flags |= CF_EOI; /* The response is terminated, add EOI */
Christopher Faulet42432f32020-11-20 17:43:16 +01004566 htxbuf(&res->buf)->flags |= HTX_FL_EOM; /* no more data are expected */
Christopher Fauletef70e252020-01-28 09:26:19 +01004567 }
Christopher Fauletcf6898c2020-06-25 15:55:11 +02004568 else {
4569 /* Send ASAP informational messages. Rely on CF_EOI for final
4570 * response.
4571 */
4572 res->flags |= CF_SEND_DONTWAIT;
4573 }
Christopher Fauletef70e252020-01-28 09:26:19 +01004574
4575 data = htx->data - co_data(res);
4576 c_adv(res, data);
4577 htx->first = -1;
4578 res->total += data;
4579 return 1;
4580}
4581
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004582void http_server_error(struct stream *s, struct stream_interface *si, int err,
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004583 int finst, struct http_reply *msg)
Christopher Faulet0f226952018-10-22 09:29:56 +02004584{
Christopher Faulet72c7d8d2020-01-27 15:32:25 +01004585 http_reply_and_close(s, s->txn->status, msg);
Christopher Faulet0f226952018-10-22 09:29:56 +02004586 if (!(s->flags & SF_ERR_MASK))
4587 s->flags |= err;
4588 if (!(s->flags & SF_FINST_MASK))
4589 s->flags |= finst;
4590}
4591
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004592void http_reply_and_close(struct stream *s, short status, struct http_reply *msg)
Christopher Faulet0f226952018-10-22 09:29:56 +02004593{
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004594 if (!msg) {
4595 channel_htx_truncate(&s->res, htxbuf(&s->res.buf));
4596 goto end;
4597 }
4598
4599 if (http_reply_message(s, msg) == -1) {
4600 /* On error, return a 500 error message, but don't rewrite it if
Christopher Faulet40e6b552020-06-25 16:04:50 +02004601 * it is already an internal error. If it was already a "const"
4602 * 500 error, just fail.
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004603 */
Christopher Faulet40e6b552020-06-25 16:04:50 +02004604 if (s->txn->status == 500) {
4605 if (s->txn->flags & TX_CONST_REPLY)
4606 goto end;
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004607 s->txn->flags |= TX_CONST_REPLY;
Christopher Faulet40e6b552020-06-25 16:04:50 +02004608 }
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004609 s->txn->status = 500;
4610 s->txn->http_reply = NULL;
4611 return http_reply_and_close(s, s->txn->status, http_error_message(s));
4612 }
4613
4614end:
4615 s->res.wex = tick_add_ifset(now_ms, s->res.wto);
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004616
Christopher Faulet0f226952018-10-22 09:29:56 +02004617 channel_auto_read(&s->req);
4618 channel_abort(&s->req);
4619 channel_auto_close(&s->req);
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004620 channel_htx_erase(&s->req, htxbuf(&s->req.buf));
Christopher Faulet72c7d8d2020-01-27 15:32:25 +01004621 channel_auto_read(&s->res);
4622 channel_auto_close(&s->res);
4623 channel_shutr_now(&s->res);
Christopher Faulet0f226952018-10-22 09:29:56 +02004624}
4625
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004626struct http_reply *http_error_message(struct stream *s)
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004627{
4628 const int msgnum = http_get_status_idx(s->txn->status);
4629
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004630 if (s->txn->http_reply)
4631 return s->txn->http_reply;
4632 else if (s->be->replies[msgnum])
4633 return s->be->replies[msgnum];
4634 else if (strm_fe(s)->replies[msgnum])
4635 return strm_fe(s)->replies[msgnum];
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004636 else
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004637 return &http_err_replies[msgnum];
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004638}
4639
Christopher Faulet40e6b552020-06-25 16:04:50 +02004640/* Produces an HTX message from an http reply. Depending on the http reply type,
4641 * a, errorfile, an raw file or a log-format string is used. On success, it
4642 * returns 0. If an error occurs -1 is returned. If it fails, this function only
4643 * exits. It is the caller responsibility to do the cleanup.
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004644 */
Christopher Fauletae43b6c2020-05-27 15:24:22 +02004645int http_reply_to_htx(struct stream *s, struct htx *htx, struct http_reply *reply)
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004646{
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004647 struct buffer *errmsg;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004648 struct htx_sl *sl;
4649 struct buffer *body = NULL;
4650 const char *status, *reason, *clen, *ctype;
4651 unsigned int slflags;
4652 int ret = 0;
4653
Christopher Faulete29a97e2020-05-14 14:49:25 +02004654 /*
4655 * - HTTP_REPLY_ERRFILES unexpected here. handled as no payload if so
4656 *
4657 * - HTTP_REPLY_INDIRECT: switch on another reply if defined or handled
4658 * as no payload if NULL. the TXN status code is set with the status
4659 * of the original reply.
4660 */
4661
4662 if (reply->type == HTTP_REPLY_INDIRECT) {
4663 if (reply->body.reply)
4664 reply = reply->body.reply;
4665 }
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004666 if (reply->type == HTTP_REPLY_ERRMSG && !reply->body.errmsg) {
4667 /* get default error message */
4668 if (reply == s->txn->http_reply)
4669 s->txn->http_reply = NULL;
4670 reply = http_error_message(s);
4671 if (reply->type == HTTP_REPLY_INDIRECT) {
4672 if (reply->body.reply)
4673 reply = reply->body.reply;
4674 }
4675 }
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004676
4677 if (reply->type == HTTP_REPLY_ERRMSG) {
4678 /* implicit or explicit error message*/
4679 errmsg = reply->body.errmsg;
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004680 if (errmsg && !b_is_null(errmsg)) {
Christopher Faulet20567362020-05-15 14:52:49 +02004681 if (!htx_copy_msg(htx, errmsg))
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004682 goto fail;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004683 }
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004684 }
4685 else {
4686 /* no payload, file or log-format string */
4687 if (reply->type == HTTP_REPLY_RAW) {
4688 /* file */
4689 body = &reply->body.obj;
4690 }
4691 else if (reply->type == HTTP_REPLY_LOGFMT) {
4692 /* log-format string */
4693 body = alloc_trash_chunk();
4694 if (!body)
4695 goto fail_alloc;
4696 body->data = build_logline(s, body->area, body->size, &reply->body.fmt);
4697 }
4698 /* else no payload */
4699
4700 status = ultoa(reply->status);
4701 reason = http_get_reason(reply->status);
4702 slflags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_CLEN);
4703 if (!body || !b_data(body))
4704 slflags |= HTX_SL_F_BODYLESS;
4705 sl = htx_add_stline(htx, HTX_BLK_RES_SL, slflags, ist("HTTP/1.1"), ist(status), ist(reason));
4706 if (!sl)
4707 goto fail;
4708 sl->info.res.status = reply->status;
4709
4710 clen = (body ? ultoa(b_data(body)) : "0");
4711 ctype = reply->ctype;
4712
4713 if (!LIST_ISEMPTY(&reply->hdrs)) {
4714 struct http_reply_hdr *hdr;
4715 struct buffer *value = alloc_trash_chunk();
4716
4717 if (!value)
4718 goto fail;
4719
4720 list_for_each_entry(hdr, &reply->hdrs, list) {
4721 chunk_reset(value);
4722 value->data = build_logline(s, value->area, value->size, &hdr->value);
4723 if (b_data(value) && !htx_add_header(htx, hdr->name, ist2(b_head(value), b_data(value)))) {
4724 free_trash_chunk(value);
4725 goto fail;
4726 }
4727 chunk_reset(value);
4728 }
4729 free_trash_chunk(value);
4730 }
4731
4732 if (!htx_add_header(htx, ist("content-length"), ist(clen)) ||
4733 (body && b_data(body) && ctype && !htx_add_header(htx, ist("content-type"), ist(ctype))) ||
4734 !htx_add_endof(htx, HTX_BLK_EOH) ||
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004735 (body && b_data(body) && !htx_add_data_atonce(htx, ist2(b_head(body), b_data(body)))))
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004736 goto fail;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004737
4738 htx->flags |= HTX_FL_EOM;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004739 }
4740
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004741 leave:
4742 if (reply->type == HTTP_REPLY_LOGFMT)
4743 free_trash_chunk(body);
4744 return ret;
4745
4746 fail_alloc:
4747 if (!(s->flags & SF_ERR_MASK))
4748 s->flags |= SF_ERR_RESOURCE;
Christopher Faulet97e466c2020-05-15 15:12:47 +02004749 /* fall through */
4750 fail:
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004751 ret = -1;
4752 goto leave;
Christopher Faulet97e466c2020-05-15 15:12:47 +02004753}
4754
4755/* Send an http reply to the client. On success, it returns 0. If an error
Christopher Faulet40e6b552020-06-25 16:04:50 +02004756 * occurs -1 is returned and the response channel is truncated, removing this
4757 * way the faulty reply. This function may fail when the reply is formatted
4758 * (http_reply_to_htx) or when the reply is forwarded
4759 * (http_forward_proxy_resp). On the last case, it is because a
4760 * http-after-response rule fails.
Christopher Faulet97e466c2020-05-15 15:12:47 +02004761 */
4762int http_reply_message(struct stream *s, struct http_reply *reply)
4763{
4764 struct channel *res = &s->res;
4765 struct htx *htx = htx_from_buf(&res->buf);
4766
4767 if (s->txn->status == -1)
4768 s->txn->status = reply->status;
4769 channel_htx_truncate(res, htx);
4770
4771 if (http_reply_to_htx(s, htx, reply) == -1)
4772 goto fail;
4773
4774 htx_to_buf(htx, &s->res.buf);
4775 if (!http_forward_proxy_resp(s, 1))
4776 goto fail;
4777 return 0;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004778
4779 fail:
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004780 channel_htx_truncate(res, htx);
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004781 if (!(s->flags & SF_ERR_MASK))
4782 s->flags |= SF_ERR_PRXCOND;
Christopher Faulet97e466c2020-05-15 15:12:47 +02004783 return -1;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004784}
4785
Christopher Faulet304cc402019-07-15 15:46:28 +02004786/* Return the error message corresponding to si->err_type. It is assumed
4787 * that the server side is closed. Note that err_type is actually a
4788 * bitmask, where almost only aborts may be cumulated with other
4789 * values. We consider that aborted operations are more important
4790 * than timeouts or errors due to the fact that nobody else in the
4791 * logs might explain incomplete retries. All others should avoid
4792 * being cumulated. It should normally not be possible to have multiple
4793 * aborts at once, but just in case, the first one in sequence is reported.
4794 * Note that connection errors appearing on the second request of a keep-alive
4795 * connection are not reported since this allows the client to retry.
4796 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004797void http_return_srv_error(struct stream *s, struct stream_interface *si)
Christopher Faulet304cc402019-07-15 15:46:28 +02004798{
4799 int err_type = si->err_type;
4800
4801 /* set s->txn->status for http_error_message(s) */
4802 s->txn->status = 503;
4803
4804 if (err_type & SI_ET_QUEUE_ABRT)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004805 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_Q,
4806 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004807 else if (err_type & SI_ET_CONN_ABRT)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004808 http_server_error(s, si, SF_ERR_CLICL, 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_QUEUE_TO)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004812 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_Q,
4813 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004814 else if (err_type & SI_ET_QUEUE_ERR)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004815 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_Q,
4816 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004817 else if (err_type & SI_ET_CONN_TO)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004818 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_C,
4819 (s->txn->flags & TX_NOT_FIRST) ? NULL :
4820 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004821 else if (err_type & SI_ET_CONN_ERR)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004822 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_C,
4823 (s->flags & SF_SRV_REUSED) ? NULL :
4824 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004825 else if (err_type & SI_ET_CONN_RES)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004826 http_server_error(s, si, SF_ERR_RESOURCE, SF_FINST_C,
4827 (s->txn->flags & TX_NOT_FIRST) ? NULL :
4828 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004829 else { /* SI_ET_CONN_OTHER and others */
4830 s->txn->status = 500;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004831 http_server_error(s, si, SF_ERR_INTERNAL, SF_FINST_C,
4832 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004833 }
4834}
4835
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004836
Christopher Faulet4a28a532019-03-01 11:19:40 +01004837/* Handle Expect: 100-continue for HTTP/1.1 messages if necessary. It returns 0
4838 * on success and -1 on error.
4839 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004840static int http_handle_expect_hdr(struct stream *s, struct htx *htx, struct http_msg *msg)
Christopher Faulet4a28a532019-03-01 11:19:40 +01004841{
4842 /* If we have HTTP/1.1 message with a body and Expect: 100-continue,
4843 * then we must send an HTTP/1.1 100 Continue intermediate response.
4844 */
4845 if (msg->msg_state == HTTP_MSG_BODY && (msg->flags & HTTP_MSGF_VER_11) &&
4846 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
4847 struct ist hdr = { .ptr = "Expect", .len = 6 };
4848 struct http_hdr_ctx ctx;
4849
4850 ctx.blk = NULL;
4851 /* Expect is allowed in 1.1, look for it */
4852 if (http_find_header(htx, hdr, &ctx, 0) &&
4853 unlikely(isteqi(ctx.value, ist2("100-continue", 12)))) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004854 if (http_reply_100_continue(s) == -1)
Christopher Faulet4a28a532019-03-01 11:19:40 +01004855 return -1;
4856 http_remove_header(htx, &ctx);
4857 }
4858 }
4859 return 0;
4860}
4861
Christopher Faulet23a3c792018-11-28 10:01:23 +01004862/* Send a 100-Continue response to the client. It returns 0 on success and -1
4863 * on error. The response channel is updated accordingly.
4864 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004865static int http_reply_100_continue(struct stream *s)
Christopher Faulet23a3c792018-11-28 10:01:23 +01004866{
4867 struct channel *res = &s->res;
4868 struct htx *htx = htx_from_buf(&res->buf);
4869 struct htx_sl *sl;
4870 unsigned int flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|
4871 HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
Christopher Faulet23a3c792018-11-28 10:01:23 +01004872
4873 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
4874 ist("HTTP/1.1"), ist("100"), ist("Continue"));
4875 if (!sl)
4876 goto fail;
4877 sl->info.res.status = 100;
4878
Christopher Faulet1d5ec092019-06-26 14:23:54 +02004879 if (!htx_add_endof(htx, HTX_BLK_EOH))
Christopher Faulet23a3c792018-11-28 10:01:23 +01004880 goto fail;
4881
Christopher Fauleta72a7e42020-01-28 09:28:11 +01004882 if (!http_forward_proxy_resp(s, 0))
4883 goto fail;
Christopher Faulet23a3c792018-11-28 10:01:23 +01004884 return 0;
4885
4886 fail:
4887 /* If an error occurred, remove the incomplete HTTP response from the
4888 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004889 channel_htx_truncate(res, htx);
Christopher Faulet23a3c792018-11-28 10:01:23 +01004890 return -1;
4891}
4892
Christopher Faulet12c51e22018-11-28 15:59:42 +01004893
Christopher Faulet0f226952018-10-22 09:29:56 +02004894/*
4895 * Capture headers from message <htx> according to header list <cap_hdr>, and
4896 * fill the <cap> pointers appropriately.
4897 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004898static void http_capture_headers(struct htx *htx, char **cap, struct cap_hdr *cap_hdr)
Christopher Faulet0f226952018-10-22 09:29:56 +02004899{
4900 struct cap_hdr *h;
4901 int32_t pos;
4902
Christopher Fauleta3f15502019-05-13 15:27:23 +02004903 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet0f226952018-10-22 09:29:56 +02004904 struct htx_blk *blk = htx_get_blk(htx, pos);
4905 enum htx_blk_type type = htx_get_blk_type(blk);
4906 struct ist n, v;
4907
4908 if (type == HTX_BLK_EOH)
4909 break;
4910 if (type != HTX_BLK_HDR)
4911 continue;
4912
4913 n = htx_get_blk_name(htx, blk);
4914
4915 for (h = cap_hdr; h; h = h->next) {
4916 if (h->namelen && (h->namelen == n.len) &&
4917 (strncasecmp(n.ptr, h->name, h->namelen) == 0)) {
4918 if (cap[h->index] == NULL)
4919 cap[h->index] =
4920 pool_alloc(h->pool);
4921
4922 if (cap[h->index] == NULL) {
4923 ha_alert("HTTP capture : out of memory.\n");
4924 break;
4925 }
4926
4927 v = htx_get_blk_value(htx, blk);
4928 if (v.len > h->len)
4929 v.len = h->len;
4930
4931 memcpy(cap[h->index], v.ptr, v.len);
4932 cap[h->index][v.len]=0;
4933 }
4934 }
4935 }
4936}
4937
Christopher Faulet0b6bdc52018-10-24 11:05:36 +02004938/* Delete a value in a header between delimiters <from> and <next>. The header
4939 * itself is delimited by <start> and <end> pointers. The number of characters
4940 * displaced is returned, and the pointer to the first delimiter is updated if
4941 * required. The function tries as much as possible to respect the following
4942 * principles :
4943 * - replace <from> delimiter by the <next> one unless <from> points to <start>,
4944 * in which case <next> is simply removed
4945 * - set exactly one space character after the new first delimiter, unless there
4946 * are not enough characters in the block being moved to do so.
4947 * - remove unneeded spaces before the previous delimiter and after the new
4948 * one.
4949 *
4950 * It is the caller's responsibility to ensure that :
4951 * - <from> points to a valid delimiter or <start> ;
4952 * - <next> points to a valid delimiter or <end> ;
4953 * - there are non-space chars before <from>.
4954 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004955static int http_del_hdr_value(char *start, char *end, char **from, char *next)
Christopher Faulet0b6bdc52018-10-24 11:05:36 +02004956{
4957 char *prev = *from;
4958
4959 if (prev == start) {
4960 /* We're removing the first value. eat the semicolon, if <next>
4961 * is lower than <end> */
4962 if (next < end)
4963 next++;
4964
4965 while (next < end && HTTP_IS_SPHT(*next))
4966 next++;
4967 }
4968 else {
4969 /* Remove useless spaces before the old delimiter. */
4970 while (HTTP_IS_SPHT(*(prev-1)))
4971 prev--;
4972 *from = prev;
4973
4974 /* copy the delimiter and if possible a space if we're
4975 * not at the end of the line.
4976 */
4977 if (next < end) {
4978 *prev++ = *next++;
4979 if (prev + 1 < next)
4980 *prev++ = ' ';
4981 while (next < end && HTTP_IS_SPHT(*next))
4982 next++;
4983 }
4984 }
4985 memmove(prev, next, end - next);
4986 return (prev - next);
4987}
4988
Christopher Faulet0f226952018-10-22 09:29:56 +02004989
4990/* Formats the start line of the request (without CRLF) and puts it in <str> and
Joseph Herlantc42c0e92018-11-25 10:43:27 -08004991 * return the written length. The line can be truncated if it exceeds <len>.
Christopher Faulet0f226952018-10-22 09:29:56 +02004992 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004993static size_t http_fmt_req_line(const struct htx_sl *sl, char *str, size_t len)
Christopher Faulet0f226952018-10-22 09:29:56 +02004994{
4995 struct ist dst = ist2(str, 0);
4996
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004997 if (istcat(&dst, htx_sl_req_meth(sl), len) == -1)
Christopher Faulet0f226952018-10-22 09:29:56 +02004998 goto end;
4999 if (dst.len + 1 > len)
5000 goto end;
5001 dst.ptr[dst.len++] = ' ';
5002
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005003 if (istcat(&dst, htx_sl_req_uri(sl), len) == -1)
Christopher Faulet0f226952018-10-22 09:29:56 +02005004 goto end;
5005 if (dst.len + 1 > len)
5006 goto end;
5007 dst.ptr[dst.len++] = ' ';
5008
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005009 istcat(&dst, htx_sl_req_vsn(sl), len);
Christopher Faulet0f226952018-10-22 09:29:56 +02005010 end:
5011 return dst.len;
5012}
5013
5014/*
5015 * Print a debug line with a start line.
5016 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005017static void http_debug_stline(const char *dir, struct stream *s, const struct htx_sl *sl)
Christopher Faulet0f226952018-10-22 09:29:56 +02005018{
5019 struct session *sess = strm_sess(s);
5020 int max;
5021
5022 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
5023 dir,
5024 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->handle.fd : -1,
5025 objt_cs(s->si[1].end) ? (unsigned short)objt_cs(s->si[1].end)->conn->handle.fd : -1);
5026
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005027 max = HTX_SL_P1_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02005028 UBOUND(max, trash.size - trash.data - 3);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005029 chunk_memcat(&trash, HTX_SL_P1_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02005030 trash.area[trash.data++] = ' ';
5031
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005032 max = HTX_SL_P2_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02005033 UBOUND(max, trash.size - trash.data - 2);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005034 chunk_memcat(&trash, HTX_SL_P2_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02005035 trash.area[trash.data++] = ' ';
5036
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005037 max = HTX_SL_P3_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02005038 UBOUND(max, trash.size - trash.data - 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005039 chunk_memcat(&trash, HTX_SL_P3_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02005040 trash.area[trash.data++] = '\n';
5041
Willy Tarreau2e8ab6b2020-03-14 11:03:20 +01005042 DISGUISE(write(1, trash.area, trash.data));
Christopher Faulet0f226952018-10-22 09:29:56 +02005043}
5044
5045/*
5046 * Print a debug line with a header.
5047 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005048static 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 +02005049{
5050 struct session *sess = strm_sess(s);
5051 int max;
5052
5053 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
5054 dir,
5055 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->handle.fd : -1,
5056 objt_cs(s->si[1].end) ? (unsigned short)objt_cs(s->si[1].end)->conn->handle.fd : -1);
5057
5058 max = n.len;
5059 UBOUND(max, trash.size - trash.data - 3);
5060 chunk_memcat(&trash, n.ptr, max);
5061 trash.area[trash.data++] = ':';
5062 trash.area[trash.data++] = ' ';
5063
5064 max = v.len;
5065 UBOUND(max, trash.size - trash.data - 1);
5066 chunk_memcat(&trash, v.ptr, max);
5067 trash.area[trash.data++] = '\n';
5068
Willy Tarreau2e8ab6b2020-03-14 11:03:20 +01005069 DISGUISE(write(1, trash.area, trash.data));
Christopher Faulet0f226952018-10-22 09:29:56 +02005070}
5071
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005072/* Allocate a new HTTP transaction for stream <s> unless there is one already.
5073 * In case of allocation failure, everything allocated is freed and NULL is
5074 * returned. Otherwise the new transaction is assigned to the stream and
5075 * returned.
5076 */
5077struct http_txn *http_alloc_txn(struct stream *s)
5078{
5079 struct http_txn *txn = s->txn;
5080
5081 if (txn)
5082 return txn;
5083
5084 txn = pool_alloc(pool_head_http_txn);
5085 if (!txn)
5086 return txn;
5087
5088 s->txn = txn;
5089 return txn;
5090}
5091
5092void http_txn_reset_req(struct http_txn *txn)
5093{
Christopher Faulet1aea50e2020-01-17 16:03:53 +01005094 txn->req.flags = 0;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005095 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
5096}
5097
5098void http_txn_reset_res(struct http_txn *txn)
5099{
Christopher Faulet1aea50e2020-01-17 16:03:53 +01005100 txn->rsp.flags = 0;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005101 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
5102}
5103
5104/*
Christopher Faulet75f619a2021-03-08 19:12:58 +01005105 * Create and initialize a new HTTP transaction for stream <s>. This should be
5106 * used before processing any new request. It returns the transaction or NLULL
5107 * on error.
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005108 */
Christopher Faulet75f619a2021-03-08 19:12:58 +01005109struct http_txn *http_create_txn(struct stream *s)
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005110{
Christopher Faulet75f619a2021-03-08 19:12:58 +01005111 struct http_txn *txn;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005112 struct conn_stream *cs = objt_cs(s->si[0].end);
5113
Christopher Faulet75f619a2021-03-08 19:12:58 +01005114 txn = pool_alloc(pool_head_http_txn);
5115 if (!txn)
5116 return NULL;
5117 s->txn = txn;
5118
Christopher Fauletda831fa2020-10-06 17:58:43 +02005119 txn->flags = ((cs && cs->flags & CS_FL_NOT_FIRST) ? TX_NOT_FIRST : 0);
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005120 txn->status = -1;
Christopher Faulet5cb513a2020-05-13 17:56:56 +02005121 txn->http_reply = NULL;
Willy Tarreau8b507582020-02-25 09:35:07 +01005122 write_u32(txn->cache_hash, 0);
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005123
5124 txn->cookie_first_date = 0;
5125 txn->cookie_last_date = 0;
5126
5127 txn->srv_cookie = NULL;
5128 txn->cli_cookie = NULL;
5129 txn->uri = NULL;
5130
5131 http_txn_reset_req(txn);
5132 http_txn_reset_res(txn);
5133
5134 txn->req.chn = &s->req;
5135 txn->rsp.chn = &s->res;
5136
5137 txn->auth.method = HTTP_AUTH_UNKNOWN;
5138
5139 vars_init(&s->vars_txn, SCOPE_TXN);
5140 vars_init(&s->vars_reqres, SCOPE_REQ);
Christopher Faulet75f619a2021-03-08 19:12:58 +01005141
5142 return txn;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005143}
5144
5145/* to be used at the end of a transaction */
Christopher Faulet75f619a2021-03-08 19:12:58 +01005146void http_destroy_txn(struct stream *s)
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005147{
5148 struct http_txn *txn = s->txn;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005149
5150 /* these ones will have been dynamically allocated */
5151 pool_free(pool_head_requri, txn->uri);
5152 pool_free(pool_head_capture, txn->cli_cookie);
5153 pool_free(pool_head_capture, txn->srv_cookie);
Tim Duesterhusa17e6622020-03-05 20:19:02 +01005154 pool_free(pool_head_uniqueid, s->unique_id.ptr);
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005155
Tim Duesterhusa17e6622020-03-05 20:19:02 +01005156 s->unique_id = IST_NULL;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005157 txn->uri = NULL;
5158 txn->srv_cookie = NULL;
5159 txn->cli_cookie = NULL;
5160
Christopher Faulet59399252019-11-07 14:27:52 +01005161 if (!LIST_ISEMPTY(&s->vars_txn.head))
5162 vars_prune(&s->vars_txn, s->sess, s);
5163 if (!LIST_ISEMPTY(&s->vars_reqres.head))
5164 vars_prune(&s->vars_reqres, s->sess, s);
Christopher Faulet75f619a2021-03-08 19:12:58 +01005165
5166 pool_free(pool_head_http_txn, txn);
5167 s->txn = NULL;
Christopher Faulet59399252019-11-07 14:27:52 +01005168}
5169
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005170
5171DECLARE_POOL(pool_head_http_txn, "http_txn", sizeof(struct http_txn));
Christopher Faulet0f226952018-10-22 09:29:56 +02005172
Christopher Fauletf4eb75d2018-10-11 15:55:07 +02005173__attribute__((constructor))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005174static void __http_protocol_init(void)
Christopher Fauletf4eb75d2018-10-11 15:55:07 +02005175{
5176}
5177
5178
5179/*
5180 * Local variables:
5181 * c-indent-level: 8
5182 * c-basic-offset: 8
5183 * End:
5184 */