blob: fe5044e068f2d0dbec79550e5bffacb6f87d1114 [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 /*
Aurelien DARRAGONf9583412022-12-29 15:45:41 +0100681 * add X-Original-To if either the frontend or the backend
Christopher Faulete0768eb2018-10-03 16:38:02 +0200682 * asks for it.
683 */
Aurelien DARRAGONf9583412022-12-29 15:45:41 +0100684 if ((sess->fe->options | s->be->options) & PR_O_HTTP_XOT) {
685 if (unlikely(!http_handle_xot_header(s, req)))
686 goto return_fail_rewrite;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200687 }
688
Christopher Fauletc2ac5e42021-03-08 18:20:09 +0100689 /* Filter the request headers if there are filters attached to the
690 * stream.
691 */
692 if (HAS_FILTERS(s))
693 req->analysers |= AN_REQ_FLT_HTTP_HDRS;
694
Christopher Faulete0768eb2018-10-03 16:38:02 +0200695 /* If we have no server assigned yet and we're balancing on url_param
696 * with a POST request, we may be interested in checking the body for
697 * that parameter. This will be done in another analyser.
698 */
699 if (!(s->flags & (SF_ASSIGNED|SF_DIRECT)) &&
Willy Tarreau089eaa02019-01-14 15:17:46 +0100700 s->txn->meth == HTTP_METH_POST &&
701 (s->be->lbprm.algo & BE_LB_ALGO) == BE_LB_ALGO_PH) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200702 channel_dont_connect(req);
703 req->analysers |= AN_REQ_HTTP_BODY;
704 }
705
706 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
707 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau1a18b542018-12-11 16:37:42 +0100708
Christopher Faulete0768eb2018-10-03 16:38:02 +0200709 /* We expect some data from the client. Unless we know for sure
710 * we already have a full request, we have to re-enable quick-ack
711 * in case we previously disabled it, otherwise we might cause
712 * the client to delay further data.
713 */
William Lallemand36119de2021-03-08 15:26:48 +0100714 if ((sess->listener && (sess->listener->options & LI_O_NOQUICKACK)) && !(htx->flags & HTX_FL_EOM))
Willy Tarreau1a18b542018-12-11 16:37:42 +0100715 conn_set_quickack(cli_conn, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200716
717 /*************************************************************
718 * OK, that's finished for the headers. We have done what we *
719 * could. Let's switch to the DATA state. *
720 ************************************************************/
721 req->analyse_exp = TICK_ETERNITY;
722 req->analysers &= ~an_bit;
723
724 s->logs.tv_request = now;
725 /* OK let's go on with the BODY now */
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100726 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200727 return 1;
728
Christopher Fauletd649b572022-06-01 17:42:35 +0200729 return_fail_rewrite:
730 if (!(s->flags & SF_ERR_MASK))
731 s->flags |= SF_ERR_PRXCOND;
732 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_rewrites);
733 if (s->flags & SF_BE_ASSIGNED)
734 _HA_ATOMIC_INC(&s->be->be_counters.failed_rewrites);
735 if (sess->listener && sess->listener->counters)
736 _HA_ATOMIC_INC(&sess->listener->counters->failed_rewrites);
737 if (objt_server(s->target))
738 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_rewrites);
739 /* fall through */
740
Christopher Fauletb8a53712019-12-16 11:29:38 +0100741 return_int_err:
742 txn->status = 500;
743 if (!(s->flags & SF_ERR_MASK))
744 s->flags |= SF_ERR_INTERNAL;
Willy Tarreau4781b152021-04-06 13:53:36 +0200745 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100746 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau4781b152021-04-06 13:53:36 +0200747 _HA_ATOMIC_INC(&s->be->be_counters.internal_errors);
William Lallemand36119de2021-03-08 15:26:48 +0100748 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200749 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100750
Christopher Fauletb8a53712019-12-16 11:29:38 +0100751 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet4a66c942023-01-13 09:43:21 +0100752 http_set_term_flags(s);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100753
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100754 DBG_TRACE_DEVEL("leaving on error",
755 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200756 return 0;
757}
758
759/* This function is an analyser which processes the HTTP tarpit. It always
760 * returns zero, at the beginning because it prevents any other processing
761 * from occurring, and at the end because it terminates the request.
762 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200763int http_process_tarpit(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200764{
765 struct http_txn *txn = s->txn;
766
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100767 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, &txn->req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200768 /* This connection is being tarpitted. The CLIENT side has
769 * already set the connect expiration date to the right
770 * timeout. We just have to check that the client is still
771 * there and that the timeout has not expired.
772 */
773 channel_dont_connect(req);
Christopher Faulet4b490b72023-01-04 11:55:15 +0100774 if (!(req->flags & CF_SHUTR) &&
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100775 !tick_is_expired(req->analyse_exp, now_ms)) {
Christopher Fauletb0c87f12021-10-29 14:37:07 +0200776 /* Be sure to drain all data from the request channel */
777 channel_htx_erase(req, htxbuf(&req->buf));
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100778 DBG_TRACE_DEVEL("waiting for tarpit timeout expiry",
779 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200780 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100781 }
782
Christopher Faulete0768eb2018-10-03 16:38:02 +0200783
784 /* We will set the queue timer to the time spent, just for
785 * logging purposes. We fake a 500 server error, so that the
786 * attacker will not suspect his connection has been tarpitted.
787 * It will not cause trouble to the logs because we can exclude
788 * the tarpitted connections by filtering on the 'PT' status flags.
789 */
790 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
791
Christopher Faulet8dfeccf2020-05-15 14:16:29 +0200792 http_reply_and_close(s, txn->status, (!(req->flags & CF_READ_ERROR) ? http_error_message(s) : NULL));
Christopher Faulet4a66c942023-01-13 09:43:21 +0100793 http_set_term_flags(s);
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100794
795 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200796 return 0;
797}
798
799/* This function is an analyser which waits for the HTTP request body. It waits
800 * for either the buffer to be full, or the full advertised contents to have
801 * reached the buffer. It must only be called after the standard HTTP request
802 * processing has occurred, because it expects the request to be parsed and will
803 * look for the Expect header. It may send a 100-Continue interim response. It
804 * takes in input any state starting from HTTP_MSG_BODY and leaves with one of
805 * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it
806 * needs to read more data, or 1 once it has completed its analysis.
807 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200808int http_wait_for_request_body(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200809{
810 struct session *sess = s->sess;
811 struct http_txn *txn = s->txn;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200812
Christopher Fauletf4569bb2023-01-13 10:49:31 +0100813 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, &s->txn->req);
Christopher Fauletf76ebe82018-10-24 11:16:22 +0200814
Christopher Fauletf76ebe82018-10-24 11:16:22 +0200815
Christopher Faulet021a8e42021-03-29 10:46:38 +0200816 switch (http_wait_for_msg_body(s, req, s->be->timeout.httpreq, 0)) {
817 case HTTP_RULE_RES_CONT:
818 goto http_end;
819 case HTTP_RULE_RES_YIELD:
820 goto missing_data_or_waiting;
821 case HTTP_RULE_RES_BADREQ:
Willy Tarreau4236f032019-03-05 10:43:32 +0100822 goto return_bad_req;
Christopher Faulet021a8e42021-03-29 10:46:38 +0200823 case HTTP_RULE_RES_ERROR:
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200824 goto return_int_err;
Christopher Faulet021a8e42021-03-29 10:46:38 +0200825 case HTTP_RULE_RES_ABRT:
Christopher Fauletb8a53712019-12-16 11:29:38 +0100826 goto return_prx_cond;
Christopher Faulet021a8e42021-03-29 10:46:38 +0200827 default:
828 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200829 }
830
831 http_end:
832 /* The situation will not evolve, so let's give up on the analysis. */
833 s->logs.tv_request = now; /* update the request timer to reflect full request */
834 req->analysers &= ~an_bit;
835 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100836 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200837 return 1;
838
Christopher Faulet021a8e42021-03-29 10:46:38 +0200839 missing_data_or_waiting:
840 channel_dont_connect(req);
841 DBG_TRACE_DEVEL("waiting for more data",
842 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
843 return 0;
844
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200845 return_int_err:
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200846 txn->status = 500;
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200847 if (!(s->flags & SF_ERR_MASK))
848 s->flags |= SF_ERR_INTERNAL;
Willy Tarreau4781b152021-04-06 13:53:36 +0200849 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100850 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau4781b152021-04-06 13:53:36 +0200851 _HA_ATOMIC_INC(&s->be->be_counters.internal_errors);
William Lallemand36119de2021-03-08 15:26:48 +0100852 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200853 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Faulet021a8e42021-03-29 10:46:38 +0200854 goto return_prx_err;
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200855
Christopher Faulete0768eb2018-10-03 16:38:02 +0200856 return_bad_req: /* let's centralize all bad requests */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200857 txn->status = 400;
Willy Tarreau4781b152021-04-06 13:53:36 +0200858 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req);
William Lallemand36119de2021-03-08 15:26:48 +0100859 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200860 _HA_ATOMIC_INC(&sess->listener->counters->failed_req);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100861 /* fall through */
862
Christopher Faulet021a8e42021-03-29 10:46:38 +0200863 return_prx_err:
Christopher Fauletb8a53712019-12-16 11:29:38 +0100864 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet021a8e42021-03-29 10:46:38 +0200865 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200866
Christopher Faulet021a8e42021-03-29 10:46:38 +0200867 return_prx_cond:
Christopher Fauletf0d80df2023-01-13 10:20:20 +0100868 http_set_term_flags(s);
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200869
Christopher Faulete0768eb2018-10-03 16:38:02 +0200870 req->analysers &= AN_REQ_FLT_END;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100871 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100872 DBG_TRACE_DEVEL("leaving on error",
873 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200874 return 0;
875}
876
877/* This function is an analyser which forwards request body (including chunk
878 * sizes if any). It is called as soon as we must forward, even if we forward
879 * zero byte. The only situation where it must not be called is when we're in
880 * tunnel mode and we want to forward till the close. It's used both to forward
881 * remaining data and to resync after end of body. It expects the msg_state to
882 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
883 * read more data, or 1 once we can go on with next request or end the stream.
884 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
885 * bytes of pending data + the headers if not already done.
886 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200887int http_request_forward_body(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200888{
889 struct session *sess = s->sess;
890 struct http_txn *txn = s->txn;
Christopher Faulet9768c262018-10-22 09:34:31 +0200891 struct http_msg *msg = &txn->req;
892 struct htx *htx;
Christopher Faulet93e02d82019-03-08 14:18:50 +0100893 short status = 0;
Christopher Fauletaed82cf2018-11-30 22:22:32 +0100894 int ret;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200895
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100896 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200897
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100898 htx = htxbuf(&req->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200899
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200900 if (htx->flags & HTX_FL_PARSING_ERROR)
901 goto return_bad_req;
902 if (htx->flags & HTX_FL_PROCESSING_ERROR)
903 goto return_int_err;
904
Christopher Faulete0768eb2018-10-03 16:38:02 +0200905 /* Note that we don't have to send 100-continue back because we don't
906 * need the data to complete our job, and it's up to the server to
907 * decide whether to return 100, 417 or anything else in return of
908 * an "Expect: 100-continue" header.
909 */
Christopher Faulet9768c262018-10-22 09:34:31 +0200910 if (msg->msg_state == HTTP_MSG_BODY)
911 msg->msg_state = HTTP_MSG_DATA;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200912
Christopher Faulete0768eb2018-10-03 16:38:02 +0200913 /* in most states, we should abort in case of early close */
914 channel_auto_close(req);
915
916 if (req->to_forward) {
Christopher Faulet66af0b22019-03-22 14:54:52 +0100917 if (req->to_forward == CHN_INFINITE_FORWARD) {
Christopher Faulet1a3e0272019-11-15 16:31:46 +0100918 if (req->flags & CF_EOI)
919 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet66af0b22019-03-22 14:54:52 +0100920 }
921 else {
922 /* We can't process the buffer's contents yet */
923 req->flags |= CF_WAKE_WRITE;
924 goto missing_data_or_waiting;
925 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200926 }
927
Christopher Faulet1a3e0272019-11-15 16:31:46 +0100928 if (msg->msg_state >= HTTP_MSG_ENDING)
929 goto ending;
930
931 if (txn->meth == HTTP_METH_CONNECT) {
932 msg->msg_state = HTTP_MSG_ENDING;
933 goto ending;
934 }
935
Christopher Fauletaed82cf2018-11-30 22:22:32 +0100936 /* Forward input data. We get it by removing all outgoing data not
937 * forwarded yet from HTX data size. If there are some data filters, we
938 * let them decide the amount of data to forward.
Christopher Faulet9768c262018-10-22 09:34:31 +0200939 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +0100940 if (HAS_REQ_DATA_FILTERS(s)) {
941 ret = flt_http_payload(s, msg, htx->data);
942 if (ret < 0)
943 goto return_bad_req;
Christopher Faulet421e7692019-06-13 11:16:45 +0200944 c_adv(req, ret);
Christopher Fauletaed82cf2018-11-30 22:22:32 +0100945 }
946 else {
Christopher Faulet421e7692019-06-13 11:16:45 +0200947 c_adv(req, htx->data - co_data(req));
Christopher Faulet66af0b22019-03-22 14:54:52 +0100948 if (msg->flags & HTTP_MSGF_XFER_LEN)
949 channel_htx_forward_forever(req, htx);
Christopher Fauletaed82cf2018-11-30 22:22:32 +0100950 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200951
Christopher Faulet1a3e0272019-11-15 16:31:46 +0100952 if (htx->data != co_data(req))
953 goto missing_data_or_waiting;
Christopher Fauletd20fdb02019-06-13 16:43:22 +0200954
Christopher Faulet9768c262018-10-22 09:34:31 +0200955 /* Check if the end-of-message is reached and if so, switch the message
Christopher Fauletd20fdb02019-06-13 16:43:22 +0200956 * in HTTP_MSG_ENDING state. Then if all data was marked to be
957 * forwarded, set the state to HTTP_MSG_DONE.
Christopher Faulet9768c262018-10-22 09:34:31 +0200958 */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +0100959 if (!(htx->flags & HTX_FL_EOM))
Christopher Faulet9768c262018-10-22 09:34:31 +0200960 goto missing_data_or_waiting;
961
Christopher Fauletd20fdb02019-06-13 16:43:22 +0200962 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet9768c262018-10-22 09:34:31 +0200963
Christopher Faulet1a3e0272019-11-15 16:31:46 +0100964 ending:
Christopher Faulet2151cdd2020-07-22 16:34:59 +0200965 req->flags &= ~CF_EXPECT_MORE; /* no more data are expected */
966
Christopher Faulet1a3e0272019-11-15 16:31:46 +0100967 /* other states, ENDING...TUNNEL */
968 if (msg->msg_state >= HTTP_MSG_DONE)
969 goto done;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200970
Christopher Fauletaed82cf2018-11-30 22:22:32 +0100971 if (HAS_REQ_DATA_FILTERS(s)) {
972 ret = flt_http_end(s, msg);
973 if (ret <= 0) {
974 if (!ret)
975 goto missing_data_or_waiting;
976 goto return_bad_req;
977 }
978 }
979
Christopher Faulet1a3e0272019-11-15 16:31:46 +0100980 if (txn->meth == HTTP_METH_CONNECT)
981 msg->msg_state = HTTP_MSG_TUNNEL;
982 else {
983 msg->msg_state = HTTP_MSG_DONE;
984 req->to_forward = 0;
985 }
986
987 done:
988 /* we don't want to forward closes on DONE except in tunnel mode. */
989 if (!(txn->flags & TX_CON_WANT_TUN))
990 channel_dont_close(req);
991
Christopher Fauletf2b02cf2023-01-13 11:02:28 +0100992 if ((req->flags & CF_SHUTW) && co_data(req)) {
993 /* request errors are most likely due to the server aborting the
994 * transfer. */
995 goto return_srv_abort;
996 }
997
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200998 http_end_request(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200999 if (!(req->analysers & an_bit)) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001000 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001001 return 1;
1002 }
1003
1004 /* If "option abortonclose" is set on the backend, we want to monitor
1005 * the client's connection and forward any shutdown notification to the
1006 * server, which will decide whether to close or to go on processing the
1007 * request. We only do that in tunnel mode, and not in other modes since
1008 * it can be abused to exhaust source ports. */
Christopher Faulet769d0e92019-03-22 14:23:18 +01001009 if (s->be->options & PR_O_ABRT_CLOSE) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001010 channel_auto_read(req);
Christopher Faulet446d8032022-12-12 07:53:18 +01001011 if ((req->flags & CF_SHUTR) && !(txn->flags & TX_CON_WANT_TUN))
Willy Tarreaucb041662022-05-17 19:44:42 +02001012 s->scb->flags |= SC_FL_NOLINGER;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001013 channel_auto_close(req);
1014 }
1015 else if (s->txn->meth == HTTP_METH_POST) {
1016 /* POST requests may require to read extra CRLF sent by broken
1017 * browsers and which could cause an RST to be sent upon close
1018 * on some systems (eg: Linux). */
1019 channel_auto_read(req);
1020 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001021 DBG_TRACE_DEVEL("waiting for the end of the HTTP txn",
1022 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001023 return 0;
1024
1025 missing_data_or_waiting:
1026 /* stop waiting for data if the input is closed before the end */
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001027 if (msg->msg_state < HTTP_MSG_ENDING && req->flags & CF_SHUTR)
Christopher Faulet93e02d82019-03-08 14:18:50 +01001028 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001029
1030 waiting:
1031 /* waiting for the last bits to leave the buffer */
1032 if (req->flags & CF_SHUTW)
Christopher Faulet93e02d82019-03-08 14:18:50 +01001033 goto return_srv_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001034
1035 /* When TE: chunked is used, we need to get there again to parse remaining
1036 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
1037 * And when content-length is used, we never want to let the possible
1038 * shutdown be forwarded to the other side, as the state machine will
1039 * take care of it once the client responds. It's also important to
1040 * prevent TIME_WAITs from accumulating on the backend side, and for
1041 * HTTP/2 where the last frame comes with a shutdown.
1042 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001043 if (msg->flags & HTTP_MSGF_XFER_LEN)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001044 channel_dont_close(req);
1045
1046 /* We know that more data are expected, but we couldn't send more that
1047 * what we did. So we always set the CF_EXPECT_MORE flag so that the
1048 * system knows it must not set a PUSH on this first part. Interactive
1049 * modes are already handled by the stream sock layer. We must not do
1050 * this in content-length mode because it could present the MSG_MORE
1051 * flag with the last block of forwarded data, which would cause an
1052 * additional delay to be observed by the receiver.
1053 */
Christopher Faulet2151cdd2020-07-22 16:34:59 +02001054 if (HAS_REQ_DATA_FILTERS(s))
Christopher Faulete0768eb2018-10-03 16:38:02 +02001055 req->flags |= CF_EXPECT_MORE;
1056
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001057 DBG_TRACE_DEVEL("waiting for more data to forward",
1058 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001059 return 0;
1060
Christopher Faulet93e02d82019-03-08 14:18:50 +01001061 return_cli_abort:
Willy Tarreau4781b152021-04-06 13:53:36 +02001062 _HA_ATOMIC_INC(&sess->fe->fe_counters.cli_aborts);
1063 _HA_ATOMIC_INC(&s->be->be_counters.cli_aborts);
William Lallemand36119de2021-03-08 15:26:48 +01001064 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001065 _HA_ATOMIC_INC(&sess->listener->counters->cli_aborts);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001066 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001067 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.cli_aborts);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001068 if (!(s->flags & SF_ERR_MASK))
Christopher Fauletf2b02cf2023-01-13 11:02:28 +01001069 s->flags |= ((req->flags & CF_READ_TIMEOUT) ? SF_ERR_CLITO : SF_ERR_CLICL);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001070 status = 400;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001071 goto return_prx_cond;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001072
1073 return_srv_abort:
Willy Tarreau4781b152021-04-06 13:53:36 +02001074 _HA_ATOMIC_INC(&sess->fe->fe_counters.srv_aborts);
1075 _HA_ATOMIC_INC(&s->be->be_counters.srv_aborts);
William Lallemand36119de2021-03-08 15:26:48 +01001076 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001077 _HA_ATOMIC_INC(&sess->listener->counters->srv_aborts);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001078 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001079 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.srv_aborts);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001080 if (!(s->flags & SF_ERR_MASK))
Christopher Fauletf2b02cf2023-01-13 11:02:28 +01001081 s->flags |= ((req->flags & CF_WRITE_TIMEOUT) ? SF_ERR_SRVTO : SF_ERR_SRVCL);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001082 status = 502;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001083 goto return_prx_cond;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001084
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001085 return_int_err:
1086 if (!(s->flags & SF_ERR_MASK))
1087 s->flags |= SF_ERR_INTERNAL;
Willy Tarreau4781b152021-04-06 13:53:36 +02001088 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
1089 _HA_ATOMIC_INC(&s->be->be_counters.internal_errors);
William Lallemand36119de2021-03-08 15:26:48 +01001090 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001091 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001092 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001093 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.internal_errors);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001094 status = 500;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001095 goto return_prx_cond;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001096
Christopher Faulet93e02d82019-03-08 14:18:50 +01001097 return_bad_req:
Willy Tarreau4781b152021-04-06 13:53:36 +02001098 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req);
William Lallemand36119de2021-03-08 15:26:48 +01001099 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001100 _HA_ATOMIC_INC(&sess->listener->counters->failed_req);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001101 status = 400;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001102 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001103
Christopher Fauletb8a53712019-12-16 11:29:38 +01001104 return_prx_cond:
Christopher Faulet9768c262018-10-22 09:34:31 +02001105 if (txn->status > 0) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001106 /* Note: we don't send any error if some data were already sent */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001107 http_reply_and_close(s, txn->status, NULL);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001108 } else {
Christopher Faulet93e02d82019-03-08 14:18:50 +01001109 txn->status = status;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001110 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001111 }
Christopher Fauletf2b02cf2023-01-13 11:02:28 +01001112 http_set_term_flags(s);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001113 DBG_TRACE_DEVEL("leaving on error ",
1114 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001115 return 0;
1116}
1117
Willy Tarreau4596fe22022-05-17 19:07:51 +02001118/* Reset the stream and the backend stream connector to a situation suitable for attemption connection */
Olivier Houcharda254a372019-04-05 15:30:12 +02001119/* Returns 0 if we can attempt to retry, -1 otherwise */
Willy Tarreaub49672d2022-05-27 10:13:37 +02001120static __inline int do_l7_retry(struct stream *s, struct stconn *sc)
Olivier Houcharda254a372019-04-05 15:30:12 +02001121{
Christopher Faulet9f5382e2021-05-21 13:46:14 +02001122 struct channel *req, *res;
1123 int co_data;
Olivier Houcharda254a372019-04-05 15:30:12 +02001124
Christopher Faulet731c8e62022-03-29 16:08:44 +02001125 s->conn_retries++;
1126 if (s->conn_retries >= s->be->conn_retries)
Christopher Faulet552601d2021-05-26 10:31:06 +02001127 return -1;
Christopher Faulet5b82cc52020-10-12 15:18:50 +02001128
Christopher Faulete763c8c2021-05-05 18:23:59 +02001129 if (objt_server(s->target)) {
1130 if (s->flags & SF_CURR_SESS) {
1131 s->flags &= ~SF_CURR_SESS;
1132 _HA_ATOMIC_DEC(&__objt_server(s->target)->cur_sess);
1133 }
Willy Tarreau4781b152021-04-06 13:53:36 +02001134 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.retries);
Christopher Faulete763c8c2021-05-05 18:23:59 +02001135 }
Willy Tarreau4781b152021-04-06 13:53:36 +02001136 _HA_ATOMIC_INC(&s->be->be_counters.retries);
Willy Tarreau223995e2019-05-04 10:38:31 +02001137
Christopher Faulet9f5382e2021-05-21 13:46:14 +02001138 req = &s->req;
1139 res = &s->res;
Olivier Houcharda254a372019-04-05 15:30:12 +02001140 /* Remove any write error from the request, and read error from the response */
1141 req->flags &= ~(CF_WRITE_ERROR | CF_WRITE_TIMEOUT | CF_SHUTW | CF_SHUTW_NOW);
Christopher Faulet6e1bbc42022-12-12 08:08:15 +01001142 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 +01001143 res->analysers &= AN_RES_FLT_END;
Christopher Faulet50264b42022-03-30 19:39:30 +02001144 s->conn_err_type = STRM_ET_NONE;
Christopher Fauletae024ce2022-03-29 19:02:31 +02001145 s->flags &= ~(SF_CONN_EXP | SF_ERR_MASK | SF_FINST_MASK);
1146 s->conn_exp = TICK_ETERNITY;
Olivier Houchard4bd58672019-07-12 16:16:59 +02001147 stream_choose_redispatch(s);
Olivier Houcharda254a372019-04-05 15:30:12 +02001148 res->rex = TICK_ETERNITY;
1149 res->to_forward = 0;
1150 res->analyse_exp = TICK_ETERNITY;
1151 res->total = 0;
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +01001152
Willy Tarreau19c65a92022-05-27 08:49:24 +02001153 if (sc_reset_endp(s->scb) < 0) {
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +01001154 if (!(s->flags & SF_ERR_MASK))
1155 s->flags |= SF_ERR_INTERNAL;
1156 return -1;
1157 }
Olivier Houcharda254a372019-04-05 15:30:12 +02001158
Christopher Faulet9f5382e2021-05-21 13:46:14 +02001159 b_free(&req->buf);
1160 /* Swap the L7 buffer with the channel buffer */
1161 /* We know we stored the co_data as b_data, so get it there */
Christopher Faulete05bf9e2022-03-29 15:23:40 +02001162 co_data = b_data(&s->txn->l7_buffer);
1163 b_set_data(&s->txn->l7_buffer, b_size(&s->txn->l7_buffer));
1164 b_xfer(&req->buf, &s->txn->l7_buffer, b_data(&s->txn->l7_buffer));
Christopher Faulet9f5382e2021-05-21 13:46:14 +02001165 co_set_data(req, co_data);
Christopher Faulet5b82cc52020-10-12 15:18:50 +02001166
Ilya Shipitsinacf84592021-02-06 22:29:08 +05001167 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 +02001168
Olivier Houcharda254a372019-04-05 15:30:12 +02001169 b_reset(&res->buf);
1170 co_set_data(res, 0);
1171 return 0;
1172}
1173
Christopher Faulete0768eb2018-10-03 16:38:02 +02001174/* This stream analyser waits for a complete HTTP response. It returns 1 if the
1175 * processing can continue on next analysers, or zero if it either needs more
1176 * data or wants to immediately abort the response (eg: timeout, error, ...). It
1177 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->res.analysers
1178 * when it has nothing left to do, and may remove any analyser when it wants to
1179 * abort.
1180 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001181int http_wait_for_response(struct stream *s, struct channel *rep, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001182{
Christopher Faulet9768c262018-10-22 09:34:31 +02001183 /*
1184 * We will analyze a complete HTTP response to check the its syntax.
1185 *
1186 * Once the start line and all headers are received, we may perform a
1187 * capture of the error (if any), and we will set a few fields. We also
1188 * logging and finally headers capture.
1189 */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001190 struct session *sess = s->sess;
1191 struct http_txn *txn = s->txn;
1192 struct http_msg *msg = &txn->rsp;
Christopher Faulet9768c262018-10-22 09:34:31 +02001193 struct htx *htx;
Christopher Faulet61608322018-11-23 16:23:45 +01001194 struct connection *srv_conn;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001195 struct htx_sl *sl;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001196 int n;
1197
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001198 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001199
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001200 htx = htxbuf(&rep->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001201
Willy Tarreau4236f032019-03-05 10:43:32 +01001202 /* Parsing errors are caught here */
1203 if (htx->flags & HTX_FL_PARSING_ERROR)
1204 goto return_bad_res;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001205 if (htx->flags & HTX_FL_PROCESSING_ERROR)
1206 goto return_int_err;
Willy Tarreau4236f032019-03-05 10:43:32 +01001207
Christopher Faulete0768eb2018-10-03 16:38:02 +02001208 /*
1209 * Now we quickly check if we have found a full valid response.
1210 * If not so, we check the FD and buffer states before leaving.
1211 * A full response is indicated by the fact that we have seen
1212 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
1213 * responses are checked first.
1214 *
1215 * Depending on whether the client is still there or not, we
1216 * may send an error response back or not. Note that normally
1217 * we should only check for HTTP status there, and check I/O
1218 * errors somewhere else.
1219 */
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001220 next_one:
Christopher Faulet29f17582019-05-23 11:03:26 +02001221 if (unlikely(htx_is_empty(htx) || htx->first == -1)) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001222 /* 1: have we encountered a read error ? */
1223 if (rep->flags & CF_READ_ERROR) {
Willy Tarreaufd9417b2022-05-18 16:23:22 +02001224 struct connection *conn = sc_conn(s->scb);
Olivier Houchard865d8392019-05-03 22:46:27 +02001225
Christopher Fauletd9769232021-05-26 12:15:37 +02001226 /* Perform a L7 retry because server refuses the early data. */
Christopher Faulete05bf9e2022-03-29 15:23:40 +02001227 if ((txn->flags & TX_L7_RETRY) &&
Christopher Fauletd9769232021-05-26 12:15:37 +02001228 (s->be->retry_type & PR_RE_EARLY_ERROR) &&
1229 conn && conn->err_code == CO_ER_SSL_EARLY_FAILED &&
Willy Tarreau7cb9e6c2022-05-17 19:40:40 +02001230 do_l7_retry(s, s->scb) == 0) {
Christopher Fauletd9769232021-05-26 12:15:37 +02001231 DBG_TRACE_DEVEL("leaving on L7 retry",
1232 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
1233 return 0;
1234 }
1235
Olivier Houchard6db16992019-05-17 15:40:49 +02001236 if (txn->flags & TX_NOT_FIRST)
1237 goto abort_keep_alive;
1238
Willy Tarreau4781b152021-04-06 13:53:36 +02001239 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001240 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001241 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001242 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001243 }
1244
Christopher Fauletd9769232021-05-26 12:15:37 +02001245 /* if the server refused the early data, just send a 425 */
1246 if (conn && conn->err_code == CO_ER_SSL_EARLY_FAILED)
Olivier Houchard865d8392019-05-03 22:46:27 +02001247 txn->status = 425;
Christopher Fauletd9769232021-05-26 12:15:37 +02001248 else {
1249 txn->status = 502;
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001250 stream_inc_http_fail_ctr(s);
Christopher Fauletd9769232021-05-26 12:15:37 +02001251 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001252
Willy Tarreaucb041662022-05-17 19:44:42 +02001253 s->scb->flags |= SC_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001254 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001255
1256 if (!(s->flags & SF_ERR_MASK))
1257 s->flags |= SF_ERR_SRVCL;
Christopher Faulet4a66c942023-01-13 09:43:21 +01001258 http_set_term_flags(s);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001259 DBG_TRACE_DEVEL("leaving on error",
1260 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001261 return 0;
1262 }
1263
Christopher Faulet9768c262018-10-22 09:34:31 +02001264 /* 2: read timeout : return a 504 to the client. */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001265 else if (rep->flags & CF_READ_TIMEOUT) {
Christopher Faulete05bf9e2022-03-29 15:23:40 +02001266 if ((txn->flags & TX_L7_RETRY) &&
Olivier Houcharda254a372019-04-05 15:30:12 +02001267 (s->be->retry_type & PR_RE_TIMEOUT)) {
Willy Tarreau7cb9e6c2022-05-17 19:40:40 +02001268 if (co_data(rep) || do_l7_retry(s, s->scb) == 0) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001269 DBG_TRACE_DEVEL("leaving on L7 retry",
1270 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharda254a372019-04-05 15:30:12 +02001271 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001272 }
Olivier Houcharda254a372019-04-05 15:30:12 +02001273 }
Willy Tarreau4781b152021-04-06 13:53:36 +02001274 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001275 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001276 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001277 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001278 }
1279
Christopher Faulete0768eb2018-10-03 16:38:02 +02001280 txn->status = 504;
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001281 stream_inc_http_fail_ctr(s);
Willy Tarreaucb041662022-05-17 19:44:42 +02001282 s->scb->flags |= SC_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001283 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001284
1285 if (!(s->flags & SF_ERR_MASK))
1286 s->flags |= SF_ERR_SRVTO;
Christopher Faulet4a66c942023-01-13 09:43:21 +01001287 http_set_term_flags(s);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001288 DBG_TRACE_DEVEL("leaving on error",
1289 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001290 return 0;
1291 }
1292
Christopher Faulet9768c262018-10-22 09:34:31 +02001293 /* 3: client abort with an abortonclose */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001294 else if ((rep->flags & CF_SHUTR) && ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001295 _HA_ATOMIC_INC(&sess->fe->fe_counters.cli_aborts);
1296 _HA_ATOMIC_INC(&s->be->be_counters.cli_aborts);
William Lallemand36119de2021-03-08 15:26:48 +01001297 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001298 _HA_ATOMIC_INC(&sess->listener->counters->cli_aborts);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001299 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001300 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.cli_aborts);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001301
Christopher Faulete0768eb2018-10-03 16:38:02 +02001302 txn->status = 400;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001303 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001304
1305 if (!(s->flags & SF_ERR_MASK))
1306 s->flags |= SF_ERR_CLICL;
Christopher Faulet4a66c942023-01-13 09:43:21 +01001307 http_set_term_flags(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001308
1309 /* process_stream() will take care of the error */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001310 DBG_TRACE_DEVEL("leaving on error",
1311 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001312 return 0;
1313 }
1314
Christopher Faulet9768c262018-10-22 09:34:31 +02001315 /* 4: close from server, capture the response if the server has started to respond */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001316 else if (rep->flags & CF_SHUTR) {
Christopher Faulete05bf9e2022-03-29 15:23:40 +02001317 if ((txn->flags & TX_L7_RETRY) &&
Olivier Houcharda254a372019-04-05 15:30:12 +02001318 (s->be->retry_type & PR_RE_DISCONNECTED)) {
Willy Tarreau7cb9e6c2022-05-17 19:40:40 +02001319 if (co_data(rep) || do_l7_retry(s, s->scb) == 0) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001320 DBG_TRACE_DEVEL("leaving on L7 retry",
1321 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharda254a372019-04-05 15:30:12 +02001322 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001323 }
Olivier Houcharda254a372019-04-05 15:30:12 +02001324 }
1325
Olivier Houchard6db16992019-05-17 15:40:49 +02001326 if (txn->flags & TX_NOT_FIRST)
1327 goto abort_keep_alive;
1328
Willy Tarreau4781b152021-04-06 13:53:36 +02001329 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001330 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001331 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001332 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001333 }
1334
Christopher Faulete0768eb2018-10-03 16:38:02 +02001335 txn->status = 502;
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001336 stream_inc_http_fail_ctr(s);
Willy Tarreaucb041662022-05-17 19:44:42 +02001337 s->scb->flags |= SC_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001338 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001339
1340 if (!(s->flags & SF_ERR_MASK))
1341 s->flags |= SF_ERR_SRVCL;
Christopher Faulet4a66c942023-01-13 09:43:21 +01001342 http_set_term_flags(s);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001343 DBG_TRACE_DEVEL("leaving on error",
1344 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001345 return 0;
1346 }
1347
Christopher Faulet9768c262018-10-22 09:34:31 +02001348 /* 5: write error to client (we don't send any message then) */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001349 else if (rep->flags & CF_WRITE_ERROR) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001350 if (txn->flags & TX_NOT_FIRST)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001351 goto abort_keep_alive;
1352
Willy Tarreau4781b152021-04-06 13:53:36 +02001353 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001354 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001355 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001356 rep->analysers &= AN_RES_FLT_END;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001357
1358 if (!(s->flags & SF_ERR_MASK))
1359 s->flags |= SF_ERR_CLICL;
Christopher Faulet4a66c942023-01-13 09:43:21 +01001360 http_set_term_flags(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001361
1362 /* process_stream() will take care of the error */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001363 DBG_TRACE_DEVEL("leaving on error",
1364 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001365 return 0;
1366 }
1367
1368 channel_dont_close(rep);
1369 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001370 DBG_TRACE_DEVEL("waiting for more data",
1371 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001372 return 0;
1373 }
1374
1375 /* More interesting part now : we know that we have a complete
1376 * response which at least looks like HTTP. We have an indicator
1377 * of each header's length, so we can parse them quickly.
1378 */
Christopher Faulet29f17582019-05-23 11:03:26 +02001379 BUG_ON(htx_get_first_type(htx) != HTX_BLK_RES_SL);
Christopher Faulet297fbb42019-05-13 14:41:27 +02001380 sl = http_get_stline(htx);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001381
Christopher Faulet1f08bff2021-05-26 13:14:39 +02001382 /* Perform a L7 retry because of the status code */
Christopher Faulete05bf9e2022-03-29 15:23:40 +02001383 if ((txn->flags & TX_L7_RETRY) &&
Christopher Faulet1f08bff2021-05-26 13:14:39 +02001384 l7_status_match(s->be, sl->info.res.status) &&
Willy Tarreau7cb9e6c2022-05-17 19:40:40 +02001385 do_l7_retry(s, s->scb) == 0) {
Christopher Faulet1f08bff2021-05-26 13:14:39 +02001386 DBG_TRACE_DEVEL("leaving on L7 retry", STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
1387 return 0;
1388 }
1389
1390 /* Now, L7 buffer is useless, it can be released */
Christopher Faulete05bf9e2022-03-29 15:23:40 +02001391 b_free(&txn->l7_buffer);
Christopher Faulet1f08bff2021-05-26 13:14:39 +02001392
1393 msg->msg_state = HTTP_MSG_BODY;
1394
1395
Christopher Faulet9768c262018-10-22 09:34:31 +02001396 /* 0: we might have to print this header in debug mode */
1397 if (unlikely((global.mode & MODE_DEBUG) &&
1398 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
1399 int32_t pos;
1400
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001401 http_debug_stline("srvrep", s, sl);
Christopher Faulet9768c262018-10-22 09:34:31 +02001402
Christopher Fauleta3f15502019-05-13 15:27:23 +02001403 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001404 struct htx_blk *blk = htx_get_blk(htx, pos);
1405 enum htx_blk_type type = htx_get_blk_type(blk);
1406
1407 if (type == HTX_BLK_EOH)
1408 break;
1409 if (type != HTX_BLK_HDR)
1410 continue;
1411
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001412 http_debug_hdr("srvhdr", s,
1413 htx_get_blk_name(htx, blk),
1414 htx_get_blk_value(htx, blk));
Christopher Faulet9768c262018-10-22 09:34:31 +02001415 }
1416 }
1417
Christopher Faulet03599112018-11-27 11:21:21 +01001418 /* 1: get the status code and the version. Also set HTTP flags */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001419 txn->status = sl->info.res.status;
Christopher Faulet03599112018-11-27 11:21:21 +01001420 if (sl->flags & HTX_SL_F_VER_11)
Christopher Faulet9768c262018-10-22 09:34:31 +02001421 msg->flags |= HTTP_MSGF_VER_11;
Christopher Faulet03599112018-11-27 11:21:21 +01001422 if (sl->flags & HTX_SL_F_XFER_LEN) {
1423 msg->flags |= HTTP_MSGF_XFER_LEN;
Christopher Faulet2a408542020-11-20 14:22:37 +01001424 if (sl->flags & HTX_SL_F_CLEN)
1425 msg->flags |= HTTP_MSGF_CNT_LEN;
1426 else if (sl->flags & HTX_SL_F_CHNK)
1427 msg->flags |= HTTP_MSGF_TE_CHNK;
Christopher Faulet03599112018-11-27 11:21:21 +01001428 }
Christopher Faulet2a408542020-11-20 14:22:37 +01001429 if (sl->flags & HTX_SL_F_BODYLESS)
1430 msg->flags |= HTTP_MSGF_BODYLESS;
Christopher Faulet576c3582021-01-08 15:53:01 +01001431 if (sl->flags & HTX_SL_F_CONN_UPG)
1432 msg->flags |= HTTP_MSGF_CONN_UPG;
Christopher Faulet9768c262018-10-22 09:34:31 +02001433
1434 n = txn->status / 100;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001435 if (n < 1 || n > 5)
1436 n = 0;
Christopher Faulet9768c262018-10-22 09:34:31 +02001437
Christopher Faulete0768eb2018-10-03 16:38:02 +02001438 /* when the client triggers a 4xx from the server, it's most often due
1439 * to a missing object or permission. These events should be tracked
1440 * because if they happen often, it may indicate a brute force or a
1441 * vulnerability scan.
1442 */
1443 if (n == 4)
1444 stream_inc_http_err_ctr(s);
1445
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001446 if (n == 5 && txn->status != 501 && txn->status != 505)
1447 stream_inc_http_fail_ctr(s);
1448
Marcin Deranek3c27dda2020-05-15 18:32:51 +02001449 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001450 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.p.http.rsp[n]);
1451 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.p.http.cum_req);
Marcin Deranek3c27dda2020-05-15 18:32:51 +02001452 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001453
Christopher Faulete0768eb2018-10-03 16:38:02 +02001454 /* Adjust server's health based on status code. Note: status codes 501
1455 * and 505 are triggered on demand by client request, so we must not
1456 * count them as server failures.
1457 */
1458 if (objt_server(s->target)) {
1459 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001460 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_OK);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001461 else
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001462 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_STS);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001463 }
1464
1465 /*
1466 * We may be facing a 100-continue response, or any other informational
1467 * 1xx response which is non-final, in which case this is not the right
1468 * response, and we're waiting for the next one. Let's allow this response
1469 * to go to the client and wait for the next one. There's an exception for
1470 * 101 which is used later in the code to switch protocols.
1471 */
1472 if (txn->status < 200 &&
1473 (txn->status == 100 || txn->status >= 102)) {
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001474 FLT_STRM_CB(s, flt_http_reset(s, msg));
Christopher Faulet421e7692019-06-13 11:16:45 +02001475 htx->first = channel_htx_fwd_headers(rep, htx);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001476 msg->msg_state = HTTP_MSG_RPBEFORE;
Christopher Faulet3499f622019-09-03 15:23:54 +02001477 msg->flags = 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001478 txn->status = 0;
1479 s->logs.t_data = -1; /* was not a response yet */
Christopher Faulet7d518452020-08-31 11:07:07 +02001480 rep->flags |= CF_SEND_DONTWAIT; /* Send ASAP informational messages */
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001481 goto next_one;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001482 }
1483
Christopher Faulet6e6c7b12021-01-08 16:02:05 +01001484 /* A 101-switching-protocols must contains a Connection header with the
1485 * "upgrade" option and the request too. It means both are agree to
1486 * upgrade. It is not so strict because there is no test on the Upgrade
1487 * header content. But it is probably stronger enough for now.
1488 */
1489 if (txn->status == 101 &&
1490 (!(txn->req.flags & HTTP_MSGF_CONN_UPG) || !(txn->rsp.flags & HTTP_MSGF_CONN_UPG)))
1491 goto return_bad_res;
1492
Christopher Faulete0768eb2018-10-03 16:38:02 +02001493 /*
1494 * 2: check for cacheability.
1495 */
1496
1497 switch (txn->status) {
1498 case 200:
1499 case 203:
1500 case 204:
1501 case 206:
1502 case 300:
1503 case 301:
1504 case 404:
1505 case 405:
1506 case 410:
1507 case 414:
1508 case 501:
1509 break;
1510 default:
1511 /* RFC7231#6.1:
1512 * Responses with status codes that are defined as
1513 * cacheable by default (e.g., 200, 203, 204, 206,
1514 * 300, 301, 404, 405, 410, 414, and 501 in this
1515 * specification) can be reused by a cache with
1516 * heuristic expiration unless otherwise indicated
1517 * by the method definition or explicit cache
1518 * controls [RFC7234]; all other status codes are
1519 * not cacheable by default.
1520 */
1521 txn->flags &= ~(TX_CACHEABLE | TX_CACHE_COOK);
1522 break;
1523 }
1524
1525 /*
1526 * 3: we may need to capture headers
1527 */
1528 s->logs.logwait &= ~LW_RESP;
1529 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->res_cap))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001530 http_capture_headers(htx, s->res_cap, sess->fe->rsp_cap);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001531
Christopher Faulet9768c262018-10-22 09:34:31 +02001532 /* Skip parsing if no content length is possible. */
Christopher Fauletc75668e2020-12-07 18:10:32 +01001533 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status >= 200 && txn->status < 300) ||
Christopher Faulete0768eb2018-10-03 16:38:02 +02001534 txn->status == 101)) {
1535 /* Either we've established an explicit tunnel, or we're
1536 * switching the protocol. In both cases, we're very unlikely
1537 * to understand the next protocols. We have to switch to tunnel
1538 * mode, so that we transfer the request and responses then let
1539 * this protocol pass unmodified. When we later implement specific
1540 * parsers for such protocols, we'll want to check the Upgrade
1541 * header which contains information about that protocol for
1542 * responses with status 101 (eg: see RFC2817 about TLS).
1543 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02001544 txn->flags |= TX_CON_WANT_TUN;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001545 }
1546
Christopher Faulet61608322018-11-23 16:23:45 +01001547 /* check for NTML authentication headers in 401 (WWW-Authenticate) and
1548 * 407 (Proxy-Authenticate) responses and set the connection to private
1549 */
Willy Tarreaufd9417b2022-05-18 16:23:22 +02001550 srv_conn = sc_conn(s->scb);
Christopher Faulet61608322018-11-23 16:23:45 +01001551 if (srv_conn) {
1552 struct ist hdr;
1553 struct http_hdr_ctx ctx;
1554
1555 if (txn->status == 401)
1556 hdr = ist("WWW-Authenticate");
1557 else if (txn->status == 407)
1558 hdr = ist("Proxy-Authenticate");
1559 else
1560 goto end;
1561
1562 ctx.blk = NULL;
1563 while (http_find_header(htx, hdr, &ctx, 0)) {
Willy Tarreauf1dcced2020-05-07 19:27:02 +02001564 /* If www-authenticate contains "Negotiate", "Nego2", or "NTLM",
1565 * possibly followed by blanks and a base64 string, the connection
1566 * is private. Since it's a mess to deal with, we only check for
1567 * values starting with "NTLM" or "Nego". Note that often multiple
1568 * headers are sent by the server there.
1569 */
1570 if ((ctx.value.len >= 4 && strncasecmp(ctx.value.ptr, "Nego", 4) == 0) ||
Willy Tarreau49a1d282020-05-07 19:10:15 +02001571 (ctx.value.len >= 4 && strncasecmp(ctx.value.ptr, "NTLM", 4) == 0)) {
Olivier Houchard250031e2019-05-29 15:01:50 +02001572 sess->flags |= SESS_FL_PREFER_LAST;
Christopher Faulet08016ab2020-07-01 16:10:06 +02001573 conn_set_owner(srv_conn, sess, NULL);
Christopher Faulet21ddc742020-07-01 15:26:14 +02001574 conn_set_private(srv_conn);
Ilya Shipitsin6b79f382020-07-23 00:32:55 +05001575 /* If it fail now, the same will be done in mux->detach() callback */
Christopher Faulet08016ab2020-07-01 16:10:06 +02001576 session_add_conn(srv_conn->owner, srv_conn, srv_conn->target);
Willy Tarreauf1dcced2020-05-07 19:27:02 +02001577 break;
Olivier Houchard250031e2019-05-29 15:01:50 +02001578 }
Christopher Faulet61608322018-11-23 16:23:45 +01001579 }
1580 }
1581
1582 end:
Christopher Faulete0768eb2018-10-03 16:38:02 +02001583 /* we want to have the response time before we start processing it */
1584 s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now);
1585
1586 /* end of job, return OK */
1587 rep->analysers &= ~an_bit;
1588 rep->analyse_exp = TICK_ETERNITY;
1589 channel_auto_close(rep);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001590 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001591 return 1;
1592
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001593 return_int_err:
Willy Tarreau4781b152021-04-06 13:53:36 +02001594 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
1595 _HA_ATOMIC_INC(&s->be->be_counters.internal_errors);
William Lallemand36119de2021-03-08 15:26:48 +01001596 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001597 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Fauletb8a53712019-12-16 11:29:38 +01001598 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001599 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.internal_errors);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001600 txn->status = 500;
1601 if (!(s->flags & SF_ERR_MASK))
1602 s->flags |= SF_ERR_INTERNAL;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001603 goto return_prx_cond;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001604
1605 return_bad_res:
Willy Tarreau4781b152021-04-06 13:53:36 +02001606 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Faulet47365272018-10-31 17:40:50 +01001607 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001608 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001609 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Christopher Faulet47365272018-10-31 17:40:50 +01001610 }
Olivier Houcharde3249a92019-05-03 23:01:47 +02001611 if ((s->be->retry_type & PR_RE_JUNK_REQUEST) &&
Christopher Faulete05bf9e2022-03-29 15:23:40 +02001612 (txn->flags & TX_L7_RETRY) &&
Willy Tarreau7cb9e6c2022-05-17 19:40:40 +02001613 do_l7_retry(s, s->scb) == 0) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001614 DBG_TRACE_DEVEL("leaving on L7 retry",
1615 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharde3249a92019-05-03 23:01:47 +02001616 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001617 }
Christopher Faulet47365272018-10-31 17:40:50 +01001618 txn->status = 502;
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001619 stream_inc_http_fail_ctr(s);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001620 /* fall through */
1621
Christopher Fauletb8a53712019-12-16 11:29:38 +01001622 return_prx_cond:
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001623 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet4a66c942023-01-13 09:43:21 +01001624 http_set_term_flags(s);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001625
Willy Tarreaucb041662022-05-17 19:44:42 +02001626 s->scb->flags |= SC_FL_NOLINGER;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001627 DBG_TRACE_DEVEL("leaving on error",
1628 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulet47365272018-10-31 17:40:50 +01001629 return 0;
1630
Christopher Faulete0768eb2018-10-03 16:38:02 +02001631 abort_keep_alive:
1632 /* A keep-alive request to the server failed on a network error.
1633 * The client is required to retry. We need to close without returning
1634 * any other information so that the client retries.
1635 */
1636 txn->status = 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001637 s->logs.logwait = 0;
1638 s->logs.level = 0;
1639 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001640 http_reply_and_close(s, txn->status, NULL);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001641 DBG_TRACE_DEVEL("leaving by closing K/A connection",
1642 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001643 return 0;
1644}
1645
1646/* This function performs all the processing enabled for the current response.
1647 * It normally returns 1 unless it wants to break. It relies on buffers flags,
1648 * and updates s->res.analysers. It might make sense to explode it into several
1649 * other functions. It works like process_request (see indications above).
1650 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001651int http_process_res_common(struct stream *s, struct channel *rep, int an_bit, struct proxy *px)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001652{
1653 struct session *sess = s->sess;
1654 struct http_txn *txn = s->txn;
1655 struct http_msg *msg = &txn->rsp;
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001656 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001657 struct proxy *cur_proxy;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001658 enum rule_result ret = HTTP_RULE_RES_CONT;
1659
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001660 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
1661 return 0;
Christopher Faulet9768c262018-10-22 09:34:31 +02001662
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001663 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001664
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001665 htx = htxbuf(&rep->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001666
1667 /* The stats applet needs to adjust the Connection header but we don't
1668 * apply any filter there.
1669 */
1670 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
1671 rep->analysers &= ~an_bit;
1672 rep->analyse_exp = TICK_ETERNITY;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001673 goto end;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001674 }
1675
1676 /*
1677 * We will have to evaluate the filters.
1678 * As opposed to version 1.2, now they will be evaluated in the
1679 * filters order and not in the header order. This means that
1680 * each filter has to be validated among all headers.
1681 *
1682 * Filters are tried with ->be first, then with ->fe if it is
1683 * different from ->be.
1684 *
1685 * Maybe we are in resume condiion. In this case I choose the
1686 * "struct proxy" which contains the rule list matching the resume
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05001687 * pointer. If none of these "struct proxy" match, I initialise
Christopher Faulete0768eb2018-10-03 16:38:02 +02001688 * the process with the first one.
1689 *
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05001690 * In fact, I check only correspondence between the current list
Christopher Faulete0768eb2018-10-03 16:38:02 +02001691 * pointer and the ->fe rule list. If it doesn't match, I initialize
1692 * the loop with the ->be.
1693 */
Christopher Fauletd4150ad2021-10-13 15:35:55 +02001694 if (s->current_rule_list == &sess->fe->http_res_rules ||
1695 (sess->fe->defpx && s->current_rule_list == &sess->fe->defpx->http_res_rules))
Christopher Faulete0768eb2018-10-03 16:38:02 +02001696 cur_proxy = sess->fe;
1697 else
1698 cur_proxy = s->be;
Christopher Fauletd4150ad2021-10-13 15:35:55 +02001699
Christopher Faulete0768eb2018-10-03 16:38:02 +02001700 while (1) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001701 /* evaluate http-response rules */
Christopher Faulet46f46df2021-11-09 16:33:25 +01001702 if (ret == HTTP_RULE_RES_CONT || ret == HTTP_RULE_RES_STOP) {
Christopher Fauletd4150ad2021-10-13 15:35:55 +02001703 struct list *def_rules, *rules;
1704
1705 def_rules = ((cur_proxy->defpx && (cur_proxy == s->be || cur_proxy->defpx != s->be->defpx)) ? &cur_proxy->defpx->http_res_rules : NULL);
1706 rules = &cur_proxy->http_res_rules;
1707
1708 ret = http_res_get_intercept_rule(cur_proxy, def_rules, rules, s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001709
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001710 switch (ret) {
1711 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
1712 goto return_prx_yield;
1713
1714 case HTTP_RULE_RES_CONT:
1715 case HTTP_RULE_RES_STOP: /* nothing to do */
1716 break;
1717
1718 case HTTP_RULE_RES_DENY: /* deny or tarpit */
1719 goto deny;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001720
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001721 case HTTP_RULE_RES_ABRT: /* abort request, response already sent */
1722 goto return_prx_cond;
1723
1724 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
Christopher Fauletb8a53712019-12-16 11:29:38 +01001725 goto done;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001726
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001727 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
1728 goto return_bad_res;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001729
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001730 case HTTP_RULE_RES_ERROR: /* failed with a bad request */
1731 goto return_int_err;
1732 }
1733
1734 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001735
Christopher Faulete0768eb2018-10-03 16:38:02 +02001736 /* check whether we're already working on the frontend */
1737 if (cur_proxy == sess->fe)
1738 break;
1739 cur_proxy = sess->fe;
1740 }
1741
Christopher Faulete0768eb2018-10-03 16:38:02 +02001742 /* OK that's all we can do for 1xx responses */
1743 if (unlikely(txn->status < 200 && txn->status != 101))
Christopher Fauletf2824e62018-10-01 12:12:37 +02001744 goto end;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001745
1746 /*
1747 * Now check for a server cookie.
1748 */
1749 if (s->be->cookie_name || sess->fe->capture_name || (s->be->options & PR_O_CHK_CACHE))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001750 http_manage_server_side_cookies(s, rep);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001751
1752 /*
1753 * Check for cache-control or pragma headers if required.
1754 */
1755 if ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001756 http_check_response_for_cacheability(s, rep);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001757
1758 /*
1759 * Add server cookie in the response if needed
1760 */
1761 if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) &&
1762 !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) &&
1763 (!(s->flags & SF_DIRECT) ||
1764 ((s->be->cookie_maxidle || txn->cookie_last_date) &&
1765 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
1766 (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
1767 (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
1768 (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
1769 !(s->flags & SF_IGNORE_PRST)) {
1770 /* the server is known, it's not the one the client requested, or the
1771 * cookie's last seen date needs to be refreshed. We have to
1772 * insert a set-cookie here, except if we want to insert only on POST
1773 * requests and this one isn't. Note that servers which don't have cookies
1774 * (eg: some backup servers) will return a full cookie removal request.
1775 */
Willy Tarreau88bc8002021-12-06 07:01:02 +00001776 if (!__objt_server(s->target)->cookie) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001777 chunk_printf(&trash,
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001778 "%s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
Christopher Faulete0768eb2018-10-03 16:38:02 +02001779 s->be->cookie_name);
1780 }
1781 else {
Willy Tarreau88bc8002021-12-06 07:01:02 +00001782 chunk_printf(&trash, "%s=%s", s->be->cookie_name, __objt_server(s->target)->cookie);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001783
1784 if (s->be->cookie_maxidle || s->be->cookie_maxlife) {
1785 /* emit last_date, which is mandatory */
1786 trash.area[trash.data++] = COOKIE_DELIM_DATE;
1787 s30tob64((date.tv_sec+3) >> 2,
1788 trash.area + trash.data);
1789 trash.data += 5;
1790
1791 if (s->be->cookie_maxlife) {
1792 /* emit first_date, which is either the original one or
1793 * the current date.
1794 */
1795 trash.area[trash.data++] = COOKIE_DELIM_DATE;
1796 s30tob64(txn->cookie_first_date ?
1797 txn->cookie_first_date >> 2 :
1798 (date.tv_sec+3) >> 2,
1799 trash.area + trash.data);
1800 trash.data += 5;
1801 }
1802 }
1803 chunk_appendf(&trash, "; path=/");
1804 }
1805
1806 if (s->be->cookie_domain)
1807 chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain);
1808
1809 if (s->be->ck_opts & PR_CK_HTTPONLY)
1810 chunk_appendf(&trash, "; HttpOnly");
1811
1812 if (s->be->ck_opts & PR_CK_SECURE)
1813 chunk_appendf(&trash, "; Secure");
1814
Christopher Faulet2f533902020-01-21 11:06:48 +01001815 if (s->be->cookie_attrs)
1816 chunk_appendf(&trash, "; %s", s->be->cookie_attrs);
1817
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001818 if (unlikely(!http_add_header(htx, ist("Set-Cookie"), ist2(trash.area, trash.data))))
Christopher Fauletd649b572022-06-01 17:42:35 +02001819 goto return_fail_rewrite;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001820
1821 txn->flags &= ~TX_SCK_MASK;
1822 if (__objt_server(s->target)->cookie && (s->flags & SF_DIRECT))
1823 /* the server did not change, only the date was updated */
1824 txn->flags |= TX_SCK_UPDATED;
1825 else
1826 txn->flags |= TX_SCK_INSERTED;
1827
1828 /* Here, we will tell an eventual cache on the client side that we don't
1829 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
1830 * Some caches understand the correct form: 'no-cache="set-cookie"', but
1831 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
1832 */
1833 if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
1834
1835 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
1836
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001837 if (unlikely(!http_add_header(htx, ist("Cache-control"), ist("private"))))
Christopher Fauletd649b572022-06-01 17:42:35 +02001838 goto return_fail_rewrite;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001839 }
1840 }
1841
1842 /*
1843 * Check if result will be cacheable with a cookie.
1844 * We'll block the response if security checks have caught
1845 * nasty things such as a cacheable cookie.
1846 */
1847 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
1848 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
1849 (s->be->options & PR_O_CHK_CACHE)) {
1850 /* we're in presence of a cacheable response containing
1851 * a set-cookie header. We'll block it as requested by
1852 * the 'checkcache' option, and send an alert.
1853 */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001854 ha_alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreau88bc8002021-12-06 07:01:02 +00001855 s->be->id, objt_server(s->target) ? __objt_server(s->target)->id : "<dispatch>");
Christopher Faulete0768eb2018-10-03 16:38:02 +02001856 send_log(s->be, LOG_ALERT,
1857 "Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreau88bc8002021-12-06 07:01:02 +00001858 s->be->id, objt_server(s->target) ? __objt_server(s->target)->id : "<dispatch>");
Christopher Fauletb8a53712019-12-16 11:29:38 +01001859 goto deny;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001860 }
1861
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001862 end:
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01001863 /*
1864 * Evaluate after-response rules before forwarding the response. rules
1865 * from the backend are evaluated first, then one from the frontend if
1866 * it differs.
1867 */
1868 if (!http_eval_after_res_rules(s))
1869 goto return_int_err;
1870
Christopher Fauletc2ac5e42021-03-08 18:20:09 +01001871 /* Filter the response headers if there are filters attached to the
1872 * stream.
1873 */
1874 if (HAS_FILTERS(s))
1875 rep->analysers |= AN_RES_FLT_HTTP_HDRS;
1876
Christopher Faulete0768eb2018-10-03 16:38:02 +02001877 /* Always enter in the body analyzer */
1878 rep->analysers &= ~AN_RES_FLT_XFER_DATA;
1879 rep->analysers |= AN_RES_HTTP_XFER_BODY;
1880
1881 /* if the user wants to log as soon as possible, without counting
1882 * bytes from the server, then this is the right moment. We have
1883 * to temporarily assign bytes_out to log what we currently have.
1884 */
1885 if (!LIST_ISEMPTY(&sess->fe->logformat) && !(s->logs.logwait & LW_BYTES)) {
1886 s->logs.t_close = s->logs.t_data; /* to get a valid end date */
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001887 s->logs.bytes_out = htx->data;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001888 s->do_log(s);
1889 s->logs.bytes_out = 0;
1890 }
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001891
Christopher Fauletb8a53712019-12-16 11:29:38 +01001892 done:
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01001893 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletb8a53712019-12-16 11:29:38 +01001894 rep->analysers &= ~an_bit;
1895 rep->analyse_exp = TICK_ETERNITY;
Christopher Fauletd4150ad2021-10-13 15:35:55 +02001896 s->current_rule = s->current_rule_list = NULL;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001897 return 1;
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001898
Christopher Fauletb8a53712019-12-16 11:29:38 +01001899 deny:
Willy Tarreau4781b152021-04-06 13:53:36 +02001900 _HA_ATOMIC_INC(&sess->fe->fe_counters.denied_resp);
1901 _HA_ATOMIC_INC(&s->be->be_counters.denied_resp);
William Lallemand36119de2021-03-08 15:26:48 +01001902 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001903 _HA_ATOMIC_INC(&sess->listener->counters->denied_resp);
Christopher Fauleta08546b2019-12-16 16:07:34 +01001904 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001905 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.denied_resp);
Christopher Fauletb8a53712019-12-16 11:29:38 +01001906 goto return_prx_err;
1907
Christopher Fauletd649b572022-06-01 17:42:35 +02001908 return_fail_rewrite:
1909 if (!(s->flags & SF_ERR_MASK))
1910 s->flags |= SF_ERR_PRXCOND;
1911 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_rewrites);
1912 _HA_ATOMIC_INC(&s->be->be_counters.failed_rewrites);
1913 if (sess->listener && sess->listener->counters)
1914 _HA_ATOMIC_INC(&sess->listener->counters->failed_rewrites);
1915 if (objt_server(s->target))
1916 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_rewrites);
1917 /* fall through */
1918
Christopher Fauletb8a53712019-12-16 11:29:38 +01001919 return_int_err:
1920 txn->status = 500;
1921 if (!(s->flags & SF_ERR_MASK))
1922 s->flags |= SF_ERR_INTERNAL;
Willy Tarreau4781b152021-04-06 13:53:36 +02001923 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
1924 _HA_ATOMIC_INC(&s->be->be_counters.internal_errors);
Dragan Dosen9a006f92021-09-21 13:02:09 +02001925 if (sess->listener && sess->listener->counters)
1926 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001927 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001928 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.internal_errors);
Christopher Fauletb8a53712019-12-16 11:29:38 +01001929 goto return_prx_err;
1930
1931 return_bad_res:
1932 txn->status = 502;
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001933 stream_inc_http_fail_ctr(s);
Willy Tarreau4781b152021-04-06 13:53:36 +02001934 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Fauleta20a6532020-02-05 10:16:41 +01001935 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001936 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Christopher Fauleta20a6532020-02-05 10:16:41 +01001937 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_RSP);
1938 }
Christopher Fauletb8a53712019-12-16 11:29:38 +01001939 /* fall through */
1940
1941 return_prx_err:
1942 http_reply_and_close(s, txn->status, http_error_message(s));
1943 /* fall through */
1944
1945 return_prx_cond:
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001946 s->logs.t_data = -1; /* was not a valid response */
Willy Tarreaucb041662022-05-17 19:44:42 +02001947 s->scb->flags |= SC_FL_NOLINGER;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001948
Christopher Faulet4a66c942023-01-13 09:43:21 +01001949 http_set_term_flags(s);
Christopher Fauletb8a53712019-12-16 11:29:38 +01001950
Christopher Faulete58c0002020-03-02 16:21:01 +01001951 rep->analysers &= AN_RES_FLT_END;
1952 s->req.analysers &= AN_REQ_FLT_END;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001953 rep->analyse_exp = TICK_ETERNITY;
Christopher Fauletd4150ad2021-10-13 15:35:55 +02001954 s->current_rule = s->current_rule_list = NULL;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001955 DBG_TRACE_DEVEL("leaving on error",
1956 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001957 return 0;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001958
1959 return_prx_yield:
1960 channel_dont_close(rep);
1961 DBG_TRACE_DEVEL("waiting for more data",
1962 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
1963 return 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001964}
1965
1966/* This function is an analyser which forwards response body (including chunk
1967 * sizes if any). It is called as soon as we must forward, even if we forward
1968 * zero byte. The only situation where it must not be called is when we're in
1969 * tunnel mode and we want to forward till the close. It's used both to forward
1970 * remaining data and to resync after end of body. It expects the msg_state to
1971 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
1972 * read more data, or 1 once we can go on with next request or end the stream.
1973 *
1974 * It is capable of compressing response data both in content-length mode and
1975 * in chunked mode. The state machines follows different flows depending on
1976 * whether content-length and chunked modes are used, since there are no
1977 * trailers in content-length :
1978 *
1979 * chk-mode cl-mode
1980 * ,----- BODY -----.
1981 * / \
1982 * V size > 0 V chk-mode
1983 * .--> SIZE -------------> DATA -------------> CRLF
1984 * | | size == 0 | last byte |
1985 * | v final crlf v inspected |
1986 * | TRAILERS -----------> DONE |
1987 * | |
1988 * `----------------------------------------------'
1989 *
1990 * Compression only happens in the DATA state, and must be flushed in final
1991 * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding
1992 * is performed at once on final states for all bytes parsed, or when leaving
1993 * on missing data.
1994 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001995int http_response_forward_body(struct stream *s, struct channel *res, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001996{
1997 struct session *sess = s->sess;
1998 struct http_txn *txn = s->txn;
1999 struct http_msg *msg = &s->txn->rsp;
Christopher Faulet9768c262018-10-22 09:34:31 +02002000 struct htx *htx;
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002001 int ret;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002002
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002003 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002004
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002005 htx = htxbuf(&res->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002006
Christopher Fauletb9a92f32019-09-09 10:15:21 +02002007 if (htx->flags & HTX_FL_PARSING_ERROR)
2008 goto return_bad_res;
2009 if (htx->flags & HTX_FL_PROCESSING_ERROR)
2010 goto return_int_err;
2011
Christopher Faulet9768c262018-10-22 09:34:31 +02002012 if (msg->msg_state == HTTP_MSG_BODY)
2013 msg->msg_state = HTTP_MSG_DATA;
2014
Christopher Faulete0768eb2018-10-03 16:38:02 +02002015 /* in most states, we should abort in case of early close */
2016 channel_auto_close(res);
2017
Christopher Faulete0768eb2018-10-03 16:38:02 +02002018 if (res->to_forward) {
Christopher Faulet66af0b22019-03-22 14:54:52 +01002019 if (res->to_forward == CHN_INFINITE_FORWARD) {
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002020 if (res->flags & CF_EOI)
2021 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet66af0b22019-03-22 14:54:52 +01002022 }
2023 else {
2024 /* We can't process the buffer's contents yet */
2025 res->flags |= CF_WAKE_WRITE;
2026 goto missing_data_or_waiting;
2027 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02002028 }
2029
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002030 if (msg->msg_state >= HTTP_MSG_ENDING)
2031 goto ending;
2032
Christopher Fauletc75668e2020-12-07 18:10:32 +01002033 if ((txn->meth == HTTP_METH_CONNECT && txn->status >= 200 && txn->status < 300) || txn->status == 101 ||
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002034 (!(msg->flags & HTTP_MSGF_XFER_LEN) && !HAS_RSP_DATA_FILTERS(s))) {
2035 msg->msg_state = HTTP_MSG_ENDING;
2036 goto ending;
2037 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002038
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002039 /* Forward input data. We get it by removing all outgoing data not
2040 * forwarded yet from HTX data size. If there are some data filters, we
2041 * let them decide the amount of data to forward.
Christopher Faulet9768c262018-10-22 09:34:31 +02002042 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002043 if (HAS_RSP_DATA_FILTERS(s)) {
2044 ret = flt_http_payload(s, msg, htx->data);
2045 if (ret < 0)
2046 goto return_bad_res;
Christopher Faulet421e7692019-06-13 11:16:45 +02002047 c_adv(res, ret);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002048 }
2049 else {
Christopher Faulet421e7692019-06-13 11:16:45 +02002050 c_adv(res, htx->data - co_data(res));
Christopher Faulet66af0b22019-03-22 14:54:52 +01002051 if (msg->flags & HTTP_MSGF_XFER_LEN)
2052 channel_htx_forward_forever(res, htx);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002053 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002054
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002055 if (htx->data != co_data(res))
2056 goto missing_data_or_waiting;
2057
2058 if (!(msg->flags & HTTP_MSGF_XFER_LEN) && res->flags & CF_SHUTR) {
2059 msg->msg_state = HTTP_MSG_ENDING;
2060 goto ending;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002061 }
2062
Christopher Faulet9768c262018-10-22 09:34:31 +02002063 /* Check if the end-of-message is reached and if so, switch the message
Christopher Fauletd20fdb02019-06-13 16:43:22 +02002064 * in HTTP_MSG_ENDING state. Then if all data was marked to be
2065 * forwarded, set the state to HTTP_MSG_DONE.
Christopher Faulet9768c262018-10-22 09:34:31 +02002066 */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002067 if (!(htx->flags & HTX_FL_EOM))
Christopher Faulet9768c262018-10-22 09:34:31 +02002068 goto missing_data_or_waiting;
2069
Christopher Fauletd20fdb02019-06-13 16:43:22 +02002070 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet9768c262018-10-22 09:34:31 +02002071
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002072 ending:
Christopher Faulet2151cdd2020-07-22 16:34:59 +02002073 res->flags &= ~CF_EXPECT_MORE; /* no more data are expected */
2074
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002075 /* other states, ENDING...TUNNEL */
2076 if (msg->msg_state >= HTTP_MSG_DONE)
2077 goto done;
Christopher Faulet9768c262018-10-22 09:34:31 +02002078
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002079 if (HAS_RSP_DATA_FILTERS(s)) {
2080 ret = flt_http_end(s, msg);
2081 if (ret <= 0) {
2082 if (!ret)
2083 goto missing_data_or_waiting;
2084 goto return_bad_res;
2085 }
2086 }
2087
Christopher Fauletc75668e2020-12-07 18:10:32 +01002088 if ((txn->meth == HTTP_METH_CONNECT && txn->status >= 200 && txn->status < 300) || txn->status == 101 ||
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002089 !(msg->flags & HTTP_MSGF_XFER_LEN)) {
2090 msg->msg_state = HTTP_MSG_TUNNEL;
2091 goto ending;
2092 }
2093 else {
2094 msg->msg_state = HTTP_MSG_DONE;
2095 res->to_forward = 0;
2096 }
2097
2098 done:
2099
2100 channel_dont_close(res);
2101
Christopher Fauletf2b02cf2023-01-13 11:02:28 +01002102 if ((res->flags & CF_SHUTW) && co_data(res)) {
2103 /* response errors are most likely due to the client aborting
2104 * the transfer. */
2105 goto return_cli_abort;
2106 }
2107
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002108 http_end_response(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002109 if (!(res->analysers & an_bit)) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002110 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002111 return 1;
2112 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002113 DBG_TRACE_DEVEL("waiting for the end of the HTTP txn",
2114 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002115 return 0;
2116
2117 missing_data_or_waiting:
2118 if (res->flags & CF_SHUTW)
Christopher Faulet93e02d82019-03-08 14:18:50 +01002119 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002120
2121 /* stop waiting for data if the input is closed before the end. If the
2122 * client side was already closed, it means that the client has aborted,
2123 * so we don't want to count this as a server abort. Otherwise it's a
2124 * server abort.
2125 */
Christopher Fauletd20fdb02019-06-13 16:43:22 +02002126 if (msg->msg_state < HTTP_MSG_ENDING && res->flags & CF_SHUTR) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02002127 if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))
Christopher Faulet93e02d82019-03-08 14:18:50 +01002128 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002129 /* If we have some pending data, we continue the processing */
Christopher Faulet93e02d82019-03-08 14:18:50 +01002130 if (htx_is_empty(htx))
2131 goto return_srv_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002132 }
2133
Christopher Faulete0768eb2018-10-03 16:38:02 +02002134 /* When TE: chunked is used, we need to get there again to parse
2135 * remaining chunks even if the server has closed, so we don't want to
Christopher Faulet9768c262018-10-22 09:34:31 +02002136 * set CF_DONTCLOSE. Similarly when there is a content-leng or if there
2137 * are filters registered on the stream, we don't want to forward a
2138 * close
Christopher Faulete0768eb2018-10-03 16:38:02 +02002139 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002140 if ((msg->flags & HTTP_MSGF_XFER_LEN) || HAS_RSP_DATA_FILTERS(s))
Christopher Faulete0768eb2018-10-03 16:38:02 +02002141 channel_dont_close(res);
2142
2143 /* We know that more data are expected, but we couldn't send more that
2144 * what we did. So we always set the CF_EXPECT_MORE flag so that the
2145 * system knows it must not set a PUSH on this first part. Interactive
2146 * modes are already handled by the stream sock layer. We must not do
2147 * this in content-length mode because it could present the MSG_MORE
2148 * flag with the last block of forwarded data, which would cause an
2149 * additional delay to be observed by the receiver.
2150 */
Christopher Faulet2151cdd2020-07-22 16:34:59 +02002151 if (HAS_RSP_DATA_FILTERS(s))
Christopher Faulete0768eb2018-10-03 16:38:02 +02002152 res->flags |= CF_EXPECT_MORE;
2153
2154 /* the stream handler will take care of timeouts and errors */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002155 DBG_TRACE_DEVEL("waiting for more data to forward",
2156 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002157 return 0;
2158
Christopher Faulet93e02d82019-03-08 14:18:50 +01002159 return_srv_abort:
Willy Tarreau4781b152021-04-06 13:53:36 +02002160 _HA_ATOMIC_INC(&sess->fe->fe_counters.srv_aborts);
2161 _HA_ATOMIC_INC(&s->be->be_counters.srv_aborts);
William Lallemand36119de2021-03-08 15:26:48 +01002162 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02002163 _HA_ATOMIC_INC(&sess->listener->counters->srv_aborts);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002164 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02002165 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.srv_aborts);
Willy Tarreau826f3ab2021-02-10 12:07:15 +01002166 stream_inc_http_fail_ctr(s);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002167 if (!(s->flags & SF_ERR_MASK))
Christopher Fauletf2b02cf2023-01-13 11:02:28 +01002168 s->flags |= ((res->flags & CF_READ_TIMEOUT) ? SF_ERR_SRVTO : SF_ERR_SRVCL);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002169 goto return_error;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002170
Christopher Faulet93e02d82019-03-08 14:18:50 +01002171 return_cli_abort:
Willy Tarreau4781b152021-04-06 13:53:36 +02002172 _HA_ATOMIC_INC(&sess->fe->fe_counters.cli_aborts);
2173 _HA_ATOMIC_INC(&s->be->be_counters.cli_aborts);
William Lallemand36119de2021-03-08 15:26:48 +01002174 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02002175 _HA_ATOMIC_INC(&sess->listener->counters->cli_aborts);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002176 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02002177 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.cli_aborts);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002178 if (!(s->flags & SF_ERR_MASK))
Christopher Fauletf2b02cf2023-01-13 11:02:28 +01002179 s->flags |= ((res->flags & CF_WRITE_TIMEOUT) ? SF_ERR_CLITO : SF_ERR_CLICL);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002180 goto return_error;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002181
Christopher Fauletb9a92f32019-09-09 10:15:21 +02002182 return_int_err:
Willy Tarreau4781b152021-04-06 13:53:36 +02002183 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
2184 _HA_ATOMIC_INC(&s->be->be_counters.internal_errors);
William Lallemand36119de2021-03-08 15:26:48 +01002185 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02002186 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002187 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02002188 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.internal_errors);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02002189 if (!(s->flags & SF_ERR_MASK))
2190 s->flags |= SF_ERR_INTERNAL;
2191 goto return_error;
2192
Christopher Faulet93e02d82019-03-08 14:18:50 +01002193 return_bad_res:
Willy Tarreau4781b152021-04-06 13:53:36 +02002194 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002195 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02002196 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002197 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_RSP);
2198 }
Willy Tarreau826f3ab2021-02-10 12:07:15 +01002199 stream_inc_http_fail_ctr(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002200 if (!(s->flags & SF_ERR_MASK))
Christopher Faulet93e02d82019-03-08 14:18:50 +01002201 s->flags |= SF_ERR_SRVCL;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002202 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +02002203
Christopher Faulet93e02d82019-03-08 14:18:50 +01002204 return_error:
Christopher Faulete0768eb2018-10-03 16:38:02 +02002205 /* don't send any error message as we're in the body */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002206 http_reply_and_close(s, txn->status, NULL);
Christopher Fauletf2b02cf2023-01-13 11:02:28 +01002207 http_set_term_flags(s);
2208 stream_inc_http_fail_ctr(s);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002209 DBG_TRACE_DEVEL("leaving on error",
2210 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002211 return 0;
2212}
2213
Christopher Fauletf2824e62018-10-01 12:12:37 +02002214/* Perform an HTTP redirect based on the information in <rule>. The function
Willy Tarreaubc1223b2021-09-02 16:54:33 +02002215 * returns zero in case of an irrecoverable error such as too large a request
2216 * to build a valid response, 1 in case of successful redirect (hence the rule
2217 * is final), or 2 if the rule has to be silently skipped.
Christopher Fauletf2824e62018-10-01 12:12:37 +02002218 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002219int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn)
Christopher Fauletf2824e62018-10-01 12:12:37 +02002220{
Christopher Faulet99daf282018-11-28 22:58:13 +01002221 struct channel *req = &s->req;
2222 struct channel *res = &s->res;
2223 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01002224 struct htx_sl *sl;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002225 struct buffer *chunk;
Christopher Faulet99daf282018-11-28 22:58:13 +01002226 struct ist status, reason, location;
2227 unsigned int flags;
Christopher Fauleteab17572022-04-26 20:34:38 +02002228 int ret = 1, close = 0; /* Try to keep the connection alive byt default */
Christopher Fauletf2824e62018-10-01 12:12:37 +02002229
2230 chunk = alloc_trash_chunk();
Christopher Fauletb8a53712019-12-16 11:29:38 +01002231 if (!chunk) {
2232 if (!(s->flags & SF_ERR_MASK))
2233 s->flags |= SF_ERR_RESOURCE;
Christopher Faulet99daf282018-11-28 22:58:13 +01002234 goto fail;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002235 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002236
Christopher Faulet99daf282018-11-28 22:58:13 +01002237 /*
2238 * Create the location
2239 */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002240 htx = htxbuf(&req->buf);
Christopher Fauletf2824e62018-10-01 12:12:37 +02002241 switch(rule->type) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002242 case REDIRECT_TYPE_SCHEME: {
2243 struct http_hdr_ctx ctx;
2244 struct ist path, host;
Amaury Denoyellec453f952021-07-06 11:40:12 +02002245 struct http_uri_parser parser;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002246
Christopher Faulet99daf282018-11-28 22:58:13 +01002247 host = ist("");
2248 ctx.blk = NULL;
2249 if (http_find_header(htx, ist("Host"), &ctx, 0))
2250 host = ctx.value;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002251
Christopher Faulet297fbb42019-05-13 14:41:27 +02002252 sl = http_get_stline(htx);
Amaury Denoyellec453f952021-07-06 11:40:12 +02002253 parser = http_uri_parser_init(htx_sl_req_uri(sl));
2254 path = http_parse_path(&parser);
Christopher Faulet99daf282018-11-28 22:58:13 +01002255 /* build message using path */
Tim Duesterhused526372020-03-05 17:56:33 +01002256 if (isttest(path)) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002257 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2258 int qs = 0;
2259 while (qs < path.len) {
2260 if (*(path.ptr + qs) == '?') {
2261 path.len = qs;
2262 break;
2263 }
2264 qs++;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002265 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002266 }
2267 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002268 else
2269 path = ist("/");
Christopher Fauletf2824e62018-10-01 12:12:37 +02002270
Christopher Faulet99daf282018-11-28 22:58:13 +01002271 if (rule->rdr_str) { /* this is an old "redirect" rule */
2272 /* add scheme */
2273 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2274 goto fail;
2275 }
2276 else {
2277 /* add scheme with executing log format */
2278 chunk->data += build_logline(s, chunk->area + chunk->data,
2279 chunk->size - chunk->data,
2280 &rule->rdr_fmt);
2281 }
2282 /* add "://" + host + path */
2283 if (!chunk_memcat(chunk, "://", 3) ||
2284 !chunk_memcat(chunk, host.ptr, host.len) ||
2285 !chunk_memcat(chunk, path.ptr, path.len))
2286 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002287
Christopher Faulet99daf282018-11-28 22:58:13 +01002288 /* append a slash at the end of the location if needed and missing */
2289 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
2290 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
2291 if (chunk->data + 1 >= chunk->size)
2292 goto fail;
2293 chunk->area[chunk->data++] = '/';
2294 }
2295 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002296 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002297
Christopher Faulet99daf282018-11-28 22:58:13 +01002298 case REDIRECT_TYPE_PREFIX: {
2299 struct ist path;
Amaury Denoyellec453f952021-07-06 11:40:12 +02002300 struct http_uri_parser parser;
Christopher Faulet99daf282018-11-28 22:58:13 +01002301
Christopher Faulet297fbb42019-05-13 14:41:27 +02002302 sl = http_get_stline(htx);
Amaury Denoyellec453f952021-07-06 11:40:12 +02002303 parser = http_uri_parser_init(htx_sl_req_uri(sl));
2304 path = http_parse_path(&parser);
Christopher Faulet99daf282018-11-28 22:58:13 +01002305 /* build message using path */
Tim Duesterhused526372020-03-05 17:56:33 +01002306 if (isttest(path)) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002307 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2308 int qs = 0;
2309 while (qs < path.len) {
2310 if (*(path.ptr + qs) == '?') {
2311 path.len = qs;
2312 break;
2313 }
2314 qs++;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002315 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002316 }
2317 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002318 else
2319 path = ist("/");
Christopher Fauletf2824e62018-10-01 12:12:37 +02002320
Christopher Faulet99daf282018-11-28 22:58:13 +01002321 if (rule->rdr_str) { /* this is an old "redirect" rule */
2322 /* add prefix. Note that if prefix == "/", we don't want to
2323 * add anything, otherwise it makes it hard for the user to
2324 * configure a self-redirection.
2325 */
2326 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
2327 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2328 goto fail;
2329 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002330 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002331 else {
2332 /* add prefix with executing log format */
2333 chunk->data += build_logline(s, chunk->area + chunk->data,
2334 chunk->size - chunk->data,
2335 &rule->rdr_fmt);
2336 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002337
Christopher Faulet99daf282018-11-28 22:58:13 +01002338 /* add path */
2339 if (!chunk_memcat(chunk, path.ptr, path.len))
2340 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002341
Christopher Faulet99daf282018-11-28 22:58:13 +01002342 /* append a slash at the end of the location if needed and missing */
2343 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
2344 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
2345 if (chunk->data + 1 >= chunk->size)
2346 goto fail;
2347 chunk->area[chunk->data++] = '/';
2348 }
2349 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002350 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002351 case REDIRECT_TYPE_LOCATION:
2352 default:
2353 if (rule->rdr_str) { /* this is an old "redirect" rule */
2354 /* add location */
2355 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2356 goto fail;
2357 }
2358 else {
2359 /* add location with executing log format */
Willy Tarreaubc1223b2021-09-02 16:54:33 +02002360 int len = build_logline(s, chunk->area + chunk->data,
2361 chunk->size - chunk->data,
2362 &rule->rdr_fmt);
Christopher Fauleteab17572022-04-26 20:34:38 +02002363 if (!len && rule->flags & REDIRECT_FLAG_IGNORE_EMPTY) {
2364 ret = 2;
2365 goto out;
2366 }
Willy Tarreaubc1223b2021-09-02 16:54:33 +02002367
2368 chunk->data += len;
Christopher Faulet99daf282018-11-28 22:58:13 +01002369 }
2370 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002371 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002372 location = ist2(chunk->area, chunk->data);
2373
2374 /*
2375 * Create the 30x response
2376 */
2377 switch (rule->code) {
2378 case 308:
2379 status = ist("308");
2380 reason = ist("Permanent Redirect");
2381 break;
2382 case 307:
2383 status = ist("307");
2384 reason = ist("Temporary Redirect");
2385 break;
2386 case 303:
2387 status = ist("303");
2388 reason = ist("See Other");
2389 break;
2390 case 301:
2391 status = ist("301");
2392 reason = ist("Moved Permanently");
2393 break;
2394 case 302:
2395 default:
2396 status = ist("302");
2397 reason = ist("Found");
2398 break;
2399 }
2400
Christopher Faulet08e66462019-05-23 16:44:59 +02002401 if (!(txn->req.flags & HTTP_MSGF_BODYLESS) && txn->req.msg_state != HTTP_MSG_DONE)
2402 close = 1;
2403
Christopher Faulet99daf282018-11-28 22:58:13 +01002404 htx = htx_from_buf(&res->buf);
Kevin Zhu96b36392020-01-07 09:42:55 +01002405 /* Trim any possible response */
2406 channel_htx_truncate(&s->res, htx);
Christopher Faulet99daf282018-11-28 22:58:13 +01002407 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
2408 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), status, reason);
2409 if (!sl)
2410 goto fail;
2411 sl->info.res.status = rule->code;
2412 s->txn->status = rule->code;
2413
Christopher Faulet08e66462019-05-23 16:44:59 +02002414 if (close && !htx_add_header(htx, ist("Connection"), ist("close")))
2415 goto fail;
2416
2417 if (!htx_add_header(htx, ist("Content-length"), ist("0")) ||
Christopher Faulet99daf282018-11-28 22:58:13 +01002418 !htx_add_header(htx, ist("Location"), location))
2419 goto fail;
2420
2421 if (rule->code == 302 || rule->code == 303 || rule->code == 307) {
2422 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")))
2423 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002424 }
2425
2426 if (rule->cookie_len) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002427 if (!htx_add_header(htx, ist("Set-Cookie"), ist2(rule->cookie_str, rule->cookie_len)))
2428 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002429 }
2430
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002431 if (!htx_add_endof(htx, HTX_BLK_EOH))
Christopher Faulet99daf282018-11-28 22:58:13 +01002432 goto fail;
2433
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002434 htx->flags |= HTX_FL_EOM;
Kevin Zhu96b36392020-01-07 09:42:55 +01002435 htx_to_buf(htx, &res->buf);
Christopher Fauleta72a7e42020-01-28 09:28:11 +01002436 if (!http_forward_proxy_resp(s, 1))
2437 goto fail;
Christopher Faulet99daf282018-11-28 22:58:13 +01002438
Christopher Faulet60b33a52020-01-28 09:18:10 +01002439 if (rule->flags & REDIRECT_FLAG_FROM_REQ) {
2440 /* let's log the request time */
2441 s->logs.tv_request = now;
Christopher Fauletd3475882021-10-04 14:16:46 +02002442 req->analysers &= AN_REQ_FLT_END;
Christopher Faulet99daf282018-11-28 22:58:13 +01002443
Christopher Faulet60b33a52020-01-28 09:18:10 +01002444 if (s->sess->fe == s->be) /* report it if the request was intercepted by the frontend */
Willy Tarreau4781b152021-04-06 13:53:36 +02002445 _HA_ATOMIC_INC(&s->sess->fe->fe_counters.intercepted_req);
Christopher Faulet60b33a52020-01-28 09:18:10 +01002446 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002447
2448 if (!(s->flags & SF_ERR_MASK))
2449 s->flags |= SF_ERR_LOCAL;
Christopher Faulet4a66c942023-01-13 09:43:21 +01002450 http_set_term_flags(s);
Christopher Fauletf2824e62018-10-01 12:12:37 +02002451
Christopher Fauleteab17572022-04-26 20:34:38 +02002452 out:
Christopher Faulet99daf282018-11-28 22:58:13 +01002453 free_trash_chunk(chunk);
Christopher Fauleteab17572022-04-26 20:34:38 +02002454 return ret;
Christopher Faulet99daf282018-11-28 22:58:13 +01002455
2456 fail:
2457 /* If an error occurred, remove the incomplete HTTP response from the
2458 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01002459 channel_htx_truncate(res, htxbuf(&res->buf));
Christopher Fauleteab17572022-04-26 20:34:38 +02002460 ret = 0;
2461 goto out;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002462}
2463
Christopher Faulet18c13d32022-05-16 11:43:10 +02002464/* This function filters the request header names to only allow [0-9a-zA-Z-]
2465 * characters. Depending on the proxy configuration, headers with a name not
2466 * matching this charset are removed or the request is rejected with a
2467 * 403-Forbidden response if such name are found. It returns HTTP_RULE_RES_CONT
2468 * to continue the request processing or HTTP_RULE_RES_DENY if the request is
2469 * rejected.
2470 */
2471static enum rule_result http_req_restrict_header_names(struct stream *s, struct htx *htx, struct proxy *px)
2472{
2473 struct htx_blk *blk;
2474 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
2475
2476 blk = htx_get_first_blk(htx);
2477 while (blk) {
2478 enum htx_blk_type type = htx_get_blk_type(blk);
2479
2480 if (type == HTX_BLK_HDR) {
2481 struct ist n = htx_get_blk_name(htx, blk);
Mateusz Malek4b85a962022-08-17 14:22:09 +02002482 int i, end = istlen(n);
Christopher Faulet18c13d32022-05-16 11:43:10 +02002483
Mateusz Malek4b85a962022-08-17 14:22:09 +02002484 for (i = 0; i < end; i++) {
Christopher Faulet18c13d32022-05-16 11:43:10 +02002485 if (!isalnum((unsigned char)n.ptr[i]) && n.ptr[i] != '-') {
Mateusz Malek4b85a962022-08-17 14:22:09 +02002486 break;
Christopher Faulet18c13d32022-05-16 11:43:10 +02002487 }
2488 }
Mateusz Malek4b85a962022-08-17 14:22:09 +02002489
2490 if (i < end) {
2491 /* Disallowed character found - block the request or remove the header */
2492 if (px->options2 & PR_O2_RSTRICT_REQ_HDR_NAMES_BLK)
2493 goto block;
2494 blk = htx_remove_blk(htx, blk);
2495 continue;
2496 }
Christopher Faulet18c13d32022-05-16 11:43:10 +02002497 }
2498 if (type == HTX_BLK_EOH)
2499 break;
2500
2501 blk = htx_get_next_blk(htx, blk);
2502 }
2503 out:
2504 return rule_ret;
2505 block:
2506 /* Block the request returning a 403-Forbidden response */
2507 s->txn->status = 403;
2508 rule_ret = HTTP_RULE_RES_DENY;
2509 goto out;
2510}
2511
Christopher Faulet92d34fe2019-12-17 09:20:34 +01002512/* Replace all headers matching the name <name>. The header value is replaced if
2513 * it matches the regex <re>. <str> is used for the replacement. If <full> is
2514 * set to 1, the full-line is matched and replaced. Otherwise, comma-separated
2515 * values are evaluated one by one. It returns 0 on success and -1 on error.
2516 */
2517int http_replace_hdrs(struct stream* s, struct htx *htx, struct ist name,
2518 const char *str, struct my_regex *re, int full)
Christopher Faulet72333522018-10-24 11:25:02 +02002519{
2520 struct http_hdr_ctx ctx;
2521 struct buffer *output = get_trash_chunk();
2522
Christopher Faulet72333522018-10-24 11:25:02 +02002523 ctx.blk = NULL;
Christopher Faulet92d34fe2019-12-17 09:20:34 +01002524 while (http_find_header(htx, name, &ctx, full)) {
Christopher Faulet72333522018-10-24 11:25:02 +02002525 if (!regex_exec_match2(re, ctx.value.ptr, ctx.value.len, MAX_MATCH, pmatch, 0))
2526 continue;
2527
2528 output->data = exp_replace(output->area, output->size, ctx.value.ptr, str, pmatch);
2529 if (output->data == -1)
2530 return -1;
2531 if (!http_replace_header_value(htx, &ctx, ist2(output->area, output->data)))
2532 return -1;
2533 }
2534 return 0;
2535}
2536
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002537/* This function executes one of the set-{method,path,query,uri} actions. It
2538 * takes the string from the variable 'replace' with length 'len', then modifies
2539 * the relevant part of the request line accordingly. Then it updates various
2540 * pointers to the next elements which were moved, and the total buffer length.
2541 * It finds the action to be performed in p[2], previously filled by function
2542 * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal
2543 * error, though this can be revisited when this code is finally exploited.
2544 *
2545 * 'action' can be '0' to replace method, '1' to replace path, '2' to replace
Christopher Faulet312294f2020-09-02 17:17:44 +02002546 * query string, 3 to replace uri or 4 to replace the path+query.
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002547 *
2548 * In query string case, the mark question '?' must be set at the start of the
2549 * string by the caller, event if the replacement query string is empty.
2550 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002551int http_req_replace_stline(int action, const char *replace, int len,
2552 struct proxy *px, struct stream *s)
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002553{
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002554 struct htx *htx = htxbuf(&s->req.buf);
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002555
2556 switch (action) {
2557 case 0: // method
2558 if (!http_replace_req_meth(htx, ist2(replace, len)))
2559 return -1;
2560 break;
2561
2562 case 1: // path
Christopher Fauletb8ce5052020-08-31 16:11:57 +02002563 if (!http_replace_req_path(htx, ist2(replace, len), 0))
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002564 return -1;
2565 break;
2566
2567 case 2: // query
2568 if (!http_replace_req_query(htx, ist2(replace, len)))
2569 return -1;
2570 break;
2571
2572 case 3: // uri
2573 if (!http_replace_req_uri(htx, ist2(replace, len)))
2574 return -1;
2575 break;
2576
Christopher Faulet312294f2020-09-02 17:17:44 +02002577 case 4: // path + query
2578 if (!http_replace_req_path(htx, ist2(replace, len), 1))
2579 return -1;
2580 break;
2581
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002582 default:
2583 return -1;
2584 }
2585 return 0;
2586}
2587
2588/* This function replace the HTTP status code and the associated message. The
Christopher Faulete00d06c2019-12-16 17:18:42 +01002589 * variable <status> contains the new status code. This function never fails. It
2590 * returns 0 in case of success, -1 in case of internal error.
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002591 */
Christopher Faulet96bff762019-12-17 13:46:18 +01002592int http_res_set_status(unsigned int status, struct ist reason, struct stream *s)
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002593{
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002594 struct htx *htx = htxbuf(&s->res.buf);
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002595 char *res;
2596
2597 chunk_reset(&trash);
2598 res = ultoa_o(status, trash.area, trash.size);
2599 trash.data = res - trash.area;
2600
2601 /* Do we have a custom reason format string? */
Tim Duesterhuse296d3e2020-03-05 17:56:31 +01002602 if (!isttest(reason)) {
Christopher Faulet96bff762019-12-17 13:46:18 +01002603 const char *str = http_get_reason(status);
Tim Duesterhusdcf753a2021-03-04 17:31:47 +01002604 reason = ist(str);
Christopher Faulet96bff762019-12-17 13:46:18 +01002605 }
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002606
Christopher Fauletbde2c4c2020-08-31 16:43:34 +02002607 if (!http_replace_res_status(htx, ist2(trash.area, trash.data), reason))
Christopher Faulete00d06c2019-12-16 17:18:42 +01002608 return -1;
Willy Tarreau640e2532023-01-10 14:50:44 +01002609 s->txn->status = status;
Christopher Faulete00d06c2019-12-16 17:18:42 +01002610 return 0;
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002611}
2612
Christopher Faulet3e964192018-10-24 11:39:23 +02002613/* Executes the http-request rules <rules> for stream <s>, proxy <px> and
2614 * transaction <txn>. Returns the verdict of the first rule that prevents
2615 * further processing of the request (auth, deny, ...), and defaults to
2616 * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or
2617 * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT
2618 * on txn->flags if it encounters a tarpit rule. If <deny_status> is not NULL
2619 * and a deny/tarpit rule is matched, it will be filled with this rule's deny
2620 * status.
2621 */
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002622static enum rule_result http_req_get_intercept_rule(struct proxy *px, struct list *def_rules,
2623 struct list *rules, struct stream *s)
Christopher Faulet3e964192018-10-24 11:39:23 +02002624{
2625 struct session *sess = strm_sess(s);
2626 struct http_txn *txn = s->txn;
Christopher Faulet3e964192018-10-24 11:39:23 +02002627 struct act_rule *rule;
Christopher Faulet3e964192018-10-24 11:39:23 +02002628 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002629 int act_opts = 0;
Christopher Faulet3e964192018-10-24 11:39:23 +02002630
Christopher Faulet3e964192018-10-24 11:39:23 +02002631 /* If "the current_rule_list" match the executed rule list, we are in
2632 * resume condition. If a resume is needed it is always in the action
2633 * and never in the ACL or converters. In this case, we initialise the
2634 * current rule, and go to the action execution point.
2635 */
2636 if (s->current_rule) {
2637 rule = s->current_rule;
2638 s->current_rule = NULL;
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002639 if (s->current_rule_list == rules || (def_rules && s->current_rule_list == def_rules))
Christopher Faulet3e964192018-10-24 11:39:23 +02002640 goto resume_execution;
2641 }
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002642 s->current_rule_list = ((!def_rules || s->current_rule_list == def_rules) ? rules : def_rules);
Christopher Faulet3e964192018-10-24 11:39:23 +02002643
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002644 restart:
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002645 /* start the ruleset evaluation in strict mode */
2646 txn->req.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01002647
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002648 list_for_each_entry(rule, s->current_rule_list, list) {
Christopher Faulet3e964192018-10-24 11:39:23 +02002649 /* check optional condition */
2650 if (rule->cond) {
2651 int ret;
2652
2653 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
2654 ret = acl_pass(ret);
2655
2656 if (rule->cond->pol == ACL_COND_UNLESS)
2657 ret = !ret;
2658
2659 if (!ret) /* condition not matched */
2660 continue;
2661 }
2662
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002663 act_opts |= ACT_OPT_FIRST;
Christopher Faulet3e964192018-10-24 11:39:23 +02002664 resume_execution:
Amaury Denoyelle03517732021-05-07 14:25:01 +02002665 if (rule->kw->flags & KWF_EXPERIMENTAL)
2666 mark_tainted(TAINTED_ACTION_EXP_EXECUTED);
2667
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002668 /* Always call the action function if defined */
2669 if (rule->action_ptr) {
2670 if ((s->req.flags & CF_READ_ERROR) ||
Christopher Faulet446d8032022-12-12 07:53:18 +01002671 ((s->req.flags & CF_SHUTR) &&
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002672 (px->options & PR_O_ABRT_CLOSE)))
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002673 act_opts |= ACT_OPT_FINAL;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002674
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002675 switch (rule->action_ptr(rule, px, sess, s, act_opts)) {
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002676 case ACT_RET_CONT:
2677 break;
2678 case ACT_RET_STOP:
2679 rule_ret = HTTP_RULE_RES_STOP;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002680 s->last_rule_file = rule->conf.file;
2681 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002682 goto end;
2683 case ACT_RET_YIELD:
2684 s->current_rule = rule;
2685 rule_ret = HTTP_RULE_RES_YIELD;
2686 goto end;
2687 case ACT_RET_ERR:
2688 rule_ret = HTTP_RULE_RES_ERROR;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002689 s->last_rule_file = rule->conf.file;
2690 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002691 goto end;
2692 case ACT_RET_DONE:
2693 rule_ret = HTTP_RULE_RES_DONE;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002694 s->last_rule_file = rule->conf.file;
2695 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002696 goto end;
2697 case ACT_RET_DENY:
Christopher Fauletb58f62b2020-01-13 16:40:13 +01002698 if (txn->status == -1)
2699 txn->status = 403;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002700 rule_ret = HTTP_RULE_RES_DENY;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002701 s->last_rule_file = rule->conf.file;
2702 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002703 goto end;
2704 case ACT_RET_ABRT:
2705 rule_ret = HTTP_RULE_RES_ABRT;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002706 s->last_rule_file = rule->conf.file;
2707 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002708 goto end;
2709 case ACT_RET_INV:
2710 rule_ret = HTTP_RULE_RES_BADREQ;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002711 s->last_rule_file = rule->conf.file;
2712 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002713 goto end;
2714 }
2715 continue; /* eval the next rule */
2716 }
2717
2718 /* If not action function defined, check for known actions */
Christopher Faulet3e964192018-10-24 11:39:23 +02002719 switch (rule->action) {
2720 case ACT_ACTION_ALLOW:
2721 rule_ret = HTTP_RULE_RES_STOP;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002722 s->last_rule_file = rule->conf.file;
2723 s->last_rule_line = rule->conf.line;
Christopher Faulet3e964192018-10-24 11:39:23 +02002724 goto end;
2725
2726 case ACT_ACTION_DENY:
Christopher Faulet5cb513a2020-05-13 17:56:56 +02002727 txn->status = rule->arg.http_reply->status;
2728 txn->http_reply = rule->arg.http_reply;
Christopher Faulet3e964192018-10-24 11:39:23 +02002729 rule_ret = HTTP_RULE_RES_DENY;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002730 s->last_rule_file = rule->conf.file;
2731 s->last_rule_line = rule->conf.line;
Christopher Faulet3e964192018-10-24 11:39:23 +02002732 goto end;
2733
2734 case ACT_HTTP_REQ_TARPIT:
2735 txn->flags |= TX_CLTARPIT;
Christopher Faulet5cb513a2020-05-13 17:56:56 +02002736 txn->status = rule->arg.http_reply->status;
2737 txn->http_reply = rule->arg.http_reply;
Christopher Faulet3e964192018-10-24 11:39:23 +02002738 rule_ret = HTTP_RULE_RES_DENY;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002739 s->last_rule_file = rule->conf.file;
2740 s->last_rule_line = rule->conf.line;
Christopher Faulet3e964192018-10-24 11:39:23 +02002741 goto end;
2742
Willy Tarreaubc1223b2021-09-02 16:54:33 +02002743 case ACT_HTTP_REDIR: {
2744 int ret = http_apply_redirect_rule(rule->arg.redir, s, txn);
2745
2746 if (ret == 2) // 2 == skip
2747 break;
2748
2749 rule_ret = ret ? HTTP_RULE_RES_ABRT : HTTP_RULE_RES_ERROR;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002750 s->last_rule_file = rule->conf.file;
2751 s->last_rule_line = rule->conf.line;
Christopher Faulet3e964192018-10-24 11:39:23 +02002752 goto end;
Willy Tarreaubc1223b2021-09-02 16:54:33 +02002753 }
Christopher Faulet3e964192018-10-24 11:39:23 +02002754
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002755 /* other flags exists, but normally, they never be matched. */
Christopher Faulet3e964192018-10-24 11:39:23 +02002756 default:
2757 break;
2758 }
2759 }
2760
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002761 if (def_rules && s->current_rule_list == def_rules) {
2762 s->current_rule_list = rules;
2763 goto restart;
2764 }
2765
Christopher Faulet3e964192018-10-24 11:39:23 +02002766 end:
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002767 /* if the ruleset evaluation is finished reset the strict mode */
Christopher Faulet46f95542019-12-20 10:07:22 +01002768 if (rule_ret != HTTP_RULE_RES_YIELD)
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002769 txn->req.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01002770
Christopher Faulet3e964192018-10-24 11:39:23 +02002771 /* we reached the end of the rules, nothing to report */
2772 return rule_ret;
2773}
2774
2775/* Executes the http-response rules <rules> for stream <s> and proxy <px>. It
2776 * returns one of 5 possible statuses: HTTP_RULE_RES_CONT, HTTP_RULE_RES_STOP,
2777 * HTTP_RULE_RES_DONE, HTTP_RULE_RES_YIELD, or HTTP_RULE_RES_BADREQ. If *CONT
2778 * is returned, the process can continue the evaluation of next rule list. If
2779 * *STOP or *DONE is returned, the process must stop the evaluation. If *BADREQ
2780 * is returned, it means the operation could not be processed and a server error
Christopher Fauleta53abad2020-05-13 08:12:22 +02002781 * must be returned. If *YIELD is returned, the caller must call again the
2782 * function with the same context.
Christopher Faulet3e964192018-10-24 11:39:23 +02002783 */
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002784static enum rule_result http_res_get_intercept_rule(struct proxy *px, struct list *def_rules,
2785 struct list *rules, struct stream *s)
Christopher Faulet3e964192018-10-24 11:39:23 +02002786{
2787 struct session *sess = strm_sess(s);
2788 struct http_txn *txn = s->txn;
Christopher Faulet3e964192018-10-24 11:39:23 +02002789 struct act_rule *rule;
Christopher Faulet3e964192018-10-24 11:39:23 +02002790 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002791 int act_opts = 0;
Christopher Faulet3e964192018-10-24 11:39:23 +02002792
Christopher Faulet3e964192018-10-24 11:39:23 +02002793 /* If "the current_rule_list" match the executed rule list, we are in
2794 * resume condition. If a resume is needed it is always in the action
2795 * and never in the ACL or converters. In this case, we initialise the
2796 * current rule, and go to the action execution point.
2797 */
2798 if (s->current_rule) {
2799 rule = s->current_rule;
2800 s->current_rule = NULL;
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002801 if (s->current_rule_list == rules || (def_rules && s->current_rule_list == def_rules))
Christopher Faulet3e964192018-10-24 11:39:23 +02002802 goto resume_execution;
2803 }
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002804 s->current_rule_list = ((!def_rules || s->current_rule_list == def_rules) ? rules : def_rules);
2805
2806 restart:
Christopher Faulet3e964192018-10-24 11:39:23 +02002807
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002808 /* start the ruleset evaluation in strict mode */
2809 txn->rsp.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01002810
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002811 list_for_each_entry(rule, s->current_rule_list, list) {
Christopher Faulet3e964192018-10-24 11:39:23 +02002812 /* check optional condition */
2813 if (rule->cond) {
2814 int ret;
2815
2816 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
2817 ret = acl_pass(ret);
2818
2819 if (rule->cond->pol == ACL_COND_UNLESS)
2820 ret = !ret;
2821
2822 if (!ret) /* condition not matched */
2823 continue;
2824 }
2825
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002826 act_opts |= ACT_OPT_FIRST;
Christopher Faulet3e964192018-10-24 11:39:23 +02002827resume_execution:
Amaury Denoyelle03517732021-05-07 14:25:01 +02002828 if (rule->kw->flags & KWF_EXPERIMENTAL)
2829 mark_tainted(TAINTED_ACTION_EXP_EXECUTED);
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002830
2831 /* Always call the action function if defined */
2832 if (rule->action_ptr) {
2833 if ((s->req.flags & CF_READ_ERROR) ||
Christopher Faulet446d8032022-12-12 07:53:18 +01002834 ((s->req.flags & CF_SHUTR) &&
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002835 (px->options & PR_O_ABRT_CLOSE)))
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002836 act_opts |= ACT_OPT_FINAL;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002837
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002838 switch (rule->action_ptr(rule, px, sess, s, act_opts)) {
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002839 case ACT_RET_CONT:
2840 break;
2841 case ACT_RET_STOP:
2842 rule_ret = HTTP_RULE_RES_STOP;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002843 s->last_rule_file = rule->conf.file;
2844 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002845 goto end;
2846 case ACT_RET_YIELD:
2847 s->current_rule = rule;
2848 rule_ret = HTTP_RULE_RES_YIELD;
2849 goto end;
2850 case ACT_RET_ERR:
2851 rule_ret = HTTP_RULE_RES_ERROR;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002852 s->last_rule_file = rule->conf.file;
2853 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002854 goto end;
2855 case ACT_RET_DONE:
2856 rule_ret = HTTP_RULE_RES_DONE;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002857 s->last_rule_file = rule->conf.file;
2858 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002859 goto end;
2860 case ACT_RET_DENY:
Christopher Fauletb58f62b2020-01-13 16:40:13 +01002861 if (txn->status == -1)
2862 txn->status = 502;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002863 rule_ret = HTTP_RULE_RES_DENY;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002864 s->last_rule_file = rule->conf.file;
2865 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002866 goto end;
2867 case ACT_RET_ABRT:
2868 rule_ret = HTTP_RULE_RES_ABRT;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002869 s->last_rule_file = rule->conf.file;
2870 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002871 goto end;
2872 case ACT_RET_INV:
2873 rule_ret = HTTP_RULE_RES_BADREQ;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002874 s->last_rule_file = rule->conf.file;
2875 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002876 goto end;
2877 }
2878 continue; /* eval the next rule */
2879 }
2880
2881 /* If not action function defined, check for known actions */
Christopher Faulet3e964192018-10-24 11:39:23 +02002882 switch (rule->action) {
2883 case ACT_ACTION_ALLOW:
2884 rule_ret = HTTP_RULE_RES_STOP; /* "allow" rules are OK */
Willy Tarreauc6dae862022-03-09 17:23:10 +01002885 s->last_rule_file = rule->conf.file;
2886 s->last_rule_line = rule->conf.line;
Christopher Faulet3e964192018-10-24 11:39:23 +02002887 goto end;
2888
2889 case ACT_ACTION_DENY:
Christopher Faulet5cb513a2020-05-13 17:56:56 +02002890 txn->status = rule->arg.http_reply->status;
2891 txn->http_reply = rule->arg.http_reply;
Christopher Faulet3a26bee2019-12-16 12:47:40 +01002892 rule_ret = HTTP_RULE_RES_DENY;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002893 s->last_rule_file = rule->conf.file;
2894 s->last_rule_line = rule->conf.line;
Christopher Faulet3e964192018-10-24 11:39:23 +02002895 goto end;
2896
Willy Tarreaubc1223b2021-09-02 16:54:33 +02002897 case ACT_HTTP_REDIR: {
2898 int ret = http_apply_redirect_rule(rule->arg.redir, s, txn);
Christopher Faulet3e964192018-10-24 11:39:23 +02002899
Willy Tarreaubc1223b2021-09-02 16:54:33 +02002900 if (ret == 2) // 2 == skip
2901 break;
2902
2903 rule_ret = ret ? HTTP_RULE_RES_ABRT : HTTP_RULE_RES_ERROR;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002904 s->last_rule_file = rule->conf.file;
2905 s->last_rule_line = rule->conf.line;
Willy Tarreaubc1223b2021-09-02 16:54:33 +02002906 goto end;
2907 }
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002908 /* other flags exists, but normally, they never be matched. */
Christopher Faulet3e964192018-10-24 11:39:23 +02002909 default:
2910 break;
2911 }
2912 }
2913
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002914 if (def_rules && s->current_rule_list == def_rules) {
2915 s->current_rule_list = rules;
2916 goto restart;
2917 }
2918
Christopher Faulet3e964192018-10-24 11:39:23 +02002919 end:
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002920 /* if the ruleset evaluation is finished reset the strict mode */
Christopher Faulet46f95542019-12-20 10:07:22 +01002921 if (rule_ret != HTTP_RULE_RES_YIELD)
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002922 txn->rsp.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01002923
Christopher Faulet3e964192018-10-24 11:39:23 +02002924 /* we reached the end of the rules, nothing to report */
2925 return rule_ret;
2926}
2927
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002928/* Executes backend and frontend http-after-response rules for the stream <s>,
2929 * in that order. it return 1 on success and 0 on error. It is the caller
2930 * responsibility to catch error or ignore it. If it catches it, this function
2931 * may be called a second time, for the internal error.
2932 */
2933int http_eval_after_res_rules(struct stream *s)
2934{
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002935 struct list *def_rules, *rules;
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002936 struct session *sess = s->sess;
2937 enum rule_result ret = HTTP_RULE_RES_CONT;
2938
Christopher Faulet507479b2020-05-15 12:29:46 +02002939 /* Eval after-response ruleset only if the reply is not const */
2940 if (s->txn->flags & TX_CONST_REPLY)
2941 goto end;
2942
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002943 /* prune the request variables if not already done and swap to the response variables. */
2944 if (s->vars_reqres.scope != SCOPE_RES) {
2945 if (!LIST_ISEMPTY(&s->vars_reqres.head))
2946 vars_prune(&s->vars_reqres, s->sess, s);
Willy Tarreaub7bfcb32021-08-31 08:13:25 +02002947 vars_init_head(&s->vars_reqres, SCOPE_RES);
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002948 }
2949
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002950 def_rules = (s->be->defpx ? &s->be->defpx->http_after_res_rules : NULL);
2951 rules = &s->be->http_after_res_rules;
2952
2953 ret = http_res_get_intercept_rule(s->be, def_rules, rules, s);
Christopher Faulet4c5a5912021-11-09 17:48:39 +01002954 if ((ret == HTTP_RULE_RES_CONT || ret == HTTP_RULE_RES_STOP) && sess->fe != s->be) {
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002955 def_rules = ((sess->fe->defpx && sess->fe->defpx != s->be->defpx) ? &sess->fe->defpx->http_after_res_rules : NULL);
2956 rules = &sess->fe->http_after_res_rules;
2957 ret = http_res_get_intercept_rule(sess->fe, def_rules, rules, s);
2958 }
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002959
Christopher Faulet507479b2020-05-15 12:29:46 +02002960 end:
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002961 /* All other codes than CONTINUE, STOP or DONE are forbidden */
2962 return (ret == HTTP_RULE_RES_CONT || ret == HTTP_RULE_RES_STOP || ret == HTTP_RULE_RES_DONE);
2963}
2964
Christopher Fauletfcda7c62018-10-24 11:56:22 +02002965/*
2966 * Manage client-side cookie. It can impact performance by about 2% so it is
2967 * desirable to call it only when needed. This code is quite complex because
2968 * of the multiple very crappy and ambiguous syntaxes we have to support. it
2969 * highly recommended not to touch this part without a good reason !
2970 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002971static void http_manage_client_side_cookies(struct stream *s, struct channel *req)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02002972{
2973 struct session *sess = s->sess;
2974 struct http_txn *txn = s->txn;
2975 struct htx *htx;
2976 struct http_hdr_ctx ctx;
2977 char *hdr_beg, *hdr_end, *del_from;
2978 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
2979 int preserve_hdr;
2980
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002981 htx = htxbuf(&req->buf);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02002982 ctx.blk = NULL;
2983 while (http_find_header(htx, ist("Cookie"), &ctx, 1)) {
Olivier Houchardf0f42382019-07-22 17:43:46 +02002984 int is_first = 1;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02002985 del_from = NULL; /* nothing to be deleted */
2986 preserve_hdr = 0; /* assume we may kill the whole header */
2987
2988 /* Now look for cookies. Conforming to RFC2109, we have to support
2989 * attributes whose name begin with a '$', and associate them with
2990 * the right cookie, if we want to delete this cookie.
2991 * So there are 3 cases for each cookie read :
2992 * 1) it's a special attribute, beginning with a '$' : ignore it.
2993 * 2) it's a server id cookie that we *MAY* want to delete : save
2994 * some pointers on it (last semi-colon, beginning of cookie...)
2995 * 3) it's an application cookie : we *MAY* have to delete a previous
2996 * "special" cookie.
2997 * At the end of loop, if a "special" cookie remains, we may have to
2998 * remove it. If no application cookie persists in the header, we
2999 * *MUST* delete it.
3000 *
3001 * Note: RFC2965 is unclear about the processing of spaces around
3002 * the equal sign in the ATTR=VALUE form. A careful inspection of
3003 * the RFC explicitly allows spaces before it, and not within the
3004 * tokens (attrs or values). An inspection of RFC2109 allows that
3005 * too but section 10.1.3 lets one think that spaces may be allowed
3006 * after the equal sign too, resulting in some (rare) buggy
3007 * implementations trying to do that. So let's do what servers do.
3008 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
3009 * allowed quoted strings in values, with any possible character
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003010 * after a backslash, including control chars and delimiters, which
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003011 * causes parsing to become ambiguous. Browsers also allow spaces
3012 * within values even without quotes.
3013 *
3014 * We have to keep multiple pointers in order to support cookie
3015 * removal at the beginning, middle or end of header without
3016 * corrupting the header. All of these headers are valid :
3017 *
3018 * hdr_beg hdr_end
3019 * | |
3020 * v |
3021 * NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3 |
3022 * NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3 v
3023 * NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3
3024 * | | | | | | |
3025 * | | | | | | |
3026 * | | | | | | +--> next
3027 * | | | | | +----> val_end
3028 * | | | | +-----------> val_beg
3029 * | | | +--------------> equal
3030 * | | +----------------> att_end
3031 * | +---------------------> att_beg
3032 * +--------------------------> prev
3033 *
3034 */
3035 hdr_beg = ctx.value.ptr;
3036 hdr_end = hdr_beg + ctx.value.len;
3037 for (prev = hdr_beg; prev < hdr_end; prev = next) {
3038 /* Iterate through all cookies on this line */
3039
3040 /* find att_beg */
3041 att_beg = prev;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003042 if (!is_first)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003043 att_beg++;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003044 is_first = 0;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003045
3046 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
3047 att_beg++;
3048
3049 /* find att_end : this is the first character after the last non
3050 * space before the equal. It may be equal to hdr_end.
3051 */
3052 equal = att_end = att_beg;
3053 while (equal < hdr_end) {
3054 if (*equal == '=' || *equal == ',' || *equal == ';')
3055 break;
3056 if (HTTP_IS_SPHT(*equal++))
3057 continue;
3058 att_end = equal;
3059 }
3060
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003061 /* here, <equal> points to '=', a delimiter or the end. <att_end>
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003062 * is between <att_beg> and <equal>, both may be identical.
3063 */
3064 /* look for end of cookie if there is an equal sign */
3065 if (equal < hdr_end && *equal == '=') {
3066 /* look for the beginning of the value */
3067 val_beg = equal + 1;
3068 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
3069 val_beg++;
3070
3071 /* find the end of the value, respecting quotes */
3072 next = http_find_cookie_value_end(val_beg, hdr_end);
3073
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003074 /* make val_end point to the first white space or delimiter after the value */
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003075 val_end = next;
3076 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
3077 val_end--;
3078 }
3079 else
3080 val_beg = val_end = next = equal;
3081
3082 /* We have nothing to do with attributes beginning with
3083 * '$'. However, they will automatically be removed if a
3084 * header before them is removed, since they're supposed
3085 * to be linked together.
3086 */
3087 if (*att_beg == '$')
3088 continue;
3089
3090 /* Ignore cookies with no equal sign */
3091 if (equal == next) {
3092 /* This is not our cookie, so we must preserve it. But if we already
3093 * scheduled another cookie for removal, we cannot remove the
3094 * complete header, but we can remove the previous block itself.
3095 */
3096 preserve_hdr = 1;
3097 if (del_from != NULL) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003098 int delta = http_del_hdr_value(hdr_beg, hdr_end, &del_from, prev);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003099 val_end += delta;
3100 next += delta;
3101 hdr_end += delta;
3102 prev = del_from;
3103 del_from = NULL;
3104 }
3105 continue;
3106 }
3107
3108 /* if there are spaces around the equal sign, we need to
3109 * strip them otherwise we'll get trouble for cookie captures,
3110 * or even for rewrites. Since this happens extremely rarely,
3111 * it does not hurt performance.
3112 */
3113 if (unlikely(att_end != equal || val_beg > equal + 1)) {
3114 int stripped_before = 0;
3115 int stripped_after = 0;
3116
3117 if (att_end != equal) {
3118 memmove(att_end, equal, hdr_end - equal);
3119 stripped_before = (att_end - equal);
3120 equal += stripped_before;
3121 val_beg += stripped_before;
3122 }
3123
3124 if (val_beg > equal + 1) {
3125 memmove(equal + 1, val_beg, hdr_end + stripped_before - val_beg);
3126 stripped_after = (equal + 1) - val_beg;
3127 val_beg += stripped_after;
3128 stripped_before += stripped_after;
3129 }
3130
3131 val_end += stripped_before;
3132 next += stripped_before;
3133 hdr_end += stripped_before;
3134 }
3135 /* now everything is as on the diagram above */
3136
3137 /* First, let's see if we want to capture this cookie. We check
3138 * that we don't already have a client side cookie, because we
3139 * can only capture one. Also as an optimisation, we ignore
3140 * cookies shorter than the declared name.
3141 */
3142 if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL &&
3143 (val_end - att_beg >= sess->fe->capture_namelen) &&
3144 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
3145 int log_len = val_end - att_beg;
3146
3147 if ((txn->cli_cookie = pool_alloc(pool_head_capture)) == NULL) {
3148 ha_alert("HTTP logging : out of memory.\n");
3149 } else {
3150 if (log_len > sess->fe->capture_len)
3151 log_len = sess->fe->capture_len;
3152 memcpy(txn->cli_cookie, att_beg, log_len);
3153 txn->cli_cookie[log_len] = 0;
3154 }
3155 }
3156
3157 /* Persistence cookies in passive, rewrite or insert mode have the
3158 * following form :
3159 *
3160 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
3161 *
3162 * For cookies in prefix mode, the form is :
3163 *
3164 * Cookie: NAME=SRV~VALUE
3165 */
3166 if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
3167 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
3168 struct server *srv = s->be->srv;
3169 char *delim;
3170
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003171 /* if we're in cookie prefix mode, we'll search the delimiter so that we
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003172 * have the server ID between val_beg and delim, and the original cookie between
3173 * delim+1 and val_end. Otherwise, delim==val_end :
3174 *
3175 * hdr_beg
3176 * |
3177 * v
3178 * NAME=SRV; # in all but prefix modes
3179 * NAME=SRV~OPAQUE ; # in prefix mode
3180 * || || | |+-> next
3181 * || || | +--> val_end
3182 * || || +---------> delim
3183 * || |+------------> val_beg
3184 * || +-------------> att_end = equal
3185 * |+-----------------> att_beg
3186 * +------------------> prev
3187 *
3188 */
3189 if (s->be->ck_opts & PR_CK_PFX) {
3190 for (delim = val_beg; delim < val_end; delim++)
3191 if (*delim == COOKIE_DELIM)
3192 break;
3193 }
3194 else {
3195 char *vbar1;
3196 delim = val_end;
3197 /* Now check if the cookie contains a date field, which would
3198 * appear after a vertical bar ('|') just after the server name
3199 * and before the delimiter.
3200 */
3201 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
3202 if (vbar1) {
3203 /* OK, so left of the bar is the server's cookie and
3204 * right is the last seen date. It is a base64 encoded
3205 * 30-bit value representing the UNIX date since the
3206 * epoch in 4-second quantities.
3207 */
3208 int val;
3209 delim = vbar1++;
3210 if (val_end - vbar1 >= 5) {
3211 val = b64tos30(vbar1);
3212 if (val > 0)
3213 txn->cookie_last_date = val << 2;
3214 }
3215 /* look for a second vertical bar */
3216 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
3217 if (vbar1 && (val_end - vbar1 > 5)) {
3218 val = b64tos30(vbar1 + 1);
3219 if (val > 0)
3220 txn->cookie_first_date = val << 2;
3221 }
3222 }
3223 }
3224
3225 /* if the cookie has an expiration date and the proxy wants to check
3226 * it, then we do that now. We first check if the cookie is too old,
3227 * then only if it has expired. We detect strict overflow because the
3228 * time resolution here is not great (4 seconds). Cookies with dates
3229 * in the future are ignored if their offset is beyond one day. This
3230 * allows an admin to fix timezone issues without expiring everyone
3231 * and at the same time avoids keeping unwanted side effects for too
3232 * long.
3233 */
3234 if (txn->cookie_first_date && s->be->cookie_maxlife &&
3235 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) ||
3236 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
3237 txn->flags &= ~TX_CK_MASK;
3238 txn->flags |= TX_CK_OLD;
3239 delim = val_beg; // let's pretend we have not found the cookie
3240 txn->cookie_first_date = 0;
3241 txn->cookie_last_date = 0;
3242 }
3243 else if (txn->cookie_last_date && s->be->cookie_maxidle &&
3244 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) ||
3245 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
3246 txn->flags &= ~TX_CK_MASK;
3247 txn->flags |= TX_CK_EXPIRED;
3248 delim = val_beg; // let's pretend we have not found the cookie
3249 txn->cookie_first_date = 0;
3250 txn->cookie_last_date = 0;
3251 }
3252
3253 /* Here, we'll look for the first running server which supports the cookie.
3254 * This allows to share a same cookie between several servers, for example
3255 * to dedicate backup servers to specific servers only.
3256 * However, to prevent clients from sticking to cookie-less backup server
3257 * when they have incidentely learned an empty cookie, we simply ignore
3258 * empty cookies and mark them as invalid.
3259 * The same behaviour is applied when persistence must be ignored.
3260 */
3261 if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
3262 srv = NULL;
3263
3264 while (srv) {
3265 if (srv->cookie && (srv->cklen == delim - val_beg) &&
3266 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
3267 if ((srv->cur_state != SRV_ST_STOPPED) ||
3268 (s->be->options & PR_O_PERSIST) ||
3269 (s->flags & SF_FORCE_PRST)) {
3270 /* we found the server and we can use it */
3271 txn->flags &= ~TX_CK_MASK;
3272 txn->flags |= (srv->cur_state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
3273 s->flags |= SF_DIRECT | SF_ASSIGNED;
3274 s->target = &srv->obj_type;
3275 break;
3276 } else {
3277 /* we found a server, but it's down,
3278 * mark it as such and go on in case
3279 * another one is available.
3280 */
3281 txn->flags &= ~TX_CK_MASK;
3282 txn->flags |= TX_CK_DOWN;
3283 }
3284 }
3285 srv = srv->next;
3286 }
3287
3288 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
3289 /* no server matched this cookie or we deliberately skipped it */
3290 txn->flags &= ~TX_CK_MASK;
3291 if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
3292 txn->flags |= TX_CK_UNUSED;
3293 else
3294 txn->flags |= TX_CK_INVALID;
3295 }
3296
3297 /* depending on the cookie mode, we may have to either :
3298 * - delete the complete cookie if we're in insert+indirect mode, so that
3299 * the server never sees it ;
3300 * - remove the server id from the cookie value, and tag the cookie as an
Joseph Herlante9d5c722018-11-25 11:00:25 -08003301 * application cookie so that it does not get accidentally removed later,
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003302 * if we're in cookie prefix mode
3303 */
3304 if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
3305 int delta; /* negative */
3306
3307 memmove(val_beg, delim + 1, hdr_end - (delim + 1));
3308 delta = val_beg - (delim + 1);
3309 val_end += delta;
3310 next += delta;
3311 hdr_end += delta;
3312 del_from = NULL;
3313 preserve_hdr = 1; /* we want to keep this cookie */
3314 }
3315 else if (del_from == NULL &&
3316 (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
3317 del_from = prev;
3318 }
3319 }
3320 else {
3321 /* This is not our cookie, so we must preserve it. But if we already
3322 * scheduled another cookie for removal, we cannot remove the
3323 * complete header, but we can remove the previous block itself.
3324 */
3325 preserve_hdr = 1;
3326
3327 if (del_from != NULL) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003328 int delta = http_del_hdr_value(hdr_beg, hdr_end, &del_from, prev);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003329 if (att_beg >= del_from)
3330 att_beg += delta;
3331 if (att_end >= del_from)
3332 att_end += delta;
3333 val_beg += delta;
3334 val_end += delta;
3335 next += delta;
3336 hdr_end += delta;
3337 prev = del_from;
3338 del_from = NULL;
3339 }
3340 }
3341
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003342 } /* for each cookie */
3343
3344
3345 /* There are no more cookies on this line.
3346 * We may still have one (or several) marked for deletion at the
3347 * end of the line. We must do this now in two ways :
3348 * - if some cookies must be preserved, we only delete from the
3349 * mark to the end of line ;
3350 * - if nothing needs to be preserved, simply delete the whole header
3351 */
3352 if (del_from) {
3353 hdr_end = (preserve_hdr ? del_from : hdr_beg);
3354 }
3355 if ((hdr_end - hdr_beg) != ctx.value.len) {
Christopher Faulet3e2638e2019-06-18 09:49:16 +02003356 if (hdr_beg != hdr_end)
3357 htx_change_blk_value_len(htx, ctx.blk, hdr_end - hdr_beg);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003358 else
3359 http_remove_header(htx, &ctx);
3360 }
3361 } /* for each "Cookie header */
3362}
3363
3364/*
3365 * Manage server-side cookies. It can impact performance by about 2% so it is
3366 * desirable to call it only when needed. This function is also used when we
3367 * just need to know if there is a cookie (eg: for check-cache).
3368 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003369static void http_manage_server_side_cookies(struct stream *s, struct channel *res)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003370{
3371 struct session *sess = s->sess;
3372 struct http_txn *txn = s->txn;
3373 struct htx *htx;
3374 struct http_hdr_ctx ctx;
3375 struct server *srv;
3376 char *hdr_beg, *hdr_end;
3377 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003378
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003379 htx = htxbuf(&res->buf);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003380
3381 ctx.blk = NULL;
Willy Tarreauaa1909e2022-11-14 18:58:35 +01003382 while (http_find_header(htx, ist("Set-Cookie"), &ctx, 1)) {
Olivier Houchardf0f42382019-07-22 17:43:46 +02003383 int is_first = 1;
3384
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003385 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
3386 * <prev> points to the colon.
3387 */
3388 txn->flags |= TX_SCK_PRESENT;
3389
3390 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
3391 * check-cache is enabled) and we are not interested in checking
3392 * them. Warning, the cookie capture is declared in the frontend.
3393 */
3394 if (s->be->cookie_name == NULL && sess->fe->capture_name == NULL)
3395 break;
3396
3397 /* OK so now we know we have to process this response cookie.
3398 * The format of the Set-Cookie header is slightly different
3399 * from the format of the Cookie header in that it does not
3400 * support the comma as a cookie delimiter (thus the header
3401 * cannot be folded) because the Expires attribute described in
3402 * the original Netscape's spec may contain an unquoted date
3403 * with a comma inside. We have to live with this because
3404 * many browsers don't support Max-Age and some browsers don't
3405 * support quoted strings. However the Set-Cookie2 header is
Willy Tarreauaa1909e2022-11-14 18:58:35 +01003406 * clean but basically nobody supports it.
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003407 *
3408 * We have to keep multiple pointers in order to support cookie
3409 * removal at the beginning, middle or end of header without
3410 * corrupting the header (in case of set-cookie2). A special
3411 * pointer, <scav> points to the beginning of the set-cookie-av
3412 * fields after the first semi-colon. The <next> pointer points
3413 * either to the end of line (set-cookie) or next unquoted comma
3414 * (set-cookie2). All of these headers are valid :
3415 *
3416 * hdr_beg hdr_end
3417 * | |
3418 * v |
3419 * NAME1 = VALUE 1 ; Secure; Path="/" |
3420 * NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT v
3421 * NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT
3422 * NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard
3423 * | | | | | | | |
3424 * | | | | | | | +-> next
3425 * | | | | | | +------------> scav
3426 * | | | | | +--------------> val_end
3427 * | | | | +--------------------> val_beg
3428 * | | | +----------------------> equal
3429 * | | +------------------------> att_end
3430 * | +----------------------------> att_beg
3431 * +------------------------------> prev
3432 * -------------------------------> hdr_beg
3433 */
3434 hdr_beg = ctx.value.ptr;
3435 hdr_end = hdr_beg + ctx.value.len;
3436 for (prev = hdr_beg; prev < hdr_end; prev = next) {
3437
3438 /* Iterate through all cookies on this line */
3439
3440 /* find att_beg */
3441 att_beg = prev;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003442 if (!is_first)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003443 att_beg++;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003444 is_first = 0;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003445
3446 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
3447 att_beg++;
3448
3449 /* find att_end : this is the first character after the last non
3450 * space before the equal. It may be equal to hdr_end.
3451 */
3452 equal = att_end = att_beg;
3453
3454 while (equal < hdr_end) {
Willy Tarreauaa1909e2022-11-14 18:58:35 +01003455 if (*equal == '=' || *equal == ';')
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003456 break;
3457 if (HTTP_IS_SPHT(*equal++))
3458 continue;
3459 att_end = equal;
3460 }
3461
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003462 /* here, <equal> points to '=', a delimiter or the end. <att_end>
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003463 * is between <att_beg> and <equal>, both may be identical.
3464 */
3465
3466 /* look for end of cookie if there is an equal sign */
3467 if (equal < hdr_end && *equal == '=') {
3468 /* look for the beginning of the value */
3469 val_beg = equal + 1;
3470 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
3471 val_beg++;
3472
3473 /* find the end of the value, respecting quotes */
3474 next = http_find_cookie_value_end(val_beg, hdr_end);
3475
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003476 /* make val_end point to the first white space or delimiter after the value */
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003477 val_end = next;
3478 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
3479 val_end--;
3480 }
3481 else {
3482 /* <equal> points to next comma, semi-colon or EOL */
3483 val_beg = val_end = next = equal;
3484 }
3485
3486 if (next < hdr_end) {
Willy Tarreauaa1909e2022-11-14 18:58:35 +01003487 /* For Set-Cookie, since commas are permitted
3488 * in values, skip to the end.
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003489 */
Willy Tarreauaa1909e2022-11-14 18:58:35 +01003490 next = hdr_end;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003491 }
3492
3493 /* Now everything is as on the diagram above */
3494
3495 /* Ignore cookies with no equal sign */
3496 if (equal == val_end)
3497 continue;
3498
3499 /* If there are spaces around the equal sign, we need to
3500 * strip them otherwise we'll get trouble for cookie captures,
3501 * or even for rewrites. Since this happens extremely rarely,
3502 * it does not hurt performance.
3503 */
3504 if (unlikely(att_end != equal || val_beg > equal + 1)) {
3505 int stripped_before = 0;
3506 int stripped_after = 0;
3507
3508 if (att_end != equal) {
3509 memmove(att_end, equal, hdr_end - equal);
3510 stripped_before = (att_end - equal);
3511 equal += stripped_before;
3512 val_beg += stripped_before;
3513 }
3514
3515 if (val_beg > equal + 1) {
3516 memmove(equal + 1, val_beg, hdr_end + stripped_before - val_beg);
3517 stripped_after = (equal + 1) - val_beg;
3518 val_beg += stripped_after;
3519 stripped_before += stripped_after;
3520 }
3521
3522 val_end += stripped_before;
3523 next += stripped_before;
3524 hdr_end += stripped_before;
3525
Christopher Faulet3e2638e2019-06-18 09:49:16 +02003526 htx_change_blk_value_len(htx, ctx.blk, hdr_end - hdr_beg);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003527 ctx.value.len = hdr_end - hdr_beg;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003528 }
3529
3530 /* First, let's see if we want to capture this cookie. We check
3531 * that we don't already have a server side cookie, because we
3532 * can only capture one. Also as an optimisation, we ignore
3533 * cookies shorter than the declared name.
3534 */
3535 if (sess->fe->capture_name != NULL &&
3536 txn->srv_cookie == NULL &&
3537 (val_end - att_beg >= sess->fe->capture_namelen) &&
3538 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
3539 int log_len = val_end - att_beg;
3540 if ((txn->srv_cookie = pool_alloc(pool_head_capture)) == NULL) {
3541 ha_alert("HTTP logging : out of memory.\n");
3542 }
3543 else {
3544 if (log_len > sess->fe->capture_len)
3545 log_len = sess->fe->capture_len;
3546 memcpy(txn->srv_cookie, att_beg, log_len);
3547 txn->srv_cookie[log_len] = 0;
3548 }
3549 }
3550
3551 srv = objt_server(s->target);
3552 /* now check if we need to process it for persistence */
3553 if (!(s->flags & SF_IGNORE_PRST) &&
3554 (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
3555 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
3556 /* assume passive cookie by default */
3557 txn->flags &= ~TX_SCK_MASK;
3558 txn->flags |= TX_SCK_FOUND;
3559
3560 /* If the cookie is in insert mode on a known server, we'll delete
3561 * this occurrence because we'll insert another one later.
3562 * We'll delete it too if the "indirect" option is set and we're in
3563 * a direct access.
3564 */
3565 if (s->be->ck_opts & PR_CK_PSV) {
3566 /* The "preserve" flag was set, we don't want to touch the
3567 * server's cookie.
3568 */
3569 }
3570 else if ((srv && (s->be->ck_opts & PR_CK_INS)) ||
3571 ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) {
3572 /* this cookie must be deleted */
3573 if (prev == hdr_beg && next == hdr_end) {
3574 /* whole header */
3575 http_remove_header(htx, &ctx);
3576 /* note: while both invalid now, <next> and <hdr_end>
3577 * are still equal, so the for() will stop as expected.
3578 */
3579 } else {
3580 /* just remove the value */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003581 int delta = http_del_hdr_value(hdr_beg, hdr_end, &prev, next);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003582 next = prev;
3583 hdr_end += delta;
3584 }
3585 txn->flags &= ~TX_SCK_MASK;
3586 txn->flags |= TX_SCK_DELETED;
3587 /* and go on with next cookie */
3588 }
3589 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) {
3590 /* replace bytes val_beg->val_end with the cookie name associated
3591 * with this server since we know it.
3592 */
3593 int sliding, delta;
3594
3595 ctx.value = ist2(val_beg, val_end - val_beg);
3596 ctx.lws_before = ctx.lws_after = 0;
3597 http_replace_header_value(htx, &ctx, ist2(srv->cookie, srv->cklen));
3598 delta = srv->cklen - (val_end - val_beg);
3599 sliding = (ctx.value.ptr - val_beg);
3600 hdr_beg += sliding;
3601 val_beg += sliding;
3602 next += sliding + delta;
3603 hdr_end += sliding + delta;
3604
3605 txn->flags &= ~TX_SCK_MASK;
3606 txn->flags |= TX_SCK_REPLACED;
3607 }
3608 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) {
3609 /* insert the cookie name associated with this server
3610 * before existing cookie, and insert a delimiter between them..
3611 */
3612 int sliding, delta;
3613 ctx.value = ist2(val_beg, 0);
3614 ctx.lws_before = ctx.lws_after = 0;
3615 http_replace_header_value(htx, &ctx, ist2(srv->cookie, srv->cklen + 1));
3616 delta = srv->cklen + 1;
3617 sliding = (ctx.value.ptr - val_beg);
3618 hdr_beg += sliding;
3619 val_beg += sliding;
3620 next += sliding + delta;
3621 hdr_end += sliding + delta;
3622
3623 val_beg[srv->cklen] = COOKIE_DELIM;
3624 txn->flags &= ~TX_SCK_MASK;
3625 txn->flags |= TX_SCK_REPLACED;
3626 }
3627 }
3628 /* that's done for this cookie, check the next one on the same
Willy Tarreauaa1909e2022-11-14 18:58:35 +01003629 * line when next != hdr_end (which should normally not happen
3630 * with set-cookie2 support removed).
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003631 */
3632 }
3633 }
3634}
3635
Christopher Faulet25a02f62018-10-24 12:00:25 +02003636/*
3637 * Parses the Cache-Control and Pragma request header fields to determine if
3638 * the request may be served from the cache and/or if it is cacheable. Updates
3639 * s->txn->flags.
3640 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003641void http_check_request_for_cacheability(struct stream *s, struct channel *req)
Christopher Faulet25a02f62018-10-24 12:00:25 +02003642{
3643 struct http_txn *txn = s->txn;
3644 struct htx *htx;
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003645 struct http_hdr_ctx ctx = { .blk = NULL };
3646 int pragma_found, cc_found;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003647
3648 if ((txn->flags & (TX_CACHEABLE|TX_CACHE_IGNORE)) == TX_CACHE_IGNORE)
3649 return; /* nothing more to do here */
3650
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003651 htx = htxbuf(&req->buf);
Christopher Faulet25a02f62018-10-24 12:00:25 +02003652 pragma_found = cc_found = 0;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003653
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003654 /* Check "pragma" header for HTTP/1.0 compatibility. */
3655 if (http_find_header(htx, ist("pragma"), &ctx, 1)) {
3656 if (isteqi(ctx.value, ist("no-cache"))) {
3657 pragma_found = 1;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003658 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003659 }
Christopher Faulet25a02f62018-10-24 12:00:25 +02003660
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003661 ctx.blk = NULL;
3662 /* Don't use the cache and don't try to store if we found the
3663 * Authorization header */
3664 if (http_find_header(htx, ist("authorization"), &ctx, 1)) {
3665 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3666 txn->flags |= TX_CACHE_IGNORE;
3667 }
Christopher Faulet25a02f62018-10-24 12:00:25 +02003668
Christopher Faulet25a02f62018-10-24 12:00:25 +02003669
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003670 /* Look for "cache-control" header and iterate over all the values
3671 * until we find one that specifies that caching is possible or not. */
3672 ctx.blk = NULL;
3673 while (http_find_header(htx, ist("cache-control"), &ctx, 0)) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003674 cc_found = 1;
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003675 /* We don't check the values after max-age, max-stale nor min-fresh,
3676 * we simply don't use the cache when they're specified. */
3677 if (istmatchi(ctx.value, ist("max-age")) ||
3678 istmatchi(ctx.value, ist("no-cache")) ||
3679 istmatchi(ctx.value, ist("max-stale")) ||
3680 istmatchi(ctx.value, ist("min-fresh"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003681 txn->flags |= TX_CACHE_IGNORE;
3682 continue;
3683 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003684 if (istmatchi(ctx.value, ist("no-store"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003685 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3686 continue;
3687 }
3688 }
3689
3690 /* RFC7234#5.4:
3691 * When the Cache-Control header field is also present and
3692 * understood in a request, Pragma is ignored.
3693 * When the Cache-Control header field is not present in a
3694 * request, caches MUST consider the no-cache request
3695 * pragma-directive as having the same effect as if
3696 * "Cache-Control: no-cache" were present.
3697 */
3698 if (!cc_found && pragma_found)
3699 txn->flags |= TX_CACHE_IGNORE;
3700}
3701
3702/*
3703 * Check if response is cacheable or not. Updates s->txn->flags.
3704 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003705void http_check_response_for_cacheability(struct stream *s, struct channel *res)
Christopher Faulet25a02f62018-10-24 12:00:25 +02003706{
3707 struct http_txn *txn = s->txn;
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003708 struct http_hdr_ctx ctx = { .blk = NULL };
Christopher Faulet25a02f62018-10-24 12:00:25 +02003709 struct htx *htx;
Remi Tricot-Le Bretoncc9bf2e2020-11-12 11:14:41 +01003710 int has_freshness_info = 0;
3711 int has_validator = 0;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003712
3713 if (txn->status < 200) {
3714 /* do not try to cache interim responses! */
3715 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3716 return;
3717 }
3718
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003719 htx = htxbuf(&res->buf);
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003720 /* Check "pragma" header for HTTP/1.0 compatibility. */
3721 if (http_find_header(htx, ist("pragma"), &ctx, 1)) {
3722 if (isteqi(ctx.value, ist("no-cache"))) {
3723 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3724 return;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003725 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003726 }
Christopher Faulet25a02f62018-10-24 12:00:25 +02003727
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003728 /* Look for "cache-control" header and iterate over all the values
3729 * until we find one that specifies that caching is possible or not. */
3730 ctx.blk = NULL;
3731 while (http_find_header(htx, ist("cache-control"), &ctx, 0)) {
3732 if (isteqi(ctx.value, ist("public"))) {
3733 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003734 continue;
3735 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003736 if (isteqi(ctx.value, ist("private")) ||
3737 isteqi(ctx.value, ist("no-cache")) ||
3738 isteqi(ctx.value, ist("no-store")) ||
3739 isteqi(ctx.value, ist("max-age=0")) ||
3740 isteqi(ctx.value, ist("s-maxage=0"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003741 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003742 continue;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003743 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003744 /* We might have a no-cache="set-cookie" form. */
3745 if (istmatchi(ctx.value, ist("no-cache=\"set-cookie"))) {
3746 txn->flags &= ~TX_CACHE_COOK;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003747 continue;
3748 }
Remi Tricot-Le Bretoncc9bf2e2020-11-12 11:14:41 +01003749
3750 if (istmatchi(ctx.value, ist("s-maxage")) ||
3751 istmatchi(ctx.value, ist("max-age"))) {
3752 has_freshness_info = 1;
3753 continue;
3754 }
3755 }
3756
3757 /* If no freshness information could be found in Cache-Control values,
3758 * look for an Expires header. */
3759 if (!has_freshness_info) {
3760 ctx.blk = NULL;
3761 has_freshness_info = http_find_header(htx, ist("expires"), &ctx, 0);
Christopher Faulet25a02f62018-10-24 12:00:25 +02003762 }
Remi Tricot-Le Bretoncc9bf2e2020-11-12 11:14:41 +01003763
3764 /* If no freshness information could be found in Cache-Control or Expires
3765 * values, look for an explicit validator. */
3766 if (!has_freshness_info) {
3767 ctx.blk = NULL;
3768 has_validator = 1;
3769 if (!http_find_header(htx, ist("etag"), &ctx, 0)) {
3770 ctx.blk = NULL;
3771 if (!http_find_header(htx, ist("last-modified"), &ctx, 0))
3772 has_validator = 0;
3773 }
3774 }
3775
3776 /* We won't store an entry that has neither a cache validator nor an
3777 * explicit expiration time, as suggested in RFC 7234#3. */
3778 if (!has_freshness_info && !has_validator)
3779 txn->flags |= TX_CACHE_IGNORE;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003780}
3781
Christopher Faulet377c5a52018-10-24 21:21:30 +02003782/*
3783 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
3784 * for the current backend.
3785 *
3786 * It is assumed that the request is either a HEAD, GET, or POST and that the
3787 * uri_auth field is valid.
3788 *
3789 * Returns 1 if stats should be provided, otherwise 0.
3790 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003791static int http_stats_check_uri(struct stream *s, struct http_txn *txn, struct proxy *backend)
Christopher Faulet377c5a52018-10-24 21:21:30 +02003792{
3793 struct uri_auth *uri_auth = backend->uri_auth;
3794 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003795 struct htx_sl *sl;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003796 struct ist uri;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003797
3798 if (!uri_auth)
3799 return 0;
3800
3801 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
3802 return 0;
3803
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003804 htx = htxbuf(&s->req.buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02003805 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003806 uri = htx_sl_req_uri(sl);
Amaury Denoyellec453f952021-07-06 11:40:12 +02003807 if (*uri_auth->uri_prefix == '/') {
3808 struct http_uri_parser parser = http_uri_parser_init(uri);
3809 uri = http_parse_path(&parser);
3810 }
Christopher Faulet377c5a52018-10-24 21:21:30 +02003811
3812 /* check URI size */
3813 if (uri_auth->uri_len > uri.len)
3814 return 0;
3815
3816 if (memcmp(uri.ptr, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
3817 return 0;
3818
3819 return 1;
3820}
3821
3822/* This function prepares an applet to handle the stats. It can deal with the
3823 * "100-continue" expectation, check that admin rules are met for POST requests,
3824 * and program a response message if something was unexpected. It cannot fail
3825 * and always relies on the stats applet to complete the job. It does not touch
3826 * analysers nor counters, which are left to the caller. It does not touch
3827 * s->target which is supposed to already point to the stats applet. The caller
3828 * is expected to have already assigned an appctx to the stream.
3829 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003830static int http_handle_stats(struct stream *s, struct channel *req)
Christopher Faulet377c5a52018-10-24 21:21:30 +02003831{
3832 struct stats_admin_rule *stats_admin_rule;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003833 struct session *sess = s->sess;
3834 struct http_txn *txn = s->txn;
3835 struct http_msg *msg = &txn->req;
3836 struct uri_auth *uri_auth = s->be->uri_auth;
3837 const char *h, *lookup, *end;
Willy Tarreau8e7c6e62022-05-18 17:58:02 +02003838 struct appctx *appctx = __sc_appctx(s->scb);
Willy Tarreau91cefca2022-05-03 17:08:29 +02003839 struct show_stat_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx));
Christopher Faulet377c5a52018-10-24 21:21:30 +02003840 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003841 struct htx_sl *sl;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003842
Willy Tarreau41f88522022-05-03 18:39:27 +02003843 appctx->st1 = 0;
Willy Tarreau6ef16482022-05-06 18:07:53 +02003844 ctx->state = STAT_STATE_INIT;
Willy Tarreau91cefca2022-05-03 17:08:29 +02003845 ctx->st_code = STAT_STATUS_INIT;
3846 ctx->flags |= uri_auth->flags;
3847 ctx->flags |= STAT_FMT_HTML; /* assume HTML mode by default */
Christopher Faulet377c5a52018-10-24 21:21:30 +02003848 if ((msg->flags & HTTP_MSGF_VER_11) && (txn->meth != HTTP_METH_HEAD))
Willy Tarreau91cefca2022-05-03 17:08:29 +02003849 ctx->flags |= STAT_CHUNKED;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003850
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003851 htx = htxbuf(&req->buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02003852 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003853 lookup = HTX_SL_REQ_UPTR(sl) + uri_auth->uri_len;
3854 end = HTX_SL_REQ_UPTR(sl) + HTX_SL_REQ_ULEN(sl);
Christopher Faulet377c5a52018-10-24 21:21:30 +02003855
3856 for (h = lookup; h <= end - 3; h++) {
3857 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02003858 ctx->flags |= STAT_HIDE_DOWN;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003859 break;
3860 }
Amaury Denoyelle91e55ea2021-02-25 14:46:08 +01003861 }
3862
3863 for (h = lookup; h <= end - 9; h++) {
3864 if (memcmp(h, ";no-maint", 9) == 0) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02003865 ctx->flags |= STAT_HIDE_MAINT;
Willy Tarreau3e320362020-10-23 17:28:57 +02003866 break;
3867 }
Christopher Faulet377c5a52018-10-24 21:21:30 +02003868 }
3869
3870 if (uri_auth->refresh) {
3871 for (h = lookup; h <= end - 10; h++) {
3872 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02003873 ctx->flags |= STAT_NO_REFRESH;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003874 break;
3875 }
3876 }
3877 }
3878
3879 for (h = lookup; h <= end - 4; h++) {
3880 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02003881 ctx->flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
Christopher Faulet377c5a52018-10-24 21:21:30 +02003882 break;
3883 }
3884 }
3885
3886 for (h = lookup; h <= end - 6; h++) {
3887 if (memcmp(h, ";typed", 6) == 0) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02003888 ctx->flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
3889 ctx->flags |= STAT_FMT_TYPED;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003890 break;
3891 }
3892 }
3893
Christopher Faulet6338a082019-09-09 15:50:54 +02003894 for (h = lookup; h <= end - 5; h++) {
3895 if (memcmp(h, ";json", 5) == 0) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02003896 ctx->flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
3897 ctx->flags |= STAT_FMT_JSON;
Christopher Faulet6338a082019-09-09 15:50:54 +02003898 break;
3899 }
3900 }
3901
3902 for (h = lookup; h <= end - 12; h++) {
3903 if (memcmp(h, ";json-schema", 12) == 0) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02003904 ctx->flags &= ~STAT_FMT_MASK;
3905 ctx->flags |= STAT_JSON_SCHM;
Christopher Faulet6338a082019-09-09 15:50:54 +02003906 break;
3907 }
3908 }
3909
Christopher Faulet377c5a52018-10-24 21:21:30 +02003910 for (h = lookup; h <= end - 8; h++) {
3911 if (memcmp(h, ";st=", 4) == 0) {
3912 int i;
3913 h += 4;
Willy Tarreau91cefca2022-05-03 17:08:29 +02003914 ctx->st_code = STAT_STATUS_UNKN;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003915 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
3916 if (strncmp(stat_status_codes[i], h, 4) == 0) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02003917 ctx->st_code = i;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003918 break;
3919 }
3920 }
3921 break;
3922 }
3923 }
3924
Willy Tarreau91cefca2022-05-03 17:08:29 +02003925 ctx->scope_str = 0;
3926 ctx->scope_len = 0;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003927 for (h = lookup; h <= end - 8; h++) {
3928 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
3929 int itx = 0;
3930 const char *h2;
3931 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
3932 const char *err;
3933
3934 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
3935 h2 = h;
Willy Tarreau91cefca2022-05-03 17:08:29 +02003936 ctx->scope_str = h2 - HTX_SL_REQ_UPTR(sl);
Christopher Fauleted7a0662019-01-14 11:07:34 +01003937 while (h < end) {
Christopher Faulet377c5a52018-10-24 21:21:30 +02003938 if (*h == ';' || *h == '&' || *h == ' ')
3939 break;
3940 itx++;
3941 h++;
3942 }
3943
3944 if (itx > STAT_SCOPE_TXT_MAXLEN)
3945 itx = STAT_SCOPE_TXT_MAXLEN;
Willy Tarreau91cefca2022-05-03 17:08:29 +02003946 ctx->scope_len = itx;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003947
Willy Tarreau91cefca2022-05-03 17:08:29 +02003948 /* scope_txt = search query, ctx->scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Christopher Faulet377c5a52018-10-24 21:21:30 +02003949 memcpy(scope_txt, h2, itx);
3950 scope_txt[itx] = '\0';
3951 err = invalid_char(scope_txt);
3952 if (err) {
3953 /* bad char in search text => clear scope */
Willy Tarreau91cefca2022-05-03 17:08:29 +02003954 ctx->scope_str = 0;
3955 ctx->scope_len = 0;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003956 }
3957 break;
3958 }
3959 }
3960
3961 /* now check whether we have some admin rules for this request */
3962 list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) {
3963 int ret = 1;
3964
3965 if (stats_admin_rule->cond) {
3966 ret = acl_exec_cond(stats_admin_rule->cond, s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
3967 ret = acl_pass(ret);
3968 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
3969 ret = !ret;
3970 }
3971
3972 if (ret) {
3973 /* no rule, or the rule matches */
Willy Tarreau91cefca2022-05-03 17:08:29 +02003974 ctx->flags |= STAT_ADMIN;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003975 break;
3976 }
3977 }
3978
Christopher Faulet5d45e382019-02-27 15:15:23 +01003979 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
3980 appctx->st0 = STAT_HTTP_HEAD;
3981 else if (txn->meth == HTTP_METH_POST) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02003982 if (ctx->flags & STAT_ADMIN) {
Christopher Faulet377c5a52018-10-24 21:21:30 +02003983 appctx->st0 = STAT_HTTP_POST;
Christopher Fauletbd9e8422019-08-15 22:26:48 +02003984 if (msg->msg_state < HTTP_MSG_DATA)
3985 req->analysers |= AN_REQ_HTTP_BODY;
3986 }
Christopher Faulet377c5a52018-10-24 21:21:30 +02003987 else {
Christopher Faulet5d45e382019-02-27 15:15:23 +01003988 /* POST without admin level */
Willy Tarreau91cefca2022-05-03 17:08:29 +02003989 ctx->flags &= ~STAT_CHUNKED;
3990 ctx->st_code = STAT_STATUS_DENY;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003991 appctx->st0 = STAT_HTTP_LAST;
3992 }
3993 }
3994 else {
Christopher Faulet5d45e382019-02-27 15:15:23 +01003995 /* Unsupported method */
Willy Tarreau91cefca2022-05-03 17:08:29 +02003996 ctx->flags &= ~STAT_CHUNKED;
3997 ctx->st_code = STAT_STATUS_IVAL;
Christopher Faulet5d45e382019-02-27 15:15:23 +01003998 appctx->st0 = STAT_HTTP_LAST;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003999 }
4000
4001 s->task->nice = -32; /* small boost for HTTP statistics */
4002 return 1;
4003}
4004
Christopher Faulet021a8e42021-03-29 10:46:38 +02004005/* This function waits for the message payload at most <time> milliseconds (may
4006 * be set to TICK_ETERNITY). It stops to wait if at least <bytes> bytes of the
4007 * payload are received (0 means no limit). It returns HTTP_RULE_* depending on
4008 * the result:
4009 *
4010 * - HTTP_RULE_RES_CONT when conditions are met to stop waiting
4011 * - HTTP_RULE_RES_YIELD to wait for more data
Ilya Shipitsinb2be9a12021-04-24 13:25:42 +05004012 * - HTTP_RULE_RES_ABRT when a timeout occurred.
Christopher Faulet021a8e42021-03-29 10:46:38 +02004013 * - HTTP_RULE_RES_BADREQ if a parsing error is raised by lower level
Ilya Shipitsinb2be9a12021-04-24 13:25:42 +05004014 * - HTTP_RULE_RES_ERROR if an internal error occurred
Christopher Faulet021a8e42021-03-29 10:46:38 +02004015 *
Ilya Shipitsinb2be9a12021-04-24 13:25:42 +05004016 * If a timeout occurred, this function is responsible to emit the right response
Christopher Faulet021a8e42021-03-29 10:46:38 +02004017 * to the client, depending on the channel (408 on request side, 504 on response
4018 * side). All other errors must be handled by the caller.
4019 */
4020enum rule_result http_wait_for_msg_body(struct stream *s, struct channel *chn,
4021 unsigned int time, unsigned int bytes)
4022{
4023 struct session *sess = s->sess;
4024 struct http_txn *txn = s->txn;
4025 struct http_msg *msg = ((chn->flags & CF_ISRESP) ? &txn->rsp : &txn->req);
4026 struct htx *htx;
4027 enum rule_result ret = HTTP_RULE_RES_CONT;
4028
4029 htx = htxbuf(&chn->buf);
4030
4031 if (htx->flags & HTX_FL_PARSING_ERROR) {
4032 ret = HTTP_RULE_RES_BADREQ;
4033 goto end;
4034 }
4035 if (htx->flags & HTX_FL_PROCESSING_ERROR) {
4036 ret = HTTP_RULE_RES_ERROR;
4037 goto end;
4038 }
4039
4040 /* Do nothing for bodyless and CONNECT requests */
4041 if (txn->meth == HTTP_METH_CONNECT || (msg->flags & HTTP_MSGF_BODYLESS))
4042 goto end;
4043
4044 if (!(chn->flags & CF_ISRESP) && msg->msg_state < HTTP_MSG_DATA) {
4045 if (http_handle_expect_hdr(s, htx, msg) == -1) {
4046 ret = HTTP_RULE_RES_ERROR;
4047 goto end;
4048 }
4049 }
4050
4051 msg->msg_state = HTTP_MSG_DATA;
4052
4053 /* Now we're in HTTP_MSG_DATA. We just need to know if all data have
4054 * been received or if the buffer is full.
4055 */
Christopher Faulet78335962021-09-23 14:46:32 +02004056 if ((htx->flags & HTX_FL_EOM) ||
4057 htx_get_tail_type(htx) > HTX_BLK_DATA ||
4058 channel_htx_full(chn, htx, global.tune.maxrewrite) ||
Willy Tarreau99615ed2022-05-25 07:29:36 +02004059 sc_waiting_room(chn_prod(chn)))
Christopher Faulet021a8e42021-03-29 10:46:38 +02004060 goto end;
4061
4062 if (bytes) {
4063 struct htx_blk *blk;
4064 unsigned int len = 0;
4065
4066 for (blk = htx_get_first_blk(htx); blk; blk = htx_get_next_blk(htx, blk)) {
4067 if (htx_get_blk_type(blk) != HTX_BLK_DATA)
4068 continue;
4069 len += htx_get_blksz(blk);
4070 if (len >= bytes)
4071 goto end;
4072 }
4073 }
4074
4075 if ((chn->flags & CF_READ_TIMEOUT) || tick_is_expired(chn->analyse_exp, now_ms)) {
4076 if (!(chn->flags & CF_ISRESP))
4077 goto abort_req;
4078 goto abort_res;
4079 }
4080
4081 /* we get here if we need to wait for more data */
Christopher Faulet4b490b72023-01-04 11:55:15 +01004082 if (!(chn->flags & CF_SHUTR)) {
Christopher Faulet021a8e42021-03-29 10:46:38 +02004083 if (!tick_isset(chn->analyse_exp))
4084 chn->analyse_exp = tick_add_ifset(now_ms, time);
4085 ret = HTTP_RULE_RES_YIELD;
4086 }
4087
4088 end:
4089 return ret;
4090
Christopher Fauletf0d80df2023-01-13 10:20:20 +01004091 abort:
4092 http_reply_and_close(s, txn->status, http_error_message(s));
4093 ret = HTTP_RULE_RES_ABRT;
4094 goto end;
4095
Christopher Faulet021a8e42021-03-29 10:46:38 +02004096 abort_req:
4097 txn->status = 408;
4098 if (!(s->flags & SF_ERR_MASK))
4099 s->flags |= SF_ERR_CLITO;
Willy Tarreau4781b152021-04-06 13:53:36 +02004100 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req);
Christopher Faulet021a8e42021-03-29 10:46:38 +02004101 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02004102 _HA_ATOMIC_INC(&sess->listener->counters->failed_req);
Christopher Fauletf0d80df2023-01-13 10:20:20 +01004103 goto abort;
Christopher Faulet021a8e42021-03-29 10:46:38 +02004104
4105 abort_res:
4106 txn->status = 504;
4107 if (!(s->flags & SF_ERR_MASK))
4108 s->flags |= SF_ERR_SRVTO;
Christopher Faulet021a8e42021-03-29 10:46:38 +02004109 stream_inc_http_fail_ctr(s);
Christopher Fauletf0d80df2023-01-13 10:20:20 +01004110 goto abort;
Christopher Faulet021a8e42021-03-29 10:46:38 +02004111}
4112
Willy Tarreaub49672d2022-05-27 10:13:37 +02004113void http_perform_server_redirect(struct stream *s, struct stconn *sc)
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004114{
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004115 struct channel *req = &s->req;
4116 struct channel *res = &s->res;
4117 struct server *srv;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004118 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004119 struct htx_sl *sl;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004120 struct ist path, location;
4121 unsigned int flags;
Amaury Denoyellec453f952021-07-06 11:40:12 +02004122 struct http_uri_parser parser;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004123
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004124 /*
4125 * Create the location
4126 */
4127 chunk_reset(&trash);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004128
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004129 /* 1: add the server's prefix */
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004130 /* special prefix "/" means don't change URL */
4131 srv = __objt_server(s->target);
4132 if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') {
4133 if (!chunk_memcat(&trash, srv->rdr_pfx, srv->rdr_len))
4134 return;
4135 }
4136
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004137 /* 2: add the request Path */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004138 htx = htxbuf(&req->buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02004139 sl = http_get_stline(htx);
Amaury Denoyellec453f952021-07-06 11:40:12 +02004140 parser = http_uri_parser_init(htx_sl_req_uri(sl));
4141 path = http_parse_path(&parser);
Tim Duesterhused526372020-03-05 17:56:33 +01004142 if (!isttest(path))
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004143 return;
4144
4145 if (!chunk_memcat(&trash, path.ptr, path.len))
4146 return;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004147 location = ist2(trash.area, trash.data);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004148
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004149 /*
Ilya Shipitsin4a689da2022-10-29 09:34:32 +05004150 * Create the 302 response
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004151 */
4152 htx = htx_from_buf(&res->buf);
4153 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
4154 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
4155 ist("HTTP/1.1"), ist("302"), ist("Found"));
4156 if (!sl)
4157 goto fail;
4158 sl->info.res.status = 302;
4159 s->txn->status = 302;
4160
4161 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) ||
4162 !htx_add_header(htx, ist("Connection"), ist("close")) ||
4163 !htx_add_header(htx, ist("Content-length"), ist("0")) ||
4164 !htx_add_header(htx, ist("Location"), location))
4165 goto fail;
4166
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004167 if (!htx_add_endof(htx, HTX_BLK_EOH))
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004168 goto fail;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004169
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004170 htx->flags |= HTX_FL_EOM;
Christopher Fauletc20afb82020-01-24 19:16:26 +01004171 htx_to_buf(htx, &res->buf);
Christopher Fauleta72a7e42020-01-28 09:28:11 +01004172 if (!http_forward_proxy_resp(s, 1))
4173 goto fail;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004174
4175 /* return without error. */
Willy Tarreaub49672d2022-05-27 10:13:37 +02004176 sc_shutr(sc);
4177 sc_shutw(sc);
Christopher Faulet50264b42022-03-30 19:39:30 +02004178 s->conn_err_type = STRM_ET_NONE;
Willy Tarreaub49672d2022-05-27 10:13:37 +02004179 sc->state = SC_ST_CLO;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004180
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004181 if (!(s->flags & SF_ERR_MASK))
4182 s->flags |= SF_ERR_LOCAL;
4183 if (!(s->flags & SF_FINST_MASK))
4184 s->flags |= SF_FINST_C;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004185
4186 /* FIXME: we should increase a counter of redirects per server and per backend. */
4187 srv_inc_sess_ctr(srv);
4188 srv_set_sess_last(srv);
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004189 return;
4190
4191 fail:
4192 /* If an error occurred, remove the incomplete HTTP response from the
4193 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004194 channel_htx_truncate(res, htx);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004195}
4196
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05004197/* This function terminates the request because it was completely analyzed or
Christopher Fauletf2824e62018-10-01 12:12:37 +02004198 * because an error was triggered during the body forwarding.
4199 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004200static void http_end_request(struct stream *s)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004201{
4202 struct channel *chn = &s->req;
4203 struct http_txn *txn = s->txn;
4204
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004205 DBG_TRACE_ENTER(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004206
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004207 if (unlikely(txn->req.msg_state < HTTP_MSG_DONE)) {
4208 DBG_TRACE_DEVEL("waiting end of the request", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004209 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004210 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004211
4212 if (txn->req.msg_state == HTTP_MSG_DONE) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004213 /* No need to read anymore, the request was completely parsed.
4214 * We can shut the read side unless we want to abort_on_close,
4215 * or we have a POST request. The issue with POST requests is
4216 * that some browsers still send a CRLF after the request, and
4217 * this CRLF must be read so that it does not remain in the kernel
4218 * buffers, otherwise a close could cause an RST on some systems
4219 * (eg: Linux).
4220 */
Christopher Faulet769d0e92019-03-22 14:23:18 +01004221 if (!(s->be->options & PR_O_ABRT_CLOSE) && txn->meth != HTTP_METH_POST)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004222 channel_dont_read(chn);
4223
4224 /* if the server closes the connection, we want to immediately react
4225 * and close the socket to save packets and syscalls.
4226 */
Willy Tarreaucb041662022-05-17 19:44:42 +02004227 s->scb->flags |= SC_FL_NOHALF;
Christopher Fauletf2824e62018-10-01 12:12:37 +02004228
4229 /* In any case we've finished parsing the request so we must
4230 * disable Nagle when sending data because 1) we're not going
4231 * to shut this side, and 2) the server is waiting for us to
4232 * send pending data.
4233 */
4234 chn->flags |= CF_NEVER_WAIT;
4235
Christopher Fauletd01ce402019-01-02 17:44:13 +01004236 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
4237 /* The server has not finished to respond, so we
4238 * don't want to move in order not to upset it.
4239 */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004240 DBG_TRACE_DEVEL("waiting end of the response", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletd01ce402019-01-02 17:44:13 +01004241 return;
4242 }
4243
Christopher Fauletf2824e62018-10-01 12:12:37 +02004244 /* When we get here, it means that both the request and the
4245 * response have finished receiving. Depending on the connection
4246 * mode, we'll have to wait for the last bytes to leave in either
4247 * direction, and sometimes for a close to be effective.
4248 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02004249 if (txn->flags & TX_CON_WANT_TUN) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004250 /* Tunnel mode will not have any analyser so it needs to
4251 * poll for reads.
4252 */
4253 channel_auto_read(chn);
4254 txn->req.msg_state = HTTP_MSG_TUNNEL;
4255 }
4256 else {
4257 /* we're not expecting any new data to come for this
4258 * transaction, so we can close it.
Christopher Faulet9768c262018-10-22 09:34:31 +02004259 *
4260 * However, there is an exception if the response
4261 * length is undefined. In this case, we need to wait
4262 * the close from the server. The response will be
4263 * switched in TUNNEL mode until the end.
Christopher Fauletf2824e62018-10-01 12:12:37 +02004264 */
4265 if (!(txn->rsp.flags & HTTP_MSGF_XFER_LEN) &&
4266 txn->rsp.msg_state != HTTP_MSG_CLOSED)
Christopher Faulet9768c262018-10-22 09:34:31 +02004267 goto check_channel_flags;
Christopher Fauletf2824e62018-10-01 12:12:37 +02004268
4269 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4270 channel_shutr_now(chn);
4271 channel_shutw_now(chn);
4272 }
4273 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004274 goto check_channel_flags;
4275 }
4276
4277 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
4278 http_msg_closing:
4279 /* nothing else to forward, just waiting for the output buffer
4280 * to be empty and for the shutw_now to take effect.
4281 */
4282 if (channel_is_empty(chn)) {
4283 txn->req.msg_state = HTTP_MSG_CLOSED;
4284 goto http_msg_closed;
4285 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004286 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004287 return;
4288 }
4289
4290 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
4291 http_msg_closed:
Christopher Fauletf2824e62018-10-01 12:12:37 +02004292 /* if we don't know whether the server will close, we need to hard close */
4293 if (txn->rsp.flags & HTTP_MSGF_XFER_LEN)
Willy Tarreaucb041662022-05-17 19:44:42 +02004294 s->scb->flags |= SC_FL_NOLINGER; /* we want to close ASAP */
Christopher Fauletf2824e62018-10-01 12:12:37 +02004295 /* see above in MSG_DONE why we only do this in these states */
Christopher Faulet769d0e92019-03-22 14:23:18 +01004296 if (!(s->be->options & PR_O_ABRT_CLOSE))
Christopher Fauletf2824e62018-10-01 12:12:37 +02004297 channel_dont_read(chn);
4298 goto end;
4299 }
4300
4301 check_channel_flags:
4302 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4303 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4304 /* if we've just closed an output, let's switch */
4305 txn->req.msg_state = HTTP_MSG_CLOSING;
4306 goto http_msg_closing;
4307 }
4308
4309 end:
4310 chn->analysers &= AN_REQ_FLT_END;
Christopher Faulet198ef8b2020-12-15 13:32:55 +01004311 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
4312 chn->flags |= CF_NEVER_WAIT;
4313 if (HAS_REQ_DATA_FILTERS(s))
Christopher Fauletf2824e62018-10-01 12:12:37 +02004314 chn->analysers |= AN_REQ_FLT_XFER_DATA;
Christopher Faulet198ef8b2020-12-15 13:32:55 +01004315 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004316 channel_auto_close(chn);
4317 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004318 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004319}
4320
4321
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05004322/* This function terminates the response because it was completely analyzed or
Christopher Fauletf2824e62018-10-01 12:12:37 +02004323 * because an error was triggered during the body forwarding.
4324 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004325static void http_end_response(struct stream *s)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004326{
4327 struct channel *chn = &s->res;
4328 struct http_txn *txn = s->txn;
4329
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004330 DBG_TRACE_ENTER(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004331
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004332 if (unlikely(txn->rsp.msg_state < HTTP_MSG_DONE)) {
4333 DBG_TRACE_DEVEL("waiting end of the response", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004334 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004335 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004336
4337 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
4338 /* In theory, we don't need to read anymore, but we must
4339 * still monitor the server connection for a possible close
4340 * while the request is being uploaded, so we don't disable
4341 * reading.
4342 */
4343 /* channel_dont_read(chn); */
4344
4345 if (txn->req.msg_state < HTTP_MSG_DONE) {
4346 /* The client seems to still be sending data, probably
4347 * because we got an error response during an upload.
4348 * We have the choice of either breaking the connection
4349 * or letting it pass through. Let's do the later.
4350 */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004351 DBG_TRACE_DEVEL("waiting end of the request", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004352 return;
4353 }
4354
4355 /* When we get here, it means that both the request and the
4356 * response have finished receiving. Depending on the connection
4357 * mode, we'll have to wait for the last bytes to leave in either
4358 * direction, and sometimes for a close to be effective.
4359 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02004360 if (txn->flags & TX_CON_WANT_TUN) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004361 channel_auto_read(chn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004362 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
4363 }
4364 else {
4365 /* we're not expecting any new data to come for this
4366 * transaction, so we can close it.
4367 */
4368 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4369 channel_shutr_now(chn);
4370 channel_shutw_now(chn);
4371 }
4372 }
4373 goto check_channel_flags;
4374 }
4375
4376 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
4377 http_msg_closing:
4378 /* nothing else to forward, just waiting for the output buffer
4379 * to be empty and for the shutw_now to take effect.
4380 */
4381 if (channel_is_empty(chn)) {
4382 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4383 goto http_msg_closed;
4384 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004385 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004386 return;
4387 }
4388
4389 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
4390 http_msg_closed:
4391 /* drop any pending data */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004392 channel_htx_truncate(&s->req, htxbuf(&s->req.buf));
Christopher Faulet9768c262018-10-22 09:34:31 +02004393 channel_abort(&s->req);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004394 goto end;
4395 }
4396
4397 check_channel_flags:
4398 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4399 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4400 /* if we've just closed an output, let's switch */
4401 txn->rsp.msg_state = HTTP_MSG_CLOSING;
4402 goto http_msg_closing;
4403 }
4404
4405 end:
4406 chn->analysers &= AN_RES_FLT_END;
Christopher Faulet198ef8b2020-12-15 13:32:55 +01004407 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
4408 chn->flags |= CF_NEVER_WAIT;
4409 if (HAS_RSP_DATA_FILTERS(s))
4410 chn->analysers |= AN_RES_FLT_XFER_DATA;
4411 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004412 channel_auto_close(chn);
4413 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004414 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004415}
4416
Christopher Fauletef70e252020-01-28 09:26:19 +01004417/* Forward a response generated by HAProxy (error/redirect/return). This
4418 * function forwards all pending incoming data. If <final> is set to 0, nothing
4419 * more is performed. It is used for 1xx informational messages. Otherwise, the
Christopher Faulet507479b2020-05-15 12:29:46 +02004420 * transaction is terminated and the request is emptied. On success 1 is
Christopher Faulet40e6b552020-06-25 16:04:50 +02004421 * returned. If an error occurred, 0 is returned. If it fails, this function
4422 * only exits. It is the caller responsibility to do the cleanup.
Christopher Fauletef70e252020-01-28 09:26:19 +01004423 */
4424int http_forward_proxy_resp(struct stream *s, int final)
4425{
4426 struct channel *req = &s->req;
4427 struct channel *res = &s->res;
4428 struct htx *htx = htxbuf(&res->buf);
4429 size_t data;
4430
4431 if (final) {
4432 htx->flags |= HTX_FL_PROXY_RESP;
Christopher Faulet507479b2020-05-15 12:29:46 +02004433
Christopher Fauletaab1b672020-11-18 16:44:02 +01004434 if (!htx_is_empty(htx) && !http_eval_after_res_rules(s))
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01004435 return 0;
Christopher Fauletef70e252020-01-28 09:26:19 +01004436
Christopher Fauletd6c48362020-10-19 18:01:38 +02004437 if (s->txn->meth == HTTP_METH_HEAD)
4438 htx_skip_msg_payload(htx);
4439
Christopher Fauletef70e252020-01-28 09:26:19 +01004440 channel_auto_read(req);
4441 channel_abort(req);
4442 channel_auto_close(req);
4443 channel_htx_erase(req, htxbuf(&req->buf));
4444
4445 res->wex = tick_add_ifset(now_ms, res->wto);
4446 channel_auto_read(res);
4447 channel_auto_close(res);
4448 channel_shutr_now(res);
Christopher Faulet1a9db7c2020-06-25 15:36:45 +02004449 res->flags |= CF_EOI; /* The response is terminated, add EOI */
Christopher Faulet42432f32020-11-20 17:43:16 +01004450 htxbuf(&res->buf)->flags |= HTX_FL_EOM; /* no more data are expected */
Christopher Fauletef70e252020-01-28 09:26:19 +01004451 }
Christopher Fauletcf6898c2020-06-25 15:55:11 +02004452 else {
4453 /* Send ASAP informational messages. Rely on CF_EOI for final
4454 * response.
4455 */
4456 res->flags |= CF_SEND_DONTWAIT;
4457 }
Christopher Fauletef70e252020-01-28 09:26:19 +01004458
4459 data = htx->data - co_data(res);
4460 c_adv(res, data);
4461 htx->first = -1;
4462 res->total += data;
4463 return 1;
4464}
4465
Willy Tarreaub49672d2022-05-27 10:13:37 +02004466void http_server_error(struct stream *s, struct stconn *sc, int err,
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004467 int finst, struct http_reply *msg)
Christopher Faulet0f226952018-10-22 09:29:56 +02004468{
Christopher Faulet72c7d8d2020-01-27 15:32:25 +01004469 http_reply_and_close(s, s->txn->status, msg);
Christopher Faulet0f226952018-10-22 09:29:56 +02004470 if (!(s->flags & SF_ERR_MASK))
4471 s->flags |= err;
4472 if (!(s->flags & SF_FINST_MASK))
4473 s->flags |= finst;
4474}
4475
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004476void http_reply_and_close(struct stream *s, short status, struct http_reply *msg)
Christopher Faulet0f226952018-10-22 09:29:56 +02004477{
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004478 if (!msg) {
4479 channel_htx_truncate(&s->res, htxbuf(&s->res.buf));
4480 goto end;
4481 }
4482
4483 if (http_reply_message(s, msg) == -1) {
4484 /* On error, return a 500 error message, but don't rewrite it if
Christopher Faulet40e6b552020-06-25 16:04:50 +02004485 * it is already an internal error. If it was already a "const"
4486 * 500 error, just fail.
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004487 */
Christopher Faulet40e6b552020-06-25 16:04:50 +02004488 if (s->txn->status == 500) {
4489 if (s->txn->flags & TX_CONST_REPLY)
4490 goto end;
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004491 s->txn->flags |= TX_CONST_REPLY;
Christopher Faulet40e6b552020-06-25 16:04:50 +02004492 }
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004493 s->txn->status = 500;
4494 s->txn->http_reply = NULL;
4495 return http_reply_and_close(s, s->txn->status, http_error_message(s));
4496 }
4497
4498end:
4499 s->res.wex = tick_add_ifset(now_ms, s->res.wto);
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004500
Christopher Faulet2d565002021-09-10 09:17:50 +02004501 /* At this staged, HTTP analysis is finished */
4502 s->req.analysers &= AN_REQ_FLT_END;
4503 s->req.analyse_exp = TICK_ETERNITY;
4504
4505 s->res.analysers &= AN_RES_FLT_END;
4506 s->res.analyse_exp = TICK_ETERNITY;
4507
Christopher Faulet0f226952018-10-22 09:29:56 +02004508 channel_auto_read(&s->req);
4509 channel_abort(&s->req);
4510 channel_auto_close(&s->req);
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004511 channel_htx_erase(&s->req, htxbuf(&s->req.buf));
Christopher Faulet72c7d8d2020-01-27 15:32:25 +01004512 channel_auto_read(&s->res);
4513 channel_auto_close(&s->res);
4514 channel_shutr_now(&s->res);
Christopher Faulet0f226952018-10-22 09:29:56 +02004515}
4516
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004517struct http_reply *http_error_message(struct stream *s)
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004518{
4519 const int msgnum = http_get_status_idx(s->txn->status);
4520
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004521 if (s->txn->http_reply)
4522 return s->txn->http_reply;
4523 else if (s->be->replies[msgnum])
4524 return s->be->replies[msgnum];
4525 else if (strm_fe(s)->replies[msgnum])
4526 return strm_fe(s)->replies[msgnum];
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004527 else
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004528 return &http_err_replies[msgnum];
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004529}
4530
Christopher Faulet40e6b552020-06-25 16:04:50 +02004531/* Produces an HTX message from an http reply. Depending on the http reply type,
4532 * a, errorfile, an raw file or a log-format string is used. On success, it
4533 * returns 0. If an error occurs -1 is returned. If it fails, this function only
4534 * exits. It is the caller responsibility to do the cleanup.
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004535 */
Christopher Fauletae43b6c2020-05-27 15:24:22 +02004536int http_reply_to_htx(struct stream *s, struct htx *htx, struct http_reply *reply)
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004537{
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004538 struct buffer *errmsg;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004539 struct htx_sl *sl;
4540 struct buffer *body = NULL;
4541 const char *status, *reason, *clen, *ctype;
4542 unsigned int slflags;
4543 int ret = 0;
4544
Christopher Faulete29a97e2020-05-14 14:49:25 +02004545 /*
4546 * - HTTP_REPLY_ERRFILES unexpected here. handled as no payload if so
4547 *
4548 * - HTTP_REPLY_INDIRECT: switch on another reply if defined or handled
4549 * as no payload if NULL. the TXN status code is set with the status
4550 * of the original reply.
4551 */
4552
4553 if (reply->type == HTTP_REPLY_INDIRECT) {
4554 if (reply->body.reply)
4555 reply = reply->body.reply;
4556 }
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004557 if (reply->type == HTTP_REPLY_ERRMSG && !reply->body.errmsg) {
4558 /* get default error message */
4559 if (reply == s->txn->http_reply)
4560 s->txn->http_reply = NULL;
4561 reply = http_error_message(s);
4562 if (reply->type == HTTP_REPLY_INDIRECT) {
4563 if (reply->body.reply)
4564 reply = reply->body.reply;
4565 }
4566 }
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004567
4568 if (reply->type == HTTP_REPLY_ERRMSG) {
4569 /* implicit or explicit error message*/
4570 errmsg = reply->body.errmsg;
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004571 if (errmsg && !b_is_null(errmsg)) {
Christopher Faulet20567362020-05-15 14:52:49 +02004572 if (!htx_copy_msg(htx, errmsg))
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004573 goto fail;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004574 }
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004575 }
4576 else {
4577 /* no payload, file or log-format string */
4578 if (reply->type == HTTP_REPLY_RAW) {
4579 /* file */
4580 body = &reply->body.obj;
4581 }
4582 else if (reply->type == HTTP_REPLY_LOGFMT) {
4583 /* log-format string */
4584 body = alloc_trash_chunk();
4585 if (!body)
4586 goto fail_alloc;
4587 body->data = build_logline(s, body->area, body->size, &reply->body.fmt);
4588 }
4589 /* else no payload */
4590
4591 status = ultoa(reply->status);
4592 reason = http_get_reason(reply->status);
4593 slflags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_CLEN);
4594 if (!body || !b_data(body))
4595 slflags |= HTX_SL_F_BODYLESS;
4596 sl = htx_add_stline(htx, HTX_BLK_RES_SL, slflags, ist("HTTP/1.1"), ist(status), ist(reason));
4597 if (!sl)
4598 goto fail;
4599 sl->info.res.status = reply->status;
4600
4601 clen = (body ? ultoa(b_data(body)) : "0");
4602 ctype = reply->ctype;
4603
4604 if (!LIST_ISEMPTY(&reply->hdrs)) {
4605 struct http_reply_hdr *hdr;
4606 struct buffer *value = alloc_trash_chunk();
4607
4608 if (!value)
4609 goto fail;
4610
4611 list_for_each_entry(hdr, &reply->hdrs, list) {
4612 chunk_reset(value);
4613 value->data = build_logline(s, value->area, value->size, &hdr->value);
4614 if (b_data(value) && !htx_add_header(htx, hdr->name, ist2(b_head(value), b_data(value)))) {
4615 free_trash_chunk(value);
4616 goto fail;
4617 }
4618 chunk_reset(value);
4619 }
4620 free_trash_chunk(value);
4621 }
4622
4623 if (!htx_add_header(htx, ist("content-length"), ist(clen)) ||
4624 (body && b_data(body) && ctype && !htx_add_header(htx, ist("content-type"), ist(ctype))) ||
4625 !htx_add_endof(htx, HTX_BLK_EOH) ||
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004626 (body && b_data(body) && !htx_add_data_atonce(htx, ist2(b_head(body), b_data(body)))))
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004627 goto fail;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004628
4629 htx->flags |= HTX_FL_EOM;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004630 }
4631
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004632 leave:
4633 if (reply->type == HTTP_REPLY_LOGFMT)
4634 free_trash_chunk(body);
4635 return ret;
4636
4637 fail_alloc:
4638 if (!(s->flags & SF_ERR_MASK))
4639 s->flags |= SF_ERR_RESOURCE;
Christopher Faulet97e466c2020-05-15 15:12:47 +02004640 /* fall through */
4641 fail:
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004642 ret = -1;
4643 goto leave;
Christopher Faulet97e466c2020-05-15 15:12:47 +02004644}
4645
4646/* Send an http reply to the client. On success, it returns 0. If an error
Christopher Faulet40e6b552020-06-25 16:04:50 +02004647 * occurs -1 is returned and the response channel is truncated, removing this
4648 * way the faulty reply. This function may fail when the reply is formatted
4649 * (http_reply_to_htx) or when the reply is forwarded
4650 * (http_forward_proxy_resp). On the last case, it is because a
4651 * http-after-response rule fails.
Christopher Faulet97e466c2020-05-15 15:12:47 +02004652 */
4653int http_reply_message(struct stream *s, struct http_reply *reply)
4654{
4655 struct channel *res = &s->res;
4656 struct htx *htx = htx_from_buf(&res->buf);
4657
4658 if (s->txn->status == -1)
4659 s->txn->status = reply->status;
4660 channel_htx_truncate(res, htx);
4661
4662 if (http_reply_to_htx(s, htx, reply) == -1)
4663 goto fail;
4664
4665 htx_to_buf(htx, &s->res.buf);
4666 if (!http_forward_proxy_resp(s, 1))
4667 goto fail;
4668 return 0;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004669
4670 fail:
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004671 channel_htx_truncate(res, htx);
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004672 if (!(s->flags & SF_ERR_MASK))
4673 s->flags |= SF_ERR_PRXCOND;
Christopher Faulet97e466c2020-05-15 15:12:47 +02004674 return -1;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004675}
4676
Christopher Faulet50264b42022-03-30 19:39:30 +02004677/* Return the error message corresponding to s->conn_err_type. It is assumed
Christopher Faulet304cc402019-07-15 15:46:28 +02004678 * that the server side is closed. Note that err_type is actually a
4679 * bitmask, where almost only aborts may be cumulated with other
4680 * values. We consider that aborted operations are more important
4681 * than timeouts or errors due to the fact that nobody else in the
4682 * logs might explain incomplete retries. All others should avoid
4683 * being cumulated. It should normally not be possible to have multiple
4684 * aborts at once, but just in case, the first one in sequence is reported.
4685 * Note that connection errors appearing on the second request of a keep-alive
4686 * connection are not reported since this allows the client to retry.
4687 */
Willy Tarreaub49672d2022-05-27 10:13:37 +02004688void http_return_srv_error(struct stream *s, struct stconn *sc)
Christopher Faulet304cc402019-07-15 15:46:28 +02004689{
Christopher Faulet50264b42022-03-30 19:39:30 +02004690 int err_type = s->conn_err_type;
Christopher Faulet304cc402019-07-15 15:46:28 +02004691
4692 /* set s->txn->status for http_error_message(s) */
Christopher Faulet50264b42022-03-30 19:39:30 +02004693 if (err_type & STRM_ET_QUEUE_ABRT) {
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004694 s->txn->status = -1;
Willy Tarreaub49672d2022-05-27 10:13:37 +02004695 http_server_error(s, sc, SF_ERR_CLICL, SF_FINST_Q, NULL);
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004696 }
Christopher Faulet50264b42022-03-30 19:39:30 +02004697 else if (err_type & STRM_ET_CONN_ABRT) {
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004698 s->txn->status = -1;
Willy Tarreaub49672d2022-05-27 10:13:37 +02004699 http_server_error(s, sc, SF_ERR_CLICL, SF_FINST_C, NULL);
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004700 }
Christopher Faulet50264b42022-03-30 19:39:30 +02004701 else if (err_type & STRM_ET_QUEUE_TO) {
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004702 s->txn->status = 503;
Willy Tarreaub49672d2022-05-27 10:13:37 +02004703 http_server_error(s, sc, SF_ERR_SRVTO, SF_FINST_Q,
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004704 http_error_message(s));
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004705 }
Christopher Faulet50264b42022-03-30 19:39:30 +02004706 else if (err_type & STRM_ET_QUEUE_ERR) {
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004707 s->txn->status = 503;
Willy Tarreaub49672d2022-05-27 10:13:37 +02004708 http_server_error(s, sc, SF_ERR_SRVCL, SF_FINST_Q,
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004709 http_error_message(s));
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004710 }
Christopher Faulet50264b42022-03-30 19:39:30 +02004711 else if (err_type & STRM_ET_CONN_TO) {
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004712 s->txn->status = 503;
Willy Tarreaub49672d2022-05-27 10:13:37 +02004713 http_server_error(s, sc, SF_ERR_SRVTO, SF_FINST_C,
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004714 (s->txn->flags & TX_NOT_FIRST) ? NULL :
4715 http_error_message(s));
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004716 }
Christopher Faulet50264b42022-03-30 19:39:30 +02004717 else if (err_type & STRM_ET_CONN_ERR) {
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004718 s->txn->status = 503;
Willy Tarreaub49672d2022-05-27 10:13:37 +02004719 http_server_error(s, sc, SF_ERR_SRVCL, SF_FINST_C,
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004720 (s->flags & SF_SRV_REUSED) ? NULL :
4721 http_error_message(s));
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004722 }
Christopher Faulet50264b42022-03-30 19:39:30 +02004723 else if (err_type & STRM_ET_CONN_RES) {
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004724 s->txn->status = 503;
Willy Tarreaub49672d2022-05-27 10:13:37 +02004725 http_server_error(s, sc, SF_ERR_RESOURCE, SF_FINST_C,
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004726 (s->txn->flags & TX_NOT_FIRST) ? NULL :
4727 http_error_message(s));
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004728 }
Christopher Faulet50264b42022-03-30 19:39:30 +02004729 else { /* STRM_ET_CONN_OTHER and others */
Christopher Faulet304cc402019-07-15 15:46:28 +02004730 s->txn->status = 500;
Willy Tarreaub49672d2022-05-27 10:13:37 +02004731 http_server_error(s, sc, SF_ERR_INTERNAL, SF_FINST_C,
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004732 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004733 }
4734}
4735
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004736
Christopher Faulet4a28a532019-03-01 11:19:40 +01004737/* Handle Expect: 100-continue for HTTP/1.1 messages if necessary. It returns 0
4738 * on success and -1 on error.
4739 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004740static int http_handle_expect_hdr(struct stream *s, struct htx *htx, struct http_msg *msg)
Christopher Faulet4a28a532019-03-01 11:19:40 +01004741{
4742 /* If we have HTTP/1.1 message with a body and Expect: 100-continue,
4743 * then we must send an HTTP/1.1 100 Continue intermediate response.
4744 */
4745 if (msg->msg_state == HTTP_MSG_BODY && (msg->flags & HTTP_MSGF_VER_11) &&
4746 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
4747 struct ist hdr = { .ptr = "Expect", .len = 6 };
4748 struct http_hdr_ctx ctx;
4749
4750 ctx.blk = NULL;
4751 /* Expect is allowed in 1.1, look for it */
4752 if (http_find_header(htx, hdr, &ctx, 0) &&
4753 unlikely(isteqi(ctx.value, ist2("100-continue", 12)))) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004754 if (http_reply_100_continue(s) == -1)
Christopher Faulet4a28a532019-03-01 11:19:40 +01004755 return -1;
4756 http_remove_header(htx, &ctx);
4757 }
4758 }
4759 return 0;
4760}
4761
Christopher Faulet23a3c792018-11-28 10:01:23 +01004762/* Send a 100-Continue response to the client. It returns 0 on success and -1
4763 * on error. The response channel is updated accordingly.
4764 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004765static int http_reply_100_continue(struct stream *s)
Christopher Faulet23a3c792018-11-28 10:01:23 +01004766{
4767 struct channel *res = &s->res;
4768 struct htx *htx = htx_from_buf(&res->buf);
4769 struct htx_sl *sl;
4770 unsigned int flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|
4771 HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
Christopher Faulet23a3c792018-11-28 10:01:23 +01004772
4773 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
4774 ist("HTTP/1.1"), ist("100"), ist("Continue"));
4775 if (!sl)
4776 goto fail;
4777 sl->info.res.status = 100;
4778
Christopher Faulet1d5ec092019-06-26 14:23:54 +02004779 if (!htx_add_endof(htx, HTX_BLK_EOH))
Christopher Faulet23a3c792018-11-28 10:01:23 +01004780 goto fail;
4781
Christopher Fauleta72a7e42020-01-28 09:28:11 +01004782 if (!http_forward_proxy_resp(s, 0))
4783 goto fail;
Christopher Faulet23a3c792018-11-28 10:01:23 +01004784 return 0;
4785
4786 fail:
4787 /* If an error occurred, remove the incomplete HTTP response from the
4788 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004789 channel_htx_truncate(res, htx);
Christopher Faulet23a3c792018-11-28 10:01:23 +01004790 return -1;
4791}
4792
Christopher Faulet12c51e22018-11-28 15:59:42 +01004793
Christopher Faulet0f226952018-10-22 09:29:56 +02004794/*
4795 * Capture headers from message <htx> according to header list <cap_hdr>, and
4796 * fill the <cap> pointers appropriately.
4797 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004798static void http_capture_headers(struct htx *htx, char **cap, struct cap_hdr *cap_hdr)
Christopher Faulet0f226952018-10-22 09:29:56 +02004799{
4800 struct cap_hdr *h;
4801 int32_t pos;
4802
Christopher Fauleta3f15502019-05-13 15:27:23 +02004803 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet0f226952018-10-22 09:29:56 +02004804 struct htx_blk *blk = htx_get_blk(htx, pos);
4805 enum htx_blk_type type = htx_get_blk_type(blk);
4806 struct ist n, v;
4807
4808 if (type == HTX_BLK_EOH)
4809 break;
4810 if (type != HTX_BLK_HDR)
4811 continue;
4812
4813 n = htx_get_blk_name(htx, blk);
4814
4815 for (h = cap_hdr; h; h = h->next) {
4816 if (h->namelen && (h->namelen == n.len) &&
4817 (strncasecmp(n.ptr, h->name, h->namelen) == 0)) {
4818 if (cap[h->index] == NULL)
4819 cap[h->index] =
4820 pool_alloc(h->pool);
4821
4822 if (cap[h->index] == NULL) {
4823 ha_alert("HTTP capture : out of memory.\n");
4824 break;
4825 }
4826
4827 v = htx_get_blk_value(htx, blk);
Tim Duesterhus2471f5c2021-11-08 09:05:01 +01004828 v = isttrim(v, h->len);
Christopher Faulet0f226952018-10-22 09:29:56 +02004829
4830 memcpy(cap[h->index], v.ptr, v.len);
4831 cap[h->index][v.len]=0;
4832 }
4833 }
4834 }
4835}
4836
Christopher Faulet0b6bdc52018-10-24 11:05:36 +02004837/* Delete a value in a header between delimiters <from> and <next>. The header
4838 * itself is delimited by <start> and <end> pointers. The number of characters
4839 * displaced is returned, and the pointer to the first delimiter is updated if
4840 * required. The function tries as much as possible to respect the following
4841 * principles :
4842 * - replace <from> delimiter by the <next> one unless <from> points to <start>,
4843 * in which case <next> is simply removed
4844 * - set exactly one space character after the new first delimiter, unless there
4845 * are not enough characters in the block being moved to do so.
4846 * - remove unneeded spaces before the previous delimiter and after the new
4847 * one.
4848 *
4849 * It is the caller's responsibility to ensure that :
4850 * - <from> points to a valid delimiter or <start> ;
4851 * - <next> points to a valid delimiter or <end> ;
4852 * - there are non-space chars before <from>.
4853 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004854static int http_del_hdr_value(char *start, char *end, char **from, char *next)
Christopher Faulet0b6bdc52018-10-24 11:05:36 +02004855{
4856 char *prev = *from;
4857
4858 if (prev == start) {
4859 /* We're removing the first value. eat the semicolon, if <next>
4860 * is lower than <end> */
4861 if (next < end)
4862 next++;
4863
4864 while (next < end && HTTP_IS_SPHT(*next))
4865 next++;
4866 }
4867 else {
4868 /* Remove useless spaces before the old delimiter. */
4869 while (HTTP_IS_SPHT(*(prev-1)))
4870 prev--;
4871 *from = prev;
4872
4873 /* copy the delimiter and if possible a space if we're
4874 * not at the end of the line.
4875 */
4876 if (next < end) {
4877 *prev++ = *next++;
4878 if (prev + 1 < next)
4879 *prev++ = ' ';
4880 while (next < end && HTTP_IS_SPHT(*next))
4881 next++;
4882 }
4883 }
4884 memmove(prev, next, end - next);
4885 return (prev - next);
4886}
4887
Christopher Faulet0f226952018-10-22 09:29:56 +02004888
4889/* Formats the start line of the request (without CRLF) and puts it in <str> and
Joseph Herlantc42c0e92018-11-25 10:43:27 -08004890 * return the written length. The line can be truncated if it exceeds <len>.
Christopher Faulet0f226952018-10-22 09:29:56 +02004891 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004892static size_t http_fmt_req_line(const struct htx_sl *sl, char *str, size_t len)
Christopher Faulet0f226952018-10-22 09:29:56 +02004893{
4894 struct ist dst = ist2(str, 0);
4895
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004896 if (istcat(&dst, htx_sl_req_meth(sl), len) == -1)
Christopher Faulet0f226952018-10-22 09:29:56 +02004897 goto end;
4898 if (dst.len + 1 > len)
4899 goto end;
4900 dst.ptr[dst.len++] = ' ';
4901
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004902 if (istcat(&dst, htx_sl_req_uri(sl), len) == -1)
Christopher Faulet0f226952018-10-22 09:29:56 +02004903 goto end;
4904 if (dst.len + 1 > len)
4905 goto end;
4906 dst.ptr[dst.len++] = ' ';
4907
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004908 istcat(&dst, htx_sl_req_vsn(sl), len);
Christopher Faulet0f226952018-10-22 09:29:56 +02004909 end:
4910 return dst.len;
4911}
4912
4913/*
4914 * Print a debug line with a start line.
4915 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004916static void http_debug_stline(const char *dir, struct stream *s, const struct htx_sl *sl)
Christopher Faulet0f226952018-10-22 09:29:56 +02004917{
4918 struct session *sess = strm_sess(s);
4919 int max;
4920
4921 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
4922 dir,
Willy Tarreau88bc8002021-12-06 07:01:02 +00004923 objt_conn(sess->origin) ? (unsigned short)__objt_conn(sess->origin)->handle.fd : -1,
Willy Tarreaufd9417b2022-05-18 16:23:22 +02004924 sc_conn(s->scb) ? (unsigned short)(__sc_conn(s->scb))->handle.fd : -1);
Christopher Faulet0f226952018-10-22 09:29:56 +02004925
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004926 max = HTX_SL_P1_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02004927 UBOUND(max, trash.size - trash.data - 3);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004928 chunk_memcat(&trash, HTX_SL_P1_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02004929 trash.area[trash.data++] = ' ';
4930
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004931 max = HTX_SL_P2_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02004932 UBOUND(max, trash.size - trash.data - 2);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004933 chunk_memcat(&trash, HTX_SL_P2_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02004934 trash.area[trash.data++] = ' ';
4935
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004936 max = HTX_SL_P3_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02004937 UBOUND(max, trash.size - trash.data - 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004938 chunk_memcat(&trash, HTX_SL_P3_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02004939 trash.area[trash.data++] = '\n';
4940
Willy Tarreau2e8ab6b2020-03-14 11:03:20 +01004941 DISGUISE(write(1, trash.area, trash.data));
Christopher Faulet0f226952018-10-22 09:29:56 +02004942}
4943
4944/*
4945 * Print a debug line with a header.
4946 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004947static 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 +02004948{
4949 struct session *sess = strm_sess(s);
4950 int max;
4951
4952 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
4953 dir,
Willy Tarreau88bc8002021-12-06 07:01:02 +00004954 objt_conn(sess->origin) ? (unsigned short)__objt_conn(sess->origin)->handle.fd : -1,
Willy Tarreaufd9417b2022-05-18 16:23:22 +02004955 sc_conn(s->scb) ? (unsigned short)(__sc_conn(s->scb))->handle.fd : -1);
Christopher Faulet0f226952018-10-22 09:29:56 +02004956
4957 max = n.len;
4958 UBOUND(max, trash.size - trash.data - 3);
4959 chunk_memcat(&trash, n.ptr, max);
4960 trash.area[trash.data++] = ':';
4961 trash.area[trash.data++] = ' ';
4962
4963 max = v.len;
4964 UBOUND(max, trash.size - trash.data - 1);
4965 chunk_memcat(&trash, v.ptr, max);
4966 trash.area[trash.data++] = '\n';
4967
Willy Tarreau2e8ab6b2020-03-14 11:03:20 +01004968 DISGUISE(write(1, trash.area, trash.data));
Christopher Faulet0f226952018-10-22 09:29:56 +02004969}
4970
Christopher Fauleta8a46e22019-07-16 14:53:09 +02004971void http_txn_reset_req(struct http_txn *txn)
4972{
Christopher Faulet1aea50e2020-01-17 16:03:53 +01004973 txn->req.flags = 0;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02004974 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
4975}
4976
4977void http_txn_reset_res(struct http_txn *txn)
4978{
Christopher Faulet1aea50e2020-01-17 16:03:53 +01004979 txn->rsp.flags = 0;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02004980 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
4981}
4982
4983/*
Christopher Faulet75f619a2021-03-08 19:12:58 +01004984 * Create and initialize a new HTTP transaction for stream <s>. This should be
4985 * used before processing any new request. It returns the transaction or NLULL
4986 * on error.
Christopher Fauleta8a46e22019-07-16 14:53:09 +02004987 */
Christopher Faulet75f619a2021-03-08 19:12:58 +01004988struct http_txn *http_create_txn(struct stream *s)
Christopher Fauleta8a46e22019-07-16 14:53:09 +02004989{
Christopher Faulet75f619a2021-03-08 19:12:58 +01004990 struct http_txn *txn;
Willy Tarreaub49672d2022-05-27 10:13:37 +02004991 struct stconn *sc = s->scf;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02004992
Christopher Faulet75f619a2021-03-08 19:12:58 +01004993 txn = pool_alloc(pool_head_http_txn);
4994 if (!txn)
4995 return NULL;
4996 s->txn = txn;
4997
Christopher Faulet5eb67f52022-06-22 17:12:05 +02004998 txn->meth = HTTP_METH_OTHER;
Willy Tarreaub49672d2022-05-27 10:13:37 +02004999 txn->flags = ((sc && sc_ep_test(sc, SE_FL_NOT_FIRST)) ? TX_NOT_FIRST : 0);
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005000 txn->status = -1;
Christopher Faulet5cb513a2020-05-13 17:56:56 +02005001 txn->http_reply = NULL;
Christopher Faulete05bf9e2022-03-29 15:23:40 +02005002 txn->l7_buffer = BUF_NULL;
Willy Tarreau8b507582020-02-25 09:35:07 +01005003 write_u32(txn->cache_hash, 0);
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005004
5005 txn->cookie_first_date = 0;
5006 txn->cookie_last_date = 0;
5007
5008 txn->srv_cookie = NULL;
5009 txn->cli_cookie = NULL;
5010 txn->uri = NULL;
5011
5012 http_txn_reset_req(txn);
5013 http_txn_reset_res(txn);
5014
5015 txn->req.chn = &s->req;
5016 txn->rsp.chn = &s->res;
5017
5018 txn->auth.method = HTTP_AUTH_UNKNOWN;
5019
Aurelien DARRAGON5ad2b642022-11-18 09:17:29 +01005020 /* here we don't want to re-initialize s->vars_txn and s->vars_reqres
5021 * variable lists, because they were already initialized upon stream
5022 * creation in stream_new(), and thus may already contain some variables
5023 */
Christopher Faulet75f619a2021-03-08 19:12:58 +01005024
5025 return txn;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005026}
5027
5028/* to be used at the end of a transaction */
Christopher Faulet75f619a2021-03-08 19:12:58 +01005029void http_destroy_txn(struct stream *s)
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005030{
5031 struct http_txn *txn = s->txn;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005032
5033 /* these ones will have been dynamically allocated */
5034 pool_free(pool_head_requri, txn->uri);
5035 pool_free(pool_head_capture, txn->cli_cookie);
5036 pool_free(pool_head_capture, txn->srv_cookie);
Tim Duesterhusa17e6622020-03-05 20:19:02 +01005037 pool_free(pool_head_uniqueid, s->unique_id.ptr);
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005038
Tim Duesterhusa17e6622020-03-05 20:19:02 +01005039 s->unique_id = IST_NULL;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005040 txn->uri = NULL;
5041 txn->srv_cookie = NULL;
5042 txn->cli_cookie = NULL;
5043
Christopher Faulet59399252019-11-07 14:27:52 +01005044 if (!LIST_ISEMPTY(&s->vars_txn.head))
5045 vars_prune(&s->vars_txn, s->sess, s);
5046 if (!LIST_ISEMPTY(&s->vars_reqres.head))
5047 vars_prune(&s->vars_reqres, s->sess, s);
Christopher Faulet75f619a2021-03-08 19:12:58 +01005048
Christopher Faulete05bf9e2022-03-29 15:23:40 +02005049 b_free(&txn->l7_buffer);
5050
Christopher Faulet75f619a2021-03-08 19:12:58 +01005051 pool_free(pool_head_http_txn, txn);
5052 s->txn = NULL;
Christopher Faulet59399252019-11-07 14:27:52 +01005053}
5054
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005055
Christopher Faulet71236de2023-01-13 09:06:38 +01005056void http_set_term_flags(struct stream *s)
5057{
5058 if (!(s->flags & SF_ERR_MASK))
5059 s->flags |= SF_ERR_PRXCOND;
5060
5061 if (!(s->flags & SF_FINST_MASK)) {
5062 if (s->scb->state == SC_ST_INI) {
5063 /* Before any connection attempt on the server side, we
5064 * are still in the request analysis. Just take case to
5065 * detect tarpit error
5066 */
5067 if (s->req.analysers & AN_REQ_HTTP_TARPIT)
5068 s->flags |= SF_FINST_T;
5069 else
5070 s->flags |= SF_FINST_R;
5071 }
5072 else if (s->scb->state == SC_ST_QUE)
5073 s->flags |= SF_FINST_Q;
5074 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)) {
5075 if (unlikely(objt_applet(s->target))) {
5076 s->flags |= SF_FINST_R;
5077 }
5078 else
5079 s->flags |= SF_FINST_C;
5080 }
5081 else {
5082 if (s->txn->rsp.msg_state < HTTP_MSG_DATA) {
5083 /* We are still processing the response headers */
5084 s->flags |= SF_FINST_H;
5085 }
5086 // (res >= done) & (res->flags & shutw)
5087 else if (s->txn->rsp.msg_state >= HTTP_MSG_DONE &&
5088 (s->flags & (SF_ERR_CLITO|SF_ERR_CLICL))) {
5089 /* A client error was reported and we are
5090 * transmitting the last block of data
5091 */
5092 s->flags |= SF_FINST_L;
5093 }
5094 else {
5095 /* Otherwise we are in DATA phase on both sides */
5096 s->flags |= SF_FINST_D;
5097 }
5098 }
5099 }
5100}
5101
5102
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005103DECLARE_POOL(pool_head_http_txn, "http_txn", sizeof(struct http_txn));
Christopher Faulet0f226952018-10-22 09:29:56 +02005104
Christopher Fauletf4eb75d2018-10-11 15:55:07 +02005105/*
5106 * Local variables:
5107 * c-indent-level: 8
5108 * c-basic-offset: 8
5109 * End:
5110 */