blob: cf1fb8c317747fc3f4c561779ff8278a6d0a7402 [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;
Christopher Faulet5b82cc52020-10-12 15:18:50 +02001112
1113 if ((s->be->retry_type &~ PR_RE_CONN_FAILED) && !(s->si[1].flags & SI_FL_D_L7_RETRY)) {
1114 struct stream_interface *si = &s->si[1];
1115
1116 /* If we want to be able to do L7 retries, copy the
1117 * request, so that we are able to resend them if
1118 * needed.
1119 *
1120 * Try to allocate a buffer if we had none. If it
1121 * fails, the next test will just disable the l7
1122 * retries.
1123 */
1124 DBG_TRACE_STATE("enable L7 retry, save the request", STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
1125 si->flags |= SI_FL_L7_RETRY;
1126 if (b_is_null(&si->l7_buffer))
1127 b_alloc(&si->l7_buffer);
1128 if (b_is_null(&si->l7_buffer))
1129 si->flags &= ~SI_FL_L7_RETRY;
1130 else {
1131 memcpy(b_orig(&si->l7_buffer), b_orig(&req->buf), b_size(&req->buf));
1132 b_add(&si->l7_buffer, co_data(req));
1133 }
1134 }
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001135 }
1136
1137 done:
1138 /* we don't want to forward closes on DONE except in tunnel mode. */
1139 if (!(txn->flags & TX_CON_WANT_TUN))
1140 channel_dont_close(req);
1141
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001142 http_end_request(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001143 if (!(req->analysers & an_bit)) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001144 http_end_response(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001145 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
1146 if (req->flags & CF_SHUTW) {
1147 /* request errors are most likely due to the
1148 * server aborting the transfer. */
Christopher Faulet93e02d82019-03-08 14:18:50 +01001149 goto return_srv_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001150 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001151 goto return_bad_req;
1152 }
Christopher Faulet5b82cc52020-10-12 15:18:50 +02001153
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001154 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001155 return 1;
1156 }
1157
1158 /* If "option abortonclose" is set on the backend, we want to monitor
1159 * the client's connection and forward any shutdown notification to the
1160 * server, which will decide whether to close or to go on processing the
1161 * request. We only do that in tunnel mode, and not in other modes since
1162 * it can be abused to exhaust source ports. */
Christopher Faulet769d0e92019-03-22 14:23:18 +01001163 if (s->be->options & PR_O_ABRT_CLOSE) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001164 channel_auto_read(req);
Christopher Fauletc41547b2019-07-16 14:32:23 +02001165 if ((req->flags & (CF_SHUTR|CF_READ_NULL)) && !(txn->flags & TX_CON_WANT_TUN))
Christopher Faulete0768eb2018-10-03 16:38:02 +02001166 s->si[1].flags |= SI_FL_NOLINGER;
1167 channel_auto_close(req);
1168 }
1169 else if (s->txn->meth == HTTP_METH_POST) {
1170 /* POST requests may require to read extra CRLF sent by broken
1171 * browsers and which could cause an RST to be sent upon close
1172 * on some systems (eg: Linux). */
1173 channel_auto_read(req);
1174 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001175 DBG_TRACE_DEVEL("waiting for the end of the HTTP txn",
1176 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001177 return 0;
1178
1179 missing_data_or_waiting:
1180 /* stop waiting for data if the input is closed before the end */
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001181 if (msg->msg_state < HTTP_MSG_ENDING && req->flags & CF_SHUTR)
Christopher Faulet93e02d82019-03-08 14:18:50 +01001182 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001183
1184 waiting:
1185 /* waiting for the last bits to leave the buffer */
1186 if (req->flags & CF_SHUTW)
Christopher Faulet93e02d82019-03-08 14:18:50 +01001187 goto return_srv_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001188
1189 /* When TE: chunked is used, we need to get there again to parse remaining
1190 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
1191 * And when content-length is used, we never want to let the possible
1192 * shutdown be forwarded to the other side, as the state machine will
1193 * take care of it once the client responds. It's also important to
1194 * prevent TIME_WAITs from accumulating on the backend side, and for
1195 * HTTP/2 where the last frame comes with a shutdown.
1196 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001197 if (msg->flags & HTTP_MSGF_XFER_LEN)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001198 channel_dont_close(req);
1199
1200 /* We know that more data are expected, but we couldn't send more that
1201 * what we did. So we always set the CF_EXPECT_MORE flag so that the
1202 * system knows it must not set a PUSH on this first part. Interactive
1203 * modes are already handled by the stream sock layer. We must not do
1204 * this in content-length mode because it could present the MSG_MORE
1205 * flag with the last block of forwarded data, which would cause an
1206 * additional delay to be observed by the receiver.
1207 */
Christopher Faulet2151cdd2020-07-22 16:34:59 +02001208 if (HAS_REQ_DATA_FILTERS(s))
Christopher Faulete0768eb2018-10-03 16:38:02 +02001209 req->flags |= CF_EXPECT_MORE;
1210
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001211 DBG_TRACE_DEVEL("waiting for more data to forward",
1212 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001213 return 0;
1214
Christopher Faulet93e02d82019-03-08 14:18:50 +01001215 return_cli_abort:
1216 _HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
1217 _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001218 if (sess->listener->counters)
1219 _HA_ATOMIC_ADD(&sess->listener->counters->cli_aborts, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001220 if (objt_server(s->target))
Christopher Fauletcff0f732019-12-16 16:13:44 +01001221 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.cli_aborts, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001222 if (!(s->flags & SF_ERR_MASK))
1223 s->flags |= SF_ERR_CLICL;
1224 status = 400;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001225 goto return_prx_cond;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001226
1227 return_srv_abort:
1228 _HA_ATOMIC_ADD(&sess->fe->fe_counters.srv_aborts, 1);
1229 _HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001230 if (sess->listener->counters)
1231 _HA_ATOMIC_ADD(&sess->listener->counters->srv_aborts, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001232 if (objt_server(s->target))
Christopher Fauletcff0f732019-12-16 16:13:44 +01001233 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.srv_aborts, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001234 if (!(s->flags & SF_ERR_MASK))
1235 s->flags |= SF_ERR_SRVCL;
1236 status = 502;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001237 goto return_prx_cond;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001238
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001239 return_int_err:
1240 if (!(s->flags & SF_ERR_MASK))
1241 s->flags |= SF_ERR_INTERNAL;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001242 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001243 _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01001244 if (sess->listener->counters)
1245 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001246 if (objt_server(s->target))
1247 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.internal_errors, 1);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001248 status = 500;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001249 goto return_prx_cond;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001250
Christopher Faulet93e02d82019-03-08 14:18:50 +01001251 return_bad_req:
Olivier Houcharda798bf52019-03-08 18:52:00 +01001252 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001253 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +01001254 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001255 status = 400;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001256 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001257
Christopher Fauletb8a53712019-12-16 11:29:38 +01001258 return_prx_cond:
Christopher Faulet9768c262018-10-22 09:34:31 +02001259 if (txn->status > 0) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001260 /* Note: we don't send any error if some data were already sent */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001261 http_reply_and_close(s, txn->status, NULL);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001262 } else {
Christopher Faulet93e02d82019-03-08 14:18:50 +01001263 txn->status = status;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001264 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001265 }
1266 req->analysers &= AN_REQ_FLT_END;
1267 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 +01001268 if (!(s->flags & SF_ERR_MASK))
1269 s->flags |= SF_ERR_PRXCOND;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001270 if (!(s->flags & SF_FINST_MASK))
1271 s->flags |= ((txn->rsp.msg_state < HTTP_MSG_ERROR) ? SF_FINST_H : SF_FINST_D);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001272 DBG_TRACE_DEVEL("leaving on error ",
1273 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001274 return 0;
1275}
1276
Olivier Houcharda254a372019-04-05 15:30:12 +02001277/* Reset the stream and the backend stream_interface to a situation suitable for attemption connection */
1278/* Returns 0 if we can attempt to retry, -1 otherwise */
1279static __inline int do_l7_retry(struct stream *s, struct stream_interface *si)
1280{
Christopher Faulet5b82cc52020-10-12 15:18:50 +02001281 struct channel *req = &s->req;
1282 struct channel *res = &s->res;
Olivier Houcharda254a372019-04-05 15:30:12 +02001283
1284 si->conn_retries--;
1285 if (si->conn_retries < 0)
Christopher Faulet5b82cc52020-10-12 15:18:50 +02001286 goto no_retry;
1287
1288 if (b_is_null(&req->buf) && !channel_alloc_buffer(req, &s->buffer_wait))
1289 goto no_retry;
Olivier Houcharda254a372019-04-05 15:30:12 +02001290
Willy Tarreau223995e2019-05-04 10:38:31 +02001291 if (objt_server(s->target))
1292 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.retries, 1);
1293 _HA_ATOMIC_ADD(&s->be->be_counters.retries, 1);
1294
Olivier Houcharda254a372019-04-05 15:30:12 +02001295 /* Remove any write error from the request, and read error from the response */
1296 req->flags &= ~(CF_WRITE_ERROR | CF_WRITE_TIMEOUT | CF_SHUTW | CF_SHUTW_NOW);
1297 res->flags &= ~(CF_READ_ERROR | CF_READ_TIMEOUT | CF_SHUTR | CF_EOI | CF_READ_NULL | CF_SHUTR_NOW);
1298 res->analysers = 0;
1299 si->flags &= ~(SI_FL_ERR | SI_FL_EXP | SI_FL_RXBLK_SHUT);
Olivier Houchard8cabc972020-05-12 22:18:14 +02001300 s->flags &= ~SF_ADDR_SET;
Olivier Houchard4bd58672019-07-12 16:16:59 +02001301 stream_choose_redispatch(s);
Olivier Houcharda254a372019-04-05 15:30:12 +02001302 si->exp = TICK_ETERNITY;
1303 res->rex = TICK_ETERNITY;
1304 res->to_forward = 0;
1305 res->analyse_exp = TICK_ETERNITY;
1306 res->total = 0;
Olivier Houchard4bd58672019-07-12 16:16:59 +02001307 s->flags &= ~(SF_ERR_SRVTO | SF_ERR_SRVCL);
Olivier Houcharda254a372019-04-05 15:30:12 +02001308 si_release_endpoint(&s->si[1]);
Olivier Houcharda254a372019-04-05 15:30:12 +02001309
Christopher Faulet5b82cc52020-10-12 15:18:50 +02001310 b_reset(&req->buf);
1311 memcpy(b_orig(&req->buf), b_orig(&si->l7_buffer), b_size(&si->l7_buffer));
1312 b_set_data(&req->buf, b_size(&req->buf));
1313 co_set_data(req, b_data(&si->l7_buffer));
1314
1315 DBG_TRACE_DEVEL("perfrom a L7 retry", STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, s->txn);
Olivier Houcharda254a372019-04-05 15:30:12 +02001316 b_reset(&res->buf);
1317 co_set_data(res, 0);
1318 return 0;
Christopher Faulet5b82cc52020-10-12 15:18:50 +02001319
1320 no_retry:
1321 b_free(&si->l7_buffer);
1322 return -1;
Olivier Houcharda254a372019-04-05 15:30:12 +02001323}
1324
Christopher Faulete0768eb2018-10-03 16:38:02 +02001325/* This stream analyser waits for a complete HTTP response. It returns 1 if the
1326 * processing can continue on next analysers, or zero if it either needs more
1327 * data or wants to immediately abort the response (eg: timeout, error, ...). It
1328 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->res.analysers
1329 * when it has nothing left to do, and may remove any analyser when it wants to
1330 * abort.
1331 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001332int http_wait_for_response(struct stream *s, struct channel *rep, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001333{
Christopher Faulet9768c262018-10-22 09:34:31 +02001334 /*
1335 * We will analyze a complete HTTP response to check the its syntax.
1336 *
1337 * Once the start line and all headers are received, we may perform a
1338 * capture of the error (if any), and we will set a few fields. We also
1339 * logging and finally headers capture.
1340 */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001341 struct session *sess = s->sess;
1342 struct http_txn *txn = s->txn;
1343 struct http_msg *msg = &txn->rsp;
Christopher Faulet9768c262018-10-22 09:34:31 +02001344 struct htx *htx;
Olivier Houcharda254a372019-04-05 15:30:12 +02001345 struct stream_interface *si_b = &s->si[1];
Christopher Faulet61608322018-11-23 16:23:45 +01001346 struct connection *srv_conn;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001347 struct htx_sl *sl;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001348 int n;
1349
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001350 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001351
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001352 htx = htxbuf(&rep->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001353
Willy Tarreau4236f032019-03-05 10:43:32 +01001354 /* Parsing errors are caught here */
1355 if (htx->flags & HTX_FL_PARSING_ERROR)
1356 goto return_bad_res;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001357 if (htx->flags & HTX_FL_PROCESSING_ERROR)
1358 goto return_int_err;
Willy Tarreau4236f032019-03-05 10:43:32 +01001359
Christopher Faulete0768eb2018-10-03 16:38:02 +02001360 /*
1361 * Now we quickly check if we have found a full valid response.
1362 * If not so, we check the FD and buffer states before leaving.
1363 * A full response is indicated by the fact that we have seen
1364 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
1365 * responses are checked first.
1366 *
1367 * Depending on whether the client is still there or not, we
1368 * may send an error response back or not. Note that normally
1369 * we should only check for HTTP status there, and check I/O
1370 * errors somewhere else.
1371 */
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001372 next_one:
Christopher Faulet29f17582019-05-23 11:03:26 +02001373 if (unlikely(htx_is_empty(htx) || htx->first == -1)) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001374 /* 1: have we encountered a read error ? */
1375 if (rep->flags & CF_READ_ERROR) {
Olivier Houchard865d8392019-05-03 22:46:27 +02001376 struct connection *conn = NULL;
1377
Olivier Houchard865d8392019-05-03 22:46:27 +02001378 if (objt_cs(s->si[1].end))
1379 conn = objt_cs(s->si[1].end)->conn;
1380
1381 if (si_b->flags & SI_FL_L7_RETRY &&
1382 (!conn || conn->err_code != CO_ER_SSL_EARLY_FAILED)) {
Olivier Houcharda254a372019-04-05 15:30:12 +02001383 /* If we arrive here, then CF_READ_ERROR was
1384 * set by si_cs_recv() because we matched a
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05001385 * status, otherwise it would have removed
Olivier Houcharda254a372019-04-05 15:30:12 +02001386 * the SI_FL_L7_RETRY flag, so it's ok not
1387 * to check s->be->retry_type.
1388 */
Christopher Faulet5b82cc52020-10-12 15:18:50 +02001389 if (co_data(rep) || do_l7_retry(s, si_b) == 0) {
1390 DBG_TRACE_DEVEL("leaving on L7 retry",
1391 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharda254a372019-04-05 15:30:12 +02001392 return 0;
Christopher Faulet5b82cc52020-10-12 15:18:50 +02001393 }
Olivier Houcharda254a372019-04-05 15:30:12 +02001394 }
1395
Olivier Houchard6db16992019-05-17 15:40:49 +02001396 if (txn->flags & TX_NOT_FIRST)
1397 goto abort_keep_alive;
1398
Olivier Houcharda798bf52019-03-08 18:52:00 +01001399 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001400 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001401 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001402 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001403 }
1404
Christopher Faulete0768eb2018-10-03 16:38:02 +02001405 rep->analysers &= AN_RES_FLT_END;
1406 txn->status = 502;
1407
1408 /* Check to see if the server refused the early data.
1409 * If so, just send a 425
1410 */
Willy Tarreauee99aaf2020-06-23 05:58:20 +02001411 if (conn && conn->err_code == CO_ER_SSL_EARLY_FAILED) {
Olivier Houchard865d8392019-05-03 22:46:27 +02001412 if ((s->be->retry_type & PR_RE_EARLY_ERROR) &&
Olivier Houchardad26d8d2019-05-10 17:48:28 +02001413 (si_b->flags & SI_FL_L7_RETRY) &&
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001414 do_l7_retry(s, si_b) == 0) {
1415 DBG_TRACE_DEVEL("leaving on L7 retry",
1416 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houchard865d8392019-05-03 22:46:27 +02001417 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001418 }
Olivier Houchard865d8392019-05-03 22:46:27 +02001419 txn->status = 425;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001420 }
1421
1422 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001423 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001424
1425 if (!(s->flags & SF_ERR_MASK))
1426 s->flags |= SF_ERR_SRVCL;
1427 if (!(s->flags & SF_FINST_MASK))
1428 s->flags |= SF_FINST_H;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001429 DBG_TRACE_DEVEL("leaving on error",
1430 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001431 return 0;
1432 }
1433
Christopher Faulet9768c262018-10-22 09:34:31 +02001434 /* 2: read timeout : return a 504 to the client. */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001435 else if (rep->flags & CF_READ_TIMEOUT) {
Olivier Houcharda254a372019-04-05 15:30:12 +02001436 if ((si_b->flags & SI_FL_L7_RETRY) &&
1437 (s->be->retry_type & PR_RE_TIMEOUT)) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001438 if (co_data(rep) || do_l7_retry(s, si_b) == 0) {
1439 DBG_TRACE_DEVEL("leaving on L7 retry",
1440 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharda254a372019-04-05 15:30:12 +02001441 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001442 }
Olivier Houcharda254a372019-04-05 15:30:12 +02001443 }
Olivier Houcharda798bf52019-03-08 18:52:00 +01001444 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001445 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001446 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001447 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001448 }
1449
Christopher Faulete0768eb2018-10-03 16:38:02 +02001450 rep->analysers &= AN_RES_FLT_END;
1451 txn->status = 504;
1452 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001453 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001454
1455 if (!(s->flags & SF_ERR_MASK))
1456 s->flags |= SF_ERR_SRVTO;
1457 if (!(s->flags & SF_FINST_MASK))
1458 s->flags |= SF_FINST_H;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001459 DBG_TRACE_DEVEL("leaving on error",
1460 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001461 return 0;
1462 }
1463
Christopher Faulet9768c262018-10-22 09:34:31 +02001464 /* 3: client abort with an abortonclose */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001465 else if ((rep->flags & CF_SHUTR) && ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001466 _HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
1467 _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001468 if (sess->listener->counters)
1469 _HA_ATOMIC_ADD(&sess->listener->counters->cli_aborts, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001470 if (objt_server(s->target))
Olivier Houcharda798bf52019-03-08 18:52:00 +01001471 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.cli_aborts, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001472
1473 rep->analysers &= AN_RES_FLT_END;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001474 txn->status = 400;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001475 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001476
1477 if (!(s->flags & SF_ERR_MASK))
1478 s->flags |= SF_ERR_CLICL;
1479 if (!(s->flags & SF_FINST_MASK))
1480 s->flags |= SF_FINST_H;
1481
1482 /* process_stream() will take care of the error */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001483 DBG_TRACE_DEVEL("leaving on error",
1484 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001485 return 0;
1486 }
1487
Christopher Faulet9768c262018-10-22 09:34:31 +02001488 /* 4: close from server, capture the response if the server has started to respond */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001489 else if (rep->flags & CF_SHUTR) {
Olivier Houcharda254a372019-04-05 15:30:12 +02001490 if ((si_b->flags & SI_FL_L7_RETRY) &&
1491 (s->be->retry_type & PR_RE_DISCONNECTED)) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001492 if (co_data(rep) || do_l7_retry(s, si_b) == 0) {
1493 DBG_TRACE_DEVEL("leaving on L7 retry",
1494 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharda254a372019-04-05 15:30:12 +02001495 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001496 }
Olivier Houcharda254a372019-04-05 15:30:12 +02001497 }
1498
Olivier Houchard6db16992019-05-17 15:40:49 +02001499 if (txn->flags & TX_NOT_FIRST)
1500 goto abort_keep_alive;
1501
Olivier Houcharda798bf52019-03-08 18:52:00 +01001502 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001503 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001504 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001505 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001506 }
1507
Christopher Faulete0768eb2018-10-03 16:38:02 +02001508 rep->analysers &= AN_RES_FLT_END;
1509 txn->status = 502;
1510 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001511 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001512
1513 if (!(s->flags & SF_ERR_MASK))
1514 s->flags |= SF_ERR_SRVCL;
1515 if (!(s->flags & SF_FINST_MASK))
1516 s->flags |= SF_FINST_H;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001517 DBG_TRACE_DEVEL("leaving on error",
1518 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001519 return 0;
1520 }
1521
Christopher Faulet9768c262018-10-22 09:34:31 +02001522 /* 5: write error to client (we don't send any message then) */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001523 else if (rep->flags & CF_WRITE_ERROR) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001524 if (txn->flags & TX_NOT_FIRST)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001525 goto abort_keep_alive;
1526
Olivier Houcharda798bf52019-03-08 18:52:00 +01001527 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001528 if (objt_server(s->target))
1529 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001530 rep->analysers &= AN_RES_FLT_END;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001531
1532 if (!(s->flags & SF_ERR_MASK))
1533 s->flags |= SF_ERR_CLICL;
1534 if (!(s->flags & SF_FINST_MASK))
1535 s->flags |= SF_FINST_H;
1536
1537 /* process_stream() will take care of the error */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001538 DBG_TRACE_DEVEL("leaving on error",
1539 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001540 return 0;
1541 }
1542
1543 channel_dont_close(rep);
1544 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001545 DBG_TRACE_DEVEL("waiting for more data",
1546 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001547 return 0;
1548 }
1549
1550 /* More interesting part now : we know that we have a complete
1551 * response which at least looks like HTTP. We have an indicator
1552 * of each header's length, so we can parse them quickly.
1553 */
Christopher Faulet29f17582019-05-23 11:03:26 +02001554 BUG_ON(htx_get_first_type(htx) != HTX_BLK_RES_SL);
Christopher Faulet297fbb42019-05-13 14:41:27 +02001555 sl = http_get_stline(htx);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001556
Christopher Faulet5b82cc52020-10-12 15:18:50 +02001557 if ((si_b->flags & SI_FL_L7_RETRY) &&
1558 l7_status_match(s->be, sl->info.res.status) &&
1559 do_l7_retry(s, si_b) == 0) {
1560 DBG_TRACE_DEVEL("leaving on L7 retry", STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
1561 return 0;
1562 }
1563 b_free(&s->si[1].l7_buffer);
1564
1565 msg->msg_state = HTTP_MSG_BODY;
1566
Christopher Faulet9768c262018-10-22 09:34:31 +02001567 /* 0: we might have to print this header in debug mode */
1568 if (unlikely((global.mode & MODE_DEBUG) &&
1569 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
1570 int32_t pos;
1571
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001572 http_debug_stline("srvrep", s, sl);
Christopher Faulet9768c262018-10-22 09:34:31 +02001573
Christopher Fauleta3f15502019-05-13 15:27:23 +02001574 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001575 struct htx_blk *blk = htx_get_blk(htx, pos);
1576 enum htx_blk_type type = htx_get_blk_type(blk);
1577
1578 if (type == HTX_BLK_EOH)
1579 break;
1580 if (type != HTX_BLK_HDR)
1581 continue;
1582
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001583 http_debug_hdr("srvhdr", s,
1584 htx_get_blk_name(htx, blk),
1585 htx_get_blk_value(htx, blk));
Christopher Faulet9768c262018-10-22 09:34:31 +02001586 }
1587 }
1588
Christopher Faulet03599112018-11-27 11:21:21 +01001589 /* 1: get the status code and the version. Also set HTTP flags */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001590 txn->status = sl->info.res.status;
Christopher Faulet03599112018-11-27 11:21:21 +01001591 if (sl->flags & HTX_SL_F_VER_11)
Christopher Faulet9768c262018-10-22 09:34:31 +02001592 msg->flags |= HTTP_MSGF_VER_11;
Christopher Faulet03599112018-11-27 11:21:21 +01001593 if (sl->flags & HTX_SL_F_XFER_LEN) {
1594 msg->flags |= HTTP_MSGF_XFER_LEN;
Christopher Faulet2a408542020-11-20 14:22:37 +01001595 if (sl->flags & HTX_SL_F_CLEN)
1596 msg->flags |= HTTP_MSGF_CNT_LEN;
1597 else if (sl->flags & HTX_SL_F_CHNK)
1598 msg->flags |= HTTP_MSGF_TE_CHNK;
Christopher Faulet03599112018-11-27 11:21:21 +01001599 }
Christopher Faulet2a408542020-11-20 14:22:37 +01001600 if (sl->flags & HTX_SL_F_BODYLESS)
1601 msg->flags |= HTTP_MSGF_BODYLESS;
Christopher Faulet576c3582021-01-08 15:53:01 +01001602 if (sl->flags & HTX_SL_F_CONN_UPG)
1603 msg->flags |= HTTP_MSGF_CONN_UPG;
Christopher Faulet9768c262018-10-22 09:34:31 +02001604
1605 n = txn->status / 100;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001606 if (n < 1 || n > 5)
1607 n = 0;
Christopher Faulet9768c262018-10-22 09:34:31 +02001608
Christopher Faulete0768eb2018-10-03 16:38:02 +02001609 /* when the client triggers a 4xx from the server, it's most often due
1610 * to a missing object or permission. These events should be tracked
1611 * because if they happen often, it may indicate a brute force or a
1612 * vulnerability scan.
1613 */
1614 if (n == 4)
1615 stream_inc_http_err_ctr(s);
1616
Marcin Deranek3c27dda2020-05-15 18:32:51 +02001617 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001618 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.p.http.rsp[n], 1);
Marcin Deranek3c27dda2020-05-15 18:32:51 +02001619 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.p.http.cum_req, 1);
1620 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001621
Christopher Faulete0768eb2018-10-03 16:38:02 +02001622 /* Adjust server's health based on status code. Note: status codes 501
1623 * and 505 are triggered on demand by client request, so we must not
1624 * count them as server failures.
1625 */
1626 if (objt_server(s->target)) {
1627 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001628 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_OK);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001629 else
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001630 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_STS);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001631 }
1632
1633 /*
1634 * We may be facing a 100-continue response, or any other informational
1635 * 1xx response which is non-final, in which case this is not the right
1636 * response, and we're waiting for the next one. Let's allow this response
1637 * to go to the client and wait for the next one. There's an exception for
1638 * 101 which is used later in the code to switch protocols.
1639 */
1640 if (txn->status < 200 &&
1641 (txn->status == 100 || txn->status >= 102)) {
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001642 FLT_STRM_CB(s, flt_http_reset(s, msg));
Christopher Faulet421e7692019-06-13 11:16:45 +02001643 htx->first = channel_htx_fwd_headers(rep, htx);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001644 msg->msg_state = HTTP_MSG_RPBEFORE;
Christopher Faulet3499f622019-09-03 15:23:54 +02001645 msg->flags = 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001646 txn->status = 0;
1647 s->logs.t_data = -1; /* was not a response yet */
Christopher Faulet7d518452020-08-31 11:07:07 +02001648 rep->flags |= CF_SEND_DONTWAIT; /* Send ASAP informational messages */
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001649 goto next_one;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001650 }
1651
Christopher Faulet6e6c7b12021-01-08 16:02:05 +01001652 /* A 101-switching-protocols must contains a Connection header with the
1653 * "upgrade" option and the request too. It means both are agree to
1654 * upgrade. It is not so strict because there is no test on the Upgrade
1655 * header content. But it is probably stronger enough for now.
1656 */
1657 if (txn->status == 101 &&
1658 (!(txn->req.flags & HTTP_MSGF_CONN_UPG) || !(txn->rsp.flags & HTTP_MSGF_CONN_UPG)))
1659 goto return_bad_res;
1660
Christopher Faulete0768eb2018-10-03 16:38:02 +02001661 /*
1662 * 2: check for cacheability.
1663 */
1664
1665 switch (txn->status) {
1666 case 200:
1667 case 203:
1668 case 204:
1669 case 206:
1670 case 300:
1671 case 301:
1672 case 404:
1673 case 405:
1674 case 410:
1675 case 414:
1676 case 501:
1677 break;
1678 default:
1679 /* RFC7231#6.1:
1680 * Responses with status codes that are defined as
1681 * cacheable by default (e.g., 200, 203, 204, 206,
1682 * 300, 301, 404, 405, 410, 414, and 501 in this
1683 * specification) can be reused by a cache with
1684 * heuristic expiration unless otherwise indicated
1685 * by the method definition or explicit cache
1686 * controls [RFC7234]; all other status codes are
1687 * not cacheable by default.
1688 */
1689 txn->flags &= ~(TX_CACHEABLE | TX_CACHE_COOK);
1690 break;
1691 }
1692
1693 /*
1694 * 3: we may need to capture headers
1695 */
1696 s->logs.logwait &= ~LW_RESP;
1697 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->res_cap))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001698 http_capture_headers(htx, s->res_cap, sess->fe->rsp_cap);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001699
Christopher Faulet9768c262018-10-22 09:34:31 +02001700 /* Skip parsing if no content length is possible. */
Christopher Fauletc75668e2020-12-07 18:10:32 +01001701 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status >= 200 && txn->status < 300) ||
Christopher Faulete0768eb2018-10-03 16:38:02 +02001702 txn->status == 101)) {
1703 /* Either we've established an explicit tunnel, or we're
1704 * switching the protocol. In both cases, we're very unlikely
1705 * to understand the next protocols. We have to switch to tunnel
1706 * mode, so that we transfer the request and responses then let
1707 * this protocol pass unmodified. When we later implement specific
1708 * parsers for such protocols, we'll want to check the Upgrade
1709 * header which contains information about that protocol for
1710 * responses with status 101 (eg: see RFC2817 about TLS).
1711 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02001712 txn->flags |= TX_CON_WANT_TUN;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001713 }
1714
Christopher Faulet61608322018-11-23 16:23:45 +01001715 /* check for NTML authentication headers in 401 (WWW-Authenticate) and
1716 * 407 (Proxy-Authenticate) responses and set the connection to private
1717 */
1718 srv_conn = cs_conn(objt_cs(s->si[1].end));
1719 if (srv_conn) {
1720 struct ist hdr;
1721 struct http_hdr_ctx ctx;
1722
1723 if (txn->status == 401)
1724 hdr = ist("WWW-Authenticate");
1725 else if (txn->status == 407)
1726 hdr = ist("Proxy-Authenticate");
1727 else
1728 goto end;
1729
1730 ctx.blk = NULL;
1731 while (http_find_header(htx, hdr, &ctx, 0)) {
Willy Tarreauf1dcced2020-05-07 19:27:02 +02001732 /* If www-authenticate contains "Negotiate", "Nego2", or "NTLM",
1733 * possibly followed by blanks and a base64 string, the connection
1734 * is private. Since it's a mess to deal with, we only check for
1735 * values starting with "NTLM" or "Nego". Note that often multiple
1736 * headers are sent by the server there.
1737 */
1738 if ((ctx.value.len >= 4 && strncasecmp(ctx.value.ptr, "Nego", 4) == 0) ||
Willy Tarreau49a1d282020-05-07 19:10:15 +02001739 (ctx.value.len >= 4 && strncasecmp(ctx.value.ptr, "NTLM", 4) == 0)) {
Olivier Houchard250031e2019-05-29 15:01:50 +02001740 sess->flags |= SESS_FL_PREFER_LAST;
Christopher Faulet08016ab2020-07-01 16:10:06 +02001741 conn_set_owner(srv_conn, sess, NULL);
Christopher Faulet21ddc742020-07-01 15:26:14 +02001742 conn_set_private(srv_conn);
Ilya Shipitsin6b79f382020-07-23 00:32:55 +05001743 /* If it fail now, the same will be done in mux->detach() callback */
Christopher Faulet08016ab2020-07-01 16:10:06 +02001744 session_add_conn(srv_conn->owner, srv_conn, srv_conn->target);
Willy Tarreauf1dcced2020-05-07 19:27:02 +02001745 break;
Olivier Houchard250031e2019-05-29 15:01:50 +02001746 }
Christopher Faulet61608322018-11-23 16:23:45 +01001747 }
1748 }
1749
1750 end:
Christopher Faulete0768eb2018-10-03 16:38:02 +02001751 /* we want to have the response time before we start processing it */
1752 s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now);
1753
1754 /* end of job, return OK */
1755 rep->analysers &= ~an_bit;
1756 rep->analyse_exp = TICK_ETERNITY;
1757 channel_auto_close(rep);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001758 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001759 return 1;
1760
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001761 return_int_err:
Christopher Fauletcff0f732019-12-16 16:13:44 +01001762 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01001763 _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001764 if (sess->listener->counters)
1765 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01001766 if (objt_server(s->target))
1767 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.internal_errors, 1);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001768 txn->status = 500;
1769 if (!(s->flags & SF_ERR_MASK))
1770 s->flags |= SF_ERR_INTERNAL;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001771 goto return_prx_cond;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001772
1773 return_bad_res:
Olivier Houcharda798bf52019-03-08 18:52:00 +01001774 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulet47365272018-10-31 17:40:50 +01001775 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001776 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001777 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Christopher Faulet47365272018-10-31 17:40:50 +01001778 }
Olivier Houcharde3249a92019-05-03 23:01:47 +02001779 if ((s->be->retry_type & PR_RE_JUNK_REQUEST) &&
Olivier Houchardad26d8d2019-05-10 17:48:28 +02001780 (si_b->flags & SI_FL_L7_RETRY) &&
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001781 do_l7_retry(s, si_b) == 0) {
1782 DBG_TRACE_DEVEL("leaving on L7 retry",
1783 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharde3249a92019-05-03 23:01:47 +02001784 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001785 }
Christopher Faulet47365272018-10-31 17:40:50 +01001786 txn->status = 502;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001787 /* fall through */
1788
Christopher Fauletb8a53712019-12-16 11:29:38 +01001789 return_prx_cond:
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001790 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet47365272018-10-31 17:40:50 +01001791
1792 if (!(s->flags & SF_ERR_MASK))
1793 s->flags |= SF_ERR_PRXCOND;
1794 if (!(s->flags & SF_FINST_MASK))
1795 s->flags |= SF_FINST_H;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001796
1797 s->si[1].flags |= SI_FL_NOLINGER;
1798 rep->analysers &= AN_RES_FLT_END;
Christopher Faulete58c0002020-03-02 16:21:01 +01001799 s->req.analysers &= AN_REQ_FLT_END;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001800 rep->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001801 DBG_TRACE_DEVEL("leaving on error",
1802 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulet47365272018-10-31 17:40:50 +01001803 return 0;
1804
Christopher Faulete0768eb2018-10-03 16:38:02 +02001805 abort_keep_alive:
1806 /* A keep-alive request to the server failed on a network error.
1807 * The client is required to retry. We need to close without returning
1808 * any other information so that the client retries.
1809 */
1810 txn->status = 0;
1811 rep->analysers &= AN_RES_FLT_END;
1812 s->req.analysers &= AN_REQ_FLT_END;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001813 s->logs.logwait = 0;
1814 s->logs.level = 0;
1815 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001816 http_reply_and_close(s, txn->status, NULL);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001817 DBG_TRACE_DEVEL("leaving by closing K/A connection",
1818 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001819 return 0;
1820}
1821
1822/* This function performs all the processing enabled for the current response.
1823 * It normally returns 1 unless it wants to break. It relies on buffers flags,
1824 * and updates s->res.analysers. It might make sense to explode it into several
1825 * other functions. It works like process_request (see indications above).
1826 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001827int http_process_res_common(struct stream *s, struct channel *rep, int an_bit, struct proxy *px)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001828{
1829 struct session *sess = s->sess;
1830 struct http_txn *txn = s->txn;
1831 struct http_msg *msg = &txn->rsp;
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001832 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001833 struct proxy *cur_proxy;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001834 enum rule_result ret = HTTP_RULE_RES_CONT;
1835
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001836 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
1837 return 0;
Christopher Faulet9768c262018-10-22 09:34:31 +02001838
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001839 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001840
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001841 htx = htxbuf(&rep->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001842
1843 /* The stats applet needs to adjust the Connection header but we don't
1844 * apply any filter there.
1845 */
1846 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
1847 rep->analysers &= ~an_bit;
1848 rep->analyse_exp = TICK_ETERNITY;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001849 goto end;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001850 }
1851
1852 /*
1853 * We will have to evaluate the filters.
1854 * As opposed to version 1.2, now they will be evaluated in the
1855 * filters order and not in the header order. This means that
1856 * each filter has to be validated among all headers.
1857 *
1858 * Filters are tried with ->be first, then with ->fe if it is
1859 * different from ->be.
1860 *
1861 * Maybe we are in resume condiion. In this case I choose the
1862 * "struct proxy" which contains the rule list matching the resume
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05001863 * pointer. If none of these "struct proxy" match, I initialise
Christopher Faulete0768eb2018-10-03 16:38:02 +02001864 * the process with the first one.
1865 *
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05001866 * In fact, I check only correspondence between the current list
Christopher Faulete0768eb2018-10-03 16:38:02 +02001867 * pointer and the ->fe rule list. If it doesn't match, I initialize
1868 * the loop with the ->be.
1869 */
1870 if (s->current_rule_list == &sess->fe->http_res_rules)
1871 cur_proxy = sess->fe;
1872 else
1873 cur_proxy = s->be;
1874 while (1) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001875 /* evaluate http-response rules */
1876 if (ret == HTTP_RULE_RES_CONT) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001877 ret = http_res_get_intercept_rule(cur_proxy, &cur_proxy->http_res_rules, s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001878
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001879 switch (ret) {
1880 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
1881 goto return_prx_yield;
1882
1883 case HTTP_RULE_RES_CONT:
1884 case HTTP_RULE_RES_STOP: /* nothing to do */
1885 break;
1886
1887 case HTTP_RULE_RES_DENY: /* deny or tarpit */
1888 goto deny;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001889
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001890 case HTTP_RULE_RES_ABRT: /* abort request, response already sent */
1891 goto return_prx_cond;
1892
1893 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
Christopher Fauletb8a53712019-12-16 11:29:38 +01001894 goto done;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001895
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001896 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
1897 goto return_bad_res;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001898
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001899 case HTTP_RULE_RES_ERROR: /* failed with a bad request */
1900 goto return_int_err;
1901 }
1902
1903 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001904
Christopher Faulete0768eb2018-10-03 16:38:02 +02001905 /* check whether we're already working on the frontend */
1906 if (cur_proxy == sess->fe)
1907 break;
1908 cur_proxy = sess->fe;
1909 }
1910
Christopher Faulete0768eb2018-10-03 16:38:02 +02001911 /* OK that's all we can do for 1xx responses */
1912 if (unlikely(txn->status < 200 && txn->status != 101))
Christopher Fauletf2824e62018-10-01 12:12:37 +02001913 goto end;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001914
1915 /*
1916 * Now check for a server cookie.
1917 */
1918 if (s->be->cookie_name || sess->fe->capture_name || (s->be->options & PR_O_CHK_CACHE))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001919 http_manage_server_side_cookies(s, rep);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001920
1921 /*
1922 * Check for cache-control or pragma headers if required.
1923 */
1924 if ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001925 http_check_response_for_cacheability(s, rep);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001926
1927 /*
1928 * Add server cookie in the response if needed
1929 */
1930 if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) &&
1931 !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) &&
1932 (!(s->flags & SF_DIRECT) ||
1933 ((s->be->cookie_maxidle || txn->cookie_last_date) &&
1934 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
1935 (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
1936 (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
1937 (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
1938 !(s->flags & SF_IGNORE_PRST)) {
1939 /* the server is known, it's not the one the client requested, or the
1940 * cookie's last seen date needs to be refreshed. We have to
1941 * insert a set-cookie here, except if we want to insert only on POST
1942 * requests and this one isn't. Note that servers which don't have cookies
1943 * (eg: some backup servers) will return a full cookie removal request.
1944 */
1945 if (!objt_server(s->target)->cookie) {
1946 chunk_printf(&trash,
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001947 "%s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
Christopher Faulete0768eb2018-10-03 16:38:02 +02001948 s->be->cookie_name);
1949 }
1950 else {
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001951 chunk_printf(&trash, "%s=%s", s->be->cookie_name, objt_server(s->target)->cookie);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001952
1953 if (s->be->cookie_maxidle || s->be->cookie_maxlife) {
1954 /* emit last_date, which is mandatory */
1955 trash.area[trash.data++] = COOKIE_DELIM_DATE;
1956 s30tob64((date.tv_sec+3) >> 2,
1957 trash.area + trash.data);
1958 trash.data += 5;
1959
1960 if (s->be->cookie_maxlife) {
1961 /* emit first_date, which is either the original one or
1962 * the current date.
1963 */
1964 trash.area[trash.data++] = COOKIE_DELIM_DATE;
1965 s30tob64(txn->cookie_first_date ?
1966 txn->cookie_first_date >> 2 :
1967 (date.tv_sec+3) >> 2,
1968 trash.area + trash.data);
1969 trash.data += 5;
1970 }
1971 }
1972 chunk_appendf(&trash, "; path=/");
1973 }
1974
1975 if (s->be->cookie_domain)
1976 chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain);
1977
1978 if (s->be->ck_opts & PR_CK_HTTPONLY)
1979 chunk_appendf(&trash, "; HttpOnly");
1980
1981 if (s->be->ck_opts & PR_CK_SECURE)
1982 chunk_appendf(&trash, "; Secure");
1983
Christopher Faulet2f533902020-01-21 11:06:48 +01001984 if (s->be->cookie_attrs)
1985 chunk_appendf(&trash, "; %s", s->be->cookie_attrs);
1986
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001987 if (unlikely(!http_add_header(htx, ist("Set-Cookie"), ist2(trash.area, trash.data))))
Christopher Fauletb8a53712019-12-16 11:29:38 +01001988 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001989
1990 txn->flags &= ~TX_SCK_MASK;
1991 if (__objt_server(s->target)->cookie && (s->flags & SF_DIRECT))
1992 /* the server did not change, only the date was updated */
1993 txn->flags |= TX_SCK_UPDATED;
1994 else
1995 txn->flags |= TX_SCK_INSERTED;
1996
1997 /* Here, we will tell an eventual cache on the client side that we don't
1998 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
1999 * Some caches understand the correct form: 'no-cache="set-cookie"', but
2000 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
2001 */
2002 if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
2003
2004 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
2005
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002006 if (unlikely(!http_add_header(htx, ist("Cache-control"), ist("private"))))
Christopher Fauletb8a53712019-12-16 11:29:38 +01002007 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002008 }
2009 }
2010
2011 /*
2012 * Check if result will be cacheable with a cookie.
2013 * We'll block the response if security checks have caught
2014 * nasty things such as a cacheable cookie.
2015 */
2016 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
2017 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
2018 (s->be->options & PR_O_CHK_CACHE)) {
2019 /* we're in presence of a cacheable response containing
2020 * a set-cookie header. We'll block it as requested by
2021 * the 'checkcache' option, and send an alert.
2022 */
Christopher Faulete0768eb2018-10-03 16:38:02 +02002023 ha_alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
2024 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
2025 send_log(s->be, LOG_ALERT,
2026 "Blocking cacheable cookie in response from instance %s, server %s.\n",
2027 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
Christopher Fauletb8a53712019-12-16 11:29:38 +01002028 goto deny;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002029 }
2030
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002031 end:
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002032 /*
2033 * Evaluate after-response rules before forwarding the response. rules
2034 * from the backend are evaluated first, then one from the frontend if
2035 * it differs.
2036 */
2037 if (!http_eval_after_res_rules(s))
2038 goto return_int_err;
2039
Christopher Faulete0768eb2018-10-03 16:38:02 +02002040 /* Always enter in the body analyzer */
2041 rep->analysers &= ~AN_RES_FLT_XFER_DATA;
2042 rep->analysers |= AN_RES_HTTP_XFER_BODY;
2043
2044 /* if the user wants to log as soon as possible, without counting
2045 * bytes from the server, then this is the right moment. We have
2046 * to temporarily assign bytes_out to log what we currently have.
2047 */
2048 if (!LIST_ISEMPTY(&sess->fe->logformat) && !(s->logs.logwait & LW_BYTES)) {
2049 s->logs.t_close = s->logs.t_data; /* to get a valid end date */
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002050 s->logs.bytes_out = htx->data;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002051 s->do_log(s);
2052 s->logs.bytes_out = 0;
2053 }
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002054
Christopher Fauletb8a53712019-12-16 11:29:38 +01002055 done:
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002056 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002057 rep->analysers &= ~an_bit;
2058 rep->analyse_exp = TICK_ETERNITY;
2059 return 1;
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002060
Christopher Fauletb8a53712019-12-16 11:29:38 +01002061 deny:
Christopher Fauletb8a53712019-12-16 11:29:38 +01002062 _HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_resp, 1);
Christopher Fauleta08546b2019-12-16 16:07:34 +01002063 _HA_ATOMIC_ADD(&s->be->be_counters.denied_resp, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002064 if (sess->listener->counters)
2065 _HA_ATOMIC_ADD(&sess->listener->counters->denied_resp, 1);
Christopher Fauleta08546b2019-12-16 16:07:34 +01002066 if (objt_server(s->target))
2067 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.denied_resp, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002068 goto return_prx_err;
2069
2070 return_int_err:
2071 txn->status = 500;
2072 if (!(s->flags & SF_ERR_MASK))
2073 s->flags |= SF_ERR_INTERNAL;
Christopher Fauletcff0f732019-12-16 16:13:44 +01002074 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002075 _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1);
2076 if (objt_server(s->target))
2077 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01002078 if (objt_server(s->target))
2079 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002080 goto return_prx_err;
2081
2082 return_bad_res:
2083 txn->status = 502;
Christopher Fauleta20a6532020-02-05 10:16:41 +01002084 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
2085 if (objt_server(s->target)) {
2086 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
2087 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_RSP);
2088 }
Christopher Fauletb8a53712019-12-16 11:29:38 +01002089 /* fall through */
2090
2091 return_prx_err:
2092 http_reply_and_close(s, txn->status, http_error_message(s));
2093 /* fall through */
2094
2095 return_prx_cond:
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002096 s->logs.t_data = -1; /* was not a valid response */
2097 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002098
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002099 if (!(s->flags & SF_ERR_MASK))
2100 s->flags |= SF_ERR_PRXCOND;
2101 if (!(s->flags & SF_FINST_MASK))
2102 s->flags |= SF_FINST_H;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002103
Christopher Faulete58c0002020-03-02 16:21:01 +01002104 rep->analysers &= AN_RES_FLT_END;
2105 s->req.analysers &= AN_REQ_FLT_END;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002106 rep->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002107 DBG_TRACE_DEVEL("leaving on error",
2108 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002109 return 0;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002110
2111 return_prx_yield:
2112 channel_dont_close(rep);
2113 DBG_TRACE_DEVEL("waiting for more data",
2114 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
2115 return 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002116}
2117
2118/* This function is an analyser which forwards response body (including chunk
2119 * sizes if any). It is called as soon as we must forward, even if we forward
2120 * zero byte. The only situation where it must not be called is when we're in
2121 * tunnel mode and we want to forward till the close. It's used both to forward
2122 * remaining data and to resync after end of body. It expects the msg_state to
2123 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
2124 * read more data, or 1 once we can go on with next request or end the stream.
2125 *
2126 * It is capable of compressing response data both in content-length mode and
2127 * in chunked mode. The state machines follows different flows depending on
2128 * whether content-length and chunked modes are used, since there are no
2129 * trailers in content-length :
2130 *
2131 * chk-mode cl-mode
2132 * ,----- BODY -----.
2133 * / \
2134 * V size > 0 V chk-mode
2135 * .--> SIZE -------------> DATA -------------> CRLF
2136 * | | size == 0 | last byte |
2137 * | v final crlf v inspected |
2138 * | TRAILERS -----------> DONE |
2139 * | |
2140 * `----------------------------------------------'
2141 *
2142 * Compression only happens in the DATA state, and must be flushed in final
2143 * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding
2144 * is performed at once on final states for all bytes parsed, or when leaving
2145 * on missing data.
2146 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002147int http_response_forward_body(struct stream *s, struct channel *res, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +02002148{
2149 struct session *sess = s->sess;
2150 struct http_txn *txn = s->txn;
2151 struct http_msg *msg = &s->txn->rsp;
Christopher Faulet9768c262018-10-22 09:34:31 +02002152 struct htx *htx;
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002153 int ret;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002154
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002155 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002156
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002157 htx = htxbuf(&res->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002158
Christopher Fauletb9a92f32019-09-09 10:15:21 +02002159 if (htx->flags & HTX_FL_PARSING_ERROR)
2160 goto return_bad_res;
2161 if (htx->flags & HTX_FL_PROCESSING_ERROR)
2162 goto return_int_err;
2163
Christopher Faulete0768eb2018-10-03 16:38:02 +02002164 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Christopher Fauletf2824e62018-10-01 12:12:37 +02002165 ((res->flags & CF_SHUTW) && (res->to_forward || co_data(res)))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02002166 /* Output closed while we were sending data. We must abort and
2167 * wake the other side up.
2168 */
Christopher Faulete0768eb2018-10-03 16:38:02 +02002169 msg->msg_state = HTTP_MSG_ERROR;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002170 http_end_response(s);
2171 http_end_request(s);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002172 DBG_TRACE_DEVEL("leaving on error",
2173 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002174 return 1;
2175 }
2176
Christopher Faulet9768c262018-10-22 09:34:31 +02002177 if (msg->msg_state == HTTP_MSG_BODY)
2178 msg->msg_state = HTTP_MSG_DATA;
2179
Christopher Faulete0768eb2018-10-03 16:38:02 +02002180 /* in most states, we should abort in case of early close */
2181 channel_auto_close(res);
2182
Christopher Faulete0768eb2018-10-03 16:38:02 +02002183 if (res->to_forward) {
Christopher Faulet66af0b22019-03-22 14:54:52 +01002184 if (res->to_forward == CHN_INFINITE_FORWARD) {
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002185 if (res->flags & CF_EOI)
2186 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet66af0b22019-03-22 14:54:52 +01002187 }
2188 else {
2189 /* We can't process the buffer's contents yet */
2190 res->flags |= CF_WAKE_WRITE;
2191 goto missing_data_or_waiting;
2192 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02002193 }
2194
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002195 if (msg->msg_state >= HTTP_MSG_ENDING)
2196 goto ending;
2197
Christopher Fauletc75668e2020-12-07 18:10:32 +01002198 if ((txn->meth == HTTP_METH_CONNECT && txn->status >= 200 && txn->status < 300) || txn->status == 101 ||
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002199 (!(msg->flags & HTTP_MSGF_XFER_LEN) && !HAS_RSP_DATA_FILTERS(s))) {
2200 msg->msg_state = HTTP_MSG_ENDING;
2201 goto ending;
2202 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002203
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002204 /* Forward input data. We get it by removing all outgoing data not
2205 * forwarded yet from HTX data size. If there are some data filters, we
2206 * let them decide the amount of data to forward.
Christopher Faulet9768c262018-10-22 09:34:31 +02002207 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002208 if (HAS_RSP_DATA_FILTERS(s)) {
2209 ret = flt_http_payload(s, msg, htx->data);
2210 if (ret < 0)
2211 goto return_bad_res;
Christopher Faulet421e7692019-06-13 11:16:45 +02002212 c_adv(res, ret);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002213 }
2214 else {
Christopher Faulet421e7692019-06-13 11:16:45 +02002215 c_adv(res, htx->data - co_data(res));
Christopher Faulet66af0b22019-03-22 14:54:52 +01002216 if (msg->flags & HTTP_MSGF_XFER_LEN)
2217 channel_htx_forward_forever(res, htx);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002218 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002219
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002220 if (htx->data != co_data(res))
2221 goto missing_data_or_waiting;
2222
2223 if (!(msg->flags & HTTP_MSGF_XFER_LEN) && res->flags & CF_SHUTR) {
2224 msg->msg_state = HTTP_MSG_ENDING;
2225 goto ending;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002226 }
2227
Christopher Faulet9768c262018-10-22 09:34:31 +02002228 /* Check if the end-of-message is reached and if so, switch the message
Christopher Fauletd20fdb02019-06-13 16:43:22 +02002229 * in HTTP_MSG_ENDING state. Then if all data was marked to be
2230 * forwarded, set the state to HTTP_MSG_DONE.
Christopher Faulet9768c262018-10-22 09:34:31 +02002231 */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002232 if (!(htx->flags & HTX_FL_EOM))
Christopher Faulet9768c262018-10-22 09:34:31 +02002233 goto missing_data_or_waiting;
2234
Christopher Fauletd20fdb02019-06-13 16:43:22 +02002235 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet9768c262018-10-22 09:34:31 +02002236
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002237 ending:
Christopher Faulet2151cdd2020-07-22 16:34:59 +02002238 res->flags &= ~CF_EXPECT_MORE; /* no more data are expected */
2239
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002240 /* other states, ENDING...TUNNEL */
2241 if (msg->msg_state >= HTTP_MSG_DONE)
2242 goto done;
Christopher Faulet9768c262018-10-22 09:34:31 +02002243
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002244 if (HAS_RSP_DATA_FILTERS(s)) {
2245 ret = flt_http_end(s, msg);
2246 if (ret <= 0) {
2247 if (!ret)
2248 goto missing_data_or_waiting;
2249 goto return_bad_res;
2250 }
2251 }
2252
Christopher Fauletc75668e2020-12-07 18:10:32 +01002253 if ((txn->meth == HTTP_METH_CONNECT && txn->status >= 200 && txn->status < 300) || txn->status == 101 ||
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002254 !(msg->flags & HTTP_MSGF_XFER_LEN)) {
2255 msg->msg_state = HTTP_MSG_TUNNEL;
2256 goto ending;
2257 }
2258 else {
2259 msg->msg_state = HTTP_MSG_DONE;
2260 res->to_forward = 0;
2261 }
2262
2263 done:
2264
2265 channel_dont_close(res);
2266
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002267 http_end_response(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002268 if (!(res->analysers & an_bit)) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002269 http_end_request(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002270 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
2271 if (res->flags & CF_SHUTW) {
2272 /* response errors are most likely due to the
2273 * client aborting the transfer. */
Christopher Faulet93e02d82019-03-08 14:18:50 +01002274 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002275 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02002276 goto return_bad_res;
2277 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002278 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002279 return 1;
2280 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002281 DBG_TRACE_DEVEL("waiting for the end of the HTTP txn",
2282 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002283 return 0;
2284
2285 missing_data_or_waiting:
2286 if (res->flags & CF_SHUTW)
Christopher Faulet93e02d82019-03-08 14:18:50 +01002287 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002288
2289 /* stop waiting for data if the input is closed before the end. If the
2290 * client side was already closed, it means that the client has aborted,
2291 * so we don't want to count this as a server abort. Otherwise it's a
2292 * server abort.
2293 */
Christopher Fauletd20fdb02019-06-13 16:43:22 +02002294 if (msg->msg_state < HTTP_MSG_ENDING && res->flags & CF_SHUTR) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02002295 if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))
Christopher Faulet93e02d82019-03-08 14:18:50 +01002296 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002297 /* If we have some pending data, we continue the processing */
Christopher Faulet93e02d82019-03-08 14:18:50 +01002298 if (htx_is_empty(htx))
2299 goto return_srv_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002300 }
2301
Christopher Faulete0768eb2018-10-03 16:38:02 +02002302 /* When TE: chunked is used, we need to get there again to parse
2303 * remaining chunks even if the server has closed, so we don't want to
Christopher Faulet9768c262018-10-22 09:34:31 +02002304 * set CF_DONTCLOSE. Similarly when there is a content-leng or if there
2305 * are filters registered on the stream, we don't want to forward a
2306 * close
Christopher Faulete0768eb2018-10-03 16:38:02 +02002307 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002308 if ((msg->flags & HTTP_MSGF_XFER_LEN) || HAS_RSP_DATA_FILTERS(s))
Christopher Faulete0768eb2018-10-03 16:38:02 +02002309 channel_dont_close(res);
2310
2311 /* We know that more data are expected, but we couldn't send more that
2312 * what we did. So we always set the CF_EXPECT_MORE flag so that the
2313 * system knows it must not set a PUSH on this first part. Interactive
2314 * modes are already handled by the stream sock layer. We must not do
2315 * this in content-length mode because it could present the MSG_MORE
2316 * flag with the last block of forwarded data, which would cause an
2317 * additional delay to be observed by the receiver.
2318 */
Christopher Faulet2151cdd2020-07-22 16:34:59 +02002319 if (HAS_RSP_DATA_FILTERS(s))
Christopher Faulete0768eb2018-10-03 16:38:02 +02002320 res->flags |= CF_EXPECT_MORE;
2321
2322 /* the stream handler will take care of timeouts and errors */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002323 DBG_TRACE_DEVEL("waiting for more data to forward",
2324 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002325 return 0;
2326
Christopher Faulet93e02d82019-03-08 14:18:50 +01002327 return_srv_abort:
2328 _HA_ATOMIC_ADD(&sess->fe->fe_counters.srv_aborts, 1);
2329 _HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01002330 if (sess->listener->counters)
2331 _HA_ATOMIC_ADD(&sess->listener->counters->srv_aborts, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002332 if (objt_server(s->target))
Christopher Fauletcff0f732019-12-16 16:13:44 +01002333 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.srv_aborts, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002334 if (!(s->flags & SF_ERR_MASK))
2335 s->flags |= SF_ERR_SRVCL;
2336 goto return_error;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002337
Christopher Faulet93e02d82019-03-08 14:18:50 +01002338 return_cli_abort:
2339 _HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
2340 _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01002341 if (sess->listener->counters)
2342 _HA_ATOMIC_ADD(&sess->listener->counters->cli_aborts, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002343 if (objt_server(s->target))
Christopher Fauletcff0f732019-12-16 16:13:44 +01002344 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.cli_aborts, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002345 if (!(s->flags & SF_ERR_MASK))
2346 s->flags |= SF_ERR_CLICL;
2347 goto return_error;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002348
Christopher Fauletb9a92f32019-09-09 10:15:21 +02002349 return_int_err:
Christopher Fauletcff0f732019-12-16 16:13:44 +01002350 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002351 _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01002352 if (sess->listener->counters)
2353 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002354 if (objt_server(s->target))
2355 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.internal_errors, 1);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02002356 if (!(s->flags & SF_ERR_MASK))
2357 s->flags |= SF_ERR_INTERNAL;
2358 goto return_error;
2359
Christopher Faulet93e02d82019-03-08 14:18:50 +01002360 return_bad_res:
2361 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
2362 if (objt_server(s->target)) {
Christopher Fauletcff0f732019-12-16 16:13:44 +01002363 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002364 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_RSP);
2365 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02002366 if (!(s->flags & SF_ERR_MASK))
Christopher Faulet93e02d82019-03-08 14:18:50 +01002367 s->flags |= SF_ERR_SRVCL;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002368 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +02002369
Christopher Faulet93e02d82019-03-08 14:18:50 +01002370 return_error:
Christopher Faulete0768eb2018-10-03 16:38:02 +02002371 /* don't send any error message as we're in the body */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002372 http_reply_and_close(s, txn->status, NULL);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002373 res->analysers &= AN_RES_FLT_END;
2374 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 +02002375 if (!(s->flags & SF_FINST_MASK))
2376 s->flags |= SF_FINST_D;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002377 DBG_TRACE_DEVEL("leaving on error",
2378 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002379 return 0;
2380}
2381
Christopher Fauletf2824e62018-10-01 12:12:37 +02002382/* Perform an HTTP redirect based on the information in <rule>. The function
Christopher Faulet99daf282018-11-28 22:58:13 +01002383 * returns zero on success, or zero in case of a, irrecoverable error such
Christopher Fauletf2824e62018-10-01 12:12:37 +02002384 * as too large a request to build a valid response.
2385 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002386int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn)
Christopher Fauletf2824e62018-10-01 12:12:37 +02002387{
Christopher Faulet99daf282018-11-28 22:58:13 +01002388 struct channel *req = &s->req;
2389 struct channel *res = &s->res;
2390 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01002391 struct htx_sl *sl;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002392 struct buffer *chunk;
Christopher Faulet99daf282018-11-28 22:58:13 +01002393 struct ist status, reason, location;
2394 unsigned int flags;
Christopher Faulet08e66462019-05-23 16:44:59 +02002395 int close = 0; /* Try to keep the connection alive byt default */
Christopher Fauletf2824e62018-10-01 12:12:37 +02002396
2397 chunk = alloc_trash_chunk();
Christopher Fauletb8a53712019-12-16 11:29:38 +01002398 if (!chunk) {
2399 if (!(s->flags & SF_ERR_MASK))
2400 s->flags |= SF_ERR_RESOURCE;
Christopher Faulet99daf282018-11-28 22:58:13 +01002401 goto fail;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002402 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002403
Christopher Faulet99daf282018-11-28 22:58:13 +01002404 /*
2405 * Create the location
2406 */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002407 htx = htxbuf(&req->buf);
Christopher Fauletf2824e62018-10-01 12:12:37 +02002408 switch(rule->type) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002409 case REDIRECT_TYPE_SCHEME: {
2410 struct http_hdr_ctx ctx;
2411 struct ist path, host;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002412
Christopher Faulet99daf282018-11-28 22:58:13 +01002413 host = ist("");
2414 ctx.blk = NULL;
2415 if (http_find_header(htx, ist("Host"), &ctx, 0))
2416 host = ctx.value;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002417
Christopher Faulet297fbb42019-05-13 14:41:27 +02002418 sl = http_get_stline(htx);
Christopher Faulet99daf282018-11-28 22:58:13 +01002419 path = http_get_path(htx_sl_req_uri(sl));
2420 /* build message using path */
Tim Duesterhused526372020-03-05 17:56:33 +01002421 if (isttest(path)) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002422 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2423 int qs = 0;
2424 while (qs < path.len) {
2425 if (*(path.ptr + qs) == '?') {
2426 path.len = qs;
2427 break;
2428 }
2429 qs++;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002430 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002431 }
2432 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002433 else
2434 path = ist("/");
Christopher Fauletf2824e62018-10-01 12:12:37 +02002435
Christopher Faulet99daf282018-11-28 22:58:13 +01002436 if (rule->rdr_str) { /* this is an old "redirect" rule */
2437 /* add scheme */
2438 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2439 goto fail;
2440 }
2441 else {
2442 /* add scheme with executing log format */
2443 chunk->data += build_logline(s, chunk->area + chunk->data,
2444 chunk->size - chunk->data,
2445 &rule->rdr_fmt);
2446 }
2447 /* add "://" + host + path */
2448 if (!chunk_memcat(chunk, "://", 3) ||
2449 !chunk_memcat(chunk, host.ptr, host.len) ||
2450 !chunk_memcat(chunk, path.ptr, path.len))
2451 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002452
Christopher Faulet99daf282018-11-28 22:58:13 +01002453 /* append a slash at the end of the location if needed and missing */
2454 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
2455 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
2456 if (chunk->data + 1 >= chunk->size)
2457 goto fail;
2458 chunk->area[chunk->data++] = '/';
2459 }
2460 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002461 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002462
Christopher Faulet99daf282018-11-28 22:58:13 +01002463 case REDIRECT_TYPE_PREFIX: {
2464 struct ist path;
2465
Christopher Faulet297fbb42019-05-13 14:41:27 +02002466 sl = http_get_stline(htx);
Christopher Faulet99daf282018-11-28 22:58:13 +01002467 path = http_get_path(htx_sl_req_uri(sl));
2468 /* build message using path */
Tim Duesterhused526372020-03-05 17:56:33 +01002469 if (isttest(path)) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002470 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2471 int qs = 0;
2472 while (qs < path.len) {
2473 if (*(path.ptr + qs) == '?') {
2474 path.len = qs;
2475 break;
2476 }
2477 qs++;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002478 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002479 }
2480 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002481 else
2482 path = ist("/");
Christopher Fauletf2824e62018-10-01 12:12:37 +02002483
Christopher Faulet99daf282018-11-28 22:58:13 +01002484 if (rule->rdr_str) { /* this is an old "redirect" rule */
2485 /* add prefix. Note that if prefix == "/", we don't want to
2486 * add anything, otherwise it makes it hard for the user to
2487 * configure a self-redirection.
2488 */
2489 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
2490 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2491 goto fail;
2492 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002493 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002494 else {
2495 /* add prefix with executing log format */
2496 chunk->data += build_logline(s, chunk->area + chunk->data,
2497 chunk->size - chunk->data,
2498 &rule->rdr_fmt);
2499 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002500
Christopher Faulet99daf282018-11-28 22:58:13 +01002501 /* add path */
2502 if (!chunk_memcat(chunk, path.ptr, path.len))
2503 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002504
Christopher Faulet99daf282018-11-28 22:58:13 +01002505 /* append a slash at the end of the location if needed and missing */
2506 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
2507 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
2508 if (chunk->data + 1 >= chunk->size)
2509 goto fail;
2510 chunk->area[chunk->data++] = '/';
2511 }
2512 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002513 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002514 case REDIRECT_TYPE_LOCATION:
2515 default:
2516 if (rule->rdr_str) { /* this is an old "redirect" rule */
2517 /* add location */
2518 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2519 goto fail;
2520 }
2521 else {
2522 /* add location with executing log format */
2523 chunk->data += build_logline(s, chunk->area + chunk->data,
2524 chunk->size - chunk->data,
2525 &rule->rdr_fmt);
2526 }
2527 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002528 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002529 location = ist2(chunk->area, chunk->data);
2530
2531 /*
2532 * Create the 30x response
2533 */
2534 switch (rule->code) {
2535 case 308:
2536 status = ist("308");
2537 reason = ist("Permanent Redirect");
2538 break;
2539 case 307:
2540 status = ist("307");
2541 reason = ist("Temporary Redirect");
2542 break;
2543 case 303:
2544 status = ist("303");
2545 reason = ist("See Other");
2546 break;
2547 case 301:
2548 status = ist("301");
2549 reason = ist("Moved Permanently");
2550 break;
2551 case 302:
2552 default:
2553 status = ist("302");
2554 reason = ist("Found");
2555 break;
2556 }
2557
Christopher Faulet08e66462019-05-23 16:44:59 +02002558 if (!(txn->req.flags & HTTP_MSGF_BODYLESS) && txn->req.msg_state != HTTP_MSG_DONE)
2559 close = 1;
2560
Christopher Faulet99daf282018-11-28 22:58:13 +01002561 htx = htx_from_buf(&res->buf);
Kevin Zhu96b36392020-01-07 09:42:55 +01002562 /* Trim any possible response */
2563 channel_htx_truncate(&s->res, htx);
Christopher Faulet99daf282018-11-28 22:58:13 +01002564 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
2565 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), status, reason);
2566 if (!sl)
2567 goto fail;
2568 sl->info.res.status = rule->code;
2569 s->txn->status = rule->code;
2570
Christopher Faulet08e66462019-05-23 16:44:59 +02002571 if (close && !htx_add_header(htx, ist("Connection"), ist("close")))
2572 goto fail;
2573
2574 if (!htx_add_header(htx, ist("Content-length"), ist("0")) ||
Christopher Faulet99daf282018-11-28 22:58:13 +01002575 !htx_add_header(htx, ist("Location"), location))
2576 goto fail;
2577
2578 if (rule->code == 302 || rule->code == 303 || rule->code == 307) {
2579 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")))
2580 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002581 }
2582
2583 if (rule->cookie_len) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002584 if (!htx_add_header(htx, ist("Set-Cookie"), ist2(rule->cookie_str, rule->cookie_len)))
2585 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002586 }
2587
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002588 if (!htx_add_endof(htx, HTX_BLK_EOH))
Christopher Faulet99daf282018-11-28 22:58:13 +01002589 goto fail;
2590
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002591 htx->flags |= HTX_FL_EOM;
Kevin Zhu96b36392020-01-07 09:42:55 +01002592 htx_to_buf(htx, &res->buf);
Christopher Fauleta72a7e42020-01-28 09:28:11 +01002593 if (!http_forward_proxy_resp(s, 1))
2594 goto fail;
Christopher Faulet99daf282018-11-28 22:58:13 +01002595
Christopher Faulet60b33a52020-01-28 09:18:10 +01002596 if (rule->flags & REDIRECT_FLAG_FROM_REQ) {
2597 /* let's log the request time */
2598 s->logs.tv_request = now;
2599 req->analysers &= AN_REQ_FLT_END;
Christopher Faulet99daf282018-11-28 22:58:13 +01002600
Christopher Faulet60b33a52020-01-28 09:18:10 +01002601 if (s->sess->fe == s->be) /* report it if the request was intercepted by the frontend */
2602 _HA_ATOMIC_ADD(&s->sess->fe->fe_counters.intercepted_req, 1);
2603 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002604
2605 if (!(s->flags & SF_ERR_MASK))
2606 s->flags |= SF_ERR_LOCAL;
2607 if (!(s->flags & SF_FINST_MASK))
Christopher Faulet60b33a52020-01-28 09:18:10 +01002608 s->flags |= ((rule->flags & REDIRECT_FLAG_FROM_REQ) ? SF_FINST_R : SF_FINST_H);
Christopher Fauletf2824e62018-10-01 12:12:37 +02002609
Christopher Faulet99daf282018-11-28 22:58:13 +01002610 free_trash_chunk(chunk);
2611 return 1;
2612
2613 fail:
2614 /* If an error occurred, remove the incomplete HTTP response from the
2615 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01002616 channel_htx_truncate(res, htxbuf(&res->buf));
Christopher Fauletf2824e62018-10-01 12:12:37 +02002617 free_trash_chunk(chunk);
Christopher Faulet99daf282018-11-28 22:58:13 +01002618 return 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002619}
2620
Christopher Faulet92d34fe2019-12-17 09:20:34 +01002621/* Replace all headers matching the name <name>. The header value is replaced if
2622 * it matches the regex <re>. <str> is used for the replacement. If <full> is
2623 * set to 1, the full-line is matched and replaced. Otherwise, comma-separated
2624 * values are evaluated one by one. It returns 0 on success and -1 on error.
2625 */
2626int http_replace_hdrs(struct stream* s, struct htx *htx, struct ist name,
2627 const char *str, struct my_regex *re, int full)
Christopher Faulet72333522018-10-24 11:25:02 +02002628{
2629 struct http_hdr_ctx ctx;
2630 struct buffer *output = get_trash_chunk();
2631
Christopher Faulet72333522018-10-24 11:25:02 +02002632 ctx.blk = NULL;
Christopher Faulet92d34fe2019-12-17 09:20:34 +01002633 while (http_find_header(htx, name, &ctx, full)) {
Christopher Faulet72333522018-10-24 11:25:02 +02002634 if (!regex_exec_match2(re, ctx.value.ptr, ctx.value.len, MAX_MATCH, pmatch, 0))
2635 continue;
2636
2637 output->data = exp_replace(output->area, output->size, ctx.value.ptr, str, pmatch);
2638 if (output->data == -1)
2639 return -1;
2640 if (!http_replace_header_value(htx, &ctx, ist2(output->area, output->data)))
2641 return -1;
2642 }
2643 return 0;
2644}
2645
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002646/* This function executes one of the set-{method,path,query,uri} actions. It
2647 * takes the string from the variable 'replace' with length 'len', then modifies
2648 * the relevant part of the request line accordingly. Then it updates various
2649 * pointers to the next elements which were moved, and the total buffer length.
2650 * It finds the action to be performed in p[2], previously filled by function
2651 * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal
2652 * error, though this can be revisited when this code is finally exploited.
2653 *
2654 * 'action' can be '0' to replace method, '1' to replace path, '2' to replace
Christopher Faulet312294f2020-09-02 17:17:44 +02002655 * query string, 3 to replace uri or 4 to replace the path+query.
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002656 *
2657 * In query string case, the mark question '?' must be set at the start of the
2658 * string by the caller, event if the replacement query string is empty.
2659 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002660int http_req_replace_stline(int action, const char *replace, int len,
2661 struct proxy *px, struct stream *s)
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002662{
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002663 struct htx *htx = htxbuf(&s->req.buf);
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002664
2665 switch (action) {
2666 case 0: // method
2667 if (!http_replace_req_meth(htx, ist2(replace, len)))
2668 return -1;
2669 break;
2670
2671 case 1: // path
Christopher Fauletb8ce5052020-08-31 16:11:57 +02002672 if (!http_replace_req_path(htx, ist2(replace, len), 0))
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002673 return -1;
2674 break;
2675
2676 case 2: // query
2677 if (!http_replace_req_query(htx, ist2(replace, len)))
2678 return -1;
2679 break;
2680
2681 case 3: // uri
2682 if (!http_replace_req_uri(htx, ist2(replace, len)))
2683 return -1;
2684 break;
2685
Christopher Faulet312294f2020-09-02 17:17:44 +02002686 case 4: // path + query
2687 if (!http_replace_req_path(htx, ist2(replace, len), 1))
2688 return -1;
2689 break;
2690
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002691 default:
2692 return -1;
2693 }
2694 return 0;
2695}
2696
2697/* This function replace the HTTP status code and the associated message. The
Christopher Faulete00d06c2019-12-16 17:18:42 +01002698 * variable <status> contains the new status code. This function never fails. It
2699 * returns 0 in case of success, -1 in case of internal error.
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002700 */
Christopher Faulet96bff762019-12-17 13:46:18 +01002701int http_res_set_status(unsigned int status, struct ist reason, struct stream *s)
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002702{
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002703 struct htx *htx = htxbuf(&s->res.buf);
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002704 char *res;
2705
2706 chunk_reset(&trash);
2707 res = ultoa_o(status, trash.area, trash.size);
2708 trash.data = res - trash.area;
2709
2710 /* Do we have a custom reason format string? */
Tim Duesterhuse296d3e2020-03-05 17:56:31 +01002711 if (!isttest(reason)) {
Christopher Faulet96bff762019-12-17 13:46:18 +01002712 const char *str = http_get_reason(status);
2713 reason = ist2(str, strlen(str));
2714 }
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002715
Christopher Fauletbde2c4c2020-08-31 16:43:34 +02002716 if (!http_replace_res_status(htx, ist2(trash.area, trash.data), reason))
Christopher Faulete00d06c2019-12-16 17:18:42 +01002717 return -1;
2718 return 0;
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002719}
2720
Christopher Faulet3e964192018-10-24 11:39:23 +02002721/* Executes the http-request rules <rules> for stream <s>, proxy <px> and
2722 * transaction <txn>. Returns the verdict of the first rule that prevents
2723 * further processing of the request (auth, deny, ...), and defaults to
2724 * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or
2725 * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT
2726 * on txn->flags if it encounters a tarpit rule. If <deny_status> is not NULL
2727 * and a deny/tarpit rule is matched, it will be filled with this rule's deny
2728 * status.
2729 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002730static enum rule_result http_req_get_intercept_rule(struct proxy *px, struct list *rules,
Christopher Fauletb58f62b2020-01-13 16:40:13 +01002731 struct stream *s)
Christopher Faulet3e964192018-10-24 11:39:23 +02002732{
2733 struct session *sess = strm_sess(s);
2734 struct http_txn *txn = s->txn;
Christopher Faulet3e964192018-10-24 11:39:23 +02002735 struct act_rule *rule;
Christopher Faulet3e964192018-10-24 11:39:23 +02002736 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002737 int act_opts = 0;
Christopher Faulet3e964192018-10-24 11:39:23 +02002738
Christopher Faulet3e964192018-10-24 11:39:23 +02002739 /* If "the current_rule_list" match the executed rule list, we are in
2740 * resume condition. If a resume is needed it is always in the action
2741 * and never in the ACL or converters. In this case, we initialise the
2742 * current rule, and go to the action execution point.
2743 */
2744 if (s->current_rule) {
2745 rule = s->current_rule;
2746 s->current_rule = NULL;
2747 if (s->current_rule_list == rules)
2748 goto resume_execution;
2749 }
2750 s->current_rule_list = rules;
2751
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002752 /* start the ruleset evaluation in strict mode */
2753 txn->req.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01002754
Christopher Faulet3e964192018-10-24 11:39:23 +02002755 list_for_each_entry(rule, rules, list) {
2756 /* check optional condition */
2757 if (rule->cond) {
2758 int ret;
2759
2760 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
2761 ret = acl_pass(ret);
2762
2763 if (rule->cond->pol == ACL_COND_UNLESS)
2764 ret = !ret;
2765
2766 if (!ret) /* condition not matched */
2767 continue;
2768 }
2769
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002770 act_opts |= ACT_OPT_FIRST;
Christopher Faulet3e964192018-10-24 11:39:23 +02002771 resume_execution:
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002772 /* Always call the action function if defined */
2773 if (rule->action_ptr) {
2774 if ((s->req.flags & CF_READ_ERROR) ||
2775 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
2776 (px->options & PR_O_ABRT_CLOSE)))
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002777 act_opts |= ACT_OPT_FINAL;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002778
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002779 switch (rule->action_ptr(rule, px, sess, s, act_opts)) {
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002780 case ACT_RET_CONT:
2781 break;
2782 case ACT_RET_STOP:
2783 rule_ret = HTTP_RULE_RES_STOP;
2784 goto end;
2785 case ACT_RET_YIELD:
2786 s->current_rule = rule;
2787 rule_ret = HTTP_RULE_RES_YIELD;
2788 goto end;
2789 case ACT_RET_ERR:
2790 rule_ret = HTTP_RULE_RES_ERROR;
2791 goto end;
2792 case ACT_RET_DONE:
2793 rule_ret = HTTP_RULE_RES_DONE;
2794 goto end;
2795 case ACT_RET_DENY:
Christopher Fauletb58f62b2020-01-13 16:40:13 +01002796 if (txn->status == -1)
2797 txn->status = 403;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002798 rule_ret = HTTP_RULE_RES_DENY;
2799 goto end;
2800 case ACT_RET_ABRT:
2801 rule_ret = HTTP_RULE_RES_ABRT;
2802 goto end;
2803 case ACT_RET_INV:
2804 rule_ret = HTTP_RULE_RES_BADREQ;
2805 goto end;
2806 }
2807 continue; /* eval the next rule */
2808 }
2809
2810 /* If not action function defined, check for known actions */
Christopher Faulet3e964192018-10-24 11:39:23 +02002811 switch (rule->action) {
2812 case ACT_ACTION_ALLOW:
2813 rule_ret = HTTP_RULE_RES_STOP;
2814 goto end;
2815
2816 case ACT_ACTION_DENY:
Christopher Faulet5cb513a2020-05-13 17:56:56 +02002817 txn->status = rule->arg.http_reply->status;
2818 txn->http_reply = rule->arg.http_reply;
Christopher Faulet3e964192018-10-24 11:39:23 +02002819 rule_ret = HTTP_RULE_RES_DENY;
2820 goto end;
2821
2822 case ACT_HTTP_REQ_TARPIT:
2823 txn->flags |= TX_CLTARPIT;
Christopher Faulet5cb513a2020-05-13 17:56:56 +02002824 txn->status = rule->arg.http_reply->status;
2825 txn->http_reply = rule->arg.http_reply;
Christopher Faulet3e964192018-10-24 11:39:23 +02002826 rule_ret = HTTP_RULE_RES_DENY;
2827 goto end;
2828
Christopher Faulet3e964192018-10-24 11:39:23 +02002829 case ACT_HTTP_REDIR:
Christopher Faulet90d22a82020-03-06 11:18:39 +01002830 rule_ret = HTTP_RULE_RES_ABRT;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002831 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
Christopher Faulet3a26bee2019-12-16 12:47:40 +01002832 rule_ret = HTTP_RULE_RES_ERROR;
Christopher Faulet3e964192018-10-24 11:39:23 +02002833 goto end;
2834
2835 case ACT_HTTP_SET_NICE:
Christopher Faulet96bff762019-12-17 13:46:18 +01002836 s->task->nice = rule->arg.http.i;
Christopher Faulet3e964192018-10-24 11:39:23 +02002837 break;
2838
2839 case ACT_HTTP_SET_TOS:
Christopher Faulet96bff762019-12-17 13:46:18 +01002840 conn_set_tos(objt_conn(sess->origin), rule->arg.http.i);
Christopher Faulet3e964192018-10-24 11:39:23 +02002841 break;
2842
2843 case ACT_HTTP_SET_MARK:
Christopher Faulet96bff762019-12-17 13:46:18 +01002844 conn_set_mark(objt_conn(sess->origin), rule->arg.http.i);
Christopher Faulet3e964192018-10-24 11:39:23 +02002845 break;
2846
2847 case ACT_HTTP_SET_LOGL:
Christopher Faulet96bff762019-12-17 13:46:18 +01002848 s->logs.level = rule->arg.http.i;
Christopher Faulet3e964192018-10-24 11:39:23 +02002849 break;
2850
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002851 /* other flags exists, but normally, they never be matched. */
Christopher Faulet3e964192018-10-24 11:39:23 +02002852 default:
2853 break;
2854 }
2855 }
2856
2857 end:
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002858 /* if the ruleset evaluation is finished reset the strict mode */
Christopher Faulet46f95542019-12-20 10:07:22 +01002859 if (rule_ret != HTTP_RULE_RES_YIELD)
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002860 txn->req.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01002861
Christopher Faulet3e964192018-10-24 11:39:23 +02002862 /* we reached the end of the rules, nothing to report */
2863 return rule_ret;
2864}
2865
2866/* Executes the http-response rules <rules> for stream <s> and proxy <px>. It
2867 * returns one of 5 possible statuses: HTTP_RULE_RES_CONT, HTTP_RULE_RES_STOP,
2868 * HTTP_RULE_RES_DONE, HTTP_RULE_RES_YIELD, or HTTP_RULE_RES_BADREQ. If *CONT
2869 * is returned, the process can continue the evaluation of next rule list. If
2870 * *STOP or *DONE is returned, the process must stop the evaluation. If *BADREQ
2871 * is returned, it means the operation could not be processed and a server error
Christopher Fauleta53abad2020-05-13 08:12:22 +02002872 * must be returned. If *YIELD is returned, the caller must call again the
2873 * function with the same context.
Christopher Faulet3e964192018-10-24 11:39:23 +02002874 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002875static enum rule_result http_res_get_intercept_rule(struct proxy *px, struct list *rules,
2876 struct stream *s)
Christopher Faulet3e964192018-10-24 11:39:23 +02002877{
2878 struct session *sess = strm_sess(s);
2879 struct http_txn *txn = s->txn;
Christopher Faulet3e964192018-10-24 11:39:23 +02002880 struct act_rule *rule;
Christopher Faulet3e964192018-10-24 11:39:23 +02002881 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002882 int act_opts = 0;
Christopher Faulet3e964192018-10-24 11:39:23 +02002883
Christopher Faulet3e964192018-10-24 11:39:23 +02002884 /* If "the current_rule_list" match the executed rule list, we are in
2885 * resume condition. If a resume is needed it is always in the action
2886 * and never in the ACL or converters. In this case, we initialise the
2887 * current rule, and go to the action execution point.
2888 */
2889 if (s->current_rule) {
2890 rule = s->current_rule;
2891 s->current_rule = NULL;
2892 if (s->current_rule_list == rules)
2893 goto resume_execution;
2894 }
2895 s->current_rule_list = rules;
2896
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002897 /* start the ruleset evaluation in strict mode */
2898 txn->rsp.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01002899
Christopher Faulet3e964192018-10-24 11:39:23 +02002900 list_for_each_entry(rule, rules, list) {
2901 /* check optional condition */
2902 if (rule->cond) {
2903 int ret;
2904
2905 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
2906 ret = acl_pass(ret);
2907
2908 if (rule->cond->pol == ACL_COND_UNLESS)
2909 ret = !ret;
2910
2911 if (!ret) /* condition not matched */
2912 continue;
2913 }
2914
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002915 act_opts |= ACT_OPT_FIRST;
Christopher Faulet3e964192018-10-24 11:39:23 +02002916resume_execution:
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002917
2918 /* Always call the action function if defined */
2919 if (rule->action_ptr) {
2920 if ((s->req.flags & CF_READ_ERROR) ||
2921 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
2922 (px->options & PR_O_ABRT_CLOSE)))
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002923 act_opts |= ACT_OPT_FINAL;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002924
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002925 switch (rule->action_ptr(rule, px, sess, s, act_opts)) {
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002926 case ACT_RET_CONT:
2927 break;
2928 case ACT_RET_STOP:
2929 rule_ret = HTTP_RULE_RES_STOP;
2930 goto end;
2931 case ACT_RET_YIELD:
2932 s->current_rule = rule;
2933 rule_ret = HTTP_RULE_RES_YIELD;
2934 goto end;
2935 case ACT_RET_ERR:
2936 rule_ret = HTTP_RULE_RES_ERROR;
2937 goto end;
2938 case ACT_RET_DONE:
2939 rule_ret = HTTP_RULE_RES_DONE;
2940 goto end;
2941 case ACT_RET_DENY:
Christopher Fauletb58f62b2020-01-13 16:40:13 +01002942 if (txn->status == -1)
2943 txn->status = 502;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002944 rule_ret = HTTP_RULE_RES_DENY;
2945 goto end;
2946 case ACT_RET_ABRT:
2947 rule_ret = HTTP_RULE_RES_ABRT;
2948 goto end;
2949 case ACT_RET_INV:
2950 rule_ret = HTTP_RULE_RES_BADREQ;
2951 goto end;
2952 }
2953 continue; /* eval the next rule */
2954 }
2955
2956 /* If not action function defined, check for known actions */
Christopher Faulet3e964192018-10-24 11:39:23 +02002957 switch (rule->action) {
2958 case ACT_ACTION_ALLOW:
2959 rule_ret = HTTP_RULE_RES_STOP; /* "allow" rules are OK */
2960 goto end;
2961
2962 case ACT_ACTION_DENY:
Christopher Faulet5cb513a2020-05-13 17:56:56 +02002963 txn->status = rule->arg.http_reply->status;
2964 txn->http_reply = rule->arg.http_reply;
Christopher Faulet3a26bee2019-12-16 12:47:40 +01002965 rule_ret = HTTP_RULE_RES_DENY;
Christopher Faulet3e964192018-10-24 11:39:23 +02002966 goto end;
2967
2968 case ACT_HTTP_SET_NICE:
Christopher Faulet96bff762019-12-17 13:46:18 +01002969 s->task->nice = rule->arg.http.i;
Christopher Faulet3e964192018-10-24 11:39:23 +02002970 break;
2971
2972 case ACT_HTTP_SET_TOS:
Christopher Faulet96bff762019-12-17 13:46:18 +01002973 conn_set_tos(objt_conn(sess->origin), rule->arg.http.i);
Christopher Faulet3e964192018-10-24 11:39:23 +02002974 break;
2975
2976 case ACT_HTTP_SET_MARK:
Christopher Faulet96bff762019-12-17 13:46:18 +01002977 conn_set_mark(objt_conn(sess->origin), rule->arg.http.i);
Christopher Faulet3e964192018-10-24 11:39:23 +02002978 break;
2979
2980 case ACT_HTTP_SET_LOGL:
Christopher Faulet96bff762019-12-17 13:46:18 +01002981 s->logs.level = rule->arg.http.i;
Christopher Faulet3e964192018-10-24 11:39:23 +02002982 break;
2983
Christopher Faulet3e964192018-10-24 11:39:23 +02002984 case ACT_HTTP_REDIR:
Christopher Faulet49c2a702020-03-06 15:44:37 +01002985 rule_ret = HTTP_RULE_RES_ABRT;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002986 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
Christopher Faulet3a26bee2019-12-16 12:47:40 +01002987 rule_ret = HTTP_RULE_RES_ERROR;
Christopher Faulet3e964192018-10-24 11:39:23 +02002988 goto end;
2989
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002990 /* other flags exists, but normally, they never be matched. */
Christopher Faulet3e964192018-10-24 11:39:23 +02002991 default:
2992 break;
2993 }
2994 }
2995
2996 end:
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002997 /* if the ruleset evaluation is finished reset the strict mode */
Christopher Faulet46f95542019-12-20 10:07:22 +01002998 if (rule_ret != HTTP_RULE_RES_YIELD)
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002999 txn->rsp.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01003000
Christopher Faulet3e964192018-10-24 11:39:23 +02003001 /* we reached the end of the rules, nothing to report */
3002 return rule_ret;
3003}
3004
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01003005/* Executes backend and frontend http-after-response rules for the stream <s>,
3006 * in that order. it return 1 on success and 0 on error. It is the caller
3007 * responsibility to catch error or ignore it. If it catches it, this function
3008 * may be called a second time, for the internal error.
3009 */
3010int http_eval_after_res_rules(struct stream *s)
3011{
3012 struct session *sess = s->sess;
3013 enum rule_result ret = HTTP_RULE_RES_CONT;
3014
Christopher Faulet507479b2020-05-15 12:29:46 +02003015 /* Eval after-response ruleset only if the reply is not const */
3016 if (s->txn->flags & TX_CONST_REPLY)
3017 goto end;
3018
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01003019 /* prune the request variables if not already done and swap to the response variables. */
3020 if (s->vars_reqres.scope != SCOPE_RES) {
3021 if (!LIST_ISEMPTY(&s->vars_reqres.head))
3022 vars_prune(&s->vars_reqres, s->sess, s);
3023 vars_init(&s->vars_reqres, SCOPE_RES);
3024 }
3025
3026 ret = http_res_get_intercept_rule(s->be, &s->be->http_after_res_rules, s);
3027 if ((ret == HTTP_RULE_RES_CONT || ret == HTTP_RULE_RES_STOP) && sess->fe != s->be)
3028 ret = http_res_get_intercept_rule(sess->fe, &sess->fe->http_after_res_rules, s);
3029
Christopher Faulet507479b2020-05-15 12:29:46 +02003030 end:
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01003031 /* All other codes than CONTINUE, STOP or DONE are forbidden */
3032 return (ret == HTTP_RULE_RES_CONT || ret == HTTP_RULE_RES_STOP || ret == HTTP_RULE_RES_DONE);
3033}
3034
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003035/*
3036 * Manage client-side cookie. It can impact performance by about 2% so it is
3037 * desirable to call it only when needed. This code is quite complex because
3038 * of the multiple very crappy and ambiguous syntaxes we have to support. it
3039 * highly recommended not to touch this part without a good reason !
3040 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003041static void http_manage_client_side_cookies(struct stream *s, struct channel *req)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003042{
3043 struct session *sess = s->sess;
3044 struct http_txn *txn = s->txn;
3045 struct htx *htx;
3046 struct http_hdr_ctx ctx;
3047 char *hdr_beg, *hdr_end, *del_from;
3048 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
3049 int preserve_hdr;
3050
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003051 htx = htxbuf(&req->buf);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003052 ctx.blk = NULL;
3053 while (http_find_header(htx, ist("Cookie"), &ctx, 1)) {
Olivier Houchardf0f42382019-07-22 17:43:46 +02003054 int is_first = 1;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003055 del_from = NULL; /* nothing to be deleted */
3056 preserve_hdr = 0; /* assume we may kill the whole header */
3057
3058 /* Now look for cookies. Conforming to RFC2109, we have to support
3059 * attributes whose name begin with a '$', and associate them with
3060 * the right cookie, if we want to delete this cookie.
3061 * So there are 3 cases for each cookie read :
3062 * 1) it's a special attribute, beginning with a '$' : ignore it.
3063 * 2) it's a server id cookie that we *MAY* want to delete : save
3064 * some pointers on it (last semi-colon, beginning of cookie...)
3065 * 3) it's an application cookie : we *MAY* have to delete a previous
3066 * "special" cookie.
3067 * At the end of loop, if a "special" cookie remains, we may have to
3068 * remove it. If no application cookie persists in the header, we
3069 * *MUST* delete it.
3070 *
3071 * Note: RFC2965 is unclear about the processing of spaces around
3072 * the equal sign in the ATTR=VALUE form. A careful inspection of
3073 * the RFC explicitly allows spaces before it, and not within the
3074 * tokens (attrs or values). An inspection of RFC2109 allows that
3075 * too but section 10.1.3 lets one think that spaces may be allowed
3076 * after the equal sign too, resulting in some (rare) buggy
3077 * implementations trying to do that. So let's do what servers do.
3078 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
3079 * allowed quoted strings in values, with any possible character
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003080 * after a backslash, including control chars and delimiters, which
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003081 * causes parsing to become ambiguous. Browsers also allow spaces
3082 * within values even without quotes.
3083 *
3084 * We have to keep multiple pointers in order to support cookie
3085 * removal at the beginning, middle or end of header without
3086 * corrupting the header. All of these headers are valid :
3087 *
3088 * hdr_beg hdr_end
3089 * | |
3090 * v |
3091 * NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3 |
3092 * NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3 v
3093 * NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3
3094 * | | | | | | |
3095 * | | | | | | |
3096 * | | | | | | +--> next
3097 * | | | | | +----> val_end
3098 * | | | | +-----------> val_beg
3099 * | | | +--------------> equal
3100 * | | +----------------> att_end
3101 * | +---------------------> att_beg
3102 * +--------------------------> prev
3103 *
3104 */
3105 hdr_beg = ctx.value.ptr;
3106 hdr_end = hdr_beg + ctx.value.len;
3107 for (prev = hdr_beg; prev < hdr_end; prev = next) {
3108 /* Iterate through all cookies on this line */
3109
3110 /* find att_beg */
3111 att_beg = prev;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003112 if (!is_first)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003113 att_beg++;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003114 is_first = 0;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003115
3116 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
3117 att_beg++;
3118
3119 /* find att_end : this is the first character after the last non
3120 * space before the equal. It may be equal to hdr_end.
3121 */
3122 equal = att_end = att_beg;
3123 while (equal < hdr_end) {
3124 if (*equal == '=' || *equal == ',' || *equal == ';')
3125 break;
3126 if (HTTP_IS_SPHT(*equal++))
3127 continue;
3128 att_end = equal;
3129 }
3130
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003131 /* here, <equal> points to '=', a delimiter or the end. <att_end>
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003132 * is between <att_beg> and <equal>, both may be identical.
3133 */
3134 /* look for end of cookie if there is an equal sign */
3135 if (equal < hdr_end && *equal == '=') {
3136 /* look for the beginning of the value */
3137 val_beg = equal + 1;
3138 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
3139 val_beg++;
3140
3141 /* find the end of the value, respecting quotes */
3142 next = http_find_cookie_value_end(val_beg, hdr_end);
3143
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003144 /* make val_end point to the first white space or delimiter after the value */
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003145 val_end = next;
3146 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
3147 val_end--;
3148 }
3149 else
3150 val_beg = val_end = next = equal;
3151
3152 /* We have nothing to do with attributes beginning with
3153 * '$'. However, they will automatically be removed if a
3154 * header before them is removed, since they're supposed
3155 * to be linked together.
3156 */
3157 if (*att_beg == '$')
3158 continue;
3159
3160 /* Ignore cookies with no equal sign */
3161 if (equal == next) {
3162 /* This is not our cookie, so we must preserve it. But if we already
3163 * scheduled another cookie for removal, we cannot remove the
3164 * complete header, but we can remove the previous block itself.
3165 */
3166 preserve_hdr = 1;
3167 if (del_from != NULL) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003168 int delta = http_del_hdr_value(hdr_beg, hdr_end, &del_from, prev);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003169 val_end += delta;
3170 next += delta;
3171 hdr_end += delta;
3172 prev = del_from;
3173 del_from = NULL;
3174 }
3175 continue;
3176 }
3177
3178 /* if there are spaces around the equal sign, we need to
3179 * strip them otherwise we'll get trouble for cookie captures,
3180 * or even for rewrites. Since this happens extremely rarely,
3181 * it does not hurt performance.
3182 */
3183 if (unlikely(att_end != equal || val_beg > equal + 1)) {
3184 int stripped_before = 0;
3185 int stripped_after = 0;
3186
3187 if (att_end != equal) {
3188 memmove(att_end, equal, hdr_end - equal);
3189 stripped_before = (att_end - equal);
3190 equal += stripped_before;
3191 val_beg += stripped_before;
3192 }
3193
3194 if (val_beg > equal + 1) {
3195 memmove(equal + 1, val_beg, hdr_end + stripped_before - val_beg);
3196 stripped_after = (equal + 1) - val_beg;
3197 val_beg += stripped_after;
3198 stripped_before += stripped_after;
3199 }
3200
3201 val_end += stripped_before;
3202 next += stripped_before;
3203 hdr_end += stripped_before;
3204 }
3205 /* now everything is as on the diagram above */
3206
3207 /* First, let's see if we want to capture this cookie. We check
3208 * that we don't already have a client side cookie, because we
3209 * can only capture one. Also as an optimisation, we ignore
3210 * cookies shorter than the declared name.
3211 */
3212 if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL &&
3213 (val_end - att_beg >= sess->fe->capture_namelen) &&
3214 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
3215 int log_len = val_end - att_beg;
3216
3217 if ((txn->cli_cookie = pool_alloc(pool_head_capture)) == NULL) {
3218 ha_alert("HTTP logging : out of memory.\n");
3219 } else {
3220 if (log_len > sess->fe->capture_len)
3221 log_len = sess->fe->capture_len;
3222 memcpy(txn->cli_cookie, att_beg, log_len);
3223 txn->cli_cookie[log_len] = 0;
3224 }
3225 }
3226
3227 /* Persistence cookies in passive, rewrite or insert mode have the
3228 * following form :
3229 *
3230 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
3231 *
3232 * For cookies in prefix mode, the form is :
3233 *
3234 * Cookie: NAME=SRV~VALUE
3235 */
3236 if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
3237 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
3238 struct server *srv = s->be->srv;
3239 char *delim;
3240
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003241 /* if we're in cookie prefix mode, we'll search the delimiter so that we
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003242 * have the server ID between val_beg and delim, and the original cookie between
3243 * delim+1 and val_end. Otherwise, delim==val_end :
3244 *
3245 * hdr_beg
3246 * |
3247 * v
3248 * NAME=SRV; # in all but prefix modes
3249 * NAME=SRV~OPAQUE ; # in prefix mode
3250 * || || | |+-> next
3251 * || || | +--> val_end
3252 * || || +---------> delim
3253 * || |+------------> val_beg
3254 * || +-------------> att_end = equal
3255 * |+-----------------> att_beg
3256 * +------------------> prev
3257 *
3258 */
3259 if (s->be->ck_opts & PR_CK_PFX) {
3260 for (delim = val_beg; delim < val_end; delim++)
3261 if (*delim == COOKIE_DELIM)
3262 break;
3263 }
3264 else {
3265 char *vbar1;
3266 delim = val_end;
3267 /* Now check if the cookie contains a date field, which would
3268 * appear after a vertical bar ('|') just after the server name
3269 * and before the delimiter.
3270 */
3271 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
3272 if (vbar1) {
3273 /* OK, so left of the bar is the server's cookie and
3274 * right is the last seen date. It is a base64 encoded
3275 * 30-bit value representing the UNIX date since the
3276 * epoch in 4-second quantities.
3277 */
3278 int val;
3279 delim = vbar1++;
3280 if (val_end - vbar1 >= 5) {
3281 val = b64tos30(vbar1);
3282 if (val > 0)
3283 txn->cookie_last_date = val << 2;
3284 }
3285 /* look for a second vertical bar */
3286 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
3287 if (vbar1 && (val_end - vbar1 > 5)) {
3288 val = b64tos30(vbar1 + 1);
3289 if (val > 0)
3290 txn->cookie_first_date = val << 2;
3291 }
3292 }
3293 }
3294
3295 /* if the cookie has an expiration date and the proxy wants to check
3296 * it, then we do that now. We first check if the cookie is too old,
3297 * then only if it has expired. We detect strict overflow because the
3298 * time resolution here is not great (4 seconds). Cookies with dates
3299 * in the future are ignored if their offset is beyond one day. This
3300 * allows an admin to fix timezone issues without expiring everyone
3301 * and at the same time avoids keeping unwanted side effects for too
3302 * long.
3303 */
3304 if (txn->cookie_first_date && s->be->cookie_maxlife &&
3305 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) ||
3306 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
3307 txn->flags &= ~TX_CK_MASK;
3308 txn->flags |= TX_CK_OLD;
3309 delim = val_beg; // let's pretend we have not found the cookie
3310 txn->cookie_first_date = 0;
3311 txn->cookie_last_date = 0;
3312 }
3313 else if (txn->cookie_last_date && s->be->cookie_maxidle &&
3314 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) ||
3315 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
3316 txn->flags &= ~TX_CK_MASK;
3317 txn->flags |= TX_CK_EXPIRED;
3318 delim = val_beg; // let's pretend we have not found the cookie
3319 txn->cookie_first_date = 0;
3320 txn->cookie_last_date = 0;
3321 }
3322
3323 /* Here, we'll look for the first running server which supports the cookie.
3324 * This allows to share a same cookie between several servers, for example
3325 * to dedicate backup servers to specific servers only.
3326 * However, to prevent clients from sticking to cookie-less backup server
3327 * when they have incidentely learned an empty cookie, we simply ignore
3328 * empty cookies and mark them as invalid.
3329 * The same behaviour is applied when persistence must be ignored.
3330 */
3331 if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
3332 srv = NULL;
3333
3334 while (srv) {
3335 if (srv->cookie && (srv->cklen == delim - val_beg) &&
3336 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
3337 if ((srv->cur_state != SRV_ST_STOPPED) ||
3338 (s->be->options & PR_O_PERSIST) ||
3339 (s->flags & SF_FORCE_PRST)) {
3340 /* we found the server and we can use it */
3341 txn->flags &= ~TX_CK_MASK;
3342 txn->flags |= (srv->cur_state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
3343 s->flags |= SF_DIRECT | SF_ASSIGNED;
3344 s->target = &srv->obj_type;
3345 break;
3346 } else {
3347 /* we found a server, but it's down,
3348 * mark it as such and go on in case
3349 * another one is available.
3350 */
3351 txn->flags &= ~TX_CK_MASK;
3352 txn->flags |= TX_CK_DOWN;
3353 }
3354 }
3355 srv = srv->next;
3356 }
3357
3358 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
3359 /* no server matched this cookie or we deliberately skipped it */
3360 txn->flags &= ~TX_CK_MASK;
3361 if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
3362 txn->flags |= TX_CK_UNUSED;
3363 else
3364 txn->flags |= TX_CK_INVALID;
3365 }
3366
3367 /* depending on the cookie mode, we may have to either :
3368 * - delete the complete cookie if we're in insert+indirect mode, so that
3369 * the server never sees it ;
3370 * - remove the server id from the cookie value, and tag the cookie as an
Joseph Herlante9d5c722018-11-25 11:00:25 -08003371 * application cookie so that it does not get accidentally removed later,
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003372 * if we're in cookie prefix mode
3373 */
3374 if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
3375 int delta; /* negative */
3376
3377 memmove(val_beg, delim + 1, hdr_end - (delim + 1));
3378 delta = val_beg - (delim + 1);
3379 val_end += delta;
3380 next += delta;
3381 hdr_end += delta;
3382 del_from = NULL;
3383 preserve_hdr = 1; /* we want to keep this cookie */
3384 }
3385 else if (del_from == NULL &&
3386 (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
3387 del_from = prev;
3388 }
3389 }
3390 else {
3391 /* This is not our cookie, so we must preserve it. But if we already
3392 * scheduled another cookie for removal, we cannot remove the
3393 * complete header, but we can remove the previous block itself.
3394 */
3395 preserve_hdr = 1;
3396
3397 if (del_from != NULL) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003398 int delta = http_del_hdr_value(hdr_beg, hdr_end, &del_from, prev);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003399 if (att_beg >= del_from)
3400 att_beg += delta;
3401 if (att_end >= del_from)
3402 att_end += delta;
3403 val_beg += delta;
3404 val_end += delta;
3405 next += delta;
3406 hdr_end += delta;
3407 prev = del_from;
3408 del_from = NULL;
3409 }
3410 }
3411
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003412 } /* for each cookie */
3413
3414
3415 /* There are no more cookies on this line.
3416 * We may still have one (or several) marked for deletion at the
3417 * end of the line. We must do this now in two ways :
3418 * - if some cookies must be preserved, we only delete from the
3419 * mark to the end of line ;
3420 * - if nothing needs to be preserved, simply delete the whole header
3421 */
3422 if (del_from) {
3423 hdr_end = (preserve_hdr ? del_from : hdr_beg);
3424 }
3425 if ((hdr_end - hdr_beg) != ctx.value.len) {
Christopher Faulet3e2638e2019-06-18 09:49:16 +02003426 if (hdr_beg != hdr_end)
3427 htx_change_blk_value_len(htx, ctx.blk, hdr_end - hdr_beg);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003428 else
3429 http_remove_header(htx, &ctx);
3430 }
3431 } /* for each "Cookie header */
3432}
3433
3434/*
3435 * Manage server-side cookies. It can impact performance by about 2% so it is
3436 * desirable to call it only when needed. This function is also used when we
3437 * just need to know if there is a cookie (eg: for check-cache).
3438 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003439static void http_manage_server_side_cookies(struct stream *s, struct channel *res)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003440{
3441 struct session *sess = s->sess;
3442 struct http_txn *txn = s->txn;
3443 struct htx *htx;
3444 struct http_hdr_ctx ctx;
3445 struct server *srv;
3446 char *hdr_beg, *hdr_end;
3447 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau6f7a02a2019-04-15 21:49:49 +02003448 int is_cookie2 = 0;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003449
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003450 htx = htxbuf(&res->buf);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003451
3452 ctx.blk = NULL;
3453 while (1) {
Olivier Houchardf0f42382019-07-22 17:43:46 +02003454 int is_first = 1;
3455
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003456 if (!http_find_header(htx, ist("Set-Cookie"), &ctx, 1)) {
3457 if (!http_find_header(htx, ist("Set-Cookie2"), &ctx, 1))
3458 break;
3459 is_cookie2 = 1;
3460 }
3461
3462 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
3463 * <prev> points to the colon.
3464 */
3465 txn->flags |= TX_SCK_PRESENT;
3466
3467 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
3468 * check-cache is enabled) and we are not interested in checking
3469 * them. Warning, the cookie capture is declared in the frontend.
3470 */
3471 if (s->be->cookie_name == NULL && sess->fe->capture_name == NULL)
3472 break;
3473
3474 /* OK so now we know we have to process this response cookie.
3475 * The format of the Set-Cookie header is slightly different
3476 * from the format of the Cookie header in that it does not
3477 * support the comma as a cookie delimiter (thus the header
3478 * cannot be folded) because the Expires attribute described in
3479 * the original Netscape's spec may contain an unquoted date
3480 * with a comma inside. We have to live with this because
3481 * many browsers don't support Max-Age and some browsers don't
3482 * support quoted strings. However the Set-Cookie2 header is
3483 * clean.
3484 *
3485 * We have to keep multiple pointers in order to support cookie
3486 * removal at the beginning, middle or end of header without
3487 * corrupting the header (in case of set-cookie2). A special
3488 * pointer, <scav> points to the beginning of the set-cookie-av
3489 * fields after the first semi-colon. The <next> pointer points
3490 * either to the end of line (set-cookie) or next unquoted comma
3491 * (set-cookie2). All of these headers are valid :
3492 *
3493 * hdr_beg hdr_end
3494 * | |
3495 * v |
3496 * NAME1 = VALUE 1 ; Secure; Path="/" |
3497 * NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT v
3498 * NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT
3499 * NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard
3500 * | | | | | | | |
3501 * | | | | | | | +-> next
3502 * | | | | | | +------------> scav
3503 * | | | | | +--------------> val_end
3504 * | | | | +--------------------> val_beg
3505 * | | | +----------------------> equal
3506 * | | +------------------------> att_end
3507 * | +----------------------------> att_beg
3508 * +------------------------------> prev
3509 * -------------------------------> hdr_beg
3510 */
3511 hdr_beg = ctx.value.ptr;
3512 hdr_end = hdr_beg + ctx.value.len;
3513 for (prev = hdr_beg; prev < hdr_end; prev = next) {
3514
3515 /* Iterate through all cookies on this line */
3516
3517 /* find att_beg */
3518 att_beg = prev;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003519 if (!is_first)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003520 att_beg++;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003521 is_first = 0;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003522
3523 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
3524 att_beg++;
3525
3526 /* find att_end : this is the first character after the last non
3527 * space before the equal. It may be equal to hdr_end.
3528 */
3529 equal = att_end = att_beg;
3530
3531 while (equal < hdr_end) {
3532 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
3533 break;
3534 if (HTTP_IS_SPHT(*equal++))
3535 continue;
3536 att_end = equal;
3537 }
3538
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003539 /* here, <equal> points to '=', a delimiter or the end. <att_end>
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003540 * is between <att_beg> and <equal>, both may be identical.
3541 */
3542
3543 /* look for end of cookie if there is an equal sign */
3544 if (equal < hdr_end && *equal == '=') {
3545 /* look for the beginning of the value */
3546 val_beg = equal + 1;
3547 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
3548 val_beg++;
3549
3550 /* find the end of the value, respecting quotes */
3551 next = http_find_cookie_value_end(val_beg, hdr_end);
3552
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003553 /* make val_end point to the first white space or delimiter after the value */
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003554 val_end = next;
3555 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
3556 val_end--;
3557 }
3558 else {
3559 /* <equal> points to next comma, semi-colon or EOL */
3560 val_beg = val_end = next = equal;
3561 }
3562
3563 if (next < hdr_end) {
3564 /* Set-Cookie2 supports multiple cookies, and <next> points to
3565 * a colon or semi-colon before the end. So skip all attr-value
3566 * pairs and look for the next comma. For Set-Cookie, since
3567 * commas are permitted in values, skip to the end.
3568 */
3569 if (is_cookie2)
3570 next = http_find_hdr_value_end(next, hdr_end);
3571 else
3572 next = hdr_end;
3573 }
3574
3575 /* Now everything is as on the diagram above */
3576
3577 /* Ignore cookies with no equal sign */
3578 if (equal == val_end)
3579 continue;
3580
3581 /* If there are spaces around the equal sign, we need to
3582 * strip them otherwise we'll get trouble for cookie captures,
3583 * or even for rewrites. Since this happens extremely rarely,
3584 * it does not hurt performance.
3585 */
3586 if (unlikely(att_end != equal || val_beg > equal + 1)) {
3587 int stripped_before = 0;
3588 int stripped_after = 0;
3589
3590 if (att_end != equal) {
3591 memmove(att_end, equal, hdr_end - equal);
3592 stripped_before = (att_end - equal);
3593 equal += stripped_before;
3594 val_beg += stripped_before;
3595 }
3596
3597 if (val_beg > equal + 1) {
3598 memmove(equal + 1, val_beg, hdr_end + stripped_before - val_beg);
3599 stripped_after = (equal + 1) - val_beg;
3600 val_beg += stripped_after;
3601 stripped_before += stripped_after;
3602 }
3603
3604 val_end += stripped_before;
3605 next += stripped_before;
3606 hdr_end += stripped_before;
3607
Christopher Faulet3e2638e2019-06-18 09:49:16 +02003608 htx_change_blk_value_len(htx, ctx.blk, hdr_end - hdr_beg);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003609 ctx.value.len = hdr_end - hdr_beg;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003610 }
3611
3612 /* First, let's see if we want to capture this cookie. We check
3613 * that we don't already have a server side cookie, because we
3614 * can only capture one. Also as an optimisation, we ignore
3615 * cookies shorter than the declared name.
3616 */
3617 if (sess->fe->capture_name != NULL &&
3618 txn->srv_cookie == NULL &&
3619 (val_end - att_beg >= sess->fe->capture_namelen) &&
3620 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
3621 int log_len = val_end - att_beg;
3622 if ((txn->srv_cookie = pool_alloc(pool_head_capture)) == NULL) {
3623 ha_alert("HTTP logging : out of memory.\n");
3624 }
3625 else {
3626 if (log_len > sess->fe->capture_len)
3627 log_len = sess->fe->capture_len;
3628 memcpy(txn->srv_cookie, att_beg, log_len);
3629 txn->srv_cookie[log_len] = 0;
3630 }
3631 }
3632
3633 srv = objt_server(s->target);
3634 /* now check if we need to process it for persistence */
3635 if (!(s->flags & SF_IGNORE_PRST) &&
3636 (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
3637 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
3638 /* assume passive cookie by default */
3639 txn->flags &= ~TX_SCK_MASK;
3640 txn->flags |= TX_SCK_FOUND;
3641
3642 /* If the cookie is in insert mode on a known server, we'll delete
3643 * this occurrence because we'll insert another one later.
3644 * We'll delete it too if the "indirect" option is set and we're in
3645 * a direct access.
3646 */
3647 if (s->be->ck_opts & PR_CK_PSV) {
3648 /* The "preserve" flag was set, we don't want to touch the
3649 * server's cookie.
3650 */
3651 }
3652 else if ((srv && (s->be->ck_opts & PR_CK_INS)) ||
3653 ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) {
3654 /* this cookie must be deleted */
3655 if (prev == hdr_beg && next == hdr_end) {
3656 /* whole header */
3657 http_remove_header(htx, &ctx);
3658 /* note: while both invalid now, <next> and <hdr_end>
3659 * are still equal, so the for() will stop as expected.
3660 */
3661 } else {
3662 /* just remove the value */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003663 int delta = http_del_hdr_value(hdr_beg, hdr_end, &prev, next);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003664 next = prev;
3665 hdr_end += delta;
3666 }
3667 txn->flags &= ~TX_SCK_MASK;
3668 txn->flags |= TX_SCK_DELETED;
3669 /* and go on with next cookie */
3670 }
3671 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) {
3672 /* replace bytes val_beg->val_end with the cookie name associated
3673 * with this server since we know it.
3674 */
3675 int sliding, delta;
3676
3677 ctx.value = ist2(val_beg, val_end - val_beg);
3678 ctx.lws_before = ctx.lws_after = 0;
3679 http_replace_header_value(htx, &ctx, ist2(srv->cookie, srv->cklen));
3680 delta = srv->cklen - (val_end - val_beg);
3681 sliding = (ctx.value.ptr - val_beg);
3682 hdr_beg += sliding;
3683 val_beg += sliding;
3684 next += sliding + delta;
3685 hdr_end += sliding + delta;
3686
3687 txn->flags &= ~TX_SCK_MASK;
3688 txn->flags |= TX_SCK_REPLACED;
3689 }
3690 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) {
3691 /* insert the cookie name associated with this server
3692 * before existing cookie, and insert a delimiter between them..
3693 */
3694 int sliding, delta;
3695 ctx.value = ist2(val_beg, 0);
3696 ctx.lws_before = ctx.lws_after = 0;
3697 http_replace_header_value(htx, &ctx, ist2(srv->cookie, srv->cklen + 1));
3698 delta = srv->cklen + 1;
3699 sliding = (ctx.value.ptr - val_beg);
3700 hdr_beg += sliding;
3701 val_beg += sliding;
3702 next += sliding + delta;
3703 hdr_end += sliding + delta;
3704
3705 val_beg[srv->cklen] = COOKIE_DELIM;
3706 txn->flags &= ~TX_SCK_MASK;
3707 txn->flags |= TX_SCK_REPLACED;
3708 }
3709 }
3710 /* that's done for this cookie, check the next one on the same
3711 * line when next != hdr_end (only if is_cookie2).
3712 */
3713 }
3714 }
3715}
3716
Christopher Faulet25a02f62018-10-24 12:00:25 +02003717/*
3718 * Parses the Cache-Control and Pragma request header fields to determine if
3719 * the request may be served from the cache and/or if it is cacheable. Updates
3720 * s->txn->flags.
3721 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003722void http_check_request_for_cacheability(struct stream *s, struct channel *req)
Christopher Faulet25a02f62018-10-24 12:00:25 +02003723{
3724 struct http_txn *txn = s->txn;
3725 struct htx *htx;
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003726 struct http_hdr_ctx ctx = { .blk = NULL };
3727 int pragma_found, cc_found;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003728
3729 if ((txn->flags & (TX_CACHEABLE|TX_CACHE_IGNORE)) == TX_CACHE_IGNORE)
3730 return; /* nothing more to do here */
3731
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003732 htx = htxbuf(&req->buf);
Christopher Faulet25a02f62018-10-24 12:00:25 +02003733 pragma_found = cc_found = 0;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003734
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003735 /* Check "pragma" header for HTTP/1.0 compatibility. */
3736 if (http_find_header(htx, ist("pragma"), &ctx, 1)) {
3737 if (isteqi(ctx.value, ist("no-cache"))) {
3738 pragma_found = 1;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003739 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003740 }
Christopher Faulet25a02f62018-10-24 12:00:25 +02003741
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003742 ctx.blk = NULL;
3743 /* Don't use the cache and don't try to store if we found the
3744 * Authorization header */
3745 if (http_find_header(htx, ist("authorization"), &ctx, 1)) {
3746 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3747 txn->flags |= TX_CACHE_IGNORE;
3748 }
Christopher Faulet25a02f62018-10-24 12:00:25 +02003749
Christopher Faulet25a02f62018-10-24 12:00:25 +02003750
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003751 /* Look for "cache-control" header and iterate over all the values
3752 * until we find one that specifies that caching is possible or not. */
3753 ctx.blk = NULL;
3754 while (http_find_header(htx, ist("cache-control"), &ctx, 0)) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003755 cc_found = 1;
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003756 /* We don't check the values after max-age, max-stale nor min-fresh,
3757 * we simply don't use the cache when they're specified. */
3758 if (istmatchi(ctx.value, ist("max-age")) ||
3759 istmatchi(ctx.value, ist("no-cache")) ||
3760 istmatchi(ctx.value, ist("max-stale")) ||
3761 istmatchi(ctx.value, ist("min-fresh"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003762 txn->flags |= TX_CACHE_IGNORE;
3763 continue;
3764 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003765 if (istmatchi(ctx.value, ist("no-store"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003766 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3767 continue;
3768 }
3769 }
3770
3771 /* RFC7234#5.4:
3772 * When the Cache-Control header field is also present and
3773 * understood in a request, Pragma is ignored.
3774 * When the Cache-Control header field is not present in a
3775 * request, caches MUST consider the no-cache request
3776 * pragma-directive as having the same effect as if
3777 * "Cache-Control: no-cache" were present.
3778 */
3779 if (!cc_found && pragma_found)
3780 txn->flags |= TX_CACHE_IGNORE;
3781}
3782
3783/*
3784 * Check if response is cacheable or not. Updates s->txn->flags.
3785 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003786void http_check_response_for_cacheability(struct stream *s, struct channel *res)
Christopher Faulet25a02f62018-10-24 12:00:25 +02003787{
3788 struct http_txn *txn = s->txn;
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003789 struct http_hdr_ctx ctx = { .blk = NULL };
Christopher Faulet25a02f62018-10-24 12:00:25 +02003790 struct htx *htx;
Remi Tricot-Le Bretoncc9bf2e2020-11-12 11:14:41 +01003791 int has_freshness_info = 0;
3792 int has_validator = 0;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003793
3794 if (txn->status < 200) {
3795 /* do not try to cache interim responses! */
3796 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3797 return;
3798 }
3799
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003800 htx = htxbuf(&res->buf);
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003801 /* Check "pragma" header for HTTP/1.0 compatibility. */
3802 if (http_find_header(htx, ist("pragma"), &ctx, 1)) {
3803 if (isteqi(ctx.value, ist("no-cache"))) {
3804 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3805 return;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003806 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003807 }
Christopher Faulet25a02f62018-10-24 12:00:25 +02003808
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003809 /* Look for "cache-control" header and iterate over all the values
3810 * until we find one that specifies that caching is possible or not. */
3811 ctx.blk = NULL;
3812 while (http_find_header(htx, ist("cache-control"), &ctx, 0)) {
3813 if (isteqi(ctx.value, ist("public"))) {
3814 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003815 continue;
3816 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003817 if (isteqi(ctx.value, ist("private")) ||
3818 isteqi(ctx.value, ist("no-cache")) ||
3819 isteqi(ctx.value, ist("no-store")) ||
3820 isteqi(ctx.value, ist("max-age=0")) ||
3821 isteqi(ctx.value, ist("s-maxage=0"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003822 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003823 continue;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003824 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003825 /* We might have a no-cache="set-cookie" form. */
3826 if (istmatchi(ctx.value, ist("no-cache=\"set-cookie"))) {
3827 txn->flags &= ~TX_CACHE_COOK;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003828 continue;
3829 }
Remi Tricot-Le Bretoncc9bf2e2020-11-12 11:14:41 +01003830
3831 if (istmatchi(ctx.value, ist("s-maxage")) ||
3832 istmatchi(ctx.value, ist("max-age"))) {
3833 has_freshness_info = 1;
3834 continue;
3835 }
3836 }
3837
3838 /* If no freshness information could be found in Cache-Control values,
3839 * look for an Expires header. */
3840 if (!has_freshness_info) {
3841 ctx.blk = NULL;
3842 has_freshness_info = http_find_header(htx, ist("expires"), &ctx, 0);
Christopher Faulet25a02f62018-10-24 12:00:25 +02003843 }
Remi Tricot-Le Bretoncc9bf2e2020-11-12 11:14:41 +01003844
3845 /* If no freshness information could be found in Cache-Control or Expires
3846 * values, look for an explicit validator. */
3847 if (!has_freshness_info) {
3848 ctx.blk = NULL;
3849 has_validator = 1;
3850 if (!http_find_header(htx, ist("etag"), &ctx, 0)) {
3851 ctx.blk = NULL;
3852 if (!http_find_header(htx, ist("last-modified"), &ctx, 0))
3853 has_validator = 0;
3854 }
3855 }
3856
3857 /* We won't store an entry that has neither a cache validator nor an
3858 * explicit expiration time, as suggested in RFC 7234#3. */
3859 if (!has_freshness_info && !has_validator)
3860 txn->flags |= TX_CACHE_IGNORE;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003861}
3862
Christopher Faulet377c5a52018-10-24 21:21:30 +02003863/*
3864 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
3865 * for the current backend.
3866 *
3867 * It is assumed that the request is either a HEAD, GET, or POST and that the
3868 * uri_auth field is valid.
3869 *
3870 * Returns 1 if stats should be provided, otherwise 0.
3871 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003872static int http_stats_check_uri(struct stream *s, struct http_txn *txn, struct proxy *backend)
Christopher Faulet377c5a52018-10-24 21:21:30 +02003873{
3874 struct uri_auth *uri_auth = backend->uri_auth;
3875 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003876 struct htx_sl *sl;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003877 struct ist uri;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003878
3879 if (!uri_auth)
3880 return 0;
3881
3882 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
3883 return 0;
3884
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003885 htx = htxbuf(&s->req.buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02003886 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003887 uri = htx_sl_req_uri(sl);
Willy Tarreau1eb3b482019-10-31 15:50:28 +01003888 if (*uri_auth->uri_prefix == '/')
3889 uri = http_get_path(uri);
Christopher Faulet377c5a52018-10-24 21:21:30 +02003890
3891 /* check URI size */
3892 if (uri_auth->uri_len > uri.len)
3893 return 0;
3894
3895 if (memcmp(uri.ptr, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
3896 return 0;
3897
3898 return 1;
3899}
3900
3901/* This function prepares an applet to handle the stats. It can deal with the
3902 * "100-continue" expectation, check that admin rules are met for POST requests,
3903 * and program a response message if something was unexpected. It cannot fail
3904 * and always relies on the stats applet to complete the job. It does not touch
3905 * analysers nor counters, which are left to the caller. It does not touch
3906 * s->target which is supposed to already point to the stats applet. The caller
3907 * is expected to have already assigned an appctx to the stream.
3908 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003909static int http_handle_stats(struct stream *s, struct channel *req)
Christopher Faulet377c5a52018-10-24 21:21:30 +02003910{
3911 struct stats_admin_rule *stats_admin_rule;
3912 struct stream_interface *si = &s->si[1];
3913 struct session *sess = s->sess;
3914 struct http_txn *txn = s->txn;
3915 struct http_msg *msg = &txn->req;
3916 struct uri_auth *uri_auth = s->be->uri_auth;
3917 const char *h, *lookup, *end;
3918 struct appctx *appctx;
3919 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003920 struct htx_sl *sl;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003921
3922 appctx = si_appctx(si);
3923 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
3924 appctx->st1 = appctx->st2 = 0;
3925 appctx->ctx.stats.st_code = STAT_STATUS_INIT;
Willy Tarreau676c29e2019-10-09 10:50:01 +02003926 appctx->ctx.stats.flags |= uri_auth->flags;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003927 appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */
3928 if ((msg->flags & HTTP_MSGF_VER_11) && (txn->meth != HTTP_METH_HEAD))
3929 appctx->ctx.stats.flags |= STAT_CHUNKED;
3930
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003931 htx = htxbuf(&req->buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02003932 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003933 lookup = HTX_SL_REQ_UPTR(sl) + uri_auth->uri_len;
3934 end = HTX_SL_REQ_UPTR(sl) + HTX_SL_REQ_ULEN(sl);
Christopher Faulet377c5a52018-10-24 21:21:30 +02003935
3936 for (h = lookup; h <= end - 3; h++) {
3937 if (memcmp(h, ";up", 3) == 0) {
3938 appctx->ctx.stats.flags |= STAT_HIDE_DOWN;
3939 break;
3940 }
Willy Tarreau3e320362020-10-23 17:28:57 +02003941 if (memcmp(h, ";no-maint", 3) == 0) {
3942 appctx->ctx.stats.flags |= STAT_HIDE_MAINT;
3943 break;
3944 }
Christopher Faulet377c5a52018-10-24 21:21:30 +02003945 }
3946
3947 if (uri_auth->refresh) {
3948 for (h = lookup; h <= end - 10; h++) {
3949 if (memcmp(h, ";norefresh", 10) == 0) {
3950 appctx->ctx.stats.flags |= STAT_NO_REFRESH;
3951 break;
3952 }
3953 }
3954 }
3955
3956 for (h = lookup; h <= end - 4; h++) {
3957 if (memcmp(h, ";csv", 4) == 0) {
Christopher Faulet6338a082019-09-09 15:50:54 +02003958 appctx->ctx.stats.flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
Christopher Faulet377c5a52018-10-24 21:21:30 +02003959 break;
3960 }
3961 }
3962
3963 for (h = lookup; h <= end - 6; h++) {
3964 if (memcmp(h, ";typed", 6) == 0) {
Christopher Faulet6338a082019-09-09 15:50:54 +02003965 appctx->ctx.stats.flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
Christopher Faulet377c5a52018-10-24 21:21:30 +02003966 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
3967 break;
3968 }
3969 }
3970
Christopher Faulet6338a082019-09-09 15:50:54 +02003971 for (h = lookup; h <= end - 5; h++) {
3972 if (memcmp(h, ";json", 5) == 0) {
3973 appctx->ctx.stats.flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
3974 appctx->ctx.stats.flags |= STAT_FMT_JSON;
3975 break;
3976 }
3977 }
3978
3979 for (h = lookup; h <= end - 12; h++) {
3980 if (memcmp(h, ";json-schema", 12) == 0) {
3981 appctx->ctx.stats.flags &= ~STAT_FMT_MASK;
3982 appctx->ctx.stats.flags |= STAT_JSON_SCHM;
3983 break;
3984 }
3985 }
3986
Christopher Faulet377c5a52018-10-24 21:21:30 +02003987 for (h = lookup; h <= end - 8; h++) {
3988 if (memcmp(h, ";st=", 4) == 0) {
3989 int i;
3990 h += 4;
3991 appctx->ctx.stats.st_code = STAT_STATUS_UNKN;
3992 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
3993 if (strncmp(stat_status_codes[i], h, 4) == 0) {
3994 appctx->ctx.stats.st_code = i;
3995 break;
3996 }
3997 }
3998 break;
3999 }
4000 }
4001
4002 appctx->ctx.stats.scope_str = 0;
4003 appctx->ctx.stats.scope_len = 0;
4004 for (h = lookup; h <= end - 8; h++) {
4005 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
4006 int itx = 0;
4007 const char *h2;
4008 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
4009 const char *err;
4010
4011 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
4012 h2 = h;
Christopher Fauleted7a0662019-01-14 11:07:34 +01004013 appctx->ctx.stats.scope_str = h2 - HTX_SL_REQ_UPTR(sl);
4014 while (h < end) {
Christopher Faulet377c5a52018-10-24 21:21:30 +02004015 if (*h == ';' || *h == '&' || *h == ' ')
4016 break;
4017 itx++;
4018 h++;
4019 }
4020
4021 if (itx > STAT_SCOPE_TXT_MAXLEN)
4022 itx = STAT_SCOPE_TXT_MAXLEN;
4023 appctx->ctx.stats.scope_len = itx;
4024
4025 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
4026 memcpy(scope_txt, h2, itx);
4027 scope_txt[itx] = '\0';
4028 err = invalid_char(scope_txt);
4029 if (err) {
4030 /* bad char in search text => clear scope */
4031 appctx->ctx.stats.scope_str = 0;
4032 appctx->ctx.stats.scope_len = 0;
4033 }
4034 break;
4035 }
4036 }
4037
4038 /* now check whether we have some admin rules for this request */
4039 list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) {
4040 int ret = 1;
4041
4042 if (stats_admin_rule->cond) {
4043 ret = acl_exec_cond(stats_admin_rule->cond, s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
4044 ret = acl_pass(ret);
4045 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
4046 ret = !ret;
4047 }
4048
4049 if (ret) {
4050 /* no rule, or the rule matches */
4051 appctx->ctx.stats.flags |= STAT_ADMIN;
4052 break;
4053 }
4054 }
4055
Christopher Faulet5d45e382019-02-27 15:15:23 +01004056 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
4057 appctx->st0 = STAT_HTTP_HEAD;
4058 else if (txn->meth == HTTP_METH_POST) {
Christopher Fauletbd9e8422019-08-15 22:26:48 +02004059 if (appctx->ctx.stats.flags & STAT_ADMIN) {
Christopher Faulet377c5a52018-10-24 21:21:30 +02004060 appctx->st0 = STAT_HTTP_POST;
Christopher Fauletbd9e8422019-08-15 22:26:48 +02004061 if (msg->msg_state < HTTP_MSG_DATA)
4062 req->analysers |= AN_REQ_HTTP_BODY;
4063 }
Christopher Faulet377c5a52018-10-24 21:21:30 +02004064 else {
Christopher Faulet5d45e382019-02-27 15:15:23 +01004065 /* POST without admin level */
Christopher Faulet377c5a52018-10-24 21:21:30 +02004066 appctx->ctx.stats.flags &= ~STAT_CHUNKED;
4067 appctx->ctx.stats.st_code = STAT_STATUS_DENY;
4068 appctx->st0 = STAT_HTTP_LAST;
4069 }
4070 }
4071 else {
Christopher Faulet5d45e382019-02-27 15:15:23 +01004072 /* Unsupported method */
4073 appctx->ctx.stats.flags &= ~STAT_CHUNKED;
4074 appctx->ctx.stats.st_code = STAT_STATUS_IVAL;
4075 appctx->st0 = STAT_HTTP_LAST;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004076 }
4077
4078 s->task->nice = -32; /* small boost for HTTP statistics */
4079 return 1;
4080}
4081
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004082void http_perform_server_redirect(struct stream *s, struct stream_interface *si)
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004083{
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004084 struct channel *req = &s->req;
4085 struct channel *res = &s->res;
4086 struct server *srv;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004087 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004088 struct htx_sl *sl;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004089 struct ist path, location;
4090 unsigned int flags;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004091
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004092 /*
4093 * Create the location
4094 */
4095 chunk_reset(&trash);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004096
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004097 /* 1: add the server's prefix */
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004098 /* special prefix "/" means don't change URL */
4099 srv = __objt_server(s->target);
4100 if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') {
4101 if (!chunk_memcat(&trash, srv->rdr_pfx, srv->rdr_len))
4102 return;
4103 }
4104
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004105 /* 2: add the request Path */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004106 htx = htxbuf(&req->buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02004107 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004108 path = http_get_path(htx_sl_req_uri(sl));
Tim Duesterhused526372020-03-05 17:56:33 +01004109 if (!isttest(path))
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004110 return;
4111
4112 if (!chunk_memcat(&trash, path.ptr, path.len))
4113 return;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004114 location = ist2(trash.area, trash.data);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004115
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004116 /*
4117 * Create the 302 respone
4118 */
4119 htx = htx_from_buf(&res->buf);
4120 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
4121 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
4122 ist("HTTP/1.1"), ist("302"), ist("Found"));
4123 if (!sl)
4124 goto fail;
4125 sl->info.res.status = 302;
4126 s->txn->status = 302;
4127
4128 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) ||
4129 !htx_add_header(htx, ist("Connection"), ist("close")) ||
4130 !htx_add_header(htx, ist("Content-length"), ist("0")) ||
4131 !htx_add_header(htx, ist("Location"), location))
4132 goto fail;
4133
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004134 if (!htx_add_endof(htx, HTX_BLK_EOH))
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004135 goto fail;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004136
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004137 htx->flags |= HTX_FL_EOM;
Christopher Fauletc20afb82020-01-24 19:16:26 +01004138 htx_to_buf(htx, &res->buf);
Christopher Fauleta72a7e42020-01-28 09:28:11 +01004139 if (!http_forward_proxy_resp(s, 1))
4140 goto fail;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004141
4142 /* return without error. */
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004143 si_shutr(si);
4144 si_shutw(si);
4145 si->err_type = SI_ET_NONE;
4146 si->state = SI_ST_CLO;
4147
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004148 if (!(s->flags & SF_ERR_MASK))
4149 s->flags |= SF_ERR_LOCAL;
4150 if (!(s->flags & SF_FINST_MASK))
4151 s->flags |= SF_FINST_C;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004152
4153 /* FIXME: we should increase a counter of redirects per server and per backend. */
4154 srv_inc_sess_ctr(srv);
4155 srv_set_sess_last(srv);
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004156 return;
4157
4158 fail:
4159 /* If an error occurred, remove the incomplete HTTP response from the
4160 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004161 channel_htx_truncate(res, htx);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004162}
4163
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05004164/* This function terminates the request because it was completely analyzed or
Christopher Fauletf2824e62018-10-01 12:12:37 +02004165 * because an error was triggered during the body forwarding.
4166 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004167static void http_end_request(struct stream *s)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004168{
4169 struct channel *chn = &s->req;
4170 struct http_txn *txn = s->txn;
4171
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004172 DBG_TRACE_ENTER(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004173
Christopher Fauletb42a8b62018-11-19 21:59:00 +01004174 if (unlikely(txn->req.msg_state == HTTP_MSG_ERROR ||
4175 txn->rsp.msg_state == HTTP_MSG_ERROR)) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004176 channel_abort(chn);
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004177 channel_htx_truncate(chn, htxbuf(&chn->buf));
Christopher Fauletf2824e62018-10-01 12:12:37 +02004178 goto end;
4179 }
4180
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004181 if (unlikely(txn->req.msg_state < HTTP_MSG_DONE)) {
4182 DBG_TRACE_DEVEL("waiting end of the request", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004183 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004184 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004185
4186 if (txn->req.msg_state == HTTP_MSG_DONE) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004187 /* No need to read anymore, the request was completely parsed.
4188 * We can shut the read side unless we want to abort_on_close,
4189 * or we have a POST request. The issue with POST requests is
4190 * that some browsers still send a CRLF after the request, and
4191 * this CRLF must be read so that it does not remain in the kernel
4192 * buffers, otherwise a close could cause an RST on some systems
4193 * (eg: Linux).
4194 */
Christopher Faulet769d0e92019-03-22 14:23:18 +01004195 if (!(s->be->options & PR_O_ABRT_CLOSE) && txn->meth != HTTP_METH_POST)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004196 channel_dont_read(chn);
4197
4198 /* if the server closes the connection, we want to immediately react
4199 * and close the socket to save packets and syscalls.
4200 */
4201 s->si[1].flags |= SI_FL_NOHALF;
4202
4203 /* In any case we've finished parsing the request so we must
4204 * disable Nagle when sending data because 1) we're not going
4205 * to shut this side, and 2) the server is waiting for us to
4206 * send pending data.
4207 */
4208 chn->flags |= CF_NEVER_WAIT;
4209
Christopher Fauletd01ce402019-01-02 17:44:13 +01004210 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
4211 /* The server has not finished to respond, so we
4212 * don't want to move in order not to upset it.
4213 */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004214 DBG_TRACE_DEVEL("waiting end of the response", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletd01ce402019-01-02 17:44:13 +01004215 return;
4216 }
4217
Christopher Fauletf2824e62018-10-01 12:12:37 +02004218 /* When we get here, it means that both the request and the
4219 * response have finished receiving. Depending on the connection
4220 * mode, we'll have to wait for the last bytes to leave in either
4221 * direction, and sometimes for a close to be effective.
4222 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02004223 if (txn->flags & TX_CON_WANT_TUN) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004224 /* Tunnel mode will not have any analyser so it needs to
4225 * poll for reads.
4226 */
4227 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004228 if (b_data(&chn->buf)) {
4229 DBG_TRACE_DEVEL("waiting to flush the request", STRM_EV_HTTP_ANA, s, txn);
Christopher Faulet9768c262018-10-22 09:34:31 +02004230 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004231 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004232 txn->req.msg_state = HTTP_MSG_TUNNEL;
4233 }
4234 else {
4235 /* we're not expecting any new data to come for this
4236 * transaction, so we can close it.
Christopher Faulet9768c262018-10-22 09:34:31 +02004237 *
4238 * However, there is an exception if the response
4239 * length is undefined. In this case, we need to wait
4240 * the close from the server. The response will be
4241 * switched in TUNNEL mode until the end.
Christopher Fauletf2824e62018-10-01 12:12:37 +02004242 */
4243 if (!(txn->rsp.flags & HTTP_MSGF_XFER_LEN) &&
4244 txn->rsp.msg_state != HTTP_MSG_CLOSED)
Christopher Faulet9768c262018-10-22 09:34:31 +02004245 goto check_channel_flags;
Christopher Fauletf2824e62018-10-01 12:12:37 +02004246
4247 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4248 channel_shutr_now(chn);
4249 channel_shutw_now(chn);
4250 }
4251 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004252 goto check_channel_flags;
4253 }
4254
4255 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
4256 http_msg_closing:
4257 /* nothing else to forward, just waiting for the output buffer
4258 * to be empty and for the shutw_now to take effect.
4259 */
4260 if (channel_is_empty(chn)) {
4261 txn->req.msg_state = HTTP_MSG_CLOSED;
4262 goto http_msg_closed;
4263 }
4264 else if (chn->flags & CF_SHUTW) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004265 txn->req.msg_state = HTTP_MSG_ERROR;
4266 goto end;
4267 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004268 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004269 return;
4270 }
4271
4272 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
4273 http_msg_closed:
Christopher Fauletf2824e62018-10-01 12:12:37 +02004274 /* if we don't know whether the server will close, we need to hard close */
4275 if (txn->rsp.flags & HTTP_MSGF_XFER_LEN)
4276 s->si[1].flags |= SI_FL_NOLINGER; /* we want to close ASAP */
Christopher Fauletf2824e62018-10-01 12:12:37 +02004277 /* see above in MSG_DONE why we only do this in these states */
Christopher Faulet769d0e92019-03-22 14:23:18 +01004278 if (!(s->be->options & PR_O_ABRT_CLOSE))
Christopher Fauletf2824e62018-10-01 12:12:37 +02004279 channel_dont_read(chn);
4280 goto end;
4281 }
4282
4283 check_channel_flags:
4284 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4285 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4286 /* if we've just closed an output, let's switch */
4287 txn->req.msg_state = HTTP_MSG_CLOSING;
4288 goto http_msg_closing;
4289 }
4290
4291 end:
4292 chn->analysers &= AN_REQ_FLT_END;
Christopher Faulet198ef8b2020-12-15 13:32:55 +01004293 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
4294 chn->flags |= CF_NEVER_WAIT;
4295 if (HAS_REQ_DATA_FILTERS(s))
Christopher Fauletf2824e62018-10-01 12:12:37 +02004296 chn->analysers |= AN_REQ_FLT_XFER_DATA;
Christopher Faulet198ef8b2020-12-15 13:32:55 +01004297 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004298 channel_auto_close(chn);
4299 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004300 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004301}
4302
4303
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05004304/* This function terminates the response because it was completely analyzed or
Christopher Fauletf2824e62018-10-01 12:12:37 +02004305 * because an error was triggered during the body forwarding.
4306 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004307static void http_end_response(struct stream *s)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004308{
4309 struct channel *chn = &s->res;
4310 struct http_txn *txn = s->txn;
4311
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004312 DBG_TRACE_ENTER(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004313
Christopher Fauletb42a8b62018-11-19 21:59:00 +01004314 if (unlikely(txn->req.msg_state == HTTP_MSG_ERROR ||
4315 txn->rsp.msg_state == HTTP_MSG_ERROR)) {
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004316 channel_htx_truncate(&s->req, htxbuf(&s->req.buf));
Christopher Faulet9768c262018-10-22 09:34:31 +02004317 channel_abort(&s->req);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004318 goto end;
4319 }
4320
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004321 if (unlikely(txn->rsp.msg_state < HTTP_MSG_DONE)) {
4322 DBG_TRACE_DEVEL("waiting end of the response", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004323 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004324 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004325
4326 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
4327 /* In theory, we don't need to read anymore, but we must
4328 * still monitor the server connection for a possible close
4329 * while the request is being uploaded, so we don't disable
4330 * reading.
4331 */
4332 /* channel_dont_read(chn); */
4333
4334 if (txn->req.msg_state < HTTP_MSG_DONE) {
4335 /* The client seems to still be sending data, probably
4336 * because we got an error response during an upload.
4337 * We have the choice of either breaking the connection
4338 * or letting it pass through. Let's do the later.
4339 */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004340 DBG_TRACE_DEVEL("waiting end of the request", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004341 return;
4342 }
4343
4344 /* When we get here, it means that both the request and the
4345 * response have finished receiving. Depending on the connection
4346 * mode, we'll have to wait for the last bytes to leave in either
4347 * direction, and sometimes for a close to be effective.
4348 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02004349 if (txn->flags & TX_CON_WANT_TUN) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004350 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004351 if (b_data(&chn->buf)) {
4352 DBG_TRACE_DEVEL("waiting to flush the respone", STRM_EV_HTTP_ANA, s, txn);
Christopher Faulet9768c262018-10-22 09:34:31 +02004353 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004354 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004355 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
4356 }
4357 else {
4358 /* we're not expecting any new data to come for this
4359 * transaction, so we can close it.
4360 */
4361 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4362 channel_shutr_now(chn);
4363 channel_shutw_now(chn);
4364 }
4365 }
4366 goto check_channel_flags;
4367 }
4368
4369 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
4370 http_msg_closing:
4371 /* nothing else to forward, just waiting for the output buffer
4372 * to be empty and for the shutw_now to take effect.
4373 */
4374 if (channel_is_empty(chn)) {
4375 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4376 goto http_msg_closed;
4377 }
4378 else if (chn->flags & CF_SHUTW) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004379 txn->rsp.msg_state = HTTP_MSG_ERROR;
Christopher Fauletcff0f732019-12-16 16:13:44 +01004380 _HA_ATOMIC_ADD(&strm_sess(s)->fe->fe_counters.cli_aborts, 1);
Olivier Houcharda798bf52019-03-08 18:52:00 +01004381 _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01004382 if (strm_sess(s)->listener->counters)
4383 _HA_ATOMIC_ADD(&strm_sess(s)->listener->counters->cli_aborts, 1);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004384 if (objt_server(s->target))
Christopher Fauletcff0f732019-12-16 16:13:44 +01004385 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.cli_aborts, 1);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004386 goto end;
4387 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004388 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004389 return;
4390 }
4391
4392 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
4393 http_msg_closed:
4394 /* drop any pending data */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004395 channel_htx_truncate(&s->req, htxbuf(&s->req.buf));
Christopher Faulet9768c262018-10-22 09:34:31 +02004396 channel_abort(&s->req);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004397 goto end;
4398 }
4399
4400 check_channel_flags:
4401 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4402 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4403 /* if we've just closed an output, let's switch */
4404 txn->rsp.msg_state = HTTP_MSG_CLOSING;
4405 goto http_msg_closing;
4406 }
4407
4408 end:
4409 chn->analysers &= AN_RES_FLT_END;
Christopher Faulet198ef8b2020-12-15 13:32:55 +01004410 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
4411 chn->flags |= CF_NEVER_WAIT;
4412 if (HAS_RSP_DATA_FILTERS(s))
4413 chn->analysers |= AN_RES_FLT_XFER_DATA;
4414 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004415 channel_auto_close(chn);
4416 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004417 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004418}
4419
Christopher Fauletef70e252020-01-28 09:26:19 +01004420/* Forward a response generated by HAProxy (error/redirect/return). This
4421 * function forwards all pending incoming data. If <final> is set to 0, nothing
4422 * more is performed. It is used for 1xx informational messages. Otherwise, the
Christopher Faulet507479b2020-05-15 12:29:46 +02004423 * transaction is terminated and the request is emptied. On success 1 is
Christopher Faulet40e6b552020-06-25 16:04:50 +02004424 * returned. If an error occurred, 0 is returned. If it fails, this function
4425 * only exits. It is the caller responsibility to do the cleanup.
Christopher Fauletef70e252020-01-28 09:26:19 +01004426 */
4427int http_forward_proxy_resp(struct stream *s, int final)
4428{
4429 struct channel *req = &s->req;
4430 struct channel *res = &s->res;
4431 struct htx *htx = htxbuf(&res->buf);
4432 size_t data;
4433
4434 if (final) {
4435 htx->flags |= HTX_FL_PROXY_RESP;
Christopher Faulet507479b2020-05-15 12:29:46 +02004436
Christopher Fauletaab1b672020-11-18 16:44:02 +01004437 if (!htx_is_empty(htx) && !http_eval_after_res_rules(s))
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01004438 return 0;
Christopher Fauletef70e252020-01-28 09:26:19 +01004439
Christopher Fauletd6c48362020-10-19 18:01:38 +02004440 if (s->txn->meth == HTTP_METH_HEAD)
4441 htx_skip_msg_payload(htx);
4442
Christopher Fauletef70e252020-01-28 09:26:19 +01004443 channel_auto_read(req);
4444 channel_abort(req);
4445 channel_auto_close(req);
4446 channel_htx_erase(req, htxbuf(&req->buf));
4447
4448 res->wex = tick_add_ifset(now_ms, res->wto);
4449 channel_auto_read(res);
4450 channel_auto_close(res);
4451 channel_shutr_now(res);
Christopher Faulet1a9db7c2020-06-25 15:36:45 +02004452 res->flags |= CF_EOI; /* The response is terminated, add EOI */
Christopher Faulet42432f32020-11-20 17:43:16 +01004453 htxbuf(&res->buf)->flags |= HTX_FL_EOM; /* no more data are expected */
Christopher Fauletef70e252020-01-28 09:26:19 +01004454 }
Christopher Fauletcf6898c2020-06-25 15:55:11 +02004455 else {
4456 /* Send ASAP informational messages. Rely on CF_EOI for final
4457 * response.
4458 */
4459 res->flags |= CF_SEND_DONTWAIT;
4460 }
Christopher Fauletef70e252020-01-28 09:26:19 +01004461
4462 data = htx->data - co_data(res);
4463 c_adv(res, data);
4464 htx->first = -1;
4465 res->total += data;
4466 return 1;
4467}
4468
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004469void http_server_error(struct stream *s, struct stream_interface *si, int err,
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004470 int finst, struct http_reply *msg)
Christopher Faulet0f226952018-10-22 09:29:56 +02004471{
Christopher Faulet72c7d8d2020-01-27 15:32:25 +01004472 http_reply_and_close(s, s->txn->status, msg);
Christopher Faulet0f226952018-10-22 09:29:56 +02004473 if (!(s->flags & SF_ERR_MASK))
4474 s->flags |= err;
4475 if (!(s->flags & SF_FINST_MASK))
4476 s->flags |= finst;
4477}
4478
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004479void http_reply_and_close(struct stream *s, short status, struct http_reply *msg)
Christopher Faulet0f226952018-10-22 09:29:56 +02004480{
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004481 if (!msg) {
4482 channel_htx_truncate(&s->res, htxbuf(&s->res.buf));
4483 goto end;
4484 }
4485
4486 if (http_reply_message(s, msg) == -1) {
4487 /* On error, return a 500 error message, but don't rewrite it if
Christopher Faulet40e6b552020-06-25 16:04:50 +02004488 * it is already an internal error. If it was already a "const"
4489 * 500 error, just fail.
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004490 */
Christopher Faulet40e6b552020-06-25 16:04:50 +02004491 if (s->txn->status == 500) {
4492 if (s->txn->flags & TX_CONST_REPLY)
4493 goto end;
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004494 s->txn->flags |= TX_CONST_REPLY;
Christopher Faulet40e6b552020-06-25 16:04:50 +02004495 }
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004496 s->txn->status = 500;
4497 s->txn->http_reply = NULL;
4498 return http_reply_and_close(s, s->txn->status, http_error_message(s));
4499 }
4500
4501end:
4502 s->res.wex = tick_add_ifset(now_ms, s->res.wto);
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004503
Christopher Faulet0f226952018-10-22 09:29:56 +02004504 channel_auto_read(&s->req);
4505 channel_abort(&s->req);
4506 channel_auto_close(&s->req);
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004507 channel_htx_erase(&s->req, htxbuf(&s->req.buf));
Christopher Faulet72c7d8d2020-01-27 15:32:25 +01004508 channel_auto_read(&s->res);
4509 channel_auto_close(&s->res);
4510 channel_shutr_now(&s->res);
Christopher Faulet0f226952018-10-22 09:29:56 +02004511}
4512
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004513struct http_reply *http_error_message(struct stream *s)
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004514{
4515 const int msgnum = http_get_status_idx(s->txn->status);
4516
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004517 if (s->txn->http_reply)
4518 return s->txn->http_reply;
4519 else if (s->be->replies[msgnum])
4520 return s->be->replies[msgnum];
4521 else if (strm_fe(s)->replies[msgnum])
4522 return strm_fe(s)->replies[msgnum];
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004523 else
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004524 return &http_err_replies[msgnum];
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004525}
4526
Christopher Faulet40e6b552020-06-25 16:04:50 +02004527/* Produces an HTX message from an http reply. Depending on the http reply type,
4528 * a, errorfile, an raw file or a log-format string is used. On success, it
4529 * returns 0. If an error occurs -1 is returned. If it fails, this function only
4530 * exits. It is the caller responsibility to do the cleanup.
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004531 */
Christopher Fauletae43b6c2020-05-27 15:24:22 +02004532int http_reply_to_htx(struct stream *s, struct htx *htx, struct http_reply *reply)
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004533{
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004534 struct buffer *errmsg;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004535 struct htx_sl *sl;
4536 struct buffer *body = NULL;
4537 const char *status, *reason, *clen, *ctype;
4538 unsigned int slflags;
4539 int ret = 0;
4540
Christopher Faulete29a97e2020-05-14 14:49:25 +02004541 /*
4542 * - HTTP_REPLY_ERRFILES unexpected here. handled as no payload if so
4543 *
4544 * - HTTP_REPLY_INDIRECT: switch on another reply if defined or handled
4545 * as no payload if NULL. the TXN status code is set with the status
4546 * of the original reply.
4547 */
4548
4549 if (reply->type == HTTP_REPLY_INDIRECT) {
4550 if (reply->body.reply)
4551 reply = reply->body.reply;
4552 }
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004553 if (reply->type == HTTP_REPLY_ERRMSG && !reply->body.errmsg) {
4554 /* get default error message */
4555 if (reply == s->txn->http_reply)
4556 s->txn->http_reply = NULL;
4557 reply = http_error_message(s);
4558 if (reply->type == HTTP_REPLY_INDIRECT) {
4559 if (reply->body.reply)
4560 reply = reply->body.reply;
4561 }
4562 }
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004563
4564 if (reply->type == HTTP_REPLY_ERRMSG) {
4565 /* implicit or explicit error message*/
4566 errmsg = reply->body.errmsg;
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004567 if (errmsg && !b_is_null(errmsg)) {
Christopher Faulet20567362020-05-15 14:52:49 +02004568 if (!htx_copy_msg(htx, errmsg))
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004569 goto fail;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004570 }
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004571 }
4572 else {
4573 /* no payload, file or log-format string */
4574 if (reply->type == HTTP_REPLY_RAW) {
4575 /* file */
4576 body = &reply->body.obj;
4577 }
4578 else if (reply->type == HTTP_REPLY_LOGFMT) {
4579 /* log-format string */
4580 body = alloc_trash_chunk();
4581 if (!body)
4582 goto fail_alloc;
4583 body->data = build_logline(s, body->area, body->size, &reply->body.fmt);
4584 }
4585 /* else no payload */
4586
4587 status = ultoa(reply->status);
4588 reason = http_get_reason(reply->status);
4589 slflags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_CLEN);
4590 if (!body || !b_data(body))
4591 slflags |= HTX_SL_F_BODYLESS;
4592 sl = htx_add_stline(htx, HTX_BLK_RES_SL, slflags, ist("HTTP/1.1"), ist(status), ist(reason));
4593 if (!sl)
4594 goto fail;
4595 sl->info.res.status = reply->status;
4596
4597 clen = (body ? ultoa(b_data(body)) : "0");
4598 ctype = reply->ctype;
4599
4600 if (!LIST_ISEMPTY(&reply->hdrs)) {
4601 struct http_reply_hdr *hdr;
4602 struct buffer *value = alloc_trash_chunk();
4603
4604 if (!value)
4605 goto fail;
4606
4607 list_for_each_entry(hdr, &reply->hdrs, list) {
4608 chunk_reset(value);
4609 value->data = build_logline(s, value->area, value->size, &hdr->value);
4610 if (b_data(value) && !htx_add_header(htx, hdr->name, ist2(b_head(value), b_data(value)))) {
4611 free_trash_chunk(value);
4612 goto fail;
4613 }
4614 chunk_reset(value);
4615 }
4616 free_trash_chunk(value);
4617 }
4618
4619 if (!htx_add_header(htx, ist("content-length"), ist(clen)) ||
4620 (body && b_data(body) && ctype && !htx_add_header(htx, ist("content-type"), ist(ctype))) ||
4621 !htx_add_endof(htx, HTX_BLK_EOH) ||
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004622 (body && b_data(body) && !htx_add_data_atonce(htx, ist2(b_head(body), b_data(body)))))
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004623 goto fail;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004624
4625 htx->flags |= HTX_FL_EOM;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004626 }
4627
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004628 leave:
4629 if (reply->type == HTTP_REPLY_LOGFMT)
4630 free_trash_chunk(body);
4631 return ret;
4632
4633 fail_alloc:
4634 if (!(s->flags & SF_ERR_MASK))
4635 s->flags |= SF_ERR_RESOURCE;
Christopher Faulet97e466c2020-05-15 15:12:47 +02004636 /* fall through */
4637 fail:
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004638 ret = -1;
4639 goto leave;
Christopher Faulet97e466c2020-05-15 15:12:47 +02004640}
4641
4642/* Send an http reply to the client. On success, it returns 0. If an error
Christopher Faulet40e6b552020-06-25 16:04:50 +02004643 * occurs -1 is returned and the response channel is truncated, removing this
4644 * way the faulty reply. This function may fail when the reply is formatted
4645 * (http_reply_to_htx) or when the reply is forwarded
4646 * (http_forward_proxy_resp). On the last case, it is because a
4647 * http-after-response rule fails.
Christopher Faulet97e466c2020-05-15 15:12:47 +02004648 */
4649int http_reply_message(struct stream *s, struct http_reply *reply)
4650{
4651 struct channel *res = &s->res;
4652 struct htx *htx = htx_from_buf(&res->buf);
4653
4654 if (s->txn->status == -1)
4655 s->txn->status = reply->status;
4656 channel_htx_truncate(res, htx);
4657
4658 if (http_reply_to_htx(s, htx, reply) == -1)
4659 goto fail;
4660
4661 htx_to_buf(htx, &s->res.buf);
4662 if (!http_forward_proxy_resp(s, 1))
4663 goto fail;
4664 return 0;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004665
4666 fail:
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004667 channel_htx_truncate(res, htx);
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004668 if (!(s->flags & SF_ERR_MASK))
4669 s->flags |= SF_ERR_PRXCOND;
Christopher Faulet97e466c2020-05-15 15:12:47 +02004670 return -1;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004671}
4672
Christopher Faulet304cc402019-07-15 15:46:28 +02004673/* Return the error message corresponding to si->err_type. It is assumed
4674 * that the server side is closed. Note that err_type is actually a
4675 * bitmask, where almost only aborts may be cumulated with other
4676 * values. We consider that aborted operations are more important
4677 * than timeouts or errors due to the fact that nobody else in the
4678 * logs might explain incomplete retries. All others should avoid
4679 * being cumulated. It should normally not be possible to have multiple
4680 * aborts at once, but just in case, the first one in sequence is reported.
4681 * Note that connection errors appearing on the second request of a keep-alive
4682 * connection are not reported since this allows the client to retry.
4683 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004684void http_return_srv_error(struct stream *s, struct stream_interface *si)
Christopher Faulet304cc402019-07-15 15:46:28 +02004685{
4686 int err_type = si->err_type;
4687
4688 /* set s->txn->status for http_error_message(s) */
4689 s->txn->status = 503;
4690
4691 if (err_type & SI_ET_QUEUE_ABRT)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004692 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_Q,
4693 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004694 else if (err_type & SI_ET_CONN_ABRT)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004695 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_C,
4696 (s->txn->flags & TX_NOT_FIRST) ? NULL :
4697 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004698 else if (err_type & SI_ET_QUEUE_TO)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004699 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_Q,
4700 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004701 else if (err_type & SI_ET_QUEUE_ERR)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004702 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_Q,
4703 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004704 else if (err_type & SI_ET_CONN_TO)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004705 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_C,
4706 (s->txn->flags & TX_NOT_FIRST) ? NULL :
4707 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004708 else if (err_type & SI_ET_CONN_ERR)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004709 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_C,
4710 (s->flags & SF_SRV_REUSED) ? NULL :
4711 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004712 else if (err_type & SI_ET_CONN_RES)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004713 http_server_error(s, si, SF_ERR_RESOURCE, SF_FINST_C,
4714 (s->txn->flags & TX_NOT_FIRST) ? NULL :
4715 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004716 else { /* SI_ET_CONN_OTHER and others */
4717 s->txn->status = 500;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004718 http_server_error(s, si, SF_ERR_INTERNAL, SF_FINST_C,
4719 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004720 }
4721}
4722
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004723
Christopher Faulet4a28a532019-03-01 11:19:40 +01004724/* Handle Expect: 100-continue for HTTP/1.1 messages if necessary. It returns 0
4725 * on success and -1 on error.
4726 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004727static int http_handle_expect_hdr(struct stream *s, struct htx *htx, struct http_msg *msg)
Christopher Faulet4a28a532019-03-01 11:19:40 +01004728{
4729 /* If we have HTTP/1.1 message with a body and Expect: 100-continue,
4730 * then we must send an HTTP/1.1 100 Continue intermediate response.
4731 */
4732 if (msg->msg_state == HTTP_MSG_BODY && (msg->flags & HTTP_MSGF_VER_11) &&
4733 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
4734 struct ist hdr = { .ptr = "Expect", .len = 6 };
4735 struct http_hdr_ctx ctx;
4736
4737 ctx.blk = NULL;
4738 /* Expect is allowed in 1.1, look for it */
4739 if (http_find_header(htx, hdr, &ctx, 0) &&
4740 unlikely(isteqi(ctx.value, ist2("100-continue", 12)))) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004741 if (http_reply_100_continue(s) == -1)
Christopher Faulet4a28a532019-03-01 11:19:40 +01004742 return -1;
4743 http_remove_header(htx, &ctx);
4744 }
4745 }
4746 return 0;
4747}
4748
Christopher Faulet23a3c792018-11-28 10:01:23 +01004749/* Send a 100-Continue response to the client. It returns 0 on success and -1
4750 * on error. The response channel is updated accordingly.
4751 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004752static int http_reply_100_continue(struct stream *s)
Christopher Faulet23a3c792018-11-28 10:01:23 +01004753{
4754 struct channel *res = &s->res;
4755 struct htx *htx = htx_from_buf(&res->buf);
4756 struct htx_sl *sl;
4757 unsigned int flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|
4758 HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
Christopher Faulet23a3c792018-11-28 10:01:23 +01004759
4760 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
4761 ist("HTTP/1.1"), ist("100"), ist("Continue"));
4762 if (!sl)
4763 goto fail;
4764 sl->info.res.status = 100;
4765
Christopher Faulet1d5ec092019-06-26 14:23:54 +02004766 if (!htx_add_endof(htx, HTX_BLK_EOH))
Christopher Faulet23a3c792018-11-28 10:01:23 +01004767 goto fail;
4768
Christopher Fauleta72a7e42020-01-28 09:28:11 +01004769 if (!http_forward_proxy_resp(s, 0))
4770 goto fail;
Christopher Faulet23a3c792018-11-28 10:01:23 +01004771 return 0;
4772
4773 fail:
4774 /* If an error occurred, remove the incomplete HTTP response from the
4775 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004776 channel_htx_truncate(res, htx);
Christopher Faulet23a3c792018-11-28 10:01:23 +01004777 return -1;
4778}
4779
Christopher Faulet12c51e22018-11-28 15:59:42 +01004780
Christopher Faulet0f226952018-10-22 09:29:56 +02004781/*
4782 * Capture headers from message <htx> according to header list <cap_hdr>, and
4783 * fill the <cap> pointers appropriately.
4784 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004785static void http_capture_headers(struct htx *htx, char **cap, struct cap_hdr *cap_hdr)
Christopher Faulet0f226952018-10-22 09:29:56 +02004786{
4787 struct cap_hdr *h;
4788 int32_t pos;
4789
Christopher Fauleta3f15502019-05-13 15:27:23 +02004790 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet0f226952018-10-22 09:29:56 +02004791 struct htx_blk *blk = htx_get_blk(htx, pos);
4792 enum htx_blk_type type = htx_get_blk_type(blk);
4793 struct ist n, v;
4794
4795 if (type == HTX_BLK_EOH)
4796 break;
4797 if (type != HTX_BLK_HDR)
4798 continue;
4799
4800 n = htx_get_blk_name(htx, blk);
4801
4802 for (h = cap_hdr; h; h = h->next) {
4803 if (h->namelen && (h->namelen == n.len) &&
4804 (strncasecmp(n.ptr, h->name, h->namelen) == 0)) {
4805 if (cap[h->index] == NULL)
4806 cap[h->index] =
4807 pool_alloc(h->pool);
4808
4809 if (cap[h->index] == NULL) {
4810 ha_alert("HTTP capture : out of memory.\n");
4811 break;
4812 }
4813
4814 v = htx_get_blk_value(htx, blk);
4815 if (v.len > h->len)
4816 v.len = h->len;
4817
4818 memcpy(cap[h->index], v.ptr, v.len);
4819 cap[h->index][v.len]=0;
4820 }
4821 }
4822 }
4823}
4824
Christopher Faulet0b6bdc52018-10-24 11:05:36 +02004825/* Delete a value in a header between delimiters <from> and <next>. The header
4826 * itself is delimited by <start> and <end> pointers. The number of characters
4827 * displaced is returned, and the pointer to the first delimiter is updated if
4828 * required. The function tries as much as possible to respect the following
4829 * principles :
4830 * - replace <from> delimiter by the <next> one unless <from> points to <start>,
4831 * in which case <next> is simply removed
4832 * - set exactly one space character after the new first delimiter, unless there
4833 * are not enough characters in the block being moved to do so.
4834 * - remove unneeded spaces before the previous delimiter and after the new
4835 * one.
4836 *
4837 * It is the caller's responsibility to ensure that :
4838 * - <from> points to a valid delimiter or <start> ;
4839 * - <next> points to a valid delimiter or <end> ;
4840 * - there are non-space chars before <from>.
4841 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004842static int http_del_hdr_value(char *start, char *end, char **from, char *next)
Christopher Faulet0b6bdc52018-10-24 11:05:36 +02004843{
4844 char *prev = *from;
4845
4846 if (prev == start) {
4847 /* We're removing the first value. eat the semicolon, if <next>
4848 * is lower than <end> */
4849 if (next < end)
4850 next++;
4851
4852 while (next < end && HTTP_IS_SPHT(*next))
4853 next++;
4854 }
4855 else {
4856 /* Remove useless spaces before the old delimiter. */
4857 while (HTTP_IS_SPHT(*(prev-1)))
4858 prev--;
4859 *from = prev;
4860
4861 /* copy the delimiter and if possible a space if we're
4862 * not at the end of the line.
4863 */
4864 if (next < end) {
4865 *prev++ = *next++;
4866 if (prev + 1 < next)
4867 *prev++ = ' ';
4868 while (next < end && HTTP_IS_SPHT(*next))
4869 next++;
4870 }
4871 }
4872 memmove(prev, next, end - next);
4873 return (prev - next);
4874}
4875
Christopher Faulet0f226952018-10-22 09:29:56 +02004876
4877/* Formats the start line of the request (without CRLF) and puts it in <str> and
Joseph Herlantc42c0e92018-11-25 10:43:27 -08004878 * return the written length. The line can be truncated if it exceeds <len>.
Christopher Faulet0f226952018-10-22 09:29:56 +02004879 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004880static size_t http_fmt_req_line(const struct htx_sl *sl, char *str, size_t len)
Christopher Faulet0f226952018-10-22 09:29:56 +02004881{
4882 struct ist dst = ist2(str, 0);
4883
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004884 if (istcat(&dst, htx_sl_req_meth(sl), len) == -1)
Christopher Faulet0f226952018-10-22 09:29:56 +02004885 goto end;
4886 if (dst.len + 1 > len)
4887 goto end;
4888 dst.ptr[dst.len++] = ' ';
4889
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004890 if (istcat(&dst, htx_sl_req_uri(sl), len) == -1)
Christopher Faulet0f226952018-10-22 09:29:56 +02004891 goto end;
4892 if (dst.len + 1 > len)
4893 goto end;
4894 dst.ptr[dst.len++] = ' ';
4895
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004896 istcat(&dst, htx_sl_req_vsn(sl), len);
Christopher Faulet0f226952018-10-22 09:29:56 +02004897 end:
4898 return dst.len;
4899}
4900
4901/*
4902 * Print a debug line with a start line.
4903 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004904static void http_debug_stline(const char *dir, struct stream *s, const struct htx_sl *sl)
Christopher Faulet0f226952018-10-22 09:29:56 +02004905{
4906 struct session *sess = strm_sess(s);
4907 int max;
4908
4909 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
4910 dir,
4911 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->handle.fd : -1,
4912 objt_cs(s->si[1].end) ? (unsigned short)objt_cs(s->si[1].end)->conn->handle.fd : -1);
4913
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004914 max = HTX_SL_P1_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02004915 UBOUND(max, trash.size - trash.data - 3);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004916 chunk_memcat(&trash, HTX_SL_P1_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02004917 trash.area[trash.data++] = ' ';
4918
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004919 max = HTX_SL_P2_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02004920 UBOUND(max, trash.size - trash.data - 2);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004921 chunk_memcat(&trash, HTX_SL_P2_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02004922 trash.area[trash.data++] = ' ';
4923
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004924 max = HTX_SL_P3_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02004925 UBOUND(max, trash.size - trash.data - 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004926 chunk_memcat(&trash, HTX_SL_P3_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02004927 trash.area[trash.data++] = '\n';
4928
Willy Tarreau2e8ab6b2020-03-14 11:03:20 +01004929 DISGUISE(write(1, trash.area, trash.data));
Christopher Faulet0f226952018-10-22 09:29:56 +02004930}
4931
4932/*
4933 * Print a debug line with a header.
4934 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004935static 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 +02004936{
4937 struct session *sess = strm_sess(s);
4938 int max;
4939
4940 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
4941 dir,
4942 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->handle.fd : -1,
4943 objt_cs(s->si[1].end) ? (unsigned short)objt_cs(s->si[1].end)->conn->handle.fd : -1);
4944
4945 max = n.len;
4946 UBOUND(max, trash.size - trash.data - 3);
4947 chunk_memcat(&trash, n.ptr, max);
4948 trash.area[trash.data++] = ':';
4949 trash.area[trash.data++] = ' ';
4950
4951 max = v.len;
4952 UBOUND(max, trash.size - trash.data - 1);
4953 chunk_memcat(&trash, v.ptr, max);
4954 trash.area[trash.data++] = '\n';
4955
Willy Tarreau2e8ab6b2020-03-14 11:03:20 +01004956 DISGUISE(write(1, trash.area, trash.data));
Christopher Faulet0f226952018-10-22 09:29:56 +02004957}
4958
Christopher Fauleta8a46e22019-07-16 14:53:09 +02004959/* Allocate a new HTTP transaction for stream <s> unless there is one already.
4960 * In case of allocation failure, everything allocated is freed and NULL is
4961 * returned. Otherwise the new transaction is assigned to the stream and
4962 * returned.
4963 */
4964struct http_txn *http_alloc_txn(struct stream *s)
4965{
4966 struct http_txn *txn = s->txn;
4967
4968 if (txn)
4969 return txn;
4970
4971 txn = pool_alloc(pool_head_http_txn);
4972 if (!txn)
4973 return txn;
4974
4975 s->txn = txn;
4976 return txn;
4977}
4978
4979void http_txn_reset_req(struct http_txn *txn)
4980{
Christopher Faulet1aea50e2020-01-17 16:03:53 +01004981 txn->req.flags = 0;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02004982 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
4983}
4984
4985void http_txn_reset_res(struct http_txn *txn)
4986{
Christopher Faulet1aea50e2020-01-17 16:03:53 +01004987 txn->rsp.flags = 0;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02004988 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
4989}
4990
4991/*
4992 * Initialize a new HTTP transaction for stream <s>. It is assumed that all
4993 * the required fields are properly allocated and that we only need to (re)init
4994 * them. This should be used before processing any new request.
4995 */
4996void http_init_txn(struct stream *s)
4997{
4998 struct http_txn *txn = s->txn;
4999 struct conn_stream *cs = objt_cs(s->si[0].end);
5000
Christopher Fauletda831fa2020-10-06 17:58:43 +02005001 txn->flags = ((cs && cs->flags & CS_FL_NOT_FIRST) ? TX_NOT_FIRST : 0);
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005002 txn->status = -1;
Christopher Faulet5cb513a2020-05-13 17:56:56 +02005003 txn->http_reply = NULL;
Willy Tarreau8b507582020-02-25 09:35:07 +01005004 write_u32(txn->cache_hash, 0);
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005005
5006 txn->cookie_first_date = 0;
5007 txn->cookie_last_date = 0;
5008
5009 txn->srv_cookie = NULL;
5010 txn->cli_cookie = NULL;
5011 txn->uri = NULL;
5012
5013 http_txn_reset_req(txn);
5014 http_txn_reset_res(txn);
5015
5016 txn->req.chn = &s->req;
5017 txn->rsp.chn = &s->res;
5018
5019 txn->auth.method = HTTP_AUTH_UNKNOWN;
5020
5021 vars_init(&s->vars_txn, SCOPE_TXN);
5022 vars_init(&s->vars_reqres, SCOPE_REQ);
5023}
5024
5025/* to be used at the end of a transaction */
5026void http_end_txn(struct stream *s)
5027{
5028 struct http_txn *txn = s->txn;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005029
5030 /* these ones will have been dynamically allocated */
5031 pool_free(pool_head_requri, txn->uri);
5032 pool_free(pool_head_capture, txn->cli_cookie);
5033 pool_free(pool_head_capture, txn->srv_cookie);
Tim Duesterhusa17e6622020-03-05 20:19:02 +01005034 pool_free(pool_head_uniqueid, s->unique_id.ptr);
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005035
Tim Duesterhusa17e6622020-03-05 20:19:02 +01005036 s->unique_id = IST_NULL;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005037 txn->uri = NULL;
5038 txn->srv_cookie = NULL;
5039 txn->cli_cookie = NULL;
5040
Christopher Faulet59399252019-11-07 14:27:52 +01005041 if (!LIST_ISEMPTY(&s->vars_txn.head))
5042 vars_prune(&s->vars_txn, s->sess, s);
5043 if (!LIST_ISEMPTY(&s->vars_reqres.head))
5044 vars_prune(&s->vars_reqres, s->sess, s);
5045}
5046
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005047
5048DECLARE_POOL(pool_head_http_txn, "http_txn", sizeof(struct http_txn));
Christopher Faulet0f226952018-10-22 09:29:56 +02005049
Christopher Fauletf4eb75d2018-10-11 15:55:07 +02005050__attribute__((constructor))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005051static void __http_protocol_init(void)
Christopher Fauletf4eb75d2018-10-11 15:55:07 +02005052{
5053}
5054
5055
5056/*
5057 * Local variables:
5058 * c-indent-level: 8
5059 * c-basic-offset: 8
5060 * End:
5061 */