blob: 33d8b7f3f6c3125179fada625adf66152540b463 [file] [log] [blame]
Christopher Fauletf4eb75d2018-10-11 15:55:07 +02001/*
2 * HTTP protocol analyzer
3 *
4 * Copyright (C) 2018 HAProxy Technologies, Christopher Faulet <cfaulet@haproxy.com>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
Willy Tarreaudcc048a2020-06-04 19:11:43 +020013#include <haproxy/acl.h>
Willy Tarreau122eba92020-06-04 10:15:32 +020014#include <haproxy/action-t.h>
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020015#include <haproxy/api.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020016#include <haproxy/backend.h>
Willy Tarreau8d366972020-05-27 16:10:29 +020017#include <haproxy/base64.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020018#include <haproxy/capture-t.h>
Willy Tarreauf1d32c42020-06-04 21:07:02 +020019#include <haproxy/channel.h>
Willy Tarreau4aa573d2020-06-04 18:21:56 +020020#include <haproxy/check.h>
Willy Tarreau7ea393d2020-06-04 18:02:10 +020021#include <haproxy/connection.h>
Willy Tarreau36979d92020-06-05 17:27:29 +020022#include <haproxy/errors.h>
Willy Tarreauc7babd82020-06-04 21:29:29 +020023#include <haproxy/filters.h>
Willy Tarreaucd72d8c2020-06-02 19:11:26 +020024#include <haproxy/http.h>
Willy Tarreauc2b1ff02020-06-04 21:21:03 +020025#include <haproxy/http_ana.h>
Willy Tarreau87735332020-06-04 09:08:41 +020026#include <haproxy/http_htx.h>
Willy Tarreau16f958c2020-06-03 08:44:35 +020027#include <haproxy/htx.h>
Willy Tarreauaeed4a82020-06-04 22:01:04 +020028#include <haproxy/log.h>
Willy Tarreau6131d6a2020-06-02 16:48:09 +020029#include <haproxy/net_helper.h>
Willy Tarreaua264d962020-06-04 22:29:18 +020030#include <haproxy/proxy.h>
Willy Tarreau7cd8b6e2020-06-02 17:32:26 +020031#include <haproxy/regex.h>
Willy Tarreau1e56f922020-06-04 23:20:13 +020032#include <haproxy/server-t.h>
Willy Tarreau2eec9b52020-06-04 19:58:55 +020033#include <haproxy/stats.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020034#include <haproxy/stream.h>
Willy Tarreau5e539c92020-06-04 20:45:39 +020035#include <haproxy/stream_interface.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020036#include <haproxy/trace.h>
Willy Tarreau8c42b8a2020-06-04 19:27:34 +020037#include <haproxy/uri_auth-t.h>
Willy Tarreaua1718922020-06-04 16:25:31 +020038#include <haproxy/vars.h>
Christopher Faulete0768eb2018-10-03 16:38:02 +020039
Christopher Faulete0768eb2018-10-03 16:38:02 +020040
Christopher Fauleteea8fc72019-11-05 16:18:10 +010041#define TRACE_SOURCE &trace_strm
42
Christopher Faulet377c5a52018-10-24 21:21:30 +020043extern const char *stat_status_codes[];
Christopher Fauletf2824e62018-10-01 12:12:37 +020044
Christopher Fauleta8a46e22019-07-16 14:53:09 +020045struct pool_head *pool_head_requri = NULL;
46struct pool_head *pool_head_capture = NULL;
47
48
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020049static void http_end_request(struct stream *s);
50static void http_end_response(struct stream *s);
Christopher Fauletf2824e62018-10-01 12:12:37 +020051
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020052static void http_capture_headers(struct htx *htx, char **cap, struct cap_hdr *cap_hdr);
53static int http_del_hdr_value(char *start, char *end, char **from, char *next);
54static size_t http_fmt_req_line(const struct htx_sl *sl, char *str, size_t len);
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020055static void http_debug_stline(const char *dir, struct stream *s, const struct htx_sl *sl);
56static void http_debug_hdr(const char *dir, struct stream *s, const struct ist n, const struct ist v);
Christopher Faulet0f226952018-10-22 09:29:56 +020057
Christopher Fauletb58f62b2020-01-13 16:40:13 +010058static enum rule_result http_req_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s);
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020059static enum rule_result http_res_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s);
Christopher Faulet3e964192018-10-24 11:39:23 +020060
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020061static void http_manage_client_side_cookies(struct stream *s, struct channel *req);
62static void http_manage_server_side_cookies(struct stream *s, struct channel *res);
Christopher Fauletfcda7c62018-10-24 11:56:22 +020063
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020064static int http_stats_check_uri(struct stream *s, struct http_txn *txn, struct proxy *backend);
65static int http_handle_stats(struct stream *s, struct channel *req);
Christopher Faulet377c5a52018-10-24 21:21:30 +020066
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020067static int http_handle_expect_hdr(struct stream *s, struct htx *htx, struct http_msg *msg);
68static int http_reply_100_continue(struct stream *s);
Christopher Faulet23a3c792018-11-28 10:01:23 +010069
Christopher Faulete0768eb2018-10-03 16:38:02 +020070/* This stream analyser waits for a complete HTTP request. It returns 1 if the
71 * processing can continue on next analysers, or zero if it either needs more
72 * data or wants to immediately abort the request (eg: timeout, error, ...). It
73 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req.analysers
74 * when it has nothing left to do, and may remove any analyser when it wants to
75 * abort.
76 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020077int http_wait_for_request(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +020078{
Christopher Faulet9768c262018-10-22 09:34:31 +020079
Christopher Faulete0768eb2018-10-03 16:38:02 +020080 /*
Christopher Faulet9768c262018-10-22 09:34:31 +020081 * We will analyze a complete HTTP request to check the its syntax.
Christopher Faulete0768eb2018-10-03 16:38:02 +020082 *
Christopher Faulet9768c262018-10-22 09:34:31 +020083 * Once the start line and all headers are received, we may perform a
84 * capture of the error (if any), and we will set a few fields. We also
85 * check for monitor-uri, logging and finally headers capture.
Christopher Faulete0768eb2018-10-03 16:38:02 +020086 */
Christopher Faulete0768eb2018-10-03 16:38:02 +020087 struct session *sess = s->sess;
88 struct http_txn *txn = s->txn;
89 struct http_msg *msg = &txn->req;
Christopher Faulet9768c262018-10-22 09:34:31 +020090 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +010091 struct htx_sl *sl;
Christopher Faulete0768eb2018-10-03 16:38:02 +020092
Christopher Fauleteea8fc72019-11-05 16:18:10 +010093 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +020094
Christopher Fauletda46a0d2021-01-21 17:32:58 +010095 if (unlikely(!IS_HTX_STRM(s))) {
96 /* It is only possible when a TCP stream is upgrade to HTTP.
97 * There is a transition period during which there is no
98 * data. The stream is still in raw mode and SF_IGNORE flag is
99 * still set. When this happens, the new mux is responsible to
Ilya Shipitsinacf84592021-02-06 22:29:08 +0500100 * handle all errors. Thus we may leave immediately.
Christopher Fauletda46a0d2021-01-21 17:32:58 +0100101 */
102 BUG_ON(!(s->flags & SF_IGNORE) || !c_empty(&s->req));
Christopher Faulet9768c262018-10-22 09:34:31 +0200103
Christopher Faulet97b3a612021-03-15 17:10:12 +0100104 /* Don't connect for now */
105 channel_dont_connect(req);
106
107 /* A SHUTR at this stage means we are performing a "destructive"
108 * HTTP upgrade (TCP>H2). In this case, we can leave.
109 */
110 if (req->flags & CF_SHUTR) {
111 s->logs.logwait = 0;
112 s->logs.level = 0;
113 channel_abort(&s->req);
114 channel_abort(&s->res);
115 req->analysers &= AN_REQ_FLT_END;
116 req->analyse_exp = TICK_ETERNITY;
117 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA, s);
118 return 1;
119 }
Christopher Fauletda46a0d2021-01-21 17:32:58 +0100120 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA, s);
121 return 0;
122 }
123
124 htx = htxbuf(&req->buf);
Christopher Faulet8bebd2f2020-10-06 17:54:56 +0200125
Willy Tarreau4236f032019-03-05 10:43:32 +0100126 /* Parsing errors are caught here */
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200127 if (htx->flags & (HTX_FL_PARSING_ERROR|HTX_FL_PROCESSING_ERROR)) {
Willy Tarreau4236f032019-03-05 10:43:32 +0100128 stream_inc_http_req_ctr(s);
Emeric Brun28976442020-10-07 08:50:09 +0200129 proxy_inc_fe_req_ctr(sess->listener, sess->fe);
Christopher Fauletbf7175f2021-02-10 14:58:01 +0100130 if (htx->flags & HTX_FL_PARSING_ERROR) {
131 stream_inc_http_err_ctr(s);
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200132 goto return_bad_req;
Christopher Fauletbf7175f2021-02-10 14:58:01 +0100133 }
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200134 else
135 goto return_int_err;
Willy Tarreau4236f032019-03-05 10:43:32 +0100136 }
137
Christopher Faulete0768eb2018-10-03 16:38:02 +0200138 /* we're speaking HTTP here, so let's speak HTTP to the client */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200139 s->srv_error = http_return_srv_error;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200140
Christopher Faulet9768c262018-10-22 09:34:31 +0200141 msg->msg_state = HTTP_MSG_BODY;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200142 stream_inc_http_req_ctr(s);
Emeric Brun28976442020-10-07 08:50:09 +0200143 proxy_inc_fe_req_ctr(sess->listener, sess->fe); /* one more valid request for this FE */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200144
Christopher Faulet9768c262018-10-22 09:34:31 +0200145 /* kill the pending keep-alive timeout */
Christopher Faulet9768c262018-10-22 09:34:31 +0200146 req->analyse_exp = TICK_ETERNITY;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200147
Christopher Faulet29f17582019-05-23 11:03:26 +0200148 BUG_ON(htx_get_first_type(htx) != HTX_BLK_REQ_SL);
Christopher Faulet297fbb42019-05-13 14:41:27 +0200149 sl = http_get_stline(htx);
Christopher Faulet03599112018-11-27 11:21:21 +0100150
Christopher Faulet9768c262018-10-22 09:34:31 +0200151 /* 0: we might have to print this header in debug mode */
152 if (unlikely((global.mode & MODE_DEBUG) &&
153 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
154 int32_t pos;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200155
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200156 http_debug_stline("clireq", s, sl);
Christopher Faulet9768c262018-10-22 09:34:31 +0200157
Christopher Fauleta3f15502019-05-13 15:27:23 +0200158 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet9768c262018-10-22 09:34:31 +0200159 struct htx_blk *blk = htx_get_blk(htx, pos);
160 enum htx_blk_type type = htx_get_blk_type(blk);
161
162 if (type == HTX_BLK_EOH)
163 break;
164 if (type != HTX_BLK_HDR)
165 continue;
166
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200167 http_debug_hdr("clihdr", s,
168 htx_get_blk_name(htx, blk),
169 htx_get_blk_value(htx, blk));
Christopher Faulet9768c262018-10-22 09:34:31 +0200170 }
171 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200172
173 /*
Christopher Faulet03599112018-11-27 11:21:21 +0100174 * 1: identify the method and the version. Also set HTTP flags
Christopher Faulete0768eb2018-10-03 16:38:02 +0200175 */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100176 txn->meth = sl->info.req.meth;
Christopher Faulet03599112018-11-27 11:21:21 +0100177 if (sl->flags & HTX_SL_F_VER_11)
Christopher Faulet9768c262018-10-22 09:34:31 +0200178 msg->flags |= HTTP_MSGF_VER_11;
Christopher Faulet03599112018-11-27 11:21:21 +0100179 msg->flags |= HTTP_MSGF_XFER_LEN;
Christopher Faulet2a408542020-11-20 14:22:37 +0100180 if (sl->flags & HTX_SL_F_CLEN)
181 msg->flags |= HTTP_MSGF_CNT_LEN;
182 else if (sl->flags & HTX_SL_F_CHNK)
183 msg->flags |= HTTP_MSGF_TE_CHNK;
Christopher Fauletb2db4fa2018-11-27 16:51:09 +0100184 if (sl->flags & HTX_SL_F_BODYLESS)
185 msg->flags |= HTTP_MSGF_BODYLESS;
Christopher Faulet576c3582021-01-08 15:53:01 +0100186 if (sl->flags & HTX_SL_F_CONN_UPG)
187 msg->flags |= HTTP_MSGF_CONN_UPG;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200188
189 /* we can make use of server redirect on GET and HEAD */
190 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
191 s->flags |= SF_REDIRECTABLE;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100192 else if (txn->meth == HTTP_METH_OTHER && isteqi(htx_sl_req_meth(sl), ist("PRI"))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200193 /* PRI is reserved for the HTTP/2 preface */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200194 goto return_bad_req;
195 }
196
197 /*
Christopher Faulet6072beb2020-02-18 15:34:58 +0100198 * 2: check if the URI matches the monitor_uri. We have to do this for
199 * every request which gets in, because the monitor-uri is defined by
200 * the frontend. If the monitor-uri starts with a '/', the matching is
201 * done against the request's path. Otherwise, the request's uri is
202 * used. It is a workaround to let HTTP/2 health-checks work as
203 * expected.
Christopher Faulete0768eb2018-10-03 16:38:02 +0200204 */
205 if (unlikely((sess->fe->monitor_uri_len != 0) &&
Christopher Faulet6072beb2020-02-18 15:34:58 +0100206 ((*sess->fe->monitor_uri == '/' && isteq(http_get_path(htx_sl_req_uri(sl)),
207 ist2(sess->fe->monitor_uri, sess->fe->monitor_uri_len))) ||
208 isteq(htx_sl_req_uri(sl), ist2(sess->fe->monitor_uri, sess->fe->monitor_uri_len))))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200209 /*
210 * We have found the monitor URI
211 */
212 struct acl_cond *cond;
213
214 s->flags |= SF_MONITOR;
Olivier Houcharda798bf52019-03-08 18:52:00 +0100215 _HA_ATOMIC_ADD(&sess->fe->fe_counters.intercepted_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200216
217 /* Check if we want to fail this monitor request or not */
218 list_for_each_entry(cond, &sess->fe->mon_fail_cond, list) {
219 int ret = acl_exec_cond(cond, sess->fe, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
220
221 ret = acl_pass(ret);
222 if (cond->pol == ACL_COND_UNLESS)
223 ret = !ret;
224
225 if (ret) {
226 /* we fail this request, let's return 503 service unavail */
227 txn->status = 503;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200228 if (!(s->flags & SF_ERR_MASK))
229 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
230 goto return_prx_cond;
231 }
232 }
233
Joseph Herlantc42c0e92018-11-25 10:43:27 -0800234 /* nothing to fail, let's reply normally */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200235 txn->status = 200;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200236 if (!(s->flags & SF_ERR_MASK))
237 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
238 goto return_prx_cond;
239 }
240
241 /*
242 * 3: Maybe we have to copy the original REQURI for the logs ?
243 * Note: we cannot log anymore if the request has been
244 * classified as invalid.
245 */
246 if (unlikely(s->logs.logwait & LW_REQ)) {
247 /* we have a complete HTTP request that we must log */
248 if ((txn->uri = pool_alloc(pool_head_requri)) != NULL) {
Christopher Faulet9768c262018-10-22 09:34:31 +0200249 size_t len;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200250
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200251 len = http_fmt_req_line(sl, txn->uri, global.tune.requri_len - 1);
Christopher Faulet9768c262018-10-22 09:34:31 +0200252 txn->uri[len] = 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200253
254 if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT)))
255 s->do_log(s);
256 } else {
257 ha_alert("HTTP logging : out of memory.\n");
258 }
259 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200260
Christopher Faulete0768eb2018-10-03 16:38:02 +0200261 /* if the frontend has "option http-use-proxy-header", we'll check if
262 * we have what looks like a proxied connection instead of a connection,
263 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
264 * Note that this is *not* RFC-compliant, however browsers and proxies
265 * happen to do that despite being non-standard :-(
266 * We consider that a request not beginning with either '/' or '*' is
267 * a proxied connection, which covers both "scheme://location" and
268 * CONNECT ip:port.
269 */
270 if ((sess->fe->options2 & PR_O2_USE_PXHDR) &&
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100271 *HTX_SL_REQ_UPTR(sl) != '/' && *HTX_SL_REQ_UPTR(sl) != '*')
Christopher Faulete0768eb2018-10-03 16:38:02 +0200272 txn->flags |= TX_USE_PX_CONN;
273
Christopher Faulete0768eb2018-10-03 16:38:02 +0200274 /* 5: we may need to capture headers */
275 if (unlikely((s->logs.logwait & LW_REQHDR) && s->req_cap))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200276 http_capture_headers(htx, s->req_cap, sess->fe->req_cap);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200277
Christopher Faulete0768eb2018-10-03 16:38:02 +0200278 /* we may have to wait for the request's body */
Christopher Faulet9768c262018-10-22 09:34:31 +0200279 if (s->be->options & PR_O_WREQ_BODY)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200280 req->analysers |= AN_REQ_HTTP_BODY;
281
282 /*
283 * RFC7234#4:
284 * A cache MUST write through requests with methods
285 * that are unsafe (Section 4.2.1 of [RFC7231]) to
286 * the origin server; i.e., a cache is not allowed
287 * to generate a reply to such a request before
288 * having forwarded the request and having received
289 * a corresponding response.
290 *
291 * RFC7231#4.2.1:
292 * Of the request methods defined by this
293 * specification, the GET, HEAD, OPTIONS, and TRACE
294 * methods are defined to be safe.
295 */
296 if (likely(txn->meth == HTTP_METH_GET ||
297 txn->meth == HTTP_METH_HEAD ||
298 txn->meth == HTTP_METH_OPTIONS ||
299 txn->meth == HTTP_METH_TRACE))
300 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
301
302 /* end of job, return OK */
303 req->analysers &= ~an_bit;
304 req->analyse_exp = TICK_ETERNITY;
Christopher Faulet9768c262018-10-22 09:34:31 +0200305
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100306 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200307 return 1;
308
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200309 return_int_err:
310 txn->status = 500;
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200311 if (!(s->flags & SF_ERR_MASK))
312 s->flags |= SF_ERR_INTERNAL;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100313 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
William Lallemand36119de2021-03-08 15:26:48 +0100314 if (sess->listener && sess->listener->counters)
Christopher Fauletb8a53712019-12-16 11:29:38 +0100315 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200316 goto return_prx_cond;
317
Christopher Faulete0768eb2018-10-03 16:38:02 +0200318 return_bad_req:
Christopher Faulet9768c262018-10-22 09:34:31 +0200319 txn->status = 400;
Olivier Houcharda798bf52019-03-08 18:52:00 +0100320 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
William Lallemand36119de2021-03-08 15:26:48 +0100321 if (sess->listener && sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100322 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200323 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200324
325 return_prx_cond:
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200326 http_reply_and_close(s, txn->status, http_error_message(s));
327
Christopher Faulete0768eb2018-10-03 16:38:02 +0200328 if (!(s->flags & SF_ERR_MASK))
329 s->flags |= SF_ERR_PRXCOND;
330 if (!(s->flags & SF_FINST_MASK))
331 s->flags |= SF_FINST_R;
332
333 req->analysers &= AN_REQ_FLT_END;
334 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100335 DBG_TRACE_DEVEL("leaving on error",
336 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200337 return 0;
338}
339
340
341/* This stream analyser runs all HTTP request processing which is common to
342 * frontends and backends, which means blocking ACLs, filters, connection-close,
343 * reqadd, stats and redirects. This is performed for the designated proxy.
344 * It returns 1 if the processing can continue on next analysers, or zero if it
345 * either needs more data or wants to immediately abort the request (eg: deny,
346 * error, ...).
347 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200348int http_process_req_common(struct stream *s, struct channel *req, int an_bit, struct proxy *px)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200349{
350 struct session *sess = s->sess;
351 struct http_txn *txn = s->txn;
352 struct http_msg *msg = &txn->req;
Christopher Fauletff2759f2018-10-24 11:13:16 +0200353 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200354 struct redirect_rule *rule;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200355 enum rule_result verdict;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200356 struct connection *conn = objt_conn(sess->origin);
357
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100358 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200359
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100360 htx = htxbuf(&req->buf);
Christopher Fauletff2759f2018-10-24 11:13:16 +0200361
Christopher Faulet1907ccc2019-04-29 13:12:02 +0200362 /* just in case we have some per-backend tracking. Only called the first
363 * execution of the analyser. */
364 if (!s->current_rule || s->current_rule_list != &px->http_req_rules)
365 stream_inc_be_http_req_ctr(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200366
367 /* evaluate http-request rules */
368 if (!LIST_ISEMPTY(&px->http_req_rules)) {
Christopher Fauletb58f62b2020-01-13 16:40:13 +0100369 verdict = http_req_get_intercept_rule(px, &px->http_req_rules, s);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200370
371 switch (verdict) {
372 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
373 goto return_prx_yield;
374
375 case HTTP_RULE_RES_CONT:
376 case HTTP_RULE_RES_STOP: /* nothing to do */
377 break;
378
379 case HTTP_RULE_RES_DENY: /* deny or tarpit */
380 if (txn->flags & TX_CLTARPIT)
381 goto tarpit;
382 goto deny;
383
384 case HTTP_RULE_RES_ABRT: /* abort request, response already sent. Eg: auth */
385 goto return_prx_cond;
386
387 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
388 goto done;
389
390 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
391 goto return_bad_req;
Christopher Faulet3a26bee2019-12-16 12:47:40 +0100392
393 case HTTP_RULE_RES_ERROR: /* failed with a bad request */
394 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200395 }
396 }
397
398 if (conn && (conn->flags & CO_FL_EARLY_DATA) &&
Olivier Houchard220a26c2020-01-23 14:57:36 +0100399 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_SSL_WAIT_HS))) {
Christopher Fauletff2759f2018-10-24 11:13:16 +0200400 struct http_hdr_ctx ctx;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200401
Christopher Fauletff2759f2018-10-24 11:13:16 +0200402 ctx.blk = NULL;
403 if (!http_find_header(htx, ist("Early-Data"), &ctx, 0)) {
404 if (unlikely(!http_add_header(htx, ist("Early-Data"), ist("1"))))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100405 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200406 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200407 }
408
409 /* OK at this stage, we know that the request was accepted according to
410 * the http-request rules, we can check for the stats. Note that the
411 * URI is detected *before* the req* rules in order not to be affected
412 * by a possible reqrep, while they are processed *after* so that a
413 * reqdeny can still block them. This clearly needs to change in 1.6!
414 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200415 if (!s->target && http_stats_check_uri(s, txn, px)) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200416 s->target = &http_stats_applet.obj_type;
Willy Tarreau14bfe9a2018-12-19 15:19:27 +0100417 if (unlikely(!si_register_handler(&s->si[1], objt_applet(s->target)))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200418 s->logs.tv_request = now;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200419 if (!(s->flags & SF_ERR_MASK))
420 s->flags |= SF_ERR_RESOURCE;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100421 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200422 }
423
424 /* parse the whole stats request and extract the relevant information */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200425 http_handle_stats(s, req);
Christopher Fauletb58f62b2020-01-13 16:40:13 +0100426 verdict = http_req_get_intercept_rule(px, &px->uri_auth->http_req_rules, s);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200427 /* not all actions implemented: deny, allow, auth */
428
429 if (verdict == HTTP_RULE_RES_DENY) /* stats http-request deny */
430 goto deny;
431
432 if (verdict == HTTP_RULE_RES_ABRT) /* stats auth / stats http-request auth */
433 goto return_prx_cond;
Christopher Faulet3a26bee2019-12-16 12:47:40 +0100434
435 if (verdict == HTTP_RULE_RES_BADREQ) /* failed with a bad request */
436 goto return_bad_req;
437
438 if (verdict == HTTP_RULE_RES_ERROR) /* failed with a bad request */
439 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200440 }
441
Christopher Faulet2571bc62019-03-01 11:44:26 +0100442 /* Proceed with the applets now. */
443 if (unlikely(objt_applet(s->target))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200444 if (sess->fe == s->be) /* report it if the request was intercepted by the frontend */
Olivier Houcharda798bf52019-03-08 18:52:00 +0100445 _HA_ATOMIC_ADD(&sess->fe->fe_counters.intercepted_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200446
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200447 if (http_handle_expect_hdr(s, htx, msg) == -1)
Christopher Fauletb8a53712019-12-16 11:29:38 +0100448 goto return_int_err;
Christopher Fauletbcf242a2019-03-01 11:36:26 +0100449
Christopher Faulete0768eb2018-10-03 16:38:02 +0200450 if (!(s->flags & SF_ERR_MASK)) // this is not really an error but it is
451 s->flags |= SF_ERR_LOCAL; // to mark that it comes from the proxy
452 if (!(s->flags & SF_FINST_MASK))
453 s->flags |= SF_FINST_R;
454
Christopher Fauletc2ac5e42021-03-08 18:20:09 +0100455 if (HAS_FILTERS(s))
456 req->analysers |= AN_REQ_FLT_HTTP_HDRS;
457
Christopher Faulete0768eb2018-10-03 16:38:02 +0200458 /* enable the minimally required analyzers to handle keep-alive and compression on the HTTP response */
459 req->analysers &= (AN_REQ_HTTP_BODY | AN_REQ_FLT_HTTP_HDRS | AN_REQ_FLT_END);
460 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
461 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Christopher Fauletbcf242a2019-03-01 11:36:26 +0100462
463 req->flags |= CF_SEND_DONTWAIT;
464 s->flags |= SF_ASSIGNED;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200465 goto done;
466 }
467
468 /* check whether we have some ACLs set to redirect this request */
469 list_for_each_entry(rule, &px->redirect_rules, list) {
470 if (rule->cond) {
471 int ret;
472
473 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
474 ret = acl_pass(ret);
475 if (rule->cond->pol == ACL_COND_UNLESS)
476 ret = !ret;
477 if (!ret)
478 continue;
479 }
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200480 if (!http_apply_redirect_rule(rule, s, txn))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100481 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200482 goto done;
483 }
484
485 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
486 * If this happens, then the data will not come immediately, so we must
487 * send all what we have without waiting. Note that due to the small gain
488 * in waiting for the body of the request, it's easier to simply put the
489 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
490 * itself once used.
491 */
492 req->flags |= CF_SEND_DONTWAIT;
493
494 done: /* done with this analyser, continue with next ones that the calling
495 * points will have set, if any.
496 */
497 req->analyse_exp = TICK_ETERNITY;
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +0500498 done_without_exp: /* done with this analyser, but don't reset the analyse_exp. */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200499 req->analysers &= ~an_bit;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100500 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200501 return 1;
502
503 tarpit:
504 /* Allow cookie logging
505 */
506 if (s->be->cookie_name || sess->fe->capture_name)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200507 http_manage_client_side_cookies(s, req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200508
509 /* When a connection is tarpitted, we use the tarpit timeout,
510 * which may be the same as the connect timeout if unspecified.
511 * If unset, then set it to zero because we really want it to
512 * eventually expire. We build the tarpit as an analyser.
513 */
Christopher Faulet202c6ce2019-01-07 14:57:35 +0100514 channel_htx_erase(&s->req, htx);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200515
516 /* wipe the request out so that we can drop the connection early
517 * if the client closes first.
518 */
519 channel_dont_connect(req);
520
Christopher Faulete0768eb2018-10-03 16:38:02 +0200521 req->analysers &= AN_REQ_FLT_END; /* remove switching rules etc... */
522 req->analysers |= AN_REQ_HTTP_TARPIT;
523 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
524 if (!req->analyse_exp)
525 req->analyse_exp = tick_add(now_ms, 0);
526 stream_inc_http_err_ctr(s);
Olivier Houcharda798bf52019-03-08 18:52:00 +0100527 _HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100528 if (s->flags & SF_BE_ASSIGNED)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100529 _HA_ATOMIC_ADD(&s->be->be_counters.denied_req, 1);
William Lallemand36119de2021-03-08 15:26:48 +0100530 if (sess->listener && sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100531 _HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200532 goto done_without_exp;
533
534 deny: /* this request was blocked (denied) */
535
536 /* Allow cookie logging
537 */
538 if (s->be->cookie_name || sess->fe->capture_name)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200539 http_manage_client_side_cookies(s, req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200540
Christopher Faulete0768eb2018-10-03 16:38:02 +0200541 s->logs.tv_request = now;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200542 stream_inc_http_err_ctr(s);
Olivier Houcharda798bf52019-03-08 18:52:00 +0100543 _HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100544 if (s->flags & SF_BE_ASSIGNED)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100545 _HA_ATOMIC_ADD(&s->be->be_counters.denied_req, 1);
William Lallemand36119de2021-03-08 15:26:48 +0100546 if (sess->listener && sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100547 _HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100548 goto return_prx_err;
549
550 return_int_err:
551 txn->status = 500;
552 if (!(s->flags & SF_ERR_MASK))
553 s->flags |= SF_ERR_INTERNAL;
554 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100555 if (s->flags & SF_BE_ASSIGNED)
556 _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1);
William Lallemand36119de2021-03-08 15:26:48 +0100557 if (sess->listener && sess->listener->counters)
Christopher Fauletb8a53712019-12-16 11:29:38 +0100558 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
559 goto return_prx_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200560
561 return_bad_req:
Christopher Faulete0768eb2018-10-03 16:38:02 +0200562 txn->status = 400;
Olivier Houcharda798bf52019-03-08 18:52:00 +0100563 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
William Lallemand36119de2021-03-08 15:26:48 +0100564 if (sess->listener && sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100565 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100566 /* fall through */
567
568 return_prx_err:
569 http_reply_and_close(s, txn->status, http_error_message(s));
570 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200571
572 return_prx_cond:
573 if (!(s->flags & SF_ERR_MASK))
574 s->flags |= SF_ERR_PRXCOND;
575 if (!(s->flags & SF_FINST_MASK))
576 s->flags |= SF_FINST_R;
577
578 req->analysers &= AN_REQ_FLT_END;
579 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100580 DBG_TRACE_DEVEL("leaving on error",
581 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200582 return 0;
583
584 return_prx_yield:
585 channel_dont_connect(req);
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100586 DBG_TRACE_DEVEL("waiting for more data",
587 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200588 return 0;
589}
590
591/* This function performs all the processing enabled for the current request.
592 * It returns 1 if the processing can continue on next analysers, or zero if it
593 * needs more data, encounters an error, or wants to immediately abort the
594 * request. It relies on buffers flags, and updates s->req.analysers.
595 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200596int http_process_request(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200597{
598 struct session *sess = s->sess;
599 struct http_txn *txn = s->txn;
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200600 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200601 struct connection *cli_conn = objt_conn(strm_sess(s)->origin);
602
Christopher Faulet8bebd2f2020-10-06 17:54:56 +0200603 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200604
605 /*
606 * Right now, we know that we have processed the entire headers
607 * and that unwanted requests have been filtered out. We can do
608 * whatever we want with the remaining request. Also, now we
609 * may have separate values for ->fe, ->be.
610 */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100611 htx = htxbuf(&req->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200612
613 /*
614 * If HTTP PROXY is set we simply get remote server address parsing
Willy Tarreau1c8d32b2019-07-18 15:47:45 +0200615 * incoming request.
Christopher Faulete0768eb2018-10-03 16:38:02 +0200616 */
617 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SF_ADDR_SET)) {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100618 struct htx_sl *sl;
619 struct ist uri, path;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200620
Willy Tarreau9b7587a2020-10-15 07:32:10 +0200621 if (!sockaddr_alloc(&s->target_addr, NULL, 0)) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200622 if (!(s->flags & SF_ERR_MASK))
623 s->flags |= SF_ERR_RESOURCE;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100624 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200625 }
Christopher Faulet297fbb42019-05-13 14:41:27 +0200626 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100627 uri = htx_sl_req_uri(sl);
628 path = http_get_path(uri);
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200629
Willy Tarreau1c8d32b2019-07-18 15:47:45 +0200630 if (url2sa(uri.ptr, uri.len - path.len, s->target_addr, NULL) == -1)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200631 goto return_bad_req;
632
Willy Tarreau1c8d32b2019-07-18 15:47:45 +0200633 s->target = &s->be->obj_type;
634 s->flags |= SF_ADDR_SET | SF_ASSIGNED;
635
Christopher Faulete0768eb2018-10-03 16:38:02 +0200636 /* if the path was found, we have to remove everything between
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200637 * uri.ptr and path.ptr (excluded). If it was not found, we need
638 * to replace from all the uri by a single "/".
639 *
Ilya Shipitsin46a030c2020-07-05 16:36:08 +0500640 * Instead of rewriting the whole start line, we just update
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100641 * the star-line URI. Some space will be lost but it should be
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200642 * insignificant.
Christopher Faulete0768eb2018-10-03 16:38:02 +0200643 */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100644 istcpy(&uri, (path.len ? path : ist("/")), uri.len);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200645 }
646
647 /*
648 * 7: Now we can work with the cookies.
649 * Note that doing so might move headers in the request, but
650 * the fields will stay coherent and the URI will not move.
651 * This should only be performed in the backend.
652 */
653 if (s->be->cookie_name || sess->fe->capture_name)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200654 http_manage_client_side_cookies(s, req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200655
Tim Duesterhus2825b4b2020-02-28 15:13:34 +0100656 /* 8: Generate unique ID if a "unique-id-format" is defined.
657 *
658 * A unique ID is generated even when it is not sent to ensure that the ID can make use of
659 * fetches only available in the HTTP request processing stage.
660 */
661 if (!LIST_ISEMPTY(&sess->fe->format_unique_id)) {
Tim Duesterhusa17e6622020-03-05 20:19:02 +0100662 struct ist unique_id = stream_generate_unique_id(s, &sess->fe->format_unique_id);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200663
Tim Duesterhusa17e6622020-03-05 20:19:02 +0100664 if (!isttest(unique_id)) {
Christopher Fauletb8a53712019-12-16 11:29:38 +0100665 if (!(s->flags & SF_ERR_MASK))
666 s->flags |= SF_ERR_RESOURCE;
667 goto return_int_err;
668 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200669
Tim Duesterhus2825b4b2020-02-28 15:13:34 +0100670 /* send unique ID if a "unique-id-header" is defined */
Tim Duesterhus0643b0e2020-03-05 17:56:35 +0100671 if (isttest(sess->fe->header_unique_id) &&
Tim Duesterhusa17e6622020-03-05 20:19:02 +0100672 unlikely(!http_add_header(htx, sess->fe->header_unique_id, s->unique_id)))
Tim Duesterhus2825b4b2020-02-28 15:13:34 +0100673 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200674 }
675
676 /*
677 * 9: add X-Forwarded-For if either the frontend or the backend
678 * asks for it.
679 */
680 if ((sess->fe->options | s->be->options) & PR_O_FWDFOR) {
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200681 struct http_hdr_ctx ctx = { .blk = NULL };
682 struct ist hdr = ist2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : sess->fe->fwdfor_hdr_name,
683 s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : sess->fe->fwdfor_hdr_len);
684
Christopher Faulete0768eb2018-10-03 16:38:02 +0200685 if (!((sess->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200686 http_find_header(htx, hdr, &ctx, 0)) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200687 /* The header is set to be added only if none is present
688 * and we found it, so don't do anything.
689 */
690 }
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200691 else if (cli_conn && conn_get_src(cli_conn) && cli_conn->src->ss_family == AF_INET) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200692 /* Add an X-Forwarded-For header unless the source IP is
693 * in the 'except' network range.
694 */
Christopher Faulet5d1def62021-02-26 09:19:15 +0100695 if (ipcmp2net(cli_conn->src, &sess->fe->except_xff_net) &&
696 ipcmp2net(cli_conn->src, &s->be->except_xff_net)) {
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200697 unsigned char *pn = (unsigned char *)&((struct sockaddr_in *)cli_conn->src)->sin_addr;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200698
699 /* Note: we rely on the backend to get the header name to be used for
700 * x-forwarded-for, because the header is really meant for the backends.
701 * However, if the backend did not specify any option, we have to rely
702 * on the frontend's header name.
703 */
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200704 chunk_printf(&trash, "%d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
705 if (unlikely(!http_add_header(htx, hdr, ist2(trash.area, trash.data))))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100706 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200707 }
708 }
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200709 else if (cli_conn && conn_get_src(cli_conn) && cli_conn->src->ss_family == AF_INET6) {
Christopher Faulet5d1def62021-02-26 09:19:15 +0100710 /* Add an X-Forwarded-For header unless the source IP is
711 * in the 'except' network range.
Christopher Faulete0768eb2018-10-03 16:38:02 +0200712 */
Christopher Faulet5d1def62021-02-26 09:19:15 +0100713 if (ipcmp2net(cli_conn->src, &sess->fe->except_xff_net) &&
714 ipcmp2net(cli_conn->src, &s->be->except_xff_net)) {
715 char pn[INET6_ADDRSTRLEN];
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200716
Christopher Faulet5d1def62021-02-26 09:19:15 +0100717 inet_ntop(AF_INET6,
718 (const void *)&((struct sockaddr_in6 *)(cli_conn->src))->sin6_addr,
719 pn, sizeof(pn));
Christopher Faulete0768eb2018-10-03 16:38:02 +0200720
Christopher Faulet5d1def62021-02-26 09:19:15 +0100721 /* Note: we rely on the backend to get the header name to be used for
722 * x-forwarded-for, because the header is really meant for the backends.
723 * However, if the backend did not specify any option, we have to rely
724 * on the frontend's header name.
725 */
726 chunk_printf(&trash, "%s", pn);
727 if (unlikely(!http_add_header(htx, hdr, ist2(trash.area, trash.data))))
728 goto return_int_err;
729 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200730 }
731 }
732
733 /*
734 * 10: add X-Original-To if either the frontend or the backend
735 * asks for it.
736 */
737 if ((sess->fe->options | s->be->options) & PR_O_ORGTO) {
Christopher Faulet5d1def62021-02-26 09:19:15 +0100738 struct ist hdr = ist2(s->be->orgto_hdr_len ? s->be->orgto_hdr_name : sess->fe->orgto_hdr_name,
739 s->be->orgto_hdr_len ? s->be->orgto_hdr_len : sess->fe->orgto_hdr_len);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200740
Christopher Fauletcccded92021-02-26 12:45:56 +0100741 if (cli_conn && conn_get_dst(cli_conn) && cli_conn->dst->ss_family == AF_INET) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200742 /* Add an X-Original-To header unless the destination IP is
743 * in the 'except' network range.
744 */
Christopher Faulet5d1def62021-02-26 09:19:15 +0100745 if (ipcmp2net(cli_conn->dst, &sess->fe->except_xot_net) &&
746 ipcmp2net(cli_conn->dst, &s->be->except_xot_net)) {
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200747 unsigned char *pn = (unsigned char *)&((struct sockaddr_in *)cli_conn->dst)->sin_addr;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200748
749 /* Note: we rely on the backend to get the header name to be used for
750 * x-original-to, because the header is really meant for the backends.
751 * However, if the backend did not specify any option, we have to rely
752 * on the frontend's header name.
753 */
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200754 chunk_printf(&trash, "%d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
755 if (unlikely(!http_add_header(htx, hdr, ist2(trash.area, trash.data))))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100756 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200757 }
758 }
Christopher Faulet5d1def62021-02-26 09:19:15 +0100759 else if (cli_conn && conn_get_dst(cli_conn) && cli_conn->dst->ss_family == AF_INET6) {
760 /* Add an X-Original-To header unless the source IP is
761 * in the 'except' network range.
762 */
763 if (ipcmp2net(cli_conn->dst, &sess->fe->except_xot_net) &&
764 ipcmp2net(cli_conn->dst, &s->be->except_xot_net)) {
765 char pn[INET6_ADDRSTRLEN];
766
767 inet_ntop(AF_INET6,
768 (const void *)&((struct sockaddr_in6 *)(cli_conn->dst))->sin6_addr,
769 pn, sizeof(pn));
770
771 /* Note: we rely on the backend to get the header name to be used for
772 * x-forwarded-for, because the header is really meant for the backends.
773 * However, if the backend did not specify any option, we have to rely
774 * on the frontend's header name.
775 */
776 chunk_printf(&trash, "%s", pn);
777 if (unlikely(!http_add_header(htx, hdr, ist2(trash.area, trash.data))))
778 goto return_int_err;
779 }
780 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200781 }
782
Christopher Fauletc2ac5e42021-03-08 18:20:09 +0100783 /* Filter the request headers if there are filters attached to the
784 * stream.
785 */
786 if (HAS_FILTERS(s))
787 req->analysers |= AN_REQ_FLT_HTTP_HDRS;
788
Christopher Faulete0768eb2018-10-03 16:38:02 +0200789 /* If we have no server assigned yet and we're balancing on url_param
790 * with a POST request, we may be interested in checking the body for
791 * that parameter. This will be done in another analyser.
792 */
793 if (!(s->flags & (SF_ASSIGNED|SF_DIRECT)) &&
Willy Tarreau089eaa02019-01-14 15:17:46 +0100794 s->txn->meth == HTTP_METH_POST &&
795 (s->be->lbprm.algo & BE_LB_ALGO) == BE_LB_ALGO_PH) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200796 channel_dont_connect(req);
797 req->analysers |= AN_REQ_HTTP_BODY;
798 }
799
800 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
801 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau1a18b542018-12-11 16:37:42 +0100802
Christopher Faulete0768eb2018-10-03 16:38:02 +0200803 /* We expect some data from the client. Unless we know for sure
804 * we already have a full request, we have to re-enable quick-ack
805 * in case we previously disabled it, otherwise we might cause
806 * the client to delay further data.
807 */
William Lallemand36119de2021-03-08 15:26:48 +0100808 if ((sess->listener && (sess->listener->options & LI_O_NOQUICKACK)) && !(htx->flags & HTX_FL_EOM))
Willy Tarreau1a18b542018-12-11 16:37:42 +0100809 conn_set_quickack(cli_conn, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200810
811 /*************************************************************
812 * OK, that's finished for the headers. We have done what we *
813 * could. Let's switch to the DATA state. *
814 ************************************************************/
815 req->analyse_exp = TICK_ETERNITY;
816 req->analysers &= ~an_bit;
817
818 s->logs.tv_request = now;
819 /* OK let's go on with the BODY now */
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100820 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200821 return 1;
822
Christopher Fauletb8a53712019-12-16 11:29:38 +0100823 return_int_err:
824 txn->status = 500;
825 if (!(s->flags & SF_ERR_MASK))
826 s->flags |= SF_ERR_INTERNAL;
827 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100828 if (s->flags & SF_BE_ASSIGNED)
Christopher Fauletbe20cf32020-01-24 11:41:38 +0100829 _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1);
William Lallemand36119de2021-03-08 15:26:48 +0100830 if (sess->listener && sess->listener->counters)
Christopher Fauletb8a53712019-12-16 11:29:38 +0100831 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
832 goto return_prx_cond;
833
Christopher Faulete0768eb2018-10-03 16:38:02 +0200834 return_bad_req: /* let's centralize all bad requests */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200835 txn->status = 400;
Olivier Houcharda798bf52019-03-08 18:52:00 +0100836 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
William Lallemand36119de2021-03-08 15:26:48 +0100837 if (sess->listener && sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100838 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100839 /* fall through */
840
841 return_prx_cond:
842 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +0200843
844 if (!(s->flags & SF_ERR_MASK))
845 s->flags |= SF_ERR_PRXCOND;
846 if (!(s->flags & SF_FINST_MASK))
847 s->flags |= SF_FINST_R;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100848
849 req->analysers &= AN_REQ_FLT_END;
850 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100851 DBG_TRACE_DEVEL("leaving on error",
852 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200853 return 0;
854}
855
856/* This function is an analyser which processes the HTTP tarpit. It always
857 * returns zero, at the beginning because it prevents any other processing
858 * from occurring, and at the end because it terminates the request.
859 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200860int http_process_tarpit(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200861{
862 struct http_txn *txn = s->txn;
863
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100864 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, &txn->req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200865 /* This connection is being tarpitted. The CLIENT side has
866 * already set the connect expiration date to the right
867 * timeout. We just have to check that the client is still
868 * there and that the timeout has not expired.
869 */
870 channel_dont_connect(req);
871 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100872 !tick_is_expired(req->analyse_exp, now_ms)) {
873 DBG_TRACE_DEVEL("waiting for tarpit timeout expiry",
874 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200875 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100876 }
877
Christopher Faulete0768eb2018-10-03 16:38:02 +0200878
879 /* We will set the queue timer to the time spent, just for
880 * logging purposes. We fake a 500 server error, so that the
881 * attacker will not suspect his connection has been tarpitted.
882 * It will not cause trouble to the logs because we can exclude
883 * the tarpitted connections by filtering on the 'PT' status flags.
884 */
885 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
886
Christopher Faulet8dfeccf2020-05-15 14:16:29 +0200887 http_reply_and_close(s, txn->status, (!(req->flags & CF_READ_ERROR) ? http_error_message(s) : NULL));
Christopher Faulet5cb513a2020-05-13 17:56:56 +0200888
889 end:
Christopher Faulete0768eb2018-10-03 16:38:02 +0200890 req->analysers &= AN_REQ_FLT_END;
891 req->analyse_exp = TICK_ETERNITY;
892
893 if (!(s->flags & SF_ERR_MASK))
894 s->flags |= SF_ERR_PRXCOND;
895 if (!(s->flags & SF_FINST_MASK))
896 s->flags |= SF_FINST_T;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100897
898 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200899 return 0;
900}
901
902/* This function is an analyser which waits for the HTTP request body. It waits
903 * for either the buffer to be full, or the full advertised contents to have
904 * reached the buffer. It must only be called after the standard HTTP request
905 * processing has occurred, because it expects the request to be parsed and will
906 * look for the Expect header. It may send a 100-Continue interim response. It
907 * takes in input any state starting from HTTP_MSG_BODY and leaves with one of
908 * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it
909 * needs to read more data, or 1 once it has completed its analysis.
910 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200911int http_wait_for_request_body(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200912{
913 struct session *sess = s->sess;
914 struct http_txn *txn = s->txn;
915 struct http_msg *msg = &s->txn->req;
916
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100917 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Fauletf76ebe82018-10-24 11:16:22 +0200918
Christopher Fauletf76ebe82018-10-24 11:16:22 +0200919
Christopher Faulet021a8e42021-03-29 10:46:38 +0200920 switch (http_wait_for_msg_body(s, req, s->be->timeout.httpreq, 0)) {
921 case HTTP_RULE_RES_CONT:
922 goto http_end;
923 case HTTP_RULE_RES_YIELD:
924 goto missing_data_or_waiting;
925 case HTTP_RULE_RES_BADREQ:
Willy Tarreau4236f032019-03-05 10:43:32 +0100926 goto return_bad_req;
Christopher Faulet021a8e42021-03-29 10:46:38 +0200927 case HTTP_RULE_RES_ERROR:
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200928 goto return_int_err;
Christopher Faulet021a8e42021-03-29 10:46:38 +0200929 case HTTP_RULE_RES_ABRT:
Christopher Fauletb8a53712019-12-16 11:29:38 +0100930 goto return_prx_cond;
Christopher Faulet021a8e42021-03-29 10:46:38 +0200931 default:
932 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200933 }
934
935 http_end:
936 /* The situation will not evolve, so let's give up on the analysis. */
937 s->logs.tv_request = now; /* update the request timer to reflect full request */
938 req->analysers &= ~an_bit;
939 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100940 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200941 return 1;
942
Christopher Faulet021a8e42021-03-29 10:46:38 +0200943 missing_data_or_waiting:
944 channel_dont_connect(req);
945 DBG_TRACE_DEVEL("waiting for more data",
946 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
947 return 0;
948
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200949 return_int_err:
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200950 txn->status = 500;
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200951 if (!(s->flags & SF_ERR_MASK))
952 s->flags |= SF_ERR_INTERNAL;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100953 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100954 if (s->flags & SF_BE_ASSIGNED)
Christopher Fauletbe20cf32020-01-24 11:41:38 +0100955 _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1);
William Lallemand36119de2021-03-08 15:26:48 +0100956 if (sess->listener && sess->listener->counters)
Christopher Fauletb8a53712019-12-16 11:29:38 +0100957 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
Christopher Faulet021a8e42021-03-29 10:46:38 +0200958 goto return_prx_err;
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200959
Christopher Faulete0768eb2018-10-03 16:38:02 +0200960 return_bad_req: /* let's centralize all bad requests */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200961 txn->status = 400;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100962 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
William Lallemand36119de2021-03-08 15:26:48 +0100963 if (sess->listener && sess->listener->counters)
Christopher Fauletb8a53712019-12-16 11:29:38 +0100964 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
965 /* fall through */
966
Christopher Faulet021a8e42021-03-29 10:46:38 +0200967 return_prx_err:
Christopher Fauletb8a53712019-12-16 11:29:38 +0100968 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet021a8e42021-03-29 10:46:38 +0200969 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200970
Christopher Faulet021a8e42021-03-29 10:46:38 +0200971 return_prx_cond:
Christopher Faulete0768eb2018-10-03 16:38:02 +0200972 if (!(s->flags & SF_ERR_MASK))
973 s->flags |= SF_ERR_PRXCOND;
974 if (!(s->flags & SF_FINST_MASK))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100975 s->flags |= (msg->msg_state < HTTP_MSG_DATA ? SF_FINST_R : SF_FINST_D);
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200976
Christopher Faulete0768eb2018-10-03 16:38:02 +0200977 req->analysers &= AN_REQ_FLT_END;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100978 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100979 DBG_TRACE_DEVEL("leaving on error",
980 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200981 return 0;
982}
983
984/* This function is an analyser which forwards request body (including chunk
985 * sizes if any). It is called as soon as we must forward, even if we forward
986 * zero byte. The only situation where it must not be called is when we're in
987 * tunnel mode and we want to forward till the close. It's used both to forward
988 * remaining data and to resync after end of body. It expects the msg_state to
989 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
990 * read more data, or 1 once we can go on with next request or end the stream.
991 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
992 * bytes of pending data + the headers if not already done.
993 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200994int http_request_forward_body(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200995{
996 struct session *sess = s->sess;
997 struct http_txn *txn = s->txn;
Christopher Faulet9768c262018-10-22 09:34:31 +0200998 struct http_msg *msg = &txn->req;
999 struct htx *htx;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001000 short status = 0;
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001001 int ret;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001002
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001003 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001004
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001005 htx = htxbuf(&req->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001006
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001007 if (htx->flags & HTX_FL_PARSING_ERROR)
1008 goto return_bad_req;
1009 if (htx->flags & HTX_FL_PROCESSING_ERROR)
1010 goto return_int_err;
1011
Christopher Faulete0768eb2018-10-03 16:38:02 +02001012 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
1013 ((req->flags & CF_SHUTW) && (req->to_forward || co_data(req)))) {
1014 /* Output closed while we were sending data. We must abort and
1015 * wake the other side up.
1016 */
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001017
Olivier Houchard29cac3c2019-07-12 15:48:58 +02001018 /* Don't abort yet if we had L7 retries activated and it
1019 * was a write error, we may recover.
1020 */
1021 if (!(req->flags & (CF_READ_ERROR | CF_READ_TIMEOUT)) &&
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001022 (s->si[1].flags & SI_FL_L7_RETRY)) {
1023 DBG_TRACE_DEVEL("leaving on L7 retry",
1024 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Olivier Houchard29cac3c2019-07-12 15:48:58 +02001025 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001026 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001027 msg->msg_state = HTTP_MSG_ERROR;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001028 http_end_request(s);
1029 http_end_response(s);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001030 DBG_TRACE_DEVEL("leaving on error",
1031 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001032 return 1;
1033 }
1034
1035 /* Note that we don't have to send 100-continue back because we don't
1036 * need the data to complete our job, and it's up to the server to
1037 * decide whether to return 100, 417 or anything else in return of
1038 * an "Expect: 100-continue" header.
1039 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001040 if (msg->msg_state == HTTP_MSG_BODY)
1041 msg->msg_state = HTTP_MSG_DATA;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001042
Christopher Faulete0768eb2018-10-03 16:38:02 +02001043 /* in most states, we should abort in case of early close */
1044 channel_auto_close(req);
1045
1046 if (req->to_forward) {
Christopher Faulet66af0b22019-03-22 14:54:52 +01001047 if (req->to_forward == CHN_INFINITE_FORWARD) {
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001048 if (req->flags & CF_EOI)
1049 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet66af0b22019-03-22 14:54:52 +01001050 }
1051 else {
1052 /* We can't process the buffer's contents yet */
1053 req->flags |= CF_WAKE_WRITE;
1054 goto missing_data_or_waiting;
1055 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001056 }
1057
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001058 if (msg->msg_state >= HTTP_MSG_ENDING)
1059 goto ending;
1060
1061 if (txn->meth == HTTP_METH_CONNECT) {
1062 msg->msg_state = HTTP_MSG_ENDING;
1063 goto ending;
1064 }
1065
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001066 /* Forward input data. We get it by removing all outgoing data not
1067 * forwarded yet from HTX data size. If there are some data filters, we
1068 * let them decide the amount of data to forward.
Christopher Faulet9768c262018-10-22 09:34:31 +02001069 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001070 if (HAS_REQ_DATA_FILTERS(s)) {
1071 ret = flt_http_payload(s, msg, htx->data);
1072 if (ret < 0)
1073 goto return_bad_req;
Christopher Faulet421e7692019-06-13 11:16:45 +02001074 c_adv(req, ret);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001075 }
1076 else {
Christopher Faulet421e7692019-06-13 11:16:45 +02001077 c_adv(req, htx->data - co_data(req));
Christopher Faulet66af0b22019-03-22 14:54:52 +01001078 if (msg->flags & HTTP_MSGF_XFER_LEN)
1079 channel_htx_forward_forever(req, htx);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001080 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001081
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001082 if (htx->data != co_data(req))
1083 goto missing_data_or_waiting;
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001084
Christopher Faulet9768c262018-10-22 09:34:31 +02001085 /* Check if the end-of-message is reached and if so, switch the message
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001086 * in HTTP_MSG_ENDING state. Then if all data was marked to be
1087 * forwarded, set the state to HTTP_MSG_DONE.
Christopher Faulet9768c262018-10-22 09:34:31 +02001088 */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001089 if (!(htx->flags & HTX_FL_EOM))
Christopher Faulet9768c262018-10-22 09:34:31 +02001090 goto missing_data_or_waiting;
1091
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001092 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet9768c262018-10-22 09:34:31 +02001093
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001094 ending:
Christopher Faulet2151cdd2020-07-22 16:34:59 +02001095 req->flags &= ~CF_EXPECT_MORE; /* no more data are expected */
1096
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001097 /* other states, ENDING...TUNNEL */
1098 if (msg->msg_state >= HTTP_MSG_DONE)
1099 goto done;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001100
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001101 if (HAS_REQ_DATA_FILTERS(s)) {
1102 ret = flt_http_end(s, msg);
1103 if (ret <= 0) {
1104 if (!ret)
1105 goto missing_data_or_waiting;
1106 goto return_bad_req;
1107 }
1108 }
1109
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001110 if (txn->meth == HTTP_METH_CONNECT)
1111 msg->msg_state = HTTP_MSG_TUNNEL;
1112 else {
1113 msg->msg_state = HTTP_MSG_DONE;
1114 req->to_forward = 0;
Christopher Faulet5b82cc52020-10-12 15:18:50 +02001115
1116 if ((s->be->retry_type &~ PR_RE_CONN_FAILED) && !(s->si[1].flags & SI_FL_D_L7_RETRY)) {
1117 struct stream_interface *si = &s->si[1];
1118
1119 /* If we want to be able to do L7 retries, copy the
1120 * request, so that we are able to resend them if
1121 * needed.
1122 *
1123 * Try to allocate a buffer if we had none. If it
1124 * fails, the next test will just disable the l7
1125 * retries.
1126 */
1127 DBG_TRACE_STATE("enable L7 retry, save the request", STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
1128 si->flags |= SI_FL_L7_RETRY;
Willy Tarreauf499f502021-03-22 16:17:37 +01001129 if (b_alloc(&si->l7_buffer) == NULL)
Christopher Faulet5b82cc52020-10-12 15:18:50 +02001130 si->flags &= ~SI_FL_L7_RETRY;
1131 else {
1132 memcpy(b_orig(&si->l7_buffer), b_orig(&req->buf), b_size(&req->buf));
1133 b_add(&si->l7_buffer, co_data(req));
1134 }
1135 }
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001136 }
1137
1138 done:
1139 /* we don't want to forward closes on DONE except in tunnel mode. */
1140 if (!(txn->flags & TX_CON_WANT_TUN))
1141 channel_dont_close(req);
1142
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001143 http_end_request(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001144 if (!(req->analysers & an_bit)) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001145 http_end_response(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001146 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
1147 if (req->flags & CF_SHUTW) {
1148 /* request errors are most likely due to the
1149 * server aborting the transfer. */
Christopher Faulet93e02d82019-03-08 14:18:50 +01001150 goto return_srv_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001151 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001152 goto return_bad_req;
1153 }
Christopher Faulet5b82cc52020-10-12 15:18:50 +02001154
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001155 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001156 return 1;
1157 }
1158
1159 /* If "option abortonclose" is set on the backend, we want to monitor
1160 * the client's connection and forward any shutdown notification to the
1161 * server, which will decide whether to close or to go on processing the
1162 * request. We only do that in tunnel mode, and not in other modes since
1163 * it can be abused to exhaust source ports. */
Christopher Faulet769d0e92019-03-22 14:23:18 +01001164 if (s->be->options & PR_O_ABRT_CLOSE) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001165 channel_auto_read(req);
Christopher Fauletc41547b2019-07-16 14:32:23 +02001166 if ((req->flags & (CF_SHUTR|CF_READ_NULL)) && !(txn->flags & TX_CON_WANT_TUN))
Christopher Faulete0768eb2018-10-03 16:38:02 +02001167 s->si[1].flags |= SI_FL_NOLINGER;
1168 channel_auto_close(req);
1169 }
1170 else if (s->txn->meth == HTTP_METH_POST) {
1171 /* POST requests may require to read extra CRLF sent by broken
1172 * browsers and which could cause an RST to be sent upon close
1173 * on some systems (eg: Linux). */
1174 channel_auto_read(req);
1175 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001176 DBG_TRACE_DEVEL("waiting for the end of the HTTP txn",
1177 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001178 return 0;
1179
1180 missing_data_or_waiting:
1181 /* stop waiting for data if the input is closed before the end */
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001182 if (msg->msg_state < HTTP_MSG_ENDING && req->flags & CF_SHUTR)
Christopher Faulet93e02d82019-03-08 14:18:50 +01001183 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001184
1185 waiting:
1186 /* waiting for the last bits to leave the buffer */
1187 if (req->flags & CF_SHUTW)
Christopher Faulet93e02d82019-03-08 14:18:50 +01001188 goto return_srv_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001189
1190 /* When TE: chunked is used, we need to get there again to parse remaining
1191 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
1192 * And when content-length is used, we never want to let the possible
1193 * shutdown be forwarded to the other side, as the state machine will
1194 * take care of it once the client responds. It's also important to
1195 * prevent TIME_WAITs from accumulating on the backend side, and for
1196 * HTTP/2 where the last frame comes with a shutdown.
1197 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001198 if (msg->flags & HTTP_MSGF_XFER_LEN)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001199 channel_dont_close(req);
1200
1201 /* We know that more data are expected, but we couldn't send more that
1202 * what we did. So we always set the CF_EXPECT_MORE flag so that the
1203 * system knows it must not set a PUSH on this first part. Interactive
1204 * modes are already handled by the stream sock layer. We must not do
1205 * this in content-length mode because it could present the MSG_MORE
1206 * flag with the last block of forwarded data, which would cause an
1207 * additional delay to be observed by the receiver.
1208 */
Christopher Faulet2151cdd2020-07-22 16:34:59 +02001209 if (HAS_REQ_DATA_FILTERS(s))
Christopher Faulete0768eb2018-10-03 16:38:02 +02001210 req->flags |= CF_EXPECT_MORE;
1211
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001212 DBG_TRACE_DEVEL("waiting for more data to forward",
1213 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001214 return 0;
1215
Christopher Faulet93e02d82019-03-08 14:18:50 +01001216 return_cli_abort:
1217 _HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
1218 _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
William Lallemand36119de2021-03-08 15:26:48 +01001219 if (sess->listener && sess->listener->counters)
Christopher Fauletcff0f732019-12-16 16:13:44 +01001220 _HA_ATOMIC_ADD(&sess->listener->counters->cli_aborts, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001221 if (objt_server(s->target))
Christopher Fauletcff0f732019-12-16 16:13:44 +01001222 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.cli_aborts, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001223 if (!(s->flags & SF_ERR_MASK))
1224 s->flags |= SF_ERR_CLICL;
1225 status = 400;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001226 goto return_prx_cond;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001227
1228 return_srv_abort:
1229 _HA_ATOMIC_ADD(&sess->fe->fe_counters.srv_aborts, 1);
1230 _HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1);
William Lallemand36119de2021-03-08 15:26:48 +01001231 if (sess->listener && sess->listener->counters)
Christopher Fauletcff0f732019-12-16 16:13:44 +01001232 _HA_ATOMIC_ADD(&sess->listener->counters->srv_aborts, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001233 if (objt_server(s->target))
Christopher Fauletcff0f732019-12-16 16:13:44 +01001234 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.srv_aborts, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001235 if (!(s->flags & SF_ERR_MASK))
1236 s->flags |= SF_ERR_SRVCL;
1237 status = 502;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001238 goto return_prx_cond;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001239
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001240 return_int_err:
1241 if (!(s->flags & SF_ERR_MASK))
1242 s->flags |= SF_ERR_INTERNAL;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001243 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001244 _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1);
William Lallemand36119de2021-03-08 15:26:48 +01001245 if (sess->listener && sess->listener->counters)
Christopher Fauletb8a53712019-12-16 11:29:38 +01001246 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001247 if (objt_server(s->target))
1248 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.internal_errors, 1);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001249 status = 500;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001250 goto return_prx_cond;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001251
Christopher Faulet93e02d82019-03-08 14:18:50 +01001252 return_bad_req:
Olivier Houcharda798bf52019-03-08 18:52:00 +01001253 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
William Lallemand36119de2021-03-08 15:26:48 +01001254 if (sess->listener && sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +01001255 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001256 status = 400;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001257 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001258
Christopher Fauletb8a53712019-12-16 11:29:38 +01001259 return_prx_cond:
Christopher Faulet9768c262018-10-22 09:34:31 +02001260 if (txn->status > 0) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001261 /* Note: we don't send any error if some data were already sent */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001262 http_reply_and_close(s, txn->status, NULL);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001263 } else {
Christopher Faulet93e02d82019-03-08 14:18:50 +01001264 txn->status = status;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001265 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001266 }
1267 req->analysers &= AN_REQ_FLT_END;
1268 s->res.analysers &= AN_RES_FLT_END; /* we're in data phase, we want to abort both directions */
Christopher Fauletb8a53712019-12-16 11:29:38 +01001269 if (!(s->flags & SF_ERR_MASK))
1270 s->flags |= SF_ERR_PRXCOND;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001271 if (!(s->flags & SF_FINST_MASK))
1272 s->flags |= ((txn->rsp.msg_state < HTTP_MSG_ERROR) ? SF_FINST_H : SF_FINST_D);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001273 DBG_TRACE_DEVEL("leaving on error ",
1274 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001275 return 0;
1276}
1277
Olivier Houcharda254a372019-04-05 15:30:12 +02001278/* Reset the stream and the backend stream_interface to a situation suitable for attemption connection */
1279/* Returns 0 if we can attempt to retry, -1 otherwise */
1280static __inline int do_l7_retry(struct stream *s, struct stream_interface *si)
1281{
Christopher Faulet5b82cc52020-10-12 15:18:50 +02001282 struct channel *req = &s->req;
1283 struct channel *res = &s->res;
Olivier Houcharda254a372019-04-05 15:30:12 +02001284
1285 si->conn_retries--;
1286 if (si->conn_retries < 0)
Christopher Faulet5b82cc52020-10-12 15:18:50 +02001287 goto no_retry;
1288
1289 if (b_is_null(&req->buf) && !channel_alloc_buffer(req, &s->buffer_wait))
1290 goto no_retry;
Olivier Houcharda254a372019-04-05 15:30:12 +02001291
Willy Tarreau223995e2019-05-04 10:38:31 +02001292 if (objt_server(s->target))
1293 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.retries, 1);
1294 _HA_ATOMIC_ADD(&s->be->be_counters.retries, 1);
1295
Olivier Houcharda254a372019-04-05 15:30:12 +02001296 /* Remove any write error from the request, and read error from the response */
1297 req->flags &= ~(CF_WRITE_ERROR | CF_WRITE_TIMEOUT | CF_SHUTW | CF_SHUTW_NOW);
1298 res->flags &= ~(CF_READ_ERROR | CF_READ_TIMEOUT | CF_SHUTR | CF_EOI | CF_READ_NULL | CF_SHUTR_NOW);
1299 res->analysers = 0;
1300 si->flags &= ~(SI_FL_ERR | SI_FL_EXP | SI_FL_RXBLK_SHUT);
Olivier Houchard8cabc972020-05-12 22:18:14 +02001301 s->flags &= ~SF_ADDR_SET;
Olivier Houchard4bd58672019-07-12 16:16:59 +02001302 stream_choose_redispatch(s);
Olivier Houcharda254a372019-04-05 15:30:12 +02001303 si->exp = TICK_ETERNITY;
1304 res->rex = TICK_ETERNITY;
1305 res->to_forward = 0;
1306 res->analyse_exp = TICK_ETERNITY;
1307 res->total = 0;
Olivier Houchard4bd58672019-07-12 16:16:59 +02001308 s->flags &= ~(SF_ERR_SRVTO | SF_ERR_SRVCL);
Olivier Houcharda254a372019-04-05 15:30:12 +02001309 si_release_endpoint(&s->si[1]);
Olivier Houcharda254a372019-04-05 15:30:12 +02001310
Christopher Faulet5b82cc52020-10-12 15:18:50 +02001311 b_reset(&req->buf);
1312 memcpy(b_orig(&req->buf), b_orig(&si->l7_buffer), b_size(&si->l7_buffer));
1313 b_set_data(&req->buf, b_size(&req->buf));
1314 co_set_data(req, b_data(&si->l7_buffer));
1315
Ilya Shipitsinacf84592021-02-06 22:29:08 +05001316 DBG_TRACE_DEVEL("perform a L7 retry", STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, s->txn);
Olivier Houcharda254a372019-04-05 15:30:12 +02001317 b_reset(&res->buf);
1318 co_set_data(res, 0);
1319 return 0;
Christopher Faulet5b82cc52020-10-12 15:18:50 +02001320
1321 no_retry:
1322 b_free(&si->l7_buffer);
1323 return -1;
Olivier Houcharda254a372019-04-05 15:30:12 +02001324}
1325
Christopher Faulete0768eb2018-10-03 16:38:02 +02001326/* This stream analyser waits for a complete HTTP response. It returns 1 if the
1327 * processing can continue on next analysers, or zero if it either needs more
1328 * data or wants to immediately abort the response (eg: timeout, error, ...). It
1329 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->res.analysers
1330 * when it has nothing left to do, and may remove any analyser when it wants to
1331 * abort.
1332 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001333int http_wait_for_response(struct stream *s, struct channel *rep, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001334{
Christopher Faulet9768c262018-10-22 09:34:31 +02001335 /*
1336 * We will analyze a complete HTTP response to check the its syntax.
1337 *
1338 * Once the start line and all headers are received, we may perform a
1339 * capture of the error (if any), and we will set a few fields. We also
1340 * logging and finally headers capture.
1341 */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001342 struct session *sess = s->sess;
1343 struct http_txn *txn = s->txn;
1344 struct http_msg *msg = &txn->rsp;
Christopher Faulet9768c262018-10-22 09:34:31 +02001345 struct htx *htx;
Olivier Houcharda254a372019-04-05 15:30:12 +02001346 struct stream_interface *si_b = &s->si[1];
Christopher Faulet61608322018-11-23 16:23:45 +01001347 struct connection *srv_conn;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001348 struct htx_sl *sl;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001349 int n;
1350
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001351 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001352
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001353 htx = htxbuf(&rep->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001354
Willy Tarreau4236f032019-03-05 10:43:32 +01001355 /* Parsing errors are caught here */
1356 if (htx->flags & HTX_FL_PARSING_ERROR)
1357 goto return_bad_res;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001358 if (htx->flags & HTX_FL_PROCESSING_ERROR)
1359 goto return_int_err;
Willy Tarreau4236f032019-03-05 10:43:32 +01001360
Christopher Faulete0768eb2018-10-03 16:38:02 +02001361 /*
1362 * Now we quickly check if we have found a full valid response.
1363 * If not so, we check the FD and buffer states before leaving.
1364 * A full response is indicated by the fact that we have seen
1365 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
1366 * responses are checked first.
1367 *
1368 * Depending on whether the client is still there or not, we
1369 * may send an error response back or not. Note that normally
1370 * we should only check for HTTP status there, and check I/O
1371 * errors somewhere else.
1372 */
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001373 next_one:
Christopher Faulet29f17582019-05-23 11:03:26 +02001374 if (unlikely(htx_is_empty(htx) || htx->first == -1)) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001375 /* 1: have we encountered a read error ? */
1376 if (rep->flags & CF_READ_ERROR) {
Olivier Houchard865d8392019-05-03 22:46:27 +02001377 struct connection *conn = NULL;
1378
Olivier Houchard865d8392019-05-03 22:46:27 +02001379 if (objt_cs(s->si[1].end))
1380 conn = objt_cs(s->si[1].end)->conn;
1381
1382 if (si_b->flags & SI_FL_L7_RETRY &&
1383 (!conn || conn->err_code != CO_ER_SSL_EARLY_FAILED)) {
Olivier Houcharda254a372019-04-05 15:30:12 +02001384 /* If we arrive here, then CF_READ_ERROR was
1385 * set by si_cs_recv() because we matched a
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05001386 * status, otherwise it would have removed
Olivier Houcharda254a372019-04-05 15:30:12 +02001387 * the SI_FL_L7_RETRY flag, so it's ok not
1388 * to check s->be->retry_type.
1389 */
Christopher Faulet5b82cc52020-10-12 15:18:50 +02001390 if (co_data(rep) || do_l7_retry(s, si_b) == 0) {
1391 DBG_TRACE_DEVEL("leaving on L7 retry",
1392 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharda254a372019-04-05 15:30:12 +02001393 return 0;
Christopher Faulet5b82cc52020-10-12 15:18:50 +02001394 }
Olivier Houcharda254a372019-04-05 15:30:12 +02001395 }
1396
Olivier Houchard6db16992019-05-17 15:40:49 +02001397 if (txn->flags & TX_NOT_FIRST)
1398 goto abort_keep_alive;
1399
Olivier Houcharda798bf52019-03-08 18:52:00 +01001400 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001401 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001402 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001403 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001404 }
1405
Christopher Faulete0768eb2018-10-03 16:38:02 +02001406 rep->analysers &= AN_RES_FLT_END;
1407 txn->status = 502;
1408
1409 /* Check to see if the server refused the early data.
1410 * If so, just send a 425
1411 */
Willy Tarreauee99aaf2020-06-23 05:58:20 +02001412 if (conn && conn->err_code == CO_ER_SSL_EARLY_FAILED) {
Olivier Houchard865d8392019-05-03 22:46:27 +02001413 if ((s->be->retry_type & PR_RE_EARLY_ERROR) &&
Olivier Houchardad26d8d2019-05-10 17:48:28 +02001414 (si_b->flags & SI_FL_L7_RETRY) &&
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001415 do_l7_retry(s, si_b) == 0) {
1416 DBG_TRACE_DEVEL("leaving on L7 retry",
1417 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houchard865d8392019-05-03 22:46:27 +02001418 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001419 }
Olivier Houchard865d8392019-05-03 22:46:27 +02001420 txn->status = 425;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001421 }
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001422 else
1423 stream_inc_http_fail_ctr(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001424
1425 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001426 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001427
1428 if (!(s->flags & SF_ERR_MASK))
1429 s->flags |= SF_ERR_SRVCL;
1430 if (!(s->flags & SF_FINST_MASK))
1431 s->flags |= SF_FINST_H;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001432 DBG_TRACE_DEVEL("leaving on error",
1433 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001434 return 0;
1435 }
1436
Christopher Faulet9768c262018-10-22 09:34:31 +02001437 /* 2: read timeout : return a 504 to the client. */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001438 else if (rep->flags & CF_READ_TIMEOUT) {
Olivier Houcharda254a372019-04-05 15:30:12 +02001439 if ((si_b->flags & SI_FL_L7_RETRY) &&
1440 (s->be->retry_type & PR_RE_TIMEOUT)) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001441 if (co_data(rep) || do_l7_retry(s, si_b) == 0) {
1442 DBG_TRACE_DEVEL("leaving on L7 retry",
1443 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharda254a372019-04-05 15:30:12 +02001444 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001445 }
Olivier Houcharda254a372019-04-05 15:30:12 +02001446 }
Olivier Houcharda798bf52019-03-08 18:52:00 +01001447 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001448 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001449 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001450 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001451 }
1452
Christopher Faulete0768eb2018-10-03 16:38:02 +02001453 rep->analysers &= AN_RES_FLT_END;
1454 txn->status = 504;
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001455 stream_inc_http_fail_ctr(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001456 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001457 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001458
1459 if (!(s->flags & SF_ERR_MASK))
1460 s->flags |= SF_ERR_SRVTO;
1461 if (!(s->flags & SF_FINST_MASK))
1462 s->flags |= SF_FINST_H;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001463 DBG_TRACE_DEVEL("leaving on error",
1464 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001465 return 0;
1466 }
1467
Christopher Faulet9768c262018-10-22 09:34:31 +02001468 /* 3: client abort with an abortonclose */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001469 else if ((rep->flags & CF_SHUTR) && ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001470 _HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
1471 _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
William Lallemand36119de2021-03-08 15:26:48 +01001472 if (sess->listener && sess->listener->counters)
Christopher Fauletcff0f732019-12-16 16:13:44 +01001473 _HA_ATOMIC_ADD(&sess->listener->counters->cli_aborts, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001474 if (objt_server(s->target))
Olivier Houcharda798bf52019-03-08 18:52:00 +01001475 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.cli_aborts, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001476
1477 rep->analysers &= AN_RES_FLT_END;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001478 txn->status = 400;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001479 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001480
1481 if (!(s->flags & SF_ERR_MASK))
1482 s->flags |= SF_ERR_CLICL;
1483 if (!(s->flags & SF_FINST_MASK))
1484 s->flags |= SF_FINST_H;
1485
1486 /* process_stream() will take care of the error */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001487 DBG_TRACE_DEVEL("leaving on error",
1488 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001489 return 0;
1490 }
1491
Christopher Faulet9768c262018-10-22 09:34:31 +02001492 /* 4: close from server, capture the response if the server has started to respond */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001493 else if (rep->flags & CF_SHUTR) {
Olivier Houcharda254a372019-04-05 15:30:12 +02001494 if ((si_b->flags & SI_FL_L7_RETRY) &&
1495 (s->be->retry_type & PR_RE_DISCONNECTED)) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001496 if (co_data(rep) || do_l7_retry(s, si_b) == 0) {
1497 DBG_TRACE_DEVEL("leaving on L7 retry",
1498 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharda254a372019-04-05 15:30:12 +02001499 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001500 }
Olivier Houcharda254a372019-04-05 15:30:12 +02001501 }
1502
Olivier Houchard6db16992019-05-17 15:40:49 +02001503 if (txn->flags & TX_NOT_FIRST)
1504 goto abort_keep_alive;
1505
Olivier Houcharda798bf52019-03-08 18:52:00 +01001506 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001507 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001508 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001509 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001510 }
1511
Christopher Faulete0768eb2018-10-03 16:38:02 +02001512 rep->analysers &= AN_RES_FLT_END;
1513 txn->status = 502;
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001514 stream_inc_http_fail_ctr(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001515 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001516 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001517
1518 if (!(s->flags & SF_ERR_MASK))
1519 s->flags |= SF_ERR_SRVCL;
1520 if (!(s->flags & SF_FINST_MASK))
1521 s->flags |= SF_FINST_H;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001522 DBG_TRACE_DEVEL("leaving on error",
1523 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001524 return 0;
1525 }
1526
Christopher Faulet9768c262018-10-22 09:34:31 +02001527 /* 5: write error to client (we don't send any message then) */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001528 else if (rep->flags & CF_WRITE_ERROR) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001529 if (txn->flags & TX_NOT_FIRST)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001530 goto abort_keep_alive;
1531
Olivier Houcharda798bf52019-03-08 18:52:00 +01001532 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001533 if (objt_server(s->target))
1534 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001535 rep->analysers &= AN_RES_FLT_END;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001536
1537 if (!(s->flags & SF_ERR_MASK))
1538 s->flags |= SF_ERR_CLICL;
1539 if (!(s->flags & SF_FINST_MASK))
1540 s->flags |= SF_FINST_H;
1541
1542 /* process_stream() will take care of the error */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001543 DBG_TRACE_DEVEL("leaving on error",
1544 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001545 return 0;
1546 }
1547
1548 channel_dont_close(rep);
1549 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001550 DBG_TRACE_DEVEL("waiting for more data",
1551 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001552 return 0;
1553 }
1554
1555 /* More interesting part now : we know that we have a complete
1556 * response which at least looks like HTTP. We have an indicator
1557 * of each header's length, so we can parse them quickly.
1558 */
Christopher Faulet29f17582019-05-23 11:03:26 +02001559 BUG_ON(htx_get_first_type(htx) != HTX_BLK_RES_SL);
Christopher Faulet297fbb42019-05-13 14:41:27 +02001560 sl = http_get_stline(htx);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001561
Christopher Faulet5b82cc52020-10-12 15:18:50 +02001562 if ((si_b->flags & SI_FL_L7_RETRY) &&
1563 l7_status_match(s->be, sl->info.res.status) &&
1564 do_l7_retry(s, si_b) == 0) {
1565 DBG_TRACE_DEVEL("leaving on L7 retry", STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
1566 return 0;
1567 }
1568 b_free(&s->si[1].l7_buffer);
1569
1570 msg->msg_state = HTTP_MSG_BODY;
1571
Christopher Faulet9768c262018-10-22 09:34:31 +02001572 /* 0: we might have to print this header in debug mode */
1573 if (unlikely((global.mode & MODE_DEBUG) &&
1574 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
1575 int32_t pos;
1576
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001577 http_debug_stline("srvrep", s, sl);
Christopher Faulet9768c262018-10-22 09:34:31 +02001578
Christopher Fauleta3f15502019-05-13 15:27:23 +02001579 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001580 struct htx_blk *blk = htx_get_blk(htx, pos);
1581 enum htx_blk_type type = htx_get_blk_type(blk);
1582
1583 if (type == HTX_BLK_EOH)
1584 break;
1585 if (type != HTX_BLK_HDR)
1586 continue;
1587
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001588 http_debug_hdr("srvhdr", s,
1589 htx_get_blk_name(htx, blk),
1590 htx_get_blk_value(htx, blk));
Christopher Faulet9768c262018-10-22 09:34:31 +02001591 }
1592 }
1593
Christopher Faulet03599112018-11-27 11:21:21 +01001594 /* 1: get the status code and the version. Also set HTTP flags */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001595 txn->status = sl->info.res.status;
Christopher Faulet03599112018-11-27 11:21:21 +01001596 if (sl->flags & HTX_SL_F_VER_11)
Christopher Faulet9768c262018-10-22 09:34:31 +02001597 msg->flags |= HTTP_MSGF_VER_11;
Christopher Faulet03599112018-11-27 11:21:21 +01001598 if (sl->flags & HTX_SL_F_XFER_LEN) {
1599 msg->flags |= HTTP_MSGF_XFER_LEN;
Christopher Faulet2a408542020-11-20 14:22:37 +01001600 if (sl->flags & HTX_SL_F_CLEN)
1601 msg->flags |= HTTP_MSGF_CNT_LEN;
1602 else if (sl->flags & HTX_SL_F_CHNK)
1603 msg->flags |= HTTP_MSGF_TE_CHNK;
Christopher Faulet03599112018-11-27 11:21:21 +01001604 }
Christopher Faulet2a408542020-11-20 14:22:37 +01001605 if (sl->flags & HTX_SL_F_BODYLESS)
1606 msg->flags |= HTTP_MSGF_BODYLESS;
Christopher Faulet576c3582021-01-08 15:53:01 +01001607 if (sl->flags & HTX_SL_F_CONN_UPG)
1608 msg->flags |= HTTP_MSGF_CONN_UPG;
Christopher Faulet9768c262018-10-22 09:34:31 +02001609
1610 n = txn->status / 100;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001611 if (n < 1 || n > 5)
1612 n = 0;
Christopher Faulet9768c262018-10-22 09:34:31 +02001613
Christopher Faulete0768eb2018-10-03 16:38:02 +02001614 /* when the client triggers a 4xx from the server, it's most often due
1615 * to a missing object or permission. These events should be tracked
1616 * because if they happen often, it may indicate a brute force or a
1617 * vulnerability scan.
1618 */
1619 if (n == 4)
1620 stream_inc_http_err_ctr(s);
1621
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001622 if (n == 5 && txn->status != 501 && txn->status != 505)
1623 stream_inc_http_fail_ctr(s);
1624
Marcin Deranek3c27dda2020-05-15 18:32:51 +02001625 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001626 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.p.http.rsp[n], 1);
Marcin Deranek3c27dda2020-05-15 18:32:51 +02001627 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.p.http.cum_req, 1);
1628 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001629
Christopher Faulete0768eb2018-10-03 16:38:02 +02001630 /* Adjust server's health based on status code. Note: status codes 501
1631 * and 505 are triggered on demand by client request, so we must not
1632 * count them as server failures.
1633 */
1634 if (objt_server(s->target)) {
1635 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001636 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_OK);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001637 else
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001638 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_STS);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001639 }
1640
1641 /*
1642 * We may be facing a 100-continue response, or any other informational
1643 * 1xx response which is non-final, in which case this is not the right
1644 * response, and we're waiting for the next one. Let's allow this response
1645 * to go to the client and wait for the next one. There's an exception for
1646 * 101 which is used later in the code to switch protocols.
1647 */
1648 if (txn->status < 200 &&
1649 (txn->status == 100 || txn->status >= 102)) {
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001650 FLT_STRM_CB(s, flt_http_reset(s, msg));
Christopher Faulet421e7692019-06-13 11:16:45 +02001651 htx->first = channel_htx_fwd_headers(rep, htx);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001652 msg->msg_state = HTTP_MSG_RPBEFORE;
Christopher Faulet3499f622019-09-03 15:23:54 +02001653 msg->flags = 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001654 txn->status = 0;
1655 s->logs.t_data = -1; /* was not a response yet */
Christopher Faulet7d518452020-08-31 11:07:07 +02001656 rep->flags |= CF_SEND_DONTWAIT; /* Send ASAP informational messages */
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001657 goto next_one;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001658 }
1659
Christopher Faulet6e6c7b12021-01-08 16:02:05 +01001660 /* A 101-switching-protocols must contains a Connection header with the
1661 * "upgrade" option and the request too. It means both are agree to
1662 * upgrade. It is not so strict because there is no test on the Upgrade
1663 * header content. But it is probably stronger enough for now.
1664 */
1665 if (txn->status == 101 &&
1666 (!(txn->req.flags & HTTP_MSGF_CONN_UPG) || !(txn->rsp.flags & HTTP_MSGF_CONN_UPG)))
1667 goto return_bad_res;
1668
Christopher Faulete0768eb2018-10-03 16:38:02 +02001669 /*
1670 * 2: check for cacheability.
1671 */
1672
1673 switch (txn->status) {
1674 case 200:
1675 case 203:
1676 case 204:
1677 case 206:
1678 case 300:
1679 case 301:
1680 case 404:
1681 case 405:
1682 case 410:
1683 case 414:
1684 case 501:
1685 break;
1686 default:
1687 /* RFC7231#6.1:
1688 * Responses with status codes that are defined as
1689 * cacheable by default (e.g., 200, 203, 204, 206,
1690 * 300, 301, 404, 405, 410, 414, and 501 in this
1691 * specification) can be reused by a cache with
1692 * heuristic expiration unless otherwise indicated
1693 * by the method definition or explicit cache
1694 * controls [RFC7234]; all other status codes are
1695 * not cacheable by default.
1696 */
1697 txn->flags &= ~(TX_CACHEABLE | TX_CACHE_COOK);
1698 break;
1699 }
1700
1701 /*
1702 * 3: we may need to capture headers
1703 */
1704 s->logs.logwait &= ~LW_RESP;
1705 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->res_cap))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001706 http_capture_headers(htx, s->res_cap, sess->fe->rsp_cap);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001707
Christopher Faulet9768c262018-10-22 09:34:31 +02001708 /* Skip parsing if no content length is possible. */
Christopher Fauletc75668e2020-12-07 18:10:32 +01001709 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status >= 200 && txn->status < 300) ||
Christopher Faulete0768eb2018-10-03 16:38:02 +02001710 txn->status == 101)) {
1711 /* Either we've established an explicit tunnel, or we're
1712 * switching the protocol. In both cases, we're very unlikely
1713 * to understand the next protocols. We have to switch to tunnel
1714 * mode, so that we transfer the request and responses then let
1715 * this protocol pass unmodified. When we later implement specific
1716 * parsers for such protocols, we'll want to check the Upgrade
1717 * header which contains information about that protocol for
1718 * responses with status 101 (eg: see RFC2817 about TLS).
1719 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02001720 txn->flags |= TX_CON_WANT_TUN;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001721 }
1722
Christopher Faulet61608322018-11-23 16:23:45 +01001723 /* check for NTML authentication headers in 401 (WWW-Authenticate) and
1724 * 407 (Proxy-Authenticate) responses and set the connection to private
1725 */
1726 srv_conn = cs_conn(objt_cs(s->si[1].end));
1727 if (srv_conn) {
1728 struct ist hdr;
1729 struct http_hdr_ctx ctx;
1730
1731 if (txn->status == 401)
1732 hdr = ist("WWW-Authenticate");
1733 else if (txn->status == 407)
1734 hdr = ist("Proxy-Authenticate");
1735 else
1736 goto end;
1737
1738 ctx.blk = NULL;
1739 while (http_find_header(htx, hdr, &ctx, 0)) {
Willy Tarreauf1dcced2020-05-07 19:27:02 +02001740 /* If www-authenticate contains "Negotiate", "Nego2", or "NTLM",
1741 * possibly followed by blanks and a base64 string, the connection
1742 * is private. Since it's a mess to deal with, we only check for
1743 * values starting with "NTLM" or "Nego". Note that often multiple
1744 * headers are sent by the server there.
1745 */
1746 if ((ctx.value.len >= 4 && strncasecmp(ctx.value.ptr, "Nego", 4) == 0) ||
Willy Tarreau49a1d282020-05-07 19:10:15 +02001747 (ctx.value.len >= 4 && strncasecmp(ctx.value.ptr, "NTLM", 4) == 0)) {
Olivier Houchard250031e2019-05-29 15:01:50 +02001748 sess->flags |= SESS_FL_PREFER_LAST;
Christopher Faulet08016ab2020-07-01 16:10:06 +02001749 conn_set_owner(srv_conn, sess, NULL);
Christopher Faulet21ddc742020-07-01 15:26:14 +02001750 conn_set_private(srv_conn);
Ilya Shipitsin6b79f382020-07-23 00:32:55 +05001751 /* If it fail now, the same will be done in mux->detach() callback */
Christopher Faulet08016ab2020-07-01 16:10:06 +02001752 session_add_conn(srv_conn->owner, srv_conn, srv_conn->target);
Willy Tarreauf1dcced2020-05-07 19:27:02 +02001753 break;
Olivier Houchard250031e2019-05-29 15:01:50 +02001754 }
Christopher Faulet61608322018-11-23 16:23:45 +01001755 }
1756 }
1757
1758 end:
Christopher Faulete0768eb2018-10-03 16:38:02 +02001759 /* we want to have the response time before we start processing it */
1760 s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now);
1761
1762 /* end of job, return OK */
1763 rep->analysers &= ~an_bit;
1764 rep->analyse_exp = TICK_ETERNITY;
1765 channel_auto_close(rep);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001766 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001767 return 1;
1768
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001769 return_int_err:
Christopher Fauletcff0f732019-12-16 16:13:44 +01001770 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01001771 _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1);
William Lallemand36119de2021-03-08 15:26:48 +01001772 if (sess->listener && sess->listener->counters)
Christopher Fauletcff0f732019-12-16 16:13:44 +01001773 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01001774 if (objt_server(s->target))
1775 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.internal_errors, 1);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001776 txn->status = 500;
1777 if (!(s->flags & SF_ERR_MASK))
1778 s->flags |= SF_ERR_INTERNAL;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001779 goto return_prx_cond;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001780
1781 return_bad_res:
Olivier Houcharda798bf52019-03-08 18:52:00 +01001782 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulet47365272018-10-31 17:40:50 +01001783 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001784 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001785 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Christopher Faulet47365272018-10-31 17:40:50 +01001786 }
Olivier Houcharde3249a92019-05-03 23:01:47 +02001787 if ((s->be->retry_type & PR_RE_JUNK_REQUEST) &&
Olivier Houchardad26d8d2019-05-10 17:48:28 +02001788 (si_b->flags & SI_FL_L7_RETRY) &&
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001789 do_l7_retry(s, si_b) == 0) {
1790 DBG_TRACE_DEVEL("leaving on L7 retry",
1791 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharde3249a92019-05-03 23:01:47 +02001792 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001793 }
Christopher Faulet47365272018-10-31 17:40:50 +01001794 txn->status = 502;
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001795 stream_inc_http_fail_ctr(s);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001796 /* fall through */
1797
Christopher Fauletb8a53712019-12-16 11:29:38 +01001798 return_prx_cond:
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001799 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet47365272018-10-31 17:40:50 +01001800
1801 if (!(s->flags & SF_ERR_MASK))
1802 s->flags |= SF_ERR_PRXCOND;
1803 if (!(s->flags & SF_FINST_MASK))
1804 s->flags |= SF_FINST_H;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001805
1806 s->si[1].flags |= SI_FL_NOLINGER;
1807 rep->analysers &= AN_RES_FLT_END;
Christopher Faulete58c0002020-03-02 16:21:01 +01001808 s->req.analysers &= AN_REQ_FLT_END;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001809 rep->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001810 DBG_TRACE_DEVEL("leaving on error",
1811 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulet47365272018-10-31 17:40:50 +01001812 return 0;
1813
Christopher Faulete0768eb2018-10-03 16:38:02 +02001814 abort_keep_alive:
1815 /* A keep-alive request to the server failed on a network error.
1816 * The client is required to retry. We need to close without returning
1817 * any other information so that the client retries.
1818 */
1819 txn->status = 0;
1820 rep->analysers &= AN_RES_FLT_END;
1821 s->req.analysers &= AN_REQ_FLT_END;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001822 s->logs.logwait = 0;
1823 s->logs.level = 0;
1824 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001825 http_reply_and_close(s, txn->status, NULL);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001826 DBG_TRACE_DEVEL("leaving by closing K/A connection",
1827 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001828 return 0;
1829}
1830
1831/* This function performs all the processing enabled for the current response.
1832 * It normally returns 1 unless it wants to break. It relies on buffers flags,
1833 * and updates s->res.analysers. It might make sense to explode it into several
1834 * other functions. It works like process_request (see indications above).
1835 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001836int http_process_res_common(struct stream *s, struct channel *rep, int an_bit, struct proxy *px)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001837{
1838 struct session *sess = s->sess;
1839 struct http_txn *txn = s->txn;
1840 struct http_msg *msg = &txn->rsp;
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001841 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001842 struct proxy *cur_proxy;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001843 enum rule_result ret = HTTP_RULE_RES_CONT;
1844
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001845 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
1846 return 0;
Christopher Faulet9768c262018-10-22 09:34:31 +02001847
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001848 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001849
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001850 htx = htxbuf(&rep->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001851
1852 /* The stats applet needs to adjust the Connection header but we don't
1853 * apply any filter there.
1854 */
1855 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
1856 rep->analysers &= ~an_bit;
1857 rep->analyse_exp = TICK_ETERNITY;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001858 goto end;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001859 }
1860
1861 /*
1862 * We will have to evaluate the filters.
1863 * As opposed to version 1.2, now they will be evaluated in the
1864 * filters order and not in the header order. This means that
1865 * each filter has to be validated among all headers.
1866 *
1867 * Filters are tried with ->be first, then with ->fe if it is
1868 * different from ->be.
1869 *
1870 * Maybe we are in resume condiion. In this case I choose the
1871 * "struct proxy" which contains the rule list matching the resume
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05001872 * pointer. If none of these "struct proxy" match, I initialise
Christopher Faulete0768eb2018-10-03 16:38:02 +02001873 * the process with the first one.
1874 *
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05001875 * In fact, I check only correspondence between the current list
Christopher Faulete0768eb2018-10-03 16:38:02 +02001876 * pointer and the ->fe rule list. If it doesn't match, I initialize
1877 * the loop with the ->be.
1878 */
1879 if (s->current_rule_list == &sess->fe->http_res_rules)
1880 cur_proxy = sess->fe;
1881 else
1882 cur_proxy = s->be;
1883 while (1) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001884 /* evaluate http-response rules */
1885 if (ret == HTTP_RULE_RES_CONT) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001886 ret = http_res_get_intercept_rule(cur_proxy, &cur_proxy->http_res_rules, s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001887
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001888 switch (ret) {
1889 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
1890 goto return_prx_yield;
1891
1892 case HTTP_RULE_RES_CONT:
1893 case HTTP_RULE_RES_STOP: /* nothing to do */
1894 break;
1895
1896 case HTTP_RULE_RES_DENY: /* deny or tarpit */
1897 goto deny;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001898
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001899 case HTTP_RULE_RES_ABRT: /* abort request, response already sent */
1900 goto return_prx_cond;
1901
1902 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
Christopher Fauletb8a53712019-12-16 11:29:38 +01001903 goto done;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001904
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001905 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
1906 goto return_bad_res;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001907
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001908 case HTTP_RULE_RES_ERROR: /* failed with a bad request */
1909 goto return_int_err;
1910 }
1911
1912 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001913
Christopher Faulete0768eb2018-10-03 16:38:02 +02001914 /* check whether we're already working on the frontend */
1915 if (cur_proxy == sess->fe)
1916 break;
1917 cur_proxy = sess->fe;
1918 }
1919
Christopher Faulete0768eb2018-10-03 16:38:02 +02001920 /* OK that's all we can do for 1xx responses */
1921 if (unlikely(txn->status < 200 && txn->status != 101))
Christopher Fauletf2824e62018-10-01 12:12:37 +02001922 goto end;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001923
1924 /*
1925 * Now check for a server cookie.
1926 */
1927 if (s->be->cookie_name || sess->fe->capture_name || (s->be->options & PR_O_CHK_CACHE))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001928 http_manage_server_side_cookies(s, rep);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001929
1930 /*
1931 * Check for cache-control or pragma headers if required.
1932 */
1933 if ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001934 http_check_response_for_cacheability(s, rep);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001935
1936 /*
1937 * Add server cookie in the response if needed
1938 */
1939 if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) &&
1940 !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) &&
1941 (!(s->flags & SF_DIRECT) ||
1942 ((s->be->cookie_maxidle || txn->cookie_last_date) &&
1943 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
1944 (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
1945 (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
1946 (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
1947 !(s->flags & SF_IGNORE_PRST)) {
1948 /* the server is known, it's not the one the client requested, or the
1949 * cookie's last seen date needs to be refreshed. We have to
1950 * insert a set-cookie here, except if we want to insert only on POST
1951 * requests and this one isn't. Note that servers which don't have cookies
1952 * (eg: some backup servers) will return a full cookie removal request.
1953 */
1954 if (!objt_server(s->target)->cookie) {
1955 chunk_printf(&trash,
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001956 "%s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
Christopher Faulete0768eb2018-10-03 16:38:02 +02001957 s->be->cookie_name);
1958 }
1959 else {
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001960 chunk_printf(&trash, "%s=%s", s->be->cookie_name, objt_server(s->target)->cookie);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001961
1962 if (s->be->cookie_maxidle || s->be->cookie_maxlife) {
1963 /* emit last_date, which is mandatory */
1964 trash.area[trash.data++] = COOKIE_DELIM_DATE;
1965 s30tob64((date.tv_sec+3) >> 2,
1966 trash.area + trash.data);
1967 trash.data += 5;
1968
1969 if (s->be->cookie_maxlife) {
1970 /* emit first_date, which is either the original one or
1971 * the current date.
1972 */
1973 trash.area[trash.data++] = COOKIE_DELIM_DATE;
1974 s30tob64(txn->cookie_first_date ?
1975 txn->cookie_first_date >> 2 :
1976 (date.tv_sec+3) >> 2,
1977 trash.area + trash.data);
1978 trash.data += 5;
1979 }
1980 }
1981 chunk_appendf(&trash, "; path=/");
1982 }
1983
1984 if (s->be->cookie_domain)
1985 chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain);
1986
1987 if (s->be->ck_opts & PR_CK_HTTPONLY)
1988 chunk_appendf(&trash, "; HttpOnly");
1989
1990 if (s->be->ck_opts & PR_CK_SECURE)
1991 chunk_appendf(&trash, "; Secure");
1992
Christopher Faulet2f533902020-01-21 11:06:48 +01001993 if (s->be->cookie_attrs)
1994 chunk_appendf(&trash, "; %s", s->be->cookie_attrs);
1995
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001996 if (unlikely(!http_add_header(htx, ist("Set-Cookie"), ist2(trash.area, trash.data))))
Christopher Fauletb8a53712019-12-16 11:29:38 +01001997 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001998
1999 txn->flags &= ~TX_SCK_MASK;
2000 if (__objt_server(s->target)->cookie && (s->flags & SF_DIRECT))
2001 /* the server did not change, only the date was updated */
2002 txn->flags |= TX_SCK_UPDATED;
2003 else
2004 txn->flags |= TX_SCK_INSERTED;
2005
2006 /* Here, we will tell an eventual cache on the client side that we don't
2007 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
2008 * Some caches understand the correct form: 'no-cache="set-cookie"', but
2009 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
2010 */
2011 if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
2012
2013 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
2014
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002015 if (unlikely(!http_add_header(htx, ist("Cache-control"), ist("private"))))
Christopher Fauletb8a53712019-12-16 11:29:38 +01002016 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002017 }
2018 }
2019
2020 /*
2021 * Check if result will be cacheable with a cookie.
2022 * We'll block the response if security checks have caught
2023 * nasty things such as a cacheable cookie.
2024 */
2025 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
2026 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
2027 (s->be->options & PR_O_CHK_CACHE)) {
2028 /* we're in presence of a cacheable response containing
2029 * a set-cookie header. We'll block it as requested by
2030 * the 'checkcache' option, and send an alert.
2031 */
Christopher Faulete0768eb2018-10-03 16:38:02 +02002032 ha_alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
2033 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
2034 send_log(s->be, LOG_ALERT,
2035 "Blocking cacheable cookie in response from instance %s, server %s.\n",
2036 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
Christopher Fauletb8a53712019-12-16 11:29:38 +01002037 goto deny;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002038 }
2039
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002040 end:
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002041 /*
2042 * Evaluate after-response rules before forwarding the response. rules
2043 * from the backend are evaluated first, then one from the frontend if
2044 * it differs.
2045 */
2046 if (!http_eval_after_res_rules(s))
2047 goto return_int_err;
2048
Christopher Fauletc2ac5e42021-03-08 18:20:09 +01002049 /* Filter the response headers if there are filters attached to the
2050 * stream.
2051 */
2052 if (HAS_FILTERS(s))
2053 rep->analysers |= AN_RES_FLT_HTTP_HDRS;
2054
Christopher Faulete0768eb2018-10-03 16:38:02 +02002055 /* Always enter in the body analyzer */
2056 rep->analysers &= ~AN_RES_FLT_XFER_DATA;
2057 rep->analysers |= AN_RES_HTTP_XFER_BODY;
2058
2059 /* if the user wants to log as soon as possible, without counting
2060 * bytes from the server, then this is the right moment. We have
2061 * to temporarily assign bytes_out to log what we currently have.
2062 */
2063 if (!LIST_ISEMPTY(&sess->fe->logformat) && !(s->logs.logwait & LW_BYTES)) {
2064 s->logs.t_close = s->logs.t_data; /* to get a valid end date */
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002065 s->logs.bytes_out = htx->data;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002066 s->do_log(s);
2067 s->logs.bytes_out = 0;
2068 }
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002069
Christopher Fauletb8a53712019-12-16 11:29:38 +01002070 done:
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002071 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002072 rep->analysers &= ~an_bit;
2073 rep->analyse_exp = TICK_ETERNITY;
2074 return 1;
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002075
Christopher Fauletb8a53712019-12-16 11:29:38 +01002076 deny:
Christopher Fauletb8a53712019-12-16 11:29:38 +01002077 _HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_resp, 1);
Christopher Fauleta08546b2019-12-16 16:07:34 +01002078 _HA_ATOMIC_ADD(&s->be->be_counters.denied_resp, 1);
William Lallemand36119de2021-03-08 15:26:48 +01002079 if (sess->listener && sess->listener->counters)
Christopher Fauletb8a53712019-12-16 11:29:38 +01002080 _HA_ATOMIC_ADD(&sess->listener->counters->denied_resp, 1);
Christopher Fauleta08546b2019-12-16 16:07:34 +01002081 if (objt_server(s->target))
2082 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.denied_resp, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002083 goto return_prx_err;
2084
2085 return_int_err:
2086 txn->status = 500;
2087 if (!(s->flags & SF_ERR_MASK))
2088 s->flags |= SF_ERR_INTERNAL;
Christopher Fauletcff0f732019-12-16 16:13:44 +01002089 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002090 _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1);
2091 if (objt_server(s->target))
2092 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01002093 if (objt_server(s->target))
2094 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002095 goto return_prx_err;
2096
2097 return_bad_res:
2098 txn->status = 502;
Willy Tarreau826f3ab2021-02-10 12:07:15 +01002099 stream_inc_http_fail_ctr(s);
Christopher Fauleta20a6532020-02-05 10:16:41 +01002100 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
2101 if (objt_server(s->target)) {
2102 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
2103 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_RSP);
2104 }
Christopher Fauletb8a53712019-12-16 11:29:38 +01002105 /* fall through */
2106
2107 return_prx_err:
2108 http_reply_and_close(s, txn->status, http_error_message(s));
2109 /* fall through */
2110
2111 return_prx_cond:
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002112 s->logs.t_data = -1; /* was not a valid response */
2113 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002114
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002115 if (!(s->flags & SF_ERR_MASK))
2116 s->flags |= SF_ERR_PRXCOND;
2117 if (!(s->flags & SF_FINST_MASK))
2118 s->flags |= SF_FINST_H;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002119
Christopher Faulete58c0002020-03-02 16:21:01 +01002120 rep->analysers &= AN_RES_FLT_END;
2121 s->req.analysers &= AN_REQ_FLT_END;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002122 rep->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002123 DBG_TRACE_DEVEL("leaving on error",
2124 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002125 return 0;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002126
2127 return_prx_yield:
2128 channel_dont_close(rep);
2129 DBG_TRACE_DEVEL("waiting for more data",
2130 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
2131 return 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002132}
2133
2134/* This function is an analyser which forwards response body (including chunk
2135 * sizes if any). It is called as soon as we must forward, even if we forward
2136 * zero byte. The only situation where it must not be called is when we're in
2137 * tunnel mode and we want to forward till the close. It's used both to forward
2138 * remaining data and to resync after end of body. It expects the msg_state to
2139 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
2140 * read more data, or 1 once we can go on with next request or end the stream.
2141 *
2142 * It is capable of compressing response data both in content-length mode and
2143 * in chunked mode. The state machines follows different flows depending on
2144 * whether content-length and chunked modes are used, since there are no
2145 * trailers in content-length :
2146 *
2147 * chk-mode cl-mode
2148 * ,----- BODY -----.
2149 * / \
2150 * V size > 0 V chk-mode
2151 * .--> SIZE -------------> DATA -------------> CRLF
2152 * | | size == 0 | last byte |
2153 * | v final crlf v inspected |
2154 * | TRAILERS -----------> DONE |
2155 * | |
2156 * `----------------------------------------------'
2157 *
2158 * Compression only happens in the DATA state, and must be flushed in final
2159 * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding
2160 * is performed at once on final states for all bytes parsed, or when leaving
2161 * on missing data.
2162 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002163int http_response_forward_body(struct stream *s, struct channel *res, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +02002164{
2165 struct session *sess = s->sess;
2166 struct http_txn *txn = s->txn;
2167 struct http_msg *msg = &s->txn->rsp;
Christopher Faulet9768c262018-10-22 09:34:31 +02002168 struct htx *htx;
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002169 int ret;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002170
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002171 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002172
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002173 htx = htxbuf(&res->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002174
Christopher Fauletb9a92f32019-09-09 10:15:21 +02002175 if (htx->flags & HTX_FL_PARSING_ERROR)
2176 goto return_bad_res;
2177 if (htx->flags & HTX_FL_PROCESSING_ERROR)
2178 goto return_int_err;
2179
Christopher Faulete0768eb2018-10-03 16:38:02 +02002180 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Christopher Fauletf2824e62018-10-01 12:12:37 +02002181 ((res->flags & CF_SHUTW) && (res->to_forward || co_data(res)))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02002182 /* Output closed while we were sending data. We must abort and
2183 * wake the other side up.
2184 */
Christopher Faulete0768eb2018-10-03 16:38:02 +02002185 msg->msg_state = HTTP_MSG_ERROR;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002186 http_end_response(s);
2187 http_end_request(s);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002188 DBG_TRACE_DEVEL("leaving on error",
2189 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002190 return 1;
2191 }
2192
Christopher Faulet9768c262018-10-22 09:34:31 +02002193 if (msg->msg_state == HTTP_MSG_BODY)
2194 msg->msg_state = HTTP_MSG_DATA;
2195
Christopher Faulete0768eb2018-10-03 16:38:02 +02002196 /* in most states, we should abort in case of early close */
2197 channel_auto_close(res);
2198
Christopher Faulete0768eb2018-10-03 16:38:02 +02002199 if (res->to_forward) {
Christopher Faulet66af0b22019-03-22 14:54:52 +01002200 if (res->to_forward == CHN_INFINITE_FORWARD) {
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002201 if (res->flags & CF_EOI)
2202 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet66af0b22019-03-22 14:54:52 +01002203 }
2204 else {
2205 /* We can't process the buffer's contents yet */
2206 res->flags |= CF_WAKE_WRITE;
2207 goto missing_data_or_waiting;
2208 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02002209 }
2210
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002211 if (msg->msg_state >= HTTP_MSG_ENDING)
2212 goto ending;
2213
Christopher Fauletc75668e2020-12-07 18:10:32 +01002214 if ((txn->meth == HTTP_METH_CONNECT && txn->status >= 200 && txn->status < 300) || txn->status == 101 ||
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002215 (!(msg->flags & HTTP_MSGF_XFER_LEN) && !HAS_RSP_DATA_FILTERS(s))) {
2216 msg->msg_state = HTTP_MSG_ENDING;
2217 goto ending;
2218 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002219
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002220 /* Forward input data. We get it by removing all outgoing data not
2221 * forwarded yet from HTX data size. If there are some data filters, we
2222 * let them decide the amount of data to forward.
Christopher Faulet9768c262018-10-22 09:34:31 +02002223 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002224 if (HAS_RSP_DATA_FILTERS(s)) {
2225 ret = flt_http_payload(s, msg, htx->data);
2226 if (ret < 0)
2227 goto return_bad_res;
Christopher Faulet421e7692019-06-13 11:16:45 +02002228 c_adv(res, ret);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002229 }
2230 else {
Christopher Faulet421e7692019-06-13 11:16:45 +02002231 c_adv(res, htx->data - co_data(res));
Christopher Faulet66af0b22019-03-22 14:54:52 +01002232 if (msg->flags & HTTP_MSGF_XFER_LEN)
2233 channel_htx_forward_forever(res, htx);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002234 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002235
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002236 if (htx->data != co_data(res))
2237 goto missing_data_or_waiting;
2238
2239 if (!(msg->flags & HTTP_MSGF_XFER_LEN) && res->flags & CF_SHUTR) {
2240 msg->msg_state = HTTP_MSG_ENDING;
2241 goto ending;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002242 }
2243
Christopher Faulet9768c262018-10-22 09:34:31 +02002244 /* Check if the end-of-message is reached and if so, switch the message
Christopher Fauletd20fdb02019-06-13 16:43:22 +02002245 * in HTTP_MSG_ENDING state. Then if all data was marked to be
2246 * forwarded, set the state to HTTP_MSG_DONE.
Christopher Faulet9768c262018-10-22 09:34:31 +02002247 */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002248 if (!(htx->flags & HTX_FL_EOM))
Christopher Faulet9768c262018-10-22 09:34:31 +02002249 goto missing_data_or_waiting;
2250
Christopher Fauletd20fdb02019-06-13 16:43:22 +02002251 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet9768c262018-10-22 09:34:31 +02002252
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002253 ending:
Christopher Faulet2151cdd2020-07-22 16:34:59 +02002254 res->flags &= ~CF_EXPECT_MORE; /* no more data are expected */
2255
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002256 /* other states, ENDING...TUNNEL */
2257 if (msg->msg_state >= HTTP_MSG_DONE)
2258 goto done;
Christopher Faulet9768c262018-10-22 09:34:31 +02002259
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002260 if (HAS_RSP_DATA_FILTERS(s)) {
2261 ret = flt_http_end(s, msg);
2262 if (ret <= 0) {
2263 if (!ret)
2264 goto missing_data_or_waiting;
2265 goto return_bad_res;
2266 }
2267 }
2268
Christopher Fauletc75668e2020-12-07 18:10:32 +01002269 if ((txn->meth == HTTP_METH_CONNECT && txn->status >= 200 && txn->status < 300) || txn->status == 101 ||
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002270 !(msg->flags & HTTP_MSGF_XFER_LEN)) {
2271 msg->msg_state = HTTP_MSG_TUNNEL;
2272 goto ending;
2273 }
2274 else {
2275 msg->msg_state = HTTP_MSG_DONE;
2276 res->to_forward = 0;
2277 }
2278
2279 done:
2280
2281 channel_dont_close(res);
2282
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002283 http_end_response(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002284 if (!(res->analysers & an_bit)) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002285 http_end_request(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002286 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
2287 if (res->flags & CF_SHUTW) {
2288 /* response errors are most likely due to the
2289 * client aborting the transfer. */
Christopher Faulet93e02d82019-03-08 14:18:50 +01002290 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002291 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02002292 goto return_bad_res;
2293 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002294 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002295 return 1;
2296 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002297 DBG_TRACE_DEVEL("waiting for the end of the HTTP txn",
2298 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002299 return 0;
2300
2301 missing_data_or_waiting:
2302 if (res->flags & CF_SHUTW)
Christopher Faulet93e02d82019-03-08 14:18:50 +01002303 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002304
2305 /* stop waiting for data if the input is closed before the end. If the
2306 * client side was already closed, it means that the client has aborted,
2307 * so we don't want to count this as a server abort. Otherwise it's a
2308 * server abort.
2309 */
Christopher Fauletd20fdb02019-06-13 16:43:22 +02002310 if (msg->msg_state < HTTP_MSG_ENDING && res->flags & CF_SHUTR) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02002311 if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))
Christopher Faulet93e02d82019-03-08 14:18:50 +01002312 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002313 /* If we have some pending data, we continue the processing */
Christopher Faulet93e02d82019-03-08 14:18:50 +01002314 if (htx_is_empty(htx))
2315 goto return_srv_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002316 }
2317
Christopher Faulete0768eb2018-10-03 16:38:02 +02002318 /* When TE: chunked is used, we need to get there again to parse
2319 * remaining chunks even if the server has closed, so we don't want to
Christopher Faulet9768c262018-10-22 09:34:31 +02002320 * set CF_DONTCLOSE. Similarly when there is a content-leng or if there
2321 * are filters registered on the stream, we don't want to forward a
2322 * close
Christopher Faulete0768eb2018-10-03 16:38:02 +02002323 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002324 if ((msg->flags & HTTP_MSGF_XFER_LEN) || HAS_RSP_DATA_FILTERS(s))
Christopher Faulete0768eb2018-10-03 16:38:02 +02002325 channel_dont_close(res);
2326
2327 /* We know that more data are expected, but we couldn't send more that
2328 * what we did. So we always set the CF_EXPECT_MORE flag so that the
2329 * system knows it must not set a PUSH on this first part. Interactive
2330 * modes are already handled by the stream sock layer. We must not do
2331 * this in content-length mode because it could present the MSG_MORE
2332 * flag with the last block of forwarded data, which would cause an
2333 * additional delay to be observed by the receiver.
2334 */
Christopher Faulet2151cdd2020-07-22 16:34:59 +02002335 if (HAS_RSP_DATA_FILTERS(s))
Christopher Faulete0768eb2018-10-03 16:38:02 +02002336 res->flags |= CF_EXPECT_MORE;
2337
2338 /* the stream handler will take care of timeouts and errors */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002339 DBG_TRACE_DEVEL("waiting for more data to forward",
2340 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002341 return 0;
2342
Christopher Faulet93e02d82019-03-08 14:18:50 +01002343 return_srv_abort:
2344 _HA_ATOMIC_ADD(&sess->fe->fe_counters.srv_aborts, 1);
2345 _HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1);
William Lallemand36119de2021-03-08 15:26:48 +01002346 if (sess->listener && sess->listener->counters)
Christopher Fauletcff0f732019-12-16 16:13:44 +01002347 _HA_ATOMIC_ADD(&sess->listener->counters->srv_aborts, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002348 if (objt_server(s->target))
Christopher Fauletcff0f732019-12-16 16:13:44 +01002349 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.srv_aborts, 1);
Willy Tarreau826f3ab2021-02-10 12:07:15 +01002350 stream_inc_http_fail_ctr(s);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002351 if (!(s->flags & SF_ERR_MASK))
2352 s->flags |= SF_ERR_SRVCL;
2353 goto return_error;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002354
Christopher Faulet93e02d82019-03-08 14:18:50 +01002355 return_cli_abort:
2356 _HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
2357 _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
William Lallemand36119de2021-03-08 15:26:48 +01002358 if (sess->listener && sess->listener->counters)
Christopher Fauletcff0f732019-12-16 16:13:44 +01002359 _HA_ATOMIC_ADD(&sess->listener->counters->cli_aborts, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002360 if (objt_server(s->target))
Christopher Fauletcff0f732019-12-16 16:13:44 +01002361 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.cli_aborts, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002362 if (!(s->flags & SF_ERR_MASK))
2363 s->flags |= SF_ERR_CLICL;
2364 goto return_error;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002365
Christopher Fauletb9a92f32019-09-09 10:15:21 +02002366 return_int_err:
Christopher Fauletcff0f732019-12-16 16:13:44 +01002367 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002368 _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1);
William Lallemand36119de2021-03-08 15:26:48 +01002369 if (sess->listener && sess->listener->counters)
Christopher Fauletcff0f732019-12-16 16:13:44 +01002370 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002371 if (objt_server(s->target))
2372 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.internal_errors, 1);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02002373 if (!(s->flags & SF_ERR_MASK))
2374 s->flags |= SF_ERR_INTERNAL;
2375 goto return_error;
2376
Christopher Faulet93e02d82019-03-08 14:18:50 +01002377 return_bad_res:
2378 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
2379 if (objt_server(s->target)) {
Christopher Fauletcff0f732019-12-16 16:13:44 +01002380 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002381 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_RSP);
2382 }
Willy Tarreau826f3ab2021-02-10 12:07:15 +01002383 stream_inc_http_fail_ctr(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002384 if (!(s->flags & SF_ERR_MASK))
Christopher Faulet93e02d82019-03-08 14:18:50 +01002385 s->flags |= SF_ERR_SRVCL;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002386 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +02002387
Christopher Faulet93e02d82019-03-08 14:18:50 +01002388 return_error:
Christopher Faulete0768eb2018-10-03 16:38:02 +02002389 /* don't send any error message as we're in the body */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002390 http_reply_and_close(s, txn->status, NULL);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002391 res->analysers &= AN_RES_FLT_END;
2392 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 +02002393 if (!(s->flags & SF_FINST_MASK))
2394 s->flags |= SF_FINST_D;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002395 DBG_TRACE_DEVEL("leaving on error",
2396 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002397 return 0;
2398}
2399
Christopher Fauletf2824e62018-10-01 12:12:37 +02002400/* Perform an HTTP redirect based on the information in <rule>. The function
Christopher Faulet99daf282018-11-28 22:58:13 +01002401 * returns zero on success, or zero in case of a, irrecoverable error such
Christopher Fauletf2824e62018-10-01 12:12:37 +02002402 * as too large a request to build a valid response.
2403 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002404int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn)
Christopher Fauletf2824e62018-10-01 12:12:37 +02002405{
Christopher Faulet99daf282018-11-28 22:58:13 +01002406 struct channel *req = &s->req;
2407 struct channel *res = &s->res;
2408 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01002409 struct htx_sl *sl;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002410 struct buffer *chunk;
Christopher Faulet99daf282018-11-28 22:58:13 +01002411 struct ist status, reason, location;
2412 unsigned int flags;
Christopher Faulet08e66462019-05-23 16:44:59 +02002413 int close = 0; /* Try to keep the connection alive byt default */
Christopher Fauletf2824e62018-10-01 12:12:37 +02002414
2415 chunk = alloc_trash_chunk();
Christopher Fauletb8a53712019-12-16 11:29:38 +01002416 if (!chunk) {
2417 if (!(s->flags & SF_ERR_MASK))
2418 s->flags |= SF_ERR_RESOURCE;
Christopher Faulet99daf282018-11-28 22:58:13 +01002419 goto fail;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002420 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002421
Christopher Faulet99daf282018-11-28 22:58:13 +01002422 /*
2423 * Create the location
2424 */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002425 htx = htxbuf(&req->buf);
Christopher Fauletf2824e62018-10-01 12:12:37 +02002426 switch(rule->type) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002427 case REDIRECT_TYPE_SCHEME: {
2428 struct http_hdr_ctx ctx;
2429 struct ist path, host;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002430
Christopher Faulet99daf282018-11-28 22:58:13 +01002431 host = ist("");
2432 ctx.blk = NULL;
2433 if (http_find_header(htx, ist("Host"), &ctx, 0))
2434 host = ctx.value;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002435
Christopher Faulet297fbb42019-05-13 14:41:27 +02002436 sl = http_get_stline(htx);
Christopher Faulet99daf282018-11-28 22:58:13 +01002437 path = http_get_path(htx_sl_req_uri(sl));
2438 /* build message using path */
Tim Duesterhused526372020-03-05 17:56:33 +01002439 if (isttest(path)) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002440 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2441 int qs = 0;
2442 while (qs < path.len) {
2443 if (*(path.ptr + qs) == '?') {
2444 path.len = qs;
2445 break;
2446 }
2447 qs++;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002448 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002449 }
2450 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002451 else
2452 path = ist("/");
Christopher Fauletf2824e62018-10-01 12:12:37 +02002453
Christopher Faulet99daf282018-11-28 22:58:13 +01002454 if (rule->rdr_str) { /* this is an old "redirect" rule */
2455 /* add scheme */
2456 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2457 goto fail;
2458 }
2459 else {
2460 /* add scheme with executing log format */
2461 chunk->data += build_logline(s, chunk->area + chunk->data,
2462 chunk->size - chunk->data,
2463 &rule->rdr_fmt);
2464 }
2465 /* add "://" + host + path */
2466 if (!chunk_memcat(chunk, "://", 3) ||
2467 !chunk_memcat(chunk, host.ptr, host.len) ||
2468 !chunk_memcat(chunk, path.ptr, path.len))
2469 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002470
Christopher Faulet99daf282018-11-28 22:58:13 +01002471 /* append a slash at the end of the location if needed and missing */
2472 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
2473 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
2474 if (chunk->data + 1 >= chunk->size)
2475 goto fail;
2476 chunk->area[chunk->data++] = '/';
2477 }
2478 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002479 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002480
Christopher Faulet99daf282018-11-28 22:58:13 +01002481 case REDIRECT_TYPE_PREFIX: {
2482 struct ist path;
2483
Christopher Faulet297fbb42019-05-13 14:41:27 +02002484 sl = http_get_stline(htx);
Christopher Faulet99daf282018-11-28 22:58:13 +01002485 path = http_get_path(htx_sl_req_uri(sl));
2486 /* build message using path */
Tim Duesterhused526372020-03-05 17:56:33 +01002487 if (isttest(path)) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002488 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2489 int qs = 0;
2490 while (qs < path.len) {
2491 if (*(path.ptr + qs) == '?') {
2492 path.len = qs;
2493 break;
2494 }
2495 qs++;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002496 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002497 }
2498 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002499 else
2500 path = ist("/");
Christopher Fauletf2824e62018-10-01 12:12:37 +02002501
Christopher Faulet99daf282018-11-28 22:58:13 +01002502 if (rule->rdr_str) { /* this is an old "redirect" rule */
2503 /* add prefix. Note that if prefix == "/", we don't want to
2504 * add anything, otherwise it makes it hard for the user to
2505 * configure a self-redirection.
2506 */
2507 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
2508 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2509 goto fail;
2510 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002511 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002512 else {
2513 /* add prefix with executing log format */
2514 chunk->data += build_logline(s, chunk->area + chunk->data,
2515 chunk->size - chunk->data,
2516 &rule->rdr_fmt);
2517 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002518
Christopher Faulet99daf282018-11-28 22:58:13 +01002519 /* add path */
2520 if (!chunk_memcat(chunk, path.ptr, path.len))
2521 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002522
Christopher Faulet99daf282018-11-28 22:58:13 +01002523 /* append a slash at the end of the location if needed and missing */
2524 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
2525 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
2526 if (chunk->data + 1 >= chunk->size)
2527 goto fail;
2528 chunk->area[chunk->data++] = '/';
2529 }
2530 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002531 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002532 case REDIRECT_TYPE_LOCATION:
2533 default:
2534 if (rule->rdr_str) { /* this is an old "redirect" rule */
2535 /* add location */
2536 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2537 goto fail;
2538 }
2539 else {
2540 /* add location with executing log format */
2541 chunk->data += build_logline(s, chunk->area + chunk->data,
2542 chunk->size - chunk->data,
2543 &rule->rdr_fmt);
2544 }
2545 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002546 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002547 location = ist2(chunk->area, chunk->data);
2548
2549 /*
2550 * Create the 30x response
2551 */
2552 switch (rule->code) {
2553 case 308:
2554 status = ist("308");
2555 reason = ist("Permanent Redirect");
2556 break;
2557 case 307:
2558 status = ist("307");
2559 reason = ist("Temporary Redirect");
2560 break;
2561 case 303:
2562 status = ist("303");
2563 reason = ist("See Other");
2564 break;
2565 case 301:
2566 status = ist("301");
2567 reason = ist("Moved Permanently");
2568 break;
2569 case 302:
2570 default:
2571 status = ist("302");
2572 reason = ist("Found");
2573 break;
2574 }
2575
Christopher Faulet08e66462019-05-23 16:44:59 +02002576 if (!(txn->req.flags & HTTP_MSGF_BODYLESS) && txn->req.msg_state != HTTP_MSG_DONE)
2577 close = 1;
2578
Christopher Faulet99daf282018-11-28 22:58:13 +01002579 htx = htx_from_buf(&res->buf);
Kevin Zhu96b36392020-01-07 09:42:55 +01002580 /* Trim any possible response */
2581 channel_htx_truncate(&s->res, htx);
Christopher Faulet99daf282018-11-28 22:58:13 +01002582 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
2583 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), status, reason);
2584 if (!sl)
2585 goto fail;
2586 sl->info.res.status = rule->code;
2587 s->txn->status = rule->code;
2588
Christopher Faulet08e66462019-05-23 16:44:59 +02002589 if (close && !htx_add_header(htx, ist("Connection"), ist("close")))
2590 goto fail;
2591
2592 if (!htx_add_header(htx, ist("Content-length"), ist("0")) ||
Christopher Faulet99daf282018-11-28 22:58:13 +01002593 !htx_add_header(htx, ist("Location"), location))
2594 goto fail;
2595
2596 if (rule->code == 302 || rule->code == 303 || rule->code == 307) {
2597 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")))
2598 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002599 }
2600
2601 if (rule->cookie_len) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002602 if (!htx_add_header(htx, ist("Set-Cookie"), ist2(rule->cookie_str, rule->cookie_len)))
2603 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002604 }
2605
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002606 if (!htx_add_endof(htx, HTX_BLK_EOH))
Christopher Faulet99daf282018-11-28 22:58:13 +01002607 goto fail;
2608
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002609 htx->flags |= HTX_FL_EOM;
Kevin Zhu96b36392020-01-07 09:42:55 +01002610 htx_to_buf(htx, &res->buf);
Christopher Fauleta72a7e42020-01-28 09:28:11 +01002611 if (!http_forward_proxy_resp(s, 1))
2612 goto fail;
Christopher Faulet99daf282018-11-28 22:58:13 +01002613
Christopher Faulet60b33a52020-01-28 09:18:10 +01002614 if (rule->flags & REDIRECT_FLAG_FROM_REQ) {
2615 /* let's log the request time */
2616 s->logs.tv_request = now;
2617 req->analysers &= AN_REQ_FLT_END;
Christopher Faulet99daf282018-11-28 22:58:13 +01002618
Christopher Faulet60b33a52020-01-28 09:18:10 +01002619 if (s->sess->fe == s->be) /* report it if the request was intercepted by the frontend */
2620 _HA_ATOMIC_ADD(&s->sess->fe->fe_counters.intercepted_req, 1);
2621 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002622
2623 if (!(s->flags & SF_ERR_MASK))
2624 s->flags |= SF_ERR_LOCAL;
2625 if (!(s->flags & SF_FINST_MASK))
Christopher Faulet60b33a52020-01-28 09:18:10 +01002626 s->flags |= ((rule->flags & REDIRECT_FLAG_FROM_REQ) ? SF_FINST_R : SF_FINST_H);
Christopher Fauletf2824e62018-10-01 12:12:37 +02002627
Christopher Faulet99daf282018-11-28 22:58:13 +01002628 free_trash_chunk(chunk);
2629 return 1;
2630
2631 fail:
2632 /* If an error occurred, remove the incomplete HTTP response from the
2633 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01002634 channel_htx_truncate(res, htxbuf(&res->buf));
Christopher Fauletf2824e62018-10-01 12:12:37 +02002635 free_trash_chunk(chunk);
Christopher Faulet99daf282018-11-28 22:58:13 +01002636 return 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002637}
2638
Christopher Faulet92d34fe2019-12-17 09:20:34 +01002639/* Replace all headers matching the name <name>. The header value is replaced if
2640 * it matches the regex <re>. <str> is used for the replacement. If <full> is
2641 * set to 1, the full-line is matched and replaced. Otherwise, comma-separated
2642 * values are evaluated one by one. It returns 0 on success and -1 on error.
2643 */
2644int http_replace_hdrs(struct stream* s, struct htx *htx, struct ist name,
2645 const char *str, struct my_regex *re, int full)
Christopher Faulet72333522018-10-24 11:25:02 +02002646{
2647 struct http_hdr_ctx ctx;
2648 struct buffer *output = get_trash_chunk();
2649
Christopher Faulet72333522018-10-24 11:25:02 +02002650 ctx.blk = NULL;
Christopher Faulet92d34fe2019-12-17 09:20:34 +01002651 while (http_find_header(htx, name, &ctx, full)) {
Christopher Faulet72333522018-10-24 11:25:02 +02002652 if (!regex_exec_match2(re, ctx.value.ptr, ctx.value.len, MAX_MATCH, pmatch, 0))
2653 continue;
2654
2655 output->data = exp_replace(output->area, output->size, ctx.value.ptr, str, pmatch);
2656 if (output->data == -1)
2657 return -1;
2658 if (!http_replace_header_value(htx, &ctx, ist2(output->area, output->data)))
2659 return -1;
2660 }
2661 return 0;
2662}
2663
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002664/* This function executes one of the set-{method,path,query,uri} actions. It
2665 * takes the string from the variable 'replace' with length 'len', then modifies
2666 * the relevant part of the request line accordingly. Then it updates various
2667 * pointers to the next elements which were moved, and the total buffer length.
2668 * It finds the action to be performed in p[2], previously filled by function
2669 * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal
2670 * error, though this can be revisited when this code is finally exploited.
2671 *
2672 * 'action' can be '0' to replace method, '1' to replace path, '2' to replace
Christopher Faulet312294f2020-09-02 17:17:44 +02002673 * query string, 3 to replace uri or 4 to replace the path+query.
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002674 *
2675 * In query string case, the mark question '?' must be set at the start of the
2676 * string by the caller, event if the replacement query string is empty.
2677 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002678int http_req_replace_stline(int action, const char *replace, int len,
2679 struct proxy *px, struct stream *s)
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002680{
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002681 struct htx *htx = htxbuf(&s->req.buf);
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002682
2683 switch (action) {
2684 case 0: // method
2685 if (!http_replace_req_meth(htx, ist2(replace, len)))
2686 return -1;
2687 break;
2688
2689 case 1: // path
Christopher Fauletb8ce5052020-08-31 16:11:57 +02002690 if (!http_replace_req_path(htx, ist2(replace, len), 0))
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002691 return -1;
2692 break;
2693
2694 case 2: // query
2695 if (!http_replace_req_query(htx, ist2(replace, len)))
2696 return -1;
2697 break;
2698
2699 case 3: // uri
2700 if (!http_replace_req_uri(htx, ist2(replace, len)))
2701 return -1;
2702 break;
2703
Christopher Faulet312294f2020-09-02 17:17:44 +02002704 case 4: // path + query
2705 if (!http_replace_req_path(htx, ist2(replace, len), 1))
2706 return -1;
2707 break;
2708
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002709 default:
2710 return -1;
2711 }
2712 return 0;
2713}
2714
2715/* This function replace the HTTP status code and the associated message. The
Christopher Faulete00d06c2019-12-16 17:18:42 +01002716 * variable <status> contains the new status code. This function never fails. It
2717 * returns 0 in case of success, -1 in case of internal error.
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002718 */
Christopher Faulet96bff762019-12-17 13:46:18 +01002719int http_res_set_status(unsigned int status, struct ist reason, struct stream *s)
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002720{
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002721 struct htx *htx = htxbuf(&s->res.buf);
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002722 char *res;
2723
2724 chunk_reset(&trash);
2725 res = ultoa_o(status, trash.area, trash.size);
2726 trash.data = res - trash.area;
2727
2728 /* Do we have a custom reason format string? */
Tim Duesterhuse296d3e2020-03-05 17:56:31 +01002729 if (!isttest(reason)) {
Christopher Faulet96bff762019-12-17 13:46:18 +01002730 const char *str = http_get_reason(status);
Tim Duesterhusdcf753a2021-03-04 17:31:47 +01002731 reason = ist(str);
Christopher Faulet96bff762019-12-17 13:46:18 +01002732 }
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002733
Christopher Fauletbde2c4c2020-08-31 16:43:34 +02002734 if (!http_replace_res_status(htx, ist2(trash.area, trash.data), reason))
Christopher Faulete00d06c2019-12-16 17:18:42 +01002735 return -1;
2736 return 0;
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002737}
2738
Christopher Faulet3e964192018-10-24 11:39:23 +02002739/* Executes the http-request rules <rules> for stream <s>, proxy <px> and
2740 * transaction <txn>. Returns the verdict of the first rule that prevents
2741 * further processing of the request (auth, deny, ...), and defaults to
2742 * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or
2743 * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT
2744 * on txn->flags if it encounters a tarpit rule. If <deny_status> is not NULL
2745 * and a deny/tarpit rule is matched, it will be filled with this rule's deny
2746 * status.
2747 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002748static enum rule_result http_req_get_intercept_rule(struct proxy *px, struct list *rules,
Christopher Fauletb58f62b2020-01-13 16:40:13 +01002749 struct stream *s)
Christopher Faulet3e964192018-10-24 11:39:23 +02002750{
2751 struct session *sess = strm_sess(s);
2752 struct http_txn *txn = s->txn;
Christopher Faulet3e964192018-10-24 11:39:23 +02002753 struct act_rule *rule;
Christopher Faulet3e964192018-10-24 11:39:23 +02002754 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002755 int act_opts = 0;
Christopher Faulet3e964192018-10-24 11:39:23 +02002756
Christopher Faulet3e964192018-10-24 11:39:23 +02002757 /* If "the current_rule_list" match the executed rule list, we are in
2758 * resume condition. If a resume is needed it is always in the action
2759 * and never in the ACL or converters. In this case, we initialise the
2760 * current rule, and go to the action execution point.
2761 */
2762 if (s->current_rule) {
2763 rule = s->current_rule;
2764 s->current_rule = NULL;
2765 if (s->current_rule_list == rules)
2766 goto resume_execution;
2767 }
2768 s->current_rule_list = rules;
2769
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002770 /* start the ruleset evaluation in strict mode */
2771 txn->req.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01002772
Christopher Faulet3e964192018-10-24 11:39:23 +02002773 list_for_each_entry(rule, rules, list) {
2774 /* check optional condition */
2775 if (rule->cond) {
2776 int ret;
2777
2778 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
2779 ret = acl_pass(ret);
2780
2781 if (rule->cond->pol == ACL_COND_UNLESS)
2782 ret = !ret;
2783
2784 if (!ret) /* condition not matched */
2785 continue;
2786 }
2787
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002788 act_opts |= ACT_OPT_FIRST;
Christopher Faulet3e964192018-10-24 11:39:23 +02002789 resume_execution:
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002790 /* Always call the action function if defined */
2791 if (rule->action_ptr) {
2792 if ((s->req.flags & CF_READ_ERROR) ||
2793 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
2794 (px->options & PR_O_ABRT_CLOSE)))
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002795 act_opts |= ACT_OPT_FINAL;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002796
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002797 switch (rule->action_ptr(rule, px, sess, s, act_opts)) {
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002798 case ACT_RET_CONT:
2799 break;
2800 case ACT_RET_STOP:
2801 rule_ret = HTTP_RULE_RES_STOP;
2802 goto end;
2803 case ACT_RET_YIELD:
2804 s->current_rule = rule;
2805 rule_ret = HTTP_RULE_RES_YIELD;
2806 goto end;
2807 case ACT_RET_ERR:
2808 rule_ret = HTTP_RULE_RES_ERROR;
2809 goto end;
2810 case ACT_RET_DONE:
2811 rule_ret = HTTP_RULE_RES_DONE;
2812 goto end;
2813 case ACT_RET_DENY:
Christopher Fauletb58f62b2020-01-13 16:40:13 +01002814 if (txn->status == -1)
2815 txn->status = 403;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002816 rule_ret = HTTP_RULE_RES_DENY;
2817 goto end;
2818 case ACT_RET_ABRT:
2819 rule_ret = HTTP_RULE_RES_ABRT;
2820 goto end;
2821 case ACT_RET_INV:
2822 rule_ret = HTTP_RULE_RES_BADREQ;
2823 goto end;
2824 }
2825 continue; /* eval the next rule */
2826 }
2827
2828 /* If not action function defined, check for known actions */
Christopher Faulet3e964192018-10-24 11:39:23 +02002829 switch (rule->action) {
2830 case ACT_ACTION_ALLOW:
2831 rule_ret = HTTP_RULE_RES_STOP;
2832 goto end;
2833
2834 case ACT_ACTION_DENY:
Christopher Faulet5cb513a2020-05-13 17:56:56 +02002835 txn->status = rule->arg.http_reply->status;
2836 txn->http_reply = rule->arg.http_reply;
Christopher Faulet3e964192018-10-24 11:39:23 +02002837 rule_ret = HTTP_RULE_RES_DENY;
2838 goto end;
2839
2840 case ACT_HTTP_REQ_TARPIT:
2841 txn->flags |= TX_CLTARPIT;
Christopher Faulet5cb513a2020-05-13 17:56:56 +02002842 txn->status = rule->arg.http_reply->status;
2843 txn->http_reply = rule->arg.http_reply;
Christopher Faulet3e964192018-10-24 11:39:23 +02002844 rule_ret = HTTP_RULE_RES_DENY;
2845 goto end;
2846
Christopher Faulet3e964192018-10-24 11:39:23 +02002847 case ACT_HTTP_REDIR:
Christopher Faulet90d22a82020-03-06 11:18:39 +01002848 rule_ret = HTTP_RULE_RES_ABRT;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002849 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
Christopher Faulet3a26bee2019-12-16 12:47:40 +01002850 rule_ret = HTTP_RULE_RES_ERROR;
Christopher Faulet3e964192018-10-24 11:39:23 +02002851 goto end;
2852
2853 case ACT_HTTP_SET_NICE:
Christopher Faulet96bff762019-12-17 13:46:18 +01002854 s->task->nice = rule->arg.http.i;
Christopher Faulet3e964192018-10-24 11:39:23 +02002855 break;
2856
2857 case ACT_HTTP_SET_TOS:
Christopher Faulet96bff762019-12-17 13:46:18 +01002858 conn_set_tos(objt_conn(sess->origin), rule->arg.http.i);
Christopher Faulet3e964192018-10-24 11:39:23 +02002859 break;
2860
2861 case ACT_HTTP_SET_MARK:
Christopher Faulet96bff762019-12-17 13:46:18 +01002862 conn_set_mark(objt_conn(sess->origin), rule->arg.http.i);
Christopher Faulet3e964192018-10-24 11:39:23 +02002863 break;
2864
2865 case ACT_HTTP_SET_LOGL:
Christopher Faulet96bff762019-12-17 13:46:18 +01002866 s->logs.level = rule->arg.http.i;
Christopher Faulet3e964192018-10-24 11:39:23 +02002867 break;
2868
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002869 /* other flags exists, but normally, they never be matched. */
Christopher Faulet3e964192018-10-24 11:39:23 +02002870 default:
2871 break;
2872 }
2873 }
2874
2875 end:
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002876 /* if the ruleset evaluation is finished reset the strict mode */
Christopher Faulet46f95542019-12-20 10:07:22 +01002877 if (rule_ret != HTTP_RULE_RES_YIELD)
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002878 txn->req.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01002879
Christopher Faulet3e964192018-10-24 11:39:23 +02002880 /* we reached the end of the rules, nothing to report */
2881 return rule_ret;
2882}
2883
2884/* Executes the http-response rules <rules> for stream <s> and proxy <px>. It
2885 * returns one of 5 possible statuses: HTTP_RULE_RES_CONT, HTTP_RULE_RES_STOP,
2886 * HTTP_RULE_RES_DONE, HTTP_RULE_RES_YIELD, or HTTP_RULE_RES_BADREQ. If *CONT
2887 * is returned, the process can continue the evaluation of next rule list. If
2888 * *STOP or *DONE is returned, the process must stop the evaluation. If *BADREQ
2889 * is returned, it means the operation could not be processed and a server error
Christopher Fauleta53abad2020-05-13 08:12:22 +02002890 * must be returned. If *YIELD is returned, the caller must call again the
2891 * function with the same context.
Christopher Faulet3e964192018-10-24 11:39:23 +02002892 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002893static enum rule_result http_res_get_intercept_rule(struct proxy *px, struct list *rules,
2894 struct stream *s)
Christopher Faulet3e964192018-10-24 11:39:23 +02002895{
2896 struct session *sess = strm_sess(s);
2897 struct http_txn *txn = s->txn;
Christopher Faulet3e964192018-10-24 11:39:23 +02002898 struct act_rule *rule;
Christopher Faulet3e964192018-10-24 11:39:23 +02002899 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002900 int act_opts = 0;
Christopher Faulet3e964192018-10-24 11:39:23 +02002901
Christopher Faulet3e964192018-10-24 11:39:23 +02002902 /* If "the current_rule_list" match the executed rule list, we are in
2903 * resume condition. If a resume is needed it is always in the action
2904 * and never in the ACL or converters. In this case, we initialise the
2905 * current rule, and go to the action execution point.
2906 */
2907 if (s->current_rule) {
2908 rule = s->current_rule;
2909 s->current_rule = NULL;
2910 if (s->current_rule_list == rules)
2911 goto resume_execution;
2912 }
2913 s->current_rule_list = rules;
2914
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002915 /* start the ruleset evaluation in strict mode */
2916 txn->rsp.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01002917
Christopher Faulet3e964192018-10-24 11:39:23 +02002918 list_for_each_entry(rule, rules, list) {
2919 /* check optional condition */
2920 if (rule->cond) {
2921 int ret;
2922
2923 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
2924 ret = acl_pass(ret);
2925
2926 if (rule->cond->pol == ACL_COND_UNLESS)
2927 ret = !ret;
2928
2929 if (!ret) /* condition not matched */
2930 continue;
2931 }
2932
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002933 act_opts |= ACT_OPT_FIRST;
Christopher Faulet3e964192018-10-24 11:39:23 +02002934resume_execution:
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002935
2936 /* Always call the action function if defined */
2937 if (rule->action_ptr) {
2938 if ((s->req.flags & CF_READ_ERROR) ||
2939 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
2940 (px->options & PR_O_ABRT_CLOSE)))
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002941 act_opts |= ACT_OPT_FINAL;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002942
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002943 switch (rule->action_ptr(rule, px, sess, s, act_opts)) {
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002944 case ACT_RET_CONT:
2945 break;
2946 case ACT_RET_STOP:
2947 rule_ret = HTTP_RULE_RES_STOP;
2948 goto end;
2949 case ACT_RET_YIELD:
2950 s->current_rule = rule;
2951 rule_ret = HTTP_RULE_RES_YIELD;
2952 goto end;
2953 case ACT_RET_ERR:
2954 rule_ret = HTTP_RULE_RES_ERROR;
2955 goto end;
2956 case ACT_RET_DONE:
2957 rule_ret = HTTP_RULE_RES_DONE;
2958 goto end;
2959 case ACT_RET_DENY:
Christopher Fauletb58f62b2020-01-13 16:40:13 +01002960 if (txn->status == -1)
2961 txn->status = 502;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002962 rule_ret = HTTP_RULE_RES_DENY;
2963 goto end;
2964 case ACT_RET_ABRT:
2965 rule_ret = HTTP_RULE_RES_ABRT;
2966 goto end;
2967 case ACT_RET_INV:
2968 rule_ret = HTTP_RULE_RES_BADREQ;
2969 goto end;
2970 }
2971 continue; /* eval the next rule */
2972 }
2973
2974 /* If not action function defined, check for known actions */
Christopher Faulet3e964192018-10-24 11:39:23 +02002975 switch (rule->action) {
2976 case ACT_ACTION_ALLOW:
2977 rule_ret = HTTP_RULE_RES_STOP; /* "allow" rules are OK */
2978 goto end;
2979
2980 case ACT_ACTION_DENY:
Christopher Faulet5cb513a2020-05-13 17:56:56 +02002981 txn->status = rule->arg.http_reply->status;
2982 txn->http_reply = rule->arg.http_reply;
Christopher Faulet3a26bee2019-12-16 12:47:40 +01002983 rule_ret = HTTP_RULE_RES_DENY;
Christopher Faulet3e964192018-10-24 11:39:23 +02002984 goto end;
2985
2986 case ACT_HTTP_SET_NICE:
Christopher Faulet96bff762019-12-17 13:46:18 +01002987 s->task->nice = rule->arg.http.i;
Christopher Faulet3e964192018-10-24 11:39:23 +02002988 break;
2989
2990 case ACT_HTTP_SET_TOS:
Christopher Faulet96bff762019-12-17 13:46:18 +01002991 conn_set_tos(objt_conn(sess->origin), rule->arg.http.i);
Christopher Faulet3e964192018-10-24 11:39:23 +02002992 break;
2993
2994 case ACT_HTTP_SET_MARK:
Christopher Faulet96bff762019-12-17 13:46:18 +01002995 conn_set_mark(objt_conn(sess->origin), rule->arg.http.i);
Christopher Faulet3e964192018-10-24 11:39:23 +02002996 break;
2997
2998 case ACT_HTTP_SET_LOGL:
Christopher Faulet96bff762019-12-17 13:46:18 +01002999 s->logs.level = rule->arg.http.i;
Christopher Faulet3e964192018-10-24 11:39:23 +02003000 break;
3001
Christopher Faulet3e964192018-10-24 11:39:23 +02003002 case ACT_HTTP_REDIR:
Christopher Faulet49c2a702020-03-06 15:44:37 +01003003 rule_ret = HTTP_RULE_RES_ABRT;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003004 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
Christopher Faulet3a26bee2019-12-16 12:47:40 +01003005 rule_ret = HTTP_RULE_RES_ERROR;
Christopher Faulet3e964192018-10-24 11:39:23 +02003006 goto end;
3007
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01003008 /* other flags exists, but normally, they never be matched. */
Christopher Faulet3e964192018-10-24 11:39:23 +02003009 default:
3010 break;
3011 }
3012 }
3013
3014 end:
Christopher Faulet1aea50e2020-01-17 16:03:53 +01003015 /* if the ruleset evaluation is finished reset the strict mode */
Christopher Faulet46f95542019-12-20 10:07:22 +01003016 if (rule_ret != HTTP_RULE_RES_YIELD)
Christopher Faulet1aea50e2020-01-17 16:03:53 +01003017 txn->rsp.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01003018
Christopher Faulet3e964192018-10-24 11:39:23 +02003019 /* we reached the end of the rules, nothing to report */
3020 return rule_ret;
3021}
3022
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01003023/* Executes backend and frontend http-after-response rules for the stream <s>,
3024 * in that order. it return 1 on success and 0 on error. It is the caller
3025 * responsibility to catch error or ignore it. If it catches it, this function
3026 * may be called a second time, for the internal error.
3027 */
3028int http_eval_after_res_rules(struct stream *s)
3029{
3030 struct session *sess = s->sess;
3031 enum rule_result ret = HTTP_RULE_RES_CONT;
3032
Christopher Faulet507479b2020-05-15 12:29:46 +02003033 /* Eval after-response ruleset only if the reply is not const */
3034 if (s->txn->flags & TX_CONST_REPLY)
3035 goto end;
3036
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01003037 /* prune the request variables if not already done and swap to the response variables. */
3038 if (s->vars_reqres.scope != SCOPE_RES) {
3039 if (!LIST_ISEMPTY(&s->vars_reqres.head))
3040 vars_prune(&s->vars_reqres, s->sess, s);
3041 vars_init(&s->vars_reqres, SCOPE_RES);
3042 }
3043
3044 ret = http_res_get_intercept_rule(s->be, &s->be->http_after_res_rules, s);
3045 if ((ret == HTTP_RULE_RES_CONT || ret == HTTP_RULE_RES_STOP) && sess->fe != s->be)
3046 ret = http_res_get_intercept_rule(sess->fe, &sess->fe->http_after_res_rules, s);
3047
Christopher Faulet507479b2020-05-15 12:29:46 +02003048 end:
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01003049 /* All other codes than CONTINUE, STOP or DONE are forbidden */
3050 return (ret == HTTP_RULE_RES_CONT || ret == HTTP_RULE_RES_STOP || ret == HTTP_RULE_RES_DONE);
3051}
3052
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003053/*
3054 * Manage client-side cookie. It can impact performance by about 2% so it is
3055 * desirable to call it only when needed. This code is quite complex because
3056 * of the multiple very crappy and ambiguous syntaxes we have to support. it
3057 * highly recommended not to touch this part without a good reason !
3058 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003059static void http_manage_client_side_cookies(struct stream *s, struct channel *req)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003060{
3061 struct session *sess = s->sess;
3062 struct http_txn *txn = s->txn;
3063 struct htx *htx;
3064 struct http_hdr_ctx ctx;
3065 char *hdr_beg, *hdr_end, *del_from;
3066 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
3067 int preserve_hdr;
3068
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003069 htx = htxbuf(&req->buf);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003070 ctx.blk = NULL;
3071 while (http_find_header(htx, ist("Cookie"), &ctx, 1)) {
Olivier Houchardf0f42382019-07-22 17:43:46 +02003072 int is_first = 1;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003073 del_from = NULL; /* nothing to be deleted */
3074 preserve_hdr = 0; /* assume we may kill the whole header */
3075
3076 /* Now look for cookies. Conforming to RFC2109, we have to support
3077 * attributes whose name begin with a '$', and associate them with
3078 * the right cookie, if we want to delete this cookie.
3079 * So there are 3 cases for each cookie read :
3080 * 1) it's a special attribute, beginning with a '$' : ignore it.
3081 * 2) it's a server id cookie that we *MAY* want to delete : save
3082 * some pointers on it (last semi-colon, beginning of cookie...)
3083 * 3) it's an application cookie : we *MAY* have to delete a previous
3084 * "special" cookie.
3085 * At the end of loop, if a "special" cookie remains, we may have to
3086 * remove it. If no application cookie persists in the header, we
3087 * *MUST* delete it.
3088 *
3089 * Note: RFC2965 is unclear about the processing of spaces around
3090 * the equal sign in the ATTR=VALUE form. A careful inspection of
3091 * the RFC explicitly allows spaces before it, and not within the
3092 * tokens (attrs or values). An inspection of RFC2109 allows that
3093 * too but section 10.1.3 lets one think that spaces may be allowed
3094 * after the equal sign too, resulting in some (rare) buggy
3095 * implementations trying to do that. So let's do what servers do.
3096 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
3097 * allowed quoted strings in values, with any possible character
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003098 * after a backslash, including control chars and delimiters, which
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003099 * causes parsing to become ambiguous. Browsers also allow spaces
3100 * within values even without quotes.
3101 *
3102 * We have to keep multiple pointers in order to support cookie
3103 * removal at the beginning, middle or end of header without
3104 * corrupting the header. All of these headers are valid :
3105 *
3106 * hdr_beg hdr_end
3107 * | |
3108 * v |
3109 * NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3 |
3110 * NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3 v
3111 * NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3
3112 * | | | | | | |
3113 * | | | | | | |
3114 * | | | | | | +--> next
3115 * | | | | | +----> val_end
3116 * | | | | +-----------> val_beg
3117 * | | | +--------------> equal
3118 * | | +----------------> att_end
3119 * | +---------------------> att_beg
3120 * +--------------------------> prev
3121 *
3122 */
3123 hdr_beg = ctx.value.ptr;
3124 hdr_end = hdr_beg + ctx.value.len;
3125 for (prev = hdr_beg; prev < hdr_end; prev = next) {
3126 /* Iterate through all cookies on this line */
3127
3128 /* find att_beg */
3129 att_beg = prev;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003130 if (!is_first)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003131 att_beg++;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003132 is_first = 0;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003133
3134 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
3135 att_beg++;
3136
3137 /* find att_end : this is the first character after the last non
3138 * space before the equal. It may be equal to hdr_end.
3139 */
3140 equal = att_end = att_beg;
3141 while (equal < hdr_end) {
3142 if (*equal == '=' || *equal == ',' || *equal == ';')
3143 break;
3144 if (HTTP_IS_SPHT(*equal++))
3145 continue;
3146 att_end = equal;
3147 }
3148
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003149 /* here, <equal> points to '=', a delimiter or the end. <att_end>
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003150 * is between <att_beg> and <equal>, both may be identical.
3151 */
3152 /* look for end of cookie if there is an equal sign */
3153 if (equal < hdr_end && *equal == '=') {
3154 /* look for the beginning of the value */
3155 val_beg = equal + 1;
3156 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
3157 val_beg++;
3158
3159 /* find the end of the value, respecting quotes */
3160 next = http_find_cookie_value_end(val_beg, hdr_end);
3161
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003162 /* make val_end point to the first white space or delimiter after the value */
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003163 val_end = next;
3164 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
3165 val_end--;
3166 }
3167 else
3168 val_beg = val_end = next = equal;
3169
3170 /* We have nothing to do with attributes beginning with
3171 * '$'. However, they will automatically be removed if a
3172 * header before them is removed, since they're supposed
3173 * to be linked together.
3174 */
3175 if (*att_beg == '$')
3176 continue;
3177
3178 /* Ignore cookies with no equal sign */
3179 if (equal == next) {
3180 /* This is not our cookie, so we must preserve it. But if we already
3181 * scheduled another cookie for removal, we cannot remove the
3182 * complete header, but we can remove the previous block itself.
3183 */
3184 preserve_hdr = 1;
3185 if (del_from != NULL) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003186 int delta = http_del_hdr_value(hdr_beg, hdr_end, &del_from, prev);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003187 val_end += delta;
3188 next += delta;
3189 hdr_end += delta;
3190 prev = del_from;
3191 del_from = NULL;
3192 }
3193 continue;
3194 }
3195
3196 /* if there are spaces around the equal sign, we need to
3197 * strip them otherwise we'll get trouble for cookie captures,
3198 * or even for rewrites. Since this happens extremely rarely,
3199 * it does not hurt performance.
3200 */
3201 if (unlikely(att_end != equal || val_beg > equal + 1)) {
3202 int stripped_before = 0;
3203 int stripped_after = 0;
3204
3205 if (att_end != equal) {
3206 memmove(att_end, equal, hdr_end - equal);
3207 stripped_before = (att_end - equal);
3208 equal += stripped_before;
3209 val_beg += stripped_before;
3210 }
3211
3212 if (val_beg > equal + 1) {
3213 memmove(equal + 1, val_beg, hdr_end + stripped_before - val_beg);
3214 stripped_after = (equal + 1) - val_beg;
3215 val_beg += stripped_after;
3216 stripped_before += stripped_after;
3217 }
3218
3219 val_end += stripped_before;
3220 next += stripped_before;
3221 hdr_end += stripped_before;
3222 }
3223 /* now everything is as on the diagram above */
3224
3225 /* First, let's see if we want to capture this cookie. We check
3226 * that we don't already have a client side cookie, because we
3227 * can only capture one. Also as an optimisation, we ignore
3228 * cookies shorter than the declared name.
3229 */
3230 if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL &&
3231 (val_end - att_beg >= sess->fe->capture_namelen) &&
3232 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
3233 int log_len = val_end - att_beg;
3234
3235 if ((txn->cli_cookie = pool_alloc(pool_head_capture)) == NULL) {
3236 ha_alert("HTTP logging : out of memory.\n");
3237 } else {
3238 if (log_len > sess->fe->capture_len)
3239 log_len = sess->fe->capture_len;
3240 memcpy(txn->cli_cookie, att_beg, log_len);
3241 txn->cli_cookie[log_len] = 0;
3242 }
3243 }
3244
3245 /* Persistence cookies in passive, rewrite or insert mode have the
3246 * following form :
3247 *
3248 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
3249 *
3250 * For cookies in prefix mode, the form is :
3251 *
3252 * Cookie: NAME=SRV~VALUE
3253 */
3254 if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
3255 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
3256 struct server *srv = s->be->srv;
3257 char *delim;
3258
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003259 /* if we're in cookie prefix mode, we'll search the delimiter so that we
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003260 * have the server ID between val_beg and delim, and the original cookie between
3261 * delim+1 and val_end. Otherwise, delim==val_end :
3262 *
3263 * hdr_beg
3264 * |
3265 * v
3266 * NAME=SRV; # in all but prefix modes
3267 * NAME=SRV~OPAQUE ; # in prefix mode
3268 * || || | |+-> next
3269 * || || | +--> val_end
3270 * || || +---------> delim
3271 * || |+------------> val_beg
3272 * || +-------------> att_end = equal
3273 * |+-----------------> att_beg
3274 * +------------------> prev
3275 *
3276 */
3277 if (s->be->ck_opts & PR_CK_PFX) {
3278 for (delim = val_beg; delim < val_end; delim++)
3279 if (*delim == COOKIE_DELIM)
3280 break;
3281 }
3282 else {
3283 char *vbar1;
3284 delim = val_end;
3285 /* Now check if the cookie contains a date field, which would
3286 * appear after a vertical bar ('|') just after the server name
3287 * and before the delimiter.
3288 */
3289 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
3290 if (vbar1) {
3291 /* OK, so left of the bar is the server's cookie and
3292 * right is the last seen date. It is a base64 encoded
3293 * 30-bit value representing the UNIX date since the
3294 * epoch in 4-second quantities.
3295 */
3296 int val;
3297 delim = vbar1++;
3298 if (val_end - vbar1 >= 5) {
3299 val = b64tos30(vbar1);
3300 if (val > 0)
3301 txn->cookie_last_date = val << 2;
3302 }
3303 /* look for a second vertical bar */
3304 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
3305 if (vbar1 && (val_end - vbar1 > 5)) {
3306 val = b64tos30(vbar1 + 1);
3307 if (val > 0)
3308 txn->cookie_first_date = val << 2;
3309 }
3310 }
3311 }
3312
3313 /* if the cookie has an expiration date and the proxy wants to check
3314 * it, then we do that now. We first check if the cookie is too old,
3315 * then only if it has expired. We detect strict overflow because the
3316 * time resolution here is not great (4 seconds). Cookies with dates
3317 * in the future are ignored if their offset is beyond one day. This
3318 * allows an admin to fix timezone issues without expiring everyone
3319 * and at the same time avoids keeping unwanted side effects for too
3320 * long.
3321 */
3322 if (txn->cookie_first_date && s->be->cookie_maxlife &&
3323 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) ||
3324 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
3325 txn->flags &= ~TX_CK_MASK;
3326 txn->flags |= TX_CK_OLD;
3327 delim = val_beg; // let's pretend we have not found the cookie
3328 txn->cookie_first_date = 0;
3329 txn->cookie_last_date = 0;
3330 }
3331 else if (txn->cookie_last_date && s->be->cookie_maxidle &&
3332 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) ||
3333 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
3334 txn->flags &= ~TX_CK_MASK;
3335 txn->flags |= TX_CK_EXPIRED;
3336 delim = val_beg; // let's pretend we have not found the cookie
3337 txn->cookie_first_date = 0;
3338 txn->cookie_last_date = 0;
3339 }
3340
3341 /* Here, we'll look for the first running server which supports the cookie.
3342 * This allows to share a same cookie between several servers, for example
3343 * to dedicate backup servers to specific servers only.
3344 * However, to prevent clients from sticking to cookie-less backup server
3345 * when they have incidentely learned an empty cookie, we simply ignore
3346 * empty cookies and mark them as invalid.
3347 * The same behaviour is applied when persistence must be ignored.
3348 */
3349 if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
3350 srv = NULL;
3351
3352 while (srv) {
3353 if (srv->cookie && (srv->cklen == delim - val_beg) &&
3354 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
3355 if ((srv->cur_state != SRV_ST_STOPPED) ||
3356 (s->be->options & PR_O_PERSIST) ||
3357 (s->flags & SF_FORCE_PRST)) {
3358 /* we found the server and we can use it */
3359 txn->flags &= ~TX_CK_MASK;
3360 txn->flags |= (srv->cur_state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
3361 s->flags |= SF_DIRECT | SF_ASSIGNED;
3362 s->target = &srv->obj_type;
3363 break;
3364 } else {
3365 /* we found a server, but it's down,
3366 * mark it as such and go on in case
3367 * another one is available.
3368 */
3369 txn->flags &= ~TX_CK_MASK;
3370 txn->flags |= TX_CK_DOWN;
3371 }
3372 }
3373 srv = srv->next;
3374 }
3375
3376 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
3377 /* no server matched this cookie or we deliberately skipped it */
3378 txn->flags &= ~TX_CK_MASK;
3379 if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
3380 txn->flags |= TX_CK_UNUSED;
3381 else
3382 txn->flags |= TX_CK_INVALID;
3383 }
3384
3385 /* depending on the cookie mode, we may have to either :
3386 * - delete the complete cookie if we're in insert+indirect mode, so that
3387 * the server never sees it ;
3388 * - remove the server id from the cookie value, and tag the cookie as an
Joseph Herlante9d5c722018-11-25 11:00:25 -08003389 * application cookie so that it does not get accidentally removed later,
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003390 * if we're in cookie prefix mode
3391 */
3392 if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
3393 int delta; /* negative */
3394
3395 memmove(val_beg, delim + 1, hdr_end - (delim + 1));
3396 delta = val_beg - (delim + 1);
3397 val_end += delta;
3398 next += delta;
3399 hdr_end += delta;
3400 del_from = NULL;
3401 preserve_hdr = 1; /* we want to keep this cookie */
3402 }
3403 else if (del_from == NULL &&
3404 (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
3405 del_from = prev;
3406 }
3407 }
3408 else {
3409 /* This is not our cookie, so we must preserve it. But if we already
3410 * scheduled another cookie for removal, we cannot remove the
3411 * complete header, but we can remove the previous block itself.
3412 */
3413 preserve_hdr = 1;
3414
3415 if (del_from != NULL) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003416 int delta = http_del_hdr_value(hdr_beg, hdr_end, &del_from, prev);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003417 if (att_beg >= del_from)
3418 att_beg += delta;
3419 if (att_end >= del_from)
3420 att_end += delta;
3421 val_beg += delta;
3422 val_end += delta;
3423 next += delta;
3424 hdr_end += delta;
3425 prev = del_from;
3426 del_from = NULL;
3427 }
3428 }
3429
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003430 } /* for each cookie */
3431
3432
3433 /* There are no more cookies on this line.
3434 * We may still have one (or several) marked for deletion at the
3435 * end of the line. We must do this now in two ways :
3436 * - if some cookies must be preserved, we only delete from the
3437 * mark to the end of line ;
3438 * - if nothing needs to be preserved, simply delete the whole header
3439 */
3440 if (del_from) {
3441 hdr_end = (preserve_hdr ? del_from : hdr_beg);
3442 }
3443 if ((hdr_end - hdr_beg) != ctx.value.len) {
Christopher Faulet3e2638e2019-06-18 09:49:16 +02003444 if (hdr_beg != hdr_end)
3445 htx_change_blk_value_len(htx, ctx.blk, hdr_end - hdr_beg);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003446 else
3447 http_remove_header(htx, &ctx);
3448 }
3449 } /* for each "Cookie header */
3450}
3451
3452/*
3453 * Manage server-side cookies. It can impact performance by about 2% so it is
3454 * desirable to call it only when needed. This function is also used when we
3455 * just need to know if there is a cookie (eg: for check-cache).
3456 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003457static void http_manage_server_side_cookies(struct stream *s, struct channel *res)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003458{
3459 struct session *sess = s->sess;
3460 struct http_txn *txn = s->txn;
3461 struct htx *htx;
3462 struct http_hdr_ctx ctx;
3463 struct server *srv;
3464 char *hdr_beg, *hdr_end;
3465 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau6f7a02a2019-04-15 21:49:49 +02003466 int is_cookie2 = 0;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003467
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003468 htx = htxbuf(&res->buf);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003469
3470 ctx.blk = NULL;
3471 while (1) {
Olivier Houchardf0f42382019-07-22 17:43:46 +02003472 int is_first = 1;
3473
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003474 if (!http_find_header(htx, ist("Set-Cookie"), &ctx, 1)) {
3475 if (!http_find_header(htx, ist("Set-Cookie2"), &ctx, 1))
3476 break;
3477 is_cookie2 = 1;
3478 }
3479
3480 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
3481 * <prev> points to the colon.
3482 */
3483 txn->flags |= TX_SCK_PRESENT;
3484
3485 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
3486 * check-cache is enabled) and we are not interested in checking
3487 * them. Warning, the cookie capture is declared in the frontend.
3488 */
3489 if (s->be->cookie_name == NULL && sess->fe->capture_name == NULL)
3490 break;
3491
3492 /* OK so now we know we have to process this response cookie.
3493 * The format of the Set-Cookie header is slightly different
3494 * from the format of the Cookie header in that it does not
3495 * support the comma as a cookie delimiter (thus the header
3496 * cannot be folded) because the Expires attribute described in
3497 * the original Netscape's spec may contain an unquoted date
3498 * with a comma inside. We have to live with this because
3499 * many browsers don't support Max-Age and some browsers don't
3500 * support quoted strings. However the Set-Cookie2 header is
3501 * clean.
3502 *
3503 * We have to keep multiple pointers in order to support cookie
3504 * removal at the beginning, middle or end of header without
3505 * corrupting the header (in case of set-cookie2). A special
3506 * pointer, <scav> points to the beginning of the set-cookie-av
3507 * fields after the first semi-colon. The <next> pointer points
3508 * either to the end of line (set-cookie) or next unquoted comma
3509 * (set-cookie2). All of these headers are valid :
3510 *
3511 * hdr_beg hdr_end
3512 * | |
3513 * v |
3514 * NAME1 = VALUE 1 ; Secure; Path="/" |
3515 * NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT v
3516 * NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT
3517 * NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard
3518 * | | | | | | | |
3519 * | | | | | | | +-> next
3520 * | | | | | | +------------> scav
3521 * | | | | | +--------------> val_end
3522 * | | | | +--------------------> val_beg
3523 * | | | +----------------------> equal
3524 * | | +------------------------> att_end
3525 * | +----------------------------> att_beg
3526 * +------------------------------> prev
3527 * -------------------------------> hdr_beg
3528 */
3529 hdr_beg = ctx.value.ptr;
3530 hdr_end = hdr_beg + ctx.value.len;
3531 for (prev = hdr_beg; prev < hdr_end; prev = next) {
3532
3533 /* Iterate through all cookies on this line */
3534
3535 /* find att_beg */
3536 att_beg = prev;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003537 if (!is_first)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003538 att_beg++;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003539 is_first = 0;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003540
3541 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
3542 att_beg++;
3543
3544 /* find att_end : this is the first character after the last non
3545 * space before the equal. It may be equal to hdr_end.
3546 */
3547 equal = att_end = att_beg;
3548
3549 while (equal < hdr_end) {
3550 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
3551 break;
3552 if (HTTP_IS_SPHT(*equal++))
3553 continue;
3554 att_end = equal;
3555 }
3556
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003557 /* here, <equal> points to '=', a delimiter or the end. <att_end>
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003558 * is between <att_beg> and <equal>, both may be identical.
3559 */
3560
3561 /* look for end of cookie if there is an equal sign */
3562 if (equal < hdr_end && *equal == '=') {
3563 /* look for the beginning of the value */
3564 val_beg = equal + 1;
3565 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
3566 val_beg++;
3567
3568 /* find the end of the value, respecting quotes */
3569 next = http_find_cookie_value_end(val_beg, hdr_end);
3570
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003571 /* make val_end point to the first white space or delimiter after the value */
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003572 val_end = next;
3573 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
3574 val_end--;
3575 }
3576 else {
3577 /* <equal> points to next comma, semi-colon or EOL */
3578 val_beg = val_end = next = equal;
3579 }
3580
3581 if (next < hdr_end) {
3582 /* Set-Cookie2 supports multiple cookies, and <next> points to
3583 * a colon or semi-colon before the end. So skip all attr-value
3584 * pairs and look for the next comma. For Set-Cookie, since
3585 * commas are permitted in values, skip to the end.
3586 */
3587 if (is_cookie2)
3588 next = http_find_hdr_value_end(next, hdr_end);
3589 else
3590 next = hdr_end;
3591 }
3592
3593 /* Now everything is as on the diagram above */
3594
3595 /* Ignore cookies with no equal sign */
3596 if (equal == val_end)
3597 continue;
3598
3599 /* If there are spaces around the equal sign, we need to
3600 * strip them otherwise we'll get trouble for cookie captures,
3601 * or even for rewrites. Since this happens extremely rarely,
3602 * it does not hurt performance.
3603 */
3604 if (unlikely(att_end != equal || val_beg > equal + 1)) {
3605 int stripped_before = 0;
3606 int stripped_after = 0;
3607
3608 if (att_end != equal) {
3609 memmove(att_end, equal, hdr_end - equal);
3610 stripped_before = (att_end - equal);
3611 equal += stripped_before;
3612 val_beg += stripped_before;
3613 }
3614
3615 if (val_beg > equal + 1) {
3616 memmove(equal + 1, val_beg, hdr_end + stripped_before - val_beg);
3617 stripped_after = (equal + 1) - val_beg;
3618 val_beg += stripped_after;
3619 stripped_before += stripped_after;
3620 }
3621
3622 val_end += stripped_before;
3623 next += stripped_before;
3624 hdr_end += stripped_before;
3625
Christopher Faulet3e2638e2019-06-18 09:49:16 +02003626 htx_change_blk_value_len(htx, ctx.blk, hdr_end - hdr_beg);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003627 ctx.value.len = hdr_end - hdr_beg;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003628 }
3629
3630 /* First, let's see if we want to capture this cookie. We check
3631 * that we don't already have a server side cookie, because we
3632 * can only capture one. Also as an optimisation, we ignore
3633 * cookies shorter than the declared name.
3634 */
3635 if (sess->fe->capture_name != NULL &&
3636 txn->srv_cookie == NULL &&
3637 (val_end - att_beg >= sess->fe->capture_namelen) &&
3638 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
3639 int log_len = val_end - att_beg;
3640 if ((txn->srv_cookie = pool_alloc(pool_head_capture)) == NULL) {
3641 ha_alert("HTTP logging : out of memory.\n");
3642 }
3643 else {
3644 if (log_len > sess->fe->capture_len)
3645 log_len = sess->fe->capture_len;
3646 memcpy(txn->srv_cookie, att_beg, log_len);
3647 txn->srv_cookie[log_len] = 0;
3648 }
3649 }
3650
3651 srv = objt_server(s->target);
3652 /* now check if we need to process it for persistence */
3653 if (!(s->flags & SF_IGNORE_PRST) &&
3654 (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
3655 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
3656 /* assume passive cookie by default */
3657 txn->flags &= ~TX_SCK_MASK;
3658 txn->flags |= TX_SCK_FOUND;
3659
3660 /* If the cookie is in insert mode on a known server, we'll delete
3661 * this occurrence because we'll insert another one later.
3662 * We'll delete it too if the "indirect" option is set and we're in
3663 * a direct access.
3664 */
3665 if (s->be->ck_opts & PR_CK_PSV) {
3666 /* The "preserve" flag was set, we don't want to touch the
3667 * server's cookie.
3668 */
3669 }
3670 else if ((srv && (s->be->ck_opts & PR_CK_INS)) ||
3671 ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) {
3672 /* this cookie must be deleted */
3673 if (prev == hdr_beg && next == hdr_end) {
3674 /* whole header */
3675 http_remove_header(htx, &ctx);
3676 /* note: while both invalid now, <next> and <hdr_end>
3677 * are still equal, so the for() will stop as expected.
3678 */
3679 } else {
3680 /* just remove the value */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003681 int delta = http_del_hdr_value(hdr_beg, hdr_end, &prev, next);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003682 next = prev;
3683 hdr_end += delta;
3684 }
3685 txn->flags &= ~TX_SCK_MASK;
3686 txn->flags |= TX_SCK_DELETED;
3687 /* and go on with next cookie */
3688 }
3689 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) {
3690 /* replace bytes val_beg->val_end with the cookie name associated
3691 * with this server since we know it.
3692 */
3693 int sliding, delta;
3694
3695 ctx.value = ist2(val_beg, val_end - val_beg);
3696 ctx.lws_before = ctx.lws_after = 0;
3697 http_replace_header_value(htx, &ctx, ist2(srv->cookie, srv->cklen));
3698 delta = srv->cklen - (val_end - val_beg);
3699 sliding = (ctx.value.ptr - val_beg);
3700 hdr_beg += sliding;
3701 val_beg += sliding;
3702 next += sliding + delta;
3703 hdr_end += sliding + delta;
3704
3705 txn->flags &= ~TX_SCK_MASK;
3706 txn->flags |= TX_SCK_REPLACED;
3707 }
3708 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) {
3709 /* insert the cookie name associated with this server
3710 * before existing cookie, and insert a delimiter between them..
3711 */
3712 int sliding, delta;
3713 ctx.value = ist2(val_beg, 0);
3714 ctx.lws_before = ctx.lws_after = 0;
3715 http_replace_header_value(htx, &ctx, ist2(srv->cookie, srv->cklen + 1));
3716 delta = srv->cklen + 1;
3717 sliding = (ctx.value.ptr - val_beg);
3718 hdr_beg += sliding;
3719 val_beg += sliding;
3720 next += sliding + delta;
3721 hdr_end += sliding + delta;
3722
3723 val_beg[srv->cklen] = COOKIE_DELIM;
3724 txn->flags &= ~TX_SCK_MASK;
3725 txn->flags |= TX_SCK_REPLACED;
3726 }
3727 }
3728 /* that's done for this cookie, check the next one on the same
3729 * line when next != hdr_end (only if is_cookie2).
3730 */
3731 }
3732 }
3733}
3734
Christopher Faulet25a02f62018-10-24 12:00:25 +02003735/*
3736 * Parses the Cache-Control and Pragma request header fields to determine if
3737 * the request may be served from the cache and/or if it is cacheable. Updates
3738 * s->txn->flags.
3739 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003740void http_check_request_for_cacheability(struct stream *s, struct channel *req)
Christopher Faulet25a02f62018-10-24 12:00:25 +02003741{
3742 struct http_txn *txn = s->txn;
3743 struct htx *htx;
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003744 struct http_hdr_ctx ctx = { .blk = NULL };
3745 int pragma_found, cc_found;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003746
3747 if ((txn->flags & (TX_CACHEABLE|TX_CACHE_IGNORE)) == TX_CACHE_IGNORE)
3748 return; /* nothing more to do here */
3749
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003750 htx = htxbuf(&req->buf);
Christopher Faulet25a02f62018-10-24 12:00:25 +02003751 pragma_found = cc_found = 0;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003752
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003753 /* Check "pragma" header for HTTP/1.0 compatibility. */
3754 if (http_find_header(htx, ist("pragma"), &ctx, 1)) {
3755 if (isteqi(ctx.value, ist("no-cache"))) {
3756 pragma_found = 1;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003757 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003758 }
Christopher Faulet25a02f62018-10-24 12:00:25 +02003759
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003760 ctx.blk = NULL;
3761 /* Don't use the cache and don't try to store if we found the
3762 * Authorization header */
3763 if (http_find_header(htx, ist("authorization"), &ctx, 1)) {
3764 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3765 txn->flags |= TX_CACHE_IGNORE;
3766 }
Christopher Faulet25a02f62018-10-24 12:00:25 +02003767
Christopher Faulet25a02f62018-10-24 12:00:25 +02003768
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003769 /* Look for "cache-control" header and iterate over all the values
3770 * until we find one that specifies that caching is possible or not. */
3771 ctx.blk = NULL;
3772 while (http_find_header(htx, ist("cache-control"), &ctx, 0)) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003773 cc_found = 1;
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003774 /* We don't check the values after max-age, max-stale nor min-fresh,
3775 * we simply don't use the cache when they're specified. */
3776 if (istmatchi(ctx.value, ist("max-age")) ||
3777 istmatchi(ctx.value, ist("no-cache")) ||
3778 istmatchi(ctx.value, ist("max-stale")) ||
3779 istmatchi(ctx.value, ist("min-fresh"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003780 txn->flags |= TX_CACHE_IGNORE;
3781 continue;
3782 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003783 if (istmatchi(ctx.value, ist("no-store"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003784 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3785 continue;
3786 }
3787 }
3788
3789 /* RFC7234#5.4:
3790 * When the Cache-Control header field is also present and
3791 * understood in a request, Pragma is ignored.
3792 * When the Cache-Control header field is not present in a
3793 * request, caches MUST consider the no-cache request
3794 * pragma-directive as having the same effect as if
3795 * "Cache-Control: no-cache" were present.
3796 */
3797 if (!cc_found && pragma_found)
3798 txn->flags |= TX_CACHE_IGNORE;
3799}
3800
3801/*
3802 * Check if response is cacheable or not. Updates s->txn->flags.
3803 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003804void http_check_response_for_cacheability(struct stream *s, struct channel *res)
Christopher Faulet25a02f62018-10-24 12:00:25 +02003805{
3806 struct http_txn *txn = s->txn;
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003807 struct http_hdr_ctx ctx = { .blk = NULL };
Christopher Faulet25a02f62018-10-24 12:00:25 +02003808 struct htx *htx;
Remi Tricot-Le Bretoncc9bf2e2020-11-12 11:14:41 +01003809 int has_freshness_info = 0;
3810 int has_validator = 0;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003811
3812 if (txn->status < 200) {
3813 /* do not try to cache interim responses! */
3814 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3815 return;
3816 }
3817
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003818 htx = htxbuf(&res->buf);
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003819 /* Check "pragma" header for HTTP/1.0 compatibility. */
3820 if (http_find_header(htx, ist("pragma"), &ctx, 1)) {
3821 if (isteqi(ctx.value, ist("no-cache"))) {
3822 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3823 return;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003824 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003825 }
Christopher Faulet25a02f62018-10-24 12:00:25 +02003826
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003827 /* Look for "cache-control" header and iterate over all the values
3828 * until we find one that specifies that caching is possible or not. */
3829 ctx.blk = NULL;
3830 while (http_find_header(htx, ist("cache-control"), &ctx, 0)) {
3831 if (isteqi(ctx.value, ist("public"))) {
3832 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003833 continue;
3834 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003835 if (isteqi(ctx.value, ist("private")) ||
3836 isteqi(ctx.value, ist("no-cache")) ||
3837 isteqi(ctx.value, ist("no-store")) ||
3838 isteqi(ctx.value, ist("max-age=0")) ||
3839 isteqi(ctx.value, ist("s-maxage=0"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003840 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003841 continue;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003842 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003843 /* We might have a no-cache="set-cookie" form. */
3844 if (istmatchi(ctx.value, ist("no-cache=\"set-cookie"))) {
3845 txn->flags &= ~TX_CACHE_COOK;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003846 continue;
3847 }
Remi Tricot-Le Bretoncc9bf2e2020-11-12 11:14:41 +01003848
3849 if (istmatchi(ctx.value, ist("s-maxage")) ||
3850 istmatchi(ctx.value, ist("max-age"))) {
3851 has_freshness_info = 1;
3852 continue;
3853 }
3854 }
3855
3856 /* If no freshness information could be found in Cache-Control values,
3857 * look for an Expires header. */
3858 if (!has_freshness_info) {
3859 ctx.blk = NULL;
3860 has_freshness_info = http_find_header(htx, ist("expires"), &ctx, 0);
Christopher Faulet25a02f62018-10-24 12:00:25 +02003861 }
Remi Tricot-Le Bretoncc9bf2e2020-11-12 11:14:41 +01003862
3863 /* If no freshness information could be found in Cache-Control or Expires
3864 * values, look for an explicit validator. */
3865 if (!has_freshness_info) {
3866 ctx.blk = NULL;
3867 has_validator = 1;
3868 if (!http_find_header(htx, ist("etag"), &ctx, 0)) {
3869 ctx.blk = NULL;
3870 if (!http_find_header(htx, ist("last-modified"), &ctx, 0))
3871 has_validator = 0;
3872 }
3873 }
3874
3875 /* We won't store an entry that has neither a cache validator nor an
3876 * explicit expiration time, as suggested in RFC 7234#3. */
3877 if (!has_freshness_info && !has_validator)
3878 txn->flags |= TX_CACHE_IGNORE;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003879}
3880
Christopher Faulet377c5a52018-10-24 21:21:30 +02003881/*
3882 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
3883 * for the current backend.
3884 *
3885 * It is assumed that the request is either a HEAD, GET, or POST and that the
3886 * uri_auth field is valid.
3887 *
3888 * Returns 1 if stats should be provided, otherwise 0.
3889 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003890static int http_stats_check_uri(struct stream *s, struct http_txn *txn, struct proxy *backend)
Christopher Faulet377c5a52018-10-24 21:21:30 +02003891{
3892 struct uri_auth *uri_auth = backend->uri_auth;
3893 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003894 struct htx_sl *sl;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003895 struct ist uri;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003896
3897 if (!uri_auth)
3898 return 0;
3899
3900 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
3901 return 0;
3902
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003903 htx = htxbuf(&s->req.buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02003904 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003905 uri = htx_sl_req_uri(sl);
Willy Tarreau1eb3b482019-10-31 15:50:28 +01003906 if (*uri_auth->uri_prefix == '/')
3907 uri = http_get_path(uri);
Christopher Faulet377c5a52018-10-24 21:21:30 +02003908
3909 /* check URI size */
3910 if (uri_auth->uri_len > uri.len)
3911 return 0;
3912
3913 if (memcmp(uri.ptr, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
3914 return 0;
3915
3916 return 1;
3917}
3918
3919/* This function prepares an applet to handle the stats. It can deal with the
3920 * "100-continue" expectation, check that admin rules are met for POST requests,
3921 * and program a response message if something was unexpected. It cannot fail
3922 * and always relies on the stats applet to complete the job. It does not touch
3923 * analysers nor counters, which are left to the caller. It does not touch
3924 * s->target which is supposed to already point to the stats applet. The caller
3925 * is expected to have already assigned an appctx to the stream.
3926 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003927static int http_handle_stats(struct stream *s, struct channel *req)
Christopher Faulet377c5a52018-10-24 21:21:30 +02003928{
3929 struct stats_admin_rule *stats_admin_rule;
3930 struct stream_interface *si = &s->si[1];
3931 struct session *sess = s->sess;
3932 struct http_txn *txn = s->txn;
3933 struct http_msg *msg = &txn->req;
3934 struct uri_auth *uri_auth = s->be->uri_auth;
3935 const char *h, *lookup, *end;
3936 struct appctx *appctx;
3937 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003938 struct htx_sl *sl;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003939
3940 appctx = si_appctx(si);
3941 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
3942 appctx->st1 = appctx->st2 = 0;
3943 appctx->ctx.stats.st_code = STAT_STATUS_INIT;
Willy Tarreau676c29e2019-10-09 10:50:01 +02003944 appctx->ctx.stats.flags |= uri_auth->flags;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003945 appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */
3946 if ((msg->flags & HTTP_MSGF_VER_11) && (txn->meth != HTTP_METH_HEAD))
3947 appctx->ctx.stats.flags |= STAT_CHUNKED;
3948
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003949 htx = htxbuf(&req->buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02003950 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003951 lookup = HTX_SL_REQ_UPTR(sl) + uri_auth->uri_len;
3952 end = HTX_SL_REQ_UPTR(sl) + HTX_SL_REQ_ULEN(sl);
Christopher Faulet377c5a52018-10-24 21:21:30 +02003953
3954 for (h = lookup; h <= end - 3; h++) {
3955 if (memcmp(h, ";up", 3) == 0) {
3956 appctx->ctx.stats.flags |= STAT_HIDE_DOWN;
3957 break;
3958 }
Amaury Denoyelle91e55ea2021-02-25 14:46:08 +01003959 }
3960
3961 for (h = lookup; h <= end - 9; h++) {
3962 if (memcmp(h, ";no-maint", 9) == 0) {
Willy Tarreau3e320362020-10-23 17:28:57 +02003963 appctx->ctx.stats.flags |= STAT_HIDE_MAINT;
3964 break;
3965 }
Christopher Faulet377c5a52018-10-24 21:21:30 +02003966 }
3967
3968 if (uri_auth->refresh) {
3969 for (h = lookup; h <= end - 10; h++) {
3970 if (memcmp(h, ";norefresh", 10) == 0) {
3971 appctx->ctx.stats.flags |= STAT_NO_REFRESH;
3972 break;
3973 }
3974 }
3975 }
3976
3977 for (h = lookup; h <= end - 4; h++) {
3978 if (memcmp(h, ";csv", 4) == 0) {
Christopher Faulet6338a082019-09-09 15:50:54 +02003979 appctx->ctx.stats.flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
Christopher Faulet377c5a52018-10-24 21:21:30 +02003980 break;
3981 }
3982 }
3983
3984 for (h = lookup; h <= end - 6; h++) {
3985 if (memcmp(h, ";typed", 6) == 0) {
Christopher Faulet6338a082019-09-09 15:50:54 +02003986 appctx->ctx.stats.flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
Christopher Faulet377c5a52018-10-24 21:21:30 +02003987 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
3988 break;
3989 }
3990 }
3991
Christopher Faulet6338a082019-09-09 15:50:54 +02003992 for (h = lookup; h <= end - 5; h++) {
3993 if (memcmp(h, ";json", 5) == 0) {
3994 appctx->ctx.stats.flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
3995 appctx->ctx.stats.flags |= STAT_FMT_JSON;
3996 break;
3997 }
3998 }
3999
4000 for (h = lookup; h <= end - 12; h++) {
4001 if (memcmp(h, ";json-schema", 12) == 0) {
4002 appctx->ctx.stats.flags &= ~STAT_FMT_MASK;
4003 appctx->ctx.stats.flags |= STAT_JSON_SCHM;
4004 break;
4005 }
4006 }
4007
Christopher Faulet377c5a52018-10-24 21:21:30 +02004008 for (h = lookup; h <= end - 8; h++) {
4009 if (memcmp(h, ";st=", 4) == 0) {
4010 int i;
4011 h += 4;
4012 appctx->ctx.stats.st_code = STAT_STATUS_UNKN;
4013 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
4014 if (strncmp(stat_status_codes[i], h, 4) == 0) {
4015 appctx->ctx.stats.st_code = i;
4016 break;
4017 }
4018 }
4019 break;
4020 }
4021 }
4022
4023 appctx->ctx.stats.scope_str = 0;
4024 appctx->ctx.stats.scope_len = 0;
4025 for (h = lookup; h <= end - 8; h++) {
4026 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
4027 int itx = 0;
4028 const char *h2;
4029 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
4030 const char *err;
4031
4032 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
4033 h2 = h;
Christopher Fauleted7a0662019-01-14 11:07:34 +01004034 appctx->ctx.stats.scope_str = h2 - HTX_SL_REQ_UPTR(sl);
4035 while (h < end) {
Christopher Faulet377c5a52018-10-24 21:21:30 +02004036 if (*h == ';' || *h == '&' || *h == ' ')
4037 break;
4038 itx++;
4039 h++;
4040 }
4041
4042 if (itx > STAT_SCOPE_TXT_MAXLEN)
4043 itx = STAT_SCOPE_TXT_MAXLEN;
4044 appctx->ctx.stats.scope_len = itx;
4045
4046 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
4047 memcpy(scope_txt, h2, itx);
4048 scope_txt[itx] = '\0';
4049 err = invalid_char(scope_txt);
4050 if (err) {
4051 /* bad char in search text => clear scope */
4052 appctx->ctx.stats.scope_str = 0;
4053 appctx->ctx.stats.scope_len = 0;
4054 }
4055 break;
4056 }
4057 }
4058
4059 /* now check whether we have some admin rules for this request */
4060 list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) {
4061 int ret = 1;
4062
4063 if (stats_admin_rule->cond) {
4064 ret = acl_exec_cond(stats_admin_rule->cond, s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
4065 ret = acl_pass(ret);
4066 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
4067 ret = !ret;
4068 }
4069
4070 if (ret) {
4071 /* no rule, or the rule matches */
4072 appctx->ctx.stats.flags |= STAT_ADMIN;
4073 break;
4074 }
4075 }
4076
Christopher Faulet5d45e382019-02-27 15:15:23 +01004077 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
4078 appctx->st0 = STAT_HTTP_HEAD;
4079 else if (txn->meth == HTTP_METH_POST) {
Christopher Fauletbd9e8422019-08-15 22:26:48 +02004080 if (appctx->ctx.stats.flags & STAT_ADMIN) {
Christopher Faulet377c5a52018-10-24 21:21:30 +02004081 appctx->st0 = STAT_HTTP_POST;
Christopher Fauletbd9e8422019-08-15 22:26:48 +02004082 if (msg->msg_state < HTTP_MSG_DATA)
4083 req->analysers |= AN_REQ_HTTP_BODY;
4084 }
Christopher Faulet377c5a52018-10-24 21:21:30 +02004085 else {
Christopher Faulet5d45e382019-02-27 15:15:23 +01004086 /* POST without admin level */
Christopher Faulet377c5a52018-10-24 21:21:30 +02004087 appctx->ctx.stats.flags &= ~STAT_CHUNKED;
4088 appctx->ctx.stats.st_code = STAT_STATUS_DENY;
4089 appctx->st0 = STAT_HTTP_LAST;
4090 }
4091 }
4092 else {
Christopher Faulet5d45e382019-02-27 15:15:23 +01004093 /* Unsupported method */
4094 appctx->ctx.stats.flags &= ~STAT_CHUNKED;
4095 appctx->ctx.stats.st_code = STAT_STATUS_IVAL;
4096 appctx->st0 = STAT_HTTP_LAST;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004097 }
4098
4099 s->task->nice = -32; /* small boost for HTTP statistics */
4100 return 1;
4101}
4102
Christopher Faulet021a8e42021-03-29 10:46:38 +02004103/* This function waits for the message payload at most <time> milliseconds (may
4104 * be set to TICK_ETERNITY). It stops to wait if at least <bytes> bytes of the
4105 * payload are received (0 means no limit). It returns HTTP_RULE_* depending on
4106 * the result:
4107 *
4108 * - HTTP_RULE_RES_CONT when conditions are met to stop waiting
4109 * - HTTP_RULE_RES_YIELD to wait for more data
4110 * - HTTP_RULE_RES_ABRT when a timeout occured.
4111 * - HTTP_RULE_RES_BADREQ if a parsing error is raised by lower level
4112 * - HTTP_RULE_RES_ERROR if an internal error occured
4113 *
4114 * If a timeout occured, this function is responsible to emit the right response
4115 * to the client, depending on the channel (408 on request side, 504 on response
4116 * side). All other errors must be handled by the caller.
4117 */
4118enum rule_result http_wait_for_msg_body(struct stream *s, struct channel *chn,
4119 unsigned int time, unsigned int bytes)
4120{
4121 struct session *sess = s->sess;
4122 struct http_txn *txn = s->txn;
4123 struct http_msg *msg = ((chn->flags & CF_ISRESP) ? &txn->rsp : &txn->req);
4124 struct htx *htx;
4125 enum rule_result ret = HTTP_RULE_RES_CONT;
4126
4127 htx = htxbuf(&chn->buf);
4128
4129 if (htx->flags & HTX_FL_PARSING_ERROR) {
4130 ret = HTTP_RULE_RES_BADREQ;
4131 goto end;
4132 }
4133 if (htx->flags & HTX_FL_PROCESSING_ERROR) {
4134 ret = HTTP_RULE_RES_ERROR;
4135 goto end;
4136 }
4137
4138 /* Do nothing for bodyless and CONNECT requests */
4139 if (txn->meth == HTTP_METH_CONNECT || (msg->flags & HTTP_MSGF_BODYLESS))
4140 goto end;
4141
4142 if (!(chn->flags & CF_ISRESP) && msg->msg_state < HTTP_MSG_DATA) {
4143 if (http_handle_expect_hdr(s, htx, msg) == -1) {
4144 ret = HTTP_RULE_RES_ERROR;
4145 goto end;
4146 }
4147 }
4148
4149 msg->msg_state = HTTP_MSG_DATA;
4150
4151 /* Now we're in HTTP_MSG_DATA. We just need to know if all data have
4152 * been received or if the buffer is full.
4153 */
4154 if ((htx->flags & HTX_FL_EOM) || htx_get_tail_type(htx) > HTX_BLK_DATA ||
4155 channel_htx_full(chn, htx, global.tune.maxrewrite))
4156 goto end;
4157
4158 if (bytes) {
4159 struct htx_blk *blk;
4160 unsigned int len = 0;
4161
4162 for (blk = htx_get_first_blk(htx); blk; blk = htx_get_next_blk(htx, blk)) {
4163 if (htx_get_blk_type(blk) != HTX_BLK_DATA)
4164 continue;
4165 len += htx_get_blksz(blk);
4166 if (len >= bytes)
4167 goto end;
4168 }
4169 }
4170
4171 if ((chn->flags & CF_READ_TIMEOUT) || tick_is_expired(chn->analyse_exp, now_ms)) {
4172 if (!(chn->flags & CF_ISRESP))
4173 goto abort_req;
4174 goto abort_res;
4175 }
4176
4177 /* we get here if we need to wait for more data */
4178 if (!(chn->flags & (CF_SHUTR | CF_READ_ERROR))) {
4179 if (!tick_isset(chn->analyse_exp))
4180 chn->analyse_exp = tick_add_ifset(now_ms, time);
4181 ret = HTTP_RULE_RES_YIELD;
4182 }
4183
4184 end:
4185 return ret;
4186
4187 abort_req:
4188 txn->status = 408;
4189 if (!(s->flags & SF_ERR_MASK))
4190 s->flags |= SF_ERR_CLITO;
4191 if (!(s->flags & SF_FINST_MASK))
4192 s->flags |= SF_FINST_D;
4193 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
4194 if (sess->listener && sess->listener->counters)
4195 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
4196 http_reply_and_close(s, txn->status, http_error_message(s));
4197 ret = HTTP_RULE_RES_ABRT;
4198 goto end;
4199
4200 abort_res:
4201 txn->status = 504;
4202 if (!(s->flags & SF_ERR_MASK))
4203 s->flags |= SF_ERR_SRVTO;
4204 if (!(s->flags & SF_FINST_MASK))
4205 s->flags |= SF_FINST_D;
4206 stream_inc_http_fail_ctr(s);
4207 http_reply_and_close(s, txn->status, http_error_message(s));
4208 ret = HTTP_RULE_RES_ABRT;
4209 goto end;
4210}
4211
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004212void http_perform_server_redirect(struct stream *s, struct stream_interface *si)
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004213{
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004214 struct channel *req = &s->req;
4215 struct channel *res = &s->res;
4216 struct server *srv;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004217 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004218 struct htx_sl *sl;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004219 struct ist path, location;
4220 unsigned int flags;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004221
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004222 /*
4223 * Create the location
4224 */
4225 chunk_reset(&trash);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004226
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004227 /* 1: add the server's prefix */
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004228 /* special prefix "/" means don't change URL */
4229 srv = __objt_server(s->target);
4230 if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') {
4231 if (!chunk_memcat(&trash, srv->rdr_pfx, srv->rdr_len))
4232 return;
4233 }
4234
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004235 /* 2: add the request Path */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004236 htx = htxbuf(&req->buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02004237 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004238 path = http_get_path(htx_sl_req_uri(sl));
Tim Duesterhused526372020-03-05 17:56:33 +01004239 if (!isttest(path))
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004240 return;
4241
4242 if (!chunk_memcat(&trash, path.ptr, path.len))
4243 return;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004244 location = ist2(trash.area, trash.data);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004245
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004246 /*
4247 * Create the 302 respone
4248 */
4249 htx = htx_from_buf(&res->buf);
4250 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
4251 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
4252 ist("HTTP/1.1"), ist("302"), ist("Found"));
4253 if (!sl)
4254 goto fail;
4255 sl->info.res.status = 302;
4256 s->txn->status = 302;
4257
4258 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) ||
4259 !htx_add_header(htx, ist("Connection"), ist("close")) ||
4260 !htx_add_header(htx, ist("Content-length"), ist("0")) ||
4261 !htx_add_header(htx, ist("Location"), location))
4262 goto fail;
4263
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004264 if (!htx_add_endof(htx, HTX_BLK_EOH))
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004265 goto fail;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004266
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004267 htx->flags |= HTX_FL_EOM;
Christopher Fauletc20afb82020-01-24 19:16:26 +01004268 htx_to_buf(htx, &res->buf);
Christopher Fauleta72a7e42020-01-28 09:28:11 +01004269 if (!http_forward_proxy_resp(s, 1))
4270 goto fail;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004271
4272 /* return without error. */
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004273 si_shutr(si);
4274 si_shutw(si);
4275 si->err_type = SI_ET_NONE;
4276 si->state = SI_ST_CLO;
4277
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004278 if (!(s->flags & SF_ERR_MASK))
4279 s->flags |= SF_ERR_LOCAL;
4280 if (!(s->flags & SF_FINST_MASK))
4281 s->flags |= SF_FINST_C;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004282
4283 /* FIXME: we should increase a counter of redirects per server and per backend. */
4284 srv_inc_sess_ctr(srv);
4285 srv_set_sess_last(srv);
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004286 return;
4287
4288 fail:
4289 /* If an error occurred, remove the incomplete HTTP response from the
4290 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004291 channel_htx_truncate(res, htx);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004292}
4293
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05004294/* This function terminates the request because it was completely analyzed or
Christopher Fauletf2824e62018-10-01 12:12:37 +02004295 * because an error was triggered during the body forwarding.
4296 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004297static void http_end_request(struct stream *s)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004298{
4299 struct channel *chn = &s->req;
4300 struct http_txn *txn = s->txn;
4301
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004302 DBG_TRACE_ENTER(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004303
Christopher Fauletb42a8b62018-11-19 21:59:00 +01004304 if (unlikely(txn->req.msg_state == HTTP_MSG_ERROR ||
4305 txn->rsp.msg_state == HTTP_MSG_ERROR)) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004306 channel_abort(chn);
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004307 channel_htx_truncate(chn, htxbuf(&chn->buf));
Christopher Fauletf2824e62018-10-01 12:12:37 +02004308 goto end;
4309 }
4310
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004311 if (unlikely(txn->req.msg_state < HTTP_MSG_DONE)) {
4312 DBG_TRACE_DEVEL("waiting end of the request", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004313 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004314 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004315
4316 if (txn->req.msg_state == HTTP_MSG_DONE) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004317 /* No need to read anymore, the request was completely parsed.
4318 * We can shut the read side unless we want to abort_on_close,
4319 * or we have a POST request. The issue with POST requests is
4320 * that some browsers still send a CRLF after the request, and
4321 * this CRLF must be read so that it does not remain in the kernel
4322 * buffers, otherwise a close could cause an RST on some systems
4323 * (eg: Linux).
4324 */
Christopher Faulet769d0e92019-03-22 14:23:18 +01004325 if (!(s->be->options & PR_O_ABRT_CLOSE) && txn->meth != HTTP_METH_POST)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004326 channel_dont_read(chn);
4327
4328 /* if the server closes the connection, we want to immediately react
4329 * and close the socket to save packets and syscalls.
4330 */
4331 s->si[1].flags |= SI_FL_NOHALF;
4332
4333 /* In any case we've finished parsing the request so we must
4334 * disable Nagle when sending data because 1) we're not going
4335 * to shut this side, and 2) the server is waiting for us to
4336 * send pending data.
4337 */
4338 chn->flags |= CF_NEVER_WAIT;
4339
Christopher Fauletd01ce402019-01-02 17:44:13 +01004340 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
4341 /* The server has not finished to respond, so we
4342 * don't want to move in order not to upset it.
4343 */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004344 DBG_TRACE_DEVEL("waiting end of the response", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletd01ce402019-01-02 17:44:13 +01004345 return;
4346 }
4347
Christopher Fauletf2824e62018-10-01 12:12:37 +02004348 /* When we get here, it means that both the request and the
4349 * response have finished receiving. Depending on the connection
4350 * mode, we'll have to wait for the last bytes to leave in either
4351 * direction, and sometimes for a close to be effective.
4352 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02004353 if (txn->flags & TX_CON_WANT_TUN) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004354 /* Tunnel mode will not have any analyser so it needs to
4355 * poll for reads.
4356 */
4357 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004358 if (b_data(&chn->buf)) {
4359 DBG_TRACE_DEVEL("waiting to flush the request", STRM_EV_HTTP_ANA, s, txn);
Christopher Faulet9768c262018-10-22 09:34:31 +02004360 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004361 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004362 txn->req.msg_state = HTTP_MSG_TUNNEL;
4363 }
4364 else {
4365 /* we're not expecting any new data to come for this
4366 * transaction, so we can close it.
Christopher Faulet9768c262018-10-22 09:34:31 +02004367 *
4368 * However, there is an exception if the response
4369 * length is undefined. In this case, we need to wait
4370 * the close from the server. The response will be
4371 * switched in TUNNEL mode until the end.
Christopher Fauletf2824e62018-10-01 12:12:37 +02004372 */
4373 if (!(txn->rsp.flags & HTTP_MSGF_XFER_LEN) &&
4374 txn->rsp.msg_state != HTTP_MSG_CLOSED)
Christopher Faulet9768c262018-10-22 09:34:31 +02004375 goto check_channel_flags;
Christopher Fauletf2824e62018-10-01 12:12:37 +02004376
4377 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4378 channel_shutr_now(chn);
4379 channel_shutw_now(chn);
4380 }
4381 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004382 goto check_channel_flags;
4383 }
4384
4385 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
4386 http_msg_closing:
4387 /* nothing else to forward, just waiting for the output buffer
4388 * to be empty and for the shutw_now to take effect.
4389 */
4390 if (channel_is_empty(chn)) {
4391 txn->req.msg_state = HTTP_MSG_CLOSED;
4392 goto http_msg_closed;
4393 }
4394 else if (chn->flags & CF_SHUTW) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004395 txn->req.msg_state = HTTP_MSG_ERROR;
4396 goto end;
4397 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004398 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004399 return;
4400 }
4401
4402 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
4403 http_msg_closed:
Christopher Fauletf2824e62018-10-01 12:12:37 +02004404 /* if we don't know whether the server will close, we need to hard close */
4405 if (txn->rsp.flags & HTTP_MSGF_XFER_LEN)
4406 s->si[1].flags |= SI_FL_NOLINGER; /* we want to close ASAP */
Christopher Fauletf2824e62018-10-01 12:12:37 +02004407 /* see above in MSG_DONE why we only do this in these states */
Christopher Faulet769d0e92019-03-22 14:23:18 +01004408 if (!(s->be->options & PR_O_ABRT_CLOSE))
Christopher Fauletf2824e62018-10-01 12:12:37 +02004409 channel_dont_read(chn);
4410 goto end;
4411 }
4412
4413 check_channel_flags:
4414 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4415 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4416 /* if we've just closed an output, let's switch */
4417 txn->req.msg_state = HTTP_MSG_CLOSING;
4418 goto http_msg_closing;
4419 }
4420
4421 end:
4422 chn->analysers &= AN_REQ_FLT_END;
Christopher Faulet198ef8b2020-12-15 13:32:55 +01004423 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
4424 chn->flags |= CF_NEVER_WAIT;
4425 if (HAS_REQ_DATA_FILTERS(s))
Christopher Fauletf2824e62018-10-01 12:12:37 +02004426 chn->analysers |= AN_REQ_FLT_XFER_DATA;
Christopher Faulet198ef8b2020-12-15 13:32:55 +01004427 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004428 channel_auto_close(chn);
4429 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004430 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004431}
4432
4433
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05004434/* This function terminates the response because it was completely analyzed or
Christopher Fauletf2824e62018-10-01 12:12:37 +02004435 * because an error was triggered during the body forwarding.
4436 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004437static void http_end_response(struct stream *s)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004438{
4439 struct channel *chn = &s->res;
4440 struct http_txn *txn = s->txn;
4441
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004442 DBG_TRACE_ENTER(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004443
Christopher Fauletb42a8b62018-11-19 21:59:00 +01004444 if (unlikely(txn->req.msg_state == HTTP_MSG_ERROR ||
4445 txn->rsp.msg_state == HTTP_MSG_ERROR)) {
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004446 channel_htx_truncate(&s->req, htxbuf(&s->req.buf));
Christopher Faulet9768c262018-10-22 09:34:31 +02004447 channel_abort(&s->req);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004448 goto end;
4449 }
4450
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004451 if (unlikely(txn->rsp.msg_state < HTTP_MSG_DONE)) {
4452 DBG_TRACE_DEVEL("waiting end of the response", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004453 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004454 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004455
4456 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
4457 /* In theory, we don't need to read anymore, but we must
4458 * still monitor the server connection for a possible close
4459 * while the request is being uploaded, so we don't disable
4460 * reading.
4461 */
4462 /* channel_dont_read(chn); */
4463
4464 if (txn->req.msg_state < HTTP_MSG_DONE) {
4465 /* The client seems to still be sending data, probably
4466 * because we got an error response during an upload.
4467 * We have the choice of either breaking the connection
4468 * or letting it pass through. Let's do the later.
4469 */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004470 DBG_TRACE_DEVEL("waiting end of the request", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004471 return;
4472 }
4473
4474 /* When we get here, it means that both the request and the
4475 * response have finished receiving. Depending on the connection
4476 * mode, we'll have to wait for the last bytes to leave in either
4477 * direction, and sometimes for a close to be effective.
4478 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02004479 if (txn->flags & TX_CON_WANT_TUN) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004480 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004481 if (b_data(&chn->buf)) {
4482 DBG_TRACE_DEVEL("waiting to flush the respone", STRM_EV_HTTP_ANA, s, txn);
Christopher Faulet9768c262018-10-22 09:34:31 +02004483 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004484 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004485 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
4486 }
4487 else {
4488 /* we're not expecting any new data to come for this
4489 * transaction, so we can close it.
4490 */
4491 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4492 channel_shutr_now(chn);
4493 channel_shutw_now(chn);
4494 }
4495 }
4496 goto check_channel_flags;
4497 }
4498
4499 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
4500 http_msg_closing:
4501 /* nothing else to forward, just waiting for the output buffer
4502 * to be empty and for the shutw_now to take effect.
4503 */
4504 if (channel_is_empty(chn)) {
4505 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4506 goto http_msg_closed;
4507 }
4508 else if (chn->flags & CF_SHUTW) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004509 txn->rsp.msg_state = HTTP_MSG_ERROR;
Christopher Fauletcff0f732019-12-16 16:13:44 +01004510 _HA_ATOMIC_ADD(&strm_sess(s)->fe->fe_counters.cli_aborts, 1);
Olivier Houcharda798bf52019-03-08 18:52:00 +01004511 _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
William Lallemand36119de2021-03-08 15:26:48 +01004512 if (strm_sess(s)->listener && strm_sess(s)->listener->counters)
Christopher Fauletcff0f732019-12-16 16:13:44 +01004513 _HA_ATOMIC_ADD(&strm_sess(s)->listener->counters->cli_aborts, 1);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004514 if (objt_server(s->target))
Christopher Fauletcff0f732019-12-16 16:13:44 +01004515 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.cli_aborts, 1);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004516 goto end;
4517 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004518 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004519 return;
4520 }
4521
4522 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
4523 http_msg_closed:
4524 /* drop any pending data */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004525 channel_htx_truncate(&s->req, htxbuf(&s->req.buf));
Christopher Faulet9768c262018-10-22 09:34:31 +02004526 channel_abort(&s->req);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004527 goto end;
4528 }
4529
4530 check_channel_flags:
4531 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4532 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4533 /* if we've just closed an output, let's switch */
4534 txn->rsp.msg_state = HTTP_MSG_CLOSING;
4535 goto http_msg_closing;
4536 }
4537
4538 end:
4539 chn->analysers &= AN_RES_FLT_END;
Christopher Faulet198ef8b2020-12-15 13:32:55 +01004540 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
4541 chn->flags |= CF_NEVER_WAIT;
4542 if (HAS_RSP_DATA_FILTERS(s))
4543 chn->analysers |= AN_RES_FLT_XFER_DATA;
4544 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004545 channel_auto_close(chn);
4546 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004547 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004548}
4549
Christopher Fauletef70e252020-01-28 09:26:19 +01004550/* Forward a response generated by HAProxy (error/redirect/return). This
4551 * function forwards all pending incoming data. If <final> is set to 0, nothing
4552 * more is performed. It is used for 1xx informational messages. Otherwise, the
Christopher Faulet507479b2020-05-15 12:29:46 +02004553 * transaction is terminated and the request is emptied. On success 1 is
Christopher Faulet40e6b552020-06-25 16:04:50 +02004554 * returned. If an error occurred, 0 is returned. If it fails, this function
4555 * only exits. It is the caller responsibility to do the cleanup.
Christopher Fauletef70e252020-01-28 09:26:19 +01004556 */
4557int http_forward_proxy_resp(struct stream *s, int final)
4558{
4559 struct channel *req = &s->req;
4560 struct channel *res = &s->res;
4561 struct htx *htx = htxbuf(&res->buf);
4562 size_t data;
4563
4564 if (final) {
4565 htx->flags |= HTX_FL_PROXY_RESP;
Christopher Faulet507479b2020-05-15 12:29:46 +02004566
Christopher Fauletaab1b672020-11-18 16:44:02 +01004567 if (!htx_is_empty(htx) && !http_eval_after_res_rules(s))
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01004568 return 0;
Christopher Fauletef70e252020-01-28 09:26:19 +01004569
Christopher Fauletd6c48362020-10-19 18:01:38 +02004570 if (s->txn->meth == HTTP_METH_HEAD)
4571 htx_skip_msg_payload(htx);
4572
Christopher Fauletef70e252020-01-28 09:26:19 +01004573 channel_auto_read(req);
4574 channel_abort(req);
4575 channel_auto_close(req);
4576 channel_htx_erase(req, htxbuf(&req->buf));
4577
4578 res->wex = tick_add_ifset(now_ms, res->wto);
4579 channel_auto_read(res);
4580 channel_auto_close(res);
4581 channel_shutr_now(res);
Christopher Faulet1a9db7c2020-06-25 15:36:45 +02004582 res->flags |= CF_EOI; /* The response is terminated, add EOI */
Christopher Faulet42432f32020-11-20 17:43:16 +01004583 htxbuf(&res->buf)->flags |= HTX_FL_EOM; /* no more data are expected */
Christopher Fauletef70e252020-01-28 09:26:19 +01004584 }
Christopher Fauletcf6898c2020-06-25 15:55:11 +02004585 else {
4586 /* Send ASAP informational messages. Rely on CF_EOI for final
4587 * response.
4588 */
4589 res->flags |= CF_SEND_DONTWAIT;
4590 }
Christopher Fauletef70e252020-01-28 09:26:19 +01004591
4592 data = htx->data - co_data(res);
4593 c_adv(res, data);
4594 htx->first = -1;
4595 res->total += data;
4596 return 1;
4597}
4598
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004599void http_server_error(struct stream *s, struct stream_interface *si, int err,
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004600 int finst, struct http_reply *msg)
Christopher Faulet0f226952018-10-22 09:29:56 +02004601{
Christopher Faulet72c7d8d2020-01-27 15:32:25 +01004602 http_reply_and_close(s, s->txn->status, msg);
Christopher Faulet0f226952018-10-22 09:29:56 +02004603 if (!(s->flags & SF_ERR_MASK))
4604 s->flags |= err;
4605 if (!(s->flags & SF_FINST_MASK))
4606 s->flags |= finst;
4607}
4608
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004609void http_reply_and_close(struct stream *s, short status, struct http_reply *msg)
Christopher Faulet0f226952018-10-22 09:29:56 +02004610{
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004611 if (!msg) {
4612 channel_htx_truncate(&s->res, htxbuf(&s->res.buf));
4613 goto end;
4614 }
4615
4616 if (http_reply_message(s, msg) == -1) {
4617 /* On error, return a 500 error message, but don't rewrite it if
Christopher Faulet40e6b552020-06-25 16:04:50 +02004618 * it is already an internal error. If it was already a "const"
4619 * 500 error, just fail.
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004620 */
Christopher Faulet40e6b552020-06-25 16:04:50 +02004621 if (s->txn->status == 500) {
4622 if (s->txn->flags & TX_CONST_REPLY)
4623 goto end;
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004624 s->txn->flags |= TX_CONST_REPLY;
Christopher Faulet40e6b552020-06-25 16:04:50 +02004625 }
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004626 s->txn->status = 500;
4627 s->txn->http_reply = NULL;
4628 return http_reply_and_close(s, s->txn->status, http_error_message(s));
4629 }
4630
4631end:
4632 s->res.wex = tick_add_ifset(now_ms, s->res.wto);
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004633
Christopher Faulet0f226952018-10-22 09:29:56 +02004634 channel_auto_read(&s->req);
4635 channel_abort(&s->req);
4636 channel_auto_close(&s->req);
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004637 channel_htx_erase(&s->req, htxbuf(&s->req.buf));
Christopher Faulet72c7d8d2020-01-27 15:32:25 +01004638 channel_auto_read(&s->res);
4639 channel_auto_close(&s->res);
4640 channel_shutr_now(&s->res);
Christopher Faulet0f226952018-10-22 09:29:56 +02004641}
4642
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004643struct http_reply *http_error_message(struct stream *s)
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004644{
4645 const int msgnum = http_get_status_idx(s->txn->status);
4646
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004647 if (s->txn->http_reply)
4648 return s->txn->http_reply;
4649 else if (s->be->replies[msgnum])
4650 return s->be->replies[msgnum];
4651 else if (strm_fe(s)->replies[msgnum])
4652 return strm_fe(s)->replies[msgnum];
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004653 else
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004654 return &http_err_replies[msgnum];
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004655}
4656
Christopher Faulet40e6b552020-06-25 16:04:50 +02004657/* Produces an HTX message from an http reply. Depending on the http reply type,
4658 * a, errorfile, an raw file or a log-format string is used. On success, it
4659 * returns 0. If an error occurs -1 is returned. If it fails, this function only
4660 * exits. It is the caller responsibility to do the cleanup.
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004661 */
Christopher Fauletae43b6c2020-05-27 15:24:22 +02004662int http_reply_to_htx(struct stream *s, struct htx *htx, struct http_reply *reply)
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004663{
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004664 struct buffer *errmsg;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004665 struct htx_sl *sl;
4666 struct buffer *body = NULL;
4667 const char *status, *reason, *clen, *ctype;
4668 unsigned int slflags;
4669 int ret = 0;
4670
Christopher Faulete29a97e2020-05-14 14:49:25 +02004671 /*
4672 * - HTTP_REPLY_ERRFILES unexpected here. handled as no payload if so
4673 *
4674 * - HTTP_REPLY_INDIRECT: switch on another reply if defined or handled
4675 * as no payload if NULL. the TXN status code is set with the status
4676 * of the original reply.
4677 */
4678
4679 if (reply->type == HTTP_REPLY_INDIRECT) {
4680 if (reply->body.reply)
4681 reply = reply->body.reply;
4682 }
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004683 if (reply->type == HTTP_REPLY_ERRMSG && !reply->body.errmsg) {
4684 /* get default error message */
4685 if (reply == s->txn->http_reply)
4686 s->txn->http_reply = NULL;
4687 reply = http_error_message(s);
4688 if (reply->type == HTTP_REPLY_INDIRECT) {
4689 if (reply->body.reply)
4690 reply = reply->body.reply;
4691 }
4692 }
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004693
4694 if (reply->type == HTTP_REPLY_ERRMSG) {
4695 /* implicit or explicit error message*/
4696 errmsg = reply->body.errmsg;
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004697 if (errmsg && !b_is_null(errmsg)) {
Christopher Faulet20567362020-05-15 14:52:49 +02004698 if (!htx_copy_msg(htx, errmsg))
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004699 goto fail;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004700 }
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004701 }
4702 else {
4703 /* no payload, file or log-format string */
4704 if (reply->type == HTTP_REPLY_RAW) {
4705 /* file */
4706 body = &reply->body.obj;
4707 }
4708 else if (reply->type == HTTP_REPLY_LOGFMT) {
4709 /* log-format string */
4710 body = alloc_trash_chunk();
4711 if (!body)
4712 goto fail_alloc;
4713 body->data = build_logline(s, body->area, body->size, &reply->body.fmt);
4714 }
4715 /* else no payload */
4716
4717 status = ultoa(reply->status);
4718 reason = http_get_reason(reply->status);
4719 slflags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_CLEN);
4720 if (!body || !b_data(body))
4721 slflags |= HTX_SL_F_BODYLESS;
4722 sl = htx_add_stline(htx, HTX_BLK_RES_SL, slflags, ist("HTTP/1.1"), ist(status), ist(reason));
4723 if (!sl)
4724 goto fail;
4725 sl->info.res.status = reply->status;
4726
4727 clen = (body ? ultoa(b_data(body)) : "0");
4728 ctype = reply->ctype;
4729
4730 if (!LIST_ISEMPTY(&reply->hdrs)) {
4731 struct http_reply_hdr *hdr;
4732 struct buffer *value = alloc_trash_chunk();
4733
4734 if (!value)
4735 goto fail;
4736
4737 list_for_each_entry(hdr, &reply->hdrs, list) {
4738 chunk_reset(value);
4739 value->data = build_logline(s, value->area, value->size, &hdr->value);
4740 if (b_data(value) && !htx_add_header(htx, hdr->name, ist2(b_head(value), b_data(value)))) {
4741 free_trash_chunk(value);
4742 goto fail;
4743 }
4744 chunk_reset(value);
4745 }
4746 free_trash_chunk(value);
4747 }
4748
4749 if (!htx_add_header(htx, ist("content-length"), ist(clen)) ||
4750 (body && b_data(body) && ctype && !htx_add_header(htx, ist("content-type"), ist(ctype))) ||
4751 !htx_add_endof(htx, HTX_BLK_EOH) ||
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004752 (body && b_data(body) && !htx_add_data_atonce(htx, ist2(b_head(body), b_data(body)))))
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004753 goto fail;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004754
4755 htx->flags |= HTX_FL_EOM;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004756 }
4757
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004758 leave:
4759 if (reply->type == HTTP_REPLY_LOGFMT)
4760 free_trash_chunk(body);
4761 return ret;
4762
4763 fail_alloc:
4764 if (!(s->flags & SF_ERR_MASK))
4765 s->flags |= SF_ERR_RESOURCE;
Christopher Faulet97e466c2020-05-15 15:12:47 +02004766 /* fall through */
4767 fail:
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004768 ret = -1;
4769 goto leave;
Christopher Faulet97e466c2020-05-15 15:12:47 +02004770}
4771
4772/* Send an http reply to the client. On success, it returns 0. If an error
Christopher Faulet40e6b552020-06-25 16:04:50 +02004773 * occurs -1 is returned and the response channel is truncated, removing this
4774 * way the faulty reply. This function may fail when the reply is formatted
4775 * (http_reply_to_htx) or when the reply is forwarded
4776 * (http_forward_proxy_resp). On the last case, it is because a
4777 * http-after-response rule fails.
Christopher Faulet97e466c2020-05-15 15:12:47 +02004778 */
4779int http_reply_message(struct stream *s, struct http_reply *reply)
4780{
4781 struct channel *res = &s->res;
4782 struct htx *htx = htx_from_buf(&res->buf);
4783
4784 if (s->txn->status == -1)
4785 s->txn->status = reply->status;
4786 channel_htx_truncate(res, htx);
4787
4788 if (http_reply_to_htx(s, htx, reply) == -1)
4789 goto fail;
4790
4791 htx_to_buf(htx, &s->res.buf);
4792 if (!http_forward_proxy_resp(s, 1))
4793 goto fail;
4794 return 0;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004795
4796 fail:
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004797 channel_htx_truncate(res, htx);
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004798 if (!(s->flags & SF_ERR_MASK))
4799 s->flags |= SF_ERR_PRXCOND;
Christopher Faulet97e466c2020-05-15 15:12:47 +02004800 return -1;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004801}
4802
Christopher Faulet304cc402019-07-15 15:46:28 +02004803/* Return the error message corresponding to si->err_type. It is assumed
4804 * that the server side is closed. Note that err_type is actually a
4805 * bitmask, where almost only aborts may be cumulated with other
4806 * values. We consider that aborted operations are more important
4807 * than timeouts or errors due to the fact that nobody else in the
4808 * logs might explain incomplete retries. All others should avoid
4809 * being cumulated. It should normally not be possible to have multiple
4810 * aborts at once, but just in case, the first one in sequence is reported.
4811 * Note that connection errors appearing on the second request of a keep-alive
4812 * connection are not reported since this allows the client to retry.
4813 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004814void http_return_srv_error(struct stream *s, struct stream_interface *si)
Christopher Faulet304cc402019-07-15 15:46:28 +02004815{
4816 int err_type = si->err_type;
4817
4818 /* set s->txn->status for http_error_message(s) */
4819 s->txn->status = 503;
4820
4821 if (err_type & SI_ET_QUEUE_ABRT)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004822 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_Q,
4823 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004824 else if (err_type & SI_ET_CONN_ABRT)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004825 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_C,
4826 (s->txn->flags & TX_NOT_FIRST) ? NULL :
4827 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004828 else if (err_type & SI_ET_QUEUE_TO)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004829 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_Q,
4830 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004831 else if (err_type & SI_ET_QUEUE_ERR)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004832 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_Q,
4833 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004834 else if (err_type & SI_ET_CONN_TO)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004835 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_C,
4836 (s->txn->flags & TX_NOT_FIRST) ? NULL :
4837 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004838 else if (err_type & SI_ET_CONN_ERR)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004839 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_C,
4840 (s->flags & SF_SRV_REUSED) ? NULL :
4841 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004842 else if (err_type & SI_ET_CONN_RES)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004843 http_server_error(s, si, SF_ERR_RESOURCE, SF_FINST_C,
4844 (s->txn->flags & TX_NOT_FIRST) ? NULL :
4845 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004846 else { /* SI_ET_CONN_OTHER and others */
4847 s->txn->status = 500;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004848 http_server_error(s, si, SF_ERR_INTERNAL, SF_FINST_C,
4849 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004850 }
4851}
4852
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004853
Christopher Faulet4a28a532019-03-01 11:19:40 +01004854/* Handle Expect: 100-continue for HTTP/1.1 messages if necessary. It returns 0
4855 * on success and -1 on error.
4856 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004857static int http_handle_expect_hdr(struct stream *s, struct htx *htx, struct http_msg *msg)
Christopher Faulet4a28a532019-03-01 11:19:40 +01004858{
4859 /* If we have HTTP/1.1 message with a body and Expect: 100-continue,
4860 * then we must send an HTTP/1.1 100 Continue intermediate response.
4861 */
4862 if (msg->msg_state == HTTP_MSG_BODY && (msg->flags & HTTP_MSGF_VER_11) &&
4863 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
4864 struct ist hdr = { .ptr = "Expect", .len = 6 };
4865 struct http_hdr_ctx ctx;
4866
4867 ctx.blk = NULL;
4868 /* Expect is allowed in 1.1, look for it */
4869 if (http_find_header(htx, hdr, &ctx, 0) &&
4870 unlikely(isteqi(ctx.value, ist2("100-continue", 12)))) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004871 if (http_reply_100_continue(s) == -1)
Christopher Faulet4a28a532019-03-01 11:19:40 +01004872 return -1;
4873 http_remove_header(htx, &ctx);
4874 }
4875 }
4876 return 0;
4877}
4878
Christopher Faulet23a3c792018-11-28 10:01:23 +01004879/* Send a 100-Continue response to the client. It returns 0 on success and -1
4880 * on error. The response channel is updated accordingly.
4881 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004882static int http_reply_100_continue(struct stream *s)
Christopher Faulet23a3c792018-11-28 10:01:23 +01004883{
4884 struct channel *res = &s->res;
4885 struct htx *htx = htx_from_buf(&res->buf);
4886 struct htx_sl *sl;
4887 unsigned int flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|
4888 HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
Christopher Faulet23a3c792018-11-28 10:01:23 +01004889
4890 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
4891 ist("HTTP/1.1"), ist("100"), ist("Continue"));
4892 if (!sl)
4893 goto fail;
4894 sl->info.res.status = 100;
4895
Christopher Faulet1d5ec092019-06-26 14:23:54 +02004896 if (!htx_add_endof(htx, HTX_BLK_EOH))
Christopher Faulet23a3c792018-11-28 10:01:23 +01004897 goto fail;
4898
Christopher Fauleta72a7e42020-01-28 09:28:11 +01004899 if (!http_forward_proxy_resp(s, 0))
4900 goto fail;
Christopher Faulet23a3c792018-11-28 10:01:23 +01004901 return 0;
4902
4903 fail:
4904 /* If an error occurred, remove the incomplete HTTP response from the
4905 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004906 channel_htx_truncate(res, htx);
Christopher Faulet23a3c792018-11-28 10:01:23 +01004907 return -1;
4908}
4909
Christopher Faulet12c51e22018-11-28 15:59:42 +01004910
Christopher Faulet0f226952018-10-22 09:29:56 +02004911/*
4912 * Capture headers from message <htx> according to header list <cap_hdr>, and
4913 * fill the <cap> pointers appropriately.
4914 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004915static void http_capture_headers(struct htx *htx, char **cap, struct cap_hdr *cap_hdr)
Christopher Faulet0f226952018-10-22 09:29:56 +02004916{
4917 struct cap_hdr *h;
4918 int32_t pos;
4919
Christopher Fauleta3f15502019-05-13 15:27:23 +02004920 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet0f226952018-10-22 09:29:56 +02004921 struct htx_blk *blk = htx_get_blk(htx, pos);
4922 enum htx_blk_type type = htx_get_blk_type(blk);
4923 struct ist n, v;
4924
4925 if (type == HTX_BLK_EOH)
4926 break;
4927 if (type != HTX_BLK_HDR)
4928 continue;
4929
4930 n = htx_get_blk_name(htx, blk);
4931
4932 for (h = cap_hdr; h; h = h->next) {
4933 if (h->namelen && (h->namelen == n.len) &&
4934 (strncasecmp(n.ptr, h->name, h->namelen) == 0)) {
4935 if (cap[h->index] == NULL)
4936 cap[h->index] =
4937 pool_alloc(h->pool);
4938
4939 if (cap[h->index] == NULL) {
4940 ha_alert("HTTP capture : out of memory.\n");
4941 break;
4942 }
4943
4944 v = htx_get_blk_value(htx, blk);
4945 if (v.len > h->len)
4946 v.len = h->len;
4947
4948 memcpy(cap[h->index], v.ptr, v.len);
4949 cap[h->index][v.len]=0;
4950 }
4951 }
4952 }
4953}
4954
Christopher Faulet0b6bdc52018-10-24 11:05:36 +02004955/* Delete a value in a header between delimiters <from> and <next>. The header
4956 * itself is delimited by <start> and <end> pointers. The number of characters
4957 * displaced is returned, and the pointer to the first delimiter is updated if
4958 * required. The function tries as much as possible to respect the following
4959 * principles :
4960 * - replace <from> delimiter by the <next> one unless <from> points to <start>,
4961 * in which case <next> is simply removed
4962 * - set exactly one space character after the new first delimiter, unless there
4963 * are not enough characters in the block being moved to do so.
4964 * - remove unneeded spaces before the previous delimiter and after the new
4965 * one.
4966 *
4967 * It is the caller's responsibility to ensure that :
4968 * - <from> points to a valid delimiter or <start> ;
4969 * - <next> points to a valid delimiter or <end> ;
4970 * - there are non-space chars before <from>.
4971 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004972static int http_del_hdr_value(char *start, char *end, char **from, char *next)
Christopher Faulet0b6bdc52018-10-24 11:05:36 +02004973{
4974 char *prev = *from;
4975
4976 if (prev == start) {
4977 /* We're removing the first value. eat the semicolon, if <next>
4978 * is lower than <end> */
4979 if (next < end)
4980 next++;
4981
4982 while (next < end && HTTP_IS_SPHT(*next))
4983 next++;
4984 }
4985 else {
4986 /* Remove useless spaces before the old delimiter. */
4987 while (HTTP_IS_SPHT(*(prev-1)))
4988 prev--;
4989 *from = prev;
4990
4991 /* copy the delimiter and if possible a space if we're
4992 * not at the end of the line.
4993 */
4994 if (next < end) {
4995 *prev++ = *next++;
4996 if (prev + 1 < next)
4997 *prev++ = ' ';
4998 while (next < end && HTTP_IS_SPHT(*next))
4999 next++;
5000 }
5001 }
5002 memmove(prev, next, end - next);
5003 return (prev - next);
5004}
5005
Christopher Faulet0f226952018-10-22 09:29:56 +02005006
5007/* Formats the start line of the request (without CRLF) and puts it in <str> and
Joseph Herlantc42c0e92018-11-25 10:43:27 -08005008 * return the written length. The line can be truncated if it exceeds <len>.
Christopher Faulet0f226952018-10-22 09:29:56 +02005009 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005010static size_t http_fmt_req_line(const struct htx_sl *sl, char *str, size_t len)
Christopher Faulet0f226952018-10-22 09:29:56 +02005011{
5012 struct ist dst = ist2(str, 0);
5013
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005014 if (istcat(&dst, htx_sl_req_meth(sl), len) == -1)
Christopher Faulet0f226952018-10-22 09:29:56 +02005015 goto end;
5016 if (dst.len + 1 > len)
5017 goto end;
5018 dst.ptr[dst.len++] = ' ';
5019
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005020 if (istcat(&dst, htx_sl_req_uri(sl), len) == -1)
Christopher Faulet0f226952018-10-22 09:29:56 +02005021 goto end;
5022 if (dst.len + 1 > len)
5023 goto end;
5024 dst.ptr[dst.len++] = ' ';
5025
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005026 istcat(&dst, htx_sl_req_vsn(sl), len);
Christopher Faulet0f226952018-10-22 09:29:56 +02005027 end:
5028 return dst.len;
5029}
5030
5031/*
5032 * Print a debug line with a start line.
5033 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005034static void http_debug_stline(const char *dir, struct stream *s, const struct htx_sl *sl)
Christopher Faulet0f226952018-10-22 09:29:56 +02005035{
5036 struct session *sess = strm_sess(s);
5037 int max;
5038
5039 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
5040 dir,
5041 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->handle.fd : -1,
5042 objt_cs(s->si[1].end) ? (unsigned short)objt_cs(s->si[1].end)->conn->handle.fd : -1);
5043
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005044 max = HTX_SL_P1_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02005045 UBOUND(max, trash.size - trash.data - 3);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005046 chunk_memcat(&trash, HTX_SL_P1_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02005047 trash.area[trash.data++] = ' ';
5048
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005049 max = HTX_SL_P2_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02005050 UBOUND(max, trash.size - trash.data - 2);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005051 chunk_memcat(&trash, HTX_SL_P2_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02005052 trash.area[trash.data++] = ' ';
5053
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005054 max = HTX_SL_P3_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02005055 UBOUND(max, trash.size - trash.data - 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005056 chunk_memcat(&trash, HTX_SL_P3_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02005057 trash.area[trash.data++] = '\n';
5058
Willy Tarreau2e8ab6b2020-03-14 11:03:20 +01005059 DISGUISE(write(1, trash.area, trash.data));
Christopher Faulet0f226952018-10-22 09:29:56 +02005060}
5061
5062/*
5063 * Print a debug line with a header.
5064 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005065static 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 +02005066{
5067 struct session *sess = strm_sess(s);
5068 int max;
5069
5070 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
5071 dir,
5072 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->handle.fd : -1,
5073 objt_cs(s->si[1].end) ? (unsigned short)objt_cs(s->si[1].end)->conn->handle.fd : -1);
5074
5075 max = n.len;
5076 UBOUND(max, trash.size - trash.data - 3);
5077 chunk_memcat(&trash, n.ptr, max);
5078 trash.area[trash.data++] = ':';
5079 trash.area[trash.data++] = ' ';
5080
5081 max = v.len;
5082 UBOUND(max, trash.size - trash.data - 1);
5083 chunk_memcat(&trash, v.ptr, max);
5084 trash.area[trash.data++] = '\n';
5085
Willy Tarreau2e8ab6b2020-03-14 11:03:20 +01005086 DISGUISE(write(1, trash.area, trash.data));
Christopher Faulet0f226952018-10-22 09:29:56 +02005087}
5088
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005089/* Allocate a new HTTP transaction for stream <s> unless there is one already.
5090 * In case of allocation failure, everything allocated is freed and NULL is
5091 * returned. Otherwise the new transaction is assigned to the stream and
5092 * returned.
5093 */
5094struct http_txn *http_alloc_txn(struct stream *s)
5095{
5096 struct http_txn *txn = s->txn;
5097
5098 if (txn)
5099 return txn;
5100
5101 txn = pool_alloc(pool_head_http_txn);
5102 if (!txn)
5103 return txn;
5104
5105 s->txn = txn;
5106 return txn;
5107}
5108
5109void http_txn_reset_req(struct http_txn *txn)
5110{
Christopher Faulet1aea50e2020-01-17 16:03:53 +01005111 txn->req.flags = 0;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005112 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
5113}
5114
5115void http_txn_reset_res(struct http_txn *txn)
5116{
Christopher Faulet1aea50e2020-01-17 16:03:53 +01005117 txn->rsp.flags = 0;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005118 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
5119}
5120
5121/*
Christopher Faulet75f619a2021-03-08 19:12:58 +01005122 * Create and initialize a new HTTP transaction for stream <s>. This should be
5123 * used before processing any new request. It returns the transaction or NLULL
5124 * on error.
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005125 */
Christopher Faulet75f619a2021-03-08 19:12:58 +01005126struct http_txn *http_create_txn(struct stream *s)
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005127{
Christopher Faulet75f619a2021-03-08 19:12:58 +01005128 struct http_txn *txn;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005129 struct conn_stream *cs = objt_cs(s->si[0].end);
5130
Christopher Faulet75f619a2021-03-08 19:12:58 +01005131 txn = pool_alloc(pool_head_http_txn);
5132 if (!txn)
5133 return NULL;
5134 s->txn = txn;
5135
Christopher Fauletda831fa2020-10-06 17:58:43 +02005136 txn->flags = ((cs && cs->flags & CS_FL_NOT_FIRST) ? TX_NOT_FIRST : 0);
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005137 txn->status = -1;
Christopher Faulet5cb513a2020-05-13 17:56:56 +02005138 txn->http_reply = NULL;
Willy Tarreau8b507582020-02-25 09:35:07 +01005139 write_u32(txn->cache_hash, 0);
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005140
5141 txn->cookie_first_date = 0;
5142 txn->cookie_last_date = 0;
5143
5144 txn->srv_cookie = NULL;
5145 txn->cli_cookie = NULL;
5146 txn->uri = NULL;
5147
5148 http_txn_reset_req(txn);
5149 http_txn_reset_res(txn);
5150
5151 txn->req.chn = &s->req;
5152 txn->rsp.chn = &s->res;
5153
5154 txn->auth.method = HTTP_AUTH_UNKNOWN;
5155
5156 vars_init(&s->vars_txn, SCOPE_TXN);
5157 vars_init(&s->vars_reqres, SCOPE_REQ);
Christopher Faulet75f619a2021-03-08 19:12:58 +01005158
5159 return txn;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005160}
5161
5162/* to be used at the end of a transaction */
Christopher Faulet75f619a2021-03-08 19:12:58 +01005163void http_destroy_txn(struct stream *s)
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005164{
5165 struct http_txn *txn = s->txn;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005166
5167 /* these ones will have been dynamically allocated */
5168 pool_free(pool_head_requri, txn->uri);
5169 pool_free(pool_head_capture, txn->cli_cookie);
5170 pool_free(pool_head_capture, txn->srv_cookie);
Tim Duesterhusa17e6622020-03-05 20:19:02 +01005171 pool_free(pool_head_uniqueid, s->unique_id.ptr);
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005172
Tim Duesterhusa17e6622020-03-05 20:19:02 +01005173 s->unique_id = IST_NULL;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005174 txn->uri = NULL;
5175 txn->srv_cookie = NULL;
5176 txn->cli_cookie = NULL;
5177
Christopher Faulet59399252019-11-07 14:27:52 +01005178 if (!LIST_ISEMPTY(&s->vars_txn.head))
5179 vars_prune(&s->vars_txn, s->sess, s);
5180 if (!LIST_ISEMPTY(&s->vars_reqres.head))
5181 vars_prune(&s->vars_reqres, s->sess, s);
Christopher Faulet75f619a2021-03-08 19:12:58 +01005182
5183 pool_free(pool_head_http_txn, txn);
5184 s->txn = NULL;
Christopher Faulet59399252019-11-07 14:27:52 +01005185}
5186
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005187
5188DECLARE_POOL(pool_head_http_txn, "http_txn", sizeof(struct http_txn));
Christopher Faulet0f226952018-10-22 09:29:56 +02005189
Christopher Fauletf4eb75d2018-10-11 15:55:07 +02005190__attribute__((constructor))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005191static void __http_protocol_init(void)
Christopher Fauletf4eb75d2018-10-11 15:55:07 +02005192{
5193}
5194
5195
5196/*
5197 * Local variables:
5198 * c-indent-level: 8
5199 * c-basic-offset: 8
5200 * End:
5201 */