blob: 8f8ecce88c5e09a4f81ec44e32edd7044ee97015 [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 */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +0100771 if ((sess->listener->options & LI_O_NOQUICKACK) && !(htx->flags & HTX_FL_EOM))
Willy Tarreau1a18b542018-12-11 16:37:42 +0100772 conn_set_quickack(cli_conn, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200773
774 /*************************************************************
775 * OK, that's finished for the headers. We have done what we *
776 * could. Let's switch to the DATA state. *
777 ************************************************************/
778 req->analyse_exp = TICK_ETERNITY;
779 req->analysers &= ~an_bit;
780
781 s->logs.tv_request = now;
782 /* OK let's go on with the BODY now */
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100783 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200784 return 1;
785
Christopher Fauletb8a53712019-12-16 11:29:38 +0100786 return_int_err:
787 txn->status = 500;
788 if (!(s->flags & SF_ERR_MASK))
789 s->flags |= SF_ERR_INTERNAL;
790 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100791 if (s->flags & SF_BE_ASSIGNED)
Christopher Fauletbe20cf32020-01-24 11:41:38 +0100792 _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100793 if (sess->listener->counters)
794 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
795 goto return_prx_cond;
796
Christopher Faulete0768eb2018-10-03 16:38:02 +0200797 return_bad_req: /* let's centralize all bad requests */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200798 txn->status = 400;
Olivier Houcharda798bf52019-03-08 18:52:00 +0100799 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200800 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100801 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100802 /* fall through */
803
804 return_prx_cond:
805 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +0200806
807 if (!(s->flags & SF_ERR_MASK))
808 s->flags |= SF_ERR_PRXCOND;
809 if (!(s->flags & SF_FINST_MASK))
810 s->flags |= SF_FINST_R;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100811
812 req->analysers &= AN_REQ_FLT_END;
813 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100814 DBG_TRACE_DEVEL("leaving on error",
815 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200816 return 0;
817}
818
819/* This function is an analyser which processes the HTTP tarpit. It always
820 * returns zero, at the beginning because it prevents any other processing
821 * from occurring, and at the end because it terminates the request.
822 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200823int http_process_tarpit(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200824{
825 struct http_txn *txn = s->txn;
826
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100827 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, &txn->req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200828 /* This connection is being tarpitted. The CLIENT side has
829 * already set the connect expiration date to the right
830 * timeout. We just have to check that the client is still
831 * there and that the timeout has not expired.
832 */
833 channel_dont_connect(req);
834 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100835 !tick_is_expired(req->analyse_exp, now_ms)) {
836 DBG_TRACE_DEVEL("waiting for tarpit timeout expiry",
837 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200838 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100839 }
840
Christopher Faulete0768eb2018-10-03 16:38:02 +0200841
842 /* We will set the queue timer to the time spent, just for
843 * logging purposes. We fake a 500 server error, so that the
844 * attacker will not suspect his connection has been tarpitted.
845 * It will not cause trouble to the logs because we can exclude
846 * the tarpitted connections by filtering on the 'PT' status flags.
847 */
848 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
849
Christopher Faulet8dfeccf2020-05-15 14:16:29 +0200850 http_reply_and_close(s, txn->status, (!(req->flags & CF_READ_ERROR) ? http_error_message(s) : NULL));
Christopher Faulet5cb513a2020-05-13 17:56:56 +0200851
852 end:
Christopher Faulete0768eb2018-10-03 16:38:02 +0200853 req->analysers &= AN_REQ_FLT_END;
854 req->analyse_exp = TICK_ETERNITY;
855
856 if (!(s->flags & SF_ERR_MASK))
857 s->flags |= SF_ERR_PRXCOND;
858 if (!(s->flags & SF_FINST_MASK))
859 s->flags |= SF_FINST_T;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100860
861 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200862 return 0;
863}
864
865/* This function is an analyser which waits for the HTTP request body. It waits
866 * for either the buffer to be full, or the full advertised contents to have
867 * reached the buffer. It must only be called after the standard HTTP request
868 * processing has occurred, because it expects the request to be parsed and will
869 * look for the Expect header. It may send a 100-Continue interim response. It
870 * takes in input any state starting from HTTP_MSG_BODY and leaves with one of
871 * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it
872 * needs to read more data, or 1 once it has completed its analysis.
873 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200874int http_wait_for_request_body(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200875{
876 struct session *sess = s->sess;
877 struct http_txn *txn = s->txn;
878 struct http_msg *msg = &s->txn->req;
Christopher Fauletf76ebe82018-10-24 11:16:22 +0200879 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200880
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100881 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Fauletf76ebe82018-10-24 11:16:22 +0200882
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100883 htx = htxbuf(&req->buf);
Christopher Fauletf76ebe82018-10-24 11:16:22 +0200884
Willy Tarreau4236f032019-03-05 10:43:32 +0100885 if (htx->flags & HTX_FL_PARSING_ERROR)
886 goto return_bad_req;
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200887 if (htx->flags & HTX_FL_PROCESSING_ERROR)
888 goto return_int_err;
Willy Tarreau4236f032019-03-05 10:43:32 +0100889
Christopher Fauletc31bc722020-11-20 14:30:38 +0100890 /* Do nothing for bodyless and CONNECT requests */
891 if (txn->meth == HTTP_METH_CONNECT || (msg->flags & HTTP_MSGF_BODYLESS))
Christopher Faulet63c69a92020-11-16 16:03:35 +0100892 goto http_end;
893
Christopher Faulete0768eb2018-10-03 16:38:02 +0200894 /* We have to parse the HTTP request body to find any required data.
895 * "balance url_param check_post" should have been the only way to get
896 * into this. We were brought here after HTTP header analysis, so all
897 * related structures are ready.
898 */
899
Christopher Fauletf76ebe82018-10-24 11:16:22 +0200900 if (msg->msg_state < HTTP_MSG_DATA) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200901 if (http_handle_expect_hdr(s, htx, msg) == -1)
Christopher Fauletb8a53712019-12-16 11:29:38 +0100902 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200903 }
904
Christopher Fauletf76ebe82018-10-24 11:16:22 +0200905 msg->msg_state = HTTP_MSG_DATA;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200906
Christopher Fauletf76ebe82018-10-24 11:16:22 +0200907 /* Now we're in HTTP_MSG_DATA. We just need to know if all data have
908 * been received or if the buffer is full.
Christopher Faulete0768eb2018-10-03 16:38:02 +0200909 */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +0100910 if ((htx->flags & HTX_FL_EOM) || htx_get_tail_type(htx) > HTX_BLK_DATA ||
Christopher Fauletdcd8c5e2019-01-21 11:24:38 +0100911 channel_htx_full(req, htx, global.tune.maxrewrite))
Christopher Faulete0768eb2018-10-03 16:38:02 +0200912 goto http_end;
913
914 if ((req->flags & CF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
915 txn->status = 408;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200916 if (!(s->flags & SF_ERR_MASK))
917 s->flags |= SF_ERR_CLITO;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100918 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
919 if (sess->listener->counters)
920 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
921 goto return_prx_cond;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200922 }
923
924 /* we get here if we need to wait for more data */
925 if (!(req->flags & (CF_SHUTR | CF_READ_ERROR))) {
926 /* Not enough data. We'll re-use the http-request
927 * timeout here. Ideally, we should set the timeout
928 * relative to the accept() date. We just set the
929 * request timeout once at the beginning of the
930 * request.
931 */
932 channel_dont_connect(req);
933 if (!tick_isset(req->analyse_exp))
934 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100935 DBG_TRACE_DEVEL("waiting for more data",
936 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200937 return 0;
938 }
939
940 http_end:
941 /* The situation will not evolve, so let's give up on the analysis. */
942 s->logs.tv_request = now; /* update the request timer to reflect full request */
943 req->analysers &= ~an_bit;
944 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100945 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200946 return 1;
947
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200948 return_int_err:
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200949 txn->status = 500;
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200950 if (!(s->flags & SF_ERR_MASK))
951 s->flags |= SF_ERR_INTERNAL;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100952 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100953 if (s->flags & SF_BE_ASSIGNED)
Christopher Fauletbe20cf32020-01-24 11:41:38 +0100954 _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100955 if (sess->listener->counters)
956 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
957 goto return_prx_cond;
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200958
Christopher Faulete0768eb2018-10-03 16:38:02 +0200959 return_bad_req: /* let's centralize all bad requests */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200960 txn->status = 400;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100961 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
962 if (sess->listener->counters)
963 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
964 /* fall through */
965
966 return_prx_cond:
967 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +0200968
969 if (!(s->flags & SF_ERR_MASK))
970 s->flags |= SF_ERR_PRXCOND;
971 if (!(s->flags & SF_FINST_MASK))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100972 s->flags |= (msg->msg_state < HTTP_MSG_DATA ? SF_FINST_R : SF_FINST_D);
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200973
Christopher Faulete0768eb2018-10-03 16:38:02 +0200974 req->analysers &= AN_REQ_FLT_END;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100975 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100976 DBG_TRACE_DEVEL("leaving on error",
977 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200978 return 0;
979}
980
981/* This function is an analyser which forwards request body (including chunk
982 * sizes if any). It is called as soon as we must forward, even if we forward
983 * zero byte. The only situation where it must not be called is when we're in
984 * tunnel mode and we want to forward till the close. It's used both to forward
985 * remaining data and to resync after end of body. It expects the msg_state to
986 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
987 * read more data, or 1 once we can go on with next request or end the stream.
988 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
989 * bytes of pending data + the headers if not already done.
990 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200991int http_request_forward_body(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200992{
993 struct session *sess = s->sess;
994 struct http_txn *txn = s->txn;
Christopher Faulet9768c262018-10-22 09:34:31 +0200995 struct http_msg *msg = &txn->req;
996 struct htx *htx;
Christopher Faulet93e02d82019-03-08 14:18:50 +0100997 short status = 0;
Christopher Fauletaed82cf2018-11-30 22:22:32 +0100998 int ret;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200999
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001000 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001001
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001002 htx = htxbuf(&req->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001003
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001004 if (htx->flags & HTX_FL_PARSING_ERROR)
1005 goto return_bad_req;
1006 if (htx->flags & HTX_FL_PROCESSING_ERROR)
1007 goto return_int_err;
1008
Christopher Faulete0768eb2018-10-03 16:38:02 +02001009 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
1010 ((req->flags & CF_SHUTW) && (req->to_forward || co_data(req)))) {
1011 /* Output closed while we were sending data. We must abort and
1012 * wake the other side up.
1013 */
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001014
Olivier Houchard29cac3c2019-07-12 15:48:58 +02001015 /* Don't abort yet if we had L7 retries activated and it
1016 * was a write error, we may recover.
1017 */
1018 if (!(req->flags & (CF_READ_ERROR | CF_READ_TIMEOUT)) &&
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001019 (s->si[1].flags & SI_FL_L7_RETRY)) {
1020 DBG_TRACE_DEVEL("leaving on L7 retry",
1021 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Olivier Houchard29cac3c2019-07-12 15:48:58 +02001022 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001023 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001024 msg->msg_state = HTTP_MSG_ERROR;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001025 http_end_request(s);
1026 http_end_response(s);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001027 DBG_TRACE_DEVEL("leaving on error",
1028 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001029 return 1;
1030 }
1031
1032 /* Note that we don't have to send 100-continue back because we don't
1033 * need the data to complete our job, and it's up to the server to
1034 * decide whether to return 100, 417 or anything else in return of
1035 * an "Expect: 100-continue" header.
1036 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001037 if (msg->msg_state == HTTP_MSG_BODY)
1038 msg->msg_state = HTTP_MSG_DATA;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001039
Christopher Faulete0768eb2018-10-03 16:38:02 +02001040 /* in most states, we should abort in case of early close */
1041 channel_auto_close(req);
1042
1043 if (req->to_forward) {
Christopher Faulet66af0b22019-03-22 14:54:52 +01001044 if (req->to_forward == CHN_INFINITE_FORWARD) {
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001045 if (req->flags & CF_EOI)
1046 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet66af0b22019-03-22 14:54:52 +01001047 }
1048 else {
1049 /* We can't process the buffer's contents yet */
1050 req->flags |= CF_WAKE_WRITE;
1051 goto missing_data_or_waiting;
1052 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001053 }
1054
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001055 if (msg->msg_state >= HTTP_MSG_ENDING)
1056 goto ending;
1057
1058 if (txn->meth == HTTP_METH_CONNECT) {
1059 msg->msg_state = HTTP_MSG_ENDING;
1060 goto ending;
1061 }
1062
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001063 /* Forward input data. We get it by removing all outgoing data not
1064 * forwarded yet from HTX data size. If there are some data filters, we
1065 * let them decide the amount of data to forward.
Christopher Faulet9768c262018-10-22 09:34:31 +02001066 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001067 if (HAS_REQ_DATA_FILTERS(s)) {
1068 ret = flt_http_payload(s, msg, htx->data);
1069 if (ret < 0)
1070 goto return_bad_req;
Christopher Faulet421e7692019-06-13 11:16:45 +02001071 c_adv(req, ret);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001072 }
1073 else {
Christopher Faulet421e7692019-06-13 11:16:45 +02001074 c_adv(req, htx->data - co_data(req));
Christopher Faulet66af0b22019-03-22 14:54:52 +01001075 if (msg->flags & HTTP_MSGF_XFER_LEN)
1076 channel_htx_forward_forever(req, htx);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001077 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001078
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001079 if (htx->data != co_data(req))
1080 goto missing_data_or_waiting;
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001081
Christopher Faulet9768c262018-10-22 09:34:31 +02001082 /* Check if the end-of-message is reached and if so, switch the message
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001083 * in HTTP_MSG_ENDING state. Then if all data was marked to be
1084 * forwarded, set the state to HTTP_MSG_DONE.
Christopher Faulet9768c262018-10-22 09:34:31 +02001085 */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001086 if (!(htx->flags & HTX_FL_EOM))
Christopher Faulet9768c262018-10-22 09:34:31 +02001087 goto missing_data_or_waiting;
1088
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001089 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet9768c262018-10-22 09:34:31 +02001090
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001091 ending:
Christopher Faulet2151cdd2020-07-22 16:34:59 +02001092 req->flags &= ~CF_EXPECT_MORE; /* no more data are expected */
1093
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001094 /* other states, ENDING...TUNNEL */
1095 if (msg->msg_state >= HTTP_MSG_DONE)
1096 goto done;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001097
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001098 if (HAS_REQ_DATA_FILTERS(s)) {
1099 ret = flt_http_end(s, msg);
1100 if (ret <= 0) {
1101 if (!ret)
1102 goto missing_data_or_waiting;
1103 goto return_bad_req;
1104 }
1105 }
1106
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001107 if (txn->meth == HTTP_METH_CONNECT)
1108 msg->msg_state = HTTP_MSG_TUNNEL;
1109 else {
1110 msg->msg_state = HTTP_MSG_DONE;
1111 req->to_forward = 0;
1112 }
1113
1114 done:
1115 /* we don't want to forward closes on DONE except in tunnel mode. */
1116 if (!(txn->flags & TX_CON_WANT_TUN))
1117 channel_dont_close(req);
1118
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001119 http_end_request(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001120 if (!(req->analysers & an_bit)) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001121 http_end_response(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001122 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
1123 if (req->flags & CF_SHUTW) {
1124 /* request errors are most likely due to the
1125 * server aborting the transfer. */
Christopher Faulet93e02d82019-03-08 14:18:50 +01001126 goto return_srv_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001127 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001128 goto return_bad_req;
1129 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001130 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001131 return 1;
1132 }
1133
1134 /* If "option abortonclose" is set on the backend, we want to monitor
1135 * the client's connection and forward any shutdown notification to the
1136 * server, which will decide whether to close or to go on processing the
1137 * request. We only do that in tunnel mode, and not in other modes since
1138 * it can be abused to exhaust source ports. */
Christopher Faulet769d0e92019-03-22 14:23:18 +01001139 if (s->be->options & PR_O_ABRT_CLOSE) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001140 channel_auto_read(req);
Christopher Fauletc41547b2019-07-16 14:32:23 +02001141 if ((req->flags & (CF_SHUTR|CF_READ_NULL)) && !(txn->flags & TX_CON_WANT_TUN))
Christopher Faulete0768eb2018-10-03 16:38:02 +02001142 s->si[1].flags |= SI_FL_NOLINGER;
1143 channel_auto_close(req);
1144 }
1145 else if (s->txn->meth == HTTP_METH_POST) {
1146 /* POST requests may require to read extra CRLF sent by broken
1147 * browsers and which could cause an RST to be sent upon close
1148 * on some systems (eg: Linux). */
1149 channel_auto_read(req);
1150 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001151 DBG_TRACE_DEVEL("waiting for the end of the HTTP txn",
1152 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001153 return 0;
1154
1155 missing_data_or_waiting:
1156 /* stop waiting for data if the input is closed before the end */
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001157 if (msg->msg_state < HTTP_MSG_ENDING && req->flags & CF_SHUTR)
Christopher Faulet93e02d82019-03-08 14:18:50 +01001158 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001159
1160 waiting:
1161 /* waiting for the last bits to leave the buffer */
1162 if (req->flags & CF_SHUTW)
Christopher Faulet93e02d82019-03-08 14:18:50 +01001163 goto return_srv_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001164
1165 /* When TE: chunked is used, we need to get there again to parse remaining
1166 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
1167 * And when content-length is used, we never want to let the possible
1168 * shutdown be forwarded to the other side, as the state machine will
1169 * take care of it once the client responds. It's also important to
1170 * prevent TIME_WAITs from accumulating on the backend side, and for
1171 * HTTP/2 where the last frame comes with a shutdown.
1172 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001173 if (msg->flags & HTTP_MSGF_XFER_LEN)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001174 channel_dont_close(req);
1175
1176 /* We know that more data are expected, but we couldn't send more that
1177 * what we did. So we always set the CF_EXPECT_MORE flag so that the
1178 * system knows it must not set a PUSH on this first part. Interactive
1179 * modes are already handled by the stream sock layer. We must not do
1180 * this in content-length mode because it could present the MSG_MORE
1181 * flag with the last block of forwarded data, which would cause an
1182 * additional delay to be observed by the receiver.
1183 */
Christopher Faulet2151cdd2020-07-22 16:34:59 +02001184 if (HAS_REQ_DATA_FILTERS(s))
Christopher Faulete0768eb2018-10-03 16:38:02 +02001185 req->flags |= CF_EXPECT_MORE;
1186
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001187 DBG_TRACE_DEVEL("waiting for more data to forward",
1188 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001189 return 0;
1190
Christopher Faulet93e02d82019-03-08 14:18:50 +01001191 return_cli_abort:
1192 _HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
1193 _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001194 if (sess->listener->counters)
1195 _HA_ATOMIC_ADD(&sess->listener->counters->cli_aborts, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001196 if (objt_server(s->target))
Christopher Fauletcff0f732019-12-16 16:13:44 +01001197 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.cli_aborts, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001198 if (!(s->flags & SF_ERR_MASK))
1199 s->flags |= SF_ERR_CLICL;
1200 status = 400;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001201 goto return_prx_cond;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001202
1203 return_srv_abort:
1204 _HA_ATOMIC_ADD(&sess->fe->fe_counters.srv_aborts, 1);
1205 _HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001206 if (sess->listener->counters)
1207 _HA_ATOMIC_ADD(&sess->listener->counters->srv_aborts, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001208 if (objt_server(s->target))
Christopher Fauletcff0f732019-12-16 16:13:44 +01001209 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.srv_aborts, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001210 if (!(s->flags & SF_ERR_MASK))
1211 s->flags |= SF_ERR_SRVCL;
1212 status = 502;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001213 goto return_prx_cond;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001214
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001215 return_int_err:
1216 if (!(s->flags & SF_ERR_MASK))
1217 s->flags |= SF_ERR_INTERNAL;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001218 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001219 _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01001220 if (sess->listener->counters)
1221 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001222 if (objt_server(s->target))
1223 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.internal_errors, 1);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001224 status = 500;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001225 goto return_prx_cond;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001226
Christopher Faulet93e02d82019-03-08 14:18:50 +01001227 return_bad_req:
Olivier Houcharda798bf52019-03-08 18:52:00 +01001228 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001229 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +01001230 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001231 status = 400;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001232 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001233
Christopher Fauletb8a53712019-12-16 11:29:38 +01001234 return_prx_cond:
Christopher Faulet9768c262018-10-22 09:34:31 +02001235 if (txn->status > 0) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001236 /* Note: we don't send any error if some data were already sent */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001237 http_reply_and_close(s, txn->status, NULL);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001238 } else {
Christopher Faulet93e02d82019-03-08 14:18:50 +01001239 txn->status = status;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001240 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001241 }
1242 req->analysers &= AN_REQ_FLT_END;
1243 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 +01001244 if (!(s->flags & SF_ERR_MASK))
1245 s->flags |= SF_ERR_PRXCOND;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001246 if (!(s->flags & SF_FINST_MASK))
1247 s->flags |= ((txn->rsp.msg_state < HTTP_MSG_ERROR) ? SF_FINST_H : SF_FINST_D);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001248 DBG_TRACE_DEVEL("leaving on error ",
1249 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001250 return 0;
1251}
1252
Olivier Houcharda254a372019-04-05 15:30:12 +02001253/* Reset the stream and the backend stream_interface to a situation suitable for attemption connection */
1254/* Returns 0 if we can attempt to retry, -1 otherwise */
1255static __inline int do_l7_retry(struct stream *s, struct stream_interface *si)
1256{
1257 struct channel *req, *res;
1258 int co_data;
1259
1260 si->conn_retries--;
1261 if (si->conn_retries < 0)
1262 return -1;
1263
Willy Tarreau223995e2019-05-04 10:38:31 +02001264 if (objt_server(s->target))
1265 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.retries, 1);
1266 _HA_ATOMIC_ADD(&s->be->be_counters.retries, 1);
1267
Olivier Houcharda254a372019-04-05 15:30:12 +02001268 req = &s->req;
1269 res = &s->res;
1270 /* Remove any write error from the request, and read error from the response */
1271 req->flags &= ~(CF_WRITE_ERROR | CF_WRITE_TIMEOUT | CF_SHUTW | CF_SHUTW_NOW);
1272 res->flags &= ~(CF_READ_ERROR | CF_READ_TIMEOUT | CF_SHUTR | CF_EOI | CF_READ_NULL | CF_SHUTR_NOW);
1273 res->analysers = 0;
1274 si->flags &= ~(SI_FL_ERR | SI_FL_EXP | SI_FL_RXBLK_SHUT);
Olivier Houchard8cabc972020-05-12 22:18:14 +02001275 s->flags &= ~SF_ADDR_SET;
Olivier Houchard4bd58672019-07-12 16:16:59 +02001276 stream_choose_redispatch(s);
Olivier Houcharda254a372019-04-05 15:30:12 +02001277 si->exp = TICK_ETERNITY;
1278 res->rex = TICK_ETERNITY;
1279 res->to_forward = 0;
1280 res->analyse_exp = TICK_ETERNITY;
1281 res->total = 0;
Olivier Houchard4bd58672019-07-12 16:16:59 +02001282 s->flags &= ~(SF_ERR_SRVTO | SF_ERR_SRVCL);
Olivier Houcharda254a372019-04-05 15:30:12 +02001283 si_release_endpoint(&s->si[1]);
1284 b_free(&req->buf);
1285 /* Swap the L7 buffer with the channel buffer */
1286 /* We know we stored the co_data as b_data, so get it there */
1287 co_data = b_data(&si->l7_buffer);
1288 b_set_data(&si->l7_buffer, b_size(&si->l7_buffer));
1289 b_xfer(&req->buf, &si->l7_buffer, b_data(&si->l7_buffer));
1290
1291 co_set_data(req, co_data);
1292 b_reset(&res->buf);
1293 co_set_data(res, 0);
1294 return 0;
1295}
1296
Christopher Faulete0768eb2018-10-03 16:38:02 +02001297/* This stream analyser waits for a complete HTTP response. It returns 1 if the
1298 * processing can continue on next analysers, or zero if it either needs more
1299 * data or wants to immediately abort the response (eg: timeout, error, ...). It
1300 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->res.analysers
1301 * when it has nothing left to do, and may remove any analyser when it wants to
1302 * abort.
1303 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001304int http_wait_for_response(struct stream *s, struct channel *rep, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001305{
Christopher Faulet9768c262018-10-22 09:34:31 +02001306 /*
1307 * We will analyze a complete HTTP response to check the its syntax.
1308 *
1309 * Once the start line and all headers are received, we may perform a
1310 * capture of the error (if any), and we will set a few fields. We also
1311 * logging and finally headers capture.
1312 */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001313 struct session *sess = s->sess;
1314 struct http_txn *txn = s->txn;
1315 struct http_msg *msg = &txn->rsp;
Christopher Faulet9768c262018-10-22 09:34:31 +02001316 struct htx *htx;
Olivier Houcharda254a372019-04-05 15:30:12 +02001317 struct stream_interface *si_b = &s->si[1];
Christopher Faulet61608322018-11-23 16:23:45 +01001318 struct connection *srv_conn;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001319 struct htx_sl *sl;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001320 int n;
1321
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001322 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001323
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001324 htx = htxbuf(&rep->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001325
Willy Tarreau4236f032019-03-05 10:43:32 +01001326 /* Parsing errors are caught here */
1327 if (htx->flags & HTX_FL_PARSING_ERROR)
1328 goto return_bad_res;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001329 if (htx->flags & HTX_FL_PROCESSING_ERROR)
1330 goto return_int_err;
Willy Tarreau4236f032019-03-05 10:43:32 +01001331
Christopher Faulete0768eb2018-10-03 16:38:02 +02001332 /*
1333 * Now we quickly check if we have found a full valid response.
1334 * If not so, we check the FD and buffer states before leaving.
1335 * A full response is indicated by the fact that we have seen
1336 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
1337 * responses are checked first.
1338 *
1339 * Depending on whether the client is still there or not, we
1340 * may send an error response back or not. Note that normally
1341 * we should only check for HTTP status there, and check I/O
1342 * errors somewhere else.
1343 */
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001344 next_one:
Christopher Faulet29f17582019-05-23 11:03:26 +02001345 if (unlikely(htx_is_empty(htx) || htx->first == -1)) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001346 /* 1: have we encountered a read error ? */
1347 if (rep->flags & CF_READ_ERROR) {
Olivier Houchard865d8392019-05-03 22:46:27 +02001348 struct connection *conn = NULL;
1349
Olivier Houchard865d8392019-05-03 22:46:27 +02001350 if (objt_cs(s->si[1].end))
1351 conn = objt_cs(s->si[1].end)->conn;
1352
1353 if (si_b->flags & SI_FL_L7_RETRY &&
1354 (!conn || conn->err_code != CO_ER_SSL_EARLY_FAILED)) {
Olivier Houcharda254a372019-04-05 15:30:12 +02001355 /* If we arrive here, then CF_READ_ERROR was
1356 * set by si_cs_recv() because we matched a
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05001357 * status, otherwise it would have removed
Olivier Houcharda254a372019-04-05 15:30:12 +02001358 * the SI_FL_L7_RETRY flag, so it's ok not
1359 * to check s->be->retry_type.
1360 */
1361 if (co_data(rep) || do_l7_retry(s, si_b) == 0)
1362 return 0;
1363 }
1364
Olivier Houchard6db16992019-05-17 15:40:49 +02001365 if (txn->flags & TX_NOT_FIRST)
1366 goto abort_keep_alive;
1367
Olivier Houcharda798bf52019-03-08 18:52:00 +01001368 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001369 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001370 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001371 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001372 }
1373
Christopher Faulete0768eb2018-10-03 16:38:02 +02001374 rep->analysers &= AN_RES_FLT_END;
1375 txn->status = 502;
1376
1377 /* Check to see if the server refused the early data.
1378 * If so, just send a 425
1379 */
Willy Tarreauee99aaf2020-06-23 05:58:20 +02001380 if (conn && conn->err_code == CO_ER_SSL_EARLY_FAILED) {
Olivier Houchard865d8392019-05-03 22:46:27 +02001381 if ((s->be->retry_type & PR_RE_EARLY_ERROR) &&
Olivier Houchardad26d8d2019-05-10 17:48:28 +02001382 (si_b->flags & SI_FL_L7_RETRY) &&
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001383 do_l7_retry(s, si_b) == 0) {
1384 DBG_TRACE_DEVEL("leaving on L7 retry",
1385 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houchard865d8392019-05-03 22:46:27 +02001386 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001387 }
Olivier Houchard865d8392019-05-03 22:46:27 +02001388 txn->status = 425;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001389 }
1390
1391 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001392 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001393
1394 if (!(s->flags & SF_ERR_MASK))
1395 s->flags |= SF_ERR_SRVCL;
1396 if (!(s->flags & SF_FINST_MASK))
1397 s->flags |= SF_FINST_H;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001398 DBG_TRACE_DEVEL("leaving on error",
1399 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001400 return 0;
1401 }
1402
Christopher Faulet9768c262018-10-22 09:34:31 +02001403 /* 2: read timeout : return a 504 to the client. */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001404 else if (rep->flags & CF_READ_TIMEOUT) {
Olivier Houcharda254a372019-04-05 15:30:12 +02001405 if ((si_b->flags & SI_FL_L7_RETRY) &&
1406 (s->be->retry_type & PR_RE_TIMEOUT)) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001407 if (co_data(rep) || do_l7_retry(s, si_b) == 0) {
1408 DBG_TRACE_DEVEL("leaving on L7 retry",
1409 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharda254a372019-04-05 15:30:12 +02001410 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001411 }
Olivier Houcharda254a372019-04-05 15:30:12 +02001412 }
Olivier Houcharda798bf52019-03-08 18:52:00 +01001413 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001414 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001415 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001416 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001417 }
1418
Christopher Faulete0768eb2018-10-03 16:38:02 +02001419 rep->analysers &= AN_RES_FLT_END;
1420 txn->status = 504;
1421 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001422 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001423
1424 if (!(s->flags & SF_ERR_MASK))
1425 s->flags |= SF_ERR_SRVTO;
1426 if (!(s->flags & SF_FINST_MASK))
1427 s->flags |= SF_FINST_H;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001428 DBG_TRACE_DEVEL("leaving on error",
1429 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001430 return 0;
1431 }
1432
Christopher Faulet9768c262018-10-22 09:34:31 +02001433 /* 3: client abort with an abortonclose */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001434 else if ((rep->flags & CF_SHUTR) && ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001435 _HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
1436 _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001437 if (sess->listener->counters)
1438 _HA_ATOMIC_ADD(&sess->listener->counters->cli_aborts, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001439 if (objt_server(s->target))
Olivier Houcharda798bf52019-03-08 18:52:00 +01001440 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.cli_aborts, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001441
1442 rep->analysers &= AN_RES_FLT_END;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001443 txn->status = 400;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001444 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001445
1446 if (!(s->flags & SF_ERR_MASK))
1447 s->flags |= SF_ERR_CLICL;
1448 if (!(s->flags & SF_FINST_MASK))
1449 s->flags |= SF_FINST_H;
1450
1451 /* process_stream() will take care of the error */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001452 DBG_TRACE_DEVEL("leaving on error",
1453 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001454 return 0;
1455 }
1456
Christopher Faulet9768c262018-10-22 09:34:31 +02001457 /* 4: close from server, capture the response if the server has started to respond */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001458 else if (rep->flags & CF_SHUTR) {
Olivier Houcharda254a372019-04-05 15:30:12 +02001459 if ((si_b->flags & SI_FL_L7_RETRY) &&
1460 (s->be->retry_type & PR_RE_DISCONNECTED)) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001461 if (co_data(rep) || do_l7_retry(s, si_b) == 0) {
1462 DBG_TRACE_DEVEL("leaving on L7 retry",
1463 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharda254a372019-04-05 15:30:12 +02001464 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001465 }
Olivier Houcharda254a372019-04-05 15:30:12 +02001466 }
1467
Olivier Houchard6db16992019-05-17 15:40:49 +02001468 if (txn->flags & TX_NOT_FIRST)
1469 goto abort_keep_alive;
1470
Olivier Houcharda798bf52019-03-08 18:52:00 +01001471 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001472 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001473 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001474 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001475 }
1476
Christopher Faulete0768eb2018-10-03 16:38:02 +02001477 rep->analysers &= AN_RES_FLT_END;
1478 txn->status = 502;
1479 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001480 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001481
1482 if (!(s->flags & SF_ERR_MASK))
1483 s->flags |= SF_ERR_SRVCL;
1484 if (!(s->flags & SF_FINST_MASK))
1485 s->flags |= SF_FINST_H;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001486 DBG_TRACE_DEVEL("leaving on error",
1487 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001488 return 0;
1489 }
1490
Christopher Faulet9768c262018-10-22 09:34:31 +02001491 /* 5: write error to client (we don't send any message then) */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001492 else if (rep->flags & CF_WRITE_ERROR) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001493 if (txn->flags & TX_NOT_FIRST)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001494 goto abort_keep_alive;
1495
Olivier Houcharda798bf52019-03-08 18:52:00 +01001496 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001497 if (objt_server(s->target))
1498 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001499 rep->analysers &= AN_RES_FLT_END;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001500
1501 if (!(s->flags & SF_ERR_MASK))
1502 s->flags |= SF_ERR_CLICL;
1503 if (!(s->flags & SF_FINST_MASK))
1504 s->flags |= SF_FINST_H;
1505
1506 /* process_stream() will take care of the error */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001507 DBG_TRACE_DEVEL("leaving on error",
1508 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001509 return 0;
1510 }
1511
1512 channel_dont_close(rep);
1513 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001514 DBG_TRACE_DEVEL("waiting for more data",
1515 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001516 return 0;
1517 }
1518
1519 /* More interesting part now : we know that we have a complete
1520 * response which at least looks like HTTP. We have an indicator
1521 * of each header's length, so we can parse them quickly.
1522 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001523 msg->msg_state = HTTP_MSG_BODY;
Christopher Faulet29f17582019-05-23 11:03:26 +02001524 BUG_ON(htx_get_first_type(htx) != HTX_BLK_RES_SL);
Christopher Faulet297fbb42019-05-13 14:41:27 +02001525 sl = http_get_stline(htx);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001526
Christopher Faulet9768c262018-10-22 09:34:31 +02001527 /* 0: we might have to print this header in debug mode */
1528 if (unlikely((global.mode & MODE_DEBUG) &&
1529 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
1530 int32_t pos;
1531
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001532 http_debug_stline("srvrep", s, sl);
Christopher Faulet9768c262018-10-22 09:34:31 +02001533
Christopher Fauleta3f15502019-05-13 15:27:23 +02001534 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001535 struct htx_blk *blk = htx_get_blk(htx, pos);
1536 enum htx_blk_type type = htx_get_blk_type(blk);
1537
1538 if (type == HTX_BLK_EOH)
1539 break;
1540 if (type != HTX_BLK_HDR)
1541 continue;
1542
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001543 http_debug_hdr("srvhdr", s,
1544 htx_get_blk_name(htx, blk),
1545 htx_get_blk_value(htx, blk));
Christopher Faulet9768c262018-10-22 09:34:31 +02001546 }
1547 }
1548
Christopher Faulet03599112018-11-27 11:21:21 +01001549 /* 1: get the status code and the version. Also set HTTP flags */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001550 txn->status = sl->info.res.status;
Christopher Faulet03599112018-11-27 11:21:21 +01001551 if (sl->flags & HTX_SL_F_VER_11)
Christopher Faulet9768c262018-10-22 09:34:31 +02001552 msg->flags |= HTTP_MSGF_VER_11;
Christopher Faulet03599112018-11-27 11:21:21 +01001553 if (sl->flags & HTX_SL_F_XFER_LEN) {
1554 msg->flags |= HTTP_MSGF_XFER_LEN;
Christopher Faulet2a408542020-11-20 14:22:37 +01001555 if (sl->flags & HTX_SL_F_CLEN)
1556 msg->flags |= HTTP_MSGF_CNT_LEN;
1557 else if (sl->flags & HTX_SL_F_CHNK)
1558 msg->flags |= HTTP_MSGF_TE_CHNK;
Christopher Faulet03599112018-11-27 11:21:21 +01001559 }
Christopher Faulet2a408542020-11-20 14:22:37 +01001560 if (sl->flags & HTX_SL_F_BODYLESS)
1561 msg->flags |= HTTP_MSGF_BODYLESS;
Christopher Faulet576c3582021-01-08 15:53:01 +01001562 if (sl->flags & HTX_SL_F_CONN_UPG)
1563 msg->flags |= HTTP_MSGF_CONN_UPG;
Christopher Faulet9768c262018-10-22 09:34:31 +02001564
1565 n = txn->status / 100;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001566 if (n < 1 || n > 5)
1567 n = 0;
Christopher Faulet9768c262018-10-22 09:34:31 +02001568
Christopher Faulete0768eb2018-10-03 16:38:02 +02001569 /* when the client triggers a 4xx from the server, it's most often due
1570 * to a missing object or permission. These events should be tracked
1571 * because if they happen often, it may indicate a brute force or a
1572 * vulnerability scan.
1573 */
1574 if (n == 4)
1575 stream_inc_http_err_ctr(s);
1576
Marcin Deranek3c27dda2020-05-15 18:32:51 +02001577 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001578 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.p.http.rsp[n], 1);
Marcin Deranek3c27dda2020-05-15 18:32:51 +02001579 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.p.http.cum_req, 1);
1580 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001581
Christopher Faulete0768eb2018-10-03 16:38:02 +02001582 /* Adjust server's health based on status code. Note: status codes 501
1583 * and 505 are triggered on demand by client request, so we must not
1584 * count them as server failures.
1585 */
1586 if (objt_server(s->target)) {
1587 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001588 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_OK);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001589 else
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001590 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_STS);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001591 }
1592
1593 /*
1594 * We may be facing a 100-continue response, or any other informational
1595 * 1xx response which is non-final, in which case this is not the right
1596 * response, and we're waiting for the next one. Let's allow this response
1597 * to go to the client and wait for the next one. There's an exception for
1598 * 101 which is used later in the code to switch protocols.
1599 */
1600 if (txn->status < 200 &&
1601 (txn->status == 100 || txn->status >= 102)) {
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001602 FLT_STRM_CB(s, flt_http_reset(s, msg));
Christopher Faulet421e7692019-06-13 11:16:45 +02001603 htx->first = channel_htx_fwd_headers(rep, htx);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001604 msg->msg_state = HTTP_MSG_RPBEFORE;
Christopher Faulet3499f622019-09-03 15:23:54 +02001605 msg->flags = 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001606 txn->status = 0;
1607 s->logs.t_data = -1; /* was not a response yet */
Christopher Faulet7d518452020-08-31 11:07:07 +02001608 rep->flags |= CF_SEND_DONTWAIT; /* Send ASAP informational messages */
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001609 goto next_one;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001610 }
1611
Christopher Faulet6e6c7b12021-01-08 16:02:05 +01001612 /* A 101-switching-protocols must contains a Connection header with the
1613 * "upgrade" option and the request too. It means both are agree to
1614 * upgrade. It is not so strict because there is no test on the Upgrade
1615 * header content. But it is probably stronger enough for now.
1616 */
1617 if (txn->status == 101 &&
1618 (!(txn->req.flags & HTTP_MSGF_CONN_UPG) || !(txn->rsp.flags & HTTP_MSGF_CONN_UPG)))
1619 goto return_bad_res;
1620
Christopher Faulete0768eb2018-10-03 16:38:02 +02001621 /*
1622 * 2: check for cacheability.
1623 */
1624
1625 switch (txn->status) {
1626 case 200:
1627 case 203:
1628 case 204:
1629 case 206:
1630 case 300:
1631 case 301:
1632 case 404:
1633 case 405:
1634 case 410:
1635 case 414:
1636 case 501:
1637 break;
1638 default:
1639 /* RFC7231#6.1:
1640 * Responses with status codes that are defined as
1641 * cacheable by default (e.g., 200, 203, 204, 206,
1642 * 300, 301, 404, 405, 410, 414, and 501 in this
1643 * specification) can be reused by a cache with
1644 * heuristic expiration unless otherwise indicated
1645 * by the method definition or explicit cache
1646 * controls [RFC7234]; all other status codes are
1647 * not cacheable by default.
1648 */
1649 txn->flags &= ~(TX_CACHEABLE | TX_CACHE_COOK);
1650 break;
1651 }
1652
1653 /*
1654 * 3: we may need to capture headers
1655 */
1656 s->logs.logwait &= ~LW_RESP;
1657 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->res_cap))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001658 http_capture_headers(htx, s->res_cap, sess->fe->rsp_cap);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001659
Christopher Faulet9768c262018-10-22 09:34:31 +02001660 /* Skip parsing if no content length is possible. */
Christopher Fauletc75668e2020-12-07 18:10:32 +01001661 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status >= 200 && txn->status < 300) ||
Christopher Faulete0768eb2018-10-03 16:38:02 +02001662 txn->status == 101)) {
1663 /* Either we've established an explicit tunnel, or we're
1664 * switching the protocol. In both cases, we're very unlikely
1665 * to understand the next protocols. We have to switch to tunnel
1666 * mode, so that we transfer the request and responses then let
1667 * this protocol pass unmodified. When we later implement specific
1668 * parsers for such protocols, we'll want to check the Upgrade
1669 * header which contains information about that protocol for
1670 * responses with status 101 (eg: see RFC2817 about TLS).
1671 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02001672 txn->flags |= TX_CON_WANT_TUN;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001673 }
1674
Christopher Faulet61608322018-11-23 16:23:45 +01001675 /* check for NTML authentication headers in 401 (WWW-Authenticate) and
1676 * 407 (Proxy-Authenticate) responses and set the connection to private
1677 */
1678 srv_conn = cs_conn(objt_cs(s->si[1].end));
1679 if (srv_conn) {
1680 struct ist hdr;
1681 struct http_hdr_ctx ctx;
1682
1683 if (txn->status == 401)
1684 hdr = ist("WWW-Authenticate");
1685 else if (txn->status == 407)
1686 hdr = ist("Proxy-Authenticate");
1687 else
1688 goto end;
1689
1690 ctx.blk = NULL;
1691 while (http_find_header(htx, hdr, &ctx, 0)) {
Willy Tarreauf1dcced2020-05-07 19:27:02 +02001692 /* If www-authenticate contains "Negotiate", "Nego2", or "NTLM",
1693 * possibly followed by blanks and a base64 string, the connection
1694 * is private. Since it's a mess to deal with, we only check for
1695 * values starting with "NTLM" or "Nego". Note that often multiple
1696 * headers are sent by the server there.
1697 */
1698 if ((ctx.value.len >= 4 && strncasecmp(ctx.value.ptr, "Nego", 4) == 0) ||
Willy Tarreau49a1d282020-05-07 19:10:15 +02001699 (ctx.value.len >= 4 && strncasecmp(ctx.value.ptr, "NTLM", 4) == 0)) {
Olivier Houchard250031e2019-05-29 15:01:50 +02001700 sess->flags |= SESS_FL_PREFER_LAST;
Christopher Faulet08016ab2020-07-01 16:10:06 +02001701 conn_set_owner(srv_conn, sess, NULL);
Christopher Faulet21ddc742020-07-01 15:26:14 +02001702 conn_set_private(srv_conn);
Ilya Shipitsin6b79f382020-07-23 00:32:55 +05001703 /* If it fail now, the same will be done in mux->detach() callback */
Christopher Faulet08016ab2020-07-01 16:10:06 +02001704 session_add_conn(srv_conn->owner, srv_conn, srv_conn->target);
Willy Tarreauf1dcced2020-05-07 19:27:02 +02001705 break;
Olivier Houchard250031e2019-05-29 15:01:50 +02001706 }
Christopher Faulet61608322018-11-23 16:23:45 +01001707 }
1708 }
1709
1710 end:
Christopher Faulete0768eb2018-10-03 16:38:02 +02001711 /* we want to have the response time before we start processing it */
1712 s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now);
1713
1714 /* end of job, return OK */
1715 rep->analysers &= ~an_bit;
1716 rep->analyse_exp = TICK_ETERNITY;
1717 channel_auto_close(rep);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001718 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001719 return 1;
1720
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001721 return_int_err:
Christopher Fauletcff0f732019-12-16 16:13:44 +01001722 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01001723 _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001724 if (sess->listener->counters)
1725 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01001726 if (objt_server(s->target))
1727 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.internal_errors, 1);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001728 txn->status = 500;
1729 if (!(s->flags & SF_ERR_MASK))
1730 s->flags |= SF_ERR_INTERNAL;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001731 goto return_prx_cond;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001732
1733 return_bad_res:
Olivier Houcharda798bf52019-03-08 18:52:00 +01001734 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulet47365272018-10-31 17:40:50 +01001735 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001736 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001737 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Christopher Faulet47365272018-10-31 17:40:50 +01001738 }
Olivier Houcharde3249a92019-05-03 23:01:47 +02001739 if ((s->be->retry_type & PR_RE_JUNK_REQUEST) &&
Olivier Houchardad26d8d2019-05-10 17:48:28 +02001740 (si_b->flags & SI_FL_L7_RETRY) &&
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001741 do_l7_retry(s, si_b) == 0) {
1742 DBG_TRACE_DEVEL("leaving on L7 retry",
1743 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharde3249a92019-05-03 23:01:47 +02001744 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001745 }
Christopher Faulet47365272018-10-31 17:40:50 +01001746 txn->status = 502;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001747 /* fall through */
1748
Christopher Fauletb8a53712019-12-16 11:29:38 +01001749 return_prx_cond:
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001750 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet47365272018-10-31 17:40:50 +01001751
1752 if (!(s->flags & SF_ERR_MASK))
1753 s->flags |= SF_ERR_PRXCOND;
1754 if (!(s->flags & SF_FINST_MASK))
1755 s->flags |= SF_FINST_H;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001756
1757 s->si[1].flags |= SI_FL_NOLINGER;
1758 rep->analysers &= AN_RES_FLT_END;
Christopher Faulete58c0002020-03-02 16:21:01 +01001759 s->req.analysers &= AN_REQ_FLT_END;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001760 rep->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001761 DBG_TRACE_DEVEL("leaving on error",
1762 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulet47365272018-10-31 17:40:50 +01001763 return 0;
1764
Christopher Faulete0768eb2018-10-03 16:38:02 +02001765 abort_keep_alive:
1766 /* A keep-alive request to the server failed on a network error.
1767 * The client is required to retry. We need to close without returning
1768 * any other information so that the client retries.
1769 */
1770 txn->status = 0;
1771 rep->analysers &= AN_RES_FLT_END;
1772 s->req.analysers &= AN_REQ_FLT_END;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001773 s->logs.logwait = 0;
1774 s->logs.level = 0;
1775 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001776 http_reply_and_close(s, txn->status, NULL);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001777 DBG_TRACE_DEVEL("leaving by closing K/A connection",
1778 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001779 return 0;
1780}
1781
1782/* This function performs all the processing enabled for the current response.
1783 * It normally returns 1 unless it wants to break. It relies on buffers flags,
1784 * and updates s->res.analysers. It might make sense to explode it into several
1785 * other functions. It works like process_request (see indications above).
1786 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001787int http_process_res_common(struct stream *s, struct channel *rep, int an_bit, struct proxy *px)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001788{
1789 struct session *sess = s->sess;
1790 struct http_txn *txn = s->txn;
1791 struct http_msg *msg = &txn->rsp;
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001792 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001793 struct proxy *cur_proxy;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001794 enum rule_result ret = HTTP_RULE_RES_CONT;
1795
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001796 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
1797 return 0;
Christopher Faulet9768c262018-10-22 09:34:31 +02001798
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001799 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001800
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001801 htx = htxbuf(&rep->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001802
1803 /* The stats applet needs to adjust the Connection header but we don't
1804 * apply any filter there.
1805 */
1806 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
1807 rep->analysers &= ~an_bit;
1808 rep->analyse_exp = TICK_ETERNITY;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001809 goto end;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001810 }
1811
1812 /*
1813 * We will have to evaluate the filters.
1814 * As opposed to version 1.2, now they will be evaluated in the
1815 * filters order and not in the header order. This means that
1816 * each filter has to be validated among all headers.
1817 *
1818 * Filters are tried with ->be first, then with ->fe if it is
1819 * different from ->be.
1820 *
1821 * Maybe we are in resume condiion. In this case I choose the
1822 * "struct proxy" which contains the rule list matching the resume
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05001823 * pointer. If none of these "struct proxy" match, I initialise
Christopher Faulete0768eb2018-10-03 16:38:02 +02001824 * the process with the first one.
1825 *
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05001826 * In fact, I check only correspondence between the current list
Christopher Faulete0768eb2018-10-03 16:38:02 +02001827 * pointer and the ->fe rule list. If it doesn't match, I initialize
1828 * the loop with the ->be.
1829 */
1830 if (s->current_rule_list == &sess->fe->http_res_rules)
1831 cur_proxy = sess->fe;
1832 else
1833 cur_proxy = s->be;
1834 while (1) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001835 /* evaluate http-response rules */
1836 if (ret == HTTP_RULE_RES_CONT) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001837 ret = http_res_get_intercept_rule(cur_proxy, &cur_proxy->http_res_rules, s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001838
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001839 switch (ret) {
1840 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
1841 goto return_prx_yield;
1842
1843 case HTTP_RULE_RES_CONT:
1844 case HTTP_RULE_RES_STOP: /* nothing to do */
1845 break;
1846
1847 case HTTP_RULE_RES_DENY: /* deny or tarpit */
1848 goto deny;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001849
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001850 case HTTP_RULE_RES_ABRT: /* abort request, response already sent */
1851 goto return_prx_cond;
1852
1853 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
Christopher Fauletb8a53712019-12-16 11:29:38 +01001854 goto done;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001855
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001856 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
1857 goto return_bad_res;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001858
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001859 case HTTP_RULE_RES_ERROR: /* failed with a bad request */
1860 goto return_int_err;
1861 }
1862
1863 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001864
Christopher Faulete0768eb2018-10-03 16:38:02 +02001865 /* check whether we're already working on the frontend */
1866 if (cur_proxy == sess->fe)
1867 break;
1868 cur_proxy = sess->fe;
1869 }
1870
Christopher Faulete0768eb2018-10-03 16:38:02 +02001871 /* OK that's all we can do for 1xx responses */
1872 if (unlikely(txn->status < 200 && txn->status != 101))
Christopher Fauletf2824e62018-10-01 12:12:37 +02001873 goto end;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001874
1875 /*
1876 * Now check for a server cookie.
1877 */
1878 if (s->be->cookie_name || sess->fe->capture_name || (s->be->options & PR_O_CHK_CACHE))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001879 http_manage_server_side_cookies(s, rep);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001880
1881 /*
1882 * Check for cache-control or pragma headers if required.
1883 */
1884 if ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001885 http_check_response_for_cacheability(s, rep);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001886
1887 /*
1888 * Add server cookie in the response if needed
1889 */
1890 if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) &&
1891 !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) &&
1892 (!(s->flags & SF_DIRECT) ||
1893 ((s->be->cookie_maxidle || txn->cookie_last_date) &&
1894 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
1895 (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
1896 (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
1897 (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
1898 !(s->flags & SF_IGNORE_PRST)) {
1899 /* the server is known, it's not the one the client requested, or the
1900 * cookie's last seen date needs to be refreshed. We have to
1901 * insert a set-cookie here, except if we want to insert only on POST
1902 * requests and this one isn't. Note that servers which don't have cookies
1903 * (eg: some backup servers) will return a full cookie removal request.
1904 */
1905 if (!objt_server(s->target)->cookie) {
1906 chunk_printf(&trash,
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001907 "%s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
Christopher Faulete0768eb2018-10-03 16:38:02 +02001908 s->be->cookie_name);
1909 }
1910 else {
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001911 chunk_printf(&trash, "%s=%s", s->be->cookie_name, objt_server(s->target)->cookie);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001912
1913 if (s->be->cookie_maxidle || s->be->cookie_maxlife) {
1914 /* emit last_date, which is mandatory */
1915 trash.area[trash.data++] = COOKIE_DELIM_DATE;
1916 s30tob64((date.tv_sec+3) >> 2,
1917 trash.area + trash.data);
1918 trash.data += 5;
1919
1920 if (s->be->cookie_maxlife) {
1921 /* emit first_date, which is either the original one or
1922 * the current date.
1923 */
1924 trash.area[trash.data++] = COOKIE_DELIM_DATE;
1925 s30tob64(txn->cookie_first_date ?
1926 txn->cookie_first_date >> 2 :
1927 (date.tv_sec+3) >> 2,
1928 trash.area + trash.data);
1929 trash.data += 5;
1930 }
1931 }
1932 chunk_appendf(&trash, "; path=/");
1933 }
1934
1935 if (s->be->cookie_domain)
1936 chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain);
1937
1938 if (s->be->ck_opts & PR_CK_HTTPONLY)
1939 chunk_appendf(&trash, "; HttpOnly");
1940
1941 if (s->be->ck_opts & PR_CK_SECURE)
1942 chunk_appendf(&trash, "; Secure");
1943
Christopher Faulet2f533902020-01-21 11:06:48 +01001944 if (s->be->cookie_attrs)
1945 chunk_appendf(&trash, "; %s", s->be->cookie_attrs);
1946
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001947 if (unlikely(!http_add_header(htx, ist("Set-Cookie"), ist2(trash.area, trash.data))))
Christopher Fauletb8a53712019-12-16 11:29:38 +01001948 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001949
1950 txn->flags &= ~TX_SCK_MASK;
1951 if (__objt_server(s->target)->cookie && (s->flags & SF_DIRECT))
1952 /* the server did not change, only the date was updated */
1953 txn->flags |= TX_SCK_UPDATED;
1954 else
1955 txn->flags |= TX_SCK_INSERTED;
1956
1957 /* Here, we will tell an eventual cache on the client side that we don't
1958 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
1959 * Some caches understand the correct form: 'no-cache="set-cookie"', but
1960 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
1961 */
1962 if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
1963
1964 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
1965
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001966 if (unlikely(!http_add_header(htx, ist("Cache-control"), ist("private"))))
Christopher Fauletb8a53712019-12-16 11:29:38 +01001967 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001968 }
1969 }
1970
1971 /*
1972 * Check if result will be cacheable with a cookie.
1973 * We'll block the response if security checks have caught
1974 * nasty things such as a cacheable cookie.
1975 */
1976 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
1977 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
1978 (s->be->options & PR_O_CHK_CACHE)) {
1979 /* we're in presence of a cacheable response containing
1980 * a set-cookie header. We'll block it as requested by
1981 * the 'checkcache' option, and send an alert.
1982 */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001983 ha_alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
1984 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
1985 send_log(s->be, LOG_ALERT,
1986 "Blocking cacheable cookie in response from instance %s, server %s.\n",
1987 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
Christopher Fauletb8a53712019-12-16 11:29:38 +01001988 goto deny;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001989 }
1990
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001991 end:
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01001992 /*
1993 * Evaluate after-response rules before forwarding the response. rules
1994 * from the backend are evaluated first, then one from the frontend if
1995 * it differs.
1996 */
1997 if (!http_eval_after_res_rules(s))
1998 goto return_int_err;
1999
Christopher Faulete0768eb2018-10-03 16:38:02 +02002000 /* Always enter in the body analyzer */
2001 rep->analysers &= ~AN_RES_FLT_XFER_DATA;
2002 rep->analysers |= AN_RES_HTTP_XFER_BODY;
2003
2004 /* if the user wants to log as soon as possible, without counting
2005 * bytes from the server, then this is the right moment. We have
2006 * to temporarily assign bytes_out to log what we currently have.
2007 */
2008 if (!LIST_ISEMPTY(&sess->fe->logformat) && !(s->logs.logwait & LW_BYTES)) {
2009 s->logs.t_close = s->logs.t_data; /* to get a valid end date */
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002010 s->logs.bytes_out = htx->data;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002011 s->do_log(s);
2012 s->logs.bytes_out = 0;
2013 }
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002014
Christopher Fauletb8a53712019-12-16 11:29:38 +01002015 done:
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002016 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002017 rep->analysers &= ~an_bit;
2018 rep->analyse_exp = TICK_ETERNITY;
2019 return 1;
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002020
Christopher Fauletb8a53712019-12-16 11:29:38 +01002021 deny:
Christopher Fauletb8a53712019-12-16 11:29:38 +01002022 _HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_resp, 1);
Christopher Fauleta08546b2019-12-16 16:07:34 +01002023 _HA_ATOMIC_ADD(&s->be->be_counters.denied_resp, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002024 if (sess->listener->counters)
2025 _HA_ATOMIC_ADD(&sess->listener->counters->denied_resp, 1);
Christopher Fauleta08546b2019-12-16 16:07:34 +01002026 if (objt_server(s->target))
2027 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.denied_resp, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002028 goto return_prx_err;
2029
2030 return_int_err:
2031 txn->status = 500;
2032 if (!(s->flags & SF_ERR_MASK))
2033 s->flags |= SF_ERR_INTERNAL;
Christopher Fauletcff0f732019-12-16 16:13:44 +01002034 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002035 _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1);
2036 if (objt_server(s->target))
2037 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01002038 if (objt_server(s->target))
2039 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002040 goto return_prx_err;
2041
2042 return_bad_res:
2043 txn->status = 502;
Christopher Fauleta20a6532020-02-05 10:16:41 +01002044 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
2045 if (objt_server(s->target)) {
2046 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
2047 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_RSP);
2048 }
Christopher Fauletb8a53712019-12-16 11:29:38 +01002049 /* fall through */
2050
2051 return_prx_err:
2052 http_reply_and_close(s, txn->status, http_error_message(s));
2053 /* fall through */
2054
2055 return_prx_cond:
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002056 s->logs.t_data = -1; /* was not a valid response */
2057 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002058
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002059 if (!(s->flags & SF_ERR_MASK))
2060 s->flags |= SF_ERR_PRXCOND;
2061 if (!(s->flags & SF_FINST_MASK))
2062 s->flags |= SF_FINST_H;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002063
Christopher Faulete58c0002020-03-02 16:21:01 +01002064 rep->analysers &= AN_RES_FLT_END;
2065 s->req.analysers &= AN_REQ_FLT_END;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002066 rep->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002067 DBG_TRACE_DEVEL("leaving on error",
2068 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002069 return 0;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002070
2071 return_prx_yield:
2072 channel_dont_close(rep);
2073 DBG_TRACE_DEVEL("waiting for more data",
2074 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
2075 return 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002076}
2077
2078/* This function is an analyser which forwards response body (including chunk
2079 * sizes if any). It is called as soon as we must forward, even if we forward
2080 * zero byte. The only situation where it must not be called is when we're in
2081 * tunnel mode and we want to forward till the close. It's used both to forward
2082 * remaining data and to resync after end of body. It expects the msg_state to
2083 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
2084 * read more data, or 1 once we can go on with next request or end the stream.
2085 *
2086 * It is capable of compressing response data both in content-length mode and
2087 * in chunked mode. The state machines follows different flows depending on
2088 * whether content-length and chunked modes are used, since there are no
2089 * trailers in content-length :
2090 *
2091 * chk-mode cl-mode
2092 * ,----- BODY -----.
2093 * / \
2094 * V size > 0 V chk-mode
2095 * .--> SIZE -------------> DATA -------------> CRLF
2096 * | | size == 0 | last byte |
2097 * | v final crlf v inspected |
2098 * | TRAILERS -----------> DONE |
2099 * | |
2100 * `----------------------------------------------'
2101 *
2102 * Compression only happens in the DATA state, and must be flushed in final
2103 * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding
2104 * is performed at once on final states for all bytes parsed, or when leaving
2105 * on missing data.
2106 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002107int http_response_forward_body(struct stream *s, struct channel *res, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +02002108{
2109 struct session *sess = s->sess;
2110 struct http_txn *txn = s->txn;
2111 struct http_msg *msg = &s->txn->rsp;
Christopher Faulet9768c262018-10-22 09:34:31 +02002112 struct htx *htx;
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002113 int ret;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002114
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002115 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002116
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002117 htx = htxbuf(&res->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002118
Christopher Fauletb9a92f32019-09-09 10:15:21 +02002119 if (htx->flags & HTX_FL_PARSING_ERROR)
2120 goto return_bad_res;
2121 if (htx->flags & HTX_FL_PROCESSING_ERROR)
2122 goto return_int_err;
2123
Christopher Faulete0768eb2018-10-03 16:38:02 +02002124 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Christopher Fauletf2824e62018-10-01 12:12:37 +02002125 ((res->flags & CF_SHUTW) && (res->to_forward || co_data(res)))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02002126 /* Output closed while we were sending data. We must abort and
2127 * wake the other side up.
2128 */
Christopher Faulete0768eb2018-10-03 16:38:02 +02002129 msg->msg_state = HTTP_MSG_ERROR;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002130 http_end_response(s);
2131 http_end_request(s);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002132 DBG_TRACE_DEVEL("leaving on error",
2133 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002134 return 1;
2135 }
2136
Christopher Faulet9768c262018-10-22 09:34:31 +02002137 if (msg->msg_state == HTTP_MSG_BODY)
2138 msg->msg_state = HTTP_MSG_DATA;
2139
Christopher Faulete0768eb2018-10-03 16:38:02 +02002140 /* in most states, we should abort in case of early close */
2141 channel_auto_close(res);
2142
Christopher Faulete0768eb2018-10-03 16:38:02 +02002143 if (res->to_forward) {
Christopher Faulet66af0b22019-03-22 14:54:52 +01002144 if (res->to_forward == CHN_INFINITE_FORWARD) {
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002145 if (res->flags & CF_EOI)
2146 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet66af0b22019-03-22 14:54:52 +01002147 }
2148 else {
2149 /* We can't process the buffer's contents yet */
2150 res->flags |= CF_WAKE_WRITE;
2151 goto missing_data_or_waiting;
2152 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02002153 }
2154
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002155 if (msg->msg_state >= HTTP_MSG_ENDING)
2156 goto ending;
2157
Christopher Fauletc75668e2020-12-07 18:10:32 +01002158 if ((txn->meth == HTTP_METH_CONNECT && txn->status >= 200 && txn->status < 300) || txn->status == 101 ||
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002159 (!(msg->flags & HTTP_MSGF_XFER_LEN) && !HAS_RSP_DATA_FILTERS(s))) {
2160 msg->msg_state = HTTP_MSG_ENDING;
2161 goto ending;
2162 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002163
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002164 /* Forward input data. We get it by removing all outgoing data not
2165 * forwarded yet from HTX data size. If there are some data filters, we
2166 * let them decide the amount of data to forward.
Christopher Faulet9768c262018-10-22 09:34:31 +02002167 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002168 if (HAS_RSP_DATA_FILTERS(s)) {
2169 ret = flt_http_payload(s, msg, htx->data);
2170 if (ret < 0)
2171 goto return_bad_res;
Christopher Faulet421e7692019-06-13 11:16:45 +02002172 c_adv(res, ret);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002173 }
2174 else {
Christopher Faulet421e7692019-06-13 11:16:45 +02002175 c_adv(res, htx->data - co_data(res));
Christopher Faulet66af0b22019-03-22 14:54:52 +01002176 if (msg->flags & HTTP_MSGF_XFER_LEN)
2177 channel_htx_forward_forever(res, htx);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002178 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002179
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002180 if (htx->data != co_data(res))
2181 goto missing_data_or_waiting;
2182
2183 if (!(msg->flags & HTTP_MSGF_XFER_LEN) && res->flags & CF_SHUTR) {
2184 msg->msg_state = HTTP_MSG_ENDING;
2185 goto ending;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002186 }
2187
Christopher Faulet9768c262018-10-22 09:34:31 +02002188 /* Check if the end-of-message is reached and if so, switch the message
Christopher Fauletd20fdb02019-06-13 16:43:22 +02002189 * in HTTP_MSG_ENDING state. Then if all data was marked to be
2190 * forwarded, set the state to HTTP_MSG_DONE.
Christopher Faulet9768c262018-10-22 09:34:31 +02002191 */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002192 if (!(htx->flags & HTX_FL_EOM))
Christopher Faulet9768c262018-10-22 09:34:31 +02002193 goto missing_data_or_waiting;
2194
Christopher Fauletd20fdb02019-06-13 16:43:22 +02002195 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet9768c262018-10-22 09:34:31 +02002196
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002197 ending:
Christopher Faulet2151cdd2020-07-22 16:34:59 +02002198 res->flags &= ~CF_EXPECT_MORE; /* no more data are expected */
2199
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002200 /* other states, ENDING...TUNNEL */
2201 if (msg->msg_state >= HTTP_MSG_DONE)
2202 goto done;
Christopher Faulet9768c262018-10-22 09:34:31 +02002203
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002204 if (HAS_RSP_DATA_FILTERS(s)) {
2205 ret = flt_http_end(s, msg);
2206 if (ret <= 0) {
2207 if (!ret)
2208 goto missing_data_or_waiting;
2209 goto return_bad_res;
2210 }
2211 }
2212
Christopher Fauletc75668e2020-12-07 18:10:32 +01002213 if ((txn->meth == HTTP_METH_CONNECT && txn->status >= 200 && txn->status < 300) || txn->status == 101 ||
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002214 !(msg->flags & HTTP_MSGF_XFER_LEN)) {
2215 msg->msg_state = HTTP_MSG_TUNNEL;
2216 goto ending;
2217 }
2218 else {
2219 msg->msg_state = HTTP_MSG_DONE;
2220 res->to_forward = 0;
2221 }
2222
2223 done:
2224
2225 channel_dont_close(res);
2226
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002227 http_end_response(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002228 if (!(res->analysers & an_bit)) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002229 http_end_request(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002230 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
2231 if (res->flags & CF_SHUTW) {
2232 /* response errors are most likely due to the
2233 * client aborting the transfer. */
Christopher Faulet93e02d82019-03-08 14:18:50 +01002234 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002235 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02002236 goto return_bad_res;
2237 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002238 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002239 return 1;
2240 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002241 DBG_TRACE_DEVEL("waiting for the end of the HTTP txn",
2242 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002243 return 0;
2244
2245 missing_data_or_waiting:
2246 if (res->flags & CF_SHUTW)
Christopher Faulet93e02d82019-03-08 14:18:50 +01002247 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002248
2249 /* stop waiting for data if the input is closed before the end. If the
2250 * client side was already closed, it means that the client has aborted,
2251 * so we don't want to count this as a server abort. Otherwise it's a
2252 * server abort.
2253 */
Christopher Fauletd20fdb02019-06-13 16:43:22 +02002254 if (msg->msg_state < HTTP_MSG_ENDING && res->flags & CF_SHUTR) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02002255 if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))
Christopher Faulet93e02d82019-03-08 14:18:50 +01002256 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002257 /* If we have some pending data, we continue the processing */
Christopher Faulet93e02d82019-03-08 14:18:50 +01002258 if (htx_is_empty(htx))
2259 goto return_srv_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002260 }
2261
Christopher Faulete0768eb2018-10-03 16:38:02 +02002262 /* When TE: chunked is used, we need to get there again to parse
2263 * remaining chunks even if the server has closed, so we don't want to
Christopher Faulet9768c262018-10-22 09:34:31 +02002264 * set CF_DONTCLOSE. Similarly when there is a content-leng or if there
2265 * are filters registered on the stream, we don't want to forward a
2266 * close
Christopher Faulete0768eb2018-10-03 16:38:02 +02002267 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002268 if ((msg->flags & HTTP_MSGF_XFER_LEN) || HAS_RSP_DATA_FILTERS(s))
Christopher Faulete0768eb2018-10-03 16:38:02 +02002269 channel_dont_close(res);
2270
2271 /* We know that more data are expected, but we couldn't send more that
2272 * what we did. So we always set the CF_EXPECT_MORE flag so that the
2273 * system knows it must not set a PUSH on this first part. Interactive
2274 * modes are already handled by the stream sock layer. We must not do
2275 * this in content-length mode because it could present the MSG_MORE
2276 * flag with the last block of forwarded data, which would cause an
2277 * additional delay to be observed by the receiver.
2278 */
Christopher Faulet2151cdd2020-07-22 16:34:59 +02002279 if (HAS_RSP_DATA_FILTERS(s))
Christopher Faulete0768eb2018-10-03 16:38:02 +02002280 res->flags |= CF_EXPECT_MORE;
2281
2282 /* the stream handler will take care of timeouts and errors */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002283 DBG_TRACE_DEVEL("waiting for more data to forward",
2284 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002285 return 0;
2286
Christopher Faulet93e02d82019-03-08 14:18:50 +01002287 return_srv_abort:
2288 _HA_ATOMIC_ADD(&sess->fe->fe_counters.srv_aborts, 1);
2289 _HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01002290 if (sess->listener->counters)
2291 _HA_ATOMIC_ADD(&sess->listener->counters->srv_aborts, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002292 if (objt_server(s->target))
Christopher Fauletcff0f732019-12-16 16:13:44 +01002293 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.srv_aborts, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002294 if (!(s->flags & SF_ERR_MASK))
2295 s->flags |= SF_ERR_SRVCL;
2296 goto return_error;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002297
Christopher Faulet93e02d82019-03-08 14:18:50 +01002298 return_cli_abort:
2299 _HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
2300 _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01002301 if (sess->listener->counters)
2302 _HA_ATOMIC_ADD(&sess->listener->counters->cli_aborts, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002303 if (objt_server(s->target))
Christopher Fauletcff0f732019-12-16 16:13:44 +01002304 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.cli_aborts, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002305 if (!(s->flags & SF_ERR_MASK))
2306 s->flags |= SF_ERR_CLICL;
2307 goto return_error;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002308
Christopher Fauletb9a92f32019-09-09 10:15:21 +02002309 return_int_err:
Christopher Fauletcff0f732019-12-16 16:13:44 +01002310 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002311 _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01002312 if (sess->listener->counters)
2313 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002314 if (objt_server(s->target))
2315 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.internal_errors, 1);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02002316 if (!(s->flags & SF_ERR_MASK))
2317 s->flags |= SF_ERR_INTERNAL;
2318 goto return_error;
2319
Christopher Faulet93e02d82019-03-08 14:18:50 +01002320 return_bad_res:
2321 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
2322 if (objt_server(s->target)) {
Christopher Fauletcff0f732019-12-16 16:13:44 +01002323 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002324 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_RSP);
2325 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02002326 if (!(s->flags & SF_ERR_MASK))
Christopher Faulet93e02d82019-03-08 14:18:50 +01002327 s->flags |= SF_ERR_SRVCL;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002328 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +02002329
Christopher Faulet93e02d82019-03-08 14:18:50 +01002330 return_error:
Christopher Faulete0768eb2018-10-03 16:38:02 +02002331 /* don't send any error message as we're in the body */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002332 http_reply_and_close(s, txn->status, NULL);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002333 res->analysers &= AN_RES_FLT_END;
2334 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 +02002335 if (!(s->flags & SF_FINST_MASK))
2336 s->flags |= SF_FINST_D;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002337 DBG_TRACE_DEVEL("leaving on error",
2338 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002339 return 0;
2340}
2341
Christopher Fauletf2824e62018-10-01 12:12:37 +02002342/* Perform an HTTP redirect based on the information in <rule>. The function
Christopher Faulet99daf282018-11-28 22:58:13 +01002343 * returns zero on success, or zero in case of a, irrecoverable error such
Christopher Fauletf2824e62018-10-01 12:12:37 +02002344 * as too large a request to build a valid response.
2345 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002346int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn)
Christopher Fauletf2824e62018-10-01 12:12:37 +02002347{
Christopher Faulet99daf282018-11-28 22:58:13 +01002348 struct channel *req = &s->req;
2349 struct channel *res = &s->res;
2350 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01002351 struct htx_sl *sl;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002352 struct buffer *chunk;
Christopher Faulet99daf282018-11-28 22:58:13 +01002353 struct ist status, reason, location;
2354 unsigned int flags;
Christopher Faulet08e66462019-05-23 16:44:59 +02002355 int close = 0; /* Try to keep the connection alive byt default */
Christopher Fauletf2824e62018-10-01 12:12:37 +02002356
2357 chunk = alloc_trash_chunk();
Christopher Fauletb8a53712019-12-16 11:29:38 +01002358 if (!chunk) {
2359 if (!(s->flags & SF_ERR_MASK))
2360 s->flags |= SF_ERR_RESOURCE;
Christopher Faulet99daf282018-11-28 22:58:13 +01002361 goto fail;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002362 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002363
Christopher Faulet99daf282018-11-28 22:58:13 +01002364 /*
2365 * Create the location
2366 */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002367 htx = htxbuf(&req->buf);
Christopher Fauletf2824e62018-10-01 12:12:37 +02002368 switch(rule->type) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002369 case REDIRECT_TYPE_SCHEME: {
2370 struct http_hdr_ctx ctx;
2371 struct ist path, host;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002372
Christopher Faulet99daf282018-11-28 22:58:13 +01002373 host = ist("");
2374 ctx.blk = NULL;
2375 if (http_find_header(htx, ist("Host"), &ctx, 0))
2376 host = ctx.value;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002377
Christopher Faulet297fbb42019-05-13 14:41:27 +02002378 sl = http_get_stline(htx);
Christopher Faulet99daf282018-11-28 22:58:13 +01002379 path = http_get_path(htx_sl_req_uri(sl));
2380 /* build message using path */
Tim Duesterhused526372020-03-05 17:56:33 +01002381 if (isttest(path)) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002382 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2383 int qs = 0;
2384 while (qs < path.len) {
2385 if (*(path.ptr + qs) == '?') {
2386 path.len = qs;
2387 break;
2388 }
2389 qs++;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002390 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002391 }
2392 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002393 else
2394 path = ist("/");
Christopher Fauletf2824e62018-10-01 12:12:37 +02002395
Christopher Faulet99daf282018-11-28 22:58:13 +01002396 if (rule->rdr_str) { /* this is an old "redirect" rule */
2397 /* add scheme */
2398 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2399 goto fail;
2400 }
2401 else {
2402 /* add scheme with executing log format */
2403 chunk->data += build_logline(s, chunk->area + chunk->data,
2404 chunk->size - chunk->data,
2405 &rule->rdr_fmt);
2406 }
2407 /* add "://" + host + path */
2408 if (!chunk_memcat(chunk, "://", 3) ||
2409 !chunk_memcat(chunk, host.ptr, host.len) ||
2410 !chunk_memcat(chunk, path.ptr, path.len))
2411 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002412
Christopher Faulet99daf282018-11-28 22:58:13 +01002413 /* append a slash at the end of the location if needed and missing */
2414 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
2415 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
2416 if (chunk->data + 1 >= chunk->size)
2417 goto fail;
2418 chunk->area[chunk->data++] = '/';
2419 }
2420 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002421 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002422
Christopher Faulet99daf282018-11-28 22:58:13 +01002423 case REDIRECT_TYPE_PREFIX: {
2424 struct ist path;
2425
Christopher Faulet297fbb42019-05-13 14:41:27 +02002426 sl = http_get_stline(htx);
Christopher Faulet99daf282018-11-28 22:58:13 +01002427 path = http_get_path(htx_sl_req_uri(sl));
2428 /* build message using path */
Tim Duesterhused526372020-03-05 17:56:33 +01002429 if (isttest(path)) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002430 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2431 int qs = 0;
2432 while (qs < path.len) {
2433 if (*(path.ptr + qs) == '?') {
2434 path.len = qs;
2435 break;
2436 }
2437 qs++;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002438 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002439 }
2440 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002441 else
2442 path = ist("/");
Christopher Fauletf2824e62018-10-01 12:12:37 +02002443
Christopher Faulet99daf282018-11-28 22:58:13 +01002444 if (rule->rdr_str) { /* this is an old "redirect" rule */
2445 /* add prefix. Note that if prefix == "/", we don't want to
2446 * add anything, otherwise it makes it hard for the user to
2447 * configure a self-redirection.
2448 */
2449 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
2450 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2451 goto fail;
2452 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002453 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002454 else {
2455 /* add prefix with executing log format */
2456 chunk->data += build_logline(s, chunk->area + chunk->data,
2457 chunk->size - chunk->data,
2458 &rule->rdr_fmt);
2459 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002460
Christopher Faulet99daf282018-11-28 22:58:13 +01002461 /* add path */
2462 if (!chunk_memcat(chunk, path.ptr, path.len))
2463 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002464
Christopher Faulet99daf282018-11-28 22:58:13 +01002465 /* append a slash at the end of the location if needed and missing */
2466 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
2467 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
2468 if (chunk->data + 1 >= chunk->size)
2469 goto fail;
2470 chunk->area[chunk->data++] = '/';
2471 }
2472 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002473 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002474 case REDIRECT_TYPE_LOCATION:
2475 default:
2476 if (rule->rdr_str) { /* this is an old "redirect" rule */
2477 /* add location */
2478 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2479 goto fail;
2480 }
2481 else {
2482 /* add location with executing log format */
2483 chunk->data += build_logline(s, chunk->area + chunk->data,
2484 chunk->size - chunk->data,
2485 &rule->rdr_fmt);
2486 }
2487 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002488 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002489 location = ist2(chunk->area, chunk->data);
2490
2491 /*
2492 * Create the 30x response
2493 */
2494 switch (rule->code) {
2495 case 308:
2496 status = ist("308");
2497 reason = ist("Permanent Redirect");
2498 break;
2499 case 307:
2500 status = ist("307");
2501 reason = ist("Temporary Redirect");
2502 break;
2503 case 303:
2504 status = ist("303");
2505 reason = ist("See Other");
2506 break;
2507 case 301:
2508 status = ist("301");
2509 reason = ist("Moved Permanently");
2510 break;
2511 case 302:
2512 default:
2513 status = ist("302");
2514 reason = ist("Found");
2515 break;
2516 }
2517
Christopher Faulet08e66462019-05-23 16:44:59 +02002518 if (!(txn->req.flags & HTTP_MSGF_BODYLESS) && txn->req.msg_state != HTTP_MSG_DONE)
2519 close = 1;
2520
Christopher Faulet99daf282018-11-28 22:58:13 +01002521 htx = htx_from_buf(&res->buf);
Kevin Zhu96b36392020-01-07 09:42:55 +01002522 /* Trim any possible response */
2523 channel_htx_truncate(&s->res, htx);
Christopher Faulet99daf282018-11-28 22:58:13 +01002524 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
2525 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), status, reason);
2526 if (!sl)
2527 goto fail;
2528 sl->info.res.status = rule->code;
2529 s->txn->status = rule->code;
2530
Christopher Faulet08e66462019-05-23 16:44:59 +02002531 if (close && !htx_add_header(htx, ist("Connection"), ist("close")))
2532 goto fail;
2533
2534 if (!htx_add_header(htx, ist("Content-length"), ist("0")) ||
Christopher Faulet99daf282018-11-28 22:58:13 +01002535 !htx_add_header(htx, ist("Location"), location))
2536 goto fail;
2537
2538 if (rule->code == 302 || rule->code == 303 || rule->code == 307) {
2539 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")))
2540 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002541 }
2542
2543 if (rule->cookie_len) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002544 if (!htx_add_header(htx, ist("Set-Cookie"), ist2(rule->cookie_str, rule->cookie_len)))
2545 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002546 }
2547
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002548 if (!htx_add_endof(htx, HTX_BLK_EOH))
Christopher Faulet99daf282018-11-28 22:58:13 +01002549 goto fail;
2550
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002551 htx->flags |= HTX_FL_EOM;
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
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004094 if (!htx_add_endof(htx, HTX_BLK_EOH))
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004095 goto fail;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004096
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004097 htx->flags |= HTX_FL_EOM;
Christopher Fauletc20afb82020-01-24 19:16:26 +01004098 htx_to_buf(htx, &res->buf);
Christopher Fauleta72a7e42020-01-28 09:28:11 +01004099 if (!http_forward_proxy_resp(s, 1))
4100 goto fail;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004101
4102 /* return without error. */
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004103 si_shutr(si);
4104 si_shutw(si);
4105 si->err_type = SI_ET_NONE;
4106 si->state = SI_ST_CLO;
4107
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004108 if (!(s->flags & SF_ERR_MASK))
4109 s->flags |= SF_ERR_LOCAL;
4110 if (!(s->flags & SF_FINST_MASK))
4111 s->flags |= SF_FINST_C;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004112
4113 /* FIXME: we should increase a counter of redirects per server and per backend. */
4114 srv_inc_sess_ctr(srv);
4115 srv_set_sess_last(srv);
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004116 return;
4117
4118 fail:
4119 /* If an error occurred, remove the incomplete HTTP response from the
4120 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004121 channel_htx_truncate(res, htx);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004122}
4123
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05004124/* This function terminates the request because it was completely analyzed or
Christopher Fauletf2824e62018-10-01 12:12:37 +02004125 * because an error was triggered during the body forwarding.
4126 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004127static void http_end_request(struct stream *s)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004128{
4129 struct channel *chn = &s->req;
4130 struct http_txn *txn = s->txn;
4131
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004132 DBG_TRACE_ENTER(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004133
Christopher Fauletb42a8b62018-11-19 21:59:00 +01004134 if (unlikely(txn->req.msg_state == HTTP_MSG_ERROR ||
4135 txn->rsp.msg_state == HTTP_MSG_ERROR)) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004136 channel_abort(chn);
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004137 channel_htx_truncate(chn, htxbuf(&chn->buf));
Christopher Fauletf2824e62018-10-01 12:12:37 +02004138 goto end;
4139 }
4140
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004141 if (unlikely(txn->req.msg_state < HTTP_MSG_DONE)) {
4142 DBG_TRACE_DEVEL("waiting end of the request", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004143 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004144 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004145
4146 if (txn->req.msg_state == HTTP_MSG_DONE) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004147 /* No need to read anymore, the request was completely parsed.
4148 * We can shut the read side unless we want to abort_on_close,
4149 * or we have a POST request. The issue with POST requests is
4150 * that some browsers still send a CRLF after the request, and
4151 * this CRLF must be read so that it does not remain in the kernel
4152 * buffers, otherwise a close could cause an RST on some systems
4153 * (eg: Linux).
4154 */
Christopher Faulet769d0e92019-03-22 14:23:18 +01004155 if (!(s->be->options & PR_O_ABRT_CLOSE) && txn->meth != HTTP_METH_POST)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004156 channel_dont_read(chn);
4157
4158 /* if the server closes the connection, we want to immediately react
4159 * and close the socket to save packets and syscalls.
4160 */
4161 s->si[1].flags |= SI_FL_NOHALF;
4162
4163 /* In any case we've finished parsing the request so we must
4164 * disable Nagle when sending data because 1) we're not going
4165 * to shut this side, and 2) the server is waiting for us to
4166 * send pending data.
4167 */
4168 chn->flags |= CF_NEVER_WAIT;
4169
Christopher Fauletd01ce402019-01-02 17:44:13 +01004170 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
4171 /* The server has not finished to respond, so we
4172 * don't want to move in order not to upset it.
4173 */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004174 DBG_TRACE_DEVEL("waiting end of the response", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletd01ce402019-01-02 17:44:13 +01004175 return;
4176 }
4177
Christopher Fauletf2824e62018-10-01 12:12:37 +02004178 /* When we get here, it means that both the request and the
4179 * response have finished receiving. Depending on the connection
4180 * mode, we'll have to wait for the last bytes to leave in either
4181 * direction, and sometimes for a close to be effective.
4182 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02004183 if (txn->flags & TX_CON_WANT_TUN) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004184 /* Tunnel mode will not have any analyser so it needs to
4185 * poll for reads.
4186 */
4187 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004188 if (b_data(&chn->buf)) {
4189 DBG_TRACE_DEVEL("waiting to flush the request", STRM_EV_HTTP_ANA, s, txn);
Christopher Faulet9768c262018-10-22 09:34:31 +02004190 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004191 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004192 txn->req.msg_state = HTTP_MSG_TUNNEL;
4193 }
4194 else {
4195 /* we're not expecting any new data to come for this
4196 * transaction, so we can close it.
Christopher Faulet9768c262018-10-22 09:34:31 +02004197 *
4198 * However, there is an exception if the response
4199 * length is undefined. In this case, we need to wait
4200 * the close from the server. The response will be
4201 * switched in TUNNEL mode until the end.
Christopher Fauletf2824e62018-10-01 12:12:37 +02004202 */
4203 if (!(txn->rsp.flags & HTTP_MSGF_XFER_LEN) &&
4204 txn->rsp.msg_state != HTTP_MSG_CLOSED)
Christopher Faulet9768c262018-10-22 09:34:31 +02004205 goto check_channel_flags;
Christopher Fauletf2824e62018-10-01 12:12:37 +02004206
4207 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4208 channel_shutr_now(chn);
4209 channel_shutw_now(chn);
4210 }
4211 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004212 goto check_channel_flags;
4213 }
4214
4215 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
4216 http_msg_closing:
4217 /* nothing else to forward, just waiting for the output buffer
4218 * to be empty and for the shutw_now to take effect.
4219 */
4220 if (channel_is_empty(chn)) {
4221 txn->req.msg_state = HTTP_MSG_CLOSED;
4222 goto http_msg_closed;
4223 }
4224 else if (chn->flags & CF_SHUTW) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004225 txn->req.msg_state = HTTP_MSG_ERROR;
4226 goto end;
4227 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004228 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004229 return;
4230 }
4231
4232 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
4233 http_msg_closed:
Christopher Fauletf2824e62018-10-01 12:12:37 +02004234 /* if we don't know whether the server will close, we need to hard close */
4235 if (txn->rsp.flags & HTTP_MSGF_XFER_LEN)
4236 s->si[1].flags |= SI_FL_NOLINGER; /* we want to close ASAP */
Christopher Fauletf2824e62018-10-01 12:12:37 +02004237 /* see above in MSG_DONE why we only do this in these states */
Christopher Faulet769d0e92019-03-22 14:23:18 +01004238 if (!(s->be->options & PR_O_ABRT_CLOSE))
Christopher Fauletf2824e62018-10-01 12:12:37 +02004239 channel_dont_read(chn);
4240 goto end;
4241 }
4242
4243 check_channel_flags:
4244 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4245 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4246 /* if we've just closed an output, let's switch */
4247 txn->req.msg_state = HTTP_MSG_CLOSING;
4248 goto http_msg_closing;
4249 }
4250
4251 end:
4252 chn->analysers &= AN_REQ_FLT_END;
Christopher Faulet198ef8b2020-12-15 13:32:55 +01004253 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
4254 chn->flags |= CF_NEVER_WAIT;
4255 if (HAS_REQ_DATA_FILTERS(s))
Christopher Fauletf2824e62018-10-01 12:12:37 +02004256 chn->analysers |= AN_REQ_FLT_XFER_DATA;
Christopher Faulet198ef8b2020-12-15 13:32:55 +01004257 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004258 channel_auto_close(chn);
4259 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004260 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004261}
4262
4263
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05004264/* This function terminates the response because it was completely analyzed or
Christopher Fauletf2824e62018-10-01 12:12:37 +02004265 * because an error was triggered during the body forwarding.
4266 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004267static void http_end_response(struct stream *s)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004268{
4269 struct channel *chn = &s->res;
4270 struct http_txn *txn = s->txn;
4271
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004272 DBG_TRACE_ENTER(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004273
Christopher Fauletb42a8b62018-11-19 21:59:00 +01004274 if (unlikely(txn->req.msg_state == HTTP_MSG_ERROR ||
4275 txn->rsp.msg_state == HTTP_MSG_ERROR)) {
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004276 channel_htx_truncate(&s->req, htxbuf(&s->req.buf));
Christopher Faulet9768c262018-10-22 09:34:31 +02004277 channel_abort(&s->req);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004278 goto end;
4279 }
4280
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004281 if (unlikely(txn->rsp.msg_state < HTTP_MSG_DONE)) {
4282 DBG_TRACE_DEVEL("waiting end of the response", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004283 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004284 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004285
4286 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
4287 /* In theory, we don't need to read anymore, but we must
4288 * still monitor the server connection for a possible close
4289 * while the request is being uploaded, so we don't disable
4290 * reading.
4291 */
4292 /* channel_dont_read(chn); */
4293
4294 if (txn->req.msg_state < HTTP_MSG_DONE) {
4295 /* The client seems to still be sending data, probably
4296 * because we got an error response during an upload.
4297 * We have the choice of either breaking the connection
4298 * or letting it pass through. Let's do the later.
4299 */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004300 DBG_TRACE_DEVEL("waiting end of the request", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004301 return;
4302 }
4303
4304 /* When we get here, it means that both the request and the
4305 * response have finished receiving. Depending on the connection
4306 * mode, we'll have to wait for the last bytes to leave in either
4307 * direction, and sometimes for a close to be effective.
4308 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02004309 if (txn->flags & TX_CON_WANT_TUN) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004310 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004311 if (b_data(&chn->buf)) {
4312 DBG_TRACE_DEVEL("waiting to flush the respone", STRM_EV_HTTP_ANA, s, txn);
Christopher Faulet9768c262018-10-22 09:34:31 +02004313 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004314 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004315 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
4316 }
4317 else {
4318 /* we're not expecting any new data to come for this
4319 * transaction, so we can close it.
4320 */
4321 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4322 channel_shutr_now(chn);
4323 channel_shutw_now(chn);
4324 }
4325 }
4326 goto check_channel_flags;
4327 }
4328
4329 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
4330 http_msg_closing:
4331 /* nothing else to forward, just waiting for the output buffer
4332 * to be empty and for the shutw_now to take effect.
4333 */
4334 if (channel_is_empty(chn)) {
4335 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4336 goto http_msg_closed;
4337 }
4338 else if (chn->flags & CF_SHUTW) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004339 txn->rsp.msg_state = HTTP_MSG_ERROR;
Christopher Fauletcff0f732019-12-16 16:13:44 +01004340 _HA_ATOMIC_ADD(&strm_sess(s)->fe->fe_counters.cli_aborts, 1);
Olivier Houcharda798bf52019-03-08 18:52:00 +01004341 _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01004342 if (strm_sess(s)->listener->counters)
4343 _HA_ATOMIC_ADD(&strm_sess(s)->listener->counters->cli_aborts, 1);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004344 if (objt_server(s->target))
Christopher Fauletcff0f732019-12-16 16:13:44 +01004345 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.cli_aborts, 1);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004346 goto end;
4347 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004348 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004349 return;
4350 }
4351
4352 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
4353 http_msg_closed:
4354 /* drop any pending data */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004355 channel_htx_truncate(&s->req, htxbuf(&s->req.buf));
Christopher Faulet9768c262018-10-22 09:34:31 +02004356 channel_abort(&s->req);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004357 goto end;
4358 }
4359
4360 check_channel_flags:
4361 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4362 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4363 /* if we've just closed an output, let's switch */
4364 txn->rsp.msg_state = HTTP_MSG_CLOSING;
4365 goto http_msg_closing;
4366 }
4367
4368 end:
4369 chn->analysers &= AN_RES_FLT_END;
Christopher Faulet198ef8b2020-12-15 13:32:55 +01004370 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
4371 chn->flags |= CF_NEVER_WAIT;
4372 if (HAS_RSP_DATA_FILTERS(s))
4373 chn->analysers |= AN_RES_FLT_XFER_DATA;
4374 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004375 channel_auto_close(chn);
4376 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004377 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004378}
4379
Christopher Fauletef70e252020-01-28 09:26:19 +01004380/* Forward a response generated by HAProxy (error/redirect/return). This
4381 * function forwards all pending incoming data. If <final> is set to 0, nothing
4382 * more is performed. It is used for 1xx informational messages. Otherwise, the
Christopher Faulet507479b2020-05-15 12:29:46 +02004383 * transaction is terminated and the request is emptied. On success 1 is
Christopher Faulet40e6b552020-06-25 16:04:50 +02004384 * returned. If an error occurred, 0 is returned. If it fails, this function
4385 * only exits. It is the caller responsibility to do the cleanup.
Christopher Fauletef70e252020-01-28 09:26:19 +01004386 */
4387int http_forward_proxy_resp(struct stream *s, int final)
4388{
4389 struct channel *req = &s->req;
4390 struct channel *res = &s->res;
4391 struct htx *htx = htxbuf(&res->buf);
4392 size_t data;
4393
4394 if (final) {
4395 htx->flags |= HTX_FL_PROXY_RESP;
Christopher Faulet507479b2020-05-15 12:29:46 +02004396
Christopher Fauletaab1b672020-11-18 16:44:02 +01004397 if (!htx_is_empty(htx) && !http_eval_after_res_rules(s))
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01004398 return 0;
Christopher Fauletef70e252020-01-28 09:26:19 +01004399
Christopher Fauletd6c48362020-10-19 18:01:38 +02004400 if (s->txn->meth == HTTP_METH_HEAD)
4401 htx_skip_msg_payload(htx);
4402
Christopher Fauletef70e252020-01-28 09:26:19 +01004403 channel_auto_read(req);
4404 channel_abort(req);
4405 channel_auto_close(req);
4406 channel_htx_erase(req, htxbuf(&req->buf));
4407
4408 res->wex = tick_add_ifset(now_ms, res->wto);
4409 channel_auto_read(res);
4410 channel_auto_close(res);
4411 channel_shutr_now(res);
Christopher Faulet1a9db7c2020-06-25 15:36:45 +02004412 res->flags |= CF_EOI; /* The response is terminated, add EOI */
Christopher Faulet42432f32020-11-20 17:43:16 +01004413 htxbuf(&res->buf)->flags |= HTX_FL_EOM; /* no more data are expected */
Christopher Fauletef70e252020-01-28 09:26:19 +01004414 }
Christopher Fauletcf6898c2020-06-25 15:55:11 +02004415 else {
4416 /* Send ASAP informational messages. Rely on CF_EOI for final
4417 * response.
4418 */
4419 res->flags |= CF_SEND_DONTWAIT;
4420 }
Christopher Fauletef70e252020-01-28 09:26:19 +01004421
4422 data = htx->data - co_data(res);
4423 c_adv(res, data);
4424 htx->first = -1;
4425 res->total += data;
4426 return 1;
4427}
4428
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004429void http_server_error(struct stream *s, struct stream_interface *si, int err,
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004430 int finst, struct http_reply *msg)
Christopher Faulet0f226952018-10-22 09:29:56 +02004431{
Christopher Faulet72c7d8d2020-01-27 15:32:25 +01004432 http_reply_and_close(s, s->txn->status, msg);
Christopher Faulet0f226952018-10-22 09:29:56 +02004433 if (!(s->flags & SF_ERR_MASK))
4434 s->flags |= err;
4435 if (!(s->flags & SF_FINST_MASK))
4436 s->flags |= finst;
4437}
4438
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004439void http_reply_and_close(struct stream *s, short status, struct http_reply *msg)
Christopher Faulet0f226952018-10-22 09:29:56 +02004440{
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004441 if (!msg) {
4442 channel_htx_truncate(&s->res, htxbuf(&s->res.buf));
4443 goto end;
4444 }
4445
4446 if (http_reply_message(s, msg) == -1) {
4447 /* On error, return a 500 error message, but don't rewrite it if
Christopher Faulet40e6b552020-06-25 16:04:50 +02004448 * it is already an internal error. If it was already a "const"
4449 * 500 error, just fail.
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004450 */
Christopher Faulet40e6b552020-06-25 16:04:50 +02004451 if (s->txn->status == 500) {
4452 if (s->txn->flags & TX_CONST_REPLY)
4453 goto end;
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004454 s->txn->flags |= TX_CONST_REPLY;
Christopher Faulet40e6b552020-06-25 16:04:50 +02004455 }
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004456 s->txn->status = 500;
4457 s->txn->http_reply = NULL;
4458 return http_reply_and_close(s, s->txn->status, http_error_message(s));
4459 }
4460
4461end:
4462 s->res.wex = tick_add_ifset(now_ms, s->res.wto);
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004463
Christopher Faulet0f226952018-10-22 09:29:56 +02004464 channel_auto_read(&s->req);
4465 channel_abort(&s->req);
4466 channel_auto_close(&s->req);
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004467 channel_htx_erase(&s->req, htxbuf(&s->req.buf));
Christopher Faulet72c7d8d2020-01-27 15:32:25 +01004468 channel_auto_read(&s->res);
4469 channel_auto_close(&s->res);
4470 channel_shutr_now(&s->res);
Christopher Faulet0f226952018-10-22 09:29:56 +02004471}
4472
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004473struct http_reply *http_error_message(struct stream *s)
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004474{
4475 const int msgnum = http_get_status_idx(s->txn->status);
4476
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004477 if (s->txn->http_reply)
4478 return s->txn->http_reply;
4479 else if (s->be->replies[msgnum])
4480 return s->be->replies[msgnum];
4481 else if (strm_fe(s)->replies[msgnum])
4482 return strm_fe(s)->replies[msgnum];
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004483 else
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004484 return &http_err_replies[msgnum];
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004485}
4486
Christopher Faulet40e6b552020-06-25 16:04:50 +02004487/* Produces an HTX message from an http reply. Depending on the http reply type,
4488 * a, errorfile, an raw file or a log-format string is used. On success, it
4489 * returns 0. If an error occurs -1 is returned. If it fails, this function only
4490 * exits. It is the caller responsibility to do the cleanup.
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004491 */
Christopher Fauletae43b6c2020-05-27 15:24:22 +02004492int http_reply_to_htx(struct stream *s, struct htx *htx, struct http_reply *reply)
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004493{
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004494 struct buffer *errmsg;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004495 struct htx_sl *sl;
4496 struct buffer *body = NULL;
4497 const char *status, *reason, *clen, *ctype;
4498 unsigned int slflags;
4499 int ret = 0;
4500
Christopher Faulete29a97e2020-05-14 14:49:25 +02004501 /*
4502 * - HTTP_REPLY_ERRFILES unexpected here. handled as no payload if so
4503 *
4504 * - HTTP_REPLY_INDIRECT: switch on another reply if defined or handled
4505 * as no payload if NULL. the TXN status code is set with the status
4506 * of the original reply.
4507 */
4508
4509 if (reply->type == HTTP_REPLY_INDIRECT) {
4510 if (reply->body.reply)
4511 reply = reply->body.reply;
4512 }
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004513 if (reply->type == HTTP_REPLY_ERRMSG && !reply->body.errmsg) {
4514 /* get default error message */
4515 if (reply == s->txn->http_reply)
4516 s->txn->http_reply = NULL;
4517 reply = http_error_message(s);
4518 if (reply->type == HTTP_REPLY_INDIRECT) {
4519 if (reply->body.reply)
4520 reply = reply->body.reply;
4521 }
4522 }
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004523
4524 if (reply->type == HTTP_REPLY_ERRMSG) {
4525 /* implicit or explicit error message*/
4526 errmsg = reply->body.errmsg;
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004527 if (errmsg && !b_is_null(errmsg)) {
Christopher Faulet20567362020-05-15 14:52:49 +02004528 if (!htx_copy_msg(htx, errmsg))
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004529 goto fail;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004530 }
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004531 }
4532 else {
4533 /* no payload, file or log-format string */
4534 if (reply->type == HTTP_REPLY_RAW) {
4535 /* file */
4536 body = &reply->body.obj;
4537 }
4538 else if (reply->type == HTTP_REPLY_LOGFMT) {
4539 /* log-format string */
4540 body = alloc_trash_chunk();
4541 if (!body)
4542 goto fail_alloc;
4543 body->data = build_logline(s, body->area, body->size, &reply->body.fmt);
4544 }
4545 /* else no payload */
4546
4547 status = ultoa(reply->status);
4548 reason = http_get_reason(reply->status);
4549 slflags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_CLEN);
4550 if (!body || !b_data(body))
4551 slflags |= HTX_SL_F_BODYLESS;
4552 sl = htx_add_stline(htx, HTX_BLK_RES_SL, slflags, ist("HTTP/1.1"), ist(status), ist(reason));
4553 if (!sl)
4554 goto fail;
4555 sl->info.res.status = reply->status;
4556
4557 clen = (body ? ultoa(b_data(body)) : "0");
4558 ctype = reply->ctype;
4559
4560 if (!LIST_ISEMPTY(&reply->hdrs)) {
4561 struct http_reply_hdr *hdr;
4562 struct buffer *value = alloc_trash_chunk();
4563
4564 if (!value)
4565 goto fail;
4566
4567 list_for_each_entry(hdr, &reply->hdrs, list) {
4568 chunk_reset(value);
4569 value->data = build_logline(s, value->area, value->size, &hdr->value);
4570 if (b_data(value) && !htx_add_header(htx, hdr->name, ist2(b_head(value), b_data(value)))) {
4571 free_trash_chunk(value);
4572 goto fail;
4573 }
4574 chunk_reset(value);
4575 }
4576 free_trash_chunk(value);
4577 }
4578
4579 if (!htx_add_header(htx, ist("content-length"), ist(clen)) ||
4580 (body && b_data(body) && ctype && !htx_add_header(htx, ist("content-type"), ist(ctype))) ||
4581 !htx_add_endof(htx, HTX_BLK_EOH) ||
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004582 (body && b_data(body) && !htx_add_data_atonce(htx, ist2(b_head(body), b_data(body)))))
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004583 goto fail;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004584
4585 htx->flags |= HTX_FL_EOM;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004586 }
4587
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004588 leave:
4589 if (reply->type == HTTP_REPLY_LOGFMT)
4590 free_trash_chunk(body);
4591 return ret;
4592
4593 fail_alloc:
4594 if (!(s->flags & SF_ERR_MASK))
4595 s->flags |= SF_ERR_RESOURCE;
Christopher Faulet97e466c2020-05-15 15:12:47 +02004596 /* fall through */
4597 fail:
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004598 ret = -1;
4599 goto leave;
Christopher Faulet97e466c2020-05-15 15:12:47 +02004600}
4601
4602/* Send an http reply to the client. On success, it returns 0. If an error
Christopher Faulet40e6b552020-06-25 16:04:50 +02004603 * occurs -1 is returned and the response channel is truncated, removing this
4604 * way the faulty reply. This function may fail when the reply is formatted
4605 * (http_reply_to_htx) or when the reply is forwarded
4606 * (http_forward_proxy_resp). On the last case, it is because a
4607 * http-after-response rule fails.
Christopher Faulet97e466c2020-05-15 15:12:47 +02004608 */
4609int http_reply_message(struct stream *s, struct http_reply *reply)
4610{
4611 struct channel *res = &s->res;
4612 struct htx *htx = htx_from_buf(&res->buf);
4613
4614 if (s->txn->status == -1)
4615 s->txn->status = reply->status;
4616 channel_htx_truncate(res, htx);
4617
4618 if (http_reply_to_htx(s, htx, reply) == -1)
4619 goto fail;
4620
4621 htx_to_buf(htx, &s->res.buf);
4622 if (!http_forward_proxy_resp(s, 1))
4623 goto fail;
4624 return 0;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004625
4626 fail:
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004627 channel_htx_truncate(res, htx);
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004628 if (!(s->flags & SF_ERR_MASK))
4629 s->flags |= SF_ERR_PRXCOND;
Christopher Faulet97e466c2020-05-15 15:12:47 +02004630 return -1;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004631}
4632
Christopher Faulet304cc402019-07-15 15:46:28 +02004633/* Return the error message corresponding to si->err_type. It is assumed
4634 * that the server side is closed. Note that err_type is actually a
4635 * bitmask, where almost only aborts may be cumulated with other
4636 * values. We consider that aborted operations are more important
4637 * than timeouts or errors due to the fact that nobody else in the
4638 * logs might explain incomplete retries. All others should avoid
4639 * being cumulated. It should normally not be possible to have multiple
4640 * aborts at once, but just in case, the first one in sequence is reported.
4641 * Note that connection errors appearing on the second request of a keep-alive
4642 * connection are not reported since this allows the client to retry.
4643 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004644void http_return_srv_error(struct stream *s, struct stream_interface *si)
Christopher Faulet304cc402019-07-15 15:46:28 +02004645{
4646 int err_type = si->err_type;
4647
4648 /* set s->txn->status for http_error_message(s) */
4649 s->txn->status = 503;
4650
4651 if (err_type & SI_ET_QUEUE_ABRT)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004652 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_Q,
4653 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004654 else if (err_type & SI_ET_CONN_ABRT)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004655 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_C,
4656 (s->txn->flags & TX_NOT_FIRST) ? NULL :
4657 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004658 else if (err_type & SI_ET_QUEUE_TO)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004659 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_Q,
4660 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004661 else if (err_type & SI_ET_QUEUE_ERR)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004662 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_Q,
4663 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004664 else if (err_type & SI_ET_CONN_TO)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004665 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_C,
4666 (s->txn->flags & TX_NOT_FIRST) ? NULL :
4667 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004668 else if (err_type & SI_ET_CONN_ERR)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004669 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_C,
4670 (s->flags & SF_SRV_REUSED) ? NULL :
4671 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004672 else if (err_type & SI_ET_CONN_RES)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004673 http_server_error(s, si, SF_ERR_RESOURCE, SF_FINST_C,
4674 (s->txn->flags & TX_NOT_FIRST) ? NULL :
4675 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004676 else { /* SI_ET_CONN_OTHER and others */
4677 s->txn->status = 500;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004678 http_server_error(s, si, SF_ERR_INTERNAL, SF_FINST_C,
4679 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004680 }
4681}
4682
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004683
Christopher Faulet4a28a532019-03-01 11:19:40 +01004684/* Handle Expect: 100-continue for HTTP/1.1 messages if necessary. It returns 0
4685 * on success and -1 on error.
4686 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004687static int http_handle_expect_hdr(struct stream *s, struct htx *htx, struct http_msg *msg)
Christopher Faulet4a28a532019-03-01 11:19:40 +01004688{
4689 /* If we have HTTP/1.1 message with a body and Expect: 100-continue,
4690 * then we must send an HTTP/1.1 100 Continue intermediate response.
4691 */
4692 if (msg->msg_state == HTTP_MSG_BODY && (msg->flags & HTTP_MSGF_VER_11) &&
4693 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
4694 struct ist hdr = { .ptr = "Expect", .len = 6 };
4695 struct http_hdr_ctx ctx;
4696
4697 ctx.blk = NULL;
4698 /* Expect is allowed in 1.1, look for it */
4699 if (http_find_header(htx, hdr, &ctx, 0) &&
4700 unlikely(isteqi(ctx.value, ist2("100-continue", 12)))) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004701 if (http_reply_100_continue(s) == -1)
Christopher Faulet4a28a532019-03-01 11:19:40 +01004702 return -1;
4703 http_remove_header(htx, &ctx);
4704 }
4705 }
4706 return 0;
4707}
4708
Christopher Faulet23a3c792018-11-28 10:01:23 +01004709/* Send a 100-Continue response to the client. It returns 0 on success and -1
4710 * on error. The response channel is updated accordingly.
4711 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004712static int http_reply_100_continue(struct stream *s)
Christopher Faulet23a3c792018-11-28 10:01:23 +01004713{
4714 struct channel *res = &s->res;
4715 struct htx *htx = htx_from_buf(&res->buf);
4716 struct htx_sl *sl;
4717 unsigned int flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|
4718 HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
Christopher Faulet23a3c792018-11-28 10:01:23 +01004719
4720 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
4721 ist("HTTP/1.1"), ist("100"), ist("Continue"));
4722 if (!sl)
4723 goto fail;
4724 sl->info.res.status = 100;
4725
Christopher Faulet1d5ec092019-06-26 14:23:54 +02004726 if (!htx_add_endof(htx, HTX_BLK_EOH))
Christopher Faulet23a3c792018-11-28 10:01:23 +01004727 goto fail;
4728
Christopher Fauleta72a7e42020-01-28 09:28:11 +01004729 if (!http_forward_proxy_resp(s, 0))
4730 goto fail;
Christopher Faulet23a3c792018-11-28 10:01:23 +01004731 return 0;
4732
4733 fail:
4734 /* If an error occurred, remove the incomplete HTTP response from the
4735 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004736 channel_htx_truncate(res, htx);
Christopher Faulet23a3c792018-11-28 10:01:23 +01004737 return -1;
4738}
4739
Christopher Faulet12c51e22018-11-28 15:59:42 +01004740
Christopher Faulet0f226952018-10-22 09:29:56 +02004741/*
4742 * Capture headers from message <htx> according to header list <cap_hdr>, and
4743 * fill the <cap> pointers appropriately.
4744 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004745static void http_capture_headers(struct htx *htx, char **cap, struct cap_hdr *cap_hdr)
Christopher Faulet0f226952018-10-22 09:29:56 +02004746{
4747 struct cap_hdr *h;
4748 int32_t pos;
4749
Christopher Fauleta3f15502019-05-13 15:27:23 +02004750 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet0f226952018-10-22 09:29:56 +02004751 struct htx_blk *blk = htx_get_blk(htx, pos);
4752 enum htx_blk_type type = htx_get_blk_type(blk);
4753 struct ist n, v;
4754
4755 if (type == HTX_BLK_EOH)
4756 break;
4757 if (type != HTX_BLK_HDR)
4758 continue;
4759
4760 n = htx_get_blk_name(htx, blk);
4761
4762 for (h = cap_hdr; h; h = h->next) {
4763 if (h->namelen && (h->namelen == n.len) &&
4764 (strncasecmp(n.ptr, h->name, h->namelen) == 0)) {
4765 if (cap[h->index] == NULL)
4766 cap[h->index] =
4767 pool_alloc(h->pool);
4768
4769 if (cap[h->index] == NULL) {
4770 ha_alert("HTTP capture : out of memory.\n");
4771 break;
4772 }
4773
4774 v = htx_get_blk_value(htx, blk);
4775 if (v.len > h->len)
4776 v.len = h->len;
4777
4778 memcpy(cap[h->index], v.ptr, v.len);
4779 cap[h->index][v.len]=0;
4780 }
4781 }
4782 }
4783}
4784
Christopher Faulet0b6bdc52018-10-24 11:05:36 +02004785/* Delete a value in a header between delimiters <from> and <next>. The header
4786 * itself is delimited by <start> and <end> pointers. The number of characters
4787 * displaced is returned, and the pointer to the first delimiter is updated if
4788 * required. The function tries as much as possible to respect the following
4789 * principles :
4790 * - replace <from> delimiter by the <next> one unless <from> points to <start>,
4791 * in which case <next> is simply removed
4792 * - set exactly one space character after the new first delimiter, unless there
4793 * are not enough characters in the block being moved to do so.
4794 * - remove unneeded spaces before the previous delimiter and after the new
4795 * one.
4796 *
4797 * It is the caller's responsibility to ensure that :
4798 * - <from> points to a valid delimiter or <start> ;
4799 * - <next> points to a valid delimiter or <end> ;
4800 * - there are non-space chars before <from>.
4801 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004802static int http_del_hdr_value(char *start, char *end, char **from, char *next)
Christopher Faulet0b6bdc52018-10-24 11:05:36 +02004803{
4804 char *prev = *from;
4805
4806 if (prev == start) {
4807 /* We're removing the first value. eat the semicolon, if <next>
4808 * is lower than <end> */
4809 if (next < end)
4810 next++;
4811
4812 while (next < end && HTTP_IS_SPHT(*next))
4813 next++;
4814 }
4815 else {
4816 /* Remove useless spaces before the old delimiter. */
4817 while (HTTP_IS_SPHT(*(prev-1)))
4818 prev--;
4819 *from = prev;
4820
4821 /* copy the delimiter and if possible a space if we're
4822 * not at the end of the line.
4823 */
4824 if (next < end) {
4825 *prev++ = *next++;
4826 if (prev + 1 < next)
4827 *prev++ = ' ';
4828 while (next < end && HTTP_IS_SPHT(*next))
4829 next++;
4830 }
4831 }
4832 memmove(prev, next, end - next);
4833 return (prev - next);
4834}
4835
Christopher Faulet0f226952018-10-22 09:29:56 +02004836
4837/* Formats the start line of the request (without CRLF) and puts it in <str> and
Joseph Herlantc42c0e92018-11-25 10:43:27 -08004838 * return the written length. The line can be truncated if it exceeds <len>.
Christopher Faulet0f226952018-10-22 09:29:56 +02004839 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004840static size_t http_fmt_req_line(const struct htx_sl *sl, char *str, size_t len)
Christopher Faulet0f226952018-10-22 09:29:56 +02004841{
4842 struct ist dst = ist2(str, 0);
4843
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004844 if (istcat(&dst, htx_sl_req_meth(sl), len) == -1)
Christopher Faulet0f226952018-10-22 09:29:56 +02004845 goto end;
4846 if (dst.len + 1 > len)
4847 goto end;
4848 dst.ptr[dst.len++] = ' ';
4849
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004850 if (istcat(&dst, htx_sl_req_uri(sl), len) == -1)
Christopher Faulet0f226952018-10-22 09:29:56 +02004851 goto end;
4852 if (dst.len + 1 > len)
4853 goto end;
4854 dst.ptr[dst.len++] = ' ';
4855
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004856 istcat(&dst, htx_sl_req_vsn(sl), len);
Christopher Faulet0f226952018-10-22 09:29:56 +02004857 end:
4858 return dst.len;
4859}
4860
4861/*
4862 * Print a debug line with a start line.
4863 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004864static void http_debug_stline(const char *dir, struct stream *s, const struct htx_sl *sl)
Christopher Faulet0f226952018-10-22 09:29:56 +02004865{
4866 struct session *sess = strm_sess(s);
4867 int max;
4868
4869 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
4870 dir,
4871 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->handle.fd : -1,
4872 objt_cs(s->si[1].end) ? (unsigned short)objt_cs(s->si[1].end)->conn->handle.fd : -1);
4873
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004874 max = HTX_SL_P1_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02004875 UBOUND(max, trash.size - trash.data - 3);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004876 chunk_memcat(&trash, HTX_SL_P1_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02004877 trash.area[trash.data++] = ' ';
4878
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004879 max = HTX_SL_P2_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02004880 UBOUND(max, trash.size - trash.data - 2);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004881 chunk_memcat(&trash, HTX_SL_P2_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02004882 trash.area[trash.data++] = ' ';
4883
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004884 max = HTX_SL_P3_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02004885 UBOUND(max, trash.size - trash.data - 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004886 chunk_memcat(&trash, HTX_SL_P3_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02004887 trash.area[trash.data++] = '\n';
4888
Willy Tarreau2e8ab6b2020-03-14 11:03:20 +01004889 DISGUISE(write(1, trash.area, trash.data));
Christopher Faulet0f226952018-10-22 09:29:56 +02004890}
4891
4892/*
4893 * Print a debug line with a header.
4894 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004895static 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 +02004896{
4897 struct session *sess = strm_sess(s);
4898 int max;
4899
4900 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
4901 dir,
4902 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->handle.fd : -1,
4903 objt_cs(s->si[1].end) ? (unsigned short)objt_cs(s->si[1].end)->conn->handle.fd : -1);
4904
4905 max = n.len;
4906 UBOUND(max, trash.size - trash.data - 3);
4907 chunk_memcat(&trash, n.ptr, max);
4908 trash.area[trash.data++] = ':';
4909 trash.area[trash.data++] = ' ';
4910
4911 max = v.len;
4912 UBOUND(max, trash.size - trash.data - 1);
4913 chunk_memcat(&trash, v.ptr, max);
4914 trash.area[trash.data++] = '\n';
4915
Willy Tarreau2e8ab6b2020-03-14 11:03:20 +01004916 DISGUISE(write(1, trash.area, trash.data));
Christopher Faulet0f226952018-10-22 09:29:56 +02004917}
4918
Christopher Fauleta8a46e22019-07-16 14:53:09 +02004919/* Allocate a new HTTP transaction for stream <s> unless there is one already.
4920 * In case of allocation failure, everything allocated is freed and NULL is
4921 * returned. Otherwise the new transaction is assigned to the stream and
4922 * returned.
4923 */
4924struct http_txn *http_alloc_txn(struct stream *s)
4925{
4926 struct http_txn *txn = s->txn;
4927
4928 if (txn)
4929 return txn;
4930
4931 txn = pool_alloc(pool_head_http_txn);
4932 if (!txn)
4933 return txn;
4934
4935 s->txn = txn;
4936 return txn;
4937}
4938
4939void http_txn_reset_req(struct http_txn *txn)
4940{
Christopher Faulet1aea50e2020-01-17 16:03:53 +01004941 txn->req.flags = 0;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02004942 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
4943}
4944
4945void http_txn_reset_res(struct http_txn *txn)
4946{
Christopher Faulet1aea50e2020-01-17 16:03:53 +01004947 txn->rsp.flags = 0;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02004948 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
4949}
4950
4951/*
4952 * Initialize a new HTTP transaction for stream <s>. It is assumed that all
4953 * the required fields are properly allocated and that we only need to (re)init
4954 * them. This should be used before processing any new request.
4955 */
4956void http_init_txn(struct stream *s)
4957{
4958 struct http_txn *txn = s->txn;
4959 struct conn_stream *cs = objt_cs(s->si[0].end);
4960
Christopher Fauletda831fa2020-10-06 17:58:43 +02004961 txn->flags = ((cs && cs->flags & CS_FL_NOT_FIRST) ? TX_NOT_FIRST : 0);
Christopher Fauleta8a46e22019-07-16 14:53:09 +02004962 txn->status = -1;
Christopher Faulet5cb513a2020-05-13 17:56:56 +02004963 txn->http_reply = NULL;
Willy Tarreau8b507582020-02-25 09:35:07 +01004964 write_u32(txn->cache_hash, 0);
Christopher Fauleta8a46e22019-07-16 14:53:09 +02004965
4966 txn->cookie_first_date = 0;
4967 txn->cookie_last_date = 0;
4968
4969 txn->srv_cookie = NULL;
4970 txn->cli_cookie = NULL;
4971 txn->uri = NULL;
4972
4973 http_txn_reset_req(txn);
4974 http_txn_reset_res(txn);
4975
4976 txn->req.chn = &s->req;
4977 txn->rsp.chn = &s->res;
4978
4979 txn->auth.method = HTTP_AUTH_UNKNOWN;
4980
4981 vars_init(&s->vars_txn, SCOPE_TXN);
4982 vars_init(&s->vars_reqres, SCOPE_REQ);
4983}
4984
4985/* to be used at the end of a transaction */
4986void http_end_txn(struct stream *s)
4987{
4988 struct http_txn *txn = s->txn;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02004989
4990 /* these ones will have been dynamically allocated */
4991 pool_free(pool_head_requri, txn->uri);
4992 pool_free(pool_head_capture, txn->cli_cookie);
4993 pool_free(pool_head_capture, txn->srv_cookie);
Tim Duesterhusa17e6622020-03-05 20:19:02 +01004994 pool_free(pool_head_uniqueid, s->unique_id.ptr);
Christopher Fauleta8a46e22019-07-16 14:53:09 +02004995
Tim Duesterhusa17e6622020-03-05 20:19:02 +01004996 s->unique_id = IST_NULL;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02004997 txn->uri = NULL;
4998 txn->srv_cookie = NULL;
4999 txn->cli_cookie = NULL;
5000
Christopher Faulet59399252019-11-07 14:27:52 +01005001 if (!LIST_ISEMPTY(&s->vars_txn.head))
5002 vars_prune(&s->vars_txn, s->sess, s);
5003 if (!LIST_ISEMPTY(&s->vars_reqres.head))
5004 vars_prune(&s->vars_reqres, s->sess, s);
5005}
5006
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005007
5008DECLARE_POOL(pool_head_http_txn, "http_txn", sizeof(struct http_txn));
Christopher Faulet0f226952018-10-22 09:29:56 +02005009
Christopher Fauletf4eb75d2018-10-11 15:55:07 +02005010__attribute__((constructor))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005011static void __http_protocol_init(void)
Christopher Fauletf4eb75d2018-10-11 15:55:07 +02005012{
5013}
5014
5015
5016/*
5017 * Local variables:
5018 * c-indent-level: 8
5019 * c-basic-offset: 8
5020 * End:
5021 */