blob: a3618b90436a0c83da704ef5acb6f74d02f8f060 [file] [log] [blame]
Christopher Fauletf4eb75d2018-10-11 15:55:07 +02001/*
2 * HTTP protocol analyzer
3 *
4 * Copyright (C) 2018 HAProxy Technologies, Christopher Faulet <cfaulet@haproxy.com>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
Willy Tarreaudcc048a2020-06-04 19:11:43 +020013#include <haproxy/acl.h>
Willy Tarreau122eba92020-06-04 10:15:32 +020014#include <haproxy/action-t.h>
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020015#include <haproxy/api.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020016#include <haproxy/backend.h>
Willy Tarreau8d366972020-05-27 16:10:29 +020017#include <haproxy/base64.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020018#include <haproxy/capture-t.h>
Amaury Denoyelle03517732021-05-07 14:25:01 +020019#include <haproxy/cfgparse.h>
Willy Tarreauf1d32c42020-06-04 21:07:02 +020020#include <haproxy/channel.h>
Willy Tarreau4aa573d2020-06-04 18:21:56 +020021#include <haproxy/check.h>
Willy Tarreau7ea393d2020-06-04 18:02:10 +020022#include <haproxy/connection.h>
Willy Tarreau36979d92020-06-05 17:27:29 +020023#include <haproxy/errors.h>
Willy Tarreauc7babd82020-06-04 21:29:29 +020024#include <haproxy/filters.h>
Willy Tarreaucd72d8c2020-06-02 19:11:26 +020025#include <haproxy/http.h>
Willy Tarreauc2b1ff02020-06-04 21:21:03 +020026#include <haproxy/http_ana.h>
Willy Tarreau87735332020-06-04 09:08:41 +020027#include <haproxy/http_htx.h>
Willy Tarreau16f958c2020-06-03 08:44:35 +020028#include <haproxy/htx.h>
Willy Tarreauaeed4a82020-06-04 22:01:04 +020029#include <haproxy/log.h>
Willy Tarreau6131d6a2020-06-02 16:48:09 +020030#include <haproxy/net_helper.h>
Willy Tarreaua264d962020-06-04 22:29:18 +020031#include <haproxy/proxy.h>
Willy Tarreau7cd8b6e2020-06-02 17:32:26 +020032#include <haproxy/regex.h>
Willy Tarreau1e56f922020-06-04 23:20:13 +020033#include <haproxy/server-t.h>
Willy Tarreau2eec9b52020-06-04 19:58:55 +020034#include <haproxy/stats.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020035#include <haproxy/stream.h>
Willy Tarreau5e539c92020-06-04 20:45:39 +020036#include <haproxy/stream_interface.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020037#include <haproxy/trace.h>
Willy Tarreau8c42b8a2020-06-04 19:27:34 +020038#include <haproxy/uri_auth-t.h>
Willy Tarreaua1718922020-06-04 16:25:31 +020039#include <haproxy/vars.h>
Christopher Faulete0768eb2018-10-03 16:38:02 +020040
Christopher Faulete0768eb2018-10-03 16:38:02 +020041
Christopher Fauleteea8fc72019-11-05 16:18:10 +010042#define TRACE_SOURCE &trace_strm
43
Christopher Faulet377c5a52018-10-24 21:21:30 +020044extern const char *stat_status_codes[];
Christopher Fauletf2824e62018-10-01 12:12:37 +020045
Willy Tarreauff882702021-04-10 17:23:00 +020046struct pool_head *pool_head_requri __read_mostly = NULL;
47struct pool_head *pool_head_capture __read_mostly = NULL;
Christopher Fauleta8a46e22019-07-16 14:53:09 +020048
49
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020050static void http_end_request(struct stream *s);
51static void http_end_response(struct stream *s);
Christopher Fauletf2824e62018-10-01 12:12:37 +020052
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020053static void http_capture_headers(struct htx *htx, char **cap, struct cap_hdr *cap_hdr);
54static int http_del_hdr_value(char *start, char *end, char **from, char *next);
55static size_t http_fmt_req_line(const struct htx_sl *sl, char *str, size_t len);
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020056static void http_debug_stline(const char *dir, struct stream *s, const struct htx_sl *sl);
57static void http_debug_hdr(const char *dir, struct stream *s, const struct ist n, const struct ist v);
Christopher Faulet0f226952018-10-22 09:29:56 +020058
Christopher Fauletb58f62b2020-01-13 16:40:13 +010059static enum rule_result http_req_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s);
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020060static enum rule_result http_res_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s);
Christopher Faulet3e964192018-10-24 11:39:23 +020061
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020062static void http_manage_client_side_cookies(struct stream *s, struct channel *req);
63static void http_manage_server_side_cookies(struct stream *s, struct channel *res);
Christopher Fauletfcda7c62018-10-24 11:56:22 +020064
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020065static int http_stats_check_uri(struct stream *s, struct http_txn *txn, struct proxy *backend);
66static int http_handle_stats(struct stream *s, struct channel *req);
Christopher Faulet377c5a52018-10-24 21:21:30 +020067
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020068static int http_handle_expect_hdr(struct stream *s, struct htx *htx, struct http_msg *msg);
69static int http_reply_100_continue(struct stream *s);
Christopher Faulet23a3c792018-11-28 10:01:23 +010070
Christopher Faulete0768eb2018-10-03 16:38:02 +020071/* This stream analyser waits for a complete HTTP request. It returns 1 if the
72 * processing can continue on next analysers, or zero if it either needs more
73 * data or wants to immediately abort the request (eg: timeout, error, ...). It
74 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req.analysers
75 * when it has nothing left to do, and may remove any analyser when it wants to
76 * abort.
77 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020078int http_wait_for_request(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +020079{
Christopher Faulet9768c262018-10-22 09:34:31 +020080
Christopher Faulete0768eb2018-10-03 16:38:02 +020081 /*
Christopher Faulet9768c262018-10-22 09:34:31 +020082 * We will analyze a complete HTTP request to check the its syntax.
Christopher Faulete0768eb2018-10-03 16:38:02 +020083 *
Christopher Faulet9768c262018-10-22 09:34:31 +020084 * Once the start line and all headers are received, we may perform a
85 * capture of the error (if any), and we will set a few fields. We also
86 * check for monitor-uri, logging and finally headers capture.
Christopher Faulete0768eb2018-10-03 16:38:02 +020087 */
Christopher Faulete0768eb2018-10-03 16:38:02 +020088 struct session *sess = s->sess;
89 struct http_txn *txn = s->txn;
90 struct http_msg *msg = &txn->req;
Christopher Faulet9768c262018-10-22 09:34:31 +020091 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +010092 struct htx_sl *sl;
Christopher Faulete0768eb2018-10-03 16:38:02 +020093
Christopher Fauleteea8fc72019-11-05 16:18:10 +010094 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +020095
Christopher Fauletda46a0d2021-01-21 17:32:58 +010096 if (unlikely(!IS_HTX_STRM(s))) {
97 /* It is only possible when a TCP stream is upgrade to HTTP.
98 * There is a transition period during which there is no
99 * data. The stream is still in raw mode and SF_IGNORE flag is
100 * still set. When this happens, the new mux is responsible to
Ilya Shipitsinacf84592021-02-06 22:29:08 +0500101 * handle all errors. Thus we may leave immediately.
Christopher Fauletda46a0d2021-01-21 17:32:58 +0100102 */
103 BUG_ON(!(s->flags & SF_IGNORE) || !c_empty(&s->req));
Christopher Faulet9768c262018-10-22 09:34:31 +0200104
Christopher Faulet97b3a612021-03-15 17:10:12 +0100105 /* Don't connect for now */
106 channel_dont_connect(req);
107
108 /* A SHUTR at this stage means we are performing a "destructive"
109 * HTTP upgrade (TCP>H2). In this case, we can leave.
110 */
111 if (req->flags & CF_SHUTR) {
112 s->logs.logwait = 0;
113 s->logs.level = 0;
114 channel_abort(&s->req);
115 channel_abort(&s->res);
116 req->analysers &= AN_REQ_FLT_END;
117 req->analyse_exp = TICK_ETERNITY;
118 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA, s);
119 return 1;
120 }
Christopher Fauletda46a0d2021-01-21 17:32:58 +0100121 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA, s);
122 return 0;
123 }
124
125 htx = htxbuf(&req->buf);
Christopher Faulet8bebd2f2020-10-06 17:54:56 +0200126
Willy Tarreau4236f032019-03-05 10:43:32 +0100127 /* Parsing errors are caught here */
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200128 if (htx->flags & (HTX_FL_PARSING_ERROR|HTX_FL_PROCESSING_ERROR)) {
Willy Tarreau4236f032019-03-05 10:43:32 +0100129 stream_inc_http_req_ctr(s);
Emeric Brun28976442020-10-07 08:50:09 +0200130 proxy_inc_fe_req_ctr(sess->listener, sess->fe);
Christopher Fauletbf7175f2021-02-10 14:58:01 +0100131 if (htx->flags & HTX_FL_PARSING_ERROR) {
132 stream_inc_http_err_ctr(s);
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200133 goto return_bad_req;
Christopher Fauletbf7175f2021-02-10 14:58:01 +0100134 }
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200135 else
136 goto return_int_err;
Willy Tarreau4236f032019-03-05 10:43:32 +0100137 }
138
Christopher Faulete0768eb2018-10-03 16:38:02 +0200139 /* we're speaking HTTP here, so let's speak HTTP to the client */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200140 s->srv_error = http_return_srv_error;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200141
Christopher Faulet9768c262018-10-22 09:34:31 +0200142 msg->msg_state = HTTP_MSG_BODY;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200143 stream_inc_http_req_ctr(s);
Emeric Brun28976442020-10-07 08:50:09 +0200144 proxy_inc_fe_req_ctr(sess->listener, sess->fe); /* one more valid request for this FE */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200145
Christopher Faulet9768c262018-10-22 09:34:31 +0200146 /* kill the pending keep-alive timeout */
Christopher Faulet9768c262018-10-22 09:34:31 +0200147 req->analyse_exp = TICK_ETERNITY;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200148
Christopher Faulet29f17582019-05-23 11:03:26 +0200149 BUG_ON(htx_get_first_type(htx) != HTX_BLK_REQ_SL);
Christopher Faulet297fbb42019-05-13 14:41:27 +0200150 sl = http_get_stline(htx);
Christopher Faulet03599112018-11-27 11:21:21 +0100151
Christopher Faulet9768c262018-10-22 09:34:31 +0200152 /* 0: we might have to print this header in debug mode */
153 if (unlikely((global.mode & MODE_DEBUG) &&
154 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
155 int32_t pos;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200156
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200157 http_debug_stline("clireq", s, sl);
Christopher Faulet9768c262018-10-22 09:34:31 +0200158
Christopher Fauleta3f15502019-05-13 15:27:23 +0200159 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet9768c262018-10-22 09:34:31 +0200160 struct htx_blk *blk = htx_get_blk(htx, pos);
161 enum htx_blk_type type = htx_get_blk_type(blk);
162
163 if (type == HTX_BLK_EOH)
164 break;
165 if (type != HTX_BLK_HDR)
166 continue;
167
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200168 http_debug_hdr("clihdr", s,
169 htx_get_blk_name(htx, blk),
170 htx_get_blk_value(htx, blk));
Christopher Faulet9768c262018-10-22 09:34:31 +0200171 }
172 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200173
174 /*
Christopher Faulet03599112018-11-27 11:21:21 +0100175 * 1: identify the method and the version. Also set HTTP flags
Christopher Faulete0768eb2018-10-03 16:38:02 +0200176 */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100177 txn->meth = sl->info.req.meth;
Christopher Faulet03599112018-11-27 11:21:21 +0100178 if (sl->flags & HTX_SL_F_VER_11)
Christopher Faulet9768c262018-10-22 09:34:31 +0200179 msg->flags |= HTTP_MSGF_VER_11;
Christopher Faulet03599112018-11-27 11:21:21 +0100180 msg->flags |= HTTP_MSGF_XFER_LEN;
Christopher Faulet2a408542020-11-20 14:22:37 +0100181 if (sl->flags & HTX_SL_F_CLEN)
182 msg->flags |= HTTP_MSGF_CNT_LEN;
183 else if (sl->flags & HTX_SL_F_CHNK)
184 msg->flags |= HTTP_MSGF_TE_CHNK;
Christopher Fauletb2db4fa2018-11-27 16:51:09 +0100185 if (sl->flags & HTX_SL_F_BODYLESS)
186 msg->flags |= HTTP_MSGF_BODYLESS;
Christopher Faulet576c3582021-01-08 15:53:01 +0100187 if (sl->flags & HTX_SL_F_CONN_UPG)
188 msg->flags |= HTTP_MSGF_CONN_UPG;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200189
190 /* we can make use of server redirect on GET and HEAD */
191 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
192 s->flags |= SF_REDIRECTABLE;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100193 else if (txn->meth == HTTP_METH_OTHER && isteqi(htx_sl_req_meth(sl), ist("PRI"))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200194 /* PRI is reserved for the HTTP/2 preface */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200195 goto return_bad_req;
196 }
197
198 /*
Christopher Faulet6072beb2020-02-18 15:34:58 +0100199 * 2: check if the URI matches the monitor_uri. We have to do this for
200 * every request which gets in, because the monitor-uri is defined by
201 * the frontend. If the monitor-uri starts with a '/', the matching is
202 * done against the request's path. Otherwise, the request's uri is
203 * used. It is a workaround to let HTTP/2 health-checks work as
204 * expected.
Christopher Faulete0768eb2018-10-03 16:38:02 +0200205 */
206 if (unlikely((sess->fe->monitor_uri_len != 0) &&
Christopher Faulet6072beb2020-02-18 15:34:58 +0100207 ((*sess->fe->monitor_uri == '/' && isteq(http_get_path(htx_sl_req_uri(sl)),
208 ist2(sess->fe->monitor_uri, sess->fe->monitor_uri_len))) ||
209 isteq(htx_sl_req_uri(sl), ist2(sess->fe->monitor_uri, sess->fe->monitor_uri_len))))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200210 /*
211 * We have found the monitor URI
212 */
213 struct acl_cond *cond;
214
215 s->flags |= SF_MONITOR;
Willy Tarreau4781b152021-04-06 13:53:36 +0200216 _HA_ATOMIC_INC(&sess->fe->fe_counters.intercepted_req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200217
218 /* Check if we want to fail this monitor request or not */
219 list_for_each_entry(cond, &sess->fe->mon_fail_cond, list) {
220 int ret = acl_exec_cond(cond, sess->fe, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
221
222 ret = acl_pass(ret);
223 if (cond->pol == ACL_COND_UNLESS)
224 ret = !ret;
225
226 if (ret) {
227 /* we fail this request, let's return 503 service unavail */
228 txn->status = 503;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200229 if (!(s->flags & SF_ERR_MASK))
230 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
231 goto return_prx_cond;
232 }
233 }
234
Joseph Herlantc42c0e92018-11-25 10:43:27 -0800235 /* nothing to fail, let's reply normally */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200236 txn->status = 200;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200237 if (!(s->flags & SF_ERR_MASK))
238 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
239 goto return_prx_cond;
240 }
241
242 /*
243 * 3: Maybe we have to copy the original REQURI for the logs ?
244 * Note: we cannot log anymore if the request has been
245 * classified as invalid.
246 */
247 if (unlikely(s->logs.logwait & LW_REQ)) {
248 /* we have a complete HTTP request that we must log */
249 if ((txn->uri = pool_alloc(pool_head_requri)) != NULL) {
Christopher Faulet9768c262018-10-22 09:34:31 +0200250 size_t len;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200251
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200252 len = http_fmt_req_line(sl, txn->uri, global.tune.requri_len - 1);
Christopher Faulet9768c262018-10-22 09:34:31 +0200253 txn->uri[len] = 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200254
255 if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT)))
256 s->do_log(s);
257 } else {
258 ha_alert("HTTP logging : out of memory.\n");
259 }
260 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200261
Christopher Faulete0768eb2018-10-03 16:38:02 +0200262 /* if the frontend has "option http-use-proxy-header", we'll check if
263 * we have what looks like a proxied connection instead of a connection,
264 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
265 * Note that this is *not* RFC-compliant, however browsers and proxies
266 * happen to do that despite being non-standard :-(
267 * We consider that a request not beginning with either '/' or '*' is
268 * a proxied connection, which covers both "scheme://location" and
269 * CONNECT ip:port.
270 */
271 if ((sess->fe->options2 & PR_O2_USE_PXHDR) &&
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100272 *HTX_SL_REQ_UPTR(sl) != '/' && *HTX_SL_REQ_UPTR(sl) != '*')
Christopher Faulete0768eb2018-10-03 16:38:02 +0200273 txn->flags |= TX_USE_PX_CONN;
274
Christopher Faulete0768eb2018-10-03 16:38:02 +0200275 /* 5: we may need to capture headers */
276 if (unlikely((s->logs.logwait & LW_REQHDR) && s->req_cap))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200277 http_capture_headers(htx, s->req_cap, sess->fe->req_cap);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200278
Christopher Faulete0768eb2018-10-03 16:38:02 +0200279 /* we may have to wait for the request's body */
Christopher Faulet9768c262018-10-22 09:34:31 +0200280 if (s->be->options & PR_O_WREQ_BODY)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200281 req->analysers |= AN_REQ_HTTP_BODY;
282
283 /*
284 * RFC7234#4:
285 * A cache MUST write through requests with methods
286 * that are unsafe (Section 4.2.1 of [RFC7231]) to
287 * the origin server; i.e., a cache is not allowed
288 * to generate a reply to such a request before
289 * having forwarded the request and having received
290 * a corresponding response.
291 *
292 * RFC7231#4.2.1:
293 * Of the request methods defined by this
294 * specification, the GET, HEAD, OPTIONS, and TRACE
295 * methods are defined to be safe.
296 */
297 if (likely(txn->meth == HTTP_METH_GET ||
298 txn->meth == HTTP_METH_HEAD ||
299 txn->meth == HTTP_METH_OPTIONS ||
300 txn->meth == HTTP_METH_TRACE))
301 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
302
303 /* end of job, return OK */
304 req->analysers &= ~an_bit;
305 req->analyse_exp = TICK_ETERNITY;
Christopher Faulet9768c262018-10-22 09:34:31 +0200306
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100307 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200308 return 1;
309
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200310 return_int_err:
311 txn->status = 500;
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200312 if (!(s->flags & SF_ERR_MASK))
313 s->flags |= SF_ERR_INTERNAL;
Willy Tarreau4781b152021-04-06 13:53:36 +0200314 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
William Lallemand36119de2021-03-08 15:26:48 +0100315 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200316 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200317 goto return_prx_cond;
318
Christopher Faulete0768eb2018-10-03 16:38:02 +0200319 return_bad_req:
Christopher Faulet9768c262018-10-22 09:34:31 +0200320 txn->status = 400;
Willy Tarreau4781b152021-04-06 13:53:36 +0200321 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req);
William Lallemand36119de2021-03-08 15:26:48 +0100322 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200323 _HA_ATOMIC_INC(&sess->listener->counters->failed_req);
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200324 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200325
326 return_prx_cond:
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200327 http_reply_and_close(s, txn->status, http_error_message(s));
328
Christopher Faulete0768eb2018-10-03 16:38:02 +0200329 if (!(s->flags & SF_ERR_MASK))
330 s->flags |= SF_ERR_PRXCOND;
331 if (!(s->flags & SF_FINST_MASK))
332 s->flags |= SF_FINST_R;
333
334 req->analysers &= AN_REQ_FLT_END;
335 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100336 DBG_TRACE_DEVEL("leaving on error",
337 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200338 return 0;
339}
340
341
342/* This stream analyser runs all HTTP request processing which is common to
343 * frontends and backends, which means blocking ACLs, filters, connection-close,
344 * reqadd, stats and redirects. This is performed for the designated proxy.
345 * It returns 1 if the processing can continue on next analysers, or zero if it
346 * either needs more data or wants to immediately abort the request (eg: deny,
347 * error, ...).
348 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200349int http_process_req_common(struct stream *s, struct channel *req, int an_bit, struct proxy *px)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200350{
351 struct session *sess = s->sess;
352 struct http_txn *txn = s->txn;
353 struct http_msg *msg = &txn->req;
Christopher Fauletff2759f2018-10-24 11:13:16 +0200354 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200355 struct redirect_rule *rule;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200356 enum rule_result verdict;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200357 struct connection *conn = objt_conn(sess->origin);
358
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100359 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200360
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100361 htx = htxbuf(&req->buf);
Christopher Fauletff2759f2018-10-24 11:13:16 +0200362
Christopher Faulet1907ccc2019-04-29 13:12:02 +0200363 /* just in case we have some per-backend tracking. Only called the first
364 * execution of the analyser. */
365 if (!s->current_rule || s->current_rule_list != &px->http_req_rules)
366 stream_inc_be_http_req_ctr(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200367
368 /* evaluate http-request rules */
369 if (!LIST_ISEMPTY(&px->http_req_rules)) {
Christopher Fauletb58f62b2020-01-13 16:40:13 +0100370 verdict = http_req_get_intercept_rule(px, &px->http_req_rules, s);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200371
372 switch (verdict) {
373 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
374 goto return_prx_yield;
375
376 case HTTP_RULE_RES_CONT:
377 case HTTP_RULE_RES_STOP: /* nothing to do */
378 break;
379
380 case HTTP_RULE_RES_DENY: /* deny or tarpit */
381 if (txn->flags & TX_CLTARPIT)
382 goto tarpit;
383 goto deny;
384
385 case HTTP_RULE_RES_ABRT: /* abort request, response already sent. Eg: auth */
386 goto return_prx_cond;
387
388 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
389 goto done;
390
391 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
392 goto return_bad_req;
Christopher Faulet3a26bee2019-12-16 12:47:40 +0100393
394 case HTTP_RULE_RES_ERROR: /* failed with a bad request */
395 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200396 }
397 }
398
399 if (conn && (conn->flags & CO_FL_EARLY_DATA) &&
Olivier Houchard220a26c2020-01-23 14:57:36 +0100400 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_SSL_WAIT_HS))) {
Christopher Fauletff2759f2018-10-24 11:13:16 +0200401 struct http_hdr_ctx ctx;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200402
Christopher Fauletff2759f2018-10-24 11:13:16 +0200403 ctx.blk = NULL;
404 if (!http_find_header(htx, ist("Early-Data"), &ctx, 0)) {
405 if (unlikely(!http_add_header(htx, ist("Early-Data"), ist("1"))))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100406 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200407 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200408 }
409
410 /* OK at this stage, we know that the request was accepted according to
411 * the http-request rules, we can check for the stats. Note that the
412 * URI is detected *before* the req* rules in order not to be affected
413 * by a possible reqrep, while they are processed *after* so that a
414 * reqdeny can still block them. This clearly needs to change in 1.6!
415 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200416 if (!s->target && http_stats_check_uri(s, txn, px)) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200417 s->target = &http_stats_applet.obj_type;
Willy Tarreau14bfe9a2018-12-19 15:19:27 +0100418 if (unlikely(!si_register_handler(&s->si[1], objt_applet(s->target)))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200419 s->logs.tv_request = now;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200420 if (!(s->flags & SF_ERR_MASK))
421 s->flags |= SF_ERR_RESOURCE;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100422 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200423 }
424
425 /* parse the whole stats request and extract the relevant information */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200426 http_handle_stats(s, req);
Christopher Fauletb58f62b2020-01-13 16:40:13 +0100427 verdict = http_req_get_intercept_rule(px, &px->uri_auth->http_req_rules, s);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200428 /* not all actions implemented: deny, allow, auth */
429
430 if (verdict == HTTP_RULE_RES_DENY) /* stats http-request deny */
431 goto deny;
432
433 if (verdict == HTTP_RULE_RES_ABRT) /* stats auth / stats http-request auth */
434 goto return_prx_cond;
Christopher Faulet3a26bee2019-12-16 12:47:40 +0100435
436 if (verdict == HTTP_RULE_RES_BADREQ) /* failed with a bad request */
437 goto return_bad_req;
438
439 if (verdict == HTTP_RULE_RES_ERROR) /* failed with a bad request */
440 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200441 }
442
Christopher Faulet2571bc62019-03-01 11:44:26 +0100443 /* Proceed with the applets now. */
444 if (unlikely(objt_applet(s->target))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200445 if (sess->fe == s->be) /* report it if the request was intercepted by the frontend */
Willy Tarreau4781b152021-04-06 13:53:36 +0200446 _HA_ATOMIC_INC(&sess->fe->fe_counters.intercepted_req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200447
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200448 if (http_handle_expect_hdr(s, htx, msg) == -1)
Christopher Fauletb8a53712019-12-16 11:29:38 +0100449 goto return_int_err;
Christopher Fauletbcf242a2019-03-01 11:36:26 +0100450
Christopher Faulete0768eb2018-10-03 16:38:02 +0200451 if (!(s->flags & SF_ERR_MASK)) // this is not really an error but it is
452 s->flags |= SF_ERR_LOCAL; // to mark that it comes from the proxy
453 if (!(s->flags & SF_FINST_MASK))
454 s->flags |= SF_FINST_R;
455
Christopher Fauletc2ac5e42021-03-08 18:20:09 +0100456 if (HAS_FILTERS(s))
457 req->analysers |= AN_REQ_FLT_HTTP_HDRS;
458
Christopher Faulete0768eb2018-10-03 16:38:02 +0200459 /* enable the minimally required analyzers to handle keep-alive and compression on the HTTP response */
460 req->analysers &= (AN_REQ_HTTP_BODY | AN_REQ_FLT_HTTP_HDRS | AN_REQ_FLT_END);
461 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
462 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Christopher Fauletbcf242a2019-03-01 11:36:26 +0100463
464 req->flags |= CF_SEND_DONTWAIT;
465 s->flags |= SF_ASSIGNED;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200466 goto done;
467 }
468
469 /* check whether we have some ACLs set to redirect this request */
470 list_for_each_entry(rule, &px->redirect_rules, list) {
471 if (rule->cond) {
472 int ret;
473
474 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
475 ret = acl_pass(ret);
476 if (rule->cond->pol == ACL_COND_UNLESS)
477 ret = !ret;
478 if (!ret)
479 continue;
480 }
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200481 if (!http_apply_redirect_rule(rule, s, txn))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100482 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200483 goto done;
484 }
485
486 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
487 * If this happens, then the data will not come immediately, so we must
488 * send all what we have without waiting. Note that due to the small gain
489 * in waiting for the body of the request, it's easier to simply put the
490 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
491 * itself once used.
492 */
493 req->flags |= CF_SEND_DONTWAIT;
494
495 done: /* done with this analyser, continue with next ones that the calling
496 * points will have set, if any.
497 */
498 req->analyse_exp = TICK_ETERNITY;
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +0500499 done_without_exp: /* done with this analyser, but don't reset the analyse_exp. */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200500 req->analysers &= ~an_bit;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100501 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200502 return 1;
503
504 tarpit:
505 /* Allow cookie logging
506 */
507 if (s->be->cookie_name || sess->fe->capture_name)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200508 http_manage_client_side_cookies(s, req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200509
510 /* When a connection is tarpitted, we use the tarpit timeout,
511 * which may be the same as the connect timeout if unspecified.
512 * If unset, then set it to zero because we really want it to
513 * eventually expire. We build the tarpit as an analyser.
514 */
Christopher Faulet202c6ce2019-01-07 14:57:35 +0100515 channel_htx_erase(&s->req, htx);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200516
517 /* wipe the request out so that we can drop the connection early
518 * if the client closes first.
519 */
520 channel_dont_connect(req);
521
Christopher Faulete0768eb2018-10-03 16:38:02 +0200522 req->analysers &= AN_REQ_FLT_END; /* remove switching rules etc... */
523 req->analysers |= AN_REQ_HTTP_TARPIT;
524 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
525 if (!req->analyse_exp)
526 req->analyse_exp = tick_add(now_ms, 0);
527 stream_inc_http_err_ctr(s);
Willy Tarreau4781b152021-04-06 13:53:36 +0200528 _HA_ATOMIC_INC(&sess->fe->fe_counters.denied_req);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100529 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau4781b152021-04-06 13:53:36 +0200530 _HA_ATOMIC_INC(&s->be->be_counters.denied_req);
William Lallemand36119de2021-03-08 15:26:48 +0100531 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200532 _HA_ATOMIC_INC(&sess->listener->counters->denied_req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200533 goto done_without_exp;
534
535 deny: /* this request was blocked (denied) */
536
537 /* Allow cookie logging
538 */
539 if (s->be->cookie_name || sess->fe->capture_name)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200540 http_manage_client_side_cookies(s, req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200541
Christopher Faulete0768eb2018-10-03 16:38:02 +0200542 s->logs.tv_request = now;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200543 stream_inc_http_err_ctr(s);
Willy Tarreau4781b152021-04-06 13:53:36 +0200544 _HA_ATOMIC_INC(&sess->fe->fe_counters.denied_req);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100545 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau4781b152021-04-06 13:53:36 +0200546 _HA_ATOMIC_INC(&s->be->be_counters.denied_req);
William Lallemand36119de2021-03-08 15:26:48 +0100547 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200548 _HA_ATOMIC_INC(&sess->listener->counters->denied_req);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100549 goto return_prx_err;
550
551 return_int_err:
552 txn->status = 500;
553 if (!(s->flags & SF_ERR_MASK))
554 s->flags |= SF_ERR_INTERNAL;
Willy Tarreau4781b152021-04-06 13:53:36 +0200555 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100556 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau4781b152021-04-06 13:53:36 +0200557 _HA_ATOMIC_INC(&s->be->be_counters.internal_errors);
William Lallemand36119de2021-03-08 15:26:48 +0100558 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200559 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100560 goto return_prx_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200561
562 return_bad_req:
Christopher Faulete0768eb2018-10-03 16:38:02 +0200563 txn->status = 400;
Willy Tarreau4781b152021-04-06 13:53:36 +0200564 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req);
William Lallemand36119de2021-03-08 15:26:48 +0100565 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200566 _HA_ATOMIC_INC(&sess->listener->counters->failed_req);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100567 /* fall through */
568
569 return_prx_err:
570 http_reply_and_close(s, txn->status, http_error_message(s));
571 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200572
573 return_prx_cond:
574 if (!(s->flags & SF_ERR_MASK))
575 s->flags |= SF_ERR_PRXCOND;
576 if (!(s->flags & SF_FINST_MASK))
577 s->flags |= SF_FINST_R;
578
579 req->analysers &= AN_REQ_FLT_END;
580 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100581 DBG_TRACE_DEVEL("leaving on error",
582 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200583 return 0;
584
585 return_prx_yield:
586 channel_dont_connect(req);
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100587 DBG_TRACE_DEVEL("waiting for more data",
588 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200589 return 0;
590}
591
592/* This function performs all the processing enabled for the current request.
593 * It returns 1 if the processing can continue on next analysers, or zero if it
594 * needs more data, encounters an error, or wants to immediately abort the
595 * request. It relies on buffers flags, and updates s->req.analysers.
596 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200597int http_process_request(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200598{
599 struct session *sess = s->sess;
600 struct http_txn *txn = s->txn;
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200601 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200602 struct connection *cli_conn = objt_conn(strm_sess(s)->origin);
603
Christopher Faulet8bebd2f2020-10-06 17:54:56 +0200604 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200605
606 /*
607 * Right now, we know that we have processed the entire headers
608 * and that unwanted requests have been filtered out. We can do
609 * whatever we want with the remaining request. Also, now we
610 * may have separate values for ->fe, ->be.
611 */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100612 htx = htxbuf(&req->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200613
614 /*
615 * If HTTP PROXY is set we simply get remote server address parsing
Willy Tarreau1c8d32b2019-07-18 15:47:45 +0200616 * incoming request.
Christopher Faulete0768eb2018-10-03 16:38:02 +0200617 */
618 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SF_ADDR_SET)) {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100619 struct htx_sl *sl;
620 struct ist uri, path;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200621
Willy Tarreau9b7587a2020-10-15 07:32:10 +0200622 if (!sockaddr_alloc(&s->target_addr, NULL, 0)) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200623 if (!(s->flags & SF_ERR_MASK))
624 s->flags |= SF_ERR_RESOURCE;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100625 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200626 }
Christopher Faulet297fbb42019-05-13 14:41:27 +0200627 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100628 uri = htx_sl_req_uri(sl);
629 path = http_get_path(uri);
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200630
Willy Tarreau1c8d32b2019-07-18 15:47:45 +0200631 if (url2sa(uri.ptr, uri.len - path.len, s->target_addr, NULL) == -1)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200632 goto return_bad_req;
633
Willy Tarreau1c8d32b2019-07-18 15:47:45 +0200634 s->target = &s->be->obj_type;
635 s->flags |= SF_ADDR_SET | SF_ASSIGNED;
636
Christopher Faulete0768eb2018-10-03 16:38:02 +0200637 /* if the path was found, we have to remove everything between
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200638 * uri.ptr and path.ptr (excluded). If it was not found, we need
639 * to replace from all the uri by a single "/".
640 *
Ilya Shipitsin46a030c2020-07-05 16:36:08 +0500641 * Instead of rewriting the whole start line, we just update
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100642 * the star-line URI. Some space will be lost but it should be
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200643 * insignificant.
Christopher Faulete0768eb2018-10-03 16:38:02 +0200644 */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100645 istcpy(&uri, (path.len ? path : ist("/")), uri.len);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200646 }
647
648 /*
649 * 7: Now we can work with the cookies.
650 * Note that doing so might move headers in the request, but
651 * the fields will stay coherent and the URI will not move.
652 * This should only be performed in the backend.
653 */
654 if (s->be->cookie_name || sess->fe->capture_name)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200655 http_manage_client_side_cookies(s, req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200656
Tim Duesterhus2825b4b2020-02-28 15:13:34 +0100657 /* 8: Generate unique ID if a "unique-id-format" is defined.
658 *
659 * A unique ID is generated even when it is not sent to ensure that the ID can make use of
660 * fetches only available in the HTTP request processing stage.
661 */
662 if (!LIST_ISEMPTY(&sess->fe->format_unique_id)) {
Tim Duesterhusa17e6622020-03-05 20:19:02 +0100663 struct ist unique_id = stream_generate_unique_id(s, &sess->fe->format_unique_id);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200664
Tim Duesterhusa17e6622020-03-05 20:19:02 +0100665 if (!isttest(unique_id)) {
Christopher Fauletb8a53712019-12-16 11:29:38 +0100666 if (!(s->flags & SF_ERR_MASK))
667 s->flags |= SF_ERR_RESOURCE;
668 goto return_int_err;
669 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200670
Tim Duesterhus2825b4b2020-02-28 15:13:34 +0100671 /* send unique ID if a "unique-id-header" is defined */
Tim Duesterhus0643b0e2020-03-05 17:56:35 +0100672 if (isttest(sess->fe->header_unique_id) &&
Tim Duesterhusa17e6622020-03-05 20:19:02 +0100673 unlikely(!http_add_header(htx, sess->fe->header_unique_id, s->unique_id)))
Tim Duesterhus2825b4b2020-02-28 15:13:34 +0100674 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200675 }
676
677 /*
678 * 9: add X-Forwarded-For if either the frontend or the backend
679 * asks for it.
680 */
681 if ((sess->fe->options | s->be->options) & PR_O_FWDFOR) {
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200682 struct http_hdr_ctx ctx = { .blk = NULL };
683 struct ist hdr = ist2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : sess->fe->fwdfor_hdr_name,
684 s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : sess->fe->fwdfor_hdr_len);
685
Christopher Faulete0768eb2018-10-03 16:38:02 +0200686 if (!((sess->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200687 http_find_header(htx, hdr, &ctx, 0)) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200688 /* The header is set to be added only if none is present
689 * and we found it, so don't do anything.
690 */
691 }
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200692 else if (cli_conn && conn_get_src(cli_conn) && cli_conn->src->ss_family == AF_INET) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200693 /* Add an X-Forwarded-For header unless the source IP is
694 * in the 'except' network range.
695 */
Christopher Faulet5d1def62021-02-26 09:19:15 +0100696 if (ipcmp2net(cli_conn->src, &sess->fe->except_xff_net) &&
697 ipcmp2net(cli_conn->src, &s->be->except_xff_net)) {
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200698 unsigned char *pn = (unsigned char *)&((struct sockaddr_in *)cli_conn->src)->sin_addr;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200699
700 /* Note: we rely on the backend to get the header name to be used for
701 * x-forwarded-for, because the header is really meant for the backends.
702 * However, if the backend did not specify any option, we have to rely
703 * on the frontend's header name.
704 */
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200705 chunk_printf(&trash, "%d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
706 if (unlikely(!http_add_header(htx, hdr, ist2(trash.area, trash.data))))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100707 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200708 }
709 }
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200710 else if (cli_conn && conn_get_src(cli_conn) && cli_conn->src->ss_family == AF_INET6) {
Christopher Faulet5d1def62021-02-26 09:19:15 +0100711 /* Add an X-Forwarded-For header unless the source IP is
712 * in the 'except' network range.
Christopher Faulete0768eb2018-10-03 16:38:02 +0200713 */
Christopher Faulet5d1def62021-02-26 09:19:15 +0100714 if (ipcmp2net(cli_conn->src, &sess->fe->except_xff_net) &&
715 ipcmp2net(cli_conn->src, &s->be->except_xff_net)) {
716 char pn[INET6_ADDRSTRLEN];
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200717
Christopher Faulet5d1def62021-02-26 09:19:15 +0100718 inet_ntop(AF_INET6,
719 (const void *)&((struct sockaddr_in6 *)(cli_conn->src))->sin6_addr,
720 pn, sizeof(pn));
Christopher Faulete0768eb2018-10-03 16:38:02 +0200721
Christopher Faulet5d1def62021-02-26 09:19:15 +0100722 /* Note: we rely on the backend to get the header name to be used for
723 * x-forwarded-for, because the header is really meant for the backends.
724 * However, if the backend did not specify any option, we have to rely
725 * on the frontend's header name.
726 */
727 chunk_printf(&trash, "%s", pn);
728 if (unlikely(!http_add_header(htx, hdr, ist2(trash.area, trash.data))))
729 goto return_int_err;
730 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200731 }
732 }
733
734 /*
735 * 10: add X-Original-To if either the frontend or the backend
736 * asks for it.
737 */
738 if ((sess->fe->options | s->be->options) & PR_O_ORGTO) {
Christopher Faulet5d1def62021-02-26 09:19:15 +0100739 struct ist hdr = ist2(s->be->orgto_hdr_len ? s->be->orgto_hdr_name : sess->fe->orgto_hdr_name,
740 s->be->orgto_hdr_len ? s->be->orgto_hdr_len : sess->fe->orgto_hdr_len);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200741
Christopher Fauletcccded92021-02-26 12:45:56 +0100742 if (cli_conn && conn_get_dst(cli_conn) && cli_conn->dst->ss_family == AF_INET) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200743 /* Add an X-Original-To header unless the destination IP is
744 * in the 'except' network range.
745 */
Christopher Faulet5d1def62021-02-26 09:19:15 +0100746 if (ipcmp2net(cli_conn->dst, &sess->fe->except_xot_net) &&
747 ipcmp2net(cli_conn->dst, &s->be->except_xot_net)) {
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200748 unsigned char *pn = (unsigned char *)&((struct sockaddr_in *)cli_conn->dst)->sin_addr;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200749
750 /* Note: we rely on the backend to get the header name to be used for
751 * x-original-to, because the header is really meant for the backends.
752 * However, if the backend did not specify any option, we have to rely
753 * on the frontend's header name.
754 */
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200755 chunk_printf(&trash, "%d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
756 if (unlikely(!http_add_header(htx, hdr, ist2(trash.area, trash.data))))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100757 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200758 }
759 }
Christopher Faulet5d1def62021-02-26 09:19:15 +0100760 else if (cli_conn && conn_get_dst(cli_conn) && cli_conn->dst->ss_family == AF_INET6) {
761 /* Add an X-Original-To header unless the source IP is
762 * in the 'except' network range.
763 */
764 if (ipcmp2net(cli_conn->dst, &sess->fe->except_xot_net) &&
765 ipcmp2net(cli_conn->dst, &s->be->except_xot_net)) {
766 char pn[INET6_ADDRSTRLEN];
767
768 inet_ntop(AF_INET6,
769 (const void *)&((struct sockaddr_in6 *)(cli_conn->dst))->sin6_addr,
770 pn, sizeof(pn));
771
772 /* Note: we rely on the backend to get the header name to be used for
773 * x-forwarded-for, because the header is really meant for the backends.
774 * However, if the backend did not specify any option, we have to rely
775 * on the frontend's header name.
776 */
777 chunk_printf(&trash, "%s", pn);
778 if (unlikely(!http_add_header(htx, hdr, ist2(trash.area, trash.data))))
779 goto return_int_err;
780 }
781 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200782 }
783
Christopher Fauletc2ac5e42021-03-08 18:20:09 +0100784 /* Filter the request headers if there are filters attached to the
785 * stream.
786 */
787 if (HAS_FILTERS(s))
788 req->analysers |= AN_REQ_FLT_HTTP_HDRS;
789
Christopher Faulete0768eb2018-10-03 16:38:02 +0200790 /* If we have no server assigned yet and we're balancing on url_param
791 * with a POST request, we may be interested in checking the body for
792 * that parameter. This will be done in another analyser.
793 */
794 if (!(s->flags & (SF_ASSIGNED|SF_DIRECT)) &&
Willy Tarreau089eaa02019-01-14 15:17:46 +0100795 s->txn->meth == HTTP_METH_POST &&
796 (s->be->lbprm.algo & BE_LB_ALGO) == BE_LB_ALGO_PH) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200797 channel_dont_connect(req);
798 req->analysers |= AN_REQ_HTTP_BODY;
799 }
800
801 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
802 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau1a18b542018-12-11 16:37:42 +0100803
Christopher Faulete0768eb2018-10-03 16:38:02 +0200804 /* We expect some data from the client. Unless we know for sure
805 * we already have a full request, we have to re-enable quick-ack
806 * in case we previously disabled it, otherwise we might cause
807 * the client to delay further data.
808 */
William Lallemand36119de2021-03-08 15:26:48 +0100809 if ((sess->listener && (sess->listener->options & LI_O_NOQUICKACK)) && !(htx->flags & HTX_FL_EOM))
Willy Tarreau1a18b542018-12-11 16:37:42 +0100810 conn_set_quickack(cli_conn, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200811
812 /*************************************************************
813 * OK, that's finished for the headers. We have done what we *
814 * could. Let's switch to the DATA state. *
815 ************************************************************/
816 req->analyse_exp = TICK_ETERNITY;
817 req->analysers &= ~an_bit;
818
819 s->logs.tv_request = now;
820 /* OK let's go on with the BODY now */
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100821 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200822 return 1;
823
Christopher Fauletb8a53712019-12-16 11:29:38 +0100824 return_int_err:
825 txn->status = 500;
826 if (!(s->flags & SF_ERR_MASK))
827 s->flags |= SF_ERR_INTERNAL;
Willy Tarreau4781b152021-04-06 13:53:36 +0200828 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100829 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau4781b152021-04-06 13:53:36 +0200830 _HA_ATOMIC_INC(&s->be->be_counters.internal_errors);
William Lallemand36119de2021-03-08 15:26:48 +0100831 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200832 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100833 goto return_prx_cond;
834
Christopher Faulete0768eb2018-10-03 16:38:02 +0200835 return_bad_req: /* let's centralize all bad requests */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200836 txn->status = 400;
Willy Tarreau4781b152021-04-06 13:53:36 +0200837 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req);
William Lallemand36119de2021-03-08 15:26:48 +0100838 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200839 _HA_ATOMIC_INC(&sess->listener->counters->failed_req);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100840 /* fall through */
841
842 return_prx_cond:
843 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +0200844
845 if (!(s->flags & SF_ERR_MASK))
846 s->flags |= SF_ERR_PRXCOND;
847 if (!(s->flags & SF_FINST_MASK))
848 s->flags |= SF_FINST_R;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100849
850 req->analysers &= AN_REQ_FLT_END;
851 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100852 DBG_TRACE_DEVEL("leaving on error",
853 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200854 return 0;
855}
856
857/* This function is an analyser which processes the HTTP tarpit. It always
858 * returns zero, at the beginning because it prevents any other processing
859 * from occurring, and at the end because it terminates the request.
860 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200861int http_process_tarpit(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200862{
863 struct http_txn *txn = s->txn;
864
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100865 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, &txn->req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200866 /* This connection is being tarpitted. The CLIENT side has
867 * already set the connect expiration date to the right
868 * timeout. We just have to check that the client is still
869 * there and that the timeout has not expired.
870 */
871 channel_dont_connect(req);
872 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100873 !tick_is_expired(req->analyse_exp, now_ms)) {
874 DBG_TRACE_DEVEL("waiting for tarpit timeout expiry",
875 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200876 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100877 }
878
Christopher Faulete0768eb2018-10-03 16:38:02 +0200879
880 /* We will set the queue timer to the time spent, just for
881 * logging purposes. We fake a 500 server error, so that the
882 * attacker will not suspect his connection has been tarpitted.
883 * It will not cause trouble to the logs because we can exclude
884 * the tarpitted connections by filtering on the 'PT' status flags.
885 */
886 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
887
Christopher Faulet8dfeccf2020-05-15 14:16:29 +0200888 http_reply_and_close(s, txn->status, (!(req->flags & CF_READ_ERROR) ? http_error_message(s) : NULL));
Christopher Faulet5cb513a2020-05-13 17:56:56 +0200889
890 end:
Christopher Faulete0768eb2018-10-03 16:38:02 +0200891 req->analysers &= AN_REQ_FLT_END;
892 req->analyse_exp = TICK_ETERNITY;
893
894 if (!(s->flags & SF_ERR_MASK))
895 s->flags |= SF_ERR_PRXCOND;
896 if (!(s->flags & SF_FINST_MASK))
897 s->flags |= SF_FINST_T;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100898
899 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200900 return 0;
901}
902
903/* This function is an analyser which waits for the HTTP request body. It waits
904 * for either the buffer to be full, or the full advertised contents to have
905 * reached the buffer. It must only be called after the standard HTTP request
906 * processing has occurred, because it expects the request to be parsed and will
907 * look for the Expect header. It may send a 100-Continue interim response. It
908 * takes in input any state starting from HTTP_MSG_BODY and leaves with one of
909 * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it
910 * needs to read more data, or 1 once it has completed its analysis.
911 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200912int http_wait_for_request_body(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200913{
914 struct session *sess = s->sess;
915 struct http_txn *txn = s->txn;
916 struct http_msg *msg = &s->txn->req;
917
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100918 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Fauletf76ebe82018-10-24 11:16:22 +0200919
Christopher Fauletf76ebe82018-10-24 11:16:22 +0200920
Christopher Faulet021a8e42021-03-29 10:46:38 +0200921 switch (http_wait_for_msg_body(s, req, s->be->timeout.httpreq, 0)) {
922 case HTTP_RULE_RES_CONT:
923 goto http_end;
924 case HTTP_RULE_RES_YIELD:
925 goto missing_data_or_waiting;
926 case HTTP_RULE_RES_BADREQ:
Willy Tarreau4236f032019-03-05 10:43:32 +0100927 goto return_bad_req;
Christopher Faulet021a8e42021-03-29 10:46:38 +0200928 case HTTP_RULE_RES_ERROR:
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200929 goto return_int_err;
Christopher Faulet021a8e42021-03-29 10:46:38 +0200930 case HTTP_RULE_RES_ABRT:
Christopher Fauletb8a53712019-12-16 11:29:38 +0100931 goto return_prx_cond;
Christopher Faulet021a8e42021-03-29 10:46:38 +0200932 default:
933 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200934 }
935
936 http_end:
937 /* The situation will not evolve, so let's give up on the analysis. */
938 s->logs.tv_request = now; /* update the request timer to reflect full request */
939 req->analysers &= ~an_bit;
940 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100941 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200942 return 1;
943
Christopher Faulet021a8e42021-03-29 10:46:38 +0200944 missing_data_or_waiting:
945 channel_dont_connect(req);
946 DBG_TRACE_DEVEL("waiting for more data",
947 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
948 return 0;
949
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200950 return_int_err:
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200951 txn->status = 500;
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200952 if (!(s->flags & SF_ERR_MASK))
953 s->flags |= SF_ERR_INTERNAL;
Willy Tarreau4781b152021-04-06 13:53:36 +0200954 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100955 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau4781b152021-04-06 13:53:36 +0200956 _HA_ATOMIC_INC(&s->be->be_counters.internal_errors);
William Lallemand36119de2021-03-08 15:26:48 +0100957 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200958 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Faulet021a8e42021-03-29 10:46:38 +0200959 goto return_prx_err;
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200960
Christopher Faulete0768eb2018-10-03 16:38:02 +0200961 return_bad_req: /* let's centralize all bad requests */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200962 txn->status = 400;
Willy Tarreau4781b152021-04-06 13:53:36 +0200963 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req);
William Lallemand36119de2021-03-08 15:26:48 +0100964 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200965 _HA_ATOMIC_INC(&sess->listener->counters->failed_req);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100966 /* fall through */
967
Christopher Faulet021a8e42021-03-29 10:46:38 +0200968 return_prx_err:
Christopher Fauletb8a53712019-12-16 11:29:38 +0100969 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet021a8e42021-03-29 10:46:38 +0200970 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200971
Christopher Faulet021a8e42021-03-29 10:46:38 +0200972 return_prx_cond:
Christopher Faulete0768eb2018-10-03 16:38:02 +0200973 if (!(s->flags & SF_ERR_MASK))
974 s->flags |= SF_ERR_PRXCOND;
975 if (!(s->flags & SF_FINST_MASK))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100976 s->flags |= (msg->msg_state < HTTP_MSG_DATA ? SF_FINST_R : SF_FINST_D);
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200977
Christopher Faulete0768eb2018-10-03 16:38:02 +0200978 req->analysers &= AN_REQ_FLT_END;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100979 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100980 DBG_TRACE_DEVEL("leaving on error",
981 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200982 return 0;
983}
984
985/* This function is an analyser which forwards request body (including chunk
986 * sizes if any). It is called as soon as we must forward, even if we forward
987 * zero byte. The only situation where it must not be called is when we're in
988 * tunnel mode and we want to forward till the close. It's used both to forward
989 * remaining data and to resync after end of body. It expects the msg_state to
990 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
991 * read more data, or 1 once we can go on with next request or end the stream.
992 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
993 * bytes of pending data + the headers if not already done.
994 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200995int http_request_forward_body(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200996{
997 struct session *sess = s->sess;
998 struct http_txn *txn = s->txn;
Christopher Faulet9768c262018-10-22 09:34:31 +0200999 struct http_msg *msg = &txn->req;
1000 struct htx *htx;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001001 short status = 0;
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001002 int ret;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001003
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001004 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001005
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001006 htx = htxbuf(&req->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001007
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001008 if (htx->flags & HTX_FL_PARSING_ERROR)
1009 goto return_bad_req;
1010 if (htx->flags & HTX_FL_PROCESSING_ERROR)
1011 goto return_int_err;
1012
Christopher Faulete0768eb2018-10-03 16:38:02 +02001013 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
1014 ((req->flags & CF_SHUTW) && (req->to_forward || co_data(req)))) {
1015 /* Output closed while we were sending data. We must abort and
1016 * wake the other side up.
Christopher Fauletf506d962021-04-27 10:56:28 +02001017 *
1018 * If we have finished to send the request and the response is
1019 * still in progress, don't catch write error on the request
1020 * side if it is in fact a read error on the server side.
Christopher Faulete0768eb2018-10-03 16:38:02 +02001021 */
Christopher Fauletf506d962021-04-27 10:56:28 +02001022 if (msg->msg_state == HTTP_MSG_DONE && (s->res.flags & CF_READ_ERROR) && s->res.analysers)
1023 return 0;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001024
Olivier Houchard29cac3c2019-07-12 15:48:58 +02001025 /* Don't abort yet if we had L7 retries activated and it
1026 * was a write error, we may recover.
1027 */
1028 if (!(req->flags & (CF_READ_ERROR | CF_READ_TIMEOUT)) &&
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001029 (s->si[1].flags & SI_FL_L7_RETRY)) {
1030 DBG_TRACE_DEVEL("leaving on L7 retry",
1031 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Olivier Houchard29cac3c2019-07-12 15:48:58 +02001032 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001033 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001034 msg->msg_state = HTTP_MSG_ERROR;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001035 http_end_request(s);
1036 http_end_response(s);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001037 DBG_TRACE_DEVEL("leaving on error",
1038 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001039 return 1;
1040 }
1041
1042 /* Note that we don't have to send 100-continue back because we don't
1043 * need the data to complete our job, and it's up to the server to
1044 * decide whether to return 100, 417 or anything else in return of
1045 * an "Expect: 100-continue" header.
1046 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001047 if (msg->msg_state == HTTP_MSG_BODY)
1048 msg->msg_state = HTTP_MSG_DATA;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001049
Christopher Faulete0768eb2018-10-03 16:38:02 +02001050 /* in most states, we should abort in case of early close */
1051 channel_auto_close(req);
1052
1053 if (req->to_forward) {
Christopher Faulet66af0b22019-03-22 14:54:52 +01001054 if (req->to_forward == CHN_INFINITE_FORWARD) {
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001055 if (req->flags & CF_EOI)
1056 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet66af0b22019-03-22 14:54:52 +01001057 }
1058 else {
1059 /* We can't process the buffer's contents yet */
1060 req->flags |= CF_WAKE_WRITE;
1061 goto missing_data_or_waiting;
1062 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001063 }
1064
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001065 if (msg->msg_state >= HTTP_MSG_ENDING)
1066 goto ending;
1067
1068 if (txn->meth == HTTP_METH_CONNECT) {
1069 msg->msg_state = HTTP_MSG_ENDING;
1070 goto ending;
1071 }
1072
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001073 /* Forward input data. We get it by removing all outgoing data not
1074 * forwarded yet from HTX data size. If there are some data filters, we
1075 * let them decide the amount of data to forward.
Christopher Faulet9768c262018-10-22 09:34:31 +02001076 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001077 if (HAS_REQ_DATA_FILTERS(s)) {
1078 ret = flt_http_payload(s, msg, htx->data);
1079 if (ret < 0)
1080 goto return_bad_req;
Christopher Faulet421e7692019-06-13 11:16:45 +02001081 c_adv(req, ret);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001082 }
1083 else {
Christopher Faulet421e7692019-06-13 11:16:45 +02001084 c_adv(req, htx->data - co_data(req));
Christopher Faulet66af0b22019-03-22 14:54:52 +01001085 if (msg->flags & HTTP_MSGF_XFER_LEN)
1086 channel_htx_forward_forever(req, htx);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001087 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001088
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001089 if (htx->data != co_data(req))
1090 goto missing_data_or_waiting;
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001091
Christopher Faulet9768c262018-10-22 09:34:31 +02001092 /* Check if the end-of-message is reached and if so, switch the message
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001093 * in HTTP_MSG_ENDING state. Then if all data was marked to be
1094 * forwarded, set the state to HTTP_MSG_DONE.
Christopher Faulet9768c262018-10-22 09:34:31 +02001095 */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001096 if (!(htx->flags & HTX_FL_EOM))
Christopher Faulet9768c262018-10-22 09:34:31 +02001097 goto missing_data_or_waiting;
1098
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001099 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet9768c262018-10-22 09:34:31 +02001100
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001101 ending:
Christopher Faulet2151cdd2020-07-22 16:34:59 +02001102 req->flags &= ~CF_EXPECT_MORE; /* no more data are expected */
1103
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001104 /* other states, ENDING...TUNNEL */
1105 if (msg->msg_state >= HTTP_MSG_DONE)
1106 goto done;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001107
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001108 if (HAS_REQ_DATA_FILTERS(s)) {
1109 ret = flt_http_end(s, msg);
1110 if (ret <= 0) {
1111 if (!ret)
1112 goto missing_data_or_waiting;
1113 goto return_bad_req;
1114 }
1115 }
1116
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001117 if (txn->meth == HTTP_METH_CONNECT)
1118 msg->msg_state = HTTP_MSG_TUNNEL;
1119 else {
1120 msg->msg_state = HTTP_MSG_DONE;
1121 req->to_forward = 0;
Christopher Faulet5b82cc52020-10-12 15:18:50 +02001122
1123 if ((s->be->retry_type &~ PR_RE_CONN_FAILED) && !(s->si[1].flags & SI_FL_D_L7_RETRY)) {
1124 struct stream_interface *si = &s->si[1];
1125
1126 /* If we want to be able to do L7 retries, copy the
1127 * request, so that we are able to resend them if
1128 * needed.
1129 *
1130 * Try to allocate a buffer if we had none. If it
1131 * fails, the next test will just disable the l7
1132 * retries.
1133 */
1134 DBG_TRACE_STATE("enable L7 retry, save the request", STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
1135 si->flags |= SI_FL_L7_RETRY;
Willy Tarreauf499f502021-03-22 16:17:37 +01001136 if (b_alloc(&si->l7_buffer) == NULL)
Christopher Faulet5b82cc52020-10-12 15:18:50 +02001137 si->flags &= ~SI_FL_L7_RETRY;
1138 else {
1139 memcpy(b_orig(&si->l7_buffer), b_orig(&req->buf), b_size(&req->buf));
1140 b_add(&si->l7_buffer, co_data(req));
1141 }
1142 }
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001143 }
1144
1145 done:
1146 /* we don't want to forward closes on DONE except in tunnel mode. */
1147 if (!(txn->flags & TX_CON_WANT_TUN))
1148 channel_dont_close(req);
1149
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001150 http_end_request(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001151 if (!(req->analysers & an_bit)) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001152 http_end_response(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001153 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
1154 if (req->flags & CF_SHUTW) {
1155 /* request errors are most likely due to the
1156 * server aborting the transfer. */
Christopher Faulet93e02d82019-03-08 14:18:50 +01001157 goto return_srv_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001158 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001159 goto return_bad_req;
1160 }
Christopher Faulet5b82cc52020-10-12 15:18:50 +02001161
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001162 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001163 return 1;
1164 }
1165
1166 /* If "option abortonclose" is set on the backend, we want to monitor
1167 * the client's connection and forward any shutdown notification to the
1168 * server, which will decide whether to close or to go on processing the
1169 * request. We only do that in tunnel mode, and not in other modes since
1170 * it can be abused to exhaust source ports. */
Christopher Faulet769d0e92019-03-22 14:23:18 +01001171 if (s->be->options & PR_O_ABRT_CLOSE) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001172 channel_auto_read(req);
Christopher Fauletc41547b2019-07-16 14:32:23 +02001173 if ((req->flags & (CF_SHUTR|CF_READ_NULL)) && !(txn->flags & TX_CON_WANT_TUN))
Christopher Faulete0768eb2018-10-03 16:38:02 +02001174 s->si[1].flags |= SI_FL_NOLINGER;
1175 channel_auto_close(req);
1176 }
1177 else if (s->txn->meth == HTTP_METH_POST) {
1178 /* POST requests may require to read extra CRLF sent by broken
1179 * browsers and which could cause an RST to be sent upon close
1180 * on some systems (eg: Linux). */
1181 channel_auto_read(req);
1182 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001183 DBG_TRACE_DEVEL("waiting for the end of the HTTP txn",
1184 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001185 return 0;
1186
1187 missing_data_or_waiting:
1188 /* stop waiting for data if the input is closed before the end */
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001189 if (msg->msg_state < HTTP_MSG_ENDING && req->flags & CF_SHUTR)
Christopher Faulet93e02d82019-03-08 14:18:50 +01001190 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001191
1192 waiting:
1193 /* waiting for the last bits to leave the buffer */
1194 if (req->flags & CF_SHUTW)
Christopher Faulet93e02d82019-03-08 14:18:50 +01001195 goto return_srv_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001196
1197 /* When TE: chunked is used, we need to get there again to parse remaining
1198 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
1199 * And when content-length is used, we never want to let the possible
1200 * shutdown be forwarded to the other side, as the state machine will
1201 * take care of it once the client responds. It's also important to
1202 * prevent TIME_WAITs from accumulating on the backend side, and for
1203 * HTTP/2 where the last frame comes with a shutdown.
1204 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001205 if (msg->flags & HTTP_MSGF_XFER_LEN)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001206 channel_dont_close(req);
1207
1208 /* We know that more data are expected, but we couldn't send more that
1209 * what we did. So we always set the CF_EXPECT_MORE flag so that the
1210 * system knows it must not set a PUSH on this first part. Interactive
1211 * modes are already handled by the stream sock layer. We must not do
1212 * this in content-length mode because it could present the MSG_MORE
1213 * flag with the last block of forwarded data, which would cause an
1214 * additional delay to be observed by the receiver.
1215 */
Christopher Faulet2151cdd2020-07-22 16:34:59 +02001216 if (HAS_REQ_DATA_FILTERS(s))
Christopher Faulete0768eb2018-10-03 16:38:02 +02001217 req->flags |= CF_EXPECT_MORE;
1218
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001219 DBG_TRACE_DEVEL("waiting for more data to forward",
1220 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001221 return 0;
1222
Christopher Faulet93e02d82019-03-08 14:18:50 +01001223 return_cli_abort:
Willy Tarreau4781b152021-04-06 13:53:36 +02001224 _HA_ATOMIC_INC(&sess->fe->fe_counters.cli_aborts);
1225 _HA_ATOMIC_INC(&s->be->be_counters.cli_aborts);
William Lallemand36119de2021-03-08 15:26:48 +01001226 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001227 _HA_ATOMIC_INC(&sess->listener->counters->cli_aborts);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001228 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001229 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.cli_aborts);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001230 if (!(s->flags & SF_ERR_MASK))
1231 s->flags |= SF_ERR_CLICL;
1232 status = 400;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001233 goto return_prx_cond;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001234
1235 return_srv_abort:
Willy Tarreau4781b152021-04-06 13:53:36 +02001236 _HA_ATOMIC_INC(&sess->fe->fe_counters.srv_aborts);
1237 _HA_ATOMIC_INC(&s->be->be_counters.srv_aborts);
William Lallemand36119de2021-03-08 15:26:48 +01001238 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001239 _HA_ATOMIC_INC(&sess->listener->counters->srv_aborts);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001240 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001241 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.srv_aborts);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001242 if (!(s->flags & SF_ERR_MASK))
1243 s->flags |= SF_ERR_SRVCL;
1244 status = 502;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001245 goto return_prx_cond;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001246
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001247 return_int_err:
1248 if (!(s->flags & SF_ERR_MASK))
1249 s->flags |= SF_ERR_INTERNAL;
Willy Tarreau4781b152021-04-06 13:53:36 +02001250 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
1251 _HA_ATOMIC_INC(&s->be->be_counters.internal_errors);
William Lallemand36119de2021-03-08 15:26:48 +01001252 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001253 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001254 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001255 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.internal_errors);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001256 status = 500;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001257 goto return_prx_cond;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001258
Christopher Faulet93e02d82019-03-08 14:18:50 +01001259 return_bad_req:
Willy Tarreau4781b152021-04-06 13:53:36 +02001260 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req);
William Lallemand36119de2021-03-08 15:26:48 +01001261 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001262 _HA_ATOMIC_INC(&sess->listener->counters->failed_req);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001263 status = 400;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001264 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001265
Christopher Fauletb8a53712019-12-16 11:29:38 +01001266 return_prx_cond:
Christopher Faulet9768c262018-10-22 09:34:31 +02001267 if (txn->status > 0) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001268 /* Note: we don't send any error if some data were already sent */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001269 http_reply_and_close(s, txn->status, NULL);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001270 } else {
Christopher Faulet93e02d82019-03-08 14:18:50 +01001271 txn->status = status;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001272 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001273 }
1274 req->analysers &= AN_REQ_FLT_END;
1275 s->res.analysers &= AN_RES_FLT_END; /* we're in data phase, we want to abort both directions */
Christopher Fauletb8a53712019-12-16 11:29:38 +01001276 if (!(s->flags & SF_ERR_MASK))
1277 s->flags |= SF_ERR_PRXCOND;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001278 if (!(s->flags & SF_FINST_MASK))
1279 s->flags |= ((txn->rsp.msg_state < HTTP_MSG_ERROR) ? SF_FINST_H : SF_FINST_D);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001280 DBG_TRACE_DEVEL("leaving on error ",
1281 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001282 return 0;
1283}
1284
Olivier Houcharda254a372019-04-05 15:30:12 +02001285/* Reset the stream and the backend stream_interface to a situation suitable for attemption connection */
1286/* Returns 0 if we can attempt to retry, -1 otherwise */
1287static __inline int do_l7_retry(struct stream *s, struct stream_interface *si)
1288{
Christopher Faulet5b82cc52020-10-12 15:18:50 +02001289 struct channel *req = &s->req;
1290 struct channel *res = &s->res;
Olivier Houcharda254a372019-04-05 15:30:12 +02001291
1292 si->conn_retries--;
1293 if (si->conn_retries < 0)
Christopher Faulet5b82cc52020-10-12 15:18:50 +02001294 goto no_retry;
1295
1296 if (b_is_null(&req->buf) && !channel_alloc_buffer(req, &s->buffer_wait))
1297 goto no_retry;
Olivier Houcharda254a372019-04-05 15:30:12 +02001298
Christopher Faulete763c8c2021-05-05 18:23:59 +02001299 if (objt_server(s->target)) {
1300 if (s->flags & SF_CURR_SESS) {
1301 s->flags &= ~SF_CURR_SESS;
1302 _HA_ATOMIC_DEC(&__objt_server(s->target)->cur_sess);
1303 }
Willy Tarreau4781b152021-04-06 13:53:36 +02001304 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.retries);
Christopher Faulete763c8c2021-05-05 18:23:59 +02001305 }
Willy Tarreau4781b152021-04-06 13:53:36 +02001306 _HA_ATOMIC_INC(&s->be->be_counters.retries);
Willy Tarreau223995e2019-05-04 10:38:31 +02001307
Olivier Houcharda254a372019-04-05 15:30:12 +02001308 /* Remove any write error from the request, and read error from the response */
1309 req->flags &= ~(CF_WRITE_ERROR | CF_WRITE_TIMEOUT | CF_SHUTW | CF_SHUTW_NOW);
1310 res->flags &= ~(CF_READ_ERROR | CF_READ_TIMEOUT | CF_SHUTR | CF_EOI | CF_READ_NULL | CF_SHUTR_NOW);
1311 res->analysers = 0;
1312 si->flags &= ~(SI_FL_ERR | SI_FL_EXP | SI_FL_RXBLK_SHUT);
Christopher Faulet30aa0da2021-05-05 21:05:09 +02001313 si->err_type = SI_ET_NONE;
1314 s->flags &= ~(SF_ERR_MASK | SF_FINST_MASK);
Olivier Houchard4bd58672019-07-12 16:16:59 +02001315 stream_choose_redispatch(s);
Olivier Houcharda254a372019-04-05 15:30:12 +02001316 si->exp = TICK_ETERNITY;
1317 res->rex = TICK_ETERNITY;
1318 res->to_forward = 0;
1319 res->analyse_exp = TICK_ETERNITY;
1320 res->total = 0;
Olivier Houcharda254a372019-04-05 15:30:12 +02001321 si_release_endpoint(&s->si[1]);
Olivier Houcharda254a372019-04-05 15:30:12 +02001322
Christopher Faulet5b82cc52020-10-12 15:18:50 +02001323 b_reset(&req->buf);
1324 memcpy(b_orig(&req->buf), b_orig(&si->l7_buffer), b_size(&si->l7_buffer));
1325 b_set_data(&req->buf, b_size(&req->buf));
1326 co_set_data(req, b_data(&si->l7_buffer));
1327
Ilya Shipitsinacf84592021-02-06 22:29:08 +05001328 DBG_TRACE_DEVEL("perform a L7 retry", STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, s->txn);
Olivier Houcharda254a372019-04-05 15:30:12 +02001329 b_reset(&res->buf);
1330 co_set_data(res, 0);
1331 return 0;
Christopher Faulet5b82cc52020-10-12 15:18:50 +02001332
1333 no_retry:
1334 b_free(&si->l7_buffer);
1335 return -1;
Olivier Houcharda254a372019-04-05 15:30:12 +02001336}
1337
Christopher Faulete0768eb2018-10-03 16:38:02 +02001338/* This stream analyser waits for a complete HTTP response. It returns 1 if the
1339 * processing can continue on next analysers, or zero if it either needs more
1340 * data or wants to immediately abort the response (eg: timeout, error, ...). It
1341 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->res.analysers
1342 * when it has nothing left to do, and may remove any analyser when it wants to
1343 * abort.
1344 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001345int http_wait_for_response(struct stream *s, struct channel *rep, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001346{
Christopher Faulet9768c262018-10-22 09:34:31 +02001347 /*
1348 * We will analyze a complete HTTP response to check the its syntax.
1349 *
1350 * Once the start line and all headers are received, we may perform a
1351 * capture of the error (if any), and we will set a few fields. We also
1352 * logging and finally headers capture.
1353 */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001354 struct session *sess = s->sess;
1355 struct http_txn *txn = s->txn;
1356 struct http_msg *msg = &txn->rsp;
Christopher Faulet9768c262018-10-22 09:34:31 +02001357 struct htx *htx;
Olivier Houcharda254a372019-04-05 15:30:12 +02001358 struct stream_interface *si_b = &s->si[1];
Christopher Faulet61608322018-11-23 16:23:45 +01001359 struct connection *srv_conn;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001360 struct htx_sl *sl;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001361 int n;
1362
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001363 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001364
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001365 htx = htxbuf(&rep->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001366
Willy Tarreau4236f032019-03-05 10:43:32 +01001367 /* Parsing errors are caught here */
1368 if (htx->flags & HTX_FL_PARSING_ERROR)
1369 goto return_bad_res;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001370 if (htx->flags & HTX_FL_PROCESSING_ERROR)
1371 goto return_int_err;
Willy Tarreau4236f032019-03-05 10:43:32 +01001372
Christopher Faulete0768eb2018-10-03 16:38:02 +02001373 /*
1374 * Now we quickly check if we have found a full valid response.
1375 * If not so, we check the FD and buffer states before leaving.
1376 * A full response is indicated by the fact that we have seen
1377 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
1378 * responses are checked first.
1379 *
1380 * Depending on whether the client is still there or not, we
1381 * may send an error response back or not. Note that normally
1382 * we should only check for HTTP status there, and check I/O
1383 * errors somewhere else.
1384 */
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001385 next_one:
Christopher Faulet29f17582019-05-23 11:03:26 +02001386 if (unlikely(htx_is_empty(htx) || htx->first == -1)) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001387 /* 1: have we encountered a read error ? */
1388 if (rep->flags & CF_READ_ERROR) {
Olivier Houchard865d8392019-05-03 22:46:27 +02001389 struct connection *conn = NULL;
1390
Olivier Houchard865d8392019-05-03 22:46:27 +02001391 if (objt_cs(s->si[1].end))
1392 conn = objt_cs(s->si[1].end)->conn;
1393
1394 if (si_b->flags & SI_FL_L7_RETRY &&
1395 (!conn || conn->err_code != CO_ER_SSL_EARLY_FAILED)) {
Olivier Houcharda254a372019-04-05 15:30:12 +02001396 /* If we arrive here, then CF_READ_ERROR was
1397 * set by si_cs_recv() because we matched a
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05001398 * status, otherwise it would have removed
Olivier Houcharda254a372019-04-05 15:30:12 +02001399 * the SI_FL_L7_RETRY flag, so it's ok not
1400 * to check s->be->retry_type.
1401 */
Christopher Faulet5b82cc52020-10-12 15:18:50 +02001402 if (co_data(rep) || do_l7_retry(s, si_b) == 0) {
1403 DBG_TRACE_DEVEL("leaving on L7 retry",
1404 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharda254a372019-04-05 15:30:12 +02001405 return 0;
Christopher Faulet5b82cc52020-10-12 15:18:50 +02001406 }
Olivier Houcharda254a372019-04-05 15:30:12 +02001407 }
1408
Olivier Houchard6db16992019-05-17 15:40:49 +02001409 if (txn->flags & TX_NOT_FIRST)
1410 goto abort_keep_alive;
1411
Willy Tarreau4781b152021-04-06 13:53:36 +02001412 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001413 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001414 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001415 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001416 }
1417
Christopher Faulete0768eb2018-10-03 16:38:02 +02001418 rep->analysers &= AN_RES_FLT_END;
1419 txn->status = 502;
1420
1421 /* Check to see if the server refused the early data.
1422 * If so, just send a 425
1423 */
Willy Tarreauee99aaf2020-06-23 05:58:20 +02001424 if (conn && conn->err_code == CO_ER_SSL_EARLY_FAILED) {
Olivier Houchard865d8392019-05-03 22:46:27 +02001425 if ((s->be->retry_type & PR_RE_EARLY_ERROR) &&
Olivier Houchardad26d8d2019-05-10 17:48:28 +02001426 (si_b->flags & SI_FL_L7_RETRY) &&
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001427 do_l7_retry(s, si_b) == 0) {
1428 DBG_TRACE_DEVEL("leaving on L7 retry",
1429 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houchard865d8392019-05-03 22:46:27 +02001430 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001431 }
Olivier Houchard865d8392019-05-03 22:46:27 +02001432 txn->status = 425;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001433 }
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001434 else
1435 stream_inc_http_fail_ctr(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001436
1437 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001438 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001439
1440 if (!(s->flags & SF_ERR_MASK))
1441 s->flags |= SF_ERR_SRVCL;
1442 if (!(s->flags & SF_FINST_MASK))
1443 s->flags |= SF_FINST_H;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001444 DBG_TRACE_DEVEL("leaving on error",
1445 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001446 return 0;
1447 }
1448
Christopher Faulet9768c262018-10-22 09:34:31 +02001449 /* 2: read timeout : return a 504 to the client. */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001450 else if (rep->flags & CF_READ_TIMEOUT) {
Olivier Houcharda254a372019-04-05 15:30:12 +02001451 if ((si_b->flags & SI_FL_L7_RETRY) &&
1452 (s->be->retry_type & PR_RE_TIMEOUT)) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001453 if (co_data(rep) || do_l7_retry(s, si_b) == 0) {
1454 DBG_TRACE_DEVEL("leaving on L7 retry",
1455 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharda254a372019-04-05 15:30:12 +02001456 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001457 }
Olivier Houcharda254a372019-04-05 15:30:12 +02001458 }
Willy Tarreau4781b152021-04-06 13:53:36 +02001459 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001460 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001461 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001462 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001463 }
1464
Christopher Faulete0768eb2018-10-03 16:38:02 +02001465 rep->analysers &= AN_RES_FLT_END;
1466 txn->status = 504;
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001467 stream_inc_http_fail_ctr(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001468 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001469 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001470
1471 if (!(s->flags & SF_ERR_MASK))
1472 s->flags |= SF_ERR_SRVTO;
1473 if (!(s->flags & SF_FINST_MASK))
1474 s->flags |= SF_FINST_H;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001475 DBG_TRACE_DEVEL("leaving on error",
1476 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001477 return 0;
1478 }
1479
Christopher Faulet9768c262018-10-22 09:34:31 +02001480 /* 3: client abort with an abortonclose */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001481 else if ((rep->flags & CF_SHUTR) && ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001482 _HA_ATOMIC_INC(&sess->fe->fe_counters.cli_aborts);
1483 _HA_ATOMIC_INC(&s->be->be_counters.cli_aborts);
William Lallemand36119de2021-03-08 15:26:48 +01001484 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001485 _HA_ATOMIC_INC(&sess->listener->counters->cli_aborts);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001486 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001487 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.cli_aborts);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001488
1489 rep->analysers &= AN_RES_FLT_END;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001490 txn->status = 400;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001491 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001492
1493 if (!(s->flags & SF_ERR_MASK))
1494 s->flags |= SF_ERR_CLICL;
1495 if (!(s->flags & SF_FINST_MASK))
1496 s->flags |= SF_FINST_H;
1497
1498 /* process_stream() will take care of the error */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001499 DBG_TRACE_DEVEL("leaving on error",
1500 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001501 return 0;
1502 }
1503
Christopher Faulet9768c262018-10-22 09:34:31 +02001504 /* 4: close from server, capture the response if the server has started to respond */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001505 else if (rep->flags & CF_SHUTR) {
Olivier Houcharda254a372019-04-05 15:30:12 +02001506 if ((si_b->flags & SI_FL_L7_RETRY) &&
1507 (s->be->retry_type & PR_RE_DISCONNECTED)) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001508 if (co_data(rep) || do_l7_retry(s, si_b) == 0) {
1509 DBG_TRACE_DEVEL("leaving on L7 retry",
1510 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharda254a372019-04-05 15:30:12 +02001511 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001512 }
Olivier Houcharda254a372019-04-05 15:30:12 +02001513 }
1514
Olivier Houchard6db16992019-05-17 15:40:49 +02001515 if (txn->flags & TX_NOT_FIRST)
1516 goto abort_keep_alive;
1517
Willy Tarreau4781b152021-04-06 13:53:36 +02001518 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001519 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001520 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001521 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001522 }
1523
Christopher Faulete0768eb2018-10-03 16:38:02 +02001524 rep->analysers &= AN_RES_FLT_END;
1525 txn->status = 502;
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001526 stream_inc_http_fail_ctr(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001527 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001528 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001529
1530 if (!(s->flags & SF_ERR_MASK))
1531 s->flags |= SF_ERR_SRVCL;
1532 if (!(s->flags & SF_FINST_MASK))
1533 s->flags |= SF_FINST_H;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001534 DBG_TRACE_DEVEL("leaving on error",
1535 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001536 return 0;
1537 }
1538
Christopher Faulet9768c262018-10-22 09:34:31 +02001539 /* 5: write error to client (we don't send any message then) */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001540 else if (rep->flags & CF_WRITE_ERROR) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001541 if (txn->flags & TX_NOT_FIRST)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001542 goto abort_keep_alive;
1543
Willy Tarreau4781b152021-04-06 13:53:36 +02001544 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001545 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001546 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001547 rep->analysers &= AN_RES_FLT_END;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001548
1549 if (!(s->flags & SF_ERR_MASK))
1550 s->flags |= SF_ERR_CLICL;
1551 if (!(s->flags & SF_FINST_MASK))
1552 s->flags |= SF_FINST_H;
1553
1554 /* process_stream() will take care of the error */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001555 DBG_TRACE_DEVEL("leaving on error",
1556 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001557 return 0;
1558 }
1559
1560 channel_dont_close(rep);
1561 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001562 DBG_TRACE_DEVEL("waiting for more data",
1563 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001564 return 0;
1565 }
1566
1567 /* More interesting part now : we know that we have a complete
1568 * response which at least looks like HTTP. We have an indicator
1569 * of each header's length, so we can parse them quickly.
1570 */
Christopher Faulet29f17582019-05-23 11:03:26 +02001571 BUG_ON(htx_get_first_type(htx) != HTX_BLK_RES_SL);
Christopher Faulet297fbb42019-05-13 14:41:27 +02001572 sl = http_get_stline(htx);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001573
Christopher Faulet5b82cc52020-10-12 15:18:50 +02001574 if ((si_b->flags & SI_FL_L7_RETRY) &&
1575 l7_status_match(s->be, sl->info.res.status) &&
1576 do_l7_retry(s, si_b) == 0) {
1577 DBG_TRACE_DEVEL("leaving on L7 retry", STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
1578 return 0;
1579 }
1580 b_free(&s->si[1].l7_buffer);
1581
1582 msg->msg_state = HTTP_MSG_BODY;
1583
Christopher Faulet9768c262018-10-22 09:34:31 +02001584 /* 0: we might have to print this header in debug mode */
1585 if (unlikely((global.mode & MODE_DEBUG) &&
1586 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
1587 int32_t pos;
1588
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001589 http_debug_stline("srvrep", s, sl);
Christopher Faulet9768c262018-10-22 09:34:31 +02001590
Christopher Fauleta3f15502019-05-13 15:27:23 +02001591 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001592 struct htx_blk *blk = htx_get_blk(htx, pos);
1593 enum htx_blk_type type = htx_get_blk_type(blk);
1594
1595 if (type == HTX_BLK_EOH)
1596 break;
1597 if (type != HTX_BLK_HDR)
1598 continue;
1599
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001600 http_debug_hdr("srvhdr", s,
1601 htx_get_blk_name(htx, blk),
1602 htx_get_blk_value(htx, blk));
Christopher Faulet9768c262018-10-22 09:34:31 +02001603 }
1604 }
1605
Christopher Faulet03599112018-11-27 11:21:21 +01001606 /* 1: get the status code and the version. Also set HTTP flags */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001607 txn->status = sl->info.res.status;
Christopher Faulet03599112018-11-27 11:21:21 +01001608 if (sl->flags & HTX_SL_F_VER_11)
Christopher Faulet9768c262018-10-22 09:34:31 +02001609 msg->flags |= HTTP_MSGF_VER_11;
Christopher Faulet03599112018-11-27 11:21:21 +01001610 if (sl->flags & HTX_SL_F_XFER_LEN) {
1611 msg->flags |= HTTP_MSGF_XFER_LEN;
Christopher Faulet2a408542020-11-20 14:22:37 +01001612 if (sl->flags & HTX_SL_F_CLEN)
1613 msg->flags |= HTTP_MSGF_CNT_LEN;
1614 else if (sl->flags & HTX_SL_F_CHNK)
1615 msg->flags |= HTTP_MSGF_TE_CHNK;
Christopher Faulet03599112018-11-27 11:21:21 +01001616 }
Christopher Faulet2a408542020-11-20 14:22:37 +01001617 if (sl->flags & HTX_SL_F_BODYLESS)
1618 msg->flags |= HTTP_MSGF_BODYLESS;
Christopher Faulet576c3582021-01-08 15:53:01 +01001619 if (sl->flags & HTX_SL_F_CONN_UPG)
1620 msg->flags |= HTTP_MSGF_CONN_UPG;
Christopher Faulet9768c262018-10-22 09:34:31 +02001621
1622 n = txn->status / 100;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001623 if (n < 1 || n > 5)
1624 n = 0;
Christopher Faulet9768c262018-10-22 09:34:31 +02001625
Christopher Faulete0768eb2018-10-03 16:38:02 +02001626 /* when the client triggers a 4xx from the server, it's most often due
1627 * to a missing object or permission. These events should be tracked
1628 * because if they happen often, it may indicate a brute force or a
1629 * vulnerability scan.
1630 */
1631 if (n == 4)
1632 stream_inc_http_err_ctr(s);
1633
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001634 if (n == 5 && txn->status != 501 && txn->status != 505)
1635 stream_inc_http_fail_ctr(s);
1636
Marcin Deranek3c27dda2020-05-15 18:32:51 +02001637 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001638 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.p.http.rsp[n]);
1639 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.p.http.cum_req);
Marcin Deranek3c27dda2020-05-15 18:32:51 +02001640 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001641
Christopher Faulete0768eb2018-10-03 16:38:02 +02001642 /* Adjust server's health based on status code. Note: status codes 501
1643 * and 505 are triggered on demand by client request, so we must not
1644 * count them as server failures.
1645 */
1646 if (objt_server(s->target)) {
1647 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001648 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_OK);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001649 else
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001650 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_STS);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001651 }
1652
1653 /*
1654 * We may be facing a 100-continue response, or any other informational
1655 * 1xx response which is non-final, in which case this is not the right
1656 * response, and we're waiting for the next one. Let's allow this response
1657 * to go to the client and wait for the next one. There's an exception for
1658 * 101 which is used later in the code to switch protocols.
1659 */
1660 if (txn->status < 200 &&
1661 (txn->status == 100 || txn->status >= 102)) {
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001662 FLT_STRM_CB(s, flt_http_reset(s, msg));
Christopher Faulet421e7692019-06-13 11:16:45 +02001663 htx->first = channel_htx_fwd_headers(rep, htx);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001664 msg->msg_state = HTTP_MSG_RPBEFORE;
Christopher Faulet3499f622019-09-03 15:23:54 +02001665 msg->flags = 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001666 txn->status = 0;
1667 s->logs.t_data = -1; /* was not a response yet */
Christopher Faulet7d518452020-08-31 11:07:07 +02001668 rep->flags |= CF_SEND_DONTWAIT; /* Send ASAP informational messages */
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001669 goto next_one;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001670 }
1671
Christopher Faulet6e6c7b12021-01-08 16:02:05 +01001672 /* A 101-switching-protocols must contains a Connection header with the
1673 * "upgrade" option and the request too. It means both are agree to
1674 * upgrade. It is not so strict because there is no test on the Upgrade
1675 * header content. But it is probably stronger enough for now.
1676 */
1677 if (txn->status == 101 &&
1678 (!(txn->req.flags & HTTP_MSGF_CONN_UPG) || !(txn->rsp.flags & HTTP_MSGF_CONN_UPG)))
1679 goto return_bad_res;
1680
Christopher Faulete0768eb2018-10-03 16:38:02 +02001681 /*
1682 * 2: check for cacheability.
1683 */
1684
1685 switch (txn->status) {
1686 case 200:
1687 case 203:
1688 case 204:
1689 case 206:
1690 case 300:
1691 case 301:
1692 case 404:
1693 case 405:
1694 case 410:
1695 case 414:
1696 case 501:
1697 break;
1698 default:
1699 /* RFC7231#6.1:
1700 * Responses with status codes that are defined as
1701 * cacheable by default (e.g., 200, 203, 204, 206,
1702 * 300, 301, 404, 405, 410, 414, and 501 in this
1703 * specification) can be reused by a cache with
1704 * heuristic expiration unless otherwise indicated
1705 * by the method definition or explicit cache
1706 * controls [RFC7234]; all other status codes are
1707 * not cacheable by default.
1708 */
1709 txn->flags &= ~(TX_CACHEABLE | TX_CACHE_COOK);
1710 break;
1711 }
1712
1713 /*
1714 * 3: we may need to capture headers
1715 */
1716 s->logs.logwait &= ~LW_RESP;
1717 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->res_cap))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001718 http_capture_headers(htx, s->res_cap, sess->fe->rsp_cap);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001719
Christopher Faulet9768c262018-10-22 09:34:31 +02001720 /* Skip parsing if no content length is possible. */
Christopher Fauletc75668e2020-12-07 18:10:32 +01001721 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status >= 200 && txn->status < 300) ||
Christopher Faulete0768eb2018-10-03 16:38:02 +02001722 txn->status == 101)) {
1723 /* Either we've established an explicit tunnel, or we're
1724 * switching the protocol. In both cases, we're very unlikely
1725 * to understand the next protocols. We have to switch to tunnel
1726 * mode, so that we transfer the request and responses then let
1727 * this protocol pass unmodified. When we later implement specific
1728 * parsers for such protocols, we'll want to check the Upgrade
1729 * header which contains information about that protocol for
1730 * responses with status 101 (eg: see RFC2817 about TLS).
1731 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02001732 txn->flags |= TX_CON_WANT_TUN;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001733 }
1734
Christopher Faulet61608322018-11-23 16:23:45 +01001735 /* check for NTML authentication headers in 401 (WWW-Authenticate) and
1736 * 407 (Proxy-Authenticate) responses and set the connection to private
1737 */
1738 srv_conn = cs_conn(objt_cs(s->si[1].end));
1739 if (srv_conn) {
1740 struct ist hdr;
1741 struct http_hdr_ctx ctx;
1742
1743 if (txn->status == 401)
1744 hdr = ist("WWW-Authenticate");
1745 else if (txn->status == 407)
1746 hdr = ist("Proxy-Authenticate");
1747 else
1748 goto end;
1749
1750 ctx.blk = NULL;
1751 while (http_find_header(htx, hdr, &ctx, 0)) {
Willy Tarreauf1dcced2020-05-07 19:27:02 +02001752 /* If www-authenticate contains "Negotiate", "Nego2", or "NTLM",
1753 * possibly followed by blanks and a base64 string, the connection
1754 * is private. Since it's a mess to deal with, we only check for
1755 * values starting with "NTLM" or "Nego". Note that often multiple
1756 * headers are sent by the server there.
1757 */
1758 if ((ctx.value.len >= 4 && strncasecmp(ctx.value.ptr, "Nego", 4) == 0) ||
Willy Tarreau49a1d282020-05-07 19:10:15 +02001759 (ctx.value.len >= 4 && strncasecmp(ctx.value.ptr, "NTLM", 4) == 0)) {
Olivier Houchard250031e2019-05-29 15:01:50 +02001760 sess->flags |= SESS_FL_PREFER_LAST;
Christopher Faulet08016ab2020-07-01 16:10:06 +02001761 conn_set_owner(srv_conn, sess, NULL);
Christopher Faulet21ddc742020-07-01 15:26:14 +02001762 conn_set_private(srv_conn);
Ilya Shipitsin6b79f382020-07-23 00:32:55 +05001763 /* If it fail now, the same will be done in mux->detach() callback */
Christopher Faulet08016ab2020-07-01 16:10:06 +02001764 session_add_conn(srv_conn->owner, srv_conn, srv_conn->target);
Willy Tarreauf1dcced2020-05-07 19:27:02 +02001765 break;
Olivier Houchard250031e2019-05-29 15:01:50 +02001766 }
Christopher Faulet61608322018-11-23 16:23:45 +01001767 }
1768 }
1769
1770 end:
Christopher Faulete0768eb2018-10-03 16:38:02 +02001771 /* we want to have the response time before we start processing it */
1772 s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now);
1773
1774 /* end of job, return OK */
1775 rep->analysers &= ~an_bit;
1776 rep->analyse_exp = TICK_ETERNITY;
1777 channel_auto_close(rep);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001778 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001779 return 1;
1780
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001781 return_int_err:
Willy Tarreau4781b152021-04-06 13:53:36 +02001782 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
1783 _HA_ATOMIC_INC(&s->be->be_counters.internal_errors);
William Lallemand36119de2021-03-08 15:26:48 +01001784 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001785 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Fauletb8a53712019-12-16 11:29:38 +01001786 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001787 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.internal_errors);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001788 txn->status = 500;
1789 if (!(s->flags & SF_ERR_MASK))
1790 s->flags |= SF_ERR_INTERNAL;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001791 goto return_prx_cond;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001792
1793 return_bad_res:
Willy Tarreau4781b152021-04-06 13:53:36 +02001794 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Faulet47365272018-10-31 17:40:50 +01001795 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001796 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001797 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Christopher Faulet47365272018-10-31 17:40:50 +01001798 }
Olivier Houcharde3249a92019-05-03 23:01:47 +02001799 if ((s->be->retry_type & PR_RE_JUNK_REQUEST) &&
Olivier Houchardad26d8d2019-05-10 17:48:28 +02001800 (si_b->flags & SI_FL_L7_RETRY) &&
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001801 do_l7_retry(s, si_b) == 0) {
1802 DBG_TRACE_DEVEL("leaving on L7 retry",
1803 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharde3249a92019-05-03 23:01:47 +02001804 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001805 }
Christopher Faulet47365272018-10-31 17:40:50 +01001806 txn->status = 502;
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001807 stream_inc_http_fail_ctr(s);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001808 /* fall through */
1809
Christopher Fauletb8a53712019-12-16 11:29:38 +01001810 return_prx_cond:
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001811 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet47365272018-10-31 17:40:50 +01001812
1813 if (!(s->flags & SF_ERR_MASK))
1814 s->flags |= SF_ERR_PRXCOND;
1815 if (!(s->flags & SF_FINST_MASK))
1816 s->flags |= SF_FINST_H;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001817
1818 s->si[1].flags |= SI_FL_NOLINGER;
1819 rep->analysers &= AN_RES_FLT_END;
Christopher Faulete58c0002020-03-02 16:21:01 +01001820 s->req.analysers &= AN_REQ_FLT_END;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001821 rep->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001822 DBG_TRACE_DEVEL("leaving on error",
1823 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulet47365272018-10-31 17:40:50 +01001824 return 0;
1825
Christopher Faulete0768eb2018-10-03 16:38:02 +02001826 abort_keep_alive:
1827 /* A keep-alive request to the server failed on a network error.
1828 * The client is required to retry. We need to close without returning
1829 * any other information so that the client retries.
1830 */
1831 txn->status = 0;
1832 rep->analysers &= AN_RES_FLT_END;
1833 s->req.analysers &= AN_REQ_FLT_END;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001834 s->logs.logwait = 0;
1835 s->logs.level = 0;
1836 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001837 http_reply_and_close(s, txn->status, NULL);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001838 DBG_TRACE_DEVEL("leaving by closing K/A connection",
1839 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001840 return 0;
1841}
1842
1843/* This function performs all the processing enabled for the current response.
1844 * It normally returns 1 unless it wants to break. It relies on buffers flags,
1845 * and updates s->res.analysers. It might make sense to explode it into several
1846 * other functions. It works like process_request (see indications above).
1847 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001848int http_process_res_common(struct stream *s, struct channel *rep, int an_bit, struct proxy *px)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001849{
1850 struct session *sess = s->sess;
1851 struct http_txn *txn = s->txn;
1852 struct http_msg *msg = &txn->rsp;
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001853 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001854 struct proxy *cur_proxy;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001855 enum rule_result ret = HTTP_RULE_RES_CONT;
1856
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001857 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
1858 return 0;
Christopher Faulet9768c262018-10-22 09:34:31 +02001859
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001860 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001861
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001862 htx = htxbuf(&rep->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001863
1864 /* The stats applet needs to adjust the Connection header but we don't
1865 * apply any filter there.
1866 */
1867 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
1868 rep->analysers &= ~an_bit;
1869 rep->analyse_exp = TICK_ETERNITY;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001870 goto end;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001871 }
1872
1873 /*
1874 * We will have to evaluate the filters.
1875 * As opposed to version 1.2, now they will be evaluated in the
1876 * filters order and not in the header order. This means that
1877 * each filter has to be validated among all headers.
1878 *
1879 * Filters are tried with ->be first, then with ->fe if it is
1880 * different from ->be.
1881 *
1882 * Maybe we are in resume condiion. In this case I choose the
1883 * "struct proxy" which contains the rule list matching the resume
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05001884 * pointer. If none of these "struct proxy" match, I initialise
Christopher Faulete0768eb2018-10-03 16:38:02 +02001885 * the process with the first one.
1886 *
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05001887 * In fact, I check only correspondence between the current list
Christopher Faulete0768eb2018-10-03 16:38:02 +02001888 * pointer and the ->fe rule list. If it doesn't match, I initialize
1889 * the loop with the ->be.
1890 */
1891 if (s->current_rule_list == &sess->fe->http_res_rules)
1892 cur_proxy = sess->fe;
1893 else
1894 cur_proxy = s->be;
1895 while (1) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001896 /* evaluate http-response rules */
1897 if (ret == HTTP_RULE_RES_CONT) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001898 ret = http_res_get_intercept_rule(cur_proxy, &cur_proxy->http_res_rules, s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001899
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001900 switch (ret) {
1901 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
1902 goto return_prx_yield;
1903
1904 case HTTP_RULE_RES_CONT:
1905 case HTTP_RULE_RES_STOP: /* nothing to do */
1906 break;
1907
1908 case HTTP_RULE_RES_DENY: /* deny or tarpit */
1909 goto deny;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001910
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001911 case HTTP_RULE_RES_ABRT: /* abort request, response already sent */
1912 goto return_prx_cond;
1913
1914 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
Christopher Fauletb8a53712019-12-16 11:29:38 +01001915 goto done;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001916
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001917 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
1918 goto return_bad_res;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001919
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001920 case HTTP_RULE_RES_ERROR: /* failed with a bad request */
1921 goto return_int_err;
1922 }
1923
1924 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001925
Christopher Faulete0768eb2018-10-03 16:38:02 +02001926 /* check whether we're already working on the frontend */
1927 if (cur_proxy == sess->fe)
1928 break;
1929 cur_proxy = sess->fe;
1930 }
1931
Christopher Faulete0768eb2018-10-03 16:38:02 +02001932 /* OK that's all we can do for 1xx responses */
1933 if (unlikely(txn->status < 200 && txn->status != 101))
Christopher Fauletf2824e62018-10-01 12:12:37 +02001934 goto end;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001935
1936 /*
1937 * Now check for a server cookie.
1938 */
1939 if (s->be->cookie_name || sess->fe->capture_name || (s->be->options & PR_O_CHK_CACHE))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001940 http_manage_server_side_cookies(s, rep);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001941
1942 /*
1943 * Check for cache-control or pragma headers if required.
1944 */
1945 if ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001946 http_check_response_for_cacheability(s, rep);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001947
1948 /*
1949 * Add server cookie in the response if needed
1950 */
1951 if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) &&
1952 !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) &&
1953 (!(s->flags & SF_DIRECT) ||
1954 ((s->be->cookie_maxidle || txn->cookie_last_date) &&
1955 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
1956 (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
1957 (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
1958 (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
1959 !(s->flags & SF_IGNORE_PRST)) {
1960 /* the server is known, it's not the one the client requested, or the
1961 * cookie's last seen date needs to be refreshed. We have to
1962 * insert a set-cookie here, except if we want to insert only on POST
1963 * requests and this one isn't. Note that servers which don't have cookies
1964 * (eg: some backup servers) will return a full cookie removal request.
1965 */
1966 if (!objt_server(s->target)->cookie) {
1967 chunk_printf(&trash,
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001968 "%s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
Christopher Faulete0768eb2018-10-03 16:38:02 +02001969 s->be->cookie_name);
1970 }
1971 else {
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001972 chunk_printf(&trash, "%s=%s", s->be->cookie_name, objt_server(s->target)->cookie);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001973
1974 if (s->be->cookie_maxidle || s->be->cookie_maxlife) {
1975 /* emit last_date, which is mandatory */
1976 trash.area[trash.data++] = COOKIE_DELIM_DATE;
1977 s30tob64((date.tv_sec+3) >> 2,
1978 trash.area + trash.data);
1979 trash.data += 5;
1980
1981 if (s->be->cookie_maxlife) {
1982 /* emit first_date, which is either the original one or
1983 * the current date.
1984 */
1985 trash.area[trash.data++] = COOKIE_DELIM_DATE;
1986 s30tob64(txn->cookie_first_date ?
1987 txn->cookie_first_date >> 2 :
1988 (date.tv_sec+3) >> 2,
1989 trash.area + trash.data);
1990 trash.data += 5;
1991 }
1992 }
1993 chunk_appendf(&trash, "; path=/");
1994 }
1995
1996 if (s->be->cookie_domain)
1997 chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain);
1998
1999 if (s->be->ck_opts & PR_CK_HTTPONLY)
2000 chunk_appendf(&trash, "; HttpOnly");
2001
2002 if (s->be->ck_opts & PR_CK_SECURE)
2003 chunk_appendf(&trash, "; Secure");
2004
Christopher Faulet2f533902020-01-21 11:06:48 +01002005 if (s->be->cookie_attrs)
2006 chunk_appendf(&trash, "; %s", s->be->cookie_attrs);
2007
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002008 if (unlikely(!http_add_header(htx, ist("Set-Cookie"), ist2(trash.area, trash.data))))
Christopher Fauletb8a53712019-12-16 11:29:38 +01002009 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002010
2011 txn->flags &= ~TX_SCK_MASK;
2012 if (__objt_server(s->target)->cookie && (s->flags & SF_DIRECT))
2013 /* the server did not change, only the date was updated */
2014 txn->flags |= TX_SCK_UPDATED;
2015 else
2016 txn->flags |= TX_SCK_INSERTED;
2017
2018 /* Here, we will tell an eventual cache on the client side that we don't
2019 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
2020 * Some caches understand the correct form: 'no-cache="set-cookie"', but
2021 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
2022 */
2023 if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
2024
2025 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
2026
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002027 if (unlikely(!http_add_header(htx, ist("Cache-control"), ist("private"))))
Christopher Fauletb8a53712019-12-16 11:29:38 +01002028 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002029 }
2030 }
2031
2032 /*
2033 * Check if result will be cacheable with a cookie.
2034 * We'll block the response if security checks have caught
2035 * nasty things such as a cacheable cookie.
2036 */
2037 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
2038 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
2039 (s->be->options & PR_O_CHK_CACHE)) {
2040 /* we're in presence of a cacheable response containing
2041 * a set-cookie header. We'll block it as requested by
2042 * the 'checkcache' option, and send an alert.
2043 */
Christopher Faulete0768eb2018-10-03 16:38:02 +02002044 ha_alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
2045 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
2046 send_log(s->be, LOG_ALERT,
2047 "Blocking cacheable cookie in response from instance %s, server %s.\n",
2048 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
Christopher Fauletb8a53712019-12-16 11:29:38 +01002049 goto deny;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002050 }
2051
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002052 end:
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002053 /*
2054 * Evaluate after-response rules before forwarding the response. rules
2055 * from the backend are evaluated first, then one from the frontend if
2056 * it differs.
2057 */
2058 if (!http_eval_after_res_rules(s))
2059 goto return_int_err;
2060
Christopher Fauletc2ac5e42021-03-08 18:20:09 +01002061 /* Filter the response headers if there are filters attached to the
2062 * stream.
2063 */
2064 if (HAS_FILTERS(s))
2065 rep->analysers |= AN_RES_FLT_HTTP_HDRS;
2066
Christopher Faulete0768eb2018-10-03 16:38:02 +02002067 /* Always enter in the body analyzer */
2068 rep->analysers &= ~AN_RES_FLT_XFER_DATA;
2069 rep->analysers |= AN_RES_HTTP_XFER_BODY;
2070
2071 /* if the user wants to log as soon as possible, without counting
2072 * bytes from the server, then this is the right moment. We have
2073 * to temporarily assign bytes_out to log what we currently have.
2074 */
2075 if (!LIST_ISEMPTY(&sess->fe->logformat) && !(s->logs.logwait & LW_BYTES)) {
2076 s->logs.t_close = s->logs.t_data; /* to get a valid end date */
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002077 s->logs.bytes_out = htx->data;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002078 s->do_log(s);
2079 s->logs.bytes_out = 0;
2080 }
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002081
Christopher Fauletb8a53712019-12-16 11:29:38 +01002082 done:
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002083 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002084 rep->analysers &= ~an_bit;
2085 rep->analyse_exp = TICK_ETERNITY;
2086 return 1;
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002087
Christopher Fauletb8a53712019-12-16 11:29:38 +01002088 deny:
Willy Tarreau4781b152021-04-06 13:53:36 +02002089 _HA_ATOMIC_INC(&sess->fe->fe_counters.denied_resp);
2090 _HA_ATOMIC_INC(&s->be->be_counters.denied_resp);
William Lallemand36119de2021-03-08 15:26:48 +01002091 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02002092 _HA_ATOMIC_INC(&sess->listener->counters->denied_resp);
Christopher Fauleta08546b2019-12-16 16:07:34 +01002093 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02002094 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.denied_resp);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002095 goto return_prx_err;
2096
2097 return_int_err:
2098 txn->status = 500;
2099 if (!(s->flags & SF_ERR_MASK))
2100 s->flags |= SF_ERR_INTERNAL;
Willy Tarreau4781b152021-04-06 13:53:36 +02002101 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
2102 _HA_ATOMIC_INC(&s->be->be_counters.internal_errors);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002103 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02002104 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.internal_errors);
Christopher Fauletcff0f732019-12-16 16:13:44 +01002105 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02002106 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.internal_errors);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002107 goto return_prx_err;
2108
2109 return_bad_res:
2110 txn->status = 502;
Willy Tarreau826f3ab2021-02-10 12:07:15 +01002111 stream_inc_http_fail_ctr(s);
Willy Tarreau4781b152021-04-06 13:53:36 +02002112 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Fauleta20a6532020-02-05 10:16:41 +01002113 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02002114 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Christopher Fauleta20a6532020-02-05 10:16:41 +01002115 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_RSP);
2116 }
Christopher Fauletb8a53712019-12-16 11:29:38 +01002117 /* fall through */
2118
2119 return_prx_err:
2120 http_reply_and_close(s, txn->status, http_error_message(s));
2121 /* fall through */
2122
2123 return_prx_cond:
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002124 s->logs.t_data = -1; /* was not a valid response */
2125 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002126
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002127 if (!(s->flags & SF_ERR_MASK))
2128 s->flags |= SF_ERR_PRXCOND;
2129 if (!(s->flags & SF_FINST_MASK))
2130 s->flags |= SF_FINST_H;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002131
Christopher Faulete58c0002020-03-02 16:21:01 +01002132 rep->analysers &= AN_RES_FLT_END;
2133 s->req.analysers &= AN_REQ_FLT_END;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002134 rep->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002135 DBG_TRACE_DEVEL("leaving on error",
2136 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002137 return 0;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002138
2139 return_prx_yield:
2140 channel_dont_close(rep);
2141 DBG_TRACE_DEVEL("waiting for more data",
2142 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
2143 return 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002144}
2145
2146/* This function is an analyser which forwards response body (including chunk
2147 * sizes if any). It is called as soon as we must forward, even if we forward
2148 * zero byte. The only situation where it must not be called is when we're in
2149 * tunnel mode and we want to forward till the close. It's used both to forward
2150 * remaining data and to resync after end of body. It expects the msg_state to
2151 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
2152 * read more data, or 1 once we can go on with next request or end the stream.
2153 *
2154 * It is capable of compressing response data both in content-length mode and
2155 * in chunked mode. The state machines follows different flows depending on
2156 * whether content-length and chunked modes are used, since there are no
2157 * trailers in content-length :
2158 *
2159 * chk-mode cl-mode
2160 * ,----- BODY -----.
2161 * / \
2162 * V size > 0 V chk-mode
2163 * .--> SIZE -------------> DATA -------------> CRLF
2164 * | | size == 0 | last byte |
2165 * | v final crlf v inspected |
2166 * | TRAILERS -----------> DONE |
2167 * | |
2168 * `----------------------------------------------'
2169 *
2170 * Compression only happens in the DATA state, and must be flushed in final
2171 * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding
2172 * is performed at once on final states for all bytes parsed, or when leaving
2173 * on missing data.
2174 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002175int http_response_forward_body(struct stream *s, struct channel *res, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +02002176{
2177 struct session *sess = s->sess;
2178 struct http_txn *txn = s->txn;
2179 struct http_msg *msg = &s->txn->rsp;
Christopher Faulet9768c262018-10-22 09:34:31 +02002180 struct htx *htx;
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002181 int ret;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002182
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002183 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002184
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002185 htx = htxbuf(&res->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002186
Christopher Fauletb9a92f32019-09-09 10:15:21 +02002187 if (htx->flags & HTX_FL_PARSING_ERROR)
2188 goto return_bad_res;
2189 if (htx->flags & HTX_FL_PROCESSING_ERROR)
2190 goto return_int_err;
2191
Christopher Faulete0768eb2018-10-03 16:38:02 +02002192 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Christopher Fauletf2824e62018-10-01 12:12:37 +02002193 ((res->flags & CF_SHUTW) && (res->to_forward || co_data(res)))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02002194 /* Output closed while we were sending data. We must abort and
2195 * wake the other side up.
2196 */
Christopher Faulete0768eb2018-10-03 16:38:02 +02002197 msg->msg_state = HTTP_MSG_ERROR;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002198 http_end_response(s);
2199 http_end_request(s);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002200 DBG_TRACE_DEVEL("leaving on error",
2201 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002202 return 1;
2203 }
2204
Christopher Faulet9768c262018-10-22 09:34:31 +02002205 if (msg->msg_state == HTTP_MSG_BODY)
2206 msg->msg_state = HTTP_MSG_DATA;
2207
Christopher Faulete0768eb2018-10-03 16:38:02 +02002208 /* in most states, we should abort in case of early close */
2209 channel_auto_close(res);
2210
Christopher Faulete0768eb2018-10-03 16:38:02 +02002211 if (res->to_forward) {
Christopher Faulet66af0b22019-03-22 14:54:52 +01002212 if (res->to_forward == CHN_INFINITE_FORWARD) {
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002213 if (res->flags & CF_EOI)
2214 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet66af0b22019-03-22 14:54:52 +01002215 }
2216 else {
2217 /* We can't process the buffer's contents yet */
2218 res->flags |= CF_WAKE_WRITE;
2219 goto missing_data_or_waiting;
2220 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02002221 }
2222
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002223 if (msg->msg_state >= HTTP_MSG_ENDING)
2224 goto ending;
2225
Christopher Fauletc75668e2020-12-07 18:10:32 +01002226 if ((txn->meth == HTTP_METH_CONNECT && txn->status >= 200 && txn->status < 300) || txn->status == 101 ||
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002227 (!(msg->flags & HTTP_MSGF_XFER_LEN) && !HAS_RSP_DATA_FILTERS(s))) {
2228 msg->msg_state = HTTP_MSG_ENDING;
2229 goto ending;
2230 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002231
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002232 /* Forward input data. We get it by removing all outgoing data not
2233 * forwarded yet from HTX data size. If there are some data filters, we
2234 * let them decide the amount of data to forward.
Christopher Faulet9768c262018-10-22 09:34:31 +02002235 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002236 if (HAS_RSP_DATA_FILTERS(s)) {
2237 ret = flt_http_payload(s, msg, htx->data);
2238 if (ret < 0)
2239 goto return_bad_res;
Christopher Faulet421e7692019-06-13 11:16:45 +02002240 c_adv(res, ret);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002241 }
2242 else {
Christopher Faulet421e7692019-06-13 11:16:45 +02002243 c_adv(res, htx->data - co_data(res));
Christopher Faulet66af0b22019-03-22 14:54:52 +01002244 if (msg->flags & HTTP_MSGF_XFER_LEN)
2245 channel_htx_forward_forever(res, htx);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002246 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002247
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002248 if (htx->data != co_data(res))
2249 goto missing_data_or_waiting;
2250
2251 if (!(msg->flags & HTTP_MSGF_XFER_LEN) && res->flags & CF_SHUTR) {
2252 msg->msg_state = HTTP_MSG_ENDING;
2253 goto ending;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002254 }
2255
Christopher Faulet9768c262018-10-22 09:34:31 +02002256 /* Check if the end-of-message is reached and if so, switch the message
Christopher Fauletd20fdb02019-06-13 16:43:22 +02002257 * in HTTP_MSG_ENDING state. Then if all data was marked to be
2258 * forwarded, set the state to HTTP_MSG_DONE.
Christopher Faulet9768c262018-10-22 09:34:31 +02002259 */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002260 if (!(htx->flags & HTX_FL_EOM))
Christopher Faulet9768c262018-10-22 09:34:31 +02002261 goto missing_data_or_waiting;
2262
Christopher Fauletd20fdb02019-06-13 16:43:22 +02002263 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet9768c262018-10-22 09:34:31 +02002264
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002265 ending:
Christopher Faulet2151cdd2020-07-22 16:34:59 +02002266 res->flags &= ~CF_EXPECT_MORE; /* no more data are expected */
2267
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002268 /* other states, ENDING...TUNNEL */
2269 if (msg->msg_state >= HTTP_MSG_DONE)
2270 goto done;
Christopher Faulet9768c262018-10-22 09:34:31 +02002271
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002272 if (HAS_RSP_DATA_FILTERS(s)) {
2273 ret = flt_http_end(s, msg);
2274 if (ret <= 0) {
2275 if (!ret)
2276 goto missing_data_or_waiting;
2277 goto return_bad_res;
2278 }
2279 }
2280
Christopher Fauletc75668e2020-12-07 18:10:32 +01002281 if ((txn->meth == HTTP_METH_CONNECT && txn->status >= 200 && txn->status < 300) || txn->status == 101 ||
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002282 !(msg->flags & HTTP_MSGF_XFER_LEN)) {
2283 msg->msg_state = HTTP_MSG_TUNNEL;
2284 goto ending;
2285 }
2286 else {
2287 msg->msg_state = HTTP_MSG_DONE;
2288 res->to_forward = 0;
2289 }
2290
2291 done:
2292
2293 channel_dont_close(res);
2294
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002295 http_end_response(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002296 if (!(res->analysers & an_bit)) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002297 http_end_request(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002298 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
2299 if (res->flags & CF_SHUTW) {
2300 /* response errors are most likely due to the
2301 * client aborting the transfer. */
Christopher Faulet93e02d82019-03-08 14:18:50 +01002302 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002303 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02002304 goto return_bad_res;
2305 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002306 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002307 return 1;
2308 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002309 DBG_TRACE_DEVEL("waiting for the end of the HTTP txn",
2310 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002311 return 0;
2312
2313 missing_data_or_waiting:
2314 if (res->flags & CF_SHUTW)
Christopher Faulet93e02d82019-03-08 14:18:50 +01002315 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002316
2317 /* stop waiting for data if the input is closed before the end. If the
2318 * client side was already closed, it means that the client has aborted,
2319 * so we don't want to count this as a server abort. Otherwise it's a
2320 * server abort.
2321 */
Christopher Fauletd20fdb02019-06-13 16:43:22 +02002322 if (msg->msg_state < HTTP_MSG_ENDING && res->flags & CF_SHUTR) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02002323 if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))
Christopher Faulet93e02d82019-03-08 14:18:50 +01002324 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002325 /* If we have some pending data, we continue the processing */
Christopher Faulet93e02d82019-03-08 14:18:50 +01002326 if (htx_is_empty(htx))
2327 goto return_srv_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002328 }
2329
Christopher Faulete0768eb2018-10-03 16:38:02 +02002330 /* When TE: chunked is used, we need to get there again to parse
2331 * remaining chunks even if the server has closed, so we don't want to
Christopher Faulet9768c262018-10-22 09:34:31 +02002332 * set CF_DONTCLOSE. Similarly when there is a content-leng or if there
2333 * are filters registered on the stream, we don't want to forward a
2334 * close
Christopher Faulete0768eb2018-10-03 16:38:02 +02002335 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002336 if ((msg->flags & HTTP_MSGF_XFER_LEN) || HAS_RSP_DATA_FILTERS(s))
Christopher Faulete0768eb2018-10-03 16:38:02 +02002337 channel_dont_close(res);
2338
2339 /* We know that more data are expected, but we couldn't send more that
2340 * what we did. So we always set the CF_EXPECT_MORE flag so that the
2341 * system knows it must not set a PUSH on this first part. Interactive
2342 * modes are already handled by the stream sock layer. We must not do
2343 * this in content-length mode because it could present the MSG_MORE
2344 * flag with the last block of forwarded data, which would cause an
2345 * additional delay to be observed by the receiver.
2346 */
Christopher Faulet2151cdd2020-07-22 16:34:59 +02002347 if (HAS_RSP_DATA_FILTERS(s))
Christopher Faulete0768eb2018-10-03 16:38:02 +02002348 res->flags |= CF_EXPECT_MORE;
2349
2350 /* the stream handler will take care of timeouts and errors */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002351 DBG_TRACE_DEVEL("waiting for more data to forward",
2352 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002353 return 0;
2354
Christopher Faulet93e02d82019-03-08 14:18:50 +01002355 return_srv_abort:
Willy Tarreau4781b152021-04-06 13:53:36 +02002356 _HA_ATOMIC_INC(&sess->fe->fe_counters.srv_aborts);
2357 _HA_ATOMIC_INC(&s->be->be_counters.srv_aborts);
William Lallemand36119de2021-03-08 15:26:48 +01002358 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02002359 _HA_ATOMIC_INC(&sess->listener->counters->srv_aborts);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002360 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02002361 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.srv_aborts);
Willy Tarreau826f3ab2021-02-10 12:07:15 +01002362 stream_inc_http_fail_ctr(s);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002363 if (!(s->flags & SF_ERR_MASK))
2364 s->flags |= SF_ERR_SRVCL;
2365 goto return_error;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002366
Christopher Faulet93e02d82019-03-08 14:18:50 +01002367 return_cli_abort:
Willy Tarreau4781b152021-04-06 13:53:36 +02002368 _HA_ATOMIC_INC(&sess->fe->fe_counters.cli_aborts);
2369 _HA_ATOMIC_INC(&s->be->be_counters.cli_aborts);
William Lallemand36119de2021-03-08 15:26:48 +01002370 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02002371 _HA_ATOMIC_INC(&sess->listener->counters->cli_aborts);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002372 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02002373 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.cli_aborts);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002374 if (!(s->flags & SF_ERR_MASK))
2375 s->flags |= SF_ERR_CLICL;
2376 goto return_error;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002377
Christopher Fauletb9a92f32019-09-09 10:15:21 +02002378 return_int_err:
Willy Tarreau4781b152021-04-06 13:53:36 +02002379 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
2380 _HA_ATOMIC_INC(&s->be->be_counters.internal_errors);
William Lallemand36119de2021-03-08 15:26:48 +01002381 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02002382 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002383 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02002384 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.internal_errors);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02002385 if (!(s->flags & SF_ERR_MASK))
2386 s->flags |= SF_ERR_INTERNAL;
2387 goto return_error;
2388
Christopher Faulet93e02d82019-03-08 14:18:50 +01002389 return_bad_res:
Willy Tarreau4781b152021-04-06 13:53:36 +02002390 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002391 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02002392 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002393 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_RSP);
2394 }
Willy Tarreau826f3ab2021-02-10 12:07:15 +01002395 stream_inc_http_fail_ctr(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002396 if (!(s->flags & SF_ERR_MASK))
Christopher Faulet93e02d82019-03-08 14:18:50 +01002397 s->flags |= SF_ERR_SRVCL;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002398 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +02002399
Christopher Faulet93e02d82019-03-08 14:18:50 +01002400 return_error:
Christopher Faulete0768eb2018-10-03 16:38:02 +02002401 /* don't send any error message as we're in the body */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002402 http_reply_and_close(s, txn->status, NULL);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002403 res->analysers &= AN_RES_FLT_END;
2404 s->req.analysers &= AN_REQ_FLT_END; /* we're in data phase, we want to abort both directions */
Christopher Faulete0768eb2018-10-03 16:38:02 +02002405 if (!(s->flags & SF_FINST_MASK))
2406 s->flags |= SF_FINST_D;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002407 DBG_TRACE_DEVEL("leaving on error",
2408 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002409 return 0;
2410}
2411
Christopher Fauletf2824e62018-10-01 12:12:37 +02002412/* Perform an HTTP redirect based on the information in <rule>. The function
Christopher Faulet99daf282018-11-28 22:58:13 +01002413 * returns zero on success, or zero in case of a, irrecoverable error such
Christopher Fauletf2824e62018-10-01 12:12:37 +02002414 * as too large a request to build a valid response.
2415 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002416int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn)
Christopher Fauletf2824e62018-10-01 12:12:37 +02002417{
Christopher Faulet99daf282018-11-28 22:58:13 +01002418 struct channel *req = &s->req;
2419 struct channel *res = &s->res;
2420 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01002421 struct htx_sl *sl;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002422 struct buffer *chunk;
Christopher Faulet99daf282018-11-28 22:58:13 +01002423 struct ist status, reason, location;
2424 unsigned int flags;
Christopher Faulet08e66462019-05-23 16:44:59 +02002425 int close = 0; /* Try to keep the connection alive byt default */
Christopher Fauletf2824e62018-10-01 12:12:37 +02002426
2427 chunk = alloc_trash_chunk();
Christopher Fauletb8a53712019-12-16 11:29:38 +01002428 if (!chunk) {
2429 if (!(s->flags & SF_ERR_MASK))
2430 s->flags |= SF_ERR_RESOURCE;
Christopher Faulet99daf282018-11-28 22:58:13 +01002431 goto fail;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002432 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002433
Christopher Faulet99daf282018-11-28 22:58:13 +01002434 /*
2435 * Create the location
2436 */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002437 htx = htxbuf(&req->buf);
Christopher Fauletf2824e62018-10-01 12:12:37 +02002438 switch(rule->type) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002439 case REDIRECT_TYPE_SCHEME: {
2440 struct http_hdr_ctx ctx;
2441 struct ist path, host;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002442
Christopher Faulet99daf282018-11-28 22:58:13 +01002443 host = ist("");
2444 ctx.blk = NULL;
2445 if (http_find_header(htx, ist("Host"), &ctx, 0))
2446 host = ctx.value;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002447
Christopher Faulet297fbb42019-05-13 14:41:27 +02002448 sl = http_get_stline(htx);
Christopher Faulet99daf282018-11-28 22:58:13 +01002449 path = http_get_path(htx_sl_req_uri(sl));
2450 /* build message using path */
Tim Duesterhused526372020-03-05 17:56:33 +01002451 if (isttest(path)) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002452 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2453 int qs = 0;
2454 while (qs < path.len) {
2455 if (*(path.ptr + qs) == '?') {
2456 path.len = qs;
2457 break;
2458 }
2459 qs++;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002460 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002461 }
2462 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002463 else
2464 path = ist("/");
Christopher Fauletf2824e62018-10-01 12:12:37 +02002465
Christopher Faulet99daf282018-11-28 22:58:13 +01002466 if (rule->rdr_str) { /* this is an old "redirect" rule */
2467 /* add scheme */
2468 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2469 goto fail;
2470 }
2471 else {
2472 /* add scheme with executing log format */
2473 chunk->data += build_logline(s, chunk->area + chunk->data,
2474 chunk->size - chunk->data,
2475 &rule->rdr_fmt);
2476 }
2477 /* add "://" + host + path */
2478 if (!chunk_memcat(chunk, "://", 3) ||
2479 !chunk_memcat(chunk, host.ptr, host.len) ||
2480 !chunk_memcat(chunk, path.ptr, path.len))
2481 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002482
Christopher Faulet99daf282018-11-28 22:58:13 +01002483 /* append a slash at the end of the location if needed and missing */
2484 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
2485 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
2486 if (chunk->data + 1 >= chunk->size)
2487 goto fail;
2488 chunk->area[chunk->data++] = '/';
2489 }
2490 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002491 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002492
Christopher Faulet99daf282018-11-28 22:58:13 +01002493 case REDIRECT_TYPE_PREFIX: {
2494 struct ist path;
2495
Christopher Faulet297fbb42019-05-13 14:41:27 +02002496 sl = http_get_stline(htx);
Christopher Faulet99daf282018-11-28 22:58:13 +01002497 path = http_get_path(htx_sl_req_uri(sl));
2498 /* build message using path */
Tim Duesterhused526372020-03-05 17:56:33 +01002499 if (isttest(path)) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002500 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2501 int qs = 0;
2502 while (qs < path.len) {
2503 if (*(path.ptr + qs) == '?') {
2504 path.len = qs;
2505 break;
2506 }
2507 qs++;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002508 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002509 }
2510 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002511 else
2512 path = ist("/");
Christopher Fauletf2824e62018-10-01 12:12:37 +02002513
Christopher Faulet99daf282018-11-28 22:58:13 +01002514 if (rule->rdr_str) { /* this is an old "redirect" rule */
2515 /* add prefix. Note that if prefix == "/", we don't want to
2516 * add anything, otherwise it makes it hard for the user to
2517 * configure a self-redirection.
2518 */
2519 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
2520 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2521 goto fail;
2522 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002523 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002524 else {
2525 /* add prefix with executing log format */
2526 chunk->data += build_logline(s, chunk->area + chunk->data,
2527 chunk->size - chunk->data,
2528 &rule->rdr_fmt);
2529 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002530
Christopher Faulet99daf282018-11-28 22:58:13 +01002531 /* add path */
2532 if (!chunk_memcat(chunk, path.ptr, path.len))
2533 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002534
Christopher Faulet99daf282018-11-28 22:58:13 +01002535 /* append a slash at the end of the location if needed and missing */
2536 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
2537 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
2538 if (chunk->data + 1 >= chunk->size)
2539 goto fail;
2540 chunk->area[chunk->data++] = '/';
2541 }
2542 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002543 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002544 case REDIRECT_TYPE_LOCATION:
2545 default:
2546 if (rule->rdr_str) { /* this is an old "redirect" rule */
2547 /* add location */
2548 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2549 goto fail;
2550 }
2551 else {
2552 /* add location with executing log format */
2553 chunk->data += build_logline(s, chunk->area + chunk->data,
2554 chunk->size - chunk->data,
2555 &rule->rdr_fmt);
2556 }
2557 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002558 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002559 location = ist2(chunk->area, chunk->data);
2560
2561 /*
2562 * Create the 30x response
2563 */
2564 switch (rule->code) {
2565 case 308:
2566 status = ist("308");
2567 reason = ist("Permanent Redirect");
2568 break;
2569 case 307:
2570 status = ist("307");
2571 reason = ist("Temporary Redirect");
2572 break;
2573 case 303:
2574 status = ist("303");
2575 reason = ist("See Other");
2576 break;
2577 case 301:
2578 status = ist("301");
2579 reason = ist("Moved Permanently");
2580 break;
2581 case 302:
2582 default:
2583 status = ist("302");
2584 reason = ist("Found");
2585 break;
2586 }
2587
Christopher Faulet08e66462019-05-23 16:44:59 +02002588 if (!(txn->req.flags & HTTP_MSGF_BODYLESS) && txn->req.msg_state != HTTP_MSG_DONE)
2589 close = 1;
2590
Christopher Faulet99daf282018-11-28 22:58:13 +01002591 htx = htx_from_buf(&res->buf);
Kevin Zhu96b36392020-01-07 09:42:55 +01002592 /* Trim any possible response */
2593 channel_htx_truncate(&s->res, htx);
Christopher Faulet99daf282018-11-28 22:58:13 +01002594 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
2595 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), status, reason);
2596 if (!sl)
2597 goto fail;
2598 sl->info.res.status = rule->code;
2599 s->txn->status = rule->code;
2600
Christopher Faulet08e66462019-05-23 16:44:59 +02002601 if (close && !htx_add_header(htx, ist("Connection"), ist("close")))
2602 goto fail;
2603
2604 if (!htx_add_header(htx, ist("Content-length"), ist("0")) ||
Christopher Faulet99daf282018-11-28 22:58:13 +01002605 !htx_add_header(htx, ist("Location"), location))
2606 goto fail;
2607
2608 if (rule->code == 302 || rule->code == 303 || rule->code == 307) {
2609 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")))
2610 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002611 }
2612
2613 if (rule->cookie_len) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002614 if (!htx_add_header(htx, ist("Set-Cookie"), ist2(rule->cookie_str, rule->cookie_len)))
2615 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002616 }
2617
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002618 if (!htx_add_endof(htx, HTX_BLK_EOH))
Christopher Faulet99daf282018-11-28 22:58:13 +01002619 goto fail;
2620
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002621 htx->flags |= HTX_FL_EOM;
Kevin Zhu96b36392020-01-07 09:42:55 +01002622 htx_to_buf(htx, &res->buf);
Christopher Fauleta72a7e42020-01-28 09:28:11 +01002623 if (!http_forward_proxy_resp(s, 1))
2624 goto fail;
Christopher Faulet99daf282018-11-28 22:58:13 +01002625
Christopher Faulet60b33a52020-01-28 09:18:10 +01002626 if (rule->flags & REDIRECT_FLAG_FROM_REQ) {
2627 /* let's log the request time */
2628 s->logs.tv_request = now;
2629 req->analysers &= AN_REQ_FLT_END;
Christopher Faulet99daf282018-11-28 22:58:13 +01002630
Christopher Faulet60b33a52020-01-28 09:18:10 +01002631 if (s->sess->fe == s->be) /* report it if the request was intercepted by the frontend */
Willy Tarreau4781b152021-04-06 13:53:36 +02002632 _HA_ATOMIC_INC(&s->sess->fe->fe_counters.intercepted_req);
Christopher Faulet60b33a52020-01-28 09:18:10 +01002633 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002634
2635 if (!(s->flags & SF_ERR_MASK))
2636 s->flags |= SF_ERR_LOCAL;
2637 if (!(s->flags & SF_FINST_MASK))
Christopher Faulet60b33a52020-01-28 09:18:10 +01002638 s->flags |= ((rule->flags & REDIRECT_FLAG_FROM_REQ) ? SF_FINST_R : SF_FINST_H);
Christopher Fauletf2824e62018-10-01 12:12:37 +02002639
Christopher Faulet99daf282018-11-28 22:58:13 +01002640 free_trash_chunk(chunk);
2641 return 1;
2642
2643 fail:
2644 /* If an error occurred, remove the incomplete HTTP response from the
2645 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01002646 channel_htx_truncate(res, htxbuf(&res->buf));
Christopher Fauletf2824e62018-10-01 12:12:37 +02002647 free_trash_chunk(chunk);
Christopher Faulet99daf282018-11-28 22:58:13 +01002648 return 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002649}
2650
Christopher Faulet92d34fe2019-12-17 09:20:34 +01002651/* Replace all headers matching the name <name>. The header value is replaced if
2652 * it matches the regex <re>. <str> is used for the replacement. If <full> is
2653 * set to 1, the full-line is matched and replaced. Otherwise, comma-separated
2654 * values are evaluated one by one. It returns 0 on success and -1 on error.
2655 */
2656int http_replace_hdrs(struct stream* s, struct htx *htx, struct ist name,
2657 const char *str, struct my_regex *re, int full)
Christopher Faulet72333522018-10-24 11:25:02 +02002658{
2659 struct http_hdr_ctx ctx;
2660 struct buffer *output = get_trash_chunk();
2661
Christopher Faulet72333522018-10-24 11:25:02 +02002662 ctx.blk = NULL;
Christopher Faulet92d34fe2019-12-17 09:20:34 +01002663 while (http_find_header(htx, name, &ctx, full)) {
Christopher Faulet72333522018-10-24 11:25:02 +02002664 if (!regex_exec_match2(re, ctx.value.ptr, ctx.value.len, MAX_MATCH, pmatch, 0))
2665 continue;
2666
2667 output->data = exp_replace(output->area, output->size, ctx.value.ptr, str, pmatch);
2668 if (output->data == -1)
2669 return -1;
2670 if (!http_replace_header_value(htx, &ctx, ist2(output->area, output->data)))
2671 return -1;
2672 }
2673 return 0;
2674}
2675
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002676/* This function executes one of the set-{method,path,query,uri} actions. It
2677 * takes the string from the variable 'replace' with length 'len', then modifies
2678 * the relevant part of the request line accordingly. Then it updates various
2679 * pointers to the next elements which were moved, and the total buffer length.
2680 * It finds the action to be performed in p[2], previously filled by function
2681 * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal
2682 * error, though this can be revisited when this code is finally exploited.
2683 *
2684 * 'action' can be '0' to replace method, '1' to replace path, '2' to replace
Christopher Faulet312294f2020-09-02 17:17:44 +02002685 * query string, 3 to replace uri or 4 to replace the path+query.
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002686 *
2687 * In query string case, the mark question '?' must be set at the start of the
2688 * string by the caller, event if the replacement query string is empty.
2689 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002690int http_req_replace_stline(int action, const char *replace, int len,
2691 struct proxy *px, struct stream *s)
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002692{
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002693 struct htx *htx = htxbuf(&s->req.buf);
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002694
2695 switch (action) {
2696 case 0: // method
2697 if (!http_replace_req_meth(htx, ist2(replace, len)))
2698 return -1;
2699 break;
2700
2701 case 1: // path
Christopher Fauletb8ce5052020-08-31 16:11:57 +02002702 if (!http_replace_req_path(htx, ist2(replace, len), 0))
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002703 return -1;
2704 break;
2705
2706 case 2: // query
2707 if (!http_replace_req_query(htx, ist2(replace, len)))
2708 return -1;
2709 break;
2710
2711 case 3: // uri
2712 if (!http_replace_req_uri(htx, ist2(replace, len)))
2713 return -1;
2714 break;
2715
Christopher Faulet312294f2020-09-02 17:17:44 +02002716 case 4: // path + query
2717 if (!http_replace_req_path(htx, ist2(replace, len), 1))
2718 return -1;
2719 break;
2720
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002721 default:
2722 return -1;
2723 }
2724 return 0;
2725}
2726
2727/* This function replace the HTTP status code and the associated message. The
Christopher Faulete00d06c2019-12-16 17:18:42 +01002728 * variable <status> contains the new status code. This function never fails. It
2729 * returns 0 in case of success, -1 in case of internal error.
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002730 */
Christopher Faulet96bff762019-12-17 13:46:18 +01002731int http_res_set_status(unsigned int status, struct ist reason, struct stream *s)
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002732{
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002733 struct htx *htx = htxbuf(&s->res.buf);
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002734 char *res;
2735
2736 chunk_reset(&trash);
2737 res = ultoa_o(status, trash.area, trash.size);
2738 trash.data = res - trash.area;
2739
2740 /* Do we have a custom reason format string? */
Tim Duesterhuse296d3e2020-03-05 17:56:31 +01002741 if (!isttest(reason)) {
Christopher Faulet96bff762019-12-17 13:46:18 +01002742 const char *str = http_get_reason(status);
Tim Duesterhusdcf753a2021-03-04 17:31:47 +01002743 reason = ist(str);
Christopher Faulet96bff762019-12-17 13:46:18 +01002744 }
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002745
Christopher Fauletbde2c4c2020-08-31 16:43:34 +02002746 if (!http_replace_res_status(htx, ist2(trash.area, trash.data), reason))
Christopher Faulete00d06c2019-12-16 17:18:42 +01002747 return -1;
2748 return 0;
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002749}
2750
Christopher Faulet3e964192018-10-24 11:39:23 +02002751/* Executes the http-request rules <rules> for stream <s>, proxy <px> and
2752 * transaction <txn>. Returns the verdict of the first rule that prevents
2753 * further processing of the request (auth, deny, ...), and defaults to
2754 * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or
2755 * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT
2756 * on txn->flags if it encounters a tarpit rule. If <deny_status> is not NULL
2757 * and a deny/tarpit rule is matched, it will be filled with this rule's deny
2758 * status.
2759 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002760static enum rule_result http_req_get_intercept_rule(struct proxy *px, struct list *rules,
Christopher Fauletb58f62b2020-01-13 16:40:13 +01002761 struct stream *s)
Christopher Faulet3e964192018-10-24 11:39:23 +02002762{
2763 struct session *sess = strm_sess(s);
2764 struct http_txn *txn = s->txn;
Christopher Faulet3e964192018-10-24 11:39:23 +02002765 struct act_rule *rule;
Christopher Faulet3e964192018-10-24 11:39:23 +02002766 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002767 int act_opts = 0;
Christopher Faulet3e964192018-10-24 11:39:23 +02002768
Christopher Faulet3e964192018-10-24 11:39:23 +02002769 /* If "the current_rule_list" match the executed rule list, we are in
2770 * resume condition. If a resume is needed it is always in the action
2771 * and never in the ACL or converters. In this case, we initialise the
2772 * current rule, and go to the action execution point.
2773 */
2774 if (s->current_rule) {
2775 rule = s->current_rule;
2776 s->current_rule = NULL;
2777 if (s->current_rule_list == rules)
2778 goto resume_execution;
2779 }
2780 s->current_rule_list = rules;
2781
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002782 /* start the ruleset evaluation in strict mode */
2783 txn->req.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01002784
Christopher Faulet3e964192018-10-24 11:39:23 +02002785 list_for_each_entry(rule, rules, list) {
2786 /* check optional condition */
2787 if (rule->cond) {
2788 int ret;
2789
2790 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
2791 ret = acl_pass(ret);
2792
2793 if (rule->cond->pol == ACL_COND_UNLESS)
2794 ret = !ret;
2795
2796 if (!ret) /* condition not matched */
2797 continue;
2798 }
2799
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002800 act_opts |= ACT_OPT_FIRST;
Christopher Faulet3e964192018-10-24 11:39:23 +02002801 resume_execution:
Amaury Denoyelle03517732021-05-07 14:25:01 +02002802 if (rule->kw->flags & KWF_EXPERIMENTAL)
2803 mark_tainted(TAINTED_ACTION_EXP_EXECUTED);
2804
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002805 /* Always call the action function if defined */
2806 if (rule->action_ptr) {
2807 if ((s->req.flags & CF_READ_ERROR) ||
2808 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
2809 (px->options & PR_O_ABRT_CLOSE)))
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002810 act_opts |= ACT_OPT_FINAL;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002811
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002812 switch (rule->action_ptr(rule, px, sess, s, act_opts)) {
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002813 case ACT_RET_CONT:
2814 break;
2815 case ACT_RET_STOP:
2816 rule_ret = HTTP_RULE_RES_STOP;
2817 goto end;
2818 case ACT_RET_YIELD:
2819 s->current_rule = rule;
2820 rule_ret = HTTP_RULE_RES_YIELD;
2821 goto end;
2822 case ACT_RET_ERR:
2823 rule_ret = HTTP_RULE_RES_ERROR;
2824 goto end;
2825 case ACT_RET_DONE:
2826 rule_ret = HTTP_RULE_RES_DONE;
2827 goto end;
2828 case ACT_RET_DENY:
Christopher Fauletb58f62b2020-01-13 16:40:13 +01002829 if (txn->status == -1)
2830 txn->status = 403;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002831 rule_ret = HTTP_RULE_RES_DENY;
2832 goto end;
2833 case ACT_RET_ABRT:
2834 rule_ret = HTTP_RULE_RES_ABRT;
2835 goto end;
2836 case ACT_RET_INV:
2837 rule_ret = HTTP_RULE_RES_BADREQ;
2838 goto end;
2839 }
2840 continue; /* eval the next rule */
2841 }
2842
2843 /* If not action function defined, check for known actions */
Christopher Faulet3e964192018-10-24 11:39:23 +02002844 switch (rule->action) {
2845 case ACT_ACTION_ALLOW:
2846 rule_ret = HTTP_RULE_RES_STOP;
2847 goto end;
2848
2849 case ACT_ACTION_DENY:
Christopher Faulet5cb513a2020-05-13 17:56:56 +02002850 txn->status = rule->arg.http_reply->status;
2851 txn->http_reply = rule->arg.http_reply;
Christopher Faulet3e964192018-10-24 11:39:23 +02002852 rule_ret = HTTP_RULE_RES_DENY;
2853 goto end;
2854
2855 case ACT_HTTP_REQ_TARPIT:
2856 txn->flags |= TX_CLTARPIT;
Christopher Faulet5cb513a2020-05-13 17:56:56 +02002857 txn->status = rule->arg.http_reply->status;
2858 txn->http_reply = rule->arg.http_reply;
Christopher Faulet3e964192018-10-24 11:39:23 +02002859 rule_ret = HTTP_RULE_RES_DENY;
2860 goto end;
2861
Christopher Faulet3e964192018-10-24 11:39:23 +02002862 case ACT_HTTP_REDIR:
Christopher Faulet90d22a82020-03-06 11:18:39 +01002863 rule_ret = HTTP_RULE_RES_ABRT;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002864 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
Christopher Faulet3a26bee2019-12-16 12:47:40 +01002865 rule_ret = HTTP_RULE_RES_ERROR;
Christopher Faulet3e964192018-10-24 11:39:23 +02002866 goto end;
2867
2868 case ACT_HTTP_SET_NICE:
Christopher Faulet96bff762019-12-17 13:46:18 +01002869 s->task->nice = rule->arg.http.i;
Christopher Faulet3e964192018-10-24 11:39:23 +02002870 break;
2871
2872 case ACT_HTTP_SET_TOS:
Christopher Faulet96bff762019-12-17 13:46:18 +01002873 conn_set_tos(objt_conn(sess->origin), rule->arg.http.i);
Christopher Faulet3e964192018-10-24 11:39:23 +02002874 break;
2875
2876 case ACT_HTTP_SET_MARK:
Christopher Faulet96bff762019-12-17 13:46:18 +01002877 conn_set_mark(objt_conn(sess->origin), rule->arg.http.i);
Christopher Faulet3e964192018-10-24 11:39:23 +02002878 break;
2879
2880 case ACT_HTTP_SET_LOGL:
Christopher Faulet96bff762019-12-17 13:46:18 +01002881 s->logs.level = rule->arg.http.i;
Christopher Faulet3e964192018-10-24 11:39:23 +02002882 break;
2883
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002884 /* other flags exists, but normally, they never be matched. */
Christopher Faulet3e964192018-10-24 11:39:23 +02002885 default:
2886 break;
2887 }
2888 }
2889
2890 end:
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002891 /* if the ruleset evaluation is finished reset the strict mode */
Christopher Faulet46f95542019-12-20 10:07:22 +01002892 if (rule_ret != HTTP_RULE_RES_YIELD)
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002893 txn->req.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01002894
Christopher Faulet3e964192018-10-24 11:39:23 +02002895 /* we reached the end of the rules, nothing to report */
2896 return rule_ret;
2897}
2898
2899/* Executes the http-response rules <rules> for stream <s> and proxy <px>. It
2900 * returns one of 5 possible statuses: HTTP_RULE_RES_CONT, HTTP_RULE_RES_STOP,
2901 * HTTP_RULE_RES_DONE, HTTP_RULE_RES_YIELD, or HTTP_RULE_RES_BADREQ. If *CONT
2902 * is returned, the process can continue the evaluation of next rule list. If
2903 * *STOP or *DONE is returned, the process must stop the evaluation. If *BADREQ
2904 * is returned, it means the operation could not be processed and a server error
Christopher Fauleta53abad2020-05-13 08:12:22 +02002905 * must be returned. If *YIELD is returned, the caller must call again the
2906 * function with the same context.
Christopher Faulet3e964192018-10-24 11:39:23 +02002907 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002908static enum rule_result http_res_get_intercept_rule(struct proxy *px, struct list *rules,
2909 struct stream *s)
Christopher Faulet3e964192018-10-24 11:39:23 +02002910{
2911 struct session *sess = strm_sess(s);
2912 struct http_txn *txn = s->txn;
Christopher Faulet3e964192018-10-24 11:39:23 +02002913 struct act_rule *rule;
Christopher Faulet3e964192018-10-24 11:39:23 +02002914 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002915 int act_opts = 0;
Christopher Faulet3e964192018-10-24 11:39:23 +02002916
Christopher Faulet3e964192018-10-24 11:39:23 +02002917 /* If "the current_rule_list" match the executed rule list, we are in
2918 * resume condition. If a resume is needed it is always in the action
2919 * and never in the ACL or converters. In this case, we initialise the
2920 * current rule, and go to the action execution point.
2921 */
2922 if (s->current_rule) {
2923 rule = s->current_rule;
2924 s->current_rule = NULL;
2925 if (s->current_rule_list == rules)
2926 goto resume_execution;
2927 }
2928 s->current_rule_list = rules;
2929
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002930 /* start the ruleset evaluation in strict mode */
2931 txn->rsp.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01002932
Christopher Faulet3e964192018-10-24 11:39:23 +02002933 list_for_each_entry(rule, rules, list) {
2934 /* check optional condition */
2935 if (rule->cond) {
2936 int ret;
2937
2938 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
2939 ret = acl_pass(ret);
2940
2941 if (rule->cond->pol == ACL_COND_UNLESS)
2942 ret = !ret;
2943
2944 if (!ret) /* condition not matched */
2945 continue;
2946 }
2947
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002948 act_opts |= ACT_OPT_FIRST;
Christopher Faulet3e964192018-10-24 11:39:23 +02002949resume_execution:
Amaury Denoyelle03517732021-05-07 14:25:01 +02002950 if (rule->kw->flags & KWF_EXPERIMENTAL)
2951 mark_tainted(TAINTED_ACTION_EXP_EXECUTED);
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002952
2953 /* Always call the action function if defined */
2954 if (rule->action_ptr) {
2955 if ((s->req.flags & CF_READ_ERROR) ||
2956 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
2957 (px->options & PR_O_ABRT_CLOSE)))
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002958 act_opts |= ACT_OPT_FINAL;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002959
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002960 switch (rule->action_ptr(rule, px, sess, s, act_opts)) {
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002961 case ACT_RET_CONT:
2962 break;
2963 case ACT_RET_STOP:
2964 rule_ret = HTTP_RULE_RES_STOP;
2965 goto end;
2966 case ACT_RET_YIELD:
2967 s->current_rule = rule;
2968 rule_ret = HTTP_RULE_RES_YIELD;
2969 goto end;
2970 case ACT_RET_ERR:
2971 rule_ret = HTTP_RULE_RES_ERROR;
2972 goto end;
2973 case ACT_RET_DONE:
2974 rule_ret = HTTP_RULE_RES_DONE;
2975 goto end;
2976 case ACT_RET_DENY:
Christopher Fauletb58f62b2020-01-13 16:40:13 +01002977 if (txn->status == -1)
2978 txn->status = 502;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002979 rule_ret = HTTP_RULE_RES_DENY;
2980 goto end;
2981 case ACT_RET_ABRT:
2982 rule_ret = HTTP_RULE_RES_ABRT;
2983 goto end;
2984 case ACT_RET_INV:
2985 rule_ret = HTTP_RULE_RES_BADREQ;
2986 goto end;
2987 }
2988 continue; /* eval the next rule */
2989 }
2990
2991 /* If not action function defined, check for known actions */
Christopher Faulet3e964192018-10-24 11:39:23 +02002992 switch (rule->action) {
2993 case ACT_ACTION_ALLOW:
2994 rule_ret = HTTP_RULE_RES_STOP; /* "allow" rules are OK */
2995 goto end;
2996
2997 case ACT_ACTION_DENY:
Christopher Faulet5cb513a2020-05-13 17:56:56 +02002998 txn->status = rule->arg.http_reply->status;
2999 txn->http_reply = rule->arg.http_reply;
Christopher Faulet3a26bee2019-12-16 12:47:40 +01003000 rule_ret = HTTP_RULE_RES_DENY;
Christopher Faulet3e964192018-10-24 11:39:23 +02003001 goto end;
3002
3003 case ACT_HTTP_SET_NICE:
Christopher Faulet96bff762019-12-17 13:46:18 +01003004 s->task->nice = rule->arg.http.i;
Christopher Faulet3e964192018-10-24 11:39:23 +02003005 break;
3006
3007 case ACT_HTTP_SET_TOS:
Christopher Faulet96bff762019-12-17 13:46:18 +01003008 conn_set_tos(objt_conn(sess->origin), rule->arg.http.i);
Christopher Faulet3e964192018-10-24 11:39:23 +02003009 break;
3010
3011 case ACT_HTTP_SET_MARK:
Christopher Faulet96bff762019-12-17 13:46:18 +01003012 conn_set_mark(objt_conn(sess->origin), rule->arg.http.i);
Christopher Faulet3e964192018-10-24 11:39:23 +02003013 break;
3014
3015 case ACT_HTTP_SET_LOGL:
Christopher Faulet96bff762019-12-17 13:46:18 +01003016 s->logs.level = rule->arg.http.i;
Christopher Faulet3e964192018-10-24 11:39:23 +02003017 break;
3018
Christopher Faulet3e964192018-10-24 11:39:23 +02003019 case ACT_HTTP_REDIR:
Christopher Faulet49c2a702020-03-06 15:44:37 +01003020 rule_ret = HTTP_RULE_RES_ABRT;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003021 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
Christopher Faulet3a26bee2019-12-16 12:47:40 +01003022 rule_ret = HTTP_RULE_RES_ERROR;
Christopher Faulet3e964192018-10-24 11:39:23 +02003023 goto end;
3024
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01003025 /* other flags exists, but normally, they never be matched. */
Christopher Faulet3e964192018-10-24 11:39:23 +02003026 default:
3027 break;
3028 }
3029 }
3030
3031 end:
Christopher Faulet1aea50e2020-01-17 16:03:53 +01003032 /* if the ruleset evaluation is finished reset the strict mode */
Christopher Faulet46f95542019-12-20 10:07:22 +01003033 if (rule_ret != HTTP_RULE_RES_YIELD)
Christopher Faulet1aea50e2020-01-17 16:03:53 +01003034 txn->rsp.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01003035
Christopher Faulet3e964192018-10-24 11:39:23 +02003036 /* we reached the end of the rules, nothing to report */
3037 return rule_ret;
3038}
3039
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01003040/* Executes backend and frontend http-after-response rules for the stream <s>,
3041 * in that order. it return 1 on success and 0 on error. It is the caller
3042 * responsibility to catch error or ignore it. If it catches it, this function
3043 * may be called a second time, for the internal error.
3044 */
3045int http_eval_after_res_rules(struct stream *s)
3046{
3047 struct session *sess = s->sess;
3048 enum rule_result ret = HTTP_RULE_RES_CONT;
3049
Christopher Faulet507479b2020-05-15 12:29:46 +02003050 /* Eval after-response ruleset only if the reply is not const */
3051 if (s->txn->flags & TX_CONST_REPLY)
3052 goto end;
3053
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01003054 /* prune the request variables if not already done and swap to the response variables. */
3055 if (s->vars_reqres.scope != SCOPE_RES) {
3056 if (!LIST_ISEMPTY(&s->vars_reqres.head))
3057 vars_prune(&s->vars_reqres, s->sess, s);
3058 vars_init(&s->vars_reqres, SCOPE_RES);
3059 }
3060
3061 ret = http_res_get_intercept_rule(s->be, &s->be->http_after_res_rules, s);
3062 if ((ret == HTTP_RULE_RES_CONT || ret == HTTP_RULE_RES_STOP) && sess->fe != s->be)
3063 ret = http_res_get_intercept_rule(sess->fe, &sess->fe->http_after_res_rules, s);
3064
Christopher Faulet507479b2020-05-15 12:29:46 +02003065 end:
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01003066 /* All other codes than CONTINUE, STOP or DONE are forbidden */
3067 return (ret == HTTP_RULE_RES_CONT || ret == HTTP_RULE_RES_STOP || ret == HTTP_RULE_RES_DONE);
3068}
3069
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003070/*
3071 * Manage client-side cookie. It can impact performance by about 2% so it is
3072 * desirable to call it only when needed. This code is quite complex because
3073 * of the multiple very crappy and ambiguous syntaxes we have to support. it
3074 * highly recommended not to touch this part without a good reason !
3075 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003076static void http_manage_client_side_cookies(struct stream *s, struct channel *req)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003077{
3078 struct session *sess = s->sess;
3079 struct http_txn *txn = s->txn;
3080 struct htx *htx;
3081 struct http_hdr_ctx ctx;
3082 char *hdr_beg, *hdr_end, *del_from;
3083 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
3084 int preserve_hdr;
3085
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003086 htx = htxbuf(&req->buf);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003087 ctx.blk = NULL;
3088 while (http_find_header(htx, ist("Cookie"), &ctx, 1)) {
Olivier Houchardf0f42382019-07-22 17:43:46 +02003089 int is_first = 1;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003090 del_from = NULL; /* nothing to be deleted */
3091 preserve_hdr = 0; /* assume we may kill the whole header */
3092
3093 /* Now look for cookies. Conforming to RFC2109, we have to support
3094 * attributes whose name begin with a '$', and associate them with
3095 * the right cookie, if we want to delete this cookie.
3096 * So there are 3 cases for each cookie read :
3097 * 1) it's a special attribute, beginning with a '$' : ignore it.
3098 * 2) it's a server id cookie that we *MAY* want to delete : save
3099 * some pointers on it (last semi-colon, beginning of cookie...)
3100 * 3) it's an application cookie : we *MAY* have to delete a previous
3101 * "special" cookie.
3102 * At the end of loop, if a "special" cookie remains, we may have to
3103 * remove it. If no application cookie persists in the header, we
3104 * *MUST* delete it.
3105 *
3106 * Note: RFC2965 is unclear about the processing of spaces around
3107 * the equal sign in the ATTR=VALUE form. A careful inspection of
3108 * the RFC explicitly allows spaces before it, and not within the
3109 * tokens (attrs or values). An inspection of RFC2109 allows that
3110 * too but section 10.1.3 lets one think that spaces may be allowed
3111 * after the equal sign too, resulting in some (rare) buggy
3112 * implementations trying to do that. So let's do what servers do.
3113 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
3114 * allowed quoted strings in values, with any possible character
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003115 * after a backslash, including control chars and delimiters, which
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003116 * causes parsing to become ambiguous. Browsers also allow spaces
3117 * within values even without quotes.
3118 *
3119 * We have to keep multiple pointers in order to support cookie
3120 * removal at the beginning, middle or end of header without
3121 * corrupting the header. All of these headers are valid :
3122 *
3123 * hdr_beg hdr_end
3124 * | |
3125 * v |
3126 * NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3 |
3127 * NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3 v
3128 * NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3
3129 * | | | | | | |
3130 * | | | | | | |
3131 * | | | | | | +--> next
3132 * | | | | | +----> val_end
3133 * | | | | +-----------> val_beg
3134 * | | | +--------------> equal
3135 * | | +----------------> att_end
3136 * | +---------------------> att_beg
3137 * +--------------------------> prev
3138 *
3139 */
3140 hdr_beg = ctx.value.ptr;
3141 hdr_end = hdr_beg + ctx.value.len;
3142 for (prev = hdr_beg; prev < hdr_end; prev = next) {
3143 /* Iterate through all cookies on this line */
3144
3145 /* find att_beg */
3146 att_beg = prev;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003147 if (!is_first)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003148 att_beg++;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003149 is_first = 0;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003150
3151 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
3152 att_beg++;
3153
3154 /* find att_end : this is the first character after the last non
3155 * space before the equal. It may be equal to hdr_end.
3156 */
3157 equal = att_end = att_beg;
3158 while (equal < hdr_end) {
3159 if (*equal == '=' || *equal == ',' || *equal == ';')
3160 break;
3161 if (HTTP_IS_SPHT(*equal++))
3162 continue;
3163 att_end = equal;
3164 }
3165
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003166 /* here, <equal> points to '=', a delimiter or the end. <att_end>
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003167 * is between <att_beg> and <equal>, both may be identical.
3168 */
3169 /* look for end of cookie if there is an equal sign */
3170 if (equal < hdr_end && *equal == '=') {
3171 /* look for the beginning of the value */
3172 val_beg = equal + 1;
3173 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
3174 val_beg++;
3175
3176 /* find the end of the value, respecting quotes */
3177 next = http_find_cookie_value_end(val_beg, hdr_end);
3178
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003179 /* make val_end point to the first white space or delimiter after the value */
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003180 val_end = next;
3181 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
3182 val_end--;
3183 }
3184 else
3185 val_beg = val_end = next = equal;
3186
3187 /* We have nothing to do with attributes beginning with
3188 * '$'. However, they will automatically be removed if a
3189 * header before them is removed, since they're supposed
3190 * to be linked together.
3191 */
3192 if (*att_beg == '$')
3193 continue;
3194
3195 /* Ignore cookies with no equal sign */
3196 if (equal == next) {
3197 /* This is not our cookie, so we must preserve it. But if we already
3198 * scheduled another cookie for removal, we cannot remove the
3199 * complete header, but we can remove the previous block itself.
3200 */
3201 preserve_hdr = 1;
3202 if (del_from != NULL) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003203 int delta = http_del_hdr_value(hdr_beg, hdr_end, &del_from, prev);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003204 val_end += delta;
3205 next += delta;
3206 hdr_end += delta;
3207 prev = del_from;
3208 del_from = NULL;
3209 }
3210 continue;
3211 }
3212
3213 /* if there are spaces around the equal sign, we need to
3214 * strip them otherwise we'll get trouble for cookie captures,
3215 * or even for rewrites. Since this happens extremely rarely,
3216 * it does not hurt performance.
3217 */
3218 if (unlikely(att_end != equal || val_beg > equal + 1)) {
3219 int stripped_before = 0;
3220 int stripped_after = 0;
3221
3222 if (att_end != equal) {
3223 memmove(att_end, equal, hdr_end - equal);
3224 stripped_before = (att_end - equal);
3225 equal += stripped_before;
3226 val_beg += stripped_before;
3227 }
3228
3229 if (val_beg > equal + 1) {
3230 memmove(equal + 1, val_beg, hdr_end + stripped_before - val_beg);
3231 stripped_after = (equal + 1) - val_beg;
3232 val_beg += stripped_after;
3233 stripped_before += stripped_after;
3234 }
3235
3236 val_end += stripped_before;
3237 next += stripped_before;
3238 hdr_end += stripped_before;
3239 }
3240 /* now everything is as on the diagram above */
3241
3242 /* First, let's see if we want to capture this cookie. We check
3243 * that we don't already have a client side cookie, because we
3244 * can only capture one. Also as an optimisation, we ignore
3245 * cookies shorter than the declared name.
3246 */
3247 if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL &&
3248 (val_end - att_beg >= sess->fe->capture_namelen) &&
3249 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
3250 int log_len = val_end - att_beg;
3251
3252 if ((txn->cli_cookie = pool_alloc(pool_head_capture)) == NULL) {
3253 ha_alert("HTTP logging : out of memory.\n");
3254 } else {
3255 if (log_len > sess->fe->capture_len)
3256 log_len = sess->fe->capture_len;
3257 memcpy(txn->cli_cookie, att_beg, log_len);
3258 txn->cli_cookie[log_len] = 0;
3259 }
3260 }
3261
3262 /* Persistence cookies in passive, rewrite or insert mode have the
3263 * following form :
3264 *
3265 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
3266 *
3267 * For cookies in prefix mode, the form is :
3268 *
3269 * Cookie: NAME=SRV~VALUE
3270 */
3271 if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
3272 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
3273 struct server *srv = s->be->srv;
3274 char *delim;
3275
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003276 /* if we're in cookie prefix mode, we'll search the delimiter so that we
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003277 * have the server ID between val_beg and delim, and the original cookie between
3278 * delim+1 and val_end. Otherwise, delim==val_end :
3279 *
3280 * hdr_beg
3281 * |
3282 * v
3283 * NAME=SRV; # in all but prefix modes
3284 * NAME=SRV~OPAQUE ; # in prefix mode
3285 * || || | |+-> next
3286 * || || | +--> val_end
3287 * || || +---------> delim
3288 * || |+------------> val_beg
3289 * || +-------------> att_end = equal
3290 * |+-----------------> att_beg
3291 * +------------------> prev
3292 *
3293 */
3294 if (s->be->ck_opts & PR_CK_PFX) {
3295 for (delim = val_beg; delim < val_end; delim++)
3296 if (*delim == COOKIE_DELIM)
3297 break;
3298 }
3299 else {
3300 char *vbar1;
3301 delim = val_end;
3302 /* Now check if the cookie contains a date field, which would
3303 * appear after a vertical bar ('|') just after the server name
3304 * and before the delimiter.
3305 */
3306 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
3307 if (vbar1) {
3308 /* OK, so left of the bar is the server's cookie and
3309 * right is the last seen date. It is a base64 encoded
3310 * 30-bit value representing the UNIX date since the
3311 * epoch in 4-second quantities.
3312 */
3313 int val;
3314 delim = vbar1++;
3315 if (val_end - vbar1 >= 5) {
3316 val = b64tos30(vbar1);
3317 if (val > 0)
3318 txn->cookie_last_date = val << 2;
3319 }
3320 /* look for a second vertical bar */
3321 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
3322 if (vbar1 && (val_end - vbar1 > 5)) {
3323 val = b64tos30(vbar1 + 1);
3324 if (val > 0)
3325 txn->cookie_first_date = val << 2;
3326 }
3327 }
3328 }
3329
3330 /* if the cookie has an expiration date and the proxy wants to check
3331 * it, then we do that now. We first check if the cookie is too old,
3332 * then only if it has expired. We detect strict overflow because the
3333 * time resolution here is not great (4 seconds). Cookies with dates
3334 * in the future are ignored if their offset is beyond one day. This
3335 * allows an admin to fix timezone issues without expiring everyone
3336 * and at the same time avoids keeping unwanted side effects for too
3337 * long.
3338 */
3339 if (txn->cookie_first_date && s->be->cookie_maxlife &&
3340 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) ||
3341 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
3342 txn->flags &= ~TX_CK_MASK;
3343 txn->flags |= TX_CK_OLD;
3344 delim = val_beg; // let's pretend we have not found the cookie
3345 txn->cookie_first_date = 0;
3346 txn->cookie_last_date = 0;
3347 }
3348 else if (txn->cookie_last_date && s->be->cookie_maxidle &&
3349 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) ||
3350 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
3351 txn->flags &= ~TX_CK_MASK;
3352 txn->flags |= TX_CK_EXPIRED;
3353 delim = val_beg; // let's pretend we have not found the cookie
3354 txn->cookie_first_date = 0;
3355 txn->cookie_last_date = 0;
3356 }
3357
3358 /* Here, we'll look for the first running server which supports the cookie.
3359 * This allows to share a same cookie between several servers, for example
3360 * to dedicate backup servers to specific servers only.
3361 * However, to prevent clients from sticking to cookie-less backup server
3362 * when they have incidentely learned an empty cookie, we simply ignore
3363 * empty cookies and mark them as invalid.
3364 * The same behaviour is applied when persistence must be ignored.
3365 */
3366 if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
3367 srv = NULL;
3368
3369 while (srv) {
3370 if (srv->cookie && (srv->cklen == delim - val_beg) &&
3371 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
3372 if ((srv->cur_state != SRV_ST_STOPPED) ||
3373 (s->be->options & PR_O_PERSIST) ||
3374 (s->flags & SF_FORCE_PRST)) {
3375 /* we found the server and we can use it */
3376 txn->flags &= ~TX_CK_MASK;
3377 txn->flags |= (srv->cur_state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
3378 s->flags |= SF_DIRECT | SF_ASSIGNED;
3379 s->target = &srv->obj_type;
3380 break;
3381 } else {
3382 /* we found a server, but it's down,
3383 * mark it as such and go on in case
3384 * another one is available.
3385 */
3386 txn->flags &= ~TX_CK_MASK;
3387 txn->flags |= TX_CK_DOWN;
3388 }
3389 }
3390 srv = srv->next;
3391 }
3392
3393 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
3394 /* no server matched this cookie or we deliberately skipped it */
3395 txn->flags &= ~TX_CK_MASK;
3396 if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
3397 txn->flags |= TX_CK_UNUSED;
3398 else
3399 txn->flags |= TX_CK_INVALID;
3400 }
3401
3402 /* depending on the cookie mode, we may have to either :
3403 * - delete the complete cookie if we're in insert+indirect mode, so that
3404 * the server never sees it ;
3405 * - remove the server id from the cookie value, and tag the cookie as an
Joseph Herlante9d5c722018-11-25 11:00:25 -08003406 * application cookie so that it does not get accidentally removed later,
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003407 * if we're in cookie prefix mode
3408 */
3409 if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
3410 int delta; /* negative */
3411
3412 memmove(val_beg, delim + 1, hdr_end - (delim + 1));
3413 delta = val_beg - (delim + 1);
3414 val_end += delta;
3415 next += delta;
3416 hdr_end += delta;
3417 del_from = NULL;
3418 preserve_hdr = 1; /* we want to keep this cookie */
3419 }
3420 else if (del_from == NULL &&
3421 (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
3422 del_from = prev;
3423 }
3424 }
3425 else {
3426 /* This is not our cookie, so we must preserve it. But if we already
3427 * scheduled another cookie for removal, we cannot remove the
3428 * complete header, but we can remove the previous block itself.
3429 */
3430 preserve_hdr = 1;
3431
3432 if (del_from != NULL) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003433 int delta = http_del_hdr_value(hdr_beg, hdr_end, &del_from, prev);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003434 if (att_beg >= del_from)
3435 att_beg += delta;
3436 if (att_end >= del_from)
3437 att_end += delta;
3438 val_beg += delta;
3439 val_end += delta;
3440 next += delta;
3441 hdr_end += delta;
3442 prev = del_from;
3443 del_from = NULL;
3444 }
3445 }
3446
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003447 } /* for each cookie */
3448
3449
3450 /* There are no more cookies on this line.
3451 * We may still have one (or several) marked for deletion at the
3452 * end of the line. We must do this now in two ways :
3453 * - if some cookies must be preserved, we only delete from the
3454 * mark to the end of line ;
3455 * - if nothing needs to be preserved, simply delete the whole header
3456 */
3457 if (del_from) {
3458 hdr_end = (preserve_hdr ? del_from : hdr_beg);
3459 }
3460 if ((hdr_end - hdr_beg) != ctx.value.len) {
Christopher Faulet3e2638e2019-06-18 09:49:16 +02003461 if (hdr_beg != hdr_end)
3462 htx_change_blk_value_len(htx, ctx.blk, hdr_end - hdr_beg);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003463 else
3464 http_remove_header(htx, &ctx);
3465 }
3466 } /* for each "Cookie header */
3467}
3468
3469/*
3470 * Manage server-side cookies. It can impact performance by about 2% so it is
3471 * desirable to call it only when needed. This function is also used when we
3472 * just need to know if there is a cookie (eg: for check-cache).
3473 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003474static void http_manage_server_side_cookies(struct stream *s, struct channel *res)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003475{
3476 struct session *sess = s->sess;
3477 struct http_txn *txn = s->txn;
3478 struct htx *htx;
3479 struct http_hdr_ctx ctx;
3480 struct server *srv;
3481 char *hdr_beg, *hdr_end;
3482 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau6f7a02a2019-04-15 21:49:49 +02003483 int is_cookie2 = 0;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003484
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003485 htx = htxbuf(&res->buf);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003486
3487 ctx.blk = NULL;
3488 while (1) {
Olivier Houchardf0f42382019-07-22 17:43:46 +02003489 int is_first = 1;
3490
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003491 if (!http_find_header(htx, ist("Set-Cookie"), &ctx, 1)) {
3492 if (!http_find_header(htx, ist("Set-Cookie2"), &ctx, 1))
3493 break;
3494 is_cookie2 = 1;
3495 }
3496
3497 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
3498 * <prev> points to the colon.
3499 */
3500 txn->flags |= TX_SCK_PRESENT;
3501
3502 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
3503 * check-cache is enabled) and we are not interested in checking
3504 * them. Warning, the cookie capture is declared in the frontend.
3505 */
3506 if (s->be->cookie_name == NULL && sess->fe->capture_name == NULL)
3507 break;
3508
3509 /* OK so now we know we have to process this response cookie.
3510 * The format of the Set-Cookie header is slightly different
3511 * from the format of the Cookie header in that it does not
3512 * support the comma as a cookie delimiter (thus the header
3513 * cannot be folded) because the Expires attribute described in
3514 * the original Netscape's spec may contain an unquoted date
3515 * with a comma inside. We have to live with this because
3516 * many browsers don't support Max-Age and some browsers don't
3517 * support quoted strings. However the Set-Cookie2 header is
3518 * clean.
3519 *
3520 * We have to keep multiple pointers in order to support cookie
3521 * removal at the beginning, middle or end of header without
3522 * corrupting the header (in case of set-cookie2). A special
3523 * pointer, <scav> points to the beginning of the set-cookie-av
3524 * fields after the first semi-colon. The <next> pointer points
3525 * either to the end of line (set-cookie) or next unquoted comma
3526 * (set-cookie2). All of these headers are valid :
3527 *
3528 * hdr_beg hdr_end
3529 * | |
3530 * v |
3531 * NAME1 = VALUE 1 ; Secure; Path="/" |
3532 * NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT v
3533 * NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT
3534 * NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard
3535 * | | | | | | | |
3536 * | | | | | | | +-> next
3537 * | | | | | | +------------> scav
3538 * | | | | | +--------------> val_end
3539 * | | | | +--------------------> val_beg
3540 * | | | +----------------------> equal
3541 * | | +------------------------> att_end
3542 * | +----------------------------> att_beg
3543 * +------------------------------> prev
3544 * -------------------------------> hdr_beg
3545 */
3546 hdr_beg = ctx.value.ptr;
3547 hdr_end = hdr_beg + ctx.value.len;
3548 for (prev = hdr_beg; prev < hdr_end; prev = next) {
3549
3550 /* Iterate through all cookies on this line */
3551
3552 /* find att_beg */
3553 att_beg = prev;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003554 if (!is_first)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003555 att_beg++;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003556 is_first = 0;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003557
3558 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
3559 att_beg++;
3560
3561 /* find att_end : this is the first character after the last non
3562 * space before the equal. It may be equal to hdr_end.
3563 */
3564 equal = att_end = att_beg;
3565
3566 while (equal < hdr_end) {
3567 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
3568 break;
3569 if (HTTP_IS_SPHT(*equal++))
3570 continue;
3571 att_end = equal;
3572 }
3573
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003574 /* here, <equal> points to '=', a delimiter or the end. <att_end>
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003575 * is between <att_beg> and <equal>, both may be identical.
3576 */
3577
3578 /* look for end of cookie if there is an equal sign */
3579 if (equal < hdr_end && *equal == '=') {
3580 /* look for the beginning of the value */
3581 val_beg = equal + 1;
3582 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
3583 val_beg++;
3584
3585 /* find the end of the value, respecting quotes */
3586 next = http_find_cookie_value_end(val_beg, hdr_end);
3587
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003588 /* make val_end point to the first white space or delimiter after the value */
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003589 val_end = next;
3590 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
3591 val_end--;
3592 }
3593 else {
3594 /* <equal> points to next comma, semi-colon or EOL */
3595 val_beg = val_end = next = equal;
3596 }
3597
3598 if (next < hdr_end) {
3599 /* Set-Cookie2 supports multiple cookies, and <next> points to
3600 * a colon or semi-colon before the end. So skip all attr-value
3601 * pairs and look for the next comma. For Set-Cookie, since
3602 * commas are permitted in values, skip to the end.
3603 */
3604 if (is_cookie2)
3605 next = http_find_hdr_value_end(next, hdr_end);
3606 else
3607 next = hdr_end;
3608 }
3609
3610 /* Now everything is as on the diagram above */
3611
3612 /* Ignore cookies with no equal sign */
3613 if (equal == val_end)
3614 continue;
3615
3616 /* If there are spaces around the equal sign, we need to
3617 * strip them otherwise we'll get trouble for cookie captures,
3618 * or even for rewrites. Since this happens extremely rarely,
3619 * it does not hurt performance.
3620 */
3621 if (unlikely(att_end != equal || val_beg > equal + 1)) {
3622 int stripped_before = 0;
3623 int stripped_after = 0;
3624
3625 if (att_end != equal) {
3626 memmove(att_end, equal, hdr_end - equal);
3627 stripped_before = (att_end - equal);
3628 equal += stripped_before;
3629 val_beg += stripped_before;
3630 }
3631
3632 if (val_beg > equal + 1) {
3633 memmove(equal + 1, val_beg, hdr_end + stripped_before - val_beg);
3634 stripped_after = (equal + 1) - val_beg;
3635 val_beg += stripped_after;
3636 stripped_before += stripped_after;
3637 }
3638
3639 val_end += stripped_before;
3640 next += stripped_before;
3641 hdr_end += stripped_before;
3642
Christopher Faulet3e2638e2019-06-18 09:49:16 +02003643 htx_change_blk_value_len(htx, ctx.blk, hdr_end - hdr_beg);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003644 ctx.value.len = hdr_end - hdr_beg;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003645 }
3646
3647 /* First, let's see if we want to capture this cookie. We check
3648 * that we don't already have a server side cookie, because we
3649 * can only capture one. Also as an optimisation, we ignore
3650 * cookies shorter than the declared name.
3651 */
3652 if (sess->fe->capture_name != NULL &&
3653 txn->srv_cookie == NULL &&
3654 (val_end - att_beg >= sess->fe->capture_namelen) &&
3655 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
3656 int log_len = val_end - att_beg;
3657 if ((txn->srv_cookie = pool_alloc(pool_head_capture)) == NULL) {
3658 ha_alert("HTTP logging : out of memory.\n");
3659 }
3660 else {
3661 if (log_len > sess->fe->capture_len)
3662 log_len = sess->fe->capture_len;
3663 memcpy(txn->srv_cookie, att_beg, log_len);
3664 txn->srv_cookie[log_len] = 0;
3665 }
3666 }
3667
3668 srv = objt_server(s->target);
3669 /* now check if we need to process it for persistence */
3670 if (!(s->flags & SF_IGNORE_PRST) &&
3671 (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
3672 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
3673 /* assume passive cookie by default */
3674 txn->flags &= ~TX_SCK_MASK;
3675 txn->flags |= TX_SCK_FOUND;
3676
3677 /* If the cookie is in insert mode on a known server, we'll delete
3678 * this occurrence because we'll insert another one later.
3679 * We'll delete it too if the "indirect" option is set and we're in
3680 * a direct access.
3681 */
3682 if (s->be->ck_opts & PR_CK_PSV) {
3683 /* The "preserve" flag was set, we don't want to touch the
3684 * server's cookie.
3685 */
3686 }
3687 else if ((srv && (s->be->ck_opts & PR_CK_INS)) ||
3688 ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) {
3689 /* this cookie must be deleted */
3690 if (prev == hdr_beg && next == hdr_end) {
3691 /* whole header */
3692 http_remove_header(htx, &ctx);
3693 /* note: while both invalid now, <next> and <hdr_end>
3694 * are still equal, so the for() will stop as expected.
3695 */
3696 } else {
3697 /* just remove the value */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003698 int delta = http_del_hdr_value(hdr_beg, hdr_end, &prev, next);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003699 next = prev;
3700 hdr_end += delta;
3701 }
3702 txn->flags &= ~TX_SCK_MASK;
3703 txn->flags |= TX_SCK_DELETED;
3704 /* and go on with next cookie */
3705 }
3706 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) {
3707 /* replace bytes val_beg->val_end with the cookie name associated
3708 * with this server since we know it.
3709 */
3710 int sliding, delta;
3711
3712 ctx.value = ist2(val_beg, val_end - val_beg);
3713 ctx.lws_before = ctx.lws_after = 0;
3714 http_replace_header_value(htx, &ctx, ist2(srv->cookie, srv->cklen));
3715 delta = srv->cklen - (val_end - val_beg);
3716 sliding = (ctx.value.ptr - val_beg);
3717 hdr_beg += sliding;
3718 val_beg += sliding;
3719 next += sliding + delta;
3720 hdr_end += sliding + delta;
3721
3722 txn->flags &= ~TX_SCK_MASK;
3723 txn->flags |= TX_SCK_REPLACED;
3724 }
3725 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) {
3726 /* insert the cookie name associated with this server
3727 * before existing cookie, and insert a delimiter between them..
3728 */
3729 int sliding, delta;
3730 ctx.value = ist2(val_beg, 0);
3731 ctx.lws_before = ctx.lws_after = 0;
3732 http_replace_header_value(htx, &ctx, ist2(srv->cookie, srv->cklen + 1));
3733 delta = srv->cklen + 1;
3734 sliding = (ctx.value.ptr - val_beg);
3735 hdr_beg += sliding;
3736 val_beg += sliding;
3737 next += sliding + delta;
3738 hdr_end += sliding + delta;
3739
3740 val_beg[srv->cklen] = COOKIE_DELIM;
3741 txn->flags &= ~TX_SCK_MASK;
3742 txn->flags |= TX_SCK_REPLACED;
3743 }
3744 }
3745 /* that's done for this cookie, check the next one on the same
3746 * line when next != hdr_end (only if is_cookie2).
3747 */
3748 }
3749 }
3750}
3751
Christopher Faulet25a02f62018-10-24 12:00:25 +02003752/*
3753 * Parses the Cache-Control and Pragma request header fields to determine if
3754 * the request may be served from the cache and/or if it is cacheable. Updates
3755 * s->txn->flags.
3756 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003757void http_check_request_for_cacheability(struct stream *s, struct channel *req)
Christopher Faulet25a02f62018-10-24 12:00:25 +02003758{
3759 struct http_txn *txn = s->txn;
3760 struct htx *htx;
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003761 struct http_hdr_ctx ctx = { .blk = NULL };
3762 int pragma_found, cc_found;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003763
3764 if ((txn->flags & (TX_CACHEABLE|TX_CACHE_IGNORE)) == TX_CACHE_IGNORE)
3765 return; /* nothing more to do here */
3766
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003767 htx = htxbuf(&req->buf);
Christopher Faulet25a02f62018-10-24 12:00:25 +02003768 pragma_found = cc_found = 0;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003769
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003770 /* Check "pragma" header for HTTP/1.0 compatibility. */
3771 if (http_find_header(htx, ist("pragma"), &ctx, 1)) {
3772 if (isteqi(ctx.value, ist("no-cache"))) {
3773 pragma_found = 1;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003774 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003775 }
Christopher Faulet25a02f62018-10-24 12:00:25 +02003776
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003777 ctx.blk = NULL;
3778 /* Don't use the cache and don't try to store if we found the
3779 * Authorization header */
3780 if (http_find_header(htx, ist("authorization"), &ctx, 1)) {
3781 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3782 txn->flags |= TX_CACHE_IGNORE;
3783 }
Christopher Faulet25a02f62018-10-24 12:00:25 +02003784
Christopher Faulet25a02f62018-10-24 12:00:25 +02003785
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003786 /* Look for "cache-control" header and iterate over all the values
3787 * until we find one that specifies that caching is possible or not. */
3788 ctx.blk = NULL;
3789 while (http_find_header(htx, ist("cache-control"), &ctx, 0)) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003790 cc_found = 1;
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003791 /* We don't check the values after max-age, max-stale nor min-fresh,
3792 * we simply don't use the cache when they're specified. */
3793 if (istmatchi(ctx.value, ist("max-age")) ||
3794 istmatchi(ctx.value, ist("no-cache")) ||
3795 istmatchi(ctx.value, ist("max-stale")) ||
3796 istmatchi(ctx.value, ist("min-fresh"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003797 txn->flags |= TX_CACHE_IGNORE;
3798 continue;
3799 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003800 if (istmatchi(ctx.value, ist("no-store"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003801 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3802 continue;
3803 }
3804 }
3805
3806 /* RFC7234#5.4:
3807 * When the Cache-Control header field is also present and
3808 * understood in a request, Pragma is ignored.
3809 * When the Cache-Control header field is not present in a
3810 * request, caches MUST consider the no-cache request
3811 * pragma-directive as having the same effect as if
3812 * "Cache-Control: no-cache" were present.
3813 */
3814 if (!cc_found && pragma_found)
3815 txn->flags |= TX_CACHE_IGNORE;
3816}
3817
3818/*
3819 * Check if response is cacheable or not. Updates s->txn->flags.
3820 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003821void http_check_response_for_cacheability(struct stream *s, struct channel *res)
Christopher Faulet25a02f62018-10-24 12:00:25 +02003822{
3823 struct http_txn *txn = s->txn;
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003824 struct http_hdr_ctx ctx = { .blk = NULL };
Christopher Faulet25a02f62018-10-24 12:00:25 +02003825 struct htx *htx;
Remi Tricot-Le Bretoncc9bf2e2020-11-12 11:14:41 +01003826 int has_freshness_info = 0;
3827 int has_validator = 0;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003828
3829 if (txn->status < 200) {
3830 /* do not try to cache interim responses! */
3831 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3832 return;
3833 }
3834
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003835 htx = htxbuf(&res->buf);
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003836 /* Check "pragma" header for HTTP/1.0 compatibility. */
3837 if (http_find_header(htx, ist("pragma"), &ctx, 1)) {
3838 if (isteqi(ctx.value, ist("no-cache"))) {
3839 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3840 return;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003841 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003842 }
Christopher Faulet25a02f62018-10-24 12:00:25 +02003843
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003844 /* Look for "cache-control" header and iterate over all the values
3845 * until we find one that specifies that caching is possible or not. */
3846 ctx.blk = NULL;
3847 while (http_find_header(htx, ist("cache-control"), &ctx, 0)) {
3848 if (isteqi(ctx.value, ist("public"))) {
3849 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003850 continue;
3851 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003852 if (isteqi(ctx.value, ist("private")) ||
3853 isteqi(ctx.value, ist("no-cache")) ||
3854 isteqi(ctx.value, ist("no-store")) ||
3855 isteqi(ctx.value, ist("max-age=0")) ||
3856 isteqi(ctx.value, ist("s-maxage=0"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003857 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003858 continue;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003859 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003860 /* We might have a no-cache="set-cookie" form. */
3861 if (istmatchi(ctx.value, ist("no-cache=\"set-cookie"))) {
3862 txn->flags &= ~TX_CACHE_COOK;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003863 continue;
3864 }
Remi Tricot-Le Bretoncc9bf2e2020-11-12 11:14:41 +01003865
3866 if (istmatchi(ctx.value, ist("s-maxage")) ||
3867 istmatchi(ctx.value, ist("max-age"))) {
3868 has_freshness_info = 1;
3869 continue;
3870 }
3871 }
3872
3873 /* If no freshness information could be found in Cache-Control values,
3874 * look for an Expires header. */
3875 if (!has_freshness_info) {
3876 ctx.blk = NULL;
3877 has_freshness_info = http_find_header(htx, ist("expires"), &ctx, 0);
Christopher Faulet25a02f62018-10-24 12:00:25 +02003878 }
Remi Tricot-Le Bretoncc9bf2e2020-11-12 11:14:41 +01003879
3880 /* If no freshness information could be found in Cache-Control or Expires
3881 * values, look for an explicit validator. */
3882 if (!has_freshness_info) {
3883 ctx.blk = NULL;
3884 has_validator = 1;
3885 if (!http_find_header(htx, ist("etag"), &ctx, 0)) {
3886 ctx.blk = NULL;
3887 if (!http_find_header(htx, ist("last-modified"), &ctx, 0))
3888 has_validator = 0;
3889 }
3890 }
3891
3892 /* We won't store an entry that has neither a cache validator nor an
3893 * explicit expiration time, as suggested in RFC 7234#3. */
3894 if (!has_freshness_info && !has_validator)
3895 txn->flags |= TX_CACHE_IGNORE;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003896}
3897
Christopher Faulet377c5a52018-10-24 21:21:30 +02003898/*
3899 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
3900 * for the current backend.
3901 *
3902 * It is assumed that the request is either a HEAD, GET, or POST and that the
3903 * uri_auth field is valid.
3904 *
3905 * Returns 1 if stats should be provided, otherwise 0.
3906 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003907static int http_stats_check_uri(struct stream *s, struct http_txn *txn, struct proxy *backend)
Christopher Faulet377c5a52018-10-24 21:21:30 +02003908{
3909 struct uri_auth *uri_auth = backend->uri_auth;
3910 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003911 struct htx_sl *sl;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003912 struct ist uri;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003913
3914 if (!uri_auth)
3915 return 0;
3916
3917 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
3918 return 0;
3919
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003920 htx = htxbuf(&s->req.buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02003921 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003922 uri = htx_sl_req_uri(sl);
Willy Tarreau1eb3b482019-10-31 15:50:28 +01003923 if (*uri_auth->uri_prefix == '/')
3924 uri = http_get_path(uri);
Christopher Faulet377c5a52018-10-24 21:21:30 +02003925
3926 /* check URI size */
3927 if (uri_auth->uri_len > uri.len)
3928 return 0;
3929
3930 if (memcmp(uri.ptr, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
3931 return 0;
3932
3933 return 1;
3934}
3935
3936/* This function prepares an applet to handle the stats. It can deal with the
3937 * "100-continue" expectation, check that admin rules are met for POST requests,
3938 * and program a response message if something was unexpected. It cannot fail
3939 * and always relies on the stats applet to complete the job. It does not touch
3940 * analysers nor counters, which are left to the caller. It does not touch
3941 * s->target which is supposed to already point to the stats applet. The caller
3942 * is expected to have already assigned an appctx to the stream.
3943 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003944static int http_handle_stats(struct stream *s, struct channel *req)
Christopher Faulet377c5a52018-10-24 21:21:30 +02003945{
3946 struct stats_admin_rule *stats_admin_rule;
3947 struct stream_interface *si = &s->si[1];
3948 struct session *sess = s->sess;
3949 struct http_txn *txn = s->txn;
3950 struct http_msg *msg = &txn->req;
3951 struct uri_auth *uri_auth = s->be->uri_auth;
3952 const char *h, *lookup, *end;
3953 struct appctx *appctx;
3954 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003955 struct htx_sl *sl;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003956
3957 appctx = si_appctx(si);
3958 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
3959 appctx->st1 = appctx->st2 = 0;
3960 appctx->ctx.stats.st_code = STAT_STATUS_INIT;
Willy Tarreau676c29e2019-10-09 10:50:01 +02003961 appctx->ctx.stats.flags |= uri_auth->flags;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003962 appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */
3963 if ((msg->flags & HTTP_MSGF_VER_11) && (txn->meth != HTTP_METH_HEAD))
3964 appctx->ctx.stats.flags |= STAT_CHUNKED;
3965
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003966 htx = htxbuf(&req->buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02003967 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003968 lookup = HTX_SL_REQ_UPTR(sl) + uri_auth->uri_len;
3969 end = HTX_SL_REQ_UPTR(sl) + HTX_SL_REQ_ULEN(sl);
Christopher Faulet377c5a52018-10-24 21:21:30 +02003970
3971 for (h = lookup; h <= end - 3; h++) {
3972 if (memcmp(h, ";up", 3) == 0) {
3973 appctx->ctx.stats.flags |= STAT_HIDE_DOWN;
3974 break;
3975 }
Amaury Denoyelle91e55ea2021-02-25 14:46:08 +01003976 }
3977
3978 for (h = lookup; h <= end - 9; h++) {
3979 if (memcmp(h, ";no-maint", 9) == 0) {
Willy Tarreau3e320362020-10-23 17:28:57 +02003980 appctx->ctx.stats.flags |= STAT_HIDE_MAINT;
3981 break;
3982 }
Christopher Faulet377c5a52018-10-24 21:21:30 +02003983 }
3984
3985 if (uri_auth->refresh) {
3986 for (h = lookup; h <= end - 10; h++) {
3987 if (memcmp(h, ";norefresh", 10) == 0) {
3988 appctx->ctx.stats.flags |= STAT_NO_REFRESH;
3989 break;
3990 }
3991 }
3992 }
3993
3994 for (h = lookup; h <= end - 4; h++) {
3995 if (memcmp(h, ";csv", 4) == 0) {
Christopher Faulet6338a082019-09-09 15:50:54 +02003996 appctx->ctx.stats.flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
Christopher Faulet377c5a52018-10-24 21:21:30 +02003997 break;
3998 }
3999 }
4000
4001 for (h = lookup; h <= end - 6; h++) {
4002 if (memcmp(h, ";typed", 6) == 0) {
Christopher Faulet6338a082019-09-09 15:50:54 +02004003 appctx->ctx.stats.flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
Christopher Faulet377c5a52018-10-24 21:21:30 +02004004 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
4005 break;
4006 }
4007 }
4008
Christopher Faulet6338a082019-09-09 15:50:54 +02004009 for (h = lookup; h <= end - 5; h++) {
4010 if (memcmp(h, ";json", 5) == 0) {
4011 appctx->ctx.stats.flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
4012 appctx->ctx.stats.flags |= STAT_FMT_JSON;
4013 break;
4014 }
4015 }
4016
4017 for (h = lookup; h <= end - 12; h++) {
4018 if (memcmp(h, ";json-schema", 12) == 0) {
4019 appctx->ctx.stats.flags &= ~STAT_FMT_MASK;
4020 appctx->ctx.stats.flags |= STAT_JSON_SCHM;
4021 break;
4022 }
4023 }
4024
Christopher Faulet377c5a52018-10-24 21:21:30 +02004025 for (h = lookup; h <= end - 8; h++) {
4026 if (memcmp(h, ";st=", 4) == 0) {
4027 int i;
4028 h += 4;
4029 appctx->ctx.stats.st_code = STAT_STATUS_UNKN;
4030 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
4031 if (strncmp(stat_status_codes[i], h, 4) == 0) {
4032 appctx->ctx.stats.st_code = i;
4033 break;
4034 }
4035 }
4036 break;
4037 }
4038 }
4039
4040 appctx->ctx.stats.scope_str = 0;
4041 appctx->ctx.stats.scope_len = 0;
4042 for (h = lookup; h <= end - 8; h++) {
4043 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
4044 int itx = 0;
4045 const char *h2;
4046 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
4047 const char *err;
4048
4049 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
4050 h2 = h;
Christopher Fauleted7a0662019-01-14 11:07:34 +01004051 appctx->ctx.stats.scope_str = h2 - HTX_SL_REQ_UPTR(sl);
4052 while (h < end) {
Christopher Faulet377c5a52018-10-24 21:21:30 +02004053 if (*h == ';' || *h == '&' || *h == ' ')
4054 break;
4055 itx++;
4056 h++;
4057 }
4058
4059 if (itx > STAT_SCOPE_TXT_MAXLEN)
4060 itx = STAT_SCOPE_TXT_MAXLEN;
4061 appctx->ctx.stats.scope_len = itx;
4062
4063 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
4064 memcpy(scope_txt, h2, itx);
4065 scope_txt[itx] = '\0';
4066 err = invalid_char(scope_txt);
4067 if (err) {
4068 /* bad char in search text => clear scope */
4069 appctx->ctx.stats.scope_str = 0;
4070 appctx->ctx.stats.scope_len = 0;
4071 }
4072 break;
4073 }
4074 }
4075
4076 /* now check whether we have some admin rules for this request */
4077 list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) {
4078 int ret = 1;
4079
4080 if (stats_admin_rule->cond) {
4081 ret = acl_exec_cond(stats_admin_rule->cond, s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
4082 ret = acl_pass(ret);
4083 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
4084 ret = !ret;
4085 }
4086
4087 if (ret) {
4088 /* no rule, or the rule matches */
4089 appctx->ctx.stats.flags |= STAT_ADMIN;
4090 break;
4091 }
4092 }
4093
Christopher Faulet5d45e382019-02-27 15:15:23 +01004094 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
4095 appctx->st0 = STAT_HTTP_HEAD;
4096 else if (txn->meth == HTTP_METH_POST) {
Christopher Fauletbd9e8422019-08-15 22:26:48 +02004097 if (appctx->ctx.stats.flags & STAT_ADMIN) {
Christopher Faulet377c5a52018-10-24 21:21:30 +02004098 appctx->st0 = STAT_HTTP_POST;
Christopher Fauletbd9e8422019-08-15 22:26:48 +02004099 if (msg->msg_state < HTTP_MSG_DATA)
4100 req->analysers |= AN_REQ_HTTP_BODY;
4101 }
Christopher Faulet377c5a52018-10-24 21:21:30 +02004102 else {
Christopher Faulet5d45e382019-02-27 15:15:23 +01004103 /* POST without admin level */
Christopher Faulet377c5a52018-10-24 21:21:30 +02004104 appctx->ctx.stats.flags &= ~STAT_CHUNKED;
4105 appctx->ctx.stats.st_code = STAT_STATUS_DENY;
4106 appctx->st0 = STAT_HTTP_LAST;
4107 }
4108 }
4109 else {
Christopher Faulet5d45e382019-02-27 15:15:23 +01004110 /* Unsupported method */
4111 appctx->ctx.stats.flags &= ~STAT_CHUNKED;
4112 appctx->ctx.stats.st_code = STAT_STATUS_IVAL;
4113 appctx->st0 = STAT_HTTP_LAST;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004114 }
4115
4116 s->task->nice = -32; /* small boost for HTTP statistics */
4117 return 1;
4118}
4119
Christopher Faulet021a8e42021-03-29 10:46:38 +02004120/* This function waits for the message payload at most <time> milliseconds (may
4121 * be set to TICK_ETERNITY). It stops to wait if at least <bytes> bytes of the
4122 * payload are received (0 means no limit). It returns HTTP_RULE_* depending on
4123 * the result:
4124 *
4125 * - HTTP_RULE_RES_CONT when conditions are met to stop waiting
4126 * - HTTP_RULE_RES_YIELD to wait for more data
Ilya Shipitsinb2be9a12021-04-24 13:25:42 +05004127 * - HTTP_RULE_RES_ABRT when a timeout occurred.
Christopher Faulet021a8e42021-03-29 10:46:38 +02004128 * - HTTP_RULE_RES_BADREQ if a parsing error is raised by lower level
Ilya Shipitsinb2be9a12021-04-24 13:25:42 +05004129 * - HTTP_RULE_RES_ERROR if an internal error occurred
Christopher Faulet021a8e42021-03-29 10:46:38 +02004130 *
Ilya Shipitsinb2be9a12021-04-24 13:25:42 +05004131 * If a timeout occurred, this function is responsible to emit the right response
Christopher Faulet021a8e42021-03-29 10:46:38 +02004132 * to the client, depending on the channel (408 on request side, 504 on response
4133 * side). All other errors must be handled by the caller.
4134 */
4135enum rule_result http_wait_for_msg_body(struct stream *s, struct channel *chn,
4136 unsigned int time, unsigned int bytes)
4137{
4138 struct session *sess = s->sess;
4139 struct http_txn *txn = s->txn;
4140 struct http_msg *msg = ((chn->flags & CF_ISRESP) ? &txn->rsp : &txn->req);
4141 struct htx *htx;
4142 enum rule_result ret = HTTP_RULE_RES_CONT;
4143
4144 htx = htxbuf(&chn->buf);
4145
4146 if (htx->flags & HTX_FL_PARSING_ERROR) {
4147 ret = HTTP_RULE_RES_BADREQ;
4148 goto end;
4149 }
4150 if (htx->flags & HTX_FL_PROCESSING_ERROR) {
4151 ret = HTTP_RULE_RES_ERROR;
4152 goto end;
4153 }
4154
4155 /* Do nothing for bodyless and CONNECT requests */
4156 if (txn->meth == HTTP_METH_CONNECT || (msg->flags & HTTP_MSGF_BODYLESS))
4157 goto end;
4158
4159 if (!(chn->flags & CF_ISRESP) && msg->msg_state < HTTP_MSG_DATA) {
4160 if (http_handle_expect_hdr(s, htx, msg) == -1) {
4161 ret = HTTP_RULE_RES_ERROR;
4162 goto end;
4163 }
4164 }
4165
4166 msg->msg_state = HTTP_MSG_DATA;
4167
4168 /* Now we're in HTTP_MSG_DATA. We just need to know if all data have
4169 * been received or if the buffer is full.
4170 */
4171 if ((htx->flags & HTX_FL_EOM) || htx_get_tail_type(htx) > HTX_BLK_DATA ||
4172 channel_htx_full(chn, htx, global.tune.maxrewrite))
4173 goto end;
4174
4175 if (bytes) {
4176 struct htx_blk *blk;
4177 unsigned int len = 0;
4178
4179 for (blk = htx_get_first_blk(htx); blk; blk = htx_get_next_blk(htx, blk)) {
4180 if (htx_get_blk_type(blk) != HTX_BLK_DATA)
4181 continue;
4182 len += htx_get_blksz(blk);
4183 if (len >= bytes)
4184 goto end;
4185 }
4186 }
4187
4188 if ((chn->flags & CF_READ_TIMEOUT) || tick_is_expired(chn->analyse_exp, now_ms)) {
4189 if (!(chn->flags & CF_ISRESP))
4190 goto abort_req;
4191 goto abort_res;
4192 }
4193
4194 /* we get here if we need to wait for more data */
4195 if (!(chn->flags & (CF_SHUTR | CF_READ_ERROR))) {
4196 if (!tick_isset(chn->analyse_exp))
4197 chn->analyse_exp = tick_add_ifset(now_ms, time);
4198 ret = HTTP_RULE_RES_YIELD;
4199 }
4200
4201 end:
4202 return ret;
4203
4204 abort_req:
4205 txn->status = 408;
4206 if (!(s->flags & SF_ERR_MASK))
4207 s->flags |= SF_ERR_CLITO;
4208 if (!(s->flags & SF_FINST_MASK))
4209 s->flags |= SF_FINST_D;
Willy Tarreau4781b152021-04-06 13:53:36 +02004210 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req);
Christopher Faulet021a8e42021-03-29 10:46:38 +02004211 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02004212 _HA_ATOMIC_INC(&sess->listener->counters->failed_req);
Christopher Faulet021a8e42021-03-29 10:46:38 +02004213 http_reply_and_close(s, txn->status, http_error_message(s));
4214 ret = HTTP_RULE_RES_ABRT;
4215 goto end;
4216
4217 abort_res:
4218 txn->status = 504;
4219 if (!(s->flags & SF_ERR_MASK))
4220 s->flags |= SF_ERR_SRVTO;
4221 if (!(s->flags & SF_FINST_MASK))
4222 s->flags |= SF_FINST_D;
4223 stream_inc_http_fail_ctr(s);
4224 http_reply_and_close(s, txn->status, http_error_message(s));
4225 ret = HTTP_RULE_RES_ABRT;
4226 goto end;
4227}
4228
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004229void http_perform_server_redirect(struct stream *s, struct stream_interface *si)
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004230{
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004231 struct channel *req = &s->req;
4232 struct channel *res = &s->res;
4233 struct server *srv;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004234 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004235 struct htx_sl *sl;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004236 struct ist path, location;
4237 unsigned int flags;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004238
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004239 /*
4240 * Create the location
4241 */
4242 chunk_reset(&trash);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004243
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004244 /* 1: add the server's prefix */
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004245 /* special prefix "/" means don't change URL */
4246 srv = __objt_server(s->target);
4247 if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') {
4248 if (!chunk_memcat(&trash, srv->rdr_pfx, srv->rdr_len))
4249 return;
4250 }
4251
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004252 /* 2: add the request Path */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004253 htx = htxbuf(&req->buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02004254 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004255 path = http_get_path(htx_sl_req_uri(sl));
Tim Duesterhused526372020-03-05 17:56:33 +01004256 if (!isttest(path))
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004257 return;
4258
4259 if (!chunk_memcat(&trash, path.ptr, path.len))
4260 return;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004261 location = ist2(trash.area, trash.data);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004262
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004263 /*
4264 * Create the 302 respone
4265 */
4266 htx = htx_from_buf(&res->buf);
4267 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
4268 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
4269 ist("HTTP/1.1"), ist("302"), ist("Found"));
4270 if (!sl)
4271 goto fail;
4272 sl->info.res.status = 302;
4273 s->txn->status = 302;
4274
4275 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) ||
4276 !htx_add_header(htx, ist("Connection"), ist("close")) ||
4277 !htx_add_header(htx, ist("Content-length"), ist("0")) ||
4278 !htx_add_header(htx, ist("Location"), location))
4279 goto fail;
4280
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004281 if (!htx_add_endof(htx, HTX_BLK_EOH))
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004282 goto fail;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004283
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004284 htx->flags |= HTX_FL_EOM;
Christopher Fauletc20afb82020-01-24 19:16:26 +01004285 htx_to_buf(htx, &res->buf);
Christopher Fauleta72a7e42020-01-28 09:28:11 +01004286 if (!http_forward_proxy_resp(s, 1))
4287 goto fail;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004288
4289 /* return without error. */
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004290 si_shutr(si);
4291 si_shutw(si);
4292 si->err_type = SI_ET_NONE;
4293 si->state = SI_ST_CLO;
4294
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004295 if (!(s->flags & SF_ERR_MASK))
4296 s->flags |= SF_ERR_LOCAL;
4297 if (!(s->flags & SF_FINST_MASK))
4298 s->flags |= SF_FINST_C;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004299
4300 /* FIXME: we should increase a counter of redirects per server and per backend. */
4301 srv_inc_sess_ctr(srv);
4302 srv_set_sess_last(srv);
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004303 return;
4304
4305 fail:
4306 /* If an error occurred, remove the incomplete HTTP response from the
4307 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004308 channel_htx_truncate(res, htx);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004309}
4310
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05004311/* This function terminates the request because it was completely analyzed or
Christopher Fauletf2824e62018-10-01 12:12:37 +02004312 * because an error was triggered during the body forwarding.
4313 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004314static void http_end_request(struct stream *s)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004315{
4316 struct channel *chn = &s->req;
4317 struct http_txn *txn = s->txn;
4318
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004319 DBG_TRACE_ENTER(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004320
Christopher Fauletb42a8b62018-11-19 21:59:00 +01004321 if (unlikely(txn->req.msg_state == HTTP_MSG_ERROR ||
4322 txn->rsp.msg_state == HTTP_MSG_ERROR)) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004323 channel_abort(chn);
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004324 channel_htx_truncate(chn, htxbuf(&chn->buf));
Christopher Fauletf2824e62018-10-01 12:12:37 +02004325 goto end;
4326 }
4327
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004328 if (unlikely(txn->req.msg_state < HTTP_MSG_DONE)) {
4329 DBG_TRACE_DEVEL("waiting end of the request", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004330 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004331 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004332
4333 if (txn->req.msg_state == HTTP_MSG_DONE) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004334 /* No need to read anymore, the request was completely parsed.
4335 * We can shut the read side unless we want to abort_on_close,
4336 * or we have a POST request. The issue with POST requests is
4337 * that some browsers still send a CRLF after the request, and
4338 * this CRLF must be read so that it does not remain in the kernel
4339 * buffers, otherwise a close could cause an RST on some systems
4340 * (eg: Linux).
4341 */
Christopher Faulet769d0e92019-03-22 14:23:18 +01004342 if (!(s->be->options & PR_O_ABRT_CLOSE) && txn->meth != HTTP_METH_POST)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004343 channel_dont_read(chn);
4344
4345 /* if the server closes the connection, we want to immediately react
4346 * and close the socket to save packets and syscalls.
4347 */
4348 s->si[1].flags |= SI_FL_NOHALF;
4349
4350 /* In any case we've finished parsing the request so we must
4351 * disable Nagle when sending data because 1) we're not going
4352 * to shut this side, and 2) the server is waiting for us to
4353 * send pending data.
4354 */
4355 chn->flags |= CF_NEVER_WAIT;
4356
Christopher Fauletd01ce402019-01-02 17:44:13 +01004357 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
4358 /* The server has not finished to respond, so we
4359 * don't want to move in order not to upset it.
4360 */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004361 DBG_TRACE_DEVEL("waiting end of the response", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletd01ce402019-01-02 17:44:13 +01004362 return;
4363 }
4364
Christopher Fauletf2824e62018-10-01 12:12:37 +02004365 /* When we get here, it means that both the request and the
4366 * response have finished receiving. Depending on the connection
4367 * mode, we'll have to wait for the last bytes to leave in either
4368 * direction, and sometimes for a close to be effective.
4369 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02004370 if (txn->flags & TX_CON_WANT_TUN) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004371 /* Tunnel mode will not have any analyser so it needs to
4372 * poll for reads.
4373 */
4374 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004375 if (b_data(&chn->buf)) {
4376 DBG_TRACE_DEVEL("waiting to flush the request", STRM_EV_HTTP_ANA, s, txn);
Christopher Faulet9768c262018-10-22 09:34:31 +02004377 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004378 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004379 txn->req.msg_state = HTTP_MSG_TUNNEL;
4380 }
4381 else {
4382 /* we're not expecting any new data to come for this
4383 * transaction, so we can close it.
Christopher Faulet9768c262018-10-22 09:34:31 +02004384 *
4385 * However, there is an exception if the response
4386 * length is undefined. In this case, we need to wait
4387 * the close from the server. The response will be
4388 * switched in TUNNEL mode until the end.
Christopher Fauletf2824e62018-10-01 12:12:37 +02004389 */
4390 if (!(txn->rsp.flags & HTTP_MSGF_XFER_LEN) &&
4391 txn->rsp.msg_state != HTTP_MSG_CLOSED)
Christopher Faulet9768c262018-10-22 09:34:31 +02004392 goto check_channel_flags;
Christopher Fauletf2824e62018-10-01 12:12:37 +02004393
4394 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4395 channel_shutr_now(chn);
4396 channel_shutw_now(chn);
4397 }
4398 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004399 goto check_channel_flags;
4400 }
4401
4402 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
4403 http_msg_closing:
4404 /* nothing else to forward, just waiting for the output buffer
4405 * to be empty and for the shutw_now to take effect.
4406 */
4407 if (channel_is_empty(chn)) {
4408 txn->req.msg_state = HTTP_MSG_CLOSED;
4409 goto http_msg_closed;
4410 }
4411 else if (chn->flags & CF_SHUTW) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004412 txn->req.msg_state = HTTP_MSG_ERROR;
4413 goto end;
4414 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004415 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004416 return;
4417 }
4418
4419 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
4420 http_msg_closed:
Christopher Fauletf2824e62018-10-01 12:12:37 +02004421 /* if we don't know whether the server will close, we need to hard close */
4422 if (txn->rsp.flags & HTTP_MSGF_XFER_LEN)
4423 s->si[1].flags |= SI_FL_NOLINGER; /* we want to close ASAP */
Christopher Fauletf2824e62018-10-01 12:12:37 +02004424 /* see above in MSG_DONE why we only do this in these states */
Christopher Faulet769d0e92019-03-22 14:23:18 +01004425 if (!(s->be->options & PR_O_ABRT_CLOSE))
Christopher Fauletf2824e62018-10-01 12:12:37 +02004426 channel_dont_read(chn);
4427 goto end;
4428 }
4429
4430 check_channel_flags:
4431 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4432 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4433 /* if we've just closed an output, let's switch */
4434 txn->req.msg_state = HTTP_MSG_CLOSING;
4435 goto http_msg_closing;
4436 }
4437
4438 end:
4439 chn->analysers &= AN_REQ_FLT_END;
Christopher Faulet198ef8b2020-12-15 13:32:55 +01004440 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
4441 chn->flags |= CF_NEVER_WAIT;
4442 if (HAS_REQ_DATA_FILTERS(s))
Christopher Fauletf2824e62018-10-01 12:12:37 +02004443 chn->analysers |= AN_REQ_FLT_XFER_DATA;
Christopher Faulet198ef8b2020-12-15 13:32:55 +01004444 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004445 channel_auto_close(chn);
4446 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004447 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004448}
4449
4450
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05004451/* This function terminates the response because it was completely analyzed or
Christopher Fauletf2824e62018-10-01 12:12:37 +02004452 * because an error was triggered during the body forwarding.
4453 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004454static void http_end_response(struct stream *s)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004455{
4456 struct channel *chn = &s->res;
4457 struct http_txn *txn = s->txn;
4458
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004459 DBG_TRACE_ENTER(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004460
Christopher Fauletb42a8b62018-11-19 21:59:00 +01004461 if (unlikely(txn->req.msg_state == HTTP_MSG_ERROR ||
4462 txn->rsp.msg_state == HTTP_MSG_ERROR)) {
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004463 channel_htx_truncate(&s->req, htxbuf(&s->req.buf));
Christopher Faulet9768c262018-10-22 09:34:31 +02004464 channel_abort(&s->req);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004465 goto end;
4466 }
4467
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004468 if (unlikely(txn->rsp.msg_state < HTTP_MSG_DONE)) {
4469 DBG_TRACE_DEVEL("waiting end of the response", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004470 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004471 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004472
4473 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
4474 /* In theory, we don't need to read anymore, but we must
4475 * still monitor the server connection for a possible close
4476 * while the request is being uploaded, so we don't disable
4477 * reading.
4478 */
4479 /* channel_dont_read(chn); */
4480
4481 if (txn->req.msg_state < HTTP_MSG_DONE) {
4482 /* The client seems to still be sending data, probably
4483 * because we got an error response during an upload.
4484 * We have the choice of either breaking the connection
4485 * or letting it pass through. Let's do the later.
4486 */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004487 DBG_TRACE_DEVEL("waiting end of the request", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004488 return;
4489 }
4490
4491 /* When we get here, it means that both the request and the
4492 * response have finished receiving. Depending on the connection
4493 * mode, we'll have to wait for the last bytes to leave in either
4494 * direction, and sometimes for a close to be effective.
4495 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02004496 if (txn->flags & TX_CON_WANT_TUN) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004497 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004498 if (b_data(&chn->buf)) {
4499 DBG_TRACE_DEVEL("waiting to flush the respone", STRM_EV_HTTP_ANA, s, txn);
Christopher Faulet9768c262018-10-22 09:34:31 +02004500 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004501 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004502 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
4503 }
4504 else {
4505 /* we're not expecting any new data to come for this
4506 * transaction, so we can close it.
4507 */
4508 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4509 channel_shutr_now(chn);
4510 channel_shutw_now(chn);
4511 }
4512 }
4513 goto check_channel_flags;
4514 }
4515
4516 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
4517 http_msg_closing:
4518 /* nothing else to forward, just waiting for the output buffer
4519 * to be empty and for the shutw_now to take effect.
4520 */
4521 if (channel_is_empty(chn)) {
4522 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4523 goto http_msg_closed;
4524 }
4525 else if (chn->flags & CF_SHUTW) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004526 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau4781b152021-04-06 13:53:36 +02004527 _HA_ATOMIC_INC(&strm_sess(s)->fe->fe_counters.cli_aborts);
4528 _HA_ATOMIC_INC(&s->be->be_counters.cli_aborts);
William Lallemand36119de2021-03-08 15:26:48 +01004529 if (strm_sess(s)->listener && strm_sess(s)->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02004530 _HA_ATOMIC_INC(&strm_sess(s)->listener->counters->cli_aborts);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004531 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02004532 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.cli_aborts);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004533 goto end;
4534 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004535 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004536 return;
4537 }
4538
4539 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
4540 http_msg_closed:
4541 /* drop any pending data */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004542 channel_htx_truncate(&s->req, htxbuf(&s->req.buf));
Christopher Faulet9768c262018-10-22 09:34:31 +02004543 channel_abort(&s->req);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004544 goto end;
4545 }
4546
4547 check_channel_flags:
4548 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4549 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4550 /* if we've just closed an output, let's switch */
4551 txn->rsp.msg_state = HTTP_MSG_CLOSING;
4552 goto http_msg_closing;
4553 }
4554
4555 end:
4556 chn->analysers &= AN_RES_FLT_END;
Christopher Faulet198ef8b2020-12-15 13:32:55 +01004557 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
4558 chn->flags |= CF_NEVER_WAIT;
4559 if (HAS_RSP_DATA_FILTERS(s))
4560 chn->analysers |= AN_RES_FLT_XFER_DATA;
4561 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004562 channel_auto_close(chn);
4563 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004564 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004565}
4566
Christopher Fauletef70e252020-01-28 09:26:19 +01004567/* Forward a response generated by HAProxy (error/redirect/return). This
4568 * function forwards all pending incoming data. If <final> is set to 0, nothing
4569 * more is performed. It is used for 1xx informational messages. Otherwise, the
Christopher Faulet507479b2020-05-15 12:29:46 +02004570 * transaction is terminated and the request is emptied. On success 1 is
Christopher Faulet40e6b552020-06-25 16:04:50 +02004571 * returned. If an error occurred, 0 is returned. If it fails, this function
4572 * only exits. It is the caller responsibility to do the cleanup.
Christopher Fauletef70e252020-01-28 09:26:19 +01004573 */
4574int http_forward_proxy_resp(struct stream *s, int final)
4575{
4576 struct channel *req = &s->req;
4577 struct channel *res = &s->res;
4578 struct htx *htx = htxbuf(&res->buf);
4579 size_t data;
4580
4581 if (final) {
4582 htx->flags |= HTX_FL_PROXY_RESP;
Christopher Faulet507479b2020-05-15 12:29:46 +02004583
Christopher Fauletaab1b672020-11-18 16:44:02 +01004584 if (!htx_is_empty(htx) && !http_eval_after_res_rules(s))
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01004585 return 0;
Christopher Fauletef70e252020-01-28 09:26:19 +01004586
Christopher Fauletd6c48362020-10-19 18:01:38 +02004587 if (s->txn->meth == HTTP_METH_HEAD)
4588 htx_skip_msg_payload(htx);
4589
Christopher Fauletef70e252020-01-28 09:26:19 +01004590 channel_auto_read(req);
4591 channel_abort(req);
4592 channel_auto_close(req);
4593 channel_htx_erase(req, htxbuf(&req->buf));
4594
4595 res->wex = tick_add_ifset(now_ms, res->wto);
4596 channel_auto_read(res);
4597 channel_auto_close(res);
4598 channel_shutr_now(res);
Christopher Faulet1a9db7c2020-06-25 15:36:45 +02004599 res->flags |= CF_EOI; /* The response is terminated, add EOI */
Christopher Faulet42432f32020-11-20 17:43:16 +01004600 htxbuf(&res->buf)->flags |= HTX_FL_EOM; /* no more data are expected */
Christopher Fauletef70e252020-01-28 09:26:19 +01004601 }
Christopher Fauletcf6898c2020-06-25 15:55:11 +02004602 else {
4603 /* Send ASAP informational messages. Rely on CF_EOI for final
4604 * response.
4605 */
4606 res->flags |= CF_SEND_DONTWAIT;
4607 }
Christopher Fauletef70e252020-01-28 09:26:19 +01004608
4609 data = htx->data - co_data(res);
4610 c_adv(res, data);
4611 htx->first = -1;
4612 res->total += data;
4613 return 1;
4614}
4615
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004616void http_server_error(struct stream *s, struct stream_interface *si, int err,
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004617 int finst, struct http_reply *msg)
Christopher Faulet0f226952018-10-22 09:29:56 +02004618{
Christopher Faulet72c7d8d2020-01-27 15:32:25 +01004619 http_reply_and_close(s, s->txn->status, msg);
Christopher Faulet0f226952018-10-22 09:29:56 +02004620 if (!(s->flags & SF_ERR_MASK))
4621 s->flags |= err;
4622 if (!(s->flags & SF_FINST_MASK))
4623 s->flags |= finst;
4624}
4625
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004626void http_reply_and_close(struct stream *s, short status, struct http_reply *msg)
Christopher Faulet0f226952018-10-22 09:29:56 +02004627{
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004628 if (!msg) {
4629 channel_htx_truncate(&s->res, htxbuf(&s->res.buf));
4630 goto end;
4631 }
4632
4633 if (http_reply_message(s, msg) == -1) {
4634 /* On error, return a 500 error message, but don't rewrite it if
Christopher Faulet40e6b552020-06-25 16:04:50 +02004635 * it is already an internal error. If it was already a "const"
4636 * 500 error, just fail.
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004637 */
Christopher Faulet40e6b552020-06-25 16:04:50 +02004638 if (s->txn->status == 500) {
4639 if (s->txn->flags & TX_CONST_REPLY)
4640 goto end;
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004641 s->txn->flags |= TX_CONST_REPLY;
Christopher Faulet40e6b552020-06-25 16:04:50 +02004642 }
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004643 s->txn->status = 500;
4644 s->txn->http_reply = NULL;
4645 return http_reply_and_close(s, s->txn->status, http_error_message(s));
4646 }
4647
4648end:
4649 s->res.wex = tick_add_ifset(now_ms, s->res.wto);
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004650
Christopher Faulet0f226952018-10-22 09:29:56 +02004651 channel_auto_read(&s->req);
4652 channel_abort(&s->req);
4653 channel_auto_close(&s->req);
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004654 channel_htx_erase(&s->req, htxbuf(&s->req.buf));
Christopher Faulet72c7d8d2020-01-27 15:32:25 +01004655 channel_auto_read(&s->res);
4656 channel_auto_close(&s->res);
4657 channel_shutr_now(&s->res);
Christopher Faulet0f226952018-10-22 09:29:56 +02004658}
4659
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004660struct http_reply *http_error_message(struct stream *s)
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004661{
4662 const int msgnum = http_get_status_idx(s->txn->status);
4663
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004664 if (s->txn->http_reply)
4665 return s->txn->http_reply;
4666 else if (s->be->replies[msgnum])
4667 return s->be->replies[msgnum];
4668 else if (strm_fe(s)->replies[msgnum])
4669 return strm_fe(s)->replies[msgnum];
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004670 else
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004671 return &http_err_replies[msgnum];
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004672}
4673
Christopher Faulet40e6b552020-06-25 16:04:50 +02004674/* Produces an HTX message from an http reply. Depending on the http reply type,
4675 * a, errorfile, an raw file or a log-format string is used. On success, it
4676 * returns 0. If an error occurs -1 is returned. If it fails, this function only
4677 * exits. It is the caller responsibility to do the cleanup.
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004678 */
Christopher Fauletae43b6c2020-05-27 15:24:22 +02004679int http_reply_to_htx(struct stream *s, struct htx *htx, struct http_reply *reply)
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004680{
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004681 struct buffer *errmsg;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004682 struct htx_sl *sl;
4683 struct buffer *body = NULL;
4684 const char *status, *reason, *clen, *ctype;
4685 unsigned int slflags;
4686 int ret = 0;
4687
Christopher Faulete29a97e2020-05-14 14:49:25 +02004688 /*
4689 * - HTTP_REPLY_ERRFILES unexpected here. handled as no payload if so
4690 *
4691 * - HTTP_REPLY_INDIRECT: switch on another reply if defined or handled
4692 * as no payload if NULL. the TXN status code is set with the status
4693 * of the original reply.
4694 */
4695
4696 if (reply->type == HTTP_REPLY_INDIRECT) {
4697 if (reply->body.reply)
4698 reply = reply->body.reply;
4699 }
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004700 if (reply->type == HTTP_REPLY_ERRMSG && !reply->body.errmsg) {
4701 /* get default error message */
4702 if (reply == s->txn->http_reply)
4703 s->txn->http_reply = NULL;
4704 reply = http_error_message(s);
4705 if (reply->type == HTTP_REPLY_INDIRECT) {
4706 if (reply->body.reply)
4707 reply = reply->body.reply;
4708 }
4709 }
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004710
4711 if (reply->type == HTTP_REPLY_ERRMSG) {
4712 /* implicit or explicit error message*/
4713 errmsg = reply->body.errmsg;
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004714 if (errmsg && !b_is_null(errmsg)) {
Christopher Faulet20567362020-05-15 14:52:49 +02004715 if (!htx_copy_msg(htx, errmsg))
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004716 goto fail;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004717 }
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004718 }
4719 else {
4720 /* no payload, file or log-format string */
4721 if (reply->type == HTTP_REPLY_RAW) {
4722 /* file */
4723 body = &reply->body.obj;
4724 }
4725 else if (reply->type == HTTP_REPLY_LOGFMT) {
4726 /* log-format string */
4727 body = alloc_trash_chunk();
4728 if (!body)
4729 goto fail_alloc;
4730 body->data = build_logline(s, body->area, body->size, &reply->body.fmt);
4731 }
4732 /* else no payload */
4733
4734 status = ultoa(reply->status);
4735 reason = http_get_reason(reply->status);
4736 slflags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_CLEN);
4737 if (!body || !b_data(body))
4738 slflags |= HTX_SL_F_BODYLESS;
4739 sl = htx_add_stline(htx, HTX_BLK_RES_SL, slflags, ist("HTTP/1.1"), ist(status), ist(reason));
4740 if (!sl)
4741 goto fail;
4742 sl->info.res.status = reply->status;
4743
4744 clen = (body ? ultoa(b_data(body)) : "0");
4745 ctype = reply->ctype;
4746
4747 if (!LIST_ISEMPTY(&reply->hdrs)) {
4748 struct http_reply_hdr *hdr;
4749 struct buffer *value = alloc_trash_chunk();
4750
4751 if (!value)
4752 goto fail;
4753
4754 list_for_each_entry(hdr, &reply->hdrs, list) {
4755 chunk_reset(value);
4756 value->data = build_logline(s, value->area, value->size, &hdr->value);
4757 if (b_data(value) && !htx_add_header(htx, hdr->name, ist2(b_head(value), b_data(value)))) {
4758 free_trash_chunk(value);
4759 goto fail;
4760 }
4761 chunk_reset(value);
4762 }
4763 free_trash_chunk(value);
4764 }
4765
4766 if (!htx_add_header(htx, ist("content-length"), ist(clen)) ||
4767 (body && b_data(body) && ctype && !htx_add_header(htx, ist("content-type"), ist(ctype))) ||
4768 !htx_add_endof(htx, HTX_BLK_EOH) ||
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004769 (body && b_data(body) && !htx_add_data_atonce(htx, ist2(b_head(body), b_data(body)))))
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004770 goto fail;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004771
4772 htx->flags |= HTX_FL_EOM;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004773 }
4774
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004775 leave:
4776 if (reply->type == HTTP_REPLY_LOGFMT)
4777 free_trash_chunk(body);
4778 return ret;
4779
4780 fail_alloc:
4781 if (!(s->flags & SF_ERR_MASK))
4782 s->flags |= SF_ERR_RESOURCE;
Christopher Faulet97e466c2020-05-15 15:12:47 +02004783 /* fall through */
4784 fail:
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004785 ret = -1;
4786 goto leave;
Christopher Faulet97e466c2020-05-15 15:12:47 +02004787}
4788
4789/* Send an http reply to the client. On success, it returns 0. If an error
Christopher Faulet40e6b552020-06-25 16:04:50 +02004790 * occurs -1 is returned and the response channel is truncated, removing this
4791 * way the faulty reply. This function may fail when the reply is formatted
4792 * (http_reply_to_htx) or when the reply is forwarded
4793 * (http_forward_proxy_resp). On the last case, it is because a
4794 * http-after-response rule fails.
Christopher Faulet97e466c2020-05-15 15:12:47 +02004795 */
4796int http_reply_message(struct stream *s, struct http_reply *reply)
4797{
4798 struct channel *res = &s->res;
4799 struct htx *htx = htx_from_buf(&res->buf);
4800
4801 if (s->txn->status == -1)
4802 s->txn->status = reply->status;
4803 channel_htx_truncate(res, htx);
4804
4805 if (http_reply_to_htx(s, htx, reply) == -1)
4806 goto fail;
4807
4808 htx_to_buf(htx, &s->res.buf);
4809 if (!http_forward_proxy_resp(s, 1))
4810 goto fail;
4811 return 0;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004812
4813 fail:
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004814 channel_htx_truncate(res, htx);
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004815 if (!(s->flags & SF_ERR_MASK))
4816 s->flags |= SF_ERR_PRXCOND;
Christopher Faulet97e466c2020-05-15 15:12:47 +02004817 return -1;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004818}
4819
Christopher Faulet304cc402019-07-15 15:46:28 +02004820/* Return the error message corresponding to si->err_type. It is assumed
4821 * that the server side is closed. Note that err_type is actually a
4822 * bitmask, where almost only aborts may be cumulated with other
4823 * values. We consider that aborted operations are more important
4824 * than timeouts or errors due to the fact that nobody else in the
4825 * logs might explain incomplete retries. All others should avoid
4826 * being cumulated. It should normally not be possible to have multiple
4827 * aborts at once, but just in case, the first one in sequence is reported.
4828 * Note that connection errors appearing on the second request of a keep-alive
4829 * connection are not reported since this allows the client to retry.
4830 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004831void http_return_srv_error(struct stream *s, struct stream_interface *si)
Christopher Faulet304cc402019-07-15 15:46:28 +02004832{
4833 int err_type = si->err_type;
4834
4835 /* set s->txn->status for http_error_message(s) */
4836 s->txn->status = 503;
4837
4838 if (err_type & SI_ET_QUEUE_ABRT)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004839 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_Q,
4840 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004841 else if (err_type & SI_ET_CONN_ABRT)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004842 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_C,
4843 (s->txn->flags & TX_NOT_FIRST) ? NULL :
4844 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004845 else if (err_type & SI_ET_QUEUE_TO)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004846 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_Q,
4847 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004848 else if (err_type & SI_ET_QUEUE_ERR)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004849 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_Q,
4850 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004851 else if (err_type & SI_ET_CONN_TO)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004852 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_C,
4853 (s->txn->flags & TX_NOT_FIRST) ? NULL :
4854 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004855 else if (err_type & SI_ET_CONN_ERR)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004856 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_C,
4857 (s->flags & SF_SRV_REUSED) ? NULL :
4858 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004859 else if (err_type & SI_ET_CONN_RES)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004860 http_server_error(s, si, SF_ERR_RESOURCE, SF_FINST_C,
4861 (s->txn->flags & TX_NOT_FIRST) ? NULL :
4862 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004863 else { /* SI_ET_CONN_OTHER and others */
4864 s->txn->status = 500;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004865 http_server_error(s, si, SF_ERR_INTERNAL, SF_FINST_C,
4866 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004867 }
4868}
4869
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004870
Christopher Faulet4a28a532019-03-01 11:19:40 +01004871/* Handle Expect: 100-continue for HTTP/1.1 messages if necessary. It returns 0
4872 * on success and -1 on error.
4873 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004874static int http_handle_expect_hdr(struct stream *s, struct htx *htx, struct http_msg *msg)
Christopher Faulet4a28a532019-03-01 11:19:40 +01004875{
4876 /* If we have HTTP/1.1 message with a body and Expect: 100-continue,
4877 * then we must send an HTTP/1.1 100 Continue intermediate response.
4878 */
4879 if (msg->msg_state == HTTP_MSG_BODY && (msg->flags & HTTP_MSGF_VER_11) &&
4880 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
4881 struct ist hdr = { .ptr = "Expect", .len = 6 };
4882 struct http_hdr_ctx ctx;
4883
4884 ctx.blk = NULL;
4885 /* Expect is allowed in 1.1, look for it */
4886 if (http_find_header(htx, hdr, &ctx, 0) &&
4887 unlikely(isteqi(ctx.value, ist2("100-continue", 12)))) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004888 if (http_reply_100_continue(s) == -1)
Christopher Faulet4a28a532019-03-01 11:19:40 +01004889 return -1;
4890 http_remove_header(htx, &ctx);
4891 }
4892 }
4893 return 0;
4894}
4895
Christopher Faulet23a3c792018-11-28 10:01:23 +01004896/* Send a 100-Continue response to the client. It returns 0 on success and -1
4897 * on error. The response channel is updated accordingly.
4898 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004899static int http_reply_100_continue(struct stream *s)
Christopher Faulet23a3c792018-11-28 10:01:23 +01004900{
4901 struct channel *res = &s->res;
4902 struct htx *htx = htx_from_buf(&res->buf);
4903 struct htx_sl *sl;
4904 unsigned int flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|
4905 HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
Christopher Faulet23a3c792018-11-28 10:01:23 +01004906
4907 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
4908 ist("HTTP/1.1"), ist("100"), ist("Continue"));
4909 if (!sl)
4910 goto fail;
4911 sl->info.res.status = 100;
4912
Christopher Faulet1d5ec092019-06-26 14:23:54 +02004913 if (!htx_add_endof(htx, HTX_BLK_EOH))
Christopher Faulet23a3c792018-11-28 10:01:23 +01004914 goto fail;
4915
Christopher Fauleta72a7e42020-01-28 09:28:11 +01004916 if (!http_forward_proxy_resp(s, 0))
4917 goto fail;
Christopher Faulet23a3c792018-11-28 10:01:23 +01004918 return 0;
4919
4920 fail:
4921 /* If an error occurred, remove the incomplete HTTP response from the
4922 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004923 channel_htx_truncate(res, htx);
Christopher Faulet23a3c792018-11-28 10:01:23 +01004924 return -1;
4925}
4926
Christopher Faulet12c51e22018-11-28 15:59:42 +01004927
Christopher Faulet0f226952018-10-22 09:29:56 +02004928/*
4929 * Capture headers from message <htx> according to header list <cap_hdr>, and
4930 * fill the <cap> pointers appropriately.
4931 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004932static void http_capture_headers(struct htx *htx, char **cap, struct cap_hdr *cap_hdr)
Christopher Faulet0f226952018-10-22 09:29:56 +02004933{
4934 struct cap_hdr *h;
4935 int32_t pos;
4936
Christopher Fauleta3f15502019-05-13 15:27:23 +02004937 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet0f226952018-10-22 09:29:56 +02004938 struct htx_blk *blk = htx_get_blk(htx, pos);
4939 enum htx_blk_type type = htx_get_blk_type(blk);
4940 struct ist n, v;
4941
4942 if (type == HTX_BLK_EOH)
4943 break;
4944 if (type != HTX_BLK_HDR)
4945 continue;
4946
4947 n = htx_get_blk_name(htx, blk);
4948
4949 for (h = cap_hdr; h; h = h->next) {
4950 if (h->namelen && (h->namelen == n.len) &&
4951 (strncasecmp(n.ptr, h->name, h->namelen) == 0)) {
4952 if (cap[h->index] == NULL)
4953 cap[h->index] =
4954 pool_alloc(h->pool);
4955
4956 if (cap[h->index] == NULL) {
4957 ha_alert("HTTP capture : out of memory.\n");
4958 break;
4959 }
4960
4961 v = htx_get_blk_value(htx, blk);
4962 if (v.len > h->len)
4963 v.len = h->len;
4964
4965 memcpy(cap[h->index], v.ptr, v.len);
4966 cap[h->index][v.len]=0;
4967 }
4968 }
4969 }
4970}
4971
Christopher Faulet0b6bdc52018-10-24 11:05:36 +02004972/* Delete a value in a header between delimiters <from> and <next>. The header
4973 * itself is delimited by <start> and <end> pointers. The number of characters
4974 * displaced is returned, and the pointer to the first delimiter is updated if
4975 * required. The function tries as much as possible to respect the following
4976 * principles :
4977 * - replace <from> delimiter by the <next> one unless <from> points to <start>,
4978 * in which case <next> is simply removed
4979 * - set exactly one space character after the new first delimiter, unless there
4980 * are not enough characters in the block being moved to do so.
4981 * - remove unneeded spaces before the previous delimiter and after the new
4982 * one.
4983 *
4984 * It is the caller's responsibility to ensure that :
4985 * - <from> points to a valid delimiter or <start> ;
4986 * - <next> points to a valid delimiter or <end> ;
4987 * - there are non-space chars before <from>.
4988 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004989static int http_del_hdr_value(char *start, char *end, char **from, char *next)
Christopher Faulet0b6bdc52018-10-24 11:05:36 +02004990{
4991 char *prev = *from;
4992
4993 if (prev == start) {
4994 /* We're removing the first value. eat the semicolon, if <next>
4995 * is lower than <end> */
4996 if (next < end)
4997 next++;
4998
4999 while (next < end && HTTP_IS_SPHT(*next))
5000 next++;
5001 }
5002 else {
5003 /* Remove useless spaces before the old delimiter. */
5004 while (HTTP_IS_SPHT(*(prev-1)))
5005 prev--;
5006 *from = prev;
5007
5008 /* copy the delimiter and if possible a space if we're
5009 * not at the end of the line.
5010 */
5011 if (next < end) {
5012 *prev++ = *next++;
5013 if (prev + 1 < next)
5014 *prev++ = ' ';
5015 while (next < end && HTTP_IS_SPHT(*next))
5016 next++;
5017 }
5018 }
5019 memmove(prev, next, end - next);
5020 return (prev - next);
5021}
5022
Christopher Faulet0f226952018-10-22 09:29:56 +02005023
5024/* Formats the start line of the request (without CRLF) and puts it in <str> and
Joseph Herlantc42c0e92018-11-25 10:43:27 -08005025 * return the written length. The line can be truncated if it exceeds <len>.
Christopher Faulet0f226952018-10-22 09:29:56 +02005026 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005027static size_t http_fmt_req_line(const struct htx_sl *sl, char *str, size_t len)
Christopher Faulet0f226952018-10-22 09:29:56 +02005028{
5029 struct ist dst = ist2(str, 0);
5030
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005031 if (istcat(&dst, htx_sl_req_meth(sl), len) == -1)
Christopher Faulet0f226952018-10-22 09:29:56 +02005032 goto end;
5033 if (dst.len + 1 > len)
5034 goto end;
5035 dst.ptr[dst.len++] = ' ';
5036
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005037 if (istcat(&dst, htx_sl_req_uri(sl), len) == -1)
Christopher Faulet0f226952018-10-22 09:29:56 +02005038 goto end;
5039 if (dst.len + 1 > len)
5040 goto end;
5041 dst.ptr[dst.len++] = ' ';
5042
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005043 istcat(&dst, htx_sl_req_vsn(sl), len);
Christopher Faulet0f226952018-10-22 09:29:56 +02005044 end:
5045 return dst.len;
5046}
5047
5048/*
5049 * Print a debug line with a start line.
5050 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005051static void http_debug_stline(const char *dir, struct stream *s, const struct htx_sl *sl)
Christopher Faulet0f226952018-10-22 09:29:56 +02005052{
5053 struct session *sess = strm_sess(s);
5054 int max;
5055
5056 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
5057 dir,
5058 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->handle.fd : -1,
5059 objt_cs(s->si[1].end) ? (unsigned short)objt_cs(s->si[1].end)->conn->handle.fd : -1);
5060
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005061 max = HTX_SL_P1_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02005062 UBOUND(max, trash.size - trash.data - 3);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005063 chunk_memcat(&trash, HTX_SL_P1_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02005064 trash.area[trash.data++] = ' ';
5065
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005066 max = HTX_SL_P2_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02005067 UBOUND(max, trash.size - trash.data - 2);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005068 chunk_memcat(&trash, HTX_SL_P2_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02005069 trash.area[trash.data++] = ' ';
5070
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005071 max = HTX_SL_P3_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02005072 UBOUND(max, trash.size - trash.data - 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005073 chunk_memcat(&trash, HTX_SL_P3_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02005074 trash.area[trash.data++] = '\n';
5075
Willy Tarreau2e8ab6b2020-03-14 11:03:20 +01005076 DISGUISE(write(1, trash.area, trash.data));
Christopher Faulet0f226952018-10-22 09:29:56 +02005077}
5078
5079/*
5080 * Print a debug line with a header.
5081 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005082static 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 +02005083{
5084 struct session *sess = strm_sess(s);
5085 int max;
5086
5087 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
5088 dir,
5089 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->handle.fd : -1,
5090 objt_cs(s->si[1].end) ? (unsigned short)objt_cs(s->si[1].end)->conn->handle.fd : -1);
5091
5092 max = n.len;
5093 UBOUND(max, trash.size - trash.data - 3);
5094 chunk_memcat(&trash, n.ptr, max);
5095 trash.area[trash.data++] = ':';
5096 trash.area[trash.data++] = ' ';
5097
5098 max = v.len;
5099 UBOUND(max, trash.size - trash.data - 1);
5100 chunk_memcat(&trash, v.ptr, max);
5101 trash.area[trash.data++] = '\n';
5102
Willy Tarreau2e8ab6b2020-03-14 11:03:20 +01005103 DISGUISE(write(1, trash.area, trash.data));
Christopher Faulet0f226952018-10-22 09:29:56 +02005104}
5105
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005106/* Allocate a new HTTP transaction for stream <s> unless there is one already.
5107 * In case of allocation failure, everything allocated is freed and NULL is
5108 * returned. Otherwise the new transaction is assigned to the stream and
5109 * returned.
5110 */
5111struct http_txn *http_alloc_txn(struct stream *s)
5112{
5113 struct http_txn *txn = s->txn;
5114
5115 if (txn)
5116 return txn;
5117
5118 txn = pool_alloc(pool_head_http_txn);
5119 if (!txn)
5120 return txn;
5121
5122 s->txn = txn;
5123 return txn;
5124}
5125
5126void http_txn_reset_req(struct http_txn *txn)
5127{
Christopher Faulet1aea50e2020-01-17 16:03:53 +01005128 txn->req.flags = 0;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005129 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
5130}
5131
5132void http_txn_reset_res(struct http_txn *txn)
5133{
Christopher Faulet1aea50e2020-01-17 16:03:53 +01005134 txn->rsp.flags = 0;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005135 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
5136}
5137
5138/*
Christopher Faulet75f619a2021-03-08 19:12:58 +01005139 * Create and initialize a new HTTP transaction for stream <s>. This should be
5140 * used before processing any new request. It returns the transaction or NLULL
5141 * on error.
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005142 */
Christopher Faulet75f619a2021-03-08 19:12:58 +01005143struct http_txn *http_create_txn(struct stream *s)
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005144{
Christopher Faulet75f619a2021-03-08 19:12:58 +01005145 struct http_txn *txn;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005146 struct conn_stream *cs = objt_cs(s->si[0].end);
5147
Christopher Faulet75f619a2021-03-08 19:12:58 +01005148 txn = pool_alloc(pool_head_http_txn);
5149 if (!txn)
5150 return NULL;
5151 s->txn = txn;
5152
Christopher Fauletda831fa2020-10-06 17:58:43 +02005153 txn->flags = ((cs && cs->flags & CS_FL_NOT_FIRST) ? TX_NOT_FIRST : 0);
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005154 txn->status = -1;
Christopher Faulet5cb513a2020-05-13 17:56:56 +02005155 txn->http_reply = NULL;
Willy Tarreau8b507582020-02-25 09:35:07 +01005156 write_u32(txn->cache_hash, 0);
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005157
5158 txn->cookie_first_date = 0;
5159 txn->cookie_last_date = 0;
5160
5161 txn->srv_cookie = NULL;
5162 txn->cli_cookie = NULL;
5163 txn->uri = NULL;
5164
5165 http_txn_reset_req(txn);
5166 http_txn_reset_res(txn);
5167
5168 txn->req.chn = &s->req;
5169 txn->rsp.chn = &s->res;
5170
5171 txn->auth.method = HTTP_AUTH_UNKNOWN;
5172
5173 vars_init(&s->vars_txn, SCOPE_TXN);
5174 vars_init(&s->vars_reqres, SCOPE_REQ);
Christopher Faulet75f619a2021-03-08 19:12:58 +01005175
5176 return txn;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005177}
5178
5179/* to be used at the end of a transaction */
Christopher Faulet75f619a2021-03-08 19:12:58 +01005180void http_destroy_txn(struct stream *s)
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005181{
5182 struct http_txn *txn = s->txn;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005183
5184 /* these ones will have been dynamically allocated */
5185 pool_free(pool_head_requri, txn->uri);
5186 pool_free(pool_head_capture, txn->cli_cookie);
5187 pool_free(pool_head_capture, txn->srv_cookie);
Tim Duesterhusa17e6622020-03-05 20:19:02 +01005188 pool_free(pool_head_uniqueid, s->unique_id.ptr);
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005189
Tim Duesterhusa17e6622020-03-05 20:19:02 +01005190 s->unique_id = IST_NULL;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005191 txn->uri = NULL;
5192 txn->srv_cookie = NULL;
5193 txn->cli_cookie = NULL;
5194
Christopher Faulet59399252019-11-07 14:27:52 +01005195 if (!LIST_ISEMPTY(&s->vars_txn.head))
5196 vars_prune(&s->vars_txn, s->sess, s);
5197 if (!LIST_ISEMPTY(&s->vars_reqres.head))
5198 vars_prune(&s->vars_reqres, s->sess, s);
Christopher Faulet75f619a2021-03-08 19:12:58 +01005199
5200 pool_free(pool_head_http_txn, txn);
5201 s->txn = NULL;
Christopher Faulet59399252019-11-07 14:27:52 +01005202}
5203
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005204
5205DECLARE_POOL(pool_head_http_txn, "http_txn", sizeof(struct http_txn));
Christopher Faulet0f226952018-10-22 09:29:56 +02005206
Christopher Fauletf4eb75d2018-10-11 15:55:07 +02005207__attribute__((constructor))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005208static void __http_protocol_init(void)
Christopher Fauletf4eb75d2018-10-11 15:55:07 +02005209{
5210}
5211
5212
5213/*
5214 * Local variables:
5215 * c-indent-level: 8
5216 * c-basic-offset: 8
5217 * End:
5218 */