blob: 4b74626e0439dff85e1cdcdd252a16e86daa8ac5 [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);
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200298 if (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);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200305 if (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);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200511 if (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);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200527 if (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);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100538 if (sess->listener->counters)
539 _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);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200545 if (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 */
676 if ((!sess->fe->except_mask.s_addr ||
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200677 (((struct sockaddr_in *)cli_conn->src)->sin_addr.s_addr & sess->fe->except_mask.s_addr)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200678 != sess->fe->except_net.s_addr) &&
679 (!s->be->except_mask.s_addr ||
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200680 (((struct sockaddr_in *)cli_conn->src)->sin_addr.s_addr & s->be->except_mask.s_addr)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200681 != s->be->except_net.s_addr)) {
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200682 unsigned char *pn = (unsigned char *)&((struct sockaddr_in *)cli_conn->src)->sin_addr;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200683
684 /* Note: we rely on the backend to get the header name to be used for
685 * x-forwarded-for, because the header is really meant for the backends.
686 * However, if the backend did not specify any option, we have to rely
687 * on the frontend's header name.
688 */
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200689 chunk_printf(&trash, "%d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
690 if (unlikely(!http_add_header(htx, hdr, ist2(trash.area, trash.data))))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100691 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200692 }
693 }
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200694 else if (cli_conn && conn_get_src(cli_conn) && cli_conn->src->ss_family == AF_INET6) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200695 /* FIXME: for the sake of completeness, we should also support
696 * 'except' here, although it is mostly useless in this case.
697 */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200698 char pn[INET6_ADDRSTRLEN];
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200699
Christopher Faulete0768eb2018-10-03 16:38:02 +0200700 inet_ntop(AF_INET6,
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200701 (const void *)&((struct sockaddr_in6 *)(cli_conn->src))->sin6_addr,
Christopher Faulete0768eb2018-10-03 16:38:02 +0200702 pn, sizeof(pn));
703
704 /* Note: we rely on the backend to get the header name to be used for
705 * x-forwarded-for, because the header is really meant for the backends.
706 * However, if the backend did not specify any option, we have to rely
707 * on the frontend's header name.
708 */
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200709 chunk_printf(&trash, "%s", pn);
710 if (unlikely(!http_add_header(htx, hdr, ist2(trash.area, trash.data))))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100711 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200712 }
713 }
714
715 /*
716 * 10: add X-Original-To if either the frontend or the backend
717 * asks for it.
718 */
719 if ((sess->fe->options | s->be->options) & PR_O_ORGTO) {
720
721 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200722 if (cli_conn && conn_get_src(cli_conn) && cli_conn->src->ss_family == AF_INET && conn_get_dst(cli_conn)) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200723 /* Add an X-Original-To header unless the destination IP is
724 * in the 'except' network range.
725 */
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200726 if (cli_conn->dst->ss_family == AF_INET &&
Christopher Faulete0768eb2018-10-03 16:38:02 +0200727 ((!sess->fe->except_mask_to.s_addr ||
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200728 (((struct sockaddr_in *)cli_conn->dst)->sin_addr.s_addr & sess->fe->except_mask_to.s_addr)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200729 != sess->fe->except_to.s_addr) &&
730 (!s->be->except_mask_to.s_addr ||
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200731 (((struct sockaddr_in *)cli_conn->dst)->sin_addr.s_addr & s->be->except_mask_to.s_addr)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200732 != s->be->except_to.s_addr))) {
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200733 struct ist hdr;
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200734 unsigned char *pn = (unsigned char *)&((struct sockaddr_in *)cli_conn->dst)->sin_addr;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200735
736 /* Note: we rely on the backend to get the header name to be used for
737 * x-original-to, because the header is really meant for the backends.
738 * However, if the backend did not specify any option, we have to rely
739 * on the frontend's header name.
740 */
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200741 if (s->be->orgto_hdr_len)
742 hdr = ist2(s->be->orgto_hdr_name, s->be->orgto_hdr_len);
743 else
744 hdr = ist2(sess->fe->orgto_hdr_name, sess->fe->orgto_hdr_len);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200745
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200746 chunk_printf(&trash, "%d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
747 if (unlikely(!http_add_header(htx, hdr, ist2(trash.area, trash.data))))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100748 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200749 }
750 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200751 }
752
Christopher Faulete0768eb2018-10-03 16:38:02 +0200753 /* If we have no server assigned yet and we're balancing on url_param
754 * with a POST request, we may be interested in checking the body for
755 * that parameter. This will be done in another analyser.
756 */
757 if (!(s->flags & (SF_ASSIGNED|SF_DIRECT)) &&
Willy Tarreau089eaa02019-01-14 15:17:46 +0100758 s->txn->meth == HTTP_METH_POST &&
759 (s->be->lbprm.algo & BE_LB_ALGO) == BE_LB_ALGO_PH) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200760 channel_dont_connect(req);
761 req->analysers |= AN_REQ_HTTP_BODY;
762 }
763
764 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
765 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau1a18b542018-12-11 16:37:42 +0100766
Christopher Faulete0768eb2018-10-03 16:38:02 +0200767 /* We expect some data from the client. Unless we know for sure
768 * we already have a full request, we have to re-enable quick-ack
769 * in case we previously disabled it, otherwise we might cause
770 * the client to delay further data.
771 */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +0100772 if ((sess->listener->options & LI_O_NOQUICKACK) && !(htx->flags & HTX_FL_EOM))
Willy Tarreau1a18b542018-12-11 16:37:42 +0100773 conn_set_quickack(cli_conn, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200774
775 /*************************************************************
776 * OK, that's finished for the headers. We have done what we *
777 * could. Let's switch to the DATA state. *
778 ************************************************************/
779 req->analyse_exp = TICK_ETERNITY;
780 req->analysers &= ~an_bit;
781
782 s->logs.tv_request = now;
783 /* OK let's go on with the BODY now */
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100784 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200785 return 1;
786
Christopher Fauletb8a53712019-12-16 11:29:38 +0100787 return_int_err:
788 txn->status = 500;
789 if (!(s->flags & SF_ERR_MASK))
790 s->flags |= SF_ERR_INTERNAL;
791 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100792 if (s->flags & SF_BE_ASSIGNED)
Christopher Fauletbe20cf32020-01-24 11:41:38 +0100793 _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100794 if (sess->listener->counters)
795 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
796 goto return_prx_cond;
797
Christopher Faulete0768eb2018-10-03 16:38:02 +0200798 return_bad_req: /* let's centralize all bad requests */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200799 txn->status = 400;
Olivier Houcharda798bf52019-03-08 18:52:00 +0100800 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200801 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100802 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100803 /* fall through */
804
805 return_prx_cond:
806 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +0200807
808 if (!(s->flags & SF_ERR_MASK))
809 s->flags |= SF_ERR_PRXCOND;
810 if (!(s->flags & SF_FINST_MASK))
811 s->flags |= SF_FINST_R;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100812
813 req->analysers &= AN_REQ_FLT_END;
814 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100815 DBG_TRACE_DEVEL("leaving on error",
816 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200817 return 0;
818}
819
820/* This function is an analyser which processes the HTTP tarpit. It always
821 * returns zero, at the beginning because it prevents any other processing
822 * from occurring, and at the end because it terminates the request.
823 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200824int http_process_tarpit(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200825{
826 struct http_txn *txn = s->txn;
827
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100828 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, &txn->req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200829 /* This connection is being tarpitted. The CLIENT side has
830 * already set the connect expiration date to the right
831 * timeout. We just have to check that the client is still
832 * there and that the timeout has not expired.
833 */
834 channel_dont_connect(req);
835 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100836 !tick_is_expired(req->analyse_exp, now_ms)) {
837 DBG_TRACE_DEVEL("waiting for tarpit timeout expiry",
838 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200839 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100840 }
841
Christopher Faulete0768eb2018-10-03 16:38:02 +0200842
843 /* We will set the queue timer to the time spent, just for
844 * logging purposes. We fake a 500 server error, so that the
845 * attacker will not suspect his connection has been tarpitted.
846 * It will not cause trouble to the logs because we can exclude
847 * the tarpitted connections by filtering on the 'PT' status flags.
848 */
849 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
850
Christopher Faulet8dfeccf2020-05-15 14:16:29 +0200851 http_reply_and_close(s, txn->status, (!(req->flags & CF_READ_ERROR) ? http_error_message(s) : NULL));
Christopher Faulet5cb513a2020-05-13 17:56:56 +0200852
853 end:
Christopher Faulete0768eb2018-10-03 16:38:02 +0200854 req->analysers &= AN_REQ_FLT_END;
855 req->analyse_exp = TICK_ETERNITY;
856
857 if (!(s->flags & SF_ERR_MASK))
858 s->flags |= SF_ERR_PRXCOND;
859 if (!(s->flags & SF_FINST_MASK))
860 s->flags |= SF_FINST_T;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100861
862 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200863 return 0;
864}
865
866/* This function is an analyser which waits for the HTTP request body. It waits
867 * for either the buffer to be full, or the full advertised contents to have
868 * reached the buffer. It must only be called after the standard HTTP request
869 * processing has occurred, because it expects the request to be parsed and will
870 * look for the Expect header. It may send a 100-Continue interim response. It
871 * takes in input any state starting from HTTP_MSG_BODY and leaves with one of
872 * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it
873 * needs to read more data, or 1 once it has completed its analysis.
874 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200875int http_wait_for_request_body(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200876{
877 struct session *sess = s->sess;
878 struct http_txn *txn = s->txn;
879 struct http_msg *msg = &s->txn->req;
Christopher Fauletf76ebe82018-10-24 11:16:22 +0200880 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200881
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100882 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Fauletf76ebe82018-10-24 11:16:22 +0200883
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100884 htx = htxbuf(&req->buf);
Christopher Fauletf76ebe82018-10-24 11:16:22 +0200885
Willy Tarreau4236f032019-03-05 10:43:32 +0100886 if (htx->flags & HTX_FL_PARSING_ERROR)
887 goto return_bad_req;
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200888 if (htx->flags & HTX_FL_PROCESSING_ERROR)
889 goto return_int_err;
Willy Tarreau4236f032019-03-05 10:43:32 +0100890
Christopher Fauletc31bc722020-11-20 14:30:38 +0100891 /* Do nothing for bodyless and CONNECT requests */
892 if (txn->meth == HTTP_METH_CONNECT || (msg->flags & HTTP_MSGF_BODYLESS))
Christopher Faulet63c69a92020-11-16 16:03:35 +0100893 goto http_end;
894
Christopher Faulete0768eb2018-10-03 16:38:02 +0200895 /* We have to parse the HTTP request body to find any required data.
896 * "balance url_param check_post" should have been the only way to get
897 * into this. We were brought here after HTTP header analysis, so all
898 * related structures are ready.
899 */
900
Christopher Fauletf76ebe82018-10-24 11:16:22 +0200901 if (msg->msg_state < HTTP_MSG_DATA) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200902 if (http_handle_expect_hdr(s, htx, msg) == -1)
Christopher Fauletb8a53712019-12-16 11:29:38 +0100903 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200904 }
905
Christopher Fauletf76ebe82018-10-24 11:16:22 +0200906 msg->msg_state = HTTP_MSG_DATA;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200907
Christopher Fauletf76ebe82018-10-24 11:16:22 +0200908 /* Now we're in HTTP_MSG_DATA. We just need to know if all data have
909 * been received or if the buffer is full.
Christopher Faulete0768eb2018-10-03 16:38:02 +0200910 */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +0100911 if ((htx->flags & HTX_FL_EOM) || htx_get_tail_type(htx) > HTX_BLK_DATA ||
Christopher Fauletdcd8c5e2019-01-21 11:24:38 +0100912 channel_htx_full(req, htx, global.tune.maxrewrite))
Christopher Faulete0768eb2018-10-03 16:38:02 +0200913 goto http_end;
914
915 if ((req->flags & CF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
916 txn->status = 408;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200917 if (!(s->flags & SF_ERR_MASK))
918 s->flags |= SF_ERR_CLITO;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100919 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
920 if (sess->listener->counters)
921 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
922 goto return_prx_cond;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200923 }
924
925 /* we get here if we need to wait for more data */
926 if (!(req->flags & (CF_SHUTR | CF_READ_ERROR))) {
927 /* Not enough data. We'll re-use the http-request
928 * timeout here. Ideally, we should set the timeout
929 * relative to the accept() date. We just set the
930 * request timeout once at the beginning of the
931 * request.
932 */
933 channel_dont_connect(req);
934 if (!tick_isset(req->analyse_exp))
935 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100936 DBG_TRACE_DEVEL("waiting for more data",
937 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200938 return 0;
939 }
940
941 http_end:
942 /* The situation will not evolve, so let's give up on the analysis. */
943 s->logs.tv_request = now; /* update the request timer to reflect full request */
944 req->analysers &= ~an_bit;
945 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100946 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200947 return 1;
948
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200949 return_int_err:
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200950 txn->status = 500;
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200951 if (!(s->flags & SF_ERR_MASK))
952 s->flags |= SF_ERR_INTERNAL;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100953 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100954 if (s->flags & SF_BE_ASSIGNED)
Christopher Fauletbe20cf32020-01-24 11:41:38 +0100955 _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100956 if (sess->listener->counters)
957 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
958 goto return_prx_cond;
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200959
Christopher Faulete0768eb2018-10-03 16:38:02 +0200960 return_bad_req: /* let's centralize all bad requests */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200961 txn->status = 400;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100962 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
963 if (sess->listener->counters)
964 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
965 /* fall through */
966
967 return_prx_cond:
968 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +0200969
970 if (!(s->flags & SF_ERR_MASK))
971 s->flags |= SF_ERR_PRXCOND;
972 if (!(s->flags & SF_FINST_MASK))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100973 s->flags |= (msg->msg_state < HTTP_MSG_DATA ? SF_FINST_R : SF_FINST_D);
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200974
Christopher Faulete0768eb2018-10-03 16:38:02 +0200975 req->analysers &= AN_REQ_FLT_END;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100976 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100977 DBG_TRACE_DEVEL("leaving on error",
978 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200979 return 0;
980}
981
982/* This function is an analyser which forwards request body (including chunk
983 * sizes if any). It is called as soon as we must forward, even if we forward
984 * zero byte. The only situation where it must not be called is when we're in
985 * tunnel mode and we want to forward till the close. It's used both to forward
986 * remaining data and to resync after end of body. It expects the msg_state to
987 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
988 * read more data, or 1 once we can go on with next request or end the stream.
989 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
990 * bytes of pending data + the headers if not already done.
991 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200992int http_request_forward_body(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200993{
994 struct session *sess = s->sess;
995 struct http_txn *txn = s->txn;
Christopher Faulet9768c262018-10-22 09:34:31 +0200996 struct http_msg *msg = &txn->req;
997 struct htx *htx;
Christopher Faulet93e02d82019-03-08 14:18:50 +0100998 short status = 0;
Christopher Fauletaed82cf2018-11-30 22:22:32 +0100999 int ret;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001000
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001001 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001002
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001003 htx = htxbuf(&req->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001004
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001005 if (htx->flags & HTX_FL_PARSING_ERROR)
1006 goto return_bad_req;
1007 if (htx->flags & HTX_FL_PROCESSING_ERROR)
1008 goto return_int_err;
1009
Christopher Faulete0768eb2018-10-03 16:38:02 +02001010 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
1011 ((req->flags & CF_SHUTW) && (req->to_forward || co_data(req)))) {
1012 /* Output closed while we were sending data. We must abort and
1013 * wake the other side up.
1014 */
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001015
Olivier Houchard29cac3c2019-07-12 15:48:58 +02001016 /* Don't abort yet if we had L7 retries activated and it
1017 * was a write error, we may recover.
1018 */
1019 if (!(req->flags & (CF_READ_ERROR | CF_READ_TIMEOUT)) &&
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001020 (s->si[1].flags & SI_FL_L7_RETRY)) {
1021 DBG_TRACE_DEVEL("leaving on L7 retry",
1022 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Olivier Houchard29cac3c2019-07-12 15:48:58 +02001023 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001024 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001025 msg->msg_state = HTTP_MSG_ERROR;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001026 http_end_request(s);
1027 http_end_response(s);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001028 DBG_TRACE_DEVEL("leaving on error",
1029 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001030 return 1;
1031 }
1032
1033 /* Note that we don't have to send 100-continue back because we don't
1034 * need the data to complete our job, and it's up to the server to
1035 * decide whether to return 100, 417 or anything else in return of
1036 * an "Expect: 100-continue" header.
1037 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001038 if (msg->msg_state == HTTP_MSG_BODY)
1039 msg->msg_state = HTTP_MSG_DATA;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001040
Christopher Faulete0768eb2018-10-03 16:38:02 +02001041 /* in most states, we should abort in case of early close */
1042 channel_auto_close(req);
1043
1044 if (req->to_forward) {
Christopher Faulet66af0b22019-03-22 14:54:52 +01001045 if (req->to_forward == CHN_INFINITE_FORWARD) {
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001046 if (req->flags & CF_EOI)
1047 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet66af0b22019-03-22 14:54:52 +01001048 }
1049 else {
1050 /* We can't process the buffer's contents yet */
1051 req->flags |= CF_WAKE_WRITE;
1052 goto missing_data_or_waiting;
1053 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001054 }
1055
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001056 if (msg->msg_state >= HTTP_MSG_ENDING)
1057 goto ending;
1058
1059 if (txn->meth == HTTP_METH_CONNECT) {
1060 msg->msg_state = HTTP_MSG_ENDING;
1061 goto ending;
1062 }
1063
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001064 /* Forward input data. We get it by removing all outgoing data not
1065 * forwarded yet from HTX data size. If there are some data filters, we
1066 * let them decide the amount of data to forward.
Christopher Faulet9768c262018-10-22 09:34:31 +02001067 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001068 if (HAS_REQ_DATA_FILTERS(s)) {
1069 ret = flt_http_payload(s, msg, htx->data);
1070 if (ret < 0)
1071 goto return_bad_req;
Christopher Faulet421e7692019-06-13 11:16:45 +02001072 c_adv(req, ret);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001073 }
1074 else {
Christopher Faulet421e7692019-06-13 11:16:45 +02001075 c_adv(req, htx->data - co_data(req));
Christopher Faulet66af0b22019-03-22 14:54:52 +01001076 if (msg->flags & HTTP_MSGF_XFER_LEN)
1077 channel_htx_forward_forever(req, htx);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001078 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001079
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001080 if (htx->data != co_data(req))
1081 goto missing_data_or_waiting;
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001082
Christopher Faulet9768c262018-10-22 09:34:31 +02001083 /* Check if the end-of-message is reached and if so, switch the message
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001084 * in HTTP_MSG_ENDING state. Then if all data was marked to be
1085 * forwarded, set the state to HTTP_MSG_DONE.
Christopher Faulet9768c262018-10-22 09:34:31 +02001086 */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001087 if (!(htx->flags & HTX_FL_EOM))
Christopher Faulet9768c262018-10-22 09:34:31 +02001088 goto missing_data_or_waiting;
1089
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001090 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet9768c262018-10-22 09:34:31 +02001091
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001092 ending:
Christopher Faulet2151cdd2020-07-22 16:34:59 +02001093 req->flags &= ~CF_EXPECT_MORE; /* no more data are expected */
1094
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001095 /* other states, ENDING...TUNNEL */
1096 if (msg->msg_state >= HTTP_MSG_DONE)
1097 goto done;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001098
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001099 if (HAS_REQ_DATA_FILTERS(s)) {
1100 ret = flt_http_end(s, msg);
1101 if (ret <= 0) {
1102 if (!ret)
1103 goto missing_data_or_waiting;
1104 goto return_bad_req;
1105 }
1106 }
1107
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001108 if (txn->meth == HTTP_METH_CONNECT)
1109 msg->msg_state = HTTP_MSG_TUNNEL;
1110 else {
1111 msg->msg_state = HTTP_MSG_DONE;
1112 req->to_forward = 0;
Christopher Faulet5b82cc52020-10-12 15:18:50 +02001113
1114 if ((s->be->retry_type &~ PR_RE_CONN_FAILED) && !(s->si[1].flags & SI_FL_D_L7_RETRY)) {
1115 struct stream_interface *si = &s->si[1];
1116
1117 /* If we want to be able to do L7 retries, copy the
1118 * request, so that we are able to resend them if
1119 * needed.
1120 *
1121 * Try to allocate a buffer if we had none. If it
1122 * fails, the next test will just disable the l7
1123 * retries.
1124 */
1125 DBG_TRACE_STATE("enable L7 retry, save the request", STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
1126 si->flags |= SI_FL_L7_RETRY;
1127 if (b_is_null(&si->l7_buffer))
1128 b_alloc(&si->l7_buffer);
1129 if (b_is_null(&si->l7_buffer))
1130 si->flags &= ~SI_FL_L7_RETRY;
1131 else {
1132 memcpy(b_orig(&si->l7_buffer), b_orig(&req->buf), b_size(&req->buf));
1133 b_add(&si->l7_buffer, co_data(req));
1134 }
1135 }
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001136 }
1137
1138 done:
1139 /* we don't want to forward closes on DONE except in tunnel mode. */
1140 if (!(txn->flags & TX_CON_WANT_TUN))
1141 channel_dont_close(req);
1142
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001143 http_end_request(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001144 if (!(req->analysers & an_bit)) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001145 http_end_response(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001146 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
1147 if (req->flags & CF_SHUTW) {
1148 /* request errors are most likely due to the
1149 * server aborting the transfer. */
Christopher Faulet93e02d82019-03-08 14:18:50 +01001150 goto return_srv_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001151 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001152 goto return_bad_req;
1153 }
Christopher Faulet5b82cc52020-10-12 15:18:50 +02001154
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001155 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001156 return 1;
1157 }
1158
1159 /* If "option abortonclose" is set on the backend, we want to monitor
1160 * the client's connection and forward any shutdown notification to the
1161 * server, which will decide whether to close or to go on processing the
1162 * request. We only do that in tunnel mode, and not in other modes since
1163 * it can be abused to exhaust source ports. */
Christopher Faulet769d0e92019-03-22 14:23:18 +01001164 if (s->be->options & PR_O_ABRT_CLOSE) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001165 channel_auto_read(req);
Christopher Fauletc41547b2019-07-16 14:32:23 +02001166 if ((req->flags & (CF_SHUTR|CF_READ_NULL)) && !(txn->flags & TX_CON_WANT_TUN))
Christopher Faulete0768eb2018-10-03 16:38:02 +02001167 s->si[1].flags |= SI_FL_NOLINGER;
1168 channel_auto_close(req);
1169 }
1170 else if (s->txn->meth == HTTP_METH_POST) {
1171 /* POST requests may require to read extra CRLF sent by broken
1172 * browsers and which could cause an RST to be sent upon close
1173 * on some systems (eg: Linux). */
1174 channel_auto_read(req);
1175 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001176 DBG_TRACE_DEVEL("waiting for the end of the HTTP txn",
1177 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001178 return 0;
1179
1180 missing_data_or_waiting:
1181 /* stop waiting for data if the input is closed before the end */
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001182 if (msg->msg_state < HTTP_MSG_ENDING && req->flags & CF_SHUTR)
Christopher Faulet93e02d82019-03-08 14:18:50 +01001183 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001184
1185 waiting:
1186 /* waiting for the last bits to leave the buffer */
1187 if (req->flags & CF_SHUTW)
Christopher Faulet93e02d82019-03-08 14:18:50 +01001188 goto return_srv_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001189
1190 /* When TE: chunked is used, we need to get there again to parse remaining
1191 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
1192 * And when content-length is used, we never want to let the possible
1193 * shutdown be forwarded to the other side, as the state machine will
1194 * take care of it once the client responds. It's also important to
1195 * prevent TIME_WAITs from accumulating on the backend side, and for
1196 * HTTP/2 where the last frame comes with a shutdown.
1197 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001198 if (msg->flags & HTTP_MSGF_XFER_LEN)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001199 channel_dont_close(req);
1200
1201 /* We know that more data are expected, but we couldn't send more that
1202 * what we did. So we always set the CF_EXPECT_MORE flag so that the
1203 * system knows it must not set a PUSH on this first part. Interactive
1204 * modes are already handled by the stream sock layer. We must not do
1205 * this in content-length mode because it could present the MSG_MORE
1206 * flag with the last block of forwarded data, which would cause an
1207 * additional delay to be observed by the receiver.
1208 */
Christopher Faulet2151cdd2020-07-22 16:34:59 +02001209 if (HAS_REQ_DATA_FILTERS(s))
Christopher Faulete0768eb2018-10-03 16:38:02 +02001210 req->flags |= CF_EXPECT_MORE;
1211
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001212 DBG_TRACE_DEVEL("waiting for more data to forward",
1213 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001214 return 0;
1215
Christopher Faulet93e02d82019-03-08 14:18:50 +01001216 return_cli_abort:
1217 _HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
1218 _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001219 if (sess->listener->counters)
1220 _HA_ATOMIC_ADD(&sess->listener->counters->cli_aborts, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001221 if (objt_server(s->target))
Christopher Fauletcff0f732019-12-16 16:13:44 +01001222 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.cli_aborts, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001223 if (!(s->flags & SF_ERR_MASK))
1224 s->flags |= SF_ERR_CLICL;
1225 status = 400;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001226 goto return_prx_cond;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001227
1228 return_srv_abort:
1229 _HA_ATOMIC_ADD(&sess->fe->fe_counters.srv_aborts, 1);
1230 _HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001231 if (sess->listener->counters)
1232 _HA_ATOMIC_ADD(&sess->listener->counters->srv_aborts, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001233 if (objt_server(s->target))
Christopher Fauletcff0f732019-12-16 16:13:44 +01001234 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.srv_aborts, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001235 if (!(s->flags & SF_ERR_MASK))
1236 s->flags |= SF_ERR_SRVCL;
1237 status = 502;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001238 goto return_prx_cond;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001239
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001240 return_int_err:
1241 if (!(s->flags & SF_ERR_MASK))
1242 s->flags |= SF_ERR_INTERNAL;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001243 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001244 _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01001245 if (sess->listener->counters)
1246 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001247 if (objt_server(s->target))
1248 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.internal_errors, 1);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001249 status = 500;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001250 goto return_prx_cond;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001251
Christopher Faulet93e02d82019-03-08 14:18:50 +01001252 return_bad_req:
Olivier Houcharda798bf52019-03-08 18:52:00 +01001253 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001254 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +01001255 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001256 status = 400;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001257 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001258
Christopher Fauletb8a53712019-12-16 11:29:38 +01001259 return_prx_cond:
Christopher Faulet9768c262018-10-22 09:34:31 +02001260 if (txn->status > 0) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001261 /* Note: we don't send any error if some data were already sent */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001262 http_reply_and_close(s, txn->status, NULL);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001263 } else {
Christopher Faulet93e02d82019-03-08 14:18:50 +01001264 txn->status = status;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001265 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001266 }
1267 req->analysers &= AN_REQ_FLT_END;
1268 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 +01001269 if (!(s->flags & SF_ERR_MASK))
1270 s->flags |= SF_ERR_PRXCOND;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001271 if (!(s->flags & SF_FINST_MASK))
1272 s->flags |= ((txn->rsp.msg_state < HTTP_MSG_ERROR) ? SF_FINST_H : SF_FINST_D);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001273 DBG_TRACE_DEVEL("leaving on error ",
1274 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001275 return 0;
1276}
1277
Olivier Houcharda254a372019-04-05 15:30:12 +02001278/* Reset the stream and the backend stream_interface to a situation suitable for attemption connection */
1279/* Returns 0 if we can attempt to retry, -1 otherwise */
1280static __inline int do_l7_retry(struct stream *s, struct stream_interface *si)
1281{
Christopher Faulet5b82cc52020-10-12 15:18:50 +02001282 struct channel *req = &s->req;
1283 struct channel *res = &s->res;
Olivier Houcharda254a372019-04-05 15:30:12 +02001284
1285 si->conn_retries--;
1286 if (si->conn_retries < 0)
Christopher Faulet5b82cc52020-10-12 15:18:50 +02001287 goto no_retry;
1288
1289 if (b_is_null(&req->buf) && !channel_alloc_buffer(req, &s->buffer_wait))
1290 goto no_retry;
Olivier Houcharda254a372019-04-05 15:30:12 +02001291
Willy Tarreau223995e2019-05-04 10:38:31 +02001292 if (objt_server(s->target))
1293 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.retries, 1);
1294 _HA_ATOMIC_ADD(&s->be->be_counters.retries, 1);
1295
Olivier Houcharda254a372019-04-05 15:30:12 +02001296 /* Remove any write error from the request, and read error from the response */
1297 req->flags &= ~(CF_WRITE_ERROR | CF_WRITE_TIMEOUT | CF_SHUTW | CF_SHUTW_NOW);
1298 res->flags &= ~(CF_READ_ERROR | CF_READ_TIMEOUT | CF_SHUTR | CF_EOI | CF_READ_NULL | CF_SHUTR_NOW);
1299 res->analysers = 0;
1300 si->flags &= ~(SI_FL_ERR | SI_FL_EXP | SI_FL_RXBLK_SHUT);
Olivier Houchard8cabc972020-05-12 22:18:14 +02001301 s->flags &= ~SF_ADDR_SET;
Olivier Houchard4bd58672019-07-12 16:16:59 +02001302 stream_choose_redispatch(s);
Olivier Houcharda254a372019-04-05 15:30:12 +02001303 si->exp = TICK_ETERNITY;
1304 res->rex = TICK_ETERNITY;
1305 res->to_forward = 0;
1306 res->analyse_exp = TICK_ETERNITY;
1307 res->total = 0;
Olivier Houchard4bd58672019-07-12 16:16:59 +02001308 s->flags &= ~(SF_ERR_SRVTO | SF_ERR_SRVCL);
Olivier Houcharda254a372019-04-05 15:30:12 +02001309 si_release_endpoint(&s->si[1]);
Olivier Houcharda254a372019-04-05 15:30:12 +02001310
Christopher Faulet5b82cc52020-10-12 15:18:50 +02001311 b_reset(&req->buf);
1312 memcpy(b_orig(&req->buf), b_orig(&si->l7_buffer), b_size(&si->l7_buffer));
1313 b_set_data(&req->buf, b_size(&req->buf));
1314 co_set_data(req, b_data(&si->l7_buffer));
1315
Ilya Shipitsinacf84592021-02-06 22:29:08 +05001316 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 +02001317 b_reset(&res->buf);
1318 co_set_data(res, 0);
1319 return 0;
Christopher Faulet5b82cc52020-10-12 15:18:50 +02001320
1321 no_retry:
1322 b_free(&si->l7_buffer);
1323 return -1;
Olivier Houcharda254a372019-04-05 15:30:12 +02001324}
1325
Christopher Faulete0768eb2018-10-03 16:38:02 +02001326/* This stream analyser waits for a complete HTTP response. It returns 1 if the
1327 * processing can continue on next analysers, or zero if it either needs more
1328 * data or wants to immediately abort the response (eg: timeout, error, ...). It
1329 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->res.analysers
1330 * when it has nothing left to do, and may remove any analyser when it wants to
1331 * abort.
1332 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001333int http_wait_for_response(struct stream *s, struct channel *rep, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001334{
Christopher Faulet9768c262018-10-22 09:34:31 +02001335 /*
1336 * We will analyze a complete HTTP response to check the its syntax.
1337 *
1338 * Once the start line and all headers are received, we may perform a
1339 * capture of the error (if any), and we will set a few fields. We also
1340 * logging and finally headers capture.
1341 */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001342 struct session *sess = s->sess;
1343 struct http_txn *txn = s->txn;
1344 struct http_msg *msg = &txn->rsp;
Christopher Faulet9768c262018-10-22 09:34:31 +02001345 struct htx *htx;
Olivier Houcharda254a372019-04-05 15:30:12 +02001346 struct stream_interface *si_b = &s->si[1];
Christopher Faulet61608322018-11-23 16:23:45 +01001347 struct connection *srv_conn;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001348 struct htx_sl *sl;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001349 int n;
1350
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001351 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001352
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001353 htx = htxbuf(&rep->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001354
Willy Tarreau4236f032019-03-05 10:43:32 +01001355 /* Parsing errors are caught here */
1356 if (htx->flags & HTX_FL_PARSING_ERROR)
1357 goto return_bad_res;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001358 if (htx->flags & HTX_FL_PROCESSING_ERROR)
1359 goto return_int_err;
Willy Tarreau4236f032019-03-05 10:43:32 +01001360
Christopher Faulete0768eb2018-10-03 16:38:02 +02001361 /*
1362 * Now we quickly check if we have found a full valid response.
1363 * If not so, we check the FD and buffer states before leaving.
1364 * A full response is indicated by the fact that we have seen
1365 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
1366 * responses are checked first.
1367 *
1368 * Depending on whether the client is still there or not, we
1369 * may send an error response back or not. Note that normally
1370 * we should only check for HTTP status there, and check I/O
1371 * errors somewhere else.
1372 */
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001373 next_one:
Christopher Faulet29f17582019-05-23 11:03:26 +02001374 if (unlikely(htx_is_empty(htx) || htx->first == -1)) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001375 /* 1: have we encountered a read error ? */
1376 if (rep->flags & CF_READ_ERROR) {
Olivier Houchard865d8392019-05-03 22:46:27 +02001377 struct connection *conn = NULL;
1378
Olivier Houchard865d8392019-05-03 22:46:27 +02001379 if (objt_cs(s->si[1].end))
1380 conn = objt_cs(s->si[1].end)->conn;
1381
1382 if (si_b->flags & SI_FL_L7_RETRY &&
1383 (!conn || conn->err_code != CO_ER_SSL_EARLY_FAILED)) {
Olivier Houcharda254a372019-04-05 15:30:12 +02001384 /* If we arrive here, then CF_READ_ERROR was
1385 * set by si_cs_recv() because we matched a
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05001386 * status, otherwise it would have removed
Olivier Houcharda254a372019-04-05 15:30:12 +02001387 * the SI_FL_L7_RETRY flag, so it's ok not
1388 * to check s->be->retry_type.
1389 */
Christopher Faulet5b82cc52020-10-12 15:18:50 +02001390 if (co_data(rep) || do_l7_retry(s, si_b) == 0) {
1391 DBG_TRACE_DEVEL("leaving on L7 retry",
1392 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharda254a372019-04-05 15:30:12 +02001393 return 0;
Christopher Faulet5b82cc52020-10-12 15:18:50 +02001394 }
Olivier Houcharda254a372019-04-05 15:30:12 +02001395 }
1396
Olivier Houchard6db16992019-05-17 15:40:49 +02001397 if (txn->flags & TX_NOT_FIRST)
1398 goto abort_keep_alive;
1399
Olivier Houcharda798bf52019-03-08 18:52:00 +01001400 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001401 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001402 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001403 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001404 }
1405
Christopher Faulete0768eb2018-10-03 16:38:02 +02001406 rep->analysers &= AN_RES_FLT_END;
1407 txn->status = 502;
1408
1409 /* Check to see if the server refused the early data.
1410 * If so, just send a 425
1411 */
Willy Tarreauee99aaf2020-06-23 05:58:20 +02001412 if (conn && conn->err_code == CO_ER_SSL_EARLY_FAILED) {
Olivier Houchard865d8392019-05-03 22:46:27 +02001413 if ((s->be->retry_type & PR_RE_EARLY_ERROR) &&
Olivier Houchardad26d8d2019-05-10 17:48:28 +02001414 (si_b->flags & SI_FL_L7_RETRY) &&
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001415 do_l7_retry(s, si_b) == 0) {
1416 DBG_TRACE_DEVEL("leaving on L7 retry",
1417 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houchard865d8392019-05-03 22:46:27 +02001418 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001419 }
Olivier Houchard865d8392019-05-03 22:46:27 +02001420 txn->status = 425;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001421 }
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001422 else
1423 stream_inc_http_fail_ctr(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001424
1425 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001426 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001427
1428 if (!(s->flags & SF_ERR_MASK))
1429 s->flags |= SF_ERR_SRVCL;
1430 if (!(s->flags & SF_FINST_MASK))
1431 s->flags |= SF_FINST_H;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001432 DBG_TRACE_DEVEL("leaving on error",
1433 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001434 return 0;
1435 }
1436
Christopher Faulet9768c262018-10-22 09:34:31 +02001437 /* 2: read timeout : return a 504 to the client. */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001438 else if (rep->flags & CF_READ_TIMEOUT) {
Olivier Houcharda254a372019-04-05 15:30:12 +02001439 if ((si_b->flags & SI_FL_L7_RETRY) &&
1440 (s->be->retry_type & PR_RE_TIMEOUT)) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001441 if (co_data(rep) || do_l7_retry(s, si_b) == 0) {
1442 DBG_TRACE_DEVEL("leaving on L7 retry",
1443 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharda254a372019-04-05 15:30:12 +02001444 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001445 }
Olivier Houcharda254a372019-04-05 15:30:12 +02001446 }
Olivier Houcharda798bf52019-03-08 18:52:00 +01001447 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001448 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001449 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001450 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001451 }
1452
Christopher Faulete0768eb2018-10-03 16:38:02 +02001453 rep->analysers &= AN_RES_FLT_END;
1454 txn->status = 504;
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001455 stream_inc_http_fail_ctr(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001456 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001457 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001458
1459 if (!(s->flags & SF_ERR_MASK))
1460 s->flags |= SF_ERR_SRVTO;
1461 if (!(s->flags & SF_FINST_MASK))
1462 s->flags |= SF_FINST_H;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001463 DBG_TRACE_DEVEL("leaving on error",
1464 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001465 return 0;
1466 }
1467
Christopher Faulet9768c262018-10-22 09:34:31 +02001468 /* 3: client abort with an abortonclose */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001469 else if ((rep->flags & CF_SHUTR) && ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001470 _HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
1471 _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001472 if (sess->listener->counters)
1473 _HA_ATOMIC_ADD(&sess->listener->counters->cli_aborts, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001474 if (objt_server(s->target))
Olivier Houcharda798bf52019-03-08 18:52:00 +01001475 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.cli_aborts, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001476
1477 rep->analysers &= AN_RES_FLT_END;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001478 txn->status = 400;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001479 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001480
1481 if (!(s->flags & SF_ERR_MASK))
1482 s->flags |= SF_ERR_CLICL;
1483 if (!(s->flags & SF_FINST_MASK))
1484 s->flags |= SF_FINST_H;
1485
1486 /* process_stream() will take care of the error */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001487 DBG_TRACE_DEVEL("leaving on error",
1488 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001489 return 0;
1490 }
1491
Christopher Faulet9768c262018-10-22 09:34:31 +02001492 /* 4: close from server, capture the response if the server has started to respond */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001493 else if (rep->flags & CF_SHUTR) {
Olivier Houcharda254a372019-04-05 15:30:12 +02001494 if ((si_b->flags & SI_FL_L7_RETRY) &&
1495 (s->be->retry_type & PR_RE_DISCONNECTED)) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001496 if (co_data(rep) || do_l7_retry(s, si_b) == 0) {
1497 DBG_TRACE_DEVEL("leaving on L7 retry",
1498 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharda254a372019-04-05 15:30:12 +02001499 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001500 }
Olivier Houcharda254a372019-04-05 15:30:12 +02001501 }
1502
Olivier Houchard6db16992019-05-17 15:40:49 +02001503 if (txn->flags & TX_NOT_FIRST)
1504 goto abort_keep_alive;
1505
Olivier Houcharda798bf52019-03-08 18:52:00 +01001506 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001507 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001508 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001509 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001510 }
1511
Christopher Faulete0768eb2018-10-03 16:38:02 +02001512 rep->analysers &= AN_RES_FLT_END;
1513 txn->status = 502;
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001514 stream_inc_http_fail_ctr(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001515 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001516 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001517
1518 if (!(s->flags & SF_ERR_MASK))
1519 s->flags |= SF_ERR_SRVCL;
1520 if (!(s->flags & SF_FINST_MASK))
1521 s->flags |= SF_FINST_H;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001522 DBG_TRACE_DEVEL("leaving on error",
1523 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001524 return 0;
1525 }
1526
Christopher Faulet9768c262018-10-22 09:34:31 +02001527 /* 5: write error to client (we don't send any message then) */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001528 else if (rep->flags & CF_WRITE_ERROR) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001529 if (txn->flags & TX_NOT_FIRST)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001530 goto abort_keep_alive;
1531
Olivier Houcharda798bf52019-03-08 18:52:00 +01001532 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001533 if (objt_server(s->target))
1534 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001535 rep->analysers &= AN_RES_FLT_END;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001536
1537 if (!(s->flags & SF_ERR_MASK))
1538 s->flags |= SF_ERR_CLICL;
1539 if (!(s->flags & SF_FINST_MASK))
1540 s->flags |= SF_FINST_H;
1541
1542 /* process_stream() will take care of the error */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001543 DBG_TRACE_DEVEL("leaving on error",
1544 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001545 return 0;
1546 }
1547
1548 channel_dont_close(rep);
1549 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001550 DBG_TRACE_DEVEL("waiting for more data",
1551 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001552 return 0;
1553 }
1554
1555 /* More interesting part now : we know that we have a complete
1556 * response which at least looks like HTTP. We have an indicator
1557 * of each header's length, so we can parse them quickly.
1558 */
Christopher Faulet29f17582019-05-23 11:03:26 +02001559 BUG_ON(htx_get_first_type(htx) != HTX_BLK_RES_SL);
Christopher Faulet297fbb42019-05-13 14:41:27 +02001560 sl = http_get_stline(htx);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001561
Christopher Faulet5b82cc52020-10-12 15:18:50 +02001562 if ((si_b->flags & SI_FL_L7_RETRY) &&
1563 l7_status_match(s->be, sl->info.res.status) &&
1564 do_l7_retry(s, si_b) == 0) {
1565 DBG_TRACE_DEVEL("leaving on L7 retry", STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
1566 return 0;
1567 }
1568 b_free(&s->si[1].l7_buffer);
1569
1570 msg->msg_state = HTTP_MSG_BODY;
1571
Christopher Faulet9768c262018-10-22 09:34:31 +02001572 /* 0: we might have to print this header in debug mode */
1573 if (unlikely((global.mode & MODE_DEBUG) &&
1574 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
1575 int32_t pos;
1576
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001577 http_debug_stline("srvrep", s, sl);
Christopher Faulet9768c262018-10-22 09:34:31 +02001578
Christopher Fauleta3f15502019-05-13 15:27:23 +02001579 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001580 struct htx_blk *blk = htx_get_blk(htx, pos);
1581 enum htx_blk_type type = htx_get_blk_type(blk);
1582
1583 if (type == HTX_BLK_EOH)
1584 break;
1585 if (type != HTX_BLK_HDR)
1586 continue;
1587
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001588 http_debug_hdr("srvhdr", s,
1589 htx_get_blk_name(htx, blk),
1590 htx_get_blk_value(htx, blk));
Christopher Faulet9768c262018-10-22 09:34:31 +02001591 }
1592 }
1593
Christopher Faulet03599112018-11-27 11:21:21 +01001594 /* 1: get the status code and the version. Also set HTTP flags */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001595 txn->status = sl->info.res.status;
Christopher Faulet03599112018-11-27 11:21:21 +01001596 if (sl->flags & HTX_SL_F_VER_11)
Christopher Faulet9768c262018-10-22 09:34:31 +02001597 msg->flags |= HTTP_MSGF_VER_11;
Christopher Faulet03599112018-11-27 11:21:21 +01001598 if (sl->flags & HTX_SL_F_XFER_LEN) {
1599 msg->flags |= HTTP_MSGF_XFER_LEN;
Christopher Faulet2a408542020-11-20 14:22:37 +01001600 if (sl->flags & HTX_SL_F_CLEN)
1601 msg->flags |= HTTP_MSGF_CNT_LEN;
1602 else if (sl->flags & HTX_SL_F_CHNK)
1603 msg->flags |= HTTP_MSGF_TE_CHNK;
Christopher Faulet03599112018-11-27 11:21:21 +01001604 }
Christopher Faulet2a408542020-11-20 14:22:37 +01001605 if (sl->flags & HTX_SL_F_BODYLESS)
1606 msg->flags |= HTTP_MSGF_BODYLESS;
Christopher Faulet576c3582021-01-08 15:53:01 +01001607 if (sl->flags & HTX_SL_F_CONN_UPG)
1608 msg->flags |= HTTP_MSGF_CONN_UPG;
Christopher Faulet9768c262018-10-22 09:34:31 +02001609
1610 n = txn->status / 100;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001611 if (n < 1 || n > 5)
1612 n = 0;
Christopher Faulet9768c262018-10-22 09:34:31 +02001613
Christopher Faulete0768eb2018-10-03 16:38:02 +02001614 /* when the client triggers a 4xx from the server, it's most often due
1615 * to a missing object or permission. These events should be tracked
1616 * because if they happen often, it may indicate a brute force or a
1617 * vulnerability scan.
1618 */
1619 if (n == 4)
1620 stream_inc_http_err_ctr(s);
1621
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001622 if (n == 5 && txn->status != 501 && txn->status != 505)
1623 stream_inc_http_fail_ctr(s);
1624
Marcin Deranek3c27dda2020-05-15 18:32:51 +02001625 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001626 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.p.http.rsp[n], 1);
Marcin Deranek3c27dda2020-05-15 18:32:51 +02001627 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.p.http.cum_req, 1);
1628 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001629
Christopher Faulete0768eb2018-10-03 16:38:02 +02001630 /* Adjust server's health based on status code. Note: status codes 501
1631 * and 505 are triggered on demand by client request, so we must not
1632 * count them as server failures.
1633 */
1634 if (objt_server(s->target)) {
1635 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001636 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_OK);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001637 else
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001638 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_STS);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001639 }
1640
1641 /*
1642 * We may be facing a 100-continue response, or any other informational
1643 * 1xx response which is non-final, in which case this is not the right
1644 * response, and we're waiting for the next one. Let's allow this response
1645 * to go to the client and wait for the next one. There's an exception for
1646 * 101 which is used later in the code to switch protocols.
1647 */
1648 if (txn->status < 200 &&
1649 (txn->status == 100 || txn->status >= 102)) {
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001650 FLT_STRM_CB(s, flt_http_reset(s, msg));
Christopher Faulet421e7692019-06-13 11:16:45 +02001651 htx->first = channel_htx_fwd_headers(rep, htx);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001652 msg->msg_state = HTTP_MSG_RPBEFORE;
Christopher Faulet3499f622019-09-03 15:23:54 +02001653 msg->flags = 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001654 txn->status = 0;
1655 s->logs.t_data = -1; /* was not a response yet */
Christopher Faulet7d518452020-08-31 11:07:07 +02001656 rep->flags |= CF_SEND_DONTWAIT; /* Send ASAP informational messages */
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001657 goto next_one;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001658 }
1659
Christopher Faulet6e6c7b12021-01-08 16:02:05 +01001660 /* A 101-switching-protocols must contains a Connection header with the
1661 * "upgrade" option and the request too. It means both are agree to
1662 * upgrade. It is not so strict because there is no test on the Upgrade
1663 * header content. But it is probably stronger enough for now.
1664 */
1665 if (txn->status == 101 &&
1666 (!(txn->req.flags & HTTP_MSGF_CONN_UPG) || !(txn->rsp.flags & HTTP_MSGF_CONN_UPG)))
1667 goto return_bad_res;
1668
Christopher Faulete0768eb2018-10-03 16:38:02 +02001669 /*
1670 * 2: check for cacheability.
1671 */
1672
1673 switch (txn->status) {
1674 case 200:
1675 case 203:
1676 case 204:
1677 case 206:
1678 case 300:
1679 case 301:
1680 case 404:
1681 case 405:
1682 case 410:
1683 case 414:
1684 case 501:
1685 break;
1686 default:
1687 /* RFC7231#6.1:
1688 * Responses with status codes that are defined as
1689 * cacheable by default (e.g., 200, 203, 204, 206,
1690 * 300, 301, 404, 405, 410, 414, and 501 in this
1691 * specification) can be reused by a cache with
1692 * heuristic expiration unless otherwise indicated
1693 * by the method definition or explicit cache
1694 * controls [RFC7234]; all other status codes are
1695 * not cacheable by default.
1696 */
1697 txn->flags &= ~(TX_CACHEABLE | TX_CACHE_COOK);
1698 break;
1699 }
1700
1701 /*
1702 * 3: we may need to capture headers
1703 */
1704 s->logs.logwait &= ~LW_RESP;
1705 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->res_cap))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001706 http_capture_headers(htx, s->res_cap, sess->fe->rsp_cap);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001707
Christopher Faulet9768c262018-10-22 09:34:31 +02001708 /* Skip parsing if no content length is possible. */
Christopher Fauletc75668e2020-12-07 18:10:32 +01001709 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status >= 200 && txn->status < 300) ||
Christopher Faulete0768eb2018-10-03 16:38:02 +02001710 txn->status == 101)) {
1711 /* Either we've established an explicit tunnel, or we're
1712 * switching the protocol. In both cases, we're very unlikely
1713 * to understand the next protocols. We have to switch to tunnel
1714 * mode, so that we transfer the request and responses then let
1715 * this protocol pass unmodified. When we later implement specific
1716 * parsers for such protocols, we'll want to check the Upgrade
1717 * header which contains information about that protocol for
1718 * responses with status 101 (eg: see RFC2817 about TLS).
1719 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02001720 txn->flags |= TX_CON_WANT_TUN;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001721 }
1722
Christopher Faulet61608322018-11-23 16:23:45 +01001723 /* check for NTML authentication headers in 401 (WWW-Authenticate) and
1724 * 407 (Proxy-Authenticate) responses and set the connection to private
1725 */
1726 srv_conn = cs_conn(objt_cs(s->si[1].end));
1727 if (srv_conn) {
1728 struct ist hdr;
1729 struct http_hdr_ctx ctx;
1730
1731 if (txn->status == 401)
1732 hdr = ist("WWW-Authenticate");
1733 else if (txn->status == 407)
1734 hdr = ist("Proxy-Authenticate");
1735 else
1736 goto end;
1737
1738 ctx.blk = NULL;
1739 while (http_find_header(htx, hdr, &ctx, 0)) {
Willy Tarreauf1dcced2020-05-07 19:27:02 +02001740 /* If www-authenticate contains "Negotiate", "Nego2", or "NTLM",
1741 * possibly followed by blanks and a base64 string, the connection
1742 * is private. Since it's a mess to deal with, we only check for
1743 * values starting with "NTLM" or "Nego". Note that often multiple
1744 * headers are sent by the server there.
1745 */
1746 if ((ctx.value.len >= 4 && strncasecmp(ctx.value.ptr, "Nego", 4) == 0) ||
Willy Tarreau49a1d282020-05-07 19:10:15 +02001747 (ctx.value.len >= 4 && strncasecmp(ctx.value.ptr, "NTLM", 4) == 0)) {
Olivier Houchard250031e2019-05-29 15:01:50 +02001748 sess->flags |= SESS_FL_PREFER_LAST;
Christopher Faulet08016ab2020-07-01 16:10:06 +02001749 conn_set_owner(srv_conn, sess, NULL);
Christopher Faulet21ddc742020-07-01 15:26:14 +02001750 conn_set_private(srv_conn);
Ilya Shipitsin6b79f382020-07-23 00:32:55 +05001751 /* If it fail now, the same will be done in mux->detach() callback */
Christopher Faulet08016ab2020-07-01 16:10:06 +02001752 session_add_conn(srv_conn->owner, srv_conn, srv_conn->target);
Willy Tarreauf1dcced2020-05-07 19:27:02 +02001753 break;
Olivier Houchard250031e2019-05-29 15:01:50 +02001754 }
Christopher Faulet61608322018-11-23 16:23:45 +01001755 }
1756 }
1757
1758 end:
Christopher Faulete0768eb2018-10-03 16:38:02 +02001759 /* we want to have the response time before we start processing it */
1760 s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now);
1761
1762 /* end of job, return OK */
1763 rep->analysers &= ~an_bit;
1764 rep->analyse_exp = TICK_ETERNITY;
1765 channel_auto_close(rep);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001766 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001767 return 1;
1768
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001769 return_int_err:
Christopher Fauletcff0f732019-12-16 16:13:44 +01001770 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01001771 _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001772 if (sess->listener->counters)
1773 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01001774 if (objt_server(s->target))
1775 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.internal_errors, 1);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001776 txn->status = 500;
1777 if (!(s->flags & SF_ERR_MASK))
1778 s->flags |= SF_ERR_INTERNAL;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001779 goto return_prx_cond;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001780
1781 return_bad_res:
Olivier Houcharda798bf52019-03-08 18:52:00 +01001782 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulet47365272018-10-31 17:40:50 +01001783 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001784 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001785 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Christopher Faulet47365272018-10-31 17:40:50 +01001786 }
Olivier Houcharde3249a92019-05-03 23:01:47 +02001787 if ((s->be->retry_type & PR_RE_JUNK_REQUEST) &&
Olivier Houchardad26d8d2019-05-10 17:48:28 +02001788 (si_b->flags & SI_FL_L7_RETRY) &&
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001789 do_l7_retry(s, si_b) == 0) {
1790 DBG_TRACE_DEVEL("leaving on L7 retry",
1791 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharde3249a92019-05-03 23:01:47 +02001792 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001793 }
Christopher Faulet47365272018-10-31 17:40:50 +01001794 txn->status = 502;
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001795 stream_inc_http_fail_ctr(s);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001796 /* fall through */
1797
Christopher Fauletb8a53712019-12-16 11:29:38 +01001798 return_prx_cond:
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001799 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet47365272018-10-31 17:40:50 +01001800
1801 if (!(s->flags & SF_ERR_MASK))
1802 s->flags |= SF_ERR_PRXCOND;
1803 if (!(s->flags & SF_FINST_MASK))
1804 s->flags |= SF_FINST_H;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001805
1806 s->si[1].flags |= SI_FL_NOLINGER;
1807 rep->analysers &= AN_RES_FLT_END;
Christopher Faulete58c0002020-03-02 16:21:01 +01001808 s->req.analysers &= AN_REQ_FLT_END;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001809 rep->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001810 DBG_TRACE_DEVEL("leaving on error",
1811 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulet47365272018-10-31 17:40:50 +01001812 return 0;
1813
Christopher Faulete0768eb2018-10-03 16:38:02 +02001814 abort_keep_alive:
1815 /* A keep-alive request to the server failed on a network error.
1816 * The client is required to retry. We need to close without returning
1817 * any other information so that the client retries.
1818 */
1819 txn->status = 0;
1820 rep->analysers &= AN_RES_FLT_END;
1821 s->req.analysers &= AN_REQ_FLT_END;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001822 s->logs.logwait = 0;
1823 s->logs.level = 0;
1824 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001825 http_reply_and_close(s, txn->status, NULL);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001826 DBG_TRACE_DEVEL("leaving by closing K/A connection",
1827 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001828 return 0;
1829}
1830
1831/* This function performs all the processing enabled for the current response.
1832 * It normally returns 1 unless it wants to break. It relies on buffers flags,
1833 * and updates s->res.analysers. It might make sense to explode it into several
1834 * other functions. It works like process_request (see indications above).
1835 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001836int http_process_res_common(struct stream *s, struct channel *rep, int an_bit, struct proxy *px)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001837{
1838 struct session *sess = s->sess;
1839 struct http_txn *txn = s->txn;
1840 struct http_msg *msg = &txn->rsp;
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001841 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001842 struct proxy *cur_proxy;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001843 enum rule_result ret = HTTP_RULE_RES_CONT;
1844
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001845 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
1846 return 0;
Christopher Faulet9768c262018-10-22 09:34:31 +02001847
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001848 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001849
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001850 htx = htxbuf(&rep->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001851
1852 /* The stats applet needs to adjust the Connection header but we don't
1853 * apply any filter there.
1854 */
1855 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
1856 rep->analysers &= ~an_bit;
1857 rep->analyse_exp = TICK_ETERNITY;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001858 goto end;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001859 }
1860
1861 /*
1862 * We will have to evaluate the filters.
1863 * As opposed to version 1.2, now they will be evaluated in the
1864 * filters order and not in the header order. This means that
1865 * each filter has to be validated among all headers.
1866 *
1867 * Filters are tried with ->be first, then with ->fe if it is
1868 * different from ->be.
1869 *
1870 * Maybe we are in resume condiion. In this case I choose the
1871 * "struct proxy" which contains the rule list matching the resume
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05001872 * pointer. If none of these "struct proxy" match, I initialise
Christopher Faulete0768eb2018-10-03 16:38:02 +02001873 * the process with the first one.
1874 *
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05001875 * In fact, I check only correspondence between the current list
Christopher Faulete0768eb2018-10-03 16:38:02 +02001876 * pointer and the ->fe rule list. If it doesn't match, I initialize
1877 * the loop with the ->be.
1878 */
1879 if (s->current_rule_list == &sess->fe->http_res_rules)
1880 cur_proxy = sess->fe;
1881 else
1882 cur_proxy = s->be;
1883 while (1) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001884 /* evaluate http-response rules */
1885 if (ret == HTTP_RULE_RES_CONT) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001886 ret = http_res_get_intercept_rule(cur_proxy, &cur_proxy->http_res_rules, s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001887
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001888 switch (ret) {
1889 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
1890 goto return_prx_yield;
1891
1892 case HTTP_RULE_RES_CONT:
1893 case HTTP_RULE_RES_STOP: /* nothing to do */
1894 break;
1895
1896 case HTTP_RULE_RES_DENY: /* deny or tarpit */
1897 goto deny;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001898
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001899 case HTTP_RULE_RES_ABRT: /* abort request, response already sent */
1900 goto return_prx_cond;
1901
1902 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
Christopher Fauletb8a53712019-12-16 11:29:38 +01001903 goto done;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001904
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001905 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
1906 goto return_bad_res;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001907
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001908 case HTTP_RULE_RES_ERROR: /* failed with a bad request */
1909 goto return_int_err;
1910 }
1911
1912 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001913
Christopher Faulete0768eb2018-10-03 16:38:02 +02001914 /* check whether we're already working on the frontend */
1915 if (cur_proxy == sess->fe)
1916 break;
1917 cur_proxy = sess->fe;
1918 }
1919
Christopher Faulete0768eb2018-10-03 16:38:02 +02001920 /* OK that's all we can do for 1xx responses */
1921 if (unlikely(txn->status < 200 && txn->status != 101))
Christopher Fauletf2824e62018-10-01 12:12:37 +02001922 goto end;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001923
1924 /*
1925 * Now check for a server cookie.
1926 */
1927 if (s->be->cookie_name || sess->fe->capture_name || (s->be->options & PR_O_CHK_CACHE))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001928 http_manage_server_side_cookies(s, rep);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001929
1930 /*
1931 * Check for cache-control or pragma headers if required.
1932 */
1933 if ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001934 http_check_response_for_cacheability(s, rep);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001935
1936 /*
1937 * Add server cookie in the response if needed
1938 */
1939 if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) &&
1940 !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) &&
1941 (!(s->flags & SF_DIRECT) ||
1942 ((s->be->cookie_maxidle || txn->cookie_last_date) &&
1943 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
1944 (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
1945 (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
1946 (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
1947 !(s->flags & SF_IGNORE_PRST)) {
1948 /* the server is known, it's not the one the client requested, or the
1949 * cookie's last seen date needs to be refreshed. We have to
1950 * insert a set-cookie here, except if we want to insert only on POST
1951 * requests and this one isn't. Note that servers which don't have cookies
1952 * (eg: some backup servers) will return a full cookie removal request.
1953 */
1954 if (!objt_server(s->target)->cookie) {
1955 chunk_printf(&trash,
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001956 "%s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
Christopher Faulete0768eb2018-10-03 16:38:02 +02001957 s->be->cookie_name);
1958 }
1959 else {
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001960 chunk_printf(&trash, "%s=%s", s->be->cookie_name, objt_server(s->target)->cookie);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001961
1962 if (s->be->cookie_maxidle || s->be->cookie_maxlife) {
1963 /* emit last_date, which is mandatory */
1964 trash.area[trash.data++] = COOKIE_DELIM_DATE;
1965 s30tob64((date.tv_sec+3) >> 2,
1966 trash.area + trash.data);
1967 trash.data += 5;
1968
1969 if (s->be->cookie_maxlife) {
1970 /* emit first_date, which is either the original one or
1971 * the current date.
1972 */
1973 trash.area[trash.data++] = COOKIE_DELIM_DATE;
1974 s30tob64(txn->cookie_first_date ?
1975 txn->cookie_first_date >> 2 :
1976 (date.tv_sec+3) >> 2,
1977 trash.area + trash.data);
1978 trash.data += 5;
1979 }
1980 }
1981 chunk_appendf(&trash, "; path=/");
1982 }
1983
1984 if (s->be->cookie_domain)
1985 chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain);
1986
1987 if (s->be->ck_opts & PR_CK_HTTPONLY)
1988 chunk_appendf(&trash, "; HttpOnly");
1989
1990 if (s->be->ck_opts & PR_CK_SECURE)
1991 chunk_appendf(&trash, "; Secure");
1992
Christopher Faulet2f533902020-01-21 11:06:48 +01001993 if (s->be->cookie_attrs)
1994 chunk_appendf(&trash, "; %s", s->be->cookie_attrs);
1995
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001996 if (unlikely(!http_add_header(htx, ist("Set-Cookie"), ist2(trash.area, trash.data))))
Christopher Fauletb8a53712019-12-16 11:29:38 +01001997 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001998
1999 txn->flags &= ~TX_SCK_MASK;
2000 if (__objt_server(s->target)->cookie && (s->flags & SF_DIRECT))
2001 /* the server did not change, only the date was updated */
2002 txn->flags |= TX_SCK_UPDATED;
2003 else
2004 txn->flags |= TX_SCK_INSERTED;
2005
2006 /* Here, we will tell an eventual cache on the client side that we don't
2007 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
2008 * Some caches understand the correct form: 'no-cache="set-cookie"', but
2009 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
2010 */
2011 if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
2012
2013 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
2014
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002015 if (unlikely(!http_add_header(htx, ist("Cache-control"), ist("private"))))
Christopher Fauletb8a53712019-12-16 11:29:38 +01002016 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002017 }
2018 }
2019
2020 /*
2021 * Check if result will be cacheable with a cookie.
2022 * We'll block the response if security checks have caught
2023 * nasty things such as a cacheable cookie.
2024 */
2025 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
2026 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
2027 (s->be->options & PR_O_CHK_CACHE)) {
2028 /* we're in presence of a cacheable response containing
2029 * a set-cookie header. We'll block it as requested by
2030 * the 'checkcache' option, and send an alert.
2031 */
Christopher Faulete0768eb2018-10-03 16:38:02 +02002032 ha_alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
2033 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
2034 send_log(s->be, LOG_ALERT,
2035 "Blocking cacheable cookie in response from instance %s, server %s.\n",
2036 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
Christopher Fauletb8a53712019-12-16 11:29:38 +01002037 goto deny;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002038 }
2039
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002040 end:
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002041 /*
2042 * Evaluate after-response rules before forwarding the response. rules
2043 * from the backend are evaluated first, then one from the frontend if
2044 * it differs.
2045 */
2046 if (!http_eval_after_res_rules(s))
2047 goto return_int_err;
2048
Christopher Faulete0768eb2018-10-03 16:38:02 +02002049 /* Always enter in the body analyzer */
2050 rep->analysers &= ~AN_RES_FLT_XFER_DATA;
2051 rep->analysers |= AN_RES_HTTP_XFER_BODY;
2052
2053 /* if the user wants to log as soon as possible, without counting
2054 * bytes from the server, then this is the right moment. We have
2055 * to temporarily assign bytes_out to log what we currently have.
2056 */
2057 if (!LIST_ISEMPTY(&sess->fe->logformat) && !(s->logs.logwait & LW_BYTES)) {
2058 s->logs.t_close = s->logs.t_data; /* to get a valid end date */
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002059 s->logs.bytes_out = htx->data;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002060 s->do_log(s);
2061 s->logs.bytes_out = 0;
2062 }
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002063
Christopher Fauletb8a53712019-12-16 11:29:38 +01002064 done:
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002065 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002066 rep->analysers &= ~an_bit;
2067 rep->analyse_exp = TICK_ETERNITY;
2068 return 1;
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002069
Christopher Fauletb8a53712019-12-16 11:29:38 +01002070 deny:
Christopher Fauletb8a53712019-12-16 11:29:38 +01002071 _HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_resp, 1);
Christopher Fauleta08546b2019-12-16 16:07:34 +01002072 _HA_ATOMIC_ADD(&s->be->be_counters.denied_resp, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002073 if (sess->listener->counters)
2074 _HA_ATOMIC_ADD(&sess->listener->counters->denied_resp, 1);
Christopher Fauleta08546b2019-12-16 16:07:34 +01002075 if (objt_server(s->target))
2076 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.denied_resp, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002077 goto return_prx_err;
2078
2079 return_int_err:
2080 txn->status = 500;
2081 if (!(s->flags & SF_ERR_MASK))
2082 s->flags |= SF_ERR_INTERNAL;
Christopher Fauletcff0f732019-12-16 16:13:44 +01002083 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002084 _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1);
2085 if (objt_server(s->target))
2086 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01002087 if (objt_server(s->target))
2088 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002089 goto return_prx_err;
2090
2091 return_bad_res:
2092 txn->status = 502;
Willy Tarreau826f3ab2021-02-10 12:07:15 +01002093 stream_inc_http_fail_ctr(s);
Christopher Fauleta20a6532020-02-05 10:16:41 +01002094 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
2095 if (objt_server(s->target)) {
2096 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
2097 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_RSP);
2098 }
Christopher Fauletb8a53712019-12-16 11:29:38 +01002099 /* fall through */
2100
2101 return_prx_err:
2102 http_reply_and_close(s, txn->status, http_error_message(s));
2103 /* fall through */
2104
2105 return_prx_cond:
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002106 s->logs.t_data = -1; /* was not a valid response */
2107 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002108
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002109 if (!(s->flags & SF_ERR_MASK))
2110 s->flags |= SF_ERR_PRXCOND;
2111 if (!(s->flags & SF_FINST_MASK))
2112 s->flags |= SF_FINST_H;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002113
Christopher Faulete58c0002020-03-02 16:21:01 +01002114 rep->analysers &= AN_RES_FLT_END;
2115 s->req.analysers &= AN_REQ_FLT_END;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002116 rep->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002117 DBG_TRACE_DEVEL("leaving on error",
2118 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002119 return 0;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002120
2121 return_prx_yield:
2122 channel_dont_close(rep);
2123 DBG_TRACE_DEVEL("waiting for more data",
2124 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
2125 return 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002126}
2127
2128/* This function is an analyser which forwards response body (including chunk
2129 * sizes if any). It is called as soon as we must forward, even if we forward
2130 * zero byte. The only situation where it must not be called is when we're in
2131 * tunnel mode and we want to forward till the close. It's used both to forward
2132 * remaining data and to resync after end of body. It expects the msg_state to
2133 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
2134 * read more data, or 1 once we can go on with next request or end the stream.
2135 *
2136 * It is capable of compressing response data both in content-length mode and
2137 * in chunked mode. The state machines follows different flows depending on
2138 * whether content-length and chunked modes are used, since there are no
2139 * trailers in content-length :
2140 *
2141 * chk-mode cl-mode
2142 * ,----- BODY -----.
2143 * / \
2144 * V size > 0 V chk-mode
2145 * .--> SIZE -------------> DATA -------------> CRLF
2146 * | | size == 0 | last byte |
2147 * | v final crlf v inspected |
2148 * | TRAILERS -----------> DONE |
2149 * | |
2150 * `----------------------------------------------'
2151 *
2152 * Compression only happens in the DATA state, and must be flushed in final
2153 * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding
2154 * is performed at once on final states for all bytes parsed, or when leaving
2155 * on missing data.
2156 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002157int http_response_forward_body(struct stream *s, struct channel *res, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +02002158{
2159 struct session *sess = s->sess;
2160 struct http_txn *txn = s->txn;
2161 struct http_msg *msg = &s->txn->rsp;
Christopher Faulet9768c262018-10-22 09:34:31 +02002162 struct htx *htx;
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002163 int ret;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002164
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002165 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002166
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002167 htx = htxbuf(&res->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002168
Christopher Fauletb9a92f32019-09-09 10:15:21 +02002169 if (htx->flags & HTX_FL_PARSING_ERROR)
2170 goto return_bad_res;
2171 if (htx->flags & HTX_FL_PROCESSING_ERROR)
2172 goto return_int_err;
2173
Christopher Faulete0768eb2018-10-03 16:38:02 +02002174 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Christopher Fauletf2824e62018-10-01 12:12:37 +02002175 ((res->flags & CF_SHUTW) && (res->to_forward || co_data(res)))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02002176 /* Output closed while we were sending data. We must abort and
2177 * wake the other side up.
2178 */
Christopher Faulete0768eb2018-10-03 16:38:02 +02002179 msg->msg_state = HTTP_MSG_ERROR;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002180 http_end_response(s);
2181 http_end_request(s);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002182 DBG_TRACE_DEVEL("leaving on error",
2183 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002184 return 1;
2185 }
2186
Christopher Faulet9768c262018-10-22 09:34:31 +02002187 if (msg->msg_state == HTTP_MSG_BODY)
2188 msg->msg_state = HTTP_MSG_DATA;
2189
Christopher Faulete0768eb2018-10-03 16:38:02 +02002190 /* in most states, we should abort in case of early close */
2191 channel_auto_close(res);
2192
Christopher Faulete0768eb2018-10-03 16:38:02 +02002193 if (res->to_forward) {
Christopher Faulet66af0b22019-03-22 14:54:52 +01002194 if (res->to_forward == CHN_INFINITE_FORWARD) {
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002195 if (res->flags & CF_EOI)
2196 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet66af0b22019-03-22 14:54:52 +01002197 }
2198 else {
2199 /* We can't process the buffer's contents yet */
2200 res->flags |= CF_WAKE_WRITE;
2201 goto missing_data_or_waiting;
2202 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02002203 }
2204
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002205 if (msg->msg_state >= HTTP_MSG_ENDING)
2206 goto ending;
2207
Christopher Fauletc75668e2020-12-07 18:10:32 +01002208 if ((txn->meth == HTTP_METH_CONNECT && txn->status >= 200 && txn->status < 300) || txn->status == 101 ||
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002209 (!(msg->flags & HTTP_MSGF_XFER_LEN) && !HAS_RSP_DATA_FILTERS(s))) {
2210 msg->msg_state = HTTP_MSG_ENDING;
2211 goto ending;
2212 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002213
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002214 /* Forward input data. We get it by removing all outgoing data not
2215 * forwarded yet from HTX data size. If there are some data filters, we
2216 * let them decide the amount of data to forward.
Christopher Faulet9768c262018-10-22 09:34:31 +02002217 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002218 if (HAS_RSP_DATA_FILTERS(s)) {
2219 ret = flt_http_payload(s, msg, htx->data);
2220 if (ret < 0)
2221 goto return_bad_res;
Christopher Faulet421e7692019-06-13 11:16:45 +02002222 c_adv(res, ret);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002223 }
2224 else {
Christopher Faulet421e7692019-06-13 11:16:45 +02002225 c_adv(res, htx->data - co_data(res));
Christopher Faulet66af0b22019-03-22 14:54:52 +01002226 if (msg->flags & HTTP_MSGF_XFER_LEN)
2227 channel_htx_forward_forever(res, htx);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002228 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002229
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002230 if (htx->data != co_data(res))
2231 goto missing_data_or_waiting;
2232
2233 if (!(msg->flags & HTTP_MSGF_XFER_LEN) && res->flags & CF_SHUTR) {
2234 msg->msg_state = HTTP_MSG_ENDING;
2235 goto ending;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002236 }
2237
Christopher Faulet9768c262018-10-22 09:34:31 +02002238 /* Check if the end-of-message is reached and if so, switch the message
Christopher Fauletd20fdb02019-06-13 16:43:22 +02002239 * in HTTP_MSG_ENDING state. Then if all data was marked to be
2240 * forwarded, set the state to HTTP_MSG_DONE.
Christopher Faulet9768c262018-10-22 09:34:31 +02002241 */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002242 if (!(htx->flags & HTX_FL_EOM))
Christopher Faulet9768c262018-10-22 09:34:31 +02002243 goto missing_data_or_waiting;
2244
Christopher Fauletd20fdb02019-06-13 16:43:22 +02002245 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet9768c262018-10-22 09:34:31 +02002246
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002247 ending:
Christopher Faulet2151cdd2020-07-22 16:34:59 +02002248 res->flags &= ~CF_EXPECT_MORE; /* no more data are expected */
2249
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002250 /* other states, ENDING...TUNNEL */
2251 if (msg->msg_state >= HTTP_MSG_DONE)
2252 goto done;
Christopher Faulet9768c262018-10-22 09:34:31 +02002253
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002254 if (HAS_RSP_DATA_FILTERS(s)) {
2255 ret = flt_http_end(s, msg);
2256 if (ret <= 0) {
2257 if (!ret)
2258 goto missing_data_or_waiting;
2259 goto return_bad_res;
2260 }
2261 }
2262
Christopher Fauletc75668e2020-12-07 18:10:32 +01002263 if ((txn->meth == HTTP_METH_CONNECT && txn->status >= 200 && txn->status < 300) || txn->status == 101 ||
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002264 !(msg->flags & HTTP_MSGF_XFER_LEN)) {
2265 msg->msg_state = HTTP_MSG_TUNNEL;
2266 goto ending;
2267 }
2268 else {
2269 msg->msg_state = HTTP_MSG_DONE;
2270 res->to_forward = 0;
2271 }
2272
2273 done:
2274
2275 channel_dont_close(res);
2276
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002277 http_end_response(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002278 if (!(res->analysers & an_bit)) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002279 http_end_request(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002280 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
2281 if (res->flags & CF_SHUTW) {
2282 /* response errors are most likely due to the
2283 * client aborting the transfer. */
Christopher Faulet93e02d82019-03-08 14:18:50 +01002284 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002285 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02002286 goto return_bad_res;
2287 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002288 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002289 return 1;
2290 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002291 DBG_TRACE_DEVEL("waiting for the end of the HTTP txn",
2292 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002293 return 0;
2294
2295 missing_data_or_waiting:
2296 if (res->flags & CF_SHUTW)
Christopher Faulet93e02d82019-03-08 14:18:50 +01002297 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002298
2299 /* stop waiting for data if the input is closed before the end. If the
2300 * client side was already closed, it means that the client has aborted,
2301 * so we don't want to count this as a server abort. Otherwise it's a
2302 * server abort.
2303 */
Christopher Fauletd20fdb02019-06-13 16:43:22 +02002304 if (msg->msg_state < HTTP_MSG_ENDING && res->flags & CF_SHUTR) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02002305 if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))
Christopher Faulet93e02d82019-03-08 14:18:50 +01002306 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002307 /* If we have some pending data, we continue the processing */
Christopher Faulet93e02d82019-03-08 14:18:50 +01002308 if (htx_is_empty(htx))
2309 goto return_srv_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002310 }
2311
Christopher Faulete0768eb2018-10-03 16:38:02 +02002312 /* When TE: chunked is used, we need to get there again to parse
2313 * remaining chunks even if the server has closed, so we don't want to
Christopher Faulet9768c262018-10-22 09:34:31 +02002314 * set CF_DONTCLOSE. Similarly when there is a content-leng or if there
2315 * are filters registered on the stream, we don't want to forward a
2316 * close
Christopher Faulete0768eb2018-10-03 16:38:02 +02002317 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002318 if ((msg->flags & HTTP_MSGF_XFER_LEN) || HAS_RSP_DATA_FILTERS(s))
Christopher Faulete0768eb2018-10-03 16:38:02 +02002319 channel_dont_close(res);
2320
2321 /* We know that more data are expected, but we couldn't send more that
2322 * what we did. So we always set the CF_EXPECT_MORE flag so that the
2323 * system knows it must not set a PUSH on this first part. Interactive
2324 * modes are already handled by the stream sock layer. We must not do
2325 * this in content-length mode because it could present the MSG_MORE
2326 * flag with the last block of forwarded data, which would cause an
2327 * additional delay to be observed by the receiver.
2328 */
Christopher Faulet2151cdd2020-07-22 16:34:59 +02002329 if (HAS_RSP_DATA_FILTERS(s))
Christopher Faulete0768eb2018-10-03 16:38:02 +02002330 res->flags |= CF_EXPECT_MORE;
2331
2332 /* the stream handler will take care of timeouts and errors */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002333 DBG_TRACE_DEVEL("waiting for more data to forward",
2334 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002335 return 0;
2336
Christopher Faulet93e02d82019-03-08 14:18:50 +01002337 return_srv_abort:
2338 _HA_ATOMIC_ADD(&sess->fe->fe_counters.srv_aborts, 1);
2339 _HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01002340 if (sess->listener->counters)
2341 _HA_ATOMIC_ADD(&sess->listener->counters->srv_aborts, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002342 if (objt_server(s->target))
Christopher Fauletcff0f732019-12-16 16:13:44 +01002343 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.srv_aborts, 1);
Willy Tarreau826f3ab2021-02-10 12:07:15 +01002344 stream_inc_http_fail_ctr(s);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002345 if (!(s->flags & SF_ERR_MASK))
2346 s->flags |= SF_ERR_SRVCL;
2347 goto return_error;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002348
Christopher Faulet93e02d82019-03-08 14:18:50 +01002349 return_cli_abort:
2350 _HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
2351 _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01002352 if (sess->listener->counters)
2353 _HA_ATOMIC_ADD(&sess->listener->counters->cli_aborts, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002354 if (objt_server(s->target))
Christopher Fauletcff0f732019-12-16 16:13:44 +01002355 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.cli_aborts, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002356 if (!(s->flags & SF_ERR_MASK))
2357 s->flags |= SF_ERR_CLICL;
2358 goto return_error;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002359
Christopher Fauletb9a92f32019-09-09 10:15:21 +02002360 return_int_err:
Christopher Fauletcff0f732019-12-16 16:13:44 +01002361 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002362 _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01002363 if (sess->listener->counters)
2364 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002365 if (objt_server(s->target))
2366 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.internal_errors, 1);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02002367 if (!(s->flags & SF_ERR_MASK))
2368 s->flags |= SF_ERR_INTERNAL;
2369 goto return_error;
2370
Christopher Faulet93e02d82019-03-08 14:18:50 +01002371 return_bad_res:
2372 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
2373 if (objt_server(s->target)) {
Christopher Fauletcff0f732019-12-16 16:13:44 +01002374 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002375 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_RSP);
2376 }
Willy Tarreau826f3ab2021-02-10 12:07:15 +01002377 stream_inc_http_fail_ctr(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002378 if (!(s->flags & SF_ERR_MASK))
Christopher Faulet93e02d82019-03-08 14:18:50 +01002379 s->flags |= SF_ERR_SRVCL;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002380 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +02002381
Christopher Faulet93e02d82019-03-08 14:18:50 +01002382 return_error:
Christopher Faulete0768eb2018-10-03 16:38:02 +02002383 /* don't send any error message as we're in the body */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002384 http_reply_and_close(s, txn->status, NULL);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002385 res->analysers &= AN_RES_FLT_END;
2386 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 +02002387 if (!(s->flags & SF_FINST_MASK))
2388 s->flags |= SF_FINST_D;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002389 DBG_TRACE_DEVEL("leaving on error",
2390 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002391 return 0;
2392}
2393
Christopher Fauletf2824e62018-10-01 12:12:37 +02002394/* Perform an HTTP redirect based on the information in <rule>. The function
Christopher Faulet99daf282018-11-28 22:58:13 +01002395 * returns zero on success, or zero in case of a, irrecoverable error such
Christopher Fauletf2824e62018-10-01 12:12:37 +02002396 * as too large a request to build a valid response.
2397 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002398int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn)
Christopher Fauletf2824e62018-10-01 12:12:37 +02002399{
Christopher Faulet99daf282018-11-28 22:58:13 +01002400 struct channel *req = &s->req;
2401 struct channel *res = &s->res;
2402 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01002403 struct htx_sl *sl;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002404 struct buffer *chunk;
Christopher Faulet99daf282018-11-28 22:58:13 +01002405 struct ist status, reason, location;
2406 unsigned int flags;
Christopher Faulet08e66462019-05-23 16:44:59 +02002407 int close = 0; /* Try to keep the connection alive byt default */
Christopher Fauletf2824e62018-10-01 12:12:37 +02002408
2409 chunk = alloc_trash_chunk();
Christopher Fauletb8a53712019-12-16 11:29:38 +01002410 if (!chunk) {
2411 if (!(s->flags & SF_ERR_MASK))
2412 s->flags |= SF_ERR_RESOURCE;
Christopher Faulet99daf282018-11-28 22:58:13 +01002413 goto fail;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002414 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002415
Christopher Faulet99daf282018-11-28 22:58:13 +01002416 /*
2417 * Create the location
2418 */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002419 htx = htxbuf(&req->buf);
Christopher Fauletf2824e62018-10-01 12:12:37 +02002420 switch(rule->type) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002421 case REDIRECT_TYPE_SCHEME: {
2422 struct http_hdr_ctx ctx;
2423 struct ist path, host;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002424
Christopher Faulet99daf282018-11-28 22:58:13 +01002425 host = ist("");
2426 ctx.blk = NULL;
2427 if (http_find_header(htx, ist("Host"), &ctx, 0))
2428 host = ctx.value;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002429
Christopher Faulet297fbb42019-05-13 14:41:27 +02002430 sl = http_get_stline(htx);
Christopher Faulet99daf282018-11-28 22:58:13 +01002431 path = http_get_path(htx_sl_req_uri(sl));
2432 /* build message using path */
Tim Duesterhused526372020-03-05 17:56:33 +01002433 if (isttest(path)) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002434 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2435 int qs = 0;
2436 while (qs < path.len) {
2437 if (*(path.ptr + qs) == '?') {
2438 path.len = qs;
2439 break;
2440 }
2441 qs++;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002442 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002443 }
2444 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002445 else
2446 path = ist("/");
Christopher Fauletf2824e62018-10-01 12:12:37 +02002447
Christopher Faulet99daf282018-11-28 22:58:13 +01002448 if (rule->rdr_str) { /* this is an old "redirect" rule */
2449 /* add scheme */
2450 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2451 goto fail;
2452 }
2453 else {
2454 /* add scheme with executing log format */
2455 chunk->data += build_logline(s, chunk->area + chunk->data,
2456 chunk->size - chunk->data,
2457 &rule->rdr_fmt);
2458 }
2459 /* add "://" + host + path */
2460 if (!chunk_memcat(chunk, "://", 3) ||
2461 !chunk_memcat(chunk, host.ptr, host.len) ||
2462 !chunk_memcat(chunk, path.ptr, path.len))
2463 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002464
Christopher Faulet99daf282018-11-28 22:58:13 +01002465 /* append a slash at the end of the location if needed and missing */
2466 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
2467 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
2468 if (chunk->data + 1 >= chunk->size)
2469 goto fail;
2470 chunk->area[chunk->data++] = '/';
2471 }
2472 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002473 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002474
Christopher Faulet99daf282018-11-28 22:58:13 +01002475 case REDIRECT_TYPE_PREFIX: {
2476 struct ist path;
2477
Christopher Faulet297fbb42019-05-13 14:41:27 +02002478 sl = http_get_stline(htx);
Christopher Faulet99daf282018-11-28 22:58:13 +01002479 path = http_get_path(htx_sl_req_uri(sl));
2480 /* build message using path */
Tim Duesterhused526372020-03-05 17:56:33 +01002481 if (isttest(path)) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002482 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2483 int qs = 0;
2484 while (qs < path.len) {
2485 if (*(path.ptr + qs) == '?') {
2486 path.len = qs;
2487 break;
2488 }
2489 qs++;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002490 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002491 }
2492 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002493 else
2494 path = ist("/");
Christopher Fauletf2824e62018-10-01 12:12:37 +02002495
Christopher Faulet99daf282018-11-28 22:58:13 +01002496 if (rule->rdr_str) { /* this is an old "redirect" rule */
2497 /* add prefix. Note that if prefix == "/", we don't want to
2498 * add anything, otherwise it makes it hard for the user to
2499 * configure a self-redirection.
2500 */
2501 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
2502 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2503 goto fail;
2504 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002505 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002506 else {
2507 /* add prefix with executing log format */
2508 chunk->data += build_logline(s, chunk->area + chunk->data,
2509 chunk->size - chunk->data,
2510 &rule->rdr_fmt);
2511 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002512
Christopher Faulet99daf282018-11-28 22:58:13 +01002513 /* add path */
2514 if (!chunk_memcat(chunk, path.ptr, path.len))
2515 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002516
Christopher Faulet99daf282018-11-28 22:58:13 +01002517 /* append a slash at the end of the location if needed and missing */
2518 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
2519 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
2520 if (chunk->data + 1 >= chunk->size)
2521 goto fail;
2522 chunk->area[chunk->data++] = '/';
2523 }
2524 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002525 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002526 case REDIRECT_TYPE_LOCATION:
2527 default:
2528 if (rule->rdr_str) { /* this is an old "redirect" rule */
2529 /* add location */
2530 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2531 goto fail;
2532 }
2533 else {
2534 /* add location with executing log format */
2535 chunk->data += build_logline(s, chunk->area + chunk->data,
2536 chunk->size - chunk->data,
2537 &rule->rdr_fmt);
2538 }
2539 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002540 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002541 location = ist2(chunk->area, chunk->data);
2542
2543 /*
2544 * Create the 30x response
2545 */
2546 switch (rule->code) {
2547 case 308:
2548 status = ist("308");
2549 reason = ist("Permanent Redirect");
2550 break;
2551 case 307:
2552 status = ist("307");
2553 reason = ist("Temporary Redirect");
2554 break;
2555 case 303:
2556 status = ist("303");
2557 reason = ist("See Other");
2558 break;
2559 case 301:
2560 status = ist("301");
2561 reason = ist("Moved Permanently");
2562 break;
2563 case 302:
2564 default:
2565 status = ist("302");
2566 reason = ist("Found");
2567 break;
2568 }
2569
Christopher Faulet08e66462019-05-23 16:44:59 +02002570 if (!(txn->req.flags & HTTP_MSGF_BODYLESS) && txn->req.msg_state != HTTP_MSG_DONE)
2571 close = 1;
2572
Christopher Faulet99daf282018-11-28 22:58:13 +01002573 htx = htx_from_buf(&res->buf);
Kevin Zhu96b36392020-01-07 09:42:55 +01002574 /* Trim any possible response */
2575 channel_htx_truncate(&s->res, htx);
Christopher Faulet99daf282018-11-28 22:58:13 +01002576 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
2577 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), status, reason);
2578 if (!sl)
2579 goto fail;
2580 sl->info.res.status = rule->code;
2581 s->txn->status = rule->code;
2582
Christopher Faulet08e66462019-05-23 16:44:59 +02002583 if (close && !htx_add_header(htx, ist("Connection"), ist("close")))
2584 goto fail;
2585
2586 if (!htx_add_header(htx, ist("Content-length"), ist("0")) ||
Christopher Faulet99daf282018-11-28 22:58:13 +01002587 !htx_add_header(htx, ist("Location"), location))
2588 goto fail;
2589
2590 if (rule->code == 302 || rule->code == 303 || rule->code == 307) {
2591 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")))
2592 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002593 }
2594
2595 if (rule->cookie_len) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002596 if (!htx_add_header(htx, ist("Set-Cookie"), ist2(rule->cookie_str, rule->cookie_len)))
2597 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002598 }
2599
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002600 if (!htx_add_endof(htx, HTX_BLK_EOH))
Christopher Faulet99daf282018-11-28 22:58:13 +01002601 goto fail;
2602
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002603 htx->flags |= HTX_FL_EOM;
Kevin Zhu96b36392020-01-07 09:42:55 +01002604 htx_to_buf(htx, &res->buf);
Christopher Fauleta72a7e42020-01-28 09:28:11 +01002605 if (!http_forward_proxy_resp(s, 1))
2606 goto fail;
Christopher Faulet99daf282018-11-28 22:58:13 +01002607
Christopher Faulet60b33a52020-01-28 09:18:10 +01002608 if (rule->flags & REDIRECT_FLAG_FROM_REQ) {
2609 /* let's log the request time */
2610 s->logs.tv_request = now;
2611 req->analysers &= AN_REQ_FLT_END;
Christopher Faulet99daf282018-11-28 22:58:13 +01002612
Christopher Faulet60b33a52020-01-28 09:18:10 +01002613 if (s->sess->fe == s->be) /* report it if the request was intercepted by the frontend */
2614 _HA_ATOMIC_ADD(&s->sess->fe->fe_counters.intercepted_req, 1);
2615 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002616
2617 if (!(s->flags & SF_ERR_MASK))
2618 s->flags |= SF_ERR_LOCAL;
2619 if (!(s->flags & SF_FINST_MASK))
Christopher Faulet60b33a52020-01-28 09:18:10 +01002620 s->flags |= ((rule->flags & REDIRECT_FLAG_FROM_REQ) ? SF_FINST_R : SF_FINST_H);
Christopher Fauletf2824e62018-10-01 12:12:37 +02002621
Christopher Faulet99daf282018-11-28 22:58:13 +01002622 free_trash_chunk(chunk);
2623 return 1;
2624
2625 fail:
2626 /* If an error occurred, remove the incomplete HTTP response from the
2627 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01002628 channel_htx_truncate(res, htxbuf(&res->buf));
Christopher Fauletf2824e62018-10-01 12:12:37 +02002629 free_trash_chunk(chunk);
Christopher Faulet99daf282018-11-28 22:58:13 +01002630 return 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002631}
2632
Christopher Faulet92d34fe2019-12-17 09:20:34 +01002633/* Replace all headers matching the name <name>. The header value is replaced if
2634 * it matches the regex <re>. <str> is used for the replacement. If <full> is
2635 * set to 1, the full-line is matched and replaced. Otherwise, comma-separated
2636 * values are evaluated one by one. It returns 0 on success and -1 on error.
2637 */
2638int http_replace_hdrs(struct stream* s, struct htx *htx, struct ist name,
2639 const char *str, struct my_regex *re, int full)
Christopher Faulet72333522018-10-24 11:25:02 +02002640{
2641 struct http_hdr_ctx ctx;
2642 struct buffer *output = get_trash_chunk();
2643
Christopher Faulet72333522018-10-24 11:25:02 +02002644 ctx.blk = NULL;
Christopher Faulet92d34fe2019-12-17 09:20:34 +01002645 while (http_find_header(htx, name, &ctx, full)) {
Christopher Faulet72333522018-10-24 11:25:02 +02002646 if (!regex_exec_match2(re, ctx.value.ptr, ctx.value.len, MAX_MATCH, pmatch, 0))
2647 continue;
2648
2649 output->data = exp_replace(output->area, output->size, ctx.value.ptr, str, pmatch);
2650 if (output->data == -1)
2651 return -1;
2652 if (!http_replace_header_value(htx, &ctx, ist2(output->area, output->data)))
2653 return -1;
2654 }
2655 return 0;
2656}
2657
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002658/* This function executes one of the set-{method,path,query,uri} actions. It
2659 * takes the string from the variable 'replace' with length 'len', then modifies
2660 * the relevant part of the request line accordingly. Then it updates various
2661 * pointers to the next elements which were moved, and the total buffer length.
2662 * It finds the action to be performed in p[2], previously filled by function
2663 * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal
2664 * error, though this can be revisited when this code is finally exploited.
2665 *
2666 * 'action' can be '0' to replace method, '1' to replace path, '2' to replace
Christopher Faulet312294f2020-09-02 17:17:44 +02002667 * query string, 3 to replace uri or 4 to replace the path+query.
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002668 *
2669 * In query string case, the mark question '?' must be set at the start of the
2670 * string by the caller, event if the replacement query string is empty.
2671 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002672int http_req_replace_stline(int action, const char *replace, int len,
2673 struct proxy *px, struct stream *s)
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002674{
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002675 struct htx *htx = htxbuf(&s->req.buf);
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002676
2677 switch (action) {
2678 case 0: // method
2679 if (!http_replace_req_meth(htx, ist2(replace, len)))
2680 return -1;
2681 break;
2682
2683 case 1: // path
Christopher Fauletb8ce5052020-08-31 16:11:57 +02002684 if (!http_replace_req_path(htx, ist2(replace, len), 0))
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002685 return -1;
2686 break;
2687
2688 case 2: // query
2689 if (!http_replace_req_query(htx, ist2(replace, len)))
2690 return -1;
2691 break;
2692
2693 case 3: // uri
2694 if (!http_replace_req_uri(htx, ist2(replace, len)))
2695 return -1;
2696 break;
2697
Christopher Faulet312294f2020-09-02 17:17:44 +02002698 case 4: // path + query
2699 if (!http_replace_req_path(htx, ist2(replace, len), 1))
2700 return -1;
2701 break;
2702
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002703 default:
2704 return -1;
2705 }
2706 return 0;
2707}
2708
2709/* This function replace the HTTP status code and the associated message. The
Christopher Faulete00d06c2019-12-16 17:18:42 +01002710 * variable <status> contains the new status code. This function never fails. It
2711 * returns 0 in case of success, -1 in case of internal error.
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002712 */
Christopher Faulet96bff762019-12-17 13:46:18 +01002713int http_res_set_status(unsigned int status, struct ist reason, struct stream *s)
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002714{
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002715 struct htx *htx = htxbuf(&s->res.buf);
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002716 char *res;
2717
2718 chunk_reset(&trash);
2719 res = ultoa_o(status, trash.area, trash.size);
2720 trash.data = res - trash.area;
2721
2722 /* Do we have a custom reason format string? */
Tim Duesterhuse296d3e2020-03-05 17:56:31 +01002723 if (!isttest(reason)) {
Christopher Faulet96bff762019-12-17 13:46:18 +01002724 const char *str = http_get_reason(status);
2725 reason = ist2(str, strlen(str));
2726 }
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002727
Christopher Fauletbde2c4c2020-08-31 16:43:34 +02002728 if (!http_replace_res_status(htx, ist2(trash.area, trash.data), reason))
Christopher Faulete00d06c2019-12-16 17:18:42 +01002729 return -1;
2730 return 0;
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002731}
2732
Christopher Faulet3e964192018-10-24 11:39:23 +02002733/* Executes the http-request rules <rules> for stream <s>, proxy <px> and
2734 * transaction <txn>. Returns the verdict of the first rule that prevents
2735 * further processing of the request (auth, deny, ...), and defaults to
2736 * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or
2737 * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT
2738 * on txn->flags if it encounters a tarpit rule. If <deny_status> is not NULL
2739 * and a deny/tarpit rule is matched, it will be filled with this rule's deny
2740 * status.
2741 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002742static enum rule_result http_req_get_intercept_rule(struct proxy *px, struct list *rules,
Christopher Fauletb58f62b2020-01-13 16:40:13 +01002743 struct stream *s)
Christopher Faulet3e964192018-10-24 11:39:23 +02002744{
2745 struct session *sess = strm_sess(s);
2746 struct http_txn *txn = s->txn;
Christopher Faulet3e964192018-10-24 11:39:23 +02002747 struct act_rule *rule;
Christopher Faulet3e964192018-10-24 11:39:23 +02002748 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002749 int act_opts = 0;
Christopher Faulet3e964192018-10-24 11:39:23 +02002750
Christopher Faulet3e964192018-10-24 11:39:23 +02002751 /* If "the current_rule_list" match the executed rule list, we are in
2752 * resume condition. If a resume is needed it is always in the action
2753 * and never in the ACL or converters. In this case, we initialise the
2754 * current rule, and go to the action execution point.
2755 */
2756 if (s->current_rule) {
2757 rule = s->current_rule;
2758 s->current_rule = NULL;
2759 if (s->current_rule_list == rules)
2760 goto resume_execution;
2761 }
2762 s->current_rule_list = rules;
2763
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002764 /* start the ruleset evaluation in strict mode */
2765 txn->req.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01002766
Christopher Faulet3e964192018-10-24 11:39:23 +02002767 list_for_each_entry(rule, rules, list) {
2768 /* check optional condition */
2769 if (rule->cond) {
2770 int ret;
2771
2772 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
2773 ret = acl_pass(ret);
2774
2775 if (rule->cond->pol == ACL_COND_UNLESS)
2776 ret = !ret;
2777
2778 if (!ret) /* condition not matched */
2779 continue;
2780 }
2781
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002782 act_opts |= ACT_OPT_FIRST;
Christopher Faulet3e964192018-10-24 11:39:23 +02002783 resume_execution:
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002784 /* Always call the action function if defined */
2785 if (rule->action_ptr) {
2786 if ((s->req.flags & CF_READ_ERROR) ||
2787 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
2788 (px->options & PR_O_ABRT_CLOSE)))
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002789 act_opts |= ACT_OPT_FINAL;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002790
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002791 switch (rule->action_ptr(rule, px, sess, s, act_opts)) {
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002792 case ACT_RET_CONT:
2793 break;
2794 case ACT_RET_STOP:
2795 rule_ret = HTTP_RULE_RES_STOP;
2796 goto end;
2797 case ACT_RET_YIELD:
2798 s->current_rule = rule;
2799 rule_ret = HTTP_RULE_RES_YIELD;
2800 goto end;
2801 case ACT_RET_ERR:
2802 rule_ret = HTTP_RULE_RES_ERROR;
2803 goto end;
2804 case ACT_RET_DONE:
2805 rule_ret = HTTP_RULE_RES_DONE;
2806 goto end;
2807 case ACT_RET_DENY:
Christopher Fauletb58f62b2020-01-13 16:40:13 +01002808 if (txn->status == -1)
2809 txn->status = 403;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002810 rule_ret = HTTP_RULE_RES_DENY;
2811 goto end;
2812 case ACT_RET_ABRT:
2813 rule_ret = HTTP_RULE_RES_ABRT;
2814 goto end;
2815 case ACT_RET_INV:
2816 rule_ret = HTTP_RULE_RES_BADREQ;
2817 goto end;
2818 }
2819 continue; /* eval the next rule */
2820 }
2821
2822 /* If not action function defined, check for known actions */
Christopher Faulet3e964192018-10-24 11:39:23 +02002823 switch (rule->action) {
2824 case ACT_ACTION_ALLOW:
2825 rule_ret = HTTP_RULE_RES_STOP;
2826 goto end;
2827
2828 case ACT_ACTION_DENY:
Christopher Faulet5cb513a2020-05-13 17:56:56 +02002829 txn->status = rule->arg.http_reply->status;
2830 txn->http_reply = rule->arg.http_reply;
Christopher Faulet3e964192018-10-24 11:39:23 +02002831 rule_ret = HTTP_RULE_RES_DENY;
2832 goto end;
2833
2834 case ACT_HTTP_REQ_TARPIT:
2835 txn->flags |= TX_CLTARPIT;
Christopher Faulet5cb513a2020-05-13 17:56:56 +02002836 txn->status = rule->arg.http_reply->status;
2837 txn->http_reply = rule->arg.http_reply;
Christopher Faulet3e964192018-10-24 11:39:23 +02002838 rule_ret = HTTP_RULE_RES_DENY;
2839 goto end;
2840
Christopher Faulet3e964192018-10-24 11:39:23 +02002841 case ACT_HTTP_REDIR:
Christopher Faulet90d22a82020-03-06 11:18:39 +01002842 rule_ret = HTTP_RULE_RES_ABRT;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002843 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
Christopher Faulet3a26bee2019-12-16 12:47:40 +01002844 rule_ret = HTTP_RULE_RES_ERROR;
Christopher Faulet3e964192018-10-24 11:39:23 +02002845 goto end;
2846
2847 case ACT_HTTP_SET_NICE:
Christopher Faulet96bff762019-12-17 13:46:18 +01002848 s->task->nice = rule->arg.http.i;
Christopher Faulet3e964192018-10-24 11:39:23 +02002849 break;
2850
2851 case ACT_HTTP_SET_TOS:
Christopher Faulet96bff762019-12-17 13:46:18 +01002852 conn_set_tos(objt_conn(sess->origin), rule->arg.http.i);
Christopher Faulet3e964192018-10-24 11:39:23 +02002853 break;
2854
2855 case ACT_HTTP_SET_MARK:
Christopher Faulet96bff762019-12-17 13:46:18 +01002856 conn_set_mark(objt_conn(sess->origin), rule->arg.http.i);
Christopher Faulet3e964192018-10-24 11:39:23 +02002857 break;
2858
2859 case ACT_HTTP_SET_LOGL:
Christopher Faulet96bff762019-12-17 13:46:18 +01002860 s->logs.level = rule->arg.http.i;
Christopher Faulet3e964192018-10-24 11:39:23 +02002861 break;
2862
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002863 /* other flags exists, but normally, they never be matched. */
Christopher Faulet3e964192018-10-24 11:39:23 +02002864 default:
2865 break;
2866 }
2867 }
2868
2869 end:
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002870 /* if the ruleset evaluation is finished reset the strict mode */
Christopher Faulet46f95542019-12-20 10:07:22 +01002871 if (rule_ret != HTTP_RULE_RES_YIELD)
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002872 txn->req.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01002873
Christopher Faulet3e964192018-10-24 11:39:23 +02002874 /* we reached the end of the rules, nothing to report */
2875 return rule_ret;
2876}
2877
2878/* Executes the http-response rules <rules> for stream <s> and proxy <px>. It
2879 * returns one of 5 possible statuses: HTTP_RULE_RES_CONT, HTTP_RULE_RES_STOP,
2880 * HTTP_RULE_RES_DONE, HTTP_RULE_RES_YIELD, or HTTP_RULE_RES_BADREQ. If *CONT
2881 * is returned, the process can continue the evaluation of next rule list. If
2882 * *STOP or *DONE is returned, the process must stop the evaluation. If *BADREQ
2883 * is returned, it means the operation could not be processed and a server error
Christopher Fauleta53abad2020-05-13 08:12:22 +02002884 * must be returned. If *YIELD is returned, the caller must call again the
2885 * function with the same context.
Christopher Faulet3e964192018-10-24 11:39:23 +02002886 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002887static enum rule_result http_res_get_intercept_rule(struct proxy *px, struct list *rules,
2888 struct stream *s)
Christopher Faulet3e964192018-10-24 11:39:23 +02002889{
2890 struct session *sess = strm_sess(s);
2891 struct http_txn *txn = s->txn;
Christopher Faulet3e964192018-10-24 11:39:23 +02002892 struct act_rule *rule;
Christopher Faulet3e964192018-10-24 11:39:23 +02002893 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002894 int act_opts = 0;
Christopher Faulet3e964192018-10-24 11:39:23 +02002895
Christopher Faulet3e964192018-10-24 11:39:23 +02002896 /* If "the current_rule_list" match the executed rule list, we are in
2897 * resume condition. If a resume is needed it is always in the action
2898 * and never in the ACL or converters. In this case, we initialise the
2899 * current rule, and go to the action execution point.
2900 */
2901 if (s->current_rule) {
2902 rule = s->current_rule;
2903 s->current_rule = NULL;
2904 if (s->current_rule_list == rules)
2905 goto resume_execution;
2906 }
2907 s->current_rule_list = rules;
2908
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002909 /* start the ruleset evaluation in strict mode */
2910 txn->rsp.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01002911
Christopher Faulet3e964192018-10-24 11:39:23 +02002912 list_for_each_entry(rule, rules, list) {
2913 /* check optional condition */
2914 if (rule->cond) {
2915 int ret;
2916
2917 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
2918 ret = acl_pass(ret);
2919
2920 if (rule->cond->pol == ACL_COND_UNLESS)
2921 ret = !ret;
2922
2923 if (!ret) /* condition not matched */
2924 continue;
2925 }
2926
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002927 act_opts |= ACT_OPT_FIRST;
Christopher Faulet3e964192018-10-24 11:39:23 +02002928resume_execution:
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002929
2930 /* Always call the action function if defined */
2931 if (rule->action_ptr) {
2932 if ((s->req.flags & CF_READ_ERROR) ||
2933 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
2934 (px->options & PR_O_ABRT_CLOSE)))
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002935 act_opts |= ACT_OPT_FINAL;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002936
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002937 switch (rule->action_ptr(rule, px, sess, s, act_opts)) {
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002938 case ACT_RET_CONT:
2939 break;
2940 case ACT_RET_STOP:
2941 rule_ret = HTTP_RULE_RES_STOP;
2942 goto end;
2943 case ACT_RET_YIELD:
2944 s->current_rule = rule;
2945 rule_ret = HTTP_RULE_RES_YIELD;
2946 goto end;
2947 case ACT_RET_ERR:
2948 rule_ret = HTTP_RULE_RES_ERROR;
2949 goto end;
2950 case ACT_RET_DONE:
2951 rule_ret = HTTP_RULE_RES_DONE;
2952 goto end;
2953 case ACT_RET_DENY:
Christopher Fauletb58f62b2020-01-13 16:40:13 +01002954 if (txn->status == -1)
2955 txn->status = 502;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002956 rule_ret = HTTP_RULE_RES_DENY;
2957 goto end;
2958 case ACT_RET_ABRT:
2959 rule_ret = HTTP_RULE_RES_ABRT;
2960 goto end;
2961 case ACT_RET_INV:
2962 rule_ret = HTTP_RULE_RES_BADREQ;
2963 goto end;
2964 }
2965 continue; /* eval the next rule */
2966 }
2967
2968 /* If not action function defined, check for known actions */
Christopher Faulet3e964192018-10-24 11:39:23 +02002969 switch (rule->action) {
2970 case ACT_ACTION_ALLOW:
2971 rule_ret = HTTP_RULE_RES_STOP; /* "allow" rules are OK */
2972 goto end;
2973
2974 case ACT_ACTION_DENY:
Christopher Faulet5cb513a2020-05-13 17:56:56 +02002975 txn->status = rule->arg.http_reply->status;
2976 txn->http_reply = rule->arg.http_reply;
Christopher Faulet3a26bee2019-12-16 12:47:40 +01002977 rule_ret = HTTP_RULE_RES_DENY;
Christopher Faulet3e964192018-10-24 11:39:23 +02002978 goto end;
2979
2980 case ACT_HTTP_SET_NICE:
Christopher Faulet96bff762019-12-17 13:46:18 +01002981 s->task->nice = rule->arg.http.i;
Christopher Faulet3e964192018-10-24 11:39:23 +02002982 break;
2983
2984 case ACT_HTTP_SET_TOS:
Christopher Faulet96bff762019-12-17 13:46:18 +01002985 conn_set_tos(objt_conn(sess->origin), rule->arg.http.i);
Christopher Faulet3e964192018-10-24 11:39:23 +02002986 break;
2987
2988 case ACT_HTTP_SET_MARK:
Christopher Faulet96bff762019-12-17 13:46:18 +01002989 conn_set_mark(objt_conn(sess->origin), rule->arg.http.i);
Christopher Faulet3e964192018-10-24 11:39:23 +02002990 break;
2991
2992 case ACT_HTTP_SET_LOGL:
Christopher Faulet96bff762019-12-17 13:46:18 +01002993 s->logs.level = rule->arg.http.i;
Christopher Faulet3e964192018-10-24 11:39:23 +02002994 break;
2995
Christopher Faulet3e964192018-10-24 11:39:23 +02002996 case ACT_HTTP_REDIR:
Christopher Faulet49c2a702020-03-06 15:44:37 +01002997 rule_ret = HTTP_RULE_RES_ABRT;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002998 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
Christopher Faulet3a26bee2019-12-16 12:47:40 +01002999 rule_ret = HTTP_RULE_RES_ERROR;
Christopher Faulet3e964192018-10-24 11:39:23 +02003000 goto end;
3001
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01003002 /* other flags exists, but normally, they never be matched. */
Christopher Faulet3e964192018-10-24 11:39:23 +02003003 default:
3004 break;
3005 }
3006 }
3007
3008 end:
Christopher Faulet1aea50e2020-01-17 16:03:53 +01003009 /* if the ruleset evaluation is finished reset the strict mode */
Christopher Faulet46f95542019-12-20 10:07:22 +01003010 if (rule_ret != HTTP_RULE_RES_YIELD)
Christopher Faulet1aea50e2020-01-17 16:03:53 +01003011 txn->rsp.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01003012
Christopher Faulet3e964192018-10-24 11:39:23 +02003013 /* we reached the end of the rules, nothing to report */
3014 return rule_ret;
3015}
3016
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01003017/* Executes backend and frontend http-after-response rules for the stream <s>,
3018 * in that order. it return 1 on success and 0 on error. It is the caller
3019 * responsibility to catch error or ignore it. If it catches it, this function
3020 * may be called a second time, for the internal error.
3021 */
3022int http_eval_after_res_rules(struct stream *s)
3023{
3024 struct session *sess = s->sess;
3025 enum rule_result ret = HTTP_RULE_RES_CONT;
3026
Christopher Faulet507479b2020-05-15 12:29:46 +02003027 /* Eval after-response ruleset only if the reply is not const */
3028 if (s->txn->flags & TX_CONST_REPLY)
3029 goto end;
3030
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01003031 /* prune the request variables if not already done and swap to the response variables. */
3032 if (s->vars_reqres.scope != SCOPE_RES) {
3033 if (!LIST_ISEMPTY(&s->vars_reqres.head))
3034 vars_prune(&s->vars_reqres, s->sess, s);
3035 vars_init(&s->vars_reqres, SCOPE_RES);
3036 }
3037
3038 ret = http_res_get_intercept_rule(s->be, &s->be->http_after_res_rules, s);
3039 if ((ret == HTTP_RULE_RES_CONT || ret == HTTP_RULE_RES_STOP) && sess->fe != s->be)
3040 ret = http_res_get_intercept_rule(sess->fe, &sess->fe->http_after_res_rules, s);
3041
Christopher Faulet507479b2020-05-15 12:29:46 +02003042 end:
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01003043 /* All other codes than CONTINUE, STOP or DONE are forbidden */
3044 return (ret == HTTP_RULE_RES_CONT || ret == HTTP_RULE_RES_STOP || ret == HTTP_RULE_RES_DONE);
3045}
3046
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003047/*
3048 * Manage client-side cookie. It can impact performance by about 2% so it is
3049 * desirable to call it only when needed. This code is quite complex because
3050 * of the multiple very crappy and ambiguous syntaxes we have to support. it
3051 * highly recommended not to touch this part without a good reason !
3052 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003053static void http_manage_client_side_cookies(struct stream *s, struct channel *req)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003054{
3055 struct session *sess = s->sess;
3056 struct http_txn *txn = s->txn;
3057 struct htx *htx;
3058 struct http_hdr_ctx ctx;
3059 char *hdr_beg, *hdr_end, *del_from;
3060 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
3061 int preserve_hdr;
3062
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003063 htx = htxbuf(&req->buf);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003064 ctx.blk = NULL;
3065 while (http_find_header(htx, ist("Cookie"), &ctx, 1)) {
Olivier Houchardf0f42382019-07-22 17:43:46 +02003066 int is_first = 1;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003067 del_from = NULL; /* nothing to be deleted */
3068 preserve_hdr = 0; /* assume we may kill the whole header */
3069
3070 /* Now look for cookies. Conforming to RFC2109, we have to support
3071 * attributes whose name begin with a '$', and associate them with
3072 * the right cookie, if we want to delete this cookie.
3073 * So there are 3 cases for each cookie read :
3074 * 1) it's a special attribute, beginning with a '$' : ignore it.
3075 * 2) it's a server id cookie that we *MAY* want to delete : save
3076 * some pointers on it (last semi-colon, beginning of cookie...)
3077 * 3) it's an application cookie : we *MAY* have to delete a previous
3078 * "special" cookie.
3079 * At the end of loop, if a "special" cookie remains, we may have to
3080 * remove it. If no application cookie persists in the header, we
3081 * *MUST* delete it.
3082 *
3083 * Note: RFC2965 is unclear about the processing of spaces around
3084 * the equal sign in the ATTR=VALUE form. A careful inspection of
3085 * the RFC explicitly allows spaces before it, and not within the
3086 * tokens (attrs or values). An inspection of RFC2109 allows that
3087 * too but section 10.1.3 lets one think that spaces may be allowed
3088 * after the equal sign too, resulting in some (rare) buggy
3089 * implementations trying to do that. So let's do what servers do.
3090 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
3091 * allowed quoted strings in values, with any possible character
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003092 * after a backslash, including control chars and delimiters, which
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003093 * causes parsing to become ambiguous. Browsers also allow spaces
3094 * within values even without quotes.
3095 *
3096 * We have to keep multiple pointers in order to support cookie
3097 * removal at the beginning, middle or end of header without
3098 * corrupting the header. All of these headers are valid :
3099 *
3100 * hdr_beg hdr_end
3101 * | |
3102 * v |
3103 * NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3 |
3104 * NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3 v
3105 * NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3
3106 * | | | | | | |
3107 * | | | | | | |
3108 * | | | | | | +--> next
3109 * | | | | | +----> val_end
3110 * | | | | +-----------> val_beg
3111 * | | | +--------------> equal
3112 * | | +----------------> att_end
3113 * | +---------------------> att_beg
3114 * +--------------------------> prev
3115 *
3116 */
3117 hdr_beg = ctx.value.ptr;
3118 hdr_end = hdr_beg + ctx.value.len;
3119 for (prev = hdr_beg; prev < hdr_end; prev = next) {
3120 /* Iterate through all cookies on this line */
3121
3122 /* find att_beg */
3123 att_beg = prev;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003124 if (!is_first)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003125 att_beg++;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003126 is_first = 0;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003127
3128 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
3129 att_beg++;
3130
3131 /* find att_end : this is the first character after the last non
3132 * space before the equal. It may be equal to hdr_end.
3133 */
3134 equal = att_end = att_beg;
3135 while (equal < hdr_end) {
3136 if (*equal == '=' || *equal == ',' || *equal == ';')
3137 break;
3138 if (HTTP_IS_SPHT(*equal++))
3139 continue;
3140 att_end = equal;
3141 }
3142
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003143 /* here, <equal> points to '=', a delimiter or the end. <att_end>
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003144 * is between <att_beg> and <equal>, both may be identical.
3145 */
3146 /* look for end of cookie if there is an equal sign */
3147 if (equal < hdr_end && *equal == '=') {
3148 /* look for the beginning of the value */
3149 val_beg = equal + 1;
3150 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
3151 val_beg++;
3152
3153 /* find the end of the value, respecting quotes */
3154 next = http_find_cookie_value_end(val_beg, hdr_end);
3155
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003156 /* make val_end point to the first white space or delimiter after the value */
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003157 val_end = next;
3158 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
3159 val_end--;
3160 }
3161 else
3162 val_beg = val_end = next = equal;
3163
3164 /* We have nothing to do with attributes beginning with
3165 * '$'. However, they will automatically be removed if a
3166 * header before them is removed, since they're supposed
3167 * to be linked together.
3168 */
3169 if (*att_beg == '$')
3170 continue;
3171
3172 /* Ignore cookies with no equal sign */
3173 if (equal == next) {
3174 /* This is not our cookie, so we must preserve it. But if we already
3175 * scheduled another cookie for removal, we cannot remove the
3176 * complete header, but we can remove the previous block itself.
3177 */
3178 preserve_hdr = 1;
3179 if (del_from != NULL) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003180 int delta = http_del_hdr_value(hdr_beg, hdr_end, &del_from, prev);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003181 val_end += delta;
3182 next += delta;
3183 hdr_end += delta;
3184 prev = del_from;
3185 del_from = NULL;
3186 }
3187 continue;
3188 }
3189
3190 /* if there are spaces around the equal sign, we need to
3191 * strip them otherwise we'll get trouble for cookie captures,
3192 * or even for rewrites. Since this happens extremely rarely,
3193 * it does not hurt performance.
3194 */
3195 if (unlikely(att_end != equal || val_beg > equal + 1)) {
3196 int stripped_before = 0;
3197 int stripped_after = 0;
3198
3199 if (att_end != equal) {
3200 memmove(att_end, equal, hdr_end - equal);
3201 stripped_before = (att_end - equal);
3202 equal += stripped_before;
3203 val_beg += stripped_before;
3204 }
3205
3206 if (val_beg > equal + 1) {
3207 memmove(equal + 1, val_beg, hdr_end + stripped_before - val_beg);
3208 stripped_after = (equal + 1) - val_beg;
3209 val_beg += stripped_after;
3210 stripped_before += stripped_after;
3211 }
3212
3213 val_end += stripped_before;
3214 next += stripped_before;
3215 hdr_end += stripped_before;
3216 }
3217 /* now everything is as on the diagram above */
3218
3219 /* First, let's see if we want to capture this cookie. We check
3220 * that we don't already have a client side cookie, because we
3221 * can only capture one. Also as an optimisation, we ignore
3222 * cookies shorter than the declared name.
3223 */
3224 if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL &&
3225 (val_end - att_beg >= sess->fe->capture_namelen) &&
3226 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
3227 int log_len = val_end - att_beg;
3228
3229 if ((txn->cli_cookie = pool_alloc(pool_head_capture)) == NULL) {
3230 ha_alert("HTTP logging : out of memory.\n");
3231 } else {
3232 if (log_len > sess->fe->capture_len)
3233 log_len = sess->fe->capture_len;
3234 memcpy(txn->cli_cookie, att_beg, log_len);
3235 txn->cli_cookie[log_len] = 0;
3236 }
3237 }
3238
3239 /* Persistence cookies in passive, rewrite or insert mode have the
3240 * following form :
3241 *
3242 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
3243 *
3244 * For cookies in prefix mode, the form is :
3245 *
3246 * Cookie: NAME=SRV~VALUE
3247 */
3248 if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
3249 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
3250 struct server *srv = s->be->srv;
3251 char *delim;
3252
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003253 /* if we're in cookie prefix mode, we'll search the delimiter so that we
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003254 * have the server ID between val_beg and delim, and the original cookie between
3255 * delim+1 and val_end. Otherwise, delim==val_end :
3256 *
3257 * hdr_beg
3258 * |
3259 * v
3260 * NAME=SRV; # in all but prefix modes
3261 * NAME=SRV~OPAQUE ; # in prefix mode
3262 * || || | |+-> next
3263 * || || | +--> val_end
3264 * || || +---------> delim
3265 * || |+------------> val_beg
3266 * || +-------------> att_end = equal
3267 * |+-----------------> att_beg
3268 * +------------------> prev
3269 *
3270 */
3271 if (s->be->ck_opts & PR_CK_PFX) {
3272 for (delim = val_beg; delim < val_end; delim++)
3273 if (*delim == COOKIE_DELIM)
3274 break;
3275 }
3276 else {
3277 char *vbar1;
3278 delim = val_end;
3279 /* Now check if the cookie contains a date field, which would
3280 * appear after a vertical bar ('|') just after the server name
3281 * and before the delimiter.
3282 */
3283 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
3284 if (vbar1) {
3285 /* OK, so left of the bar is the server's cookie and
3286 * right is the last seen date. It is a base64 encoded
3287 * 30-bit value representing the UNIX date since the
3288 * epoch in 4-second quantities.
3289 */
3290 int val;
3291 delim = vbar1++;
3292 if (val_end - vbar1 >= 5) {
3293 val = b64tos30(vbar1);
3294 if (val > 0)
3295 txn->cookie_last_date = val << 2;
3296 }
3297 /* look for a second vertical bar */
3298 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
3299 if (vbar1 && (val_end - vbar1 > 5)) {
3300 val = b64tos30(vbar1 + 1);
3301 if (val > 0)
3302 txn->cookie_first_date = val << 2;
3303 }
3304 }
3305 }
3306
3307 /* if the cookie has an expiration date and the proxy wants to check
3308 * it, then we do that now. We first check if the cookie is too old,
3309 * then only if it has expired. We detect strict overflow because the
3310 * time resolution here is not great (4 seconds). Cookies with dates
3311 * in the future are ignored if their offset is beyond one day. This
3312 * allows an admin to fix timezone issues without expiring everyone
3313 * and at the same time avoids keeping unwanted side effects for too
3314 * long.
3315 */
3316 if (txn->cookie_first_date && s->be->cookie_maxlife &&
3317 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) ||
3318 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
3319 txn->flags &= ~TX_CK_MASK;
3320 txn->flags |= TX_CK_OLD;
3321 delim = val_beg; // let's pretend we have not found the cookie
3322 txn->cookie_first_date = 0;
3323 txn->cookie_last_date = 0;
3324 }
3325 else if (txn->cookie_last_date && s->be->cookie_maxidle &&
3326 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) ||
3327 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
3328 txn->flags &= ~TX_CK_MASK;
3329 txn->flags |= TX_CK_EXPIRED;
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
3335 /* Here, we'll look for the first running server which supports the cookie.
3336 * This allows to share a same cookie between several servers, for example
3337 * to dedicate backup servers to specific servers only.
3338 * However, to prevent clients from sticking to cookie-less backup server
3339 * when they have incidentely learned an empty cookie, we simply ignore
3340 * empty cookies and mark them as invalid.
3341 * The same behaviour is applied when persistence must be ignored.
3342 */
3343 if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
3344 srv = NULL;
3345
3346 while (srv) {
3347 if (srv->cookie && (srv->cklen == delim - val_beg) &&
3348 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
3349 if ((srv->cur_state != SRV_ST_STOPPED) ||
3350 (s->be->options & PR_O_PERSIST) ||
3351 (s->flags & SF_FORCE_PRST)) {
3352 /* we found the server and we can use it */
3353 txn->flags &= ~TX_CK_MASK;
3354 txn->flags |= (srv->cur_state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
3355 s->flags |= SF_DIRECT | SF_ASSIGNED;
3356 s->target = &srv->obj_type;
3357 break;
3358 } else {
3359 /* we found a server, but it's down,
3360 * mark it as such and go on in case
3361 * another one is available.
3362 */
3363 txn->flags &= ~TX_CK_MASK;
3364 txn->flags |= TX_CK_DOWN;
3365 }
3366 }
3367 srv = srv->next;
3368 }
3369
3370 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
3371 /* no server matched this cookie or we deliberately skipped it */
3372 txn->flags &= ~TX_CK_MASK;
3373 if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
3374 txn->flags |= TX_CK_UNUSED;
3375 else
3376 txn->flags |= TX_CK_INVALID;
3377 }
3378
3379 /* depending on the cookie mode, we may have to either :
3380 * - delete the complete cookie if we're in insert+indirect mode, so that
3381 * the server never sees it ;
3382 * - remove the server id from the cookie value, and tag the cookie as an
Joseph Herlante9d5c722018-11-25 11:00:25 -08003383 * application cookie so that it does not get accidentally removed later,
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003384 * if we're in cookie prefix mode
3385 */
3386 if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
3387 int delta; /* negative */
3388
3389 memmove(val_beg, delim + 1, hdr_end - (delim + 1));
3390 delta = val_beg - (delim + 1);
3391 val_end += delta;
3392 next += delta;
3393 hdr_end += delta;
3394 del_from = NULL;
3395 preserve_hdr = 1; /* we want to keep this cookie */
3396 }
3397 else if (del_from == NULL &&
3398 (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
3399 del_from = prev;
3400 }
3401 }
3402 else {
3403 /* This is not our cookie, so we must preserve it. But if we already
3404 * scheduled another cookie for removal, we cannot remove the
3405 * complete header, but we can remove the previous block itself.
3406 */
3407 preserve_hdr = 1;
3408
3409 if (del_from != NULL) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003410 int delta = http_del_hdr_value(hdr_beg, hdr_end, &del_from, prev);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003411 if (att_beg >= del_from)
3412 att_beg += delta;
3413 if (att_end >= del_from)
3414 att_end += delta;
3415 val_beg += delta;
3416 val_end += delta;
3417 next += delta;
3418 hdr_end += delta;
3419 prev = del_from;
3420 del_from = NULL;
3421 }
3422 }
3423
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003424 } /* for each cookie */
3425
3426
3427 /* There are no more cookies on this line.
3428 * We may still have one (or several) marked for deletion at the
3429 * end of the line. We must do this now in two ways :
3430 * - if some cookies must be preserved, we only delete from the
3431 * mark to the end of line ;
3432 * - if nothing needs to be preserved, simply delete the whole header
3433 */
3434 if (del_from) {
3435 hdr_end = (preserve_hdr ? del_from : hdr_beg);
3436 }
3437 if ((hdr_end - hdr_beg) != ctx.value.len) {
Christopher Faulet3e2638e2019-06-18 09:49:16 +02003438 if (hdr_beg != hdr_end)
3439 htx_change_blk_value_len(htx, ctx.blk, hdr_end - hdr_beg);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003440 else
3441 http_remove_header(htx, &ctx);
3442 }
3443 } /* for each "Cookie header */
3444}
3445
3446/*
3447 * Manage server-side cookies. It can impact performance by about 2% so it is
3448 * desirable to call it only when needed. This function is also used when we
3449 * just need to know if there is a cookie (eg: for check-cache).
3450 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003451static void http_manage_server_side_cookies(struct stream *s, struct channel *res)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003452{
3453 struct session *sess = s->sess;
3454 struct http_txn *txn = s->txn;
3455 struct htx *htx;
3456 struct http_hdr_ctx ctx;
3457 struct server *srv;
3458 char *hdr_beg, *hdr_end;
3459 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau6f7a02a2019-04-15 21:49:49 +02003460 int is_cookie2 = 0;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003461
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003462 htx = htxbuf(&res->buf);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003463
3464 ctx.blk = NULL;
3465 while (1) {
Olivier Houchardf0f42382019-07-22 17:43:46 +02003466 int is_first = 1;
3467
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003468 if (!http_find_header(htx, ist("Set-Cookie"), &ctx, 1)) {
3469 if (!http_find_header(htx, ist("Set-Cookie2"), &ctx, 1))
3470 break;
3471 is_cookie2 = 1;
3472 }
3473
3474 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
3475 * <prev> points to the colon.
3476 */
3477 txn->flags |= TX_SCK_PRESENT;
3478
3479 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
3480 * check-cache is enabled) and we are not interested in checking
3481 * them. Warning, the cookie capture is declared in the frontend.
3482 */
3483 if (s->be->cookie_name == NULL && sess->fe->capture_name == NULL)
3484 break;
3485
3486 /* OK so now we know we have to process this response cookie.
3487 * The format of the Set-Cookie header is slightly different
3488 * from the format of the Cookie header in that it does not
3489 * support the comma as a cookie delimiter (thus the header
3490 * cannot be folded) because the Expires attribute described in
3491 * the original Netscape's spec may contain an unquoted date
3492 * with a comma inside. We have to live with this because
3493 * many browsers don't support Max-Age and some browsers don't
3494 * support quoted strings. However the Set-Cookie2 header is
3495 * clean.
3496 *
3497 * We have to keep multiple pointers in order to support cookie
3498 * removal at the beginning, middle or end of header without
3499 * corrupting the header (in case of set-cookie2). A special
3500 * pointer, <scav> points to the beginning of the set-cookie-av
3501 * fields after the first semi-colon. The <next> pointer points
3502 * either to the end of line (set-cookie) or next unquoted comma
3503 * (set-cookie2). All of these headers are valid :
3504 *
3505 * hdr_beg hdr_end
3506 * | |
3507 * v |
3508 * NAME1 = VALUE 1 ; Secure; Path="/" |
3509 * NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT v
3510 * NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT
3511 * NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard
3512 * | | | | | | | |
3513 * | | | | | | | +-> next
3514 * | | | | | | +------------> scav
3515 * | | | | | +--------------> val_end
3516 * | | | | +--------------------> val_beg
3517 * | | | +----------------------> equal
3518 * | | +------------------------> att_end
3519 * | +----------------------------> att_beg
3520 * +------------------------------> prev
3521 * -------------------------------> hdr_beg
3522 */
3523 hdr_beg = ctx.value.ptr;
3524 hdr_end = hdr_beg + ctx.value.len;
3525 for (prev = hdr_beg; prev < hdr_end; prev = next) {
3526
3527 /* Iterate through all cookies on this line */
3528
3529 /* find att_beg */
3530 att_beg = prev;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003531 if (!is_first)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003532 att_beg++;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003533 is_first = 0;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003534
3535 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
3536 att_beg++;
3537
3538 /* find att_end : this is the first character after the last non
3539 * space before the equal. It may be equal to hdr_end.
3540 */
3541 equal = att_end = att_beg;
3542
3543 while (equal < hdr_end) {
3544 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
3545 break;
3546 if (HTTP_IS_SPHT(*equal++))
3547 continue;
3548 att_end = equal;
3549 }
3550
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003551 /* here, <equal> points to '=', a delimiter or the end. <att_end>
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003552 * is between <att_beg> and <equal>, both may be identical.
3553 */
3554
3555 /* look for end of cookie if there is an equal sign */
3556 if (equal < hdr_end && *equal == '=') {
3557 /* look for the beginning of the value */
3558 val_beg = equal + 1;
3559 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
3560 val_beg++;
3561
3562 /* find the end of the value, respecting quotes */
3563 next = http_find_cookie_value_end(val_beg, hdr_end);
3564
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003565 /* make val_end point to the first white space or delimiter after the value */
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003566 val_end = next;
3567 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
3568 val_end--;
3569 }
3570 else {
3571 /* <equal> points to next comma, semi-colon or EOL */
3572 val_beg = val_end = next = equal;
3573 }
3574
3575 if (next < hdr_end) {
3576 /* Set-Cookie2 supports multiple cookies, and <next> points to
3577 * a colon or semi-colon before the end. So skip all attr-value
3578 * pairs and look for the next comma. For Set-Cookie, since
3579 * commas are permitted in values, skip to the end.
3580 */
3581 if (is_cookie2)
3582 next = http_find_hdr_value_end(next, hdr_end);
3583 else
3584 next = hdr_end;
3585 }
3586
3587 /* Now everything is as on the diagram above */
3588
3589 /* Ignore cookies with no equal sign */
3590 if (equal == val_end)
3591 continue;
3592
3593 /* If there are spaces around the equal sign, we need to
3594 * strip them otherwise we'll get trouble for cookie captures,
3595 * or even for rewrites. Since this happens extremely rarely,
3596 * it does not hurt performance.
3597 */
3598 if (unlikely(att_end != equal || val_beg > equal + 1)) {
3599 int stripped_before = 0;
3600 int stripped_after = 0;
3601
3602 if (att_end != equal) {
3603 memmove(att_end, equal, hdr_end - equal);
3604 stripped_before = (att_end - equal);
3605 equal += stripped_before;
3606 val_beg += stripped_before;
3607 }
3608
3609 if (val_beg > equal + 1) {
3610 memmove(equal + 1, val_beg, hdr_end + stripped_before - val_beg);
3611 stripped_after = (equal + 1) - val_beg;
3612 val_beg += stripped_after;
3613 stripped_before += stripped_after;
3614 }
3615
3616 val_end += stripped_before;
3617 next += stripped_before;
3618 hdr_end += stripped_before;
3619
Christopher Faulet3e2638e2019-06-18 09:49:16 +02003620 htx_change_blk_value_len(htx, ctx.blk, hdr_end - hdr_beg);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003621 ctx.value.len = hdr_end - hdr_beg;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003622 }
3623
3624 /* First, let's see if we want to capture this cookie. We check
3625 * that we don't already have a server side cookie, because we
3626 * can only capture one. Also as an optimisation, we ignore
3627 * cookies shorter than the declared name.
3628 */
3629 if (sess->fe->capture_name != NULL &&
3630 txn->srv_cookie == NULL &&
3631 (val_end - att_beg >= sess->fe->capture_namelen) &&
3632 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
3633 int log_len = val_end - att_beg;
3634 if ((txn->srv_cookie = pool_alloc(pool_head_capture)) == NULL) {
3635 ha_alert("HTTP logging : out of memory.\n");
3636 }
3637 else {
3638 if (log_len > sess->fe->capture_len)
3639 log_len = sess->fe->capture_len;
3640 memcpy(txn->srv_cookie, att_beg, log_len);
3641 txn->srv_cookie[log_len] = 0;
3642 }
3643 }
3644
3645 srv = objt_server(s->target);
3646 /* now check if we need to process it for persistence */
3647 if (!(s->flags & SF_IGNORE_PRST) &&
3648 (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
3649 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
3650 /* assume passive cookie by default */
3651 txn->flags &= ~TX_SCK_MASK;
3652 txn->flags |= TX_SCK_FOUND;
3653
3654 /* If the cookie is in insert mode on a known server, we'll delete
3655 * this occurrence because we'll insert another one later.
3656 * We'll delete it too if the "indirect" option is set and we're in
3657 * a direct access.
3658 */
3659 if (s->be->ck_opts & PR_CK_PSV) {
3660 /* The "preserve" flag was set, we don't want to touch the
3661 * server's cookie.
3662 */
3663 }
3664 else if ((srv && (s->be->ck_opts & PR_CK_INS)) ||
3665 ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) {
3666 /* this cookie must be deleted */
3667 if (prev == hdr_beg && next == hdr_end) {
3668 /* whole header */
3669 http_remove_header(htx, &ctx);
3670 /* note: while both invalid now, <next> and <hdr_end>
3671 * are still equal, so the for() will stop as expected.
3672 */
3673 } else {
3674 /* just remove the value */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003675 int delta = http_del_hdr_value(hdr_beg, hdr_end, &prev, next);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003676 next = prev;
3677 hdr_end += delta;
3678 }
3679 txn->flags &= ~TX_SCK_MASK;
3680 txn->flags |= TX_SCK_DELETED;
3681 /* and go on with next cookie */
3682 }
3683 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) {
3684 /* replace bytes val_beg->val_end with the cookie name associated
3685 * with this server since we know it.
3686 */
3687 int sliding, delta;
3688
3689 ctx.value = ist2(val_beg, val_end - val_beg);
3690 ctx.lws_before = ctx.lws_after = 0;
3691 http_replace_header_value(htx, &ctx, ist2(srv->cookie, srv->cklen));
3692 delta = srv->cklen - (val_end - val_beg);
3693 sliding = (ctx.value.ptr - val_beg);
3694 hdr_beg += sliding;
3695 val_beg += sliding;
3696 next += sliding + delta;
3697 hdr_end += sliding + delta;
3698
3699 txn->flags &= ~TX_SCK_MASK;
3700 txn->flags |= TX_SCK_REPLACED;
3701 }
3702 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) {
3703 /* insert the cookie name associated with this server
3704 * before existing cookie, and insert a delimiter between them..
3705 */
3706 int sliding, delta;
3707 ctx.value = ist2(val_beg, 0);
3708 ctx.lws_before = ctx.lws_after = 0;
3709 http_replace_header_value(htx, &ctx, ist2(srv->cookie, srv->cklen + 1));
3710 delta = srv->cklen + 1;
3711 sliding = (ctx.value.ptr - val_beg);
3712 hdr_beg += sliding;
3713 val_beg += sliding;
3714 next += sliding + delta;
3715 hdr_end += sliding + delta;
3716
3717 val_beg[srv->cklen] = COOKIE_DELIM;
3718 txn->flags &= ~TX_SCK_MASK;
3719 txn->flags |= TX_SCK_REPLACED;
3720 }
3721 }
3722 /* that's done for this cookie, check the next one on the same
3723 * line when next != hdr_end (only if is_cookie2).
3724 */
3725 }
3726 }
3727}
3728
Christopher Faulet25a02f62018-10-24 12:00:25 +02003729/*
3730 * Parses the Cache-Control and Pragma request header fields to determine if
3731 * the request may be served from the cache and/or if it is cacheable. Updates
3732 * s->txn->flags.
3733 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003734void http_check_request_for_cacheability(struct stream *s, struct channel *req)
Christopher Faulet25a02f62018-10-24 12:00:25 +02003735{
3736 struct http_txn *txn = s->txn;
3737 struct htx *htx;
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003738 struct http_hdr_ctx ctx = { .blk = NULL };
3739 int pragma_found, cc_found;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003740
3741 if ((txn->flags & (TX_CACHEABLE|TX_CACHE_IGNORE)) == TX_CACHE_IGNORE)
3742 return; /* nothing more to do here */
3743
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003744 htx = htxbuf(&req->buf);
Christopher Faulet25a02f62018-10-24 12:00:25 +02003745 pragma_found = cc_found = 0;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003746
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003747 /* Check "pragma" header for HTTP/1.0 compatibility. */
3748 if (http_find_header(htx, ist("pragma"), &ctx, 1)) {
3749 if (isteqi(ctx.value, ist("no-cache"))) {
3750 pragma_found = 1;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003751 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003752 }
Christopher Faulet25a02f62018-10-24 12:00:25 +02003753
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003754 ctx.blk = NULL;
3755 /* Don't use the cache and don't try to store if we found the
3756 * Authorization header */
3757 if (http_find_header(htx, ist("authorization"), &ctx, 1)) {
3758 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3759 txn->flags |= TX_CACHE_IGNORE;
3760 }
Christopher Faulet25a02f62018-10-24 12:00:25 +02003761
Christopher Faulet25a02f62018-10-24 12:00:25 +02003762
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003763 /* Look for "cache-control" header and iterate over all the values
3764 * until we find one that specifies that caching is possible or not. */
3765 ctx.blk = NULL;
3766 while (http_find_header(htx, ist("cache-control"), &ctx, 0)) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003767 cc_found = 1;
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003768 /* We don't check the values after max-age, max-stale nor min-fresh,
3769 * we simply don't use the cache when they're specified. */
3770 if (istmatchi(ctx.value, ist("max-age")) ||
3771 istmatchi(ctx.value, ist("no-cache")) ||
3772 istmatchi(ctx.value, ist("max-stale")) ||
3773 istmatchi(ctx.value, ist("min-fresh"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003774 txn->flags |= TX_CACHE_IGNORE;
3775 continue;
3776 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003777 if (istmatchi(ctx.value, ist("no-store"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003778 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3779 continue;
3780 }
3781 }
3782
3783 /* RFC7234#5.4:
3784 * When the Cache-Control header field is also present and
3785 * understood in a request, Pragma is ignored.
3786 * When the Cache-Control header field is not present in a
3787 * request, caches MUST consider the no-cache request
3788 * pragma-directive as having the same effect as if
3789 * "Cache-Control: no-cache" were present.
3790 */
3791 if (!cc_found && pragma_found)
3792 txn->flags |= TX_CACHE_IGNORE;
3793}
3794
3795/*
3796 * Check if response is cacheable or not. Updates s->txn->flags.
3797 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003798void http_check_response_for_cacheability(struct stream *s, struct channel *res)
Christopher Faulet25a02f62018-10-24 12:00:25 +02003799{
3800 struct http_txn *txn = s->txn;
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003801 struct http_hdr_ctx ctx = { .blk = NULL };
Christopher Faulet25a02f62018-10-24 12:00:25 +02003802 struct htx *htx;
Remi Tricot-Le Bretoncc9bf2e2020-11-12 11:14:41 +01003803 int has_freshness_info = 0;
3804 int has_validator = 0;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003805
3806 if (txn->status < 200) {
3807 /* do not try to cache interim responses! */
3808 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3809 return;
3810 }
3811
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003812 htx = htxbuf(&res->buf);
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003813 /* Check "pragma" header for HTTP/1.0 compatibility. */
3814 if (http_find_header(htx, ist("pragma"), &ctx, 1)) {
3815 if (isteqi(ctx.value, ist("no-cache"))) {
3816 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3817 return;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003818 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003819 }
Christopher Faulet25a02f62018-10-24 12:00:25 +02003820
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003821 /* Look for "cache-control" header and iterate over all the values
3822 * until we find one that specifies that caching is possible or not. */
3823 ctx.blk = NULL;
3824 while (http_find_header(htx, ist("cache-control"), &ctx, 0)) {
3825 if (isteqi(ctx.value, ist("public"))) {
3826 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003827 continue;
3828 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003829 if (isteqi(ctx.value, ist("private")) ||
3830 isteqi(ctx.value, ist("no-cache")) ||
3831 isteqi(ctx.value, ist("no-store")) ||
3832 isteqi(ctx.value, ist("max-age=0")) ||
3833 isteqi(ctx.value, ist("s-maxage=0"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003834 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003835 continue;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003836 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003837 /* We might have a no-cache="set-cookie" form. */
3838 if (istmatchi(ctx.value, ist("no-cache=\"set-cookie"))) {
3839 txn->flags &= ~TX_CACHE_COOK;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003840 continue;
3841 }
Remi Tricot-Le Bretoncc9bf2e2020-11-12 11:14:41 +01003842
3843 if (istmatchi(ctx.value, ist("s-maxage")) ||
3844 istmatchi(ctx.value, ist("max-age"))) {
3845 has_freshness_info = 1;
3846 continue;
3847 }
3848 }
3849
3850 /* If no freshness information could be found in Cache-Control values,
3851 * look for an Expires header. */
3852 if (!has_freshness_info) {
3853 ctx.blk = NULL;
3854 has_freshness_info = http_find_header(htx, ist("expires"), &ctx, 0);
Christopher Faulet25a02f62018-10-24 12:00:25 +02003855 }
Remi Tricot-Le Bretoncc9bf2e2020-11-12 11:14:41 +01003856
3857 /* If no freshness information could be found in Cache-Control or Expires
3858 * values, look for an explicit validator. */
3859 if (!has_freshness_info) {
3860 ctx.blk = NULL;
3861 has_validator = 1;
3862 if (!http_find_header(htx, ist("etag"), &ctx, 0)) {
3863 ctx.blk = NULL;
3864 if (!http_find_header(htx, ist("last-modified"), &ctx, 0))
3865 has_validator = 0;
3866 }
3867 }
3868
3869 /* We won't store an entry that has neither a cache validator nor an
3870 * explicit expiration time, as suggested in RFC 7234#3. */
3871 if (!has_freshness_info && !has_validator)
3872 txn->flags |= TX_CACHE_IGNORE;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003873}
3874
Christopher Faulet377c5a52018-10-24 21:21:30 +02003875/*
3876 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
3877 * for the current backend.
3878 *
3879 * It is assumed that the request is either a HEAD, GET, or POST and that the
3880 * uri_auth field is valid.
3881 *
3882 * Returns 1 if stats should be provided, otherwise 0.
3883 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003884static int http_stats_check_uri(struct stream *s, struct http_txn *txn, struct proxy *backend)
Christopher Faulet377c5a52018-10-24 21:21:30 +02003885{
3886 struct uri_auth *uri_auth = backend->uri_auth;
3887 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003888 struct htx_sl *sl;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003889 struct ist uri;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003890
3891 if (!uri_auth)
3892 return 0;
3893
3894 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
3895 return 0;
3896
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003897 htx = htxbuf(&s->req.buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02003898 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003899 uri = htx_sl_req_uri(sl);
Willy Tarreau1eb3b482019-10-31 15:50:28 +01003900 if (*uri_auth->uri_prefix == '/')
3901 uri = http_get_path(uri);
Christopher Faulet377c5a52018-10-24 21:21:30 +02003902
3903 /* check URI size */
3904 if (uri_auth->uri_len > uri.len)
3905 return 0;
3906
3907 if (memcmp(uri.ptr, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
3908 return 0;
3909
3910 return 1;
3911}
3912
3913/* This function prepares an applet to handle the stats. It can deal with the
3914 * "100-continue" expectation, check that admin rules are met for POST requests,
3915 * and program a response message if something was unexpected. It cannot fail
3916 * and always relies on the stats applet to complete the job. It does not touch
3917 * analysers nor counters, which are left to the caller. It does not touch
3918 * s->target which is supposed to already point to the stats applet. The caller
3919 * is expected to have already assigned an appctx to the stream.
3920 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003921static int http_handle_stats(struct stream *s, struct channel *req)
Christopher Faulet377c5a52018-10-24 21:21:30 +02003922{
3923 struct stats_admin_rule *stats_admin_rule;
3924 struct stream_interface *si = &s->si[1];
3925 struct session *sess = s->sess;
3926 struct http_txn *txn = s->txn;
3927 struct http_msg *msg = &txn->req;
3928 struct uri_auth *uri_auth = s->be->uri_auth;
3929 const char *h, *lookup, *end;
3930 struct appctx *appctx;
3931 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003932 struct htx_sl *sl;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003933
3934 appctx = si_appctx(si);
3935 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
3936 appctx->st1 = appctx->st2 = 0;
3937 appctx->ctx.stats.st_code = STAT_STATUS_INIT;
Willy Tarreau676c29e2019-10-09 10:50:01 +02003938 appctx->ctx.stats.flags |= uri_auth->flags;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003939 appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */
3940 if ((msg->flags & HTTP_MSGF_VER_11) && (txn->meth != HTTP_METH_HEAD))
3941 appctx->ctx.stats.flags |= STAT_CHUNKED;
3942
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003943 htx = htxbuf(&req->buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02003944 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003945 lookup = HTX_SL_REQ_UPTR(sl) + uri_auth->uri_len;
3946 end = HTX_SL_REQ_UPTR(sl) + HTX_SL_REQ_ULEN(sl);
Christopher Faulet377c5a52018-10-24 21:21:30 +02003947
3948 for (h = lookup; h <= end - 3; h++) {
3949 if (memcmp(h, ";up", 3) == 0) {
3950 appctx->ctx.stats.flags |= STAT_HIDE_DOWN;
3951 break;
3952 }
Willy Tarreau3e320362020-10-23 17:28:57 +02003953 if (memcmp(h, ";no-maint", 3) == 0) {
3954 appctx->ctx.stats.flags |= STAT_HIDE_MAINT;
3955 break;
3956 }
Christopher Faulet377c5a52018-10-24 21:21:30 +02003957 }
3958
3959 if (uri_auth->refresh) {
3960 for (h = lookup; h <= end - 10; h++) {
3961 if (memcmp(h, ";norefresh", 10) == 0) {
3962 appctx->ctx.stats.flags |= STAT_NO_REFRESH;
3963 break;
3964 }
3965 }
3966 }
3967
3968 for (h = lookup; h <= end - 4; h++) {
3969 if (memcmp(h, ";csv", 4) == 0) {
Christopher Faulet6338a082019-09-09 15:50:54 +02003970 appctx->ctx.stats.flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
Christopher Faulet377c5a52018-10-24 21:21:30 +02003971 break;
3972 }
3973 }
3974
3975 for (h = lookup; h <= end - 6; h++) {
3976 if (memcmp(h, ";typed", 6) == 0) {
Christopher Faulet6338a082019-09-09 15:50:54 +02003977 appctx->ctx.stats.flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
Christopher Faulet377c5a52018-10-24 21:21:30 +02003978 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
3979 break;
3980 }
3981 }
3982
Christopher Faulet6338a082019-09-09 15:50:54 +02003983 for (h = lookup; h <= end - 5; h++) {
3984 if (memcmp(h, ";json", 5) == 0) {
3985 appctx->ctx.stats.flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
3986 appctx->ctx.stats.flags |= STAT_FMT_JSON;
3987 break;
3988 }
3989 }
3990
3991 for (h = lookup; h <= end - 12; h++) {
3992 if (memcmp(h, ";json-schema", 12) == 0) {
3993 appctx->ctx.stats.flags &= ~STAT_FMT_MASK;
3994 appctx->ctx.stats.flags |= STAT_JSON_SCHM;
3995 break;
3996 }
3997 }
3998
Christopher Faulet377c5a52018-10-24 21:21:30 +02003999 for (h = lookup; h <= end - 8; h++) {
4000 if (memcmp(h, ";st=", 4) == 0) {
4001 int i;
4002 h += 4;
4003 appctx->ctx.stats.st_code = STAT_STATUS_UNKN;
4004 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
4005 if (strncmp(stat_status_codes[i], h, 4) == 0) {
4006 appctx->ctx.stats.st_code = i;
4007 break;
4008 }
4009 }
4010 break;
4011 }
4012 }
4013
4014 appctx->ctx.stats.scope_str = 0;
4015 appctx->ctx.stats.scope_len = 0;
4016 for (h = lookup; h <= end - 8; h++) {
4017 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
4018 int itx = 0;
4019 const char *h2;
4020 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
4021 const char *err;
4022
4023 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
4024 h2 = h;
Christopher Fauleted7a0662019-01-14 11:07:34 +01004025 appctx->ctx.stats.scope_str = h2 - HTX_SL_REQ_UPTR(sl);
4026 while (h < end) {
Christopher Faulet377c5a52018-10-24 21:21:30 +02004027 if (*h == ';' || *h == '&' || *h == ' ')
4028 break;
4029 itx++;
4030 h++;
4031 }
4032
4033 if (itx > STAT_SCOPE_TXT_MAXLEN)
4034 itx = STAT_SCOPE_TXT_MAXLEN;
4035 appctx->ctx.stats.scope_len = itx;
4036
4037 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
4038 memcpy(scope_txt, h2, itx);
4039 scope_txt[itx] = '\0';
4040 err = invalid_char(scope_txt);
4041 if (err) {
4042 /* bad char in search text => clear scope */
4043 appctx->ctx.stats.scope_str = 0;
4044 appctx->ctx.stats.scope_len = 0;
4045 }
4046 break;
4047 }
4048 }
4049
4050 /* now check whether we have some admin rules for this request */
4051 list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) {
4052 int ret = 1;
4053
4054 if (stats_admin_rule->cond) {
4055 ret = acl_exec_cond(stats_admin_rule->cond, s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
4056 ret = acl_pass(ret);
4057 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
4058 ret = !ret;
4059 }
4060
4061 if (ret) {
4062 /* no rule, or the rule matches */
4063 appctx->ctx.stats.flags |= STAT_ADMIN;
4064 break;
4065 }
4066 }
4067
Christopher Faulet5d45e382019-02-27 15:15:23 +01004068 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
4069 appctx->st0 = STAT_HTTP_HEAD;
4070 else if (txn->meth == HTTP_METH_POST) {
Christopher Fauletbd9e8422019-08-15 22:26:48 +02004071 if (appctx->ctx.stats.flags & STAT_ADMIN) {
Christopher Faulet377c5a52018-10-24 21:21:30 +02004072 appctx->st0 = STAT_HTTP_POST;
Christopher Fauletbd9e8422019-08-15 22:26:48 +02004073 if (msg->msg_state < HTTP_MSG_DATA)
4074 req->analysers |= AN_REQ_HTTP_BODY;
4075 }
Christopher Faulet377c5a52018-10-24 21:21:30 +02004076 else {
Christopher Faulet5d45e382019-02-27 15:15:23 +01004077 /* POST without admin level */
Christopher Faulet377c5a52018-10-24 21:21:30 +02004078 appctx->ctx.stats.flags &= ~STAT_CHUNKED;
4079 appctx->ctx.stats.st_code = STAT_STATUS_DENY;
4080 appctx->st0 = STAT_HTTP_LAST;
4081 }
4082 }
4083 else {
Christopher Faulet5d45e382019-02-27 15:15:23 +01004084 /* Unsupported method */
4085 appctx->ctx.stats.flags &= ~STAT_CHUNKED;
4086 appctx->ctx.stats.st_code = STAT_STATUS_IVAL;
4087 appctx->st0 = STAT_HTTP_LAST;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004088 }
4089
4090 s->task->nice = -32; /* small boost for HTTP statistics */
4091 return 1;
4092}
4093
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004094void http_perform_server_redirect(struct stream *s, struct stream_interface *si)
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004095{
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004096 struct channel *req = &s->req;
4097 struct channel *res = &s->res;
4098 struct server *srv;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004099 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004100 struct htx_sl *sl;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004101 struct ist path, location;
4102 unsigned int flags;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004103
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004104 /*
4105 * Create the location
4106 */
4107 chunk_reset(&trash);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004108
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004109 /* 1: add the server's prefix */
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004110 /* special prefix "/" means don't change URL */
4111 srv = __objt_server(s->target);
4112 if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') {
4113 if (!chunk_memcat(&trash, srv->rdr_pfx, srv->rdr_len))
4114 return;
4115 }
4116
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004117 /* 2: add the request Path */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004118 htx = htxbuf(&req->buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02004119 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004120 path = http_get_path(htx_sl_req_uri(sl));
Tim Duesterhused526372020-03-05 17:56:33 +01004121 if (!isttest(path))
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004122 return;
4123
4124 if (!chunk_memcat(&trash, path.ptr, path.len))
4125 return;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004126 location = ist2(trash.area, trash.data);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004127
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004128 /*
4129 * Create the 302 respone
4130 */
4131 htx = htx_from_buf(&res->buf);
4132 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
4133 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
4134 ist("HTTP/1.1"), ist("302"), ist("Found"));
4135 if (!sl)
4136 goto fail;
4137 sl->info.res.status = 302;
4138 s->txn->status = 302;
4139
4140 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) ||
4141 !htx_add_header(htx, ist("Connection"), ist("close")) ||
4142 !htx_add_header(htx, ist("Content-length"), ist("0")) ||
4143 !htx_add_header(htx, ist("Location"), location))
4144 goto fail;
4145
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004146 if (!htx_add_endof(htx, HTX_BLK_EOH))
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004147 goto fail;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004148
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004149 htx->flags |= HTX_FL_EOM;
Christopher Fauletc20afb82020-01-24 19:16:26 +01004150 htx_to_buf(htx, &res->buf);
Christopher Fauleta72a7e42020-01-28 09:28:11 +01004151 if (!http_forward_proxy_resp(s, 1))
4152 goto fail;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004153
4154 /* return without error. */
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004155 si_shutr(si);
4156 si_shutw(si);
4157 si->err_type = SI_ET_NONE;
4158 si->state = SI_ST_CLO;
4159
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004160 if (!(s->flags & SF_ERR_MASK))
4161 s->flags |= SF_ERR_LOCAL;
4162 if (!(s->flags & SF_FINST_MASK))
4163 s->flags |= SF_FINST_C;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004164
4165 /* FIXME: we should increase a counter of redirects per server and per backend. */
4166 srv_inc_sess_ctr(srv);
4167 srv_set_sess_last(srv);
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004168 return;
4169
4170 fail:
4171 /* If an error occurred, remove the incomplete HTTP response from the
4172 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004173 channel_htx_truncate(res, htx);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004174}
4175
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05004176/* This function terminates the request because it was completely analyzed or
Christopher Fauletf2824e62018-10-01 12:12:37 +02004177 * because an error was triggered during the body forwarding.
4178 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004179static void http_end_request(struct stream *s)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004180{
4181 struct channel *chn = &s->req;
4182 struct http_txn *txn = s->txn;
4183
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004184 DBG_TRACE_ENTER(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004185
Christopher Fauletb42a8b62018-11-19 21:59:00 +01004186 if (unlikely(txn->req.msg_state == HTTP_MSG_ERROR ||
4187 txn->rsp.msg_state == HTTP_MSG_ERROR)) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004188 channel_abort(chn);
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004189 channel_htx_truncate(chn, htxbuf(&chn->buf));
Christopher Fauletf2824e62018-10-01 12:12:37 +02004190 goto end;
4191 }
4192
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004193 if (unlikely(txn->req.msg_state < HTTP_MSG_DONE)) {
4194 DBG_TRACE_DEVEL("waiting end of the request", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004195 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004196 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004197
4198 if (txn->req.msg_state == HTTP_MSG_DONE) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004199 /* No need to read anymore, the request was completely parsed.
4200 * We can shut the read side unless we want to abort_on_close,
4201 * or we have a POST request. The issue with POST requests is
4202 * that some browsers still send a CRLF after the request, and
4203 * this CRLF must be read so that it does not remain in the kernel
4204 * buffers, otherwise a close could cause an RST on some systems
4205 * (eg: Linux).
4206 */
Christopher Faulet769d0e92019-03-22 14:23:18 +01004207 if (!(s->be->options & PR_O_ABRT_CLOSE) && txn->meth != HTTP_METH_POST)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004208 channel_dont_read(chn);
4209
4210 /* if the server closes the connection, we want to immediately react
4211 * and close the socket to save packets and syscalls.
4212 */
4213 s->si[1].flags |= SI_FL_NOHALF;
4214
4215 /* In any case we've finished parsing the request so we must
4216 * disable Nagle when sending data because 1) we're not going
4217 * to shut this side, and 2) the server is waiting for us to
4218 * send pending data.
4219 */
4220 chn->flags |= CF_NEVER_WAIT;
4221
Christopher Fauletd01ce402019-01-02 17:44:13 +01004222 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
4223 /* The server has not finished to respond, so we
4224 * don't want to move in order not to upset it.
4225 */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004226 DBG_TRACE_DEVEL("waiting end of the response", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletd01ce402019-01-02 17:44:13 +01004227 return;
4228 }
4229
Christopher Fauletf2824e62018-10-01 12:12:37 +02004230 /* When we get here, it means that both the request and the
4231 * response have finished receiving. Depending on the connection
4232 * mode, we'll have to wait for the last bytes to leave in either
4233 * direction, and sometimes for a close to be effective.
4234 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02004235 if (txn->flags & TX_CON_WANT_TUN) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004236 /* Tunnel mode will not have any analyser so it needs to
4237 * poll for reads.
4238 */
4239 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004240 if (b_data(&chn->buf)) {
4241 DBG_TRACE_DEVEL("waiting to flush the request", STRM_EV_HTTP_ANA, s, txn);
Christopher Faulet9768c262018-10-22 09:34:31 +02004242 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004243 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004244 txn->req.msg_state = HTTP_MSG_TUNNEL;
4245 }
4246 else {
4247 /* we're not expecting any new data to come for this
4248 * transaction, so we can close it.
Christopher Faulet9768c262018-10-22 09:34:31 +02004249 *
4250 * However, there is an exception if the response
4251 * length is undefined. In this case, we need to wait
4252 * the close from the server. The response will be
4253 * switched in TUNNEL mode until the end.
Christopher Fauletf2824e62018-10-01 12:12:37 +02004254 */
4255 if (!(txn->rsp.flags & HTTP_MSGF_XFER_LEN) &&
4256 txn->rsp.msg_state != HTTP_MSG_CLOSED)
Christopher Faulet9768c262018-10-22 09:34:31 +02004257 goto check_channel_flags;
Christopher Fauletf2824e62018-10-01 12:12:37 +02004258
4259 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4260 channel_shutr_now(chn);
4261 channel_shutw_now(chn);
4262 }
4263 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004264 goto check_channel_flags;
4265 }
4266
4267 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
4268 http_msg_closing:
4269 /* nothing else to forward, just waiting for the output buffer
4270 * to be empty and for the shutw_now to take effect.
4271 */
4272 if (channel_is_empty(chn)) {
4273 txn->req.msg_state = HTTP_MSG_CLOSED;
4274 goto http_msg_closed;
4275 }
4276 else if (chn->flags & CF_SHUTW) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004277 txn->req.msg_state = HTTP_MSG_ERROR;
4278 goto end;
4279 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004280 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004281 return;
4282 }
4283
4284 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
4285 http_msg_closed:
Christopher Fauletf2824e62018-10-01 12:12:37 +02004286 /* if we don't know whether the server will close, we need to hard close */
4287 if (txn->rsp.flags & HTTP_MSGF_XFER_LEN)
4288 s->si[1].flags |= SI_FL_NOLINGER; /* we want to close ASAP */
Christopher Fauletf2824e62018-10-01 12:12:37 +02004289 /* see above in MSG_DONE why we only do this in these states */
Christopher Faulet769d0e92019-03-22 14:23:18 +01004290 if (!(s->be->options & PR_O_ABRT_CLOSE))
Christopher Fauletf2824e62018-10-01 12:12:37 +02004291 channel_dont_read(chn);
4292 goto end;
4293 }
4294
4295 check_channel_flags:
4296 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4297 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4298 /* if we've just closed an output, let's switch */
4299 txn->req.msg_state = HTTP_MSG_CLOSING;
4300 goto http_msg_closing;
4301 }
4302
4303 end:
4304 chn->analysers &= AN_REQ_FLT_END;
Christopher Faulet198ef8b2020-12-15 13:32:55 +01004305 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
4306 chn->flags |= CF_NEVER_WAIT;
4307 if (HAS_REQ_DATA_FILTERS(s))
Christopher Fauletf2824e62018-10-01 12:12:37 +02004308 chn->analysers |= AN_REQ_FLT_XFER_DATA;
Christopher Faulet198ef8b2020-12-15 13:32:55 +01004309 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004310 channel_auto_close(chn);
4311 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004312 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004313}
4314
4315
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05004316/* This function terminates the response because it was completely analyzed or
Christopher Fauletf2824e62018-10-01 12:12:37 +02004317 * because an error was triggered during the body forwarding.
4318 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004319static void http_end_response(struct stream *s)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004320{
4321 struct channel *chn = &s->res;
4322 struct http_txn *txn = s->txn;
4323
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004324 DBG_TRACE_ENTER(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004325
Christopher Fauletb42a8b62018-11-19 21:59:00 +01004326 if (unlikely(txn->req.msg_state == HTTP_MSG_ERROR ||
4327 txn->rsp.msg_state == HTTP_MSG_ERROR)) {
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004328 channel_htx_truncate(&s->req, htxbuf(&s->req.buf));
Christopher Faulet9768c262018-10-22 09:34:31 +02004329 channel_abort(&s->req);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004330 goto end;
4331 }
4332
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004333 if (unlikely(txn->rsp.msg_state < HTTP_MSG_DONE)) {
4334 DBG_TRACE_DEVEL("waiting end of the response", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004335 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004336 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004337
4338 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
4339 /* In theory, we don't need to read anymore, but we must
4340 * still monitor the server connection for a possible close
4341 * while the request is being uploaded, so we don't disable
4342 * reading.
4343 */
4344 /* channel_dont_read(chn); */
4345
4346 if (txn->req.msg_state < HTTP_MSG_DONE) {
4347 /* The client seems to still be sending data, probably
4348 * because we got an error response during an upload.
4349 * We have the choice of either breaking the connection
4350 * or letting it pass through. Let's do the later.
4351 */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004352 DBG_TRACE_DEVEL("waiting end of the request", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004353 return;
4354 }
4355
4356 /* When we get here, it means that both the request and the
4357 * response have finished receiving. Depending on the connection
4358 * mode, we'll have to wait for the last bytes to leave in either
4359 * direction, and sometimes for a close to be effective.
4360 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02004361 if (txn->flags & TX_CON_WANT_TUN) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004362 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004363 if (b_data(&chn->buf)) {
4364 DBG_TRACE_DEVEL("waiting to flush the respone", STRM_EV_HTTP_ANA, s, txn);
Christopher Faulet9768c262018-10-22 09:34:31 +02004365 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004366 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004367 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
4368 }
4369 else {
4370 /* we're not expecting any new data to come for this
4371 * transaction, so we can close it.
4372 */
4373 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4374 channel_shutr_now(chn);
4375 channel_shutw_now(chn);
4376 }
4377 }
4378 goto check_channel_flags;
4379 }
4380
4381 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
4382 http_msg_closing:
4383 /* nothing else to forward, just waiting for the output buffer
4384 * to be empty and for the shutw_now to take effect.
4385 */
4386 if (channel_is_empty(chn)) {
4387 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4388 goto http_msg_closed;
4389 }
4390 else if (chn->flags & CF_SHUTW) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004391 txn->rsp.msg_state = HTTP_MSG_ERROR;
Christopher Fauletcff0f732019-12-16 16:13:44 +01004392 _HA_ATOMIC_ADD(&strm_sess(s)->fe->fe_counters.cli_aborts, 1);
Olivier Houcharda798bf52019-03-08 18:52:00 +01004393 _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01004394 if (strm_sess(s)->listener->counters)
4395 _HA_ATOMIC_ADD(&strm_sess(s)->listener->counters->cli_aborts, 1);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004396 if (objt_server(s->target))
Christopher Fauletcff0f732019-12-16 16:13:44 +01004397 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.cli_aborts, 1);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004398 goto end;
4399 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004400 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004401 return;
4402 }
4403
4404 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
4405 http_msg_closed:
4406 /* drop any pending data */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004407 channel_htx_truncate(&s->req, htxbuf(&s->req.buf));
Christopher Faulet9768c262018-10-22 09:34:31 +02004408 channel_abort(&s->req);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004409 goto end;
4410 }
4411
4412 check_channel_flags:
4413 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4414 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4415 /* if we've just closed an output, let's switch */
4416 txn->rsp.msg_state = HTTP_MSG_CLOSING;
4417 goto http_msg_closing;
4418 }
4419
4420 end:
4421 chn->analysers &= AN_RES_FLT_END;
Christopher Faulet198ef8b2020-12-15 13:32:55 +01004422 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
4423 chn->flags |= CF_NEVER_WAIT;
4424 if (HAS_RSP_DATA_FILTERS(s))
4425 chn->analysers |= AN_RES_FLT_XFER_DATA;
4426 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004427 channel_auto_close(chn);
4428 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004429 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004430}
4431
Christopher Fauletef70e252020-01-28 09:26:19 +01004432/* Forward a response generated by HAProxy (error/redirect/return). This
4433 * function forwards all pending incoming data. If <final> is set to 0, nothing
4434 * more is performed. It is used for 1xx informational messages. Otherwise, the
Christopher Faulet507479b2020-05-15 12:29:46 +02004435 * transaction is terminated and the request is emptied. On success 1 is
Christopher Faulet40e6b552020-06-25 16:04:50 +02004436 * returned. If an error occurred, 0 is returned. If it fails, this function
4437 * only exits. It is the caller responsibility to do the cleanup.
Christopher Fauletef70e252020-01-28 09:26:19 +01004438 */
4439int http_forward_proxy_resp(struct stream *s, int final)
4440{
4441 struct channel *req = &s->req;
4442 struct channel *res = &s->res;
4443 struct htx *htx = htxbuf(&res->buf);
4444 size_t data;
4445
4446 if (final) {
4447 htx->flags |= HTX_FL_PROXY_RESP;
Christopher Faulet507479b2020-05-15 12:29:46 +02004448
Christopher Fauletaab1b672020-11-18 16:44:02 +01004449 if (!htx_is_empty(htx) && !http_eval_after_res_rules(s))
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01004450 return 0;
Christopher Fauletef70e252020-01-28 09:26:19 +01004451
Christopher Fauletd6c48362020-10-19 18:01:38 +02004452 if (s->txn->meth == HTTP_METH_HEAD)
4453 htx_skip_msg_payload(htx);
4454
Christopher Fauletef70e252020-01-28 09:26:19 +01004455 channel_auto_read(req);
4456 channel_abort(req);
4457 channel_auto_close(req);
4458 channel_htx_erase(req, htxbuf(&req->buf));
4459
4460 res->wex = tick_add_ifset(now_ms, res->wto);
4461 channel_auto_read(res);
4462 channel_auto_close(res);
4463 channel_shutr_now(res);
Christopher Faulet1a9db7c2020-06-25 15:36:45 +02004464 res->flags |= CF_EOI; /* The response is terminated, add EOI */
Christopher Faulet42432f32020-11-20 17:43:16 +01004465 htxbuf(&res->buf)->flags |= HTX_FL_EOM; /* no more data are expected */
Christopher Fauletef70e252020-01-28 09:26:19 +01004466 }
Christopher Fauletcf6898c2020-06-25 15:55:11 +02004467 else {
4468 /* Send ASAP informational messages. Rely on CF_EOI for final
4469 * response.
4470 */
4471 res->flags |= CF_SEND_DONTWAIT;
4472 }
Christopher Fauletef70e252020-01-28 09:26:19 +01004473
4474 data = htx->data - co_data(res);
4475 c_adv(res, data);
4476 htx->first = -1;
4477 res->total += data;
4478 return 1;
4479}
4480
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004481void http_server_error(struct stream *s, struct stream_interface *si, int err,
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004482 int finst, struct http_reply *msg)
Christopher Faulet0f226952018-10-22 09:29:56 +02004483{
Christopher Faulet72c7d8d2020-01-27 15:32:25 +01004484 http_reply_and_close(s, s->txn->status, msg);
Christopher Faulet0f226952018-10-22 09:29:56 +02004485 if (!(s->flags & SF_ERR_MASK))
4486 s->flags |= err;
4487 if (!(s->flags & SF_FINST_MASK))
4488 s->flags |= finst;
4489}
4490
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004491void http_reply_and_close(struct stream *s, short status, struct http_reply *msg)
Christopher Faulet0f226952018-10-22 09:29:56 +02004492{
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004493 if (!msg) {
4494 channel_htx_truncate(&s->res, htxbuf(&s->res.buf));
4495 goto end;
4496 }
4497
4498 if (http_reply_message(s, msg) == -1) {
4499 /* On error, return a 500 error message, but don't rewrite it if
Christopher Faulet40e6b552020-06-25 16:04:50 +02004500 * it is already an internal error. If it was already a "const"
4501 * 500 error, just fail.
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004502 */
Christopher Faulet40e6b552020-06-25 16:04:50 +02004503 if (s->txn->status == 500) {
4504 if (s->txn->flags & TX_CONST_REPLY)
4505 goto end;
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004506 s->txn->flags |= TX_CONST_REPLY;
Christopher Faulet40e6b552020-06-25 16:04:50 +02004507 }
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004508 s->txn->status = 500;
4509 s->txn->http_reply = NULL;
4510 return http_reply_and_close(s, s->txn->status, http_error_message(s));
4511 }
4512
4513end:
4514 s->res.wex = tick_add_ifset(now_ms, s->res.wto);
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004515
Christopher Faulet0f226952018-10-22 09:29:56 +02004516 channel_auto_read(&s->req);
4517 channel_abort(&s->req);
4518 channel_auto_close(&s->req);
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004519 channel_htx_erase(&s->req, htxbuf(&s->req.buf));
Christopher Faulet72c7d8d2020-01-27 15:32:25 +01004520 channel_auto_read(&s->res);
4521 channel_auto_close(&s->res);
4522 channel_shutr_now(&s->res);
Christopher Faulet0f226952018-10-22 09:29:56 +02004523}
4524
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004525struct http_reply *http_error_message(struct stream *s)
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004526{
4527 const int msgnum = http_get_status_idx(s->txn->status);
4528
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004529 if (s->txn->http_reply)
4530 return s->txn->http_reply;
4531 else if (s->be->replies[msgnum])
4532 return s->be->replies[msgnum];
4533 else if (strm_fe(s)->replies[msgnum])
4534 return strm_fe(s)->replies[msgnum];
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004535 else
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004536 return &http_err_replies[msgnum];
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004537}
4538
Christopher Faulet40e6b552020-06-25 16:04:50 +02004539/* Produces an HTX message from an http reply. Depending on the http reply type,
4540 * a, errorfile, an raw file or a log-format string is used. On success, it
4541 * returns 0. If an error occurs -1 is returned. If it fails, this function only
4542 * exits. It is the caller responsibility to do the cleanup.
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004543 */
Christopher Fauletae43b6c2020-05-27 15:24:22 +02004544int http_reply_to_htx(struct stream *s, struct htx *htx, struct http_reply *reply)
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004545{
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004546 struct buffer *errmsg;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004547 struct htx_sl *sl;
4548 struct buffer *body = NULL;
4549 const char *status, *reason, *clen, *ctype;
4550 unsigned int slflags;
4551 int ret = 0;
4552
Christopher Faulete29a97e2020-05-14 14:49:25 +02004553 /*
4554 * - HTTP_REPLY_ERRFILES unexpected here. handled as no payload if so
4555 *
4556 * - HTTP_REPLY_INDIRECT: switch on another reply if defined or handled
4557 * as no payload if NULL. the TXN status code is set with the status
4558 * of the original reply.
4559 */
4560
4561 if (reply->type == HTTP_REPLY_INDIRECT) {
4562 if (reply->body.reply)
4563 reply = reply->body.reply;
4564 }
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004565 if (reply->type == HTTP_REPLY_ERRMSG && !reply->body.errmsg) {
4566 /* get default error message */
4567 if (reply == s->txn->http_reply)
4568 s->txn->http_reply = NULL;
4569 reply = http_error_message(s);
4570 if (reply->type == HTTP_REPLY_INDIRECT) {
4571 if (reply->body.reply)
4572 reply = reply->body.reply;
4573 }
4574 }
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004575
4576 if (reply->type == HTTP_REPLY_ERRMSG) {
4577 /* implicit or explicit error message*/
4578 errmsg = reply->body.errmsg;
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004579 if (errmsg && !b_is_null(errmsg)) {
Christopher Faulet20567362020-05-15 14:52:49 +02004580 if (!htx_copy_msg(htx, errmsg))
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004581 goto fail;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004582 }
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004583 }
4584 else {
4585 /* no payload, file or log-format string */
4586 if (reply->type == HTTP_REPLY_RAW) {
4587 /* file */
4588 body = &reply->body.obj;
4589 }
4590 else if (reply->type == HTTP_REPLY_LOGFMT) {
4591 /* log-format string */
4592 body = alloc_trash_chunk();
4593 if (!body)
4594 goto fail_alloc;
4595 body->data = build_logline(s, body->area, body->size, &reply->body.fmt);
4596 }
4597 /* else no payload */
4598
4599 status = ultoa(reply->status);
4600 reason = http_get_reason(reply->status);
4601 slflags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_CLEN);
4602 if (!body || !b_data(body))
4603 slflags |= HTX_SL_F_BODYLESS;
4604 sl = htx_add_stline(htx, HTX_BLK_RES_SL, slflags, ist("HTTP/1.1"), ist(status), ist(reason));
4605 if (!sl)
4606 goto fail;
4607 sl->info.res.status = reply->status;
4608
4609 clen = (body ? ultoa(b_data(body)) : "0");
4610 ctype = reply->ctype;
4611
4612 if (!LIST_ISEMPTY(&reply->hdrs)) {
4613 struct http_reply_hdr *hdr;
4614 struct buffer *value = alloc_trash_chunk();
4615
4616 if (!value)
4617 goto fail;
4618
4619 list_for_each_entry(hdr, &reply->hdrs, list) {
4620 chunk_reset(value);
4621 value->data = build_logline(s, value->area, value->size, &hdr->value);
4622 if (b_data(value) && !htx_add_header(htx, hdr->name, ist2(b_head(value), b_data(value)))) {
4623 free_trash_chunk(value);
4624 goto fail;
4625 }
4626 chunk_reset(value);
4627 }
4628 free_trash_chunk(value);
4629 }
4630
4631 if (!htx_add_header(htx, ist("content-length"), ist(clen)) ||
4632 (body && b_data(body) && ctype && !htx_add_header(htx, ist("content-type"), ist(ctype))) ||
4633 !htx_add_endof(htx, HTX_BLK_EOH) ||
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004634 (body && b_data(body) && !htx_add_data_atonce(htx, ist2(b_head(body), b_data(body)))))
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004635 goto fail;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004636
4637 htx->flags |= HTX_FL_EOM;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004638 }
4639
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004640 leave:
4641 if (reply->type == HTTP_REPLY_LOGFMT)
4642 free_trash_chunk(body);
4643 return ret;
4644
4645 fail_alloc:
4646 if (!(s->flags & SF_ERR_MASK))
4647 s->flags |= SF_ERR_RESOURCE;
Christopher Faulet97e466c2020-05-15 15:12:47 +02004648 /* fall through */
4649 fail:
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004650 ret = -1;
4651 goto leave;
Christopher Faulet97e466c2020-05-15 15:12:47 +02004652}
4653
4654/* Send an http reply to the client. On success, it returns 0. If an error
Christopher Faulet40e6b552020-06-25 16:04:50 +02004655 * occurs -1 is returned and the response channel is truncated, removing this
4656 * way the faulty reply. This function may fail when the reply is formatted
4657 * (http_reply_to_htx) or when the reply is forwarded
4658 * (http_forward_proxy_resp). On the last case, it is because a
4659 * http-after-response rule fails.
Christopher Faulet97e466c2020-05-15 15:12:47 +02004660 */
4661int http_reply_message(struct stream *s, struct http_reply *reply)
4662{
4663 struct channel *res = &s->res;
4664 struct htx *htx = htx_from_buf(&res->buf);
4665
4666 if (s->txn->status == -1)
4667 s->txn->status = reply->status;
4668 channel_htx_truncate(res, htx);
4669
4670 if (http_reply_to_htx(s, htx, reply) == -1)
4671 goto fail;
4672
4673 htx_to_buf(htx, &s->res.buf);
4674 if (!http_forward_proxy_resp(s, 1))
4675 goto fail;
4676 return 0;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004677
4678 fail:
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004679 channel_htx_truncate(res, htx);
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004680 if (!(s->flags & SF_ERR_MASK))
4681 s->flags |= SF_ERR_PRXCOND;
Christopher Faulet97e466c2020-05-15 15:12:47 +02004682 return -1;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004683}
4684
Christopher Faulet304cc402019-07-15 15:46:28 +02004685/* Return the error message corresponding to si->err_type. It is assumed
4686 * that the server side is closed. Note that err_type is actually a
4687 * bitmask, where almost only aborts may be cumulated with other
4688 * values. We consider that aborted operations are more important
4689 * than timeouts or errors due to the fact that nobody else in the
4690 * logs might explain incomplete retries. All others should avoid
4691 * being cumulated. It should normally not be possible to have multiple
4692 * aborts at once, but just in case, the first one in sequence is reported.
4693 * Note that connection errors appearing on the second request of a keep-alive
4694 * connection are not reported since this allows the client to retry.
4695 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004696void http_return_srv_error(struct stream *s, struct stream_interface *si)
Christopher Faulet304cc402019-07-15 15:46:28 +02004697{
4698 int err_type = si->err_type;
4699
4700 /* set s->txn->status for http_error_message(s) */
4701 s->txn->status = 503;
4702
4703 if (err_type & SI_ET_QUEUE_ABRT)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004704 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_Q,
4705 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004706 else if (err_type & SI_ET_CONN_ABRT)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004707 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_C,
4708 (s->txn->flags & TX_NOT_FIRST) ? NULL :
4709 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004710 else if (err_type & SI_ET_QUEUE_TO)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004711 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_Q,
4712 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004713 else if (err_type & SI_ET_QUEUE_ERR)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004714 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_Q,
4715 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004716 else if (err_type & SI_ET_CONN_TO)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004717 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_C,
4718 (s->txn->flags & TX_NOT_FIRST) ? NULL :
4719 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004720 else if (err_type & SI_ET_CONN_ERR)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004721 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_C,
4722 (s->flags & SF_SRV_REUSED) ? NULL :
4723 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004724 else if (err_type & SI_ET_CONN_RES)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004725 http_server_error(s, si, SF_ERR_RESOURCE, SF_FINST_C,
4726 (s->txn->flags & TX_NOT_FIRST) ? NULL :
4727 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004728 else { /* SI_ET_CONN_OTHER and others */
4729 s->txn->status = 500;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004730 http_server_error(s, si, SF_ERR_INTERNAL, SF_FINST_C,
4731 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004732 }
4733}
4734
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004735
Christopher Faulet4a28a532019-03-01 11:19:40 +01004736/* Handle Expect: 100-continue for HTTP/1.1 messages if necessary. It returns 0
4737 * on success and -1 on error.
4738 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004739static int http_handle_expect_hdr(struct stream *s, struct htx *htx, struct http_msg *msg)
Christopher Faulet4a28a532019-03-01 11:19:40 +01004740{
4741 /* If we have HTTP/1.1 message with a body and Expect: 100-continue,
4742 * then we must send an HTTP/1.1 100 Continue intermediate response.
4743 */
4744 if (msg->msg_state == HTTP_MSG_BODY && (msg->flags & HTTP_MSGF_VER_11) &&
4745 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
4746 struct ist hdr = { .ptr = "Expect", .len = 6 };
4747 struct http_hdr_ctx ctx;
4748
4749 ctx.blk = NULL;
4750 /* Expect is allowed in 1.1, look for it */
4751 if (http_find_header(htx, hdr, &ctx, 0) &&
4752 unlikely(isteqi(ctx.value, ist2("100-continue", 12)))) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004753 if (http_reply_100_continue(s) == -1)
Christopher Faulet4a28a532019-03-01 11:19:40 +01004754 return -1;
4755 http_remove_header(htx, &ctx);
4756 }
4757 }
4758 return 0;
4759}
4760
Christopher Faulet23a3c792018-11-28 10:01:23 +01004761/* Send a 100-Continue response to the client. It returns 0 on success and -1
4762 * on error. The response channel is updated accordingly.
4763 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004764static int http_reply_100_continue(struct stream *s)
Christopher Faulet23a3c792018-11-28 10:01:23 +01004765{
4766 struct channel *res = &s->res;
4767 struct htx *htx = htx_from_buf(&res->buf);
4768 struct htx_sl *sl;
4769 unsigned int flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|
4770 HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
Christopher Faulet23a3c792018-11-28 10:01:23 +01004771
4772 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
4773 ist("HTTP/1.1"), ist("100"), ist("Continue"));
4774 if (!sl)
4775 goto fail;
4776 sl->info.res.status = 100;
4777
Christopher Faulet1d5ec092019-06-26 14:23:54 +02004778 if (!htx_add_endof(htx, HTX_BLK_EOH))
Christopher Faulet23a3c792018-11-28 10:01:23 +01004779 goto fail;
4780
Christopher Fauleta72a7e42020-01-28 09:28:11 +01004781 if (!http_forward_proxy_resp(s, 0))
4782 goto fail;
Christopher Faulet23a3c792018-11-28 10:01:23 +01004783 return 0;
4784
4785 fail:
4786 /* If an error occurred, remove the incomplete HTTP response from the
4787 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004788 channel_htx_truncate(res, htx);
Christopher Faulet23a3c792018-11-28 10:01:23 +01004789 return -1;
4790}
4791
Christopher Faulet12c51e22018-11-28 15:59:42 +01004792
Christopher Faulet0f226952018-10-22 09:29:56 +02004793/*
4794 * Capture headers from message <htx> according to header list <cap_hdr>, and
4795 * fill the <cap> pointers appropriately.
4796 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004797static void http_capture_headers(struct htx *htx, char **cap, struct cap_hdr *cap_hdr)
Christopher Faulet0f226952018-10-22 09:29:56 +02004798{
4799 struct cap_hdr *h;
4800 int32_t pos;
4801
Christopher Fauleta3f15502019-05-13 15:27:23 +02004802 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet0f226952018-10-22 09:29:56 +02004803 struct htx_blk *blk = htx_get_blk(htx, pos);
4804 enum htx_blk_type type = htx_get_blk_type(blk);
4805 struct ist n, v;
4806
4807 if (type == HTX_BLK_EOH)
4808 break;
4809 if (type != HTX_BLK_HDR)
4810 continue;
4811
4812 n = htx_get_blk_name(htx, blk);
4813
4814 for (h = cap_hdr; h; h = h->next) {
4815 if (h->namelen && (h->namelen == n.len) &&
4816 (strncasecmp(n.ptr, h->name, h->namelen) == 0)) {
4817 if (cap[h->index] == NULL)
4818 cap[h->index] =
4819 pool_alloc(h->pool);
4820
4821 if (cap[h->index] == NULL) {
4822 ha_alert("HTTP capture : out of memory.\n");
4823 break;
4824 }
4825
4826 v = htx_get_blk_value(htx, blk);
4827 if (v.len > h->len)
4828 v.len = h->len;
4829
4830 memcpy(cap[h->index], v.ptr, v.len);
4831 cap[h->index][v.len]=0;
4832 }
4833 }
4834 }
4835}
4836
Christopher Faulet0b6bdc52018-10-24 11:05:36 +02004837/* Delete a value in a header between delimiters <from> and <next>. The header
4838 * itself is delimited by <start> and <end> pointers. The number of characters
4839 * displaced is returned, and the pointer to the first delimiter is updated if
4840 * required. The function tries as much as possible to respect the following
4841 * principles :
4842 * - replace <from> delimiter by the <next> one unless <from> points to <start>,
4843 * in which case <next> is simply removed
4844 * - set exactly one space character after the new first delimiter, unless there
4845 * are not enough characters in the block being moved to do so.
4846 * - remove unneeded spaces before the previous delimiter and after the new
4847 * one.
4848 *
4849 * It is the caller's responsibility to ensure that :
4850 * - <from> points to a valid delimiter or <start> ;
4851 * - <next> points to a valid delimiter or <end> ;
4852 * - there are non-space chars before <from>.
4853 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004854static int http_del_hdr_value(char *start, char *end, char **from, char *next)
Christopher Faulet0b6bdc52018-10-24 11:05:36 +02004855{
4856 char *prev = *from;
4857
4858 if (prev == start) {
4859 /* We're removing the first value. eat the semicolon, if <next>
4860 * is lower than <end> */
4861 if (next < end)
4862 next++;
4863
4864 while (next < end && HTTP_IS_SPHT(*next))
4865 next++;
4866 }
4867 else {
4868 /* Remove useless spaces before the old delimiter. */
4869 while (HTTP_IS_SPHT(*(prev-1)))
4870 prev--;
4871 *from = prev;
4872
4873 /* copy the delimiter and if possible a space if we're
4874 * not at the end of the line.
4875 */
4876 if (next < end) {
4877 *prev++ = *next++;
4878 if (prev + 1 < next)
4879 *prev++ = ' ';
4880 while (next < end && HTTP_IS_SPHT(*next))
4881 next++;
4882 }
4883 }
4884 memmove(prev, next, end - next);
4885 return (prev - next);
4886}
4887
Christopher Faulet0f226952018-10-22 09:29:56 +02004888
4889/* Formats the start line of the request (without CRLF) and puts it in <str> and
Joseph Herlantc42c0e92018-11-25 10:43:27 -08004890 * return the written length. The line can be truncated if it exceeds <len>.
Christopher Faulet0f226952018-10-22 09:29:56 +02004891 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004892static size_t http_fmt_req_line(const struct htx_sl *sl, char *str, size_t len)
Christopher Faulet0f226952018-10-22 09:29:56 +02004893{
4894 struct ist dst = ist2(str, 0);
4895
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004896 if (istcat(&dst, htx_sl_req_meth(sl), len) == -1)
Christopher Faulet0f226952018-10-22 09:29:56 +02004897 goto end;
4898 if (dst.len + 1 > len)
4899 goto end;
4900 dst.ptr[dst.len++] = ' ';
4901
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004902 if (istcat(&dst, htx_sl_req_uri(sl), len) == -1)
Christopher Faulet0f226952018-10-22 09:29:56 +02004903 goto end;
4904 if (dst.len + 1 > len)
4905 goto end;
4906 dst.ptr[dst.len++] = ' ';
4907
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004908 istcat(&dst, htx_sl_req_vsn(sl), len);
Christopher Faulet0f226952018-10-22 09:29:56 +02004909 end:
4910 return dst.len;
4911}
4912
4913/*
4914 * Print a debug line with a start line.
4915 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004916static void http_debug_stline(const char *dir, struct stream *s, const struct htx_sl *sl)
Christopher Faulet0f226952018-10-22 09:29:56 +02004917{
4918 struct session *sess = strm_sess(s);
4919 int max;
4920
4921 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
4922 dir,
4923 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->handle.fd : -1,
4924 objt_cs(s->si[1].end) ? (unsigned short)objt_cs(s->si[1].end)->conn->handle.fd : -1);
4925
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004926 max = HTX_SL_P1_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02004927 UBOUND(max, trash.size - trash.data - 3);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004928 chunk_memcat(&trash, HTX_SL_P1_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02004929 trash.area[trash.data++] = ' ';
4930
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004931 max = HTX_SL_P2_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02004932 UBOUND(max, trash.size - trash.data - 2);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004933 chunk_memcat(&trash, HTX_SL_P2_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02004934 trash.area[trash.data++] = ' ';
4935
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004936 max = HTX_SL_P3_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02004937 UBOUND(max, trash.size - trash.data - 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004938 chunk_memcat(&trash, HTX_SL_P3_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02004939 trash.area[trash.data++] = '\n';
4940
Willy Tarreau2e8ab6b2020-03-14 11:03:20 +01004941 DISGUISE(write(1, trash.area, trash.data));
Christopher Faulet0f226952018-10-22 09:29:56 +02004942}
4943
4944/*
4945 * Print a debug line with a header.
4946 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004947static 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 +02004948{
4949 struct session *sess = strm_sess(s);
4950 int max;
4951
4952 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
4953 dir,
4954 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->handle.fd : -1,
4955 objt_cs(s->si[1].end) ? (unsigned short)objt_cs(s->si[1].end)->conn->handle.fd : -1);
4956
4957 max = n.len;
4958 UBOUND(max, trash.size - trash.data - 3);
4959 chunk_memcat(&trash, n.ptr, max);
4960 trash.area[trash.data++] = ':';
4961 trash.area[trash.data++] = ' ';
4962
4963 max = v.len;
4964 UBOUND(max, trash.size - trash.data - 1);
4965 chunk_memcat(&trash, v.ptr, max);
4966 trash.area[trash.data++] = '\n';
4967
Willy Tarreau2e8ab6b2020-03-14 11:03:20 +01004968 DISGUISE(write(1, trash.area, trash.data));
Christopher Faulet0f226952018-10-22 09:29:56 +02004969}
4970
Christopher Fauleta8a46e22019-07-16 14:53:09 +02004971/* Allocate a new HTTP transaction for stream <s> unless there is one already.
4972 * In case of allocation failure, everything allocated is freed and NULL is
4973 * returned. Otherwise the new transaction is assigned to the stream and
4974 * returned.
4975 */
4976struct http_txn *http_alloc_txn(struct stream *s)
4977{
4978 struct http_txn *txn = s->txn;
4979
4980 if (txn)
4981 return txn;
4982
4983 txn = pool_alloc(pool_head_http_txn);
4984 if (!txn)
4985 return txn;
4986
4987 s->txn = txn;
4988 return txn;
4989}
4990
4991void http_txn_reset_req(struct http_txn *txn)
4992{
Christopher Faulet1aea50e2020-01-17 16:03:53 +01004993 txn->req.flags = 0;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02004994 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
4995}
4996
4997void http_txn_reset_res(struct http_txn *txn)
4998{
Christopher Faulet1aea50e2020-01-17 16:03:53 +01004999 txn->rsp.flags = 0;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005000 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
5001}
5002
5003/*
5004 * Initialize a new HTTP transaction for stream <s>. It is assumed that all
5005 * the required fields are properly allocated and that we only need to (re)init
5006 * them. This should be used before processing any new request.
5007 */
5008void http_init_txn(struct stream *s)
5009{
5010 struct http_txn *txn = s->txn;
5011 struct conn_stream *cs = objt_cs(s->si[0].end);
5012
Christopher Fauletda831fa2020-10-06 17:58:43 +02005013 txn->flags = ((cs && cs->flags & CS_FL_NOT_FIRST) ? TX_NOT_FIRST : 0);
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005014 txn->status = -1;
Christopher Faulet5cb513a2020-05-13 17:56:56 +02005015 txn->http_reply = NULL;
Willy Tarreau8b507582020-02-25 09:35:07 +01005016 write_u32(txn->cache_hash, 0);
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005017
5018 txn->cookie_first_date = 0;
5019 txn->cookie_last_date = 0;
5020
5021 txn->srv_cookie = NULL;
5022 txn->cli_cookie = NULL;
5023 txn->uri = NULL;
5024
5025 http_txn_reset_req(txn);
5026 http_txn_reset_res(txn);
5027
5028 txn->req.chn = &s->req;
5029 txn->rsp.chn = &s->res;
5030
5031 txn->auth.method = HTTP_AUTH_UNKNOWN;
5032
5033 vars_init(&s->vars_txn, SCOPE_TXN);
5034 vars_init(&s->vars_reqres, SCOPE_REQ);
5035}
5036
5037/* to be used at the end of a transaction */
5038void http_end_txn(struct stream *s)
5039{
5040 struct http_txn *txn = s->txn;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005041
5042 /* these ones will have been dynamically allocated */
5043 pool_free(pool_head_requri, txn->uri);
5044 pool_free(pool_head_capture, txn->cli_cookie);
5045 pool_free(pool_head_capture, txn->srv_cookie);
Tim Duesterhusa17e6622020-03-05 20:19:02 +01005046 pool_free(pool_head_uniqueid, s->unique_id.ptr);
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005047
Tim Duesterhusa17e6622020-03-05 20:19:02 +01005048 s->unique_id = IST_NULL;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005049 txn->uri = NULL;
5050 txn->srv_cookie = NULL;
5051 txn->cli_cookie = NULL;
5052
Christopher Faulet59399252019-11-07 14:27:52 +01005053 if (!LIST_ISEMPTY(&s->vars_txn.head))
5054 vars_prune(&s->vars_txn, s->sess, s);
5055 if (!LIST_ISEMPTY(&s->vars_reqres.head))
5056 vars_prune(&s->vars_reqres, s->sess, s);
5057}
5058
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005059
5060DECLARE_POOL(pool_head_http_txn, "http_txn", sizeof(struct http_txn));
Christopher Faulet0f226952018-10-22 09:29:56 +02005061
Christopher Fauletf4eb75d2018-10-11 15:55:07 +02005062__attribute__((constructor))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005063static void __http_protocol_init(void)
Christopher Fauletf4eb75d2018-10-11 15:55:07 +02005064{
5065}
5066
5067
5068/*
5069 * Local variables:
5070 * c-indent-level: 8
5071 * c-basic-offset: 8
5072 * End:
5073 */