blob: 5536c0402f9a3515afd1f42c115ce4f422aa38a1 [file] [log] [blame]
Christopher Fauletf4eb75d2018-10-11 15:55:07 +02001/*
2 * HTTP protocol analyzer
3 *
4 * Copyright (C) 2018 HAProxy Technologies, Christopher Faulet <cfaulet@haproxy.com>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
Willy Tarreaudcc048a2020-06-04 19:11:43 +020013#include <haproxy/acl.h>
Willy Tarreau122eba92020-06-04 10:15:32 +020014#include <haproxy/action-t.h>
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020015#include <haproxy/api.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020016#include <haproxy/backend.h>
Willy Tarreau8d366972020-05-27 16:10:29 +020017#include <haproxy/base64.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020018#include <haproxy/capture-t.h>
Willy Tarreauf1d32c42020-06-04 21:07:02 +020019#include <haproxy/channel.h>
Willy Tarreau4aa573d2020-06-04 18:21:56 +020020#include <haproxy/check.h>
Willy Tarreau7ea393d2020-06-04 18:02:10 +020021#include <haproxy/connection.h>
Willy Tarreau36979d92020-06-05 17:27:29 +020022#include <haproxy/errors.h>
Willy Tarreauc7babd82020-06-04 21:29:29 +020023#include <haproxy/filters.h>
Willy Tarreaucd72d8c2020-06-02 19:11:26 +020024#include <haproxy/http.h>
Willy Tarreauc2b1ff02020-06-04 21:21:03 +020025#include <haproxy/http_ana.h>
Willy Tarreau87735332020-06-04 09:08:41 +020026#include <haproxy/http_htx.h>
Willy Tarreau16f958c2020-06-03 08:44:35 +020027#include <haproxy/htx.h>
Willy Tarreauaeed4a82020-06-04 22:01:04 +020028#include <haproxy/log.h>
Willy Tarreau6131d6a2020-06-02 16:48:09 +020029#include <haproxy/net_helper.h>
Willy Tarreaua264d962020-06-04 22:29:18 +020030#include <haproxy/proxy.h>
Willy Tarreau7cd8b6e2020-06-02 17:32:26 +020031#include <haproxy/regex.h>
Willy Tarreau1e56f922020-06-04 23:20:13 +020032#include <haproxy/server-t.h>
Willy Tarreau2eec9b52020-06-04 19:58:55 +020033#include <haproxy/stats.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020034#include <haproxy/stream.h>
Willy Tarreau5e539c92020-06-04 20:45:39 +020035#include <haproxy/stream_interface.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020036#include <haproxy/trace.h>
Willy Tarreau8c42b8a2020-06-04 19:27:34 +020037#include <haproxy/uri_auth-t.h>
Willy Tarreaua1718922020-06-04 16:25:31 +020038#include <haproxy/vars.h>
Christopher Faulete0768eb2018-10-03 16:38:02 +020039
Christopher Faulete0768eb2018-10-03 16:38:02 +020040
Christopher Fauleteea8fc72019-11-05 16:18:10 +010041#define TRACE_SOURCE &trace_strm
42
Christopher Faulet377c5a52018-10-24 21:21:30 +020043extern const char *stat_status_codes[];
Christopher Fauletf2824e62018-10-01 12:12:37 +020044
Christopher Fauleta8a46e22019-07-16 14:53:09 +020045struct pool_head *pool_head_requri = NULL;
46struct pool_head *pool_head_capture = NULL;
47
48
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020049static void http_end_request(struct stream *s);
50static void http_end_response(struct stream *s);
Christopher Fauletf2824e62018-10-01 12:12:37 +020051
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020052static void http_capture_headers(struct htx *htx, char **cap, struct cap_hdr *cap_hdr);
53static int http_del_hdr_value(char *start, char *end, char **from, char *next);
54static size_t http_fmt_req_line(const struct htx_sl *sl, char *str, size_t len);
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020055static void http_debug_stline(const char *dir, struct stream *s, const struct htx_sl *sl);
56static void http_debug_hdr(const char *dir, struct stream *s, const struct ist n, const struct ist v);
Christopher Faulet0f226952018-10-22 09:29:56 +020057
Christopher Fauletb58f62b2020-01-13 16:40:13 +010058static enum rule_result http_req_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s);
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020059static enum rule_result http_res_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s);
Christopher Faulet3e964192018-10-24 11:39:23 +020060
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020061static void http_manage_client_side_cookies(struct stream *s, struct channel *req);
62static void http_manage_server_side_cookies(struct stream *s, struct channel *res);
Christopher Fauletfcda7c62018-10-24 11:56:22 +020063
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020064static int http_stats_check_uri(struct stream *s, struct http_txn *txn, struct proxy *backend);
65static int http_handle_stats(struct stream *s, struct channel *req);
Christopher Faulet377c5a52018-10-24 21:21:30 +020066
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020067static int http_handle_expect_hdr(struct stream *s, struct htx *htx, struct http_msg *msg);
68static int http_reply_100_continue(struct stream *s);
Christopher Faulet23a3c792018-11-28 10:01:23 +010069
Christopher Faulete0768eb2018-10-03 16:38:02 +020070/* This stream analyser waits for a complete HTTP request. It returns 1 if the
71 * processing can continue on next analysers, or zero if it either needs more
72 * data or wants to immediately abort the request (eg: timeout, error, ...). It
73 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req.analysers
74 * when it has nothing left to do, and may remove any analyser when it wants to
75 * abort.
76 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020077int http_wait_for_request(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +020078{
Christopher Faulet9768c262018-10-22 09:34:31 +020079
Christopher Faulete0768eb2018-10-03 16:38:02 +020080 /*
Christopher Faulet9768c262018-10-22 09:34:31 +020081 * We will analyze a complete HTTP request to check the its syntax.
Christopher Faulete0768eb2018-10-03 16:38:02 +020082 *
Christopher Faulet9768c262018-10-22 09:34:31 +020083 * Once the start line and all headers are received, we may perform a
84 * capture of the error (if any), and we will set a few fields. We also
85 * check for monitor-uri, logging and finally headers capture.
Christopher Faulete0768eb2018-10-03 16:38:02 +020086 */
Christopher Faulete0768eb2018-10-03 16:38:02 +020087 struct session *sess = s->sess;
88 struct http_txn *txn = s->txn;
89 struct http_msg *msg = &txn->req;
Christopher Faulet9768c262018-10-22 09:34:31 +020090 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +010091 struct htx_sl *sl;
Christopher Faulete0768eb2018-10-03 16:38:02 +020092
Christopher Fauleteea8fc72019-11-05 16:18:10 +010093 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +020094
Christopher Fauletda46a0d2021-01-21 17:32:58 +010095 if (unlikely(!IS_HTX_STRM(s))) {
96 /* It is only possible when a TCP stream is upgrade to HTTP.
97 * There is a transition period during which there is no
98 * data. The stream is still in raw mode and SF_IGNORE flag is
99 * still set. When this happens, the new mux is responsible to
Ilya Shipitsinacf84592021-02-06 22:29:08 +0500100 * handle all errors. Thus we may leave immediately.
Christopher Fauletda46a0d2021-01-21 17:32:58 +0100101 */
102 BUG_ON(!(s->flags & SF_IGNORE) || !c_empty(&s->req));
Christopher Faulet9768c262018-10-22 09:34:31 +0200103
Christopher Fauletda46a0d2021-01-21 17:32:58 +0100104 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA, s);
105 return 0;
106 }
107
108 htx = htxbuf(&req->buf);
Christopher Faulet8bebd2f2020-10-06 17:54:56 +0200109
Willy Tarreau4236f032019-03-05 10:43:32 +0100110 /* Parsing errors are caught here */
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200111 if (htx->flags & (HTX_FL_PARSING_ERROR|HTX_FL_PROCESSING_ERROR)) {
Willy Tarreau4236f032019-03-05 10:43:32 +0100112 stream_inc_http_req_ctr(s);
Emeric Brun28976442020-10-07 08:50:09 +0200113 proxy_inc_fe_req_ctr(sess->listener, sess->fe);
Christopher Fauletbf7175f2021-02-10 14:58:01 +0100114 if (htx->flags & HTX_FL_PARSING_ERROR) {
115 stream_inc_http_err_ctr(s);
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200116 goto return_bad_req;
Christopher Fauletbf7175f2021-02-10 14:58:01 +0100117 }
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200118 else
119 goto return_int_err;
Willy Tarreau4236f032019-03-05 10:43:32 +0100120 }
121
Christopher Faulete0768eb2018-10-03 16:38:02 +0200122 /* we're speaking HTTP here, so let's speak HTTP to the client */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200123 s->srv_error = http_return_srv_error;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200124
Christopher Faulet9768c262018-10-22 09:34:31 +0200125 msg->msg_state = HTTP_MSG_BODY;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200126 stream_inc_http_req_ctr(s);
Emeric Brun28976442020-10-07 08:50:09 +0200127 proxy_inc_fe_req_ctr(sess->listener, sess->fe); /* one more valid request for this FE */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200128
Christopher Faulet9768c262018-10-22 09:34:31 +0200129 /* kill the pending keep-alive timeout */
Christopher Faulet9768c262018-10-22 09:34:31 +0200130 req->analyse_exp = TICK_ETERNITY;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200131
Christopher Faulet29f17582019-05-23 11:03:26 +0200132 BUG_ON(htx_get_first_type(htx) != HTX_BLK_REQ_SL);
Christopher Faulet297fbb42019-05-13 14:41:27 +0200133 sl = http_get_stline(htx);
Christopher Faulet03599112018-11-27 11:21:21 +0100134
Christopher Faulet9768c262018-10-22 09:34:31 +0200135 /* 0: we might have to print this header in debug mode */
136 if (unlikely((global.mode & MODE_DEBUG) &&
137 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
138 int32_t pos;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200139
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200140 http_debug_stline("clireq", s, sl);
Christopher Faulet9768c262018-10-22 09:34:31 +0200141
Christopher Fauleta3f15502019-05-13 15:27:23 +0200142 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet9768c262018-10-22 09:34:31 +0200143 struct htx_blk *blk = htx_get_blk(htx, pos);
144 enum htx_blk_type type = htx_get_blk_type(blk);
145
146 if (type == HTX_BLK_EOH)
147 break;
148 if (type != HTX_BLK_HDR)
149 continue;
150
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200151 http_debug_hdr("clihdr", s,
152 htx_get_blk_name(htx, blk),
153 htx_get_blk_value(htx, blk));
Christopher Faulet9768c262018-10-22 09:34:31 +0200154 }
155 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200156
157 /*
Christopher Faulet03599112018-11-27 11:21:21 +0100158 * 1: identify the method and the version. Also set HTTP flags
Christopher Faulete0768eb2018-10-03 16:38:02 +0200159 */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100160 txn->meth = sl->info.req.meth;
Christopher Faulet03599112018-11-27 11:21:21 +0100161 if (sl->flags & HTX_SL_F_VER_11)
Christopher Faulet9768c262018-10-22 09:34:31 +0200162 msg->flags |= HTTP_MSGF_VER_11;
Christopher Faulet03599112018-11-27 11:21:21 +0100163 msg->flags |= HTTP_MSGF_XFER_LEN;
Christopher Faulet2a408542020-11-20 14:22:37 +0100164 if (sl->flags & HTX_SL_F_CLEN)
165 msg->flags |= HTTP_MSGF_CNT_LEN;
166 else if (sl->flags & HTX_SL_F_CHNK)
167 msg->flags |= HTTP_MSGF_TE_CHNK;
Christopher Fauletb2db4fa2018-11-27 16:51:09 +0100168 if (sl->flags & HTX_SL_F_BODYLESS)
169 msg->flags |= HTTP_MSGF_BODYLESS;
Christopher Faulet576c3582021-01-08 15:53:01 +0100170 if (sl->flags & HTX_SL_F_CONN_UPG)
171 msg->flags |= HTTP_MSGF_CONN_UPG;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200172
173 /* we can make use of server redirect on GET and HEAD */
174 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
175 s->flags |= SF_REDIRECTABLE;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100176 else if (txn->meth == HTTP_METH_OTHER && isteqi(htx_sl_req_meth(sl), ist("PRI"))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200177 /* PRI is reserved for the HTTP/2 preface */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200178 goto return_bad_req;
179 }
180
181 /*
Christopher Faulet6072beb2020-02-18 15:34:58 +0100182 * 2: check if the URI matches the monitor_uri. We have to do this for
183 * every request which gets in, because the monitor-uri is defined by
184 * the frontend. If the monitor-uri starts with a '/', the matching is
185 * done against the request's path. Otherwise, the request's uri is
186 * used. It is a workaround to let HTTP/2 health-checks work as
187 * expected.
Christopher Faulete0768eb2018-10-03 16:38:02 +0200188 */
189 if (unlikely((sess->fe->monitor_uri_len != 0) &&
Christopher Faulet6072beb2020-02-18 15:34:58 +0100190 ((*sess->fe->monitor_uri == '/' && isteq(http_get_path(htx_sl_req_uri(sl)),
191 ist2(sess->fe->monitor_uri, sess->fe->monitor_uri_len))) ||
192 isteq(htx_sl_req_uri(sl), ist2(sess->fe->monitor_uri, sess->fe->monitor_uri_len))))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200193 /*
194 * We have found the monitor URI
195 */
196 struct acl_cond *cond;
197
198 s->flags |= SF_MONITOR;
Olivier Houcharda798bf52019-03-08 18:52:00 +0100199 _HA_ATOMIC_ADD(&sess->fe->fe_counters.intercepted_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200200
201 /* Check if we want to fail this monitor request or not */
202 list_for_each_entry(cond, &sess->fe->mon_fail_cond, list) {
203 int ret = acl_exec_cond(cond, sess->fe, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
204
205 ret = acl_pass(ret);
206 if (cond->pol == ACL_COND_UNLESS)
207 ret = !ret;
208
209 if (ret) {
210 /* we fail this request, let's return 503 service unavail */
211 txn->status = 503;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200212 if (!(s->flags & SF_ERR_MASK))
213 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
214 goto return_prx_cond;
215 }
216 }
217
Joseph Herlantc42c0e92018-11-25 10:43:27 -0800218 /* nothing to fail, let's reply normally */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200219 txn->status = 200;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200220 if (!(s->flags & SF_ERR_MASK))
221 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
222 goto return_prx_cond;
223 }
224
225 /*
226 * 3: Maybe we have to copy the original REQURI for the logs ?
227 * Note: we cannot log anymore if the request has been
228 * classified as invalid.
229 */
230 if (unlikely(s->logs.logwait & LW_REQ)) {
231 /* we have a complete HTTP request that we must log */
232 if ((txn->uri = pool_alloc(pool_head_requri)) != NULL) {
Christopher Faulet9768c262018-10-22 09:34:31 +0200233 size_t len;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200234
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200235 len = http_fmt_req_line(sl, txn->uri, global.tune.requri_len - 1);
Christopher Faulet9768c262018-10-22 09:34:31 +0200236 txn->uri[len] = 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200237
238 if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT)))
239 s->do_log(s);
240 } else {
241 ha_alert("HTTP logging : out of memory.\n");
242 }
243 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200244
Christopher Faulete0768eb2018-10-03 16:38:02 +0200245 /* if the frontend has "option http-use-proxy-header", we'll check if
246 * we have what looks like a proxied connection instead of a connection,
247 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
248 * Note that this is *not* RFC-compliant, however browsers and proxies
249 * happen to do that despite being non-standard :-(
250 * We consider that a request not beginning with either '/' or '*' is
251 * a proxied connection, which covers both "scheme://location" and
252 * CONNECT ip:port.
253 */
254 if ((sess->fe->options2 & PR_O2_USE_PXHDR) &&
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100255 *HTX_SL_REQ_UPTR(sl) != '/' && *HTX_SL_REQ_UPTR(sl) != '*')
Christopher Faulete0768eb2018-10-03 16:38:02 +0200256 txn->flags |= TX_USE_PX_CONN;
257
Christopher Faulete0768eb2018-10-03 16:38:02 +0200258 /* 5: we may need to capture headers */
259 if (unlikely((s->logs.logwait & LW_REQHDR) && s->req_cap))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200260 http_capture_headers(htx, s->req_cap, sess->fe->req_cap);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200261
Christopher Faulete0768eb2018-10-03 16:38:02 +0200262 /* we may have to wait for the request's body */
Christopher Faulet9768c262018-10-22 09:34:31 +0200263 if (s->be->options & PR_O_WREQ_BODY)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200264 req->analysers |= AN_REQ_HTTP_BODY;
265
266 /*
267 * RFC7234#4:
268 * A cache MUST write through requests with methods
269 * that are unsafe (Section 4.2.1 of [RFC7231]) to
270 * the origin server; i.e., a cache is not allowed
271 * to generate a reply to such a request before
272 * having forwarded the request and having received
273 * a corresponding response.
274 *
275 * RFC7231#4.2.1:
276 * Of the request methods defined by this
277 * specification, the GET, HEAD, OPTIONS, and TRACE
278 * methods are defined to be safe.
279 */
280 if (likely(txn->meth == HTTP_METH_GET ||
281 txn->meth == HTTP_METH_HEAD ||
282 txn->meth == HTTP_METH_OPTIONS ||
283 txn->meth == HTTP_METH_TRACE))
284 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
285
286 /* end of job, return OK */
287 req->analysers &= ~an_bit;
288 req->analyse_exp = TICK_ETERNITY;
Christopher Faulet9768c262018-10-22 09:34:31 +0200289
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100290 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200291 return 1;
292
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200293 return_int_err:
294 txn->status = 500;
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200295 if (!(s->flags & SF_ERR_MASK))
296 s->flags |= SF_ERR_INTERNAL;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100297 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200298 if (sess->listener->counters)
Christopher Fauletb8a53712019-12-16 11:29:38 +0100299 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200300 goto return_prx_cond;
301
Christopher Faulete0768eb2018-10-03 16:38:02 +0200302 return_bad_req:
Christopher Faulet9768c262018-10-22 09:34:31 +0200303 txn->status = 400;
Olivier Houcharda798bf52019-03-08 18:52:00 +0100304 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200305 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100306 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200307 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200308
309 return_prx_cond:
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200310 http_reply_and_close(s, txn->status, http_error_message(s));
311
Christopher Faulete0768eb2018-10-03 16:38:02 +0200312 if (!(s->flags & SF_ERR_MASK))
313 s->flags |= SF_ERR_PRXCOND;
314 if (!(s->flags & SF_FINST_MASK))
315 s->flags |= SF_FINST_R;
316
317 req->analysers &= AN_REQ_FLT_END;
318 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100319 DBG_TRACE_DEVEL("leaving on error",
320 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200321 return 0;
322}
323
324
325/* This stream analyser runs all HTTP request processing which is common to
326 * frontends and backends, which means blocking ACLs, filters, connection-close,
327 * reqadd, stats and redirects. This is performed for the designated proxy.
328 * It returns 1 if the processing can continue on next analysers, or zero if it
329 * either needs more data or wants to immediately abort the request (eg: deny,
330 * error, ...).
331 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200332int http_process_req_common(struct stream *s, struct channel *req, int an_bit, struct proxy *px)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200333{
334 struct session *sess = s->sess;
335 struct http_txn *txn = s->txn;
336 struct http_msg *msg = &txn->req;
Christopher Fauletff2759f2018-10-24 11:13:16 +0200337 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200338 struct redirect_rule *rule;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200339 enum rule_result verdict;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200340 struct connection *conn = objt_conn(sess->origin);
341
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100342 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200343
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100344 htx = htxbuf(&req->buf);
Christopher Fauletff2759f2018-10-24 11:13:16 +0200345
Christopher Faulet1907ccc2019-04-29 13:12:02 +0200346 /* just in case we have some per-backend tracking. Only called the first
347 * execution of the analyser. */
348 if (!s->current_rule || s->current_rule_list != &px->http_req_rules)
349 stream_inc_be_http_req_ctr(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200350
351 /* evaluate http-request rules */
352 if (!LIST_ISEMPTY(&px->http_req_rules)) {
Christopher Fauletb58f62b2020-01-13 16:40:13 +0100353 verdict = http_req_get_intercept_rule(px, &px->http_req_rules, s);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200354
355 switch (verdict) {
356 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
357 goto return_prx_yield;
358
359 case HTTP_RULE_RES_CONT:
360 case HTTP_RULE_RES_STOP: /* nothing to do */
361 break;
362
363 case HTTP_RULE_RES_DENY: /* deny or tarpit */
364 if (txn->flags & TX_CLTARPIT)
365 goto tarpit;
366 goto deny;
367
368 case HTTP_RULE_RES_ABRT: /* abort request, response already sent. Eg: auth */
369 goto return_prx_cond;
370
371 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
372 goto done;
373
374 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
375 goto return_bad_req;
Christopher Faulet3a26bee2019-12-16 12:47:40 +0100376
377 case HTTP_RULE_RES_ERROR: /* failed with a bad request */
378 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200379 }
380 }
381
382 if (conn && (conn->flags & CO_FL_EARLY_DATA) &&
Olivier Houchard220a26c2020-01-23 14:57:36 +0100383 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_SSL_WAIT_HS))) {
Christopher Fauletff2759f2018-10-24 11:13:16 +0200384 struct http_hdr_ctx ctx;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200385
Christopher Fauletff2759f2018-10-24 11:13:16 +0200386 ctx.blk = NULL;
387 if (!http_find_header(htx, ist("Early-Data"), &ctx, 0)) {
388 if (unlikely(!http_add_header(htx, ist("Early-Data"), ist("1"))))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100389 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200390 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200391 }
392
393 /* OK at this stage, we know that the request was accepted according to
394 * the http-request rules, we can check for the stats. Note that the
395 * URI is detected *before* the req* rules in order not to be affected
396 * by a possible reqrep, while they are processed *after* so that a
397 * reqdeny can still block them. This clearly needs to change in 1.6!
398 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200399 if (!s->target && http_stats_check_uri(s, txn, px)) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200400 s->target = &http_stats_applet.obj_type;
Willy Tarreau14bfe9a2018-12-19 15:19:27 +0100401 if (unlikely(!si_register_handler(&s->si[1], objt_applet(s->target)))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200402 s->logs.tv_request = now;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200403 if (!(s->flags & SF_ERR_MASK))
404 s->flags |= SF_ERR_RESOURCE;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100405 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200406 }
407
408 /* parse the whole stats request and extract the relevant information */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200409 http_handle_stats(s, req);
Christopher Fauletb58f62b2020-01-13 16:40:13 +0100410 verdict = http_req_get_intercept_rule(px, &px->uri_auth->http_req_rules, s);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200411 /* not all actions implemented: deny, allow, auth */
412
413 if (verdict == HTTP_RULE_RES_DENY) /* stats http-request deny */
414 goto deny;
415
416 if (verdict == HTTP_RULE_RES_ABRT) /* stats auth / stats http-request auth */
417 goto return_prx_cond;
Christopher Faulet3a26bee2019-12-16 12:47:40 +0100418
419 if (verdict == HTTP_RULE_RES_BADREQ) /* failed with a bad request */
420 goto return_bad_req;
421
422 if (verdict == HTTP_RULE_RES_ERROR) /* failed with a bad request */
423 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200424 }
425
Christopher Faulet2571bc62019-03-01 11:44:26 +0100426 /* Proceed with the applets now. */
427 if (unlikely(objt_applet(s->target))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200428 if (sess->fe == s->be) /* report it if the request was intercepted by the frontend */
Olivier Houcharda798bf52019-03-08 18:52:00 +0100429 _HA_ATOMIC_ADD(&sess->fe->fe_counters.intercepted_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200430
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200431 if (http_handle_expect_hdr(s, htx, msg) == -1)
Christopher Fauletb8a53712019-12-16 11:29:38 +0100432 goto return_int_err;
Christopher Fauletbcf242a2019-03-01 11:36:26 +0100433
Christopher Faulete0768eb2018-10-03 16:38:02 +0200434 if (!(s->flags & SF_ERR_MASK)) // this is not really an error but it is
435 s->flags |= SF_ERR_LOCAL; // to mark that it comes from the proxy
436 if (!(s->flags & SF_FINST_MASK))
437 s->flags |= SF_FINST_R;
438
439 /* enable the minimally required analyzers to handle keep-alive and compression on the HTTP response */
440 req->analysers &= (AN_REQ_HTTP_BODY | AN_REQ_FLT_HTTP_HDRS | AN_REQ_FLT_END);
441 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
442 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Christopher Fauletbcf242a2019-03-01 11:36:26 +0100443
444 req->flags |= CF_SEND_DONTWAIT;
445 s->flags |= SF_ASSIGNED;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200446 goto done;
447 }
448
449 /* check whether we have some ACLs set to redirect this request */
450 list_for_each_entry(rule, &px->redirect_rules, list) {
451 if (rule->cond) {
452 int ret;
453
454 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
455 ret = acl_pass(ret);
456 if (rule->cond->pol == ACL_COND_UNLESS)
457 ret = !ret;
458 if (!ret)
459 continue;
460 }
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200461 if (!http_apply_redirect_rule(rule, s, txn))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100462 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200463 goto done;
464 }
465
466 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
467 * If this happens, then the data will not come immediately, so we must
468 * send all what we have without waiting. Note that due to the small gain
469 * in waiting for the body of the request, it's easier to simply put the
470 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
471 * itself once used.
472 */
473 req->flags |= CF_SEND_DONTWAIT;
474
475 done: /* done with this analyser, continue with next ones that the calling
476 * points will have set, if any.
477 */
478 req->analyse_exp = TICK_ETERNITY;
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +0500479 done_without_exp: /* done with this analyser, but don't reset the analyse_exp. */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200480 req->analysers &= ~an_bit;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100481 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200482 return 1;
483
484 tarpit:
485 /* Allow cookie logging
486 */
487 if (s->be->cookie_name || sess->fe->capture_name)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200488 http_manage_client_side_cookies(s, req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200489
490 /* When a connection is tarpitted, we use the tarpit timeout,
491 * which may be the same as the connect timeout if unspecified.
492 * If unset, then set it to zero because we really want it to
493 * eventually expire. We build the tarpit as an analyser.
494 */
Christopher Faulet202c6ce2019-01-07 14:57:35 +0100495 channel_htx_erase(&s->req, htx);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200496
497 /* wipe the request out so that we can drop the connection early
498 * if the client closes first.
499 */
500 channel_dont_connect(req);
501
Christopher Faulete0768eb2018-10-03 16:38:02 +0200502 req->analysers &= AN_REQ_FLT_END; /* remove switching rules etc... */
503 req->analysers |= AN_REQ_HTTP_TARPIT;
504 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
505 if (!req->analyse_exp)
506 req->analyse_exp = tick_add(now_ms, 0);
507 stream_inc_http_err_ctr(s);
Olivier Houcharda798bf52019-03-08 18:52:00 +0100508 _HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100509 if (s->flags & SF_BE_ASSIGNED)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100510 _HA_ATOMIC_ADD(&s->be->be_counters.denied_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200511 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100512 _HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200513 goto done_without_exp;
514
515 deny: /* this request was blocked (denied) */
516
517 /* Allow cookie logging
518 */
519 if (s->be->cookie_name || sess->fe->capture_name)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200520 http_manage_client_side_cookies(s, req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200521
Christopher Faulete0768eb2018-10-03 16:38:02 +0200522 s->logs.tv_request = now;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200523 stream_inc_http_err_ctr(s);
Olivier Houcharda798bf52019-03-08 18:52:00 +0100524 _HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100525 if (s->flags & SF_BE_ASSIGNED)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100526 _HA_ATOMIC_ADD(&s->be->be_counters.denied_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200527 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100528 _HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100529 goto return_prx_err;
530
531 return_int_err:
532 txn->status = 500;
533 if (!(s->flags & SF_ERR_MASK))
534 s->flags |= SF_ERR_INTERNAL;
535 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100536 if (s->flags & SF_BE_ASSIGNED)
537 _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100538 if (sess->listener->counters)
539 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
540 goto return_prx_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200541
542 return_bad_req:
Christopher Faulete0768eb2018-10-03 16:38:02 +0200543 txn->status = 400;
Olivier Houcharda798bf52019-03-08 18:52:00 +0100544 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200545 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100546 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100547 /* fall through */
548
549 return_prx_err:
550 http_reply_and_close(s, txn->status, http_error_message(s));
551 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200552
553 return_prx_cond:
554 if (!(s->flags & SF_ERR_MASK))
555 s->flags |= SF_ERR_PRXCOND;
556 if (!(s->flags & SF_FINST_MASK))
557 s->flags |= SF_FINST_R;
558
559 req->analysers &= AN_REQ_FLT_END;
560 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100561 DBG_TRACE_DEVEL("leaving on error",
562 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200563 return 0;
564
565 return_prx_yield:
566 channel_dont_connect(req);
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100567 DBG_TRACE_DEVEL("waiting for more data",
568 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200569 return 0;
570}
571
572/* This function performs all the processing enabled for the current request.
573 * It returns 1 if the processing can continue on next analysers, or zero if it
574 * needs more data, encounters an error, or wants to immediately abort the
575 * request. It relies on buffers flags, and updates s->req.analysers.
576 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200577int http_process_request(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200578{
579 struct session *sess = s->sess;
580 struct http_txn *txn = s->txn;
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200581 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200582 struct connection *cli_conn = objt_conn(strm_sess(s)->origin);
583
Christopher Faulet8bebd2f2020-10-06 17:54:56 +0200584 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200585
586 /*
587 * Right now, we know that we have processed the entire headers
588 * and that unwanted requests have been filtered out. We can do
589 * whatever we want with the remaining request. Also, now we
590 * may have separate values for ->fe, ->be.
591 */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100592 htx = htxbuf(&req->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200593
594 /*
595 * If HTTP PROXY is set we simply get remote server address parsing
Willy Tarreau1c8d32b2019-07-18 15:47:45 +0200596 * incoming request.
Christopher Faulete0768eb2018-10-03 16:38:02 +0200597 */
598 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SF_ADDR_SET)) {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100599 struct htx_sl *sl;
600 struct ist uri, path;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200601
Willy Tarreau9b7587a2020-10-15 07:32:10 +0200602 if (!sockaddr_alloc(&s->target_addr, NULL, 0)) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200603 if (!(s->flags & SF_ERR_MASK))
604 s->flags |= SF_ERR_RESOURCE;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100605 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200606 }
Christopher Faulet297fbb42019-05-13 14:41:27 +0200607 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100608 uri = htx_sl_req_uri(sl);
609 path = http_get_path(uri);
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200610
Willy Tarreau1c8d32b2019-07-18 15:47:45 +0200611 if (url2sa(uri.ptr, uri.len - path.len, s->target_addr, NULL) == -1)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200612 goto return_bad_req;
613
Willy Tarreau1c8d32b2019-07-18 15:47:45 +0200614 s->target = &s->be->obj_type;
615 s->flags |= SF_ADDR_SET | SF_ASSIGNED;
616
Christopher Faulete0768eb2018-10-03 16:38:02 +0200617 /* if the path was found, we have to remove everything between
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200618 * uri.ptr and path.ptr (excluded). If it was not found, we need
619 * to replace from all the uri by a single "/".
620 *
Ilya Shipitsin46a030c2020-07-05 16:36:08 +0500621 * Instead of rewriting the whole start line, we just update
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100622 * the star-line URI. Some space will be lost but it should be
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200623 * insignificant.
Christopher Faulete0768eb2018-10-03 16:38:02 +0200624 */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100625 istcpy(&uri, (path.len ? path : ist("/")), uri.len);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200626 }
627
628 /*
629 * 7: Now we can work with the cookies.
630 * Note that doing so might move headers in the request, but
631 * the fields will stay coherent and the URI will not move.
632 * This should only be performed in the backend.
633 */
634 if (s->be->cookie_name || sess->fe->capture_name)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200635 http_manage_client_side_cookies(s, req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200636
Tim Duesterhus2825b4b2020-02-28 15:13:34 +0100637 /* 8: Generate unique ID if a "unique-id-format" is defined.
638 *
639 * A unique ID is generated even when it is not sent to ensure that the ID can make use of
640 * fetches only available in the HTTP request processing stage.
641 */
642 if (!LIST_ISEMPTY(&sess->fe->format_unique_id)) {
Tim Duesterhusa17e6622020-03-05 20:19:02 +0100643 struct ist unique_id = stream_generate_unique_id(s, &sess->fe->format_unique_id);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200644
Tim Duesterhusa17e6622020-03-05 20:19:02 +0100645 if (!isttest(unique_id)) {
Christopher Fauletb8a53712019-12-16 11:29:38 +0100646 if (!(s->flags & SF_ERR_MASK))
647 s->flags |= SF_ERR_RESOURCE;
648 goto return_int_err;
649 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200650
Tim Duesterhus2825b4b2020-02-28 15:13:34 +0100651 /* send unique ID if a "unique-id-header" is defined */
Tim Duesterhus0643b0e2020-03-05 17:56:35 +0100652 if (isttest(sess->fe->header_unique_id) &&
Tim Duesterhusa17e6622020-03-05 20:19:02 +0100653 unlikely(!http_add_header(htx, sess->fe->header_unique_id, s->unique_id)))
Tim Duesterhus2825b4b2020-02-28 15:13:34 +0100654 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200655 }
656
657 /*
658 * 9: add X-Forwarded-For if either the frontend or the backend
659 * asks for it.
660 */
661 if ((sess->fe->options | s->be->options) & PR_O_FWDFOR) {
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200662 struct http_hdr_ctx ctx = { .blk = NULL };
663 struct ist hdr = ist2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : sess->fe->fwdfor_hdr_name,
664 s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : sess->fe->fwdfor_hdr_len);
665
Christopher Faulete0768eb2018-10-03 16:38:02 +0200666 if (!((sess->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200667 http_find_header(htx, hdr, &ctx, 0)) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200668 /* The header is set to be added only if none is present
669 * and we found it, so don't do anything.
670 */
671 }
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200672 else if (cli_conn && conn_get_src(cli_conn) && cli_conn->src->ss_family == AF_INET) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200673 /* Add an X-Forwarded-For header unless the source IP is
674 * in the 'except' network range.
675 */
Christopher Faulet5d1def62021-02-26 09:19:15 +0100676 if (ipcmp2net(cli_conn->src, &sess->fe->except_xff_net) &&
677 ipcmp2net(cli_conn->src, &s->be->except_xff_net)) {
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200678 unsigned char *pn = (unsigned char *)&((struct sockaddr_in *)cli_conn->src)->sin_addr;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200679
680 /* Note: we rely on the backend to get the header name to be used for
681 * x-forwarded-for, because the header is really meant for the backends.
682 * However, if the backend did not specify any option, we have to rely
683 * on the frontend's header name.
684 */
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200685 chunk_printf(&trash, "%d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
686 if (unlikely(!http_add_header(htx, hdr, ist2(trash.area, trash.data))))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100687 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200688 }
689 }
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200690 else if (cli_conn && conn_get_src(cli_conn) && cli_conn->src->ss_family == AF_INET6) {
Christopher Faulet5d1def62021-02-26 09:19:15 +0100691 /* Add an X-Forwarded-For header unless the source IP is
692 * in the 'except' network range.
Christopher Faulete0768eb2018-10-03 16:38:02 +0200693 */
Christopher Faulet5d1def62021-02-26 09:19:15 +0100694 if (ipcmp2net(cli_conn->src, &sess->fe->except_xff_net) &&
695 ipcmp2net(cli_conn->src, &s->be->except_xff_net)) {
696 char pn[INET6_ADDRSTRLEN];
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200697
Christopher Faulet5d1def62021-02-26 09:19:15 +0100698 inet_ntop(AF_INET6,
699 (const void *)&((struct sockaddr_in6 *)(cli_conn->src))->sin6_addr,
700 pn, sizeof(pn));
Christopher Faulete0768eb2018-10-03 16:38:02 +0200701
Christopher Faulet5d1def62021-02-26 09:19:15 +0100702 /* Note: we rely on the backend to get the header name to be used for
703 * x-forwarded-for, because the header is really meant for the backends.
704 * However, if the backend did not specify any option, we have to rely
705 * on the frontend's header name.
706 */
707 chunk_printf(&trash, "%s", pn);
708 if (unlikely(!http_add_header(htx, hdr, ist2(trash.area, trash.data))))
709 goto return_int_err;
710 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200711 }
712 }
713
714 /*
715 * 10: add X-Original-To if either the frontend or the backend
716 * asks for it.
717 */
718 if ((sess->fe->options | s->be->options) & PR_O_ORGTO) {
Christopher Faulet5d1def62021-02-26 09:19:15 +0100719 struct ist hdr = ist2(s->be->orgto_hdr_len ? s->be->orgto_hdr_name : sess->fe->orgto_hdr_name,
720 s->be->orgto_hdr_len ? s->be->orgto_hdr_len : sess->fe->orgto_hdr_len);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200721
Christopher Fauletcccded92021-02-26 12:45:56 +0100722 if (cli_conn && conn_get_dst(cli_conn) && cli_conn->dst->ss_family == AF_INET) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200723 /* Add an X-Original-To header unless the destination IP is
724 * in the 'except' network range.
725 */
Christopher Faulet5d1def62021-02-26 09:19:15 +0100726 if (ipcmp2net(cli_conn->dst, &sess->fe->except_xot_net) &&
727 ipcmp2net(cli_conn->dst, &s->be->except_xot_net)) {
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200728 unsigned char *pn = (unsigned char *)&((struct sockaddr_in *)cli_conn->dst)->sin_addr;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200729
730 /* Note: we rely on the backend to get the header name to be used for
731 * x-original-to, because the header is really meant for the backends.
732 * However, if the backend did not specify any option, we have to rely
733 * on the frontend's header name.
734 */
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200735 chunk_printf(&trash, "%d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
736 if (unlikely(!http_add_header(htx, hdr, ist2(trash.area, trash.data))))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100737 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200738 }
739 }
Christopher Faulet5d1def62021-02-26 09:19:15 +0100740 else if (cli_conn && conn_get_dst(cli_conn) && cli_conn->dst->ss_family == AF_INET6) {
741 /* Add an X-Original-To header unless the source IP is
742 * in the 'except' network range.
743 */
744 if (ipcmp2net(cli_conn->dst, &sess->fe->except_xot_net) &&
745 ipcmp2net(cli_conn->dst, &s->be->except_xot_net)) {
746 char pn[INET6_ADDRSTRLEN];
747
748 inet_ntop(AF_INET6,
749 (const void *)&((struct sockaddr_in6 *)(cli_conn->dst))->sin6_addr,
750 pn, sizeof(pn));
751
752 /* Note: we rely on the backend to get the header name to be used for
753 * x-forwarded-for, because the header is really meant for the backends.
754 * However, if the backend did not specify any option, we have to rely
755 * on the frontend's header name.
756 */
757 chunk_printf(&trash, "%s", pn);
758 if (unlikely(!http_add_header(htx, hdr, ist2(trash.area, trash.data))))
759 goto return_int_err;
760 }
761 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200762 }
763
Christopher Faulete0768eb2018-10-03 16:38:02 +0200764 /* If we have no server assigned yet and we're balancing on url_param
765 * with a POST request, we may be interested in checking the body for
766 * that parameter. This will be done in another analyser.
767 */
768 if (!(s->flags & (SF_ASSIGNED|SF_DIRECT)) &&
Willy Tarreau089eaa02019-01-14 15:17:46 +0100769 s->txn->meth == HTTP_METH_POST &&
770 (s->be->lbprm.algo & BE_LB_ALGO) == BE_LB_ALGO_PH) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200771 channel_dont_connect(req);
772 req->analysers |= AN_REQ_HTTP_BODY;
773 }
774
775 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
776 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau1a18b542018-12-11 16:37:42 +0100777
Christopher Faulete0768eb2018-10-03 16:38:02 +0200778 /* We expect some data from the client. Unless we know for sure
779 * we already have a full request, we have to re-enable quick-ack
780 * in case we previously disabled it, otherwise we might cause
781 * the client to delay further data.
782 */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +0100783 if ((sess->listener->options & LI_O_NOQUICKACK) && !(htx->flags & HTX_FL_EOM))
Willy Tarreau1a18b542018-12-11 16:37:42 +0100784 conn_set_quickack(cli_conn, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200785
786 /*************************************************************
787 * OK, that's finished for the headers. We have done what we *
788 * could. Let's switch to the DATA state. *
789 ************************************************************/
790 req->analyse_exp = TICK_ETERNITY;
791 req->analysers &= ~an_bit;
792
793 s->logs.tv_request = now;
794 /* OK let's go on with the BODY now */
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100795 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200796 return 1;
797
Christopher Fauletb8a53712019-12-16 11:29:38 +0100798 return_int_err:
799 txn->status = 500;
800 if (!(s->flags & SF_ERR_MASK))
801 s->flags |= SF_ERR_INTERNAL;
802 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100803 if (s->flags & SF_BE_ASSIGNED)
Christopher Fauletbe20cf32020-01-24 11:41:38 +0100804 _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100805 if (sess->listener->counters)
806 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
807 goto return_prx_cond;
808
Christopher Faulete0768eb2018-10-03 16:38:02 +0200809 return_bad_req: /* let's centralize all bad requests */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200810 txn->status = 400;
Olivier Houcharda798bf52019-03-08 18:52:00 +0100811 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200812 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100813 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100814 /* fall through */
815
816 return_prx_cond:
817 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +0200818
819 if (!(s->flags & SF_ERR_MASK))
820 s->flags |= SF_ERR_PRXCOND;
821 if (!(s->flags & SF_FINST_MASK))
822 s->flags |= SF_FINST_R;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100823
824 req->analysers &= AN_REQ_FLT_END;
825 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100826 DBG_TRACE_DEVEL("leaving on error",
827 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200828 return 0;
829}
830
831/* This function is an analyser which processes the HTTP tarpit. It always
832 * returns zero, at the beginning because it prevents any other processing
833 * from occurring, and at the end because it terminates the request.
834 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200835int http_process_tarpit(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200836{
837 struct http_txn *txn = s->txn;
838
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100839 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, &txn->req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200840 /* This connection is being tarpitted. The CLIENT side has
841 * already set the connect expiration date to the right
842 * timeout. We just have to check that the client is still
843 * there and that the timeout has not expired.
844 */
845 channel_dont_connect(req);
846 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100847 !tick_is_expired(req->analyse_exp, now_ms)) {
848 DBG_TRACE_DEVEL("waiting for tarpit timeout expiry",
849 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200850 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100851 }
852
Christopher Faulete0768eb2018-10-03 16:38:02 +0200853
854 /* We will set the queue timer to the time spent, just for
855 * logging purposes. We fake a 500 server error, so that the
856 * attacker will not suspect his connection has been tarpitted.
857 * It will not cause trouble to the logs because we can exclude
858 * the tarpitted connections by filtering on the 'PT' status flags.
859 */
860 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
861
Christopher Faulet8dfeccf2020-05-15 14:16:29 +0200862 http_reply_and_close(s, txn->status, (!(req->flags & CF_READ_ERROR) ? http_error_message(s) : NULL));
Christopher Faulet5cb513a2020-05-13 17:56:56 +0200863
864 end:
Christopher Faulete0768eb2018-10-03 16:38:02 +0200865 req->analysers &= AN_REQ_FLT_END;
866 req->analyse_exp = TICK_ETERNITY;
867
868 if (!(s->flags & SF_ERR_MASK))
869 s->flags |= SF_ERR_PRXCOND;
870 if (!(s->flags & SF_FINST_MASK))
871 s->flags |= SF_FINST_T;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100872
873 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200874 return 0;
875}
876
877/* This function is an analyser which waits for the HTTP request body. It waits
878 * for either the buffer to be full, or the full advertised contents to have
879 * reached the buffer. It must only be called after the standard HTTP request
880 * processing has occurred, because it expects the request to be parsed and will
881 * look for the Expect header. It may send a 100-Continue interim response. It
882 * takes in input any state starting from HTTP_MSG_BODY and leaves with one of
883 * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it
884 * needs to read more data, or 1 once it has completed its analysis.
885 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200886int http_wait_for_request_body(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200887{
888 struct session *sess = s->sess;
889 struct http_txn *txn = s->txn;
890 struct http_msg *msg = &s->txn->req;
Christopher Fauletf76ebe82018-10-24 11:16:22 +0200891 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200892
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100893 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Fauletf76ebe82018-10-24 11:16:22 +0200894
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100895 htx = htxbuf(&req->buf);
Christopher Fauletf76ebe82018-10-24 11:16:22 +0200896
Willy Tarreau4236f032019-03-05 10:43:32 +0100897 if (htx->flags & HTX_FL_PARSING_ERROR)
898 goto return_bad_req;
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200899 if (htx->flags & HTX_FL_PROCESSING_ERROR)
900 goto return_int_err;
Willy Tarreau4236f032019-03-05 10:43:32 +0100901
Christopher Fauletc31bc722020-11-20 14:30:38 +0100902 /* Do nothing for bodyless and CONNECT requests */
903 if (txn->meth == HTTP_METH_CONNECT || (msg->flags & HTTP_MSGF_BODYLESS))
Christopher Faulet63c69a92020-11-16 16:03:35 +0100904 goto http_end;
905
Christopher Faulete0768eb2018-10-03 16:38:02 +0200906 /* We have to parse the HTTP request body to find any required data.
907 * "balance url_param check_post" should have been the only way to get
908 * into this. We were brought here after HTTP header analysis, so all
909 * related structures are ready.
910 */
911
Christopher Fauletf76ebe82018-10-24 11:16:22 +0200912 if (msg->msg_state < HTTP_MSG_DATA) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200913 if (http_handle_expect_hdr(s, htx, msg) == -1)
Christopher Fauletb8a53712019-12-16 11:29:38 +0100914 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200915 }
916
Christopher Fauletf76ebe82018-10-24 11:16:22 +0200917 msg->msg_state = HTTP_MSG_DATA;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200918
Christopher Fauletf76ebe82018-10-24 11:16:22 +0200919 /* Now we're in HTTP_MSG_DATA. We just need to know if all data have
920 * been received or if the buffer is full.
Christopher Faulete0768eb2018-10-03 16:38:02 +0200921 */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +0100922 if ((htx->flags & HTX_FL_EOM) || htx_get_tail_type(htx) > HTX_BLK_DATA ||
Christopher Fauletdcd8c5e2019-01-21 11:24:38 +0100923 channel_htx_full(req, htx, global.tune.maxrewrite))
Christopher Faulete0768eb2018-10-03 16:38:02 +0200924 goto http_end;
925
926 if ((req->flags & CF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
927 txn->status = 408;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200928 if (!(s->flags & SF_ERR_MASK))
929 s->flags |= SF_ERR_CLITO;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100930 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
931 if (sess->listener->counters)
932 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
933 goto return_prx_cond;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200934 }
935
936 /* we get here if we need to wait for more data */
937 if (!(req->flags & (CF_SHUTR | CF_READ_ERROR))) {
938 /* Not enough data. We'll re-use the http-request
939 * timeout here. Ideally, we should set the timeout
940 * relative to the accept() date. We just set the
941 * request timeout once at the beginning of the
942 * request.
943 */
944 channel_dont_connect(req);
945 if (!tick_isset(req->analyse_exp))
946 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100947 DBG_TRACE_DEVEL("waiting for more data",
948 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200949 return 0;
950 }
951
952 http_end:
953 /* The situation will not evolve, so let's give up on the analysis. */
954 s->logs.tv_request = now; /* update the request timer to reflect full request */
955 req->analysers &= ~an_bit;
956 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100957 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200958 return 1;
959
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200960 return_int_err:
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200961 txn->status = 500;
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200962 if (!(s->flags & SF_ERR_MASK))
963 s->flags |= SF_ERR_INTERNAL;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100964 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100965 if (s->flags & SF_BE_ASSIGNED)
Christopher Fauletbe20cf32020-01-24 11:41:38 +0100966 _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100967 if (sess->listener->counters)
968 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
969 goto return_prx_cond;
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200970
Christopher Faulete0768eb2018-10-03 16:38:02 +0200971 return_bad_req: /* let's centralize all bad requests */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200972 txn->status = 400;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100973 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
974 if (sess->listener->counters)
975 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
976 /* fall through */
977
978 return_prx_cond:
979 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +0200980
981 if (!(s->flags & SF_ERR_MASK))
982 s->flags |= SF_ERR_PRXCOND;
983 if (!(s->flags & SF_FINST_MASK))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100984 s->flags |= (msg->msg_state < HTTP_MSG_DATA ? SF_FINST_R : SF_FINST_D);
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200985
Christopher Faulete0768eb2018-10-03 16:38:02 +0200986 req->analysers &= AN_REQ_FLT_END;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100987 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100988 DBG_TRACE_DEVEL("leaving on error",
989 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200990 return 0;
991}
992
993/* This function is an analyser which forwards request body (including chunk
994 * sizes if any). It is called as soon as we must forward, even if we forward
995 * zero byte. The only situation where it must not be called is when we're in
996 * tunnel mode and we want to forward till the close. It's used both to forward
997 * remaining data and to resync after end of body. It expects the msg_state to
998 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
999 * read more data, or 1 once we can go on with next request or end the stream.
1000 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
1001 * bytes of pending data + the headers if not already done.
1002 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001003int http_request_forward_body(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001004{
1005 struct session *sess = s->sess;
1006 struct http_txn *txn = s->txn;
Christopher Faulet9768c262018-10-22 09:34:31 +02001007 struct http_msg *msg = &txn->req;
1008 struct htx *htx;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001009 short status = 0;
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001010 int ret;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001011
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001012 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001013
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001014 htx = htxbuf(&req->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001015
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001016 if (htx->flags & HTX_FL_PARSING_ERROR)
1017 goto return_bad_req;
1018 if (htx->flags & HTX_FL_PROCESSING_ERROR)
1019 goto return_int_err;
1020
Christopher Faulete0768eb2018-10-03 16:38:02 +02001021 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
1022 ((req->flags & CF_SHUTW) && (req->to_forward || co_data(req)))) {
1023 /* Output closed while we were sending data. We must abort and
1024 * wake the other side up.
1025 */
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001026
Olivier Houchard29cac3c2019-07-12 15:48:58 +02001027 /* Don't abort yet if we had L7 retries activated and it
1028 * was a write error, we may recover.
1029 */
1030 if (!(req->flags & (CF_READ_ERROR | CF_READ_TIMEOUT)) &&
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001031 (s->si[1].flags & SI_FL_L7_RETRY)) {
1032 DBG_TRACE_DEVEL("leaving on L7 retry",
1033 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Olivier Houchard29cac3c2019-07-12 15:48:58 +02001034 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001035 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001036 msg->msg_state = HTTP_MSG_ERROR;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001037 http_end_request(s);
1038 http_end_response(s);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001039 DBG_TRACE_DEVEL("leaving on error",
1040 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001041 return 1;
1042 }
1043
1044 /* Note that we don't have to send 100-continue back because we don't
1045 * need the data to complete our job, and it's up to the server to
1046 * decide whether to return 100, 417 or anything else in return of
1047 * an "Expect: 100-continue" header.
1048 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001049 if (msg->msg_state == HTTP_MSG_BODY)
1050 msg->msg_state = HTTP_MSG_DATA;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001051
Christopher Faulete0768eb2018-10-03 16:38:02 +02001052 /* in most states, we should abort in case of early close */
1053 channel_auto_close(req);
1054
1055 if (req->to_forward) {
Christopher Faulet66af0b22019-03-22 14:54:52 +01001056 if (req->to_forward == CHN_INFINITE_FORWARD) {
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001057 if (req->flags & CF_EOI)
1058 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet66af0b22019-03-22 14:54:52 +01001059 }
1060 else {
1061 /* We can't process the buffer's contents yet */
1062 req->flags |= CF_WAKE_WRITE;
1063 goto missing_data_or_waiting;
1064 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001065 }
1066
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001067 if (msg->msg_state >= HTTP_MSG_ENDING)
1068 goto ending;
1069
1070 if (txn->meth == HTTP_METH_CONNECT) {
1071 msg->msg_state = HTTP_MSG_ENDING;
1072 goto ending;
1073 }
1074
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001075 /* Forward input data. We get it by removing all outgoing data not
1076 * forwarded yet from HTX data size. If there are some data filters, we
1077 * let them decide the amount of data to forward.
Christopher Faulet9768c262018-10-22 09:34:31 +02001078 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001079 if (HAS_REQ_DATA_FILTERS(s)) {
1080 ret = flt_http_payload(s, msg, htx->data);
1081 if (ret < 0)
1082 goto return_bad_req;
Christopher Faulet421e7692019-06-13 11:16:45 +02001083 c_adv(req, ret);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001084 }
1085 else {
Christopher Faulet421e7692019-06-13 11:16:45 +02001086 c_adv(req, htx->data - co_data(req));
Christopher Faulet66af0b22019-03-22 14:54:52 +01001087 if (msg->flags & HTTP_MSGF_XFER_LEN)
1088 channel_htx_forward_forever(req, htx);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001089 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001090
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001091 if (htx->data != co_data(req))
1092 goto missing_data_or_waiting;
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001093
Christopher Faulet9768c262018-10-22 09:34:31 +02001094 /* Check if the end-of-message is reached and if so, switch the message
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001095 * in HTTP_MSG_ENDING state. Then if all data was marked to be
1096 * forwarded, set the state to HTTP_MSG_DONE.
Christopher Faulet9768c262018-10-22 09:34:31 +02001097 */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001098 if (!(htx->flags & HTX_FL_EOM))
Christopher Faulet9768c262018-10-22 09:34:31 +02001099 goto missing_data_or_waiting;
1100
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001101 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet9768c262018-10-22 09:34:31 +02001102
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001103 ending:
Christopher Faulet2151cdd2020-07-22 16:34:59 +02001104 req->flags &= ~CF_EXPECT_MORE; /* no more data are expected */
1105
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001106 /* other states, ENDING...TUNNEL */
1107 if (msg->msg_state >= HTTP_MSG_DONE)
1108 goto done;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001109
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001110 if (HAS_REQ_DATA_FILTERS(s)) {
1111 ret = flt_http_end(s, msg);
1112 if (ret <= 0) {
1113 if (!ret)
1114 goto missing_data_or_waiting;
1115 goto return_bad_req;
1116 }
1117 }
1118
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001119 if (txn->meth == HTTP_METH_CONNECT)
1120 msg->msg_state = HTTP_MSG_TUNNEL;
1121 else {
1122 msg->msg_state = HTTP_MSG_DONE;
1123 req->to_forward = 0;
Christopher Faulet5b82cc52020-10-12 15:18:50 +02001124
1125 if ((s->be->retry_type &~ PR_RE_CONN_FAILED) && !(s->si[1].flags & SI_FL_D_L7_RETRY)) {
1126 struct stream_interface *si = &s->si[1];
1127
1128 /* If we want to be able to do L7 retries, copy the
1129 * request, so that we are able to resend them if
1130 * needed.
1131 *
1132 * Try to allocate a buffer if we had none. If it
1133 * fails, the next test will just disable the l7
1134 * retries.
1135 */
1136 DBG_TRACE_STATE("enable L7 retry, save the request", STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
1137 si->flags |= SI_FL_L7_RETRY;
1138 if (b_is_null(&si->l7_buffer))
1139 b_alloc(&si->l7_buffer);
1140 if (b_is_null(&si->l7_buffer))
1141 si->flags &= ~SI_FL_L7_RETRY;
1142 else {
1143 memcpy(b_orig(&si->l7_buffer), b_orig(&req->buf), b_size(&req->buf));
1144 b_add(&si->l7_buffer, co_data(req));
1145 }
1146 }
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001147 }
1148
1149 done:
1150 /* we don't want to forward closes on DONE except in tunnel mode. */
1151 if (!(txn->flags & TX_CON_WANT_TUN))
1152 channel_dont_close(req);
1153
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001154 http_end_request(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001155 if (!(req->analysers & an_bit)) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001156 http_end_response(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001157 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
1158 if (req->flags & CF_SHUTW) {
1159 /* request errors are most likely due to the
1160 * server aborting the transfer. */
Christopher Faulet93e02d82019-03-08 14:18:50 +01001161 goto return_srv_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001162 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001163 goto return_bad_req;
1164 }
Christopher Faulet5b82cc52020-10-12 15:18:50 +02001165
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001166 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001167 return 1;
1168 }
1169
1170 /* If "option abortonclose" is set on the backend, we want to monitor
1171 * the client's connection and forward any shutdown notification to the
1172 * server, which will decide whether to close or to go on processing the
1173 * request. We only do that in tunnel mode, and not in other modes since
1174 * it can be abused to exhaust source ports. */
Christopher Faulet769d0e92019-03-22 14:23:18 +01001175 if (s->be->options & PR_O_ABRT_CLOSE) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001176 channel_auto_read(req);
Christopher Fauletc41547b2019-07-16 14:32:23 +02001177 if ((req->flags & (CF_SHUTR|CF_READ_NULL)) && !(txn->flags & TX_CON_WANT_TUN))
Christopher Faulete0768eb2018-10-03 16:38:02 +02001178 s->si[1].flags |= SI_FL_NOLINGER;
1179 channel_auto_close(req);
1180 }
1181 else if (s->txn->meth == HTTP_METH_POST) {
1182 /* POST requests may require to read extra CRLF sent by broken
1183 * browsers and which could cause an RST to be sent upon close
1184 * on some systems (eg: Linux). */
1185 channel_auto_read(req);
1186 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001187 DBG_TRACE_DEVEL("waiting for the end of the HTTP txn",
1188 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001189 return 0;
1190
1191 missing_data_or_waiting:
1192 /* stop waiting for data if the input is closed before the end */
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001193 if (msg->msg_state < HTTP_MSG_ENDING && req->flags & CF_SHUTR)
Christopher Faulet93e02d82019-03-08 14:18:50 +01001194 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001195
1196 waiting:
1197 /* waiting for the last bits to leave the buffer */
1198 if (req->flags & CF_SHUTW)
Christopher Faulet93e02d82019-03-08 14:18:50 +01001199 goto return_srv_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001200
1201 /* When TE: chunked is used, we need to get there again to parse remaining
1202 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
1203 * And when content-length is used, we never want to let the possible
1204 * shutdown be forwarded to the other side, as the state machine will
1205 * take care of it once the client responds. It's also important to
1206 * prevent TIME_WAITs from accumulating on the backend side, and for
1207 * HTTP/2 where the last frame comes with a shutdown.
1208 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001209 if (msg->flags & HTTP_MSGF_XFER_LEN)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001210 channel_dont_close(req);
1211
1212 /* We know that more data are expected, but we couldn't send more that
1213 * what we did. So we always set the CF_EXPECT_MORE flag so that the
1214 * system knows it must not set a PUSH on this first part. Interactive
1215 * modes are already handled by the stream sock layer. We must not do
1216 * this in content-length mode because it could present the MSG_MORE
1217 * flag with the last block of forwarded data, which would cause an
1218 * additional delay to be observed by the receiver.
1219 */
Christopher Faulet2151cdd2020-07-22 16:34:59 +02001220 if (HAS_REQ_DATA_FILTERS(s))
Christopher Faulete0768eb2018-10-03 16:38:02 +02001221 req->flags |= CF_EXPECT_MORE;
1222
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001223 DBG_TRACE_DEVEL("waiting for more data to forward",
1224 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001225 return 0;
1226
Christopher Faulet93e02d82019-03-08 14:18:50 +01001227 return_cli_abort:
1228 _HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
1229 _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001230 if (sess->listener->counters)
1231 _HA_ATOMIC_ADD(&sess->listener->counters->cli_aborts, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001232 if (objt_server(s->target))
Christopher Fauletcff0f732019-12-16 16:13:44 +01001233 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.cli_aborts, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001234 if (!(s->flags & SF_ERR_MASK))
1235 s->flags |= SF_ERR_CLICL;
1236 status = 400;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001237 goto return_prx_cond;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001238
1239 return_srv_abort:
1240 _HA_ATOMIC_ADD(&sess->fe->fe_counters.srv_aborts, 1);
1241 _HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001242 if (sess->listener->counters)
1243 _HA_ATOMIC_ADD(&sess->listener->counters->srv_aborts, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001244 if (objt_server(s->target))
Christopher Fauletcff0f732019-12-16 16:13:44 +01001245 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.srv_aborts, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001246 if (!(s->flags & SF_ERR_MASK))
1247 s->flags |= SF_ERR_SRVCL;
1248 status = 502;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001249 goto return_prx_cond;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001250
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001251 return_int_err:
1252 if (!(s->flags & SF_ERR_MASK))
1253 s->flags |= SF_ERR_INTERNAL;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001254 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001255 _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01001256 if (sess->listener->counters)
1257 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001258 if (objt_server(s->target))
1259 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.internal_errors, 1);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001260 status = 500;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001261 goto return_prx_cond;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001262
Christopher Faulet93e02d82019-03-08 14:18:50 +01001263 return_bad_req:
Olivier Houcharda798bf52019-03-08 18:52:00 +01001264 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001265 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +01001266 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001267 status = 400;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001268 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001269
Christopher Fauletb8a53712019-12-16 11:29:38 +01001270 return_prx_cond:
Christopher Faulet9768c262018-10-22 09:34:31 +02001271 if (txn->status > 0) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001272 /* Note: we don't send any error if some data were already sent */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001273 http_reply_and_close(s, txn->status, NULL);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001274 } else {
Christopher Faulet93e02d82019-03-08 14:18:50 +01001275 txn->status = status;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001276 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001277 }
1278 req->analysers &= AN_REQ_FLT_END;
1279 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 +01001280 if (!(s->flags & SF_ERR_MASK))
1281 s->flags |= SF_ERR_PRXCOND;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001282 if (!(s->flags & SF_FINST_MASK))
1283 s->flags |= ((txn->rsp.msg_state < HTTP_MSG_ERROR) ? SF_FINST_H : SF_FINST_D);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001284 DBG_TRACE_DEVEL("leaving on error ",
1285 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001286 return 0;
1287}
1288
Olivier Houcharda254a372019-04-05 15:30:12 +02001289/* Reset the stream and the backend stream_interface to a situation suitable for attemption connection */
1290/* Returns 0 if we can attempt to retry, -1 otherwise */
1291static __inline int do_l7_retry(struct stream *s, struct stream_interface *si)
1292{
Christopher Faulet5b82cc52020-10-12 15:18:50 +02001293 struct channel *req = &s->req;
1294 struct channel *res = &s->res;
Olivier Houcharda254a372019-04-05 15:30:12 +02001295
1296 si->conn_retries--;
1297 if (si->conn_retries < 0)
Christopher Faulet5b82cc52020-10-12 15:18:50 +02001298 goto no_retry;
1299
1300 if (b_is_null(&req->buf) && !channel_alloc_buffer(req, &s->buffer_wait))
1301 goto no_retry;
Olivier Houcharda254a372019-04-05 15:30:12 +02001302
Willy Tarreau223995e2019-05-04 10:38:31 +02001303 if (objt_server(s->target))
1304 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.retries, 1);
1305 _HA_ATOMIC_ADD(&s->be->be_counters.retries, 1);
1306
Olivier Houcharda254a372019-04-05 15:30:12 +02001307 /* Remove any write error from the request, and read error from the response */
1308 req->flags &= ~(CF_WRITE_ERROR | CF_WRITE_TIMEOUT | CF_SHUTW | CF_SHUTW_NOW);
1309 res->flags &= ~(CF_READ_ERROR | CF_READ_TIMEOUT | CF_SHUTR | CF_EOI | CF_READ_NULL | CF_SHUTR_NOW);
1310 res->analysers = 0;
1311 si->flags &= ~(SI_FL_ERR | SI_FL_EXP | SI_FL_RXBLK_SHUT);
Olivier Houchard8cabc972020-05-12 22:18:14 +02001312 s->flags &= ~SF_ADDR_SET;
Olivier Houchard4bd58672019-07-12 16:16:59 +02001313 stream_choose_redispatch(s);
Olivier Houcharda254a372019-04-05 15:30:12 +02001314 si->exp = TICK_ETERNITY;
1315 res->rex = TICK_ETERNITY;
1316 res->to_forward = 0;
1317 res->analyse_exp = TICK_ETERNITY;
1318 res->total = 0;
Olivier Houchard4bd58672019-07-12 16:16:59 +02001319 s->flags &= ~(SF_ERR_SRVTO | SF_ERR_SRVCL);
Olivier Houcharda254a372019-04-05 15:30:12 +02001320 si_release_endpoint(&s->si[1]);
Olivier Houcharda254a372019-04-05 15:30:12 +02001321
Christopher Faulet5b82cc52020-10-12 15:18:50 +02001322 b_reset(&req->buf);
1323 memcpy(b_orig(&req->buf), b_orig(&si->l7_buffer), b_size(&si->l7_buffer));
1324 b_set_data(&req->buf, b_size(&req->buf));
1325 co_set_data(req, b_data(&si->l7_buffer));
1326
Ilya Shipitsinacf84592021-02-06 22:29:08 +05001327 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 +02001328 b_reset(&res->buf);
1329 co_set_data(res, 0);
1330 return 0;
Christopher Faulet5b82cc52020-10-12 15:18:50 +02001331
1332 no_retry:
1333 b_free(&si->l7_buffer);
1334 return -1;
Olivier Houcharda254a372019-04-05 15:30:12 +02001335}
1336
Christopher Faulete0768eb2018-10-03 16:38:02 +02001337/* This stream analyser waits for a complete HTTP response. It returns 1 if the
1338 * processing can continue on next analysers, or zero if it either needs more
1339 * data or wants to immediately abort the response (eg: timeout, error, ...). It
1340 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->res.analysers
1341 * when it has nothing left to do, and may remove any analyser when it wants to
1342 * abort.
1343 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001344int http_wait_for_response(struct stream *s, struct channel *rep, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001345{
Christopher Faulet9768c262018-10-22 09:34:31 +02001346 /*
1347 * We will analyze a complete HTTP response to check the its syntax.
1348 *
1349 * Once the start line and all headers are received, we may perform a
1350 * capture of the error (if any), and we will set a few fields. We also
1351 * logging and finally headers capture.
1352 */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001353 struct session *sess = s->sess;
1354 struct http_txn *txn = s->txn;
1355 struct http_msg *msg = &txn->rsp;
Christopher Faulet9768c262018-10-22 09:34:31 +02001356 struct htx *htx;
Olivier Houcharda254a372019-04-05 15:30:12 +02001357 struct stream_interface *si_b = &s->si[1];
Christopher Faulet61608322018-11-23 16:23:45 +01001358 struct connection *srv_conn;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001359 struct htx_sl *sl;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001360 int n;
1361
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001362 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001363
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001364 htx = htxbuf(&rep->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001365
Willy Tarreau4236f032019-03-05 10:43:32 +01001366 /* Parsing errors are caught here */
1367 if (htx->flags & HTX_FL_PARSING_ERROR)
1368 goto return_bad_res;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001369 if (htx->flags & HTX_FL_PROCESSING_ERROR)
1370 goto return_int_err;
Willy Tarreau4236f032019-03-05 10:43:32 +01001371
Christopher Faulete0768eb2018-10-03 16:38:02 +02001372 /*
1373 * Now we quickly check if we have found a full valid response.
1374 * If not so, we check the FD and buffer states before leaving.
1375 * A full response is indicated by the fact that we have seen
1376 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
1377 * responses are checked first.
1378 *
1379 * Depending on whether the client is still there or not, we
1380 * may send an error response back or not. Note that normally
1381 * we should only check for HTTP status there, and check I/O
1382 * errors somewhere else.
1383 */
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001384 next_one:
Christopher Faulet29f17582019-05-23 11:03:26 +02001385 if (unlikely(htx_is_empty(htx) || htx->first == -1)) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001386 /* 1: have we encountered a read error ? */
1387 if (rep->flags & CF_READ_ERROR) {
Olivier Houchard865d8392019-05-03 22:46:27 +02001388 struct connection *conn = NULL;
1389
Olivier Houchard865d8392019-05-03 22:46:27 +02001390 if (objt_cs(s->si[1].end))
1391 conn = objt_cs(s->si[1].end)->conn;
1392
1393 if (si_b->flags & SI_FL_L7_RETRY &&
1394 (!conn || conn->err_code != CO_ER_SSL_EARLY_FAILED)) {
Olivier Houcharda254a372019-04-05 15:30:12 +02001395 /* If we arrive here, then CF_READ_ERROR was
1396 * set by si_cs_recv() because we matched a
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05001397 * status, otherwise it would have removed
Olivier Houcharda254a372019-04-05 15:30:12 +02001398 * the SI_FL_L7_RETRY flag, so it's ok not
1399 * to check s->be->retry_type.
1400 */
Christopher Faulet5b82cc52020-10-12 15:18:50 +02001401 if (co_data(rep) || do_l7_retry(s, si_b) == 0) {
1402 DBG_TRACE_DEVEL("leaving on L7 retry",
1403 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharda254a372019-04-05 15:30:12 +02001404 return 0;
Christopher Faulet5b82cc52020-10-12 15:18:50 +02001405 }
Olivier Houcharda254a372019-04-05 15:30:12 +02001406 }
1407
Olivier Houchard6db16992019-05-17 15:40:49 +02001408 if (txn->flags & TX_NOT_FIRST)
1409 goto abort_keep_alive;
1410
Olivier Houcharda798bf52019-03-08 18:52:00 +01001411 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001412 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001413 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001414 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001415 }
1416
Christopher Faulete0768eb2018-10-03 16:38:02 +02001417 rep->analysers &= AN_RES_FLT_END;
1418 txn->status = 502;
1419
1420 /* Check to see if the server refused the early data.
1421 * If so, just send a 425
1422 */
Willy Tarreauee99aaf2020-06-23 05:58:20 +02001423 if (conn && conn->err_code == CO_ER_SSL_EARLY_FAILED) {
Olivier Houchard865d8392019-05-03 22:46:27 +02001424 if ((s->be->retry_type & PR_RE_EARLY_ERROR) &&
Olivier Houchardad26d8d2019-05-10 17:48:28 +02001425 (si_b->flags & SI_FL_L7_RETRY) &&
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001426 do_l7_retry(s, si_b) == 0) {
1427 DBG_TRACE_DEVEL("leaving on L7 retry",
1428 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houchard865d8392019-05-03 22:46:27 +02001429 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001430 }
Olivier Houchard865d8392019-05-03 22:46:27 +02001431 txn->status = 425;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001432 }
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001433 else
1434 stream_inc_http_fail_ctr(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001435
1436 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001437 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001438
1439 if (!(s->flags & SF_ERR_MASK))
1440 s->flags |= SF_ERR_SRVCL;
1441 if (!(s->flags & SF_FINST_MASK))
1442 s->flags |= SF_FINST_H;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001443 DBG_TRACE_DEVEL("leaving on error",
1444 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001445 return 0;
1446 }
1447
Christopher Faulet9768c262018-10-22 09:34:31 +02001448 /* 2: read timeout : return a 504 to the client. */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001449 else if (rep->flags & CF_READ_TIMEOUT) {
Olivier Houcharda254a372019-04-05 15:30:12 +02001450 if ((si_b->flags & SI_FL_L7_RETRY) &&
1451 (s->be->retry_type & PR_RE_TIMEOUT)) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001452 if (co_data(rep) || do_l7_retry(s, si_b) == 0) {
1453 DBG_TRACE_DEVEL("leaving on L7 retry",
1454 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharda254a372019-04-05 15:30:12 +02001455 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001456 }
Olivier Houcharda254a372019-04-05 15:30:12 +02001457 }
Olivier Houcharda798bf52019-03-08 18:52:00 +01001458 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001459 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001460 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001461 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001462 }
1463
Christopher Faulete0768eb2018-10-03 16:38:02 +02001464 rep->analysers &= AN_RES_FLT_END;
1465 txn->status = 504;
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001466 stream_inc_http_fail_ctr(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001467 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001468 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001469
1470 if (!(s->flags & SF_ERR_MASK))
1471 s->flags |= SF_ERR_SRVTO;
1472 if (!(s->flags & SF_FINST_MASK))
1473 s->flags |= SF_FINST_H;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001474 DBG_TRACE_DEVEL("leaving on error",
1475 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001476 return 0;
1477 }
1478
Christopher Faulet9768c262018-10-22 09:34:31 +02001479 /* 3: client abort with an abortonclose */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001480 else if ((rep->flags & CF_SHUTR) && ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001481 _HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
1482 _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001483 if (sess->listener->counters)
1484 _HA_ATOMIC_ADD(&sess->listener->counters->cli_aborts, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001485 if (objt_server(s->target))
Olivier Houcharda798bf52019-03-08 18:52:00 +01001486 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.cli_aborts, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001487
1488 rep->analysers &= AN_RES_FLT_END;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001489 txn->status = 400;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001490 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001491
1492 if (!(s->flags & SF_ERR_MASK))
1493 s->flags |= SF_ERR_CLICL;
1494 if (!(s->flags & SF_FINST_MASK))
1495 s->flags |= SF_FINST_H;
1496
1497 /* process_stream() will take care of the error */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001498 DBG_TRACE_DEVEL("leaving on error",
1499 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001500 return 0;
1501 }
1502
Christopher Faulet9768c262018-10-22 09:34:31 +02001503 /* 4: close from server, capture the response if the server has started to respond */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001504 else if (rep->flags & CF_SHUTR) {
Olivier Houcharda254a372019-04-05 15:30:12 +02001505 if ((si_b->flags & SI_FL_L7_RETRY) &&
1506 (s->be->retry_type & PR_RE_DISCONNECTED)) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001507 if (co_data(rep) || do_l7_retry(s, si_b) == 0) {
1508 DBG_TRACE_DEVEL("leaving on L7 retry",
1509 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharda254a372019-04-05 15:30:12 +02001510 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001511 }
Olivier Houcharda254a372019-04-05 15:30:12 +02001512 }
1513
Olivier Houchard6db16992019-05-17 15:40:49 +02001514 if (txn->flags & TX_NOT_FIRST)
1515 goto abort_keep_alive;
1516
Olivier Houcharda798bf52019-03-08 18:52:00 +01001517 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001518 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001519 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001520 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001521 }
1522
Christopher Faulete0768eb2018-10-03 16:38:02 +02001523 rep->analysers &= AN_RES_FLT_END;
1524 txn->status = 502;
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001525 stream_inc_http_fail_ctr(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001526 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001527 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001528
1529 if (!(s->flags & SF_ERR_MASK))
1530 s->flags |= SF_ERR_SRVCL;
1531 if (!(s->flags & SF_FINST_MASK))
1532 s->flags |= SF_FINST_H;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001533 DBG_TRACE_DEVEL("leaving on error",
1534 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001535 return 0;
1536 }
1537
Christopher Faulet9768c262018-10-22 09:34:31 +02001538 /* 5: write error to client (we don't send any message then) */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001539 else if (rep->flags & CF_WRITE_ERROR) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001540 if (txn->flags & TX_NOT_FIRST)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001541 goto abort_keep_alive;
1542
Olivier Houcharda798bf52019-03-08 18:52:00 +01001543 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001544 if (objt_server(s->target))
1545 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001546 rep->analysers &= AN_RES_FLT_END;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001547
1548 if (!(s->flags & SF_ERR_MASK))
1549 s->flags |= SF_ERR_CLICL;
1550 if (!(s->flags & SF_FINST_MASK))
1551 s->flags |= SF_FINST_H;
1552
1553 /* process_stream() will take care of the error */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001554 DBG_TRACE_DEVEL("leaving on error",
1555 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001556 return 0;
1557 }
1558
1559 channel_dont_close(rep);
1560 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001561 DBG_TRACE_DEVEL("waiting for more data",
1562 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001563 return 0;
1564 }
1565
1566 /* More interesting part now : we know that we have a complete
1567 * response which at least looks like HTTP. We have an indicator
1568 * of each header's length, so we can parse them quickly.
1569 */
Christopher Faulet29f17582019-05-23 11:03:26 +02001570 BUG_ON(htx_get_first_type(htx) != HTX_BLK_RES_SL);
Christopher Faulet297fbb42019-05-13 14:41:27 +02001571 sl = http_get_stline(htx);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001572
Christopher Faulet5b82cc52020-10-12 15:18:50 +02001573 if ((si_b->flags & SI_FL_L7_RETRY) &&
1574 l7_status_match(s->be, sl->info.res.status) &&
1575 do_l7_retry(s, si_b) == 0) {
1576 DBG_TRACE_DEVEL("leaving on L7 retry", STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
1577 return 0;
1578 }
1579 b_free(&s->si[1].l7_buffer);
1580
1581 msg->msg_state = HTTP_MSG_BODY;
1582
Christopher Faulet9768c262018-10-22 09:34:31 +02001583 /* 0: we might have to print this header in debug mode */
1584 if (unlikely((global.mode & MODE_DEBUG) &&
1585 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
1586 int32_t pos;
1587
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001588 http_debug_stline("srvrep", s, sl);
Christopher Faulet9768c262018-10-22 09:34:31 +02001589
Christopher Fauleta3f15502019-05-13 15:27:23 +02001590 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001591 struct htx_blk *blk = htx_get_blk(htx, pos);
1592 enum htx_blk_type type = htx_get_blk_type(blk);
1593
1594 if (type == HTX_BLK_EOH)
1595 break;
1596 if (type != HTX_BLK_HDR)
1597 continue;
1598
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001599 http_debug_hdr("srvhdr", s,
1600 htx_get_blk_name(htx, blk),
1601 htx_get_blk_value(htx, blk));
Christopher Faulet9768c262018-10-22 09:34:31 +02001602 }
1603 }
1604
Christopher Faulet03599112018-11-27 11:21:21 +01001605 /* 1: get the status code and the version. Also set HTTP flags */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001606 txn->status = sl->info.res.status;
Christopher Faulet03599112018-11-27 11:21:21 +01001607 if (sl->flags & HTX_SL_F_VER_11)
Christopher Faulet9768c262018-10-22 09:34:31 +02001608 msg->flags |= HTTP_MSGF_VER_11;
Christopher Faulet03599112018-11-27 11:21:21 +01001609 if (sl->flags & HTX_SL_F_XFER_LEN) {
1610 msg->flags |= HTTP_MSGF_XFER_LEN;
Christopher Faulet2a408542020-11-20 14:22:37 +01001611 if (sl->flags & HTX_SL_F_CLEN)
1612 msg->flags |= HTTP_MSGF_CNT_LEN;
1613 else if (sl->flags & HTX_SL_F_CHNK)
1614 msg->flags |= HTTP_MSGF_TE_CHNK;
Christopher Faulet03599112018-11-27 11:21:21 +01001615 }
Christopher Faulet2a408542020-11-20 14:22:37 +01001616 if (sl->flags & HTX_SL_F_BODYLESS)
1617 msg->flags |= HTTP_MSGF_BODYLESS;
Christopher Faulet576c3582021-01-08 15:53:01 +01001618 if (sl->flags & HTX_SL_F_CONN_UPG)
1619 msg->flags |= HTTP_MSGF_CONN_UPG;
Christopher Faulet9768c262018-10-22 09:34:31 +02001620
1621 n = txn->status / 100;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001622 if (n < 1 || n > 5)
1623 n = 0;
Christopher Faulet9768c262018-10-22 09:34:31 +02001624
Christopher Faulete0768eb2018-10-03 16:38:02 +02001625 /* when the client triggers a 4xx from the server, it's most often due
1626 * to a missing object or permission. These events should be tracked
1627 * because if they happen often, it may indicate a brute force or a
1628 * vulnerability scan.
1629 */
1630 if (n == 4)
1631 stream_inc_http_err_ctr(s);
1632
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001633 if (n == 5 && txn->status != 501 && txn->status != 505)
1634 stream_inc_http_fail_ctr(s);
1635
Marcin Deranek3c27dda2020-05-15 18:32:51 +02001636 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001637 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.p.http.rsp[n], 1);
Marcin Deranek3c27dda2020-05-15 18:32:51 +02001638 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.p.http.cum_req, 1);
1639 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001640
Christopher Faulete0768eb2018-10-03 16:38:02 +02001641 /* Adjust server's health based on status code. Note: status codes 501
1642 * and 505 are triggered on demand by client request, so we must not
1643 * count them as server failures.
1644 */
1645 if (objt_server(s->target)) {
1646 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001647 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_OK);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001648 else
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001649 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_STS);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001650 }
1651
1652 /*
1653 * We may be facing a 100-continue response, or any other informational
1654 * 1xx response which is non-final, in which case this is not the right
1655 * response, and we're waiting for the next one. Let's allow this response
1656 * to go to the client and wait for the next one. There's an exception for
1657 * 101 which is used later in the code to switch protocols.
1658 */
1659 if (txn->status < 200 &&
1660 (txn->status == 100 || txn->status >= 102)) {
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001661 FLT_STRM_CB(s, flt_http_reset(s, msg));
Christopher Faulet421e7692019-06-13 11:16:45 +02001662 htx->first = channel_htx_fwd_headers(rep, htx);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001663 msg->msg_state = HTTP_MSG_RPBEFORE;
Christopher Faulet3499f622019-09-03 15:23:54 +02001664 msg->flags = 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001665 txn->status = 0;
1666 s->logs.t_data = -1; /* was not a response yet */
Christopher Faulet7d518452020-08-31 11:07:07 +02001667 rep->flags |= CF_SEND_DONTWAIT; /* Send ASAP informational messages */
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001668 goto next_one;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001669 }
1670
Christopher Faulet6e6c7b12021-01-08 16:02:05 +01001671 /* A 101-switching-protocols must contains a Connection header with the
1672 * "upgrade" option and the request too. It means both are agree to
1673 * upgrade. It is not so strict because there is no test on the Upgrade
1674 * header content. But it is probably stronger enough for now.
1675 */
1676 if (txn->status == 101 &&
1677 (!(txn->req.flags & HTTP_MSGF_CONN_UPG) || !(txn->rsp.flags & HTTP_MSGF_CONN_UPG)))
1678 goto return_bad_res;
1679
Christopher Faulete0768eb2018-10-03 16:38:02 +02001680 /*
1681 * 2: check for cacheability.
1682 */
1683
1684 switch (txn->status) {
1685 case 200:
1686 case 203:
1687 case 204:
1688 case 206:
1689 case 300:
1690 case 301:
1691 case 404:
1692 case 405:
1693 case 410:
1694 case 414:
1695 case 501:
1696 break;
1697 default:
1698 /* RFC7231#6.1:
1699 * Responses with status codes that are defined as
1700 * cacheable by default (e.g., 200, 203, 204, 206,
1701 * 300, 301, 404, 405, 410, 414, and 501 in this
1702 * specification) can be reused by a cache with
1703 * heuristic expiration unless otherwise indicated
1704 * by the method definition or explicit cache
1705 * controls [RFC7234]; all other status codes are
1706 * not cacheable by default.
1707 */
1708 txn->flags &= ~(TX_CACHEABLE | TX_CACHE_COOK);
1709 break;
1710 }
1711
1712 /*
1713 * 3: we may need to capture headers
1714 */
1715 s->logs.logwait &= ~LW_RESP;
1716 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->res_cap))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001717 http_capture_headers(htx, s->res_cap, sess->fe->rsp_cap);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001718
Christopher Faulet9768c262018-10-22 09:34:31 +02001719 /* Skip parsing if no content length is possible. */
Christopher Fauletc75668e2020-12-07 18:10:32 +01001720 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status >= 200 && txn->status < 300) ||
Christopher Faulete0768eb2018-10-03 16:38:02 +02001721 txn->status == 101)) {
1722 /* Either we've established an explicit tunnel, or we're
1723 * switching the protocol. In both cases, we're very unlikely
1724 * to understand the next protocols. We have to switch to tunnel
1725 * mode, so that we transfer the request and responses then let
1726 * this protocol pass unmodified. When we later implement specific
1727 * parsers for such protocols, we'll want to check the Upgrade
1728 * header which contains information about that protocol for
1729 * responses with status 101 (eg: see RFC2817 about TLS).
1730 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02001731 txn->flags |= TX_CON_WANT_TUN;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001732 }
1733
Christopher Faulet61608322018-11-23 16:23:45 +01001734 /* check for NTML authentication headers in 401 (WWW-Authenticate) and
1735 * 407 (Proxy-Authenticate) responses and set the connection to private
1736 */
1737 srv_conn = cs_conn(objt_cs(s->si[1].end));
1738 if (srv_conn) {
1739 struct ist hdr;
1740 struct http_hdr_ctx ctx;
1741
1742 if (txn->status == 401)
1743 hdr = ist("WWW-Authenticate");
1744 else if (txn->status == 407)
1745 hdr = ist("Proxy-Authenticate");
1746 else
1747 goto end;
1748
1749 ctx.blk = NULL;
1750 while (http_find_header(htx, hdr, &ctx, 0)) {
Willy Tarreauf1dcced2020-05-07 19:27:02 +02001751 /* If www-authenticate contains "Negotiate", "Nego2", or "NTLM",
1752 * possibly followed by blanks and a base64 string, the connection
1753 * is private. Since it's a mess to deal with, we only check for
1754 * values starting with "NTLM" or "Nego". Note that often multiple
1755 * headers are sent by the server there.
1756 */
1757 if ((ctx.value.len >= 4 && strncasecmp(ctx.value.ptr, "Nego", 4) == 0) ||
Willy Tarreau49a1d282020-05-07 19:10:15 +02001758 (ctx.value.len >= 4 && strncasecmp(ctx.value.ptr, "NTLM", 4) == 0)) {
Olivier Houchard250031e2019-05-29 15:01:50 +02001759 sess->flags |= SESS_FL_PREFER_LAST;
Christopher Faulet08016ab2020-07-01 16:10:06 +02001760 conn_set_owner(srv_conn, sess, NULL);
Christopher Faulet21ddc742020-07-01 15:26:14 +02001761 conn_set_private(srv_conn);
Ilya Shipitsin6b79f382020-07-23 00:32:55 +05001762 /* If it fail now, the same will be done in mux->detach() callback */
Christopher Faulet08016ab2020-07-01 16:10:06 +02001763 session_add_conn(srv_conn->owner, srv_conn, srv_conn->target);
Willy Tarreauf1dcced2020-05-07 19:27:02 +02001764 break;
Olivier Houchard250031e2019-05-29 15:01:50 +02001765 }
Christopher Faulet61608322018-11-23 16:23:45 +01001766 }
1767 }
1768
1769 end:
Christopher Faulete0768eb2018-10-03 16:38:02 +02001770 /* we want to have the response time before we start processing it */
1771 s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now);
1772
1773 /* end of job, return OK */
1774 rep->analysers &= ~an_bit;
1775 rep->analyse_exp = TICK_ETERNITY;
1776 channel_auto_close(rep);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001777 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001778 return 1;
1779
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001780 return_int_err:
Christopher Fauletcff0f732019-12-16 16:13:44 +01001781 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01001782 _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001783 if (sess->listener->counters)
1784 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01001785 if (objt_server(s->target))
1786 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.internal_errors, 1);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001787 txn->status = 500;
1788 if (!(s->flags & SF_ERR_MASK))
1789 s->flags |= SF_ERR_INTERNAL;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001790 goto return_prx_cond;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001791
1792 return_bad_res:
Olivier Houcharda798bf52019-03-08 18:52:00 +01001793 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulet47365272018-10-31 17:40:50 +01001794 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001795 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001796 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Christopher Faulet47365272018-10-31 17:40:50 +01001797 }
Olivier Houcharde3249a92019-05-03 23:01:47 +02001798 if ((s->be->retry_type & PR_RE_JUNK_REQUEST) &&
Olivier Houchardad26d8d2019-05-10 17:48:28 +02001799 (si_b->flags & SI_FL_L7_RETRY) &&
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001800 do_l7_retry(s, si_b) == 0) {
1801 DBG_TRACE_DEVEL("leaving on L7 retry",
1802 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharde3249a92019-05-03 23:01:47 +02001803 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001804 }
Christopher Faulet47365272018-10-31 17:40:50 +01001805 txn->status = 502;
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001806 stream_inc_http_fail_ctr(s);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001807 /* fall through */
1808
Christopher Fauletb8a53712019-12-16 11:29:38 +01001809 return_prx_cond:
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001810 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet47365272018-10-31 17:40:50 +01001811
1812 if (!(s->flags & SF_ERR_MASK))
1813 s->flags |= SF_ERR_PRXCOND;
1814 if (!(s->flags & SF_FINST_MASK))
1815 s->flags |= SF_FINST_H;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001816
1817 s->si[1].flags |= SI_FL_NOLINGER;
1818 rep->analysers &= AN_RES_FLT_END;
Christopher Faulete58c0002020-03-02 16:21:01 +01001819 s->req.analysers &= AN_REQ_FLT_END;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001820 rep->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001821 DBG_TRACE_DEVEL("leaving on error",
1822 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulet47365272018-10-31 17:40:50 +01001823 return 0;
1824
Christopher Faulete0768eb2018-10-03 16:38:02 +02001825 abort_keep_alive:
1826 /* A keep-alive request to the server failed on a network error.
1827 * The client is required to retry. We need to close without returning
1828 * any other information so that the client retries.
1829 */
1830 txn->status = 0;
1831 rep->analysers &= AN_RES_FLT_END;
1832 s->req.analysers &= AN_REQ_FLT_END;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001833 s->logs.logwait = 0;
1834 s->logs.level = 0;
1835 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001836 http_reply_and_close(s, txn->status, NULL);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001837 DBG_TRACE_DEVEL("leaving by closing K/A connection",
1838 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001839 return 0;
1840}
1841
1842/* This function performs all the processing enabled for the current response.
1843 * It normally returns 1 unless it wants to break. It relies on buffers flags,
1844 * and updates s->res.analysers. It might make sense to explode it into several
1845 * other functions. It works like process_request (see indications above).
1846 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001847int http_process_res_common(struct stream *s, struct channel *rep, int an_bit, struct proxy *px)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001848{
1849 struct session *sess = s->sess;
1850 struct http_txn *txn = s->txn;
1851 struct http_msg *msg = &txn->rsp;
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001852 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001853 struct proxy *cur_proxy;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001854 enum rule_result ret = HTTP_RULE_RES_CONT;
1855
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001856 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
1857 return 0;
Christopher Faulet9768c262018-10-22 09:34:31 +02001858
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001859 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001860
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001861 htx = htxbuf(&rep->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001862
1863 /* The stats applet needs to adjust the Connection header but we don't
1864 * apply any filter there.
1865 */
1866 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
1867 rep->analysers &= ~an_bit;
1868 rep->analyse_exp = TICK_ETERNITY;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001869 goto end;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001870 }
1871
1872 /*
1873 * We will have to evaluate the filters.
1874 * As opposed to version 1.2, now they will be evaluated in the
1875 * filters order and not in the header order. This means that
1876 * each filter has to be validated among all headers.
1877 *
1878 * Filters are tried with ->be first, then with ->fe if it is
1879 * different from ->be.
1880 *
1881 * Maybe we are in resume condiion. In this case I choose the
1882 * "struct proxy" which contains the rule list matching the resume
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05001883 * pointer. If none of these "struct proxy" match, I initialise
Christopher Faulete0768eb2018-10-03 16:38:02 +02001884 * the process with the first one.
1885 *
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05001886 * In fact, I check only correspondence between the current list
Christopher Faulete0768eb2018-10-03 16:38:02 +02001887 * pointer and the ->fe rule list. If it doesn't match, I initialize
1888 * the loop with the ->be.
1889 */
1890 if (s->current_rule_list == &sess->fe->http_res_rules)
1891 cur_proxy = sess->fe;
1892 else
1893 cur_proxy = s->be;
1894 while (1) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001895 /* evaluate http-response rules */
1896 if (ret == HTTP_RULE_RES_CONT) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001897 ret = http_res_get_intercept_rule(cur_proxy, &cur_proxy->http_res_rules, s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001898
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001899 switch (ret) {
1900 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
1901 goto return_prx_yield;
1902
1903 case HTTP_RULE_RES_CONT:
1904 case HTTP_RULE_RES_STOP: /* nothing to do */
1905 break;
1906
1907 case HTTP_RULE_RES_DENY: /* deny or tarpit */
1908 goto deny;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001909
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001910 case HTTP_RULE_RES_ABRT: /* abort request, response already sent */
1911 goto return_prx_cond;
1912
1913 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
Christopher Fauletb8a53712019-12-16 11:29:38 +01001914 goto done;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001915
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001916 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
1917 goto return_bad_res;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001918
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001919 case HTTP_RULE_RES_ERROR: /* failed with a bad request */
1920 goto return_int_err;
1921 }
1922
1923 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001924
Christopher Faulete0768eb2018-10-03 16:38:02 +02001925 /* check whether we're already working on the frontend */
1926 if (cur_proxy == sess->fe)
1927 break;
1928 cur_proxy = sess->fe;
1929 }
1930
Christopher Faulete0768eb2018-10-03 16:38:02 +02001931 /* OK that's all we can do for 1xx responses */
1932 if (unlikely(txn->status < 200 && txn->status != 101))
Christopher Fauletf2824e62018-10-01 12:12:37 +02001933 goto end;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001934
1935 /*
1936 * Now check for a server cookie.
1937 */
1938 if (s->be->cookie_name || sess->fe->capture_name || (s->be->options & PR_O_CHK_CACHE))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001939 http_manage_server_side_cookies(s, rep);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001940
1941 /*
1942 * Check for cache-control or pragma headers if required.
1943 */
1944 if ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001945 http_check_response_for_cacheability(s, rep);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001946
1947 /*
1948 * Add server cookie in the response if needed
1949 */
1950 if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) &&
1951 !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) &&
1952 (!(s->flags & SF_DIRECT) ||
1953 ((s->be->cookie_maxidle || txn->cookie_last_date) &&
1954 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
1955 (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
1956 (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
1957 (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
1958 !(s->flags & SF_IGNORE_PRST)) {
1959 /* the server is known, it's not the one the client requested, or the
1960 * cookie's last seen date needs to be refreshed. We have to
1961 * insert a set-cookie here, except if we want to insert only on POST
1962 * requests and this one isn't. Note that servers which don't have cookies
1963 * (eg: some backup servers) will return a full cookie removal request.
1964 */
1965 if (!objt_server(s->target)->cookie) {
1966 chunk_printf(&trash,
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001967 "%s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
Christopher Faulete0768eb2018-10-03 16:38:02 +02001968 s->be->cookie_name);
1969 }
1970 else {
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001971 chunk_printf(&trash, "%s=%s", s->be->cookie_name, objt_server(s->target)->cookie);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001972
1973 if (s->be->cookie_maxidle || s->be->cookie_maxlife) {
1974 /* emit last_date, which is mandatory */
1975 trash.area[trash.data++] = COOKIE_DELIM_DATE;
1976 s30tob64((date.tv_sec+3) >> 2,
1977 trash.area + trash.data);
1978 trash.data += 5;
1979
1980 if (s->be->cookie_maxlife) {
1981 /* emit first_date, which is either the original one or
1982 * the current date.
1983 */
1984 trash.area[trash.data++] = COOKIE_DELIM_DATE;
1985 s30tob64(txn->cookie_first_date ?
1986 txn->cookie_first_date >> 2 :
1987 (date.tv_sec+3) >> 2,
1988 trash.area + trash.data);
1989 trash.data += 5;
1990 }
1991 }
1992 chunk_appendf(&trash, "; path=/");
1993 }
1994
1995 if (s->be->cookie_domain)
1996 chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain);
1997
1998 if (s->be->ck_opts & PR_CK_HTTPONLY)
1999 chunk_appendf(&trash, "; HttpOnly");
2000
2001 if (s->be->ck_opts & PR_CK_SECURE)
2002 chunk_appendf(&trash, "; Secure");
2003
Christopher Faulet2f533902020-01-21 11:06:48 +01002004 if (s->be->cookie_attrs)
2005 chunk_appendf(&trash, "; %s", s->be->cookie_attrs);
2006
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002007 if (unlikely(!http_add_header(htx, ist("Set-Cookie"), ist2(trash.area, trash.data))))
Christopher Fauletb8a53712019-12-16 11:29:38 +01002008 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002009
2010 txn->flags &= ~TX_SCK_MASK;
2011 if (__objt_server(s->target)->cookie && (s->flags & SF_DIRECT))
2012 /* the server did not change, only the date was updated */
2013 txn->flags |= TX_SCK_UPDATED;
2014 else
2015 txn->flags |= TX_SCK_INSERTED;
2016
2017 /* Here, we will tell an eventual cache on the client side that we don't
2018 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
2019 * Some caches understand the correct form: 'no-cache="set-cookie"', but
2020 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
2021 */
2022 if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
2023
2024 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
2025
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002026 if (unlikely(!http_add_header(htx, ist("Cache-control"), ist("private"))))
Christopher Fauletb8a53712019-12-16 11:29:38 +01002027 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002028 }
2029 }
2030
2031 /*
2032 * Check if result will be cacheable with a cookie.
2033 * We'll block the response if security checks have caught
2034 * nasty things such as a cacheable cookie.
2035 */
2036 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
2037 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
2038 (s->be->options & PR_O_CHK_CACHE)) {
2039 /* we're in presence of a cacheable response containing
2040 * a set-cookie header. We'll block it as requested by
2041 * the 'checkcache' option, and send an alert.
2042 */
Christopher Faulete0768eb2018-10-03 16:38:02 +02002043 ha_alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
2044 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
2045 send_log(s->be, LOG_ALERT,
2046 "Blocking cacheable cookie in response from instance %s, server %s.\n",
2047 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
Christopher Fauletb8a53712019-12-16 11:29:38 +01002048 goto deny;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002049 }
2050
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002051 end:
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002052 /*
2053 * Evaluate after-response rules before forwarding the response. rules
2054 * from the backend are evaluated first, then one from the frontend if
2055 * it differs.
2056 */
2057 if (!http_eval_after_res_rules(s))
2058 goto return_int_err;
2059
Christopher Faulete0768eb2018-10-03 16:38:02 +02002060 /* Always enter in the body analyzer */
2061 rep->analysers &= ~AN_RES_FLT_XFER_DATA;
2062 rep->analysers |= AN_RES_HTTP_XFER_BODY;
2063
2064 /* if the user wants to log as soon as possible, without counting
2065 * bytes from the server, then this is the right moment. We have
2066 * to temporarily assign bytes_out to log what we currently have.
2067 */
2068 if (!LIST_ISEMPTY(&sess->fe->logformat) && !(s->logs.logwait & LW_BYTES)) {
2069 s->logs.t_close = s->logs.t_data; /* to get a valid end date */
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002070 s->logs.bytes_out = htx->data;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002071 s->do_log(s);
2072 s->logs.bytes_out = 0;
2073 }
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002074
Christopher Fauletb8a53712019-12-16 11:29:38 +01002075 done:
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002076 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002077 rep->analysers &= ~an_bit;
2078 rep->analyse_exp = TICK_ETERNITY;
2079 return 1;
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002080
Christopher Fauletb8a53712019-12-16 11:29:38 +01002081 deny:
Christopher Fauletb8a53712019-12-16 11:29:38 +01002082 _HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_resp, 1);
Christopher Fauleta08546b2019-12-16 16:07:34 +01002083 _HA_ATOMIC_ADD(&s->be->be_counters.denied_resp, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002084 if (sess->listener->counters)
2085 _HA_ATOMIC_ADD(&sess->listener->counters->denied_resp, 1);
Christopher Fauleta08546b2019-12-16 16:07:34 +01002086 if (objt_server(s->target))
2087 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.denied_resp, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002088 goto return_prx_err;
2089
2090 return_int_err:
2091 txn->status = 500;
2092 if (!(s->flags & SF_ERR_MASK))
2093 s->flags |= SF_ERR_INTERNAL;
Christopher Fauletcff0f732019-12-16 16:13:44 +01002094 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002095 _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1);
2096 if (objt_server(s->target))
2097 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01002098 if (objt_server(s->target))
2099 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002100 goto return_prx_err;
2101
2102 return_bad_res:
2103 txn->status = 502;
Willy Tarreau826f3ab2021-02-10 12:07:15 +01002104 stream_inc_http_fail_ctr(s);
Christopher Fauleta20a6532020-02-05 10:16:41 +01002105 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
2106 if (objt_server(s->target)) {
2107 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
2108 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_RSP);
2109 }
Christopher Fauletb8a53712019-12-16 11:29:38 +01002110 /* fall through */
2111
2112 return_prx_err:
2113 http_reply_and_close(s, txn->status, http_error_message(s));
2114 /* fall through */
2115
2116 return_prx_cond:
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002117 s->logs.t_data = -1; /* was not a valid response */
2118 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002119
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002120 if (!(s->flags & SF_ERR_MASK))
2121 s->flags |= SF_ERR_PRXCOND;
2122 if (!(s->flags & SF_FINST_MASK))
2123 s->flags |= SF_FINST_H;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002124
Christopher Faulete58c0002020-03-02 16:21:01 +01002125 rep->analysers &= AN_RES_FLT_END;
2126 s->req.analysers &= AN_REQ_FLT_END;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002127 rep->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002128 DBG_TRACE_DEVEL("leaving on error",
2129 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002130 return 0;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002131
2132 return_prx_yield:
2133 channel_dont_close(rep);
2134 DBG_TRACE_DEVEL("waiting for more data",
2135 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
2136 return 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002137}
2138
2139/* This function is an analyser which forwards response body (including chunk
2140 * sizes if any). It is called as soon as we must forward, even if we forward
2141 * zero byte. The only situation where it must not be called is when we're in
2142 * tunnel mode and we want to forward till the close. It's used both to forward
2143 * remaining data and to resync after end of body. It expects the msg_state to
2144 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
2145 * read more data, or 1 once we can go on with next request or end the stream.
2146 *
2147 * It is capable of compressing response data both in content-length mode and
2148 * in chunked mode. The state machines follows different flows depending on
2149 * whether content-length and chunked modes are used, since there are no
2150 * trailers in content-length :
2151 *
2152 * chk-mode cl-mode
2153 * ,----- BODY -----.
2154 * / \
2155 * V size > 0 V chk-mode
2156 * .--> SIZE -------------> DATA -------------> CRLF
2157 * | | size == 0 | last byte |
2158 * | v final crlf v inspected |
2159 * | TRAILERS -----------> DONE |
2160 * | |
2161 * `----------------------------------------------'
2162 *
2163 * Compression only happens in the DATA state, and must be flushed in final
2164 * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding
2165 * is performed at once on final states for all bytes parsed, or when leaving
2166 * on missing data.
2167 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002168int http_response_forward_body(struct stream *s, struct channel *res, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +02002169{
2170 struct session *sess = s->sess;
2171 struct http_txn *txn = s->txn;
2172 struct http_msg *msg = &s->txn->rsp;
Christopher Faulet9768c262018-10-22 09:34:31 +02002173 struct htx *htx;
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002174 int ret;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002175
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002176 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002177
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002178 htx = htxbuf(&res->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002179
Christopher Fauletb9a92f32019-09-09 10:15:21 +02002180 if (htx->flags & HTX_FL_PARSING_ERROR)
2181 goto return_bad_res;
2182 if (htx->flags & HTX_FL_PROCESSING_ERROR)
2183 goto return_int_err;
2184
Christopher Faulete0768eb2018-10-03 16:38:02 +02002185 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Christopher Fauletf2824e62018-10-01 12:12:37 +02002186 ((res->flags & CF_SHUTW) && (res->to_forward || co_data(res)))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02002187 /* Output closed while we were sending data. We must abort and
2188 * wake the other side up.
2189 */
Christopher Faulete0768eb2018-10-03 16:38:02 +02002190 msg->msg_state = HTTP_MSG_ERROR;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002191 http_end_response(s);
2192 http_end_request(s);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002193 DBG_TRACE_DEVEL("leaving on error",
2194 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002195 return 1;
2196 }
2197
Christopher Faulet9768c262018-10-22 09:34:31 +02002198 if (msg->msg_state == HTTP_MSG_BODY)
2199 msg->msg_state = HTTP_MSG_DATA;
2200
Christopher Faulete0768eb2018-10-03 16:38:02 +02002201 /* in most states, we should abort in case of early close */
2202 channel_auto_close(res);
2203
Christopher Faulete0768eb2018-10-03 16:38:02 +02002204 if (res->to_forward) {
Christopher Faulet66af0b22019-03-22 14:54:52 +01002205 if (res->to_forward == CHN_INFINITE_FORWARD) {
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002206 if (res->flags & CF_EOI)
2207 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet66af0b22019-03-22 14:54:52 +01002208 }
2209 else {
2210 /* We can't process the buffer's contents yet */
2211 res->flags |= CF_WAKE_WRITE;
2212 goto missing_data_or_waiting;
2213 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02002214 }
2215
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002216 if (msg->msg_state >= HTTP_MSG_ENDING)
2217 goto ending;
2218
Christopher Fauletc75668e2020-12-07 18:10:32 +01002219 if ((txn->meth == HTTP_METH_CONNECT && txn->status >= 200 && txn->status < 300) || txn->status == 101 ||
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002220 (!(msg->flags & HTTP_MSGF_XFER_LEN) && !HAS_RSP_DATA_FILTERS(s))) {
2221 msg->msg_state = HTTP_MSG_ENDING;
2222 goto ending;
2223 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002224
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002225 /* Forward input data. We get it by removing all outgoing data not
2226 * forwarded yet from HTX data size. If there are some data filters, we
2227 * let them decide the amount of data to forward.
Christopher Faulet9768c262018-10-22 09:34:31 +02002228 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002229 if (HAS_RSP_DATA_FILTERS(s)) {
2230 ret = flt_http_payload(s, msg, htx->data);
2231 if (ret < 0)
2232 goto return_bad_res;
Christopher Faulet421e7692019-06-13 11:16:45 +02002233 c_adv(res, ret);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002234 }
2235 else {
Christopher Faulet421e7692019-06-13 11:16:45 +02002236 c_adv(res, htx->data - co_data(res));
Christopher Faulet66af0b22019-03-22 14:54:52 +01002237 if (msg->flags & HTTP_MSGF_XFER_LEN)
2238 channel_htx_forward_forever(res, htx);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002239 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002240
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002241 if (htx->data != co_data(res))
2242 goto missing_data_or_waiting;
2243
2244 if (!(msg->flags & HTTP_MSGF_XFER_LEN) && res->flags & CF_SHUTR) {
2245 msg->msg_state = HTTP_MSG_ENDING;
2246 goto ending;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002247 }
2248
Christopher Faulet9768c262018-10-22 09:34:31 +02002249 /* Check if the end-of-message is reached and if so, switch the message
Christopher Fauletd20fdb02019-06-13 16:43:22 +02002250 * in HTTP_MSG_ENDING state. Then if all data was marked to be
2251 * forwarded, set the state to HTTP_MSG_DONE.
Christopher Faulet9768c262018-10-22 09:34:31 +02002252 */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002253 if (!(htx->flags & HTX_FL_EOM))
Christopher Faulet9768c262018-10-22 09:34:31 +02002254 goto missing_data_or_waiting;
2255
Christopher Fauletd20fdb02019-06-13 16:43:22 +02002256 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet9768c262018-10-22 09:34:31 +02002257
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002258 ending:
Christopher Faulet2151cdd2020-07-22 16:34:59 +02002259 res->flags &= ~CF_EXPECT_MORE; /* no more data are expected */
2260
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002261 /* other states, ENDING...TUNNEL */
2262 if (msg->msg_state >= HTTP_MSG_DONE)
2263 goto done;
Christopher Faulet9768c262018-10-22 09:34:31 +02002264
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002265 if (HAS_RSP_DATA_FILTERS(s)) {
2266 ret = flt_http_end(s, msg);
2267 if (ret <= 0) {
2268 if (!ret)
2269 goto missing_data_or_waiting;
2270 goto return_bad_res;
2271 }
2272 }
2273
Christopher Fauletc75668e2020-12-07 18:10:32 +01002274 if ((txn->meth == HTTP_METH_CONNECT && txn->status >= 200 && txn->status < 300) || txn->status == 101 ||
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002275 !(msg->flags & HTTP_MSGF_XFER_LEN)) {
2276 msg->msg_state = HTTP_MSG_TUNNEL;
2277 goto ending;
2278 }
2279 else {
2280 msg->msg_state = HTTP_MSG_DONE;
2281 res->to_forward = 0;
2282 }
2283
2284 done:
2285
2286 channel_dont_close(res);
2287
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002288 http_end_response(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002289 if (!(res->analysers & an_bit)) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002290 http_end_request(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002291 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
2292 if (res->flags & CF_SHUTW) {
2293 /* response errors are most likely due to the
2294 * client aborting the transfer. */
Christopher Faulet93e02d82019-03-08 14:18:50 +01002295 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002296 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02002297 goto return_bad_res;
2298 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002299 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002300 return 1;
2301 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002302 DBG_TRACE_DEVEL("waiting for the end of the HTTP txn",
2303 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002304 return 0;
2305
2306 missing_data_or_waiting:
2307 if (res->flags & CF_SHUTW)
Christopher Faulet93e02d82019-03-08 14:18:50 +01002308 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002309
2310 /* stop waiting for data if the input is closed before the end. If the
2311 * client side was already closed, it means that the client has aborted,
2312 * so we don't want to count this as a server abort. Otherwise it's a
2313 * server abort.
2314 */
Christopher Fauletd20fdb02019-06-13 16:43:22 +02002315 if (msg->msg_state < HTTP_MSG_ENDING && res->flags & CF_SHUTR) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02002316 if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))
Christopher Faulet93e02d82019-03-08 14:18:50 +01002317 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002318 /* If we have some pending data, we continue the processing */
Christopher Faulet93e02d82019-03-08 14:18:50 +01002319 if (htx_is_empty(htx))
2320 goto return_srv_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002321 }
2322
Christopher Faulete0768eb2018-10-03 16:38:02 +02002323 /* When TE: chunked is used, we need to get there again to parse
2324 * remaining chunks even if the server has closed, so we don't want to
Christopher Faulet9768c262018-10-22 09:34:31 +02002325 * set CF_DONTCLOSE. Similarly when there is a content-leng or if there
2326 * are filters registered on the stream, we don't want to forward a
2327 * close
Christopher Faulete0768eb2018-10-03 16:38:02 +02002328 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002329 if ((msg->flags & HTTP_MSGF_XFER_LEN) || HAS_RSP_DATA_FILTERS(s))
Christopher Faulete0768eb2018-10-03 16:38:02 +02002330 channel_dont_close(res);
2331
2332 /* We know that more data are expected, but we couldn't send more that
2333 * what we did. So we always set the CF_EXPECT_MORE flag so that the
2334 * system knows it must not set a PUSH on this first part. Interactive
2335 * modes are already handled by the stream sock layer. We must not do
2336 * this in content-length mode because it could present the MSG_MORE
2337 * flag with the last block of forwarded data, which would cause an
2338 * additional delay to be observed by the receiver.
2339 */
Christopher Faulet2151cdd2020-07-22 16:34:59 +02002340 if (HAS_RSP_DATA_FILTERS(s))
Christopher Faulete0768eb2018-10-03 16:38:02 +02002341 res->flags |= CF_EXPECT_MORE;
2342
2343 /* the stream handler will take care of timeouts and errors */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002344 DBG_TRACE_DEVEL("waiting for more data to forward",
2345 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002346 return 0;
2347
Christopher Faulet93e02d82019-03-08 14:18:50 +01002348 return_srv_abort:
2349 _HA_ATOMIC_ADD(&sess->fe->fe_counters.srv_aborts, 1);
2350 _HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01002351 if (sess->listener->counters)
2352 _HA_ATOMIC_ADD(&sess->listener->counters->srv_aborts, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002353 if (objt_server(s->target))
Christopher Fauletcff0f732019-12-16 16:13:44 +01002354 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.srv_aborts, 1);
Willy Tarreau826f3ab2021-02-10 12:07:15 +01002355 stream_inc_http_fail_ctr(s);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002356 if (!(s->flags & SF_ERR_MASK))
2357 s->flags |= SF_ERR_SRVCL;
2358 goto return_error;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002359
Christopher Faulet93e02d82019-03-08 14:18:50 +01002360 return_cli_abort:
2361 _HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
2362 _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01002363 if (sess->listener->counters)
2364 _HA_ATOMIC_ADD(&sess->listener->counters->cli_aborts, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002365 if (objt_server(s->target))
Christopher Fauletcff0f732019-12-16 16:13:44 +01002366 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.cli_aborts, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002367 if (!(s->flags & SF_ERR_MASK))
2368 s->flags |= SF_ERR_CLICL;
2369 goto return_error;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002370
Christopher Fauletb9a92f32019-09-09 10:15:21 +02002371 return_int_err:
Christopher Fauletcff0f732019-12-16 16:13:44 +01002372 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002373 _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01002374 if (sess->listener->counters)
2375 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002376 if (objt_server(s->target))
2377 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.internal_errors, 1);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02002378 if (!(s->flags & SF_ERR_MASK))
2379 s->flags |= SF_ERR_INTERNAL;
2380 goto return_error;
2381
Christopher Faulet93e02d82019-03-08 14:18:50 +01002382 return_bad_res:
2383 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
2384 if (objt_server(s->target)) {
Christopher Fauletcff0f732019-12-16 16:13:44 +01002385 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002386 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_RSP);
2387 }
Willy Tarreau826f3ab2021-02-10 12:07:15 +01002388 stream_inc_http_fail_ctr(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002389 if (!(s->flags & SF_ERR_MASK))
Christopher Faulet93e02d82019-03-08 14:18:50 +01002390 s->flags |= SF_ERR_SRVCL;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002391 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +02002392
Christopher Faulet93e02d82019-03-08 14:18:50 +01002393 return_error:
Christopher Faulete0768eb2018-10-03 16:38:02 +02002394 /* don't send any error message as we're in the body */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002395 http_reply_and_close(s, txn->status, NULL);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002396 res->analysers &= AN_RES_FLT_END;
2397 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 +02002398 if (!(s->flags & SF_FINST_MASK))
2399 s->flags |= SF_FINST_D;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002400 DBG_TRACE_DEVEL("leaving on error",
2401 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002402 return 0;
2403}
2404
Christopher Fauletf2824e62018-10-01 12:12:37 +02002405/* Perform an HTTP redirect based on the information in <rule>. The function
Christopher Faulet99daf282018-11-28 22:58:13 +01002406 * returns zero on success, or zero in case of a, irrecoverable error such
Christopher Fauletf2824e62018-10-01 12:12:37 +02002407 * as too large a request to build a valid response.
2408 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002409int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn)
Christopher Fauletf2824e62018-10-01 12:12:37 +02002410{
Christopher Faulet99daf282018-11-28 22:58:13 +01002411 struct channel *req = &s->req;
2412 struct channel *res = &s->res;
2413 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01002414 struct htx_sl *sl;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002415 struct buffer *chunk;
Christopher Faulet99daf282018-11-28 22:58:13 +01002416 struct ist status, reason, location;
2417 unsigned int flags;
Christopher Faulet08e66462019-05-23 16:44:59 +02002418 int close = 0; /* Try to keep the connection alive byt default */
Christopher Fauletf2824e62018-10-01 12:12:37 +02002419
2420 chunk = alloc_trash_chunk();
Christopher Fauletb8a53712019-12-16 11:29:38 +01002421 if (!chunk) {
2422 if (!(s->flags & SF_ERR_MASK))
2423 s->flags |= SF_ERR_RESOURCE;
Christopher Faulet99daf282018-11-28 22:58:13 +01002424 goto fail;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002425 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002426
Christopher Faulet99daf282018-11-28 22:58:13 +01002427 /*
2428 * Create the location
2429 */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002430 htx = htxbuf(&req->buf);
Christopher Fauletf2824e62018-10-01 12:12:37 +02002431 switch(rule->type) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002432 case REDIRECT_TYPE_SCHEME: {
2433 struct http_hdr_ctx ctx;
2434 struct ist path, host;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002435
Christopher Faulet99daf282018-11-28 22:58:13 +01002436 host = ist("");
2437 ctx.blk = NULL;
2438 if (http_find_header(htx, ist("Host"), &ctx, 0))
2439 host = ctx.value;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002440
Christopher Faulet297fbb42019-05-13 14:41:27 +02002441 sl = http_get_stline(htx);
Christopher Faulet99daf282018-11-28 22:58:13 +01002442 path = http_get_path(htx_sl_req_uri(sl));
2443 /* build message using path */
Tim Duesterhused526372020-03-05 17:56:33 +01002444 if (isttest(path)) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002445 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2446 int qs = 0;
2447 while (qs < path.len) {
2448 if (*(path.ptr + qs) == '?') {
2449 path.len = qs;
2450 break;
2451 }
2452 qs++;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002453 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002454 }
2455 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002456 else
2457 path = ist("/");
Christopher Fauletf2824e62018-10-01 12:12:37 +02002458
Christopher Faulet99daf282018-11-28 22:58:13 +01002459 if (rule->rdr_str) { /* this is an old "redirect" rule */
2460 /* add scheme */
2461 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2462 goto fail;
2463 }
2464 else {
2465 /* add scheme with executing log format */
2466 chunk->data += build_logline(s, chunk->area + chunk->data,
2467 chunk->size - chunk->data,
2468 &rule->rdr_fmt);
2469 }
2470 /* add "://" + host + path */
2471 if (!chunk_memcat(chunk, "://", 3) ||
2472 !chunk_memcat(chunk, host.ptr, host.len) ||
2473 !chunk_memcat(chunk, path.ptr, path.len))
2474 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002475
Christopher Faulet99daf282018-11-28 22:58:13 +01002476 /* append a slash at the end of the location if needed and missing */
2477 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
2478 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
2479 if (chunk->data + 1 >= chunk->size)
2480 goto fail;
2481 chunk->area[chunk->data++] = '/';
2482 }
2483 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002484 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002485
Christopher Faulet99daf282018-11-28 22:58:13 +01002486 case REDIRECT_TYPE_PREFIX: {
2487 struct ist path;
2488
Christopher Faulet297fbb42019-05-13 14:41:27 +02002489 sl = http_get_stline(htx);
Christopher Faulet99daf282018-11-28 22:58:13 +01002490 path = http_get_path(htx_sl_req_uri(sl));
2491 /* build message using path */
Tim Duesterhused526372020-03-05 17:56:33 +01002492 if (isttest(path)) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002493 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2494 int qs = 0;
2495 while (qs < path.len) {
2496 if (*(path.ptr + qs) == '?') {
2497 path.len = qs;
2498 break;
2499 }
2500 qs++;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002501 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002502 }
2503 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002504 else
2505 path = ist("/");
Christopher Fauletf2824e62018-10-01 12:12:37 +02002506
Christopher Faulet99daf282018-11-28 22:58:13 +01002507 if (rule->rdr_str) { /* this is an old "redirect" rule */
2508 /* add prefix. Note that if prefix == "/", we don't want to
2509 * add anything, otherwise it makes it hard for the user to
2510 * configure a self-redirection.
2511 */
2512 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
2513 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2514 goto fail;
2515 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002516 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002517 else {
2518 /* add prefix with executing log format */
2519 chunk->data += build_logline(s, chunk->area + chunk->data,
2520 chunk->size - chunk->data,
2521 &rule->rdr_fmt);
2522 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002523
Christopher Faulet99daf282018-11-28 22:58:13 +01002524 /* add path */
2525 if (!chunk_memcat(chunk, path.ptr, path.len))
2526 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002527
Christopher Faulet99daf282018-11-28 22:58:13 +01002528 /* append a slash at the end of the location if needed and missing */
2529 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
2530 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
2531 if (chunk->data + 1 >= chunk->size)
2532 goto fail;
2533 chunk->area[chunk->data++] = '/';
2534 }
2535 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002536 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002537 case REDIRECT_TYPE_LOCATION:
2538 default:
2539 if (rule->rdr_str) { /* this is an old "redirect" rule */
2540 /* add location */
2541 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2542 goto fail;
2543 }
2544 else {
2545 /* add location with executing log format */
2546 chunk->data += build_logline(s, chunk->area + chunk->data,
2547 chunk->size - chunk->data,
2548 &rule->rdr_fmt);
2549 }
2550 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002551 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002552 location = ist2(chunk->area, chunk->data);
2553
2554 /*
2555 * Create the 30x response
2556 */
2557 switch (rule->code) {
2558 case 308:
2559 status = ist("308");
2560 reason = ist("Permanent Redirect");
2561 break;
2562 case 307:
2563 status = ist("307");
2564 reason = ist("Temporary Redirect");
2565 break;
2566 case 303:
2567 status = ist("303");
2568 reason = ist("See Other");
2569 break;
2570 case 301:
2571 status = ist("301");
2572 reason = ist("Moved Permanently");
2573 break;
2574 case 302:
2575 default:
2576 status = ist("302");
2577 reason = ist("Found");
2578 break;
2579 }
2580
Christopher Faulet08e66462019-05-23 16:44:59 +02002581 if (!(txn->req.flags & HTTP_MSGF_BODYLESS) && txn->req.msg_state != HTTP_MSG_DONE)
2582 close = 1;
2583
Christopher Faulet99daf282018-11-28 22:58:13 +01002584 htx = htx_from_buf(&res->buf);
Kevin Zhu96b36392020-01-07 09:42:55 +01002585 /* Trim any possible response */
2586 channel_htx_truncate(&s->res, htx);
Christopher Faulet99daf282018-11-28 22:58:13 +01002587 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
2588 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), status, reason);
2589 if (!sl)
2590 goto fail;
2591 sl->info.res.status = rule->code;
2592 s->txn->status = rule->code;
2593
Christopher Faulet08e66462019-05-23 16:44:59 +02002594 if (close && !htx_add_header(htx, ist("Connection"), ist("close")))
2595 goto fail;
2596
2597 if (!htx_add_header(htx, ist("Content-length"), ist("0")) ||
Christopher Faulet99daf282018-11-28 22:58:13 +01002598 !htx_add_header(htx, ist("Location"), location))
2599 goto fail;
2600
2601 if (rule->code == 302 || rule->code == 303 || rule->code == 307) {
2602 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")))
2603 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002604 }
2605
2606 if (rule->cookie_len) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002607 if (!htx_add_header(htx, ist("Set-Cookie"), ist2(rule->cookie_str, rule->cookie_len)))
2608 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002609 }
2610
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002611 if (!htx_add_endof(htx, HTX_BLK_EOH))
Christopher Faulet99daf282018-11-28 22:58:13 +01002612 goto fail;
2613
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002614 htx->flags |= HTX_FL_EOM;
Kevin Zhu96b36392020-01-07 09:42:55 +01002615 htx_to_buf(htx, &res->buf);
Christopher Fauleta72a7e42020-01-28 09:28:11 +01002616 if (!http_forward_proxy_resp(s, 1))
2617 goto fail;
Christopher Faulet99daf282018-11-28 22:58:13 +01002618
Christopher Faulet60b33a52020-01-28 09:18:10 +01002619 if (rule->flags & REDIRECT_FLAG_FROM_REQ) {
2620 /* let's log the request time */
2621 s->logs.tv_request = now;
2622 req->analysers &= AN_REQ_FLT_END;
Christopher Faulet99daf282018-11-28 22:58:13 +01002623
Christopher Faulet60b33a52020-01-28 09:18:10 +01002624 if (s->sess->fe == s->be) /* report it if the request was intercepted by the frontend */
2625 _HA_ATOMIC_ADD(&s->sess->fe->fe_counters.intercepted_req, 1);
2626 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002627
2628 if (!(s->flags & SF_ERR_MASK))
2629 s->flags |= SF_ERR_LOCAL;
2630 if (!(s->flags & SF_FINST_MASK))
Christopher Faulet60b33a52020-01-28 09:18:10 +01002631 s->flags |= ((rule->flags & REDIRECT_FLAG_FROM_REQ) ? SF_FINST_R : SF_FINST_H);
Christopher Fauletf2824e62018-10-01 12:12:37 +02002632
Christopher Faulet99daf282018-11-28 22:58:13 +01002633 free_trash_chunk(chunk);
2634 return 1;
2635
2636 fail:
2637 /* If an error occurred, remove the incomplete HTTP response from the
2638 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01002639 channel_htx_truncate(res, htxbuf(&res->buf));
Christopher Fauletf2824e62018-10-01 12:12:37 +02002640 free_trash_chunk(chunk);
Christopher Faulet99daf282018-11-28 22:58:13 +01002641 return 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002642}
2643
Christopher Faulet92d34fe2019-12-17 09:20:34 +01002644/* Replace all headers matching the name <name>. The header value is replaced if
2645 * it matches the regex <re>. <str> is used for the replacement. If <full> is
2646 * set to 1, the full-line is matched and replaced. Otherwise, comma-separated
2647 * values are evaluated one by one. It returns 0 on success and -1 on error.
2648 */
2649int http_replace_hdrs(struct stream* s, struct htx *htx, struct ist name,
2650 const char *str, struct my_regex *re, int full)
Christopher Faulet72333522018-10-24 11:25:02 +02002651{
2652 struct http_hdr_ctx ctx;
2653 struct buffer *output = get_trash_chunk();
2654
Christopher Faulet72333522018-10-24 11:25:02 +02002655 ctx.blk = NULL;
Christopher Faulet92d34fe2019-12-17 09:20:34 +01002656 while (http_find_header(htx, name, &ctx, full)) {
Christopher Faulet72333522018-10-24 11:25:02 +02002657 if (!regex_exec_match2(re, ctx.value.ptr, ctx.value.len, MAX_MATCH, pmatch, 0))
2658 continue;
2659
2660 output->data = exp_replace(output->area, output->size, ctx.value.ptr, str, pmatch);
2661 if (output->data == -1)
2662 return -1;
2663 if (!http_replace_header_value(htx, &ctx, ist2(output->area, output->data)))
2664 return -1;
2665 }
2666 return 0;
2667}
2668
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002669/* This function executes one of the set-{method,path,query,uri} actions. It
2670 * takes the string from the variable 'replace' with length 'len', then modifies
2671 * the relevant part of the request line accordingly. Then it updates various
2672 * pointers to the next elements which were moved, and the total buffer length.
2673 * It finds the action to be performed in p[2], previously filled by function
2674 * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal
2675 * error, though this can be revisited when this code is finally exploited.
2676 *
2677 * 'action' can be '0' to replace method, '1' to replace path, '2' to replace
Christopher Faulet312294f2020-09-02 17:17:44 +02002678 * query string, 3 to replace uri or 4 to replace the path+query.
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002679 *
2680 * In query string case, the mark question '?' must be set at the start of the
2681 * string by the caller, event if the replacement query string is empty.
2682 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002683int http_req_replace_stline(int action, const char *replace, int len,
2684 struct proxy *px, struct stream *s)
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002685{
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002686 struct htx *htx = htxbuf(&s->req.buf);
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002687
2688 switch (action) {
2689 case 0: // method
2690 if (!http_replace_req_meth(htx, ist2(replace, len)))
2691 return -1;
2692 break;
2693
2694 case 1: // path
Christopher Fauletb8ce5052020-08-31 16:11:57 +02002695 if (!http_replace_req_path(htx, ist2(replace, len), 0))
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002696 return -1;
2697 break;
2698
2699 case 2: // query
2700 if (!http_replace_req_query(htx, ist2(replace, len)))
2701 return -1;
2702 break;
2703
2704 case 3: // uri
2705 if (!http_replace_req_uri(htx, ist2(replace, len)))
2706 return -1;
2707 break;
2708
Christopher Faulet312294f2020-09-02 17:17:44 +02002709 case 4: // path + query
2710 if (!http_replace_req_path(htx, ist2(replace, len), 1))
2711 return -1;
2712 break;
2713
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002714 default:
2715 return -1;
2716 }
2717 return 0;
2718}
2719
2720/* This function replace the HTTP status code and the associated message. The
Christopher Faulete00d06c2019-12-16 17:18:42 +01002721 * variable <status> contains the new status code. This function never fails. It
2722 * returns 0 in case of success, -1 in case of internal error.
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002723 */
Christopher Faulet96bff762019-12-17 13:46:18 +01002724int http_res_set_status(unsigned int status, struct ist reason, struct stream *s)
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002725{
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002726 struct htx *htx = htxbuf(&s->res.buf);
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002727 char *res;
2728
2729 chunk_reset(&trash);
2730 res = ultoa_o(status, trash.area, trash.size);
2731 trash.data = res - trash.area;
2732
2733 /* Do we have a custom reason format string? */
Tim Duesterhuse296d3e2020-03-05 17:56:31 +01002734 if (!isttest(reason)) {
Christopher Faulet96bff762019-12-17 13:46:18 +01002735 const char *str = http_get_reason(status);
2736 reason = ist2(str, strlen(str));
2737 }
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002738
Christopher Fauletbde2c4c2020-08-31 16:43:34 +02002739 if (!http_replace_res_status(htx, ist2(trash.area, trash.data), reason))
Christopher Faulete00d06c2019-12-16 17:18:42 +01002740 return -1;
2741 return 0;
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002742}
2743
Christopher Faulet3e964192018-10-24 11:39:23 +02002744/* Executes the http-request rules <rules> for stream <s>, proxy <px> and
2745 * transaction <txn>. Returns the verdict of the first rule that prevents
2746 * further processing of the request (auth, deny, ...), and defaults to
2747 * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or
2748 * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT
2749 * on txn->flags if it encounters a tarpit rule. If <deny_status> is not NULL
2750 * and a deny/tarpit rule is matched, it will be filled with this rule's deny
2751 * status.
2752 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002753static enum rule_result http_req_get_intercept_rule(struct proxy *px, struct list *rules,
Christopher Fauletb58f62b2020-01-13 16:40:13 +01002754 struct stream *s)
Christopher Faulet3e964192018-10-24 11:39:23 +02002755{
2756 struct session *sess = strm_sess(s);
2757 struct http_txn *txn = s->txn;
Christopher Faulet3e964192018-10-24 11:39:23 +02002758 struct act_rule *rule;
Christopher Faulet3e964192018-10-24 11:39:23 +02002759 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002760 int act_opts = 0;
Christopher Faulet3e964192018-10-24 11:39:23 +02002761
Christopher Faulet3e964192018-10-24 11:39:23 +02002762 /* If "the current_rule_list" match the executed rule list, we are in
2763 * resume condition. If a resume is needed it is always in the action
2764 * and never in the ACL or converters. In this case, we initialise the
2765 * current rule, and go to the action execution point.
2766 */
2767 if (s->current_rule) {
2768 rule = s->current_rule;
2769 s->current_rule = NULL;
2770 if (s->current_rule_list == rules)
2771 goto resume_execution;
2772 }
2773 s->current_rule_list = rules;
2774
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002775 /* start the ruleset evaluation in strict mode */
2776 txn->req.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01002777
Christopher Faulet3e964192018-10-24 11:39:23 +02002778 list_for_each_entry(rule, rules, list) {
2779 /* check optional condition */
2780 if (rule->cond) {
2781 int ret;
2782
2783 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
2784 ret = acl_pass(ret);
2785
2786 if (rule->cond->pol == ACL_COND_UNLESS)
2787 ret = !ret;
2788
2789 if (!ret) /* condition not matched */
2790 continue;
2791 }
2792
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002793 act_opts |= ACT_OPT_FIRST;
Christopher Faulet3e964192018-10-24 11:39:23 +02002794 resume_execution:
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002795 /* Always call the action function if defined */
2796 if (rule->action_ptr) {
2797 if ((s->req.flags & CF_READ_ERROR) ||
2798 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
2799 (px->options & PR_O_ABRT_CLOSE)))
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002800 act_opts |= ACT_OPT_FINAL;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002801
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002802 switch (rule->action_ptr(rule, px, sess, s, act_opts)) {
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002803 case ACT_RET_CONT:
2804 break;
2805 case ACT_RET_STOP:
2806 rule_ret = HTTP_RULE_RES_STOP;
2807 goto end;
2808 case ACT_RET_YIELD:
2809 s->current_rule = rule;
2810 rule_ret = HTTP_RULE_RES_YIELD;
2811 goto end;
2812 case ACT_RET_ERR:
2813 rule_ret = HTTP_RULE_RES_ERROR;
2814 goto end;
2815 case ACT_RET_DONE:
2816 rule_ret = HTTP_RULE_RES_DONE;
2817 goto end;
2818 case ACT_RET_DENY:
Christopher Fauletb58f62b2020-01-13 16:40:13 +01002819 if (txn->status == -1)
2820 txn->status = 403;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002821 rule_ret = HTTP_RULE_RES_DENY;
2822 goto end;
2823 case ACT_RET_ABRT:
2824 rule_ret = HTTP_RULE_RES_ABRT;
2825 goto end;
2826 case ACT_RET_INV:
2827 rule_ret = HTTP_RULE_RES_BADREQ;
2828 goto end;
2829 }
2830 continue; /* eval the next rule */
2831 }
2832
2833 /* If not action function defined, check for known actions */
Christopher Faulet3e964192018-10-24 11:39:23 +02002834 switch (rule->action) {
2835 case ACT_ACTION_ALLOW:
2836 rule_ret = HTTP_RULE_RES_STOP;
2837 goto end;
2838
2839 case ACT_ACTION_DENY:
Christopher Faulet5cb513a2020-05-13 17:56:56 +02002840 txn->status = rule->arg.http_reply->status;
2841 txn->http_reply = rule->arg.http_reply;
Christopher Faulet3e964192018-10-24 11:39:23 +02002842 rule_ret = HTTP_RULE_RES_DENY;
2843 goto end;
2844
2845 case ACT_HTTP_REQ_TARPIT:
2846 txn->flags |= TX_CLTARPIT;
Christopher Faulet5cb513a2020-05-13 17:56:56 +02002847 txn->status = rule->arg.http_reply->status;
2848 txn->http_reply = rule->arg.http_reply;
Christopher Faulet3e964192018-10-24 11:39:23 +02002849 rule_ret = HTTP_RULE_RES_DENY;
2850 goto end;
2851
Christopher Faulet3e964192018-10-24 11:39:23 +02002852 case ACT_HTTP_REDIR:
Christopher Faulet90d22a82020-03-06 11:18:39 +01002853 rule_ret = HTTP_RULE_RES_ABRT;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002854 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
Christopher Faulet3a26bee2019-12-16 12:47:40 +01002855 rule_ret = HTTP_RULE_RES_ERROR;
Christopher Faulet3e964192018-10-24 11:39:23 +02002856 goto end;
2857
2858 case ACT_HTTP_SET_NICE:
Christopher Faulet96bff762019-12-17 13:46:18 +01002859 s->task->nice = rule->arg.http.i;
Christopher Faulet3e964192018-10-24 11:39:23 +02002860 break;
2861
2862 case ACT_HTTP_SET_TOS:
Christopher Faulet96bff762019-12-17 13:46:18 +01002863 conn_set_tos(objt_conn(sess->origin), rule->arg.http.i);
Christopher Faulet3e964192018-10-24 11:39:23 +02002864 break;
2865
2866 case ACT_HTTP_SET_MARK:
Christopher Faulet96bff762019-12-17 13:46:18 +01002867 conn_set_mark(objt_conn(sess->origin), rule->arg.http.i);
Christopher Faulet3e964192018-10-24 11:39:23 +02002868 break;
2869
2870 case ACT_HTTP_SET_LOGL:
Christopher Faulet96bff762019-12-17 13:46:18 +01002871 s->logs.level = rule->arg.http.i;
Christopher Faulet3e964192018-10-24 11:39:23 +02002872 break;
2873
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002874 /* other flags exists, but normally, they never be matched. */
Christopher Faulet3e964192018-10-24 11:39:23 +02002875 default:
2876 break;
2877 }
2878 }
2879
2880 end:
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002881 /* if the ruleset evaluation is finished reset the strict mode */
Christopher Faulet46f95542019-12-20 10:07:22 +01002882 if (rule_ret != HTTP_RULE_RES_YIELD)
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002883 txn->req.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01002884
Christopher Faulet3e964192018-10-24 11:39:23 +02002885 /* we reached the end of the rules, nothing to report */
2886 return rule_ret;
2887}
2888
2889/* Executes the http-response rules <rules> for stream <s> and proxy <px>. It
2890 * returns one of 5 possible statuses: HTTP_RULE_RES_CONT, HTTP_RULE_RES_STOP,
2891 * HTTP_RULE_RES_DONE, HTTP_RULE_RES_YIELD, or HTTP_RULE_RES_BADREQ. If *CONT
2892 * is returned, the process can continue the evaluation of next rule list. If
2893 * *STOP or *DONE is returned, the process must stop the evaluation. If *BADREQ
2894 * is returned, it means the operation could not be processed and a server error
Christopher Fauleta53abad2020-05-13 08:12:22 +02002895 * must be returned. If *YIELD is returned, the caller must call again the
2896 * function with the same context.
Christopher Faulet3e964192018-10-24 11:39:23 +02002897 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002898static enum rule_result http_res_get_intercept_rule(struct proxy *px, struct list *rules,
2899 struct stream *s)
Christopher Faulet3e964192018-10-24 11:39:23 +02002900{
2901 struct session *sess = strm_sess(s);
2902 struct http_txn *txn = s->txn;
Christopher Faulet3e964192018-10-24 11:39:23 +02002903 struct act_rule *rule;
Christopher Faulet3e964192018-10-24 11:39:23 +02002904 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002905 int act_opts = 0;
Christopher Faulet3e964192018-10-24 11:39:23 +02002906
Christopher Faulet3e964192018-10-24 11:39:23 +02002907 /* If "the current_rule_list" match the executed rule list, we are in
2908 * resume condition. If a resume is needed it is always in the action
2909 * and never in the ACL or converters. In this case, we initialise the
2910 * current rule, and go to the action execution point.
2911 */
2912 if (s->current_rule) {
2913 rule = s->current_rule;
2914 s->current_rule = NULL;
2915 if (s->current_rule_list == rules)
2916 goto resume_execution;
2917 }
2918 s->current_rule_list = rules;
2919
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002920 /* start the ruleset evaluation in strict mode */
2921 txn->rsp.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01002922
Christopher Faulet3e964192018-10-24 11:39:23 +02002923 list_for_each_entry(rule, rules, list) {
2924 /* check optional condition */
2925 if (rule->cond) {
2926 int ret;
2927
2928 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
2929 ret = acl_pass(ret);
2930
2931 if (rule->cond->pol == ACL_COND_UNLESS)
2932 ret = !ret;
2933
2934 if (!ret) /* condition not matched */
2935 continue;
2936 }
2937
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002938 act_opts |= ACT_OPT_FIRST;
Christopher Faulet3e964192018-10-24 11:39:23 +02002939resume_execution:
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002940
2941 /* Always call the action function if defined */
2942 if (rule->action_ptr) {
2943 if ((s->req.flags & CF_READ_ERROR) ||
2944 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
2945 (px->options & PR_O_ABRT_CLOSE)))
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002946 act_opts |= ACT_OPT_FINAL;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002947
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002948 switch (rule->action_ptr(rule, px, sess, s, act_opts)) {
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002949 case ACT_RET_CONT:
2950 break;
2951 case ACT_RET_STOP:
2952 rule_ret = HTTP_RULE_RES_STOP;
2953 goto end;
2954 case ACT_RET_YIELD:
2955 s->current_rule = rule;
2956 rule_ret = HTTP_RULE_RES_YIELD;
2957 goto end;
2958 case ACT_RET_ERR:
2959 rule_ret = HTTP_RULE_RES_ERROR;
2960 goto end;
2961 case ACT_RET_DONE:
2962 rule_ret = HTTP_RULE_RES_DONE;
2963 goto end;
2964 case ACT_RET_DENY:
Christopher Fauletb58f62b2020-01-13 16:40:13 +01002965 if (txn->status == -1)
2966 txn->status = 502;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002967 rule_ret = HTTP_RULE_RES_DENY;
2968 goto end;
2969 case ACT_RET_ABRT:
2970 rule_ret = HTTP_RULE_RES_ABRT;
2971 goto end;
2972 case ACT_RET_INV:
2973 rule_ret = HTTP_RULE_RES_BADREQ;
2974 goto end;
2975 }
2976 continue; /* eval the next rule */
2977 }
2978
2979 /* If not action function defined, check for known actions */
Christopher Faulet3e964192018-10-24 11:39:23 +02002980 switch (rule->action) {
2981 case ACT_ACTION_ALLOW:
2982 rule_ret = HTTP_RULE_RES_STOP; /* "allow" rules are OK */
2983 goto end;
2984
2985 case ACT_ACTION_DENY:
Christopher Faulet5cb513a2020-05-13 17:56:56 +02002986 txn->status = rule->arg.http_reply->status;
2987 txn->http_reply = rule->arg.http_reply;
Christopher Faulet3a26bee2019-12-16 12:47:40 +01002988 rule_ret = HTTP_RULE_RES_DENY;
Christopher Faulet3e964192018-10-24 11:39:23 +02002989 goto end;
2990
2991 case ACT_HTTP_SET_NICE:
Christopher Faulet96bff762019-12-17 13:46:18 +01002992 s->task->nice = rule->arg.http.i;
Christopher Faulet3e964192018-10-24 11:39:23 +02002993 break;
2994
2995 case ACT_HTTP_SET_TOS:
Christopher Faulet96bff762019-12-17 13:46:18 +01002996 conn_set_tos(objt_conn(sess->origin), rule->arg.http.i);
Christopher Faulet3e964192018-10-24 11:39:23 +02002997 break;
2998
2999 case ACT_HTTP_SET_MARK:
Christopher Faulet96bff762019-12-17 13:46:18 +01003000 conn_set_mark(objt_conn(sess->origin), rule->arg.http.i);
Christopher Faulet3e964192018-10-24 11:39:23 +02003001 break;
3002
3003 case ACT_HTTP_SET_LOGL:
Christopher Faulet96bff762019-12-17 13:46:18 +01003004 s->logs.level = rule->arg.http.i;
Christopher Faulet3e964192018-10-24 11:39:23 +02003005 break;
3006
Christopher Faulet3e964192018-10-24 11:39:23 +02003007 case ACT_HTTP_REDIR:
Christopher Faulet49c2a702020-03-06 15:44:37 +01003008 rule_ret = HTTP_RULE_RES_ABRT;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003009 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
Christopher Faulet3a26bee2019-12-16 12:47:40 +01003010 rule_ret = HTTP_RULE_RES_ERROR;
Christopher Faulet3e964192018-10-24 11:39:23 +02003011 goto end;
3012
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01003013 /* other flags exists, but normally, they never be matched. */
Christopher Faulet3e964192018-10-24 11:39:23 +02003014 default:
3015 break;
3016 }
3017 }
3018
3019 end:
Christopher Faulet1aea50e2020-01-17 16:03:53 +01003020 /* if the ruleset evaluation is finished reset the strict mode */
Christopher Faulet46f95542019-12-20 10:07:22 +01003021 if (rule_ret != HTTP_RULE_RES_YIELD)
Christopher Faulet1aea50e2020-01-17 16:03:53 +01003022 txn->rsp.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01003023
Christopher Faulet3e964192018-10-24 11:39:23 +02003024 /* we reached the end of the rules, nothing to report */
3025 return rule_ret;
3026}
3027
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01003028/* Executes backend and frontend http-after-response rules for the stream <s>,
3029 * in that order. it return 1 on success and 0 on error. It is the caller
3030 * responsibility to catch error or ignore it. If it catches it, this function
3031 * may be called a second time, for the internal error.
3032 */
3033int http_eval_after_res_rules(struct stream *s)
3034{
3035 struct session *sess = s->sess;
3036 enum rule_result ret = HTTP_RULE_RES_CONT;
3037
Christopher Faulet507479b2020-05-15 12:29:46 +02003038 /* Eval after-response ruleset only if the reply is not const */
3039 if (s->txn->flags & TX_CONST_REPLY)
3040 goto end;
3041
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01003042 /* prune the request variables if not already done and swap to the response variables. */
3043 if (s->vars_reqres.scope != SCOPE_RES) {
3044 if (!LIST_ISEMPTY(&s->vars_reqres.head))
3045 vars_prune(&s->vars_reqres, s->sess, s);
3046 vars_init(&s->vars_reqres, SCOPE_RES);
3047 }
3048
3049 ret = http_res_get_intercept_rule(s->be, &s->be->http_after_res_rules, s);
3050 if ((ret == HTTP_RULE_RES_CONT || ret == HTTP_RULE_RES_STOP) && sess->fe != s->be)
3051 ret = http_res_get_intercept_rule(sess->fe, &sess->fe->http_after_res_rules, s);
3052
Christopher Faulet507479b2020-05-15 12:29:46 +02003053 end:
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01003054 /* All other codes than CONTINUE, STOP or DONE are forbidden */
3055 return (ret == HTTP_RULE_RES_CONT || ret == HTTP_RULE_RES_STOP || ret == HTTP_RULE_RES_DONE);
3056}
3057
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003058/*
3059 * Manage client-side cookie. It can impact performance by about 2% so it is
3060 * desirable to call it only when needed. This code is quite complex because
3061 * of the multiple very crappy and ambiguous syntaxes we have to support. it
3062 * highly recommended not to touch this part without a good reason !
3063 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003064static void http_manage_client_side_cookies(struct stream *s, struct channel *req)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003065{
3066 struct session *sess = s->sess;
3067 struct http_txn *txn = s->txn;
3068 struct htx *htx;
3069 struct http_hdr_ctx ctx;
3070 char *hdr_beg, *hdr_end, *del_from;
3071 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
3072 int preserve_hdr;
3073
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003074 htx = htxbuf(&req->buf);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003075 ctx.blk = NULL;
3076 while (http_find_header(htx, ist("Cookie"), &ctx, 1)) {
Olivier Houchardf0f42382019-07-22 17:43:46 +02003077 int is_first = 1;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003078 del_from = NULL; /* nothing to be deleted */
3079 preserve_hdr = 0; /* assume we may kill the whole header */
3080
3081 /* Now look for cookies. Conforming to RFC2109, we have to support
3082 * attributes whose name begin with a '$', and associate them with
3083 * the right cookie, if we want to delete this cookie.
3084 * So there are 3 cases for each cookie read :
3085 * 1) it's a special attribute, beginning with a '$' : ignore it.
3086 * 2) it's a server id cookie that we *MAY* want to delete : save
3087 * some pointers on it (last semi-colon, beginning of cookie...)
3088 * 3) it's an application cookie : we *MAY* have to delete a previous
3089 * "special" cookie.
3090 * At the end of loop, if a "special" cookie remains, we may have to
3091 * remove it. If no application cookie persists in the header, we
3092 * *MUST* delete it.
3093 *
3094 * Note: RFC2965 is unclear about the processing of spaces around
3095 * the equal sign in the ATTR=VALUE form. A careful inspection of
3096 * the RFC explicitly allows spaces before it, and not within the
3097 * tokens (attrs or values). An inspection of RFC2109 allows that
3098 * too but section 10.1.3 lets one think that spaces may be allowed
3099 * after the equal sign too, resulting in some (rare) buggy
3100 * implementations trying to do that. So let's do what servers do.
3101 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
3102 * allowed quoted strings in values, with any possible character
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003103 * after a backslash, including control chars and delimiters, which
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003104 * causes parsing to become ambiguous. Browsers also allow spaces
3105 * within values even without quotes.
3106 *
3107 * We have to keep multiple pointers in order to support cookie
3108 * removal at the beginning, middle or end of header without
3109 * corrupting the header. All of these headers are valid :
3110 *
3111 * hdr_beg hdr_end
3112 * | |
3113 * v |
3114 * NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3 |
3115 * NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3 v
3116 * NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3
3117 * | | | | | | |
3118 * | | | | | | |
3119 * | | | | | | +--> next
3120 * | | | | | +----> val_end
3121 * | | | | +-----------> val_beg
3122 * | | | +--------------> equal
3123 * | | +----------------> att_end
3124 * | +---------------------> att_beg
3125 * +--------------------------> prev
3126 *
3127 */
3128 hdr_beg = ctx.value.ptr;
3129 hdr_end = hdr_beg + ctx.value.len;
3130 for (prev = hdr_beg; prev < hdr_end; prev = next) {
3131 /* Iterate through all cookies on this line */
3132
3133 /* find att_beg */
3134 att_beg = prev;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003135 if (!is_first)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003136 att_beg++;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003137 is_first = 0;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003138
3139 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
3140 att_beg++;
3141
3142 /* find att_end : this is the first character after the last non
3143 * space before the equal. It may be equal to hdr_end.
3144 */
3145 equal = att_end = att_beg;
3146 while (equal < hdr_end) {
3147 if (*equal == '=' || *equal == ',' || *equal == ';')
3148 break;
3149 if (HTTP_IS_SPHT(*equal++))
3150 continue;
3151 att_end = equal;
3152 }
3153
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003154 /* here, <equal> points to '=', a delimiter or the end. <att_end>
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003155 * is between <att_beg> and <equal>, both may be identical.
3156 */
3157 /* look for end of cookie if there is an equal sign */
3158 if (equal < hdr_end && *equal == '=') {
3159 /* look for the beginning of the value */
3160 val_beg = equal + 1;
3161 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
3162 val_beg++;
3163
3164 /* find the end of the value, respecting quotes */
3165 next = http_find_cookie_value_end(val_beg, hdr_end);
3166
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003167 /* make val_end point to the first white space or delimiter after the value */
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003168 val_end = next;
3169 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
3170 val_end--;
3171 }
3172 else
3173 val_beg = val_end = next = equal;
3174
3175 /* We have nothing to do with attributes beginning with
3176 * '$'. However, they will automatically be removed if a
3177 * header before them is removed, since they're supposed
3178 * to be linked together.
3179 */
3180 if (*att_beg == '$')
3181 continue;
3182
3183 /* Ignore cookies with no equal sign */
3184 if (equal == next) {
3185 /* This is not our cookie, so we must preserve it. But if we already
3186 * scheduled another cookie for removal, we cannot remove the
3187 * complete header, but we can remove the previous block itself.
3188 */
3189 preserve_hdr = 1;
3190 if (del_from != NULL) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003191 int delta = http_del_hdr_value(hdr_beg, hdr_end, &del_from, prev);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003192 val_end += delta;
3193 next += delta;
3194 hdr_end += delta;
3195 prev = del_from;
3196 del_from = NULL;
3197 }
3198 continue;
3199 }
3200
3201 /* if there are spaces around the equal sign, we need to
3202 * strip them otherwise we'll get trouble for cookie captures,
3203 * or even for rewrites. Since this happens extremely rarely,
3204 * it does not hurt performance.
3205 */
3206 if (unlikely(att_end != equal || val_beg > equal + 1)) {
3207 int stripped_before = 0;
3208 int stripped_after = 0;
3209
3210 if (att_end != equal) {
3211 memmove(att_end, equal, hdr_end - equal);
3212 stripped_before = (att_end - equal);
3213 equal += stripped_before;
3214 val_beg += stripped_before;
3215 }
3216
3217 if (val_beg > equal + 1) {
3218 memmove(equal + 1, val_beg, hdr_end + stripped_before - val_beg);
3219 stripped_after = (equal + 1) - val_beg;
3220 val_beg += stripped_after;
3221 stripped_before += stripped_after;
3222 }
3223
3224 val_end += stripped_before;
3225 next += stripped_before;
3226 hdr_end += stripped_before;
3227 }
3228 /* now everything is as on the diagram above */
3229
3230 /* First, let's see if we want to capture this cookie. We check
3231 * that we don't already have a client side cookie, because we
3232 * can only capture one. Also as an optimisation, we ignore
3233 * cookies shorter than the declared name.
3234 */
3235 if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL &&
3236 (val_end - att_beg >= sess->fe->capture_namelen) &&
3237 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
3238 int log_len = val_end - att_beg;
3239
3240 if ((txn->cli_cookie = pool_alloc(pool_head_capture)) == NULL) {
3241 ha_alert("HTTP logging : out of memory.\n");
3242 } else {
3243 if (log_len > sess->fe->capture_len)
3244 log_len = sess->fe->capture_len;
3245 memcpy(txn->cli_cookie, att_beg, log_len);
3246 txn->cli_cookie[log_len] = 0;
3247 }
3248 }
3249
3250 /* Persistence cookies in passive, rewrite or insert mode have the
3251 * following form :
3252 *
3253 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
3254 *
3255 * For cookies in prefix mode, the form is :
3256 *
3257 * Cookie: NAME=SRV~VALUE
3258 */
3259 if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
3260 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
3261 struct server *srv = s->be->srv;
3262 char *delim;
3263
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003264 /* if we're in cookie prefix mode, we'll search the delimiter so that we
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003265 * have the server ID between val_beg and delim, and the original cookie between
3266 * delim+1 and val_end. Otherwise, delim==val_end :
3267 *
3268 * hdr_beg
3269 * |
3270 * v
3271 * NAME=SRV; # in all but prefix modes
3272 * NAME=SRV~OPAQUE ; # in prefix mode
3273 * || || | |+-> next
3274 * || || | +--> val_end
3275 * || || +---------> delim
3276 * || |+------------> val_beg
3277 * || +-------------> att_end = equal
3278 * |+-----------------> att_beg
3279 * +------------------> prev
3280 *
3281 */
3282 if (s->be->ck_opts & PR_CK_PFX) {
3283 for (delim = val_beg; delim < val_end; delim++)
3284 if (*delim == COOKIE_DELIM)
3285 break;
3286 }
3287 else {
3288 char *vbar1;
3289 delim = val_end;
3290 /* Now check if the cookie contains a date field, which would
3291 * appear after a vertical bar ('|') just after the server name
3292 * and before the delimiter.
3293 */
3294 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
3295 if (vbar1) {
3296 /* OK, so left of the bar is the server's cookie and
3297 * right is the last seen date. It is a base64 encoded
3298 * 30-bit value representing the UNIX date since the
3299 * epoch in 4-second quantities.
3300 */
3301 int val;
3302 delim = vbar1++;
3303 if (val_end - vbar1 >= 5) {
3304 val = b64tos30(vbar1);
3305 if (val > 0)
3306 txn->cookie_last_date = val << 2;
3307 }
3308 /* look for a second vertical bar */
3309 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
3310 if (vbar1 && (val_end - vbar1 > 5)) {
3311 val = b64tos30(vbar1 + 1);
3312 if (val > 0)
3313 txn->cookie_first_date = val << 2;
3314 }
3315 }
3316 }
3317
3318 /* if the cookie has an expiration date and the proxy wants to check
3319 * it, then we do that now. We first check if the cookie is too old,
3320 * then only if it has expired. We detect strict overflow because the
3321 * time resolution here is not great (4 seconds). Cookies with dates
3322 * in the future are ignored if their offset is beyond one day. This
3323 * allows an admin to fix timezone issues without expiring everyone
3324 * and at the same time avoids keeping unwanted side effects for too
3325 * long.
3326 */
3327 if (txn->cookie_first_date && s->be->cookie_maxlife &&
3328 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) ||
3329 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
3330 txn->flags &= ~TX_CK_MASK;
3331 txn->flags |= TX_CK_OLD;
3332 delim = val_beg; // let's pretend we have not found the cookie
3333 txn->cookie_first_date = 0;
3334 txn->cookie_last_date = 0;
3335 }
3336 else if (txn->cookie_last_date && s->be->cookie_maxidle &&
3337 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) ||
3338 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
3339 txn->flags &= ~TX_CK_MASK;
3340 txn->flags |= TX_CK_EXPIRED;
3341 delim = val_beg; // let's pretend we have not found the cookie
3342 txn->cookie_first_date = 0;
3343 txn->cookie_last_date = 0;
3344 }
3345
3346 /* Here, we'll look for the first running server which supports the cookie.
3347 * This allows to share a same cookie between several servers, for example
3348 * to dedicate backup servers to specific servers only.
3349 * However, to prevent clients from sticking to cookie-less backup server
3350 * when they have incidentely learned an empty cookie, we simply ignore
3351 * empty cookies and mark them as invalid.
3352 * The same behaviour is applied when persistence must be ignored.
3353 */
3354 if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
3355 srv = NULL;
3356
3357 while (srv) {
3358 if (srv->cookie && (srv->cklen == delim - val_beg) &&
3359 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
3360 if ((srv->cur_state != SRV_ST_STOPPED) ||
3361 (s->be->options & PR_O_PERSIST) ||
3362 (s->flags & SF_FORCE_PRST)) {
3363 /* we found the server and we can use it */
3364 txn->flags &= ~TX_CK_MASK;
3365 txn->flags |= (srv->cur_state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
3366 s->flags |= SF_DIRECT | SF_ASSIGNED;
3367 s->target = &srv->obj_type;
3368 break;
3369 } else {
3370 /* we found a server, but it's down,
3371 * mark it as such and go on in case
3372 * another one is available.
3373 */
3374 txn->flags &= ~TX_CK_MASK;
3375 txn->flags |= TX_CK_DOWN;
3376 }
3377 }
3378 srv = srv->next;
3379 }
3380
3381 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
3382 /* no server matched this cookie or we deliberately skipped it */
3383 txn->flags &= ~TX_CK_MASK;
3384 if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
3385 txn->flags |= TX_CK_UNUSED;
3386 else
3387 txn->flags |= TX_CK_INVALID;
3388 }
3389
3390 /* depending on the cookie mode, we may have to either :
3391 * - delete the complete cookie if we're in insert+indirect mode, so that
3392 * the server never sees it ;
3393 * - remove the server id from the cookie value, and tag the cookie as an
Joseph Herlante9d5c722018-11-25 11:00:25 -08003394 * application cookie so that it does not get accidentally removed later,
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003395 * if we're in cookie prefix mode
3396 */
3397 if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
3398 int delta; /* negative */
3399
3400 memmove(val_beg, delim + 1, hdr_end - (delim + 1));
3401 delta = val_beg - (delim + 1);
3402 val_end += delta;
3403 next += delta;
3404 hdr_end += delta;
3405 del_from = NULL;
3406 preserve_hdr = 1; /* we want to keep this cookie */
3407 }
3408 else if (del_from == NULL &&
3409 (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
3410 del_from = prev;
3411 }
3412 }
3413 else {
3414 /* This is not our cookie, so we must preserve it. But if we already
3415 * scheduled another cookie for removal, we cannot remove the
3416 * complete header, but we can remove the previous block itself.
3417 */
3418 preserve_hdr = 1;
3419
3420 if (del_from != NULL) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003421 int delta = http_del_hdr_value(hdr_beg, hdr_end, &del_from, prev);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003422 if (att_beg >= del_from)
3423 att_beg += delta;
3424 if (att_end >= del_from)
3425 att_end += delta;
3426 val_beg += delta;
3427 val_end += delta;
3428 next += delta;
3429 hdr_end += delta;
3430 prev = del_from;
3431 del_from = NULL;
3432 }
3433 }
3434
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003435 } /* for each cookie */
3436
3437
3438 /* There are no more cookies on this line.
3439 * We may still have one (or several) marked for deletion at the
3440 * end of the line. We must do this now in two ways :
3441 * - if some cookies must be preserved, we only delete from the
3442 * mark to the end of line ;
3443 * - if nothing needs to be preserved, simply delete the whole header
3444 */
3445 if (del_from) {
3446 hdr_end = (preserve_hdr ? del_from : hdr_beg);
3447 }
3448 if ((hdr_end - hdr_beg) != ctx.value.len) {
Christopher Faulet3e2638e2019-06-18 09:49:16 +02003449 if (hdr_beg != hdr_end)
3450 htx_change_blk_value_len(htx, ctx.blk, hdr_end - hdr_beg);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003451 else
3452 http_remove_header(htx, &ctx);
3453 }
3454 } /* for each "Cookie header */
3455}
3456
3457/*
3458 * Manage server-side cookies. It can impact performance by about 2% so it is
3459 * desirable to call it only when needed. This function is also used when we
3460 * just need to know if there is a cookie (eg: for check-cache).
3461 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003462static void http_manage_server_side_cookies(struct stream *s, struct channel *res)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003463{
3464 struct session *sess = s->sess;
3465 struct http_txn *txn = s->txn;
3466 struct htx *htx;
3467 struct http_hdr_ctx ctx;
3468 struct server *srv;
3469 char *hdr_beg, *hdr_end;
3470 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau6f7a02a2019-04-15 21:49:49 +02003471 int is_cookie2 = 0;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003472
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003473 htx = htxbuf(&res->buf);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003474
3475 ctx.blk = NULL;
3476 while (1) {
Olivier Houchardf0f42382019-07-22 17:43:46 +02003477 int is_first = 1;
3478
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003479 if (!http_find_header(htx, ist("Set-Cookie"), &ctx, 1)) {
3480 if (!http_find_header(htx, ist("Set-Cookie2"), &ctx, 1))
3481 break;
3482 is_cookie2 = 1;
3483 }
3484
3485 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
3486 * <prev> points to the colon.
3487 */
3488 txn->flags |= TX_SCK_PRESENT;
3489
3490 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
3491 * check-cache is enabled) and we are not interested in checking
3492 * them. Warning, the cookie capture is declared in the frontend.
3493 */
3494 if (s->be->cookie_name == NULL && sess->fe->capture_name == NULL)
3495 break;
3496
3497 /* OK so now we know we have to process this response cookie.
3498 * The format of the Set-Cookie header is slightly different
3499 * from the format of the Cookie header in that it does not
3500 * support the comma as a cookie delimiter (thus the header
3501 * cannot be folded) because the Expires attribute described in
3502 * the original Netscape's spec may contain an unquoted date
3503 * with a comma inside. We have to live with this because
3504 * many browsers don't support Max-Age and some browsers don't
3505 * support quoted strings. However the Set-Cookie2 header is
3506 * clean.
3507 *
3508 * We have to keep multiple pointers in order to support cookie
3509 * removal at the beginning, middle or end of header without
3510 * corrupting the header (in case of set-cookie2). A special
3511 * pointer, <scav> points to the beginning of the set-cookie-av
3512 * fields after the first semi-colon. The <next> pointer points
3513 * either to the end of line (set-cookie) or next unquoted comma
3514 * (set-cookie2). All of these headers are valid :
3515 *
3516 * hdr_beg hdr_end
3517 * | |
3518 * v |
3519 * NAME1 = VALUE 1 ; Secure; Path="/" |
3520 * NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT v
3521 * NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT
3522 * NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard
3523 * | | | | | | | |
3524 * | | | | | | | +-> next
3525 * | | | | | | +------------> scav
3526 * | | | | | +--------------> val_end
3527 * | | | | +--------------------> val_beg
3528 * | | | +----------------------> equal
3529 * | | +------------------------> att_end
3530 * | +----------------------------> att_beg
3531 * +------------------------------> prev
3532 * -------------------------------> hdr_beg
3533 */
3534 hdr_beg = ctx.value.ptr;
3535 hdr_end = hdr_beg + ctx.value.len;
3536 for (prev = hdr_beg; prev < hdr_end; prev = next) {
3537
3538 /* Iterate through all cookies on this line */
3539
3540 /* find att_beg */
3541 att_beg = prev;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003542 if (!is_first)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003543 att_beg++;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003544 is_first = 0;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003545
3546 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
3547 att_beg++;
3548
3549 /* find att_end : this is the first character after the last non
3550 * space before the equal. It may be equal to hdr_end.
3551 */
3552 equal = att_end = att_beg;
3553
3554 while (equal < hdr_end) {
3555 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
3556 break;
3557 if (HTTP_IS_SPHT(*equal++))
3558 continue;
3559 att_end = equal;
3560 }
3561
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003562 /* here, <equal> points to '=', a delimiter or the end. <att_end>
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003563 * is between <att_beg> and <equal>, both may be identical.
3564 */
3565
3566 /* look for end of cookie if there is an equal sign */
3567 if (equal < hdr_end && *equal == '=') {
3568 /* look for the beginning of the value */
3569 val_beg = equal + 1;
3570 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
3571 val_beg++;
3572
3573 /* find the end of the value, respecting quotes */
3574 next = http_find_cookie_value_end(val_beg, hdr_end);
3575
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003576 /* make val_end point to the first white space or delimiter after the value */
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003577 val_end = next;
3578 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
3579 val_end--;
3580 }
3581 else {
3582 /* <equal> points to next comma, semi-colon or EOL */
3583 val_beg = val_end = next = equal;
3584 }
3585
3586 if (next < hdr_end) {
3587 /* Set-Cookie2 supports multiple cookies, and <next> points to
3588 * a colon or semi-colon before the end. So skip all attr-value
3589 * pairs and look for the next comma. For Set-Cookie, since
3590 * commas are permitted in values, skip to the end.
3591 */
3592 if (is_cookie2)
3593 next = http_find_hdr_value_end(next, hdr_end);
3594 else
3595 next = hdr_end;
3596 }
3597
3598 /* Now everything is as on the diagram above */
3599
3600 /* Ignore cookies with no equal sign */
3601 if (equal == val_end)
3602 continue;
3603
3604 /* If there are spaces around the equal sign, we need to
3605 * strip them otherwise we'll get trouble for cookie captures,
3606 * or even for rewrites. Since this happens extremely rarely,
3607 * it does not hurt performance.
3608 */
3609 if (unlikely(att_end != equal || val_beg > equal + 1)) {
3610 int stripped_before = 0;
3611 int stripped_after = 0;
3612
3613 if (att_end != equal) {
3614 memmove(att_end, equal, hdr_end - equal);
3615 stripped_before = (att_end - equal);
3616 equal += stripped_before;
3617 val_beg += stripped_before;
3618 }
3619
3620 if (val_beg > equal + 1) {
3621 memmove(equal + 1, val_beg, hdr_end + stripped_before - val_beg);
3622 stripped_after = (equal + 1) - val_beg;
3623 val_beg += stripped_after;
3624 stripped_before += stripped_after;
3625 }
3626
3627 val_end += stripped_before;
3628 next += stripped_before;
3629 hdr_end += stripped_before;
3630
Christopher Faulet3e2638e2019-06-18 09:49:16 +02003631 htx_change_blk_value_len(htx, ctx.blk, hdr_end - hdr_beg);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003632 ctx.value.len = hdr_end - hdr_beg;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003633 }
3634
3635 /* First, let's see if we want to capture this cookie. We check
3636 * that we don't already have a server side cookie, because we
3637 * can only capture one. Also as an optimisation, we ignore
3638 * cookies shorter than the declared name.
3639 */
3640 if (sess->fe->capture_name != NULL &&
3641 txn->srv_cookie == NULL &&
3642 (val_end - att_beg >= sess->fe->capture_namelen) &&
3643 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
3644 int log_len = val_end - att_beg;
3645 if ((txn->srv_cookie = pool_alloc(pool_head_capture)) == NULL) {
3646 ha_alert("HTTP logging : out of memory.\n");
3647 }
3648 else {
3649 if (log_len > sess->fe->capture_len)
3650 log_len = sess->fe->capture_len;
3651 memcpy(txn->srv_cookie, att_beg, log_len);
3652 txn->srv_cookie[log_len] = 0;
3653 }
3654 }
3655
3656 srv = objt_server(s->target);
3657 /* now check if we need to process it for persistence */
3658 if (!(s->flags & SF_IGNORE_PRST) &&
3659 (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
3660 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
3661 /* assume passive cookie by default */
3662 txn->flags &= ~TX_SCK_MASK;
3663 txn->flags |= TX_SCK_FOUND;
3664
3665 /* If the cookie is in insert mode on a known server, we'll delete
3666 * this occurrence because we'll insert another one later.
3667 * We'll delete it too if the "indirect" option is set and we're in
3668 * a direct access.
3669 */
3670 if (s->be->ck_opts & PR_CK_PSV) {
3671 /* The "preserve" flag was set, we don't want to touch the
3672 * server's cookie.
3673 */
3674 }
3675 else if ((srv && (s->be->ck_opts & PR_CK_INS)) ||
3676 ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) {
3677 /* this cookie must be deleted */
3678 if (prev == hdr_beg && next == hdr_end) {
3679 /* whole header */
3680 http_remove_header(htx, &ctx);
3681 /* note: while both invalid now, <next> and <hdr_end>
3682 * are still equal, so the for() will stop as expected.
3683 */
3684 } else {
3685 /* just remove the value */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003686 int delta = http_del_hdr_value(hdr_beg, hdr_end, &prev, next);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003687 next = prev;
3688 hdr_end += delta;
3689 }
3690 txn->flags &= ~TX_SCK_MASK;
3691 txn->flags |= TX_SCK_DELETED;
3692 /* and go on with next cookie */
3693 }
3694 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) {
3695 /* replace bytes val_beg->val_end with the cookie name associated
3696 * with this server since we know it.
3697 */
3698 int sliding, delta;
3699
3700 ctx.value = ist2(val_beg, val_end - val_beg);
3701 ctx.lws_before = ctx.lws_after = 0;
3702 http_replace_header_value(htx, &ctx, ist2(srv->cookie, srv->cklen));
3703 delta = srv->cklen - (val_end - val_beg);
3704 sliding = (ctx.value.ptr - val_beg);
3705 hdr_beg += sliding;
3706 val_beg += sliding;
3707 next += sliding + delta;
3708 hdr_end += sliding + delta;
3709
3710 txn->flags &= ~TX_SCK_MASK;
3711 txn->flags |= TX_SCK_REPLACED;
3712 }
3713 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) {
3714 /* insert the cookie name associated with this server
3715 * before existing cookie, and insert a delimiter between them..
3716 */
3717 int sliding, delta;
3718 ctx.value = ist2(val_beg, 0);
3719 ctx.lws_before = ctx.lws_after = 0;
3720 http_replace_header_value(htx, &ctx, ist2(srv->cookie, srv->cklen + 1));
3721 delta = srv->cklen + 1;
3722 sliding = (ctx.value.ptr - val_beg);
3723 hdr_beg += sliding;
3724 val_beg += sliding;
3725 next += sliding + delta;
3726 hdr_end += sliding + delta;
3727
3728 val_beg[srv->cklen] = COOKIE_DELIM;
3729 txn->flags &= ~TX_SCK_MASK;
3730 txn->flags |= TX_SCK_REPLACED;
3731 }
3732 }
3733 /* that's done for this cookie, check the next one on the same
3734 * line when next != hdr_end (only if is_cookie2).
3735 */
3736 }
3737 }
3738}
3739
Christopher Faulet25a02f62018-10-24 12:00:25 +02003740/*
3741 * Parses the Cache-Control and Pragma request header fields to determine if
3742 * the request may be served from the cache and/or if it is cacheable. Updates
3743 * s->txn->flags.
3744 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003745void http_check_request_for_cacheability(struct stream *s, struct channel *req)
Christopher Faulet25a02f62018-10-24 12:00:25 +02003746{
3747 struct http_txn *txn = s->txn;
3748 struct htx *htx;
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003749 struct http_hdr_ctx ctx = { .blk = NULL };
3750 int pragma_found, cc_found;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003751
3752 if ((txn->flags & (TX_CACHEABLE|TX_CACHE_IGNORE)) == TX_CACHE_IGNORE)
3753 return; /* nothing more to do here */
3754
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003755 htx = htxbuf(&req->buf);
Christopher Faulet25a02f62018-10-24 12:00:25 +02003756 pragma_found = cc_found = 0;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003757
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003758 /* Check "pragma" header for HTTP/1.0 compatibility. */
3759 if (http_find_header(htx, ist("pragma"), &ctx, 1)) {
3760 if (isteqi(ctx.value, ist("no-cache"))) {
3761 pragma_found = 1;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003762 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003763 }
Christopher Faulet25a02f62018-10-24 12:00:25 +02003764
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003765 ctx.blk = NULL;
3766 /* Don't use the cache and don't try to store if we found the
3767 * Authorization header */
3768 if (http_find_header(htx, ist("authorization"), &ctx, 1)) {
3769 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3770 txn->flags |= TX_CACHE_IGNORE;
3771 }
Christopher Faulet25a02f62018-10-24 12:00:25 +02003772
Christopher Faulet25a02f62018-10-24 12:00:25 +02003773
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003774 /* Look for "cache-control" header and iterate over all the values
3775 * until we find one that specifies that caching is possible or not. */
3776 ctx.blk = NULL;
3777 while (http_find_header(htx, ist("cache-control"), &ctx, 0)) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003778 cc_found = 1;
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003779 /* We don't check the values after max-age, max-stale nor min-fresh,
3780 * we simply don't use the cache when they're specified. */
3781 if (istmatchi(ctx.value, ist("max-age")) ||
3782 istmatchi(ctx.value, ist("no-cache")) ||
3783 istmatchi(ctx.value, ist("max-stale")) ||
3784 istmatchi(ctx.value, ist("min-fresh"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003785 txn->flags |= TX_CACHE_IGNORE;
3786 continue;
3787 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003788 if (istmatchi(ctx.value, ist("no-store"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003789 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3790 continue;
3791 }
3792 }
3793
3794 /* RFC7234#5.4:
3795 * When the Cache-Control header field is also present and
3796 * understood in a request, Pragma is ignored.
3797 * When the Cache-Control header field is not present in a
3798 * request, caches MUST consider the no-cache request
3799 * pragma-directive as having the same effect as if
3800 * "Cache-Control: no-cache" were present.
3801 */
3802 if (!cc_found && pragma_found)
3803 txn->flags |= TX_CACHE_IGNORE;
3804}
3805
3806/*
3807 * Check if response is cacheable or not. Updates s->txn->flags.
3808 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003809void http_check_response_for_cacheability(struct stream *s, struct channel *res)
Christopher Faulet25a02f62018-10-24 12:00:25 +02003810{
3811 struct http_txn *txn = s->txn;
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003812 struct http_hdr_ctx ctx = { .blk = NULL };
Christopher Faulet25a02f62018-10-24 12:00:25 +02003813 struct htx *htx;
Remi Tricot-Le Bretoncc9bf2e2020-11-12 11:14:41 +01003814 int has_freshness_info = 0;
3815 int has_validator = 0;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003816
3817 if (txn->status < 200) {
3818 /* do not try to cache interim responses! */
3819 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3820 return;
3821 }
3822
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003823 htx = htxbuf(&res->buf);
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003824 /* Check "pragma" header for HTTP/1.0 compatibility. */
3825 if (http_find_header(htx, ist("pragma"), &ctx, 1)) {
3826 if (isteqi(ctx.value, ist("no-cache"))) {
3827 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3828 return;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003829 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003830 }
Christopher Faulet25a02f62018-10-24 12:00:25 +02003831
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003832 /* Look for "cache-control" header and iterate over all the values
3833 * until we find one that specifies that caching is possible or not. */
3834 ctx.blk = NULL;
3835 while (http_find_header(htx, ist("cache-control"), &ctx, 0)) {
3836 if (isteqi(ctx.value, ist("public"))) {
3837 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003838 continue;
3839 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003840 if (isteqi(ctx.value, ist("private")) ||
3841 isteqi(ctx.value, ist("no-cache")) ||
3842 isteqi(ctx.value, ist("no-store")) ||
3843 isteqi(ctx.value, ist("max-age=0")) ||
3844 isteqi(ctx.value, ist("s-maxage=0"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003845 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003846 continue;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003847 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003848 /* We might have a no-cache="set-cookie" form. */
3849 if (istmatchi(ctx.value, ist("no-cache=\"set-cookie"))) {
3850 txn->flags &= ~TX_CACHE_COOK;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003851 continue;
3852 }
Remi Tricot-Le Bretoncc9bf2e2020-11-12 11:14:41 +01003853
3854 if (istmatchi(ctx.value, ist("s-maxage")) ||
3855 istmatchi(ctx.value, ist("max-age"))) {
3856 has_freshness_info = 1;
3857 continue;
3858 }
3859 }
3860
3861 /* If no freshness information could be found in Cache-Control values,
3862 * look for an Expires header. */
3863 if (!has_freshness_info) {
3864 ctx.blk = NULL;
3865 has_freshness_info = http_find_header(htx, ist("expires"), &ctx, 0);
Christopher Faulet25a02f62018-10-24 12:00:25 +02003866 }
Remi Tricot-Le Bretoncc9bf2e2020-11-12 11:14:41 +01003867
3868 /* If no freshness information could be found in Cache-Control or Expires
3869 * values, look for an explicit validator. */
3870 if (!has_freshness_info) {
3871 ctx.blk = NULL;
3872 has_validator = 1;
3873 if (!http_find_header(htx, ist("etag"), &ctx, 0)) {
3874 ctx.blk = NULL;
3875 if (!http_find_header(htx, ist("last-modified"), &ctx, 0))
3876 has_validator = 0;
3877 }
3878 }
3879
3880 /* We won't store an entry that has neither a cache validator nor an
3881 * explicit expiration time, as suggested in RFC 7234#3. */
3882 if (!has_freshness_info && !has_validator)
3883 txn->flags |= TX_CACHE_IGNORE;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003884}
3885
Christopher Faulet377c5a52018-10-24 21:21:30 +02003886/*
3887 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
3888 * for the current backend.
3889 *
3890 * It is assumed that the request is either a HEAD, GET, or POST and that the
3891 * uri_auth field is valid.
3892 *
3893 * Returns 1 if stats should be provided, otherwise 0.
3894 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003895static int http_stats_check_uri(struct stream *s, struct http_txn *txn, struct proxy *backend)
Christopher Faulet377c5a52018-10-24 21:21:30 +02003896{
3897 struct uri_auth *uri_auth = backend->uri_auth;
3898 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003899 struct htx_sl *sl;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003900 struct ist uri;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003901
3902 if (!uri_auth)
3903 return 0;
3904
3905 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
3906 return 0;
3907
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003908 htx = htxbuf(&s->req.buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02003909 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003910 uri = htx_sl_req_uri(sl);
Willy Tarreau1eb3b482019-10-31 15:50:28 +01003911 if (*uri_auth->uri_prefix == '/')
3912 uri = http_get_path(uri);
Christopher Faulet377c5a52018-10-24 21:21:30 +02003913
3914 /* check URI size */
3915 if (uri_auth->uri_len > uri.len)
3916 return 0;
3917
3918 if (memcmp(uri.ptr, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
3919 return 0;
3920
3921 return 1;
3922}
3923
3924/* This function prepares an applet to handle the stats. It can deal with the
3925 * "100-continue" expectation, check that admin rules are met for POST requests,
3926 * and program a response message if something was unexpected. It cannot fail
3927 * and always relies on the stats applet to complete the job. It does not touch
3928 * analysers nor counters, which are left to the caller. It does not touch
3929 * s->target which is supposed to already point to the stats applet. The caller
3930 * is expected to have already assigned an appctx to the stream.
3931 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003932static int http_handle_stats(struct stream *s, struct channel *req)
Christopher Faulet377c5a52018-10-24 21:21:30 +02003933{
3934 struct stats_admin_rule *stats_admin_rule;
3935 struct stream_interface *si = &s->si[1];
3936 struct session *sess = s->sess;
3937 struct http_txn *txn = s->txn;
3938 struct http_msg *msg = &txn->req;
3939 struct uri_auth *uri_auth = s->be->uri_auth;
3940 const char *h, *lookup, *end;
3941 struct appctx *appctx;
3942 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003943 struct htx_sl *sl;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003944
3945 appctx = si_appctx(si);
3946 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
3947 appctx->st1 = appctx->st2 = 0;
3948 appctx->ctx.stats.st_code = STAT_STATUS_INIT;
Willy Tarreau676c29e2019-10-09 10:50:01 +02003949 appctx->ctx.stats.flags |= uri_auth->flags;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003950 appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */
3951 if ((msg->flags & HTTP_MSGF_VER_11) && (txn->meth != HTTP_METH_HEAD))
3952 appctx->ctx.stats.flags |= STAT_CHUNKED;
3953
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003954 htx = htxbuf(&req->buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02003955 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003956 lookup = HTX_SL_REQ_UPTR(sl) + uri_auth->uri_len;
3957 end = HTX_SL_REQ_UPTR(sl) + HTX_SL_REQ_ULEN(sl);
Christopher Faulet377c5a52018-10-24 21:21:30 +02003958
3959 for (h = lookup; h <= end - 3; h++) {
3960 if (memcmp(h, ";up", 3) == 0) {
3961 appctx->ctx.stats.flags |= STAT_HIDE_DOWN;
3962 break;
3963 }
Amaury Denoyelle91e55ea2021-02-25 14:46:08 +01003964 }
3965
3966 for (h = lookup; h <= end - 9; h++) {
3967 if (memcmp(h, ";no-maint", 9) == 0) {
Willy Tarreau3e320362020-10-23 17:28:57 +02003968 appctx->ctx.stats.flags |= STAT_HIDE_MAINT;
3969 break;
3970 }
Christopher Faulet377c5a52018-10-24 21:21:30 +02003971 }
3972
3973 if (uri_auth->refresh) {
3974 for (h = lookup; h <= end - 10; h++) {
3975 if (memcmp(h, ";norefresh", 10) == 0) {
3976 appctx->ctx.stats.flags |= STAT_NO_REFRESH;
3977 break;
3978 }
3979 }
3980 }
3981
3982 for (h = lookup; h <= end - 4; h++) {
3983 if (memcmp(h, ";csv", 4) == 0) {
Christopher Faulet6338a082019-09-09 15:50:54 +02003984 appctx->ctx.stats.flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
Christopher Faulet377c5a52018-10-24 21:21:30 +02003985 break;
3986 }
3987 }
3988
3989 for (h = lookup; h <= end - 6; h++) {
3990 if (memcmp(h, ";typed", 6) == 0) {
Christopher Faulet6338a082019-09-09 15:50:54 +02003991 appctx->ctx.stats.flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
Christopher Faulet377c5a52018-10-24 21:21:30 +02003992 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
3993 break;
3994 }
3995 }
3996
Christopher Faulet6338a082019-09-09 15:50:54 +02003997 for (h = lookup; h <= end - 5; h++) {
3998 if (memcmp(h, ";json", 5) == 0) {
3999 appctx->ctx.stats.flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
4000 appctx->ctx.stats.flags |= STAT_FMT_JSON;
4001 break;
4002 }
4003 }
4004
4005 for (h = lookup; h <= end - 12; h++) {
4006 if (memcmp(h, ";json-schema", 12) == 0) {
4007 appctx->ctx.stats.flags &= ~STAT_FMT_MASK;
4008 appctx->ctx.stats.flags |= STAT_JSON_SCHM;
4009 break;
4010 }
4011 }
4012
Christopher Faulet377c5a52018-10-24 21:21:30 +02004013 for (h = lookup; h <= end - 8; h++) {
4014 if (memcmp(h, ";st=", 4) == 0) {
4015 int i;
4016 h += 4;
4017 appctx->ctx.stats.st_code = STAT_STATUS_UNKN;
4018 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
4019 if (strncmp(stat_status_codes[i], h, 4) == 0) {
4020 appctx->ctx.stats.st_code = i;
4021 break;
4022 }
4023 }
4024 break;
4025 }
4026 }
4027
4028 appctx->ctx.stats.scope_str = 0;
4029 appctx->ctx.stats.scope_len = 0;
4030 for (h = lookup; h <= end - 8; h++) {
4031 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
4032 int itx = 0;
4033 const char *h2;
4034 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
4035 const char *err;
4036
4037 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
4038 h2 = h;
Christopher Fauleted7a0662019-01-14 11:07:34 +01004039 appctx->ctx.stats.scope_str = h2 - HTX_SL_REQ_UPTR(sl);
4040 while (h < end) {
Christopher Faulet377c5a52018-10-24 21:21:30 +02004041 if (*h == ';' || *h == '&' || *h == ' ')
4042 break;
4043 itx++;
4044 h++;
4045 }
4046
4047 if (itx > STAT_SCOPE_TXT_MAXLEN)
4048 itx = STAT_SCOPE_TXT_MAXLEN;
4049 appctx->ctx.stats.scope_len = itx;
4050
4051 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
4052 memcpy(scope_txt, h2, itx);
4053 scope_txt[itx] = '\0';
4054 err = invalid_char(scope_txt);
4055 if (err) {
4056 /* bad char in search text => clear scope */
4057 appctx->ctx.stats.scope_str = 0;
4058 appctx->ctx.stats.scope_len = 0;
4059 }
4060 break;
4061 }
4062 }
4063
4064 /* now check whether we have some admin rules for this request */
4065 list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) {
4066 int ret = 1;
4067
4068 if (stats_admin_rule->cond) {
4069 ret = acl_exec_cond(stats_admin_rule->cond, s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
4070 ret = acl_pass(ret);
4071 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
4072 ret = !ret;
4073 }
4074
4075 if (ret) {
4076 /* no rule, or the rule matches */
4077 appctx->ctx.stats.flags |= STAT_ADMIN;
4078 break;
4079 }
4080 }
4081
Christopher Faulet5d45e382019-02-27 15:15:23 +01004082 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
4083 appctx->st0 = STAT_HTTP_HEAD;
4084 else if (txn->meth == HTTP_METH_POST) {
Christopher Fauletbd9e8422019-08-15 22:26:48 +02004085 if (appctx->ctx.stats.flags & STAT_ADMIN) {
Christopher Faulet377c5a52018-10-24 21:21:30 +02004086 appctx->st0 = STAT_HTTP_POST;
Christopher Fauletbd9e8422019-08-15 22:26:48 +02004087 if (msg->msg_state < HTTP_MSG_DATA)
4088 req->analysers |= AN_REQ_HTTP_BODY;
4089 }
Christopher Faulet377c5a52018-10-24 21:21:30 +02004090 else {
Christopher Faulet5d45e382019-02-27 15:15:23 +01004091 /* POST without admin level */
Christopher Faulet377c5a52018-10-24 21:21:30 +02004092 appctx->ctx.stats.flags &= ~STAT_CHUNKED;
4093 appctx->ctx.stats.st_code = STAT_STATUS_DENY;
4094 appctx->st0 = STAT_HTTP_LAST;
4095 }
4096 }
4097 else {
Christopher Faulet5d45e382019-02-27 15:15:23 +01004098 /* Unsupported method */
4099 appctx->ctx.stats.flags &= ~STAT_CHUNKED;
4100 appctx->ctx.stats.st_code = STAT_STATUS_IVAL;
4101 appctx->st0 = STAT_HTTP_LAST;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004102 }
4103
4104 s->task->nice = -32; /* small boost for HTTP statistics */
4105 return 1;
4106}
4107
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004108void http_perform_server_redirect(struct stream *s, struct stream_interface *si)
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004109{
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004110 struct channel *req = &s->req;
4111 struct channel *res = &s->res;
4112 struct server *srv;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004113 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004114 struct htx_sl *sl;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004115 struct ist path, location;
4116 unsigned int flags;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004117
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004118 /*
4119 * Create the location
4120 */
4121 chunk_reset(&trash);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004122
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004123 /* 1: add the server's prefix */
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004124 /* special prefix "/" means don't change URL */
4125 srv = __objt_server(s->target);
4126 if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') {
4127 if (!chunk_memcat(&trash, srv->rdr_pfx, srv->rdr_len))
4128 return;
4129 }
4130
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004131 /* 2: add the request Path */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004132 htx = htxbuf(&req->buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02004133 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004134 path = http_get_path(htx_sl_req_uri(sl));
Tim Duesterhused526372020-03-05 17:56:33 +01004135 if (!isttest(path))
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004136 return;
4137
4138 if (!chunk_memcat(&trash, path.ptr, path.len))
4139 return;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004140 location = ist2(trash.area, trash.data);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004141
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004142 /*
4143 * Create the 302 respone
4144 */
4145 htx = htx_from_buf(&res->buf);
4146 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
4147 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
4148 ist("HTTP/1.1"), ist("302"), ist("Found"));
4149 if (!sl)
4150 goto fail;
4151 sl->info.res.status = 302;
4152 s->txn->status = 302;
4153
4154 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) ||
4155 !htx_add_header(htx, ist("Connection"), ist("close")) ||
4156 !htx_add_header(htx, ist("Content-length"), ist("0")) ||
4157 !htx_add_header(htx, ist("Location"), location))
4158 goto fail;
4159
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004160 if (!htx_add_endof(htx, HTX_BLK_EOH))
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004161 goto fail;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004162
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004163 htx->flags |= HTX_FL_EOM;
Christopher Fauletc20afb82020-01-24 19:16:26 +01004164 htx_to_buf(htx, &res->buf);
Christopher Fauleta72a7e42020-01-28 09:28:11 +01004165 if (!http_forward_proxy_resp(s, 1))
4166 goto fail;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004167
4168 /* return without error. */
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004169 si_shutr(si);
4170 si_shutw(si);
4171 si->err_type = SI_ET_NONE;
4172 si->state = SI_ST_CLO;
4173
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004174 if (!(s->flags & SF_ERR_MASK))
4175 s->flags |= SF_ERR_LOCAL;
4176 if (!(s->flags & SF_FINST_MASK))
4177 s->flags |= SF_FINST_C;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004178
4179 /* FIXME: we should increase a counter of redirects per server and per backend. */
4180 srv_inc_sess_ctr(srv);
4181 srv_set_sess_last(srv);
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004182 return;
4183
4184 fail:
4185 /* If an error occurred, remove the incomplete HTTP response from the
4186 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004187 channel_htx_truncate(res, htx);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004188}
4189
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05004190/* This function terminates the request because it was completely analyzed or
Christopher Fauletf2824e62018-10-01 12:12:37 +02004191 * because an error was triggered during the body forwarding.
4192 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004193static void http_end_request(struct stream *s)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004194{
4195 struct channel *chn = &s->req;
4196 struct http_txn *txn = s->txn;
4197
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004198 DBG_TRACE_ENTER(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004199
Christopher Fauletb42a8b62018-11-19 21:59:00 +01004200 if (unlikely(txn->req.msg_state == HTTP_MSG_ERROR ||
4201 txn->rsp.msg_state == HTTP_MSG_ERROR)) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004202 channel_abort(chn);
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004203 channel_htx_truncate(chn, htxbuf(&chn->buf));
Christopher Fauletf2824e62018-10-01 12:12:37 +02004204 goto end;
4205 }
4206
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004207 if (unlikely(txn->req.msg_state < HTTP_MSG_DONE)) {
4208 DBG_TRACE_DEVEL("waiting end of the request", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004209 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004210 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004211
4212 if (txn->req.msg_state == HTTP_MSG_DONE) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004213 /* No need to read anymore, the request was completely parsed.
4214 * We can shut the read side unless we want to abort_on_close,
4215 * or we have a POST request. The issue with POST requests is
4216 * that some browsers still send a CRLF after the request, and
4217 * this CRLF must be read so that it does not remain in the kernel
4218 * buffers, otherwise a close could cause an RST on some systems
4219 * (eg: Linux).
4220 */
Christopher Faulet769d0e92019-03-22 14:23:18 +01004221 if (!(s->be->options & PR_O_ABRT_CLOSE) && txn->meth != HTTP_METH_POST)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004222 channel_dont_read(chn);
4223
4224 /* if the server closes the connection, we want to immediately react
4225 * and close the socket to save packets and syscalls.
4226 */
4227 s->si[1].flags |= SI_FL_NOHALF;
4228
4229 /* In any case we've finished parsing the request so we must
4230 * disable Nagle when sending data because 1) we're not going
4231 * to shut this side, and 2) the server is waiting for us to
4232 * send pending data.
4233 */
4234 chn->flags |= CF_NEVER_WAIT;
4235
Christopher Fauletd01ce402019-01-02 17:44:13 +01004236 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
4237 /* The server has not finished to respond, so we
4238 * don't want to move in order not to upset it.
4239 */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004240 DBG_TRACE_DEVEL("waiting end of the response", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletd01ce402019-01-02 17:44:13 +01004241 return;
4242 }
4243
Christopher Fauletf2824e62018-10-01 12:12:37 +02004244 /* When we get here, it means that both the request and the
4245 * response have finished receiving. Depending on the connection
4246 * mode, we'll have to wait for the last bytes to leave in either
4247 * direction, and sometimes for a close to be effective.
4248 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02004249 if (txn->flags & TX_CON_WANT_TUN) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004250 /* Tunnel mode will not have any analyser so it needs to
4251 * poll for reads.
4252 */
4253 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004254 if (b_data(&chn->buf)) {
4255 DBG_TRACE_DEVEL("waiting to flush the request", STRM_EV_HTTP_ANA, s, txn);
Christopher Faulet9768c262018-10-22 09:34:31 +02004256 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004257 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004258 txn->req.msg_state = HTTP_MSG_TUNNEL;
4259 }
4260 else {
4261 /* we're not expecting any new data to come for this
4262 * transaction, so we can close it.
Christopher Faulet9768c262018-10-22 09:34:31 +02004263 *
4264 * However, there is an exception if the response
4265 * length is undefined. In this case, we need to wait
4266 * the close from the server. The response will be
4267 * switched in TUNNEL mode until the end.
Christopher Fauletf2824e62018-10-01 12:12:37 +02004268 */
4269 if (!(txn->rsp.flags & HTTP_MSGF_XFER_LEN) &&
4270 txn->rsp.msg_state != HTTP_MSG_CLOSED)
Christopher Faulet9768c262018-10-22 09:34:31 +02004271 goto check_channel_flags;
Christopher Fauletf2824e62018-10-01 12:12:37 +02004272
4273 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4274 channel_shutr_now(chn);
4275 channel_shutw_now(chn);
4276 }
4277 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004278 goto check_channel_flags;
4279 }
4280
4281 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
4282 http_msg_closing:
4283 /* nothing else to forward, just waiting for the output buffer
4284 * to be empty and for the shutw_now to take effect.
4285 */
4286 if (channel_is_empty(chn)) {
4287 txn->req.msg_state = HTTP_MSG_CLOSED;
4288 goto http_msg_closed;
4289 }
4290 else if (chn->flags & CF_SHUTW) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004291 txn->req.msg_state = HTTP_MSG_ERROR;
4292 goto end;
4293 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004294 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004295 return;
4296 }
4297
4298 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
4299 http_msg_closed:
Christopher Fauletf2824e62018-10-01 12:12:37 +02004300 /* if we don't know whether the server will close, we need to hard close */
4301 if (txn->rsp.flags & HTTP_MSGF_XFER_LEN)
4302 s->si[1].flags |= SI_FL_NOLINGER; /* we want to close ASAP */
Christopher Fauletf2824e62018-10-01 12:12:37 +02004303 /* see above in MSG_DONE why we only do this in these states */
Christopher Faulet769d0e92019-03-22 14:23:18 +01004304 if (!(s->be->options & PR_O_ABRT_CLOSE))
Christopher Fauletf2824e62018-10-01 12:12:37 +02004305 channel_dont_read(chn);
4306 goto end;
4307 }
4308
4309 check_channel_flags:
4310 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4311 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4312 /* if we've just closed an output, let's switch */
4313 txn->req.msg_state = HTTP_MSG_CLOSING;
4314 goto http_msg_closing;
4315 }
4316
4317 end:
4318 chn->analysers &= AN_REQ_FLT_END;
Christopher Faulet198ef8b2020-12-15 13:32:55 +01004319 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
4320 chn->flags |= CF_NEVER_WAIT;
4321 if (HAS_REQ_DATA_FILTERS(s))
Christopher Fauletf2824e62018-10-01 12:12:37 +02004322 chn->analysers |= AN_REQ_FLT_XFER_DATA;
Christopher Faulet198ef8b2020-12-15 13:32:55 +01004323 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004324 channel_auto_close(chn);
4325 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004326 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004327}
4328
4329
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05004330/* This function terminates the response because it was completely analyzed or
Christopher Fauletf2824e62018-10-01 12:12:37 +02004331 * because an error was triggered during the body forwarding.
4332 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004333static void http_end_response(struct stream *s)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004334{
4335 struct channel *chn = &s->res;
4336 struct http_txn *txn = s->txn;
4337
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004338 DBG_TRACE_ENTER(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004339
Christopher Fauletb42a8b62018-11-19 21:59:00 +01004340 if (unlikely(txn->req.msg_state == HTTP_MSG_ERROR ||
4341 txn->rsp.msg_state == HTTP_MSG_ERROR)) {
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004342 channel_htx_truncate(&s->req, htxbuf(&s->req.buf));
Christopher Faulet9768c262018-10-22 09:34:31 +02004343 channel_abort(&s->req);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004344 goto end;
4345 }
4346
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004347 if (unlikely(txn->rsp.msg_state < HTTP_MSG_DONE)) {
4348 DBG_TRACE_DEVEL("waiting end of the response", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004349 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004350 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004351
4352 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
4353 /* In theory, we don't need to read anymore, but we must
4354 * still monitor the server connection for a possible close
4355 * while the request is being uploaded, so we don't disable
4356 * reading.
4357 */
4358 /* channel_dont_read(chn); */
4359
4360 if (txn->req.msg_state < HTTP_MSG_DONE) {
4361 /* The client seems to still be sending data, probably
4362 * because we got an error response during an upload.
4363 * We have the choice of either breaking the connection
4364 * or letting it pass through. Let's do the later.
4365 */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004366 DBG_TRACE_DEVEL("waiting end of the request", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004367 return;
4368 }
4369
4370 /* When we get here, it means that both the request and the
4371 * response have finished receiving. Depending on the connection
4372 * mode, we'll have to wait for the last bytes to leave in either
4373 * direction, and sometimes for a close to be effective.
4374 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02004375 if (txn->flags & TX_CON_WANT_TUN) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004376 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004377 if (b_data(&chn->buf)) {
4378 DBG_TRACE_DEVEL("waiting to flush the respone", STRM_EV_HTTP_ANA, s, txn);
Christopher Faulet9768c262018-10-22 09:34:31 +02004379 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004380 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004381 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
4382 }
4383 else {
4384 /* we're not expecting any new data to come for this
4385 * transaction, so we can close it.
4386 */
4387 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4388 channel_shutr_now(chn);
4389 channel_shutw_now(chn);
4390 }
4391 }
4392 goto check_channel_flags;
4393 }
4394
4395 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
4396 http_msg_closing:
4397 /* nothing else to forward, just waiting for the output buffer
4398 * to be empty and for the shutw_now to take effect.
4399 */
4400 if (channel_is_empty(chn)) {
4401 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4402 goto http_msg_closed;
4403 }
4404 else if (chn->flags & CF_SHUTW) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004405 txn->rsp.msg_state = HTTP_MSG_ERROR;
Christopher Fauletcff0f732019-12-16 16:13:44 +01004406 _HA_ATOMIC_ADD(&strm_sess(s)->fe->fe_counters.cli_aborts, 1);
Olivier Houcharda798bf52019-03-08 18:52:00 +01004407 _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01004408 if (strm_sess(s)->listener->counters)
4409 _HA_ATOMIC_ADD(&strm_sess(s)->listener->counters->cli_aborts, 1);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004410 if (objt_server(s->target))
Christopher Fauletcff0f732019-12-16 16:13:44 +01004411 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.cli_aborts, 1);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004412 goto end;
4413 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004414 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004415 return;
4416 }
4417
4418 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
4419 http_msg_closed:
4420 /* drop any pending data */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004421 channel_htx_truncate(&s->req, htxbuf(&s->req.buf));
Christopher Faulet9768c262018-10-22 09:34:31 +02004422 channel_abort(&s->req);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004423 goto end;
4424 }
4425
4426 check_channel_flags:
4427 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4428 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4429 /* if we've just closed an output, let's switch */
4430 txn->rsp.msg_state = HTTP_MSG_CLOSING;
4431 goto http_msg_closing;
4432 }
4433
4434 end:
4435 chn->analysers &= AN_RES_FLT_END;
Christopher Faulet198ef8b2020-12-15 13:32:55 +01004436 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
4437 chn->flags |= CF_NEVER_WAIT;
4438 if (HAS_RSP_DATA_FILTERS(s))
4439 chn->analysers |= AN_RES_FLT_XFER_DATA;
4440 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004441 channel_auto_close(chn);
4442 channel_auto_read(chn);
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}
4445
Christopher Fauletef70e252020-01-28 09:26:19 +01004446/* Forward a response generated by HAProxy (error/redirect/return). This
4447 * function forwards all pending incoming data. If <final> is set to 0, nothing
4448 * more is performed. It is used for 1xx informational messages. Otherwise, the
Christopher Faulet507479b2020-05-15 12:29:46 +02004449 * transaction is terminated and the request is emptied. On success 1 is
Christopher Faulet40e6b552020-06-25 16:04:50 +02004450 * returned. If an error occurred, 0 is returned. If it fails, this function
4451 * only exits. It is the caller responsibility to do the cleanup.
Christopher Fauletef70e252020-01-28 09:26:19 +01004452 */
4453int http_forward_proxy_resp(struct stream *s, int final)
4454{
4455 struct channel *req = &s->req;
4456 struct channel *res = &s->res;
4457 struct htx *htx = htxbuf(&res->buf);
4458 size_t data;
4459
4460 if (final) {
4461 htx->flags |= HTX_FL_PROXY_RESP;
Christopher Faulet507479b2020-05-15 12:29:46 +02004462
Christopher Fauletaab1b672020-11-18 16:44:02 +01004463 if (!htx_is_empty(htx) && !http_eval_after_res_rules(s))
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01004464 return 0;
Christopher Fauletef70e252020-01-28 09:26:19 +01004465
Christopher Fauletd6c48362020-10-19 18:01:38 +02004466 if (s->txn->meth == HTTP_METH_HEAD)
4467 htx_skip_msg_payload(htx);
4468
Christopher Fauletef70e252020-01-28 09:26:19 +01004469 channel_auto_read(req);
4470 channel_abort(req);
4471 channel_auto_close(req);
4472 channel_htx_erase(req, htxbuf(&req->buf));
4473
4474 res->wex = tick_add_ifset(now_ms, res->wto);
4475 channel_auto_read(res);
4476 channel_auto_close(res);
4477 channel_shutr_now(res);
Christopher Faulet1a9db7c2020-06-25 15:36:45 +02004478 res->flags |= CF_EOI; /* The response is terminated, add EOI */
Christopher Faulet42432f32020-11-20 17:43:16 +01004479 htxbuf(&res->buf)->flags |= HTX_FL_EOM; /* no more data are expected */
Christopher Fauletef70e252020-01-28 09:26:19 +01004480 }
Christopher Fauletcf6898c2020-06-25 15:55:11 +02004481 else {
4482 /* Send ASAP informational messages. Rely on CF_EOI for final
4483 * response.
4484 */
4485 res->flags |= CF_SEND_DONTWAIT;
4486 }
Christopher Fauletef70e252020-01-28 09:26:19 +01004487
4488 data = htx->data - co_data(res);
4489 c_adv(res, data);
4490 htx->first = -1;
4491 res->total += data;
4492 return 1;
4493}
4494
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004495void http_server_error(struct stream *s, struct stream_interface *si, int err,
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004496 int finst, struct http_reply *msg)
Christopher Faulet0f226952018-10-22 09:29:56 +02004497{
Christopher Faulet72c7d8d2020-01-27 15:32:25 +01004498 http_reply_and_close(s, s->txn->status, msg);
Christopher Faulet0f226952018-10-22 09:29:56 +02004499 if (!(s->flags & SF_ERR_MASK))
4500 s->flags |= err;
4501 if (!(s->flags & SF_FINST_MASK))
4502 s->flags |= finst;
4503}
4504
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004505void http_reply_and_close(struct stream *s, short status, struct http_reply *msg)
Christopher Faulet0f226952018-10-22 09:29:56 +02004506{
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004507 if (!msg) {
4508 channel_htx_truncate(&s->res, htxbuf(&s->res.buf));
4509 goto end;
4510 }
4511
4512 if (http_reply_message(s, msg) == -1) {
4513 /* On error, return a 500 error message, but don't rewrite it if
Christopher Faulet40e6b552020-06-25 16:04:50 +02004514 * it is already an internal error. If it was already a "const"
4515 * 500 error, just fail.
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004516 */
Christopher Faulet40e6b552020-06-25 16:04:50 +02004517 if (s->txn->status == 500) {
4518 if (s->txn->flags & TX_CONST_REPLY)
4519 goto end;
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004520 s->txn->flags |= TX_CONST_REPLY;
Christopher Faulet40e6b552020-06-25 16:04:50 +02004521 }
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004522 s->txn->status = 500;
4523 s->txn->http_reply = NULL;
4524 return http_reply_and_close(s, s->txn->status, http_error_message(s));
4525 }
4526
4527end:
4528 s->res.wex = tick_add_ifset(now_ms, s->res.wto);
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004529
Christopher Faulet0f226952018-10-22 09:29:56 +02004530 channel_auto_read(&s->req);
4531 channel_abort(&s->req);
4532 channel_auto_close(&s->req);
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004533 channel_htx_erase(&s->req, htxbuf(&s->req.buf));
Christopher Faulet72c7d8d2020-01-27 15:32:25 +01004534 channel_auto_read(&s->res);
4535 channel_auto_close(&s->res);
4536 channel_shutr_now(&s->res);
Christopher Faulet0f226952018-10-22 09:29:56 +02004537}
4538
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004539struct http_reply *http_error_message(struct stream *s)
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004540{
4541 const int msgnum = http_get_status_idx(s->txn->status);
4542
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004543 if (s->txn->http_reply)
4544 return s->txn->http_reply;
4545 else if (s->be->replies[msgnum])
4546 return s->be->replies[msgnum];
4547 else if (strm_fe(s)->replies[msgnum])
4548 return strm_fe(s)->replies[msgnum];
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004549 else
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004550 return &http_err_replies[msgnum];
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004551}
4552
Christopher Faulet40e6b552020-06-25 16:04:50 +02004553/* Produces an HTX message from an http reply. Depending on the http reply type,
4554 * a, errorfile, an raw file or a log-format string is used. On success, it
4555 * returns 0. If an error occurs -1 is returned. If it fails, this function only
4556 * exits. It is the caller responsibility to do the cleanup.
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004557 */
Christopher Fauletae43b6c2020-05-27 15:24:22 +02004558int http_reply_to_htx(struct stream *s, struct htx *htx, struct http_reply *reply)
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004559{
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004560 struct buffer *errmsg;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004561 struct htx_sl *sl;
4562 struct buffer *body = NULL;
4563 const char *status, *reason, *clen, *ctype;
4564 unsigned int slflags;
4565 int ret = 0;
4566
Christopher Faulete29a97e2020-05-14 14:49:25 +02004567 /*
4568 * - HTTP_REPLY_ERRFILES unexpected here. handled as no payload if so
4569 *
4570 * - HTTP_REPLY_INDIRECT: switch on another reply if defined or handled
4571 * as no payload if NULL. the TXN status code is set with the status
4572 * of the original reply.
4573 */
4574
4575 if (reply->type == HTTP_REPLY_INDIRECT) {
4576 if (reply->body.reply)
4577 reply = reply->body.reply;
4578 }
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004579 if (reply->type == HTTP_REPLY_ERRMSG && !reply->body.errmsg) {
4580 /* get default error message */
4581 if (reply == s->txn->http_reply)
4582 s->txn->http_reply = NULL;
4583 reply = http_error_message(s);
4584 if (reply->type == HTTP_REPLY_INDIRECT) {
4585 if (reply->body.reply)
4586 reply = reply->body.reply;
4587 }
4588 }
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004589
4590 if (reply->type == HTTP_REPLY_ERRMSG) {
4591 /* implicit or explicit error message*/
4592 errmsg = reply->body.errmsg;
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004593 if (errmsg && !b_is_null(errmsg)) {
Christopher Faulet20567362020-05-15 14:52:49 +02004594 if (!htx_copy_msg(htx, errmsg))
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004595 goto fail;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004596 }
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004597 }
4598 else {
4599 /* no payload, file or log-format string */
4600 if (reply->type == HTTP_REPLY_RAW) {
4601 /* file */
4602 body = &reply->body.obj;
4603 }
4604 else if (reply->type == HTTP_REPLY_LOGFMT) {
4605 /* log-format string */
4606 body = alloc_trash_chunk();
4607 if (!body)
4608 goto fail_alloc;
4609 body->data = build_logline(s, body->area, body->size, &reply->body.fmt);
4610 }
4611 /* else no payload */
4612
4613 status = ultoa(reply->status);
4614 reason = http_get_reason(reply->status);
4615 slflags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_CLEN);
4616 if (!body || !b_data(body))
4617 slflags |= HTX_SL_F_BODYLESS;
4618 sl = htx_add_stline(htx, HTX_BLK_RES_SL, slflags, ist("HTTP/1.1"), ist(status), ist(reason));
4619 if (!sl)
4620 goto fail;
4621 sl->info.res.status = reply->status;
4622
4623 clen = (body ? ultoa(b_data(body)) : "0");
4624 ctype = reply->ctype;
4625
4626 if (!LIST_ISEMPTY(&reply->hdrs)) {
4627 struct http_reply_hdr *hdr;
4628 struct buffer *value = alloc_trash_chunk();
4629
4630 if (!value)
4631 goto fail;
4632
4633 list_for_each_entry(hdr, &reply->hdrs, list) {
4634 chunk_reset(value);
4635 value->data = build_logline(s, value->area, value->size, &hdr->value);
4636 if (b_data(value) && !htx_add_header(htx, hdr->name, ist2(b_head(value), b_data(value)))) {
4637 free_trash_chunk(value);
4638 goto fail;
4639 }
4640 chunk_reset(value);
4641 }
4642 free_trash_chunk(value);
4643 }
4644
4645 if (!htx_add_header(htx, ist("content-length"), ist(clen)) ||
4646 (body && b_data(body) && ctype && !htx_add_header(htx, ist("content-type"), ist(ctype))) ||
4647 !htx_add_endof(htx, HTX_BLK_EOH) ||
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004648 (body && b_data(body) && !htx_add_data_atonce(htx, ist2(b_head(body), b_data(body)))))
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004649 goto fail;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004650
4651 htx->flags |= HTX_FL_EOM;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004652 }
4653
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004654 leave:
4655 if (reply->type == HTTP_REPLY_LOGFMT)
4656 free_trash_chunk(body);
4657 return ret;
4658
4659 fail_alloc:
4660 if (!(s->flags & SF_ERR_MASK))
4661 s->flags |= SF_ERR_RESOURCE;
Christopher Faulet97e466c2020-05-15 15:12:47 +02004662 /* fall through */
4663 fail:
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004664 ret = -1;
4665 goto leave;
Christopher Faulet97e466c2020-05-15 15:12:47 +02004666}
4667
4668/* Send an http reply to the client. On success, it returns 0. If an error
Christopher Faulet40e6b552020-06-25 16:04:50 +02004669 * occurs -1 is returned and the response channel is truncated, removing this
4670 * way the faulty reply. This function may fail when the reply is formatted
4671 * (http_reply_to_htx) or when the reply is forwarded
4672 * (http_forward_proxy_resp). On the last case, it is because a
4673 * http-after-response rule fails.
Christopher Faulet97e466c2020-05-15 15:12:47 +02004674 */
4675int http_reply_message(struct stream *s, struct http_reply *reply)
4676{
4677 struct channel *res = &s->res;
4678 struct htx *htx = htx_from_buf(&res->buf);
4679
4680 if (s->txn->status == -1)
4681 s->txn->status = reply->status;
4682 channel_htx_truncate(res, htx);
4683
4684 if (http_reply_to_htx(s, htx, reply) == -1)
4685 goto fail;
4686
4687 htx_to_buf(htx, &s->res.buf);
4688 if (!http_forward_proxy_resp(s, 1))
4689 goto fail;
4690 return 0;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004691
4692 fail:
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004693 channel_htx_truncate(res, htx);
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004694 if (!(s->flags & SF_ERR_MASK))
4695 s->flags |= SF_ERR_PRXCOND;
Christopher Faulet97e466c2020-05-15 15:12:47 +02004696 return -1;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004697}
4698
Christopher Faulet304cc402019-07-15 15:46:28 +02004699/* Return the error message corresponding to si->err_type. It is assumed
4700 * that the server side is closed. Note that err_type is actually a
4701 * bitmask, where almost only aborts may be cumulated with other
4702 * values. We consider that aborted operations are more important
4703 * than timeouts or errors due to the fact that nobody else in the
4704 * logs might explain incomplete retries. All others should avoid
4705 * being cumulated. It should normally not be possible to have multiple
4706 * aborts at once, but just in case, the first one in sequence is reported.
4707 * Note that connection errors appearing on the second request of a keep-alive
4708 * connection are not reported since this allows the client to retry.
4709 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004710void http_return_srv_error(struct stream *s, struct stream_interface *si)
Christopher Faulet304cc402019-07-15 15:46:28 +02004711{
4712 int err_type = si->err_type;
4713
4714 /* set s->txn->status for http_error_message(s) */
4715 s->txn->status = 503;
4716
4717 if (err_type & SI_ET_QUEUE_ABRT)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004718 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_Q,
4719 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004720 else if (err_type & SI_ET_CONN_ABRT)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004721 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_C,
4722 (s->txn->flags & TX_NOT_FIRST) ? NULL :
4723 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004724 else if (err_type & SI_ET_QUEUE_TO)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004725 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_Q,
4726 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004727 else if (err_type & SI_ET_QUEUE_ERR)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004728 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_Q,
4729 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004730 else if (err_type & SI_ET_CONN_TO)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004731 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_C,
4732 (s->txn->flags & TX_NOT_FIRST) ? NULL :
4733 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004734 else if (err_type & SI_ET_CONN_ERR)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004735 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_C,
4736 (s->flags & SF_SRV_REUSED) ? NULL :
4737 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004738 else if (err_type & SI_ET_CONN_RES)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004739 http_server_error(s, si, SF_ERR_RESOURCE, SF_FINST_C,
4740 (s->txn->flags & TX_NOT_FIRST) ? NULL :
4741 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004742 else { /* SI_ET_CONN_OTHER and others */
4743 s->txn->status = 500;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004744 http_server_error(s, si, SF_ERR_INTERNAL, SF_FINST_C,
4745 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004746 }
4747}
4748
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004749
Christopher Faulet4a28a532019-03-01 11:19:40 +01004750/* Handle Expect: 100-continue for HTTP/1.1 messages if necessary. It returns 0
4751 * on success and -1 on error.
4752 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004753static int http_handle_expect_hdr(struct stream *s, struct htx *htx, struct http_msg *msg)
Christopher Faulet4a28a532019-03-01 11:19:40 +01004754{
4755 /* If we have HTTP/1.1 message with a body and Expect: 100-continue,
4756 * then we must send an HTTP/1.1 100 Continue intermediate response.
4757 */
4758 if (msg->msg_state == HTTP_MSG_BODY && (msg->flags & HTTP_MSGF_VER_11) &&
4759 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
4760 struct ist hdr = { .ptr = "Expect", .len = 6 };
4761 struct http_hdr_ctx ctx;
4762
4763 ctx.blk = NULL;
4764 /* Expect is allowed in 1.1, look for it */
4765 if (http_find_header(htx, hdr, &ctx, 0) &&
4766 unlikely(isteqi(ctx.value, ist2("100-continue", 12)))) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004767 if (http_reply_100_continue(s) == -1)
Christopher Faulet4a28a532019-03-01 11:19:40 +01004768 return -1;
4769 http_remove_header(htx, &ctx);
4770 }
4771 }
4772 return 0;
4773}
4774
Christopher Faulet23a3c792018-11-28 10:01:23 +01004775/* Send a 100-Continue response to the client. It returns 0 on success and -1
4776 * on error. The response channel is updated accordingly.
4777 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004778static int http_reply_100_continue(struct stream *s)
Christopher Faulet23a3c792018-11-28 10:01:23 +01004779{
4780 struct channel *res = &s->res;
4781 struct htx *htx = htx_from_buf(&res->buf);
4782 struct htx_sl *sl;
4783 unsigned int flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|
4784 HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
Christopher Faulet23a3c792018-11-28 10:01:23 +01004785
4786 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
4787 ist("HTTP/1.1"), ist("100"), ist("Continue"));
4788 if (!sl)
4789 goto fail;
4790 sl->info.res.status = 100;
4791
Christopher Faulet1d5ec092019-06-26 14:23:54 +02004792 if (!htx_add_endof(htx, HTX_BLK_EOH))
Christopher Faulet23a3c792018-11-28 10:01:23 +01004793 goto fail;
4794
Christopher Fauleta72a7e42020-01-28 09:28:11 +01004795 if (!http_forward_proxy_resp(s, 0))
4796 goto fail;
Christopher Faulet23a3c792018-11-28 10:01:23 +01004797 return 0;
4798
4799 fail:
4800 /* If an error occurred, remove the incomplete HTTP response from the
4801 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004802 channel_htx_truncate(res, htx);
Christopher Faulet23a3c792018-11-28 10:01:23 +01004803 return -1;
4804}
4805
Christopher Faulet12c51e22018-11-28 15:59:42 +01004806
Christopher Faulet0f226952018-10-22 09:29:56 +02004807/*
4808 * Capture headers from message <htx> according to header list <cap_hdr>, and
4809 * fill the <cap> pointers appropriately.
4810 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004811static void http_capture_headers(struct htx *htx, char **cap, struct cap_hdr *cap_hdr)
Christopher Faulet0f226952018-10-22 09:29:56 +02004812{
4813 struct cap_hdr *h;
4814 int32_t pos;
4815
Christopher Fauleta3f15502019-05-13 15:27:23 +02004816 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet0f226952018-10-22 09:29:56 +02004817 struct htx_blk *blk = htx_get_blk(htx, pos);
4818 enum htx_blk_type type = htx_get_blk_type(blk);
4819 struct ist n, v;
4820
4821 if (type == HTX_BLK_EOH)
4822 break;
4823 if (type != HTX_BLK_HDR)
4824 continue;
4825
4826 n = htx_get_blk_name(htx, blk);
4827
4828 for (h = cap_hdr; h; h = h->next) {
4829 if (h->namelen && (h->namelen == n.len) &&
4830 (strncasecmp(n.ptr, h->name, h->namelen) == 0)) {
4831 if (cap[h->index] == NULL)
4832 cap[h->index] =
4833 pool_alloc(h->pool);
4834
4835 if (cap[h->index] == NULL) {
4836 ha_alert("HTTP capture : out of memory.\n");
4837 break;
4838 }
4839
4840 v = htx_get_blk_value(htx, blk);
4841 if (v.len > h->len)
4842 v.len = h->len;
4843
4844 memcpy(cap[h->index], v.ptr, v.len);
4845 cap[h->index][v.len]=0;
4846 }
4847 }
4848 }
4849}
4850
Christopher Faulet0b6bdc52018-10-24 11:05:36 +02004851/* Delete a value in a header between delimiters <from> and <next>. The header
4852 * itself is delimited by <start> and <end> pointers. The number of characters
4853 * displaced is returned, and the pointer to the first delimiter is updated if
4854 * required. The function tries as much as possible to respect the following
4855 * principles :
4856 * - replace <from> delimiter by the <next> one unless <from> points to <start>,
4857 * in which case <next> is simply removed
4858 * - set exactly one space character after the new first delimiter, unless there
4859 * are not enough characters in the block being moved to do so.
4860 * - remove unneeded spaces before the previous delimiter and after the new
4861 * one.
4862 *
4863 * It is the caller's responsibility to ensure that :
4864 * - <from> points to a valid delimiter or <start> ;
4865 * - <next> points to a valid delimiter or <end> ;
4866 * - there are non-space chars before <from>.
4867 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004868static int http_del_hdr_value(char *start, char *end, char **from, char *next)
Christopher Faulet0b6bdc52018-10-24 11:05:36 +02004869{
4870 char *prev = *from;
4871
4872 if (prev == start) {
4873 /* We're removing the first value. eat the semicolon, if <next>
4874 * is lower than <end> */
4875 if (next < end)
4876 next++;
4877
4878 while (next < end && HTTP_IS_SPHT(*next))
4879 next++;
4880 }
4881 else {
4882 /* Remove useless spaces before the old delimiter. */
4883 while (HTTP_IS_SPHT(*(prev-1)))
4884 prev--;
4885 *from = prev;
4886
4887 /* copy the delimiter and if possible a space if we're
4888 * not at the end of the line.
4889 */
4890 if (next < end) {
4891 *prev++ = *next++;
4892 if (prev + 1 < next)
4893 *prev++ = ' ';
4894 while (next < end && HTTP_IS_SPHT(*next))
4895 next++;
4896 }
4897 }
4898 memmove(prev, next, end - next);
4899 return (prev - next);
4900}
4901
Christopher Faulet0f226952018-10-22 09:29:56 +02004902
4903/* Formats the start line of the request (without CRLF) and puts it in <str> and
Joseph Herlantc42c0e92018-11-25 10:43:27 -08004904 * return the written length. The line can be truncated if it exceeds <len>.
Christopher Faulet0f226952018-10-22 09:29:56 +02004905 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004906static size_t http_fmt_req_line(const struct htx_sl *sl, char *str, size_t len)
Christopher Faulet0f226952018-10-22 09:29:56 +02004907{
4908 struct ist dst = ist2(str, 0);
4909
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004910 if (istcat(&dst, htx_sl_req_meth(sl), len) == -1)
Christopher Faulet0f226952018-10-22 09:29:56 +02004911 goto end;
4912 if (dst.len + 1 > len)
4913 goto end;
4914 dst.ptr[dst.len++] = ' ';
4915
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004916 if (istcat(&dst, htx_sl_req_uri(sl), len) == -1)
Christopher Faulet0f226952018-10-22 09:29:56 +02004917 goto end;
4918 if (dst.len + 1 > len)
4919 goto end;
4920 dst.ptr[dst.len++] = ' ';
4921
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004922 istcat(&dst, htx_sl_req_vsn(sl), len);
Christopher Faulet0f226952018-10-22 09:29:56 +02004923 end:
4924 return dst.len;
4925}
4926
4927/*
4928 * Print a debug line with a start line.
4929 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004930static void http_debug_stline(const char *dir, struct stream *s, const struct htx_sl *sl)
Christopher Faulet0f226952018-10-22 09:29:56 +02004931{
4932 struct session *sess = strm_sess(s);
4933 int max;
4934
4935 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
4936 dir,
4937 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->handle.fd : -1,
4938 objt_cs(s->si[1].end) ? (unsigned short)objt_cs(s->si[1].end)->conn->handle.fd : -1);
4939
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004940 max = HTX_SL_P1_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02004941 UBOUND(max, trash.size - trash.data - 3);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004942 chunk_memcat(&trash, HTX_SL_P1_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02004943 trash.area[trash.data++] = ' ';
4944
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004945 max = HTX_SL_P2_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02004946 UBOUND(max, trash.size - trash.data - 2);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004947 chunk_memcat(&trash, HTX_SL_P2_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02004948 trash.area[trash.data++] = ' ';
4949
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004950 max = HTX_SL_P3_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02004951 UBOUND(max, trash.size - trash.data - 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004952 chunk_memcat(&trash, HTX_SL_P3_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02004953 trash.area[trash.data++] = '\n';
4954
Willy Tarreau2e8ab6b2020-03-14 11:03:20 +01004955 DISGUISE(write(1, trash.area, trash.data));
Christopher Faulet0f226952018-10-22 09:29:56 +02004956}
4957
4958/*
4959 * Print a debug line with a header.
4960 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004961static 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 +02004962{
4963 struct session *sess = strm_sess(s);
4964 int max;
4965
4966 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
4967 dir,
4968 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->handle.fd : -1,
4969 objt_cs(s->si[1].end) ? (unsigned short)objt_cs(s->si[1].end)->conn->handle.fd : -1);
4970
4971 max = n.len;
4972 UBOUND(max, trash.size - trash.data - 3);
4973 chunk_memcat(&trash, n.ptr, max);
4974 trash.area[trash.data++] = ':';
4975 trash.area[trash.data++] = ' ';
4976
4977 max = v.len;
4978 UBOUND(max, trash.size - trash.data - 1);
4979 chunk_memcat(&trash, v.ptr, max);
4980 trash.area[trash.data++] = '\n';
4981
Willy Tarreau2e8ab6b2020-03-14 11:03:20 +01004982 DISGUISE(write(1, trash.area, trash.data));
Christopher Faulet0f226952018-10-22 09:29:56 +02004983}
4984
Christopher Fauleta8a46e22019-07-16 14:53:09 +02004985/* Allocate a new HTTP transaction for stream <s> unless there is one already.
4986 * In case of allocation failure, everything allocated is freed and NULL is
4987 * returned. Otherwise the new transaction is assigned to the stream and
4988 * returned.
4989 */
4990struct http_txn *http_alloc_txn(struct stream *s)
4991{
4992 struct http_txn *txn = s->txn;
4993
4994 if (txn)
4995 return txn;
4996
4997 txn = pool_alloc(pool_head_http_txn);
4998 if (!txn)
4999 return txn;
5000
5001 s->txn = txn;
5002 return txn;
5003}
5004
5005void http_txn_reset_req(struct http_txn *txn)
5006{
Christopher Faulet1aea50e2020-01-17 16:03:53 +01005007 txn->req.flags = 0;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005008 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
5009}
5010
5011void http_txn_reset_res(struct http_txn *txn)
5012{
Christopher Faulet1aea50e2020-01-17 16:03:53 +01005013 txn->rsp.flags = 0;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005014 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
5015}
5016
5017/*
5018 * Initialize a new HTTP transaction for stream <s>. It is assumed that all
5019 * the required fields are properly allocated and that we only need to (re)init
5020 * them. This should be used before processing any new request.
5021 */
5022void http_init_txn(struct stream *s)
5023{
5024 struct http_txn *txn = s->txn;
5025 struct conn_stream *cs = objt_cs(s->si[0].end);
5026
Christopher Fauletda831fa2020-10-06 17:58:43 +02005027 txn->flags = ((cs && cs->flags & CS_FL_NOT_FIRST) ? TX_NOT_FIRST : 0);
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005028 txn->status = -1;
Christopher Faulet5cb513a2020-05-13 17:56:56 +02005029 txn->http_reply = NULL;
Willy Tarreau8b507582020-02-25 09:35:07 +01005030 write_u32(txn->cache_hash, 0);
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005031
5032 txn->cookie_first_date = 0;
5033 txn->cookie_last_date = 0;
5034
5035 txn->srv_cookie = NULL;
5036 txn->cli_cookie = NULL;
5037 txn->uri = NULL;
5038
5039 http_txn_reset_req(txn);
5040 http_txn_reset_res(txn);
5041
5042 txn->req.chn = &s->req;
5043 txn->rsp.chn = &s->res;
5044
5045 txn->auth.method = HTTP_AUTH_UNKNOWN;
5046
5047 vars_init(&s->vars_txn, SCOPE_TXN);
5048 vars_init(&s->vars_reqres, SCOPE_REQ);
5049}
5050
5051/* to be used at the end of a transaction */
5052void http_end_txn(struct stream *s)
5053{
5054 struct http_txn *txn = s->txn;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005055
5056 /* these ones will have been dynamically allocated */
5057 pool_free(pool_head_requri, txn->uri);
5058 pool_free(pool_head_capture, txn->cli_cookie);
5059 pool_free(pool_head_capture, txn->srv_cookie);
Tim Duesterhusa17e6622020-03-05 20:19:02 +01005060 pool_free(pool_head_uniqueid, s->unique_id.ptr);
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005061
Tim Duesterhusa17e6622020-03-05 20:19:02 +01005062 s->unique_id = IST_NULL;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005063 txn->uri = NULL;
5064 txn->srv_cookie = NULL;
5065 txn->cli_cookie = NULL;
5066
Christopher Faulet59399252019-11-07 14:27:52 +01005067 if (!LIST_ISEMPTY(&s->vars_txn.head))
5068 vars_prune(&s->vars_txn, s->sess, s);
5069 if (!LIST_ISEMPTY(&s->vars_reqres.head))
5070 vars_prune(&s->vars_reqres, s->sess, s);
5071}
5072
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005073
5074DECLARE_POOL(pool_head_http_txn, "http_txn", sizeof(struct http_txn));
Christopher Faulet0f226952018-10-22 09:29:56 +02005075
Christopher Fauletf4eb75d2018-10-11 15:55:07 +02005076__attribute__((constructor))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005077static void __http_protocol_init(void)
Christopher Fauletf4eb75d2018-10-11 15:55:07 +02005078{
5079}
5080
5081
5082/*
5083 * Local variables:
5084 * c-indent-level: 8
5085 * c-basic-offset: 8
5086 * End:
5087 */