blob: 4f1decdbbd4c7b5c77fcefad76adfc62f602e8f5 [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 DARRAGONb2e2ec52023-01-09 11:09:03 +0100665 /* handle http extensions (if configured) */
666 if (unlikely(!http_handle_7239_header(s, req)))
667 goto return_fail_rewrite;
668 if (unlikely(!http_handle_xff_header(s, req)))
669 goto return_fail_rewrite;
670 if (unlikely(!http_handle_xot_header(s, req)))
671 goto return_fail_rewrite;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200672
Christopher Fauletc2ac5e42021-03-08 18:20:09 +0100673 /* Filter the request headers if there are filters attached to the
674 * stream.
675 */
676 if (HAS_FILTERS(s))
677 req->analysers |= AN_REQ_FLT_HTTP_HDRS;
678
Christopher Faulete0768eb2018-10-03 16:38:02 +0200679 /* If we have no server assigned yet and we're balancing on url_param
680 * with a POST request, we may be interested in checking the body for
681 * that parameter. This will be done in another analyser.
682 */
683 if (!(s->flags & (SF_ASSIGNED|SF_DIRECT)) &&
Willy Tarreau089eaa02019-01-14 15:17:46 +0100684 s->txn->meth == HTTP_METH_POST &&
685 (s->be->lbprm.algo & BE_LB_ALGO) == BE_LB_ALGO_PH) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200686 channel_dont_connect(req);
687 req->analysers |= AN_REQ_HTTP_BODY;
688 }
689
690 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
691 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau1a18b542018-12-11 16:37:42 +0100692
Christopher Faulete0768eb2018-10-03 16:38:02 +0200693 /* We expect some data from the client. Unless we know for sure
694 * we already have a full request, we have to re-enable quick-ack
695 * in case we previously disabled it, otherwise we might cause
696 * the client to delay further data.
697 */
William Lallemand36119de2021-03-08 15:26:48 +0100698 if ((sess->listener && (sess->listener->options & LI_O_NOQUICKACK)) && !(htx->flags & HTX_FL_EOM))
Willy Tarreau1a18b542018-12-11 16:37:42 +0100699 conn_set_quickack(cli_conn, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200700
701 /*************************************************************
702 * OK, that's finished for the headers. We have done what we *
703 * could. Let's switch to the DATA state. *
704 ************************************************************/
705 req->analyse_exp = TICK_ETERNITY;
706 req->analysers &= ~an_bit;
707
708 s->logs.tv_request = now;
709 /* OK let's go on with the BODY now */
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100710 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200711 return 1;
712
Christopher Fauletd649b572022-06-01 17:42:35 +0200713 return_fail_rewrite:
714 if (!(s->flags & SF_ERR_MASK))
715 s->flags |= SF_ERR_PRXCOND;
716 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_rewrites);
717 if (s->flags & SF_BE_ASSIGNED)
718 _HA_ATOMIC_INC(&s->be->be_counters.failed_rewrites);
719 if (sess->listener && sess->listener->counters)
720 _HA_ATOMIC_INC(&sess->listener->counters->failed_rewrites);
721 if (objt_server(s->target))
722 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_rewrites);
723 /* fall through */
724
Christopher Fauletb8a53712019-12-16 11:29:38 +0100725 return_int_err:
726 txn->status = 500;
727 if (!(s->flags & SF_ERR_MASK))
728 s->flags |= SF_ERR_INTERNAL;
Willy Tarreau4781b152021-04-06 13:53:36 +0200729 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100730 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau4781b152021-04-06 13:53:36 +0200731 _HA_ATOMIC_INC(&s->be->be_counters.internal_errors);
William Lallemand36119de2021-03-08 15:26:48 +0100732 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200733 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100734
Christopher Fauletb8a53712019-12-16 11:29:38 +0100735 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet4a66c942023-01-13 09:43:21 +0100736 http_set_term_flags(s);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100737
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100738 DBG_TRACE_DEVEL("leaving on error",
739 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200740 return 0;
741}
742
743/* This function is an analyser which processes the HTTP tarpit. It always
744 * returns zero, at the beginning because it prevents any other processing
745 * from occurring, and at the end because it terminates the request.
746 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200747int http_process_tarpit(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200748{
749 struct http_txn *txn = s->txn;
750
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100751 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, &txn->req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200752 /* This connection is being tarpitted. The CLIENT side has
753 * already set the connect expiration date to the right
754 * timeout. We just have to check that the client is still
755 * there and that the timeout has not expired.
756 */
757 channel_dont_connect(req);
Christopher Faulet4b490b72023-01-04 11:55:15 +0100758 if (!(req->flags & CF_SHUTR) &&
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100759 !tick_is_expired(req->analyse_exp, now_ms)) {
Christopher Fauletb0c87f12021-10-29 14:37:07 +0200760 /* Be sure to drain all data from the request channel */
761 channel_htx_erase(req, htxbuf(&req->buf));
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100762 DBG_TRACE_DEVEL("waiting for tarpit timeout expiry",
763 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200764 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100765 }
766
Christopher Faulete0768eb2018-10-03 16:38:02 +0200767
768 /* We will set the queue timer to the time spent, just for
769 * logging purposes. We fake a 500 server error, so that the
770 * attacker will not suspect his connection has been tarpitted.
771 * It will not cause trouble to the logs because we can exclude
772 * the tarpitted connections by filtering on the 'PT' status flags.
773 */
774 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
775
Christopher Faulet8dfeccf2020-05-15 14:16:29 +0200776 http_reply_and_close(s, txn->status, (!(req->flags & CF_READ_ERROR) ? http_error_message(s) : NULL));
Christopher Faulet4a66c942023-01-13 09:43:21 +0100777 http_set_term_flags(s);
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100778
779 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200780 return 0;
781}
782
783/* This function is an analyser which waits for the HTTP request body. It waits
784 * for either the buffer to be full, or the full advertised contents to have
785 * reached the buffer. It must only be called after the standard HTTP request
786 * processing has occurred, because it expects the request to be parsed and will
787 * look for the Expect header. It may send a 100-Continue interim response. It
788 * takes in input any state starting from HTTP_MSG_BODY and leaves with one of
789 * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it
790 * needs to read more data, or 1 once it has completed its analysis.
791 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200792int http_wait_for_request_body(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200793{
794 struct session *sess = s->sess;
795 struct http_txn *txn = s->txn;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200796
Christopher Fauletf4569bb2023-01-13 10:49:31 +0100797 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, &s->txn->req);
Christopher Fauletf76ebe82018-10-24 11:16:22 +0200798
Christopher Fauletf76ebe82018-10-24 11:16:22 +0200799
Christopher Faulet021a8e42021-03-29 10:46:38 +0200800 switch (http_wait_for_msg_body(s, req, s->be->timeout.httpreq, 0)) {
801 case HTTP_RULE_RES_CONT:
802 goto http_end;
803 case HTTP_RULE_RES_YIELD:
804 goto missing_data_or_waiting;
805 case HTTP_RULE_RES_BADREQ:
Willy Tarreau4236f032019-03-05 10:43:32 +0100806 goto return_bad_req;
Christopher Faulet021a8e42021-03-29 10:46:38 +0200807 case HTTP_RULE_RES_ERROR:
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200808 goto return_int_err;
Christopher Faulet021a8e42021-03-29 10:46:38 +0200809 case HTTP_RULE_RES_ABRT:
Christopher Fauletb8a53712019-12-16 11:29:38 +0100810 goto return_prx_cond;
Christopher Faulet021a8e42021-03-29 10:46:38 +0200811 default:
812 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200813 }
814
815 http_end:
816 /* The situation will not evolve, so let's give up on the analysis. */
817 s->logs.tv_request = now; /* update the request timer to reflect full request */
818 req->analysers &= ~an_bit;
819 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100820 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200821 return 1;
822
Christopher Faulet021a8e42021-03-29 10:46:38 +0200823 missing_data_or_waiting:
824 channel_dont_connect(req);
825 DBG_TRACE_DEVEL("waiting for more data",
826 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
827 return 0;
828
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200829 return_int_err:
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200830 txn->status = 500;
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200831 if (!(s->flags & SF_ERR_MASK))
832 s->flags |= SF_ERR_INTERNAL;
Willy Tarreau4781b152021-04-06 13:53:36 +0200833 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100834 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau4781b152021-04-06 13:53:36 +0200835 _HA_ATOMIC_INC(&s->be->be_counters.internal_errors);
William Lallemand36119de2021-03-08 15:26:48 +0100836 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200837 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Faulet021a8e42021-03-29 10:46:38 +0200838 goto return_prx_err;
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200839
Christopher Faulete0768eb2018-10-03 16:38:02 +0200840 return_bad_req: /* let's centralize all bad requests */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200841 txn->status = 400;
Willy Tarreau4781b152021-04-06 13:53:36 +0200842 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req);
William Lallemand36119de2021-03-08 15:26:48 +0100843 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200844 _HA_ATOMIC_INC(&sess->listener->counters->failed_req);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100845 /* fall through */
846
Christopher Faulet021a8e42021-03-29 10:46:38 +0200847 return_prx_err:
Christopher Fauletb8a53712019-12-16 11:29:38 +0100848 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet021a8e42021-03-29 10:46:38 +0200849 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200850
Christopher Faulet021a8e42021-03-29 10:46:38 +0200851 return_prx_cond:
Christopher Fauletf0d80df2023-01-13 10:20:20 +0100852 http_set_term_flags(s);
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200853
Christopher Faulete0768eb2018-10-03 16:38:02 +0200854 req->analysers &= AN_REQ_FLT_END;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100855 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100856 DBG_TRACE_DEVEL("leaving on error",
857 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200858 return 0;
859}
860
861/* This function is an analyser which forwards request body (including chunk
862 * sizes if any). It is called as soon as we must forward, even if we forward
863 * zero byte. The only situation where it must not be called is when we're in
864 * tunnel mode and we want to forward till the close. It's used both to forward
865 * remaining data and to resync after end of body. It expects the msg_state to
866 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
867 * read more data, or 1 once we can go on with next request or end the stream.
868 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
869 * bytes of pending data + the headers if not already done.
870 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200871int http_request_forward_body(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200872{
873 struct session *sess = s->sess;
874 struct http_txn *txn = s->txn;
Christopher Faulet9768c262018-10-22 09:34:31 +0200875 struct http_msg *msg = &txn->req;
876 struct htx *htx;
Christopher Faulet93e02d82019-03-08 14:18:50 +0100877 short status = 0;
Christopher Fauletaed82cf2018-11-30 22:22:32 +0100878 int ret;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200879
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100880 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200881
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100882 htx = htxbuf(&req->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200883
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200884 if (htx->flags & HTX_FL_PARSING_ERROR)
885 goto return_bad_req;
886 if (htx->flags & HTX_FL_PROCESSING_ERROR)
887 goto return_int_err;
888
Christopher Faulete0768eb2018-10-03 16:38:02 +0200889 /* Note that we don't have to send 100-continue back because we don't
890 * need the data to complete our job, and it's up to the server to
891 * decide whether to return 100, 417 or anything else in return of
892 * an "Expect: 100-continue" header.
893 */
Christopher Faulet9768c262018-10-22 09:34:31 +0200894 if (msg->msg_state == HTTP_MSG_BODY)
895 msg->msg_state = HTTP_MSG_DATA;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200896
Christopher Faulete0768eb2018-10-03 16:38:02 +0200897 /* in most states, we should abort in case of early close */
898 channel_auto_close(req);
899
900 if (req->to_forward) {
Christopher Faulet66af0b22019-03-22 14:54:52 +0100901 if (req->to_forward == CHN_INFINITE_FORWARD) {
Christopher Faulet1a3e0272019-11-15 16:31:46 +0100902 if (req->flags & CF_EOI)
903 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet66af0b22019-03-22 14:54:52 +0100904 }
905 else {
906 /* We can't process the buffer's contents yet */
907 req->flags |= CF_WAKE_WRITE;
908 goto missing_data_or_waiting;
909 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200910 }
911
Christopher Faulet1a3e0272019-11-15 16:31:46 +0100912 if (msg->msg_state >= HTTP_MSG_ENDING)
913 goto ending;
914
915 if (txn->meth == HTTP_METH_CONNECT) {
916 msg->msg_state = HTTP_MSG_ENDING;
917 goto ending;
918 }
919
Christopher Fauletaed82cf2018-11-30 22:22:32 +0100920 /* Forward input data. We get it by removing all outgoing data not
921 * forwarded yet from HTX data size. If there are some data filters, we
922 * let them decide the amount of data to forward.
Christopher Faulet9768c262018-10-22 09:34:31 +0200923 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +0100924 if (HAS_REQ_DATA_FILTERS(s)) {
925 ret = flt_http_payload(s, msg, htx->data);
926 if (ret < 0)
927 goto return_bad_req;
Christopher Faulet421e7692019-06-13 11:16:45 +0200928 c_adv(req, ret);
Christopher Fauletaed82cf2018-11-30 22:22:32 +0100929 }
930 else {
Christopher Faulet421e7692019-06-13 11:16:45 +0200931 c_adv(req, htx->data - co_data(req));
Christopher Faulet66af0b22019-03-22 14:54:52 +0100932 if (msg->flags & HTTP_MSGF_XFER_LEN)
933 channel_htx_forward_forever(req, htx);
Christopher Fauletaed82cf2018-11-30 22:22:32 +0100934 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200935
Christopher Faulet1a3e0272019-11-15 16:31:46 +0100936 if (htx->data != co_data(req))
937 goto missing_data_or_waiting;
Christopher Fauletd20fdb02019-06-13 16:43:22 +0200938
Christopher Faulet9768c262018-10-22 09:34:31 +0200939 /* Check if the end-of-message is reached and if so, switch the message
Christopher Fauletd20fdb02019-06-13 16:43:22 +0200940 * in HTTP_MSG_ENDING state. Then if all data was marked to be
941 * forwarded, set the state to HTTP_MSG_DONE.
Christopher Faulet9768c262018-10-22 09:34:31 +0200942 */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +0100943 if (!(htx->flags & HTX_FL_EOM))
Christopher Faulet9768c262018-10-22 09:34:31 +0200944 goto missing_data_or_waiting;
945
Christopher Fauletd20fdb02019-06-13 16:43:22 +0200946 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet9768c262018-10-22 09:34:31 +0200947
Christopher Faulet1a3e0272019-11-15 16:31:46 +0100948 ending:
Christopher Faulet2151cdd2020-07-22 16:34:59 +0200949 req->flags &= ~CF_EXPECT_MORE; /* no more data are expected */
950
Christopher Faulet1a3e0272019-11-15 16:31:46 +0100951 /* other states, ENDING...TUNNEL */
952 if (msg->msg_state >= HTTP_MSG_DONE)
953 goto done;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200954
Christopher Fauletaed82cf2018-11-30 22:22:32 +0100955 if (HAS_REQ_DATA_FILTERS(s)) {
956 ret = flt_http_end(s, msg);
957 if (ret <= 0) {
958 if (!ret)
959 goto missing_data_or_waiting;
960 goto return_bad_req;
961 }
962 }
963
Christopher Faulet1a3e0272019-11-15 16:31:46 +0100964 if (txn->meth == HTTP_METH_CONNECT)
965 msg->msg_state = HTTP_MSG_TUNNEL;
966 else {
967 msg->msg_state = HTTP_MSG_DONE;
968 req->to_forward = 0;
969 }
970
971 done:
972 /* we don't want to forward closes on DONE except in tunnel mode. */
973 if (!(txn->flags & TX_CON_WANT_TUN))
974 channel_dont_close(req);
975
Christopher Fauletf2b02cf2023-01-13 11:02:28 +0100976 if ((req->flags & CF_SHUTW) && co_data(req)) {
977 /* request errors are most likely due to the server aborting the
978 * transfer. */
979 goto return_srv_abort;
980 }
981
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200982 http_end_request(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200983 if (!(req->analysers & an_bit)) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100984 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200985 return 1;
986 }
987
988 /* If "option abortonclose" is set on the backend, we want to monitor
989 * the client's connection and forward any shutdown notification to the
990 * server, which will decide whether to close or to go on processing the
991 * request. We only do that in tunnel mode, and not in other modes since
992 * it can be abused to exhaust source ports. */
Christopher Faulet769d0e92019-03-22 14:23:18 +0100993 if (s->be->options & PR_O_ABRT_CLOSE) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200994 channel_auto_read(req);
Christopher Faulet446d8032022-12-12 07:53:18 +0100995 if ((req->flags & CF_SHUTR) && !(txn->flags & TX_CON_WANT_TUN))
Willy Tarreaucb041662022-05-17 19:44:42 +0200996 s->scb->flags |= SC_FL_NOLINGER;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200997 channel_auto_close(req);
998 }
999 else if (s->txn->meth == HTTP_METH_POST) {
1000 /* POST requests may require to read extra CRLF sent by broken
1001 * browsers and which could cause an RST to be sent upon close
1002 * on some systems (eg: Linux). */
1003 channel_auto_read(req);
1004 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001005 DBG_TRACE_DEVEL("waiting for the end of the HTTP txn",
1006 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001007 return 0;
1008
1009 missing_data_or_waiting:
1010 /* stop waiting for data if the input is closed before the end */
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001011 if (msg->msg_state < HTTP_MSG_ENDING && req->flags & CF_SHUTR)
Christopher Faulet93e02d82019-03-08 14:18:50 +01001012 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001013
1014 waiting:
1015 /* waiting for the last bits to leave the buffer */
1016 if (req->flags & CF_SHUTW)
Christopher Faulet93e02d82019-03-08 14:18:50 +01001017 goto return_srv_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001018
1019 /* When TE: chunked is used, we need to get there again to parse remaining
1020 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
1021 * And when content-length is used, we never want to let the possible
1022 * shutdown be forwarded to the other side, as the state machine will
1023 * take care of it once the client responds. It's also important to
1024 * prevent TIME_WAITs from accumulating on the backend side, and for
1025 * HTTP/2 where the last frame comes with a shutdown.
1026 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001027 if (msg->flags & HTTP_MSGF_XFER_LEN)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001028 channel_dont_close(req);
1029
1030 /* We know that more data are expected, but we couldn't send more that
1031 * what we did. So we always set the CF_EXPECT_MORE flag so that the
1032 * system knows it must not set a PUSH on this first part. Interactive
1033 * modes are already handled by the stream sock layer. We must not do
1034 * this in content-length mode because it could present the MSG_MORE
1035 * flag with the last block of forwarded data, which would cause an
1036 * additional delay to be observed by the receiver.
1037 */
Christopher Faulet2151cdd2020-07-22 16:34:59 +02001038 if (HAS_REQ_DATA_FILTERS(s))
Christopher Faulete0768eb2018-10-03 16:38:02 +02001039 req->flags |= CF_EXPECT_MORE;
1040
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001041 DBG_TRACE_DEVEL("waiting for more data to forward",
1042 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001043 return 0;
1044
Christopher Faulet93e02d82019-03-08 14:18:50 +01001045 return_cli_abort:
Willy Tarreau4781b152021-04-06 13:53:36 +02001046 _HA_ATOMIC_INC(&sess->fe->fe_counters.cli_aborts);
1047 _HA_ATOMIC_INC(&s->be->be_counters.cli_aborts);
William Lallemand36119de2021-03-08 15:26:48 +01001048 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001049 _HA_ATOMIC_INC(&sess->listener->counters->cli_aborts);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001050 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001051 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.cli_aborts);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001052 if (!(s->flags & SF_ERR_MASK))
Christopher Fauletf2b02cf2023-01-13 11:02:28 +01001053 s->flags |= ((req->flags & CF_READ_TIMEOUT) ? SF_ERR_CLITO : SF_ERR_CLICL);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001054 status = 400;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001055 goto return_prx_cond;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001056
1057 return_srv_abort:
Willy Tarreau4781b152021-04-06 13:53:36 +02001058 _HA_ATOMIC_INC(&sess->fe->fe_counters.srv_aborts);
1059 _HA_ATOMIC_INC(&s->be->be_counters.srv_aborts);
William Lallemand36119de2021-03-08 15:26:48 +01001060 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001061 _HA_ATOMIC_INC(&sess->listener->counters->srv_aborts);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001062 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001063 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.srv_aborts);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001064 if (!(s->flags & SF_ERR_MASK))
Christopher Fauletf2b02cf2023-01-13 11:02:28 +01001065 s->flags |= ((req->flags & CF_WRITE_TIMEOUT) ? SF_ERR_SRVTO : SF_ERR_SRVCL);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001066 status = 502;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001067 goto return_prx_cond;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001068
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001069 return_int_err:
1070 if (!(s->flags & SF_ERR_MASK))
1071 s->flags |= SF_ERR_INTERNAL;
Willy Tarreau4781b152021-04-06 13:53:36 +02001072 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
1073 _HA_ATOMIC_INC(&s->be->be_counters.internal_errors);
William Lallemand36119de2021-03-08 15:26:48 +01001074 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001075 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001076 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001077 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.internal_errors);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001078 status = 500;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001079 goto return_prx_cond;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001080
Christopher Faulet93e02d82019-03-08 14:18:50 +01001081 return_bad_req:
Willy Tarreau4781b152021-04-06 13:53:36 +02001082 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req);
William Lallemand36119de2021-03-08 15:26:48 +01001083 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001084 _HA_ATOMIC_INC(&sess->listener->counters->failed_req);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001085 status = 400;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001086 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001087
Christopher Fauletb8a53712019-12-16 11:29:38 +01001088 return_prx_cond:
Christopher Faulet9768c262018-10-22 09:34:31 +02001089 if (txn->status > 0) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001090 /* Note: we don't send any error if some data were already sent */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001091 http_reply_and_close(s, txn->status, NULL);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001092 } else {
Christopher Faulet93e02d82019-03-08 14:18:50 +01001093 txn->status = status;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001094 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001095 }
Christopher Fauletf2b02cf2023-01-13 11:02:28 +01001096 http_set_term_flags(s);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001097 DBG_TRACE_DEVEL("leaving on error ",
1098 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001099 return 0;
1100}
1101
Willy Tarreau4596fe22022-05-17 19:07:51 +02001102/* Reset the stream and the backend stream connector to a situation suitable for attemption connection */
Olivier Houcharda254a372019-04-05 15:30:12 +02001103/* Returns 0 if we can attempt to retry, -1 otherwise */
Willy Tarreaub49672d2022-05-27 10:13:37 +02001104static __inline int do_l7_retry(struct stream *s, struct stconn *sc)
Olivier Houcharda254a372019-04-05 15:30:12 +02001105{
Christopher Faulet9f5382e2021-05-21 13:46:14 +02001106 struct channel *req, *res;
1107 int co_data;
Olivier Houcharda254a372019-04-05 15:30:12 +02001108
Christopher Faulet731c8e62022-03-29 16:08:44 +02001109 s->conn_retries++;
1110 if (s->conn_retries >= s->be->conn_retries)
Christopher Faulet552601d2021-05-26 10:31:06 +02001111 return -1;
Christopher Faulet5b82cc52020-10-12 15:18:50 +02001112
Christopher Faulete763c8c2021-05-05 18:23:59 +02001113 if (objt_server(s->target)) {
1114 if (s->flags & SF_CURR_SESS) {
1115 s->flags &= ~SF_CURR_SESS;
1116 _HA_ATOMIC_DEC(&__objt_server(s->target)->cur_sess);
1117 }
Willy Tarreau4781b152021-04-06 13:53:36 +02001118 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.retries);
Christopher Faulete763c8c2021-05-05 18:23:59 +02001119 }
Willy Tarreau4781b152021-04-06 13:53:36 +02001120 _HA_ATOMIC_INC(&s->be->be_counters.retries);
Willy Tarreau223995e2019-05-04 10:38:31 +02001121
Christopher Faulet9f5382e2021-05-21 13:46:14 +02001122 req = &s->req;
1123 res = &s->res;
Olivier Houcharda254a372019-04-05 15:30:12 +02001124 /* Remove any write error from the request, and read error from the response */
1125 req->flags &= ~(CF_WRITE_ERROR | CF_WRITE_TIMEOUT | CF_SHUTW | CF_SHUTW_NOW);
Christopher Faulet6e1bbc42022-12-12 08:08:15 +01001126 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 +01001127 res->analysers &= AN_RES_FLT_END;
Christopher Faulet50264b42022-03-30 19:39:30 +02001128 s->conn_err_type = STRM_ET_NONE;
Christopher Fauletae024ce2022-03-29 19:02:31 +02001129 s->flags &= ~(SF_CONN_EXP | SF_ERR_MASK | SF_FINST_MASK);
1130 s->conn_exp = TICK_ETERNITY;
Olivier Houchard4bd58672019-07-12 16:16:59 +02001131 stream_choose_redispatch(s);
Olivier Houcharda254a372019-04-05 15:30:12 +02001132 res->rex = TICK_ETERNITY;
1133 res->to_forward = 0;
1134 res->analyse_exp = TICK_ETERNITY;
1135 res->total = 0;
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +01001136
Willy Tarreau19c65a92022-05-27 08:49:24 +02001137 if (sc_reset_endp(s->scb) < 0) {
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +01001138 if (!(s->flags & SF_ERR_MASK))
1139 s->flags |= SF_ERR_INTERNAL;
1140 return -1;
1141 }
Olivier Houcharda254a372019-04-05 15:30:12 +02001142
Christopher Faulet9f5382e2021-05-21 13:46:14 +02001143 b_free(&req->buf);
1144 /* Swap the L7 buffer with the channel buffer */
1145 /* We know we stored the co_data as b_data, so get it there */
Christopher Faulete05bf9e2022-03-29 15:23:40 +02001146 co_data = b_data(&s->txn->l7_buffer);
1147 b_set_data(&s->txn->l7_buffer, b_size(&s->txn->l7_buffer));
1148 b_xfer(&req->buf, &s->txn->l7_buffer, b_data(&s->txn->l7_buffer));
Christopher Faulet9f5382e2021-05-21 13:46:14 +02001149 co_set_data(req, co_data);
Christopher Faulet5b82cc52020-10-12 15:18:50 +02001150
Ilya Shipitsinacf84592021-02-06 22:29:08 +05001151 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 +02001152
Olivier Houcharda254a372019-04-05 15:30:12 +02001153 b_reset(&res->buf);
1154 co_set_data(res, 0);
1155 return 0;
1156}
1157
Christopher Faulete0768eb2018-10-03 16:38:02 +02001158/* This stream analyser waits for a complete HTTP response. It returns 1 if the
1159 * processing can continue on next analysers, or zero if it either needs more
1160 * data or wants to immediately abort the response (eg: timeout, error, ...). It
1161 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->res.analysers
1162 * when it has nothing left to do, and may remove any analyser when it wants to
1163 * abort.
1164 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001165int http_wait_for_response(struct stream *s, struct channel *rep, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001166{
Christopher Faulet9768c262018-10-22 09:34:31 +02001167 /*
1168 * We will analyze a complete HTTP response to check the its syntax.
1169 *
1170 * Once the start line and all headers are received, we may perform a
1171 * capture of the error (if any), and we will set a few fields. We also
1172 * logging and finally headers capture.
1173 */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001174 struct session *sess = s->sess;
1175 struct http_txn *txn = s->txn;
1176 struct http_msg *msg = &txn->rsp;
Christopher Faulet9768c262018-10-22 09:34:31 +02001177 struct htx *htx;
Christopher Faulet61608322018-11-23 16:23:45 +01001178 struct connection *srv_conn;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001179 struct htx_sl *sl;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001180 int n;
1181
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001182 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001183
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001184 htx = htxbuf(&rep->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001185
Willy Tarreau4236f032019-03-05 10:43:32 +01001186 /* Parsing errors are caught here */
1187 if (htx->flags & HTX_FL_PARSING_ERROR)
1188 goto return_bad_res;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001189 if (htx->flags & HTX_FL_PROCESSING_ERROR)
1190 goto return_int_err;
Willy Tarreau4236f032019-03-05 10:43:32 +01001191
Christopher Faulete0768eb2018-10-03 16:38:02 +02001192 /*
1193 * Now we quickly check if we have found a full valid response.
1194 * If not so, we check the FD and buffer states before leaving.
1195 * A full response is indicated by the fact that we have seen
1196 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
1197 * responses are checked first.
1198 *
1199 * Depending on whether the client is still there or not, we
1200 * may send an error response back or not. Note that normally
1201 * we should only check for HTTP status there, and check I/O
1202 * errors somewhere else.
1203 */
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001204 next_one:
Christopher Faulet29f17582019-05-23 11:03:26 +02001205 if (unlikely(htx_is_empty(htx) || htx->first == -1)) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001206 /* 1: have we encountered a read error ? */
1207 if (rep->flags & CF_READ_ERROR) {
Willy Tarreaufd9417b2022-05-18 16:23:22 +02001208 struct connection *conn = sc_conn(s->scb);
Olivier Houchard865d8392019-05-03 22:46:27 +02001209
Christopher Fauletd9769232021-05-26 12:15:37 +02001210 /* Perform a L7 retry because server refuses the early data. */
Christopher Faulete05bf9e2022-03-29 15:23:40 +02001211 if ((txn->flags & TX_L7_RETRY) &&
Christopher Fauletd9769232021-05-26 12:15:37 +02001212 (s->be->retry_type & PR_RE_EARLY_ERROR) &&
1213 conn && conn->err_code == CO_ER_SSL_EARLY_FAILED &&
Willy Tarreau7cb9e6c2022-05-17 19:40:40 +02001214 do_l7_retry(s, s->scb) == 0) {
Christopher Fauletd9769232021-05-26 12:15:37 +02001215 DBG_TRACE_DEVEL("leaving on L7 retry",
1216 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
1217 return 0;
1218 }
1219
Olivier Houchard6db16992019-05-17 15:40:49 +02001220 if (txn->flags & TX_NOT_FIRST)
1221 goto abort_keep_alive;
1222
Willy Tarreau4781b152021-04-06 13:53:36 +02001223 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001224 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001225 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001226 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001227 }
1228
Christopher Fauletd9769232021-05-26 12:15:37 +02001229 /* if the server refused the early data, just send a 425 */
1230 if (conn && conn->err_code == CO_ER_SSL_EARLY_FAILED)
Olivier Houchard865d8392019-05-03 22:46:27 +02001231 txn->status = 425;
Christopher Fauletd9769232021-05-26 12:15:37 +02001232 else {
1233 txn->status = 502;
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001234 stream_inc_http_fail_ctr(s);
Christopher Fauletd9769232021-05-26 12:15:37 +02001235 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001236
Willy Tarreaucb041662022-05-17 19:44:42 +02001237 s->scb->flags |= SC_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001238 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001239
1240 if (!(s->flags & SF_ERR_MASK))
1241 s->flags |= SF_ERR_SRVCL;
Christopher Faulet4a66c942023-01-13 09:43:21 +01001242 http_set_term_flags(s);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001243 DBG_TRACE_DEVEL("leaving on error",
1244 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001245 return 0;
1246 }
1247
Christopher Faulet9768c262018-10-22 09:34:31 +02001248 /* 2: read timeout : return a 504 to the client. */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001249 else if (rep->flags & CF_READ_TIMEOUT) {
Christopher Faulete05bf9e2022-03-29 15:23:40 +02001250 if ((txn->flags & TX_L7_RETRY) &&
Olivier Houcharda254a372019-04-05 15:30:12 +02001251 (s->be->retry_type & PR_RE_TIMEOUT)) {
Willy Tarreau7cb9e6c2022-05-17 19:40:40 +02001252 if (co_data(rep) || do_l7_retry(s, s->scb) == 0) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001253 DBG_TRACE_DEVEL("leaving on L7 retry",
1254 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharda254a372019-04-05 15:30:12 +02001255 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001256 }
Olivier Houcharda254a372019-04-05 15:30:12 +02001257 }
Willy Tarreau4781b152021-04-06 13:53:36 +02001258 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001259 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001260 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001261 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001262 }
1263
Christopher Faulete0768eb2018-10-03 16:38:02 +02001264 txn->status = 504;
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001265 stream_inc_http_fail_ctr(s);
Willy Tarreaucb041662022-05-17 19:44:42 +02001266 s->scb->flags |= SC_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001267 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001268
1269 if (!(s->flags & SF_ERR_MASK))
1270 s->flags |= SF_ERR_SRVTO;
Christopher Faulet4a66c942023-01-13 09:43:21 +01001271 http_set_term_flags(s);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001272 DBG_TRACE_DEVEL("leaving on error",
1273 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001274 return 0;
1275 }
1276
Christopher Faulet9768c262018-10-22 09:34:31 +02001277 /* 3: client abort with an abortonclose */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001278 else if ((rep->flags & CF_SHUTR) && ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001279 _HA_ATOMIC_INC(&sess->fe->fe_counters.cli_aborts);
1280 _HA_ATOMIC_INC(&s->be->be_counters.cli_aborts);
William Lallemand36119de2021-03-08 15:26:48 +01001281 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001282 _HA_ATOMIC_INC(&sess->listener->counters->cli_aborts);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001283 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001284 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.cli_aborts);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001285
Christopher Faulete0768eb2018-10-03 16:38:02 +02001286 txn->status = 400;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001287 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001288
1289 if (!(s->flags & SF_ERR_MASK))
1290 s->flags |= SF_ERR_CLICL;
Christopher Faulet4a66c942023-01-13 09:43:21 +01001291 http_set_term_flags(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001292
1293 /* process_stream() will take care of the error */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001294 DBG_TRACE_DEVEL("leaving on error",
1295 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001296 return 0;
1297 }
1298
Christopher Faulet9768c262018-10-22 09:34:31 +02001299 /* 4: close from server, capture the response if the server has started to respond */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001300 else if (rep->flags & CF_SHUTR) {
Christopher Faulete05bf9e2022-03-29 15:23:40 +02001301 if ((txn->flags & TX_L7_RETRY) &&
Olivier Houcharda254a372019-04-05 15:30:12 +02001302 (s->be->retry_type & PR_RE_DISCONNECTED)) {
Willy Tarreau7cb9e6c2022-05-17 19:40:40 +02001303 if (co_data(rep) || do_l7_retry(s, s->scb) == 0) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001304 DBG_TRACE_DEVEL("leaving on L7 retry",
1305 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharda254a372019-04-05 15:30:12 +02001306 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001307 }
Olivier Houcharda254a372019-04-05 15:30:12 +02001308 }
1309
Olivier Houchard6db16992019-05-17 15:40:49 +02001310 if (txn->flags & TX_NOT_FIRST)
1311 goto abort_keep_alive;
1312
Willy Tarreau4781b152021-04-06 13:53:36 +02001313 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001314 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001315 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001316 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001317 }
1318
Christopher Faulete0768eb2018-10-03 16:38:02 +02001319 txn->status = 502;
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001320 stream_inc_http_fail_ctr(s);
Willy Tarreaucb041662022-05-17 19:44:42 +02001321 s->scb->flags |= SC_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001322 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001323
1324 if (!(s->flags & SF_ERR_MASK))
1325 s->flags |= SF_ERR_SRVCL;
Christopher Faulet4a66c942023-01-13 09:43:21 +01001326 http_set_term_flags(s);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001327 DBG_TRACE_DEVEL("leaving on error",
1328 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001329 return 0;
1330 }
1331
Christopher Faulet9768c262018-10-22 09:34:31 +02001332 /* 5: write error to client (we don't send any message then) */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001333 else if (rep->flags & CF_WRITE_ERROR) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001334 if (txn->flags & TX_NOT_FIRST)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001335 goto abort_keep_alive;
1336
Willy Tarreau4781b152021-04-06 13:53:36 +02001337 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001338 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001339 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001340 rep->analysers &= AN_RES_FLT_END;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001341
1342 if (!(s->flags & SF_ERR_MASK))
1343 s->flags |= SF_ERR_CLICL;
Christopher Faulet4a66c942023-01-13 09:43:21 +01001344 http_set_term_flags(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001345
1346 /* process_stream() will take care of the error */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001347 DBG_TRACE_DEVEL("leaving on error",
1348 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001349 return 0;
1350 }
1351
1352 channel_dont_close(rep);
1353 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001354 DBG_TRACE_DEVEL("waiting for more data",
1355 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001356 return 0;
1357 }
1358
1359 /* More interesting part now : we know that we have a complete
1360 * response which at least looks like HTTP. We have an indicator
1361 * of each header's length, so we can parse them quickly.
1362 */
Christopher Faulet29f17582019-05-23 11:03:26 +02001363 BUG_ON(htx_get_first_type(htx) != HTX_BLK_RES_SL);
Christopher Faulet297fbb42019-05-13 14:41:27 +02001364 sl = http_get_stline(htx);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001365
Christopher Faulet1f08bff2021-05-26 13:14:39 +02001366 /* Perform a L7 retry because of the status code */
Christopher Faulete05bf9e2022-03-29 15:23:40 +02001367 if ((txn->flags & TX_L7_RETRY) &&
Christopher Faulet1f08bff2021-05-26 13:14:39 +02001368 l7_status_match(s->be, sl->info.res.status) &&
Willy Tarreau7cb9e6c2022-05-17 19:40:40 +02001369 do_l7_retry(s, s->scb) == 0) {
Christopher Faulet1f08bff2021-05-26 13:14:39 +02001370 DBG_TRACE_DEVEL("leaving on L7 retry", STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
1371 return 0;
1372 }
1373
1374 /* Now, L7 buffer is useless, it can be released */
Christopher Faulete05bf9e2022-03-29 15:23:40 +02001375 b_free(&txn->l7_buffer);
Christopher Faulet1f08bff2021-05-26 13:14:39 +02001376
1377 msg->msg_state = HTTP_MSG_BODY;
1378
1379
Christopher Faulet9768c262018-10-22 09:34:31 +02001380 /* 0: we might have to print this header in debug mode */
1381 if (unlikely((global.mode & MODE_DEBUG) &&
1382 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
1383 int32_t pos;
1384
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001385 http_debug_stline("srvrep", s, sl);
Christopher Faulet9768c262018-10-22 09:34:31 +02001386
Christopher Fauleta3f15502019-05-13 15:27:23 +02001387 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001388 struct htx_blk *blk = htx_get_blk(htx, pos);
1389 enum htx_blk_type type = htx_get_blk_type(blk);
1390
1391 if (type == HTX_BLK_EOH)
1392 break;
1393 if (type != HTX_BLK_HDR)
1394 continue;
1395
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001396 http_debug_hdr("srvhdr", s,
1397 htx_get_blk_name(htx, blk),
1398 htx_get_blk_value(htx, blk));
Christopher Faulet9768c262018-10-22 09:34:31 +02001399 }
1400 }
1401
Christopher Faulet03599112018-11-27 11:21:21 +01001402 /* 1: get the status code and the version. Also set HTTP flags */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001403 txn->status = sl->info.res.status;
Christopher Faulet03599112018-11-27 11:21:21 +01001404 if (sl->flags & HTX_SL_F_VER_11)
Christopher Faulet9768c262018-10-22 09:34:31 +02001405 msg->flags |= HTTP_MSGF_VER_11;
Christopher Faulet03599112018-11-27 11:21:21 +01001406 if (sl->flags & HTX_SL_F_XFER_LEN) {
1407 msg->flags |= HTTP_MSGF_XFER_LEN;
Christopher Faulet2a408542020-11-20 14:22:37 +01001408 if (sl->flags & HTX_SL_F_CLEN)
1409 msg->flags |= HTTP_MSGF_CNT_LEN;
1410 else if (sl->flags & HTX_SL_F_CHNK)
1411 msg->flags |= HTTP_MSGF_TE_CHNK;
Christopher Faulet03599112018-11-27 11:21:21 +01001412 }
Christopher Faulet2a408542020-11-20 14:22:37 +01001413 if (sl->flags & HTX_SL_F_BODYLESS)
1414 msg->flags |= HTTP_MSGF_BODYLESS;
Christopher Faulet576c3582021-01-08 15:53:01 +01001415 if (sl->flags & HTX_SL_F_CONN_UPG)
1416 msg->flags |= HTTP_MSGF_CONN_UPG;
Christopher Faulet9768c262018-10-22 09:34:31 +02001417
1418 n = txn->status / 100;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001419 if (n < 1 || n > 5)
1420 n = 0;
Christopher Faulet9768c262018-10-22 09:34:31 +02001421
Christopher Faulete0768eb2018-10-03 16:38:02 +02001422 /* when the client triggers a 4xx from the server, it's most often due
1423 * to a missing object or permission. These events should be tracked
1424 * because if they happen often, it may indicate a brute force or a
1425 * vulnerability scan.
1426 */
1427 if (n == 4)
1428 stream_inc_http_err_ctr(s);
1429
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001430 if (n == 5 && txn->status != 501 && txn->status != 505)
1431 stream_inc_http_fail_ctr(s);
1432
Marcin Deranek3c27dda2020-05-15 18:32:51 +02001433 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001434 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.p.http.rsp[n]);
1435 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.p.http.cum_req);
Marcin Deranek3c27dda2020-05-15 18:32:51 +02001436 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001437
Christopher Faulete0768eb2018-10-03 16:38:02 +02001438 /* Adjust server's health based on status code. Note: status codes 501
1439 * and 505 are triggered on demand by client request, so we must not
1440 * count them as server failures.
1441 */
1442 if (objt_server(s->target)) {
1443 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001444 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_OK);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001445 else
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001446 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_STS);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001447 }
1448
1449 /*
1450 * We may be facing a 100-continue response, or any other informational
1451 * 1xx response which is non-final, in which case this is not the right
1452 * response, and we're waiting for the next one. Let's allow this response
1453 * to go to the client and wait for the next one. There's an exception for
1454 * 101 which is used later in the code to switch protocols.
1455 */
1456 if (txn->status < 200 &&
1457 (txn->status == 100 || txn->status >= 102)) {
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001458 FLT_STRM_CB(s, flt_http_reset(s, msg));
Christopher Faulet421e7692019-06-13 11:16:45 +02001459 htx->first = channel_htx_fwd_headers(rep, htx);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001460 msg->msg_state = HTTP_MSG_RPBEFORE;
Christopher Faulet3499f622019-09-03 15:23:54 +02001461 msg->flags = 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001462 txn->status = 0;
1463 s->logs.t_data = -1; /* was not a response yet */
Christopher Faulet7d518452020-08-31 11:07:07 +02001464 rep->flags |= CF_SEND_DONTWAIT; /* Send ASAP informational messages */
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001465 goto next_one;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001466 }
1467
Christopher Faulet6e6c7b12021-01-08 16:02:05 +01001468 /* A 101-switching-protocols must contains a Connection header with the
1469 * "upgrade" option and the request too. It means both are agree to
1470 * upgrade. It is not so strict because there is no test on the Upgrade
1471 * header content. But it is probably stronger enough for now.
1472 */
1473 if (txn->status == 101 &&
1474 (!(txn->req.flags & HTTP_MSGF_CONN_UPG) || !(txn->rsp.flags & HTTP_MSGF_CONN_UPG)))
1475 goto return_bad_res;
1476
Christopher Faulete0768eb2018-10-03 16:38:02 +02001477 /*
1478 * 2: check for cacheability.
1479 */
1480
1481 switch (txn->status) {
1482 case 200:
1483 case 203:
1484 case 204:
1485 case 206:
1486 case 300:
1487 case 301:
1488 case 404:
1489 case 405:
1490 case 410:
1491 case 414:
1492 case 501:
1493 break;
1494 default:
1495 /* RFC7231#6.1:
1496 * Responses with status codes that are defined as
1497 * cacheable by default (e.g., 200, 203, 204, 206,
1498 * 300, 301, 404, 405, 410, 414, and 501 in this
1499 * specification) can be reused by a cache with
1500 * heuristic expiration unless otherwise indicated
1501 * by the method definition or explicit cache
1502 * controls [RFC7234]; all other status codes are
1503 * not cacheable by default.
1504 */
1505 txn->flags &= ~(TX_CACHEABLE | TX_CACHE_COOK);
1506 break;
1507 }
1508
1509 /*
1510 * 3: we may need to capture headers
1511 */
1512 s->logs.logwait &= ~LW_RESP;
1513 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->res_cap))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001514 http_capture_headers(htx, s->res_cap, sess->fe->rsp_cap);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001515
Christopher Faulet9768c262018-10-22 09:34:31 +02001516 /* Skip parsing if no content length is possible. */
Christopher Fauletc75668e2020-12-07 18:10:32 +01001517 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status >= 200 && txn->status < 300) ||
Christopher Faulete0768eb2018-10-03 16:38:02 +02001518 txn->status == 101)) {
1519 /* Either we've established an explicit tunnel, or we're
1520 * switching the protocol. In both cases, we're very unlikely
1521 * to understand the next protocols. We have to switch to tunnel
1522 * mode, so that we transfer the request and responses then let
1523 * this protocol pass unmodified. When we later implement specific
1524 * parsers for such protocols, we'll want to check the Upgrade
1525 * header which contains information about that protocol for
1526 * responses with status 101 (eg: see RFC2817 about TLS).
1527 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02001528 txn->flags |= TX_CON_WANT_TUN;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001529 }
1530
Christopher Faulet61608322018-11-23 16:23:45 +01001531 /* check for NTML authentication headers in 401 (WWW-Authenticate) and
1532 * 407 (Proxy-Authenticate) responses and set the connection to private
1533 */
Willy Tarreaufd9417b2022-05-18 16:23:22 +02001534 srv_conn = sc_conn(s->scb);
Christopher Faulet61608322018-11-23 16:23:45 +01001535 if (srv_conn) {
1536 struct ist hdr;
1537 struct http_hdr_ctx ctx;
1538
1539 if (txn->status == 401)
1540 hdr = ist("WWW-Authenticate");
1541 else if (txn->status == 407)
1542 hdr = ist("Proxy-Authenticate");
1543 else
1544 goto end;
1545
1546 ctx.blk = NULL;
1547 while (http_find_header(htx, hdr, &ctx, 0)) {
Willy Tarreauf1dcced2020-05-07 19:27:02 +02001548 /* If www-authenticate contains "Negotiate", "Nego2", or "NTLM",
1549 * possibly followed by blanks and a base64 string, the connection
1550 * is private. Since it's a mess to deal with, we only check for
1551 * values starting with "NTLM" or "Nego". Note that often multiple
1552 * headers are sent by the server there.
1553 */
1554 if ((ctx.value.len >= 4 && strncasecmp(ctx.value.ptr, "Nego", 4) == 0) ||
Willy Tarreau49a1d282020-05-07 19:10:15 +02001555 (ctx.value.len >= 4 && strncasecmp(ctx.value.ptr, "NTLM", 4) == 0)) {
Olivier Houchard250031e2019-05-29 15:01:50 +02001556 sess->flags |= SESS_FL_PREFER_LAST;
Christopher Faulet08016ab2020-07-01 16:10:06 +02001557 conn_set_owner(srv_conn, sess, NULL);
Christopher Faulet21ddc742020-07-01 15:26:14 +02001558 conn_set_private(srv_conn);
Ilya Shipitsin6b79f382020-07-23 00:32:55 +05001559 /* If it fail now, the same will be done in mux->detach() callback */
Christopher Faulet08016ab2020-07-01 16:10:06 +02001560 session_add_conn(srv_conn->owner, srv_conn, srv_conn->target);
Willy Tarreauf1dcced2020-05-07 19:27:02 +02001561 break;
Olivier Houchard250031e2019-05-29 15:01:50 +02001562 }
Christopher Faulet61608322018-11-23 16:23:45 +01001563 }
1564 }
1565
1566 end:
Christopher Faulete0768eb2018-10-03 16:38:02 +02001567 /* we want to have the response time before we start processing it */
1568 s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now);
1569
1570 /* end of job, return OK */
1571 rep->analysers &= ~an_bit;
1572 rep->analyse_exp = TICK_ETERNITY;
1573 channel_auto_close(rep);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001574 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001575 return 1;
1576
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001577 return_int_err:
Willy Tarreau4781b152021-04-06 13:53:36 +02001578 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
1579 _HA_ATOMIC_INC(&s->be->be_counters.internal_errors);
William Lallemand36119de2021-03-08 15:26:48 +01001580 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001581 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Fauletb8a53712019-12-16 11:29:38 +01001582 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001583 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.internal_errors);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001584 txn->status = 500;
1585 if (!(s->flags & SF_ERR_MASK))
1586 s->flags |= SF_ERR_INTERNAL;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001587 goto return_prx_cond;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001588
1589 return_bad_res:
Willy Tarreau4781b152021-04-06 13:53:36 +02001590 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Faulet47365272018-10-31 17:40:50 +01001591 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001592 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001593 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Christopher Faulet47365272018-10-31 17:40:50 +01001594 }
Olivier Houcharde3249a92019-05-03 23:01:47 +02001595 if ((s->be->retry_type & PR_RE_JUNK_REQUEST) &&
Christopher Faulete05bf9e2022-03-29 15:23:40 +02001596 (txn->flags & TX_L7_RETRY) &&
Willy Tarreau7cb9e6c2022-05-17 19:40:40 +02001597 do_l7_retry(s, s->scb) == 0) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001598 DBG_TRACE_DEVEL("leaving on L7 retry",
1599 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharde3249a92019-05-03 23:01:47 +02001600 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001601 }
Christopher Faulet47365272018-10-31 17:40:50 +01001602 txn->status = 502;
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001603 stream_inc_http_fail_ctr(s);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001604 /* fall through */
1605
Christopher Fauletb8a53712019-12-16 11:29:38 +01001606 return_prx_cond:
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001607 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet4a66c942023-01-13 09:43:21 +01001608 http_set_term_flags(s);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001609
Willy Tarreaucb041662022-05-17 19:44:42 +02001610 s->scb->flags |= SC_FL_NOLINGER;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001611 DBG_TRACE_DEVEL("leaving on error",
1612 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulet47365272018-10-31 17:40:50 +01001613 return 0;
1614
Christopher Faulete0768eb2018-10-03 16:38:02 +02001615 abort_keep_alive:
1616 /* A keep-alive request to the server failed on a network error.
1617 * The client is required to retry. We need to close without returning
1618 * any other information so that the client retries.
1619 */
1620 txn->status = 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001621 s->logs.logwait = 0;
1622 s->logs.level = 0;
1623 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001624 http_reply_and_close(s, txn->status, NULL);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001625 DBG_TRACE_DEVEL("leaving by closing K/A connection",
1626 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001627 return 0;
1628}
1629
1630/* This function performs all the processing enabled for the current response.
1631 * It normally returns 1 unless it wants to break. It relies on buffers flags,
1632 * and updates s->res.analysers. It might make sense to explode it into several
1633 * other functions. It works like process_request (see indications above).
1634 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001635int http_process_res_common(struct stream *s, struct channel *rep, int an_bit, struct proxy *px)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001636{
1637 struct session *sess = s->sess;
1638 struct http_txn *txn = s->txn;
1639 struct http_msg *msg = &txn->rsp;
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001640 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001641 struct proxy *cur_proxy;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001642 enum rule_result ret = HTTP_RULE_RES_CONT;
1643
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001644 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
1645 return 0;
Christopher Faulet9768c262018-10-22 09:34:31 +02001646
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001647 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001648
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001649 htx = htxbuf(&rep->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001650
1651 /* The stats applet needs to adjust the Connection header but we don't
1652 * apply any filter there.
1653 */
1654 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
1655 rep->analysers &= ~an_bit;
1656 rep->analyse_exp = TICK_ETERNITY;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001657 goto end;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001658 }
1659
1660 /*
1661 * We will have to evaluate the filters.
1662 * As opposed to version 1.2, now they will be evaluated in the
1663 * filters order and not in the header order. This means that
1664 * each filter has to be validated among all headers.
1665 *
1666 * Filters are tried with ->be first, then with ->fe if it is
1667 * different from ->be.
1668 *
1669 * Maybe we are in resume condiion. In this case I choose the
1670 * "struct proxy" which contains the rule list matching the resume
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05001671 * pointer. If none of these "struct proxy" match, I initialise
Christopher Faulete0768eb2018-10-03 16:38:02 +02001672 * the process with the first one.
1673 *
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05001674 * In fact, I check only correspondence between the current list
Christopher Faulete0768eb2018-10-03 16:38:02 +02001675 * pointer and the ->fe rule list. If it doesn't match, I initialize
1676 * the loop with the ->be.
1677 */
Christopher Fauletd4150ad2021-10-13 15:35:55 +02001678 if (s->current_rule_list == &sess->fe->http_res_rules ||
1679 (sess->fe->defpx && s->current_rule_list == &sess->fe->defpx->http_res_rules))
Christopher Faulete0768eb2018-10-03 16:38:02 +02001680 cur_proxy = sess->fe;
1681 else
1682 cur_proxy = s->be;
Christopher Fauletd4150ad2021-10-13 15:35:55 +02001683
Christopher Faulete0768eb2018-10-03 16:38:02 +02001684 while (1) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001685 /* evaluate http-response rules */
Christopher Faulet46f46df2021-11-09 16:33:25 +01001686 if (ret == HTTP_RULE_RES_CONT || ret == HTTP_RULE_RES_STOP) {
Christopher Fauletd4150ad2021-10-13 15:35:55 +02001687 struct list *def_rules, *rules;
1688
1689 def_rules = ((cur_proxy->defpx && (cur_proxy == s->be || cur_proxy->defpx != s->be->defpx)) ? &cur_proxy->defpx->http_res_rules : NULL);
1690 rules = &cur_proxy->http_res_rules;
1691
1692 ret = http_res_get_intercept_rule(cur_proxy, def_rules, rules, s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001693
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001694 switch (ret) {
1695 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
1696 goto return_prx_yield;
1697
1698 case HTTP_RULE_RES_CONT:
1699 case HTTP_RULE_RES_STOP: /* nothing to do */
1700 break;
1701
1702 case HTTP_RULE_RES_DENY: /* deny or tarpit */
1703 goto deny;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001704
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001705 case HTTP_RULE_RES_ABRT: /* abort request, response already sent */
1706 goto return_prx_cond;
1707
1708 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
Christopher Fauletb8a53712019-12-16 11:29:38 +01001709 goto done;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001710
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001711 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
1712 goto return_bad_res;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001713
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001714 case HTTP_RULE_RES_ERROR: /* failed with a bad request */
1715 goto return_int_err;
1716 }
1717
1718 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001719
Christopher Faulete0768eb2018-10-03 16:38:02 +02001720 /* check whether we're already working on the frontend */
1721 if (cur_proxy == sess->fe)
1722 break;
1723 cur_proxy = sess->fe;
1724 }
1725
Christopher Faulete0768eb2018-10-03 16:38:02 +02001726 /* OK that's all we can do for 1xx responses */
1727 if (unlikely(txn->status < 200 && txn->status != 101))
Christopher Fauletf2824e62018-10-01 12:12:37 +02001728 goto end;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001729
1730 /*
1731 * Now check for a server cookie.
1732 */
1733 if (s->be->cookie_name || sess->fe->capture_name || (s->be->options & PR_O_CHK_CACHE))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001734 http_manage_server_side_cookies(s, rep);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001735
1736 /*
1737 * Check for cache-control or pragma headers if required.
1738 */
1739 if ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001740 http_check_response_for_cacheability(s, rep);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001741
1742 /*
1743 * Add server cookie in the response if needed
1744 */
1745 if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) &&
1746 !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) &&
1747 (!(s->flags & SF_DIRECT) ||
1748 ((s->be->cookie_maxidle || txn->cookie_last_date) &&
1749 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
1750 (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
1751 (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
1752 (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
1753 !(s->flags & SF_IGNORE_PRST)) {
1754 /* the server is known, it's not the one the client requested, or the
1755 * cookie's last seen date needs to be refreshed. We have to
1756 * insert a set-cookie here, except if we want to insert only on POST
1757 * requests and this one isn't. Note that servers which don't have cookies
1758 * (eg: some backup servers) will return a full cookie removal request.
1759 */
Willy Tarreau88bc8002021-12-06 07:01:02 +00001760 if (!__objt_server(s->target)->cookie) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001761 chunk_printf(&trash,
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001762 "%s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
Christopher Faulete0768eb2018-10-03 16:38:02 +02001763 s->be->cookie_name);
1764 }
1765 else {
Willy Tarreau88bc8002021-12-06 07:01:02 +00001766 chunk_printf(&trash, "%s=%s", s->be->cookie_name, __objt_server(s->target)->cookie);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001767
1768 if (s->be->cookie_maxidle || s->be->cookie_maxlife) {
1769 /* emit last_date, which is mandatory */
1770 trash.area[trash.data++] = COOKIE_DELIM_DATE;
1771 s30tob64((date.tv_sec+3) >> 2,
1772 trash.area + trash.data);
1773 trash.data += 5;
1774
1775 if (s->be->cookie_maxlife) {
1776 /* emit first_date, which is either the original one or
1777 * the current date.
1778 */
1779 trash.area[trash.data++] = COOKIE_DELIM_DATE;
1780 s30tob64(txn->cookie_first_date ?
1781 txn->cookie_first_date >> 2 :
1782 (date.tv_sec+3) >> 2,
1783 trash.area + trash.data);
1784 trash.data += 5;
1785 }
1786 }
1787 chunk_appendf(&trash, "; path=/");
1788 }
1789
1790 if (s->be->cookie_domain)
1791 chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain);
1792
1793 if (s->be->ck_opts & PR_CK_HTTPONLY)
1794 chunk_appendf(&trash, "; HttpOnly");
1795
1796 if (s->be->ck_opts & PR_CK_SECURE)
1797 chunk_appendf(&trash, "; Secure");
1798
Christopher Faulet2f533902020-01-21 11:06:48 +01001799 if (s->be->cookie_attrs)
1800 chunk_appendf(&trash, "; %s", s->be->cookie_attrs);
1801
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001802 if (unlikely(!http_add_header(htx, ist("Set-Cookie"), ist2(trash.area, trash.data))))
Christopher Fauletd649b572022-06-01 17:42:35 +02001803 goto return_fail_rewrite;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001804
1805 txn->flags &= ~TX_SCK_MASK;
1806 if (__objt_server(s->target)->cookie && (s->flags & SF_DIRECT))
1807 /* the server did not change, only the date was updated */
1808 txn->flags |= TX_SCK_UPDATED;
1809 else
1810 txn->flags |= TX_SCK_INSERTED;
1811
1812 /* Here, we will tell an eventual cache on the client side that we don't
1813 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
1814 * Some caches understand the correct form: 'no-cache="set-cookie"', but
1815 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
1816 */
1817 if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
1818
1819 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
1820
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001821 if (unlikely(!http_add_header(htx, ist("Cache-control"), ist("private"))))
Christopher Fauletd649b572022-06-01 17:42:35 +02001822 goto return_fail_rewrite;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001823 }
1824 }
1825
1826 /*
1827 * Check if result will be cacheable with a cookie.
1828 * We'll block the response if security checks have caught
1829 * nasty things such as a cacheable cookie.
1830 */
1831 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
1832 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
1833 (s->be->options & PR_O_CHK_CACHE)) {
1834 /* we're in presence of a cacheable response containing
1835 * a set-cookie header. We'll block it as requested by
1836 * the 'checkcache' option, and send an alert.
1837 */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001838 ha_alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreau88bc8002021-12-06 07:01:02 +00001839 s->be->id, objt_server(s->target) ? __objt_server(s->target)->id : "<dispatch>");
Christopher Faulete0768eb2018-10-03 16:38:02 +02001840 send_log(s->be, LOG_ALERT,
1841 "Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreau88bc8002021-12-06 07:01:02 +00001842 s->be->id, objt_server(s->target) ? __objt_server(s->target)->id : "<dispatch>");
Christopher Fauletb8a53712019-12-16 11:29:38 +01001843 goto deny;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001844 }
1845
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001846 end:
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01001847 /*
1848 * Evaluate after-response rules before forwarding the response. rules
1849 * from the backend are evaluated first, then one from the frontend if
1850 * it differs.
1851 */
1852 if (!http_eval_after_res_rules(s))
1853 goto return_int_err;
1854
Christopher Fauletc2ac5e42021-03-08 18:20:09 +01001855 /* Filter the response headers if there are filters attached to the
1856 * stream.
1857 */
1858 if (HAS_FILTERS(s))
1859 rep->analysers |= AN_RES_FLT_HTTP_HDRS;
1860
Christopher Faulete0768eb2018-10-03 16:38:02 +02001861 /* Always enter in the body analyzer */
1862 rep->analysers &= ~AN_RES_FLT_XFER_DATA;
1863 rep->analysers |= AN_RES_HTTP_XFER_BODY;
1864
1865 /* if the user wants to log as soon as possible, without counting
1866 * bytes from the server, then this is the right moment. We have
1867 * to temporarily assign bytes_out to log what we currently have.
1868 */
1869 if (!LIST_ISEMPTY(&sess->fe->logformat) && !(s->logs.logwait & LW_BYTES)) {
1870 s->logs.t_close = s->logs.t_data; /* to get a valid end date */
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001871 s->logs.bytes_out = htx->data;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001872 s->do_log(s);
1873 s->logs.bytes_out = 0;
1874 }
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001875
Christopher Fauletb8a53712019-12-16 11:29:38 +01001876 done:
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01001877 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletb8a53712019-12-16 11:29:38 +01001878 rep->analysers &= ~an_bit;
1879 rep->analyse_exp = TICK_ETERNITY;
Christopher Fauletd4150ad2021-10-13 15:35:55 +02001880 s->current_rule = s->current_rule_list = NULL;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001881 return 1;
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001882
Christopher Fauletb8a53712019-12-16 11:29:38 +01001883 deny:
Willy Tarreau4781b152021-04-06 13:53:36 +02001884 _HA_ATOMIC_INC(&sess->fe->fe_counters.denied_resp);
1885 _HA_ATOMIC_INC(&s->be->be_counters.denied_resp);
William Lallemand36119de2021-03-08 15:26:48 +01001886 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001887 _HA_ATOMIC_INC(&sess->listener->counters->denied_resp);
Christopher Fauleta08546b2019-12-16 16:07:34 +01001888 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001889 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.denied_resp);
Christopher Fauletb8a53712019-12-16 11:29:38 +01001890 goto return_prx_err;
1891
Christopher Fauletd649b572022-06-01 17:42:35 +02001892 return_fail_rewrite:
1893 if (!(s->flags & SF_ERR_MASK))
1894 s->flags |= SF_ERR_PRXCOND;
1895 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_rewrites);
1896 _HA_ATOMIC_INC(&s->be->be_counters.failed_rewrites);
1897 if (sess->listener && sess->listener->counters)
1898 _HA_ATOMIC_INC(&sess->listener->counters->failed_rewrites);
1899 if (objt_server(s->target))
1900 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_rewrites);
1901 /* fall through */
1902
Christopher Fauletb8a53712019-12-16 11:29:38 +01001903 return_int_err:
1904 txn->status = 500;
1905 if (!(s->flags & SF_ERR_MASK))
1906 s->flags |= SF_ERR_INTERNAL;
Willy Tarreau4781b152021-04-06 13:53:36 +02001907 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
1908 _HA_ATOMIC_INC(&s->be->be_counters.internal_errors);
Dragan Dosen9a006f92021-09-21 13:02:09 +02001909 if (sess->listener && sess->listener->counters)
1910 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001911 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001912 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.internal_errors);
Christopher Fauletb8a53712019-12-16 11:29:38 +01001913 goto return_prx_err;
1914
1915 return_bad_res:
1916 txn->status = 502;
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001917 stream_inc_http_fail_ctr(s);
Willy Tarreau4781b152021-04-06 13:53:36 +02001918 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Fauleta20a6532020-02-05 10:16:41 +01001919 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001920 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Christopher Fauleta20a6532020-02-05 10:16:41 +01001921 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_RSP);
1922 }
Christopher Fauletb8a53712019-12-16 11:29:38 +01001923 /* fall through */
1924
1925 return_prx_err:
1926 http_reply_and_close(s, txn->status, http_error_message(s));
1927 /* fall through */
1928
1929 return_prx_cond:
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001930 s->logs.t_data = -1; /* was not a valid response */
Willy Tarreaucb041662022-05-17 19:44:42 +02001931 s->scb->flags |= SC_FL_NOLINGER;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001932
Christopher Faulet4a66c942023-01-13 09:43:21 +01001933 http_set_term_flags(s);
Christopher Fauletb8a53712019-12-16 11:29:38 +01001934
Christopher Faulete58c0002020-03-02 16:21:01 +01001935 rep->analysers &= AN_RES_FLT_END;
1936 s->req.analysers &= AN_REQ_FLT_END;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001937 rep->analyse_exp = TICK_ETERNITY;
Christopher Fauletd4150ad2021-10-13 15:35:55 +02001938 s->current_rule = s->current_rule_list = NULL;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001939 DBG_TRACE_DEVEL("leaving on error",
1940 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001941 return 0;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001942
1943 return_prx_yield:
1944 channel_dont_close(rep);
1945 DBG_TRACE_DEVEL("waiting for more data",
1946 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
1947 return 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001948}
1949
1950/* This function is an analyser which forwards response body (including chunk
1951 * sizes if any). It is called as soon as we must forward, even if we forward
1952 * zero byte. The only situation where it must not be called is when we're in
1953 * tunnel mode and we want to forward till the close. It's used both to forward
1954 * remaining data and to resync after end of body. It expects the msg_state to
1955 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
1956 * read more data, or 1 once we can go on with next request or end the stream.
1957 *
1958 * It is capable of compressing response data both in content-length mode and
1959 * in chunked mode. The state machines follows different flows depending on
1960 * whether content-length and chunked modes are used, since there are no
1961 * trailers in content-length :
1962 *
1963 * chk-mode cl-mode
1964 * ,----- BODY -----.
1965 * / \
1966 * V size > 0 V chk-mode
1967 * .--> SIZE -------------> DATA -------------> CRLF
1968 * | | size == 0 | last byte |
1969 * | v final crlf v inspected |
1970 * | TRAILERS -----------> DONE |
1971 * | |
1972 * `----------------------------------------------'
1973 *
1974 * Compression only happens in the DATA state, and must be flushed in final
1975 * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding
1976 * is performed at once on final states for all bytes parsed, or when leaving
1977 * on missing data.
1978 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001979int http_response_forward_body(struct stream *s, struct channel *res, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001980{
1981 struct session *sess = s->sess;
1982 struct http_txn *txn = s->txn;
1983 struct http_msg *msg = &s->txn->rsp;
Christopher Faulet9768c262018-10-22 09:34:31 +02001984 struct htx *htx;
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001985 int ret;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001986
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001987 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001988
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001989 htx = htxbuf(&res->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001990
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001991 if (htx->flags & HTX_FL_PARSING_ERROR)
1992 goto return_bad_res;
1993 if (htx->flags & HTX_FL_PROCESSING_ERROR)
1994 goto return_int_err;
1995
Christopher Faulet9768c262018-10-22 09:34:31 +02001996 if (msg->msg_state == HTTP_MSG_BODY)
1997 msg->msg_state = HTTP_MSG_DATA;
1998
Christopher Faulete0768eb2018-10-03 16:38:02 +02001999 /* in most states, we should abort in case of early close */
2000 channel_auto_close(res);
2001
Christopher Faulete0768eb2018-10-03 16:38:02 +02002002 if (res->to_forward) {
Christopher Faulet66af0b22019-03-22 14:54:52 +01002003 if (res->to_forward == CHN_INFINITE_FORWARD) {
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002004 if (res->flags & CF_EOI)
2005 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet66af0b22019-03-22 14:54:52 +01002006 }
2007 else {
2008 /* We can't process the buffer's contents yet */
2009 res->flags |= CF_WAKE_WRITE;
2010 goto missing_data_or_waiting;
2011 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02002012 }
2013
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002014 if (msg->msg_state >= HTTP_MSG_ENDING)
2015 goto ending;
2016
Christopher Fauletc75668e2020-12-07 18:10:32 +01002017 if ((txn->meth == HTTP_METH_CONNECT && txn->status >= 200 && txn->status < 300) || txn->status == 101 ||
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002018 (!(msg->flags & HTTP_MSGF_XFER_LEN) && !HAS_RSP_DATA_FILTERS(s))) {
2019 msg->msg_state = HTTP_MSG_ENDING;
2020 goto ending;
2021 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002022
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002023 /* Forward input data. We get it by removing all outgoing data not
2024 * forwarded yet from HTX data size. If there are some data filters, we
2025 * let them decide the amount of data to forward.
Christopher Faulet9768c262018-10-22 09:34:31 +02002026 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002027 if (HAS_RSP_DATA_FILTERS(s)) {
2028 ret = flt_http_payload(s, msg, htx->data);
2029 if (ret < 0)
2030 goto return_bad_res;
Christopher Faulet421e7692019-06-13 11:16:45 +02002031 c_adv(res, ret);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002032 }
2033 else {
Christopher Faulet421e7692019-06-13 11:16:45 +02002034 c_adv(res, htx->data - co_data(res));
Christopher Faulet66af0b22019-03-22 14:54:52 +01002035 if (msg->flags & HTTP_MSGF_XFER_LEN)
2036 channel_htx_forward_forever(res, htx);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002037 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002038
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002039 if (htx->data != co_data(res))
2040 goto missing_data_or_waiting;
2041
2042 if (!(msg->flags & HTTP_MSGF_XFER_LEN) && res->flags & CF_SHUTR) {
2043 msg->msg_state = HTTP_MSG_ENDING;
2044 goto ending;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002045 }
2046
Christopher Faulet9768c262018-10-22 09:34:31 +02002047 /* Check if the end-of-message is reached and if so, switch the message
Christopher Fauletd20fdb02019-06-13 16:43:22 +02002048 * in HTTP_MSG_ENDING state. Then if all data was marked to be
2049 * forwarded, set the state to HTTP_MSG_DONE.
Christopher Faulet9768c262018-10-22 09:34:31 +02002050 */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002051 if (!(htx->flags & HTX_FL_EOM))
Christopher Faulet9768c262018-10-22 09:34:31 +02002052 goto missing_data_or_waiting;
2053
Christopher Fauletd20fdb02019-06-13 16:43:22 +02002054 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet9768c262018-10-22 09:34:31 +02002055
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002056 ending:
Christopher Faulet2151cdd2020-07-22 16:34:59 +02002057 res->flags &= ~CF_EXPECT_MORE; /* no more data are expected */
2058
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002059 /* other states, ENDING...TUNNEL */
2060 if (msg->msg_state >= HTTP_MSG_DONE)
2061 goto done;
Christopher Faulet9768c262018-10-22 09:34:31 +02002062
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002063 if (HAS_RSP_DATA_FILTERS(s)) {
2064 ret = flt_http_end(s, msg);
2065 if (ret <= 0) {
2066 if (!ret)
2067 goto missing_data_or_waiting;
2068 goto return_bad_res;
2069 }
2070 }
2071
Christopher Fauletc75668e2020-12-07 18:10:32 +01002072 if ((txn->meth == HTTP_METH_CONNECT && txn->status >= 200 && txn->status < 300) || txn->status == 101 ||
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002073 !(msg->flags & HTTP_MSGF_XFER_LEN)) {
2074 msg->msg_state = HTTP_MSG_TUNNEL;
2075 goto ending;
2076 }
2077 else {
2078 msg->msg_state = HTTP_MSG_DONE;
2079 res->to_forward = 0;
2080 }
2081
2082 done:
2083
2084 channel_dont_close(res);
2085
Christopher Fauletf2b02cf2023-01-13 11:02:28 +01002086 if ((res->flags & CF_SHUTW) && co_data(res)) {
2087 /* response errors are most likely due to the client aborting
2088 * the transfer. */
2089 goto return_cli_abort;
2090 }
2091
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002092 http_end_response(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002093 if (!(res->analysers & an_bit)) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002094 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002095 return 1;
2096 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002097 DBG_TRACE_DEVEL("waiting for the end of the HTTP txn",
2098 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002099 return 0;
2100
2101 missing_data_or_waiting:
2102 if (res->flags & CF_SHUTW)
Christopher Faulet93e02d82019-03-08 14:18:50 +01002103 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002104
2105 /* stop waiting for data if the input is closed before the end. If the
2106 * client side was already closed, it means that the client has aborted,
2107 * so we don't want to count this as a server abort. Otherwise it's a
2108 * server abort.
2109 */
Christopher Fauletd20fdb02019-06-13 16:43:22 +02002110 if (msg->msg_state < HTTP_MSG_ENDING && res->flags & CF_SHUTR) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02002111 if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))
Christopher Faulet93e02d82019-03-08 14:18:50 +01002112 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002113 /* If we have some pending data, we continue the processing */
Christopher Faulet93e02d82019-03-08 14:18:50 +01002114 if (htx_is_empty(htx))
2115 goto return_srv_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002116 }
2117
Christopher Faulete0768eb2018-10-03 16:38:02 +02002118 /* When TE: chunked is used, we need to get there again to parse
2119 * remaining chunks even if the server has closed, so we don't want to
Christopher Faulet9768c262018-10-22 09:34:31 +02002120 * set CF_DONTCLOSE. Similarly when there is a content-leng or if there
2121 * are filters registered on the stream, we don't want to forward a
2122 * close
Christopher Faulete0768eb2018-10-03 16:38:02 +02002123 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002124 if ((msg->flags & HTTP_MSGF_XFER_LEN) || HAS_RSP_DATA_FILTERS(s))
Christopher Faulete0768eb2018-10-03 16:38:02 +02002125 channel_dont_close(res);
2126
2127 /* We know that more data are expected, but we couldn't send more that
2128 * what we did. So we always set the CF_EXPECT_MORE flag so that the
2129 * system knows it must not set a PUSH on this first part. Interactive
2130 * modes are already handled by the stream sock layer. We must not do
2131 * this in content-length mode because it could present the MSG_MORE
2132 * flag with the last block of forwarded data, which would cause an
2133 * additional delay to be observed by the receiver.
2134 */
Christopher Faulet2151cdd2020-07-22 16:34:59 +02002135 if (HAS_RSP_DATA_FILTERS(s))
Christopher Faulete0768eb2018-10-03 16:38:02 +02002136 res->flags |= CF_EXPECT_MORE;
2137
2138 /* the stream handler will take care of timeouts and errors */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002139 DBG_TRACE_DEVEL("waiting for more data to forward",
2140 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002141 return 0;
2142
Christopher Faulet93e02d82019-03-08 14:18:50 +01002143 return_srv_abort:
Willy Tarreau4781b152021-04-06 13:53:36 +02002144 _HA_ATOMIC_INC(&sess->fe->fe_counters.srv_aborts);
2145 _HA_ATOMIC_INC(&s->be->be_counters.srv_aborts);
William Lallemand36119de2021-03-08 15:26:48 +01002146 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02002147 _HA_ATOMIC_INC(&sess->listener->counters->srv_aborts);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002148 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02002149 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.srv_aborts);
Willy Tarreau826f3ab2021-02-10 12:07:15 +01002150 stream_inc_http_fail_ctr(s);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002151 if (!(s->flags & SF_ERR_MASK))
Christopher Fauletf2b02cf2023-01-13 11:02:28 +01002152 s->flags |= ((res->flags & CF_READ_TIMEOUT) ? SF_ERR_SRVTO : SF_ERR_SRVCL);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002153 goto return_error;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002154
Christopher Faulet93e02d82019-03-08 14:18:50 +01002155 return_cli_abort:
Willy Tarreau4781b152021-04-06 13:53:36 +02002156 _HA_ATOMIC_INC(&sess->fe->fe_counters.cli_aborts);
2157 _HA_ATOMIC_INC(&s->be->be_counters.cli_aborts);
William Lallemand36119de2021-03-08 15:26:48 +01002158 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02002159 _HA_ATOMIC_INC(&sess->listener->counters->cli_aborts);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002160 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02002161 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.cli_aborts);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002162 if (!(s->flags & SF_ERR_MASK))
Christopher Fauletf2b02cf2023-01-13 11:02:28 +01002163 s->flags |= ((res->flags & CF_WRITE_TIMEOUT) ? SF_ERR_CLITO : SF_ERR_CLICL);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002164 goto return_error;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002165
Christopher Fauletb9a92f32019-09-09 10:15:21 +02002166 return_int_err:
Willy Tarreau4781b152021-04-06 13:53:36 +02002167 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
2168 _HA_ATOMIC_INC(&s->be->be_counters.internal_errors);
William Lallemand36119de2021-03-08 15:26:48 +01002169 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02002170 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002171 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02002172 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.internal_errors);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02002173 if (!(s->flags & SF_ERR_MASK))
2174 s->flags |= SF_ERR_INTERNAL;
2175 goto return_error;
2176
Christopher Faulet93e02d82019-03-08 14:18:50 +01002177 return_bad_res:
Willy Tarreau4781b152021-04-06 13:53:36 +02002178 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002179 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02002180 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002181 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_RSP);
2182 }
Willy Tarreau826f3ab2021-02-10 12:07:15 +01002183 stream_inc_http_fail_ctr(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002184 if (!(s->flags & SF_ERR_MASK))
Christopher Faulet93e02d82019-03-08 14:18:50 +01002185 s->flags |= SF_ERR_SRVCL;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002186 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +02002187
Christopher Faulet93e02d82019-03-08 14:18:50 +01002188 return_error:
Christopher Faulete0768eb2018-10-03 16:38:02 +02002189 /* don't send any error message as we're in the body */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002190 http_reply_and_close(s, txn->status, NULL);
Christopher Fauletf2b02cf2023-01-13 11:02:28 +01002191 http_set_term_flags(s);
2192 stream_inc_http_fail_ctr(s);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002193 DBG_TRACE_DEVEL("leaving on error",
2194 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002195 return 0;
2196}
2197
Christopher Fauletf2824e62018-10-01 12:12:37 +02002198/* Perform an HTTP redirect based on the information in <rule>. The function
Willy Tarreaubc1223b2021-09-02 16:54:33 +02002199 * returns zero in case of an irrecoverable error such as too large a request
2200 * to build a valid response, 1 in case of successful redirect (hence the rule
2201 * is final), or 2 if the rule has to be silently skipped.
Christopher Fauletf2824e62018-10-01 12:12:37 +02002202 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002203int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn)
Christopher Fauletf2824e62018-10-01 12:12:37 +02002204{
Christopher Faulet99daf282018-11-28 22:58:13 +01002205 struct channel *req = &s->req;
2206 struct channel *res = &s->res;
2207 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01002208 struct htx_sl *sl;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002209 struct buffer *chunk;
Christopher Faulet99daf282018-11-28 22:58:13 +01002210 struct ist status, reason, location;
2211 unsigned int flags;
Christopher Fauleteab17572022-04-26 20:34:38 +02002212 int ret = 1, close = 0; /* Try to keep the connection alive byt default */
Christopher Fauletf2824e62018-10-01 12:12:37 +02002213
2214 chunk = alloc_trash_chunk();
Christopher Fauletb8a53712019-12-16 11:29:38 +01002215 if (!chunk) {
2216 if (!(s->flags & SF_ERR_MASK))
2217 s->flags |= SF_ERR_RESOURCE;
Christopher Faulet99daf282018-11-28 22:58:13 +01002218 goto fail;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002219 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002220
Christopher Faulet99daf282018-11-28 22:58:13 +01002221 /*
2222 * Create the location
2223 */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002224 htx = htxbuf(&req->buf);
Christopher Fauletf2824e62018-10-01 12:12:37 +02002225 switch(rule->type) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002226 case REDIRECT_TYPE_SCHEME: {
2227 struct http_hdr_ctx ctx;
2228 struct ist path, host;
Amaury Denoyellec453f952021-07-06 11:40:12 +02002229 struct http_uri_parser parser;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002230
Christopher Faulet99daf282018-11-28 22:58:13 +01002231 host = ist("");
2232 ctx.blk = NULL;
2233 if (http_find_header(htx, ist("Host"), &ctx, 0))
2234 host = ctx.value;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002235
Christopher Faulet297fbb42019-05-13 14:41:27 +02002236 sl = http_get_stline(htx);
Amaury Denoyellec453f952021-07-06 11:40:12 +02002237 parser = http_uri_parser_init(htx_sl_req_uri(sl));
2238 path = http_parse_path(&parser);
Christopher Faulet99daf282018-11-28 22:58:13 +01002239 /* build message using path */
Tim Duesterhused526372020-03-05 17:56:33 +01002240 if (isttest(path)) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002241 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2242 int qs = 0;
2243 while (qs < path.len) {
2244 if (*(path.ptr + qs) == '?') {
2245 path.len = qs;
2246 break;
2247 }
2248 qs++;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002249 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002250 }
2251 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002252 else
2253 path = ist("/");
Christopher Fauletf2824e62018-10-01 12:12:37 +02002254
Christopher Faulet99daf282018-11-28 22:58:13 +01002255 if (rule->rdr_str) { /* this is an old "redirect" rule */
2256 /* add scheme */
2257 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2258 goto fail;
2259 }
2260 else {
2261 /* add scheme with executing log format */
2262 chunk->data += build_logline(s, chunk->area + chunk->data,
2263 chunk->size - chunk->data,
2264 &rule->rdr_fmt);
2265 }
2266 /* add "://" + host + path */
2267 if (!chunk_memcat(chunk, "://", 3) ||
2268 !chunk_memcat(chunk, host.ptr, host.len) ||
2269 !chunk_memcat(chunk, path.ptr, path.len))
2270 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002271
Christopher Faulet99daf282018-11-28 22:58:13 +01002272 /* append a slash at the end of the location if needed and missing */
2273 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
2274 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
2275 if (chunk->data + 1 >= chunk->size)
2276 goto fail;
2277 chunk->area[chunk->data++] = '/';
2278 }
2279 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002280 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002281
Christopher Faulet99daf282018-11-28 22:58:13 +01002282 case REDIRECT_TYPE_PREFIX: {
2283 struct ist path;
Amaury Denoyellec453f952021-07-06 11:40:12 +02002284 struct http_uri_parser parser;
Christopher Faulet99daf282018-11-28 22:58:13 +01002285
Christopher Faulet297fbb42019-05-13 14:41:27 +02002286 sl = http_get_stline(htx);
Amaury Denoyellec453f952021-07-06 11:40:12 +02002287 parser = http_uri_parser_init(htx_sl_req_uri(sl));
2288 path = http_parse_path(&parser);
Christopher Faulet99daf282018-11-28 22:58:13 +01002289 /* build message using path */
Tim Duesterhused526372020-03-05 17:56:33 +01002290 if (isttest(path)) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002291 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2292 int qs = 0;
2293 while (qs < path.len) {
2294 if (*(path.ptr + qs) == '?') {
2295 path.len = qs;
2296 break;
2297 }
2298 qs++;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002299 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002300 }
2301 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002302 else
2303 path = ist("/");
Christopher Fauletf2824e62018-10-01 12:12:37 +02002304
Christopher Faulet99daf282018-11-28 22:58:13 +01002305 if (rule->rdr_str) { /* this is an old "redirect" rule */
2306 /* add prefix. Note that if prefix == "/", we don't want to
2307 * add anything, otherwise it makes it hard for the user to
2308 * configure a self-redirection.
2309 */
2310 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
2311 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2312 goto fail;
2313 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002314 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002315 else {
2316 /* add prefix with executing log format */
2317 chunk->data += build_logline(s, chunk->area + chunk->data,
2318 chunk->size - chunk->data,
2319 &rule->rdr_fmt);
2320 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002321
Christopher Faulet99daf282018-11-28 22:58:13 +01002322 /* add path */
2323 if (!chunk_memcat(chunk, path.ptr, path.len))
2324 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002325
Christopher Faulet99daf282018-11-28 22:58:13 +01002326 /* append a slash at the end of the location if needed and missing */
2327 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
2328 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
2329 if (chunk->data + 1 >= chunk->size)
2330 goto fail;
2331 chunk->area[chunk->data++] = '/';
2332 }
2333 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002334 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002335 case REDIRECT_TYPE_LOCATION:
2336 default:
2337 if (rule->rdr_str) { /* this is an old "redirect" rule */
2338 /* add location */
2339 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2340 goto fail;
2341 }
2342 else {
2343 /* add location with executing log format */
Willy Tarreaubc1223b2021-09-02 16:54:33 +02002344 int len = build_logline(s, chunk->area + chunk->data,
2345 chunk->size - chunk->data,
2346 &rule->rdr_fmt);
Christopher Fauleteab17572022-04-26 20:34:38 +02002347 if (!len && rule->flags & REDIRECT_FLAG_IGNORE_EMPTY) {
2348 ret = 2;
2349 goto out;
2350 }
Willy Tarreaubc1223b2021-09-02 16:54:33 +02002351
2352 chunk->data += len;
Christopher Faulet99daf282018-11-28 22:58:13 +01002353 }
2354 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002355 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002356 location = ist2(chunk->area, chunk->data);
2357
2358 /*
2359 * Create the 30x response
2360 */
2361 switch (rule->code) {
2362 case 308:
2363 status = ist("308");
2364 reason = ist("Permanent Redirect");
2365 break;
2366 case 307:
2367 status = ist("307");
2368 reason = ist("Temporary Redirect");
2369 break;
2370 case 303:
2371 status = ist("303");
2372 reason = ist("See Other");
2373 break;
2374 case 301:
2375 status = ist("301");
2376 reason = ist("Moved Permanently");
2377 break;
2378 case 302:
2379 default:
2380 status = ist("302");
2381 reason = ist("Found");
2382 break;
2383 }
2384
Christopher Faulet08e66462019-05-23 16:44:59 +02002385 if (!(txn->req.flags & HTTP_MSGF_BODYLESS) && txn->req.msg_state != HTTP_MSG_DONE)
2386 close = 1;
2387
Christopher Faulet99daf282018-11-28 22:58:13 +01002388 htx = htx_from_buf(&res->buf);
Kevin Zhu96b36392020-01-07 09:42:55 +01002389 /* Trim any possible response */
2390 channel_htx_truncate(&s->res, htx);
Christopher Faulet99daf282018-11-28 22:58:13 +01002391 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
2392 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), status, reason);
2393 if (!sl)
2394 goto fail;
2395 sl->info.res.status = rule->code;
2396 s->txn->status = rule->code;
2397
Christopher Faulet08e66462019-05-23 16:44:59 +02002398 if (close && !htx_add_header(htx, ist("Connection"), ist("close")))
2399 goto fail;
2400
2401 if (!htx_add_header(htx, ist("Content-length"), ist("0")) ||
Christopher Faulet99daf282018-11-28 22:58:13 +01002402 !htx_add_header(htx, ist("Location"), location))
2403 goto fail;
2404
2405 if (rule->code == 302 || rule->code == 303 || rule->code == 307) {
2406 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")))
2407 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002408 }
2409
2410 if (rule->cookie_len) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002411 if (!htx_add_header(htx, ist("Set-Cookie"), ist2(rule->cookie_str, rule->cookie_len)))
2412 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002413 }
2414
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002415 if (!htx_add_endof(htx, HTX_BLK_EOH))
Christopher Faulet99daf282018-11-28 22:58:13 +01002416 goto fail;
2417
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002418 htx->flags |= HTX_FL_EOM;
Kevin Zhu96b36392020-01-07 09:42:55 +01002419 htx_to_buf(htx, &res->buf);
Christopher Fauleta72a7e42020-01-28 09:28:11 +01002420 if (!http_forward_proxy_resp(s, 1))
2421 goto fail;
Christopher Faulet99daf282018-11-28 22:58:13 +01002422
Christopher Faulet60b33a52020-01-28 09:18:10 +01002423 if (rule->flags & REDIRECT_FLAG_FROM_REQ) {
2424 /* let's log the request time */
2425 s->logs.tv_request = now;
Christopher Fauletd3475882021-10-04 14:16:46 +02002426 req->analysers &= AN_REQ_FLT_END;
Christopher Faulet99daf282018-11-28 22:58:13 +01002427
Christopher Faulet60b33a52020-01-28 09:18:10 +01002428 if (s->sess->fe == s->be) /* report it if the request was intercepted by the frontend */
Willy Tarreau4781b152021-04-06 13:53:36 +02002429 _HA_ATOMIC_INC(&s->sess->fe->fe_counters.intercepted_req);
Christopher Faulet60b33a52020-01-28 09:18:10 +01002430 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002431
2432 if (!(s->flags & SF_ERR_MASK))
2433 s->flags |= SF_ERR_LOCAL;
Christopher Faulet4a66c942023-01-13 09:43:21 +01002434 http_set_term_flags(s);
Christopher Fauletf2824e62018-10-01 12:12:37 +02002435
Christopher Fauleteab17572022-04-26 20:34:38 +02002436 out:
Christopher Faulet99daf282018-11-28 22:58:13 +01002437 free_trash_chunk(chunk);
Christopher Fauleteab17572022-04-26 20:34:38 +02002438 return ret;
Christopher Faulet99daf282018-11-28 22:58:13 +01002439
2440 fail:
2441 /* If an error occurred, remove the incomplete HTTP response from the
2442 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01002443 channel_htx_truncate(res, htxbuf(&res->buf));
Christopher Fauleteab17572022-04-26 20:34:38 +02002444 ret = 0;
2445 goto out;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002446}
2447
Christopher Faulet18c13d32022-05-16 11:43:10 +02002448/* This function filters the request header names to only allow [0-9a-zA-Z-]
2449 * characters. Depending on the proxy configuration, headers with a name not
2450 * matching this charset are removed or the request is rejected with a
2451 * 403-Forbidden response if such name are found. It returns HTTP_RULE_RES_CONT
2452 * to continue the request processing or HTTP_RULE_RES_DENY if the request is
2453 * rejected.
2454 */
2455static enum rule_result http_req_restrict_header_names(struct stream *s, struct htx *htx, struct proxy *px)
2456{
2457 struct htx_blk *blk;
2458 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
2459
2460 blk = htx_get_first_blk(htx);
2461 while (blk) {
2462 enum htx_blk_type type = htx_get_blk_type(blk);
2463
2464 if (type == HTX_BLK_HDR) {
2465 struct ist n = htx_get_blk_name(htx, blk);
Mateusz Malek4b85a962022-08-17 14:22:09 +02002466 int i, end = istlen(n);
Christopher Faulet18c13d32022-05-16 11:43:10 +02002467
Mateusz Malek4b85a962022-08-17 14:22:09 +02002468 for (i = 0; i < end; i++) {
Christopher Faulet18c13d32022-05-16 11:43:10 +02002469 if (!isalnum((unsigned char)n.ptr[i]) && n.ptr[i] != '-') {
Mateusz Malek4b85a962022-08-17 14:22:09 +02002470 break;
Christopher Faulet18c13d32022-05-16 11:43:10 +02002471 }
2472 }
Mateusz Malek4b85a962022-08-17 14:22:09 +02002473
2474 if (i < end) {
2475 /* Disallowed character found - block the request or remove the header */
2476 if (px->options2 & PR_O2_RSTRICT_REQ_HDR_NAMES_BLK)
2477 goto block;
2478 blk = htx_remove_blk(htx, blk);
2479 continue;
2480 }
Christopher Faulet18c13d32022-05-16 11:43:10 +02002481 }
2482 if (type == HTX_BLK_EOH)
2483 break;
2484
2485 blk = htx_get_next_blk(htx, blk);
2486 }
2487 out:
2488 return rule_ret;
2489 block:
2490 /* Block the request returning a 403-Forbidden response */
2491 s->txn->status = 403;
2492 rule_ret = HTTP_RULE_RES_DENY;
2493 goto out;
2494}
2495
Christopher Faulet92d34fe2019-12-17 09:20:34 +01002496/* Replace all headers matching the name <name>. The header value is replaced if
2497 * it matches the regex <re>. <str> is used for the replacement. If <full> is
2498 * set to 1, the full-line is matched and replaced. Otherwise, comma-separated
2499 * values are evaluated one by one. It returns 0 on success and -1 on error.
2500 */
2501int http_replace_hdrs(struct stream* s, struct htx *htx, struct ist name,
2502 const char *str, struct my_regex *re, int full)
Christopher Faulet72333522018-10-24 11:25:02 +02002503{
2504 struct http_hdr_ctx ctx;
2505 struct buffer *output = get_trash_chunk();
2506
Christopher Faulet72333522018-10-24 11:25:02 +02002507 ctx.blk = NULL;
Christopher Faulet92d34fe2019-12-17 09:20:34 +01002508 while (http_find_header(htx, name, &ctx, full)) {
Christopher Faulet72333522018-10-24 11:25:02 +02002509 if (!regex_exec_match2(re, ctx.value.ptr, ctx.value.len, MAX_MATCH, pmatch, 0))
2510 continue;
2511
2512 output->data = exp_replace(output->area, output->size, ctx.value.ptr, str, pmatch);
2513 if (output->data == -1)
2514 return -1;
2515 if (!http_replace_header_value(htx, &ctx, ist2(output->area, output->data)))
2516 return -1;
2517 }
2518 return 0;
2519}
2520
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002521/* This function executes one of the set-{method,path,query,uri} actions. It
2522 * takes the string from the variable 'replace' with length 'len', then modifies
2523 * the relevant part of the request line accordingly. Then it updates various
2524 * pointers to the next elements which were moved, and the total buffer length.
2525 * It finds the action to be performed in p[2], previously filled by function
2526 * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal
2527 * error, though this can be revisited when this code is finally exploited.
2528 *
2529 * 'action' can be '0' to replace method, '1' to replace path, '2' to replace
Christopher Faulet312294f2020-09-02 17:17:44 +02002530 * query string, 3 to replace uri or 4 to replace the path+query.
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002531 *
2532 * In query string case, the mark question '?' must be set at the start of the
2533 * string by the caller, event if the replacement query string is empty.
2534 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002535int http_req_replace_stline(int action, const char *replace, int len,
2536 struct proxy *px, struct stream *s)
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002537{
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002538 struct htx *htx = htxbuf(&s->req.buf);
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002539
2540 switch (action) {
2541 case 0: // method
2542 if (!http_replace_req_meth(htx, ist2(replace, len)))
2543 return -1;
2544 break;
2545
2546 case 1: // path
Christopher Fauletb8ce5052020-08-31 16:11:57 +02002547 if (!http_replace_req_path(htx, ist2(replace, len), 0))
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002548 return -1;
2549 break;
2550
2551 case 2: // query
2552 if (!http_replace_req_query(htx, ist2(replace, len)))
2553 return -1;
2554 break;
2555
2556 case 3: // uri
2557 if (!http_replace_req_uri(htx, ist2(replace, len)))
2558 return -1;
2559 break;
2560
Christopher Faulet312294f2020-09-02 17:17:44 +02002561 case 4: // path + query
2562 if (!http_replace_req_path(htx, ist2(replace, len), 1))
2563 return -1;
2564 break;
2565
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002566 default:
2567 return -1;
2568 }
2569 return 0;
2570}
2571
2572/* This function replace the HTTP status code and the associated message. The
Christopher Faulete00d06c2019-12-16 17:18:42 +01002573 * variable <status> contains the new status code. This function never fails. It
2574 * returns 0 in case of success, -1 in case of internal error.
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002575 */
Christopher Faulet96bff762019-12-17 13:46:18 +01002576int http_res_set_status(unsigned int status, struct ist reason, struct stream *s)
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002577{
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002578 struct htx *htx = htxbuf(&s->res.buf);
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002579 char *res;
2580
2581 chunk_reset(&trash);
2582 res = ultoa_o(status, trash.area, trash.size);
2583 trash.data = res - trash.area;
2584
2585 /* Do we have a custom reason format string? */
Tim Duesterhuse296d3e2020-03-05 17:56:31 +01002586 if (!isttest(reason)) {
Christopher Faulet96bff762019-12-17 13:46:18 +01002587 const char *str = http_get_reason(status);
Tim Duesterhusdcf753a2021-03-04 17:31:47 +01002588 reason = ist(str);
Christopher Faulet96bff762019-12-17 13:46:18 +01002589 }
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002590
Christopher Fauletbde2c4c2020-08-31 16:43:34 +02002591 if (!http_replace_res_status(htx, ist2(trash.area, trash.data), reason))
Christopher Faulete00d06c2019-12-16 17:18:42 +01002592 return -1;
Willy Tarreau640e2532023-01-10 14:50:44 +01002593 s->txn->status = status;
Christopher Faulete00d06c2019-12-16 17:18:42 +01002594 return 0;
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002595}
2596
Christopher Faulet3e964192018-10-24 11:39:23 +02002597/* Executes the http-request rules <rules> for stream <s>, proxy <px> and
2598 * transaction <txn>. Returns the verdict of the first rule that prevents
2599 * further processing of the request (auth, deny, ...), and defaults to
2600 * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or
2601 * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT
2602 * on txn->flags if it encounters a tarpit rule. If <deny_status> is not NULL
2603 * and a deny/tarpit rule is matched, it will be filled with this rule's deny
2604 * status.
2605 */
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002606static enum rule_result http_req_get_intercept_rule(struct proxy *px, struct list *def_rules,
2607 struct list *rules, struct stream *s)
Christopher Faulet3e964192018-10-24 11:39:23 +02002608{
2609 struct session *sess = strm_sess(s);
2610 struct http_txn *txn = s->txn;
Christopher Faulet3e964192018-10-24 11:39:23 +02002611 struct act_rule *rule;
Christopher Faulet3e964192018-10-24 11:39:23 +02002612 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002613 int act_opts = 0;
Christopher Faulet3e964192018-10-24 11:39:23 +02002614
Christopher Faulet3e964192018-10-24 11:39:23 +02002615 /* If "the current_rule_list" match the executed rule list, we are in
2616 * resume condition. If a resume is needed it is always in the action
2617 * and never in the ACL or converters. In this case, we initialise the
2618 * current rule, and go to the action execution point.
2619 */
2620 if (s->current_rule) {
2621 rule = s->current_rule;
2622 s->current_rule = NULL;
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002623 if (s->current_rule_list == rules || (def_rules && s->current_rule_list == def_rules))
Christopher Faulet3e964192018-10-24 11:39:23 +02002624 goto resume_execution;
2625 }
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002626 s->current_rule_list = ((!def_rules || s->current_rule_list == def_rules) ? rules : def_rules);
Christopher Faulet3e964192018-10-24 11:39:23 +02002627
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002628 restart:
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002629 /* start the ruleset evaluation in strict mode */
2630 txn->req.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01002631
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002632 list_for_each_entry(rule, s->current_rule_list, list) {
Christopher Faulet3e964192018-10-24 11:39:23 +02002633 /* check optional condition */
2634 if (rule->cond) {
2635 int ret;
2636
2637 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
2638 ret = acl_pass(ret);
2639
2640 if (rule->cond->pol == ACL_COND_UNLESS)
2641 ret = !ret;
2642
2643 if (!ret) /* condition not matched */
2644 continue;
2645 }
2646
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002647 act_opts |= ACT_OPT_FIRST;
Christopher Faulet3e964192018-10-24 11:39:23 +02002648 resume_execution:
Amaury Denoyelle03517732021-05-07 14:25:01 +02002649 if (rule->kw->flags & KWF_EXPERIMENTAL)
2650 mark_tainted(TAINTED_ACTION_EXP_EXECUTED);
2651
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002652 /* Always call the action function if defined */
2653 if (rule->action_ptr) {
2654 if ((s->req.flags & CF_READ_ERROR) ||
Christopher Faulet446d8032022-12-12 07:53:18 +01002655 ((s->req.flags & CF_SHUTR) &&
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002656 (px->options & PR_O_ABRT_CLOSE)))
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002657 act_opts |= ACT_OPT_FINAL;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002658
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002659 switch (rule->action_ptr(rule, px, sess, s, act_opts)) {
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002660 case ACT_RET_CONT:
2661 break;
2662 case ACT_RET_STOP:
2663 rule_ret = HTTP_RULE_RES_STOP;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002664 s->last_rule_file = rule->conf.file;
2665 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002666 goto end;
2667 case ACT_RET_YIELD:
2668 s->current_rule = rule;
2669 rule_ret = HTTP_RULE_RES_YIELD;
2670 goto end;
2671 case ACT_RET_ERR:
2672 rule_ret = HTTP_RULE_RES_ERROR;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002673 s->last_rule_file = rule->conf.file;
2674 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002675 goto end;
2676 case ACT_RET_DONE:
2677 rule_ret = HTTP_RULE_RES_DONE;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002678 s->last_rule_file = rule->conf.file;
2679 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002680 goto end;
2681 case ACT_RET_DENY:
Christopher Fauletb58f62b2020-01-13 16:40:13 +01002682 if (txn->status == -1)
2683 txn->status = 403;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002684 rule_ret = HTTP_RULE_RES_DENY;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002685 s->last_rule_file = rule->conf.file;
2686 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002687 goto end;
2688 case ACT_RET_ABRT:
2689 rule_ret = HTTP_RULE_RES_ABRT;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002690 s->last_rule_file = rule->conf.file;
2691 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002692 goto end;
2693 case ACT_RET_INV:
2694 rule_ret = HTTP_RULE_RES_BADREQ;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002695 s->last_rule_file = rule->conf.file;
2696 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002697 goto end;
2698 }
2699 continue; /* eval the next rule */
2700 }
2701
2702 /* If not action function defined, check for known actions */
Christopher Faulet3e964192018-10-24 11:39:23 +02002703 switch (rule->action) {
2704 case ACT_ACTION_ALLOW:
2705 rule_ret = HTTP_RULE_RES_STOP;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002706 s->last_rule_file = rule->conf.file;
2707 s->last_rule_line = rule->conf.line;
Christopher Faulet3e964192018-10-24 11:39:23 +02002708 goto end;
2709
2710 case ACT_ACTION_DENY:
Christopher Faulet5cb513a2020-05-13 17:56:56 +02002711 txn->status = rule->arg.http_reply->status;
2712 txn->http_reply = rule->arg.http_reply;
Christopher Faulet3e964192018-10-24 11:39:23 +02002713 rule_ret = HTTP_RULE_RES_DENY;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002714 s->last_rule_file = rule->conf.file;
2715 s->last_rule_line = rule->conf.line;
Christopher Faulet3e964192018-10-24 11:39:23 +02002716 goto end;
2717
2718 case ACT_HTTP_REQ_TARPIT:
2719 txn->flags |= TX_CLTARPIT;
Christopher Faulet5cb513a2020-05-13 17:56:56 +02002720 txn->status = rule->arg.http_reply->status;
2721 txn->http_reply = rule->arg.http_reply;
Christopher Faulet3e964192018-10-24 11:39:23 +02002722 rule_ret = HTTP_RULE_RES_DENY;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002723 s->last_rule_file = rule->conf.file;
2724 s->last_rule_line = rule->conf.line;
Christopher Faulet3e964192018-10-24 11:39:23 +02002725 goto end;
2726
Willy Tarreaubc1223b2021-09-02 16:54:33 +02002727 case ACT_HTTP_REDIR: {
2728 int ret = http_apply_redirect_rule(rule->arg.redir, s, txn);
2729
2730 if (ret == 2) // 2 == skip
2731 break;
2732
2733 rule_ret = ret ? HTTP_RULE_RES_ABRT : HTTP_RULE_RES_ERROR;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002734 s->last_rule_file = rule->conf.file;
2735 s->last_rule_line = rule->conf.line;
Christopher Faulet3e964192018-10-24 11:39:23 +02002736 goto end;
Willy Tarreaubc1223b2021-09-02 16:54:33 +02002737 }
Christopher Faulet3e964192018-10-24 11:39:23 +02002738
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002739 /* other flags exists, but normally, they never be matched. */
Christopher Faulet3e964192018-10-24 11:39:23 +02002740 default:
2741 break;
2742 }
2743 }
2744
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002745 if (def_rules && s->current_rule_list == def_rules) {
2746 s->current_rule_list = rules;
2747 goto restart;
2748 }
2749
Christopher Faulet3e964192018-10-24 11:39:23 +02002750 end:
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002751 /* if the ruleset evaluation is finished reset the strict mode */
Christopher Faulet46f95542019-12-20 10:07:22 +01002752 if (rule_ret != HTTP_RULE_RES_YIELD)
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002753 txn->req.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01002754
Christopher Faulet3e964192018-10-24 11:39:23 +02002755 /* we reached the end of the rules, nothing to report */
2756 return rule_ret;
2757}
2758
2759/* Executes the http-response rules <rules> for stream <s> and proxy <px>. It
2760 * returns one of 5 possible statuses: HTTP_RULE_RES_CONT, HTTP_RULE_RES_STOP,
2761 * HTTP_RULE_RES_DONE, HTTP_RULE_RES_YIELD, or HTTP_RULE_RES_BADREQ. If *CONT
2762 * is returned, the process can continue the evaluation of next rule list. If
2763 * *STOP or *DONE is returned, the process must stop the evaluation. If *BADREQ
2764 * is returned, it means the operation could not be processed and a server error
Christopher Fauleta53abad2020-05-13 08:12:22 +02002765 * must be returned. If *YIELD is returned, the caller must call again the
2766 * function with the same context.
Christopher Faulet3e964192018-10-24 11:39:23 +02002767 */
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002768static enum rule_result http_res_get_intercept_rule(struct proxy *px, struct list *def_rules,
2769 struct list *rules, struct stream *s)
Christopher Faulet3e964192018-10-24 11:39:23 +02002770{
2771 struct session *sess = strm_sess(s);
2772 struct http_txn *txn = s->txn;
Christopher Faulet3e964192018-10-24 11:39:23 +02002773 struct act_rule *rule;
Christopher Faulet3e964192018-10-24 11:39:23 +02002774 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002775 int act_opts = 0;
Christopher Faulet3e964192018-10-24 11:39:23 +02002776
Christopher Faulet3e964192018-10-24 11:39:23 +02002777 /* If "the current_rule_list" match the executed rule list, we are in
2778 * resume condition. If a resume is needed it is always in the action
2779 * and never in the ACL or converters. In this case, we initialise the
2780 * current rule, and go to the action execution point.
2781 */
2782 if (s->current_rule) {
2783 rule = s->current_rule;
2784 s->current_rule = NULL;
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002785 if (s->current_rule_list == rules || (def_rules && s->current_rule_list == def_rules))
Christopher Faulet3e964192018-10-24 11:39:23 +02002786 goto resume_execution;
2787 }
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002788 s->current_rule_list = ((!def_rules || s->current_rule_list == def_rules) ? rules : def_rules);
2789
2790 restart:
Christopher Faulet3e964192018-10-24 11:39:23 +02002791
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002792 /* start the ruleset evaluation in strict mode */
2793 txn->rsp.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01002794
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002795 list_for_each_entry(rule, s->current_rule_list, list) {
Christopher Faulet3e964192018-10-24 11:39:23 +02002796 /* check optional condition */
2797 if (rule->cond) {
2798 int ret;
2799
2800 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
2801 ret = acl_pass(ret);
2802
2803 if (rule->cond->pol == ACL_COND_UNLESS)
2804 ret = !ret;
2805
2806 if (!ret) /* condition not matched */
2807 continue;
2808 }
2809
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002810 act_opts |= ACT_OPT_FIRST;
Christopher Faulet3e964192018-10-24 11:39:23 +02002811resume_execution:
Amaury Denoyelle03517732021-05-07 14:25:01 +02002812 if (rule->kw->flags & KWF_EXPERIMENTAL)
2813 mark_tainted(TAINTED_ACTION_EXP_EXECUTED);
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002814
2815 /* Always call the action function if defined */
2816 if (rule->action_ptr) {
2817 if ((s->req.flags & CF_READ_ERROR) ||
Christopher Faulet446d8032022-12-12 07:53:18 +01002818 ((s->req.flags & CF_SHUTR) &&
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002819 (px->options & PR_O_ABRT_CLOSE)))
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002820 act_opts |= ACT_OPT_FINAL;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002821
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002822 switch (rule->action_ptr(rule, px, sess, s, act_opts)) {
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002823 case ACT_RET_CONT:
2824 break;
2825 case ACT_RET_STOP:
2826 rule_ret = HTTP_RULE_RES_STOP;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002827 s->last_rule_file = rule->conf.file;
2828 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002829 goto end;
2830 case ACT_RET_YIELD:
2831 s->current_rule = rule;
2832 rule_ret = HTTP_RULE_RES_YIELD;
2833 goto end;
2834 case ACT_RET_ERR:
2835 rule_ret = HTTP_RULE_RES_ERROR;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002836 s->last_rule_file = rule->conf.file;
2837 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002838 goto end;
2839 case ACT_RET_DONE:
2840 rule_ret = HTTP_RULE_RES_DONE;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002841 s->last_rule_file = rule->conf.file;
2842 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002843 goto end;
2844 case ACT_RET_DENY:
Christopher Fauletb58f62b2020-01-13 16:40:13 +01002845 if (txn->status == -1)
2846 txn->status = 502;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002847 rule_ret = HTTP_RULE_RES_DENY;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002848 s->last_rule_file = rule->conf.file;
2849 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002850 goto end;
2851 case ACT_RET_ABRT:
2852 rule_ret = HTTP_RULE_RES_ABRT;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002853 s->last_rule_file = rule->conf.file;
2854 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002855 goto end;
2856 case ACT_RET_INV:
2857 rule_ret = HTTP_RULE_RES_BADREQ;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002858 s->last_rule_file = rule->conf.file;
2859 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002860 goto end;
2861 }
2862 continue; /* eval the next rule */
2863 }
2864
2865 /* If not action function defined, check for known actions */
Christopher Faulet3e964192018-10-24 11:39:23 +02002866 switch (rule->action) {
2867 case ACT_ACTION_ALLOW:
2868 rule_ret = HTTP_RULE_RES_STOP; /* "allow" rules are OK */
Willy Tarreauc6dae862022-03-09 17:23:10 +01002869 s->last_rule_file = rule->conf.file;
2870 s->last_rule_line = rule->conf.line;
Christopher Faulet3e964192018-10-24 11:39:23 +02002871 goto end;
2872
2873 case ACT_ACTION_DENY:
Christopher Faulet5cb513a2020-05-13 17:56:56 +02002874 txn->status = rule->arg.http_reply->status;
2875 txn->http_reply = rule->arg.http_reply;
Christopher Faulet3a26bee2019-12-16 12:47:40 +01002876 rule_ret = HTTP_RULE_RES_DENY;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002877 s->last_rule_file = rule->conf.file;
2878 s->last_rule_line = rule->conf.line;
Christopher Faulet3e964192018-10-24 11:39:23 +02002879 goto end;
2880
Willy Tarreaubc1223b2021-09-02 16:54:33 +02002881 case ACT_HTTP_REDIR: {
2882 int ret = http_apply_redirect_rule(rule->arg.redir, s, txn);
Christopher Faulet3e964192018-10-24 11:39:23 +02002883
Willy Tarreaubc1223b2021-09-02 16:54:33 +02002884 if (ret == 2) // 2 == skip
2885 break;
2886
2887 rule_ret = ret ? HTTP_RULE_RES_ABRT : HTTP_RULE_RES_ERROR;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002888 s->last_rule_file = rule->conf.file;
2889 s->last_rule_line = rule->conf.line;
Willy Tarreaubc1223b2021-09-02 16:54:33 +02002890 goto end;
2891 }
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002892 /* other flags exists, but normally, they never be matched. */
Christopher Faulet3e964192018-10-24 11:39:23 +02002893 default:
2894 break;
2895 }
2896 }
2897
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002898 if (def_rules && s->current_rule_list == def_rules) {
2899 s->current_rule_list = rules;
2900 goto restart;
2901 }
2902
Christopher Faulet3e964192018-10-24 11:39:23 +02002903 end:
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002904 /* if the ruleset evaluation is finished reset the strict mode */
Christopher Faulet46f95542019-12-20 10:07:22 +01002905 if (rule_ret != HTTP_RULE_RES_YIELD)
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002906 txn->rsp.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01002907
Christopher Faulet3e964192018-10-24 11:39:23 +02002908 /* we reached the end of the rules, nothing to report */
2909 return rule_ret;
2910}
2911
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002912/* Executes backend and frontend http-after-response rules for the stream <s>,
2913 * in that order. it return 1 on success and 0 on error. It is the caller
2914 * responsibility to catch error or ignore it. If it catches it, this function
2915 * may be called a second time, for the internal error.
2916 */
2917int http_eval_after_res_rules(struct stream *s)
2918{
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002919 struct list *def_rules, *rules;
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002920 struct session *sess = s->sess;
2921 enum rule_result ret = HTTP_RULE_RES_CONT;
2922
Christopher Faulet507479b2020-05-15 12:29:46 +02002923 /* Eval after-response ruleset only if the reply is not const */
2924 if (s->txn->flags & TX_CONST_REPLY)
2925 goto end;
2926
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002927 /* prune the request variables if not already done and swap to the response variables. */
2928 if (s->vars_reqres.scope != SCOPE_RES) {
2929 if (!LIST_ISEMPTY(&s->vars_reqres.head))
2930 vars_prune(&s->vars_reqres, s->sess, s);
Willy Tarreaub7bfcb32021-08-31 08:13:25 +02002931 vars_init_head(&s->vars_reqres, SCOPE_RES);
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002932 }
2933
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002934 def_rules = (s->be->defpx ? &s->be->defpx->http_after_res_rules : NULL);
2935 rules = &s->be->http_after_res_rules;
2936
2937 ret = http_res_get_intercept_rule(s->be, def_rules, rules, s);
Christopher Faulet4c5a5912021-11-09 17:48:39 +01002938 if ((ret == HTTP_RULE_RES_CONT || ret == HTTP_RULE_RES_STOP) && sess->fe != s->be) {
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002939 def_rules = ((sess->fe->defpx && sess->fe->defpx != s->be->defpx) ? &sess->fe->defpx->http_after_res_rules : NULL);
2940 rules = &sess->fe->http_after_res_rules;
2941 ret = http_res_get_intercept_rule(sess->fe, def_rules, rules, s);
2942 }
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002943
Christopher Faulet507479b2020-05-15 12:29:46 +02002944 end:
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002945 /* All other codes than CONTINUE, STOP or DONE are forbidden */
2946 return (ret == HTTP_RULE_RES_CONT || ret == HTTP_RULE_RES_STOP || ret == HTTP_RULE_RES_DONE);
2947}
2948
Christopher Fauletfcda7c62018-10-24 11:56:22 +02002949/*
2950 * Manage client-side cookie. It can impact performance by about 2% so it is
2951 * desirable to call it only when needed. This code is quite complex because
2952 * of the multiple very crappy and ambiguous syntaxes we have to support. it
2953 * highly recommended not to touch this part without a good reason !
2954 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002955static void http_manage_client_side_cookies(struct stream *s, struct channel *req)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02002956{
2957 struct session *sess = s->sess;
2958 struct http_txn *txn = s->txn;
2959 struct htx *htx;
2960 struct http_hdr_ctx ctx;
2961 char *hdr_beg, *hdr_end, *del_from;
2962 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
2963 int preserve_hdr;
2964
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002965 htx = htxbuf(&req->buf);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02002966 ctx.blk = NULL;
2967 while (http_find_header(htx, ist("Cookie"), &ctx, 1)) {
Olivier Houchardf0f42382019-07-22 17:43:46 +02002968 int is_first = 1;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02002969 del_from = NULL; /* nothing to be deleted */
2970 preserve_hdr = 0; /* assume we may kill the whole header */
2971
2972 /* Now look for cookies. Conforming to RFC2109, we have to support
2973 * attributes whose name begin with a '$', and associate them with
2974 * the right cookie, if we want to delete this cookie.
2975 * So there are 3 cases for each cookie read :
2976 * 1) it's a special attribute, beginning with a '$' : ignore it.
2977 * 2) it's a server id cookie that we *MAY* want to delete : save
2978 * some pointers on it (last semi-colon, beginning of cookie...)
2979 * 3) it's an application cookie : we *MAY* have to delete a previous
2980 * "special" cookie.
2981 * At the end of loop, if a "special" cookie remains, we may have to
2982 * remove it. If no application cookie persists in the header, we
2983 * *MUST* delete it.
2984 *
2985 * Note: RFC2965 is unclear about the processing of spaces around
2986 * the equal sign in the ATTR=VALUE form. A careful inspection of
2987 * the RFC explicitly allows spaces before it, and not within the
2988 * tokens (attrs or values). An inspection of RFC2109 allows that
2989 * too but section 10.1.3 lets one think that spaces may be allowed
2990 * after the equal sign too, resulting in some (rare) buggy
2991 * implementations trying to do that. So let's do what servers do.
2992 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
2993 * allowed quoted strings in values, with any possible character
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05002994 * after a backslash, including control chars and delimiters, which
Christopher Fauletfcda7c62018-10-24 11:56:22 +02002995 * causes parsing to become ambiguous. Browsers also allow spaces
2996 * within values even without quotes.
2997 *
2998 * We have to keep multiple pointers in order to support cookie
2999 * removal at the beginning, middle or end of header without
3000 * corrupting the header. All of these headers are valid :
3001 *
3002 * hdr_beg hdr_end
3003 * | |
3004 * v |
3005 * NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3 |
3006 * NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3 v
3007 * NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3
3008 * | | | | | | |
3009 * | | | | | | |
3010 * | | | | | | +--> next
3011 * | | | | | +----> val_end
3012 * | | | | +-----------> val_beg
3013 * | | | +--------------> equal
3014 * | | +----------------> att_end
3015 * | +---------------------> att_beg
3016 * +--------------------------> prev
3017 *
3018 */
3019 hdr_beg = ctx.value.ptr;
3020 hdr_end = hdr_beg + ctx.value.len;
3021 for (prev = hdr_beg; prev < hdr_end; prev = next) {
3022 /* Iterate through all cookies on this line */
3023
3024 /* find att_beg */
3025 att_beg = prev;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003026 if (!is_first)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003027 att_beg++;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003028 is_first = 0;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003029
3030 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
3031 att_beg++;
3032
3033 /* find att_end : this is the first character after the last non
3034 * space before the equal. It may be equal to hdr_end.
3035 */
3036 equal = att_end = att_beg;
3037 while (equal < hdr_end) {
3038 if (*equal == '=' || *equal == ',' || *equal == ';')
3039 break;
3040 if (HTTP_IS_SPHT(*equal++))
3041 continue;
3042 att_end = equal;
3043 }
3044
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003045 /* here, <equal> points to '=', a delimiter or the end. <att_end>
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003046 * is between <att_beg> and <equal>, both may be identical.
3047 */
3048 /* look for end of cookie if there is an equal sign */
3049 if (equal < hdr_end && *equal == '=') {
3050 /* look for the beginning of the value */
3051 val_beg = equal + 1;
3052 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
3053 val_beg++;
3054
3055 /* find the end of the value, respecting quotes */
3056 next = http_find_cookie_value_end(val_beg, hdr_end);
3057
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003058 /* make val_end point to the first white space or delimiter after the value */
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003059 val_end = next;
3060 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
3061 val_end--;
3062 }
3063 else
3064 val_beg = val_end = next = equal;
3065
3066 /* We have nothing to do with attributes beginning with
3067 * '$'. However, they will automatically be removed if a
3068 * header before them is removed, since they're supposed
3069 * to be linked together.
3070 */
3071 if (*att_beg == '$')
3072 continue;
3073
3074 /* Ignore cookies with no equal sign */
3075 if (equal == next) {
3076 /* This is not our cookie, so we must preserve it. But if we already
3077 * scheduled another cookie for removal, we cannot remove the
3078 * complete header, but we can remove the previous block itself.
3079 */
3080 preserve_hdr = 1;
3081 if (del_from != NULL) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003082 int delta = http_del_hdr_value(hdr_beg, hdr_end, &del_from, prev);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003083 val_end += delta;
3084 next += delta;
3085 hdr_end += delta;
3086 prev = del_from;
3087 del_from = NULL;
3088 }
3089 continue;
3090 }
3091
3092 /* if there are spaces around the equal sign, we need to
3093 * strip them otherwise we'll get trouble for cookie captures,
3094 * or even for rewrites. Since this happens extremely rarely,
3095 * it does not hurt performance.
3096 */
3097 if (unlikely(att_end != equal || val_beg > equal + 1)) {
3098 int stripped_before = 0;
3099 int stripped_after = 0;
3100
3101 if (att_end != equal) {
3102 memmove(att_end, equal, hdr_end - equal);
3103 stripped_before = (att_end - equal);
3104 equal += stripped_before;
3105 val_beg += stripped_before;
3106 }
3107
3108 if (val_beg > equal + 1) {
3109 memmove(equal + 1, val_beg, hdr_end + stripped_before - val_beg);
3110 stripped_after = (equal + 1) - val_beg;
3111 val_beg += stripped_after;
3112 stripped_before += stripped_after;
3113 }
3114
3115 val_end += stripped_before;
3116 next += stripped_before;
3117 hdr_end += stripped_before;
3118 }
3119 /* now everything is as on the diagram above */
3120
3121 /* First, let's see if we want to capture this cookie. We check
3122 * that we don't already have a client side cookie, because we
3123 * can only capture one. Also as an optimisation, we ignore
3124 * cookies shorter than the declared name.
3125 */
3126 if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL &&
3127 (val_end - att_beg >= sess->fe->capture_namelen) &&
3128 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
3129 int log_len = val_end - att_beg;
3130
3131 if ((txn->cli_cookie = pool_alloc(pool_head_capture)) == NULL) {
3132 ha_alert("HTTP logging : out of memory.\n");
3133 } else {
3134 if (log_len > sess->fe->capture_len)
3135 log_len = sess->fe->capture_len;
3136 memcpy(txn->cli_cookie, att_beg, log_len);
3137 txn->cli_cookie[log_len] = 0;
3138 }
3139 }
3140
3141 /* Persistence cookies in passive, rewrite or insert mode have the
3142 * following form :
3143 *
3144 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
3145 *
3146 * For cookies in prefix mode, the form is :
3147 *
3148 * Cookie: NAME=SRV~VALUE
3149 */
3150 if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
3151 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
3152 struct server *srv = s->be->srv;
3153 char *delim;
3154
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003155 /* if we're in cookie prefix mode, we'll search the delimiter so that we
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003156 * have the server ID between val_beg and delim, and the original cookie between
3157 * delim+1 and val_end. Otherwise, delim==val_end :
3158 *
3159 * hdr_beg
3160 * |
3161 * v
3162 * NAME=SRV; # in all but prefix modes
3163 * NAME=SRV~OPAQUE ; # in prefix mode
3164 * || || | |+-> next
3165 * || || | +--> val_end
3166 * || || +---------> delim
3167 * || |+------------> val_beg
3168 * || +-------------> att_end = equal
3169 * |+-----------------> att_beg
3170 * +------------------> prev
3171 *
3172 */
3173 if (s->be->ck_opts & PR_CK_PFX) {
3174 for (delim = val_beg; delim < val_end; delim++)
3175 if (*delim == COOKIE_DELIM)
3176 break;
3177 }
3178 else {
3179 char *vbar1;
3180 delim = val_end;
3181 /* Now check if the cookie contains a date field, which would
3182 * appear after a vertical bar ('|') just after the server name
3183 * and before the delimiter.
3184 */
3185 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
3186 if (vbar1) {
3187 /* OK, so left of the bar is the server's cookie and
3188 * right is the last seen date. It is a base64 encoded
3189 * 30-bit value representing the UNIX date since the
3190 * epoch in 4-second quantities.
3191 */
3192 int val;
3193 delim = vbar1++;
3194 if (val_end - vbar1 >= 5) {
3195 val = b64tos30(vbar1);
3196 if (val > 0)
3197 txn->cookie_last_date = val << 2;
3198 }
3199 /* look for a second vertical bar */
3200 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
3201 if (vbar1 && (val_end - vbar1 > 5)) {
3202 val = b64tos30(vbar1 + 1);
3203 if (val > 0)
3204 txn->cookie_first_date = val << 2;
3205 }
3206 }
3207 }
3208
3209 /* if the cookie has an expiration date and the proxy wants to check
3210 * it, then we do that now. We first check if the cookie is too old,
3211 * then only if it has expired. We detect strict overflow because the
3212 * time resolution here is not great (4 seconds). Cookies with dates
3213 * in the future are ignored if their offset is beyond one day. This
3214 * allows an admin to fix timezone issues without expiring everyone
3215 * and at the same time avoids keeping unwanted side effects for too
3216 * long.
3217 */
3218 if (txn->cookie_first_date && s->be->cookie_maxlife &&
3219 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) ||
3220 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
3221 txn->flags &= ~TX_CK_MASK;
3222 txn->flags |= TX_CK_OLD;
3223 delim = val_beg; // let's pretend we have not found the cookie
3224 txn->cookie_first_date = 0;
3225 txn->cookie_last_date = 0;
3226 }
3227 else if (txn->cookie_last_date && s->be->cookie_maxidle &&
3228 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) ||
3229 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
3230 txn->flags &= ~TX_CK_MASK;
3231 txn->flags |= TX_CK_EXPIRED;
3232 delim = val_beg; // let's pretend we have not found the cookie
3233 txn->cookie_first_date = 0;
3234 txn->cookie_last_date = 0;
3235 }
3236
3237 /* Here, we'll look for the first running server which supports the cookie.
3238 * This allows to share a same cookie between several servers, for example
3239 * to dedicate backup servers to specific servers only.
3240 * However, to prevent clients from sticking to cookie-less backup server
3241 * when they have incidentely learned an empty cookie, we simply ignore
3242 * empty cookies and mark them as invalid.
3243 * The same behaviour is applied when persistence must be ignored.
3244 */
3245 if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
3246 srv = NULL;
3247
3248 while (srv) {
3249 if (srv->cookie && (srv->cklen == delim - val_beg) &&
3250 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
3251 if ((srv->cur_state != SRV_ST_STOPPED) ||
3252 (s->be->options & PR_O_PERSIST) ||
3253 (s->flags & SF_FORCE_PRST)) {
3254 /* we found the server and we can use it */
3255 txn->flags &= ~TX_CK_MASK;
3256 txn->flags |= (srv->cur_state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
3257 s->flags |= SF_DIRECT | SF_ASSIGNED;
3258 s->target = &srv->obj_type;
3259 break;
3260 } else {
3261 /* we found a server, but it's down,
3262 * mark it as such and go on in case
3263 * another one is available.
3264 */
3265 txn->flags &= ~TX_CK_MASK;
3266 txn->flags |= TX_CK_DOWN;
3267 }
3268 }
3269 srv = srv->next;
3270 }
3271
3272 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
3273 /* no server matched this cookie or we deliberately skipped it */
3274 txn->flags &= ~TX_CK_MASK;
3275 if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
3276 txn->flags |= TX_CK_UNUSED;
3277 else
3278 txn->flags |= TX_CK_INVALID;
3279 }
3280
3281 /* depending on the cookie mode, we may have to either :
3282 * - delete the complete cookie if we're in insert+indirect mode, so that
3283 * the server never sees it ;
3284 * - remove the server id from the cookie value, and tag the cookie as an
Joseph Herlante9d5c722018-11-25 11:00:25 -08003285 * application cookie so that it does not get accidentally removed later,
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003286 * if we're in cookie prefix mode
3287 */
3288 if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
3289 int delta; /* negative */
3290
3291 memmove(val_beg, delim + 1, hdr_end - (delim + 1));
3292 delta = val_beg - (delim + 1);
3293 val_end += delta;
3294 next += delta;
3295 hdr_end += delta;
3296 del_from = NULL;
3297 preserve_hdr = 1; /* we want to keep this cookie */
3298 }
3299 else if (del_from == NULL &&
3300 (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
3301 del_from = prev;
3302 }
3303 }
3304 else {
3305 /* This is not our cookie, so we must preserve it. But if we already
3306 * scheduled another cookie for removal, we cannot remove the
3307 * complete header, but we can remove the previous block itself.
3308 */
3309 preserve_hdr = 1;
3310
3311 if (del_from != NULL) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003312 int delta = http_del_hdr_value(hdr_beg, hdr_end, &del_from, prev);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003313 if (att_beg >= del_from)
3314 att_beg += delta;
3315 if (att_end >= del_from)
3316 att_end += delta;
3317 val_beg += delta;
3318 val_end += delta;
3319 next += delta;
3320 hdr_end += delta;
3321 prev = del_from;
3322 del_from = NULL;
3323 }
3324 }
3325
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003326 } /* for each cookie */
3327
3328
3329 /* There are no more cookies on this line.
3330 * We may still have one (or several) marked for deletion at the
3331 * end of the line. We must do this now in two ways :
3332 * - if some cookies must be preserved, we only delete from the
3333 * mark to the end of line ;
3334 * - if nothing needs to be preserved, simply delete the whole header
3335 */
3336 if (del_from) {
3337 hdr_end = (preserve_hdr ? del_from : hdr_beg);
3338 }
3339 if ((hdr_end - hdr_beg) != ctx.value.len) {
Christopher Faulet3e2638e2019-06-18 09:49:16 +02003340 if (hdr_beg != hdr_end)
3341 htx_change_blk_value_len(htx, ctx.blk, hdr_end - hdr_beg);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003342 else
3343 http_remove_header(htx, &ctx);
3344 }
3345 } /* for each "Cookie header */
3346}
3347
3348/*
3349 * Manage server-side cookies. It can impact performance by about 2% so it is
3350 * desirable to call it only when needed. This function is also used when we
3351 * just need to know if there is a cookie (eg: for check-cache).
3352 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003353static void http_manage_server_side_cookies(struct stream *s, struct channel *res)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003354{
3355 struct session *sess = s->sess;
3356 struct http_txn *txn = s->txn;
3357 struct htx *htx;
3358 struct http_hdr_ctx ctx;
3359 struct server *srv;
3360 char *hdr_beg, *hdr_end;
3361 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003362
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003363 htx = htxbuf(&res->buf);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003364
3365 ctx.blk = NULL;
Willy Tarreauaa1909e2022-11-14 18:58:35 +01003366 while (http_find_header(htx, ist("Set-Cookie"), &ctx, 1)) {
Olivier Houchardf0f42382019-07-22 17:43:46 +02003367 int is_first = 1;
3368
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003369 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
3370 * <prev> points to the colon.
3371 */
3372 txn->flags |= TX_SCK_PRESENT;
3373
3374 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
3375 * check-cache is enabled) and we are not interested in checking
3376 * them. Warning, the cookie capture is declared in the frontend.
3377 */
3378 if (s->be->cookie_name == NULL && sess->fe->capture_name == NULL)
3379 break;
3380
3381 /* OK so now we know we have to process this response cookie.
3382 * The format of the Set-Cookie header is slightly different
3383 * from the format of the Cookie header in that it does not
3384 * support the comma as a cookie delimiter (thus the header
3385 * cannot be folded) because the Expires attribute described in
3386 * the original Netscape's spec may contain an unquoted date
3387 * with a comma inside. We have to live with this because
3388 * many browsers don't support Max-Age and some browsers don't
3389 * support quoted strings. However the Set-Cookie2 header is
Willy Tarreauaa1909e2022-11-14 18:58:35 +01003390 * clean but basically nobody supports it.
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003391 *
3392 * We have to keep multiple pointers in order to support cookie
3393 * removal at the beginning, middle or end of header without
3394 * corrupting the header (in case of set-cookie2). A special
3395 * pointer, <scav> points to the beginning of the set-cookie-av
3396 * fields after the first semi-colon. The <next> pointer points
3397 * either to the end of line (set-cookie) or next unquoted comma
3398 * (set-cookie2). All of these headers are valid :
3399 *
3400 * hdr_beg hdr_end
3401 * | |
3402 * v |
3403 * NAME1 = VALUE 1 ; Secure; Path="/" |
3404 * NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT v
3405 * NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT
3406 * NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard
3407 * | | | | | | | |
3408 * | | | | | | | +-> next
3409 * | | | | | | +------------> scav
3410 * | | | | | +--------------> val_end
3411 * | | | | +--------------------> val_beg
3412 * | | | +----------------------> equal
3413 * | | +------------------------> att_end
3414 * | +----------------------------> att_beg
3415 * +------------------------------> prev
3416 * -------------------------------> hdr_beg
3417 */
3418 hdr_beg = ctx.value.ptr;
3419 hdr_end = hdr_beg + ctx.value.len;
3420 for (prev = hdr_beg; prev < hdr_end; prev = next) {
3421
3422 /* Iterate through all cookies on this line */
3423
3424 /* find att_beg */
3425 att_beg = prev;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003426 if (!is_first)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003427 att_beg++;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003428 is_first = 0;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003429
3430 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
3431 att_beg++;
3432
3433 /* find att_end : this is the first character after the last non
3434 * space before the equal. It may be equal to hdr_end.
3435 */
3436 equal = att_end = att_beg;
3437
3438 while (equal < hdr_end) {
Willy Tarreauaa1909e2022-11-14 18:58:35 +01003439 if (*equal == '=' || *equal == ';')
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003440 break;
3441 if (HTTP_IS_SPHT(*equal++))
3442 continue;
3443 att_end = equal;
3444 }
3445
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003446 /* here, <equal> points to '=', a delimiter or the end. <att_end>
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003447 * is between <att_beg> and <equal>, both may be identical.
3448 */
3449
3450 /* look for end of cookie if there is an equal sign */
3451 if (equal < hdr_end && *equal == '=') {
3452 /* look for the beginning of the value */
3453 val_beg = equal + 1;
3454 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
3455 val_beg++;
3456
3457 /* find the end of the value, respecting quotes */
3458 next = http_find_cookie_value_end(val_beg, hdr_end);
3459
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003460 /* make val_end point to the first white space or delimiter after the value */
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003461 val_end = next;
3462 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
3463 val_end--;
3464 }
3465 else {
3466 /* <equal> points to next comma, semi-colon or EOL */
3467 val_beg = val_end = next = equal;
3468 }
3469
3470 if (next < hdr_end) {
Willy Tarreauaa1909e2022-11-14 18:58:35 +01003471 /* For Set-Cookie, since commas are permitted
3472 * in values, skip to the end.
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003473 */
Willy Tarreauaa1909e2022-11-14 18:58:35 +01003474 next = hdr_end;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003475 }
3476
3477 /* Now everything is as on the diagram above */
3478
3479 /* Ignore cookies with no equal sign */
3480 if (equal == val_end)
3481 continue;
3482
3483 /* If there are spaces around the equal sign, we need to
3484 * strip them otherwise we'll get trouble for cookie captures,
3485 * or even for rewrites. Since this happens extremely rarely,
3486 * it does not hurt performance.
3487 */
3488 if (unlikely(att_end != equal || val_beg > equal + 1)) {
3489 int stripped_before = 0;
3490 int stripped_after = 0;
3491
3492 if (att_end != equal) {
3493 memmove(att_end, equal, hdr_end - equal);
3494 stripped_before = (att_end - equal);
3495 equal += stripped_before;
3496 val_beg += stripped_before;
3497 }
3498
3499 if (val_beg > equal + 1) {
3500 memmove(equal + 1, val_beg, hdr_end + stripped_before - val_beg);
3501 stripped_after = (equal + 1) - val_beg;
3502 val_beg += stripped_after;
3503 stripped_before += stripped_after;
3504 }
3505
3506 val_end += stripped_before;
3507 next += stripped_before;
3508 hdr_end += stripped_before;
3509
Christopher Faulet3e2638e2019-06-18 09:49:16 +02003510 htx_change_blk_value_len(htx, ctx.blk, hdr_end - hdr_beg);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003511 ctx.value.len = hdr_end - hdr_beg;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003512 }
3513
3514 /* First, let's see if we want to capture this cookie. We check
3515 * that we don't already have a server side cookie, because we
3516 * can only capture one. Also as an optimisation, we ignore
3517 * cookies shorter than the declared name.
3518 */
3519 if (sess->fe->capture_name != NULL &&
3520 txn->srv_cookie == NULL &&
3521 (val_end - att_beg >= sess->fe->capture_namelen) &&
3522 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
3523 int log_len = val_end - att_beg;
3524 if ((txn->srv_cookie = pool_alloc(pool_head_capture)) == NULL) {
3525 ha_alert("HTTP logging : out of memory.\n");
3526 }
3527 else {
3528 if (log_len > sess->fe->capture_len)
3529 log_len = sess->fe->capture_len;
3530 memcpy(txn->srv_cookie, att_beg, log_len);
3531 txn->srv_cookie[log_len] = 0;
3532 }
3533 }
3534
3535 srv = objt_server(s->target);
3536 /* now check if we need to process it for persistence */
3537 if (!(s->flags & SF_IGNORE_PRST) &&
3538 (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
3539 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
3540 /* assume passive cookie by default */
3541 txn->flags &= ~TX_SCK_MASK;
3542 txn->flags |= TX_SCK_FOUND;
3543
3544 /* If the cookie is in insert mode on a known server, we'll delete
3545 * this occurrence because we'll insert another one later.
3546 * We'll delete it too if the "indirect" option is set and we're in
3547 * a direct access.
3548 */
3549 if (s->be->ck_opts & PR_CK_PSV) {
3550 /* The "preserve" flag was set, we don't want to touch the
3551 * server's cookie.
3552 */
3553 }
3554 else if ((srv && (s->be->ck_opts & PR_CK_INS)) ||
3555 ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) {
3556 /* this cookie must be deleted */
3557 if (prev == hdr_beg && next == hdr_end) {
3558 /* whole header */
3559 http_remove_header(htx, &ctx);
3560 /* note: while both invalid now, <next> and <hdr_end>
3561 * are still equal, so the for() will stop as expected.
3562 */
3563 } else {
3564 /* just remove the value */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003565 int delta = http_del_hdr_value(hdr_beg, hdr_end, &prev, next);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003566 next = prev;
3567 hdr_end += delta;
3568 }
3569 txn->flags &= ~TX_SCK_MASK;
3570 txn->flags |= TX_SCK_DELETED;
3571 /* and go on with next cookie */
3572 }
3573 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) {
3574 /* replace bytes val_beg->val_end with the cookie name associated
3575 * with this server since we know it.
3576 */
3577 int sliding, delta;
3578
3579 ctx.value = ist2(val_beg, val_end - val_beg);
3580 ctx.lws_before = ctx.lws_after = 0;
3581 http_replace_header_value(htx, &ctx, ist2(srv->cookie, srv->cklen));
3582 delta = srv->cklen - (val_end - val_beg);
3583 sliding = (ctx.value.ptr - val_beg);
3584 hdr_beg += sliding;
3585 val_beg += sliding;
3586 next += sliding + delta;
3587 hdr_end += sliding + delta;
3588
3589 txn->flags &= ~TX_SCK_MASK;
3590 txn->flags |= TX_SCK_REPLACED;
3591 }
3592 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) {
3593 /* insert the cookie name associated with this server
3594 * before existing cookie, and insert a delimiter between them..
3595 */
3596 int sliding, delta;
3597 ctx.value = ist2(val_beg, 0);
3598 ctx.lws_before = ctx.lws_after = 0;
3599 http_replace_header_value(htx, &ctx, ist2(srv->cookie, srv->cklen + 1));
3600 delta = srv->cklen + 1;
3601 sliding = (ctx.value.ptr - val_beg);
3602 hdr_beg += sliding;
3603 val_beg += sliding;
3604 next += sliding + delta;
3605 hdr_end += sliding + delta;
3606
3607 val_beg[srv->cklen] = COOKIE_DELIM;
3608 txn->flags &= ~TX_SCK_MASK;
3609 txn->flags |= TX_SCK_REPLACED;
3610 }
3611 }
3612 /* that's done for this cookie, check the next one on the same
Willy Tarreauaa1909e2022-11-14 18:58:35 +01003613 * line when next != hdr_end (which should normally not happen
3614 * with set-cookie2 support removed).
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003615 */
3616 }
3617 }
3618}
3619
Christopher Faulet25a02f62018-10-24 12:00:25 +02003620/*
3621 * Parses the Cache-Control and Pragma request header fields to determine if
3622 * the request may be served from the cache and/or if it is cacheable. Updates
3623 * s->txn->flags.
3624 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003625void http_check_request_for_cacheability(struct stream *s, struct channel *req)
Christopher Faulet25a02f62018-10-24 12:00:25 +02003626{
3627 struct http_txn *txn = s->txn;
3628 struct htx *htx;
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003629 struct http_hdr_ctx ctx = { .blk = NULL };
3630 int pragma_found, cc_found;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003631
3632 if ((txn->flags & (TX_CACHEABLE|TX_CACHE_IGNORE)) == TX_CACHE_IGNORE)
3633 return; /* nothing more to do here */
3634
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003635 htx = htxbuf(&req->buf);
Christopher Faulet25a02f62018-10-24 12:00:25 +02003636 pragma_found = cc_found = 0;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003637
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003638 /* Check "pragma" header for HTTP/1.0 compatibility. */
3639 if (http_find_header(htx, ist("pragma"), &ctx, 1)) {
3640 if (isteqi(ctx.value, ist("no-cache"))) {
3641 pragma_found = 1;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003642 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003643 }
Christopher Faulet25a02f62018-10-24 12:00:25 +02003644
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003645 ctx.blk = NULL;
3646 /* Don't use the cache and don't try to store if we found the
3647 * Authorization header */
3648 if (http_find_header(htx, ist("authorization"), &ctx, 1)) {
3649 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3650 txn->flags |= TX_CACHE_IGNORE;
3651 }
Christopher Faulet25a02f62018-10-24 12:00:25 +02003652
Christopher Faulet25a02f62018-10-24 12:00:25 +02003653
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003654 /* Look for "cache-control" header and iterate over all the values
3655 * until we find one that specifies that caching is possible or not. */
3656 ctx.blk = NULL;
3657 while (http_find_header(htx, ist("cache-control"), &ctx, 0)) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003658 cc_found = 1;
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003659 /* We don't check the values after max-age, max-stale nor min-fresh,
3660 * we simply don't use the cache when they're specified. */
3661 if (istmatchi(ctx.value, ist("max-age")) ||
3662 istmatchi(ctx.value, ist("no-cache")) ||
3663 istmatchi(ctx.value, ist("max-stale")) ||
3664 istmatchi(ctx.value, ist("min-fresh"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003665 txn->flags |= TX_CACHE_IGNORE;
3666 continue;
3667 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003668 if (istmatchi(ctx.value, ist("no-store"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003669 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3670 continue;
3671 }
3672 }
3673
3674 /* RFC7234#5.4:
3675 * When the Cache-Control header field is also present and
3676 * understood in a request, Pragma is ignored.
3677 * When the Cache-Control header field is not present in a
3678 * request, caches MUST consider the no-cache request
3679 * pragma-directive as having the same effect as if
3680 * "Cache-Control: no-cache" were present.
3681 */
3682 if (!cc_found && pragma_found)
3683 txn->flags |= TX_CACHE_IGNORE;
3684}
3685
3686/*
3687 * Check if response is cacheable or not. Updates s->txn->flags.
3688 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003689void http_check_response_for_cacheability(struct stream *s, struct channel *res)
Christopher Faulet25a02f62018-10-24 12:00:25 +02003690{
3691 struct http_txn *txn = s->txn;
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003692 struct http_hdr_ctx ctx = { .blk = NULL };
Christopher Faulet25a02f62018-10-24 12:00:25 +02003693 struct htx *htx;
Remi Tricot-Le Bretoncc9bf2e2020-11-12 11:14:41 +01003694 int has_freshness_info = 0;
3695 int has_validator = 0;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003696
3697 if (txn->status < 200) {
3698 /* do not try to cache interim responses! */
3699 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3700 return;
3701 }
3702
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003703 htx = htxbuf(&res->buf);
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003704 /* Check "pragma" header for HTTP/1.0 compatibility. */
3705 if (http_find_header(htx, ist("pragma"), &ctx, 1)) {
3706 if (isteqi(ctx.value, ist("no-cache"))) {
3707 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3708 return;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003709 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003710 }
Christopher Faulet25a02f62018-10-24 12:00:25 +02003711
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003712 /* Look for "cache-control" header and iterate over all the values
3713 * until we find one that specifies that caching is possible or not. */
3714 ctx.blk = NULL;
3715 while (http_find_header(htx, ist("cache-control"), &ctx, 0)) {
3716 if (isteqi(ctx.value, ist("public"))) {
3717 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003718 continue;
3719 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003720 if (isteqi(ctx.value, ist("private")) ||
3721 isteqi(ctx.value, ist("no-cache")) ||
3722 isteqi(ctx.value, ist("no-store")) ||
3723 isteqi(ctx.value, ist("max-age=0")) ||
3724 isteqi(ctx.value, ist("s-maxage=0"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003725 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003726 continue;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003727 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003728 /* We might have a no-cache="set-cookie" form. */
3729 if (istmatchi(ctx.value, ist("no-cache=\"set-cookie"))) {
3730 txn->flags &= ~TX_CACHE_COOK;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003731 continue;
3732 }
Remi Tricot-Le Bretoncc9bf2e2020-11-12 11:14:41 +01003733
3734 if (istmatchi(ctx.value, ist("s-maxage")) ||
3735 istmatchi(ctx.value, ist("max-age"))) {
3736 has_freshness_info = 1;
3737 continue;
3738 }
3739 }
3740
3741 /* If no freshness information could be found in Cache-Control values,
3742 * look for an Expires header. */
3743 if (!has_freshness_info) {
3744 ctx.blk = NULL;
3745 has_freshness_info = http_find_header(htx, ist("expires"), &ctx, 0);
Christopher Faulet25a02f62018-10-24 12:00:25 +02003746 }
Remi Tricot-Le Bretoncc9bf2e2020-11-12 11:14:41 +01003747
3748 /* If no freshness information could be found in Cache-Control or Expires
3749 * values, look for an explicit validator. */
3750 if (!has_freshness_info) {
3751 ctx.blk = NULL;
3752 has_validator = 1;
3753 if (!http_find_header(htx, ist("etag"), &ctx, 0)) {
3754 ctx.blk = NULL;
3755 if (!http_find_header(htx, ist("last-modified"), &ctx, 0))
3756 has_validator = 0;
3757 }
3758 }
3759
3760 /* We won't store an entry that has neither a cache validator nor an
3761 * explicit expiration time, as suggested in RFC 7234#3. */
3762 if (!has_freshness_info && !has_validator)
3763 txn->flags |= TX_CACHE_IGNORE;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003764}
3765
Christopher Faulet377c5a52018-10-24 21:21:30 +02003766/*
3767 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
3768 * for the current backend.
3769 *
3770 * It is assumed that the request is either a HEAD, GET, or POST and that the
3771 * uri_auth field is valid.
3772 *
3773 * Returns 1 if stats should be provided, otherwise 0.
3774 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003775static int http_stats_check_uri(struct stream *s, struct http_txn *txn, struct proxy *backend)
Christopher Faulet377c5a52018-10-24 21:21:30 +02003776{
3777 struct uri_auth *uri_auth = backend->uri_auth;
3778 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003779 struct htx_sl *sl;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003780 struct ist uri;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003781
3782 if (!uri_auth)
3783 return 0;
3784
3785 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
3786 return 0;
3787
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003788 htx = htxbuf(&s->req.buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02003789 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003790 uri = htx_sl_req_uri(sl);
Amaury Denoyellec453f952021-07-06 11:40:12 +02003791 if (*uri_auth->uri_prefix == '/') {
3792 struct http_uri_parser parser = http_uri_parser_init(uri);
3793 uri = http_parse_path(&parser);
3794 }
Christopher Faulet377c5a52018-10-24 21:21:30 +02003795
3796 /* check URI size */
3797 if (uri_auth->uri_len > uri.len)
3798 return 0;
3799
3800 if (memcmp(uri.ptr, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
3801 return 0;
3802
3803 return 1;
3804}
3805
3806/* This function prepares an applet to handle the stats. It can deal with the
3807 * "100-continue" expectation, check that admin rules are met for POST requests,
3808 * and program a response message if something was unexpected. It cannot fail
3809 * and always relies on the stats applet to complete the job. It does not touch
3810 * analysers nor counters, which are left to the caller. It does not touch
3811 * s->target which is supposed to already point to the stats applet. The caller
3812 * is expected to have already assigned an appctx to the stream.
3813 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003814static int http_handle_stats(struct stream *s, struct channel *req)
Christopher Faulet377c5a52018-10-24 21:21:30 +02003815{
3816 struct stats_admin_rule *stats_admin_rule;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003817 struct session *sess = s->sess;
3818 struct http_txn *txn = s->txn;
3819 struct http_msg *msg = &txn->req;
3820 struct uri_auth *uri_auth = s->be->uri_auth;
3821 const char *h, *lookup, *end;
Willy Tarreau8e7c6e62022-05-18 17:58:02 +02003822 struct appctx *appctx = __sc_appctx(s->scb);
Willy Tarreau91cefca2022-05-03 17:08:29 +02003823 struct show_stat_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx));
Christopher Faulet377c5a52018-10-24 21:21:30 +02003824 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003825 struct htx_sl *sl;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003826
Willy Tarreau41f88522022-05-03 18:39:27 +02003827 appctx->st1 = 0;
Willy Tarreau6ef16482022-05-06 18:07:53 +02003828 ctx->state = STAT_STATE_INIT;
Willy Tarreau91cefca2022-05-03 17:08:29 +02003829 ctx->st_code = STAT_STATUS_INIT;
3830 ctx->flags |= uri_auth->flags;
3831 ctx->flags |= STAT_FMT_HTML; /* assume HTML mode by default */
Christopher Faulet377c5a52018-10-24 21:21:30 +02003832 if ((msg->flags & HTTP_MSGF_VER_11) && (txn->meth != HTTP_METH_HEAD))
Willy Tarreau91cefca2022-05-03 17:08:29 +02003833 ctx->flags |= STAT_CHUNKED;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003834
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003835 htx = htxbuf(&req->buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02003836 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003837 lookup = HTX_SL_REQ_UPTR(sl) + uri_auth->uri_len;
3838 end = HTX_SL_REQ_UPTR(sl) + HTX_SL_REQ_ULEN(sl);
Christopher Faulet377c5a52018-10-24 21:21:30 +02003839
3840 for (h = lookup; h <= end - 3; h++) {
3841 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02003842 ctx->flags |= STAT_HIDE_DOWN;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003843 break;
3844 }
Amaury Denoyelle91e55ea2021-02-25 14:46:08 +01003845 }
3846
3847 for (h = lookup; h <= end - 9; h++) {
3848 if (memcmp(h, ";no-maint", 9) == 0) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02003849 ctx->flags |= STAT_HIDE_MAINT;
Willy Tarreau3e320362020-10-23 17:28:57 +02003850 break;
3851 }
Christopher Faulet377c5a52018-10-24 21:21:30 +02003852 }
3853
3854 if (uri_auth->refresh) {
3855 for (h = lookup; h <= end - 10; h++) {
3856 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02003857 ctx->flags |= STAT_NO_REFRESH;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003858 break;
3859 }
3860 }
3861 }
3862
3863 for (h = lookup; h <= end - 4; h++) {
3864 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02003865 ctx->flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
Christopher Faulet377c5a52018-10-24 21:21:30 +02003866 break;
3867 }
3868 }
3869
3870 for (h = lookup; h <= end - 6; h++) {
3871 if (memcmp(h, ";typed", 6) == 0) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02003872 ctx->flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
3873 ctx->flags |= STAT_FMT_TYPED;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003874 break;
3875 }
3876 }
3877
Christopher Faulet6338a082019-09-09 15:50:54 +02003878 for (h = lookup; h <= end - 5; h++) {
3879 if (memcmp(h, ";json", 5) == 0) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02003880 ctx->flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
3881 ctx->flags |= STAT_FMT_JSON;
Christopher Faulet6338a082019-09-09 15:50:54 +02003882 break;
3883 }
3884 }
3885
3886 for (h = lookup; h <= end - 12; h++) {
3887 if (memcmp(h, ";json-schema", 12) == 0) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02003888 ctx->flags &= ~STAT_FMT_MASK;
3889 ctx->flags |= STAT_JSON_SCHM;
Christopher Faulet6338a082019-09-09 15:50:54 +02003890 break;
3891 }
3892 }
3893
Christopher Faulet377c5a52018-10-24 21:21:30 +02003894 for (h = lookup; h <= end - 8; h++) {
3895 if (memcmp(h, ";st=", 4) == 0) {
3896 int i;
3897 h += 4;
Willy Tarreau91cefca2022-05-03 17:08:29 +02003898 ctx->st_code = STAT_STATUS_UNKN;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003899 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
3900 if (strncmp(stat_status_codes[i], h, 4) == 0) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02003901 ctx->st_code = i;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003902 break;
3903 }
3904 }
3905 break;
3906 }
3907 }
3908
Willy Tarreau91cefca2022-05-03 17:08:29 +02003909 ctx->scope_str = 0;
3910 ctx->scope_len = 0;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003911 for (h = lookup; h <= end - 8; h++) {
3912 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
3913 int itx = 0;
3914 const char *h2;
3915 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
3916 const char *err;
3917
3918 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
3919 h2 = h;
Willy Tarreau91cefca2022-05-03 17:08:29 +02003920 ctx->scope_str = h2 - HTX_SL_REQ_UPTR(sl);
Christopher Fauleted7a0662019-01-14 11:07:34 +01003921 while (h < end) {
Christopher Faulet377c5a52018-10-24 21:21:30 +02003922 if (*h == ';' || *h == '&' || *h == ' ')
3923 break;
3924 itx++;
3925 h++;
3926 }
3927
3928 if (itx > STAT_SCOPE_TXT_MAXLEN)
3929 itx = STAT_SCOPE_TXT_MAXLEN;
Willy Tarreau91cefca2022-05-03 17:08:29 +02003930 ctx->scope_len = itx;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003931
Willy Tarreau91cefca2022-05-03 17:08:29 +02003932 /* scope_txt = search query, ctx->scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Christopher Faulet377c5a52018-10-24 21:21:30 +02003933 memcpy(scope_txt, h2, itx);
3934 scope_txt[itx] = '\0';
3935 err = invalid_char(scope_txt);
3936 if (err) {
3937 /* bad char in search text => clear scope */
Willy Tarreau91cefca2022-05-03 17:08:29 +02003938 ctx->scope_str = 0;
3939 ctx->scope_len = 0;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003940 }
3941 break;
3942 }
3943 }
3944
3945 /* now check whether we have some admin rules for this request */
3946 list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) {
3947 int ret = 1;
3948
3949 if (stats_admin_rule->cond) {
3950 ret = acl_exec_cond(stats_admin_rule->cond, s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
3951 ret = acl_pass(ret);
3952 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
3953 ret = !ret;
3954 }
3955
3956 if (ret) {
3957 /* no rule, or the rule matches */
Willy Tarreau91cefca2022-05-03 17:08:29 +02003958 ctx->flags |= STAT_ADMIN;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003959 break;
3960 }
3961 }
3962
Christopher Faulet5d45e382019-02-27 15:15:23 +01003963 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
3964 appctx->st0 = STAT_HTTP_HEAD;
3965 else if (txn->meth == HTTP_METH_POST) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02003966 if (ctx->flags & STAT_ADMIN) {
Christopher Faulet377c5a52018-10-24 21:21:30 +02003967 appctx->st0 = STAT_HTTP_POST;
Christopher Fauletbd9e8422019-08-15 22:26:48 +02003968 if (msg->msg_state < HTTP_MSG_DATA)
3969 req->analysers |= AN_REQ_HTTP_BODY;
3970 }
Christopher Faulet377c5a52018-10-24 21:21:30 +02003971 else {
Christopher Faulet5d45e382019-02-27 15:15:23 +01003972 /* POST without admin level */
Willy Tarreau91cefca2022-05-03 17:08:29 +02003973 ctx->flags &= ~STAT_CHUNKED;
3974 ctx->st_code = STAT_STATUS_DENY;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003975 appctx->st0 = STAT_HTTP_LAST;
3976 }
3977 }
3978 else {
Christopher Faulet5d45e382019-02-27 15:15:23 +01003979 /* Unsupported method */
Willy Tarreau91cefca2022-05-03 17:08:29 +02003980 ctx->flags &= ~STAT_CHUNKED;
3981 ctx->st_code = STAT_STATUS_IVAL;
Christopher Faulet5d45e382019-02-27 15:15:23 +01003982 appctx->st0 = STAT_HTTP_LAST;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003983 }
3984
3985 s->task->nice = -32; /* small boost for HTTP statistics */
3986 return 1;
3987}
3988
Christopher Faulet021a8e42021-03-29 10:46:38 +02003989/* This function waits for the message payload at most <time> milliseconds (may
3990 * be set to TICK_ETERNITY). It stops to wait if at least <bytes> bytes of the
3991 * payload are received (0 means no limit). It returns HTTP_RULE_* depending on
3992 * the result:
3993 *
3994 * - HTTP_RULE_RES_CONT when conditions are met to stop waiting
3995 * - HTTP_RULE_RES_YIELD to wait for more data
Ilya Shipitsinb2be9a12021-04-24 13:25:42 +05003996 * - HTTP_RULE_RES_ABRT when a timeout occurred.
Christopher Faulet021a8e42021-03-29 10:46:38 +02003997 * - HTTP_RULE_RES_BADREQ if a parsing error is raised by lower level
Ilya Shipitsinb2be9a12021-04-24 13:25:42 +05003998 * - HTTP_RULE_RES_ERROR if an internal error occurred
Christopher Faulet021a8e42021-03-29 10:46:38 +02003999 *
Ilya Shipitsinb2be9a12021-04-24 13:25:42 +05004000 * If a timeout occurred, this function is responsible to emit the right response
Christopher Faulet021a8e42021-03-29 10:46:38 +02004001 * to the client, depending on the channel (408 on request side, 504 on response
4002 * side). All other errors must be handled by the caller.
4003 */
4004enum rule_result http_wait_for_msg_body(struct stream *s, struct channel *chn,
4005 unsigned int time, unsigned int bytes)
4006{
4007 struct session *sess = s->sess;
4008 struct http_txn *txn = s->txn;
4009 struct http_msg *msg = ((chn->flags & CF_ISRESP) ? &txn->rsp : &txn->req);
4010 struct htx *htx;
4011 enum rule_result ret = HTTP_RULE_RES_CONT;
4012
4013 htx = htxbuf(&chn->buf);
4014
4015 if (htx->flags & HTX_FL_PARSING_ERROR) {
4016 ret = HTTP_RULE_RES_BADREQ;
4017 goto end;
4018 }
4019 if (htx->flags & HTX_FL_PROCESSING_ERROR) {
4020 ret = HTTP_RULE_RES_ERROR;
4021 goto end;
4022 }
4023
4024 /* Do nothing for bodyless and CONNECT requests */
4025 if (txn->meth == HTTP_METH_CONNECT || (msg->flags & HTTP_MSGF_BODYLESS))
4026 goto end;
4027
4028 if (!(chn->flags & CF_ISRESP) && msg->msg_state < HTTP_MSG_DATA) {
4029 if (http_handle_expect_hdr(s, htx, msg) == -1) {
4030 ret = HTTP_RULE_RES_ERROR;
4031 goto end;
4032 }
4033 }
4034
4035 msg->msg_state = HTTP_MSG_DATA;
4036
4037 /* Now we're in HTTP_MSG_DATA. We just need to know if all data have
4038 * been received or if the buffer is full.
4039 */
Christopher Faulet78335962021-09-23 14:46:32 +02004040 if ((htx->flags & HTX_FL_EOM) ||
4041 htx_get_tail_type(htx) > HTX_BLK_DATA ||
4042 channel_htx_full(chn, htx, global.tune.maxrewrite) ||
Willy Tarreau99615ed2022-05-25 07:29:36 +02004043 sc_waiting_room(chn_prod(chn)))
Christopher Faulet021a8e42021-03-29 10:46:38 +02004044 goto end;
4045
4046 if (bytes) {
4047 struct htx_blk *blk;
4048 unsigned int len = 0;
4049
4050 for (blk = htx_get_first_blk(htx); blk; blk = htx_get_next_blk(htx, blk)) {
4051 if (htx_get_blk_type(blk) != HTX_BLK_DATA)
4052 continue;
4053 len += htx_get_blksz(blk);
4054 if (len >= bytes)
4055 goto end;
4056 }
4057 }
4058
4059 if ((chn->flags & CF_READ_TIMEOUT) || tick_is_expired(chn->analyse_exp, now_ms)) {
4060 if (!(chn->flags & CF_ISRESP))
4061 goto abort_req;
4062 goto abort_res;
4063 }
4064
4065 /* we get here if we need to wait for more data */
Christopher Faulet4b490b72023-01-04 11:55:15 +01004066 if (!(chn->flags & CF_SHUTR)) {
Christopher Faulet021a8e42021-03-29 10:46:38 +02004067 if (!tick_isset(chn->analyse_exp))
4068 chn->analyse_exp = tick_add_ifset(now_ms, time);
4069 ret = HTTP_RULE_RES_YIELD;
4070 }
4071
4072 end:
4073 return ret;
4074
Christopher Fauletf0d80df2023-01-13 10:20:20 +01004075 abort:
4076 http_reply_and_close(s, txn->status, http_error_message(s));
4077 ret = HTTP_RULE_RES_ABRT;
4078 goto end;
4079
Christopher Faulet021a8e42021-03-29 10:46:38 +02004080 abort_req:
4081 txn->status = 408;
4082 if (!(s->flags & SF_ERR_MASK))
4083 s->flags |= SF_ERR_CLITO;
Willy Tarreau4781b152021-04-06 13:53:36 +02004084 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req);
Christopher Faulet021a8e42021-03-29 10:46:38 +02004085 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02004086 _HA_ATOMIC_INC(&sess->listener->counters->failed_req);
Christopher Fauletf0d80df2023-01-13 10:20:20 +01004087 goto abort;
Christopher Faulet021a8e42021-03-29 10:46:38 +02004088
4089 abort_res:
4090 txn->status = 504;
4091 if (!(s->flags & SF_ERR_MASK))
4092 s->flags |= SF_ERR_SRVTO;
Christopher Faulet021a8e42021-03-29 10:46:38 +02004093 stream_inc_http_fail_ctr(s);
Christopher Fauletf0d80df2023-01-13 10:20:20 +01004094 goto abort;
Christopher Faulet021a8e42021-03-29 10:46:38 +02004095}
4096
Willy Tarreaub49672d2022-05-27 10:13:37 +02004097void http_perform_server_redirect(struct stream *s, struct stconn *sc)
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004098{
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004099 struct channel *req = &s->req;
4100 struct channel *res = &s->res;
4101 struct server *srv;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004102 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004103 struct htx_sl *sl;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004104 struct ist path, location;
4105 unsigned int flags;
Amaury Denoyellec453f952021-07-06 11:40:12 +02004106 struct http_uri_parser parser;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004107
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004108 /*
4109 * Create the location
4110 */
4111 chunk_reset(&trash);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004112
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004113 /* 1: add the server's prefix */
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004114 /* special prefix "/" means don't change URL */
4115 srv = __objt_server(s->target);
4116 if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') {
4117 if (!chunk_memcat(&trash, srv->rdr_pfx, srv->rdr_len))
4118 return;
4119 }
4120
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004121 /* 2: add the request Path */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004122 htx = htxbuf(&req->buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02004123 sl = http_get_stline(htx);
Amaury Denoyellec453f952021-07-06 11:40:12 +02004124 parser = http_uri_parser_init(htx_sl_req_uri(sl));
4125 path = http_parse_path(&parser);
Tim Duesterhused526372020-03-05 17:56:33 +01004126 if (!isttest(path))
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004127 return;
4128
4129 if (!chunk_memcat(&trash, path.ptr, path.len))
4130 return;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004131 location = ist2(trash.area, trash.data);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004132
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004133 /*
Ilya Shipitsin4a689da2022-10-29 09:34:32 +05004134 * Create the 302 response
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004135 */
4136 htx = htx_from_buf(&res->buf);
4137 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
4138 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
4139 ist("HTTP/1.1"), ist("302"), ist("Found"));
4140 if (!sl)
4141 goto fail;
4142 sl->info.res.status = 302;
4143 s->txn->status = 302;
4144
4145 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) ||
4146 !htx_add_header(htx, ist("Connection"), ist("close")) ||
4147 !htx_add_header(htx, ist("Content-length"), ist("0")) ||
4148 !htx_add_header(htx, ist("Location"), location))
4149 goto fail;
4150
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004151 if (!htx_add_endof(htx, HTX_BLK_EOH))
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004152 goto fail;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004153
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004154 htx->flags |= HTX_FL_EOM;
Christopher Fauletc20afb82020-01-24 19:16:26 +01004155 htx_to_buf(htx, &res->buf);
Christopher Fauleta72a7e42020-01-28 09:28:11 +01004156 if (!http_forward_proxy_resp(s, 1))
4157 goto fail;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004158
4159 /* return without error. */
Willy Tarreaub49672d2022-05-27 10:13:37 +02004160 sc_shutr(sc);
4161 sc_shutw(sc);
Christopher Faulet50264b42022-03-30 19:39:30 +02004162 s->conn_err_type = STRM_ET_NONE;
Willy Tarreaub49672d2022-05-27 10:13:37 +02004163 sc->state = SC_ST_CLO;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004164
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004165 if (!(s->flags & SF_ERR_MASK))
4166 s->flags |= SF_ERR_LOCAL;
4167 if (!(s->flags & SF_FINST_MASK))
4168 s->flags |= SF_FINST_C;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004169
4170 /* FIXME: we should increase a counter of redirects per server and per backend. */
4171 srv_inc_sess_ctr(srv);
4172 srv_set_sess_last(srv);
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004173 return;
4174
4175 fail:
4176 /* If an error occurred, remove the incomplete HTTP response from the
4177 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004178 channel_htx_truncate(res, htx);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004179}
4180
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05004181/* This function terminates the request because it was completely analyzed or
Christopher Fauletf2824e62018-10-01 12:12:37 +02004182 * because an error was triggered during the body forwarding.
4183 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004184static void http_end_request(struct stream *s)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004185{
4186 struct channel *chn = &s->req;
4187 struct http_txn *txn = s->txn;
4188
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004189 DBG_TRACE_ENTER(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004190
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004191 if (unlikely(txn->req.msg_state < HTTP_MSG_DONE)) {
4192 DBG_TRACE_DEVEL("waiting end of the request", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004193 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004194 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004195
4196 if (txn->req.msg_state == HTTP_MSG_DONE) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004197 /* No need to read anymore, the request was completely parsed.
4198 * We can shut the read side unless we want to abort_on_close,
4199 * or we have a POST request. The issue with POST requests is
4200 * that some browsers still send a CRLF after the request, and
4201 * this CRLF must be read so that it does not remain in the kernel
4202 * buffers, otherwise a close could cause an RST on some systems
4203 * (eg: Linux).
4204 */
Christopher Faulet769d0e92019-03-22 14:23:18 +01004205 if (!(s->be->options & PR_O_ABRT_CLOSE) && txn->meth != HTTP_METH_POST)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004206 channel_dont_read(chn);
4207
4208 /* if the server closes the connection, we want to immediately react
4209 * and close the socket to save packets and syscalls.
4210 */
Willy Tarreaucb041662022-05-17 19:44:42 +02004211 s->scb->flags |= SC_FL_NOHALF;
Christopher Fauletf2824e62018-10-01 12:12:37 +02004212
4213 /* In any case we've finished parsing the request so we must
4214 * disable Nagle when sending data because 1) we're not going
4215 * to shut this side, and 2) the server is waiting for us to
4216 * send pending data.
4217 */
4218 chn->flags |= CF_NEVER_WAIT;
4219
Christopher Fauletd01ce402019-01-02 17:44:13 +01004220 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
4221 /* The server has not finished to respond, so we
4222 * don't want to move in order not to upset it.
4223 */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004224 DBG_TRACE_DEVEL("waiting end of the response", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletd01ce402019-01-02 17:44:13 +01004225 return;
4226 }
4227
Christopher Fauletf2824e62018-10-01 12:12:37 +02004228 /* When we get here, it means that both the request and the
4229 * response have finished receiving. Depending on the connection
4230 * mode, we'll have to wait for the last bytes to leave in either
4231 * direction, and sometimes for a close to be effective.
4232 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02004233 if (txn->flags & TX_CON_WANT_TUN) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004234 /* Tunnel mode will not have any analyser so it needs to
4235 * poll for reads.
4236 */
4237 channel_auto_read(chn);
4238 txn->req.msg_state = HTTP_MSG_TUNNEL;
4239 }
4240 else {
4241 /* we're not expecting any new data to come for this
4242 * transaction, so we can close it.
Christopher Faulet9768c262018-10-22 09:34:31 +02004243 *
4244 * However, there is an exception if the response
4245 * length is undefined. In this case, we need to wait
4246 * the close from the server. The response will be
4247 * switched in TUNNEL mode until the end.
Christopher Fauletf2824e62018-10-01 12:12:37 +02004248 */
4249 if (!(txn->rsp.flags & HTTP_MSGF_XFER_LEN) &&
4250 txn->rsp.msg_state != HTTP_MSG_CLOSED)
Christopher Faulet9768c262018-10-22 09:34:31 +02004251 goto check_channel_flags;
Christopher Fauletf2824e62018-10-01 12:12:37 +02004252
4253 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4254 channel_shutr_now(chn);
4255 channel_shutw_now(chn);
4256 }
4257 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004258 goto check_channel_flags;
4259 }
4260
4261 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
4262 http_msg_closing:
4263 /* nothing else to forward, just waiting for the output buffer
4264 * to be empty and for the shutw_now to take effect.
4265 */
4266 if (channel_is_empty(chn)) {
4267 txn->req.msg_state = HTTP_MSG_CLOSED;
4268 goto http_msg_closed;
4269 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004270 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004271 return;
4272 }
4273
4274 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
4275 http_msg_closed:
Christopher Fauletf2824e62018-10-01 12:12:37 +02004276 /* if we don't know whether the server will close, we need to hard close */
4277 if (txn->rsp.flags & HTTP_MSGF_XFER_LEN)
Willy Tarreaucb041662022-05-17 19:44:42 +02004278 s->scb->flags |= SC_FL_NOLINGER; /* we want to close ASAP */
Christopher Fauletf2824e62018-10-01 12:12:37 +02004279 /* see above in MSG_DONE why we only do this in these states */
Christopher Faulet769d0e92019-03-22 14:23:18 +01004280 if (!(s->be->options & PR_O_ABRT_CLOSE))
Christopher Fauletf2824e62018-10-01 12:12:37 +02004281 channel_dont_read(chn);
4282 goto end;
4283 }
4284
4285 check_channel_flags:
4286 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4287 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4288 /* if we've just closed an output, let's switch */
4289 txn->req.msg_state = HTTP_MSG_CLOSING;
4290 goto http_msg_closing;
4291 }
4292
4293 end:
4294 chn->analysers &= AN_REQ_FLT_END;
Christopher Faulet198ef8b2020-12-15 13:32:55 +01004295 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
4296 chn->flags |= CF_NEVER_WAIT;
4297 if (HAS_REQ_DATA_FILTERS(s))
Christopher Fauletf2824e62018-10-01 12:12:37 +02004298 chn->analysers |= AN_REQ_FLT_XFER_DATA;
Christopher Faulet198ef8b2020-12-15 13:32:55 +01004299 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004300 channel_auto_close(chn);
4301 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004302 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004303}
4304
4305
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05004306/* This function terminates the response because it was completely analyzed or
Christopher Fauletf2824e62018-10-01 12:12:37 +02004307 * because an error was triggered during the body forwarding.
4308 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004309static void http_end_response(struct stream *s)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004310{
4311 struct channel *chn = &s->res;
4312 struct http_txn *txn = s->txn;
4313
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004314 DBG_TRACE_ENTER(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004315
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004316 if (unlikely(txn->rsp.msg_state < HTTP_MSG_DONE)) {
4317 DBG_TRACE_DEVEL("waiting end of the response", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004318 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004319 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004320
4321 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
4322 /* In theory, we don't need to read anymore, but we must
4323 * still monitor the server connection for a possible close
4324 * while the request is being uploaded, so we don't disable
4325 * reading.
4326 */
4327 /* channel_dont_read(chn); */
4328
4329 if (txn->req.msg_state < HTTP_MSG_DONE) {
4330 /* The client seems to still be sending data, probably
4331 * because we got an error response during an upload.
4332 * We have the choice of either breaking the connection
4333 * or letting it pass through. Let's do the later.
4334 */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004335 DBG_TRACE_DEVEL("waiting end of the request", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004336 return;
4337 }
4338
4339 /* When we get here, it means that both the request and the
4340 * response have finished receiving. Depending on the connection
4341 * mode, we'll have to wait for the last bytes to leave in either
4342 * direction, and sometimes for a close to be effective.
4343 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02004344 if (txn->flags & TX_CON_WANT_TUN) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004345 channel_auto_read(chn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004346 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
4347 }
4348 else {
4349 /* we're not expecting any new data to come for this
4350 * transaction, so we can close it.
4351 */
4352 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4353 channel_shutr_now(chn);
4354 channel_shutw_now(chn);
4355 }
4356 }
4357 goto check_channel_flags;
4358 }
4359
4360 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
4361 http_msg_closing:
4362 /* nothing else to forward, just waiting for the output buffer
4363 * to be empty and for the shutw_now to take effect.
4364 */
4365 if (channel_is_empty(chn)) {
4366 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4367 goto http_msg_closed;
4368 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004369 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004370 return;
4371 }
4372
4373 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
4374 http_msg_closed:
4375 /* drop any pending data */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004376 channel_htx_truncate(&s->req, htxbuf(&s->req.buf));
Christopher Faulet9768c262018-10-22 09:34:31 +02004377 channel_abort(&s->req);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004378 goto end;
4379 }
4380
4381 check_channel_flags:
4382 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4383 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4384 /* if we've just closed an output, let's switch */
4385 txn->rsp.msg_state = HTTP_MSG_CLOSING;
4386 goto http_msg_closing;
4387 }
4388
4389 end:
4390 chn->analysers &= AN_RES_FLT_END;
Christopher Faulet198ef8b2020-12-15 13:32:55 +01004391 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
4392 chn->flags |= CF_NEVER_WAIT;
4393 if (HAS_RSP_DATA_FILTERS(s))
4394 chn->analysers |= AN_RES_FLT_XFER_DATA;
4395 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004396 channel_auto_close(chn);
4397 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004398 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004399}
4400
Christopher Fauletef70e252020-01-28 09:26:19 +01004401/* Forward a response generated by HAProxy (error/redirect/return). This
4402 * function forwards all pending incoming data. If <final> is set to 0, nothing
4403 * more is performed. It is used for 1xx informational messages. Otherwise, the
Christopher Faulet507479b2020-05-15 12:29:46 +02004404 * transaction is terminated and the request is emptied. On success 1 is
Christopher Faulet40e6b552020-06-25 16:04:50 +02004405 * returned. If an error occurred, 0 is returned. If it fails, this function
4406 * only exits. It is the caller responsibility to do the cleanup.
Christopher Fauletef70e252020-01-28 09:26:19 +01004407 */
4408int http_forward_proxy_resp(struct stream *s, int final)
4409{
4410 struct channel *req = &s->req;
4411 struct channel *res = &s->res;
4412 struct htx *htx = htxbuf(&res->buf);
4413 size_t data;
4414
4415 if (final) {
4416 htx->flags |= HTX_FL_PROXY_RESP;
Christopher Faulet507479b2020-05-15 12:29:46 +02004417
Christopher Fauletaab1b672020-11-18 16:44:02 +01004418 if (!htx_is_empty(htx) && !http_eval_after_res_rules(s))
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01004419 return 0;
Christopher Fauletef70e252020-01-28 09:26:19 +01004420
Christopher Fauletd6c48362020-10-19 18:01:38 +02004421 if (s->txn->meth == HTTP_METH_HEAD)
4422 htx_skip_msg_payload(htx);
4423
Christopher Fauletef70e252020-01-28 09:26:19 +01004424 channel_auto_read(req);
4425 channel_abort(req);
4426 channel_auto_close(req);
4427 channel_htx_erase(req, htxbuf(&req->buf));
4428
4429 res->wex = tick_add_ifset(now_ms, res->wto);
4430 channel_auto_read(res);
4431 channel_auto_close(res);
4432 channel_shutr_now(res);
Christopher Faulet1a9db7c2020-06-25 15:36:45 +02004433 res->flags |= CF_EOI; /* The response is terminated, add EOI */
Christopher Faulet42432f32020-11-20 17:43:16 +01004434 htxbuf(&res->buf)->flags |= HTX_FL_EOM; /* no more data are expected */
Christopher Fauletef70e252020-01-28 09:26:19 +01004435 }
Christopher Fauletcf6898c2020-06-25 15:55:11 +02004436 else {
4437 /* Send ASAP informational messages. Rely on CF_EOI for final
4438 * response.
4439 */
4440 res->flags |= CF_SEND_DONTWAIT;
4441 }
Christopher Fauletef70e252020-01-28 09:26:19 +01004442
4443 data = htx->data - co_data(res);
4444 c_adv(res, data);
4445 htx->first = -1;
4446 res->total += data;
4447 return 1;
4448}
4449
Willy Tarreaub49672d2022-05-27 10:13:37 +02004450void http_server_error(struct stream *s, struct stconn *sc, int err,
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004451 int finst, struct http_reply *msg)
Christopher Faulet0f226952018-10-22 09:29:56 +02004452{
Christopher Faulet72c7d8d2020-01-27 15:32:25 +01004453 http_reply_and_close(s, s->txn->status, msg);
Christopher Faulet0f226952018-10-22 09:29:56 +02004454 if (!(s->flags & SF_ERR_MASK))
4455 s->flags |= err;
4456 if (!(s->flags & SF_FINST_MASK))
4457 s->flags |= finst;
4458}
4459
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004460void http_reply_and_close(struct stream *s, short status, struct http_reply *msg)
Christopher Faulet0f226952018-10-22 09:29:56 +02004461{
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004462 if (!msg) {
4463 channel_htx_truncate(&s->res, htxbuf(&s->res.buf));
4464 goto end;
4465 }
4466
4467 if (http_reply_message(s, msg) == -1) {
4468 /* On error, return a 500 error message, but don't rewrite it if
Christopher Faulet40e6b552020-06-25 16:04:50 +02004469 * it is already an internal error. If it was already a "const"
4470 * 500 error, just fail.
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004471 */
Christopher Faulet40e6b552020-06-25 16:04:50 +02004472 if (s->txn->status == 500) {
4473 if (s->txn->flags & TX_CONST_REPLY)
4474 goto end;
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004475 s->txn->flags |= TX_CONST_REPLY;
Christopher Faulet40e6b552020-06-25 16:04:50 +02004476 }
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004477 s->txn->status = 500;
4478 s->txn->http_reply = NULL;
4479 return http_reply_and_close(s, s->txn->status, http_error_message(s));
4480 }
4481
4482end:
4483 s->res.wex = tick_add_ifset(now_ms, s->res.wto);
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004484
Christopher Faulet2d565002021-09-10 09:17:50 +02004485 /* At this staged, HTTP analysis is finished */
4486 s->req.analysers &= AN_REQ_FLT_END;
4487 s->req.analyse_exp = TICK_ETERNITY;
4488
4489 s->res.analysers &= AN_RES_FLT_END;
4490 s->res.analyse_exp = TICK_ETERNITY;
4491
Christopher Faulet0f226952018-10-22 09:29:56 +02004492 channel_auto_read(&s->req);
4493 channel_abort(&s->req);
4494 channel_auto_close(&s->req);
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004495 channel_htx_erase(&s->req, htxbuf(&s->req.buf));
Christopher Faulet72c7d8d2020-01-27 15:32:25 +01004496 channel_auto_read(&s->res);
4497 channel_auto_close(&s->res);
4498 channel_shutr_now(&s->res);
Christopher Faulet0f226952018-10-22 09:29:56 +02004499}
4500
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004501struct http_reply *http_error_message(struct stream *s)
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004502{
4503 const int msgnum = http_get_status_idx(s->txn->status);
4504
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004505 if (s->txn->http_reply)
4506 return s->txn->http_reply;
4507 else if (s->be->replies[msgnum])
4508 return s->be->replies[msgnum];
4509 else if (strm_fe(s)->replies[msgnum])
4510 return strm_fe(s)->replies[msgnum];
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004511 else
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004512 return &http_err_replies[msgnum];
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004513}
4514
Christopher Faulet40e6b552020-06-25 16:04:50 +02004515/* Produces an HTX message from an http reply. Depending on the http reply type,
4516 * a, errorfile, an raw file or a log-format string is used. On success, it
4517 * returns 0. If an error occurs -1 is returned. If it fails, this function only
4518 * exits. It is the caller responsibility to do the cleanup.
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004519 */
Christopher Fauletae43b6c2020-05-27 15:24:22 +02004520int http_reply_to_htx(struct stream *s, struct htx *htx, struct http_reply *reply)
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004521{
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004522 struct buffer *errmsg;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004523 struct htx_sl *sl;
4524 struct buffer *body = NULL;
4525 const char *status, *reason, *clen, *ctype;
4526 unsigned int slflags;
4527 int ret = 0;
4528
Christopher Faulete29a97e2020-05-14 14:49:25 +02004529 /*
4530 * - HTTP_REPLY_ERRFILES unexpected here. handled as no payload if so
4531 *
4532 * - HTTP_REPLY_INDIRECT: switch on another reply if defined or handled
4533 * as no payload if NULL. the TXN status code is set with the status
4534 * of the original reply.
4535 */
4536
4537 if (reply->type == HTTP_REPLY_INDIRECT) {
4538 if (reply->body.reply)
4539 reply = reply->body.reply;
4540 }
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004541 if (reply->type == HTTP_REPLY_ERRMSG && !reply->body.errmsg) {
4542 /* get default error message */
4543 if (reply == s->txn->http_reply)
4544 s->txn->http_reply = NULL;
4545 reply = http_error_message(s);
4546 if (reply->type == HTTP_REPLY_INDIRECT) {
4547 if (reply->body.reply)
4548 reply = reply->body.reply;
4549 }
4550 }
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004551
4552 if (reply->type == HTTP_REPLY_ERRMSG) {
4553 /* implicit or explicit error message*/
4554 errmsg = reply->body.errmsg;
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004555 if (errmsg && !b_is_null(errmsg)) {
Christopher Faulet20567362020-05-15 14:52:49 +02004556 if (!htx_copy_msg(htx, errmsg))
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004557 goto fail;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004558 }
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004559 }
4560 else {
4561 /* no payload, file or log-format string */
4562 if (reply->type == HTTP_REPLY_RAW) {
4563 /* file */
4564 body = &reply->body.obj;
4565 }
4566 else if (reply->type == HTTP_REPLY_LOGFMT) {
4567 /* log-format string */
4568 body = alloc_trash_chunk();
4569 if (!body)
4570 goto fail_alloc;
4571 body->data = build_logline(s, body->area, body->size, &reply->body.fmt);
4572 }
4573 /* else no payload */
4574
4575 status = ultoa(reply->status);
4576 reason = http_get_reason(reply->status);
4577 slflags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_CLEN);
4578 if (!body || !b_data(body))
4579 slflags |= HTX_SL_F_BODYLESS;
4580 sl = htx_add_stline(htx, HTX_BLK_RES_SL, slflags, ist("HTTP/1.1"), ist(status), ist(reason));
4581 if (!sl)
4582 goto fail;
4583 sl->info.res.status = reply->status;
4584
4585 clen = (body ? ultoa(b_data(body)) : "0");
4586 ctype = reply->ctype;
4587
4588 if (!LIST_ISEMPTY(&reply->hdrs)) {
4589 struct http_reply_hdr *hdr;
4590 struct buffer *value = alloc_trash_chunk();
4591
4592 if (!value)
4593 goto fail;
4594
4595 list_for_each_entry(hdr, &reply->hdrs, list) {
4596 chunk_reset(value);
4597 value->data = build_logline(s, value->area, value->size, &hdr->value);
4598 if (b_data(value) && !htx_add_header(htx, hdr->name, ist2(b_head(value), b_data(value)))) {
4599 free_trash_chunk(value);
4600 goto fail;
4601 }
4602 chunk_reset(value);
4603 }
4604 free_trash_chunk(value);
4605 }
4606
4607 if (!htx_add_header(htx, ist("content-length"), ist(clen)) ||
4608 (body && b_data(body) && ctype && !htx_add_header(htx, ist("content-type"), ist(ctype))) ||
4609 !htx_add_endof(htx, HTX_BLK_EOH) ||
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004610 (body && b_data(body) && !htx_add_data_atonce(htx, ist2(b_head(body), b_data(body)))))
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004611 goto fail;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004612
4613 htx->flags |= HTX_FL_EOM;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004614 }
4615
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004616 leave:
4617 if (reply->type == HTTP_REPLY_LOGFMT)
4618 free_trash_chunk(body);
4619 return ret;
4620
4621 fail_alloc:
4622 if (!(s->flags & SF_ERR_MASK))
4623 s->flags |= SF_ERR_RESOURCE;
Christopher Faulet97e466c2020-05-15 15:12:47 +02004624 /* fall through */
4625 fail:
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004626 ret = -1;
4627 goto leave;
Christopher Faulet97e466c2020-05-15 15:12:47 +02004628}
4629
4630/* Send an http reply to the client. On success, it returns 0. If an error
Christopher Faulet40e6b552020-06-25 16:04:50 +02004631 * occurs -1 is returned and the response channel is truncated, removing this
4632 * way the faulty reply. This function may fail when the reply is formatted
4633 * (http_reply_to_htx) or when the reply is forwarded
4634 * (http_forward_proxy_resp). On the last case, it is because a
4635 * http-after-response rule fails.
Christopher Faulet97e466c2020-05-15 15:12:47 +02004636 */
4637int http_reply_message(struct stream *s, struct http_reply *reply)
4638{
4639 struct channel *res = &s->res;
4640 struct htx *htx = htx_from_buf(&res->buf);
4641
4642 if (s->txn->status == -1)
4643 s->txn->status = reply->status;
4644 channel_htx_truncate(res, htx);
4645
4646 if (http_reply_to_htx(s, htx, reply) == -1)
4647 goto fail;
4648
4649 htx_to_buf(htx, &s->res.buf);
4650 if (!http_forward_proxy_resp(s, 1))
4651 goto fail;
4652 return 0;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004653
4654 fail:
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004655 channel_htx_truncate(res, htx);
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004656 if (!(s->flags & SF_ERR_MASK))
4657 s->flags |= SF_ERR_PRXCOND;
Christopher Faulet97e466c2020-05-15 15:12:47 +02004658 return -1;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004659}
4660
Christopher Faulet50264b42022-03-30 19:39:30 +02004661/* Return the error message corresponding to s->conn_err_type. It is assumed
Christopher Faulet304cc402019-07-15 15:46:28 +02004662 * that the server side is closed. Note that err_type is actually a
4663 * bitmask, where almost only aborts may be cumulated with other
4664 * values. We consider that aborted operations are more important
4665 * than timeouts or errors due to the fact that nobody else in the
4666 * logs might explain incomplete retries. All others should avoid
4667 * being cumulated. It should normally not be possible to have multiple
4668 * aborts at once, but just in case, the first one in sequence is reported.
4669 * Note that connection errors appearing on the second request of a keep-alive
4670 * connection are not reported since this allows the client to retry.
4671 */
Willy Tarreaub49672d2022-05-27 10:13:37 +02004672void http_return_srv_error(struct stream *s, struct stconn *sc)
Christopher Faulet304cc402019-07-15 15:46:28 +02004673{
Christopher Faulet50264b42022-03-30 19:39:30 +02004674 int err_type = s->conn_err_type;
Christopher Faulet304cc402019-07-15 15:46:28 +02004675
4676 /* set s->txn->status for http_error_message(s) */
Christopher Faulet50264b42022-03-30 19:39:30 +02004677 if (err_type & STRM_ET_QUEUE_ABRT) {
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004678 s->txn->status = -1;
Willy Tarreaub49672d2022-05-27 10:13:37 +02004679 http_server_error(s, sc, SF_ERR_CLICL, SF_FINST_Q, NULL);
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004680 }
Christopher Faulet50264b42022-03-30 19:39:30 +02004681 else if (err_type & STRM_ET_CONN_ABRT) {
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004682 s->txn->status = -1;
Willy Tarreaub49672d2022-05-27 10:13:37 +02004683 http_server_error(s, sc, SF_ERR_CLICL, SF_FINST_C, NULL);
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004684 }
Christopher Faulet50264b42022-03-30 19:39:30 +02004685 else if (err_type & STRM_ET_QUEUE_TO) {
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004686 s->txn->status = 503;
Willy Tarreaub49672d2022-05-27 10:13:37 +02004687 http_server_error(s, sc, SF_ERR_SRVTO, SF_FINST_Q,
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004688 http_error_message(s));
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004689 }
Christopher Faulet50264b42022-03-30 19:39:30 +02004690 else if (err_type & STRM_ET_QUEUE_ERR) {
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004691 s->txn->status = 503;
Willy Tarreaub49672d2022-05-27 10:13:37 +02004692 http_server_error(s, sc, SF_ERR_SRVCL, SF_FINST_Q,
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004693 http_error_message(s));
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004694 }
Christopher Faulet50264b42022-03-30 19:39:30 +02004695 else if (err_type & STRM_ET_CONN_TO) {
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004696 s->txn->status = 503;
Willy Tarreaub49672d2022-05-27 10:13:37 +02004697 http_server_error(s, sc, SF_ERR_SRVTO, SF_FINST_C,
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004698 (s->txn->flags & TX_NOT_FIRST) ? NULL :
4699 http_error_message(s));
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004700 }
Christopher Faulet50264b42022-03-30 19:39:30 +02004701 else if (err_type & STRM_ET_CONN_ERR) {
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_SRVCL, SF_FINST_C,
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004704 (s->flags & SF_SRV_REUSED) ? NULL :
4705 http_error_message(s));
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004706 }
Christopher Faulet50264b42022-03-30 19:39:30 +02004707 else if (err_type & STRM_ET_CONN_RES) {
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004708 s->txn->status = 503;
Willy Tarreaub49672d2022-05-27 10:13:37 +02004709 http_server_error(s, sc, SF_ERR_RESOURCE, SF_FINST_C,
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004710 (s->txn->flags & TX_NOT_FIRST) ? NULL :
4711 http_error_message(s));
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004712 }
Christopher Faulet50264b42022-03-30 19:39:30 +02004713 else { /* STRM_ET_CONN_OTHER and others */
Christopher Faulet304cc402019-07-15 15:46:28 +02004714 s->txn->status = 500;
Willy Tarreaub49672d2022-05-27 10:13:37 +02004715 http_server_error(s, sc, SF_ERR_INTERNAL, SF_FINST_C,
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004716 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004717 }
4718}
4719
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004720
Christopher Faulet4a28a532019-03-01 11:19:40 +01004721/* Handle Expect: 100-continue for HTTP/1.1 messages if necessary. It returns 0
4722 * on success and -1 on error.
4723 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004724static int http_handle_expect_hdr(struct stream *s, struct htx *htx, struct http_msg *msg)
Christopher Faulet4a28a532019-03-01 11:19:40 +01004725{
4726 /* If we have HTTP/1.1 message with a body and Expect: 100-continue,
4727 * then we must send an HTTP/1.1 100 Continue intermediate response.
4728 */
4729 if (msg->msg_state == HTTP_MSG_BODY && (msg->flags & HTTP_MSGF_VER_11) &&
4730 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
4731 struct ist hdr = { .ptr = "Expect", .len = 6 };
4732 struct http_hdr_ctx ctx;
4733
4734 ctx.blk = NULL;
4735 /* Expect is allowed in 1.1, look for it */
4736 if (http_find_header(htx, hdr, &ctx, 0) &&
4737 unlikely(isteqi(ctx.value, ist2("100-continue", 12)))) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004738 if (http_reply_100_continue(s) == -1)
Christopher Faulet4a28a532019-03-01 11:19:40 +01004739 return -1;
4740 http_remove_header(htx, &ctx);
4741 }
4742 }
4743 return 0;
4744}
4745
Christopher Faulet23a3c792018-11-28 10:01:23 +01004746/* Send a 100-Continue response to the client. It returns 0 on success and -1
4747 * on error. The response channel is updated accordingly.
4748 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004749static int http_reply_100_continue(struct stream *s)
Christopher Faulet23a3c792018-11-28 10:01:23 +01004750{
4751 struct channel *res = &s->res;
4752 struct htx *htx = htx_from_buf(&res->buf);
4753 struct htx_sl *sl;
4754 unsigned int flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|
4755 HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
Christopher Faulet23a3c792018-11-28 10:01:23 +01004756
4757 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
4758 ist("HTTP/1.1"), ist("100"), ist("Continue"));
4759 if (!sl)
4760 goto fail;
4761 sl->info.res.status = 100;
4762
Christopher Faulet1d5ec092019-06-26 14:23:54 +02004763 if (!htx_add_endof(htx, HTX_BLK_EOH))
Christopher Faulet23a3c792018-11-28 10:01:23 +01004764 goto fail;
4765
Christopher Fauleta72a7e42020-01-28 09:28:11 +01004766 if (!http_forward_proxy_resp(s, 0))
4767 goto fail;
Christopher Faulet23a3c792018-11-28 10:01:23 +01004768 return 0;
4769
4770 fail:
4771 /* If an error occurred, remove the incomplete HTTP response from the
4772 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004773 channel_htx_truncate(res, htx);
Christopher Faulet23a3c792018-11-28 10:01:23 +01004774 return -1;
4775}
4776
Christopher Faulet12c51e22018-11-28 15:59:42 +01004777
Christopher Faulet0f226952018-10-22 09:29:56 +02004778/*
4779 * Capture headers from message <htx> according to header list <cap_hdr>, and
4780 * fill the <cap> pointers appropriately.
4781 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004782static void http_capture_headers(struct htx *htx, char **cap, struct cap_hdr *cap_hdr)
Christopher Faulet0f226952018-10-22 09:29:56 +02004783{
4784 struct cap_hdr *h;
4785 int32_t pos;
4786
Christopher Fauleta3f15502019-05-13 15:27:23 +02004787 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet0f226952018-10-22 09:29:56 +02004788 struct htx_blk *blk = htx_get_blk(htx, pos);
4789 enum htx_blk_type type = htx_get_blk_type(blk);
4790 struct ist n, v;
4791
4792 if (type == HTX_BLK_EOH)
4793 break;
4794 if (type != HTX_BLK_HDR)
4795 continue;
4796
4797 n = htx_get_blk_name(htx, blk);
4798
4799 for (h = cap_hdr; h; h = h->next) {
4800 if (h->namelen && (h->namelen == n.len) &&
4801 (strncasecmp(n.ptr, h->name, h->namelen) == 0)) {
4802 if (cap[h->index] == NULL)
4803 cap[h->index] =
4804 pool_alloc(h->pool);
4805
4806 if (cap[h->index] == NULL) {
4807 ha_alert("HTTP capture : out of memory.\n");
4808 break;
4809 }
4810
4811 v = htx_get_blk_value(htx, blk);
Tim Duesterhus2471f5c2021-11-08 09:05:01 +01004812 v = isttrim(v, h->len);
Christopher Faulet0f226952018-10-22 09:29:56 +02004813
4814 memcpy(cap[h->index], v.ptr, v.len);
4815 cap[h->index][v.len]=0;
4816 }
4817 }
4818 }
4819}
4820
Christopher Faulet0b6bdc52018-10-24 11:05:36 +02004821/* Delete a value in a header between delimiters <from> and <next>. The header
4822 * itself is delimited by <start> and <end> pointers. The number of characters
4823 * displaced is returned, and the pointer to the first delimiter is updated if
4824 * required. The function tries as much as possible to respect the following
4825 * principles :
4826 * - replace <from> delimiter by the <next> one unless <from> points to <start>,
4827 * in which case <next> is simply removed
4828 * - set exactly one space character after the new first delimiter, unless there
4829 * are not enough characters in the block being moved to do so.
4830 * - remove unneeded spaces before the previous delimiter and after the new
4831 * one.
4832 *
4833 * It is the caller's responsibility to ensure that :
4834 * - <from> points to a valid delimiter or <start> ;
4835 * - <next> points to a valid delimiter or <end> ;
4836 * - there are non-space chars before <from>.
4837 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004838static int http_del_hdr_value(char *start, char *end, char **from, char *next)
Christopher Faulet0b6bdc52018-10-24 11:05:36 +02004839{
4840 char *prev = *from;
4841
4842 if (prev == start) {
4843 /* We're removing the first value. eat the semicolon, if <next>
4844 * is lower than <end> */
4845 if (next < end)
4846 next++;
4847
4848 while (next < end && HTTP_IS_SPHT(*next))
4849 next++;
4850 }
4851 else {
4852 /* Remove useless spaces before the old delimiter. */
4853 while (HTTP_IS_SPHT(*(prev-1)))
4854 prev--;
4855 *from = prev;
4856
4857 /* copy the delimiter and if possible a space if we're
4858 * not at the end of the line.
4859 */
4860 if (next < end) {
4861 *prev++ = *next++;
4862 if (prev + 1 < next)
4863 *prev++ = ' ';
4864 while (next < end && HTTP_IS_SPHT(*next))
4865 next++;
4866 }
4867 }
4868 memmove(prev, next, end - next);
4869 return (prev - next);
4870}
4871
Christopher Faulet0f226952018-10-22 09:29:56 +02004872
4873/* Formats the start line of the request (without CRLF) and puts it in <str> and
Joseph Herlantc42c0e92018-11-25 10:43:27 -08004874 * return the written length. The line can be truncated if it exceeds <len>.
Christopher Faulet0f226952018-10-22 09:29:56 +02004875 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004876static size_t http_fmt_req_line(const struct htx_sl *sl, char *str, size_t len)
Christopher Faulet0f226952018-10-22 09:29:56 +02004877{
4878 struct ist dst = ist2(str, 0);
4879
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004880 if (istcat(&dst, htx_sl_req_meth(sl), len) == -1)
Christopher Faulet0f226952018-10-22 09:29:56 +02004881 goto end;
4882 if (dst.len + 1 > len)
4883 goto end;
4884 dst.ptr[dst.len++] = ' ';
4885
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004886 if (istcat(&dst, htx_sl_req_uri(sl), len) == -1)
Christopher Faulet0f226952018-10-22 09:29:56 +02004887 goto end;
4888 if (dst.len + 1 > len)
4889 goto end;
4890 dst.ptr[dst.len++] = ' ';
4891
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004892 istcat(&dst, htx_sl_req_vsn(sl), len);
Christopher Faulet0f226952018-10-22 09:29:56 +02004893 end:
4894 return dst.len;
4895}
4896
4897/*
4898 * Print a debug line with a start line.
4899 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004900static void http_debug_stline(const char *dir, struct stream *s, const struct htx_sl *sl)
Christopher Faulet0f226952018-10-22 09:29:56 +02004901{
4902 struct session *sess = strm_sess(s);
4903 int max;
4904
4905 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
4906 dir,
Willy Tarreau88bc8002021-12-06 07:01:02 +00004907 objt_conn(sess->origin) ? (unsigned short)__objt_conn(sess->origin)->handle.fd : -1,
Willy Tarreaufd9417b2022-05-18 16:23:22 +02004908 sc_conn(s->scb) ? (unsigned short)(__sc_conn(s->scb))->handle.fd : -1);
Christopher Faulet0f226952018-10-22 09:29:56 +02004909
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004910 max = HTX_SL_P1_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02004911 UBOUND(max, trash.size - trash.data - 3);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004912 chunk_memcat(&trash, HTX_SL_P1_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02004913 trash.area[trash.data++] = ' ';
4914
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004915 max = HTX_SL_P2_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02004916 UBOUND(max, trash.size - trash.data - 2);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004917 chunk_memcat(&trash, HTX_SL_P2_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02004918 trash.area[trash.data++] = ' ';
4919
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004920 max = HTX_SL_P3_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02004921 UBOUND(max, trash.size - trash.data - 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004922 chunk_memcat(&trash, HTX_SL_P3_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02004923 trash.area[trash.data++] = '\n';
4924
Willy Tarreau2e8ab6b2020-03-14 11:03:20 +01004925 DISGUISE(write(1, trash.area, trash.data));
Christopher Faulet0f226952018-10-22 09:29:56 +02004926}
4927
4928/*
4929 * Print a debug line with a header.
4930 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004931static 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 +02004932{
4933 struct session *sess = strm_sess(s);
4934 int max;
4935
4936 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
4937 dir,
Willy Tarreau88bc8002021-12-06 07:01:02 +00004938 objt_conn(sess->origin) ? (unsigned short)__objt_conn(sess->origin)->handle.fd : -1,
Willy Tarreaufd9417b2022-05-18 16:23:22 +02004939 sc_conn(s->scb) ? (unsigned short)(__sc_conn(s->scb))->handle.fd : -1);
Christopher Faulet0f226952018-10-22 09:29:56 +02004940
4941 max = n.len;
4942 UBOUND(max, trash.size - trash.data - 3);
4943 chunk_memcat(&trash, n.ptr, max);
4944 trash.area[trash.data++] = ':';
4945 trash.area[trash.data++] = ' ';
4946
4947 max = v.len;
4948 UBOUND(max, trash.size - trash.data - 1);
4949 chunk_memcat(&trash, v.ptr, max);
4950 trash.area[trash.data++] = '\n';
4951
Willy Tarreau2e8ab6b2020-03-14 11:03:20 +01004952 DISGUISE(write(1, trash.area, trash.data));
Christopher Faulet0f226952018-10-22 09:29:56 +02004953}
4954
Christopher Fauleta8a46e22019-07-16 14:53:09 +02004955void http_txn_reset_req(struct http_txn *txn)
4956{
Christopher Faulet1aea50e2020-01-17 16:03:53 +01004957 txn->req.flags = 0;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02004958 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
4959}
4960
4961void http_txn_reset_res(struct http_txn *txn)
4962{
Christopher Faulet1aea50e2020-01-17 16:03:53 +01004963 txn->rsp.flags = 0;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02004964 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
4965}
4966
4967/*
Christopher Faulet75f619a2021-03-08 19:12:58 +01004968 * Create and initialize a new HTTP transaction for stream <s>. This should be
4969 * used before processing any new request. It returns the transaction or NLULL
4970 * on error.
Christopher Fauleta8a46e22019-07-16 14:53:09 +02004971 */
Christopher Faulet75f619a2021-03-08 19:12:58 +01004972struct http_txn *http_create_txn(struct stream *s)
Christopher Fauleta8a46e22019-07-16 14:53:09 +02004973{
Christopher Faulet75f619a2021-03-08 19:12:58 +01004974 struct http_txn *txn;
Willy Tarreaub49672d2022-05-27 10:13:37 +02004975 struct stconn *sc = s->scf;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02004976
Christopher Faulet75f619a2021-03-08 19:12:58 +01004977 txn = pool_alloc(pool_head_http_txn);
4978 if (!txn)
4979 return NULL;
4980 s->txn = txn;
4981
Christopher Faulet5eb67f52022-06-22 17:12:05 +02004982 txn->meth = HTTP_METH_OTHER;
Willy Tarreaub49672d2022-05-27 10:13:37 +02004983 txn->flags = ((sc && sc_ep_test(sc, SE_FL_NOT_FIRST)) ? TX_NOT_FIRST : 0);
Christopher Fauleta8a46e22019-07-16 14:53:09 +02004984 txn->status = -1;
Christopher Faulet5cb513a2020-05-13 17:56:56 +02004985 txn->http_reply = NULL;
Christopher Faulete05bf9e2022-03-29 15:23:40 +02004986 txn->l7_buffer = BUF_NULL;
Willy Tarreau8b507582020-02-25 09:35:07 +01004987 write_u32(txn->cache_hash, 0);
Christopher Fauleta8a46e22019-07-16 14:53:09 +02004988
4989 txn->cookie_first_date = 0;
4990 txn->cookie_last_date = 0;
4991
4992 txn->srv_cookie = NULL;
4993 txn->cli_cookie = NULL;
4994 txn->uri = NULL;
4995
4996 http_txn_reset_req(txn);
4997 http_txn_reset_res(txn);
4998
4999 txn->req.chn = &s->req;
5000 txn->rsp.chn = &s->res;
5001
5002 txn->auth.method = HTTP_AUTH_UNKNOWN;
5003
Aurelien DARRAGON5ad2b642022-11-18 09:17:29 +01005004 /* here we don't want to re-initialize s->vars_txn and s->vars_reqres
5005 * variable lists, because they were already initialized upon stream
5006 * creation in stream_new(), and thus may already contain some variables
5007 */
Christopher Faulet75f619a2021-03-08 19:12:58 +01005008
5009 return txn;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005010}
5011
5012/* to be used at the end of a transaction */
Christopher Faulet75f619a2021-03-08 19:12:58 +01005013void http_destroy_txn(struct stream *s)
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005014{
5015 struct http_txn *txn = s->txn;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005016
5017 /* these ones will have been dynamically allocated */
5018 pool_free(pool_head_requri, txn->uri);
5019 pool_free(pool_head_capture, txn->cli_cookie);
5020 pool_free(pool_head_capture, txn->srv_cookie);
Tim Duesterhusa17e6622020-03-05 20:19:02 +01005021 pool_free(pool_head_uniqueid, s->unique_id.ptr);
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005022
Tim Duesterhusa17e6622020-03-05 20:19:02 +01005023 s->unique_id = IST_NULL;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005024 txn->uri = NULL;
5025 txn->srv_cookie = NULL;
5026 txn->cli_cookie = NULL;
5027
Christopher Faulet59399252019-11-07 14:27:52 +01005028 if (!LIST_ISEMPTY(&s->vars_txn.head))
5029 vars_prune(&s->vars_txn, s->sess, s);
5030 if (!LIST_ISEMPTY(&s->vars_reqres.head))
5031 vars_prune(&s->vars_reqres, s->sess, s);
Christopher Faulet75f619a2021-03-08 19:12:58 +01005032
Christopher Faulete05bf9e2022-03-29 15:23:40 +02005033 b_free(&txn->l7_buffer);
5034
Christopher Faulet75f619a2021-03-08 19:12:58 +01005035 pool_free(pool_head_http_txn, txn);
5036 s->txn = NULL;
Christopher Faulet59399252019-11-07 14:27:52 +01005037}
5038
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005039
Christopher Faulet71236de2023-01-13 09:06:38 +01005040void http_set_term_flags(struct stream *s)
5041{
5042 if (!(s->flags & SF_ERR_MASK))
5043 s->flags |= SF_ERR_PRXCOND;
5044
5045 if (!(s->flags & SF_FINST_MASK)) {
5046 if (s->scb->state == SC_ST_INI) {
5047 /* Before any connection attempt on the server side, we
5048 * are still in the request analysis. Just take case to
5049 * detect tarpit error
5050 */
5051 if (s->req.analysers & AN_REQ_HTTP_TARPIT)
5052 s->flags |= SF_FINST_T;
5053 else
5054 s->flags |= SF_FINST_R;
5055 }
5056 else if (s->scb->state == SC_ST_QUE)
5057 s->flags |= SF_FINST_Q;
5058 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)) {
5059 if (unlikely(objt_applet(s->target))) {
5060 s->flags |= SF_FINST_R;
5061 }
5062 else
5063 s->flags |= SF_FINST_C;
5064 }
5065 else {
5066 if (s->txn->rsp.msg_state < HTTP_MSG_DATA) {
5067 /* We are still processing the response headers */
5068 s->flags |= SF_FINST_H;
5069 }
5070 // (res >= done) & (res->flags & shutw)
5071 else if (s->txn->rsp.msg_state >= HTTP_MSG_DONE &&
5072 (s->flags & (SF_ERR_CLITO|SF_ERR_CLICL))) {
5073 /* A client error was reported and we are
5074 * transmitting the last block of data
5075 */
5076 s->flags |= SF_FINST_L;
5077 }
5078 else {
5079 /* Otherwise we are in DATA phase on both sides */
5080 s->flags |= SF_FINST_D;
5081 }
5082 }
5083 }
5084}
5085
5086
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005087DECLARE_POOL(pool_head_http_txn, "http_txn", sizeof(struct http_txn));
Christopher Faulet0f226952018-10-22 09:29:56 +02005088
Christopher Fauletf4eb75d2018-10-11 15:55:07 +02005089/*
5090 * Local variables:
5091 * c-indent-level: 8
5092 * c-basic-offset: 8
5093 * End:
5094 */