blob: 36014718ab26fdc69fd802017cfc515ed6edd052 [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 Tarreau91cefca2022-05-03 17:08:29 +020016#include <haproxy/applet.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020017#include <haproxy/backend.h>
Willy Tarreau8d366972020-05-27 16:10:29 +020018#include <haproxy/base64.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020019#include <haproxy/capture-t.h>
Amaury Denoyelle03517732021-05-07 14:25:01 +020020#include <haproxy/cfgparse.h>
Willy Tarreauf1d32c42020-06-04 21:07:02 +020021#include <haproxy/channel.h>
Willy Tarreau4aa573d2020-06-04 18:21:56 +020022#include <haproxy/check.h>
Willy Tarreau7ea393d2020-06-04 18:02:10 +020023#include <haproxy/connection.h>
Willy Tarreau36979d92020-06-05 17:27:29 +020024#include <haproxy/errors.h>
Willy Tarreauc7babd82020-06-04 21:29:29 +020025#include <haproxy/filters.h>
Willy Tarreaucd72d8c2020-06-02 19:11:26 +020026#include <haproxy/http.h>
Willy Tarreauc2b1ff02020-06-04 21:21:03 +020027#include <haproxy/http_ana.h>
Willy Tarreau87735332020-06-04 09:08:41 +020028#include <haproxy/http_htx.h>
Aurelien DARRAGONb2bb9252022-12-28 15:37:57 +010029#include <haproxy/http_ext.h>
Willy Tarreau16f958c2020-06-03 08:44:35 +020030#include <haproxy/htx.h>
Willy Tarreauaeed4a82020-06-04 22:01:04 +020031#include <haproxy/log.h>
Willy Tarreau6131d6a2020-06-02 16:48:09 +020032#include <haproxy/net_helper.h>
Willy Tarreaua264d962020-06-04 22:29:18 +020033#include <haproxy/proxy.h>
Willy Tarreau7cd8b6e2020-06-02 17:32:26 +020034#include <haproxy/regex.h>
Willy Tarreau5edca2f2022-05-27 09:25:10 +020035#include <haproxy/sc_strm.h>
Willy Tarreau1e56f922020-06-04 23:20:13 +020036#include <haproxy/server-t.h>
Willy Tarreau2eec9b52020-06-04 19:58:55 +020037#include <haproxy/stats.h>
Willy Tarreaucb086c62022-05-27 09:47:12 +020038#include <haproxy/stconn.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020039#include <haproxy/stream.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020040#include <haproxy/trace.h>
Willy Tarreau8c42b8a2020-06-04 19:27:34 +020041#include <haproxy/uri_auth-t.h>
Willy Tarreaua1718922020-06-04 16:25:31 +020042#include <haproxy/vars.h>
Christopher Faulete0768eb2018-10-03 16:38:02 +020043
Christopher Faulete0768eb2018-10-03 16:38:02 +020044
Christopher Fauleteea8fc72019-11-05 16:18:10 +010045#define TRACE_SOURCE &trace_strm
46
Christopher Faulet377c5a52018-10-24 21:21:30 +020047extern const char *stat_status_codes[];
Christopher Fauletf2824e62018-10-01 12:12:37 +020048
Willy Tarreauff882702021-04-10 17:23:00 +020049struct pool_head *pool_head_requri __read_mostly = NULL;
50struct pool_head *pool_head_capture __read_mostly = NULL;
Christopher Fauleta8a46e22019-07-16 14:53:09 +020051
52
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020053static void http_end_request(struct stream *s);
54static void http_end_response(struct stream *s);
Christopher Fauletf2824e62018-10-01 12:12:37 +020055
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020056static void http_capture_headers(struct htx *htx, char **cap, struct cap_hdr *cap_hdr);
57static int http_del_hdr_value(char *start, char *end, char **from, char *next);
58static size_t http_fmt_req_line(const struct htx_sl *sl, char *str, size_t len);
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020059static void http_debug_stline(const char *dir, struct stream *s, const struct htx_sl *sl);
60static 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 +020061
Christopher Fauletd4150ad2021-10-13 15:35:55 +020062static enum rule_result http_req_get_intercept_rule(struct proxy *px, struct list *def_rules, struct list *rules, struct stream *s);
63static enum rule_result http_res_get_intercept_rule(struct proxy *px, struct list *def_rules, struct list *rules, struct stream *s);
Christopher Faulet18c13d32022-05-16 11:43:10 +020064static enum rule_result http_req_restrict_header_names(struct stream *s, struct htx *htx, struct proxy *px);
Christopher Faulet3e964192018-10-24 11:39:23 +020065
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020066static void http_manage_client_side_cookies(struct stream *s, struct channel *req);
67static void http_manage_server_side_cookies(struct stream *s, struct channel *res);
Christopher Fauletfcda7c62018-10-24 11:56:22 +020068
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020069static int http_stats_check_uri(struct stream *s, struct http_txn *txn, struct proxy *backend);
70static int http_handle_stats(struct stream *s, struct channel *req);
Christopher Faulet377c5a52018-10-24 21:21:30 +020071
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020072static int http_handle_expect_hdr(struct stream *s, struct htx *htx, struct http_msg *msg);
73static int http_reply_100_continue(struct stream *s);
Christopher Faulet23a3c792018-11-28 10:01:23 +010074
Christopher Faulete0768eb2018-10-03 16:38:02 +020075/* This stream analyser waits for a complete HTTP request. It returns 1 if the
76 * processing can continue on next analysers, or zero if it either needs more
77 * data or wants to immediately abort the request (eg: timeout, error, ...). It
78 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req.analysers
79 * when it has nothing left to do, and may remove any analyser when it wants to
80 * abort.
81 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020082int http_wait_for_request(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +020083{
Christopher Faulet9768c262018-10-22 09:34:31 +020084
Christopher Faulete0768eb2018-10-03 16:38:02 +020085 /*
Christopher Faulet9768c262018-10-22 09:34:31 +020086 * We will analyze a complete HTTP request to check the its syntax.
Christopher Faulete0768eb2018-10-03 16:38:02 +020087 *
Christopher Faulet9768c262018-10-22 09:34:31 +020088 * Once the start line and all headers are received, we may perform a
89 * capture of the error (if any), and we will set a few fields. We also
90 * check for monitor-uri, logging and finally headers capture.
Christopher Faulete0768eb2018-10-03 16:38:02 +020091 */
Christopher Faulete0768eb2018-10-03 16:38:02 +020092 struct session *sess = s->sess;
93 struct http_txn *txn = s->txn;
94 struct http_msg *msg = &txn->req;
Christopher Faulet9768c262018-10-22 09:34:31 +020095 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +010096 struct htx_sl *sl;
Christopher Faulete0768eb2018-10-03 16:38:02 +020097
Christopher Fauleteea8fc72019-11-05 16:18:10 +010098 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +020099
Christopher Fauletda46a0d2021-01-21 17:32:58 +0100100 if (unlikely(!IS_HTX_STRM(s))) {
101 /* It is only possible when a TCP stream is upgrade to HTTP.
102 * There is a transition period during which there is no
103 * data. The stream is still in raw mode and SF_IGNORE flag is
104 * still set. When this happens, the new mux is responsible to
Ilya Shipitsinacf84592021-02-06 22:29:08 +0500105 * handle all errors. Thus we may leave immediately.
Christopher Fauletda46a0d2021-01-21 17:32:58 +0100106 */
107 BUG_ON(!(s->flags & SF_IGNORE) || !c_empty(&s->req));
Christopher Faulet9768c262018-10-22 09:34:31 +0200108
Christopher Faulet97b3a612021-03-15 17:10:12 +0100109 /* Don't connect for now */
110 channel_dont_connect(req);
111
112 /* A SHUTR at this stage means we are performing a "destructive"
113 * HTTP upgrade (TCP>H2). In this case, we can leave.
114 */
115 if (req->flags & CF_SHUTR) {
116 s->logs.logwait = 0;
117 s->logs.level = 0;
118 channel_abort(&s->req);
119 channel_abort(&s->res);
120 req->analysers &= AN_REQ_FLT_END;
121 req->analyse_exp = TICK_ETERNITY;
122 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA, s);
123 return 1;
124 }
Christopher Fauletda46a0d2021-01-21 17:32:58 +0100125 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA, s);
126 return 0;
127 }
128
129 htx = htxbuf(&req->buf);
Christopher Faulet8bebd2f2020-10-06 17:54:56 +0200130
Willy Tarreau4236f032019-03-05 10:43:32 +0100131 /* Parsing errors are caught here */
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200132 if (htx->flags & (HTX_FL_PARSING_ERROR|HTX_FL_PROCESSING_ERROR)) {
Willy Tarreau4236f032019-03-05 10:43:32 +0100133 stream_inc_http_req_ctr(s);
Emeric Brun28976442020-10-07 08:50:09 +0200134 proxy_inc_fe_req_ctr(sess->listener, sess->fe);
Christopher Fauletbf7175f2021-02-10 14:58:01 +0100135 if (htx->flags & HTX_FL_PARSING_ERROR) {
136 stream_inc_http_err_ctr(s);
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200137 goto return_bad_req;
Christopher Fauletbf7175f2021-02-10 14:58:01 +0100138 }
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200139 else
140 goto return_int_err;
Willy Tarreau4236f032019-03-05 10:43:32 +0100141 }
142
Christopher Faulete0768eb2018-10-03 16:38:02 +0200143 /* we're speaking HTTP here, so let's speak HTTP to the client */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200144 s->srv_error = http_return_srv_error;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200145
Christopher Faulet9768c262018-10-22 09:34:31 +0200146 msg->msg_state = HTTP_MSG_BODY;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200147 stream_inc_http_req_ctr(s);
Emeric Brun28976442020-10-07 08:50:09 +0200148 proxy_inc_fe_req_ctr(sess->listener, sess->fe); /* one more valid request for this FE */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200149
Christopher Faulet9768c262018-10-22 09:34:31 +0200150 /* kill the pending keep-alive timeout */
Christopher Faulet9768c262018-10-22 09:34:31 +0200151 req->analyse_exp = TICK_ETERNITY;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200152
Christopher Faulet29f17582019-05-23 11:03:26 +0200153 BUG_ON(htx_get_first_type(htx) != HTX_BLK_REQ_SL);
Christopher Faulet297fbb42019-05-13 14:41:27 +0200154 sl = http_get_stline(htx);
Christopher Faulet03599112018-11-27 11:21:21 +0100155
Christopher Faulet9768c262018-10-22 09:34:31 +0200156 /* 0: we might have to print this header in debug mode */
157 if (unlikely((global.mode & MODE_DEBUG) &&
158 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
159 int32_t pos;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200160
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200161 http_debug_stline("clireq", s, sl);
Christopher Faulet9768c262018-10-22 09:34:31 +0200162
Christopher Fauleta3f15502019-05-13 15:27:23 +0200163 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet9768c262018-10-22 09:34:31 +0200164 struct htx_blk *blk = htx_get_blk(htx, pos);
165 enum htx_blk_type type = htx_get_blk_type(blk);
166
167 if (type == HTX_BLK_EOH)
168 break;
169 if (type != HTX_BLK_HDR)
170 continue;
171
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200172 http_debug_hdr("clihdr", s,
173 htx_get_blk_name(htx, blk),
174 htx_get_blk_value(htx, blk));
Christopher Faulet9768c262018-10-22 09:34:31 +0200175 }
176 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200177
178 /*
Christopher Faulet03599112018-11-27 11:21:21 +0100179 * 1: identify the method and the version. Also set HTTP flags
Christopher Faulete0768eb2018-10-03 16:38:02 +0200180 */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100181 txn->meth = sl->info.req.meth;
Christopher Faulet03599112018-11-27 11:21:21 +0100182 if (sl->flags & HTX_SL_F_VER_11)
Christopher Faulet9768c262018-10-22 09:34:31 +0200183 msg->flags |= HTTP_MSGF_VER_11;
Christopher Faulet03599112018-11-27 11:21:21 +0100184 msg->flags |= HTTP_MSGF_XFER_LEN;
Christopher Faulet2a408542020-11-20 14:22:37 +0100185 if (sl->flags & HTX_SL_F_CLEN)
186 msg->flags |= HTTP_MSGF_CNT_LEN;
187 else if (sl->flags & HTX_SL_F_CHNK)
188 msg->flags |= HTTP_MSGF_TE_CHNK;
Christopher Fauletb2db4fa2018-11-27 16:51:09 +0100189 if (sl->flags & HTX_SL_F_BODYLESS)
190 msg->flags |= HTTP_MSGF_BODYLESS;
Christopher Faulet576c3582021-01-08 15:53:01 +0100191 if (sl->flags & HTX_SL_F_CONN_UPG)
192 msg->flags |= HTTP_MSGF_CONN_UPG;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200193
194 /* we can make use of server redirect on GET and HEAD */
195 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
196 s->flags |= SF_REDIRECTABLE;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100197 else if (txn->meth == HTTP_METH_OTHER && isteqi(htx_sl_req_meth(sl), ist("PRI"))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200198 /* PRI is reserved for the HTTP/2 preface */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200199 goto return_bad_req;
200 }
201
202 /*
Christopher Faulet6072beb2020-02-18 15:34:58 +0100203 * 2: check if the URI matches the monitor_uri. We have to do this for
204 * every request which gets in, because the monitor-uri is defined by
205 * the frontend. If the monitor-uri starts with a '/', the matching is
206 * done against the request's path. Otherwise, the request's uri is
207 * used. It is a workaround to let HTTP/2 health-checks work as
208 * expected.
Christopher Faulete0768eb2018-10-03 16:38:02 +0200209 */
Tim Duesterhus4b1fcaa2022-03-05 00:52:40 +0100210 if (unlikely(isttest(sess->fe->monitor_uri))) {
211 const struct ist monitor_uri = sess->fe->monitor_uri;
Amaury Denoyellec453f952021-07-06 11:40:12 +0200212 struct http_uri_parser parser = http_uri_parser_init(htx_sl_req_uri(sl));
Christopher Faulete0768eb2018-10-03 16:38:02 +0200213
Amaury Denoyelle5a9bd372021-07-06 11:23:10 +0200214 if ((istptr(monitor_uri)[0] == '/' &&
Amaury Denoyellec453f952021-07-06 11:40:12 +0200215 isteq(http_parse_path(&parser), monitor_uri)) ||
Amaury Denoyelle5a9bd372021-07-06 11:23:10 +0200216 isteq(htx_sl_req_uri(sl), monitor_uri)) {
217 /*
218 * We have found the monitor URI
219 */
220 struct acl_cond *cond;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200221
Amaury Denoyelle5a9bd372021-07-06 11:23:10 +0200222 s->flags |= SF_MONITOR;
223 _HA_ATOMIC_INC(&sess->fe->fe_counters.intercepted_req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200224
Amaury Denoyelle5a9bd372021-07-06 11:23:10 +0200225 /* Check if we want to fail this monitor request or not */
226 list_for_each_entry(cond, &sess->fe->mon_fail_cond, list) {
227 int ret = acl_exec_cond(cond, sess->fe, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200228
Amaury Denoyelle5a9bd372021-07-06 11:23:10 +0200229 ret = acl_pass(ret);
230 if (cond->pol == ACL_COND_UNLESS)
231 ret = !ret;
232
233 if (ret) {
234 /* we fail this request, let's return 503 service unavail */
235 txn->status = 503;
236 if (!(s->flags & SF_ERR_MASK))
237 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
238 goto return_prx_cond;
239 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200240 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200241
Amaury Denoyelle5a9bd372021-07-06 11:23:10 +0200242 /* nothing to fail, let's reply normally */
243 txn->status = 200;
244 if (!(s->flags & SF_ERR_MASK))
245 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
246 goto return_prx_cond;
247 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200248 }
249
250 /*
251 * 3: Maybe we have to copy the original REQURI for the logs ?
252 * Note: we cannot log anymore if the request has been
253 * classified as invalid.
254 */
255 if (unlikely(s->logs.logwait & LW_REQ)) {
256 /* we have a complete HTTP request that we must log */
257 if ((txn->uri = pool_alloc(pool_head_requri)) != NULL) {
Christopher Faulet9768c262018-10-22 09:34:31 +0200258 size_t len;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200259
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200260 len = http_fmt_req_line(sl, txn->uri, global.tune.requri_len - 1);
Christopher Faulet9768c262018-10-22 09:34:31 +0200261 txn->uri[len] = 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200262
263 if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT)))
264 s->do_log(s);
265 } else {
266 ha_alert("HTTP logging : out of memory.\n");
267 }
268 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200269
Christopher Faulete0768eb2018-10-03 16:38:02 +0200270 /* if the frontend has "option http-use-proxy-header", we'll check if
271 * we have what looks like a proxied connection instead of a connection,
272 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
273 * Note that this is *not* RFC-compliant, however browsers and proxies
274 * happen to do that despite being non-standard :-(
275 * We consider that a request not beginning with either '/' or '*' is
276 * a proxied connection, which covers both "scheme://location" and
277 * CONNECT ip:port.
278 */
279 if ((sess->fe->options2 & PR_O2_USE_PXHDR) &&
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100280 *HTX_SL_REQ_UPTR(sl) != '/' && *HTX_SL_REQ_UPTR(sl) != '*')
Christopher Faulete0768eb2018-10-03 16:38:02 +0200281 txn->flags |= TX_USE_PX_CONN;
282
Christopher Faulete0768eb2018-10-03 16:38:02 +0200283 /* 5: we may need to capture headers */
284 if (unlikely((s->logs.logwait & LW_REQHDR) && s->req_cap))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200285 http_capture_headers(htx, s->req_cap, sess->fe->req_cap);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200286
Christopher Faulete0768eb2018-10-03 16:38:02 +0200287 /* we may have to wait for the request's body */
Christopher Faulet9768c262018-10-22 09:34:31 +0200288 if (s->be->options & PR_O_WREQ_BODY)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200289 req->analysers |= AN_REQ_HTTP_BODY;
290
291 /*
292 * RFC7234#4:
293 * A cache MUST write through requests with methods
294 * that are unsafe (Section 4.2.1 of [RFC7231]) to
295 * the origin server; i.e., a cache is not allowed
296 * to generate a reply to such a request before
297 * having forwarded the request and having received
298 * a corresponding response.
299 *
300 * RFC7231#4.2.1:
301 * Of the request methods defined by this
302 * specification, the GET, HEAD, OPTIONS, and TRACE
303 * methods are defined to be safe.
304 */
305 if (likely(txn->meth == HTTP_METH_GET ||
306 txn->meth == HTTP_METH_HEAD ||
307 txn->meth == HTTP_METH_OPTIONS ||
308 txn->meth == HTTP_METH_TRACE))
309 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
310
311 /* end of job, return OK */
312 req->analysers &= ~an_bit;
313 req->analyse_exp = TICK_ETERNITY;
Christopher Faulet9768c262018-10-22 09:34:31 +0200314
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100315 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200316 return 1;
317
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200318 return_int_err:
319 txn->status = 500;
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200320 if (!(s->flags & SF_ERR_MASK))
321 s->flags |= SF_ERR_INTERNAL;
Willy Tarreau4781b152021-04-06 13:53:36 +0200322 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
William Lallemand36119de2021-03-08 15:26:48 +0100323 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200324 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200325 goto return_prx_cond;
326
Christopher Faulete0768eb2018-10-03 16:38:02 +0200327 return_bad_req:
Christopher Faulet9768c262018-10-22 09:34:31 +0200328 txn->status = 400;
Willy Tarreau4781b152021-04-06 13:53:36 +0200329 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req);
William Lallemand36119de2021-03-08 15:26:48 +0100330 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200331 _HA_ATOMIC_INC(&sess->listener->counters->failed_req);
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200332 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200333
334 return_prx_cond:
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200335 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet4a66c942023-01-13 09:43:21 +0100336 http_set_term_flags(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200337
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100338 DBG_TRACE_DEVEL("leaving on error",
339 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200340 return 0;
341}
342
343
344/* This stream analyser runs all HTTP request processing which is common to
345 * frontends and backends, which means blocking ACLs, filters, connection-close,
346 * reqadd, stats and redirects. This is performed for the designated proxy.
347 * It returns 1 if the processing can continue on next analysers, or zero if it
348 * either needs more data or wants to immediately abort the request (eg: deny,
349 * error, ...).
350 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200351int http_process_req_common(struct stream *s, struct channel *req, int an_bit, struct proxy *px)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200352{
Christopher Fauletd4150ad2021-10-13 15:35:55 +0200353 struct list *def_rules, *rules;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200354 struct session *sess = s->sess;
355 struct http_txn *txn = s->txn;
356 struct http_msg *msg = &txn->req;
Christopher Fauletff2759f2018-10-24 11:13:16 +0200357 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200358 struct redirect_rule *rule;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200359 enum rule_result verdict;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200360 struct connection *conn = objt_conn(sess->origin);
361
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100362 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200363
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100364 htx = htxbuf(&req->buf);
Christopher Fauletff2759f2018-10-24 11:13:16 +0200365
Christopher Faulet1907ccc2019-04-29 13:12:02 +0200366 /* just in case we have some per-backend tracking. Only called the first
367 * execution of the analyser. */
Christopher Fauletd4150ad2021-10-13 15:35:55 +0200368 if (!s->current_rule && !s->current_rule_list)
Christopher Faulet1907ccc2019-04-29 13:12:02 +0200369 stream_inc_be_http_req_ctr(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200370
Christopher Fauletd4150ad2021-10-13 15:35:55 +0200371 def_rules = ((px->defpx && (an_bit == AN_REQ_HTTP_PROCESS_FE || px != sess->fe)) ? &px->defpx->http_req_rules : NULL);
372 rules = &px->http_req_rules;
373
Christopher Faulete0768eb2018-10-03 16:38:02 +0200374 /* evaluate http-request rules */
Christopher Fauletd4150ad2021-10-13 15:35:55 +0200375 if ((def_rules && !LIST_ISEMPTY(def_rules)) || !LIST_ISEMPTY(rules)) {
376 verdict = http_req_get_intercept_rule(px, def_rules, rules, s);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200377
378 switch (verdict) {
379 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
380 goto return_prx_yield;
381
382 case HTTP_RULE_RES_CONT:
383 case HTTP_RULE_RES_STOP: /* nothing to do */
384 break;
385
386 case HTTP_RULE_RES_DENY: /* deny or tarpit */
387 if (txn->flags & TX_CLTARPIT)
388 goto tarpit;
389 goto deny;
390
391 case HTTP_RULE_RES_ABRT: /* abort request, response already sent. Eg: auth */
392 goto return_prx_cond;
393
394 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
395 goto done;
396
397 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
398 goto return_bad_req;
Christopher Faulet3a26bee2019-12-16 12:47:40 +0100399
400 case HTTP_RULE_RES_ERROR: /* failed with a bad request */
401 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200402 }
403 }
404
Christopher Faulet18c13d32022-05-16 11:43:10 +0200405 if (px->options2 & (PR_O2_RSTRICT_REQ_HDR_NAMES_BLK|PR_O2_RSTRICT_REQ_HDR_NAMES_DEL)) {
406 verdict = http_req_restrict_header_names(s, htx, px);
407 if (verdict == HTTP_RULE_RES_DENY)
408 goto deny;
409 }
410
Christopher Faulete0768eb2018-10-03 16:38:02 +0200411 if (conn && (conn->flags & CO_FL_EARLY_DATA) &&
Olivier Houchard220a26c2020-01-23 14:57:36 +0100412 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_SSL_WAIT_HS))) {
Christopher Fauletff2759f2018-10-24 11:13:16 +0200413 struct http_hdr_ctx ctx;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200414
Christopher Fauletff2759f2018-10-24 11:13:16 +0200415 ctx.blk = NULL;
416 if (!http_find_header(htx, ist("Early-Data"), &ctx, 0)) {
417 if (unlikely(!http_add_header(htx, ist("Early-Data"), ist("1"))))
Christopher Fauletd649b572022-06-01 17:42:35 +0200418 goto return_fail_rewrite;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200419 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200420 }
421
422 /* OK at this stage, we know that the request was accepted according to
423 * the http-request rules, we can check for the stats. Note that the
424 * URI is detected *before* the req* rules in order not to be affected
425 * by a possible reqrep, while they are processed *after* so that a
426 * reqdeny can still block them. This clearly needs to change in 1.6!
427 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200428 if (!s->target && http_stats_check_uri(s, txn, px)) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200429 s->target = &http_stats_applet.obj_type;
Willy Tarreaua0b58b52022-05-27 08:33:53 +0200430 if (unlikely(!sc_applet_create(s->scb, objt_applet(s->target)))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200431 s->logs.tv_request = now;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200432 if (!(s->flags & SF_ERR_MASK))
433 s->flags |= SF_ERR_RESOURCE;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100434 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200435 }
436
437 /* parse the whole stats request and extract the relevant information */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200438 http_handle_stats(s, req);
Christopher Fauletd4150ad2021-10-13 15:35:55 +0200439 verdict = http_req_get_intercept_rule(px, NULL, &px->uri_auth->http_req_rules, s);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200440 /* not all actions implemented: deny, allow, auth */
441
442 if (verdict == HTTP_RULE_RES_DENY) /* stats http-request deny */
443 goto deny;
444
445 if (verdict == HTTP_RULE_RES_ABRT) /* stats auth / stats http-request auth */
446 goto return_prx_cond;
Christopher Faulet3a26bee2019-12-16 12:47:40 +0100447
448 if (verdict == HTTP_RULE_RES_BADREQ) /* failed with a bad request */
449 goto return_bad_req;
450
451 if (verdict == HTTP_RULE_RES_ERROR) /* failed with a bad request */
452 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200453 }
454
Christopher Faulet2571bc62019-03-01 11:44:26 +0100455 /* Proceed with the applets now. */
456 if (unlikely(objt_applet(s->target))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200457 if (sess->fe == s->be) /* report it if the request was intercepted by the frontend */
Willy Tarreau4781b152021-04-06 13:53:36 +0200458 _HA_ATOMIC_INC(&sess->fe->fe_counters.intercepted_req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200459
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200460 if (http_handle_expect_hdr(s, htx, msg) == -1)
Christopher Fauletb8a53712019-12-16 11:29:38 +0100461 goto return_int_err;
Christopher Fauletbcf242a2019-03-01 11:36:26 +0100462
Christopher Faulete0768eb2018-10-03 16:38:02 +0200463 if (!(s->flags & SF_ERR_MASK)) // this is not really an error but it is
464 s->flags |= SF_ERR_LOCAL; // to mark that it comes from the proxy
Christopher Faulet4a66c942023-01-13 09:43:21 +0100465 http_set_term_flags(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200466
Christopher Fauletc2ac5e42021-03-08 18:20:09 +0100467 if (HAS_FILTERS(s))
468 req->analysers |= AN_REQ_FLT_HTTP_HDRS;
469
Christopher Faulete0768eb2018-10-03 16:38:02 +0200470 /* enable the minimally required analyzers to handle keep-alive and compression on the HTTP response */
471 req->analysers &= (AN_REQ_HTTP_BODY | AN_REQ_FLT_HTTP_HDRS | AN_REQ_FLT_END);
472 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
473 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Christopher Fauletbcf242a2019-03-01 11:36:26 +0100474
475 req->flags |= CF_SEND_DONTWAIT;
476 s->flags |= SF_ASSIGNED;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200477 goto done;
478 }
479
480 /* check whether we have some ACLs set to redirect this request */
481 list_for_each_entry(rule, &px->redirect_rules, list) {
482 if (rule->cond) {
483 int ret;
484
485 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
486 ret = acl_pass(ret);
487 if (rule->cond->pol == ACL_COND_UNLESS)
488 ret = !ret;
489 if (!ret)
490 continue;
491 }
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200492 if (!http_apply_redirect_rule(rule, s, txn))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100493 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200494 goto done;
495 }
496
497 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
498 * If this happens, then the data will not come immediately, so we must
499 * send all what we have without waiting. Note that due to the small gain
500 * in waiting for the body of the request, it's easier to simply put the
501 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
502 * itself once used.
503 */
504 req->flags |= CF_SEND_DONTWAIT;
505
506 done: /* done with this analyser, continue with next ones that the calling
507 * points will have set, if any.
508 */
509 req->analyse_exp = TICK_ETERNITY;
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +0500510 done_without_exp: /* done with this analyser, but don't reset the analyse_exp. */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200511 req->analysers &= ~an_bit;
Christopher Fauletd4150ad2021-10-13 15:35:55 +0200512 s->current_rule = s->current_rule_list = NULL;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100513 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200514 return 1;
515
516 tarpit:
517 /* Allow cookie logging
518 */
519 if (s->be->cookie_name || sess->fe->capture_name)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200520 http_manage_client_side_cookies(s, req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200521
522 /* When a connection is tarpitted, we use the tarpit timeout,
523 * which may be the same as the connect timeout if unspecified.
524 * If unset, then set it to zero because we really want it to
525 * eventually expire. We build the tarpit as an analyser.
526 */
Christopher Faulet202c6ce2019-01-07 14:57:35 +0100527 channel_htx_erase(&s->req, htx);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200528
529 /* wipe the request out so that we can drop the connection early
530 * if the client closes first.
531 */
532 channel_dont_connect(req);
533
Christopher Faulete0768eb2018-10-03 16:38:02 +0200534 req->analysers &= AN_REQ_FLT_END; /* remove switching rules etc... */
535 req->analysers |= AN_REQ_HTTP_TARPIT;
536 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
537 if (!req->analyse_exp)
538 req->analyse_exp = tick_add(now_ms, 0);
539 stream_inc_http_err_ctr(s);
Willy Tarreau4781b152021-04-06 13:53:36 +0200540 _HA_ATOMIC_INC(&sess->fe->fe_counters.denied_req);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100541 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau4781b152021-04-06 13:53:36 +0200542 _HA_ATOMIC_INC(&s->be->be_counters.denied_req);
William Lallemand36119de2021-03-08 15:26:48 +0100543 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200544 _HA_ATOMIC_INC(&sess->listener->counters->denied_req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200545 goto done_without_exp;
546
547 deny: /* this request was blocked (denied) */
548
549 /* Allow cookie logging
550 */
551 if (s->be->cookie_name || sess->fe->capture_name)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200552 http_manage_client_side_cookies(s, req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200553
Christopher Faulete0768eb2018-10-03 16:38:02 +0200554 s->logs.tv_request = now;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200555 stream_inc_http_err_ctr(s);
Willy Tarreau4781b152021-04-06 13:53:36 +0200556 _HA_ATOMIC_INC(&sess->fe->fe_counters.denied_req);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100557 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau4781b152021-04-06 13:53:36 +0200558 _HA_ATOMIC_INC(&s->be->be_counters.denied_req);
William Lallemand36119de2021-03-08 15:26:48 +0100559 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200560 _HA_ATOMIC_INC(&sess->listener->counters->denied_req);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100561 goto return_prx_err;
562
Christopher Fauletd649b572022-06-01 17:42:35 +0200563 return_fail_rewrite:
564 if (!(s->flags & SF_ERR_MASK))
565 s->flags |= SF_ERR_PRXCOND;
566 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_rewrites);
567 if (s->flags & SF_BE_ASSIGNED)
568 _HA_ATOMIC_INC(&s->be->be_counters.failed_rewrites);
569 if (sess->listener && sess->listener->counters)
570 _HA_ATOMIC_INC(&sess->listener->counters->failed_rewrites);
571 if (objt_server(s->target))
572 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_rewrites);
573 /* fall through */
574
Christopher Fauletb8a53712019-12-16 11:29:38 +0100575 return_int_err:
576 txn->status = 500;
577 if (!(s->flags & SF_ERR_MASK))
578 s->flags |= SF_ERR_INTERNAL;
Willy Tarreau4781b152021-04-06 13:53:36 +0200579 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100580 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau4781b152021-04-06 13:53:36 +0200581 _HA_ATOMIC_INC(&s->be->be_counters.internal_errors);
William Lallemand36119de2021-03-08 15:26:48 +0100582 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200583 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100584 goto return_prx_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200585
586 return_bad_req:
Christopher Faulete0768eb2018-10-03 16:38:02 +0200587 txn->status = 400;
Willy Tarreau4781b152021-04-06 13:53:36 +0200588 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req);
William Lallemand36119de2021-03-08 15:26:48 +0100589 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200590 _HA_ATOMIC_INC(&sess->listener->counters->failed_req);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100591 /* fall through */
592
593 return_prx_err:
594 http_reply_and_close(s, txn->status, http_error_message(s));
595 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200596
597 return_prx_cond:
Christopher Faulet4a66c942023-01-13 09:43:21 +0100598 http_set_term_flags(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200599
600 req->analysers &= AN_REQ_FLT_END;
601 req->analyse_exp = TICK_ETERNITY;
Christopher Fauletd4150ad2021-10-13 15:35:55 +0200602 s->current_rule = s->current_rule_list = NULL;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100603 DBG_TRACE_DEVEL("leaving on error",
604 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200605 return 0;
606
607 return_prx_yield:
608 channel_dont_connect(req);
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100609 DBG_TRACE_DEVEL("waiting for more data",
610 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200611 return 0;
612}
613
614/* This function performs all the processing enabled for the current request.
615 * It returns 1 if the processing can continue on next analysers, or zero if it
616 * needs more data, encounters an error, or wants to immediately abort the
617 * request. It relies on buffers flags, and updates s->req.analysers.
618 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200619int http_process_request(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200620{
621 struct session *sess = s->sess;
622 struct http_txn *txn = s->txn;
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200623 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200624 struct connection *cli_conn = objt_conn(strm_sess(s)->origin);
625
Christopher Faulet8bebd2f2020-10-06 17:54:56 +0200626 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200627
628 /*
629 * Right now, we know that we have processed the entire headers
630 * and that unwanted requests have been filtered out. We can do
631 * whatever we want with the remaining request. Also, now we
632 * may have separate values for ->fe, ->be.
633 */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100634 htx = htxbuf(&req->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200635
636 /*
Christopher Faulete0768eb2018-10-03 16:38:02 +0200637 * 7: Now we can work with the cookies.
638 * Note that doing so might move headers in the request, but
639 * the fields will stay coherent and the URI will not move.
640 * This should only be performed in the backend.
641 */
642 if (s->be->cookie_name || sess->fe->capture_name)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200643 http_manage_client_side_cookies(s, req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200644
Tim Duesterhus2825b4b2020-02-28 15:13:34 +0100645 /* 8: Generate unique ID if a "unique-id-format" is defined.
646 *
647 * A unique ID is generated even when it is not sent to ensure that the ID can make use of
648 * fetches only available in the HTTP request processing stage.
649 */
650 if (!LIST_ISEMPTY(&sess->fe->format_unique_id)) {
Tim Duesterhusa17e6622020-03-05 20:19:02 +0100651 struct ist unique_id = stream_generate_unique_id(s, &sess->fe->format_unique_id);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200652
Tim Duesterhusa17e6622020-03-05 20:19:02 +0100653 if (!isttest(unique_id)) {
Christopher Fauletb8a53712019-12-16 11:29:38 +0100654 if (!(s->flags & SF_ERR_MASK))
655 s->flags |= SF_ERR_RESOURCE;
656 goto return_int_err;
657 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200658
Tim Duesterhus2825b4b2020-02-28 15:13:34 +0100659 /* send unique ID if a "unique-id-header" is defined */
Tim Duesterhus0643b0e2020-03-05 17:56:35 +0100660 if (isttest(sess->fe->header_unique_id) &&
Tim Duesterhus7ad27d42022-05-18 00:22:15 +0200661 unlikely(!http_add_header(htx, sess->fe->header_unique_id, unique_id)))
Christopher Fauletd649b572022-06-01 17:42:35 +0200662 goto return_fail_rewrite;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200663 }
664
Aurelien DARRAGONb2bb9252022-12-28 15:37:57 +0100665 /* add forwarded header (RFC 7239) (ignored for frontends) */
666 if (s->be->options & PR_O_HTTP_7239) {
667 if (unlikely(!http_handle_7239_header(s, req)))
668 goto return_fail_rewrite;
669 }
670
Christopher Faulete0768eb2018-10-03 16:38:02 +0200671 /*
Aurelien DARRAGON730b9832022-12-28 18:53:05 +0100672 * add X-Forwarded-For if either the frontend or the backend
Christopher Faulete0768eb2018-10-03 16:38:02 +0200673 * asks for it.
674 */
Aurelien DARRAGON730b9832022-12-28 18:53:05 +0100675 if ((sess->fe->options | s->be->options) & PR_O_HTTP_XFF) {
676 if (unlikely(!http_handle_xff_header(s, req)))
677 goto return_fail_rewrite;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200678 }
679
680 /*
681 * 10: add X-Original-To if either the frontend or the backend
682 * asks for it.
683 */
684 if ((sess->fe->options | s->be->options) & PR_O_ORGTO) {
Willy Tarreaud68ff012022-05-27 08:57:21 +0200685 const struct sockaddr_storage *dst = sc_dst(s->scf);
Tim Duesterhuse502c3e2022-03-05 00:52:42 +0100686 struct ist hdr = isttest(s->be->orgto_hdr_name) ? s->be->orgto_hdr_name : sess->fe->orgto_hdr_name;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200687
Christopher Faulet8a104ba2021-10-25 07:41:30 +0200688 if (dst && dst->ss_family == AF_INET) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200689 /* Add an X-Original-To header unless the destination IP is
690 * in the 'except' network range.
691 */
Christopher Faulet8a104ba2021-10-25 07:41:30 +0200692 if (ipcmp2net(dst, &sess->fe->except_xot_net) &&
693 ipcmp2net(dst, &s->be->except_xot_net)) {
694 unsigned char *pn = (unsigned char *)&((struct sockaddr_in *)dst)->sin_addr;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200695
696 /* Note: we rely on the backend to get the header name to be used for
697 * x-original-to, because the header is really meant for the backends.
698 * However, if the backend did not specify any option, we have to rely
699 * on the frontend's header name.
700 */
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200701 chunk_printf(&trash, "%d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
702 if (unlikely(!http_add_header(htx, hdr, ist2(trash.area, trash.data))))
Christopher Fauletd649b572022-06-01 17:42:35 +0200703 goto return_fail_rewrite;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200704 }
705 }
Christopher Faulet8a104ba2021-10-25 07:41:30 +0200706 else if (dst && dst->ss_family == AF_INET6) {
Christopher Faulet5d1def62021-02-26 09:19:15 +0100707 /* Add an X-Original-To header unless the source IP is
708 * in the 'except' network range.
709 */
Christopher Faulet8a104ba2021-10-25 07:41:30 +0200710 if (ipcmp2net(dst, &sess->fe->except_xot_net) &&
711 ipcmp2net(dst, &s->be->except_xot_net)) {
Christopher Faulet5d1def62021-02-26 09:19:15 +0100712 char pn[INET6_ADDRSTRLEN];
713
714 inet_ntop(AF_INET6,
Christopher Faulet8a104ba2021-10-25 07:41:30 +0200715 (const void *)&((struct sockaddr_in6 *)dst)->sin6_addr,
Christopher Faulet5d1def62021-02-26 09:19:15 +0100716 pn, sizeof(pn));
717
718 /* Note: we rely on the backend to get the header name to be used for
719 * x-forwarded-for, because the header is really meant for the backends.
720 * However, if the backend did not specify any option, we have to rely
721 * on the frontend's header name.
722 */
723 chunk_printf(&trash, "%s", pn);
724 if (unlikely(!http_add_header(htx, hdr, ist2(trash.area, trash.data))))
Christopher Fauletd649b572022-06-01 17:42:35 +0200725 goto return_fail_rewrite;
Christopher Faulet5d1def62021-02-26 09:19:15 +0100726 }
727 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200728 }
729
Christopher Fauletc2ac5e42021-03-08 18:20:09 +0100730 /* Filter the request headers if there are filters attached to the
731 * stream.
732 */
733 if (HAS_FILTERS(s))
734 req->analysers |= AN_REQ_FLT_HTTP_HDRS;
735
Christopher Faulete0768eb2018-10-03 16:38:02 +0200736 /* If we have no server assigned yet and we're balancing on url_param
737 * with a POST request, we may be interested in checking the body for
738 * that parameter. This will be done in another analyser.
739 */
740 if (!(s->flags & (SF_ASSIGNED|SF_DIRECT)) &&
Willy Tarreau089eaa02019-01-14 15:17:46 +0100741 s->txn->meth == HTTP_METH_POST &&
742 (s->be->lbprm.algo & BE_LB_ALGO) == BE_LB_ALGO_PH) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200743 channel_dont_connect(req);
744 req->analysers |= AN_REQ_HTTP_BODY;
745 }
746
747 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
748 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau1a18b542018-12-11 16:37:42 +0100749
Christopher Faulete0768eb2018-10-03 16:38:02 +0200750 /* We expect some data from the client. Unless we know for sure
751 * we already have a full request, we have to re-enable quick-ack
752 * in case we previously disabled it, otherwise we might cause
753 * the client to delay further data.
754 */
William Lallemand36119de2021-03-08 15:26:48 +0100755 if ((sess->listener && (sess->listener->options & LI_O_NOQUICKACK)) && !(htx->flags & HTX_FL_EOM))
Willy Tarreau1a18b542018-12-11 16:37:42 +0100756 conn_set_quickack(cli_conn, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200757
758 /*************************************************************
759 * OK, that's finished for the headers. We have done what we *
760 * could. Let's switch to the DATA state. *
761 ************************************************************/
762 req->analyse_exp = TICK_ETERNITY;
763 req->analysers &= ~an_bit;
764
765 s->logs.tv_request = now;
766 /* OK let's go on with the BODY now */
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100767 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200768 return 1;
769
Christopher Fauletd649b572022-06-01 17:42:35 +0200770 return_fail_rewrite:
771 if (!(s->flags & SF_ERR_MASK))
772 s->flags |= SF_ERR_PRXCOND;
773 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_rewrites);
774 if (s->flags & SF_BE_ASSIGNED)
775 _HA_ATOMIC_INC(&s->be->be_counters.failed_rewrites);
776 if (sess->listener && sess->listener->counters)
777 _HA_ATOMIC_INC(&sess->listener->counters->failed_rewrites);
778 if (objt_server(s->target))
779 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_rewrites);
780 /* fall through */
781
Christopher Fauletb8a53712019-12-16 11:29:38 +0100782 return_int_err:
783 txn->status = 500;
784 if (!(s->flags & SF_ERR_MASK))
785 s->flags |= SF_ERR_INTERNAL;
Willy Tarreau4781b152021-04-06 13:53:36 +0200786 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100787 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau4781b152021-04-06 13:53:36 +0200788 _HA_ATOMIC_INC(&s->be->be_counters.internal_errors);
William Lallemand36119de2021-03-08 15:26:48 +0100789 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200790 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100791
Christopher Fauletb8a53712019-12-16 11:29:38 +0100792 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet4a66c942023-01-13 09:43:21 +0100793 http_set_term_flags(s);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100794
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100795 DBG_TRACE_DEVEL("leaving on error",
796 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200797 return 0;
798}
799
800/* This function is an analyser which processes the HTTP tarpit. It always
801 * returns zero, at the beginning because it prevents any other processing
802 * from occurring, and at the end because it terminates the request.
803 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200804int http_process_tarpit(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200805{
806 struct http_txn *txn = s->txn;
807
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100808 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, &txn->req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200809 /* This connection is being tarpitted. The CLIENT side has
810 * already set the connect expiration date to the right
811 * timeout. We just have to check that the client is still
812 * there and that the timeout has not expired.
813 */
814 channel_dont_connect(req);
Christopher Faulet4b490b72023-01-04 11:55:15 +0100815 if (!(req->flags & CF_SHUTR) &&
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100816 !tick_is_expired(req->analyse_exp, now_ms)) {
Christopher Fauletb0c87f12021-10-29 14:37:07 +0200817 /* Be sure to drain all data from the request channel */
818 channel_htx_erase(req, htxbuf(&req->buf));
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100819 DBG_TRACE_DEVEL("waiting for tarpit timeout expiry",
820 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200821 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100822 }
823
Christopher Faulete0768eb2018-10-03 16:38:02 +0200824
825 /* We will set the queue timer to the time spent, just for
826 * logging purposes. We fake a 500 server error, so that the
827 * attacker will not suspect his connection has been tarpitted.
828 * It will not cause trouble to the logs because we can exclude
829 * the tarpitted connections by filtering on the 'PT' status flags.
830 */
831 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
832
Christopher Faulet8dfeccf2020-05-15 14:16:29 +0200833 http_reply_and_close(s, txn->status, (!(req->flags & CF_READ_ERROR) ? http_error_message(s) : NULL));
Christopher Faulet4a66c942023-01-13 09:43:21 +0100834 http_set_term_flags(s);
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100835
836 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200837 return 0;
838}
839
840/* This function is an analyser which waits for the HTTP request body. It waits
841 * for either the buffer to be full, or the full advertised contents to have
842 * reached the buffer. It must only be called after the standard HTTP request
843 * processing has occurred, because it expects the request to be parsed and will
844 * look for the Expect header. It may send a 100-Continue interim response. It
845 * takes in input any state starting from HTTP_MSG_BODY and leaves with one of
846 * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it
847 * needs to read more data, or 1 once it has completed its analysis.
848 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200849int http_wait_for_request_body(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200850{
851 struct session *sess = s->sess;
852 struct http_txn *txn = s->txn;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200853
Christopher Fauletf4569bb2023-01-13 10:49:31 +0100854 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, &s->txn->req);
Christopher Fauletf76ebe82018-10-24 11:16:22 +0200855
Christopher Fauletf76ebe82018-10-24 11:16:22 +0200856
Christopher Faulet021a8e42021-03-29 10:46:38 +0200857 switch (http_wait_for_msg_body(s, req, s->be->timeout.httpreq, 0)) {
858 case HTTP_RULE_RES_CONT:
859 goto http_end;
860 case HTTP_RULE_RES_YIELD:
861 goto missing_data_or_waiting;
862 case HTTP_RULE_RES_BADREQ:
Willy Tarreau4236f032019-03-05 10:43:32 +0100863 goto return_bad_req;
Christopher Faulet021a8e42021-03-29 10:46:38 +0200864 case HTTP_RULE_RES_ERROR:
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200865 goto return_int_err;
Christopher Faulet021a8e42021-03-29 10:46:38 +0200866 case HTTP_RULE_RES_ABRT:
Christopher Fauletb8a53712019-12-16 11:29:38 +0100867 goto return_prx_cond;
Christopher Faulet021a8e42021-03-29 10:46:38 +0200868 default:
869 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200870 }
871
872 http_end:
873 /* The situation will not evolve, so let's give up on the analysis. */
874 s->logs.tv_request = now; /* update the request timer to reflect full request */
875 req->analysers &= ~an_bit;
876 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100877 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200878 return 1;
879
Christopher Faulet021a8e42021-03-29 10:46:38 +0200880 missing_data_or_waiting:
881 channel_dont_connect(req);
882 DBG_TRACE_DEVEL("waiting for more data",
883 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
884 return 0;
885
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200886 return_int_err:
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200887 txn->status = 500;
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200888 if (!(s->flags & SF_ERR_MASK))
889 s->flags |= SF_ERR_INTERNAL;
Willy Tarreau4781b152021-04-06 13:53:36 +0200890 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100891 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau4781b152021-04-06 13:53:36 +0200892 _HA_ATOMIC_INC(&s->be->be_counters.internal_errors);
William Lallemand36119de2021-03-08 15:26:48 +0100893 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200894 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Faulet021a8e42021-03-29 10:46:38 +0200895 goto return_prx_err;
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200896
Christopher Faulete0768eb2018-10-03 16:38:02 +0200897 return_bad_req: /* let's centralize all bad requests */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200898 txn->status = 400;
Willy Tarreau4781b152021-04-06 13:53:36 +0200899 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req);
William Lallemand36119de2021-03-08 15:26:48 +0100900 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200901 _HA_ATOMIC_INC(&sess->listener->counters->failed_req);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100902 /* fall through */
903
Christopher Faulet021a8e42021-03-29 10:46:38 +0200904 return_prx_err:
Christopher Fauletb8a53712019-12-16 11:29:38 +0100905 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet021a8e42021-03-29 10:46:38 +0200906 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200907
Christopher Faulet021a8e42021-03-29 10:46:38 +0200908 return_prx_cond:
Christopher Fauletf0d80df2023-01-13 10:20:20 +0100909 http_set_term_flags(s);
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200910
Christopher Faulete0768eb2018-10-03 16:38:02 +0200911 req->analysers &= AN_REQ_FLT_END;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100912 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100913 DBG_TRACE_DEVEL("leaving on error",
914 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200915 return 0;
916}
917
918/* This function is an analyser which forwards request body (including chunk
919 * sizes if any). It is called as soon as we must forward, even if we forward
920 * zero byte. The only situation where it must not be called is when we're in
921 * tunnel mode and we want to forward till the close. It's used both to forward
922 * remaining data and to resync after end of body. It expects the msg_state to
923 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
924 * read more data, or 1 once we can go on with next request or end the stream.
925 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
926 * bytes of pending data + the headers if not already done.
927 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200928int http_request_forward_body(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200929{
930 struct session *sess = s->sess;
931 struct http_txn *txn = s->txn;
Christopher Faulet9768c262018-10-22 09:34:31 +0200932 struct http_msg *msg = &txn->req;
933 struct htx *htx;
Christopher Faulet93e02d82019-03-08 14:18:50 +0100934 short status = 0;
Christopher Fauletaed82cf2018-11-30 22:22:32 +0100935 int ret;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200936
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100937 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200938
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100939 htx = htxbuf(&req->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200940
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200941 if (htx->flags & HTX_FL_PARSING_ERROR)
942 goto return_bad_req;
943 if (htx->flags & HTX_FL_PROCESSING_ERROR)
944 goto return_int_err;
945
Christopher Faulete0768eb2018-10-03 16:38:02 +0200946 /* Note that we don't have to send 100-continue back because we don't
947 * need the data to complete our job, and it's up to the server to
948 * decide whether to return 100, 417 or anything else in return of
949 * an "Expect: 100-continue" header.
950 */
Christopher Faulet9768c262018-10-22 09:34:31 +0200951 if (msg->msg_state == HTTP_MSG_BODY)
952 msg->msg_state = HTTP_MSG_DATA;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200953
Christopher Faulete0768eb2018-10-03 16:38:02 +0200954 /* in most states, we should abort in case of early close */
955 channel_auto_close(req);
956
957 if (req->to_forward) {
Christopher Faulet66af0b22019-03-22 14:54:52 +0100958 if (req->to_forward == CHN_INFINITE_FORWARD) {
Christopher Faulet1a3e0272019-11-15 16:31:46 +0100959 if (req->flags & CF_EOI)
960 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet66af0b22019-03-22 14:54:52 +0100961 }
962 else {
963 /* We can't process the buffer's contents yet */
964 req->flags |= CF_WAKE_WRITE;
965 goto missing_data_or_waiting;
966 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200967 }
968
Christopher Faulet1a3e0272019-11-15 16:31:46 +0100969 if (msg->msg_state >= HTTP_MSG_ENDING)
970 goto ending;
971
972 if (txn->meth == HTTP_METH_CONNECT) {
973 msg->msg_state = HTTP_MSG_ENDING;
974 goto ending;
975 }
976
Christopher Fauletaed82cf2018-11-30 22:22:32 +0100977 /* Forward input data. We get it by removing all outgoing data not
978 * forwarded yet from HTX data size. If there are some data filters, we
979 * let them decide the amount of data to forward.
Christopher Faulet9768c262018-10-22 09:34:31 +0200980 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +0100981 if (HAS_REQ_DATA_FILTERS(s)) {
982 ret = flt_http_payload(s, msg, htx->data);
983 if (ret < 0)
984 goto return_bad_req;
Christopher Faulet421e7692019-06-13 11:16:45 +0200985 c_adv(req, ret);
Christopher Fauletaed82cf2018-11-30 22:22:32 +0100986 }
987 else {
Christopher Faulet421e7692019-06-13 11:16:45 +0200988 c_adv(req, htx->data - co_data(req));
Christopher Faulet66af0b22019-03-22 14:54:52 +0100989 if (msg->flags & HTTP_MSGF_XFER_LEN)
990 channel_htx_forward_forever(req, htx);
Christopher Fauletaed82cf2018-11-30 22:22:32 +0100991 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200992
Christopher Faulet1a3e0272019-11-15 16:31:46 +0100993 if (htx->data != co_data(req))
994 goto missing_data_or_waiting;
Christopher Fauletd20fdb02019-06-13 16:43:22 +0200995
Christopher Faulet9768c262018-10-22 09:34:31 +0200996 /* Check if the end-of-message is reached and if so, switch the message
Christopher Fauletd20fdb02019-06-13 16:43:22 +0200997 * in HTTP_MSG_ENDING state. Then if all data was marked to be
998 * forwarded, set the state to HTTP_MSG_DONE.
Christopher Faulet9768c262018-10-22 09:34:31 +0200999 */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001000 if (!(htx->flags & HTX_FL_EOM))
Christopher Faulet9768c262018-10-22 09:34:31 +02001001 goto missing_data_or_waiting;
1002
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001003 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet9768c262018-10-22 09:34:31 +02001004
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001005 ending:
Christopher Faulet2151cdd2020-07-22 16:34:59 +02001006 req->flags &= ~CF_EXPECT_MORE; /* no more data are expected */
1007
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001008 /* other states, ENDING...TUNNEL */
1009 if (msg->msg_state >= HTTP_MSG_DONE)
1010 goto done;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001011
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001012 if (HAS_REQ_DATA_FILTERS(s)) {
1013 ret = flt_http_end(s, msg);
1014 if (ret <= 0) {
1015 if (!ret)
1016 goto missing_data_or_waiting;
1017 goto return_bad_req;
1018 }
1019 }
1020
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001021 if (txn->meth == HTTP_METH_CONNECT)
1022 msg->msg_state = HTTP_MSG_TUNNEL;
1023 else {
1024 msg->msg_state = HTTP_MSG_DONE;
1025 req->to_forward = 0;
1026 }
1027
1028 done:
1029 /* we don't want to forward closes on DONE except in tunnel mode. */
1030 if (!(txn->flags & TX_CON_WANT_TUN))
1031 channel_dont_close(req);
1032
Christopher Fauletf2b02cf2023-01-13 11:02:28 +01001033 if ((req->flags & CF_SHUTW) && co_data(req)) {
1034 /* request errors are most likely due to the server aborting the
1035 * transfer. */
1036 goto return_srv_abort;
1037 }
1038
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001039 http_end_request(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001040 if (!(req->analysers & an_bit)) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001041 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001042 return 1;
1043 }
1044
1045 /* If "option abortonclose" is set on the backend, we want to monitor
1046 * the client's connection and forward any shutdown notification to the
1047 * server, which will decide whether to close or to go on processing the
1048 * request. We only do that in tunnel mode, and not in other modes since
1049 * it can be abused to exhaust source ports. */
Christopher Faulet769d0e92019-03-22 14:23:18 +01001050 if (s->be->options & PR_O_ABRT_CLOSE) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001051 channel_auto_read(req);
Christopher Faulet446d8032022-12-12 07:53:18 +01001052 if ((req->flags & CF_SHUTR) && !(txn->flags & TX_CON_WANT_TUN))
Willy Tarreaucb041662022-05-17 19:44:42 +02001053 s->scb->flags |= SC_FL_NOLINGER;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001054 channel_auto_close(req);
1055 }
1056 else if (s->txn->meth == HTTP_METH_POST) {
1057 /* POST requests may require to read extra CRLF sent by broken
1058 * browsers and which could cause an RST to be sent upon close
1059 * on some systems (eg: Linux). */
1060 channel_auto_read(req);
1061 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001062 DBG_TRACE_DEVEL("waiting for the end of the HTTP txn",
1063 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001064 return 0;
1065
1066 missing_data_or_waiting:
1067 /* stop waiting for data if the input is closed before the end */
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001068 if (msg->msg_state < HTTP_MSG_ENDING && req->flags & CF_SHUTR)
Christopher Faulet93e02d82019-03-08 14:18:50 +01001069 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001070
1071 waiting:
1072 /* waiting for the last bits to leave the buffer */
1073 if (req->flags & CF_SHUTW)
Christopher Faulet93e02d82019-03-08 14:18:50 +01001074 goto return_srv_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001075
1076 /* When TE: chunked is used, we need to get there again to parse remaining
1077 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
1078 * And when content-length is used, we never want to let the possible
1079 * shutdown be forwarded to the other side, as the state machine will
1080 * take care of it once the client responds. It's also important to
1081 * prevent TIME_WAITs from accumulating on the backend side, and for
1082 * HTTP/2 where the last frame comes with a shutdown.
1083 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001084 if (msg->flags & HTTP_MSGF_XFER_LEN)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001085 channel_dont_close(req);
1086
1087 /* We know that more data are expected, but we couldn't send more that
1088 * what we did. So we always set the CF_EXPECT_MORE flag so that the
1089 * system knows it must not set a PUSH on this first part. Interactive
1090 * modes are already handled by the stream sock layer. We must not do
1091 * this in content-length mode because it could present the MSG_MORE
1092 * flag with the last block of forwarded data, which would cause an
1093 * additional delay to be observed by the receiver.
1094 */
Christopher Faulet2151cdd2020-07-22 16:34:59 +02001095 if (HAS_REQ_DATA_FILTERS(s))
Christopher Faulete0768eb2018-10-03 16:38:02 +02001096 req->flags |= CF_EXPECT_MORE;
1097
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001098 DBG_TRACE_DEVEL("waiting for more data to forward",
1099 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001100 return 0;
1101
Christopher Faulet93e02d82019-03-08 14:18:50 +01001102 return_cli_abort:
Willy Tarreau4781b152021-04-06 13:53:36 +02001103 _HA_ATOMIC_INC(&sess->fe->fe_counters.cli_aborts);
1104 _HA_ATOMIC_INC(&s->be->be_counters.cli_aborts);
William Lallemand36119de2021-03-08 15:26:48 +01001105 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001106 _HA_ATOMIC_INC(&sess->listener->counters->cli_aborts);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001107 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001108 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.cli_aborts);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001109 if (!(s->flags & SF_ERR_MASK))
Christopher Fauletf2b02cf2023-01-13 11:02:28 +01001110 s->flags |= ((req->flags & CF_READ_TIMEOUT) ? SF_ERR_CLITO : SF_ERR_CLICL);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001111 status = 400;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001112 goto return_prx_cond;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001113
1114 return_srv_abort:
Willy Tarreau4781b152021-04-06 13:53:36 +02001115 _HA_ATOMIC_INC(&sess->fe->fe_counters.srv_aborts);
1116 _HA_ATOMIC_INC(&s->be->be_counters.srv_aborts);
William Lallemand36119de2021-03-08 15:26:48 +01001117 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001118 _HA_ATOMIC_INC(&sess->listener->counters->srv_aborts);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001119 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001120 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.srv_aborts);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001121 if (!(s->flags & SF_ERR_MASK))
Christopher Fauletf2b02cf2023-01-13 11:02:28 +01001122 s->flags |= ((req->flags & CF_WRITE_TIMEOUT) ? SF_ERR_SRVTO : SF_ERR_SRVCL);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001123 status = 502;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001124 goto return_prx_cond;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001125
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001126 return_int_err:
1127 if (!(s->flags & SF_ERR_MASK))
1128 s->flags |= SF_ERR_INTERNAL;
Willy Tarreau4781b152021-04-06 13:53:36 +02001129 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
1130 _HA_ATOMIC_INC(&s->be->be_counters.internal_errors);
William Lallemand36119de2021-03-08 15:26:48 +01001131 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001132 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001133 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001134 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.internal_errors);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001135 status = 500;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001136 goto return_prx_cond;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001137
Christopher Faulet93e02d82019-03-08 14:18:50 +01001138 return_bad_req:
Willy Tarreau4781b152021-04-06 13:53:36 +02001139 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req);
William Lallemand36119de2021-03-08 15:26:48 +01001140 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001141 _HA_ATOMIC_INC(&sess->listener->counters->failed_req);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001142 status = 400;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001143 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001144
Christopher Fauletb8a53712019-12-16 11:29:38 +01001145 return_prx_cond:
Christopher Faulet9768c262018-10-22 09:34:31 +02001146 if (txn->status > 0) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001147 /* Note: we don't send any error if some data were already sent */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001148 http_reply_and_close(s, txn->status, NULL);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001149 } else {
Christopher Faulet93e02d82019-03-08 14:18:50 +01001150 txn->status = status;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001151 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001152 }
Christopher Fauletf2b02cf2023-01-13 11:02:28 +01001153 http_set_term_flags(s);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001154 DBG_TRACE_DEVEL("leaving on error ",
1155 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001156 return 0;
1157}
1158
Willy Tarreau4596fe22022-05-17 19:07:51 +02001159/* Reset the stream and the backend stream connector to a situation suitable for attemption connection */
Olivier Houcharda254a372019-04-05 15:30:12 +02001160/* Returns 0 if we can attempt to retry, -1 otherwise */
Willy Tarreaub49672d2022-05-27 10:13:37 +02001161static __inline int do_l7_retry(struct stream *s, struct stconn *sc)
Olivier Houcharda254a372019-04-05 15:30:12 +02001162{
Christopher Faulet9f5382e2021-05-21 13:46:14 +02001163 struct channel *req, *res;
1164 int co_data;
Olivier Houcharda254a372019-04-05 15:30:12 +02001165
Christopher Faulet731c8e62022-03-29 16:08:44 +02001166 s->conn_retries++;
1167 if (s->conn_retries >= s->be->conn_retries)
Christopher Faulet552601d2021-05-26 10:31:06 +02001168 return -1;
Christopher Faulet5b82cc52020-10-12 15:18:50 +02001169
Christopher Faulete763c8c2021-05-05 18:23:59 +02001170 if (objt_server(s->target)) {
1171 if (s->flags & SF_CURR_SESS) {
1172 s->flags &= ~SF_CURR_SESS;
1173 _HA_ATOMIC_DEC(&__objt_server(s->target)->cur_sess);
1174 }
Willy Tarreau4781b152021-04-06 13:53:36 +02001175 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.retries);
Christopher Faulete763c8c2021-05-05 18:23:59 +02001176 }
Willy Tarreau4781b152021-04-06 13:53:36 +02001177 _HA_ATOMIC_INC(&s->be->be_counters.retries);
Willy Tarreau223995e2019-05-04 10:38:31 +02001178
Christopher Faulet9f5382e2021-05-21 13:46:14 +02001179 req = &s->req;
1180 res = &s->res;
Olivier Houcharda254a372019-04-05 15:30:12 +02001181 /* Remove any write error from the request, and read error from the response */
1182 req->flags &= ~(CF_WRITE_ERROR | CF_WRITE_TIMEOUT | CF_SHUTW | CF_SHUTW_NOW);
Christopher Faulet6e1bbc42022-12-12 08:08:15 +01001183 res->flags &= ~(CF_READ_ERROR | CF_READ_TIMEOUT | CF_SHUTR | CF_EOI | CF_READ_EVENT | CF_SHUTR_NOW);
Christopher Faulet7bf46bb2022-01-04 10:56:03 +01001184 res->analysers &= AN_RES_FLT_END;
Christopher Faulet50264b42022-03-30 19:39:30 +02001185 s->conn_err_type = STRM_ET_NONE;
Christopher Fauletae024ce2022-03-29 19:02:31 +02001186 s->flags &= ~(SF_CONN_EXP | SF_ERR_MASK | SF_FINST_MASK);
1187 s->conn_exp = TICK_ETERNITY;
Olivier Houchard4bd58672019-07-12 16:16:59 +02001188 stream_choose_redispatch(s);
Olivier Houcharda254a372019-04-05 15:30:12 +02001189 res->rex = TICK_ETERNITY;
1190 res->to_forward = 0;
1191 res->analyse_exp = TICK_ETERNITY;
1192 res->total = 0;
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +01001193
Willy Tarreau19c65a92022-05-27 08:49:24 +02001194 if (sc_reset_endp(s->scb) < 0) {
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +01001195 if (!(s->flags & SF_ERR_MASK))
1196 s->flags |= SF_ERR_INTERNAL;
1197 return -1;
1198 }
Olivier Houcharda254a372019-04-05 15:30:12 +02001199
Christopher Faulet9f5382e2021-05-21 13:46:14 +02001200 b_free(&req->buf);
1201 /* Swap the L7 buffer with the channel buffer */
1202 /* We know we stored the co_data as b_data, so get it there */
Christopher Faulete05bf9e2022-03-29 15:23:40 +02001203 co_data = b_data(&s->txn->l7_buffer);
1204 b_set_data(&s->txn->l7_buffer, b_size(&s->txn->l7_buffer));
1205 b_xfer(&req->buf, &s->txn->l7_buffer, b_data(&s->txn->l7_buffer));
Christopher Faulet9f5382e2021-05-21 13:46:14 +02001206 co_set_data(req, co_data);
Christopher Faulet5b82cc52020-10-12 15:18:50 +02001207
Ilya Shipitsinacf84592021-02-06 22:29:08 +05001208 DBG_TRACE_DEVEL("perform a L7 retry", STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, s->txn);
Christopher Faulet9f5382e2021-05-21 13:46:14 +02001209
Olivier Houcharda254a372019-04-05 15:30:12 +02001210 b_reset(&res->buf);
1211 co_set_data(res, 0);
1212 return 0;
1213}
1214
Christopher Faulete0768eb2018-10-03 16:38:02 +02001215/* This stream analyser waits for a complete HTTP response. It returns 1 if the
1216 * processing can continue on next analysers, or zero if it either needs more
1217 * data or wants to immediately abort the response (eg: timeout, error, ...). It
1218 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->res.analysers
1219 * when it has nothing left to do, and may remove any analyser when it wants to
1220 * abort.
1221 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001222int http_wait_for_response(struct stream *s, struct channel *rep, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001223{
Christopher Faulet9768c262018-10-22 09:34:31 +02001224 /*
1225 * We will analyze a complete HTTP response to check the its syntax.
1226 *
1227 * Once the start line and all headers are received, we may perform a
1228 * capture of the error (if any), and we will set a few fields. We also
1229 * logging and finally headers capture.
1230 */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001231 struct session *sess = s->sess;
1232 struct http_txn *txn = s->txn;
1233 struct http_msg *msg = &txn->rsp;
Christopher Faulet9768c262018-10-22 09:34:31 +02001234 struct htx *htx;
Christopher Faulet61608322018-11-23 16:23:45 +01001235 struct connection *srv_conn;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001236 struct htx_sl *sl;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001237 int n;
1238
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001239 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001240
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001241 htx = htxbuf(&rep->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001242
Willy Tarreau4236f032019-03-05 10:43:32 +01001243 /* Parsing errors are caught here */
1244 if (htx->flags & HTX_FL_PARSING_ERROR)
1245 goto return_bad_res;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001246 if (htx->flags & HTX_FL_PROCESSING_ERROR)
1247 goto return_int_err;
Willy Tarreau4236f032019-03-05 10:43:32 +01001248
Christopher Faulete0768eb2018-10-03 16:38:02 +02001249 /*
1250 * Now we quickly check if we have found a full valid response.
1251 * If not so, we check the FD and buffer states before leaving.
1252 * A full response is indicated by the fact that we have seen
1253 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
1254 * responses are checked first.
1255 *
1256 * Depending on whether the client is still there or not, we
1257 * may send an error response back or not. Note that normally
1258 * we should only check for HTTP status there, and check I/O
1259 * errors somewhere else.
1260 */
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001261 next_one:
Christopher Faulet29f17582019-05-23 11:03:26 +02001262 if (unlikely(htx_is_empty(htx) || htx->first == -1)) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001263 /* 1: have we encountered a read error ? */
1264 if (rep->flags & CF_READ_ERROR) {
Willy Tarreaufd9417b2022-05-18 16:23:22 +02001265 struct connection *conn = sc_conn(s->scb);
Olivier Houchard865d8392019-05-03 22:46:27 +02001266
Christopher Fauletd9769232021-05-26 12:15:37 +02001267 /* Perform a L7 retry because server refuses the early data. */
Christopher Faulete05bf9e2022-03-29 15:23:40 +02001268 if ((txn->flags & TX_L7_RETRY) &&
Christopher Fauletd9769232021-05-26 12:15:37 +02001269 (s->be->retry_type & PR_RE_EARLY_ERROR) &&
1270 conn && conn->err_code == CO_ER_SSL_EARLY_FAILED &&
Willy Tarreau7cb9e6c2022-05-17 19:40:40 +02001271 do_l7_retry(s, s->scb) == 0) {
Christopher Fauletd9769232021-05-26 12:15:37 +02001272 DBG_TRACE_DEVEL("leaving on L7 retry",
1273 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
1274 return 0;
1275 }
1276
Olivier Houchard6db16992019-05-17 15:40:49 +02001277 if (txn->flags & TX_NOT_FIRST)
1278 goto abort_keep_alive;
1279
Willy Tarreau4781b152021-04-06 13:53:36 +02001280 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001281 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001282 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001283 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001284 }
1285
Christopher Fauletd9769232021-05-26 12:15:37 +02001286 /* if the server refused the early data, just send a 425 */
1287 if (conn && conn->err_code == CO_ER_SSL_EARLY_FAILED)
Olivier Houchard865d8392019-05-03 22:46:27 +02001288 txn->status = 425;
Christopher Fauletd9769232021-05-26 12:15:37 +02001289 else {
1290 txn->status = 502;
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001291 stream_inc_http_fail_ctr(s);
Christopher Fauletd9769232021-05-26 12:15:37 +02001292 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001293
Willy Tarreaucb041662022-05-17 19:44:42 +02001294 s->scb->flags |= SC_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001295 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001296
1297 if (!(s->flags & SF_ERR_MASK))
1298 s->flags |= SF_ERR_SRVCL;
Christopher Faulet4a66c942023-01-13 09:43:21 +01001299 http_set_term_flags(s);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001300 DBG_TRACE_DEVEL("leaving on error",
1301 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001302 return 0;
1303 }
1304
Christopher Faulet9768c262018-10-22 09:34:31 +02001305 /* 2: read timeout : return a 504 to the client. */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001306 else if (rep->flags & CF_READ_TIMEOUT) {
Christopher Faulete05bf9e2022-03-29 15:23:40 +02001307 if ((txn->flags & TX_L7_RETRY) &&
Olivier Houcharda254a372019-04-05 15:30:12 +02001308 (s->be->retry_type & PR_RE_TIMEOUT)) {
Willy Tarreau7cb9e6c2022-05-17 19:40:40 +02001309 if (co_data(rep) || do_l7_retry(s, s->scb) == 0) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001310 DBG_TRACE_DEVEL("leaving on L7 retry",
1311 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharda254a372019-04-05 15:30:12 +02001312 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001313 }
Olivier Houcharda254a372019-04-05 15:30:12 +02001314 }
Willy Tarreau4781b152021-04-06 13:53:36 +02001315 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001316 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001317 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001318 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001319 }
1320
Christopher Faulete0768eb2018-10-03 16:38:02 +02001321 txn->status = 504;
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001322 stream_inc_http_fail_ctr(s);
Willy Tarreaucb041662022-05-17 19:44:42 +02001323 s->scb->flags |= SC_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001324 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001325
1326 if (!(s->flags & SF_ERR_MASK))
1327 s->flags |= SF_ERR_SRVTO;
Christopher Faulet4a66c942023-01-13 09:43:21 +01001328 http_set_term_flags(s);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001329 DBG_TRACE_DEVEL("leaving on error",
1330 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001331 return 0;
1332 }
1333
Christopher Faulet9768c262018-10-22 09:34:31 +02001334 /* 3: client abort with an abortonclose */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001335 else if ((rep->flags & CF_SHUTR) && ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001336 _HA_ATOMIC_INC(&sess->fe->fe_counters.cli_aborts);
1337 _HA_ATOMIC_INC(&s->be->be_counters.cli_aborts);
William Lallemand36119de2021-03-08 15:26:48 +01001338 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001339 _HA_ATOMIC_INC(&sess->listener->counters->cli_aborts);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001340 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001341 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.cli_aborts);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001342
Christopher Faulete0768eb2018-10-03 16:38:02 +02001343 txn->status = 400;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001344 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001345
1346 if (!(s->flags & SF_ERR_MASK))
1347 s->flags |= SF_ERR_CLICL;
Christopher Faulet4a66c942023-01-13 09:43:21 +01001348 http_set_term_flags(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001349
1350 /* process_stream() will take care of the error */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001351 DBG_TRACE_DEVEL("leaving on error",
1352 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001353 return 0;
1354 }
1355
Christopher Faulet9768c262018-10-22 09:34:31 +02001356 /* 4: close from server, capture the response if the server has started to respond */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001357 else if (rep->flags & CF_SHUTR) {
Christopher Faulete05bf9e2022-03-29 15:23:40 +02001358 if ((txn->flags & TX_L7_RETRY) &&
Olivier Houcharda254a372019-04-05 15:30:12 +02001359 (s->be->retry_type & PR_RE_DISCONNECTED)) {
Willy Tarreau7cb9e6c2022-05-17 19:40:40 +02001360 if (co_data(rep) || do_l7_retry(s, s->scb) == 0) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001361 DBG_TRACE_DEVEL("leaving on L7 retry",
1362 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharda254a372019-04-05 15:30:12 +02001363 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001364 }
Olivier Houcharda254a372019-04-05 15:30:12 +02001365 }
1366
Olivier Houchard6db16992019-05-17 15:40:49 +02001367 if (txn->flags & TX_NOT_FIRST)
1368 goto abort_keep_alive;
1369
Willy Tarreau4781b152021-04-06 13:53:36 +02001370 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001371 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001372 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001373 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001374 }
1375
Christopher Faulete0768eb2018-10-03 16:38:02 +02001376 txn->status = 502;
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001377 stream_inc_http_fail_ctr(s);
Willy Tarreaucb041662022-05-17 19:44:42 +02001378 s->scb->flags |= SC_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001379 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001380
1381 if (!(s->flags & SF_ERR_MASK))
1382 s->flags |= SF_ERR_SRVCL;
Christopher Faulet4a66c942023-01-13 09:43:21 +01001383 http_set_term_flags(s);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001384 DBG_TRACE_DEVEL("leaving on error",
1385 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001386 return 0;
1387 }
1388
Christopher Faulet9768c262018-10-22 09:34:31 +02001389 /* 5: write error to client (we don't send any message then) */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001390 else if (rep->flags & CF_WRITE_ERROR) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001391 if (txn->flags & TX_NOT_FIRST)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001392 goto abort_keep_alive;
1393
Willy Tarreau4781b152021-04-06 13:53:36 +02001394 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001395 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001396 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001397 rep->analysers &= AN_RES_FLT_END;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001398
1399 if (!(s->flags & SF_ERR_MASK))
1400 s->flags |= SF_ERR_CLICL;
Christopher Faulet4a66c942023-01-13 09:43:21 +01001401 http_set_term_flags(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001402
1403 /* process_stream() will take care of the error */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001404 DBG_TRACE_DEVEL("leaving on error",
1405 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001406 return 0;
1407 }
1408
1409 channel_dont_close(rep);
1410 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001411 DBG_TRACE_DEVEL("waiting for more data",
1412 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001413 return 0;
1414 }
1415
1416 /* More interesting part now : we know that we have a complete
1417 * response which at least looks like HTTP. We have an indicator
1418 * of each header's length, so we can parse them quickly.
1419 */
Christopher Faulet29f17582019-05-23 11:03:26 +02001420 BUG_ON(htx_get_first_type(htx) != HTX_BLK_RES_SL);
Christopher Faulet297fbb42019-05-13 14:41:27 +02001421 sl = http_get_stline(htx);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001422
Christopher Faulet1f08bff2021-05-26 13:14:39 +02001423 /* Perform a L7 retry because of the status code */
Christopher Faulete05bf9e2022-03-29 15:23:40 +02001424 if ((txn->flags & TX_L7_RETRY) &&
Christopher Faulet1f08bff2021-05-26 13:14:39 +02001425 l7_status_match(s->be, sl->info.res.status) &&
Willy Tarreau7cb9e6c2022-05-17 19:40:40 +02001426 do_l7_retry(s, s->scb) == 0) {
Christopher Faulet1f08bff2021-05-26 13:14:39 +02001427 DBG_TRACE_DEVEL("leaving on L7 retry", STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
1428 return 0;
1429 }
1430
1431 /* Now, L7 buffer is useless, it can be released */
Christopher Faulete05bf9e2022-03-29 15:23:40 +02001432 b_free(&txn->l7_buffer);
Christopher Faulet1f08bff2021-05-26 13:14:39 +02001433
1434 msg->msg_state = HTTP_MSG_BODY;
1435
1436
Christopher Faulet9768c262018-10-22 09:34:31 +02001437 /* 0: we might have to print this header in debug mode */
1438 if (unlikely((global.mode & MODE_DEBUG) &&
1439 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
1440 int32_t pos;
1441
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001442 http_debug_stline("srvrep", s, sl);
Christopher Faulet9768c262018-10-22 09:34:31 +02001443
Christopher Fauleta3f15502019-05-13 15:27:23 +02001444 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001445 struct htx_blk *blk = htx_get_blk(htx, pos);
1446 enum htx_blk_type type = htx_get_blk_type(blk);
1447
1448 if (type == HTX_BLK_EOH)
1449 break;
1450 if (type != HTX_BLK_HDR)
1451 continue;
1452
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001453 http_debug_hdr("srvhdr", s,
1454 htx_get_blk_name(htx, blk),
1455 htx_get_blk_value(htx, blk));
Christopher Faulet9768c262018-10-22 09:34:31 +02001456 }
1457 }
1458
Christopher Faulet03599112018-11-27 11:21:21 +01001459 /* 1: get the status code and the version. Also set HTTP flags */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001460 txn->status = sl->info.res.status;
Christopher Faulet03599112018-11-27 11:21:21 +01001461 if (sl->flags & HTX_SL_F_VER_11)
Christopher Faulet9768c262018-10-22 09:34:31 +02001462 msg->flags |= HTTP_MSGF_VER_11;
Christopher Faulet03599112018-11-27 11:21:21 +01001463 if (sl->flags & HTX_SL_F_XFER_LEN) {
1464 msg->flags |= HTTP_MSGF_XFER_LEN;
Christopher Faulet2a408542020-11-20 14:22:37 +01001465 if (sl->flags & HTX_SL_F_CLEN)
1466 msg->flags |= HTTP_MSGF_CNT_LEN;
1467 else if (sl->flags & HTX_SL_F_CHNK)
1468 msg->flags |= HTTP_MSGF_TE_CHNK;
Christopher Faulet03599112018-11-27 11:21:21 +01001469 }
Christopher Faulet2a408542020-11-20 14:22:37 +01001470 if (sl->flags & HTX_SL_F_BODYLESS)
1471 msg->flags |= HTTP_MSGF_BODYLESS;
Christopher Faulet576c3582021-01-08 15:53:01 +01001472 if (sl->flags & HTX_SL_F_CONN_UPG)
1473 msg->flags |= HTTP_MSGF_CONN_UPG;
Christopher Faulet9768c262018-10-22 09:34:31 +02001474
1475 n = txn->status / 100;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001476 if (n < 1 || n > 5)
1477 n = 0;
Christopher Faulet9768c262018-10-22 09:34:31 +02001478
Christopher Faulete0768eb2018-10-03 16:38:02 +02001479 /* when the client triggers a 4xx from the server, it's most often due
1480 * to a missing object or permission. These events should be tracked
1481 * because if they happen often, it may indicate a brute force or a
1482 * vulnerability scan.
1483 */
1484 if (n == 4)
1485 stream_inc_http_err_ctr(s);
1486
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001487 if (n == 5 && txn->status != 501 && txn->status != 505)
1488 stream_inc_http_fail_ctr(s);
1489
Marcin Deranek3c27dda2020-05-15 18:32:51 +02001490 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001491 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.p.http.rsp[n]);
1492 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.p.http.cum_req);
Marcin Deranek3c27dda2020-05-15 18:32:51 +02001493 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001494
Christopher Faulete0768eb2018-10-03 16:38:02 +02001495 /* Adjust server's health based on status code. Note: status codes 501
1496 * and 505 are triggered on demand by client request, so we must not
1497 * count them as server failures.
1498 */
1499 if (objt_server(s->target)) {
1500 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001501 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_OK);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001502 else
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001503 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_STS);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001504 }
1505
1506 /*
1507 * We may be facing a 100-continue response, or any other informational
1508 * 1xx response which is non-final, in which case this is not the right
1509 * response, and we're waiting for the next one. Let's allow this response
1510 * to go to the client and wait for the next one. There's an exception for
1511 * 101 which is used later in the code to switch protocols.
1512 */
1513 if (txn->status < 200 &&
1514 (txn->status == 100 || txn->status >= 102)) {
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001515 FLT_STRM_CB(s, flt_http_reset(s, msg));
Christopher Faulet421e7692019-06-13 11:16:45 +02001516 htx->first = channel_htx_fwd_headers(rep, htx);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001517 msg->msg_state = HTTP_MSG_RPBEFORE;
Christopher Faulet3499f622019-09-03 15:23:54 +02001518 msg->flags = 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001519 txn->status = 0;
1520 s->logs.t_data = -1; /* was not a response yet */
Christopher Faulet7d518452020-08-31 11:07:07 +02001521 rep->flags |= CF_SEND_DONTWAIT; /* Send ASAP informational messages */
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001522 goto next_one;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001523 }
1524
Christopher Faulet6e6c7b12021-01-08 16:02:05 +01001525 /* A 101-switching-protocols must contains a Connection header with the
1526 * "upgrade" option and the request too. It means both are agree to
1527 * upgrade. It is not so strict because there is no test on the Upgrade
1528 * header content. But it is probably stronger enough for now.
1529 */
1530 if (txn->status == 101 &&
1531 (!(txn->req.flags & HTTP_MSGF_CONN_UPG) || !(txn->rsp.flags & HTTP_MSGF_CONN_UPG)))
1532 goto return_bad_res;
1533
Christopher Faulete0768eb2018-10-03 16:38:02 +02001534 /*
1535 * 2: check for cacheability.
1536 */
1537
1538 switch (txn->status) {
1539 case 200:
1540 case 203:
1541 case 204:
1542 case 206:
1543 case 300:
1544 case 301:
1545 case 404:
1546 case 405:
1547 case 410:
1548 case 414:
1549 case 501:
1550 break;
1551 default:
1552 /* RFC7231#6.1:
1553 * Responses with status codes that are defined as
1554 * cacheable by default (e.g., 200, 203, 204, 206,
1555 * 300, 301, 404, 405, 410, 414, and 501 in this
1556 * specification) can be reused by a cache with
1557 * heuristic expiration unless otherwise indicated
1558 * by the method definition or explicit cache
1559 * controls [RFC7234]; all other status codes are
1560 * not cacheable by default.
1561 */
1562 txn->flags &= ~(TX_CACHEABLE | TX_CACHE_COOK);
1563 break;
1564 }
1565
1566 /*
1567 * 3: we may need to capture headers
1568 */
1569 s->logs.logwait &= ~LW_RESP;
1570 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->res_cap))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001571 http_capture_headers(htx, s->res_cap, sess->fe->rsp_cap);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001572
Christopher Faulet9768c262018-10-22 09:34:31 +02001573 /* Skip parsing if no content length is possible. */
Christopher Fauletc75668e2020-12-07 18:10:32 +01001574 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status >= 200 && txn->status < 300) ||
Christopher Faulete0768eb2018-10-03 16:38:02 +02001575 txn->status == 101)) {
1576 /* Either we've established an explicit tunnel, or we're
1577 * switching the protocol. In both cases, we're very unlikely
1578 * to understand the next protocols. We have to switch to tunnel
1579 * mode, so that we transfer the request and responses then let
1580 * this protocol pass unmodified. When we later implement specific
1581 * parsers for such protocols, we'll want to check the Upgrade
1582 * header which contains information about that protocol for
1583 * responses with status 101 (eg: see RFC2817 about TLS).
1584 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02001585 txn->flags |= TX_CON_WANT_TUN;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001586 }
1587
Christopher Faulet61608322018-11-23 16:23:45 +01001588 /* check for NTML authentication headers in 401 (WWW-Authenticate) and
1589 * 407 (Proxy-Authenticate) responses and set the connection to private
1590 */
Willy Tarreaufd9417b2022-05-18 16:23:22 +02001591 srv_conn = sc_conn(s->scb);
Christopher Faulet61608322018-11-23 16:23:45 +01001592 if (srv_conn) {
1593 struct ist hdr;
1594 struct http_hdr_ctx ctx;
1595
1596 if (txn->status == 401)
1597 hdr = ist("WWW-Authenticate");
1598 else if (txn->status == 407)
1599 hdr = ist("Proxy-Authenticate");
1600 else
1601 goto end;
1602
1603 ctx.blk = NULL;
1604 while (http_find_header(htx, hdr, &ctx, 0)) {
Willy Tarreauf1dcced2020-05-07 19:27:02 +02001605 /* If www-authenticate contains "Negotiate", "Nego2", or "NTLM",
1606 * possibly followed by blanks and a base64 string, the connection
1607 * is private. Since it's a mess to deal with, we only check for
1608 * values starting with "NTLM" or "Nego". Note that often multiple
1609 * headers are sent by the server there.
1610 */
1611 if ((ctx.value.len >= 4 && strncasecmp(ctx.value.ptr, "Nego", 4) == 0) ||
Willy Tarreau49a1d282020-05-07 19:10:15 +02001612 (ctx.value.len >= 4 && strncasecmp(ctx.value.ptr, "NTLM", 4) == 0)) {
Olivier Houchard250031e2019-05-29 15:01:50 +02001613 sess->flags |= SESS_FL_PREFER_LAST;
Christopher Faulet08016ab2020-07-01 16:10:06 +02001614 conn_set_owner(srv_conn, sess, NULL);
Christopher Faulet21ddc742020-07-01 15:26:14 +02001615 conn_set_private(srv_conn);
Ilya Shipitsin6b79f382020-07-23 00:32:55 +05001616 /* If it fail now, the same will be done in mux->detach() callback */
Christopher Faulet08016ab2020-07-01 16:10:06 +02001617 session_add_conn(srv_conn->owner, srv_conn, srv_conn->target);
Willy Tarreauf1dcced2020-05-07 19:27:02 +02001618 break;
Olivier Houchard250031e2019-05-29 15:01:50 +02001619 }
Christopher Faulet61608322018-11-23 16:23:45 +01001620 }
1621 }
1622
1623 end:
Christopher Faulete0768eb2018-10-03 16:38:02 +02001624 /* we want to have the response time before we start processing it */
1625 s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now);
1626
1627 /* end of job, return OK */
1628 rep->analysers &= ~an_bit;
1629 rep->analyse_exp = TICK_ETERNITY;
1630 channel_auto_close(rep);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001631 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001632 return 1;
1633
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001634 return_int_err:
Willy Tarreau4781b152021-04-06 13:53:36 +02001635 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
1636 _HA_ATOMIC_INC(&s->be->be_counters.internal_errors);
William Lallemand36119de2021-03-08 15:26:48 +01001637 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001638 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Fauletb8a53712019-12-16 11:29:38 +01001639 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001640 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.internal_errors);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001641 txn->status = 500;
1642 if (!(s->flags & SF_ERR_MASK))
1643 s->flags |= SF_ERR_INTERNAL;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001644 goto return_prx_cond;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001645
1646 return_bad_res:
Willy Tarreau4781b152021-04-06 13:53:36 +02001647 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Faulet47365272018-10-31 17:40:50 +01001648 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001649 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001650 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Christopher Faulet47365272018-10-31 17:40:50 +01001651 }
Olivier Houcharde3249a92019-05-03 23:01:47 +02001652 if ((s->be->retry_type & PR_RE_JUNK_REQUEST) &&
Christopher Faulete05bf9e2022-03-29 15:23:40 +02001653 (txn->flags & TX_L7_RETRY) &&
Willy Tarreau7cb9e6c2022-05-17 19:40:40 +02001654 do_l7_retry(s, s->scb) == 0) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001655 DBG_TRACE_DEVEL("leaving on L7 retry",
1656 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharde3249a92019-05-03 23:01:47 +02001657 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001658 }
Christopher Faulet47365272018-10-31 17:40:50 +01001659 txn->status = 502;
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001660 stream_inc_http_fail_ctr(s);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001661 /* fall through */
1662
Christopher Fauletb8a53712019-12-16 11:29:38 +01001663 return_prx_cond:
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001664 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet4a66c942023-01-13 09:43:21 +01001665 http_set_term_flags(s);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001666
Willy Tarreaucb041662022-05-17 19:44:42 +02001667 s->scb->flags |= SC_FL_NOLINGER;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001668 DBG_TRACE_DEVEL("leaving on error",
1669 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulet47365272018-10-31 17:40:50 +01001670 return 0;
1671
Christopher Faulete0768eb2018-10-03 16:38:02 +02001672 abort_keep_alive:
1673 /* A keep-alive request to the server failed on a network error.
1674 * The client is required to retry. We need to close without returning
1675 * any other information so that the client retries.
1676 */
1677 txn->status = 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001678 s->logs.logwait = 0;
1679 s->logs.level = 0;
1680 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001681 http_reply_and_close(s, txn->status, NULL);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001682 DBG_TRACE_DEVEL("leaving by closing K/A connection",
1683 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001684 return 0;
1685}
1686
1687/* This function performs all the processing enabled for the current response.
1688 * It normally returns 1 unless it wants to break. It relies on buffers flags,
1689 * and updates s->res.analysers. It might make sense to explode it into several
1690 * other functions. It works like process_request (see indications above).
1691 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001692int http_process_res_common(struct stream *s, struct channel *rep, int an_bit, struct proxy *px)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001693{
1694 struct session *sess = s->sess;
1695 struct http_txn *txn = s->txn;
1696 struct http_msg *msg = &txn->rsp;
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001697 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001698 struct proxy *cur_proxy;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001699 enum rule_result ret = HTTP_RULE_RES_CONT;
1700
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001701 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
1702 return 0;
Christopher Faulet9768c262018-10-22 09:34:31 +02001703
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001704 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001705
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001706 htx = htxbuf(&rep->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001707
1708 /* The stats applet needs to adjust the Connection header but we don't
1709 * apply any filter there.
1710 */
1711 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
1712 rep->analysers &= ~an_bit;
1713 rep->analyse_exp = TICK_ETERNITY;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001714 goto end;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001715 }
1716
1717 /*
1718 * We will have to evaluate the filters.
1719 * As opposed to version 1.2, now they will be evaluated in the
1720 * filters order and not in the header order. This means that
1721 * each filter has to be validated among all headers.
1722 *
1723 * Filters are tried with ->be first, then with ->fe if it is
1724 * different from ->be.
1725 *
1726 * Maybe we are in resume condiion. In this case I choose the
1727 * "struct proxy" which contains the rule list matching the resume
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05001728 * pointer. If none of these "struct proxy" match, I initialise
Christopher Faulete0768eb2018-10-03 16:38:02 +02001729 * the process with the first one.
1730 *
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05001731 * In fact, I check only correspondence between the current list
Christopher Faulete0768eb2018-10-03 16:38:02 +02001732 * pointer and the ->fe rule list. If it doesn't match, I initialize
1733 * the loop with the ->be.
1734 */
Christopher Fauletd4150ad2021-10-13 15:35:55 +02001735 if (s->current_rule_list == &sess->fe->http_res_rules ||
1736 (sess->fe->defpx && s->current_rule_list == &sess->fe->defpx->http_res_rules))
Christopher Faulete0768eb2018-10-03 16:38:02 +02001737 cur_proxy = sess->fe;
1738 else
1739 cur_proxy = s->be;
Christopher Fauletd4150ad2021-10-13 15:35:55 +02001740
Christopher Faulete0768eb2018-10-03 16:38:02 +02001741 while (1) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001742 /* evaluate http-response rules */
Christopher Faulet46f46df2021-11-09 16:33:25 +01001743 if (ret == HTTP_RULE_RES_CONT || ret == HTTP_RULE_RES_STOP) {
Christopher Fauletd4150ad2021-10-13 15:35:55 +02001744 struct list *def_rules, *rules;
1745
1746 def_rules = ((cur_proxy->defpx && (cur_proxy == s->be || cur_proxy->defpx != s->be->defpx)) ? &cur_proxy->defpx->http_res_rules : NULL);
1747 rules = &cur_proxy->http_res_rules;
1748
1749 ret = http_res_get_intercept_rule(cur_proxy, def_rules, rules, s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001750
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001751 switch (ret) {
1752 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
1753 goto return_prx_yield;
1754
1755 case HTTP_RULE_RES_CONT:
1756 case HTTP_RULE_RES_STOP: /* nothing to do */
1757 break;
1758
1759 case HTTP_RULE_RES_DENY: /* deny or tarpit */
1760 goto deny;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001761
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001762 case HTTP_RULE_RES_ABRT: /* abort request, response already sent */
1763 goto return_prx_cond;
1764
1765 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
Christopher Fauletb8a53712019-12-16 11:29:38 +01001766 goto done;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001767
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001768 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
1769 goto return_bad_res;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001770
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001771 case HTTP_RULE_RES_ERROR: /* failed with a bad request */
1772 goto return_int_err;
1773 }
1774
1775 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001776
Christopher Faulete0768eb2018-10-03 16:38:02 +02001777 /* check whether we're already working on the frontend */
1778 if (cur_proxy == sess->fe)
1779 break;
1780 cur_proxy = sess->fe;
1781 }
1782
Christopher Faulete0768eb2018-10-03 16:38:02 +02001783 /* OK that's all we can do for 1xx responses */
1784 if (unlikely(txn->status < 200 && txn->status != 101))
Christopher Fauletf2824e62018-10-01 12:12:37 +02001785 goto end;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001786
1787 /*
1788 * Now check for a server cookie.
1789 */
1790 if (s->be->cookie_name || sess->fe->capture_name || (s->be->options & PR_O_CHK_CACHE))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001791 http_manage_server_side_cookies(s, rep);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001792
1793 /*
1794 * Check for cache-control or pragma headers if required.
1795 */
1796 if ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001797 http_check_response_for_cacheability(s, rep);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001798
1799 /*
1800 * Add server cookie in the response if needed
1801 */
1802 if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) &&
1803 !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) &&
1804 (!(s->flags & SF_DIRECT) ||
1805 ((s->be->cookie_maxidle || txn->cookie_last_date) &&
1806 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
1807 (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
1808 (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
1809 (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
1810 !(s->flags & SF_IGNORE_PRST)) {
1811 /* the server is known, it's not the one the client requested, or the
1812 * cookie's last seen date needs to be refreshed. We have to
1813 * insert a set-cookie here, except if we want to insert only on POST
1814 * requests and this one isn't. Note that servers which don't have cookies
1815 * (eg: some backup servers) will return a full cookie removal request.
1816 */
Willy Tarreau88bc8002021-12-06 07:01:02 +00001817 if (!__objt_server(s->target)->cookie) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001818 chunk_printf(&trash,
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001819 "%s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
Christopher Faulete0768eb2018-10-03 16:38:02 +02001820 s->be->cookie_name);
1821 }
1822 else {
Willy Tarreau88bc8002021-12-06 07:01:02 +00001823 chunk_printf(&trash, "%s=%s", s->be->cookie_name, __objt_server(s->target)->cookie);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001824
1825 if (s->be->cookie_maxidle || s->be->cookie_maxlife) {
1826 /* emit last_date, which is mandatory */
1827 trash.area[trash.data++] = COOKIE_DELIM_DATE;
1828 s30tob64((date.tv_sec+3) >> 2,
1829 trash.area + trash.data);
1830 trash.data += 5;
1831
1832 if (s->be->cookie_maxlife) {
1833 /* emit first_date, which is either the original one or
1834 * the current date.
1835 */
1836 trash.area[trash.data++] = COOKIE_DELIM_DATE;
1837 s30tob64(txn->cookie_first_date ?
1838 txn->cookie_first_date >> 2 :
1839 (date.tv_sec+3) >> 2,
1840 trash.area + trash.data);
1841 trash.data += 5;
1842 }
1843 }
1844 chunk_appendf(&trash, "; path=/");
1845 }
1846
1847 if (s->be->cookie_domain)
1848 chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain);
1849
1850 if (s->be->ck_opts & PR_CK_HTTPONLY)
1851 chunk_appendf(&trash, "; HttpOnly");
1852
1853 if (s->be->ck_opts & PR_CK_SECURE)
1854 chunk_appendf(&trash, "; Secure");
1855
Christopher Faulet2f533902020-01-21 11:06:48 +01001856 if (s->be->cookie_attrs)
1857 chunk_appendf(&trash, "; %s", s->be->cookie_attrs);
1858
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001859 if (unlikely(!http_add_header(htx, ist("Set-Cookie"), ist2(trash.area, trash.data))))
Christopher Fauletd649b572022-06-01 17:42:35 +02001860 goto return_fail_rewrite;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001861
1862 txn->flags &= ~TX_SCK_MASK;
1863 if (__objt_server(s->target)->cookie && (s->flags & SF_DIRECT))
1864 /* the server did not change, only the date was updated */
1865 txn->flags |= TX_SCK_UPDATED;
1866 else
1867 txn->flags |= TX_SCK_INSERTED;
1868
1869 /* Here, we will tell an eventual cache on the client side that we don't
1870 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
1871 * Some caches understand the correct form: 'no-cache="set-cookie"', but
1872 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
1873 */
1874 if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
1875
1876 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
1877
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001878 if (unlikely(!http_add_header(htx, ist("Cache-control"), ist("private"))))
Christopher Fauletd649b572022-06-01 17:42:35 +02001879 goto return_fail_rewrite;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001880 }
1881 }
1882
1883 /*
1884 * Check if result will be cacheable with a cookie.
1885 * We'll block the response if security checks have caught
1886 * nasty things such as a cacheable cookie.
1887 */
1888 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
1889 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
1890 (s->be->options & PR_O_CHK_CACHE)) {
1891 /* we're in presence of a cacheable response containing
1892 * a set-cookie header. We'll block it as requested by
1893 * the 'checkcache' option, and send an alert.
1894 */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001895 ha_alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreau88bc8002021-12-06 07:01:02 +00001896 s->be->id, objt_server(s->target) ? __objt_server(s->target)->id : "<dispatch>");
Christopher Faulete0768eb2018-10-03 16:38:02 +02001897 send_log(s->be, LOG_ALERT,
1898 "Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreau88bc8002021-12-06 07:01:02 +00001899 s->be->id, objt_server(s->target) ? __objt_server(s->target)->id : "<dispatch>");
Christopher Fauletb8a53712019-12-16 11:29:38 +01001900 goto deny;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001901 }
1902
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001903 end:
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01001904 /*
1905 * Evaluate after-response rules before forwarding the response. rules
1906 * from the backend are evaluated first, then one from the frontend if
1907 * it differs.
1908 */
1909 if (!http_eval_after_res_rules(s))
1910 goto return_int_err;
1911
Christopher Fauletc2ac5e42021-03-08 18:20:09 +01001912 /* Filter the response headers if there are filters attached to the
1913 * stream.
1914 */
1915 if (HAS_FILTERS(s))
1916 rep->analysers |= AN_RES_FLT_HTTP_HDRS;
1917
Christopher Faulete0768eb2018-10-03 16:38:02 +02001918 /* Always enter in the body analyzer */
1919 rep->analysers &= ~AN_RES_FLT_XFER_DATA;
1920 rep->analysers |= AN_RES_HTTP_XFER_BODY;
1921
1922 /* if the user wants to log as soon as possible, without counting
1923 * bytes from the server, then this is the right moment. We have
1924 * to temporarily assign bytes_out to log what we currently have.
1925 */
1926 if (!LIST_ISEMPTY(&sess->fe->logformat) && !(s->logs.logwait & LW_BYTES)) {
1927 s->logs.t_close = s->logs.t_data; /* to get a valid end date */
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001928 s->logs.bytes_out = htx->data;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001929 s->do_log(s);
1930 s->logs.bytes_out = 0;
1931 }
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001932
Christopher Fauletb8a53712019-12-16 11:29:38 +01001933 done:
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01001934 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletb8a53712019-12-16 11:29:38 +01001935 rep->analysers &= ~an_bit;
1936 rep->analyse_exp = TICK_ETERNITY;
Christopher Fauletd4150ad2021-10-13 15:35:55 +02001937 s->current_rule = s->current_rule_list = NULL;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001938 return 1;
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001939
Christopher Fauletb8a53712019-12-16 11:29:38 +01001940 deny:
Willy Tarreau4781b152021-04-06 13:53:36 +02001941 _HA_ATOMIC_INC(&sess->fe->fe_counters.denied_resp);
1942 _HA_ATOMIC_INC(&s->be->be_counters.denied_resp);
William Lallemand36119de2021-03-08 15:26:48 +01001943 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001944 _HA_ATOMIC_INC(&sess->listener->counters->denied_resp);
Christopher Fauleta08546b2019-12-16 16:07:34 +01001945 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001946 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.denied_resp);
Christopher Fauletb8a53712019-12-16 11:29:38 +01001947 goto return_prx_err;
1948
Christopher Fauletd649b572022-06-01 17:42:35 +02001949 return_fail_rewrite:
1950 if (!(s->flags & SF_ERR_MASK))
1951 s->flags |= SF_ERR_PRXCOND;
1952 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_rewrites);
1953 _HA_ATOMIC_INC(&s->be->be_counters.failed_rewrites);
1954 if (sess->listener && sess->listener->counters)
1955 _HA_ATOMIC_INC(&sess->listener->counters->failed_rewrites);
1956 if (objt_server(s->target))
1957 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_rewrites);
1958 /* fall through */
1959
Christopher Fauletb8a53712019-12-16 11:29:38 +01001960 return_int_err:
1961 txn->status = 500;
1962 if (!(s->flags & SF_ERR_MASK))
1963 s->flags |= SF_ERR_INTERNAL;
Willy Tarreau4781b152021-04-06 13:53:36 +02001964 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
1965 _HA_ATOMIC_INC(&s->be->be_counters.internal_errors);
Dragan Dosen9a006f92021-09-21 13:02:09 +02001966 if (sess->listener && sess->listener->counters)
1967 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001968 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001969 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.internal_errors);
Christopher Fauletb8a53712019-12-16 11:29:38 +01001970 goto return_prx_err;
1971
1972 return_bad_res:
1973 txn->status = 502;
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001974 stream_inc_http_fail_ctr(s);
Willy Tarreau4781b152021-04-06 13:53:36 +02001975 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Fauleta20a6532020-02-05 10:16:41 +01001976 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001977 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Christopher Fauleta20a6532020-02-05 10:16:41 +01001978 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_RSP);
1979 }
Christopher Fauletb8a53712019-12-16 11:29:38 +01001980 /* fall through */
1981
1982 return_prx_err:
1983 http_reply_and_close(s, txn->status, http_error_message(s));
1984 /* fall through */
1985
1986 return_prx_cond:
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001987 s->logs.t_data = -1; /* was not a valid response */
Willy Tarreaucb041662022-05-17 19:44:42 +02001988 s->scb->flags |= SC_FL_NOLINGER;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001989
Christopher Faulet4a66c942023-01-13 09:43:21 +01001990 http_set_term_flags(s);
Christopher Fauletb8a53712019-12-16 11:29:38 +01001991
Christopher Faulete58c0002020-03-02 16:21:01 +01001992 rep->analysers &= AN_RES_FLT_END;
1993 s->req.analysers &= AN_REQ_FLT_END;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001994 rep->analyse_exp = TICK_ETERNITY;
Christopher Fauletd4150ad2021-10-13 15:35:55 +02001995 s->current_rule = s->current_rule_list = NULL;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001996 DBG_TRACE_DEVEL("leaving on error",
1997 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001998 return 0;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001999
2000 return_prx_yield:
2001 channel_dont_close(rep);
2002 DBG_TRACE_DEVEL("waiting for more data",
2003 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
2004 return 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002005}
2006
2007/* This function is an analyser which forwards response body (including chunk
2008 * sizes if any). It is called as soon as we must forward, even if we forward
2009 * zero byte. The only situation where it must not be called is when we're in
2010 * tunnel mode and we want to forward till the close. It's used both to forward
2011 * remaining data and to resync after end of body. It expects the msg_state to
2012 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
2013 * read more data, or 1 once we can go on with next request or end the stream.
2014 *
2015 * It is capable of compressing response data both in content-length mode and
2016 * in chunked mode. The state machines follows different flows depending on
2017 * whether content-length and chunked modes are used, since there are no
2018 * trailers in content-length :
2019 *
2020 * chk-mode cl-mode
2021 * ,----- BODY -----.
2022 * / \
2023 * V size > 0 V chk-mode
2024 * .--> SIZE -------------> DATA -------------> CRLF
2025 * | | size == 0 | last byte |
2026 * | v final crlf v inspected |
2027 * | TRAILERS -----------> DONE |
2028 * | |
2029 * `----------------------------------------------'
2030 *
2031 * Compression only happens in the DATA state, and must be flushed in final
2032 * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding
2033 * is performed at once on final states for all bytes parsed, or when leaving
2034 * on missing data.
2035 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002036int http_response_forward_body(struct stream *s, struct channel *res, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +02002037{
2038 struct session *sess = s->sess;
2039 struct http_txn *txn = s->txn;
2040 struct http_msg *msg = &s->txn->rsp;
Christopher Faulet9768c262018-10-22 09:34:31 +02002041 struct htx *htx;
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002042 int ret;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002043
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002044 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002045
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002046 htx = htxbuf(&res->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002047
Christopher Fauletb9a92f32019-09-09 10:15:21 +02002048 if (htx->flags & HTX_FL_PARSING_ERROR)
2049 goto return_bad_res;
2050 if (htx->flags & HTX_FL_PROCESSING_ERROR)
2051 goto return_int_err;
2052
Christopher Faulet9768c262018-10-22 09:34:31 +02002053 if (msg->msg_state == HTTP_MSG_BODY)
2054 msg->msg_state = HTTP_MSG_DATA;
2055
Christopher Faulete0768eb2018-10-03 16:38:02 +02002056 /* in most states, we should abort in case of early close */
2057 channel_auto_close(res);
2058
Christopher Faulete0768eb2018-10-03 16:38:02 +02002059 if (res->to_forward) {
Christopher Faulet66af0b22019-03-22 14:54:52 +01002060 if (res->to_forward == CHN_INFINITE_FORWARD) {
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002061 if (res->flags & CF_EOI)
2062 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet66af0b22019-03-22 14:54:52 +01002063 }
2064 else {
2065 /* We can't process the buffer's contents yet */
2066 res->flags |= CF_WAKE_WRITE;
2067 goto missing_data_or_waiting;
2068 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02002069 }
2070
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002071 if (msg->msg_state >= HTTP_MSG_ENDING)
2072 goto ending;
2073
Christopher Fauletc75668e2020-12-07 18:10:32 +01002074 if ((txn->meth == HTTP_METH_CONNECT && txn->status >= 200 && txn->status < 300) || txn->status == 101 ||
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002075 (!(msg->flags & HTTP_MSGF_XFER_LEN) && !HAS_RSP_DATA_FILTERS(s))) {
2076 msg->msg_state = HTTP_MSG_ENDING;
2077 goto ending;
2078 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002079
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002080 /* Forward input data. We get it by removing all outgoing data not
2081 * forwarded yet from HTX data size. If there are some data filters, we
2082 * let them decide the amount of data to forward.
Christopher Faulet9768c262018-10-22 09:34:31 +02002083 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002084 if (HAS_RSP_DATA_FILTERS(s)) {
2085 ret = flt_http_payload(s, msg, htx->data);
2086 if (ret < 0)
2087 goto return_bad_res;
Christopher Faulet421e7692019-06-13 11:16:45 +02002088 c_adv(res, ret);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002089 }
2090 else {
Christopher Faulet421e7692019-06-13 11:16:45 +02002091 c_adv(res, htx->data - co_data(res));
Christopher Faulet66af0b22019-03-22 14:54:52 +01002092 if (msg->flags & HTTP_MSGF_XFER_LEN)
2093 channel_htx_forward_forever(res, htx);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002094 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002095
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002096 if (htx->data != co_data(res))
2097 goto missing_data_or_waiting;
2098
2099 if (!(msg->flags & HTTP_MSGF_XFER_LEN) && res->flags & CF_SHUTR) {
2100 msg->msg_state = HTTP_MSG_ENDING;
2101 goto ending;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002102 }
2103
Christopher Faulet9768c262018-10-22 09:34:31 +02002104 /* Check if the end-of-message is reached and if so, switch the message
Christopher Fauletd20fdb02019-06-13 16:43:22 +02002105 * in HTTP_MSG_ENDING state. Then if all data was marked to be
2106 * forwarded, set the state to HTTP_MSG_DONE.
Christopher Faulet9768c262018-10-22 09:34:31 +02002107 */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002108 if (!(htx->flags & HTX_FL_EOM))
Christopher Faulet9768c262018-10-22 09:34:31 +02002109 goto missing_data_or_waiting;
2110
Christopher Fauletd20fdb02019-06-13 16:43:22 +02002111 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet9768c262018-10-22 09:34:31 +02002112
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002113 ending:
Christopher Faulet2151cdd2020-07-22 16:34:59 +02002114 res->flags &= ~CF_EXPECT_MORE; /* no more data are expected */
2115
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002116 /* other states, ENDING...TUNNEL */
2117 if (msg->msg_state >= HTTP_MSG_DONE)
2118 goto done;
Christopher Faulet9768c262018-10-22 09:34:31 +02002119
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002120 if (HAS_RSP_DATA_FILTERS(s)) {
2121 ret = flt_http_end(s, msg);
2122 if (ret <= 0) {
2123 if (!ret)
2124 goto missing_data_or_waiting;
2125 goto return_bad_res;
2126 }
2127 }
2128
Christopher Fauletc75668e2020-12-07 18:10:32 +01002129 if ((txn->meth == HTTP_METH_CONNECT && txn->status >= 200 && txn->status < 300) || txn->status == 101 ||
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002130 !(msg->flags & HTTP_MSGF_XFER_LEN)) {
2131 msg->msg_state = HTTP_MSG_TUNNEL;
2132 goto ending;
2133 }
2134 else {
2135 msg->msg_state = HTTP_MSG_DONE;
2136 res->to_forward = 0;
2137 }
2138
2139 done:
2140
2141 channel_dont_close(res);
2142
Christopher Fauletf2b02cf2023-01-13 11:02:28 +01002143 if ((res->flags & CF_SHUTW) && co_data(res)) {
2144 /* response errors are most likely due to the client aborting
2145 * the transfer. */
2146 goto return_cli_abort;
2147 }
2148
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002149 http_end_response(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002150 if (!(res->analysers & an_bit)) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002151 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002152 return 1;
2153 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002154 DBG_TRACE_DEVEL("waiting for the end of the HTTP txn",
2155 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002156 return 0;
2157
2158 missing_data_or_waiting:
2159 if (res->flags & CF_SHUTW)
Christopher Faulet93e02d82019-03-08 14:18:50 +01002160 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002161
2162 /* stop waiting for data if the input is closed before the end. If the
2163 * client side was already closed, it means that the client has aborted,
2164 * so we don't want to count this as a server abort. Otherwise it's a
2165 * server abort.
2166 */
Christopher Fauletd20fdb02019-06-13 16:43:22 +02002167 if (msg->msg_state < HTTP_MSG_ENDING && res->flags & CF_SHUTR) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02002168 if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))
Christopher Faulet93e02d82019-03-08 14:18:50 +01002169 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002170 /* If we have some pending data, we continue the processing */
Christopher Faulet93e02d82019-03-08 14:18:50 +01002171 if (htx_is_empty(htx))
2172 goto return_srv_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002173 }
2174
Christopher Faulete0768eb2018-10-03 16:38:02 +02002175 /* When TE: chunked is used, we need to get there again to parse
2176 * remaining chunks even if the server has closed, so we don't want to
Christopher Faulet9768c262018-10-22 09:34:31 +02002177 * set CF_DONTCLOSE. Similarly when there is a content-leng or if there
2178 * are filters registered on the stream, we don't want to forward a
2179 * close
Christopher Faulete0768eb2018-10-03 16:38:02 +02002180 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002181 if ((msg->flags & HTTP_MSGF_XFER_LEN) || HAS_RSP_DATA_FILTERS(s))
Christopher Faulete0768eb2018-10-03 16:38:02 +02002182 channel_dont_close(res);
2183
2184 /* We know that more data are expected, but we couldn't send more that
2185 * what we did. So we always set the CF_EXPECT_MORE flag so that the
2186 * system knows it must not set a PUSH on this first part. Interactive
2187 * modes are already handled by the stream sock layer. We must not do
2188 * this in content-length mode because it could present the MSG_MORE
2189 * flag with the last block of forwarded data, which would cause an
2190 * additional delay to be observed by the receiver.
2191 */
Christopher Faulet2151cdd2020-07-22 16:34:59 +02002192 if (HAS_RSP_DATA_FILTERS(s))
Christopher Faulete0768eb2018-10-03 16:38:02 +02002193 res->flags |= CF_EXPECT_MORE;
2194
2195 /* the stream handler will take care of timeouts and errors */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002196 DBG_TRACE_DEVEL("waiting for more data to forward",
2197 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002198 return 0;
2199
Christopher Faulet93e02d82019-03-08 14:18:50 +01002200 return_srv_abort:
Willy Tarreau4781b152021-04-06 13:53:36 +02002201 _HA_ATOMIC_INC(&sess->fe->fe_counters.srv_aborts);
2202 _HA_ATOMIC_INC(&s->be->be_counters.srv_aborts);
William Lallemand36119de2021-03-08 15:26:48 +01002203 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02002204 _HA_ATOMIC_INC(&sess->listener->counters->srv_aborts);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002205 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02002206 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.srv_aborts);
Willy Tarreau826f3ab2021-02-10 12:07:15 +01002207 stream_inc_http_fail_ctr(s);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002208 if (!(s->flags & SF_ERR_MASK))
Christopher Fauletf2b02cf2023-01-13 11:02:28 +01002209 s->flags |= ((res->flags & CF_READ_TIMEOUT) ? SF_ERR_SRVTO : SF_ERR_SRVCL);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002210 goto return_error;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002211
Christopher Faulet93e02d82019-03-08 14:18:50 +01002212 return_cli_abort:
Willy Tarreau4781b152021-04-06 13:53:36 +02002213 _HA_ATOMIC_INC(&sess->fe->fe_counters.cli_aborts);
2214 _HA_ATOMIC_INC(&s->be->be_counters.cli_aborts);
William Lallemand36119de2021-03-08 15:26:48 +01002215 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02002216 _HA_ATOMIC_INC(&sess->listener->counters->cli_aborts);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002217 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02002218 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.cli_aborts);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002219 if (!(s->flags & SF_ERR_MASK))
Christopher Fauletf2b02cf2023-01-13 11:02:28 +01002220 s->flags |= ((res->flags & CF_WRITE_TIMEOUT) ? SF_ERR_CLITO : SF_ERR_CLICL);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002221 goto return_error;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002222
Christopher Fauletb9a92f32019-09-09 10:15:21 +02002223 return_int_err:
Willy Tarreau4781b152021-04-06 13:53:36 +02002224 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
2225 _HA_ATOMIC_INC(&s->be->be_counters.internal_errors);
William Lallemand36119de2021-03-08 15:26:48 +01002226 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02002227 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002228 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02002229 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.internal_errors);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02002230 if (!(s->flags & SF_ERR_MASK))
2231 s->flags |= SF_ERR_INTERNAL;
2232 goto return_error;
2233
Christopher Faulet93e02d82019-03-08 14:18:50 +01002234 return_bad_res:
Willy Tarreau4781b152021-04-06 13:53:36 +02002235 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002236 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02002237 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002238 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_RSP);
2239 }
Willy Tarreau826f3ab2021-02-10 12:07:15 +01002240 stream_inc_http_fail_ctr(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002241 if (!(s->flags & SF_ERR_MASK))
Christopher Faulet93e02d82019-03-08 14:18:50 +01002242 s->flags |= SF_ERR_SRVCL;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002243 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +02002244
Christopher Faulet93e02d82019-03-08 14:18:50 +01002245 return_error:
Christopher Faulete0768eb2018-10-03 16:38:02 +02002246 /* don't send any error message as we're in the body */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002247 http_reply_and_close(s, txn->status, NULL);
Christopher Fauletf2b02cf2023-01-13 11:02:28 +01002248 http_set_term_flags(s);
2249 stream_inc_http_fail_ctr(s);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002250 DBG_TRACE_DEVEL("leaving on error",
2251 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002252 return 0;
2253}
2254
Christopher Fauletf2824e62018-10-01 12:12:37 +02002255/* Perform an HTTP redirect based on the information in <rule>. The function
Willy Tarreaubc1223b2021-09-02 16:54:33 +02002256 * returns zero in case of an irrecoverable error such as too large a request
2257 * to build a valid response, 1 in case of successful redirect (hence the rule
2258 * is final), or 2 if the rule has to be silently skipped.
Christopher Fauletf2824e62018-10-01 12:12:37 +02002259 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002260int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn)
Christopher Fauletf2824e62018-10-01 12:12:37 +02002261{
Christopher Faulet99daf282018-11-28 22:58:13 +01002262 struct channel *req = &s->req;
2263 struct channel *res = &s->res;
2264 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01002265 struct htx_sl *sl;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002266 struct buffer *chunk;
Christopher Faulet99daf282018-11-28 22:58:13 +01002267 struct ist status, reason, location;
2268 unsigned int flags;
Christopher Fauleteab17572022-04-26 20:34:38 +02002269 int ret = 1, close = 0; /* Try to keep the connection alive byt default */
Christopher Fauletf2824e62018-10-01 12:12:37 +02002270
2271 chunk = alloc_trash_chunk();
Christopher Fauletb8a53712019-12-16 11:29:38 +01002272 if (!chunk) {
2273 if (!(s->flags & SF_ERR_MASK))
2274 s->flags |= SF_ERR_RESOURCE;
Christopher Faulet99daf282018-11-28 22:58:13 +01002275 goto fail;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002276 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002277
Christopher Faulet99daf282018-11-28 22:58:13 +01002278 /*
2279 * Create the location
2280 */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002281 htx = htxbuf(&req->buf);
Christopher Fauletf2824e62018-10-01 12:12:37 +02002282 switch(rule->type) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002283 case REDIRECT_TYPE_SCHEME: {
2284 struct http_hdr_ctx ctx;
2285 struct ist path, host;
Amaury Denoyellec453f952021-07-06 11:40:12 +02002286 struct http_uri_parser parser;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002287
Christopher Faulet99daf282018-11-28 22:58:13 +01002288 host = ist("");
2289 ctx.blk = NULL;
2290 if (http_find_header(htx, ist("Host"), &ctx, 0))
2291 host = ctx.value;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002292
Christopher Faulet297fbb42019-05-13 14:41:27 +02002293 sl = http_get_stline(htx);
Amaury Denoyellec453f952021-07-06 11:40:12 +02002294 parser = http_uri_parser_init(htx_sl_req_uri(sl));
2295 path = http_parse_path(&parser);
Christopher Faulet99daf282018-11-28 22:58:13 +01002296 /* build message using path */
Tim Duesterhused526372020-03-05 17:56:33 +01002297 if (isttest(path)) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002298 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2299 int qs = 0;
2300 while (qs < path.len) {
2301 if (*(path.ptr + qs) == '?') {
2302 path.len = qs;
2303 break;
2304 }
2305 qs++;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002306 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002307 }
2308 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002309 else
2310 path = ist("/");
Christopher Fauletf2824e62018-10-01 12:12:37 +02002311
Christopher Faulet99daf282018-11-28 22:58:13 +01002312 if (rule->rdr_str) { /* this is an old "redirect" rule */
2313 /* add scheme */
2314 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2315 goto fail;
2316 }
2317 else {
2318 /* add scheme with executing log format */
2319 chunk->data += build_logline(s, chunk->area + chunk->data,
2320 chunk->size - chunk->data,
2321 &rule->rdr_fmt);
2322 }
2323 /* add "://" + host + path */
2324 if (!chunk_memcat(chunk, "://", 3) ||
2325 !chunk_memcat(chunk, host.ptr, host.len) ||
2326 !chunk_memcat(chunk, path.ptr, path.len))
2327 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002328
Christopher Faulet99daf282018-11-28 22:58:13 +01002329 /* append a slash at the end of the location if needed and missing */
2330 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
2331 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
2332 if (chunk->data + 1 >= chunk->size)
2333 goto fail;
2334 chunk->area[chunk->data++] = '/';
2335 }
2336 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002337 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002338
Christopher Faulet99daf282018-11-28 22:58:13 +01002339 case REDIRECT_TYPE_PREFIX: {
2340 struct ist path;
Amaury Denoyellec453f952021-07-06 11:40:12 +02002341 struct http_uri_parser parser;
Christopher Faulet99daf282018-11-28 22:58:13 +01002342
Christopher Faulet297fbb42019-05-13 14:41:27 +02002343 sl = http_get_stline(htx);
Amaury Denoyellec453f952021-07-06 11:40:12 +02002344 parser = http_uri_parser_init(htx_sl_req_uri(sl));
2345 path = http_parse_path(&parser);
Christopher Faulet99daf282018-11-28 22:58:13 +01002346 /* build message using path */
Tim Duesterhused526372020-03-05 17:56:33 +01002347 if (isttest(path)) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002348 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2349 int qs = 0;
2350 while (qs < path.len) {
2351 if (*(path.ptr + qs) == '?') {
2352 path.len = qs;
2353 break;
2354 }
2355 qs++;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002356 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002357 }
2358 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002359 else
2360 path = ist("/");
Christopher Fauletf2824e62018-10-01 12:12:37 +02002361
Christopher Faulet99daf282018-11-28 22:58:13 +01002362 if (rule->rdr_str) { /* this is an old "redirect" rule */
2363 /* add prefix. Note that if prefix == "/", we don't want to
2364 * add anything, otherwise it makes it hard for the user to
2365 * configure a self-redirection.
2366 */
2367 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
2368 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2369 goto fail;
2370 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002371 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002372 else {
2373 /* add prefix with executing log format */
2374 chunk->data += build_logline(s, chunk->area + chunk->data,
2375 chunk->size - chunk->data,
2376 &rule->rdr_fmt);
2377 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002378
Christopher Faulet99daf282018-11-28 22:58:13 +01002379 /* add path */
2380 if (!chunk_memcat(chunk, path.ptr, path.len))
2381 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002382
Christopher Faulet99daf282018-11-28 22:58:13 +01002383 /* append a slash at the end of the location if needed and missing */
2384 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
2385 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
2386 if (chunk->data + 1 >= chunk->size)
2387 goto fail;
2388 chunk->area[chunk->data++] = '/';
2389 }
2390 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002391 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002392 case REDIRECT_TYPE_LOCATION:
2393 default:
2394 if (rule->rdr_str) { /* this is an old "redirect" rule */
2395 /* add location */
2396 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2397 goto fail;
2398 }
2399 else {
2400 /* add location with executing log format */
Willy Tarreaubc1223b2021-09-02 16:54:33 +02002401 int len = build_logline(s, chunk->area + chunk->data,
2402 chunk->size - chunk->data,
2403 &rule->rdr_fmt);
Christopher Fauleteab17572022-04-26 20:34:38 +02002404 if (!len && rule->flags & REDIRECT_FLAG_IGNORE_EMPTY) {
2405 ret = 2;
2406 goto out;
2407 }
Willy Tarreaubc1223b2021-09-02 16:54:33 +02002408
2409 chunk->data += len;
Christopher Faulet99daf282018-11-28 22:58:13 +01002410 }
2411 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002412 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002413 location = ist2(chunk->area, chunk->data);
2414
2415 /*
2416 * Create the 30x response
2417 */
2418 switch (rule->code) {
2419 case 308:
2420 status = ist("308");
2421 reason = ist("Permanent Redirect");
2422 break;
2423 case 307:
2424 status = ist("307");
2425 reason = ist("Temporary Redirect");
2426 break;
2427 case 303:
2428 status = ist("303");
2429 reason = ist("See Other");
2430 break;
2431 case 301:
2432 status = ist("301");
2433 reason = ist("Moved Permanently");
2434 break;
2435 case 302:
2436 default:
2437 status = ist("302");
2438 reason = ist("Found");
2439 break;
2440 }
2441
Christopher Faulet08e66462019-05-23 16:44:59 +02002442 if (!(txn->req.flags & HTTP_MSGF_BODYLESS) && txn->req.msg_state != HTTP_MSG_DONE)
2443 close = 1;
2444
Christopher Faulet99daf282018-11-28 22:58:13 +01002445 htx = htx_from_buf(&res->buf);
Kevin Zhu96b36392020-01-07 09:42:55 +01002446 /* Trim any possible response */
2447 channel_htx_truncate(&s->res, htx);
Christopher Faulet99daf282018-11-28 22:58:13 +01002448 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
2449 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), status, reason);
2450 if (!sl)
2451 goto fail;
2452 sl->info.res.status = rule->code;
2453 s->txn->status = rule->code;
2454
Christopher Faulet08e66462019-05-23 16:44:59 +02002455 if (close && !htx_add_header(htx, ist("Connection"), ist("close")))
2456 goto fail;
2457
2458 if (!htx_add_header(htx, ist("Content-length"), ist("0")) ||
Christopher Faulet99daf282018-11-28 22:58:13 +01002459 !htx_add_header(htx, ist("Location"), location))
2460 goto fail;
2461
2462 if (rule->code == 302 || rule->code == 303 || rule->code == 307) {
2463 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")))
2464 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002465 }
2466
2467 if (rule->cookie_len) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002468 if (!htx_add_header(htx, ist("Set-Cookie"), ist2(rule->cookie_str, rule->cookie_len)))
2469 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002470 }
2471
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002472 if (!htx_add_endof(htx, HTX_BLK_EOH))
Christopher Faulet99daf282018-11-28 22:58:13 +01002473 goto fail;
2474
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002475 htx->flags |= HTX_FL_EOM;
Kevin Zhu96b36392020-01-07 09:42:55 +01002476 htx_to_buf(htx, &res->buf);
Christopher Fauleta72a7e42020-01-28 09:28:11 +01002477 if (!http_forward_proxy_resp(s, 1))
2478 goto fail;
Christopher Faulet99daf282018-11-28 22:58:13 +01002479
Christopher Faulet60b33a52020-01-28 09:18:10 +01002480 if (rule->flags & REDIRECT_FLAG_FROM_REQ) {
2481 /* let's log the request time */
2482 s->logs.tv_request = now;
Christopher Fauletd3475882021-10-04 14:16:46 +02002483 req->analysers &= AN_REQ_FLT_END;
Christopher Faulet99daf282018-11-28 22:58:13 +01002484
Christopher Faulet60b33a52020-01-28 09:18:10 +01002485 if (s->sess->fe == s->be) /* report it if the request was intercepted by the frontend */
Willy Tarreau4781b152021-04-06 13:53:36 +02002486 _HA_ATOMIC_INC(&s->sess->fe->fe_counters.intercepted_req);
Christopher Faulet60b33a52020-01-28 09:18:10 +01002487 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002488
2489 if (!(s->flags & SF_ERR_MASK))
2490 s->flags |= SF_ERR_LOCAL;
Christopher Faulet4a66c942023-01-13 09:43:21 +01002491 http_set_term_flags(s);
Christopher Fauletf2824e62018-10-01 12:12:37 +02002492
Christopher Fauleteab17572022-04-26 20:34:38 +02002493 out:
Christopher Faulet99daf282018-11-28 22:58:13 +01002494 free_trash_chunk(chunk);
Christopher Fauleteab17572022-04-26 20:34:38 +02002495 return ret;
Christopher Faulet99daf282018-11-28 22:58:13 +01002496
2497 fail:
2498 /* If an error occurred, remove the incomplete HTTP response from the
2499 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01002500 channel_htx_truncate(res, htxbuf(&res->buf));
Christopher Fauleteab17572022-04-26 20:34:38 +02002501 ret = 0;
2502 goto out;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002503}
2504
Christopher Faulet18c13d32022-05-16 11:43:10 +02002505/* This function filters the request header names to only allow [0-9a-zA-Z-]
2506 * characters. Depending on the proxy configuration, headers with a name not
2507 * matching this charset are removed or the request is rejected with a
2508 * 403-Forbidden response if such name are found. It returns HTTP_RULE_RES_CONT
2509 * to continue the request processing or HTTP_RULE_RES_DENY if the request is
2510 * rejected.
2511 */
2512static enum rule_result http_req_restrict_header_names(struct stream *s, struct htx *htx, struct proxy *px)
2513{
2514 struct htx_blk *blk;
2515 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
2516
2517 blk = htx_get_first_blk(htx);
2518 while (blk) {
2519 enum htx_blk_type type = htx_get_blk_type(blk);
2520
2521 if (type == HTX_BLK_HDR) {
2522 struct ist n = htx_get_blk_name(htx, blk);
Mateusz Malek4b85a962022-08-17 14:22:09 +02002523 int i, end = istlen(n);
Christopher Faulet18c13d32022-05-16 11:43:10 +02002524
Mateusz Malek4b85a962022-08-17 14:22:09 +02002525 for (i = 0; i < end; i++) {
Christopher Faulet18c13d32022-05-16 11:43:10 +02002526 if (!isalnum((unsigned char)n.ptr[i]) && n.ptr[i] != '-') {
Mateusz Malek4b85a962022-08-17 14:22:09 +02002527 break;
Christopher Faulet18c13d32022-05-16 11:43:10 +02002528 }
2529 }
Mateusz Malek4b85a962022-08-17 14:22:09 +02002530
2531 if (i < end) {
2532 /* Disallowed character found - block the request or remove the header */
2533 if (px->options2 & PR_O2_RSTRICT_REQ_HDR_NAMES_BLK)
2534 goto block;
2535 blk = htx_remove_blk(htx, blk);
2536 continue;
2537 }
Christopher Faulet18c13d32022-05-16 11:43:10 +02002538 }
2539 if (type == HTX_BLK_EOH)
2540 break;
2541
2542 blk = htx_get_next_blk(htx, blk);
2543 }
2544 out:
2545 return rule_ret;
2546 block:
2547 /* Block the request returning a 403-Forbidden response */
2548 s->txn->status = 403;
2549 rule_ret = HTTP_RULE_RES_DENY;
2550 goto out;
2551}
2552
Christopher Faulet92d34fe2019-12-17 09:20:34 +01002553/* Replace all headers matching the name <name>. The header value is replaced if
2554 * it matches the regex <re>. <str> is used for the replacement. If <full> is
2555 * set to 1, the full-line is matched and replaced. Otherwise, comma-separated
2556 * values are evaluated one by one. It returns 0 on success and -1 on error.
2557 */
2558int http_replace_hdrs(struct stream* s, struct htx *htx, struct ist name,
2559 const char *str, struct my_regex *re, int full)
Christopher Faulet72333522018-10-24 11:25:02 +02002560{
2561 struct http_hdr_ctx ctx;
2562 struct buffer *output = get_trash_chunk();
2563
Christopher Faulet72333522018-10-24 11:25:02 +02002564 ctx.blk = NULL;
Christopher Faulet92d34fe2019-12-17 09:20:34 +01002565 while (http_find_header(htx, name, &ctx, full)) {
Christopher Faulet72333522018-10-24 11:25:02 +02002566 if (!regex_exec_match2(re, ctx.value.ptr, ctx.value.len, MAX_MATCH, pmatch, 0))
2567 continue;
2568
2569 output->data = exp_replace(output->area, output->size, ctx.value.ptr, str, pmatch);
2570 if (output->data == -1)
2571 return -1;
2572 if (!http_replace_header_value(htx, &ctx, ist2(output->area, output->data)))
2573 return -1;
2574 }
2575 return 0;
2576}
2577
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002578/* This function executes one of the set-{method,path,query,uri} actions. It
2579 * takes the string from the variable 'replace' with length 'len', then modifies
2580 * the relevant part of the request line accordingly. Then it updates various
2581 * pointers to the next elements which were moved, and the total buffer length.
2582 * It finds the action to be performed in p[2], previously filled by function
2583 * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal
2584 * error, though this can be revisited when this code is finally exploited.
2585 *
2586 * 'action' can be '0' to replace method, '1' to replace path, '2' to replace
Christopher Faulet312294f2020-09-02 17:17:44 +02002587 * query string, 3 to replace uri or 4 to replace the path+query.
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002588 *
2589 * In query string case, the mark question '?' must be set at the start of the
2590 * string by the caller, event if the replacement query string is empty.
2591 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002592int http_req_replace_stline(int action, const char *replace, int len,
2593 struct proxy *px, struct stream *s)
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002594{
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002595 struct htx *htx = htxbuf(&s->req.buf);
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002596
2597 switch (action) {
2598 case 0: // method
2599 if (!http_replace_req_meth(htx, ist2(replace, len)))
2600 return -1;
2601 break;
2602
2603 case 1: // path
Christopher Fauletb8ce5052020-08-31 16:11:57 +02002604 if (!http_replace_req_path(htx, ist2(replace, len), 0))
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002605 return -1;
2606 break;
2607
2608 case 2: // query
2609 if (!http_replace_req_query(htx, ist2(replace, len)))
2610 return -1;
2611 break;
2612
2613 case 3: // uri
2614 if (!http_replace_req_uri(htx, ist2(replace, len)))
2615 return -1;
2616 break;
2617
Christopher Faulet312294f2020-09-02 17:17:44 +02002618 case 4: // path + query
2619 if (!http_replace_req_path(htx, ist2(replace, len), 1))
2620 return -1;
2621 break;
2622
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002623 default:
2624 return -1;
2625 }
2626 return 0;
2627}
2628
2629/* This function replace the HTTP status code and the associated message. The
Christopher Faulete00d06c2019-12-16 17:18:42 +01002630 * variable <status> contains the new status code. This function never fails. It
2631 * returns 0 in case of success, -1 in case of internal error.
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002632 */
Christopher Faulet96bff762019-12-17 13:46:18 +01002633int http_res_set_status(unsigned int status, struct ist reason, struct stream *s)
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002634{
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002635 struct htx *htx = htxbuf(&s->res.buf);
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002636 char *res;
2637
2638 chunk_reset(&trash);
2639 res = ultoa_o(status, trash.area, trash.size);
2640 trash.data = res - trash.area;
2641
2642 /* Do we have a custom reason format string? */
Tim Duesterhuse296d3e2020-03-05 17:56:31 +01002643 if (!isttest(reason)) {
Christopher Faulet96bff762019-12-17 13:46:18 +01002644 const char *str = http_get_reason(status);
Tim Duesterhusdcf753a2021-03-04 17:31:47 +01002645 reason = ist(str);
Christopher Faulet96bff762019-12-17 13:46:18 +01002646 }
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002647
Christopher Fauletbde2c4c2020-08-31 16:43:34 +02002648 if (!http_replace_res_status(htx, ist2(trash.area, trash.data), reason))
Christopher Faulete00d06c2019-12-16 17:18:42 +01002649 return -1;
Willy Tarreau640e2532023-01-10 14:50:44 +01002650 s->txn->status = status;
Christopher Faulete00d06c2019-12-16 17:18:42 +01002651 return 0;
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002652}
2653
Christopher Faulet3e964192018-10-24 11:39:23 +02002654/* Executes the http-request rules <rules> for stream <s>, proxy <px> and
2655 * transaction <txn>. Returns the verdict of the first rule that prevents
2656 * further processing of the request (auth, deny, ...), and defaults to
2657 * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or
2658 * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT
2659 * on txn->flags if it encounters a tarpit rule. If <deny_status> is not NULL
2660 * and a deny/tarpit rule is matched, it will be filled with this rule's deny
2661 * status.
2662 */
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002663static enum rule_result http_req_get_intercept_rule(struct proxy *px, struct list *def_rules,
2664 struct list *rules, struct stream *s)
Christopher Faulet3e964192018-10-24 11:39:23 +02002665{
2666 struct session *sess = strm_sess(s);
2667 struct http_txn *txn = s->txn;
Christopher Faulet3e964192018-10-24 11:39:23 +02002668 struct act_rule *rule;
Christopher Faulet3e964192018-10-24 11:39:23 +02002669 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002670 int act_opts = 0;
Christopher Faulet3e964192018-10-24 11:39:23 +02002671
Christopher Faulet3e964192018-10-24 11:39:23 +02002672 /* If "the current_rule_list" match the executed rule list, we are in
2673 * resume condition. If a resume is needed it is always in the action
2674 * and never in the ACL or converters. In this case, we initialise the
2675 * current rule, and go to the action execution point.
2676 */
2677 if (s->current_rule) {
2678 rule = s->current_rule;
2679 s->current_rule = NULL;
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002680 if (s->current_rule_list == rules || (def_rules && s->current_rule_list == def_rules))
Christopher Faulet3e964192018-10-24 11:39:23 +02002681 goto resume_execution;
2682 }
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002683 s->current_rule_list = ((!def_rules || s->current_rule_list == def_rules) ? rules : def_rules);
Christopher Faulet3e964192018-10-24 11:39:23 +02002684
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002685 restart:
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002686 /* start the ruleset evaluation in strict mode */
2687 txn->req.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01002688
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002689 list_for_each_entry(rule, s->current_rule_list, list) {
Christopher Faulet3e964192018-10-24 11:39:23 +02002690 /* check optional condition */
2691 if (rule->cond) {
2692 int ret;
2693
2694 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
2695 ret = acl_pass(ret);
2696
2697 if (rule->cond->pol == ACL_COND_UNLESS)
2698 ret = !ret;
2699
2700 if (!ret) /* condition not matched */
2701 continue;
2702 }
2703
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002704 act_opts |= ACT_OPT_FIRST;
Christopher Faulet3e964192018-10-24 11:39:23 +02002705 resume_execution:
Amaury Denoyelle03517732021-05-07 14:25:01 +02002706 if (rule->kw->flags & KWF_EXPERIMENTAL)
2707 mark_tainted(TAINTED_ACTION_EXP_EXECUTED);
2708
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002709 /* Always call the action function if defined */
2710 if (rule->action_ptr) {
2711 if ((s->req.flags & CF_READ_ERROR) ||
Christopher Faulet446d8032022-12-12 07:53:18 +01002712 ((s->req.flags & CF_SHUTR) &&
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002713 (px->options & PR_O_ABRT_CLOSE)))
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002714 act_opts |= ACT_OPT_FINAL;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002715
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002716 switch (rule->action_ptr(rule, px, sess, s, act_opts)) {
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002717 case ACT_RET_CONT:
2718 break;
2719 case ACT_RET_STOP:
2720 rule_ret = HTTP_RULE_RES_STOP;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002721 s->last_rule_file = rule->conf.file;
2722 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002723 goto end;
2724 case ACT_RET_YIELD:
2725 s->current_rule = rule;
2726 rule_ret = HTTP_RULE_RES_YIELD;
2727 goto end;
2728 case ACT_RET_ERR:
2729 rule_ret = HTTP_RULE_RES_ERROR;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002730 s->last_rule_file = rule->conf.file;
2731 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002732 goto end;
2733 case ACT_RET_DONE:
2734 rule_ret = HTTP_RULE_RES_DONE;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002735 s->last_rule_file = rule->conf.file;
2736 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002737 goto end;
2738 case ACT_RET_DENY:
Christopher Fauletb58f62b2020-01-13 16:40:13 +01002739 if (txn->status == -1)
2740 txn->status = 403;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002741 rule_ret = HTTP_RULE_RES_DENY;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002742 s->last_rule_file = rule->conf.file;
2743 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002744 goto end;
2745 case ACT_RET_ABRT:
2746 rule_ret = HTTP_RULE_RES_ABRT;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002747 s->last_rule_file = rule->conf.file;
2748 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002749 goto end;
2750 case ACT_RET_INV:
2751 rule_ret = HTTP_RULE_RES_BADREQ;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002752 s->last_rule_file = rule->conf.file;
2753 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002754 goto end;
2755 }
2756 continue; /* eval the next rule */
2757 }
2758
2759 /* If not action function defined, check for known actions */
Christopher Faulet3e964192018-10-24 11:39:23 +02002760 switch (rule->action) {
2761 case ACT_ACTION_ALLOW:
2762 rule_ret = HTTP_RULE_RES_STOP;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002763 s->last_rule_file = rule->conf.file;
2764 s->last_rule_line = rule->conf.line;
Christopher Faulet3e964192018-10-24 11:39:23 +02002765 goto end;
2766
2767 case ACT_ACTION_DENY:
Christopher Faulet5cb513a2020-05-13 17:56:56 +02002768 txn->status = rule->arg.http_reply->status;
2769 txn->http_reply = rule->arg.http_reply;
Christopher Faulet3e964192018-10-24 11:39:23 +02002770 rule_ret = HTTP_RULE_RES_DENY;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002771 s->last_rule_file = rule->conf.file;
2772 s->last_rule_line = rule->conf.line;
Christopher Faulet3e964192018-10-24 11:39:23 +02002773 goto end;
2774
2775 case ACT_HTTP_REQ_TARPIT:
2776 txn->flags |= TX_CLTARPIT;
Christopher Faulet5cb513a2020-05-13 17:56:56 +02002777 txn->status = rule->arg.http_reply->status;
2778 txn->http_reply = rule->arg.http_reply;
Christopher Faulet3e964192018-10-24 11:39:23 +02002779 rule_ret = HTTP_RULE_RES_DENY;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002780 s->last_rule_file = rule->conf.file;
2781 s->last_rule_line = rule->conf.line;
Christopher Faulet3e964192018-10-24 11:39:23 +02002782 goto end;
2783
Willy Tarreaubc1223b2021-09-02 16:54:33 +02002784 case ACT_HTTP_REDIR: {
2785 int ret = http_apply_redirect_rule(rule->arg.redir, s, txn);
2786
2787 if (ret == 2) // 2 == skip
2788 break;
2789
2790 rule_ret = ret ? HTTP_RULE_RES_ABRT : HTTP_RULE_RES_ERROR;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002791 s->last_rule_file = rule->conf.file;
2792 s->last_rule_line = rule->conf.line;
Christopher Faulet3e964192018-10-24 11:39:23 +02002793 goto end;
Willy Tarreaubc1223b2021-09-02 16:54:33 +02002794 }
Christopher Faulet3e964192018-10-24 11:39:23 +02002795
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002796 /* other flags exists, but normally, they never be matched. */
Christopher Faulet3e964192018-10-24 11:39:23 +02002797 default:
2798 break;
2799 }
2800 }
2801
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002802 if (def_rules && s->current_rule_list == def_rules) {
2803 s->current_rule_list = rules;
2804 goto restart;
2805 }
2806
Christopher Faulet3e964192018-10-24 11:39:23 +02002807 end:
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002808 /* if the ruleset evaluation is finished reset the strict mode */
Christopher Faulet46f95542019-12-20 10:07:22 +01002809 if (rule_ret != HTTP_RULE_RES_YIELD)
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002810 txn->req.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01002811
Christopher Faulet3e964192018-10-24 11:39:23 +02002812 /* we reached the end of the rules, nothing to report */
2813 return rule_ret;
2814}
2815
2816/* Executes the http-response rules <rules> for stream <s> and proxy <px>. It
2817 * returns one of 5 possible statuses: HTTP_RULE_RES_CONT, HTTP_RULE_RES_STOP,
2818 * HTTP_RULE_RES_DONE, HTTP_RULE_RES_YIELD, or HTTP_RULE_RES_BADREQ. If *CONT
2819 * is returned, the process can continue the evaluation of next rule list. If
2820 * *STOP or *DONE is returned, the process must stop the evaluation. If *BADREQ
2821 * is returned, it means the operation could not be processed and a server error
Christopher Fauleta53abad2020-05-13 08:12:22 +02002822 * must be returned. If *YIELD is returned, the caller must call again the
2823 * function with the same context.
Christopher Faulet3e964192018-10-24 11:39:23 +02002824 */
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002825static enum rule_result http_res_get_intercept_rule(struct proxy *px, struct list *def_rules,
2826 struct list *rules, struct stream *s)
Christopher Faulet3e964192018-10-24 11:39:23 +02002827{
2828 struct session *sess = strm_sess(s);
2829 struct http_txn *txn = s->txn;
Christopher Faulet3e964192018-10-24 11:39:23 +02002830 struct act_rule *rule;
Christopher Faulet3e964192018-10-24 11:39:23 +02002831 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002832 int act_opts = 0;
Christopher Faulet3e964192018-10-24 11:39:23 +02002833
Christopher Faulet3e964192018-10-24 11:39:23 +02002834 /* If "the current_rule_list" match the executed rule list, we are in
2835 * resume condition. If a resume is needed it is always in the action
2836 * and never in the ACL or converters. In this case, we initialise the
2837 * current rule, and go to the action execution point.
2838 */
2839 if (s->current_rule) {
2840 rule = s->current_rule;
2841 s->current_rule = NULL;
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002842 if (s->current_rule_list == rules || (def_rules && s->current_rule_list == def_rules))
Christopher Faulet3e964192018-10-24 11:39:23 +02002843 goto resume_execution;
2844 }
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002845 s->current_rule_list = ((!def_rules || s->current_rule_list == def_rules) ? rules : def_rules);
2846
2847 restart:
Christopher Faulet3e964192018-10-24 11:39:23 +02002848
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002849 /* start the ruleset evaluation in strict mode */
2850 txn->rsp.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01002851
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002852 list_for_each_entry(rule, s->current_rule_list, list) {
Christopher Faulet3e964192018-10-24 11:39:23 +02002853 /* check optional condition */
2854 if (rule->cond) {
2855 int ret;
2856
2857 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
2858 ret = acl_pass(ret);
2859
2860 if (rule->cond->pol == ACL_COND_UNLESS)
2861 ret = !ret;
2862
2863 if (!ret) /* condition not matched */
2864 continue;
2865 }
2866
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002867 act_opts |= ACT_OPT_FIRST;
Christopher Faulet3e964192018-10-24 11:39:23 +02002868resume_execution:
Amaury Denoyelle03517732021-05-07 14:25:01 +02002869 if (rule->kw->flags & KWF_EXPERIMENTAL)
2870 mark_tainted(TAINTED_ACTION_EXP_EXECUTED);
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002871
2872 /* Always call the action function if defined */
2873 if (rule->action_ptr) {
2874 if ((s->req.flags & CF_READ_ERROR) ||
Christopher Faulet446d8032022-12-12 07:53:18 +01002875 ((s->req.flags & CF_SHUTR) &&
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002876 (px->options & PR_O_ABRT_CLOSE)))
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002877 act_opts |= ACT_OPT_FINAL;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002878
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002879 switch (rule->action_ptr(rule, px, sess, s, act_opts)) {
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002880 case ACT_RET_CONT:
2881 break;
2882 case ACT_RET_STOP:
2883 rule_ret = HTTP_RULE_RES_STOP;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002884 s->last_rule_file = rule->conf.file;
2885 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002886 goto end;
2887 case ACT_RET_YIELD:
2888 s->current_rule = rule;
2889 rule_ret = HTTP_RULE_RES_YIELD;
2890 goto end;
2891 case ACT_RET_ERR:
2892 rule_ret = HTTP_RULE_RES_ERROR;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002893 s->last_rule_file = rule->conf.file;
2894 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002895 goto end;
2896 case ACT_RET_DONE:
2897 rule_ret = HTTP_RULE_RES_DONE;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002898 s->last_rule_file = rule->conf.file;
2899 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002900 goto end;
2901 case ACT_RET_DENY:
Christopher Fauletb58f62b2020-01-13 16:40:13 +01002902 if (txn->status == -1)
2903 txn->status = 502;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002904 rule_ret = HTTP_RULE_RES_DENY;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002905 s->last_rule_file = rule->conf.file;
2906 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002907 goto end;
2908 case ACT_RET_ABRT:
2909 rule_ret = HTTP_RULE_RES_ABRT;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002910 s->last_rule_file = rule->conf.file;
2911 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002912 goto end;
2913 case ACT_RET_INV:
2914 rule_ret = HTTP_RULE_RES_BADREQ;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002915 s->last_rule_file = rule->conf.file;
2916 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002917 goto end;
2918 }
2919 continue; /* eval the next rule */
2920 }
2921
2922 /* If not action function defined, check for known actions */
Christopher Faulet3e964192018-10-24 11:39:23 +02002923 switch (rule->action) {
2924 case ACT_ACTION_ALLOW:
2925 rule_ret = HTTP_RULE_RES_STOP; /* "allow" rules are OK */
Willy Tarreauc6dae862022-03-09 17:23:10 +01002926 s->last_rule_file = rule->conf.file;
2927 s->last_rule_line = rule->conf.line;
Christopher Faulet3e964192018-10-24 11:39:23 +02002928 goto end;
2929
2930 case ACT_ACTION_DENY:
Christopher Faulet5cb513a2020-05-13 17:56:56 +02002931 txn->status = rule->arg.http_reply->status;
2932 txn->http_reply = rule->arg.http_reply;
Christopher Faulet3a26bee2019-12-16 12:47:40 +01002933 rule_ret = HTTP_RULE_RES_DENY;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002934 s->last_rule_file = rule->conf.file;
2935 s->last_rule_line = rule->conf.line;
Christopher Faulet3e964192018-10-24 11:39:23 +02002936 goto end;
2937
Willy Tarreaubc1223b2021-09-02 16:54:33 +02002938 case ACT_HTTP_REDIR: {
2939 int ret = http_apply_redirect_rule(rule->arg.redir, s, txn);
Christopher Faulet3e964192018-10-24 11:39:23 +02002940
Willy Tarreaubc1223b2021-09-02 16:54:33 +02002941 if (ret == 2) // 2 == skip
2942 break;
2943
2944 rule_ret = ret ? HTTP_RULE_RES_ABRT : HTTP_RULE_RES_ERROR;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002945 s->last_rule_file = rule->conf.file;
2946 s->last_rule_line = rule->conf.line;
Willy Tarreaubc1223b2021-09-02 16:54:33 +02002947 goto end;
2948 }
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002949 /* other flags exists, but normally, they never be matched. */
Christopher Faulet3e964192018-10-24 11:39:23 +02002950 default:
2951 break;
2952 }
2953 }
2954
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002955 if (def_rules && s->current_rule_list == def_rules) {
2956 s->current_rule_list = rules;
2957 goto restart;
2958 }
2959
Christopher Faulet3e964192018-10-24 11:39:23 +02002960 end:
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002961 /* if the ruleset evaluation is finished reset the strict mode */
Christopher Faulet46f95542019-12-20 10:07:22 +01002962 if (rule_ret != HTTP_RULE_RES_YIELD)
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002963 txn->rsp.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01002964
Christopher Faulet3e964192018-10-24 11:39:23 +02002965 /* we reached the end of the rules, nothing to report */
2966 return rule_ret;
2967}
2968
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002969/* Executes backend and frontend http-after-response rules for the stream <s>,
2970 * in that order. it return 1 on success and 0 on error. It is the caller
2971 * responsibility to catch error or ignore it. If it catches it, this function
2972 * may be called a second time, for the internal error.
2973 */
2974int http_eval_after_res_rules(struct stream *s)
2975{
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002976 struct list *def_rules, *rules;
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002977 struct session *sess = s->sess;
2978 enum rule_result ret = HTTP_RULE_RES_CONT;
2979
Christopher Faulet507479b2020-05-15 12:29:46 +02002980 /* Eval after-response ruleset only if the reply is not const */
2981 if (s->txn->flags & TX_CONST_REPLY)
2982 goto end;
2983
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002984 /* prune the request variables if not already done and swap to the response variables. */
2985 if (s->vars_reqres.scope != SCOPE_RES) {
2986 if (!LIST_ISEMPTY(&s->vars_reqres.head))
2987 vars_prune(&s->vars_reqres, s->sess, s);
Willy Tarreaub7bfcb32021-08-31 08:13:25 +02002988 vars_init_head(&s->vars_reqres, SCOPE_RES);
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002989 }
2990
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002991 def_rules = (s->be->defpx ? &s->be->defpx->http_after_res_rules : NULL);
2992 rules = &s->be->http_after_res_rules;
2993
2994 ret = http_res_get_intercept_rule(s->be, def_rules, rules, s);
Christopher Faulet4c5a5912021-11-09 17:48:39 +01002995 if ((ret == HTTP_RULE_RES_CONT || ret == HTTP_RULE_RES_STOP) && sess->fe != s->be) {
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002996 def_rules = ((sess->fe->defpx && sess->fe->defpx != s->be->defpx) ? &sess->fe->defpx->http_after_res_rules : NULL);
2997 rules = &sess->fe->http_after_res_rules;
2998 ret = http_res_get_intercept_rule(sess->fe, def_rules, rules, s);
2999 }
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01003000
Christopher Faulet507479b2020-05-15 12:29:46 +02003001 end:
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01003002 /* All other codes than CONTINUE, STOP or DONE are forbidden */
3003 return (ret == HTTP_RULE_RES_CONT || ret == HTTP_RULE_RES_STOP || ret == HTTP_RULE_RES_DONE);
3004}
3005
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003006/*
3007 * Manage client-side cookie. It can impact performance by about 2% so it is
3008 * desirable to call it only when needed. This code is quite complex because
3009 * of the multiple very crappy and ambiguous syntaxes we have to support. it
3010 * highly recommended not to touch this part without a good reason !
3011 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003012static void http_manage_client_side_cookies(struct stream *s, struct channel *req)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003013{
3014 struct session *sess = s->sess;
3015 struct http_txn *txn = s->txn;
3016 struct htx *htx;
3017 struct http_hdr_ctx ctx;
3018 char *hdr_beg, *hdr_end, *del_from;
3019 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
3020 int preserve_hdr;
3021
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003022 htx = htxbuf(&req->buf);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003023 ctx.blk = NULL;
3024 while (http_find_header(htx, ist("Cookie"), &ctx, 1)) {
Olivier Houchardf0f42382019-07-22 17:43:46 +02003025 int is_first = 1;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003026 del_from = NULL; /* nothing to be deleted */
3027 preserve_hdr = 0; /* assume we may kill the whole header */
3028
3029 /* Now look for cookies. Conforming to RFC2109, we have to support
3030 * attributes whose name begin with a '$', and associate them with
3031 * the right cookie, if we want to delete this cookie.
3032 * So there are 3 cases for each cookie read :
3033 * 1) it's a special attribute, beginning with a '$' : ignore it.
3034 * 2) it's a server id cookie that we *MAY* want to delete : save
3035 * some pointers on it (last semi-colon, beginning of cookie...)
3036 * 3) it's an application cookie : we *MAY* have to delete a previous
3037 * "special" cookie.
3038 * At the end of loop, if a "special" cookie remains, we may have to
3039 * remove it. If no application cookie persists in the header, we
3040 * *MUST* delete it.
3041 *
3042 * Note: RFC2965 is unclear about the processing of spaces around
3043 * the equal sign in the ATTR=VALUE form. A careful inspection of
3044 * the RFC explicitly allows spaces before it, and not within the
3045 * tokens (attrs or values). An inspection of RFC2109 allows that
3046 * too but section 10.1.3 lets one think that spaces may be allowed
3047 * after the equal sign too, resulting in some (rare) buggy
3048 * implementations trying to do that. So let's do what servers do.
3049 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
3050 * allowed quoted strings in values, with any possible character
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003051 * after a backslash, including control chars and delimiters, which
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003052 * causes parsing to become ambiguous. Browsers also allow spaces
3053 * within values even without quotes.
3054 *
3055 * We have to keep multiple pointers in order to support cookie
3056 * removal at the beginning, middle or end of header without
3057 * corrupting the header. All of these headers are valid :
3058 *
3059 * hdr_beg hdr_end
3060 * | |
3061 * v |
3062 * NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3 |
3063 * NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3 v
3064 * NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3
3065 * | | | | | | |
3066 * | | | | | | |
3067 * | | | | | | +--> next
3068 * | | | | | +----> val_end
3069 * | | | | +-----------> val_beg
3070 * | | | +--------------> equal
3071 * | | +----------------> att_end
3072 * | +---------------------> att_beg
3073 * +--------------------------> prev
3074 *
3075 */
3076 hdr_beg = ctx.value.ptr;
3077 hdr_end = hdr_beg + ctx.value.len;
3078 for (prev = hdr_beg; prev < hdr_end; prev = next) {
3079 /* Iterate through all cookies on this line */
3080
3081 /* find att_beg */
3082 att_beg = prev;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003083 if (!is_first)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003084 att_beg++;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003085 is_first = 0;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003086
3087 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
3088 att_beg++;
3089
3090 /* find att_end : this is the first character after the last non
3091 * space before the equal. It may be equal to hdr_end.
3092 */
3093 equal = att_end = att_beg;
3094 while (equal < hdr_end) {
3095 if (*equal == '=' || *equal == ',' || *equal == ';')
3096 break;
3097 if (HTTP_IS_SPHT(*equal++))
3098 continue;
3099 att_end = equal;
3100 }
3101
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003102 /* here, <equal> points to '=', a delimiter or the end. <att_end>
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003103 * is between <att_beg> and <equal>, both may be identical.
3104 */
3105 /* look for end of cookie if there is an equal sign */
3106 if (equal < hdr_end && *equal == '=') {
3107 /* look for the beginning of the value */
3108 val_beg = equal + 1;
3109 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
3110 val_beg++;
3111
3112 /* find the end of the value, respecting quotes */
3113 next = http_find_cookie_value_end(val_beg, hdr_end);
3114
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003115 /* make val_end point to the first white space or delimiter after the value */
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003116 val_end = next;
3117 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
3118 val_end--;
3119 }
3120 else
3121 val_beg = val_end = next = equal;
3122
3123 /* We have nothing to do with attributes beginning with
3124 * '$'. However, they will automatically be removed if a
3125 * header before them is removed, since they're supposed
3126 * to be linked together.
3127 */
3128 if (*att_beg == '$')
3129 continue;
3130
3131 /* Ignore cookies with no equal sign */
3132 if (equal == next) {
3133 /* This is not our cookie, so we must preserve it. But if we already
3134 * scheduled another cookie for removal, we cannot remove the
3135 * complete header, but we can remove the previous block itself.
3136 */
3137 preserve_hdr = 1;
3138 if (del_from != NULL) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003139 int delta = http_del_hdr_value(hdr_beg, hdr_end, &del_from, prev);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003140 val_end += delta;
3141 next += delta;
3142 hdr_end += delta;
3143 prev = del_from;
3144 del_from = NULL;
3145 }
3146 continue;
3147 }
3148
3149 /* if there are spaces around the equal sign, we need to
3150 * strip them otherwise we'll get trouble for cookie captures,
3151 * or even for rewrites. Since this happens extremely rarely,
3152 * it does not hurt performance.
3153 */
3154 if (unlikely(att_end != equal || val_beg > equal + 1)) {
3155 int stripped_before = 0;
3156 int stripped_after = 0;
3157
3158 if (att_end != equal) {
3159 memmove(att_end, equal, hdr_end - equal);
3160 stripped_before = (att_end - equal);
3161 equal += stripped_before;
3162 val_beg += stripped_before;
3163 }
3164
3165 if (val_beg > equal + 1) {
3166 memmove(equal + 1, val_beg, hdr_end + stripped_before - val_beg);
3167 stripped_after = (equal + 1) - val_beg;
3168 val_beg += stripped_after;
3169 stripped_before += stripped_after;
3170 }
3171
3172 val_end += stripped_before;
3173 next += stripped_before;
3174 hdr_end += stripped_before;
3175 }
3176 /* now everything is as on the diagram above */
3177
3178 /* First, let's see if we want to capture this cookie. We check
3179 * that we don't already have a client side cookie, because we
3180 * can only capture one. Also as an optimisation, we ignore
3181 * cookies shorter than the declared name.
3182 */
3183 if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL &&
3184 (val_end - att_beg >= sess->fe->capture_namelen) &&
3185 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
3186 int log_len = val_end - att_beg;
3187
3188 if ((txn->cli_cookie = pool_alloc(pool_head_capture)) == NULL) {
3189 ha_alert("HTTP logging : out of memory.\n");
3190 } else {
3191 if (log_len > sess->fe->capture_len)
3192 log_len = sess->fe->capture_len;
3193 memcpy(txn->cli_cookie, att_beg, log_len);
3194 txn->cli_cookie[log_len] = 0;
3195 }
3196 }
3197
3198 /* Persistence cookies in passive, rewrite or insert mode have the
3199 * following form :
3200 *
3201 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
3202 *
3203 * For cookies in prefix mode, the form is :
3204 *
3205 * Cookie: NAME=SRV~VALUE
3206 */
3207 if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
3208 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
3209 struct server *srv = s->be->srv;
3210 char *delim;
3211
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003212 /* if we're in cookie prefix mode, we'll search the delimiter so that we
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003213 * have the server ID between val_beg and delim, and the original cookie between
3214 * delim+1 and val_end. Otherwise, delim==val_end :
3215 *
3216 * hdr_beg
3217 * |
3218 * v
3219 * NAME=SRV; # in all but prefix modes
3220 * NAME=SRV~OPAQUE ; # in prefix mode
3221 * || || | |+-> next
3222 * || || | +--> val_end
3223 * || || +---------> delim
3224 * || |+------------> val_beg
3225 * || +-------------> att_end = equal
3226 * |+-----------------> att_beg
3227 * +------------------> prev
3228 *
3229 */
3230 if (s->be->ck_opts & PR_CK_PFX) {
3231 for (delim = val_beg; delim < val_end; delim++)
3232 if (*delim == COOKIE_DELIM)
3233 break;
3234 }
3235 else {
3236 char *vbar1;
3237 delim = val_end;
3238 /* Now check if the cookie contains a date field, which would
3239 * appear after a vertical bar ('|') just after the server name
3240 * and before the delimiter.
3241 */
3242 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
3243 if (vbar1) {
3244 /* OK, so left of the bar is the server's cookie and
3245 * right is the last seen date. It is a base64 encoded
3246 * 30-bit value representing the UNIX date since the
3247 * epoch in 4-second quantities.
3248 */
3249 int val;
3250 delim = vbar1++;
3251 if (val_end - vbar1 >= 5) {
3252 val = b64tos30(vbar1);
3253 if (val > 0)
3254 txn->cookie_last_date = val << 2;
3255 }
3256 /* look for a second vertical bar */
3257 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
3258 if (vbar1 && (val_end - vbar1 > 5)) {
3259 val = b64tos30(vbar1 + 1);
3260 if (val > 0)
3261 txn->cookie_first_date = val << 2;
3262 }
3263 }
3264 }
3265
3266 /* if the cookie has an expiration date and the proxy wants to check
3267 * it, then we do that now. We first check if the cookie is too old,
3268 * then only if it has expired. We detect strict overflow because the
3269 * time resolution here is not great (4 seconds). Cookies with dates
3270 * in the future are ignored if their offset is beyond one day. This
3271 * allows an admin to fix timezone issues without expiring everyone
3272 * and at the same time avoids keeping unwanted side effects for too
3273 * long.
3274 */
3275 if (txn->cookie_first_date && s->be->cookie_maxlife &&
3276 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) ||
3277 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
3278 txn->flags &= ~TX_CK_MASK;
3279 txn->flags |= TX_CK_OLD;
3280 delim = val_beg; // let's pretend we have not found the cookie
3281 txn->cookie_first_date = 0;
3282 txn->cookie_last_date = 0;
3283 }
3284 else if (txn->cookie_last_date && s->be->cookie_maxidle &&
3285 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) ||
3286 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
3287 txn->flags &= ~TX_CK_MASK;
3288 txn->flags |= TX_CK_EXPIRED;
3289 delim = val_beg; // let's pretend we have not found the cookie
3290 txn->cookie_first_date = 0;
3291 txn->cookie_last_date = 0;
3292 }
3293
3294 /* Here, we'll look for the first running server which supports the cookie.
3295 * This allows to share a same cookie between several servers, for example
3296 * to dedicate backup servers to specific servers only.
3297 * However, to prevent clients from sticking to cookie-less backup server
3298 * when they have incidentely learned an empty cookie, we simply ignore
3299 * empty cookies and mark them as invalid.
3300 * The same behaviour is applied when persistence must be ignored.
3301 */
3302 if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
3303 srv = NULL;
3304
3305 while (srv) {
3306 if (srv->cookie && (srv->cklen == delim - val_beg) &&
3307 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
3308 if ((srv->cur_state != SRV_ST_STOPPED) ||
3309 (s->be->options & PR_O_PERSIST) ||
3310 (s->flags & SF_FORCE_PRST)) {
3311 /* we found the server and we can use it */
3312 txn->flags &= ~TX_CK_MASK;
3313 txn->flags |= (srv->cur_state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
3314 s->flags |= SF_DIRECT | SF_ASSIGNED;
3315 s->target = &srv->obj_type;
3316 break;
3317 } else {
3318 /* we found a server, but it's down,
3319 * mark it as such and go on in case
3320 * another one is available.
3321 */
3322 txn->flags &= ~TX_CK_MASK;
3323 txn->flags |= TX_CK_DOWN;
3324 }
3325 }
3326 srv = srv->next;
3327 }
3328
3329 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
3330 /* no server matched this cookie or we deliberately skipped it */
3331 txn->flags &= ~TX_CK_MASK;
3332 if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
3333 txn->flags |= TX_CK_UNUSED;
3334 else
3335 txn->flags |= TX_CK_INVALID;
3336 }
3337
3338 /* depending on the cookie mode, we may have to either :
3339 * - delete the complete cookie if we're in insert+indirect mode, so that
3340 * the server never sees it ;
3341 * - remove the server id from the cookie value, and tag the cookie as an
Joseph Herlante9d5c722018-11-25 11:00:25 -08003342 * application cookie so that it does not get accidentally removed later,
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003343 * if we're in cookie prefix mode
3344 */
3345 if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
3346 int delta; /* negative */
3347
3348 memmove(val_beg, delim + 1, hdr_end - (delim + 1));
3349 delta = val_beg - (delim + 1);
3350 val_end += delta;
3351 next += delta;
3352 hdr_end += delta;
3353 del_from = NULL;
3354 preserve_hdr = 1; /* we want to keep this cookie */
3355 }
3356 else if (del_from == NULL &&
3357 (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
3358 del_from = prev;
3359 }
3360 }
3361 else {
3362 /* This is not our cookie, so we must preserve it. But if we already
3363 * scheduled another cookie for removal, we cannot remove the
3364 * complete header, but we can remove the previous block itself.
3365 */
3366 preserve_hdr = 1;
3367
3368 if (del_from != NULL) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003369 int delta = http_del_hdr_value(hdr_beg, hdr_end, &del_from, prev);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003370 if (att_beg >= del_from)
3371 att_beg += delta;
3372 if (att_end >= del_from)
3373 att_end += delta;
3374 val_beg += delta;
3375 val_end += delta;
3376 next += delta;
3377 hdr_end += delta;
3378 prev = del_from;
3379 del_from = NULL;
3380 }
3381 }
3382
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003383 } /* for each cookie */
3384
3385
3386 /* There are no more cookies on this line.
3387 * We may still have one (or several) marked for deletion at the
3388 * end of the line. We must do this now in two ways :
3389 * - if some cookies must be preserved, we only delete from the
3390 * mark to the end of line ;
3391 * - if nothing needs to be preserved, simply delete the whole header
3392 */
3393 if (del_from) {
3394 hdr_end = (preserve_hdr ? del_from : hdr_beg);
3395 }
3396 if ((hdr_end - hdr_beg) != ctx.value.len) {
Christopher Faulet3e2638e2019-06-18 09:49:16 +02003397 if (hdr_beg != hdr_end)
3398 htx_change_blk_value_len(htx, ctx.blk, hdr_end - hdr_beg);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003399 else
3400 http_remove_header(htx, &ctx);
3401 }
3402 } /* for each "Cookie header */
3403}
3404
3405/*
3406 * Manage server-side cookies. It can impact performance by about 2% so it is
3407 * desirable to call it only when needed. This function is also used when we
3408 * just need to know if there is a cookie (eg: for check-cache).
3409 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003410static void http_manage_server_side_cookies(struct stream *s, struct channel *res)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003411{
3412 struct session *sess = s->sess;
3413 struct http_txn *txn = s->txn;
3414 struct htx *htx;
3415 struct http_hdr_ctx ctx;
3416 struct server *srv;
3417 char *hdr_beg, *hdr_end;
3418 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003419
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003420 htx = htxbuf(&res->buf);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003421
3422 ctx.blk = NULL;
Willy Tarreauaa1909e2022-11-14 18:58:35 +01003423 while (http_find_header(htx, ist("Set-Cookie"), &ctx, 1)) {
Olivier Houchardf0f42382019-07-22 17:43:46 +02003424 int is_first = 1;
3425
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003426 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
3427 * <prev> points to the colon.
3428 */
3429 txn->flags |= TX_SCK_PRESENT;
3430
3431 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
3432 * check-cache is enabled) and we are not interested in checking
3433 * them. Warning, the cookie capture is declared in the frontend.
3434 */
3435 if (s->be->cookie_name == NULL && sess->fe->capture_name == NULL)
3436 break;
3437
3438 /* OK so now we know we have to process this response cookie.
3439 * The format of the Set-Cookie header is slightly different
3440 * from the format of the Cookie header in that it does not
3441 * support the comma as a cookie delimiter (thus the header
3442 * cannot be folded) because the Expires attribute described in
3443 * the original Netscape's spec may contain an unquoted date
3444 * with a comma inside. We have to live with this because
3445 * many browsers don't support Max-Age and some browsers don't
3446 * support quoted strings. However the Set-Cookie2 header is
Willy Tarreauaa1909e2022-11-14 18:58:35 +01003447 * clean but basically nobody supports it.
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003448 *
3449 * We have to keep multiple pointers in order to support cookie
3450 * removal at the beginning, middle or end of header without
3451 * corrupting the header (in case of set-cookie2). A special
3452 * pointer, <scav> points to the beginning of the set-cookie-av
3453 * fields after the first semi-colon. The <next> pointer points
3454 * either to the end of line (set-cookie) or next unquoted comma
3455 * (set-cookie2). All of these headers are valid :
3456 *
3457 * hdr_beg hdr_end
3458 * | |
3459 * v |
3460 * NAME1 = VALUE 1 ; Secure; Path="/" |
3461 * NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT v
3462 * NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT
3463 * NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard
3464 * | | | | | | | |
3465 * | | | | | | | +-> next
3466 * | | | | | | +------------> scav
3467 * | | | | | +--------------> val_end
3468 * | | | | +--------------------> val_beg
3469 * | | | +----------------------> equal
3470 * | | +------------------------> att_end
3471 * | +----------------------------> att_beg
3472 * +------------------------------> prev
3473 * -------------------------------> hdr_beg
3474 */
3475 hdr_beg = ctx.value.ptr;
3476 hdr_end = hdr_beg + ctx.value.len;
3477 for (prev = hdr_beg; prev < hdr_end; prev = next) {
3478
3479 /* Iterate through all cookies on this line */
3480
3481 /* find att_beg */
3482 att_beg = prev;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003483 if (!is_first)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003484 att_beg++;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003485 is_first = 0;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003486
3487 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
3488 att_beg++;
3489
3490 /* find att_end : this is the first character after the last non
3491 * space before the equal. It may be equal to hdr_end.
3492 */
3493 equal = att_end = att_beg;
3494
3495 while (equal < hdr_end) {
Willy Tarreauaa1909e2022-11-14 18:58:35 +01003496 if (*equal == '=' || *equal == ';')
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003497 break;
3498 if (HTTP_IS_SPHT(*equal++))
3499 continue;
3500 att_end = equal;
3501 }
3502
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003503 /* here, <equal> points to '=', a delimiter or the end. <att_end>
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003504 * is between <att_beg> and <equal>, both may be identical.
3505 */
3506
3507 /* look for end of cookie if there is an equal sign */
3508 if (equal < hdr_end && *equal == '=') {
3509 /* look for the beginning of the value */
3510 val_beg = equal + 1;
3511 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
3512 val_beg++;
3513
3514 /* find the end of the value, respecting quotes */
3515 next = http_find_cookie_value_end(val_beg, hdr_end);
3516
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003517 /* make val_end point to the first white space or delimiter after the value */
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003518 val_end = next;
3519 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
3520 val_end--;
3521 }
3522 else {
3523 /* <equal> points to next comma, semi-colon or EOL */
3524 val_beg = val_end = next = equal;
3525 }
3526
3527 if (next < hdr_end) {
Willy Tarreauaa1909e2022-11-14 18:58:35 +01003528 /* For Set-Cookie, since commas are permitted
3529 * in values, skip to the end.
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003530 */
Willy Tarreauaa1909e2022-11-14 18:58:35 +01003531 next = hdr_end;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003532 }
3533
3534 /* Now everything is as on the diagram above */
3535
3536 /* Ignore cookies with no equal sign */
3537 if (equal == val_end)
3538 continue;
3539
3540 /* If there are spaces around the equal sign, we need to
3541 * strip them otherwise we'll get trouble for cookie captures,
3542 * or even for rewrites. Since this happens extremely rarely,
3543 * it does not hurt performance.
3544 */
3545 if (unlikely(att_end != equal || val_beg > equal + 1)) {
3546 int stripped_before = 0;
3547 int stripped_after = 0;
3548
3549 if (att_end != equal) {
3550 memmove(att_end, equal, hdr_end - equal);
3551 stripped_before = (att_end - equal);
3552 equal += stripped_before;
3553 val_beg += stripped_before;
3554 }
3555
3556 if (val_beg > equal + 1) {
3557 memmove(equal + 1, val_beg, hdr_end + stripped_before - val_beg);
3558 stripped_after = (equal + 1) - val_beg;
3559 val_beg += stripped_after;
3560 stripped_before += stripped_after;
3561 }
3562
3563 val_end += stripped_before;
3564 next += stripped_before;
3565 hdr_end += stripped_before;
3566
Christopher Faulet3e2638e2019-06-18 09:49:16 +02003567 htx_change_blk_value_len(htx, ctx.blk, hdr_end - hdr_beg);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003568 ctx.value.len = hdr_end - hdr_beg;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003569 }
3570
3571 /* First, let's see if we want to capture this cookie. We check
3572 * that we don't already have a server side cookie, because we
3573 * can only capture one. Also as an optimisation, we ignore
3574 * cookies shorter than the declared name.
3575 */
3576 if (sess->fe->capture_name != NULL &&
3577 txn->srv_cookie == NULL &&
3578 (val_end - att_beg >= sess->fe->capture_namelen) &&
3579 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
3580 int log_len = val_end - att_beg;
3581 if ((txn->srv_cookie = pool_alloc(pool_head_capture)) == NULL) {
3582 ha_alert("HTTP logging : out of memory.\n");
3583 }
3584 else {
3585 if (log_len > sess->fe->capture_len)
3586 log_len = sess->fe->capture_len;
3587 memcpy(txn->srv_cookie, att_beg, log_len);
3588 txn->srv_cookie[log_len] = 0;
3589 }
3590 }
3591
3592 srv = objt_server(s->target);
3593 /* now check if we need to process it for persistence */
3594 if (!(s->flags & SF_IGNORE_PRST) &&
3595 (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
3596 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
3597 /* assume passive cookie by default */
3598 txn->flags &= ~TX_SCK_MASK;
3599 txn->flags |= TX_SCK_FOUND;
3600
3601 /* If the cookie is in insert mode on a known server, we'll delete
3602 * this occurrence because we'll insert another one later.
3603 * We'll delete it too if the "indirect" option is set and we're in
3604 * a direct access.
3605 */
3606 if (s->be->ck_opts & PR_CK_PSV) {
3607 /* The "preserve" flag was set, we don't want to touch the
3608 * server's cookie.
3609 */
3610 }
3611 else if ((srv && (s->be->ck_opts & PR_CK_INS)) ||
3612 ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) {
3613 /* this cookie must be deleted */
3614 if (prev == hdr_beg && next == hdr_end) {
3615 /* whole header */
3616 http_remove_header(htx, &ctx);
3617 /* note: while both invalid now, <next> and <hdr_end>
3618 * are still equal, so the for() will stop as expected.
3619 */
3620 } else {
3621 /* just remove the value */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003622 int delta = http_del_hdr_value(hdr_beg, hdr_end, &prev, next);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003623 next = prev;
3624 hdr_end += delta;
3625 }
3626 txn->flags &= ~TX_SCK_MASK;
3627 txn->flags |= TX_SCK_DELETED;
3628 /* and go on with next cookie */
3629 }
3630 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) {
3631 /* replace bytes val_beg->val_end with the cookie name associated
3632 * with this server since we know it.
3633 */
3634 int sliding, delta;
3635
3636 ctx.value = ist2(val_beg, val_end - val_beg);
3637 ctx.lws_before = ctx.lws_after = 0;
3638 http_replace_header_value(htx, &ctx, ist2(srv->cookie, srv->cklen));
3639 delta = srv->cklen - (val_end - val_beg);
3640 sliding = (ctx.value.ptr - val_beg);
3641 hdr_beg += sliding;
3642 val_beg += sliding;
3643 next += sliding + delta;
3644 hdr_end += sliding + delta;
3645
3646 txn->flags &= ~TX_SCK_MASK;
3647 txn->flags |= TX_SCK_REPLACED;
3648 }
3649 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) {
3650 /* insert the cookie name associated with this server
3651 * before existing cookie, and insert a delimiter between them..
3652 */
3653 int sliding, delta;
3654 ctx.value = ist2(val_beg, 0);
3655 ctx.lws_before = ctx.lws_after = 0;
3656 http_replace_header_value(htx, &ctx, ist2(srv->cookie, srv->cklen + 1));
3657 delta = srv->cklen + 1;
3658 sliding = (ctx.value.ptr - val_beg);
3659 hdr_beg += sliding;
3660 val_beg += sliding;
3661 next += sliding + delta;
3662 hdr_end += sliding + delta;
3663
3664 val_beg[srv->cklen] = COOKIE_DELIM;
3665 txn->flags &= ~TX_SCK_MASK;
3666 txn->flags |= TX_SCK_REPLACED;
3667 }
3668 }
3669 /* that's done for this cookie, check the next one on the same
Willy Tarreauaa1909e2022-11-14 18:58:35 +01003670 * line when next != hdr_end (which should normally not happen
3671 * with set-cookie2 support removed).
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003672 */
3673 }
3674 }
3675}
3676
Christopher Faulet25a02f62018-10-24 12:00:25 +02003677/*
3678 * Parses the Cache-Control and Pragma request header fields to determine if
3679 * the request may be served from the cache and/or if it is cacheable. Updates
3680 * s->txn->flags.
3681 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003682void http_check_request_for_cacheability(struct stream *s, struct channel *req)
Christopher Faulet25a02f62018-10-24 12:00:25 +02003683{
3684 struct http_txn *txn = s->txn;
3685 struct htx *htx;
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003686 struct http_hdr_ctx ctx = { .blk = NULL };
3687 int pragma_found, cc_found;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003688
3689 if ((txn->flags & (TX_CACHEABLE|TX_CACHE_IGNORE)) == TX_CACHE_IGNORE)
3690 return; /* nothing more to do here */
3691
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003692 htx = htxbuf(&req->buf);
Christopher Faulet25a02f62018-10-24 12:00:25 +02003693 pragma_found = cc_found = 0;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003694
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003695 /* Check "pragma" header for HTTP/1.0 compatibility. */
3696 if (http_find_header(htx, ist("pragma"), &ctx, 1)) {
3697 if (isteqi(ctx.value, ist("no-cache"))) {
3698 pragma_found = 1;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003699 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003700 }
Christopher Faulet25a02f62018-10-24 12:00:25 +02003701
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003702 ctx.blk = NULL;
3703 /* Don't use the cache and don't try to store if we found the
3704 * Authorization header */
3705 if (http_find_header(htx, ist("authorization"), &ctx, 1)) {
3706 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3707 txn->flags |= TX_CACHE_IGNORE;
3708 }
Christopher Faulet25a02f62018-10-24 12:00:25 +02003709
Christopher Faulet25a02f62018-10-24 12:00:25 +02003710
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003711 /* Look for "cache-control" header and iterate over all the values
3712 * until we find one that specifies that caching is possible or not. */
3713 ctx.blk = NULL;
3714 while (http_find_header(htx, ist("cache-control"), &ctx, 0)) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003715 cc_found = 1;
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003716 /* We don't check the values after max-age, max-stale nor min-fresh,
3717 * we simply don't use the cache when they're specified. */
3718 if (istmatchi(ctx.value, ist("max-age")) ||
3719 istmatchi(ctx.value, ist("no-cache")) ||
3720 istmatchi(ctx.value, ist("max-stale")) ||
3721 istmatchi(ctx.value, ist("min-fresh"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003722 txn->flags |= TX_CACHE_IGNORE;
3723 continue;
3724 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003725 if (istmatchi(ctx.value, ist("no-store"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003726 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3727 continue;
3728 }
3729 }
3730
3731 /* RFC7234#5.4:
3732 * When the Cache-Control header field is also present and
3733 * understood in a request, Pragma is ignored.
3734 * When the Cache-Control header field is not present in a
3735 * request, caches MUST consider the no-cache request
3736 * pragma-directive as having the same effect as if
3737 * "Cache-Control: no-cache" were present.
3738 */
3739 if (!cc_found && pragma_found)
3740 txn->flags |= TX_CACHE_IGNORE;
3741}
3742
3743/*
3744 * Check if response is cacheable or not. Updates s->txn->flags.
3745 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003746void http_check_response_for_cacheability(struct stream *s, struct channel *res)
Christopher Faulet25a02f62018-10-24 12:00:25 +02003747{
3748 struct http_txn *txn = s->txn;
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003749 struct http_hdr_ctx ctx = { .blk = NULL };
Christopher Faulet25a02f62018-10-24 12:00:25 +02003750 struct htx *htx;
Remi Tricot-Le Bretoncc9bf2e2020-11-12 11:14:41 +01003751 int has_freshness_info = 0;
3752 int has_validator = 0;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003753
3754 if (txn->status < 200) {
3755 /* do not try to cache interim responses! */
3756 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3757 return;
3758 }
3759
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003760 htx = htxbuf(&res->buf);
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003761 /* Check "pragma" header for HTTP/1.0 compatibility. */
3762 if (http_find_header(htx, ist("pragma"), &ctx, 1)) {
3763 if (isteqi(ctx.value, ist("no-cache"))) {
3764 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3765 return;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003766 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003767 }
Christopher Faulet25a02f62018-10-24 12:00:25 +02003768
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003769 /* Look for "cache-control" header and iterate over all the values
3770 * until we find one that specifies that caching is possible or not. */
3771 ctx.blk = NULL;
3772 while (http_find_header(htx, ist("cache-control"), &ctx, 0)) {
3773 if (isteqi(ctx.value, ist("public"))) {
3774 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003775 continue;
3776 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003777 if (isteqi(ctx.value, ist("private")) ||
3778 isteqi(ctx.value, ist("no-cache")) ||
3779 isteqi(ctx.value, ist("no-store")) ||
3780 isteqi(ctx.value, ist("max-age=0")) ||
3781 isteqi(ctx.value, ist("s-maxage=0"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003782 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003783 continue;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003784 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003785 /* We might have a no-cache="set-cookie" form. */
3786 if (istmatchi(ctx.value, ist("no-cache=\"set-cookie"))) {
3787 txn->flags &= ~TX_CACHE_COOK;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003788 continue;
3789 }
Remi Tricot-Le Bretoncc9bf2e2020-11-12 11:14:41 +01003790
3791 if (istmatchi(ctx.value, ist("s-maxage")) ||
3792 istmatchi(ctx.value, ist("max-age"))) {
3793 has_freshness_info = 1;
3794 continue;
3795 }
3796 }
3797
3798 /* If no freshness information could be found in Cache-Control values,
3799 * look for an Expires header. */
3800 if (!has_freshness_info) {
3801 ctx.blk = NULL;
3802 has_freshness_info = http_find_header(htx, ist("expires"), &ctx, 0);
Christopher Faulet25a02f62018-10-24 12:00:25 +02003803 }
Remi Tricot-Le Bretoncc9bf2e2020-11-12 11:14:41 +01003804
3805 /* If no freshness information could be found in Cache-Control or Expires
3806 * values, look for an explicit validator. */
3807 if (!has_freshness_info) {
3808 ctx.blk = NULL;
3809 has_validator = 1;
3810 if (!http_find_header(htx, ist("etag"), &ctx, 0)) {
3811 ctx.blk = NULL;
3812 if (!http_find_header(htx, ist("last-modified"), &ctx, 0))
3813 has_validator = 0;
3814 }
3815 }
3816
3817 /* We won't store an entry that has neither a cache validator nor an
3818 * explicit expiration time, as suggested in RFC 7234#3. */
3819 if (!has_freshness_info && !has_validator)
3820 txn->flags |= TX_CACHE_IGNORE;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003821}
3822
Christopher Faulet377c5a52018-10-24 21:21:30 +02003823/*
3824 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
3825 * for the current backend.
3826 *
3827 * It is assumed that the request is either a HEAD, GET, or POST and that the
3828 * uri_auth field is valid.
3829 *
3830 * Returns 1 if stats should be provided, otherwise 0.
3831 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003832static int http_stats_check_uri(struct stream *s, struct http_txn *txn, struct proxy *backend)
Christopher Faulet377c5a52018-10-24 21:21:30 +02003833{
3834 struct uri_auth *uri_auth = backend->uri_auth;
3835 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003836 struct htx_sl *sl;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003837 struct ist uri;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003838
3839 if (!uri_auth)
3840 return 0;
3841
3842 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
3843 return 0;
3844
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003845 htx = htxbuf(&s->req.buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02003846 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003847 uri = htx_sl_req_uri(sl);
Amaury Denoyellec453f952021-07-06 11:40:12 +02003848 if (*uri_auth->uri_prefix == '/') {
3849 struct http_uri_parser parser = http_uri_parser_init(uri);
3850 uri = http_parse_path(&parser);
3851 }
Christopher Faulet377c5a52018-10-24 21:21:30 +02003852
3853 /* check URI size */
3854 if (uri_auth->uri_len > uri.len)
3855 return 0;
3856
3857 if (memcmp(uri.ptr, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
3858 return 0;
3859
3860 return 1;
3861}
3862
3863/* This function prepares an applet to handle the stats. It can deal with the
3864 * "100-continue" expectation, check that admin rules are met for POST requests,
3865 * and program a response message if something was unexpected. It cannot fail
3866 * and always relies on the stats applet to complete the job. It does not touch
3867 * analysers nor counters, which are left to the caller. It does not touch
3868 * s->target which is supposed to already point to the stats applet. The caller
3869 * is expected to have already assigned an appctx to the stream.
3870 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003871static int http_handle_stats(struct stream *s, struct channel *req)
Christopher Faulet377c5a52018-10-24 21:21:30 +02003872{
3873 struct stats_admin_rule *stats_admin_rule;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003874 struct session *sess = s->sess;
3875 struct http_txn *txn = s->txn;
3876 struct http_msg *msg = &txn->req;
3877 struct uri_auth *uri_auth = s->be->uri_auth;
3878 const char *h, *lookup, *end;
Willy Tarreau8e7c6e62022-05-18 17:58:02 +02003879 struct appctx *appctx = __sc_appctx(s->scb);
Willy Tarreau91cefca2022-05-03 17:08:29 +02003880 struct show_stat_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx));
Christopher Faulet377c5a52018-10-24 21:21:30 +02003881 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003882 struct htx_sl *sl;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003883
Willy Tarreau41f88522022-05-03 18:39:27 +02003884 appctx->st1 = 0;
Willy Tarreau6ef16482022-05-06 18:07:53 +02003885 ctx->state = STAT_STATE_INIT;
Willy Tarreau91cefca2022-05-03 17:08:29 +02003886 ctx->st_code = STAT_STATUS_INIT;
3887 ctx->flags |= uri_auth->flags;
3888 ctx->flags |= STAT_FMT_HTML; /* assume HTML mode by default */
Christopher Faulet377c5a52018-10-24 21:21:30 +02003889 if ((msg->flags & HTTP_MSGF_VER_11) && (txn->meth != HTTP_METH_HEAD))
Willy Tarreau91cefca2022-05-03 17:08:29 +02003890 ctx->flags |= STAT_CHUNKED;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003891
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003892 htx = htxbuf(&req->buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02003893 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003894 lookup = HTX_SL_REQ_UPTR(sl) + uri_auth->uri_len;
3895 end = HTX_SL_REQ_UPTR(sl) + HTX_SL_REQ_ULEN(sl);
Christopher Faulet377c5a52018-10-24 21:21:30 +02003896
3897 for (h = lookup; h <= end - 3; h++) {
3898 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02003899 ctx->flags |= STAT_HIDE_DOWN;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003900 break;
3901 }
Amaury Denoyelle91e55ea2021-02-25 14:46:08 +01003902 }
3903
3904 for (h = lookup; h <= end - 9; h++) {
3905 if (memcmp(h, ";no-maint", 9) == 0) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02003906 ctx->flags |= STAT_HIDE_MAINT;
Willy Tarreau3e320362020-10-23 17:28:57 +02003907 break;
3908 }
Christopher Faulet377c5a52018-10-24 21:21:30 +02003909 }
3910
3911 if (uri_auth->refresh) {
3912 for (h = lookup; h <= end - 10; h++) {
3913 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02003914 ctx->flags |= STAT_NO_REFRESH;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003915 break;
3916 }
3917 }
3918 }
3919
3920 for (h = lookup; h <= end - 4; h++) {
3921 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02003922 ctx->flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
Christopher Faulet377c5a52018-10-24 21:21:30 +02003923 break;
3924 }
3925 }
3926
3927 for (h = lookup; h <= end - 6; h++) {
3928 if (memcmp(h, ";typed", 6) == 0) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02003929 ctx->flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
3930 ctx->flags |= STAT_FMT_TYPED;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003931 break;
3932 }
3933 }
3934
Christopher Faulet6338a082019-09-09 15:50:54 +02003935 for (h = lookup; h <= end - 5; h++) {
3936 if (memcmp(h, ";json", 5) == 0) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02003937 ctx->flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
3938 ctx->flags |= STAT_FMT_JSON;
Christopher Faulet6338a082019-09-09 15:50:54 +02003939 break;
3940 }
3941 }
3942
3943 for (h = lookup; h <= end - 12; h++) {
3944 if (memcmp(h, ";json-schema", 12) == 0) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02003945 ctx->flags &= ~STAT_FMT_MASK;
3946 ctx->flags |= STAT_JSON_SCHM;
Christopher Faulet6338a082019-09-09 15:50:54 +02003947 break;
3948 }
3949 }
3950
Christopher Faulet377c5a52018-10-24 21:21:30 +02003951 for (h = lookup; h <= end - 8; h++) {
3952 if (memcmp(h, ";st=", 4) == 0) {
3953 int i;
3954 h += 4;
Willy Tarreau91cefca2022-05-03 17:08:29 +02003955 ctx->st_code = STAT_STATUS_UNKN;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003956 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
3957 if (strncmp(stat_status_codes[i], h, 4) == 0) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02003958 ctx->st_code = i;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003959 break;
3960 }
3961 }
3962 break;
3963 }
3964 }
3965
Willy Tarreau91cefca2022-05-03 17:08:29 +02003966 ctx->scope_str = 0;
3967 ctx->scope_len = 0;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003968 for (h = lookup; h <= end - 8; h++) {
3969 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
3970 int itx = 0;
3971 const char *h2;
3972 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
3973 const char *err;
3974
3975 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
3976 h2 = h;
Willy Tarreau91cefca2022-05-03 17:08:29 +02003977 ctx->scope_str = h2 - HTX_SL_REQ_UPTR(sl);
Christopher Fauleted7a0662019-01-14 11:07:34 +01003978 while (h < end) {
Christopher Faulet377c5a52018-10-24 21:21:30 +02003979 if (*h == ';' || *h == '&' || *h == ' ')
3980 break;
3981 itx++;
3982 h++;
3983 }
3984
3985 if (itx > STAT_SCOPE_TXT_MAXLEN)
3986 itx = STAT_SCOPE_TXT_MAXLEN;
Willy Tarreau91cefca2022-05-03 17:08:29 +02003987 ctx->scope_len = itx;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003988
Willy Tarreau91cefca2022-05-03 17:08:29 +02003989 /* scope_txt = search query, ctx->scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Christopher Faulet377c5a52018-10-24 21:21:30 +02003990 memcpy(scope_txt, h2, itx);
3991 scope_txt[itx] = '\0';
3992 err = invalid_char(scope_txt);
3993 if (err) {
3994 /* bad char in search text => clear scope */
Willy Tarreau91cefca2022-05-03 17:08:29 +02003995 ctx->scope_str = 0;
3996 ctx->scope_len = 0;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003997 }
3998 break;
3999 }
4000 }
4001
4002 /* now check whether we have some admin rules for this request */
4003 list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) {
4004 int ret = 1;
4005
4006 if (stats_admin_rule->cond) {
4007 ret = acl_exec_cond(stats_admin_rule->cond, s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
4008 ret = acl_pass(ret);
4009 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
4010 ret = !ret;
4011 }
4012
4013 if (ret) {
4014 /* no rule, or the rule matches */
Willy Tarreau91cefca2022-05-03 17:08:29 +02004015 ctx->flags |= STAT_ADMIN;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004016 break;
4017 }
4018 }
4019
Christopher Faulet5d45e382019-02-27 15:15:23 +01004020 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
4021 appctx->st0 = STAT_HTTP_HEAD;
4022 else if (txn->meth == HTTP_METH_POST) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02004023 if (ctx->flags & STAT_ADMIN) {
Christopher Faulet377c5a52018-10-24 21:21:30 +02004024 appctx->st0 = STAT_HTTP_POST;
Christopher Fauletbd9e8422019-08-15 22:26:48 +02004025 if (msg->msg_state < HTTP_MSG_DATA)
4026 req->analysers |= AN_REQ_HTTP_BODY;
4027 }
Christopher Faulet377c5a52018-10-24 21:21:30 +02004028 else {
Christopher Faulet5d45e382019-02-27 15:15:23 +01004029 /* POST without admin level */
Willy Tarreau91cefca2022-05-03 17:08:29 +02004030 ctx->flags &= ~STAT_CHUNKED;
4031 ctx->st_code = STAT_STATUS_DENY;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004032 appctx->st0 = STAT_HTTP_LAST;
4033 }
4034 }
4035 else {
Christopher Faulet5d45e382019-02-27 15:15:23 +01004036 /* Unsupported method */
Willy Tarreau91cefca2022-05-03 17:08:29 +02004037 ctx->flags &= ~STAT_CHUNKED;
4038 ctx->st_code = STAT_STATUS_IVAL;
Christopher Faulet5d45e382019-02-27 15:15:23 +01004039 appctx->st0 = STAT_HTTP_LAST;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004040 }
4041
4042 s->task->nice = -32; /* small boost for HTTP statistics */
4043 return 1;
4044}
4045
Christopher Faulet021a8e42021-03-29 10:46:38 +02004046/* This function waits for the message payload at most <time> milliseconds (may
4047 * be set to TICK_ETERNITY). It stops to wait if at least <bytes> bytes of the
4048 * payload are received (0 means no limit). It returns HTTP_RULE_* depending on
4049 * the result:
4050 *
4051 * - HTTP_RULE_RES_CONT when conditions are met to stop waiting
4052 * - HTTP_RULE_RES_YIELD to wait for more data
Ilya Shipitsinb2be9a12021-04-24 13:25:42 +05004053 * - HTTP_RULE_RES_ABRT when a timeout occurred.
Christopher Faulet021a8e42021-03-29 10:46:38 +02004054 * - HTTP_RULE_RES_BADREQ if a parsing error is raised by lower level
Ilya Shipitsinb2be9a12021-04-24 13:25:42 +05004055 * - HTTP_RULE_RES_ERROR if an internal error occurred
Christopher Faulet021a8e42021-03-29 10:46:38 +02004056 *
Ilya Shipitsinb2be9a12021-04-24 13:25:42 +05004057 * If a timeout occurred, this function is responsible to emit the right response
Christopher Faulet021a8e42021-03-29 10:46:38 +02004058 * to the client, depending on the channel (408 on request side, 504 on response
4059 * side). All other errors must be handled by the caller.
4060 */
4061enum rule_result http_wait_for_msg_body(struct stream *s, struct channel *chn,
4062 unsigned int time, unsigned int bytes)
4063{
4064 struct session *sess = s->sess;
4065 struct http_txn *txn = s->txn;
4066 struct http_msg *msg = ((chn->flags & CF_ISRESP) ? &txn->rsp : &txn->req);
4067 struct htx *htx;
4068 enum rule_result ret = HTTP_RULE_RES_CONT;
4069
4070 htx = htxbuf(&chn->buf);
4071
4072 if (htx->flags & HTX_FL_PARSING_ERROR) {
4073 ret = HTTP_RULE_RES_BADREQ;
4074 goto end;
4075 }
4076 if (htx->flags & HTX_FL_PROCESSING_ERROR) {
4077 ret = HTTP_RULE_RES_ERROR;
4078 goto end;
4079 }
4080
4081 /* Do nothing for bodyless and CONNECT requests */
4082 if (txn->meth == HTTP_METH_CONNECT || (msg->flags & HTTP_MSGF_BODYLESS))
4083 goto end;
4084
4085 if (!(chn->flags & CF_ISRESP) && msg->msg_state < HTTP_MSG_DATA) {
4086 if (http_handle_expect_hdr(s, htx, msg) == -1) {
4087 ret = HTTP_RULE_RES_ERROR;
4088 goto end;
4089 }
4090 }
4091
4092 msg->msg_state = HTTP_MSG_DATA;
4093
4094 /* Now we're in HTTP_MSG_DATA. We just need to know if all data have
4095 * been received or if the buffer is full.
4096 */
Christopher Faulet78335962021-09-23 14:46:32 +02004097 if ((htx->flags & HTX_FL_EOM) ||
4098 htx_get_tail_type(htx) > HTX_BLK_DATA ||
4099 channel_htx_full(chn, htx, global.tune.maxrewrite) ||
Willy Tarreau99615ed2022-05-25 07:29:36 +02004100 sc_waiting_room(chn_prod(chn)))
Christopher Faulet021a8e42021-03-29 10:46:38 +02004101 goto end;
4102
4103 if (bytes) {
4104 struct htx_blk *blk;
4105 unsigned int len = 0;
4106
4107 for (blk = htx_get_first_blk(htx); blk; blk = htx_get_next_blk(htx, blk)) {
4108 if (htx_get_blk_type(blk) != HTX_BLK_DATA)
4109 continue;
4110 len += htx_get_blksz(blk);
4111 if (len >= bytes)
4112 goto end;
4113 }
4114 }
4115
4116 if ((chn->flags & CF_READ_TIMEOUT) || tick_is_expired(chn->analyse_exp, now_ms)) {
4117 if (!(chn->flags & CF_ISRESP))
4118 goto abort_req;
4119 goto abort_res;
4120 }
4121
4122 /* we get here if we need to wait for more data */
Christopher Faulet4b490b72023-01-04 11:55:15 +01004123 if (!(chn->flags & CF_SHUTR)) {
Christopher Faulet021a8e42021-03-29 10:46:38 +02004124 if (!tick_isset(chn->analyse_exp))
4125 chn->analyse_exp = tick_add_ifset(now_ms, time);
4126 ret = HTTP_RULE_RES_YIELD;
4127 }
4128
4129 end:
4130 return ret;
4131
Christopher Fauletf0d80df2023-01-13 10:20:20 +01004132 abort:
4133 http_reply_and_close(s, txn->status, http_error_message(s));
4134 ret = HTTP_RULE_RES_ABRT;
4135 goto end;
4136
Christopher Faulet021a8e42021-03-29 10:46:38 +02004137 abort_req:
4138 txn->status = 408;
4139 if (!(s->flags & SF_ERR_MASK))
4140 s->flags |= SF_ERR_CLITO;
Willy Tarreau4781b152021-04-06 13:53:36 +02004141 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req);
Christopher Faulet021a8e42021-03-29 10:46:38 +02004142 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02004143 _HA_ATOMIC_INC(&sess->listener->counters->failed_req);
Christopher Fauletf0d80df2023-01-13 10:20:20 +01004144 goto abort;
Christopher Faulet021a8e42021-03-29 10:46:38 +02004145
4146 abort_res:
4147 txn->status = 504;
4148 if (!(s->flags & SF_ERR_MASK))
4149 s->flags |= SF_ERR_SRVTO;
Christopher Faulet021a8e42021-03-29 10:46:38 +02004150 stream_inc_http_fail_ctr(s);
Christopher Fauletf0d80df2023-01-13 10:20:20 +01004151 goto abort;
Christopher Faulet021a8e42021-03-29 10:46:38 +02004152}
4153
Willy Tarreaub49672d2022-05-27 10:13:37 +02004154void http_perform_server_redirect(struct stream *s, struct stconn *sc)
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004155{
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004156 struct channel *req = &s->req;
4157 struct channel *res = &s->res;
4158 struct server *srv;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004159 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004160 struct htx_sl *sl;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004161 struct ist path, location;
4162 unsigned int flags;
Amaury Denoyellec453f952021-07-06 11:40:12 +02004163 struct http_uri_parser parser;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004164
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004165 /*
4166 * Create the location
4167 */
4168 chunk_reset(&trash);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004169
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004170 /* 1: add the server's prefix */
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004171 /* special prefix "/" means don't change URL */
4172 srv = __objt_server(s->target);
4173 if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') {
4174 if (!chunk_memcat(&trash, srv->rdr_pfx, srv->rdr_len))
4175 return;
4176 }
4177
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004178 /* 2: add the request Path */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004179 htx = htxbuf(&req->buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02004180 sl = http_get_stline(htx);
Amaury Denoyellec453f952021-07-06 11:40:12 +02004181 parser = http_uri_parser_init(htx_sl_req_uri(sl));
4182 path = http_parse_path(&parser);
Tim Duesterhused526372020-03-05 17:56:33 +01004183 if (!isttest(path))
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004184 return;
4185
4186 if (!chunk_memcat(&trash, path.ptr, path.len))
4187 return;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004188 location = ist2(trash.area, trash.data);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004189
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004190 /*
Ilya Shipitsin4a689da2022-10-29 09:34:32 +05004191 * Create the 302 response
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004192 */
4193 htx = htx_from_buf(&res->buf);
4194 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
4195 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
4196 ist("HTTP/1.1"), ist("302"), ist("Found"));
4197 if (!sl)
4198 goto fail;
4199 sl->info.res.status = 302;
4200 s->txn->status = 302;
4201
4202 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) ||
4203 !htx_add_header(htx, ist("Connection"), ist("close")) ||
4204 !htx_add_header(htx, ist("Content-length"), ist("0")) ||
4205 !htx_add_header(htx, ist("Location"), location))
4206 goto fail;
4207
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004208 if (!htx_add_endof(htx, HTX_BLK_EOH))
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004209 goto fail;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004210
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004211 htx->flags |= HTX_FL_EOM;
Christopher Fauletc20afb82020-01-24 19:16:26 +01004212 htx_to_buf(htx, &res->buf);
Christopher Fauleta72a7e42020-01-28 09:28:11 +01004213 if (!http_forward_proxy_resp(s, 1))
4214 goto fail;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004215
4216 /* return without error. */
Willy Tarreaub49672d2022-05-27 10:13:37 +02004217 sc_shutr(sc);
4218 sc_shutw(sc);
Christopher Faulet50264b42022-03-30 19:39:30 +02004219 s->conn_err_type = STRM_ET_NONE;
Willy Tarreaub49672d2022-05-27 10:13:37 +02004220 sc->state = SC_ST_CLO;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004221
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004222 if (!(s->flags & SF_ERR_MASK))
4223 s->flags |= SF_ERR_LOCAL;
4224 if (!(s->flags & SF_FINST_MASK))
4225 s->flags |= SF_FINST_C;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004226
4227 /* FIXME: we should increase a counter of redirects per server and per backend. */
4228 srv_inc_sess_ctr(srv);
4229 srv_set_sess_last(srv);
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004230 return;
4231
4232 fail:
4233 /* If an error occurred, remove the incomplete HTTP response from the
4234 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004235 channel_htx_truncate(res, htx);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004236}
4237
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05004238/* This function terminates the request because it was completely analyzed or
Christopher Fauletf2824e62018-10-01 12:12:37 +02004239 * because an error was triggered during the body forwarding.
4240 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004241static void http_end_request(struct stream *s)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004242{
4243 struct channel *chn = &s->req;
4244 struct http_txn *txn = s->txn;
4245
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004246 DBG_TRACE_ENTER(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004247
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004248 if (unlikely(txn->req.msg_state < HTTP_MSG_DONE)) {
4249 DBG_TRACE_DEVEL("waiting end of the request", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004250 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004251 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004252
4253 if (txn->req.msg_state == HTTP_MSG_DONE) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004254 /* No need to read anymore, the request was completely parsed.
4255 * We can shut the read side unless we want to abort_on_close,
4256 * or we have a POST request. The issue with POST requests is
4257 * that some browsers still send a CRLF after the request, and
4258 * this CRLF must be read so that it does not remain in the kernel
4259 * buffers, otherwise a close could cause an RST on some systems
4260 * (eg: Linux).
4261 */
Christopher Faulet769d0e92019-03-22 14:23:18 +01004262 if (!(s->be->options & PR_O_ABRT_CLOSE) && txn->meth != HTTP_METH_POST)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004263 channel_dont_read(chn);
4264
4265 /* if the server closes the connection, we want to immediately react
4266 * and close the socket to save packets and syscalls.
4267 */
Willy Tarreaucb041662022-05-17 19:44:42 +02004268 s->scb->flags |= SC_FL_NOHALF;
Christopher Fauletf2824e62018-10-01 12:12:37 +02004269
4270 /* In any case we've finished parsing the request so we must
4271 * disable Nagle when sending data because 1) we're not going
4272 * to shut this side, and 2) the server is waiting for us to
4273 * send pending data.
4274 */
4275 chn->flags |= CF_NEVER_WAIT;
4276
Christopher Fauletd01ce402019-01-02 17:44:13 +01004277 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
4278 /* The server has not finished to respond, so we
4279 * don't want to move in order not to upset it.
4280 */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004281 DBG_TRACE_DEVEL("waiting end of the response", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletd01ce402019-01-02 17:44:13 +01004282 return;
4283 }
4284
Christopher Fauletf2824e62018-10-01 12:12:37 +02004285 /* When we get here, it means that both the request and the
4286 * response have finished receiving. Depending on the connection
4287 * mode, we'll have to wait for the last bytes to leave in either
4288 * direction, and sometimes for a close to be effective.
4289 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02004290 if (txn->flags & TX_CON_WANT_TUN) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004291 /* Tunnel mode will not have any analyser so it needs to
4292 * poll for reads.
4293 */
4294 channel_auto_read(chn);
4295 txn->req.msg_state = HTTP_MSG_TUNNEL;
4296 }
4297 else {
4298 /* we're not expecting any new data to come for this
4299 * transaction, so we can close it.
Christopher Faulet9768c262018-10-22 09:34:31 +02004300 *
4301 * However, there is an exception if the response
4302 * length is undefined. In this case, we need to wait
4303 * the close from the server. The response will be
4304 * switched in TUNNEL mode until the end.
Christopher Fauletf2824e62018-10-01 12:12:37 +02004305 */
4306 if (!(txn->rsp.flags & HTTP_MSGF_XFER_LEN) &&
4307 txn->rsp.msg_state != HTTP_MSG_CLOSED)
Christopher Faulet9768c262018-10-22 09:34:31 +02004308 goto check_channel_flags;
Christopher Fauletf2824e62018-10-01 12:12:37 +02004309
4310 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4311 channel_shutr_now(chn);
4312 channel_shutw_now(chn);
4313 }
4314 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004315 goto check_channel_flags;
4316 }
4317
4318 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
4319 http_msg_closing:
4320 /* nothing else to forward, just waiting for the output buffer
4321 * to be empty and for the shutw_now to take effect.
4322 */
4323 if (channel_is_empty(chn)) {
4324 txn->req.msg_state = HTTP_MSG_CLOSED;
4325 goto http_msg_closed;
4326 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004327 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004328 return;
4329 }
4330
4331 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
4332 http_msg_closed:
Christopher Fauletf2824e62018-10-01 12:12:37 +02004333 /* if we don't know whether the server will close, we need to hard close */
4334 if (txn->rsp.flags & HTTP_MSGF_XFER_LEN)
Willy Tarreaucb041662022-05-17 19:44:42 +02004335 s->scb->flags |= SC_FL_NOLINGER; /* we want to close ASAP */
Christopher Fauletf2824e62018-10-01 12:12:37 +02004336 /* see above in MSG_DONE why we only do this in these states */
Christopher Faulet769d0e92019-03-22 14:23:18 +01004337 if (!(s->be->options & PR_O_ABRT_CLOSE))
Christopher Fauletf2824e62018-10-01 12:12:37 +02004338 channel_dont_read(chn);
4339 goto end;
4340 }
4341
4342 check_channel_flags:
4343 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4344 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4345 /* if we've just closed an output, let's switch */
4346 txn->req.msg_state = HTTP_MSG_CLOSING;
4347 goto http_msg_closing;
4348 }
4349
4350 end:
4351 chn->analysers &= AN_REQ_FLT_END;
Christopher Faulet198ef8b2020-12-15 13:32:55 +01004352 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
4353 chn->flags |= CF_NEVER_WAIT;
4354 if (HAS_REQ_DATA_FILTERS(s))
Christopher Fauletf2824e62018-10-01 12:12:37 +02004355 chn->analysers |= AN_REQ_FLT_XFER_DATA;
Christopher Faulet198ef8b2020-12-15 13:32:55 +01004356 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004357 channel_auto_close(chn);
4358 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004359 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004360}
4361
4362
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05004363/* This function terminates the response because it was completely analyzed or
Christopher Fauletf2824e62018-10-01 12:12:37 +02004364 * because an error was triggered during the body forwarding.
4365 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004366static void http_end_response(struct stream *s)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004367{
4368 struct channel *chn = &s->res;
4369 struct http_txn *txn = s->txn;
4370
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004371 DBG_TRACE_ENTER(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004372
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004373 if (unlikely(txn->rsp.msg_state < HTTP_MSG_DONE)) {
4374 DBG_TRACE_DEVEL("waiting end of the response", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004375 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004376 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004377
4378 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
4379 /* In theory, we don't need to read anymore, but we must
4380 * still monitor the server connection for a possible close
4381 * while the request is being uploaded, so we don't disable
4382 * reading.
4383 */
4384 /* channel_dont_read(chn); */
4385
4386 if (txn->req.msg_state < HTTP_MSG_DONE) {
4387 /* The client seems to still be sending data, probably
4388 * because we got an error response during an upload.
4389 * We have the choice of either breaking the connection
4390 * or letting it pass through. Let's do the later.
4391 */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004392 DBG_TRACE_DEVEL("waiting end of the request", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004393 return;
4394 }
4395
4396 /* When we get here, it means that both the request and the
4397 * response have finished receiving. Depending on the connection
4398 * mode, we'll have to wait for the last bytes to leave in either
4399 * direction, and sometimes for a close to be effective.
4400 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02004401 if (txn->flags & TX_CON_WANT_TUN) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004402 channel_auto_read(chn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004403 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
4404 }
4405 else {
4406 /* we're not expecting any new data to come for this
4407 * transaction, so we can close it.
4408 */
4409 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4410 channel_shutr_now(chn);
4411 channel_shutw_now(chn);
4412 }
4413 }
4414 goto check_channel_flags;
4415 }
4416
4417 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
4418 http_msg_closing:
4419 /* nothing else to forward, just waiting for the output buffer
4420 * to be empty and for the shutw_now to take effect.
4421 */
4422 if (channel_is_empty(chn)) {
4423 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4424 goto http_msg_closed;
4425 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004426 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004427 return;
4428 }
4429
4430 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
4431 http_msg_closed:
4432 /* drop any pending data */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004433 channel_htx_truncate(&s->req, htxbuf(&s->req.buf));
Christopher Faulet9768c262018-10-22 09:34:31 +02004434 channel_abort(&s->req);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004435 goto end;
4436 }
4437
4438 check_channel_flags:
4439 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4440 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4441 /* if we've just closed an output, let's switch */
4442 txn->rsp.msg_state = HTTP_MSG_CLOSING;
4443 goto http_msg_closing;
4444 }
4445
4446 end:
4447 chn->analysers &= AN_RES_FLT_END;
Christopher Faulet198ef8b2020-12-15 13:32:55 +01004448 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
4449 chn->flags |= CF_NEVER_WAIT;
4450 if (HAS_RSP_DATA_FILTERS(s))
4451 chn->analysers |= AN_RES_FLT_XFER_DATA;
4452 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004453 channel_auto_close(chn);
4454 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004455 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004456}
4457
Christopher Fauletef70e252020-01-28 09:26:19 +01004458/* Forward a response generated by HAProxy (error/redirect/return). This
4459 * function forwards all pending incoming data. If <final> is set to 0, nothing
4460 * more is performed. It is used for 1xx informational messages. Otherwise, the
Christopher Faulet507479b2020-05-15 12:29:46 +02004461 * transaction is terminated and the request is emptied. On success 1 is
Christopher Faulet40e6b552020-06-25 16:04:50 +02004462 * returned. If an error occurred, 0 is returned. If it fails, this function
4463 * only exits. It is the caller responsibility to do the cleanup.
Christopher Fauletef70e252020-01-28 09:26:19 +01004464 */
4465int http_forward_proxy_resp(struct stream *s, int final)
4466{
4467 struct channel *req = &s->req;
4468 struct channel *res = &s->res;
4469 struct htx *htx = htxbuf(&res->buf);
4470 size_t data;
4471
4472 if (final) {
4473 htx->flags |= HTX_FL_PROXY_RESP;
Christopher Faulet507479b2020-05-15 12:29:46 +02004474
Christopher Fauletaab1b672020-11-18 16:44:02 +01004475 if (!htx_is_empty(htx) && !http_eval_after_res_rules(s))
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01004476 return 0;
Christopher Fauletef70e252020-01-28 09:26:19 +01004477
Christopher Fauletd6c48362020-10-19 18:01:38 +02004478 if (s->txn->meth == HTTP_METH_HEAD)
4479 htx_skip_msg_payload(htx);
4480
Christopher Fauletef70e252020-01-28 09:26:19 +01004481 channel_auto_read(req);
4482 channel_abort(req);
4483 channel_auto_close(req);
4484 channel_htx_erase(req, htxbuf(&req->buf));
4485
4486 res->wex = tick_add_ifset(now_ms, res->wto);
4487 channel_auto_read(res);
4488 channel_auto_close(res);
4489 channel_shutr_now(res);
Christopher Faulet1a9db7c2020-06-25 15:36:45 +02004490 res->flags |= CF_EOI; /* The response is terminated, add EOI */
Christopher Faulet42432f32020-11-20 17:43:16 +01004491 htxbuf(&res->buf)->flags |= HTX_FL_EOM; /* no more data are expected */
Christopher Fauletef70e252020-01-28 09:26:19 +01004492 }
Christopher Fauletcf6898c2020-06-25 15:55:11 +02004493 else {
4494 /* Send ASAP informational messages. Rely on CF_EOI for final
4495 * response.
4496 */
4497 res->flags |= CF_SEND_DONTWAIT;
4498 }
Christopher Fauletef70e252020-01-28 09:26:19 +01004499
4500 data = htx->data - co_data(res);
4501 c_adv(res, data);
4502 htx->first = -1;
4503 res->total += data;
4504 return 1;
4505}
4506
Willy Tarreaub49672d2022-05-27 10:13:37 +02004507void http_server_error(struct stream *s, struct stconn *sc, int err,
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004508 int finst, struct http_reply *msg)
Christopher Faulet0f226952018-10-22 09:29:56 +02004509{
Christopher Faulet72c7d8d2020-01-27 15:32:25 +01004510 http_reply_and_close(s, s->txn->status, msg);
Christopher Faulet0f226952018-10-22 09:29:56 +02004511 if (!(s->flags & SF_ERR_MASK))
4512 s->flags |= err;
4513 if (!(s->flags & SF_FINST_MASK))
4514 s->flags |= finst;
4515}
4516
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004517void http_reply_and_close(struct stream *s, short status, struct http_reply *msg)
Christopher Faulet0f226952018-10-22 09:29:56 +02004518{
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004519 if (!msg) {
4520 channel_htx_truncate(&s->res, htxbuf(&s->res.buf));
4521 goto end;
4522 }
4523
4524 if (http_reply_message(s, msg) == -1) {
4525 /* On error, return a 500 error message, but don't rewrite it if
Christopher Faulet40e6b552020-06-25 16:04:50 +02004526 * it is already an internal error. If it was already a "const"
4527 * 500 error, just fail.
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004528 */
Christopher Faulet40e6b552020-06-25 16:04:50 +02004529 if (s->txn->status == 500) {
4530 if (s->txn->flags & TX_CONST_REPLY)
4531 goto end;
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004532 s->txn->flags |= TX_CONST_REPLY;
Christopher Faulet40e6b552020-06-25 16:04:50 +02004533 }
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004534 s->txn->status = 500;
4535 s->txn->http_reply = NULL;
4536 return http_reply_and_close(s, s->txn->status, http_error_message(s));
4537 }
4538
4539end:
4540 s->res.wex = tick_add_ifset(now_ms, s->res.wto);
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004541
Christopher Faulet2d565002021-09-10 09:17:50 +02004542 /* At this staged, HTTP analysis is finished */
4543 s->req.analysers &= AN_REQ_FLT_END;
4544 s->req.analyse_exp = TICK_ETERNITY;
4545
4546 s->res.analysers &= AN_RES_FLT_END;
4547 s->res.analyse_exp = TICK_ETERNITY;
4548
Christopher Faulet0f226952018-10-22 09:29:56 +02004549 channel_auto_read(&s->req);
4550 channel_abort(&s->req);
4551 channel_auto_close(&s->req);
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004552 channel_htx_erase(&s->req, htxbuf(&s->req.buf));
Christopher Faulet72c7d8d2020-01-27 15:32:25 +01004553 channel_auto_read(&s->res);
4554 channel_auto_close(&s->res);
4555 channel_shutr_now(&s->res);
Christopher Faulet0f226952018-10-22 09:29:56 +02004556}
4557
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004558struct http_reply *http_error_message(struct stream *s)
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004559{
4560 const int msgnum = http_get_status_idx(s->txn->status);
4561
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004562 if (s->txn->http_reply)
4563 return s->txn->http_reply;
4564 else if (s->be->replies[msgnum])
4565 return s->be->replies[msgnum];
4566 else if (strm_fe(s)->replies[msgnum])
4567 return strm_fe(s)->replies[msgnum];
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004568 else
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004569 return &http_err_replies[msgnum];
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004570}
4571
Christopher Faulet40e6b552020-06-25 16:04:50 +02004572/* Produces an HTX message from an http reply. Depending on the http reply type,
4573 * a, errorfile, an raw file or a log-format string is used. On success, it
4574 * returns 0. If an error occurs -1 is returned. If it fails, this function only
4575 * exits. It is the caller responsibility to do the cleanup.
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004576 */
Christopher Fauletae43b6c2020-05-27 15:24:22 +02004577int http_reply_to_htx(struct stream *s, struct htx *htx, struct http_reply *reply)
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004578{
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004579 struct buffer *errmsg;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004580 struct htx_sl *sl;
4581 struct buffer *body = NULL;
4582 const char *status, *reason, *clen, *ctype;
4583 unsigned int slflags;
4584 int ret = 0;
4585
Christopher Faulete29a97e2020-05-14 14:49:25 +02004586 /*
4587 * - HTTP_REPLY_ERRFILES unexpected here. handled as no payload if so
4588 *
4589 * - HTTP_REPLY_INDIRECT: switch on another reply if defined or handled
4590 * as no payload if NULL. the TXN status code is set with the status
4591 * of the original reply.
4592 */
4593
4594 if (reply->type == HTTP_REPLY_INDIRECT) {
4595 if (reply->body.reply)
4596 reply = reply->body.reply;
4597 }
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004598 if (reply->type == HTTP_REPLY_ERRMSG && !reply->body.errmsg) {
4599 /* get default error message */
4600 if (reply == s->txn->http_reply)
4601 s->txn->http_reply = NULL;
4602 reply = http_error_message(s);
4603 if (reply->type == HTTP_REPLY_INDIRECT) {
4604 if (reply->body.reply)
4605 reply = reply->body.reply;
4606 }
4607 }
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004608
4609 if (reply->type == HTTP_REPLY_ERRMSG) {
4610 /* implicit or explicit error message*/
4611 errmsg = reply->body.errmsg;
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004612 if (errmsg && !b_is_null(errmsg)) {
Christopher Faulet20567362020-05-15 14:52:49 +02004613 if (!htx_copy_msg(htx, errmsg))
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004614 goto fail;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004615 }
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004616 }
4617 else {
4618 /* no payload, file or log-format string */
4619 if (reply->type == HTTP_REPLY_RAW) {
4620 /* file */
4621 body = &reply->body.obj;
4622 }
4623 else if (reply->type == HTTP_REPLY_LOGFMT) {
4624 /* log-format string */
4625 body = alloc_trash_chunk();
4626 if (!body)
4627 goto fail_alloc;
4628 body->data = build_logline(s, body->area, body->size, &reply->body.fmt);
4629 }
4630 /* else no payload */
4631
4632 status = ultoa(reply->status);
4633 reason = http_get_reason(reply->status);
4634 slflags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_CLEN);
4635 if (!body || !b_data(body))
4636 slflags |= HTX_SL_F_BODYLESS;
4637 sl = htx_add_stline(htx, HTX_BLK_RES_SL, slflags, ist("HTTP/1.1"), ist(status), ist(reason));
4638 if (!sl)
4639 goto fail;
4640 sl->info.res.status = reply->status;
4641
4642 clen = (body ? ultoa(b_data(body)) : "0");
4643 ctype = reply->ctype;
4644
4645 if (!LIST_ISEMPTY(&reply->hdrs)) {
4646 struct http_reply_hdr *hdr;
4647 struct buffer *value = alloc_trash_chunk();
4648
4649 if (!value)
4650 goto fail;
4651
4652 list_for_each_entry(hdr, &reply->hdrs, list) {
4653 chunk_reset(value);
4654 value->data = build_logline(s, value->area, value->size, &hdr->value);
4655 if (b_data(value) && !htx_add_header(htx, hdr->name, ist2(b_head(value), b_data(value)))) {
4656 free_trash_chunk(value);
4657 goto fail;
4658 }
4659 chunk_reset(value);
4660 }
4661 free_trash_chunk(value);
4662 }
4663
4664 if (!htx_add_header(htx, ist("content-length"), ist(clen)) ||
4665 (body && b_data(body) && ctype && !htx_add_header(htx, ist("content-type"), ist(ctype))) ||
4666 !htx_add_endof(htx, HTX_BLK_EOH) ||
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004667 (body && b_data(body) && !htx_add_data_atonce(htx, ist2(b_head(body), b_data(body)))))
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004668 goto fail;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004669
4670 htx->flags |= HTX_FL_EOM;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004671 }
4672
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004673 leave:
4674 if (reply->type == HTTP_REPLY_LOGFMT)
4675 free_trash_chunk(body);
4676 return ret;
4677
4678 fail_alloc:
4679 if (!(s->flags & SF_ERR_MASK))
4680 s->flags |= SF_ERR_RESOURCE;
Christopher Faulet97e466c2020-05-15 15:12:47 +02004681 /* fall through */
4682 fail:
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004683 ret = -1;
4684 goto leave;
Christopher Faulet97e466c2020-05-15 15:12:47 +02004685}
4686
4687/* Send an http reply to the client. On success, it returns 0. If an error
Christopher Faulet40e6b552020-06-25 16:04:50 +02004688 * occurs -1 is returned and the response channel is truncated, removing this
4689 * way the faulty reply. This function may fail when the reply is formatted
4690 * (http_reply_to_htx) or when the reply is forwarded
4691 * (http_forward_proxy_resp). On the last case, it is because a
4692 * http-after-response rule fails.
Christopher Faulet97e466c2020-05-15 15:12:47 +02004693 */
4694int http_reply_message(struct stream *s, struct http_reply *reply)
4695{
4696 struct channel *res = &s->res;
4697 struct htx *htx = htx_from_buf(&res->buf);
4698
4699 if (s->txn->status == -1)
4700 s->txn->status = reply->status;
4701 channel_htx_truncate(res, htx);
4702
4703 if (http_reply_to_htx(s, htx, reply) == -1)
4704 goto fail;
4705
4706 htx_to_buf(htx, &s->res.buf);
4707 if (!http_forward_proxy_resp(s, 1))
4708 goto fail;
4709 return 0;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004710
4711 fail:
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004712 channel_htx_truncate(res, htx);
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004713 if (!(s->flags & SF_ERR_MASK))
4714 s->flags |= SF_ERR_PRXCOND;
Christopher Faulet97e466c2020-05-15 15:12:47 +02004715 return -1;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004716}
4717
Christopher Faulet50264b42022-03-30 19:39:30 +02004718/* Return the error message corresponding to s->conn_err_type. It is assumed
Christopher Faulet304cc402019-07-15 15:46:28 +02004719 * that the server side is closed. Note that err_type is actually a
4720 * bitmask, where almost only aborts may be cumulated with other
4721 * values. We consider that aborted operations are more important
4722 * than timeouts or errors due to the fact that nobody else in the
4723 * logs might explain incomplete retries. All others should avoid
4724 * being cumulated. It should normally not be possible to have multiple
4725 * aborts at once, but just in case, the first one in sequence is reported.
4726 * Note that connection errors appearing on the second request of a keep-alive
4727 * connection are not reported since this allows the client to retry.
4728 */
Willy Tarreaub49672d2022-05-27 10:13:37 +02004729void http_return_srv_error(struct stream *s, struct stconn *sc)
Christopher Faulet304cc402019-07-15 15:46:28 +02004730{
Christopher Faulet50264b42022-03-30 19:39:30 +02004731 int err_type = s->conn_err_type;
Christopher Faulet304cc402019-07-15 15:46:28 +02004732
4733 /* set s->txn->status for http_error_message(s) */
Christopher Faulet50264b42022-03-30 19:39:30 +02004734 if (err_type & STRM_ET_QUEUE_ABRT) {
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004735 s->txn->status = -1;
Willy Tarreaub49672d2022-05-27 10:13:37 +02004736 http_server_error(s, sc, SF_ERR_CLICL, SF_FINST_Q, NULL);
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004737 }
Christopher Faulet50264b42022-03-30 19:39:30 +02004738 else if (err_type & STRM_ET_CONN_ABRT) {
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004739 s->txn->status = -1;
Willy Tarreaub49672d2022-05-27 10:13:37 +02004740 http_server_error(s, sc, SF_ERR_CLICL, SF_FINST_C, NULL);
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004741 }
Christopher Faulet50264b42022-03-30 19:39:30 +02004742 else if (err_type & STRM_ET_QUEUE_TO) {
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004743 s->txn->status = 503;
Willy Tarreaub49672d2022-05-27 10:13:37 +02004744 http_server_error(s, sc, SF_ERR_SRVTO, SF_FINST_Q,
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004745 http_error_message(s));
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004746 }
Christopher Faulet50264b42022-03-30 19:39:30 +02004747 else if (err_type & STRM_ET_QUEUE_ERR) {
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004748 s->txn->status = 503;
Willy Tarreaub49672d2022-05-27 10:13:37 +02004749 http_server_error(s, sc, SF_ERR_SRVCL, SF_FINST_Q,
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004750 http_error_message(s));
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004751 }
Christopher Faulet50264b42022-03-30 19:39:30 +02004752 else if (err_type & STRM_ET_CONN_TO) {
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004753 s->txn->status = 503;
Willy Tarreaub49672d2022-05-27 10:13:37 +02004754 http_server_error(s, sc, SF_ERR_SRVTO, SF_FINST_C,
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004755 (s->txn->flags & TX_NOT_FIRST) ? NULL :
4756 http_error_message(s));
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004757 }
Christopher Faulet50264b42022-03-30 19:39:30 +02004758 else if (err_type & STRM_ET_CONN_ERR) {
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004759 s->txn->status = 503;
Willy Tarreaub49672d2022-05-27 10:13:37 +02004760 http_server_error(s, sc, SF_ERR_SRVCL, SF_FINST_C,
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004761 (s->flags & SF_SRV_REUSED) ? NULL :
4762 http_error_message(s));
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004763 }
Christopher Faulet50264b42022-03-30 19:39:30 +02004764 else if (err_type & STRM_ET_CONN_RES) {
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004765 s->txn->status = 503;
Willy Tarreaub49672d2022-05-27 10:13:37 +02004766 http_server_error(s, sc, SF_ERR_RESOURCE, SF_FINST_C,
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004767 (s->txn->flags & TX_NOT_FIRST) ? NULL :
4768 http_error_message(s));
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004769 }
Christopher Faulet50264b42022-03-30 19:39:30 +02004770 else { /* STRM_ET_CONN_OTHER and others */
Christopher Faulet304cc402019-07-15 15:46:28 +02004771 s->txn->status = 500;
Willy Tarreaub49672d2022-05-27 10:13:37 +02004772 http_server_error(s, sc, SF_ERR_INTERNAL, SF_FINST_C,
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004773 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004774 }
4775}
4776
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004777
Christopher Faulet4a28a532019-03-01 11:19:40 +01004778/* Handle Expect: 100-continue for HTTP/1.1 messages if necessary. It returns 0
4779 * on success and -1 on error.
4780 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004781static int http_handle_expect_hdr(struct stream *s, struct htx *htx, struct http_msg *msg)
Christopher Faulet4a28a532019-03-01 11:19:40 +01004782{
4783 /* If we have HTTP/1.1 message with a body and Expect: 100-continue,
4784 * then we must send an HTTP/1.1 100 Continue intermediate response.
4785 */
4786 if (msg->msg_state == HTTP_MSG_BODY && (msg->flags & HTTP_MSGF_VER_11) &&
4787 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
4788 struct ist hdr = { .ptr = "Expect", .len = 6 };
4789 struct http_hdr_ctx ctx;
4790
4791 ctx.blk = NULL;
4792 /* Expect is allowed in 1.1, look for it */
4793 if (http_find_header(htx, hdr, &ctx, 0) &&
4794 unlikely(isteqi(ctx.value, ist2("100-continue", 12)))) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004795 if (http_reply_100_continue(s) == -1)
Christopher Faulet4a28a532019-03-01 11:19:40 +01004796 return -1;
4797 http_remove_header(htx, &ctx);
4798 }
4799 }
4800 return 0;
4801}
4802
Christopher Faulet23a3c792018-11-28 10:01:23 +01004803/* Send a 100-Continue response to the client. It returns 0 on success and -1
4804 * on error. The response channel is updated accordingly.
4805 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004806static int http_reply_100_continue(struct stream *s)
Christopher Faulet23a3c792018-11-28 10:01:23 +01004807{
4808 struct channel *res = &s->res;
4809 struct htx *htx = htx_from_buf(&res->buf);
4810 struct htx_sl *sl;
4811 unsigned int flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|
4812 HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
Christopher Faulet23a3c792018-11-28 10:01:23 +01004813
4814 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
4815 ist("HTTP/1.1"), ist("100"), ist("Continue"));
4816 if (!sl)
4817 goto fail;
4818 sl->info.res.status = 100;
4819
Christopher Faulet1d5ec092019-06-26 14:23:54 +02004820 if (!htx_add_endof(htx, HTX_BLK_EOH))
Christopher Faulet23a3c792018-11-28 10:01:23 +01004821 goto fail;
4822
Christopher Fauleta72a7e42020-01-28 09:28:11 +01004823 if (!http_forward_proxy_resp(s, 0))
4824 goto fail;
Christopher Faulet23a3c792018-11-28 10:01:23 +01004825 return 0;
4826
4827 fail:
4828 /* If an error occurred, remove the incomplete HTTP response from the
4829 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004830 channel_htx_truncate(res, htx);
Christopher Faulet23a3c792018-11-28 10:01:23 +01004831 return -1;
4832}
4833
Christopher Faulet12c51e22018-11-28 15:59:42 +01004834
Christopher Faulet0f226952018-10-22 09:29:56 +02004835/*
4836 * Capture headers from message <htx> according to header list <cap_hdr>, and
4837 * fill the <cap> pointers appropriately.
4838 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004839static void http_capture_headers(struct htx *htx, char **cap, struct cap_hdr *cap_hdr)
Christopher Faulet0f226952018-10-22 09:29:56 +02004840{
4841 struct cap_hdr *h;
4842 int32_t pos;
4843
Christopher Fauleta3f15502019-05-13 15:27:23 +02004844 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet0f226952018-10-22 09:29:56 +02004845 struct htx_blk *blk = htx_get_blk(htx, pos);
4846 enum htx_blk_type type = htx_get_blk_type(blk);
4847 struct ist n, v;
4848
4849 if (type == HTX_BLK_EOH)
4850 break;
4851 if (type != HTX_BLK_HDR)
4852 continue;
4853
4854 n = htx_get_blk_name(htx, blk);
4855
4856 for (h = cap_hdr; h; h = h->next) {
4857 if (h->namelen && (h->namelen == n.len) &&
4858 (strncasecmp(n.ptr, h->name, h->namelen) == 0)) {
4859 if (cap[h->index] == NULL)
4860 cap[h->index] =
4861 pool_alloc(h->pool);
4862
4863 if (cap[h->index] == NULL) {
4864 ha_alert("HTTP capture : out of memory.\n");
4865 break;
4866 }
4867
4868 v = htx_get_blk_value(htx, blk);
Tim Duesterhus2471f5c2021-11-08 09:05:01 +01004869 v = isttrim(v, h->len);
Christopher Faulet0f226952018-10-22 09:29:56 +02004870
4871 memcpy(cap[h->index], v.ptr, v.len);
4872 cap[h->index][v.len]=0;
4873 }
4874 }
4875 }
4876}
4877
Christopher Faulet0b6bdc52018-10-24 11:05:36 +02004878/* Delete a value in a header between delimiters <from> and <next>. The header
4879 * itself is delimited by <start> and <end> pointers. The number of characters
4880 * displaced is returned, and the pointer to the first delimiter is updated if
4881 * required. The function tries as much as possible to respect the following
4882 * principles :
4883 * - replace <from> delimiter by the <next> one unless <from> points to <start>,
4884 * in which case <next> is simply removed
4885 * - set exactly one space character after the new first delimiter, unless there
4886 * are not enough characters in the block being moved to do so.
4887 * - remove unneeded spaces before the previous delimiter and after the new
4888 * one.
4889 *
4890 * It is the caller's responsibility to ensure that :
4891 * - <from> points to a valid delimiter or <start> ;
4892 * - <next> points to a valid delimiter or <end> ;
4893 * - there are non-space chars before <from>.
4894 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004895static int http_del_hdr_value(char *start, char *end, char **from, char *next)
Christopher Faulet0b6bdc52018-10-24 11:05:36 +02004896{
4897 char *prev = *from;
4898
4899 if (prev == start) {
4900 /* We're removing the first value. eat the semicolon, if <next>
4901 * is lower than <end> */
4902 if (next < end)
4903 next++;
4904
4905 while (next < end && HTTP_IS_SPHT(*next))
4906 next++;
4907 }
4908 else {
4909 /* Remove useless spaces before the old delimiter. */
4910 while (HTTP_IS_SPHT(*(prev-1)))
4911 prev--;
4912 *from = prev;
4913
4914 /* copy the delimiter and if possible a space if we're
4915 * not at the end of the line.
4916 */
4917 if (next < end) {
4918 *prev++ = *next++;
4919 if (prev + 1 < next)
4920 *prev++ = ' ';
4921 while (next < end && HTTP_IS_SPHT(*next))
4922 next++;
4923 }
4924 }
4925 memmove(prev, next, end - next);
4926 return (prev - next);
4927}
4928
Christopher Faulet0f226952018-10-22 09:29:56 +02004929
4930/* Formats the start line of the request (without CRLF) and puts it in <str> and
Joseph Herlantc42c0e92018-11-25 10:43:27 -08004931 * return the written length. The line can be truncated if it exceeds <len>.
Christopher Faulet0f226952018-10-22 09:29:56 +02004932 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004933static size_t http_fmt_req_line(const struct htx_sl *sl, char *str, size_t len)
Christopher Faulet0f226952018-10-22 09:29:56 +02004934{
4935 struct ist dst = ist2(str, 0);
4936
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004937 if (istcat(&dst, htx_sl_req_meth(sl), len) == -1)
Christopher Faulet0f226952018-10-22 09:29:56 +02004938 goto end;
4939 if (dst.len + 1 > len)
4940 goto end;
4941 dst.ptr[dst.len++] = ' ';
4942
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004943 if (istcat(&dst, htx_sl_req_uri(sl), len) == -1)
Christopher Faulet0f226952018-10-22 09:29:56 +02004944 goto end;
4945 if (dst.len + 1 > len)
4946 goto end;
4947 dst.ptr[dst.len++] = ' ';
4948
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004949 istcat(&dst, htx_sl_req_vsn(sl), len);
Christopher Faulet0f226952018-10-22 09:29:56 +02004950 end:
4951 return dst.len;
4952}
4953
4954/*
4955 * Print a debug line with a start line.
4956 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004957static void http_debug_stline(const char *dir, struct stream *s, const struct htx_sl *sl)
Christopher Faulet0f226952018-10-22 09:29:56 +02004958{
4959 struct session *sess = strm_sess(s);
4960 int max;
4961
4962 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
4963 dir,
Willy Tarreau88bc8002021-12-06 07:01:02 +00004964 objt_conn(sess->origin) ? (unsigned short)__objt_conn(sess->origin)->handle.fd : -1,
Willy Tarreaufd9417b2022-05-18 16:23:22 +02004965 sc_conn(s->scb) ? (unsigned short)(__sc_conn(s->scb))->handle.fd : -1);
Christopher Faulet0f226952018-10-22 09:29:56 +02004966
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004967 max = HTX_SL_P1_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02004968 UBOUND(max, trash.size - trash.data - 3);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004969 chunk_memcat(&trash, HTX_SL_P1_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02004970 trash.area[trash.data++] = ' ';
4971
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004972 max = HTX_SL_P2_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02004973 UBOUND(max, trash.size - trash.data - 2);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004974 chunk_memcat(&trash, HTX_SL_P2_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02004975 trash.area[trash.data++] = ' ';
4976
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004977 max = HTX_SL_P3_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02004978 UBOUND(max, trash.size - trash.data - 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004979 chunk_memcat(&trash, HTX_SL_P3_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02004980 trash.area[trash.data++] = '\n';
4981
Willy Tarreau2e8ab6b2020-03-14 11:03:20 +01004982 DISGUISE(write(1, trash.area, trash.data));
Christopher Faulet0f226952018-10-22 09:29:56 +02004983}
4984
4985/*
4986 * Print a debug line with a header.
4987 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004988static 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 +02004989{
4990 struct session *sess = strm_sess(s);
4991 int max;
4992
4993 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
4994 dir,
Willy Tarreau88bc8002021-12-06 07:01:02 +00004995 objt_conn(sess->origin) ? (unsigned short)__objt_conn(sess->origin)->handle.fd : -1,
Willy Tarreaufd9417b2022-05-18 16:23:22 +02004996 sc_conn(s->scb) ? (unsigned short)(__sc_conn(s->scb))->handle.fd : -1);
Christopher Faulet0f226952018-10-22 09:29:56 +02004997
4998 max = n.len;
4999 UBOUND(max, trash.size - trash.data - 3);
5000 chunk_memcat(&trash, n.ptr, max);
5001 trash.area[trash.data++] = ':';
5002 trash.area[trash.data++] = ' ';
5003
5004 max = v.len;
5005 UBOUND(max, trash.size - trash.data - 1);
5006 chunk_memcat(&trash, v.ptr, max);
5007 trash.area[trash.data++] = '\n';
5008
Willy Tarreau2e8ab6b2020-03-14 11:03:20 +01005009 DISGUISE(write(1, trash.area, trash.data));
Christopher Faulet0f226952018-10-22 09:29:56 +02005010}
5011
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005012void http_txn_reset_req(struct http_txn *txn)
5013{
Christopher Faulet1aea50e2020-01-17 16:03:53 +01005014 txn->req.flags = 0;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005015 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
5016}
5017
5018void http_txn_reset_res(struct http_txn *txn)
5019{
Christopher Faulet1aea50e2020-01-17 16:03:53 +01005020 txn->rsp.flags = 0;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005021 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
5022}
5023
5024/*
Christopher Faulet75f619a2021-03-08 19:12:58 +01005025 * Create and initialize a new HTTP transaction for stream <s>. This should be
5026 * used before processing any new request. It returns the transaction or NLULL
5027 * on error.
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005028 */
Christopher Faulet75f619a2021-03-08 19:12:58 +01005029struct http_txn *http_create_txn(struct stream *s)
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005030{
Christopher Faulet75f619a2021-03-08 19:12:58 +01005031 struct http_txn *txn;
Willy Tarreaub49672d2022-05-27 10:13:37 +02005032 struct stconn *sc = s->scf;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005033
Christopher Faulet75f619a2021-03-08 19:12:58 +01005034 txn = pool_alloc(pool_head_http_txn);
5035 if (!txn)
5036 return NULL;
5037 s->txn = txn;
5038
Christopher Faulet5eb67f52022-06-22 17:12:05 +02005039 txn->meth = HTTP_METH_OTHER;
Willy Tarreaub49672d2022-05-27 10:13:37 +02005040 txn->flags = ((sc && sc_ep_test(sc, SE_FL_NOT_FIRST)) ? TX_NOT_FIRST : 0);
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005041 txn->status = -1;
Christopher Faulet5cb513a2020-05-13 17:56:56 +02005042 txn->http_reply = NULL;
Christopher Faulete05bf9e2022-03-29 15:23:40 +02005043 txn->l7_buffer = BUF_NULL;
Willy Tarreau8b507582020-02-25 09:35:07 +01005044 write_u32(txn->cache_hash, 0);
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005045
5046 txn->cookie_first_date = 0;
5047 txn->cookie_last_date = 0;
5048
5049 txn->srv_cookie = NULL;
5050 txn->cli_cookie = NULL;
5051 txn->uri = NULL;
5052
5053 http_txn_reset_req(txn);
5054 http_txn_reset_res(txn);
5055
5056 txn->req.chn = &s->req;
5057 txn->rsp.chn = &s->res;
5058
5059 txn->auth.method = HTTP_AUTH_UNKNOWN;
5060
Aurelien DARRAGON5ad2b642022-11-18 09:17:29 +01005061 /* here we don't want to re-initialize s->vars_txn and s->vars_reqres
5062 * variable lists, because they were already initialized upon stream
5063 * creation in stream_new(), and thus may already contain some variables
5064 */
Christopher Faulet75f619a2021-03-08 19:12:58 +01005065
5066 return txn;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005067}
5068
5069/* to be used at the end of a transaction */
Christopher Faulet75f619a2021-03-08 19:12:58 +01005070void http_destroy_txn(struct stream *s)
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005071{
5072 struct http_txn *txn = s->txn;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005073
5074 /* these ones will have been dynamically allocated */
5075 pool_free(pool_head_requri, txn->uri);
5076 pool_free(pool_head_capture, txn->cli_cookie);
5077 pool_free(pool_head_capture, txn->srv_cookie);
Tim Duesterhusa17e6622020-03-05 20:19:02 +01005078 pool_free(pool_head_uniqueid, s->unique_id.ptr);
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005079
Tim Duesterhusa17e6622020-03-05 20:19:02 +01005080 s->unique_id = IST_NULL;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005081 txn->uri = NULL;
5082 txn->srv_cookie = NULL;
5083 txn->cli_cookie = NULL;
5084
Christopher Faulet59399252019-11-07 14:27:52 +01005085 if (!LIST_ISEMPTY(&s->vars_txn.head))
5086 vars_prune(&s->vars_txn, s->sess, s);
5087 if (!LIST_ISEMPTY(&s->vars_reqres.head))
5088 vars_prune(&s->vars_reqres, s->sess, s);
Christopher Faulet75f619a2021-03-08 19:12:58 +01005089
Christopher Faulete05bf9e2022-03-29 15:23:40 +02005090 b_free(&txn->l7_buffer);
5091
Christopher Faulet75f619a2021-03-08 19:12:58 +01005092 pool_free(pool_head_http_txn, txn);
5093 s->txn = NULL;
Christopher Faulet59399252019-11-07 14:27:52 +01005094}
5095
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005096
Christopher Faulet71236de2023-01-13 09:06:38 +01005097void http_set_term_flags(struct stream *s)
5098{
5099 if (!(s->flags & SF_ERR_MASK))
5100 s->flags |= SF_ERR_PRXCOND;
5101
5102 if (!(s->flags & SF_FINST_MASK)) {
5103 if (s->scb->state == SC_ST_INI) {
5104 /* Before any connection attempt on the server side, we
5105 * are still in the request analysis. Just take case to
5106 * detect tarpit error
5107 */
5108 if (s->req.analysers & AN_REQ_HTTP_TARPIT)
5109 s->flags |= SF_FINST_T;
5110 else
5111 s->flags |= SF_FINST_R;
5112 }
5113 else if (s->scb->state == SC_ST_QUE)
5114 s->flags |= SF_FINST_Q;
5115 else if (sc_state_in(s->scb->state, SC_SB_REQ|SC_SB_TAR|SC_SB_ASS|SC_SB_CON|SC_SB_CER|SC_SB_RDY)) {
5116 if (unlikely(objt_applet(s->target))) {
5117 s->flags |= SF_FINST_R;
5118 }
5119 else
5120 s->flags |= SF_FINST_C;
5121 }
5122 else {
5123 if (s->txn->rsp.msg_state < HTTP_MSG_DATA) {
5124 /* We are still processing the response headers */
5125 s->flags |= SF_FINST_H;
5126 }
5127 // (res >= done) & (res->flags & shutw)
5128 else if (s->txn->rsp.msg_state >= HTTP_MSG_DONE &&
5129 (s->flags & (SF_ERR_CLITO|SF_ERR_CLICL))) {
5130 /* A client error was reported and we are
5131 * transmitting the last block of data
5132 */
5133 s->flags |= SF_FINST_L;
5134 }
5135 else {
5136 /* Otherwise we are in DATA phase on both sides */
5137 s->flags |= SF_FINST_D;
5138 }
5139 }
5140 }
5141}
5142
5143
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005144DECLARE_POOL(pool_head_http_txn, "http_txn", sizeof(struct http_txn));
Christopher Faulet0f226952018-10-22 09:29:56 +02005145
Christopher Fauletf4eb75d2018-10-11 15:55:07 +02005146/*
5147 * Local variables:
5148 * c-indent-level: 8
5149 * c-basic-offset: 8
5150 * End:
5151 */