blob: f662a31b9de76854e217837f65bceb90372fa0ab [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
Willy Tarreauff882702021-04-10 17:23:00 +020045struct pool_head *pool_head_requri __read_mostly = NULL;
46struct pool_head *pool_head_capture __read_mostly = NULL;
Christopher Fauleta8a46e22019-07-16 14:53:09 +020047
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;
Willy Tarreau4781b152021-04-06 13:53:36 +0200215 _HA_ATOMIC_INC(&sess->fe->fe_counters.intercepted_req);
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;
Willy Tarreau4781b152021-04-06 13:53:36 +0200313 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
William Lallemand36119de2021-03-08 15:26:48 +0100314 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200315 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
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;
Willy Tarreau4781b152021-04-06 13:53:36 +0200320 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req);
William Lallemand36119de2021-03-08 15:26:48 +0100321 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200322 _HA_ATOMIC_INC(&sess->listener->counters->failed_req);
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 */
Willy Tarreau4781b152021-04-06 13:53:36 +0200445 _HA_ATOMIC_INC(&sess->fe->fe_counters.intercepted_req);
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);
Willy Tarreau4781b152021-04-06 13:53:36 +0200527 _HA_ATOMIC_INC(&sess->fe->fe_counters.denied_req);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100528 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau4781b152021-04-06 13:53:36 +0200529 _HA_ATOMIC_INC(&s->be->be_counters.denied_req);
William Lallemand36119de2021-03-08 15:26:48 +0100530 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200531 _HA_ATOMIC_INC(&sess->listener->counters->denied_req);
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);
Willy Tarreau4781b152021-04-06 13:53:36 +0200543 _HA_ATOMIC_INC(&sess->fe->fe_counters.denied_req);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100544 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau4781b152021-04-06 13:53:36 +0200545 _HA_ATOMIC_INC(&s->be->be_counters.denied_req);
William Lallemand36119de2021-03-08 15:26:48 +0100546 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200547 _HA_ATOMIC_INC(&sess->listener->counters->denied_req);
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;
Willy Tarreau4781b152021-04-06 13:53:36 +0200554 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100555 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau4781b152021-04-06 13:53:36 +0200556 _HA_ATOMIC_INC(&s->be->be_counters.internal_errors);
William Lallemand36119de2021-03-08 15:26:48 +0100557 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200558 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100559 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;
Willy Tarreau4781b152021-04-06 13:53:36 +0200563 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req);
William Lallemand36119de2021-03-08 15:26:48 +0100564 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200565 _HA_ATOMIC_INC(&sess->listener->counters->failed_req);
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;
Willy Tarreau4781b152021-04-06 13:53:36 +0200827 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100828 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau4781b152021-04-06 13:53:36 +0200829 _HA_ATOMIC_INC(&s->be->be_counters.internal_errors);
William Lallemand36119de2021-03-08 15:26:48 +0100830 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200831 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100832 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;
Willy Tarreau4781b152021-04-06 13:53:36 +0200836 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req);
William Lallemand36119de2021-03-08 15:26:48 +0100837 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200838 _HA_ATOMIC_INC(&sess->listener->counters->failed_req);
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;
Willy Tarreau4781b152021-04-06 13:53:36 +0200953 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100954 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau4781b152021-04-06 13:53:36 +0200955 _HA_ATOMIC_INC(&s->be->be_counters.internal_errors);
William Lallemand36119de2021-03-08 15:26:48 +0100956 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200957 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
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;
Willy Tarreau4781b152021-04-06 13:53:36 +0200962 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req);
William Lallemand36119de2021-03-08 15:26:48 +0100963 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200964 _HA_ATOMIC_INC(&sess->listener->counters->failed_req);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100965 /* 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.
Christopher Fauletf506d962021-04-27 10:56:28 +02001016 *
1017 * If we have finished to send the request and the response is
1018 * still in progress, don't catch write error on the request
1019 * side if it is in fact a read error on the server side.
Christopher Faulete0768eb2018-10-03 16:38:02 +02001020 */
Christopher Fauletf506d962021-04-27 10:56:28 +02001021 if (msg->msg_state == HTTP_MSG_DONE && (s->res.flags & CF_READ_ERROR) && s->res.analysers)
1022 return 0;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001023
Olivier Houchard29cac3c2019-07-12 15:48:58 +02001024 /* Don't abort yet if we had L7 retries activated and it
1025 * was a write error, we may recover.
1026 */
1027 if (!(req->flags & (CF_READ_ERROR | CF_READ_TIMEOUT)) &&
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001028 (s->si[1].flags & SI_FL_L7_RETRY)) {
1029 DBG_TRACE_DEVEL("leaving on L7 retry",
1030 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Olivier Houchard29cac3c2019-07-12 15:48:58 +02001031 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001032 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001033 msg->msg_state = HTTP_MSG_ERROR;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001034 http_end_request(s);
1035 http_end_response(s);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001036 DBG_TRACE_DEVEL("leaving on error",
1037 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001038 return 1;
1039 }
1040
1041 /* Note that we don't have to send 100-continue back because we don't
1042 * need the data to complete our job, and it's up to the server to
1043 * decide whether to return 100, 417 or anything else in return of
1044 * an "Expect: 100-continue" header.
1045 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001046 if (msg->msg_state == HTTP_MSG_BODY)
1047 msg->msg_state = HTTP_MSG_DATA;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001048
Christopher Faulete0768eb2018-10-03 16:38:02 +02001049 /* in most states, we should abort in case of early close */
1050 channel_auto_close(req);
1051
1052 if (req->to_forward) {
Christopher Faulet66af0b22019-03-22 14:54:52 +01001053 if (req->to_forward == CHN_INFINITE_FORWARD) {
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001054 if (req->flags & CF_EOI)
1055 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet66af0b22019-03-22 14:54:52 +01001056 }
1057 else {
1058 /* We can't process the buffer's contents yet */
1059 req->flags |= CF_WAKE_WRITE;
1060 goto missing_data_or_waiting;
1061 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001062 }
1063
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001064 if (msg->msg_state >= HTTP_MSG_ENDING)
1065 goto ending;
1066
1067 if (txn->meth == HTTP_METH_CONNECT) {
1068 msg->msg_state = HTTP_MSG_ENDING;
1069 goto ending;
1070 }
1071
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001072 /* Forward input data. We get it by removing all outgoing data not
1073 * forwarded yet from HTX data size. If there are some data filters, we
1074 * let them decide the amount of data to forward.
Christopher Faulet9768c262018-10-22 09:34:31 +02001075 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001076 if (HAS_REQ_DATA_FILTERS(s)) {
1077 ret = flt_http_payload(s, msg, htx->data);
1078 if (ret < 0)
1079 goto return_bad_req;
Christopher Faulet421e7692019-06-13 11:16:45 +02001080 c_adv(req, ret);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001081 }
1082 else {
Christopher Faulet421e7692019-06-13 11:16:45 +02001083 c_adv(req, htx->data - co_data(req));
Christopher Faulet66af0b22019-03-22 14:54:52 +01001084 if (msg->flags & HTTP_MSGF_XFER_LEN)
1085 channel_htx_forward_forever(req, htx);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001086 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001087
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001088 if (htx->data != co_data(req))
1089 goto missing_data_or_waiting;
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001090
Christopher Faulet9768c262018-10-22 09:34:31 +02001091 /* Check if the end-of-message is reached and if so, switch the message
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001092 * in HTTP_MSG_ENDING state. Then if all data was marked to be
1093 * forwarded, set the state to HTTP_MSG_DONE.
Christopher Faulet9768c262018-10-22 09:34:31 +02001094 */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001095 if (!(htx->flags & HTX_FL_EOM))
Christopher Faulet9768c262018-10-22 09:34:31 +02001096 goto missing_data_or_waiting;
1097
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001098 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet9768c262018-10-22 09:34:31 +02001099
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001100 ending:
Christopher Faulet2151cdd2020-07-22 16:34:59 +02001101 req->flags &= ~CF_EXPECT_MORE; /* no more data are expected */
1102
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001103 /* other states, ENDING...TUNNEL */
1104 if (msg->msg_state >= HTTP_MSG_DONE)
1105 goto done;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001106
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001107 if (HAS_REQ_DATA_FILTERS(s)) {
1108 ret = flt_http_end(s, msg);
1109 if (ret <= 0) {
1110 if (!ret)
1111 goto missing_data_or_waiting;
1112 goto return_bad_req;
1113 }
1114 }
1115
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001116 if (txn->meth == HTTP_METH_CONNECT)
1117 msg->msg_state = HTTP_MSG_TUNNEL;
1118 else {
1119 msg->msg_state = HTTP_MSG_DONE;
1120 req->to_forward = 0;
Christopher Faulet5b82cc52020-10-12 15:18:50 +02001121
1122 if ((s->be->retry_type &~ PR_RE_CONN_FAILED) && !(s->si[1].flags & SI_FL_D_L7_RETRY)) {
1123 struct stream_interface *si = &s->si[1];
1124
1125 /* If we want to be able to do L7 retries, copy the
1126 * request, so that we are able to resend them if
1127 * needed.
1128 *
1129 * Try to allocate a buffer if we had none. If it
1130 * fails, the next test will just disable the l7
1131 * retries.
1132 */
1133 DBG_TRACE_STATE("enable L7 retry, save the request", STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
1134 si->flags |= SI_FL_L7_RETRY;
Willy Tarreauf499f502021-03-22 16:17:37 +01001135 if (b_alloc(&si->l7_buffer) == NULL)
Christopher Faulet5b82cc52020-10-12 15:18:50 +02001136 si->flags &= ~SI_FL_L7_RETRY;
1137 else {
1138 memcpy(b_orig(&si->l7_buffer), b_orig(&req->buf), b_size(&req->buf));
1139 b_add(&si->l7_buffer, co_data(req));
1140 }
1141 }
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001142 }
1143
1144 done:
1145 /* we don't want to forward closes on DONE except in tunnel mode. */
1146 if (!(txn->flags & TX_CON_WANT_TUN))
1147 channel_dont_close(req);
1148
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001149 http_end_request(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001150 if (!(req->analysers & an_bit)) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001151 http_end_response(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001152 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
1153 if (req->flags & CF_SHUTW) {
1154 /* request errors are most likely due to the
1155 * server aborting the transfer. */
Christopher Faulet93e02d82019-03-08 14:18:50 +01001156 goto return_srv_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001157 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001158 goto return_bad_req;
1159 }
Christopher Faulet5b82cc52020-10-12 15:18:50 +02001160
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001161 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001162 return 1;
1163 }
1164
1165 /* If "option abortonclose" is set on the backend, we want to monitor
1166 * the client's connection and forward any shutdown notification to the
1167 * server, which will decide whether to close or to go on processing the
1168 * request. We only do that in tunnel mode, and not in other modes since
1169 * it can be abused to exhaust source ports. */
Christopher Faulet769d0e92019-03-22 14:23:18 +01001170 if (s->be->options & PR_O_ABRT_CLOSE) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001171 channel_auto_read(req);
Christopher Fauletc41547b2019-07-16 14:32:23 +02001172 if ((req->flags & (CF_SHUTR|CF_READ_NULL)) && !(txn->flags & TX_CON_WANT_TUN))
Christopher Faulete0768eb2018-10-03 16:38:02 +02001173 s->si[1].flags |= SI_FL_NOLINGER;
1174 channel_auto_close(req);
1175 }
1176 else if (s->txn->meth == HTTP_METH_POST) {
1177 /* POST requests may require to read extra CRLF sent by broken
1178 * browsers and which could cause an RST to be sent upon close
1179 * on some systems (eg: Linux). */
1180 channel_auto_read(req);
1181 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001182 DBG_TRACE_DEVEL("waiting for the end of the HTTP txn",
1183 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001184 return 0;
1185
1186 missing_data_or_waiting:
1187 /* stop waiting for data if the input is closed before the end */
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001188 if (msg->msg_state < HTTP_MSG_ENDING && req->flags & CF_SHUTR)
Christopher Faulet93e02d82019-03-08 14:18:50 +01001189 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001190
1191 waiting:
1192 /* waiting for the last bits to leave the buffer */
1193 if (req->flags & CF_SHUTW)
Christopher Faulet93e02d82019-03-08 14:18:50 +01001194 goto return_srv_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001195
1196 /* When TE: chunked is used, we need to get there again to parse remaining
1197 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
1198 * And when content-length is used, we never want to let the possible
1199 * shutdown be forwarded to the other side, as the state machine will
1200 * take care of it once the client responds. It's also important to
1201 * prevent TIME_WAITs from accumulating on the backend side, and for
1202 * HTTP/2 where the last frame comes with a shutdown.
1203 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001204 if (msg->flags & HTTP_MSGF_XFER_LEN)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001205 channel_dont_close(req);
1206
1207 /* We know that more data are expected, but we couldn't send more that
1208 * what we did. So we always set the CF_EXPECT_MORE flag so that the
1209 * system knows it must not set a PUSH on this first part. Interactive
1210 * modes are already handled by the stream sock layer. We must not do
1211 * this in content-length mode because it could present the MSG_MORE
1212 * flag with the last block of forwarded data, which would cause an
1213 * additional delay to be observed by the receiver.
1214 */
Christopher Faulet2151cdd2020-07-22 16:34:59 +02001215 if (HAS_REQ_DATA_FILTERS(s))
Christopher Faulete0768eb2018-10-03 16:38:02 +02001216 req->flags |= CF_EXPECT_MORE;
1217
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001218 DBG_TRACE_DEVEL("waiting for more data to forward",
1219 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001220 return 0;
1221
Christopher Faulet93e02d82019-03-08 14:18:50 +01001222 return_cli_abort:
Willy Tarreau4781b152021-04-06 13:53:36 +02001223 _HA_ATOMIC_INC(&sess->fe->fe_counters.cli_aborts);
1224 _HA_ATOMIC_INC(&s->be->be_counters.cli_aborts);
William Lallemand36119de2021-03-08 15:26:48 +01001225 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001226 _HA_ATOMIC_INC(&sess->listener->counters->cli_aborts);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001227 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001228 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.cli_aborts);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001229 if (!(s->flags & SF_ERR_MASK))
1230 s->flags |= SF_ERR_CLICL;
1231 status = 400;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001232 goto return_prx_cond;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001233
1234 return_srv_abort:
Willy Tarreau4781b152021-04-06 13:53:36 +02001235 _HA_ATOMIC_INC(&sess->fe->fe_counters.srv_aborts);
1236 _HA_ATOMIC_INC(&s->be->be_counters.srv_aborts);
William Lallemand36119de2021-03-08 15:26:48 +01001237 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001238 _HA_ATOMIC_INC(&sess->listener->counters->srv_aborts);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001239 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001240 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.srv_aborts);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001241 if (!(s->flags & SF_ERR_MASK))
1242 s->flags |= SF_ERR_SRVCL;
1243 status = 502;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001244 goto return_prx_cond;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001245
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001246 return_int_err:
1247 if (!(s->flags & SF_ERR_MASK))
1248 s->flags |= SF_ERR_INTERNAL;
Willy Tarreau4781b152021-04-06 13:53:36 +02001249 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
1250 _HA_ATOMIC_INC(&s->be->be_counters.internal_errors);
William Lallemand36119de2021-03-08 15:26:48 +01001251 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001252 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001253 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001254 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.internal_errors);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001255 status = 500;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001256 goto return_prx_cond;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001257
Christopher Faulet93e02d82019-03-08 14:18:50 +01001258 return_bad_req:
Willy Tarreau4781b152021-04-06 13:53:36 +02001259 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req);
William Lallemand36119de2021-03-08 15:26:48 +01001260 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001261 _HA_ATOMIC_INC(&sess->listener->counters->failed_req);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001262 status = 400;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001263 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001264
Christopher Fauletb8a53712019-12-16 11:29:38 +01001265 return_prx_cond:
Christopher Faulet9768c262018-10-22 09:34:31 +02001266 if (txn->status > 0) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001267 /* Note: we don't send any error if some data were already sent */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001268 http_reply_and_close(s, txn->status, NULL);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001269 } else {
Christopher Faulet93e02d82019-03-08 14:18:50 +01001270 txn->status = status;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001271 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001272 }
1273 req->analysers &= AN_REQ_FLT_END;
1274 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 +01001275 if (!(s->flags & SF_ERR_MASK))
1276 s->flags |= SF_ERR_PRXCOND;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001277 if (!(s->flags & SF_FINST_MASK))
1278 s->flags |= ((txn->rsp.msg_state < HTTP_MSG_ERROR) ? SF_FINST_H : SF_FINST_D);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001279 DBG_TRACE_DEVEL("leaving on error ",
1280 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001281 return 0;
1282}
1283
Olivier Houcharda254a372019-04-05 15:30:12 +02001284/* Reset the stream and the backend stream_interface to a situation suitable for attemption connection */
1285/* Returns 0 if we can attempt to retry, -1 otherwise */
1286static __inline int do_l7_retry(struct stream *s, struct stream_interface *si)
1287{
Christopher Faulet5b82cc52020-10-12 15:18:50 +02001288 struct channel *req = &s->req;
1289 struct channel *res = &s->res;
Olivier Houcharda254a372019-04-05 15:30:12 +02001290
1291 si->conn_retries--;
1292 if (si->conn_retries < 0)
Christopher Faulet5b82cc52020-10-12 15:18:50 +02001293 goto no_retry;
1294
1295 if (b_is_null(&req->buf) && !channel_alloc_buffer(req, &s->buffer_wait))
1296 goto no_retry;
Olivier Houcharda254a372019-04-05 15:30:12 +02001297
Willy Tarreau223995e2019-05-04 10:38:31 +02001298 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001299 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.retries);
1300 _HA_ATOMIC_INC(&s->be->be_counters.retries);
Willy Tarreau223995e2019-05-04 10:38:31 +02001301
Olivier Houcharda254a372019-04-05 15:30:12 +02001302 /* Remove any write error from the request, and read error from the response */
1303 req->flags &= ~(CF_WRITE_ERROR | CF_WRITE_TIMEOUT | CF_SHUTW | CF_SHUTW_NOW);
1304 res->flags &= ~(CF_READ_ERROR | CF_READ_TIMEOUT | CF_SHUTR | CF_EOI | CF_READ_NULL | CF_SHUTR_NOW);
1305 res->analysers = 0;
1306 si->flags &= ~(SI_FL_ERR | SI_FL_EXP | SI_FL_RXBLK_SHUT);
Olivier Houchard8cabc972020-05-12 22:18:14 +02001307 s->flags &= ~SF_ADDR_SET;
Olivier Houchard4bd58672019-07-12 16:16:59 +02001308 stream_choose_redispatch(s);
Olivier Houcharda254a372019-04-05 15:30:12 +02001309 si->exp = TICK_ETERNITY;
1310 res->rex = TICK_ETERNITY;
1311 res->to_forward = 0;
1312 res->analyse_exp = TICK_ETERNITY;
1313 res->total = 0;
Olivier Houchard4bd58672019-07-12 16:16:59 +02001314 s->flags &= ~(SF_ERR_SRVTO | SF_ERR_SRVCL);
Olivier Houcharda254a372019-04-05 15:30:12 +02001315 si_release_endpoint(&s->si[1]);
Olivier Houcharda254a372019-04-05 15:30:12 +02001316
Christopher Faulet5b82cc52020-10-12 15:18:50 +02001317 b_reset(&req->buf);
1318 memcpy(b_orig(&req->buf), b_orig(&si->l7_buffer), b_size(&si->l7_buffer));
1319 b_set_data(&req->buf, b_size(&req->buf));
1320 co_set_data(req, b_data(&si->l7_buffer));
1321
Ilya Shipitsinacf84592021-02-06 22:29:08 +05001322 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 +02001323 b_reset(&res->buf);
1324 co_set_data(res, 0);
1325 return 0;
Christopher Faulet5b82cc52020-10-12 15:18:50 +02001326
1327 no_retry:
1328 b_free(&si->l7_buffer);
1329 return -1;
Olivier Houcharda254a372019-04-05 15:30:12 +02001330}
1331
Christopher Faulete0768eb2018-10-03 16:38:02 +02001332/* This stream analyser waits for a complete HTTP response. It returns 1 if the
1333 * processing can continue on next analysers, or zero if it either needs more
1334 * data or wants to immediately abort the response (eg: timeout, error, ...). It
1335 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->res.analysers
1336 * when it has nothing left to do, and may remove any analyser when it wants to
1337 * abort.
1338 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001339int http_wait_for_response(struct stream *s, struct channel *rep, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001340{
Christopher Faulet9768c262018-10-22 09:34:31 +02001341 /*
1342 * We will analyze a complete HTTP response to check the its syntax.
1343 *
1344 * Once the start line and all headers are received, we may perform a
1345 * capture of the error (if any), and we will set a few fields. We also
1346 * logging and finally headers capture.
1347 */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001348 struct session *sess = s->sess;
1349 struct http_txn *txn = s->txn;
1350 struct http_msg *msg = &txn->rsp;
Christopher Faulet9768c262018-10-22 09:34:31 +02001351 struct htx *htx;
Olivier Houcharda254a372019-04-05 15:30:12 +02001352 struct stream_interface *si_b = &s->si[1];
Christopher Faulet61608322018-11-23 16:23:45 +01001353 struct connection *srv_conn;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001354 struct htx_sl *sl;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001355 int n;
1356
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001357 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001358
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001359 htx = htxbuf(&rep->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001360
Willy Tarreau4236f032019-03-05 10:43:32 +01001361 /* Parsing errors are caught here */
1362 if (htx->flags & HTX_FL_PARSING_ERROR)
1363 goto return_bad_res;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001364 if (htx->flags & HTX_FL_PROCESSING_ERROR)
1365 goto return_int_err;
Willy Tarreau4236f032019-03-05 10:43:32 +01001366
Christopher Faulete0768eb2018-10-03 16:38:02 +02001367 /*
1368 * Now we quickly check if we have found a full valid response.
1369 * If not so, we check the FD and buffer states before leaving.
1370 * A full response is indicated by the fact that we have seen
1371 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
1372 * responses are checked first.
1373 *
1374 * Depending on whether the client is still there or not, we
1375 * may send an error response back or not. Note that normally
1376 * we should only check for HTTP status there, and check I/O
1377 * errors somewhere else.
1378 */
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001379 next_one:
Christopher Faulet29f17582019-05-23 11:03:26 +02001380 if (unlikely(htx_is_empty(htx) || htx->first == -1)) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001381 /* 1: have we encountered a read error ? */
1382 if (rep->flags & CF_READ_ERROR) {
Olivier Houchard865d8392019-05-03 22:46:27 +02001383 struct connection *conn = NULL;
1384
Olivier Houchard865d8392019-05-03 22:46:27 +02001385 if (objt_cs(s->si[1].end))
1386 conn = objt_cs(s->si[1].end)->conn;
1387
1388 if (si_b->flags & SI_FL_L7_RETRY &&
1389 (!conn || conn->err_code != CO_ER_SSL_EARLY_FAILED)) {
Olivier Houcharda254a372019-04-05 15:30:12 +02001390 /* If we arrive here, then CF_READ_ERROR was
1391 * set by si_cs_recv() because we matched a
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05001392 * status, otherwise it would have removed
Olivier Houcharda254a372019-04-05 15:30:12 +02001393 * the SI_FL_L7_RETRY flag, so it's ok not
1394 * to check s->be->retry_type.
1395 */
Christopher Faulet5b82cc52020-10-12 15:18:50 +02001396 if (co_data(rep) || do_l7_retry(s, si_b) == 0) {
1397 DBG_TRACE_DEVEL("leaving on L7 retry",
1398 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharda254a372019-04-05 15:30:12 +02001399 return 0;
Christopher Faulet5b82cc52020-10-12 15:18:50 +02001400 }
Olivier Houcharda254a372019-04-05 15:30:12 +02001401 }
1402
Olivier Houchard6db16992019-05-17 15:40:49 +02001403 if (txn->flags & TX_NOT_FIRST)
1404 goto abort_keep_alive;
1405
Willy Tarreau4781b152021-04-06 13:53:36 +02001406 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001407 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001408 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001409 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001410 }
1411
Christopher Faulete0768eb2018-10-03 16:38:02 +02001412 rep->analysers &= AN_RES_FLT_END;
1413 txn->status = 502;
1414
1415 /* Check to see if the server refused the early data.
1416 * If so, just send a 425
1417 */
Willy Tarreauee99aaf2020-06-23 05:58:20 +02001418 if (conn && conn->err_code == CO_ER_SSL_EARLY_FAILED) {
Olivier Houchard865d8392019-05-03 22:46:27 +02001419 if ((s->be->retry_type & PR_RE_EARLY_ERROR) &&
Olivier Houchardad26d8d2019-05-10 17:48:28 +02001420 (si_b->flags & SI_FL_L7_RETRY) &&
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001421 do_l7_retry(s, si_b) == 0) {
1422 DBG_TRACE_DEVEL("leaving on L7 retry",
1423 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houchard865d8392019-05-03 22:46:27 +02001424 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001425 }
Olivier Houchard865d8392019-05-03 22:46:27 +02001426 txn->status = 425;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001427 }
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001428 else
1429 stream_inc_http_fail_ctr(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001430
1431 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001432 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001433
1434 if (!(s->flags & SF_ERR_MASK))
1435 s->flags |= SF_ERR_SRVCL;
1436 if (!(s->flags & SF_FINST_MASK))
1437 s->flags |= SF_FINST_H;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001438 DBG_TRACE_DEVEL("leaving on error",
1439 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001440 return 0;
1441 }
1442
Christopher Faulet9768c262018-10-22 09:34:31 +02001443 /* 2: read timeout : return a 504 to the client. */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001444 else if (rep->flags & CF_READ_TIMEOUT) {
Olivier Houcharda254a372019-04-05 15:30:12 +02001445 if ((si_b->flags & SI_FL_L7_RETRY) &&
1446 (s->be->retry_type & PR_RE_TIMEOUT)) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001447 if (co_data(rep) || do_l7_retry(s, si_b) == 0) {
1448 DBG_TRACE_DEVEL("leaving on L7 retry",
1449 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharda254a372019-04-05 15:30:12 +02001450 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001451 }
Olivier Houcharda254a372019-04-05 15:30:12 +02001452 }
Willy Tarreau4781b152021-04-06 13:53:36 +02001453 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001454 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001455 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001456 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001457 }
1458
Christopher Faulete0768eb2018-10-03 16:38:02 +02001459 rep->analysers &= AN_RES_FLT_END;
1460 txn->status = 504;
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001461 stream_inc_http_fail_ctr(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001462 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001463 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001464
1465 if (!(s->flags & SF_ERR_MASK))
1466 s->flags |= SF_ERR_SRVTO;
1467 if (!(s->flags & SF_FINST_MASK))
1468 s->flags |= SF_FINST_H;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001469 DBG_TRACE_DEVEL("leaving on error",
1470 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001471 return 0;
1472 }
1473
Christopher Faulet9768c262018-10-22 09:34:31 +02001474 /* 3: client abort with an abortonclose */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001475 else if ((rep->flags & CF_SHUTR) && ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001476 _HA_ATOMIC_INC(&sess->fe->fe_counters.cli_aborts);
1477 _HA_ATOMIC_INC(&s->be->be_counters.cli_aborts);
William Lallemand36119de2021-03-08 15:26:48 +01001478 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001479 _HA_ATOMIC_INC(&sess->listener->counters->cli_aborts);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001480 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001481 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.cli_aborts);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001482
1483 rep->analysers &= AN_RES_FLT_END;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001484 txn->status = 400;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001485 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001486
1487 if (!(s->flags & SF_ERR_MASK))
1488 s->flags |= SF_ERR_CLICL;
1489 if (!(s->flags & SF_FINST_MASK))
1490 s->flags |= SF_FINST_H;
1491
1492 /* process_stream() will take care of the error */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001493 DBG_TRACE_DEVEL("leaving on error",
1494 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001495 return 0;
1496 }
1497
Christopher Faulet9768c262018-10-22 09:34:31 +02001498 /* 4: close from server, capture the response if the server has started to respond */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001499 else if (rep->flags & CF_SHUTR) {
Olivier Houcharda254a372019-04-05 15:30:12 +02001500 if ((si_b->flags & SI_FL_L7_RETRY) &&
1501 (s->be->retry_type & PR_RE_DISCONNECTED)) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001502 if (co_data(rep) || do_l7_retry(s, si_b) == 0) {
1503 DBG_TRACE_DEVEL("leaving on L7 retry",
1504 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharda254a372019-04-05 15:30:12 +02001505 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001506 }
Olivier Houcharda254a372019-04-05 15:30:12 +02001507 }
1508
Olivier Houchard6db16992019-05-17 15:40:49 +02001509 if (txn->flags & TX_NOT_FIRST)
1510 goto abort_keep_alive;
1511
Willy Tarreau4781b152021-04-06 13:53:36 +02001512 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001513 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001514 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001515 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001516 }
1517
Christopher Faulete0768eb2018-10-03 16:38:02 +02001518 rep->analysers &= AN_RES_FLT_END;
1519 txn->status = 502;
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001520 stream_inc_http_fail_ctr(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001521 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001522 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001523
1524 if (!(s->flags & SF_ERR_MASK))
1525 s->flags |= SF_ERR_SRVCL;
1526 if (!(s->flags & SF_FINST_MASK))
1527 s->flags |= SF_FINST_H;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001528 DBG_TRACE_DEVEL("leaving on error",
1529 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001530 return 0;
1531 }
1532
Christopher Faulet9768c262018-10-22 09:34:31 +02001533 /* 5: write error to client (we don't send any message then) */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001534 else if (rep->flags & CF_WRITE_ERROR) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001535 if (txn->flags & TX_NOT_FIRST)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001536 goto abort_keep_alive;
1537
Willy Tarreau4781b152021-04-06 13:53:36 +02001538 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001539 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001540 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001541 rep->analysers &= AN_RES_FLT_END;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001542
1543 if (!(s->flags & SF_ERR_MASK))
1544 s->flags |= SF_ERR_CLICL;
1545 if (!(s->flags & SF_FINST_MASK))
1546 s->flags |= SF_FINST_H;
1547
1548 /* process_stream() will take care of the error */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001549 DBG_TRACE_DEVEL("leaving on error",
1550 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001551 return 0;
1552 }
1553
1554 channel_dont_close(rep);
1555 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001556 DBG_TRACE_DEVEL("waiting for more data",
1557 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001558 return 0;
1559 }
1560
1561 /* More interesting part now : we know that we have a complete
1562 * response which at least looks like HTTP. We have an indicator
1563 * of each header's length, so we can parse them quickly.
1564 */
Christopher Faulet29f17582019-05-23 11:03:26 +02001565 BUG_ON(htx_get_first_type(htx) != HTX_BLK_RES_SL);
Christopher Faulet297fbb42019-05-13 14:41:27 +02001566 sl = http_get_stline(htx);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001567
Christopher Faulet5b82cc52020-10-12 15:18:50 +02001568 if ((si_b->flags & SI_FL_L7_RETRY) &&
1569 l7_status_match(s->be, sl->info.res.status) &&
1570 do_l7_retry(s, si_b) == 0) {
1571 DBG_TRACE_DEVEL("leaving on L7 retry", STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
1572 return 0;
1573 }
1574 b_free(&s->si[1].l7_buffer);
1575
1576 msg->msg_state = HTTP_MSG_BODY;
1577
Christopher Faulet9768c262018-10-22 09:34:31 +02001578 /* 0: we might have to print this header in debug mode */
1579 if (unlikely((global.mode & MODE_DEBUG) &&
1580 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
1581 int32_t pos;
1582
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001583 http_debug_stline("srvrep", s, sl);
Christopher Faulet9768c262018-10-22 09:34:31 +02001584
Christopher Fauleta3f15502019-05-13 15:27:23 +02001585 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001586 struct htx_blk *blk = htx_get_blk(htx, pos);
1587 enum htx_blk_type type = htx_get_blk_type(blk);
1588
1589 if (type == HTX_BLK_EOH)
1590 break;
1591 if (type != HTX_BLK_HDR)
1592 continue;
1593
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001594 http_debug_hdr("srvhdr", s,
1595 htx_get_blk_name(htx, blk),
1596 htx_get_blk_value(htx, blk));
Christopher Faulet9768c262018-10-22 09:34:31 +02001597 }
1598 }
1599
Christopher Faulet03599112018-11-27 11:21:21 +01001600 /* 1: get the status code and the version. Also set HTTP flags */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001601 txn->status = sl->info.res.status;
Christopher Faulet03599112018-11-27 11:21:21 +01001602 if (sl->flags & HTX_SL_F_VER_11)
Christopher Faulet9768c262018-10-22 09:34:31 +02001603 msg->flags |= HTTP_MSGF_VER_11;
Christopher Faulet03599112018-11-27 11:21:21 +01001604 if (sl->flags & HTX_SL_F_XFER_LEN) {
1605 msg->flags |= HTTP_MSGF_XFER_LEN;
Christopher Faulet2a408542020-11-20 14:22:37 +01001606 if (sl->flags & HTX_SL_F_CLEN)
1607 msg->flags |= HTTP_MSGF_CNT_LEN;
1608 else if (sl->flags & HTX_SL_F_CHNK)
1609 msg->flags |= HTTP_MSGF_TE_CHNK;
Christopher Faulet03599112018-11-27 11:21:21 +01001610 }
Christopher Faulet2a408542020-11-20 14:22:37 +01001611 if (sl->flags & HTX_SL_F_BODYLESS)
1612 msg->flags |= HTTP_MSGF_BODYLESS;
Christopher Faulet576c3582021-01-08 15:53:01 +01001613 if (sl->flags & HTX_SL_F_CONN_UPG)
1614 msg->flags |= HTTP_MSGF_CONN_UPG;
Christopher Faulet9768c262018-10-22 09:34:31 +02001615
1616 n = txn->status / 100;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001617 if (n < 1 || n > 5)
1618 n = 0;
Christopher Faulet9768c262018-10-22 09:34:31 +02001619
Christopher Faulete0768eb2018-10-03 16:38:02 +02001620 /* when the client triggers a 4xx from the server, it's most often due
1621 * to a missing object or permission. These events should be tracked
1622 * because if they happen often, it may indicate a brute force or a
1623 * vulnerability scan.
1624 */
1625 if (n == 4)
1626 stream_inc_http_err_ctr(s);
1627
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001628 if (n == 5 && txn->status != 501 && txn->status != 505)
1629 stream_inc_http_fail_ctr(s);
1630
Marcin Deranek3c27dda2020-05-15 18:32:51 +02001631 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001632 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.p.http.rsp[n]);
1633 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.p.http.cum_req);
Marcin Deranek3c27dda2020-05-15 18:32:51 +02001634 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001635
Christopher Faulete0768eb2018-10-03 16:38:02 +02001636 /* Adjust server's health based on status code. Note: status codes 501
1637 * and 505 are triggered on demand by client request, so we must not
1638 * count them as server failures.
1639 */
1640 if (objt_server(s->target)) {
1641 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001642 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_OK);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001643 else
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001644 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_STS);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001645 }
1646
1647 /*
1648 * We may be facing a 100-continue response, or any other informational
1649 * 1xx response which is non-final, in which case this is not the right
1650 * response, and we're waiting for the next one. Let's allow this response
1651 * to go to the client and wait for the next one. There's an exception for
1652 * 101 which is used later in the code to switch protocols.
1653 */
1654 if (txn->status < 200 &&
1655 (txn->status == 100 || txn->status >= 102)) {
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001656 FLT_STRM_CB(s, flt_http_reset(s, msg));
Christopher Faulet421e7692019-06-13 11:16:45 +02001657 htx->first = channel_htx_fwd_headers(rep, htx);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001658 msg->msg_state = HTTP_MSG_RPBEFORE;
Christopher Faulet3499f622019-09-03 15:23:54 +02001659 msg->flags = 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001660 txn->status = 0;
1661 s->logs.t_data = -1; /* was not a response yet */
Christopher Faulet7d518452020-08-31 11:07:07 +02001662 rep->flags |= CF_SEND_DONTWAIT; /* Send ASAP informational messages */
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001663 goto next_one;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001664 }
1665
Christopher Faulet6e6c7b12021-01-08 16:02:05 +01001666 /* A 101-switching-protocols must contains a Connection header with the
1667 * "upgrade" option and the request too. It means both are agree to
1668 * upgrade. It is not so strict because there is no test on the Upgrade
1669 * header content. But it is probably stronger enough for now.
1670 */
1671 if (txn->status == 101 &&
1672 (!(txn->req.flags & HTTP_MSGF_CONN_UPG) || !(txn->rsp.flags & HTTP_MSGF_CONN_UPG)))
1673 goto return_bad_res;
1674
Christopher Faulete0768eb2018-10-03 16:38:02 +02001675 /*
1676 * 2: check for cacheability.
1677 */
1678
1679 switch (txn->status) {
1680 case 200:
1681 case 203:
1682 case 204:
1683 case 206:
1684 case 300:
1685 case 301:
1686 case 404:
1687 case 405:
1688 case 410:
1689 case 414:
1690 case 501:
1691 break;
1692 default:
1693 /* RFC7231#6.1:
1694 * Responses with status codes that are defined as
1695 * cacheable by default (e.g., 200, 203, 204, 206,
1696 * 300, 301, 404, 405, 410, 414, and 501 in this
1697 * specification) can be reused by a cache with
1698 * heuristic expiration unless otherwise indicated
1699 * by the method definition or explicit cache
1700 * controls [RFC7234]; all other status codes are
1701 * not cacheable by default.
1702 */
1703 txn->flags &= ~(TX_CACHEABLE | TX_CACHE_COOK);
1704 break;
1705 }
1706
1707 /*
1708 * 3: we may need to capture headers
1709 */
1710 s->logs.logwait &= ~LW_RESP;
1711 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->res_cap))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001712 http_capture_headers(htx, s->res_cap, sess->fe->rsp_cap);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001713
Christopher Faulet9768c262018-10-22 09:34:31 +02001714 /* Skip parsing if no content length is possible. */
Christopher Fauletc75668e2020-12-07 18:10:32 +01001715 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status >= 200 && txn->status < 300) ||
Christopher Faulete0768eb2018-10-03 16:38:02 +02001716 txn->status == 101)) {
1717 /* Either we've established an explicit tunnel, or we're
1718 * switching the protocol. In both cases, we're very unlikely
1719 * to understand the next protocols. We have to switch to tunnel
1720 * mode, so that we transfer the request and responses then let
1721 * this protocol pass unmodified. When we later implement specific
1722 * parsers for such protocols, we'll want to check the Upgrade
1723 * header which contains information about that protocol for
1724 * responses with status 101 (eg: see RFC2817 about TLS).
1725 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02001726 txn->flags |= TX_CON_WANT_TUN;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001727 }
1728
Christopher Faulet61608322018-11-23 16:23:45 +01001729 /* check for NTML authentication headers in 401 (WWW-Authenticate) and
1730 * 407 (Proxy-Authenticate) responses and set the connection to private
1731 */
1732 srv_conn = cs_conn(objt_cs(s->si[1].end));
1733 if (srv_conn) {
1734 struct ist hdr;
1735 struct http_hdr_ctx ctx;
1736
1737 if (txn->status == 401)
1738 hdr = ist("WWW-Authenticate");
1739 else if (txn->status == 407)
1740 hdr = ist("Proxy-Authenticate");
1741 else
1742 goto end;
1743
1744 ctx.blk = NULL;
1745 while (http_find_header(htx, hdr, &ctx, 0)) {
Willy Tarreauf1dcced2020-05-07 19:27:02 +02001746 /* If www-authenticate contains "Negotiate", "Nego2", or "NTLM",
1747 * possibly followed by blanks and a base64 string, the connection
1748 * is private. Since it's a mess to deal with, we only check for
1749 * values starting with "NTLM" or "Nego". Note that often multiple
1750 * headers are sent by the server there.
1751 */
1752 if ((ctx.value.len >= 4 && strncasecmp(ctx.value.ptr, "Nego", 4) == 0) ||
Willy Tarreau49a1d282020-05-07 19:10:15 +02001753 (ctx.value.len >= 4 && strncasecmp(ctx.value.ptr, "NTLM", 4) == 0)) {
Olivier Houchard250031e2019-05-29 15:01:50 +02001754 sess->flags |= SESS_FL_PREFER_LAST;
Christopher Faulet08016ab2020-07-01 16:10:06 +02001755 conn_set_owner(srv_conn, sess, NULL);
Christopher Faulet21ddc742020-07-01 15:26:14 +02001756 conn_set_private(srv_conn);
Ilya Shipitsin6b79f382020-07-23 00:32:55 +05001757 /* If it fail now, the same will be done in mux->detach() callback */
Christopher Faulet08016ab2020-07-01 16:10:06 +02001758 session_add_conn(srv_conn->owner, srv_conn, srv_conn->target);
Willy Tarreauf1dcced2020-05-07 19:27:02 +02001759 break;
Olivier Houchard250031e2019-05-29 15:01:50 +02001760 }
Christopher Faulet61608322018-11-23 16:23:45 +01001761 }
1762 }
1763
1764 end:
Christopher Faulete0768eb2018-10-03 16:38:02 +02001765 /* we want to have the response time before we start processing it */
1766 s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now);
1767
1768 /* end of job, return OK */
1769 rep->analysers &= ~an_bit;
1770 rep->analyse_exp = TICK_ETERNITY;
1771 channel_auto_close(rep);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001772 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001773 return 1;
1774
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001775 return_int_err:
Willy Tarreau4781b152021-04-06 13:53:36 +02001776 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
1777 _HA_ATOMIC_INC(&s->be->be_counters.internal_errors);
William Lallemand36119de2021-03-08 15:26:48 +01001778 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001779 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Fauletb8a53712019-12-16 11:29:38 +01001780 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001781 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.internal_errors);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001782 txn->status = 500;
1783 if (!(s->flags & SF_ERR_MASK))
1784 s->flags |= SF_ERR_INTERNAL;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001785 goto return_prx_cond;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001786
1787 return_bad_res:
Willy Tarreau4781b152021-04-06 13:53:36 +02001788 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Faulet47365272018-10-31 17:40:50 +01001789 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001790 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001791 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Christopher Faulet47365272018-10-31 17:40:50 +01001792 }
Olivier Houcharde3249a92019-05-03 23:01:47 +02001793 if ((s->be->retry_type & PR_RE_JUNK_REQUEST) &&
Olivier Houchardad26d8d2019-05-10 17:48:28 +02001794 (si_b->flags & SI_FL_L7_RETRY) &&
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001795 do_l7_retry(s, si_b) == 0) {
1796 DBG_TRACE_DEVEL("leaving on L7 retry",
1797 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharde3249a92019-05-03 23:01:47 +02001798 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001799 }
Christopher Faulet47365272018-10-31 17:40:50 +01001800 txn->status = 502;
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001801 stream_inc_http_fail_ctr(s);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001802 /* fall through */
1803
Christopher Fauletb8a53712019-12-16 11:29:38 +01001804 return_prx_cond:
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001805 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet47365272018-10-31 17:40:50 +01001806
1807 if (!(s->flags & SF_ERR_MASK))
1808 s->flags |= SF_ERR_PRXCOND;
1809 if (!(s->flags & SF_FINST_MASK))
1810 s->flags |= SF_FINST_H;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001811
1812 s->si[1].flags |= SI_FL_NOLINGER;
1813 rep->analysers &= AN_RES_FLT_END;
Christopher Faulete58c0002020-03-02 16:21:01 +01001814 s->req.analysers &= AN_REQ_FLT_END;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001815 rep->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001816 DBG_TRACE_DEVEL("leaving on error",
1817 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulet47365272018-10-31 17:40:50 +01001818 return 0;
1819
Christopher Faulete0768eb2018-10-03 16:38:02 +02001820 abort_keep_alive:
1821 /* A keep-alive request to the server failed on a network error.
1822 * The client is required to retry. We need to close without returning
1823 * any other information so that the client retries.
1824 */
1825 txn->status = 0;
1826 rep->analysers &= AN_RES_FLT_END;
1827 s->req.analysers &= AN_REQ_FLT_END;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001828 s->logs.logwait = 0;
1829 s->logs.level = 0;
1830 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001831 http_reply_and_close(s, txn->status, NULL);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001832 DBG_TRACE_DEVEL("leaving by closing K/A connection",
1833 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001834 return 0;
1835}
1836
1837/* This function performs all the processing enabled for the current response.
1838 * It normally returns 1 unless it wants to break. It relies on buffers flags,
1839 * and updates s->res.analysers. It might make sense to explode it into several
1840 * other functions. It works like process_request (see indications above).
1841 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001842int http_process_res_common(struct stream *s, struct channel *rep, int an_bit, struct proxy *px)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001843{
1844 struct session *sess = s->sess;
1845 struct http_txn *txn = s->txn;
1846 struct http_msg *msg = &txn->rsp;
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001847 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001848 struct proxy *cur_proxy;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001849 enum rule_result ret = HTTP_RULE_RES_CONT;
1850
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001851 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
1852 return 0;
Christopher Faulet9768c262018-10-22 09:34:31 +02001853
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001854 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001855
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001856 htx = htxbuf(&rep->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001857
1858 /* The stats applet needs to adjust the Connection header but we don't
1859 * apply any filter there.
1860 */
1861 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
1862 rep->analysers &= ~an_bit;
1863 rep->analyse_exp = TICK_ETERNITY;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001864 goto end;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001865 }
1866
1867 /*
1868 * We will have to evaluate the filters.
1869 * As opposed to version 1.2, now they will be evaluated in the
1870 * filters order and not in the header order. This means that
1871 * each filter has to be validated among all headers.
1872 *
1873 * Filters are tried with ->be first, then with ->fe if it is
1874 * different from ->be.
1875 *
1876 * Maybe we are in resume condiion. In this case I choose the
1877 * "struct proxy" which contains the rule list matching the resume
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05001878 * pointer. If none of these "struct proxy" match, I initialise
Christopher Faulete0768eb2018-10-03 16:38:02 +02001879 * the process with the first one.
1880 *
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05001881 * In fact, I check only correspondence between the current list
Christopher Faulete0768eb2018-10-03 16:38:02 +02001882 * pointer and the ->fe rule list. If it doesn't match, I initialize
1883 * the loop with the ->be.
1884 */
1885 if (s->current_rule_list == &sess->fe->http_res_rules)
1886 cur_proxy = sess->fe;
1887 else
1888 cur_proxy = s->be;
1889 while (1) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001890 /* evaluate http-response rules */
1891 if (ret == HTTP_RULE_RES_CONT) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001892 ret = http_res_get_intercept_rule(cur_proxy, &cur_proxy->http_res_rules, s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001893
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001894 switch (ret) {
1895 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
1896 goto return_prx_yield;
1897
1898 case HTTP_RULE_RES_CONT:
1899 case HTTP_RULE_RES_STOP: /* nothing to do */
1900 break;
1901
1902 case HTTP_RULE_RES_DENY: /* deny or tarpit */
1903 goto deny;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001904
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001905 case HTTP_RULE_RES_ABRT: /* abort request, response already sent */
1906 goto return_prx_cond;
1907
1908 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
Christopher Fauletb8a53712019-12-16 11:29:38 +01001909 goto done;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001910
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001911 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
1912 goto return_bad_res;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001913
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001914 case HTTP_RULE_RES_ERROR: /* failed with a bad request */
1915 goto return_int_err;
1916 }
1917
1918 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001919
Christopher Faulete0768eb2018-10-03 16:38:02 +02001920 /* check whether we're already working on the frontend */
1921 if (cur_proxy == sess->fe)
1922 break;
1923 cur_proxy = sess->fe;
1924 }
1925
Christopher Faulete0768eb2018-10-03 16:38:02 +02001926 /* OK that's all we can do for 1xx responses */
1927 if (unlikely(txn->status < 200 && txn->status != 101))
Christopher Fauletf2824e62018-10-01 12:12:37 +02001928 goto end;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001929
1930 /*
1931 * Now check for a server cookie.
1932 */
1933 if (s->be->cookie_name || sess->fe->capture_name || (s->be->options & PR_O_CHK_CACHE))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001934 http_manage_server_side_cookies(s, rep);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001935
1936 /*
1937 * Check for cache-control or pragma headers if required.
1938 */
1939 if ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001940 http_check_response_for_cacheability(s, rep);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001941
1942 /*
1943 * Add server cookie in the response if needed
1944 */
1945 if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) &&
1946 !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) &&
1947 (!(s->flags & SF_DIRECT) ||
1948 ((s->be->cookie_maxidle || txn->cookie_last_date) &&
1949 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
1950 (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
1951 (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
1952 (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
1953 !(s->flags & SF_IGNORE_PRST)) {
1954 /* the server is known, it's not the one the client requested, or the
1955 * cookie's last seen date needs to be refreshed. We have to
1956 * insert a set-cookie here, except if we want to insert only on POST
1957 * requests and this one isn't. Note that servers which don't have cookies
1958 * (eg: some backup servers) will return a full cookie removal request.
1959 */
1960 if (!objt_server(s->target)->cookie) {
1961 chunk_printf(&trash,
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001962 "%s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
Christopher Faulete0768eb2018-10-03 16:38:02 +02001963 s->be->cookie_name);
1964 }
1965 else {
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001966 chunk_printf(&trash, "%s=%s", s->be->cookie_name, objt_server(s->target)->cookie);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001967
1968 if (s->be->cookie_maxidle || s->be->cookie_maxlife) {
1969 /* emit last_date, which is mandatory */
1970 trash.area[trash.data++] = COOKIE_DELIM_DATE;
1971 s30tob64((date.tv_sec+3) >> 2,
1972 trash.area + trash.data);
1973 trash.data += 5;
1974
1975 if (s->be->cookie_maxlife) {
1976 /* emit first_date, which is either the original one or
1977 * the current date.
1978 */
1979 trash.area[trash.data++] = COOKIE_DELIM_DATE;
1980 s30tob64(txn->cookie_first_date ?
1981 txn->cookie_first_date >> 2 :
1982 (date.tv_sec+3) >> 2,
1983 trash.area + trash.data);
1984 trash.data += 5;
1985 }
1986 }
1987 chunk_appendf(&trash, "; path=/");
1988 }
1989
1990 if (s->be->cookie_domain)
1991 chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain);
1992
1993 if (s->be->ck_opts & PR_CK_HTTPONLY)
1994 chunk_appendf(&trash, "; HttpOnly");
1995
1996 if (s->be->ck_opts & PR_CK_SECURE)
1997 chunk_appendf(&trash, "; Secure");
1998
Christopher Faulet2f533902020-01-21 11:06:48 +01001999 if (s->be->cookie_attrs)
2000 chunk_appendf(&trash, "; %s", s->be->cookie_attrs);
2001
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002002 if (unlikely(!http_add_header(htx, ist("Set-Cookie"), ist2(trash.area, trash.data))))
Christopher Fauletb8a53712019-12-16 11:29:38 +01002003 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002004
2005 txn->flags &= ~TX_SCK_MASK;
2006 if (__objt_server(s->target)->cookie && (s->flags & SF_DIRECT))
2007 /* the server did not change, only the date was updated */
2008 txn->flags |= TX_SCK_UPDATED;
2009 else
2010 txn->flags |= TX_SCK_INSERTED;
2011
2012 /* Here, we will tell an eventual cache on the client side that we don't
2013 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
2014 * Some caches understand the correct form: 'no-cache="set-cookie"', but
2015 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
2016 */
2017 if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
2018
2019 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
2020
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002021 if (unlikely(!http_add_header(htx, ist("Cache-control"), ist("private"))))
Christopher Fauletb8a53712019-12-16 11:29:38 +01002022 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002023 }
2024 }
2025
2026 /*
2027 * Check if result will be cacheable with a cookie.
2028 * We'll block the response if security checks have caught
2029 * nasty things such as a cacheable cookie.
2030 */
2031 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
2032 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
2033 (s->be->options & PR_O_CHK_CACHE)) {
2034 /* we're in presence of a cacheable response containing
2035 * a set-cookie header. We'll block it as requested by
2036 * the 'checkcache' option, and send an alert.
2037 */
Christopher Faulete0768eb2018-10-03 16:38:02 +02002038 ha_alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
2039 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
2040 send_log(s->be, LOG_ALERT,
2041 "Blocking cacheable cookie in response from instance %s, server %s.\n",
2042 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
Christopher Fauletb8a53712019-12-16 11:29:38 +01002043 goto deny;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002044 }
2045
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002046 end:
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002047 /*
2048 * Evaluate after-response rules before forwarding the response. rules
2049 * from the backend are evaluated first, then one from the frontend if
2050 * it differs.
2051 */
2052 if (!http_eval_after_res_rules(s))
2053 goto return_int_err;
2054
Christopher Fauletc2ac5e42021-03-08 18:20:09 +01002055 /* Filter the response headers if there are filters attached to the
2056 * stream.
2057 */
2058 if (HAS_FILTERS(s))
2059 rep->analysers |= AN_RES_FLT_HTTP_HDRS;
2060
Christopher Faulete0768eb2018-10-03 16:38:02 +02002061 /* Always enter in the body analyzer */
2062 rep->analysers &= ~AN_RES_FLT_XFER_DATA;
2063 rep->analysers |= AN_RES_HTTP_XFER_BODY;
2064
2065 /* if the user wants to log as soon as possible, without counting
2066 * bytes from the server, then this is the right moment. We have
2067 * to temporarily assign bytes_out to log what we currently have.
2068 */
2069 if (!LIST_ISEMPTY(&sess->fe->logformat) && !(s->logs.logwait & LW_BYTES)) {
2070 s->logs.t_close = s->logs.t_data; /* to get a valid end date */
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002071 s->logs.bytes_out = htx->data;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002072 s->do_log(s);
2073 s->logs.bytes_out = 0;
2074 }
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002075
Christopher Fauletb8a53712019-12-16 11:29:38 +01002076 done:
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002077 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002078 rep->analysers &= ~an_bit;
2079 rep->analyse_exp = TICK_ETERNITY;
2080 return 1;
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002081
Christopher Fauletb8a53712019-12-16 11:29:38 +01002082 deny:
Willy Tarreau4781b152021-04-06 13:53:36 +02002083 _HA_ATOMIC_INC(&sess->fe->fe_counters.denied_resp);
2084 _HA_ATOMIC_INC(&s->be->be_counters.denied_resp);
William Lallemand36119de2021-03-08 15:26:48 +01002085 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02002086 _HA_ATOMIC_INC(&sess->listener->counters->denied_resp);
Christopher Fauleta08546b2019-12-16 16:07:34 +01002087 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02002088 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.denied_resp);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002089 goto return_prx_err;
2090
2091 return_int_err:
2092 txn->status = 500;
2093 if (!(s->flags & SF_ERR_MASK))
2094 s->flags |= SF_ERR_INTERNAL;
Willy Tarreau4781b152021-04-06 13:53:36 +02002095 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
2096 _HA_ATOMIC_INC(&s->be->be_counters.internal_errors);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002097 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02002098 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.internal_errors);
Christopher Fauletcff0f732019-12-16 16:13:44 +01002099 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02002100 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.internal_errors);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002101 goto return_prx_err;
2102
2103 return_bad_res:
2104 txn->status = 502;
Willy Tarreau826f3ab2021-02-10 12:07:15 +01002105 stream_inc_http_fail_ctr(s);
Willy Tarreau4781b152021-04-06 13:53:36 +02002106 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Fauleta20a6532020-02-05 10:16:41 +01002107 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02002108 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Christopher Fauleta20a6532020-02-05 10:16:41 +01002109 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_RSP);
2110 }
Christopher Fauletb8a53712019-12-16 11:29:38 +01002111 /* fall through */
2112
2113 return_prx_err:
2114 http_reply_and_close(s, txn->status, http_error_message(s));
2115 /* fall through */
2116
2117 return_prx_cond:
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002118 s->logs.t_data = -1; /* was not a valid response */
2119 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002120
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002121 if (!(s->flags & SF_ERR_MASK))
2122 s->flags |= SF_ERR_PRXCOND;
2123 if (!(s->flags & SF_FINST_MASK))
2124 s->flags |= SF_FINST_H;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002125
Christopher Faulete58c0002020-03-02 16:21:01 +01002126 rep->analysers &= AN_RES_FLT_END;
2127 s->req.analysers &= AN_REQ_FLT_END;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002128 rep->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002129 DBG_TRACE_DEVEL("leaving on error",
2130 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002131 return 0;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002132
2133 return_prx_yield:
2134 channel_dont_close(rep);
2135 DBG_TRACE_DEVEL("waiting for more data",
2136 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
2137 return 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002138}
2139
2140/* This function is an analyser which forwards response body (including chunk
2141 * sizes if any). It is called as soon as we must forward, even if we forward
2142 * zero byte. The only situation where it must not be called is when we're in
2143 * tunnel mode and we want to forward till the close. It's used both to forward
2144 * remaining data and to resync after end of body. It expects the msg_state to
2145 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
2146 * read more data, or 1 once we can go on with next request or end the stream.
2147 *
2148 * It is capable of compressing response data both in content-length mode and
2149 * in chunked mode. The state machines follows different flows depending on
2150 * whether content-length and chunked modes are used, since there are no
2151 * trailers in content-length :
2152 *
2153 * chk-mode cl-mode
2154 * ,----- BODY -----.
2155 * / \
2156 * V size > 0 V chk-mode
2157 * .--> SIZE -------------> DATA -------------> CRLF
2158 * | | size == 0 | last byte |
2159 * | v final crlf v inspected |
2160 * | TRAILERS -----------> DONE |
2161 * | |
2162 * `----------------------------------------------'
2163 *
2164 * Compression only happens in the DATA state, and must be flushed in final
2165 * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding
2166 * is performed at once on final states for all bytes parsed, or when leaving
2167 * on missing data.
2168 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002169int http_response_forward_body(struct stream *s, struct channel *res, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +02002170{
2171 struct session *sess = s->sess;
2172 struct http_txn *txn = s->txn;
2173 struct http_msg *msg = &s->txn->rsp;
Christopher Faulet9768c262018-10-22 09:34:31 +02002174 struct htx *htx;
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002175 int ret;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002176
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002177 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002178
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002179 htx = htxbuf(&res->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002180
Christopher Fauletb9a92f32019-09-09 10:15:21 +02002181 if (htx->flags & HTX_FL_PARSING_ERROR)
2182 goto return_bad_res;
2183 if (htx->flags & HTX_FL_PROCESSING_ERROR)
2184 goto return_int_err;
2185
Christopher Faulete0768eb2018-10-03 16:38:02 +02002186 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Christopher Fauletf2824e62018-10-01 12:12:37 +02002187 ((res->flags & CF_SHUTW) && (res->to_forward || co_data(res)))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02002188 /* Output closed while we were sending data. We must abort and
2189 * wake the other side up.
2190 */
Christopher Faulete0768eb2018-10-03 16:38:02 +02002191 msg->msg_state = HTTP_MSG_ERROR;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002192 http_end_response(s);
2193 http_end_request(s);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002194 DBG_TRACE_DEVEL("leaving on error",
2195 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002196 return 1;
2197 }
2198
Christopher Faulet9768c262018-10-22 09:34:31 +02002199 if (msg->msg_state == HTTP_MSG_BODY)
2200 msg->msg_state = HTTP_MSG_DATA;
2201
Christopher Faulete0768eb2018-10-03 16:38:02 +02002202 /* in most states, we should abort in case of early close */
2203 channel_auto_close(res);
2204
Christopher Faulete0768eb2018-10-03 16:38:02 +02002205 if (res->to_forward) {
Christopher Faulet66af0b22019-03-22 14:54:52 +01002206 if (res->to_forward == CHN_INFINITE_FORWARD) {
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002207 if (res->flags & CF_EOI)
2208 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet66af0b22019-03-22 14:54:52 +01002209 }
2210 else {
2211 /* We can't process the buffer's contents yet */
2212 res->flags |= CF_WAKE_WRITE;
2213 goto missing_data_or_waiting;
2214 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02002215 }
2216
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002217 if (msg->msg_state >= HTTP_MSG_ENDING)
2218 goto ending;
2219
Christopher Fauletc75668e2020-12-07 18:10:32 +01002220 if ((txn->meth == HTTP_METH_CONNECT && txn->status >= 200 && txn->status < 300) || txn->status == 101 ||
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002221 (!(msg->flags & HTTP_MSGF_XFER_LEN) && !HAS_RSP_DATA_FILTERS(s))) {
2222 msg->msg_state = HTTP_MSG_ENDING;
2223 goto ending;
2224 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002225
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002226 /* Forward input data. We get it by removing all outgoing data not
2227 * forwarded yet from HTX data size. If there are some data filters, we
2228 * let them decide the amount of data to forward.
Christopher Faulet9768c262018-10-22 09:34:31 +02002229 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002230 if (HAS_RSP_DATA_FILTERS(s)) {
2231 ret = flt_http_payload(s, msg, htx->data);
2232 if (ret < 0)
2233 goto return_bad_res;
Christopher Faulet421e7692019-06-13 11:16:45 +02002234 c_adv(res, ret);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002235 }
2236 else {
Christopher Faulet421e7692019-06-13 11:16:45 +02002237 c_adv(res, htx->data - co_data(res));
Christopher Faulet66af0b22019-03-22 14:54:52 +01002238 if (msg->flags & HTTP_MSGF_XFER_LEN)
2239 channel_htx_forward_forever(res, htx);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002240 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002241
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002242 if (htx->data != co_data(res))
2243 goto missing_data_or_waiting;
2244
2245 if (!(msg->flags & HTTP_MSGF_XFER_LEN) && res->flags & CF_SHUTR) {
2246 msg->msg_state = HTTP_MSG_ENDING;
2247 goto ending;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002248 }
2249
Christopher Faulet9768c262018-10-22 09:34:31 +02002250 /* Check if the end-of-message is reached and if so, switch the message
Christopher Fauletd20fdb02019-06-13 16:43:22 +02002251 * in HTTP_MSG_ENDING state. Then if all data was marked to be
2252 * forwarded, set the state to HTTP_MSG_DONE.
Christopher Faulet9768c262018-10-22 09:34:31 +02002253 */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002254 if (!(htx->flags & HTX_FL_EOM))
Christopher Faulet9768c262018-10-22 09:34:31 +02002255 goto missing_data_or_waiting;
2256
Christopher Fauletd20fdb02019-06-13 16:43:22 +02002257 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet9768c262018-10-22 09:34:31 +02002258
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002259 ending:
Christopher Faulet2151cdd2020-07-22 16:34:59 +02002260 res->flags &= ~CF_EXPECT_MORE; /* no more data are expected */
2261
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002262 /* other states, ENDING...TUNNEL */
2263 if (msg->msg_state >= HTTP_MSG_DONE)
2264 goto done;
Christopher Faulet9768c262018-10-22 09:34:31 +02002265
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002266 if (HAS_RSP_DATA_FILTERS(s)) {
2267 ret = flt_http_end(s, msg);
2268 if (ret <= 0) {
2269 if (!ret)
2270 goto missing_data_or_waiting;
2271 goto return_bad_res;
2272 }
2273 }
2274
Christopher Fauletc75668e2020-12-07 18:10:32 +01002275 if ((txn->meth == HTTP_METH_CONNECT && txn->status >= 200 && txn->status < 300) || txn->status == 101 ||
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002276 !(msg->flags & HTTP_MSGF_XFER_LEN)) {
2277 msg->msg_state = HTTP_MSG_TUNNEL;
2278 goto ending;
2279 }
2280 else {
2281 msg->msg_state = HTTP_MSG_DONE;
2282 res->to_forward = 0;
2283 }
2284
2285 done:
2286
2287 channel_dont_close(res);
2288
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002289 http_end_response(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002290 if (!(res->analysers & an_bit)) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002291 http_end_request(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002292 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
2293 if (res->flags & CF_SHUTW) {
2294 /* response errors are most likely due to the
2295 * client aborting the transfer. */
Christopher Faulet93e02d82019-03-08 14:18:50 +01002296 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002297 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02002298 goto return_bad_res;
2299 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002300 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002301 return 1;
2302 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002303 DBG_TRACE_DEVEL("waiting for the end of the HTTP txn",
2304 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002305 return 0;
2306
2307 missing_data_or_waiting:
2308 if (res->flags & CF_SHUTW)
Christopher Faulet93e02d82019-03-08 14:18:50 +01002309 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002310
2311 /* stop waiting for data if the input is closed before the end. If the
2312 * client side was already closed, it means that the client has aborted,
2313 * so we don't want to count this as a server abort. Otherwise it's a
2314 * server abort.
2315 */
Christopher Fauletd20fdb02019-06-13 16:43:22 +02002316 if (msg->msg_state < HTTP_MSG_ENDING && res->flags & CF_SHUTR) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02002317 if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))
Christopher Faulet93e02d82019-03-08 14:18:50 +01002318 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002319 /* If we have some pending data, we continue the processing */
Christopher Faulet93e02d82019-03-08 14:18:50 +01002320 if (htx_is_empty(htx))
2321 goto return_srv_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002322 }
2323
Christopher Faulete0768eb2018-10-03 16:38:02 +02002324 /* When TE: chunked is used, we need to get there again to parse
2325 * remaining chunks even if the server has closed, so we don't want to
Christopher Faulet9768c262018-10-22 09:34:31 +02002326 * set CF_DONTCLOSE. Similarly when there is a content-leng or if there
2327 * are filters registered on the stream, we don't want to forward a
2328 * close
Christopher Faulete0768eb2018-10-03 16:38:02 +02002329 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002330 if ((msg->flags & HTTP_MSGF_XFER_LEN) || HAS_RSP_DATA_FILTERS(s))
Christopher Faulete0768eb2018-10-03 16:38:02 +02002331 channel_dont_close(res);
2332
2333 /* We know that more data are expected, but we couldn't send more that
2334 * what we did. So we always set the CF_EXPECT_MORE flag so that the
2335 * system knows it must not set a PUSH on this first part. Interactive
2336 * modes are already handled by the stream sock layer. We must not do
2337 * this in content-length mode because it could present the MSG_MORE
2338 * flag with the last block of forwarded data, which would cause an
2339 * additional delay to be observed by the receiver.
2340 */
Christopher Faulet2151cdd2020-07-22 16:34:59 +02002341 if (HAS_RSP_DATA_FILTERS(s))
Christopher Faulete0768eb2018-10-03 16:38:02 +02002342 res->flags |= CF_EXPECT_MORE;
2343
2344 /* the stream handler will take care of timeouts and errors */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002345 DBG_TRACE_DEVEL("waiting for more data to forward",
2346 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002347 return 0;
2348
Christopher Faulet93e02d82019-03-08 14:18:50 +01002349 return_srv_abort:
Willy Tarreau4781b152021-04-06 13:53:36 +02002350 _HA_ATOMIC_INC(&sess->fe->fe_counters.srv_aborts);
2351 _HA_ATOMIC_INC(&s->be->be_counters.srv_aborts);
William Lallemand36119de2021-03-08 15:26:48 +01002352 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02002353 _HA_ATOMIC_INC(&sess->listener->counters->srv_aborts);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002354 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02002355 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.srv_aborts);
Willy Tarreau826f3ab2021-02-10 12:07:15 +01002356 stream_inc_http_fail_ctr(s);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002357 if (!(s->flags & SF_ERR_MASK))
2358 s->flags |= SF_ERR_SRVCL;
2359 goto return_error;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002360
Christopher Faulet93e02d82019-03-08 14:18:50 +01002361 return_cli_abort:
Willy Tarreau4781b152021-04-06 13:53:36 +02002362 _HA_ATOMIC_INC(&sess->fe->fe_counters.cli_aborts);
2363 _HA_ATOMIC_INC(&s->be->be_counters.cli_aborts);
William Lallemand36119de2021-03-08 15:26:48 +01002364 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02002365 _HA_ATOMIC_INC(&sess->listener->counters->cli_aborts);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002366 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02002367 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.cli_aborts);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002368 if (!(s->flags & SF_ERR_MASK))
2369 s->flags |= SF_ERR_CLICL;
2370 goto return_error;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002371
Christopher Fauletb9a92f32019-09-09 10:15:21 +02002372 return_int_err:
Willy Tarreau4781b152021-04-06 13:53:36 +02002373 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
2374 _HA_ATOMIC_INC(&s->be->be_counters.internal_errors);
William Lallemand36119de2021-03-08 15:26:48 +01002375 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02002376 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002377 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02002378 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.internal_errors);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02002379 if (!(s->flags & SF_ERR_MASK))
2380 s->flags |= SF_ERR_INTERNAL;
2381 goto return_error;
2382
Christopher Faulet93e02d82019-03-08 14:18:50 +01002383 return_bad_res:
Willy Tarreau4781b152021-04-06 13:53:36 +02002384 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002385 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02002386 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002387 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_RSP);
2388 }
Willy Tarreau826f3ab2021-02-10 12:07:15 +01002389 stream_inc_http_fail_ctr(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002390 if (!(s->flags & SF_ERR_MASK))
Christopher Faulet93e02d82019-03-08 14:18:50 +01002391 s->flags |= SF_ERR_SRVCL;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002392 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +02002393
Christopher Faulet93e02d82019-03-08 14:18:50 +01002394 return_error:
Christopher Faulete0768eb2018-10-03 16:38:02 +02002395 /* don't send any error message as we're in the body */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002396 http_reply_and_close(s, txn->status, NULL);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002397 res->analysers &= AN_RES_FLT_END;
2398 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 +02002399 if (!(s->flags & SF_FINST_MASK))
2400 s->flags |= SF_FINST_D;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002401 DBG_TRACE_DEVEL("leaving on error",
2402 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002403 return 0;
2404}
2405
Christopher Fauletf2824e62018-10-01 12:12:37 +02002406/* Perform an HTTP redirect based on the information in <rule>. The function
Christopher Faulet99daf282018-11-28 22:58:13 +01002407 * returns zero on success, or zero in case of a, irrecoverable error such
Christopher Fauletf2824e62018-10-01 12:12:37 +02002408 * as too large a request to build a valid response.
2409 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002410int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn)
Christopher Fauletf2824e62018-10-01 12:12:37 +02002411{
Christopher Faulet99daf282018-11-28 22:58:13 +01002412 struct channel *req = &s->req;
2413 struct channel *res = &s->res;
2414 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01002415 struct htx_sl *sl;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002416 struct buffer *chunk;
Christopher Faulet99daf282018-11-28 22:58:13 +01002417 struct ist status, reason, location;
2418 unsigned int flags;
Christopher Faulet08e66462019-05-23 16:44:59 +02002419 int close = 0; /* Try to keep the connection alive byt default */
Christopher Fauletf2824e62018-10-01 12:12:37 +02002420
2421 chunk = alloc_trash_chunk();
Christopher Fauletb8a53712019-12-16 11:29:38 +01002422 if (!chunk) {
2423 if (!(s->flags & SF_ERR_MASK))
2424 s->flags |= SF_ERR_RESOURCE;
Christopher Faulet99daf282018-11-28 22:58:13 +01002425 goto fail;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002426 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002427
Christopher Faulet99daf282018-11-28 22:58:13 +01002428 /*
2429 * Create the location
2430 */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002431 htx = htxbuf(&req->buf);
Christopher Fauletf2824e62018-10-01 12:12:37 +02002432 switch(rule->type) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002433 case REDIRECT_TYPE_SCHEME: {
2434 struct http_hdr_ctx ctx;
2435 struct ist path, host;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002436
Christopher Faulet99daf282018-11-28 22:58:13 +01002437 host = ist("");
2438 ctx.blk = NULL;
2439 if (http_find_header(htx, ist("Host"), &ctx, 0))
2440 host = ctx.value;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002441
Christopher Faulet297fbb42019-05-13 14:41:27 +02002442 sl = http_get_stline(htx);
Christopher Faulet99daf282018-11-28 22:58:13 +01002443 path = http_get_path(htx_sl_req_uri(sl));
2444 /* build message using path */
Tim Duesterhused526372020-03-05 17:56:33 +01002445 if (isttest(path)) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002446 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2447 int qs = 0;
2448 while (qs < path.len) {
2449 if (*(path.ptr + qs) == '?') {
2450 path.len = qs;
2451 break;
2452 }
2453 qs++;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002454 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002455 }
2456 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002457 else
2458 path = ist("/");
Christopher Fauletf2824e62018-10-01 12:12:37 +02002459
Christopher Faulet99daf282018-11-28 22:58:13 +01002460 if (rule->rdr_str) { /* this is an old "redirect" rule */
2461 /* add scheme */
2462 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2463 goto fail;
2464 }
2465 else {
2466 /* add scheme with executing log format */
2467 chunk->data += build_logline(s, chunk->area + chunk->data,
2468 chunk->size - chunk->data,
2469 &rule->rdr_fmt);
2470 }
2471 /* add "://" + host + path */
2472 if (!chunk_memcat(chunk, "://", 3) ||
2473 !chunk_memcat(chunk, host.ptr, host.len) ||
2474 !chunk_memcat(chunk, path.ptr, path.len))
2475 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002476
Christopher Faulet99daf282018-11-28 22:58:13 +01002477 /* append a slash at the end of the location if needed and missing */
2478 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
2479 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
2480 if (chunk->data + 1 >= chunk->size)
2481 goto fail;
2482 chunk->area[chunk->data++] = '/';
2483 }
2484 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002485 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002486
Christopher Faulet99daf282018-11-28 22:58:13 +01002487 case REDIRECT_TYPE_PREFIX: {
2488 struct ist path;
2489
Christopher Faulet297fbb42019-05-13 14:41:27 +02002490 sl = http_get_stline(htx);
Christopher Faulet99daf282018-11-28 22:58:13 +01002491 path = http_get_path(htx_sl_req_uri(sl));
2492 /* build message using path */
Tim Duesterhused526372020-03-05 17:56:33 +01002493 if (isttest(path)) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002494 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2495 int qs = 0;
2496 while (qs < path.len) {
2497 if (*(path.ptr + qs) == '?') {
2498 path.len = qs;
2499 break;
2500 }
2501 qs++;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002502 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002503 }
2504 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002505 else
2506 path = ist("/");
Christopher Fauletf2824e62018-10-01 12:12:37 +02002507
Christopher Faulet99daf282018-11-28 22:58:13 +01002508 if (rule->rdr_str) { /* this is an old "redirect" rule */
2509 /* add prefix. Note that if prefix == "/", we don't want to
2510 * add anything, otherwise it makes it hard for the user to
2511 * configure a self-redirection.
2512 */
2513 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
2514 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2515 goto fail;
2516 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002517 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002518 else {
2519 /* add prefix with executing log format */
2520 chunk->data += build_logline(s, chunk->area + chunk->data,
2521 chunk->size - chunk->data,
2522 &rule->rdr_fmt);
2523 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002524
Christopher Faulet99daf282018-11-28 22:58:13 +01002525 /* add path */
2526 if (!chunk_memcat(chunk, path.ptr, path.len))
2527 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002528
Christopher Faulet99daf282018-11-28 22:58:13 +01002529 /* append a slash at the end of the location if needed and missing */
2530 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
2531 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
2532 if (chunk->data + 1 >= chunk->size)
2533 goto fail;
2534 chunk->area[chunk->data++] = '/';
2535 }
2536 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002537 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002538 case REDIRECT_TYPE_LOCATION:
2539 default:
2540 if (rule->rdr_str) { /* this is an old "redirect" rule */
2541 /* add location */
2542 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2543 goto fail;
2544 }
2545 else {
2546 /* add location with executing log format */
2547 chunk->data += build_logline(s, chunk->area + chunk->data,
2548 chunk->size - chunk->data,
2549 &rule->rdr_fmt);
2550 }
2551 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002552 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002553 location = ist2(chunk->area, chunk->data);
2554
2555 /*
2556 * Create the 30x response
2557 */
2558 switch (rule->code) {
2559 case 308:
2560 status = ist("308");
2561 reason = ist("Permanent Redirect");
2562 break;
2563 case 307:
2564 status = ist("307");
2565 reason = ist("Temporary Redirect");
2566 break;
2567 case 303:
2568 status = ist("303");
2569 reason = ist("See Other");
2570 break;
2571 case 301:
2572 status = ist("301");
2573 reason = ist("Moved Permanently");
2574 break;
2575 case 302:
2576 default:
2577 status = ist("302");
2578 reason = ist("Found");
2579 break;
2580 }
2581
Christopher Faulet08e66462019-05-23 16:44:59 +02002582 if (!(txn->req.flags & HTTP_MSGF_BODYLESS) && txn->req.msg_state != HTTP_MSG_DONE)
2583 close = 1;
2584
Christopher Faulet99daf282018-11-28 22:58:13 +01002585 htx = htx_from_buf(&res->buf);
Kevin Zhu96b36392020-01-07 09:42:55 +01002586 /* Trim any possible response */
2587 channel_htx_truncate(&s->res, htx);
Christopher Faulet99daf282018-11-28 22:58:13 +01002588 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
2589 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), status, reason);
2590 if (!sl)
2591 goto fail;
2592 sl->info.res.status = rule->code;
2593 s->txn->status = rule->code;
2594
Christopher Faulet08e66462019-05-23 16:44:59 +02002595 if (close && !htx_add_header(htx, ist("Connection"), ist("close")))
2596 goto fail;
2597
2598 if (!htx_add_header(htx, ist("Content-length"), ist("0")) ||
Christopher Faulet99daf282018-11-28 22:58:13 +01002599 !htx_add_header(htx, ist("Location"), location))
2600 goto fail;
2601
2602 if (rule->code == 302 || rule->code == 303 || rule->code == 307) {
2603 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")))
2604 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002605 }
2606
2607 if (rule->cookie_len) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002608 if (!htx_add_header(htx, ist("Set-Cookie"), ist2(rule->cookie_str, rule->cookie_len)))
2609 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002610 }
2611
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002612 if (!htx_add_endof(htx, HTX_BLK_EOH))
Christopher Faulet99daf282018-11-28 22:58:13 +01002613 goto fail;
2614
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002615 htx->flags |= HTX_FL_EOM;
Kevin Zhu96b36392020-01-07 09:42:55 +01002616 htx_to_buf(htx, &res->buf);
Christopher Fauleta72a7e42020-01-28 09:28:11 +01002617 if (!http_forward_proxy_resp(s, 1))
2618 goto fail;
Christopher Faulet99daf282018-11-28 22:58:13 +01002619
Christopher Faulet60b33a52020-01-28 09:18:10 +01002620 if (rule->flags & REDIRECT_FLAG_FROM_REQ) {
2621 /* let's log the request time */
2622 s->logs.tv_request = now;
2623 req->analysers &= AN_REQ_FLT_END;
Christopher Faulet99daf282018-11-28 22:58:13 +01002624
Christopher Faulet60b33a52020-01-28 09:18:10 +01002625 if (s->sess->fe == s->be) /* report it if the request was intercepted by the frontend */
Willy Tarreau4781b152021-04-06 13:53:36 +02002626 _HA_ATOMIC_INC(&s->sess->fe->fe_counters.intercepted_req);
Christopher Faulet60b33a52020-01-28 09:18:10 +01002627 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002628
2629 if (!(s->flags & SF_ERR_MASK))
2630 s->flags |= SF_ERR_LOCAL;
2631 if (!(s->flags & SF_FINST_MASK))
Christopher Faulet60b33a52020-01-28 09:18:10 +01002632 s->flags |= ((rule->flags & REDIRECT_FLAG_FROM_REQ) ? SF_FINST_R : SF_FINST_H);
Christopher Fauletf2824e62018-10-01 12:12:37 +02002633
Christopher Faulet99daf282018-11-28 22:58:13 +01002634 free_trash_chunk(chunk);
2635 return 1;
2636
2637 fail:
2638 /* If an error occurred, remove the incomplete HTTP response from the
2639 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01002640 channel_htx_truncate(res, htxbuf(&res->buf));
Christopher Fauletf2824e62018-10-01 12:12:37 +02002641 free_trash_chunk(chunk);
Christopher Faulet99daf282018-11-28 22:58:13 +01002642 return 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002643}
2644
Christopher Faulet92d34fe2019-12-17 09:20:34 +01002645/* Replace all headers matching the name <name>. The header value is replaced if
2646 * it matches the regex <re>. <str> is used for the replacement. If <full> is
2647 * set to 1, the full-line is matched and replaced. Otherwise, comma-separated
2648 * values are evaluated one by one. It returns 0 on success and -1 on error.
2649 */
2650int http_replace_hdrs(struct stream* s, struct htx *htx, struct ist name,
2651 const char *str, struct my_regex *re, int full)
Christopher Faulet72333522018-10-24 11:25:02 +02002652{
2653 struct http_hdr_ctx ctx;
2654 struct buffer *output = get_trash_chunk();
2655
Christopher Faulet72333522018-10-24 11:25:02 +02002656 ctx.blk = NULL;
Christopher Faulet92d34fe2019-12-17 09:20:34 +01002657 while (http_find_header(htx, name, &ctx, full)) {
Christopher Faulet72333522018-10-24 11:25:02 +02002658 if (!regex_exec_match2(re, ctx.value.ptr, ctx.value.len, MAX_MATCH, pmatch, 0))
2659 continue;
2660
2661 output->data = exp_replace(output->area, output->size, ctx.value.ptr, str, pmatch);
2662 if (output->data == -1)
2663 return -1;
2664 if (!http_replace_header_value(htx, &ctx, ist2(output->area, output->data)))
2665 return -1;
2666 }
2667 return 0;
2668}
2669
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002670/* This function executes one of the set-{method,path,query,uri} actions. It
2671 * takes the string from the variable 'replace' with length 'len', then modifies
2672 * the relevant part of the request line accordingly. Then it updates various
2673 * pointers to the next elements which were moved, and the total buffer length.
2674 * It finds the action to be performed in p[2], previously filled by function
2675 * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal
2676 * error, though this can be revisited when this code is finally exploited.
2677 *
2678 * 'action' can be '0' to replace method, '1' to replace path, '2' to replace
Christopher Faulet312294f2020-09-02 17:17:44 +02002679 * query string, 3 to replace uri or 4 to replace the path+query.
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002680 *
2681 * In query string case, the mark question '?' must be set at the start of the
2682 * string by the caller, event if the replacement query string is empty.
2683 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002684int http_req_replace_stline(int action, const char *replace, int len,
2685 struct proxy *px, struct stream *s)
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002686{
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002687 struct htx *htx = htxbuf(&s->req.buf);
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002688
2689 switch (action) {
2690 case 0: // method
2691 if (!http_replace_req_meth(htx, ist2(replace, len)))
2692 return -1;
2693 break;
2694
2695 case 1: // path
Christopher Fauletb8ce5052020-08-31 16:11:57 +02002696 if (!http_replace_req_path(htx, ist2(replace, len), 0))
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002697 return -1;
2698 break;
2699
2700 case 2: // query
2701 if (!http_replace_req_query(htx, ist2(replace, len)))
2702 return -1;
2703 break;
2704
2705 case 3: // uri
2706 if (!http_replace_req_uri(htx, ist2(replace, len)))
2707 return -1;
2708 break;
2709
Christopher Faulet312294f2020-09-02 17:17:44 +02002710 case 4: // path + query
2711 if (!http_replace_req_path(htx, ist2(replace, len), 1))
2712 return -1;
2713 break;
2714
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002715 default:
2716 return -1;
2717 }
2718 return 0;
2719}
2720
2721/* This function replace the HTTP status code and the associated message. The
Christopher Faulete00d06c2019-12-16 17:18:42 +01002722 * variable <status> contains the new status code. This function never fails. It
2723 * returns 0 in case of success, -1 in case of internal error.
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002724 */
Christopher Faulet96bff762019-12-17 13:46:18 +01002725int http_res_set_status(unsigned int status, struct ist reason, struct stream *s)
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002726{
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002727 struct htx *htx = htxbuf(&s->res.buf);
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002728 char *res;
2729
2730 chunk_reset(&trash);
2731 res = ultoa_o(status, trash.area, trash.size);
2732 trash.data = res - trash.area;
2733
2734 /* Do we have a custom reason format string? */
Tim Duesterhuse296d3e2020-03-05 17:56:31 +01002735 if (!isttest(reason)) {
Christopher Faulet96bff762019-12-17 13:46:18 +01002736 const char *str = http_get_reason(status);
Tim Duesterhusdcf753a2021-03-04 17:31:47 +01002737 reason = ist(str);
Christopher Faulet96bff762019-12-17 13:46:18 +01002738 }
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002739
Christopher Fauletbde2c4c2020-08-31 16:43:34 +02002740 if (!http_replace_res_status(htx, ist2(trash.area, trash.data), reason))
Christopher Faulete00d06c2019-12-16 17:18:42 +01002741 return -1;
2742 return 0;
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002743}
2744
Christopher Faulet3e964192018-10-24 11:39:23 +02002745/* Executes the http-request rules <rules> for stream <s>, proxy <px> and
2746 * transaction <txn>. Returns the verdict of the first rule that prevents
2747 * further processing of the request (auth, deny, ...), and defaults to
2748 * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or
2749 * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT
2750 * on txn->flags if it encounters a tarpit rule. If <deny_status> is not NULL
2751 * and a deny/tarpit rule is matched, it will be filled with this rule's deny
2752 * status.
2753 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002754static enum rule_result http_req_get_intercept_rule(struct proxy *px, struct list *rules,
Christopher Fauletb58f62b2020-01-13 16:40:13 +01002755 struct stream *s)
Christopher Faulet3e964192018-10-24 11:39:23 +02002756{
2757 struct session *sess = strm_sess(s);
2758 struct http_txn *txn = s->txn;
Christopher Faulet3e964192018-10-24 11:39:23 +02002759 struct act_rule *rule;
Christopher Faulet3e964192018-10-24 11:39:23 +02002760 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002761 int act_opts = 0;
Christopher Faulet3e964192018-10-24 11:39:23 +02002762
Christopher Faulet3e964192018-10-24 11:39:23 +02002763 /* If "the current_rule_list" match the executed rule list, we are in
2764 * resume condition. If a resume is needed it is always in the action
2765 * and never in the ACL or converters. In this case, we initialise the
2766 * current rule, and go to the action execution point.
2767 */
2768 if (s->current_rule) {
2769 rule = s->current_rule;
2770 s->current_rule = NULL;
2771 if (s->current_rule_list == rules)
2772 goto resume_execution;
2773 }
2774 s->current_rule_list = rules;
2775
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002776 /* start the ruleset evaluation in strict mode */
2777 txn->req.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01002778
Christopher Faulet3e964192018-10-24 11:39:23 +02002779 list_for_each_entry(rule, rules, list) {
2780 /* check optional condition */
2781 if (rule->cond) {
2782 int ret;
2783
2784 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
2785 ret = acl_pass(ret);
2786
2787 if (rule->cond->pol == ACL_COND_UNLESS)
2788 ret = !ret;
2789
2790 if (!ret) /* condition not matched */
2791 continue;
2792 }
2793
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002794 act_opts |= ACT_OPT_FIRST;
Christopher Faulet3e964192018-10-24 11:39:23 +02002795 resume_execution:
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002796 /* Always call the action function if defined */
2797 if (rule->action_ptr) {
2798 if ((s->req.flags & CF_READ_ERROR) ||
2799 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
2800 (px->options & PR_O_ABRT_CLOSE)))
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002801 act_opts |= ACT_OPT_FINAL;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002802
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002803 switch (rule->action_ptr(rule, px, sess, s, act_opts)) {
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002804 case ACT_RET_CONT:
2805 break;
2806 case ACT_RET_STOP:
2807 rule_ret = HTTP_RULE_RES_STOP;
2808 goto end;
2809 case ACT_RET_YIELD:
2810 s->current_rule = rule;
2811 rule_ret = HTTP_RULE_RES_YIELD;
2812 goto end;
2813 case ACT_RET_ERR:
2814 rule_ret = HTTP_RULE_RES_ERROR;
2815 goto end;
2816 case ACT_RET_DONE:
2817 rule_ret = HTTP_RULE_RES_DONE;
2818 goto end;
2819 case ACT_RET_DENY:
Christopher Fauletb58f62b2020-01-13 16:40:13 +01002820 if (txn->status == -1)
2821 txn->status = 403;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002822 rule_ret = HTTP_RULE_RES_DENY;
2823 goto end;
2824 case ACT_RET_ABRT:
2825 rule_ret = HTTP_RULE_RES_ABRT;
2826 goto end;
2827 case ACT_RET_INV:
2828 rule_ret = HTTP_RULE_RES_BADREQ;
2829 goto end;
2830 }
2831 continue; /* eval the next rule */
2832 }
2833
2834 /* If not action function defined, check for known actions */
Christopher Faulet3e964192018-10-24 11:39:23 +02002835 switch (rule->action) {
2836 case ACT_ACTION_ALLOW:
2837 rule_ret = HTTP_RULE_RES_STOP;
2838 goto end;
2839
2840 case ACT_ACTION_DENY:
Christopher Faulet5cb513a2020-05-13 17:56:56 +02002841 txn->status = rule->arg.http_reply->status;
2842 txn->http_reply = rule->arg.http_reply;
Christopher Faulet3e964192018-10-24 11:39:23 +02002843 rule_ret = HTTP_RULE_RES_DENY;
2844 goto end;
2845
2846 case ACT_HTTP_REQ_TARPIT:
2847 txn->flags |= TX_CLTARPIT;
Christopher Faulet5cb513a2020-05-13 17:56:56 +02002848 txn->status = rule->arg.http_reply->status;
2849 txn->http_reply = rule->arg.http_reply;
Christopher Faulet3e964192018-10-24 11:39:23 +02002850 rule_ret = HTTP_RULE_RES_DENY;
2851 goto end;
2852
Christopher Faulet3e964192018-10-24 11:39:23 +02002853 case ACT_HTTP_REDIR:
Christopher Faulet90d22a82020-03-06 11:18:39 +01002854 rule_ret = HTTP_RULE_RES_ABRT;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002855 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
Christopher Faulet3a26bee2019-12-16 12:47:40 +01002856 rule_ret = HTTP_RULE_RES_ERROR;
Christopher Faulet3e964192018-10-24 11:39:23 +02002857 goto end;
2858
2859 case ACT_HTTP_SET_NICE:
Christopher Faulet96bff762019-12-17 13:46:18 +01002860 s->task->nice = rule->arg.http.i;
Christopher Faulet3e964192018-10-24 11:39:23 +02002861 break;
2862
2863 case ACT_HTTP_SET_TOS:
Christopher Faulet96bff762019-12-17 13:46:18 +01002864 conn_set_tos(objt_conn(sess->origin), rule->arg.http.i);
Christopher Faulet3e964192018-10-24 11:39:23 +02002865 break;
2866
2867 case ACT_HTTP_SET_MARK:
Christopher Faulet96bff762019-12-17 13:46:18 +01002868 conn_set_mark(objt_conn(sess->origin), rule->arg.http.i);
Christopher Faulet3e964192018-10-24 11:39:23 +02002869 break;
2870
2871 case ACT_HTTP_SET_LOGL:
Christopher Faulet96bff762019-12-17 13:46:18 +01002872 s->logs.level = rule->arg.http.i;
Christopher Faulet3e964192018-10-24 11:39:23 +02002873 break;
2874
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002875 /* other flags exists, but normally, they never be matched. */
Christopher Faulet3e964192018-10-24 11:39:23 +02002876 default:
2877 break;
2878 }
2879 }
2880
2881 end:
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002882 /* if the ruleset evaluation is finished reset the strict mode */
Christopher Faulet46f95542019-12-20 10:07:22 +01002883 if (rule_ret != HTTP_RULE_RES_YIELD)
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002884 txn->req.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01002885
Christopher Faulet3e964192018-10-24 11:39:23 +02002886 /* we reached the end of the rules, nothing to report */
2887 return rule_ret;
2888}
2889
2890/* Executes the http-response rules <rules> for stream <s> and proxy <px>. It
2891 * returns one of 5 possible statuses: HTTP_RULE_RES_CONT, HTTP_RULE_RES_STOP,
2892 * HTTP_RULE_RES_DONE, HTTP_RULE_RES_YIELD, or HTTP_RULE_RES_BADREQ. If *CONT
2893 * is returned, the process can continue the evaluation of next rule list. If
2894 * *STOP or *DONE is returned, the process must stop the evaluation. If *BADREQ
2895 * is returned, it means the operation could not be processed and a server error
Christopher Fauleta53abad2020-05-13 08:12:22 +02002896 * must be returned. If *YIELD is returned, the caller must call again the
2897 * function with the same context.
Christopher Faulet3e964192018-10-24 11:39:23 +02002898 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002899static enum rule_result http_res_get_intercept_rule(struct proxy *px, struct list *rules,
2900 struct stream *s)
Christopher Faulet3e964192018-10-24 11:39:23 +02002901{
2902 struct session *sess = strm_sess(s);
2903 struct http_txn *txn = s->txn;
Christopher Faulet3e964192018-10-24 11:39:23 +02002904 struct act_rule *rule;
Christopher Faulet3e964192018-10-24 11:39:23 +02002905 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002906 int act_opts = 0;
Christopher Faulet3e964192018-10-24 11:39:23 +02002907
Christopher Faulet3e964192018-10-24 11:39:23 +02002908 /* If "the current_rule_list" match the executed rule list, we are in
2909 * resume condition. If a resume is needed it is always in the action
2910 * and never in the ACL or converters. In this case, we initialise the
2911 * current rule, and go to the action execution point.
2912 */
2913 if (s->current_rule) {
2914 rule = s->current_rule;
2915 s->current_rule = NULL;
2916 if (s->current_rule_list == rules)
2917 goto resume_execution;
2918 }
2919 s->current_rule_list = rules;
2920
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002921 /* start the ruleset evaluation in strict mode */
2922 txn->rsp.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01002923
Christopher Faulet3e964192018-10-24 11:39:23 +02002924 list_for_each_entry(rule, rules, list) {
2925 /* check optional condition */
2926 if (rule->cond) {
2927 int ret;
2928
2929 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
2930 ret = acl_pass(ret);
2931
2932 if (rule->cond->pol == ACL_COND_UNLESS)
2933 ret = !ret;
2934
2935 if (!ret) /* condition not matched */
2936 continue;
2937 }
2938
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002939 act_opts |= ACT_OPT_FIRST;
Christopher Faulet3e964192018-10-24 11:39:23 +02002940resume_execution:
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002941
2942 /* Always call the action function if defined */
2943 if (rule->action_ptr) {
2944 if ((s->req.flags & CF_READ_ERROR) ||
2945 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
2946 (px->options & PR_O_ABRT_CLOSE)))
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002947 act_opts |= ACT_OPT_FINAL;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002948
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002949 switch (rule->action_ptr(rule, px, sess, s, act_opts)) {
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002950 case ACT_RET_CONT:
2951 break;
2952 case ACT_RET_STOP:
2953 rule_ret = HTTP_RULE_RES_STOP;
2954 goto end;
2955 case ACT_RET_YIELD:
2956 s->current_rule = rule;
2957 rule_ret = HTTP_RULE_RES_YIELD;
2958 goto end;
2959 case ACT_RET_ERR:
2960 rule_ret = HTTP_RULE_RES_ERROR;
2961 goto end;
2962 case ACT_RET_DONE:
2963 rule_ret = HTTP_RULE_RES_DONE;
2964 goto end;
2965 case ACT_RET_DENY:
Christopher Fauletb58f62b2020-01-13 16:40:13 +01002966 if (txn->status == -1)
2967 txn->status = 502;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002968 rule_ret = HTTP_RULE_RES_DENY;
2969 goto end;
2970 case ACT_RET_ABRT:
2971 rule_ret = HTTP_RULE_RES_ABRT;
2972 goto end;
2973 case ACT_RET_INV:
2974 rule_ret = HTTP_RULE_RES_BADREQ;
2975 goto end;
2976 }
2977 continue; /* eval the next rule */
2978 }
2979
2980 /* If not action function defined, check for known actions */
Christopher Faulet3e964192018-10-24 11:39:23 +02002981 switch (rule->action) {
2982 case ACT_ACTION_ALLOW:
2983 rule_ret = HTTP_RULE_RES_STOP; /* "allow" rules are OK */
2984 goto end;
2985
2986 case ACT_ACTION_DENY:
Christopher Faulet5cb513a2020-05-13 17:56:56 +02002987 txn->status = rule->arg.http_reply->status;
2988 txn->http_reply = rule->arg.http_reply;
Christopher Faulet3a26bee2019-12-16 12:47:40 +01002989 rule_ret = HTTP_RULE_RES_DENY;
Christopher Faulet3e964192018-10-24 11:39:23 +02002990 goto end;
2991
2992 case ACT_HTTP_SET_NICE:
Christopher Faulet96bff762019-12-17 13:46:18 +01002993 s->task->nice = rule->arg.http.i;
Christopher Faulet3e964192018-10-24 11:39:23 +02002994 break;
2995
2996 case ACT_HTTP_SET_TOS:
Christopher Faulet96bff762019-12-17 13:46:18 +01002997 conn_set_tos(objt_conn(sess->origin), rule->arg.http.i);
Christopher Faulet3e964192018-10-24 11:39:23 +02002998 break;
2999
3000 case ACT_HTTP_SET_MARK:
Christopher Faulet96bff762019-12-17 13:46:18 +01003001 conn_set_mark(objt_conn(sess->origin), rule->arg.http.i);
Christopher Faulet3e964192018-10-24 11:39:23 +02003002 break;
3003
3004 case ACT_HTTP_SET_LOGL:
Christopher Faulet96bff762019-12-17 13:46:18 +01003005 s->logs.level = rule->arg.http.i;
Christopher Faulet3e964192018-10-24 11:39:23 +02003006 break;
3007
Christopher Faulet3e964192018-10-24 11:39:23 +02003008 case ACT_HTTP_REDIR:
Christopher Faulet49c2a702020-03-06 15:44:37 +01003009 rule_ret = HTTP_RULE_RES_ABRT;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003010 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
Christopher Faulet3a26bee2019-12-16 12:47:40 +01003011 rule_ret = HTTP_RULE_RES_ERROR;
Christopher Faulet3e964192018-10-24 11:39:23 +02003012 goto end;
3013
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01003014 /* other flags exists, but normally, they never be matched. */
Christopher Faulet3e964192018-10-24 11:39:23 +02003015 default:
3016 break;
3017 }
3018 }
3019
3020 end:
Christopher Faulet1aea50e2020-01-17 16:03:53 +01003021 /* if the ruleset evaluation is finished reset the strict mode */
Christopher Faulet46f95542019-12-20 10:07:22 +01003022 if (rule_ret != HTTP_RULE_RES_YIELD)
Christopher Faulet1aea50e2020-01-17 16:03:53 +01003023 txn->rsp.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01003024
Christopher Faulet3e964192018-10-24 11:39:23 +02003025 /* we reached the end of the rules, nothing to report */
3026 return rule_ret;
3027}
3028
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01003029/* Executes backend and frontend http-after-response rules for the stream <s>,
3030 * in that order. it return 1 on success and 0 on error. It is the caller
3031 * responsibility to catch error or ignore it. If it catches it, this function
3032 * may be called a second time, for the internal error.
3033 */
3034int http_eval_after_res_rules(struct stream *s)
3035{
3036 struct session *sess = s->sess;
3037 enum rule_result ret = HTTP_RULE_RES_CONT;
3038
Christopher Faulet507479b2020-05-15 12:29:46 +02003039 /* Eval after-response ruleset only if the reply is not const */
3040 if (s->txn->flags & TX_CONST_REPLY)
3041 goto end;
3042
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01003043 /* prune the request variables if not already done and swap to the response variables. */
3044 if (s->vars_reqres.scope != SCOPE_RES) {
3045 if (!LIST_ISEMPTY(&s->vars_reqres.head))
3046 vars_prune(&s->vars_reqres, s->sess, s);
3047 vars_init(&s->vars_reqres, SCOPE_RES);
3048 }
3049
3050 ret = http_res_get_intercept_rule(s->be, &s->be->http_after_res_rules, s);
3051 if ((ret == HTTP_RULE_RES_CONT || ret == HTTP_RULE_RES_STOP) && sess->fe != s->be)
3052 ret = http_res_get_intercept_rule(sess->fe, &sess->fe->http_after_res_rules, s);
3053
Christopher Faulet507479b2020-05-15 12:29:46 +02003054 end:
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01003055 /* All other codes than CONTINUE, STOP or DONE are forbidden */
3056 return (ret == HTTP_RULE_RES_CONT || ret == HTTP_RULE_RES_STOP || ret == HTTP_RULE_RES_DONE);
3057}
3058
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003059/*
3060 * Manage client-side cookie. It can impact performance by about 2% so it is
3061 * desirable to call it only when needed. This code is quite complex because
3062 * of the multiple very crappy and ambiguous syntaxes we have to support. it
3063 * highly recommended not to touch this part without a good reason !
3064 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003065static void http_manage_client_side_cookies(struct stream *s, struct channel *req)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003066{
3067 struct session *sess = s->sess;
3068 struct http_txn *txn = s->txn;
3069 struct htx *htx;
3070 struct http_hdr_ctx ctx;
3071 char *hdr_beg, *hdr_end, *del_from;
3072 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
3073 int preserve_hdr;
3074
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003075 htx = htxbuf(&req->buf);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003076 ctx.blk = NULL;
3077 while (http_find_header(htx, ist("Cookie"), &ctx, 1)) {
Olivier Houchardf0f42382019-07-22 17:43:46 +02003078 int is_first = 1;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003079 del_from = NULL; /* nothing to be deleted */
3080 preserve_hdr = 0; /* assume we may kill the whole header */
3081
3082 /* Now look for cookies. Conforming to RFC2109, we have to support
3083 * attributes whose name begin with a '$', and associate them with
3084 * the right cookie, if we want to delete this cookie.
3085 * So there are 3 cases for each cookie read :
3086 * 1) it's a special attribute, beginning with a '$' : ignore it.
3087 * 2) it's a server id cookie that we *MAY* want to delete : save
3088 * some pointers on it (last semi-colon, beginning of cookie...)
3089 * 3) it's an application cookie : we *MAY* have to delete a previous
3090 * "special" cookie.
3091 * At the end of loop, if a "special" cookie remains, we may have to
3092 * remove it. If no application cookie persists in the header, we
3093 * *MUST* delete it.
3094 *
3095 * Note: RFC2965 is unclear about the processing of spaces around
3096 * the equal sign in the ATTR=VALUE form. A careful inspection of
3097 * the RFC explicitly allows spaces before it, and not within the
3098 * tokens (attrs or values). An inspection of RFC2109 allows that
3099 * too but section 10.1.3 lets one think that spaces may be allowed
3100 * after the equal sign too, resulting in some (rare) buggy
3101 * implementations trying to do that. So let's do what servers do.
3102 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
3103 * allowed quoted strings in values, with any possible character
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003104 * after a backslash, including control chars and delimiters, which
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003105 * causes parsing to become ambiguous. Browsers also allow spaces
3106 * within values even without quotes.
3107 *
3108 * We have to keep multiple pointers in order to support cookie
3109 * removal at the beginning, middle or end of header without
3110 * corrupting the header. All of these headers are valid :
3111 *
3112 * hdr_beg hdr_end
3113 * | |
3114 * v |
3115 * NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3 |
3116 * NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3 v
3117 * NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3
3118 * | | | | | | |
3119 * | | | | | | |
3120 * | | | | | | +--> next
3121 * | | | | | +----> val_end
3122 * | | | | +-----------> val_beg
3123 * | | | +--------------> equal
3124 * | | +----------------> att_end
3125 * | +---------------------> att_beg
3126 * +--------------------------> prev
3127 *
3128 */
3129 hdr_beg = ctx.value.ptr;
3130 hdr_end = hdr_beg + ctx.value.len;
3131 for (prev = hdr_beg; prev < hdr_end; prev = next) {
3132 /* Iterate through all cookies on this line */
3133
3134 /* find att_beg */
3135 att_beg = prev;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003136 if (!is_first)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003137 att_beg++;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003138 is_first = 0;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003139
3140 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
3141 att_beg++;
3142
3143 /* find att_end : this is the first character after the last non
3144 * space before the equal. It may be equal to hdr_end.
3145 */
3146 equal = att_end = att_beg;
3147 while (equal < hdr_end) {
3148 if (*equal == '=' || *equal == ',' || *equal == ';')
3149 break;
3150 if (HTTP_IS_SPHT(*equal++))
3151 continue;
3152 att_end = equal;
3153 }
3154
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003155 /* here, <equal> points to '=', a delimiter or the end. <att_end>
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003156 * is between <att_beg> and <equal>, both may be identical.
3157 */
3158 /* look for end of cookie if there is an equal sign */
3159 if (equal < hdr_end && *equal == '=') {
3160 /* look for the beginning of the value */
3161 val_beg = equal + 1;
3162 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
3163 val_beg++;
3164
3165 /* find the end of the value, respecting quotes */
3166 next = http_find_cookie_value_end(val_beg, hdr_end);
3167
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003168 /* make val_end point to the first white space or delimiter after the value */
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003169 val_end = next;
3170 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
3171 val_end--;
3172 }
3173 else
3174 val_beg = val_end = next = equal;
3175
3176 /* We have nothing to do with attributes beginning with
3177 * '$'. However, they will automatically be removed if a
3178 * header before them is removed, since they're supposed
3179 * to be linked together.
3180 */
3181 if (*att_beg == '$')
3182 continue;
3183
3184 /* Ignore cookies with no equal sign */
3185 if (equal == next) {
3186 /* This is not our cookie, so we must preserve it. But if we already
3187 * scheduled another cookie for removal, we cannot remove the
3188 * complete header, but we can remove the previous block itself.
3189 */
3190 preserve_hdr = 1;
3191 if (del_from != NULL) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003192 int delta = http_del_hdr_value(hdr_beg, hdr_end, &del_from, prev);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003193 val_end += delta;
3194 next += delta;
3195 hdr_end += delta;
3196 prev = del_from;
3197 del_from = NULL;
3198 }
3199 continue;
3200 }
3201
3202 /* if there are spaces around the equal sign, we need to
3203 * strip them otherwise we'll get trouble for cookie captures,
3204 * or even for rewrites. Since this happens extremely rarely,
3205 * it does not hurt performance.
3206 */
3207 if (unlikely(att_end != equal || val_beg > equal + 1)) {
3208 int stripped_before = 0;
3209 int stripped_after = 0;
3210
3211 if (att_end != equal) {
3212 memmove(att_end, equal, hdr_end - equal);
3213 stripped_before = (att_end - equal);
3214 equal += stripped_before;
3215 val_beg += stripped_before;
3216 }
3217
3218 if (val_beg > equal + 1) {
3219 memmove(equal + 1, val_beg, hdr_end + stripped_before - val_beg);
3220 stripped_after = (equal + 1) - val_beg;
3221 val_beg += stripped_after;
3222 stripped_before += stripped_after;
3223 }
3224
3225 val_end += stripped_before;
3226 next += stripped_before;
3227 hdr_end += stripped_before;
3228 }
3229 /* now everything is as on the diagram above */
3230
3231 /* First, let's see if we want to capture this cookie. We check
3232 * that we don't already have a client side cookie, because we
3233 * can only capture one. Also as an optimisation, we ignore
3234 * cookies shorter than the declared name.
3235 */
3236 if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL &&
3237 (val_end - att_beg >= sess->fe->capture_namelen) &&
3238 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
3239 int log_len = val_end - att_beg;
3240
3241 if ((txn->cli_cookie = pool_alloc(pool_head_capture)) == NULL) {
3242 ha_alert("HTTP logging : out of memory.\n");
3243 } else {
3244 if (log_len > sess->fe->capture_len)
3245 log_len = sess->fe->capture_len;
3246 memcpy(txn->cli_cookie, att_beg, log_len);
3247 txn->cli_cookie[log_len] = 0;
3248 }
3249 }
3250
3251 /* Persistence cookies in passive, rewrite or insert mode have the
3252 * following form :
3253 *
3254 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
3255 *
3256 * For cookies in prefix mode, the form is :
3257 *
3258 * Cookie: NAME=SRV~VALUE
3259 */
3260 if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
3261 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
3262 struct server *srv = s->be->srv;
3263 char *delim;
3264
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003265 /* if we're in cookie prefix mode, we'll search the delimiter so that we
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003266 * have the server ID between val_beg and delim, and the original cookie between
3267 * delim+1 and val_end. Otherwise, delim==val_end :
3268 *
3269 * hdr_beg
3270 * |
3271 * v
3272 * NAME=SRV; # in all but prefix modes
3273 * NAME=SRV~OPAQUE ; # in prefix mode
3274 * || || | |+-> next
3275 * || || | +--> val_end
3276 * || || +---------> delim
3277 * || |+------------> val_beg
3278 * || +-------------> att_end = equal
3279 * |+-----------------> att_beg
3280 * +------------------> prev
3281 *
3282 */
3283 if (s->be->ck_opts & PR_CK_PFX) {
3284 for (delim = val_beg; delim < val_end; delim++)
3285 if (*delim == COOKIE_DELIM)
3286 break;
3287 }
3288 else {
3289 char *vbar1;
3290 delim = val_end;
3291 /* Now check if the cookie contains a date field, which would
3292 * appear after a vertical bar ('|') just after the server name
3293 * and before the delimiter.
3294 */
3295 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
3296 if (vbar1) {
3297 /* OK, so left of the bar is the server's cookie and
3298 * right is the last seen date. It is a base64 encoded
3299 * 30-bit value representing the UNIX date since the
3300 * epoch in 4-second quantities.
3301 */
3302 int val;
3303 delim = vbar1++;
3304 if (val_end - vbar1 >= 5) {
3305 val = b64tos30(vbar1);
3306 if (val > 0)
3307 txn->cookie_last_date = val << 2;
3308 }
3309 /* look for a second vertical bar */
3310 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
3311 if (vbar1 && (val_end - vbar1 > 5)) {
3312 val = b64tos30(vbar1 + 1);
3313 if (val > 0)
3314 txn->cookie_first_date = val << 2;
3315 }
3316 }
3317 }
3318
3319 /* if the cookie has an expiration date and the proxy wants to check
3320 * it, then we do that now. We first check if the cookie is too old,
3321 * then only if it has expired. We detect strict overflow because the
3322 * time resolution here is not great (4 seconds). Cookies with dates
3323 * in the future are ignored if their offset is beyond one day. This
3324 * allows an admin to fix timezone issues without expiring everyone
3325 * and at the same time avoids keeping unwanted side effects for too
3326 * long.
3327 */
3328 if (txn->cookie_first_date && s->be->cookie_maxlife &&
3329 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) ||
3330 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
3331 txn->flags &= ~TX_CK_MASK;
3332 txn->flags |= TX_CK_OLD;
3333 delim = val_beg; // let's pretend we have not found the cookie
3334 txn->cookie_first_date = 0;
3335 txn->cookie_last_date = 0;
3336 }
3337 else if (txn->cookie_last_date && s->be->cookie_maxidle &&
3338 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) ||
3339 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
3340 txn->flags &= ~TX_CK_MASK;
3341 txn->flags |= TX_CK_EXPIRED;
3342 delim = val_beg; // let's pretend we have not found the cookie
3343 txn->cookie_first_date = 0;
3344 txn->cookie_last_date = 0;
3345 }
3346
3347 /* Here, we'll look for the first running server which supports the cookie.
3348 * This allows to share a same cookie between several servers, for example
3349 * to dedicate backup servers to specific servers only.
3350 * However, to prevent clients from sticking to cookie-less backup server
3351 * when they have incidentely learned an empty cookie, we simply ignore
3352 * empty cookies and mark them as invalid.
3353 * The same behaviour is applied when persistence must be ignored.
3354 */
3355 if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
3356 srv = NULL;
3357
3358 while (srv) {
3359 if (srv->cookie && (srv->cklen == delim - val_beg) &&
3360 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
3361 if ((srv->cur_state != SRV_ST_STOPPED) ||
3362 (s->be->options & PR_O_PERSIST) ||
3363 (s->flags & SF_FORCE_PRST)) {
3364 /* we found the server and we can use it */
3365 txn->flags &= ~TX_CK_MASK;
3366 txn->flags |= (srv->cur_state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
3367 s->flags |= SF_DIRECT | SF_ASSIGNED;
3368 s->target = &srv->obj_type;
3369 break;
3370 } else {
3371 /* we found a server, but it's down,
3372 * mark it as such and go on in case
3373 * another one is available.
3374 */
3375 txn->flags &= ~TX_CK_MASK;
3376 txn->flags |= TX_CK_DOWN;
3377 }
3378 }
3379 srv = srv->next;
3380 }
3381
3382 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
3383 /* no server matched this cookie or we deliberately skipped it */
3384 txn->flags &= ~TX_CK_MASK;
3385 if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
3386 txn->flags |= TX_CK_UNUSED;
3387 else
3388 txn->flags |= TX_CK_INVALID;
3389 }
3390
3391 /* depending on the cookie mode, we may have to either :
3392 * - delete the complete cookie if we're in insert+indirect mode, so that
3393 * the server never sees it ;
3394 * - remove the server id from the cookie value, and tag the cookie as an
Joseph Herlante9d5c722018-11-25 11:00:25 -08003395 * application cookie so that it does not get accidentally removed later,
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003396 * if we're in cookie prefix mode
3397 */
3398 if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
3399 int delta; /* negative */
3400
3401 memmove(val_beg, delim + 1, hdr_end - (delim + 1));
3402 delta = val_beg - (delim + 1);
3403 val_end += delta;
3404 next += delta;
3405 hdr_end += delta;
3406 del_from = NULL;
3407 preserve_hdr = 1; /* we want to keep this cookie */
3408 }
3409 else if (del_from == NULL &&
3410 (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
3411 del_from = prev;
3412 }
3413 }
3414 else {
3415 /* This is not our cookie, so we must preserve it. But if we already
3416 * scheduled another cookie for removal, we cannot remove the
3417 * complete header, but we can remove the previous block itself.
3418 */
3419 preserve_hdr = 1;
3420
3421 if (del_from != NULL) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003422 int delta = http_del_hdr_value(hdr_beg, hdr_end, &del_from, prev);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003423 if (att_beg >= del_from)
3424 att_beg += delta;
3425 if (att_end >= del_from)
3426 att_end += delta;
3427 val_beg += delta;
3428 val_end += delta;
3429 next += delta;
3430 hdr_end += delta;
3431 prev = del_from;
3432 del_from = NULL;
3433 }
3434 }
3435
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003436 } /* for each cookie */
3437
3438
3439 /* There are no more cookies on this line.
3440 * We may still have one (or several) marked for deletion at the
3441 * end of the line. We must do this now in two ways :
3442 * - if some cookies must be preserved, we only delete from the
3443 * mark to the end of line ;
3444 * - if nothing needs to be preserved, simply delete the whole header
3445 */
3446 if (del_from) {
3447 hdr_end = (preserve_hdr ? del_from : hdr_beg);
3448 }
3449 if ((hdr_end - hdr_beg) != ctx.value.len) {
Christopher Faulet3e2638e2019-06-18 09:49:16 +02003450 if (hdr_beg != hdr_end)
3451 htx_change_blk_value_len(htx, ctx.blk, hdr_end - hdr_beg);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003452 else
3453 http_remove_header(htx, &ctx);
3454 }
3455 } /* for each "Cookie header */
3456}
3457
3458/*
3459 * Manage server-side cookies. It can impact performance by about 2% so it is
3460 * desirable to call it only when needed. This function is also used when we
3461 * just need to know if there is a cookie (eg: for check-cache).
3462 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003463static void http_manage_server_side_cookies(struct stream *s, struct channel *res)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003464{
3465 struct session *sess = s->sess;
3466 struct http_txn *txn = s->txn;
3467 struct htx *htx;
3468 struct http_hdr_ctx ctx;
3469 struct server *srv;
3470 char *hdr_beg, *hdr_end;
3471 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau6f7a02a2019-04-15 21:49:49 +02003472 int is_cookie2 = 0;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003473
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003474 htx = htxbuf(&res->buf);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003475
3476 ctx.blk = NULL;
3477 while (1) {
Olivier Houchardf0f42382019-07-22 17:43:46 +02003478 int is_first = 1;
3479
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003480 if (!http_find_header(htx, ist("Set-Cookie"), &ctx, 1)) {
3481 if (!http_find_header(htx, ist("Set-Cookie2"), &ctx, 1))
3482 break;
3483 is_cookie2 = 1;
3484 }
3485
3486 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
3487 * <prev> points to the colon.
3488 */
3489 txn->flags |= TX_SCK_PRESENT;
3490
3491 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
3492 * check-cache is enabled) and we are not interested in checking
3493 * them. Warning, the cookie capture is declared in the frontend.
3494 */
3495 if (s->be->cookie_name == NULL && sess->fe->capture_name == NULL)
3496 break;
3497
3498 /* OK so now we know we have to process this response cookie.
3499 * The format of the Set-Cookie header is slightly different
3500 * from the format of the Cookie header in that it does not
3501 * support the comma as a cookie delimiter (thus the header
3502 * cannot be folded) because the Expires attribute described in
3503 * the original Netscape's spec may contain an unquoted date
3504 * with a comma inside. We have to live with this because
3505 * many browsers don't support Max-Age and some browsers don't
3506 * support quoted strings. However the Set-Cookie2 header is
3507 * clean.
3508 *
3509 * We have to keep multiple pointers in order to support cookie
3510 * removal at the beginning, middle or end of header without
3511 * corrupting the header (in case of set-cookie2). A special
3512 * pointer, <scav> points to the beginning of the set-cookie-av
3513 * fields after the first semi-colon. The <next> pointer points
3514 * either to the end of line (set-cookie) or next unquoted comma
3515 * (set-cookie2). All of these headers are valid :
3516 *
3517 * hdr_beg hdr_end
3518 * | |
3519 * v |
3520 * NAME1 = VALUE 1 ; Secure; Path="/" |
3521 * NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT v
3522 * NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT
3523 * NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard
3524 * | | | | | | | |
3525 * | | | | | | | +-> next
3526 * | | | | | | +------------> scav
3527 * | | | | | +--------------> val_end
3528 * | | | | +--------------------> val_beg
3529 * | | | +----------------------> equal
3530 * | | +------------------------> att_end
3531 * | +----------------------------> att_beg
3532 * +------------------------------> prev
3533 * -------------------------------> hdr_beg
3534 */
3535 hdr_beg = ctx.value.ptr;
3536 hdr_end = hdr_beg + ctx.value.len;
3537 for (prev = hdr_beg; prev < hdr_end; prev = next) {
3538
3539 /* Iterate through all cookies on this line */
3540
3541 /* find att_beg */
3542 att_beg = prev;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003543 if (!is_first)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003544 att_beg++;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003545 is_first = 0;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003546
3547 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
3548 att_beg++;
3549
3550 /* find att_end : this is the first character after the last non
3551 * space before the equal. It may be equal to hdr_end.
3552 */
3553 equal = att_end = att_beg;
3554
3555 while (equal < hdr_end) {
3556 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
3557 break;
3558 if (HTTP_IS_SPHT(*equal++))
3559 continue;
3560 att_end = equal;
3561 }
3562
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003563 /* here, <equal> points to '=', a delimiter or the end. <att_end>
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003564 * is between <att_beg> and <equal>, both may be identical.
3565 */
3566
3567 /* look for end of cookie if there is an equal sign */
3568 if (equal < hdr_end && *equal == '=') {
3569 /* look for the beginning of the value */
3570 val_beg = equal + 1;
3571 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
3572 val_beg++;
3573
3574 /* find the end of the value, respecting quotes */
3575 next = http_find_cookie_value_end(val_beg, hdr_end);
3576
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003577 /* make val_end point to the first white space or delimiter after the value */
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003578 val_end = next;
3579 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
3580 val_end--;
3581 }
3582 else {
3583 /* <equal> points to next comma, semi-colon or EOL */
3584 val_beg = val_end = next = equal;
3585 }
3586
3587 if (next < hdr_end) {
3588 /* Set-Cookie2 supports multiple cookies, and <next> points to
3589 * a colon or semi-colon before the end. So skip all attr-value
3590 * pairs and look for the next comma. For Set-Cookie, since
3591 * commas are permitted in values, skip to the end.
3592 */
3593 if (is_cookie2)
3594 next = http_find_hdr_value_end(next, hdr_end);
3595 else
3596 next = hdr_end;
3597 }
3598
3599 /* Now everything is as on the diagram above */
3600
3601 /* Ignore cookies with no equal sign */
3602 if (equal == val_end)
3603 continue;
3604
3605 /* If there are spaces around the equal sign, we need to
3606 * strip them otherwise we'll get trouble for cookie captures,
3607 * or even for rewrites. Since this happens extremely rarely,
3608 * it does not hurt performance.
3609 */
3610 if (unlikely(att_end != equal || val_beg > equal + 1)) {
3611 int stripped_before = 0;
3612 int stripped_after = 0;
3613
3614 if (att_end != equal) {
3615 memmove(att_end, equal, hdr_end - equal);
3616 stripped_before = (att_end - equal);
3617 equal += stripped_before;
3618 val_beg += stripped_before;
3619 }
3620
3621 if (val_beg > equal + 1) {
3622 memmove(equal + 1, val_beg, hdr_end + stripped_before - val_beg);
3623 stripped_after = (equal + 1) - val_beg;
3624 val_beg += stripped_after;
3625 stripped_before += stripped_after;
3626 }
3627
3628 val_end += stripped_before;
3629 next += stripped_before;
3630 hdr_end += stripped_before;
3631
Christopher Faulet3e2638e2019-06-18 09:49:16 +02003632 htx_change_blk_value_len(htx, ctx.blk, hdr_end - hdr_beg);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003633 ctx.value.len = hdr_end - hdr_beg;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003634 }
3635
3636 /* First, let's see if we want to capture this cookie. We check
3637 * that we don't already have a server side cookie, because we
3638 * can only capture one. Also as an optimisation, we ignore
3639 * cookies shorter than the declared name.
3640 */
3641 if (sess->fe->capture_name != NULL &&
3642 txn->srv_cookie == NULL &&
3643 (val_end - att_beg >= sess->fe->capture_namelen) &&
3644 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
3645 int log_len = val_end - att_beg;
3646 if ((txn->srv_cookie = pool_alloc(pool_head_capture)) == NULL) {
3647 ha_alert("HTTP logging : out of memory.\n");
3648 }
3649 else {
3650 if (log_len > sess->fe->capture_len)
3651 log_len = sess->fe->capture_len;
3652 memcpy(txn->srv_cookie, att_beg, log_len);
3653 txn->srv_cookie[log_len] = 0;
3654 }
3655 }
3656
3657 srv = objt_server(s->target);
3658 /* now check if we need to process it for persistence */
3659 if (!(s->flags & SF_IGNORE_PRST) &&
3660 (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
3661 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
3662 /* assume passive cookie by default */
3663 txn->flags &= ~TX_SCK_MASK;
3664 txn->flags |= TX_SCK_FOUND;
3665
3666 /* If the cookie is in insert mode on a known server, we'll delete
3667 * this occurrence because we'll insert another one later.
3668 * We'll delete it too if the "indirect" option is set and we're in
3669 * a direct access.
3670 */
3671 if (s->be->ck_opts & PR_CK_PSV) {
3672 /* The "preserve" flag was set, we don't want to touch the
3673 * server's cookie.
3674 */
3675 }
3676 else if ((srv && (s->be->ck_opts & PR_CK_INS)) ||
3677 ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) {
3678 /* this cookie must be deleted */
3679 if (prev == hdr_beg && next == hdr_end) {
3680 /* whole header */
3681 http_remove_header(htx, &ctx);
3682 /* note: while both invalid now, <next> and <hdr_end>
3683 * are still equal, so the for() will stop as expected.
3684 */
3685 } else {
3686 /* just remove the value */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003687 int delta = http_del_hdr_value(hdr_beg, hdr_end, &prev, next);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003688 next = prev;
3689 hdr_end += delta;
3690 }
3691 txn->flags &= ~TX_SCK_MASK;
3692 txn->flags |= TX_SCK_DELETED;
3693 /* and go on with next cookie */
3694 }
3695 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) {
3696 /* replace bytes val_beg->val_end with the cookie name associated
3697 * with this server since we know it.
3698 */
3699 int sliding, delta;
3700
3701 ctx.value = ist2(val_beg, val_end - val_beg);
3702 ctx.lws_before = ctx.lws_after = 0;
3703 http_replace_header_value(htx, &ctx, ist2(srv->cookie, srv->cklen));
3704 delta = srv->cklen - (val_end - val_beg);
3705 sliding = (ctx.value.ptr - val_beg);
3706 hdr_beg += sliding;
3707 val_beg += sliding;
3708 next += sliding + delta;
3709 hdr_end += sliding + delta;
3710
3711 txn->flags &= ~TX_SCK_MASK;
3712 txn->flags |= TX_SCK_REPLACED;
3713 }
3714 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) {
3715 /* insert the cookie name associated with this server
3716 * before existing cookie, and insert a delimiter between them..
3717 */
3718 int sliding, delta;
3719 ctx.value = ist2(val_beg, 0);
3720 ctx.lws_before = ctx.lws_after = 0;
3721 http_replace_header_value(htx, &ctx, ist2(srv->cookie, srv->cklen + 1));
3722 delta = srv->cklen + 1;
3723 sliding = (ctx.value.ptr - val_beg);
3724 hdr_beg += sliding;
3725 val_beg += sliding;
3726 next += sliding + delta;
3727 hdr_end += sliding + delta;
3728
3729 val_beg[srv->cklen] = COOKIE_DELIM;
3730 txn->flags &= ~TX_SCK_MASK;
3731 txn->flags |= TX_SCK_REPLACED;
3732 }
3733 }
3734 /* that's done for this cookie, check the next one on the same
3735 * line when next != hdr_end (only if is_cookie2).
3736 */
3737 }
3738 }
3739}
3740
Christopher Faulet25a02f62018-10-24 12:00:25 +02003741/*
3742 * Parses the Cache-Control and Pragma request header fields to determine if
3743 * the request may be served from the cache and/or if it is cacheable. Updates
3744 * s->txn->flags.
3745 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003746void http_check_request_for_cacheability(struct stream *s, struct channel *req)
Christopher Faulet25a02f62018-10-24 12:00:25 +02003747{
3748 struct http_txn *txn = s->txn;
3749 struct htx *htx;
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003750 struct http_hdr_ctx ctx = { .blk = NULL };
3751 int pragma_found, cc_found;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003752
3753 if ((txn->flags & (TX_CACHEABLE|TX_CACHE_IGNORE)) == TX_CACHE_IGNORE)
3754 return; /* nothing more to do here */
3755
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003756 htx = htxbuf(&req->buf);
Christopher Faulet25a02f62018-10-24 12:00:25 +02003757 pragma_found = cc_found = 0;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003758
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003759 /* Check "pragma" header for HTTP/1.0 compatibility. */
3760 if (http_find_header(htx, ist("pragma"), &ctx, 1)) {
3761 if (isteqi(ctx.value, ist("no-cache"))) {
3762 pragma_found = 1;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003763 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003764 }
Christopher Faulet25a02f62018-10-24 12:00:25 +02003765
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003766 ctx.blk = NULL;
3767 /* Don't use the cache and don't try to store if we found the
3768 * Authorization header */
3769 if (http_find_header(htx, ist("authorization"), &ctx, 1)) {
3770 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3771 txn->flags |= TX_CACHE_IGNORE;
3772 }
Christopher Faulet25a02f62018-10-24 12:00:25 +02003773
Christopher Faulet25a02f62018-10-24 12:00:25 +02003774
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003775 /* Look for "cache-control" header and iterate over all the values
3776 * until we find one that specifies that caching is possible or not. */
3777 ctx.blk = NULL;
3778 while (http_find_header(htx, ist("cache-control"), &ctx, 0)) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003779 cc_found = 1;
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003780 /* We don't check the values after max-age, max-stale nor min-fresh,
3781 * we simply don't use the cache when they're specified. */
3782 if (istmatchi(ctx.value, ist("max-age")) ||
3783 istmatchi(ctx.value, ist("no-cache")) ||
3784 istmatchi(ctx.value, ist("max-stale")) ||
3785 istmatchi(ctx.value, ist("min-fresh"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003786 txn->flags |= TX_CACHE_IGNORE;
3787 continue;
3788 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003789 if (istmatchi(ctx.value, ist("no-store"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003790 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3791 continue;
3792 }
3793 }
3794
3795 /* RFC7234#5.4:
3796 * When the Cache-Control header field is also present and
3797 * understood in a request, Pragma is ignored.
3798 * When the Cache-Control header field is not present in a
3799 * request, caches MUST consider the no-cache request
3800 * pragma-directive as having the same effect as if
3801 * "Cache-Control: no-cache" were present.
3802 */
3803 if (!cc_found && pragma_found)
3804 txn->flags |= TX_CACHE_IGNORE;
3805}
3806
3807/*
3808 * Check if response is cacheable or not. Updates s->txn->flags.
3809 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003810void http_check_response_for_cacheability(struct stream *s, struct channel *res)
Christopher Faulet25a02f62018-10-24 12:00:25 +02003811{
3812 struct http_txn *txn = s->txn;
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003813 struct http_hdr_ctx ctx = { .blk = NULL };
Christopher Faulet25a02f62018-10-24 12:00:25 +02003814 struct htx *htx;
Remi Tricot-Le Bretoncc9bf2e2020-11-12 11:14:41 +01003815 int has_freshness_info = 0;
3816 int has_validator = 0;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003817
3818 if (txn->status < 200) {
3819 /* do not try to cache interim responses! */
3820 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3821 return;
3822 }
3823
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003824 htx = htxbuf(&res->buf);
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003825 /* Check "pragma" header for HTTP/1.0 compatibility. */
3826 if (http_find_header(htx, ist("pragma"), &ctx, 1)) {
3827 if (isteqi(ctx.value, ist("no-cache"))) {
3828 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3829 return;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003830 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003831 }
Christopher Faulet25a02f62018-10-24 12:00:25 +02003832
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003833 /* Look for "cache-control" header and iterate over all the values
3834 * until we find one that specifies that caching is possible or not. */
3835 ctx.blk = NULL;
3836 while (http_find_header(htx, ist("cache-control"), &ctx, 0)) {
3837 if (isteqi(ctx.value, ist("public"))) {
3838 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003839 continue;
3840 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003841 if (isteqi(ctx.value, ist("private")) ||
3842 isteqi(ctx.value, ist("no-cache")) ||
3843 isteqi(ctx.value, ist("no-store")) ||
3844 isteqi(ctx.value, ist("max-age=0")) ||
3845 isteqi(ctx.value, ist("s-maxage=0"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003846 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003847 continue;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003848 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003849 /* We might have a no-cache="set-cookie" form. */
3850 if (istmatchi(ctx.value, ist("no-cache=\"set-cookie"))) {
3851 txn->flags &= ~TX_CACHE_COOK;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003852 continue;
3853 }
Remi Tricot-Le Bretoncc9bf2e2020-11-12 11:14:41 +01003854
3855 if (istmatchi(ctx.value, ist("s-maxage")) ||
3856 istmatchi(ctx.value, ist("max-age"))) {
3857 has_freshness_info = 1;
3858 continue;
3859 }
3860 }
3861
3862 /* If no freshness information could be found in Cache-Control values,
3863 * look for an Expires header. */
3864 if (!has_freshness_info) {
3865 ctx.blk = NULL;
3866 has_freshness_info = http_find_header(htx, ist("expires"), &ctx, 0);
Christopher Faulet25a02f62018-10-24 12:00:25 +02003867 }
Remi Tricot-Le Bretoncc9bf2e2020-11-12 11:14:41 +01003868
3869 /* If no freshness information could be found in Cache-Control or Expires
3870 * values, look for an explicit validator. */
3871 if (!has_freshness_info) {
3872 ctx.blk = NULL;
3873 has_validator = 1;
3874 if (!http_find_header(htx, ist("etag"), &ctx, 0)) {
3875 ctx.blk = NULL;
3876 if (!http_find_header(htx, ist("last-modified"), &ctx, 0))
3877 has_validator = 0;
3878 }
3879 }
3880
3881 /* We won't store an entry that has neither a cache validator nor an
3882 * explicit expiration time, as suggested in RFC 7234#3. */
3883 if (!has_freshness_info && !has_validator)
3884 txn->flags |= TX_CACHE_IGNORE;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003885}
3886
Christopher Faulet377c5a52018-10-24 21:21:30 +02003887/*
3888 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
3889 * for the current backend.
3890 *
3891 * It is assumed that the request is either a HEAD, GET, or POST and that the
3892 * uri_auth field is valid.
3893 *
3894 * Returns 1 if stats should be provided, otherwise 0.
3895 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003896static int http_stats_check_uri(struct stream *s, struct http_txn *txn, struct proxy *backend)
Christopher Faulet377c5a52018-10-24 21:21:30 +02003897{
3898 struct uri_auth *uri_auth = backend->uri_auth;
3899 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003900 struct htx_sl *sl;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003901 struct ist uri;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003902
3903 if (!uri_auth)
3904 return 0;
3905
3906 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
3907 return 0;
3908
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003909 htx = htxbuf(&s->req.buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02003910 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003911 uri = htx_sl_req_uri(sl);
Willy Tarreau1eb3b482019-10-31 15:50:28 +01003912 if (*uri_auth->uri_prefix == '/')
3913 uri = http_get_path(uri);
Christopher Faulet377c5a52018-10-24 21:21:30 +02003914
3915 /* check URI size */
3916 if (uri_auth->uri_len > uri.len)
3917 return 0;
3918
3919 if (memcmp(uri.ptr, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
3920 return 0;
3921
3922 return 1;
3923}
3924
3925/* This function prepares an applet to handle the stats. It can deal with the
3926 * "100-continue" expectation, check that admin rules are met for POST requests,
3927 * and program a response message if something was unexpected. It cannot fail
3928 * and always relies on the stats applet to complete the job. It does not touch
3929 * analysers nor counters, which are left to the caller. It does not touch
3930 * s->target which is supposed to already point to the stats applet. The caller
3931 * is expected to have already assigned an appctx to the stream.
3932 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003933static int http_handle_stats(struct stream *s, struct channel *req)
Christopher Faulet377c5a52018-10-24 21:21:30 +02003934{
3935 struct stats_admin_rule *stats_admin_rule;
3936 struct stream_interface *si = &s->si[1];
3937 struct session *sess = s->sess;
3938 struct http_txn *txn = s->txn;
3939 struct http_msg *msg = &txn->req;
3940 struct uri_auth *uri_auth = s->be->uri_auth;
3941 const char *h, *lookup, *end;
3942 struct appctx *appctx;
3943 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003944 struct htx_sl *sl;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003945
3946 appctx = si_appctx(si);
3947 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
3948 appctx->st1 = appctx->st2 = 0;
3949 appctx->ctx.stats.st_code = STAT_STATUS_INIT;
Willy Tarreau676c29e2019-10-09 10:50:01 +02003950 appctx->ctx.stats.flags |= uri_auth->flags;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003951 appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */
3952 if ((msg->flags & HTTP_MSGF_VER_11) && (txn->meth != HTTP_METH_HEAD))
3953 appctx->ctx.stats.flags |= STAT_CHUNKED;
3954
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003955 htx = htxbuf(&req->buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02003956 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003957 lookup = HTX_SL_REQ_UPTR(sl) + uri_auth->uri_len;
3958 end = HTX_SL_REQ_UPTR(sl) + HTX_SL_REQ_ULEN(sl);
Christopher Faulet377c5a52018-10-24 21:21:30 +02003959
3960 for (h = lookup; h <= end - 3; h++) {
3961 if (memcmp(h, ";up", 3) == 0) {
3962 appctx->ctx.stats.flags |= STAT_HIDE_DOWN;
3963 break;
3964 }
Amaury Denoyelle91e55ea2021-02-25 14:46:08 +01003965 }
3966
3967 for (h = lookup; h <= end - 9; h++) {
3968 if (memcmp(h, ";no-maint", 9) == 0) {
Willy Tarreau3e320362020-10-23 17:28:57 +02003969 appctx->ctx.stats.flags |= STAT_HIDE_MAINT;
3970 break;
3971 }
Christopher Faulet377c5a52018-10-24 21:21:30 +02003972 }
3973
3974 if (uri_auth->refresh) {
3975 for (h = lookup; h <= end - 10; h++) {
3976 if (memcmp(h, ";norefresh", 10) == 0) {
3977 appctx->ctx.stats.flags |= STAT_NO_REFRESH;
3978 break;
3979 }
3980 }
3981 }
3982
3983 for (h = lookup; h <= end - 4; h++) {
3984 if (memcmp(h, ";csv", 4) == 0) {
Christopher Faulet6338a082019-09-09 15:50:54 +02003985 appctx->ctx.stats.flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
Christopher Faulet377c5a52018-10-24 21:21:30 +02003986 break;
3987 }
3988 }
3989
3990 for (h = lookup; h <= end - 6; h++) {
3991 if (memcmp(h, ";typed", 6) == 0) {
Christopher Faulet6338a082019-09-09 15:50:54 +02003992 appctx->ctx.stats.flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
Christopher Faulet377c5a52018-10-24 21:21:30 +02003993 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
3994 break;
3995 }
3996 }
3997
Christopher Faulet6338a082019-09-09 15:50:54 +02003998 for (h = lookup; h <= end - 5; h++) {
3999 if (memcmp(h, ";json", 5) == 0) {
4000 appctx->ctx.stats.flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
4001 appctx->ctx.stats.flags |= STAT_FMT_JSON;
4002 break;
4003 }
4004 }
4005
4006 for (h = lookup; h <= end - 12; h++) {
4007 if (memcmp(h, ";json-schema", 12) == 0) {
4008 appctx->ctx.stats.flags &= ~STAT_FMT_MASK;
4009 appctx->ctx.stats.flags |= STAT_JSON_SCHM;
4010 break;
4011 }
4012 }
4013
Christopher Faulet377c5a52018-10-24 21:21:30 +02004014 for (h = lookup; h <= end - 8; h++) {
4015 if (memcmp(h, ";st=", 4) == 0) {
4016 int i;
4017 h += 4;
4018 appctx->ctx.stats.st_code = STAT_STATUS_UNKN;
4019 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
4020 if (strncmp(stat_status_codes[i], h, 4) == 0) {
4021 appctx->ctx.stats.st_code = i;
4022 break;
4023 }
4024 }
4025 break;
4026 }
4027 }
4028
4029 appctx->ctx.stats.scope_str = 0;
4030 appctx->ctx.stats.scope_len = 0;
4031 for (h = lookup; h <= end - 8; h++) {
4032 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
4033 int itx = 0;
4034 const char *h2;
4035 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
4036 const char *err;
4037
4038 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
4039 h2 = h;
Christopher Fauleted7a0662019-01-14 11:07:34 +01004040 appctx->ctx.stats.scope_str = h2 - HTX_SL_REQ_UPTR(sl);
4041 while (h < end) {
Christopher Faulet377c5a52018-10-24 21:21:30 +02004042 if (*h == ';' || *h == '&' || *h == ' ')
4043 break;
4044 itx++;
4045 h++;
4046 }
4047
4048 if (itx > STAT_SCOPE_TXT_MAXLEN)
4049 itx = STAT_SCOPE_TXT_MAXLEN;
4050 appctx->ctx.stats.scope_len = itx;
4051
4052 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
4053 memcpy(scope_txt, h2, itx);
4054 scope_txt[itx] = '\0';
4055 err = invalid_char(scope_txt);
4056 if (err) {
4057 /* bad char in search text => clear scope */
4058 appctx->ctx.stats.scope_str = 0;
4059 appctx->ctx.stats.scope_len = 0;
4060 }
4061 break;
4062 }
4063 }
4064
4065 /* now check whether we have some admin rules for this request */
4066 list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) {
4067 int ret = 1;
4068
4069 if (stats_admin_rule->cond) {
4070 ret = acl_exec_cond(stats_admin_rule->cond, s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
4071 ret = acl_pass(ret);
4072 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
4073 ret = !ret;
4074 }
4075
4076 if (ret) {
4077 /* no rule, or the rule matches */
4078 appctx->ctx.stats.flags |= STAT_ADMIN;
4079 break;
4080 }
4081 }
4082
Christopher Faulet5d45e382019-02-27 15:15:23 +01004083 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
4084 appctx->st0 = STAT_HTTP_HEAD;
4085 else if (txn->meth == HTTP_METH_POST) {
Christopher Fauletbd9e8422019-08-15 22:26:48 +02004086 if (appctx->ctx.stats.flags & STAT_ADMIN) {
Christopher Faulet377c5a52018-10-24 21:21:30 +02004087 appctx->st0 = STAT_HTTP_POST;
Christopher Fauletbd9e8422019-08-15 22:26:48 +02004088 if (msg->msg_state < HTTP_MSG_DATA)
4089 req->analysers |= AN_REQ_HTTP_BODY;
4090 }
Christopher Faulet377c5a52018-10-24 21:21:30 +02004091 else {
Christopher Faulet5d45e382019-02-27 15:15:23 +01004092 /* POST without admin level */
Christopher Faulet377c5a52018-10-24 21:21:30 +02004093 appctx->ctx.stats.flags &= ~STAT_CHUNKED;
4094 appctx->ctx.stats.st_code = STAT_STATUS_DENY;
4095 appctx->st0 = STAT_HTTP_LAST;
4096 }
4097 }
4098 else {
Christopher Faulet5d45e382019-02-27 15:15:23 +01004099 /* Unsupported method */
4100 appctx->ctx.stats.flags &= ~STAT_CHUNKED;
4101 appctx->ctx.stats.st_code = STAT_STATUS_IVAL;
4102 appctx->st0 = STAT_HTTP_LAST;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004103 }
4104
4105 s->task->nice = -32; /* small boost for HTTP statistics */
4106 return 1;
4107}
4108
Christopher Faulet021a8e42021-03-29 10:46:38 +02004109/* This function waits for the message payload at most <time> milliseconds (may
4110 * be set to TICK_ETERNITY). It stops to wait if at least <bytes> bytes of the
4111 * payload are received (0 means no limit). It returns HTTP_RULE_* depending on
4112 * the result:
4113 *
4114 * - HTTP_RULE_RES_CONT when conditions are met to stop waiting
4115 * - HTTP_RULE_RES_YIELD to wait for more data
Ilya Shipitsinb2be9a12021-04-24 13:25:42 +05004116 * - HTTP_RULE_RES_ABRT when a timeout occurred.
Christopher Faulet021a8e42021-03-29 10:46:38 +02004117 * - HTTP_RULE_RES_BADREQ if a parsing error is raised by lower level
Ilya Shipitsinb2be9a12021-04-24 13:25:42 +05004118 * - HTTP_RULE_RES_ERROR if an internal error occurred
Christopher Faulet021a8e42021-03-29 10:46:38 +02004119 *
Ilya Shipitsinb2be9a12021-04-24 13:25:42 +05004120 * If a timeout occurred, this function is responsible to emit the right response
Christopher Faulet021a8e42021-03-29 10:46:38 +02004121 * to the client, depending on the channel (408 on request side, 504 on response
4122 * side). All other errors must be handled by the caller.
4123 */
4124enum rule_result http_wait_for_msg_body(struct stream *s, struct channel *chn,
4125 unsigned int time, unsigned int bytes)
4126{
4127 struct session *sess = s->sess;
4128 struct http_txn *txn = s->txn;
4129 struct http_msg *msg = ((chn->flags & CF_ISRESP) ? &txn->rsp : &txn->req);
4130 struct htx *htx;
4131 enum rule_result ret = HTTP_RULE_RES_CONT;
4132
4133 htx = htxbuf(&chn->buf);
4134
4135 if (htx->flags & HTX_FL_PARSING_ERROR) {
4136 ret = HTTP_RULE_RES_BADREQ;
4137 goto end;
4138 }
4139 if (htx->flags & HTX_FL_PROCESSING_ERROR) {
4140 ret = HTTP_RULE_RES_ERROR;
4141 goto end;
4142 }
4143
4144 /* Do nothing for bodyless and CONNECT requests */
4145 if (txn->meth == HTTP_METH_CONNECT || (msg->flags & HTTP_MSGF_BODYLESS))
4146 goto end;
4147
4148 if (!(chn->flags & CF_ISRESP) && msg->msg_state < HTTP_MSG_DATA) {
4149 if (http_handle_expect_hdr(s, htx, msg) == -1) {
4150 ret = HTTP_RULE_RES_ERROR;
4151 goto end;
4152 }
4153 }
4154
4155 msg->msg_state = HTTP_MSG_DATA;
4156
4157 /* Now we're in HTTP_MSG_DATA. We just need to know if all data have
4158 * been received or if the buffer is full.
4159 */
4160 if ((htx->flags & HTX_FL_EOM) || htx_get_tail_type(htx) > HTX_BLK_DATA ||
4161 channel_htx_full(chn, htx, global.tune.maxrewrite))
4162 goto end;
4163
4164 if (bytes) {
4165 struct htx_blk *blk;
4166 unsigned int len = 0;
4167
4168 for (blk = htx_get_first_blk(htx); blk; blk = htx_get_next_blk(htx, blk)) {
4169 if (htx_get_blk_type(blk) != HTX_BLK_DATA)
4170 continue;
4171 len += htx_get_blksz(blk);
4172 if (len >= bytes)
4173 goto end;
4174 }
4175 }
4176
4177 if ((chn->flags & CF_READ_TIMEOUT) || tick_is_expired(chn->analyse_exp, now_ms)) {
4178 if (!(chn->flags & CF_ISRESP))
4179 goto abort_req;
4180 goto abort_res;
4181 }
4182
4183 /* we get here if we need to wait for more data */
4184 if (!(chn->flags & (CF_SHUTR | CF_READ_ERROR))) {
4185 if (!tick_isset(chn->analyse_exp))
4186 chn->analyse_exp = tick_add_ifset(now_ms, time);
4187 ret = HTTP_RULE_RES_YIELD;
4188 }
4189
4190 end:
4191 return ret;
4192
4193 abort_req:
4194 txn->status = 408;
4195 if (!(s->flags & SF_ERR_MASK))
4196 s->flags |= SF_ERR_CLITO;
4197 if (!(s->flags & SF_FINST_MASK))
4198 s->flags |= SF_FINST_D;
Willy Tarreau4781b152021-04-06 13:53:36 +02004199 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req);
Christopher Faulet021a8e42021-03-29 10:46:38 +02004200 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02004201 _HA_ATOMIC_INC(&sess->listener->counters->failed_req);
Christopher Faulet021a8e42021-03-29 10:46:38 +02004202 http_reply_and_close(s, txn->status, http_error_message(s));
4203 ret = HTTP_RULE_RES_ABRT;
4204 goto end;
4205
4206 abort_res:
4207 txn->status = 504;
4208 if (!(s->flags & SF_ERR_MASK))
4209 s->flags |= SF_ERR_SRVTO;
4210 if (!(s->flags & SF_FINST_MASK))
4211 s->flags |= SF_FINST_D;
4212 stream_inc_http_fail_ctr(s);
4213 http_reply_and_close(s, txn->status, http_error_message(s));
4214 ret = HTTP_RULE_RES_ABRT;
4215 goto end;
4216}
4217
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004218void http_perform_server_redirect(struct stream *s, struct stream_interface *si)
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004219{
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004220 struct channel *req = &s->req;
4221 struct channel *res = &s->res;
4222 struct server *srv;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004223 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004224 struct htx_sl *sl;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004225 struct ist path, location;
4226 unsigned int flags;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004227
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004228 /*
4229 * Create the location
4230 */
4231 chunk_reset(&trash);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004232
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004233 /* 1: add the server's prefix */
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004234 /* special prefix "/" means don't change URL */
4235 srv = __objt_server(s->target);
4236 if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') {
4237 if (!chunk_memcat(&trash, srv->rdr_pfx, srv->rdr_len))
4238 return;
4239 }
4240
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004241 /* 2: add the request Path */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004242 htx = htxbuf(&req->buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02004243 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004244 path = http_get_path(htx_sl_req_uri(sl));
Tim Duesterhused526372020-03-05 17:56:33 +01004245 if (!isttest(path))
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004246 return;
4247
4248 if (!chunk_memcat(&trash, path.ptr, path.len))
4249 return;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004250 location = ist2(trash.area, trash.data);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004251
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004252 /*
4253 * Create the 302 respone
4254 */
4255 htx = htx_from_buf(&res->buf);
4256 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
4257 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
4258 ist("HTTP/1.1"), ist("302"), ist("Found"));
4259 if (!sl)
4260 goto fail;
4261 sl->info.res.status = 302;
4262 s->txn->status = 302;
4263
4264 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) ||
4265 !htx_add_header(htx, ist("Connection"), ist("close")) ||
4266 !htx_add_header(htx, ist("Content-length"), ist("0")) ||
4267 !htx_add_header(htx, ist("Location"), location))
4268 goto fail;
4269
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004270 if (!htx_add_endof(htx, HTX_BLK_EOH))
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004271 goto fail;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004272
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004273 htx->flags |= HTX_FL_EOM;
Christopher Fauletc20afb82020-01-24 19:16:26 +01004274 htx_to_buf(htx, &res->buf);
Christopher Fauleta72a7e42020-01-28 09:28:11 +01004275 if (!http_forward_proxy_resp(s, 1))
4276 goto fail;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004277
4278 /* return without error. */
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004279 si_shutr(si);
4280 si_shutw(si);
4281 si->err_type = SI_ET_NONE;
4282 si->state = SI_ST_CLO;
4283
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004284 if (!(s->flags & SF_ERR_MASK))
4285 s->flags |= SF_ERR_LOCAL;
4286 if (!(s->flags & SF_FINST_MASK))
4287 s->flags |= SF_FINST_C;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004288
4289 /* FIXME: we should increase a counter of redirects per server and per backend. */
4290 srv_inc_sess_ctr(srv);
4291 srv_set_sess_last(srv);
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004292 return;
4293
4294 fail:
4295 /* If an error occurred, remove the incomplete HTTP response from the
4296 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004297 channel_htx_truncate(res, htx);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004298}
4299
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05004300/* This function terminates the request because it was completely analyzed or
Christopher Fauletf2824e62018-10-01 12:12:37 +02004301 * because an error was triggered during the body forwarding.
4302 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004303static void http_end_request(struct stream *s)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004304{
4305 struct channel *chn = &s->req;
4306 struct http_txn *txn = s->txn;
4307
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004308 DBG_TRACE_ENTER(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004309
Christopher Fauletb42a8b62018-11-19 21:59:00 +01004310 if (unlikely(txn->req.msg_state == HTTP_MSG_ERROR ||
4311 txn->rsp.msg_state == HTTP_MSG_ERROR)) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004312 channel_abort(chn);
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004313 channel_htx_truncate(chn, htxbuf(&chn->buf));
Christopher Fauletf2824e62018-10-01 12:12:37 +02004314 goto end;
4315 }
4316
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004317 if (unlikely(txn->req.msg_state < HTTP_MSG_DONE)) {
4318 DBG_TRACE_DEVEL("waiting end of the request", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004319 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004320 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004321
4322 if (txn->req.msg_state == HTTP_MSG_DONE) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004323 /* No need to read anymore, the request was completely parsed.
4324 * We can shut the read side unless we want to abort_on_close,
4325 * or we have a POST request. The issue with POST requests is
4326 * that some browsers still send a CRLF after the request, and
4327 * this CRLF must be read so that it does not remain in the kernel
4328 * buffers, otherwise a close could cause an RST on some systems
4329 * (eg: Linux).
4330 */
Christopher Faulet769d0e92019-03-22 14:23:18 +01004331 if (!(s->be->options & PR_O_ABRT_CLOSE) && txn->meth != HTTP_METH_POST)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004332 channel_dont_read(chn);
4333
4334 /* if the server closes the connection, we want to immediately react
4335 * and close the socket to save packets and syscalls.
4336 */
4337 s->si[1].flags |= SI_FL_NOHALF;
4338
4339 /* In any case we've finished parsing the request so we must
4340 * disable Nagle when sending data because 1) we're not going
4341 * to shut this side, and 2) the server is waiting for us to
4342 * send pending data.
4343 */
4344 chn->flags |= CF_NEVER_WAIT;
4345
Christopher Fauletd01ce402019-01-02 17:44:13 +01004346 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
4347 /* The server has not finished to respond, so we
4348 * don't want to move in order not to upset it.
4349 */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004350 DBG_TRACE_DEVEL("waiting end of the response", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletd01ce402019-01-02 17:44:13 +01004351 return;
4352 }
4353
Christopher Fauletf2824e62018-10-01 12:12:37 +02004354 /* When we get here, it means that both the request and the
4355 * response have finished receiving. Depending on the connection
4356 * mode, we'll have to wait for the last bytes to leave in either
4357 * direction, and sometimes for a close to be effective.
4358 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02004359 if (txn->flags & TX_CON_WANT_TUN) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004360 /* Tunnel mode will not have any analyser so it needs to
4361 * poll for reads.
4362 */
4363 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004364 if (b_data(&chn->buf)) {
4365 DBG_TRACE_DEVEL("waiting to flush the request", STRM_EV_HTTP_ANA, s, txn);
Christopher Faulet9768c262018-10-22 09:34:31 +02004366 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004367 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004368 txn->req.msg_state = HTTP_MSG_TUNNEL;
4369 }
4370 else {
4371 /* we're not expecting any new data to come for this
4372 * transaction, so we can close it.
Christopher Faulet9768c262018-10-22 09:34:31 +02004373 *
4374 * However, there is an exception if the response
4375 * length is undefined. In this case, we need to wait
4376 * the close from the server. The response will be
4377 * switched in TUNNEL mode until the end.
Christopher Fauletf2824e62018-10-01 12:12:37 +02004378 */
4379 if (!(txn->rsp.flags & HTTP_MSGF_XFER_LEN) &&
4380 txn->rsp.msg_state != HTTP_MSG_CLOSED)
Christopher Faulet9768c262018-10-22 09:34:31 +02004381 goto check_channel_flags;
Christopher Fauletf2824e62018-10-01 12:12:37 +02004382
4383 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4384 channel_shutr_now(chn);
4385 channel_shutw_now(chn);
4386 }
4387 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004388 goto check_channel_flags;
4389 }
4390
4391 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
4392 http_msg_closing:
4393 /* nothing else to forward, just waiting for the output buffer
4394 * to be empty and for the shutw_now to take effect.
4395 */
4396 if (channel_is_empty(chn)) {
4397 txn->req.msg_state = HTTP_MSG_CLOSED;
4398 goto http_msg_closed;
4399 }
4400 else if (chn->flags & CF_SHUTW) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004401 txn->req.msg_state = HTTP_MSG_ERROR;
4402 goto end;
4403 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004404 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004405 return;
4406 }
4407
4408 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
4409 http_msg_closed:
Christopher Fauletf2824e62018-10-01 12:12:37 +02004410 /* if we don't know whether the server will close, we need to hard close */
4411 if (txn->rsp.flags & HTTP_MSGF_XFER_LEN)
4412 s->si[1].flags |= SI_FL_NOLINGER; /* we want to close ASAP */
Christopher Fauletf2824e62018-10-01 12:12:37 +02004413 /* see above in MSG_DONE why we only do this in these states */
Christopher Faulet769d0e92019-03-22 14:23:18 +01004414 if (!(s->be->options & PR_O_ABRT_CLOSE))
Christopher Fauletf2824e62018-10-01 12:12:37 +02004415 channel_dont_read(chn);
4416 goto end;
4417 }
4418
4419 check_channel_flags:
4420 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4421 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4422 /* if we've just closed an output, let's switch */
4423 txn->req.msg_state = HTTP_MSG_CLOSING;
4424 goto http_msg_closing;
4425 }
4426
4427 end:
4428 chn->analysers &= AN_REQ_FLT_END;
Christopher Faulet198ef8b2020-12-15 13:32:55 +01004429 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
4430 chn->flags |= CF_NEVER_WAIT;
4431 if (HAS_REQ_DATA_FILTERS(s))
Christopher Fauletf2824e62018-10-01 12:12:37 +02004432 chn->analysers |= AN_REQ_FLT_XFER_DATA;
Christopher Faulet198ef8b2020-12-15 13:32:55 +01004433 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004434 channel_auto_close(chn);
4435 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004436 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004437}
4438
4439
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05004440/* This function terminates the response because it was completely analyzed or
Christopher Fauletf2824e62018-10-01 12:12:37 +02004441 * because an error was triggered during the body forwarding.
4442 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004443static void http_end_response(struct stream *s)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004444{
4445 struct channel *chn = &s->res;
4446 struct http_txn *txn = s->txn;
4447
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004448 DBG_TRACE_ENTER(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004449
Christopher Fauletb42a8b62018-11-19 21:59:00 +01004450 if (unlikely(txn->req.msg_state == HTTP_MSG_ERROR ||
4451 txn->rsp.msg_state == HTTP_MSG_ERROR)) {
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004452 channel_htx_truncate(&s->req, htxbuf(&s->req.buf));
Christopher Faulet9768c262018-10-22 09:34:31 +02004453 channel_abort(&s->req);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004454 goto end;
4455 }
4456
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004457 if (unlikely(txn->rsp.msg_state < HTTP_MSG_DONE)) {
4458 DBG_TRACE_DEVEL("waiting end of the response", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004459 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004460 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004461
4462 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
4463 /* In theory, we don't need to read anymore, but we must
4464 * still monitor the server connection for a possible close
4465 * while the request is being uploaded, so we don't disable
4466 * reading.
4467 */
4468 /* channel_dont_read(chn); */
4469
4470 if (txn->req.msg_state < HTTP_MSG_DONE) {
4471 /* The client seems to still be sending data, probably
4472 * because we got an error response during an upload.
4473 * We have the choice of either breaking the connection
4474 * or letting it pass through. Let's do the later.
4475 */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004476 DBG_TRACE_DEVEL("waiting end of the request", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004477 return;
4478 }
4479
4480 /* When we get here, it means that both the request and the
4481 * response have finished receiving. Depending on the connection
4482 * mode, we'll have to wait for the last bytes to leave in either
4483 * direction, and sometimes for a close to be effective.
4484 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02004485 if (txn->flags & TX_CON_WANT_TUN) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004486 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004487 if (b_data(&chn->buf)) {
4488 DBG_TRACE_DEVEL("waiting to flush the respone", STRM_EV_HTTP_ANA, s, txn);
Christopher Faulet9768c262018-10-22 09:34:31 +02004489 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004490 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004491 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
4492 }
4493 else {
4494 /* we're not expecting any new data to come for this
4495 * transaction, so we can close it.
4496 */
4497 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4498 channel_shutr_now(chn);
4499 channel_shutw_now(chn);
4500 }
4501 }
4502 goto check_channel_flags;
4503 }
4504
4505 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
4506 http_msg_closing:
4507 /* nothing else to forward, just waiting for the output buffer
4508 * to be empty and for the shutw_now to take effect.
4509 */
4510 if (channel_is_empty(chn)) {
4511 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4512 goto http_msg_closed;
4513 }
4514 else if (chn->flags & CF_SHUTW) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004515 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau4781b152021-04-06 13:53:36 +02004516 _HA_ATOMIC_INC(&strm_sess(s)->fe->fe_counters.cli_aborts);
4517 _HA_ATOMIC_INC(&s->be->be_counters.cli_aborts);
William Lallemand36119de2021-03-08 15:26:48 +01004518 if (strm_sess(s)->listener && strm_sess(s)->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02004519 _HA_ATOMIC_INC(&strm_sess(s)->listener->counters->cli_aborts);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004520 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02004521 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.cli_aborts);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004522 goto end;
4523 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004524 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004525 return;
4526 }
4527
4528 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
4529 http_msg_closed:
4530 /* drop any pending data */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004531 channel_htx_truncate(&s->req, htxbuf(&s->req.buf));
Christopher Faulet9768c262018-10-22 09:34:31 +02004532 channel_abort(&s->req);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004533 goto end;
4534 }
4535
4536 check_channel_flags:
4537 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4538 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4539 /* if we've just closed an output, let's switch */
4540 txn->rsp.msg_state = HTTP_MSG_CLOSING;
4541 goto http_msg_closing;
4542 }
4543
4544 end:
4545 chn->analysers &= AN_RES_FLT_END;
Christopher Faulet198ef8b2020-12-15 13:32:55 +01004546 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
4547 chn->flags |= CF_NEVER_WAIT;
4548 if (HAS_RSP_DATA_FILTERS(s))
4549 chn->analysers |= AN_RES_FLT_XFER_DATA;
4550 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004551 channel_auto_close(chn);
4552 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004553 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004554}
4555
Christopher Fauletef70e252020-01-28 09:26:19 +01004556/* Forward a response generated by HAProxy (error/redirect/return). This
4557 * function forwards all pending incoming data. If <final> is set to 0, nothing
4558 * more is performed. It is used for 1xx informational messages. Otherwise, the
Christopher Faulet507479b2020-05-15 12:29:46 +02004559 * transaction is terminated and the request is emptied. On success 1 is
Christopher Faulet40e6b552020-06-25 16:04:50 +02004560 * returned. If an error occurred, 0 is returned. If it fails, this function
4561 * only exits. It is the caller responsibility to do the cleanup.
Christopher Fauletef70e252020-01-28 09:26:19 +01004562 */
4563int http_forward_proxy_resp(struct stream *s, int final)
4564{
4565 struct channel *req = &s->req;
4566 struct channel *res = &s->res;
4567 struct htx *htx = htxbuf(&res->buf);
4568 size_t data;
4569
4570 if (final) {
4571 htx->flags |= HTX_FL_PROXY_RESP;
Christopher Faulet507479b2020-05-15 12:29:46 +02004572
Christopher Fauletaab1b672020-11-18 16:44:02 +01004573 if (!htx_is_empty(htx) && !http_eval_after_res_rules(s))
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01004574 return 0;
Christopher Fauletef70e252020-01-28 09:26:19 +01004575
Christopher Fauletd6c48362020-10-19 18:01:38 +02004576 if (s->txn->meth == HTTP_METH_HEAD)
4577 htx_skip_msg_payload(htx);
4578
Christopher Fauletef70e252020-01-28 09:26:19 +01004579 channel_auto_read(req);
4580 channel_abort(req);
4581 channel_auto_close(req);
4582 channel_htx_erase(req, htxbuf(&req->buf));
4583
4584 res->wex = tick_add_ifset(now_ms, res->wto);
4585 channel_auto_read(res);
4586 channel_auto_close(res);
4587 channel_shutr_now(res);
Christopher Faulet1a9db7c2020-06-25 15:36:45 +02004588 res->flags |= CF_EOI; /* The response is terminated, add EOI */
Christopher Faulet42432f32020-11-20 17:43:16 +01004589 htxbuf(&res->buf)->flags |= HTX_FL_EOM; /* no more data are expected */
Christopher Fauletef70e252020-01-28 09:26:19 +01004590 }
Christopher Fauletcf6898c2020-06-25 15:55:11 +02004591 else {
4592 /* Send ASAP informational messages. Rely on CF_EOI for final
4593 * response.
4594 */
4595 res->flags |= CF_SEND_DONTWAIT;
4596 }
Christopher Fauletef70e252020-01-28 09:26:19 +01004597
4598 data = htx->data - co_data(res);
4599 c_adv(res, data);
4600 htx->first = -1;
4601 res->total += data;
4602 return 1;
4603}
4604
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004605void http_server_error(struct stream *s, struct stream_interface *si, int err,
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004606 int finst, struct http_reply *msg)
Christopher Faulet0f226952018-10-22 09:29:56 +02004607{
Christopher Faulet72c7d8d2020-01-27 15:32:25 +01004608 http_reply_and_close(s, s->txn->status, msg);
Christopher Faulet0f226952018-10-22 09:29:56 +02004609 if (!(s->flags & SF_ERR_MASK))
4610 s->flags |= err;
4611 if (!(s->flags & SF_FINST_MASK))
4612 s->flags |= finst;
4613}
4614
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004615void http_reply_and_close(struct stream *s, short status, struct http_reply *msg)
Christopher Faulet0f226952018-10-22 09:29:56 +02004616{
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004617 if (!msg) {
4618 channel_htx_truncate(&s->res, htxbuf(&s->res.buf));
4619 goto end;
4620 }
4621
4622 if (http_reply_message(s, msg) == -1) {
4623 /* On error, return a 500 error message, but don't rewrite it if
Christopher Faulet40e6b552020-06-25 16:04:50 +02004624 * it is already an internal error. If it was already a "const"
4625 * 500 error, just fail.
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004626 */
Christopher Faulet40e6b552020-06-25 16:04:50 +02004627 if (s->txn->status == 500) {
4628 if (s->txn->flags & TX_CONST_REPLY)
4629 goto end;
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004630 s->txn->flags |= TX_CONST_REPLY;
Christopher Faulet40e6b552020-06-25 16:04:50 +02004631 }
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004632 s->txn->status = 500;
4633 s->txn->http_reply = NULL;
4634 return http_reply_and_close(s, s->txn->status, http_error_message(s));
4635 }
4636
4637end:
4638 s->res.wex = tick_add_ifset(now_ms, s->res.wto);
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004639
Christopher Faulet0f226952018-10-22 09:29:56 +02004640 channel_auto_read(&s->req);
4641 channel_abort(&s->req);
4642 channel_auto_close(&s->req);
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004643 channel_htx_erase(&s->req, htxbuf(&s->req.buf));
Christopher Faulet72c7d8d2020-01-27 15:32:25 +01004644 channel_auto_read(&s->res);
4645 channel_auto_close(&s->res);
4646 channel_shutr_now(&s->res);
Christopher Faulet0f226952018-10-22 09:29:56 +02004647}
4648
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004649struct http_reply *http_error_message(struct stream *s)
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004650{
4651 const int msgnum = http_get_status_idx(s->txn->status);
4652
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004653 if (s->txn->http_reply)
4654 return s->txn->http_reply;
4655 else if (s->be->replies[msgnum])
4656 return s->be->replies[msgnum];
4657 else if (strm_fe(s)->replies[msgnum])
4658 return strm_fe(s)->replies[msgnum];
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004659 else
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004660 return &http_err_replies[msgnum];
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004661}
4662
Christopher Faulet40e6b552020-06-25 16:04:50 +02004663/* Produces an HTX message from an http reply. Depending on the http reply type,
4664 * a, errorfile, an raw file or a log-format string is used. On success, it
4665 * returns 0. If an error occurs -1 is returned. If it fails, this function only
4666 * exits. It is the caller responsibility to do the cleanup.
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004667 */
Christopher Fauletae43b6c2020-05-27 15:24:22 +02004668int http_reply_to_htx(struct stream *s, struct htx *htx, struct http_reply *reply)
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004669{
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004670 struct buffer *errmsg;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004671 struct htx_sl *sl;
4672 struct buffer *body = NULL;
4673 const char *status, *reason, *clen, *ctype;
4674 unsigned int slflags;
4675 int ret = 0;
4676
Christopher Faulete29a97e2020-05-14 14:49:25 +02004677 /*
4678 * - HTTP_REPLY_ERRFILES unexpected here. handled as no payload if so
4679 *
4680 * - HTTP_REPLY_INDIRECT: switch on another reply if defined or handled
4681 * as no payload if NULL. the TXN status code is set with the status
4682 * of the original reply.
4683 */
4684
4685 if (reply->type == HTTP_REPLY_INDIRECT) {
4686 if (reply->body.reply)
4687 reply = reply->body.reply;
4688 }
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004689 if (reply->type == HTTP_REPLY_ERRMSG && !reply->body.errmsg) {
4690 /* get default error message */
4691 if (reply == s->txn->http_reply)
4692 s->txn->http_reply = NULL;
4693 reply = http_error_message(s);
4694 if (reply->type == HTTP_REPLY_INDIRECT) {
4695 if (reply->body.reply)
4696 reply = reply->body.reply;
4697 }
4698 }
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004699
4700 if (reply->type == HTTP_REPLY_ERRMSG) {
4701 /* implicit or explicit error message*/
4702 errmsg = reply->body.errmsg;
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004703 if (errmsg && !b_is_null(errmsg)) {
Christopher Faulet20567362020-05-15 14:52:49 +02004704 if (!htx_copy_msg(htx, errmsg))
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004705 goto fail;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004706 }
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004707 }
4708 else {
4709 /* no payload, file or log-format string */
4710 if (reply->type == HTTP_REPLY_RAW) {
4711 /* file */
4712 body = &reply->body.obj;
4713 }
4714 else if (reply->type == HTTP_REPLY_LOGFMT) {
4715 /* log-format string */
4716 body = alloc_trash_chunk();
4717 if (!body)
4718 goto fail_alloc;
4719 body->data = build_logline(s, body->area, body->size, &reply->body.fmt);
4720 }
4721 /* else no payload */
4722
4723 status = ultoa(reply->status);
4724 reason = http_get_reason(reply->status);
4725 slflags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_CLEN);
4726 if (!body || !b_data(body))
4727 slflags |= HTX_SL_F_BODYLESS;
4728 sl = htx_add_stline(htx, HTX_BLK_RES_SL, slflags, ist("HTTP/1.1"), ist(status), ist(reason));
4729 if (!sl)
4730 goto fail;
4731 sl->info.res.status = reply->status;
4732
4733 clen = (body ? ultoa(b_data(body)) : "0");
4734 ctype = reply->ctype;
4735
4736 if (!LIST_ISEMPTY(&reply->hdrs)) {
4737 struct http_reply_hdr *hdr;
4738 struct buffer *value = alloc_trash_chunk();
4739
4740 if (!value)
4741 goto fail;
4742
4743 list_for_each_entry(hdr, &reply->hdrs, list) {
4744 chunk_reset(value);
4745 value->data = build_logline(s, value->area, value->size, &hdr->value);
4746 if (b_data(value) && !htx_add_header(htx, hdr->name, ist2(b_head(value), b_data(value)))) {
4747 free_trash_chunk(value);
4748 goto fail;
4749 }
4750 chunk_reset(value);
4751 }
4752 free_trash_chunk(value);
4753 }
4754
4755 if (!htx_add_header(htx, ist("content-length"), ist(clen)) ||
4756 (body && b_data(body) && ctype && !htx_add_header(htx, ist("content-type"), ist(ctype))) ||
4757 !htx_add_endof(htx, HTX_BLK_EOH) ||
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004758 (body && b_data(body) && !htx_add_data_atonce(htx, ist2(b_head(body), b_data(body)))))
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004759 goto fail;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004760
4761 htx->flags |= HTX_FL_EOM;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004762 }
4763
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004764 leave:
4765 if (reply->type == HTTP_REPLY_LOGFMT)
4766 free_trash_chunk(body);
4767 return ret;
4768
4769 fail_alloc:
4770 if (!(s->flags & SF_ERR_MASK))
4771 s->flags |= SF_ERR_RESOURCE;
Christopher Faulet97e466c2020-05-15 15:12:47 +02004772 /* fall through */
4773 fail:
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004774 ret = -1;
4775 goto leave;
Christopher Faulet97e466c2020-05-15 15:12:47 +02004776}
4777
4778/* Send an http reply to the client. On success, it returns 0. If an error
Christopher Faulet40e6b552020-06-25 16:04:50 +02004779 * occurs -1 is returned and the response channel is truncated, removing this
4780 * way the faulty reply. This function may fail when the reply is formatted
4781 * (http_reply_to_htx) or when the reply is forwarded
4782 * (http_forward_proxy_resp). On the last case, it is because a
4783 * http-after-response rule fails.
Christopher Faulet97e466c2020-05-15 15:12:47 +02004784 */
4785int http_reply_message(struct stream *s, struct http_reply *reply)
4786{
4787 struct channel *res = &s->res;
4788 struct htx *htx = htx_from_buf(&res->buf);
4789
4790 if (s->txn->status == -1)
4791 s->txn->status = reply->status;
4792 channel_htx_truncate(res, htx);
4793
4794 if (http_reply_to_htx(s, htx, reply) == -1)
4795 goto fail;
4796
4797 htx_to_buf(htx, &s->res.buf);
4798 if (!http_forward_proxy_resp(s, 1))
4799 goto fail;
4800 return 0;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004801
4802 fail:
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004803 channel_htx_truncate(res, htx);
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004804 if (!(s->flags & SF_ERR_MASK))
4805 s->flags |= SF_ERR_PRXCOND;
Christopher Faulet97e466c2020-05-15 15:12:47 +02004806 return -1;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004807}
4808
Christopher Faulet304cc402019-07-15 15:46:28 +02004809/* Return the error message corresponding to si->err_type. It is assumed
4810 * that the server side is closed. Note that err_type is actually a
4811 * bitmask, where almost only aborts may be cumulated with other
4812 * values. We consider that aborted operations are more important
4813 * than timeouts or errors due to the fact that nobody else in the
4814 * logs might explain incomplete retries. All others should avoid
4815 * being cumulated. It should normally not be possible to have multiple
4816 * aborts at once, but just in case, the first one in sequence is reported.
4817 * Note that connection errors appearing on the second request of a keep-alive
4818 * connection are not reported since this allows the client to retry.
4819 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004820void http_return_srv_error(struct stream *s, struct stream_interface *si)
Christopher Faulet304cc402019-07-15 15:46:28 +02004821{
4822 int err_type = si->err_type;
4823
4824 /* set s->txn->status for http_error_message(s) */
4825 s->txn->status = 503;
4826
4827 if (err_type & SI_ET_QUEUE_ABRT)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004828 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_Q,
4829 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004830 else if (err_type & SI_ET_CONN_ABRT)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004831 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_C,
4832 (s->txn->flags & TX_NOT_FIRST) ? NULL :
4833 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004834 else if (err_type & SI_ET_QUEUE_TO)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004835 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_Q,
4836 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004837 else if (err_type & SI_ET_QUEUE_ERR)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004838 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_Q,
4839 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004840 else if (err_type & SI_ET_CONN_TO)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004841 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_C,
4842 (s->txn->flags & TX_NOT_FIRST) ? NULL :
4843 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004844 else if (err_type & SI_ET_CONN_ERR)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004845 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_C,
4846 (s->flags & SF_SRV_REUSED) ? NULL :
4847 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004848 else if (err_type & SI_ET_CONN_RES)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004849 http_server_error(s, si, SF_ERR_RESOURCE, SF_FINST_C,
4850 (s->txn->flags & TX_NOT_FIRST) ? NULL :
4851 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004852 else { /* SI_ET_CONN_OTHER and others */
4853 s->txn->status = 500;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004854 http_server_error(s, si, SF_ERR_INTERNAL, SF_FINST_C,
4855 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004856 }
4857}
4858
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004859
Christopher Faulet4a28a532019-03-01 11:19:40 +01004860/* Handle Expect: 100-continue for HTTP/1.1 messages if necessary. It returns 0
4861 * on success and -1 on error.
4862 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004863static int http_handle_expect_hdr(struct stream *s, struct htx *htx, struct http_msg *msg)
Christopher Faulet4a28a532019-03-01 11:19:40 +01004864{
4865 /* If we have HTTP/1.1 message with a body and Expect: 100-continue,
4866 * then we must send an HTTP/1.1 100 Continue intermediate response.
4867 */
4868 if (msg->msg_state == HTTP_MSG_BODY && (msg->flags & HTTP_MSGF_VER_11) &&
4869 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
4870 struct ist hdr = { .ptr = "Expect", .len = 6 };
4871 struct http_hdr_ctx ctx;
4872
4873 ctx.blk = NULL;
4874 /* Expect is allowed in 1.1, look for it */
4875 if (http_find_header(htx, hdr, &ctx, 0) &&
4876 unlikely(isteqi(ctx.value, ist2("100-continue", 12)))) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004877 if (http_reply_100_continue(s) == -1)
Christopher Faulet4a28a532019-03-01 11:19:40 +01004878 return -1;
4879 http_remove_header(htx, &ctx);
4880 }
4881 }
4882 return 0;
4883}
4884
Christopher Faulet23a3c792018-11-28 10:01:23 +01004885/* Send a 100-Continue response to the client. It returns 0 on success and -1
4886 * on error. The response channel is updated accordingly.
4887 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004888static int http_reply_100_continue(struct stream *s)
Christopher Faulet23a3c792018-11-28 10:01:23 +01004889{
4890 struct channel *res = &s->res;
4891 struct htx *htx = htx_from_buf(&res->buf);
4892 struct htx_sl *sl;
4893 unsigned int flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|
4894 HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
Christopher Faulet23a3c792018-11-28 10:01:23 +01004895
4896 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
4897 ist("HTTP/1.1"), ist("100"), ist("Continue"));
4898 if (!sl)
4899 goto fail;
4900 sl->info.res.status = 100;
4901
Christopher Faulet1d5ec092019-06-26 14:23:54 +02004902 if (!htx_add_endof(htx, HTX_BLK_EOH))
Christopher Faulet23a3c792018-11-28 10:01:23 +01004903 goto fail;
4904
Christopher Fauleta72a7e42020-01-28 09:28:11 +01004905 if (!http_forward_proxy_resp(s, 0))
4906 goto fail;
Christopher Faulet23a3c792018-11-28 10:01:23 +01004907 return 0;
4908
4909 fail:
4910 /* If an error occurred, remove the incomplete HTTP response from the
4911 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004912 channel_htx_truncate(res, htx);
Christopher Faulet23a3c792018-11-28 10:01:23 +01004913 return -1;
4914}
4915
Christopher Faulet12c51e22018-11-28 15:59:42 +01004916
Christopher Faulet0f226952018-10-22 09:29:56 +02004917/*
4918 * Capture headers from message <htx> according to header list <cap_hdr>, and
4919 * fill the <cap> pointers appropriately.
4920 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004921static void http_capture_headers(struct htx *htx, char **cap, struct cap_hdr *cap_hdr)
Christopher Faulet0f226952018-10-22 09:29:56 +02004922{
4923 struct cap_hdr *h;
4924 int32_t pos;
4925
Christopher Fauleta3f15502019-05-13 15:27:23 +02004926 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet0f226952018-10-22 09:29:56 +02004927 struct htx_blk *blk = htx_get_blk(htx, pos);
4928 enum htx_blk_type type = htx_get_blk_type(blk);
4929 struct ist n, v;
4930
4931 if (type == HTX_BLK_EOH)
4932 break;
4933 if (type != HTX_BLK_HDR)
4934 continue;
4935
4936 n = htx_get_blk_name(htx, blk);
4937
4938 for (h = cap_hdr; h; h = h->next) {
4939 if (h->namelen && (h->namelen == n.len) &&
4940 (strncasecmp(n.ptr, h->name, h->namelen) == 0)) {
4941 if (cap[h->index] == NULL)
4942 cap[h->index] =
4943 pool_alloc(h->pool);
4944
4945 if (cap[h->index] == NULL) {
4946 ha_alert("HTTP capture : out of memory.\n");
4947 break;
4948 }
4949
4950 v = htx_get_blk_value(htx, blk);
4951 if (v.len > h->len)
4952 v.len = h->len;
4953
4954 memcpy(cap[h->index], v.ptr, v.len);
4955 cap[h->index][v.len]=0;
4956 }
4957 }
4958 }
4959}
4960
Christopher Faulet0b6bdc52018-10-24 11:05:36 +02004961/* Delete a value in a header between delimiters <from> and <next>. The header
4962 * itself is delimited by <start> and <end> pointers. The number of characters
4963 * displaced is returned, and the pointer to the first delimiter is updated if
4964 * required. The function tries as much as possible to respect the following
4965 * principles :
4966 * - replace <from> delimiter by the <next> one unless <from> points to <start>,
4967 * in which case <next> is simply removed
4968 * - set exactly one space character after the new first delimiter, unless there
4969 * are not enough characters in the block being moved to do so.
4970 * - remove unneeded spaces before the previous delimiter and after the new
4971 * one.
4972 *
4973 * It is the caller's responsibility to ensure that :
4974 * - <from> points to a valid delimiter or <start> ;
4975 * - <next> points to a valid delimiter or <end> ;
4976 * - there are non-space chars before <from>.
4977 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004978static int http_del_hdr_value(char *start, char *end, char **from, char *next)
Christopher Faulet0b6bdc52018-10-24 11:05:36 +02004979{
4980 char *prev = *from;
4981
4982 if (prev == start) {
4983 /* We're removing the first value. eat the semicolon, if <next>
4984 * is lower than <end> */
4985 if (next < end)
4986 next++;
4987
4988 while (next < end && HTTP_IS_SPHT(*next))
4989 next++;
4990 }
4991 else {
4992 /* Remove useless spaces before the old delimiter. */
4993 while (HTTP_IS_SPHT(*(prev-1)))
4994 prev--;
4995 *from = prev;
4996
4997 /* copy the delimiter and if possible a space if we're
4998 * not at the end of the line.
4999 */
5000 if (next < end) {
5001 *prev++ = *next++;
5002 if (prev + 1 < next)
5003 *prev++ = ' ';
5004 while (next < end && HTTP_IS_SPHT(*next))
5005 next++;
5006 }
5007 }
5008 memmove(prev, next, end - next);
5009 return (prev - next);
5010}
5011
Christopher Faulet0f226952018-10-22 09:29:56 +02005012
5013/* Formats the start line of the request (without CRLF) and puts it in <str> and
Joseph Herlantc42c0e92018-11-25 10:43:27 -08005014 * return the written length. The line can be truncated if it exceeds <len>.
Christopher Faulet0f226952018-10-22 09:29:56 +02005015 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005016static size_t http_fmt_req_line(const struct htx_sl *sl, char *str, size_t len)
Christopher Faulet0f226952018-10-22 09:29:56 +02005017{
5018 struct ist dst = ist2(str, 0);
5019
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005020 if (istcat(&dst, htx_sl_req_meth(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 if (istcat(&dst, htx_sl_req_uri(sl), len) == -1)
Christopher Faulet0f226952018-10-22 09:29:56 +02005027 goto end;
5028 if (dst.len + 1 > len)
5029 goto end;
5030 dst.ptr[dst.len++] = ' ';
5031
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005032 istcat(&dst, htx_sl_req_vsn(sl), len);
Christopher Faulet0f226952018-10-22 09:29:56 +02005033 end:
5034 return dst.len;
5035}
5036
5037/*
5038 * Print a debug line with a start line.
5039 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005040static void http_debug_stline(const char *dir, struct stream *s, const struct htx_sl *sl)
Christopher Faulet0f226952018-10-22 09:29:56 +02005041{
5042 struct session *sess = strm_sess(s);
5043 int max;
5044
5045 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
5046 dir,
5047 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->handle.fd : -1,
5048 objt_cs(s->si[1].end) ? (unsigned short)objt_cs(s->si[1].end)->conn->handle.fd : -1);
5049
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005050 max = HTX_SL_P1_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02005051 UBOUND(max, trash.size - trash.data - 3);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005052 chunk_memcat(&trash, HTX_SL_P1_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02005053 trash.area[trash.data++] = ' ';
5054
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005055 max = HTX_SL_P2_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02005056 UBOUND(max, trash.size - trash.data - 2);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005057 chunk_memcat(&trash, HTX_SL_P2_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02005058 trash.area[trash.data++] = ' ';
5059
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005060 max = HTX_SL_P3_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02005061 UBOUND(max, trash.size - trash.data - 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005062 chunk_memcat(&trash, HTX_SL_P3_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02005063 trash.area[trash.data++] = '\n';
5064
Willy Tarreau2e8ab6b2020-03-14 11:03:20 +01005065 DISGUISE(write(1, trash.area, trash.data));
Christopher Faulet0f226952018-10-22 09:29:56 +02005066}
5067
5068/*
5069 * Print a debug line with a header.
5070 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005071static 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 +02005072{
5073 struct session *sess = strm_sess(s);
5074 int max;
5075
5076 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
5077 dir,
5078 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->handle.fd : -1,
5079 objt_cs(s->si[1].end) ? (unsigned short)objt_cs(s->si[1].end)->conn->handle.fd : -1);
5080
5081 max = n.len;
5082 UBOUND(max, trash.size - trash.data - 3);
5083 chunk_memcat(&trash, n.ptr, max);
5084 trash.area[trash.data++] = ':';
5085 trash.area[trash.data++] = ' ';
5086
5087 max = v.len;
5088 UBOUND(max, trash.size - trash.data - 1);
5089 chunk_memcat(&trash, v.ptr, max);
5090 trash.area[trash.data++] = '\n';
5091
Willy Tarreau2e8ab6b2020-03-14 11:03:20 +01005092 DISGUISE(write(1, trash.area, trash.data));
Christopher Faulet0f226952018-10-22 09:29:56 +02005093}
5094
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005095/* Allocate a new HTTP transaction for stream <s> unless there is one already.
5096 * In case of allocation failure, everything allocated is freed and NULL is
5097 * returned. Otherwise the new transaction is assigned to the stream and
5098 * returned.
5099 */
5100struct http_txn *http_alloc_txn(struct stream *s)
5101{
5102 struct http_txn *txn = s->txn;
5103
5104 if (txn)
5105 return txn;
5106
5107 txn = pool_alloc(pool_head_http_txn);
5108 if (!txn)
5109 return txn;
5110
5111 s->txn = txn;
5112 return txn;
5113}
5114
5115void http_txn_reset_req(struct http_txn *txn)
5116{
Christopher Faulet1aea50e2020-01-17 16:03:53 +01005117 txn->req.flags = 0;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005118 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
5119}
5120
5121void http_txn_reset_res(struct http_txn *txn)
5122{
Christopher Faulet1aea50e2020-01-17 16:03:53 +01005123 txn->rsp.flags = 0;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005124 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
5125}
5126
5127/*
Christopher Faulet75f619a2021-03-08 19:12:58 +01005128 * Create and initialize a new HTTP transaction for stream <s>. This should be
5129 * used before processing any new request. It returns the transaction or NLULL
5130 * on error.
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005131 */
Christopher Faulet75f619a2021-03-08 19:12:58 +01005132struct http_txn *http_create_txn(struct stream *s)
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005133{
Christopher Faulet75f619a2021-03-08 19:12:58 +01005134 struct http_txn *txn;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005135 struct conn_stream *cs = objt_cs(s->si[0].end);
5136
Christopher Faulet75f619a2021-03-08 19:12:58 +01005137 txn = pool_alloc(pool_head_http_txn);
5138 if (!txn)
5139 return NULL;
5140 s->txn = txn;
5141
Christopher Fauletda831fa2020-10-06 17:58:43 +02005142 txn->flags = ((cs && cs->flags & CS_FL_NOT_FIRST) ? TX_NOT_FIRST : 0);
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005143 txn->status = -1;
Christopher Faulet5cb513a2020-05-13 17:56:56 +02005144 txn->http_reply = NULL;
Willy Tarreau8b507582020-02-25 09:35:07 +01005145 write_u32(txn->cache_hash, 0);
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005146
5147 txn->cookie_first_date = 0;
5148 txn->cookie_last_date = 0;
5149
5150 txn->srv_cookie = NULL;
5151 txn->cli_cookie = NULL;
5152 txn->uri = NULL;
5153
5154 http_txn_reset_req(txn);
5155 http_txn_reset_res(txn);
5156
5157 txn->req.chn = &s->req;
5158 txn->rsp.chn = &s->res;
5159
5160 txn->auth.method = HTTP_AUTH_UNKNOWN;
5161
5162 vars_init(&s->vars_txn, SCOPE_TXN);
5163 vars_init(&s->vars_reqres, SCOPE_REQ);
Christopher Faulet75f619a2021-03-08 19:12:58 +01005164
5165 return txn;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005166}
5167
5168/* to be used at the end of a transaction */
Christopher Faulet75f619a2021-03-08 19:12:58 +01005169void http_destroy_txn(struct stream *s)
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005170{
5171 struct http_txn *txn = s->txn;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005172
5173 /* these ones will have been dynamically allocated */
5174 pool_free(pool_head_requri, txn->uri);
5175 pool_free(pool_head_capture, txn->cli_cookie);
5176 pool_free(pool_head_capture, txn->srv_cookie);
Tim Duesterhusa17e6622020-03-05 20:19:02 +01005177 pool_free(pool_head_uniqueid, s->unique_id.ptr);
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005178
Tim Duesterhusa17e6622020-03-05 20:19:02 +01005179 s->unique_id = IST_NULL;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005180 txn->uri = NULL;
5181 txn->srv_cookie = NULL;
5182 txn->cli_cookie = NULL;
5183
Christopher Faulet59399252019-11-07 14:27:52 +01005184 if (!LIST_ISEMPTY(&s->vars_txn.head))
5185 vars_prune(&s->vars_txn, s->sess, s);
5186 if (!LIST_ISEMPTY(&s->vars_reqres.head))
5187 vars_prune(&s->vars_reqres, s->sess, s);
Christopher Faulet75f619a2021-03-08 19:12:58 +01005188
5189 pool_free(pool_head_http_txn, txn);
5190 s->txn = NULL;
Christopher Faulet59399252019-11-07 14:27:52 +01005191}
5192
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005193
5194DECLARE_POOL(pool_head_http_txn, "http_txn", sizeof(struct http_txn));
Christopher Faulet0f226952018-10-22 09:29:56 +02005195
Christopher Fauletf4eb75d2018-10-11 15:55:07 +02005196__attribute__((constructor))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005197static void __http_protocol_init(void)
Christopher Fauletf4eb75d2018-10-11 15:55:07 +02005198{
5199}
5200
5201
5202/*
5203 * Local variables:
5204 * c-indent-level: 8
5205 * c-basic-offset: 8
5206 * End:
5207 */