blob: a045e53fe04fa322cdd9299dd2b963f2e29a9c1e [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
1371 if (si_b->flags & SI_FL_L7_RETRY &&
1372 (!conn || conn->err_code != CO_ER_SSL_EARLY_FAILED)) {
Olivier Houcharda254a372019-04-05 15:30:12 +02001373 /* If we arrive here, then CF_READ_ERROR was
1374 * set by si_cs_recv() because we matched a
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05001375 * status, otherwise it would have removed
Olivier Houcharda254a372019-04-05 15:30:12 +02001376 * the SI_FL_L7_RETRY flag, so it's ok not
1377 * to check s->be->retry_type.
1378 */
Christopher Faulet5bf85852021-05-21 13:46:14 +02001379 if (co_data(rep) || do_l7_retry(s, si_b) == 0)
Olivier Houcharda254a372019-04-05 15:30:12 +02001380 return 0;
1381 }
1382
Christopher Fauletb1875342021-05-26 12:15:37 +02001383 /* Perform a L7 retry because server refuses the early data. */
1384 if ((si_b->flags & SI_FL_L7_RETRY) &&
1385 (s->be->retry_type & PR_RE_EARLY_ERROR) &&
1386 conn && conn->err_code == CO_ER_SSL_EARLY_FAILED &&
1387 do_l7_retry(s, si_b) == 0) {
1388 DBG_TRACE_DEVEL("leaving on L7 retry",
1389 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
1390 return 0;
1391 }
1392
Olivier Houchard6db16992019-05-17 15:40:49 +02001393 if (txn->flags & TX_NOT_FIRST)
1394 goto abort_keep_alive;
1395
Willy Tarreau4781b152021-04-06 13:53:36 +02001396 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001397 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001398 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001399 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001400 }
1401
Christopher Fauletb1875342021-05-26 12:15:37 +02001402 /* if the server refused the early data, just send a 425 */
1403 if (conn && conn->err_code == CO_ER_SSL_EARLY_FAILED)
Olivier Houchard865d8392019-05-03 22:46:27 +02001404 txn->status = 425;
Christopher Fauletb1875342021-05-26 12:15:37 +02001405 else {
1406 txn->status = 502;
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001407 stream_inc_http_fail_ctr(s);
Christopher Fauletb1875342021-05-26 12:15:37 +02001408 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001409
Christopher Fauletb1875342021-05-26 12:15:37 +02001410 rep->analysers &= AN_RES_FLT_END;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001411 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001412 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001413
1414 if (!(s->flags & SF_ERR_MASK))
1415 s->flags |= SF_ERR_SRVCL;
1416 if (!(s->flags & SF_FINST_MASK))
1417 s->flags |= SF_FINST_H;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001418 DBG_TRACE_DEVEL("leaving on error",
1419 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001420 return 0;
1421 }
1422
Christopher Faulet9768c262018-10-22 09:34:31 +02001423 /* 2: read timeout : return a 504 to the client. */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001424 else if (rep->flags & CF_READ_TIMEOUT) {
Olivier Houcharda254a372019-04-05 15:30:12 +02001425 if ((si_b->flags & SI_FL_L7_RETRY) &&
1426 (s->be->retry_type & PR_RE_TIMEOUT)) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001427 if (co_data(rep) || do_l7_retry(s, si_b) == 0) {
1428 DBG_TRACE_DEVEL("leaving on L7 retry",
1429 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharda254a372019-04-05 15:30:12 +02001430 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001431 }
Olivier Houcharda254a372019-04-05 15:30:12 +02001432 }
Willy Tarreau4781b152021-04-06 13:53:36 +02001433 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001434 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001435 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001436 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001437 }
1438
Christopher Faulete0768eb2018-10-03 16:38:02 +02001439 rep->analysers &= AN_RES_FLT_END;
1440 txn->status = 504;
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001441 stream_inc_http_fail_ctr(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001442 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001443 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001444
1445 if (!(s->flags & SF_ERR_MASK))
1446 s->flags |= SF_ERR_SRVTO;
1447 if (!(s->flags & SF_FINST_MASK))
1448 s->flags |= SF_FINST_H;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001449 DBG_TRACE_DEVEL("leaving on error",
1450 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001451 return 0;
1452 }
1453
Christopher Faulet9768c262018-10-22 09:34:31 +02001454 /* 3: client abort with an abortonclose */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001455 else if ((rep->flags & CF_SHUTR) && ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001456 _HA_ATOMIC_INC(&sess->fe->fe_counters.cli_aborts);
1457 _HA_ATOMIC_INC(&s->be->be_counters.cli_aborts);
William Lallemand36119de2021-03-08 15:26:48 +01001458 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001459 _HA_ATOMIC_INC(&sess->listener->counters->cli_aborts);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001460 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001461 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.cli_aborts);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001462
1463 rep->analysers &= AN_RES_FLT_END;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001464 txn->status = 400;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001465 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001466
1467 if (!(s->flags & SF_ERR_MASK))
1468 s->flags |= SF_ERR_CLICL;
1469 if (!(s->flags & SF_FINST_MASK))
1470 s->flags |= SF_FINST_H;
1471
1472 /* process_stream() will take care of the error */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001473 DBG_TRACE_DEVEL("leaving on error",
1474 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001475 return 0;
1476 }
1477
Christopher Faulet9768c262018-10-22 09:34:31 +02001478 /* 4: close from server, capture the response if the server has started to respond */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001479 else if (rep->flags & CF_SHUTR) {
Olivier Houcharda254a372019-04-05 15:30:12 +02001480 if ((si_b->flags & SI_FL_L7_RETRY) &&
1481 (s->be->retry_type & PR_RE_DISCONNECTED)) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001482 if (co_data(rep) || do_l7_retry(s, si_b) == 0) {
1483 DBG_TRACE_DEVEL("leaving on L7 retry",
1484 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharda254a372019-04-05 15:30:12 +02001485 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001486 }
Olivier Houcharda254a372019-04-05 15:30:12 +02001487 }
1488
Olivier Houchard6db16992019-05-17 15:40:49 +02001489 if (txn->flags & TX_NOT_FIRST)
1490 goto abort_keep_alive;
1491
Willy Tarreau4781b152021-04-06 13:53:36 +02001492 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001493 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001494 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001495 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001496 }
1497
Christopher Faulete0768eb2018-10-03 16:38:02 +02001498 rep->analysers &= AN_RES_FLT_END;
1499 txn->status = 502;
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001500 stream_inc_http_fail_ctr(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001501 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001502 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001503
1504 if (!(s->flags & SF_ERR_MASK))
1505 s->flags |= SF_ERR_SRVCL;
1506 if (!(s->flags & SF_FINST_MASK))
1507 s->flags |= SF_FINST_H;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001508 DBG_TRACE_DEVEL("leaving on error",
1509 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001510 return 0;
1511 }
1512
Christopher Faulet9768c262018-10-22 09:34:31 +02001513 /* 5: write error to client (we don't send any message then) */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001514 else if (rep->flags & CF_WRITE_ERROR) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001515 if (txn->flags & TX_NOT_FIRST)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001516 goto abort_keep_alive;
1517
Willy Tarreau4781b152021-04-06 13:53:36 +02001518 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001519 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001520 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001521 rep->analysers &= AN_RES_FLT_END;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001522
1523 if (!(s->flags & SF_ERR_MASK))
1524 s->flags |= SF_ERR_CLICL;
1525 if (!(s->flags & SF_FINST_MASK))
1526 s->flags |= SF_FINST_H;
1527
1528 /* process_stream() will take care of the error */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001529 DBG_TRACE_DEVEL("leaving on error",
1530 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001531 return 0;
1532 }
1533
1534 channel_dont_close(rep);
1535 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001536 DBG_TRACE_DEVEL("waiting for more data",
1537 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001538 return 0;
1539 }
1540
1541 /* More interesting part now : we know that we have a complete
1542 * response which at least looks like HTTP. We have an indicator
1543 * of each header's length, so we can parse them quickly.
1544 */
Christopher Faulet29f17582019-05-23 11:03:26 +02001545 BUG_ON(htx_get_first_type(htx) != HTX_BLK_RES_SL);
Christopher Faulet297fbb42019-05-13 14:41:27 +02001546 sl = http_get_stline(htx);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001547
Christopher Faulet0f66d552021-05-26 13:14:39 +02001548 /* Perform a L7 retry because of the status code */
1549 if ((si_b->flags & SI_FL_L7_RETRY) &&
1550 l7_status_match(s->be, sl->info.res.status) &&
1551 do_l7_retry(s, si_b) == 0) {
1552 DBG_TRACE_DEVEL("leaving on L7 retry", STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
1553 return 0;
1554 }
1555
1556 /* Now, L7 buffer is useless, it can be released */
1557 b_free(&s->si[1].l7_buffer);
1558
1559 msg->msg_state = HTTP_MSG_BODY;
1560
1561
Christopher Faulet9768c262018-10-22 09:34:31 +02001562 /* 0: we might have to print this header in debug mode */
1563 if (unlikely((global.mode & MODE_DEBUG) &&
1564 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
1565 int32_t pos;
1566
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001567 http_debug_stline("srvrep", s, sl);
Christopher Faulet9768c262018-10-22 09:34:31 +02001568
Christopher Fauleta3f15502019-05-13 15:27:23 +02001569 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001570 struct htx_blk *blk = htx_get_blk(htx, pos);
1571 enum htx_blk_type type = htx_get_blk_type(blk);
1572
1573 if (type == HTX_BLK_EOH)
1574 break;
1575 if (type != HTX_BLK_HDR)
1576 continue;
1577
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001578 http_debug_hdr("srvhdr", s,
1579 htx_get_blk_name(htx, blk),
1580 htx_get_blk_value(htx, blk));
Christopher Faulet9768c262018-10-22 09:34:31 +02001581 }
1582 }
1583
Christopher Faulet03599112018-11-27 11:21:21 +01001584 /* 1: get the status code and the version. Also set HTTP flags */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001585 txn->status = sl->info.res.status;
Christopher Faulet03599112018-11-27 11:21:21 +01001586 if (sl->flags & HTX_SL_F_VER_11)
Christopher Faulet9768c262018-10-22 09:34:31 +02001587 msg->flags |= HTTP_MSGF_VER_11;
Christopher Faulet03599112018-11-27 11:21:21 +01001588 if (sl->flags & HTX_SL_F_XFER_LEN) {
1589 msg->flags |= HTTP_MSGF_XFER_LEN;
Christopher Faulet2a408542020-11-20 14:22:37 +01001590 if (sl->flags & HTX_SL_F_CLEN)
1591 msg->flags |= HTTP_MSGF_CNT_LEN;
1592 else if (sl->flags & HTX_SL_F_CHNK)
1593 msg->flags |= HTTP_MSGF_TE_CHNK;
Christopher Faulet03599112018-11-27 11:21:21 +01001594 }
Christopher Faulet2a408542020-11-20 14:22:37 +01001595 if (sl->flags & HTX_SL_F_BODYLESS)
1596 msg->flags |= HTTP_MSGF_BODYLESS;
Christopher Faulet576c3582021-01-08 15:53:01 +01001597 if (sl->flags & HTX_SL_F_CONN_UPG)
1598 msg->flags |= HTTP_MSGF_CONN_UPG;
Christopher Faulet9768c262018-10-22 09:34:31 +02001599
1600 n = txn->status / 100;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001601 if (n < 1 || n > 5)
1602 n = 0;
Christopher Faulet9768c262018-10-22 09:34:31 +02001603
Christopher Faulete0768eb2018-10-03 16:38:02 +02001604 /* when the client triggers a 4xx from the server, it's most often due
1605 * to a missing object or permission. These events should be tracked
1606 * because if they happen often, it may indicate a brute force or a
1607 * vulnerability scan.
1608 */
1609 if (n == 4)
1610 stream_inc_http_err_ctr(s);
1611
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001612 if (n == 5 && txn->status != 501 && txn->status != 505)
1613 stream_inc_http_fail_ctr(s);
1614
Marcin Deranek3c27dda2020-05-15 18:32:51 +02001615 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001616 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.p.http.rsp[n]);
1617 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.p.http.cum_req);
Marcin Deranek3c27dda2020-05-15 18:32:51 +02001618 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001619
Christopher Faulete0768eb2018-10-03 16:38:02 +02001620 /* Adjust server's health based on status code. Note: status codes 501
1621 * and 505 are triggered on demand by client request, so we must not
1622 * count them as server failures.
1623 */
1624 if (objt_server(s->target)) {
1625 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001626 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_OK);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001627 else
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001628 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_STS);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001629 }
1630
1631 /*
1632 * We may be facing a 100-continue response, or any other informational
1633 * 1xx response which is non-final, in which case this is not the right
1634 * response, and we're waiting for the next one. Let's allow this response
1635 * to go to the client and wait for the next one. There's an exception for
1636 * 101 which is used later in the code to switch protocols.
1637 */
1638 if (txn->status < 200 &&
1639 (txn->status == 100 || txn->status >= 102)) {
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001640 FLT_STRM_CB(s, flt_http_reset(s, msg));
Christopher Faulet421e7692019-06-13 11:16:45 +02001641 htx->first = channel_htx_fwd_headers(rep, htx);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001642 msg->msg_state = HTTP_MSG_RPBEFORE;
Christopher Faulet3499f622019-09-03 15:23:54 +02001643 msg->flags = 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001644 txn->status = 0;
1645 s->logs.t_data = -1; /* was not a response yet */
Christopher Faulet7d518452020-08-31 11:07:07 +02001646 rep->flags |= CF_SEND_DONTWAIT; /* Send ASAP informational messages */
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001647 goto next_one;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001648 }
1649
Christopher Faulet6e6c7b12021-01-08 16:02:05 +01001650 /* A 101-switching-protocols must contains a Connection header with the
1651 * "upgrade" option and the request too. It means both are agree to
1652 * upgrade. It is not so strict because there is no test on the Upgrade
1653 * header content. But it is probably stronger enough for now.
1654 */
1655 if (txn->status == 101 &&
1656 (!(txn->req.flags & HTTP_MSGF_CONN_UPG) || !(txn->rsp.flags & HTTP_MSGF_CONN_UPG)))
1657 goto return_bad_res;
1658
Christopher Faulete0768eb2018-10-03 16:38:02 +02001659 /*
1660 * 2: check for cacheability.
1661 */
1662
1663 switch (txn->status) {
1664 case 200:
1665 case 203:
1666 case 204:
1667 case 206:
1668 case 300:
1669 case 301:
1670 case 404:
1671 case 405:
1672 case 410:
1673 case 414:
1674 case 501:
1675 break;
1676 default:
1677 /* RFC7231#6.1:
1678 * Responses with status codes that are defined as
1679 * cacheable by default (e.g., 200, 203, 204, 206,
1680 * 300, 301, 404, 405, 410, 414, and 501 in this
1681 * specification) can be reused by a cache with
1682 * heuristic expiration unless otherwise indicated
1683 * by the method definition or explicit cache
1684 * controls [RFC7234]; all other status codes are
1685 * not cacheable by default.
1686 */
1687 txn->flags &= ~(TX_CACHEABLE | TX_CACHE_COOK);
1688 break;
1689 }
1690
1691 /*
1692 * 3: we may need to capture headers
1693 */
1694 s->logs.logwait &= ~LW_RESP;
1695 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->res_cap))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001696 http_capture_headers(htx, s->res_cap, sess->fe->rsp_cap);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001697
Christopher Faulet9768c262018-10-22 09:34:31 +02001698 /* Skip parsing if no content length is possible. */
Christopher Fauletc75668e2020-12-07 18:10:32 +01001699 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status >= 200 && txn->status < 300) ||
Christopher Faulete0768eb2018-10-03 16:38:02 +02001700 txn->status == 101)) {
1701 /* Either we've established an explicit tunnel, or we're
1702 * switching the protocol. In both cases, we're very unlikely
1703 * to understand the next protocols. We have to switch to tunnel
1704 * mode, so that we transfer the request and responses then let
1705 * this protocol pass unmodified. When we later implement specific
1706 * parsers for such protocols, we'll want to check the Upgrade
1707 * header which contains information about that protocol for
1708 * responses with status 101 (eg: see RFC2817 about TLS).
1709 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02001710 txn->flags |= TX_CON_WANT_TUN;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001711 }
1712
Christopher Faulet61608322018-11-23 16:23:45 +01001713 /* check for NTML authentication headers in 401 (WWW-Authenticate) and
1714 * 407 (Proxy-Authenticate) responses and set the connection to private
1715 */
1716 srv_conn = cs_conn(objt_cs(s->si[1].end));
1717 if (srv_conn) {
1718 struct ist hdr;
1719 struct http_hdr_ctx ctx;
1720
1721 if (txn->status == 401)
1722 hdr = ist("WWW-Authenticate");
1723 else if (txn->status == 407)
1724 hdr = ist("Proxy-Authenticate");
1725 else
1726 goto end;
1727
1728 ctx.blk = NULL;
1729 while (http_find_header(htx, hdr, &ctx, 0)) {
Willy Tarreauf1dcced2020-05-07 19:27:02 +02001730 /* If www-authenticate contains "Negotiate", "Nego2", or "NTLM",
1731 * possibly followed by blanks and a base64 string, the connection
1732 * is private. Since it's a mess to deal with, we only check for
1733 * values starting with "NTLM" or "Nego". Note that often multiple
1734 * headers are sent by the server there.
1735 */
1736 if ((ctx.value.len >= 4 && strncasecmp(ctx.value.ptr, "Nego", 4) == 0) ||
Willy Tarreau49a1d282020-05-07 19:10:15 +02001737 (ctx.value.len >= 4 && strncasecmp(ctx.value.ptr, "NTLM", 4) == 0)) {
Olivier Houchard250031e2019-05-29 15:01:50 +02001738 sess->flags |= SESS_FL_PREFER_LAST;
Christopher Faulet08016ab2020-07-01 16:10:06 +02001739 conn_set_owner(srv_conn, sess, NULL);
Christopher Faulet21ddc742020-07-01 15:26:14 +02001740 conn_set_private(srv_conn);
Ilya Shipitsin6b79f382020-07-23 00:32:55 +05001741 /* If it fail now, the same will be done in mux->detach() callback */
Christopher Faulet08016ab2020-07-01 16:10:06 +02001742 session_add_conn(srv_conn->owner, srv_conn, srv_conn->target);
Willy Tarreauf1dcced2020-05-07 19:27:02 +02001743 break;
Olivier Houchard250031e2019-05-29 15:01:50 +02001744 }
Christopher Faulet61608322018-11-23 16:23:45 +01001745 }
1746 }
1747
1748 end:
Christopher Faulete0768eb2018-10-03 16:38:02 +02001749 /* we want to have the response time before we start processing it */
1750 s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now);
1751
1752 /* end of job, return OK */
1753 rep->analysers &= ~an_bit;
1754 rep->analyse_exp = TICK_ETERNITY;
1755 channel_auto_close(rep);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001756 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001757 return 1;
1758
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001759 return_int_err:
Willy Tarreau4781b152021-04-06 13:53:36 +02001760 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
1761 _HA_ATOMIC_INC(&s->be->be_counters.internal_errors);
William Lallemand36119de2021-03-08 15:26:48 +01001762 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001763 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Fauletb8a53712019-12-16 11:29:38 +01001764 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001765 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.internal_errors);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001766 txn->status = 500;
1767 if (!(s->flags & SF_ERR_MASK))
1768 s->flags |= SF_ERR_INTERNAL;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001769 goto return_prx_cond;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001770
1771 return_bad_res:
Willy Tarreau4781b152021-04-06 13:53:36 +02001772 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Faulet47365272018-10-31 17:40:50 +01001773 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001774 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001775 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Christopher Faulet47365272018-10-31 17:40:50 +01001776 }
Olivier Houcharde3249a92019-05-03 23:01:47 +02001777 if ((s->be->retry_type & PR_RE_JUNK_REQUEST) &&
Olivier Houchardad26d8d2019-05-10 17:48:28 +02001778 (si_b->flags & SI_FL_L7_RETRY) &&
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001779 do_l7_retry(s, si_b) == 0) {
1780 DBG_TRACE_DEVEL("leaving on L7 retry",
1781 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharde3249a92019-05-03 23:01:47 +02001782 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001783 }
Christopher Faulet47365272018-10-31 17:40:50 +01001784 txn->status = 502;
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001785 stream_inc_http_fail_ctr(s);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001786 /* fall through */
1787
Christopher Fauletb8a53712019-12-16 11:29:38 +01001788 return_prx_cond:
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001789 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet47365272018-10-31 17:40:50 +01001790
1791 if (!(s->flags & SF_ERR_MASK))
1792 s->flags |= SF_ERR_PRXCOND;
1793 if (!(s->flags & SF_FINST_MASK))
1794 s->flags |= SF_FINST_H;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001795
1796 s->si[1].flags |= SI_FL_NOLINGER;
1797 rep->analysers &= AN_RES_FLT_END;
Christopher Faulete58c0002020-03-02 16:21:01 +01001798 s->req.analysers &= AN_REQ_FLT_END;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001799 rep->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001800 DBG_TRACE_DEVEL("leaving on error",
1801 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulet47365272018-10-31 17:40:50 +01001802 return 0;
1803
Christopher Faulete0768eb2018-10-03 16:38:02 +02001804 abort_keep_alive:
1805 /* A keep-alive request to the server failed on a network error.
1806 * The client is required to retry. We need to close without returning
1807 * any other information so that the client retries.
1808 */
1809 txn->status = 0;
1810 rep->analysers &= AN_RES_FLT_END;
1811 s->req.analysers &= AN_REQ_FLT_END;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001812 s->logs.logwait = 0;
1813 s->logs.level = 0;
1814 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001815 http_reply_and_close(s, txn->status, NULL);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001816 DBG_TRACE_DEVEL("leaving by closing K/A connection",
1817 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001818 return 0;
1819}
1820
1821/* This function performs all the processing enabled for the current response.
1822 * It normally returns 1 unless it wants to break. It relies on buffers flags,
1823 * and updates s->res.analysers. It might make sense to explode it into several
1824 * other functions. It works like process_request (see indications above).
1825 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001826int http_process_res_common(struct stream *s, struct channel *rep, int an_bit, struct proxy *px)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001827{
1828 struct session *sess = s->sess;
1829 struct http_txn *txn = s->txn;
1830 struct http_msg *msg = &txn->rsp;
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001831 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001832 struct proxy *cur_proxy;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001833 enum rule_result ret = HTTP_RULE_RES_CONT;
1834
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001835 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
1836 return 0;
Christopher Faulet9768c262018-10-22 09:34:31 +02001837
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001838 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001839
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001840 htx = htxbuf(&rep->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001841
1842 /* The stats applet needs to adjust the Connection header but we don't
1843 * apply any filter there.
1844 */
1845 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
1846 rep->analysers &= ~an_bit;
1847 rep->analyse_exp = TICK_ETERNITY;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001848 goto end;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001849 }
1850
1851 /*
1852 * We will have to evaluate the filters.
1853 * As opposed to version 1.2, now they will be evaluated in the
1854 * filters order and not in the header order. This means that
1855 * each filter has to be validated among all headers.
1856 *
1857 * Filters are tried with ->be first, then with ->fe if it is
1858 * different from ->be.
1859 *
1860 * Maybe we are in resume condiion. In this case I choose the
1861 * "struct proxy" which contains the rule list matching the resume
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05001862 * pointer. If none of these "struct proxy" match, I initialise
Christopher Faulete0768eb2018-10-03 16:38:02 +02001863 * the process with the first one.
1864 *
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05001865 * In fact, I check only correspondence between the current list
Christopher Faulete0768eb2018-10-03 16:38:02 +02001866 * pointer and the ->fe rule list. If it doesn't match, I initialize
1867 * the loop with the ->be.
1868 */
1869 if (s->current_rule_list == &sess->fe->http_res_rules)
1870 cur_proxy = sess->fe;
1871 else
1872 cur_proxy = s->be;
1873 while (1) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001874 /* evaluate http-response rules */
1875 if (ret == HTTP_RULE_RES_CONT) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001876 ret = http_res_get_intercept_rule(cur_proxy, &cur_proxy->http_res_rules, s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001877
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001878 switch (ret) {
1879 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
1880 goto return_prx_yield;
1881
1882 case HTTP_RULE_RES_CONT:
1883 case HTTP_RULE_RES_STOP: /* nothing to do */
1884 break;
1885
1886 case HTTP_RULE_RES_DENY: /* deny or tarpit */
1887 goto deny;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001888
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001889 case HTTP_RULE_RES_ABRT: /* abort request, response already sent */
1890 goto return_prx_cond;
1891
1892 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
Christopher Fauletb8a53712019-12-16 11:29:38 +01001893 goto done;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001894
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001895 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
1896 goto return_bad_res;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001897
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001898 case HTTP_RULE_RES_ERROR: /* failed with a bad request */
1899 goto return_int_err;
1900 }
1901
1902 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001903
Christopher Faulete0768eb2018-10-03 16:38:02 +02001904 /* check whether we're already working on the frontend */
1905 if (cur_proxy == sess->fe)
1906 break;
1907 cur_proxy = sess->fe;
1908 }
1909
Christopher Faulete0768eb2018-10-03 16:38:02 +02001910 /* OK that's all we can do for 1xx responses */
1911 if (unlikely(txn->status < 200 && txn->status != 101))
Christopher Fauletf2824e62018-10-01 12:12:37 +02001912 goto end;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001913
1914 /*
1915 * Now check for a server cookie.
1916 */
1917 if (s->be->cookie_name || sess->fe->capture_name || (s->be->options & PR_O_CHK_CACHE))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001918 http_manage_server_side_cookies(s, rep);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001919
1920 /*
1921 * Check for cache-control or pragma headers if required.
1922 */
1923 if ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001924 http_check_response_for_cacheability(s, rep);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001925
1926 /*
1927 * Add server cookie in the response if needed
1928 */
1929 if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) &&
1930 !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) &&
1931 (!(s->flags & SF_DIRECT) ||
1932 ((s->be->cookie_maxidle || txn->cookie_last_date) &&
1933 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
1934 (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
1935 (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
1936 (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
1937 !(s->flags & SF_IGNORE_PRST)) {
1938 /* the server is known, it's not the one the client requested, or the
1939 * cookie's last seen date needs to be refreshed. We have to
1940 * insert a set-cookie here, except if we want to insert only on POST
1941 * requests and this one isn't. Note that servers which don't have cookies
1942 * (eg: some backup servers) will return a full cookie removal request.
1943 */
1944 if (!objt_server(s->target)->cookie) {
1945 chunk_printf(&trash,
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001946 "%s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
Christopher Faulete0768eb2018-10-03 16:38:02 +02001947 s->be->cookie_name);
1948 }
1949 else {
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001950 chunk_printf(&trash, "%s=%s", s->be->cookie_name, objt_server(s->target)->cookie);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001951
1952 if (s->be->cookie_maxidle || s->be->cookie_maxlife) {
1953 /* emit last_date, which is mandatory */
1954 trash.area[trash.data++] = COOKIE_DELIM_DATE;
1955 s30tob64((date.tv_sec+3) >> 2,
1956 trash.area + trash.data);
1957 trash.data += 5;
1958
1959 if (s->be->cookie_maxlife) {
1960 /* emit first_date, which is either the original one or
1961 * the current date.
1962 */
1963 trash.area[trash.data++] = COOKIE_DELIM_DATE;
1964 s30tob64(txn->cookie_first_date ?
1965 txn->cookie_first_date >> 2 :
1966 (date.tv_sec+3) >> 2,
1967 trash.area + trash.data);
1968 trash.data += 5;
1969 }
1970 }
1971 chunk_appendf(&trash, "; path=/");
1972 }
1973
1974 if (s->be->cookie_domain)
1975 chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain);
1976
1977 if (s->be->ck_opts & PR_CK_HTTPONLY)
1978 chunk_appendf(&trash, "; HttpOnly");
1979
1980 if (s->be->ck_opts & PR_CK_SECURE)
1981 chunk_appendf(&trash, "; Secure");
1982
Christopher Faulet2f533902020-01-21 11:06:48 +01001983 if (s->be->cookie_attrs)
1984 chunk_appendf(&trash, "; %s", s->be->cookie_attrs);
1985
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001986 if (unlikely(!http_add_header(htx, ist("Set-Cookie"), ist2(trash.area, trash.data))))
Christopher Fauletb8a53712019-12-16 11:29:38 +01001987 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001988
1989 txn->flags &= ~TX_SCK_MASK;
1990 if (__objt_server(s->target)->cookie && (s->flags & SF_DIRECT))
1991 /* the server did not change, only the date was updated */
1992 txn->flags |= TX_SCK_UPDATED;
1993 else
1994 txn->flags |= TX_SCK_INSERTED;
1995
1996 /* Here, we will tell an eventual cache on the client side that we don't
1997 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
1998 * Some caches understand the correct form: 'no-cache="set-cookie"', but
1999 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
2000 */
2001 if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
2002
2003 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
2004
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002005 if (unlikely(!http_add_header(htx, ist("Cache-control"), ist("private"))))
Christopher Fauletb8a53712019-12-16 11:29:38 +01002006 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002007 }
2008 }
2009
2010 /*
2011 * Check if result will be cacheable with a cookie.
2012 * We'll block the response if security checks have caught
2013 * nasty things such as a cacheable cookie.
2014 */
2015 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
2016 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
2017 (s->be->options & PR_O_CHK_CACHE)) {
2018 /* we're in presence of a cacheable response containing
2019 * a set-cookie header. We'll block it as requested by
2020 * the 'checkcache' option, and send an alert.
2021 */
Christopher Faulete0768eb2018-10-03 16:38:02 +02002022 ha_alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
2023 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
2024 send_log(s->be, LOG_ALERT,
2025 "Blocking cacheable cookie in response from instance %s, server %s.\n",
2026 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
Christopher Fauletb8a53712019-12-16 11:29:38 +01002027 goto deny;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002028 }
2029
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002030 end:
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002031 /*
2032 * Evaluate after-response rules before forwarding the response. rules
2033 * from the backend are evaluated first, then one from the frontend if
2034 * it differs.
2035 */
2036 if (!http_eval_after_res_rules(s))
2037 goto return_int_err;
2038
Christopher Fauletc2ac5e42021-03-08 18:20:09 +01002039 /* Filter the response headers if there are filters attached to the
2040 * stream.
2041 */
2042 if (HAS_FILTERS(s))
2043 rep->analysers |= AN_RES_FLT_HTTP_HDRS;
2044
Christopher Faulete0768eb2018-10-03 16:38:02 +02002045 /* Always enter in the body analyzer */
2046 rep->analysers &= ~AN_RES_FLT_XFER_DATA;
2047 rep->analysers |= AN_RES_HTTP_XFER_BODY;
2048
2049 /* if the user wants to log as soon as possible, without counting
2050 * bytes from the server, then this is the right moment. We have
2051 * to temporarily assign bytes_out to log what we currently have.
2052 */
2053 if (!LIST_ISEMPTY(&sess->fe->logformat) && !(s->logs.logwait & LW_BYTES)) {
2054 s->logs.t_close = s->logs.t_data; /* to get a valid end date */
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002055 s->logs.bytes_out = htx->data;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002056 s->do_log(s);
2057 s->logs.bytes_out = 0;
2058 }
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002059
Christopher Fauletb8a53712019-12-16 11:29:38 +01002060 done:
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002061 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002062 rep->analysers &= ~an_bit;
2063 rep->analyse_exp = TICK_ETERNITY;
2064 return 1;
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002065
Christopher Fauletb8a53712019-12-16 11:29:38 +01002066 deny:
Willy Tarreau4781b152021-04-06 13:53:36 +02002067 _HA_ATOMIC_INC(&sess->fe->fe_counters.denied_resp);
2068 _HA_ATOMIC_INC(&s->be->be_counters.denied_resp);
William Lallemand36119de2021-03-08 15:26:48 +01002069 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02002070 _HA_ATOMIC_INC(&sess->listener->counters->denied_resp);
Christopher Fauleta08546b2019-12-16 16:07:34 +01002071 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02002072 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.denied_resp);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002073 goto return_prx_err;
2074
2075 return_int_err:
2076 txn->status = 500;
2077 if (!(s->flags & SF_ERR_MASK))
2078 s->flags |= SF_ERR_INTERNAL;
Willy Tarreau4781b152021-04-06 13:53:36 +02002079 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
2080 _HA_ATOMIC_INC(&s->be->be_counters.internal_errors);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002081 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02002082 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.internal_errors);
Christopher Fauletcff0f732019-12-16 16:13:44 +01002083 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02002084 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.internal_errors);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002085 goto return_prx_err;
2086
2087 return_bad_res:
2088 txn->status = 502;
Willy Tarreau826f3ab2021-02-10 12:07:15 +01002089 stream_inc_http_fail_ctr(s);
Willy Tarreau4781b152021-04-06 13:53:36 +02002090 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Fauleta20a6532020-02-05 10:16:41 +01002091 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02002092 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Christopher Fauleta20a6532020-02-05 10:16:41 +01002093 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_RSP);
2094 }
Christopher Fauletb8a53712019-12-16 11:29:38 +01002095 /* fall through */
2096
2097 return_prx_err:
2098 http_reply_and_close(s, txn->status, http_error_message(s));
2099 /* fall through */
2100
2101 return_prx_cond:
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002102 s->logs.t_data = -1; /* was not a valid response */
2103 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002104
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002105 if (!(s->flags & SF_ERR_MASK))
2106 s->flags |= SF_ERR_PRXCOND;
2107 if (!(s->flags & SF_FINST_MASK))
2108 s->flags |= SF_FINST_H;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002109
Christopher Faulete58c0002020-03-02 16:21:01 +01002110 rep->analysers &= AN_RES_FLT_END;
2111 s->req.analysers &= AN_REQ_FLT_END;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002112 rep->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002113 DBG_TRACE_DEVEL("leaving on error",
2114 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002115 return 0;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002116
2117 return_prx_yield:
2118 channel_dont_close(rep);
2119 DBG_TRACE_DEVEL("waiting for more data",
2120 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
2121 return 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002122}
2123
2124/* This function is an analyser which forwards response body (including chunk
2125 * sizes if any). It is called as soon as we must forward, even if we forward
2126 * zero byte. The only situation where it must not be called is when we're in
2127 * tunnel mode and we want to forward till the close. It's used both to forward
2128 * remaining data and to resync after end of body. It expects the msg_state to
2129 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
2130 * read more data, or 1 once we can go on with next request or end the stream.
2131 *
2132 * It is capable of compressing response data both in content-length mode and
2133 * in chunked mode. The state machines follows different flows depending on
2134 * whether content-length and chunked modes are used, since there are no
2135 * trailers in content-length :
2136 *
2137 * chk-mode cl-mode
2138 * ,----- BODY -----.
2139 * / \
2140 * V size > 0 V chk-mode
2141 * .--> SIZE -------------> DATA -------------> CRLF
2142 * | | size == 0 | last byte |
2143 * | v final crlf v inspected |
2144 * | TRAILERS -----------> DONE |
2145 * | |
2146 * `----------------------------------------------'
2147 *
2148 * Compression only happens in the DATA state, and must be flushed in final
2149 * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding
2150 * is performed at once on final states for all bytes parsed, or when leaving
2151 * on missing data.
2152 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002153int http_response_forward_body(struct stream *s, struct channel *res, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +02002154{
2155 struct session *sess = s->sess;
2156 struct http_txn *txn = s->txn;
2157 struct http_msg *msg = &s->txn->rsp;
Christopher Faulet9768c262018-10-22 09:34:31 +02002158 struct htx *htx;
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002159 int ret;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002160
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002161 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002162
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002163 htx = htxbuf(&res->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002164
Christopher Fauletb9a92f32019-09-09 10:15:21 +02002165 if (htx->flags & HTX_FL_PARSING_ERROR)
2166 goto return_bad_res;
2167 if (htx->flags & HTX_FL_PROCESSING_ERROR)
2168 goto return_int_err;
2169
Christopher Faulete0768eb2018-10-03 16:38:02 +02002170 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Christopher Fauletf2824e62018-10-01 12:12:37 +02002171 ((res->flags & CF_SHUTW) && (res->to_forward || co_data(res)))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02002172 /* Output closed while we were sending data. We must abort and
2173 * wake the other side up.
2174 */
Christopher Faulete0768eb2018-10-03 16:38:02 +02002175 msg->msg_state = HTTP_MSG_ERROR;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002176 http_end_response(s);
2177 http_end_request(s);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002178 DBG_TRACE_DEVEL("leaving on error",
2179 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002180 return 1;
2181 }
2182
Christopher Faulet9768c262018-10-22 09:34:31 +02002183 if (msg->msg_state == HTTP_MSG_BODY)
2184 msg->msg_state = HTTP_MSG_DATA;
2185
Christopher Faulete0768eb2018-10-03 16:38:02 +02002186 /* in most states, we should abort in case of early close */
2187 channel_auto_close(res);
2188
Christopher Faulete0768eb2018-10-03 16:38:02 +02002189 if (res->to_forward) {
Christopher Faulet66af0b22019-03-22 14:54:52 +01002190 if (res->to_forward == CHN_INFINITE_FORWARD) {
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002191 if (res->flags & CF_EOI)
2192 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet66af0b22019-03-22 14:54:52 +01002193 }
2194 else {
2195 /* We can't process the buffer's contents yet */
2196 res->flags |= CF_WAKE_WRITE;
2197 goto missing_data_or_waiting;
2198 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02002199 }
2200
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002201 if (msg->msg_state >= HTTP_MSG_ENDING)
2202 goto ending;
2203
Christopher Fauletc75668e2020-12-07 18:10:32 +01002204 if ((txn->meth == HTTP_METH_CONNECT && txn->status >= 200 && txn->status < 300) || txn->status == 101 ||
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002205 (!(msg->flags & HTTP_MSGF_XFER_LEN) && !HAS_RSP_DATA_FILTERS(s))) {
2206 msg->msg_state = HTTP_MSG_ENDING;
2207 goto ending;
2208 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002209
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002210 /* Forward input data. We get it by removing all outgoing data not
2211 * forwarded yet from HTX data size. If there are some data filters, we
2212 * let them decide the amount of data to forward.
Christopher Faulet9768c262018-10-22 09:34:31 +02002213 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002214 if (HAS_RSP_DATA_FILTERS(s)) {
2215 ret = flt_http_payload(s, msg, htx->data);
2216 if (ret < 0)
2217 goto return_bad_res;
Christopher Faulet421e7692019-06-13 11:16:45 +02002218 c_adv(res, ret);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002219 }
2220 else {
Christopher Faulet421e7692019-06-13 11:16:45 +02002221 c_adv(res, htx->data - co_data(res));
Christopher Faulet66af0b22019-03-22 14:54:52 +01002222 if (msg->flags & HTTP_MSGF_XFER_LEN)
2223 channel_htx_forward_forever(res, htx);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002224 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002225
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002226 if (htx->data != co_data(res))
2227 goto missing_data_or_waiting;
2228
2229 if (!(msg->flags & HTTP_MSGF_XFER_LEN) && res->flags & CF_SHUTR) {
2230 msg->msg_state = HTTP_MSG_ENDING;
2231 goto ending;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002232 }
2233
Christopher Faulet9768c262018-10-22 09:34:31 +02002234 /* Check if the end-of-message is reached and if so, switch the message
Christopher Fauletd20fdb02019-06-13 16:43:22 +02002235 * in HTTP_MSG_ENDING state. Then if all data was marked to be
2236 * forwarded, set the state to HTTP_MSG_DONE.
Christopher Faulet9768c262018-10-22 09:34:31 +02002237 */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002238 if (!(htx->flags & HTX_FL_EOM))
Christopher Faulet9768c262018-10-22 09:34:31 +02002239 goto missing_data_or_waiting;
2240
Christopher Fauletd20fdb02019-06-13 16:43:22 +02002241 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet9768c262018-10-22 09:34:31 +02002242
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002243 ending:
Christopher Faulet2151cdd2020-07-22 16:34:59 +02002244 res->flags &= ~CF_EXPECT_MORE; /* no more data are expected */
2245
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002246 /* other states, ENDING...TUNNEL */
2247 if (msg->msg_state >= HTTP_MSG_DONE)
2248 goto done;
Christopher Faulet9768c262018-10-22 09:34:31 +02002249
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002250 if (HAS_RSP_DATA_FILTERS(s)) {
2251 ret = flt_http_end(s, msg);
2252 if (ret <= 0) {
2253 if (!ret)
2254 goto missing_data_or_waiting;
2255 goto return_bad_res;
2256 }
2257 }
2258
Christopher Fauletc75668e2020-12-07 18:10:32 +01002259 if ((txn->meth == HTTP_METH_CONNECT && txn->status >= 200 && txn->status < 300) || txn->status == 101 ||
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002260 !(msg->flags & HTTP_MSGF_XFER_LEN)) {
2261 msg->msg_state = HTTP_MSG_TUNNEL;
2262 goto ending;
2263 }
2264 else {
2265 msg->msg_state = HTTP_MSG_DONE;
2266 res->to_forward = 0;
2267 }
2268
2269 done:
2270
2271 channel_dont_close(res);
2272
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002273 http_end_response(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002274 if (!(res->analysers & an_bit)) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002275 http_end_request(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002276 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
2277 if (res->flags & CF_SHUTW) {
2278 /* response errors are most likely due to the
2279 * client aborting the transfer. */
Christopher Faulet93e02d82019-03-08 14:18:50 +01002280 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002281 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02002282 goto return_bad_res;
2283 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002284 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002285 return 1;
2286 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002287 DBG_TRACE_DEVEL("waiting for the end of the HTTP txn",
2288 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002289 return 0;
2290
2291 missing_data_or_waiting:
2292 if (res->flags & CF_SHUTW)
Christopher Faulet93e02d82019-03-08 14:18:50 +01002293 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002294
2295 /* stop waiting for data if the input is closed before the end. If the
2296 * client side was already closed, it means that the client has aborted,
2297 * so we don't want to count this as a server abort. Otherwise it's a
2298 * server abort.
2299 */
Christopher Fauletd20fdb02019-06-13 16:43:22 +02002300 if (msg->msg_state < HTTP_MSG_ENDING && res->flags & CF_SHUTR) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02002301 if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))
Christopher Faulet93e02d82019-03-08 14:18:50 +01002302 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002303 /* If we have some pending data, we continue the processing */
Christopher Faulet93e02d82019-03-08 14:18:50 +01002304 if (htx_is_empty(htx))
2305 goto return_srv_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002306 }
2307
Christopher Faulete0768eb2018-10-03 16:38:02 +02002308 /* When TE: chunked is used, we need to get there again to parse
2309 * remaining chunks even if the server has closed, so we don't want to
Christopher Faulet9768c262018-10-22 09:34:31 +02002310 * set CF_DONTCLOSE. Similarly when there is a content-leng or if there
2311 * are filters registered on the stream, we don't want to forward a
2312 * close
Christopher Faulete0768eb2018-10-03 16:38:02 +02002313 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002314 if ((msg->flags & HTTP_MSGF_XFER_LEN) || HAS_RSP_DATA_FILTERS(s))
Christopher Faulete0768eb2018-10-03 16:38:02 +02002315 channel_dont_close(res);
2316
2317 /* We know that more data are expected, but we couldn't send more that
2318 * what we did. So we always set the CF_EXPECT_MORE flag so that the
2319 * system knows it must not set a PUSH on this first part. Interactive
2320 * modes are already handled by the stream sock layer. We must not do
2321 * this in content-length mode because it could present the MSG_MORE
2322 * flag with the last block of forwarded data, which would cause an
2323 * additional delay to be observed by the receiver.
2324 */
Christopher Faulet2151cdd2020-07-22 16:34:59 +02002325 if (HAS_RSP_DATA_FILTERS(s))
Christopher Faulete0768eb2018-10-03 16:38:02 +02002326 res->flags |= CF_EXPECT_MORE;
2327
2328 /* the stream handler will take care of timeouts and errors */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002329 DBG_TRACE_DEVEL("waiting for more data to forward",
2330 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002331 return 0;
2332
Christopher Faulet93e02d82019-03-08 14:18:50 +01002333 return_srv_abort:
Willy Tarreau4781b152021-04-06 13:53:36 +02002334 _HA_ATOMIC_INC(&sess->fe->fe_counters.srv_aborts);
2335 _HA_ATOMIC_INC(&s->be->be_counters.srv_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->srv_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.srv_aborts);
Willy Tarreau826f3ab2021-02-10 12:07:15 +01002340 stream_inc_http_fail_ctr(s);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002341 if (!(s->flags & SF_ERR_MASK))
2342 s->flags |= SF_ERR_SRVCL;
2343 goto return_error;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002344
Christopher Faulet93e02d82019-03-08 14:18:50 +01002345 return_cli_abort:
Willy Tarreau4781b152021-04-06 13:53:36 +02002346 _HA_ATOMIC_INC(&sess->fe->fe_counters.cli_aborts);
2347 _HA_ATOMIC_INC(&s->be->be_counters.cli_aborts);
William Lallemand36119de2021-03-08 15:26:48 +01002348 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02002349 _HA_ATOMIC_INC(&sess->listener->counters->cli_aborts);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002350 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02002351 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.cli_aborts);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002352 if (!(s->flags & SF_ERR_MASK))
2353 s->flags |= SF_ERR_CLICL;
2354 goto return_error;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002355
Christopher Fauletb9a92f32019-09-09 10:15:21 +02002356 return_int_err:
Willy Tarreau4781b152021-04-06 13:53:36 +02002357 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
2358 _HA_ATOMIC_INC(&s->be->be_counters.internal_errors);
William Lallemand36119de2021-03-08 15:26:48 +01002359 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02002360 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002361 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02002362 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.internal_errors);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02002363 if (!(s->flags & SF_ERR_MASK))
2364 s->flags |= SF_ERR_INTERNAL;
2365 goto return_error;
2366
Christopher Faulet93e02d82019-03-08 14:18:50 +01002367 return_bad_res:
Willy Tarreau4781b152021-04-06 13:53:36 +02002368 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002369 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02002370 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002371 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_RSP);
2372 }
Willy Tarreau826f3ab2021-02-10 12:07:15 +01002373 stream_inc_http_fail_ctr(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002374 if (!(s->flags & SF_ERR_MASK))
Christopher Faulet93e02d82019-03-08 14:18:50 +01002375 s->flags |= SF_ERR_SRVCL;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002376 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +02002377
Christopher Faulet93e02d82019-03-08 14:18:50 +01002378 return_error:
Christopher Faulete0768eb2018-10-03 16:38:02 +02002379 /* don't send any error message as we're in the body */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002380 http_reply_and_close(s, txn->status, NULL);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002381 res->analysers &= AN_RES_FLT_END;
2382 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 +02002383 if (!(s->flags & SF_FINST_MASK))
2384 s->flags |= SF_FINST_D;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002385 DBG_TRACE_DEVEL("leaving on error",
2386 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002387 return 0;
2388}
2389
Christopher Fauletf2824e62018-10-01 12:12:37 +02002390/* Perform an HTTP redirect based on the information in <rule>. The function
Christopher Faulet99daf282018-11-28 22:58:13 +01002391 * returns zero on success, or zero in case of a, irrecoverable error such
Christopher Fauletf2824e62018-10-01 12:12:37 +02002392 * as too large a request to build a valid response.
2393 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002394int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn)
Christopher Fauletf2824e62018-10-01 12:12:37 +02002395{
Christopher Faulet99daf282018-11-28 22:58:13 +01002396 struct channel *req = &s->req;
2397 struct channel *res = &s->res;
2398 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01002399 struct htx_sl *sl;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002400 struct buffer *chunk;
Christopher Faulet99daf282018-11-28 22:58:13 +01002401 struct ist status, reason, location;
2402 unsigned int flags;
Christopher Faulet08e66462019-05-23 16:44:59 +02002403 int close = 0; /* Try to keep the connection alive byt default */
Christopher Fauletf2824e62018-10-01 12:12:37 +02002404
2405 chunk = alloc_trash_chunk();
Christopher Fauletb8a53712019-12-16 11:29:38 +01002406 if (!chunk) {
2407 if (!(s->flags & SF_ERR_MASK))
2408 s->flags |= SF_ERR_RESOURCE;
Christopher Faulet99daf282018-11-28 22:58:13 +01002409 goto fail;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002410 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002411
Christopher Faulet99daf282018-11-28 22:58:13 +01002412 /*
2413 * Create the location
2414 */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002415 htx = htxbuf(&req->buf);
Christopher Fauletf2824e62018-10-01 12:12:37 +02002416 switch(rule->type) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002417 case REDIRECT_TYPE_SCHEME: {
2418 struct http_hdr_ctx ctx;
2419 struct ist path, host;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002420
Christopher Faulet99daf282018-11-28 22:58:13 +01002421 host = ist("");
2422 ctx.blk = NULL;
2423 if (http_find_header(htx, ist("Host"), &ctx, 0))
2424 host = ctx.value;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002425
Christopher Faulet297fbb42019-05-13 14:41:27 +02002426 sl = http_get_stline(htx);
Christopher Faulet99daf282018-11-28 22:58:13 +01002427 path = http_get_path(htx_sl_req_uri(sl));
2428 /* build message using path */
Tim Duesterhused526372020-03-05 17:56:33 +01002429 if (isttest(path)) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002430 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2431 int qs = 0;
2432 while (qs < path.len) {
2433 if (*(path.ptr + qs) == '?') {
2434 path.len = qs;
2435 break;
2436 }
2437 qs++;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002438 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002439 }
2440 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002441 else
2442 path = ist("/");
Christopher Fauletf2824e62018-10-01 12:12:37 +02002443
Christopher Faulet99daf282018-11-28 22:58:13 +01002444 if (rule->rdr_str) { /* this is an old "redirect" rule */
2445 /* add scheme */
2446 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2447 goto fail;
2448 }
2449 else {
2450 /* add scheme with executing log format */
2451 chunk->data += build_logline(s, chunk->area + chunk->data,
2452 chunk->size - chunk->data,
2453 &rule->rdr_fmt);
2454 }
2455 /* add "://" + host + path */
2456 if (!chunk_memcat(chunk, "://", 3) ||
2457 !chunk_memcat(chunk, host.ptr, host.len) ||
2458 !chunk_memcat(chunk, path.ptr, path.len))
2459 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002460
Christopher Faulet99daf282018-11-28 22:58:13 +01002461 /* append a slash at the end of the location if needed and missing */
2462 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
2463 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
2464 if (chunk->data + 1 >= chunk->size)
2465 goto fail;
2466 chunk->area[chunk->data++] = '/';
2467 }
2468 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002469 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002470
Christopher Faulet99daf282018-11-28 22:58:13 +01002471 case REDIRECT_TYPE_PREFIX: {
2472 struct ist path;
2473
Christopher Faulet297fbb42019-05-13 14:41:27 +02002474 sl = http_get_stline(htx);
Christopher Faulet99daf282018-11-28 22:58:13 +01002475 path = http_get_path(htx_sl_req_uri(sl));
2476 /* build message using path */
Tim Duesterhused526372020-03-05 17:56:33 +01002477 if (isttest(path)) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002478 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2479 int qs = 0;
2480 while (qs < path.len) {
2481 if (*(path.ptr + qs) == '?') {
2482 path.len = qs;
2483 break;
2484 }
2485 qs++;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002486 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002487 }
2488 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002489 else
2490 path = ist("/");
Christopher Fauletf2824e62018-10-01 12:12:37 +02002491
Christopher Faulet99daf282018-11-28 22:58:13 +01002492 if (rule->rdr_str) { /* this is an old "redirect" rule */
2493 /* add prefix. Note that if prefix == "/", we don't want to
2494 * add anything, otherwise it makes it hard for the user to
2495 * configure a self-redirection.
2496 */
2497 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
2498 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2499 goto fail;
2500 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002501 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002502 else {
2503 /* add prefix with executing log format */
2504 chunk->data += build_logline(s, chunk->area + chunk->data,
2505 chunk->size - chunk->data,
2506 &rule->rdr_fmt);
2507 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002508
Christopher Faulet99daf282018-11-28 22:58:13 +01002509 /* add path */
2510 if (!chunk_memcat(chunk, path.ptr, path.len))
2511 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002512
Christopher Faulet99daf282018-11-28 22:58:13 +01002513 /* append a slash at the end of the location if needed and missing */
2514 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
2515 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
2516 if (chunk->data + 1 >= chunk->size)
2517 goto fail;
2518 chunk->area[chunk->data++] = '/';
2519 }
2520 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002521 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002522 case REDIRECT_TYPE_LOCATION:
2523 default:
2524 if (rule->rdr_str) { /* this is an old "redirect" rule */
2525 /* add location */
2526 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2527 goto fail;
2528 }
2529 else {
2530 /* add location with executing log format */
2531 chunk->data += build_logline(s, chunk->area + chunk->data,
2532 chunk->size - chunk->data,
2533 &rule->rdr_fmt);
2534 }
2535 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002536 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002537 location = ist2(chunk->area, chunk->data);
2538
2539 /*
2540 * Create the 30x response
2541 */
2542 switch (rule->code) {
2543 case 308:
2544 status = ist("308");
2545 reason = ist("Permanent Redirect");
2546 break;
2547 case 307:
2548 status = ist("307");
2549 reason = ist("Temporary Redirect");
2550 break;
2551 case 303:
2552 status = ist("303");
2553 reason = ist("See Other");
2554 break;
2555 case 301:
2556 status = ist("301");
2557 reason = ist("Moved Permanently");
2558 break;
2559 case 302:
2560 default:
2561 status = ist("302");
2562 reason = ist("Found");
2563 break;
2564 }
2565
Christopher Faulet08e66462019-05-23 16:44:59 +02002566 if (!(txn->req.flags & HTTP_MSGF_BODYLESS) && txn->req.msg_state != HTTP_MSG_DONE)
2567 close = 1;
2568
Christopher Faulet99daf282018-11-28 22:58:13 +01002569 htx = htx_from_buf(&res->buf);
Kevin Zhu96b36392020-01-07 09:42:55 +01002570 /* Trim any possible response */
2571 channel_htx_truncate(&s->res, htx);
Christopher Faulet99daf282018-11-28 22:58:13 +01002572 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
2573 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), status, reason);
2574 if (!sl)
2575 goto fail;
2576 sl->info.res.status = rule->code;
2577 s->txn->status = rule->code;
2578
Christopher Faulet08e66462019-05-23 16:44:59 +02002579 if (close && !htx_add_header(htx, ist("Connection"), ist("close")))
2580 goto fail;
2581
2582 if (!htx_add_header(htx, ist("Content-length"), ist("0")) ||
Christopher Faulet99daf282018-11-28 22:58:13 +01002583 !htx_add_header(htx, ist("Location"), location))
2584 goto fail;
2585
2586 if (rule->code == 302 || rule->code == 303 || rule->code == 307) {
2587 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")))
2588 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002589 }
2590
2591 if (rule->cookie_len) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002592 if (!htx_add_header(htx, ist("Set-Cookie"), ist2(rule->cookie_str, rule->cookie_len)))
2593 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002594 }
2595
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002596 if (!htx_add_endof(htx, HTX_BLK_EOH))
Christopher Faulet99daf282018-11-28 22:58:13 +01002597 goto fail;
2598
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002599 htx->flags |= HTX_FL_EOM;
Kevin Zhu96b36392020-01-07 09:42:55 +01002600 htx_to_buf(htx, &res->buf);
Christopher Fauleta72a7e42020-01-28 09:28:11 +01002601 if (!http_forward_proxy_resp(s, 1))
2602 goto fail;
Christopher Faulet99daf282018-11-28 22:58:13 +01002603
Christopher Faulet60b33a52020-01-28 09:18:10 +01002604 if (rule->flags & REDIRECT_FLAG_FROM_REQ) {
2605 /* let's log the request time */
2606 s->logs.tv_request = now;
2607 req->analysers &= AN_REQ_FLT_END;
Christopher Faulet99daf282018-11-28 22:58:13 +01002608
Christopher Faulet60b33a52020-01-28 09:18:10 +01002609 if (s->sess->fe == s->be) /* report it if the request was intercepted by the frontend */
Willy Tarreau4781b152021-04-06 13:53:36 +02002610 _HA_ATOMIC_INC(&s->sess->fe->fe_counters.intercepted_req);
Christopher Faulet60b33a52020-01-28 09:18:10 +01002611 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002612
2613 if (!(s->flags & SF_ERR_MASK))
2614 s->flags |= SF_ERR_LOCAL;
2615 if (!(s->flags & SF_FINST_MASK))
Christopher Faulet60b33a52020-01-28 09:18:10 +01002616 s->flags |= ((rule->flags & REDIRECT_FLAG_FROM_REQ) ? SF_FINST_R : SF_FINST_H);
Christopher Fauletf2824e62018-10-01 12:12:37 +02002617
Christopher Faulet99daf282018-11-28 22:58:13 +01002618 free_trash_chunk(chunk);
2619 return 1;
2620
2621 fail:
2622 /* If an error occurred, remove the incomplete HTTP response from the
2623 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01002624 channel_htx_truncate(res, htxbuf(&res->buf));
Christopher Fauletf2824e62018-10-01 12:12:37 +02002625 free_trash_chunk(chunk);
Christopher Faulet99daf282018-11-28 22:58:13 +01002626 return 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002627}
2628
Christopher Faulet92d34fe2019-12-17 09:20:34 +01002629/* Replace all headers matching the name <name>. The header value is replaced if
2630 * it matches the regex <re>. <str> is used for the replacement. If <full> is
2631 * set to 1, the full-line is matched and replaced. Otherwise, comma-separated
2632 * values are evaluated one by one. It returns 0 on success and -1 on error.
2633 */
2634int http_replace_hdrs(struct stream* s, struct htx *htx, struct ist name,
2635 const char *str, struct my_regex *re, int full)
Christopher Faulet72333522018-10-24 11:25:02 +02002636{
2637 struct http_hdr_ctx ctx;
2638 struct buffer *output = get_trash_chunk();
2639
Christopher Faulet72333522018-10-24 11:25:02 +02002640 ctx.blk = NULL;
Christopher Faulet92d34fe2019-12-17 09:20:34 +01002641 while (http_find_header(htx, name, &ctx, full)) {
Christopher Faulet72333522018-10-24 11:25:02 +02002642 if (!regex_exec_match2(re, ctx.value.ptr, ctx.value.len, MAX_MATCH, pmatch, 0))
2643 continue;
2644
2645 output->data = exp_replace(output->area, output->size, ctx.value.ptr, str, pmatch);
2646 if (output->data == -1)
2647 return -1;
2648 if (!http_replace_header_value(htx, &ctx, ist2(output->area, output->data)))
2649 return -1;
2650 }
2651 return 0;
2652}
2653
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002654/* This function executes one of the set-{method,path,query,uri} actions. It
2655 * takes the string from the variable 'replace' with length 'len', then modifies
2656 * the relevant part of the request line accordingly. Then it updates various
2657 * pointers to the next elements which were moved, and the total buffer length.
2658 * It finds the action to be performed in p[2], previously filled by function
2659 * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal
2660 * error, though this can be revisited when this code is finally exploited.
2661 *
2662 * 'action' can be '0' to replace method, '1' to replace path, '2' to replace
Christopher Faulet312294f2020-09-02 17:17:44 +02002663 * query string, 3 to replace uri or 4 to replace the path+query.
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002664 *
2665 * In query string case, the mark question '?' must be set at the start of the
2666 * string by the caller, event if the replacement query string is empty.
2667 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002668int http_req_replace_stline(int action, const char *replace, int len,
2669 struct proxy *px, struct stream *s)
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002670{
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002671 struct htx *htx = htxbuf(&s->req.buf);
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002672
2673 switch (action) {
2674 case 0: // method
2675 if (!http_replace_req_meth(htx, ist2(replace, len)))
2676 return -1;
2677 break;
2678
2679 case 1: // path
Christopher Fauletb8ce5052020-08-31 16:11:57 +02002680 if (!http_replace_req_path(htx, ist2(replace, len), 0))
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002681 return -1;
2682 break;
2683
2684 case 2: // query
2685 if (!http_replace_req_query(htx, ist2(replace, len)))
2686 return -1;
2687 break;
2688
2689 case 3: // uri
2690 if (!http_replace_req_uri(htx, ist2(replace, len)))
2691 return -1;
2692 break;
2693
Christopher Faulet312294f2020-09-02 17:17:44 +02002694 case 4: // path + query
2695 if (!http_replace_req_path(htx, ist2(replace, len), 1))
2696 return -1;
2697 break;
2698
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002699 default:
2700 return -1;
2701 }
2702 return 0;
2703}
2704
2705/* This function replace the HTTP status code and the associated message. The
Christopher Faulete00d06c2019-12-16 17:18:42 +01002706 * variable <status> contains the new status code. This function never fails. It
2707 * returns 0 in case of success, -1 in case of internal error.
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002708 */
Christopher Faulet96bff762019-12-17 13:46:18 +01002709int http_res_set_status(unsigned int status, struct ist reason, struct stream *s)
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002710{
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002711 struct htx *htx = htxbuf(&s->res.buf);
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002712 char *res;
2713
2714 chunk_reset(&trash);
2715 res = ultoa_o(status, trash.area, trash.size);
2716 trash.data = res - trash.area;
2717
2718 /* Do we have a custom reason format string? */
Tim Duesterhuse296d3e2020-03-05 17:56:31 +01002719 if (!isttest(reason)) {
Christopher Faulet96bff762019-12-17 13:46:18 +01002720 const char *str = http_get_reason(status);
Tim Duesterhusdcf753a2021-03-04 17:31:47 +01002721 reason = ist(str);
Christopher Faulet96bff762019-12-17 13:46:18 +01002722 }
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002723
Christopher Fauletbde2c4c2020-08-31 16:43:34 +02002724 if (!http_replace_res_status(htx, ist2(trash.area, trash.data), reason))
Christopher Faulete00d06c2019-12-16 17:18:42 +01002725 return -1;
2726 return 0;
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002727}
2728
Christopher Faulet3e964192018-10-24 11:39:23 +02002729/* Executes the http-request rules <rules> for stream <s>, proxy <px> and
2730 * transaction <txn>. Returns the verdict of the first rule that prevents
2731 * further processing of the request (auth, deny, ...), and defaults to
2732 * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or
2733 * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT
2734 * on txn->flags if it encounters a tarpit rule. If <deny_status> is not NULL
2735 * and a deny/tarpit rule is matched, it will be filled with this rule's deny
2736 * status.
2737 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002738static enum rule_result http_req_get_intercept_rule(struct proxy *px, struct list *rules,
Christopher Fauletb58f62b2020-01-13 16:40:13 +01002739 struct stream *s)
Christopher Faulet3e964192018-10-24 11:39:23 +02002740{
2741 struct session *sess = strm_sess(s);
2742 struct http_txn *txn = s->txn;
Christopher Faulet3e964192018-10-24 11:39:23 +02002743 struct act_rule *rule;
Christopher Faulet3e964192018-10-24 11:39:23 +02002744 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002745 int act_opts = 0;
Christopher Faulet3e964192018-10-24 11:39:23 +02002746
Christopher Faulet3e964192018-10-24 11:39:23 +02002747 /* If "the current_rule_list" match the executed rule list, we are in
2748 * resume condition. If a resume is needed it is always in the action
2749 * and never in the ACL or converters. In this case, we initialise the
2750 * current rule, and go to the action execution point.
2751 */
2752 if (s->current_rule) {
2753 rule = s->current_rule;
2754 s->current_rule = NULL;
2755 if (s->current_rule_list == rules)
2756 goto resume_execution;
2757 }
2758 s->current_rule_list = rules;
2759
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002760 /* start the ruleset evaluation in strict mode */
2761 txn->req.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01002762
Christopher Faulet3e964192018-10-24 11:39:23 +02002763 list_for_each_entry(rule, rules, list) {
2764 /* check optional condition */
2765 if (rule->cond) {
2766 int ret;
2767
2768 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
2769 ret = acl_pass(ret);
2770
2771 if (rule->cond->pol == ACL_COND_UNLESS)
2772 ret = !ret;
2773
2774 if (!ret) /* condition not matched */
2775 continue;
2776 }
2777
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002778 act_opts |= ACT_OPT_FIRST;
Christopher Faulet3e964192018-10-24 11:39:23 +02002779 resume_execution:
Amaury Denoyelle03517732021-05-07 14:25:01 +02002780 if (rule->kw->flags & KWF_EXPERIMENTAL)
2781 mark_tainted(TAINTED_ACTION_EXP_EXECUTED);
2782
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002783 /* Always call the action function if defined */
2784 if (rule->action_ptr) {
2785 if ((s->req.flags & CF_READ_ERROR) ||
2786 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
2787 (px->options & PR_O_ABRT_CLOSE)))
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002788 act_opts |= ACT_OPT_FINAL;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002789
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002790 switch (rule->action_ptr(rule, px, sess, s, act_opts)) {
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002791 case ACT_RET_CONT:
2792 break;
2793 case ACT_RET_STOP:
2794 rule_ret = HTTP_RULE_RES_STOP;
2795 goto end;
2796 case ACT_RET_YIELD:
2797 s->current_rule = rule;
2798 rule_ret = HTTP_RULE_RES_YIELD;
2799 goto end;
2800 case ACT_RET_ERR:
2801 rule_ret = HTTP_RULE_RES_ERROR;
2802 goto end;
2803 case ACT_RET_DONE:
2804 rule_ret = HTTP_RULE_RES_DONE;
2805 goto end;
2806 case ACT_RET_DENY:
Christopher Fauletb58f62b2020-01-13 16:40:13 +01002807 if (txn->status == -1)
2808 txn->status = 403;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002809 rule_ret = HTTP_RULE_RES_DENY;
2810 goto end;
2811 case ACT_RET_ABRT:
2812 rule_ret = HTTP_RULE_RES_ABRT;
2813 goto end;
2814 case ACT_RET_INV:
2815 rule_ret = HTTP_RULE_RES_BADREQ;
2816 goto end;
2817 }
2818 continue; /* eval the next rule */
2819 }
2820
2821 /* If not action function defined, check for known actions */
Christopher Faulet3e964192018-10-24 11:39:23 +02002822 switch (rule->action) {
2823 case ACT_ACTION_ALLOW:
2824 rule_ret = HTTP_RULE_RES_STOP;
2825 goto end;
2826
2827 case ACT_ACTION_DENY:
Christopher Faulet5cb513a2020-05-13 17:56:56 +02002828 txn->status = rule->arg.http_reply->status;
2829 txn->http_reply = rule->arg.http_reply;
Christopher Faulet3e964192018-10-24 11:39:23 +02002830 rule_ret = HTTP_RULE_RES_DENY;
2831 goto end;
2832
2833 case ACT_HTTP_REQ_TARPIT:
2834 txn->flags |= TX_CLTARPIT;
Christopher Faulet5cb513a2020-05-13 17:56:56 +02002835 txn->status = rule->arg.http_reply->status;
2836 txn->http_reply = rule->arg.http_reply;
Christopher Faulet3e964192018-10-24 11:39:23 +02002837 rule_ret = HTTP_RULE_RES_DENY;
2838 goto end;
2839
Christopher Faulet3e964192018-10-24 11:39:23 +02002840 case ACT_HTTP_REDIR:
Christopher Faulet90d22a82020-03-06 11:18:39 +01002841 rule_ret = HTTP_RULE_RES_ABRT;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002842 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
Christopher Faulet3a26bee2019-12-16 12:47:40 +01002843 rule_ret = HTTP_RULE_RES_ERROR;
Christopher Faulet3e964192018-10-24 11:39:23 +02002844 goto end;
2845
2846 case ACT_HTTP_SET_NICE:
Christopher Faulet96bff762019-12-17 13:46:18 +01002847 s->task->nice = rule->arg.http.i;
Christopher Faulet3e964192018-10-24 11:39:23 +02002848 break;
2849
2850 case ACT_HTTP_SET_TOS:
Christopher Faulet96bff762019-12-17 13:46:18 +01002851 conn_set_tos(objt_conn(sess->origin), rule->arg.http.i);
Christopher Faulet3e964192018-10-24 11:39:23 +02002852 break;
2853
2854 case ACT_HTTP_SET_MARK:
Christopher Faulet96bff762019-12-17 13:46:18 +01002855 conn_set_mark(objt_conn(sess->origin), rule->arg.http.i);
Christopher Faulet3e964192018-10-24 11:39:23 +02002856 break;
2857
2858 case ACT_HTTP_SET_LOGL:
Christopher Faulet96bff762019-12-17 13:46:18 +01002859 s->logs.level = rule->arg.http.i;
Christopher Faulet3e964192018-10-24 11:39:23 +02002860 break;
2861
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002862 /* other flags exists, but normally, they never be matched. */
Christopher Faulet3e964192018-10-24 11:39:23 +02002863 default:
2864 break;
2865 }
2866 }
2867
2868 end:
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002869 /* if the ruleset evaluation is finished reset the strict mode */
Christopher Faulet46f95542019-12-20 10:07:22 +01002870 if (rule_ret != HTTP_RULE_RES_YIELD)
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002871 txn->req.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01002872
Christopher Faulet3e964192018-10-24 11:39:23 +02002873 /* we reached the end of the rules, nothing to report */
2874 return rule_ret;
2875}
2876
2877/* Executes the http-response rules <rules> for stream <s> and proxy <px>. It
2878 * returns one of 5 possible statuses: HTTP_RULE_RES_CONT, HTTP_RULE_RES_STOP,
2879 * HTTP_RULE_RES_DONE, HTTP_RULE_RES_YIELD, or HTTP_RULE_RES_BADREQ. If *CONT
2880 * is returned, the process can continue the evaluation of next rule list. If
2881 * *STOP or *DONE is returned, the process must stop the evaluation. If *BADREQ
2882 * is returned, it means the operation could not be processed and a server error
Christopher Fauleta53abad2020-05-13 08:12:22 +02002883 * must be returned. If *YIELD is returned, the caller must call again the
2884 * function with the same context.
Christopher Faulet3e964192018-10-24 11:39:23 +02002885 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002886static enum rule_result http_res_get_intercept_rule(struct proxy *px, struct list *rules,
2887 struct stream *s)
Christopher Faulet3e964192018-10-24 11:39:23 +02002888{
2889 struct session *sess = strm_sess(s);
2890 struct http_txn *txn = s->txn;
Christopher Faulet3e964192018-10-24 11:39:23 +02002891 struct act_rule *rule;
Christopher Faulet3e964192018-10-24 11:39:23 +02002892 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002893 int act_opts = 0;
Christopher Faulet3e964192018-10-24 11:39:23 +02002894
Christopher Faulet3e964192018-10-24 11:39:23 +02002895 /* If "the current_rule_list" match the executed rule list, we are in
2896 * resume condition. If a resume is needed it is always in the action
2897 * and never in the ACL or converters. In this case, we initialise the
2898 * current rule, and go to the action execution point.
2899 */
2900 if (s->current_rule) {
2901 rule = s->current_rule;
2902 s->current_rule = NULL;
2903 if (s->current_rule_list == rules)
2904 goto resume_execution;
2905 }
2906 s->current_rule_list = rules;
2907
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002908 /* start the ruleset evaluation in strict mode */
2909 txn->rsp.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01002910
Christopher Faulet3e964192018-10-24 11:39:23 +02002911 list_for_each_entry(rule, rules, list) {
2912 /* check optional condition */
2913 if (rule->cond) {
2914 int ret;
2915
2916 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
2917 ret = acl_pass(ret);
2918
2919 if (rule->cond->pol == ACL_COND_UNLESS)
2920 ret = !ret;
2921
2922 if (!ret) /* condition not matched */
2923 continue;
2924 }
2925
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002926 act_opts |= ACT_OPT_FIRST;
Christopher Faulet3e964192018-10-24 11:39:23 +02002927resume_execution:
Amaury Denoyelle03517732021-05-07 14:25:01 +02002928 if (rule->kw->flags & KWF_EXPERIMENTAL)
2929 mark_tainted(TAINTED_ACTION_EXP_EXECUTED);
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002930
2931 /* Always call the action function if defined */
2932 if (rule->action_ptr) {
2933 if ((s->req.flags & CF_READ_ERROR) ||
2934 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
2935 (px->options & PR_O_ABRT_CLOSE)))
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002936 act_opts |= ACT_OPT_FINAL;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002937
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002938 switch (rule->action_ptr(rule, px, sess, s, act_opts)) {
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002939 case ACT_RET_CONT:
2940 break;
2941 case ACT_RET_STOP:
2942 rule_ret = HTTP_RULE_RES_STOP;
2943 goto end;
2944 case ACT_RET_YIELD:
2945 s->current_rule = rule;
2946 rule_ret = HTTP_RULE_RES_YIELD;
2947 goto end;
2948 case ACT_RET_ERR:
2949 rule_ret = HTTP_RULE_RES_ERROR;
2950 goto end;
2951 case ACT_RET_DONE:
2952 rule_ret = HTTP_RULE_RES_DONE;
2953 goto end;
2954 case ACT_RET_DENY:
Christopher Fauletb58f62b2020-01-13 16:40:13 +01002955 if (txn->status == -1)
2956 txn->status = 502;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002957 rule_ret = HTTP_RULE_RES_DENY;
2958 goto end;
2959 case ACT_RET_ABRT:
2960 rule_ret = HTTP_RULE_RES_ABRT;
2961 goto end;
2962 case ACT_RET_INV:
2963 rule_ret = HTTP_RULE_RES_BADREQ;
2964 goto end;
2965 }
2966 continue; /* eval the next rule */
2967 }
2968
2969 /* If not action function defined, check for known actions */
Christopher Faulet3e964192018-10-24 11:39:23 +02002970 switch (rule->action) {
2971 case ACT_ACTION_ALLOW:
2972 rule_ret = HTTP_RULE_RES_STOP; /* "allow" rules are OK */
2973 goto end;
2974
2975 case ACT_ACTION_DENY:
Christopher Faulet5cb513a2020-05-13 17:56:56 +02002976 txn->status = rule->arg.http_reply->status;
2977 txn->http_reply = rule->arg.http_reply;
Christopher Faulet3a26bee2019-12-16 12:47:40 +01002978 rule_ret = HTTP_RULE_RES_DENY;
Christopher Faulet3e964192018-10-24 11:39:23 +02002979 goto end;
2980
2981 case ACT_HTTP_SET_NICE:
Christopher Faulet96bff762019-12-17 13:46:18 +01002982 s->task->nice = rule->arg.http.i;
Christopher Faulet3e964192018-10-24 11:39:23 +02002983 break;
2984
2985 case ACT_HTTP_SET_TOS:
Christopher Faulet96bff762019-12-17 13:46:18 +01002986 conn_set_tos(objt_conn(sess->origin), rule->arg.http.i);
Christopher Faulet3e964192018-10-24 11:39:23 +02002987 break;
2988
2989 case ACT_HTTP_SET_MARK:
Christopher Faulet96bff762019-12-17 13:46:18 +01002990 conn_set_mark(objt_conn(sess->origin), rule->arg.http.i);
Christopher Faulet3e964192018-10-24 11:39:23 +02002991 break;
2992
2993 case ACT_HTTP_SET_LOGL:
Christopher Faulet96bff762019-12-17 13:46:18 +01002994 s->logs.level = rule->arg.http.i;
Christopher Faulet3e964192018-10-24 11:39:23 +02002995 break;
2996
Christopher Faulet3e964192018-10-24 11:39:23 +02002997 case ACT_HTTP_REDIR:
Christopher Faulet49c2a702020-03-06 15:44:37 +01002998 rule_ret = HTTP_RULE_RES_ABRT;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002999 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
Christopher Faulet3a26bee2019-12-16 12:47:40 +01003000 rule_ret = HTTP_RULE_RES_ERROR;
Christopher Faulet3e964192018-10-24 11:39:23 +02003001 goto end;
3002
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01003003 /* other flags exists, but normally, they never be matched. */
Christopher Faulet3e964192018-10-24 11:39:23 +02003004 default:
3005 break;
3006 }
3007 }
3008
3009 end:
Christopher Faulet1aea50e2020-01-17 16:03:53 +01003010 /* if the ruleset evaluation is finished reset the strict mode */
Christopher Faulet46f95542019-12-20 10:07:22 +01003011 if (rule_ret != HTTP_RULE_RES_YIELD)
Christopher Faulet1aea50e2020-01-17 16:03:53 +01003012 txn->rsp.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01003013
Christopher Faulet3e964192018-10-24 11:39:23 +02003014 /* we reached the end of the rules, nothing to report */
3015 return rule_ret;
3016}
3017
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01003018/* Executes backend and frontend http-after-response rules for the stream <s>,
3019 * in that order. it return 1 on success and 0 on error. It is the caller
3020 * responsibility to catch error or ignore it. If it catches it, this function
3021 * may be called a second time, for the internal error.
3022 */
3023int http_eval_after_res_rules(struct stream *s)
3024{
3025 struct session *sess = s->sess;
3026 enum rule_result ret = HTTP_RULE_RES_CONT;
3027
Christopher Faulet507479b2020-05-15 12:29:46 +02003028 /* Eval after-response ruleset only if the reply is not const */
3029 if (s->txn->flags & TX_CONST_REPLY)
3030 goto end;
3031
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01003032 /* prune the request variables if not already done and swap to the response variables. */
3033 if (s->vars_reqres.scope != SCOPE_RES) {
3034 if (!LIST_ISEMPTY(&s->vars_reqres.head))
3035 vars_prune(&s->vars_reqres, s->sess, s);
3036 vars_init(&s->vars_reqres, SCOPE_RES);
3037 }
3038
3039 ret = http_res_get_intercept_rule(s->be, &s->be->http_after_res_rules, s);
3040 if ((ret == HTTP_RULE_RES_CONT || ret == HTTP_RULE_RES_STOP) && sess->fe != s->be)
3041 ret = http_res_get_intercept_rule(sess->fe, &sess->fe->http_after_res_rules, s);
3042
Christopher Faulet507479b2020-05-15 12:29:46 +02003043 end:
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01003044 /* All other codes than CONTINUE, STOP or DONE are forbidden */
3045 return (ret == HTTP_RULE_RES_CONT || ret == HTTP_RULE_RES_STOP || ret == HTTP_RULE_RES_DONE);
3046}
3047
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003048/*
3049 * Manage client-side cookie. It can impact performance by about 2% so it is
3050 * desirable to call it only when needed. This code is quite complex because
3051 * of the multiple very crappy and ambiguous syntaxes we have to support. it
3052 * highly recommended not to touch this part without a good reason !
3053 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003054static void http_manage_client_side_cookies(struct stream *s, struct channel *req)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003055{
3056 struct session *sess = s->sess;
3057 struct http_txn *txn = s->txn;
3058 struct htx *htx;
3059 struct http_hdr_ctx ctx;
3060 char *hdr_beg, *hdr_end, *del_from;
3061 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
3062 int preserve_hdr;
3063
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003064 htx = htxbuf(&req->buf);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003065 ctx.blk = NULL;
3066 while (http_find_header(htx, ist("Cookie"), &ctx, 1)) {
Olivier Houchardf0f42382019-07-22 17:43:46 +02003067 int is_first = 1;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003068 del_from = NULL; /* nothing to be deleted */
3069 preserve_hdr = 0; /* assume we may kill the whole header */
3070
3071 /* Now look for cookies. Conforming to RFC2109, we have to support
3072 * attributes whose name begin with a '$', and associate them with
3073 * the right cookie, if we want to delete this cookie.
3074 * So there are 3 cases for each cookie read :
3075 * 1) it's a special attribute, beginning with a '$' : ignore it.
3076 * 2) it's a server id cookie that we *MAY* want to delete : save
3077 * some pointers on it (last semi-colon, beginning of cookie...)
3078 * 3) it's an application cookie : we *MAY* have to delete a previous
3079 * "special" cookie.
3080 * At the end of loop, if a "special" cookie remains, we may have to
3081 * remove it. If no application cookie persists in the header, we
3082 * *MUST* delete it.
3083 *
3084 * Note: RFC2965 is unclear about the processing of spaces around
3085 * the equal sign in the ATTR=VALUE form. A careful inspection of
3086 * the RFC explicitly allows spaces before it, and not within the
3087 * tokens (attrs or values). An inspection of RFC2109 allows that
3088 * too but section 10.1.3 lets one think that spaces may be allowed
3089 * after the equal sign too, resulting in some (rare) buggy
3090 * implementations trying to do that. So let's do what servers do.
3091 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
3092 * allowed quoted strings in values, with any possible character
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003093 * after a backslash, including control chars and delimiters, which
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003094 * causes parsing to become ambiguous. Browsers also allow spaces
3095 * within values even without quotes.
3096 *
3097 * We have to keep multiple pointers in order to support cookie
3098 * removal at the beginning, middle or end of header without
3099 * corrupting the header. All of these headers are valid :
3100 *
3101 * hdr_beg hdr_end
3102 * | |
3103 * v |
3104 * NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3 |
3105 * NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3 v
3106 * NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3
3107 * | | | | | | |
3108 * | | | | | | |
3109 * | | | | | | +--> next
3110 * | | | | | +----> val_end
3111 * | | | | +-----------> val_beg
3112 * | | | +--------------> equal
3113 * | | +----------------> att_end
3114 * | +---------------------> att_beg
3115 * +--------------------------> prev
3116 *
3117 */
3118 hdr_beg = ctx.value.ptr;
3119 hdr_end = hdr_beg + ctx.value.len;
3120 for (prev = hdr_beg; prev < hdr_end; prev = next) {
3121 /* Iterate through all cookies on this line */
3122
3123 /* find att_beg */
3124 att_beg = prev;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003125 if (!is_first)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003126 att_beg++;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003127 is_first = 0;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003128
3129 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
3130 att_beg++;
3131
3132 /* find att_end : this is the first character after the last non
3133 * space before the equal. It may be equal to hdr_end.
3134 */
3135 equal = att_end = att_beg;
3136 while (equal < hdr_end) {
3137 if (*equal == '=' || *equal == ',' || *equal == ';')
3138 break;
3139 if (HTTP_IS_SPHT(*equal++))
3140 continue;
3141 att_end = equal;
3142 }
3143
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003144 /* here, <equal> points to '=', a delimiter or the end. <att_end>
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003145 * is between <att_beg> and <equal>, both may be identical.
3146 */
3147 /* look for end of cookie if there is an equal sign */
3148 if (equal < hdr_end && *equal == '=') {
3149 /* look for the beginning of the value */
3150 val_beg = equal + 1;
3151 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
3152 val_beg++;
3153
3154 /* find the end of the value, respecting quotes */
3155 next = http_find_cookie_value_end(val_beg, hdr_end);
3156
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003157 /* make val_end point to the first white space or delimiter after the value */
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003158 val_end = next;
3159 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
3160 val_end--;
3161 }
3162 else
3163 val_beg = val_end = next = equal;
3164
3165 /* We have nothing to do with attributes beginning with
3166 * '$'. However, they will automatically be removed if a
3167 * header before them is removed, since they're supposed
3168 * to be linked together.
3169 */
3170 if (*att_beg == '$')
3171 continue;
3172
3173 /* Ignore cookies with no equal sign */
3174 if (equal == next) {
3175 /* This is not our cookie, so we must preserve it. But if we already
3176 * scheduled another cookie for removal, we cannot remove the
3177 * complete header, but we can remove the previous block itself.
3178 */
3179 preserve_hdr = 1;
3180 if (del_from != NULL) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003181 int delta = http_del_hdr_value(hdr_beg, hdr_end, &del_from, prev);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003182 val_end += delta;
3183 next += delta;
3184 hdr_end += delta;
3185 prev = del_from;
3186 del_from = NULL;
3187 }
3188 continue;
3189 }
3190
3191 /* if there are spaces around the equal sign, we need to
3192 * strip them otherwise we'll get trouble for cookie captures,
3193 * or even for rewrites. Since this happens extremely rarely,
3194 * it does not hurt performance.
3195 */
3196 if (unlikely(att_end != equal || val_beg > equal + 1)) {
3197 int stripped_before = 0;
3198 int stripped_after = 0;
3199
3200 if (att_end != equal) {
3201 memmove(att_end, equal, hdr_end - equal);
3202 stripped_before = (att_end - equal);
3203 equal += stripped_before;
3204 val_beg += stripped_before;
3205 }
3206
3207 if (val_beg > equal + 1) {
3208 memmove(equal + 1, val_beg, hdr_end + stripped_before - val_beg);
3209 stripped_after = (equal + 1) - val_beg;
3210 val_beg += stripped_after;
3211 stripped_before += stripped_after;
3212 }
3213
3214 val_end += stripped_before;
3215 next += stripped_before;
3216 hdr_end += stripped_before;
3217 }
3218 /* now everything is as on the diagram above */
3219
3220 /* First, let's see if we want to capture this cookie. We check
3221 * that we don't already have a client side cookie, because we
3222 * can only capture one. Also as an optimisation, we ignore
3223 * cookies shorter than the declared name.
3224 */
3225 if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL &&
3226 (val_end - att_beg >= sess->fe->capture_namelen) &&
3227 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
3228 int log_len = val_end - att_beg;
3229
3230 if ((txn->cli_cookie = pool_alloc(pool_head_capture)) == NULL) {
3231 ha_alert("HTTP logging : out of memory.\n");
3232 } else {
3233 if (log_len > sess->fe->capture_len)
3234 log_len = sess->fe->capture_len;
3235 memcpy(txn->cli_cookie, att_beg, log_len);
3236 txn->cli_cookie[log_len] = 0;
3237 }
3238 }
3239
3240 /* Persistence cookies in passive, rewrite or insert mode have the
3241 * following form :
3242 *
3243 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
3244 *
3245 * For cookies in prefix mode, the form is :
3246 *
3247 * Cookie: NAME=SRV~VALUE
3248 */
3249 if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
3250 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
3251 struct server *srv = s->be->srv;
3252 char *delim;
3253
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003254 /* if we're in cookie prefix mode, we'll search the delimiter so that we
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003255 * have the server ID between val_beg and delim, and the original cookie between
3256 * delim+1 and val_end. Otherwise, delim==val_end :
3257 *
3258 * hdr_beg
3259 * |
3260 * v
3261 * NAME=SRV; # in all but prefix modes
3262 * NAME=SRV~OPAQUE ; # in prefix mode
3263 * || || | |+-> next
3264 * || || | +--> val_end
3265 * || || +---------> delim
3266 * || |+------------> val_beg
3267 * || +-------------> att_end = equal
3268 * |+-----------------> att_beg
3269 * +------------------> prev
3270 *
3271 */
3272 if (s->be->ck_opts & PR_CK_PFX) {
3273 for (delim = val_beg; delim < val_end; delim++)
3274 if (*delim == COOKIE_DELIM)
3275 break;
3276 }
3277 else {
3278 char *vbar1;
3279 delim = val_end;
3280 /* Now check if the cookie contains a date field, which would
3281 * appear after a vertical bar ('|') just after the server name
3282 * and before the delimiter.
3283 */
3284 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
3285 if (vbar1) {
3286 /* OK, so left of the bar is the server's cookie and
3287 * right is the last seen date. It is a base64 encoded
3288 * 30-bit value representing the UNIX date since the
3289 * epoch in 4-second quantities.
3290 */
3291 int val;
3292 delim = vbar1++;
3293 if (val_end - vbar1 >= 5) {
3294 val = b64tos30(vbar1);
3295 if (val > 0)
3296 txn->cookie_last_date = val << 2;
3297 }
3298 /* look for a second vertical bar */
3299 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
3300 if (vbar1 && (val_end - vbar1 > 5)) {
3301 val = b64tos30(vbar1 + 1);
3302 if (val > 0)
3303 txn->cookie_first_date = val << 2;
3304 }
3305 }
3306 }
3307
3308 /* if the cookie has an expiration date and the proxy wants to check
3309 * it, then we do that now. We first check if the cookie is too old,
3310 * then only if it has expired. We detect strict overflow because the
3311 * time resolution here is not great (4 seconds). Cookies with dates
3312 * in the future are ignored if their offset is beyond one day. This
3313 * allows an admin to fix timezone issues without expiring everyone
3314 * and at the same time avoids keeping unwanted side effects for too
3315 * long.
3316 */
3317 if (txn->cookie_first_date && s->be->cookie_maxlife &&
3318 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) ||
3319 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
3320 txn->flags &= ~TX_CK_MASK;
3321 txn->flags |= TX_CK_OLD;
3322 delim = val_beg; // let's pretend we have not found the cookie
3323 txn->cookie_first_date = 0;
3324 txn->cookie_last_date = 0;
3325 }
3326 else if (txn->cookie_last_date && s->be->cookie_maxidle &&
3327 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) ||
3328 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
3329 txn->flags &= ~TX_CK_MASK;
3330 txn->flags |= TX_CK_EXPIRED;
3331 delim = val_beg; // let's pretend we have not found the cookie
3332 txn->cookie_first_date = 0;
3333 txn->cookie_last_date = 0;
3334 }
3335
3336 /* Here, we'll look for the first running server which supports the cookie.
3337 * This allows to share a same cookie between several servers, for example
3338 * to dedicate backup servers to specific servers only.
3339 * However, to prevent clients from sticking to cookie-less backup server
3340 * when they have incidentely learned an empty cookie, we simply ignore
3341 * empty cookies and mark them as invalid.
3342 * The same behaviour is applied when persistence must be ignored.
3343 */
3344 if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
3345 srv = NULL;
3346
3347 while (srv) {
3348 if (srv->cookie && (srv->cklen == delim - val_beg) &&
3349 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
3350 if ((srv->cur_state != SRV_ST_STOPPED) ||
3351 (s->be->options & PR_O_PERSIST) ||
3352 (s->flags & SF_FORCE_PRST)) {
3353 /* we found the server and we can use it */
3354 txn->flags &= ~TX_CK_MASK;
3355 txn->flags |= (srv->cur_state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
3356 s->flags |= SF_DIRECT | SF_ASSIGNED;
3357 s->target = &srv->obj_type;
3358 break;
3359 } else {
3360 /* we found a server, but it's down,
3361 * mark it as such and go on in case
3362 * another one is available.
3363 */
3364 txn->flags &= ~TX_CK_MASK;
3365 txn->flags |= TX_CK_DOWN;
3366 }
3367 }
3368 srv = srv->next;
3369 }
3370
3371 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
3372 /* no server matched this cookie or we deliberately skipped it */
3373 txn->flags &= ~TX_CK_MASK;
3374 if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
3375 txn->flags |= TX_CK_UNUSED;
3376 else
3377 txn->flags |= TX_CK_INVALID;
3378 }
3379
3380 /* depending on the cookie mode, we may have to either :
3381 * - delete the complete cookie if we're in insert+indirect mode, so that
3382 * the server never sees it ;
3383 * - remove the server id from the cookie value, and tag the cookie as an
Joseph Herlante9d5c722018-11-25 11:00:25 -08003384 * application cookie so that it does not get accidentally removed later,
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003385 * if we're in cookie prefix mode
3386 */
3387 if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
3388 int delta; /* negative */
3389
3390 memmove(val_beg, delim + 1, hdr_end - (delim + 1));
3391 delta = val_beg - (delim + 1);
3392 val_end += delta;
3393 next += delta;
3394 hdr_end += delta;
3395 del_from = NULL;
3396 preserve_hdr = 1; /* we want to keep this cookie */
3397 }
3398 else if (del_from == NULL &&
3399 (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
3400 del_from = prev;
3401 }
3402 }
3403 else {
3404 /* This is not our cookie, so we must preserve it. But if we already
3405 * scheduled another cookie for removal, we cannot remove the
3406 * complete header, but we can remove the previous block itself.
3407 */
3408 preserve_hdr = 1;
3409
3410 if (del_from != NULL) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003411 int delta = http_del_hdr_value(hdr_beg, hdr_end, &del_from, prev);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003412 if (att_beg >= del_from)
3413 att_beg += delta;
3414 if (att_end >= del_from)
3415 att_end += delta;
3416 val_beg += delta;
3417 val_end += delta;
3418 next += delta;
3419 hdr_end += delta;
3420 prev = del_from;
3421 del_from = NULL;
3422 }
3423 }
3424
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003425 } /* for each cookie */
3426
3427
3428 /* There are no more cookies on this line.
3429 * We may still have one (or several) marked for deletion at the
3430 * end of the line. We must do this now in two ways :
3431 * - if some cookies must be preserved, we only delete from the
3432 * mark to the end of line ;
3433 * - if nothing needs to be preserved, simply delete the whole header
3434 */
3435 if (del_from) {
3436 hdr_end = (preserve_hdr ? del_from : hdr_beg);
3437 }
3438 if ((hdr_end - hdr_beg) != ctx.value.len) {
Christopher Faulet3e2638e2019-06-18 09:49:16 +02003439 if (hdr_beg != hdr_end)
3440 htx_change_blk_value_len(htx, ctx.blk, hdr_end - hdr_beg);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003441 else
3442 http_remove_header(htx, &ctx);
3443 }
3444 } /* for each "Cookie header */
3445}
3446
3447/*
3448 * Manage server-side cookies. It can impact performance by about 2% so it is
3449 * desirable to call it only when needed. This function is also used when we
3450 * just need to know if there is a cookie (eg: for check-cache).
3451 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003452static void http_manage_server_side_cookies(struct stream *s, struct channel *res)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003453{
3454 struct session *sess = s->sess;
3455 struct http_txn *txn = s->txn;
3456 struct htx *htx;
3457 struct http_hdr_ctx ctx;
3458 struct server *srv;
3459 char *hdr_beg, *hdr_end;
3460 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau6f7a02a2019-04-15 21:49:49 +02003461 int is_cookie2 = 0;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003462
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003463 htx = htxbuf(&res->buf);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003464
3465 ctx.blk = NULL;
3466 while (1) {
Olivier Houchardf0f42382019-07-22 17:43:46 +02003467 int is_first = 1;
3468
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003469 if (!http_find_header(htx, ist("Set-Cookie"), &ctx, 1)) {
3470 if (!http_find_header(htx, ist("Set-Cookie2"), &ctx, 1))
3471 break;
3472 is_cookie2 = 1;
3473 }
3474
3475 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
3476 * <prev> points to the colon.
3477 */
3478 txn->flags |= TX_SCK_PRESENT;
3479
3480 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
3481 * check-cache is enabled) and we are not interested in checking
3482 * them. Warning, the cookie capture is declared in the frontend.
3483 */
3484 if (s->be->cookie_name == NULL && sess->fe->capture_name == NULL)
3485 break;
3486
3487 /* OK so now we know we have to process this response cookie.
3488 * The format of the Set-Cookie header is slightly different
3489 * from the format of the Cookie header in that it does not
3490 * support the comma as a cookie delimiter (thus the header
3491 * cannot be folded) because the Expires attribute described in
3492 * the original Netscape's spec may contain an unquoted date
3493 * with a comma inside. We have to live with this because
3494 * many browsers don't support Max-Age and some browsers don't
3495 * support quoted strings. However the Set-Cookie2 header is
3496 * clean.
3497 *
3498 * We have to keep multiple pointers in order to support cookie
3499 * removal at the beginning, middle or end of header without
3500 * corrupting the header (in case of set-cookie2). A special
3501 * pointer, <scav> points to the beginning of the set-cookie-av
3502 * fields after the first semi-colon. The <next> pointer points
3503 * either to the end of line (set-cookie) or next unquoted comma
3504 * (set-cookie2). All of these headers are valid :
3505 *
3506 * hdr_beg hdr_end
3507 * | |
3508 * v |
3509 * NAME1 = VALUE 1 ; Secure; Path="/" |
3510 * NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT v
3511 * NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT
3512 * NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard
3513 * | | | | | | | |
3514 * | | | | | | | +-> next
3515 * | | | | | | +------------> scav
3516 * | | | | | +--------------> val_end
3517 * | | | | +--------------------> val_beg
3518 * | | | +----------------------> equal
3519 * | | +------------------------> att_end
3520 * | +----------------------------> att_beg
3521 * +------------------------------> prev
3522 * -------------------------------> hdr_beg
3523 */
3524 hdr_beg = ctx.value.ptr;
3525 hdr_end = hdr_beg + ctx.value.len;
3526 for (prev = hdr_beg; prev < hdr_end; prev = next) {
3527
3528 /* Iterate through all cookies on this line */
3529
3530 /* find att_beg */
3531 att_beg = prev;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003532 if (!is_first)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003533 att_beg++;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003534 is_first = 0;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003535
3536 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
3537 att_beg++;
3538
3539 /* find att_end : this is the first character after the last non
3540 * space before the equal. It may be equal to hdr_end.
3541 */
3542 equal = att_end = att_beg;
3543
3544 while (equal < hdr_end) {
3545 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
3546 break;
3547 if (HTTP_IS_SPHT(*equal++))
3548 continue;
3549 att_end = equal;
3550 }
3551
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003552 /* here, <equal> points to '=', a delimiter or the end. <att_end>
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003553 * is between <att_beg> and <equal>, both may be identical.
3554 */
3555
3556 /* look for end of cookie if there is an equal sign */
3557 if (equal < hdr_end && *equal == '=') {
3558 /* look for the beginning of the value */
3559 val_beg = equal + 1;
3560 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
3561 val_beg++;
3562
3563 /* find the end of the value, respecting quotes */
3564 next = http_find_cookie_value_end(val_beg, hdr_end);
3565
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003566 /* make val_end point to the first white space or delimiter after the value */
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003567 val_end = next;
3568 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
3569 val_end--;
3570 }
3571 else {
3572 /* <equal> points to next comma, semi-colon or EOL */
3573 val_beg = val_end = next = equal;
3574 }
3575
3576 if (next < hdr_end) {
3577 /* Set-Cookie2 supports multiple cookies, and <next> points to
3578 * a colon or semi-colon before the end. So skip all attr-value
3579 * pairs and look for the next comma. For Set-Cookie, since
3580 * commas are permitted in values, skip to the end.
3581 */
3582 if (is_cookie2)
3583 next = http_find_hdr_value_end(next, hdr_end);
3584 else
3585 next = hdr_end;
3586 }
3587
3588 /* Now everything is as on the diagram above */
3589
3590 /* Ignore cookies with no equal sign */
3591 if (equal == val_end)
3592 continue;
3593
3594 /* If there are spaces around the equal sign, we need to
3595 * strip them otherwise we'll get trouble for cookie captures,
3596 * or even for rewrites. Since this happens extremely rarely,
3597 * it does not hurt performance.
3598 */
3599 if (unlikely(att_end != equal || val_beg > equal + 1)) {
3600 int stripped_before = 0;
3601 int stripped_after = 0;
3602
3603 if (att_end != equal) {
3604 memmove(att_end, equal, hdr_end - equal);
3605 stripped_before = (att_end - equal);
3606 equal += stripped_before;
3607 val_beg += stripped_before;
3608 }
3609
3610 if (val_beg > equal + 1) {
3611 memmove(equal + 1, val_beg, hdr_end + stripped_before - val_beg);
3612 stripped_after = (equal + 1) - val_beg;
3613 val_beg += stripped_after;
3614 stripped_before += stripped_after;
3615 }
3616
3617 val_end += stripped_before;
3618 next += stripped_before;
3619 hdr_end += stripped_before;
3620
Christopher Faulet3e2638e2019-06-18 09:49:16 +02003621 htx_change_blk_value_len(htx, ctx.blk, hdr_end - hdr_beg);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003622 ctx.value.len = hdr_end - hdr_beg;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003623 }
3624
3625 /* First, let's see if we want to capture this cookie. We check
3626 * that we don't already have a server side cookie, because we
3627 * can only capture one. Also as an optimisation, we ignore
3628 * cookies shorter than the declared name.
3629 */
3630 if (sess->fe->capture_name != NULL &&
3631 txn->srv_cookie == NULL &&
3632 (val_end - att_beg >= sess->fe->capture_namelen) &&
3633 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
3634 int log_len = val_end - att_beg;
3635 if ((txn->srv_cookie = pool_alloc(pool_head_capture)) == NULL) {
3636 ha_alert("HTTP logging : out of memory.\n");
3637 }
3638 else {
3639 if (log_len > sess->fe->capture_len)
3640 log_len = sess->fe->capture_len;
3641 memcpy(txn->srv_cookie, att_beg, log_len);
3642 txn->srv_cookie[log_len] = 0;
3643 }
3644 }
3645
3646 srv = objt_server(s->target);
3647 /* now check if we need to process it for persistence */
3648 if (!(s->flags & SF_IGNORE_PRST) &&
3649 (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
3650 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
3651 /* assume passive cookie by default */
3652 txn->flags &= ~TX_SCK_MASK;
3653 txn->flags |= TX_SCK_FOUND;
3654
3655 /* If the cookie is in insert mode on a known server, we'll delete
3656 * this occurrence because we'll insert another one later.
3657 * We'll delete it too if the "indirect" option is set and we're in
3658 * a direct access.
3659 */
3660 if (s->be->ck_opts & PR_CK_PSV) {
3661 /* The "preserve" flag was set, we don't want to touch the
3662 * server's cookie.
3663 */
3664 }
3665 else if ((srv && (s->be->ck_opts & PR_CK_INS)) ||
3666 ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) {
3667 /* this cookie must be deleted */
3668 if (prev == hdr_beg && next == hdr_end) {
3669 /* whole header */
3670 http_remove_header(htx, &ctx);
3671 /* note: while both invalid now, <next> and <hdr_end>
3672 * are still equal, so the for() will stop as expected.
3673 */
3674 } else {
3675 /* just remove the value */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003676 int delta = http_del_hdr_value(hdr_beg, hdr_end, &prev, next);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003677 next = prev;
3678 hdr_end += delta;
3679 }
3680 txn->flags &= ~TX_SCK_MASK;
3681 txn->flags |= TX_SCK_DELETED;
3682 /* and go on with next cookie */
3683 }
3684 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) {
3685 /* replace bytes val_beg->val_end with the cookie name associated
3686 * with this server since we know it.
3687 */
3688 int sliding, delta;
3689
3690 ctx.value = ist2(val_beg, val_end - val_beg);
3691 ctx.lws_before = ctx.lws_after = 0;
3692 http_replace_header_value(htx, &ctx, ist2(srv->cookie, srv->cklen));
3693 delta = srv->cklen - (val_end - val_beg);
3694 sliding = (ctx.value.ptr - val_beg);
3695 hdr_beg += sliding;
3696 val_beg += sliding;
3697 next += sliding + delta;
3698 hdr_end += sliding + delta;
3699
3700 txn->flags &= ~TX_SCK_MASK;
3701 txn->flags |= TX_SCK_REPLACED;
3702 }
3703 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) {
3704 /* insert the cookie name associated with this server
3705 * before existing cookie, and insert a delimiter between them..
3706 */
3707 int sliding, delta;
3708 ctx.value = ist2(val_beg, 0);
3709 ctx.lws_before = ctx.lws_after = 0;
3710 http_replace_header_value(htx, &ctx, ist2(srv->cookie, srv->cklen + 1));
3711 delta = srv->cklen + 1;
3712 sliding = (ctx.value.ptr - val_beg);
3713 hdr_beg += sliding;
3714 val_beg += sliding;
3715 next += sliding + delta;
3716 hdr_end += sliding + delta;
3717
3718 val_beg[srv->cklen] = COOKIE_DELIM;
3719 txn->flags &= ~TX_SCK_MASK;
3720 txn->flags |= TX_SCK_REPLACED;
3721 }
3722 }
3723 /* that's done for this cookie, check the next one on the same
3724 * line when next != hdr_end (only if is_cookie2).
3725 */
3726 }
3727 }
3728}
3729
Christopher Faulet25a02f62018-10-24 12:00:25 +02003730/*
3731 * Parses the Cache-Control and Pragma request header fields to determine if
3732 * the request may be served from the cache and/or if it is cacheable. Updates
3733 * s->txn->flags.
3734 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003735void http_check_request_for_cacheability(struct stream *s, struct channel *req)
Christopher Faulet25a02f62018-10-24 12:00:25 +02003736{
3737 struct http_txn *txn = s->txn;
3738 struct htx *htx;
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003739 struct http_hdr_ctx ctx = { .blk = NULL };
3740 int pragma_found, cc_found;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003741
3742 if ((txn->flags & (TX_CACHEABLE|TX_CACHE_IGNORE)) == TX_CACHE_IGNORE)
3743 return; /* nothing more to do here */
3744
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003745 htx = htxbuf(&req->buf);
Christopher Faulet25a02f62018-10-24 12:00:25 +02003746 pragma_found = cc_found = 0;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003747
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003748 /* Check "pragma" header for HTTP/1.0 compatibility. */
3749 if (http_find_header(htx, ist("pragma"), &ctx, 1)) {
3750 if (isteqi(ctx.value, ist("no-cache"))) {
3751 pragma_found = 1;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003752 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003753 }
Christopher Faulet25a02f62018-10-24 12:00:25 +02003754
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003755 ctx.blk = NULL;
3756 /* Don't use the cache and don't try to store if we found the
3757 * Authorization header */
3758 if (http_find_header(htx, ist("authorization"), &ctx, 1)) {
3759 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3760 txn->flags |= TX_CACHE_IGNORE;
3761 }
Christopher Faulet25a02f62018-10-24 12:00:25 +02003762
Christopher Faulet25a02f62018-10-24 12:00:25 +02003763
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003764 /* Look for "cache-control" header and iterate over all the values
3765 * until we find one that specifies that caching is possible or not. */
3766 ctx.blk = NULL;
3767 while (http_find_header(htx, ist("cache-control"), &ctx, 0)) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003768 cc_found = 1;
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003769 /* We don't check the values after max-age, max-stale nor min-fresh,
3770 * we simply don't use the cache when they're specified. */
3771 if (istmatchi(ctx.value, ist("max-age")) ||
3772 istmatchi(ctx.value, ist("no-cache")) ||
3773 istmatchi(ctx.value, ist("max-stale")) ||
3774 istmatchi(ctx.value, ist("min-fresh"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003775 txn->flags |= TX_CACHE_IGNORE;
3776 continue;
3777 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003778 if (istmatchi(ctx.value, ist("no-store"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003779 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3780 continue;
3781 }
3782 }
3783
3784 /* RFC7234#5.4:
3785 * When the Cache-Control header field is also present and
3786 * understood in a request, Pragma is ignored.
3787 * When the Cache-Control header field is not present in a
3788 * request, caches MUST consider the no-cache request
3789 * pragma-directive as having the same effect as if
3790 * "Cache-Control: no-cache" were present.
3791 */
3792 if (!cc_found && pragma_found)
3793 txn->flags |= TX_CACHE_IGNORE;
3794}
3795
3796/*
3797 * Check if response is cacheable or not. Updates s->txn->flags.
3798 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003799void http_check_response_for_cacheability(struct stream *s, struct channel *res)
Christopher Faulet25a02f62018-10-24 12:00:25 +02003800{
3801 struct http_txn *txn = s->txn;
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003802 struct http_hdr_ctx ctx = { .blk = NULL };
Christopher Faulet25a02f62018-10-24 12:00:25 +02003803 struct htx *htx;
Remi Tricot-Le Bretoncc9bf2e2020-11-12 11:14:41 +01003804 int has_freshness_info = 0;
3805 int has_validator = 0;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003806
3807 if (txn->status < 200) {
3808 /* do not try to cache interim responses! */
3809 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3810 return;
3811 }
3812
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003813 htx = htxbuf(&res->buf);
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003814 /* Check "pragma" header for HTTP/1.0 compatibility. */
3815 if (http_find_header(htx, ist("pragma"), &ctx, 1)) {
3816 if (isteqi(ctx.value, ist("no-cache"))) {
3817 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3818 return;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003819 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003820 }
Christopher Faulet25a02f62018-10-24 12:00:25 +02003821
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003822 /* Look for "cache-control" header and iterate over all the values
3823 * until we find one that specifies that caching is possible or not. */
3824 ctx.blk = NULL;
3825 while (http_find_header(htx, ist("cache-control"), &ctx, 0)) {
3826 if (isteqi(ctx.value, ist("public"))) {
3827 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003828 continue;
3829 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003830 if (isteqi(ctx.value, ist("private")) ||
3831 isteqi(ctx.value, ist("no-cache")) ||
3832 isteqi(ctx.value, ist("no-store")) ||
3833 isteqi(ctx.value, ist("max-age=0")) ||
3834 isteqi(ctx.value, ist("s-maxage=0"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003835 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003836 continue;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003837 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003838 /* We might have a no-cache="set-cookie" form. */
3839 if (istmatchi(ctx.value, ist("no-cache=\"set-cookie"))) {
3840 txn->flags &= ~TX_CACHE_COOK;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003841 continue;
3842 }
Remi Tricot-Le Bretoncc9bf2e2020-11-12 11:14:41 +01003843
3844 if (istmatchi(ctx.value, ist("s-maxage")) ||
3845 istmatchi(ctx.value, ist("max-age"))) {
3846 has_freshness_info = 1;
3847 continue;
3848 }
3849 }
3850
3851 /* If no freshness information could be found in Cache-Control values,
3852 * look for an Expires header. */
3853 if (!has_freshness_info) {
3854 ctx.blk = NULL;
3855 has_freshness_info = http_find_header(htx, ist("expires"), &ctx, 0);
Christopher Faulet25a02f62018-10-24 12:00:25 +02003856 }
Remi Tricot-Le Bretoncc9bf2e2020-11-12 11:14:41 +01003857
3858 /* If no freshness information could be found in Cache-Control or Expires
3859 * values, look for an explicit validator. */
3860 if (!has_freshness_info) {
3861 ctx.blk = NULL;
3862 has_validator = 1;
3863 if (!http_find_header(htx, ist("etag"), &ctx, 0)) {
3864 ctx.blk = NULL;
3865 if (!http_find_header(htx, ist("last-modified"), &ctx, 0))
3866 has_validator = 0;
3867 }
3868 }
3869
3870 /* We won't store an entry that has neither a cache validator nor an
3871 * explicit expiration time, as suggested in RFC 7234#3. */
3872 if (!has_freshness_info && !has_validator)
3873 txn->flags |= TX_CACHE_IGNORE;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003874}
3875
Christopher Faulet377c5a52018-10-24 21:21:30 +02003876/*
3877 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
3878 * for the current backend.
3879 *
3880 * It is assumed that the request is either a HEAD, GET, or POST and that the
3881 * uri_auth field is valid.
3882 *
3883 * Returns 1 if stats should be provided, otherwise 0.
3884 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003885static int http_stats_check_uri(struct stream *s, struct http_txn *txn, struct proxy *backend)
Christopher Faulet377c5a52018-10-24 21:21:30 +02003886{
3887 struct uri_auth *uri_auth = backend->uri_auth;
3888 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003889 struct htx_sl *sl;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003890 struct ist uri;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003891
3892 if (!uri_auth)
3893 return 0;
3894
3895 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
3896 return 0;
3897
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003898 htx = htxbuf(&s->req.buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02003899 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003900 uri = htx_sl_req_uri(sl);
Willy Tarreau1eb3b482019-10-31 15:50:28 +01003901 if (*uri_auth->uri_prefix == '/')
3902 uri = http_get_path(uri);
Christopher Faulet377c5a52018-10-24 21:21:30 +02003903
3904 /* check URI size */
3905 if (uri_auth->uri_len > uri.len)
3906 return 0;
3907
3908 if (memcmp(uri.ptr, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
3909 return 0;
3910
3911 return 1;
3912}
3913
3914/* This function prepares an applet to handle the stats. It can deal with the
3915 * "100-continue" expectation, check that admin rules are met for POST requests,
3916 * and program a response message if something was unexpected. It cannot fail
3917 * and always relies on the stats applet to complete the job. It does not touch
3918 * analysers nor counters, which are left to the caller. It does not touch
3919 * s->target which is supposed to already point to the stats applet. The caller
3920 * is expected to have already assigned an appctx to the stream.
3921 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003922static int http_handle_stats(struct stream *s, struct channel *req)
Christopher Faulet377c5a52018-10-24 21:21:30 +02003923{
3924 struct stats_admin_rule *stats_admin_rule;
3925 struct stream_interface *si = &s->si[1];
3926 struct session *sess = s->sess;
3927 struct http_txn *txn = s->txn;
3928 struct http_msg *msg = &txn->req;
3929 struct uri_auth *uri_auth = s->be->uri_auth;
3930 const char *h, *lookup, *end;
3931 struct appctx *appctx;
3932 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003933 struct htx_sl *sl;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003934
3935 appctx = si_appctx(si);
3936 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
3937 appctx->st1 = appctx->st2 = 0;
3938 appctx->ctx.stats.st_code = STAT_STATUS_INIT;
Willy Tarreau676c29e2019-10-09 10:50:01 +02003939 appctx->ctx.stats.flags |= uri_auth->flags;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003940 appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */
3941 if ((msg->flags & HTTP_MSGF_VER_11) && (txn->meth != HTTP_METH_HEAD))
3942 appctx->ctx.stats.flags |= STAT_CHUNKED;
3943
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003944 htx = htxbuf(&req->buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02003945 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003946 lookup = HTX_SL_REQ_UPTR(sl) + uri_auth->uri_len;
3947 end = HTX_SL_REQ_UPTR(sl) + HTX_SL_REQ_ULEN(sl);
Christopher Faulet377c5a52018-10-24 21:21:30 +02003948
3949 for (h = lookup; h <= end - 3; h++) {
3950 if (memcmp(h, ";up", 3) == 0) {
3951 appctx->ctx.stats.flags |= STAT_HIDE_DOWN;
3952 break;
3953 }
Amaury Denoyelle91e55ea2021-02-25 14:46:08 +01003954 }
3955
3956 for (h = lookup; h <= end - 9; h++) {
3957 if (memcmp(h, ";no-maint", 9) == 0) {
Willy Tarreau3e320362020-10-23 17:28:57 +02003958 appctx->ctx.stats.flags |= STAT_HIDE_MAINT;
3959 break;
3960 }
Christopher Faulet377c5a52018-10-24 21:21:30 +02003961 }
3962
3963 if (uri_auth->refresh) {
3964 for (h = lookup; h <= end - 10; h++) {
3965 if (memcmp(h, ";norefresh", 10) == 0) {
3966 appctx->ctx.stats.flags |= STAT_NO_REFRESH;
3967 break;
3968 }
3969 }
3970 }
3971
3972 for (h = lookup; h <= end - 4; h++) {
3973 if (memcmp(h, ";csv", 4) == 0) {
Christopher Faulet6338a082019-09-09 15:50:54 +02003974 appctx->ctx.stats.flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
Christopher Faulet377c5a52018-10-24 21:21:30 +02003975 break;
3976 }
3977 }
3978
3979 for (h = lookup; h <= end - 6; h++) {
3980 if (memcmp(h, ";typed", 6) == 0) {
Christopher Faulet6338a082019-09-09 15:50:54 +02003981 appctx->ctx.stats.flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
Christopher Faulet377c5a52018-10-24 21:21:30 +02003982 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
3983 break;
3984 }
3985 }
3986
Christopher Faulet6338a082019-09-09 15:50:54 +02003987 for (h = lookup; h <= end - 5; h++) {
3988 if (memcmp(h, ";json", 5) == 0) {
3989 appctx->ctx.stats.flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
3990 appctx->ctx.stats.flags |= STAT_FMT_JSON;
3991 break;
3992 }
3993 }
3994
3995 for (h = lookup; h <= end - 12; h++) {
3996 if (memcmp(h, ";json-schema", 12) == 0) {
3997 appctx->ctx.stats.flags &= ~STAT_FMT_MASK;
3998 appctx->ctx.stats.flags |= STAT_JSON_SCHM;
3999 break;
4000 }
4001 }
4002
Christopher Faulet377c5a52018-10-24 21:21:30 +02004003 for (h = lookup; h <= end - 8; h++) {
4004 if (memcmp(h, ";st=", 4) == 0) {
4005 int i;
4006 h += 4;
4007 appctx->ctx.stats.st_code = STAT_STATUS_UNKN;
4008 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
4009 if (strncmp(stat_status_codes[i], h, 4) == 0) {
4010 appctx->ctx.stats.st_code = i;
4011 break;
4012 }
4013 }
4014 break;
4015 }
4016 }
4017
4018 appctx->ctx.stats.scope_str = 0;
4019 appctx->ctx.stats.scope_len = 0;
4020 for (h = lookup; h <= end - 8; h++) {
4021 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
4022 int itx = 0;
4023 const char *h2;
4024 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
4025 const char *err;
4026
4027 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
4028 h2 = h;
Christopher Fauleted7a0662019-01-14 11:07:34 +01004029 appctx->ctx.stats.scope_str = h2 - HTX_SL_REQ_UPTR(sl);
4030 while (h < end) {
Christopher Faulet377c5a52018-10-24 21:21:30 +02004031 if (*h == ';' || *h == '&' || *h == ' ')
4032 break;
4033 itx++;
4034 h++;
4035 }
4036
4037 if (itx > STAT_SCOPE_TXT_MAXLEN)
4038 itx = STAT_SCOPE_TXT_MAXLEN;
4039 appctx->ctx.stats.scope_len = itx;
4040
4041 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
4042 memcpy(scope_txt, h2, itx);
4043 scope_txt[itx] = '\0';
4044 err = invalid_char(scope_txt);
4045 if (err) {
4046 /* bad char in search text => clear scope */
4047 appctx->ctx.stats.scope_str = 0;
4048 appctx->ctx.stats.scope_len = 0;
4049 }
4050 break;
4051 }
4052 }
4053
4054 /* now check whether we have some admin rules for this request */
4055 list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) {
4056 int ret = 1;
4057
4058 if (stats_admin_rule->cond) {
4059 ret = acl_exec_cond(stats_admin_rule->cond, s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
4060 ret = acl_pass(ret);
4061 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
4062 ret = !ret;
4063 }
4064
4065 if (ret) {
4066 /* no rule, or the rule matches */
4067 appctx->ctx.stats.flags |= STAT_ADMIN;
4068 break;
4069 }
4070 }
4071
Christopher Faulet5d45e382019-02-27 15:15:23 +01004072 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
4073 appctx->st0 = STAT_HTTP_HEAD;
4074 else if (txn->meth == HTTP_METH_POST) {
Christopher Fauletbd9e8422019-08-15 22:26:48 +02004075 if (appctx->ctx.stats.flags & STAT_ADMIN) {
Christopher Faulet377c5a52018-10-24 21:21:30 +02004076 appctx->st0 = STAT_HTTP_POST;
Christopher Fauletbd9e8422019-08-15 22:26:48 +02004077 if (msg->msg_state < HTTP_MSG_DATA)
4078 req->analysers |= AN_REQ_HTTP_BODY;
4079 }
Christopher Faulet377c5a52018-10-24 21:21:30 +02004080 else {
Christopher Faulet5d45e382019-02-27 15:15:23 +01004081 /* POST without admin level */
Christopher Faulet377c5a52018-10-24 21:21:30 +02004082 appctx->ctx.stats.flags &= ~STAT_CHUNKED;
4083 appctx->ctx.stats.st_code = STAT_STATUS_DENY;
4084 appctx->st0 = STAT_HTTP_LAST;
4085 }
4086 }
4087 else {
Christopher Faulet5d45e382019-02-27 15:15:23 +01004088 /* Unsupported method */
4089 appctx->ctx.stats.flags &= ~STAT_CHUNKED;
4090 appctx->ctx.stats.st_code = STAT_STATUS_IVAL;
4091 appctx->st0 = STAT_HTTP_LAST;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004092 }
4093
4094 s->task->nice = -32; /* small boost for HTTP statistics */
4095 return 1;
4096}
4097
Christopher Faulet021a8e42021-03-29 10:46:38 +02004098/* This function waits for the message payload at most <time> milliseconds (may
4099 * be set to TICK_ETERNITY). It stops to wait if at least <bytes> bytes of the
4100 * payload are received (0 means no limit). It returns HTTP_RULE_* depending on
4101 * the result:
4102 *
4103 * - HTTP_RULE_RES_CONT when conditions are met to stop waiting
4104 * - HTTP_RULE_RES_YIELD to wait for more data
Ilya Shipitsinb2be9a12021-04-24 13:25:42 +05004105 * - HTTP_RULE_RES_ABRT when a timeout occurred.
Christopher Faulet021a8e42021-03-29 10:46:38 +02004106 * - HTTP_RULE_RES_BADREQ if a parsing error is raised by lower level
Ilya Shipitsinb2be9a12021-04-24 13:25:42 +05004107 * - HTTP_RULE_RES_ERROR if an internal error occurred
Christopher Faulet021a8e42021-03-29 10:46:38 +02004108 *
Ilya Shipitsinb2be9a12021-04-24 13:25:42 +05004109 * If a timeout occurred, this function is responsible to emit the right response
Christopher Faulet021a8e42021-03-29 10:46:38 +02004110 * to the client, depending on the channel (408 on request side, 504 on response
4111 * side). All other errors must be handled by the caller.
4112 */
4113enum rule_result http_wait_for_msg_body(struct stream *s, struct channel *chn,
4114 unsigned int time, unsigned int bytes)
4115{
4116 struct session *sess = s->sess;
4117 struct http_txn *txn = s->txn;
4118 struct http_msg *msg = ((chn->flags & CF_ISRESP) ? &txn->rsp : &txn->req);
4119 struct htx *htx;
4120 enum rule_result ret = HTTP_RULE_RES_CONT;
4121
4122 htx = htxbuf(&chn->buf);
4123
4124 if (htx->flags & HTX_FL_PARSING_ERROR) {
4125 ret = HTTP_RULE_RES_BADREQ;
4126 goto end;
4127 }
4128 if (htx->flags & HTX_FL_PROCESSING_ERROR) {
4129 ret = HTTP_RULE_RES_ERROR;
4130 goto end;
4131 }
4132
4133 /* Do nothing for bodyless and CONNECT requests */
4134 if (txn->meth == HTTP_METH_CONNECT || (msg->flags & HTTP_MSGF_BODYLESS))
4135 goto end;
4136
4137 if (!(chn->flags & CF_ISRESP) && msg->msg_state < HTTP_MSG_DATA) {
4138 if (http_handle_expect_hdr(s, htx, msg) == -1) {
4139 ret = HTTP_RULE_RES_ERROR;
4140 goto end;
4141 }
4142 }
4143
4144 msg->msg_state = HTTP_MSG_DATA;
4145
4146 /* Now we're in HTTP_MSG_DATA. We just need to know if all data have
4147 * been received or if the buffer is full.
4148 */
4149 if ((htx->flags & HTX_FL_EOM) || htx_get_tail_type(htx) > HTX_BLK_DATA ||
4150 channel_htx_full(chn, htx, global.tune.maxrewrite))
4151 goto end;
4152
4153 if (bytes) {
4154 struct htx_blk *blk;
4155 unsigned int len = 0;
4156
4157 for (blk = htx_get_first_blk(htx); blk; blk = htx_get_next_blk(htx, blk)) {
4158 if (htx_get_blk_type(blk) != HTX_BLK_DATA)
4159 continue;
4160 len += htx_get_blksz(blk);
4161 if (len >= bytes)
4162 goto end;
4163 }
4164 }
4165
4166 if ((chn->flags & CF_READ_TIMEOUT) || tick_is_expired(chn->analyse_exp, now_ms)) {
4167 if (!(chn->flags & CF_ISRESP))
4168 goto abort_req;
4169 goto abort_res;
4170 }
4171
4172 /* we get here if we need to wait for more data */
4173 if (!(chn->flags & (CF_SHUTR | CF_READ_ERROR))) {
4174 if (!tick_isset(chn->analyse_exp))
4175 chn->analyse_exp = tick_add_ifset(now_ms, time);
4176 ret = HTTP_RULE_RES_YIELD;
4177 }
4178
4179 end:
4180 return ret;
4181
4182 abort_req:
4183 txn->status = 408;
4184 if (!(s->flags & SF_ERR_MASK))
4185 s->flags |= SF_ERR_CLITO;
4186 if (!(s->flags & SF_FINST_MASK))
4187 s->flags |= SF_FINST_D;
Willy Tarreau4781b152021-04-06 13:53:36 +02004188 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req);
Christopher Faulet021a8e42021-03-29 10:46:38 +02004189 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02004190 _HA_ATOMIC_INC(&sess->listener->counters->failed_req);
Christopher Faulet021a8e42021-03-29 10:46:38 +02004191 http_reply_and_close(s, txn->status, http_error_message(s));
4192 ret = HTTP_RULE_RES_ABRT;
4193 goto end;
4194
4195 abort_res:
4196 txn->status = 504;
4197 if (!(s->flags & SF_ERR_MASK))
4198 s->flags |= SF_ERR_SRVTO;
4199 if (!(s->flags & SF_FINST_MASK))
4200 s->flags |= SF_FINST_D;
4201 stream_inc_http_fail_ctr(s);
4202 http_reply_and_close(s, txn->status, http_error_message(s));
4203 ret = HTTP_RULE_RES_ABRT;
4204 goto end;
4205}
4206
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004207void http_perform_server_redirect(struct stream *s, struct stream_interface *si)
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004208{
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004209 struct channel *req = &s->req;
4210 struct channel *res = &s->res;
4211 struct server *srv;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004212 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004213 struct htx_sl *sl;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004214 struct ist path, location;
4215 unsigned int flags;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004216
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004217 /*
4218 * Create the location
4219 */
4220 chunk_reset(&trash);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004221
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004222 /* 1: add the server's prefix */
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004223 /* special prefix "/" means don't change URL */
4224 srv = __objt_server(s->target);
4225 if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') {
4226 if (!chunk_memcat(&trash, srv->rdr_pfx, srv->rdr_len))
4227 return;
4228 }
4229
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004230 /* 2: add the request Path */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004231 htx = htxbuf(&req->buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02004232 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004233 path = http_get_path(htx_sl_req_uri(sl));
Tim Duesterhused526372020-03-05 17:56:33 +01004234 if (!isttest(path))
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004235 return;
4236
4237 if (!chunk_memcat(&trash, path.ptr, path.len))
4238 return;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004239 location = ist2(trash.area, trash.data);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004240
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004241 /*
4242 * Create the 302 respone
4243 */
4244 htx = htx_from_buf(&res->buf);
4245 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
4246 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
4247 ist("HTTP/1.1"), ist("302"), ist("Found"));
4248 if (!sl)
4249 goto fail;
4250 sl->info.res.status = 302;
4251 s->txn->status = 302;
4252
4253 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) ||
4254 !htx_add_header(htx, ist("Connection"), ist("close")) ||
4255 !htx_add_header(htx, ist("Content-length"), ist("0")) ||
4256 !htx_add_header(htx, ist("Location"), location))
4257 goto fail;
4258
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004259 if (!htx_add_endof(htx, HTX_BLK_EOH))
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004260 goto fail;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004261
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004262 htx->flags |= HTX_FL_EOM;
Christopher Fauletc20afb82020-01-24 19:16:26 +01004263 htx_to_buf(htx, &res->buf);
Christopher Fauleta72a7e42020-01-28 09:28:11 +01004264 if (!http_forward_proxy_resp(s, 1))
4265 goto fail;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004266
4267 /* return without error. */
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004268 si_shutr(si);
4269 si_shutw(si);
4270 si->err_type = SI_ET_NONE;
4271 si->state = SI_ST_CLO;
4272
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004273 if (!(s->flags & SF_ERR_MASK))
4274 s->flags |= SF_ERR_LOCAL;
4275 if (!(s->flags & SF_FINST_MASK))
4276 s->flags |= SF_FINST_C;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004277
4278 /* FIXME: we should increase a counter of redirects per server and per backend. */
4279 srv_inc_sess_ctr(srv);
4280 srv_set_sess_last(srv);
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004281 return;
4282
4283 fail:
4284 /* If an error occurred, remove the incomplete HTTP response from the
4285 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004286 channel_htx_truncate(res, htx);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004287}
4288
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05004289/* This function terminates the request because it was completely analyzed or
Christopher Fauletf2824e62018-10-01 12:12:37 +02004290 * because an error was triggered during the body forwarding.
4291 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004292static void http_end_request(struct stream *s)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004293{
4294 struct channel *chn = &s->req;
4295 struct http_txn *txn = s->txn;
4296
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004297 DBG_TRACE_ENTER(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004298
Christopher Fauletb42a8b62018-11-19 21:59:00 +01004299 if (unlikely(txn->req.msg_state == HTTP_MSG_ERROR ||
4300 txn->rsp.msg_state == HTTP_MSG_ERROR)) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004301 channel_abort(chn);
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004302 channel_htx_truncate(chn, htxbuf(&chn->buf));
Christopher Fauletf2824e62018-10-01 12:12:37 +02004303 goto end;
4304 }
4305
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004306 if (unlikely(txn->req.msg_state < HTTP_MSG_DONE)) {
4307 DBG_TRACE_DEVEL("waiting end of the request", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004308 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004309 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004310
4311 if (txn->req.msg_state == HTTP_MSG_DONE) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004312 /* No need to read anymore, the request was completely parsed.
4313 * We can shut the read side unless we want to abort_on_close,
4314 * or we have a POST request. The issue with POST requests is
4315 * that some browsers still send a CRLF after the request, and
4316 * this CRLF must be read so that it does not remain in the kernel
4317 * buffers, otherwise a close could cause an RST on some systems
4318 * (eg: Linux).
4319 */
Christopher Faulet769d0e92019-03-22 14:23:18 +01004320 if (!(s->be->options & PR_O_ABRT_CLOSE) && txn->meth != HTTP_METH_POST)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004321 channel_dont_read(chn);
4322
4323 /* if the server closes the connection, we want to immediately react
4324 * and close the socket to save packets and syscalls.
4325 */
4326 s->si[1].flags |= SI_FL_NOHALF;
4327
4328 /* In any case we've finished parsing the request so we must
4329 * disable Nagle when sending data because 1) we're not going
4330 * to shut this side, and 2) the server is waiting for us to
4331 * send pending data.
4332 */
4333 chn->flags |= CF_NEVER_WAIT;
4334
Christopher Fauletd01ce402019-01-02 17:44:13 +01004335 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
4336 /* The server has not finished to respond, so we
4337 * don't want to move in order not to upset it.
4338 */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004339 DBG_TRACE_DEVEL("waiting end of the response", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletd01ce402019-01-02 17:44:13 +01004340 return;
4341 }
4342
Christopher Fauletf2824e62018-10-01 12:12:37 +02004343 /* When we get here, it means that both the request and the
4344 * response have finished receiving. Depending on the connection
4345 * mode, we'll have to wait for the last bytes to leave in either
4346 * direction, and sometimes for a close to be effective.
4347 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02004348 if (txn->flags & TX_CON_WANT_TUN) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004349 /* Tunnel mode will not have any analyser so it needs to
4350 * poll for reads.
4351 */
4352 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004353 if (b_data(&chn->buf)) {
4354 DBG_TRACE_DEVEL("waiting to flush the request", STRM_EV_HTTP_ANA, s, txn);
Christopher Faulet9768c262018-10-22 09:34:31 +02004355 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004356 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004357 txn->req.msg_state = HTTP_MSG_TUNNEL;
4358 }
4359 else {
4360 /* we're not expecting any new data to come for this
4361 * transaction, so we can close it.
Christopher Faulet9768c262018-10-22 09:34:31 +02004362 *
4363 * However, there is an exception if the response
4364 * length is undefined. In this case, we need to wait
4365 * the close from the server. The response will be
4366 * switched in TUNNEL mode until the end.
Christopher Fauletf2824e62018-10-01 12:12:37 +02004367 */
4368 if (!(txn->rsp.flags & HTTP_MSGF_XFER_LEN) &&
4369 txn->rsp.msg_state != HTTP_MSG_CLOSED)
Christopher Faulet9768c262018-10-22 09:34:31 +02004370 goto check_channel_flags;
Christopher Fauletf2824e62018-10-01 12:12:37 +02004371
4372 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4373 channel_shutr_now(chn);
4374 channel_shutw_now(chn);
4375 }
4376 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004377 goto check_channel_flags;
4378 }
4379
4380 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
4381 http_msg_closing:
4382 /* nothing else to forward, just waiting for the output buffer
4383 * to be empty and for the shutw_now to take effect.
4384 */
4385 if (channel_is_empty(chn)) {
4386 txn->req.msg_state = HTTP_MSG_CLOSED;
4387 goto http_msg_closed;
4388 }
4389 else if (chn->flags & CF_SHUTW) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004390 txn->req.msg_state = HTTP_MSG_ERROR;
4391 goto end;
4392 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004393 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004394 return;
4395 }
4396
4397 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
4398 http_msg_closed:
Christopher Fauletf2824e62018-10-01 12:12:37 +02004399 /* if we don't know whether the server will close, we need to hard close */
4400 if (txn->rsp.flags & HTTP_MSGF_XFER_LEN)
4401 s->si[1].flags |= SI_FL_NOLINGER; /* we want to close ASAP */
Christopher Fauletf2824e62018-10-01 12:12:37 +02004402 /* see above in MSG_DONE why we only do this in these states */
Christopher Faulet769d0e92019-03-22 14:23:18 +01004403 if (!(s->be->options & PR_O_ABRT_CLOSE))
Christopher Fauletf2824e62018-10-01 12:12:37 +02004404 channel_dont_read(chn);
4405 goto end;
4406 }
4407
4408 check_channel_flags:
4409 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4410 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4411 /* if we've just closed an output, let's switch */
4412 txn->req.msg_state = HTTP_MSG_CLOSING;
4413 goto http_msg_closing;
4414 }
4415
4416 end:
4417 chn->analysers &= AN_REQ_FLT_END;
Christopher Faulet198ef8b2020-12-15 13:32:55 +01004418 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
4419 chn->flags |= CF_NEVER_WAIT;
4420 if (HAS_REQ_DATA_FILTERS(s))
Christopher Fauletf2824e62018-10-01 12:12:37 +02004421 chn->analysers |= AN_REQ_FLT_XFER_DATA;
Christopher Faulet198ef8b2020-12-15 13:32:55 +01004422 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004423 channel_auto_close(chn);
4424 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004425 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004426}
4427
4428
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05004429/* This function terminates the response because it was completely analyzed or
Christopher Fauletf2824e62018-10-01 12:12:37 +02004430 * because an error was triggered during the body forwarding.
4431 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004432static void http_end_response(struct stream *s)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004433{
4434 struct channel *chn = &s->res;
4435 struct http_txn *txn = s->txn;
4436
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004437 DBG_TRACE_ENTER(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004438
Christopher Fauletb42a8b62018-11-19 21:59:00 +01004439 if (unlikely(txn->req.msg_state == HTTP_MSG_ERROR ||
4440 txn->rsp.msg_state == HTTP_MSG_ERROR)) {
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004441 channel_htx_truncate(&s->req, htxbuf(&s->req.buf));
Christopher Faulet9768c262018-10-22 09:34:31 +02004442 channel_abort(&s->req);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004443 goto end;
4444 }
4445
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004446 if (unlikely(txn->rsp.msg_state < HTTP_MSG_DONE)) {
4447 DBG_TRACE_DEVEL("waiting end of the response", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004448 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004449 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004450
4451 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
4452 /* In theory, we don't need to read anymore, but we must
4453 * still monitor the server connection for a possible close
4454 * while the request is being uploaded, so we don't disable
4455 * reading.
4456 */
4457 /* channel_dont_read(chn); */
4458
4459 if (txn->req.msg_state < HTTP_MSG_DONE) {
4460 /* The client seems to still be sending data, probably
4461 * because we got an error response during an upload.
4462 * We have the choice of either breaking the connection
4463 * or letting it pass through. Let's do the later.
4464 */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004465 DBG_TRACE_DEVEL("waiting end of the request", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004466 return;
4467 }
4468
4469 /* When we get here, it means that both the request and the
4470 * response have finished receiving. Depending on the connection
4471 * mode, we'll have to wait for the last bytes to leave in either
4472 * direction, and sometimes for a close to be effective.
4473 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02004474 if (txn->flags & TX_CON_WANT_TUN) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004475 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004476 if (b_data(&chn->buf)) {
4477 DBG_TRACE_DEVEL("waiting to flush the respone", STRM_EV_HTTP_ANA, s, txn);
Christopher Faulet9768c262018-10-22 09:34:31 +02004478 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004479 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004480 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
4481 }
4482 else {
4483 /* we're not expecting any new data to come for this
4484 * transaction, so we can close it.
4485 */
4486 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4487 channel_shutr_now(chn);
4488 channel_shutw_now(chn);
4489 }
4490 }
4491 goto check_channel_flags;
4492 }
4493
4494 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
4495 http_msg_closing:
4496 /* nothing else to forward, just waiting for the output buffer
4497 * to be empty and for the shutw_now to take effect.
4498 */
4499 if (channel_is_empty(chn)) {
4500 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4501 goto http_msg_closed;
4502 }
4503 else if (chn->flags & CF_SHUTW) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004504 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau4781b152021-04-06 13:53:36 +02004505 _HA_ATOMIC_INC(&strm_sess(s)->fe->fe_counters.cli_aborts);
4506 _HA_ATOMIC_INC(&s->be->be_counters.cli_aborts);
William Lallemand36119de2021-03-08 15:26:48 +01004507 if (strm_sess(s)->listener && strm_sess(s)->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02004508 _HA_ATOMIC_INC(&strm_sess(s)->listener->counters->cli_aborts);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004509 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02004510 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.cli_aborts);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004511 goto end;
4512 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004513 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004514 return;
4515 }
4516
4517 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
4518 http_msg_closed:
4519 /* drop any pending data */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004520 channel_htx_truncate(&s->req, htxbuf(&s->req.buf));
Christopher Faulet9768c262018-10-22 09:34:31 +02004521 channel_abort(&s->req);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004522 goto end;
4523 }
4524
4525 check_channel_flags:
4526 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4527 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4528 /* if we've just closed an output, let's switch */
4529 txn->rsp.msg_state = HTTP_MSG_CLOSING;
4530 goto http_msg_closing;
4531 }
4532
4533 end:
4534 chn->analysers &= AN_RES_FLT_END;
Christopher Faulet198ef8b2020-12-15 13:32:55 +01004535 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
4536 chn->flags |= CF_NEVER_WAIT;
4537 if (HAS_RSP_DATA_FILTERS(s))
4538 chn->analysers |= AN_RES_FLT_XFER_DATA;
4539 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004540 channel_auto_close(chn);
4541 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004542 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004543}
4544
Christopher Fauletef70e252020-01-28 09:26:19 +01004545/* Forward a response generated by HAProxy (error/redirect/return). This
4546 * function forwards all pending incoming data. If <final> is set to 0, nothing
4547 * more is performed. It is used for 1xx informational messages. Otherwise, the
Christopher Faulet507479b2020-05-15 12:29:46 +02004548 * transaction is terminated and the request is emptied. On success 1 is
Christopher Faulet40e6b552020-06-25 16:04:50 +02004549 * returned. If an error occurred, 0 is returned. If it fails, this function
4550 * only exits. It is the caller responsibility to do the cleanup.
Christopher Fauletef70e252020-01-28 09:26:19 +01004551 */
4552int http_forward_proxy_resp(struct stream *s, int final)
4553{
4554 struct channel *req = &s->req;
4555 struct channel *res = &s->res;
4556 struct htx *htx = htxbuf(&res->buf);
4557 size_t data;
4558
4559 if (final) {
4560 htx->flags |= HTX_FL_PROXY_RESP;
Christopher Faulet507479b2020-05-15 12:29:46 +02004561
Christopher Fauletaab1b672020-11-18 16:44:02 +01004562 if (!htx_is_empty(htx) && !http_eval_after_res_rules(s))
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01004563 return 0;
Christopher Fauletef70e252020-01-28 09:26:19 +01004564
Christopher Fauletd6c48362020-10-19 18:01:38 +02004565 if (s->txn->meth == HTTP_METH_HEAD)
4566 htx_skip_msg_payload(htx);
4567
Christopher Fauletef70e252020-01-28 09:26:19 +01004568 channel_auto_read(req);
4569 channel_abort(req);
4570 channel_auto_close(req);
4571 channel_htx_erase(req, htxbuf(&req->buf));
4572
4573 res->wex = tick_add_ifset(now_ms, res->wto);
4574 channel_auto_read(res);
4575 channel_auto_close(res);
4576 channel_shutr_now(res);
Christopher Faulet1a9db7c2020-06-25 15:36:45 +02004577 res->flags |= CF_EOI; /* The response is terminated, add EOI */
Christopher Faulet42432f32020-11-20 17:43:16 +01004578 htxbuf(&res->buf)->flags |= HTX_FL_EOM; /* no more data are expected */
Christopher Fauletef70e252020-01-28 09:26:19 +01004579 }
Christopher Fauletcf6898c2020-06-25 15:55:11 +02004580 else {
4581 /* Send ASAP informational messages. Rely on CF_EOI for final
4582 * response.
4583 */
4584 res->flags |= CF_SEND_DONTWAIT;
4585 }
Christopher Fauletef70e252020-01-28 09:26:19 +01004586
4587 data = htx->data - co_data(res);
4588 c_adv(res, data);
4589 htx->first = -1;
4590 res->total += data;
4591 return 1;
4592}
4593
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004594void http_server_error(struct stream *s, struct stream_interface *si, int err,
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004595 int finst, struct http_reply *msg)
Christopher Faulet0f226952018-10-22 09:29:56 +02004596{
Christopher Faulet72c7d8d2020-01-27 15:32:25 +01004597 http_reply_and_close(s, s->txn->status, msg);
Christopher Faulet0f226952018-10-22 09:29:56 +02004598 if (!(s->flags & SF_ERR_MASK))
4599 s->flags |= err;
4600 if (!(s->flags & SF_FINST_MASK))
4601 s->flags |= finst;
4602}
4603
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004604void http_reply_and_close(struct stream *s, short status, struct http_reply *msg)
Christopher Faulet0f226952018-10-22 09:29:56 +02004605{
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004606 if (!msg) {
4607 channel_htx_truncate(&s->res, htxbuf(&s->res.buf));
4608 goto end;
4609 }
4610
4611 if (http_reply_message(s, msg) == -1) {
4612 /* On error, return a 500 error message, but don't rewrite it if
Christopher Faulet40e6b552020-06-25 16:04:50 +02004613 * it is already an internal error. If it was already a "const"
4614 * 500 error, just fail.
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004615 */
Christopher Faulet40e6b552020-06-25 16:04:50 +02004616 if (s->txn->status == 500) {
4617 if (s->txn->flags & TX_CONST_REPLY)
4618 goto end;
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004619 s->txn->flags |= TX_CONST_REPLY;
Christopher Faulet40e6b552020-06-25 16:04:50 +02004620 }
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004621 s->txn->status = 500;
4622 s->txn->http_reply = NULL;
4623 return http_reply_and_close(s, s->txn->status, http_error_message(s));
4624 }
4625
4626end:
4627 s->res.wex = tick_add_ifset(now_ms, s->res.wto);
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004628
Christopher Faulet0f226952018-10-22 09:29:56 +02004629 channel_auto_read(&s->req);
4630 channel_abort(&s->req);
4631 channel_auto_close(&s->req);
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004632 channel_htx_erase(&s->req, htxbuf(&s->req.buf));
Christopher Faulet72c7d8d2020-01-27 15:32:25 +01004633 channel_auto_read(&s->res);
4634 channel_auto_close(&s->res);
4635 channel_shutr_now(&s->res);
Christopher Faulet0f226952018-10-22 09:29:56 +02004636}
4637
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004638struct http_reply *http_error_message(struct stream *s)
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004639{
4640 const int msgnum = http_get_status_idx(s->txn->status);
4641
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004642 if (s->txn->http_reply)
4643 return s->txn->http_reply;
4644 else if (s->be->replies[msgnum])
4645 return s->be->replies[msgnum];
4646 else if (strm_fe(s)->replies[msgnum])
4647 return strm_fe(s)->replies[msgnum];
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004648 else
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004649 return &http_err_replies[msgnum];
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004650}
4651
Christopher Faulet40e6b552020-06-25 16:04:50 +02004652/* Produces an HTX message from an http reply. Depending on the http reply type,
4653 * a, errorfile, an raw file or a log-format string is used. On success, it
4654 * returns 0. If an error occurs -1 is returned. If it fails, this function only
4655 * exits. It is the caller responsibility to do the cleanup.
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004656 */
Christopher Fauletae43b6c2020-05-27 15:24:22 +02004657int http_reply_to_htx(struct stream *s, struct htx *htx, struct http_reply *reply)
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004658{
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004659 struct buffer *errmsg;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004660 struct htx_sl *sl;
4661 struct buffer *body = NULL;
4662 const char *status, *reason, *clen, *ctype;
4663 unsigned int slflags;
4664 int ret = 0;
4665
Christopher Faulete29a97e2020-05-14 14:49:25 +02004666 /*
4667 * - HTTP_REPLY_ERRFILES unexpected here. handled as no payload if so
4668 *
4669 * - HTTP_REPLY_INDIRECT: switch on another reply if defined or handled
4670 * as no payload if NULL. the TXN status code is set with the status
4671 * of the original reply.
4672 */
4673
4674 if (reply->type == HTTP_REPLY_INDIRECT) {
4675 if (reply->body.reply)
4676 reply = reply->body.reply;
4677 }
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004678 if (reply->type == HTTP_REPLY_ERRMSG && !reply->body.errmsg) {
4679 /* get default error message */
4680 if (reply == s->txn->http_reply)
4681 s->txn->http_reply = NULL;
4682 reply = http_error_message(s);
4683 if (reply->type == HTTP_REPLY_INDIRECT) {
4684 if (reply->body.reply)
4685 reply = reply->body.reply;
4686 }
4687 }
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004688
4689 if (reply->type == HTTP_REPLY_ERRMSG) {
4690 /* implicit or explicit error message*/
4691 errmsg = reply->body.errmsg;
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004692 if (errmsg && !b_is_null(errmsg)) {
Christopher Faulet20567362020-05-15 14:52:49 +02004693 if (!htx_copy_msg(htx, errmsg))
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004694 goto fail;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004695 }
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004696 }
4697 else {
4698 /* no payload, file or log-format string */
4699 if (reply->type == HTTP_REPLY_RAW) {
4700 /* file */
4701 body = &reply->body.obj;
4702 }
4703 else if (reply->type == HTTP_REPLY_LOGFMT) {
4704 /* log-format string */
4705 body = alloc_trash_chunk();
4706 if (!body)
4707 goto fail_alloc;
4708 body->data = build_logline(s, body->area, body->size, &reply->body.fmt);
4709 }
4710 /* else no payload */
4711
4712 status = ultoa(reply->status);
4713 reason = http_get_reason(reply->status);
4714 slflags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_CLEN);
4715 if (!body || !b_data(body))
4716 slflags |= HTX_SL_F_BODYLESS;
4717 sl = htx_add_stline(htx, HTX_BLK_RES_SL, slflags, ist("HTTP/1.1"), ist(status), ist(reason));
4718 if (!sl)
4719 goto fail;
4720 sl->info.res.status = reply->status;
4721
4722 clen = (body ? ultoa(b_data(body)) : "0");
4723 ctype = reply->ctype;
4724
4725 if (!LIST_ISEMPTY(&reply->hdrs)) {
4726 struct http_reply_hdr *hdr;
4727 struct buffer *value = alloc_trash_chunk();
4728
4729 if (!value)
4730 goto fail;
4731
4732 list_for_each_entry(hdr, &reply->hdrs, list) {
4733 chunk_reset(value);
4734 value->data = build_logline(s, value->area, value->size, &hdr->value);
4735 if (b_data(value) && !htx_add_header(htx, hdr->name, ist2(b_head(value), b_data(value)))) {
4736 free_trash_chunk(value);
4737 goto fail;
4738 }
4739 chunk_reset(value);
4740 }
4741 free_trash_chunk(value);
4742 }
4743
4744 if (!htx_add_header(htx, ist("content-length"), ist(clen)) ||
4745 (body && b_data(body) && ctype && !htx_add_header(htx, ist("content-type"), ist(ctype))) ||
4746 !htx_add_endof(htx, HTX_BLK_EOH) ||
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004747 (body && b_data(body) && !htx_add_data_atonce(htx, ist2(b_head(body), b_data(body)))))
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004748 goto fail;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004749
4750 htx->flags |= HTX_FL_EOM;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004751 }
4752
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004753 leave:
4754 if (reply->type == HTTP_REPLY_LOGFMT)
4755 free_trash_chunk(body);
4756 return ret;
4757
4758 fail_alloc:
4759 if (!(s->flags & SF_ERR_MASK))
4760 s->flags |= SF_ERR_RESOURCE;
Christopher Faulet97e466c2020-05-15 15:12:47 +02004761 /* fall through */
4762 fail:
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004763 ret = -1;
4764 goto leave;
Christopher Faulet97e466c2020-05-15 15:12:47 +02004765}
4766
4767/* Send an http reply to the client. On success, it returns 0. If an error
Christopher Faulet40e6b552020-06-25 16:04:50 +02004768 * occurs -1 is returned and the response channel is truncated, removing this
4769 * way the faulty reply. This function may fail when the reply is formatted
4770 * (http_reply_to_htx) or when the reply is forwarded
4771 * (http_forward_proxy_resp). On the last case, it is because a
4772 * http-after-response rule fails.
Christopher Faulet97e466c2020-05-15 15:12:47 +02004773 */
4774int http_reply_message(struct stream *s, struct http_reply *reply)
4775{
4776 struct channel *res = &s->res;
4777 struct htx *htx = htx_from_buf(&res->buf);
4778
4779 if (s->txn->status == -1)
4780 s->txn->status = reply->status;
4781 channel_htx_truncate(res, htx);
4782
4783 if (http_reply_to_htx(s, htx, reply) == -1)
4784 goto fail;
4785
4786 htx_to_buf(htx, &s->res.buf);
4787 if (!http_forward_proxy_resp(s, 1))
4788 goto fail;
4789 return 0;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004790
4791 fail:
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004792 channel_htx_truncate(res, htx);
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004793 if (!(s->flags & SF_ERR_MASK))
4794 s->flags |= SF_ERR_PRXCOND;
Christopher Faulet97e466c2020-05-15 15:12:47 +02004795 return -1;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004796}
4797
Christopher Faulet304cc402019-07-15 15:46:28 +02004798/* Return the error message corresponding to si->err_type. It is assumed
4799 * that the server side is closed. Note that err_type is actually a
4800 * bitmask, where almost only aborts may be cumulated with other
4801 * values. We consider that aborted operations are more important
4802 * than timeouts or errors due to the fact that nobody else in the
4803 * logs might explain incomplete retries. All others should avoid
4804 * being cumulated. It should normally not be possible to have multiple
4805 * aborts at once, but just in case, the first one in sequence is reported.
4806 * Note that connection errors appearing on the second request of a keep-alive
4807 * connection are not reported since this allows the client to retry.
4808 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004809void http_return_srv_error(struct stream *s, struct stream_interface *si)
Christopher Faulet304cc402019-07-15 15:46:28 +02004810{
4811 int err_type = si->err_type;
4812
4813 /* set s->txn->status for http_error_message(s) */
4814 s->txn->status = 503;
4815
4816 if (err_type & SI_ET_QUEUE_ABRT)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004817 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_Q,
4818 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004819 else if (err_type & SI_ET_CONN_ABRT)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004820 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_C,
4821 (s->txn->flags & TX_NOT_FIRST) ? NULL :
4822 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004823 else if (err_type & SI_ET_QUEUE_TO)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004824 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_Q,
4825 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004826 else if (err_type & SI_ET_QUEUE_ERR)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004827 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_Q,
4828 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004829 else if (err_type & SI_ET_CONN_TO)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004830 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_C,
4831 (s->txn->flags & TX_NOT_FIRST) ? NULL :
4832 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004833 else if (err_type & SI_ET_CONN_ERR)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004834 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_C,
4835 (s->flags & SF_SRV_REUSED) ? NULL :
4836 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004837 else if (err_type & SI_ET_CONN_RES)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004838 http_server_error(s, si, SF_ERR_RESOURCE, SF_FINST_C,
4839 (s->txn->flags & TX_NOT_FIRST) ? NULL :
4840 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004841 else { /* SI_ET_CONN_OTHER and others */
4842 s->txn->status = 500;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004843 http_server_error(s, si, SF_ERR_INTERNAL, SF_FINST_C,
4844 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004845 }
4846}
4847
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004848
Christopher Faulet4a28a532019-03-01 11:19:40 +01004849/* Handle Expect: 100-continue for HTTP/1.1 messages if necessary. It returns 0
4850 * on success and -1 on error.
4851 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004852static int http_handle_expect_hdr(struct stream *s, struct htx *htx, struct http_msg *msg)
Christopher Faulet4a28a532019-03-01 11:19:40 +01004853{
4854 /* If we have HTTP/1.1 message with a body and Expect: 100-continue,
4855 * then we must send an HTTP/1.1 100 Continue intermediate response.
4856 */
4857 if (msg->msg_state == HTTP_MSG_BODY && (msg->flags & HTTP_MSGF_VER_11) &&
4858 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
4859 struct ist hdr = { .ptr = "Expect", .len = 6 };
4860 struct http_hdr_ctx ctx;
4861
4862 ctx.blk = NULL;
4863 /* Expect is allowed in 1.1, look for it */
4864 if (http_find_header(htx, hdr, &ctx, 0) &&
4865 unlikely(isteqi(ctx.value, ist2("100-continue", 12)))) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004866 if (http_reply_100_continue(s) == -1)
Christopher Faulet4a28a532019-03-01 11:19:40 +01004867 return -1;
4868 http_remove_header(htx, &ctx);
4869 }
4870 }
4871 return 0;
4872}
4873
Christopher Faulet23a3c792018-11-28 10:01:23 +01004874/* Send a 100-Continue response to the client. It returns 0 on success and -1
4875 * on error. The response channel is updated accordingly.
4876 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004877static int http_reply_100_continue(struct stream *s)
Christopher Faulet23a3c792018-11-28 10:01:23 +01004878{
4879 struct channel *res = &s->res;
4880 struct htx *htx = htx_from_buf(&res->buf);
4881 struct htx_sl *sl;
4882 unsigned int flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|
4883 HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
Christopher Faulet23a3c792018-11-28 10:01:23 +01004884
4885 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
4886 ist("HTTP/1.1"), ist("100"), ist("Continue"));
4887 if (!sl)
4888 goto fail;
4889 sl->info.res.status = 100;
4890
Christopher Faulet1d5ec092019-06-26 14:23:54 +02004891 if (!htx_add_endof(htx, HTX_BLK_EOH))
Christopher Faulet23a3c792018-11-28 10:01:23 +01004892 goto fail;
4893
Christopher Fauleta72a7e42020-01-28 09:28:11 +01004894 if (!http_forward_proxy_resp(s, 0))
4895 goto fail;
Christopher Faulet23a3c792018-11-28 10:01:23 +01004896 return 0;
4897
4898 fail:
4899 /* If an error occurred, remove the incomplete HTTP response from the
4900 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004901 channel_htx_truncate(res, htx);
Christopher Faulet23a3c792018-11-28 10:01:23 +01004902 return -1;
4903}
4904
Christopher Faulet12c51e22018-11-28 15:59:42 +01004905
Christopher Faulet0f226952018-10-22 09:29:56 +02004906/*
4907 * Capture headers from message <htx> according to header list <cap_hdr>, and
4908 * fill the <cap> pointers appropriately.
4909 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004910static void http_capture_headers(struct htx *htx, char **cap, struct cap_hdr *cap_hdr)
Christopher Faulet0f226952018-10-22 09:29:56 +02004911{
4912 struct cap_hdr *h;
4913 int32_t pos;
4914
Christopher Fauleta3f15502019-05-13 15:27:23 +02004915 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet0f226952018-10-22 09:29:56 +02004916 struct htx_blk *blk = htx_get_blk(htx, pos);
4917 enum htx_blk_type type = htx_get_blk_type(blk);
4918 struct ist n, v;
4919
4920 if (type == HTX_BLK_EOH)
4921 break;
4922 if (type != HTX_BLK_HDR)
4923 continue;
4924
4925 n = htx_get_blk_name(htx, blk);
4926
4927 for (h = cap_hdr; h; h = h->next) {
4928 if (h->namelen && (h->namelen == n.len) &&
4929 (strncasecmp(n.ptr, h->name, h->namelen) == 0)) {
4930 if (cap[h->index] == NULL)
4931 cap[h->index] =
4932 pool_alloc(h->pool);
4933
4934 if (cap[h->index] == NULL) {
4935 ha_alert("HTTP capture : out of memory.\n");
4936 break;
4937 }
4938
4939 v = htx_get_blk_value(htx, blk);
4940 if (v.len > h->len)
4941 v.len = h->len;
4942
4943 memcpy(cap[h->index], v.ptr, v.len);
4944 cap[h->index][v.len]=0;
4945 }
4946 }
4947 }
4948}
4949
Christopher Faulet0b6bdc52018-10-24 11:05:36 +02004950/* Delete a value in a header between delimiters <from> and <next>. The header
4951 * itself is delimited by <start> and <end> pointers. The number of characters
4952 * displaced is returned, and the pointer to the first delimiter is updated if
4953 * required. The function tries as much as possible to respect the following
4954 * principles :
4955 * - replace <from> delimiter by the <next> one unless <from> points to <start>,
4956 * in which case <next> is simply removed
4957 * - set exactly one space character after the new first delimiter, unless there
4958 * are not enough characters in the block being moved to do so.
4959 * - remove unneeded spaces before the previous delimiter and after the new
4960 * one.
4961 *
4962 * It is the caller's responsibility to ensure that :
4963 * - <from> points to a valid delimiter or <start> ;
4964 * - <next> points to a valid delimiter or <end> ;
4965 * - there are non-space chars before <from>.
4966 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004967static int http_del_hdr_value(char *start, char *end, char **from, char *next)
Christopher Faulet0b6bdc52018-10-24 11:05:36 +02004968{
4969 char *prev = *from;
4970
4971 if (prev == start) {
4972 /* We're removing the first value. eat the semicolon, if <next>
4973 * is lower than <end> */
4974 if (next < end)
4975 next++;
4976
4977 while (next < end && HTTP_IS_SPHT(*next))
4978 next++;
4979 }
4980 else {
4981 /* Remove useless spaces before the old delimiter. */
4982 while (HTTP_IS_SPHT(*(prev-1)))
4983 prev--;
4984 *from = prev;
4985
4986 /* copy the delimiter and if possible a space if we're
4987 * not at the end of the line.
4988 */
4989 if (next < end) {
4990 *prev++ = *next++;
4991 if (prev + 1 < next)
4992 *prev++ = ' ';
4993 while (next < end && HTTP_IS_SPHT(*next))
4994 next++;
4995 }
4996 }
4997 memmove(prev, next, end - next);
4998 return (prev - next);
4999}
5000
Christopher Faulet0f226952018-10-22 09:29:56 +02005001
5002/* Formats the start line of the request (without CRLF) and puts it in <str> and
Joseph Herlantc42c0e92018-11-25 10:43:27 -08005003 * return the written length. The line can be truncated if it exceeds <len>.
Christopher Faulet0f226952018-10-22 09:29:56 +02005004 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005005static size_t http_fmt_req_line(const struct htx_sl *sl, char *str, size_t len)
Christopher Faulet0f226952018-10-22 09:29:56 +02005006{
5007 struct ist dst = ist2(str, 0);
5008
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005009 if (istcat(&dst, htx_sl_req_meth(sl), len) == -1)
Christopher Faulet0f226952018-10-22 09:29:56 +02005010 goto end;
5011 if (dst.len + 1 > len)
5012 goto end;
5013 dst.ptr[dst.len++] = ' ';
5014
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005015 if (istcat(&dst, htx_sl_req_uri(sl), len) == -1)
Christopher Faulet0f226952018-10-22 09:29:56 +02005016 goto end;
5017 if (dst.len + 1 > len)
5018 goto end;
5019 dst.ptr[dst.len++] = ' ';
5020
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005021 istcat(&dst, htx_sl_req_vsn(sl), len);
Christopher Faulet0f226952018-10-22 09:29:56 +02005022 end:
5023 return dst.len;
5024}
5025
5026/*
5027 * Print a debug line with a start line.
5028 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005029static void http_debug_stline(const char *dir, struct stream *s, const struct htx_sl *sl)
Christopher Faulet0f226952018-10-22 09:29:56 +02005030{
5031 struct session *sess = strm_sess(s);
5032 int max;
5033
5034 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
5035 dir,
5036 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->handle.fd : -1,
5037 objt_cs(s->si[1].end) ? (unsigned short)objt_cs(s->si[1].end)->conn->handle.fd : -1);
5038
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005039 max = HTX_SL_P1_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02005040 UBOUND(max, trash.size - trash.data - 3);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005041 chunk_memcat(&trash, HTX_SL_P1_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02005042 trash.area[trash.data++] = ' ';
5043
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005044 max = HTX_SL_P2_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02005045 UBOUND(max, trash.size - trash.data - 2);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005046 chunk_memcat(&trash, HTX_SL_P2_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02005047 trash.area[trash.data++] = ' ';
5048
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005049 max = HTX_SL_P3_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02005050 UBOUND(max, trash.size - trash.data - 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005051 chunk_memcat(&trash, HTX_SL_P3_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02005052 trash.area[trash.data++] = '\n';
5053
Willy Tarreau2e8ab6b2020-03-14 11:03:20 +01005054 DISGUISE(write(1, trash.area, trash.data));
Christopher Faulet0f226952018-10-22 09:29:56 +02005055}
5056
5057/*
5058 * Print a debug line with a header.
5059 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005060static 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 +02005061{
5062 struct session *sess = strm_sess(s);
5063 int max;
5064
5065 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
5066 dir,
5067 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->handle.fd : -1,
5068 objt_cs(s->si[1].end) ? (unsigned short)objt_cs(s->si[1].end)->conn->handle.fd : -1);
5069
5070 max = n.len;
5071 UBOUND(max, trash.size - trash.data - 3);
5072 chunk_memcat(&trash, n.ptr, max);
5073 trash.area[trash.data++] = ':';
5074 trash.area[trash.data++] = ' ';
5075
5076 max = v.len;
5077 UBOUND(max, trash.size - trash.data - 1);
5078 chunk_memcat(&trash, v.ptr, max);
5079 trash.area[trash.data++] = '\n';
5080
Willy Tarreau2e8ab6b2020-03-14 11:03:20 +01005081 DISGUISE(write(1, trash.area, trash.data));
Christopher Faulet0f226952018-10-22 09:29:56 +02005082}
5083
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005084/* Allocate a new HTTP transaction for stream <s> unless there is one already.
5085 * In case of allocation failure, everything allocated is freed and NULL is
5086 * returned. Otherwise the new transaction is assigned to the stream and
5087 * returned.
5088 */
5089struct http_txn *http_alloc_txn(struct stream *s)
5090{
5091 struct http_txn *txn = s->txn;
5092
5093 if (txn)
5094 return txn;
5095
5096 txn = pool_alloc(pool_head_http_txn);
5097 if (!txn)
5098 return txn;
5099
5100 s->txn = txn;
5101 return txn;
5102}
5103
5104void http_txn_reset_req(struct http_txn *txn)
5105{
Christopher Faulet1aea50e2020-01-17 16:03:53 +01005106 txn->req.flags = 0;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005107 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
5108}
5109
5110void http_txn_reset_res(struct http_txn *txn)
5111{
Christopher Faulet1aea50e2020-01-17 16:03:53 +01005112 txn->rsp.flags = 0;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005113 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
5114}
5115
5116/*
Christopher Faulet75f619a2021-03-08 19:12:58 +01005117 * Create and initialize a new HTTP transaction for stream <s>. This should be
5118 * used before processing any new request. It returns the transaction or NLULL
5119 * on error.
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005120 */
Christopher Faulet75f619a2021-03-08 19:12:58 +01005121struct http_txn *http_create_txn(struct stream *s)
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005122{
Christopher Faulet75f619a2021-03-08 19:12:58 +01005123 struct http_txn *txn;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005124 struct conn_stream *cs = objt_cs(s->si[0].end);
5125
Christopher Faulet75f619a2021-03-08 19:12:58 +01005126 txn = pool_alloc(pool_head_http_txn);
5127 if (!txn)
5128 return NULL;
5129 s->txn = txn;
5130
Christopher Fauletda831fa2020-10-06 17:58:43 +02005131 txn->flags = ((cs && cs->flags & CS_FL_NOT_FIRST) ? TX_NOT_FIRST : 0);
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005132 txn->status = -1;
Christopher Faulet5cb513a2020-05-13 17:56:56 +02005133 txn->http_reply = NULL;
Willy Tarreau8b507582020-02-25 09:35:07 +01005134 write_u32(txn->cache_hash, 0);
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005135
5136 txn->cookie_first_date = 0;
5137 txn->cookie_last_date = 0;
5138
5139 txn->srv_cookie = NULL;
5140 txn->cli_cookie = NULL;
5141 txn->uri = NULL;
5142
5143 http_txn_reset_req(txn);
5144 http_txn_reset_res(txn);
5145
5146 txn->req.chn = &s->req;
5147 txn->rsp.chn = &s->res;
5148
5149 txn->auth.method = HTTP_AUTH_UNKNOWN;
5150
5151 vars_init(&s->vars_txn, SCOPE_TXN);
5152 vars_init(&s->vars_reqres, SCOPE_REQ);
Christopher Faulet75f619a2021-03-08 19:12:58 +01005153
5154 return txn;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005155}
5156
5157/* to be used at the end of a transaction */
Christopher Faulet75f619a2021-03-08 19:12:58 +01005158void http_destroy_txn(struct stream *s)
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005159{
5160 struct http_txn *txn = s->txn;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005161
5162 /* these ones will have been dynamically allocated */
5163 pool_free(pool_head_requri, txn->uri);
5164 pool_free(pool_head_capture, txn->cli_cookie);
5165 pool_free(pool_head_capture, txn->srv_cookie);
Tim Duesterhusa17e6622020-03-05 20:19:02 +01005166 pool_free(pool_head_uniqueid, s->unique_id.ptr);
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005167
Tim Duesterhusa17e6622020-03-05 20:19:02 +01005168 s->unique_id = IST_NULL;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005169 txn->uri = NULL;
5170 txn->srv_cookie = NULL;
5171 txn->cli_cookie = NULL;
5172
Christopher Faulet59399252019-11-07 14:27:52 +01005173 if (!LIST_ISEMPTY(&s->vars_txn.head))
5174 vars_prune(&s->vars_txn, s->sess, s);
5175 if (!LIST_ISEMPTY(&s->vars_reqres.head))
5176 vars_prune(&s->vars_reqres, s->sess, s);
Christopher Faulet75f619a2021-03-08 19:12:58 +01005177
5178 pool_free(pool_head_http_txn, txn);
5179 s->txn = NULL;
Christopher Faulet59399252019-11-07 14:27:52 +01005180}
5181
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005182
5183DECLARE_POOL(pool_head_http_txn, "http_txn", sizeof(struct http_txn));
Christopher Faulet0f226952018-10-22 09:29:56 +02005184
Christopher Fauletf4eb75d2018-10-11 15:55:07 +02005185__attribute__((constructor))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005186static void __http_protocol_init(void)
Christopher Fauletf4eb75d2018-10-11 15:55:07 +02005187{
5188}
5189
5190
5191/*
5192 * Local variables:
5193 * c-indent-level: 8
5194 * c-basic-offset: 8
5195 * End:
5196 */