blob: 82b389a5ddfb03f99d4433620ef7a187b95ec2b9 [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
Ilya Shipitsinacf84592021-02-06 22:29:08 +0500100 * handle all errors. Thus we may leave immediately.
Christopher Fauletda46a0d2021-01-21 17:32:58 +0100101 */
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);
Emeric Brun28976442020-10-07 08:50:09 +0200113 proxy_inc_fe_req_ctr(sess->listener, sess->fe);
Christopher Fauletbf7175f2021-02-10 14:58:01 +0100114 if (htx->flags & HTX_FL_PARSING_ERROR) {
115 stream_inc_http_err_ctr(s);
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200116 goto return_bad_req;
Christopher Fauletbf7175f2021-02-10 14:58:01 +0100117 }
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200118 else
119 goto return_int_err;
Willy Tarreau4236f032019-03-05 10:43:32 +0100120 }
121
Christopher Faulete0768eb2018-10-03 16:38:02 +0200122 /* we're speaking HTTP here, so let's speak HTTP to the client */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200123 s->srv_error = http_return_srv_error;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200124
Christopher Faulet9768c262018-10-22 09:34:31 +0200125 msg->msg_state = HTTP_MSG_BODY;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200126 stream_inc_http_req_ctr(s);
Emeric Brun28976442020-10-07 08:50:09 +0200127 proxy_inc_fe_req_ctr(sess->listener, sess->fe); /* one more valid request for this FE */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200128
Christopher Faulet9768c262018-10-22 09:34:31 +0200129 /* kill the pending keep-alive timeout */
Christopher Faulet9768c262018-10-22 09:34:31 +0200130 req->analyse_exp = TICK_ETERNITY;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200131
Christopher Faulet29f17582019-05-23 11:03:26 +0200132 BUG_ON(htx_get_first_type(htx) != HTX_BLK_REQ_SL);
Christopher Faulet297fbb42019-05-13 14:41:27 +0200133 sl = http_get_stline(htx);
Christopher Faulet03599112018-11-27 11:21:21 +0100134
Christopher Faulet9768c262018-10-22 09:34:31 +0200135 /* 0: we might have to print this header in debug mode */
136 if (unlikely((global.mode & MODE_DEBUG) &&
137 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
138 int32_t pos;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200139
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200140 http_debug_stline("clireq", s, sl);
Christopher Faulet9768c262018-10-22 09:34:31 +0200141
Christopher Fauleta3f15502019-05-13 15:27:23 +0200142 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet9768c262018-10-22 09:34:31 +0200143 struct htx_blk *blk = htx_get_blk(htx, pos);
144 enum htx_blk_type type = htx_get_blk_type(blk);
145
146 if (type == HTX_BLK_EOH)
147 break;
148 if (type != HTX_BLK_HDR)
149 continue;
150
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200151 http_debug_hdr("clihdr", s,
152 htx_get_blk_name(htx, blk),
153 htx_get_blk_value(htx, blk));
Christopher Faulet9768c262018-10-22 09:34:31 +0200154 }
155 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200156
157 /*
Christopher Faulet03599112018-11-27 11:21:21 +0100158 * 1: identify the method and the version. Also set HTTP flags
Christopher Faulete0768eb2018-10-03 16:38:02 +0200159 */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100160 txn->meth = sl->info.req.meth;
Christopher Faulet03599112018-11-27 11:21:21 +0100161 if (sl->flags & HTX_SL_F_VER_11)
Christopher Faulet9768c262018-10-22 09:34:31 +0200162 msg->flags |= HTTP_MSGF_VER_11;
Christopher Faulet03599112018-11-27 11:21:21 +0100163 msg->flags |= HTTP_MSGF_XFER_LEN;
Christopher Faulet2a408542020-11-20 14:22:37 +0100164 if (sl->flags & HTX_SL_F_CLEN)
165 msg->flags |= HTTP_MSGF_CNT_LEN;
166 else if (sl->flags & HTX_SL_F_CHNK)
167 msg->flags |= HTTP_MSGF_TE_CHNK;
Christopher Fauletb2db4fa2018-11-27 16:51:09 +0100168 if (sl->flags & HTX_SL_F_BODYLESS)
169 msg->flags |= HTTP_MSGF_BODYLESS;
Christopher Faulet576c3582021-01-08 15:53:01 +0100170 if (sl->flags & HTX_SL_F_CONN_UPG)
171 msg->flags |= HTTP_MSGF_CONN_UPG;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200172
173 /* we can make use of server redirect on GET and HEAD */
174 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
175 s->flags |= SF_REDIRECTABLE;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100176 else if (txn->meth == HTTP_METH_OTHER && isteqi(htx_sl_req_meth(sl), ist("PRI"))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200177 /* PRI is reserved for the HTTP/2 preface */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200178 goto return_bad_req;
179 }
180
181 /*
Christopher Faulet6072beb2020-02-18 15:34:58 +0100182 * 2: check if the URI matches the monitor_uri. We have to do this for
183 * every request which gets in, because the monitor-uri is defined by
184 * the frontend. If the monitor-uri starts with a '/', the matching is
185 * done against the request's path. Otherwise, the request's uri is
186 * used. It is a workaround to let HTTP/2 health-checks work as
187 * expected.
Christopher Faulete0768eb2018-10-03 16:38:02 +0200188 */
189 if (unlikely((sess->fe->monitor_uri_len != 0) &&
Christopher Faulet6072beb2020-02-18 15:34:58 +0100190 ((*sess->fe->monitor_uri == '/' && isteq(http_get_path(htx_sl_req_uri(sl)),
191 ist2(sess->fe->monitor_uri, sess->fe->monitor_uri_len))) ||
192 isteq(htx_sl_req_uri(sl), ist2(sess->fe->monitor_uri, sess->fe->monitor_uri_len))))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200193 /*
194 * We have found the monitor URI
195 */
196 struct acl_cond *cond;
197
198 s->flags |= SF_MONITOR;
Olivier Houcharda798bf52019-03-08 18:52:00 +0100199 _HA_ATOMIC_ADD(&sess->fe->fe_counters.intercepted_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200200
201 /* Check if we want to fail this monitor request or not */
202 list_for_each_entry(cond, &sess->fe->mon_fail_cond, list) {
203 int ret = acl_exec_cond(cond, sess->fe, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
204
205 ret = acl_pass(ret);
206 if (cond->pol == ACL_COND_UNLESS)
207 ret = !ret;
208
209 if (ret) {
210 /* we fail this request, let's return 503 service unavail */
211 txn->status = 503;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200212 if (!(s->flags & SF_ERR_MASK))
213 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
214 goto return_prx_cond;
215 }
216 }
217
Joseph Herlantc42c0e92018-11-25 10:43:27 -0800218 /* nothing to fail, let's reply normally */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200219 txn->status = 200;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200220 if (!(s->flags & SF_ERR_MASK))
221 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
222 goto return_prx_cond;
223 }
224
225 /*
226 * 3: Maybe we have to copy the original REQURI for the logs ?
227 * Note: we cannot log anymore if the request has been
228 * classified as invalid.
229 */
230 if (unlikely(s->logs.logwait & LW_REQ)) {
231 /* we have a complete HTTP request that we must log */
232 if ((txn->uri = pool_alloc(pool_head_requri)) != NULL) {
Christopher Faulet9768c262018-10-22 09:34:31 +0200233 size_t len;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200234
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200235 len = http_fmt_req_line(sl, txn->uri, global.tune.requri_len - 1);
Christopher Faulet9768c262018-10-22 09:34:31 +0200236 txn->uri[len] = 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200237
238 if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT)))
239 s->do_log(s);
240 } else {
241 ha_alert("HTTP logging : out of memory.\n");
242 }
243 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200244
Christopher Faulete0768eb2018-10-03 16:38:02 +0200245 /* if the frontend has "option http-use-proxy-header", we'll check if
246 * we have what looks like a proxied connection instead of a connection,
247 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
248 * Note that this is *not* RFC-compliant, however browsers and proxies
249 * happen to do that despite being non-standard :-(
250 * We consider that a request not beginning with either '/' or '*' is
251 * a proxied connection, which covers both "scheme://location" and
252 * CONNECT ip:port.
253 */
254 if ((sess->fe->options2 & PR_O2_USE_PXHDR) &&
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100255 *HTX_SL_REQ_UPTR(sl) != '/' && *HTX_SL_REQ_UPTR(sl) != '*')
Christopher Faulete0768eb2018-10-03 16:38:02 +0200256 txn->flags |= TX_USE_PX_CONN;
257
Christopher Faulete0768eb2018-10-03 16:38:02 +0200258 /* 5: we may need to capture headers */
259 if (unlikely((s->logs.logwait & LW_REQHDR) && s->req_cap))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200260 http_capture_headers(htx, s->req_cap, sess->fe->req_cap);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200261
Christopher Faulete0768eb2018-10-03 16:38:02 +0200262 /* we may have to wait for the request's body */
Christopher Faulet9768c262018-10-22 09:34:31 +0200263 if (s->be->options & PR_O_WREQ_BODY)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200264 req->analysers |= AN_REQ_HTTP_BODY;
265
266 /*
267 * RFC7234#4:
268 * A cache MUST write through requests with methods
269 * that are unsafe (Section 4.2.1 of [RFC7231]) to
270 * the origin server; i.e., a cache is not allowed
271 * to generate a reply to such a request before
272 * having forwarded the request and having received
273 * a corresponding response.
274 *
275 * RFC7231#4.2.1:
276 * Of the request methods defined by this
277 * specification, the GET, HEAD, OPTIONS, and TRACE
278 * methods are defined to be safe.
279 */
280 if (likely(txn->meth == HTTP_METH_GET ||
281 txn->meth == HTTP_METH_HEAD ||
282 txn->meth == HTTP_METH_OPTIONS ||
283 txn->meth == HTTP_METH_TRACE))
284 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
285
286 /* end of job, return OK */
287 req->analysers &= ~an_bit;
288 req->analyse_exp = TICK_ETERNITY;
Christopher Faulet9768c262018-10-22 09:34:31 +0200289
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100290 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200291 return 1;
292
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200293 return_int_err:
294 txn->status = 500;
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200295 if (!(s->flags & SF_ERR_MASK))
296 s->flags |= SF_ERR_INTERNAL;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100297 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
William Lallemand36119de2021-03-08 15:26:48 +0100298 if (sess->listener && sess->listener->counters)
Christopher Fauletb8a53712019-12-16 11:29:38 +0100299 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200300 goto return_prx_cond;
301
Christopher Faulete0768eb2018-10-03 16:38:02 +0200302 return_bad_req:
Christopher Faulet9768c262018-10-22 09:34:31 +0200303 txn->status = 400;
Olivier Houcharda798bf52019-03-08 18:52:00 +0100304 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
William Lallemand36119de2021-03-08 15:26:48 +0100305 if (sess->listener && sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100306 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200307 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200308
309 return_prx_cond:
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200310 http_reply_and_close(s, txn->status, http_error_message(s));
311
Christopher Faulete0768eb2018-10-03 16:38:02 +0200312 if (!(s->flags & SF_ERR_MASK))
313 s->flags |= SF_ERR_PRXCOND;
314 if (!(s->flags & SF_FINST_MASK))
315 s->flags |= SF_FINST_R;
316
317 req->analysers &= AN_REQ_FLT_END;
318 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100319 DBG_TRACE_DEVEL("leaving on error",
320 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200321 return 0;
322}
323
324
325/* This stream analyser runs all HTTP request processing which is common to
326 * frontends and backends, which means blocking ACLs, filters, connection-close,
327 * reqadd, stats and redirects. This is performed for the designated proxy.
328 * It returns 1 if the processing can continue on next analysers, or zero if it
329 * either needs more data or wants to immediately abort the request (eg: deny,
330 * error, ...).
331 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200332int http_process_req_common(struct stream *s, struct channel *req, int an_bit, struct proxy *px)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200333{
334 struct session *sess = s->sess;
335 struct http_txn *txn = s->txn;
336 struct http_msg *msg = &txn->req;
Christopher Fauletff2759f2018-10-24 11:13:16 +0200337 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200338 struct redirect_rule *rule;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200339 enum rule_result verdict;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200340 struct connection *conn = objt_conn(sess->origin);
341
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100342 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200343
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100344 htx = htxbuf(&req->buf);
Christopher Fauletff2759f2018-10-24 11:13:16 +0200345
Christopher Faulet1907ccc2019-04-29 13:12:02 +0200346 /* just in case we have some per-backend tracking. Only called the first
347 * execution of the analyser. */
348 if (!s->current_rule || s->current_rule_list != &px->http_req_rules)
349 stream_inc_be_http_req_ctr(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200350
351 /* evaluate http-request rules */
352 if (!LIST_ISEMPTY(&px->http_req_rules)) {
Christopher Fauletb58f62b2020-01-13 16:40:13 +0100353 verdict = http_req_get_intercept_rule(px, &px->http_req_rules, s);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200354
355 switch (verdict) {
356 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
357 goto return_prx_yield;
358
359 case HTTP_RULE_RES_CONT:
360 case HTTP_RULE_RES_STOP: /* nothing to do */
361 break;
362
363 case HTTP_RULE_RES_DENY: /* deny or tarpit */
364 if (txn->flags & TX_CLTARPIT)
365 goto tarpit;
366 goto deny;
367
368 case HTTP_RULE_RES_ABRT: /* abort request, response already sent. Eg: auth */
369 goto return_prx_cond;
370
371 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
372 goto done;
373
374 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
375 goto return_bad_req;
Christopher Faulet3a26bee2019-12-16 12:47:40 +0100376
377 case HTTP_RULE_RES_ERROR: /* failed with a bad request */
378 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200379 }
380 }
381
382 if (conn && (conn->flags & CO_FL_EARLY_DATA) &&
Olivier Houchard220a26c2020-01-23 14:57:36 +0100383 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_SSL_WAIT_HS))) {
Christopher Fauletff2759f2018-10-24 11:13:16 +0200384 struct http_hdr_ctx ctx;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200385
Christopher Fauletff2759f2018-10-24 11:13:16 +0200386 ctx.blk = NULL;
387 if (!http_find_header(htx, ist("Early-Data"), &ctx, 0)) {
388 if (unlikely(!http_add_header(htx, ist("Early-Data"), ist("1"))))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100389 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200390 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200391 }
392
393 /* OK at this stage, we know that the request was accepted according to
394 * the http-request rules, we can check for the stats. Note that the
395 * URI is detected *before* the req* rules in order not to be affected
396 * by a possible reqrep, while they are processed *after* so that a
397 * reqdeny can still block them. This clearly needs to change in 1.6!
398 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200399 if (!s->target && http_stats_check_uri(s, txn, px)) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200400 s->target = &http_stats_applet.obj_type;
Willy Tarreau14bfe9a2018-12-19 15:19:27 +0100401 if (unlikely(!si_register_handler(&s->si[1], objt_applet(s->target)))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200402 s->logs.tv_request = now;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200403 if (!(s->flags & SF_ERR_MASK))
404 s->flags |= SF_ERR_RESOURCE;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100405 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200406 }
407
408 /* parse the whole stats request and extract the relevant information */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200409 http_handle_stats(s, req);
Christopher Fauletb58f62b2020-01-13 16:40:13 +0100410 verdict = http_req_get_intercept_rule(px, &px->uri_auth->http_req_rules, s);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200411 /* not all actions implemented: deny, allow, auth */
412
413 if (verdict == HTTP_RULE_RES_DENY) /* stats http-request deny */
414 goto deny;
415
416 if (verdict == HTTP_RULE_RES_ABRT) /* stats auth / stats http-request auth */
417 goto return_prx_cond;
Christopher Faulet3a26bee2019-12-16 12:47:40 +0100418
419 if (verdict == HTTP_RULE_RES_BADREQ) /* failed with a bad request */
420 goto return_bad_req;
421
422 if (verdict == HTTP_RULE_RES_ERROR) /* failed with a bad request */
423 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200424 }
425
Christopher Faulet2571bc62019-03-01 11:44:26 +0100426 /* Proceed with the applets now. */
427 if (unlikely(objt_applet(s->target))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200428 if (sess->fe == s->be) /* report it if the request was intercepted by the frontend */
Olivier Houcharda798bf52019-03-08 18:52:00 +0100429 _HA_ATOMIC_ADD(&sess->fe->fe_counters.intercepted_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200430
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200431 if (http_handle_expect_hdr(s, htx, msg) == -1)
Christopher Fauletb8a53712019-12-16 11:29:38 +0100432 goto return_int_err;
Christopher Fauletbcf242a2019-03-01 11:36:26 +0100433
Christopher Faulete0768eb2018-10-03 16:38:02 +0200434 if (!(s->flags & SF_ERR_MASK)) // this is not really an error but it is
435 s->flags |= SF_ERR_LOCAL; // to mark that it comes from the proxy
436 if (!(s->flags & SF_FINST_MASK))
437 s->flags |= SF_FINST_R;
438
439 /* enable the minimally required analyzers to handle keep-alive and compression on the HTTP response */
440 req->analysers &= (AN_REQ_HTTP_BODY | AN_REQ_FLT_HTTP_HDRS | AN_REQ_FLT_END);
441 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
442 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Christopher Fauletbcf242a2019-03-01 11:36:26 +0100443
444 req->flags |= CF_SEND_DONTWAIT;
445 s->flags |= SF_ASSIGNED;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200446 goto done;
447 }
448
449 /* check whether we have some ACLs set to redirect this request */
450 list_for_each_entry(rule, &px->redirect_rules, list) {
451 if (rule->cond) {
452 int ret;
453
454 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
455 ret = acl_pass(ret);
456 if (rule->cond->pol == ACL_COND_UNLESS)
457 ret = !ret;
458 if (!ret)
459 continue;
460 }
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200461 if (!http_apply_redirect_rule(rule, s, txn))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100462 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200463 goto done;
464 }
465
466 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
467 * If this happens, then the data will not come immediately, so we must
468 * send all what we have without waiting. Note that due to the small gain
469 * in waiting for the body of the request, it's easier to simply put the
470 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
471 * itself once used.
472 */
473 req->flags |= CF_SEND_DONTWAIT;
474
475 done: /* done with this analyser, continue with next ones that the calling
476 * points will have set, if any.
477 */
478 req->analyse_exp = TICK_ETERNITY;
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +0500479 done_without_exp: /* done with this analyser, but don't reset the analyse_exp. */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200480 req->analysers &= ~an_bit;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100481 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200482 return 1;
483
484 tarpit:
485 /* Allow cookie logging
486 */
487 if (s->be->cookie_name || sess->fe->capture_name)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200488 http_manage_client_side_cookies(s, req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200489
490 /* When a connection is tarpitted, we use the tarpit timeout,
491 * which may be the same as the connect timeout if unspecified.
492 * If unset, then set it to zero because we really want it to
493 * eventually expire. We build the tarpit as an analyser.
494 */
Christopher Faulet202c6ce2019-01-07 14:57:35 +0100495 channel_htx_erase(&s->req, htx);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200496
497 /* wipe the request out so that we can drop the connection early
498 * if the client closes first.
499 */
500 channel_dont_connect(req);
501
Christopher Faulete0768eb2018-10-03 16:38:02 +0200502 req->analysers &= AN_REQ_FLT_END; /* remove switching rules etc... */
503 req->analysers |= AN_REQ_HTTP_TARPIT;
504 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
505 if (!req->analyse_exp)
506 req->analyse_exp = tick_add(now_ms, 0);
507 stream_inc_http_err_ctr(s);
Olivier Houcharda798bf52019-03-08 18:52:00 +0100508 _HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100509 if (s->flags & SF_BE_ASSIGNED)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100510 _HA_ATOMIC_ADD(&s->be->be_counters.denied_req, 1);
William Lallemand36119de2021-03-08 15:26:48 +0100511 if (sess->listener && sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100512 _HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200513 goto done_without_exp;
514
515 deny: /* this request was blocked (denied) */
516
517 /* Allow cookie logging
518 */
519 if (s->be->cookie_name || sess->fe->capture_name)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200520 http_manage_client_side_cookies(s, req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200521
Christopher Faulete0768eb2018-10-03 16:38:02 +0200522 s->logs.tv_request = now;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200523 stream_inc_http_err_ctr(s);
Olivier Houcharda798bf52019-03-08 18:52:00 +0100524 _HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100525 if (s->flags & SF_BE_ASSIGNED)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100526 _HA_ATOMIC_ADD(&s->be->be_counters.denied_req, 1);
William Lallemand36119de2021-03-08 15:26:48 +0100527 if (sess->listener && sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100528 _HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100529 goto return_prx_err;
530
531 return_int_err:
532 txn->status = 500;
533 if (!(s->flags & SF_ERR_MASK))
534 s->flags |= SF_ERR_INTERNAL;
535 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100536 if (s->flags & SF_BE_ASSIGNED)
537 _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1);
William Lallemand36119de2021-03-08 15:26:48 +0100538 if (sess->listener && sess->listener->counters)
Christopher Fauletb8a53712019-12-16 11:29:38 +0100539 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
540 goto return_prx_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200541
542 return_bad_req:
Christopher Faulete0768eb2018-10-03 16:38:02 +0200543 txn->status = 400;
Olivier Houcharda798bf52019-03-08 18:52:00 +0100544 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
William Lallemand36119de2021-03-08 15:26:48 +0100545 if (sess->listener && sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100546 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100547 /* fall through */
548
549 return_prx_err:
550 http_reply_and_close(s, txn->status, http_error_message(s));
551 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200552
553 return_prx_cond:
554 if (!(s->flags & SF_ERR_MASK))
555 s->flags |= SF_ERR_PRXCOND;
556 if (!(s->flags & SF_FINST_MASK))
557 s->flags |= SF_FINST_R;
558
559 req->analysers &= AN_REQ_FLT_END;
560 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100561 DBG_TRACE_DEVEL("leaving on error",
562 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200563 return 0;
564
565 return_prx_yield:
566 channel_dont_connect(req);
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100567 DBG_TRACE_DEVEL("waiting for more data",
568 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200569 return 0;
570}
571
572/* This function performs all the processing enabled for the current request.
573 * It returns 1 if the processing can continue on next analysers, or zero if it
574 * needs more data, encounters an error, or wants to immediately abort the
575 * request. It relies on buffers flags, and updates s->req.analysers.
576 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200577int http_process_request(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200578{
579 struct session *sess = s->sess;
580 struct http_txn *txn = s->txn;
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200581 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200582 struct connection *cli_conn = objt_conn(strm_sess(s)->origin);
583
Christopher Faulet8bebd2f2020-10-06 17:54:56 +0200584 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200585
586 /*
587 * Right now, we know that we have processed the entire headers
588 * and that unwanted requests have been filtered out. We can do
589 * whatever we want with the remaining request. Also, now we
590 * may have separate values for ->fe, ->be.
591 */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100592 htx = htxbuf(&req->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200593
594 /*
595 * If HTTP PROXY is set we simply get remote server address parsing
Willy Tarreau1c8d32b2019-07-18 15:47:45 +0200596 * incoming request.
Christopher Faulete0768eb2018-10-03 16:38:02 +0200597 */
598 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SF_ADDR_SET)) {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100599 struct htx_sl *sl;
600 struct ist uri, path;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200601
Willy Tarreau9b7587a2020-10-15 07:32:10 +0200602 if (!sockaddr_alloc(&s->target_addr, NULL, 0)) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200603 if (!(s->flags & SF_ERR_MASK))
604 s->flags |= SF_ERR_RESOURCE;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100605 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200606 }
Christopher Faulet297fbb42019-05-13 14:41:27 +0200607 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100608 uri = htx_sl_req_uri(sl);
609 path = http_get_path(uri);
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200610
Willy Tarreau1c8d32b2019-07-18 15:47:45 +0200611 if (url2sa(uri.ptr, uri.len - path.len, s->target_addr, NULL) == -1)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200612 goto return_bad_req;
613
Willy Tarreau1c8d32b2019-07-18 15:47:45 +0200614 s->target = &s->be->obj_type;
615 s->flags |= SF_ADDR_SET | SF_ASSIGNED;
616
Christopher Faulete0768eb2018-10-03 16:38:02 +0200617 /* if the path was found, we have to remove everything between
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200618 * uri.ptr and path.ptr (excluded). If it was not found, we need
619 * to replace from all the uri by a single "/".
620 *
Ilya Shipitsin46a030c2020-07-05 16:36:08 +0500621 * Instead of rewriting the whole start line, we just update
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100622 * the star-line URI. Some space will be lost but it should be
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200623 * insignificant.
Christopher Faulete0768eb2018-10-03 16:38:02 +0200624 */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100625 istcpy(&uri, (path.len ? path : ist("/")), uri.len);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200626 }
627
628 /*
629 * 7: Now we can work with the cookies.
630 * Note that doing so might move headers in the request, but
631 * the fields will stay coherent and the URI will not move.
632 * This should only be performed in the backend.
633 */
634 if (s->be->cookie_name || sess->fe->capture_name)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200635 http_manage_client_side_cookies(s, req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200636
Tim Duesterhus2825b4b2020-02-28 15:13:34 +0100637 /* 8: Generate unique ID if a "unique-id-format" is defined.
638 *
639 * A unique ID is generated even when it is not sent to ensure that the ID can make use of
640 * fetches only available in the HTTP request processing stage.
641 */
642 if (!LIST_ISEMPTY(&sess->fe->format_unique_id)) {
Tim Duesterhusa17e6622020-03-05 20:19:02 +0100643 struct ist unique_id = stream_generate_unique_id(s, &sess->fe->format_unique_id);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200644
Tim Duesterhusa17e6622020-03-05 20:19:02 +0100645 if (!isttest(unique_id)) {
Christopher Fauletb8a53712019-12-16 11:29:38 +0100646 if (!(s->flags & SF_ERR_MASK))
647 s->flags |= SF_ERR_RESOURCE;
648 goto return_int_err;
649 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200650
Tim Duesterhus2825b4b2020-02-28 15:13:34 +0100651 /* send unique ID if a "unique-id-header" is defined */
Tim Duesterhus0643b0e2020-03-05 17:56:35 +0100652 if (isttest(sess->fe->header_unique_id) &&
Tim Duesterhusa17e6622020-03-05 20:19:02 +0100653 unlikely(!http_add_header(htx, sess->fe->header_unique_id, s->unique_id)))
Tim Duesterhus2825b4b2020-02-28 15:13:34 +0100654 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200655 }
656
657 /*
658 * 9: add X-Forwarded-For if either the frontend or the backend
659 * asks for it.
660 */
661 if ((sess->fe->options | s->be->options) & PR_O_FWDFOR) {
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200662 struct http_hdr_ctx ctx = { .blk = NULL };
663 struct ist hdr = ist2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : sess->fe->fwdfor_hdr_name,
664 s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : sess->fe->fwdfor_hdr_len);
665
Christopher Faulete0768eb2018-10-03 16:38:02 +0200666 if (!((sess->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200667 http_find_header(htx, hdr, &ctx, 0)) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200668 /* The header is set to be added only if none is present
669 * and we found it, so don't do anything.
670 */
671 }
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200672 else if (cli_conn && conn_get_src(cli_conn) && cli_conn->src->ss_family == AF_INET) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200673 /* Add an X-Forwarded-For header unless the source IP is
674 * in the 'except' network range.
675 */
Christopher Faulet5d1def62021-02-26 09:19:15 +0100676 if (ipcmp2net(cli_conn->src, &sess->fe->except_xff_net) &&
677 ipcmp2net(cli_conn->src, &s->be->except_xff_net)) {
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200678 unsigned char *pn = (unsigned char *)&((struct sockaddr_in *)cli_conn->src)->sin_addr;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200679
680 /* Note: we rely on the backend to get the header name to be used for
681 * x-forwarded-for, because the header is really meant for the backends.
682 * However, if the backend did not specify any option, we have to rely
683 * on the frontend's header name.
684 */
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200685 chunk_printf(&trash, "%d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
686 if (unlikely(!http_add_header(htx, hdr, ist2(trash.area, trash.data))))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100687 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200688 }
689 }
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200690 else if (cli_conn && conn_get_src(cli_conn) && cli_conn->src->ss_family == AF_INET6) {
Christopher Faulet5d1def62021-02-26 09:19:15 +0100691 /* Add an X-Forwarded-For header unless the source IP is
692 * in the 'except' network range.
Christopher Faulete0768eb2018-10-03 16:38:02 +0200693 */
Christopher Faulet5d1def62021-02-26 09:19:15 +0100694 if (ipcmp2net(cli_conn->src, &sess->fe->except_xff_net) &&
695 ipcmp2net(cli_conn->src, &s->be->except_xff_net)) {
696 char pn[INET6_ADDRSTRLEN];
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200697
Christopher Faulet5d1def62021-02-26 09:19:15 +0100698 inet_ntop(AF_INET6,
699 (const void *)&((struct sockaddr_in6 *)(cli_conn->src))->sin6_addr,
700 pn, sizeof(pn));
Christopher Faulete0768eb2018-10-03 16:38:02 +0200701
Christopher Faulet5d1def62021-02-26 09:19:15 +0100702 /* Note: we rely on the backend to get the header name to be used for
703 * x-forwarded-for, because the header is really meant for the backends.
704 * However, if the backend did not specify any option, we have to rely
705 * on the frontend's header name.
706 */
707 chunk_printf(&trash, "%s", pn);
708 if (unlikely(!http_add_header(htx, hdr, ist2(trash.area, trash.data))))
709 goto return_int_err;
710 }
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) {
Christopher Faulet5d1def62021-02-26 09:19:15 +0100719 struct ist hdr = ist2(s->be->orgto_hdr_len ? s->be->orgto_hdr_name : sess->fe->orgto_hdr_name,
720 s->be->orgto_hdr_len ? s->be->orgto_hdr_len : sess->fe->orgto_hdr_len);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200721
Christopher Fauletcccded92021-02-26 12:45:56 +0100722 if (cli_conn && conn_get_dst(cli_conn) && cli_conn->dst->ss_family == AF_INET) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200723 /* Add an X-Original-To header unless the destination IP is
724 * in the 'except' network range.
725 */
Christopher Faulet5d1def62021-02-26 09:19:15 +0100726 if (ipcmp2net(cli_conn->dst, &sess->fe->except_xot_net) &&
727 ipcmp2net(cli_conn->dst, &s->be->except_xot_net)) {
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200728 unsigned char *pn = (unsigned char *)&((struct sockaddr_in *)cli_conn->dst)->sin_addr;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200729
730 /* Note: we rely on the backend to get the header name to be used for
731 * x-original-to, because the header is really meant for the backends.
732 * However, if the backend did not specify any option, we have to rely
733 * on the frontend's header name.
734 */
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200735 chunk_printf(&trash, "%d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
736 if (unlikely(!http_add_header(htx, hdr, ist2(trash.area, trash.data))))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100737 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200738 }
739 }
Christopher Faulet5d1def62021-02-26 09:19:15 +0100740 else if (cli_conn && conn_get_dst(cli_conn) && cli_conn->dst->ss_family == AF_INET6) {
741 /* Add an X-Original-To header unless the source IP is
742 * in the 'except' network range.
743 */
744 if (ipcmp2net(cli_conn->dst, &sess->fe->except_xot_net) &&
745 ipcmp2net(cli_conn->dst, &s->be->except_xot_net)) {
746 char pn[INET6_ADDRSTRLEN];
747
748 inet_ntop(AF_INET6,
749 (const void *)&((struct sockaddr_in6 *)(cli_conn->dst))->sin6_addr,
750 pn, sizeof(pn));
751
752 /* Note: we rely on the backend to get the header name to be used for
753 * x-forwarded-for, because the header is really meant for the backends.
754 * However, if the backend did not specify any option, we have to rely
755 * on the frontend's header name.
756 */
757 chunk_printf(&trash, "%s", pn);
758 if (unlikely(!http_add_header(htx, hdr, ist2(trash.area, trash.data))))
759 goto return_int_err;
760 }
761 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200762 }
763
Christopher Faulete0768eb2018-10-03 16:38:02 +0200764 /* If we have no server assigned yet and we're balancing on url_param
765 * with a POST request, we may be interested in checking the body for
766 * that parameter. This will be done in another analyser.
767 */
768 if (!(s->flags & (SF_ASSIGNED|SF_DIRECT)) &&
Willy Tarreau089eaa02019-01-14 15:17:46 +0100769 s->txn->meth == HTTP_METH_POST &&
770 (s->be->lbprm.algo & BE_LB_ALGO) == BE_LB_ALGO_PH) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200771 channel_dont_connect(req);
772 req->analysers |= AN_REQ_HTTP_BODY;
773 }
774
775 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
776 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau1a18b542018-12-11 16:37:42 +0100777
Christopher Faulete0768eb2018-10-03 16:38:02 +0200778 /* We expect some data from the client. Unless we know for sure
779 * we already have a full request, we have to re-enable quick-ack
780 * in case we previously disabled it, otherwise we might cause
781 * the client to delay further data.
782 */
William Lallemand36119de2021-03-08 15:26:48 +0100783 if ((sess->listener && (sess->listener->options & LI_O_NOQUICKACK)) && !(htx->flags & HTX_FL_EOM))
Willy Tarreau1a18b542018-12-11 16:37:42 +0100784 conn_set_quickack(cli_conn, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200785
786 /*************************************************************
787 * OK, that's finished for the headers. We have done what we *
788 * could. Let's switch to the DATA state. *
789 ************************************************************/
790 req->analyse_exp = TICK_ETERNITY;
791 req->analysers &= ~an_bit;
792
793 s->logs.tv_request = now;
794 /* OK let's go on with the BODY now */
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100795 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200796 return 1;
797
Christopher Fauletb8a53712019-12-16 11:29:38 +0100798 return_int_err:
799 txn->status = 500;
800 if (!(s->flags & SF_ERR_MASK))
801 s->flags |= SF_ERR_INTERNAL;
802 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100803 if (s->flags & SF_BE_ASSIGNED)
Christopher Fauletbe20cf32020-01-24 11:41:38 +0100804 _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1);
William Lallemand36119de2021-03-08 15:26:48 +0100805 if (sess->listener && sess->listener->counters)
Christopher Fauletb8a53712019-12-16 11:29:38 +0100806 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
807 goto return_prx_cond;
808
Christopher Faulete0768eb2018-10-03 16:38:02 +0200809 return_bad_req: /* let's centralize all bad requests */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200810 txn->status = 400;
Olivier Houcharda798bf52019-03-08 18:52:00 +0100811 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
William Lallemand36119de2021-03-08 15:26:48 +0100812 if (sess->listener && sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100813 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100814 /* fall through */
815
816 return_prx_cond:
817 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +0200818
819 if (!(s->flags & SF_ERR_MASK))
820 s->flags |= SF_ERR_PRXCOND;
821 if (!(s->flags & SF_FINST_MASK))
822 s->flags |= SF_FINST_R;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100823
824 req->analysers &= AN_REQ_FLT_END;
825 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100826 DBG_TRACE_DEVEL("leaving on error",
827 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200828 return 0;
829}
830
831/* This function is an analyser which processes the HTTP tarpit. It always
832 * returns zero, at the beginning because it prevents any other processing
833 * from occurring, and at the end because it terminates the request.
834 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200835int http_process_tarpit(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200836{
837 struct http_txn *txn = s->txn;
838
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100839 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, &txn->req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200840 /* This connection is being tarpitted. The CLIENT side has
841 * already set the connect expiration date to the right
842 * timeout. We just have to check that the client is still
843 * there and that the timeout has not expired.
844 */
845 channel_dont_connect(req);
846 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100847 !tick_is_expired(req->analyse_exp, now_ms)) {
848 DBG_TRACE_DEVEL("waiting for tarpit timeout expiry",
849 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200850 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100851 }
852
Christopher Faulete0768eb2018-10-03 16:38:02 +0200853
854 /* We will set the queue timer to the time spent, just for
855 * logging purposes. We fake a 500 server error, so that the
856 * attacker will not suspect his connection has been tarpitted.
857 * It will not cause trouble to the logs because we can exclude
858 * the tarpitted connections by filtering on the 'PT' status flags.
859 */
860 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
861
Christopher Faulet8dfeccf2020-05-15 14:16:29 +0200862 http_reply_and_close(s, txn->status, (!(req->flags & CF_READ_ERROR) ? http_error_message(s) : NULL));
Christopher Faulet5cb513a2020-05-13 17:56:56 +0200863
864 end:
Christopher Faulete0768eb2018-10-03 16:38:02 +0200865 req->analysers &= AN_REQ_FLT_END;
866 req->analyse_exp = TICK_ETERNITY;
867
868 if (!(s->flags & SF_ERR_MASK))
869 s->flags |= SF_ERR_PRXCOND;
870 if (!(s->flags & SF_FINST_MASK))
871 s->flags |= SF_FINST_T;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100872
873 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200874 return 0;
875}
876
877/* This function is an analyser which waits for the HTTP request body. It waits
878 * for either the buffer to be full, or the full advertised contents to have
879 * reached the buffer. It must only be called after the standard HTTP request
880 * processing has occurred, because it expects the request to be parsed and will
881 * look for the Expect header. It may send a 100-Continue interim response. It
882 * takes in input any state starting from HTTP_MSG_BODY and leaves with one of
883 * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it
884 * needs to read more data, or 1 once it has completed its analysis.
885 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200886int http_wait_for_request_body(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200887{
888 struct session *sess = s->sess;
889 struct http_txn *txn = s->txn;
890 struct http_msg *msg = &s->txn->req;
Christopher Fauletf76ebe82018-10-24 11:16:22 +0200891 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200892
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100893 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Fauletf76ebe82018-10-24 11:16:22 +0200894
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100895 htx = htxbuf(&req->buf);
Christopher Fauletf76ebe82018-10-24 11:16:22 +0200896
Willy Tarreau4236f032019-03-05 10:43:32 +0100897 if (htx->flags & HTX_FL_PARSING_ERROR)
898 goto return_bad_req;
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200899 if (htx->flags & HTX_FL_PROCESSING_ERROR)
900 goto return_int_err;
Willy Tarreau4236f032019-03-05 10:43:32 +0100901
Christopher Fauletc31bc722020-11-20 14:30:38 +0100902 /* Do nothing for bodyless and CONNECT requests */
903 if (txn->meth == HTTP_METH_CONNECT || (msg->flags & HTTP_MSGF_BODYLESS))
Christopher Faulet63c69a92020-11-16 16:03:35 +0100904 goto http_end;
905
Christopher Faulete0768eb2018-10-03 16:38:02 +0200906 /* We have to parse the HTTP request body to find any required data.
907 * "balance url_param check_post" should have been the only way to get
908 * into this. We were brought here after HTTP header analysis, so all
909 * related structures are ready.
910 */
911
Christopher Fauletf76ebe82018-10-24 11:16:22 +0200912 if (msg->msg_state < HTTP_MSG_DATA) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200913 if (http_handle_expect_hdr(s, htx, msg) == -1)
Christopher Fauletb8a53712019-12-16 11:29:38 +0100914 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200915 }
916
Christopher Fauletf76ebe82018-10-24 11:16:22 +0200917 msg->msg_state = HTTP_MSG_DATA;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200918
Christopher Fauletf76ebe82018-10-24 11:16:22 +0200919 /* Now we're in HTTP_MSG_DATA. We just need to know if all data have
920 * been received or if the buffer is full.
Christopher Faulete0768eb2018-10-03 16:38:02 +0200921 */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +0100922 if ((htx->flags & HTX_FL_EOM) || htx_get_tail_type(htx) > HTX_BLK_DATA ||
Christopher Fauletdcd8c5e2019-01-21 11:24:38 +0100923 channel_htx_full(req, htx, global.tune.maxrewrite))
Christopher Faulete0768eb2018-10-03 16:38:02 +0200924 goto http_end;
925
926 if ((req->flags & CF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
927 txn->status = 408;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200928 if (!(s->flags & SF_ERR_MASK))
929 s->flags |= SF_ERR_CLITO;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100930 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
William Lallemand36119de2021-03-08 15:26:48 +0100931 if (sess->listener && sess->listener->counters)
Christopher Fauletb8a53712019-12-16 11:29:38 +0100932 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
933 goto return_prx_cond;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200934 }
935
936 /* we get here if we need to wait for more data */
937 if (!(req->flags & (CF_SHUTR | CF_READ_ERROR))) {
938 /* Not enough data. We'll re-use the http-request
939 * timeout here. Ideally, we should set the timeout
940 * relative to the accept() date. We just set the
941 * request timeout once at the beginning of the
942 * request.
943 */
944 channel_dont_connect(req);
945 if (!tick_isset(req->analyse_exp))
946 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100947 DBG_TRACE_DEVEL("waiting for more data",
948 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200949 return 0;
950 }
951
952 http_end:
953 /* The situation will not evolve, so let's give up on the analysis. */
954 s->logs.tv_request = now; /* update the request timer to reflect full request */
955 req->analysers &= ~an_bit;
956 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100957 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200958 return 1;
959
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200960 return_int_err:
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200961 txn->status = 500;
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200962 if (!(s->flags & SF_ERR_MASK))
963 s->flags |= SF_ERR_INTERNAL;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100964 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100965 if (s->flags & SF_BE_ASSIGNED)
Christopher Fauletbe20cf32020-01-24 11:41:38 +0100966 _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1);
William Lallemand36119de2021-03-08 15:26:48 +0100967 if (sess->listener && sess->listener->counters)
Christopher Fauletb8a53712019-12-16 11:29:38 +0100968 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
969 goto return_prx_cond;
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200970
Christopher Faulete0768eb2018-10-03 16:38:02 +0200971 return_bad_req: /* let's centralize all bad requests */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200972 txn->status = 400;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100973 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
William Lallemand36119de2021-03-08 15:26:48 +0100974 if (sess->listener && sess->listener->counters)
Christopher Fauletb8a53712019-12-16 11:29:38 +0100975 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
976 /* fall through */
977
978 return_prx_cond:
979 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +0200980
981 if (!(s->flags & SF_ERR_MASK))
982 s->flags |= SF_ERR_PRXCOND;
983 if (!(s->flags & SF_FINST_MASK))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100984 s->flags |= (msg->msg_state < HTTP_MSG_DATA ? SF_FINST_R : SF_FINST_D);
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200985
Christopher Faulete0768eb2018-10-03 16:38:02 +0200986 req->analysers &= AN_REQ_FLT_END;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100987 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100988 DBG_TRACE_DEVEL("leaving on error",
989 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200990 return 0;
991}
992
993/* This function is an analyser which forwards request body (including chunk
994 * sizes if any). It is called as soon as we must forward, even if we forward
995 * zero byte. The only situation where it must not be called is when we're in
996 * tunnel mode and we want to forward till the close. It's used both to forward
997 * remaining data and to resync after end of body. It expects the msg_state to
998 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
999 * read more data, or 1 once we can go on with next request or end the stream.
1000 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
1001 * bytes of pending data + the headers if not already done.
1002 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001003int http_request_forward_body(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001004{
1005 struct session *sess = s->sess;
1006 struct http_txn *txn = s->txn;
Christopher Faulet9768c262018-10-22 09:34:31 +02001007 struct http_msg *msg = &txn->req;
1008 struct htx *htx;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001009 short status = 0;
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001010 int ret;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001011
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001012 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001013
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001014 htx = htxbuf(&req->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001015
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001016 if (htx->flags & HTX_FL_PARSING_ERROR)
1017 goto return_bad_req;
1018 if (htx->flags & HTX_FL_PROCESSING_ERROR)
1019 goto return_int_err;
1020
Christopher Faulete0768eb2018-10-03 16:38:02 +02001021 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
1022 ((req->flags & CF_SHUTW) && (req->to_forward || co_data(req)))) {
1023 /* Output closed while we were sending data. We must abort and
1024 * wake the other side up.
1025 */
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001026
Olivier Houchard29cac3c2019-07-12 15:48:58 +02001027 /* Don't abort yet if we had L7 retries activated and it
1028 * was a write error, we may recover.
1029 */
1030 if (!(req->flags & (CF_READ_ERROR | CF_READ_TIMEOUT)) &&
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001031 (s->si[1].flags & SI_FL_L7_RETRY)) {
1032 DBG_TRACE_DEVEL("leaving on L7 retry",
1033 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Olivier Houchard29cac3c2019-07-12 15:48:58 +02001034 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001035 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001036 msg->msg_state = HTTP_MSG_ERROR;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001037 http_end_request(s);
1038 http_end_response(s);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001039 DBG_TRACE_DEVEL("leaving on error",
1040 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001041 return 1;
1042 }
1043
1044 /* Note that we don't have to send 100-continue back because we don't
1045 * need the data to complete our job, and it's up to the server to
1046 * decide whether to return 100, 417 or anything else in return of
1047 * an "Expect: 100-continue" header.
1048 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001049 if (msg->msg_state == HTTP_MSG_BODY)
1050 msg->msg_state = HTTP_MSG_DATA;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001051
Christopher Faulete0768eb2018-10-03 16:38:02 +02001052 /* in most states, we should abort in case of early close */
1053 channel_auto_close(req);
1054
1055 if (req->to_forward) {
Christopher Faulet66af0b22019-03-22 14:54:52 +01001056 if (req->to_forward == CHN_INFINITE_FORWARD) {
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001057 if (req->flags & CF_EOI)
1058 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet66af0b22019-03-22 14:54:52 +01001059 }
1060 else {
1061 /* We can't process the buffer's contents yet */
1062 req->flags |= CF_WAKE_WRITE;
1063 goto missing_data_or_waiting;
1064 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001065 }
1066
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001067 if (msg->msg_state >= HTTP_MSG_ENDING)
1068 goto ending;
1069
1070 if (txn->meth == HTTP_METH_CONNECT) {
1071 msg->msg_state = HTTP_MSG_ENDING;
1072 goto ending;
1073 }
1074
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001075 /* Forward input data. We get it by removing all outgoing data not
1076 * forwarded yet from HTX data size. If there are some data filters, we
1077 * let them decide the amount of data to forward.
Christopher Faulet9768c262018-10-22 09:34:31 +02001078 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001079 if (HAS_REQ_DATA_FILTERS(s)) {
1080 ret = flt_http_payload(s, msg, htx->data);
1081 if (ret < 0)
1082 goto return_bad_req;
Christopher Faulet421e7692019-06-13 11:16:45 +02001083 c_adv(req, ret);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001084 }
1085 else {
Christopher Faulet421e7692019-06-13 11:16:45 +02001086 c_adv(req, htx->data - co_data(req));
Christopher Faulet66af0b22019-03-22 14:54:52 +01001087 if (msg->flags & HTTP_MSGF_XFER_LEN)
1088 channel_htx_forward_forever(req, htx);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001089 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001090
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001091 if (htx->data != co_data(req))
1092 goto missing_data_or_waiting;
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001093
Christopher Faulet9768c262018-10-22 09:34:31 +02001094 /* Check if the end-of-message is reached and if so, switch the message
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001095 * in HTTP_MSG_ENDING state. Then if all data was marked to be
1096 * forwarded, set the state to HTTP_MSG_DONE.
Christopher Faulet9768c262018-10-22 09:34:31 +02001097 */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001098 if (!(htx->flags & HTX_FL_EOM))
Christopher Faulet9768c262018-10-22 09:34:31 +02001099 goto missing_data_or_waiting;
1100
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001101 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet9768c262018-10-22 09:34:31 +02001102
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001103 ending:
Christopher Faulet2151cdd2020-07-22 16:34:59 +02001104 req->flags &= ~CF_EXPECT_MORE; /* no more data are expected */
1105
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001106 /* other states, ENDING...TUNNEL */
1107 if (msg->msg_state >= HTTP_MSG_DONE)
1108 goto done;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001109
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001110 if (HAS_REQ_DATA_FILTERS(s)) {
1111 ret = flt_http_end(s, msg);
1112 if (ret <= 0) {
1113 if (!ret)
1114 goto missing_data_or_waiting;
1115 goto return_bad_req;
1116 }
1117 }
1118
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001119 if (txn->meth == HTTP_METH_CONNECT)
1120 msg->msg_state = HTTP_MSG_TUNNEL;
1121 else {
1122 msg->msg_state = HTTP_MSG_DONE;
1123 req->to_forward = 0;
Christopher Faulet5b82cc52020-10-12 15:18:50 +02001124
1125 if ((s->be->retry_type &~ PR_RE_CONN_FAILED) && !(s->si[1].flags & SI_FL_D_L7_RETRY)) {
1126 struct stream_interface *si = &s->si[1];
1127
1128 /* If we want to be able to do L7 retries, copy the
1129 * request, so that we are able to resend them if
1130 * needed.
1131 *
1132 * Try to allocate a buffer if we had none. If it
1133 * fails, the next test will just disable the l7
1134 * retries.
1135 */
1136 DBG_TRACE_STATE("enable L7 retry, save the request", STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
1137 si->flags |= SI_FL_L7_RETRY;
Willy Tarreauf499f502021-03-22 16:17:37 +01001138 if (b_alloc(&si->l7_buffer) == NULL)
Christopher Faulet5b82cc52020-10-12 15:18:50 +02001139 si->flags &= ~SI_FL_L7_RETRY;
1140 else {
1141 memcpy(b_orig(&si->l7_buffer), b_orig(&req->buf), b_size(&req->buf));
1142 b_add(&si->l7_buffer, co_data(req));
1143 }
1144 }
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001145 }
1146
1147 done:
1148 /* we don't want to forward closes on DONE except in tunnel mode. */
1149 if (!(txn->flags & TX_CON_WANT_TUN))
1150 channel_dont_close(req);
1151
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001152 http_end_request(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001153 if (!(req->analysers & an_bit)) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001154 http_end_response(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001155 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
1156 if (req->flags & CF_SHUTW) {
1157 /* request errors are most likely due to the
1158 * server aborting the transfer. */
Christopher Faulet93e02d82019-03-08 14:18:50 +01001159 goto return_srv_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001160 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001161 goto return_bad_req;
1162 }
Christopher Faulet5b82cc52020-10-12 15:18:50 +02001163
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001164 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001165 return 1;
1166 }
1167
1168 /* If "option abortonclose" is set on the backend, we want to monitor
1169 * the client's connection and forward any shutdown notification to the
1170 * server, which will decide whether to close or to go on processing the
1171 * request. We only do that in tunnel mode, and not in other modes since
1172 * it can be abused to exhaust source ports. */
Christopher Faulet769d0e92019-03-22 14:23:18 +01001173 if (s->be->options & PR_O_ABRT_CLOSE) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001174 channel_auto_read(req);
Christopher Fauletc41547b2019-07-16 14:32:23 +02001175 if ((req->flags & (CF_SHUTR|CF_READ_NULL)) && !(txn->flags & TX_CON_WANT_TUN))
Christopher Faulete0768eb2018-10-03 16:38:02 +02001176 s->si[1].flags |= SI_FL_NOLINGER;
1177 channel_auto_close(req);
1178 }
1179 else if (s->txn->meth == HTTP_METH_POST) {
1180 /* POST requests may require to read extra CRLF sent by broken
1181 * browsers and which could cause an RST to be sent upon close
1182 * on some systems (eg: Linux). */
1183 channel_auto_read(req);
1184 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001185 DBG_TRACE_DEVEL("waiting for the end of the HTTP txn",
1186 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001187 return 0;
1188
1189 missing_data_or_waiting:
1190 /* stop waiting for data if the input is closed before the end */
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001191 if (msg->msg_state < HTTP_MSG_ENDING && req->flags & CF_SHUTR)
Christopher Faulet93e02d82019-03-08 14:18:50 +01001192 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001193
1194 waiting:
1195 /* waiting for the last bits to leave the buffer */
1196 if (req->flags & CF_SHUTW)
Christopher Faulet93e02d82019-03-08 14:18:50 +01001197 goto return_srv_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001198
1199 /* When TE: chunked is used, we need to get there again to parse remaining
1200 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
1201 * And when content-length is used, we never want to let the possible
1202 * shutdown be forwarded to the other side, as the state machine will
1203 * take care of it once the client responds. It's also important to
1204 * prevent TIME_WAITs from accumulating on the backend side, and for
1205 * HTTP/2 where the last frame comes with a shutdown.
1206 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001207 if (msg->flags & HTTP_MSGF_XFER_LEN)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001208 channel_dont_close(req);
1209
1210 /* We know that more data are expected, but we couldn't send more that
1211 * what we did. So we always set the CF_EXPECT_MORE flag so that the
1212 * system knows it must not set a PUSH on this first part. Interactive
1213 * modes are already handled by the stream sock layer. We must not do
1214 * this in content-length mode because it could present the MSG_MORE
1215 * flag with the last block of forwarded data, which would cause an
1216 * additional delay to be observed by the receiver.
1217 */
Christopher Faulet2151cdd2020-07-22 16:34:59 +02001218 if (HAS_REQ_DATA_FILTERS(s))
Christopher Faulete0768eb2018-10-03 16:38:02 +02001219 req->flags |= CF_EXPECT_MORE;
1220
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001221 DBG_TRACE_DEVEL("waiting for more data to forward",
1222 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001223 return 0;
1224
Christopher Faulet93e02d82019-03-08 14:18:50 +01001225 return_cli_abort:
1226 _HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
1227 _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
William Lallemand36119de2021-03-08 15:26:48 +01001228 if (sess->listener && sess->listener->counters)
Christopher Fauletcff0f732019-12-16 16:13:44 +01001229 _HA_ATOMIC_ADD(&sess->listener->counters->cli_aborts, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001230 if (objt_server(s->target))
Christopher Fauletcff0f732019-12-16 16:13:44 +01001231 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.cli_aborts, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001232 if (!(s->flags & SF_ERR_MASK))
1233 s->flags |= SF_ERR_CLICL;
1234 status = 400;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001235 goto return_prx_cond;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001236
1237 return_srv_abort:
1238 _HA_ATOMIC_ADD(&sess->fe->fe_counters.srv_aborts, 1);
1239 _HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1);
William Lallemand36119de2021-03-08 15:26:48 +01001240 if (sess->listener && sess->listener->counters)
Christopher Fauletcff0f732019-12-16 16:13:44 +01001241 _HA_ATOMIC_ADD(&sess->listener->counters->srv_aborts, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001242 if (objt_server(s->target))
Christopher Fauletcff0f732019-12-16 16:13:44 +01001243 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.srv_aborts, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001244 if (!(s->flags & SF_ERR_MASK))
1245 s->flags |= SF_ERR_SRVCL;
1246 status = 502;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001247 goto return_prx_cond;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001248
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001249 return_int_err:
1250 if (!(s->flags & SF_ERR_MASK))
1251 s->flags |= SF_ERR_INTERNAL;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001252 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001253 _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1);
William Lallemand36119de2021-03-08 15:26:48 +01001254 if (sess->listener && sess->listener->counters)
Christopher Fauletb8a53712019-12-16 11:29:38 +01001255 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001256 if (objt_server(s->target))
1257 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.internal_errors, 1);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001258 status = 500;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001259 goto return_prx_cond;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001260
Christopher Faulet93e02d82019-03-08 14:18:50 +01001261 return_bad_req:
Olivier Houcharda798bf52019-03-08 18:52:00 +01001262 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
William Lallemand36119de2021-03-08 15:26:48 +01001263 if (sess->listener && sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +01001264 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001265 status = 400;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001266 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001267
Christopher Fauletb8a53712019-12-16 11:29:38 +01001268 return_prx_cond:
Christopher Faulet9768c262018-10-22 09:34:31 +02001269 if (txn->status > 0) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001270 /* Note: we don't send any error if some data were already sent */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001271 http_reply_and_close(s, txn->status, NULL);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001272 } else {
Christopher Faulet93e02d82019-03-08 14:18:50 +01001273 txn->status = status;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001274 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001275 }
1276 req->analysers &= AN_REQ_FLT_END;
1277 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 +01001278 if (!(s->flags & SF_ERR_MASK))
1279 s->flags |= SF_ERR_PRXCOND;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001280 if (!(s->flags & SF_FINST_MASK))
1281 s->flags |= ((txn->rsp.msg_state < HTTP_MSG_ERROR) ? SF_FINST_H : SF_FINST_D);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001282 DBG_TRACE_DEVEL("leaving on error ",
1283 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001284 return 0;
1285}
1286
Olivier Houcharda254a372019-04-05 15:30:12 +02001287/* Reset the stream and the backend stream_interface to a situation suitable for attemption connection */
1288/* Returns 0 if we can attempt to retry, -1 otherwise */
1289static __inline int do_l7_retry(struct stream *s, struct stream_interface *si)
1290{
Christopher Faulet5b82cc52020-10-12 15:18:50 +02001291 struct channel *req = &s->req;
1292 struct channel *res = &s->res;
Olivier Houcharda254a372019-04-05 15:30:12 +02001293
1294 si->conn_retries--;
1295 if (si->conn_retries < 0)
Christopher Faulet5b82cc52020-10-12 15:18:50 +02001296 goto no_retry;
1297
1298 if (b_is_null(&req->buf) && !channel_alloc_buffer(req, &s->buffer_wait))
1299 goto no_retry;
Olivier Houcharda254a372019-04-05 15:30:12 +02001300
Willy Tarreau223995e2019-05-04 10:38:31 +02001301 if (objt_server(s->target))
1302 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.retries, 1);
1303 _HA_ATOMIC_ADD(&s->be->be_counters.retries, 1);
1304
Olivier Houcharda254a372019-04-05 15:30:12 +02001305 /* Remove any write error from the request, and read error from the response */
1306 req->flags &= ~(CF_WRITE_ERROR | CF_WRITE_TIMEOUT | CF_SHUTW | CF_SHUTW_NOW);
1307 res->flags &= ~(CF_READ_ERROR | CF_READ_TIMEOUT | CF_SHUTR | CF_EOI | CF_READ_NULL | CF_SHUTR_NOW);
1308 res->analysers = 0;
1309 si->flags &= ~(SI_FL_ERR | SI_FL_EXP | SI_FL_RXBLK_SHUT);
Olivier Houchard8cabc972020-05-12 22:18:14 +02001310 s->flags &= ~SF_ADDR_SET;
Olivier Houchard4bd58672019-07-12 16:16:59 +02001311 stream_choose_redispatch(s);
Olivier Houcharda254a372019-04-05 15:30:12 +02001312 si->exp = TICK_ETERNITY;
1313 res->rex = TICK_ETERNITY;
1314 res->to_forward = 0;
1315 res->analyse_exp = TICK_ETERNITY;
1316 res->total = 0;
Olivier Houchard4bd58672019-07-12 16:16:59 +02001317 s->flags &= ~(SF_ERR_SRVTO | SF_ERR_SRVCL);
Olivier Houcharda254a372019-04-05 15:30:12 +02001318 si_release_endpoint(&s->si[1]);
Olivier Houcharda254a372019-04-05 15:30:12 +02001319
Christopher Faulet5b82cc52020-10-12 15:18:50 +02001320 b_reset(&req->buf);
1321 memcpy(b_orig(&req->buf), b_orig(&si->l7_buffer), b_size(&si->l7_buffer));
1322 b_set_data(&req->buf, b_size(&req->buf));
1323 co_set_data(req, b_data(&si->l7_buffer));
1324
Ilya Shipitsinacf84592021-02-06 22:29:08 +05001325 DBG_TRACE_DEVEL("perform a L7 retry", STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, s->txn);
Olivier Houcharda254a372019-04-05 15:30:12 +02001326 b_reset(&res->buf);
1327 co_set_data(res, 0);
1328 return 0;
Christopher Faulet5b82cc52020-10-12 15:18:50 +02001329
1330 no_retry:
1331 b_free(&si->l7_buffer);
1332 return -1;
Olivier Houcharda254a372019-04-05 15:30:12 +02001333}
1334
Christopher Faulete0768eb2018-10-03 16:38:02 +02001335/* This stream analyser waits for a complete HTTP response. It returns 1 if the
1336 * processing can continue on next analysers, or zero if it either needs more
1337 * data or wants to immediately abort the response (eg: timeout, error, ...). It
1338 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->res.analysers
1339 * when it has nothing left to do, and may remove any analyser when it wants to
1340 * abort.
1341 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001342int http_wait_for_response(struct stream *s, struct channel *rep, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001343{
Christopher Faulet9768c262018-10-22 09:34:31 +02001344 /*
1345 * We will analyze a complete HTTP response to check the its syntax.
1346 *
1347 * Once the start line and all headers are received, we may perform a
1348 * capture of the error (if any), and we will set a few fields. We also
1349 * logging and finally headers capture.
1350 */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001351 struct session *sess = s->sess;
1352 struct http_txn *txn = s->txn;
1353 struct http_msg *msg = &txn->rsp;
Christopher Faulet9768c262018-10-22 09:34:31 +02001354 struct htx *htx;
Olivier Houcharda254a372019-04-05 15:30:12 +02001355 struct stream_interface *si_b = &s->si[1];
Christopher Faulet61608322018-11-23 16:23:45 +01001356 struct connection *srv_conn;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001357 struct htx_sl *sl;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001358 int n;
1359
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001360 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001361
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001362 htx = htxbuf(&rep->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001363
Willy Tarreau4236f032019-03-05 10:43:32 +01001364 /* Parsing errors are caught here */
1365 if (htx->flags & HTX_FL_PARSING_ERROR)
1366 goto return_bad_res;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001367 if (htx->flags & HTX_FL_PROCESSING_ERROR)
1368 goto return_int_err;
Willy Tarreau4236f032019-03-05 10:43:32 +01001369
Christopher Faulete0768eb2018-10-03 16:38:02 +02001370 /*
1371 * Now we quickly check if we have found a full valid response.
1372 * If not so, we check the FD and buffer states before leaving.
1373 * A full response is indicated by the fact that we have seen
1374 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
1375 * responses are checked first.
1376 *
1377 * Depending on whether the client is still there or not, we
1378 * may send an error response back or not. Note that normally
1379 * we should only check for HTTP status there, and check I/O
1380 * errors somewhere else.
1381 */
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001382 next_one:
Christopher Faulet29f17582019-05-23 11:03:26 +02001383 if (unlikely(htx_is_empty(htx) || htx->first == -1)) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001384 /* 1: have we encountered a read error ? */
1385 if (rep->flags & CF_READ_ERROR) {
Olivier Houchard865d8392019-05-03 22:46:27 +02001386 struct connection *conn = NULL;
1387
Olivier Houchard865d8392019-05-03 22:46:27 +02001388 if (objt_cs(s->si[1].end))
1389 conn = objt_cs(s->si[1].end)->conn;
1390
1391 if (si_b->flags & SI_FL_L7_RETRY &&
1392 (!conn || conn->err_code != CO_ER_SSL_EARLY_FAILED)) {
Olivier Houcharda254a372019-04-05 15:30:12 +02001393 /* If we arrive here, then CF_READ_ERROR was
1394 * set by si_cs_recv() because we matched a
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05001395 * status, otherwise it would have removed
Olivier Houcharda254a372019-04-05 15:30:12 +02001396 * the SI_FL_L7_RETRY flag, so it's ok not
1397 * to check s->be->retry_type.
1398 */
Christopher Faulet5b82cc52020-10-12 15:18:50 +02001399 if (co_data(rep) || do_l7_retry(s, si_b) == 0) {
1400 DBG_TRACE_DEVEL("leaving on L7 retry",
1401 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharda254a372019-04-05 15:30:12 +02001402 return 0;
Christopher Faulet5b82cc52020-10-12 15:18:50 +02001403 }
Olivier Houcharda254a372019-04-05 15:30:12 +02001404 }
1405
Olivier Houchard6db16992019-05-17 15:40:49 +02001406 if (txn->flags & TX_NOT_FIRST)
1407 goto abort_keep_alive;
1408
Olivier Houcharda798bf52019-03-08 18:52:00 +01001409 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001410 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001411 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001412 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001413 }
1414
Christopher Faulete0768eb2018-10-03 16:38:02 +02001415 rep->analysers &= AN_RES_FLT_END;
1416 txn->status = 502;
1417
1418 /* Check to see if the server refused the early data.
1419 * If so, just send a 425
1420 */
Willy Tarreauee99aaf2020-06-23 05:58:20 +02001421 if (conn && conn->err_code == CO_ER_SSL_EARLY_FAILED) {
Olivier Houchard865d8392019-05-03 22:46:27 +02001422 if ((s->be->retry_type & PR_RE_EARLY_ERROR) &&
Olivier Houchardad26d8d2019-05-10 17:48:28 +02001423 (si_b->flags & SI_FL_L7_RETRY) &&
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001424 do_l7_retry(s, si_b) == 0) {
1425 DBG_TRACE_DEVEL("leaving on L7 retry",
1426 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houchard865d8392019-05-03 22:46:27 +02001427 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001428 }
Olivier Houchard865d8392019-05-03 22:46:27 +02001429 txn->status = 425;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001430 }
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001431 else
1432 stream_inc_http_fail_ctr(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001433
1434 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001435 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001436
1437 if (!(s->flags & SF_ERR_MASK))
1438 s->flags |= SF_ERR_SRVCL;
1439 if (!(s->flags & SF_FINST_MASK))
1440 s->flags |= SF_FINST_H;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001441 DBG_TRACE_DEVEL("leaving on error",
1442 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001443 return 0;
1444 }
1445
Christopher Faulet9768c262018-10-22 09:34:31 +02001446 /* 2: read timeout : return a 504 to the client. */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001447 else if (rep->flags & CF_READ_TIMEOUT) {
Olivier Houcharda254a372019-04-05 15:30:12 +02001448 if ((si_b->flags & SI_FL_L7_RETRY) &&
1449 (s->be->retry_type & PR_RE_TIMEOUT)) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001450 if (co_data(rep) || do_l7_retry(s, si_b) == 0) {
1451 DBG_TRACE_DEVEL("leaving on L7 retry",
1452 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharda254a372019-04-05 15:30:12 +02001453 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001454 }
Olivier Houcharda254a372019-04-05 15:30:12 +02001455 }
Olivier Houcharda798bf52019-03-08 18:52:00 +01001456 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001457 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001458 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001459 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001460 }
1461
Christopher Faulete0768eb2018-10-03 16:38:02 +02001462 rep->analysers &= AN_RES_FLT_END;
1463 txn->status = 504;
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001464 stream_inc_http_fail_ctr(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001465 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001466 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001467
1468 if (!(s->flags & SF_ERR_MASK))
1469 s->flags |= SF_ERR_SRVTO;
1470 if (!(s->flags & SF_FINST_MASK))
1471 s->flags |= SF_FINST_H;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001472 DBG_TRACE_DEVEL("leaving on error",
1473 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001474 return 0;
1475 }
1476
Christopher Faulet9768c262018-10-22 09:34:31 +02001477 /* 3: client abort with an abortonclose */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001478 else if ((rep->flags & CF_SHUTR) && ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001479 _HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
1480 _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
William Lallemand36119de2021-03-08 15:26:48 +01001481 if (sess->listener && sess->listener->counters)
Christopher Fauletcff0f732019-12-16 16:13:44 +01001482 _HA_ATOMIC_ADD(&sess->listener->counters->cli_aborts, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001483 if (objt_server(s->target))
Olivier Houcharda798bf52019-03-08 18:52:00 +01001484 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.cli_aborts, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001485
1486 rep->analysers &= AN_RES_FLT_END;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001487 txn->status = 400;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001488 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001489
1490 if (!(s->flags & SF_ERR_MASK))
1491 s->flags |= SF_ERR_CLICL;
1492 if (!(s->flags & SF_FINST_MASK))
1493 s->flags |= SF_FINST_H;
1494
1495 /* process_stream() will take care of the error */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001496 DBG_TRACE_DEVEL("leaving on error",
1497 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001498 return 0;
1499 }
1500
Christopher Faulet9768c262018-10-22 09:34:31 +02001501 /* 4: close from server, capture the response if the server has started to respond */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001502 else if (rep->flags & CF_SHUTR) {
Olivier Houcharda254a372019-04-05 15:30:12 +02001503 if ((si_b->flags & SI_FL_L7_RETRY) &&
1504 (s->be->retry_type & PR_RE_DISCONNECTED)) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001505 if (co_data(rep) || do_l7_retry(s, si_b) == 0) {
1506 DBG_TRACE_DEVEL("leaving on L7 retry",
1507 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharda254a372019-04-05 15:30:12 +02001508 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001509 }
Olivier Houcharda254a372019-04-05 15:30:12 +02001510 }
1511
Olivier Houchard6db16992019-05-17 15:40:49 +02001512 if (txn->flags & TX_NOT_FIRST)
1513 goto abort_keep_alive;
1514
Olivier Houcharda798bf52019-03-08 18:52:00 +01001515 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001516 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001517 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001518 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001519 }
1520
Christopher Faulete0768eb2018-10-03 16:38:02 +02001521 rep->analysers &= AN_RES_FLT_END;
1522 txn->status = 502;
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001523 stream_inc_http_fail_ctr(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001524 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001525 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001526
1527 if (!(s->flags & SF_ERR_MASK))
1528 s->flags |= SF_ERR_SRVCL;
1529 if (!(s->flags & SF_FINST_MASK))
1530 s->flags |= SF_FINST_H;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001531 DBG_TRACE_DEVEL("leaving on error",
1532 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001533 return 0;
1534 }
1535
Christopher Faulet9768c262018-10-22 09:34:31 +02001536 /* 5: write error to client (we don't send any message then) */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001537 else if (rep->flags & CF_WRITE_ERROR) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001538 if (txn->flags & TX_NOT_FIRST)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001539 goto abort_keep_alive;
1540
Olivier Houcharda798bf52019-03-08 18:52:00 +01001541 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001542 if (objt_server(s->target))
1543 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001544 rep->analysers &= AN_RES_FLT_END;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001545
1546 if (!(s->flags & SF_ERR_MASK))
1547 s->flags |= SF_ERR_CLICL;
1548 if (!(s->flags & SF_FINST_MASK))
1549 s->flags |= SF_FINST_H;
1550
1551 /* process_stream() will take care of the error */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001552 DBG_TRACE_DEVEL("leaving on error",
1553 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001554 return 0;
1555 }
1556
1557 channel_dont_close(rep);
1558 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001559 DBG_TRACE_DEVEL("waiting for more data",
1560 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001561 return 0;
1562 }
1563
1564 /* More interesting part now : we know that we have a complete
1565 * response which at least looks like HTTP. We have an indicator
1566 * of each header's length, so we can parse them quickly.
1567 */
Christopher Faulet29f17582019-05-23 11:03:26 +02001568 BUG_ON(htx_get_first_type(htx) != HTX_BLK_RES_SL);
Christopher Faulet297fbb42019-05-13 14:41:27 +02001569 sl = http_get_stline(htx);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001570
Christopher Faulet5b82cc52020-10-12 15:18:50 +02001571 if ((si_b->flags & SI_FL_L7_RETRY) &&
1572 l7_status_match(s->be, sl->info.res.status) &&
1573 do_l7_retry(s, si_b) == 0) {
1574 DBG_TRACE_DEVEL("leaving on L7 retry", STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
1575 return 0;
1576 }
1577 b_free(&s->si[1].l7_buffer);
1578
1579 msg->msg_state = HTTP_MSG_BODY;
1580
Christopher Faulet9768c262018-10-22 09:34:31 +02001581 /* 0: we might have to print this header in debug mode */
1582 if (unlikely((global.mode & MODE_DEBUG) &&
1583 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
1584 int32_t pos;
1585
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001586 http_debug_stline("srvrep", s, sl);
Christopher Faulet9768c262018-10-22 09:34:31 +02001587
Christopher Fauleta3f15502019-05-13 15:27:23 +02001588 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001589 struct htx_blk *blk = htx_get_blk(htx, pos);
1590 enum htx_blk_type type = htx_get_blk_type(blk);
1591
1592 if (type == HTX_BLK_EOH)
1593 break;
1594 if (type != HTX_BLK_HDR)
1595 continue;
1596
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001597 http_debug_hdr("srvhdr", s,
1598 htx_get_blk_name(htx, blk),
1599 htx_get_blk_value(htx, blk));
Christopher Faulet9768c262018-10-22 09:34:31 +02001600 }
1601 }
1602
Christopher Faulet03599112018-11-27 11:21:21 +01001603 /* 1: get the status code and the version. Also set HTTP flags */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001604 txn->status = sl->info.res.status;
Christopher Faulet03599112018-11-27 11:21:21 +01001605 if (sl->flags & HTX_SL_F_VER_11)
Christopher Faulet9768c262018-10-22 09:34:31 +02001606 msg->flags |= HTTP_MSGF_VER_11;
Christopher Faulet03599112018-11-27 11:21:21 +01001607 if (sl->flags & HTX_SL_F_XFER_LEN) {
1608 msg->flags |= HTTP_MSGF_XFER_LEN;
Christopher Faulet2a408542020-11-20 14:22:37 +01001609 if (sl->flags & HTX_SL_F_CLEN)
1610 msg->flags |= HTTP_MSGF_CNT_LEN;
1611 else if (sl->flags & HTX_SL_F_CHNK)
1612 msg->flags |= HTTP_MSGF_TE_CHNK;
Christopher Faulet03599112018-11-27 11:21:21 +01001613 }
Christopher Faulet2a408542020-11-20 14:22:37 +01001614 if (sl->flags & HTX_SL_F_BODYLESS)
1615 msg->flags |= HTTP_MSGF_BODYLESS;
Christopher Faulet576c3582021-01-08 15:53:01 +01001616 if (sl->flags & HTX_SL_F_CONN_UPG)
1617 msg->flags |= HTTP_MSGF_CONN_UPG;
Christopher Faulet9768c262018-10-22 09:34:31 +02001618
1619 n = txn->status / 100;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001620 if (n < 1 || n > 5)
1621 n = 0;
Christopher Faulet9768c262018-10-22 09:34:31 +02001622
Christopher Faulete0768eb2018-10-03 16:38:02 +02001623 /* when the client triggers a 4xx from the server, it's most often due
1624 * to a missing object or permission. These events should be tracked
1625 * because if they happen often, it may indicate a brute force or a
1626 * vulnerability scan.
1627 */
1628 if (n == 4)
1629 stream_inc_http_err_ctr(s);
1630
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001631 if (n == 5 && txn->status != 501 && txn->status != 505)
1632 stream_inc_http_fail_ctr(s);
1633
Marcin Deranek3c27dda2020-05-15 18:32:51 +02001634 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001635 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.p.http.rsp[n], 1);
Marcin Deranek3c27dda2020-05-15 18:32:51 +02001636 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.p.http.cum_req, 1);
1637 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001638
Christopher Faulete0768eb2018-10-03 16:38:02 +02001639 /* Adjust server's health based on status code. Note: status codes 501
1640 * and 505 are triggered on demand by client request, so we must not
1641 * count them as server failures.
1642 */
1643 if (objt_server(s->target)) {
1644 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001645 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_OK);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001646 else
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001647 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_STS);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001648 }
1649
1650 /*
1651 * We may be facing a 100-continue response, or any other informational
1652 * 1xx response which is non-final, in which case this is not the right
1653 * response, and we're waiting for the next one. Let's allow this response
1654 * to go to the client and wait for the next one. There's an exception for
1655 * 101 which is used later in the code to switch protocols.
1656 */
1657 if (txn->status < 200 &&
1658 (txn->status == 100 || txn->status >= 102)) {
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001659 FLT_STRM_CB(s, flt_http_reset(s, msg));
Christopher Faulet421e7692019-06-13 11:16:45 +02001660 htx->first = channel_htx_fwd_headers(rep, htx);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001661 msg->msg_state = HTTP_MSG_RPBEFORE;
Christopher Faulet3499f622019-09-03 15:23:54 +02001662 msg->flags = 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001663 txn->status = 0;
1664 s->logs.t_data = -1; /* was not a response yet */
Christopher Faulet7d518452020-08-31 11:07:07 +02001665 rep->flags |= CF_SEND_DONTWAIT; /* Send ASAP informational messages */
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001666 goto next_one;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001667 }
1668
Christopher Faulet6e6c7b12021-01-08 16:02:05 +01001669 /* A 101-switching-protocols must contains a Connection header with the
1670 * "upgrade" option and the request too. It means both are agree to
1671 * upgrade. It is not so strict because there is no test on the Upgrade
1672 * header content. But it is probably stronger enough for now.
1673 */
1674 if (txn->status == 101 &&
1675 (!(txn->req.flags & HTTP_MSGF_CONN_UPG) || !(txn->rsp.flags & HTTP_MSGF_CONN_UPG)))
1676 goto return_bad_res;
1677
Christopher Faulete0768eb2018-10-03 16:38:02 +02001678 /*
1679 * 2: check for cacheability.
1680 */
1681
1682 switch (txn->status) {
1683 case 200:
1684 case 203:
1685 case 204:
1686 case 206:
1687 case 300:
1688 case 301:
1689 case 404:
1690 case 405:
1691 case 410:
1692 case 414:
1693 case 501:
1694 break;
1695 default:
1696 /* RFC7231#6.1:
1697 * Responses with status codes that are defined as
1698 * cacheable by default (e.g., 200, 203, 204, 206,
1699 * 300, 301, 404, 405, 410, 414, and 501 in this
1700 * specification) can be reused by a cache with
1701 * heuristic expiration unless otherwise indicated
1702 * by the method definition or explicit cache
1703 * controls [RFC7234]; all other status codes are
1704 * not cacheable by default.
1705 */
1706 txn->flags &= ~(TX_CACHEABLE | TX_CACHE_COOK);
1707 break;
1708 }
1709
1710 /*
1711 * 3: we may need to capture headers
1712 */
1713 s->logs.logwait &= ~LW_RESP;
1714 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->res_cap))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001715 http_capture_headers(htx, s->res_cap, sess->fe->rsp_cap);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001716
Christopher Faulet9768c262018-10-22 09:34:31 +02001717 /* Skip parsing if no content length is possible. */
Christopher Fauletc75668e2020-12-07 18:10:32 +01001718 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status >= 200 && txn->status < 300) ||
Christopher Faulete0768eb2018-10-03 16:38:02 +02001719 txn->status == 101)) {
1720 /* Either we've established an explicit tunnel, or we're
1721 * switching the protocol. In both cases, we're very unlikely
1722 * to understand the next protocols. We have to switch to tunnel
1723 * mode, so that we transfer the request and responses then let
1724 * this protocol pass unmodified. When we later implement specific
1725 * parsers for such protocols, we'll want to check the Upgrade
1726 * header which contains information about that protocol for
1727 * responses with status 101 (eg: see RFC2817 about TLS).
1728 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02001729 txn->flags |= TX_CON_WANT_TUN;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001730 }
1731
Christopher Faulet61608322018-11-23 16:23:45 +01001732 /* check for NTML authentication headers in 401 (WWW-Authenticate) and
1733 * 407 (Proxy-Authenticate) responses and set the connection to private
1734 */
1735 srv_conn = cs_conn(objt_cs(s->si[1].end));
1736 if (srv_conn) {
1737 struct ist hdr;
1738 struct http_hdr_ctx ctx;
1739
1740 if (txn->status == 401)
1741 hdr = ist("WWW-Authenticate");
1742 else if (txn->status == 407)
1743 hdr = ist("Proxy-Authenticate");
1744 else
1745 goto end;
1746
1747 ctx.blk = NULL;
1748 while (http_find_header(htx, hdr, &ctx, 0)) {
Willy Tarreauf1dcced2020-05-07 19:27:02 +02001749 /* If www-authenticate contains "Negotiate", "Nego2", or "NTLM",
1750 * possibly followed by blanks and a base64 string, the connection
1751 * is private. Since it's a mess to deal with, we only check for
1752 * values starting with "NTLM" or "Nego". Note that often multiple
1753 * headers are sent by the server there.
1754 */
1755 if ((ctx.value.len >= 4 && strncasecmp(ctx.value.ptr, "Nego", 4) == 0) ||
Willy Tarreau49a1d282020-05-07 19:10:15 +02001756 (ctx.value.len >= 4 && strncasecmp(ctx.value.ptr, "NTLM", 4) == 0)) {
Olivier Houchard250031e2019-05-29 15:01:50 +02001757 sess->flags |= SESS_FL_PREFER_LAST;
Christopher Faulet08016ab2020-07-01 16:10:06 +02001758 conn_set_owner(srv_conn, sess, NULL);
Christopher Faulet21ddc742020-07-01 15:26:14 +02001759 conn_set_private(srv_conn);
Ilya Shipitsin6b79f382020-07-23 00:32:55 +05001760 /* If it fail now, the same will be done in mux->detach() callback */
Christopher Faulet08016ab2020-07-01 16:10:06 +02001761 session_add_conn(srv_conn->owner, srv_conn, srv_conn->target);
Willy Tarreauf1dcced2020-05-07 19:27:02 +02001762 break;
Olivier Houchard250031e2019-05-29 15:01:50 +02001763 }
Christopher Faulet61608322018-11-23 16:23:45 +01001764 }
1765 }
1766
1767 end:
Christopher Faulete0768eb2018-10-03 16:38:02 +02001768 /* we want to have the response time before we start processing it */
1769 s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now);
1770
1771 /* end of job, return OK */
1772 rep->analysers &= ~an_bit;
1773 rep->analyse_exp = TICK_ETERNITY;
1774 channel_auto_close(rep);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001775 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001776 return 1;
1777
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001778 return_int_err:
Christopher Fauletcff0f732019-12-16 16:13:44 +01001779 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01001780 _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1);
William Lallemand36119de2021-03-08 15:26:48 +01001781 if (sess->listener && sess->listener->counters)
Christopher Fauletcff0f732019-12-16 16:13:44 +01001782 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01001783 if (objt_server(s->target))
1784 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.internal_errors, 1);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001785 txn->status = 500;
1786 if (!(s->flags & SF_ERR_MASK))
1787 s->flags |= SF_ERR_INTERNAL;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001788 goto return_prx_cond;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001789
1790 return_bad_res:
Olivier Houcharda798bf52019-03-08 18:52:00 +01001791 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulet47365272018-10-31 17:40:50 +01001792 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001793 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001794 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Christopher Faulet47365272018-10-31 17:40:50 +01001795 }
Olivier Houcharde3249a92019-05-03 23:01:47 +02001796 if ((s->be->retry_type & PR_RE_JUNK_REQUEST) &&
Olivier Houchardad26d8d2019-05-10 17:48:28 +02001797 (si_b->flags & SI_FL_L7_RETRY) &&
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001798 do_l7_retry(s, si_b) == 0) {
1799 DBG_TRACE_DEVEL("leaving on L7 retry",
1800 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharde3249a92019-05-03 23:01:47 +02001801 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001802 }
Christopher Faulet47365272018-10-31 17:40:50 +01001803 txn->status = 502;
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001804 stream_inc_http_fail_ctr(s);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001805 /* fall through */
1806
Christopher Fauletb8a53712019-12-16 11:29:38 +01001807 return_prx_cond:
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001808 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet47365272018-10-31 17:40:50 +01001809
1810 if (!(s->flags & SF_ERR_MASK))
1811 s->flags |= SF_ERR_PRXCOND;
1812 if (!(s->flags & SF_FINST_MASK))
1813 s->flags |= SF_FINST_H;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001814
1815 s->si[1].flags |= SI_FL_NOLINGER;
1816 rep->analysers &= AN_RES_FLT_END;
Christopher Faulete58c0002020-03-02 16:21:01 +01001817 s->req.analysers &= AN_REQ_FLT_END;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001818 rep->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001819 DBG_TRACE_DEVEL("leaving on error",
1820 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulet47365272018-10-31 17:40:50 +01001821 return 0;
1822
Christopher Faulete0768eb2018-10-03 16:38:02 +02001823 abort_keep_alive:
1824 /* A keep-alive request to the server failed on a network error.
1825 * The client is required to retry. We need to close without returning
1826 * any other information so that the client retries.
1827 */
1828 txn->status = 0;
1829 rep->analysers &= AN_RES_FLT_END;
1830 s->req.analysers &= AN_REQ_FLT_END;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001831 s->logs.logwait = 0;
1832 s->logs.level = 0;
1833 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001834 http_reply_and_close(s, txn->status, NULL);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001835 DBG_TRACE_DEVEL("leaving by closing K/A connection",
1836 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001837 return 0;
1838}
1839
1840/* This function performs all the processing enabled for the current response.
1841 * It normally returns 1 unless it wants to break. It relies on buffers flags,
1842 * and updates s->res.analysers. It might make sense to explode it into several
1843 * other functions. It works like process_request (see indications above).
1844 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001845int http_process_res_common(struct stream *s, struct channel *rep, int an_bit, struct proxy *px)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001846{
1847 struct session *sess = s->sess;
1848 struct http_txn *txn = s->txn;
1849 struct http_msg *msg = &txn->rsp;
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001850 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001851 struct proxy *cur_proxy;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001852 enum rule_result ret = HTTP_RULE_RES_CONT;
1853
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001854 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
1855 return 0;
Christopher Faulet9768c262018-10-22 09:34:31 +02001856
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001857 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001858
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001859 htx = htxbuf(&rep->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001860
1861 /* The stats applet needs to adjust the Connection header but we don't
1862 * apply any filter there.
1863 */
1864 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
1865 rep->analysers &= ~an_bit;
1866 rep->analyse_exp = TICK_ETERNITY;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001867 goto end;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001868 }
1869
1870 /*
1871 * We will have to evaluate the filters.
1872 * As opposed to version 1.2, now they will be evaluated in the
1873 * filters order and not in the header order. This means that
1874 * each filter has to be validated among all headers.
1875 *
1876 * Filters are tried with ->be first, then with ->fe if it is
1877 * different from ->be.
1878 *
1879 * Maybe we are in resume condiion. In this case I choose the
1880 * "struct proxy" which contains the rule list matching the resume
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05001881 * pointer. If none of these "struct proxy" match, I initialise
Christopher Faulete0768eb2018-10-03 16:38:02 +02001882 * the process with the first one.
1883 *
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05001884 * In fact, I check only correspondence between the current list
Christopher Faulete0768eb2018-10-03 16:38:02 +02001885 * pointer and the ->fe rule list. If it doesn't match, I initialize
1886 * the loop with the ->be.
1887 */
1888 if (s->current_rule_list == &sess->fe->http_res_rules)
1889 cur_proxy = sess->fe;
1890 else
1891 cur_proxy = s->be;
1892 while (1) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001893 /* evaluate http-response rules */
1894 if (ret == HTTP_RULE_RES_CONT) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001895 ret = http_res_get_intercept_rule(cur_proxy, &cur_proxy->http_res_rules, s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001896
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001897 switch (ret) {
1898 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
1899 goto return_prx_yield;
1900
1901 case HTTP_RULE_RES_CONT:
1902 case HTTP_RULE_RES_STOP: /* nothing to do */
1903 break;
1904
1905 case HTTP_RULE_RES_DENY: /* deny or tarpit */
1906 goto deny;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001907
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001908 case HTTP_RULE_RES_ABRT: /* abort request, response already sent */
1909 goto return_prx_cond;
1910
1911 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
Christopher Fauletb8a53712019-12-16 11:29:38 +01001912 goto done;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001913
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001914 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
1915 goto return_bad_res;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001916
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001917 case HTTP_RULE_RES_ERROR: /* failed with a bad request */
1918 goto return_int_err;
1919 }
1920
1921 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001922
Christopher Faulete0768eb2018-10-03 16:38:02 +02001923 /* check whether we're already working on the frontend */
1924 if (cur_proxy == sess->fe)
1925 break;
1926 cur_proxy = sess->fe;
1927 }
1928
Christopher Faulete0768eb2018-10-03 16:38:02 +02001929 /* OK that's all we can do for 1xx responses */
1930 if (unlikely(txn->status < 200 && txn->status != 101))
Christopher Fauletf2824e62018-10-01 12:12:37 +02001931 goto end;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001932
1933 /*
1934 * Now check for a server cookie.
1935 */
1936 if (s->be->cookie_name || sess->fe->capture_name || (s->be->options & PR_O_CHK_CACHE))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001937 http_manage_server_side_cookies(s, rep);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001938
1939 /*
1940 * Check for cache-control or pragma headers if required.
1941 */
1942 if ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001943 http_check_response_for_cacheability(s, rep);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001944
1945 /*
1946 * Add server cookie in the response if needed
1947 */
1948 if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) &&
1949 !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) &&
1950 (!(s->flags & SF_DIRECT) ||
1951 ((s->be->cookie_maxidle || txn->cookie_last_date) &&
1952 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
1953 (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
1954 (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
1955 (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
1956 !(s->flags & SF_IGNORE_PRST)) {
1957 /* the server is known, it's not the one the client requested, or the
1958 * cookie's last seen date needs to be refreshed. We have to
1959 * insert a set-cookie here, except if we want to insert only on POST
1960 * requests and this one isn't. Note that servers which don't have cookies
1961 * (eg: some backup servers) will return a full cookie removal request.
1962 */
1963 if (!objt_server(s->target)->cookie) {
1964 chunk_printf(&trash,
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001965 "%s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
Christopher Faulete0768eb2018-10-03 16:38:02 +02001966 s->be->cookie_name);
1967 }
1968 else {
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001969 chunk_printf(&trash, "%s=%s", s->be->cookie_name, objt_server(s->target)->cookie);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001970
1971 if (s->be->cookie_maxidle || s->be->cookie_maxlife) {
1972 /* emit last_date, which is mandatory */
1973 trash.area[trash.data++] = COOKIE_DELIM_DATE;
1974 s30tob64((date.tv_sec+3) >> 2,
1975 trash.area + trash.data);
1976 trash.data += 5;
1977
1978 if (s->be->cookie_maxlife) {
1979 /* emit first_date, which is either the original one or
1980 * the current date.
1981 */
1982 trash.area[trash.data++] = COOKIE_DELIM_DATE;
1983 s30tob64(txn->cookie_first_date ?
1984 txn->cookie_first_date >> 2 :
1985 (date.tv_sec+3) >> 2,
1986 trash.area + trash.data);
1987 trash.data += 5;
1988 }
1989 }
1990 chunk_appendf(&trash, "; path=/");
1991 }
1992
1993 if (s->be->cookie_domain)
1994 chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain);
1995
1996 if (s->be->ck_opts & PR_CK_HTTPONLY)
1997 chunk_appendf(&trash, "; HttpOnly");
1998
1999 if (s->be->ck_opts & PR_CK_SECURE)
2000 chunk_appendf(&trash, "; Secure");
2001
Christopher Faulet2f533902020-01-21 11:06:48 +01002002 if (s->be->cookie_attrs)
2003 chunk_appendf(&trash, "; %s", s->be->cookie_attrs);
2004
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002005 if (unlikely(!http_add_header(htx, ist("Set-Cookie"), ist2(trash.area, trash.data))))
Christopher Fauletb8a53712019-12-16 11:29:38 +01002006 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002007
2008 txn->flags &= ~TX_SCK_MASK;
2009 if (__objt_server(s->target)->cookie && (s->flags & SF_DIRECT))
2010 /* the server did not change, only the date was updated */
2011 txn->flags |= TX_SCK_UPDATED;
2012 else
2013 txn->flags |= TX_SCK_INSERTED;
2014
2015 /* Here, we will tell an eventual cache on the client side that we don't
2016 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
2017 * Some caches understand the correct form: 'no-cache="set-cookie"', but
2018 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
2019 */
2020 if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
2021
2022 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
2023
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002024 if (unlikely(!http_add_header(htx, ist("Cache-control"), ist("private"))))
Christopher Fauletb8a53712019-12-16 11:29:38 +01002025 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002026 }
2027 }
2028
2029 /*
2030 * Check if result will be cacheable with a cookie.
2031 * We'll block the response if security checks have caught
2032 * nasty things such as a cacheable cookie.
2033 */
2034 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
2035 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
2036 (s->be->options & PR_O_CHK_CACHE)) {
2037 /* we're in presence of a cacheable response containing
2038 * a set-cookie header. We'll block it as requested by
2039 * the 'checkcache' option, and send an alert.
2040 */
Christopher Faulete0768eb2018-10-03 16:38:02 +02002041 ha_alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
2042 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
2043 send_log(s->be, LOG_ALERT,
2044 "Blocking cacheable cookie in response from instance %s, server %s.\n",
2045 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
Christopher Fauletb8a53712019-12-16 11:29:38 +01002046 goto deny;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002047 }
2048
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002049 end:
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002050 /*
2051 * Evaluate after-response rules before forwarding the response. rules
2052 * from the backend are evaluated first, then one from the frontend if
2053 * it differs.
2054 */
2055 if (!http_eval_after_res_rules(s))
2056 goto return_int_err;
2057
Christopher Faulete0768eb2018-10-03 16:38:02 +02002058 /* Always enter in the body analyzer */
2059 rep->analysers &= ~AN_RES_FLT_XFER_DATA;
2060 rep->analysers |= AN_RES_HTTP_XFER_BODY;
2061
2062 /* if the user wants to log as soon as possible, without counting
2063 * bytes from the server, then this is the right moment. We have
2064 * to temporarily assign bytes_out to log what we currently have.
2065 */
2066 if (!LIST_ISEMPTY(&sess->fe->logformat) && !(s->logs.logwait & LW_BYTES)) {
2067 s->logs.t_close = s->logs.t_data; /* to get a valid end date */
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002068 s->logs.bytes_out = htx->data;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002069 s->do_log(s);
2070 s->logs.bytes_out = 0;
2071 }
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002072
Christopher Fauletb8a53712019-12-16 11:29:38 +01002073 done:
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002074 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002075 rep->analysers &= ~an_bit;
2076 rep->analyse_exp = TICK_ETERNITY;
2077 return 1;
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002078
Christopher Fauletb8a53712019-12-16 11:29:38 +01002079 deny:
Christopher Fauletb8a53712019-12-16 11:29:38 +01002080 _HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_resp, 1);
Christopher Fauleta08546b2019-12-16 16:07:34 +01002081 _HA_ATOMIC_ADD(&s->be->be_counters.denied_resp, 1);
William Lallemand36119de2021-03-08 15:26:48 +01002082 if (sess->listener && sess->listener->counters)
Christopher Fauletb8a53712019-12-16 11:29:38 +01002083 _HA_ATOMIC_ADD(&sess->listener->counters->denied_resp, 1);
Christopher Fauleta08546b2019-12-16 16:07:34 +01002084 if (objt_server(s->target))
2085 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.denied_resp, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002086 goto return_prx_err;
2087
2088 return_int_err:
2089 txn->status = 500;
2090 if (!(s->flags & SF_ERR_MASK))
2091 s->flags |= SF_ERR_INTERNAL;
Christopher Fauletcff0f732019-12-16 16:13:44 +01002092 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002093 _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1);
2094 if (objt_server(s->target))
2095 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01002096 if (objt_server(s->target))
2097 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002098 goto return_prx_err;
2099
2100 return_bad_res:
2101 txn->status = 502;
Willy Tarreau826f3ab2021-02-10 12:07:15 +01002102 stream_inc_http_fail_ctr(s);
Christopher Fauleta20a6532020-02-05 10:16:41 +01002103 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
2104 if (objt_server(s->target)) {
2105 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
2106 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_RSP);
2107 }
Christopher Fauletb8a53712019-12-16 11:29:38 +01002108 /* fall through */
2109
2110 return_prx_err:
2111 http_reply_and_close(s, txn->status, http_error_message(s));
2112 /* fall through */
2113
2114 return_prx_cond:
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002115 s->logs.t_data = -1; /* was not a valid response */
2116 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002117
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002118 if (!(s->flags & SF_ERR_MASK))
2119 s->flags |= SF_ERR_PRXCOND;
2120 if (!(s->flags & SF_FINST_MASK))
2121 s->flags |= SF_FINST_H;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002122
Christopher Faulete58c0002020-03-02 16:21:01 +01002123 rep->analysers &= AN_RES_FLT_END;
2124 s->req.analysers &= AN_REQ_FLT_END;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002125 rep->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002126 DBG_TRACE_DEVEL("leaving on error",
2127 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002128 return 0;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002129
2130 return_prx_yield:
2131 channel_dont_close(rep);
2132 DBG_TRACE_DEVEL("waiting for more data",
2133 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
2134 return 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002135}
2136
2137/* This function is an analyser which forwards response body (including chunk
2138 * sizes if any). It is called as soon as we must forward, even if we forward
2139 * zero byte. The only situation where it must not be called is when we're in
2140 * tunnel mode and we want to forward till the close. It's used both to forward
2141 * remaining data and to resync after end of body. It expects the msg_state to
2142 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
2143 * read more data, or 1 once we can go on with next request or end the stream.
2144 *
2145 * It is capable of compressing response data both in content-length mode and
2146 * in chunked mode. The state machines follows different flows depending on
2147 * whether content-length and chunked modes are used, since there are no
2148 * trailers in content-length :
2149 *
2150 * chk-mode cl-mode
2151 * ,----- BODY -----.
2152 * / \
2153 * V size > 0 V chk-mode
2154 * .--> SIZE -------------> DATA -------------> CRLF
2155 * | | size == 0 | last byte |
2156 * | v final crlf v inspected |
2157 * | TRAILERS -----------> DONE |
2158 * | |
2159 * `----------------------------------------------'
2160 *
2161 * Compression only happens in the DATA state, and must be flushed in final
2162 * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding
2163 * is performed at once on final states for all bytes parsed, or when leaving
2164 * on missing data.
2165 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002166int http_response_forward_body(struct stream *s, struct channel *res, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +02002167{
2168 struct session *sess = s->sess;
2169 struct http_txn *txn = s->txn;
2170 struct http_msg *msg = &s->txn->rsp;
Christopher Faulet9768c262018-10-22 09:34:31 +02002171 struct htx *htx;
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002172 int ret;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002173
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002174 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002175
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002176 htx = htxbuf(&res->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002177
Christopher Fauletb9a92f32019-09-09 10:15:21 +02002178 if (htx->flags & HTX_FL_PARSING_ERROR)
2179 goto return_bad_res;
2180 if (htx->flags & HTX_FL_PROCESSING_ERROR)
2181 goto return_int_err;
2182
Christopher Faulete0768eb2018-10-03 16:38:02 +02002183 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Christopher Fauletf2824e62018-10-01 12:12:37 +02002184 ((res->flags & CF_SHUTW) && (res->to_forward || co_data(res)))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02002185 /* Output closed while we were sending data. We must abort and
2186 * wake the other side up.
2187 */
Christopher Faulete0768eb2018-10-03 16:38:02 +02002188 msg->msg_state = HTTP_MSG_ERROR;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002189 http_end_response(s);
2190 http_end_request(s);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002191 DBG_TRACE_DEVEL("leaving on error",
2192 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002193 return 1;
2194 }
2195
Christopher Faulet9768c262018-10-22 09:34:31 +02002196 if (msg->msg_state == HTTP_MSG_BODY)
2197 msg->msg_state = HTTP_MSG_DATA;
2198
Christopher Faulete0768eb2018-10-03 16:38:02 +02002199 /* in most states, we should abort in case of early close */
2200 channel_auto_close(res);
2201
Christopher Faulete0768eb2018-10-03 16:38:02 +02002202 if (res->to_forward) {
Christopher Faulet66af0b22019-03-22 14:54:52 +01002203 if (res->to_forward == CHN_INFINITE_FORWARD) {
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002204 if (res->flags & CF_EOI)
2205 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet66af0b22019-03-22 14:54:52 +01002206 }
2207 else {
2208 /* We can't process the buffer's contents yet */
2209 res->flags |= CF_WAKE_WRITE;
2210 goto missing_data_or_waiting;
2211 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02002212 }
2213
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002214 if (msg->msg_state >= HTTP_MSG_ENDING)
2215 goto ending;
2216
Christopher Fauletc75668e2020-12-07 18:10:32 +01002217 if ((txn->meth == HTTP_METH_CONNECT && txn->status >= 200 && txn->status < 300) || txn->status == 101 ||
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002218 (!(msg->flags & HTTP_MSGF_XFER_LEN) && !HAS_RSP_DATA_FILTERS(s))) {
2219 msg->msg_state = HTTP_MSG_ENDING;
2220 goto ending;
2221 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002222
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002223 /* Forward input data. We get it by removing all outgoing data not
2224 * forwarded yet from HTX data size. If there are some data filters, we
2225 * let them decide the amount of data to forward.
Christopher Faulet9768c262018-10-22 09:34:31 +02002226 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002227 if (HAS_RSP_DATA_FILTERS(s)) {
2228 ret = flt_http_payload(s, msg, htx->data);
2229 if (ret < 0)
2230 goto return_bad_res;
Christopher Faulet421e7692019-06-13 11:16:45 +02002231 c_adv(res, ret);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002232 }
2233 else {
Christopher Faulet421e7692019-06-13 11:16:45 +02002234 c_adv(res, htx->data - co_data(res));
Christopher Faulet66af0b22019-03-22 14:54:52 +01002235 if (msg->flags & HTTP_MSGF_XFER_LEN)
2236 channel_htx_forward_forever(res, htx);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002237 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002238
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002239 if (htx->data != co_data(res))
2240 goto missing_data_or_waiting;
2241
2242 if (!(msg->flags & HTTP_MSGF_XFER_LEN) && res->flags & CF_SHUTR) {
2243 msg->msg_state = HTTP_MSG_ENDING;
2244 goto ending;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002245 }
2246
Christopher Faulet9768c262018-10-22 09:34:31 +02002247 /* Check if the end-of-message is reached and if so, switch the message
Christopher Fauletd20fdb02019-06-13 16:43:22 +02002248 * in HTTP_MSG_ENDING state. Then if all data was marked to be
2249 * forwarded, set the state to HTTP_MSG_DONE.
Christopher Faulet9768c262018-10-22 09:34:31 +02002250 */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002251 if (!(htx->flags & HTX_FL_EOM))
Christopher Faulet9768c262018-10-22 09:34:31 +02002252 goto missing_data_or_waiting;
2253
Christopher Fauletd20fdb02019-06-13 16:43:22 +02002254 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet9768c262018-10-22 09:34:31 +02002255
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002256 ending:
Christopher Faulet2151cdd2020-07-22 16:34:59 +02002257 res->flags &= ~CF_EXPECT_MORE; /* no more data are expected */
2258
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002259 /* other states, ENDING...TUNNEL */
2260 if (msg->msg_state >= HTTP_MSG_DONE)
2261 goto done;
Christopher Faulet9768c262018-10-22 09:34:31 +02002262
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002263 if (HAS_RSP_DATA_FILTERS(s)) {
2264 ret = flt_http_end(s, msg);
2265 if (ret <= 0) {
2266 if (!ret)
2267 goto missing_data_or_waiting;
2268 goto return_bad_res;
2269 }
2270 }
2271
Christopher Fauletc75668e2020-12-07 18:10:32 +01002272 if ((txn->meth == HTTP_METH_CONNECT && txn->status >= 200 && txn->status < 300) || txn->status == 101 ||
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002273 !(msg->flags & HTTP_MSGF_XFER_LEN)) {
2274 msg->msg_state = HTTP_MSG_TUNNEL;
2275 goto ending;
2276 }
2277 else {
2278 msg->msg_state = HTTP_MSG_DONE;
2279 res->to_forward = 0;
2280 }
2281
2282 done:
2283
2284 channel_dont_close(res);
2285
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002286 http_end_response(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002287 if (!(res->analysers & an_bit)) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002288 http_end_request(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002289 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
2290 if (res->flags & CF_SHUTW) {
2291 /* response errors are most likely due to the
2292 * client aborting the transfer. */
Christopher Faulet93e02d82019-03-08 14:18:50 +01002293 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002294 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02002295 goto return_bad_res;
2296 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002297 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002298 return 1;
2299 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002300 DBG_TRACE_DEVEL("waiting for the end of the HTTP txn",
2301 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002302 return 0;
2303
2304 missing_data_or_waiting:
2305 if (res->flags & CF_SHUTW)
Christopher Faulet93e02d82019-03-08 14:18:50 +01002306 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002307
2308 /* stop waiting for data if the input is closed before the end. If the
2309 * client side was already closed, it means that the client has aborted,
2310 * so we don't want to count this as a server abort. Otherwise it's a
2311 * server abort.
2312 */
Christopher Fauletd20fdb02019-06-13 16:43:22 +02002313 if (msg->msg_state < HTTP_MSG_ENDING && res->flags & CF_SHUTR) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02002314 if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))
Christopher Faulet93e02d82019-03-08 14:18:50 +01002315 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002316 /* If we have some pending data, we continue the processing */
Christopher Faulet93e02d82019-03-08 14:18:50 +01002317 if (htx_is_empty(htx))
2318 goto return_srv_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002319 }
2320
Christopher Faulete0768eb2018-10-03 16:38:02 +02002321 /* When TE: chunked is used, we need to get there again to parse
2322 * remaining chunks even if the server has closed, so we don't want to
Christopher Faulet9768c262018-10-22 09:34:31 +02002323 * set CF_DONTCLOSE. Similarly when there is a content-leng or if there
2324 * are filters registered on the stream, we don't want to forward a
2325 * close
Christopher Faulete0768eb2018-10-03 16:38:02 +02002326 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002327 if ((msg->flags & HTTP_MSGF_XFER_LEN) || HAS_RSP_DATA_FILTERS(s))
Christopher Faulete0768eb2018-10-03 16:38:02 +02002328 channel_dont_close(res);
2329
2330 /* We know that more data are expected, but we couldn't send more that
2331 * what we did. So we always set the CF_EXPECT_MORE flag so that the
2332 * system knows it must not set a PUSH on this first part. Interactive
2333 * modes are already handled by the stream sock layer. We must not do
2334 * this in content-length mode because it could present the MSG_MORE
2335 * flag with the last block of forwarded data, which would cause an
2336 * additional delay to be observed by the receiver.
2337 */
Christopher Faulet2151cdd2020-07-22 16:34:59 +02002338 if (HAS_RSP_DATA_FILTERS(s))
Christopher Faulete0768eb2018-10-03 16:38:02 +02002339 res->flags |= CF_EXPECT_MORE;
2340
2341 /* the stream handler will take care of timeouts and errors */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002342 DBG_TRACE_DEVEL("waiting for more data to forward",
2343 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002344 return 0;
2345
Christopher Faulet93e02d82019-03-08 14:18:50 +01002346 return_srv_abort:
2347 _HA_ATOMIC_ADD(&sess->fe->fe_counters.srv_aborts, 1);
2348 _HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1);
William Lallemand36119de2021-03-08 15:26:48 +01002349 if (sess->listener && sess->listener->counters)
Christopher Fauletcff0f732019-12-16 16:13:44 +01002350 _HA_ATOMIC_ADD(&sess->listener->counters->srv_aborts, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002351 if (objt_server(s->target))
Christopher Fauletcff0f732019-12-16 16:13:44 +01002352 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.srv_aborts, 1);
Willy Tarreau826f3ab2021-02-10 12:07:15 +01002353 stream_inc_http_fail_ctr(s);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002354 if (!(s->flags & SF_ERR_MASK))
2355 s->flags |= SF_ERR_SRVCL;
2356 goto return_error;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002357
Christopher Faulet93e02d82019-03-08 14:18:50 +01002358 return_cli_abort:
2359 _HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
2360 _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
William Lallemand36119de2021-03-08 15:26:48 +01002361 if (sess->listener && sess->listener->counters)
Christopher Fauletcff0f732019-12-16 16:13:44 +01002362 _HA_ATOMIC_ADD(&sess->listener->counters->cli_aborts, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002363 if (objt_server(s->target))
Christopher Fauletcff0f732019-12-16 16:13:44 +01002364 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.cli_aborts, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002365 if (!(s->flags & SF_ERR_MASK))
2366 s->flags |= SF_ERR_CLICL;
2367 goto return_error;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002368
Christopher Fauletb9a92f32019-09-09 10:15:21 +02002369 return_int_err:
Christopher Fauletcff0f732019-12-16 16:13:44 +01002370 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002371 _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1);
William Lallemand36119de2021-03-08 15:26:48 +01002372 if (sess->listener && sess->listener->counters)
Christopher Fauletcff0f732019-12-16 16:13:44 +01002373 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002374 if (objt_server(s->target))
2375 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.internal_errors, 1);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02002376 if (!(s->flags & SF_ERR_MASK))
2377 s->flags |= SF_ERR_INTERNAL;
2378 goto return_error;
2379
Christopher Faulet93e02d82019-03-08 14:18:50 +01002380 return_bad_res:
2381 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
2382 if (objt_server(s->target)) {
Christopher Fauletcff0f732019-12-16 16:13:44 +01002383 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002384 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_RSP);
2385 }
Willy Tarreau826f3ab2021-02-10 12:07:15 +01002386 stream_inc_http_fail_ctr(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002387 if (!(s->flags & SF_ERR_MASK))
Christopher Faulet93e02d82019-03-08 14:18:50 +01002388 s->flags |= SF_ERR_SRVCL;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002389 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +02002390
Christopher Faulet93e02d82019-03-08 14:18:50 +01002391 return_error:
Christopher Faulete0768eb2018-10-03 16:38:02 +02002392 /* don't send any error message as we're in the body */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002393 http_reply_and_close(s, txn->status, NULL);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002394 res->analysers &= AN_RES_FLT_END;
2395 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 +02002396 if (!(s->flags & SF_FINST_MASK))
2397 s->flags |= SF_FINST_D;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002398 DBG_TRACE_DEVEL("leaving on error",
2399 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002400 return 0;
2401}
2402
Christopher Fauletf2824e62018-10-01 12:12:37 +02002403/* Perform an HTTP redirect based on the information in <rule>. The function
Christopher Faulet99daf282018-11-28 22:58:13 +01002404 * returns zero on success, or zero in case of a, irrecoverable error such
Christopher Fauletf2824e62018-10-01 12:12:37 +02002405 * as too large a request to build a valid response.
2406 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002407int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn)
Christopher Fauletf2824e62018-10-01 12:12:37 +02002408{
Christopher Faulet99daf282018-11-28 22:58:13 +01002409 struct channel *req = &s->req;
2410 struct channel *res = &s->res;
2411 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01002412 struct htx_sl *sl;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002413 struct buffer *chunk;
Christopher Faulet99daf282018-11-28 22:58:13 +01002414 struct ist status, reason, location;
2415 unsigned int flags;
Christopher Faulet08e66462019-05-23 16:44:59 +02002416 int close = 0; /* Try to keep the connection alive byt default */
Christopher Fauletf2824e62018-10-01 12:12:37 +02002417
2418 chunk = alloc_trash_chunk();
Christopher Fauletb8a53712019-12-16 11:29:38 +01002419 if (!chunk) {
2420 if (!(s->flags & SF_ERR_MASK))
2421 s->flags |= SF_ERR_RESOURCE;
Christopher Faulet99daf282018-11-28 22:58:13 +01002422 goto fail;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002423 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002424
Christopher Faulet99daf282018-11-28 22:58:13 +01002425 /*
2426 * Create the location
2427 */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002428 htx = htxbuf(&req->buf);
Christopher Fauletf2824e62018-10-01 12:12:37 +02002429 switch(rule->type) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002430 case REDIRECT_TYPE_SCHEME: {
2431 struct http_hdr_ctx ctx;
2432 struct ist path, host;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002433
Christopher Faulet99daf282018-11-28 22:58:13 +01002434 host = ist("");
2435 ctx.blk = NULL;
2436 if (http_find_header(htx, ist("Host"), &ctx, 0))
2437 host = ctx.value;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002438
Christopher Faulet297fbb42019-05-13 14:41:27 +02002439 sl = http_get_stline(htx);
Christopher Faulet99daf282018-11-28 22:58:13 +01002440 path = http_get_path(htx_sl_req_uri(sl));
2441 /* build message using path */
Tim Duesterhused526372020-03-05 17:56:33 +01002442 if (isttest(path)) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002443 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2444 int qs = 0;
2445 while (qs < path.len) {
2446 if (*(path.ptr + qs) == '?') {
2447 path.len = qs;
2448 break;
2449 }
2450 qs++;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002451 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002452 }
2453 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002454 else
2455 path = ist("/");
Christopher Fauletf2824e62018-10-01 12:12:37 +02002456
Christopher Faulet99daf282018-11-28 22:58:13 +01002457 if (rule->rdr_str) { /* this is an old "redirect" rule */
2458 /* add scheme */
2459 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2460 goto fail;
2461 }
2462 else {
2463 /* add scheme with executing log format */
2464 chunk->data += build_logline(s, chunk->area + chunk->data,
2465 chunk->size - chunk->data,
2466 &rule->rdr_fmt);
2467 }
2468 /* add "://" + host + path */
2469 if (!chunk_memcat(chunk, "://", 3) ||
2470 !chunk_memcat(chunk, host.ptr, host.len) ||
2471 !chunk_memcat(chunk, path.ptr, path.len))
2472 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002473
Christopher Faulet99daf282018-11-28 22:58:13 +01002474 /* append a slash at the end of the location if needed and missing */
2475 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
2476 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
2477 if (chunk->data + 1 >= chunk->size)
2478 goto fail;
2479 chunk->area[chunk->data++] = '/';
2480 }
2481 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002482 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002483
Christopher Faulet99daf282018-11-28 22:58:13 +01002484 case REDIRECT_TYPE_PREFIX: {
2485 struct ist path;
2486
Christopher Faulet297fbb42019-05-13 14:41:27 +02002487 sl = http_get_stline(htx);
Christopher Faulet99daf282018-11-28 22:58:13 +01002488 path = http_get_path(htx_sl_req_uri(sl));
2489 /* build message using path */
Tim Duesterhused526372020-03-05 17:56:33 +01002490 if (isttest(path)) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002491 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2492 int qs = 0;
2493 while (qs < path.len) {
2494 if (*(path.ptr + qs) == '?') {
2495 path.len = qs;
2496 break;
2497 }
2498 qs++;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002499 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002500 }
2501 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002502 else
2503 path = ist("/");
Christopher Fauletf2824e62018-10-01 12:12:37 +02002504
Christopher Faulet99daf282018-11-28 22:58:13 +01002505 if (rule->rdr_str) { /* this is an old "redirect" rule */
2506 /* add prefix. Note that if prefix == "/", we don't want to
2507 * add anything, otherwise it makes it hard for the user to
2508 * configure a self-redirection.
2509 */
2510 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
2511 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2512 goto fail;
2513 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002514 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002515 else {
2516 /* add prefix with executing log format */
2517 chunk->data += build_logline(s, chunk->area + chunk->data,
2518 chunk->size - chunk->data,
2519 &rule->rdr_fmt);
2520 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002521
Christopher Faulet99daf282018-11-28 22:58:13 +01002522 /* add path */
2523 if (!chunk_memcat(chunk, path.ptr, path.len))
2524 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002525
Christopher Faulet99daf282018-11-28 22:58:13 +01002526 /* append a slash at the end of the location if needed and missing */
2527 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
2528 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
2529 if (chunk->data + 1 >= chunk->size)
2530 goto fail;
2531 chunk->area[chunk->data++] = '/';
2532 }
2533 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002534 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002535 case REDIRECT_TYPE_LOCATION:
2536 default:
2537 if (rule->rdr_str) { /* this is an old "redirect" rule */
2538 /* add location */
2539 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2540 goto fail;
2541 }
2542 else {
2543 /* add location with executing log format */
2544 chunk->data += build_logline(s, chunk->area + chunk->data,
2545 chunk->size - chunk->data,
2546 &rule->rdr_fmt);
2547 }
2548 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002549 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002550 location = ist2(chunk->area, chunk->data);
2551
2552 /*
2553 * Create the 30x response
2554 */
2555 switch (rule->code) {
2556 case 308:
2557 status = ist("308");
2558 reason = ist("Permanent Redirect");
2559 break;
2560 case 307:
2561 status = ist("307");
2562 reason = ist("Temporary Redirect");
2563 break;
2564 case 303:
2565 status = ist("303");
2566 reason = ist("See Other");
2567 break;
2568 case 301:
2569 status = ist("301");
2570 reason = ist("Moved Permanently");
2571 break;
2572 case 302:
2573 default:
2574 status = ist("302");
2575 reason = ist("Found");
2576 break;
2577 }
2578
Christopher Faulet08e66462019-05-23 16:44:59 +02002579 if (!(txn->req.flags & HTTP_MSGF_BODYLESS) && txn->req.msg_state != HTTP_MSG_DONE)
2580 close = 1;
2581
Christopher Faulet99daf282018-11-28 22:58:13 +01002582 htx = htx_from_buf(&res->buf);
Kevin Zhu96b36392020-01-07 09:42:55 +01002583 /* Trim any possible response */
2584 channel_htx_truncate(&s->res, htx);
Christopher Faulet99daf282018-11-28 22:58:13 +01002585 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
2586 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), status, reason);
2587 if (!sl)
2588 goto fail;
2589 sl->info.res.status = rule->code;
2590 s->txn->status = rule->code;
2591
Christopher Faulet08e66462019-05-23 16:44:59 +02002592 if (close && !htx_add_header(htx, ist("Connection"), ist("close")))
2593 goto fail;
2594
2595 if (!htx_add_header(htx, ist("Content-length"), ist("0")) ||
Christopher Faulet99daf282018-11-28 22:58:13 +01002596 !htx_add_header(htx, ist("Location"), location))
2597 goto fail;
2598
2599 if (rule->code == 302 || rule->code == 303 || rule->code == 307) {
2600 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")))
2601 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002602 }
2603
2604 if (rule->cookie_len) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002605 if (!htx_add_header(htx, ist("Set-Cookie"), ist2(rule->cookie_str, rule->cookie_len)))
2606 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002607 }
2608
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002609 if (!htx_add_endof(htx, HTX_BLK_EOH))
Christopher Faulet99daf282018-11-28 22:58:13 +01002610 goto fail;
2611
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002612 htx->flags |= HTX_FL_EOM;
Kevin Zhu96b36392020-01-07 09:42:55 +01002613 htx_to_buf(htx, &res->buf);
Christopher Fauleta72a7e42020-01-28 09:28:11 +01002614 if (!http_forward_proxy_resp(s, 1))
2615 goto fail;
Christopher Faulet99daf282018-11-28 22:58:13 +01002616
Christopher Faulet60b33a52020-01-28 09:18:10 +01002617 if (rule->flags & REDIRECT_FLAG_FROM_REQ) {
2618 /* let's log the request time */
2619 s->logs.tv_request = now;
2620 req->analysers &= AN_REQ_FLT_END;
Christopher Faulet99daf282018-11-28 22:58:13 +01002621
Christopher Faulet60b33a52020-01-28 09:18:10 +01002622 if (s->sess->fe == s->be) /* report it if the request was intercepted by the frontend */
2623 _HA_ATOMIC_ADD(&s->sess->fe->fe_counters.intercepted_req, 1);
2624 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002625
2626 if (!(s->flags & SF_ERR_MASK))
2627 s->flags |= SF_ERR_LOCAL;
2628 if (!(s->flags & SF_FINST_MASK))
Christopher Faulet60b33a52020-01-28 09:18:10 +01002629 s->flags |= ((rule->flags & REDIRECT_FLAG_FROM_REQ) ? SF_FINST_R : SF_FINST_H);
Christopher Fauletf2824e62018-10-01 12:12:37 +02002630
Christopher Faulet99daf282018-11-28 22:58:13 +01002631 free_trash_chunk(chunk);
2632 return 1;
2633
2634 fail:
2635 /* If an error occurred, remove the incomplete HTTP response from the
2636 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01002637 channel_htx_truncate(res, htxbuf(&res->buf));
Christopher Fauletf2824e62018-10-01 12:12:37 +02002638 free_trash_chunk(chunk);
Christopher Faulet99daf282018-11-28 22:58:13 +01002639 return 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002640}
2641
Christopher Faulet92d34fe2019-12-17 09:20:34 +01002642/* Replace all headers matching the name <name>. The header value is replaced if
2643 * it matches the regex <re>. <str> is used for the replacement. If <full> is
2644 * set to 1, the full-line is matched and replaced. Otherwise, comma-separated
2645 * values are evaluated one by one. It returns 0 on success and -1 on error.
2646 */
2647int http_replace_hdrs(struct stream* s, struct htx *htx, struct ist name,
2648 const char *str, struct my_regex *re, int full)
Christopher Faulet72333522018-10-24 11:25:02 +02002649{
2650 struct http_hdr_ctx ctx;
2651 struct buffer *output = get_trash_chunk();
2652
Christopher Faulet72333522018-10-24 11:25:02 +02002653 ctx.blk = NULL;
Christopher Faulet92d34fe2019-12-17 09:20:34 +01002654 while (http_find_header(htx, name, &ctx, full)) {
Christopher Faulet72333522018-10-24 11:25:02 +02002655 if (!regex_exec_match2(re, ctx.value.ptr, ctx.value.len, MAX_MATCH, pmatch, 0))
2656 continue;
2657
2658 output->data = exp_replace(output->area, output->size, ctx.value.ptr, str, pmatch);
2659 if (output->data == -1)
2660 return -1;
2661 if (!http_replace_header_value(htx, &ctx, ist2(output->area, output->data)))
2662 return -1;
2663 }
2664 return 0;
2665}
2666
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002667/* This function executes one of the set-{method,path,query,uri} actions. It
2668 * takes the string from the variable 'replace' with length 'len', then modifies
2669 * the relevant part of the request line accordingly. Then it updates various
2670 * pointers to the next elements which were moved, and the total buffer length.
2671 * It finds the action to be performed in p[2], previously filled by function
2672 * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal
2673 * error, though this can be revisited when this code is finally exploited.
2674 *
2675 * 'action' can be '0' to replace method, '1' to replace path, '2' to replace
Christopher Faulet312294f2020-09-02 17:17:44 +02002676 * query string, 3 to replace uri or 4 to replace the path+query.
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002677 *
2678 * In query string case, the mark question '?' must be set at the start of the
2679 * string by the caller, event if the replacement query string is empty.
2680 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002681int http_req_replace_stline(int action, const char *replace, int len,
2682 struct proxy *px, struct stream *s)
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002683{
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002684 struct htx *htx = htxbuf(&s->req.buf);
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002685
2686 switch (action) {
2687 case 0: // method
2688 if (!http_replace_req_meth(htx, ist2(replace, len)))
2689 return -1;
2690 break;
2691
2692 case 1: // path
Christopher Fauletb8ce5052020-08-31 16:11:57 +02002693 if (!http_replace_req_path(htx, ist2(replace, len), 0))
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002694 return -1;
2695 break;
2696
2697 case 2: // query
2698 if (!http_replace_req_query(htx, ist2(replace, len)))
2699 return -1;
2700 break;
2701
2702 case 3: // uri
2703 if (!http_replace_req_uri(htx, ist2(replace, len)))
2704 return -1;
2705 break;
2706
Christopher Faulet312294f2020-09-02 17:17:44 +02002707 case 4: // path + query
2708 if (!http_replace_req_path(htx, ist2(replace, len), 1))
2709 return -1;
2710 break;
2711
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002712 default:
2713 return -1;
2714 }
2715 return 0;
2716}
2717
2718/* This function replace the HTTP status code and the associated message. The
Christopher Faulete00d06c2019-12-16 17:18:42 +01002719 * variable <status> contains the new status code. This function never fails. It
2720 * returns 0 in case of success, -1 in case of internal error.
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002721 */
Christopher Faulet96bff762019-12-17 13:46:18 +01002722int http_res_set_status(unsigned int status, struct ist reason, struct stream *s)
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002723{
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002724 struct htx *htx = htxbuf(&s->res.buf);
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002725 char *res;
2726
2727 chunk_reset(&trash);
2728 res = ultoa_o(status, trash.area, trash.size);
2729 trash.data = res - trash.area;
2730
2731 /* Do we have a custom reason format string? */
Tim Duesterhuse296d3e2020-03-05 17:56:31 +01002732 if (!isttest(reason)) {
Christopher Faulet96bff762019-12-17 13:46:18 +01002733 const char *str = http_get_reason(status);
Tim Duesterhusdcf753a2021-03-04 17:31:47 +01002734 reason = ist(str);
Christopher Faulet96bff762019-12-17 13:46:18 +01002735 }
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002736
Christopher Fauletbde2c4c2020-08-31 16:43:34 +02002737 if (!http_replace_res_status(htx, ist2(trash.area, trash.data), reason))
Christopher Faulete00d06c2019-12-16 17:18:42 +01002738 return -1;
2739 return 0;
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002740}
2741
Christopher Faulet3e964192018-10-24 11:39:23 +02002742/* Executes the http-request rules <rules> for stream <s>, proxy <px> and
2743 * transaction <txn>. Returns the verdict of the first rule that prevents
2744 * further processing of the request (auth, deny, ...), and defaults to
2745 * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or
2746 * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT
2747 * on txn->flags if it encounters a tarpit rule. If <deny_status> is not NULL
2748 * and a deny/tarpit rule is matched, it will be filled with this rule's deny
2749 * status.
2750 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002751static enum rule_result http_req_get_intercept_rule(struct proxy *px, struct list *rules,
Christopher Fauletb58f62b2020-01-13 16:40:13 +01002752 struct stream *s)
Christopher Faulet3e964192018-10-24 11:39:23 +02002753{
2754 struct session *sess = strm_sess(s);
2755 struct http_txn *txn = s->txn;
Christopher Faulet3e964192018-10-24 11:39:23 +02002756 struct act_rule *rule;
Christopher Faulet3e964192018-10-24 11:39:23 +02002757 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002758 int act_opts = 0;
Christopher Faulet3e964192018-10-24 11:39:23 +02002759
Christopher Faulet3e964192018-10-24 11:39:23 +02002760 /* If "the current_rule_list" match the executed rule list, we are in
2761 * resume condition. If a resume is needed it is always in the action
2762 * and never in the ACL or converters. In this case, we initialise the
2763 * current rule, and go to the action execution point.
2764 */
2765 if (s->current_rule) {
2766 rule = s->current_rule;
2767 s->current_rule = NULL;
2768 if (s->current_rule_list == rules)
2769 goto resume_execution;
2770 }
2771 s->current_rule_list = rules;
2772
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002773 /* start the ruleset evaluation in strict mode */
2774 txn->req.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01002775
Christopher Faulet3e964192018-10-24 11:39:23 +02002776 list_for_each_entry(rule, rules, list) {
2777 /* check optional condition */
2778 if (rule->cond) {
2779 int ret;
2780
2781 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
2782 ret = acl_pass(ret);
2783
2784 if (rule->cond->pol == ACL_COND_UNLESS)
2785 ret = !ret;
2786
2787 if (!ret) /* condition not matched */
2788 continue;
2789 }
2790
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002791 act_opts |= ACT_OPT_FIRST;
Christopher Faulet3e964192018-10-24 11:39:23 +02002792 resume_execution:
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002793 /* Always call the action function if defined */
2794 if (rule->action_ptr) {
2795 if ((s->req.flags & CF_READ_ERROR) ||
2796 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
2797 (px->options & PR_O_ABRT_CLOSE)))
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002798 act_opts |= ACT_OPT_FINAL;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002799
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002800 switch (rule->action_ptr(rule, px, sess, s, act_opts)) {
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002801 case ACT_RET_CONT:
2802 break;
2803 case ACT_RET_STOP:
2804 rule_ret = HTTP_RULE_RES_STOP;
2805 goto end;
2806 case ACT_RET_YIELD:
2807 s->current_rule = rule;
2808 rule_ret = HTTP_RULE_RES_YIELD;
2809 goto end;
2810 case ACT_RET_ERR:
2811 rule_ret = HTTP_RULE_RES_ERROR;
2812 goto end;
2813 case ACT_RET_DONE:
2814 rule_ret = HTTP_RULE_RES_DONE;
2815 goto end;
2816 case ACT_RET_DENY:
Christopher Fauletb58f62b2020-01-13 16:40:13 +01002817 if (txn->status == -1)
2818 txn->status = 403;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002819 rule_ret = HTTP_RULE_RES_DENY;
2820 goto end;
2821 case ACT_RET_ABRT:
2822 rule_ret = HTTP_RULE_RES_ABRT;
2823 goto end;
2824 case ACT_RET_INV:
2825 rule_ret = HTTP_RULE_RES_BADREQ;
2826 goto end;
2827 }
2828 continue; /* eval the next rule */
2829 }
2830
2831 /* If not action function defined, check for known actions */
Christopher Faulet3e964192018-10-24 11:39:23 +02002832 switch (rule->action) {
2833 case ACT_ACTION_ALLOW:
2834 rule_ret = HTTP_RULE_RES_STOP;
2835 goto end;
2836
2837 case ACT_ACTION_DENY:
Christopher Faulet5cb513a2020-05-13 17:56:56 +02002838 txn->status = rule->arg.http_reply->status;
2839 txn->http_reply = rule->arg.http_reply;
Christopher Faulet3e964192018-10-24 11:39:23 +02002840 rule_ret = HTTP_RULE_RES_DENY;
2841 goto end;
2842
2843 case ACT_HTTP_REQ_TARPIT:
2844 txn->flags |= TX_CLTARPIT;
Christopher Faulet5cb513a2020-05-13 17:56:56 +02002845 txn->status = rule->arg.http_reply->status;
2846 txn->http_reply = rule->arg.http_reply;
Christopher Faulet3e964192018-10-24 11:39:23 +02002847 rule_ret = HTTP_RULE_RES_DENY;
2848 goto end;
2849
Christopher Faulet3e964192018-10-24 11:39:23 +02002850 case ACT_HTTP_REDIR:
Christopher Faulet90d22a82020-03-06 11:18:39 +01002851 rule_ret = HTTP_RULE_RES_ABRT;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002852 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
Christopher Faulet3a26bee2019-12-16 12:47:40 +01002853 rule_ret = HTTP_RULE_RES_ERROR;
Christopher Faulet3e964192018-10-24 11:39:23 +02002854 goto end;
2855
2856 case ACT_HTTP_SET_NICE:
Christopher Faulet96bff762019-12-17 13:46:18 +01002857 s->task->nice = rule->arg.http.i;
Christopher Faulet3e964192018-10-24 11:39:23 +02002858 break;
2859
2860 case ACT_HTTP_SET_TOS:
Christopher Faulet96bff762019-12-17 13:46:18 +01002861 conn_set_tos(objt_conn(sess->origin), rule->arg.http.i);
Christopher Faulet3e964192018-10-24 11:39:23 +02002862 break;
2863
2864 case ACT_HTTP_SET_MARK:
Christopher Faulet96bff762019-12-17 13:46:18 +01002865 conn_set_mark(objt_conn(sess->origin), rule->arg.http.i);
Christopher Faulet3e964192018-10-24 11:39:23 +02002866 break;
2867
2868 case ACT_HTTP_SET_LOGL:
Christopher Faulet96bff762019-12-17 13:46:18 +01002869 s->logs.level = rule->arg.http.i;
Christopher Faulet3e964192018-10-24 11:39:23 +02002870 break;
2871
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002872 /* other flags exists, but normally, they never be matched. */
Christopher Faulet3e964192018-10-24 11:39:23 +02002873 default:
2874 break;
2875 }
2876 }
2877
2878 end:
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002879 /* if the ruleset evaluation is finished reset the strict mode */
Christopher Faulet46f95542019-12-20 10:07:22 +01002880 if (rule_ret != HTTP_RULE_RES_YIELD)
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002881 txn->req.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01002882
Christopher Faulet3e964192018-10-24 11:39:23 +02002883 /* we reached the end of the rules, nothing to report */
2884 return rule_ret;
2885}
2886
2887/* Executes the http-response rules <rules> for stream <s> and proxy <px>. It
2888 * returns one of 5 possible statuses: HTTP_RULE_RES_CONT, HTTP_RULE_RES_STOP,
2889 * HTTP_RULE_RES_DONE, HTTP_RULE_RES_YIELD, or HTTP_RULE_RES_BADREQ. If *CONT
2890 * is returned, the process can continue the evaluation of next rule list. If
2891 * *STOP or *DONE is returned, the process must stop the evaluation. If *BADREQ
2892 * is returned, it means the operation could not be processed and a server error
Christopher Fauleta53abad2020-05-13 08:12:22 +02002893 * must be returned. If *YIELD is returned, the caller must call again the
2894 * function with the same context.
Christopher Faulet3e964192018-10-24 11:39:23 +02002895 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002896static enum rule_result http_res_get_intercept_rule(struct proxy *px, struct list *rules,
2897 struct stream *s)
Christopher Faulet3e964192018-10-24 11:39:23 +02002898{
2899 struct session *sess = strm_sess(s);
2900 struct http_txn *txn = s->txn;
Christopher Faulet3e964192018-10-24 11:39:23 +02002901 struct act_rule *rule;
Christopher Faulet3e964192018-10-24 11:39:23 +02002902 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002903 int act_opts = 0;
Christopher Faulet3e964192018-10-24 11:39:23 +02002904
Christopher Faulet3e964192018-10-24 11:39:23 +02002905 /* If "the current_rule_list" match the executed rule list, we are in
2906 * resume condition. If a resume is needed it is always in the action
2907 * and never in the ACL or converters. In this case, we initialise the
2908 * current rule, and go to the action execution point.
2909 */
2910 if (s->current_rule) {
2911 rule = s->current_rule;
2912 s->current_rule = NULL;
2913 if (s->current_rule_list == rules)
2914 goto resume_execution;
2915 }
2916 s->current_rule_list = rules;
2917
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002918 /* start the ruleset evaluation in strict mode */
2919 txn->rsp.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01002920
Christopher Faulet3e964192018-10-24 11:39:23 +02002921 list_for_each_entry(rule, rules, list) {
2922 /* check optional condition */
2923 if (rule->cond) {
2924 int ret;
2925
2926 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
2927 ret = acl_pass(ret);
2928
2929 if (rule->cond->pol == ACL_COND_UNLESS)
2930 ret = !ret;
2931
2932 if (!ret) /* condition not matched */
2933 continue;
2934 }
2935
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002936 act_opts |= ACT_OPT_FIRST;
Christopher Faulet3e964192018-10-24 11:39:23 +02002937resume_execution:
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002938
2939 /* Always call the action function if defined */
2940 if (rule->action_ptr) {
2941 if ((s->req.flags & CF_READ_ERROR) ||
2942 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
2943 (px->options & PR_O_ABRT_CLOSE)))
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002944 act_opts |= ACT_OPT_FINAL;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002945
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002946 switch (rule->action_ptr(rule, px, sess, s, act_opts)) {
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002947 case ACT_RET_CONT:
2948 break;
2949 case ACT_RET_STOP:
2950 rule_ret = HTTP_RULE_RES_STOP;
2951 goto end;
2952 case ACT_RET_YIELD:
2953 s->current_rule = rule;
2954 rule_ret = HTTP_RULE_RES_YIELD;
2955 goto end;
2956 case ACT_RET_ERR:
2957 rule_ret = HTTP_RULE_RES_ERROR;
2958 goto end;
2959 case ACT_RET_DONE:
2960 rule_ret = HTTP_RULE_RES_DONE;
2961 goto end;
2962 case ACT_RET_DENY:
Christopher Fauletb58f62b2020-01-13 16:40:13 +01002963 if (txn->status == -1)
2964 txn->status = 502;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002965 rule_ret = HTTP_RULE_RES_DENY;
2966 goto end;
2967 case ACT_RET_ABRT:
2968 rule_ret = HTTP_RULE_RES_ABRT;
2969 goto end;
2970 case ACT_RET_INV:
2971 rule_ret = HTTP_RULE_RES_BADREQ;
2972 goto end;
2973 }
2974 continue; /* eval the next rule */
2975 }
2976
2977 /* If not action function defined, check for known actions */
Christopher Faulet3e964192018-10-24 11:39:23 +02002978 switch (rule->action) {
2979 case ACT_ACTION_ALLOW:
2980 rule_ret = HTTP_RULE_RES_STOP; /* "allow" rules are OK */
2981 goto end;
2982
2983 case ACT_ACTION_DENY:
Christopher Faulet5cb513a2020-05-13 17:56:56 +02002984 txn->status = rule->arg.http_reply->status;
2985 txn->http_reply = rule->arg.http_reply;
Christopher Faulet3a26bee2019-12-16 12:47:40 +01002986 rule_ret = HTTP_RULE_RES_DENY;
Christopher Faulet3e964192018-10-24 11:39:23 +02002987 goto end;
2988
2989 case ACT_HTTP_SET_NICE:
Christopher Faulet96bff762019-12-17 13:46:18 +01002990 s->task->nice = rule->arg.http.i;
Christopher Faulet3e964192018-10-24 11:39:23 +02002991 break;
2992
2993 case ACT_HTTP_SET_TOS:
Christopher Faulet96bff762019-12-17 13:46:18 +01002994 conn_set_tos(objt_conn(sess->origin), rule->arg.http.i);
Christopher Faulet3e964192018-10-24 11:39:23 +02002995 break;
2996
2997 case ACT_HTTP_SET_MARK:
Christopher Faulet96bff762019-12-17 13:46:18 +01002998 conn_set_mark(objt_conn(sess->origin), rule->arg.http.i);
Christopher Faulet3e964192018-10-24 11:39:23 +02002999 break;
3000
3001 case ACT_HTTP_SET_LOGL:
Christopher Faulet96bff762019-12-17 13:46:18 +01003002 s->logs.level = rule->arg.http.i;
Christopher Faulet3e964192018-10-24 11:39:23 +02003003 break;
3004
Christopher Faulet3e964192018-10-24 11:39:23 +02003005 case ACT_HTTP_REDIR:
Christopher Faulet49c2a702020-03-06 15:44:37 +01003006 rule_ret = HTTP_RULE_RES_ABRT;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003007 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
Christopher Faulet3a26bee2019-12-16 12:47:40 +01003008 rule_ret = HTTP_RULE_RES_ERROR;
Christopher Faulet3e964192018-10-24 11:39:23 +02003009 goto end;
3010
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01003011 /* other flags exists, but normally, they never be matched. */
Christopher Faulet3e964192018-10-24 11:39:23 +02003012 default:
3013 break;
3014 }
3015 }
3016
3017 end:
Christopher Faulet1aea50e2020-01-17 16:03:53 +01003018 /* if the ruleset evaluation is finished reset the strict mode */
Christopher Faulet46f95542019-12-20 10:07:22 +01003019 if (rule_ret != HTTP_RULE_RES_YIELD)
Christopher Faulet1aea50e2020-01-17 16:03:53 +01003020 txn->rsp.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01003021
Christopher Faulet3e964192018-10-24 11:39:23 +02003022 /* we reached the end of the rules, nothing to report */
3023 return rule_ret;
3024}
3025
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01003026/* Executes backend and frontend http-after-response rules for the stream <s>,
3027 * in that order. it return 1 on success and 0 on error. It is the caller
3028 * responsibility to catch error or ignore it. If it catches it, this function
3029 * may be called a second time, for the internal error.
3030 */
3031int http_eval_after_res_rules(struct stream *s)
3032{
3033 struct session *sess = s->sess;
3034 enum rule_result ret = HTTP_RULE_RES_CONT;
3035
Christopher Faulet507479b2020-05-15 12:29:46 +02003036 /* Eval after-response ruleset only if the reply is not const */
3037 if (s->txn->flags & TX_CONST_REPLY)
3038 goto end;
3039
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01003040 /* prune the request variables if not already done and swap to the response variables. */
3041 if (s->vars_reqres.scope != SCOPE_RES) {
3042 if (!LIST_ISEMPTY(&s->vars_reqres.head))
3043 vars_prune(&s->vars_reqres, s->sess, s);
3044 vars_init(&s->vars_reqres, SCOPE_RES);
3045 }
3046
3047 ret = http_res_get_intercept_rule(s->be, &s->be->http_after_res_rules, s);
3048 if ((ret == HTTP_RULE_RES_CONT || ret == HTTP_RULE_RES_STOP) && sess->fe != s->be)
3049 ret = http_res_get_intercept_rule(sess->fe, &sess->fe->http_after_res_rules, s);
3050
Christopher Faulet507479b2020-05-15 12:29:46 +02003051 end:
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01003052 /* All other codes than CONTINUE, STOP or DONE are forbidden */
3053 return (ret == HTTP_RULE_RES_CONT || ret == HTTP_RULE_RES_STOP || ret == HTTP_RULE_RES_DONE);
3054}
3055
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003056/*
3057 * Manage client-side cookie. It can impact performance by about 2% so it is
3058 * desirable to call it only when needed. This code is quite complex because
3059 * of the multiple very crappy and ambiguous syntaxes we have to support. it
3060 * highly recommended not to touch this part without a good reason !
3061 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003062static void http_manage_client_side_cookies(struct stream *s, struct channel *req)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003063{
3064 struct session *sess = s->sess;
3065 struct http_txn *txn = s->txn;
3066 struct htx *htx;
3067 struct http_hdr_ctx ctx;
3068 char *hdr_beg, *hdr_end, *del_from;
3069 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
3070 int preserve_hdr;
3071
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003072 htx = htxbuf(&req->buf);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003073 ctx.blk = NULL;
3074 while (http_find_header(htx, ist("Cookie"), &ctx, 1)) {
Olivier Houchardf0f42382019-07-22 17:43:46 +02003075 int is_first = 1;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003076 del_from = NULL; /* nothing to be deleted */
3077 preserve_hdr = 0; /* assume we may kill the whole header */
3078
3079 /* Now look for cookies. Conforming to RFC2109, we have to support
3080 * attributes whose name begin with a '$', and associate them with
3081 * the right cookie, if we want to delete this cookie.
3082 * So there are 3 cases for each cookie read :
3083 * 1) it's a special attribute, beginning with a '$' : ignore it.
3084 * 2) it's a server id cookie that we *MAY* want to delete : save
3085 * some pointers on it (last semi-colon, beginning of cookie...)
3086 * 3) it's an application cookie : we *MAY* have to delete a previous
3087 * "special" cookie.
3088 * At the end of loop, if a "special" cookie remains, we may have to
3089 * remove it. If no application cookie persists in the header, we
3090 * *MUST* delete it.
3091 *
3092 * Note: RFC2965 is unclear about the processing of spaces around
3093 * the equal sign in the ATTR=VALUE form. A careful inspection of
3094 * the RFC explicitly allows spaces before it, and not within the
3095 * tokens (attrs or values). An inspection of RFC2109 allows that
3096 * too but section 10.1.3 lets one think that spaces may be allowed
3097 * after the equal sign too, resulting in some (rare) buggy
3098 * implementations trying to do that. So let's do what servers do.
3099 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
3100 * allowed quoted strings in values, with any possible character
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003101 * after a backslash, including control chars and delimiters, which
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003102 * causes parsing to become ambiguous. Browsers also allow spaces
3103 * within values even without quotes.
3104 *
3105 * We have to keep multiple pointers in order to support cookie
3106 * removal at the beginning, middle or end of header without
3107 * corrupting the header. All of these headers are valid :
3108 *
3109 * hdr_beg hdr_end
3110 * | |
3111 * v |
3112 * NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3 |
3113 * NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3 v
3114 * NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3
3115 * | | | | | | |
3116 * | | | | | | |
3117 * | | | | | | +--> next
3118 * | | | | | +----> val_end
3119 * | | | | +-----------> val_beg
3120 * | | | +--------------> equal
3121 * | | +----------------> att_end
3122 * | +---------------------> att_beg
3123 * +--------------------------> prev
3124 *
3125 */
3126 hdr_beg = ctx.value.ptr;
3127 hdr_end = hdr_beg + ctx.value.len;
3128 for (prev = hdr_beg; prev < hdr_end; prev = next) {
3129 /* Iterate through all cookies on this line */
3130
3131 /* find att_beg */
3132 att_beg = prev;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003133 if (!is_first)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003134 att_beg++;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003135 is_first = 0;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003136
3137 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
3138 att_beg++;
3139
3140 /* find att_end : this is the first character after the last non
3141 * space before the equal. It may be equal to hdr_end.
3142 */
3143 equal = att_end = att_beg;
3144 while (equal < hdr_end) {
3145 if (*equal == '=' || *equal == ',' || *equal == ';')
3146 break;
3147 if (HTTP_IS_SPHT(*equal++))
3148 continue;
3149 att_end = equal;
3150 }
3151
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003152 /* here, <equal> points to '=', a delimiter or the end. <att_end>
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003153 * is between <att_beg> and <equal>, both may be identical.
3154 */
3155 /* look for end of cookie if there is an equal sign */
3156 if (equal < hdr_end && *equal == '=') {
3157 /* look for the beginning of the value */
3158 val_beg = equal + 1;
3159 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
3160 val_beg++;
3161
3162 /* find the end of the value, respecting quotes */
3163 next = http_find_cookie_value_end(val_beg, hdr_end);
3164
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003165 /* make val_end point to the first white space or delimiter after the value */
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003166 val_end = next;
3167 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
3168 val_end--;
3169 }
3170 else
3171 val_beg = val_end = next = equal;
3172
3173 /* We have nothing to do with attributes beginning with
3174 * '$'. However, they will automatically be removed if a
3175 * header before them is removed, since they're supposed
3176 * to be linked together.
3177 */
3178 if (*att_beg == '$')
3179 continue;
3180
3181 /* Ignore cookies with no equal sign */
3182 if (equal == next) {
3183 /* This is not our cookie, so we must preserve it. But if we already
3184 * scheduled another cookie for removal, we cannot remove the
3185 * complete header, but we can remove the previous block itself.
3186 */
3187 preserve_hdr = 1;
3188 if (del_from != NULL) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003189 int delta = http_del_hdr_value(hdr_beg, hdr_end, &del_from, prev);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003190 val_end += delta;
3191 next += delta;
3192 hdr_end += delta;
3193 prev = del_from;
3194 del_from = NULL;
3195 }
3196 continue;
3197 }
3198
3199 /* if there are spaces around the equal sign, we need to
3200 * strip them otherwise we'll get trouble for cookie captures,
3201 * or even for rewrites. Since this happens extremely rarely,
3202 * it does not hurt performance.
3203 */
3204 if (unlikely(att_end != equal || val_beg > equal + 1)) {
3205 int stripped_before = 0;
3206 int stripped_after = 0;
3207
3208 if (att_end != equal) {
3209 memmove(att_end, equal, hdr_end - equal);
3210 stripped_before = (att_end - equal);
3211 equal += stripped_before;
3212 val_beg += stripped_before;
3213 }
3214
3215 if (val_beg > equal + 1) {
3216 memmove(equal + 1, val_beg, hdr_end + stripped_before - val_beg);
3217 stripped_after = (equal + 1) - val_beg;
3218 val_beg += stripped_after;
3219 stripped_before += stripped_after;
3220 }
3221
3222 val_end += stripped_before;
3223 next += stripped_before;
3224 hdr_end += stripped_before;
3225 }
3226 /* now everything is as on the diagram above */
3227
3228 /* First, let's see if we want to capture this cookie. We check
3229 * that we don't already have a client side cookie, because we
3230 * can only capture one. Also as an optimisation, we ignore
3231 * cookies shorter than the declared name.
3232 */
3233 if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL &&
3234 (val_end - att_beg >= sess->fe->capture_namelen) &&
3235 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
3236 int log_len = val_end - att_beg;
3237
3238 if ((txn->cli_cookie = pool_alloc(pool_head_capture)) == NULL) {
3239 ha_alert("HTTP logging : out of memory.\n");
3240 } else {
3241 if (log_len > sess->fe->capture_len)
3242 log_len = sess->fe->capture_len;
3243 memcpy(txn->cli_cookie, att_beg, log_len);
3244 txn->cli_cookie[log_len] = 0;
3245 }
3246 }
3247
3248 /* Persistence cookies in passive, rewrite or insert mode have the
3249 * following form :
3250 *
3251 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
3252 *
3253 * For cookies in prefix mode, the form is :
3254 *
3255 * Cookie: NAME=SRV~VALUE
3256 */
3257 if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
3258 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
3259 struct server *srv = s->be->srv;
3260 char *delim;
3261
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003262 /* if we're in cookie prefix mode, we'll search the delimiter so that we
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003263 * have the server ID between val_beg and delim, and the original cookie between
3264 * delim+1 and val_end. Otherwise, delim==val_end :
3265 *
3266 * hdr_beg
3267 * |
3268 * v
3269 * NAME=SRV; # in all but prefix modes
3270 * NAME=SRV~OPAQUE ; # in prefix mode
3271 * || || | |+-> next
3272 * || || | +--> val_end
3273 * || || +---------> delim
3274 * || |+------------> val_beg
3275 * || +-------------> att_end = equal
3276 * |+-----------------> att_beg
3277 * +------------------> prev
3278 *
3279 */
3280 if (s->be->ck_opts & PR_CK_PFX) {
3281 for (delim = val_beg; delim < val_end; delim++)
3282 if (*delim == COOKIE_DELIM)
3283 break;
3284 }
3285 else {
3286 char *vbar1;
3287 delim = val_end;
3288 /* Now check if the cookie contains a date field, which would
3289 * appear after a vertical bar ('|') just after the server name
3290 * and before the delimiter.
3291 */
3292 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
3293 if (vbar1) {
3294 /* OK, so left of the bar is the server's cookie and
3295 * right is the last seen date. It is a base64 encoded
3296 * 30-bit value representing the UNIX date since the
3297 * epoch in 4-second quantities.
3298 */
3299 int val;
3300 delim = vbar1++;
3301 if (val_end - vbar1 >= 5) {
3302 val = b64tos30(vbar1);
3303 if (val > 0)
3304 txn->cookie_last_date = val << 2;
3305 }
3306 /* look for a second vertical bar */
3307 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
3308 if (vbar1 && (val_end - vbar1 > 5)) {
3309 val = b64tos30(vbar1 + 1);
3310 if (val > 0)
3311 txn->cookie_first_date = val << 2;
3312 }
3313 }
3314 }
3315
3316 /* if the cookie has an expiration date and the proxy wants to check
3317 * it, then we do that now. We first check if the cookie is too old,
3318 * then only if it has expired. We detect strict overflow because the
3319 * time resolution here is not great (4 seconds). Cookies with dates
3320 * in the future are ignored if their offset is beyond one day. This
3321 * allows an admin to fix timezone issues without expiring everyone
3322 * and at the same time avoids keeping unwanted side effects for too
3323 * long.
3324 */
3325 if (txn->cookie_first_date && s->be->cookie_maxlife &&
3326 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) ||
3327 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
3328 txn->flags &= ~TX_CK_MASK;
3329 txn->flags |= TX_CK_OLD;
3330 delim = val_beg; // let's pretend we have not found the cookie
3331 txn->cookie_first_date = 0;
3332 txn->cookie_last_date = 0;
3333 }
3334 else if (txn->cookie_last_date && s->be->cookie_maxidle &&
3335 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) ||
3336 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
3337 txn->flags &= ~TX_CK_MASK;
3338 txn->flags |= TX_CK_EXPIRED;
3339 delim = val_beg; // let's pretend we have not found the cookie
3340 txn->cookie_first_date = 0;
3341 txn->cookie_last_date = 0;
3342 }
3343
3344 /* Here, we'll look for the first running server which supports the cookie.
3345 * This allows to share a same cookie between several servers, for example
3346 * to dedicate backup servers to specific servers only.
3347 * However, to prevent clients from sticking to cookie-less backup server
3348 * when they have incidentely learned an empty cookie, we simply ignore
3349 * empty cookies and mark them as invalid.
3350 * The same behaviour is applied when persistence must be ignored.
3351 */
3352 if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
3353 srv = NULL;
3354
3355 while (srv) {
3356 if (srv->cookie && (srv->cklen == delim - val_beg) &&
3357 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
3358 if ((srv->cur_state != SRV_ST_STOPPED) ||
3359 (s->be->options & PR_O_PERSIST) ||
3360 (s->flags & SF_FORCE_PRST)) {
3361 /* we found the server and we can use it */
3362 txn->flags &= ~TX_CK_MASK;
3363 txn->flags |= (srv->cur_state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
3364 s->flags |= SF_DIRECT | SF_ASSIGNED;
3365 s->target = &srv->obj_type;
3366 break;
3367 } else {
3368 /* we found a server, but it's down,
3369 * mark it as such and go on in case
3370 * another one is available.
3371 */
3372 txn->flags &= ~TX_CK_MASK;
3373 txn->flags |= TX_CK_DOWN;
3374 }
3375 }
3376 srv = srv->next;
3377 }
3378
3379 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
3380 /* no server matched this cookie or we deliberately skipped it */
3381 txn->flags &= ~TX_CK_MASK;
3382 if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
3383 txn->flags |= TX_CK_UNUSED;
3384 else
3385 txn->flags |= TX_CK_INVALID;
3386 }
3387
3388 /* depending on the cookie mode, we may have to either :
3389 * - delete the complete cookie if we're in insert+indirect mode, so that
3390 * the server never sees it ;
3391 * - remove the server id from the cookie value, and tag the cookie as an
Joseph Herlante9d5c722018-11-25 11:00:25 -08003392 * application cookie so that it does not get accidentally removed later,
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003393 * if we're in cookie prefix mode
3394 */
3395 if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
3396 int delta; /* negative */
3397
3398 memmove(val_beg, delim + 1, hdr_end - (delim + 1));
3399 delta = val_beg - (delim + 1);
3400 val_end += delta;
3401 next += delta;
3402 hdr_end += delta;
3403 del_from = NULL;
3404 preserve_hdr = 1; /* we want to keep this cookie */
3405 }
3406 else if (del_from == NULL &&
3407 (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
3408 del_from = prev;
3409 }
3410 }
3411 else {
3412 /* This is not our cookie, so we must preserve it. But if we already
3413 * scheduled another cookie for removal, we cannot remove the
3414 * complete header, but we can remove the previous block itself.
3415 */
3416 preserve_hdr = 1;
3417
3418 if (del_from != NULL) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003419 int delta = http_del_hdr_value(hdr_beg, hdr_end, &del_from, prev);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003420 if (att_beg >= del_from)
3421 att_beg += delta;
3422 if (att_end >= del_from)
3423 att_end += delta;
3424 val_beg += delta;
3425 val_end += delta;
3426 next += delta;
3427 hdr_end += delta;
3428 prev = del_from;
3429 del_from = NULL;
3430 }
3431 }
3432
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003433 } /* for each cookie */
3434
3435
3436 /* There are no more cookies on this line.
3437 * We may still have one (or several) marked for deletion at the
3438 * end of the line. We must do this now in two ways :
3439 * - if some cookies must be preserved, we only delete from the
3440 * mark to the end of line ;
3441 * - if nothing needs to be preserved, simply delete the whole header
3442 */
3443 if (del_from) {
3444 hdr_end = (preserve_hdr ? del_from : hdr_beg);
3445 }
3446 if ((hdr_end - hdr_beg) != ctx.value.len) {
Christopher Faulet3e2638e2019-06-18 09:49:16 +02003447 if (hdr_beg != hdr_end)
3448 htx_change_blk_value_len(htx, ctx.blk, hdr_end - hdr_beg);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003449 else
3450 http_remove_header(htx, &ctx);
3451 }
3452 } /* for each "Cookie header */
3453}
3454
3455/*
3456 * Manage server-side cookies. It can impact performance by about 2% so it is
3457 * desirable to call it only when needed. This function is also used when we
3458 * just need to know if there is a cookie (eg: for check-cache).
3459 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003460static void http_manage_server_side_cookies(struct stream *s, struct channel *res)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003461{
3462 struct session *sess = s->sess;
3463 struct http_txn *txn = s->txn;
3464 struct htx *htx;
3465 struct http_hdr_ctx ctx;
3466 struct server *srv;
3467 char *hdr_beg, *hdr_end;
3468 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau6f7a02a2019-04-15 21:49:49 +02003469 int is_cookie2 = 0;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003470
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003471 htx = htxbuf(&res->buf);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003472
3473 ctx.blk = NULL;
3474 while (1) {
Olivier Houchardf0f42382019-07-22 17:43:46 +02003475 int is_first = 1;
3476
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003477 if (!http_find_header(htx, ist("Set-Cookie"), &ctx, 1)) {
3478 if (!http_find_header(htx, ist("Set-Cookie2"), &ctx, 1))
3479 break;
3480 is_cookie2 = 1;
3481 }
3482
3483 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
3484 * <prev> points to the colon.
3485 */
3486 txn->flags |= TX_SCK_PRESENT;
3487
3488 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
3489 * check-cache is enabled) and we are not interested in checking
3490 * them. Warning, the cookie capture is declared in the frontend.
3491 */
3492 if (s->be->cookie_name == NULL && sess->fe->capture_name == NULL)
3493 break;
3494
3495 /* OK so now we know we have to process this response cookie.
3496 * The format of the Set-Cookie header is slightly different
3497 * from the format of the Cookie header in that it does not
3498 * support the comma as a cookie delimiter (thus the header
3499 * cannot be folded) because the Expires attribute described in
3500 * the original Netscape's spec may contain an unquoted date
3501 * with a comma inside. We have to live with this because
3502 * many browsers don't support Max-Age and some browsers don't
3503 * support quoted strings. However the Set-Cookie2 header is
3504 * clean.
3505 *
3506 * We have to keep multiple pointers in order to support cookie
3507 * removal at the beginning, middle or end of header without
3508 * corrupting the header (in case of set-cookie2). A special
3509 * pointer, <scav> points to the beginning of the set-cookie-av
3510 * fields after the first semi-colon. The <next> pointer points
3511 * either to the end of line (set-cookie) or next unquoted comma
3512 * (set-cookie2). All of these headers are valid :
3513 *
3514 * hdr_beg hdr_end
3515 * | |
3516 * v |
3517 * NAME1 = VALUE 1 ; Secure; Path="/" |
3518 * NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT v
3519 * NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT
3520 * NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard
3521 * | | | | | | | |
3522 * | | | | | | | +-> next
3523 * | | | | | | +------------> scav
3524 * | | | | | +--------------> val_end
3525 * | | | | +--------------------> val_beg
3526 * | | | +----------------------> equal
3527 * | | +------------------------> att_end
3528 * | +----------------------------> att_beg
3529 * +------------------------------> prev
3530 * -------------------------------> hdr_beg
3531 */
3532 hdr_beg = ctx.value.ptr;
3533 hdr_end = hdr_beg + ctx.value.len;
3534 for (prev = hdr_beg; prev < hdr_end; prev = next) {
3535
3536 /* Iterate through all cookies on this line */
3537
3538 /* find att_beg */
3539 att_beg = prev;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003540 if (!is_first)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003541 att_beg++;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003542 is_first = 0;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003543
3544 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
3545 att_beg++;
3546
3547 /* find att_end : this is the first character after the last non
3548 * space before the equal. It may be equal to hdr_end.
3549 */
3550 equal = att_end = att_beg;
3551
3552 while (equal < hdr_end) {
3553 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
3554 break;
3555 if (HTTP_IS_SPHT(*equal++))
3556 continue;
3557 att_end = equal;
3558 }
3559
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003560 /* here, <equal> points to '=', a delimiter or the end. <att_end>
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003561 * is between <att_beg> and <equal>, both may be identical.
3562 */
3563
3564 /* look for end of cookie if there is an equal sign */
3565 if (equal < hdr_end && *equal == '=') {
3566 /* look for the beginning of the value */
3567 val_beg = equal + 1;
3568 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
3569 val_beg++;
3570
3571 /* find the end of the value, respecting quotes */
3572 next = http_find_cookie_value_end(val_beg, hdr_end);
3573
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003574 /* make val_end point to the first white space or delimiter after the value */
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003575 val_end = next;
3576 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
3577 val_end--;
3578 }
3579 else {
3580 /* <equal> points to next comma, semi-colon or EOL */
3581 val_beg = val_end = next = equal;
3582 }
3583
3584 if (next < hdr_end) {
3585 /* Set-Cookie2 supports multiple cookies, and <next> points to
3586 * a colon or semi-colon before the end. So skip all attr-value
3587 * pairs and look for the next comma. For Set-Cookie, since
3588 * commas are permitted in values, skip to the end.
3589 */
3590 if (is_cookie2)
3591 next = http_find_hdr_value_end(next, hdr_end);
3592 else
3593 next = hdr_end;
3594 }
3595
3596 /* Now everything is as on the diagram above */
3597
3598 /* Ignore cookies with no equal sign */
3599 if (equal == val_end)
3600 continue;
3601
3602 /* If there are spaces around the equal sign, we need to
3603 * strip them otherwise we'll get trouble for cookie captures,
3604 * or even for rewrites. Since this happens extremely rarely,
3605 * it does not hurt performance.
3606 */
3607 if (unlikely(att_end != equal || val_beg > equal + 1)) {
3608 int stripped_before = 0;
3609 int stripped_after = 0;
3610
3611 if (att_end != equal) {
3612 memmove(att_end, equal, hdr_end - equal);
3613 stripped_before = (att_end - equal);
3614 equal += stripped_before;
3615 val_beg += stripped_before;
3616 }
3617
3618 if (val_beg > equal + 1) {
3619 memmove(equal + 1, val_beg, hdr_end + stripped_before - val_beg);
3620 stripped_after = (equal + 1) - val_beg;
3621 val_beg += stripped_after;
3622 stripped_before += stripped_after;
3623 }
3624
3625 val_end += stripped_before;
3626 next += stripped_before;
3627 hdr_end += stripped_before;
3628
Christopher Faulet3e2638e2019-06-18 09:49:16 +02003629 htx_change_blk_value_len(htx, ctx.blk, hdr_end - hdr_beg);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003630 ctx.value.len = hdr_end - hdr_beg;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003631 }
3632
3633 /* First, let's see if we want to capture this cookie. We check
3634 * that we don't already have a server side cookie, because we
3635 * can only capture one. Also as an optimisation, we ignore
3636 * cookies shorter than the declared name.
3637 */
3638 if (sess->fe->capture_name != NULL &&
3639 txn->srv_cookie == NULL &&
3640 (val_end - att_beg >= sess->fe->capture_namelen) &&
3641 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
3642 int log_len = val_end - att_beg;
3643 if ((txn->srv_cookie = pool_alloc(pool_head_capture)) == NULL) {
3644 ha_alert("HTTP logging : out of memory.\n");
3645 }
3646 else {
3647 if (log_len > sess->fe->capture_len)
3648 log_len = sess->fe->capture_len;
3649 memcpy(txn->srv_cookie, att_beg, log_len);
3650 txn->srv_cookie[log_len] = 0;
3651 }
3652 }
3653
3654 srv = objt_server(s->target);
3655 /* now check if we need to process it for persistence */
3656 if (!(s->flags & SF_IGNORE_PRST) &&
3657 (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
3658 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
3659 /* assume passive cookie by default */
3660 txn->flags &= ~TX_SCK_MASK;
3661 txn->flags |= TX_SCK_FOUND;
3662
3663 /* If the cookie is in insert mode on a known server, we'll delete
3664 * this occurrence because we'll insert another one later.
3665 * We'll delete it too if the "indirect" option is set and we're in
3666 * a direct access.
3667 */
3668 if (s->be->ck_opts & PR_CK_PSV) {
3669 /* The "preserve" flag was set, we don't want to touch the
3670 * server's cookie.
3671 */
3672 }
3673 else if ((srv && (s->be->ck_opts & PR_CK_INS)) ||
3674 ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) {
3675 /* this cookie must be deleted */
3676 if (prev == hdr_beg && next == hdr_end) {
3677 /* whole header */
3678 http_remove_header(htx, &ctx);
3679 /* note: while both invalid now, <next> and <hdr_end>
3680 * are still equal, so the for() will stop as expected.
3681 */
3682 } else {
3683 /* just remove the value */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003684 int delta = http_del_hdr_value(hdr_beg, hdr_end, &prev, next);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003685 next = prev;
3686 hdr_end += delta;
3687 }
3688 txn->flags &= ~TX_SCK_MASK;
3689 txn->flags |= TX_SCK_DELETED;
3690 /* and go on with next cookie */
3691 }
3692 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) {
3693 /* replace bytes val_beg->val_end with the cookie name associated
3694 * with this server since we know it.
3695 */
3696 int sliding, delta;
3697
3698 ctx.value = ist2(val_beg, val_end - val_beg);
3699 ctx.lws_before = ctx.lws_after = 0;
3700 http_replace_header_value(htx, &ctx, ist2(srv->cookie, srv->cklen));
3701 delta = srv->cklen - (val_end - val_beg);
3702 sliding = (ctx.value.ptr - val_beg);
3703 hdr_beg += sliding;
3704 val_beg += sliding;
3705 next += sliding + delta;
3706 hdr_end += sliding + delta;
3707
3708 txn->flags &= ~TX_SCK_MASK;
3709 txn->flags |= TX_SCK_REPLACED;
3710 }
3711 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) {
3712 /* insert the cookie name associated with this server
3713 * before existing cookie, and insert a delimiter between them..
3714 */
3715 int sliding, delta;
3716 ctx.value = ist2(val_beg, 0);
3717 ctx.lws_before = ctx.lws_after = 0;
3718 http_replace_header_value(htx, &ctx, ist2(srv->cookie, srv->cklen + 1));
3719 delta = srv->cklen + 1;
3720 sliding = (ctx.value.ptr - val_beg);
3721 hdr_beg += sliding;
3722 val_beg += sliding;
3723 next += sliding + delta;
3724 hdr_end += sliding + delta;
3725
3726 val_beg[srv->cklen] = COOKIE_DELIM;
3727 txn->flags &= ~TX_SCK_MASK;
3728 txn->flags |= TX_SCK_REPLACED;
3729 }
3730 }
3731 /* that's done for this cookie, check the next one on the same
3732 * line when next != hdr_end (only if is_cookie2).
3733 */
3734 }
3735 }
3736}
3737
Christopher Faulet25a02f62018-10-24 12:00:25 +02003738/*
3739 * Parses the Cache-Control and Pragma request header fields to determine if
3740 * the request may be served from the cache and/or if it is cacheable. Updates
3741 * s->txn->flags.
3742 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003743void http_check_request_for_cacheability(struct stream *s, struct channel *req)
Christopher Faulet25a02f62018-10-24 12:00:25 +02003744{
3745 struct http_txn *txn = s->txn;
3746 struct htx *htx;
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003747 struct http_hdr_ctx ctx = { .blk = NULL };
3748 int pragma_found, cc_found;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003749
3750 if ((txn->flags & (TX_CACHEABLE|TX_CACHE_IGNORE)) == TX_CACHE_IGNORE)
3751 return; /* nothing more to do here */
3752
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003753 htx = htxbuf(&req->buf);
Christopher Faulet25a02f62018-10-24 12:00:25 +02003754 pragma_found = cc_found = 0;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003755
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003756 /* Check "pragma" header for HTTP/1.0 compatibility. */
3757 if (http_find_header(htx, ist("pragma"), &ctx, 1)) {
3758 if (isteqi(ctx.value, ist("no-cache"))) {
3759 pragma_found = 1;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003760 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003761 }
Christopher Faulet25a02f62018-10-24 12:00:25 +02003762
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003763 ctx.blk = NULL;
3764 /* Don't use the cache and don't try to store if we found the
3765 * Authorization header */
3766 if (http_find_header(htx, ist("authorization"), &ctx, 1)) {
3767 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3768 txn->flags |= TX_CACHE_IGNORE;
3769 }
Christopher Faulet25a02f62018-10-24 12:00:25 +02003770
Christopher Faulet25a02f62018-10-24 12:00:25 +02003771
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003772 /* Look for "cache-control" header and iterate over all the values
3773 * until we find one that specifies that caching is possible or not. */
3774 ctx.blk = NULL;
3775 while (http_find_header(htx, ist("cache-control"), &ctx, 0)) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003776 cc_found = 1;
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003777 /* We don't check the values after max-age, max-stale nor min-fresh,
3778 * we simply don't use the cache when they're specified. */
3779 if (istmatchi(ctx.value, ist("max-age")) ||
3780 istmatchi(ctx.value, ist("no-cache")) ||
3781 istmatchi(ctx.value, ist("max-stale")) ||
3782 istmatchi(ctx.value, ist("min-fresh"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003783 txn->flags |= TX_CACHE_IGNORE;
3784 continue;
3785 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003786 if (istmatchi(ctx.value, ist("no-store"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003787 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3788 continue;
3789 }
3790 }
3791
3792 /* RFC7234#5.4:
3793 * When the Cache-Control header field is also present and
3794 * understood in a request, Pragma is ignored.
3795 * When the Cache-Control header field is not present in a
3796 * request, caches MUST consider the no-cache request
3797 * pragma-directive as having the same effect as if
3798 * "Cache-Control: no-cache" were present.
3799 */
3800 if (!cc_found && pragma_found)
3801 txn->flags |= TX_CACHE_IGNORE;
3802}
3803
3804/*
3805 * Check if response is cacheable or not. Updates s->txn->flags.
3806 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003807void http_check_response_for_cacheability(struct stream *s, struct channel *res)
Christopher Faulet25a02f62018-10-24 12:00:25 +02003808{
3809 struct http_txn *txn = s->txn;
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003810 struct http_hdr_ctx ctx = { .blk = NULL };
Christopher Faulet25a02f62018-10-24 12:00:25 +02003811 struct htx *htx;
Remi Tricot-Le Bretoncc9bf2e2020-11-12 11:14:41 +01003812 int has_freshness_info = 0;
3813 int has_validator = 0;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003814
3815 if (txn->status < 200) {
3816 /* do not try to cache interim responses! */
3817 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3818 return;
3819 }
3820
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003821 htx = htxbuf(&res->buf);
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003822 /* Check "pragma" header for HTTP/1.0 compatibility. */
3823 if (http_find_header(htx, ist("pragma"), &ctx, 1)) {
3824 if (isteqi(ctx.value, ist("no-cache"))) {
3825 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3826 return;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003827 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003828 }
Christopher Faulet25a02f62018-10-24 12:00:25 +02003829
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003830 /* Look for "cache-control" header and iterate over all the values
3831 * until we find one that specifies that caching is possible or not. */
3832 ctx.blk = NULL;
3833 while (http_find_header(htx, ist("cache-control"), &ctx, 0)) {
3834 if (isteqi(ctx.value, ist("public"))) {
3835 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003836 continue;
3837 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003838 if (isteqi(ctx.value, ist("private")) ||
3839 isteqi(ctx.value, ist("no-cache")) ||
3840 isteqi(ctx.value, ist("no-store")) ||
3841 isteqi(ctx.value, ist("max-age=0")) ||
3842 isteqi(ctx.value, ist("s-maxage=0"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003843 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003844 continue;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003845 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003846 /* We might have a no-cache="set-cookie" form. */
3847 if (istmatchi(ctx.value, ist("no-cache=\"set-cookie"))) {
3848 txn->flags &= ~TX_CACHE_COOK;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003849 continue;
3850 }
Remi Tricot-Le Bretoncc9bf2e2020-11-12 11:14:41 +01003851
3852 if (istmatchi(ctx.value, ist("s-maxage")) ||
3853 istmatchi(ctx.value, ist("max-age"))) {
3854 has_freshness_info = 1;
3855 continue;
3856 }
3857 }
3858
3859 /* If no freshness information could be found in Cache-Control values,
3860 * look for an Expires header. */
3861 if (!has_freshness_info) {
3862 ctx.blk = NULL;
3863 has_freshness_info = http_find_header(htx, ist("expires"), &ctx, 0);
Christopher Faulet25a02f62018-10-24 12:00:25 +02003864 }
Remi Tricot-Le Bretoncc9bf2e2020-11-12 11:14:41 +01003865
3866 /* If no freshness information could be found in Cache-Control or Expires
3867 * values, look for an explicit validator. */
3868 if (!has_freshness_info) {
3869 ctx.blk = NULL;
3870 has_validator = 1;
3871 if (!http_find_header(htx, ist("etag"), &ctx, 0)) {
3872 ctx.blk = NULL;
3873 if (!http_find_header(htx, ist("last-modified"), &ctx, 0))
3874 has_validator = 0;
3875 }
3876 }
3877
3878 /* We won't store an entry that has neither a cache validator nor an
3879 * explicit expiration time, as suggested in RFC 7234#3. */
3880 if (!has_freshness_info && !has_validator)
3881 txn->flags |= TX_CACHE_IGNORE;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003882}
3883
Christopher Faulet377c5a52018-10-24 21:21:30 +02003884/*
3885 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
3886 * for the current backend.
3887 *
3888 * It is assumed that the request is either a HEAD, GET, or POST and that the
3889 * uri_auth field is valid.
3890 *
3891 * Returns 1 if stats should be provided, otherwise 0.
3892 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003893static int http_stats_check_uri(struct stream *s, struct http_txn *txn, struct proxy *backend)
Christopher Faulet377c5a52018-10-24 21:21:30 +02003894{
3895 struct uri_auth *uri_auth = backend->uri_auth;
3896 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003897 struct htx_sl *sl;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003898 struct ist uri;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003899
3900 if (!uri_auth)
3901 return 0;
3902
3903 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
3904 return 0;
3905
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003906 htx = htxbuf(&s->req.buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02003907 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003908 uri = htx_sl_req_uri(sl);
Willy Tarreau1eb3b482019-10-31 15:50:28 +01003909 if (*uri_auth->uri_prefix == '/')
3910 uri = http_get_path(uri);
Christopher Faulet377c5a52018-10-24 21:21:30 +02003911
3912 /* check URI size */
3913 if (uri_auth->uri_len > uri.len)
3914 return 0;
3915
3916 if (memcmp(uri.ptr, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
3917 return 0;
3918
3919 return 1;
3920}
3921
3922/* This function prepares an applet to handle the stats. It can deal with the
3923 * "100-continue" expectation, check that admin rules are met for POST requests,
3924 * and program a response message if something was unexpected. It cannot fail
3925 * and always relies on the stats applet to complete the job. It does not touch
3926 * analysers nor counters, which are left to the caller. It does not touch
3927 * s->target which is supposed to already point to the stats applet. The caller
3928 * is expected to have already assigned an appctx to the stream.
3929 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003930static int http_handle_stats(struct stream *s, struct channel *req)
Christopher Faulet377c5a52018-10-24 21:21:30 +02003931{
3932 struct stats_admin_rule *stats_admin_rule;
3933 struct stream_interface *si = &s->si[1];
3934 struct session *sess = s->sess;
3935 struct http_txn *txn = s->txn;
3936 struct http_msg *msg = &txn->req;
3937 struct uri_auth *uri_auth = s->be->uri_auth;
3938 const char *h, *lookup, *end;
3939 struct appctx *appctx;
3940 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003941 struct htx_sl *sl;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003942
3943 appctx = si_appctx(si);
3944 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
3945 appctx->st1 = appctx->st2 = 0;
3946 appctx->ctx.stats.st_code = STAT_STATUS_INIT;
Willy Tarreau676c29e2019-10-09 10:50:01 +02003947 appctx->ctx.stats.flags |= uri_auth->flags;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003948 appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */
3949 if ((msg->flags & HTTP_MSGF_VER_11) && (txn->meth != HTTP_METH_HEAD))
3950 appctx->ctx.stats.flags |= STAT_CHUNKED;
3951
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003952 htx = htxbuf(&req->buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02003953 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003954 lookup = HTX_SL_REQ_UPTR(sl) + uri_auth->uri_len;
3955 end = HTX_SL_REQ_UPTR(sl) + HTX_SL_REQ_ULEN(sl);
Christopher Faulet377c5a52018-10-24 21:21:30 +02003956
3957 for (h = lookup; h <= end - 3; h++) {
3958 if (memcmp(h, ";up", 3) == 0) {
3959 appctx->ctx.stats.flags |= STAT_HIDE_DOWN;
3960 break;
3961 }
Amaury Denoyelle91e55ea2021-02-25 14:46:08 +01003962 }
3963
3964 for (h = lookup; h <= end - 9; h++) {
3965 if (memcmp(h, ";no-maint", 9) == 0) {
Willy Tarreau3e320362020-10-23 17:28:57 +02003966 appctx->ctx.stats.flags |= STAT_HIDE_MAINT;
3967 break;
3968 }
Christopher Faulet377c5a52018-10-24 21:21:30 +02003969 }
3970
3971 if (uri_auth->refresh) {
3972 for (h = lookup; h <= end - 10; h++) {
3973 if (memcmp(h, ";norefresh", 10) == 0) {
3974 appctx->ctx.stats.flags |= STAT_NO_REFRESH;
3975 break;
3976 }
3977 }
3978 }
3979
3980 for (h = lookup; h <= end - 4; h++) {
3981 if (memcmp(h, ";csv", 4) == 0) {
Christopher Faulet6338a082019-09-09 15:50:54 +02003982 appctx->ctx.stats.flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
Christopher Faulet377c5a52018-10-24 21:21:30 +02003983 break;
3984 }
3985 }
3986
3987 for (h = lookup; h <= end - 6; h++) {
3988 if (memcmp(h, ";typed", 6) == 0) {
Christopher Faulet6338a082019-09-09 15:50:54 +02003989 appctx->ctx.stats.flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
Christopher Faulet377c5a52018-10-24 21:21:30 +02003990 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
3991 break;
3992 }
3993 }
3994
Christopher Faulet6338a082019-09-09 15:50:54 +02003995 for (h = lookup; h <= end - 5; h++) {
3996 if (memcmp(h, ";json", 5) == 0) {
3997 appctx->ctx.stats.flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
3998 appctx->ctx.stats.flags |= STAT_FMT_JSON;
3999 break;
4000 }
4001 }
4002
4003 for (h = lookup; h <= end - 12; h++) {
4004 if (memcmp(h, ";json-schema", 12) == 0) {
4005 appctx->ctx.stats.flags &= ~STAT_FMT_MASK;
4006 appctx->ctx.stats.flags |= STAT_JSON_SCHM;
4007 break;
4008 }
4009 }
4010
Christopher Faulet377c5a52018-10-24 21:21:30 +02004011 for (h = lookup; h <= end - 8; h++) {
4012 if (memcmp(h, ";st=", 4) == 0) {
4013 int i;
4014 h += 4;
4015 appctx->ctx.stats.st_code = STAT_STATUS_UNKN;
4016 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
4017 if (strncmp(stat_status_codes[i], h, 4) == 0) {
4018 appctx->ctx.stats.st_code = i;
4019 break;
4020 }
4021 }
4022 break;
4023 }
4024 }
4025
4026 appctx->ctx.stats.scope_str = 0;
4027 appctx->ctx.stats.scope_len = 0;
4028 for (h = lookup; h <= end - 8; h++) {
4029 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
4030 int itx = 0;
4031 const char *h2;
4032 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
4033 const char *err;
4034
4035 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
4036 h2 = h;
Christopher Fauleted7a0662019-01-14 11:07:34 +01004037 appctx->ctx.stats.scope_str = h2 - HTX_SL_REQ_UPTR(sl);
4038 while (h < end) {
Christopher Faulet377c5a52018-10-24 21:21:30 +02004039 if (*h == ';' || *h == '&' || *h == ' ')
4040 break;
4041 itx++;
4042 h++;
4043 }
4044
4045 if (itx > STAT_SCOPE_TXT_MAXLEN)
4046 itx = STAT_SCOPE_TXT_MAXLEN;
4047 appctx->ctx.stats.scope_len = itx;
4048
4049 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
4050 memcpy(scope_txt, h2, itx);
4051 scope_txt[itx] = '\0';
4052 err = invalid_char(scope_txt);
4053 if (err) {
4054 /* bad char in search text => clear scope */
4055 appctx->ctx.stats.scope_str = 0;
4056 appctx->ctx.stats.scope_len = 0;
4057 }
4058 break;
4059 }
4060 }
4061
4062 /* now check whether we have some admin rules for this request */
4063 list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) {
4064 int ret = 1;
4065
4066 if (stats_admin_rule->cond) {
4067 ret = acl_exec_cond(stats_admin_rule->cond, s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
4068 ret = acl_pass(ret);
4069 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
4070 ret = !ret;
4071 }
4072
4073 if (ret) {
4074 /* no rule, or the rule matches */
4075 appctx->ctx.stats.flags |= STAT_ADMIN;
4076 break;
4077 }
4078 }
4079
Christopher Faulet5d45e382019-02-27 15:15:23 +01004080 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
4081 appctx->st0 = STAT_HTTP_HEAD;
4082 else if (txn->meth == HTTP_METH_POST) {
Christopher Fauletbd9e8422019-08-15 22:26:48 +02004083 if (appctx->ctx.stats.flags & STAT_ADMIN) {
Christopher Faulet377c5a52018-10-24 21:21:30 +02004084 appctx->st0 = STAT_HTTP_POST;
Christopher Fauletbd9e8422019-08-15 22:26:48 +02004085 if (msg->msg_state < HTTP_MSG_DATA)
4086 req->analysers |= AN_REQ_HTTP_BODY;
4087 }
Christopher Faulet377c5a52018-10-24 21:21:30 +02004088 else {
Christopher Faulet5d45e382019-02-27 15:15:23 +01004089 /* POST without admin level */
Christopher Faulet377c5a52018-10-24 21:21:30 +02004090 appctx->ctx.stats.flags &= ~STAT_CHUNKED;
4091 appctx->ctx.stats.st_code = STAT_STATUS_DENY;
4092 appctx->st0 = STAT_HTTP_LAST;
4093 }
4094 }
4095 else {
Christopher Faulet5d45e382019-02-27 15:15:23 +01004096 /* Unsupported method */
4097 appctx->ctx.stats.flags &= ~STAT_CHUNKED;
4098 appctx->ctx.stats.st_code = STAT_STATUS_IVAL;
4099 appctx->st0 = STAT_HTTP_LAST;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004100 }
4101
4102 s->task->nice = -32; /* small boost for HTTP statistics */
4103 return 1;
4104}
4105
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004106void http_perform_server_redirect(struct stream *s, struct stream_interface *si)
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004107{
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004108 struct channel *req = &s->req;
4109 struct channel *res = &s->res;
4110 struct server *srv;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004111 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004112 struct htx_sl *sl;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004113 struct ist path, location;
4114 unsigned int flags;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004115
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004116 /*
4117 * Create the location
4118 */
4119 chunk_reset(&trash);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004120
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004121 /* 1: add the server's prefix */
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004122 /* special prefix "/" means don't change URL */
4123 srv = __objt_server(s->target);
4124 if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') {
4125 if (!chunk_memcat(&trash, srv->rdr_pfx, srv->rdr_len))
4126 return;
4127 }
4128
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004129 /* 2: add the request Path */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004130 htx = htxbuf(&req->buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02004131 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004132 path = http_get_path(htx_sl_req_uri(sl));
Tim Duesterhused526372020-03-05 17:56:33 +01004133 if (!isttest(path))
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004134 return;
4135
4136 if (!chunk_memcat(&trash, path.ptr, path.len))
4137 return;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004138 location = ist2(trash.area, trash.data);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004139
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004140 /*
4141 * Create the 302 respone
4142 */
4143 htx = htx_from_buf(&res->buf);
4144 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
4145 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
4146 ist("HTTP/1.1"), ist("302"), ist("Found"));
4147 if (!sl)
4148 goto fail;
4149 sl->info.res.status = 302;
4150 s->txn->status = 302;
4151
4152 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) ||
4153 !htx_add_header(htx, ist("Connection"), ist("close")) ||
4154 !htx_add_header(htx, ist("Content-length"), ist("0")) ||
4155 !htx_add_header(htx, ist("Location"), location))
4156 goto fail;
4157
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004158 if (!htx_add_endof(htx, HTX_BLK_EOH))
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004159 goto fail;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004160
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004161 htx->flags |= HTX_FL_EOM;
Christopher Fauletc20afb82020-01-24 19:16:26 +01004162 htx_to_buf(htx, &res->buf);
Christopher Fauleta72a7e42020-01-28 09:28:11 +01004163 if (!http_forward_proxy_resp(s, 1))
4164 goto fail;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004165
4166 /* return without error. */
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004167 si_shutr(si);
4168 si_shutw(si);
4169 si->err_type = SI_ET_NONE;
4170 si->state = SI_ST_CLO;
4171
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004172 if (!(s->flags & SF_ERR_MASK))
4173 s->flags |= SF_ERR_LOCAL;
4174 if (!(s->flags & SF_FINST_MASK))
4175 s->flags |= SF_FINST_C;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004176
4177 /* FIXME: we should increase a counter of redirects per server and per backend. */
4178 srv_inc_sess_ctr(srv);
4179 srv_set_sess_last(srv);
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004180 return;
4181
4182 fail:
4183 /* If an error occurred, remove the incomplete HTTP response from the
4184 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004185 channel_htx_truncate(res, htx);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004186}
4187
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05004188/* This function terminates the request because it was completely analyzed or
Christopher Fauletf2824e62018-10-01 12:12:37 +02004189 * because an error was triggered during the body forwarding.
4190 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004191static void http_end_request(struct stream *s)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004192{
4193 struct channel *chn = &s->req;
4194 struct http_txn *txn = s->txn;
4195
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004196 DBG_TRACE_ENTER(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004197
Christopher Fauletb42a8b62018-11-19 21:59:00 +01004198 if (unlikely(txn->req.msg_state == HTTP_MSG_ERROR ||
4199 txn->rsp.msg_state == HTTP_MSG_ERROR)) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004200 channel_abort(chn);
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004201 channel_htx_truncate(chn, htxbuf(&chn->buf));
Christopher Fauletf2824e62018-10-01 12:12:37 +02004202 goto end;
4203 }
4204
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004205 if (unlikely(txn->req.msg_state < HTTP_MSG_DONE)) {
4206 DBG_TRACE_DEVEL("waiting end of the request", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004207 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004208 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004209
4210 if (txn->req.msg_state == HTTP_MSG_DONE) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004211 /* No need to read anymore, the request was completely parsed.
4212 * We can shut the read side unless we want to abort_on_close,
4213 * or we have a POST request. The issue with POST requests is
4214 * that some browsers still send a CRLF after the request, and
4215 * this CRLF must be read so that it does not remain in the kernel
4216 * buffers, otherwise a close could cause an RST on some systems
4217 * (eg: Linux).
4218 */
Christopher Faulet769d0e92019-03-22 14:23:18 +01004219 if (!(s->be->options & PR_O_ABRT_CLOSE) && txn->meth != HTTP_METH_POST)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004220 channel_dont_read(chn);
4221
4222 /* if the server closes the connection, we want to immediately react
4223 * and close the socket to save packets and syscalls.
4224 */
4225 s->si[1].flags |= SI_FL_NOHALF;
4226
4227 /* In any case we've finished parsing the request so we must
4228 * disable Nagle when sending data because 1) we're not going
4229 * to shut this side, and 2) the server is waiting for us to
4230 * send pending data.
4231 */
4232 chn->flags |= CF_NEVER_WAIT;
4233
Christopher Fauletd01ce402019-01-02 17:44:13 +01004234 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
4235 /* The server has not finished to respond, so we
4236 * don't want to move in order not to upset it.
4237 */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004238 DBG_TRACE_DEVEL("waiting end of the response", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletd01ce402019-01-02 17:44:13 +01004239 return;
4240 }
4241
Christopher Fauletf2824e62018-10-01 12:12:37 +02004242 /* When we get here, it means that both the request and the
4243 * response have finished receiving. Depending on the connection
4244 * mode, we'll have to wait for the last bytes to leave in either
4245 * direction, and sometimes for a close to be effective.
4246 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02004247 if (txn->flags & TX_CON_WANT_TUN) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004248 /* Tunnel mode will not have any analyser so it needs to
4249 * poll for reads.
4250 */
4251 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004252 if (b_data(&chn->buf)) {
4253 DBG_TRACE_DEVEL("waiting to flush the request", STRM_EV_HTTP_ANA, s, txn);
Christopher Faulet9768c262018-10-22 09:34:31 +02004254 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004255 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004256 txn->req.msg_state = HTTP_MSG_TUNNEL;
4257 }
4258 else {
4259 /* we're not expecting any new data to come for this
4260 * transaction, so we can close it.
Christopher Faulet9768c262018-10-22 09:34:31 +02004261 *
4262 * However, there is an exception if the response
4263 * length is undefined. In this case, we need to wait
4264 * the close from the server. The response will be
4265 * switched in TUNNEL mode until the end.
Christopher Fauletf2824e62018-10-01 12:12:37 +02004266 */
4267 if (!(txn->rsp.flags & HTTP_MSGF_XFER_LEN) &&
4268 txn->rsp.msg_state != HTTP_MSG_CLOSED)
Christopher Faulet9768c262018-10-22 09:34:31 +02004269 goto check_channel_flags;
Christopher Fauletf2824e62018-10-01 12:12:37 +02004270
4271 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4272 channel_shutr_now(chn);
4273 channel_shutw_now(chn);
4274 }
4275 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004276 goto check_channel_flags;
4277 }
4278
4279 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
4280 http_msg_closing:
4281 /* nothing else to forward, just waiting for the output buffer
4282 * to be empty and for the shutw_now to take effect.
4283 */
4284 if (channel_is_empty(chn)) {
4285 txn->req.msg_state = HTTP_MSG_CLOSED;
4286 goto http_msg_closed;
4287 }
4288 else if (chn->flags & CF_SHUTW) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004289 txn->req.msg_state = HTTP_MSG_ERROR;
4290 goto end;
4291 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004292 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004293 return;
4294 }
4295
4296 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
4297 http_msg_closed:
Christopher Fauletf2824e62018-10-01 12:12:37 +02004298 /* if we don't know whether the server will close, we need to hard close */
4299 if (txn->rsp.flags & HTTP_MSGF_XFER_LEN)
4300 s->si[1].flags |= SI_FL_NOLINGER; /* we want to close ASAP */
Christopher Fauletf2824e62018-10-01 12:12:37 +02004301 /* see above in MSG_DONE why we only do this in these states */
Christopher Faulet769d0e92019-03-22 14:23:18 +01004302 if (!(s->be->options & PR_O_ABRT_CLOSE))
Christopher Fauletf2824e62018-10-01 12:12:37 +02004303 channel_dont_read(chn);
4304 goto end;
4305 }
4306
4307 check_channel_flags:
4308 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4309 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4310 /* if we've just closed an output, let's switch */
4311 txn->req.msg_state = HTTP_MSG_CLOSING;
4312 goto http_msg_closing;
4313 }
4314
4315 end:
4316 chn->analysers &= AN_REQ_FLT_END;
Christopher Faulet198ef8b2020-12-15 13:32:55 +01004317 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
4318 chn->flags |= CF_NEVER_WAIT;
4319 if (HAS_REQ_DATA_FILTERS(s))
Christopher Fauletf2824e62018-10-01 12:12:37 +02004320 chn->analysers |= AN_REQ_FLT_XFER_DATA;
Christopher Faulet198ef8b2020-12-15 13:32:55 +01004321 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004322 channel_auto_close(chn);
4323 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004324 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004325}
4326
4327
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05004328/* This function terminates the response because it was completely analyzed or
Christopher Fauletf2824e62018-10-01 12:12:37 +02004329 * because an error was triggered during the body forwarding.
4330 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004331static void http_end_response(struct stream *s)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004332{
4333 struct channel *chn = &s->res;
4334 struct http_txn *txn = s->txn;
4335
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004336 DBG_TRACE_ENTER(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004337
Christopher Fauletb42a8b62018-11-19 21:59:00 +01004338 if (unlikely(txn->req.msg_state == HTTP_MSG_ERROR ||
4339 txn->rsp.msg_state == HTTP_MSG_ERROR)) {
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004340 channel_htx_truncate(&s->req, htxbuf(&s->req.buf));
Christopher Faulet9768c262018-10-22 09:34:31 +02004341 channel_abort(&s->req);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004342 goto end;
4343 }
4344
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004345 if (unlikely(txn->rsp.msg_state < HTTP_MSG_DONE)) {
4346 DBG_TRACE_DEVEL("waiting end of the response", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004347 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004348 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004349
4350 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
4351 /* In theory, we don't need to read anymore, but we must
4352 * still monitor the server connection for a possible close
4353 * while the request is being uploaded, so we don't disable
4354 * reading.
4355 */
4356 /* channel_dont_read(chn); */
4357
4358 if (txn->req.msg_state < HTTP_MSG_DONE) {
4359 /* The client seems to still be sending data, probably
4360 * because we got an error response during an upload.
4361 * We have the choice of either breaking the connection
4362 * or letting it pass through. Let's do the later.
4363 */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004364 DBG_TRACE_DEVEL("waiting end of the request", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004365 return;
4366 }
4367
4368 /* When we get here, it means that both the request and the
4369 * response have finished receiving. Depending on the connection
4370 * mode, we'll have to wait for the last bytes to leave in either
4371 * direction, and sometimes for a close to be effective.
4372 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02004373 if (txn->flags & TX_CON_WANT_TUN) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004374 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004375 if (b_data(&chn->buf)) {
4376 DBG_TRACE_DEVEL("waiting to flush the respone", STRM_EV_HTTP_ANA, s, txn);
Christopher Faulet9768c262018-10-22 09:34:31 +02004377 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004378 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004379 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
4380 }
4381 else {
4382 /* we're not expecting any new data to come for this
4383 * transaction, so we can close it.
4384 */
4385 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4386 channel_shutr_now(chn);
4387 channel_shutw_now(chn);
4388 }
4389 }
4390 goto check_channel_flags;
4391 }
4392
4393 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
4394 http_msg_closing:
4395 /* nothing else to forward, just waiting for the output buffer
4396 * to be empty and for the shutw_now to take effect.
4397 */
4398 if (channel_is_empty(chn)) {
4399 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4400 goto http_msg_closed;
4401 }
4402 else if (chn->flags & CF_SHUTW) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004403 txn->rsp.msg_state = HTTP_MSG_ERROR;
Christopher Fauletcff0f732019-12-16 16:13:44 +01004404 _HA_ATOMIC_ADD(&strm_sess(s)->fe->fe_counters.cli_aborts, 1);
Olivier Houcharda798bf52019-03-08 18:52:00 +01004405 _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
William Lallemand36119de2021-03-08 15:26:48 +01004406 if (strm_sess(s)->listener && strm_sess(s)->listener->counters)
Christopher Fauletcff0f732019-12-16 16:13:44 +01004407 _HA_ATOMIC_ADD(&strm_sess(s)->listener->counters->cli_aborts, 1);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004408 if (objt_server(s->target))
Christopher Fauletcff0f732019-12-16 16:13:44 +01004409 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.cli_aborts, 1);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004410 goto end;
4411 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004412 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004413 return;
4414 }
4415
4416 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
4417 http_msg_closed:
4418 /* drop any pending data */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004419 channel_htx_truncate(&s->req, htxbuf(&s->req.buf));
Christopher Faulet9768c262018-10-22 09:34:31 +02004420 channel_abort(&s->req);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004421 goto end;
4422 }
4423
4424 check_channel_flags:
4425 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4426 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4427 /* if we've just closed an output, let's switch */
4428 txn->rsp.msg_state = HTTP_MSG_CLOSING;
4429 goto http_msg_closing;
4430 }
4431
4432 end:
4433 chn->analysers &= AN_RES_FLT_END;
Christopher Faulet198ef8b2020-12-15 13:32:55 +01004434 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
4435 chn->flags |= CF_NEVER_WAIT;
4436 if (HAS_RSP_DATA_FILTERS(s))
4437 chn->analysers |= AN_RES_FLT_XFER_DATA;
4438 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004439 channel_auto_close(chn);
4440 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004441 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004442}
4443
Christopher Fauletef70e252020-01-28 09:26:19 +01004444/* Forward a response generated by HAProxy (error/redirect/return). This
4445 * function forwards all pending incoming data. If <final> is set to 0, nothing
4446 * more is performed. It is used for 1xx informational messages. Otherwise, the
Christopher Faulet507479b2020-05-15 12:29:46 +02004447 * transaction is terminated and the request is emptied. On success 1 is
Christopher Faulet40e6b552020-06-25 16:04:50 +02004448 * returned. If an error occurred, 0 is returned. If it fails, this function
4449 * only exits. It is the caller responsibility to do the cleanup.
Christopher Fauletef70e252020-01-28 09:26:19 +01004450 */
4451int http_forward_proxy_resp(struct stream *s, int final)
4452{
4453 struct channel *req = &s->req;
4454 struct channel *res = &s->res;
4455 struct htx *htx = htxbuf(&res->buf);
4456 size_t data;
4457
4458 if (final) {
4459 htx->flags |= HTX_FL_PROXY_RESP;
Christopher Faulet507479b2020-05-15 12:29:46 +02004460
Christopher Fauletaab1b672020-11-18 16:44:02 +01004461 if (!htx_is_empty(htx) && !http_eval_after_res_rules(s))
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01004462 return 0;
Christopher Fauletef70e252020-01-28 09:26:19 +01004463
Christopher Fauletd6c48362020-10-19 18:01:38 +02004464 if (s->txn->meth == HTTP_METH_HEAD)
4465 htx_skip_msg_payload(htx);
4466
Christopher Fauletef70e252020-01-28 09:26:19 +01004467 channel_auto_read(req);
4468 channel_abort(req);
4469 channel_auto_close(req);
4470 channel_htx_erase(req, htxbuf(&req->buf));
4471
4472 res->wex = tick_add_ifset(now_ms, res->wto);
4473 channel_auto_read(res);
4474 channel_auto_close(res);
4475 channel_shutr_now(res);
Christopher Faulet1a9db7c2020-06-25 15:36:45 +02004476 res->flags |= CF_EOI; /* The response is terminated, add EOI */
Christopher Faulet42432f32020-11-20 17:43:16 +01004477 htxbuf(&res->buf)->flags |= HTX_FL_EOM; /* no more data are expected */
Christopher Fauletef70e252020-01-28 09:26:19 +01004478 }
Christopher Fauletcf6898c2020-06-25 15:55:11 +02004479 else {
4480 /* Send ASAP informational messages. Rely on CF_EOI for final
4481 * response.
4482 */
4483 res->flags |= CF_SEND_DONTWAIT;
4484 }
Christopher Fauletef70e252020-01-28 09:26:19 +01004485
4486 data = htx->data - co_data(res);
4487 c_adv(res, data);
4488 htx->first = -1;
4489 res->total += data;
4490 return 1;
4491}
4492
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004493void http_server_error(struct stream *s, struct stream_interface *si, int err,
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004494 int finst, struct http_reply *msg)
Christopher Faulet0f226952018-10-22 09:29:56 +02004495{
Christopher Faulet72c7d8d2020-01-27 15:32:25 +01004496 http_reply_and_close(s, s->txn->status, msg);
Christopher Faulet0f226952018-10-22 09:29:56 +02004497 if (!(s->flags & SF_ERR_MASK))
4498 s->flags |= err;
4499 if (!(s->flags & SF_FINST_MASK))
4500 s->flags |= finst;
4501}
4502
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004503void http_reply_and_close(struct stream *s, short status, struct http_reply *msg)
Christopher Faulet0f226952018-10-22 09:29:56 +02004504{
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004505 if (!msg) {
4506 channel_htx_truncate(&s->res, htxbuf(&s->res.buf));
4507 goto end;
4508 }
4509
4510 if (http_reply_message(s, msg) == -1) {
4511 /* On error, return a 500 error message, but don't rewrite it if
Christopher Faulet40e6b552020-06-25 16:04:50 +02004512 * it is already an internal error. If it was already a "const"
4513 * 500 error, just fail.
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004514 */
Christopher Faulet40e6b552020-06-25 16:04:50 +02004515 if (s->txn->status == 500) {
4516 if (s->txn->flags & TX_CONST_REPLY)
4517 goto end;
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004518 s->txn->flags |= TX_CONST_REPLY;
Christopher Faulet40e6b552020-06-25 16:04:50 +02004519 }
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004520 s->txn->status = 500;
4521 s->txn->http_reply = NULL;
4522 return http_reply_and_close(s, s->txn->status, http_error_message(s));
4523 }
4524
4525end:
4526 s->res.wex = tick_add_ifset(now_ms, s->res.wto);
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004527
Christopher Faulet0f226952018-10-22 09:29:56 +02004528 channel_auto_read(&s->req);
4529 channel_abort(&s->req);
4530 channel_auto_close(&s->req);
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004531 channel_htx_erase(&s->req, htxbuf(&s->req.buf));
Christopher Faulet72c7d8d2020-01-27 15:32:25 +01004532 channel_auto_read(&s->res);
4533 channel_auto_close(&s->res);
4534 channel_shutr_now(&s->res);
Christopher Faulet0f226952018-10-22 09:29:56 +02004535}
4536
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004537struct http_reply *http_error_message(struct stream *s)
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004538{
4539 const int msgnum = http_get_status_idx(s->txn->status);
4540
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004541 if (s->txn->http_reply)
4542 return s->txn->http_reply;
4543 else if (s->be->replies[msgnum])
4544 return s->be->replies[msgnum];
4545 else if (strm_fe(s)->replies[msgnum])
4546 return strm_fe(s)->replies[msgnum];
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004547 else
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004548 return &http_err_replies[msgnum];
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004549}
4550
Christopher Faulet40e6b552020-06-25 16:04:50 +02004551/* Produces an HTX message from an http reply. Depending on the http reply type,
4552 * a, errorfile, an raw file or a log-format string is used. On success, it
4553 * returns 0. If an error occurs -1 is returned. If it fails, this function only
4554 * exits. It is the caller responsibility to do the cleanup.
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004555 */
Christopher Fauletae43b6c2020-05-27 15:24:22 +02004556int http_reply_to_htx(struct stream *s, struct htx *htx, struct http_reply *reply)
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004557{
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004558 struct buffer *errmsg;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004559 struct htx_sl *sl;
4560 struct buffer *body = NULL;
4561 const char *status, *reason, *clen, *ctype;
4562 unsigned int slflags;
4563 int ret = 0;
4564
Christopher Faulete29a97e2020-05-14 14:49:25 +02004565 /*
4566 * - HTTP_REPLY_ERRFILES unexpected here. handled as no payload if so
4567 *
4568 * - HTTP_REPLY_INDIRECT: switch on another reply if defined or handled
4569 * as no payload if NULL. the TXN status code is set with the status
4570 * of the original reply.
4571 */
4572
4573 if (reply->type == HTTP_REPLY_INDIRECT) {
4574 if (reply->body.reply)
4575 reply = reply->body.reply;
4576 }
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004577 if (reply->type == HTTP_REPLY_ERRMSG && !reply->body.errmsg) {
4578 /* get default error message */
4579 if (reply == s->txn->http_reply)
4580 s->txn->http_reply = NULL;
4581 reply = http_error_message(s);
4582 if (reply->type == HTTP_REPLY_INDIRECT) {
4583 if (reply->body.reply)
4584 reply = reply->body.reply;
4585 }
4586 }
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004587
4588 if (reply->type == HTTP_REPLY_ERRMSG) {
4589 /* implicit or explicit error message*/
4590 errmsg = reply->body.errmsg;
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004591 if (errmsg && !b_is_null(errmsg)) {
Christopher Faulet20567362020-05-15 14:52:49 +02004592 if (!htx_copy_msg(htx, errmsg))
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004593 goto fail;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004594 }
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004595 }
4596 else {
4597 /* no payload, file or log-format string */
4598 if (reply->type == HTTP_REPLY_RAW) {
4599 /* file */
4600 body = &reply->body.obj;
4601 }
4602 else if (reply->type == HTTP_REPLY_LOGFMT) {
4603 /* log-format string */
4604 body = alloc_trash_chunk();
4605 if (!body)
4606 goto fail_alloc;
4607 body->data = build_logline(s, body->area, body->size, &reply->body.fmt);
4608 }
4609 /* else no payload */
4610
4611 status = ultoa(reply->status);
4612 reason = http_get_reason(reply->status);
4613 slflags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_CLEN);
4614 if (!body || !b_data(body))
4615 slflags |= HTX_SL_F_BODYLESS;
4616 sl = htx_add_stline(htx, HTX_BLK_RES_SL, slflags, ist("HTTP/1.1"), ist(status), ist(reason));
4617 if (!sl)
4618 goto fail;
4619 sl->info.res.status = reply->status;
4620
4621 clen = (body ? ultoa(b_data(body)) : "0");
4622 ctype = reply->ctype;
4623
4624 if (!LIST_ISEMPTY(&reply->hdrs)) {
4625 struct http_reply_hdr *hdr;
4626 struct buffer *value = alloc_trash_chunk();
4627
4628 if (!value)
4629 goto fail;
4630
4631 list_for_each_entry(hdr, &reply->hdrs, list) {
4632 chunk_reset(value);
4633 value->data = build_logline(s, value->area, value->size, &hdr->value);
4634 if (b_data(value) && !htx_add_header(htx, hdr->name, ist2(b_head(value), b_data(value)))) {
4635 free_trash_chunk(value);
4636 goto fail;
4637 }
4638 chunk_reset(value);
4639 }
4640 free_trash_chunk(value);
4641 }
4642
4643 if (!htx_add_header(htx, ist("content-length"), ist(clen)) ||
4644 (body && b_data(body) && ctype && !htx_add_header(htx, ist("content-type"), ist(ctype))) ||
4645 !htx_add_endof(htx, HTX_BLK_EOH) ||
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004646 (body && b_data(body) && !htx_add_data_atonce(htx, ist2(b_head(body), b_data(body)))))
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004647 goto fail;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004648
4649 htx->flags |= HTX_FL_EOM;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004650 }
4651
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004652 leave:
4653 if (reply->type == HTTP_REPLY_LOGFMT)
4654 free_trash_chunk(body);
4655 return ret;
4656
4657 fail_alloc:
4658 if (!(s->flags & SF_ERR_MASK))
4659 s->flags |= SF_ERR_RESOURCE;
Christopher Faulet97e466c2020-05-15 15:12:47 +02004660 /* fall through */
4661 fail:
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004662 ret = -1;
4663 goto leave;
Christopher Faulet97e466c2020-05-15 15:12:47 +02004664}
4665
4666/* Send an http reply to the client. On success, it returns 0. If an error
Christopher Faulet40e6b552020-06-25 16:04:50 +02004667 * occurs -1 is returned and the response channel is truncated, removing this
4668 * way the faulty reply. This function may fail when the reply is formatted
4669 * (http_reply_to_htx) or when the reply is forwarded
4670 * (http_forward_proxy_resp). On the last case, it is because a
4671 * http-after-response rule fails.
Christopher Faulet97e466c2020-05-15 15:12:47 +02004672 */
4673int http_reply_message(struct stream *s, struct http_reply *reply)
4674{
4675 struct channel *res = &s->res;
4676 struct htx *htx = htx_from_buf(&res->buf);
4677
4678 if (s->txn->status == -1)
4679 s->txn->status = reply->status;
4680 channel_htx_truncate(res, htx);
4681
4682 if (http_reply_to_htx(s, htx, reply) == -1)
4683 goto fail;
4684
4685 htx_to_buf(htx, &s->res.buf);
4686 if (!http_forward_proxy_resp(s, 1))
4687 goto fail;
4688 return 0;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004689
4690 fail:
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004691 channel_htx_truncate(res, htx);
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004692 if (!(s->flags & SF_ERR_MASK))
4693 s->flags |= SF_ERR_PRXCOND;
Christopher Faulet97e466c2020-05-15 15:12:47 +02004694 return -1;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004695}
4696
Christopher Faulet304cc402019-07-15 15:46:28 +02004697/* Return the error message corresponding to si->err_type. It is assumed
4698 * that the server side is closed. Note that err_type is actually a
4699 * bitmask, where almost only aborts may be cumulated with other
4700 * values. We consider that aborted operations are more important
4701 * than timeouts or errors due to the fact that nobody else in the
4702 * logs might explain incomplete retries. All others should avoid
4703 * being cumulated. It should normally not be possible to have multiple
4704 * aborts at once, but just in case, the first one in sequence is reported.
4705 * Note that connection errors appearing on the second request of a keep-alive
4706 * connection are not reported since this allows the client to retry.
4707 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004708void http_return_srv_error(struct stream *s, struct stream_interface *si)
Christopher Faulet304cc402019-07-15 15:46:28 +02004709{
4710 int err_type = si->err_type;
4711
4712 /* set s->txn->status for http_error_message(s) */
4713 s->txn->status = 503;
4714
4715 if (err_type & SI_ET_QUEUE_ABRT)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004716 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_Q,
4717 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004718 else if (err_type & SI_ET_CONN_ABRT)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004719 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_C,
4720 (s->txn->flags & TX_NOT_FIRST) ? NULL :
4721 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004722 else if (err_type & SI_ET_QUEUE_TO)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004723 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_Q,
4724 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004725 else if (err_type & SI_ET_QUEUE_ERR)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004726 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_Q,
4727 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004728 else if (err_type & SI_ET_CONN_TO)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004729 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_C,
4730 (s->txn->flags & TX_NOT_FIRST) ? NULL :
4731 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004732 else if (err_type & SI_ET_CONN_ERR)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004733 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_C,
4734 (s->flags & SF_SRV_REUSED) ? NULL :
4735 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004736 else if (err_type & SI_ET_CONN_RES)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004737 http_server_error(s, si, SF_ERR_RESOURCE, SF_FINST_C,
4738 (s->txn->flags & TX_NOT_FIRST) ? NULL :
4739 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004740 else { /* SI_ET_CONN_OTHER and others */
4741 s->txn->status = 500;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004742 http_server_error(s, si, SF_ERR_INTERNAL, SF_FINST_C,
4743 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004744 }
4745}
4746
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004747
Christopher Faulet4a28a532019-03-01 11:19:40 +01004748/* Handle Expect: 100-continue for HTTP/1.1 messages if necessary. It returns 0
4749 * on success and -1 on error.
4750 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004751static int http_handle_expect_hdr(struct stream *s, struct htx *htx, struct http_msg *msg)
Christopher Faulet4a28a532019-03-01 11:19:40 +01004752{
4753 /* If we have HTTP/1.1 message with a body and Expect: 100-continue,
4754 * then we must send an HTTP/1.1 100 Continue intermediate response.
4755 */
4756 if (msg->msg_state == HTTP_MSG_BODY && (msg->flags & HTTP_MSGF_VER_11) &&
4757 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
4758 struct ist hdr = { .ptr = "Expect", .len = 6 };
4759 struct http_hdr_ctx ctx;
4760
4761 ctx.blk = NULL;
4762 /* Expect is allowed in 1.1, look for it */
4763 if (http_find_header(htx, hdr, &ctx, 0) &&
4764 unlikely(isteqi(ctx.value, ist2("100-continue", 12)))) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004765 if (http_reply_100_continue(s) == -1)
Christopher Faulet4a28a532019-03-01 11:19:40 +01004766 return -1;
4767 http_remove_header(htx, &ctx);
4768 }
4769 }
4770 return 0;
4771}
4772
Christopher Faulet23a3c792018-11-28 10:01:23 +01004773/* Send a 100-Continue response to the client. It returns 0 on success and -1
4774 * on error. The response channel is updated accordingly.
4775 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004776static int http_reply_100_continue(struct stream *s)
Christopher Faulet23a3c792018-11-28 10:01:23 +01004777{
4778 struct channel *res = &s->res;
4779 struct htx *htx = htx_from_buf(&res->buf);
4780 struct htx_sl *sl;
4781 unsigned int flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|
4782 HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
Christopher Faulet23a3c792018-11-28 10:01:23 +01004783
4784 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
4785 ist("HTTP/1.1"), ist("100"), ist("Continue"));
4786 if (!sl)
4787 goto fail;
4788 sl->info.res.status = 100;
4789
Christopher Faulet1d5ec092019-06-26 14:23:54 +02004790 if (!htx_add_endof(htx, HTX_BLK_EOH))
Christopher Faulet23a3c792018-11-28 10:01:23 +01004791 goto fail;
4792
Christopher Fauleta72a7e42020-01-28 09:28:11 +01004793 if (!http_forward_proxy_resp(s, 0))
4794 goto fail;
Christopher Faulet23a3c792018-11-28 10:01:23 +01004795 return 0;
4796
4797 fail:
4798 /* If an error occurred, remove the incomplete HTTP response from the
4799 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004800 channel_htx_truncate(res, htx);
Christopher Faulet23a3c792018-11-28 10:01:23 +01004801 return -1;
4802}
4803
Christopher Faulet12c51e22018-11-28 15:59:42 +01004804
Christopher Faulet0f226952018-10-22 09:29:56 +02004805/*
4806 * Capture headers from message <htx> according to header list <cap_hdr>, and
4807 * fill the <cap> pointers appropriately.
4808 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004809static void http_capture_headers(struct htx *htx, char **cap, struct cap_hdr *cap_hdr)
Christopher Faulet0f226952018-10-22 09:29:56 +02004810{
4811 struct cap_hdr *h;
4812 int32_t pos;
4813
Christopher Fauleta3f15502019-05-13 15:27:23 +02004814 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet0f226952018-10-22 09:29:56 +02004815 struct htx_blk *blk = htx_get_blk(htx, pos);
4816 enum htx_blk_type type = htx_get_blk_type(blk);
4817 struct ist n, v;
4818
4819 if (type == HTX_BLK_EOH)
4820 break;
4821 if (type != HTX_BLK_HDR)
4822 continue;
4823
4824 n = htx_get_blk_name(htx, blk);
4825
4826 for (h = cap_hdr; h; h = h->next) {
4827 if (h->namelen && (h->namelen == n.len) &&
4828 (strncasecmp(n.ptr, h->name, h->namelen) == 0)) {
4829 if (cap[h->index] == NULL)
4830 cap[h->index] =
4831 pool_alloc(h->pool);
4832
4833 if (cap[h->index] == NULL) {
4834 ha_alert("HTTP capture : out of memory.\n");
4835 break;
4836 }
4837
4838 v = htx_get_blk_value(htx, blk);
4839 if (v.len > h->len)
4840 v.len = h->len;
4841
4842 memcpy(cap[h->index], v.ptr, v.len);
4843 cap[h->index][v.len]=0;
4844 }
4845 }
4846 }
4847}
4848
Christopher Faulet0b6bdc52018-10-24 11:05:36 +02004849/* Delete a value in a header between delimiters <from> and <next>. The header
4850 * itself is delimited by <start> and <end> pointers. The number of characters
4851 * displaced is returned, and the pointer to the first delimiter is updated if
4852 * required. The function tries as much as possible to respect the following
4853 * principles :
4854 * - replace <from> delimiter by the <next> one unless <from> points to <start>,
4855 * in which case <next> is simply removed
4856 * - set exactly one space character after the new first delimiter, unless there
4857 * are not enough characters in the block being moved to do so.
4858 * - remove unneeded spaces before the previous delimiter and after the new
4859 * one.
4860 *
4861 * It is the caller's responsibility to ensure that :
4862 * - <from> points to a valid delimiter or <start> ;
4863 * - <next> points to a valid delimiter or <end> ;
4864 * - there are non-space chars before <from>.
4865 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004866static int http_del_hdr_value(char *start, char *end, char **from, char *next)
Christopher Faulet0b6bdc52018-10-24 11:05:36 +02004867{
4868 char *prev = *from;
4869
4870 if (prev == start) {
4871 /* We're removing the first value. eat the semicolon, if <next>
4872 * is lower than <end> */
4873 if (next < end)
4874 next++;
4875
4876 while (next < end && HTTP_IS_SPHT(*next))
4877 next++;
4878 }
4879 else {
4880 /* Remove useless spaces before the old delimiter. */
4881 while (HTTP_IS_SPHT(*(prev-1)))
4882 prev--;
4883 *from = prev;
4884
4885 /* copy the delimiter and if possible a space if we're
4886 * not at the end of the line.
4887 */
4888 if (next < end) {
4889 *prev++ = *next++;
4890 if (prev + 1 < next)
4891 *prev++ = ' ';
4892 while (next < end && HTTP_IS_SPHT(*next))
4893 next++;
4894 }
4895 }
4896 memmove(prev, next, end - next);
4897 return (prev - next);
4898}
4899
Christopher Faulet0f226952018-10-22 09:29:56 +02004900
4901/* Formats the start line of the request (without CRLF) and puts it in <str> and
Joseph Herlantc42c0e92018-11-25 10:43:27 -08004902 * return the written length. The line can be truncated if it exceeds <len>.
Christopher Faulet0f226952018-10-22 09:29:56 +02004903 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004904static size_t http_fmt_req_line(const struct htx_sl *sl, char *str, size_t len)
Christopher Faulet0f226952018-10-22 09:29:56 +02004905{
4906 struct ist dst = ist2(str, 0);
4907
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004908 if (istcat(&dst, htx_sl_req_meth(sl), len) == -1)
Christopher Faulet0f226952018-10-22 09:29:56 +02004909 goto end;
4910 if (dst.len + 1 > len)
4911 goto end;
4912 dst.ptr[dst.len++] = ' ';
4913
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004914 if (istcat(&dst, htx_sl_req_uri(sl), len) == -1)
Christopher Faulet0f226952018-10-22 09:29:56 +02004915 goto end;
4916 if (dst.len + 1 > len)
4917 goto end;
4918 dst.ptr[dst.len++] = ' ';
4919
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004920 istcat(&dst, htx_sl_req_vsn(sl), len);
Christopher Faulet0f226952018-10-22 09:29:56 +02004921 end:
4922 return dst.len;
4923}
4924
4925/*
4926 * Print a debug line with a start line.
4927 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004928static void http_debug_stline(const char *dir, struct stream *s, const struct htx_sl *sl)
Christopher Faulet0f226952018-10-22 09:29:56 +02004929{
4930 struct session *sess = strm_sess(s);
4931 int max;
4932
4933 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
4934 dir,
4935 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->handle.fd : -1,
4936 objt_cs(s->si[1].end) ? (unsigned short)objt_cs(s->si[1].end)->conn->handle.fd : -1);
4937
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004938 max = HTX_SL_P1_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02004939 UBOUND(max, trash.size - trash.data - 3);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004940 chunk_memcat(&trash, HTX_SL_P1_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02004941 trash.area[trash.data++] = ' ';
4942
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004943 max = HTX_SL_P2_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02004944 UBOUND(max, trash.size - trash.data - 2);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004945 chunk_memcat(&trash, HTX_SL_P2_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02004946 trash.area[trash.data++] = ' ';
4947
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004948 max = HTX_SL_P3_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02004949 UBOUND(max, trash.size - trash.data - 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004950 chunk_memcat(&trash, HTX_SL_P3_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02004951 trash.area[trash.data++] = '\n';
4952
Willy Tarreau2e8ab6b2020-03-14 11:03:20 +01004953 DISGUISE(write(1, trash.area, trash.data));
Christopher Faulet0f226952018-10-22 09:29:56 +02004954}
4955
4956/*
4957 * Print a debug line with a header.
4958 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004959static 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 +02004960{
4961 struct session *sess = strm_sess(s);
4962 int max;
4963
4964 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
4965 dir,
4966 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->handle.fd : -1,
4967 objt_cs(s->si[1].end) ? (unsigned short)objt_cs(s->si[1].end)->conn->handle.fd : -1);
4968
4969 max = n.len;
4970 UBOUND(max, trash.size - trash.data - 3);
4971 chunk_memcat(&trash, n.ptr, max);
4972 trash.area[trash.data++] = ':';
4973 trash.area[trash.data++] = ' ';
4974
4975 max = v.len;
4976 UBOUND(max, trash.size - trash.data - 1);
4977 chunk_memcat(&trash, v.ptr, max);
4978 trash.area[trash.data++] = '\n';
4979
Willy Tarreau2e8ab6b2020-03-14 11:03:20 +01004980 DISGUISE(write(1, trash.area, trash.data));
Christopher Faulet0f226952018-10-22 09:29:56 +02004981}
4982
Christopher Fauleta8a46e22019-07-16 14:53:09 +02004983/* Allocate a new HTTP transaction for stream <s> unless there is one already.
4984 * In case of allocation failure, everything allocated is freed and NULL is
4985 * returned. Otherwise the new transaction is assigned to the stream and
4986 * returned.
4987 */
4988struct http_txn *http_alloc_txn(struct stream *s)
4989{
4990 struct http_txn *txn = s->txn;
4991
4992 if (txn)
4993 return txn;
4994
4995 txn = pool_alloc(pool_head_http_txn);
4996 if (!txn)
4997 return txn;
4998
4999 s->txn = txn;
5000 return txn;
5001}
5002
5003void http_txn_reset_req(struct http_txn *txn)
5004{
Christopher Faulet1aea50e2020-01-17 16:03:53 +01005005 txn->req.flags = 0;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005006 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
5007}
5008
5009void http_txn_reset_res(struct http_txn *txn)
5010{
Christopher Faulet1aea50e2020-01-17 16:03:53 +01005011 txn->rsp.flags = 0;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005012 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
5013}
5014
5015/*
5016 * Initialize a new HTTP transaction for stream <s>. It is assumed that all
5017 * the required fields are properly allocated and that we only need to (re)init
5018 * them. This should be used before processing any new request.
5019 */
5020void http_init_txn(struct stream *s)
5021{
5022 struct http_txn *txn = s->txn;
5023 struct conn_stream *cs = objt_cs(s->si[0].end);
5024
Christopher Fauletda831fa2020-10-06 17:58:43 +02005025 txn->flags = ((cs && cs->flags & CS_FL_NOT_FIRST) ? TX_NOT_FIRST : 0);
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005026 txn->status = -1;
Christopher Faulet5cb513a2020-05-13 17:56:56 +02005027 txn->http_reply = NULL;
Willy Tarreau8b507582020-02-25 09:35:07 +01005028 write_u32(txn->cache_hash, 0);
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005029
5030 txn->cookie_first_date = 0;
5031 txn->cookie_last_date = 0;
5032
5033 txn->srv_cookie = NULL;
5034 txn->cli_cookie = NULL;
5035 txn->uri = NULL;
5036
5037 http_txn_reset_req(txn);
5038 http_txn_reset_res(txn);
5039
5040 txn->req.chn = &s->req;
5041 txn->rsp.chn = &s->res;
5042
5043 txn->auth.method = HTTP_AUTH_UNKNOWN;
5044
5045 vars_init(&s->vars_txn, SCOPE_TXN);
5046 vars_init(&s->vars_reqres, SCOPE_REQ);
5047}
5048
5049/* to be used at the end of a transaction */
5050void http_end_txn(struct stream *s)
5051{
5052 struct http_txn *txn = s->txn;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005053
5054 /* these ones will have been dynamically allocated */
5055 pool_free(pool_head_requri, txn->uri);
5056 pool_free(pool_head_capture, txn->cli_cookie);
5057 pool_free(pool_head_capture, txn->srv_cookie);
Tim Duesterhusa17e6622020-03-05 20:19:02 +01005058 pool_free(pool_head_uniqueid, s->unique_id.ptr);
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005059
Tim Duesterhusa17e6622020-03-05 20:19:02 +01005060 s->unique_id = IST_NULL;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005061 txn->uri = NULL;
5062 txn->srv_cookie = NULL;
5063 txn->cli_cookie = NULL;
5064
Christopher Faulet59399252019-11-07 14:27:52 +01005065 if (!LIST_ISEMPTY(&s->vars_txn.head))
5066 vars_prune(&s->vars_txn, s->sess, s);
5067 if (!LIST_ISEMPTY(&s->vars_reqres.head))
5068 vars_prune(&s->vars_reqres, s->sess, s);
5069}
5070
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005071
5072DECLARE_POOL(pool_head_http_txn, "http_txn", sizeof(struct http_txn));
Christopher Faulet0f226952018-10-22 09:29:56 +02005073
Christopher Fauletf4eb75d2018-10-11 15:55:07 +02005074__attribute__((constructor))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005075static void __http_protocol_init(void)
Christopher Fauletf4eb75d2018-10-11 15:55:07 +02005076{
5077}
5078
5079
5080/*
5081 * Local variables:
5082 * c-indent-level: 8
5083 * c-basic-offset: 8
5084 * End:
5085 */