blob: 51445e996bca4443c1d530878def55b20337ac45 [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
Christopher Fauletc2ac5e42021-03-08 18:20:09 +0100455 if (HAS_FILTERS(s))
456 req->analysers |= AN_REQ_FLT_HTTP_HDRS;
457
Christopher Faulete0768eb2018-10-03 16:38:02 +0200458 /* enable the minimally required analyzers to handle keep-alive and compression on the HTTP response */
459 req->analysers &= (AN_REQ_HTTP_BODY | AN_REQ_FLT_HTTP_HDRS | AN_REQ_FLT_END);
460 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
461 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Christopher Fauletbcf242a2019-03-01 11:36:26 +0100462
463 req->flags |= CF_SEND_DONTWAIT;
464 s->flags |= SF_ASSIGNED;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200465 goto done;
466 }
467
468 /* check whether we have some ACLs set to redirect this request */
469 list_for_each_entry(rule, &px->redirect_rules, list) {
470 if (rule->cond) {
471 int ret;
472
473 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
474 ret = acl_pass(ret);
475 if (rule->cond->pol == ACL_COND_UNLESS)
476 ret = !ret;
477 if (!ret)
478 continue;
479 }
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200480 if (!http_apply_redirect_rule(rule, s, txn))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100481 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200482 goto done;
483 }
484
485 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
486 * If this happens, then the data will not come immediately, so we must
487 * send all what we have without waiting. Note that due to the small gain
488 * in waiting for the body of the request, it's easier to simply put the
489 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
490 * itself once used.
491 */
492 req->flags |= CF_SEND_DONTWAIT;
493
494 done: /* done with this analyser, continue with next ones that the calling
495 * points will have set, if any.
496 */
497 req->analyse_exp = TICK_ETERNITY;
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +0500498 done_without_exp: /* done with this analyser, but don't reset the analyse_exp. */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200499 req->analysers &= ~an_bit;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100500 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200501 return 1;
502
503 tarpit:
504 /* Allow cookie logging
505 */
506 if (s->be->cookie_name || sess->fe->capture_name)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200507 http_manage_client_side_cookies(s, req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200508
509 /* When a connection is tarpitted, we use the tarpit timeout,
510 * which may be the same as the connect timeout if unspecified.
511 * If unset, then set it to zero because we really want it to
512 * eventually expire. We build the tarpit as an analyser.
513 */
Christopher Faulet202c6ce2019-01-07 14:57:35 +0100514 channel_htx_erase(&s->req, htx);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200515
516 /* wipe the request out so that we can drop the connection early
517 * if the client closes first.
518 */
519 channel_dont_connect(req);
520
Christopher Faulete0768eb2018-10-03 16:38:02 +0200521 req->analysers &= AN_REQ_FLT_END; /* remove switching rules etc... */
522 req->analysers |= AN_REQ_HTTP_TARPIT;
523 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
524 if (!req->analyse_exp)
525 req->analyse_exp = tick_add(now_ms, 0);
526 stream_inc_http_err_ctr(s);
Olivier Houcharda798bf52019-03-08 18:52:00 +0100527 _HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100528 if (s->flags & SF_BE_ASSIGNED)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100529 _HA_ATOMIC_ADD(&s->be->be_counters.denied_req, 1);
William Lallemand36119de2021-03-08 15:26:48 +0100530 if (sess->listener && sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100531 _HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200532 goto done_without_exp;
533
534 deny: /* this request was blocked (denied) */
535
536 /* Allow cookie logging
537 */
538 if (s->be->cookie_name || sess->fe->capture_name)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200539 http_manage_client_side_cookies(s, req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200540
Christopher Faulete0768eb2018-10-03 16:38:02 +0200541 s->logs.tv_request = now;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200542 stream_inc_http_err_ctr(s);
Olivier Houcharda798bf52019-03-08 18:52:00 +0100543 _HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100544 if (s->flags & SF_BE_ASSIGNED)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100545 _HA_ATOMIC_ADD(&s->be->be_counters.denied_req, 1);
William Lallemand36119de2021-03-08 15:26:48 +0100546 if (sess->listener && sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100547 _HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100548 goto return_prx_err;
549
550 return_int_err:
551 txn->status = 500;
552 if (!(s->flags & SF_ERR_MASK))
553 s->flags |= SF_ERR_INTERNAL;
554 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100555 if (s->flags & SF_BE_ASSIGNED)
556 _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1);
William Lallemand36119de2021-03-08 15:26:48 +0100557 if (sess->listener && sess->listener->counters)
Christopher Fauletb8a53712019-12-16 11:29:38 +0100558 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
559 goto return_prx_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200560
561 return_bad_req:
Christopher Faulete0768eb2018-10-03 16:38:02 +0200562 txn->status = 400;
Olivier Houcharda798bf52019-03-08 18:52:00 +0100563 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
William Lallemand36119de2021-03-08 15:26:48 +0100564 if (sess->listener && sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100565 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100566 /* fall through */
567
568 return_prx_err:
569 http_reply_and_close(s, txn->status, http_error_message(s));
570 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200571
572 return_prx_cond:
573 if (!(s->flags & SF_ERR_MASK))
574 s->flags |= SF_ERR_PRXCOND;
575 if (!(s->flags & SF_FINST_MASK))
576 s->flags |= SF_FINST_R;
577
578 req->analysers &= AN_REQ_FLT_END;
579 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100580 DBG_TRACE_DEVEL("leaving on error",
581 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200582 return 0;
583
584 return_prx_yield:
585 channel_dont_connect(req);
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100586 DBG_TRACE_DEVEL("waiting for more data",
587 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200588 return 0;
589}
590
591/* This function performs all the processing enabled for the current request.
592 * It returns 1 if the processing can continue on next analysers, or zero if it
593 * needs more data, encounters an error, or wants to immediately abort the
594 * request. It relies on buffers flags, and updates s->req.analysers.
595 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200596int http_process_request(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200597{
598 struct session *sess = s->sess;
599 struct http_txn *txn = s->txn;
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200600 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200601 struct connection *cli_conn = objt_conn(strm_sess(s)->origin);
602
Christopher Faulet8bebd2f2020-10-06 17:54:56 +0200603 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200604
605 /*
606 * Right now, we know that we have processed the entire headers
607 * and that unwanted requests have been filtered out. We can do
608 * whatever we want with the remaining request. Also, now we
609 * may have separate values for ->fe, ->be.
610 */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100611 htx = htxbuf(&req->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200612
613 /*
614 * If HTTP PROXY is set we simply get remote server address parsing
Willy Tarreau1c8d32b2019-07-18 15:47:45 +0200615 * incoming request.
Christopher Faulete0768eb2018-10-03 16:38:02 +0200616 */
617 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SF_ADDR_SET)) {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100618 struct htx_sl *sl;
619 struct ist uri, path;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200620
Willy Tarreau9b7587a2020-10-15 07:32:10 +0200621 if (!sockaddr_alloc(&s->target_addr, NULL, 0)) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200622 if (!(s->flags & SF_ERR_MASK))
623 s->flags |= SF_ERR_RESOURCE;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100624 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200625 }
Christopher Faulet297fbb42019-05-13 14:41:27 +0200626 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100627 uri = htx_sl_req_uri(sl);
628 path = http_get_path(uri);
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200629
Willy Tarreau1c8d32b2019-07-18 15:47:45 +0200630 if (url2sa(uri.ptr, uri.len - path.len, s->target_addr, NULL) == -1)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200631 goto return_bad_req;
632
Willy Tarreau1c8d32b2019-07-18 15:47:45 +0200633 s->target = &s->be->obj_type;
634 s->flags |= SF_ADDR_SET | SF_ASSIGNED;
635
Christopher Faulete0768eb2018-10-03 16:38:02 +0200636 /* if the path was found, we have to remove everything between
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200637 * uri.ptr and path.ptr (excluded). If it was not found, we need
638 * to replace from all the uri by a single "/".
639 *
Ilya Shipitsin46a030c2020-07-05 16:36:08 +0500640 * Instead of rewriting the whole start line, we just update
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100641 * the star-line URI. Some space will be lost but it should be
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200642 * insignificant.
Christopher Faulete0768eb2018-10-03 16:38:02 +0200643 */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100644 istcpy(&uri, (path.len ? path : ist("/")), uri.len);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200645 }
646
647 /*
648 * 7: Now we can work with the cookies.
649 * Note that doing so might move headers in the request, but
650 * the fields will stay coherent and the URI will not move.
651 * This should only be performed in the backend.
652 */
653 if (s->be->cookie_name || sess->fe->capture_name)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200654 http_manage_client_side_cookies(s, req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200655
Tim Duesterhus2825b4b2020-02-28 15:13:34 +0100656 /* 8: Generate unique ID if a "unique-id-format" is defined.
657 *
658 * A unique ID is generated even when it is not sent to ensure that the ID can make use of
659 * fetches only available in the HTTP request processing stage.
660 */
661 if (!LIST_ISEMPTY(&sess->fe->format_unique_id)) {
Tim Duesterhusa17e6622020-03-05 20:19:02 +0100662 struct ist unique_id = stream_generate_unique_id(s, &sess->fe->format_unique_id);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200663
Tim Duesterhusa17e6622020-03-05 20:19:02 +0100664 if (!isttest(unique_id)) {
Christopher Fauletb8a53712019-12-16 11:29:38 +0100665 if (!(s->flags & SF_ERR_MASK))
666 s->flags |= SF_ERR_RESOURCE;
667 goto return_int_err;
668 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200669
Tim Duesterhus2825b4b2020-02-28 15:13:34 +0100670 /* send unique ID if a "unique-id-header" is defined */
Tim Duesterhus0643b0e2020-03-05 17:56:35 +0100671 if (isttest(sess->fe->header_unique_id) &&
Tim Duesterhusa17e6622020-03-05 20:19:02 +0100672 unlikely(!http_add_header(htx, sess->fe->header_unique_id, s->unique_id)))
Tim Duesterhus2825b4b2020-02-28 15:13:34 +0100673 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200674 }
675
676 /*
677 * 9: add X-Forwarded-For if either the frontend or the backend
678 * asks for it.
679 */
680 if ((sess->fe->options | s->be->options) & PR_O_FWDFOR) {
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200681 struct http_hdr_ctx ctx = { .blk = NULL };
682 struct ist hdr = ist2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : sess->fe->fwdfor_hdr_name,
683 s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : sess->fe->fwdfor_hdr_len);
684
Christopher Faulete0768eb2018-10-03 16:38:02 +0200685 if (!((sess->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200686 http_find_header(htx, hdr, &ctx, 0)) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200687 /* The header is set to be added only if none is present
688 * and we found it, so don't do anything.
689 */
690 }
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200691 else if (cli_conn && conn_get_src(cli_conn) && cli_conn->src->ss_family == AF_INET) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200692 /* Add an X-Forwarded-For header unless the source IP is
693 * in the 'except' network range.
694 */
Christopher Faulet5d1def62021-02-26 09:19:15 +0100695 if (ipcmp2net(cli_conn->src, &sess->fe->except_xff_net) &&
696 ipcmp2net(cli_conn->src, &s->be->except_xff_net)) {
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200697 unsigned char *pn = (unsigned char *)&((struct sockaddr_in *)cli_conn->src)->sin_addr;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200698
699 /* Note: we rely on the backend to get the header name to be used for
700 * x-forwarded-for, because the header is really meant for the backends.
701 * However, if the backend did not specify any option, we have to rely
702 * on the frontend's header name.
703 */
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200704 chunk_printf(&trash, "%d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
705 if (unlikely(!http_add_header(htx, hdr, ist2(trash.area, trash.data))))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100706 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200707 }
708 }
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200709 else if (cli_conn && conn_get_src(cli_conn) && cli_conn->src->ss_family == AF_INET6) {
Christopher Faulet5d1def62021-02-26 09:19:15 +0100710 /* Add an X-Forwarded-For header unless the source IP is
711 * in the 'except' network range.
Christopher Faulete0768eb2018-10-03 16:38:02 +0200712 */
Christopher Faulet5d1def62021-02-26 09:19:15 +0100713 if (ipcmp2net(cli_conn->src, &sess->fe->except_xff_net) &&
714 ipcmp2net(cli_conn->src, &s->be->except_xff_net)) {
715 char pn[INET6_ADDRSTRLEN];
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200716
Christopher Faulet5d1def62021-02-26 09:19:15 +0100717 inet_ntop(AF_INET6,
718 (const void *)&((struct sockaddr_in6 *)(cli_conn->src))->sin6_addr,
719 pn, sizeof(pn));
Christopher Faulete0768eb2018-10-03 16:38:02 +0200720
Christopher Faulet5d1def62021-02-26 09:19:15 +0100721 /* Note: we rely on the backend to get the header name to be used for
722 * x-forwarded-for, because the header is really meant for the backends.
723 * However, if the backend did not specify any option, we have to rely
724 * on the frontend's header name.
725 */
726 chunk_printf(&trash, "%s", pn);
727 if (unlikely(!http_add_header(htx, hdr, ist2(trash.area, trash.data))))
728 goto return_int_err;
729 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200730 }
731 }
732
733 /*
734 * 10: add X-Original-To if either the frontend or the backend
735 * asks for it.
736 */
737 if ((sess->fe->options | s->be->options) & PR_O_ORGTO) {
Christopher Faulet5d1def62021-02-26 09:19:15 +0100738 struct ist hdr = ist2(s->be->orgto_hdr_len ? s->be->orgto_hdr_name : sess->fe->orgto_hdr_name,
739 s->be->orgto_hdr_len ? s->be->orgto_hdr_len : sess->fe->orgto_hdr_len);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200740
Christopher Fauletcccded92021-02-26 12:45:56 +0100741 if (cli_conn && conn_get_dst(cli_conn) && cli_conn->dst->ss_family == AF_INET) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200742 /* Add an X-Original-To header unless the destination IP is
743 * in the 'except' network range.
744 */
Christopher Faulet5d1def62021-02-26 09:19:15 +0100745 if (ipcmp2net(cli_conn->dst, &sess->fe->except_xot_net) &&
746 ipcmp2net(cli_conn->dst, &s->be->except_xot_net)) {
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200747 unsigned char *pn = (unsigned char *)&((struct sockaddr_in *)cli_conn->dst)->sin_addr;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200748
749 /* Note: we rely on the backend to get the header name to be used for
750 * x-original-to, because the header is really meant for the backends.
751 * However, if the backend did not specify any option, we have to rely
752 * on the frontend's header name.
753 */
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200754 chunk_printf(&trash, "%d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
755 if (unlikely(!http_add_header(htx, hdr, ist2(trash.area, trash.data))))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100756 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200757 }
758 }
Christopher Faulet5d1def62021-02-26 09:19:15 +0100759 else if (cli_conn && conn_get_dst(cli_conn) && cli_conn->dst->ss_family == AF_INET6) {
760 /* Add an X-Original-To header unless the source IP is
761 * in the 'except' network range.
762 */
763 if (ipcmp2net(cli_conn->dst, &sess->fe->except_xot_net) &&
764 ipcmp2net(cli_conn->dst, &s->be->except_xot_net)) {
765 char pn[INET6_ADDRSTRLEN];
766
767 inet_ntop(AF_INET6,
768 (const void *)&((struct sockaddr_in6 *)(cli_conn->dst))->sin6_addr,
769 pn, sizeof(pn));
770
771 /* Note: we rely on the backend to get the header name to be used for
772 * x-forwarded-for, because the header is really meant for the backends.
773 * However, if the backend did not specify any option, we have to rely
774 * on the frontend's header name.
775 */
776 chunk_printf(&trash, "%s", pn);
777 if (unlikely(!http_add_header(htx, hdr, ist2(trash.area, trash.data))))
778 goto return_int_err;
779 }
780 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200781 }
782
Christopher Fauletc2ac5e42021-03-08 18:20:09 +0100783 /* Filter the request headers if there are filters attached to the
784 * stream.
785 */
786 if (HAS_FILTERS(s))
787 req->analysers |= AN_REQ_FLT_HTTP_HDRS;
788
Christopher Faulete0768eb2018-10-03 16:38:02 +0200789 /* If we have no server assigned yet and we're balancing on url_param
790 * with a POST request, we may be interested in checking the body for
791 * that parameter. This will be done in another analyser.
792 */
793 if (!(s->flags & (SF_ASSIGNED|SF_DIRECT)) &&
Willy Tarreau089eaa02019-01-14 15:17:46 +0100794 s->txn->meth == HTTP_METH_POST &&
795 (s->be->lbprm.algo & BE_LB_ALGO) == BE_LB_ALGO_PH) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200796 channel_dont_connect(req);
797 req->analysers |= AN_REQ_HTTP_BODY;
798 }
799
800 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
801 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau1a18b542018-12-11 16:37:42 +0100802
Christopher Faulete0768eb2018-10-03 16:38:02 +0200803 /* We expect some data from the client. Unless we know for sure
804 * we already have a full request, we have to re-enable quick-ack
805 * in case we previously disabled it, otherwise we might cause
806 * the client to delay further data.
807 */
William Lallemand36119de2021-03-08 15:26:48 +0100808 if ((sess->listener && (sess->listener->options & LI_O_NOQUICKACK)) && !(htx->flags & HTX_FL_EOM))
Willy Tarreau1a18b542018-12-11 16:37:42 +0100809 conn_set_quickack(cli_conn, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200810
811 /*************************************************************
812 * OK, that's finished for the headers. We have done what we *
813 * could. Let's switch to the DATA state. *
814 ************************************************************/
815 req->analyse_exp = TICK_ETERNITY;
816 req->analysers &= ~an_bit;
817
818 s->logs.tv_request = now;
819 /* OK let's go on with the BODY now */
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100820 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200821 return 1;
822
Christopher Fauletb8a53712019-12-16 11:29:38 +0100823 return_int_err:
824 txn->status = 500;
825 if (!(s->flags & SF_ERR_MASK))
826 s->flags |= SF_ERR_INTERNAL;
827 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100828 if (s->flags & SF_BE_ASSIGNED)
Christopher Fauletbe20cf32020-01-24 11:41:38 +0100829 _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1);
William Lallemand36119de2021-03-08 15:26:48 +0100830 if (sess->listener && sess->listener->counters)
Christopher Fauletb8a53712019-12-16 11:29:38 +0100831 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
832 goto return_prx_cond;
833
Christopher Faulete0768eb2018-10-03 16:38:02 +0200834 return_bad_req: /* let's centralize all bad requests */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200835 txn->status = 400;
Olivier Houcharda798bf52019-03-08 18:52:00 +0100836 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
William Lallemand36119de2021-03-08 15:26:48 +0100837 if (sess->listener && sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100838 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100839 /* fall through */
840
841 return_prx_cond:
842 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +0200843
844 if (!(s->flags & SF_ERR_MASK))
845 s->flags |= SF_ERR_PRXCOND;
846 if (!(s->flags & SF_FINST_MASK))
847 s->flags |= SF_FINST_R;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100848
849 req->analysers &= AN_REQ_FLT_END;
850 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100851 DBG_TRACE_DEVEL("leaving on error",
852 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200853 return 0;
854}
855
856/* This function is an analyser which processes the HTTP tarpit. It always
857 * returns zero, at the beginning because it prevents any other processing
858 * from occurring, and at the end because it terminates the request.
859 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200860int http_process_tarpit(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200861{
862 struct http_txn *txn = s->txn;
863
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100864 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, &txn->req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200865 /* This connection is being tarpitted. The CLIENT side has
866 * already set the connect expiration date to the right
867 * timeout. We just have to check that the client is still
868 * there and that the timeout has not expired.
869 */
870 channel_dont_connect(req);
871 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100872 !tick_is_expired(req->analyse_exp, now_ms)) {
873 DBG_TRACE_DEVEL("waiting for tarpit timeout expiry",
874 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200875 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100876 }
877
Christopher Faulete0768eb2018-10-03 16:38:02 +0200878
879 /* We will set the queue timer to the time spent, just for
880 * logging purposes. We fake a 500 server error, so that the
881 * attacker will not suspect his connection has been tarpitted.
882 * It will not cause trouble to the logs because we can exclude
883 * the tarpitted connections by filtering on the 'PT' status flags.
884 */
885 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
886
Christopher Faulet8dfeccf2020-05-15 14:16:29 +0200887 http_reply_and_close(s, txn->status, (!(req->flags & CF_READ_ERROR) ? http_error_message(s) : NULL));
Christopher Faulet5cb513a2020-05-13 17:56:56 +0200888
889 end:
Christopher Faulete0768eb2018-10-03 16:38:02 +0200890 req->analysers &= AN_REQ_FLT_END;
891 req->analyse_exp = TICK_ETERNITY;
892
893 if (!(s->flags & SF_ERR_MASK))
894 s->flags |= SF_ERR_PRXCOND;
895 if (!(s->flags & SF_FINST_MASK))
896 s->flags |= SF_FINST_T;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100897
898 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200899 return 0;
900}
901
902/* This function is an analyser which waits for the HTTP request body. It waits
903 * for either the buffer to be full, or the full advertised contents to have
904 * reached the buffer. It must only be called after the standard HTTP request
905 * processing has occurred, because it expects the request to be parsed and will
906 * look for the Expect header. It may send a 100-Continue interim response. It
907 * takes in input any state starting from HTTP_MSG_BODY and leaves with one of
908 * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it
909 * needs to read more data, or 1 once it has completed its analysis.
910 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200911int http_wait_for_request_body(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200912{
913 struct session *sess = s->sess;
914 struct http_txn *txn = s->txn;
915 struct http_msg *msg = &s->txn->req;
Christopher Fauletf76ebe82018-10-24 11:16:22 +0200916 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200917
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100918 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Fauletf76ebe82018-10-24 11:16:22 +0200919
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100920 htx = htxbuf(&req->buf);
Christopher Fauletf76ebe82018-10-24 11:16:22 +0200921
Willy Tarreau4236f032019-03-05 10:43:32 +0100922 if (htx->flags & HTX_FL_PARSING_ERROR)
923 goto return_bad_req;
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200924 if (htx->flags & HTX_FL_PROCESSING_ERROR)
925 goto return_int_err;
Willy Tarreau4236f032019-03-05 10:43:32 +0100926
Christopher Fauletc31bc722020-11-20 14:30:38 +0100927 /* Do nothing for bodyless and CONNECT requests */
928 if (txn->meth == HTTP_METH_CONNECT || (msg->flags & HTTP_MSGF_BODYLESS))
Christopher Faulet63c69a92020-11-16 16:03:35 +0100929 goto http_end;
930
Christopher Faulete0768eb2018-10-03 16:38:02 +0200931 /* We have to parse the HTTP request body to find any required data.
932 * "balance url_param check_post" should have been the only way to get
933 * into this. We were brought here after HTTP header analysis, so all
934 * related structures are ready.
935 */
936
Christopher Fauletf76ebe82018-10-24 11:16:22 +0200937 if (msg->msg_state < HTTP_MSG_DATA) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200938 if (http_handle_expect_hdr(s, htx, msg) == -1)
Christopher Fauletb8a53712019-12-16 11:29:38 +0100939 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200940 }
941
Christopher Fauletf76ebe82018-10-24 11:16:22 +0200942 msg->msg_state = HTTP_MSG_DATA;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200943
Christopher Fauletf76ebe82018-10-24 11:16:22 +0200944 /* Now we're in HTTP_MSG_DATA. We just need to know if all data have
945 * been received or if the buffer is full.
Christopher Faulete0768eb2018-10-03 16:38:02 +0200946 */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +0100947 if ((htx->flags & HTX_FL_EOM) || htx_get_tail_type(htx) > HTX_BLK_DATA ||
Christopher Fauletdcd8c5e2019-01-21 11:24:38 +0100948 channel_htx_full(req, htx, global.tune.maxrewrite))
Christopher Faulete0768eb2018-10-03 16:38:02 +0200949 goto http_end;
950
951 if ((req->flags & CF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
952 txn->status = 408;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200953 if (!(s->flags & SF_ERR_MASK))
954 s->flags |= SF_ERR_CLITO;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100955 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
William Lallemand36119de2021-03-08 15:26:48 +0100956 if (sess->listener && sess->listener->counters)
Christopher Fauletb8a53712019-12-16 11:29:38 +0100957 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
958 goto return_prx_cond;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200959 }
960
961 /* we get here if we need to wait for more data */
962 if (!(req->flags & (CF_SHUTR | CF_READ_ERROR))) {
963 /* Not enough data. We'll re-use the http-request
964 * timeout here. Ideally, we should set the timeout
965 * relative to the accept() date. We just set the
966 * request timeout once at the beginning of the
967 * request.
968 */
969 channel_dont_connect(req);
970 if (!tick_isset(req->analyse_exp))
971 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100972 DBG_TRACE_DEVEL("waiting for more data",
973 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200974 return 0;
975 }
976
977 http_end:
978 /* The situation will not evolve, so let's give up on the analysis. */
979 s->logs.tv_request = now; /* update the request timer to reflect full request */
980 req->analysers &= ~an_bit;
981 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100982 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200983 return 1;
984
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200985 return_int_err:
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200986 txn->status = 500;
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200987 if (!(s->flags & SF_ERR_MASK))
988 s->flags |= SF_ERR_INTERNAL;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100989 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100990 if (s->flags & SF_BE_ASSIGNED)
Christopher Fauletbe20cf32020-01-24 11:41:38 +0100991 _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1);
William Lallemand36119de2021-03-08 15:26:48 +0100992 if (sess->listener && sess->listener->counters)
Christopher Fauletb8a53712019-12-16 11:29:38 +0100993 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
994 goto return_prx_cond;
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200995
Christopher Faulete0768eb2018-10-03 16:38:02 +0200996 return_bad_req: /* let's centralize all bad requests */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200997 txn->status = 400;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100998 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
William Lallemand36119de2021-03-08 15:26:48 +0100999 if (sess->listener && sess->listener->counters)
Christopher Fauletb8a53712019-12-16 11:29:38 +01001000 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
1001 /* fall through */
1002
1003 return_prx_cond:
1004 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001005
1006 if (!(s->flags & SF_ERR_MASK))
1007 s->flags |= SF_ERR_PRXCOND;
1008 if (!(s->flags & SF_FINST_MASK))
Christopher Fauletb8a53712019-12-16 11:29:38 +01001009 s->flags |= (msg->msg_state < HTTP_MSG_DATA ? SF_FINST_R : SF_FINST_D);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001010
Christopher Faulete0768eb2018-10-03 16:38:02 +02001011 req->analysers &= AN_REQ_FLT_END;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001012 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001013 DBG_TRACE_DEVEL("leaving on error",
1014 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001015 return 0;
1016}
1017
1018/* This function is an analyser which forwards request body (including chunk
1019 * sizes if any). It is called as soon as we must forward, even if we forward
1020 * zero byte. The only situation where it must not be called is when we're in
1021 * tunnel mode and we want to forward till the close. It's used both to forward
1022 * remaining data and to resync after end of body. It expects the msg_state to
1023 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
1024 * read more data, or 1 once we can go on with next request or end the stream.
1025 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
1026 * bytes of pending data + the headers if not already done.
1027 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001028int http_request_forward_body(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001029{
1030 struct session *sess = s->sess;
1031 struct http_txn *txn = s->txn;
Christopher Faulet9768c262018-10-22 09:34:31 +02001032 struct http_msg *msg = &txn->req;
1033 struct htx *htx;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001034 short status = 0;
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001035 int ret;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001036
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001037 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001038
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001039 htx = htxbuf(&req->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001040
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001041 if (htx->flags & HTX_FL_PARSING_ERROR)
1042 goto return_bad_req;
1043 if (htx->flags & HTX_FL_PROCESSING_ERROR)
1044 goto return_int_err;
1045
Christopher Faulete0768eb2018-10-03 16:38:02 +02001046 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
1047 ((req->flags & CF_SHUTW) && (req->to_forward || co_data(req)))) {
1048 /* Output closed while we were sending data. We must abort and
1049 * wake the other side up.
1050 */
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001051
Olivier Houchard29cac3c2019-07-12 15:48:58 +02001052 /* Don't abort yet if we had L7 retries activated and it
1053 * was a write error, we may recover.
1054 */
1055 if (!(req->flags & (CF_READ_ERROR | CF_READ_TIMEOUT)) &&
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001056 (s->si[1].flags & SI_FL_L7_RETRY)) {
1057 DBG_TRACE_DEVEL("leaving on L7 retry",
1058 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Olivier Houchard29cac3c2019-07-12 15:48:58 +02001059 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001060 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001061 msg->msg_state = HTTP_MSG_ERROR;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001062 http_end_request(s);
1063 http_end_response(s);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001064 DBG_TRACE_DEVEL("leaving on error",
1065 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001066 return 1;
1067 }
1068
1069 /* Note that we don't have to send 100-continue back because we don't
1070 * need the data to complete our job, and it's up to the server to
1071 * decide whether to return 100, 417 or anything else in return of
1072 * an "Expect: 100-continue" header.
1073 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001074 if (msg->msg_state == HTTP_MSG_BODY)
1075 msg->msg_state = HTTP_MSG_DATA;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001076
Christopher Faulete0768eb2018-10-03 16:38:02 +02001077 /* in most states, we should abort in case of early close */
1078 channel_auto_close(req);
1079
1080 if (req->to_forward) {
Christopher Faulet66af0b22019-03-22 14:54:52 +01001081 if (req->to_forward == CHN_INFINITE_FORWARD) {
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001082 if (req->flags & CF_EOI)
1083 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet66af0b22019-03-22 14:54:52 +01001084 }
1085 else {
1086 /* We can't process the buffer's contents yet */
1087 req->flags |= CF_WAKE_WRITE;
1088 goto missing_data_or_waiting;
1089 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001090 }
1091
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001092 if (msg->msg_state >= HTTP_MSG_ENDING)
1093 goto ending;
1094
1095 if (txn->meth == HTTP_METH_CONNECT) {
1096 msg->msg_state = HTTP_MSG_ENDING;
1097 goto ending;
1098 }
1099
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001100 /* Forward input data. We get it by removing all outgoing data not
1101 * forwarded yet from HTX data size. If there are some data filters, we
1102 * let them decide the amount of data to forward.
Christopher Faulet9768c262018-10-22 09:34:31 +02001103 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001104 if (HAS_REQ_DATA_FILTERS(s)) {
1105 ret = flt_http_payload(s, msg, htx->data);
1106 if (ret < 0)
1107 goto return_bad_req;
Christopher Faulet421e7692019-06-13 11:16:45 +02001108 c_adv(req, ret);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001109 }
1110 else {
Christopher Faulet421e7692019-06-13 11:16:45 +02001111 c_adv(req, htx->data - co_data(req));
Christopher Faulet66af0b22019-03-22 14:54:52 +01001112 if (msg->flags & HTTP_MSGF_XFER_LEN)
1113 channel_htx_forward_forever(req, htx);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001114 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001115
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001116 if (htx->data != co_data(req))
1117 goto missing_data_or_waiting;
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001118
Christopher Faulet9768c262018-10-22 09:34:31 +02001119 /* Check if the end-of-message is reached and if so, switch the message
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001120 * in HTTP_MSG_ENDING state. Then if all data was marked to be
1121 * forwarded, set the state to HTTP_MSG_DONE.
Christopher Faulet9768c262018-10-22 09:34:31 +02001122 */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001123 if (!(htx->flags & HTX_FL_EOM))
Christopher Faulet9768c262018-10-22 09:34:31 +02001124 goto missing_data_or_waiting;
1125
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001126 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet9768c262018-10-22 09:34:31 +02001127
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001128 ending:
Christopher Faulet2151cdd2020-07-22 16:34:59 +02001129 req->flags &= ~CF_EXPECT_MORE; /* no more data are expected */
1130
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001131 /* other states, ENDING...TUNNEL */
1132 if (msg->msg_state >= HTTP_MSG_DONE)
1133 goto done;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001134
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001135 if (HAS_REQ_DATA_FILTERS(s)) {
1136 ret = flt_http_end(s, msg);
1137 if (ret <= 0) {
1138 if (!ret)
1139 goto missing_data_or_waiting;
1140 goto return_bad_req;
1141 }
1142 }
1143
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001144 if (txn->meth == HTTP_METH_CONNECT)
1145 msg->msg_state = HTTP_MSG_TUNNEL;
1146 else {
1147 msg->msg_state = HTTP_MSG_DONE;
1148 req->to_forward = 0;
Christopher Faulet5b82cc52020-10-12 15:18:50 +02001149
1150 if ((s->be->retry_type &~ PR_RE_CONN_FAILED) && !(s->si[1].flags & SI_FL_D_L7_RETRY)) {
1151 struct stream_interface *si = &s->si[1];
1152
1153 /* If we want to be able to do L7 retries, copy the
1154 * request, so that we are able to resend them if
1155 * needed.
1156 *
1157 * Try to allocate a buffer if we had none. If it
1158 * fails, the next test will just disable the l7
1159 * retries.
1160 */
1161 DBG_TRACE_STATE("enable L7 retry, save the request", STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
1162 si->flags |= SI_FL_L7_RETRY;
Willy Tarreauf499f502021-03-22 16:17:37 +01001163 if (b_alloc(&si->l7_buffer) == NULL)
Christopher Faulet5b82cc52020-10-12 15:18:50 +02001164 si->flags &= ~SI_FL_L7_RETRY;
1165 else {
1166 memcpy(b_orig(&si->l7_buffer), b_orig(&req->buf), b_size(&req->buf));
1167 b_add(&si->l7_buffer, co_data(req));
1168 }
1169 }
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001170 }
1171
1172 done:
1173 /* we don't want to forward closes on DONE except in tunnel mode. */
1174 if (!(txn->flags & TX_CON_WANT_TUN))
1175 channel_dont_close(req);
1176
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001177 http_end_request(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001178 if (!(req->analysers & an_bit)) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001179 http_end_response(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001180 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
1181 if (req->flags & CF_SHUTW) {
1182 /* request errors are most likely due to the
1183 * server aborting the transfer. */
Christopher Faulet93e02d82019-03-08 14:18:50 +01001184 goto return_srv_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001185 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001186 goto return_bad_req;
1187 }
Christopher Faulet5b82cc52020-10-12 15:18:50 +02001188
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001189 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001190 return 1;
1191 }
1192
1193 /* If "option abortonclose" is set on the backend, we want to monitor
1194 * the client's connection and forward any shutdown notification to the
1195 * server, which will decide whether to close or to go on processing the
1196 * request. We only do that in tunnel mode, and not in other modes since
1197 * it can be abused to exhaust source ports. */
Christopher Faulet769d0e92019-03-22 14:23:18 +01001198 if (s->be->options & PR_O_ABRT_CLOSE) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001199 channel_auto_read(req);
Christopher Fauletc41547b2019-07-16 14:32:23 +02001200 if ((req->flags & (CF_SHUTR|CF_READ_NULL)) && !(txn->flags & TX_CON_WANT_TUN))
Christopher Faulete0768eb2018-10-03 16:38:02 +02001201 s->si[1].flags |= SI_FL_NOLINGER;
1202 channel_auto_close(req);
1203 }
1204 else if (s->txn->meth == HTTP_METH_POST) {
1205 /* POST requests may require to read extra CRLF sent by broken
1206 * browsers and which could cause an RST to be sent upon close
1207 * on some systems (eg: Linux). */
1208 channel_auto_read(req);
1209 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001210 DBG_TRACE_DEVEL("waiting for the end of the HTTP txn",
1211 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001212 return 0;
1213
1214 missing_data_or_waiting:
1215 /* stop waiting for data if the input is closed before the end */
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001216 if (msg->msg_state < HTTP_MSG_ENDING && req->flags & CF_SHUTR)
Christopher Faulet93e02d82019-03-08 14:18:50 +01001217 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001218
1219 waiting:
1220 /* waiting for the last bits to leave the buffer */
1221 if (req->flags & CF_SHUTW)
Christopher Faulet93e02d82019-03-08 14:18:50 +01001222 goto return_srv_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001223
1224 /* When TE: chunked is used, we need to get there again to parse remaining
1225 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
1226 * And when content-length is used, we never want to let the possible
1227 * shutdown be forwarded to the other side, as the state machine will
1228 * take care of it once the client responds. It's also important to
1229 * prevent TIME_WAITs from accumulating on the backend side, and for
1230 * HTTP/2 where the last frame comes with a shutdown.
1231 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001232 if (msg->flags & HTTP_MSGF_XFER_LEN)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001233 channel_dont_close(req);
1234
1235 /* We know that more data are expected, but we couldn't send more that
1236 * what we did. So we always set the CF_EXPECT_MORE flag so that the
1237 * system knows it must not set a PUSH on this first part. Interactive
1238 * modes are already handled by the stream sock layer. We must not do
1239 * this in content-length mode because it could present the MSG_MORE
1240 * flag with the last block of forwarded data, which would cause an
1241 * additional delay to be observed by the receiver.
1242 */
Christopher Faulet2151cdd2020-07-22 16:34:59 +02001243 if (HAS_REQ_DATA_FILTERS(s))
Christopher Faulete0768eb2018-10-03 16:38:02 +02001244 req->flags |= CF_EXPECT_MORE;
1245
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001246 DBG_TRACE_DEVEL("waiting for more data to forward",
1247 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001248 return 0;
1249
Christopher Faulet93e02d82019-03-08 14:18:50 +01001250 return_cli_abort:
1251 _HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
1252 _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
William Lallemand36119de2021-03-08 15:26:48 +01001253 if (sess->listener && sess->listener->counters)
Christopher Fauletcff0f732019-12-16 16:13:44 +01001254 _HA_ATOMIC_ADD(&sess->listener->counters->cli_aborts, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001255 if (objt_server(s->target))
Christopher Fauletcff0f732019-12-16 16:13:44 +01001256 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.cli_aborts, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001257 if (!(s->flags & SF_ERR_MASK))
1258 s->flags |= SF_ERR_CLICL;
1259 status = 400;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001260 goto return_prx_cond;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001261
1262 return_srv_abort:
1263 _HA_ATOMIC_ADD(&sess->fe->fe_counters.srv_aborts, 1);
1264 _HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1);
William Lallemand36119de2021-03-08 15:26:48 +01001265 if (sess->listener && sess->listener->counters)
Christopher Fauletcff0f732019-12-16 16:13:44 +01001266 _HA_ATOMIC_ADD(&sess->listener->counters->srv_aborts, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001267 if (objt_server(s->target))
Christopher Fauletcff0f732019-12-16 16:13:44 +01001268 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.srv_aborts, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001269 if (!(s->flags & SF_ERR_MASK))
1270 s->flags |= SF_ERR_SRVCL;
1271 status = 502;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001272 goto return_prx_cond;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001273
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001274 return_int_err:
1275 if (!(s->flags & SF_ERR_MASK))
1276 s->flags |= SF_ERR_INTERNAL;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001277 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001278 _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1);
William Lallemand36119de2021-03-08 15:26:48 +01001279 if (sess->listener && sess->listener->counters)
Christopher Fauletb8a53712019-12-16 11:29:38 +01001280 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001281 if (objt_server(s->target))
1282 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.internal_errors, 1);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001283 status = 500;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001284 goto return_prx_cond;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001285
Christopher Faulet93e02d82019-03-08 14:18:50 +01001286 return_bad_req:
Olivier Houcharda798bf52019-03-08 18:52:00 +01001287 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
William Lallemand36119de2021-03-08 15:26:48 +01001288 if (sess->listener && sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +01001289 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001290 status = 400;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001291 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001292
Christopher Fauletb8a53712019-12-16 11:29:38 +01001293 return_prx_cond:
Christopher Faulet9768c262018-10-22 09:34:31 +02001294 if (txn->status > 0) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001295 /* Note: we don't send any error if some data were already sent */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001296 http_reply_and_close(s, txn->status, NULL);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001297 } else {
Christopher Faulet93e02d82019-03-08 14:18:50 +01001298 txn->status = status;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001299 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001300 }
1301 req->analysers &= AN_REQ_FLT_END;
1302 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 +01001303 if (!(s->flags & SF_ERR_MASK))
1304 s->flags |= SF_ERR_PRXCOND;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001305 if (!(s->flags & SF_FINST_MASK))
1306 s->flags |= ((txn->rsp.msg_state < HTTP_MSG_ERROR) ? SF_FINST_H : SF_FINST_D);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001307 DBG_TRACE_DEVEL("leaving on error ",
1308 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001309 return 0;
1310}
1311
Olivier Houcharda254a372019-04-05 15:30:12 +02001312/* Reset the stream and the backend stream_interface to a situation suitable for attemption connection */
1313/* Returns 0 if we can attempt to retry, -1 otherwise */
1314static __inline int do_l7_retry(struct stream *s, struct stream_interface *si)
1315{
Christopher Faulet5b82cc52020-10-12 15:18:50 +02001316 struct channel *req = &s->req;
1317 struct channel *res = &s->res;
Olivier Houcharda254a372019-04-05 15:30:12 +02001318
1319 si->conn_retries--;
1320 if (si->conn_retries < 0)
Christopher Faulet5b82cc52020-10-12 15:18:50 +02001321 goto no_retry;
1322
1323 if (b_is_null(&req->buf) && !channel_alloc_buffer(req, &s->buffer_wait))
1324 goto no_retry;
Olivier Houcharda254a372019-04-05 15:30:12 +02001325
Willy Tarreau223995e2019-05-04 10:38:31 +02001326 if (objt_server(s->target))
1327 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.retries, 1);
1328 _HA_ATOMIC_ADD(&s->be->be_counters.retries, 1);
1329
Olivier Houcharda254a372019-04-05 15:30:12 +02001330 /* Remove any write error from the request, and read error from the response */
1331 req->flags &= ~(CF_WRITE_ERROR | CF_WRITE_TIMEOUT | CF_SHUTW | CF_SHUTW_NOW);
1332 res->flags &= ~(CF_READ_ERROR | CF_READ_TIMEOUT | CF_SHUTR | CF_EOI | CF_READ_NULL | CF_SHUTR_NOW);
1333 res->analysers = 0;
1334 si->flags &= ~(SI_FL_ERR | SI_FL_EXP | SI_FL_RXBLK_SHUT);
Olivier Houchard8cabc972020-05-12 22:18:14 +02001335 s->flags &= ~SF_ADDR_SET;
Olivier Houchard4bd58672019-07-12 16:16:59 +02001336 stream_choose_redispatch(s);
Olivier Houcharda254a372019-04-05 15:30:12 +02001337 si->exp = TICK_ETERNITY;
1338 res->rex = TICK_ETERNITY;
1339 res->to_forward = 0;
1340 res->analyse_exp = TICK_ETERNITY;
1341 res->total = 0;
Olivier Houchard4bd58672019-07-12 16:16:59 +02001342 s->flags &= ~(SF_ERR_SRVTO | SF_ERR_SRVCL);
Olivier Houcharda254a372019-04-05 15:30:12 +02001343 si_release_endpoint(&s->si[1]);
Olivier Houcharda254a372019-04-05 15:30:12 +02001344
Christopher Faulet5b82cc52020-10-12 15:18:50 +02001345 b_reset(&req->buf);
1346 memcpy(b_orig(&req->buf), b_orig(&si->l7_buffer), b_size(&si->l7_buffer));
1347 b_set_data(&req->buf, b_size(&req->buf));
1348 co_set_data(req, b_data(&si->l7_buffer));
1349
Ilya Shipitsinacf84592021-02-06 22:29:08 +05001350 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 +02001351 b_reset(&res->buf);
1352 co_set_data(res, 0);
1353 return 0;
Christopher Faulet5b82cc52020-10-12 15:18:50 +02001354
1355 no_retry:
1356 b_free(&si->l7_buffer);
1357 return -1;
Olivier Houcharda254a372019-04-05 15:30:12 +02001358}
1359
Christopher Faulete0768eb2018-10-03 16:38:02 +02001360/* This stream analyser waits for a complete HTTP response. It returns 1 if the
1361 * processing can continue on next analysers, or zero if it either needs more
1362 * data or wants to immediately abort the response (eg: timeout, error, ...). It
1363 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->res.analysers
1364 * when it has nothing left to do, and may remove any analyser when it wants to
1365 * abort.
1366 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001367int http_wait_for_response(struct stream *s, struct channel *rep, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001368{
Christopher Faulet9768c262018-10-22 09:34:31 +02001369 /*
1370 * We will analyze a complete HTTP response to check the its syntax.
1371 *
1372 * Once the start line and all headers are received, we may perform a
1373 * capture of the error (if any), and we will set a few fields. We also
1374 * logging and finally headers capture.
1375 */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001376 struct session *sess = s->sess;
1377 struct http_txn *txn = s->txn;
1378 struct http_msg *msg = &txn->rsp;
Christopher Faulet9768c262018-10-22 09:34:31 +02001379 struct htx *htx;
Olivier Houcharda254a372019-04-05 15:30:12 +02001380 struct stream_interface *si_b = &s->si[1];
Christopher Faulet61608322018-11-23 16:23:45 +01001381 struct connection *srv_conn;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001382 struct htx_sl *sl;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001383 int n;
1384
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001385 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001386
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001387 htx = htxbuf(&rep->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001388
Willy Tarreau4236f032019-03-05 10:43:32 +01001389 /* Parsing errors are caught here */
1390 if (htx->flags & HTX_FL_PARSING_ERROR)
1391 goto return_bad_res;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001392 if (htx->flags & HTX_FL_PROCESSING_ERROR)
1393 goto return_int_err;
Willy Tarreau4236f032019-03-05 10:43:32 +01001394
Christopher Faulete0768eb2018-10-03 16:38:02 +02001395 /*
1396 * Now we quickly check if we have found a full valid response.
1397 * If not so, we check the FD and buffer states before leaving.
1398 * A full response is indicated by the fact that we have seen
1399 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
1400 * responses are checked first.
1401 *
1402 * Depending on whether the client is still there or not, we
1403 * may send an error response back or not. Note that normally
1404 * we should only check for HTTP status there, and check I/O
1405 * errors somewhere else.
1406 */
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001407 next_one:
Christopher Faulet29f17582019-05-23 11:03:26 +02001408 if (unlikely(htx_is_empty(htx) || htx->first == -1)) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001409 /* 1: have we encountered a read error ? */
1410 if (rep->flags & CF_READ_ERROR) {
Olivier Houchard865d8392019-05-03 22:46:27 +02001411 struct connection *conn = NULL;
1412
Olivier Houchard865d8392019-05-03 22:46:27 +02001413 if (objt_cs(s->si[1].end))
1414 conn = objt_cs(s->si[1].end)->conn;
1415
1416 if (si_b->flags & SI_FL_L7_RETRY &&
1417 (!conn || conn->err_code != CO_ER_SSL_EARLY_FAILED)) {
Olivier Houcharda254a372019-04-05 15:30:12 +02001418 /* If we arrive here, then CF_READ_ERROR was
1419 * set by si_cs_recv() because we matched a
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05001420 * status, otherwise it would have removed
Olivier Houcharda254a372019-04-05 15:30:12 +02001421 * the SI_FL_L7_RETRY flag, so it's ok not
1422 * to check s->be->retry_type.
1423 */
Christopher Faulet5b82cc52020-10-12 15:18:50 +02001424 if (co_data(rep) || do_l7_retry(s, si_b) == 0) {
1425 DBG_TRACE_DEVEL("leaving on L7 retry",
1426 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharda254a372019-04-05 15:30:12 +02001427 return 0;
Christopher Faulet5b82cc52020-10-12 15:18:50 +02001428 }
Olivier Houcharda254a372019-04-05 15:30:12 +02001429 }
1430
Olivier Houchard6db16992019-05-17 15:40:49 +02001431 if (txn->flags & TX_NOT_FIRST)
1432 goto abort_keep_alive;
1433
Olivier Houcharda798bf52019-03-08 18:52:00 +01001434 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001435 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001436 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001437 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001438 }
1439
Christopher Faulete0768eb2018-10-03 16:38:02 +02001440 rep->analysers &= AN_RES_FLT_END;
1441 txn->status = 502;
1442
1443 /* Check to see if the server refused the early data.
1444 * If so, just send a 425
1445 */
Willy Tarreauee99aaf2020-06-23 05:58:20 +02001446 if (conn && conn->err_code == CO_ER_SSL_EARLY_FAILED) {
Olivier Houchard865d8392019-05-03 22:46:27 +02001447 if ((s->be->retry_type & PR_RE_EARLY_ERROR) &&
Olivier Houchardad26d8d2019-05-10 17:48:28 +02001448 (si_b->flags & SI_FL_L7_RETRY) &&
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001449 do_l7_retry(s, si_b) == 0) {
1450 DBG_TRACE_DEVEL("leaving on L7 retry",
1451 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houchard865d8392019-05-03 22:46:27 +02001452 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001453 }
Olivier Houchard865d8392019-05-03 22:46:27 +02001454 txn->status = 425;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001455 }
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001456 else
1457 stream_inc_http_fail_ctr(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001458
1459 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001460 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001461
1462 if (!(s->flags & SF_ERR_MASK))
1463 s->flags |= SF_ERR_SRVCL;
1464 if (!(s->flags & SF_FINST_MASK))
1465 s->flags |= SF_FINST_H;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001466 DBG_TRACE_DEVEL("leaving on error",
1467 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001468 return 0;
1469 }
1470
Christopher Faulet9768c262018-10-22 09:34:31 +02001471 /* 2: read timeout : return a 504 to the client. */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001472 else if (rep->flags & CF_READ_TIMEOUT) {
Olivier Houcharda254a372019-04-05 15:30:12 +02001473 if ((si_b->flags & SI_FL_L7_RETRY) &&
1474 (s->be->retry_type & PR_RE_TIMEOUT)) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001475 if (co_data(rep) || do_l7_retry(s, si_b) == 0) {
1476 DBG_TRACE_DEVEL("leaving on L7 retry",
1477 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharda254a372019-04-05 15:30:12 +02001478 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001479 }
Olivier Houcharda254a372019-04-05 15:30:12 +02001480 }
Olivier Houcharda798bf52019-03-08 18:52:00 +01001481 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001482 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001483 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001484 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001485 }
1486
Christopher Faulete0768eb2018-10-03 16:38:02 +02001487 rep->analysers &= AN_RES_FLT_END;
1488 txn->status = 504;
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001489 stream_inc_http_fail_ctr(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001490 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001491 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001492
1493 if (!(s->flags & SF_ERR_MASK))
1494 s->flags |= SF_ERR_SRVTO;
1495 if (!(s->flags & SF_FINST_MASK))
1496 s->flags |= SF_FINST_H;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001497 DBG_TRACE_DEVEL("leaving on error",
1498 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001499 return 0;
1500 }
1501
Christopher Faulet9768c262018-10-22 09:34:31 +02001502 /* 3: client abort with an abortonclose */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001503 else if ((rep->flags & CF_SHUTR) && ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001504 _HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
1505 _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
William Lallemand36119de2021-03-08 15:26:48 +01001506 if (sess->listener && sess->listener->counters)
Christopher Fauletcff0f732019-12-16 16:13:44 +01001507 _HA_ATOMIC_ADD(&sess->listener->counters->cli_aborts, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001508 if (objt_server(s->target))
Olivier Houcharda798bf52019-03-08 18:52:00 +01001509 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.cli_aborts, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001510
1511 rep->analysers &= AN_RES_FLT_END;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001512 txn->status = 400;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001513 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001514
1515 if (!(s->flags & SF_ERR_MASK))
1516 s->flags |= SF_ERR_CLICL;
1517 if (!(s->flags & SF_FINST_MASK))
1518 s->flags |= SF_FINST_H;
1519
1520 /* process_stream() will take care of the error */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001521 DBG_TRACE_DEVEL("leaving on error",
1522 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001523 return 0;
1524 }
1525
Christopher Faulet9768c262018-10-22 09:34:31 +02001526 /* 4: close from server, capture the response if the server has started to respond */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001527 else if (rep->flags & CF_SHUTR) {
Olivier Houcharda254a372019-04-05 15:30:12 +02001528 if ((si_b->flags & SI_FL_L7_RETRY) &&
1529 (s->be->retry_type & PR_RE_DISCONNECTED)) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001530 if (co_data(rep) || do_l7_retry(s, si_b) == 0) {
1531 DBG_TRACE_DEVEL("leaving on L7 retry",
1532 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharda254a372019-04-05 15:30:12 +02001533 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001534 }
Olivier Houcharda254a372019-04-05 15:30:12 +02001535 }
1536
Olivier Houchard6db16992019-05-17 15:40:49 +02001537 if (txn->flags & TX_NOT_FIRST)
1538 goto abort_keep_alive;
1539
Olivier Houcharda798bf52019-03-08 18:52:00 +01001540 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001541 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001542 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001543 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001544 }
1545
Christopher Faulete0768eb2018-10-03 16:38:02 +02001546 rep->analysers &= AN_RES_FLT_END;
1547 txn->status = 502;
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001548 stream_inc_http_fail_ctr(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001549 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001550 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001551
1552 if (!(s->flags & SF_ERR_MASK))
1553 s->flags |= SF_ERR_SRVCL;
1554 if (!(s->flags & SF_FINST_MASK))
1555 s->flags |= SF_FINST_H;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001556 DBG_TRACE_DEVEL("leaving on error",
1557 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001558 return 0;
1559 }
1560
Christopher Faulet9768c262018-10-22 09:34:31 +02001561 /* 5: write error to client (we don't send any message then) */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001562 else if (rep->flags & CF_WRITE_ERROR) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001563 if (txn->flags & TX_NOT_FIRST)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001564 goto abort_keep_alive;
1565
Olivier Houcharda798bf52019-03-08 18:52:00 +01001566 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001567 if (objt_server(s->target))
1568 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001569 rep->analysers &= AN_RES_FLT_END;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001570
1571 if (!(s->flags & SF_ERR_MASK))
1572 s->flags |= SF_ERR_CLICL;
1573 if (!(s->flags & SF_FINST_MASK))
1574 s->flags |= SF_FINST_H;
1575
1576 /* process_stream() will take care of the error */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001577 DBG_TRACE_DEVEL("leaving on error",
1578 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001579 return 0;
1580 }
1581
1582 channel_dont_close(rep);
1583 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001584 DBG_TRACE_DEVEL("waiting for more data",
1585 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001586 return 0;
1587 }
1588
1589 /* More interesting part now : we know that we have a complete
1590 * response which at least looks like HTTP. We have an indicator
1591 * of each header's length, so we can parse them quickly.
1592 */
Christopher Faulet29f17582019-05-23 11:03:26 +02001593 BUG_ON(htx_get_first_type(htx) != HTX_BLK_RES_SL);
Christopher Faulet297fbb42019-05-13 14:41:27 +02001594 sl = http_get_stline(htx);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001595
Christopher Faulet5b82cc52020-10-12 15:18:50 +02001596 if ((si_b->flags & SI_FL_L7_RETRY) &&
1597 l7_status_match(s->be, sl->info.res.status) &&
1598 do_l7_retry(s, si_b) == 0) {
1599 DBG_TRACE_DEVEL("leaving on L7 retry", STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
1600 return 0;
1601 }
1602 b_free(&s->si[1].l7_buffer);
1603
1604 msg->msg_state = HTTP_MSG_BODY;
1605
Christopher Faulet9768c262018-10-22 09:34:31 +02001606 /* 0: we might have to print this header in debug mode */
1607 if (unlikely((global.mode & MODE_DEBUG) &&
1608 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
1609 int32_t pos;
1610
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001611 http_debug_stline("srvrep", s, sl);
Christopher Faulet9768c262018-10-22 09:34:31 +02001612
Christopher Fauleta3f15502019-05-13 15:27:23 +02001613 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001614 struct htx_blk *blk = htx_get_blk(htx, pos);
1615 enum htx_blk_type type = htx_get_blk_type(blk);
1616
1617 if (type == HTX_BLK_EOH)
1618 break;
1619 if (type != HTX_BLK_HDR)
1620 continue;
1621
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001622 http_debug_hdr("srvhdr", s,
1623 htx_get_blk_name(htx, blk),
1624 htx_get_blk_value(htx, blk));
Christopher Faulet9768c262018-10-22 09:34:31 +02001625 }
1626 }
1627
Christopher Faulet03599112018-11-27 11:21:21 +01001628 /* 1: get the status code and the version. Also set HTTP flags */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001629 txn->status = sl->info.res.status;
Christopher Faulet03599112018-11-27 11:21:21 +01001630 if (sl->flags & HTX_SL_F_VER_11)
Christopher Faulet9768c262018-10-22 09:34:31 +02001631 msg->flags |= HTTP_MSGF_VER_11;
Christopher Faulet03599112018-11-27 11:21:21 +01001632 if (sl->flags & HTX_SL_F_XFER_LEN) {
1633 msg->flags |= HTTP_MSGF_XFER_LEN;
Christopher Faulet2a408542020-11-20 14:22:37 +01001634 if (sl->flags & HTX_SL_F_CLEN)
1635 msg->flags |= HTTP_MSGF_CNT_LEN;
1636 else if (sl->flags & HTX_SL_F_CHNK)
1637 msg->flags |= HTTP_MSGF_TE_CHNK;
Christopher Faulet03599112018-11-27 11:21:21 +01001638 }
Christopher Faulet2a408542020-11-20 14:22:37 +01001639 if (sl->flags & HTX_SL_F_BODYLESS)
1640 msg->flags |= HTTP_MSGF_BODYLESS;
Christopher Faulet576c3582021-01-08 15:53:01 +01001641 if (sl->flags & HTX_SL_F_CONN_UPG)
1642 msg->flags |= HTTP_MSGF_CONN_UPG;
Christopher Faulet9768c262018-10-22 09:34:31 +02001643
1644 n = txn->status / 100;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001645 if (n < 1 || n > 5)
1646 n = 0;
Christopher Faulet9768c262018-10-22 09:34:31 +02001647
Christopher Faulete0768eb2018-10-03 16:38:02 +02001648 /* when the client triggers a 4xx from the server, it's most often due
1649 * to a missing object or permission. These events should be tracked
1650 * because if they happen often, it may indicate a brute force or a
1651 * vulnerability scan.
1652 */
1653 if (n == 4)
1654 stream_inc_http_err_ctr(s);
1655
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001656 if (n == 5 && txn->status != 501 && txn->status != 505)
1657 stream_inc_http_fail_ctr(s);
1658
Marcin Deranek3c27dda2020-05-15 18:32:51 +02001659 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001660 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.p.http.rsp[n], 1);
Marcin Deranek3c27dda2020-05-15 18:32:51 +02001661 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.p.http.cum_req, 1);
1662 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001663
Christopher Faulete0768eb2018-10-03 16:38:02 +02001664 /* Adjust server's health based on status code. Note: status codes 501
1665 * and 505 are triggered on demand by client request, so we must not
1666 * count them as server failures.
1667 */
1668 if (objt_server(s->target)) {
1669 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001670 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_OK);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001671 else
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001672 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_STS);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001673 }
1674
1675 /*
1676 * We may be facing a 100-continue response, or any other informational
1677 * 1xx response which is non-final, in which case this is not the right
1678 * response, and we're waiting for the next one. Let's allow this response
1679 * to go to the client and wait for the next one. There's an exception for
1680 * 101 which is used later in the code to switch protocols.
1681 */
1682 if (txn->status < 200 &&
1683 (txn->status == 100 || txn->status >= 102)) {
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001684 FLT_STRM_CB(s, flt_http_reset(s, msg));
Christopher Faulet421e7692019-06-13 11:16:45 +02001685 htx->first = channel_htx_fwd_headers(rep, htx);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001686 msg->msg_state = HTTP_MSG_RPBEFORE;
Christopher Faulet3499f622019-09-03 15:23:54 +02001687 msg->flags = 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001688 txn->status = 0;
1689 s->logs.t_data = -1; /* was not a response yet */
Christopher Faulet7d518452020-08-31 11:07:07 +02001690 rep->flags |= CF_SEND_DONTWAIT; /* Send ASAP informational messages */
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001691 goto next_one;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001692 }
1693
Christopher Faulet6e6c7b12021-01-08 16:02:05 +01001694 /* A 101-switching-protocols must contains a Connection header with the
1695 * "upgrade" option and the request too. It means both are agree to
1696 * upgrade. It is not so strict because there is no test on the Upgrade
1697 * header content. But it is probably stronger enough for now.
1698 */
1699 if (txn->status == 101 &&
1700 (!(txn->req.flags & HTTP_MSGF_CONN_UPG) || !(txn->rsp.flags & HTTP_MSGF_CONN_UPG)))
1701 goto return_bad_res;
1702
Christopher Faulete0768eb2018-10-03 16:38:02 +02001703 /*
1704 * 2: check for cacheability.
1705 */
1706
1707 switch (txn->status) {
1708 case 200:
1709 case 203:
1710 case 204:
1711 case 206:
1712 case 300:
1713 case 301:
1714 case 404:
1715 case 405:
1716 case 410:
1717 case 414:
1718 case 501:
1719 break;
1720 default:
1721 /* RFC7231#6.1:
1722 * Responses with status codes that are defined as
1723 * cacheable by default (e.g., 200, 203, 204, 206,
1724 * 300, 301, 404, 405, 410, 414, and 501 in this
1725 * specification) can be reused by a cache with
1726 * heuristic expiration unless otherwise indicated
1727 * by the method definition or explicit cache
1728 * controls [RFC7234]; all other status codes are
1729 * not cacheable by default.
1730 */
1731 txn->flags &= ~(TX_CACHEABLE | TX_CACHE_COOK);
1732 break;
1733 }
1734
1735 /*
1736 * 3: we may need to capture headers
1737 */
1738 s->logs.logwait &= ~LW_RESP;
1739 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->res_cap))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001740 http_capture_headers(htx, s->res_cap, sess->fe->rsp_cap);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001741
Christopher Faulet9768c262018-10-22 09:34:31 +02001742 /* Skip parsing if no content length is possible. */
Christopher Fauletc75668e2020-12-07 18:10:32 +01001743 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status >= 200 && txn->status < 300) ||
Christopher Faulete0768eb2018-10-03 16:38:02 +02001744 txn->status == 101)) {
1745 /* Either we've established an explicit tunnel, or we're
1746 * switching the protocol. In both cases, we're very unlikely
1747 * to understand the next protocols. We have to switch to tunnel
1748 * mode, so that we transfer the request and responses then let
1749 * this protocol pass unmodified. When we later implement specific
1750 * parsers for such protocols, we'll want to check the Upgrade
1751 * header which contains information about that protocol for
1752 * responses with status 101 (eg: see RFC2817 about TLS).
1753 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02001754 txn->flags |= TX_CON_WANT_TUN;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001755 }
1756
Christopher Faulet61608322018-11-23 16:23:45 +01001757 /* check for NTML authentication headers in 401 (WWW-Authenticate) and
1758 * 407 (Proxy-Authenticate) responses and set the connection to private
1759 */
1760 srv_conn = cs_conn(objt_cs(s->si[1].end));
1761 if (srv_conn) {
1762 struct ist hdr;
1763 struct http_hdr_ctx ctx;
1764
1765 if (txn->status == 401)
1766 hdr = ist("WWW-Authenticate");
1767 else if (txn->status == 407)
1768 hdr = ist("Proxy-Authenticate");
1769 else
1770 goto end;
1771
1772 ctx.blk = NULL;
1773 while (http_find_header(htx, hdr, &ctx, 0)) {
Willy Tarreauf1dcced2020-05-07 19:27:02 +02001774 /* If www-authenticate contains "Negotiate", "Nego2", or "NTLM",
1775 * possibly followed by blanks and a base64 string, the connection
1776 * is private. Since it's a mess to deal with, we only check for
1777 * values starting with "NTLM" or "Nego". Note that often multiple
1778 * headers are sent by the server there.
1779 */
1780 if ((ctx.value.len >= 4 && strncasecmp(ctx.value.ptr, "Nego", 4) == 0) ||
Willy Tarreau49a1d282020-05-07 19:10:15 +02001781 (ctx.value.len >= 4 && strncasecmp(ctx.value.ptr, "NTLM", 4) == 0)) {
Olivier Houchard250031e2019-05-29 15:01:50 +02001782 sess->flags |= SESS_FL_PREFER_LAST;
Christopher Faulet08016ab2020-07-01 16:10:06 +02001783 conn_set_owner(srv_conn, sess, NULL);
Christopher Faulet21ddc742020-07-01 15:26:14 +02001784 conn_set_private(srv_conn);
Ilya Shipitsin6b79f382020-07-23 00:32:55 +05001785 /* If it fail now, the same will be done in mux->detach() callback */
Christopher Faulet08016ab2020-07-01 16:10:06 +02001786 session_add_conn(srv_conn->owner, srv_conn, srv_conn->target);
Willy Tarreauf1dcced2020-05-07 19:27:02 +02001787 break;
Olivier Houchard250031e2019-05-29 15:01:50 +02001788 }
Christopher Faulet61608322018-11-23 16:23:45 +01001789 }
1790 }
1791
1792 end:
Christopher Faulete0768eb2018-10-03 16:38:02 +02001793 /* we want to have the response time before we start processing it */
1794 s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now);
1795
1796 /* end of job, return OK */
1797 rep->analysers &= ~an_bit;
1798 rep->analyse_exp = TICK_ETERNITY;
1799 channel_auto_close(rep);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001800 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001801 return 1;
1802
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001803 return_int_err:
Christopher Fauletcff0f732019-12-16 16:13:44 +01001804 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01001805 _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1);
William Lallemand36119de2021-03-08 15:26:48 +01001806 if (sess->listener && sess->listener->counters)
Christopher Fauletcff0f732019-12-16 16:13:44 +01001807 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01001808 if (objt_server(s->target))
1809 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.internal_errors, 1);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001810 txn->status = 500;
1811 if (!(s->flags & SF_ERR_MASK))
1812 s->flags |= SF_ERR_INTERNAL;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001813 goto return_prx_cond;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001814
1815 return_bad_res:
Olivier Houcharda798bf52019-03-08 18:52:00 +01001816 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulet47365272018-10-31 17:40:50 +01001817 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001818 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001819 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Christopher Faulet47365272018-10-31 17:40:50 +01001820 }
Olivier Houcharde3249a92019-05-03 23:01:47 +02001821 if ((s->be->retry_type & PR_RE_JUNK_REQUEST) &&
Olivier Houchardad26d8d2019-05-10 17:48:28 +02001822 (si_b->flags & SI_FL_L7_RETRY) &&
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001823 do_l7_retry(s, si_b) == 0) {
1824 DBG_TRACE_DEVEL("leaving on L7 retry",
1825 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharde3249a92019-05-03 23:01:47 +02001826 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001827 }
Christopher Faulet47365272018-10-31 17:40:50 +01001828 txn->status = 502;
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001829 stream_inc_http_fail_ctr(s);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001830 /* fall through */
1831
Christopher Fauletb8a53712019-12-16 11:29:38 +01001832 return_prx_cond:
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001833 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet47365272018-10-31 17:40:50 +01001834
1835 if (!(s->flags & SF_ERR_MASK))
1836 s->flags |= SF_ERR_PRXCOND;
1837 if (!(s->flags & SF_FINST_MASK))
1838 s->flags |= SF_FINST_H;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001839
1840 s->si[1].flags |= SI_FL_NOLINGER;
1841 rep->analysers &= AN_RES_FLT_END;
Christopher Faulete58c0002020-03-02 16:21:01 +01001842 s->req.analysers &= AN_REQ_FLT_END;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001843 rep->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001844 DBG_TRACE_DEVEL("leaving on error",
1845 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulet47365272018-10-31 17:40:50 +01001846 return 0;
1847
Christopher Faulete0768eb2018-10-03 16:38:02 +02001848 abort_keep_alive:
1849 /* A keep-alive request to the server failed on a network error.
1850 * The client is required to retry. We need to close without returning
1851 * any other information so that the client retries.
1852 */
1853 txn->status = 0;
1854 rep->analysers &= AN_RES_FLT_END;
1855 s->req.analysers &= AN_REQ_FLT_END;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001856 s->logs.logwait = 0;
1857 s->logs.level = 0;
1858 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001859 http_reply_and_close(s, txn->status, NULL);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001860 DBG_TRACE_DEVEL("leaving by closing K/A connection",
1861 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001862 return 0;
1863}
1864
1865/* This function performs all the processing enabled for the current response.
1866 * It normally returns 1 unless it wants to break. It relies on buffers flags,
1867 * and updates s->res.analysers. It might make sense to explode it into several
1868 * other functions. It works like process_request (see indications above).
1869 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001870int http_process_res_common(struct stream *s, struct channel *rep, int an_bit, struct proxy *px)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001871{
1872 struct session *sess = s->sess;
1873 struct http_txn *txn = s->txn;
1874 struct http_msg *msg = &txn->rsp;
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001875 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001876 struct proxy *cur_proxy;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001877 enum rule_result ret = HTTP_RULE_RES_CONT;
1878
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001879 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
1880 return 0;
Christopher Faulet9768c262018-10-22 09:34:31 +02001881
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001882 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001883
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001884 htx = htxbuf(&rep->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001885
1886 /* The stats applet needs to adjust the Connection header but we don't
1887 * apply any filter there.
1888 */
1889 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
1890 rep->analysers &= ~an_bit;
1891 rep->analyse_exp = TICK_ETERNITY;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001892 goto end;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001893 }
1894
1895 /*
1896 * We will have to evaluate the filters.
1897 * As opposed to version 1.2, now they will be evaluated in the
1898 * filters order and not in the header order. This means that
1899 * each filter has to be validated among all headers.
1900 *
1901 * Filters are tried with ->be first, then with ->fe if it is
1902 * different from ->be.
1903 *
1904 * Maybe we are in resume condiion. In this case I choose the
1905 * "struct proxy" which contains the rule list matching the resume
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05001906 * pointer. If none of these "struct proxy" match, I initialise
Christopher Faulete0768eb2018-10-03 16:38:02 +02001907 * the process with the first one.
1908 *
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05001909 * In fact, I check only correspondence between the current list
Christopher Faulete0768eb2018-10-03 16:38:02 +02001910 * pointer and the ->fe rule list. If it doesn't match, I initialize
1911 * the loop with the ->be.
1912 */
1913 if (s->current_rule_list == &sess->fe->http_res_rules)
1914 cur_proxy = sess->fe;
1915 else
1916 cur_proxy = s->be;
1917 while (1) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001918 /* evaluate http-response rules */
1919 if (ret == HTTP_RULE_RES_CONT) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001920 ret = http_res_get_intercept_rule(cur_proxy, &cur_proxy->http_res_rules, s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001921
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001922 switch (ret) {
1923 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
1924 goto return_prx_yield;
1925
1926 case HTTP_RULE_RES_CONT:
1927 case HTTP_RULE_RES_STOP: /* nothing to do */
1928 break;
1929
1930 case HTTP_RULE_RES_DENY: /* deny or tarpit */
1931 goto deny;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001932
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001933 case HTTP_RULE_RES_ABRT: /* abort request, response already sent */
1934 goto return_prx_cond;
1935
1936 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
Christopher Fauletb8a53712019-12-16 11:29:38 +01001937 goto done;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001938
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001939 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
1940 goto return_bad_res;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001941
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001942 case HTTP_RULE_RES_ERROR: /* failed with a bad request */
1943 goto return_int_err;
1944 }
1945
1946 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001947
Christopher Faulete0768eb2018-10-03 16:38:02 +02001948 /* check whether we're already working on the frontend */
1949 if (cur_proxy == sess->fe)
1950 break;
1951 cur_proxy = sess->fe;
1952 }
1953
Christopher Faulete0768eb2018-10-03 16:38:02 +02001954 /* OK that's all we can do for 1xx responses */
1955 if (unlikely(txn->status < 200 && txn->status != 101))
Christopher Fauletf2824e62018-10-01 12:12:37 +02001956 goto end;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001957
1958 /*
1959 * Now check for a server cookie.
1960 */
1961 if (s->be->cookie_name || sess->fe->capture_name || (s->be->options & PR_O_CHK_CACHE))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001962 http_manage_server_side_cookies(s, rep);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001963
1964 /*
1965 * Check for cache-control or pragma headers if required.
1966 */
1967 if ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001968 http_check_response_for_cacheability(s, rep);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001969
1970 /*
1971 * Add server cookie in the response if needed
1972 */
1973 if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) &&
1974 !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) &&
1975 (!(s->flags & SF_DIRECT) ||
1976 ((s->be->cookie_maxidle || txn->cookie_last_date) &&
1977 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
1978 (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
1979 (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
1980 (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
1981 !(s->flags & SF_IGNORE_PRST)) {
1982 /* the server is known, it's not the one the client requested, or the
1983 * cookie's last seen date needs to be refreshed. We have to
1984 * insert a set-cookie here, except if we want to insert only on POST
1985 * requests and this one isn't. Note that servers which don't have cookies
1986 * (eg: some backup servers) will return a full cookie removal request.
1987 */
1988 if (!objt_server(s->target)->cookie) {
1989 chunk_printf(&trash,
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001990 "%s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
Christopher Faulete0768eb2018-10-03 16:38:02 +02001991 s->be->cookie_name);
1992 }
1993 else {
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001994 chunk_printf(&trash, "%s=%s", s->be->cookie_name, objt_server(s->target)->cookie);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001995
1996 if (s->be->cookie_maxidle || s->be->cookie_maxlife) {
1997 /* emit last_date, which is mandatory */
1998 trash.area[trash.data++] = COOKIE_DELIM_DATE;
1999 s30tob64((date.tv_sec+3) >> 2,
2000 trash.area + trash.data);
2001 trash.data += 5;
2002
2003 if (s->be->cookie_maxlife) {
2004 /* emit first_date, which is either the original one or
2005 * the current date.
2006 */
2007 trash.area[trash.data++] = COOKIE_DELIM_DATE;
2008 s30tob64(txn->cookie_first_date ?
2009 txn->cookie_first_date >> 2 :
2010 (date.tv_sec+3) >> 2,
2011 trash.area + trash.data);
2012 trash.data += 5;
2013 }
2014 }
2015 chunk_appendf(&trash, "; path=/");
2016 }
2017
2018 if (s->be->cookie_domain)
2019 chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain);
2020
2021 if (s->be->ck_opts & PR_CK_HTTPONLY)
2022 chunk_appendf(&trash, "; HttpOnly");
2023
2024 if (s->be->ck_opts & PR_CK_SECURE)
2025 chunk_appendf(&trash, "; Secure");
2026
Christopher Faulet2f533902020-01-21 11:06:48 +01002027 if (s->be->cookie_attrs)
2028 chunk_appendf(&trash, "; %s", s->be->cookie_attrs);
2029
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002030 if (unlikely(!http_add_header(htx, ist("Set-Cookie"), ist2(trash.area, trash.data))))
Christopher Fauletb8a53712019-12-16 11:29:38 +01002031 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002032
2033 txn->flags &= ~TX_SCK_MASK;
2034 if (__objt_server(s->target)->cookie && (s->flags & SF_DIRECT))
2035 /* the server did not change, only the date was updated */
2036 txn->flags |= TX_SCK_UPDATED;
2037 else
2038 txn->flags |= TX_SCK_INSERTED;
2039
2040 /* Here, we will tell an eventual cache on the client side that we don't
2041 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
2042 * Some caches understand the correct form: 'no-cache="set-cookie"', but
2043 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
2044 */
2045 if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
2046
2047 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
2048
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002049 if (unlikely(!http_add_header(htx, ist("Cache-control"), ist("private"))))
Christopher Fauletb8a53712019-12-16 11:29:38 +01002050 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002051 }
2052 }
2053
2054 /*
2055 * Check if result will be cacheable with a cookie.
2056 * We'll block the response if security checks have caught
2057 * nasty things such as a cacheable cookie.
2058 */
2059 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
2060 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
2061 (s->be->options & PR_O_CHK_CACHE)) {
2062 /* we're in presence of a cacheable response containing
2063 * a set-cookie header. We'll block it as requested by
2064 * the 'checkcache' option, and send an alert.
2065 */
Christopher Faulete0768eb2018-10-03 16:38:02 +02002066 ha_alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
2067 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
2068 send_log(s->be, LOG_ALERT,
2069 "Blocking cacheable cookie in response from instance %s, server %s.\n",
2070 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
Christopher Fauletb8a53712019-12-16 11:29:38 +01002071 goto deny;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002072 }
2073
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002074 end:
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002075 /*
2076 * Evaluate after-response rules before forwarding the response. rules
2077 * from the backend are evaluated first, then one from the frontend if
2078 * it differs.
2079 */
2080 if (!http_eval_after_res_rules(s))
2081 goto return_int_err;
2082
Christopher Fauletc2ac5e42021-03-08 18:20:09 +01002083 /* Filter the response headers if there are filters attached to the
2084 * stream.
2085 */
2086 if (HAS_FILTERS(s))
2087 rep->analysers |= AN_RES_FLT_HTTP_HDRS;
2088
Christopher Faulete0768eb2018-10-03 16:38:02 +02002089 /* Always enter in the body analyzer */
2090 rep->analysers &= ~AN_RES_FLT_XFER_DATA;
2091 rep->analysers |= AN_RES_HTTP_XFER_BODY;
2092
2093 /* if the user wants to log as soon as possible, without counting
2094 * bytes from the server, then this is the right moment. We have
2095 * to temporarily assign bytes_out to log what we currently have.
2096 */
2097 if (!LIST_ISEMPTY(&sess->fe->logformat) && !(s->logs.logwait & LW_BYTES)) {
2098 s->logs.t_close = s->logs.t_data; /* to get a valid end date */
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002099 s->logs.bytes_out = htx->data;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002100 s->do_log(s);
2101 s->logs.bytes_out = 0;
2102 }
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002103
Christopher Fauletb8a53712019-12-16 11:29:38 +01002104 done:
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002105 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002106 rep->analysers &= ~an_bit;
2107 rep->analyse_exp = TICK_ETERNITY;
2108 return 1;
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002109
Christopher Fauletb8a53712019-12-16 11:29:38 +01002110 deny:
Christopher Fauletb8a53712019-12-16 11:29:38 +01002111 _HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_resp, 1);
Christopher Fauleta08546b2019-12-16 16:07:34 +01002112 _HA_ATOMIC_ADD(&s->be->be_counters.denied_resp, 1);
William Lallemand36119de2021-03-08 15:26:48 +01002113 if (sess->listener && sess->listener->counters)
Christopher Fauletb8a53712019-12-16 11:29:38 +01002114 _HA_ATOMIC_ADD(&sess->listener->counters->denied_resp, 1);
Christopher Fauleta08546b2019-12-16 16:07:34 +01002115 if (objt_server(s->target))
2116 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.denied_resp, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002117 goto return_prx_err;
2118
2119 return_int_err:
2120 txn->status = 500;
2121 if (!(s->flags & SF_ERR_MASK))
2122 s->flags |= SF_ERR_INTERNAL;
Christopher Fauletcff0f732019-12-16 16:13:44 +01002123 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002124 _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1);
2125 if (objt_server(s->target))
2126 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01002127 if (objt_server(s->target))
2128 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002129 goto return_prx_err;
2130
2131 return_bad_res:
2132 txn->status = 502;
Willy Tarreau826f3ab2021-02-10 12:07:15 +01002133 stream_inc_http_fail_ctr(s);
Christopher Fauleta20a6532020-02-05 10:16:41 +01002134 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
2135 if (objt_server(s->target)) {
2136 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
2137 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_RSP);
2138 }
Christopher Fauletb8a53712019-12-16 11:29:38 +01002139 /* fall through */
2140
2141 return_prx_err:
2142 http_reply_and_close(s, txn->status, http_error_message(s));
2143 /* fall through */
2144
2145 return_prx_cond:
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002146 s->logs.t_data = -1; /* was not a valid response */
2147 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002148
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002149 if (!(s->flags & SF_ERR_MASK))
2150 s->flags |= SF_ERR_PRXCOND;
2151 if (!(s->flags & SF_FINST_MASK))
2152 s->flags |= SF_FINST_H;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002153
Christopher Faulete58c0002020-03-02 16:21:01 +01002154 rep->analysers &= AN_RES_FLT_END;
2155 s->req.analysers &= AN_REQ_FLT_END;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002156 rep->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002157 DBG_TRACE_DEVEL("leaving on error",
2158 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002159 return 0;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002160
2161 return_prx_yield:
2162 channel_dont_close(rep);
2163 DBG_TRACE_DEVEL("waiting for more data",
2164 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
2165 return 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002166}
2167
2168/* This function is an analyser which forwards response body (including chunk
2169 * sizes if any). It is called as soon as we must forward, even if we forward
2170 * zero byte. The only situation where it must not be called is when we're in
2171 * tunnel mode and we want to forward till the close. It's used both to forward
2172 * remaining data and to resync after end of body. It expects the msg_state to
2173 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
2174 * read more data, or 1 once we can go on with next request or end the stream.
2175 *
2176 * It is capable of compressing response data both in content-length mode and
2177 * in chunked mode. The state machines follows different flows depending on
2178 * whether content-length and chunked modes are used, since there are no
2179 * trailers in content-length :
2180 *
2181 * chk-mode cl-mode
2182 * ,----- BODY -----.
2183 * / \
2184 * V size > 0 V chk-mode
2185 * .--> SIZE -------------> DATA -------------> CRLF
2186 * | | size == 0 | last byte |
2187 * | v final crlf v inspected |
2188 * | TRAILERS -----------> DONE |
2189 * | |
2190 * `----------------------------------------------'
2191 *
2192 * Compression only happens in the DATA state, and must be flushed in final
2193 * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding
2194 * is performed at once on final states for all bytes parsed, or when leaving
2195 * on missing data.
2196 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002197int http_response_forward_body(struct stream *s, struct channel *res, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +02002198{
2199 struct session *sess = s->sess;
2200 struct http_txn *txn = s->txn;
2201 struct http_msg *msg = &s->txn->rsp;
Christopher Faulet9768c262018-10-22 09:34:31 +02002202 struct htx *htx;
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002203 int ret;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002204
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002205 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002206
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002207 htx = htxbuf(&res->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002208
Christopher Fauletb9a92f32019-09-09 10:15:21 +02002209 if (htx->flags & HTX_FL_PARSING_ERROR)
2210 goto return_bad_res;
2211 if (htx->flags & HTX_FL_PROCESSING_ERROR)
2212 goto return_int_err;
2213
Christopher Faulete0768eb2018-10-03 16:38:02 +02002214 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Christopher Fauletf2824e62018-10-01 12:12:37 +02002215 ((res->flags & CF_SHUTW) && (res->to_forward || co_data(res)))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02002216 /* Output closed while we were sending data. We must abort and
2217 * wake the other side up.
2218 */
Christopher Faulete0768eb2018-10-03 16:38:02 +02002219 msg->msg_state = HTTP_MSG_ERROR;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002220 http_end_response(s);
2221 http_end_request(s);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002222 DBG_TRACE_DEVEL("leaving on error",
2223 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002224 return 1;
2225 }
2226
Christopher Faulet9768c262018-10-22 09:34:31 +02002227 if (msg->msg_state == HTTP_MSG_BODY)
2228 msg->msg_state = HTTP_MSG_DATA;
2229
Christopher Faulete0768eb2018-10-03 16:38:02 +02002230 /* in most states, we should abort in case of early close */
2231 channel_auto_close(res);
2232
Christopher Faulete0768eb2018-10-03 16:38:02 +02002233 if (res->to_forward) {
Christopher Faulet66af0b22019-03-22 14:54:52 +01002234 if (res->to_forward == CHN_INFINITE_FORWARD) {
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002235 if (res->flags & CF_EOI)
2236 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet66af0b22019-03-22 14:54:52 +01002237 }
2238 else {
2239 /* We can't process the buffer's contents yet */
2240 res->flags |= CF_WAKE_WRITE;
2241 goto missing_data_or_waiting;
2242 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02002243 }
2244
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002245 if (msg->msg_state >= HTTP_MSG_ENDING)
2246 goto ending;
2247
Christopher Fauletc75668e2020-12-07 18:10:32 +01002248 if ((txn->meth == HTTP_METH_CONNECT && txn->status >= 200 && txn->status < 300) || txn->status == 101 ||
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002249 (!(msg->flags & HTTP_MSGF_XFER_LEN) && !HAS_RSP_DATA_FILTERS(s))) {
2250 msg->msg_state = HTTP_MSG_ENDING;
2251 goto ending;
2252 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002253
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002254 /* Forward input data. We get it by removing all outgoing data not
2255 * forwarded yet from HTX data size. If there are some data filters, we
2256 * let them decide the amount of data to forward.
Christopher Faulet9768c262018-10-22 09:34:31 +02002257 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002258 if (HAS_RSP_DATA_FILTERS(s)) {
2259 ret = flt_http_payload(s, msg, htx->data);
2260 if (ret < 0)
2261 goto return_bad_res;
Christopher Faulet421e7692019-06-13 11:16:45 +02002262 c_adv(res, ret);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002263 }
2264 else {
Christopher Faulet421e7692019-06-13 11:16:45 +02002265 c_adv(res, htx->data - co_data(res));
Christopher Faulet66af0b22019-03-22 14:54:52 +01002266 if (msg->flags & HTTP_MSGF_XFER_LEN)
2267 channel_htx_forward_forever(res, htx);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002268 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002269
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002270 if (htx->data != co_data(res))
2271 goto missing_data_or_waiting;
2272
2273 if (!(msg->flags & HTTP_MSGF_XFER_LEN) && res->flags & CF_SHUTR) {
2274 msg->msg_state = HTTP_MSG_ENDING;
2275 goto ending;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002276 }
2277
Christopher Faulet9768c262018-10-22 09:34:31 +02002278 /* Check if the end-of-message is reached and if so, switch the message
Christopher Fauletd20fdb02019-06-13 16:43:22 +02002279 * in HTTP_MSG_ENDING state. Then if all data was marked to be
2280 * forwarded, set the state to HTTP_MSG_DONE.
Christopher Faulet9768c262018-10-22 09:34:31 +02002281 */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002282 if (!(htx->flags & HTX_FL_EOM))
Christopher Faulet9768c262018-10-22 09:34:31 +02002283 goto missing_data_or_waiting;
2284
Christopher Fauletd20fdb02019-06-13 16:43:22 +02002285 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet9768c262018-10-22 09:34:31 +02002286
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002287 ending:
Christopher Faulet2151cdd2020-07-22 16:34:59 +02002288 res->flags &= ~CF_EXPECT_MORE; /* no more data are expected */
2289
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002290 /* other states, ENDING...TUNNEL */
2291 if (msg->msg_state >= HTTP_MSG_DONE)
2292 goto done;
Christopher Faulet9768c262018-10-22 09:34:31 +02002293
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002294 if (HAS_RSP_DATA_FILTERS(s)) {
2295 ret = flt_http_end(s, msg);
2296 if (ret <= 0) {
2297 if (!ret)
2298 goto missing_data_or_waiting;
2299 goto return_bad_res;
2300 }
2301 }
2302
Christopher Fauletc75668e2020-12-07 18:10:32 +01002303 if ((txn->meth == HTTP_METH_CONNECT && txn->status >= 200 && txn->status < 300) || txn->status == 101 ||
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002304 !(msg->flags & HTTP_MSGF_XFER_LEN)) {
2305 msg->msg_state = HTTP_MSG_TUNNEL;
2306 goto ending;
2307 }
2308 else {
2309 msg->msg_state = HTTP_MSG_DONE;
2310 res->to_forward = 0;
2311 }
2312
2313 done:
2314
2315 channel_dont_close(res);
2316
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002317 http_end_response(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002318 if (!(res->analysers & an_bit)) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002319 http_end_request(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002320 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
2321 if (res->flags & CF_SHUTW) {
2322 /* response errors are most likely due to the
2323 * client aborting the transfer. */
Christopher Faulet93e02d82019-03-08 14:18:50 +01002324 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002325 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02002326 goto return_bad_res;
2327 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002328 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002329 return 1;
2330 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002331 DBG_TRACE_DEVEL("waiting for the end of the HTTP txn",
2332 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002333 return 0;
2334
2335 missing_data_or_waiting:
2336 if (res->flags & CF_SHUTW)
Christopher Faulet93e02d82019-03-08 14:18:50 +01002337 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002338
2339 /* stop waiting for data if the input is closed before the end. If the
2340 * client side was already closed, it means that the client has aborted,
2341 * so we don't want to count this as a server abort. Otherwise it's a
2342 * server abort.
2343 */
Christopher Fauletd20fdb02019-06-13 16:43:22 +02002344 if (msg->msg_state < HTTP_MSG_ENDING && res->flags & CF_SHUTR) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02002345 if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))
Christopher Faulet93e02d82019-03-08 14:18:50 +01002346 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002347 /* If we have some pending data, we continue the processing */
Christopher Faulet93e02d82019-03-08 14:18:50 +01002348 if (htx_is_empty(htx))
2349 goto return_srv_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002350 }
2351
Christopher Faulete0768eb2018-10-03 16:38:02 +02002352 /* When TE: chunked is used, we need to get there again to parse
2353 * remaining chunks even if the server has closed, so we don't want to
Christopher Faulet9768c262018-10-22 09:34:31 +02002354 * set CF_DONTCLOSE. Similarly when there is a content-leng or if there
2355 * are filters registered on the stream, we don't want to forward a
2356 * close
Christopher Faulete0768eb2018-10-03 16:38:02 +02002357 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002358 if ((msg->flags & HTTP_MSGF_XFER_LEN) || HAS_RSP_DATA_FILTERS(s))
Christopher Faulete0768eb2018-10-03 16:38:02 +02002359 channel_dont_close(res);
2360
2361 /* We know that more data are expected, but we couldn't send more that
2362 * what we did. So we always set the CF_EXPECT_MORE flag so that the
2363 * system knows it must not set a PUSH on this first part. Interactive
2364 * modes are already handled by the stream sock layer. We must not do
2365 * this in content-length mode because it could present the MSG_MORE
2366 * flag with the last block of forwarded data, which would cause an
2367 * additional delay to be observed by the receiver.
2368 */
Christopher Faulet2151cdd2020-07-22 16:34:59 +02002369 if (HAS_RSP_DATA_FILTERS(s))
Christopher Faulete0768eb2018-10-03 16:38:02 +02002370 res->flags |= CF_EXPECT_MORE;
2371
2372 /* the stream handler will take care of timeouts and errors */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002373 DBG_TRACE_DEVEL("waiting for more data to forward",
2374 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002375 return 0;
2376
Christopher Faulet93e02d82019-03-08 14:18:50 +01002377 return_srv_abort:
2378 _HA_ATOMIC_ADD(&sess->fe->fe_counters.srv_aborts, 1);
2379 _HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1);
William Lallemand36119de2021-03-08 15:26:48 +01002380 if (sess->listener && sess->listener->counters)
Christopher Fauletcff0f732019-12-16 16:13:44 +01002381 _HA_ATOMIC_ADD(&sess->listener->counters->srv_aborts, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002382 if (objt_server(s->target))
Christopher Fauletcff0f732019-12-16 16:13:44 +01002383 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.srv_aborts, 1);
Willy Tarreau826f3ab2021-02-10 12:07:15 +01002384 stream_inc_http_fail_ctr(s);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002385 if (!(s->flags & SF_ERR_MASK))
2386 s->flags |= SF_ERR_SRVCL;
2387 goto return_error;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002388
Christopher Faulet93e02d82019-03-08 14:18:50 +01002389 return_cli_abort:
2390 _HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
2391 _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
William Lallemand36119de2021-03-08 15:26:48 +01002392 if (sess->listener && sess->listener->counters)
Christopher Fauletcff0f732019-12-16 16:13:44 +01002393 _HA_ATOMIC_ADD(&sess->listener->counters->cli_aborts, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002394 if (objt_server(s->target))
Christopher Fauletcff0f732019-12-16 16:13:44 +01002395 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.cli_aborts, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002396 if (!(s->flags & SF_ERR_MASK))
2397 s->flags |= SF_ERR_CLICL;
2398 goto return_error;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002399
Christopher Fauletb9a92f32019-09-09 10:15:21 +02002400 return_int_err:
Christopher Fauletcff0f732019-12-16 16:13:44 +01002401 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002402 _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1);
William Lallemand36119de2021-03-08 15:26:48 +01002403 if (sess->listener && sess->listener->counters)
Christopher Fauletcff0f732019-12-16 16:13:44 +01002404 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002405 if (objt_server(s->target))
2406 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.internal_errors, 1);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02002407 if (!(s->flags & SF_ERR_MASK))
2408 s->flags |= SF_ERR_INTERNAL;
2409 goto return_error;
2410
Christopher Faulet93e02d82019-03-08 14:18:50 +01002411 return_bad_res:
2412 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
2413 if (objt_server(s->target)) {
Christopher Fauletcff0f732019-12-16 16:13:44 +01002414 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002415 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_RSP);
2416 }
Willy Tarreau826f3ab2021-02-10 12:07:15 +01002417 stream_inc_http_fail_ctr(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002418 if (!(s->flags & SF_ERR_MASK))
Christopher Faulet93e02d82019-03-08 14:18:50 +01002419 s->flags |= SF_ERR_SRVCL;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002420 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +02002421
Christopher Faulet93e02d82019-03-08 14:18:50 +01002422 return_error:
Christopher Faulete0768eb2018-10-03 16:38:02 +02002423 /* don't send any error message as we're in the body */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002424 http_reply_and_close(s, txn->status, NULL);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002425 res->analysers &= AN_RES_FLT_END;
2426 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 +02002427 if (!(s->flags & SF_FINST_MASK))
2428 s->flags |= SF_FINST_D;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002429 DBG_TRACE_DEVEL("leaving on error",
2430 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002431 return 0;
2432}
2433
Christopher Fauletf2824e62018-10-01 12:12:37 +02002434/* Perform an HTTP redirect based on the information in <rule>. The function
Christopher Faulet99daf282018-11-28 22:58:13 +01002435 * returns zero on success, or zero in case of a, irrecoverable error such
Christopher Fauletf2824e62018-10-01 12:12:37 +02002436 * as too large a request to build a valid response.
2437 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002438int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn)
Christopher Fauletf2824e62018-10-01 12:12:37 +02002439{
Christopher Faulet99daf282018-11-28 22:58:13 +01002440 struct channel *req = &s->req;
2441 struct channel *res = &s->res;
2442 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01002443 struct htx_sl *sl;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002444 struct buffer *chunk;
Christopher Faulet99daf282018-11-28 22:58:13 +01002445 struct ist status, reason, location;
2446 unsigned int flags;
Christopher Faulet08e66462019-05-23 16:44:59 +02002447 int close = 0; /* Try to keep the connection alive byt default */
Christopher Fauletf2824e62018-10-01 12:12:37 +02002448
2449 chunk = alloc_trash_chunk();
Christopher Fauletb8a53712019-12-16 11:29:38 +01002450 if (!chunk) {
2451 if (!(s->flags & SF_ERR_MASK))
2452 s->flags |= SF_ERR_RESOURCE;
Christopher Faulet99daf282018-11-28 22:58:13 +01002453 goto fail;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002454 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002455
Christopher Faulet99daf282018-11-28 22:58:13 +01002456 /*
2457 * Create the location
2458 */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002459 htx = htxbuf(&req->buf);
Christopher Fauletf2824e62018-10-01 12:12:37 +02002460 switch(rule->type) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002461 case REDIRECT_TYPE_SCHEME: {
2462 struct http_hdr_ctx ctx;
2463 struct ist path, host;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002464
Christopher Faulet99daf282018-11-28 22:58:13 +01002465 host = ist("");
2466 ctx.blk = NULL;
2467 if (http_find_header(htx, ist("Host"), &ctx, 0))
2468 host = ctx.value;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002469
Christopher Faulet297fbb42019-05-13 14:41:27 +02002470 sl = http_get_stline(htx);
Christopher Faulet99daf282018-11-28 22:58:13 +01002471 path = http_get_path(htx_sl_req_uri(sl));
2472 /* build message using path */
Tim Duesterhused526372020-03-05 17:56:33 +01002473 if (isttest(path)) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002474 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2475 int qs = 0;
2476 while (qs < path.len) {
2477 if (*(path.ptr + qs) == '?') {
2478 path.len = qs;
2479 break;
2480 }
2481 qs++;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002482 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002483 }
2484 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002485 else
2486 path = ist("/");
Christopher Fauletf2824e62018-10-01 12:12:37 +02002487
Christopher Faulet99daf282018-11-28 22:58:13 +01002488 if (rule->rdr_str) { /* this is an old "redirect" rule */
2489 /* add scheme */
2490 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2491 goto fail;
2492 }
2493 else {
2494 /* add scheme with executing log format */
2495 chunk->data += build_logline(s, chunk->area + chunk->data,
2496 chunk->size - chunk->data,
2497 &rule->rdr_fmt);
2498 }
2499 /* add "://" + host + path */
2500 if (!chunk_memcat(chunk, "://", 3) ||
2501 !chunk_memcat(chunk, host.ptr, host.len) ||
2502 !chunk_memcat(chunk, path.ptr, path.len))
2503 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002504
Christopher Faulet99daf282018-11-28 22:58:13 +01002505 /* append a slash at the end of the location if needed and missing */
2506 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
2507 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
2508 if (chunk->data + 1 >= chunk->size)
2509 goto fail;
2510 chunk->area[chunk->data++] = '/';
2511 }
2512 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002513 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002514
Christopher Faulet99daf282018-11-28 22:58:13 +01002515 case REDIRECT_TYPE_PREFIX: {
2516 struct ist path;
2517
Christopher Faulet297fbb42019-05-13 14:41:27 +02002518 sl = http_get_stline(htx);
Christopher Faulet99daf282018-11-28 22:58:13 +01002519 path = http_get_path(htx_sl_req_uri(sl));
2520 /* build message using path */
Tim Duesterhused526372020-03-05 17:56:33 +01002521 if (isttest(path)) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002522 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2523 int qs = 0;
2524 while (qs < path.len) {
2525 if (*(path.ptr + qs) == '?') {
2526 path.len = qs;
2527 break;
2528 }
2529 qs++;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002530 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002531 }
2532 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002533 else
2534 path = ist("/");
Christopher Fauletf2824e62018-10-01 12:12:37 +02002535
Christopher Faulet99daf282018-11-28 22:58:13 +01002536 if (rule->rdr_str) { /* this is an old "redirect" rule */
2537 /* add prefix. Note that if prefix == "/", we don't want to
2538 * add anything, otherwise it makes it hard for the user to
2539 * configure a self-redirection.
2540 */
2541 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
2542 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2543 goto fail;
2544 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002545 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002546 else {
2547 /* add prefix with executing log format */
2548 chunk->data += build_logline(s, chunk->area + chunk->data,
2549 chunk->size - chunk->data,
2550 &rule->rdr_fmt);
2551 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002552
Christopher Faulet99daf282018-11-28 22:58:13 +01002553 /* add path */
2554 if (!chunk_memcat(chunk, path.ptr, path.len))
2555 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002556
Christopher Faulet99daf282018-11-28 22:58:13 +01002557 /* append a slash at the end of the location if needed and missing */
2558 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
2559 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
2560 if (chunk->data + 1 >= chunk->size)
2561 goto fail;
2562 chunk->area[chunk->data++] = '/';
2563 }
2564 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002565 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002566 case REDIRECT_TYPE_LOCATION:
2567 default:
2568 if (rule->rdr_str) { /* this is an old "redirect" rule */
2569 /* add location */
2570 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2571 goto fail;
2572 }
2573 else {
2574 /* add location with executing log format */
2575 chunk->data += build_logline(s, chunk->area + chunk->data,
2576 chunk->size - chunk->data,
2577 &rule->rdr_fmt);
2578 }
2579 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002580 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002581 location = ist2(chunk->area, chunk->data);
2582
2583 /*
2584 * Create the 30x response
2585 */
2586 switch (rule->code) {
2587 case 308:
2588 status = ist("308");
2589 reason = ist("Permanent Redirect");
2590 break;
2591 case 307:
2592 status = ist("307");
2593 reason = ist("Temporary Redirect");
2594 break;
2595 case 303:
2596 status = ist("303");
2597 reason = ist("See Other");
2598 break;
2599 case 301:
2600 status = ist("301");
2601 reason = ist("Moved Permanently");
2602 break;
2603 case 302:
2604 default:
2605 status = ist("302");
2606 reason = ist("Found");
2607 break;
2608 }
2609
Christopher Faulet08e66462019-05-23 16:44:59 +02002610 if (!(txn->req.flags & HTTP_MSGF_BODYLESS) && txn->req.msg_state != HTTP_MSG_DONE)
2611 close = 1;
2612
Christopher Faulet99daf282018-11-28 22:58:13 +01002613 htx = htx_from_buf(&res->buf);
Kevin Zhu96b36392020-01-07 09:42:55 +01002614 /* Trim any possible response */
2615 channel_htx_truncate(&s->res, htx);
Christopher Faulet99daf282018-11-28 22:58:13 +01002616 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
2617 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), status, reason);
2618 if (!sl)
2619 goto fail;
2620 sl->info.res.status = rule->code;
2621 s->txn->status = rule->code;
2622
Christopher Faulet08e66462019-05-23 16:44:59 +02002623 if (close && !htx_add_header(htx, ist("Connection"), ist("close")))
2624 goto fail;
2625
2626 if (!htx_add_header(htx, ist("Content-length"), ist("0")) ||
Christopher Faulet99daf282018-11-28 22:58:13 +01002627 !htx_add_header(htx, ist("Location"), location))
2628 goto fail;
2629
2630 if (rule->code == 302 || rule->code == 303 || rule->code == 307) {
2631 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")))
2632 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002633 }
2634
2635 if (rule->cookie_len) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002636 if (!htx_add_header(htx, ist("Set-Cookie"), ist2(rule->cookie_str, rule->cookie_len)))
2637 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002638 }
2639
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002640 if (!htx_add_endof(htx, HTX_BLK_EOH))
Christopher Faulet99daf282018-11-28 22:58:13 +01002641 goto fail;
2642
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002643 htx->flags |= HTX_FL_EOM;
Kevin Zhu96b36392020-01-07 09:42:55 +01002644 htx_to_buf(htx, &res->buf);
Christopher Fauleta72a7e42020-01-28 09:28:11 +01002645 if (!http_forward_proxy_resp(s, 1))
2646 goto fail;
Christopher Faulet99daf282018-11-28 22:58:13 +01002647
Christopher Faulet60b33a52020-01-28 09:18:10 +01002648 if (rule->flags & REDIRECT_FLAG_FROM_REQ) {
2649 /* let's log the request time */
2650 s->logs.tv_request = now;
2651 req->analysers &= AN_REQ_FLT_END;
Christopher Faulet99daf282018-11-28 22:58:13 +01002652
Christopher Faulet60b33a52020-01-28 09:18:10 +01002653 if (s->sess->fe == s->be) /* report it if the request was intercepted by the frontend */
2654 _HA_ATOMIC_ADD(&s->sess->fe->fe_counters.intercepted_req, 1);
2655 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002656
2657 if (!(s->flags & SF_ERR_MASK))
2658 s->flags |= SF_ERR_LOCAL;
2659 if (!(s->flags & SF_FINST_MASK))
Christopher Faulet60b33a52020-01-28 09:18:10 +01002660 s->flags |= ((rule->flags & REDIRECT_FLAG_FROM_REQ) ? SF_FINST_R : SF_FINST_H);
Christopher Fauletf2824e62018-10-01 12:12:37 +02002661
Christopher Faulet99daf282018-11-28 22:58:13 +01002662 free_trash_chunk(chunk);
2663 return 1;
2664
2665 fail:
2666 /* If an error occurred, remove the incomplete HTTP response from the
2667 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01002668 channel_htx_truncate(res, htxbuf(&res->buf));
Christopher Fauletf2824e62018-10-01 12:12:37 +02002669 free_trash_chunk(chunk);
Christopher Faulet99daf282018-11-28 22:58:13 +01002670 return 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002671}
2672
Christopher Faulet92d34fe2019-12-17 09:20:34 +01002673/* Replace all headers matching the name <name>. The header value is replaced if
2674 * it matches the regex <re>. <str> is used for the replacement. If <full> is
2675 * set to 1, the full-line is matched and replaced. Otherwise, comma-separated
2676 * values are evaluated one by one. It returns 0 on success and -1 on error.
2677 */
2678int http_replace_hdrs(struct stream* s, struct htx *htx, struct ist name,
2679 const char *str, struct my_regex *re, int full)
Christopher Faulet72333522018-10-24 11:25:02 +02002680{
2681 struct http_hdr_ctx ctx;
2682 struct buffer *output = get_trash_chunk();
2683
Christopher Faulet72333522018-10-24 11:25:02 +02002684 ctx.blk = NULL;
Christopher Faulet92d34fe2019-12-17 09:20:34 +01002685 while (http_find_header(htx, name, &ctx, full)) {
Christopher Faulet72333522018-10-24 11:25:02 +02002686 if (!regex_exec_match2(re, ctx.value.ptr, ctx.value.len, MAX_MATCH, pmatch, 0))
2687 continue;
2688
2689 output->data = exp_replace(output->area, output->size, ctx.value.ptr, str, pmatch);
2690 if (output->data == -1)
2691 return -1;
2692 if (!http_replace_header_value(htx, &ctx, ist2(output->area, output->data)))
2693 return -1;
2694 }
2695 return 0;
2696}
2697
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002698/* This function executes one of the set-{method,path,query,uri} actions. It
2699 * takes the string from the variable 'replace' with length 'len', then modifies
2700 * the relevant part of the request line accordingly. Then it updates various
2701 * pointers to the next elements which were moved, and the total buffer length.
2702 * It finds the action to be performed in p[2], previously filled by function
2703 * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal
2704 * error, though this can be revisited when this code is finally exploited.
2705 *
2706 * 'action' can be '0' to replace method, '1' to replace path, '2' to replace
Christopher Faulet312294f2020-09-02 17:17:44 +02002707 * query string, 3 to replace uri or 4 to replace the path+query.
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002708 *
2709 * In query string case, the mark question '?' must be set at the start of the
2710 * string by the caller, event if the replacement query string is empty.
2711 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002712int http_req_replace_stline(int action, const char *replace, int len,
2713 struct proxy *px, struct stream *s)
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002714{
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002715 struct htx *htx = htxbuf(&s->req.buf);
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002716
2717 switch (action) {
2718 case 0: // method
2719 if (!http_replace_req_meth(htx, ist2(replace, len)))
2720 return -1;
2721 break;
2722
2723 case 1: // path
Christopher Fauletb8ce5052020-08-31 16:11:57 +02002724 if (!http_replace_req_path(htx, ist2(replace, len), 0))
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002725 return -1;
2726 break;
2727
2728 case 2: // query
2729 if (!http_replace_req_query(htx, ist2(replace, len)))
2730 return -1;
2731 break;
2732
2733 case 3: // uri
2734 if (!http_replace_req_uri(htx, ist2(replace, len)))
2735 return -1;
2736 break;
2737
Christopher Faulet312294f2020-09-02 17:17:44 +02002738 case 4: // path + query
2739 if (!http_replace_req_path(htx, ist2(replace, len), 1))
2740 return -1;
2741 break;
2742
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002743 default:
2744 return -1;
2745 }
2746 return 0;
2747}
2748
2749/* This function replace the HTTP status code and the associated message. The
Christopher Faulete00d06c2019-12-16 17:18:42 +01002750 * variable <status> contains the new status code. This function never fails. It
2751 * returns 0 in case of success, -1 in case of internal error.
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002752 */
Christopher Faulet96bff762019-12-17 13:46:18 +01002753int http_res_set_status(unsigned int status, struct ist reason, struct stream *s)
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002754{
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002755 struct htx *htx = htxbuf(&s->res.buf);
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002756 char *res;
2757
2758 chunk_reset(&trash);
2759 res = ultoa_o(status, trash.area, trash.size);
2760 trash.data = res - trash.area;
2761
2762 /* Do we have a custom reason format string? */
Tim Duesterhuse296d3e2020-03-05 17:56:31 +01002763 if (!isttest(reason)) {
Christopher Faulet96bff762019-12-17 13:46:18 +01002764 const char *str = http_get_reason(status);
Tim Duesterhusdcf753a2021-03-04 17:31:47 +01002765 reason = ist(str);
Christopher Faulet96bff762019-12-17 13:46:18 +01002766 }
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002767
Christopher Fauletbde2c4c2020-08-31 16:43:34 +02002768 if (!http_replace_res_status(htx, ist2(trash.area, trash.data), reason))
Christopher Faulete00d06c2019-12-16 17:18:42 +01002769 return -1;
2770 return 0;
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002771}
2772
Christopher Faulet3e964192018-10-24 11:39:23 +02002773/* Executes the http-request rules <rules> for stream <s>, proxy <px> and
2774 * transaction <txn>. Returns the verdict of the first rule that prevents
2775 * further processing of the request (auth, deny, ...), and defaults to
2776 * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or
2777 * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT
2778 * on txn->flags if it encounters a tarpit rule. If <deny_status> is not NULL
2779 * and a deny/tarpit rule is matched, it will be filled with this rule's deny
2780 * status.
2781 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002782static enum rule_result http_req_get_intercept_rule(struct proxy *px, struct list *rules,
Christopher Fauletb58f62b2020-01-13 16:40:13 +01002783 struct stream *s)
Christopher Faulet3e964192018-10-24 11:39:23 +02002784{
2785 struct session *sess = strm_sess(s);
2786 struct http_txn *txn = s->txn;
Christopher Faulet3e964192018-10-24 11:39:23 +02002787 struct act_rule *rule;
Christopher Faulet3e964192018-10-24 11:39:23 +02002788 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002789 int act_opts = 0;
Christopher Faulet3e964192018-10-24 11:39:23 +02002790
Christopher Faulet3e964192018-10-24 11:39:23 +02002791 /* If "the current_rule_list" match the executed rule list, we are in
2792 * resume condition. If a resume is needed it is always in the action
2793 * and never in the ACL or converters. In this case, we initialise the
2794 * current rule, and go to the action execution point.
2795 */
2796 if (s->current_rule) {
2797 rule = s->current_rule;
2798 s->current_rule = NULL;
2799 if (s->current_rule_list == rules)
2800 goto resume_execution;
2801 }
2802 s->current_rule_list = rules;
2803
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002804 /* start the ruleset evaluation in strict mode */
2805 txn->req.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01002806
Christopher Faulet3e964192018-10-24 11:39:23 +02002807 list_for_each_entry(rule, rules, list) {
2808 /* check optional condition */
2809 if (rule->cond) {
2810 int ret;
2811
2812 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
2813 ret = acl_pass(ret);
2814
2815 if (rule->cond->pol == ACL_COND_UNLESS)
2816 ret = !ret;
2817
2818 if (!ret) /* condition not matched */
2819 continue;
2820 }
2821
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002822 act_opts |= ACT_OPT_FIRST;
Christopher Faulet3e964192018-10-24 11:39:23 +02002823 resume_execution:
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002824 /* Always call the action function if defined */
2825 if (rule->action_ptr) {
2826 if ((s->req.flags & CF_READ_ERROR) ||
2827 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
2828 (px->options & PR_O_ABRT_CLOSE)))
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002829 act_opts |= ACT_OPT_FINAL;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002830
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002831 switch (rule->action_ptr(rule, px, sess, s, act_opts)) {
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002832 case ACT_RET_CONT:
2833 break;
2834 case ACT_RET_STOP:
2835 rule_ret = HTTP_RULE_RES_STOP;
2836 goto end;
2837 case ACT_RET_YIELD:
2838 s->current_rule = rule;
2839 rule_ret = HTTP_RULE_RES_YIELD;
2840 goto end;
2841 case ACT_RET_ERR:
2842 rule_ret = HTTP_RULE_RES_ERROR;
2843 goto end;
2844 case ACT_RET_DONE:
2845 rule_ret = HTTP_RULE_RES_DONE;
2846 goto end;
2847 case ACT_RET_DENY:
Christopher Fauletb58f62b2020-01-13 16:40:13 +01002848 if (txn->status == -1)
2849 txn->status = 403;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002850 rule_ret = HTTP_RULE_RES_DENY;
2851 goto end;
2852 case ACT_RET_ABRT:
2853 rule_ret = HTTP_RULE_RES_ABRT;
2854 goto end;
2855 case ACT_RET_INV:
2856 rule_ret = HTTP_RULE_RES_BADREQ;
2857 goto end;
2858 }
2859 continue; /* eval the next rule */
2860 }
2861
2862 /* If not action function defined, check for known actions */
Christopher Faulet3e964192018-10-24 11:39:23 +02002863 switch (rule->action) {
2864 case ACT_ACTION_ALLOW:
2865 rule_ret = HTTP_RULE_RES_STOP;
2866 goto end;
2867
2868 case ACT_ACTION_DENY:
Christopher Faulet5cb513a2020-05-13 17:56:56 +02002869 txn->status = rule->arg.http_reply->status;
2870 txn->http_reply = rule->arg.http_reply;
Christopher Faulet3e964192018-10-24 11:39:23 +02002871 rule_ret = HTTP_RULE_RES_DENY;
2872 goto end;
2873
2874 case ACT_HTTP_REQ_TARPIT:
2875 txn->flags |= TX_CLTARPIT;
Christopher Faulet5cb513a2020-05-13 17:56:56 +02002876 txn->status = rule->arg.http_reply->status;
2877 txn->http_reply = rule->arg.http_reply;
Christopher Faulet3e964192018-10-24 11:39:23 +02002878 rule_ret = HTTP_RULE_RES_DENY;
2879 goto end;
2880
Christopher Faulet3e964192018-10-24 11:39:23 +02002881 case ACT_HTTP_REDIR:
Christopher Faulet90d22a82020-03-06 11:18:39 +01002882 rule_ret = HTTP_RULE_RES_ABRT;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002883 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
Christopher Faulet3a26bee2019-12-16 12:47:40 +01002884 rule_ret = HTTP_RULE_RES_ERROR;
Christopher Faulet3e964192018-10-24 11:39:23 +02002885 goto end;
2886
2887 case ACT_HTTP_SET_NICE:
Christopher Faulet96bff762019-12-17 13:46:18 +01002888 s->task->nice = rule->arg.http.i;
Christopher Faulet3e964192018-10-24 11:39:23 +02002889 break;
2890
2891 case ACT_HTTP_SET_TOS:
Christopher Faulet96bff762019-12-17 13:46:18 +01002892 conn_set_tos(objt_conn(sess->origin), rule->arg.http.i);
Christopher Faulet3e964192018-10-24 11:39:23 +02002893 break;
2894
2895 case ACT_HTTP_SET_MARK:
Christopher Faulet96bff762019-12-17 13:46:18 +01002896 conn_set_mark(objt_conn(sess->origin), rule->arg.http.i);
Christopher Faulet3e964192018-10-24 11:39:23 +02002897 break;
2898
2899 case ACT_HTTP_SET_LOGL:
Christopher Faulet96bff762019-12-17 13:46:18 +01002900 s->logs.level = rule->arg.http.i;
Christopher Faulet3e964192018-10-24 11:39:23 +02002901 break;
2902
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002903 /* other flags exists, but normally, they never be matched. */
Christopher Faulet3e964192018-10-24 11:39:23 +02002904 default:
2905 break;
2906 }
2907 }
2908
2909 end:
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002910 /* if the ruleset evaluation is finished reset the strict mode */
Christopher Faulet46f95542019-12-20 10:07:22 +01002911 if (rule_ret != HTTP_RULE_RES_YIELD)
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002912 txn->req.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01002913
Christopher Faulet3e964192018-10-24 11:39:23 +02002914 /* we reached the end of the rules, nothing to report */
2915 return rule_ret;
2916}
2917
2918/* Executes the http-response rules <rules> for stream <s> and proxy <px>. It
2919 * returns one of 5 possible statuses: HTTP_RULE_RES_CONT, HTTP_RULE_RES_STOP,
2920 * HTTP_RULE_RES_DONE, HTTP_RULE_RES_YIELD, or HTTP_RULE_RES_BADREQ. If *CONT
2921 * is returned, the process can continue the evaluation of next rule list. If
2922 * *STOP or *DONE is returned, the process must stop the evaluation. If *BADREQ
2923 * is returned, it means the operation could not be processed and a server error
Christopher Fauleta53abad2020-05-13 08:12:22 +02002924 * must be returned. If *YIELD is returned, the caller must call again the
2925 * function with the same context.
Christopher Faulet3e964192018-10-24 11:39:23 +02002926 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002927static enum rule_result http_res_get_intercept_rule(struct proxy *px, struct list *rules,
2928 struct stream *s)
Christopher Faulet3e964192018-10-24 11:39:23 +02002929{
2930 struct session *sess = strm_sess(s);
2931 struct http_txn *txn = s->txn;
Christopher Faulet3e964192018-10-24 11:39:23 +02002932 struct act_rule *rule;
Christopher Faulet3e964192018-10-24 11:39:23 +02002933 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002934 int act_opts = 0;
Christopher Faulet3e964192018-10-24 11:39:23 +02002935
Christopher Faulet3e964192018-10-24 11:39:23 +02002936 /* If "the current_rule_list" match the executed rule list, we are in
2937 * resume condition. If a resume is needed it is always in the action
2938 * and never in the ACL or converters. In this case, we initialise the
2939 * current rule, and go to the action execution point.
2940 */
2941 if (s->current_rule) {
2942 rule = s->current_rule;
2943 s->current_rule = NULL;
2944 if (s->current_rule_list == rules)
2945 goto resume_execution;
2946 }
2947 s->current_rule_list = rules;
2948
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002949 /* start the ruleset evaluation in strict mode */
2950 txn->rsp.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01002951
Christopher Faulet3e964192018-10-24 11:39:23 +02002952 list_for_each_entry(rule, rules, list) {
2953 /* check optional condition */
2954 if (rule->cond) {
2955 int ret;
2956
2957 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
2958 ret = acl_pass(ret);
2959
2960 if (rule->cond->pol == ACL_COND_UNLESS)
2961 ret = !ret;
2962
2963 if (!ret) /* condition not matched */
2964 continue;
2965 }
2966
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002967 act_opts |= ACT_OPT_FIRST;
Christopher Faulet3e964192018-10-24 11:39:23 +02002968resume_execution:
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002969
2970 /* Always call the action function if defined */
2971 if (rule->action_ptr) {
2972 if ((s->req.flags & CF_READ_ERROR) ||
2973 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
2974 (px->options & PR_O_ABRT_CLOSE)))
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002975 act_opts |= ACT_OPT_FINAL;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002976
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002977 switch (rule->action_ptr(rule, px, sess, s, act_opts)) {
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002978 case ACT_RET_CONT:
2979 break;
2980 case ACT_RET_STOP:
2981 rule_ret = HTTP_RULE_RES_STOP;
2982 goto end;
2983 case ACT_RET_YIELD:
2984 s->current_rule = rule;
2985 rule_ret = HTTP_RULE_RES_YIELD;
2986 goto end;
2987 case ACT_RET_ERR:
2988 rule_ret = HTTP_RULE_RES_ERROR;
2989 goto end;
2990 case ACT_RET_DONE:
2991 rule_ret = HTTP_RULE_RES_DONE;
2992 goto end;
2993 case ACT_RET_DENY:
Christopher Fauletb58f62b2020-01-13 16:40:13 +01002994 if (txn->status == -1)
2995 txn->status = 502;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002996 rule_ret = HTTP_RULE_RES_DENY;
2997 goto end;
2998 case ACT_RET_ABRT:
2999 rule_ret = HTTP_RULE_RES_ABRT;
3000 goto end;
3001 case ACT_RET_INV:
3002 rule_ret = HTTP_RULE_RES_BADREQ;
3003 goto end;
3004 }
3005 continue; /* eval the next rule */
3006 }
3007
3008 /* If not action function defined, check for known actions */
Christopher Faulet3e964192018-10-24 11:39:23 +02003009 switch (rule->action) {
3010 case ACT_ACTION_ALLOW:
3011 rule_ret = HTTP_RULE_RES_STOP; /* "allow" rules are OK */
3012 goto end;
3013
3014 case ACT_ACTION_DENY:
Christopher Faulet5cb513a2020-05-13 17:56:56 +02003015 txn->status = rule->arg.http_reply->status;
3016 txn->http_reply = rule->arg.http_reply;
Christopher Faulet3a26bee2019-12-16 12:47:40 +01003017 rule_ret = HTTP_RULE_RES_DENY;
Christopher Faulet3e964192018-10-24 11:39:23 +02003018 goto end;
3019
3020 case ACT_HTTP_SET_NICE:
Christopher Faulet96bff762019-12-17 13:46:18 +01003021 s->task->nice = rule->arg.http.i;
Christopher Faulet3e964192018-10-24 11:39:23 +02003022 break;
3023
3024 case ACT_HTTP_SET_TOS:
Christopher Faulet96bff762019-12-17 13:46:18 +01003025 conn_set_tos(objt_conn(sess->origin), rule->arg.http.i);
Christopher Faulet3e964192018-10-24 11:39:23 +02003026 break;
3027
3028 case ACT_HTTP_SET_MARK:
Christopher Faulet96bff762019-12-17 13:46:18 +01003029 conn_set_mark(objt_conn(sess->origin), rule->arg.http.i);
Christopher Faulet3e964192018-10-24 11:39:23 +02003030 break;
3031
3032 case ACT_HTTP_SET_LOGL:
Christopher Faulet96bff762019-12-17 13:46:18 +01003033 s->logs.level = rule->arg.http.i;
Christopher Faulet3e964192018-10-24 11:39:23 +02003034 break;
3035
Christopher Faulet3e964192018-10-24 11:39:23 +02003036 case ACT_HTTP_REDIR:
Christopher Faulet49c2a702020-03-06 15:44:37 +01003037 rule_ret = HTTP_RULE_RES_ABRT;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003038 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
Christopher Faulet3a26bee2019-12-16 12:47:40 +01003039 rule_ret = HTTP_RULE_RES_ERROR;
Christopher Faulet3e964192018-10-24 11:39:23 +02003040 goto end;
3041
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01003042 /* other flags exists, but normally, they never be matched. */
Christopher Faulet3e964192018-10-24 11:39:23 +02003043 default:
3044 break;
3045 }
3046 }
3047
3048 end:
Christopher Faulet1aea50e2020-01-17 16:03:53 +01003049 /* if the ruleset evaluation is finished reset the strict mode */
Christopher Faulet46f95542019-12-20 10:07:22 +01003050 if (rule_ret != HTTP_RULE_RES_YIELD)
Christopher Faulet1aea50e2020-01-17 16:03:53 +01003051 txn->rsp.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01003052
Christopher Faulet3e964192018-10-24 11:39:23 +02003053 /* we reached the end of the rules, nothing to report */
3054 return rule_ret;
3055}
3056
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01003057/* Executes backend and frontend http-after-response rules for the stream <s>,
3058 * in that order. it return 1 on success and 0 on error. It is the caller
3059 * responsibility to catch error or ignore it. If it catches it, this function
3060 * may be called a second time, for the internal error.
3061 */
3062int http_eval_after_res_rules(struct stream *s)
3063{
3064 struct session *sess = s->sess;
3065 enum rule_result ret = HTTP_RULE_RES_CONT;
3066
Christopher Faulet507479b2020-05-15 12:29:46 +02003067 /* Eval after-response ruleset only if the reply is not const */
3068 if (s->txn->flags & TX_CONST_REPLY)
3069 goto end;
3070
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01003071 /* prune the request variables if not already done and swap to the response variables. */
3072 if (s->vars_reqres.scope != SCOPE_RES) {
3073 if (!LIST_ISEMPTY(&s->vars_reqres.head))
3074 vars_prune(&s->vars_reqres, s->sess, s);
3075 vars_init(&s->vars_reqres, SCOPE_RES);
3076 }
3077
3078 ret = http_res_get_intercept_rule(s->be, &s->be->http_after_res_rules, s);
3079 if ((ret == HTTP_RULE_RES_CONT || ret == HTTP_RULE_RES_STOP) && sess->fe != s->be)
3080 ret = http_res_get_intercept_rule(sess->fe, &sess->fe->http_after_res_rules, s);
3081
Christopher Faulet507479b2020-05-15 12:29:46 +02003082 end:
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01003083 /* All other codes than CONTINUE, STOP or DONE are forbidden */
3084 return (ret == HTTP_RULE_RES_CONT || ret == HTTP_RULE_RES_STOP || ret == HTTP_RULE_RES_DONE);
3085}
3086
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003087/*
3088 * Manage client-side cookie. It can impact performance by about 2% so it is
3089 * desirable to call it only when needed. This code is quite complex because
3090 * of the multiple very crappy and ambiguous syntaxes we have to support. it
3091 * highly recommended not to touch this part without a good reason !
3092 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003093static void http_manage_client_side_cookies(struct stream *s, struct channel *req)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003094{
3095 struct session *sess = s->sess;
3096 struct http_txn *txn = s->txn;
3097 struct htx *htx;
3098 struct http_hdr_ctx ctx;
3099 char *hdr_beg, *hdr_end, *del_from;
3100 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
3101 int preserve_hdr;
3102
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003103 htx = htxbuf(&req->buf);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003104 ctx.blk = NULL;
3105 while (http_find_header(htx, ist("Cookie"), &ctx, 1)) {
Olivier Houchardf0f42382019-07-22 17:43:46 +02003106 int is_first = 1;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003107 del_from = NULL; /* nothing to be deleted */
3108 preserve_hdr = 0; /* assume we may kill the whole header */
3109
3110 /* Now look for cookies. Conforming to RFC2109, we have to support
3111 * attributes whose name begin with a '$', and associate them with
3112 * the right cookie, if we want to delete this cookie.
3113 * So there are 3 cases for each cookie read :
3114 * 1) it's a special attribute, beginning with a '$' : ignore it.
3115 * 2) it's a server id cookie that we *MAY* want to delete : save
3116 * some pointers on it (last semi-colon, beginning of cookie...)
3117 * 3) it's an application cookie : we *MAY* have to delete a previous
3118 * "special" cookie.
3119 * At the end of loop, if a "special" cookie remains, we may have to
3120 * remove it. If no application cookie persists in the header, we
3121 * *MUST* delete it.
3122 *
3123 * Note: RFC2965 is unclear about the processing of spaces around
3124 * the equal sign in the ATTR=VALUE form. A careful inspection of
3125 * the RFC explicitly allows spaces before it, and not within the
3126 * tokens (attrs or values). An inspection of RFC2109 allows that
3127 * too but section 10.1.3 lets one think that spaces may be allowed
3128 * after the equal sign too, resulting in some (rare) buggy
3129 * implementations trying to do that. So let's do what servers do.
3130 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
3131 * allowed quoted strings in values, with any possible character
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003132 * after a backslash, including control chars and delimiters, which
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003133 * causes parsing to become ambiguous. Browsers also allow spaces
3134 * within values even without quotes.
3135 *
3136 * We have to keep multiple pointers in order to support cookie
3137 * removal at the beginning, middle or end of header without
3138 * corrupting the header. All of these headers are valid :
3139 *
3140 * hdr_beg hdr_end
3141 * | |
3142 * v |
3143 * NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3 |
3144 * NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3 v
3145 * NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3
3146 * | | | | | | |
3147 * | | | | | | |
3148 * | | | | | | +--> next
3149 * | | | | | +----> val_end
3150 * | | | | +-----------> val_beg
3151 * | | | +--------------> equal
3152 * | | +----------------> att_end
3153 * | +---------------------> att_beg
3154 * +--------------------------> prev
3155 *
3156 */
3157 hdr_beg = ctx.value.ptr;
3158 hdr_end = hdr_beg + ctx.value.len;
3159 for (prev = hdr_beg; prev < hdr_end; prev = next) {
3160 /* Iterate through all cookies on this line */
3161
3162 /* find att_beg */
3163 att_beg = prev;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003164 if (!is_first)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003165 att_beg++;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003166 is_first = 0;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003167
3168 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
3169 att_beg++;
3170
3171 /* find att_end : this is the first character after the last non
3172 * space before the equal. It may be equal to hdr_end.
3173 */
3174 equal = att_end = att_beg;
3175 while (equal < hdr_end) {
3176 if (*equal == '=' || *equal == ',' || *equal == ';')
3177 break;
3178 if (HTTP_IS_SPHT(*equal++))
3179 continue;
3180 att_end = equal;
3181 }
3182
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003183 /* here, <equal> points to '=', a delimiter or the end. <att_end>
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003184 * is between <att_beg> and <equal>, both may be identical.
3185 */
3186 /* look for end of cookie if there is an equal sign */
3187 if (equal < hdr_end && *equal == '=') {
3188 /* look for the beginning of the value */
3189 val_beg = equal + 1;
3190 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
3191 val_beg++;
3192
3193 /* find the end of the value, respecting quotes */
3194 next = http_find_cookie_value_end(val_beg, hdr_end);
3195
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003196 /* make val_end point to the first white space or delimiter after the value */
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003197 val_end = next;
3198 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
3199 val_end--;
3200 }
3201 else
3202 val_beg = val_end = next = equal;
3203
3204 /* We have nothing to do with attributes beginning with
3205 * '$'. However, they will automatically be removed if a
3206 * header before them is removed, since they're supposed
3207 * to be linked together.
3208 */
3209 if (*att_beg == '$')
3210 continue;
3211
3212 /* Ignore cookies with no equal sign */
3213 if (equal == next) {
3214 /* This is not our cookie, so we must preserve it. But if we already
3215 * scheduled another cookie for removal, we cannot remove the
3216 * complete header, but we can remove the previous block itself.
3217 */
3218 preserve_hdr = 1;
3219 if (del_from != NULL) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003220 int delta = http_del_hdr_value(hdr_beg, hdr_end, &del_from, prev);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003221 val_end += delta;
3222 next += delta;
3223 hdr_end += delta;
3224 prev = del_from;
3225 del_from = NULL;
3226 }
3227 continue;
3228 }
3229
3230 /* if there are spaces around the equal sign, we need to
3231 * strip them otherwise we'll get trouble for cookie captures,
3232 * or even for rewrites. Since this happens extremely rarely,
3233 * it does not hurt performance.
3234 */
3235 if (unlikely(att_end != equal || val_beg > equal + 1)) {
3236 int stripped_before = 0;
3237 int stripped_after = 0;
3238
3239 if (att_end != equal) {
3240 memmove(att_end, equal, hdr_end - equal);
3241 stripped_before = (att_end - equal);
3242 equal += stripped_before;
3243 val_beg += stripped_before;
3244 }
3245
3246 if (val_beg > equal + 1) {
3247 memmove(equal + 1, val_beg, hdr_end + stripped_before - val_beg);
3248 stripped_after = (equal + 1) - val_beg;
3249 val_beg += stripped_after;
3250 stripped_before += stripped_after;
3251 }
3252
3253 val_end += stripped_before;
3254 next += stripped_before;
3255 hdr_end += stripped_before;
3256 }
3257 /* now everything is as on the diagram above */
3258
3259 /* First, let's see if we want to capture this cookie. We check
3260 * that we don't already have a client side cookie, because we
3261 * can only capture one. Also as an optimisation, we ignore
3262 * cookies shorter than the declared name.
3263 */
3264 if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL &&
3265 (val_end - att_beg >= sess->fe->capture_namelen) &&
3266 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
3267 int log_len = val_end - att_beg;
3268
3269 if ((txn->cli_cookie = pool_alloc(pool_head_capture)) == NULL) {
3270 ha_alert("HTTP logging : out of memory.\n");
3271 } else {
3272 if (log_len > sess->fe->capture_len)
3273 log_len = sess->fe->capture_len;
3274 memcpy(txn->cli_cookie, att_beg, log_len);
3275 txn->cli_cookie[log_len] = 0;
3276 }
3277 }
3278
3279 /* Persistence cookies in passive, rewrite or insert mode have the
3280 * following form :
3281 *
3282 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
3283 *
3284 * For cookies in prefix mode, the form is :
3285 *
3286 * Cookie: NAME=SRV~VALUE
3287 */
3288 if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
3289 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
3290 struct server *srv = s->be->srv;
3291 char *delim;
3292
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003293 /* if we're in cookie prefix mode, we'll search the delimiter so that we
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003294 * have the server ID between val_beg and delim, and the original cookie between
3295 * delim+1 and val_end. Otherwise, delim==val_end :
3296 *
3297 * hdr_beg
3298 * |
3299 * v
3300 * NAME=SRV; # in all but prefix modes
3301 * NAME=SRV~OPAQUE ; # in prefix mode
3302 * || || | |+-> next
3303 * || || | +--> val_end
3304 * || || +---------> delim
3305 * || |+------------> val_beg
3306 * || +-------------> att_end = equal
3307 * |+-----------------> att_beg
3308 * +------------------> prev
3309 *
3310 */
3311 if (s->be->ck_opts & PR_CK_PFX) {
3312 for (delim = val_beg; delim < val_end; delim++)
3313 if (*delim == COOKIE_DELIM)
3314 break;
3315 }
3316 else {
3317 char *vbar1;
3318 delim = val_end;
3319 /* Now check if the cookie contains a date field, which would
3320 * appear after a vertical bar ('|') just after the server name
3321 * and before the delimiter.
3322 */
3323 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
3324 if (vbar1) {
3325 /* OK, so left of the bar is the server's cookie and
3326 * right is the last seen date. It is a base64 encoded
3327 * 30-bit value representing the UNIX date since the
3328 * epoch in 4-second quantities.
3329 */
3330 int val;
3331 delim = vbar1++;
3332 if (val_end - vbar1 >= 5) {
3333 val = b64tos30(vbar1);
3334 if (val > 0)
3335 txn->cookie_last_date = val << 2;
3336 }
3337 /* look for a second vertical bar */
3338 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
3339 if (vbar1 && (val_end - vbar1 > 5)) {
3340 val = b64tos30(vbar1 + 1);
3341 if (val > 0)
3342 txn->cookie_first_date = val << 2;
3343 }
3344 }
3345 }
3346
3347 /* if the cookie has an expiration date and the proxy wants to check
3348 * it, then we do that now. We first check if the cookie is too old,
3349 * then only if it has expired. We detect strict overflow because the
3350 * time resolution here is not great (4 seconds). Cookies with dates
3351 * in the future are ignored if their offset is beyond one day. This
3352 * allows an admin to fix timezone issues without expiring everyone
3353 * and at the same time avoids keeping unwanted side effects for too
3354 * long.
3355 */
3356 if (txn->cookie_first_date && s->be->cookie_maxlife &&
3357 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) ||
3358 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
3359 txn->flags &= ~TX_CK_MASK;
3360 txn->flags |= TX_CK_OLD;
3361 delim = val_beg; // let's pretend we have not found the cookie
3362 txn->cookie_first_date = 0;
3363 txn->cookie_last_date = 0;
3364 }
3365 else if (txn->cookie_last_date && s->be->cookie_maxidle &&
3366 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) ||
3367 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
3368 txn->flags &= ~TX_CK_MASK;
3369 txn->flags |= TX_CK_EXPIRED;
3370 delim = val_beg; // let's pretend we have not found the cookie
3371 txn->cookie_first_date = 0;
3372 txn->cookie_last_date = 0;
3373 }
3374
3375 /* Here, we'll look for the first running server which supports the cookie.
3376 * This allows to share a same cookie between several servers, for example
3377 * to dedicate backup servers to specific servers only.
3378 * However, to prevent clients from sticking to cookie-less backup server
3379 * when they have incidentely learned an empty cookie, we simply ignore
3380 * empty cookies and mark them as invalid.
3381 * The same behaviour is applied when persistence must be ignored.
3382 */
3383 if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
3384 srv = NULL;
3385
3386 while (srv) {
3387 if (srv->cookie && (srv->cklen == delim - val_beg) &&
3388 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
3389 if ((srv->cur_state != SRV_ST_STOPPED) ||
3390 (s->be->options & PR_O_PERSIST) ||
3391 (s->flags & SF_FORCE_PRST)) {
3392 /* we found the server and we can use it */
3393 txn->flags &= ~TX_CK_MASK;
3394 txn->flags |= (srv->cur_state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
3395 s->flags |= SF_DIRECT | SF_ASSIGNED;
3396 s->target = &srv->obj_type;
3397 break;
3398 } else {
3399 /* we found a server, but it's down,
3400 * mark it as such and go on in case
3401 * another one is available.
3402 */
3403 txn->flags &= ~TX_CK_MASK;
3404 txn->flags |= TX_CK_DOWN;
3405 }
3406 }
3407 srv = srv->next;
3408 }
3409
3410 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
3411 /* no server matched this cookie or we deliberately skipped it */
3412 txn->flags &= ~TX_CK_MASK;
3413 if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
3414 txn->flags |= TX_CK_UNUSED;
3415 else
3416 txn->flags |= TX_CK_INVALID;
3417 }
3418
3419 /* depending on the cookie mode, we may have to either :
3420 * - delete the complete cookie if we're in insert+indirect mode, so that
3421 * the server never sees it ;
3422 * - remove the server id from the cookie value, and tag the cookie as an
Joseph Herlante9d5c722018-11-25 11:00:25 -08003423 * application cookie so that it does not get accidentally removed later,
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003424 * if we're in cookie prefix mode
3425 */
3426 if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
3427 int delta; /* negative */
3428
3429 memmove(val_beg, delim + 1, hdr_end - (delim + 1));
3430 delta = val_beg - (delim + 1);
3431 val_end += delta;
3432 next += delta;
3433 hdr_end += delta;
3434 del_from = NULL;
3435 preserve_hdr = 1; /* we want to keep this cookie */
3436 }
3437 else if (del_from == NULL &&
3438 (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
3439 del_from = prev;
3440 }
3441 }
3442 else {
3443 /* This is not our cookie, so we must preserve it. But if we already
3444 * scheduled another cookie for removal, we cannot remove the
3445 * complete header, but we can remove the previous block itself.
3446 */
3447 preserve_hdr = 1;
3448
3449 if (del_from != NULL) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003450 int delta = http_del_hdr_value(hdr_beg, hdr_end, &del_from, prev);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003451 if (att_beg >= del_from)
3452 att_beg += delta;
3453 if (att_end >= del_from)
3454 att_end += delta;
3455 val_beg += delta;
3456 val_end += delta;
3457 next += delta;
3458 hdr_end += delta;
3459 prev = del_from;
3460 del_from = NULL;
3461 }
3462 }
3463
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003464 } /* for each cookie */
3465
3466
3467 /* There are no more cookies on this line.
3468 * We may still have one (or several) marked for deletion at the
3469 * end of the line. We must do this now in two ways :
3470 * - if some cookies must be preserved, we only delete from the
3471 * mark to the end of line ;
3472 * - if nothing needs to be preserved, simply delete the whole header
3473 */
3474 if (del_from) {
3475 hdr_end = (preserve_hdr ? del_from : hdr_beg);
3476 }
3477 if ((hdr_end - hdr_beg) != ctx.value.len) {
Christopher Faulet3e2638e2019-06-18 09:49:16 +02003478 if (hdr_beg != hdr_end)
3479 htx_change_blk_value_len(htx, ctx.blk, hdr_end - hdr_beg);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003480 else
3481 http_remove_header(htx, &ctx);
3482 }
3483 } /* for each "Cookie header */
3484}
3485
3486/*
3487 * Manage server-side cookies. It can impact performance by about 2% so it is
3488 * desirable to call it only when needed. This function is also used when we
3489 * just need to know if there is a cookie (eg: for check-cache).
3490 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003491static void http_manage_server_side_cookies(struct stream *s, struct channel *res)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003492{
3493 struct session *sess = s->sess;
3494 struct http_txn *txn = s->txn;
3495 struct htx *htx;
3496 struct http_hdr_ctx ctx;
3497 struct server *srv;
3498 char *hdr_beg, *hdr_end;
3499 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau6f7a02a2019-04-15 21:49:49 +02003500 int is_cookie2 = 0;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003501
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003502 htx = htxbuf(&res->buf);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003503
3504 ctx.blk = NULL;
3505 while (1) {
Olivier Houchardf0f42382019-07-22 17:43:46 +02003506 int is_first = 1;
3507
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003508 if (!http_find_header(htx, ist("Set-Cookie"), &ctx, 1)) {
3509 if (!http_find_header(htx, ist("Set-Cookie2"), &ctx, 1))
3510 break;
3511 is_cookie2 = 1;
3512 }
3513
3514 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
3515 * <prev> points to the colon.
3516 */
3517 txn->flags |= TX_SCK_PRESENT;
3518
3519 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
3520 * check-cache is enabled) and we are not interested in checking
3521 * them. Warning, the cookie capture is declared in the frontend.
3522 */
3523 if (s->be->cookie_name == NULL && sess->fe->capture_name == NULL)
3524 break;
3525
3526 /* OK so now we know we have to process this response cookie.
3527 * The format of the Set-Cookie header is slightly different
3528 * from the format of the Cookie header in that it does not
3529 * support the comma as a cookie delimiter (thus the header
3530 * cannot be folded) because the Expires attribute described in
3531 * the original Netscape's spec may contain an unquoted date
3532 * with a comma inside. We have to live with this because
3533 * many browsers don't support Max-Age and some browsers don't
3534 * support quoted strings. However the Set-Cookie2 header is
3535 * clean.
3536 *
3537 * We have to keep multiple pointers in order to support cookie
3538 * removal at the beginning, middle or end of header without
3539 * corrupting the header (in case of set-cookie2). A special
3540 * pointer, <scav> points to the beginning of the set-cookie-av
3541 * fields after the first semi-colon. The <next> pointer points
3542 * either to the end of line (set-cookie) or next unquoted comma
3543 * (set-cookie2). All of these headers are valid :
3544 *
3545 * hdr_beg hdr_end
3546 * | |
3547 * v |
3548 * NAME1 = VALUE 1 ; Secure; Path="/" |
3549 * NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT v
3550 * NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT
3551 * NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard
3552 * | | | | | | | |
3553 * | | | | | | | +-> next
3554 * | | | | | | +------------> scav
3555 * | | | | | +--------------> val_end
3556 * | | | | +--------------------> val_beg
3557 * | | | +----------------------> equal
3558 * | | +------------------------> att_end
3559 * | +----------------------------> att_beg
3560 * +------------------------------> prev
3561 * -------------------------------> hdr_beg
3562 */
3563 hdr_beg = ctx.value.ptr;
3564 hdr_end = hdr_beg + ctx.value.len;
3565 for (prev = hdr_beg; prev < hdr_end; prev = next) {
3566
3567 /* Iterate through all cookies on this line */
3568
3569 /* find att_beg */
3570 att_beg = prev;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003571 if (!is_first)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003572 att_beg++;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003573 is_first = 0;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003574
3575 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
3576 att_beg++;
3577
3578 /* find att_end : this is the first character after the last non
3579 * space before the equal. It may be equal to hdr_end.
3580 */
3581 equal = att_end = att_beg;
3582
3583 while (equal < hdr_end) {
3584 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
3585 break;
3586 if (HTTP_IS_SPHT(*equal++))
3587 continue;
3588 att_end = equal;
3589 }
3590
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003591 /* here, <equal> points to '=', a delimiter or the end. <att_end>
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003592 * is between <att_beg> and <equal>, both may be identical.
3593 */
3594
3595 /* look for end of cookie if there is an equal sign */
3596 if (equal < hdr_end && *equal == '=') {
3597 /* look for the beginning of the value */
3598 val_beg = equal + 1;
3599 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
3600 val_beg++;
3601
3602 /* find the end of the value, respecting quotes */
3603 next = http_find_cookie_value_end(val_beg, hdr_end);
3604
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003605 /* make val_end point to the first white space or delimiter after the value */
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003606 val_end = next;
3607 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
3608 val_end--;
3609 }
3610 else {
3611 /* <equal> points to next comma, semi-colon or EOL */
3612 val_beg = val_end = next = equal;
3613 }
3614
3615 if (next < hdr_end) {
3616 /* Set-Cookie2 supports multiple cookies, and <next> points to
3617 * a colon or semi-colon before the end. So skip all attr-value
3618 * pairs and look for the next comma. For Set-Cookie, since
3619 * commas are permitted in values, skip to the end.
3620 */
3621 if (is_cookie2)
3622 next = http_find_hdr_value_end(next, hdr_end);
3623 else
3624 next = hdr_end;
3625 }
3626
3627 /* Now everything is as on the diagram above */
3628
3629 /* Ignore cookies with no equal sign */
3630 if (equal == val_end)
3631 continue;
3632
3633 /* If there are spaces around the equal sign, we need to
3634 * strip them otherwise we'll get trouble for cookie captures,
3635 * or even for rewrites. Since this happens extremely rarely,
3636 * it does not hurt performance.
3637 */
3638 if (unlikely(att_end != equal || val_beg > equal + 1)) {
3639 int stripped_before = 0;
3640 int stripped_after = 0;
3641
3642 if (att_end != equal) {
3643 memmove(att_end, equal, hdr_end - equal);
3644 stripped_before = (att_end - equal);
3645 equal += stripped_before;
3646 val_beg += stripped_before;
3647 }
3648
3649 if (val_beg > equal + 1) {
3650 memmove(equal + 1, val_beg, hdr_end + stripped_before - val_beg);
3651 stripped_after = (equal + 1) - val_beg;
3652 val_beg += stripped_after;
3653 stripped_before += stripped_after;
3654 }
3655
3656 val_end += stripped_before;
3657 next += stripped_before;
3658 hdr_end += stripped_before;
3659
Christopher Faulet3e2638e2019-06-18 09:49:16 +02003660 htx_change_blk_value_len(htx, ctx.blk, hdr_end - hdr_beg);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003661 ctx.value.len = hdr_end - hdr_beg;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003662 }
3663
3664 /* First, let's see if we want to capture this cookie. We check
3665 * that we don't already have a server side cookie, because we
3666 * can only capture one. Also as an optimisation, we ignore
3667 * cookies shorter than the declared name.
3668 */
3669 if (sess->fe->capture_name != NULL &&
3670 txn->srv_cookie == NULL &&
3671 (val_end - att_beg >= sess->fe->capture_namelen) &&
3672 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
3673 int log_len = val_end - att_beg;
3674 if ((txn->srv_cookie = pool_alloc(pool_head_capture)) == NULL) {
3675 ha_alert("HTTP logging : out of memory.\n");
3676 }
3677 else {
3678 if (log_len > sess->fe->capture_len)
3679 log_len = sess->fe->capture_len;
3680 memcpy(txn->srv_cookie, att_beg, log_len);
3681 txn->srv_cookie[log_len] = 0;
3682 }
3683 }
3684
3685 srv = objt_server(s->target);
3686 /* now check if we need to process it for persistence */
3687 if (!(s->flags & SF_IGNORE_PRST) &&
3688 (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
3689 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
3690 /* assume passive cookie by default */
3691 txn->flags &= ~TX_SCK_MASK;
3692 txn->flags |= TX_SCK_FOUND;
3693
3694 /* If the cookie is in insert mode on a known server, we'll delete
3695 * this occurrence because we'll insert another one later.
3696 * We'll delete it too if the "indirect" option is set and we're in
3697 * a direct access.
3698 */
3699 if (s->be->ck_opts & PR_CK_PSV) {
3700 /* The "preserve" flag was set, we don't want to touch the
3701 * server's cookie.
3702 */
3703 }
3704 else if ((srv && (s->be->ck_opts & PR_CK_INS)) ||
3705 ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) {
3706 /* this cookie must be deleted */
3707 if (prev == hdr_beg && next == hdr_end) {
3708 /* whole header */
3709 http_remove_header(htx, &ctx);
3710 /* note: while both invalid now, <next> and <hdr_end>
3711 * are still equal, so the for() will stop as expected.
3712 */
3713 } else {
3714 /* just remove the value */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003715 int delta = http_del_hdr_value(hdr_beg, hdr_end, &prev, next);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003716 next = prev;
3717 hdr_end += delta;
3718 }
3719 txn->flags &= ~TX_SCK_MASK;
3720 txn->flags |= TX_SCK_DELETED;
3721 /* and go on with next cookie */
3722 }
3723 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) {
3724 /* replace bytes val_beg->val_end with the cookie name associated
3725 * with this server since we know it.
3726 */
3727 int sliding, delta;
3728
3729 ctx.value = ist2(val_beg, val_end - val_beg);
3730 ctx.lws_before = ctx.lws_after = 0;
3731 http_replace_header_value(htx, &ctx, ist2(srv->cookie, srv->cklen));
3732 delta = srv->cklen - (val_end - val_beg);
3733 sliding = (ctx.value.ptr - val_beg);
3734 hdr_beg += sliding;
3735 val_beg += sliding;
3736 next += sliding + delta;
3737 hdr_end += sliding + delta;
3738
3739 txn->flags &= ~TX_SCK_MASK;
3740 txn->flags |= TX_SCK_REPLACED;
3741 }
3742 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) {
3743 /* insert the cookie name associated with this server
3744 * before existing cookie, and insert a delimiter between them..
3745 */
3746 int sliding, delta;
3747 ctx.value = ist2(val_beg, 0);
3748 ctx.lws_before = ctx.lws_after = 0;
3749 http_replace_header_value(htx, &ctx, ist2(srv->cookie, srv->cklen + 1));
3750 delta = srv->cklen + 1;
3751 sliding = (ctx.value.ptr - val_beg);
3752 hdr_beg += sliding;
3753 val_beg += sliding;
3754 next += sliding + delta;
3755 hdr_end += sliding + delta;
3756
3757 val_beg[srv->cklen] = COOKIE_DELIM;
3758 txn->flags &= ~TX_SCK_MASK;
3759 txn->flags |= TX_SCK_REPLACED;
3760 }
3761 }
3762 /* that's done for this cookie, check the next one on the same
3763 * line when next != hdr_end (only if is_cookie2).
3764 */
3765 }
3766 }
3767}
3768
Christopher Faulet25a02f62018-10-24 12:00:25 +02003769/*
3770 * Parses the Cache-Control and Pragma request header fields to determine if
3771 * the request may be served from the cache and/or if it is cacheable. Updates
3772 * s->txn->flags.
3773 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003774void http_check_request_for_cacheability(struct stream *s, struct channel *req)
Christopher Faulet25a02f62018-10-24 12:00:25 +02003775{
3776 struct http_txn *txn = s->txn;
3777 struct htx *htx;
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003778 struct http_hdr_ctx ctx = { .blk = NULL };
3779 int pragma_found, cc_found;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003780
3781 if ((txn->flags & (TX_CACHEABLE|TX_CACHE_IGNORE)) == TX_CACHE_IGNORE)
3782 return; /* nothing more to do here */
3783
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003784 htx = htxbuf(&req->buf);
Christopher Faulet25a02f62018-10-24 12:00:25 +02003785 pragma_found = cc_found = 0;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003786
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003787 /* Check "pragma" header for HTTP/1.0 compatibility. */
3788 if (http_find_header(htx, ist("pragma"), &ctx, 1)) {
3789 if (isteqi(ctx.value, ist("no-cache"))) {
3790 pragma_found = 1;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003791 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003792 }
Christopher Faulet25a02f62018-10-24 12:00:25 +02003793
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003794 ctx.blk = NULL;
3795 /* Don't use the cache and don't try to store if we found the
3796 * Authorization header */
3797 if (http_find_header(htx, ist("authorization"), &ctx, 1)) {
3798 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3799 txn->flags |= TX_CACHE_IGNORE;
3800 }
Christopher Faulet25a02f62018-10-24 12:00:25 +02003801
Christopher Faulet25a02f62018-10-24 12:00:25 +02003802
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003803 /* Look for "cache-control" header and iterate over all the values
3804 * until we find one that specifies that caching is possible or not. */
3805 ctx.blk = NULL;
3806 while (http_find_header(htx, ist("cache-control"), &ctx, 0)) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003807 cc_found = 1;
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003808 /* We don't check the values after max-age, max-stale nor min-fresh,
3809 * we simply don't use the cache when they're specified. */
3810 if (istmatchi(ctx.value, ist("max-age")) ||
3811 istmatchi(ctx.value, ist("no-cache")) ||
3812 istmatchi(ctx.value, ist("max-stale")) ||
3813 istmatchi(ctx.value, ist("min-fresh"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003814 txn->flags |= TX_CACHE_IGNORE;
3815 continue;
3816 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003817 if (istmatchi(ctx.value, ist("no-store"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003818 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3819 continue;
3820 }
3821 }
3822
3823 /* RFC7234#5.4:
3824 * When the Cache-Control header field is also present and
3825 * understood in a request, Pragma is ignored.
3826 * When the Cache-Control header field is not present in a
3827 * request, caches MUST consider the no-cache request
3828 * pragma-directive as having the same effect as if
3829 * "Cache-Control: no-cache" were present.
3830 */
3831 if (!cc_found && pragma_found)
3832 txn->flags |= TX_CACHE_IGNORE;
3833}
3834
3835/*
3836 * Check if response is cacheable or not. Updates s->txn->flags.
3837 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003838void http_check_response_for_cacheability(struct stream *s, struct channel *res)
Christopher Faulet25a02f62018-10-24 12:00:25 +02003839{
3840 struct http_txn *txn = s->txn;
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003841 struct http_hdr_ctx ctx = { .blk = NULL };
Christopher Faulet25a02f62018-10-24 12:00:25 +02003842 struct htx *htx;
Remi Tricot-Le Bretoncc9bf2e2020-11-12 11:14:41 +01003843 int has_freshness_info = 0;
3844 int has_validator = 0;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003845
3846 if (txn->status < 200) {
3847 /* do not try to cache interim responses! */
3848 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3849 return;
3850 }
3851
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003852 htx = htxbuf(&res->buf);
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003853 /* Check "pragma" header for HTTP/1.0 compatibility. */
3854 if (http_find_header(htx, ist("pragma"), &ctx, 1)) {
3855 if (isteqi(ctx.value, ist("no-cache"))) {
3856 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3857 return;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003858 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003859 }
Christopher Faulet25a02f62018-10-24 12:00:25 +02003860
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003861 /* Look for "cache-control" header and iterate over all the values
3862 * until we find one that specifies that caching is possible or not. */
3863 ctx.blk = NULL;
3864 while (http_find_header(htx, ist("cache-control"), &ctx, 0)) {
3865 if (isteqi(ctx.value, ist("public"))) {
3866 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003867 continue;
3868 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003869 if (isteqi(ctx.value, ist("private")) ||
3870 isteqi(ctx.value, ist("no-cache")) ||
3871 isteqi(ctx.value, ist("no-store")) ||
3872 isteqi(ctx.value, ist("max-age=0")) ||
3873 isteqi(ctx.value, ist("s-maxage=0"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003874 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003875 continue;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003876 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003877 /* We might have a no-cache="set-cookie" form. */
3878 if (istmatchi(ctx.value, ist("no-cache=\"set-cookie"))) {
3879 txn->flags &= ~TX_CACHE_COOK;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003880 continue;
3881 }
Remi Tricot-Le Bretoncc9bf2e2020-11-12 11:14:41 +01003882
3883 if (istmatchi(ctx.value, ist("s-maxage")) ||
3884 istmatchi(ctx.value, ist("max-age"))) {
3885 has_freshness_info = 1;
3886 continue;
3887 }
3888 }
3889
3890 /* If no freshness information could be found in Cache-Control values,
3891 * look for an Expires header. */
3892 if (!has_freshness_info) {
3893 ctx.blk = NULL;
3894 has_freshness_info = http_find_header(htx, ist("expires"), &ctx, 0);
Christopher Faulet25a02f62018-10-24 12:00:25 +02003895 }
Remi Tricot-Le Bretoncc9bf2e2020-11-12 11:14:41 +01003896
3897 /* If no freshness information could be found in Cache-Control or Expires
3898 * values, look for an explicit validator. */
3899 if (!has_freshness_info) {
3900 ctx.blk = NULL;
3901 has_validator = 1;
3902 if (!http_find_header(htx, ist("etag"), &ctx, 0)) {
3903 ctx.blk = NULL;
3904 if (!http_find_header(htx, ist("last-modified"), &ctx, 0))
3905 has_validator = 0;
3906 }
3907 }
3908
3909 /* We won't store an entry that has neither a cache validator nor an
3910 * explicit expiration time, as suggested in RFC 7234#3. */
3911 if (!has_freshness_info && !has_validator)
3912 txn->flags |= TX_CACHE_IGNORE;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003913}
3914
Christopher Faulet377c5a52018-10-24 21:21:30 +02003915/*
3916 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
3917 * for the current backend.
3918 *
3919 * It is assumed that the request is either a HEAD, GET, or POST and that the
3920 * uri_auth field is valid.
3921 *
3922 * Returns 1 if stats should be provided, otherwise 0.
3923 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003924static int http_stats_check_uri(struct stream *s, struct http_txn *txn, struct proxy *backend)
Christopher Faulet377c5a52018-10-24 21:21:30 +02003925{
3926 struct uri_auth *uri_auth = backend->uri_auth;
3927 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003928 struct htx_sl *sl;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003929 struct ist uri;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003930
3931 if (!uri_auth)
3932 return 0;
3933
3934 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
3935 return 0;
3936
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003937 htx = htxbuf(&s->req.buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02003938 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003939 uri = htx_sl_req_uri(sl);
Willy Tarreau1eb3b482019-10-31 15:50:28 +01003940 if (*uri_auth->uri_prefix == '/')
3941 uri = http_get_path(uri);
Christopher Faulet377c5a52018-10-24 21:21:30 +02003942
3943 /* check URI size */
3944 if (uri_auth->uri_len > uri.len)
3945 return 0;
3946
3947 if (memcmp(uri.ptr, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
3948 return 0;
3949
3950 return 1;
3951}
3952
3953/* This function prepares an applet to handle the stats. It can deal with the
3954 * "100-continue" expectation, check that admin rules are met for POST requests,
3955 * and program a response message if something was unexpected. It cannot fail
3956 * and always relies on the stats applet to complete the job. It does not touch
3957 * analysers nor counters, which are left to the caller. It does not touch
3958 * s->target which is supposed to already point to the stats applet. The caller
3959 * is expected to have already assigned an appctx to the stream.
3960 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003961static int http_handle_stats(struct stream *s, struct channel *req)
Christopher Faulet377c5a52018-10-24 21:21:30 +02003962{
3963 struct stats_admin_rule *stats_admin_rule;
3964 struct stream_interface *si = &s->si[1];
3965 struct session *sess = s->sess;
3966 struct http_txn *txn = s->txn;
3967 struct http_msg *msg = &txn->req;
3968 struct uri_auth *uri_auth = s->be->uri_auth;
3969 const char *h, *lookup, *end;
3970 struct appctx *appctx;
3971 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003972 struct htx_sl *sl;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003973
3974 appctx = si_appctx(si);
3975 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
3976 appctx->st1 = appctx->st2 = 0;
3977 appctx->ctx.stats.st_code = STAT_STATUS_INIT;
Willy Tarreau676c29e2019-10-09 10:50:01 +02003978 appctx->ctx.stats.flags |= uri_auth->flags;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003979 appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */
3980 if ((msg->flags & HTTP_MSGF_VER_11) && (txn->meth != HTTP_METH_HEAD))
3981 appctx->ctx.stats.flags |= STAT_CHUNKED;
3982
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003983 htx = htxbuf(&req->buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02003984 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003985 lookup = HTX_SL_REQ_UPTR(sl) + uri_auth->uri_len;
3986 end = HTX_SL_REQ_UPTR(sl) + HTX_SL_REQ_ULEN(sl);
Christopher Faulet377c5a52018-10-24 21:21:30 +02003987
3988 for (h = lookup; h <= end - 3; h++) {
3989 if (memcmp(h, ";up", 3) == 0) {
3990 appctx->ctx.stats.flags |= STAT_HIDE_DOWN;
3991 break;
3992 }
Amaury Denoyelle91e55ea2021-02-25 14:46:08 +01003993 }
3994
3995 for (h = lookup; h <= end - 9; h++) {
3996 if (memcmp(h, ";no-maint", 9) == 0) {
Willy Tarreau3e320362020-10-23 17:28:57 +02003997 appctx->ctx.stats.flags |= STAT_HIDE_MAINT;
3998 break;
3999 }
Christopher Faulet377c5a52018-10-24 21:21:30 +02004000 }
4001
4002 if (uri_auth->refresh) {
4003 for (h = lookup; h <= end - 10; h++) {
4004 if (memcmp(h, ";norefresh", 10) == 0) {
4005 appctx->ctx.stats.flags |= STAT_NO_REFRESH;
4006 break;
4007 }
4008 }
4009 }
4010
4011 for (h = lookup; h <= end - 4; h++) {
4012 if (memcmp(h, ";csv", 4) == 0) {
Christopher Faulet6338a082019-09-09 15:50:54 +02004013 appctx->ctx.stats.flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
Christopher Faulet377c5a52018-10-24 21:21:30 +02004014 break;
4015 }
4016 }
4017
4018 for (h = lookup; h <= end - 6; h++) {
4019 if (memcmp(h, ";typed", 6) == 0) {
Christopher Faulet6338a082019-09-09 15:50:54 +02004020 appctx->ctx.stats.flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
Christopher Faulet377c5a52018-10-24 21:21:30 +02004021 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
4022 break;
4023 }
4024 }
4025
Christopher Faulet6338a082019-09-09 15:50:54 +02004026 for (h = lookup; h <= end - 5; h++) {
4027 if (memcmp(h, ";json", 5) == 0) {
4028 appctx->ctx.stats.flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
4029 appctx->ctx.stats.flags |= STAT_FMT_JSON;
4030 break;
4031 }
4032 }
4033
4034 for (h = lookup; h <= end - 12; h++) {
4035 if (memcmp(h, ";json-schema", 12) == 0) {
4036 appctx->ctx.stats.flags &= ~STAT_FMT_MASK;
4037 appctx->ctx.stats.flags |= STAT_JSON_SCHM;
4038 break;
4039 }
4040 }
4041
Christopher Faulet377c5a52018-10-24 21:21:30 +02004042 for (h = lookup; h <= end - 8; h++) {
4043 if (memcmp(h, ";st=", 4) == 0) {
4044 int i;
4045 h += 4;
4046 appctx->ctx.stats.st_code = STAT_STATUS_UNKN;
4047 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
4048 if (strncmp(stat_status_codes[i], h, 4) == 0) {
4049 appctx->ctx.stats.st_code = i;
4050 break;
4051 }
4052 }
4053 break;
4054 }
4055 }
4056
4057 appctx->ctx.stats.scope_str = 0;
4058 appctx->ctx.stats.scope_len = 0;
4059 for (h = lookup; h <= end - 8; h++) {
4060 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
4061 int itx = 0;
4062 const char *h2;
4063 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
4064 const char *err;
4065
4066 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
4067 h2 = h;
Christopher Fauleted7a0662019-01-14 11:07:34 +01004068 appctx->ctx.stats.scope_str = h2 - HTX_SL_REQ_UPTR(sl);
4069 while (h < end) {
Christopher Faulet377c5a52018-10-24 21:21:30 +02004070 if (*h == ';' || *h == '&' || *h == ' ')
4071 break;
4072 itx++;
4073 h++;
4074 }
4075
4076 if (itx > STAT_SCOPE_TXT_MAXLEN)
4077 itx = STAT_SCOPE_TXT_MAXLEN;
4078 appctx->ctx.stats.scope_len = itx;
4079
4080 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
4081 memcpy(scope_txt, h2, itx);
4082 scope_txt[itx] = '\0';
4083 err = invalid_char(scope_txt);
4084 if (err) {
4085 /* bad char in search text => clear scope */
4086 appctx->ctx.stats.scope_str = 0;
4087 appctx->ctx.stats.scope_len = 0;
4088 }
4089 break;
4090 }
4091 }
4092
4093 /* now check whether we have some admin rules for this request */
4094 list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) {
4095 int ret = 1;
4096
4097 if (stats_admin_rule->cond) {
4098 ret = acl_exec_cond(stats_admin_rule->cond, s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
4099 ret = acl_pass(ret);
4100 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
4101 ret = !ret;
4102 }
4103
4104 if (ret) {
4105 /* no rule, or the rule matches */
4106 appctx->ctx.stats.flags |= STAT_ADMIN;
4107 break;
4108 }
4109 }
4110
Christopher Faulet5d45e382019-02-27 15:15:23 +01004111 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
4112 appctx->st0 = STAT_HTTP_HEAD;
4113 else if (txn->meth == HTTP_METH_POST) {
Christopher Fauletbd9e8422019-08-15 22:26:48 +02004114 if (appctx->ctx.stats.flags & STAT_ADMIN) {
Christopher Faulet377c5a52018-10-24 21:21:30 +02004115 appctx->st0 = STAT_HTTP_POST;
Christopher Fauletbd9e8422019-08-15 22:26:48 +02004116 if (msg->msg_state < HTTP_MSG_DATA)
4117 req->analysers |= AN_REQ_HTTP_BODY;
4118 }
Christopher Faulet377c5a52018-10-24 21:21:30 +02004119 else {
Christopher Faulet5d45e382019-02-27 15:15:23 +01004120 /* POST without admin level */
Christopher Faulet377c5a52018-10-24 21:21:30 +02004121 appctx->ctx.stats.flags &= ~STAT_CHUNKED;
4122 appctx->ctx.stats.st_code = STAT_STATUS_DENY;
4123 appctx->st0 = STAT_HTTP_LAST;
4124 }
4125 }
4126 else {
Christopher Faulet5d45e382019-02-27 15:15:23 +01004127 /* Unsupported method */
4128 appctx->ctx.stats.flags &= ~STAT_CHUNKED;
4129 appctx->ctx.stats.st_code = STAT_STATUS_IVAL;
4130 appctx->st0 = STAT_HTTP_LAST;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004131 }
4132
4133 s->task->nice = -32; /* small boost for HTTP statistics */
4134 return 1;
4135}
4136
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004137void http_perform_server_redirect(struct stream *s, struct stream_interface *si)
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004138{
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004139 struct channel *req = &s->req;
4140 struct channel *res = &s->res;
4141 struct server *srv;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004142 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004143 struct htx_sl *sl;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004144 struct ist path, location;
4145 unsigned int flags;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004146
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004147 /*
4148 * Create the location
4149 */
4150 chunk_reset(&trash);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004151
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004152 /* 1: add the server's prefix */
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004153 /* special prefix "/" means don't change URL */
4154 srv = __objt_server(s->target);
4155 if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') {
4156 if (!chunk_memcat(&trash, srv->rdr_pfx, srv->rdr_len))
4157 return;
4158 }
4159
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004160 /* 2: add the request Path */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004161 htx = htxbuf(&req->buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02004162 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004163 path = http_get_path(htx_sl_req_uri(sl));
Tim Duesterhused526372020-03-05 17:56:33 +01004164 if (!isttest(path))
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004165 return;
4166
4167 if (!chunk_memcat(&trash, path.ptr, path.len))
4168 return;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004169 location = ist2(trash.area, trash.data);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004170
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004171 /*
4172 * Create the 302 respone
4173 */
4174 htx = htx_from_buf(&res->buf);
4175 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
4176 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
4177 ist("HTTP/1.1"), ist("302"), ist("Found"));
4178 if (!sl)
4179 goto fail;
4180 sl->info.res.status = 302;
4181 s->txn->status = 302;
4182
4183 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) ||
4184 !htx_add_header(htx, ist("Connection"), ist("close")) ||
4185 !htx_add_header(htx, ist("Content-length"), ist("0")) ||
4186 !htx_add_header(htx, ist("Location"), location))
4187 goto fail;
4188
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004189 if (!htx_add_endof(htx, HTX_BLK_EOH))
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004190 goto fail;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004191
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004192 htx->flags |= HTX_FL_EOM;
Christopher Fauletc20afb82020-01-24 19:16:26 +01004193 htx_to_buf(htx, &res->buf);
Christopher Fauleta72a7e42020-01-28 09:28:11 +01004194 if (!http_forward_proxy_resp(s, 1))
4195 goto fail;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004196
4197 /* return without error. */
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004198 si_shutr(si);
4199 si_shutw(si);
4200 si->err_type = SI_ET_NONE;
4201 si->state = SI_ST_CLO;
4202
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004203 if (!(s->flags & SF_ERR_MASK))
4204 s->flags |= SF_ERR_LOCAL;
4205 if (!(s->flags & SF_FINST_MASK))
4206 s->flags |= SF_FINST_C;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004207
4208 /* FIXME: we should increase a counter of redirects per server and per backend. */
4209 srv_inc_sess_ctr(srv);
4210 srv_set_sess_last(srv);
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004211 return;
4212
4213 fail:
4214 /* If an error occurred, remove the incomplete HTTP response from the
4215 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004216 channel_htx_truncate(res, htx);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004217}
4218
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05004219/* This function terminates the request because it was completely analyzed or
Christopher Fauletf2824e62018-10-01 12:12:37 +02004220 * because an error was triggered during the body forwarding.
4221 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004222static void http_end_request(struct stream *s)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004223{
4224 struct channel *chn = &s->req;
4225 struct http_txn *txn = s->txn;
4226
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004227 DBG_TRACE_ENTER(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004228
Christopher Fauletb42a8b62018-11-19 21:59:00 +01004229 if (unlikely(txn->req.msg_state == HTTP_MSG_ERROR ||
4230 txn->rsp.msg_state == HTTP_MSG_ERROR)) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004231 channel_abort(chn);
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004232 channel_htx_truncate(chn, htxbuf(&chn->buf));
Christopher Fauletf2824e62018-10-01 12:12:37 +02004233 goto end;
4234 }
4235
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004236 if (unlikely(txn->req.msg_state < HTTP_MSG_DONE)) {
4237 DBG_TRACE_DEVEL("waiting end of the request", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004238 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004239 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004240
4241 if (txn->req.msg_state == HTTP_MSG_DONE) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004242 /* No need to read anymore, the request was completely parsed.
4243 * We can shut the read side unless we want to abort_on_close,
4244 * or we have a POST request. The issue with POST requests is
4245 * that some browsers still send a CRLF after the request, and
4246 * this CRLF must be read so that it does not remain in the kernel
4247 * buffers, otherwise a close could cause an RST on some systems
4248 * (eg: Linux).
4249 */
Christopher Faulet769d0e92019-03-22 14:23:18 +01004250 if (!(s->be->options & PR_O_ABRT_CLOSE) && txn->meth != HTTP_METH_POST)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004251 channel_dont_read(chn);
4252
4253 /* if the server closes the connection, we want to immediately react
4254 * and close the socket to save packets and syscalls.
4255 */
4256 s->si[1].flags |= SI_FL_NOHALF;
4257
4258 /* In any case we've finished parsing the request so we must
4259 * disable Nagle when sending data because 1) we're not going
4260 * to shut this side, and 2) the server is waiting for us to
4261 * send pending data.
4262 */
4263 chn->flags |= CF_NEVER_WAIT;
4264
Christopher Fauletd01ce402019-01-02 17:44:13 +01004265 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
4266 /* The server has not finished to respond, so we
4267 * don't want to move in order not to upset it.
4268 */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004269 DBG_TRACE_DEVEL("waiting end of the response", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletd01ce402019-01-02 17:44:13 +01004270 return;
4271 }
4272
Christopher Fauletf2824e62018-10-01 12:12:37 +02004273 /* When we get here, it means that both the request and the
4274 * response have finished receiving. Depending on the connection
4275 * mode, we'll have to wait for the last bytes to leave in either
4276 * direction, and sometimes for a close to be effective.
4277 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02004278 if (txn->flags & TX_CON_WANT_TUN) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004279 /* Tunnel mode will not have any analyser so it needs to
4280 * poll for reads.
4281 */
4282 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004283 if (b_data(&chn->buf)) {
4284 DBG_TRACE_DEVEL("waiting to flush the request", STRM_EV_HTTP_ANA, s, txn);
Christopher Faulet9768c262018-10-22 09:34:31 +02004285 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004286 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004287 txn->req.msg_state = HTTP_MSG_TUNNEL;
4288 }
4289 else {
4290 /* we're not expecting any new data to come for this
4291 * transaction, so we can close it.
Christopher Faulet9768c262018-10-22 09:34:31 +02004292 *
4293 * However, there is an exception if the response
4294 * length is undefined. In this case, we need to wait
4295 * the close from the server. The response will be
4296 * switched in TUNNEL mode until the end.
Christopher Fauletf2824e62018-10-01 12:12:37 +02004297 */
4298 if (!(txn->rsp.flags & HTTP_MSGF_XFER_LEN) &&
4299 txn->rsp.msg_state != HTTP_MSG_CLOSED)
Christopher Faulet9768c262018-10-22 09:34:31 +02004300 goto check_channel_flags;
Christopher Fauletf2824e62018-10-01 12:12:37 +02004301
4302 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4303 channel_shutr_now(chn);
4304 channel_shutw_now(chn);
4305 }
4306 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004307 goto check_channel_flags;
4308 }
4309
4310 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
4311 http_msg_closing:
4312 /* nothing else to forward, just waiting for the output buffer
4313 * to be empty and for the shutw_now to take effect.
4314 */
4315 if (channel_is_empty(chn)) {
4316 txn->req.msg_state = HTTP_MSG_CLOSED;
4317 goto http_msg_closed;
4318 }
4319 else if (chn->flags & CF_SHUTW) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004320 txn->req.msg_state = HTTP_MSG_ERROR;
4321 goto end;
4322 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004323 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004324 return;
4325 }
4326
4327 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
4328 http_msg_closed:
Christopher Fauletf2824e62018-10-01 12:12:37 +02004329 /* if we don't know whether the server will close, we need to hard close */
4330 if (txn->rsp.flags & HTTP_MSGF_XFER_LEN)
4331 s->si[1].flags |= SI_FL_NOLINGER; /* we want to close ASAP */
Christopher Fauletf2824e62018-10-01 12:12:37 +02004332 /* see above in MSG_DONE why we only do this in these states */
Christopher Faulet769d0e92019-03-22 14:23:18 +01004333 if (!(s->be->options & PR_O_ABRT_CLOSE))
Christopher Fauletf2824e62018-10-01 12:12:37 +02004334 channel_dont_read(chn);
4335 goto end;
4336 }
4337
4338 check_channel_flags:
4339 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4340 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4341 /* if we've just closed an output, let's switch */
4342 txn->req.msg_state = HTTP_MSG_CLOSING;
4343 goto http_msg_closing;
4344 }
4345
4346 end:
4347 chn->analysers &= AN_REQ_FLT_END;
Christopher Faulet198ef8b2020-12-15 13:32:55 +01004348 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
4349 chn->flags |= CF_NEVER_WAIT;
4350 if (HAS_REQ_DATA_FILTERS(s))
Christopher Fauletf2824e62018-10-01 12:12:37 +02004351 chn->analysers |= AN_REQ_FLT_XFER_DATA;
Christopher Faulet198ef8b2020-12-15 13:32:55 +01004352 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004353 channel_auto_close(chn);
4354 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004355 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004356}
4357
4358
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05004359/* This function terminates the response because it was completely analyzed or
Christopher Fauletf2824e62018-10-01 12:12:37 +02004360 * because an error was triggered during the body forwarding.
4361 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004362static void http_end_response(struct stream *s)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004363{
4364 struct channel *chn = &s->res;
4365 struct http_txn *txn = s->txn;
4366
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004367 DBG_TRACE_ENTER(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004368
Christopher Fauletb42a8b62018-11-19 21:59:00 +01004369 if (unlikely(txn->req.msg_state == HTTP_MSG_ERROR ||
4370 txn->rsp.msg_state == HTTP_MSG_ERROR)) {
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004371 channel_htx_truncate(&s->req, htxbuf(&s->req.buf));
Christopher Faulet9768c262018-10-22 09:34:31 +02004372 channel_abort(&s->req);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004373 goto end;
4374 }
4375
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004376 if (unlikely(txn->rsp.msg_state < HTTP_MSG_DONE)) {
4377 DBG_TRACE_DEVEL("waiting end of the response", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004378 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004379 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004380
4381 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
4382 /* In theory, we don't need to read anymore, but we must
4383 * still monitor the server connection for a possible close
4384 * while the request is being uploaded, so we don't disable
4385 * reading.
4386 */
4387 /* channel_dont_read(chn); */
4388
4389 if (txn->req.msg_state < HTTP_MSG_DONE) {
4390 /* The client seems to still be sending data, probably
4391 * because we got an error response during an upload.
4392 * We have the choice of either breaking the connection
4393 * or letting it pass through. Let's do the later.
4394 */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004395 DBG_TRACE_DEVEL("waiting end of the request", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004396 return;
4397 }
4398
4399 /* When we get here, it means that both the request and the
4400 * response have finished receiving. Depending on the connection
4401 * mode, we'll have to wait for the last bytes to leave in either
4402 * direction, and sometimes for a close to be effective.
4403 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02004404 if (txn->flags & TX_CON_WANT_TUN) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004405 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004406 if (b_data(&chn->buf)) {
4407 DBG_TRACE_DEVEL("waiting to flush the respone", STRM_EV_HTTP_ANA, s, txn);
Christopher Faulet9768c262018-10-22 09:34:31 +02004408 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004409 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004410 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
4411 }
4412 else {
4413 /* we're not expecting any new data to come for this
4414 * transaction, so we can close it.
4415 */
4416 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4417 channel_shutr_now(chn);
4418 channel_shutw_now(chn);
4419 }
4420 }
4421 goto check_channel_flags;
4422 }
4423
4424 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
4425 http_msg_closing:
4426 /* nothing else to forward, just waiting for the output buffer
4427 * to be empty and for the shutw_now to take effect.
4428 */
4429 if (channel_is_empty(chn)) {
4430 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4431 goto http_msg_closed;
4432 }
4433 else if (chn->flags & CF_SHUTW) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004434 txn->rsp.msg_state = HTTP_MSG_ERROR;
Christopher Fauletcff0f732019-12-16 16:13:44 +01004435 _HA_ATOMIC_ADD(&strm_sess(s)->fe->fe_counters.cli_aborts, 1);
Olivier Houcharda798bf52019-03-08 18:52:00 +01004436 _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
William Lallemand36119de2021-03-08 15:26:48 +01004437 if (strm_sess(s)->listener && strm_sess(s)->listener->counters)
Christopher Fauletcff0f732019-12-16 16:13:44 +01004438 _HA_ATOMIC_ADD(&strm_sess(s)->listener->counters->cli_aborts, 1);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004439 if (objt_server(s->target))
Christopher Fauletcff0f732019-12-16 16:13:44 +01004440 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.cli_aborts, 1);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004441 goto end;
4442 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004443 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004444 return;
4445 }
4446
4447 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
4448 http_msg_closed:
4449 /* drop any pending data */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004450 channel_htx_truncate(&s->req, htxbuf(&s->req.buf));
Christopher Faulet9768c262018-10-22 09:34:31 +02004451 channel_abort(&s->req);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004452 goto end;
4453 }
4454
4455 check_channel_flags:
4456 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4457 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4458 /* if we've just closed an output, let's switch */
4459 txn->rsp.msg_state = HTTP_MSG_CLOSING;
4460 goto http_msg_closing;
4461 }
4462
4463 end:
4464 chn->analysers &= AN_RES_FLT_END;
Christopher Faulet198ef8b2020-12-15 13:32:55 +01004465 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
4466 chn->flags |= CF_NEVER_WAIT;
4467 if (HAS_RSP_DATA_FILTERS(s))
4468 chn->analysers |= AN_RES_FLT_XFER_DATA;
4469 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004470 channel_auto_close(chn);
4471 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004472 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004473}
4474
Christopher Fauletef70e252020-01-28 09:26:19 +01004475/* Forward a response generated by HAProxy (error/redirect/return). This
4476 * function forwards all pending incoming data. If <final> is set to 0, nothing
4477 * more is performed. It is used for 1xx informational messages. Otherwise, the
Christopher Faulet507479b2020-05-15 12:29:46 +02004478 * transaction is terminated and the request is emptied. On success 1 is
Christopher Faulet40e6b552020-06-25 16:04:50 +02004479 * returned. If an error occurred, 0 is returned. If it fails, this function
4480 * only exits. It is the caller responsibility to do the cleanup.
Christopher Fauletef70e252020-01-28 09:26:19 +01004481 */
4482int http_forward_proxy_resp(struct stream *s, int final)
4483{
4484 struct channel *req = &s->req;
4485 struct channel *res = &s->res;
4486 struct htx *htx = htxbuf(&res->buf);
4487 size_t data;
4488
4489 if (final) {
4490 htx->flags |= HTX_FL_PROXY_RESP;
Christopher Faulet507479b2020-05-15 12:29:46 +02004491
Christopher Fauletaab1b672020-11-18 16:44:02 +01004492 if (!htx_is_empty(htx) && !http_eval_after_res_rules(s))
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01004493 return 0;
Christopher Fauletef70e252020-01-28 09:26:19 +01004494
Christopher Fauletd6c48362020-10-19 18:01:38 +02004495 if (s->txn->meth == HTTP_METH_HEAD)
4496 htx_skip_msg_payload(htx);
4497
Christopher Fauletef70e252020-01-28 09:26:19 +01004498 channel_auto_read(req);
4499 channel_abort(req);
4500 channel_auto_close(req);
4501 channel_htx_erase(req, htxbuf(&req->buf));
4502
4503 res->wex = tick_add_ifset(now_ms, res->wto);
4504 channel_auto_read(res);
4505 channel_auto_close(res);
4506 channel_shutr_now(res);
Christopher Faulet1a9db7c2020-06-25 15:36:45 +02004507 res->flags |= CF_EOI; /* The response is terminated, add EOI */
Christopher Faulet42432f32020-11-20 17:43:16 +01004508 htxbuf(&res->buf)->flags |= HTX_FL_EOM; /* no more data are expected */
Christopher Fauletef70e252020-01-28 09:26:19 +01004509 }
Christopher Fauletcf6898c2020-06-25 15:55:11 +02004510 else {
4511 /* Send ASAP informational messages. Rely on CF_EOI for final
4512 * response.
4513 */
4514 res->flags |= CF_SEND_DONTWAIT;
4515 }
Christopher Fauletef70e252020-01-28 09:26:19 +01004516
4517 data = htx->data - co_data(res);
4518 c_adv(res, data);
4519 htx->first = -1;
4520 res->total += data;
4521 return 1;
4522}
4523
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004524void http_server_error(struct stream *s, struct stream_interface *si, int err,
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004525 int finst, struct http_reply *msg)
Christopher Faulet0f226952018-10-22 09:29:56 +02004526{
Christopher Faulet72c7d8d2020-01-27 15:32:25 +01004527 http_reply_and_close(s, s->txn->status, msg);
Christopher Faulet0f226952018-10-22 09:29:56 +02004528 if (!(s->flags & SF_ERR_MASK))
4529 s->flags |= err;
4530 if (!(s->flags & SF_FINST_MASK))
4531 s->flags |= finst;
4532}
4533
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004534void http_reply_and_close(struct stream *s, short status, struct http_reply *msg)
Christopher Faulet0f226952018-10-22 09:29:56 +02004535{
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004536 if (!msg) {
4537 channel_htx_truncate(&s->res, htxbuf(&s->res.buf));
4538 goto end;
4539 }
4540
4541 if (http_reply_message(s, msg) == -1) {
4542 /* On error, return a 500 error message, but don't rewrite it if
Christopher Faulet40e6b552020-06-25 16:04:50 +02004543 * it is already an internal error. If it was already a "const"
4544 * 500 error, just fail.
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004545 */
Christopher Faulet40e6b552020-06-25 16:04:50 +02004546 if (s->txn->status == 500) {
4547 if (s->txn->flags & TX_CONST_REPLY)
4548 goto end;
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004549 s->txn->flags |= TX_CONST_REPLY;
Christopher Faulet40e6b552020-06-25 16:04:50 +02004550 }
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004551 s->txn->status = 500;
4552 s->txn->http_reply = NULL;
4553 return http_reply_and_close(s, s->txn->status, http_error_message(s));
4554 }
4555
4556end:
4557 s->res.wex = tick_add_ifset(now_ms, s->res.wto);
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004558
Christopher Faulet0f226952018-10-22 09:29:56 +02004559 channel_auto_read(&s->req);
4560 channel_abort(&s->req);
4561 channel_auto_close(&s->req);
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004562 channel_htx_erase(&s->req, htxbuf(&s->req.buf));
Christopher Faulet72c7d8d2020-01-27 15:32:25 +01004563 channel_auto_read(&s->res);
4564 channel_auto_close(&s->res);
4565 channel_shutr_now(&s->res);
Christopher Faulet0f226952018-10-22 09:29:56 +02004566}
4567
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004568struct http_reply *http_error_message(struct stream *s)
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004569{
4570 const int msgnum = http_get_status_idx(s->txn->status);
4571
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004572 if (s->txn->http_reply)
4573 return s->txn->http_reply;
4574 else if (s->be->replies[msgnum])
4575 return s->be->replies[msgnum];
4576 else if (strm_fe(s)->replies[msgnum])
4577 return strm_fe(s)->replies[msgnum];
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004578 else
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004579 return &http_err_replies[msgnum];
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004580}
4581
Christopher Faulet40e6b552020-06-25 16:04:50 +02004582/* Produces an HTX message from an http reply. Depending on the http reply type,
4583 * a, errorfile, an raw file or a log-format string is used. On success, it
4584 * returns 0. If an error occurs -1 is returned. If it fails, this function only
4585 * exits. It is the caller responsibility to do the cleanup.
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004586 */
Christopher Fauletae43b6c2020-05-27 15:24:22 +02004587int http_reply_to_htx(struct stream *s, struct htx *htx, struct http_reply *reply)
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004588{
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004589 struct buffer *errmsg;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004590 struct htx_sl *sl;
4591 struct buffer *body = NULL;
4592 const char *status, *reason, *clen, *ctype;
4593 unsigned int slflags;
4594 int ret = 0;
4595
Christopher Faulete29a97e2020-05-14 14:49:25 +02004596 /*
4597 * - HTTP_REPLY_ERRFILES unexpected here. handled as no payload if so
4598 *
4599 * - HTTP_REPLY_INDIRECT: switch on another reply if defined or handled
4600 * as no payload if NULL. the TXN status code is set with the status
4601 * of the original reply.
4602 */
4603
4604 if (reply->type == HTTP_REPLY_INDIRECT) {
4605 if (reply->body.reply)
4606 reply = reply->body.reply;
4607 }
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004608 if (reply->type == HTTP_REPLY_ERRMSG && !reply->body.errmsg) {
4609 /* get default error message */
4610 if (reply == s->txn->http_reply)
4611 s->txn->http_reply = NULL;
4612 reply = http_error_message(s);
4613 if (reply->type == HTTP_REPLY_INDIRECT) {
4614 if (reply->body.reply)
4615 reply = reply->body.reply;
4616 }
4617 }
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004618
4619 if (reply->type == HTTP_REPLY_ERRMSG) {
4620 /* implicit or explicit error message*/
4621 errmsg = reply->body.errmsg;
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004622 if (errmsg && !b_is_null(errmsg)) {
Christopher Faulet20567362020-05-15 14:52:49 +02004623 if (!htx_copy_msg(htx, errmsg))
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004624 goto fail;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004625 }
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004626 }
4627 else {
4628 /* no payload, file or log-format string */
4629 if (reply->type == HTTP_REPLY_RAW) {
4630 /* file */
4631 body = &reply->body.obj;
4632 }
4633 else if (reply->type == HTTP_REPLY_LOGFMT) {
4634 /* log-format string */
4635 body = alloc_trash_chunk();
4636 if (!body)
4637 goto fail_alloc;
4638 body->data = build_logline(s, body->area, body->size, &reply->body.fmt);
4639 }
4640 /* else no payload */
4641
4642 status = ultoa(reply->status);
4643 reason = http_get_reason(reply->status);
4644 slflags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_CLEN);
4645 if (!body || !b_data(body))
4646 slflags |= HTX_SL_F_BODYLESS;
4647 sl = htx_add_stline(htx, HTX_BLK_RES_SL, slflags, ist("HTTP/1.1"), ist(status), ist(reason));
4648 if (!sl)
4649 goto fail;
4650 sl->info.res.status = reply->status;
4651
4652 clen = (body ? ultoa(b_data(body)) : "0");
4653 ctype = reply->ctype;
4654
4655 if (!LIST_ISEMPTY(&reply->hdrs)) {
4656 struct http_reply_hdr *hdr;
4657 struct buffer *value = alloc_trash_chunk();
4658
4659 if (!value)
4660 goto fail;
4661
4662 list_for_each_entry(hdr, &reply->hdrs, list) {
4663 chunk_reset(value);
4664 value->data = build_logline(s, value->area, value->size, &hdr->value);
4665 if (b_data(value) && !htx_add_header(htx, hdr->name, ist2(b_head(value), b_data(value)))) {
4666 free_trash_chunk(value);
4667 goto fail;
4668 }
4669 chunk_reset(value);
4670 }
4671 free_trash_chunk(value);
4672 }
4673
4674 if (!htx_add_header(htx, ist("content-length"), ist(clen)) ||
4675 (body && b_data(body) && ctype && !htx_add_header(htx, ist("content-type"), ist(ctype))) ||
4676 !htx_add_endof(htx, HTX_BLK_EOH) ||
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004677 (body && b_data(body) && !htx_add_data_atonce(htx, ist2(b_head(body), b_data(body)))))
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004678 goto fail;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004679
4680 htx->flags |= HTX_FL_EOM;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004681 }
4682
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004683 leave:
4684 if (reply->type == HTTP_REPLY_LOGFMT)
4685 free_trash_chunk(body);
4686 return ret;
4687
4688 fail_alloc:
4689 if (!(s->flags & SF_ERR_MASK))
4690 s->flags |= SF_ERR_RESOURCE;
Christopher Faulet97e466c2020-05-15 15:12:47 +02004691 /* fall through */
4692 fail:
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004693 ret = -1;
4694 goto leave;
Christopher Faulet97e466c2020-05-15 15:12:47 +02004695}
4696
4697/* Send an http reply to the client. On success, it returns 0. If an error
Christopher Faulet40e6b552020-06-25 16:04:50 +02004698 * occurs -1 is returned and the response channel is truncated, removing this
4699 * way the faulty reply. This function may fail when the reply is formatted
4700 * (http_reply_to_htx) or when the reply is forwarded
4701 * (http_forward_proxy_resp). On the last case, it is because a
4702 * http-after-response rule fails.
Christopher Faulet97e466c2020-05-15 15:12:47 +02004703 */
4704int http_reply_message(struct stream *s, struct http_reply *reply)
4705{
4706 struct channel *res = &s->res;
4707 struct htx *htx = htx_from_buf(&res->buf);
4708
4709 if (s->txn->status == -1)
4710 s->txn->status = reply->status;
4711 channel_htx_truncate(res, htx);
4712
4713 if (http_reply_to_htx(s, htx, reply) == -1)
4714 goto fail;
4715
4716 htx_to_buf(htx, &s->res.buf);
4717 if (!http_forward_proxy_resp(s, 1))
4718 goto fail;
4719 return 0;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004720
4721 fail:
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004722 channel_htx_truncate(res, htx);
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004723 if (!(s->flags & SF_ERR_MASK))
4724 s->flags |= SF_ERR_PRXCOND;
Christopher Faulet97e466c2020-05-15 15:12:47 +02004725 return -1;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004726}
4727
Christopher Faulet304cc402019-07-15 15:46:28 +02004728/* Return the error message corresponding to si->err_type. It is assumed
4729 * that the server side is closed. Note that err_type is actually a
4730 * bitmask, where almost only aborts may be cumulated with other
4731 * values. We consider that aborted operations are more important
4732 * than timeouts or errors due to the fact that nobody else in the
4733 * logs might explain incomplete retries. All others should avoid
4734 * being cumulated. It should normally not be possible to have multiple
4735 * aborts at once, but just in case, the first one in sequence is reported.
4736 * Note that connection errors appearing on the second request of a keep-alive
4737 * connection are not reported since this allows the client to retry.
4738 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004739void http_return_srv_error(struct stream *s, struct stream_interface *si)
Christopher Faulet304cc402019-07-15 15:46:28 +02004740{
4741 int err_type = si->err_type;
4742
4743 /* set s->txn->status for http_error_message(s) */
4744 s->txn->status = 503;
4745
4746 if (err_type & SI_ET_QUEUE_ABRT)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004747 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_Q,
4748 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004749 else if (err_type & SI_ET_CONN_ABRT)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004750 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_C,
4751 (s->txn->flags & TX_NOT_FIRST) ? NULL :
4752 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004753 else if (err_type & SI_ET_QUEUE_TO)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004754 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_Q,
4755 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004756 else if (err_type & SI_ET_QUEUE_ERR)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004757 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_Q,
4758 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004759 else if (err_type & SI_ET_CONN_TO)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004760 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_C,
4761 (s->txn->flags & TX_NOT_FIRST) ? NULL :
4762 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004763 else if (err_type & SI_ET_CONN_ERR)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004764 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_C,
4765 (s->flags & SF_SRV_REUSED) ? NULL :
4766 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004767 else if (err_type & SI_ET_CONN_RES)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004768 http_server_error(s, si, SF_ERR_RESOURCE, SF_FINST_C,
4769 (s->txn->flags & TX_NOT_FIRST) ? NULL :
4770 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004771 else { /* SI_ET_CONN_OTHER and others */
4772 s->txn->status = 500;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004773 http_server_error(s, si, SF_ERR_INTERNAL, SF_FINST_C,
4774 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004775 }
4776}
4777
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004778
Christopher Faulet4a28a532019-03-01 11:19:40 +01004779/* Handle Expect: 100-continue for HTTP/1.1 messages if necessary. It returns 0
4780 * on success and -1 on error.
4781 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004782static int http_handle_expect_hdr(struct stream *s, struct htx *htx, struct http_msg *msg)
Christopher Faulet4a28a532019-03-01 11:19:40 +01004783{
4784 /* If we have HTTP/1.1 message with a body and Expect: 100-continue,
4785 * then we must send an HTTP/1.1 100 Continue intermediate response.
4786 */
4787 if (msg->msg_state == HTTP_MSG_BODY && (msg->flags & HTTP_MSGF_VER_11) &&
4788 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
4789 struct ist hdr = { .ptr = "Expect", .len = 6 };
4790 struct http_hdr_ctx ctx;
4791
4792 ctx.blk = NULL;
4793 /* Expect is allowed in 1.1, look for it */
4794 if (http_find_header(htx, hdr, &ctx, 0) &&
4795 unlikely(isteqi(ctx.value, ist2("100-continue", 12)))) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004796 if (http_reply_100_continue(s) == -1)
Christopher Faulet4a28a532019-03-01 11:19:40 +01004797 return -1;
4798 http_remove_header(htx, &ctx);
4799 }
4800 }
4801 return 0;
4802}
4803
Christopher Faulet23a3c792018-11-28 10:01:23 +01004804/* Send a 100-Continue response to the client. It returns 0 on success and -1
4805 * on error. The response channel is updated accordingly.
4806 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004807static int http_reply_100_continue(struct stream *s)
Christopher Faulet23a3c792018-11-28 10:01:23 +01004808{
4809 struct channel *res = &s->res;
4810 struct htx *htx = htx_from_buf(&res->buf);
4811 struct htx_sl *sl;
4812 unsigned int flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|
4813 HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
Christopher Faulet23a3c792018-11-28 10:01:23 +01004814
4815 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
4816 ist("HTTP/1.1"), ist("100"), ist("Continue"));
4817 if (!sl)
4818 goto fail;
4819 sl->info.res.status = 100;
4820
Christopher Faulet1d5ec092019-06-26 14:23:54 +02004821 if (!htx_add_endof(htx, HTX_BLK_EOH))
Christopher Faulet23a3c792018-11-28 10:01:23 +01004822 goto fail;
4823
Christopher Fauleta72a7e42020-01-28 09:28:11 +01004824 if (!http_forward_proxy_resp(s, 0))
4825 goto fail;
Christopher Faulet23a3c792018-11-28 10:01:23 +01004826 return 0;
4827
4828 fail:
4829 /* If an error occurred, remove the incomplete HTTP response from the
4830 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004831 channel_htx_truncate(res, htx);
Christopher Faulet23a3c792018-11-28 10:01:23 +01004832 return -1;
4833}
4834
Christopher Faulet12c51e22018-11-28 15:59:42 +01004835
Christopher Faulet0f226952018-10-22 09:29:56 +02004836/*
4837 * Capture headers from message <htx> according to header list <cap_hdr>, and
4838 * fill the <cap> pointers appropriately.
4839 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004840static void http_capture_headers(struct htx *htx, char **cap, struct cap_hdr *cap_hdr)
Christopher Faulet0f226952018-10-22 09:29:56 +02004841{
4842 struct cap_hdr *h;
4843 int32_t pos;
4844
Christopher Fauleta3f15502019-05-13 15:27:23 +02004845 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet0f226952018-10-22 09:29:56 +02004846 struct htx_blk *blk = htx_get_blk(htx, pos);
4847 enum htx_blk_type type = htx_get_blk_type(blk);
4848 struct ist n, v;
4849
4850 if (type == HTX_BLK_EOH)
4851 break;
4852 if (type != HTX_BLK_HDR)
4853 continue;
4854
4855 n = htx_get_blk_name(htx, blk);
4856
4857 for (h = cap_hdr; h; h = h->next) {
4858 if (h->namelen && (h->namelen == n.len) &&
4859 (strncasecmp(n.ptr, h->name, h->namelen) == 0)) {
4860 if (cap[h->index] == NULL)
4861 cap[h->index] =
4862 pool_alloc(h->pool);
4863
4864 if (cap[h->index] == NULL) {
4865 ha_alert("HTTP capture : out of memory.\n");
4866 break;
4867 }
4868
4869 v = htx_get_blk_value(htx, blk);
4870 if (v.len > h->len)
4871 v.len = h->len;
4872
4873 memcpy(cap[h->index], v.ptr, v.len);
4874 cap[h->index][v.len]=0;
4875 }
4876 }
4877 }
4878}
4879
Christopher Faulet0b6bdc52018-10-24 11:05:36 +02004880/* Delete a value in a header between delimiters <from> and <next>. The header
4881 * itself is delimited by <start> and <end> pointers. The number of characters
4882 * displaced is returned, and the pointer to the first delimiter is updated if
4883 * required. The function tries as much as possible to respect the following
4884 * principles :
4885 * - replace <from> delimiter by the <next> one unless <from> points to <start>,
4886 * in which case <next> is simply removed
4887 * - set exactly one space character after the new first delimiter, unless there
4888 * are not enough characters in the block being moved to do so.
4889 * - remove unneeded spaces before the previous delimiter and after the new
4890 * one.
4891 *
4892 * It is the caller's responsibility to ensure that :
4893 * - <from> points to a valid delimiter or <start> ;
4894 * - <next> points to a valid delimiter or <end> ;
4895 * - there are non-space chars before <from>.
4896 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004897static int http_del_hdr_value(char *start, char *end, char **from, char *next)
Christopher Faulet0b6bdc52018-10-24 11:05:36 +02004898{
4899 char *prev = *from;
4900
4901 if (prev == start) {
4902 /* We're removing the first value. eat the semicolon, if <next>
4903 * is lower than <end> */
4904 if (next < end)
4905 next++;
4906
4907 while (next < end && HTTP_IS_SPHT(*next))
4908 next++;
4909 }
4910 else {
4911 /* Remove useless spaces before the old delimiter. */
4912 while (HTTP_IS_SPHT(*(prev-1)))
4913 prev--;
4914 *from = prev;
4915
4916 /* copy the delimiter and if possible a space if we're
4917 * not at the end of the line.
4918 */
4919 if (next < end) {
4920 *prev++ = *next++;
4921 if (prev + 1 < next)
4922 *prev++ = ' ';
4923 while (next < end && HTTP_IS_SPHT(*next))
4924 next++;
4925 }
4926 }
4927 memmove(prev, next, end - next);
4928 return (prev - next);
4929}
4930
Christopher Faulet0f226952018-10-22 09:29:56 +02004931
4932/* Formats the start line of the request (without CRLF) and puts it in <str> and
Joseph Herlantc42c0e92018-11-25 10:43:27 -08004933 * return the written length. The line can be truncated if it exceeds <len>.
Christopher Faulet0f226952018-10-22 09:29:56 +02004934 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004935static size_t http_fmt_req_line(const struct htx_sl *sl, char *str, size_t len)
Christopher Faulet0f226952018-10-22 09:29:56 +02004936{
4937 struct ist dst = ist2(str, 0);
4938
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004939 if (istcat(&dst, htx_sl_req_meth(sl), len) == -1)
Christopher Faulet0f226952018-10-22 09:29:56 +02004940 goto end;
4941 if (dst.len + 1 > len)
4942 goto end;
4943 dst.ptr[dst.len++] = ' ';
4944
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004945 if (istcat(&dst, htx_sl_req_uri(sl), len) == -1)
Christopher Faulet0f226952018-10-22 09:29:56 +02004946 goto end;
4947 if (dst.len + 1 > len)
4948 goto end;
4949 dst.ptr[dst.len++] = ' ';
4950
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004951 istcat(&dst, htx_sl_req_vsn(sl), len);
Christopher Faulet0f226952018-10-22 09:29:56 +02004952 end:
4953 return dst.len;
4954}
4955
4956/*
4957 * Print a debug line with a start line.
4958 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004959static void http_debug_stline(const char *dir, struct stream *s, const struct htx_sl *sl)
Christopher Faulet0f226952018-10-22 09:29:56 +02004960{
4961 struct session *sess = strm_sess(s);
4962 int max;
4963
4964 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
4965 dir,
4966 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->handle.fd : -1,
4967 objt_cs(s->si[1].end) ? (unsigned short)objt_cs(s->si[1].end)->conn->handle.fd : -1);
4968
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004969 max = HTX_SL_P1_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02004970 UBOUND(max, trash.size - trash.data - 3);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004971 chunk_memcat(&trash, HTX_SL_P1_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02004972 trash.area[trash.data++] = ' ';
4973
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004974 max = HTX_SL_P2_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02004975 UBOUND(max, trash.size - trash.data - 2);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004976 chunk_memcat(&trash, HTX_SL_P2_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02004977 trash.area[trash.data++] = ' ';
4978
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004979 max = HTX_SL_P3_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02004980 UBOUND(max, trash.size - trash.data - 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004981 chunk_memcat(&trash, HTX_SL_P3_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02004982 trash.area[trash.data++] = '\n';
4983
Willy Tarreau2e8ab6b2020-03-14 11:03:20 +01004984 DISGUISE(write(1, trash.area, trash.data));
Christopher Faulet0f226952018-10-22 09:29:56 +02004985}
4986
4987/*
4988 * Print a debug line with a header.
4989 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004990static 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 +02004991{
4992 struct session *sess = strm_sess(s);
4993 int max;
4994
4995 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
4996 dir,
4997 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->handle.fd : -1,
4998 objt_cs(s->si[1].end) ? (unsigned short)objt_cs(s->si[1].end)->conn->handle.fd : -1);
4999
5000 max = n.len;
5001 UBOUND(max, trash.size - trash.data - 3);
5002 chunk_memcat(&trash, n.ptr, max);
5003 trash.area[trash.data++] = ':';
5004 trash.area[trash.data++] = ' ';
5005
5006 max = v.len;
5007 UBOUND(max, trash.size - trash.data - 1);
5008 chunk_memcat(&trash, v.ptr, max);
5009 trash.area[trash.data++] = '\n';
5010
Willy Tarreau2e8ab6b2020-03-14 11:03:20 +01005011 DISGUISE(write(1, trash.area, trash.data));
Christopher Faulet0f226952018-10-22 09:29:56 +02005012}
5013
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005014/* Allocate a new HTTP transaction for stream <s> unless there is one already.
5015 * In case of allocation failure, everything allocated is freed and NULL is
5016 * returned. Otherwise the new transaction is assigned to the stream and
5017 * returned.
5018 */
5019struct http_txn *http_alloc_txn(struct stream *s)
5020{
5021 struct http_txn *txn = s->txn;
5022
5023 if (txn)
5024 return txn;
5025
5026 txn = pool_alloc(pool_head_http_txn);
5027 if (!txn)
5028 return txn;
5029
5030 s->txn = txn;
5031 return txn;
5032}
5033
5034void http_txn_reset_req(struct http_txn *txn)
5035{
Christopher Faulet1aea50e2020-01-17 16:03:53 +01005036 txn->req.flags = 0;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005037 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
5038}
5039
5040void http_txn_reset_res(struct http_txn *txn)
5041{
Christopher Faulet1aea50e2020-01-17 16:03:53 +01005042 txn->rsp.flags = 0;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005043 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
5044}
5045
5046/*
Christopher Faulet75f619a2021-03-08 19:12:58 +01005047 * Create and initialize a new HTTP transaction for stream <s>. This should be
5048 * used before processing any new request. It returns the transaction or NLULL
5049 * on error.
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005050 */
Christopher Faulet75f619a2021-03-08 19:12:58 +01005051struct http_txn *http_create_txn(struct stream *s)
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005052{
Christopher Faulet75f619a2021-03-08 19:12:58 +01005053 struct http_txn *txn;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005054 struct conn_stream *cs = objt_cs(s->si[0].end);
5055
Christopher Faulet75f619a2021-03-08 19:12:58 +01005056 txn = pool_alloc(pool_head_http_txn);
5057 if (!txn)
5058 return NULL;
5059 s->txn = txn;
5060
Christopher Fauletda831fa2020-10-06 17:58:43 +02005061 txn->flags = ((cs && cs->flags & CS_FL_NOT_FIRST) ? TX_NOT_FIRST : 0);
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005062 txn->status = -1;
Christopher Faulet5cb513a2020-05-13 17:56:56 +02005063 txn->http_reply = NULL;
Willy Tarreau8b507582020-02-25 09:35:07 +01005064 write_u32(txn->cache_hash, 0);
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005065
5066 txn->cookie_first_date = 0;
5067 txn->cookie_last_date = 0;
5068
5069 txn->srv_cookie = NULL;
5070 txn->cli_cookie = NULL;
5071 txn->uri = NULL;
5072
5073 http_txn_reset_req(txn);
5074 http_txn_reset_res(txn);
5075
5076 txn->req.chn = &s->req;
5077 txn->rsp.chn = &s->res;
5078
5079 txn->auth.method = HTTP_AUTH_UNKNOWN;
5080
5081 vars_init(&s->vars_txn, SCOPE_TXN);
5082 vars_init(&s->vars_reqres, SCOPE_REQ);
Christopher Faulet75f619a2021-03-08 19:12:58 +01005083
5084 return txn;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005085}
5086
5087/* to be used at the end of a transaction */
Christopher Faulet75f619a2021-03-08 19:12:58 +01005088void http_destroy_txn(struct stream *s)
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005089{
5090 struct http_txn *txn = s->txn;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005091
5092 /* these ones will have been dynamically allocated */
5093 pool_free(pool_head_requri, txn->uri);
5094 pool_free(pool_head_capture, txn->cli_cookie);
5095 pool_free(pool_head_capture, txn->srv_cookie);
Tim Duesterhusa17e6622020-03-05 20:19:02 +01005096 pool_free(pool_head_uniqueid, s->unique_id.ptr);
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005097
Tim Duesterhusa17e6622020-03-05 20:19:02 +01005098 s->unique_id = IST_NULL;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005099 txn->uri = NULL;
5100 txn->srv_cookie = NULL;
5101 txn->cli_cookie = NULL;
5102
Christopher Faulet59399252019-11-07 14:27:52 +01005103 if (!LIST_ISEMPTY(&s->vars_txn.head))
5104 vars_prune(&s->vars_txn, s->sess, s);
5105 if (!LIST_ISEMPTY(&s->vars_reqres.head))
5106 vars_prune(&s->vars_reqres, s->sess, s);
Christopher Faulet75f619a2021-03-08 19:12:58 +01005107
5108 pool_free(pool_head_http_txn, txn);
5109 s->txn = NULL;
Christopher Faulet59399252019-11-07 14:27:52 +01005110}
5111
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005112
5113DECLARE_POOL(pool_head_http_txn, "http_txn", sizeof(struct http_txn));
Christopher Faulet0f226952018-10-22 09:29:56 +02005114
Christopher Fauletf4eb75d2018-10-11 15:55:07 +02005115__attribute__((constructor))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005116static void __http_protocol_init(void)
Christopher Fauletf4eb75d2018-10-11 15:55:07 +02005117{
5118}
5119
5120
5121/*
5122 * Local variables:
5123 * c-indent-level: 8
5124 * c-basic-offset: 8
5125 * End:
5126 */