blob: 469b9c095c9733601d629508b2cc3bf441c0f19b [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>
Willy Tarreauf1d32c42020-06-04 21:07:02 +020019#include <haproxy/channel.h>
Willy Tarreau4aa573d2020-06-04 18:21:56 +020020#include <haproxy/check.h>
Willy Tarreau7ea393d2020-06-04 18:02:10 +020021#include <haproxy/connection.h>
Willy Tarreau36979d92020-06-05 17:27:29 +020022#include <haproxy/errors.h>
Willy Tarreauc7babd82020-06-04 21:29:29 +020023#include <haproxy/filters.h>
Willy Tarreaucd72d8c2020-06-02 19:11:26 +020024#include <haproxy/http.h>
Willy Tarreauc2b1ff02020-06-04 21:21:03 +020025#include <haproxy/http_ana.h>
Willy Tarreau87735332020-06-04 09:08:41 +020026#include <haproxy/http_htx.h>
Willy Tarreau16f958c2020-06-03 08:44:35 +020027#include <haproxy/htx.h>
Willy Tarreauaeed4a82020-06-04 22:01:04 +020028#include <haproxy/log.h>
Willy Tarreau6131d6a2020-06-02 16:48:09 +020029#include <haproxy/net_helper.h>
Willy Tarreaua264d962020-06-04 22:29:18 +020030#include <haproxy/proxy.h>
Willy Tarreau7cd8b6e2020-06-02 17:32:26 +020031#include <haproxy/regex.h>
Willy Tarreau1e56f922020-06-04 23:20:13 +020032#include <haproxy/server-t.h>
Willy Tarreau2eec9b52020-06-04 19:58:55 +020033#include <haproxy/stats.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020034#include <haproxy/stream.h>
Willy Tarreau5e539c92020-06-04 20:45:39 +020035#include <haproxy/stream_interface.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020036#include <haproxy/trace.h>
Willy Tarreau8c42b8a2020-06-04 19:27:34 +020037#include <haproxy/uri_auth-t.h>
Willy Tarreaua1718922020-06-04 16:25:31 +020038#include <haproxy/vars.h>
Christopher Faulete0768eb2018-10-03 16:38:02 +020039
Christopher Faulete0768eb2018-10-03 16:38:02 +020040
Christopher Fauleteea8fc72019-11-05 16:18:10 +010041#define TRACE_SOURCE &trace_strm
42
Christopher Faulet377c5a52018-10-24 21:21:30 +020043extern const char *stat_status_codes[];
Christopher Fauletf2824e62018-10-01 12:12:37 +020044
Christopher Fauleta8a46e22019-07-16 14:53:09 +020045struct pool_head *pool_head_requri = NULL;
46struct pool_head *pool_head_capture = NULL;
47
48
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020049static void http_end_request(struct stream *s);
50static void http_end_response(struct stream *s);
Christopher Fauletf2824e62018-10-01 12:12:37 +020051
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020052static void http_capture_headers(struct htx *htx, char **cap, struct cap_hdr *cap_hdr);
53static int http_del_hdr_value(char *start, char *end, char **from, char *next);
54static size_t http_fmt_req_line(const struct htx_sl *sl, char *str, size_t len);
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020055static void http_debug_stline(const char *dir, struct stream *s, const struct htx_sl *sl);
56static 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 +020057
Christopher Fauletb58f62b2020-01-13 16:40:13 +010058static enum rule_result http_req_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s);
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020059static enum rule_result http_res_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s);
Christopher Faulet3e964192018-10-24 11:39:23 +020060
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020061static void http_manage_client_side_cookies(struct stream *s, struct channel *req);
62static void http_manage_server_side_cookies(struct stream *s, struct channel *res);
Christopher Fauletfcda7c62018-10-24 11:56:22 +020063
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020064static int http_stats_check_uri(struct stream *s, struct http_txn *txn, struct proxy *backend);
65static int http_handle_stats(struct stream *s, struct channel *req);
Christopher Faulet377c5a52018-10-24 21:21:30 +020066
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020067static int http_handle_expect_hdr(struct stream *s, struct htx *htx, struct http_msg *msg);
68static int http_reply_100_continue(struct stream *s);
Christopher Faulet23a3c792018-11-28 10:01:23 +010069
Christopher Faulete0768eb2018-10-03 16:38:02 +020070/* This stream analyser waits for a complete HTTP request. It returns 1 if the
71 * processing can continue on next analysers, or zero if it either needs more
72 * data or wants to immediately abort the request (eg: timeout, error, ...). It
73 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req.analysers
74 * when it has nothing left to do, and may remove any analyser when it wants to
75 * abort.
76 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020077int http_wait_for_request(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +020078{
Christopher Faulet9768c262018-10-22 09:34:31 +020079
Christopher Faulete0768eb2018-10-03 16:38:02 +020080 /*
Christopher Faulet9768c262018-10-22 09:34:31 +020081 * We will analyze a complete HTTP request to check the its syntax.
Christopher Faulete0768eb2018-10-03 16:38:02 +020082 *
Christopher Faulet9768c262018-10-22 09:34:31 +020083 * Once the start line and all headers are received, we may perform a
84 * capture of the error (if any), and we will set a few fields. We also
85 * check for monitor-uri, logging and finally headers capture.
Christopher Faulete0768eb2018-10-03 16:38:02 +020086 */
Christopher Faulete0768eb2018-10-03 16:38:02 +020087 struct session *sess = s->sess;
88 struct http_txn *txn = s->txn;
89 struct http_msg *msg = &txn->req;
Christopher Faulet9768c262018-10-22 09:34:31 +020090 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +010091 struct htx_sl *sl;
Christopher Faulete0768eb2018-10-03 16:38:02 +020092
Christopher Fauleteea8fc72019-11-05 16:18:10 +010093 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +020094
Christopher Fauletda46a0d2021-01-21 17:32:58 +010095 if (unlikely(!IS_HTX_STRM(s))) {
96 /* It is only possible when a TCP stream is upgrade to HTTP.
97 * There is a transition period during which there is no
98 * data. The stream is still in raw mode and SF_IGNORE flag is
99 * still set. When this happens, the new mux is responsible to
100 * handle all errors. Thus we may leave immediatly.
101 */
102 BUG_ON(!(s->flags & SF_IGNORE) || !c_empty(&s->req));
Christopher Faulet9768c262018-10-22 09:34:31 +0200103
Christopher Fauletda46a0d2021-01-21 17:32:58 +0100104 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA, s);
105 return 0;
106 }
107
108 htx = htxbuf(&req->buf);
Christopher Faulet8bebd2f2020-10-06 17:54:56 +0200109
Willy Tarreau4236f032019-03-05 10:43:32 +0100110 /* Parsing errors are caught here */
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200111 if (htx->flags & (HTX_FL_PARSING_ERROR|HTX_FL_PROCESSING_ERROR)) {
Willy Tarreau4236f032019-03-05 10:43:32 +0100112 stream_inc_http_req_ctr(s);
113 stream_inc_http_err_ctr(s);
Emeric Brun28976442020-10-07 08:50:09 +0200114 proxy_inc_fe_req_ctr(sess->listener, sess->fe);
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200115 if (htx->flags & HTX_FL_PARSING_ERROR)
116 goto return_bad_req;
117 else
118 goto return_int_err;
Willy Tarreau4236f032019-03-05 10:43:32 +0100119 }
120
Christopher Faulete0768eb2018-10-03 16:38:02 +0200121 /* we're speaking HTTP here, so let's speak HTTP to the client */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200122 s->srv_error = http_return_srv_error;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200123
Christopher Faulet9768c262018-10-22 09:34:31 +0200124 msg->msg_state = HTTP_MSG_BODY;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200125 stream_inc_http_req_ctr(s);
Emeric Brun28976442020-10-07 08:50:09 +0200126 proxy_inc_fe_req_ctr(sess->listener, sess->fe); /* one more valid request for this FE */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200127
Christopher Faulet9768c262018-10-22 09:34:31 +0200128 /* kill the pending keep-alive timeout */
Christopher Faulet9768c262018-10-22 09:34:31 +0200129 req->analyse_exp = TICK_ETERNITY;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200130
Christopher Faulet29f17582019-05-23 11:03:26 +0200131 BUG_ON(htx_get_first_type(htx) != HTX_BLK_REQ_SL);
Christopher Faulet297fbb42019-05-13 14:41:27 +0200132 sl = http_get_stline(htx);
Christopher Faulet03599112018-11-27 11:21:21 +0100133
Christopher Faulet9768c262018-10-22 09:34:31 +0200134 /* 0: we might have to print this header in debug mode */
135 if (unlikely((global.mode & MODE_DEBUG) &&
136 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
137 int32_t pos;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200138
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200139 http_debug_stline("clireq", s, sl);
Christopher Faulet9768c262018-10-22 09:34:31 +0200140
Christopher Fauleta3f15502019-05-13 15:27:23 +0200141 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet9768c262018-10-22 09:34:31 +0200142 struct htx_blk *blk = htx_get_blk(htx, pos);
143 enum htx_blk_type type = htx_get_blk_type(blk);
144
145 if (type == HTX_BLK_EOH)
146 break;
147 if (type != HTX_BLK_HDR)
148 continue;
149
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200150 http_debug_hdr("clihdr", s,
151 htx_get_blk_name(htx, blk),
152 htx_get_blk_value(htx, blk));
Christopher Faulet9768c262018-10-22 09:34:31 +0200153 }
154 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200155
156 /*
Christopher Faulet03599112018-11-27 11:21:21 +0100157 * 1: identify the method and the version. Also set HTTP flags
Christopher Faulete0768eb2018-10-03 16:38:02 +0200158 */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100159 txn->meth = sl->info.req.meth;
Christopher Faulet03599112018-11-27 11:21:21 +0100160 if (sl->flags & HTX_SL_F_VER_11)
Christopher Faulet9768c262018-10-22 09:34:31 +0200161 msg->flags |= HTTP_MSGF_VER_11;
Christopher Faulet03599112018-11-27 11:21:21 +0100162 msg->flags |= HTTP_MSGF_XFER_LEN;
Christopher Faulet2a408542020-11-20 14:22:37 +0100163 if (sl->flags & HTX_SL_F_CLEN)
164 msg->flags |= HTTP_MSGF_CNT_LEN;
165 else if (sl->flags & HTX_SL_F_CHNK)
166 msg->flags |= HTTP_MSGF_TE_CHNK;
Christopher Fauletb2db4fa2018-11-27 16:51:09 +0100167 if (sl->flags & HTX_SL_F_BODYLESS)
168 msg->flags |= HTTP_MSGF_BODYLESS;
Christopher Faulet576c3582021-01-08 15:53:01 +0100169 if (sl->flags & HTX_SL_F_CONN_UPG)
170 msg->flags |= HTTP_MSGF_CONN_UPG;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200171
172 /* we can make use of server redirect on GET and HEAD */
173 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
174 s->flags |= SF_REDIRECTABLE;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100175 else if (txn->meth == HTTP_METH_OTHER && isteqi(htx_sl_req_meth(sl), ist("PRI"))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200176 /* PRI is reserved for the HTTP/2 preface */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200177 goto return_bad_req;
178 }
179
180 /*
Christopher Faulet6072beb2020-02-18 15:34:58 +0100181 * 2: check if the URI matches the monitor_uri. We have to do this for
182 * every request which gets in, because the monitor-uri is defined by
183 * the frontend. If the monitor-uri starts with a '/', the matching is
184 * done against the request's path. Otherwise, the request's uri is
185 * used. It is a workaround to let HTTP/2 health-checks work as
186 * expected.
Christopher Faulete0768eb2018-10-03 16:38:02 +0200187 */
188 if (unlikely((sess->fe->monitor_uri_len != 0) &&
Christopher Faulet6072beb2020-02-18 15:34:58 +0100189 ((*sess->fe->monitor_uri == '/' && isteq(http_get_path(htx_sl_req_uri(sl)),
190 ist2(sess->fe->monitor_uri, sess->fe->monitor_uri_len))) ||
191 isteq(htx_sl_req_uri(sl), ist2(sess->fe->monitor_uri, sess->fe->monitor_uri_len))))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200192 /*
193 * We have found the monitor URI
194 */
195 struct acl_cond *cond;
196
197 s->flags |= SF_MONITOR;
Olivier Houcharda798bf52019-03-08 18:52:00 +0100198 _HA_ATOMIC_ADD(&sess->fe->fe_counters.intercepted_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200199
200 /* Check if we want to fail this monitor request or not */
201 list_for_each_entry(cond, &sess->fe->mon_fail_cond, list) {
202 int ret = acl_exec_cond(cond, sess->fe, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
203
204 ret = acl_pass(ret);
205 if (cond->pol == ACL_COND_UNLESS)
206 ret = !ret;
207
208 if (ret) {
209 /* we fail this request, let's return 503 service unavail */
210 txn->status = 503;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200211 if (!(s->flags & SF_ERR_MASK))
212 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
213 goto return_prx_cond;
214 }
215 }
216
Joseph Herlantc42c0e92018-11-25 10:43:27 -0800217 /* nothing to fail, let's reply normally */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200218 txn->status = 200;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200219 if (!(s->flags & SF_ERR_MASK))
220 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
221 goto return_prx_cond;
222 }
223
224 /*
225 * 3: Maybe we have to copy the original REQURI for the logs ?
226 * Note: we cannot log anymore if the request has been
227 * classified as invalid.
228 */
229 if (unlikely(s->logs.logwait & LW_REQ)) {
230 /* we have a complete HTTP request that we must log */
231 if ((txn->uri = pool_alloc(pool_head_requri)) != NULL) {
Christopher Faulet9768c262018-10-22 09:34:31 +0200232 size_t len;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200233
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200234 len = http_fmt_req_line(sl, txn->uri, global.tune.requri_len - 1);
Christopher Faulet9768c262018-10-22 09:34:31 +0200235 txn->uri[len] = 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200236
237 if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT)))
238 s->do_log(s);
239 } else {
240 ha_alert("HTTP logging : out of memory.\n");
241 }
242 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200243
Christopher Faulete0768eb2018-10-03 16:38:02 +0200244 /* if the frontend has "option http-use-proxy-header", we'll check if
245 * we have what looks like a proxied connection instead of a connection,
246 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
247 * Note that this is *not* RFC-compliant, however browsers and proxies
248 * happen to do that despite being non-standard :-(
249 * We consider that a request not beginning with either '/' or '*' is
250 * a proxied connection, which covers both "scheme://location" and
251 * CONNECT ip:port.
252 */
253 if ((sess->fe->options2 & PR_O2_USE_PXHDR) &&
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100254 *HTX_SL_REQ_UPTR(sl) != '/' && *HTX_SL_REQ_UPTR(sl) != '*')
Christopher Faulete0768eb2018-10-03 16:38:02 +0200255 txn->flags |= TX_USE_PX_CONN;
256
Christopher Faulete0768eb2018-10-03 16:38:02 +0200257 /* 5: we may need to capture headers */
258 if (unlikely((s->logs.logwait & LW_REQHDR) && s->req_cap))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200259 http_capture_headers(htx, s->req_cap, sess->fe->req_cap);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200260
Christopher Faulete0768eb2018-10-03 16:38:02 +0200261 /* we may have to wait for the request's body */
Christopher Faulet9768c262018-10-22 09:34:31 +0200262 if (s->be->options & PR_O_WREQ_BODY)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200263 req->analysers |= AN_REQ_HTTP_BODY;
264
265 /*
266 * RFC7234#4:
267 * A cache MUST write through requests with methods
268 * that are unsafe (Section 4.2.1 of [RFC7231]) to
269 * the origin server; i.e., a cache is not allowed
270 * to generate a reply to such a request before
271 * having forwarded the request and having received
272 * a corresponding response.
273 *
274 * RFC7231#4.2.1:
275 * Of the request methods defined by this
276 * specification, the GET, HEAD, OPTIONS, and TRACE
277 * methods are defined to be safe.
278 */
279 if (likely(txn->meth == HTTP_METH_GET ||
280 txn->meth == HTTP_METH_HEAD ||
281 txn->meth == HTTP_METH_OPTIONS ||
282 txn->meth == HTTP_METH_TRACE))
283 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
284
285 /* end of job, return OK */
286 req->analysers &= ~an_bit;
287 req->analyse_exp = TICK_ETERNITY;
Christopher Faulet9768c262018-10-22 09:34:31 +0200288
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100289 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200290 return 1;
291
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200292 return_int_err:
293 txn->status = 500;
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200294 if (!(s->flags & SF_ERR_MASK))
295 s->flags |= SF_ERR_INTERNAL;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100296 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200297 if (sess->listener->counters)
Christopher Fauletb8a53712019-12-16 11:29:38 +0100298 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200299 goto return_prx_cond;
300
Christopher Faulete0768eb2018-10-03 16:38:02 +0200301 return_bad_req:
Christopher Faulet9768c262018-10-22 09:34:31 +0200302 txn->status = 400;
Olivier Houcharda798bf52019-03-08 18:52:00 +0100303 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200304 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100305 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200306 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200307
308 return_prx_cond:
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200309 http_reply_and_close(s, txn->status, http_error_message(s));
310
Christopher Faulete0768eb2018-10-03 16:38:02 +0200311 if (!(s->flags & SF_ERR_MASK))
312 s->flags |= SF_ERR_PRXCOND;
313 if (!(s->flags & SF_FINST_MASK))
314 s->flags |= SF_FINST_R;
315
316 req->analysers &= AN_REQ_FLT_END;
317 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100318 DBG_TRACE_DEVEL("leaving on error",
319 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200320 return 0;
321}
322
323
324/* This stream analyser runs all HTTP request processing which is common to
325 * frontends and backends, which means blocking ACLs, filters, connection-close,
326 * reqadd, stats and redirects. This is performed for the designated proxy.
327 * It returns 1 if the processing can continue on next analysers, or zero if it
328 * either needs more data or wants to immediately abort the request (eg: deny,
329 * error, ...).
330 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200331int http_process_req_common(struct stream *s, struct channel *req, int an_bit, struct proxy *px)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200332{
333 struct session *sess = s->sess;
334 struct http_txn *txn = s->txn;
335 struct http_msg *msg = &txn->req;
Christopher Fauletff2759f2018-10-24 11:13:16 +0200336 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200337 struct redirect_rule *rule;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200338 enum rule_result verdict;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200339 struct connection *conn = objt_conn(sess->origin);
340
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100341 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200342
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100343 htx = htxbuf(&req->buf);
Christopher Fauletff2759f2018-10-24 11:13:16 +0200344
Christopher Faulet1907ccc2019-04-29 13:12:02 +0200345 /* just in case we have some per-backend tracking. Only called the first
346 * execution of the analyser. */
347 if (!s->current_rule || s->current_rule_list != &px->http_req_rules)
348 stream_inc_be_http_req_ctr(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200349
350 /* evaluate http-request rules */
351 if (!LIST_ISEMPTY(&px->http_req_rules)) {
Christopher Fauletb58f62b2020-01-13 16:40:13 +0100352 verdict = http_req_get_intercept_rule(px, &px->http_req_rules, s);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200353
354 switch (verdict) {
355 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
356 goto return_prx_yield;
357
358 case HTTP_RULE_RES_CONT:
359 case HTTP_RULE_RES_STOP: /* nothing to do */
360 break;
361
362 case HTTP_RULE_RES_DENY: /* deny or tarpit */
363 if (txn->flags & TX_CLTARPIT)
364 goto tarpit;
365 goto deny;
366
367 case HTTP_RULE_RES_ABRT: /* abort request, response already sent. Eg: auth */
368 goto return_prx_cond;
369
370 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
371 goto done;
372
373 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
374 goto return_bad_req;
Christopher Faulet3a26bee2019-12-16 12:47:40 +0100375
376 case HTTP_RULE_RES_ERROR: /* failed with a bad request */
377 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200378 }
379 }
380
381 if (conn && (conn->flags & CO_FL_EARLY_DATA) &&
Olivier Houchard220a26c2020-01-23 14:57:36 +0100382 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_SSL_WAIT_HS))) {
Christopher Fauletff2759f2018-10-24 11:13:16 +0200383 struct http_hdr_ctx ctx;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200384
Christopher Fauletff2759f2018-10-24 11:13:16 +0200385 ctx.blk = NULL;
386 if (!http_find_header(htx, ist("Early-Data"), &ctx, 0)) {
387 if (unlikely(!http_add_header(htx, ist("Early-Data"), ist("1"))))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100388 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200389 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200390 }
391
392 /* OK at this stage, we know that the request was accepted according to
393 * the http-request rules, we can check for the stats. Note that the
394 * URI is detected *before* the req* rules in order not to be affected
395 * by a possible reqrep, while they are processed *after* so that a
396 * reqdeny can still block them. This clearly needs to change in 1.6!
397 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200398 if (!s->target && http_stats_check_uri(s, txn, px)) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200399 s->target = &http_stats_applet.obj_type;
Willy Tarreau14bfe9a2018-12-19 15:19:27 +0100400 if (unlikely(!si_register_handler(&s->si[1], objt_applet(s->target)))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200401 s->logs.tv_request = now;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200402 if (!(s->flags & SF_ERR_MASK))
403 s->flags |= SF_ERR_RESOURCE;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100404 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200405 }
406
407 /* parse the whole stats request and extract the relevant information */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200408 http_handle_stats(s, req);
Christopher Fauletb58f62b2020-01-13 16:40:13 +0100409 verdict = http_req_get_intercept_rule(px, &px->uri_auth->http_req_rules, s);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200410 /* not all actions implemented: deny, allow, auth */
411
412 if (verdict == HTTP_RULE_RES_DENY) /* stats http-request deny */
413 goto deny;
414
415 if (verdict == HTTP_RULE_RES_ABRT) /* stats auth / stats http-request auth */
416 goto return_prx_cond;
Christopher Faulet3a26bee2019-12-16 12:47:40 +0100417
418 if (verdict == HTTP_RULE_RES_BADREQ) /* failed with a bad request */
419 goto return_bad_req;
420
421 if (verdict == HTTP_RULE_RES_ERROR) /* failed with a bad request */
422 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200423 }
424
Christopher Faulet2571bc62019-03-01 11:44:26 +0100425 /* Proceed with the applets now. */
426 if (unlikely(objt_applet(s->target))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200427 if (sess->fe == s->be) /* report it if the request was intercepted by the frontend */
Olivier Houcharda798bf52019-03-08 18:52:00 +0100428 _HA_ATOMIC_ADD(&sess->fe->fe_counters.intercepted_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200429
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200430 if (http_handle_expect_hdr(s, htx, msg) == -1)
Christopher Fauletb8a53712019-12-16 11:29:38 +0100431 goto return_int_err;
Christopher Fauletbcf242a2019-03-01 11:36:26 +0100432
Christopher Faulete0768eb2018-10-03 16:38:02 +0200433 if (!(s->flags & SF_ERR_MASK)) // this is not really an error but it is
434 s->flags |= SF_ERR_LOCAL; // to mark that it comes from the proxy
435 if (!(s->flags & SF_FINST_MASK))
436 s->flags |= SF_FINST_R;
437
438 /* enable the minimally required analyzers to handle keep-alive and compression on the HTTP response */
439 req->analysers &= (AN_REQ_HTTP_BODY | AN_REQ_FLT_HTTP_HDRS | AN_REQ_FLT_END);
440 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
441 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Christopher Fauletbcf242a2019-03-01 11:36:26 +0100442
443 req->flags |= CF_SEND_DONTWAIT;
444 s->flags |= SF_ASSIGNED;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200445 goto done;
446 }
447
448 /* check whether we have some ACLs set to redirect this request */
449 list_for_each_entry(rule, &px->redirect_rules, list) {
450 if (rule->cond) {
451 int ret;
452
453 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
454 ret = acl_pass(ret);
455 if (rule->cond->pol == ACL_COND_UNLESS)
456 ret = !ret;
457 if (!ret)
458 continue;
459 }
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200460 if (!http_apply_redirect_rule(rule, s, txn))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100461 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200462 goto done;
463 }
464
465 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
466 * If this happens, then the data will not come immediately, so we must
467 * send all what we have without waiting. Note that due to the small gain
468 * in waiting for the body of the request, it's easier to simply put the
469 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
470 * itself once used.
471 */
472 req->flags |= CF_SEND_DONTWAIT;
473
474 done: /* done with this analyser, continue with next ones that the calling
475 * points will have set, if any.
476 */
477 req->analyse_exp = TICK_ETERNITY;
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +0500478 done_without_exp: /* done with this analyser, but don't reset the analyse_exp. */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200479 req->analysers &= ~an_bit;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100480 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200481 return 1;
482
483 tarpit:
484 /* Allow cookie logging
485 */
486 if (s->be->cookie_name || sess->fe->capture_name)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200487 http_manage_client_side_cookies(s, req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200488
489 /* When a connection is tarpitted, we use the tarpit timeout,
490 * which may be the same as the connect timeout if unspecified.
491 * If unset, then set it to zero because we really want it to
492 * eventually expire. We build the tarpit as an analyser.
493 */
Christopher Faulet202c6ce2019-01-07 14:57:35 +0100494 channel_htx_erase(&s->req, htx);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200495
496 /* wipe the request out so that we can drop the connection early
497 * if the client closes first.
498 */
499 channel_dont_connect(req);
500
Christopher Faulete0768eb2018-10-03 16:38:02 +0200501 req->analysers &= AN_REQ_FLT_END; /* remove switching rules etc... */
502 req->analysers |= AN_REQ_HTTP_TARPIT;
503 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
504 if (!req->analyse_exp)
505 req->analyse_exp = tick_add(now_ms, 0);
506 stream_inc_http_err_ctr(s);
Olivier Houcharda798bf52019-03-08 18:52:00 +0100507 _HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100508 if (s->flags & SF_BE_ASSIGNED)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100509 _HA_ATOMIC_ADD(&s->be->be_counters.denied_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200510 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100511 _HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200512 goto done_without_exp;
513
514 deny: /* this request was blocked (denied) */
515
516 /* Allow cookie logging
517 */
518 if (s->be->cookie_name || sess->fe->capture_name)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200519 http_manage_client_side_cookies(s, req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200520
Christopher Faulete0768eb2018-10-03 16:38:02 +0200521 s->logs.tv_request = now;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200522 stream_inc_http_err_ctr(s);
Olivier Houcharda798bf52019-03-08 18:52:00 +0100523 _HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100524 if (s->flags & SF_BE_ASSIGNED)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100525 _HA_ATOMIC_ADD(&s->be->be_counters.denied_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200526 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100527 _HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100528 goto return_prx_err;
529
530 return_int_err:
531 txn->status = 500;
532 if (!(s->flags & SF_ERR_MASK))
533 s->flags |= SF_ERR_INTERNAL;
534 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100535 if (s->flags & SF_BE_ASSIGNED)
536 _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100537 if (sess->listener->counters)
538 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
539 goto return_prx_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200540
541 return_bad_req:
Christopher Faulete0768eb2018-10-03 16:38:02 +0200542 txn->status = 400;
Olivier Houcharda798bf52019-03-08 18:52:00 +0100543 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200544 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100545 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100546 /* fall through */
547
548 return_prx_err:
549 http_reply_and_close(s, txn->status, http_error_message(s));
550 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200551
552 return_prx_cond:
553 if (!(s->flags & SF_ERR_MASK))
554 s->flags |= SF_ERR_PRXCOND;
555 if (!(s->flags & SF_FINST_MASK))
556 s->flags |= SF_FINST_R;
557
558 req->analysers &= AN_REQ_FLT_END;
559 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100560 DBG_TRACE_DEVEL("leaving on error",
561 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200562 return 0;
563
564 return_prx_yield:
565 channel_dont_connect(req);
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100566 DBG_TRACE_DEVEL("waiting for more data",
567 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200568 return 0;
569}
570
571/* This function performs all the processing enabled for the current request.
572 * It returns 1 if the processing can continue on next analysers, or zero if it
573 * needs more data, encounters an error, or wants to immediately abort the
574 * request. It relies on buffers flags, and updates s->req.analysers.
575 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200576int http_process_request(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200577{
578 struct session *sess = s->sess;
579 struct http_txn *txn = s->txn;
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200580 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200581 struct connection *cli_conn = objt_conn(strm_sess(s)->origin);
582
Christopher Faulet8bebd2f2020-10-06 17:54:56 +0200583 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200584
585 /*
586 * Right now, we know that we have processed the entire headers
587 * and that unwanted requests have been filtered out. We can do
588 * whatever we want with the remaining request. Also, now we
589 * may have separate values for ->fe, ->be.
590 */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100591 htx = htxbuf(&req->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200592
593 /*
594 * If HTTP PROXY is set we simply get remote server address parsing
Willy Tarreau1c8d32b2019-07-18 15:47:45 +0200595 * incoming request.
Christopher Faulete0768eb2018-10-03 16:38:02 +0200596 */
597 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SF_ADDR_SET)) {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100598 struct htx_sl *sl;
599 struct ist uri, path;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200600
Willy Tarreau9b7587a2020-10-15 07:32:10 +0200601 if (!sockaddr_alloc(&s->target_addr, NULL, 0)) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200602 if (!(s->flags & SF_ERR_MASK))
603 s->flags |= SF_ERR_RESOURCE;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100604 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200605 }
Christopher Faulet297fbb42019-05-13 14:41:27 +0200606 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100607 uri = htx_sl_req_uri(sl);
608 path = http_get_path(uri);
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200609
Willy Tarreau1c8d32b2019-07-18 15:47:45 +0200610 if (url2sa(uri.ptr, uri.len - path.len, s->target_addr, NULL) == -1)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200611 goto return_bad_req;
612
Willy Tarreau1c8d32b2019-07-18 15:47:45 +0200613 s->target = &s->be->obj_type;
614 s->flags |= SF_ADDR_SET | SF_ASSIGNED;
615
Christopher Faulete0768eb2018-10-03 16:38:02 +0200616 /* if the path was found, we have to remove everything between
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200617 * uri.ptr and path.ptr (excluded). If it was not found, we need
618 * to replace from all the uri by a single "/".
619 *
Ilya Shipitsin46a030c2020-07-05 16:36:08 +0500620 * Instead of rewriting the whole start line, we just update
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100621 * the star-line URI. Some space will be lost but it should be
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200622 * insignificant.
Christopher Faulete0768eb2018-10-03 16:38:02 +0200623 */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100624 istcpy(&uri, (path.len ? path : ist("/")), uri.len);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200625 }
626
627 /*
628 * 7: Now we can work with the cookies.
629 * Note that doing so might move headers in the request, but
630 * the fields will stay coherent and the URI will not move.
631 * This should only be performed in the backend.
632 */
633 if (s->be->cookie_name || sess->fe->capture_name)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200634 http_manage_client_side_cookies(s, req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200635
Tim Duesterhus2825b4b2020-02-28 15:13:34 +0100636 /* 8: Generate unique ID if a "unique-id-format" is defined.
637 *
638 * A unique ID is generated even when it is not sent to ensure that the ID can make use of
639 * fetches only available in the HTTP request processing stage.
640 */
641 if (!LIST_ISEMPTY(&sess->fe->format_unique_id)) {
Tim Duesterhusa17e6622020-03-05 20:19:02 +0100642 struct ist unique_id = stream_generate_unique_id(s, &sess->fe->format_unique_id);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200643
Tim Duesterhusa17e6622020-03-05 20:19:02 +0100644 if (!isttest(unique_id)) {
Christopher Fauletb8a53712019-12-16 11:29:38 +0100645 if (!(s->flags & SF_ERR_MASK))
646 s->flags |= SF_ERR_RESOURCE;
647 goto return_int_err;
648 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200649
Tim Duesterhus2825b4b2020-02-28 15:13:34 +0100650 /* send unique ID if a "unique-id-header" is defined */
Tim Duesterhus0643b0e2020-03-05 17:56:35 +0100651 if (isttest(sess->fe->header_unique_id) &&
Tim Duesterhusa17e6622020-03-05 20:19:02 +0100652 unlikely(!http_add_header(htx, sess->fe->header_unique_id, s->unique_id)))
Tim Duesterhus2825b4b2020-02-28 15:13:34 +0100653 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200654 }
655
656 /*
657 * 9: add X-Forwarded-For if either the frontend or the backend
658 * asks for it.
659 */
660 if ((sess->fe->options | s->be->options) & PR_O_FWDFOR) {
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200661 struct http_hdr_ctx ctx = { .blk = NULL };
662 struct ist hdr = ist2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : sess->fe->fwdfor_hdr_name,
663 s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : sess->fe->fwdfor_hdr_len);
664
Christopher Faulete0768eb2018-10-03 16:38:02 +0200665 if (!((sess->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200666 http_find_header(htx, hdr, &ctx, 0)) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200667 /* The header is set to be added only if none is present
668 * and we found it, so don't do anything.
669 */
670 }
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200671 else if (cli_conn && conn_get_src(cli_conn) && cli_conn->src->ss_family == AF_INET) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200672 /* Add an X-Forwarded-For header unless the source IP is
673 * in the 'except' network range.
674 */
675 if ((!sess->fe->except_mask.s_addr ||
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200676 (((struct sockaddr_in *)cli_conn->src)->sin_addr.s_addr & sess->fe->except_mask.s_addr)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200677 != sess->fe->except_net.s_addr) &&
678 (!s->be->except_mask.s_addr ||
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200679 (((struct sockaddr_in *)cli_conn->src)->sin_addr.s_addr & s->be->except_mask.s_addr)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200680 != s->be->except_net.s_addr)) {
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200681 unsigned char *pn = (unsigned char *)&((struct sockaddr_in *)cli_conn->src)->sin_addr;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200682
683 /* Note: we rely on the backend to get the header name to be used for
684 * x-forwarded-for, because the header is really meant for the backends.
685 * However, if the backend did not specify any option, we have to rely
686 * on the frontend's header name.
687 */
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200688 chunk_printf(&trash, "%d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
689 if (unlikely(!http_add_header(htx, hdr, ist2(trash.area, trash.data))))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100690 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200691 }
692 }
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200693 else if (cli_conn && conn_get_src(cli_conn) && cli_conn->src->ss_family == AF_INET6) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200694 /* FIXME: for the sake of completeness, we should also support
695 * 'except' here, although it is mostly useless in this case.
696 */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200697 char pn[INET6_ADDRSTRLEN];
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200698
Christopher Faulete0768eb2018-10-03 16:38:02 +0200699 inet_ntop(AF_INET6,
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200700 (const void *)&((struct sockaddr_in6 *)(cli_conn->src))->sin6_addr,
Christopher Faulete0768eb2018-10-03 16:38:02 +0200701 pn, sizeof(pn));
702
703 /* Note: we rely on the backend to get the header name to be used for
704 * x-forwarded-for, because the header is really meant for the backends.
705 * However, if the backend did not specify any option, we have to rely
706 * on the frontend's header name.
707 */
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200708 chunk_printf(&trash, "%s", pn);
709 if (unlikely(!http_add_header(htx, hdr, ist2(trash.area, trash.data))))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100710 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200711 }
712 }
713
714 /*
715 * 10: add X-Original-To if either the frontend or the backend
716 * asks for it.
717 */
718 if ((sess->fe->options | s->be->options) & PR_O_ORGTO) {
719
720 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200721 if (cli_conn && conn_get_src(cli_conn) && cli_conn->src->ss_family == AF_INET && conn_get_dst(cli_conn)) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200722 /* Add an X-Original-To header unless the destination IP is
723 * in the 'except' network range.
724 */
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200725 if (cli_conn->dst->ss_family == AF_INET &&
Christopher Faulete0768eb2018-10-03 16:38:02 +0200726 ((!sess->fe->except_mask_to.s_addr ||
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200727 (((struct sockaddr_in *)cli_conn->dst)->sin_addr.s_addr & sess->fe->except_mask_to.s_addr)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200728 != sess->fe->except_to.s_addr) &&
729 (!s->be->except_mask_to.s_addr ||
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200730 (((struct sockaddr_in *)cli_conn->dst)->sin_addr.s_addr & s->be->except_mask_to.s_addr)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200731 != s->be->except_to.s_addr))) {
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200732 struct ist hdr;
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200733 unsigned char *pn = (unsigned char *)&((struct sockaddr_in *)cli_conn->dst)->sin_addr;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200734
735 /* Note: we rely on the backend to get the header name to be used for
736 * x-original-to, because the header is really meant for the backends.
737 * However, if the backend did not specify any option, we have to rely
738 * on the frontend's header name.
739 */
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200740 if (s->be->orgto_hdr_len)
741 hdr = ist2(s->be->orgto_hdr_name, s->be->orgto_hdr_len);
742 else
743 hdr = ist2(sess->fe->orgto_hdr_name, sess->fe->orgto_hdr_len);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200744
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200745 chunk_printf(&trash, "%d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
746 if (unlikely(!http_add_header(htx, hdr, ist2(trash.area, trash.data))))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100747 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200748 }
749 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200750 }
751
Christopher Faulete0768eb2018-10-03 16:38:02 +0200752 /* If we have no server assigned yet and we're balancing on url_param
753 * with a POST request, we may be interested in checking the body for
754 * that parameter. This will be done in another analyser.
755 */
756 if (!(s->flags & (SF_ASSIGNED|SF_DIRECT)) &&
Willy Tarreau089eaa02019-01-14 15:17:46 +0100757 s->txn->meth == HTTP_METH_POST &&
758 (s->be->lbprm.algo & BE_LB_ALGO) == BE_LB_ALGO_PH) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200759 channel_dont_connect(req);
760 req->analysers |= AN_REQ_HTTP_BODY;
761 }
762
763 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
764 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau1a18b542018-12-11 16:37:42 +0100765
Christopher Faulete0768eb2018-10-03 16:38:02 +0200766 /* We expect some data from the client. Unless we know for sure
767 * we already have a full request, we have to re-enable quick-ack
768 * in case we previously disabled it, otherwise we might cause
769 * the client to delay further data.
770 */
771 if ((sess->listener->options & LI_O_NOQUICKACK) &&
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200772 (htx_get_tail_type(htx) != HTX_BLK_EOM))
Willy Tarreau1a18b542018-12-11 16:37:42 +0100773 conn_set_quickack(cli_conn, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200774
775 /*************************************************************
776 * OK, that's finished for the headers. We have done what we *
777 * could. Let's switch to the DATA state. *
778 ************************************************************/
779 req->analyse_exp = TICK_ETERNITY;
780 req->analysers &= ~an_bit;
781
782 s->logs.tv_request = now;
783 /* OK let's go on with the BODY now */
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100784 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200785 return 1;
786
Christopher Fauletb8a53712019-12-16 11:29:38 +0100787 return_int_err:
788 txn->status = 500;
789 if (!(s->flags & SF_ERR_MASK))
790 s->flags |= SF_ERR_INTERNAL;
791 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100792 if (s->flags & SF_BE_ASSIGNED)
Christopher Fauletbe20cf32020-01-24 11:41:38 +0100793 _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100794 if (sess->listener->counters)
795 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
796 goto return_prx_cond;
797
Christopher Faulete0768eb2018-10-03 16:38:02 +0200798 return_bad_req: /* let's centralize all bad requests */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200799 txn->status = 400;
Olivier Houcharda798bf52019-03-08 18:52:00 +0100800 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200801 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100802 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100803 /* fall through */
804
805 return_prx_cond:
806 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +0200807
808 if (!(s->flags & SF_ERR_MASK))
809 s->flags |= SF_ERR_PRXCOND;
810 if (!(s->flags & SF_FINST_MASK))
811 s->flags |= SF_FINST_R;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100812
813 req->analysers &= AN_REQ_FLT_END;
814 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100815 DBG_TRACE_DEVEL("leaving on error",
816 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200817 return 0;
818}
819
820/* This function is an analyser which processes the HTTP tarpit. It always
821 * returns zero, at the beginning because it prevents any other processing
822 * from occurring, and at the end because it terminates the request.
823 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200824int http_process_tarpit(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200825{
826 struct http_txn *txn = s->txn;
827
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100828 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, &txn->req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200829 /* This connection is being tarpitted. The CLIENT side has
830 * already set the connect expiration date to the right
831 * timeout. We just have to check that the client is still
832 * there and that the timeout has not expired.
833 */
834 channel_dont_connect(req);
835 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100836 !tick_is_expired(req->analyse_exp, now_ms)) {
837 DBG_TRACE_DEVEL("waiting for tarpit timeout expiry",
838 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200839 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100840 }
841
Christopher Faulete0768eb2018-10-03 16:38:02 +0200842
843 /* We will set the queue timer to the time spent, just for
844 * logging purposes. We fake a 500 server error, so that the
845 * attacker will not suspect his connection has been tarpitted.
846 * It will not cause trouble to the logs because we can exclude
847 * the tarpitted connections by filtering on the 'PT' status flags.
848 */
849 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
850
Christopher Faulet8dfeccf2020-05-15 14:16:29 +0200851 http_reply_and_close(s, txn->status, (!(req->flags & CF_READ_ERROR) ? http_error_message(s) : NULL));
Christopher Faulet5cb513a2020-05-13 17:56:56 +0200852
853 end:
Christopher Faulete0768eb2018-10-03 16:38:02 +0200854 req->analysers &= AN_REQ_FLT_END;
855 req->analyse_exp = TICK_ETERNITY;
856
857 if (!(s->flags & SF_ERR_MASK))
858 s->flags |= SF_ERR_PRXCOND;
859 if (!(s->flags & SF_FINST_MASK))
860 s->flags |= SF_FINST_T;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100861
862 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200863 return 0;
864}
865
866/* This function is an analyser which waits for the HTTP request body. It waits
867 * for either the buffer to be full, or the full advertised contents to have
868 * reached the buffer. It must only be called after the standard HTTP request
869 * processing has occurred, because it expects the request to be parsed and will
870 * look for the Expect header. It may send a 100-Continue interim response. It
871 * takes in input any state starting from HTTP_MSG_BODY and leaves with one of
872 * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it
873 * needs to read more data, or 1 once it has completed its analysis.
874 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200875int http_wait_for_request_body(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200876{
877 struct session *sess = s->sess;
878 struct http_txn *txn = s->txn;
879 struct http_msg *msg = &s->txn->req;
Christopher Fauletf76ebe82018-10-24 11:16:22 +0200880 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200881
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100882 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Fauletf76ebe82018-10-24 11:16:22 +0200883
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100884 htx = htxbuf(&req->buf);
Christopher Fauletf76ebe82018-10-24 11:16:22 +0200885
Willy Tarreau4236f032019-03-05 10:43:32 +0100886 if (htx->flags & HTX_FL_PARSING_ERROR)
887 goto return_bad_req;
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200888 if (htx->flags & HTX_FL_PROCESSING_ERROR)
889 goto return_int_err;
Willy Tarreau4236f032019-03-05 10:43:32 +0100890
Christopher Fauletc31bc722020-11-20 14:30:38 +0100891 /* Do nothing for bodyless and CONNECT requests */
892 if (txn->meth == HTTP_METH_CONNECT || (msg->flags & HTTP_MSGF_BODYLESS))
Christopher Faulet63c69a92020-11-16 16:03:35 +0100893 goto http_end;
894
Christopher Faulete0768eb2018-10-03 16:38:02 +0200895 /* We have to parse the HTTP request body to find any required data.
896 * "balance url_param check_post" should have been the only way to get
897 * into this. We were brought here after HTTP header analysis, so all
898 * related structures are ready.
899 */
900
Christopher Fauletf76ebe82018-10-24 11:16:22 +0200901 if (msg->msg_state < HTTP_MSG_DATA) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200902 if (http_handle_expect_hdr(s, htx, msg) == -1)
Christopher Fauletb8a53712019-12-16 11:29:38 +0100903 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200904 }
905
Christopher Fauletf76ebe82018-10-24 11:16:22 +0200906 msg->msg_state = HTTP_MSG_DATA;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200907
Christopher Fauletf76ebe82018-10-24 11:16:22 +0200908 /* Now we're in HTTP_MSG_DATA. We just need to know if all data have
909 * been received or if the buffer is full.
Christopher Faulete0768eb2018-10-03 16:38:02 +0200910 */
Christopher Faulet54b5e212019-06-04 10:08:28 +0200911 if (htx_get_tail_type(htx) > HTX_BLK_DATA ||
Christopher Fauletdcd8c5e2019-01-21 11:24:38 +0100912 channel_htx_full(req, htx, global.tune.maxrewrite))
Christopher Faulete0768eb2018-10-03 16:38:02 +0200913 goto http_end;
914
915 if ((req->flags & CF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
916 txn->status = 408;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200917 if (!(s->flags & SF_ERR_MASK))
918 s->flags |= SF_ERR_CLITO;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100919 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
920 if (sess->listener->counters)
921 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
922 goto return_prx_cond;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200923 }
924
925 /* we get here if we need to wait for more data */
926 if (!(req->flags & (CF_SHUTR | CF_READ_ERROR))) {
927 /* Not enough data. We'll re-use the http-request
928 * timeout here. Ideally, we should set the timeout
929 * relative to the accept() date. We just set the
930 * request timeout once at the beginning of the
931 * request.
932 */
933 channel_dont_connect(req);
934 if (!tick_isset(req->analyse_exp))
935 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100936 DBG_TRACE_DEVEL("waiting for more data",
937 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200938 return 0;
939 }
940
941 http_end:
942 /* The situation will not evolve, so let's give up on the analysis. */
943 s->logs.tv_request = now; /* update the request timer to reflect full request */
944 req->analysers &= ~an_bit;
945 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100946 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200947 return 1;
948
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200949 return_int_err:
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200950 txn->status = 500;
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200951 if (!(s->flags & SF_ERR_MASK))
952 s->flags |= SF_ERR_INTERNAL;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100953 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100954 if (s->flags & SF_BE_ASSIGNED)
Christopher Fauletbe20cf32020-01-24 11:41:38 +0100955 _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100956 if (sess->listener->counters)
957 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
958 goto return_prx_cond;
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200959
Christopher Faulete0768eb2018-10-03 16:38:02 +0200960 return_bad_req: /* let's centralize all bad requests */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200961 txn->status = 400;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100962 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
963 if (sess->listener->counters)
964 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
965 /* fall through */
966
967 return_prx_cond:
968 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +0200969
970 if (!(s->flags & SF_ERR_MASK))
971 s->flags |= SF_ERR_PRXCOND;
972 if (!(s->flags & SF_FINST_MASK))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100973 s->flags |= (msg->msg_state < HTTP_MSG_DATA ? SF_FINST_R : SF_FINST_D);
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200974
Christopher Faulete0768eb2018-10-03 16:38:02 +0200975 req->analysers &= AN_REQ_FLT_END;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100976 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100977 DBG_TRACE_DEVEL("leaving on error",
978 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200979 return 0;
980}
981
982/* This function is an analyser which forwards request body (including chunk
983 * sizes if any). It is called as soon as we must forward, even if we forward
984 * zero byte. The only situation where it must not be called is when we're in
985 * tunnel mode and we want to forward till the close. It's used both to forward
986 * remaining data and to resync after end of body. It expects the msg_state to
987 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
988 * read more data, or 1 once we can go on with next request or end the stream.
989 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
990 * bytes of pending data + the headers if not already done.
991 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200992int http_request_forward_body(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200993{
994 struct session *sess = s->sess;
995 struct http_txn *txn = s->txn;
Christopher Faulet9768c262018-10-22 09:34:31 +0200996 struct http_msg *msg = &txn->req;
997 struct htx *htx;
Christopher Faulet93e02d82019-03-08 14:18:50 +0100998 short status = 0;
Christopher Fauletaed82cf2018-11-30 22:22:32 +0100999 int ret;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001000
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001001 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001002
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001003 htx = htxbuf(&req->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001004
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001005 if (htx->flags & HTX_FL_PARSING_ERROR)
1006 goto return_bad_req;
1007 if (htx->flags & HTX_FL_PROCESSING_ERROR)
1008 goto return_int_err;
1009
Christopher Faulete0768eb2018-10-03 16:38:02 +02001010 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
1011 ((req->flags & CF_SHUTW) && (req->to_forward || co_data(req)))) {
1012 /* Output closed while we were sending data. We must abort and
1013 * wake the other side up.
1014 */
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001015
Olivier Houchard29cac3c2019-07-12 15:48:58 +02001016 /* Don't abort yet if we had L7 retries activated and it
1017 * was a write error, we may recover.
1018 */
1019 if (!(req->flags & (CF_READ_ERROR | CF_READ_TIMEOUT)) &&
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001020 (s->si[1].flags & SI_FL_L7_RETRY)) {
1021 DBG_TRACE_DEVEL("leaving on L7 retry",
1022 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Olivier Houchard29cac3c2019-07-12 15:48:58 +02001023 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001024 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001025 msg->msg_state = HTTP_MSG_ERROR;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001026 http_end_request(s);
1027 http_end_response(s);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001028 DBG_TRACE_DEVEL("leaving on error",
1029 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001030 return 1;
1031 }
1032
1033 /* Note that we don't have to send 100-continue back because we don't
1034 * need the data to complete our job, and it's up to the server to
1035 * decide whether to return 100, 417 or anything else in return of
1036 * an "Expect: 100-continue" header.
1037 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001038 if (msg->msg_state == HTTP_MSG_BODY)
1039 msg->msg_state = HTTP_MSG_DATA;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001040
Christopher Faulete0768eb2018-10-03 16:38:02 +02001041 /* in most states, we should abort in case of early close */
1042 channel_auto_close(req);
1043
1044 if (req->to_forward) {
Christopher Faulet66af0b22019-03-22 14:54:52 +01001045 if (req->to_forward == CHN_INFINITE_FORWARD) {
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001046 if (req->flags & CF_EOI)
1047 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet66af0b22019-03-22 14:54:52 +01001048 }
1049 else {
1050 /* We can't process the buffer's contents yet */
1051 req->flags |= CF_WAKE_WRITE;
1052 goto missing_data_or_waiting;
1053 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001054 }
1055
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001056 if (msg->msg_state >= HTTP_MSG_ENDING)
1057 goto ending;
1058
1059 if (txn->meth == HTTP_METH_CONNECT) {
1060 msg->msg_state = HTTP_MSG_ENDING;
1061 goto ending;
1062 }
1063
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001064 /* Forward input data. We get it by removing all outgoing data not
1065 * forwarded yet from HTX data size. If there are some data filters, we
1066 * let them decide the amount of data to forward.
Christopher Faulet9768c262018-10-22 09:34:31 +02001067 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001068 if (HAS_REQ_DATA_FILTERS(s)) {
1069 ret = flt_http_payload(s, msg, htx->data);
1070 if (ret < 0)
1071 goto return_bad_req;
Christopher Faulet421e7692019-06-13 11:16:45 +02001072 c_adv(req, ret);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001073 }
1074 else {
Christopher Faulet421e7692019-06-13 11:16:45 +02001075 c_adv(req, htx->data - co_data(req));
Christopher Faulet66af0b22019-03-22 14:54:52 +01001076 if (msg->flags & HTTP_MSGF_XFER_LEN)
1077 channel_htx_forward_forever(req, htx);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001078 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001079
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001080 if (htx->data != co_data(req))
1081 goto missing_data_or_waiting;
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001082
Christopher Faulet9768c262018-10-22 09:34:31 +02001083 /* Check if the end-of-message is reached and if so, switch the message
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001084 * in HTTP_MSG_ENDING state. Then if all data was marked to be
1085 * forwarded, set the state to HTTP_MSG_DONE.
Christopher Faulet9768c262018-10-22 09:34:31 +02001086 */
1087 if (htx_get_tail_type(htx) != HTX_BLK_EOM)
1088 goto missing_data_or_waiting;
1089
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001090 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet9768c262018-10-22 09:34:31 +02001091
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001092 ending:
Christopher Faulet2151cdd2020-07-22 16:34:59 +02001093 req->flags &= ~CF_EXPECT_MORE; /* no more data are expected */
1094
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001095 /* other states, ENDING...TUNNEL */
1096 if (msg->msg_state >= HTTP_MSG_DONE)
1097 goto done;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001098
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001099 if (HAS_REQ_DATA_FILTERS(s)) {
1100 ret = flt_http_end(s, msg);
1101 if (ret <= 0) {
1102 if (!ret)
1103 goto missing_data_or_waiting;
1104 goto return_bad_req;
1105 }
1106 }
1107
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001108 if (txn->meth == HTTP_METH_CONNECT)
1109 msg->msg_state = HTTP_MSG_TUNNEL;
1110 else {
1111 msg->msg_state = HTTP_MSG_DONE;
1112 req->to_forward = 0;
1113 }
1114
1115 done:
1116 /* we don't want to forward closes on DONE except in tunnel mode. */
1117 if (!(txn->flags & TX_CON_WANT_TUN))
1118 channel_dont_close(req);
1119
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001120 http_end_request(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001121 if (!(req->analysers & an_bit)) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001122 http_end_response(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001123 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
1124 if (req->flags & CF_SHUTW) {
1125 /* request errors are most likely due to the
1126 * server aborting the transfer. */
Christopher Faulet93e02d82019-03-08 14:18:50 +01001127 goto return_srv_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001128 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001129 goto return_bad_req;
1130 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001131 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001132 return 1;
1133 }
1134
1135 /* If "option abortonclose" is set on the backend, we want to monitor
1136 * the client's connection and forward any shutdown notification to the
1137 * server, which will decide whether to close or to go on processing the
1138 * request. We only do that in tunnel mode, and not in other modes since
1139 * it can be abused to exhaust source ports. */
Christopher Faulet769d0e92019-03-22 14:23:18 +01001140 if (s->be->options & PR_O_ABRT_CLOSE) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001141 channel_auto_read(req);
Christopher Fauletc41547b2019-07-16 14:32:23 +02001142 if ((req->flags & (CF_SHUTR|CF_READ_NULL)) && !(txn->flags & TX_CON_WANT_TUN))
Christopher Faulete0768eb2018-10-03 16:38:02 +02001143 s->si[1].flags |= SI_FL_NOLINGER;
1144 channel_auto_close(req);
1145 }
1146 else if (s->txn->meth == HTTP_METH_POST) {
1147 /* POST requests may require to read extra CRLF sent by broken
1148 * browsers and which could cause an RST to be sent upon close
1149 * on some systems (eg: Linux). */
1150 channel_auto_read(req);
1151 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001152 DBG_TRACE_DEVEL("waiting for the end of the HTTP txn",
1153 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001154 return 0;
1155
1156 missing_data_or_waiting:
1157 /* stop waiting for data if the input is closed before the end */
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001158 if (msg->msg_state < HTTP_MSG_ENDING && req->flags & CF_SHUTR)
Christopher Faulet93e02d82019-03-08 14:18:50 +01001159 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001160
1161 waiting:
1162 /* waiting for the last bits to leave the buffer */
1163 if (req->flags & CF_SHUTW)
Christopher Faulet93e02d82019-03-08 14:18:50 +01001164 goto return_srv_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001165
1166 /* When TE: chunked is used, we need to get there again to parse remaining
1167 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
1168 * And when content-length is used, we never want to let the possible
1169 * shutdown be forwarded to the other side, as the state machine will
1170 * take care of it once the client responds. It's also important to
1171 * prevent TIME_WAITs from accumulating on the backend side, and for
1172 * HTTP/2 where the last frame comes with a shutdown.
1173 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001174 if (msg->flags & HTTP_MSGF_XFER_LEN)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001175 channel_dont_close(req);
1176
1177 /* We know that more data are expected, but we couldn't send more that
1178 * what we did. So we always set the CF_EXPECT_MORE flag so that the
1179 * system knows it must not set a PUSH on this first part. Interactive
1180 * modes are already handled by the stream sock layer. We must not do
1181 * this in content-length mode because it could present the MSG_MORE
1182 * flag with the last block of forwarded data, which would cause an
1183 * additional delay to be observed by the receiver.
1184 */
Christopher Faulet2151cdd2020-07-22 16:34:59 +02001185 if (HAS_REQ_DATA_FILTERS(s))
Christopher Faulete0768eb2018-10-03 16:38:02 +02001186 req->flags |= CF_EXPECT_MORE;
1187
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001188 DBG_TRACE_DEVEL("waiting for more data to forward",
1189 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001190 return 0;
1191
Christopher Faulet93e02d82019-03-08 14:18:50 +01001192 return_cli_abort:
1193 _HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
1194 _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001195 if (sess->listener->counters)
1196 _HA_ATOMIC_ADD(&sess->listener->counters->cli_aborts, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001197 if (objt_server(s->target))
Christopher Fauletcff0f732019-12-16 16:13:44 +01001198 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.cli_aborts, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001199 if (!(s->flags & SF_ERR_MASK))
1200 s->flags |= SF_ERR_CLICL;
1201 status = 400;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001202 goto return_prx_cond;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001203
1204 return_srv_abort:
1205 _HA_ATOMIC_ADD(&sess->fe->fe_counters.srv_aborts, 1);
1206 _HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001207 if (sess->listener->counters)
1208 _HA_ATOMIC_ADD(&sess->listener->counters->srv_aborts, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001209 if (objt_server(s->target))
Christopher Fauletcff0f732019-12-16 16:13:44 +01001210 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.srv_aborts, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001211 if (!(s->flags & SF_ERR_MASK))
1212 s->flags |= SF_ERR_SRVCL;
1213 status = 502;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001214 goto return_prx_cond;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001215
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001216 return_int_err:
1217 if (!(s->flags & SF_ERR_MASK))
1218 s->flags |= SF_ERR_INTERNAL;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001219 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001220 _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01001221 if (sess->listener->counters)
1222 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001223 if (objt_server(s->target))
1224 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.internal_errors, 1);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001225 status = 500;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001226 goto return_prx_cond;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001227
Christopher Faulet93e02d82019-03-08 14:18:50 +01001228 return_bad_req:
Olivier Houcharda798bf52019-03-08 18:52:00 +01001229 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001230 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +01001231 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001232 status = 400;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001233 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001234
Christopher Fauletb8a53712019-12-16 11:29:38 +01001235 return_prx_cond:
Christopher Faulet9768c262018-10-22 09:34:31 +02001236 if (txn->status > 0) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001237 /* Note: we don't send any error if some data were already sent */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001238 http_reply_and_close(s, txn->status, NULL);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001239 } else {
Christopher Faulet93e02d82019-03-08 14:18:50 +01001240 txn->status = status;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001241 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001242 }
1243 req->analysers &= AN_REQ_FLT_END;
1244 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 +01001245 if (!(s->flags & SF_ERR_MASK))
1246 s->flags |= SF_ERR_PRXCOND;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001247 if (!(s->flags & SF_FINST_MASK))
1248 s->flags |= ((txn->rsp.msg_state < HTTP_MSG_ERROR) ? SF_FINST_H : SF_FINST_D);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001249 DBG_TRACE_DEVEL("leaving on error ",
1250 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001251 return 0;
1252}
1253
Olivier Houcharda254a372019-04-05 15:30:12 +02001254/* Reset the stream and the backend stream_interface to a situation suitable for attemption connection */
1255/* Returns 0 if we can attempt to retry, -1 otherwise */
1256static __inline int do_l7_retry(struct stream *s, struct stream_interface *si)
1257{
1258 struct channel *req, *res;
1259 int co_data;
1260
1261 si->conn_retries--;
1262 if (si->conn_retries < 0)
1263 return -1;
1264
Willy Tarreau223995e2019-05-04 10:38:31 +02001265 if (objt_server(s->target))
1266 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.retries, 1);
1267 _HA_ATOMIC_ADD(&s->be->be_counters.retries, 1);
1268
Olivier Houcharda254a372019-04-05 15:30:12 +02001269 req = &s->req;
1270 res = &s->res;
1271 /* Remove any write error from the request, and read error from the response */
1272 req->flags &= ~(CF_WRITE_ERROR | CF_WRITE_TIMEOUT | CF_SHUTW | CF_SHUTW_NOW);
1273 res->flags &= ~(CF_READ_ERROR | CF_READ_TIMEOUT | CF_SHUTR | CF_EOI | CF_READ_NULL | CF_SHUTR_NOW);
1274 res->analysers = 0;
1275 si->flags &= ~(SI_FL_ERR | SI_FL_EXP | SI_FL_RXBLK_SHUT);
Olivier Houchard8cabc972020-05-12 22:18:14 +02001276 s->flags &= ~SF_ADDR_SET;
Olivier Houchard4bd58672019-07-12 16:16:59 +02001277 stream_choose_redispatch(s);
Olivier Houcharda254a372019-04-05 15:30:12 +02001278 si->exp = TICK_ETERNITY;
1279 res->rex = TICK_ETERNITY;
1280 res->to_forward = 0;
1281 res->analyse_exp = TICK_ETERNITY;
1282 res->total = 0;
Olivier Houchard4bd58672019-07-12 16:16:59 +02001283 s->flags &= ~(SF_ERR_SRVTO | SF_ERR_SRVCL);
Olivier Houcharda254a372019-04-05 15:30:12 +02001284 si_release_endpoint(&s->si[1]);
1285 b_free(&req->buf);
1286 /* Swap the L7 buffer with the channel buffer */
1287 /* We know we stored the co_data as b_data, so get it there */
1288 co_data = b_data(&si->l7_buffer);
1289 b_set_data(&si->l7_buffer, b_size(&si->l7_buffer));
1290 b_xfer(&req->buf, &si->l7_buffer, b_data(&si->l7_buffer));
1291
1292 co_set_data(req, co_data);
1293 b_reset(&res->buf);
1294 co_set_data(res, 0);
1295 return 0;
1296}
1297
Christopher Faulete0768eb2018-10-03 16:38:02 +02001298/* This stream analyser waits for a complete HTTP response. It returns 1 if the
1299 * processing can continue on next analysers, or zero if it either needs more
1300 * data or wants to immediately abort the response (eg: timeout, error, ...). It
1301 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->res.analysers
1302 * when it has nothing left to do, and may remove any analyser when it wants to
1303 * abort.
1304 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001305int http_wait_for_response(struct stream *s, struct channel *rep, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001306{
Christopher Faulet9768c262018-10-22 09:34:31 +02001307 /*
1308 * We will analyze a complete HTTP response to check the its syntax.
1309 *
1310 * Once the start line and all headers are received, we may perform a
1311 * capture of the error (if any), and we will set a few fields. We also
1312 * logging and finally headers capture.
1313 */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001314 struct session *sess = s->sess;
1315 struct http_txn *txn = s->txn;
1316 struct http_msg *msg = &txn->rsp;
Christopher Faulet9768c262018-10-22 09:34:31 +02001317 struct htx *htx;
Olivier Houcharda254a372019-04-05 15:30:12 +02001318 struct stream_interface *si_b = &s->si[1];
Christopher Faulet61608322018-11-23 16:23:45 +01001319 struct connection *srv_conn;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001320 struct htx_sl *sl;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001321 int n;
1322
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001323 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001324
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001325 htx = htxbuf(&rep->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001326
Willy Tarreau4236f032019-03-05 10:43:32 +01001327 /* Parsing errors are caught here */
1328 if (htx->flags & HTX_FL_PARSING_ERROR)
1329 goto return_bad_res;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001330 if (htx->flags & HTX_FL_PROCESSING_ERROR)
1331 goto return_int_err;
Willy Tarreau4236f032019-03-05 10:43:32 +01001332
Christopher Faulete0768eb2018-10-03 16:38:02 +02001333 /*
1334 * Now we quickly check if we have found a full valid response.
1335 * If not so, we check the FD and buffer states before leaving.
1336 * A full response is indicated by the fact that we have seen
1337 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
1338 * responses are checked first.
1339 *
1340 * Depending on whether the client is still there or not, we
1341 * may send an error response back or not. Note that normally
1342 * we should only check for HTTP status there, and check I/O
1343 * errors somewhere else.
1344 */
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001345 next_one:
Christopher Faulet29f17582019-05-23 11:03:26 +02001346 if (unlikely(htx_is_empty(htx) || htx->first == -1)) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001347 /* 1: have we encountered a read error ? */
1348 if (rep->flags & CF_READ_ERROR) {
Olivier Houchard865d8392019-05-03 22:46:27 +02001349 struct connection *conn = NULL;
1350
Olivier Houchard865d8392019-05-03 22:46:27 +02001351 if (objt_cs(s->si[1].end))
1352 conn = objt_cs(s->si[1].end)->conn;
1353
1354 if (si_b->flags & SI_FL_L7_RETRY &&
1355 (!conn || conn->err_code != CO_ER_SSL_EARLY_FAILED)) {
Olivier Houcharda254a372019-04-05 15:30:12 +02001356 /* If we arrive here, then CF_READ_ERROR was
1357 * set by si_cs_recv() because we matched a
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05001358 * status, otherwise it would have removed
Olivier Houcharda254a372019-04-05 15:30:12 +02001359 * the SI_FL_L7_RETRY flag, so it's ok not
1360 * to check s->be->retry_type.
1361 */
1362 if (co_data(rep) || do_l7_retry(s, si_b) == 0)
1363 return 0;
1364 }
1365
Olivier Houchard6db16992019-05-17 15:40:49 +02001366 if (txn->flags & TX_NOT_FIRST)
1367 goto abort_keep_alive;
1368
Olivier Houcharda798bf52019-03-08 18:52:00 +01001369 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001370 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001371 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001372 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001373 }
1374
Christopher Faulete0768eb2018-10-03 16:38:02 +02001375 rep->analysers &= AN_RES_FLT_END;
1376 txn->status = 502;
1377
1378 /* Check to see if the server refused the early data.
1379 * If so, just send a 425
1380 */
Willy Tarreauee99aaf2020-06-23 05:58:20 +02001381 if (conn && conn->err_code == CO_ER_SSL_EARLY_FAILED) {
Olivier Houchard865d8392019-05-03 22:46:27 +02001382 if ((s->be->retry_type & PR_RE_EARLY_ERROR) &&
Olivier Houchardad26d8d2019-05-10 17:48:28 +02001383 (si_b->flags & SI_FL_L7_RETRY) &&
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001384 do_l7_retry(s, si_b) == 0) {
1385 DBG_TRACE_DEVEL("leaving on L7 retry",
1386 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houchard865d8392019-05-03 22:46:27 +02001387 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001388 }
Olivier Houchard865d8392019-05-03 22:46:27 +02001389 txn->status = 425;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001390 }
1391
1392 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001393 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001394
1395 if (!(s->flags & SF_ERR_MASK))
1396 s->flags |= SF_ERR_SRVCL;
1397 if (!(s->flags & SF_FINST_MASK))
1398 s->flags |= SF_FINST_H;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001399 DBG_TRACE_DEVEL("leaving on error",
1400 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001401 return 0;
1402 }
1403
Christopher Faulet9768c262018-10-22 09:34:31 +02001404 /* 2: read timeout : return a 504 to the client. */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001405 else if (rep->flags & CF_READ_TIMEOUT) {
Olivier Houcharda254a372019-04-05 15:30:12 +02001406 if ((si_b->flags & SI_FL_L7_RETRY) &&
1407 (s->be->retry_type & PR_RE_TIMEOUT)) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001408 if (co_data(rep) || do_l7_retry(s, si_b) == 0) {
1409 DBG_TRACE_DEVEL("leaving on L7 retry",
1410 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharda254a372019-04-05 15:30:12 +02001411 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001412 }
Olivier Houcharda254a372019-04-05 15:30:12 +02001413 }
Olivier Houcharda798bf52019-03-08 18:52:00 +01001414 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001415 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001416 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001417 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001418 }
1419
Christopher Faulete0768eb2018-10-03 16:38:02 +02001420 rep->analysers &= AN_RES_FLT_END;
1421 txn->status = 504;
1422 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001423 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001424
1425 if (!(s->flags & SF_ERR_MASK))
1426 s->flags |= SF_ERR_SRVTO;
1427 if (!(s->flags & SF_FINST_MASK))
1428 s->flags |= SF_FINST_H;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001429 DBG_TRACE_DEVEL("leaving on error",
1430 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001431 return 0;
1432 }
1433
Christopher Faulet9768c262018-10-22 09:34:31 +02001434 /* 3: client abort with an abortonclose */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001435 else if ((rep->flags & CF_SHUTR) && ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001436 _HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
1437 _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001438 if (sess->listener->counters)
1439 _HA_ATOMIC_ADD(&sess->listener->counters->cli_aborts, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001440 if (objt_server(s->target))
Olivier Houcharda798bf52019-03-08 18:52:00 +01001441 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.cli_aborts, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001442
1443 rep->analysers &= AN_RES_FLT_END;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001444 txn->status = 400;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001445 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001446
1447 if (!(s->flags & SF_ERR_MASK))
1448 s->flags |= SF_ERR_CLICL;
1449 if (!(s->flags & SF_FINST_MASK))
1450 s->flags |= SF_FINST_H;
1451
1452 /* process_stream() will take care of the error */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001453 DBG_TRACE_DEVEL("leaving on error",
1454 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001455 return 0;
1456 }
1457
Christopher Faulet9768c262018-10-22 09:34:31 +02001458 /* 4: close from server, capture the response if the server has started to respond */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001459 else if (rep->flags & CF_SHUTR) {
Olivier Houcharda254a372019-04-05 15:30:12 +02001460 if ((si_b->flags & SI_FL_L7_RETRY) &&
1461 (s->be->retry_type & PR_RE_DISCONNECTED)) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001462 if (co_data(rep) || do_l7_retry(s, si_b) == 0) {
1463 DBG_TRACE_DEVEL("leaving on L7 retry",
1464 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharda254a372019-04-05 15:30:12 +02001465 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001466 }
Olivier Houcharda254a372019-04-05 15:30:12 +02001467 }
1468
Olivier Houchard6db16992019-05-17 15:40:49 +02001469 if (txn->flags & TX_NOT_FIRST)
1470 goto abort_keep_alive;
1471
Olivier Houcharda798bf52019-03-08 18:52:00 +01001472 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001473 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001474 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001475 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001476 }
1477
Christopher Faulete0768eb2018-10-03 16:38:02 +02001478 rep->analysers &= AN_RES_FLT_END;
1479 txn->status = 502;
1480 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001481 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001482
1483 if (!(s->flags & SF_ERR_MASK))
1484 s->flags |= SF_ERR_SRVCL;
1485 if (!(s->flags & SF_FINST_MASK))
1486 s->flags |= SF_FINST_H;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001487 DBG_TRACE_DEVEL("leaving on error",
1488 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001489 return 0;
1490 }
1491
Christopher Faulet9768c262018-10-22 09:34:31 +02001492 /* 5: write error to client (we don't send any message then) */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001493 else if (rep->flags & CF_WRITE_ERROR) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001494 if (txn->flags & TX_NOT_FIRST)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001495 goto abort_keep_alive;
1496
Olivier Houcharda798bf52019-03-08 18:52:00 +01001497 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001498 if (objt_server(s->target))
1499 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001500 rep->analysers &= AN_RES_FLT_END;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001501
1502 if (!(s->flags & SF_ERR_MASK))
1503 s->flags |= SF_ERR_CLICL;
1504 if (!(s->flags & SF_FINST_MASK))
1505 s->flags |= SF_FINST_H;
1506
1507 /* process_stream() will take care of the error */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001508 DBG_TRACE_DEVEL("leaving on error",
1509 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001510 return 0;
1511 }
1512
1513 channel_dont_close(rep);
1514 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001515 DBG_TRACE_DEVEL("waiting for more data",
1516 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001517 return 0;
1518 }
1519
1520 /* More interesting part now : we know that we have a complete
1521 * response which at least looks like HTTP. We have an indicator
1522 * of each header's length, so we can parse them quickly.
1523 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001524 msg->msg_state = HTTP_MSG_BODY;
Christopher Faulet29f17582019-05-23 11:03:26 +02001525 BUG_ON(htx_get_first_type(htx) != HTX_BLK_RES_SL);
Christopher Faulet297fbb42019-05-13 14:41:27 +02001526 sl = http_get_stline(htx);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001527
Christopher Faulet9768c262018-10-22 09:34:31 +02001528 /* 0: we might have to print this header in debug mode */
1529 if (unlikely((global.mode & MODE_DEBUG) &&
1530 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
1531 int32_t pos;
1532
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001533 http_debug_stline("srvrep", s, sl);
Christopher Faulet9768c262018-10-22 09:34:31 +02001534
Christopher Fauleta3f15502019-05-13 15:27:23 +02001535 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001536 struct htx_blk *blk = htx_get_blk(htx, pos);
1537 enum htx_blk_type type = htx_get_blk_type(blk);
1538
1539 if (type == HTX_BLK_EOH)
1540 break;
1541 if (type != HTX_BLK_HDR)
1542 continue;
1543
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001544 http_debug_hdr("srvhdr", s,
1545 htx_get_blk_name(htx, blk),
1546 htx_get_blk_value(htx, blk));
Christopher Faulet9768c262018-10-22 09:34:31 +02001547 }
1548 }
1549
Christopher Faulet03599112018-11-27 11:21:21 +01001550 /* 1: get the status code and the version. Also set HTTP flags */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001551 txn->status = sl->info.res.status;
Christopher Faulet03599112018-11-27 11:21:21 +01001552 if (sl->flags & HTX_SL_F_VER_11)
Christopher Faulet9768c262018-10-22 09:34:31 +02001553 msg->flags |= HTTP_MSGF_VER_11;
Christopher Faulet03599112018-11-27 11:21:21 +01001554 if (sl->flags & HTX_SL_F_XFER_LEN) {
1555 msg->flags |= HTTP_MSGF_XFER_LEN;
Christopher Faulet2a408542020-11-20 14:22:37 +01001556 if (sl->flags & HTX_SL_F_CLEN)
1557 msg->flags |= HTTP_MSGF_CNT_LEN;
1558 else if (sl->flags & HTX_SL_F_CHNK)
1559 msg->flags |= HTTP_MSGF_TE_CHNK;
Christopher Faulet03599112018-11-27 11:21:21 +01001560 }
Christopher Faulet2a408542020-11-20 14:22:37 +01001561 if (sl->flags & HTX_SL_F_BODYLESS)
1562 msg->flags |= HTTP_MSGF_BODYLESS;
Christopher Faulet576c3582021-01-08 15:53:01 +01001563 if (sl->flags & HTX_SL_F_CONN_UPG)
1564 msg->flags |= HTTP_MSGF_CONN_UPG;
Christopher Faulet9768c262018-10-22 09:34:31 +02001565
1566 n = txn->status / 100;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001567 if (n < 1 || n > 5)
1568 n = 0;
Christopher Faulet9768c262018-10-22 09:34:31 +02001569
Christopher Faulete0768eb2018-10-03 16:38:02 +02001570 /* when the client triggers a 4xx from the server, it's most often due
1571 * to a missing object or permission. These events should be tracked
1572 * because if they happen often, it may indicate a brute force or a
1573 * vulnerability scan.
1574 */
1575 if (n == 4)
1576 stream_inc_http_err_ctr(s);
1577
Marcin Deranek3c27dda2020-05-15 18:32:51 +02001578 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001579 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.p.http.rsp[n], 1);
Marcin Deranek3c27dda2020-05-15 18:32:51 +02001580 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.p.http.cum_req, 1);
1581 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001582
Christopher Faulete0768eb2018-10-03 16:38:02 +02001583 /* Adjust server's health based on status code. Note: status codes 501
1584 * and 505 are triggered on demand by client request, so we must not
1585 * count them as server failures.
1586 */
1587 if (objt_server(s->target)) {
1588 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001589 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_OK);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001590 else
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001591 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_STS);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001592 }
1593
1594 /*
1595 * We may be facing a 100-continue response, or any other informational
1596 * 1xx response which is non-final, in which case this is not the right
1597 * response, and we're waiting for the next one. Let's allow this response
1598 * to go to the client and wait for the next one. There's an exception for
1599 * 101 which is used later in the code to switch protocols.
1600 */
1601 if (txn->status < 200 &&
1602 (txn->status == 100 || txn->status >= 102)) {
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001603 FLT_STRM_CB(s, flt_http_reset(s, msg));
Christopher Faulet421e7692019-06-13 11:16:45 +02001604 htx->first = channel_htx_fwd_headers(rep, htx);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001605 msg->msg_state = HTTP_MSG_RPBEFORE;
Christopher Faulet3499f622019-09-03 15:23:54 +02001606 msg->flags = 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001607 txn->status = 0;
1608 s->logs.t_data = -1; /* was not a response yet */
Christopher Faulet7d518452020-08-31 11:07:07 +02001609 rep->flags |= CF_SEND_DONTWAIT; /* Send ASAP informational messages */
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001610 goto next_one;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001611 }
1612
Christopher Faulet6e6c7b12021-01-08 16:02:05 +01001613 /* A 101-switching-protocols must contains a Connection header with the
1614 * "upgrade" option and the request too. It means both are agree to
1615 * upgrade. It is not so strict because there is no test on the Upgrade
1616 * header content. But it is probably stronger enough for now.
1617 */
1618 if (txn->status == 101 &&
1619 (!(txn->req.flags & HTTP_MSGF_CONN_UPG) || !(txn->rsp.flags & HTTP_MSGF_CONN_UPG)))
1620 goto return_bad_res;
1621
Christopher Faulete0768eb2018-10-03 16:38:02 +02001622 /*
1623 * 2: check for cacheability.
1624 */
1625
1626 switch (txn->status) {
1627 case 200:
1628 case 203:
1629 case 204:
1630 case 206:
1631 case 300:
1632 case 301:
1633 case 404:
1634 case 405:
1635 case 410:
1636 case 414:
1637 case 501:
1638 break;
1639 default:
1640 /* RFC7231#6.1:
1641 * Responses with status codes that are defined as
1642 * cacheable by default (e.g., 200, 203, 204, 206,
1643 * 300, 301, 404, 405, 410, 414, and 501 in this
1644 * specification) can be reused by a cache with
1645 * heuristic expiration unless otherwise indicated
1646 * by the method definition or explicit cache
1647 * controls [RFC7234]; all other status codes are
1648 * not cacheable by default.
1649 */
1650 txn->flags &= ~(TX_CACHEABLE | TX_CACHE_COOK);
1651 break;
1652 }
1653
1654 /*
1655 * 3: we may need to capture headers
1656 */
1657 s->logs.logwait &= ~LW_RESP;
1658 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->res_cap))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001659 http_capture_headers(htx, s->res_cap, sess->fe->rsp_cap);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001660
Christopher Faulet9768c262018-10-22 09:34:31 +02001661 /* Skip parsing if no content length is possible. */
Christopher Fauletc75668e2020-12-07 18:10:32 +01001662 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status >= 200 && txn->status < 300) ||
Christopher Faulete0768eb2018-10-03 16:38:02 +02001663 txn->status == 101)) {
1664 /* Either we've established an explicit tunnel, or we're
1665 * switching the protocol. In both cases, we're very unlikely
1666 * to understand the next protocols. We have to switch to tunnel
1667 * mode, so that we transfer the request and responses then let
1668 * this protocol pass unmodified. When we later implement specific
1669 * parsers for such protocols, we'll want to check the Upgrade
1670 * header which contains information about that protocol for
1671 * responses with status 101 (eg: see RFC2817 about TLS).
1672 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02001673 txn->flags |= TX_CON_WANT_TUN;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001674 }
1675
Christopher Faulet61608322018-11-23 16:23:45 +01001676 /* check for NTML authentication headers in 401 (WWW-Authenticate) and
1677 * 407 (Proxy-Authenticate) responses and set the connection to private
1678 */
1679 srv_conn = cs_conn(objt_cs(s->si[1].end));
1680 if (srv_conn) {
1681 struct ist hdr;
1682 struct http_hdr_ctx ctx;
1683
1684 if (txn->status == 401)
1685 hdr = ist("WWW-Authenticate");
1686 else if (txn->status == 407)
1687 hdr = ist("Proxy-Authenticate");
1688 else
1689 goto end;
1690
1691 ctx.blk = NULL;
1692 while (http_find_header(htx, hdr, &ctx, 0)) {
Willy Tarreauf1dcced2020-05-07 19:27:02 +02001693 /* If www-authenticate contains "Negotiate", "Nego2", or "NTLM",
1694 * possibly followed by blanks and a base64 string, the connection
1695 * is private. Since it's a mess to deal with, we only check for
1696 * values starting with "NTLM" or "Nego". Note that often multiple
1697 * headers are sent by the server there.
1698 */
1699 if ((ctx.value.len >= 4 && strncasecmp(ctx.value.ptr, "Nego", 4) == 0) ||
Willy Tarreau49a1d282020-05-07 19:10:15 +02001700 (ctx.value.len >= 4 && strncasecmp(ctx.value.ptr, "NTLM", 4) == 0)) {
Olivier Houchard250031e2019-05-29 15:01:50 +02001701 sess->flags |= SESS_FL_PREFER_LAST;
Christopher Faulet08016ab2020-07-01 16:10:06 +02001702 conn_set_owner(srv_conn, sess, NULL);
Christopher Faulet21ddc742020-07-01 15:26:14 +02001703 conn_set_private(srv_conn);
Ilya Shipitsin6b79f382020-07-23 00:32:55 +05001704 /* If it fail now, the same will be done in mux->detach() callback */
Christopher Faulet08016ab2020-07-01 16:10:06 +02001705 session_add_conn(srv_conn->owner, srv_conn, srv_conn->target);
Willy Tarreauf1dcced2020-05-07 19:27:02 +02001706 break;
Olivier Houchard250031e2019-05-29 15:01:50 +02001707 }
Christopher Faulet61608322018-11-23 16:23:45 +01001708 }
1709 }
1710
1711 end:
Christopher Faulete0768eb2018-10-03 16:38:02 +02001712 /* we want to have the response time before we start processing it */
1713 s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now);
1714
1715 /* end of job, return OK */
1716 rep->analysers &= ~an_bit;
1717 rep->analyse_exp = TICK_ETERNITY;
1718 channel_auto_close(rep);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001719 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001720 return 1;
1721
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001722 return_int_err:
Christopher Fauletcff0f732019-12-16 16:13:44 +01001723 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01001724 _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001725 if (sess->listener->counters)
1726 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01001727 if (objt_server(s->target))
1728 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.internal_errors, 1);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001729 txn->status = 500;
1730 if (!(s->flags & SF_ERR_MASK))
1731 s->flags |= SF_ERR_INTERNAL;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001732 goto return_prx_cond;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001733
1734 return_bad_res:
Olivier Houcharda798bf52019-03-08 18:52:00 +01001735 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulet47365272018-10-31 17:40:50 +01001736 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001737 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001738 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Christopher Faulet47365272018-10-31 17:40:50 +01001739 }
Olivier Houcharde3249a92019-05-03 23:01:47 +02001740 if ((s->be->retry_type & PR_RE_JUNK_REQUEST) &&
Olivier Houchardad26d8d2019-05-10 17:48:28 +02001741 (si_b->flags & SI_FL_L7_RETRY) &&
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001742 do_l7_retry(s, si_b) == 0) {
1743 DBG_TRACE_DEVEL("leaving on L7 retry",
1744 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharde3249a92019-05-03 23:01:47 +02001745 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001746 }
Christopher Faulet47365272018-10-31 17:40:50 +01001747 txn->status = 502;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001748 /* fall through */
1749
Christopher Fauletb8a53712019-12-16 11:29:38 +01001750 return_prx_cond:
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001751 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet47365272018-10-31 17:40:50 +01001752
1753 if (!(s->flags & SF_ERR_MASK))
1754 s->flags |= SF_ERR_PRXCOND;
1755 if (!(s->flags & SF_FINST_MASK))
1756 s->flags |= SF_FINST_H;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001757
1758 s->si[1].flags |= SI_FL_NOLINGER;
1759 rep->analysers &= AN_RES_FLT_END;
Christopher Faulete58c0002020-03-02 16:21:01 +01001760 s->req.analysers &= AN_REQ_FLT_END;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001761 rep->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001762 DBG_TRACE_DEVEL("leaving on error",
1763 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulet47365272018-10-31 17:40:50 +01001764 return 0;
1765
Christopher Faulete0768eb2018-10-03 16:38:02 +02001766 abort_keep_alive:
1767 /* A keep-alive request to the server failed on a network error.
1768 * The client is required to retry. We need to close without returning
1769 * any other information so that the client retries.
1770 */
1771 txn->status = 0;
1772 rep->analysers &= AN_RES_FLT_END;
1773 s->req.analysers &= AN_REQ_FLT_END;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001774 s->logs.logwait = 0;
1775 s->logs.level = 0;
1776 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001777 http_reply_and_close(s, txn->status, NULL);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001778 DBG_TRACE_DEVEL("leaving by closing K/A connection",
1779 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001780 return 0;
1781}
1782
1783/* This function performs all the processing enabled for the current response.
1784 * It normally returns 1 unless it wants to break. It relies on buffers flags,
1785 * and updates s->res.analysers. It might make sense to explode it into several
1786 * other functions. It works like process_request (see indications above).
1787 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001788int http_process_res_common(struct stream *s, struct channel *rep, int an_bit, struct proxy *px)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001789{
1790 struct session *sess = s->sess;
1791 struct http_txn *txn = s->txn;
1792 struct http_msg *msg = &txn->rsp;
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001793 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001794 struct proxy *cur_proxy;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001795 enum rule_result ret = HTTP_RULE_RES_CONT;
1796
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001797 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
1798 return 0;
Christopher Faulet9768c262018-10-22 09:34:31 +02001799
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001800 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001801
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001802 htx = htxbuf(&rep->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001803
1804 /* The stats applet needs to adjust the Connection header but we don't
1805 * apply any filter there.
1806 */
1807 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
1808 rep->analysers &= ~an_bit;
1809 rep->analyse_exp = TICK_ETERNITY;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001810 goto end;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001811 }
1812
1813 /*
1814 * We will have to evaluate the filters.
1815 * As opposed to version 1.2, now they will be evaluated in the
1816 * filters order and not in the header order. This means that
1817 * each filter has to be validated among all headers.
1818 *
1819 * Filters are tried with ->be first, then with ->fe if it is
1820 * different from ->be.
1821 *
1822 * Maybe we are in resume condiion. In this case I choose the
1823 * "struct proxy" which contains the rule list matching the resume
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05001824 * pointer. If none of these "struct proxy" match, I initialise
Christopher Faulete0768eb2018-10-03 16:38:02 +02001825 * the process with the first one.
1826 *
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05001827 * In fact, I check only correspondence between the current list
Christopher Faulete0768eb2018-10-03 16:38:02 +02001828 * pointer and the ->fe rule list. If it doesn't match, I initialize
1829 * the loop with the ->be.
1830 */
1831 if (s->current_rule_list == &sess->fe->http_res_rules)
1832 cur_proxy = sess->fe;
1833 else
1834 cur_proxy = s->be;
1835 while (1) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001836 /* evaluate http-response rules */
1837 if (ret == HTTP_RULE_RES_CONT) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001838 ret = http_res_get_intercept_rule(cur_proxy, &cur_proxy->http_res_rules, s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001839
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001840 switch (ret) {
1841 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
1842 goto return_prx_yield;
1843
1844 case HTTP_RULE_RES_CONT:
1845 case HTTP_RULE_RES_STOP: /* nothing to do */
1846 break;
1847
1848 case HTTP_RULE_RES_DENY: /* deny or tarpit */
1849 goto deny;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001850
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001851 case HTTP_RULE_RES_ABRT: /* abort request, response already sent */
1852 goto return_prx_cond;
1853
1854 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
Christopher Fauletb8a53712019-12-16 11:29:38 +01001855 goto done;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001856
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001857 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
1858 goto return_bad_res;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001859
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001860 case HTTP_RULE_RES_ERROR: /* failed with a bad request */
1861 goto return_int_err;
1862 }
1863
1864 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001865
Christopher Faulete0768eb2018-10-03 16:38:02 +02001866 /* check whether we're already working on the frontend */
1867 if (cur_proxy == sess->fe)
1868 break;
1869 cur_proxy = sess->fe;
1870 }
1871
Christopher Faulete0768eb2018-10-03 16:38:02 +02001872 /* OK that's all we can do for 1xx responses */
1873 if (unlikely(txn->status < 200 && txn->status != 101))
Christopher Fauletf2824e62018-10-01 12:12:37 +02001874 goto end;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001875
1876 /*
1877 * Now check for a server cookie.
1878 */
1879 if (s->be->cookie_name || sess->fe->capture_name || (s->be->options & PR_O_CHK_CACHE))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001880 http_manage_server_side_cookies(s, rep);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001881
1882 /*
1883 * Check for cache-control or pragma headers if required.
1884 */
1885 if ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001886 http_check_response_for_cacheability(s, rep);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001887
1888 /*
1889 * Add server cookie in the response if needed
1890 */
1891 if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) &&
1892 !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) &&
1893 (!(s->flags & SF_DIRECT) ||
1894 ((s->be->cookie_maxidle || txn->cookie_last_date) &&
1895 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
1896 (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
1897 (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
1898 (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
1899 !(s->flags & SF_IGNORE_PRST)) {
1900 /* the server is known, it's not the one the client requested, or the
1901 * cookie's last seen date needs to be refreshed. We have to
1902 * insert a set-cookie here, except if we want to insert only on POST
1903 * requests and this one isn't. Note that servers which don't have cookies
1904 * (eg: some backup servers) will return a full cookie removal request.
1905 */
1906 if (!objt_server(s->target)->cookie) {
1907 chunk_printf(&trash,
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001908 "%s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
Christopher Faulete0768eb2018-10-03 16:38:02 +02001909 s->be->cookie_name);
1910 }
1911 else {
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001912 chunk_printf(&trash, "%s=%s", s->be->cookie_name, objt_server(s->target)->cookie);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001913
1914 if (s->be->cookie_maxidle || s->be->cookie_maxlife) {
1915 /* emit last_date, which is mandatory */
1916 trash.area[trash.data++] = COOKIE_DELIM_DATE;
1917 s30tob64((date.tv_sec+3) >> 2,
1918 trash.area + trash.data);
1919 trash.data += 5;
1920
1921 if (s->be->cookie_maxlife) {
1922 /* emit first_date, which is either the original one or
1923 * the current date.
1924 */
1925 trash.area[trash.data++] = COOKIE_DELIM_DATE;
1926 s30tob64(txn->cookie_first_date ?
1927 txn->cookie_first_date >> 2 :
1928 (date.tv_sec+3) >> 2,
1929 trash.area + trash.data);
1930 trash.data += 5;
1931 }
1932 }
1933 chunk_appendf(&trash, "; path=/");
1934 }
1935
1936 if (s->be->cookie_domain)
1937 chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain);
1938
1939 if (s->be->ck_opts & PR_CK_HTTPONLY)
1940 chunk_appendf(&trash, "; HttpOnly");
1941
1942 if (s->be->ck_opts & PR_CK_SECURE)
1943 chunk_appendf(&trash, "; Secure");
1944
Christopher Faulet2f533902020-01-21 11:06:48 +01001945 if (s->be->cookie_attrs)
1946 chunk_appendf(&trash, "; %s", s->be->cookie_attrs);
1947
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001948 if (unlikely(!http_add_header(htx, ist("Set-Cookie"), ist2(trash.area, trash.data))))
Christopher Fauletb8a53712019-12-16 11:29:38 +01001949 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001950
1951 txn->flags &= ~TX_SCK_MASK;
1952 if (__objt_server(s->target)->cookie && (s->flags & SF_DIRECT))
1953 /* the server did not change, only the date was updated */
1954 txn->flags |= TX_SCK_UPDATED;
1955 else
1956 txn->flags |= TX_SCK_INSERTED;
1957
1958 /* Here, we will tell an eventual cache on the client side that we don't
1959 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
1960 * Some caches understand the correct form: 'no-cache="set-cookie"', but
1961 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
1962 */
1963 if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
1964
1965 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
1966
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001967 if (unlikely(!http_add_header(htx, ist("Cache-control"), ist("private"))))
Christopher Fauletb8a53712019-12-16 11:29:38 +01001968 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001969 }
1970 }
1971
1972 /*
1973 * Check if result will be cacheable with a cookie.
1974 * We'll block the response if security checks have caught
1975 * nasty things such as a cacheable cookie.
1976 */
1977 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
1978 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
1979 (s->be->options & PR_O_CHK_CACHE)) {
1980 /* we're in presence of a cacheable response containing
1981 * a set-cookie header. We'll block it as requested by
1982 * the 'checkcache' option, and send an alert.
1983 */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001984 ha_alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
1985 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
1986 send_log(s->be, LOG_ALERT,
1987 "Blocking cacheable cookie in response from instance %s, server %s.\n",
1988 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
Christopher Fauletb8a53712019-12-16 11:29:38 +01001989 goto deny;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001990 }
1991
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001992 end:
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01001993 /*
1994 * Evaluate after-response rules before forwarding the response. rules
1995 * from the backend are evaluated first, then one from the frontend if
1996 * it differs.
1997 */
1998 if (!http_eval_after_res_rules(s))
1999 goto return_int_err;
2000
Christopher Faulete0768eb2018-10-03 16:38:02 +02002001 /* Always enter in the body analyzer */
2002 rep->analysers &= ~AN_RES_FLT_XFER_DATA;
2003 rep->analysers |= AN_RES_HTTP_XFER_BODY;
2004
2005 /* if the user wants to log as soon as possible, without counting
2006 * bytes from the server, then this is the right moment. We have
2007 * to temporarily assign bytes_out to log what we currently have.
2008 */
2009 if (!LIST_ISEMPTY(&sess->fe->logformat) && !(s->logs.logwait & LW_BYTES)) {
2010 s->logs.t_close = s->logs.t_data; /* to get a valid end date */
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002011 s->logs.bytes_out = htx->data;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002012 s->do_log(s);
2013 s->logs.bytes_out = 0;
2014 }
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002015
Christopher Fauletb8a53712019-12-16 11:29:38 +01002016 done:
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002017 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002018 rep->analysers &= ~an_bit;
2019 rep->analyse_exp = TICK_ETERNITY;
2020 return 1;
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002021
Christopher Fauletb8a53712019-12-16 11:29:38 +01002022 deny:
Christopher Fauletb8a53712019-12-16 11:29:38 +01002023 _HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_resp, 1);
Christopher Fauleta08546b2019-12-16 16:07:34 +01002024 _HA_ATOMIC_ADD(&s->be->be_counters.denied_resp, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002025 if (sess->listener->counters)
2026 _HA_ATOMIC_ADD(&sess->listener->counters->denied_resp, 1);
Christopher Fauleta08546b2019-12-16 16:07:34 +01002027 if (objt_server(s->target))
2028 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.denied_resp, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002029 goto return_prx_err;
2030
2031 return_int_err:
2032 txn->status = 500;
2033 if (!(s->flags & SF_ERR_MASK))
2034 s->flags |= SF_ERR_INTERNAL;
Christopher Fauletcff0f732019-12-16 16:13:44 +01002035 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002036 _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1);
2037 if (objt_server(s->target))
2038 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01002039 if (objt_server(s->target))
2040 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002041 goto return_prx_err;
2042
2043 return_bad_res:
2044 txn->status = 502;
Christopher Fauleta20a6532020-02-05 10:16:41 +01002045 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
2046 if (objt_server(s->target)) {
2047 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
2048 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_RSP);
2049 }
Christopher Fauletb8a53712019-12-16 11:29:38 +01002050 /* fall through */
2051
2052 return_prx_err:
2053 http_reply_and_close(s, txn->status, http_error_message(s));
2054 /* fall through */
2055
2056 return_prx_cond:
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002057 s->logs.t_data = -1; /* was not a valid response */
2058 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002059
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002060 if (!(s->flags & SF_ERR_MASK))
2061 s->flags |= SF_ERR_PRXCOND;
2062 if (!(s->flags & SF_FINST_MASK))
2063 s->flags |= SF_FINST_H;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002064
Christopher Faulete58c0002020-03-02 16:21:01 +01002065 rep->analysers &= AN_RES_FLT_END;
2066 s->req.analysers &= AN_REQ_FLT_END;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002067 rep->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002068 DBG_TRACE_DEVEL("leaving on error",
2069 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002070 return 0;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002071
2072 return_prx_yield:
2073 channel_dont_close(rep);
2074 DBG_TRACE_DEVEL("waiting for more data",
2075 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
2076 return 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002077}
2078
2079/* This function is an analyser which forwards response body (including chunk
2080 * sizes if any). It is called as soon as we must forward, even if we forward
2081 * zero byte. The only situation where it must not be called is when we're in
2082 * tunnel mode and we want to forward till the close. It's used both to forward
2083 * remaining data and to resync after end of body. It expects the msg_state to
2084 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
2085 * read more data, or 1 once we can go on with next request or end the stream.
2086 *
2087 * It is capable of compressing response data both in content-length mode and
2088 * in chunked mode. The state machines follows different flows depending on
2089 * whether content-length and chunked modes are used, since there are no
2090 * trailers in content-length :
2091 *
2092 * chk-mode cl-mode
2093 * ,----- BODY -----.
2094 * / \
2095 * V size > 0 V chk-mode
2096 * .--> SIZE -------------> DATA -------------> CRLF
2097 * | | size == 0 | last byte |
2098 * | v final crlf v inspected |
2099 * | TRAILERS -----------> DONE |
2100 * | |
2101 * `----------------------------------------------'
2102 *
2103 * Compression only happens in the DATA state, and must be flushed in final
2104 * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding
2105 * is performed at once on final states for all bytes parsed, or when leaving
2106 * on missing data.
2107 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002108int http_response_forward_body(struct stream *s, struct channel *res, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +02002109{
2110 struct session *sess = s->sess;
2111 struct http_txn *txn = s->txn;
2112 struct http_msg *msg = &s->txn->rsp;
Christopher Faulet9768c262018-10-22 09:34:31 +02002113 struct htx *htx;
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002114 int ret;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002115
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002116 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002117
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002118 htx = htxbuf(&res->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002119
Christopher Fauletb9a92f32019-09-09 10:15:21 +02002120 if (htx->flags & HTX_FL_PARSING_ERROR)
2121 goto return_bad_res;
2122 if (htx->flags & HTX_FL_PROCESSING_ERROR)
2123 goto return_int_err;
2124
Christopher Faulete0768eb2018-10-03 16:38:02 +02002125 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Christopher Fauletf2824e62018-10-01 12:12:37 +02002126 ((res->flags & CF_SHUTW) && (res->to_forward || co_data(res)))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02002127 /* Output closed while we were sending data. We must abort and
2128 * wake the other side up.
2129 */
Christopher Faulete0768eb2018-10-03 16:38:02 +02002130 msg->msg_state = HTTP_MSG_ERROR;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002131 http_end_response(s);
2132 http_end_request(s);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002133 DBG_TRACE_DEVEL("leaving on error",
2134 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002135 return 1;
2136 }
2137
Christopher Faulet9768c262018-10-22 09:34:31 +02002138 if (msg->msg_state == HTTP_MSG_BODY)
2139 msg->msg_state = HTTP_MSG_DATA;
2140
Christopher Faulete0768eb2018-10-03 16:38:02 +02002141 /* in most states, we should abort in case of early close */
2142 channel_auto_close(res);
2143
Christopher Faulete0768eb2018-10-03 16:38:02 +02002144 if (res->to_forward) {
Christopher Faulet66af0b22019-03-22 14:54:52 +01002145 if (res->to_forward == CHN_INFINITE_FORWARD) {
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002146 if (res->flags & CF_EOI)
2147 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet66af0b22019-03-22 14:54:52 +01002148 }
2149 else {
2150 /* We can't process the buffer's contents yet */
2151 res->flags |= CF_WAKE_WRITE;
2152 goto missing_data_or_waiting;
2153 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02002154 }
2155
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002156 if (msg->msg_state >= HTTP_MSG_ENDING)
2157 goto ending;
2158
Christopher Fauletc75668e2020-12-07 18:10:32 +01002159 if ((txn->meth == HTTP_METH_CONNECT && txn->status >= 200 && txn->status < 300) || txn->status == 101 ||
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002160 (!(msg->flags & HTTP_MSGF_XFER_LEN) && !HAS_RSP_DATA_FILTERS(s))) {
2161 msg->msg_state = HTTP_MSG_ENDING;
2162 goto ending;
2163 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002164
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002165 /* Forward input data. We get it by removing all outgoing data not
2166 * forwarded yet from HTX data size. If there are some data filters, we
2167 * let them decide the amount of data to forward.
Christopher Faulet9768c262018-10-22 09:34:31 +02002168 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002169 if (HAS_RSP_DATA_FILTERS(s)) {
2170 ret = flt_http_payload(s, msg, htx->data);
2171 if (ret < 0)
2172 goto return_bad_res;
Christopher Faulet421e7692019-06-13 11:16:45 +02002173 c_adv(res, ret);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002174 }
2175 else {
Christopher Faulet421e7692019-06-13 11:16:45 +02002176 c_adv(res, htx->data - co_data(res));
Christopher Faulet66af0b22019-03-22 14:54:52 +01002177 if (msg->flags & HTTP_MSGF_XFER_LEN)
2178 channel_htx_forward_forever(res, htx);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002179 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002180
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002181 if (htx->data != co_data(res))
2182 goto missing_data_or_waiting;
2183
2184 if (!(msg->flags & HTTP_MSGF_XFER_LEN) && res->flags & CF_SHUTR) {
2185 msg->msg_state = HTTP_MSG_ENDING;
2186 goto ending;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002187 }
2188
Christopher Faulet9768c262018-10-22 09:34:31 +02002189 /* Check if the end-of-message is reached and if so, switch the message
Christopher Fauletd20fdb02019-06-13 16:43:22 +02002190 * in HTTP_MSG_ENDING state. Then if all data was marked to be
2191 * forwarded, set the state to HTTP_MSG_DONE.
Christopher Faulet9768c262018-10-22 09:34:31 +02002192 */
2193 if (htx_get_tail_type(htx) != HTX_BLK_EOM)
2194 goto missing_data_or_waiting;
2195
Christopher Fauletd20fdb02019-06-13 16:43:22 +02002196 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet9768c262018-10-22 09:34:31 +02002197
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002198 ending:
Christopher Faulet2151cdd2020-07-22 16:34:59 +02002199 res->flags &= ~CF_EXPECT_MORE; /* no more data are expected */
2200
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002201 /* other states, ENDING...TUNNEL */
2202 if (msg->msg_state >= HTTP_MSG_DONE)
2203 goto done;
Christopher Faulet9768c262018-10-22 09:34:31 +02002204
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002205 if (HAS_RSP_DATA_FILTERS(s)) {
2206 ret = flt_http_end(s, msg);
2207 if (ret <= 0) {
2208 if (!ret)
2209 goto missing_data_or_waiting;
2210 goto return_bad_res;
2211 }
2212 }
2213
Christopher Fauletc75668e2020-12-07 18:10:32 +01002214 if ((txn->meth == HTTP_METH_CONNECT && txn->status >= 200 && txn->status < 300) || txn->status == 101 ||
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002215 !(msg->flags & HTTP_MSGF_XFER_LEN)) {
2216 msg->msg_state = HTTP_MSG_TUNNEL;
2217 goto ending;
2218 }
2219 else {
2220 msg->msg_state = HTTP_MSG_DONE;
2221 res->to_forward = 0;
2222 }
2223
2224 done:
2225
2226 channel_dont_close(res);
2227
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002228 http_end_response(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002229 if (!(res->analysers & an_bit)) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002230 http_end_request(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002231 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
2232 if (res->flags & CF_SHUTW) {
2233 /* response errors are most likely due to the
2234 * client aborting the transfer. */
Christopher Faulet93e02d82019-03-08 14:18:50 +01002235 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002236 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02002237 goto return_bad_res;
2238 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002239 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002240 return 1;
2241 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002242 DBG_TRACE_DEVEL("waiting for the end of the HTTP txn",
2243 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002244 return 0;
2245
2246 missing_data_or_waiting:
2247 if (res->flags & CF_SHUTW)
Christopher Faulet93e02d82019-03-08 14:18:50 +01002248 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002249
2250 /* stop waiting for data if the input is closed before the end. If the
2251 * client side was already closed, it means that the client has aborted,
2252 * so we don't want to count this as a server abort. Otherwise it's a
2253 * server abort.
2254 */
Christopher Fauletd20fdb02019-06-13 16:43:22 +02002255 if (msg->msg_state < HTTP_MSG_ENDING && res->flags & CF_SHUTR) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02002256 if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))
Christopher Faulet93e02d82019-03-08 14:18:50 +01002257 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002258 /* If we have some pending data, we continue the processing */
Christopher Faulet93e02d82019-03-08 14:18:50 +01002259 if (htx_is_empty(htx))
2260 goto return_srv_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002261 }
2262
Christopher Faulete0768eb2018-10-03 16:38:02 +02002263 /* When TE: chunked is used, we need to get there again to parse
2264 * remaining chunks even if the server has closed, so we don't want to
Christopher Faulet9768c262018-10-22 09:34:31 +02002265 * set CF_DONTCLOSE. Similarly when there is a content-leng or if there
2266 * are filters registered on the stream, we don't want to forward a
2267 * close
Christopher Faulete0768eb2018-10-03 16:38:02 +02002268 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002269 if ((msg->flags & HTTP_MSGF_XFER_LEN) || HAS_RSP_DATA_FILTERS(s))
Christopher Faulete0768eb2018-10-03 16:38:02 +02002270 channel_dont_close(res);
2271
2272 /* We know that more data are expected, but we couldn't send more that
2273 * what we did. So we always set the CF_EXPECT_MORE flag so that the
2274 * system knows it must not set a PUSH on this first part. Interactive
2275 * modes are already handled by the stream sock layer. We must not do
2276 * this in content-length mode because it could present the MSG_MORE
2277 * flag with the last block of forwarded data, which would cause an
2278 * additional delay to be observed by the receiver.
2279 */
Christopher Faulet2151cdd2020-07-22 16:34:59 +02002280 if (HAS_RSP_DATA_FILTERS(s))
Christopher Faulete0768eb2018-10-03 16:38:02 +02002281 res->flags |= CF_EXPECT_MORE;
2282
2283 /* the stream handler will take care of timeouts and errors */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002284 DBG_TRACE_DEVEL("waiting for more data to forward",
2285 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002286 return 0;
2287
Christopher Faulet93e02d82019-03-08 14:18:50 +01002288 return_srv_abort:
2289 _HA_ATOMIC_ADD(&sess->fe->fe_counters.srv_aborts, 1);
2290 _HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01002291 if (sess->listener->counters)
2292 _HA_ATOMIC_ADD(&sess->listener->counters->srv_aborts, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002293 if (objt_server(s->target))
Christopher Fauletcff0f732019-12-16 16:13:44 +01002294 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.srv_aborts, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002295 if (!(s->flags & SF_ERR_MASK))
2296 s->flags |= SF_ERR_SRVCL;
2297 goto return_error;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002298
Christopher Faulet93e02d82019-03-08 14:18:50 +01002299 return_cli_abort:
2300 _HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
2301 _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01002302 if (sess->listener->counters)
2303 _HA_ATOMIC_ADD(&sess->listener->counters->cli_aborts, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002304 if (objt_server(s->target))
Christopher Fauletcff0f732019-12-16 16:13:44 +01002305 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.cli_aborts, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002306 if (!(s->flags & SF_ERR_MASK))
2307 s->flags |= SF_ERR_CLICL;
2308 goto return_error;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002309
Christopher Fauletb9a92f32019-09-09 10:15:21 +02002310 return_int_err:
Christopher Fauletcff0f732019-12-16 16:13:44 +01002311 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002312 _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01002313 if (sess->listener->counters)
2314 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002315 if (objt_server(s->target))
2316 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.internal_errors, 1);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02002317 if (!(s->flags & SF_ERR_MASK))
2318 s->flags |= SF_ERR_INTERNAL;
2319 goto return_error;
2320
Christopher Faulet93e02d82019-03-08 14:18:50 +01002321 return_bad_res:
2322 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
2323 if (objt_server(s->target)) {
Christopher Fauletcff0f732019-12-16 16:13:44 +01002324 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002325 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_RSP);
2326 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02002327 if (!(s->flags & SF_ERR_MASK))
Christopher Faulet93e02d82019-03-08 14:18:50 +01002328 s->flags |= SF_ERR_SRVCL;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002329 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +02002330
Christopher Faulet93e02d82019-03-08 14:18:50 +01002331 return_error:
Christopher Faulete0768eb2018-10-03 16:38:02 +02002332 /* don't send any error message as we're in the body */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002333 http_reply_and_close(s, txn->status, NULL);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002334 res->analysers &= AN_RES_FLT_END;
2335 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 +02002336 if (!(s->flags & SF_FINST_MASK))
2337 s->flags |= SF_FINST_D;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002338 DBG_TRACE_DEVEL("leaving on error",
2339 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002340 return 0;
2341}
2342
Christopher Fauletf2824e62018-10-01 12:12:37 +02002343/* Perform an HTTP redirect based on the information in <rule>. The function
Christopher Faulet99daf282018-11-28 22:58:13 +01002344 * returns zero on success, or zero in case of a, irrecoverable error such
Christopher Fauletf2824e62018-10-01 12:12:37 +02002345 * as too large a request to build a valid response.
2346 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002347int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn)
Christopher Fauletf2824e62018-10-01 12:12:37 +02002348{
Christopher Faulet99daf282018-11-28 22:58:13 +01002349 struct channel *req = &s->req;
2350 struct channel *res = &s->res;
2351 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01002352 struct htx_sl *sl;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002353 struct buffer *chunk;
Christopher Faulet99daf282018-11-28 22:58:13 +01002354 struct ist status, reason, location;
2355 unsigned int flags;
Christopher Faulet08e66462019-05-23 16:44:59 +02002356 int close = 0; /* Try to keep the connection alive byt default */
Christopher Fauletf2824e62018-10-01 12:12:37 +02002357
2358 chunk = alloc_trash_chunk();
Christopher Fauletb8a53712019-12-16 11:29:38 +01002359 if (!chunk) {
2360 if (!(s->flags & SF_ERR_MASK))
2361 s->flags |= SF_ERR_RESOURCE;
Christopher Faulet99daf282018-11-28 22:58:13 +01002362 goto fail;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002363 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002364
Christopher Faulet99daf282018-11-28 22:58:13 +01002365 /*
2366 * Create the location
2367 */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002368 htx = htxbuf(&req->buf);
Christopher Fauletf2824e62018-10-01 12:12:37 +02002369 switch(rule->type) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002370 case REDIRECT_TYPE_SCHEME: {
2371 struct http_hdr_ctx ctx;
2372 struct ist path, host;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002373
Christopher Faulet99daf282018-11-28 22:58:13 +01002374 host = ist("");
2375 ctx.blk = NULL;
2376 if (http_find_header(htx, ist("Host"), &ctx, 0))
2377 host = ctx.value;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002378
Christopher Faulet297fbb42019-05-13 14:41:27 +02002379 sl = http_get_stline(htx);
Christopher Faulet99daf282018-11-28 22:58:13 +01002380 path = http_get_path(htx_sl_req_uri(sl));
2381 /* build message using path */
Tim Duesterhused526372020-03-05 17:56:33 +01002382 if (isttest(path)) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002383 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2384 int qs = 0;
2385 while (qs < path.len) {
2386 if (*(path.ptr + qs) == '?') {
2387 path.len = qs;
2388 break;
2389 }
2390 qs++;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002391 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002392 }
2393 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002394 else
2395 path = ist("/");
Christopher Fauletf2824e62018-10-01 12:12:37 +02002396
Christopher Faulet99daf282018-11-28 22:58:13 +01002397 if (rule->rdr_str) { /* this is an old "redirect" rule */
2398 /* add scheme */
2399 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2400 goto fail;
2401 }
2402 else {
2403 /* add scheme with executing log format */
2404 chunk->data += build_logline(s, chunk->area + chunk->data,
2405 chunk->size - chunk->data,
2406 &rule->rdr_fmt);
2407 }
2408 /* add "://" + host + path */
2409 if (!chunk_memcat(chunk, "://", 3) ||
2410 !chunk_memcat(chunk, host.ptr, host.len) ||
2411 !chunk_memcat(chunk, path.ptr, path.len))
2412 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002413
Christopher Faulet99daf282018-11-28 22:58:13 +01002414 /* append a slash at the end of the location if needed and missing */
2415 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
2416 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
2417 if (chunk->data + 1 >= chunk->size)
2418 goto fail;
2419 chunk->area[chunk->data++] = '/';
2420 }
2421 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002422 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002423
Christopher Faulet99daf282018-11-28 22:58:13 +01002424 case REDIRECT_TYPE_PREFIX: {
2425 struct ist path;
2426
Christopher Faulet297fbb42019-05-13 14:41:27 +02002427 sl = http_get_stline(htx);
Christopher Faulet99daf282018-11-28 22:58:13 +01002428 path = http_get_path(htx_sl_req_uri(sl));
2429 /* build message using path */
Tim Duesterhused526372020-03-05 17:56:33 +01002430 if (isttest(path)) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002431 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2432 int qs = 0;
2433 while (qs < path.len) {
2434 if (*(path.ptr + qs) == '?') {
2435 path.len = qs;
2436 break;
2437 }
2438 qs++;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002439 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002440 }
2441 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002442 else
2443 path = ist("/");
Christopher Fauletf2824e62018-10-01 12:12:37 +02002444
Christopher Faulet99daf282018-11-28 22:58:13 +01002445 if (rule->rdr_str) { /* this is an old "redirect" rule */
2446 /* add prefix. Note that if prefix == "/", we don't want to
2447 * add anything, otherwise it makes it hard for the user to
2448 * configure a self-redirection.
2449 */
2450 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
2451 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2452 goto fail;
2453 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002454 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002455 else {
2456 /* add prefix with executing log format */
2457 chunk->data += build_logline(s, chunk->area + chunk->data,
2458 chunk->size - chunk->data,
2459 &rule->rdr_fmt);
2460 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002461
Christopher Faulet99daf282018-11-28 22:58:13 +01002462 /* add path */
2463 if (!chunk_memcat(chunk, path.ptr, path.len))
2464 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002465
Christopher Faulet99daf282018-11-28 22:58:13 +01002466 /* append a slash at the end of the location if needed and missing */
2467 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
2468 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
2469 if (chunk->data + 1 >= chunk->size)
2470 goto fail;
2471 chunk->area[chunk->data++] = '/';
2472 }
2473 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002474 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002475 case REDIRECT_TYPE_LOCATION:
2476 default:
2477 if (rule->rdr_str) { /* this is an old "redirect" rule */
2478 /* add location */
2479 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2480 goto fail;
2481 }
2482 else {
2483 /* add location with executing log format */
2484 chunk->data += build_logline(s, chunk->area + chunk->data,
2485 chunk->size - chunk->data,
2486 &rule->rdr_fmt);
2487 }
2488 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002489 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002490 location = ist2(chunk->area, chunk->data);
2491
2492 /*
2493 * Create the 30x response
2494 */
2495 switch (rule->code) {
2496 case 308:
2497 status = ist("308");
2498 reason = ist("Permanent Redirect");
2499 break;
2500 case 307:
2501 status = ist("307");
2502 reason = ist("Temporary Redirect");
2503 break;
2504 case 303:
2505 status = ist("303");
2506 reason = ist("See Other");
2507 break;
2508 case 301:
2509 status = ist("301");
2510 reason = ist("Moved Permanently");
2511 break;
2512 case 302:
2513 default:
2514 status = ist("302");
2515 reason = ist("Found");
2516 break;
2517 }
2518
Christopher Faulet08e66462019-05-23 16:44:59 +02002519 if (!(txn->req.flags & HTTP_MSGF_BODYLESS) && txn->req.msg_state != HTTP_MSG_DONE)
2520 close = 1;
2521
Christopher Faulet99daf282018-11-28 22:58:13 +01002522 htx = htx_from_buf(&res->buf);
Kevin Zhu96b36392020-01-07 09:42:55 +01002523 /* Trim any possible response */
2524 channel_htx_truncate(&s->res, htx);
Christopher Faulet99daf282018-11-28 22:58:13 +01002525 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
2526 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), status, reason);
2527 if (!sl)
2528 goto fail;
2529 sl->info.res.status = rule->code;
2530 s->txn->status = rule->code;
2531
Christopher Faulet08e66462019-05-23 16:44:59 +02002532 if (close && !htx_add_header(htx, ist("Connection"), ist("close")))
2533 goto fail;
2534
2535 if (!htx_add_header(htx, ist("Content-length"), ist("0")) ||
Christopher Faulet99daf282018-11-28 22:58:13 +01002536 !htx_add_header(htx, ist("Location"), location))
2537 goto fail;
2538
2539 if (rule->code == 302 || rule->code == 303 || rule->code == 307) {
2540 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")))
2541 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002542 }
2543
2544 if (rule->cookie_len) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002545 if (!htx_add_header(htx, ist("Set-Cookie"), ist2(rule->cookie_str, rule->cookie_len)))
2546 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002547 }
2548
Christopher Faulet99daf282018-11-28 22:58:13 +01002549 if (!htx_add_endof(htx, HTX_BLK_EOH) || !htx_add_endof(htx, HTX_BLK_EOM))
2550 goto fail;
2551
Kevin Zhu96b36392020-01-07 09:42:55 +01002552 htx_to_buf(htx, &res->buf);
Christopher Fauleta72a7e42020-01-28 09:28:11 +01002553 if (!http_forward_proxy_resp(s, 1))
2554 goto fail;
Christopher Faulet99daf282018-11-28 22:58:13 +01002555
Christopher Faulet60b33a52020-01-28 09:18:10 +01002556 if (rule->flags & REDIRECT_FLAG_FROM_REQ) {
2557 /* let's log the request time */
2558 s->logs.tv_request = now;
2559 req->analysers &= AN_REQ_FLT_END;
Christopher Faulet99daf282018-11-28 22:58:13 +01002560
Christopher Faulet60b33a52020-01-28 09:18:10 +01002561 if (s->sess->fe == s->be) /* report it if the request was intercepted by the frontend */
2562 _HA_ATOMIC_ADD(&s->sess->fe->fe_counters.intercepted_req, 1);
2563 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002564
2565 if (!(s->flags & SF_ERR_MASK))
2566 s->flags |= SF_ERR_LOCAL;
2567 if (!(s->flags & SF_FINST_MASK))
Christopher Faulet60b33a52020-01-28 09:18:10 +01002568 s->flags |= ((rule->flags & REDIRECT_FLAG_FROM_REQ) ? SF_FINST_R : SF_FINST_H);
Christopher Fauletf2824e62018-10-01 12:12:37 +02002569
Christopher Faulet99daf282018-11-28 22:58:13 +01002570 free_trash_chunk(chunk);
2571 return 1;
2572
2573 fail:
2574 /* If an error occurred, remove the incomplete HTTP response from the
2575 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01002576 channel_htx_truncate(res, htxbuf(&res->buf));
Christopher Fauletf2824e62018-10-01 12:12:37 +02002577 free_trash_chunk(chunk);
Christopher Faulet99daf282018-11-28 22:58:13 +01002578 return 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002579}
2580
Christopher Faulet92d34fe2019-12-17 09:20:34 +01002581/* Replace all headers matching the name <name>. The header value is replaced if
2582 * it matches the regex <re>. <str> is used for the replacement. If <full> is
2583 * set to 1, the full-line is matched and replaced. Otherwise, comma-separated
2584 * values are evaluated one by one. It returns 0 on success and -1 on error.
2585 */
2586int http_replace_hdrs(struct stream* s, struct htx *htx, struct ist name,
2587 const char *str, struct my_regex *re, int full)
Christopher Faulet72333522018-10-24 11:25:02 +02002588{
2589 struct http_hdr_ctx ctx;
2590 struct buffer *output = get_trash_chunk();
2591
Christopher Faulet72333522018-10-24 11:25:02 +02002592 ctx.blk = NULL;
Christopher Faulet92d34fe2019-12-17 09:20:34 +01002593 while (http_find_header(htx, name, &ctx, full)) {
Christopher Faulet72333522018-10-24 11:25:02 +02002594 if (!regex_exec_match2(re, ctx.value.ptr, ctx.value.len, MAX_MATCH, pmatch, 0))
2595 continue;
2596
2597 output->data = exp_replace(output->area, output->size, ctx.value.ptr, str, pmatch);
2598 if (output->data == -1)
2599 return -1;
2600 if (!http_replace_header_value(htx, &ctx, ist2(output->area, output->data)))
2601 return -1;
2602 }
2603 return 0;
2604}
2605
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002606/* This function executes one of the set-{method,path,query,uri} actions. It
2607 * takes the string from the variable 'replace' with length 'len', then modifies
2608 * the relevant part of the request line accordingly. Then it updates various
2609 * pointers to the next elements which were moved, and the total buffer length.
2610 * It finds the action to be performed in p[2], previously filled by function
2611 * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal
2612 * error, though this can be revisited when this code is finally exploited.
2613 *
2614 * 'action' can be '0' to replace method, '1' to replace path, '2' to replace
Christopher Faulet312294f2020-09-02 17:17:44 +02002615 * query string, 3 to replace uri or 4 to replace the path+query.
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002616 *
2617 * In query string case, the mark question '?' must be set at the start of the
2618 * string by the caller, event if the replacement query string is empty.
2619 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002620int http_req_replace_stline(int action, const char *replace, int len,
2621 struct proxy *px, struct stream *s)
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002622{
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002623 struct htx *htx = htxbuf(&s->req.buf);
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002624
2625 switch (action) {
2626 case 0: // method
2627 if (!http_replace_req_meth(htx, ist2(replace, len)))
2628 return -1;
2629 break;
2630
2631 case 1: // path
Christopher Fauletb8ce5052020-08-31 16:11:57 +02002632 if (!http_replace_req_path(htx, ist2(replace, len), 0))
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002633 return -1;
2634 break;
2635
2636 case 2: // query
2637 if (!http_replace_req_query(htx, ist2(replace, len)))
2638 return -1;
2639 break;
2640
2641 case 3: // uri
2642 if (!http_replace_req_uri(htx, ist2(replace, len)))
2643 return -1;
2644 break;
2645
Christopher Faulet312294f2020-09-02 17:17:44 +02002646 case 4: // path + query
2647 if (!http_replace_req_path(htx, ist2(replace, len), 1))
2648 return -1;
2649 break;
2650
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002651 default:
2652 return -1;
2653 }
2654 return 0;
2655}
2656
2657/* This function replace the HTTP status code and the associated message. The
Christopher Faulete00d06c2019-12-16 17:18:42 +01002658 * variable <status> contains the new status code. This function never fails. It
2659 * returns 0 in case of success, -1 in case of internal error.
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002660 */
Christopher Faulet96bff762019-12-17 13:46:18 +01002661int http_res_set_status(unsigned int status, struct ist reason, struct stream *s)
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002662{
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002663 struct htx *htx = htxbuf(&s->res.buf);
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002664 char *res;
2665
2666 chunk_reset(&trash);
2667 res = ultoa_o(status, trash.area, trash.size);
2668 trash.data = res - trash.area;
2669
2670 /* Do we have a custom reason format string? */
Tim Duesterhuse296d3e2020-03-05 17:56:31 +01002671 if (!isttest(reason)) {
Christopher Faulet96bff762019-12-17 13:46:18 +01002672 const char *str = http_get_reason(status);
2673 reason = ist2(str, strlen(str));
2674 }
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002675
Christopher Fauletbde2c4c2020-08-31 16:43:34 +02002676 if (!http_replace_res_status(htx, ist2(trash.area, trash.data), reason))
Christopher Faulete00d06c2019-12-16 17:18:42 +01002677 return -1;
2678 return 0;
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002679}
2680
Christopher Faulet3e964192018-10-24 11:39:23 +02002681/* Executes the http-request rules <rules> for stream <s>, proxy <px> and
2682 * transaction <txn>. Returns the verdict of the first rule that prevents
2683 * further processing of the request (auth, deny, ...), and defaults to
2684 * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or
2685 * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT
2686 * on txn->flags if it encounters a tarpit rule. If <deny_status> is not NULL
2687 * and a deny/tarpit rule is matched, it will be filled with this rule's deny
2688 * status.
2689 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002690static enum rule_result http_req_get_intercept_rule(struct proxy *px, struct list *rules,
Christopher Fauletb58f62b2020-01-13 16:40:13 +01002691 struct stream *s)
Christopher Faulet3e964192018-10-24 11:39:23 +02002692{
2693 struct session *sess = strm_sess(s);
2694 struct http_txn *txn = s->txn;
Christopher Faulet3e964192018-10-24 11:39:23 +02002695 struct act_rule *rule;
Christopher Faulet3e964192018-10-24 11:39:23 +02002696 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002697 int act_opts = 0;
Christopher Faulet3e964192018-10-24 11:39:23 +02002698
Christopher Faulet3e964192018-10-24 11:39:23 +02002699 /* If "the current_rule_list" match the executed rule list, we are in
2700 * resume condition. If a resume is needed it is always in the action
2701 * and never in the ACL or converters. In this case, we initialise the
2702 * current rule, and go to the action execution point.
2703 */
2704 if (s->current_rule) {
2705 rule = s->current_rule;
2706 s->current_rule = NULL;
2707 if (s->current_rule_list == rules)
2708 goto resume_execution;
2709 }
2710 s->current_rule_list = rules;
2711
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002712 /* start the ruleset evaluation in strict mode */
2713 txn->req.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01002714
Christopher Faulet3e964192018-10-24 11:39:23 +02002715 list_for_each_entry(rule, rules, list) {
2716 /* check optional condition */
2717 if (rule->cond) {
2718 int ret;
2719
2720 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
2721 ret = acl_pass(ret);
2722
2723 if (rule->cond->pol == ACL_COND_UNLESS)
2724 ret = !ret;
2725
2726 if (!ret) /* condition not matched */
2727 continue;
2728 }
2729
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002730 act_opts |= ACT_OPT_FIRST;
Christopher Faulet3e964192018-10-24 11:39:23 +02002731 resume_execution:
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002732 /* Always call the action function if defined */
2733 if (rule->action_ptr) {
2734 if ((s->req.flags & CF_READ_ERROR) ||
2735 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
2736 (px->options & PR_O_ABRT_CLOSE)))
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002737 act_opts |= ACT_OPT_FINAL;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002738
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002739 switch (rule->action_ptr(rule, px, sess, s, act_opts)) {
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002740 case ACT_RET_CONT:
2741 break;
2742 case ACT_RET_STOP:
2743 rule_ret = HTTP_RULE_RES_STOP;
2744 goto end;
2745 case ACT_RET_YIELD:
2746 s->current_rule = rule;
2747 rule_ret = HTTP_RULE_RES_YIELD;
2748 goto end;
2749 case ACT_RET_ERR:
2750 rule_ret = HTTP_RULE_RES_ERROR;
2751 goto end;
2752 case ACT_RET_DONE:
2753 rule_ret = HTTP_RULE_RES_DONE;
2754 goto end;
2755 case ACT_RET_DENY:
Christopher Fauletb58f62b2020-01-13 16:40:13 +01002756 if (txn->status == -1)
2757 txn->status = 403;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002758 rule_ret = HTTP_RULE_RES_DENY;
2759 goto end;
2760 case ACT_RET_ABRT:
2761 rule_ret = HTTP_RULE_RES_ABRT;
2762 goto end;
2763 case ACT_RET_INV:
2764 rule_ret = HTTP_RULE_RES_BADREQ;
2765 goto end;
2766 }
2767 continue; /* eval the next rule */
2768 }
2769
2770 /* If not action function defined, check for known actions */
Christopher Faulet3e964192018-10-24 11:39:23 +02002771 switch (rule->action) {
2772 case ACT_ACTION_ALLOW:
2773 rule_ret = HTTP_RULE_RES_STOP;
2774 goto end;
2775
2776 case ACT_ACTION_DENY:
Christopher Faulet5cb513a2020-05-13 17:56:56 +02002777 txn->status = rule->arg.http_reply->status;
2778 txn->http_reply = rule->arg.http_reply;
Christopher Faulet3e964192018-10-24 11:39:23 +02002779 rule_ret = HTTP_RULE_RES_DENY;
2780 goto end;
2781
2782 case ACT_HTTP_REQ_TARPIT:
2783 txn->flags |= TX_CLTARPIT;
Christopher Faulet5cb513a2020-05-13 17:56:56 +02002784 txn->status = rule->arg.http_reply->status;
2785 txn->http_reply = rule->arg.http_reply;
Christopher Faulet3e964192018-10-24 11:39:23 +02002786 rule_ret = HTTP_RULE_RES_DENY;
2787 goto end;
2788
Christopher Faulet3e964192018-10-24 11:39:23 +02002789 case ACT_HTTP_REDIR:
Christopher Faulet90d22a82020-03-06 11:18:39 +01002790 rule_ret = HTTP_RULE_RES_ABRT;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002791 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
Christopher Faulet3a26bee2019-12-16 12:47:40 +01002792 rule_ret = HTTP_RULE_RES_ERROR;
Christopher Faulet3e964192018-10-24 11:39:23 +02002793 goto end;
2794
2795 case ACT_HTTP_SET_NICE:
Christopher Faulet96bff762019-12-17 13:46:18 +01002796 s->task->nice = rule->arg.http.i;
Christopher Faulet3e964192018-10-24 11:39:23 +02002797 break;
2798
2799 case ACT_HTTP_SET_TOS:
Christopher Faulet96bff762019-12-17 13:46:18 +01002800 conn_set_tos(objt_conn(sess->origin), rule->arg.http.i);
Christopher Faulet3e964192018-10-24 11:39:23 +02002801 break;
2802
2803 case ACT_HTTP_SET_MARK:
Christopher Faulet96bff762019-12-17 13:46:18 +01002804 conn_set_mark(objt_conn(sess->origin), rule->arg.http.i);
Christopher Faulet3e964192018-10-24 11:39:23 +02002805 break;
2806
2807 case ACT_HTTP_SET_LOGL:
Christopher Faulet96bff762019-12-17 13:46:18 +01002808 s->logs.level = rule->arg.http.i;
Christopher Faulet3e964192018-10-24 11:39:23 +02002809 break;
2810
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002811 /* other flags exists, but normally, they never be matched. */
Christopher Faulet3e964192018-10-24 11:39:23 +02002812 default:
2813 break;
2814 }
2815 }
2816
2817 end:
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002818 /* if the ruleset evaluation is finished reset the strict mode */
Christopher Faulet46f95542019-12-20 10:07:22 +01002819 if (rule_ret != HTTP_RULE_RES_YIELD)
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002820 txn->req.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01002821
Christopher Faulet3e964192018-10-24 11:39:23 +02002822 /* we reached the end of the rules, nothing to report */
2823 return rule_ret;
2824}
2825
2826/* Executes the http-response rules <rules> for stream <s> and proxy <px>. It
2827 * returns one of 5 possible statuses: HTTP_RULE_RES_CONT, HTTP_RULE_RES_STOP,
2828 * HTTP_RULE_RES_DONE, HTTP_RULE_RES_YIELD, or HTTP_RULE_RES_BADREQ. If *CONT
2829 * is returned, the process can continue the evaluation of next rule list. If
2830 * *STOP or *DONE is returned, the process must stop the evaluation. If *BADREQ
2831 * is returned, it means the operation could not be processed and a server error
Christopher Fauleta53abad2020-05-13 08:12:22 +02002832 * must be returned. If *YIELD is returned, the caller must call again the
2833 * function with the same context.
Christopher Faulet3e964192018-10-24 11:39:23 +02002834 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002835static enum rule_result http_res_get_intercept_rule(struct proxy *px, struct list *rules,
2836 struct stream *s)
Christopher Faulet3e964192018-10-24 11:39:23 +02002837{
2838 struct session *sess = strm_sess(s);
2839 struct http_txn *txn = s->txn;
Christopher Faulet3e964192018-10-24 11:39:23 +02002840 struct act_rule *rule;
Christopher Faulet3e964192018-10-24 11:39:23 +02002841 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002842 int act_opts = 0;
Christopher Faulet3e964192018-10-24 11:39:23 +02002843
Christopher Faulet3e964192018-10-24 11:39:23 +02002844 /* If "the current_rule_list" match the executed rule list, we are in
2845 * resume condition. If a resume is needed it is always in the action
2846 * and never in the ACL or converters. In this case, we initialise the
2847 * current rule, and go to the action execution point.
2848 */
2849 if (s->current_rule) {
2850 rule = s->current_rule;
2851 s->current_rule = NULL;
2852 if (s->current_rule_list == rules)
2853 goto resume_execution;
2854 }
2855 s->current_rule_list = rules;
2856
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002857 /* start the ruleset evaluation in strict mode */
2858 txn->rsp.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01002859
Christopher Faulet3e964192018-10-24 11:39:23 +02002860 list_for_each_entry(rule, rules, list) {
2861 /* check optional condition */
2862 if (rule->cond) {
2863 int ret;
2864
2865 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
2866 ret = acl_pass(ret);
2867
2868 if (rule->cond->pol == ACL_COND_UNLESS)
2869 ret = !ret;
2870
2871 if (!ret) /* condition not matched */
2872 continue;
2873 }
2874
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002875 act_opts |= ACT_OPT_FIRST;
Christopher Faulet3e964192018-10-24 11:39:23 +02002876resume_execution:
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002877
2878 /* Always call the action function if defined */
2879 if (rule->action_ptr) {
2880 if ((s->req.flags & CF_READ_ERROR) ||
2881 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
2882 (px->options & PR_O_ABRT_CLOSE)))
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002883 act_opts |= ACT_OPT_FINAL;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002884
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002885 switch (rule->action_ptr(rule, px, sess, s, act_opts)) {
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002886 case ACT_RET_CONT:
2887 break;
2888 case ACT_RET_STOP:
2889 rule_ret = HTTP_RULE_RES_STOP;
2890 goto end;
2891 case ACT_RET_YIELD:
2892 s->current_rule = rule;
2893 rule_ret = HTTP_RULE_RES_YIELD;
2894 goto end;
2895 case ACT_RET_ERR:
2896 rule_ret = HTTP_RULE_RES_ERROR;
2897 goto end;
2898 case ACT_RET_DONE:
2899 rule_ret = HTTP_RULE_RES_DONE;
2900 goto end;
2901 case ACT_RET_DENY:
Christopher Fauletb58f62b2020-01-13 16:40:13 +01002902 if (txn->status == -1)
2903 txn->status = 502;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002904 rule_ret = HTTP_RULE_RES_DENY;
2905 goto end;
2906 case ACT_RET_ABRT:
2907 rule_ret = HTTP_RULE_RES_ABRT;
2908 goto end;
2909 case ACT_RET_INV:
2910 rule_ret = HTTP_RULE_RES_BADREQ;
2911 goto end;
2912 }
2913 continue; /* eval the next rule */
2914 }
2915
2916 /* If not action function defined, check for known actions */
Christopher Faulet3e964192018-10-24 11:39:23 +02002917 switch (rule->action) {
2918 case ACT_ACTION_ALLOW:
2919 rule_ret = HTTP_RULE_RES_STOP; /* "allow" rules are OK */
2920 goto end;
2921
2922 case ACT_ACTION_DENY:
Christopher Faulet5cb513a2020-05-13 17:56:56 +02002923 txn->status = rule->arg.http_reply->status;
2924 txn->http_reply = rule->arg.http_reply;
Christopher Faulet3a26bee2019-12-16 12:47:40 +01002925 rule_ret = HTTP_RULE_RES_DENY;
Christopher Faulet3e964192018-10-24 11:39:23 +02002926 goto end;
2927
2928 case ACT_HTTP_SET_NICE:
Christopher Faulet96bff762019-12-17 13:46:18 +01002929 s->task->nice = rule->arg.http.i;
Christopher Faulet3e964192018-10-24 11:39:23 +02002930 break;
2931
2932 case ACT_HTTP_SET_TOS:
Christopher Faulet96bff762019-12-17 13:46:18 +01002933 conn_set_tos(objt_conn(sess->origin), rule->arg.http.i);
Christopher Faulet3e964192018-10-24 11:39:23 +02002934 break;
2935
2936 case ACT_HTTP_SET_MARK:
Christopher Faulet96bff762019-12-17 13:46:18 +01002937 conn_set_mark(objt_conn(sess->origin), rule->arg.http.i);
Christopher Faulet3e964192018-10-24 11:39:23 +02002938 break;
2939
2940 case ACT_HTTP_SET_LOGL:
Christopher Faulet96bff762019-12-17 13:46:18 +01002941 s->logs.level = rule->arg.http.i;
Christopher Faulet3e964192018-10-24 11:39:23 +02002942 break;
2943
Christopher Faulet3e964192018-10-24 11:39:23 +02002944 case ACT_HTTP_REDIR:
Christopher Faulet49c2a702020-03-06 15:44:37 +01002945 rule_ret = HTTP_RULE_RES_ABRT;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002946 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
Christopher Faulet3a26bee2019-12-16 12:47:40 +01002947 rule_ret = HTTP_RULE_RES_ERROR;
Christopher Faulet3e964192018-10-24 11:39:23 +02002948 goto end;
2949
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002950 /* other flags exists, but normally, they never be matched. */
Christopher Faulet3e964192018-10-24 11:39:23 +02002951 default:
2952 break;
2953 }
2954 }
2955
2956 end:
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002957 /* if the ruleset evaluation is finished reset the strict mode */
Christopher Faulet46f95542019-12-20 10:07:22 +01002958 if (rule_ret != HTTP_RULE_RES_YIELD)
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002959 txn->rsp.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01002960
Christopher Faulet3e964192018-10-24 11:39:23 +02002961 /* we reached the end of the rules, nothing to report */
2962 return rule_ret;
2963}
2964
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002965/* Executes backend and frontend http-after-response rules for the stream <s>,
2966 * in that order. it return 1 on success and 0 on error. It is the caller
2967 * responsibility to catch error or ignore it. If it catches it, this function
2968 * may be called a second time, for the internal error.
2969 */
2970int http_eval_after_res_rules(struct stream *s)
2971{
2972 struct session *sess = s->sess;
2973 enum rule_result ret = HTTP_RULE_RES_CONT;
2974
Christopher Faulet507479b2020-05-15 12:29:46 +02002975 /* Eval after-response ruleset only if the reply is not const */
2976 if (s->txn->flags & TX_CONST_REPLY)
2977 goto end;
2978
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002979 /* prune the request variables if not already done and swap to the response variables. */
2980 if (s->vars_reqres.scope != SCOPE_RES) {
2981 if (!LIST_ISEMPTY(&s->vars_reqres.head))
2982 vars_prune(&s->vars_reqres, s->sess, s);
2983 vars_init(&s->vars_reqres, SCOPE_RES);
2984 }
2985
2986 ret = http_res_get_intercept_rule(s->be, &s->be->http_after_res_rules, s);
2987 if ((ret == HTTP_RULE_RES_CONT || ret == HTTP_RULE_RES_STOP) && sess->fe != s->be)
2988 ret = http_res_get_intercept_rule(sess->fe, &sess->fe->http_after_res_rules, s);
2989
Christopher Faulet507479b2020-05-15 12:29:46 +02002990 end:
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002991 /* All other codes than CONTINUE, STOP or DONE are forbidden */
2992 return (ret == HTTP_RULE_RES_CONT || ret == HTTP_RULE_RES_STOP || ret == HTTP_RULE_RES_DONE);
2993}
2994
Christopher Fauletfcda7c62018-10-24 11:56:22 +02002995/*
2996 * Manage client-side cookie. It can impact performance by about 2% so it is
2997 * desirable to call it only when needed. This code is quite complex because
2998 * of the multiple very crappy and ambiguous syntaxes we have to support. it
2999 * highly recommended not to touch this part without a good reason !
3000 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003001static void http_manage_client_side_cookies(struct stream *s, struct channel *req)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003002{
3003 struct session *sess = s->sess;
3004 struct http_txn *txn = s->txn;
3005 struct htx *htx;
3006 struct http_hdr_ctx ctx;
3007 char *hdr_beg, *hdr_end, *del_from;
3008 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
3009 int preserve_hdr;
3010
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003011 htx = htxbuf(&req->buf);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003012 ctx.blk = NULL;
3013 while (http_find_header(htx, ist("Cookie"), &ctx, 1)) {
Olivier Houchardf0f42382019-07-22 17:43:46 +02003014 int is_first = 1;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003015 del_from = NULL; /* nothing to be deleted */
3016 preserve_hdr = 0; /* assume we may kill the whole header */
3017
3018 /* Now look for cookies. Conforming to RFC2109, we have to support
3019 * attributes whose name begin with a '$', and associate them with
3020 * the right cookie, if we want to delete this cookie.
3021 * So there are 3 cases for each cookie read :
3022 * 1) it's a special attribute, beginning with a '$' : ignore it.
3023 * 2) it's a server id cookie that we *MAY* want to delete : save
3024 * some pointers on it (last semi-colon, beginning of cookie...)
3025 * 3) it's an application cookie : we *MAY* have to delete a previous
3026 * "special" cookie.
3027 * At the end of loop, if a "special" cookie remains, we may have to
3028 * remove it. If no application cookie persists in the header, we
3029 * *MUST* delete it.
3030 *
3031 * Note: RFC2965 is unclear about the processing of spaces around
3032 * the equal sign in the ATTR=VALUE form. A careful inspection of
3033 * the RFC explicitly allows spaces before it, and not within the
3034 * tokens (attrs or values). An inspection of RFC2109 allows that
3035 * too but section 10.1.3 lets one think that spaces may be allowed
3036 * after the equal sign too, resulting in some (rare) buggy
3037 * implementations trying to do that. So let's do what servers do.
3038 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
3039 * allowed quoted strings in values, with any possible character
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003040 * after a backslash, including control chars and delimiters, which
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003041 * causes parsing to become ambiguous. Browsers also allow spaces
3042 * within values even without quotes.
3043 *
3044 * We have to keep multiple pointers in order to support cookie
3045 * removal at the beginning, middle or end of header without
3046 * corrupting the header. All of these headers are valid :
3047 *
3048 * hdr_beg hdr_end
3049 * | |
3050 * v |
3051 * NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3 |
3052 * NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3 v
3053 * NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3
3054 * | | | | | | |
3055 * | | | | | | |
3056 * | | | | | | +--> next
3057 * | | | | | +----> val_end
3058 * | | | | +-----------> val_beg
3059 * | | | +--------------> equal
3060 * | | +----------------> att_end
3061 * | +---------------------> att_beg
3062 * +--------------------------> prev
3063 *
3064 */
3065 hdr_beg = ctx.value.ptr;
3066 hdr_end = hdr_beg + ctx.value.len;
3067 for (prev = hdr_beg; prev < hdr_end; prev = next) {
3068 /* Iterate through all cookies on this line */
3069
3070 /* find att_beg */
3071 att_beg = prev;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003072 if (!is_first)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003073 att_beg++;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003074 is_first = 0;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003075
3076 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
3077 att_beg++;
3078
3079 /* find att_end : this is the first character after the last non
3080 * space before the equal. It may be equal to hdr_end.
3081 */
3082 equal = att_end = att_beg;
3083 while (equal < hdr_end) {
3084 if (*equal == '=' || *equal == ',' || *equal == ';')
3085 break;
3086 if (HTTP_IS_SPHT(*equal++))
3087 continue;
3088 att_end = equal;
3089 }
3090
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003091 /* here, <equal> points to '=', a delimiter or the end. <att_end>
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003092 * is between <att_beg> and <equal>, both may be identical.
3093 */
3094 /* look for end of cookie if there is an equal sign */
3095 if (equal < hdr_end && *equal == '=') {
3096 /* look for the beginning of the value */
3097 val_beg = equal + 1;
3098 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
3099 val_beg++;
3100
3101 /* find the end of the value, respecting quotes */
3102 next = http_find_cookie_value_end(val_beg, hdr_end);
3103
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003104 /* make val_end point to the first white space or delimiter after the value */
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003105 val_end = next;
3106 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
3107 val_end--;
3108 }
3109 else
3110 val_beg = val_end = next = equal;
3111
3112 /* We have nothing to do with attributes beginning with
3113 * '$'. However, they will automatically be removed if a
3114 * header before them is removed, since they're supposed
3115 * to be linked together.
3116 */
3117 if (*att_beg == '$')
3118 continue;
3119
3120 /* Ignore cookies with no equal sign */
3121 if (equal == next) {
3122 /* This is not our cookie, so we must preserve it. But if we already
3123 * scheduled another cookie for removal, we cannot remove the
3124 * complete header, but we can remove the previous block itself.
3125 */
3126 preserve_hdr = 1;
3127 if (del_from != NULL) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003128 int delta = http_del_hdr_value(hdr_beg, hdr_end, &del_from, prev);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003129 val_end += delta;
3130 next += delta;
3131 hdr_end += delta;
3132 prev = del_from;
3133 del_from = NULL;
3134 }
3135 continue;
3136 }
3137
3138 /* if there are spaces around the equal sign, we need to
3139 * strip them otherwise we'll get trouble for cookie captures,
3140 * or even for rewrites. Since this happens extremely rarely,
3141 * it does not hurt performance.
3142 */
3143 if (unlikely(att_end != equal || val_beg > equal + 1)) {
3144 int stripped_before = 0;
3145 int stripped_after = 0;
3146
3147 if (att_end != equal) {
3148 memmove(att_end, equal, hdr_end - equal);
3149 stripped_before = (att_end - equal);
3150 equal += stripped_before;
3151 val_beg += stripped_before;
3152 }
3153
3154 if (val_beg > equal + 1) {
3155 memmove(equal + 1, val_beg, hdr_end + stripped_before - val_beg);
3156 stripped_after = (equal + 1) - val_beg;
3157 val_beg += stripped_after;
3158 stripped_before += stripped_after;
3159 }
3160
3161 val_end += stripped_before;
3162 next += stripped_before;
3163 hdr_end += stripped_before;
3164 }
3165 /* now everything is as on the diagram above */
3166
3167 /* First, let's see if we want to capture this cookie. We check
3168 * that we don't already have a client side cookie, because we
3169 * can only capture one. Also as an optimisation, we ignore
3170 * cookies shorter than the declared name.
3171 */
3172 if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL &&
3173 (val_end - att_beg >= sess->fe->capture_namelen) &&
3174 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
3175 int log_len = val_end - att_beg;
3176
3177 if ((txn->cli_cookie = pool_alloc(pool_head_capture)) == NULL) {
3178 ha_alert("HTTP logging : out of memory.\n");
3179 } else {
3180 if (log_len > sess->fe->capture_len)
3181 log_len = sess->fe->capture_len;
3182 memcpy(txn->cli_cookie, att_beg, log_len);
3183 txn->cli_cookie[log_len] = 0;
3184 }
3185 }
3186
3187 /* Persistence cookies in passive, rewrite or insert mode have the
3188 * following form :
3189 *
3190 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
3191 *
3192 * For cookies in prefix mode, the form is :
3193 *
3194 * Cookie: NAME=SRV~VALUE
3195 */
3196 if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
3197 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
3198 struct server *srv = s->be->srv;
3199 char *delim;
3200
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003201 /* if we're in cookie prefix mode, we'll search the delimiter so that we
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003202 * have the server ID between val_beg and delim, and the original cookie between
3203 * delim+1 and val_end. Otherwise, delim==val_end :
3204 *
3205 * hdr_beg
3206 * |
3207 * v
3208 * NAME=SRV; # in all but prefix modes
3209 * NAME=SRV~OPAQUE ; # in prefix mode
3210 * || || | |+-> next
3211 * || || | +--> val_end
3212 * || || +---------> delim
3213 * || |+------------> val_beg
3214 * || +-------------> att_end = equal
3215 * |+-----------------> att_beg
3216 * +------------------> prev
3217 *
3218 */
3219 if (s->be->ck_opts & PR_CK_PFX) {
3220 for (delim = val_beg; delim < val_end; delim++)
3221 if (*delim == COOKIE_DELIM)
3222 break;
3223 }
3224 else {
3225 char *vbar1;
3226 delim = val_end;
3227 /* Now check if the cookie contains a date field, which would
3228 * appear after a vertical bar ('|') just after the server name
3229 * and before the delimiter.
3230 */
3231 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
3232 if (vbar1) {
3233 /* OK, so left of the bar is the server's cookie and
3234 * right is the last seen date. It is a base64 encoded
3235 * 30-bit value representing the UNIX date since the
3236 * epoch in 4-second quantities.
3237 */
3238 int val;
3239 delim = vbar1++;
3240 if (val_end - vbar1 >= 5) {
3241 val = b64tos30(vbar1);
3242 if (val > 0)
3243 txn->cookie_last_date = val << 2;
3244 }
3245 /* look for a second vertical bar */
3246 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
3247 if (vbar1 && (val_end - vbar1 > 5)) {
3248 val = b64tos30(vbar1 + 1);
3249 if (val > 0)
3250 txn->cookie_first_date = val << 2;
3251 }
3252 }
3253 }
3254
3255 /* if the cookie has an expiration date and the proxy wants to check
3256 * it, then we do that now. We first check if the cookie is too old,
3257 * then only if it has expired. We detect strict overflow because the
3258 * time resolution here is not great (4 seconds). Cookies with dates
3259 * in the future are ignored if their offset is beyond one day. This
3260 * allows an admin to fix timezone issues without expiring everyone
3261 * and at the same time avoids keeping unwanted side effects for too
3262 * long.
3263 */
3264 if (txn->cookie_first_date && s->be->cookie_maxlife &&
3265 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) ||
3266 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
3267 txn->flags &= ~TX_CK_MASK;
3268 txn->flags |= TX_CK_OLD;
3269 delim = val_beg; // let's pretend we have not found the cookie
3270 txn->cookie_first_date = 0;
3271 txn->cookie_last_date = 0;
3272 }
3273 else if (txn->cookie_last_date && s->be->cookie_maxidle &&
3274 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) ||
3275 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
3276 txn->flags &= ~TX_CK_MASK;
3277 txn->flags |= TX_CK_EXPIRED;
3278 delim = val_beg; // let's pretend we have not found the cookie
3279 txn->cookie_first_date = 0;
3280 txn->cookie_last_date = 0;
3281 }
3282
3283 /* Here, we'll look for the first running server which supports the cookie.
3284 * This allows to share a same cookie between several servers, for example
3285 * to dedicate backup servers to specific servers only.
3286 * However, to prevent clients from sticking to cookie-less backup server
3287 * when they have incidentely learned an empty cookie, we simply ignore
3288 * empty cookies and mark them as invalid.
3289 * The same behaviour is applied when persistence must be ignored.
3290 */
3291 if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
3292 srv = NULL;
3293
3294 while (srv) {
3295 if (srv->cookie && (srv->cklen == delim - val_beg) &&
3296 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
3297 if ((srv->cur_state != SRV_ST_STOPPED) ||
3298 (s->be->options & PR_O_PERSIST) ||
3299 (s->flags & SF_FORCE_PRST)) {
3300 /* we found the server and we can use it */
3301 txn->flags &= ~TX_CK_MASK;
3302 txn->flags |= (srv->cur_state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
3303 s->flags |= SF_DIRECT | SF_ASSIGNED;
3304 s->target = &srv->obj_type;
3305 break;
3306 } else {
3307 /* we found a server, but it's down,
3308 * mark it as such and go on in case
3309 * another one is available.
3310 */
3311 txn->flags &= ~TX_CK_MASK;
3312 txn->flags |= TX_CK_DOWN;
3313 }
3314 }
3315 srv = srv->next;
3316 }
3317
3318 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
3319 /* no server matched this cookie or we deliberately skipped it */
3320 txn->flags &= ~TX_CK_MASK;
3321 if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
3322 txn->flags |= TX_CK_UNUSED;
3323 else
3324 txn->flags |= TX_CK_INVALID;
3325 }
3326
3327 /* depending on the cookie mode, we may have to either :
3328 * - delete the complete cookie if we're in insert+indirect mode, so that
3329 * the server never sees it ;
3330 * - remove the server id from the cookie value, and tag the cookie as an
Joseph Herlante9d5c722018-11-25 11:00:25 -08003331 * application cookie so that it does not get accidentally removed later,
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003332 * if we're in cookie prefix mode
3333 */
3334 if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
3335 int delta; /* negative */
3336
3337 memmove(val_beg, delim + 1, hdr_end - (delim + 1));
3338 delta = val_beg - (delim + 1);
3339 val_end += delta;
3340 next += delta;
3341 hdr_end += delta;
3342 del_from = NULL;
3343 preserve_hdr = 1; /* we want to keep this cookie */
3344 }
3345 else if (del_from == NULL &&
3346 (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
3347 del_from = prev;
3348 }
3349 }
3350 else {
3351 /* This is not our cookie, so we must preserve it. But if we already
3352 * scheduled another cookie for removal, we cannot remove the
3353 * complete header, but we can remove the previous block itself.
3354 */
3355 preserve_hdr = 1;
3356
3357 if (del_from != NULL) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003358 int delta = http_del_hdr_value(hdr_beg, hdr_end, &del_from, prev);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003359 if (att_beg >= del_from)
3360 att_beg += delta;
3361 if (att_end >= del_from)
3362 att_end += delta;
3363 val_beg += delta;
3364 val_end += delta;
3365 next += delta;
3366 hdr_end += delta;
3367 prev = del_from;
3368 del_from = NULL;
3369 }
3370 }
3371
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003372 } /* for each cookie */
3373
3374
3375 /* There are no more cookies on this line.
3376 * We may still have one (or several) marked for deletion at the
3377 * end of the line. We must do this now in two ways :
3378 * - if some cookies must be preserved, we only delete from the
3379 * mark to the end of line ;
3380 * - if nothing needs to be preserved, simply delete the whole header
3381 */
3382 if (del_from) {
3383 hdr_end = (preserve_hdr ? del_from : hdr_beg);
3384 }
3385 if ((hdr_end - hdr_beg) != ctx.value.len) {
Christopher Faulet3e2638e2019-06-18 09:49:16 +02003386 if (hdr_beg != hdr_end)
3387 htx_change_blk_value_len(htx, ctx.blk, hdr_end - hdr_beg);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003388 else
3389 http_remove_header(htx, &ctx);
3390 }
3391 } /* for each "Cookie header */
3392}
3393
3394/*
3395 * Manage server-side cookies. It can impact performance by about 2% so it is
3396 * desirable to call it only when needed. This function is also used when we
3397 * just need to know if there is a cookie (eg: for check-cache).
3398 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003399static void http_manage_server_side_cookies(struct stream *s, struct channel *res)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003400{
3401 struct session *sess = s->sess;
3402 struct http_txn *txn = s->txn;
3403 struct htx *htx;
3404 struct http_hdr_ctx ctx;
3405 struct server *srv;
3406 char *hdr_beg, *hdr_end;
3407 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau6f7a02a2019-04-15 21:49:49 +02003408 int is_cookie2 = 0;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003409
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003410 htx = htxbuf(&res->buf);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003411
3412 ctx.blk = NULL;
3413 while (1) {
Olivier Houchardf0f42382019-07-22 17:43:46 +02003414 int is_first = 1;
3415
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003416 if (!http_find_header(htx, ist("Set-Cookie"), &ctx, 1)) {
3417 if (!http_find_header(htx, ist("Set-Cookie2"), &ctx, 1))
3418 break;
3419 is_cookie2 = 1;
3420 }
3421
3422 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
3423 * <prev> points to the colon.
3424 */
3425 txn->flags |= TX_SCK_PRESENT;
3426
3427 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
3428 * check-cache is enabled) and we are not interested in checking
3429 * them. Warning, the cookie capture is declared in the frontend.
3430 */
3431 if (s->be->cookie_name == NULL && sess->fe->capture_name == NULL)
3432 break;
3433
3434 /* OK so now we know we have to process this response cookie.
3435 * The format of the Set-Cookie header is slightly different
3436 * from the format of the Cookie header in that it does not
3437 * support the comma as a cookie delimiter (thus the header
3438 * cannot be folded) because the Expires attribute described in
3439 * the original Netscape's spec may contain an unquoted date
3440 * with a comma inside. We have to live with this because
3441 * many browsers don't support Max-Age and some browsers don't
3442 * support quoted strings. However the Set-Cookie2 header is
3443 * clean.
3444 *
3445 * We have to keep multiple pointers in order to support cookie
3446 * removal at the beginning, middle or end of header without
3447 * corrupting the header (in case of set-cookie2). A special
3448 * pointer, <scav> points to the beginning of the set-cookie-av
3449 * fields after the first semi-colon. The <next> pointer points
3450 * either to the end of line (set-cookie) or next unquoted comma
3451 * (set-cookie2). All of these headers are valid :
3452 *
3453 * hdr_beg hdr_end
3454 * | |
3455 * v |
3456 * NAME1 = VALUE 1 ; Secure; Path="/" |
3457 * NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT v
3458 * NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT
3459 * NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard
3460 * | | | | | | | |
3461 * | | | | | | | +-> next
3462 * | | | | | | +------------> scav
3463 * | | | | | +--------------> val_end
3464 * | | | | +--------------------> val_beg
3465 * | | | +----------------------> equal
3466 * | | +------------------------> att_end
3467 * | +----------------------------> att_beg
3468 * +------------------------------> prev
3469 * -------------------------------> hdr_beg
3470 */
3471 hdr_beg = ctx.value.ptr;
3472 hdr_end = hdr_beg + ctx.value.len;
3473 for (prev = hdr_beg; prev < hdr_end; prev = next) {
3474
3475 /* Iterate through all cookies on this line */
3476
3477 /* find att_beg */
3478 att_beg = prev;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003479 if (!is_first)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003480 att_beg++;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003481 is_first = 0;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003482
3483 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
3484 att_beg++;
3485
3486 /* find att_end : this is the first character after the last non
3487 * space before the equal. It may be equal to hdr_end.
3488 */
3489 equal = att_end = att_beg;
3490
3491 while (equal < hdr_end) {
3492 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
3493 break;
3494 if (HTTP_IS_SPHT(*equal++))
3495 continue;
3496 att_end = equal;
3497 }
3498
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003499 /* here, <equal> points to '=', a delimiter or the end. <att_end>
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003500 * is between <att_beg> and <equal>, both may be identical.
3501 */
3502
3503 /* look for end of cookie if there is an equal sign */
3504 if (equal < hdr_end && *equal == '=') {
3505 /* look for the beginning of the value */
3506 val_beg = equal + 1;
3507 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
3508 val_beg++;
3509
3510 /* find the end of the value, respecting quotes */
3511 next = http_find_cookie_value_end(val_beg, hdr_end);
3512
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003513 /* make val_end point to the first white space or delimiter after the value */
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003514 val_end = next;
3515 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
3516 val_end--;
3517 }
3518 else {
3519 /* <equal> points to next comma, semi-colon or EOL */
3520 val_beg = val_end = next = equal;
3521 }
3522
3523 if (next < hdr_end) {
3524 /* Set-Cookie2 supports multiple cookies, and <next> points to
3525 * a colon or semi-colon before the end. So skip all attr-value
3526 * pairs and look for the next comma. For Set-Cookie, since
3527 * commas are permitted in values, skip to the end.
3528 */
3529 if (is_cookie2)
3530 next = http_find_hdr_value_end(next, hdr_end);
3531 else
3532 next = hdr_end;
3533 }
3534
3535 /* Now everything is as on the diagram above */
3536
3537 /* Ignore cookies with no equal sign */
3538 if (equal == val_end)
3539 continue;
3540
3541 /* If there are spaces around the equal sign, we need to
3542 * strip them otherwise we'll get trouble for cookie captures,
3543 * or even for rewrites. Since this happens extremely rarely,
3544 * it does not hurt performance.
3545 */
3546 if (unlikely(att_end != equal || val_beg > equal + 1)) {
3547 int stripped_before = 0;
3548 int stripped_after = 0;
3549
3550 if (att_end != equal) {
3551 memmove(att_end, equal, hdr_end - equal);
3552 stripped_before = (att_end - equal);
3553 equal += stripped_before;
3554 val_beg += stripped_before;
3555 }
3556
3557 if (val_beg > equal + 1) {
3558 memmove(equal + 1, val_beg, hdr_end + stripped_before - val_beg);
3559 stripped_after = (equal + 1) - val_beg;
3560 val_beg += stripped_after;
3561 stripped_before += stripped_after;
3562 }
3563
3564 val_end += stripped_before;
3565 next += stripped_before;
3566 hdr_end += stripped_before;
3567
Christopher Faulet3e2638e2019-06-18 09:49:16 +02003568 htx_change_blk_value_len(htx, ctx.blk, hdr_end - hdr_beg);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003569 ctx.value.len = hdr_end - hdr_beg;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003570 }
3571
3572 /* First, let's see if we want to capture this cookie. We check
3573 * that we don't already have a server side cookie, because we
3574 * can only capture one. Also as an optimisation, we ignore
3575 * cookies shorter than the declared name.
3576 */
3577 if (sess->fe->capture_name != NULL &&
3578 txn->srv_cookie == NULL &&
3579 (val_end - att_beg >= sess->fe->capture_namelen) &&
3580 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
3581 int log_len = val_end - att_beg;
3582 if ((txn->srv_cookie = pool_alloc(pool_head_capture)) == NULL) {
3583 ha_alert("HTTP logging : out of memory.\n");
3584 }
3585 else {
3586 if (log_len > sess->fe->capture_len)
3587 log_len = sess->fe->capture_len;
3588 memcpy(txn->srv_cookie, att_beg, log_len);
3589 txn->srv_cookie[log_len] = 0;
3590 }
3591 }
3592
3593 srv = objt_server(s->target);
3594 /* now check if we need to process it for persistence */
3595 if (!(s->flags & SF_IGNORE_PRST) &&
3596 (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
3597 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
3598 /* assume passive cookie by default */
3599 txn->flags &= ~TX_SCK_MASK;
3600 txn->flags |= TX_SCK_FOUND;
3601
3602 /* If the cookie is in insert mode on a known server, we'll delete
3603 * this occurrence because we'll insert another one later.
3604 * We'll delete it too if the "indirect" option is set and we're in
3605 * a direct access.
3606 */
3607 if (s->be->ck_opts & PR_CK_PSV) {
3608 /* The "preserve" flag was set, we don't want to touch the
3609 * server's cookie.
3610 */
3611 }
3612 else if ((srv && (s->be->ck_opts & PR_CK_INS)) ||
3613 ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) {
3614 /* this cookie must be deleted */
3615 if (prev == hdr_beg && next == hdr_end) {
3616 /* whole header */
3617 http_remove_header(htx, &ctx);
3618 /* note: while both invalid now, <next> and <hdr_end>
3619 * are still equal, so the for() will stop as expected.
3620 */
3621 } else {
3622 /* just remove the value */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003623 int delta = http_del_hdr_value(hdr_beg, hdr_end, &prev, next);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003624 next = prev;
3625 hdr_end += delta;
3626 }
3627 txn->flags &= ~TX_SCK_MASK;
3628 txn->flags |= TX_SCK_DELETED;
3629 /* and go on with next cookie */
3630 }
3631 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) {
3632 /* replace bytes val_beg->val_end with the cookie name associated
3633 * with this server since we know it.
3634 */
3635 int sliding, delta;
3636
3637 ctx.value = ist2(val_beg, val_end - val_beg);
3638 ctx.lws_before = ctx.lws_after = 0;
3639 http_replace_header_value(htx, &ctx, ist2(srv->cookie, srv->cklen));
3640 delta = srv->cklen - (val_end - val_beg);
3641 sliding = (ctx.value.ptr - val_beg);
3642 hdr_beg += sliding;
3643 val_beg += sliding;
3644 next += sliding + delta;
3645 hdr_end += sliding + delta;
3646
3647 txn->flags &= ~TX_SCK_MASK;
3648 txn->flags |= TX_SCK_REPLACED;
3649 }
3650 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) {
3651 /* insert the cookie name associated with this server
3652 * before existing cookie, and insert a delimiter between them..
3653 */
3654 int sliding, delta;
3655 ctx.value = ist2(val_beg, 0);
3656 ctx.lws_before = ctx.lws_after = 0;
3657 http_replace_header_value(htx, &ctx, ist2(srv->cookie, srv->cklen + 1));
3658 delta = srv->cklen + 1;
3659 sliding = (ctx.value.ptr - val_beg);
3660 hdr_beg += sliding;
3661 val_beg += sliding;
3662 next += sliding + delta;
3663 hdr_end += sliding + delta;
3664
3665 val_beg[srv->cklen] = COOKIE_DELIM;
3666 txn->flags &= ~TX_SCK_MASK;
3667 txn->flags |= TX_SCK_REPLACED;
3668 }
3669 }
3670 /* that's done for this cookie, check the next one on the same
3671 * line when next != hdr_end (only if is_cookie2).
3672 */
3673 }
3674 }
3675}
3676
Christopher Faulet25a02f62018-10-24 12:00:25 +02003677/*
3678 * Parses the Cache-Control and Pragma request header fields to determine if
3679 * the request may be served from the cache and/or if it is cacheable. Updates
3680 * s->txn->flags.
3681 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003682void http_check_request_for_cacheability(struct stream *s, struct channel *req)
Christopher Faulet25a02f62018-10-24 12:00:25 +02003683{
3684 struct http_txn *txn = s->txn;
3685 struct htx *htx;
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003686 struct http_hdr_ctx ctx = { .blk = NULL };
3687 int pragma_found, cc_found;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003688
3689 if ((txn->flags & (TX_CACHEABLE|TX_CACHE_IGNORE)) == TX_CACHE_IGNORE)
3690 return; /* nothing more to do here */
3691
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003692 htx = htxbuf(&req->buf);
Christopher Faulet25a02f62018-10-24 12:00:25 +02003693 pragma_found = cc_found = 0;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003694
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003695 /* Check "pragma" header for HTTP/1.0 compatibility. */
3696 if (http_find_header(htx, ist("pragma"), &ctx, 1)) {
3697 if (isteqi(ctx.value, ist("no-cache"))) {
3698 pragma_found = 1;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003699 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003700 }
Christopher Faulet25a02f62018-10-24 12:00:25 +02003701
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003702 ctx.blk = NULL;
3703 /* Don't use the cache and don't try to store if we found the
3704 * Authorization header */
3705 if (http_find_header(htx, ist("authorization"), &ctx, 1)) {
3706 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3707 txn->flags |= TX_CACHE_IGNORE;
3708 }
Christopher Faulet25a02f62018-10-24 12:00:25 +02003709
Christopher Faulet25a02f62018-10-24 12:00:25 +02003710
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003711 /* Look for "cache-control" header and iterate over all the values
3712 * until we find one that specifies that caching is possible or not. */
3713 ctx.blk = NULL;
3714 while (http_find_header(htx, ist("cache-control"), &ctx, 0)) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003715 cc_found = 1;
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003716 /* We don't check the values after max-age, max-stale nor min-fresh,
3717 * we simply don't use the cache when they're specified. */
3718 if (istmatchi(ctx.value, ist("max-age")) ||
3719 istmatchi(ctx.value, ist("no-cache")) ||
3720 istmatchi(ctx.value, ist("max-stale")) ||
3721 istmatchi(ctx.value, ist("min-fresh"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003722 txn->flags |= TX_CACHE_IGNORE;
3723 continue;
3724 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003725 if (istmatchi(ctx.value, ist("no-store"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003726 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3727 continue;
3728 }
3729 }
3730
3731 /* RFC7234#5.4:
3732 * When the Cache-Control header field is also present and
3733 * understood in a request, Pragma is ignored.
3734 * When the Cache-Control header field is not present in a
3735 * request, caches MUST consider the no-cache request
3736 * pragma-directive as having the same effect as if
3737 * "Cache-Control: no-cache" were present.
3738 */
3739 if (!cc_found && pragma_found)
3740 txn->flags |= TX_CACHE_IGNORE;
3741}
3742
3743/*
3744 * Check if response is cacheable or not. Updates s->txn->flags.
3745 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003746void http_check_response_for_cacheability(struct stream *s, struct channel *res)
Christopher Faulet25a02f62018-10-24 12:00:25 +02003747{
3748 struct http_txn *txn = s->txn;
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003749 struct http_hdr_ctx ctx = { .blk = NULL };
Christopher Faulet25a02f62018-10-24 12:00:25 +02003750 struct htx *htx;
Remi Tricot-Le Bretoncc9bf2e2020-11-12 11:14:41 +01003751 int has_freshness_info = 0;
3752 int has_validator = 0;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003753
3754 if (txn->status < 200) {
3755 /* do not try to cache interim responses! */
3756 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3757 return;
3758 }
3759
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003760 htx = htxbuf(&res->buf);
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003761 /* Check "pragma" header for HTTP/1.0 compatibility. */
3762 if (http_find_header(htx, ist("pragma"), &ctx, 1)) {
3763 if (isteqi(ctx.value, ist("no-cache"))) {
3764 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3765 return;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003766 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003767 }
Christopher Faulet25a02f62018-10-24 12:00:25 +02003768
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003769 /* Look for "cache-control" header and iterate over all the values
3770 * until we find one that specifies that caching is possible or not. */
3771 ctx.blk = NULL;
3772 while (http_find_header(htx, ist("cache-control"), &ctx, 0)) {
3773 if (isteqi(ctx.value, ist("public"))) {
3774 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003775 continue;
3776 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003777 if (isteqi(ctx.value, ist("private")) ||
3778 isteqi(ctx.value, ist("no-cache")) ||
3779 isteqi(ctx.value, ist("no-store")) ||
3780 isteqi(ctx.value, ist("max-age=0")) ||
3781 isteqi(ctx.value, ist("s-maxage=0"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003782 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003783 continue;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003784 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003785 /* We might have a no-cache="set-cookie" form. */
3786 if (istmatchi(ctx.value, ist("no-cache=\"set-cookie"))) {
3787 txn->flags &= ~TX_CACHE_COOK;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003788 continue;
3789 }
Remi Tricot-Le Bretoncc9bf2e2020-11-12 11:14:41 +01003790
3791 if (istmatchi(ctx.value, ist("s-maxage")) ||
3792 istmatchi(ctx.value, ist("max-age"))) {
3793 has_freshness_info = 1;
3794 continue;
3795 }
3796 }
3797
3798 /* If no freshness information could be found in Cache-Control values,
3799 * look for an Expires header. */
3800 if (!has_freshness_info) {
3801 ctx.blk = NULL;
3802 has_freshness_info = http_find_header(htx, ist("expires"), &ctx, 0);
Christopher Faulet25a02f62018-10-24 12:00:25 +02003803 }
Remi Tricot-Le Bretoncc9bf2e2020-11-12 11:14:41 +01003804
3805 /* If no freshness information could be found in Cache-Control or Expires
3806 * values, look for an explicit validator. */
3807 if (!has_freshness_info) {
3808 ctx.blk = NULL;
3809 has_validator = 1;
3810 if (!http_find_header(htx, ist("etag"), &ctx, 0)) {
3811 ctx.blk = NULL;
3812 if (!http_find_header(htx, ist("last-modified"), &ctx, 0))
3813 has_validator = 0;
3814 }
3815 }
3816
3817 /* We won't store an entry that has neither a cache validator nor an
3818 * explicit expiration time, as suggested in RFC 7234#3. */
3819 if (!has_freshness_info && !has_validator)
3820 txn->flags |= TX_CACHE_IGNORE;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003821}
3822
Christopher Faulet377c5a52018-10-24 21:21:30 +02003823/*
3824 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
3825 * for the current backend.
3826 *
3827 * It is assumed that the request is either a HEAD, GET, or POST and that the
3828 * uri_auth field is valid.
3829 *
3830 * Returns 1 if stats should be provided, otherwise 0.
3831 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003832static int http_stats_check_uri(struct stream *s, struct http_txn *txn, struct proxy *backend)
Christopher Faulet377c5a52018-10-24 21:21:30 +02003833{
3834 struct uri_auth *uri_auth = backend->uri_auth;
3835 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003836 struct htx_sl *sl;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003837 struct ist uri;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003838
3839 if (!uri_auth)
3840 return 0;
3841
3842 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
3843 return 0;
3844
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003845 htx = htxbuf(&s->req.buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02003846 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003847 uri = htx_sl_req_uri(sl);
Willy Tarreau1eb3b482019-10-31 15:50:28 +01003848 if (*uri_auth->uri_prefix == '/')
3849 uri = http_get_path(uri);
Christopher Faulet377c5a52018-10-24 21:21:30 +02003850
3851 /* check URI size */
3852 if (uri_auth->uri_len > uri.len)
3853 return 0;
3854
3855 if (memcmp(uri.ptr, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
3856 return 0;
3857
3858 return 1;
3859}
3860
3861/* This function prepares an applet to handle the stats. It can deal with the
3862 * "100-continue" expectation, check that admin rules are met for POST requests,
3863 * and program a response message if something was unexpected. It cannot fail
3864 * and always relies on the stats applet to complete the job. It does not touch
3865 * analysers nor counters, which are left to the caller. It does not touch
3866 * s->target which is supposed to already point to the stats applet. The caller
3867 * is expected to have already assigned an appctx to the stream.
3868 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003869static int http_handle_stats(struct stream *s, struct channel *req)
Christopher Faulet377c5a52018-10-24 21:21:30 +02003870{
3871 struct stats_admin_rule *stats_admin_rule;
3872 struct stream_interface *si = &s->si[1];
3873 struct session *sess = s->sess;
3874 struct http_txn *txn = s->txn;
3875 struct http_msg *msg = &txn->req;
3876 struct uri_auth *uri_auth = s->be->uri_auth;
3877 const char *h, *lookup, *end;
3878 struct appctx *appctx;
3879 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003880 struct htx_sl *sl;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003881
3882 appctx = si_appctx(si);
3883 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
3884 appctx->st1 = appctx->st2 = 0;
3885 appctx->ctx.stats.st_code = STAT_STATUS_INIT;
Willy Tarreau676c29e2019-10-09 10:50:01 +02003886 appctx->ctx.stats.flags |= uri_auth->flags;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003887 appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */
3888 if ((msg->flags & HTTP_MSGF_VER_11) && (txn->meth != HTTP_METH_HEAD))
3889 appctx->ctx.stats.flags |= STAT_CHUNKED;
3890
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003891 htx = htxbuf(&req->buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02003892 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003893 lookup = HTX_SL_REQ_UPTR(sl) + uri_auth->uri_len;
3894 end = HTX_SL_REQ_UPTR(sl) + HTX_SL_REQ_ULEN(sl);
Christopher Faulet377c5a52018-10-24 21:21:30 +02003895
3896 for (h = lookup; h <= end - 3; h++) {
3897 if (memcmp(h, ";up", 3) == 0) {
3898 appctx->ctx.stats.flags |= STAT_HIDE_DOWN;
3899 break;
3900 }
Willy Tarreau3e320362020-10-23 17:28:57 +02003901 if (memcmp(h, ";no-maint", 3) == 0) {
3902 appctx->ctx.stats.flags |= STAT_HIDE_MAINT;
3903 break;
3904 }
Christopher Faulet377c5a52018-10-24 21:21:30 +02003905 }
3906
3907 if (uri_auth->refresh) {
3908 for (h = lookup; h <= end - 10; h++) {
3909 if (memcmp(h, ";norefresh", 10) == 0) {
3910 appctx->ctx.stats.flags |= STAT_NO_REFRESH;
3911 break;
3912 }
3913 }
3914 }
3915
3916 for (h = lookup; h <= end - 4; h++) {
3917 if (memcmp(h, ";csv", 4) == 0) {
Christopher Faulet6338a082019-09-09 15:50:54 +02003918 appctx->ctx.stats.flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
Christopher Faulet377c5a52018-10-24 21:21:30 +02003919 break;
3920 }
3921 }
3922
3923 for (h = lookup; h <= end - 6; h++) {
3924 if (memcmp(h, ";typed", 6) == 0) {
Christopher Faulet6338a082019-09-09 15:50:54 +02003925 appctx->ctx.stats.flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
Christopher Faulet377c5a52018-10-24 21:21:30 +02003926 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
3927 break;
3928 }
3929 }
3930
Christopher Faulet6338a082019-09-09 15:50:54 +02003931 for (h = lookup; h <= end - 5; h++) {
3932 if (memcmp(h, ";json", 5) == 0) {
3933 appctx->ctx.stats.flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
3934 appctx->ctx.stats.flags |= STAT_FMT_JSON;
3935 break;
3936 }
3937 }
3938
3939 for (h = lookup; h <= end - 12; h++) {
3940 if (memcmp(h, ";json-schema", 12) == 0) {
3941 appctx->ctx.stats.flags &= ~STAT_FMT_MASK;
3942 appctx->ctx.stats.flags |= STAT_JSON_SCHM;
3943 break;
3944 }
3945 }
3946
Christopher Faulet377c5a52018-10-24 21:21:30 +02003947 for (h = lookup; h <= end - 8; h++) {
3948 if (memcmp(h, ";st=", 4) == 0) {
3949 int i;
3950 h += 4;
3951 appctx->ctx.stats.st_code = STAT_STATUS_UNKN;
3952 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
3953 if (strncmp(stat_status_codes[i], h, 4) == 0) {
3954 appctx->ctx.stats.st_code = i;
3955 break;
3956 }
3957 }
3958 break;
3959 }
3960 }
3961
3962 appctx->ctx.stats.scope_str = 0;
3963 appctx->ctx.stats.scope_len = 0;
3964 for (h = lookup; h <= end - 8; h++) {
3965 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
3966 int itx = 0;
3967 const char *h2;
3968 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
3969 const char *err;
3970
3971 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
3972 h2 = h;
Christopher Fauleted7a0662019-01-14 11:07:34 +01003973 appctx->ctx.stats.scope_str = h2 - HTX_SL_REQ_UPTR(sl);
3974 while (h < end) {
Christopher Faulet377c5a52018-10-24 21:21:30 +02003975 if (*h == ';' || *h == '&' || *h == ' ')
3976 break;
3977 itx++;
3978 h++;
3979 }
3980
3981 if (itx > STAT_SCOPE_TXT_MAXLEN)
3982 itx = STAT_SCOPE_TXT_MAXLEN;
3983 appctx->ctx.stats.scope_len = itx;
3984
3985 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
3986 memcpy(scope_txt, h2, itx);
3987 scope_txt[itx] = '\0';
3988 err = invalid_char(scope_txt);
3989 if (err) {
3990 /* bad char in search text => clear scope */
3991 appctx->ctx.stats.scope_str = 0;
3992 appctx->ctx.stats.scope_len = 0;
3993 }
3994 break;
3995 }
3996 }
3997
3998 /* now check whether we have some admin rules for this request */
3999 list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) {
4000 int ret = 1;
4001
4002 if (stats_admin_rule->cond) {
4003 ret = acl_exec_cond(stats_admin_rule->cond, s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
4004 ret = acl_pass(ret);
4005 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
4006 ret = !ret;
4007 }
4008
4009 if (ret) {
4010 /* no rule, or the rule matches */
4011 appctx->ctx.stats.flags |= STAT_ADMIN;
4012 break;
4013 }
4014 }
4015
Christopher Faulet5d45e382019-02-27 15:15:23 +01004016 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
4017 appctx->st0 = STAT_HTTP_HEAD;
4018 else if (txn->meth == HTTP_METH_POST) {
Christopher Fauletbd9e8422019-08-15 22:26:48 +02004019 if (appctx->ctx.stats.flags & STAT_ADMIN) {
Christopher Faulet377c5a52018-10-24 21:21:30 +02004020 appctx->st0 = STAT_HTTP_POST;
Christopher Fauletbd9e8422019-08-15 22:26:48 +02004021 if (msg->msg_state < HTTP_MSG_DATA)
4022 req->analysers |= AN_REQ_HTTP_BODY;
4023 }
Christopher Faulet377c5a52018-10-24 21:21:30 +02004024 else {
Christopher Faulet5d45e382019-02-27 15:15:23 +01004025 /* POST without admin level */
Christopher Faulet377c5a52018-10-24 21:21:30 +02004026 appctx->ctx.stats.flags &= ~STAT_CHUNKED;
4027 appctx->ctx.stats.st_code = STAT_STATUS_DENY;
4028 appctx->st0 = STAT_HTTP_LAST;
4029 }
4030 }
4031 else {
Christopher Faulet5d45e382019-02-27 15:15:23 +01004032 /* Unsupported method */
4033 appctx->ctx.stats.flags &= ~STAT_CHUNKED;
4034 appctx->ctx.stats.st_code = STAT_STATUS_IVAL;
4035 appctx->st0 = STAT_HTTP_LAST;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004036 }
4037
4038 s->task->nice = -32; /* small boost for HTTP statistics */
4039 return 1;
4040}
4041
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004042void http_perform_server_redirect(struct stream *s, struct stream_interface *si)
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004043{
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004044 struct channel *req = &s->req;
4045 struct channel *res = &s->res;
4046 struct server *srv;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004047 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004048 struct htx_sl *sl;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004049 struct ist path, location;
4050 unsigned int flags;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004051
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004052 /*
4053 * Create the location
4054 */
4055 chunk_reset(&trash);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004056
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004057 /* 1: add the server's prefix */
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004058 /* special prefix "/" means don't change URL */
4059 srv = __objt_server(s->target);
4060 if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') {
4061 if (!chunk_memcat(&trash, srv->rdr_pfx, srv->rdr_len))
4062 return;
4063 }
4064
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004065 /* 2: add the request Path */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004066 htx = htxbuf(&req->buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02004067 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004068 path = http_get_path(htx_sl_req_uri(sl));
Tim Duesterhused526372020-03-05 17:56:33 +01004069 if (!isttest(path))
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004070 return;
4071
4072 if (!chunk_memcat(&trash, path.ptr, path.len))
4073 return;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004074 location = ist2(trash.area, trash.data);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004075
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004076 /*
4077 * Create the 302 respone
4078 */
4079 htx = htx_from_buf(&res->buf);
4080 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
4081 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
4082 ist("HTTP/1.1"), ist("302"), ist("Found"));
4083 if (!sl)
4084 goto fail;
4085 sl->info.res.status = 302;
4086 s->txn->status = 302;
4087
4088 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) ||
4089 !htx_add_header(htx, ist("Connection"), ist("close")) ||
4090 !htx_add_header(htx, ist("Content-length"), ist("0")) ||
4091 !htx_add_header(htx, ist("Location"), location))
4092 goto fail;
4093
4094 if (!htx_add_endof(htx, HTX_BLK_EOH) || !htx_add_endof(htx, HTX_BLK_EOM))
4095 goto fail;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004096
Christopher Fauletc20afb82020-01-24 19:16:26 +01004097 htx_to_buf(htx, &res->buf);
Christopher Fauleta72a7e42020-01-28 09:28:11 +01004098 if (!http_forward_proxy_resp(s, 1))
4099 goto fail;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004100
4101 /* return without error. */
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004102 si_shutr(si);
4103 si_shutw(si);
4104 si->err_type = SI_ET_NONE;
4105 si->state = SI_ST_CLO;
4106
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004107 if (!(s->flags & SF_ERR_MASK))
4108 s->flags |= SF_ERR_LOCAL;
4109 if (!(s->flags & SF_FINST_MASK))
4110 s->flags |= SF_FINST_C;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004111
4112 /* FIXME: we should increase a counter of redirects per server and per backend. */
4113 srv_inc_sess_ctr(srv);
4114 srv_set_sess_last(srv);
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004115 return;
4116
4117 fail:
4118 /* If an error occurred, remove the incomplete HTTP response from the
4119 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004120 channel_htx_truncate(res, htx);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004121}
4122
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05004123/* This function terminates the request because it was completely analyzed or
Christopher Fauletf2824e62018-10-01 12:12:37 +02004124 * because an error was triggered during the body forwarding.
4125 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004126static void http_end_request(struct stream *s)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004127{
4128 struct channel *chn = &s->req;
4129 struct http_txn *txn = s->txn;
4130
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004131 DBG_TRACE_ENTER(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004132
Christopher Fauletb42a8b62018-11-19 21:59:00 +01004133 if (unlikely(txn->req.msg_state == HTTP_MSG_ERROR ||
4134 txn->rsp.msg_state == HTTP_MSG_ERROR)) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004135 channel_abort(chn);
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004136 channel_htx_truncate(chn, htxbuf(&chn->buf));
Christopher Fauletf2824e62018-10-01 12:12:37 +02004137 goto end;
4138 }
4139
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004140 if (unlikely(txn->req.msg_state < HTTP_MSG_DONE)) {
4141 DBG_TRACE_DEVEL("waiting end of the request", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004142 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004143 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004144
4145 if (txn->req.msg_state == HTTP_MSG_DONE) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004146 /* No need to read anymore, the request was completely parsed.
4147 * We can shut the read side unless we want to abort_on_close,
4148 * or we have a POST request. The issue with POST requests is
4149 * that some browsers still send a CRLF after the request, and
4150 * this CRLF must be read so that it does not remain in the kernel
4151 * buffers, otherwise a close could cause an RST on some systems
4152 * (eg: Linux).
4153 */
Christopher Faulet769d0e92019-03-22 14:23:18 +01004154 if (!(s->be->options & PR_O_ABRT_CLOSE) && txn->meth != HTTP_METH_POST)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004155 channel_dont_read(chn);
4156
4157 /* if the server closes the connection, we want to immediately react
4158 * and close the socket to save packets and syscalls.
4159 */
4160 s->si[1].flags |= SI_FL_NOHALF;
4161
4162 /* In any case we've finished parsing the request so we must
4163 * disable Nagle when sending data because 1) we're not going
4164 * to shut this side, and 2) the server is waiting for us to
4165 * send pending data.
4166 */
4167 chn->flags |= CF_NEVER_WAIT;
4168
Christopher Fauletd01ce402019-01-02 17:44:13 +01004169 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
4170 /* The server has not finished to respond, so we
4171 * don't want to move in order not to upset it.
4172 */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004173 DBG_TRACE_DEVEL("waiting end of the response", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletd01ce402019-01-02 17:44:13 +01004174 return;
4175 }
4176
Christopher Fauletf2824e62018-10-01 12:12:37 +02004177 /* When we get here, it means that both the request and the
4178 * response have finished receiving. Depending on the connection
4179 * mode, we'll have to wait for the last bytes to leave in either
4180 * direction, and sometimes for a close to be effective.
4181 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02004182 if (txn->flags & TX_CON_WANT_TUN) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004183 /* Tunnel mode will not have any analyser so it needs to
4184 * poll for reads.
4185 */
4186 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004187 if (b_data(&chn->buf)) {
4188 DBG_TRACE_DEVEL("waiting to flush the request", STRM_EV_HTTP_ANA, s, txn);
Christopher Faulet9768c262018-10-22 09:34:31 +02004189 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004190 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004191 txn->req.msg_state = HTTP_MSG_TUNNEL;
4192 }
4193 else {
4194 /* we're not expecting any new data to come for this
4195 * transaction, so we can close it.
Christopher Faulet9768c262018-10-22 09:34:31 +02004196 *
4197 * However, there is an exception if the response
4198 * length is undefined. In this case, we need to wait
4199 * the close from the server. The response will be
4200 * switched in TUNNEL mode until the end.
Christopher Fauletf2824e62018-10-01 12:12:37 +02004201 */
4202 if (!(txn->rsp.flags & HTTP_MSGF_XFER_LEN) &&
4203 txn->rsp.msg_state != HTTP_MSG_CLOSED)
Christopher Faulet9768c262018-10-22 09:34:31 +02004204 goto check_channel_flags;
Christopher Fauletf2824e62018-10-01 12:12:37 +02004205
4206 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4207 channel_shutr_now(chn);
4208 channel_shutw_now(chn);
4209 }
4210 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004211 goto check_channel_flags;
4212 }
4213
4214 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
4215 http_msg_closing:
4216 /* nothing else to forward, just waiting for the output buffer
4217 * to be empty and for the shutw_now to take effect.
4218 */
4219 if (channel_is_empty(chn)) {
4220 txn->req.msg_state = HTTP_MSG_CLOSED;
4221 goto http_msg_closed;
4222 }
4223 else if (chn->flags & CF_SHUTW) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004224 txn->req.msg_state = HTTP_MSG_ERROR;
4225 goto end;
4226 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004227 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004228 return;
4229 }
4230
4231 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
4232 http_msg_closed:
Christopher Fauletf2824e62018-10-01 12:12:37 +02004233 /* if we don't know whether the server will close, we need to hard close */
4234 if (txn->rsp.flags & HTTP_MSGF_XFER_LEN)
4235 s->si[1].flags |= SI_FL_NOLINGER; /* we want to close ASAP */
Christopher Fauletf2824e62018-10-01 12:12:37 +02004236 /* see above in MSG_DONE why we only do this in these states */
Christopher Faulet769d0e92019-03-22 14:23:18 +01004237 if (!(s->be->options & PR_O_ABRT_CLOSE))
Christopher Fauletf2824e62018-10-01 12:12:37 +02004238 channel_dont_read(chn);
4239 goto end;
4240 }
4241
4242 check_channel_flags:
4243 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4244 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4245 /* if we've just closed an output, let's switch */
4246 txn->req.msg_state = HTTP_MSG_CLOSING;
4247 goto http_msg_closing;
4248 }
4249
4250 end:
4251 chn->analysers &= AN_REQ_FLT_END;
Christopher Faulet198ef8b2020-12-15 13:32:55 +01004252 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
4253 chn->flags |= CF_NEVER_WAIT;
4254 if (HAS_REQ_DATA_FILTERS(s))
Christopher Fauletf2824e62018-10-01 12:12:37 +02004255 chn->analysers |= AN_REQ_FLT_XFER_DATA;
Christopher Faulet198ef8b2020-12-15 13:32:55 +01004256 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004257 channel_auto_close(chn);
4258 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004259 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004260}
4261
4262
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05004263/* This function terminates the response because it was completely analyzed or
Christopher Fauletf2824e62018-10-01 12:12:37 +02004264 * because an error was triggered during the body forwarding.
4265 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004266static void http_end_response(struct stream *s)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004267{
4268 struct channel *chn = &s->res;
4269 struct http_txn *txn = s->txn;
4270
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004271 DBG_TRACE_ENTER(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004272
Christopher Fauletb42a8b62018-11-19 21:59:00 +01004273 if (unlikely(txn->req.msg_state == HTTP_MSG_ERROR ||
4274 txn->rsp.msg_state == HTTP_MSG_ERROR)) {
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004275 channel_htx_truncate(&s->req, htxbuf(&s->req.buf));
Christopher Faulet9768c262018-10-22 09:34:31 +02004276 channel_abort(&s->req);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004277 goto end;
4278 }
4279
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004280 if (unlikely(txn->rsp.msg_state < HTTP_MSG_DONE)) {
4281 DBG_TRACE_DEVEL("waiting end of the response", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004282 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004283 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004284
4285 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
4286 /* In theory, we don't need to read anymore, but we must
4287 * still monitor the server connection for a possible close
4288 * while the request is being uploaded, so we don't disable
4289 * reading.
4290 */
4291 /* channel_dont_read(chn); */
4292
4293 if (txn->req.msg_state < HTTP_MSG_DONE) {
4294 /* The client seems to still be sending data, probably
4295 * because we got an error response during an upload.
4296 * We have the choice of either breaking the connection
4297 * or letting it pass through. Let's do the later.
4298 */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004299 DBG_TRACE_DEVEL("waiting end of the request", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004300 return;
4301 }
4302
4303 /* When we get here, it means that both the request and the
4304 * response have finished receiving. Depending on the connection
4305 * mode, we'll have to wait for the last bytes to leave in either
4306 * direction, and sometimes for a close to be effective.
4307 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02004308 if (txn->flags & TX_CON_WANT_TUN) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004309 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004310 if (b_data(&chn->buf)) {
4311 DBG_TRACE_DEVEL("waiting to flush the respone", STRM_EV_HTTP_ANA, s, txn);
Christopher Faulet9768c262018-10-22 09:34:31 +02004312 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004313 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004314 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
4315 }
4316 else {
4317 /* we're not expecting any new data to come for this
4318 * transaction, so we can close it.
4319 */
4320 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4321 channel_shutr_now(chn);
4322 channel_shutw_now(chn);
4323 }
4324 }
4325 goto check_channel_flags;
4326 }
4327
4328 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
4329 http_msg_closing:
4330 /* nothing else to forward, just waiting for the output buffer
4331 * to be empty and for the shutw_now to take effect.
4332 */
4333 if (channel_is_empty(chn)) {
4334 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4335 goto http_msg_closed;
4336 }
4337 else if (chn->flags & CF_SHUTW) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004338 txn->rsp.msg_state = HTTP_MSG_ERROR;
Christopher Fauletcff0f732019-12-16 16:13:44 +01004339 _HA_ATOMIC_ADD(&strm_sess(s)->fe->fe_counters.cli_aborts, 1);
Olivier Houcharda798bf52019-03-08 18:52:00 +01004340 _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01004341 if (strm_sess(s)->listener->counters)
4342 _HA_ATOMIC_ADD(&strm_sess(s)->listener->counters->cli_aborts, 1);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004343 if (objt_server(s->target))
Christopher Fauletcff0f732019-12-16 16:13:44 +01004344 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.cli_aborts, 1);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004345 goto end;
4346 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004347 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004348 return;
4349 }
4350
4351 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
4352 http_msg_closed:
4353 /* drop any pending data */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004354 channel_htx_truncate(&s->req, htxbuf(&s->req.buf));
Christopher Faulet9768c262018-10-22 09:34:31 +02004355 channel_abort(&s->req);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004356 goto end;
4357 }
4358
4359 check_channel_flags:
4360 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4361 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4362 /* if we've just closed an output, let's switch */
4363 txn->rsp.msg_state = HTTP_MSG_CLOSING;
4364 goto http_msg_closing;
4365 }
4366
4367 end:
4368 chn->analysers &= AN_RES_FLT_END;
Christopher Faulet198ef8b2020-12-15 13:32:55 +01004369 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
4370 chn->flags |= CF_NEVER_WAIT;
4371 if (HAS_RSP_DATA_FILTERS(s))
4372 chn->analysers |= AN_RES_FLT_XFER_DATA;
4373 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004374 channel_auto_close(chn);
4375 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004376 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004377}
4378
Christopher Fauletef70e252020-01-28 09:26:19 +01004379/* Forward a response generated by HAProxy (error/redirect/return). This
4380 * function forwards all pending incoming data. If <final> is set to 0, nothing
4381 * more is performed. It is used for 1xx informational messages. Otherwise, the
Christopher Faulet507479b2020-05-15 12:29:46 +02004382 * transaction is terminated and the request is emptied. On success 1 is
Christopher Faulet40e6b552020-06-25 16:04:50 +02004383 * returned. If an error occurred, 0 is returned. If it fails, this function
4384 * only exits. It is the caller responsibility to do the cleanup.
Christopher Fauletef70e252020-01-28 09:26:19 +01004385 */
4386int http_forward_proxy_resp(struct stream *s, int final)
4387{
4388 struct channel *req = &s->req;
4389 struct channel *res = &s->res;
4390 struct htx *htx = htxbuf(&res->buf);
4391 size_t data;
4392
4393 if (final) {
4394 htx->flags |= HTX_FL_PROXY_RESP;
Christopher Faulet507479b2020-05-15 12:29:46 +02004395
Christopher Fauletaab1b672020-11-18 16:44:02 +01004396 if (!htx_is_empty(htx) && !http_eval_after_res_rules(s))
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01004397 return 0;
Christopher Fauletef70e252020-01-28 09:26:19 +01004398
Christopher Fauletd6c48362020-10-19 18:01:38 +02004399 if (s->txn->meth == HTTP_METH_HEAD)
4400 htx_skip_msg_payload(htx);
4401
Christopher Fauletef70e252020-01-28 09:26:19 +01004402 channel_auto_read(req);
4403 channel_abort(req);
4404 channel_auto_close(req);
4405 channel_htx_erase(req, htxbuf(&req->buf));
4406
4407 res->wex = tick_add_ifset(now_ms, res->wto);
4408 channel_auto_read(res);
4409 channel_auto_close(res);
4410 channel_shutr_now(res);
Christopher Faulet1a9db7c2020-06-25 15:36:45 +02004411 res->flags |= CF_EOI; /* The response is terminated, add EOI */
Christopher Faulet42432f32020-11-20 17:43:16 +01004412 htxbuf(&res->buf)->flags |= HTX_FL_EOM; /* no more data are expected */
Christopher Fauletef70e252020-01-28 09:26:19 +01004413 }
Christopher Fauletcf6898c2020-06-25 15:55:11 +02004414 else {
4415 /* Send ASAP informational messages. Rely on CF_EOI for final
4416 * response.
4417 */
4418 res->flags |= CF_SEND_DONTWAIT;
4419 }
Christopher Fauletef70e252020-01-28 09:26:19 +01004420
4421 data = htx->data - co_data(res);
4422 c_adv(res, data);
4423 htx->first = -1;
4424 res->total += data;
4425 return 1;
4426}
4427
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004428void http_server_error(struct stream *s, struct stream_interface *si, int err,
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004429 int finst, struct http_reply *msg)
Christopher Faulet0f226952018-10-22 09:29:56 +02004430{
Christopher Faulet72c7d8d2020-01-27 15:32:25 +01004431 http_reply_and_close(s, s->txn->status, msg);
Christopher Faulet0f226952018-10-22 09:29:56 +02004432 if (!(s->flags & SF_ERR_MASK))
4433 s->flags |= err;
4434 if (!(s->flags & SF_FINST_MASK))
4435 s->flags |= finst;
4436}
4437
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004438void http_reply_and_close(struct stream *s, short status, struct http_reply *msg)
Christopher Faulet0f226952018-10-22 09:29:56 +02004439{
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004440 if (!msg) {
4441 channel_htx_truncate(&s->res, htxbuf(&s->res.buf));
4442 goto end;
4443 }
4444
4445 if (http_reply_message(s, msg) == -1) {
4446 /* On error, return a 500 error message, but don't rewrite it if
Christopher Faulet40e6b552020-06-25 16:04:50 +02004447 * it is already an internal error. If it was already a "const"
4448 * 500 error, just fail.
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004449 */
Christopher Faulet40e6b552020-06-25 16:04:50 +02004450 if (s->txn->status == 500) {
4451 if (s->txn->flags & TX_CONST_REPLY)
4452 goto end;
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004453 s->txn->flags |= TX_CONST_REPLY;
Christopher Faulet40e6b552020-06-25 16:04:50 +02004454 }
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004455 s->txn->status = 500;
4456 s->txn->http_reply = NULL;
4457 return http_reply_and_close(s, s->txn->status, http_error_message(s));
4458 }
4459
4460end:
4461 s->res.wex = tick_add_ifset(now_ms, s->res.wto);
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004462
Christopher Faulet0f226952018-10-22 09:29:56 +02004463 channel_auto_read(&s->req);
4464 channel_abort(&s->req);
4465 channel_auto_close(&s->req);
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004466 channel_htx_erase(&s->req, htxbuf(&s->req.buf));
Christopher Faulet72c7d8d2020-01-27 15:32:25 +01004467 channel_auto_read(&s->res);
4468 channel_auto_close(&s->res);
4469 channel_shutr_now(&s->res);
Christopher Faulet0f226952018-10-22 09:29:56 +02004470}
4471
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004472struct http_reply *http_error_message(struct stream *s)
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004473{
4474 const int msgnum = http_get_status_idx(s->txn->status);
4475
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004476 if (s->txn->http_reply)
4477 return s->txn->http_reply;
4478 else if (s->be->replies[msgnum])
4479 return s->be->replies[msgnum];
4480 else if (strm_fe(s)->replies[msgnum])
4481 return strm_fe(s)->replies[msgnum];
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004482 else
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004483 return &http_err_replies[msgnum];
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004484}
4485
Christopher Faulet40e6b552020-06-25 16:04:50 +02004486/* Produces an HTX message from an http reply. Depending on the http reply type,
4487 * a, errorfile, an raw file or a log-format string is used. On success, it
4488 * returns 0. If an error occurs -1 is returned. If it fails, this function only
4489 * exits. It is the caller responsibility to do the cleanup.
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004490 */
Christopher Fauletae43b6c2020-05-27 15:24:22 +02004491int http_reply_to_htx(struct stream *s, struct htx *htx, struct http_reply *reply)
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004492{
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004493 struct buffer *errmsg;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004494 struct htx_sl *sl;
4495 struct buffer *body = NULL;
4496 const char *status, *reason, *clen, *ctype;
4497 unsigned int slflags;
4498 int ret = 0;
4499
Christopher Faulete29a97e2020-05-14 14:49:25 +02004500 /*
4501 * - HTTP_REPLY_ERRFILES unexpected here. handled as no payload if so
4502 *
4503 * - HTTP_REPLY_INDIRECT: switch on another reply if defined or handled
4504 * as no payload if NULL. the TXN status code is set with the status
4505 * of the original reply.
4506 */
4507
4508 if (reply->type == HTTP_REPLY_INDIRECT) {
4509 if (reply->body.reply)
4510 reply = reply->body.reply;
4511 }
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004512 if (reply->type == HTTP_REPLY_ERRMSG && !reply->body.errmsg) {
4513 /* get default error message */
4514 if (reply == s->txn->http_reply)
4515 s->txn->http_reply = NULL;
4516 reply = http_error_message(s);
4517 if (reply->type == HTTP_REPLY_INDIRECT) {
4518 if (reply->body.reply)
4519 reply = reply->body.reply;
4520 }
4521 }
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004522
4523 if (reply->type == HTTP_REPLY_ERRMSG) {
4524 /* implicit or explicit error message*/
4525 errmsg = reply->body.errmsg;
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004526 if (errmsg && !b_is_null(errmsg)) {
Christopher Faulet20567362020-05-15 14:52:49 +02004527 if (!htx_copy_msg(htx, errmsg))
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004528 goto fail;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004529 }
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004530 }
4531 else {
4532 /* no payload, file or log-format string */
4533 if (reply->type == HTTP_REPLY_RAW) {
4534 /* file */
4535 body = &reply->body.obj;
4536 }
4537 else if (reply->type == HTTP_REPLY_LOGFMT) {
4538 /* log-format string */
4539 body = alloc_trash_chunk();
4540 if (!body)
4541 goto fail_alloc;
4542 body->data = build_logline(s, body->area, body->size, &reply->body.fmt);
4543 }
4544 /* else no payload */
4545
4546 status = ultoa(reply->status);
4547 reason = http_get_reason(reply->status);
4548 slflags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_CLEN);
4549 if (!body || !b_data(body))
4550 slflags |= HTX_SL_F_BODYLESS;
4551 sl = htx_add_stline(htx, HTX_BLK_RES_SL, slflags, ist("HTTP/1.1"), ist(status), ist(reason));
4552 if (!sl)
4553 goto fail;
4554 sl->info.res.status = reply->status;
4555
4556 clen = (body ? ultoa(b_data(body)) : "0");
4557 ctype = reply->ctype;
4558
4559 if (!LIST_ISEMPTY(&reply->hdrs)) {
4560 struct http_reply_hdr *hdr;
4561 struct buffer *value = alloc_trash_chunk();
4562
4563 if (!value)
4564 goto fail;
4565
4566 list_for_each_entry(hdr, &reply->hdrs, list) {
4567 chunk_reset(value);
4568 value->data = build_logline(s, value->area, value->size, &hdr->value);
4569 if (b_data(value) && !htx_add_header(htx, hdr->name, ist2(b_head(value), b_data(value)))) {
4570 free_trash_chunk(value);
4571 goto fail;
4572 }
4573 chunk_reset(value);
4574 }
4575 free_trash_chunk(value);
4576 }
4577
4578 if (!htx_add_header(htx, ist("content-length"), ist(clen)) ||
4579 (body && b_data(body) && ctype && !htx_add_header(htx, ist("content-type"), ist(ctype))) ||
4580 !htx_add_endof(htx, HTX_BLK_EOH) ||
4581 (body && b_data(body) && !htx_add_data_atonce(htx, ist2(b_head(body), b_data(body)))) ||
4582 !htx_add_endof(htx, HTX_BLK_EOM))
4583 goto fail;
4584 }
4585
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004586 leave:
4587 if (reply->type == HTTP_REPLY_LOGFMT)
4588 free_trash_chunk(body);
4589 return ret;
4590
4591 fail_alloc:
4592 if (!(s->flags & SF_ERR_MASK))
4593 s->flags |= SF_ERR_RESOURCE;
Christopher Faulet97e466c2020-05-15 15:12:47 +02004594 /* fall through */
4595 fail:
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004596 ret = -1;
4597 goto leave;
Christopher Faulet97e466c2020-05-15 15:12:47 +02004598}
4599
4600/* Send an http reply to the client. On success, it returns 0. If an error
Christopher Faulet40e6b552020-06-25 16:04:50 +02004601 * occurs -1 is returned and the response channel is truncated, removing this
4602 * way the faulty reply. This function may fail when the reply is formatted
4603 * (http_reply_to_htx) or when the reply is forwarded
4604 * (http_forward_proxy_resp). On the last case, it is because a
4605 * http-after-response rule fails.
Christopher Faulet97e466c2020-05-15 15:12:47 +02004606 */
4607int http_reply_message(struct stream *s, struct http_reply *reply)
4608{
4609 struct channel *res = &s->res;
4610 struct htx *htx = htx_from_buf(&res->buf);
4611
4612 if (s->txn->status == -1)
4613 s->txn->status = reply->status;
4614 channel_htx_truncate(res, htx);
4615
4616 if (http_reply_to_htx(s, htx, reply) == -1)
4617 goto fail;
4618
4619 htx_to_buf(htx, &s->res.buf);
4620 if (!http_forward_proxy_resp(s, 1))
4621 goto fail;
4622 return 0;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004623
4624 fail:
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004625 channel_htx_truncate(res, htx);
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004626 if (!(s->flags & SF_ERR_MASK))
4627 s->flags |= SF_ERR_PRXCOND;
Christopher Faulet97e466c2020-05-15 15:12:47 +02004628 return -1;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004629}
4630
Christopher Faulet304cc402019-07-15 15:46:28 +02004631/* Return the error message corresponding to si->err_type. It is assumed
4632 * that the server side is closed. Note that err_type is actually a
4633 * bitmask, where almost only aborts may be cumulated with other
4634 * values. We consider that aborted operations are more important
4635 * than timeouts or errors due to the fact that nobody else in the
4636 * logs might explain incomplete retries. All others should avoid
4637 * being cumulated. It should normally not be possible to have multiple
4638 * aborts at once, but just in case, the first one in sequence is reported.
4639 * Note that connection errors appearing on the second request of a keep-alive
4640 * connection are not reported since this allows the client to retry.
4641 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004642void http_return_srv_error(struct stream *s, struct stream_interface *si)
Christopher Faulet304cc402019-07-15 15:46:28 +02004643{
4644 int err_type = si->err_type;
4645
4646 /* set s->txn->status for http_error_message(s) */
4647 s->txn->status = 503;
4648
4649 if (err_type & SI_ET_QUEUE_ABRT)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004650 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_Q,
4651 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004652 else if (err_type & SI_ET_CONN_ABRT)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004653 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_C,
4654 (s->txn->flags & TX_NOT_FIRST) ? NULL :
4655 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004656 else if (err_type & SI_ET_QUEUE_TO)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004657 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_Q,
4658 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004659 else if (err_type & SI_ET_QUEUE_ERR)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004660 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_Q,
4661 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004662 else if (err_type & SI_ET_CONN_TO)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004663 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_C,
4664 (s->txn->flags & TX_NOT_FIRST) ? NULL :
4665 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004666 else if (err_type & SI_ET_CONN_ERR)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004667 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_C,
4668 (s->flags & SF_SRV_REUSED) ? NULL :
4669 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004670 else if (err_type & SI_ET_CONN_RES)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004671 http_server_error(s, si, SF_ERR_RESOURCE, SF_FINST_C,
4672 (s->txn->flags & TX_NOT_FIRST) ? NULL :
4673 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004674 else { /* SI_ET_CONN_OTHER and others */
4675 s->txn->status = 500;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004676 http_server_error(s, si, SF_ERR_INTERNAL, SF_FINST_C,
4677 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004678 }
4679}
4680
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004681
Christopher Faulet4a28a532019-03-01 11:19:40 +01004682/* Handle Expect: 100-continue for HTTP/1.1 messages if necessary. It returns 0
4683 * on success and -1 on error.
4684 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004685static int http_handle_expect_hdr(struct stream *s, struct htx *htx, struct http_msg *msg)
Christopher Faulet4a28a532019-03-01 11:19:40 +01004686{
4687 /* If we have HTTP/1.1 message with a body and Expect: 100-continue,
4688 * then we must send an HTTP/1.1 100 Continue intermediate response.
4689 */
4690 if (msg->msg_state == HTTP_MSG_BODY && (msg->flags & HTTP_MSGF_VER_11) &&
4691 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
4692 struct ist hdr = { .ptr = "Expect", .len = 6 };
4693 struct http_hdr_ctx ctx;
4694
4695 ctx.blk = NULL;
4696 /* Expect is allowed in 1.1, look for it */
4697 if (http_find_header(htx, hdr, &ctx, 0) &&
4698 unlikely(isteqi(ctx.value, ist2("100-continue", 12)))) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004699 if (http_reply_100_continue(s) == -1)
Christopher Faulet4a28a532019-03-01 11:19:40 +01004700 return -1;
4701 http_remove_header(htx, &ctx);
4702 }
4703 }
4704 return 0;
4705}
4706
Christopher Faulet23a3c792018-11-28 10:01:23 +01004707/* Send a 100-Continue response to the client. It returns 0 on success and -1
4708 * on error. The response channel is updated accordingly.
4709 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004710static int http_reply_100_continue(struct stream *s)
Christopher Faulet23a3c792018-11-28 10:01:23 +01004711{
4712 struct channel *res = &s->res;
4713 struct htx *htx = htx_from_buf(&res->buf);
4714 struct htx_sl *sl;
4715 unsigned int flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|
4716 HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
Christopher Faulet23a3c792018-11-28 10:01:23 +01004717
4718 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
4719 ist("HTTP/1.1"), ist("100"), ist("Continue"));
4720 if (!sl)
4721 goto fail;
4722 sl->info.res.status = 100;
4723
Christopher Faulet1d5ec092019-06-26 14:23:54 +02004724 if (!htx_add_endof(htx, HTX_BLK_EOH))
Christopher Faulet23a3c792018-11-28 10:01:23 +01004725 goto fail;
4726
Christopher Fauleta72a7e42020-01-28 09:28:11 +01004727 if (!http_forward_proxy_resp(s, 0))
4728 goto fail;
Christopher Faulet23a3c792018-11-28 10:01:23 +01004729 return 0;
4730
4731 fail:
4732 /* If an error occurred, remove the incomplete HTTP response from the
4733 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004734 channel_htx_truncate(res, htx);
Christopher Faulet23a3c792018-11-28 10:01:23 +01004735 return -1;
4736}
4737
Christopher Faulet12c51e22018-11-28 15:59:42 +01004738
Christopher Faulet0f226952018-10-22 09:29:56 +02004739/*
4740 * Capture headers from message <htx> according to header list <cap_hdr>, and
4741 * fill the <cap> pointers appropriately.
4742 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004743static void http_capture_headers(struct htx *htx, char **cap, struct cap_hdr *cap_hdr)
Christopher Faulet0f226952018-10-22 09:29:56 +02004744{
4745 struct cap_hdr *h;
4746 int32_t pos;
4747
Christopher Fauleta3f15502019-05-13 15:27:23 +02004748 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet0f226952018-10-22 09:29:56 +02004749 struct htx_blk *blk = htx_get_blk(htx, pos);
4750 enum htx_blk_type type = htx_get_blk_type(blk);
4751 struct ist n, v;
4752
4753 if (type == HTX_BLK_EOH)
4754 break;
4755 if (type != HTX_BLK_HDR)
4756 continue;
4757
4758 n = htx_get_blk_name(htx, blk);
4759
4760 for (h = cap_hdr; h; h = h->next) {
4761 if (h->namelen && (h->namelen == n.len) &&
4762 (strncasecmp(n.ptr, h->name, h->namelen) == 0)) {
4763 if (cap[h->index] == NULL)
4764 cap[h->index] =
4765 pool_alloc(h->pool);
4766
4767 if (cap[h->index] == NULL) {
4768 ha_alert("HTTP capture : out of memory.\n");
4769 break;
4770 }
4771
4772 v = htx_get_blk_value(htx, blk);
4773 if (v.len > h->len)
4774 v.len = h->len;
4775
4776 memcpy(cap[h->index], v.ptr, v.len);
4777 cap[h->index][v.len]=0;
4778 }
4779 }
4780 }
4781}
4782
Christopher Faulet0b6bdc52018-10-24 11:05:36 +02004783/* Delete a value in a header between delimiters <from> and <next>. The header
4784 * itself is delimited by <start> and <end> pointers. The number of characters
4785 * displaced is returned, and the pointer to the first delimiter is updated if
4786 * required. The function tries as much as possible to respect the following
4787 * principles :
4788 * - replace <from> delimiter by the <next> one unless <from> points to <start>,
4789 * in which case <next> is simply removed
4790 * - set exactly one space character after the new first delimiter, unless there
4791 * are not enough characters in the block being moved to do so.
4792 * - remove unneeded spaces before the previous delimiter and after the new
4793 * one.
4794 *
4795 * It is the caller's responsibility to ensure that :
4796 * - <from> points to a valid delimiter or <start> ;
4797 * - <next> points to a valid delimiter or <end> ;
4798 * - there are non-space chars before <from>.
4799 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004800static int http_del_hdr_value(char *start, char *end, char **from, char *next)
Christopher Faulet0b6bdc52018-10-24 11:05:36 +02004801{
4802 char *prev = *from;
4803
4804 if (prev == start) {
4805 /* We're removing the first value. eat the semicolon, if <next>
4806 * is lower than <end> */
4807 if (next < end)
4808 next++;
4809
4810 while (next < end && HTTP_IS_SPHT(*next))
4811 next++;
4812 }
4813 else {
4814 /* Remove useless spaces before the old delimiter. */
4815 while (HTTP_IS_SPHT(*(prev-1)))
4816 prev--;
4817 *from = prev;
4818
4819 /* copy the delimiter and if possible a space if we're
4820 * not at the end of the line.
4821 */
4822 if (next < end) {
4823 *prev++ = *next++;
4824 if (prev + 1 < next)
4825 *prev++ = ' ';
4826 while (next < end && HTTP_IS_SPHT(*next))
4827 next++;
4828 }
4829 }
4830 memmove(prev, next, end - next);
4831 return (prev - next);
4832}
4833
Christopher Faulet0f226952018-10-22 09:29:56 +02004834
4835/* Formats the start line of the request (without CRLF) and puts it in <str> and
Joseph Herlantc42c0e92018-11-25 10:43:27 -08004836 * return the written length. The line can be truncated if it exceeds <len>.
Christopher Faulet0f226952018-10-22 09:29:56 +02004837 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004838static size_t http_fmt_req_line(const struct htx_sl *sl, char *str, size_t len)
Christopher Faulet0f226952018-10-22 09:29:56 +02004839{
4840 struct ist dst = ist2(str, 0);
4841
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004842 if (istcat(&dst, htx_sl_req_meth(sl), len) == -1)
Christopher Faulet0f226952018-10-22 09:29:56 +02004843 goto end;
4844 if (dst.len + 1 > len)
4845 goto end;
4846 dst.ptr[dst.len++] = ' ';
4847
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004848 if (istcat(&dst, htx_sl_req_uri(sl), len) == -1)
Christopher Faulet0f226952018-10-22 09:29:56 +02004849 goto end;
4850 if (dst.len + 1 > len)
4851 goto end;
4852 dst.ptr[dst.len++] = ' ';
4853
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004854 istcat(&dst, htx_sl_req_vsn(sl), len);
Christopher Faulet0f226952018-10-22 09:29:56 +02004855 end:
4856 return dst.len;
4857}
4858
4859/*
4860 * Print a debug line with a start line.
4861 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004862static void http_debug_stline(const char *dir, struct stream *s, const struct htx_sl *sl)
Christopher Faulet0f226952018-10-22 09:29:56 +02004863{
4864 struct session *sess = strm_sess(s);
4865 int max;
4866
4867 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
4868 dir,
4869 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->handle.fd : -1,
4870 objt_cs(s->si[1].end) ? (unsigned short)objt_cs(s->si[1].end)->conn->handle.fd : -1);
4871
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004872 max = HTX_SL_P1_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02004873 UBOUND(max, trash.size - trash.data - 3);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004874 chunk_memcat(&trash, HTX_SL_P1_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02004875 trash.area[trash.data++] = ' ';
4876
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004877 max = HTX_SL_P2_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02004878 UBOUND(max, trash.size - trash.data - 2);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004879 chunk_memcat(&trash, HTX_SL_P2_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02004880 trash.area[trash.data++] = ' ';
4881
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004882 max = HTX_SL_P3_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02004883 UBOUND(max, trash.size - trash.data - 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004884 chunk_memcat(&trash, HTX_SL_P3_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02004885 trash.area[trash.data++] = '\n';
4886
Willy Tarreau2e8ab6b2020-03-14 11:03:20 +01004887 DISGUISE(write(1, trash.area, trash.data));
Christopher Faulet0f226952018-10-22 09:29:56 +02004888}
4889
4890/*
4891 * Print a debug line with a header.
4892 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004893static 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 +02004894{
4895 struct session *sess = strm_sess(s);
4896 int max;
4897
4898 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
4899 dir,
4900 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->handle.fd : -1,
4901 objt_cs(s->si[1].end) ? (unsigned short)objt_cs(s->si[1].end)->conn->handle.fd : -1);
4902
4903 max = n.len;
4904 UBOUND(max, trash.size - trash.data - 3);
4905 chunk_memcat(&trash, n.ptr, max);
4906 trash.area[trash.data++] = ':';
4907 trash.area[trash.data++] = ' ';
4908
4909 max = v.len;
4910 UBOUND(max, trash.size - trash.data - 1);
4911 chunk_memcat(&trash, v.ptr, max);
4912 trash.area[trash.data++] = '\n';
4913
Willy Tarreau2e8ab6b2020-03-14 11:03:20 +01004914 DISGUISE(write(1, trash.area, trash.data));
Christopher Faulet0f226952018-10-22 09:29:56 +02004915}
4916
Christopher Fauleta8a46e22019-07-16 14:53:09 +02004917/* Allocate a new HTTP transaction for stream <s> unless there is one already.
4918 * In case of allocation failure, everything allocated is freed and NULL is
4919 * returned. Otherwise the new transaction is assigned to the stream and
4920 * returned.
4921 */
4922struct http_txn *http_alloc_txn(struct stream *s)
4923{
4924 struct http_txn *txn = s->txn;
4925
4926 if (txn)
4927 return txn;
4928
4929 txn = pool_alloc(pool_head_http_txn);
4930 if (!txn)
4931 return txn;
4932
4933 s->txn = txn;
4934 return txn;
4935}
4936
4937void http_txn_reset_req(struct http_txn *txn)
4938{
Christopher Faulet1aea50e2020-01-17 16:03:53 +01004939 txn->req.flags = 0;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02004940 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
4941}
4942
4943void http_txn_reset_res(struct http_txn *txn)
4944{
Christopher Faulet1aea50e2020-01-17 16:03:53 +01004945 txn->rsp.flags = 0;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02004946 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
4947}
4948
4949/*
4950 * Initialize a new HTTP transaction for stream <s>. It is assumed that all
4951 * the required fields are properly allocated and that we only need to (re)init
4952 * them. This should be used before processing any new request.
4953 */
4954void http_init_txn(struct stream *s)
4955{
4956 struct http_txn *txn = s->txn;
4957 struct conn_stream *cs = objt_cs(s->si[0].end);
4958
Christopher Fauletda831fa2020-10-06 17:58:43 +02004959 txn->flags = ((cs && cs->flags & CS_FL_NOT_FIRST) ? TX_NOT_FIRST : 0);
Christopher Fauleta8a46e22019-07-16 14:53:09 +02004960 txn->status = -1;
Christopher Faulet5cb513a2020-05-13 17:56:56 +02004961 txn->http_reply = NULL;
Willy Tarreau8b507582020-02-25 09:35:07 +01004962 write_u32(txn->cache_hash, 0);
Christopher Fauleta8a46e22019-07-16 14:53:09 +02004963
4964 txn->cookie_first_date = 0;
4965 txn->cookie_last_date = 0;
4966
4967 txn->srv_cookie = NULL;
4968 txn->cli_cookie = NULL;
4969 txn->uri = NULL;
4970
4971 http_txn_reset_req(txn);
4972 http_txn_reset_res(txn);
4973
4974 txn->req.chn = &s->req;
4975 txn->rsp.chn = &s->res;
4976
4977 txn->auth.method = HTTP_AUTH_UNKNOWN;
4978
4979 vars_init(&s->vars_txn, SCOPE_TXN);
4980 vars_init(&s->vars_reqres, SCOPE_REQ);
4981}
4982
4983/* to be used at the end of a transaction */
4984void http_end_txn(struct stream *s)
4985{
4986 struct http_txn *txn = s->txn;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02004987
4988 /* these ones will have been dynamically allocated */
4989 pool_free(pool_head_requri, txn->uri);
4990 pool_free(pool_head_capture, txn->cli_cookie);
4991 pool_free(pool_head_capture, txn->srv_cookie);
Tim Duesterhusa17e6622020-03-05 20:19:02 +01004992 pool_free(pool_head_uniqueid, s->unique_id.ptr);
Christopher Fauleta8a46e22019-07-16 14:53:09 +02004993
Tim Duesterhusa17e6622020-03-05 20:19:02 +01004994 s->unique_id = IST_NULL;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02004995 txn->uri = NULL;
4996 txn->srv_cookie = NULL;
4997 txn->cli_cookie = NULL;
4998
Christopher Faulet59399252019-11-07 14:27:52 +01004999 if (!LIST_ISEMPTY(&s->vars_txn.head))
5000 vars_prune(&s->vars_txn, s->sess, s);
5001 if (!LIST_ISEMPTY(&s->vars_reqres.head))
5002 vars_prune(&s->vars_reqres, s->sess, s);
5003}
5004
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005005
5006DECLARE_POOL(pool_head_http_txn, "http_txn", sizeof(struct http_txn));
Christopher Faulet0f226952018-10-22 09:29:56 +02005007
Christopher Fauletf4eb75d2018-10-11 15:55:07 +02005008__attribute__((constructor))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005009static void __http_protocol_init(void)
Christopher Fauletf4eb75d2018-10-11 15:55:07 +02005010{
5011}
5012
5013
5014/*
5015 * Local variables:
5016 * c-indent-level: 8
5017 * c-basic-offset: 8
5018 * End:
5019 */