blob: de99a5d92bd7206b56a43141ded69fe0b0b68f6d [file] [log] [blame]
Christopher Fauletf4eb75d2018-10-11 15:55:07 +02001/*
2 * HTTP protocol analyzer
3 *
4 * Copyright (C) 2018 HAProxy Technologies, Christopher Faulet <cfaulet@haproxy.com>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
Willy Tarreaudcc048a2020-06-04 19:11:43 +020013#include <haproxy/acl.h>
Willy Tarreau122eba92020-06-04 10:15:32 +020014#include <haproxy/action-t.h>
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020015#include <haproxy/api.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020016#include <haproxy/backend.h>
Willy Tarreau8d366972020-05-27 16:10:29 +020017#include <haproxy/base64.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020018#include <haproxy/capture-t.h>
Willy Tarreauf1d32c42020-06-04 21:07:02 +020019#include <haproxy/channel.h>
Willy Tarreau4aa573d2020-06-04 18:21:56 +020020#include <haproxy/check.h>
Willy Tarreau7ea393d2020-06-04 18:02:10 +020021#include <haproxy/connection.h>
Willy Tarreau36979d92020-06-05 17:27:29 +020022#include <haproxy/errors.h>
Willy Tarreauc7babd82020-06-04 21:29:29 +020023#include <haproxy/filters.h>
Willy Tarreaucd72d8c2020-06-02 19:11:26 +020024#include <haproxy/http.h>
Willy Tarreauc2b1ff02020-06-04 21:21:03 +020025#include <haproxy/http_ana.h>
Willy Tarreau87735332020-06-04 09:08:41 +020026#include <haproxy/http_htx.h>
Willy Tarreau16f958c2020-06-03 08:44:35 +020027#include <haproxy/htx.h>
Willy Tarreauaeed4a82020-06-04 22:01:04 +020028#include <haproxy/log.h>
Willy Tarreau6131d6a2020-06-02 16:48:09 +020029#include <haproxy/net_helper.h>
Willy Tarreaua264d962020-06-04 22:29:18 +020030#include <haproxy/proxy.h>
Willy Tarreau7cd8b6e2020-06-02 17:32:26 +020031#include <haproxy/regex.h>
Willy Tarreau1e56f922020-06-04 23:20:13 +020032#include <haproxy/server-t.h>
Willy Tarreau2eec9b52020-06-04 19:58:55 +020033#include <haproxy/stats.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020034#include <haproxy/stream.h>
Willy Tarreau5e539c92020-06-04 20:45:39 +020035#include <haproxy/stream_interface.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020036#include <haproxy/trace.h>
Willy Tarreau8c42b8a2020-06-04 19:27:34 +020037#include <haproxy/uri_auth-t.h>
Willy Tarreaua1718922020-06-04 16:25:31 +020038#include <haproxy/vars.h>
Christopher Faulete0768eb2018-10-03 16:38:02 +020039
Christopher Faulete0768eb2018-10-03 16:38:02 +020040
Christopher Fauleteea8fc72019-11-05 16:18:10 +010041#define TRACE_SOURCE &trace_strm
42
Christopher Faulet377c5a52018-10-24 21:21:30 +020043extern const char *stat_status_codes[];
Christopher Fauletf2824e62018-10-01 12:12:37 +020044
Christopher Fauleta8a46e22019-07-16 14:53:09 +020045struct pool_head *pool_head_requri = NULL;
46struct pool_head *pool_head_capture = NULL;
47
48
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020049static void http_end_request(struct stream *s);
50static void http_end_response(struct stream *s);
Christopher Fauletf2824e62018-10-01 12:12:37 +020051
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020052static void http_capture_headers(struct htx *htx, char **cap, struct cap_hdr *cap_hdr);
53static int http_del_hdr_value(char *start, char *end, char **from, char *next);
54static size_t http_fmt_req_line(const struct htx_sl *sl, char *str, size_t len);
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020055static void http_debug_stline(const char *dir, struct stream *s, const struct htx_sl *sl);
56static void http_debug_hdr(const char *dir, struct stream *s, const struct ist n, const struct ist v);
Christopher Faulet0f226952018-10-22 09:29:56 +020057
Christopher Fauletb58f62b2020-01-13 16:40:13 +010058static enum rule_result http_req_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s);
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020059static enum rule_result http_res_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s);
Christopher Faulet3e964192018-10-24 11:39:23 +020060
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020061static void http_manage_client_side_cookies(struct stream *s, struct channel *req);
62static void http_manage_server_side_cookies(struct stream *s, struct channel *res);
Christopher Fauletfcda7c62018-10-24 11:56:22 +020063
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020064static int http_stats_check_uri(struct stream *s, struct http_txn *txn, struct proxy *backend);
65static int http_handle_stats(struct stream *s, struct channel *req);
Christopher Faulet377c5a52018-10-24 21:21:30 +020066
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020067static int http_handle_expect_hdr(struct stream *s, struct htx *htx, struct http_msg *msg);
68static int http_reply_100_continue(struct stream *s);
Christopher Faulet23a3c792018-11-28 10:01:23 +010069
Christopher Faulete0768eb2018-10-03 16:38:02 +020070/* This stream analyser waits for a complete HTTP request. It returns 1 if the
71 * processing can continue on next analysers, or zero if it either needs more
72 * data or wants to immediately abort the request (eg: timeout, error, ...). It
73 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req.analysers
74 * when it has nothing left to do, and may remove any analyser when it wants to
75 * abort.
76 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020077int http_wait_for_request(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +020078{
Christopher Faulet9768c262018-10-22 09:34:31 +020079
Christopher Faulete0768eb2018-10-03 16:38:02 +020080 /*
Christopher Faulet9768c262018-10-22 09:34:31 +020081 * We will analyze a complete HTTP request to check the its syntax.
Christopher Faulete0768eb2018-10-03 16:38:02 +020082 *
Christopher Faulet9768c262018-10-22 09:34:31 +020083 * Once the start line and all headers are received, we may perform a
84 * capture of the error (if any), and we will set a few fields. We also
85 * check for monitor-uri, logging and finally headers capture.
Christopher Faulete0768eb2018-10-03 16:38:02 +020086 */
Christopher Faulete0768eb2018-10-03 16:38:02 +020087 struct session *sess = s->sess;
88 struct http_txn *txn = s->txn;
89 struct http_msg *msg = &txn->req;
Christopher Faulet9768c262018-10-22 09:34:31 +020090 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +010091 struct htx_sl *sl;
Christopher Faulete0768eb2018-10-03 16:38:02 +020092
Christopher Fauleteea8fc72019-11-05 16:18:10 +010093 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +020094
Christopher Faulet27ba2dc2018-12-05 11:53:24 +010095 htx = htxbuf(&req->buf);
Christopher Faulet9768c262018-10-22 09:34:31 +020096
Christopher Faulet8bebd2f2020-10-06 17:54:56 +020097 BUG_ON(htx_is_empty(htx) || htx->first == -1);
98
Willy Tarreau4236f032019-03-05 10:43:32 +010099 /* Parsing errors are caught here */
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200100 if (htx->flags & (HTX_FL_PARSING_ERROR|HTX_FL_PROCESSING_ERROR)) {
Willy Tarreau4236f032019-03-05 10:43:32 +0100101 stream_inc_http_req_ctr(s);
102 stream_inc_http_err_ctr(s);
Emeric Brun28976442020-10-07 08:50:09 +0200103 proxy_inc_fe_req_ctr(sess->listener, sess->fe);
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200104 if (htx->flags & HTX_FL_PARSING_ERROR)
105 goto return_bad_req;
106 else
107 goto return_int_err;
Willy Tarreau4236f032019-03-05 10:43:32 +0100108 }
109
Christopher Faulete0768eb2018-10-03 16:38:02 +0200110 /* we're speaking HTTP here, so let's speak HTTP to the client */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200111 s->srv_error = http_return_srv_error;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200112
Christopher Faulet9768c262018-10-22 09:34:31 +0200113 msg->msg_state = HTTP_MSG_BODY;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200114 stream_inc_http_req_ctr(s);
Emeric Brun28976442020-10-07 08:50:09 +0200115 proxy_inc_fe_req_ctr(sess->listener, sess->fe); /* one more valid request for this FE */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200116
Christopher Faulet9768c262018-10-22 09:34:31 +0200117 /* kill the pending keep-alive timeout */
Christopher Faulet9768c262018-10-22 09:34:31 +0200118 req->analyse_exp = TICK_ETERNITY;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200119
Christopher Faulet29f17582019-05-23 11:03:26 +0200120 BUG_ON(htx_get_first_type(htx) != HTX_BLK_REQ_SL);
Christopher Faulet297fbb42019-05-13 14:41:27 +0200121 sl = http_get_stline(htx);
Christopher Faulet03599112018-11-27 11:21:21 +0100122
Christopher Faulet9768c262018-10-22 09:34:31 +0200123 /* 0: we might have to print this header in debug mode */
124 if (unlikely((global.mode & MODE_DEBUG) &&
125 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
126 int32_t pos;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200127
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200128 http_debug_stline("clireq", s, sl);
Christopher Faulet9768c262018-10-22 09:34:31 +0200129
Christopher Fauleta3f15502019-05-13 15:27:23 +0200130 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet9768c262018-10-22 09:34:31 +0200131 struct htx_blk *blk = htx_get_blk(htx, pos);
132 enum htx_blk_type type = htx_get_blk_type(blk);
133
134 if (type == HTX_BLK_EOH)
135 break;
136 if (type != HTX_BLK_HDR)
137 continue;
138
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200139 http_debug_hdr("clihdr", s,
140 htx_get_blk_name(htx, blk),
141 htx_get_blk_value(htx, blk));
Christopher Faulet9768c262018-10-22 09:34:31 +0200142 }
143 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200144
145 /*
Christopher Faulet03599112018-11-27 11:21:21 +0100146 * 1: identify the method and the version. Also set HTTP flags
Christopher Faulete0768eb2018-10-03 16:38:02 +0200147 */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100148 txn->meth = sl->info.req.meth;
Christopher Faulet03599112018-11-27 11:21:21 +0100149 if (sl->flags & HTX_SL_F_VER_11)
Christopher Faulet9768c262018-10-22 09:34:31 +0200150 msg->flags |= HTTP_MSGF_VER_11;
Christopher Faulet03599112018-11-27 11:21:21 +0100151 msg->flags |= HTTP_MSGF_XFER_LEN;
Christopher Faulet834eee72019-02-18 11:35:02 +0100152 msg->flags |= ((sl->flags & HTX_SL_F_CLEN) ? HTTP_MSGF_CNT_LEN : HTTP_MSGF_TE_CHNK);
Christopher Fauletb2db4fa2018-11-27 16:51:09 +0100153 if (sl->flags & HTX_SL_F_BODYLESS)
154 msg->flags |= HTTP_MSGF_BODYLESS;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200155
156 /* we can make use of server redirect on GET and HEAD */
157 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
158 s->flags |= SF_REDIRECTABLE;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100159 else if (txn->meth == HTTP_METH_OTHER && isteqi(htx_sl_req_meth(sl), ist("PRI"))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200160 /* PRI is reserved for the HTTP/2 preface */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200161 goto return_bad_req;
162 }
163
164 /*
Christopher Faulet6072beb2020-02-18 15:34:58 +0100165 * 2: check if the URI matches the monitor_uri. We have to do this for
166 * every request which gets in, because the monitor-uri is defined by
167 * the frontend. If the monitor-uri starts with a '/', the matching is
168 * done against the request's path. Otherwise, the request's uri is
169 * used. It is a workaround to let HTTP/2 health-checks work as
170 * expected.
Christopher Faulete0768eb2018-10-03 16:38:02 +0200171 */
172 if (unlikely((sess->fe->monitor_uri_len != 0) &&
Christopher Faulet6072beb2020-02-18 15:34:58 +0100173 ((*sess->fe->monitor_uri == '/' && isteq(http_get_path(htx_sl_req_uri(sl)),
174 ist2(sess->fe->monitor_uri, sess->fe->monitor_uri_len))) ||
175 isteq(htx_sl_req_uri(sl), ist2(sess->fe->monitor_uri, sess->fe->monitor_uri_len))))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200176 /*
177 * We have found the monitor URI
178 */
179 struct acl_cond *cond;
180
181 s->flags |= SF_MONITOR;
Olivier Houcharda798bf52019-03-08 18:52:00 +0100182 _HA_ATOMIC_ADD(&sess->fe->fe_counters.intercepted_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200183
184 /* Check if we want to fail this monitor request or not */
185 list_for_each_entry(cond, &sess->fe->mon_fail_cond, list) {
186 int ret = acl_exec_cond(cond, sess->fe, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
187
188 ret = acl_pass(ret);
189 if (cond->pol == ACL_COND_UNLESS)
190 ret = !ret;
191
192 if (ret) {
193 /* we fail this request, let's return 503 service unavail */
194 txn->status = 503;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200195 if (!(s->flags & SF_ERR_MASK))
196 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
197 goto return_prx_cond;
198 }
199 }
200
Joseph Herlantc42c0e92018-11-25 10:43:27 -0800201 /* nothing to fail, let's reply normally */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200202 txn->status = 200;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200203 if (!(s->flags & SF_ERR_MASK))
204 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
205 goto return_prx_cond;
206 }
207
208 /*
209 * 3: Maybe we have to copy the original REQURI for the logs ?
210 * Note: we cannot log anymore if the request has been
211 * classified as invalid.
212 */
213 if (unlikely(s->logs.logwait & LW_REQ)) {
214 /* we have a complete HTTP request that we must log */
215 if ((txn->uri = pool_alloc(pool_head_requri)) != NULL) {
Christopher Faulet9768c262018-10-22 09:34:31 +0200216 size_t len;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200217
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200218 len = http_fmt_req_line(sl, txn->uri, global.tune.requri_len - 1);
Christopher Faulet9768c262018-10-22 09:34:31 +0200219 txn->uri[len] = 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200220
221 if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT)))
222 s->do_log(s);
223 } else {
224 ha_alert("HTTP logging : out of memory.\n");
225 }
226 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200227
Christopher Faulete0768eb2018-10-03 16:38:02 +0200228 /* if the frontend has "option http-use-proxy-header", we'll check if
229 * we have what looks like a proxied connection instead of a connection,
230 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
231 * Note that this is *not* RFC-compliant, however browsers and proxies
232 * happen to do that despite being non-standard :-(
233 * We consider that a request not beginning with either '/' or '*' is
234 * a proxied connection, which covers both "scheme://location" and
235 * CONNECT ip:port.
236 */
237 if ((sess->fe->options2 & PR_O2_USE_PXHDR) &&
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100238 *HTX_SL_REQ_UPTR(sl) != '/' && *HTX_SL_REQ_UPTR(sl) != '*')
Christopher Faulete0768eb2018-10-03 16:38:02 +0200239 txn->flags |= TX_USE_PX_CONN;
240
Christopher Faulete0768eb2018-10-03 16:38:02 +0200241 /* 5: we may need to capture headers */
242 if (unlikely((s->logs.logwait & LW_REQHDR) && s->req_cap))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200243 http_capture_headers(htx, s->req_cap, sess->fe->req_cap);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200244
Christopher Faulete0768eb2018-10-03 16:38:02 +0200245 /* we may have to wait for the request's body */
Christopher Faulet9768c262018-10-22 09:34:31 +0200246 if (s->be->options & PR_O_WREQ_BODY)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200247 req->analysers |= AN_REQ_HTTP_BODY;
248
249 /*
250 * RFC7234#4:
251 * A cache MUST write through requests with methods
252 * that are unsafe (Section 4.2.1 of [RFC7231]) to
253 * the origin server; i.e., a cache is not allowed
254 * to generate a reply to such a request before
255 * having forwarded the request and having received
256 * a corresponding response.
257 *
258 * RFC7231#4.2.1:
259 * Of the request methods defined by this
260 * specification, the GET, HEAD, OPTIONS, and TRACE
261 * methods are defined to be safe.
262 */
263 if (likely(txn->meth == HTTP_METH_GET ||
264 txn->meth == HTTP_METH_HEAD ||
265 txn->meth == HTTP_METH_OPTIONS ||
266 txn->meth == HTTP_METH_TRACE))
267 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
268
269 /* end of job, return OK */
270 req->analysers &= ~an_bit;
271 req->analyse_exp = TICK_ETERNITY;
Christopher Faulet9768c262018-10-22 09:34:31 +0200272
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100273 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200274 return 1;
275
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200276 return_int_err:
277 txn->status = 500;
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200278 if (!(s->flags & SF_ERR_MASK))
279 s->flags |= SF_ERR_INTERNAL;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100280 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200281 if (sess->listener->counters)
Christopher Fauletb8a53712019-12-16 11:29:38 +0100282 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200283 goto return_prx_cond;
284
Christopher Faulete0768eb2018-10-03 16:38:02 +0200285 return_bad_req:
Christopher Faulet9768c262018-10-22 09:34:31 +0200286 txn->status = 400;
Olivier Houcharda798bf52019-03-08 18:52:00 +0100287 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200288 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100289 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200290 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200291
292 return_prx_cond:
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200293 http_reply_and_close(s, txn->status, http_error_message(s));
294
Christopher Faulete0768eb2018-10-03 16:38:02 +0200295 if (!(s->flags & SF_ERR_MASK))
296 s->flags |= SF_ERR_PRXCOND;
297 if (!(s->flags & SF_FINST_MASK))
298 s->flags |= SF_FINST_R;
299
300 req->analysers &= AN_REQ_FLT_END;
301 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100302 DBG_TRACE_DEVEL("leaving on error",
303 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200304 return 0;
305}
306
307
308/* This stream analyser runs all HTTP request processing which is common to
309 * frontends and backends, which means blocking ACLs, filters, connection-close,
310 * reqadd, stats and redirects. This is performed for the designated proxy.
311 * It returns 1 if the processing can continue on next analysers, or zero if it
312 * either needs more data or wants to immediately abort the request (eg: deny,
313 * error, ...).
314 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200315int http_process_req_common(struct stream *s, struct channel *req, int an_bit, struct proxy *px)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200316{
317 struct session *sess = s->sess;
318 struct http_txn *txn = s->txn;
319 struct http_msg *msg = &txn->req;
Christopher Fauletff2759f2018-10-24 11:13:16 +0200320 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200321 struct redirect_rule *rule;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200322 enum rule_result verdict;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200323 struct connection *conn = objt_conn(sess->origin);
324
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100325 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200326
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100327 htx = htxbuf(&req->buf);
Christopher Fauletff2759f2018-10-24 11:13:16 +0200328
Christopher Faulet1907ccc2019-04-29 13:12:02 +0200329 /* just in case we have some per-backend tracking. Only called the first
330 * execution of the analyser. */
331 if (!s->current_rule || s->current_rule_list != &px->http_req_rules)
332 stream_inc_be_http_req_ctr(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200333
334 /* evaluate http-request rules */
335 if (!LIST_ISEMPTY(&px->http_req_rules)) {
Christopher Fauletb58f62b2020-01-13 16:40:13 +0100336 verdict = http_req_get_intercept_rule(px, &px->http_req_rules, s);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200337
338 switch (verdict) {
339 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
340 goto return_prx_yield;
341
342 case HTTP_RULE_RES_CONT:
343 case HTTP_RULE_RES_STOP: /* nothing to do */
344 break;
345
346 case HTTP_RULE_RES_DENY: /* deny or tarpit */
347 if (txn->flags & TX_CLTARPIT)
348 goto tarpit;
349 goto deny;
350
351 case HTTP_RULE_RES_ABRT: /* abort request, response already sent. Eg: auth */
352 goto return_prx_cond;
353
354 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
355 goto done;
356
357 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
358 goto return_bad_req;
Christopher Faulet3a26bee2019-12-16 12:47:40 +0100359
360 case HTTP_RULE_RES_ERROR: /* failed with a bad request */
361 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200362 }
363 }
364
365 if (conn && (conn->flags & CO_FL_EARLY_DATA) &&
Olivier Houchard220a26c2020-01-23 14:57:36 +0100366 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_SSL_WAIT_HS))) {
Christopher Fauletff2759f2018-10-24 11:13:16 +0200367 struct http_hdr_ctx ctx;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200368
Christopher Fauletff2759f2018-10-24 11:13:16 +0200369 ctx.blk = NULL;
370 if (!http_find_header(htx, ist("Early-Data"), &ctx, 0)) {
371 if (unlikely(!http_add_header(htx, ist("Early-Data"), ist("1"))))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100372 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200373 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200374 }
375
376 /* OK at this stage, we know that the request was accepted according to
377 * the http-request rules, we can check for the stats. Note that the
378 * URI is detected *before* the req* rules in order not to be affected
379 * by a possible reqrep, while they are processed *after* so that a
380 * reqdeny can still block them. This clearly needs to change in 1.6!
381 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200382 if (!s->target && http_stats_check_uri(s, txn, px)) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200383 s->target = &http_stats_applet.obj_type;
Willy Tarreau14bfe9a2018-12-19 15:19:27 +0100384 if (unlikely(!si_register_handler(&s->si[1], objt_applet(s->target)))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200385 s->logs.tv_request = now;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200386 if (!(s->flags & SF_ERR_MASK))
387 s->flags |= SF_ERR_RESOURCE;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100388 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200389 }
390
391 /* parse the whole stats request and extract the relevant information */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200392 http_handle_stats(s, req);
Christopher Fauletb58f62b2020-01-13 16:40:13 +0100393 verdict = http_req_get_intercept_rule(px, &px->uri_auth->http_req_rules, s);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200394 /* not all actions implemented: deny, allow, auth */
395
396 if (verdict == HTTP_RULE_RES_DENY) /* stats http-request deny */
397 goto deny;
398
399 if (verdict == HTTP_RULE_RES_ABRT) /* stats auth / stats http-request auth */
400 goto return_prx_cond;
Christopher Faulet3a26bee2019-12-16 12:47:40 +0100401
402 if (verdict == HTTP_RULE_RES_BADREQ) /* failed with a bad request */
403 goto return_bad_req;
404
405 if (verdict == HTTP_RULE_RES_ERROR) /* failed with a bad request */
406 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200407 }
408
Christopher Faulet2571bc62019-03-01 11:44:26 +0100409 /* Proceed with the applets now. */
410 if (unlikely(objt_applet(s->target))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200411 if (sess->fe == s->be) /* report it if the request was intercepted by the frontend */
Olivier Houcharda798bf52019-03-08 18:52:00 +0100412 _HA_ATOMIC_ADD(&sess->fe->fe_counters.intercepted_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200413
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200414 if (http_handle_expect_hdr(s, htx, msg) == -1)
Christopher Fauletb8a53712019-12-16 11:29:38 +0100415 goto return_int_err;
Christopher Fauletbcf242a2019-03-01 11:36:26 +0100416
Christopher Faulete0768eb2018-10-03 16:38:02 +0200417 if (!(s->flags & SF_ERR_MASK)) // this is not really an error but it is
418 s->flags |= SF_ERR_LOCAL; // to mark that it comes from the proxy
419 if (!(s->flags & SF_FINST_MASK))
420 s->flags |= SF_FINST_R;
421
422 /* enable the minimally required analyzers to handle keep-alive and compression on the HTTP response */
423 req->analysers &= (AN_REQ_HTTP_BODY | AN_REQ_FLT_HTTP_HDRS | AN_REQ_FLT_END);
424 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
425 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Christopher Fauletbcf242a2019-03-01 11:36:26 +0100426
427 req->flags |= CF_SEND_DONTWAIT;
428 s->flags |= SF_ASSIGNED;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200429 goto done;
430 }
431
432 /* check whether we have some ACLs set to redirect this request */
433 list_for_each_entry(rule, &px->redirect_rules, list) {
434 if (rule->cond) {
435 int ret;
436
437 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
438 ret = acl_pass(ret);
439 if (rule->cond->pol == ACL_COND_UNLESS)
440 ret = !ret;
441 if (!ret)
442 continue;
443 }
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200444 if (!http_apply_redirect_rule(rule, s, txn))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100445 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200446 goto done;
447 }
448
449 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
450 * If this happens, then the data will not come immediately, so we must
451 * send all what we have without waiting. Note that due to the small gain
452 * in waiting for the body of the request, it's easier to simply put the
453 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
454 * itself once used.
455 */
456 req->flags |= CF_SEND_DONTWAIT;
457
458 done: /* done with this analyser, continue with next ones that the calling
459 * points will have set, if any.
460 */
461 req->analyse_exp = TICK_ETERNITY;
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +0500462 done_without_exp: /* done with this analyser, but don't reset the analyse_exp. */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200463 req->analysers &= ~an_bit;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100464 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200465 return 1;
466
467 tarpit:
468 /* Allow cookie logging
469 */
470 if (s->be->cookie_name || sess->fe->capture_name)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200471 http_manage_client_side_cookies(s, req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200472
473 /* When a connection is tarpitted, we use the tarpit timeout,
474 * which may be the same as the connect timeout if unspecified.
475 * If unset, then set it to zero because we really want it to
476 * eventually expire. We build the tarpit as an analyser.
477 */
Christopher Faulet202c6ce2019-01-07 14:57:35 +0100478 channel_htx_erase(&s->req, htx);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200479
480 /* wipe the request out so that we can drop the connection early
481 * if the client closes first.
482 */
483 channel_dont_connect(req);
484
Christopher Faulete0768eb2018-10-03 16:38:02 +0200485 req->analysers &= AN_REQ_FLT_END; /* remove switching rules etc... */
486 req->analysers |= AN_REQ_HTTP_TARPIT;
487 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
488 if (!req->analyse_exp)
489 req->analyse_exp = tick_add(now_ms, 0);
490 stream_inc_http_err_ctr(s);
Olivier Houcharda798bf52019-03-08 18:52:00 +0100491 _HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100492 if (s->flags & SF_BE_ASSIGNED)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100493 _HA_ATOMIC_ADD(&s->be->be_counters.denied_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200494 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100495 _HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200496 goto done_without_exp;
497
498 deny: /* this request was blocked (denied) */
499
500 /* Allow cookie logging
501 */
502 if (s->be->cookie_name || sess->fe->capture_name)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200503 http_manage_client_side_cookies(s, req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200504
Christopher Faulete0768eb2018-10-03 16:38:02 +0200505 s->logs.tv_request = now;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200506 stream_inc_http_err_ctr(s);
Olivier Houcharda798bf52019-03-08 18:52:00 +0100507 _HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100508 if (s->flags & SF_BE_ASSIGNED)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100509 _HA_ATOMIC_ADD(&s->be->be_counters.denied_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200510 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100511 _HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100512 goto return_prx_err;
513
514 return_int_err:
515 txn->status = 500;
516 if (!(s->flags & SF_ERR_MASK))
517 s->flags |= SF_ERR_INTERNAL;
518 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100519 if (s->flags & SF_BE_ASSIGNED)
520 _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100521 if (sess->listener->counters)
522 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
523 goto return_prx_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200524
525 return_bad_req:
Christopher Faulete0768eb2018-10-03 16:38:02 +0200526 txn->status = 400;
Olivier Houcharda798bf52019-03-08 18:52:00 +0100527 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200528 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100529 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100530 /* fall through */
531
532 return_prx_err:
533 http_reply_and_close(s, txn->status, http_error_message(s));
534 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200535
536 return_prx_cond:
537 if (!(s->flags & SF_ERR_MASK))
538 s->flags |= SF_ERR_PRXCOND;
539 if (!(s->flags & SF_FINST_MASK))
540 s->flags |= SF_FINST_R;
541
542 req->analysers &= AN_REQ_FLT_END;
543 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100544 DBG_TRACE_DEVEL("leaving on error",
545 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200546 return 0;
547
548 return_prx_yield:
549 channel_dont_connect(req);
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100550 DBG_TRACE_DEVEL("waiting for more data",
551 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200552 return 0;
553}
554
555/* This function performs all the processing enabled for the current request.
556 * It returns 1 if the processing can continue on next analysers, or zero if it
557 * needs more data, encounters an error, or wants to immediately abort the
558 * request. It relies on buffers flags, and updates s->req.analysers.
559 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200560int http_process_request(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200561{
562 struct session *sess = s->sess;
563 struct http_txn *txn = s->txn;
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200564 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200565 struct connection *cli_conn = objt_conn(strm_sess(s)->origin);
566
Christopher Faulet8bebd2f2020-10-06 17:54:56 +0200567 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200568
569 /*
570 * Right now, we know that we have processed the entire headers
571 * and that unwanted requests have been filtered out. We can do
572 * whatever we want with the remaining request. Also, now we
573 * may have separate values for ->fe, ->be.
574 */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100575 htx = htxbuf(&req->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200576
577 /*
578 * If HTTP PROXY is set we simply get remote server address parsing
Willy Tarreau1c8d32b2019-07-18 15:47:45 +0200579 * incoming request.
Christopher Faulete0768eb2018-10-03 16:38:02 +0200580 */
581 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SF_ADDR_SET)) {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100582 struct htx_sl *sl;
583 struct ist uri, path;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200584
Willy Tarreau9b7587a2020-10-15 07:32:10 +0200585 if (!sockaddr_alloc(&s->target_addr, NULL, 0)) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200586 if (!(s->flags & SF_ERR_MASK))
587 s->flags |= SF_ERR_RESOURCE;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100588 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200589 }
Christopher Faulet297fbb42019-05-13 14:41:27 +0200590 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100591 uri = htx_sl_req_uri(sl);
592 path = http_get_path(uri);
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200593
Willy Tarreau1c8d32b2019-07-18 15:47:45 +0200594 if (url2sa(uri.ptr, uri.len - path.len, s->target_addr, NULL) == -1)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200595 goto return_bad_req;
596
Willy Tarreau1c8d32b2019-07-18 15:47:45 +0200597 s->target = &s->be->obj_type;
598 s->flags |= SF_ADDR_SET | SF_ASSIGNED;
599
Christopher Faulete0768eb2018-10-03 16:38:02 +0200600 /* if the path was found, we have to remove everything between
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200601 * uri.ptr and path.ptr (excluded). If it was not found, we need
602 * to replace from all the uri by a single "/".
603 *
Ilya Shipitsin46a030c2020-07-05 16:36:08 +0500604 * Instead of rewriting the whole start line, we just update
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100605 * the star-line URI. Some space will be lost but it should be
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200606 * insignificant.
Christopher Faulete0768eb2018-10-03 16:38:02 +0200607 */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100608 istcpy(&uri, (path.len ? path : ist("/")), uri.len);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200609 }
610
611 /*
612 * 7: Now we can work with the cookies.
613 * Note that doing so might move headers in the request, but
614 * the fields will stay coherent and the URI will not move.
615 * This should only be performed in the backend.
616 */
617 if (s->be->cookie_name || sess->fe->capture_name)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200618 http_manage_client_side_cookies(s, req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200619
Tim Duesterhus2825b4b2020-02-28 15:13:34 +0100620 /* 8: Generate unique ID if a "unique-id-format" is defined.
621 *
622 * A unique ID is generated even when it is not sent to ensure that the ID can make use of
623 * fetches only available in the HTTP request processing stage.
624 */
625 if (!LIST_ISEMPTY(&sess->fe->format_unique_id)) {
Tim Duesterhusa17e6622020-03-05 20:19:02 +0100626 struct ist unique_id = stream_generate_unique_id(s, &sess->fe->format_unique_id);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200627
Tim Duesterhusa17e6622020-03-05 20:19:02 +0100628 if (!isttest(unique_id)) {
Christopher Fauletb8a53712019-12-16 11:29:38 +0100629 if (!(s->flags & SF_ERR_MASK))
630 s->flags |= SF_ERR_RESOURCE;
631 goto return_int_err;
632 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200633
Tim Duesterhus2825b4b2020-02-28 15:13:34 +0100634 /* send unique ID if a "unique-id-header" is defined */
Tim Duesterhus0643b0e2020-03-05 17:56:35 +0100635 if (isttest(sess->fe->header_unique_id) &&
Tim Duesterhusa17e6622020-03-05 20:19:02 +0100636 unlikely(!http_add_header(htx, sess->fe->header_unique_id, s->unique_id)))
Tim Duesterhus2825b4b2020-02-28 15:13:34 +0100637 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200638 }
639
640 /*
641 * 9: add X-Forwarded-For if either the frontend or the backend
642 * asks for it.
643 */
644 if ((sess->fe->options | s->be->options) & PR_O_FWDFOR) {
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200645 struct http_hdr_ctx ctx = { .blk = NULL };
646 struct ist hdr = ist2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : sess->fe->fwdfor_hdr_name,
647 s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : sess->fe->fwdfor_hdr_len);
648
Christopher Faulete0768eb2018-10-03 16:38:02 +0200649 if (!((sess->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200650 http_find_header(htx, hdr, &ctx, 0)) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200651 /* The header is set to be added only if none is present
652 * and we found it, so don't do anything.
653 */
654 }
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200655 else if (cli_conn && conn_get_src(cli_conn) && cli_conn->src->ss_family == AF_INET) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200656 /* Add an X-Forwarded-For header unless the source IP is
657 * in the 'except' network range.
658 */
659 if ((!sess->fe->except_mask.s_addr ||
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200660 (((struct sockaddr_in *)cli_conn->src)->sin_addr.s_addr & sess->fe->except_mask.s_addr)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200661 != sess->fe->except_net.s_addr) &&
662 (!s->be->except_mask.s_addr ||
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200663 (((struct sockaddr_in *)cli_conn->src)->sin_addr.s_addr & s->be->except_mask.s_addr)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200664 != s->be->except_net.s_addr)) {
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200665 unsigned char *pn = (unsigned char *)&((struct sockaddr_in *)cli_conn->src)->sin_addr;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200666
667 /* Note: we rely on the backend to get the header name to be used for
668 * x-forwarded-for, because the header is really meant for the backends.
669 * However, if the backend did not specify any option, we have to rely
670 * on the frontend's header name.
671 */
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200672 chunk_printf(&trash, "%d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
673 if (unlikely(!http_add_header(htx, hdr, ist2(trash.area, trash.data))))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100674 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200675 }
676 }
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200677 else if (cli_conn && conn_get_src(cli_conn) && cli_conn->src->ss_family == AF_INET6) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200678 /* FIXME: for the sake of completeness, we should also support
679 * 'except' here, although it is mostly useless in this case.
680 */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200681 char pn[INET6_ADDRSTRLEN];
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200682
Christopher Faulete0768eb2018-10-03 16:38:02 +0200683 inet_ntop(AF_INET6,
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200684 (const void *)&((struct sockaddr_in6 *)(cli_conn->src))->sin6_addr,
Christopher Faulete0768eb2018-10-03 16:38:02 +0200685 pn, sizeof(pn));
686
687 /* Note: we rely on the backend to get the header name to be used for
688 * x-forwarded-for, because the header is really meant for the backends.
689 * However, if the backend did not specify any option, we have to rely
690 * on the frontend's header name.
691 */
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200692 chunk_printf(&trash, "%s", pn);
693 if (unlikely(!http_add_header(htx, hdr, ist2(trash.area, trash.data))))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100694 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200695 }
696 }
697
698 /*
699 * 10: add X-Original-To if either the frontend or the backend
700 * asks for it.
701 */
702 if ((sess->fe->options | s->be->options) & PR_O_ORGTO) {
703
704 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200705 if (cli_conn && conn_get_src(cli_conn) && cli_conn->src->ss_family == AF_INET && conn_get_dst(cli_conn)) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200706 /* Add an X-Original-To header unless the destination IP is
707 * in the 'except' network range.
708 */
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200709 if (cli_conn->dst->ss_family == AF_INET &&
Christopher Faulete0768eb2018-10-03 16:38:02 +0200710 ((!sess->fe->except_mask_to.s_addr ||
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200711 (((struct sockaddr_in *)cli_conn->dst)->sin_addr.s_addr & sess->fe->except_mask_to.s_addr)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200712 != sess->fe->except_to.s_addr) &&
713 (!s->be->except_mask_to.s_addr ||
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200714 (((struct sockaddr_in *)cli_conn->dst)->sin_addr.s_addr & s->be->except_mask_to.s_addr)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200715 != s->be->except_to.s_addr))) {
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200716 struct ist hdr;
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200717 unsigned char *pn = (unsigned char *)&((struct sockaddr_in *)cli_conn->dst)->sin_addr;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200718
719 /* Note: we rely on the backend to get the header name to be used for
720 * x-original-to, because the header is really meant for the backends.
721 * However, if the backend did not specify any option, we have to rely
722 * on the frontend's header name.
723 */
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200724 if (s->be->orgto_hdr_len)
725 hdr = ist2(s->be->orgto_hdr_name, s->be->orgto_hdr_len);
726 else
727 hdr = ist2(sess->fe->orgto_hdr_name, sess->fe->orgto_hdr_len);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200728
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200729 chunk_printf(&trash, "%d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
730 if (unlikely(!http_add_header(htx, hdr, ist2(trash.area, trash.data))))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100731 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200732 }
733 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200734 }
735
Christopher Faulete0768eb2018-10-03 16:38:02 +0200736 /* If we have no server assigned yet and we're balancing on url_param
737 * with a POST request, we may be interested in checking the body for
738 * that parameter. This will be done in another analyser.
739 */
740 if (!(s->flags & (SF_ASSIGNED|SF_DIRECT)) &&
Willy Tarreau089eaa02019-01-14 15:17:46 +0100741 s->txn->meth == HTTP_METH_POST &&
742 (s->be->lbprm.algo & BE_LB_ALGO) == BE_LB_ALGO_PH) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200743 channel_dont_connect(req);
744 req->analysers |= AN_REQ_HTTP_BODY;
745 }
746
747 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
748 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau1a18b542018-12-11 16:37:42 +0100749
Christopher Faulete0768eb2018-10-03 16:38:02 +0200750 /* We expect some data from the client. Unless we know for sure
751 * we already have a full request, we have to re-enable quick-ack
752 * in case we previously disabled it, otherwise we might cause
753 * the client to delay further data.
754 */
755 if ((sess->listener->options & LI_O_NOQUICKACK) &&
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200756 (htx_get_tail_type(htx) != HTX_BLK_EOM))
Willy Tarreau1a18b542018-12-11 16:37:42 +0100757 conn_set_quickack(cli_conn, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200758
759 /*************************************************************
760 * OK, that's finished for the headers. We have done what we *
761 * could. Let's switch to the DATA state. *
762 ************************************************************/
763 req->analyse_exp = TICK_ETERNITY;
764 req->analysers &= ~an_bit;
765
766 s->logs.tv_request = now;
767 /* OK let's go on with the BODY now */
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100768 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200769 return 1;
770
Christopher Fauletb8a53712019-12-16 11:29:38 +0100771 return_int_err:
772 txn->status = 500;
773 if (!(s->flags & SF_ERR_MASK))
774 s->flags |= SF_ERR_INTERNAL;
775 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100776 if (s->flags & SF_BE_ASSIGNED)
Christopher Fauletbe20cf32020-01-24 11:41:38 +0100777 _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100778 if (sess->listener->counters)
779 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
780 goto return_prx_cond;
781
Christopher Faulete0768eb2018-10-03 16:38:02 +0200782 return_bad_req: /* let's centralize all bad requests */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200783 txn->status = 400;
Olivier Houcharda798bf52019-03-08 18:52:00 +0100784 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200785 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100786 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100787 /* fall through */
788
789 return_prx_cond:
790 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +0200791
792 if (!(s->flags & SF_ERR_MASK))
793 s->flags |= SF_ERR_PRXCOND;
794 if (!(s->flags & SF_FINST_MASK))
795 s->flags |= SF_FINST_R;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100796
797 req->analysers &= AN_REQ_FLT_END;
798 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100799 DBG_TRACE_DEVEL("leaving on error",
800 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200801 return 0;
802}
803
804/* This function is an analyser which processes the HTTP tarpit. It always
805 * returns zero, at the beginning because it prevents any other processing
806 * from occurring, and at the end because it terminates the request.
807 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200808int http_process_tarpit(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200809{
810 struct http_txn *txn = s->txn;
811
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100812 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, &txn->req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200813 /* This connection is being tarpitted. The CLIENT side has
814 * already set the connect expiration date to the right
815 * timeout. We just have to check that the client is still
816 * there and that the timeout has not expired.
817 */
818 channel_dont_connect(req);
819 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100820 !tick_is_expired(req->analyse_exp, now_ms)) {
821 DBG_TRACE_DEVEL("waiting for tarpit timeout expiry",
822 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200823 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100824 }
825
Christopher Faulete0768eb2018-10-03 16:38:02 +0200826
827 /* We will set the queue timer to the time spent, just for
828 * logging purposes. We fake a 500 server error, so that the
829 * attacker will not suspect his connection has been tarpitted.
830 * It will not cause trouble to the logs because we can exclude
831 * the tarpitted connections by filtering on the 'PT' status flags.
832 */
833 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
834
Christopher Faulet8dfeccf2020-05-15 14:16:29 +0200835 http_reply_and_close(s, txn->status, (!(req->flags & CF_READ_ERROR) ? http_error_message(s) : NULL));
Christopher Faulet5cb513a2020-05-13 17:56:56 +0200836
837 end:
Christopher Faulete0768eb2018-10-03 16:38:02 +0200838 req->analysers &= AN_REQ_FLT_END;
839 req->analyse_exp = TICK_ETERNITY;
840
841 if (!(s->flags & SF_ERR_MASK))
842 s->flags |= SF_ERR_PRXCOND;
843 if (!(s->flags & SF_FINST_MASK))
844 s->flags |= SF_FINST_T;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100845
846 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200847 return 0;
848}
849
850/* This function is an analyser which waits for the HTTP request body. It waits
851 * for either the buffer to be full, or the full advertised contents to have
852 * reached the buffer. It must only be called after the standard HTTP request
853 * processing has occurred, because it expects the request to be parsed and will
854 * look for the Expect header. It may send a 100-Continue interim response. It
855 * takes in input any state starting from HTTP_MSG_BODY and leaves with one of
856 * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it
857 * needs to read more data, or 1 once it has completed its analysis.
858 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200859int http_wait_for_request_body(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200860{
861 struct session *sess = s->sess;
862 struct http_txn *txn = s->txn;
863 struct http_msg *msg = &s->txn->req;
Christopher Fauletf76ebe82018-10-24 11:16:22 +0200864 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200865
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100866 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Fauletf76ebe82018-10-24 11:16:22 +0200867
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100868 htx = htxbuf(&req->buf);
Christopher Fauletf76ebe82018-10-24 11:16:22 +0200869
Willy Tarreau4236f032019-03-05 10:43:32 +0100870 if (htx->flags & HTX_FL_PARSING_ERROR)
871 goto return_bad_req;
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200872 if (htx->flags & HTX_FL_PROCESSING_ERROR)
873 goto return_int_err;
Willy Tarreau4236f032019-03-05 10:43:32 +0100874
Christopher Faulet63c69a92020-11-16 16:03:35 +0100875 /* CONNECT requests have no body */
876 if (txn->meth == HTTP_METH_CONNECT)
877 goto http_end;
878
Christopher Faulete0768eb2018-10-03 16:38:02 +0200879 /* We have to parse the HTTP request body to find any required data.
880 * "balance url_param check_post" should have been the only way to get
881 * into this. We were brought here after HTTP header analysis, so all
882 * related structures are ready.
883 */
884
Christopher Fauletf76ebe82018-10-24 11:16:22 +0200885 if (msg->msg_state < HTTP_MSG_DATA) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200886 if (http_handle_expect_hdr(s, htx, msg) == -1)
Christopher Fauletb8a53712019-12-16 11:29:38 +0100887 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200888 }
889
Christopher Fauletf76ebe82018-10-24 11:16:22 +0200890 msg->msg_state = HTTP_MSG_DATA;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200891
Christopher Fauletf76ebe82018-10-24 11:16:22 +0200892 /* Now we're in HTTP_MSG_DATA. We just need to know if all data have
893 * been received or if the buffer is full.
Christopher Faulete0768eb2018-10-03 16:38:02 +0200894 */
Christopher Faulet54b5e212019-06-04 10:08:28 +0200895 if (htx_get_tail_type(htx) > HTX_BLK_DATA ||
Christopher Fauletdcd8c5e2019-01-21 11:24:38 +0100896 channel_htx_full(req, htx, global.tune.maxrewrite))
Christopher Faulete0768eb2018-10-03 16:38:02 +0200897 goto http_end;
898
899 if ((req->flags & CF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
900 txn->status = 408;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200901 if (!(s->flags & SF_ERR_MASK))
902 s->flags |= SF_ERR_CLITO;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100903 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
904 if (sess->listener->counters)
905 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
906 goto return_prx_cond;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200907 }
908
909 /* we get here if we need to wait for more data */
910 if (!(req->flags & (CF_SHUTR | CF_READ_ERROR))) {
911 /* Not enough data. We'll re-use the http-request
912 * timeout here. Ideally, we should set the timeout
913 * relative to the accept() date. We just set the
914 * request timeout once at the beginning of the
915 * request.
916 */
917 channel_dont_connect(req);
918 if (!tick_isset(req->analyse_exp))
919 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100920 DBG_TRACE_DEVEL("waiting for more data",
921 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200922 return 0;
923 }
924
925 http_end:
926 /* The situation will not evolve, so let's give up on the analysis. */
927 s->logs.tv_request = now; /* update the request timer to reflect full request */
928 req->analysers &= ~an_bit;
929 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100930 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200931 return 1;
932
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200933 return_int_err:
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200934 txn->status = 500;
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200935 if (!(s->flags & SF_ERR_MASK))
936 s->flags |= SF_ERR_INTERNAL;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100937 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100938 if (s->flags & SF_BE_ASSIGNED)
Christopher Fauletbe20cf32020-01-24 11:41:38 +0100939 _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100940 if (sess->listener->counters)
941 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
942 goto return_prx_cond;
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200943
Christopher Faulete0768eb2018-10-03 16:38:02 +0200944 return_bad_req: /* let's centralize all bad requests */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200945 txn->status = 400;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100946 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
947 if (sess->listener->counters)
948 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
949 /* fall through */
950
951 return_prx_cond:
952 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +0200953
954 if (!(s->flags & SF_ERR_MASK))
955 s->flags |= SF_ERR_PRXCOND;
956 if (!(s->flags & SF_FINST_MASK))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100957 s->flags |= (msg->msg_state < HTTP_MSG_DATA ? SF_FINST_R : SF_FINST_D);
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200958
Christopher Faulete0768eb2018-10-03 16:38:02 +0200959 req->analysers &= AN_REQ_FLT_END;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100960 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100961 DBG_TRACE_DEVEL("leaving on error",
962 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200963 return 0;
964}
965
966/* This function is an analyser which forwards request body (including chunk
967 * sizes if any). It is called as soon as we must forward, even if we forward
968 * zero byte. The only situation where it must not be called is when we're in
969 * tunnel mode and we want to forward till the close. It's used both to forward
970 * remaining data and to resync after end of body. It expects the msg_state to
971 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
972 * read more data, or 1 once we can go on with next request or end the stream.
973 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
974 * bytes of pending data + the headers if not already done.
975 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200976int http_request_forward_body(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200977{
978 struct session *sess = s->sess;
979 struct http_txn *txn = s->txn;
Christopher Faulet9768c262018-10-22 09:34:31 +0200980 struct http_msg *msg = &txn->req;
981 struct htx *htx;
Christopher Faulet93e02d82019-03-08 14:18:50 +0100982 short status = 0;
Christopher Fauletaed82cf2018-11-30 22:22:32 +0100983 int ret;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200984
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100985 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200986
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100987 htx = htxbuf(&req->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200988
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200989 if (htx->flags & HTX_FL_PARSING_ERROR)
990 goto return_bad_req;
991 if (htx->flags & HTX_FL_PROCESSING_ERROR)
992 goto return_int_err;
993
Christopher Faulete0768eb2018-10-03 16:38:02 +0200994 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
995 ((req->flags & CF_SHUTW) && (req->to_forward || co_data(req)))) {
996 /* Output closed while we were sending data. We must abort and
997 * wake the other side up.
998 */
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200999
Olivier Houchard29cac3c2019-07-12 15:48:58 +02001000 /* Don't abort yet if we had L7 retries activated and it
1001 * was a write error, we may recover.
1002 */
1003 if (!(req->flags & (CF_READ_ERROR | CF_READ_TIMEOUT)) &&
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001004 (s->si[1].flags & SI_FL_L7_RETRY)) {
1005 DBG_TRACE_DEVEL("leaving on L7 retry",
1006 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Olivier Houchard29cac3c2019-07-12 15:48:58 +02001007 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001008 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001009 msg->msg_state = HTTP_MSG_ERROR;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001010 http_end_request(s);
1011 http_end_response(s);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001012 DBG_TRACE_DEVEL("leaving on error",
1013 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001014 return 1;
1015 }
1016
1017 /* Note that we don't have to send 100-continue back because we don't
1018 * need the data to complete our job, and it's up to the server to
1019 * decide whether to return 100, 417 or anything else in return of
1020 * an "Expect: 100-continue" header.
1021 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001022 if (msg->msg_state == HTTP_MSG_BODY)
1023 msg->msg_state = HTTP_MSG_DATA;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001024
Christopher Faulete0768eb2018-10-03 16:38:02 +02001025 /* in most states, we should abort in case of early close */
1026 channel_auto_close(req);
1027
1028 if (req->to_forward) {
Christopher Faulet66af0b22019-03-22 14:54:52 +01001029 if (req->to_forward == CHN_INFINITE_FORWARD) {
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001030 if (req->flags & CF_EOI)
1031 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet66af0b22019-03-22 14:54:52 +01001032 }
1033 else {
1034 /* We can't process the buffer's contents yet */
1035 req->flags |= CF_WAKE_WRITE;
1036 goto missing_data_or_waiting;
1037 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001038 }
1039
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001040 if (msg->msg_state >= HTTP_MSG_ENDING)
1041 goto ending;
1042
1043 if (txn->meth == HTTP_METH_CONNECT) {
1044 msg->msg_state = HTTP_MSG_ENDING;
1045 goto ending;
1046 }
1047
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001048 /* Forward input data. We get it by removing all outgoing data not
1049 * forwarded yet from HTX data size. If there are some data filters, we
1050 * let them decide the amount of data to forward.
Christopher Faulet9768c262018-10-22 09:34:31 +02001051 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001052 if (HAS_REQ_DATA_FILTERS(s)) {
1053 ret = flt_http_payload(s, msg, htx->data);
1054 if (ret < 0)
1055 goto return_bad_req;
Christopher Faulet421e7692019-06-13 11:16:45 +02001056 c_adv(req, ret);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001057 }
1058 else {
Christopher Faulet421e7692019-06-13 11:16:45 +02001059 c_adv(req, htx->data - co_data(req));
Christopher Faulet66af0b22019-03-22 14:54:52 +01001060 if (msg->flags & HTTP_MSGF_XFER_LEN)
1061 channel_htx_forward_forever(req, htx);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001062 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001063
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001064 if (htx->data != co_data(req))
1065 goto missing_data_or_waiting;
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001066
Christopher Faulet9768c262018-10-22 09:34:31 +02001067 /* Check if the end-of-message is reached and if so, switch the message
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001068 * in HTTP_MSG_ENDING state. Then if all data was marked to be
1069 * forwarded, set the state to HTTP_MSG_DONE.
Christopher Faulet9768c262018-10-22 09:34:31 +02001070 */
1071 if (htx_get_tail_type(htx) != HTX_BLK_EOM)
1072 goto missing_data_or_waiting;
1073
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001074 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet9768c262018-10-22 09:34:31 +02001075
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001076 ending:
Christopher Faulet2151cdd2020-07-22 16:34:59 +02001077 req->flags &= ~CF_EXPECT_MORE; /* no more data are expected */
1078
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001079 /* other states, ENDING...TUNNEL */
1080 if (msg->msg_state >= HTTP_MSG_DONE)
1081 goto done;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001082
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001083 if (HAS_REQ_DATA_FILTERS(s)) {
1084 ret = flt_http_end(s, msg);
1085 if (ret <= 0) {
1086 if (!ret)
1087 goto missing_data_or_waiting;
1088 goto return_bad_req;
1089 }
1090 }
1091
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001092 if (txn->meth == HTTP_METH_CONNECT)
1093 msg->msg_state = HTTP_MSG_TUNNEL;
1094 else {
1095 msg->msg_state = HTTP_MSG_DONE;
1096 req->to_forward = 0;
1097 }
1098
1099 done:
1100 /* we don't want to forward closes on DONE except in tunnel mode. */
1101 if (!(txn->flags & TX_CON_WANT_TUN))
1102 channel_dont_close(req);
1103
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001104 http_end_request(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001105 if (!(req->analysers & an_bit)) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001106 http_end_response(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001107 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
1108 if (req->flags & CF_SHUTW) {
1109 /* request errors are most likely due to the
1110 * server aborting the transfer. */
Christopher Faulet93e02d82019-03-08 14:18:50 +01001111 goto return_srv_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001112 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001113 goto return_bad_req;
1114 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001115 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001116 return 1;
1117 }
1118
1119 /* If "option abortonclose" is set on the backend, we want to monitor
1120 * the client's connection and forward any shutdown notification to the
1121 * server, which will decide whether to close or to go on processing the
1122 * request. We only do that in tunnel mode, and not in other modes since
1123 * it can be abused to exhaust source ports. */
Christopher Faulet769d0e92019-03-22 14:23:18 +01001124 if (s->be->options & PR_O_ABRT_CLOSE) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001125 channel_auto_read(req);
Christopher Fauletc41547b2019-07-16 14:32:23 +02001126 if ((req->flags & (CF_SHUTR|CF_READ_NULL)) && !(txn->flags & TX_CON_WANT_TUN))
Christopher Faulete0768eb2018-10-03 16:38:02 +02001127 s->si[1].flags |= SI_FL_NOLINGER;
1128 channel_auto_close(req);
1129 }
1130 else if (s->txn->meth == HTTP_METH_POST) {
1131 /* POST requests may require to read extra CRLF sent by broken
1132 * browsers and which could cause an RST to be sent upon close
1133 * on some systems (eg: Linux). */
1134 channel_auto_read(req);
1135 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001136 DBG_TRACE_DEVEL("waiting for the end of the HTTP txn",
1137 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001138 return 0;
1139
1140 missing_data_or_waiting:
1141 /* stop waiting for data if the input is closed before the end */
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001142 if (msg->msg_state < HTTP_MSG_ENDING && req->flags & CF_SHUTR)
Christopher Faulet93e02d82019-03-08 14:18:50 +01001143 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001144
1145 waiting:
1146 /* waiting for the last bits to leave the buffer */
1147 if (req->flags & CF_SHUTW)
Christopher Faulet93e02d82019-03-08 14:18:50 +01001148 goto return_srv_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001149
1150 /* When TE: chunked is used, we need to get there again to parse remaining
1151 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
1152 * And when content-length is used, we never want to let the possible
1153 * shutdown be forwarded to the other side, as the state machine will
1154 * take care of it once the client responds. It's also important to
1155 * prevent TIME_WAITs from accumulating on the backend side, and for
1156 * HTTP/2 where the last frame comes with a shutdown.
1157 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001158 if (msg->flags & HTTP_MSGF_XFER_LEN)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001159 channel_dont_close(req);
1160
1161 /* We know that more data are expected, but we couldn't send more that
1162 * what we did. So we always set the CF_EXPECT_MORE flag so that the
1163 * system knows it must not set a PUSH on this first part. Interactive
1164 * modes are already handled by the stream sock layer. We must not do
1165 * this in content-length mode because it could present the MSG_MORE
1166 * flag with the last block of forwarded data, which would cause an
1167 * additional delay to be observed by the receiver.
1168 */
Christopher Faulet2151cdd2020-07-22 16:34:59 +02001169 if (HAS_REQ_DATA_FILTERS(s))
Christopher Faulete0768eb2018-10-03 16:38:02 +02001170 req->flags |= CF_EXPECT_MORE;
1171
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001172 DBG_TRACE_DEVEL("waiting for more data to forward",
1173 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001174 return 0;
1175
Christopher Faulet93e02d82019-03-08 14:18:50 +01001176 return_cli_abort:
1177 _HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
1178 _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001179 if (sess->listener->counters)
1180 _HA_ATOMIC_ADD(&sess->listener->counters->cli_aborts, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001181 if (objt_server(s->target))
Christopher Fauletcff0f732019-12-16 16:13:44 +01001182 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.cli_aborts, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001183 if (!(s->flags & SF_ERR_MASK))
1184 s->flags |= SF_ERR_CLICL;
1185 status = 400;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001186 goto return_prx_cond;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001187
1188 return_srv_abort:
1189 _HA_ATOMIC_ADD(&sess->fe->fe_counters.srv_aborts, 1);
1190 _HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001191 if (sess->listener->counters)
1192 _HA_ATOMIC_ADD(&sess->listener->counters->srv_aborts, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001193 if (objt_server(s->target))
Christopher Fauletcff0f732019-12-16 16:13:44 +01001194 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.srv_aborts, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001195 if (!(s->flags & SF_ERR_MASK))
1196 s->flags |= SF_ERR_SRVCL;
1197 status = 502;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001198 goto return_prx_cond;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001199
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001200 return_int_err:
1201 if (!(s->flags & SF_ERR_MASK))
1202 s->flags |= SF_ERR_INTERNAL;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001203 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001204 _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01001205 if (sess->listener->counters)
1206 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001207 if (objt_server(s->target))
1208 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.internal_errors, 1);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001209 status = 500;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001210 goto return_prx_cond;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001211
Christopher Faulet93e02d82019-03-08 14:18:50 +01001212 return_bad_req:
Olivier Houcharda798bf52019-03-08 18:52:00 +01001213 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001214 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +01001215 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001216 status = 400;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001217 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001218
Christopher Fauletb8a53712019-12-16 11:29:38 +01001219 return_prx_cond:
Christopher Faulet9768c262018-10-22 09:34:31 +02001220 if (txn->status > 0) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001221 /* Note: we don't send any error if some data were already sent */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001222 http_reply_and_close(s, txn->status, NULL);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001223 } else {
Christopher Faulet93e02d82019-03-08 14:18:50 +01001224 txn->status = status;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001225 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001226 }
1227 req->analysers &= AN_REQ_FLT_END;
1228 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 +01001229 if (!(s->flags & SF_ERR_MASK))
1230 s->flags |= SF_ERR_PRXCOND;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001231 if (!(s->flags & SF_FINST_MASK))
1232 s->flags |= ((txn->rsp.msg_state < HTTP_MSG_ERROR) ? SF_FINST_H : SF_FINST_D);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001233 DBG_TRACE_DEVEL("leaving on error ",
1234 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001235 return 0;
1236}
1237
Olivier Houcharda254a372019-04-05 15:30:12 +02001238/* Reset the stream and the backend stream_interface to a situation suitable for attemption connection */
1239/* Returns 0 if we can attempt to retry, -1 otherwise */
1240static __inline int do_l7_retry(struct stream *s, struct stream_interface *si)
1241{
1242 struct channel *req, *res;
1243 int co_data;
1244
1245 si->conn_retries--;
1246 if (si->conn_retries < 0)
1247 return -1;
1248
Willy Tarreau223995e2019-05-04 10:38:31 +02001249 if (objt_server(s->target))
1250 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.retries, 1);
1251 _HA_ATOMIC_ADD(&s->be->be_counters.retries, 1);
1252
Olivier Houcharda254a372019-04-05 15:30:12 +02001253 req = &s->req;
1254 res = &s->res;
1255 /* Remove any write error from the request, and read error from the response */
1256 req->flags &= ~(CF_WRITE_ERROR | CF_WRITE_TIMEOUT | CF_SHUTW | CF_SHUTW_NOW);
1257 res->flags &= ~(CF_READ_ERROR | CF_READ_TIMEOUT | CF_SHUTR | CF_EOI | CF_READ_NULL | CF_SHUTR_NOW);
1258 res->analysers = 0;
1259 si->flags &= ~(SI_FL_ERR | SI_FL_EXP | SI_FL_RXBLK_SHUT);
Olivier Houchard8cabc972020-05-12 22:18:14 +02001260 s->flags &= ~SF_ADDR_SET;
Olivier Houchard4bd58672019-07-12 16:16:59 +02001261 stream_choose_redispatch(s);
Olivier Houcharda254a372019-04-05 15:30:12 +02001262 si->exp = TICK_ETERNITY;
1263 res->rex = TICK_ETERNITY;
1264 res->to_forward = 0;
1265 res->analyse_exp = TICK_ETERNITY;
1266 res->total = 0;
Olivier Houchard4bd58672019-07-12 16:16:59 +02001267 s->flags &= ~(SF_ERR_SRVTO | SF_ERR_SRVCL);
Olivier Houcharda254a372019-04-05 15:30:12 +02001268 si_release_endpoint(&s->si[1]);
1269 b_free(&req->buf);
1270 /* Swap the L7 buffer with the channel buffer */
1271 /* We know we stored the co_data as b_data, so get it there */
1272 co_data = b_data(&si->l7_buffer);
1273 b_set_data(&si->l7_buffer, b_size(&si->l7_buffer));
1274 b_xfer(&req->buf, &si->l7_buffer, b_data(&si->l7_buffer));
1275
1276 co_set_data(req, co_data);
1277 b_reset(&res->buf);
1278 co_set_data(res, 0);
1279 return 0;
1280}
1281
Christopher Faulete0768eb2018-10-03 16:38:02 +02001282/* This stream analyser waits for a complete HTTP response. It returns 1 if the
1283 * processing can continue on next analysers, or zero if it either needs more
1284 * data or wants to immediately abort the response (eg: timeout, error, ...). It
1285 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->res.analysers
1286 * when it has nothing left to do, and may remove any analyser when it wants to
1287 * abort.
1288 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001289int http_wait_for_response(struct stream *s, struct channel *rep, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001290{
Christopher Faulet9768c262018-10-22 09:34:31 +02001291 /*
1292 * We will analyze a complete HTTP response to check the its syntax.
1293 *
1294 * Once the start line and all headers are received, we may perform a
1295 * capture of the error (if any), and we will set a few fields. We also
1296 * logging and finally headers capture.
1297 */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001298 struct session *sess = s->sess;
1299 struct http_txn *txn = s->txn;
1300 struct http_msg *msg = &txn->rsp;
Christopher Faulet9768c262018-10-22 09:34:31 +02001301 struct htx *htx;
Olivier Houcharda254a372019-04-05 15:30:12 +02001302 struct stream_interface *si_b = &s->si[1];
Christopher Faulet61608322018-11-23 16:23:45 +01001303 struct connection *srv_conn;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001304 struct htx_sl *sl;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001305 int n;
1306
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001307 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001308
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001309 htx = htxbuf(&rep->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001310
Willy Tarreau4236f032019-03-05 10:43:32 +01001311 /* Parsing errors are caught here */
1312 if (htx->flags & HTX_FL_PARSING_ERROR)
1313 goto return_bad_res;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001314 if (htx->flags & HTX_FL_PROCESSING_ERROR)
1315 goto return_int_err;
Willy Tarreau4236f032019-03-05 10:43:32 +01001316
Christopher Faulete0768eb2018-10-03 16:38:02 +02001317 /*
1318 * Now we quickly check if we have found a full valid response.
1319 * If not so, we check the FD and buffer states before leaving.
1320 * A full response is indicated by the fact that we have seen
1321 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
1322 * responses are checked first.
1323 *
1324 * Depending on whether the client is still there or not, we
1325 * may send an error response back or not. Note that normally
1326 * we should only check for HTTP status there, and check I/O
1327 * errors somewhere else.
1328 */
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001329 next_one:
Christopher Faulet29f17582019-05-23 11:03:26 +02001330 if (unlikely(htx_is_empty(htx) || htx->first == -1)) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001331 /* 1: have we encountered a read error ? */
1332 if (rep->flags & CF_READ_ERROR) {
Olivier Houchard865d8392019-05-03 22:46:27 +02001333 struct connection *conn = NULL;
1334
Olivier Houchard865d8392019-05-03 22:46:27 +02001335 if (objt_cs(s->si[1].end))
1336 conn = objt_cs(s->si[1].end)->conn;
1337
1338 if (si_b->flags & SI_FL_L7_RETRY &&
1339 (!conn || conn->err_code != CO_ER_SSL_EARLY_FAILED)) {
Olivier Houcharda254a372019-04-05 15:30:12 +02001340 /* If we arrive here, then CF_READ_ERROR was
1341 * set by si_cs_recv() because we matched a
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05001342 * status, otherwise it would have removed
Olivier Houcharda254a372019-04-05 15:30:12 +02001343 * the SI_FL_L7_RETRY flag, so it's ok not
1344 * to check s->be->retry_type.
1345 */
1346 if (co_data(rep) || do_l7_retry(s, si_b) == 0)
1347 return 0;
1348 }
1349
Olivier Houchard6db16992019-05-17 15:40:49 +02001350 if (txn->flags & TX_NOT_FIRST)
1351 goto abort_keep_alive;
1352
Olivier Houcharda798bf52019-03-08 18:52:00 +01001353 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001354 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001355 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001356 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001357 }
1358
Christopher Faulete0768eb2018-10-03 16:38:02 +02001359 rep->analysers &= AN_RES_FLT_END;
1360 txn->status = 502;
1361
1362 /* Check to see if the server refused the early data.
1363 * If so, just send a 425
1364 */
Willy Tarreauee99aaf2020-06-23 05:58:20 +02001365 if (conn && conn->err_code == CO_ER_SSL_EARLY_FAILED) {
Olivier Houchard865d8392019-05-03 22:46:27 +02001366 if ((s->be->retry_type & PR_RE_EARLY_ERROR) &&
Olivier Houchardad26d8d2019-05-10 17:48:28 +02001367 (si_b->flags & SI_FL_L7_RETRY) &&
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001368 do_l7_retry(s, si_b) == 0) {
1369 DBG_TRACE_DEVEL("leaving on L7 retry",
1370 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houchard865d8392019-05-03 22:46:27 +02001371 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001372 }
Olivier Houchard865d8392019-05-03 22:46:27 +02001373 txn->status = 425;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001374 }
1375
1376 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001377 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001378
1379 if (!(s->flags & SF_ERR_MASK))
1380 s->flags |= SF_ERR_SRVCL;
1381 if (!(s->flags & SF_FINST_MASK))
1382 s->flags |= SF_FINST_H;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001383 DBG_TRACE_DEVEL("leaving on error",
1384 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001385 return 0;
1386 }
1387
Christopher Faulet9768c262018-10-22 09:34:31 +02001388 /* 2: read timeout : return a 504 to the client. */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001389 else if (rep->flags & CF_READ_TIMEOUT) {
Olivier Houcharda254a372019-04-05 15:30:12 +02001390 if ((si_b->flags & SI_FL_L7_RETRY) &&
1391 (s->be->retry_type & PR_RE_TIMEOUT)) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001392 if (co_data(rep) || do_l7_retry(s, si_b) == 0) {
1393 DBG_TRACE_DEVEL("leaving on L7 retry",
1394 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharda254a372019-04-05 15:30:12 +02001395 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001396 }
Olivier Houcharda254a372019-04-05 15:30:12 +02001397 }
Olivier Houcharda798bf52019-03-08 18:52:00 +01001398 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001399 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001400 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001401 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001402 }
1403
Christopher Faulete0768eb2018-10-03 16:38:02 +02001404 rep->analysers &= AN_RES_FLT_END;
1405 txn->status = 504;
1406 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001407 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001408
1409 if (!(s->flags & SF_ERR_MASK))
1410 s->flags |= SF_ERR_SRVTO;
1411 if (!(s->flags & SF_FINST_MASK))
1412 s->flags |= SF_FINST_H;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001413 DBG_TRACE_DEVEL("leaving on error",
1414 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001415 return 0;
1416 }
1417
Christopher Faulet9768c262018-10-22 09:34:31 +02001418 /* 3: client abort with an abortonclose */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001419 else if ((rep->flags & CF_SHUTR) && ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001420 _HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
1421 _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001422 if (sess->listener->counters)
1423 _HA_ATOMIC_ADD(&sess->listener->counters->cli_aborts, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001424 if (objt_server(s->target))
Olivier Houcharda798bf52019-03-08 18:52:00 +01001425 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.cli_aborts, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001426
1427 rep->analysers &= AN_RES_FLT_END;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001428 txn->status = 400;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001429 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001430
1431 if (!(s->flags & SF_ERR_MASK))
1432 s->flags |= SF_ERR_CLICL;
1433 if (!(s->flags & SF_FINST_MASK))
1434 s->flags |= SF_FINST_H;
1435
1436 /* process_stream() will take care of the error */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001437 DBG_TRACE_DEVEL("leaving on error",
1438 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001439 return 0;
1440 }
1441
Christopher Faulet9768c262018-10-22 09:34:31 +02001442 /* 4: close from server, capture the response if the server has started to respond */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001443 else if (rep->flags & CF_SHUTR) {
Olivier Houcharda254a372019-04-05 15:30:12 +02001444 if ((si_b->flags & SI_FL_L7_RETRY) &&
1445 (s->be->retry_type & PR_RE_DISCONNECTED)) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001446 if (co_data(rep) || do_l7_retry(s, si_b) == 0) {
1447 DBG_TRACE_DEVEL("leaving on L7 retry",
1448 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharda254a372019-04-05 15:30:12 +02001449 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001450 }
Olivier Houcharda254a372019-04-05 15:30:12 +02001451 }
1452
Olivier Houchard6db16992019-05-17 15:40:49 +02001453 if (txn->flags & TX_NOT_FIRST)
1454 goto abort_keep_alive;
1455
Olivier Houcharda798bf52019-03-08 18:52:00 +01001456 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001457 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001458 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001459 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001460 }
1461
Christopher Faulete0768eb2018-10-03 16:38:02 +02001462 rep->analysers &= AN_RES_FLT_END;
1463 txn->status = 502;
1464 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001465 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001466
1467 if (!(s->flags & SF_ERR_MASK))
1468 s->flags |= SF_ERR_SRVCL;
1469 if (!(s->flags & SF_FINST_MASK))
1470 s->flags |= SF_FINST_H;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001471 DBG_TRACE_DEVEL("leaving on error",
1472 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001473 return 0;
1474 }
1475
Christopher Faulet9768c262018-10-22 09:34:31 +02001476 /* 5: write error to client (we don't send any message then) */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001477 else if (rep->flags & CF_WRITE_ERROR) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001478 if (txn->flags & TX_NOT_FIRST)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001479 goto abort_keep_alive;
1480
Olivier Houcharda798bf52019-03-08 18:52:00 +01001481 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001482 if (objt_server(s->target))
1483 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001484 rep->analysers &= AN_RES_FLT_END;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001485
1486 if (!(s->flags & SF_ERR_MASK))
1487 s->flags |= SF_ERR_CLICL;
1488 if (!(s->flags & SF_FINST_MASK))
1489 s->flags |= SF_FINST_H;
1490
1491 /* process_stream() will take care of the error */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001492 DBG_TRACE_DEVEL("leaving on error",
1493 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001494 return 0;
1495 }
1496
1497 channel_dont_close(rep);
1498 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001499 DBG_TRACE_DEVEL("waiting for more data",
1500 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001501 return 0;
1502 }
1503
1504 /* More interesting part now : we know that we have a complete
1505 * response which at least looks like HTTP. We have an indicator
1506 * of each header's length, so we can parse them quickly.
1507 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001508 msg->msg_state = HTTP_MSG_BODY;
Christopher Faulet29f17582019-05-23 11:03:26 +02001509 BUG_ON(htx_get_first_type(htx) != HTX_BLK_RES_SL);
Christopher Faulet297fbb42019-05-13 14:41:27 +02001510 sl = http_get_stline(htx);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001511
Christopher Faulet9768c262018-10-22 09:34:31 +02001512 /* 0: we might have to print this header in debug mode */
1513 if (unlikely((global.mode & MODE_DEBUG) &&
1514 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
1515 int32_t pos;
1516
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001517 http_debug_stline("srvrep", s, sl);
Christopher Faulet9768c262018-10-22 09:34:31 +02001518
Christopher Fauleta3f15502019-05-13 15:27:23 +02001519 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001520 struct htx_blk *blk = htx_get_blk(htx, pos);
1521 enum htx_blk_type type = htx_get_blk_type(blk);
1522
1523 if (type == HTX_BLK_EOH)
1524 break;
1525 if (type != HTX_BLK_HDR)
1526 continue;
1527
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001528 http_debug_hdr("srvhdr", s,
1529 htx_get_blk_name(htx, blk),
1530 htx_get_blk_value(htx, blk));
Christopher Faulet9768c262018-10-22 09:34:31 +02001531 }
1532 }
1533
Christopher Faulet03599112018-11-27 11:21:21 +01001534 /* 1: get the status code and the version. Also set HTTP flags */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001535 txn->status = sl->info.res.status;
Christopher Faulet03599112018-11-27 11:21:21 +01001536 if (sl->flags & HTX_SL_F_VER_11)
Christopher Faulet9768c262018-10-22 09:34:31 +02001537 msg->flags |= HTTP_MSGF_VER_11;
Christopher Faulet03599112018-11-27 11:21:21 +01001538 if (sl->flags & HTX_SL_F_XFER_LEN) {
1539 msg->flags |= HTTP_MSGF_XFER_LEN;
Christopher Faulet834eee72019-02-18 11:35:02 +01001540 msg->flags |= ((sl->flags & HTX_SL_F_CLEN) ? HTTP_MSGF_CNT_LEN : HTTP_MSGF_TE_CHNK);
Christopher Fauletb2db4fa2018-11-27 16:51:09 +01001541 if (sl->flags & HTX_SL_F_BODYLESS)
1542 msg->flags |= HTTP_MSGF_BODYLESS;
Christopher Faulet03599112018-11-27 11:21:21 +01001543 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001544
1545 n = txn->status / 100;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001546 if (n < 1 || n > 5)
1547 n = 0;
Christopher Faulet9768c262018-10-22 09:34:31 +02001548
Christopher Faulete0768eb2018-10-03 16:38:02 +02001549 /* when the client triggers a 4xx from the server, it's most often due
1550 * to a missing object or permission. These events should be tracked
1551 * because if they happen often, it may indicate a brute force or a
1552 * vulnerability scan.
1553 */
1554 if (n == 4)
1555 stream_inc_http_err_ctr(s);
1556
Marcin Deranek3c27dda2020-05-15 18:32:51 +02001557 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001558 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.p.http.rsp[n], 1);
Marcin Deranek3c27dda2020-05-15 18:32:51 +02001559 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.p.http.cum_req, 1);
1560 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001561
Christopher Faulete0768eb2018-10-03 16:38:02 +02001562 /* Adjust server's health based on status code. Note: status codes 501
1563 * and 505 are triggered on demand by client request, so we must not
1564 * count them as server failures.
1565 */
1566 if (objt_server(s->target)) {
1567 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001568 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_OK);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001569 else
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001570 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_STS);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001571 }
1572
1573 /*
1574 * We may be facing a 100-continue response, or any other informational
1575 * 1xx response which is non-final, in which case this is not the right
1576 * response, and we're waiting for the next one. Let's allow this response
1577 * to go to the client and wait for the next one. There's an exception for
1578 * 101 which is used later in the code to switch protocols.
1579 */
1580 if (txn->status < 200 &&
1581 (txn->status == 100 || txn->status >= 102)) {
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001582 FLT_STRM_CB(s, flt_http_reset(s, msg));
Christopher Faulet421e7692019-06-13 11:16:45 +02001583 htx->first = channel_htx_fwd_headers(rep, htx);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001584 msg->msg_state = HTTP_MSG_RPBEFORE;
Christopher Faulet3499f622019-09-03 15:23:54 +02001585 msg->flags = 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001586 txn->status = 0;
1587 s->logs.t_data = -1; /* was not a response yet */
Christopher Faulet7d518452020-08-31 11:07:07 +02001588 rep->flags |= CF_SEND_DONTWAIT; /* Send ASAP informational messages */
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001589 goto next_one;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001590 }
1591
1592 /*
1593 * 2: check for cacheability.
1594 */
1595
1596 switch (txn->status) {
1597 case 200:
1598 case 203:
1599 case 204:
1600 case 206:
1601 case 300:
1602 case 301:
1603 case 404:
1604 case 405:
1605 case 410:
1606 case 414:
1607 case 501:
1608 break;
1609 default:
1610 /* RFC7231#6.1:
1611 * Responses with status codes that are defined as
1612 * cacheable by default (e.g., 200, 203, 204, 206,
1613 * 300, 301, 404, 405, 410, 414, and 501 in this
1614 * specification) can be reused by a cache with
1615 * heuristic expiration unless otherwise indicated
1616 * by the method definition or explicit cache
1617 * controls [RFC7234]; all other status codes are
1618 * not cacheable by default.
1619 */
1620 txn->flags &= ~(TX_CACHEABLE | TX_CACHE_COOK);
1621 break;
1622 }
1623
1624 /*
1625 * 3: we may need to capture headers
1626 */
1627 s->logs.logwait &= ~LW_RESP;
1628 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->res_cap))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001629 http_capture_headers(htx, s->res_cap, sess->fe->rsp_cap);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001630
Christopher Faulet9768c262018-10-22 09:34:31 +02001631 /* Skip parsing if no content length is possible. */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001632 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
1633 txn->status == 101)) {
1634 /* Either we've established an explicit tunnel, or we're
1635 * switching the protocol. In both cases, we're very unlikely
1636 * to understand the next protocols. We have to switch to tunnel
1637 * mode, so that we transfer the request and responses then let
1638 * this protocol pass unmodified. When we later implement specific
1639 * parsers for such protocols, we'll want to check the Upgrade
1640 * header which contains information about that protocol for
1641 * responses with status 101 (eg: see RFC2817 about TLS).
1642 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02001643 txn->flags |= TX_CON_WANT_TUN;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001644 }
1645
Christopher Faulet61608322018-11-23 16:23:45 +01001646 /* check for NTML authentication headers in 401 (WWW-Authenticate) and
1647 * 407 (Proxy-Authenticate) responses and set the connection to private
1648 */
1649 srv_conn = cs_conn(objt_cs(s->si[1].end));
1650 if (srv_conn) {
1651 struct ist hdr;
1652 struct http_hdr_ctx ctx;
1653
1654 if (txn->status == 401)
1655 hdr = ist("WWW-Authenticate");
1656 else if (txn->status == 407)
1657 hdr = ist("Proxy-Authenticate");
1658 else
1659 goto end;
1660
1661 ctx.blk = NULL;
1662 while (http_find_header(htx, hdr, &ctx, 0)) {
Willy Tarreauf1dcced2020-05-07 19:27:02 +02001663 /* If www-authenticate contains "Negotiate", "Nego2", or "NTLM",
1664 * possibly followed by blanks and a base64 string, the connection
1665 * is private. Since it's a mess to deal with, we only check for
1666 * values starting with "NTLM" or "Nego". Note that often multiple
1667 * headers are sent by the server there.
1668 */
1669 if ((ctx.value.len >= 4 && strncasecmp(ctx.value.ptr, "Nego", 4) == 0) ||
Willy Tarreau49a1d282020-05-07 19:10:15 +02001670 (ctx.value.len >= 4 && strncasecmp(ctx.value.ptr, "NTLM", 4) == 0)) {
Olivier Houchard250031e2019-05-29 15:01:50 +02001671 sess->flags |= SESS_FL_PREFER_LAST;
Christopher Faulet08016ab2020-07-01 16:10:06 +02001672 conn_set_owner(srv_conn, sess, NULL);
Christopher Faulet21ddc742020-07-01 15:26:14 +02001673 conn_set_private(srv_conn);
Ilya Shipitsin6b79f382020-07-23 00:32:55 +05001674 /* If it fail now, the same will be done in mux->detach() callback */
Christopher Faulet08016ab2020-07-01 16:10:06 +02001675 session_add_conn(srv_conn->owner, srv_conn, srv_conn->target);
Willy Tarreauf1dcced2020-05-07 19:27:02 +02001676 break;
Olivier Houchard250031e2019-05-29 15:01:50 +02001677 }
Christopher Faulet61608322018-11-23 16:23:45 +01001678 }
1679 }
1680
1681 end:
Christopher Faulete0768eb2018-10-03 16:38:02 +02001682 /* we want to have the response time before we start processing it */
1683 s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now);
1684
1685 /* end of job, return OK */
1686 rep->analysers &= ~an_bit;
1687 rep->analyse_exp = TICK_ETERNITY;
1688 channel_auto_close(rep);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001689 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001690 return 1;
1691
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001692 return_int_err:
Christopher Fauletcff0f732019-12-16 16:13:44 +01001693 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01001694 _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001695 if (sess->listener->counters)
1696 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01001697 if (objt_server(s->target))
1698 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.internal_errors, 1);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001699 txn->status = 500;
1700 if (!(s->flags & SF_ERR_MASK))
1701 s->flags |= SF_ERR_INTERNAL;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001702 goto return_prx_cond;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001703
1704 return_bad_res:
Olivier Houcharda798bf52019-03-08 18:52:00 +01001705 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulet47365272018-10-31 17:40:50 +01001706 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001707 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001708 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Christopher Faulet47365272018-10-31 17:40:50 +01001709 }
Olivier Houcharde3249a92019-05-03 23:01:47 +02001710 if ((s->be->retry_type & PR_RE_JUNK_REQUEST) &&
Olivier Houchardad26d8d2019-05-10 17:48:28 +02001711 (si_b->flags & SI_FL_L7_RETRY) &&
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001712 do_l7_retry(s, si_b) == 0) {
1713 DBG_TRACE_DEVEL("leaving on L7 retry",
1714 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharde3249a92019-05-03 23:01:47 +02001715 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001716 }
Christopher Faulet47365272018-10-31 17:40:50 +01001717 txn->status = 502;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001718 /* fall through */
1719
Christopher Fauletb8a53712019-12-16 11:29:38 +01001720 return_prx_cond:
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001721 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet47365272018-10-31 17:40:50 +01001722
1723 if (!(s->flags & SF_ERR_MASK))
1724 s->flags |= SF_ERR_PRXCOND;
1725 if (!(s->flags & SF_FINST_MASK))
1726 s->flags |= SF_FINST_H;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001727
1728 s->si[1].flags |= SI_FL_NOLINGER;
1729 rep->analysers &= AN_RES_FLT_END;
Christopher Faulete58c0002020-03-02 16:21:01 +01001730 s->req.analysers &= AN_REQ_FLT_END;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001731 rep->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001732 DBG_TRACE_DEVEL("leaving on error",
1733 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulet47365272018-10-31 17:40:50 +01001734 return 0;
1735
Christopher Faulete0768eb2018-10-03 16:38:02 +02001736 abort_keep_alive:
1737 /* A keep-alive request to the server failed on a network error.
1738 * The client is required to retry. We need to close without returning
1739 * any other information so that the client retries.
1740 */
1741 txn->status = 0;
1742 rep->analysers &= AN_RES_FLT_END;
1743 s->req.analysers &= AN_REQ_FLT_END;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001744 s->logs.logwait = 0;
1745 s->logs.level = 0;
1746 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001747 http_reply_and_close(s, txn->status, NULL);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001748 DBG_TRACE_DEVEL("leaving by closing K/A connection",
1749 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001750 return 0;
1751}
1752
1753/* This function performs all the processing enabled for the current response.
1754 * It normally returns 1 unless it wants to break. It relies on buffers flags,
1755 * and updates s->res.analysers. It might make sense to explode it into several
1756 * other functions. It works like process_request (see indications above).
1757 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001758int http_process_res_common(struct stream *s, struct channel *rep, int an_bit, struct proxy *px)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001759{
1760 struct session *sess = s->sess;
1761 struct http_txn *txn = s->txn;
1762 struct http_msg *msg = &txn->rsp;
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001763 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001764 struct proxy *cur_proxy;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001765 enum rule_result ret = HTTP_RULE_RES_CONT;
1766
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001767 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
1768 return 0;
Christopher Faulet9768c262018-10-22 09:34:31 +02001769
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001770 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001771
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001772 htx = htxbuf(&rep->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001773
1774 /* The stats applet needs to adjust the Connection header but we don't
1775 * apply any filter there.
1776 */
1777 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
1778 rep->analysers &= ~an_bit;
1779 rep->analyse_exp = TICK_ETERNITY;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001780 goto end;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001781 }
1782
1783 /*
1784 * We will have to evaluate the filters.
1785 * As opposed to version 1.2, now they will be evaluated in the
1786 * filters order and not in the header order. This means that
1787 * each filter has to be validated among all headers.
1788 *
1789 * Filters are tried with ->be first, then with ->fe if it is
1790 * different from ->be.
1791 *
1792 * Maybe we are in resume condiion. In this case I choose the
1793 * "struct proxy" which contains the rule list matching the resume
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05001794 * pointer. If none of these "struct proxy" match, I initialise
Christopher Faulete0768eb2018-10-03 16:38:02 +02001795 * the process with the first one.
1796 *
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05001797 * In fact, I check only correspondence between the current list
Christopher Faulete0768eb2018-10-03 16:38:02 +02001798 * pointer and the ->fe rule list. If it doesn't match, I initialize
1799 * the loop with the ->be.
1800 */
1801 if (s->current_rule_list == &sess->fe->http_res_rules)
1802 cur_proxy = sess->fe;
1803 else
1804 cur_proxy = s->be;
1805 while (1) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001806 /* evaluate http-response rules */
1807 if (ret == HTTP_RULE_RES_CONT) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001808 ret = http_res_get_intercept_rule(cur_proxy, &cur_proxy->http_res_rules, s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001809
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001810 switch (ret) {
1811 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
1812 goto return_prx_yield;
1813
1814 case HTTP_RULE_RES_CONT:
1815 case HTTP_RULE_RES_STOP: /* nothing to do */
1816 break;
1817
1818 case HTTP_RULE_RES_DENY: /* deny or tarpit */
1819 goto deny;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001820
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001821 case HTTP_RULE_RES_ABRT: /* abort request, response already sent */
1822 goto return_prx_cond;
1823
1824 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
Christopher Fauletb8a53712019-12-16 11:29:38 +01001825 goto done;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001826
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001827 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
1828 goto return_bad_res;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001829
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001830 case HTTP_RULE_RES_ERROR: /* failed with a bad request */
1831 goto return_int_err;
1832 }
1833
1834 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001835
Christopher Faulete0768eb2018-10-03 16:38:02 +02001836 /* check whether we're already working on the frontend */
1837 if (cur_proxy == sess->fe)
1838 break;
1839 cur_proxy = sess->fe;
1840 }
1841
Christopher Faulete0768eb2018-10-03 16:38:02 +02001842 /* OK that's all we can do for 1xx responses */
1843 if (unlikely(txn->status < 200 && txn->status != 101))
Christopher Fauletf2824e62018-10-01 12:12:37 +02001844 goto end;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001845
1846 /*
1847 * Now check for a server cookie.
1848 */
1849 if (s->be->cookie_name || sess->fe->capture_name || (s->be->options & PR_O_CHK_CACHE))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001850 http_manage_server_side_cookies(s, rep);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001851
1852 /*
1853 * Check for cache-control or pragma headers if required.
1854 */
1855 if ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001856 http_check_response_for_cacheability(s, rep);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001857
1858 /*
1859 * Add server cookie in the response if needed
1860 */
1861 if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) &&
1862 !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) &&
1863 (!(s->flags & SF_DIRECT) ||
1864 ((s->be->cookie_maxidle || txn->cookie_last_date) &&
1865 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
1866 (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
1867 (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
1868 (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
1869 !(s->flags & SF_IGNORE_PRST)) {
1870 /* the server is known, it's not the one the client requested, or the
1871 * cookie's last seen date needs to be refreshed. We have to
1872 * insert a set-cookie here, except if we want to insert only on POST
1873 * requests and this one isn't. Note that servers which don't have cookies
1874 * (eg: some backup servers) will return a full cookie removal request.
1875 */
1876 if (!objt_server(s->target)->cookie) {
1877 chunk_printf(&trash,
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001878 "%s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
Christopher Faulete0768eb2018-10-03 16:38:02 +02001879 s->be->cookie_name);
1880 }
1881 else {
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001882 chunk_printf(&trash, "%s=%s", s->be->cookie_name, objt_server(s->target)->cookie);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001883
1884 if (s->be->cookie_maxidle || s->be->cookie_maxlife) {
1885 /* emit last_date, which is mandatory */
1886 trash.area[trash.data++] = COOKIE_DELIM_DATE;
1887 s30tob64((date.tv_sec+3) >> 2,
1888 trash.area + trash.data);
1889 trash.data += 5;
1890
1891 if (s->be->cookie_maxlife) {
1892 /* emit first_date, which is either the original one or
1893 * the current date.
1894 */
1895 trash.area[trash.data++] = COOKIE_DELIM_DATE;
1896 s30tob64(txn->cookie_first_date ?
1897 txn->cookie_first_date >> 2 :
1898 (date.tv_sec+3) >> 2,
1899 trash.area + trash.data);
1900 trash.data += 5;
1901 }
1902 }
1903 chunk_appendf(&trash, "; path=/");
1904 }
1905
1906 if (s->be->cookie_domain)
1907 chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain);
1908
1909 if (s->be->ck_opts & PR_CK_HTTPONLY)
1910 chunk_appendf(&trash, "; HttpOnly");
1911
1912 if (s->be->ck_opts & PR_CK_SECURE)
1913 chunk_appendf(&trash, "; Secure");
1914
Christopher Faulet2f533902020-01-21 11:06:48 +01001915 if (s->be->cookie_attrs)
1916 chunk_appendf(&trash, "; %s", s->be->cookie_attrs);
1917
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001918 if (unlikely(!http_add_header(htx, ist("Set-Cookie"), ist2(trash.area, trash.data))))
Christopher Fauletb8a53712019-12-16 11:29:38 +01001919 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001920
1921 txn->flags &= ~TX_SCK_MASK;
1922 if (__objt_server(s->target)->cookie && (s->flags & SF_DIRECT))
1923 /* the server did not change, only the date was updated */
1924 txn->flags |= TX_SCK_UPDATED;
1925 else
1926 txn->flags |= TX_SCK_INSERTED;
1927
1928 /* Here, we will tell an eventual cache on the client side that we don't
1929 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
1930 * Some caches understand the correct form: 'no-cache="set-cookie"', but
1931 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
1932 */
1933 if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
1934
1935 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
1936
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001937 if (unlikely(!http_add_header(htx, ist("Cache-control"), ist("private"))))
Christopher Fauletb8a53712019-12-16 11:29:38 +01001938 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001939 }
1940 }
1941
1942 /*
1943 * Check if result will be cacheable with a cookie.
1944 * We'll block the response if security checks have caught
1945 * nasty things such as a cacheable cookie.
1946 */
1947 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
1948 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
1949 (s->be->options & PR_O_CHK_CACHE)) {
1950 /* we're in presence of a cacheable response containing
1951 * a set-cookie header. We'll block it as requested by
1952 * the 'checkcache' option, and send an alert.
1953 */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001954 ha_alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
1955 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
1956 send_log(s->be, LOG_ALERT,
1957 "Blocking cacheable cookie in response from instance %s, server %s.\n",
1958 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
Christopher Fauletb8a53712019-12-16 11:29:38 +01001959 goto deny;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001960 }
1961
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001962 end:
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01001963 /*
1964 * Evaluate after-response rules before forwarding the response. rules
1965 * from the backend are evaluated first, then one from the frontend if
1966 * it differs.
1967 */
1968 if (!http_eval_after_res_rules(s))
1969 goto return_int_err;
1970
Christopher Faulete0768eb2018-10-03 16:38:02 +02001971 /* Always enter in the body analyzer */
1972 rep->analysers &= ~AN_RES_FLT_XFER_DATA;
1973 rep->analysers |= AN_RES_HTTP_XFER_BODY;
1974
1975 /* if the user wants to log as soon as possible, without counting
1976 * bytes from the server, then this is the right moment. We have
1977 * to temporarily assign bytes_out to log what we currently have.
1978 */
1979 if (!LIST_ISEMPTY(&sess->fe->logformat) && !(s->logs.logwait & LW_BYTES)) {
1980 s->logs.t_close = s->logs.t_data; /* to get a valid end date */
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001981 s->logs.bytes_out = htx->data;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001982 s->do_log(s);
1983 s->logs.bytes_out = 0;
1984 }
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001985
Christopher Fauletb8a53712019-12-16 11:29:38 +01001986 done:
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01001987 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletb8a53712019-12-16 11:29:38 +01001988 rep->analysers &= ~an_bit;
1989 rep->analyse_exp = TICK_ETERNITY;
1990 return 1;
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001991
Christopher Fauletb8a53712019-12-16 11:29:38 +01001992 deny:
Christopher Fauletb8a53712019-12-16 11:29:38 +01001993 _HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_resp, 1);
Christopher Fauleta08546b2019-12-16 16:07:34 +01001994 _HA_ATOMIC_ADD(&s->be->be_counters.denied_resp, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01001995 if (sess->listener->counters)
1996 _HA_ATOMIC_ADD(&sess->listener->counters->denied_resp, 1);
Christopher Fauleta08546b2019-12-16 16:07:34 +01001997 if (objt_server(s->target))
1998 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.denied_resp, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01001999 goto return_prx_err;
2000
2001 return_int_err:
2002 txn->status = 500;
2003 if (!(s->flags & SF_ERR_MASK))
2004 s->flags |= SF_ERR_INTERNAL;
Christopher Fauletcff0f732019-12-16 16:13:44 +01002005 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002006 _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1);
2007 if (objt_server(s->target))
2008 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01002009 if (objt_server(s->target))
2010 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002011 goto return_prx_err;
2012
2013 return_bad_res:
2014 txn->status = 502;
Christopher Fauleta20a6532020-02-05 10:16:41 +01002015 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
2016 if (objt_server(s->target)) {
2017 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
2018 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_RSP);
2019 }
Christopher Fauletb8a53712019-12-16 11:29:38 +01002020 /* fall through */
2021
2022 return_prx_err:
2023 http_reply_and_close(s, txn->status, http_error_message(s));
2024 /* fall through */
2025
2026 return_prx_cond:
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002027 s->logs.t_data = -1; /* was not a valid response */
2028 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002029
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002030 if (!(s->flags & SF_ERR_MASK))
2031 s->flags |= SF_ERR_PRXCOND;
2032 if (!(s->flags & SF_FINST_MASK))
2033 s->flags |= SF_FINST_H;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002034
Christopher Faulete58c0002020-03-02 16:21:01 +01002035 rep->analysers &= AN_RES_FLT_END;
2036 s->req.analysers &= AN_REQ_FLT_END;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002037 rep->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002038 DBG_TRACE_DEVEL("leaving on error",
2039 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002040 return 0;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002041
2042 return_prx_yield:
2043 channel_dont_close(rep);
2044 DBG_TRACE_DEVEL("waiting for more data",
2045 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
2046 return 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002047}
2048
2049/* This function is an analyser which forwards response body (including chunk
2050 * sizes if any). It is called as soon as we must forward, even if we forward
2051 * zero byte. The only situation where it must not be called is when we're in
2052 * tunnel mode and we want to forward till the close. It's used both to forward
2053 * remaining data and to resync after end of body. It expects the msg_state to
2054 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
2055 * read more data, or 1 once we can go on with next request or end the stream.
2056 *
2057 * It is capable of compressing response data both in content-length mode and
2058 * in chunked mode. The state machines follows different flows depending on
2059 * whether content-length and chunked modes are used, since there are no
2060 * trailers in content-length :
2061 *
2062 * chk-mode cl-mode
2063 * ,----- BODY -----.
2064 * / \
2065 * V size > 0 V chk-mode
2066 * .--> SIZE -------------> DATA -------------> CRLF
2067 * | | size == 0 | last byte |
2068 * | v final crlf v inspected |
2069 * | TRAILERS -----------> DONE |
2070 * | |
2071 * `----------------------------------------------'
2072 *
2073 * Compression only happens in the DATA state, and must be flushed in final
2074 * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding
2075 * is performed at once on final states for all bytes parsed, or when leaving
2076 * on missing data.
2077 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002078int http_response_forward_body(struct stream *s, struct channel *res, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +02002079{
2080 struct session *sess = s->sess;
2081 struct http_txn *txn = s->txn;
2082 struct http_msg *msg = &s->txn->rsp;
Christopher Faulet9768c262018-10-22 09:34:31 +02002083 struct htx *htx;
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002084 int ret;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002085
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002086 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002087
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002088 htx = htxbuf(&res->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002089
Christopher Fauletb9a92f32019-09-09 10:15:21 +02002090 if (htx->flags & HTX_FL_PARSING_ERROR)
2091 goto return_bad_res;
2092 if (htx->flags & HTX_FL_PROCESSING_ERROR)
2093 goto return_int_err;
2094
Christopher Faulete0768eb2018-10-03 16:38:02 +02002095 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Christopher Fauletf2824e62018-10-01 12:12:37 +02002096 ((res->flags & CF_SHUTW) && (res->to_forward || co_data(res)))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02002097 /* Output closed while we were sending data. We must abort and
2098 * wake the other side up.
2099 */
Christopher Faulete0768eb2018-10-03 16:38:02 +02002100 msg->msg_state = HTTP_MSG_ERROR;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002101 http_end_response(s);
2102 http_end_request(s);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002103 DBG_TRACE_DEVEL("leaving on error",
2104 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002105 return 1;
2106 }
2107
Christopher Faulet9768c262018-10-22 09:34:31 +02002108 if (msg->msg_state == HTTP_MSG_BODY)
2109 msg->msg_state = HTTP_MSG_DATA;
2110
Christopher Faulete0768eb2018-10-03 16:38:02 +02002111 /* in most states, we should abort in case of early close */
2112 channel_auto_close(res);
2113
Christopher Faulete0768eb2018-10-03 16:38:02 +02002114 if (res->to_forward) {
Christopher Faulet66af0b22019-03-22 14:54:52 +01002115 if (res->to_forward == CHN_INFINITE_FORWARD) {
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002116 if (res->flags & CF_EOI)
2117 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet66af0b22019-03-22 14:54:52 +01002118 }
2119 else {
2120 /* We can't process the buffer's contents yet */
2121 res->flags |= CF_WAKE_WRITE;
2122 goto missing_data_or_waiting;
2123 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02002124 }
2125
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002126 if (msg->msg_state >= HTTP_MSG_ENDING)
2127 goto ending;
2128
2129 if ((txn->meth == HTTP_METH_CONNECT && txn->status == 200) || txn->status == 101 ||
2130 (!(msg->flags & HTTP_MSGF_XFER_LEN) && !HAS_RSP_DATA_FILTERS(s))) {
2131 msg->msg_state = HTTP_MSG_ENDING;
2132 goto ending;
2133 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002134
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002135 /* Forward input data. We get it by removing all outgoing data not
2136 * forwarded yet from HTX data size. If there are some data filters, we
2137 * let them decide the amount of data to forward.
Christopher Faulet9768c262018-10-22 09:34:31 +02002138 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002139 if (HAS_RSP_DATA_FILTERS(s)) {
2140 ret = flt_http_payload(s, msg, htx->data);
2141 if (ret < 0)
2142 goto return_bad_res;
Christopher Faulet421e7692019-06-13 11:16:45 +02002143 c_adv(res, ret);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002144 }
2145 else {
Christopher Faulet421e7692019-06-13 11:16:45 +02002146 c_adv(res, htx->data - co_data(res));
Christopher Faulet66af0b22019-03-22 14:54:52 +01002147 if (msg->flags & HTTP_MSGF_XFER_LEN)
2148 channel_htx_forward_forever(res, htx);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002149 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002150
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002151 if (htx->data != co_data(res))
2152 goto missing_data_or_waiting;
2153
2154 if (!(msg->flags & HTTP_MSGF_XFER_LEN) && res->flags & CF_SHUTR) {
2155 msg->msg_state = HTTP_MSG_ENDING;
2156 goto ending;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002157 }
2158
Christopher Faulet9768c262018-10-22 09:34:31 +02002159 /* Check if the end-of-message is reached and if so, switch the message
Christopher Fauletd20fdb02019-06-13 16:43:22 +02002160 * in HTTP_MSG_ENDING state. Then if all data was marked to be
2161 * forwarded, set the state to HTTP_MSG_DONE.
Christopher Faulet9768c262018-10-22 09:34:31 +02002162 */
2163 if (htx_get_tail_type(htx) != HTX_BLK_EOM)
2164 goto missing_data_or_waiting;
2165
Christopher Fauletd20fdb02019-06-13 16:43:22 +02002166 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet9768c262018-10-22 09:34:31 +02002167
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002168 ending:
Christopher Faulet2151cdd2020-07-22 16:34:59 +02002169 res->flags &= ~CF_EXPECT_MORE; /* no more data are expected */
2170
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002171 /* other states, ENDING...TUNNEL */
2172 if (msg->msg_state >= HTTP_MSG_DONE)
2173 goto done;
Christopher Faulet9768c262018-10-22 09:34:31 +02002174
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002175 if (HAS_RSP_DATA_FILTERS(s)) {
2176 ret = flt_http_end(s, msg);
2177 if (ret <= 0) {
2178 if (!ret)
2179 goto missing_data_or_waiting;
2180 goto return_bad_res;
2181 }
2182 }
2183
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002184 if ((txn->meth == HTTP_METH_CONNECT && txn->status == 200) || txn->status == 101 ||
2185 !(msg->flags & HTTP_MSGF_XFER_LEN)) {
2186 msg->msg_state = HTTP_MSG_TUNNEL;
2187 goto ending;
2188 }
2189 else {
2190 msg->msg_state = HTTP_MSG_DONE;
2191 res->to_forward = 0;
2192 }
2193
2194 done:
2195
2196 channel_dont_close(res);
2197
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002198 http_end_response(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002199 if (!(res->analysers & an_bit)) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002200 http_end_request(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002201 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
2202 if (res->flags & CF_SHUTW) {
2203 /* response errors are most likely due to the
2204 * client aborting the transfer. */
Christopher Faulet93e02d82019-03-08 14:18:50 +01002205 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002206 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02002207 goto return_bad_res;
2208 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002209 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002210 return 1;
2211 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002212 DBG_TRACE_DEVEL("waiting for the end of the HTTP txn",
2213 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002214 return 0;
2215
2216 missing_data_or_waiting:
2217 if (res->flags & CF_SHUTW)
Christopher Faulet93e02d82019-03-08 14:18:50 +01002218 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002219
2220 /* stop waiting for data if the input is closed before the end. If the
2221 * client side was already closed, it means that the client has aborted,
2222 * so we don't want to count this as a server abort. Otherwise it's a
2223 * server abort.
2224 */
Christopher Fauletd20fdb02019-06-13 16:43:22 +02002225 if (msg->msg_state < HTTP_MSG_ENDING && res->flags & CF_SHUTR) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02002226 if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))
Christopher Faulet93e02d82019-03-08 14:18:50 +01002227 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002228 /* If we have some pending data, we continue the processing */
Christopher Faulet93e02d82019-03-08 14:18:50 +01002229 if (htx_is_empty(htx))
2230 goto return_srv_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002231 }
2232
Christopher Faulete0768eb2018-10-03 16:38:02 +02002233 /* When TE: chunked is used, we need to get there again to parse
2234 * remaining chunks even if the server has closed, so we don't want to
Christopher Faulet9768c262018-10-22 09:34:31 +02002235 * set CF_DONTCLOSE. Similarly when there is a content-leng or if there
2236 * are filters registered on the stream, we don't want to forward a
2237 * close
Christopher Faulete0768eb2018-10-03 16:38:02 +02002238 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002239 if ((msg->flags & HTTP_MSGF_XFER_LEN) || HAS_RSP_DATA_FILTERS(s))
Christopher Faulete0768eb2018-10-03 16:38:02 +02002240 channel_dont_close(res);
2241
2242 /* We know that more data are expected, but we couldn't send more that
2243 * what we did. So we always set the CF_EXPECT_MORE flag so that the
2244 * system knows it must not set a PUSH on this first part. Interactive
2245 * modes are already handled by the stream sock layer. We must not do
2246 * this in content-length mode because it could present the MSG_MORE
2247 * flag with the last block of forwarded data, which would cause an
2248 * additional delay to be observed by the receiver.
2249 */
Christopher Faulet2151cdd2020-07-22 16:34:59 +02002250 if (HAS_RSP_DATA_FILTERS(s))
Christopher Faulete0768eb2018-10-03 16:38:02 +02002251 res->flags |= CF_EXPECT_MORE;
2252
2253 /* the stream handler will take care of timeouts and errors */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002254 DBG_TRACE_DEVEL("waiting for more data to forward",
2255 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002256 return 0;
2257
Christopher Faulet93e02d82019-03-08 14:18:50 +01002258 return_srv_abort:
2259 _HA_ATOMIC_ADD(&sess->fe->fe_counters.srv_aborts, 1);
2260 _HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01002261 if (sess->listener->counters)
2262 _HA_ATOMIC_ADD(&sess->listener->counters->srv_aborts, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002263 if (objt_server(s->target))
Christopher Fauletcff0f732019-12-16 16:13:44 +01002264 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.srv_aborts, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002265 if (!(s->flags & SF_ERR_MASK))
2266 s->flags |= SF_ERR_SRVCL;
2267 goto return_error;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002268
Christopher Faulet93e02d82019-03-08 14:18:50 +01002269 return_cli_abort:
2270 _HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
2271 _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01002272 if (sess->listener->counters)
2273 _HA_ATOMIC_ADD(&sess->listener->counters->cli_aborts, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002274 if (objt_server(s->target))
Christopher Fauletcff0f732019-12-16 16:13:44 +01002275 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.cli_aborts, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002276 if (!(s->flags & SF_ERR_MASK))
2277 s->flags |= SF_ERR_CLICL;
2278 goto return_error;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002279
Christopher Fauletb9a92f32019-09-09 10:15:21 +02002280 return_int_err:
Christopher Fauletcff0f732019-12-16 16:13:44 +01002281 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002282 _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01002283 if (sess->listener->counters)
2284 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002285 if (objt_server(s->target))
2286 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.internal_errors, 1);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02002287 if (!(s->flags & SF_ERR_MASK))
2288 s->flags |= SF_ERR_INTERNAL;
2289 goto return_error;
2290
Christopher Faulet93e02d82019-03-08 14:18:50 +01002291 return_bad_res:
2292 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
2293 if (objt_server(s->target)) {
Christopher Fauletcff0f732019-12-16 16:13:44 +01002294 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002295 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_RSP);
2296 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02002297 if (!(s->flags & SF_ERR_MASK))
Christopher Faulet93e02d82019-03-08 14:18:50 +01002298 s->flags |= SF_ERR_SRVCL;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002299 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +02002300
Christopher Faulet93e02d82019-03-08 14:18:50 +01002301 return_error:
Christopher Faulete0768eb2018-10-03 16:38:02 +02002302 /* don't send any error message as we're in the body */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002303 http_reply_and_close(s, txn->status, NULL);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002304 res->analysers &= AN_RES_FLT_END;
2305 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 +02002306 if (!(s->flags & SF_FINST_MASK))
2307 s->flags |= SF_FINST_D;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002308 DBG_TRACE_DEVEL("leaving on error",
2309 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002310 return 0;
2311}
2312
Christopher Fauletf2824e62018-10-01 12:12:37 +02002313/* Perform an HTTP redirect based on the information in <rule>. The function
Christopher Faulet99daf282018-11-28 22:58:13 +01002314 * returns zero on success, or zero in case of a, irrecoverable error such
Christopher Fauletf2824e62018-10-01 12:12:37 +02002315 * as too large a request to build a valid response.
2316 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002317int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn)
Christopher Fauletf2824e62018-10-01 12:12:37 +02002318{
Christopher Faulet99daf282018-11-28 22:58:13 +01002319 struct channel *req = &s->req;
2320 struct channel *res = &s->res;
2321 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01002322 struct htx_sl *sl;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002323 struct buffer *chunk;
Christopher Faulet99daf282018-11-28 22:58:13 +01002324 struct ist status, reason, location;
2325 unsigned int flags;
Christopher Faulet08e66462019-05-23 16:44:59 +02002326 int close = 0; /* Try to keep the connection alive byt default */
Christopher Fauletf2824e62018-10-01 12:12:37 +02002327
2328 chunk = alloc_trash_chunk();
Christopher Fauletb8a53712019-12-16 11:29:38 +01002329 if (!chunk) {
2330 if (!(s->flags & SF_ERR_MASK))
2331 s->flags |= SF_ERR_RESOURCE;
Christopher Faulet99daf282018-11-28 22:58:13 +01002332 goto fail;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002333 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002334
Christopher Faulet99daf282018-11-28 22:58:13 +01002335 /*
2336 * Create the location
2337 */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002338 htx = htxbuf(&req->buf);
Christopher Fauletf2824e62018-10-01 12:12:37 +02002339 switch(rule->type) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002340 case REDIRECT_TYPE_SCHEME: {
2341 struct http_hdr_ctx ctx;
2342 struct ist path, host;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002343
Christopher Faulet99daf282018-11-28 22:58:13 +01002344 host = ist("");
2345 ctx.blk = NULL;
2346 if (http_find_header(htx, ist("Host"), &ctx, 0))
2347 host = ctx.value;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002348
Christopher Faulet297fbb42019-05-13 14:41:27 +02002349 sl = http_get_stline(htx);
Christopher Faulet99daf282018-11-28 22:58:13 +01002350 path = http_get_path(htx_sl_req_uri(sl));
2351 /* build message using path */
Tim Duesterhused526372020-03-05 17:56:33 +01002352 if (isttest(path)) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002353 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2354 int qs = 0;
2355 while (qs < path.len) {
2356 if (*(path.ptr + qs) == '?') {
2357 path.len = qs;
2358 break;
2359 }
2360 qs++;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002361 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002362 }
2363 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002364 else
2365 path = ist("/");
Christopher Fauletf2824e62018-10-01 12:12:37 +02002366
Christopher Faulet99daf282018-11-28 22:58:13 +01002367 if (rule->rdr_str) { /* this is an old "redirect" rule */
2368 /* add scheme */
2369 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2370 goto fail;
2371 }
2372 else {
2373 /* add scheme with executing log format */
2374 chunk->data += build_logline(s, chunk->area + chunk->data,
2375 chunk->size - chunk->data,
2376 &rule->rdr_fmt);
2377 }
2378 /* add "://" + host + path */
2379 if (!chunk_memcat(chunk, "://", 3) ||
2380 !chunk_memcat(chunk, host.ptr, host.len) ||
2381 !chunk_memcat(chunk, path.ptr, path.len))
2382 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002383
Christopher Faulet99daf282018-11-28 22:58:13 +01002384 /* append a slash at the end of the location if needed and missing */
2385 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
2386 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
2387 if (chunk->data + 1 >= chunk->size)
2388 goto fail;
2389 chunk->area[chunk->data++] = '/';
2390 }
2391 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002392 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002393
Christopher Faulet99daf282018-11-28 22:58:13 +01002394 case REDIRECT_TYPE_PREFIX: {
2395 struct ist path;
2396
Christopher Faulet297fbb42019-05-13 14:41:27 +02002397 sl = http_get_stline(htx);
Christopher Faulet99daf282018-11-28 22:58:13 +01002398 path = http_get_path(htx_sl_req_uri(sl));
2399 /* build message using path */
Tim Duesterhused526372020-03-05 17:56:33 +01002400 if (isttest(path)) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002401 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2402 int qs = 0;
2403 while (qs < path.len) {
2404 if (*(path.ptr + qs) == '?') {
2405 path.len = qs;
2406 break;
2407 }
2408 qs++;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002409 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002410 }
2411 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002412 else
2413 path = ist("/");
Christopher Fauletf2824e62018-10-01 12:12:37 +02002414
Christopher Faulet99daf282018-11-28 22:58:13 +01002415 if (rule->rdr_str) { /* this is an old "redirect" rule */
2416 /* add prefix. Note that if prefix == "/", we don't want to
2417 * add anything, otherwise it makes it hard for the user to
2418 * configure a self-redirection.
2419 */
2420 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
2421 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2422 goto fail;
2423 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002424 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002425 else {
2426 /* add prefix with executing log format */
2427 chunk->data += build_logline(s, chunk->area + chunk->data,
2428 chunk->size - chunk->data,
2429 &rule->rdr_fmt);
2430 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002431
Christopher Faulet99daf282018-11-28 22:58:13 +01002432 /* add path */
2433 if (!chunk_memcat(chunk, path.ptr, path.len))
2434 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002435
Christopher Faulet99daf282018-11-28 22:58:13 +01002436 /* append a slash at the end of the location if needed and missing */
2437 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
2438 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
2439 if (chunk->data + 1 >= chunk->size)
2440 goto fail;
2441 chunk->area[chunk->data++] = '/';
2442 }
2443 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002444 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002445 case REDIRECT_TYPE_LOCATION:
2446 default:
2447 if (rule->rdr_str) { /* this is an old "redirect" rule */
2448 /* add location */
2449 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2450 goto fail;
2451 }
2452 else {
2453 /* add location with executing log format */
2454 chunk->data += build_logline(s, chunk->area + chunk->data,
2455 chunk->size - chunk->data,
2456 &rule->rdr_fmt);
2457 }
2458 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002459 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002460 location = ist2(chunk->area, chunk->data);
2461
2462 /*
2463 * Create the 30x response
2464 */
2465 switch (rule->code) {
2466 case 308:
2467 status = ist("308");
2468 reason = ist("Permanent Redirect");
2469 break;
2470 case 307:
2471 status = ist("307");
2472 reason = ist("Temporary Redirect");
2473 break;
2474 case 303:
2475 status = ist("303");
2476 reason = ist("See Other");
2477 break;
2478 case 301:
2479 status = ist("301");
2480 reason = ist("Moved Permanently");
2481 break;
2482 case 302:
2483 default:
2484 status = ist("302");
2485 reason = ist("Found");
2486 break;
2487 }
2488
Christopher Faulet08e66462019-05-23 16:44:59 +02002489 if (!(txn->req.flags & HTTP_MSGF_BODYLESS) && txn->req.msg_state != HTTP_MSG_DONE)
2490 close = 1;
2491
Christopher Faulet99daf282018-11-28 22:58:13 +01002492 htx = htx_from_buf(&res->buf);
Kevin Zhu96b36392020-01-07 09:42:55 +01002493 /* Trim any possible response */
2494 channel_htx_truncate(&s->res, htx);
Christopher Faulet99daf282018-11-28 22:58:13 +01002495 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
2496 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), status, reason);
2497 if (!sl)
2498 goto fail;
2499 sl->info.res.status = rule->code;
2500 s->txn->status = rule->code;
2501
Christopher Faulet08e66462019-05-23 16:44:59 +02002502 if (close && !htx_add_header(htx, ist("Connection"), ist("close")))
2503 goto fail;
2504
2505 if (!htx_add_header(htx, ist("Content-length"), ist("0")) ||
Christopher Faulet99daf282018-11-28 22:58:13 +01002506 !htx_add_header(htx, ist("Location"), location))
2507 goto fail;
2508
2509 if (rule->code == 302 || rule->code == 303 || rule->code == 307) {
2510 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")))
2511 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002512 }
2513
2514 if (rule->cookie_len) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002515 if (!htx_add_header(htx, ist("Set-Cookie"), ist2(rule->cookie_str, rule->cookie_len)))
2516 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002517 }
2518
Christopher Faulet99daf282018-11-28 22:58:13 +01002519 if (!htx_add_endof(htx, HTX_BLK_EOH) || !htx_add_endof(htx, HTX_BLK_EOM))
2520 goto fail;
2521
Kevin Zhu96b36392020-01-07 09:42:55 +01002522 htx_to_buf(htx, &res->buf);
Christopher Fauleta72a7e42020-01-28 09:28:11 +01002523 if (!http_forward_proxy_resp(s, 1))
2524 goto fail;
Christopher Faulet99daf282018-11-28 22:58:13 +01002525
Christopher Faulet60b33a52020-01-28 09:18:10 +01002526 if (rule->flags & REDIRECT_FLAG_FROM_REQ) {
2527 /* let's log the request time */
2528 s->logs.tv_request = now;
2529 req->analysers &= AN_REQ_FLT_END;
Christopher Faulet99daf282018-11-28 22:58:13 +01002530
Christopher Faulet60b33a52020-01-28 09:18:10 +01002531 if (s->sess->fe == s->be) /* report it if the request was intercepted by the frontend */
2532 _HA_ATOMIC_ADD(&s->sess->fe->fe_counters.intercepted_req, 1);
2533 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002534
2535 if (!(s->flags & SF_ERR_MASK))
2536 s->flags |= SF_ERR_LOCAL;
2537 if (!(s->flags & SF_FINST_MASK))
Christopher Faulet60b33a52020-01-28 09:18:10 +01002538 s->flags |= ((rule->flags & REDIRECT_FLAG_FROM_REQ) ? SF_FINST_R : SF_FINST_H);
Christopher Fauletf2824e62018-10-01 12:12:37 +02002539
Christopher Faulet99daf282018-11-28 22:58:13 +01002540 free_trash_chunk(chunk);
2541 return 1;
2542
2543 fail:
2544 /* If an error occurred, remove the incomplete HTTP response from the
2545 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01002546 channel_htx_truncate(res, htxbuf(&res->buf));
Christopher Fauletf2824e62018-10-01 12:12:37 +02002547 free_trash_chunk(chunk);
Christopher Faulet99daf282018-11-28 22:58:13 +01002548 return 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002549}
2550
Christopher Faulet92d34fe2019-12-17 09:20:34 +01002551/* Replace all headers matching the name <name>. The header value is replaced if
2552 * it matches the regex <re>. <str> is used for the replacement. If <full> is
2553 * set to 1, the full-line is matched and replaced. Otherwise, comma-separated
2554 * values are evaluated one by one. It returns 0 on success and -1 on error.
2555 */
2556int http_replace_hdrs(struct stream* s, struct htx *htx, struct ist name,
2557 const char *str, struct my_regex *re, int full)
Christopher Faulet72333522018-10-24 11:25:02 +02002558{
2559 struct http_hdr_ctx ctx;
2560 struct buffer *output = get_trash_chunk();
2561
Christopher Faulet72333522018-10-24 11:25:02 +02002562 ctx.blk = NULL;
Christopher Faulet92d34fe2019-12-17 09:20:34 +01002563 while (http_find_header(htx, name, &ctx, full)) {
Christopher Faulet72333522018-10-24 11:25:02 +02002564 if (!regex_exec_match2(re, ctx.value.ptr, ctx.value.len, MAX_MATCH, pmatch, 0))
2565 continue;
2566
2567 output->data = exp_replace(output->area, output->size, ctx.value.ptr, str, pmatch);
2568 if (output->data == -1)
2569 return -1;
2570 if (!http_replace_header_value(htx, &ctx, ist2(output->area, output->data)))
2571 return -1;
2572 }
2573 return 0;
2574}
2575
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002576/* This function executes one of the set-{method,path,query,uri} actions. It
2577 * takes the string from the variable 'replace' with length 'len', then modifies
2578 * the relevant part of the request line accordingly. Then it updates various
2579 * pointers to the next elements which were moved, and the total buffer length.
2580 * It finds the action to be performed in p[2], previously filled by function
2581 * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal
2582 * error, though this can be revisited when this code is finally exploited.
2583 *
2584 * 'action' can be '0' to replace method, '1' to replace path, '2' to replace
Christopher Faulet312294f2020-09-02 17:17:44 +02002585 * query string, 3 to replace uri or 4 to replace the path+query.
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002586 *
2587 * In query string case, the mark question '?' must be set at the start of the
2588 * string by the caller, event if the replacement query string is empty.
2589 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002590int http_req_replace_stline(int action, const char *replace, int len,
2591 struct proxy *px, struct stream *s)
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002592{
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002593 struct htx *htx = htxbuf(&s->req.buf);
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002594
2595 switch (action) {
2596 case 0: // method
2597 if (!http_replace_req_meth(htx, ist2(replace, len)))
2598 return -1;
2599 break;
2600
2601 case 1: // path
Christopher Fauletb8ce5052020-08-31 16:11:57 +02002602 if (!http_replace_req_path(htx, ist2(replace, len), 0))
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002603 return -1;
2604 break;
2605
2606 case 2: // query
2607 if (!http_replace_req_query(htx, ist2(replace, len)))
2608 return -1;
2609 break;
2610
2611 case 3: // uri
2612 if (!http_replace_req_uri(htx, ist2(replace, len)))
2613 return -1;
2614 break;
2615
Christopher Faulet312294f2020-09-02 17:17:44 +02002616 case 4: // path + query
2617 if (!http_replace_req_path(htx, ist2(replace, len), 1))
2618 return -1;
2619 break;
2620
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002621 default:
2622 return -1;
2623 }
2624 return 0;
2625}
2626
2627/* This function replace the HTTP status code and the associated message. The
Christopher Faulete00d06c2019-12-16 17:18:42 +01002628 * variable <status> contains the new status code. This function never fails. It
2629 * returns 0 in case of success, -1 in case of internal error.
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002630 */
Christopher Faulet96bff762019-12-17 13:46:18 +01002631int http_res_set_status(unsigned int status, struct ist reason, struct stream *s)
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002632{
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002633 struct htx *htx = htxbuf(&s->res.buf);
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002634 char *res;
2635
2636 chunk_reset(&trash);
2637 res = ultoa_o(status, trash.area, trash.size);
2638 trash.data = res - trash.area;
2639
2640 /* Do we have a custom reason format string? */
Tim Duesterhuse296d3e2020-03-05 17:56:31 +01002641 if (!isttest(reason)) {
Christopher Faulet96bff762019-12-17 13:46:18 +01002642 const char *str = http_get_reason(status);
2643 reason = ist2(str, strlen(str));
2644 }
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002645
Christopher Fauletbde2c4c2020-08-31 16:43:34 +02002646 if (!http_replace_res_status(htx, ist2(trash.area, trash.data), reason))
Christopher Faulete00d06c2019-12-16 17:18:42 +01002647 return -1;
2648 return 0;
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002649}
2650
Christopher Faulet3e964192018-10-24 11:39:23 +02002651/* Executes the http-request rules <rules> for stream <s>, proxy <px> and
2652 * transaction <txn>. Returns the verdict of the first rule that prevents
2653 * further processing of the request (auth, deny, ...), and defaults to
2654 * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or
2655 * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT
2656 * on txn->flags if it encounters a tarpit rule. If <deny_status> is not NULL
2657 * and a deny/tarpit rule is matched, it will be filled with this rule's deny
2658 * status.
2659 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002660static enum rule_result http_req_get_intercept_rule(struct proxy *px, struct list *rules,
Christopher Fauletb58f62b2020-01-13 16:40:13 +01002661 struct stream *s)
Christopher Faulet3e964192018-10-24 11:39:23 +02002662{
2663 struct session *sess = strm_sess(s);
2664 struct http_txn *txn = s->txn;
Christopher Faulet3e964192018-10-24 11:39:23 +02002665 struct act_rule *rule;
Christopher Faulet3e964192018-10-24 11:39:23 +02002666 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002667 int act_opts = 0;
Christopher Faulet3e964192018-10-24 11:39:23 +02002668
Christopher Faulet3e964192018-10-24 11:39:23 +02002669 /* If "the current_rule_list" match the executed rule list, we are in
2670 * resume condition. If a resume is needed it is always in the action
2671 * and never in the ACL or converters. In this case, we initialise the
2672 * current rule, and go to the action execution point.
2673 */
2674 if (s->current_rule) {
2675 rule = s->current_rule;
2676 s->current_rule = NULL;
2677 if (s->current_rule_list == rules)
2678 goto resume_execution;
2679 }
2680 s->current_rule_list = rules;
2681
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002682 /* start the ruleset evaluation in strict mode */
2683 txn->req.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01002684
Christopher Faulet3e964192018-10-24 11:39:23 +02002685 list_for_each_entry(rule, rules, list) {
2686 /* check optional condition */
2687 if (rule->cond) {
2688 int ret;
2689
2690 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
2691 ret = acl_pass(ret);
2692
2693 if (rule->cond->pol == ACL_COND_UNLESS)
2694 ret = !ret;
2695
2696 if (!ret) /* condition not matched */
2697 continue;
2698 }
2699
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002700 act_opts |= ACT_OPT_FIRST;
Christopher Faulet3e964192018-10-24 11:39:23 +02002701 resume_execution:
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002702 /* Always call the action function if defined */
2703 if (rule->action_ptr) {
2704 if ((s->req.flags & CF_READ_ERROR) ||
2705 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
2706 (px->options & PR_O_ABRT_CLOSE)))
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002707 act_opts |= ACT_OPT_FINAL;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002708
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002709 switch (rule->action_ptr(rule, px, sess, s, act_opts)) {
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002710 case ACT_RET_CONT:
2711 break;
2712 case ACT_RET_STOP:
2713 rule_ret = HTTP_RULE_RES_STOP;
2714 goto end;
2715 case ACT_RET_YIELD:
2716 s->current_rule = rule;
2717 rule_ret = HTTP_RULE_RES_YIELD;
2718 goto end;
2719 case ACT_RET_ERR:
2720 rule_ret = HTTP_RULE_RES_ERROR;
2721 goto end;
2722 case ACT_RET_DONE:
2723 rule_ret = HTTP_RULE_RES_DONE;
2724 goto end;
2725 case ACT_RET_DENY:
Christopher Fauletb58f62b2020-01-13 16:40:13 +01002726 if (txn->status == -1)
2727 txn->status = 403;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002728 rule_ret = HTTP_RULE_RES_DENY;
2729 goto end;
2730 case ACT_RET_ABRT:
2731 rule_ret = HTTP_RULE_RES_ABRT;
2732 goto end;
2733 case ACT_RET_INV:
2734 rule_ret = HTTP_RULE_RES_BADREQ;
2735 goto end;
2736 }
2737 continue; /* eval the next rule */
2738 }
2739
2740 /* If not action function defined, check for known actions */
Christopher Faulet3e964192018-10-24 11:39:23 +02002741 switch (rule->action) {
2742 case ACT_ACTION_ALLOW:
2743 rule_ret = HTTP_RULE_RES_STOP;
2744 goto end;
2745
2746 case ACT_ACTION_DENY:
Christopher Faulet5cb513a2020-05-13 17:56:56 +02002747 txn->status = rule->arg.http_reply->status;
2748 txn->http_reply = rule->arg.http_reply;
Christopher Faulet3e964192018-10-24 11:39:23 +02002749 rule_ret = HTTP_RULE_RES_DENY;
2750 goto end;
2751
2752 case ACT_HTTP_REQ_TARPIT:
2753 txn->flags |= TX_CLTARPIT;
Christopher Faulet5cb513a2020-05-13 17:56:56 +02002754 txn->status = rule->arg.http_reply->status;
2755 txn->http_reply = rule->arg.http_reply;
Christopher Faulet3e964192018-10-24 11:39:23 +02002756 rule_ret = HTTP_RULE_RES_DENY;
2757 goto end;
2758
Christopher Faulet3e964192018-10-24 11:39:23 +02002759 case ACT_HTTP_REDIR:
Christopher Faulet90d22a82020-03-06 11:18:39 +01002760 rule_ret = HTTP_RULE_RES_ABRT;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002761 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
Christopher Faulet3a26bee2019-12-16 12:47:40 +01002762 rule_ret = HTTP_RULE_RES_ERROR;
Christopher Faulet3e964192018-10-24 11:39:23 +02002763 goto end;
2764
2765 case ACT_HTTP_SET_NICE:
Christopher Faulet96bff762019-12-17 13:46:18 +01002766 s->task->nice = rule->arg.http.i;
Christopher Faulet3e964192018-10-24 11:39:23 +02002767 break;
2768
2769 case ACT_HTTP_SET_TOS:
Christopher Faulet96bff762019-12-17 13:46:18 +01002770 conn_set_tos(objt_conn(sess->origin), rule->arg.http.i);
Christopher Faulet3e964192018-10-24 11:39:23 +02002771 break;
2772
2773 case ACT_HTTP_SET_MARK:
Christopher Faulet96bff762019-12-17 13:46:18 +01002774 conn_set_mark(objt_conn(sess->origin), rule->arg.http.i);
Christopher Faulet3e964192018-10-24 11:39:23 +02002775 break;
2776
2777 case ACT_HTTP_SET_LOGL:
Christopher Faulet96bff762019-12-17 13:46:18 +01002778 s->logs.level = rule->arg.http.i;
Christopher Faulet3e964192018-10-24 11:39:23 +02002779 break;
2780
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002781 /* other flags exists, but normally, they never be matched. */
Christopher Faulet3e964192018-10-24 11:39:23 +02002782 default:
2783 break;
2784 }
2785 }
2786
2787 end:
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002788 /* if the ruleset evaluation is finished reset the strict mode */
Christopher Faulet46f95542019-12-20 10:07:22 +01002789 if (rule_ret != HTTP_RULE_RES_YIELD)
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002790 txn->req.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01002791
Christopher Faulet3e964192018-10-24 11:39:23 +02002792 /* we reached the end of the rules, nothing to report */
2793 return rule_ret;
2794}
2795
2796/* Executes the http-response rules <rules> for stream <s> and proxy <px>. It
2797 * returns one of 5 possible statuses: HTTP_RULE_RES_CONT, HTTP_RULE_RES_STOP,
2798 * HTTP_RULE_RES_DONE, HTTP_RULE_RES_YIELD, or HTTP_RULE_RES_BADREQ. If *CONT
2799 * is returned, the process can continue the evaluation of next rule list. If
2800 * *STOP or *DONE is returned, the process must stop the evaluation. If *BADREQ
2801 * is returned, it means the operation could not be processed and a server error
Christopher Fauleta53abad2020-05-13 08:12:22 +02002802 * must be returned. If *YIELD is returned, the caller must call again the
2803 * function with the same context.
Christopher Faulet3e964192018-10-24 11:39:23 +02002804 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002805static enum rule_result http_res_get_intercept_rule(struct proxy *px, struct list *rules,
2806 struct stream *s)
Christopher Faulet3e964192018-10-24 11:39:23 +02002807{
2808 struct session *sess = strm_sess(s);
2809 struct http_txn *txn = s->txn;
Christopher Faulet3e964192018-10-24 11:39:23 +02002810 struct act_rule *rule;
Christopher Faulet3e964192018-10-24 11:39:23 +02002811 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002812 int act_opts = 0;
Christopher Faulet3e964192018-10-24 11:39:23 +02002813
Christopher Faulet3e964192018-10-24 11:39:23 +02002814 /* If "the current_rule_list" match the executed rule list, we are in
2815 * resume condition. If a resume is needed it is always in the action
2816 * and never in the ACL or converters. In this case, we initialise the
2817 * current rule, and go to the action execution point.
2818 */
2819 if (s->current_rule) {
2820 rule = s->current_rule;
2821 s->current_rule = NULL;
2822 if (s->current_rule_list == rules)
2823 goto resume_execution;
2824 }
2825 s->current_rule_list = rules;
2826
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002827 /* start the ruleset evaluation in strict mode */
2828 txn->rsp.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01002829
Christopher Faulet3e964192018-10-24 11:39:23 +02002830 list_for_each_entry(rule, rules, list) {
2831 /* check optional condition */
2832 if (rule->cond) {
2833 int ret;
2834
2835 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
2836 ret = acl_pass(ret);
2837
2838 if (rule->cond->pol == ACL_COND_UNLESS)
2839 ret = !ret;
2840
2841 if (!ret) /* condition not matched */
2842 continue;
2843 }
2844
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002845 act_opts |= ACT_OPT_FIRST;
Christopher Faulet3e964192018-10-24 11:39:23 +02002846resume_execution:
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002847
2848 /* Always call the action function if defined */
2849 if (rule->action_ptr) {
2850 if ((s->req.flags & CF_READ_ERROR) ||
2851 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
2852 (px->options & PR_O_ABRT_CLOSE)))
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002853 act_opts |= ACT_OPT_FINAL;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002854
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002855 switch (rule->action_ptr(rule, px, sess, s, act_opts)) {
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002856 case ACT_RET_CONT:
2857 break;
2858 case ACT_RET_STOP:
2859 rule_ret = HTTP_RULE_RES_STOP;
2860 goto end;
2861 case ACT_RET_YIELD:
2862 s->current_rule = rule;
2863 rule_ret = HTTP_RULE_RES_YIELD;
2864 goto end;
2865 case ACT_RET_ERR:
2866 rule_ret = HTTP_RULE_RES_ERROR;
2867 goto end;
2868 case ACT_RET_DONE:
2869 rule_ret = HTTP_RULE_RES_DONE;
2870 goto end;
2871 case ACT_RET_DENY:
Christopher Fauletb58f62b2020-01-13 16:40:13 +01002872 if (txn->status == -1)
2873 txn->status = 502;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002874 rule_ret = HTTP_RULE_RES_DENY;
2875 goto end;
2876 case ACT_RET_ABRT:
2877 rule_ret = HTTP_RULE_RES_ABRT;
2878 goto end;
2879 case ACT_RET_INV:
2880 rule_ret = HTTP_RULE_RES_BADREQ;
2881 goto end;
2882 }
2883 continue; /* eval the next rule */
2884 }
2885
2886 /* If not action function defined, check for known actions */
Christopher Faulet3e964192018-10-24 11:39:23 +02002887 switch (rule->action) {
2888 case ACT_ACTION_ALLOW:
2889 rule_ret = HTTP_RULE_RES_STOP; /* "allow" rules are OK */
2890 goto end;
2891
2892 case ACT_ACTION_DENY:
Christopher Faulet5cb513a2020-05-13 17:56:56 +02002893 txn->status = rule->arg.http_reply->status;
2894 txn->http_reply = rule->arg.http_reply;
Christopher Faulet3a26bee2019-12-16 12:47:40 +01002895 rule_ret = HTTP_RULE_RES_DENY;
Christopher Faulet3e964192018-10-24 11:39:23 +02002896 goto end;
2897
2898 case ACT_HTTP_SET_NICE:
Christopher Faulet96bff762019-12-17 13:46:18 +01002899 s->task->nice = rule->arg.http.i;
Christopher Faulet3e964192018-10-24 11:39:23 +02002900 break;
2901
2902 case ACT_HTTP_SET_TOS:
Christopher Faulet96bff762019-12-17 13:46:18 +01002903 conn_set_tos(objt_conn(sess->origin), rule->arg.http.i);
Christopher Faulet3e964192018-10-24 11:39:23 +02002904 break;
2905
2906 case ACT_HTTP_SET_MARK:
Christopher Faulet96bff762019-12-17 13:46:18 +01002907 conn_set_mark(objt_conn(sess->origin), rule->arg.http.i);
Christopher Faulet3e964192018-10-24 11:39:23 +02002908 break;
2909
2910 case ACT_HTTP_SET_LOGL:
Christopher Faulet96bff762019-12-17 13:46:18 +01002911 s->logs.level = rule->arg.http.i;
Christopher Faulet3e964192018-10-24 11:39:23 +02002912 break;
2913
Christopher Faulet3e964192018-10-24 11:39:23 +02002914 case ACT_HTTP_REDIR:
Christopher Faulet49c2a702020-03-06 15:44:37 +01002915 rule_ret = HTTP_RULE_RES_ABRT;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002916 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
Christopher Faulet3a26bee2019-12-16 12:47:40 +01002917 rule_ret = HTTP_RULE_RES_ERROR;
Christopher Faulet3e964192018-10-24 11:39:23 +02002918 goto end;
2919
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002920 /* other flags exists, but normally, they never be matched. */
Christopher Faulet3e964192018-10-24 11:39:23 +02002921 default:
2922 break;
2923 }
2924 }
2925
2926 end:
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002927 /* if the ruleset evaluation is finished reset the strict mode */
Christopher Faulet46f95542019-12-20 10:07:22 +01002928 if (rule_ret != HTTP_RULE_RES_YIELD)
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002929 txn->rsp.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01002930
Christopher Faulet3e964192018-10-24 11:39:23 +02002931 /* we reached the end of the rules, nothing to report */
2932 return rule_ret;
2933}
2934
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002935/* Executes backend and frontend http-after-response rules for the stream <s>,
2936 * in that order. it return 1 on success and 0 on error. It is the caller
2937 * responsibility to catch error or ignore it. If it catches it, this function
2938 * may be called a second time, for the internal error.
2939 */
2940int http_eval_after_res_rules(struct stream *s)
2941{
2942 struct session *sess = s->sess;
2943 enum rule_result ret = HTTP_RULE_RES_CONT;
2944
Christopher Faulet507479b2020-05-15 12:29:46 +02002945 /* Eval after-response ruleset only if the reply is not const */
2946 if (s->txn->flags & TX_CONST_REPLY)
2947 goto end;
2948
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002949 /* prune the request variables if not already done and swap to the response variables. */
2950 if (s->vars_reqres.scope != SCOPE_RES) {
2951 if (!LIST_ISEMPTY(&s->vars_reqres.head))
2952 vars_prune(&s->vars_reqres, s->sess, s);
2953 vars_init(&s->vars_reqres, SCOPE_RES);
2954 }
2955
2956 ret = http_res_get_intercept_rule(s->be, &s->be->http_after_res_rules, s);
2957 if ((ret == HTTP_RULE_RES_CONT || ret == HTTP_RULE_RES_STOP) && sess->fe != s->be)
2958 ret = http_res_get_intercept_rule(sess->fe, &sess->fe->http_after_res_rules, s);
2959
Christopher Faulet507479b2020-05-15 12:29:46 +02002960 end:
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002961 /* All other codes than CONTINUE, STOP or DONE are forbidden */
2962 return (ret == HTTP_RULE_RES_CONT || ret == HTTP_RULE_RES_STOP || ret == HTTP_RULE_RES_DONE);
2963}
2964
Christopher Fauletfcda7c62018-10-24 11:56:22 +02002965/*
2966 * Manage client-side cookie. It can impact performance by about 2% so it is
2967 * desirable to call it only when needed. This code is quite complex because
2968 * of the multiple very crappy and ambiguous syntaxes we have to support. it
2969 * highly recommended not to touch this part without a good reason !
2970 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002971static void http_manage_client_side_cookies(struct stream *s, struct channel *req)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02002972{
2973 struct session *sess = s->sess;
2974 struct http_txn *txn = s->txn;
2975 struct htx *htx;
2976 struct http_hdr_ctx ctx;
2977 char *hdr_beg, *hdr_end, *del_from;
2978 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
2979 int preserve_hdr;
2980
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002981 htx = htxbuf(&req->buf);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02002982 ctx.blk = NULL;
2983 while (http_find_header(htx, ist("Cookie"), &ctx, 1)) {
Olivier Houchardf0f42382019-07-22 17:43:46 +02002984 int is_first = 1;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02002985 del_from = NULL; /* nothing to be deleted */
2986 preserve_hdr = 0; /* assume we may kill the whole header */
2987
2988 /* Now look for cookies. Conforming to RFC2109, we have to support
2989 * attributes whose name begin with a '$', and associate them with
2990 * the right cookie, if we want to delete this cookie.
2991 * So there are 3 cases for each cookie read :
2992 * 1) it's a special attribute, beginning with a '$' : ignore it.
2993 * 2) it's a server id cookie that we *MAY* want to delete : save
2994 * some pointers on it (last semi-colon, beginning of cookie...)
2995 * 3) it's an application cookie : we *MAY* have to delete a previous
2996 * "special" cookie.
2997 * At the end of loop, if a "special" cookie remains, we may have to
2998 * remove it. If no application cookie persists in the header, we
2999 * *MUST* delete it.
3000 *
3001 * Note: RFC2965 is unclear about the processing of spaces around
3002 * the equal sign in the ATTR=VALUE form. A careful inspection of
3003 * the RFC explicitly allows spaces before it, and not within the
3004 * tokens (attrs or values). An inspection of RFC2109 allows that
3005 * too but section 10.1.3 lets one think that spaces may be allowed
3006 * after the equal sign too, resulting in some (rare) buggy
3007 * implementations trying to do that. So let's do what servers do.
3008 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
3009 * allowed quoted strings in values, with any possible character
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003010 * after a backslash, including control chars and delimiters, which
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003011 * causes parsing to become ambiguous. Browsers also allow spaces
3012 * within values even without quotes.
3013 *
3014 * We have to keep multiple pointers in order to support cookie
3015 * removal at the beginning, middle or end of header without
3016 * corrupting the header. All of these headers are valid :
3017 *
3018 * hdr_beg hdr_end
3019 * | |
3020 * v |
3021 * NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3 |
3022 * NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3 v
3023 * NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3
3024 * | | | | | | |
3025 * | | | | | | |
3026 * | | | | | | +--> next
3027 * | | | | | +----> val_end
3028 * | | | | +-----------> val_beg
3029 * | | | +--------------> equal
3030 * | | +----------------> att_end
3031 * | +---------------------> att_beg
3032 * +--------------------------> prev
3033 *
3034 */
3035 hdr_beg = ctx.value.ptr;
3036 hdr_end = hdr_beg + ctx.value.len;
3037 for (prev = hdr_beg; prev < hdr_end; prev = next) {
3038 /* Iterate through all cookies on this line */
3039
3040 /* find att_beg */
3041 att_beg = prev;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003042 if (!is_first)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003043 att_beg++;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003044 is_first = 0;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003045
3046 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
3047 att_beg++;
3048
3049 /* find att_end : this is the first character after the last non
3050 * space before the equal. It may be equal to hdr_end.
3051 */
3052 equal = att_end = att_beg;
3053 while (equal < hdr_end) {
3054 if (*equal == '=' || *equal == ',' || *equal == ';')
3055 break;
3056 if (HTTP_IS_SPHT(*equal++))
3057 continue;
3058 att_end = equal;
3059 }
3060
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003061 /* here, <equal> points to '=', a delimiter or the end. <att_end>
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003062 * is between <att_beg> and <equal>, both may be identical.
3063 */
3064 /* look for end of cookie if there is an equal sign */
3065 if (equal < hdr_end && *equal == '=') {
3066 /* look for the beginning of the value */
3067 val_beg = equal + 1;
3068 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
3069 val_beg++;
3070
3071 /* find the end of the value, respecting quotes */
3072 next = http_find_cookie_value_end(val_beg, hdr_end);
3073
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003074 /* make val_end point to the first white space or delimiter after the value */
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003075 val_end = next;
3076 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
3077 val_end--;
3078 }
3079 else
3080 val_beg = val_end = next = equal;
3081
3082 /* We have nothing to do with attributes beginning with
3083 * '$'. However, they will automatically be removed if a
3084 * header before them is removed, since they're supposed
3085 * to be linked together.
3086 */
3087 if (*att_beg == '$')
3088 continue;
3089
3090 /* Ignore cookies with no equal sign */
3091 if (equal == next) {
3092 /* This is not our cookie, so we must preserve it. But if we already
3093 * scheduled another cookie for removal, we cannot remove the
3094 * complete header, but we can remove the previous block itself.
3095 */
3096 preserve_hdr = 1;
3097 if (del_from != NULL) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003098 int delta = http_del_hdr_value(hdr_beg, hdr_end, &del_from, prev);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003099 val_end += delta;
3100 next += delta;
3101 hdr_end += delta;
3102 prev = del_from;
3103 del_from = NULL;
3104 }
3105 continue;
3106 }
3107
3108 /* if there are spaces around the equal sign, we need to
3109 * strip them otherwise we'll get trouble for cookie captures,
3110 * or even for rewrites. Since this happens extremely rarely,
3111 * it does not hurt performance.
3112 */
3113 if (unlikely(att_end != equal || val_beg > equal + 1)) {
3114 int stripped_before = 0;
3115 int stripped_after = 0;
3116
3117 if (att_end != equal) {
3118 memmove(att_end, equal, hdr_end - equal);
3119 stripped_before = (att_end - equal);
3120 equal += stripped_before;
3121 val_beg += stripped_before;
3122 }
3123
3124 if (val_beg > equal + 1) {
3125 memmove(equal + 1, val_beg, hdr_end + stripped_before - val_beg);
3126 stripped_after = (equal + 1) - val_beg;
3127 val_beg += stripped_after;
3128 stripped_before += stripped_after;
3129 }
3130
3131 val_end += stripped_before;
3132 next += stripped_before;
3133 hdr_end += stripped_before;
3134 }
3135 /* now everything is as on the diagram above */
3136
3137 /* First, let's see if we want to capture this cookie. We check
3138 * that we don't already have a client side cookie, because we
3139 * can only capture one. Also as an optimisation, we ignore
3140 * cookies shorter than the declared name.
3141 */
3142 if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL &&
3143 (val_end - att_beg >= sess->fe->capture_namelen) &&
3144 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
3145 int log_len = val_end - att_beg;
3146
3147 if ((txn->cli_cookie = pool_alloc(pool_head_capture)) == NULL) {
3148 ha_alert("HTTP logging : out of memory.\n");
3149 } else {
3150 if (log_len > sess->fe->capture_len)
3151 log_len = sess->fe->capture_len;
3152 memcpy(txn->cli_cookie, att_beg, log_len);
3153 txn->cli_cookie[log_len] = 0;
3154 }
3155 }
3156
3157 /* Persistence cookies in passive, rewrite or insert mode have the
3158 * following form :
3159 *
3160 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
3161 *
3162 * For cookies in prefix mode, the form is :
3163 *
3164 * Cookie: NAME=SRV~VALUE
3165 */
3166 if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
3167 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
3168 struct server *srv = s->be->srv;
3169 char *delim;
3170
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003171 /* if we're in cookie prefix mode, we'll search the delimiter so that we
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003172 * have the server ID between val_beg and delim, and the original cookie between
3173 * delim+1 and val_end. Otherwise, delim==val_end :
3174 *
3175 * hdr_beg
3176 * |
3177 * v
3178 * NAME=SRV; # in all but prefix modes
3179 * NAME=SRV~OPAQUE ; # in prefix mode
3180 * || || | |+-> next
3181 * || || | +--> val_end
3182 * || || +---------> delim
3183 * || |+------------> val_beg
3184 * || +-------------> att_end = equal
3185 * |+-----------------> att_beg
3186 * +------------------> prev
3187 *
3188 */
3189 if (s->be->ck_opts & PR_CK_PFX) {
3190 for (delim = val_beg; delim < val_end; delim++)
3191 if (*delim == COOKIE_DELIM)
3192 break;
3193 }
3194 else {
3195 char *vbar1;
3196 delim = val_end;
3197 /* Now check if the cookie contains a date field, which would
3198 * appear after a vertical bar ('|') just after the server name
3199 * and before the delimiter.
3200 */
3201 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
3202 if (vbar1) {
3203 /* OK, so left of the bar is the server's cookie and
3204 * right is the last seen date. It is a base64 encoded
3205 * 30-bit value representing the UNIX date since the
3206 * epoch in 4-second quantities.
3207 */
3208 int val;
3209 delim = vbar1++;
3210 if (val_end - vbar1 >= 5) {
3211 val = b64tos30(vbar1);
3212 if (val > 0)
3213 txn->cookie_last_date = val << 2;
3214 }
3215 /* look for a second vertical bar */
3216 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
3217 if (vbar1 && (val_end - vbar1 > 5)) {
3218 val = b64tos30(vbar1 + 1);
3219 if (val > 0)
3220 txn->cookie_first_date = val << 2;
3221 }
3222 }
3223 }
3224
3225 /* if the cookie has an expiration date and the proxy wants to check
3226 * it, then we do that now. We first check if the cookie is too old,
3227 * then only if it has expired. We detect strict overflow because the
3228 * time resolution here is not great (4 seconds). Cookies with dates
3229 * in the future are ignored if their offset is beyond one day. This
3230 * allows an admin to fix timezone issues without expiring everyone
3231 * and at the same time avoids keeping unwanted side effects for too
3232 * long.
3233 */
3234 if (txn->cookie_first_date && s->be->cookie_maxlife &&
3235 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) ||
3236 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
3237 txn->flags &= ~TX_CK_MASK;
3238 txn->flags |= TX_CK_OLD;
3239 delim = val_beg; // let's pretend we have not found the cookie
3240 txn->cookie_first_date = 0;
3241 txn->cookie_last_date = 0;
3242 }
3243 else if (txn->cookie_last_date && s->be->cookie_maxidle &&
3244 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) ||
3245 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
3246 txn->flags &= ~TX_CK_MASK;
3247 txn->flags |= TX_CK_EXPIRED;
3248 delim = val_beg; // let's pretend we have not found the cookie
3249 txn->cookie_first_date = 0;
3250 txn->cookie_last_date = 0;
3251 }
3252
3253 /* Here, we'll look for the first running server which supports the cookie.
3254 * This allows to share a same cookie between several servers, for example
3255 * to dedicate backup servers to specific servers only.
3256 * However, to prevent clients from sticking to cookie-less backup server
3257 * when they have incidentely learned an empty cookie, we simply ignore
3258 * empty cookies and mark them as invalid.
3259 * The same behaviour is applied when persistence must be ignored.
3260 */
3261 if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
3262 srv = NULL;
3263
3264 while (srv) {
3265 if (srv->cookie && (srv->cklen == delim - val_beg) &&
3266 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
3267 if ((srv->cur_state != SRV_ST_STOPPED) ||
3268 (s->be->options & PR_O_PERSIST) ||
3269 (s->flags & SF_FORCE_PRST)) {
3270 /* we found the server and we can use it */
3271 txn->flags &= ~TX_CK_MASK;
3272 txn->flags |= (srv->cur_state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
3273 s->flags |= SF_DIRECT | SF_ASSIGNED;
3274 s->target = &srv->obj_type;
3275 break;
3276 } else {
3277 /* we found a server, but it's down,
3278 * mark it as such and go on in case
3279 * another one is available.
3280 */
3281 txn->flags &= ~TX_CK_MASK;
3282 txn->flags |= TX_CK_DOWN;
3283 }
3284 }
3285 srv = srv->next;
3286 }
3287
3288 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
3289 /* no server matched this cookie or we deliberately skipped it */
3290 txn->flags &= ~TX_CK_MASK;
3291 if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
3292 txn->flags |= TX_CK_UNUSED;
3293 else
3294 txn->flags |= TX_CK_INVALID;
3295 }
3296
3297 /* depending on the cookie mode, we may have to either :
3298 * - delete the complete cookie if we're in insert+indirect mode, so that
3299 * the server never sees it ;
3300 * - remove the server id from the cookie value, and tag the cookie as an
Joseph Herlante9d5c722018-11-25 11:00:25 -08003301 * application cookie so that it does not get accidentally removed later,
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003302 * if we're in cookie prefix mode
3303 */
3304 if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
3305 int delta; /* negative */
3306
3307 memmove(val_beg, delim + 1, hdr_end - (delim + 1));
3308 delta = val_beg - (delim + 1);
3309 val_end += delta;
3310 next += delta;
3311 hdr_end += delta;
3312 del_from = NULL;
3313 preserve_hdr = 1; /* we want to keep this cookie */
3314 }
3315 else if (del_from == NULL &&
3316 (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
3317 del_from = prev;
3318 }
3319 }
3320 else {
3321 /* This is not our cookie, so we must preserve it. But if we already
3322 * scheduled another cookie for removal, we cannot remove the
3323 * complete header, but we can remove the previous block itself.
3324 */
3325 preserve_hdr = 1;
3326
3327 if (del_from != NULL) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003328 int delta = http_del_hdr_value(hdr_beg, hdr_end, &del_from, prev);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003329 if (att_beg >= del_from)
3330 att_beg += delta;
3331 if (att_end >= del_from)
3332 att_end += delta;
3333 val_beg += delta;
3334 val_end += delta;
3335 next += delta;
3336 hdr_end += delta;
3337 prev = del_from;
3338 del_from = NULL;
3339 }
3340 }
3341
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003342 } /* for each cookie */
3343
3344
3345 /* There are no more cookies on this line.
3346 * We may still have one (or several) marked for deletion at the
3347 * end of the line. We must do this now in two ways :
3348 * - if some cookies must be preserved, we only delete from the
3349 * mark to the end of line ;
3350 * - if nothing needs to be preserved, simply delete the whole header
3351 */
3352 if (del_from) {
3353 hdr_end = (preserve_hdr ? del_from : hdr_beg);
3354 }
3355 if ((hdr_end - hdr_beg) != ctx.value.len) {
Christopher Faulet3e2638e2019-06-18 09:49:16 +02003356 if (hdr_beg != hdr_end)
3357 htx_change_blk_value_len(htx, ctx.blk, hdr_end - hdr_beg);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003358 else
3359 http_remove_header(htx, &ctx);
3360 }
3361 } /* for each "Cookie header */
3362}
3363
3364/*
3365 * Manage server-side cookies. It can impact performance by about 2% so it is
3366 * desirable to call it only when needed. This function is also used when we
3367 * just need to know if there is a cookie (eg: for check-cache).
3368 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003369static void http_manage_server_side_cookies(struct stream *s, struct channel *res)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003370{
3371 struct session *sess = s->sess;
3372 struct http_txn *txn = s->txn;
3373 struct htx *htx;
3374 struct http_hdr_ctx ctx;
3375 struct server *srv;
3376 char *hdr_beg, *hdr_end;
3377 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau6f7a02a2019-04-15 21:49:49 +02003378 int is_cookie2 = 0;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003379
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003380 htx = htxbuf(&res->buf);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003381
3382 ctx.blk = NULL;
3383 while (1) {
Olivier Houchardf0f42382019-07-22 17:43:46 +02003384 int is_first = 1;
3385
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003386 if (!http_find_header(htx, ist("Set-Cookie"), &ctx, 1)) {
3387 if (!http_find_header(htx, ist("Set-Cookie2"), &ctx, 1))
3388 break;
3389 is_cookie2 = 1;
3390 }
3391
3392 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
3393 * <prev> points to the colon.
3394 */
3395 txn->flags |= TX_SCK_PRESENT;
3396
3397 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
3398 * check-cache is enabled) and we are not interested in checking
3399 * them. Warning, the cookie capture is declared in the frontend.
3400 */
3401 if (s->be->cookie_name == NULL && sess->fe->capture_name == NULL)
3402 break;
3403
3404 /* OK so now we know we have to process this response cookie.
3405 * The format of the Set-Cookie header is slightly different
3406 * from the format of the Cookie header in that it does not
3407 * support the comma as a cookie delimiter (thus the header
3408 * cannot be folded) because the Expires attribute described in
3409 * the original Netscape's spec may contain an unquoted date
3410 * with a comma inside. We have to live with this because
3411 * many browsers don't support Max-Age and some browsers don't
3412 * support quoted strings. However the Set-Cookie2 header is
3413 * clean.
3414 *
3415 * We have to keep multiple pointers in order to support cookie
3416 * removal at the beginning, middle or end of header without
3417 * corrupting the header (in case of set-cookie2). A special
3418 * pointer, <scav> points to the beginning of the set-cookie-av
3419 * fields after the first semi-colon. The <next> pointer points
3420 * either to the end of line (set-cookie) or next unquoted comma
3421 * (set-cookie2). All of these headers are valid :
3422 *
3423 * hdr_beg hdr_end
3424 * | |
3425 * v |
3426 * NAME1 = VALUE 1 ; Secure; Path="/" |
3427 * NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT v
3428 * NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT
3429 * NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard
3430 * | | | | | | | |
3431 * | | | | | | | +-> next
3432 * | | | | | | +------------> scav
3433 * | | | | | +--------------> val_end
3434 * | | | | +--------------------> val_beg
3435 * | | | +----------------------> equal
3436 * | | +------------------------> att_end
3437 * | +----------------------------> att_beg
3438 * +------------------------------> prev
3439 * -------------------------------> hdr_beg
3440 */
3441 hdr_beg = ctx.value.ptr;
3442 hdr_end = hdr_beg + ctx.value.len;
3443 for (prev = hdr_beg; prev < hdr_end; prev = next) {
3444
3445 /* Iterate through all cookies on this line */
3446
3447 /* find att_beg */
3448 att_beg = prev;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003449 if (!is_first)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003450 att_beg++;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003451 is_first = 0;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003452
3453 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
3454 att_beg++;
3455
3456 /* find att_end : this is the first character after the last non
3457 * space before the equal. It may be equal to hdr_end.
3458 */
3459 equal = att_end = att_beg;
3460
3461 while (equal < hdr_end) {
3462 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
3463 break;
3464 if (HTTP_IS_SPHT(*equal++))
3465 continue;
3466 att_end = equal;
3467 }
3468
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003469 /* here, <equal> points to '=', a delimiter or the end. <att_end>
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003470 * is between <att_beg> and <equal>, both may be identical.
3471 */
3472
3473 /* look for end of cookie if there is an equal sign */
3474 if (equal < hdr_end && *equal == '=') {
3475 /* look for the beginning of the value */
3476 val_beg = equal + 1;
3477 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
3478 val_beg++;
3479
3480 /* find the end of the value, respecting quotes */
3481 next = http_find_cookie_value_end(val_beg, hdr_end);
3482
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003483 /* make val_end point to the first white space or delimiter after the value */
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003484 val_end = next;
3485 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
3486 val_end--;
3487 }
3488 else {
3489 /* <equal> points to next comma, semi-colon or EOL */
3490 val_beg = val_end = next = equal;
3491 }
3492
3493 if (next < hdr_end) {
3494 /* Set-Cookie2 supports multiple cookies, and <next> points to
3495 * a colon or semi-colon before the end. So skip all attr-value
3496 * pairs and look for the next comma. For Set-Cookie, since
3497 * commas are permitted in values, skip to the end.
3498 */
3499 if (is_cookie2)
3500 next = http_find_hdr_value_end(next, hdr_end);
3501 else
3502 next = hdr_end;
3503 }
3504
3505 /* Now everything is as on the diagram above */
3506
3507 /* Ignore cookies with no equal sign */
3508 if (equal == val_end)
3509 continue;
3510
3511 /* If there are spaces around the equal sign, we need to
3512 * strip them otherwise we'll get trouble for cookie captures,
3513 * or even for rewrites. Since this happens extremely rarely,
3514 * it does not hurt performance.
3515 */
3516 if (unlikely(att_end != equal || val_beg > equal + 1)) {
3517 int stripped_before = 0;
3518 int stripped_after = 0;
3519
3520 if (att_end != equal) {
3521 memmove(att_end, equal, hdr_end - equal);
3522 stripped_before = (att_end - equal);
3523 equal += stripped_before;
3524 val_beg += stripped_before;
3525 }
3526
3527 if (val_beg > equal + 1) {
3528 memmove(equal + 1, val_beg, hdr_end + stripped_before - val_beg);
3529 stripped_after = (equal + 1) - val_beg;
3530 val_beg += stripped_after;
3531 stripped_before += stripped_after;
3532 }
3533
3534 val_end += stripped_before;
3535 next += stripped_before;
3536 hdr_end += stripped_before;
3537
Christopher Faulet3e2638e2019-06-18 09:49:16 +02003538 htx_change_blk_value_len(htx, ctx.blk, hdr_end - hdr_beg);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003539 ctx.value.len = hdr_end - hdr_beg;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003540 }
3541
3542 /* First, let's see if we want to capture this cookie. We check
3543 * that we don't already have a server side cookie, because we
3544 * can only capture one. Also as an optimisation, we ignore
3545 * cookies shorter than the declared name.
3546 */
3547 if (sess->fe->capture_name != NULL &&
3548 txn->srv_cookie == NULL &&
3549 (val_end - att_beg >= sess->fe->capture_namelen) &&
3550 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
3551 int log_len = val_end - att_beg;
3552 if ((txn->srv_cookie = pool_alloc(pool_head_capture)) == NULL) {
3553 ha_alert("HTTP logging : out of memory.\n");
3554 }
3555 else {
3556 if (log_len > sess->fe->capture_len)
3557 log_len = sess->fe->capture_len;
3558 memcpy(txn->srv_cookie, att_beg, log_len);
3559 txn->srv_cookie[log_len] = 0;
3560 }
3561 }
3562
3563 srv = objt_server(s->target);
3564 /* now check if we need to process it for persistence */
3565 if (!(s->flags & SF_IGNORE_PRST) &&
3566 (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
3567 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
3568 /* assume passive cookie by default */
3569 txn->flags &= ~TX_SCK_MASK;
3570 txn->flags |= TX_SCK_FOUND;
3571
3572 /* If the cookie is in insert mode on a known server, we'll delete
3573 * this occurrence because we'll insert another one later.
3574 * We'll delete it too if the "indirect" option is set and we're in
3575 * a direct access.
3576 */
3577 if (s->be->ck_opts & PR_CK_PSV) {
3578 /* The "preserve" flag was set, we don't want to touch the
3579 * server's cookie.
3580 */
3581 }
3582 else if ((srv && (s->be->ck_opts & PR_CK_INS)) ||
3583 ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) {
3584 /* this cookie must be deleted */
3585 if (prev == hdr_beg && next == hdr_end) {
3586 /* whole header */
3587 http_remove_header(htx, &ctx);
3588 /* note: while both invalid now, <next> and <hdr_end>
3589 * are still equal, so the for() will stop as expected.
3590 */
3591 } else {
3592 /* just remove the value */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003593 int delta = http_del_hdr_value(hdr_beg, hdr_end, &prev, next);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003594 next = prev;
3595 hdr_end += delta;
3596 }
3597 txn->flags &= ~TX_SCK_MASK;
3598 txn->flags |= TX_SCK_DELETED;
3599 /* and go on with next cookie */
3600 }
3601 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) {
3602 /* replace bytes val_beg->val_end with the cookie name associated
3603 * with this server since we know it.
3604 */
3605 int sliding, delta;
3606
3607 ctx.value = ist2(val_beg, val_end - val_beg);
3608 ctx.lws_before = ctx.lws_after = 0;
3609 http_replace_header_value(htx, &ctx, ist2(srv->cookie, srv->cklen));
3610 delta = srv->cklen - (val_end - val_beg);
3611 sliding = (ctx.value.ptr - val_beg);
3612 hdr_beg += sliding;
3613 val_beg += sliding;
3614 next += sliding + delta;
3615 hdr_end += sliding + delta;
3616
3617 txn->flags &= ~TX_SCK_MASK;
3618 txn->flags |= TX_SCK_REPLACED;
3619 }
3620 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) {
3621 /* insert the cookie name associated with this server
3622 * before existing cookie, and insert a delimiter between them..
3623 */
3624 int sliding, delta;
3625 ctx.value = ist2(val_beg, 0);
3626 ctx.lws_before = ctx.lws_after = 0;
3627 http_replace_header_value(htx, &ctx, ist2(srv->cookie, srv->cklen + 1));
3628 delta = srv->cklen + 1;
3629 sliding = (ctx.value.ptr - val_beg);
3630 hdr_beg += sliding;
3631 val_beg += sliding;
3632 next += sliding + delta;
3633 hdr_end += sliding + delta;
3634
3635 val_beg[srv->cklen] = COOKIE_DELIM;
3636 txn->flags &= ~TX_SCK_MASK;
3637 txn->flags |= TX_SCK_REPLACED;
3638 }
3639 }
3640 /* that's done for this cookie, check the next one on the same
3641 * line when next != hdr_end (only if is_cookie2).
3642 */
3643 }
3644 }
3645}
3646
Christopher Faulet25a02f62018-10-24 12:00:25 +02003647/*
3648 * Parses the Cache-Control and Pragma request header fields to determine if
3649 * the request may be served from the cache and/or if it is cacheable. Updates
3650 * s->txn->flags.
3651 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003652void http_check_request_for_cacheability(struct stream *s, struct channel *req)
Christopher Faulet25a02f62018-10-24 12:00:25 +02003653{
3654 struct http_txn *txn = s->txn;
3655 struct htx *htx;
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003656 struct http_hdr_ctx ctx = { .blk = NULL };
3657 int pragma_found, cc_found;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003658
3659 if ((txn->flags & (TX_CACHEABLE|TX_CACHE_IGNORE)) == TX_CACHE_IGNORE)
3660 return; /* nothing more to do here */
3661
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003662 htx = htxbuf(&req->buf);
Christopher Faulet25a02f62018-10-24 12:00:25 +02003663 pragma_found = cc_found = 0;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003664
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003665 /* Check "pragma" header for HTTP/1.0 compatibility. */
3666 if (http_find_header(htx, ist("pragma"), &ctx, 1)) {
3667 if (isteqi(ctx.value, ist("no-cache"))) {
3668 pragma_found = 1;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003669 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003670 }
Christopher Faulet25a02f62018-10-24 12:00:25 +02003671
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003672 ctx.blk = NULL;
3673 /* Don't use the cache and don't try to store if we found the
3674 * Authorization header */
3675 if (http_find_header(htx, ist("authorization"), &ctx, 1)) {
3676 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3677 txn->flags |= TX_CACHE_IGNORE;
3678 }
Christopher Faulet25a02f62018-10-24 12:00:25 +02003679
Christopher Faulet25a02f62018-10-24 12:00:25 +02003680
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003681 /* Look for "cache-control" header and iterate over all the values
3682 * until we find one that specifies that caching is possible or not. */
3683 ctx.blk = NULL;
3684 while (http_find_header(htx, ist("cache-control"), &ctx, 0)) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003685 cc_found = 1;
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003686 /* We don't check the values after max-age, max-stale nor min-fresh,
3687 * we simply don't use the cache when they're specified. */
3688 if (istmatchi(ctx.value, ist("max-age")) ||
3689 istmatchi(ctx.value, ist("no-cache")) ||
3690 istmatchi(ctx.value, ist("max-stale")) ||
3691 istmatchi(ctx.value, ist("min-fresh"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003692 txn->flags |= TX_CACHE_IGNORE;
3693 continue;
3694 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003695 if (istmatchi(ctx.value, ist("no-store"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003696 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3697 continue;
3698 }
3699 }
3700
3701 /* RFC7234#5.4:
3702 * When the Cache-Control header field is also present and
3703 * understood in a request, Pragma is ignored.
3704 * When the Cache-Control header field is not present in a
3705 * request, caches MUST consider the no-cache request
3706 * pragma-directive as having the same effect as if
3707 * "Cache-Control: no-cache" were present.
3708 */
3709 if (!cc_found && pragma_found)
3710 txn->flags |= TX_CACHE_IGNORE;
3711}
3712
3713/*
3714 * Check if response is cacheable or not. Updates s->txn->flags.
3715 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003716void http_check_response_for_cacheability(struct stream *s, struct channel *res)
Christopher Faulet25a02f62018-10-24 12:00:25 +02003717{
3718 struct http_txn *txn = s->txn;
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003719 struct http_hdr_ctx ctx = { .blk = NULL };
Christopher Faulet25a02f62018-10-24 12:00:25 +02003720 struct htx *htx;
Remi Tricot-Le Bretoncc9bf2e2020-11-12 11:14:41 +01003721 int has_freshness_info = 0;
3722 int has_validator = 0;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003723
3724 if (txn->status < 200) {
3725 /* do not try to cache interim responses! */
3726 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3727 return;
3728 }
3729
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003730 htx = htxbuf(&res->buf);
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003731 /* Check "pragma" header for HTTP/1.0 compatibility. */
3732 if (http_find_header(htx, ist("pragma"), &ctx, 1)) {
3733 if (isteqi(ctx.value, ist("no-cache"))) {
3734 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3735 return;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003736 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003737 }
Christopher Faulet25a02f62018-10-24 12:00:25 +02003738
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003739 /* Look for "cache-control" header and iterate over all the values
3740 * until we find one that specifies that caching is possible or not. */
3741 ctx.blk = NULL;
3742 while (http_find_header(htx, ist("cache-control"), &ctx, 0)) {
3743 if (isteqi(ctx.value, ist("public"))) {
3744 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003745 continue;
3746 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003747 if (isteqi(ctx.value, ist("private")) ||
3748 isteqi(ctx.value, ist("no-cache")) ||
3749 isteqi(ctx.value, ist("no-store")) ||
3750 isteqi(ctx.value, ist("max-age=0")) ||
3751 isteqi(ctx.value, ist("s-maxage=0"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003752 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003753 continue;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003754 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003755 /* We might have a no-cache="set-cookie" form. */
3756 if (istmatchi(ctx.value, ist("no-cache=\"set-cookie"))) {
3757 txn->flags &= ~TX_CACHE_COOK;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003758 continue;
3759 }
Remi Tricot-Le Bretoncc9bf2e2020-11-12 11:14:41 +01003760
3761 if (istmatchi(ctx.value, ist("s-maxage")) ||
3762 istmatchi(ctx.value, ist("max-age"))) {
3763 has_freshness_info = 1;
3764 continue;
3765 }
3766 }
3767
3768 /* If no freshness information could be found in Cache-Control values,
3769 * look for an Expires header. */
3770 if (!has_freshness_info) {
3771 ctx.blk = NULL;
3772 has_freshness_info = http_find_header(htx, ist("expires"), &ctx, 0);
Christopher Faulet25a02f62018-10-24 12:00:25 +02003773 }
Remi Tricot-Le Bretoncc9bf2e2020-11-12 11:14:41 +01003774
3775 /* If no freshness information could be found in Cache-Control or Expires
3776 * values, look for an explicit validator. */
3777 if (!has_freshness_info) {
3778 ctx.blk = NULL;
3779 has_validator = 1;
3780 if (!http_find_header(htx, ist("etag"), &ctx, 0)) {
3781 ctx.blk = NULL;
3782 if (!http_find_header(htx, ist("last-modified"), &ctx, 0))
3783 has_validator = 0;
3784 }
3785 }
3786
3787 /* We won't store an entry that has neither a cache validator nor an
3788 * explicit expiration time, as suggested in RFC 7234#3. */
3789 if (!has_freshness_info && !has_validator)
3790 txn->flags |= TX_CACHE_IGNORE;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003791}
3792
Christopher Faulet377c5a52018-10-24 21:21:30 +02003793/*
3794 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
3795 * for the current backend.
3796 *
3797 * It is assumed that the request is either a HEAD, GET, or POST and that the
3798 * uri_auth field is valid.
3799 *
3800 * Returns 1 if stats should be provided, otherwise 0.
3801 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003802static int http_stats_check_uri(struct stream *s, struct http_txn *txn, struct proxy *backend)
Christopher Faulet377c5a52018-10-24 21:21:30 +02003803{
3804 struct uri_auth *uri_auth = backend->uri_auth;
3805 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003806 struct htx_sl *sl;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003807 struct ist uri;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003808
3809 if (!uri_auth)
3810 return 0;
3811
3812 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
3813 return 0;
3814
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003815 htx = htxbuf(&s->req.buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02003816 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003817 uri = htx_sl_req_uri(sl);
Willy Tarreau1eb3b482019-10-31 15:50:28 +01003818 if (*uri_auth->uri_prefix == '/')
3819 uri = http_get_path(uri);
Christopher Faulet377c5a52018-10-24 21:21:30 +02003820
3821 /* check URI size */
3822 if (uri_auth->uri_len > uri.len)
3823 return 0;
3824
3825 if (memcmp(uri.ptr, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
3826 return 0;
3827
3828 return 1;
3829}
3830
3831/* This function prepares an applet to handle the stats. It can deal with the
3832 * "100-continue" expectation, check that admin rules are met for POST requests,
3833 * and program a response message if something was unexpected. It cannot fail
3834 * and always relies on the stats applet to complete the job. It does not touch
3835 * analysers nor counters, which are left to the caller. It does not touch
3836 * s->target which is supposed to already point to the stats applet. The caller
3837 * is expected to have already assigned an appctx to the stream.
3838 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003839static int http_handle_stats(struct stream *s, struct channel *req)
Christopher Faulet377c5a52018-10-24 21:21:30 +02003840{
3841 struct stats_admin_rule *stats_admin_rule;
3842 struct stream_interface *si = &s->si[1];
3843 struct session *sess = s->sess;
3844 struct http_txn *txn = s->txn;
3845 struct http_msg *msg = &txn->req;
3846 struct uri_auth *uri_auth = s->be->uri_auth;
3847 const char *h, *lookup, *end;
3848 struct appctx *appctx;
3849 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003850 struct htx_sl *sl;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003851
3852 appctx = si_appctx(si);
3853 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
3854 appctx->st1 = appctx->st2 = 0;
3855 appctx->ctx.stats.st_code = STAT_STATUS_INIT;
Willy Tarreau676c29e2019-10-09 10:50:01 +02003856 appctx->ctx.stats.flags |= uri_auth->flags;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003857 appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */
3858 if ((msg->flags & HTTP_MSGF_VER_11) && (txn->meth != HTTP_METH_HEAD))
3859 appctx->ctx.stats.flags |= STAT_CHUNKED;
3860
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003861 htx = htxbuf(&req->buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02003862 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003863 lookup = HTX_SL_REQ_UPTR(sl) + uri_auth->uri_len;
3864 end = HTX_SL_REQ_UPTR(sl) + HTX_SL_REQ_ULEN(sl);
Christopher Faulet377c5a52018-10-24 21:21:30 +02003865
3866 for (h = lookup; h <= end - 3; h++) {
3867 if (memcmp(h, ";up", 3) == 0) {
3868 appctx->ctx.stats.flags |= STAT_HIDE_DOWN;
3869 break;
3870 }
Willy Tarreau3e320362020-10-23 17:28:57 +02003871 if (memcmp(h, ";no-maint", 3) == 0) {
3872 appctx->ctx.stats.flags |= STAT_HIDE_MAINT;
3873 break;
3874 }
Christopher Faulet377c5a52018-10-24 21:21:30 +02003875 }
3876
3877 if (uri_auth->refresh) {
3878 for (h = lookup; h <= end - 10; h++) {
3879 if (memcmp(h, ";norefresh", 10) == 0) {
3880 appctx->ctx.stats.flags |= STAT_NO_REFRESH;
3881 break;
3882 }
3883 }
3884 }
3885
3886 for (h = lookup; h <= end - 4; h++) {
3887 if (memcmp(h, ";csv", 4) == 0) {
Christopher Faulet6338a082019-09-09 15:50:54 +02003888 appctx->ctx.stats.flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
Christopher Faulet377c5a52018-10-24 21:21:30 +02003889 break;
3890 }
3891 }
3892
3893 for (h = lookup; h <= end - 6; h++) {
3894 if (memcmp(h, ";typed", 6) == 0) {
Christopher Faulet6338a082019-09-09 15:50:54 +02003895 appctx->ctx.stats.flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
Christopher Faulet377c5a52018-10-24 21:21:30 +02003896 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
3897 break;
3898 }
3899 }
3900
Christopher Faulet6338a082019-09-09 15:50:54 +02003901 for (h = lookup; h <= end - 5; h++) {
3902 if (memcmp(h, ";json", 5) == 0) {
3903 appctx->ctx.stats.flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
3904 appctx->ctx.stats.flags |= STAT_FMT_JSON;
3905 break;
3906 }
3907 }
3908
3909 for (h = lookup; h <= end - 12; h++) {
3910 if (memcmp(h, ";json-schema", 12) == 0) {
3911 appctx->ctx.stats.flags &= ~STAT_FMT_MASK;
3912 appctx->ctx.stats.flags |= STAT_JSON_SCHM;
3913 break;
3914 }
3915 }
3916
Christopher Faulet377c5a52018-10-24 21:21:30 +02003917 for (h = lookup; h <= end - 8; h++) {
3918 if (memcmp(h, ";st=", 4) == 0) {
3919 int i;
3920 h += 4;
3921 appctx->ctx.stats.st_code = STAT_STATUS_UNKN;
3922 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
3923 if (strncmp(stat_status_codes[i], h, 4) == 0) {
3924 appctx->ctx.stats.st_code = i;
3925 break;
3926 }
3927 }
3928 break;
3929 }
3930 }
3931
3932 appctx->ctx.stats.scope_str = 0;
3933 appctx->ctx.stats.scope_len = 0;
3934 for (h = lookup; h <= end - 8; h++) {
3935 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
3936 int itx = 0;
3937 const char *h2;
3938 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
3939 const char *err;
3940
3941 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
3942 h2 = h;
Christopher Fauleted7a0662019-01-14 11:07:34 +01003943 appctx->ctx.stats.scope_str = h2 - HTX_SL_REQ_UPTR(sl);
3944 while (h < end) {
Christopher Faulet377c5a52018-10-24 21:21:30 +02003945 if (*h == ';' || *h == '&' || *h == ' ')
3946 break;
3947 itx++;
3948 h++;
3949 }
3950
3951 if (itx > STAT_SCOPE_TXT_MAXLEN)
3952 itx = STAT_SCOPE_TXT_MAXLEN;
3953 appctx->ctx.stats.scope_len = itx;
3954
3955 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
3956 memcpy(scope_txt, h2, itx);
3957 scope_txt[itx] = '\0';
3958 err = invalid_char(scope_txt);
3959 if (err) {
3960 /* bad char in search text => clear scope */
3961 appctx->ctx.stats.scope_str = 0;
3962 appctx->ctx.stats.scope_len = 0;
3963 }
3964 break;
3965 }
3966 }
3967
3968 /* now check whether we have some admin rules for this request */
3969 list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) {
3970 int ret = 1;
3971
3972 if (stats_admin_rule->cond) {
3973 ret = acl_exec_cond(stats_admin_rule->cond, s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
3974 ret = acl_pass(ret);
3975 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
3976 ret = !ret;
3977 }
3978
3979 if (ret) {
3980 /* no rule, or the rule matches */
3981 appctx->ctx.stats.flags |= STAT_ADMIN;
3982 break;
3983 }
3984 }
3985
Christopher Faulet5d45e382019-02-27 15:15:23 +01003986 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
3987 appctx->st0 = STAT_HTTP_HEAD;
3988 else if (txn->meth == HTTP_METH_POST) {
Christopher Fauletbd9e8422019-08-15 22:26:48 +02003989 if (appctx->ctx.stats.flags & STAT_ADMIN) {
Christopher Faulet377c5a52018-10-24 21:21:30 +02003990 appctx->st0 = STAT_HTTP_POST;
Christopher Fauletbd9e8422019-08-15 22:26:48 +02003991 if (msg->msg_state < HTTP_MSG_DATA)
3992 req->analysers |= AN_REQ_HTTP_BODY;
3993 }
Christopher Faulet377c5a52018-10-24 21:21:30 +02003994 else {
Christopher Faulet5d45e382019-02-27 15:15:23 +01003995 /* POST without admin level */
Christopher Faulet377c5a52018-10-24 21:21:30 +02003996 appctx->ctx.stats.flags &= ~STAT_CHUNKED;
3997 appctx->ctx.stats.st_code = STAT_STATUS_DENY;
3998 appctx->st0 = STAT_HTTP_LAST;
3999 }
4000 }
4001 else {
Christopher Faulet5d45e382019-02-27 15:15:23 +01004002 /* Unsupported method */
4003 appctx->ctx.stats.flags &= ~STAT_CHUNKED;
4004 appctx->ctx.stats.st_code = STAT_STATUS_IVAL;
4005 appctx->st0 = STAT_HTTP_LAST;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004006 }
4007
4008 s->task->nice = -32; /* small boost for HTTP statistics */
4009 return 1;
4010}
4011
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004012void http_perform_server_redirect(struct stream *s, struct stream_interface *si)
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004013{
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004014 struct channel *req = &s->req;
4015 struct channel *res = &s->res;
4016 struct server *srv;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004017 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004018 struct htx_sl *sl;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004019 struct ist path, location;
4020 unsigned int flags;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004021
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004022 /*
4023 * Create the location
4024 */
4025 chunk_reset(&trash);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004026
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004027 /* 1: add the server's prefix */
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004028 /* special prefix "/" means don't change URL */
4029 srv = __objt_server(s->target);
4030 if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') {
4031 if (!chunk_memcat(&trash, srv->rdr_pfx, srv->rdr_len))
4032 return;
4033 }
4034
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004035 /* 2: add the request Path */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004036 htx = htxbuf(&req->buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02004037 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004038 path = http_get_path(htx_sl_req_uri(sl));
Tim Duesterhused526372020-03-05 17:56:33 +01004039 if (!isttest(path))
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004040 return;
4041
4042 if (!chunk_memcat(&trash, path.ptr, path.len))
4043 return;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004044 location = ist2(trash.area, trash.data);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004045
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004046 /*
4047 * Create the 302 respone
4048 */
4049 htx = htx_from_buf(&res->buf);
4050 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
4051 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
4052 ist("HTTP/1.1"), ist("302"), ist("Found"));
4053 if (!sl)
4054 goto fail;
4055 sl->info.res.status = 302;
4056 s->txn->status = 302;
4057
4058 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) ||
4059 !htx_add_header(htx, ist("Connection"), ist("close")) ||
4060 !htx_add_header(htx, ist("Content-length"), ist("0")) ||
4061 !htx_add_header(htx, ist("Location"), location))
4062 goto fail;
4063
4064 if (!htx_add_endof(htx, HTX_BLK_EOH) || !htx_add_endof(htx, HTX_BLK_EOM))
4065 goto fail;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004066
Christopher Fauletc20afb82020-01-24 19:16:26 +01004067 htx_to_buf(htx, &res->buf);
Christopher Fauleta72a7e42020-01-28 09:28:11 +01004068 if (!http_forward_proxy_resp(s, 1))
4069 goto fail;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004070
4071 /* return without error. */
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004072 si_shutr(si);
4073 si_shutw(si);
4074 si->err_type = SI_ET_NONE;
4075 si->state = SI_ST_CLO;
4076
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004077 if (!(s->flags & SF_ERR_MASK))
4078 s->flags |= SF_ERR_LOCAL;
4079 if (!(s->flags & SF_FINST_MASK))
4080 s->flags |= SF_FINST_C;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004081
4082 /* FIXME: we should increase a counter of redirects per server and per backend. */
4083 srv_inc_sess_ctr(srv);
4084 srv_set_sess_last(srv);
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004085 return;
4086
4087 fail:
4088 /* If an error occurred, remove the incomplete HTTP response from the
4089 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004090 channel_htx_truncate(res, htx);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004091}
4092
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05004093/* This function terminates the request because it was completely analyzed or
Christopher Fauletf2824e62018-10-01 12:12:37 +02004094 * because an error was triggered during the body forwarding.
4095 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004096static void http_end_request(struct stream *s)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004097{
4098 struct channel *chn = &s->req;
4099 struct http_txn *txn = s->txn;
4100
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004101 DBG_TRACE_ENTER(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004102
Christopher Fauletb42a8b62018-11-19 21:59:00 +01004103 if (unlikely(txn->req.msg_state == HTTP_MSG_ERROR ||
4104 txn->rsp.msg_state == HTTP_MSG_ERROR)) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004105 channel_abort(chn);
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004106 channel_htx_truncate(chn, htxbuf(&chn->buf));
Christopher Fauletf2824e62018-10-01 12:12:37 +02004107 goto end;
4108 }
4109
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004110 if (unlikely(txn->req.msg_state < HTTP_MSG_DONE)) {
4111 DBG_TRACE_DEVEL("waiting end of the request", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004112 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004113 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004114
4115 if (txn->req.msg_state == HTTP_MSG_DONE) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004116 /* No need to read anymore, the request was completely parsed.
4117 * We can shut the read side unless we want to abort_on_close,
4118 * or we have a POST request. The issue with POST requests is
4119 * that some browsers still send a CRLF after the request, and
4120 * this CRLF must be read so that it does not remain in the kernel
4121 * buffers, otherwise a close could cause an RST on some systems
4122 * (eg: Linux).
4123 */
Christopher Faulet769d0e92019-03-22 14:23:18 +01004124 if (!(s->be->options & PR_O_ABRT_CLOSE) && txn->meth != HTTP_METH_POST)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004125 channel_dont_read(chn);
4126
4127 /* if the server closes the connection, we want to immediately react
4128 * and close the socket to save packets and syscalls.
4129 */
4130 s->si[1].flags |= SI_FL_NOHALF;
4131
4132 /* In any case we've finished parsing the request so we must
4133 * disable Nagle when sending data because 1) we're not going
4134 * to shut this side, and 2) the server is waiting for us to
4135 * send pending data.
4136 */
4137 chn->flags |= CF_NEVER_WAIT;
4138
Christopher Fauletd01ce402019-01-02 17:44:13 +01004139 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
4140 /* The server has not finished to respond, so we
4141 * don't want to move in order not to upset it.
4142 */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004143 DBG_TRACE_DEVEL("waiting end of the response", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletd01ce402019-01-02 17:44:13 +01004144 return;
4145 }
4146
Christopher Fauletf2824e62018-10-01 12:12:37 +02004147 /* When we get here, it means that both the request and the
4148 * response have finished receiving. Depending on the connection
4149 * mode, we'll have to wait for the last bytes to leave in either
4150 * direction, and sometimes for a close to be effective.
4151 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02004152 if (txn->flags & TX_CON_WANT_TUN) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004153 /* Tunnel mode will not have any analyser so it needs to
4154 * poll for reads.
4155 */
4156 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004157 if (b_data(&chn->buf)) {
4158 DBG_TRACE_DEVEL("waiting to flush the request", STRM_EV_HTTP_ANA, s, txn);
Christopher Faulet9768c262018-10-22 09:34:31 +02004159 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004160 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004161 txn->req.msg_state = HTTP_MSG_TUNNEL;
4162 }
4163 else {
4164 /* we're not expecting any new data to come for this
4165 * transaction, so we can close it.
Christopher Faulet9768c262018-10-22 09:34:31 +02004166 *
4167 * However, there is an exception if the response
4168 * length is undefined. In this case, we need to wait
4169 * the close from the server. The response will be
4170 * switched in TUNNEL mode until the end.
Christopher Fauletf2824e62018-10-01 12:12:37 +02004171 */
4172 if (!(txn->rsp.flags & HTTP_MSGF_XFER_LEN) &&
4173 txn->rsp.msg_state != HTTP_MSG_CLOSED)
Christopher Faulet9768c262018-10-22 09:34:31 +02004174 goto check_channel_flags;
Christopher Fauletf2824e62018-10-01 12:12:37 +02004175
4176 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4177 channel_shutr_now(chn);
4178 channel_shutw_now(chn);
4179 }
4180 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004181 goto check_channel_flags;
4182 }
4183
4184 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
4185 http_msg_closing:
4186 /* nothing else to forward, just waiting for the output buffer
4187 * to be empty and for the shutw_now to take effect.
4188 */
4189 if (channel_is_empty(chn)) {
4190 txn->req.msg_state = HTTP_MSG_CLOSED;
4191 goto http_msg_closed;
4192 }
4193 else if (chn->flags & CF_SHUTW) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004194 txn->req.msg_state = HTTP_MSG_ERROR;
4195 goto end;
4196 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004197 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004198 return;
4199 }
4200
4201 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
4202 http_msg_closed:
Christopher Fauletf2824e62018-10-01 12:12:37 +02004203 /* if we don't know whether the server will close, we need to hard close */
4204 if (txn->rsp.flags & HTTP_MSGF_XFER_LEN)
4205 s->si[1].flags |= SI_FL_NOLINGER; /* we want to close ASAP */
Christopher Fauletf2824e62018-10-01 12:12:37 +02004206 /* see above in MSG_DONE why we only do this in these states */
Christopher Faulet769d0e92019-03-22 14:23:18 +01004207 if (!(s->be->options & PR_O_ABRT_CLOSE))
Christopher Fauletf2824e62018-10-01 12:12:37 +02004208 channel_dont_read(chn);
4209 goto end;
4210 }
4211
4212 check_channel_flags:
4213 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4214 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4215 /* if we've just closed an output, let's switch */
4216 txn->req.msg_state = HTTP_MSG_CLOSING;
4217 goto http_msg_closing;
4218 }
4219
4220 end:
4221 chn->analysers &= AN_REQ_FLT_END;
4222 if (txn->req.msg_state == HTTP_MSG_TUNNEL && HAS_REQ_DATA_FILTERS(s))
4223 chn->analysers |= AN_REQ_FLT_XFER_DATA;
4224 channel_auto_close(chn);
4225 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004226 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004227}
4228
4229
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05004230/* This function terminates the response because it was completely analyzed or
Christopher Fauletf2824e62018-10-01 12:12:37 +02004231 * because an error was triggered during the body forwarding.
4232 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004233static void http_end_response(struct stream *s)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004234{
4235 struct channel *chn = &s->res;
4236 struct http_txn *txn = s->txn;
4237
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004238 DBG_TRACE_ENTER(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004239
Christopher Fauletb42a8b62018-11-19 21:59:00 +01004240 if (unlikely(txn->req.msg_state == HTTP_MSG_ERROR ||
4241 txn->rsp.msg_state == HTTP_MSG_ERROR)) {
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004242 channel_htx_truncate(&s->req, htxbuf(&s->req.buf));
Christopher Faulet9768c262018-10-22 09:34:31 +02004243 channel_abort(&s->req);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004244 goto end;
4245 }
4246
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004247 if (unlikely(txn->rsp.msg_state < HTTP_MSG_DONE)) {
4248 DBG_TRACE_DEVEL("waiting end of the response", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004249 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004250 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004251
4252 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
4253 /* In theory, we don't need to read anymore, but we must
4254 * still monitor the server connection for a possible close
4255 * while the request is being uploaded, so we don't disable
4256 * reading.
4257 */
4258 /* channel_dont_read(chn); */
4259
4260 if (txn->req.msg_state < HTTP_MSG_DONE) {
4261 /* The client seems to still be sending data, probably
4262 * because we got an error response during an upload.
4263 * We have the choice of either breaking the connection
4264 * or letting it pass through. Let's do the later.
4265 */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004266 DBG_TRACE_DEVEL("waiting end of the request", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004267 return;
4268 }
4269
4270 /* When we get here, it means that both the request and the
4271 * response have finished receiving. Depending on the connection
4272 * mode, we'll have to wait for the last bytes to leave in either
4273 * direction, and sometimes for a close to be effective.
4274 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02004275 if (txn->flags & TX_CON_WANT_TUN) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004276 channel_auto_read(chn);
4277 chn->flags |= CF_NEVER_WAIT;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004278 if (b_data(&chn->buf)) {
4279 DBG_TRACE_DEVEL("waiting to flush the respone", STRM_EV_HTTP_ANA, s, txn);
Christopher Faulet9768c262018-10-22 09:34:31 +02004280 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004281 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004282 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
4283 }
4284 else {
4285 /* we're not expecting any new data to come for this
4286 * transaction, so we can close it.
4287 */
4288 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4289 channel_shutr_now(chn);
4290 channel_shutw_now(chn);
4291 }
4292 }
4293 goto check_channel_flags;
4294 }
4295
4296 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
4297 http_msg_closing:
4298 /* nothing else to forward, just waiting for the output buffer
4299 * to be empty and for the shutw_now to take effect.
4300 */
4301 if (channel_is_empty(chn)) {
4302 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4303 goto http_msg_closed;
4304 }
4305 else if (chn->flags & CF_SHUTW) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004306 txn->rsp.msg_state = HTTP_MSG_ERROR;
Christopher Fauletcff0f732019-12-16 16:13:44 +01004307 _HA_ATOMIC_ADD(&strm_sess(s)->fe->fe_counters.cli_aborts, 1);
Olivier Houcharda798bf52019-03-08 18:52:00 +01004308 _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01004309 if (strm_sess(s)->listener->counters)
4310 _HA_ATOMIC_ADD(&strm_sess(s)->listener->counters->cli_aborts, 1);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004311 if (objt_server(s->target))
Christopher Fauletcff0f732019-12-16 16:13:44 +01004312 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.cli_aborts, 1);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004313 goto end;
4314 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004315 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004316 return;
4317 }
4318
4319 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
4320 http_msg_closed:
4321 /* drop any pending data */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004322 channel_htx_truncate(&s->req, htxbuf(&s->req.buf));
Christopher Faulet9768c262018-10-22 09:34:31 +02004323 channel_abort(&s->req);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004324 goto end;
4325 }
4326
4327 check_channel_flags:
4328 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4329 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4330 /* if we've just closed an output, let's switch */
4331 txn->rsp.msg_state = HTTP_MSG_CLOSING;
4332 goto http_msg_closing;
4333 }
4334
4335 end:
4336 chn->analysers &= AN_RES_FLT_END;
4337 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL && HAS_RSP_DATA_FILTERS(s))
4338 chn->analysers |= AN_RES_FLT_XFER_DATA;
4339 channel_auto_close(chn);
4340 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004341 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004342}
4343
Christopher Fauletef70e252020-01-28 09:26:19 +01004344/* Forward a response generated by HAProxy (error/redirect/return). This
4345 * function forwards all pending incoming data. If <final> is set to 0, nothing
4346 * more is performed. It is used for 1xx informational messages. Otherwise, the
Christopher Faulet507479b2020-05-15 12:29:46 +02004347 * transaction is terminated and the request is emptied. On success 1 is
Christopher Faulet40e6b552020-06-25 16:04:50 +02004348 * returned. If an error occurred, 0 is returned. If it fails, this function
4349 * only exits. It is the caller responsibility to do the cleanup.
Christopher Fauletef70e252020-01-28 09:26:19 +01004350 */
4351int http_forward_proxy_resp(struct stream *s, int final)
4352{
4353 struct channel *req = &s->req;
4354 struct channel *res = &s->res;
4355 struct htx *htx = htxbuf(&res->buf);
4356 size_t data;
4357
4358 if (final) {
4359 htx->flags |= HTX_FL_PROXY_RESP;
Christopher Faulet507479b2020-05-15 12:29:46 +02004360
Christopher Fauletaab1b672020-11-18 16:44:02 +01004361 if (!htx_is_empty(htx) && !http_eval_after_res_rules(s))
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01004362 return 0;
Christopher Fauletef70e252020-01-28 09:26:19 +01004363
Christopher Fauletd6c48362020-10-19 18:01:38 +02004364 if (s->txn->meth == HTTP_METH_HEAD)
4365 htx_skip_msg_payload(htx);
4366
Christopher Fauletef70e252020-01-28 09:26:19 +01004367 channel_auto_read(req);
4368 channel_abort(req);
4369 channel_auto_close(req);
4370 channel_htx_erase(req, htxbuf(&req->buf));
4371
4372 res->wex = tick_add_ifset(now_ms, res->wto);
4373 channel_auto_read(res);
4374 channel_auto_close(res);
4375 channel_shutr_now(res);
Christopher Faulet1a9db7c2020-06-25 15:36:45 +02004376 res->flags |= CF_EOI; /* The response is terminated, add EOI */
Christopher Faulet810df062020-07-22 16:20:34 +02004377 htxbuf(&res->buf)->flags |= HTX_FL_EOI; /* no more data are expected */
Christopher Fauletef70e252020-01-28 09:26:19 +01004378 }
Christopher Fauletcf6898c2020-06-25 15:55:11 +02004379 else {
4380 /* Send ASAP informational messages. Rely on CF_EOI for final
4381 * response.
4382 */
4383 res->flags |= CF_SEND_DONTWAIT;
4384 }
Christopher Fauletef70e252020-01-28 09:26:19 +01004385
4386 data = htx->data - co_data(res);
4387 c_adv(res, data);
4388 htx->first = -1;
4389 res->total += data;
4390 return 1;
4391}
4392
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004393void http_server_error(struct stream *s, struct stream_interface *si, int err,
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004394 int finst, struct http_reply *msg)
Christopher Faulet0f226952018-10-22 09:29:56 +02004395{
Christopher Faulet72c7d8d2020-01-27 15:32:25 +01004396 http_reply_and_close(s, s->txn->status, msg);
Christopher Faulet0f226952018-10-22 09:29:56 +02004397 if (!(s->flags & SF_ERR_MASK))
4398 s->flags |= err;
4399 if (!(s->flags & SF_FINST_MASK))
4400 s->flags |= finst;
4401}
4402
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004403void http_reply_and_close(struct stream *s, short status, struct http_reply *msg)
Christopher Faulet0f226952018-10-22 09:29:56 +02004404{
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004405 if (!msg) {
4406 channel_htx_truncate(&s->res, htxbuf(&s->res.buf));
4407 goto end;
4408 }
4409
4410 if (http_reply_message(s, msg) == -1) {
4411 /* On error, return a 500 error message, but don't rewrite it if
Christopher Faulet40e6b552020-06-25 16:04:50 +02004412 * it is already an internal error. If it was already a "const"
4413 * 500 error, just fail.
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004414 */
Christopher Faulet40e6b552020-06-25 16:04:50 +02004415 if (s->txn->status == 500) {
4416 if (s->txn->flags & TX_CONST_REPLY)
4417 goto end;
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004418 s->txn->flags |= TX_CONST_REPLY;
Christopher Faulet40e6b552020-06-25 16:04:50 +02004419 }
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004420 s->txn->status = 500;
4421 s->txn->http_reply = NULL;
4422 return http_reply_and_close(s, s->txn->status, http_error_message(s));
4423 }
4424
4425end:
4426 s->res.wex = tick_add_ifset(now_ms, s->res.wto);
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004427
Christopher Faulet0f226952018-10-22 09:29:56 +02004428 channel_auto_read(&s->req);
4429 channel_abort(&s->req);
4430 channel_auto_close(&s->req);
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004431 channel_htx_erase(&s->req, htxbuf(&s->req.buf));
Christopher Faulet72c7d8d2020-01-27 15:32:25 +01004432 channel_auto_read(&s->res);
4433 channel_auto_close(&s->res);
4434 channel_shutr_now(&s->res);
Christopher Faulet0f226952018-10-22 09:29:56 +02004435}
4436
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004437struct http_reply *http_error_message(struct stream *s)
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004438{
4439 const int msgnum = http_get_status_idx(s->txn->status);
4440
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004441 if (s->txn->http_reply)
4442 return s->txn->http_reply;
4443 else if (s->be->replies[msgnum])
4444 return s->be->replies[msgnum];
4445 else if (strm_fe(s)->replies[msgnum])
4446 return strm_fe(s)->replies[msgnum];
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004447 else
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004448 return &http_err_replies[msgnum];
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004449}
4450
Christopher Faulet40e6b552020-06-25 16:04:50 +02004451/* Produces an HTX message from an http reply. Depending on the http reply type,
4452 * a, errorfile, an raw file or a log-format string is used. On success, it
4453 * returns 0. If an error occurs -1 is returned. If it fails, this function only
4454 * exits. It is the caller responsibility to do the cleanup.
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004455 */
Christopher Fauletae43b6c2020-05-27 15:24:22 +02004456int http_reply_to_htx(struct stream *s, struct htx *htx, struct http_reply *reply)
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004457{
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004458 struct buffer *errmsg;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004459 struct htx_sl *sl;
4460 struct buffer *body = NULL;
4461 const char *status, *reason, *clen, *ctype;
4462 unsigned int slflags;
4463 int ret = 0;
4464
Christopher Faulete29a97e2020-05-14 14:49:25 +02004465 /*
4466 * - HTTP_REPLY_ERRFILES unexpected here. handled as no payload if so
4467 *
4468 * - HTTP_REPLY_INDIRECT: switch on another reply if defined or handled
4469 * as no payload if NULL. the TXN status code is set with the status
4470 * of the original reply.
4471 */
4472
4473 if (reply->type == HTTP_REPLY_INDIRECT) {
4474 if (reply->body.reply)
4475 reply = reply->body.reply;
4476 }
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004477 if (reply->type == HTTP_REPLY_ERRMSG && !reply->body.errmsg) {
4478 /* get default error message */
4479 if (reply == s->txn->http_reply)
4480 s->txn->http_reply = NULL;
4481 reply = http_error_message(s);
4482 if (reply->type == HTTP_REPLY_INDIRECT) {
4483 if (reply->body.reply)
4484 reply = reply->body.reply;
4485 }
4486 }
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004487
4488 if (reply->type == HTTP_REPLY_ERRMSG) {
4489 /* implicit or explicit error message*/
4490 errmsg = reply->body.errmsg;
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004491 if (errmsg && !b_is_null(errmsg)) {
Christopher Faulet20567362020-05-15 14:52:49 +02004492 if (!htx_copy_msg(htx, errmsg))
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004493 goto fail;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004494 }
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004495 }
4496 else {
4497 /* no payload, file or log-format string */
4498 if (reply->type == HTTP_REPLY_RAW) {
4499 /* file */
4500 body = &reply->body.obj;
4501 }
4502 else if (reply->type == HTTP_REPLY_LOGFMT) {
4503 /* log-format string */
4504 body = alloc_trash_chunk();
4505 if (!body)
4506 goto fail_alloc;
4507 body->data = build_logline(s, body->area, body->size, &reply->body.fmt);
4508 }
4509 /* else no payload */
4510
4511 status = ultoa(reply->status);
4512 reason = http_get_reason(reply->status);
4513 slflags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_CLEN);
4514 if (!body || !b_data(body))
4515 slflags |= HTX_SL_F_BODYLESS;
4516 sl = htx_add_stline(htx, HTX_BLK_RES_SL, slflags, ist("HTTP/1.1"), ist(status), ist(reason));
4517 if (!sl)
4518 goto fail;
4519 sl->info.res.status = reply->status;
4520
4521 clen = (body ? ultoa(b_data(body)) : "0");
4522 ctype = reply->ctype;
4523
4524 if (!LIST_ISEMPTY(&reply->hdrs)) {
4525 struct http_reply_hdr *hdr;
4526 struct buffer *value = alloc_trash_chunk();
4527
4528 if (!value)
4529 goto fail;
4530
4531 list_for_each_entry(hdr, &reply->hdrs, list) {
4532 chunk_reset(value);
4533 value->data = build_logline(s, value->area, value->size, &hdr->value);
4534 if (b_data(value) && !htx_add_header(htx, hdr->name, ist2(b_head(value), b_data(value)))) {
4535 free_trash_chunk(value);
4536 goto fail;
4537 }
4538 chunk_reset(value);
4539 }
4540 free_trash_chunk(value);
4541 }
4542
4543 if (!htx_add_header(htx, ist("content-length"), ist(clen)) ||
4544 (body && b_data(body) && ctype && !htx_add_header(htx, ist("content-type"), ist(ctype))) ||
4545 !htx_add_endof(htx, HTX_BLK_EOH) ||
4546 (body && b_data(body) && !htx_add_data_atonce(htx, ist2(b_head(body), b_data(body)))) ||
4547 !htx_add_endof(htx, HTX_BLK_EOM))
4548 goto fail;
4549 }
4550
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004551 leave:
4552 if (reply->type == HTTP_REPLY_LOGFMT)
4553 free_trash_chunk(body);
4554 return ret;
4555
4556 fail_alloc:
4557 if (!(s->flags & SF_ERR_MASK))
4558 s->flags |= SF_ERR_RESOURCE;
Christopher Faulet97e466c2020-05-15 15:12:47 +02004559 /* fall through */
4560 fail:
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004561 ret = -1;
4562 goto leave;
Christopher Faulet97e466c2020-05-15 15:12:47 +02004563}
4564
4565/* Send an http reply to the client. On success, it returns 0. If an error
Christopher Faulet40e6b552020-06-25 16:04:50 +02004566 * occurs -1 is returned and the response channel is truncated, removing this
4567 * way the faulty reply. This function may fail when the reply is formatted
4568 * (http_reply_to_htx) or when the reply is forwarded
4569 * (http_forward_proxy_resp). On the last case, it is because a
4570 * http-after-response rule fails.
Christopher Faulet97e466c2020-05-15 15:12:47 +02004571 */
4572int http_reply_message(struct stream *s, struct http_reply *reply)
4573{
4574 struct channel *res = &s->res;
4575 struct htx *htx = htx_from_buf(&res->buf);
4576
4577 if (s->txn->status == -1)
4578 s->txn->status = reply->status;
4579 channel_htx_truncate(res, htx);
4580
4581 if (http_reply_to_htx(s, htx, reply) == -1)
4582 goto fail;
4583
4584 htx_to_buf(htx, &s->res.buf);
4585 if (!http_forward_proxy_resp(s, 1))
4586 goto fail;
4587 return 0;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004588
4589 fail:
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004590 channel_htx_truncate(res, htx);
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004591 if (!(s->flags & SF_ERR_MASK))
4592 s->flags |= SF_ERR_PRXCOND;
Christopher Faulet97e466c2020-05-15 15:12:47 +02004593 return -1;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004594}
4595
Christopher Faulet304cc402019-07-15 15:46:28 +02004596/* Return the error message corresponding to si->err_type. It is assumed
4597 * that the server side is closed. Note that err_type is actually a
4598 * bitmask, where almost only aborts may be cumulated with other
4599 * values. We consider that aborted operations are more important
4600 * than timeouts or errors due to the fact that nobody else in the
4601 * logs might explain incomplete retries. All others should avoid
4602 * being cumulated. It should normally not be possible to have multiple
4603 * aborts at once, but just in case, the first one in sequence is reported.
4604 * Note that connection errors appearing on the second request of a keep-alive
4605 * connection are not reported since this allows the client to retry.
4606 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004607void http_return_srv_error(struct stream *s, struct stream_interface *si)
Christopher Faulet304cc402019-07-15 15:46:28 +02004608{
4609 int err_type = si->err_type;
4610
4611 /* set s->txn->status for http_error_message(s) */
4612 s->txn->status = 503;
4613
4614 if (err_type & SI_ET_QUEUE_ABRT)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004615 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_Q,
4616 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004617 else if (err_type & SI_ET_CONN_ABRT)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004618 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_C,
4619 (s->txn->flags & TX_NOT_FIRST) ? NULL :
4620 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004621 else if (err_type & SI_ET_QUEUE_TO)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004622 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_Q,
4623 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004624 else if (err_type & SI_ET_QUEUE_ERR)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004625 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_Q,
4626 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004627 else if (err_type & SI_ET_CONN_TO)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004628 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_C,
4629 (s->txn->flags & TX_NOT_FIRST) ? NULL :
4630 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004631 else if (err_type & SI_ET_CONN_ERR)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004632 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_C,
4633 (s->flags & SF_SRV_REUSED) ? NULL :
4634 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004635 else if (err_type & SI_ET_CONN_RES)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004636 http_server_error(s, si, SF_ERR_RESOURCE, SF_FINST_C,
4637 (s->txn->flags & TX_NOT_FIRST) ? NULL :
4638 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004639 else { /* SI_ET_CONN_OTHER and others */
4640 s->txn->status = 500;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004641 http_server_error(s, si, SF_ERR_INTERNAL, SF_FINST_C,
4642 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004643 }
4644}
4645
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004646
Christopher Faulet4a28a532019-03-01 11:19:40 +01004647/* Handle Expect: 100-continue for HTTP/1.1 messages if necessary. It returns 0
4648 * on success and -1 on error.
4649 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004650static int http_handle_expect_hdr(struct stream *s, struct htx *htx, struct http_msg *msg)
Christopher Faulet4a28a532019-03-01 11:19:40 +01004651{
4652 /* If we have HTTP/1.1 message with a body and Expect: 100-continue,
4653 * then we must send an HTTP/1.1 100 Continue intermediate response.
4654 */
4655 if (msg->msg_state == HTTP_MSG_BODY && (msg->flags & HTTP_MSGF_VER_11) &&
4656 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
4657 struct ist hdr = { .ptr = "Expect", .len = 6 };
4658 struct http_hdr_ctx ctx;
4659
4660 ctx.blk = NULL;
4661 /* Expect is allowed in 1.1, look for it */
4662 if (http_find_header(htx, hdr, &ctx, 0) &&
4663 unlikely(isteqi(ctx.value, ist2("100-continue", 12)))) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004664 if (http_reply_100_continue(s) == -1)
Christopher Faulet4a28a532019-03-01 11:19:40 +01004665 return -1;
4666 http_remove_header(htx, &ctx);
4667 }
4668 }
4669 return 0;
4670}
4671
Christopher Faulet23a3c792018-11-28 10:01:23 +01004672/* Send a 100-Continue response to the client. It returns 0 on success and -1
4673 * on error. The response channel is updated accordingly.
4674 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004675static int http_reply_100_continue(struct stream *s)
Christopher Faulet23a3c792018-11-28 10:01:23 +01004676{
4677 struct channel *res = &s->res;
4678 struct htx *htx = htx_from_buf(&res->buf);
4679 struct htx_sl *sl;
4680 unsigned int flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|
4681 HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
Christopher Faulet23a3c792018-11-28 10:01:23 +01004682
4683 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
4684 ist("HTTP/1.1"), ist("100"), ist("Continue"));
4685 if (!sl)
4686 goto fail;
4687 sl->info.res.status = 100;
4688
Christopher Faulet1d5ec092019-06-26 14:23:54 +02004689 if (!htx_add_endof(htx, HTX_BLK_EOH))
Christopher Faulet23a3c792018-11-28 10:01:23 +01004690 goto fail;
4691
Christopher Fauleta72a7e42020-01-28 09:28:11 +01004692 if (!http_forward_proxy_resp(s, 0))
4693 goto fail;
Christopher Faulet23a3c792018-11-28 10:01:23 +01004694 return 0;
4695
4696 fail:
4697 /* If an error occurred, remove the incomplete HTTP response from the
4698 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004699 channel_htx_truncate(res, htx);
Christopher Faulet23a3c792018-11-28 10:01:23 +01004700 return -1;
4701}
4702
Christopher Faulet12c51e22018-11-28 15:59:42 +01004703
Christopher Faulet0f226952018-10-22 09:29:56 +02004704/*
4705 * Capture headers from message <htx> according to header list <cap_hdr>, and
4706 * fill the <cap> pointers appropriately.
4707 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004708static void http_capture_headers(struct htx *htx, char **cap, struct cap_hdr *cap_hdr)
Christopher Faulet0f226952018-10-22 09:29:56 +02004709{
4710 struct cap_hdr *h;
4711 int32_t pos;
4712
Christopher Fauleta3f15502019-05-13 15:27:23 +02004713 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet0f226952018-10-22 09:29:56 +02004714 struct htx_blk *blk = htx_get_blk(htx, pos);
4715 enum htx_blk_type type = htx_get_blk_type(blk);
4716 struct ist n, v;
4717
4718 if (type == HTX_BLK_EOH)
4719 break;
4720 if (type != HTX_BLK_HDR)
4721 continue;
4722
4723 n = htx_get_blk_name(htx, blk);
4724
4725 for (h = cap_hdr; h; h = h->next) {
4726 if (h->namelen && (h->namelen == n.len) &&
4727 (strncasecmp(n.ptr, h->name, h->namelen) == 0)) {
4728 if (cap[h->index] == NULL)
4729 cap[h->index] =
4730 pool_alloc(h->pool);
4731
4732 if (cap[h->index] == NULL) {
4733 ha_alert("HTTP capture : out of memory.\n");
4734 break;
4735 }
4736
4737 v = htx_get_blk_value(htx, blk);
4738 if (v.len > h->len)
4739 v.len = h->len;
4740
4741 memcpy(cap[h->index], v.ptr, v.len);
4742 cap[h->index][v.len]=0;
4743 }
4744 }
4745 }
4746}
4747
Christopher Faulet0b6bdc52018-10-24 11:05:36 +02004748/* Delete a value in a header between delimiters <from> and <next>. The header
4749 * itself is delimited by <start> and <end> pointers. The number of characters
4750 * displaced is returned, and the pointer to the first delimiter is updated if
4751 * required. The function tries as much as possible to respect the following
4752 * principles :
4753 * - replace <from> delimiter by the <next> one unless <from> points to <start>,
4754 * in which case <next> is simply removed
4755 * - set exactly one space character after the new first delimiter, unless there
4756 * are not enough characters in the block being moved to do so.
4757 * - remove unneeded spaces before the previous delimiter and after the new
4758 * one.
4759 *
4760 * It is the caller's responsibility to ensure that :
4761 * - <from> points to a valid delimiter or <start> ;
4762 * - <next> points to a valid delimiter or <end> ;
4763 * - there are non-space chars before <from>.
4764 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004765static int http_del_hdr_value(char *start, char *end, char **from, char *next)
Christopher Faulet0b6bdc52018-10-24 11:05:36 +02004766{
4767 char *prev = *from;
4768
4769 if (prev == start) {
4770 /* We're removing the first value. eat the semicolon, if <next>
4771 * is lower than <end> */
4772 if (next < end)
4773 next++;
4774
4775 while (next < end && HTTP_IS_SPHT(*next))
4776 next++;
4777 }
4778 else {
4779 /* Remove useless spaces before the old delimiter. */
4780 while (HTTP_IS_SPHT(*(prev-1)))
4781 prev--;
4782 *from = prev;
4783
4784 /* copy the delimiter and if possible a space if we're
4785 * not at the end of the line.
4786 */
4787 if (next < end) {
4788 *prev++ = *next++;
4789 if (prev + 1 < next)
4790 *prev++ = ' ';
4791 while (next < end && HTTP_IS_SPHT(*next))
4792 next++;
4793 }
4794 }
4795 memmove(prev, next, end - next);
4796 return (prev - next);
4797}
4798
Christopher Faulet0f226952018-10-22 09:29:56 +02004799
4800/* Formats the start line of the request (without CRLF) and puts it in <str> and
Joseph Herlantc42c0e92018-11-25 10:43:27 -08004801 * return the written length. The line can be truncated if it exceeds <len>.
Christopher Faulet0f226952018-10-22 09:29:56 +02004802 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004803static size_t http_fmt_req_line(const struct htx_sl *sl, char *str, size_t len)
Christopher Faulet0f226952018-10-22 09:29:56 +02004804{
4805 struct ist dst = ist2(str, 0);
4806
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004807 if (istcat(&dst, htx_sl_req_meth(sl), len) == -1)
Christopher Faulet0f226952018-10-22 09:29:56 +02004808 goto end;
4809 if (dst.len + 1 > len)
4810 goto end;
4811 dst.ptr[dst.len++] = ' ';
4812
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004813 if (istcat(&dst, htx_sl_req_uri(sl), len) == -1)
Christopher Faulet0f226952018-10-22 09:29:56 +02004814 goto end;
4815 if (dst.len + 1 > len)
4816 goto end;
4817 dst.ptr[dst.len++] = ' ';
4818
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004819 istcat(&dst, htx_sl_req_vsn(sl), len);
Christopher Faulet0f226952018-10-22 09:29:56 +02004820 end:
4821 return dst.len;
4822}
4823
4824/*
4825 * Print a debug line with a start line.
4826 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004827static void http_debug_stline(const char *dir, struct stream *s, const struct htx_sl *sl)
Christopher Faulet0f226952018-10-22 09:29:56 +02004828{
4829 struct session *sess = strm_sess(s);
4830 int max;
4831
4832 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
4833 dir,
4834 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->handle.fd : -1,
4835 objt_cs(s->si[1].end) ? (unsigned short)objt_cs(s->si[1].end)->conn->handle.fd : -1);
4836
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004837 max = HTX_SL_P1_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02004838 UBOUND(max, trash.size - trash.data - 3);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004839 chunk_memcat(&trash, HTX_SL_P1_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02004840 trash.area[trash.data++] = ' ';
4841
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004842 max = HTX_SL_P2_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02004843 UBOUND(max, trash.size - trash.data - 2);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004844 chunk_memcat(&trash, HTX_SL_P2_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02004845 trash.area[trash.data++] = ' ';
4846
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004847 max = HTX_SL_P3_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02004848 UBOUND(max, trash.size - trash.data - 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004849 chunk_memcat(&trash, HTX_SL_P3_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02004850 trash.area[trash.data++] = '\n';
4851
Willy Tarreau2e8ab6b2020-03-14 11:03:20 +01004852 DISGUISE(write(1, trash.area, trash.data));
Christopher Faulet0f226952018-10-22 09:29:56 +02004853}
4854
4855/*
4856 * Print a debug line with a header.
4857 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004858static 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 +02004859{
4860 struct session *sess = strm_sess(s);
4861 int max;
4862
4863 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
4864 dir,
4865 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->handle.fd : -1,
4866 objt_cs(s->si[1].end) ? (unsigned short)objt_cs(s->si[1].end)->conn->handle.fd : -1);
4867
4868 max = n.len;
4869 UBOUND(max, trash.size - trash.data - 3);
4870 chunk_memcat(&trash, n.ptr, max);
4871 trash.area[trash.data++] = ':';
4872 trash.area[trash.data++] = ' ';
4873
4874 max = v.len;
4875 UBOUND(max, trash.size - trash.data - 1);
4876 chunk_memcat(&trash, v.ptr, max);
4877 trash.area[trash.data++] = '\n';
4878
Willy Tarreau2e8ab6b2020-03-14 11:03:20 +01004879 DISGUISE(write(1, trash.area, trash.data));
Christopher Faulet0f226952018-10-22 09:29:56 +02004880}
4881
Christopher Fauleta8a46e22019-07-16 14:53:09 +02004882/* Allocate a new HTTP transaction for stream <s> unless there is one already.
4883 * In case of allocation failure, everything allocated is freed and NULL is
4884 * returned. Otherwise the new transaction is assigned to the stream and
4885 * returned.
4886 */
4887struct http_txn *http_alloc_txn(struct stream *s)
4888{
4889 struct http_txn *txn = s->txn;
4890
4891 if (txn)
4892 return txn;
4893
4894 txn = pool_alloc(pool_head_http_txn);
4895 if (!txn)
4896 return txn;
4897
4898 s->txn = txn;
4899 return txn;
4900}
4901
4902void http_txn_reset_req(struct http_txn *txn)
4903{
Christopher Faulet1aea50e2020-01-17 16:03:53 +01004904 txn->req.flags = 0;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02004905 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
4906}
4907
4908void http_txn_reset_res(struct http_txn *txn)
4909{
Christopher Faulet1aea50e2020-01-17 16:03:53 +01004910 txn->rsp.flags = 0;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02004911 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
4912}
4913
4914/*
4915 * Initialize a new HTTP transaction for stream <s>. It is assumed that all
4916 * the required fields are properly allocated and that we only need to (re)init
4917 * them. This should be used before processing any new request.
4918 */
4919void http_init_txn(struct stream *s)
4920{
4921 struct http_txn *txn = s->txn;
4922 struct conn_stream *cs = objt_cs(s->si[0].end);
4923
Christopher Fauletda831fa2020-10-06 17:58:43 +02004924 txn->flags = ((cs && cs->flags & CS_FL_NOT_FIRST) ? TX_NOT_FIRST : 0);
Christopher Fauleta8a46e22019-07-16 14:53:09 +02004925 txn->status = -1;
Christopher Faulet5cb513a2020-05-13 17:56:56 +02004926 txn->http_reply = NULL;
Willy Tarreau8b507582020-02-25 09:35:07 +01004927 write_u32(txn->cache_hash, 0);
Christopher Fauleta8a46e22019-07-16 14:53:09 +02004928
4929 txn->cookie_first_date = 0;
4930 txn->cookie_last_date = 0;
4931
4932 txn->srv_cookie = NULL;
4933 txn->cli_cookie = NULL;
4934 txn->uri = NULL;
4935
4936 http_txn_reset_req(txn);
4937 http_txn_reset_res(txn);
4938
4939 txn->req.chn = &s->req;
4940 txn->rsp.chn = &s->res;
4941
4942 txn->auth.method = HTTP_AUTH_UNKNOWN;
4943
4944 vars_init(&s->vars_txn, SCOPE_TXN);
4945 vars_init(&s->vars_reqres, SCOPE_REQ);
4946}
4947
4948/* to be used at the end of a transaction */
4949void http_end_txn(struct stream *s)
4950{
4951 struct http_txn *txn = s->txn;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02004952
4953 /* these ones will have been dynamically allocated */
4954 pool_free(pool_head_requri, txn->uri);
4955 pool_free(pool_head_capture, txn->cli_cookie);
4956 pool_free(pool_head_capture, txn->srv_cookie);
Tim Duesterhusa17e6622020-03-05 20:19:02 +01004957 pool_free(pool_head_uniqueid, s->unique_id.ptr);
Christopher Fauleta8a46e22019-07-16 14:53:09 +02004958
Tim Duesterhusa17e6622020-03-05 20:19:02 +01004959 s->unique_id = IST_NULL;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02004960 txn->uri = NULL;
4961 txn->srv_cookie = NULL;
4962 txn->cli_cookie = NULL;
4963
Christopher Faulet59399252019-11-07 14:27:52 +01004964 if (!LIST_ISEMPTY(&s->vars_txn.head))
4965 vars_prune(&s->vars_txn, s->sess, s);
4966 if (!LIST_ISEMPTY(&s->vars_reqres.head))
4967 vars_prune(&s->vars_reqres, s->sess, s);
4968}
4969
Christopher Fauleta8a46e22019-07-16 14:53:09 +02004970
4971DECLARE_POOL(pool_head_http_txn, "http_txn", sizeof(struct http_txn));
Christopher Faulet0f226952018-10-22 09:29:56 +02004972
Christopher Fauletf4eb75d2018-10-11 15:55:07 +02004973__attribute__((constructor))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004974static void __http_protocol_init(void)
Christopher Fauletf4eb75d2018-10-11 15:55:07 +02004975{
4976}
4977
4978
4979/*
4980 * Local variables:
4981 * c-indent-level: 8
4982 * c-basic-offset: 8
4983 * End:
4984 */