blob: a13c9468266b56ac484c3964a520793a104fea63 [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 Faulet97b3a612021-03-15 17:10:12 +0100104 /* Don't connect for now */
105 channel_dont_connect(req);
106
107 /* A SHUTR at this stage means we are performing a "destructive"
108 * HTTP upgrade (TCP>H2). In this case, we can leave.
109 */
110 if (req->flags & CF_SHUTR) {
111 s->logs.logwait = 0;
112 s->logs.level = 0;
113 channel_abort(&s->req);
114 channel_abort(&s->res);
115 req->analysers &= AN_REQ_FLT_END;
116 req->analyse_exp = TICK_ETERNITY;
117 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA, s);
118 return 1;
119 }
Christopher Fauletda46a0d2021-01-21 17:32:58 +0100120 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA, s);
121 return 0;
122 }
123
124 htx = htxbuf(&req->buf);
Christopher Faulet8bebd2f2020-10-06 17:54:56 +0200125
Willy Tarreau4236f032019-03-05 10:43:32 +0100126 /* Parsing errors are caught here */
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200127 if (htx->flags & (HTX_FL_PARSING_ERROR|HTX_FL_PROCESSING_ERROR)) {
Willy Tarreau4236f032019-03-05 10:43:32 +0100128 stream_inc_http_req_ctr(s);
Emeric Brun28976442020-10-07 08:50:09 +0200129 proxy_inc_fe_req_ctr(sess->listener, sess->fe);
Christopher Fauletbf7175f2021-02-10 14:58:01 +0100130 if (htx->flags & HTX_FL_PARSING_ERROR) {
131 stream_inc_http_err_ctr(s);
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200132 goto return_bad_req;
Christopher Fauletbf7175f2021-02-10 14:58:01 +0100133 }
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200134 else
135 goto return_int_err;
Willy Tarreau4236f032019-03-05 10:43:32 +0100136 }
137
Christopher Faulete0768eb2018-10-03 16:38:02 +0200138 /* we're speaking HTTP here, so let's speak HTTP to the client */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200139 s->srv_error = http_return_srv_error;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200140
Christopher Faulet9768c262018-10-22 09:34:31 +0200141 msg->msg_state = HTTP_MSG_BODY;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200142 stream_inc_http_req_ctr(s);
Emeric Brun28976442020-10-07 08:50:09 +0200143 proxy_inc_fe_req_ctr(sess->listener, sess->fe); /* one more valid request for this FE */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200144
Christopher Faulet9768c262018-10-22 09:34:31 +0200145 /* kill the pending keep-alive timeout */
Christopher Faulet9768c262018-10-22 09:34:31 +0200146 req->analyse_exp = TICK_ETERNITY;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200147
Christopher Faulet29f17582019-05-23 11:03:26 +0200148 BUG_ON(htx_get_first_type(htx) != HTX_BLK_REQ_SL);
Christopher Faulet297fbb42019-05-13 14:41:27 +0200149 sl = http_get_stline(htx);
Christopher Faulet03599112018-11-27 11:21:21 +0100150
Christopher Faulet9768c262018-10-22 09:34:31 +0200151 /* 0: we might have to print this header in debug mode */
152 if (unlikely((global.mode & MODE_DEBUG) &&
153 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
154 int32_t pos;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200155
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200156 http_debug_stline("clireq", s, sl);
Christopher Faulet9768c262018-10-22 09:34:31 +0200157
Christopher Fauleta3f15502019-05-13 15:27:23 +0200158 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet9768c262018-10-22 09:34:31 +0200159 struct htx_blk *blk = htx_get_blk(htx, pos);
160 enum htx_blk_type type = htx_get_blk_type(blk);
161
162 if (type == HTX_BLK_EOH)
163 break;
164 if (type != HTX_BLK_HDR)
165 continue;
166
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200167 http_debug_hdr("clihdr", s,
168 htx_get_blk_name(htx, blk),
169 htx_get_blk_value(htx, blk));
Christopher Faulet9768c262018-10-22 09:34:31 +0200170 }
171 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200172
173 /*
Christopher Faulet03599112018-11-27 11:21:21 +0100174 * 1: identify the method and the version. Also set HTTP flags
Christopher Faulete0768eb2018-10-03 16:38:02 +0200175 */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100176 txn->meth = sl->info.req.meth;
Christopher Faulet03599112018-11-27 11:21:21 +0100177 if (sl->flags & HTX_SL_F_VER_11)
Christopher Faulet9768c262018-10-22 09:34:31 +0200178 msg->flags |= HTTP_MSGF_VER_11;
Christopher Faulet03599112018-11-27 11:21:21 +0100179 msg->flags |= HTTP_MSGF_XFER_LEN;
Christopher Faulet2a408542020-11-20 14:22:37 +0100180 if (sl->flags & HTX_SL_F_CLEN)
181 msg->flags |= HTTP_MSGF_CNT_LEN;
182 else if (sl->flags & HTX_SL_F_CHNK)
183 msg->flags |= HTTP_MSGF_TE_CHNK;
Christopher Fauletb2db4fa2018-11-27 16:51:09 +0100184 if (sl->flags & HTX_SL_F_BODYLESS)
185 msg->flags |= HTTP_MSGF_BODYLESS;
Christopher Faulet576c3582021-01-08 15:53:01 +0100186 if (sl->flags & HTX_SL_F_CONN_UPG)
187 msg->flags |= HTTP_MSGF_CONN_UPG;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200188
189 /* we can make use of server redirect on GET and HEAD */
190 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
191 s->flags |= SF_REDIRECTABLE;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100192 else if (txn->meth == HTTP_METH_OTHER && isteqi(htx_sl_req_meth(sl), ist("PRI"))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200193 /* PRI is reserved for the HTTP/2 preface */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200194 goto return_bad_req;
195 }
196
197 /*
Christopher Faulet6072beb2020-02-18 15:34:58 +0100198 * 2: check if the URI matches the monitor_uri. We have to do this for
199 * every request which gets in, because the monitor-uri is defined by
200 * the frontend. If the monitor-uri starts with a '/', the matching is
201 * done against the request's path. Otherwise, the request's uri is
202 * used. It is a workaround to let HTTP/2 health-checks work as
203 * expected.
Christopher Faulete0768eb2018-10-03 16:38:02 +0200204 */
205 if (unlikely((sess->fe->monitor_uri_len != 0) &&
Christopher Faulet6072beb2020-02-18 15:34:58 +0100206 ((*sess->fe->monitor_uri == '/' && isteq(http_get_path(htx_sl_req_uri(sl)),
207 ist2(sess->fe->monitor_uri, sess->fe->monitor_uri_len))) ||
208 isteq(htx_sl_req_uri(sl), ist2(sess->fe->monitor_uri, sess->fe->monitor_uri_len))))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200209 /*
210 * We have found the monitor URI
211 */
212 struct acl_cond *cond;
213
214 s->flags |= SF_MONITOR;
Olivier Houcharda798bf52019-03-08 18:52:00 +0100215 _HA_ATOMIC_ADD(&sess->fe->fe_counters.intercepted_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200216
217 /* Check if we want to fail this monitor request or not */
218 list_for_each_entry(cond, &sess->fe->mon_fail_cond, list) {
219 int ret = acl_exec_cond(cond, sess->fe, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
220
221 ret = acl_pass(ret);
222 if (cond->pol == ACL_COND_UNLESS)
223 ret = !ret;
224
225 if (ret) {
226 /* we fail this request, let's return 503 service unavail */
227 txn->status = 503;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200228 if (!(s->flags & SF_ERR_MASK))
229 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
230 goto return_prx_cond;
231 }
232 }
233
Joseph Herlantc42c0e92018-11-25 10:43:27 -0800234 /* nothing to fail, let's reply normally */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200235 txn->status = 200;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200236 if (!(s->flags & SF_ERR_MASK))
237 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
238 goto return_prx_cond;
239 }
240
241 /*
242 * 3: Maybe we have to copy the original REQURI for the logs ?
243 * Note: we cannot log anymore if the request has been
244 * classified as invalid.
245 */
246 if (unlikely(s->logs.logwait & LW_REQ)) {
247 /* we have a complete HTTP request that we must log */
248 if ((txn->uri = pool_alloc(pool_head_requri)) != NULL) {
Christopher Faulet9768c262018-10-22 09:34:31 +0200249 size_t len;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200250
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200251 len = http_fmt_req_line(sl, txn->uri, global.tune.requri_len - 1);
Christopher Faulet9768c262018-10-22 09:34:31 +0200252 txn->uri[len] = 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200253
254 if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT)))
255 s->do_log(s);
256 } else {
257 ha_alert("HTTP logging : out of memory.\n");
258 }
259 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200260
Christopher Faulete0768eb2018-10-03 16:38:02 +0200261 /* if the frontend has "option http-use-proxy-header", we'll check if
262 * we have what looks like a proxied connection instead of a connection,
263 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
264 * Note that this is *not* RFC-compliant, however browsers and proxies
265 * happen to do that despite being non-standard :-(
266 * We consider that a request not beginning with either '/' or '*' is
267 * a proxied connection, which covers both "scheme://location" and
268 * CONNECT ip:port.
269 */
270 if ((sess->fe->options2 & PR_O2_USE_PXHDR) &&
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100271 *HTX_SL_REQ_UPTR(sl) != '/' && *HTX_SL_REQ_UPTR(sl) != '*')
Christopher Faulete0768eb2018-10-03 16:38:02 +0200272 txn->flags |= TX_USE_PX_CONN;
273
Christopher Faulete0768eb2018-10-03 16:38:02 +0200274 /* 5: we may need to capture headers */
275 if (unlikely((s->logs.logwait & LW_REQHDR) && s->req_cap))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200276 http_capture_headers(htx, s->req_cap, sess->fe->req_cap);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200277
Christopher Faulete0768eb2018-10-03 16:38:02 +0200278 /* we may have to wait for the request's body */
Christopher Faulet9768c262018-10-22 09:34:31 +0200279 if (s->be->options & PR_O_WREQ_BODY)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200280 req->analysers |= AN_REQ_HTTP_BODY;
281
282 /*
283 * RFC7234#4:
284 * A cache MUST write through requests with methods
285 * that are unsafe (Section 4.2.1 of [RFC7231]) to
286 * the origin server; i.e., a cache is not allowed
287 * to generate a reply to such a request before
288 * having forwarded the request and having received
289 * a corresponding response.
290 *
291 * RFC7231#4.2.1:
292 * Of the request methods defined by this
293 * specification, the GET, HEAD, OPTIONS, and TRACE
294 * methods are defined to be safe.
295 */
296 if (likely(txn->meth == HTTP_METH_GET ||
297 txn->meth == HTTP_METH_HEAD ||
298 txn->meth == HTTP_METH_OPTIONS ||
299 txn->meth == HTTP_METH_TRACE))
300 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
301
302 /* end of job, return OK */
303 req->analysers &= ~an_bit;
304 req->analyse_exp = TICK_ETERNITY;
Christopher Faulet9768c262018-10-22 09:34:31 +0200305
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100306 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200307 return 1;
308
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200309 return_int_err:
310 txn->status = 500;
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200311 if (!(s->flags & SF_ERR_MASK))
312 s->flags |= SF_ERR_INTERNAL;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100313 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
William Lallemand36119de2021-03-08 15:26:48 +0100314 if (sess->listener && sess->listener->counters)
Christopher Fauletb8a53712019-12-16 11:29:38 +0100315 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200316 goto return_prx_cond;
317
Christopher Faulete0768eb2018-10-03 16:38:02 +0200318 return_bad_req:
Christopher Faulet9768c262018-10-22 09:34:31 +0200319 txn->status = 400;
Olivier Houcharda798bf52019-03-08 18:52:00 +0100320 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
William Lallemand36119de2021-03-08 15:26:48 +0100321 if (sess->listener && sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100322 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200323 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200324
325 return_prx_cond:
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200326 http_reply_and_close(s, txn->status, http_error_message(s));
327
Christopher Faulete0768eb2018-10-03 16:38:02 +0200328 if (!(s->flags & SF_ERR_MASK))
329 s->flags |= SF_ERR_PRXCOND;
330 if (!(s->flags & SF_FINST_MASK))
331 s->flags |= SF_FINST_R;
332
333 req->analysers &= AN_REQ_FLT_END;
334 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100335 DBG_TRACE_DEVEL("leaving on error",
336 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200337 return 0;
338}
339
340
341/* This stream analyser runs all HTTP request processing which is common to
342 * frontends and backends, which means blocking ACLs, filters, connection-close,
343 * reqadd, stats and redirects. This is performed for the designated proxy.
344 * It returns 1 if the processing can continue on next analysers, or zero if it
345 * either needs more data or wants to immediately abort the request (eg: deny,
346 * error, ...).
347 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200348int http_process_req_common(struct stream *s, struct channel *req, int an_bit, struct proxy *px)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200349{
350 struct session *sess = s->sess;
351 struct http_txn *txn = s->txn;
352 struct http_msg *msg = &txn->req;
Christopher Fauletff2759f2018-10-24 11:13:16 +0200353 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200354 struct redirect_rule *rule;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200355 enum rule_result verdict;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200356 struct connection *conn = objt_conn(sess->origin);
357
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100358 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200359
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100360 htx = htxbuf(&req->buf);
Christopher Fauletff2759f2018-10-24 11:13:16 +0200361
Christopher Faulet1907ccc2019-04-29 13:12:02 +0200362 /* just in case we have some per-backend tracking. Only called the first
363 * execution of the analyser. */
364 if (!s->current_rule || s->current_rule_list != &px->http_req_rules)
365 stream_inc_be_http_req_ctr(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200366
367 /* evaluate http-request rules */
368 if (!LIST_ISEMPTY(&px->http_req_rules)) {
Christopher Fauletb58f62b2020-01-13 16:40:13 +0100369 verdict = http_req_get_intercept_rule(px, &px->http_req_rules, s);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200370
371 switch (verdict) {
372 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
373 goto return_prx_yield;
374
375 case HTTP_RULE_RES_CONT:
376 case HTTP_RULE_RES_STOP: /* nothing to do */
377 break;
378
379 case HTTP_RULE_RES_DENY: /* deny or tarpit */
380 if (txn->flags & TX_CLTARPIT)
381 goto tarpit;
382 goto deny;
383
384 case HTTP_RULE_RES_ABRT: /* abort request, response already sent. Eg: auth */
385 goto return_prx_cond;
386
387 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
388 goto done;
389
390 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
391 goto return_bad_req;
Christopher Faulet3a26bee2019-12-16 12:47:40 +0100392
393 case HTTP_RULE_RES_ERROR: /* failed with a bad request */
394 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200395 }
396 }
397
398 if (conn && (conn->flags & CO_FL_EARLY_DATA) &&
Olivier Houchard220a26c2020-01-23 14:57:36 +0100399 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_SSL_WAIT_HS))) {
Christopher Fauletff2759f2018-10-24 11:13:16 +0200400 struct http_hdr_ctx ctx;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200401
Christopher Fauletff2759f2018-10-24 11:13:16 +0200402 ctx.blk = NULL;
403 if (!http_find_header(htx, ist("Early-Data"), &ctx, 0)) {
404 if (unlikely(!http_add_header(htx, ist("Early-Data"), ist("1"))))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100405 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200406 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200407 }
408
409 /* OK at this stage, we know that the request was accepted according to
410 * the http-request rules, we can check for the stats. Note that the
411 * URI is detected *before* the req* rules in order not to be affected
412 * by a possible reqrep, while they are processed *after* so that a
413 * reqdeny can still block them. This clearly needs to change in 1.6!
414 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200415 if (!s->target && http_stats_check_uri(s, txn, px)) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200416 s->target = &http_stats_applet.obj_type;
Willy Tarreau14bfe9a2018-12-19 15:19:27 +0100417 if (unlikely(!si_register_handler(&s->si[1], objt_applet(s->target)))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200418 s->logs.tv_request = now;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200419 if (!(s->flags & SF_ERR_MASK))
420 s->flags |= SF_ERR_RESOURCE;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100421 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200422 }
423
424 /* parse the whole stats request and extract the relevant information */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200425 http_handle_stats(s, req);
Christopher Fauletb58f62b2020-01-13 16:40:13 +0100426 verdict = http_req_get_intercept_rule(px, &px->uri_auth->http_req_rules, s);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200427 /* not all actions implemented: deny, allow, auth */
428
429 if (verdict == HTTP_RULE_RES_DENY) /* stats http-request deny */
430 goto deny;
431
432 if (verdict == HTTP_RULE_RES_ABRT) /* stats auth / stats http-request auth */
433 goto return_prx_cond;
Christopher Faulet3a26bee2019-12-16 12:47:40 +0100434
435 if (verdict == HTTP_RULE_RES_BADREQ) /* failed with a bad request */
436 goto return_bad_req;
437
438 if (verdict == HTTP_RULE_RES_ERROR) /* failed with a bad request */
439 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200440 }
441
Christopher Faulet2571bc62019-03-01 11:44:26 +0100442 /* Proceed with the applets now. */
443 if (unlikely(objt_applet(s->target))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200444 if (sess->fe == s->be) /* report it if the request was intercepted by the frontend */
Olivier Houcharda798bf52019-03-08 18:52:00 +0100445 _HA_ATOMIC_ADD(&sess->fe->fe_counters.intercepted_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200446
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200447 if (http_handle_expect_hdr(s, htx, msg) == -1)
Christopher Fauletb8a53712019-12-16 11:29:38 +0100448 goto return_int_err;
Christopher Fauletbcf242a2019-03-01 11:36:26 +0100449
Christopher Faulete0768eb2018-10-03 16:38:02 +0200450 if (!(s->flags & SF_ERR_MASK)) // this is not really an error but it is
451 s->flags |= SF_ERR_LOCAL; // to mark that it comes from the proxy
452 if (!(s->flags & SF_FINST_MASK))
453 s->flags |= SF_FINST_R;
454
455 /* enable the minimally required analyzers to handle keep-alive and compression on the HTTP response */
456 req->analysers &= (AN_REQ_HTTP_BODY | AN_REQ_FLT_HTTP_HDRS | AN_REQ_FLT_END);
457 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
458 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Christopher Fauletbcf242a2019-03-01 11:36:26 +0100459
460 req->flags |= CF_SEND_DONTWAIT;
461 s->flags |= SF_ASSIGNED;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200462 goto done;
463 }
464
465 /* check whether we have some ACLs set to redirect this request */
466 list_for_each_entry(rule, &px->redirect_rules, list) {
467 if (rule->cond) {
468 int ret;
469
470 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
471 ret = acl_pass(ret);
472 if (rule->cond->pol == ACL_COND_UNLESS)
473 ret = !ret;
474 if (!ret)
475 continue;
476 }
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200477 if (!http_apply_redirect_rule(rule, s, txn))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100478 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200479 goto done;
480 }
481
482 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
483 * If this happens, then the data will not come immediately, so we must
484 * send all what we have without waiting. Note that due to the small gain
485 * in waiting for the body of the request, it's easier to simply put the
486 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
487 * itself once used.
488 */
489 req->flags |= CF_SEND_DONTWAIT;
490
491 done: /* done with this analyser, continue with next ones that the calling
492 * points will have set, if any.
493 */
494 req->analyse_exp = TICK_ETERNITY;
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +0500495 done_without_exp: /* done with this analyser, but don't reset the analyse_exp. */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200496 req->analysers &= ~an_bit;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100497 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200498 return 1;
499
500 tarpit:
501 /* Allow cookie logging
502 */
503 if (s->be->cookie_name || sess->fe->capture_name)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200504 http_manage_client_side_cookies(s, req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200505
506 /* When a connection is tarpitted, we use the tarpit timeout,
507 * which may be the same as the connect timeout if unspecified.
508 * If unset, then set it to zero because we really want it to
509 * eventually expire. We build the tarpit as an analyser.
510 */
Christopher Faulet202c6ce2019-01-07 14:57:35 +0100511 channel_htx_erase(&s->req, htx);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200512
513 /* wipe the request out so that we can drop the connection early
514 * if the client closes first.
515 */
516 channel_dont_connect(req);
517
Christopher Faulete0768eb2018-10-03 16:38:02 +0200518 req->analysers &= AN_REQ_FLT_END; /* remove switching rules etc... */
519 req->analysers |= AN_REQ_HTTP_TARPIT;
520 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
521 if (!req->analyse_exp)
522 req->analyse_exp = tick_add(now_ms, 0);
523 stream_inc_http_err_ctr(s);
Olivier Houcharda798bf52019-03-08 18:52:00 +0100524 _HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100525 if (s->flags & SF_BE_ASSIGNED)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100526 _HA_ATOMIC_ADD(&s->be->be_counters.denied_req, 1);
William Lallemand36119de2021-03-08 15:26:48 +0100527 if (sess->listener && sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100528 _HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200529 goto done_without_exp;
530
531 deny: /* this request was blocked (denied) */
532
533 /* Allow cookie logging
534 */
535 if (s->be->cookie_name || sess->fe->capture_name)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200536 http_manage_client_side_cookies(s, req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200537
Christopher Faulete0768eb2018-10-03 16:38:02 +0200538 s->logs.tv_request = now;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200539 stream_inc_http_err_ctr(s);
Olivier Houcharda798bf52019-03-08 18:52:00 +0100540 _HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100541 if (s->flags & SF_BE_ASSIGNED)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100542 _HA_ATOMIC_ADD(&s->be->be_counters.denied_req, 1);
William Lallemand36119de2021-03-08 15:26:48 +0100543 if (sess->listener && sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100544 _HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100545 goto return_prx_err;
546
547 return_int_err:
548 txn->status = 500;
549 if (!(s->flags & SF_ERR_MASK))
550 s->flags |= SF_ERR_INTERNAL;
551 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100552 if (s->flags & SF_BE_ASSIGNED)
553 _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1);
William Lallemand36119de2021-03-08 15:26:48 +0100554 if (sess->listener && sess->listener->counters)
Christopher Fauletb8a53712019-12-16 11:29:38 +0100555 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
556 goto return_prx_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200557
558 return_bad_req:
Christopher Faulete0768eb2018-10-03 16:38:02 +0200559 txn->status = 400;
Olivier Houcharda798bf52019-03-08 18:52:00 +0100560 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
William Lallemand36119de2021-03-08 15:26:48 +0100561 if (sess->listener && sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100562 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100563 /* fall through */
564
565 return_prx_err:
566 http_reply_and_close(s, txn->status, http_error_message(s));
567 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200568
569 return_prx_cond:
570 if (!(s->flags & SF_ERR_MASK))
571 s->flags |= SF_ERR_PRXCOND;
572 if (!(s->flags & SF_FINST_MASK))
573 s->flags |= SF_FINST_R;
574
575 req->analysers &= AN_REQ_FLT_END;
576 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100577 DBG_TRACE_DEVEL("leaving on error",
578 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200579 return 0;
580
581 return_prx_yield:
582 channel_dont_connect(req);
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100583 DBG_TRACE_DEVEL("waiting for more data",
584 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200585 return 0;
586}
587
588/* This function performs all the processing enabled for the current request.
589 * It returns 1 if the processing can continue on next analysers, or zero if it
590 * needs more data, encounters an error, or wants to immediately abort the
591 * request. It relies on buffers flags, and updates s->req.analysers.
592 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200593int http_process_request(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200594{
595 struct session *sess = s->sess;
596 struct http_txn *txn = s->txn;
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200597 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200598 struct connection *cli_conn = objt_conn(strm_sess(s)->origin);
599
Christopher Faulet8bebd2f2020-10-06 17:54:56 +0200600 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200601
602 /*
603 * Right now, we know that we have processed the entire headers
604 * and that unwanted requests have been filtered out. We can do
605 * whatever we want with the remaining request. Also, now we
606 * may have separate values for ->fe, ->be.
607 */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100608 htx = htxbuf(&req->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200609
610 /*
611 * If HTTP PROXY is set we simply get remote server address parsing
Willy Tarreau1c8d32b2019-07-18 15:47:45 +0200612 * incoming request.
Christopher Faulete0768eb2018-10-03 16:38:02 +0200613 */
614 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SF_ADDR_SET)) {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100615 struct htx_sl *sl;
616 struct ist uri, path;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200617
Willy Tarreau9b7587a2020-10-15 07:32:10 +0200618 if (!sockaddr_alloc(&s->target_addr, NULL, 0)) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200619 if (!(s->flags & SF_ERR_MASK))
620 s->flags |= SF_ERR_RESOURCE;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100621 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200622 }
Christopher Faulet297fbb42019-05-13 14:41:27 +0200623 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100624 uri = htx_sl_req_uri(sl);
625 path = http_get_path(uri);
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200626
Willy Tarreau1c8d32b2019-07-18 15:47:45 +0200627 if (url2sa(uri.ptr, uri.len - path.len, s->target_addr, NULL) == -1)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200628 goto return_bad_req;
629
Willy Tarreau1c8d32b2019-07-18 15:47:45 +0200630 s->target = &s->be->obj_type;
631 s->flags |= SF_ADDR_SET | SF_ASSIGNED;
632
Christopher Faulete0768eb2018-10-03 16:38:02 +0200633 /* if the path was found, we have to remove everything between
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200634 * uri.ptr and path.ptr (excluded). If it was not found, we need
635 * to replace from all the uri by a single "/".
636 *
Ilya Shipitsin46a030c2020-07-05 16:36:08 +0500637 * Instead of rewriting the whole start line, we just update
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100638 * the star-line URI. Some space will be lost but it should be
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200639 * insignificant.
Christopher Faulete0768eb2018-10-03 16:38:02 +0200640 */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100641 istcpy(&uri, (path.len ? path : ist("/")), uri.len);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200642 }
643
644 /*
645 * 7: Now we can work with the cookies.
646 * Note that doing so might move headers in the request, but
647 * the fields will stay coherent and the URI will not move.
648 * This should only be performed in the backend.
649 */
650 if (s->be->cookie_name || sess->fe->capture_name)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200651 http_manage_client_side_cookies(s, req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200652
Tim Duesterhus2825b4b2020-02-28 15:13:34 +0100653 /* 8: Generate unique ID if a "unique-id-format" is defined.
654 *
655 * A unique ID is generated even when it is not sent to ensure that the ID can make use of
656 * fetches only available in the HTTP request processing stage.
657 */
658 if (!LIST_ISEMPTY(&sess->fe->format_unique_id)) {
Tim Duesterhusa17e6622020-03-05 20:19:02 +0100659 struct ist unique_id = stream_generate_unique_id(s, &sess->fe->format_unique_id);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200660
Tim Duesterhusa17e6622020-03-05 20:19:02 +0100661 if (!isttest(unique_id)) {
Christopher Fauletb8a53712019-12-16 11:29:38 +0100662 if (!(s->flags & SF_ERR_MASK))
663 s->flags |= SF_ERR_RESOURCE;
664 goto return_int_err;
665 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200666
Tim Duesterhus2825b4b2020-02-28 15:13:34 +0100667 /* send unique ID if a "unique-id-header" is defined */
Tim Duesterhus0643b0e2020-03-05 17:56:35 +0100668 if (isttest(sess->fe->header_unique_id) &&
Tim Duesterhusa17e6622020-03-05 20:19:02 +0100669 unlikely(!http_add_header(htx, sess->fe->header_unique_id, s->unique_id)))
Tim Duesterhus2825b4b2020-02-28 15:13:34 +0100670 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200671 }
672
673 /*
674 * 9: add X-Forwarded-For if either the frontend or the backend
675 * asks for it.
676 */
677 if ((sess->fe->options | s->be->options) & PR_O_FWDFOR) {
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200678 struct http_hdr_ctx ctx = { .blk = NULL };
679 struct ist hdr = ist2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : sess->fe->fwdfor_hdr_name,
680 s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : sess->fe->fwdfor_hdr_len);
681
Christopher Faulete0768eb2018-10-03 16:38:02 +0200682 if (!((sess->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200683 http_find_header(htx, hdr, &ctx, 0)) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200684 /* The header is set to be added only if none is present
685 * and we found it, so don't do anything.
686 */
687 }
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200688 else if (cli_conn && conn_get_src(cli_conn) && cli_conn->src->ss_family == AF_INET) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200689 /* Add an X-Forwarded-For header unless the source IP is
690 * in the 'except' network range.
691 */
Christopher Faulet5d1def62021-02-26 09:19:15 +0100692 if (ipcmp2net(cli_conn->src, &sess->fe->except_xff_net) &&
693 ipcmp2net(cli_conn->src, &s->be->except_xff_net)) {
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200694 unsigned char *pn = (unsigned char *)&((struct sockaddr_in *)cli_conn->src)->sin_addr;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200695
696 /* Note: we rely on the backend to get the header name to be used for
697 * x-forwarded-for, because the header is really meant for the backends.
698 * However, if the backend did not specify any option, we have to rely
699 * on the frontend's header name.
700 */
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200701 chunk_printf(&trash, "%d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
702 if (unlikely(!http_add_header(htx, hdr, ist2(trash.area, trash.data))))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100703 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200704 }
705 }
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200706 else if (cli_conn && conn_get_src(cli_conn) && cli_conn->src->ss_family == AF_INET6) {
Christopher Faulet5d1def62021-02-26 09:19:15 +0100707 /* Add an X-Forwarded-For header unless the source IP is
708 * in the 'except' network range.
Christopher Faulete0768eb2018-10-03 16:38:02 +0200709 */
Christopher Faulet5d1def62021-02-26 09:19:15 +0100710 if (ipcmp2net(cli_conn->src, &sess->fe->except_xff_net) &&
711 ipcmp2net(cli_conn->src, &s->be->except_xff_net)) {
712 char pn[INET6_ADDRSTRLEN];
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200713
Christopher Faulet5d1def62021-02-26 09:19:15 +0100714 inet_ntop(AF_INET6,
715 (const void *)&((struct sockaddr_in6 *)(cli_conn->src))->sin6_addr,
716 pn, sizeof(pn));
Christopher Faulete0768eb2018-10-03 16:38:02 +0200717
Christopher Faulet5d1def62021-02-26 09:19:15 +0100718 /* Note: we rely on the backend to get the header name to be used for
719 * x-forwarded-for, because the header is really meant for the backends.
720 * However, if the backend did not specify any option, we have to rely
721 * on the frontend's header name.
722 */
723 chunk_printf(&trash, "%s", pn);
724 if (unlikely(!http_add_header(htx, hdr, ist2(trash.area, trash.data))))
725 goto return_int_err;
726 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200727 }
728 }
729
730 /*
731 * 10: add X-Original-To if either the frontend or the backend
732 * asks for it.
733 */
734 if ((sess->fe->options | s->be->options) & PR_O_ORGTO) {
Christopher Faulet5d1def62021-02-26 09:19:15 +0100735 struct ist hdr = ist2(s->be->orgto_hdr_len ? s->be->orgto_hdr_name : sess->fe->orgto_hdr_name,
736 s->be->orgto_hdr_len ? s->be->orgto_hdr_len : sess->fe->orgto_hdr_len);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200737
Christopher Fauletcccded92021-02-26 12:45:56 +0100738 if (cli_conn && conn_get_dst(cli_conn) && cli_conn->dst->ss_family == AF_INET) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200739 /* Add an X-Original-To header unless the destination IP is
740 * in the 'except' network range.
741 */
Christopher Faulet5d1def62021-02-26 09:19:15 +0100742 if (ipcmp2net(cli_conn->dst, &sess->fe->except_xot_net) &&
743 ipcmp2net(cli_conn->dst, &s->be->except_xot_net)) {
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200744 unsigned char *pn = (unsigned char *)&((struct sockaddr_in *)cli_conn->dst)->sin_addr;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200745
746 /* Note: we rely on the backend to get the header name to be used for
747 * x-original-to, because the header is really meant for the backends.
748 * However, if the backend did not specify any option, we have to rely
749 * on the frontend's header name.
750 */
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200751 chunk_printf(&trash, "%d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
752 if (unlikely(!http_add_header(htx, hdr, ist2(trash.area, trash.data))))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100753 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200754 }
755 }
Christopher Faulet5d1def62021-02-26 09:19:15 +0100756 else if (cli_conn && conn_get_dst(cli_conn) && cli_conn->dst->ss_family == AF_INET6) {
757 /* Add an X-Original-To header unless the source IP is
758 * in the 'except' network range.
759 */
760 if (ipcmp2net(cli_conn->dst, &sess->fe->except_xot_net) &&
761 ipcmp2net(cli_conn->dst, &s->be->except_xot_net)) {
762 char pn[INET6_ADDRSTRLEN];
763
764 inet_ntop(AF_INET6,
765 (const void *)&((struct sockaddr_in6 *)(cli_conn->dst))->sin6_addr,
766 pn, sizeof(pn));
767
768 /* Note: we rely on the backend to get the header name to be used for
769 * x-forwarded-for, because the header is really meant for the backends.
770 * However, if the backend did not specify any option, we have to rely
771 * on the frontend's header name.
772 */
773 chunk_printf(&trash, "%s", pn);
774 if (unlikely(!http_add_header(htx, hdr, ist2(trash.area, trash.data))))
775 goto return_int_err;
776 }
777 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200778 }
779
Christopher Faulete0768eb2018-10-03 16:38:02 +0200780 /* If we have no server assigned yet and we're balancing on url_param
781 * with a POST request, we may be interested in checking the body for
782 * that parameter. This will be done in another analyser.
783 */
784 if (!(s->flags & (SF_ASSIGNED|SF_DIRECT)) &&
Willy Tarreau089eaa02019-01-14 15:17:46 +0100785 s->txn->meth == HTTP_METH_POST &&
786 (s->be->lbprm.algo & BE_LB_ALGO) == BE_LB_ALGO_PH) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200787 channel_dont_connect(req);
788 req->analysers |= AN_REQ_HTTP_BODY;
789 }
790
791 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
792 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau1a18b542018-12-11 16:37:42 +0100793
Christopher Faulete0768eb2018-10-03 16:38:02 +0200794 /* We expect some data from the client. Unless we know for sure
795 * we already have a full request, we have to re-enable quick-ack
796 * in case we previously disabled it, otherwise we might cause
797 * the client to delay further data.
798 */
William Lallemand36119de2021-03-08 15:26:48 +0100799 if ((sess->listener && (sess->listener->options & LI_O_NOQUICKACK)) && !(htx->flags & HTX_FL_EOM))
Willy Tarreau1a18b542018-12-11 16:37:42 +0100800 conn_set_quickack(cli_conn, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200801
802 /*************************************************************
803 * OK, that's finished for the headers. We have done what we *
804 * could. Let's switch to the DATA state. *
805 ************************************************************/
806 req->analyse_exp = TICK_ETERNITY;
807 req->analysers &= ~an_bit;
808
809 s->logs.tv_request = now;
810 /* OK let's go on with the BODY now */
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100811 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200812 return 1;
813
Christopher Fauletb8a53712019-12-16 11:29:38 +0100814 return_int_err:
815 txn->status = 500;
816 if (!(s->flags & SF_ERR_MASK))
817 s->flags |= SF_ERR_INTERNAL;
818 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100819 if (s->flags & SF_BE_ASSIGNED)
Christopher Fauletbe20cf32020-01-24 11:41:38 +0100820 _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1);
William Lallemand36119de2021-03-08 15:26:48 +0100821 if (sess->listener && sess->listener->counters)
Christopher Fauletb8a53712019-12-16 11:29:38 +0100822 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
823 goto return_prx_cond;
824
Christopher Faulete0768eb2018-10-03 16:38:02 +0200825 return_bad_req: /* let's centralize all bad requests */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200826 txn->status = 400;
Olivier Houcharda798bf52019-03-08 18:52:00 +0100827 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
William Lallemand36119de2021-03-08 15:26:48 +0100828 if (sess->listener && sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100829 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100830 /* fall through */
831
832 return_prx_cond:
833 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +0200834
835 if (!(s->flags & SF_ERR_MASK))
836 s->flags |= SF_ERR_PRXCOND;
837 if (!(s->flags & SF_FINST_MASK))
838 s->flags |= SF_FINST_R;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100839
840 req->analysers &= AN_REQ_FLT_END;
841 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100842 DBG_TRACE_DEVEL("leaving on error",
843 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200844 return 0;
845}
846
847/* This function is an analyser which processes the HTTP tarpit. It always
848 * returns zero, at the beginning because it prevents any other processing
849 * from occurring, and at the end because it terminates the request.
850 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200851int http_process_tarpit(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200852{
853 struct http_txn *txn = s->txn;
854
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100855 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, &txn->req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200856 /* This connection is being tarpitted. The CLIENT side has
857 * already set the connect expiration date to the right
858 * timeout. We just have to check that the client is still
859 * there and that the timeout has not expired.
860 */
861 channel_dont_connect(req);
862 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100863 !tick_is_expired(req->analyse_exp, now_ms)) {
864 DBG_TRACE_DEVEL("waiting for tarpit timeout expiry",
865 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200866 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100867 }
868
Christopher Faulete0768eb2018-10-03 16:38:02 +0200869
870 /* We will set the queue timer to the time spent, just for
871 * logging purposes. We fake a 500 server error, so that the
872 * attacker will not suspect his connection has been tarpitted.
873 * It will not cause trouble to the logs because we can exclude
874 * the tarpitted connections by filtering on the 'PT' status flags.
875 */
876 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
877
Christopher Faulet8dfeccf2020-05-15 14:16:29 +0200878 http_reply_and_close(s, txn->status, (!(req->flags & CF_READ_ERROR) ? http_error_message(s) : NULL));
Christopher Faulet5cb513a2020-05-13 17:56:56 +0200879
880 end:
Christopher Faulete0768eb2018-10-03 16:38:02 +0200881 req->analysers &= AN_REQ_FLT_END;
882 req->analyse_exp = TICK_ETERNITY;
883
884 if (!(s->flags & SF_ERR_MASK))
885 s->flags |= SF_ERR_PRXCOND;
886 if (!(s->flags & SF_FINST_MASK))
887 s->flags |= SF_FINST_T;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100888
889 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200890 return 0;
891}
892
893/* This function is an analyser which waits for the HTTP request body. It waits
894 * for either the buffer to be full, or the full advertised contents to have
895 * reached the buffer. It must only be called after the standard HTTP request
896 * processing has occurred, because it expects the request to be parsed and will
897 * look for the Expect header. It may send a 100-Continue interim response. It
898 * takes in input any state starting from HTTP_MSG_BODY and leaves with one of
899 * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it
900 * needs to read more data, or 1 once it has completed its analysis.
901 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200902int http_wait_for_request_body(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200903{
904 struct session *sess = s->sess;
905 struct http_txn *txn = s->txn;
906 struct http_msg *msg = &s->txn->req;
Christopher Fauletf76ebe82018-10-24 11:16:22 +0200907 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200908
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100909 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Fauletf76ebe82018-10-24 11:16:22 +0200910
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100911 htx = htxbuf(&req->buf);
Christopher Fauletf76ebe82018-10-24 11:16:22 +0200912
Willy Tarreau4236f032019-03-05 10:43:32 +0100913 if (htx->flags & HTX_FL_PARSING_ERROR)
914 goto return_bad_req;
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200915 if (htx->flags & HTX_FL_PROCESSING_ERROR)
916 goto return_int_err;
Willy Tarreau4236f032019-03-05 10:43:32 +0100917
Christopher Fauletc31bc722020-11-20 14:30:38 +0100918 /* Do nothing for bodyless and CONNECT requests */
919 if (txn->meth == HTTP_METH_CONNECT || (msg->flags & HTTP_MSGF_BODYLESS))
Christopher Faulet63c69a92020-11-16 16:03:35 +0100920 goto http_end;
921
Christopher Faulete0768eb2018-10-03 16:38:02 +0200922 /* We have to parse the HTTP request body to find any required data.
923 * "balance url_param check_post" should have been the only way to get
924 * into this. We were brought here after HTTP header analysis, so all
925 * related structures are ready.
926 */
927
Christopher Fauletf76ebe82018-10-24 11:16:22 +0200928 if (msg->msg_state < HTTP_MSG_DATA) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200929 if (http_handle_expect_hdr(s, htx, msg) == -1)
Christopher Fauletb8a53712019-12-16 11:29:38 +0100930 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200931 }
932
Christopher Fauletf76ebe82018-10-24 11:16:22 +0200933 msg->msg_state = HTTP_MSG_DATA;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200934
Christopher Fauletf76ebe82018-10-24 11:16:22 +0200935 /* Now we're in HTTP_MSG_DATA. We just need to know if all data have
936 * been received or if the buffer is full.
Christopher Faulete0768eb2018-10-03 16:38:02 +0200937 */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +0100938 if ((htx->flags & HTX_FL_EOM) || htx_get_tail_type(htx) > HTX_BLK_DATA ||
Christopher Fauletdcd8c5e2019-01-21 11:24:38 +0100939 channel_htx_full(req, htx, global.tune.maxrewrite))
Christopher Faulete0768eb2018-10-03 16:38:02 +0200940 goto http_end;
941
942 if ((req->flags & CF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
943 txn->status = 408;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200944 if (!(s->flags & SF_ERR_MASK))
945 s->flags |= SF_ERR_CLITO;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100946 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
William Lallemand36119de2021-03-08 15:26:48 +0100947 if (sess->listener && sess->listener->counters)
Christopher Fauletb8a53712019-12-16 11:29:38 +0100948 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
949 goto return_prx_cond;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200950 }
951
952 /* we get here if we need to wait for more data */
953 if (!(req->flags & (CF_SHUTR | CF_READ_ERROR))) {
954 /* Not enough data. We'll re-use the http-request
955 * timeout here. Ideally, we should set the timeout
956 * relative to the accept() date. We just set the
957 * request timeout once at the beginning of the
958 * request.
959 */
960 channel_dont_connect(req);
961 if (!tick_isset(req->analyse_exp))
962 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100963 DBG_TRACE_DEVEL("waiting for more data",
964 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200965 return 0;
966 }
967
968 http_end:
969 /* The situation will not evolve, so let's give up on the analysis. */
970 s->logs.tv_request = now; /* update the request timer to reflect full request */
971 req->analysers &= ~an_bit;
972 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100973 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200974 return 1;
975
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200976 return_int_err:
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200977 txn->status = 500;
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200978 if (!(s->flags & SF_ERR_MASK))
979 s->flags |= SF_ERR_INTERNAL;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100980 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100981 if (s->flags & SF_BE_ASSIGNED)
Christopher Fauletbe20cf32020-01-24 11:41:38 +0100982 _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1);
William Lallemand36119de2021-03-08 15:26:48 +0100983 if (sess->listener && sess->listener->counters)
Christopher Fauletb8a53712019-12-16 11:29:38 +0100984 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
985 goto return_prx_cond;
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200986
Christopher Faulete0768eb2018-10-03 16:38:02 +0200987 return_bad_req: /* let's centralize all bad requests */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200988 txn->status = 400;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100989 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
William Lallemand36119de2021-03-08 15:26:48 +0100990 if (sess->listener && sess->listener->counters)
Christopher Fauletb8a53712019-12-16 11:29:38 +0100991 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
992 /* fall through */
993
994 return_prx_cond:
995 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +0200996
997 if (!(s->flags & SF_ERR_MASK))
998 s->flags |= SF_ERR_PRXCOND;
999 if (!(s->flags & SF_FINST_MASK))
Christopher Fauletb8a53712019-12-16 11:29:38 +01001000 s->flags |= (msg->msg_state < HTTP_MSG_DATA ? SF_FINST_R : SF_FINST_D);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001001
Christopher Faulete0768eb2018-10-03 16:38:02 +02001002 req->analysers &= AN_REQ_FLT_END;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001003 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001004 DBG_TRACE_DEVEL("leaving on error",
1005 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001006 return 0;
1007}
1008
1009/* This function is an analyser which forwards request body (including chunk
1010 * sizes if any). It is called as soon as we must forward, even if we forward
1011 * zero byte. The only situation where it must not be called is when we're in
1012 * tunnel mode and we want to forward till the close. It's used both to forward
1013 * remaining data and to resync after end of body. It expects the msg_state to
1014 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
1015 * read more data, or 1 once we can go on with next request or end the stream.
1016 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
1017 * bytes of pending data + the headers if not already done.
1018 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001019int http_request_forward_body(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001020{
1021 struct session *sess = s->sess;
1022 struct http_txn *txn = s->txn;
Christopher Faulet9768c262018-10-22 09:34:31 +02001023 struct http_msg *msg = &txn->req;
1024 struct htx *htx;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001025 short status = 0;
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001026 int ret;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001027
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001028 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001029
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001030 htx = htxbuf(&req->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001031
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001032 if (htx->flags & HTX_FL_PARSING_ERROR)
1033 goto return_bad_req;
1034 if (htx->flags & HTX_FL_PROCESSING_ERROR)
1035 goto return_int_err;
1036
Christopher Faulete0768eb2018-10-03 16:38:02 +02001037 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
1038 ((req->flags & CF_SHUTW) && (req->to_forward || co_data(req)))) {
1039 /* Output closed while we were sending data. We must abort and
1040 * wake the other side up.
1041 */
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001042
Olivier Houchard29cac3c2019-07-12 15:48:58 +02001043 /* Don't abort yet if we had L7 retries activated and it
1044 * was a write error, we may recover.
1045 */
1046 if (!(req->flags & (CF_READ_ERROR | CF_READ_TIMEOUT)) &&
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001047 (s->si[1].flags & SI_FL_L7_RETRY)) {
1048 DBG_TRACE_DEVEL("leaving on L7 retry",
1049 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Olivier Houchard29cac3c2019-07-12 15:48:58 +02001050 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001051 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001052 msg->msg_state = HTTP_MSG_ERROR;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001053 http_end_request(s);
1054 http_end_response(s);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001055 DBG_TRACE_DEVEL("leaving on error",
1056 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001057 return 1;
1058 }
1059
1060 /* Note that we don't have to send 100-continue back because we don't
1061 * need the data to complete our job, and it's up to the server to
1062 * decide whether to return 100, 417 or anything else in return of
1063 * an "Expect: 100-continue" header.
1064 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001065 if (msg->msg_state == HTTP_MSG_BODY)
1066 msg->msg_state = HTTP_MSG_DATA;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001067
Christopher Faulete0768eb2018-10-03 16:38:02 +02001068 /* in most states, we should abort in case of early close */
1069 channel_auto_close(req);
1070
1071 if (req->to_forward) {
Christopher Faulet66af0b22019-03-22 14:54:52 +01001072 if (req->to_forward == CHN_INFINITE_FORWARD) {
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001073 if (req->flags & CF_EOI)
1074 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet66af0b22019-03-22 14:54:52 +01001075 }
1076 else {
1077 /* We can't process the buffer's contents yet */
1078 req->flags |= CF_WAKE_WRITE;
1079 goto missing_data_or_waiting;
1080 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001081 }
1082
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001083 if (msg->msg_state >= HTTP_MSG_ENDING)
1084 goto ending;
1085
1086 if (txn->meth == HTTP_METH_CONNECT) {
1087 msg->msg_state = HTTP_MSG_ENDING;
1088 goto ending;
1089 }
1090
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001091 /* Forward input data. We get it by removing all outgoing data not
1092 * forwarded yet from HTX data size. If there are some data filters, we
1093 * let them decide the amount of data to forward.
Christopher Faulet9768c262018-10-22 09:34:31 +02001094 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001095 if (HAS_REQ_DATA_FILTERS(s)) {
1096 ret = flt_http_payload(s, msg, htx->data);
1097 if (ret < 0)
1098 goto return_bad_req;
Christopher Faulet421e7692019-06-13 11:16:45 +02001099 c_adv(req, ret);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001100 }
1101 else {
Christopher Faulet421e7692019-06-13 11:16:45 +02001102 c_adv(req, htx->data - co_data(req));
Christopher Faulet66af0b22019-03-22 14:54:52 +01001103 if (msg->flags & HTTP_MSGF_XFER_LEN)
1104 channel_htx_forward_forever(req, htx);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001105 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001106
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001107 if (htx->data != co_data(req))
1108 goto missing_data_or_waiting;
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001109
Christopher Faulet9768c262018-10-22 09:34:31 +02001110 /* Check if the end-of-message is reached and if so, switch the message
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001111 * in HTTP_MSG_ENDING state. Then if all data was marked to be
1112 * forwarded, set the state to HTTP_MSG_DONE.
Christopher Faulet9768c262018-10-22 09:34:31 +02001113 */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001114 if (!(htx->flags & HTX_FL_EOM))
Christopher Faulet9768c262018-10-22 09:34:31 +02001115 goto missing_data_or_waiting;
1116
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001117 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet9768c262018-10-22 09:34:31 +02001118
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001119 ending:
Christopher Faulet2151cdd2020-07-22 16:34:59 +02001120 req->flags &= ~CF_EXPECT_MORE; /* no more data are expected */
1121
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001122 /* other states, ENDING...TUNNEL */
1123 if (msg->msg_state >= HTTP_MSG_DONE)
1124 goto done;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001125
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001126 if (HAS_REQ_DATA_FILTERS(s)) {
1127 ret = flt_http_end(s, msg);
1128 if (ret <= 0) {
1129 if (!ret)
1130 goto missing_data_or_waiting;
1131 goto return_bad_req;
1132 }
1133 }
1134
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001135 if (txn->meth == HTTP_METH_CONNECT)
1136 msg->msg_state = HTTP_MSG_TUNNEL;
1137 else {
1138 msg->msg_state = HTTP_MSG_DONE;
1139 req->to_forward = 0;
Christopher Faulet5b82cc52020-10-12 15:18:50 +02001140
1141 if ((s->be->retry_type &~ PR_RE_CONN_FAILED) && !(s->si[1].flags & SI_FL_D_L7_RETRY)) {
1142 struct stream_interface *si = &s->si[1];
1143
1144 /* If we want to be able to do L7 retries, copy the
1145 * request, so that we are able to resend them if
1146 * needed.
1147 *
1148 * Try to allocate a buffer if we had none. If it
1149 * fails, the next test will just disable the l7
1150 * retries.
1151 */
1152 DBG_TRACE_STATE("enable L7 retry, save the request", STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
1153 si->flags |= SI_FL_L7_RETRY;
Willy Tarreauf499f502021-03-22 16:17:37 +01001154 if (b_alloc(&si->l7_buffer) == NULL)
Christopher Faulet5b82cc52020-10-12 15:18:50 +02001155 si->flags &= ~SI_FL_L7_RETRY;
1156 else {
1157 memcpy(b_orig(&si->l7_buffer), b_orig(&req->buf), b_size(&req->buf));
1158 b_add(&si->l7_buffer, co_data(req));
1159 }
1160 }
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001161 }
1162
1163 done:
1164 /* we don't want to forward closes on DONE except in tunnel mode. */
1165 if (!(txn->flags & TX_CON_WANT_TUN))
1166 channel_dont_close(req);
1167
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001168 http_end_request(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001169 if (!(req->analysers & an_bit)) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001170 http_end_response(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001171 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
1172 if (req->flags & CF_SHUTW) {
1173 /* request errors are most likely due to the
1174 * server aborting the transfer. */
Christopher Faulet93e02d82019-03-08 14:18:50 +01001175 goto return_srv_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001176 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001177 goto return_bad_req;
1178 }
Christopher Faulet5b82cc52020-10-12 15:18:50 +02001179
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001180 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001181 return 1;
1182 }
1183
1184 /* If "option abortonclose" is set on the backend, we want to monitor
1185 * the client's connection and forward any shutdown notification to the
1186 * server, which will decide whether to close or to go on processing the
1187 * request. We only do that in tunnel mode, and not in other modes since
1188 * it can be abused to exhaust source ports. */
Christopher Faulet769d0e92019-03-22 14:23:18 +01001189 if (s->be->options & PR_O_ABRT_CLOSE) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001190 channel_auto_read(req);
Christopher Fauletc41547b2019-07-16 14:32:23 +02001191 if ((req->flags & (CF_SHUTR|CF_READ_NULL)) && !(txn->flags & TX_CON_WANT_TUN))
Christopher Faulete0768eb2018-10-03 16:38:02 +02001192 s->si[1].flags |= SI_FL_NOLINGER;
1193 channel_auto_close(req);
1194 }
1195 else if (s->txn->meth == HTTP_METH_POST) {
1196 /* POST requests may require to read extra CRLF sent by broken
1197 * browsers and which could cause an RST to be sent upon close
1198 * on some systems (eg: Linux). */
1199 channel_auto_read(req);
1200 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001201 DBG_TRACE_DEVEL("waiting for the end of the HTTP txn",
1202 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001203 return 0;
1204
1205 missing_data_or_waiting:
1206 /* stop waiting for data if the input is closed before the end */
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001207 if (msg->msg_state < HTTP_MSG_ENDING && req->flags & CF_SHUTR)
Christopher Faulet93e02d82019-03-08 14:18:50 +01001208 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001209
1210 waiting:
1211 /* waiting for the last bits to leave the buffer */
1212 if (req->flags & CF_SHUTW)
Christopher Faulet93e02d82019-03-08 14:18:50 +01001213 goto return_srv_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001214
1215 /* When TE: chunked is used, we need to get there again to parse remaining
1216 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
1217 * And when content-length is used, we never want to let the possible
1218 * shutdown be forwarded to the other side, as the state machine will
1219 * take care of it once the client responds. It's also important to
1220 * prevent TIME_WAITs from accumulating on the backend side, and for
1221 * HTTP/2 where the last frame comes with a shutdown.
1222 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001223 if (msg->flags & HTTP_MSGF_XFER_LEN)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001224 channel_dont_close(req);
1225
1226 /* We know that more data are expected, but we couldn't send more that
1227 * what we did. So we always set the CF_EXPECT_MORE flag so that the
1228 * system knows it must not set a PUSH on this first part. Interactive
1229 * modes are already handled by the stream sock layer. We must not do
1230 * this in content-length mode because it could present the MSG_MORE
1231 * flag with the last block of forwarded data, which would cause an
1232 * additional delay to be observed by the receiver.
1233 */
Christopher Faulet2151cdd2020-07-22 16:34:59 +02001234 if (HAS_REQ_DATA_FILTERS(s))
Christopher Faulete0768eb2018-10-03 16:38:02 +02001235 req->flags |= CF_EXPECT_MORE;
1236
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001237 DBG_TRACE_DEVEL("waiting for more data to forward",
1238 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001239 return 0;
1240
Christopher Faulet93e02d82019-03-08 14:18:50 +01001241 return_cli_abort:
1242 _HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
1243 _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
William Lallemand36119de2021-03-08 15:26:48 +01001244 if (sess->listener && sess->listener->counters)
Christopher Fauletcff0f732019-12-16 16:13:44 +01001245 _HA_ATOMIC_ADD(&sess->listener->counters->cli_aborts, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001246 if (objt_server(s->target))
Christopher Fauletcff0f732019-12-16 16:13:44 +01001247 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.cli_aborts, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001248 if (!(s->flags & SF_ERR_MASK))
1249 s->flags |= SF_ERR_CLICL;
1250 status = 400;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001251 goto return_prx_cond;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001252
1253 return_srv_abort:
1254 _HA_ATOMIC_ADD(&sess->fe->fe_counters.srv_aborts, 1);
1255 _HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1);
William Lallemand36119de2021-03-08 15:26:48 +01001256 if (sess->listener && sess->listener->counters)
Christopher Fauletcff0f732019-12-16 16:13:44 +01001257 _HA_ATOMIC_ADD(&sess->listener->counters->srv_aborts, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001258 if (objt_server(s->target))
Christopher Fauletcff0f732019-12-16 16:13:44 +01001259 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.srv_aborts, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001260 if (!(s->flags & SF_ERR_MASK))
1261 s->flags |= SF_ERR_SRVCL;
1262 status = 502;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001263 goto return_prx_cond;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001264
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001265 return_int_err:
1266 if (!(s->flags & SF_ERR_MASK))
1267 s->flags |= SF_ERR_INTERNAL;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001268 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001269 _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1);
William Lallemand36119de2021-03-08 15:26:48 +01001270 if (sess->listener && sess->listener->counters)
Christopher Fauletb8a53712019-12-16 11:29:38 +01001271 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001272 if (objt_server(s->target))
1273 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.internal_errors, 1);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001274 status = 500;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001275 goto return_prx_cond;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001276
Christopher Faulet93e02d82019-03-08 14:18:50 +01001277 return_bad_req:
Olivier Houcharda798bf52019-03-08 18:52:00 +01001278 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
William Lallemand36119de2021-03-08 15:26:48 +01001279 if (sess->listener && sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +01001280 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001281 status = 400;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001282 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001283
Christopher Fauletb8a53712019-12-16 11:29:38 +01001284 return_prx_cond:
Christopher Faulet9768c262018-10-22 09:34:31 +02001285 if (txn->status > 0) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001286 /* Note: we don't send any error if some data were already sent */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001287 http_reply_and_close(s, txn->status, NULL);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001288 } else {
Christopher Faulet93e02d82019-03-08 14:18:50 +01001289 txn->status = status;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001290 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001291 }
1292 req->analysers &= AN_REQ_FLT_END;
1293 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 +01001294 if (!(s->flags & SF_ERR_MASK))
1295 s->flags |= SF_ERR_PRXCOND;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001296 if (!(s->flags & SF_FINST_MASK))
1297 s->flags |= ((txn->rsp.msg_state < HTTP_MSG_ERROR) ? SF_FINST_H : SF_FINST_D);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001298 DBG_TRACE_DEVEL("leaving on error ",
1299 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001300 return 0;
1301}
1302
Olivier Houcharda254a372019-04-05 15:30:12 +02001303/* Reset the stream and the backend stream_interface to a situation suitable for attemption connection */
1304/* Returns 0 if we can attempt to retry, -1 otherwise */
1305static __inline int do_l7_retry(struct stream *s, struct stream_interface *si)
1306{
Christopher Faulet5b82cc52020-10-12 15:18:50 +02001307 struct channel *req = &s->req;
1308 struct channel *res = &s->res;
Olivier Houcharda254a372019-04-05 15:30:12 +02001309
1310 si->conn_retries--;
1311 if (si->conn_retries < 0)
Christopher Faulet5b82cc52020-10-12 15:18:50 +02001312 goto no_retry;
1313
1314 if (b_is_null(&req->buf) && !channel_alloc_buffer(req, &s->buffer_wait))
1315 goto no_retry;
Olivier Houcharda254a372019-04-05 15:30:12 +02001316
Willy Tarreau223995e2019-05-04 10:38:31 +02001317 if (objt_server(s->target))
1318 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.retries, 1);
1319 _HA_ATOMIC_ADD(&s->be->be_counters.retries, 1);
1320
Olivier Houcharda254a372019-04-05 15:30:12 +02001321 /* Remove any write error from the request, and read error from the response */
1322 req->flags &= ~(CF_WRITE_ERROR | CF_WRITE_TIMEOUT | CF_SHUTW | CF_SHUTW_NOW);
1323 res->flags &= ~(CF_READ_ERROR | CF_READ_TIMEOUT | CF_SHUTR | CF_EOI | CF_READ_NULL | CF_SHUTR_NOW);
1324 res->analysers = 0;
1325 si->flags &= ~(SI_FL_ERR | SI_FL_EXP | SI_FL_RXBLK_SHUT);
Olivier Houchard8cabc972020-05-12 22:18:14 +02001326 s->flags &= ~SF_ADDR_SET;
Olivier Houchard4bd58672019-07-12 16:16:59 +02001327 stream_choose_redispatch(s);
Olivier Houcharda254a372019-04-05 15:30:12 +02001328 si->exp = TICK_ETERNITY;
1329 res->rex = TICK_ETERNITY;
1330 res->to_forward = 0;
1331 res->analyse_exp = TICK_ETERNITY;
1332 res->total = 0;
Olivier Houchard4bd58672019-07-12 16:16:59 +02001333 s->flags &= ~(SF_ERR_SRVTO | SF_ERR_SRVCL);
Olivier Houcharda254a372019-04-05 15:30:12 +02001334 si_release_endpoint(&s->si[1]);
Olivier Houcharda254a372019-04-05 15:30:12 +02001335
Christopher Faulet5b82cc52020-10-12 15:18:50 +02001336 b_reset(&req->buf);
1337 memcpy(b_orig(&req->buf), b_orig(&si->l7_buffer), b_size(&si->l7_buffer));
1338 b_set_data(&req->buf, b_size(&req->buf));
1339 co_set_data(req, b_data(&si->l7_buffer));
1340
Ilya Shipitsinacf84592021-02-06 22:29:08 +05001341 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 +02001342 b_reset(&res->buf);
1343 co_set_data(res, 0);
1344 return 0;
Christopher Faulet5b82cc52020-10-12 15:18:50 +02001345
1346 no_retry:
1347 b_free(&si->l7_buffer);
1348 return -1;
Olivier Houcharda254a372019-04-05 15:30:12 +02001349}
1350
Christopher Faulete0768eb2018-10-03 16:38:02 +02001351/* This stream analyser waits for a complete HTTP response. It returns 1 if the
1352 * processing can continue on next analysers, or zero if it either needs more
1353 * data or wants to immediately abort the response (eg: timeout, error, ...). It
1354 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->res.analysers
1355 * when it has nothing left to do, and may remove any analyser when it wants to
1356 * abort.
1357 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001358int http_wait_for_response(struct stream *s, struct channel *rep, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001359{
Christopher Faulet9768c262018-10-22 09:34:31 +02001360 /*
1361 * We will analyze a complete HTTP response to check the its syntax.
1362 *
1363 * Once the start line and all headers are received, we may perform a
1364 * capture of the error (if any), and we will set a few fields. We also
1365 * logging and finally headers capture.
1366 */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001367 struct session *sess = s->sess;
1368 struct http_txn *txn = s->txn;
1369 struct http_msg *msg = &txn->rsp;
Christopher Faulet9768c262018-10-22 09:34:31 +02001370 struct htx *htx;
Olivier Houcharda254a372019-04-05 15:30:12 +02001371 struct stream_interface *si_b = &s->si[1];
Christopher Faulet61608322018-11-23 16:23:45 +01001372 struct connection *srv_conn;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001373 struct htx_sl *sl;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001374 int n;
1375
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001376 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001377
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001378 htx = htxbuf(&rep->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001379
Willy Tarreau4236f032019-03-05 10:43:32 +01001380 /* Parsing errors are caught here */
1381 if (htx->flags & HTX_FL_PARSING_ERROR)
1382 goto return_bad_res;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001383 if (htx->flags & HTX_FL_PROCESSING_ERROR)
1384 goto return_int_err;
Willy Tarreau4236f032019-03-05 10:43:32 +01001385
Christopher Faulete0768eb2018-10-03 16:38:02 +02001386 /*
1387 * Now we quickly check if we have found a full valid response.
1388 * If not so, we check the FD and buffer states before leaving.
1389 * A full response is indicated by the fact that we have seen
1390 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
1391 * responses are checked first.
1392 *
1393 * Depending on whether the client is still there or not, we
1394 * may send an error response back or not. Note that normally
1395 * we should only check for HTTP status there, and check I/O
1396 * errors somewhere else.
1397 */
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001398 next_one:
Christopher Faulet29f17582019-05-23 11:03:26 +02001399 if (unlikely(htx_is_empty(htx) || htx->first == -1)) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001400 /* 1: have we encountered a read error ? */
1401 if (rep->flags & CF_READ_ERROR) {
Olivier Houchard865d8392019-05-03 22:46:27 +02001402 struct connection *conn = NULL;
1403
Olivier Houchard865d8392019-05-03 22:46:27 +02001404 if (objt_cs(s->si[1].end))
1405 conn = objt_cs(s->si[1].end)->conn;
1406
1407 if (si_b->flags & SI_FL_L7_RETRY &&
1408 (!conn || conn->err_code != CO_ER_SSL_EARLY_FAILED)) {
Olivier Houcharda254a372019-04-05 15:30:12 +02001409 /* If we arrive here, then CF_READ_ERROR was
1410 * set by si_cs_recv() because we matched a
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05001411 * status, otherwise it would have removed
Olivier Houcharda254a372019-04-05 15:30:12 +02001412 * the SI_FL_L7_RETRY flag, so it's ok not
1413 * to check s->be->retry_type.
1414 */
Christopher Faulet5b82cc52020-10-12 15:18:50 +02001415 if (co_data(rep) || 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 Houcharda254a372019-04-05 15:30:12 +02001418 return 0;
Christopher Faulet5b82cc52020-10-12 15:18:50 +02001419 }
Olivier Houcharda254a372019-04-05 15:30:12 +02001420 }
1421
Olivier Houchard6db16992019-05-17 15:40:49 +02001422 if (txn->flags & TX_NOT_FIRST)
1423 goto abort_keep_alive;
1424
Olivier Houcharda798bf52019-03-08 18:52:00 +01001425 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001426 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001427 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001428 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001429 }
1430
Christopher Faulete0768eb2018-10-03 16:38:02 +02001431 rep->analysers &= AN_RES_FLT_END;
1432 txn->status = 502;
1433
1434 /* Check to see if the server refused the early data.
1435 * If so, just send a 425
1436 */
Willy Tarreauee99aaf2020-06-23 05:58:20 +02001437 if (conn && conn->err_code == CO_ER_SSL_EARLY_FAILED) {
Olivier Houchard865d8392019-05-03 22:46:27 +02001438 if ((s->be->retry_type & PR_RE_EARLY_ERROR) &&
Olivier Houchardad26d8d2019-05-10 17:48:28 +02001439 (si_b->flags & SI_FL_L7_RETRY) &&
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001440 do_l7_retry(s, si_b) == 0) {
1441 DBG_TRACE_DEVEL("leaving on L7 retry",
1442 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houchard865d8392019-05-03 22:46:27 +02001443 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001444 }
Olivier Houchard865d8392019-05-03 22:46:27 +02001445 txn->status = 425;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001446 }
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001447 else
1448 stream_inc_http_fail_ctr(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001449
1450 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001451 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001452
1453 if (!(s->flags & SF_ERR_MASK))
1454 s->flags |= SF_ERR_SRVCL;
1455 if (!(s->flags & SF_FINST_MASK))
1456 s->flags |= SF_FINST_H;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001457 DBG_TRACE_DEVEL("leaving on error",
1458 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001459 return 0;
1460 }
1461
Christopher Faulet9768c262018-10-22 09:34:31 +02001462 /* 2: read timeout : return a 504 to the client. */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001463 else if (rep->flags & CF_READ_TIMEOUT) {
Olivier Houcharda254a372019-04-05 15:30:12 +02001464 if ((si_b->flags & SI_FL_L7_RETRY) &&
1465 (s->be->retry_type & PR_RE_TIMEOUT)) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001466 if (co_data(rep) || do_l7_retry(s, si_b) == 0) {
1467 DBG_TRACE_DEVEL("leaving on L7 retry",
1468 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharda254a372019-04-05 15:30:12 +02001469 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001470 }
Olivier Houcharda254a372019-04-05 15:30:12 +02001471 }
Olivier Houcharda798bf52019-03-08 18:52:00 +01001472 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001473 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001474 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001475 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001476 }
1477
Christopher Faulete0768eb2018-10-03 16:38:02 +02001478 rep->analysers &= AN_RES_FLT_END;
1479 txn->status = 504;
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001480 stream_inc_http_fail_ctr(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001481 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001482 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001483
1484 if (!(s->flags & SF_ERR_MASK))
1485 s->flags |= SF_ERR_SRVTO;
1486 if (!(s->flags & SF_FINST_MASK))
1487 s->flags |= SF_FINST_H;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001488 DBG_TRACE_DEVEL("leaving on error",
1489 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001490 return 0;
1491 }
1492
Christopher Faulet9768c262018-10-22 09:34:31 +02001493 /* 3: client abort with an abortonclose */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001494 else if ((rep->flags & CF_SHUTR) && ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001495 _HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
1496 _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
William Lallemand36119de2021-03-08 15:26:48 +01001497 if (sess->listener && sess->listener->counters)
Christopher Fauletcff0f732019-12-16 16:13:44 +01001498 _HA_ATOMIC_ADD(&sess->listener->counters->cli_aborts, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001499 if (objt_server(s->target))
Olivier Houcharda798bf52019-03-08 18:52:00 +01001500 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.cli_aborts, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001501
1502 rep->analysers &= AN_RES_FLT_END;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001503 txn->status = 400;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001504 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001505
1506 if (!(s->flags & SF_ERR_MASK))
1507 s->flags |= SF_ERR_CLICL;
1508 if (!(s->flags & SF_FINST_MASK))
1509 s->flags |= SF_FINST_H;
1510
1511 /* process_stream() will take care of the error */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001512 DBG_TRACE_DEVEL("leaving on error",
1513 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001514 return 0;
1515 }
1516
Christopher Faulet9768c262018-10-22 09:34:31 +02001517 /* 4: close from server, capture the response if the server has started to respond */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001518 else if (rep->flags & CF_SHUTR) {
Olivier Houcharda254a372019-04-05 15:30:12 +02001519 if ((si_b->flags & SI_FL_L7_RETRY) &&
1520 (s->be->retry_type & PR_RE_DISCONNECTED)) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001521 if (co_data(rep) || do_l7_retry(s, si_b) == 0) {
1522 DBG_TRACE_DEVEL("leaving on L7 retry",
1523 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharda254a372019-04-05 15:30:12 +02001524 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001525 }
Olivier Houcharda254a372019-04-05 15:30:12 +02001526 }
1527
Olivier Houchard6db16992019-05-17 15:40:49 +02001528 if (txn->flags & TX_NOT_FIRST)
1529 goto abort_keep_alive;
1530
Olivier Houcharda798bf52019-03-08 18:52:00 +01001531 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001532 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001533 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001534 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001535 }
1536
Christopher Faulete0768eb2018-10-03 16:38:02 +02001537 rep->analysers &= AN_RES_FLT_END;
1538 txn->status = 502;
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001539 stream_inc_http_fail_ctr(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001540 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001541 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001542
1543 if (!(s->flags & SF_ERR_MASK))
1544 s->flags |= SF_ERR_SRVCL;
1545 if (!(s->flags & SF_FINST_MASK))
1546 s->flags |= SF_FINST_H;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001547 DBG_TRACE_DEVEL("leaving on error",
1548 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001549 return 0;
1550 }
1551
Christopher Faulet9768c262018-10-22 09:34:31 +02001552 /* 5: write error to client (we don't send any message then) */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001553 else if (rep->flags & CF_WRITE_ERROR) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001554 if (txn->flags & TX_NOT_FIRST)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001555 goto abort_keep_alive;
1556
Olivier Houcharda798bf52019-03-08 18:52:00 +01001557 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001558 if (objt_server(s->target))
1559 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001560 rep->analysers &= AN_RES_FLT_END;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001561
1562 if (!(s->flags & SF_ERR_MASK))
1563 s->flags |= SF_ERR_CLICL;
1564 if (!(s->flags & SF_FINST_MASK))
1565 s->flags |= SF_FINST_H;
1566
1567 /* process_stream() will take care of the error */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001568 DBG_TRACE_DEVEL("leaving on error",
1569 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001570 return 0;
1571 }
1572
1573 channel_dont_close(rep);
1574 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001575 DBG_TRACE_DEVEL("waiting for more data",
1576 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001577 return 0;
1578 }
1579
1580 /* More interesting part now : we know that we have a complete
1581 * response which at least looks like HTTP. We have an indicator
1582 * of each header's length, so we can parse them quickly.
1583 */
Christopher Faulet29f17582019-05-23 11:03:26 +02001584 BUG_ON(htx_get_first_type(htx) != HTX_BLK_RES_SL);
Christopher Faulet297fbb42019-05-13 14:41:27 +02001585 sl = http_get_stline(htx);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001586
Christopher Faulet5b82cc52020-10-12 15:18:50 +02001587 if ((si_b->flags & SI_FL_L7_RETRY) &&
1588 l7_status_match(s->be, sl->info.res.status) &&
1589 do_l7_retry(s, si_b) == 0) {
1590 DBG_TRACE_DEVEL("leaving on L7 retry", STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
1591 return 0;
1592 }
1593 b_free(&s->si[1].l7_buffer);
1594
1595 msg->msg_state = HTTP_MSG_BODY;
1596
Christopher Faulet9768c262018-10-22 09:34:31 +02001597 /* 0: we might have to print this header in debug mode */
1598 if (unlikely((global.mode & MODE_DEBUG) &&
1599 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
1600 int32_t pos;
1601
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001602 http_debug_stline("srvrep", s, sl);
Christopher Faulet9768c262018-10-22 09:34:31 +02001603
Christopher Fauleta3f15502019-05-13 15:27:23 +02001604 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001605 struct htx_blk *blk = htx_get_blk(htx, pos);
1606 enum htx_blk_type type = htx_get_blk_type(blk);
1607
1608 if (type == HTX_BLK_EOH)
1609 break;
1610 if (type != HTX_BLK_HDR)
1611 continue;
1612
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001613 http_debug_hdr("srvhdr", s,
1614 htx_get_blk_name(htx, blk),
1615 htx_get_blk_value(htx, blk));
Christopher Faulet9768c262018-10-22 09:34:31 +02001616 }
1617 }
1618
Christopher Faulet03599112018-11-27 11:21:21 +01001619 /* 1: get the status code and the version. Also set HTTP flags */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001620 txn->status = sl->info.res.status;
Christopher Faulet03599112018-11-27 11:21:21 +01001621 if (sl->flags & HTX_SL_F_VER_11)
Christopher Faulet9768c262018-10-22 09:34:31 +02001622 msg->flags |= HTTP_MSGF_VER_11;
Christopher Faulet03599112018-11-27 11:21:21 +01001623 if (sl->flags & HTX_SL_F_XFER_LEN) {
1624 msg->flags |= HTTP_MSGF_XFER_LEN;
Christopher Faulet2a408542020-11-20 14:22:37 +01001625 if (sl->flags & HTX_SL_F_CLEN)
1626 msg->flags |= HTTP_MSGF_CNT_LEN;
1627 else if (sl->flags & HTX_SL_F_CHNK)
1628 msg->flags |= HTTP_MSGF_TE_CHNK;
Christopher Faulet03599112018-11-27 11:21:21 +01001629 }
Christopher Faulet2a408542020-11-20 14:22:37 +01001630 if (sl->flags & HTX_SL_F_BODYLESS)
1631 msg->flags |= HTTP_MSGF_BODYLESS;
Christopher Faulet576c3582021-01-08 15:53:01 +01001632 if (sl->flags & HTX_SL_F_CONN_UPG)
1633 msg->flags |= HTTP_MSGF_CONN_UPG;
Christopher Faulet9768c262018-10-22 09:34:31 +02001634
1635 n = txn->status / 100;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001636 if (n < 1 || n > 5)
1637 n = 0;
Christopher Faulet9768c262018-10-22 09:34:31 +02001638
Christopher Faulete0768eb2018-10-03 16:38:02 +02001639 /* when the client triggers a 4xx from the server, it's most often due
1640 * to a missing object or permission. These events should be tracked
1641 * because if they happen often, it may indicate a brute force or a
1642 * vulnerability scan.
1643 */
1644 if (n == 4)
1645 stream_inc_http_err_ctr(s);
1646
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001647 if (n == 5 && txn->status != 501 && txn->status != 505)
1648 stream_inc_http_fail_ctr(s);
1649
Marcin Deranek3c27dda2020-05-15 18:32:51 +02001650 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001651 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.p.http.rsp[n], 1);
Marcin Deranek3c27dda2020-05-15 18:32:51 +02001652 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.p.http.cum_req, 1);
1653 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001654
Christopher Faulete0768eb2018-10-03 16:38:02 +02001655 /* Adjust server's health based on status code. Note: status codes 501
1656 * and 505 are triggered on demand by client request, so we must not
1657 * count them as server failures.
1658 */
1659 if (objt_server(s->target)) {
1660 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001661 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_OK);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001662 else
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001663 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_STS);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001664 }
1665
1666 /*
1667 * We may be facing a 100-continue response, or any other informational
1668 * 1xx response which is non-final, in which case this is not the right
1669 * response, and we're waiting for the next one. Let's allow this response
1670 * to go to the client and wait for the next one. There's an exception for
1671 * 101 which is used later in the code to switch protocols.
1672 */
1673 if (txn->status < 200 &&
1674 (txn->status == 100 || txn->status >= 102)) {
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001675 FLT_STRM_CB(s, flt_http_reset(s, msg));
Christopher Faulet421e7692019-06-13 11:16:45 +02001676 htx->first = channel_htx_fwd_headers(rep, htx);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001677 msg->msg_state = HTTP_MSG_RPBEFORE;
Christopher Faulet3499f622019-09-03 15:23:54 +02001678 msg->flags = 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001679 txn->status = 0;
1680 s->logs.t_data = -1; /* was not a response yet */
Christopher Faulet7d518452020-08-31 11:07:07 +02001681 rep->flags |= CF_SEND_DONTWAIT; /* Send ASAP informational messages */
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001682 goto next_one;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001683 }
1684
Christopher Faulet6e6c7b12021-01-08 16:02:05 +01001685 /* A 101-switching-protocols must contains a Connection header with the
1686 * "upgrade" option and the request too. It means both are agree to
1687 * upgrade. It is not so strict because there is no test on the Upgrade
1688 * header content. But it is probably stronger enough for now.
1689 */
1690 if (txn->status == 101 &&
1691 (!(txn->req.flags & HTTP_MSGF_CONN_UPG) || !(txn->rsp.flags & HTTP_MSGF_CONN_UPG)))
1692 goto return_bad_res;
1693
Christopher Faulete0768eb2018-10-03 16:38:02 +02001694 /*
1695 * 2: check for cacheability.
1696 */
1697
1698 switch (txn->status) {
1699 case 200:
1700 case 203:
1701 case 204:
1702 case 206:
1703 case 300:
1704 case 301:
1705 case 404:
1706 case 405:
1707 case 410:
1708 case 414:
1709 case 501:
1710 break;
1711 default:
1712 /* RFC7231#6.1:
1713 * Responses with status codes that are defined as
1714 * cacheable by default (e.g., 200, 203, 204, 206,
1715 * 300, 301, 404, 405, 410, 414, and 501 in this
1716 * specification) can be reused by a cache with
1717 * heuristic expiration unless otherwise indicated
1718 * by the method definition or explicit cache
1719 * controls [RFC7234]; all other status codes are
1720 * not cacheable by default.
1721 */
1722 txn->flags &= ~(TX_CACHEABLE | TX_CACHE_COOK);
1723 break;
1724 }
1725
1726 /*
1727 * 3: we may need to capture headers
1728 */
1729 s->logs.logwait &= ~LW_RESP;
1730 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->res_cap))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001731 http_capture_headers(htx, s->res_cap, sess->fe->rsp_cap);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001732
Christopher Faulet9768c262018-10-22 09:34:31 +02001733 /* Skip parsing if no content length is possible. */
Christopher Fauletc75668e2020-12-07 18:10:32 +01001734 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status >= 200 && txn->status < 300) ||
Christopher Faulete0768eb2018-10-03 16:38:02 +02001735 txn->status == 101)) {
1736 /* Either we've established an explicit tunnel, or we're
1737 * switching the protocol. In both cases, we're very unlikely
1738 * to understand the next protocols. We have to switch to tunnel
1739 * mode, so that we transfer the request and responses then let
1740 * this protocol pass unmodified. When we later implement specific
1741 * parsers for such protocols, we'll want to check the Upgrade
1742 * header which contains information about that protocol for
1743 * responses with status 101 (eg: see RFC2817 about TLS).
1744 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02001745 txn->flags |= TX_CON_WANT_TUN;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001746 }
1747
Christopher Faulet61608322018-11-23 16:23:45 +01001748 /* check for NTML authentication headers in 401 (WWW-Authenticate) and
1749 * 407 (Proxy-Authenticate) responses and set the connection to private
1750 */
1751 srv_conn = cs_conn(objt_cs(s->si[1].end));
1752 if (srv_conn) {
1753 struct ist hdr;
1754 struct http_hdr_ctx ctx;
1755
1756 if (txn->status == 401)
1757 hdr = ist("WWW-Authenticate");
1758 else if (txn->status == 407)
1759 hdr = ist("Proxy-Authenticate");
1760 else
1761 goto end;
1762
1763 ctx.blk = NULL;
1764 while (http_find_header(htx, hdr, &ctx, 0)) {
Willy Tarreauf1dcced2020-05-07 19:27:02 +02001765 /* If www-authenticate contains "Negotiate", "Nego2", or "NTLM",
1766 * possibly followed by blanks and a base64 string, the connection
1767 * is private. Since it's a mess to deal with, we only check for
1768 * values starting with "NTLM" or "Nego". Note that often multiple
1769 * headers are sent by the server there.
1770 */
1771 if ((ctx.value.len >= 4 && strncasecmp(ctx.value.ptr, "Nego", 4) == 0) ||
Willy Tarreau49a1d282020-05-07 19:10:15 +02001772 (ctx.value.len >= 4 && strncasecmp(ctx.value.ptr, "NTLM", 4) == 0)) {
Olivier Houchard250031e2019-05-29 15:01:50 +02001773 sess->flags |= SESS_FL_PREFER_LAST;
Christopher Faulet08016ab2020-07-01 16:10:06 +02001774 conn_set_owner(srv_conn, sess, NULL);
Christopher Faulet21ddc742020-07-01 15:26:14 +02001775 conn_set_private(srv_conn);
Ilya Shipitsin6b79f382020-07-23 00:32:55 +05001776 /* If it fail now, the same will be done in mux->detach() callback */
Christopher Faulet08016ab2020-07-01 16:10:06 +02001777 session_add_conn(srv_conn->owner, srv_conn, srv_conn->target);
Willy Tarreauf1dcced2020-05-07 19:27:02 +02001778 break;
Olivier Houchard250031e2019-05-29 15:01:50 +02001779 }
Christopher Faulet61608322018-11-23 16:23:45 +01001780 }
1781 }
1782
1783 end:
Christopher Faulete0768eb2018-10-03 16:38:02 +02001784 /* we want to have the response time before we start processing it */
1785 s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now);
1786
1787 /* end of job, return OK */
1788 rep->analysers &= ~an_bit;
1789 rep->analyse_exp = TICK_ETERNITY;
1790 channel_auto_close(rep);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001791 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001792 return 1;
1793
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001794 return_int_err:
Christopher Fauletcff0f732019-12-16 16:13:44 +01001795 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01001796 _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1);
William Lallemand36119de2021-03-08 15:26:48 +01001797 if (sess->listener && sess->listener->counters)
Christopher Fauletcff0f732019-12-16 16:13:44 +01001798 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01001799 if (objt_server(s->target))
1800 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.internal_errors, 1);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001801 txn->status = 500;
1802 if (!(s->flags & SF_ERR_MASK))
1803 s->flags |= SF_ERR_INTERNAL;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001804 goto return_prx_cond;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001805
1806 return_bad_res:
Olivier Houcharda798bf52019-03-08 18:52:00 +01001807 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulet47365272018-10-31 17:40:50 +01001808 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001809 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001810 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Christopher Faulet47365272018-10-31 17:40:50 +01001811 }
Olivier Houcharde3249a92019-05-03 23:01:47 +02001812 if ((s->be->retry_type & PR_RE_JUNK_REQUEST) &&
Olivier Houchardad26d8d2019-05-10 17:48:28 +02001813 (si_b->flags & SI_FL_L7_RETRY) &&
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001814 do_l7_retry(s, si_b) == 0) {
1815 DBG_TRACE_DEVEL("leaving on L7 retry",
1816 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharde3249a92019-05-03 23:01:47 +02001817 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001818 }
Christopher Faulet47365272018-10-31 17:40:50 +01001819 txn->status = 502;
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001820 stream_inc_http_fail_ctr(s);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001821 /* fall through */
1822
Christopher Fauletb8a53712019-12-16 11:29:38 +01001823 return_prx_cond:
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001824 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet47365272018-10-31 17:40:50 +01001825
1826 if (!(s->flags & SF_ERR_MASK))
1827 s->flags |= SF_ERR_PRXCOND;
1828 if (!(s->flags & SF_FINST_MASK))
1829 s->flags |= SF_FINST_H;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001830
1831 s->si[1].flags |= SI_FL_NOLINGER;
1832 rep->analysers &= AN_RES_FLT_END;
Christopher Faulete58c0002020-03-02 16:21:01 +01001833 s->req.analysers &= AN_REQ_FLT_END;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001834 rep->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001835 DBG_TRACE_DEVEL("leaving on error",
1836 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulet47365272018-10-31 17:40:50 +01001837 return 0;
1838
Christopher Faulete0768eb2018-10-03 16:38:02 +02001839 abort_keep_alive:
1840 /* A keep-alive request to the server failed on a network error.
1841 * The client is required to retry. We need to close without returning
1842 * any other information so that the client retries.
1843 */
1844 txn->status = 0;
1845 rep->analysers &= AN_RES_FLT_END;
1846 s->req.analysers &= AN_REQ_FLT_END;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001847 s->logs.logwait = 0;
1848 s->logs.level = 0;
1849 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001850 http_reply_and_close(s, txn->status, NULL);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001851 DBG_TRACE_DEVEL("leaving by closing K/A connection",
1852 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001853 return 0;
1854}
1855
1856/* This function performs all the processing enabled for the current response.
1857 * It normally returns 1 unless it wants to break. It relies on buffers flags,
1858 * and updates s->res.analysers. It might make sense to explode it into several
1859 * other functions. It works like process_request (see indications above).
1860 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001861int http_process_res_common(struct stream *s, struct channel *rep, int an_bit, struct proxy *px)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001862{
1863 struct session *sess = s->sess;
1864 struct http_txn *txn = s->txn;
1865 struct http_msg *msg = &txn->rsp;
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001866 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001867 struct proxy *cur_proxy;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001868 enum rule_result ret = HTTP_RULE_RES_CONT;
1869
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001870 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
1871 return 0;
Christopher Faulet9768c262018-10-22 09:34:31 +02001872
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001873 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001874
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001875 htx = htxbuf(&rep->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001876
1877 /* The stats applet needs to adjust the Connection header but we don't
1878 * apply any filter there.
1879 */
1880 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
1881 rep->analysers &= ~an_bit;
1882 rep->analyse_exp = TICK_ETERNITY;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001883 goto end;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001884 }
1885
1886 /*
1887 * We will have to evaluate the filters.
1888 * As opposed to version 1.2, now they will be evaluated in the
1889 * filters order and not in the header order. This means that
1890 * each filter has to be validated among all headers.
1891 *
1892 * Filters are tried with ->be first, then with ->fe if it is
1893 * different from ->be.
1894 *
1895 * Maybe we are in resume condiion. In this case I choose the
1896 * "struct proxy" which contains the rule list matching the resume
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05001897 * pointer. If none of these "struct proxy" match, I initialise
Christopher Faulete0768eb2018-10-03 16:38:02 +02001898 * the process with the first one.
1899 *
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05001900 * In fact, I check only correspondence between the current list
Christopher Faulete0768eb2018-10-03 16:38:02 +02001901 * pointer and the ->fe rule list. If it doesn't match, I initialize
1902 * the loop with the ->be.
1903 */
1904 if (s->current_rule_list == &sess->fe->http_res_rules)
1905 cur_proxy = sess->fe;
1906 else
1907 cur_proxy = s->be;
1908 while (1) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001909 /* evaluate http-response rules */
1910 if (ret == HTTP_RULE_RES_CONT) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001911 ret = http_res_get_intercept_rule(cur_proxy, &cur_proxy->http_res_rules, s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001912
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001913 switch (ret) {
1914 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
1915 goto return_prx_yield;
1916
1917 case HTTP_RULE_RES_CONT:
1918 case HTTP_RULE_RES_STOP: /* nothing to do */
1919 break;
1920
1921 case HTTP_RULE_RES_DENY: /* deny or tarpit */
1922 goto deny;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001923
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001924 case HTTP_RULE_RES_ABRT: /* abort request, response already sent */
1925 goto return_prx_cond;
1926
1927 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
Christopher Fauletb8a53712019-12-16 11:29:38 +01001928 goto done;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001929
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001930 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
1931 goto return_bad_res;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001932
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001933 case HTTP_RULE_RES_ERROR: /* failed with a bad request */
1934 goto return_int_err;
1935 }
1936
1937 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001938
Christopher Faulete0768eb2018-10-03 16:38:02 +02001939 /* check whether we're already working on the frontend */
1940 if (cur_proxy == sess->fe)
1941 break;
1942 cur_proxy = sess->fe;
1943 }
1944
Christopher Faulete0768eb2018-10-03 16:38:02 +02001945 /* OK that's all we can do for 1xx responses */
1946 if (unlikely(txn->status < 200 && txn->status != 101))
Christopher Fauletf2824e62018-10-01 12:12:37 +02001947 goto end;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001948
1949 /*
1950 * Now check for a server cookie.
1951 */
1952 if (s->be->cookie_name || sess->fe->capture_name || (s->be->options & PR_O_CHK_CACHE))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001953 http_manage_server_side_cookies(s, rep);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001954
1955 /*
1956 * Check for cache-control or pragma headers if required.
1957 */
1958 if ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001959 http_check_response_for_cacheability(s, rep);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001960
1961 /*
1962 * Add server cookie in the response if needed
1963 */
1964 if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) &&
1965 !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) &&
1966 (!(s->flags & SF_DIRECT) ||
1967 ((s->be->cookie_maxidle || txn->cookie_last_date) &&
1968 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
1969 (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
1970 (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
1971 (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
1972 !(s->flags & SF_IGNORE_PRST)) {
1973 /* the server is known, it's not the one the client requested, or the
1974 * cookie's last seen date needs to be refreshed. We have to
1975 * insert a set-cookie here, except if we want to insert only on POST
1976 * requests and this one isn't. Note that servers which don't have cookies
1977 * (eg: some backup servers) will return a full cookie removal request.
1978 */
1979 if (!objt_server(s->target)->cookie) {
1980 chunk_printf(&trash,
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001981 "%s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
Christopher Faulete0768eb2018-10-03 16:38:02 +02001982 s->be->cookie_name);
1983 }
1984 else {
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001985 chunk_printf(&trash, "%s=%s", s->be->cookie_name, objt_server(s->target)->cookie);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001986
1987 if (s->be->cookie_maxidle || s->be->cookie_maxlife) {
1988 /* emit last_date, which is mandatory */
1989 trash.area[trash.data++] = COOKIE_DELIM_DATE;
1990 s30tob64((date.tv_sec+3) >> 2,
1991 trash.area + trash.data);
1992 trash.data += 5;
1993
1994 if (s->be->cookie_maxlife) {
1995 /* emit first_date, which is either the original one or
1996 * the current date.
1997 */
1998 trash.area[trash.data++] = COOKIE_DELIM_DATE;
1999 s30tob64(txn->cookie_first_date ?
2000 txn->cookie_first_date >> 2 :
2001 (date.tv_sec+3) >> 2,
2002 trash.area + trash.data);
2003 trash.data += 5;
2004 }
2005 }
2006 chunk_appendf(&trash, "; path=/");
2007 }
2008
2009 if (s->be->cookie_domain)
2010 chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain);
2011
2012 if (s->be->ck_opts & PR_CK_HTTPONLY)
2013 chunk_appendf(&trash, "; HttpOnly");
2014
2015 if (s->be->ck_opts & PR_CK_SECURE)
2016 chunk_appendf(&trash, "; Secure");
2017
Christopher Faulet2f533902020-01-21 11:06:48 +01002018 if (s->be->cookie_attrs)
2019 chunk_appendf(&trash, "; %s", s->be->cookie_attrs);
2020
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002021 if (unlikely(!http_add_header(htx, ist("Set-Cookie"), ist2(trash.area, trash.data))))
Christopher Fauletb8a53712019-12-16 11:29:38 +01002022 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002023
2024 txn->flags &= ~TX_SCK_MASK;
2025 if (__objt_server(s->target)->cookie && (s->flags & SF_DIRECT))
2026 /* the server did not change, only the date was updated */
2027 txn->flags |= TX_SCK_UPDATED;
2028 else
2029 txn->flags |= TX_SCK_INSERTED;
2030
2031 /* Here, we will tell an eventual cache on the client side that we don't
2032 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
2033 * Some caches understand the correct form: 'no-cache="set-cookie"', but
2034 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
2035 */
2036 if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
2037
2038 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
2039
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002040 if (unlikely(!http_add_header(htx, ist("Cache-control"), ist("private"))))
Christopher Fauletb8a53712019-12-16 11:29:38 +01002041 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002042 }
2043 }
2044
2045 /*
2046 * Check if result will be cacheable with a cookie.
2047 * We'll block the response if security checks have caught
2048 * nasty things such as a cacheable cookie.
2049 */
2050 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
2051 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
2052 (s->be->options & PR_O_CHK_CACHE)) {
2053 /* we're in presence of a cacheable response containing
2054 * a set-cookie header. We'll block it as requested by
2055 * the 'checkcache' option, and send an alert.
2056 */
Christopher Faulete0768eb2018-10-03 16:38:02 +02002057 ha_alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
2058 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
2059 send_log(s->be, LOG_ALERT,
2060 "Blocking cacheable cookie in response from instance %s, server %s.\n",
2061 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
Christopher Fauletb8a53712019-12-16 11:29:38 +01002062 goto deny;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002063 }
2064
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002065 end:
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002066 /*
2067 * Evaluate after-response rules before forwarding the response. rules
2068 * from the backend are evaluated first, then one from the frontend if
2069 * it differs.
2070 */
2071 if (!http_eval_after_res_rules(s))
2072 goto return_int_err;
2073
Christopher Faulete0768eb2018-10-03 16:38:02 +02002074 /* Always enter in the body analyzer */
2075 rep->analysers &= ~AN_RES_FLT_XFER_DATA;
2076 rep->analysers |= AN_RES_HTTP_XFER_BODY;
2077
2078 /* if the user wants to log as soon as possible, without counting
2079 * bytes from the server, then this is the right moment. We have
2080 * to temporarily assign bytes_out to log what we currently have.
2081 */
2082 if (!LIST_ISEMPTY(&sess->fe->logformat) && !(s->logs.logwait & LW_BYTES)) {
2083 s->logs.t_close = s->logs.t_data; /* to get a valid end date */
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002084 s->logs.bytes_out = htx->data;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002085 s->do_log(s);
2086 s->logs.bytes_out = 0;
2087 }
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002088
Christopher Fauletb8a53712019-12-16 11:29:38 +01002089 done:
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002090 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002091 rep->analysers &= ~an_bit;
2092 rep->analyse_exp = TICK_ETERNITY;
2093 return 1;
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002094
Christopher Fauletb8a53712019-12-16 11:29:38 +01002095 deny:
Christopher Fauletb8a53712019-12-16 11:29:38 +01002096 _HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_resp, 1);
Christopher Fauleta08546b2019-12-16 16:07:34 +01002097 _HA_ATOMIC_ADD(&s->be->be_counters.denied_resp, 1);
William Lallemand36119de2021-03-08 15:26:48 +01002098 if (sess->listener && sess->listener->counters)
Christopher Fauletb8a53712019-12-16 11:29:38 +01002099 _HA_ATOMIC_ADD(&sess->listener->counters->denied_resp, 1);
Christopher Fauleta08546b2019-12-16 16:07:34 +01002100 if (objt_server(s->target))
2101 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.denied_resp, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002102 goto return_prx_err;
2103
2104 return_int_err:
2105 txn->status = 500;
2106 if (!(s->flags & SF_ERR_MASK))
2107 s->flags |= SF_ERR_INTERNAL;
Christopher Fauletcff0f732019-12-16 16:13:44 +01002108 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002109 _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1);
2110 if (objt_server(s->target))
2111 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01002112 if (objt_server(s->target))
2113 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002114 goto return_prx_err;
2115
2116 return_bad_res:
2117 txn->status = 502;
Willy Tarreau826f3ab2021-02-10 12:07:15 +01002118 stream_inc_http_fail_ctr(s);
Christopher Fauleta20a6532020-02-05 10:16:41 +01002119 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
2120 if (objt_server(s->target)) {
2121 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
2122 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_RSP);
2123 }
Christopher Fauletb8a53712019-12-16 11:29:38 +01002124 /* fall through */
2125
2126 return_prx_err:
2127 http_reply_and_close(s, txn->status, http_error_message(s));
2128 /* fall through */
2129
2130 return_prx_cond:
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002131 s->logs.t_data = -1; /* was not a valid response */
2132 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002133
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002134 if (!(s->flags & SF_ERR_MASK))
2135 s->flags |= SF_ERR_PRXCOND;
2136 if (!(s->flags & SF_FINST_MASK))
2137 s->flags |= SF_FINST_H;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002138
Christopher Faulete58c0002020-03-02 16:21:01 +01002139 rep->analysers &= AN_RES_FLT_END;
2140 s->req.analysers &= AN_REQ_FLT_END;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002141 rep->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002142 DBG_TRACE_DEVEL("leaving on error",
2143 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002144 return 0;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002145
2146 return_prx_yield:
2147 channel_dont_close(rep);
2148 DBG_TRACE_DEVEL("waiting for more data",
2149 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
2150 return 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002151}
2152
2153/* This function is an analyser which forwards response body (including chunk
2154 * sizes if any). It is called as soon as we must forward, even if we forward
2155 * zero byte. The only situation where it must not be called is when we're in
2156 * tunnel mode and we want to forward till the close. It's used both to forward
2157 * remaining data and to resync after end of body. It expects the msg_state to
2158 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
2159 * read more data, or 1 once we can go on with next request or end the stream.
2160 *
2161 * It is capable of compressing response data both in content-length mode and
2162 * in chunked mode. The state machines follows different flows depending on
2163 * whether content-length and chunked modes are used, since there are no
2164 * trailers in content-length :
2165 *
2166 * chk-mode cl-mode
2167 * ,----- BODY -----.
2168 * / \
2169 * V size > 0 V chk-mode
2170 * .--> SIZE -------------> DATA -------------> CRLF
2171 * | | size == 0 | last byte |
2172 * | v final crlf v inspected |
2173 * | TRAILERS -----------> DONE |
2174 * | |
2175 * `----------------------------------------------'
2176 *
2177 * Compression only happens in the DATA state, and must be flushed in final
2178 * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding
2179 * is performed at once on final states for all bytes parsed, or when leaving
2180 * on missing data.
2181 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002182int http_response_forward_body(struct stream *s, struct channel *res, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +02002183{
2184 struct session *sess = s->sess;
2185 struct http_txn *txn = s->txn;
2186 struct http_msg *msg = &s->txn->rsp;
Christopher Faulet9768c262018-10-22 09:34:31 +02002187 struct htx *htx;
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002188 int ret;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002189
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002190 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002191
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002192 htx = htxbuf(&res->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002193
Christopher Fauletb9a92f32019-09-09 10:15:21 +02002194 if (htx->flags & HTX_FL_PARSING_ERROR)
2195 goto return_bad_res;
2196 if (htx->flags & HTX_FL_PROCESSING_ERROR)
2197 goto return_int_err;
2198
Christopher Faulete0768eb2018-10-03 16:38:02 +02002199 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Christopher Fauletf2824e62018-10-01 12:12:37 +02002200 ((res->flags & CF_SHUTW) && (res->to_forward || co_data(res)))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02002201 /* Output closed while we were sending data. We must abort and
2202 * wake the other side up.
2203 */
Christopher Faulete0768eb2018-10-03 16:38:02 +02002204 msg->msg_state = HTTP_MSG_ERROR;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002205 http_end_response(s);
2206 http_end_request(s);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002207 DBG_TRACE_DEVEL("leaving on error",
2208 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002209 return 1;
2210 }
2211
Christopher Faulet9768c262018-10-22 09:34:31 +02002212 if (msg->msg_state == HTTP_MSG_BODY)
2213 msg->msg_state = HTTP_MSG_DATA;
2214
Christopher Faulete0768eb2018-10-03 16:38:02 +02002215 /* in most states, we should abort in case of early close */
2216 channel_auto_close(res);
2217
Christopher Faulete0768eb2018-10-03 16:38:02 +02002218 if (res->to_forward) {
Christopher Faulet66af0b22019-03-22 14:54:52 +01002219 if (res->to_forward == CHN_INFINITE_FORWARD) {
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002220 if (res->flags & CF_EOI)
2221 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet66af0b22019-03-22 14:54:52 +01002222 }
2223 else {
2224 /* We can't process the buffer's contents yet */
2225 res->flags |= CF_WAKE_WRITE;
2226 goto missing_data_or_waiting;
2227 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02002228 }
2229
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002230 if (msg->msg_state >= HTTP_MSG_ENDING)
2231 goto ending;
2232
Christopher Fauletc75668e2020-12-07 18:10:32 +01002233 if ((txn->meth == HTTP_METH_CONNECT && txn->status >= 200 && txn->status < 300) || txn->status == 101 ||
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002234 (!(msg->flags & HTTP_MSGF_XFER_LEN) && !HAS_RSP_DATA_FILTERS(s))) {
2235 msg->msg_state = HTTP_MSG_ENDING;
2236 goto ending;
2237 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002238
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002239 /* Forward input data. We get it by removing all outgoing data not
2240 * forwarded yet from HTX data size. If there are some data filters, we
2241 * let them decide the amount of data to forward.
Christopher Faulet9768c262018-10-22 09:34:31 +02002242 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002243 if (HAS_RSP_DATA_FILTERS(s)) {
2244 ret = flt_http_payload(s, msg, htx->data);
2245 if (ret < 0)
2246 goto return_bad_res;
Christopher Faulet421e7692019-06-13 11:16:45 +02002247 c_adv(res, ret);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002248 }
2249 else {
Christopher Faulet421e7692019-06-13 11:16:45 +02002250 c_adv(res, htx->data - co_data(res));
Christopher Faulet66af0b22019-03-22 14:54:52 +01002251 if (msg->flags & HTTP_MSGF_XFER_LEN)
2252 channel_htx_forward_forever(res, htx);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002253 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002254
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002255 if (htx->data != co_data(res))
2256 goto missing_data_or_waiting;
2257
2258 if (!(msg->flags & HTTP_MSGF_XFER_LEN) && res->flags & CF_SHUTR) {
2259 msg->msg_state = HTTP_MSG_ENDING;
2260 goto ending;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002261 }
2262
Christopher Faulet9768c262018-10-22 09:34:31 +02002263 /* Check if the end-of-message is reached and if so, switch the message
Christopher Fauletd20fdb02019-06-13 16:43:22 +02002264 * in HTTP_MSG_ENDING state. Then if all data was marked to be
2265 * forwarded, set the state to HTTP_MSG_DONE.
Christopher Faulet9768c262018-10-22 09:34:31 +02002266 */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002267 if (!(htx->flags & HTX_FL_EOM))
Christopher Faulet9768c262018-10-22 09:34:31 +02002268 goto missing_data_or_waiting;
2269
Christopher Fauletd20fdb02019-06-13 16:43:22 +02002270 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet9768c262018-10-22 09:34:31 +02002271
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002272 ending:
Christopher Faulet2151cdd2020-07-22 16:34:59 +02002273 res->flags &= ~CF_EXPECT_MORE; /* no more data are expected */
2274
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002275 /* other states, ENDING...TUNNEL */
2276 if (msg->msg_state >= HTTP_MSG_DONE)
2277 goto done;
Christopher Faulet9768c262018-10-22 09:34:31 +02002278
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002279 if (HAS_RSP_DATA_FILTERS(s)) {
2280 ret = flt_http_end(s, msg);
2281 if (ret <= 0) {
2282 if (!ret)
2283 goto missing_data_or_waiting;
2284 goto return_bad_res;
2285 }
2286 }
2287
Christopher Fauletc75668e2020-12-07 18:10:32 +01002288 if ((txn->meth == HTTP_METH_CONNECT && txn->status >= 200 && txn->status < 300) || txn->status == 101 ||
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002289 !(msg->flags & HTTP_MSGF_XFER_LEN)) {
2290 msg->msg_state = HTTP_MSG_TUNNEL;
2291 goto ending;
2292 }
2293 else {
2294 msg->msg_state = HTTP_MSG_DONE;
2295 res->to_forward = 0;
2296 }
2297
2298 done:
2299
2300 channel_dont_close(res);
2301
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002302 http_end_response(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002303 if (!(res->analysers & an_bit)) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002304 http_end_request(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002305 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
2306 if (res->flags & CF_SHUTW) {
2307 /* response errors are most likely due to the
2308 * client aborting the transfer. */
Christopher Faulet93e02d82019-03-08 14:18:50 +01002309 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002310 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02002311 goto return_bad_res;
2312 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002313 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002314 return 1;
2315 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002316 DBG_TRACE_DEVEL("waiting for the end of the HTTP txn",
2317 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002318 return 0;
2319
2320 missing_data_or_waiting:
2321 if (res->flags & CF_SHUTW)
Christopher Faulet93e02d82019-03-08 14:18:50 +01002322 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002323
2324 /* stop waiting for data if the input is closed before the end. If the
2325 * client side was already closed, it means that the client has aborted,
2326 * so we don't want to count this as a server abort. Otherwise it's a
2327 * server abort.
2328 */
Christopher Fauletd20fdb02019-06-13 16:43:22 +02002329 if (msg->msg_state < HTTP_MSG_ENDING && res->flags & CF_SHUTR) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02002330 if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))
Christopher Faulet93e02d82019-03-08 14:18:50 +01002331 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002332 /* If we have some pending data, we continue the processing */
Christopher Faulet93e02d82019-03-08 14:18:50 +01002333 if (htx_is_empty(htx))
2334 goto return_srv_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002335 }
2336
Christopher Faulete0768eb2018-10-03 16:38:02 +02002337 /* When TE: chunked is used, we need to get there again to parse
2338 * remaining chunks even if the server has closed, so we don't want to
Christopher Faulet9768c262018-10-22 09:34:31 +02002339 * set CF_DONTCLOSE. Similarly when there is a content-leng or if there
2340 * are filters registered on the stream, we don't want to forward a
2341 * close
Christopher Faulete0768eb2018-10-03 16:38:02 +02002342 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002343 if ((msg->flags & HTTP_MSGF_XFER_LEN) || HAS_RSP_DATA_FILTERS(s))
Christopher Faulete0768eb2018-10-03 16:38:02 +02002344 channel_dont_close(res);
2345
2346 /* We know that more data are expected, but we couldn't send more that
2347 * what we did. So we always set the CF_EXPECT_MORE flag so that the
2348 * system knows it must not set a PUSH on this first part. Interactive
2349 * modes are already handled by the stream sock layer. We must not do
2350 * this in content-length mode because it could present the MSG_MORE
2351 * flag with the last block of forwarded data, which would cause an
2352 * additional delay to be observed by the receiver.
2353 */
Christopher Faulet2151cdd2020-07-22 16:34:59 +02002354 if (HAS_RSP_DATA_FILTERS(s))
Christopher Faulete0768eb2018-10-03 16:38:02 +02002355 res->flags |= CF_EXPECT_MORE;
2356
2357 /* the stream handler will take care of timeouts and errors */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002358 DBG_TRACE_DEVEL("waiting for more data to forward",
2359 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002360 return 0;
2361
Christopher Faulet93e02d82019-03-08 14:18:50 +01002362 return_srv_abort:
2363 _HA_ATOMIC_ADD(&sess->fe->fe_counters.srv_aborts, 1);
2364 _HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1);
William Lallemand36119de2021-03-08 15:26:48 +01002365 if (sess->listener && sess->listener->counters)
Christopher Fauletcff0f732019-12-16 16:13:44 +01002366 _HA_ATOMIC_ADD(&sess->listener->counters->srv_aborts, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002367 if (objt_server(s->target))
Christopher Fauletcff0f732019-12-16 16:13:44 +01002368 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.srv_aborts, 1);
Willy Tarreau826f3ab2021-02-10 12:07:15 +01002369 stream_inc_http_fail_ctr(s);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002370 if (!(s->flags & SF_ERR_MASK))
2371 s->flags |= SF_ERR_SRVCL;
2372 goto return_error;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002373
Christopher Faulet93e02d82019-03-08 14:18:50 +01002374 return_cli_abort:
2375 _HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
2376 _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
William Lallemand36119de2021-03-08 15:26:48 +01002377 if (sess->listener && sess->listener->counters)
Christopher Fauletcff0f732019-12-16 16:13:44 +01002378 _HA_ATOMIC_ADD(&sess->listener->counters->cli_aborts, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002379 if (objt_server(s->target))
Christopher Fauletcff0f732019-12-16 16:13:44 +01002380 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.cli_aborts, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002381 if (!(s->flags & SF_ERR_MASK))
2382 s->flags |= SF_ERR_CLICL;
2383 goto return_error;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002384
Christopher Fauletb9a92f32019-09-09 10:15:21 +02002385 return_int_err:
Christopher Fauletcff0f732019-12-16 16:13:44 +01002386 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002387 _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1);
William Lallemand36119de2021-03-08 15:26:48 +01002388 if (sess->listener && sess->listener->counters)
Christopher Fauletcff0f732019-12-16 16:13:44 +01002389 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002390 if (objt_server(s->target))
2391 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.internal_errors, 1);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02002392 if (!(s->flags & SF_ERR_MASK))
2393 s->flags |= SF_ERR_INTERNAL;
2394 goto return_error;
2395
Christopher Faulet93e02d82019-03-08 14:18:50 +01002396 return_bad_res:
2397 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
2398 if (objt_server(s->target)) {
Christopher Fauletcff0f732019-12-16 16:13:44 +01002399 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002400 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_RSP);
2401 }
Willy Tarreau826f3ab2021-02-10 12:07:15 +01002402 stream_inc_http_fail_ctr(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002403 if (!(s->flags & SF_ERR_MASK))
Christopher Faulet93e02d82019-03-08 14:18:50 +01002404 s->flags |= SF_ERR_SRVCL;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002405 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +02002406
Christopher Faulet93e02d82019-03-08 14:18:50 +01002407 return_error:
Christopher Faulete0768eb2018-10-03 16:38:02 +02002408 /* don't send any error message as we're in the body */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002409 http_reply_and_close(s, txn->status, NULL);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002410 res->analysers &= AN_RES_FLT_END;
2411 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 +02002412 if (!(s->flags & SF_FINST_MASK))
2413 s->flags |= SF_FINST_D;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002414 DBG_TRACE_DEVEL("leaving on error",
2415 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002416 return 0;
2417}
2418
Christopher Fauletf2824e62018-10-01 12:12:37 +02002419/* Perform an HTTP redirect based on the information in <rule>. The function
Christopher Faulet99daf282018-11-28 22:58:13 +01002420 * returns zero on success, or zero in case of a, irrecoverable error such
Christopher Fauletf2824e62018-10-01 12:12:37 +02002421 * as too large a request to build a valid response.
2422 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002423int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn)
Christopher Fauletf2824e62018-10-01 12:12:37 +02002424{
Christopher Faulet99daf282018-11-28 22:58:13 +01002425 struct channel *req = &s->req;
2426 struct channel *res = &s->res;
2427 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01002428 struct htx_sl *sl;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002429 struct buffer *chunk;
Christopher Faulet99daf282018-11-28 22:58:13 +01002430 struct ist status, reason, location;
2431 unsigned int flags;
Christopher Faulet08e66462019-05-23 16:44:59 +02002432 int close = 0; /* Try to keep the connection alive byt default */
Christopher Fauletf2824e62018-10-01 12:12:37 +02002433
2434 chunk = alloc_trash_chunk();
Christopher Fauletb8a53712019-12-16 11:29:38 +01002435 if (!chunk) {
2436 if (!(s->flags & SF_ERR_MASK))
2437 s->flags |= SF_ERR_RESOURCE;
Christopher Faulet99daf282018-11-28 22:58:13 +01002438 goto fail;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002439 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002440
Christopher Faulet99daf282018-11-28 22:58:13 +01002441 /*
2442 * Create the location
2443 */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002444 htx = htxbuf(&req->buf);
Christopher Fauletf2824e62018-10-01 12:12:37 +02002445 switch(rule->type) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002446 case REDIRECT_TYPE_SCHEME: {
2447 struct http_hdr_ctx ctx;
2448 struct ist path, host;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002449
Christopher Faulet99daf282018-11-28 22:58:13 +01002450 host = ist("");
2451 ctx.blk = NULL;
2452 if (http_find_header(htx, ist("Host"), &ctx, 0))
2453 host = ctx.value;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002454
Christopher Faulet297fbb42019-05-13 14:41:27 +02002455 sl = http_get_stline(htx);
Christopher Faulet99daf282018-11-28 22:58:13 +01002456 path = http_get_path(htx_sl_req_uri(sl));
2457 /* build message using path */
Tim Duesterhused526372020-03-05 17:56:33 +01002458 if (isttest(path)) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002459 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2460 int qs = 0;
2461 while (qs < path.len) {
2462 if (*(path.ptr + qs) == '?') {
2463 path.len = qs;
2464 break;
2465 }
2466 qs++;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002467 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002468 }
2469 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002470 else
2471 path = ist("/");
Christopher Fauletf2824e62018-10-01 12:12:37 +02002472
Christopher Faulet99daf282018-11-28 22:58:13 +01002473 if (rule->rdr_str) { /* this is an old "redirect" rule */
2474 /* add scheme */
2475 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2476 goto fail;
2477 }
2478 else {
2479 /* add scheme with executing log format */
2480 chunk->data += build_logline(s, chunk->area + chunk->data,
2481 chunk->size - chunk->data,
2482 &rule->rdr_fmt);
2483 }
2484 /* add "://" + host + path */
2485 if (!chunk_memcat(chunk, "://", 3) ||
2486 !chunk_memcat(chunk, host.ptr, host.len) ||
2487 !chunk_memcat(chunk, path.ptr, path.len))
2488 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002489
Christopher Faulet99daf282018-11-28 22:58:13 +01002490 /* append a slash at the end of the location if needed and missing */
2491 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
2492 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
2493 if (chunk->data + 1 >= chunk->size)
2494 goto fail;
2495 chunk->area[chunk->data++] = '/';
2496 }
2497 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002498 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002499
Christopher Faulet99daf282018-11-28 22:58:13 +01002500 case REDIRECT_TYPE_PREFIX: {
2501 struct ist path;
2502
Christopher Faulet297fbb42019-05-13 14:41:27 +02002503 sl = http_get_stline(htx);
Christopher Faulet99daf282018-11-28 22:58:13 +01002504 path = http_get_path(htx_sl_req_uri(sl));
2505 /* build message using path */
Tim Duesterhused526372020-03-05 17:56:33 +01002506 if (isttest(path)) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002507 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2508 int qs = 0;
2509 while (qs < path.len) {
2510 if (*(path.ptr + qs) == '?') {
2511 path.len = qs;
2512 break;
2513 }
2514 qs++;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002515 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002516 }
2517 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002518 else
2519 path = ist("/");
Christopher Fauletf2824e62018-10-01 12:12:37 +02002520
Christopher Faulet99daf282018-11-28 22:58:13 +01002521 if (rule->rdr_str) { /* this is an old "redirect" rule */
2522 /* add prefix. Note that if prefix == "/", we don't want to
2523 * add anything, otherwise it makes it hard for the user to
2524 * configure a self-redirection.
2525 */
2526 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
2527 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2528 goto fail;
2529 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002530 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002531 else {
2532 /* add prefix with executing log format */
2533 chunk->data += build_logline(s, chunk->area + chunk->data,
2534 chunk->size - chunk->data,
2535 &rule->rdr_fmt);
2536 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002537
Christopher Faulet99daf282018-11-28 22:58:13 +01002538 /* add path */
2539 if (!chunk_memcat(chunk, path.ptr, path.len))
2540 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002541
Christopher Faulet99daf282018-11-28 22:58:13 +01002542 /* append a slash at the end of the location if needed and missing */
2543 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
2544 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
2545 if (chunk->data + 1 >= chunk->size)
2546 goto fail;
2547 chunk->area[chunk->data++] = '/';
2548 }
2549 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002550 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002551 case REDIRECT_TYPE_LOCATION:
2552 default:
2553 if (rule->rdr_str) { /* this is an old "redirect" rule */
2554 /* add location */
2555 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2556 goto fail;
2557 }
2558 else {
2559 /* add location with executing log format */
2560 chunk->data += build_logline(s, chunk->area + chunk->data,
2561 chunk->size - chunk->data,
2562 &rule->rdr_fmt);
2563 }
2564 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002565 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002566 location = ist2(chunk->area, chunk->data);
2567
2568 /*
2569 * Create the 30x response
2570 */
2571 switch (rule->code) {
2572 case 308:
2573 status = ist("308");
2574 reason = ist("Permanent Redirect");
2575 break;
2576 case 307:
2577 status = ist("307");
2578 reason = ist("Temporary Redirect");
2579 break;
2580 case 303:
2581 status = ist("303");
2582 reason = ist("See Other");
2583 break;
2584 case 301:
2585 status = ist("301");
2586 reason = ist("Moved Permanently");
2587 break;
2588 case 302:
2589 default:
2590 status = ist("302");
2591 reason = ist("Found");
2592 break;
2593 }
2594
Christopher Faulet08e66462019-05-23 16:44:59 +02002595 if (!(txn->req.flags & HTTP_MSGF_BODYLESS) && txn->req.msg_state != HTTP_MSG_DONE)
2596 close = 1;
2597
Christopher Faulet99daf282018-11-28 22:58:13 +01002598 htx = htx_from_buf(&res->buf);
Kevin Zhu96b36392020-01-07 09:42:55 +01002599 /* Trim any possible response */
2600 channel_htx_truncate(&s->res, htx);
Christopher Faulet99daf282018-11-28 22:58:13 +01002601 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
2602 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), status, reason);
2603 if (!sl)
2604 goto fail;
2605 sl->info.res.status = rule->code;
2606 s->txn->status = rule->code;
2607
Christopher Faulet08e66462019-05-23 16:44:59 +02002608 if (close && !htx_add_header(htx, ist("Connection"), ist("close")))
2609 goto fail;
2610
2611 if (!htx_add_header(htx, ist("Content-length"), ist("0")) ||
Christopher Faulet99daf282018-11-28 22:58:13 +01002612 !htx_add_header(htx, ist("Location"), location))
2613 goto fail;
2614
2615 if (rule->code == 302 || rule->code == 303 || rule->code == 307) {
2616 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")))
2617 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002618 }
2619
2620 if (rule->cookie_len) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002621 if (!htx_add_header(htx, ist("Set-Cookie"), ist2(rule->cookie_str, rule->cookie_len)))
2622 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002623 }
2624
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002625 if (!htx_add_endof(htx, HTX_BLK_EOH))
Christopher Faulet99daf282018-11-28 22:58:13 +01002626 goto fail;
2627
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002628 htx->flags |= HTX_FL_EOM;
Kevin Zhu96b36392020-01-07 09:42:55 +01002629 htx_to_buf(htx, &res->buf);
Christopher Fauleta72a7e42020-01-28 09:28:11 +01002630 if (!http_forward_proxy_resp(s, 1))
2631 goto fail;
Christopher Faulet99daf282018-11-28 22:58:13 +01002632
Christopher Faulet60b33a52020-01-28 09:18:10 +01002633 if (rule->flags & REDIRECT_FLAG_FROM_REQ) {
2634 /* let's log the request time */
2635 s->logs.tv_request = now;
2636 req->analysers &= AN_REQ_FLT_END;
Christopher Faulet99daf282018-11-28 22:58:13 +01002637
Christopher Faulet60b33a52020-01-28 09:18:10 +01002638 if (s->sess->fe == s->be) /* report it if the request was intercepted by the frontend */
2639 _HA_ATOMIC_ADD(&s->sess->fe->fe_counters.intercepted_req, 1);
2640 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002641
2642 if (!(s->flags & SF_ERR_MASK))
2643 s->flags |= SF_ERR_LOCAL;
2644 if (!(s->flags & SF_FINST_MASK))
Christopher Faulet60b33a52020-01-28 09:18:10 +01002645 s->flags |= ((rule->flags & REDIRECT_FLAG_FROM_REQ) ? SF_FINST_R : SF_FINST_H);
Christopher Fauletf2824e62018-10-01 12:12:37 +02002646
Christopher Faulet99daf282018-11-28 22:58:13 +01002647 free_trash_chunk(chunk);
2648 return 1;
2649
2650 fail:
2651 /* If an error occurred, remove the incomplete HTTP response from the
2652 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01002653 channel_htx_truncate(res, htxbuf(&res->buf));
Christopher Fauletf2824e62018-10-01 12:12:37 +02002654 free_trash_chunk(chunk);
Christopher Faulet99daf282018-11-28 22:58:13 +01002655 return 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002656}
2657
Christopher Faulet92d34fe2019-12-17 09:20:34 +01002658/* Replace all headers matching the name <name>. The header value is replaced if
2659 * it matches the regex <re>. <str> is used for the replacement. If <full> is
2660 * set to 1, the full-line is matched and replaced. Otherwise, comma-separated
2661 * values are evaluated one by one. It returns 0 on success and -1 on error.
2662 */
2663int http_replace_hdrs(struct stream* s, struct htx *htx, struct ist name,
2664 const char *str, struct my_regex *re, int full)
Christopher Faulet72333522018-10-24 11:25:02 +02002665{
2666 struct http_hdr_ctx ctx;
2667 struct buffer *output = get_trash_chunk();
2668
Christopher Faulet72333522018-10-24 11:25:02 +02002669 ctx.blk = NULL;
Christopher Faulet92d34fe2019-12-17 09:20:34 +01002670 while (http_find_header(htx, name, &ctx, full)) {
Christopher Faulet72333522018-10-24 11:25:02 +02002671 if (!regex_exec_match2(re, ctx.value.ptr, ctx.value.len, MAX_MATCH, pmatch, 0))
2672 continue;
2673
2674 output->data = exp_replace(output->area, output->size, ctx.value.ptr, str, pmatch);
2675 if (output->data == -1)
2676 return -1;
2677 if (!http_replace_header_value(htx, &ctx, ist2(output->area, output->data)))
2678 return -1;
2679 }
2680 return 0;
2681}
2682
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002683/* This function executes one of the set-{method,path,query,uri} actions. It
2684 * takes the string from the variable 'replace' with length 'len', then modifies
2685 * the relevant part of the request line accordingly. Then it updates various
2686 * pointers to the next elements which were moved, and the total buffer length.
2687 * It finds the action to be performed in p[2], previously filled by function
2688 * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal
2689 * error, though this can be revisited when this code is finally exploited.
2690 *
2691 * 'action' can be '0' to replace method, '1' to replace path, '2' to replace
Christopher Faulet312294f2020-09-02 17:17:44 +02002692 * query string, 3 to replace uri or 4 to replace the path+query.
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002693 *
2694 * In query string case, the mark question '?' must be set at the start of the
2695 * string by the caller, event if the replacement query string is empty.
2696 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002697int http_req_replace_stline(int action, const char *replace, int len,
2698 struct proxy *px, struct stream *s)
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002699{
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002700 struct htx *htx = htxbuf(&s->req.buf);
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002701
2702 switch (action) {
2703 case 0: // method
2704 if (!http_replace_req_meth(htx, ist2(replace, len)))
2705 return -1;
2706 break;
2707
2708 case 1: // path
Christopher Fauletb8ce5052020-08-31 16:11:57 +02002709 if (!http_replace_req_path(htx, ist2(replace, len), 0))
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002710 return -1;
2711 break;
2712
2713 case 2: // query
2714 if (!http_replace_req_query(htx, ist2(replace, len)))
2715 return -1;
2716 break;
2717
2718 case 3: // uri
2719 if (!http_replace_req_uri(htx, ist2(replace, len)))
2720 return -1;
2721 break;
2722
Christopher Faulet312294f2020-09-02 17:17:44 +02002723 case 4: // path + query
2724 if (!http_replace_req_path(htx, ist2(replace, len), 1))
2725 return -1;
2726 break;
2727
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002728 default:
2729 return -1;
2730 }
2731 return 0;
2732}
2733
2734/* This function replace the HTTP status code and the associated message. The
Christopher Faulete00d06c2019-12-16 17:18:42 +01002735 * variable <status> contains the new status code. This function never fails. It
2736 * returns 0 in case of success, -1 in case of internal error.
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002737 */
Christopher Faulet96bff762019-12-17 13:46:18 +01002738int http_res_set_status(unsigned int status, struct ist reason, struct stream *s)
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002739{
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002740 struct htx *htx = htxbuf(&s->res.buf);
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002741 char *res;
2742
2743 chunk_reset(&trash);
2744 res = ultoa_o(status, trash.area, trash.size);
2745 trash.data = res - trash.area;
2746
2747 /* Do we have a custom reason format string? */
Tim Duesterhuse296d3e2020-03-05 17:56:31 +01002748 if (!isttest(reason)) {
Christopher Faulet96bff762019-12-17 13:46:18 +01002749 const char *str = http_get_reason(status);
Tim Duesterhusdcf753a2021-03-04 17:31:47 +01002750 reason = ist(str);
Christopher Faulet96bff762019-12-17 13:46:18 +01002751 }
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002752
Christopher Fauletbde2c4c2020-08-31 16:43:34 +02002753 if (!http_replace_res_status(htx, ist2(trash.area, trash.data), reason))
Christopher Faulete00d06c2019-12-16 17:18:42 +01002754 return -1;
2755 return 0;
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002756}
2757
Christopher Faulet3e964192018-10-24 11:39:23 +02002758/* Executes the http-request rules <rules> for stream <s>, proxy <px> and
2759 * transaction <txn>. Returns the verdict of the first rule that prevents
2760 * further processing of the request (auth, deny, ...), and defaults to
2761 * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or
2762 * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT
2763 * on txn->flags if it encounters a tarpit rule. If <deny_status> is not NULL
2764 * and a deny/tarpit rule is matched, it will be filled with this rule's deny
2765 * status.
2766 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002767static enum rule_result http_req_get_intercept_rule(struct proxy *px, struct list *rules,
Christopher Fauletb58f62b2020-01-13 16:40:13 +01002768 struct stream *s)
Christopher Faulet3e964192018-10-24 11:39:23 +02002769{
2770 struct session *sess = strm_sess(s);
2771 struct http_txn *txn = s->txn;
Christopher Faulet3e964192018-10-24 11:39:23 +02002772 struct act_rule *rule;
Christopher Faulet3e964192018-10-24 11:39:23 +02002773 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002774 int act_opts = 0;
Christopher Faulet3e964192018-10-24 11:39:23 +02002775
Christopher Faulet3e964192018-10-24 11:39:23 +02002776 /* If "the current_rule_list" match the executed rule list, we are in
2777 * resume condition. If a resume is needed it is always in the action
2778 * and never in the ACL or converters. In this case, we initialise the
2779 * current rule, and go to the action execution point.
2780 */
2781 if (s->current_rule) {
2782 rule = s->current_rule;
2783 s->current_rule = NULL;
2784 if (s->current_rule_list == rules)
2785 goto resume_execution;
2786 }
2787 s->current_rule_list = rules;
2788
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002789 /* start the ruleset evaluation in strict mode */
2790 txn->req.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01002791
Christopher Faulet3e964192018-10-24 11:39:23 +02002792 list_for_each_entry(rule, rules, list) {
2793 /* check optional condition */
2794 if (rule->cond) {
2795 int ret;
2796
2797 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
2798 ret = acl_pass(ret);
2799
2800 if (rule->cond->pol == ACL_COND_UNLESS)
2801 ret = !ret;
2802
2803 if (!ret) /* condition not matched */
2804 continue;
2805 }
2806
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002807 act_opts |= ACT_OPT_FIRST;
Christopher Faulet3e964192018-10-24 11:39:23 +02002808 resume_execution:
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002809 /* Always call the action function if defined */
2810 if (rule->action_ptr) {
2811 if ((s->req.flags & CF_READ_ERROR) ||
2812 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
2813 (px->options & PR_O_ABRT_CLOSE)))
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002814 act_opts |= ACT_OPT_FINAL;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002815
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002816 switch (rule->action_ptr(rule, px, sess, s, act_opts)) {
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002817 case ACT_RET_CONT:
2818 break;
2819 case ACT_RET_STOP:
2820 rule_ret = HTTP_RULE_RES_STOP;
2821 goto end;
2822 case ACT_RET_YIELD:
2823 s->current_rule = rule;
2824 rule_ret = HTTP_RULE_RES_YIELD;
2825 goto end;
2826 case ACT_RET_ERR:
2827 rule_ret = HTTP_RULE_RES_ERROR;
2828 goto end;
2829 case ACT_RET_DONE:
2830 rule_ret = HTTP_RULE_RES_DONE;
2831 goto end;
2832 case ACT_RET_DENY:
Christopher Fauletb58f62b2020-01-13 16:40:13 +01002833 if (txn->status == -1)
2834 txn->status = 403;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002835 rule_ret = HTTP_RULE_RES_DENY;
2836 goto end;
2837 case ACT_RET_ABRT:
2838 rule_ret = HTTP_RULE_RES_ABRT;
2839 goto end;
2840 case ACT_RET_INV:
2841 rule_ret = HTTP_RULE_RES_BADREQ;
2842 goto end;
2843 }
2844 continue; /* eval the next rule */
2845 }
2846
2847 /* If not action function defined, check for known actions */
Christopher Faulet3e964192018-10-24 11:39:23 +02002848 switch (rule->action) {
2849 case ACT_ACTION_ALLOW:
2850 rule_ret = HTTP_RULE_RES_STOP;
2851 goto end;
2852
2853 case ACT_ACTION_DENY:
Christopher Faulet5cb513a2020-05-13 17:56:56 +02002854 txn->status = rule->arg.http_reply->status;
2855 txn->http_reply = rule->arg.http_reply;
Christopher Faulet3e964192018-10-24 11:39:23 +02002856 rule_ret = HTTP_RULE_RES_DENY;
2857 goto end;
2858
2859 case ACT_HTTP_REQ_TARPIT:
2860 txn->flags |= TX_CLTARPIT;
Christopher Faulet5cb513a2020-05-13 17:56:56 +02002861 txn->status = rule->arg.http_reply->status;
2862 txn->http_reply = rule->arg.http_reply;
Christopher Faulet3e964192018-10-24 11:39:23 +02002863 rule_ret = HTTP_RULE_RES_DENY;
2864 goto end;
2865
Christopher Faulet3e964192018-10-24 11:39:23 +02002866 case ACT_HTTP_REDIR:
Christopher Faulet90d22a82020-03-06 11:18:39 +01002867 rule_ret = HTTP_RULE_RES_ABRT;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002868 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
Christopher Faulet3a26bee2019-12-16 12:47:40 +01002869 rule_ret = HTTP_RULE_RES_ERROR;
Christopher Faulet3e964192018-10-24 11:39:23 +02002870 goto end;
2871
2872 case ACT_HTTP_SET_NICE:
Christopher Faulet96bff762019-12-17 13:46:18 +01002873 s->task->nice = rule->arg.http.i;
Christopher Faulet3e964192018-10-24 11:39:23 +02002874 break;
2875
2876 case ACT_HTTP_SET_TOS:
Christopher Faulet96bff762019-12-17 13:46:18 +01002877 conn_set_tos(objt_conn(sess->origin), rule->arg.http.i);
Christopher Faulet3e964192018-10-24 11:39:23 +02002878 break;
2879
2880 case ACT_HTTP_SET_MARK:
Christopher Faulet96bff762019-12-17 13:46:18 +01002881 conn_set_mark(objt_conn(sess->origin), rule->arg.http.i);
Christopher Faulet3e964192018-10-24 11:39:23 +02002882 break;
2883
2884 case ACT_HTTP_SET_LOGL:
Christopher Faulet96bff762019-12-17 13:46:18 +01002885 s->logs.level = rule->arg.http.i;
Christopher Faulet3e964192018-10-24 11:39:23 +02002886 break;
2887
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002888 /* other flags exists, but normally, they never be matched. */
Christopher Faulet3e964192018-10-24 11:39:23 +02002889 default:
2890 break;
2891 }
2892 }
2893
2894 end:
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002895 /* if the ruleset evaluation is finished reset the strict mode */
Christopher Faulet46f95542019-12-20 10:07:22 +01002896 if (rule_ret != HTTP_RULE_RES_YIELD)
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002897 txn->req.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01002898
Christopher Faulet3e964192018-10-24 11:39:23 +02002899 /* we reached the end of the rules, nothing to report */
2900 return rule_ret;
2901}
2902
2903/* Executes the http-response rules <rules> for stream <s> and proxy <px>. It
2904 * returns one of 5 possible statuses: HTTP_RULE_RES_CONT, HTTP_RULE_RES_STOP,
2905 * HTTP_RULE_RES_DONE, HTTP_RULE_RES_YIELD, or HTTP_RULE_RES_BADREQ. If *CONT
2906 * is returned, the process can continue the evaluation of next rule list. If
2907 * *STOP or *DONE is returned, the process must stop the evaluation. If *BADREQ
2908 * is returned, it means the operation could not be processed and a server error
Christopher Fauleta53abad2020-05-13 08:12:22 +02002909 * must be returned. If *YIELD is returned, the caller must call again the
2910 * function with the same context.
Christopher Faulet3e964192018-10-24 11:39:23 +02002911 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002912static enum rule_result http_res_get_intercept_rule(struct proxy *px, struct list *rules,
2913 struct stream *s)
Christopher Faulet3e964192018-10-24 11:39:23 +02002914{
2915 struct session *sess = strm_sess(s);
2916 struct http_txn *txn = s->txn;
Christopher Faulet3e964192018-10-24 11:39:23 +02002917 struct act_rule *rule;
Christopher Faulet3e964192018-10-24 11:39:23 +02002918 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002919 int act_opts = 0;
Christopher Faulet3e964192018-10-24 11:39:23 +02002920
Christopher Faulet3e964192018-10-24 11:39:23 +02002921 /* If "the current_rule_list" match the executed rule list, we are in
2922 * resume condition. If a resume is needed it is always in the action
2923 * and never in the ACL or converters. In this case, we initialise the
2924 * current rule, and go to the action execution point.
2925 */
2926 if (s->current_rule) {
2927 rule = s->current_rule;
2928 s->current_rule = NULL;
2929 if (s->current_rule_list == rules)
2930 goto resume_execution;
2931 }
2932 s->current_rule_list = rules;
2933
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002934 /* start the ruleset evaluation in strict mode */
2935 txn->rsp.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01002936
Christopher Faulet3e964192018-10-24 11:39:23 +02002937 list_for_each_entry(rule, rules, list) {
2938 /* check optional condition */
2939 if (rule->cond) {
2940 int ret;
2941
2942 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
2943 ret = acl_pass(ret);
2944
2945 if (rule->cond->pol == ACL_COND_UNLESS)
2946 ret = !ret;
2947
2948 if (!ret) /* condition not matched */
2949 continue;
2950 }
2951
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002952 act_opts |= ACT_OPT_FIRST;
Christopher Faulet3e964192018-10-24 11:39:23 +02002953resume_execution:
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002954
2955 /* Always call the action function if defined */
2956 if (rule->action_ptr) {
2957 if ((s->req.flags & CF_READ_ERROR) ||
2958 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
2959 (px->options & PR_O_ABRT_CLOSE)))
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002960 act_opts |= ACT_OPT_FINAL;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002961
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002962 switch (rule->action_ptr(rule, px, sess, s, act_opts)) {
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002963 case ACT_RET_CONT:
2964 break;
2965 case ACT_RET_STOP:
2966 rule_ret = HTTP_RULE_RES_STOP;
2967 goto end;
2968 case ACT_RET_YIELD:
2969 s->current_rule = rule;
2970 rule_ret = HTTP_RULE_RES_YIELD;
2971 goto end;
2972 case ACT_RET_ERR:
2973 rule_ret = HTTP_RULE_RES_ERROR;
2974 goto end;
2975 case ACT_RET_DONE:
2976 rule_ret = HTTP_RULE_RES_DONE;
2977 goto end;
2978 case ACT_RET_DENY:
Christopher Fauletb58f62b2020-01-13 16:40:13 +01002979 if (txn->status == -1)
2980 txn->status = 502;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002981 rule_ret = HTTP_RULE_RES_DENY;
2982 goto end;
2983 case ACT_RET_ABRT:
2984 rule_ret = HTTP_RULE_RES_ABRT;
2985 goto end;
2986 case ACT_RET_INV:
2987 rule_ret = HTTP_RULE_RES_BADREQ;
2988 goto end;
2989 }
2990 continue; /* eval the next rule */
2991 }
2992
2993 /* If not action function defined, check for known actions */
Christopher Faulet3e964192018-10-24 11:39:23 +02002994 switch (rule->action) {
2995 case ACT_ACTION_ALLOW:
2996 rule_ret = HTTP_RULE_RES_STOP; /* "allow" rules are OK */
2997 goto end;
2998
2999 case ACT_ACTION_DENY:
Christopher Faulet5cb513a2020-05-13 17:56:56 +02003000 txn->status = rule->arg.http_reply->status;
3001 txn->http_reply = rule->arg.http_reply;
Christopher Faulet3a26bee2019-12-16 12:47:40 +01003002 rule_ret = HTTP_RULE_RES_DENY;
Christopher Faulet3e964192018-10-24 11:39:23 +02003003 goto end;
3004
3005 case ACT_HTTP_SET_NICE:
Christopher Faulet96bff762019-12-17 13:46:18 +01003006 s->task->nice = rule->arg.http.i;
Christopher Faulet3e964192018-10-24 11:39:23 +02003007 break;
3008
3009 case ACT_HTTP_SET_TOS:
Christopher Faulet96bff762019-12-17 13:46:18 +01003010 conn_set_tos(objt_conn(sess->origin), rule->arg.http.i);
Christopher Faulet3e964192018-10-24 11:39:23 +02003011 break;
3012
3013 case ACT_HTTP_SET_MARK:
Christopher Faulet96bff762019-12-17 13:46:18 +01003014 conn_set_mark(objt_conn(sess->origin), rule->arg.http.i);
Christopher Faulet3e964192018-10-24 11:39:23 +02003015 break;
3016
3017 case ACT_HTTP_SET_LOGL:
Christopher Faulet96bff762019-12-17 13:46:18 +01003018 s->logs.level = rule->arg.http.i;
Christopher Faulet3e964192018-10-24 11:39:23 +02003019 break;
3020
Christopher Faulet3e964192018-10-24 11:39:23 +02003021 case ACT_HTTP_REDIR:
Christopher Faulet49c2a702020-03-06 15:44:37 +01003022 rule_ret = HTTP_RULE_RES_ABRT;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003023 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
Christopher Faulet3a26bee2019-12-16 12:47:40 +01003024 rule_ret = HTTP_RULE_RES_ERROR;
Christopher Faulet3e964192018-10-24 11:39:23 +02003025 goto end;
3026
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01003027 /* other flags exists, but normally, they never be matched. */
Christopher Faulet3e964192018-10-24 11:39:23 +02003028 default:
3029 break;
3030 }
3031 }
3032
3033 end:
Christopher Faulet1aea50e2020-01-17 16:03:53 +01003034 /* if the ruleset evaluation is finished reset the strict mode */
Christopher Faulet46f95542019-12-20 10:07:22 +01003035 if (rule_ret != HTTP_RULE_RES_YIELD)
Christopher Faulet1aea50e2020-01-17 16:03:53 +01003036 txn->rsp.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01003037
Christopher Faulet3e964192018-10-24 11:39:23 +02003038 /* we reached the end of the rules, nothing to report */
3039 return rule_ret;
3040}
3041
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01003042/* Executes backend and frontend http-after-response rules for the stream <s>,
3043 * in that order. it return 1 on success and 0 on error. It is the caller
3044 * responsibility to catch error or ignore it. If it catches it, this function
3045 * may be called a second time, for the internal error.
3046 */
3047int http_eval_after_res_rules(struct stream *s)
3048{
3049 struct session *sess = s->sess;
3050 enum rule_result ret = HTTP_RULE_RES_CONT;
3051
Christopher Faulet507479b2020-05-15 12:29:46 +02003052 /* Eval after-response ruleset only if the reply is not const */
3053 if (s->txn->flags & TX_CONST_REPLY)
3054 goto end;
3055
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01003056 /* prune the request variables if not already done and swap to the response variables. */
3057 if (s->vars_reqres.scope != SCOPE_RES) {
3058 if (!LIST_ISEMPTY(&s->vars_reqres.head))
3059 vars_prune(&s->vars_reqres, s->sess, s);
3060 vars_init(&s->vars_reqres, SCOPE_RES);
3061 }
3062
3063 ret = http_res_get_intercept_rule(s->be, &s->be->http_after_res_rules, s);
3064 if ((ret == HTTP_RULE_RES_CONT || ret == HTTP_RULE_RES_STOP) && sess->fe != s->be)
3065 ret = http_res_get_intercept_rule(sess->fe, &sess->fe->http_after_res_rules, s);
3066
Christopher Faulet507479b2020-05-15 12:29:46 +02003067 end:
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01003068 /* All other codes than CONTINUE, STOP or DONE are forbidden */
3069 return (ret == HTTP_RULE_RES_CONT || ret == HTTP_RULE_RES_STOP || ret == HTTP_RULE_RES_DONE);
3070}
3071
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003072/*
3073 * Manage client-side cookie. It can impact performance by about 2% so it is
3074 * desirable to call it only when needed. This code is quite complex because
3075 * of the multiple very crappy and ambiguous syntaxes we have to support. it
3076 * highly recommended not to touch this part without a good reason !
3077 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003078static void http_manage_client_side_cookies(struct stream *s, struct channel *req)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003079{
3080 struct session *sess = s->sess;
3081 struct http_txn *txn = s->txn;
3082 struct htx *htx;
3083 struct http_hdr_ctx ctx;
3084 char *hdr_beg, *hdr_end, *del_from;
3085 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
3086 int preserve_hdr;
3087
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003088 htx = htxbuf(&req->buf);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003089 ctx.blk = NULL;
3090 while (http_find_header(htx, ist("Cookie"), &ctx, 1)) {
Olivier Houchardf0f42382019-07-22 17:43:46 +02003091 int is_first = 1;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003092 del_from = NULL; /* nothing to be deleted */
3093 preserve_hdr = 0; /* assume we may kill the whole header */
3094
3095 /* Now look for cookies. Conforming to RFC2109, we have to support
3096 * attributes whose name begin with a '$', and associate them with
3097 * the right cookie, if we want to delete this cookie.
3098 * So there are 3 cases for each cookie read :
3099 * 1) it's a special attribute, beginning with a '$' : ignore it.
3100 * 2) it's a server id cookie that we *MAY* want to delete : save
3101 * some pointers on it (last semi-colon, beginning of cookie...)
3102 * 3) it's an application cookie : we *MAY* have to delete a previous
3103 * "special" cookie.
3104 * At the end of loop, if a "special" cookie remains, we may have to
3105 * remove it. If no application cookie persists in the header, we
3106 * *MUST* delete it.
3107 *
3108 * Note: RFC2965 is unclear about the processing of spaces around
3109 * the equal sign in the ATTR=VALUE form. A careful inspection of
3110 * the RFC explicitly allows spaces before it, and not within the
3111 * tokens (attrs or values). An inspection of RFC2109 allows that
3112 * too but section 10.1.3 lets one think that spaces may be allowed
3113 * after the equal sign too, resulting in some (rare) buggy
3114 * implementations trying to do that. So let's do what servers do.
3115 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
3116 * allowed quoted strings in values, with any possible character
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003117 * after a backslash, including control chars and delimiters, which
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003118 * causes parsing to become ambiguous. Browsers also allow spaces
3119 * within values even without quotes.
3120 *
3121 * We have to keep multiple pointers in order to support cookie
3122 * removal at the beginning, middle or end of header without
3123 * corrupting the header. All of these headers are valid :
3124 *
3125 * hdr_beg hdr_end
3126 * | |
3127 * v |
3128 * NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3 |
3129 * NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3 v
3130 * NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3
3131 * | | | | | | |
3132 * | | | | | | |
3133 * | | | | | | +--> next
3134 * | | | | | +----> val_end
3135 * | | | | +-----------> val_beg
3136 * | | | +--------------> equal
3137 * | | +----------------> att_end
3138 * | +---------------------> att_beg
3139 * +--------------------------> prev
3140 *
3141 */
3142 hdr_beg = ctx.value.ptr;
3143 hdr_end = hdr_beg + ctx.value.len;
3144 for (prev = hdr_beg; prev < hdr_end; prev = next) {
3145 /* Iterate through all cookies on this line */
3146
3147 /* find att_beg */
3148 att_beg = prev;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003149 if (!is_first)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003150 att_beg++;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003151 is_first = 0;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003152
3153 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
3154 att_beg++;
3155
3156 /* find att_end : this is the first character after the last non
3157 * space before the equal. It may be equal to hdr_end.
3158 */
3159 equal = att_end = att_beg;
3160 while (equal < hdr_end) {
3161 if (*equal == '=' || *equal == ',' || *equal == ';')
3162 break;
3163 if (HTTP_IS_SPHT(*equal++))
3164 continue;
3165 att_end = equal;
3166 }
3167
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003168 /* here, <equal> points to '=', a delimiter or the end. <att_end>
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003169 * is between <att_beg> and <equal>, both may be identical.
3170 */
3171 /* look for end of cookie if there is an equal sign */
3172 if (equal < hdr_end && *equal == '=') {
3173 /* look for the beginning of the value */
3174 val_beg = equal + 1;
3175 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
3176 val_beg++;
3177
3178 /* find the end of the value, respecting quotes */
3179 next = http_find_cookie_value_end(val_beg, hdr_end);
3180
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003181 /* make val_end point to the first white space or delimiter after the value */
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003182 val_end = next;
3183 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
3184 val_end--;
3185 }
3186 else
3187 val_beg = val_end = next = equal;
3188
3189 /* We have nothing to do with attributes beginning with
3190 * '$'. However, they will automatically be removed if a
3191 * header before them is removed, since they're supposed
3192 * to be linked together.
3193 */
3194 if (*att_beg == '$')
3195 continue;
3196
3197 /* Ignore cookies with no equal sign */
3198 if (equal == next) {
3199 /* This is not our cookie, so we must preserve it. But if we already
3200 * scheduled another cookie for removal, we cannot remove the
3201 * complete header, but we can remove the previous block itself.
3202 */
3203 preserve_hdr = 1;
3204 if (del_from != NULL) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003205 int delta = http_del_hdr_value(hdr_beg, hdr_end, &del_from, prev);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003206 val_end += delta;
3207 next += delta;
3208 hdr_end += delta;
3209 prev = del_from;
3210 del_from = NULL;
3211 }
3212 continue;
3213 }
3214
3215 /* if there are spaces around the equal sign, we need to
3216 * strip them otherwise we'll get trouble for cookie captures,
3217 * or even for rewrites. Since this happens extremely rarely,
3218 * it does not hurt performance.
3219 */
3220 if (unlikely(att_end != equal || val_beg > equal + 1)) {
3221 int stripped_before = 0;
3222 int stripped_after = 0;
3223
3224 if (att_end != equal) {
3225 memmove(att_end, equal, hdr_end - equal);
3226 stripped_before = (att_end - equal);
3227 equal += stripped_before;
3228 val_beg += stripped_before;
3229 }
3230
3231 if (val_beg > equal + 1) {
3232 memmove(equal + 1, val_beg, hdr_end + stripped_before - val_beg);
3233 stripped_after = (equal + 1) - val_beg;
3234 val_beg += stripped_after;
3235 stripped_before += stripped_after;
3236 }
3237
3238 val_end += stripped_before;
3239 next += stripped_before;
3240 hdr_end += stripped_before;
3241 }
3242 /* now everything is as on the diagram above */
3243
3244 /* First, let's see if we want to capture this cookie. We check
3245 * that we don't already have a client side cookie, because we
3246 * can only capture one. Also as an optimisation, we ignore
3247 * cookies shorter than the declared name.
3248 */
3249 if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL &&
3250 (val_end - att_beg >= sess->fe->capture_namelen) &&
3251 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
3252 int log_len = val_end - att_beg;
3253
3254 if ((txn->cli_cookie = pool_alloc(pool_head_capture)) == NULL) {
3255 ha_alert("HTTP logging : out of memory.\n");
3256 } else {
3257 if (log_len > sess->fe->capture_len)
3258 log_len = sess->fe->capture_len;
3259 memcpy(txn->cli_cookie, att_beg, log_len);
3260 txn->cli_cookie[log_len] = 0;
3261 }
3262 }
3263
3264 /* Persistence cookies in passive, rewrite or insert mode have the
3265 * following form :
3266 *
3267 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
3268 *
3269 * For cookies in prefix mode, the form is :
3270 *
3271 * Cookie: NAME=SRV~VALUE
3272 */
3273 if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
3274 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
3275 struct server *srv = s->be->srv;
3276 char *delim;
3277
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003278 /* if we're in cookie prefix mode, we'll search the delimiter so that we
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003279 * have the server ID between val_beg and delim, and the original cookie between
3280 * delim+1 and val_end. Otherwise, delim==val_end :
3281 *
3282 * hdr_beg
3283 * |
3284 * v
3285 * NAME=SRV; # in all but prefix modes
3286 * NAME=SRV~OPAQUE ; # in prefix mode
3287 * || || | |+-> next
3288 * || || | +--> val_end
3289 * || || +---------> delim
3290 * || |+------------> val_beg
3291 * || +-------------> att_end = equal
3292 * |+-----------------> att_beg
3293 * +------------------> prev
3294 *
3295 */
3296 if (s->be->ck_opts & PR_CK_PFX) {
3297 for (delim = val_beg; delim < val_end; delim++)
3298 if (*delim == COOKIE_DELIM)
3299 break;
3300 }
3301 else {
3302 char *vbar1;
3303 delim = val_end;
3304 /* Now check if the cookie contains a date field, which would
3305 * appear after a vertical bar ('|') just after the server name
3306 * and before the delimiter.
3307 */
3308 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
3309 if (vbar1) {
3310 /* OK, so left of the bar is the server's cookie and
3311 * right is the last seen date. It is a base64 encoded
3312 * 30-bit value representing the UNIX date since the
3313 * epoch in 4-second quantities.
3314 */
3315 int val;
3316 delim = vbar1++;
3317 if (val_end - vbar1 >= 5) {
3318 val = b64tos30(vbar1);
3319 if (val > 0)
3320 txn->cookie_last_date = val << 2;
3321 }
3322 /* look for a second vertical bar */
3323 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
3324 if (vbar1 && (val_end - vbar1 > 5)) {
3325 val = b64tos30(vbar1 + 1);
3326 if (val > 0)
3327 txn->cookie_first_date = val << 2;
3328 }
3329 }
3330 }
3331
3332 /* if the cookie has an expiration date and the proxy wants to check
3333 * it, then we do that now. We first check if the cookie is too old,
3334 * then only if it has expired. We detect strict overflow because the
3335 * time resolution here is not great (4 seconds). Cookies with dates
3336 * in the future are ignored if their offset is beyond one day. This
3337 * allows an admin to fix timezone issues without expiring everyone
3338 * and at the same time avoids keeping unwanted side effects for too
3339 * long.
3340 */
3341 if (txn->cookie_first_date && s->be->cookie_maxlife &&
3342 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) ||
3343 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
3344 txn->flags &= ~TX_CK_MASK;
3345 txn->flags |= TX_CK_OLD;
3346 delim = val_beg; // let's pretend we have not found the cookie
3347 txn->cookie_first_date = 0;
3348 txn->cookie_last_date = 0;
3349 }
3350 else if (txn->cookie_last_date && s->be->cookie_maxidle &&
3351 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) ||
3352 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
3353 txn->flags &= ~TX_CK_MASK;
3354 txn->flags |= TX_CK_EXPIRED;
3355 delim = val_beg; // let's pretend we have not found the cookie
3356 txn->cookie_first_date = 0;
3357 txn->cookie_last_date = 0;
3358 }
3359
3360 /* Here, we'll look for the first running server which supports the cookie.
3361 * This allows to share a same cookie between several servers, for example
3362 * to dedicate backup servers to specific servers only.
3363 * However, to prevent clients from sticking to cookie-less backup server
3364 * when they have incidentely learned an empty cookie, we simply ignore
3365 * empty cookies and mark them as invalid.
3366 * The same behaviour is applied when persistence must be ignored.
3367 */
3368 if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
3369 srv = NULL;
3370
3371 while (srv) {
3372 if (srv->cookie && (srv->cklen == delim - val_beg) &&
3373 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
3374 if ((srv->cur_state != SRV_ST_STOPPED) ||
3375 (s->be->options & PR_O_PERSIST) ||
3376 (s->flags & SF_FORCE_PRST)) {
3377 /* we found the server and we can use it */
3378 txn->flags &= ~TX_CK_MASK;
3379 txn->flags |= (srv->cur_state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
3380 s->flags |= SF_DIRECT | SF_ASSIGNED;
3381 s->target = &srv->obj_type;
3382 break;
3383 } else {
3384 /* we found a server, but it's down,
3385 * mark it as such and go on in case
3386 * another one is available.
3387 */
3388 txn->flags &= ~TX_CK_MASK;
3389 txn->flags |= TX_CK_DOWN;
3390 }
3391 }
3392 srv = srv->next;
3393 }
3394
3395 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
3396 /* no server matched this cookie or we deliberately skipped it */
3397 txn->flags &= ~TX_CK_MASK;
3398 if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
3399 txn->flags |= TX_CK_UNUSED;
3400 else
3401 txn->flags |= TX_CK_INVALID;
3402 }
3403
3404 /* depending on the cookie mode, we may have to either :
3405 * - delete the complete cookie if we're in insert+indirect mode, so that
3406 * the server never sees it ;
3407 * - remove the server id from the cookie value, and tag the cookie as an
Joseph Herlante9d5c722018-11-25 11:00:25 -08003408 * application cookie so that it does not get accidentally removed later,
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003409 * if we're in cookie prefix mode
3410 */
3411 if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
3412 int delta; /* negative */
3413
3414 memmove(val_beg, delim + 1, hdr_end - (delim + 1));
3415 delta = val_beg - (delim + 1);
3416 val_end += delta;
3417 next += delta;
3418 hdr_end += delta;
3419 del_from = NULL;
3420 preserve_hdr = 1; /* we want to keep this cookie */
3421 }
3422 else if (del_from == NULL &&
3423 (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
3424 del_from = prev;
3425 }
3426 }
3427 else {
3428 /* This is not our cookie, so we must preserve it. But if we already
3429 * scheduled another cookie for removal, we cannot remove the
3430 * complete header, but we can remove the previous block itself.
3431 */
3432 preserve_hdr = 1;
3433
3434 if (del_from != NULL) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003435 int delta = http_del_hdr_value(hdr_beg, hdr_end, &del_from, prev);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003436 if (att_beg >= del_from)
3437 att_beg += delta;
3438 if (att_end >= del_from)
3439 att_end += delta;
3440 val_beg += delta;
3441 val_end += delta;
3442 next += delta;
3443 hdr_end += delta;
3444 prev = del_from;
3445 del_from = NULL;
3446 }
3447 }
3448
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003449 } /* for each cookie */
3450
3451
3452 /* There are no more cookies on this line.
3453 * We may still have one (or several) marked for deletion at the
3454 * end of the line. We must do this now in two ways :
3455 * - if some cookies must be preserved, we only delete from the
3456 * mark to the end of line ;
3457 * - if nothing needs to be preserved, simply delete the whole header
3458 */
3459 if (del_from) {
3460 hdr_end = (preserve_hdr ? del_from : hdr_beg);
3461 }
3462 if ((hdr_end - hdr_beg) != ctx.value.len) {
Christopher Faulet3e2638e2019-06-18 09:49:16 +02003463 if (hdr_beg != hdr_end)
3464 htx_change_blk_value_len(htx, ctx.blk, hdr_end - hdr_beg);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003465 else
3466 http_remove_header(htx, &ctx);
3467 }
3468 } /* for each "Cookie header */
3469}
3470
3471/*
3472 * Manage server-side cookies. It can impact performance by about 2% so it is
3473 * desirable to call it only when needed. This function is also used when we
3474 * just need to know if there is a cookie (eg: for check-cache).
3475 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003476static void http_manage_server_side_cookies(struct stream *s, struct channel *res)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003477{
3478 struct session *sess = s->sess;
3479 struct http_txn *txn = s->txn;
3480 struct htx *htx;
3481 struct http_hdr_ctx ctx;
3482 struct server *srv;
3483 char *hdr_beg, *hdr_end;
3484 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau6f7a02a2019-04-15 21:49:49 +02003485 int is_cookie2 = 0;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003486
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003487 htx = htxbuf(&res->buf);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003488
3489 ctx.blk = NULL;
3490 while (1) {
Olivier Houchardf0f42382019-07-22 17:43:46 +02003491 int is_first = 1;
3492
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003493 if (!http_find_header(htx, ist("Set-Cookie"), &ctx, 1)) {
3494 if (!http_find_header(htx, ist("Set-Cookie2"), &ctx, 1))
3495 break;
3496 is_cookie2 = 1;
3497 }
3498
3499 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
3500 * <prev> points to the colon.
3501 */
3502 txn->flags |= TX_SCK_PRESENT;
3503
3504 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
3505 * check-cache is enabled) and we are not interested in checking
3506 * them. Warning, the cookie capture is declared in the frontend.
3507 */
3508 if (s->be->cookie_name == NULL && sess->fe->capture_name == NULL)
3509 break;
3510
3511 /* OK so now we know we have to process this response cookie.
3512 * The format of the Set-Cookie header is slightly different
3513 * from the format of the Cookie header in that it does not
3514 * support the comma as a cookie delimiter (thus the header
3515 * cannot be folded) because the Expires attribute described in
3516 * the original Netscape's spec may contain an unquoted date
3517 * with a comma inside. We have to live with this because
3518 * many browsers don't support Max-Age and some browsers don't
3519 * support quoted strings. However the Set-Cookie2 header is
3520 * clean.
3521 *
3522 * We have to keep multiple pointers in order to support cookie
3523 * removal at the beginning, middle or end of header without
3524 * corrupting the header (in case of set-cookie2). A special
3525 * pointer, <scav> points to the beginning of the set-cookie-av
3526 * fields after the first semi-colon. The <next> pointer points
3527 * either to the end of line (set-cookie) or next unquoted comma
3528 * (set-cookie2). All of these headers are valid :
3529 *
3530 * hdr_beg hdr_end
3531 * | |
3532 * v |
3533 * NAME1 = VALUE 1 ; Secure; Path="/" |
3534 * NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT v
3535 * NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT
3536 * NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard
3537 * | | | | | | | |
3538 * | | | | | | | +-> next
3539 * | | | | | | +------------> scav
3540 * | | | | | +--------------> val_end
3541 * | | | | +--------------------> val_beg
3542 * | | | +----------------------> equal
3543 * | | +------------------------> att_end
3544 * | +----------------------------> att_beg
3545 * +------------------------------> prev
3546 * -------------------------------> hdr_beg
3547 */
3548 hdr_beg = ctx.value.ptr;
3549 hdr_end = hdr_beg + ctx.value.len;
3550 for (prev = hdr_beg; prev < hdr_end; prev = next) {
3551
3552 /* Iterate through all cookies on this line */
3553
3554 /* find att_beg */
3555 att_beg = prev;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003556 if (!is_first)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003557 att_beg++;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003558 is_first = 0;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003559
3560 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
3561 att_beg++;
3562
3563 /* find att_end : this is the first character after the last non
3564 * space before the equal. It may be equal to hdr_end.
3565 */
3566 equal = att_end = att_beg;
3567
3568 while (equal < hdr_end) {
3569 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
3570 break;
3571 if (HTTP_IS_SPHT(*equal++))
3572 continue;
3573 att_end = equal;
3574 }
3575
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003576 /* here, <equal> points to '=', a delimiter or the end. <att_end>
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003577 * is between <att_beg> and <equal>, both may be identical.
3578 */
3579
3580 /* look for end of cookie if there is an equal sign */
3581 if (equal < hdr_end && *equal == '=') {
3582 /* look for the beginning of the value */
3583 val_beg = equal + 1;
3584 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
3585 val_beg++;
3586
3587 /* find the end of the value, respecting quotes */
3588 next = http_find_cookie_value_end(val_beg, hdr_end);
3589
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003590 /* make val_end point to the first white space or delimiter after the value */
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003591 val_end = next;
3592 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
3593 val_end--;
3594 }
3595 else {
3596 /* <equal> points to next comma, semi-colon or EOL */
3597 val_beg = val_end = next = equal;
3598 }
3599
3600 if (next < hdr_end) {
3601 /* Set-Cookie2 supports multiple cookies, and <next> points to
3602 * a colon or semi-colon before the end. So skip all attr-value
3603 * pairs and look for the next comma. For Set-Cookie, since
3604 * commas are permitted in values, skip to the end.
3605 */
3606 if (is_cookie2)
3607 next = http_find_hdr_value_end(next, hdr_end);
3608 else
3609 next = hdr_end;
3610 }
3611
3612 /* Now everything is as on the diagram above */
3613
3614 /* Ignore cookies with no equal sign */
3615 if (equal == val_end)
3616 continue;
3617
3618 /* If there are spaces around the equal sign, we need to
3619 * strip them otherwise we'll get trouble for cookie captures,
3620 * or even for rewrites. Since this happens extremely rarely,
3621 * it does not hurt performance.
3622 */
3623 if (unlikely(att_end != equal || val_beg > equal + 1)) {
3624 int stripped_before = 0;
3625 int stripped_after = 0;
3626
3627 if (att_end != equal) {
3628 memmove(att_end, equal, hdr_end - equal);
3629 stripped_before = (att_end - equal);
3630 equal += stripped_before;
3631 val_beg += stripped_before;
3632 }
3633
3634 if (val_beg > equal + 1) {
3635 memmove(equal + 1, val_beg, hdr_end + stripped_before - val_beg);
3636 stripped_after = (equal + 1) - val_beg;
3637 val_beg += stripped_after;
3638 stripped_before += stripped_after;
3639 }
3640
3641 val_end += stripped_before;
3642 next += stripped_before;
3643 hdr_end += stripped_before;
3644
Christopher Faulet3e2638e2019-06-18 09:49:16 +02003645 htx_change_blk_value_len(htx, ctx.blk, hdr_end - hdr_beg);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003646 ctx.value.len = hdr_end - hdr_beg;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003647 }
3648
3649 /* First, let's see if we want to capture this cookie. We check
3650 * that we don't already have a server side cookie, because we
3651 * can only capture one. Also as an optimisation, we ignore
3652 * cookies shorter than the declared name.
3653 */
3654 if (sess->fe->capture_name != NULL &&
3655 txn->srv_cookie == NULL &&
3656 (val_end - att_beg >= sess->fe->capture_namelen) &&
3657 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
3658 int log_len = val_end - att_beg;
3659 if ((txn->srv_cookie = pool_alloc(pool_head_capture)) == NULL) {
3660 ha_alert("HTTP logging : out of memory.\n");
3661 }
3662 else {
3663 if (log_len > sess->fe->capture_len)
3664 log_len = sess->fe->capture_len;
3665 memcpy(txn->srv_cookie, att_beg, log_len);
3666 txn->srv_cookie[log_len] = 0;
3667 }
3668 }
3669
3670 srv = objt_server(s->target);
3671 /* now check if we need to process it for persistence */
3672 if (!(s->flags & SF_IGNORE_PRST) &&
3673 (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
3674 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
3675 /* assume passive cookie by default */
3676 txn->flags &= ~TX_SCK_MASK;
3677 txn->flags |= TX_SCK_FOUND;
3678
3679 /* If the cookie is in insert mode on a known server, we'll delete
3680 * this occurrence because we'll insert another one later.
3681 * We'll delete it too if the "indirect" option is set and we're in
3682 * a direct access.
3683 */
3684 if (s->be->ck_opts & PR_CK_PSV) {
3685 /* The "preserve" flag was set, we don't want to touch the
3686 * server's cookie.
3687 */
3688 }
3689 else if ((srv && (s->be->ck_opts & PR_CK_INS)) ||
3690 ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) {
3691 /* this cookie must be deleted */
3692 if (prev == hdr_beg && next == hdr_end) {
3693 /* whole header */
3694 http_remove_header(htx, &ctx);
3695 /* note: while both invalid now, <next> and <hdr_end>
3696 * are still equal, so the for() will stop as expected.
3697 */
3698 } else {
3699 /* just remove the value */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003700 int delta = http_del_hdr_value(hdr_beg, hdr_end, &prev, next);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003701 next = prev;
3702 hdr_end += delta;
3703 }
3704 txn->flags &= ~TX_SCK_MASK;
3705 txn->flags |= TX_SCK_DELETED;
3706 /* and go on with next cookie */
3707 }
3708 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) {
3709 /* replace bytes val_beg->val_end with the cookie name associated
3710 * with this server since we know it.
3711 */
3712 int sliding, delta;
3713
3714 ctx.value = ist2(val_beg, val_end - val_beg);
3715 ctx.lws_before = ctx.lws_after = 0;
3716 http_replace_header_value(htx, &ctx, ist2(srv->cookie, srv->cklen));
3717 delta = srv->cklen - (val_end - val_beg);
3718 sliding = (ctx.value.ptr - val_beg);
3719 hdr_beg += sliding;
3720 val_beg += sliding;
3721 next += sliding + delta;
3722 hdr_end += sliding + delta;
3723
3724 txn->flags &= ~TX_SCK_MASK;
3725 txn->flags |= TX_SCK_REPLACED;
3726 }
3727 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) {
3728 /* insert the cookie name associated with this server
3729 * before existing cookie, and insert a delimiter between them..
3730 */
3731 int sliding, delta;
3732 ctx.value = ist2(val_beg, 0);
3733 ctx.lws_before = ctx.lws_after = 0;
3734 http_replace_header_value(htx, &ctx, ist2(srv->cookie, srv->cklen + 1));
3735 delta = srv->cklen + 1;
3736 sliding = (ctx.value.ptr - val_beg);
3737 hdr_beg += sliding;
3738 val_beg += sliding;
3739 next += sliding + delta;
3740 hdr_end += sliding + delta;
3741
3742 val_beg[srv->cklen] = COOKIE_DELIM;
3743 txn->flags &= ~TX_SCK_MASK;
3744 txn->flags |= TX_SCK_REPLACED;
3745 }
3746 }
3747 /* that's done for this cookie, check the next one on the same
3748 * line when next != hdr_end (only if is_cookie2).
3749 */
3750 }
3751 }
3752}
3753
Christopher Faulet25a02f62018-10-24 12:00:25 +02003754/*
3755 * Parses the Cache-Control and Pragma request header fields to determine if
3756 * the request may be served from the cache and/or if it is cacheable. Updates
3757 * s->txn->flags.
3758 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003759void http_check_request_for_cacheability(struct stream *s, struct channel *req)
Christopher Faulet25a02f62018-10-24 12:00:25 +02003760{
3761 struct http_txn *txn = s->txn;
3762 struct htx *htx;
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003763 struct http_hdr_ctx ctx = { .blk = NULL };
3764 int pragma_found, cc_found;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003765
3766 if ((txn->flags & (TX_CACHEABLE|TX_CACHE_IGNORE)) == TX_CACHE_IGNORE)
3767 return; /* nothing more to do here */
3768
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003769 htx = htxbuf(&req->buf);
Christopher Faulet25a02f62018-10-24 12:00:25 +02003770 pragma_found = cc_found = 0;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003771
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003772 /* Check "pragma" header for HTTP/1.0 compatibility. */
3773 if (http_find_header(htx, ist("pragma"), &ctx, 1)) {
3774 if (isteqi(ctx.value, ist("no-cache"))) {
3775 pragma_found = 1;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003776 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003777 }
Christopher Faulet25a02f62018-10-24 12:00:25 +02003778
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003779 ctx.blk = NULL;
3780 /* Don't use the cache and don't try to store if we found the
3781 * Authorization header */
3782 if (http_find_header(htx, ist("authorization"), &ctx, 1)) {
3783 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3784 txn->flags |= TX_CACHE_IGNORE;
3785 }
Christopher Faulet25a02f62018-10-24 12:00:25 +02003786
Christopher Faulet25a02f62018-10-24 12:00:25 +02003787
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003788 /* Look for "cache-control" header and iterate over all the values
3789 * until we find one that specifies that caching is possible or not. */
3790 ctx.blk = NULL;
3791 while (http_find_header(htx, ist("cache-control"), &ctx, 0)) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003792 cc_found = 1;
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003793 /* We don't check the values after max-age, max-stale nor min-fresh,
3794 * we simply don't use the cache when they're specified. */
3795 if (istmatchi(ctx.value, ist("max-age")) ||
3796 istmatchi(ctx.value, ist("no-cache")) ||
3797 istmatchi(ctx.value, ist("max-stale")) ||
3798 istmatchi(ctx.value, ist("min-fresh"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003799 txn->flags |= TX_CACHE_IGNORE;
3800 continue;
3801 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003802 if (istmatchi(ctx.value, ist("no-store"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003803 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3804 continue;
3805 }
3806 }
3807
3808 /* RFC7234#5.4:
3809 * When the Cache-Control header field is also present and
3810 * understood in a request, Pragma is ignored.
3811 * When the Cache-Control header field is not present in a
3812 * request, caches MUST consider the no-cache request
3813 * pragma-directive as having the same effect as if
3814 * "Cache-Control: no-cache" were present.
3815 */
3816 if (!cc_found && pragma_found)
3817 txn->flags |= TX_CACHE_IGNORE;
3818}
3819
3820/*
3821 * Check if response is cacheable or not. Updates s->txn->flags.
3822 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003823void http_check_response_for_cacheability(struct stream *s, struct channel *res)
Christopher Faulet25a02f62018-10-24 12:00:25 +02003824{
3825 struct http_txn *txn = s->txn;
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003826 struct http_hdr_ctx ctx = { .blk = NULL };
Christopher Faulet25a02f62018-10-24 12:00:25 +02003827 struct htx *htx;
Remi Tricot-Le Bretoncc9bf2e2020-11-12 11:14:41 +01003828 int has_freshness_info = 0;
3829 int has_validator = 0;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003830
3831 if (txn->status < 200) {
3832 /* do not try to cache interim responses! */
3833 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3834 return;
3835 }
3836
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003837 htx = htxbuf(&res->buf);
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003838 /* Check "pragma" header for HTTP/1.0 compatibility. */
3839 if (http_find_header(htx, ist("pragma"), &ctx, 1)) {
3840 if (isteqi(ctx.value, ist("no-cache"))) {
3841 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3842 return;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003843 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003844 }
Christopher Faulet25a02f62018-10-24 12:00:25 +02003845
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003846 /* Look for "cache-control" header and iterate over all the values
3847 * until we find one that specifies that caching is possible or not. */
3848 ctx.blk = NULL;
3849 while (http_find_header(htx, ist("cache-control"), &ctx, 0)) {
3850 if (isteqi(ctx.value, ist("public"))) {
3851 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003852 continue;
3853 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003854 if (isteqi(ctx.value, ist("private")) ||
3855 isteqi(ctx.value, ist("no-cache")) ||
3856 isteqi(ctx.value, ist("no-store")) ||
3857 isteqi(ctx.value, ist("max-age=0")) ||
3858 isteqi(ctx.value, ist("s-maxage=0"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003859 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003860 continue;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003861 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003862 /* We might have a no-cache="set-cookie" form. */
3863 if (istmatchi(ctx.value, ist("no-cache=\"set-cookie"))) {
3864 txn->flags &= ~TX_CACHE_COOK;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003865 continue;
3866 }
Remi Tricot-Le Bretoncc9bf2e2020-11-12 11:14:41 +01003867
3868 if (istmatchi(ctx.value, ist("s-maxage")) ||
3869 istmatchi(ctx.value, ist("max-age"))) {
3870 has_freshness_info = 1;
3871 continue;
3872 }
3873 }
3874
3875 /* If no freshness information could be found in Cache-Control values,
3876 * look for an Expires header. */
3877 if (!has_freshness_info) {
3878 ctx.blk = NULL;
3879 has_freshness_info = http_find_header(htx, ist("expires"), &ctx, 0);
Christopher Faulet25a02f62018-10-24 12:00:25 +02003880 }
Remi Tricot-Le Bretoncc9bf2e2020-11-12 11:14:41 +01003881
3882 /* If no freshness information could be found in Cache-Control or Expires
3883 * values, look for an explicit validator. */
3884 if (!has_freshness_info) {
3885 ctx.blk = NULL;
3886 has_validator = 1;
3887 if (!http_find_header(htx, ist("etag"), &ctx, 0)) {
3888 ctx.blk = NULL;
3889 if (!http_find_header(htx, ist("last-modified"), &ctx, 0))
3890 has_validator = 0;
3891 }
3892 }
3893
3894 /* We won't store an entry that has neither a cache validator nor an
3895 * explicit expiration time, as suggested in RFC 7234#3. */
3896 if (!has_freshness_info && !has_validator)
3897 txn->flags |= TX_CACHE_IGNORE;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003898}
3899
Christopher Faulet377c5a52018-10-24 21:21:30 +02003900/*
3901 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
3902 * for the current backend.
3903 *
3904 * It is assumed that the request is either a HEAD, GET, or POST and that the
3905 * uri_auth field is valid.
3906 *
3907 * Returns 1 if stats should be provided, otherwise 0.
3908 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003909static int http_stats_check_uri(struct stream *s, struct http_txn *txn, struct proxy *backend)
Christopher Faulet377c5a52018-10-24 21:21:30 +02003910{
3911 struct uri_auth *uri_auth = backend->uri_auth;
3912 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003913 struct htx_sl *sl;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003914 struct ist uri;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003915
3916 if (!uri_auth)
3917 return 0;
3918
3919 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
3920 return 0;
3921
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003922 htx = htxbuf(&s->req.buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02003923 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003924 uri = htx_sl_req_uri(sl);
Willy Tarreau1eb3b482019-10-31 15:50:28 +01003925 if (*uri_auth->uri_prefix == '/')
3926 uri = http_get_path(uri);
Christopher Faulet377c5a52018-10-24 21:21:30 +02003927
3928 /* check URI size */
3929 if (uri_auth->uri_len > uri.len)
3930 return 0;
3931
3932 if (memcmp(uri.ptr, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
3933 return 0;
3934
3935 return 1;
3936}
3937
3938/* This function prepares an applet to handle the stats. It can deal with the
3939 * "100-continue" expectation, check that admin rules are met for POST requests,
3940 * and program a response message if something was unexpected. It cannot fail
3941 * and always relies on the stats applet to complete the job. It does not touch
3942 * analysers nor counters, which are left to the caller. It does not touch
3943 * s->target which is supposed to already point to the stats applet. The caller
3944 * is expected to have already assigned an appctx to the stream.
3945 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003946static int http_handle_stats(struct stream *s, struct channel *req)
Christopher Faulet377c5a52018-10-24 21:21:30 +02003947{
3948 struct stats_admin_rule *stats_admin_rule;
3949 struct stream_interface *si = &s->si[1];
3950 struct session *sess = s->sess;
3951 struct http_txn *txn = s->txn;
3952 struct http_msg *msg = &txn->req;
3953 struct uri_auth *uri_auth = s->be->uri_auth;
3954 const char *h, *lookup, *end;
3955 struct appctx *appctx;
3956 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003957 struct htx_sl *sl;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003958
3959 appctx = si_appctx(si);
3960 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
3961 appctx->st1 = appctx->st2 = 0;
3962 appctx->ctx.stats.st_code = STAT_STATUS_INIT;
Willy Tarreau676c29e2019-10-09 10:50:01 +02003963 appctx->ctx.stats.flags |= uri_auth->flags;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003964 appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */
3965 if ((msg->flags & HTTP_MSGF_VER_11) && (txn->meth != HTTP_METH_HEAD))
3966 appctx->ctx.stats.flags |= STAT_CHUNKED;
3967
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003968 htx = htxbuf(&req->buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02003969 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003970 lookup = HTX_SL_REQ_UPTR(sl) + uri_auth->uri_len;
3971 end = HTX_SL_REQ_UPTR(sl) + HTX_SL_REQ_ULEN(sl);
Christopher Faulet377c5a52018-10-24 21:21:30 +02003972
3973 for (h = lookup; h <= end - 3; h++) {
3974 if (memcmp(h, ";up", 3) == 0) {
3975 appctx->ctx.stats.flags |= STAT_HIDE_DOWN;
3976 break;
3977 }
Amaury Denoyelle91e55ea2021-02-25 14:46:08 +01003978 }
3979
3980 for (h = lookup; h <= end - 9; h++) {
3981 if (memcmp(h, ";no-maint", 9) == 0) {
Willy Tarreau3e320362020-10-23 17:28:57 +02003982 appctx->ctx.stats.flags |= STAT_HIDE_MAINT;
3983 break;
3984 }
Christopher Faulet377c5a52018-10-24 21:21:30 +02003985 }
3986
3987 if (uri_auth->refresh) {
3988 for (h = lookup; h <= end - 10; h++) {
3989 if (memcmp(h, ";norefresh", 10) == 0) {
3990 appctx->ctx.stats.flags |= STAT_NO_REFRESH;
3991 break;
3992 }
3993 }
3994 }
3995
3996 for (h = lookup; h <= end - 4; h++) {
3997 if (memcmp(h, ";csv", 4) == 0) {
Christopher Faulet6338a082019-09-09 15:50:54 +02003998 appctx->ctx.stats.flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
Christopher Faulet377c5a52018-10-24 21:21:30 +02003999 break;
4000 }
4001 }
4002
4003 for (h = lookup; h <= end - 6; h++) {
4004 if (memcmp(h, ";typed", 6) == 0) {
Christopher Faulet6338a082019-09-09 15:50:54 +02004005 appctx->ctx.stats.flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
Christopher Faulet377c5a52018-10-24 21:21:30 +02004006 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
4007 break;
4008 }
4009 }
4010
Christopher Faulet6338a082019-09-09 15:50:54 +02004011 for (h = lookup; h <= end - 5; h++) {
4012 if (memcmp(h, ";json", 5) == 0) {
4013 appctx->ctx.stats.flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
4014 appctx->ctx.stats.flags |= STAT_FMT_JSON;
4015 break;
4016 }
4017 }
4018
4019 for (h = lookup; h <= end - 12; h++) {
4020 if (memcmp(h, ";json-schema", 12) == 0) {
4021 appctx->ctx.stats.flags &= ~STAT_FMT_MASK;
4022 appctx->ctx.stats.flags |= STAT_JSON_SCHM;
4023 break;
4024 }
4025 }
4026
Christopher Faulet377c5a52018-10-24 21:21:30 +02004027 for (h = lookup; h <= end - 8; h++) {
4028 if (memcmp(h, ";st=", 4) == 0) {
4029 int i;
4030 h += 4;
4031 appctx->ctx.stats.st_code = STAT_STATUS_UNKN;
4032 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
4033 if (strncmp(stat_status_codes[i], h, 4) == 0) {
4034 appctx->ctx.stats.st_code = i;
4035 break;
4036 }
4037 }
4038 break;
4039 }
4040 }
4041
4042 appctx->ctx.stats.scope_str = 0;
4043 appctx->ctx.stats.scope_len = 0;
4044 for (h = lookup; h <= end - 8; h++) {
4045 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
4046 int itx = 0;
4047 const char *h2;
4048 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
4049 const char *err;
4050
4051 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
4052 h2 = h;
Christopher Fauleted7a0662019-01-14 11:07:34 +01004053 appctx->ctx.stats.scope_str = h2 - HTX_SL_REQ_UPTR(sl);
4054 while (h < end) {
Christopher Faulet377c5a52018-10-24 21:21:30 +02004055 if (*h == ';' || *h == '&' || *h == ' ')
4056 break;
4057 itx++;
4058 h++;
4059 }
4060
4061 if (itx > STAT_SCOPE_TXT_MAXLEN)
4062 itx = STAT_SCOPE_TXT_MAXLEN;
4063 appctx->ctx.stats.scope_len = itx;
4064
4065 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
4066 memcpy(scope_txt, h2, itx);
4067 scope_txt[itx] = '\0';
4068 err = invalid_char(scope_txt);
4069 if (err) {
4070 /* bad char in search text => clear scope */
4071 appctx->ctx.stats.scope_str = 0;
4072 appctx->ctx.stats.scope_len = 0;
4073 }
4074 break;
4075 }
4076 }
4077
4078 /* now check whether we have some admin rules for this request */
4079 list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) {
4080 int ret = 1;
4081
4082 if (stats_admin_rule->cond) {
4083 ret = acl_exec_cond(stats_admin_rule->cond, s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
4084 ret = acl_pass(ret);
4085 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
4086 ret = !ret;
4087 }
4088
4089 if (ret) {
4090 /* no rule, or the rule matches */
4091 appctx->ctx.stats.flags |= STAT_ADMIN;
4092 break;
4093 }
4094 }
4095
Christopher Faulet5d45e382019-02-27 15:15:23 +01004096 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
4097 appctx->st0 = STAT_HTTP_HEAD;
4098 else if (txn->meth == HTTP_METH_POST) {
Christopher Fauletbd9e8422019-08-15 22:26:48 +02004099 if (appctx->ctx.stats.flags & STAT_ADMIN) {
Christopher Faulet377c5a52018-10-24 21:21:30 +02004100 appctx->st0 = STAT_HTTP_POST;
Christopher Fauletbd9e8422019-08-15 22:26:48 +02004101 if (msg->msg_state < HTTP_MSG_DATA)
4102 req->analysers |= AN_REQ_HTTP_BODY;
4103 }
Christopher Faulet377c5a52018-10-24 21:21:30 +02004104 else {
Christopher Faulet5d45e382019-02-27 15:15:23 +01004105 /* POST without admin level */
Christopher Faulet377c5a52018-10-24 21:21:30 +02004106 appctx->ctx.stats.flags &= ~STAT_CHUNKED;
4107 appctx->ctx.stats.st_code = STAT_STATUS_DENY;
4108 appctx->st0 = STAT_HTTP_LAST;
4109 }
4110 }
4111 else {
Christopher Faulet5d45e382019-02-27 15:15:23 +01004112 /* Unsupported method */
4113 appctx->ctx.stats.flags &= ~STAT_CHUNKED;
4114 appctx->ctx.stats.st_code = STAT_STATUS_IVAL;
4115 appctx->st0 = STAT_HTTP_LAST;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004116 }
4117
4118 s->task->nice = -32; /* small boost for HTTP statistics */
4119 return 1;
4120}
4121
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004122void http_perform_server_redirect(struct stream *s, struct stream_interface *si)
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004123{
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004124 struct channel *req = &s->req;
4125 struct channel *res = &s->res;
4126 struct server *srv;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004127 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004128 struct htx_sl *sl;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004129 struct ist path, location;
4130 unsigned int flags;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004131
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004132 /*
4133 * Create the location
4134 */
4135 chunk_reset(&trash);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004136
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004137 /* 1: add the server's prefix */
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004138 /* special prefix "/" means don't change URL */
4139 srv = __objt_server(s->target);
4140 if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') {
4141 if (!chunk_memcat(&trash, srv->rdr_pfx, srv->rdr_len))
4142 return;
4143 }
4144
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004145 /* 2: add the request Path */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004146 htx = htxbuf(&req->buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02004147 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004148 path = http_get_path(htx_sl_req_uri(sl));
Tim Duesterhused526372020-03-05 17:56:33 +01004149 if (!isttest(path))
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004150 return;
4151
4152 if (!chunk_memcat(&trash, path.ptr, path.len))
4153 return;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004154 location = ist2(trash.area, trash.data);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004155
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004156 /*
4157 * Create the 302 respone
4158 */
4159 htx = htx_from_buf(&res->buf);
4160 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
4161 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
4162 ist("HTTP/1.1"), ist("302"), ist("Found"));
4163 if (!sl)
4164 goto fail;
4165 sl->info.res.status = 302;
4166 s->txn->status = 302;
4167
4168 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) ||
4169 !htx_add_header(htx, ist("Connection"), ist("close")) ||
4170 !htx_add_header(htx, ist("Content-length"), ist("0")) ||
4171 !htx_add_header(htx, ist("Location"), location))
4172 goto fail;
4173
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004174 if (!htx_add_endof(htx, HTX_BLK_EOH))
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004175 goto fail;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004176
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004177 htx->flags |= HTX_FL_EOM;
Christopher Fauletc20afb82020-01-24 19:16:26 +01004178 htx_to_buf(htx, &res->buf);
Christopher Fauleta72a7e42020-01-28 09:28:11 +01004179 if (!http_forward_proxy_resp(s, 1))
4180 goto fail;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004181
4182 /* return without error. */
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004183 si_shutr(si);
4184 si_shutw(si);
4185 si->err_type = SI_ET_NONE;
4186 si->state = SI_ST_CLO;
4187
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004188 if (!(s->flags & SF_ERR_MASK))
4189 s->flags |= SF_ERR_LOCAL;
4190 if (!(s->flags & SF_FINST_MASK))
4191 s->flags |= SF_FINST_C;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004192
4193 /* FIXME: we should increase a counter of redirects per server and per backend. */
4194 srv_inc_sess_ctr(srv);
4195 srv_set_sess_last(srv);
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004196 return;
4197
4198 fail:
4199 /* If an error occurred, remove the incomplete HTTP response from the
4200 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004201 channel_htx_truncate(res, htx);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004202}
4203
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05004204/* This function terminates the request because it was completely analyzed or
Christopher Fauletf2824e62018-10-01 12:12:37 +02004205 * because an error was triggered during the body forwarding.
4206 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004207static void http_end_request(struct stream *s)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004208{
4209 struct channel *chn = &s->req;
4210 struct http_txn *txn = s->txn;
4211
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004212 DBG_TRACE_ENTER(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004213
Christopher Fauletb42a8b62018-11-19 21:59:00 +01004214 if (unlikely(txn->req.msg_state == HTTP_MSG_ERROR ||
4215 txn->rsp.msg_state == HTTP_MSG_ERROR)) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004216 channel_abort(chn);
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004217 channel_htx_truncate(chn, htxbuf(&chn->buf));
Christopher Fauletf2824e62018-10-01 12:12:37 +02004218 goto end;
4219 }
4220
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004221 if (unlikely(txn->req.msg_state < HTTP_MSG_DONE)) {
4222 DBG_TRACE_DEVEL("waiting end of the request", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004223 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004224 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004225
4226 if (txn->req.msg_state == HTTP_MSG_DONE) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004227 /* No need to read anymore, the request was completely parsed.
4228 * We can shut the read side unless we want to abort_on_close,
4229 * or we have a POST request. The issue with POST requests is
4230 * that some browsers still send a CRLF after the request, and
4231 * this CRLF must be read so that it does not remain in the kernel
4232 * buffers, otherwise a close could cause an RST on some systems
4233 * (eg: Linux).
4234 */
Christopher Faulet769d0e92019-03-22 14:23:18 +01004235 if (!(s->be->options & PR_O_ABRT_CLOSE) && txn->meth != HTTP_METH_POST)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004236 channel_dont_read(chn);
4237
4238 /* if the server closes the connection, we want to immediately react
4239 * and close the socket to save packets and syscalls.
4240 */
4241 s->si[1].flags |= SI_FL_NOHALF;
4242
4243 /* In any case we've finished parsing the request so we must
4244 * disable Nagle when sending data because 1) we're not going
4245 * to shut this side, and 2) the server is waiting for us to
4246 * send pending data.
4247 */
4248 chn->flags |= CF_NEVER_WAIT;
4249
Christopher Fauletd01ce402019-01-02 17:44:13 +01004250 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
4251 /* The server has not finished to respond, so we
4252 * don't want to move in order not to upset it.
4253 */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004254 DBG_TRACE_DEVEL("waiting end of the response", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletd01ce402019-01-02 17:44:13 +01004255 return;
4256 }
4257
Christopher Fauletf2824e62018-10-01 12:12:37 +02004258 /* When we get here, it means that both the request and the
4259 * response have finished receiving. Depending on the connection
4260 * mode, we'll have to wait for the last bytes to leave in either
4261 * direction, and sometimes for a close to be effective.
4262 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02004263 if (txn->flags & TX_CON_WANT_TUN) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004264 /* Tunnel mode will not have any analyser so it needs to
4265 * poll for reads.
4266 */
4267 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004268 if (b_data(&chn->buf)) {
4269 DBG_TRACE_DEVEL("waiting to flush the request", STRM_EV_HTTP_ANA, s, txn);
Christopher Faulet9768c262018-10-22 09:34:31 +02004270 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004271 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004272 txn->req.msg_state = HTTP_MSG_TUNNEL;
4273 }
4274 else {
4275 /* we're not expecting any new data to come for this
4276 * transaction, so we can close it.
Christopher Faulet9768c262018-10-22 09:34:31 +02004277 *
4278 * However, there is an exception if the response
4279 * length is undefined. In this case, we need to wait
4280 * the close from the server. The response will be
4281 * switched in TUNNEL mode until the end.
Christopher Fauletf2824e62018-10-01 12:12:37 +02004282 */
4283 if (!(txn->rsp.flags & HTTP_MSGF_XFER_LEN) &&
4284 txn->rsp.msg_state != HTTP_MSG_CLOSED)
Christopher Faulet9768c262018-10-22 09:34:31 +02004285 goto check_channel_flags;
Christopher Fauletf2824e62018-10-01 12:12:37 +02004286
4287 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4288 channel_shutr_now(chn);
4289 channel_shutw_now(chn);
4290 }
4291 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004292 goto check_channel_flags;
4293 }
4294
4295 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
4296 http_msg_closing:
4297 /* nothing else to forward, just waiting for the output buffer
4298 * to be empty and for the shutw_now to take effect.
4299 */
4300 if (channel_is_empty(chn)) {
4301 txn->req.msg_state = HTTP_MSG_CLOSED;
4302 goto http_msg_closed;
4303 }
4304 else if (chn->flags & CF_SHUTW) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004305 txn->req.msg_state = HTTP_MSG_ERROR;
4306 goto end;
4307 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004308 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004309 return;
4310 }
4311
4312 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
4313 http_msg_closed:
Christopher Fauletf2824e62018-10-01 12:12:37 +02004314 /* if we don't know whether the server will close, we need to hard close */
4315 if (txn->rsp.flags & HTTP_MSGF_XFER_LEN)
4316 s->si[1].flags |= SI_FL_NOLINGER; /* we want to close ASAP */
Christopher Fauletf2824e62018-10-01 12:12:37 +02004317 /* see above in MSG_DONE why we only do this in these states */
Christopher Faulet769d0e92019-03-22 14:23:18 +01004318 if (!(s->be->options & PR_O_ABRT_CLOSE))
Christopher Fauletf2824e62018-10-01 12:12:37 +02004319 channel_dont_read(chn);
4320 goto end;
4321 }
4322
4323 check_channel_flags:
4324 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4325 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4326 /* if we've just closed an output, let's switch */
4327 txn->req.msg_state = HTTP_MSG_CLOSING;
4328 goto http_msg_closing;
4329 }
4330
4331 end:
4332 chn->analysers &= AN_REQ_FLT_END;
Christopher Faulet198ef8b2020-12-15 13:32:55 +01004333 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
4334 chn->flags |= CF_NEVER_WAIT;
4335 if (HAS_REQ_DATA_FILTERS(s))
Christopher Fauletf2824e62018-10-01 12:12:37 +02004336 chn->analysers |= AN_REQ_FLT_XFER_DATA;
Christopher Faulet198ef8b2020-12-15 13:32:55 +01004337 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004338 channel_auto_close(chn);
4339 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004340 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004341}
4342
4343
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05004344/* This function terminates the response because it was completely analyzed or
Christopher Fauletf2824e62018-10-01 12:12:37 +02004345 * because an error was triggered during the body forwarding.
4346 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004347static void http_end_response(struct stream *s)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004348{
4349 struct channel *chn = &s->res;
4350 struct http_txn *txn = s->txn;
4351
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004352 DBG_TRACE_ENTER(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004353
Christopher Fauletb42a8b62018-11-19 21:59:00 +01004354 if (unlikely(txn->req.msg_state == HTTP_MSG_ERROR ||
4355 txn->rsp.msg_state == HTTP_MSG_ERROR)) {
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004356 channel_htx_truncate(&s->req, htxbuf(&s->req.buf));
Christopher Faulet9768c262018-10-22 09:34:31 +02004357 channel_abort(&s->req);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004358 goto end;
4359 }
4360
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004361 if (unlikely(txn->rsp.msg_state < HTTP_MSG_DONE)) {
4362 DBG_TRACE_DEVEL("waiting end of the response", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004363 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004364 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004365
4366 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
4367 /* In theory, we don't need to read anymore, but we must
4368 * still monitor the server connection for a possible close
4369 * while the request is being uploaded, so we don't disable
4370 * reading.
4371 */
4372 /* channel_dont_read(chn); */
4373
4374 if (txn->req.msg_state < HTTP_MSG_DONE) {
4375 /* The client seems to still be sending data, probably
4376 * because we got an error response during an upload.
4377 * We have the choice of either breaking the connection
4378 * or letting it pass through. Let's do the later.
4379 */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004380 DBG_TRACE_DEVEL("waiting end of the request", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004381 return;
4382 }
4383
4384 /* When we get here, it means that both the request and the
4385 * response have finished receiving. Depending on the connection
4386 * mode, we'll have to wait for the last bytes to leave in either
4387 * direction, and sometimes for a close to be effective.
4388 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02004389 if (txn->flags & TX_CON_WANT_TUN) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004390 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004391 if (b_data(&chn->buf)) {
4392 DBG_TRACE_DEVEL("waiting to flush the respone", STRM_EV_HTTP_ANA, s, txn);
Christopher Faulet9768c262018-10-22 09:34:31 +02004393 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004394 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004395 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
4396 }
4397 else {
4398 /* we're not expecting any new data to come for this
4399 * transaction, so we can close it.
4400 */
4401 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4402 channel_shutr_now(chn);
4403 channel_shutw_now(chn);
4404 }
4405 }
4406 goto check_channel_flags;
4407 }
4408
4409 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
4410 http_msg_closing:
4411 /* nothing else to forward, just waiting for the output buffer
4412 * to be empty and for the shutw_now to take effect.
4413 */
4414 if (channel_is_empty(chn)) {
4415 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4416 goto http_msg_closed;
4417 }
4418 else if (chn->flags & CF_SHUTW) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004419 txn->rsp.msg_state = HTTP_MSG_ERROR;
Christopher Fauletcff0f732019-12-16 16:13:44 +01004420 _HA_ATOMIC_ADD(&strm_sess(s)->fe->fe_counters.cli_aborts, 1);
Olivier Houcharda798bf52019-03-08 18:52:00 +01004421 _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
William Lallemand36119de2021-03-08 15:26:48 +01004422 if (strm_sess(s)->listener && strm_sess(s)->listener->counters)
Christopher Fauletcff0f732019-12-16 16:13:44 +01004423 _HA_ATOMIC_ADD(&strm_sess(s)->listener->counters->cli_aborts, 1);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004424 if (objt_server(s->target))
Christopher Fauletcff0f732019-12-16 16:13:44 +01004425 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.cli_aborts, 1);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004426 goto end;
4427 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004428 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004429 return;
4430 }
4431
4432 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
4433 http_msg_closed:
4434 /* drop any pending data */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004435 channel_htx_truncate(&s->req, htxbuf(&s->req.buf));
Christopher Faulet9768c262018-10-22 09:34:31 +02004436 channel_abort(&s->req);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004437 goto end;
4438 }
4439
4440 check_channel_flags:
4441 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4442 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4443 /* if we've just closed an output, let's switch */
4444 txn->rsp.msg_state = HTTP_MSG_CLOSING;
4445 goto http_msg_closing;
4446 }
4447
4448 end:
4449 chn->analysers &= AN_RES_FLT_END;
Christopher Faulet198ef8b2020-12-15 13:32:55 +01004450 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
4451 chn->flags |= CF_NEVER_WAIT;
4452 if (HAS_RSP_DATA_FILTERS(s))
4453 chn->analysers |= AN_RES_FLT_XFER_DATA;
4454 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004455 channel_auto_close(chn);
4456 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004457 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004458}
4459
Christopher Fauletef70e252020-01-28 09:26:19 +01004460/* Forward a response generated by HAProxy (error/redirect/return). This
4461 * function forwards all pending incoming data. If <final> is set to 0, nothing
4462 * more is performed. It is used for 1xx informational messages. Otherwise, the
Christopher Faulet507479b2020-05-15 12:29:46 +02004463 * transaction is terminated and the request is emptied. On success 1 is
Christopher Faulet40e6b552020-06-25 16:04:50 +02004464 * returned. If an error occurred, 0 is returned. If it fails, this function
4465 * only exits. It is the caller responsibility to do the cleanup.
Christopher Fauletef70e252020-01-28 09:26:19 +01004466 */
4467int http_forward_proxy_resp(struct stream *s, int final)
4468{
4469 struct channel *req = &s->req;
4470 struct channel *res = &s->res;
4471 struct htx *htx = htxbuf(&res->buf);
4472 size_t data;
4473
4474 if (final) {
4475 htx->flags |= HTX_FL_PROXY_RESP;
Christopher Faulet507479b2020-05-15 12:29:46 +02004476
Christopher Fauletaab1b672020-11-18 16:44:02 +01004477 if (!htx_is_empty(htx) && !http_eval_after_res_rules(s))
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01004478 return 0;
Christopher Fauletef70e252020-01-28 09:26:19 +01004479
Christopher Fauletd6c48362020-10-19 18:01:38 +02004480 if (s->txn->meth == HTTP_METH_HEAD)
4481 htx_skip_msg_payload(htx);
4482
Christopher Fauletef70e252020-01-28 09:26:19 +01004483 channel_auto_read(req);
4484 channel_abort(req);
4485 channel_auto_close(req);
4486 channel_htx_erase(req, htxbuf(&req->buf));
4487
4488 res->wex = tick_add_ifset(now_ms, res->wto);
4489 channel_auto_read(res);
4490 channel_auto_close(res);
4491 channel_shutr_now(res);
Christopher Faulet1a9db7c2020-06-25 15:36:45 +02004492 res->flags |= CF_EOI; /* The response is terminated, add EOI */
Christopher Faulet42432f32020-11-20 17:43:16 +01004493 htxbuf(&res->buf)->flags |= HTX_FL_EOM; /* no more data are expected */
Christopher Fauletef70e252020-01-28 09:26:19 +01004494 }
Christopher Fauletcf6898c2020-06-25 15:55:11 +02004495 else {
4496 /* Send ASAP informational messages. Rely on CF_EOI for final
4497 * response.
4498 */
4499 res->flags |= CF_SEND_DONTWAIT;
4500 }
Christopher Fauletef70e252020-01-28 09:26:19 +01004501
4502 data = htx->data - co_data(res);
4503 c_adv(res, data);
4504 htx->first = -1;
4505 res->total += data;
4506 return 1;
4507}
4508
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004509void http_server_error(struct stream *s, struct stream_interface *si, int err,
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004510 int finst, struct http_reply *msg)
Christopher Faulet0f226952018-10-22 09:29:56 +02004511{
Christopher Faulet72c7d8d2020-01-27 15:32:25 +01004512 http_reply_and_close(s, s->txn->status, msg);
Christopher Faulet0f226952018-10-22 09:29:56 +02004513 if (!(s->flags & SF_ERR_MASK))
4514 s->flags |= err;
4515 if (!(s->flags & SF_FINST_MASK))
4516 s->flags |= finst;
4517}
4518
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004519void http_reply_and_close(struct stream *s, short status, struct http_reply *msg)
Christopher Faulet0f226952018-10-22 09:29:56 +02004520{
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004521 if (!msg) {
4522 channel_htx_truncate(&s->res, htxbuf(&s->res.buf));
4523 goto end;
4524 }
4525
4526 if (http_reply_message(s, msg) == -1) {
4527 /* On error, return a 500 error message, but don't rewrite it if
Christopher Faulet40e6b552020-06-25 16:04:50 +02004528 * it is already an internal error. If it was already a "const"
4529 * 500 error, just fail.
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004530 */
Christopher Faulet40e6b552020-06-25 16:04:50 +02004531 if (s->txn->status == 500) {
4532 if (s->txn->flags & TX_CONST_REPLY)
4533 goto end;
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004534 s->txn->flags |= TX_CONST_REPLY;
Christopher Faulet40e6b552020-06-25 16:04:50 +02004535 }
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004536 s->txn->status = 500;
4537 s->txn->http_reply = NULL;
4538 return http_reply_and_close(s, s->txn->status, http_error_message(s));
4539 }
4540
4541end:
4542 s->res.wex = tick_add_ifset(now_ms, s->res.wto);
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004543
Christopher Faulet0f226952018-10-22 09:29:56 +02004544 channel_auto_read(&s->req);
4545 channel_abort(&s->req);
4546 channel_auto_close(&s->req);
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004547 channel_htx_erase(&s->req, htxbuf(&s->req.buf));
Christopher Faulet72c7d8d2020-01-27 15:32:25 +01004548 channel_auto_read(&s->res);
4549 channel_auto_close(&s->res);
4550 channel_shutr_now(&s->res);
Christopher Faulet0f226952018-10-22 09:29:56 +02004551}
4552
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004553struct http_reply *http_error_message(struct stream *s)
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004554{
4555 const int msgnum = http_get_status_idx(s->txn->status);
4556
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004557 if (s->txn->http_reply)
4558 return s->txn->http_reply;
4559 else if (s->be->replies[msgnum])
4560 return s->be->replies[msgnum];
4561 else if (strm_fe(s)->replies[msgnum])
4562 return strm_fe(s)->replies[msgnum];
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004563 else
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004564 return &http_err_replies[msgnum];
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004565}
4566
Christopher Faulet40e6b552020-06-25 16:04:50 +02004567/* Produces an HTX message from an http reply. Depending on the http reply type,
4568 * a, errorfile, an raw file or a log-format string is used. On success, it
4569 * returns 0. If an error occurs -1 is returned. If it fails, this function only
4570 * exits. It is the caller responsibility to do the cleanup.
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004571 */
Christopher Fauletae43b6c2020-05-27 15:24:22 +02004572int http_reply_to_htx(struct stream *s, struct htx *htx, struct http_reply *reply)
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004573{
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004574 struct buffer *errmsg;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004575 struct htx_sl *sl;
4576 struct buffer *body = NULL;
4577 const char *status, *reason, *clen, *ctype;
4578 unsigned int slflags;
4579 int ret = 0;
4580
Christopher Faulete29a97e2020-05-14 14:49:25 +02004581 /*
4582 * - HTTP_REPLY_ERRFILES unexpected here. handled as no payload if so
4583 *
4584 * - HTTP_REPLY_INDIRECT: switch on another reply if defined or handled
4585 * as no payload if NULL. the TXN status code is set with the status
4586 * of the original reply.
4587 */
4588
4589 if (reply->type == HTTP_REPLY_INDIRECT) {
4590 if (reply->body.reply)
4591 reply = reply->body.reply;
4592 }
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004593 if (reply->type == HTTP_REPLY_ERRMSG && !reply->body.errmsg) {
4594 /* get default error message */
4595 if (reply == s->txn->http_reply)
4596 s->txn->http_reply = NULL;
4597 reply = http_error_message(s);
4598 if (reply->type == HTTP_REPLY_INDIRECT) {
4599 if (reply->body.reply)
4600 reply = reply->body.reply;
4601 }
4602 }
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004603
4604 if (reply->type == HTTP_REPLY_ERRMSG) {
4605 /* implicit or explicit error message*/
4606 errmsg = reply->body.errmsg;
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004607 if (errmsg && !b_is_null(errmsg)) {
Christopher Faulet20567362020-05-15 14:52:49 +02004608 if (!htx_copy_msg(htx, errmsg))
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004609 goto fail;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004610 }
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004611 }
4612 else {
4613 /* no payload, file or log-format string */
4614 if (reply->type == HTTP_REPLY_RAW) {
4615 /* file */
4616 body = &reply->body.obj;
4617 }
4618 else if (reply->type == HTTP_REPLY_LOGFMT) {
4619 /* log-format string */
4620 body = alloc_trash_chunk();
4621 if (!body)
4622 goto fail_alloc;
4623 body->data = build_logline(s, body->area, body->size, &reply->body.fmt);
4624 }
4625 /* else no payload */
4626
4627 status = ultoa(reply->status);
4628 reason = http_get_reason(reply->status);
4629 slflags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_CLEN);
4630 if (!body || !b_data(body))
4631 slflags |= HTX_SL_F_BODYLESS;
4632 sl = htx_add_stline(htx, HTX_BLK_RES_SL, slflags, ist("HTTP/1.1"), ist(status), ist(reason));
4633 if (!sl)
4634 goto fail;
4635 sl->info.res.status = reply->status;
4636
4637 clen = (body ? ultoa(b_data(body)) : "0");
4638 ctype = reply->ctype;
4639
4640 if (!LIST_ISEMPTY(&reply->hdrs)) {
4641 struct http_reply_hdr *hdr;
4642 struct buffer *value = alloc_trash_chunk();
4643
4644 if (!value)
4645 goto fail;
4646
4647 list_for_each_entry(hdr, &reply->hdrs, list) {
4648 chunk_reset(value);
4649 value->data = build_logline(s, value->area, value->size, &hdr->value);
4650 if (b_data(value) && !htx_add_header(htx, hdr->name, ist2(b_head(value), b_data(value)))) {
4651 free_trash_chunk(value);
4652 goto fail;
4653 }
4654 chunk_reset(value);
4655 }
4656 free_trash_chunk(value);
4657 }
4658
4659 if (!htx_add_header(htx, ist("content-length"), ist(clen)) ||
4660 (body && b_data(body) && ctype && !htx_add_header(htx, ist("content-type"), ist(ctype))) ||
4661 !htx_add_endof(htx, HTX_BLK_EOH) ||
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004662 (body && b_data(body) && !htx_add_data_atonce(htx, ist2(b_head(body), b_data(body)))))
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004663 goto fail;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004664
4665 htx->flags |= HTX_FL_EOM;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004666 }
4667
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004668 leave:
4669 if (reply->type == HTTP_REPLY_LOGFMT)
4670 free_trash_chunk(body);
4671 return ret;
4672
4673 fail_alloc:
4674 if (!(s->flags & SF_ERR_MASK))
4675 s->flags |= SF_ERR_RESOURCE;
Christopher Faulet97e466c2020-05-15 15:12:47 +02004676 /* fall through */
4677 fail:
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004678 ret = -1;
4679 goto leave;
Christopher Faulet97e466c2020-05-15 15:12:47 +02004680}
4681
4682/* Send an http reply to the client. On success, it returns 0. If an error
Christopher Faulet40e6b552020-06-25 16:04:50 +02004683 * occurs -1 is returned and the response channel is truncated, removing this
4684 * way the faulty reply. This function may fail when the reply is formatted
4685 * (http_reply_to_htx) or when the reply is forwarded
4686 * (http_forward_proxy_resp). On the last case, it is because a
4687 * http-after-response rule fails.
Christopher Faulet97e466c2020-05-15 15:12:47 +02004688 */
4689int http_reply_message(struct stream *s, struct http_reply *reply)
4690{
4691 struct channel *res = &s->res;
4692 struct htx *htx = htx_from_buf(&res->buf);
4693
4694 if (s->txn->status == -1)
4695 s->txn->status = reply->status;
4696 channel_htx_truncate(res, htx);
4697
4698 if (http_reply_to_htx(s, htx, reply) == -1)
4699 goto fail;
4700
4701 htx_to_buf(htx, &s->res.buf);
4702 if (!http_forward_proxy_resp(s, 1))
4703 goto fail;
4704 return 0;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004705
4706 fail:
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004707 channel_htx_truncate(res, htx);
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004708 if (!(s->flags & SF_ERR_MASK))
4709 s->flags |= SF_ERR_PRXCOND;
Christopher Faulet97e466c2020-05-15 15:12:47 +02004710 return -1;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004711}
4712
Christopher Faulet304cc402019-07-15 15:46:28 +02004713/* Return the error message corresponding to si->err_type. It is assumed
4714 * that the server side is closed. Note that err_type is actually a
4715 * bitmask, where almost only aborts may be cumulated with other
4716 * values. We consider that aborted operations are more important
4717 * than timeouts or errors due to the fact that nobody else in the
4718 * logs might explain incomplete retries. All others should avoid
4719 * being cumulated. It should normally not be possible to have multiple
4720 * aborts at once, but just in case, the first one in sequence is reported.
4721 * Note that connection errors appearing on the second request of a keep-alive
4722 * connection are not reported since this allows the client to retry.
4723 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004724void http_return_srv_error(struct stream *s, struct stream_interface *si)
Christopher Faulet304cc402019-07-15 15:46:28 +02004725{
4726 int err_type = si->err_type;
4727
4728 /* set s->txn->status for http_error_message(s) */
4729 s->txn->status = 503;
4730
4731 if (err_type & SI_ET_QUEUE_ABRT)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004732 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_Q,
4733 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004734 else if (err_type & SI_ET_CONN_ABRT)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004735 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_C,
4736 (s->txn->flags & TX_NOT_FIRST) ? NULL :
4737 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004738 else if (err_type & SI_ET_QUEUE_TO)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004739 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_Q,
4740 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004741 else if (err_type & SI_ET_QUEUE_ERR)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004742 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_Q,
4743 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004744 else if (err_type & SI_ET_CONN_TO)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004745 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_C,
4746 (s->txn->flags & TX_NOT_FIRST) ? NULL :
4747 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004748 else if (err_type & SI_ET_CONN_ERR)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004749 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_C,
4750 (s->flags & SF_SRV_REUSED) ? NULL :
4751 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004752 else if (err_type & SI_ET_CONN_RES)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004753 http_server_error(s, si, SF_ERR_RESOURCE, SF_FINST_C,
4754 (s->txn->flags & TX_NOT_FIRST) ? NULL :
4755 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004756 else { /* SI_ET_CONN_OTHER and others */
4757 s->txn->status = 500;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004758 http_server_error(s, si, SF_ERR_INTERNAL, SF_FINST_C,
4759 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004760 }
4761}
4762
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004763
Christopher Faulet4a28a532019-03-01 11:19:40 +01004764/* Handle Expect: 100-continue for HTTP/1.1 messages if necessary. It returns 0
4765 * on success and -1 on error.
4766 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004767static int http_handle_expect_hdr(struct stream *s, struct htx *htx, struct http_msg *msg)
Christopher Faulet4a28a532019-03-01 11:19:40 +01004768{
4769 /* If we have HTTP/1.1 message with a body and Expect: 100-continue,
4770 * then we must send an HTTP/1.1 100 Continue intermediate response.
4771 */
4772 if (msg->msg_state == HTTP_MSG_BODY && (msg->flags & HTTP_MSGF_VER_11) &&
4773 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
4774 struct ist hdr = { .ptr = "Expect", .len = 6 };
4775 struct http_hdr_ctx ctx;
4776
4777 ctx.blk = NULL;
4778 /* Expect is allowed in 1.1, look for it */
4779 if (http_find_header(htx, hdr, &ctx, 0) &&
4780 unlikely(isteqi(ctx.value, ist2("100-continue", 12)))) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004781 if (http_reply_100_continue(s) == -1)
Christopher Faulet4a28a532019-03-01 11:19:40 +01004782 return -1;
4783 http_remove_header(htx, &ctx);
4784 }
4785 }
4786 return 0;
4787}
4788
Christopher Faulet23a3c792018-11-28 10:01:23 +01004789/* Send a 100-Continue response to the client. It returns 0 on success and -1
4790 * on error. The response channel is updated accordingly.
4791 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004792static int http_reply_100_continue(struct stream *s)
Christopher Faulet23a3c792018-11-28 10:01:23 +01004793{
4794 struct channel *res = &s->res;
4795 struct htx *htx = htx_from_buf(&res->buf);
4796 struct htx_sl *sl;
4797 unsigned int flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|
4798 HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
Christopher Faulet23a3c792018-11-28 10:01:23 +01004799
4800 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
4801 ist("HTTP/1.1"), ist("100"), ist("Continue"));
4802 if (!sl)
4803 goto fail;
4804 sl->info.res.status = 100;
4805
Christopher Faulet1d5ec092019-06-26 14:23:54 +02004806 if (!htx_add_endof(htx, HTX_BLK_EOH))
Christopher Faulet23a3c792018-11-28 10:01:23 +01004807 goto fail;
4808
Christopher Fauleta72a7e42020-01-28 09:28:11 +01004809 if (!http_forward_proxy_resp(s, 0))
4810 goto fail;
Christopher Faulet23a3c792018-11-28 10:01:23 +01004811 return 0;
4812
4813 fail:
4814 /* If an error occurred, remove the incomplete HTTP response from the
4815 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004816 channel_htx_truncate(res, htx);
Christopher Faulet23a3c792018-11-28 10:01:23 +01004817 return -1;
4818}
4819
Christopher Faulet12c51e22018-11-28 15:59:42 +01004820
Christopher Faulet0f226952018-10-22 09:29:56 +02004821/*
4822 * Capture headers from message <htx> according to header list <cap_hdr>, and
4823 * fill the <cap> pointers appropriately.
4824 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004825static void http_capture_headers(struct htx *htx, char **cap, struct cap_hdr *cap_hdr)
Christopher Faulet0f226952018-10-22 09:29:56 +02004826{
4827 struct cap_hdr *h;
4828 int32_t pos;
4829
Christopher Fauleta3f15502019-05-13 15:27:23 +02004830 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet0f226952018-10-22 09:29:56 +02004831 struct htx_blk *blk = htx_get_blk(htx, pos);
4832 enum htx_blk_type type = htx_get_blk_type(blk);
4833 struct ist n, v;
4834
4835 if (type == HTX_BLK_EOH)
4836 break;
4837 if (type != HTX_BLK_HDR)
4838 continue;
4839
4840 n = htx_get_blk_name(htx, blk);
4841
4842 for (h = cap_hdr; h; h = h->next) {
4843 if (h->namelen && (h->namelen == n.len) &&
4844 (strncasecmp(n.ptr, h->name, h->namelen) == 0)) {
4845 if (cap[h->index] == NULL)
4846 cap[h->index] =
4847 pool_alloc(h->pool);
4848
4849 if (cap[h->index] == NULL) {
4850 ha_alert("HTTP capture : out of memory.\n");
4851 break;
4852 }
4853
4854 v = htx_get_blk_value(htx, blk);
4855 if (v.len > h->len)
4856 v.len = h->len;
4857
4858 memcpy(cap[h->index], v.ptr, v.len);
4859 cap[h->index][v.len]=0;
4860 }
4861 }
4862 }
4863}
4864
Christopher Faulet0b6bdc52018-10-24 11:05:36 +02004865/* Delete a value in a header between delimiters <from> and <next>. The header
4866 * itself is delimited by <start> and <end> pointers. The number of characters
4867 * displaced is returned, and the pointer to the first delimiter is updated if
4868 * required. The function tries as much as possible to respect the following
4869 * principles :
4870 * - replace <from> delimiter by the <next> one unless <from> points to <start>,
4871 * in which case <next> is simply removed
4872 * - set exactly one space character after the new first delimiter, unless there
4873 * are not enough characters in the block being moved to do so.
4874 * - remove unneeded spaces before the previous delimiter and after the new
4875 * one.
4876 *
4877 * It is the caller's responsibility to ensure that :
4878 * - <from> points to a valid delimiter or <start> ;
4879 * - <next> points to a valid delimiter or <end> ;
4880 * - there are non-space chars before <from>.
4881 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004882static int http_del_hdr_value(char *start, char *end, char **from, char *next)
Christopher Faulet0b6bdc52018-10-24 11:05:36 +02004883{
4884 char *prev = *from;
4885
4886 if (prev == start) {
4887 /* We're removing the first value. eat the semicolon, if <next>
4888 * is lower than <end> */
4889 if (next < end)
4890 next++;
4891
4892 while (next < end && HTTP_IS_SPHT(*next))
4893 next++;
4894 }
4895 else {
4896 /* Remove useless spaces before the old delimiter. */
4897 while (HTTP_IS_SPHT(*(prev-1)))
4898 prev--;
4899 *from = prev;
4900
4901 /* copy the delimiter and if possible a space if we're
4902 * not at the end of the line.
4903 */
4904 if (next < end) {
4905 *prev++ = *next++;
4906 if (prev + 1 < next)
4907 *prev++ = ' ';
4908 while (next < end && HTTP_IS_SPHT(*next))
4909 next++;
4910 }
4911 }
4912 memmove(prev, next, end - next);
4913 return (prev - next);
4914}
4915
Christopher Faulet0f226952018-10-22 09:29:56 +02004916
4917/* Formats the start line of the request (without CRLF) and puts it in <str> and
Joseph Herlantc42c0e92018-11-25 10:43:27 -08004918 * return the written length. The line can be truncated if it exceeds <len>.
Christopher Faulet0f226952018-10-22 09:29:56 +02004919 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004920static size_t http_fmt_req_line(const struct htx_sl *sl, char *str, size_t len)
Christopher Faulet0f226952018-10-22 09:29:56 +02004921{
4922 struct ist dst = ist2(str, 0);
4923
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004924 if (istcat(&dst, htx_sl_req_meth(sl), len) == -1)
Christopher Faulet0f226952018-10-22 09:29:56 +02004925 goto end;
4926 if (dst.len + 1 > len)
4927 goto end;
4928 dst.ptr[dst.len++] = ' ';
4929
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004930 if (istcat(&dst, htx_sl_req_uri(sl), len) == -1)
Christopher Faulet0f226952018-10-22 09:29:56 +02004931 goto end;
4932 if (dst.len + 1 > len)
4933 goto end;
4934 dst.ptr[dst.len++] = ' ';
4935
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004936 istcat(&dst, htx_sl_req_vsn(sl), len);
Christopher Faulet0f226952018-10-22 09:29:56 +02004937 end:
4938 return dst.len;
4939}
4940
4941/*
4942 * Print a debug line with a start line.
4943 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004944static void http_debug_stline(const char *dir, struct stream *s, const struct htx_sl *sl)
Christopher Faulet0f226952018-10-22 09:29:56 +02004945{
4946 struct session *sess = strm_sess(s);
4947 int max;
4948
4949 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
4950 dir,
4951 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->handle.fd : -1,
4952 objt_cs(s->si[1].end) ? (unsigned short)objt_cs(s->si[1].end)->conn->handle.fd : -1);
4953
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004954 max = HTX_SL_P1_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02004955 UBOUND(max, trash.size - trash.data - 3);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004956 chunk_memcat(&trash, HTX_SL_P1_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02004957 trash.area[trash.data++] = ' ';
4958
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004959 max = HTX_SL_P2_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02004960 UBOUND(max, trash.size - trash.data - 2);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004961 chunk_memcat(&trash, HTX_SL_P2_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02004962 trash.area[trash.data++] = ' ';
4963
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004964 max = HTX_SL_P3_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02004965 UBOUND(max, trash.size - trash.data - 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004966 chunk_memcat(&trash, HTX_SL_P3_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02004967 trash.area[trash.data++] = '\n';
4968
Willy Tarreau2e8ab6b2020-03-14 11:03:20 +01004969 DISGUISE(write(1, trash.area, trash.data));
Christopher Faulet0f226952018-10-22 09:29:56 +02004970}
4971
4972/*
4973 * Print a debug line with a header.
4974 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004975static 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 +02004976{
4977 struct session *sess = strm_sess(s);
4978 int max;
4979
4980 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
4981 dir,
4982 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->handle.fd : -1,
4983 objt_cs(s->si[1].end) ? (unsigned short)objt_cs(s->si[1].end)->conn->handle.fd : -1);
4984
4985 max = n.len;
4986 UBOUND(max, trash.size - trash.data - 3);
4987 chunk_memcat(&trash, n.ptr, max);
4988 trash.area[trash.data++] = ':';
4989 trash.area[trash.data++] = ' ';
4990
4991 max = v.len;
4992 UBOUND(max, trash.size - trash.data - 1);
4993 chunk_memcat(&trash, v.ptr, max);
4994 trash.area[trash.data++] = '\n';
4995
Willy Tarreau2e8ab6b2020-03-14 11:03:20 +01004996 DISGUISE(write(1, trash.area, trash.data));
Christopher Faulet0f226952018-10-22 09:29:56 +02004997}
4998
Christopher Fauleta8a46e22019-07-16 14:53:09 +02004999/* Allocate a new HTTP transaction for stream <s> unless there is one already.
5000 * In case of allocation failure, everything allocated is freed and NULL is
5001 * returned. Otherwise the new transaction is assigned to the stream and
5002 * returned.
5003 */
5004struct http_txn *http_alloc_txn(struct stream *s)
5005{
5006 struct http_txn *txn = s->txn;
5007
5008 if (txn)
5009 return txn;
5010
5011 txn = pool_alloc(pool_head_http_txn);
5012 if (!txn)
5013 return txn;
5014
5015 s->txn = txn;
5016 return txn;
5017}
5018
5019void http_txn_reset_req(struct http_txn *txn)
5020{
Christopher Faulet1aea50e2020-01-17 16:03:53 +01005021 txn->req.flags = 0;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005022 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
5023}
5024
5025void http_txn_reset_res(struct http_txn *txn)
5026{
Christopher Faulet1aea50e2020-01-17 16:03:53 +01005027 txn->rsp.flags = 0;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005028 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
5029}
5030
5031/*
5032 * Initialize a new HTTP transaction for stream <s>. It is assumed that all
5033 * the required fields are properly allocated and that we only need to (re)init
5034 * them. This should be used before processing any new request.
5035 */
5036void http_init_txn(struct stream *s)
5037{
5038 struct http_txn *txn = s->txn;
5039 struct conn_stream *cs = objt_cs(s->si[0].end);
5040
Christopher Fauletda831fa2020-10-06 17:58:43 +02005041 txn->flags = ((cs && cs->flags & CS_FL_NOT_FIRST) ? TX_NOT_FIRST : 0);
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005042 txn->status = -1;
Christopher Faulet5cb513a2020-05-13 17:56:56 +02005043 txn->http_reply = NULL;
Willy Tarreau8b507582020-02-25 09:35:07 +01005044 write_u32(txn->cache_hash, 0);
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005045
5046 txn->cookie_first_date = 0;
5047 txn->cookie_last_date = 0;
5048
5049 txn->srv_cookie = NULL;
5050 txn->cli_cookie = NULL;
5051 txn->uri = NULL;
5052
5053 http_txn_reset_req(txn);
5054 http_txn_reset_res(txn);
5055
5056 txn->req.chn = &s->req;
5057 txn->rsp.chn = &s->res;
5058
5059 txn->auth.method = HTTP_AUTH_UNKNOWN;
5060
5061 vars_init(&s->vars_txn, SCOPE_TXN);
5062 vars_init(&s->vars_reqres, SCOPE_REQ);
5063}
5064
5065/* to be used at the end of a transaction */
5066void http_end_txn(struct stream *s)
5067{
5068 struct http_txn *txn = s->txn;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005069
5070 /* these ones will have been dynamically allocated */
5071 pool_free(pool_head_requri, txn->uri);
5072 pool_free(pool_head_capture, txn->cli_cookie);
5073 pool_free(pool_head_capture, txn->srv_cookie);
Tim Duesterhusa17e6622020-03-05 20:19:02 +01005074 pool_free(pool_head_uniqueid, s->unique_id.ptr);
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005075
Tim Duesterhusa17e6622020-03-05 20:19:02 +01005076 s->unique_id = IST_NULL;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005077 txn->uri = NULL;
5078 txn->srv_cookie = NULL;
5079 txn->cli_cookie = NULL;
5080
Christopher Faulet59399252019-11-07 14:27:52 +01005081 if (!LIST_ISEMPTY(&s->vars_txn.head))
5082 vars_prune(&s->vars_txn, s->sess, s);
5083 if (!LIST_ISEMPTY(&s->vars_reqres.head))
5084 vars_prune(&s->vars_reqres, s->sess, s);
5085}
5086
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005087
5088DECLARE_POOL(pool_head_http_txn, "http_txn", sizeof(struct http_txn));
Christopher Faulet0f226952018-10-22 09:29:56 +02005089
Christopher Fauletf4eb75d2018-10-11 15:55:07 +02005090__attribute__((constructor))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005091static void __http_protocol_init(void)
Christopher Fauletf4eb75d2018-10-11 15:55:07 +02005092{
5093}
5094
5095
5096/*
5097 * Local variables:
5098 * c-indent-level: 8
5099 * c-basic-offset: 8
5100 * End:
5101 */