blob: 5dfd41bde61cd3ee5fc3607071ffab1a7120117d [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 Faulet2a408542020-11-20 14:22:37 +0100152 if (sl->flags & HTX_SL_F_CLEN)
153 msg->flags |= HTTP_MSGF_CNT_LEN;
154 else if (sl->flags & HTX_SL_F_CHNK)
155 msg->flags |= HTTP_MSGF_TE_CHNK;
Christopher Fauletb2db4fa2018-11-27 16:51:09 +0100156 if (sl->flags & HTX_SL_F_BODYLESS)
157 msg->flags |= HTTP_MSGF_BODYLESS;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200158
159 /* we can make use of server redirect on GET and HEAD */
160 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
161 s->flags |= SF_REDIRECTABLE;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100162 else if (txn->meth == HTTP_METH_OTHER && isteqi(htx_sl_req_meth(sl), ist("PRI"))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200163 /* PRI is reserved for the HTTP/2 preface */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200164 goto return_bad_req;
165 }
166
167 /*
Christopher Faulet6072beb2020-02-18 15:34:58 +0100168 * 2: check if the URI matches the monitor_uri. We have to do this for
169 * every request which gets in, because the monitor-uri is defined by
170 * the frontend. If the monitor-uri starts with a '/', the matching is
171 * done against the request's path. Otherwise, the request's uri is
172 * used. It is a workaround to let HTTP/2 health-checks work as
173 * expected.
Christopher Faulete0768eb2018-10-03 16:38:02 +0200174 */
175 if (unlikely((sess->fe->monitor_uri_len != 0) &&
Christopher Faulet6072beb2020-02-18 15:34:58 +0100176 ((*sess->fe->monitor_uri == '/' && isteq(http_get_path(htx_sl_req_uri(sl)),
177 ist2(sess->fe->monitor_uri, sess->fe->monitor_uri_len))) ||
178 isteq(htx_sl_req_uri(sl), ist2(sess->fe->monitor_uri, sess->fe->monitor_uri_len))))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200179 /*
180 * We have found the monitor URI
181 */
182 struct acl_cond *cond;
183
184 s->flags |= SF_MONITOR;
Olivier Houcharda798bf52019-03-08 18:52:00 +0100185 _HA_ATOMIC_ADD(&sess->fe->fe_counters.intercepted_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200186
187 /* Check if we want to fail this monitor request or not */
188 list_for_each_entry(cond, &sess->fe->mon_fail_cond, list) {
189 int ret = acl_exec_cond(cond, sess->fe, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
190
191 ret = acl_pass(ret);
192 if (cond->pol == ACL_COND_UNLESS)
193 ret = !ret;
194
195 if (ret) {
196 /* we fail this request, let's return 503 service unavail */
197 txn->status = 503;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200198 if (!(s->flags & SF_ERR_MASK))
199 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
200 goto return_prx_cond;
201 }
202 }
203
Joseph Herlantc42c0e92018-11-25 10:43:27 -0800204 /* nothing to fail, let's reply normally */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200205 txn->status = 200;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200206 if (!(s->flags & SF_ERR_MASK))
207 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
208 goto return_prx_cond;
209 }
210
211 /*
212 * 3: Maybe we have to copy the original REQURI for the logs ?
213 * Note: we cannot log anymore if the request has been
214 * classified as invalid.
215 */
216 if (unlikely(s->logs.logwait & LW_REQ)) {
217 /* we have a complete HTTP request that we must log */
218 if ((txn->uri = pool_alloc(pool_head_requri)) != NULL) {
Christopher Faulet9768c262018-10-22 09:34:31 +0200219 size_t len;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200220
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200221 len = http_fmt_req_line(sl, txn->uri, global.tune.requri_len - 1);
Christopher Faulet9768c262018-10-22 09:34:31 +0200222 txn->uri[len] = 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200223
224 if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT)))
225 s->do_log(s);
226 } else {
227 ha_alert("HTTP logging : out of memory.\n");
228 }
229 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200230
Christopher Faulete0768eb2018-10-03 16:38:02 +0200231 /* if the frontend has "option http-use-proxy-header", we'll check if
232 * we have what looks like a proxied connection instead of a connection,
233 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
234 * Note that this is *not* RFC-compliant, however browsers and proxies
235 * happen to do that despite being non-standard :-(
236 * We consider that a request not beginning with either '/' or '*' is
237 * a proxied connection, which covers both "scheme://location" and
238 * CONNECT ip:port.
239 */
240 if ((sess->fe->options2 & PR_O2_USE_PXHDR) &&
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100241 *HTX_SL_REQ_UPTR(sl) != '/' && *HTX_SL_REQ_UPTR(sl) != '*')
Christopher Faulete0768eb2018-10-03 16:38:02 +0200242 txn->flags |= TX_USE_PX_CONN;
243
Christopher Faulete0768eb2018-10-03 16:38:02 +0200244 /* 5: we may need to capture headers */
245 if (unlikely((s->logs.logwait & LW_REQHDR) && s->req_cap))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200246 http_capture_headers(htx, s->req_cap, sess->fe->req_cap);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200247
Christopher Faulete0768eb2018-10-03 16:38:02 +0200248 /* we may have to wait for the request's body */
Christopher Faulet9768c262018-10-22 09:34:31 +0200249 if (s->be->options & PR_O_WREQ_BODY)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200250 req->analysers |= AN_REQ_HTTP_BODY;
251
252 /*
253 * RFC7234#4:
254 * A cache MUST write through requests with methods
255 * that are unsafe (Section 4.2.1 of [RFC7231]) to
256 * the origin server; i.e., a cache is not allowed
257 * to generate a reply to such a request before
258 * having forwarded the request and having received
259 * a corresponding response.
260 *
261 * RFC7231#4.2.1:
262 * Of the request methods defined by this
263 * specification, the GET, HEAD, OPTIONS, and TRACE
264 * methods are defined to be safe.
265 */
266 if (likely(txn->meth == HTTP_METH_GET ||
267 txn->meth == HTTP_METH_HEAD ||
268 txn->meth == HTTP_METH_OPTIONS ||
269 txn->meth == HTTP_METH_TRACE))
270 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
271
272 /* end of job, return OK */
273 req->analysers &= ~an_bit;
274 req->analyse_exp = TICK_ETERNITY;
Christopher Faulet9768c262018-10-22 09:34:31 +0200275
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100276 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200277 return 1;
278
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200279 return_int_err:
280 txn->status = 500;
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200281 if (!(s->flags & SF_ERR_MASK))
282 s->flags |= SF_ERR_INTERNAL;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100283 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200284 if (sess->listener->counters)
Christopher Fauletb8a53712019-12-16 11:29:38 +0100285 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200286 goto return_prx_cond;
287
Christopher Faulete0768eb2018-10-03 16:38:02 +0200288 return_bad_req:
Christopher Faulet9768c262018-10-22 09:34:31 +0200289 txn->status = 400;
Olivier Houcharda798bf52019-03-08 18:52:00 +0100290 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200291 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100292 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200293 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200294
295 return_prx_cond:
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200296 http_reply_and_close(s, txn->status, http_error_message(s));
297
Christopher Faulete0768eb2018-10-03 16:38:02 +0200298 if (!(s->flags & SF_ERR_MASK))
299 s->flags |= SF_ERR_PRXCOND;
300 if (!(s->flags & SF_FINST_MASK))
301 s->flags |= SF_FINST_R;
302
303 req->analysers &= AN_REQ_FLT_END;
304 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100305 DBG_TRACE_DEVEL("leaving on error",
306 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200307 return 0;
308}
309
310
311/* This stream analyser runs all HTTP request processing which is common to
312 * frontends and backends, which means blocking ACLs, filters, connection-close,
313 * reqadd, stats and redirects. This is performed for the designated proxy.
314 * It returns 1 if the processing can continue on next analysers, or zero if it
315 * either needs more data or wants to immediately abort the request (eg: deny,
316 * error, ...).
317 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200318int http_process_req_common(struct stream *s, struct channel *req, int an_bit, struct proxy *px)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200319{
320 struct session *sess = s->sess;
321 struct http_txn *txn = s->txn;
322 struct http_msg *msg = &txn->req;
Christopher Fauletff2759f2018-10-24 11:13:16 +0200323 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200324 struct redirect_rule *rule;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200325 enum rule_result verdict;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200326 struct connection *conn = objt_conn(sess->origin);
327
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100328 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200329
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100330 htx = htxbuf(&req->buf);
Christopher Fauletff2759f2018-10-24 11:13:16 +0200331
Christopher Faulet1907ccc2019-04-29 13:12:02 +0200332 /* just in case we have some per-backend tracking. Only called the first
333 * execution of the analyser. */
334 if (!s->current_rule || s->current_rule_list != &px->http_req_rules)
335 stream_inc_be_http_req_ctr(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200336
337 /* evaluate http-request rules */
338 if (!LIST_ISEMPTY(&px->http_req_rules)) {
Christopher Fauletb58f62b2020-01-13 16:40:13 +0100339 verdict = http_req_get_intercept_rule(px, &px->http_req_rules, s);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200340
341 switch (verdict) {
342 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
343 goto return_prx_yield;
344
345 case HTTP_RULE_RES_CONT:
346 case HTTP_RULE_RES_STOP: /* nothing to do */
347 break;
348
349 case HTTP_RULE_RES_DENY: /* deny or tarpit */
350 if (txn->flags & TX_CLTARPIT)
351 goto tarpit;
352 goto deny;
353
354 case HTTP_RULE_RES_ABRT: /* abort request, response already sent. Eg: auth */
355 goto return_prx_cond;
356
357 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
358 goto done;
359
360 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
361 goto return_bad_req;
Christopher Faulet3a26bee2019-12-16 12:47:40 +0100362
363 case HTTP_RULE_RES_ERROR: /* failed with a bad request */
364 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200365 }
366 }
367
368 if (conn && (conn->flags & CO_FL_EARLY_DATA) &&
Olivier Houchard220a26c2020-01-23 14:57:36 +0100369 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_SSL_WAIT_HS))) {
Christopher Fauletff2759f2018-10-24 11:13:16 +0200370 struct http_hdr_ctx ctx;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200371
Christopher Fauletff2759f2018-10-24 11:13:16 +0200372 ctx.blk = NULL;
373 if (!http_find_header(htx, ist("Early-Data"), &ctx, 0)) {
374 if (unlikely(!http_add_header(htx, ist("Early-Data"), ist("1"))))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100375 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200376 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200377 }
378
379 /* OK at this stage, we know that the request was accepted according to
380 * the http-request rules, we can check for the stats. Note that the
381 * URI is detected *before* the req* rules in order not to be affected
382 * by a possible reqrep, while they are processed *after* so that a
383 * reqdeny can still block them. This clearly needs to change in 1.6!
384 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200385 if (!s->target && http_stats_check_uri(s, txn, px)) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200386 s->target = &http_stats_applet.obj_type;
Willy Tarreau14bfe9a2018-12-19 15:19:27 +0100387 if (unlikely(!si_register_handler(&s->si[1], objt_applet(s->target)))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200388 s->logs.tv_request = now;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200389 if (!(s->flags & SF_ERR_MASK))
390 s->flags |= SF_ERR_RESOURCE;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100391 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200392 }
393
394 /* parse the whole stats request and extract the relevant information */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200395 http_handle_stats(s, req);
Christopher Fauletb58f62b2020-01-13 16:40:13 +0100396 verdict = http_req_get_intercept_rule(px, &px->uri_auth->http_req_rules, s);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200397 /* not all actions implemented: deny, allow, auth */
398
399 if (verdict == HTTP_RULE_RES_DENY) /* stats http-request deny */
400 goto deny;
401
402 if (verdict == HTTP_RULE_RES_ABRT) /* stats auth / stats http-request auth */
403 goto return_prx_cond;
Christopher Faulet3a26bee2019-12-16 12:47:40 +0100404
405 if (verdict == HTTP_RULE_RES_BADREQ) /* failed with a bad request */
406 goto return_bad_req;
407
408 if (verdict == HTTP_RULE_RES_ERROR) /* failed with a bad request */
409 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200410 }
411
Christopher Faulet2571bc62019-03-01 11:44:26 +0100412 /* Proceed with the applets now. */
413 if (unlikely(objt_applet(s->target))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200414 if (sess->fe == s->be) /* report it if the request was intercepted by the frontend */
Olivier Houcharda798bf52019-03-08 18:52:00 +0100415 _HA_ATOMIC_ADD(&sess->fe->fe_counters.intercepted_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200416
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200417 if (http_handle_expect_hdr(s, htx, msg) == -1)
Christopher Fauletb8a53712019-12-16 11:29:38 +0100418 goto return_int_err;
Christopher Fauletbcf242a2019-03-01 11:36:26 +0100419
Christopher Faulete0768eb2018-10-03 16:38:02 +0200420 if (!(s->flags & SF_ERR_MASK)) // this is not really an error but it is
421 s->flags |= SF_ERR_LOCAL; // to mark that it comes from the proxy
422 if (!(s->flags & SF_FINST_MASK))
423 s->flags |= SF_FINST_R;
424
425 /* enable the minimally required analyzers to handle keep-alive and compression on the HTTP response */
426 req->analysers &= (AN_REQ_HTTP_BODY | AN_REQ_FLT_HTTP_HDRS | AN_REQ_FLT_END);
427 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
428 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Christopher Fauletbcf242a2019-03-01 11:36:26 +0100429
430 req->flags |= CF_SEND_DONTWAIT;
431 s->flags |= SF_ASSIGNED;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200432 goto done;
433 }
434
435 /* check whether we have some ACLs set to redirect this request */
436 list_for_each_entry(rule, &px->redirect_rules, list) {
437 if (rule->cond) {
438 int ret;
439
440 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
441 ret = acl_pass(ret);
442 if (rule->cond->pol == ACL_COND_UNLESS)
443 ret = !ret;
444 if (!ret)
445 continue;
446 }
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200447 if (!http_apply_redirect_rule(rule, s, txn))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100448 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200449 goto done;
450 }
451
452 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
453 * If this happens, then the data will not come immediately, so we must
454 * send all what we have without waiting. Note that due to the small gain
455 * in waiting for the body of the request, it's easier to simply put the
456 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
457 * itself once used.
458 */
459 req->flags |= CF_SEND_DONTWAIT;
460
461 done: /* done with this analyser, continue with next ones that the calling
462 * points will have set, if any.
463 */
464 req->analyse_exp = TICK_ETERNITY;
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +0500465 done_without_exp: /* done with this analyser, but don't reset the analyse_exp. */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200466 req->analysers &= ~an_bit;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100467 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200468 return 1;
469
470 tarpit:
471 /* Allow cookie logging
472 */
473 if (s->be->cookie_name || sess->fe->capture_name)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200474 http_manage_client_side_cookies(s, req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200475
476 /* When a connection is tarpitted, we use the tarpit timeout,
477 * which may be the same as the connect timeout if unspecified.
478 * If unset, then set it to zero because we really want it to
479 * eventually expire. We build the tarpit as an analyser.
480 */
Christopher Faulet202c6ce2019-01-07 14:57:35 +0100481 channel_htx_erase(&s->req, htx);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200482
483 /* wipe the request out so that we can drop the connection early
484 * if the client closes first.
485 */
486 channel_dont_connect(req);
487
Christopher Faulete0768eb2018-10-03 16:38:02 +0200488 req->analysers &= AN_REQ_FLT_END; /* remove switching rules etc... */
489 req->analysers |= AN_REQ_HTTP_TARPIT;
490 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
491 if (!req->analyse_exp)
492 req->analyse_exp = tick_add(now_ms, 0);
493 stream_inc_http_err_ctr(s);
Olivier Houcharda798bf52019-03-08 18:52:00 +0100494 _HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100495 if (s->flags & SF_BE_ASSIGNED)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100496 _HA_ATOMIC_ADD(&s->be->be_counters.denied_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200497 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100498 _HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200499 goto done_without_exp;
500
501 deny: /* this request was blocked (denied) */
502
503 /* Allow cookie logging
504 */
505 if (s->be->cookie_name || sess->fe->capture_name)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200506 http_manage_client_side_cookies(s, req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200507
Christopher Faulete0768eb2018-10-03 16:38:02 +0200508 s->logs.tv_request = now;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200509 stream_inc_http_err_ctr(s);
Olivier Houcharda798bf52019-03-08 18:52:00 +0100510 _HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100511 if (s->flags & SF_BE_ASSIGNED)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100512 _HA_ATOMIC_ADD(&s->be->be_counters.denied_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200513 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100514 _HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100515 goto return_prx_err;
516
517 return_int_err:
518 txn->status = 500;
519 if (!(s->flags & SF_ERR_MASK))
520 s->flags |= SF_ERR_INTERNAL;
521 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100522 if (s->flags & SF_BE_ASSIGNED)
523 _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100524 if (sess->listener->counters)
525 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
526 goto return_prx_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200527
528 return_bad_req:
Christopher Faulete0768eb2018-10-03 16:38:02 +0200529 txn->status = 400;
Olivier Houcharda798bf52019-03-08 18:52:00 +0100530 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200531 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100532 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100533 /* fall through */
534
535 return_prx_err:
536 http_reply_and_close(s, txn->status, http_error_message(s));
537 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200538
539 return_prx_cond:
540 if (!(s->flags & SF_ERR_MASK))
541 s->flags |= SF_ERR_PRXCOND;
542 if (!(s->flags & SF_FINST_MASK))
543 s->flags |= SF_FINST_R;
544
545 req->analysers &= AN_REQ_FLT_END;
546 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100547 DBG_TRACE_DEVEL("leaving on error",
548 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200549 return 0;
550
551 return_prx_yield:
552 channel_dont_connect(req);
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100553 DBG_TRACE_DEVEL("waiting for more data",
554 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200555 return 0;
556}
557
558/* This function performs all the processing enabled for the current request.
559 * It returns 1 if the processing can continue on next analysers, or zero if it
560 * needs more data, encounters an error, or wants to immediately abort the
561 * request. It relies on buffers flags, and updates s->req.analysers.
562 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200563int http_process_request(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200564{
565 struct session *sess = s->sess;
566 struct http_txn *txn = s->txn;
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200567 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200568 struct connection *cli_conn = objt_conn(strm_sess(s)->origin);
569
Christopher Faulet8bebd2f2020-10-06 17:54:56 +0200570 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200571
572 /*
573 * Right now, we know that we have processed the entire headers
574 * and that unwanted requests have been filtered out. We can do
575 * whatever we want with the remaining request. Also, now we
576 * may have separate values for ->fe, ->be.
577 */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100578 htx = htxbuf(&req->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200579
580 /*
581 * If HTTP PROXY is set we simply get remote server address parsing
Willy Tarreau1c8d32b2019-07-18 15:47:45 +0200582 * incoming request.
Christopher Faulete0768eb2018-10-03 16:38:02 +0200583 */
584 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SF_ADDR_SET)) {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100585 struct htx_sl *sl;
586 struct ist uri, path;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200587
Willy Tarreau9b7587a2020-10-15 07:32:10 +0200588 if (!sockaddr_alloc(&s->target_addr, NULL, 0)) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200589 if (!(s->flags & SF_ERR_MASK))
590 s->flags |= SF_ERR_RESOURCE;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100591 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200592 }
Christopher Faulet297fbb42019-05-13 14:41:27 +0200593 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100594 uri = htx_sl_req_uri(sl);
595 path = http_get_path(uri);
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200596
Willy Tarreau1c8d32b2019-07-18 15:47:45 +0200597 if (url2sa(uri.ptr, uri.len - path.len, s->target_addr, NULL) == -1)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200598 goto return_bad_req;
599
Willy Tarreau1c8d32b2019-07-18 15:47:45 +0200600 s->target = &s->be->obj_type;
601 s->flags |= SF_ADDR_SET | SF_ASSIGNED;
602
Christopher Faulete0768eb2018-10-03 16:38:02 +0200603 /* if the path was found, we have to remove everything between
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200604 * uri.ptr and path.ptr (excluded). If it was not found, we need
605 * to replace from all the uri by a single "/".
606 *
Ilya Shipitsin46a030c2020-07-05 16:36:08 +0500607 * Instead of rewriting the whole start line, we just update
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100608 * the star-line URI. Some space will be lost but it should be
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200609 * insignificant.
Christopher Faulete0768eb2018-10-03 16:38:02 +0200610 */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100611 istcpy(&uri, (path.len ? path : ist("/")), uri.len);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200612 }
613
614 /*
615 * 7: Now we can work with the cookies.
616 * Note that doing so might move headers in the request, but
617 * the fields will stay coherent and the URI will not move.
618 * This should only be performed in the backend.
619 */
620 if (s->be->cookie_name || sess->fe->capture_name)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200621 http_manage_client_side_cookies(s, req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200622
Tim Duesterhus2825b4b2020-02-28 15:13:34 +0100623 /* 8: Generate unique ID if a "unique-id-format" is defined.
624 *
625 * A unique ID is generated even when it is not sent to ensure that the ID can make use of
626 * fetches only available in the HTTP request processing stage.
627 */
628 if (!LIST_ISEMPTY(&sess->fe->format_unique_id)) {
Tim Duesterhusa17e6622020-03-05 20:19:02 +0100629 struct ist unique_id = stream_generate_unique_id(s, &sess->fe->format_unique_id);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200630
Tim Duesterhusa17e6622020-03-05 20:19:02 +0100631 if (!isttest(unique_id)) {
Christopher Fauletb8a53712019-12-16 11:29:38 +0100632 if (!(s->flags & SF_ERR_MASK))
633 s->flags |= SF_ERR_RESOURCE;
634 goto return_int_err;
635 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200636
Tim Duesterhus2825b4b2020-02-28 15:13:34 +0100637 /* send unique ID if a "unique-id-header" is defined */
Tim Duesterhus0643b0e2020-03-05 17:56:35 +0100638 if (isttest(sess->fe->header_unique_id) &&
Tim Duesterhusa17e6622020-03-05 20:19:02 +0100639 unlikely(!http_add_header(htx, sess->fe->header_unique_id, s->unique_id)))
Tim Duesterhus2825b4b2020-02-28 15:13:34 +0100640 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200641 }
642
643 /*
644 * 9: add X-Forwarded-For if either the frontend or the backend
645 * asks for it.
646 */
647 if ((sess->fe->options | s->be->options) & PR_O_FWDFOR) {
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200648 struct http_hdr_ctx ctx = { .blk = NULL };
649 struct ist hdr = ist2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : sess->fe->fwdfor_hdr_name,
650 s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : sess->fe->fwdfor_hdr_len);
651
Christopher Faulete0768eb2018-10-03 16:38:02 +0200652 if (!((sess->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200653 http_find_header(htx, hdr, &ctx, 0)) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200654 /* The header is set to be added only if none is present
655 * and we found it, so don't do anything.
656 */
657 }
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200658 else if (cli_conn && conn_get_src(cli_conn) && cli_conn->src->ss_family == AF_INET) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200659 /* Add an X-Forwarded-For header unless the source IP is
660 * in the 'except' network range.
661 */
662 if ((!sess->fe->except_mask.s_addr ||
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200663 (((struct sockaddr_in *)cli_conn->src)->sin_addr.s_addr & sess->fe->except_mask.s_addr)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200664 != sess->fe->except_net.s_addr) &&
665 (!s->be->except_mask.s_addr ||
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200666 (((struct sockaddr_in *)cli_conn->src)->sin_addr.s_addr & s->be->except_mask.s_addr)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200667 != s->be->except_net.s_addr)) {
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200668 unsigned char *pn = (unsigned char *)&((struct sockaddr_in *)cli_conn->src)->sin_addr;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200669
670 /* Note: we rely on the backend to get the header name to be used for
671 * x-forwarded-for, because the header is really meant for the backends.
672 * However, if the backend did not specify any option, we have to rely
673 * on the frontend's header name.
674 */
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200675 chunk_printf(&trash, "%d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
676 if (unlikely(!http_add_header(htx, hdr, ist2(trash.area, trash.data))))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100677 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200678 }
679 }
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200680 else if (cli_conn && conn_get_src(cli_conn) && cli_conn->src->ss_family == AF_INET6) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200681 /* FIXME: for the sake of completeness, we should also support
682 * 'except' here, although it is mostly useless in this case.
683 */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200684 char pn[INET6_ADDRSTRLEN];
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200685
Christopher Faulete0768eb2018-10-03 16:38:02 +0200686 inet_ntop(AF_INET6,
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200687 (const void *)&((struct sockaddr_in6 *)(cli_conn->src))->sin6_addr,
Christopher Faulete0768eb2018-10-03 16:38:02 +0200688 pn, sizeof(pn));
689
690 /* Note: we rely on the backend to get the header name to be used for
691 * x-forwarded-for, because the header is really meant for the backends.
692 * However, if the backend did not specify any option, we have to rely
693 * on the frontend's header name.
694 */
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200695 chunk_printf(&trash, "%s", pn);
696 if (unlikely(!http_add_header(htx, hdr, ist2(trash.area, trash.data))))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100697 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200698 }
699 }
700
701 /*
702 * 10: add X-Original-To if either the frontend or the backend
703 * asks for it.
704 */
705 if ((sess->fe->options | s->be->options) & PR_O_ORGTO) {
706
707 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200708 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 +0200709 /* Add an X-Original-To header unless the destination IP is
710 * in the 'except' network range.
711 */
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200712 if (cli_conn->dst->ss_family == AF_INET &&
Christopher Faulete0768eb2018-10-03 16:38:02 +0200713 ((!sess->fe->except_mask_to.s_addr ||
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200714 (((struct sockaddr_in *)cli_conn->dst)->sin_addr.s_addr & sess->fe->except_mask_to.s_addr)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200715 != sess->fe->except_to.s_addr) &&
716 (!s->be->except_mask_to.s_addr ||
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200717 (((struct sockaddr_in *)cli_conn->dst)->sin_addr.s_addr & s->be->except_mask_to.s_addr)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200718 != s->be->except_to.s_addr))) {
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200719 struct ist hdr;
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200720 unsigned char *pn = (unsigned char *)&((struct sockaddr_in *)cli_conn->dst)->sin_addr;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200721
722 /* Note: we rely on the backend to get the header name to be used for
723 * x-original-to, because the header is really meant for the backends.
724 * However, if the backend did not specify any option, we have to rely
725 * on the frontend's header name.
726 */
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200727 if (s->be->orgto_hdr_len)
728 hdr = ist2(s->be->orgto_hdr_name, s->be->orgto_hdr_len);
729 else
730 hdr = ist2(sess->fe->orgto_hdr_name, sess->fe->orgto_hdr_len);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200731
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200732 chunk_printf(&trash, "%d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
733 if (unlikely(!http_add_header(htx, hdr, ist2(trash.area, trash.data))))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100734 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200735 }
736 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200737 }
738
Christopher Faulete0768eb2018-10-03 16:38:02 +0200739 /* If we have no server assigned yet and we're balancing on url_param
740 * with a POST request, we may be interested in checking the body for
741 * that parameter. This will be done in another analyser.
742 */
743 if (!(s->flags & (SF_ASSIGNED|SF_DIRECT)) &&
Willy Tarreau089eaa02019-01-14 15:17:46 +0100744 s->txn->meth == HTTP_METH_POST &&
745 (s->be->lbprm.algo & BE_LB_ALGO) == BE_LB_ALGO_PH) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200746 channel_dont_connect(req);
747 req->analysers |= AN_REQ_HTTP_BODY;
748 }
749
750 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
751 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau1a18b542018-12-11 16:37:42 +0100752
Christopher Faulete0768eb2018-10-03 16:38:02 +0200753 /* We expect some data from the client. Unless we know for sure
754 * we already have a full request, we have to re-enable quick-ack
755 * in case we previously disabled it, otherwise we might cause
756 * the client to delay further data.
757 */
758 if ((sess->listener->options & LI_O_NOQUICKACK) &&
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200759 (htx_get_tail_type(htx) != HTX_BLK_EOM))
Willy Tarreau1a18b542018-12-11 16:37:42 +0100760 conn_set_quickack(cli_conn, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200761
762 /*************************************************************
763 * OK, that's finished for the headers. We have done what we *
764 * could. Let's switch to the DATA state. *
765 ************************************************************/
766 req->analyse_exp = TICK_ETERNITY;
767 req->analysers &= ~an_bit;
768
769 s->logs.tv_request = now;
770 /* OK let's go on with the BODY now */
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100771 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200772 return 1;
773
Christopher Fauletb8a53712019-12-16 11:29:38 +0100774 return_int_err:
775 txn->status = 500;
776 if (!(s->flags & SF_ERR_MASK))
777 s->flags |= SF_ERR_INTERNAL;
778 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100779 if (s->flags & SF_BE_ASSIGNED)
Christopher Fauletbe20cf32020-01-24 11:41:38 +0100780 _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100781 if (sess->listener->counters)
782 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
783 goto return_prx_cond;
784
Christopher Faulete0768eb2018-10-03 16:38:02 +0200785 return_bad_req: /* let's centralize all bad requests */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200786 txn->status = 400;
Olivier Houcharda798bf52019-03-08 18:52:00 +0100787 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200788 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100789 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100790 /* fall through */
791
792 return_prx_cond:
793 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +0200794
795 if (!(s->flags & SF_ERR_MASK))
796 s->flags |= SF_ERR_PRXCOND;
797 if (!(s->flags & SF_FINST_MASK))
798 s->flags |= SF_FINST_R;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100799
800 req->analysers &= AN_REQ_FLT_END;
801 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100802 DBG_TRACE_DEVEL("leaving on error",
803 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200804 return 0;
805}
806
807/* This function is an analyser which processes the HTTP tarpit. It always
808 * returns zero, at the beginning because it prevents any other processing
809 * from occurring, and at the end because it terminates the request.
810 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200811int http_process_tarpit(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200812{
813 struct http_txn *txn = s->txn;
814
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100815 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, &txn->req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200816 /* This connection is being tarpitted. The CLIENT side has
817 * already set the connect expiration date to the right
818 * timeout. We just have to check that the client is still
819 * there and that the timeout has not expired.
820 */
821 channel_dont_connect(req);
822 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100823 !tick_is_expired(req->analyse_exp, now_ms)) {
824 DBG_TRACE_DEVEL("waiting for tarpit timeout expiry",
825 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200826 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100827 }
828
Christopher Faulete0768eb2018-10-03 16:38:02 +0200829
830 /* We will set the queue timer to the time spent, just for
831 * logging purposes. We fake a 500 server error, so that the
832 * attacker will not suspect his connection has been tarpitted.
833 * It will not cause trouble to the logs because we can exclude
834 * the tarpitted connections by filtering on the 'PT' status flags.
835 */
836 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
837
Christopher Faulet8dfeccf2020-05-15 14:16:29 +0200838 http_reply_and_close(s, txn->status, (!(req->flags & CF_READ_ERROR) ? http_error_message(s) : NULL));
Christopher Faulet5cb513a2020-05-13 17:56:56 +0200839
840 end:
Christopher Faulete0768eb2018-10-03 16:38:02 +0200841 req->analysers &= AN_REQ_FLT_END;
842 req->analyse_exp = TICK_ETERNITY;
843
844 if (!(s->flags & SF_ERR_MASK))
845 s->flags |= SF_ERR_PRXCOND;
846 if (!(s->flags & SF_FINST_MASK))
847 s->flags |= SF_FINST_T;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100848
849 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200850 return 0;
851}
852
853/* This function is an analyser which waits for the HTTP request body. It waits
854 * for either the buffer to be full, or the full advertised contents to have
855 * reached the buffer. It must only be called after the standard HTTP request
856 * processing has occurred, because it expects the request to be parsed and will
857 * look for the Expect header. It may send a 100-Continue interim response. It
858 * takes in input any state starting from HTTP_MSG_BODY and leaves with one of
859 * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it
860 * needs to read more data, or 1 once it has completed its analysis.
861 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200862int http_wait_for_request_body(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200863{
864 struct session *sess = s->sess;
865 struct http_txn *txn = s->txn;
866 struct http_msg *msg = &s->txn->req;
Christopher Fauletf76ebe82018-10-24 11:16:22 +0200867 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200868
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100869 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Fauletf76ebe82018-10-24 11:16:22 +0200870
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100871 htx = htxbuf(&req->buf);
Christopher Fauletf76ebe82018-10-24 11:16:22 +0200872
Willy Tarreau4236f032019-03-05 10:43:32 +0100873 if (htx->flags & HTX_FL_PARSING_ERROR)
874 goto return_bad_req;
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200875 if (htx->flags & HTX_FL_PROCESSING_ERROR)
876 goto return_int_err;
Willy Tarreau4236f032019-03-05 10:43:32 +0100877
Christopher Fauletc31bc722020-11-20 14:30:38 +0100878 /* Do nothing for bodyless and CONNECT requests */
879 if (txn->meth == HTTP_METH_CONNECT || (msg->flags & HTTP_MSGF_BODYLESS))
Christopher Faulet63c69a92020-11-16 16:03:35 +0100880 goto http_end;
881
Christopher Faulete0768eb2018-10-03 16:38:02 +0200882 /* We have to parse the HTTP request body to find any required data.
883 * "balance url_param check_post" should have been the only way to get
884 * into this. We were brought here after HTTP header analysis, so all
885 * related structures are ready.
886 */
887
Christopher Fauletf76ebe82018-10-24 11:16:22 +0200888 if (msg->msg_state < HTTP_MSG_DATA) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200889 if (http_handle_expect_hdr(s, htx, msg) == -1)
Christopher Fauletb8a53712019-12-16 11:29:38 +0100890 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200891 }
892
Christopher Fauletf76ebe82018-10-24 11:16:22 +0200893 msg->msg_state = HTTP_MSG_DATA;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200894
Christopher Fauletf76ebe82018-10-24 11:16:22 +0200895 /* Now we're in HTTP_MSG_DATA. We just need to know if all data have
896 * been received or if the buffer is full.
Christopher Faulete0768eb2018-10-03 16:38:02 +0200897 */
Christopher Faulet54b5e212019-06-04 10:08:28 +0200898 if (htx_get_tail_type(htx) > HTX_BLK_DATA ||
Christopher Fauletdcd8c5e2019-01-21 11:24:38 +0100899 channel_htx_full(req, htx, global.tune.maxrewrite))
Christopher Faulete0768eb2018-10-03 16:38:02 +0200900 goto http_end;
901
902 if ((req->flags & CF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
903 txn->status = 408;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200904 if (!(s->flags & SF_ERR_MASK))
905 s->flags |= SF_ERR_CLITO;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100906 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
907 if (sess->listener->counters)
908 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
909 goto return_prx_cond;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200910 }
911
912 /* we get here if we need to wait for more data */
913 if (!(req->flags & (CF_SHUTR | CF_READ_ERROR))) {
914 /* Not enough data. We'll re-use the http-request
915 * timeout here. Ideally, we should set the timeout
916 * relative to the accept() date. We just set the
917 * request timeout once at the beginning of the
918 * request.
919 */
920 channel_dont_connect(req);
921 if (!tick_isset(req->analyse_exp))
922 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100923 DBG_TRACE_DEVEL("waiting for more data",
924 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200925 return 0;
926 }
927
928 http_end:
929 /* The situation will not evolve, so let's give up on the analysis. */
930 s->logs.tv_request = now; /* update the request timer to reflect full request */
931 req->analysers &= ~an_bit;
932 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100933 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200934 return 1;
935
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200936 return_int_err:
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200937 txn->status = 500;
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200938 if (!(s->flags & SF_ERR_MASK))
939 s->flags |= SF_ERR_INTERNAL;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100940 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100941 if (s->flags & SF_BE_ASSIGNED)
Christopher Fauletbe20cf32020-01-24 11:41:38 +0100942 _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100943 if (sess->listener->counters)
944 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
945 goto return_prx_cond;
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200946
Christopher Faulete0768eb2018-10-03 16:38:02 +0200947 return_bad_req: /* let's centralize all bad requests */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200948 txn->status = 400;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100949 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
950 if (sess->listener->counters)
951 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
952 /* fall through */
953
954 return_prx_cond:
955 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +0200956
957 if (!(s->flags & SF_ERR_MASK))
958 s->flags |= SF_ERR_PRXCOND;
959 if (!(s->flags & SF_FINST_MASK))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100960 s->flags |= (msg->msg_state < HTTP_MSG_DATA ? SF_FINST_R : SF_FINST_D);
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200961
Christopher Faulete0768eb2018-10-03 16:38:02 +0200962 req->analysers &= AN_REQ_FLT_END;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100963 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100964 DBG_TRACE_DEVEL("leaving on error",
965 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200966 return 0;
967}
968
969/* This function is an analyser which forwards request body (including chunk
970 * sizes if any). It is called as soon as we must forward, even if we forward
971 * zero byte. The only situation where it must not be called is when we're in
972 * tunnel mode and we want to forward till the close. It's used both to forward
973 * remaining data and to resync after end of body. It expects the msg_state to
974 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
975 * read more data, or 1 once we can go on with next request or end the stream.
976 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
977 * bytes of pending data + the headers if not already done.
978 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200979int http_request_forward_body(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200980{
981 struct session *sess = s->sess;
982 struct http_txn *txn = s->txn;
Christopher Faulet9768c262018-10-22 09:34:31 +0200983 struct http_msg *msg = &txn->req;
984 struct htx *htx;
Christopher Faulet93e02d82019-03-08 14:18:50 +0100985 short status = 0;
Christopher Fauletaed82cf2018-11-30 22:22:32 +0100986 int ret;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200987
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100988 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200989
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100990 htx = htxbuf(&req->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200991
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200992 if (htx->flags & HTX_FL_PARSING_ERROR)
993 goto return_bad_req;
994 if (htx->flags & HTX_FL_PROCESSING_ERROR)
995 goto return_int_err;
996
Christopher Faulete0768eb2018-10-03 16:38:02 +0200997 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
998 ((req->flags & CF_SHUTW) && (req->to_forward || co_data(req)))) {
999 /* Output closed while we were sending data. We must abort and
1000 * wake the other side up.
1001 */
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001002
Olivier Houchard29cac3c2019-07-12 15:48:58 +02001003 /* Don't abort yet if we had L7 retries activated and it
1004 * was a write error, we may recover.
1005 */
1006 if (!(req->flags & (CF_READ_ERROR | CF_READ_TIMEOUT)) &&
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001007 (s->si[1].flags & SI_FL_L7_RETRY)) {
1008 DBG_TRACE_DEVEL("leaving on L7 retry",
1009 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Olivier Houchard29cac3c2019-07-12 15:48:58 +02001010 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001011 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001012 msg->msg_state = HTTP_MSG_ERROR;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001013 http_end_request(s);
1014 http_end_response(s);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001015 DBG_TRACE_DEVEL("leaving on error",
1016 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001017 return 1;
1018 }
1019
1020 /* Note that we don't have to send 100-continue back because we don't
1021 * need the data to complete our job, and it's up to the server to
1022 * decide whether to return 100, 417 or anything else in return of
1023 * an "Expect: 100-continue" header.
1024 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001025 if (msg->msg_state == HTTP_MSG_BODY)
1026 msg->msg_state = HTTP_MSG_DATA;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001027
Christopher Faulete0768eb2018-10-03 16:38:02 +02001028 /* in most states, we should abort in case of early close */
1029 channel_auto_close(req);
1030
1031 if (req->to_forward) {
Christopher Faulet66af0b22019-03-22 14:54:52 +01001032 if (req->to_forward == CHN_INFINITE_FORWARD) {
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001033 if (req->flags & CF_EOI)
1034 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet66af0b22019-03-22 14:54:52 +01001035 }
1036 else {
1037 /* We can't process the buffer's contents yet */
1038 req->flags |= CF_WAKE_WRITE;
1039 goto missing_data_or_waiting;
1040 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001041 }
1042
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001043 if (msg->msg_state >= HTTP_MSG_ENDING)
1044 goto ending;
1045
1046 if (txn->meth == HTTP_METH_CONNECT) {
1047 msg->msg_state = HTTP_MSG_ENDING;
1048 goto ending;
1049 }
1050
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001051 /* Forward input data. We get it by removing all outgoing data not
1052 * forwarded yet from HTX data size. If there are some data filters, we
1053 * let them decide the amount of data to forward.
Christopher Faulet9768c262018-10-22 09:34:31 +02001054 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001055 if (HAS_REQ_DATA_FILTERS(s)) {
1056 ret = flt_http_payload(s, msg, htx->data);
1057 if (ret < 0)
1058 goto return_bad_req;
Christopher Faulet421e7692019-06-13 11:16:45 +02001059 c_adv(req, ret);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001060 }
1061 else {
Christopher Faulet421e7692019-06-13 11:16:45 +02001062 c_adv(req, htx->data - co_data(req));
Christopher Faulet66af0b22019-03-22 14:54:52 +01001063 if (msg->flags & HTTP_MSGF_XFER_LEN)
1064 channel_htx_forward_forever(req, htx);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001065 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001066
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001067 if (htx->data != co_data(req))
1068 goto missing_data_or_waiting;
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001069
Christopher Faulet9768c262018-10-22 09:34:31 +02001070 /* Check if the end-of-message is reached and if so, switch the message
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001071 * in HTTP_MSG_ENDING state. Then if all data was marked to be
1072 * forwarded, set the state to HTTP_MSG_DONE.
Christopher Faulet9768c262018-10-22 09:34:31 +02001073 */
1074 if (htx_get_tail_type(htx) != HTX_BLK_EOM)
1075 goto missing_data_or_waiting;
1076
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001077 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet9768c262018-10-22 09:34:31 +02001078
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001079 ending:
Christopher Faulet2151cdd2020-07-22 16:34:59 +02001080 req->flags &= ~CF_EXPECT_MORE; /* no more data are expected */
1081
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001082 /* other states, ENDING...TUNNEL */
1083 if (msg->msg_state >= HTTP_MSG_DONE)
1084 goto done;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001085
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001086 if (HAS_REQ_DATA_FILTERS(s)) {
1087 ret = flt_http_end(s, msg);
1088 if (ret <= 0) {
1089 if (!ret)
1090 goto missing_data_or_waiting;
1091 goto return_bad_req;
1092 }
1093 }
1094
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001095 if (txn->meth == HTTP_METH_CONNECT)
1096 msg->msg_state = HTTP_MSG_TUNNEL;
1097 else {
1098 msg->msg_state = HTTP_MSG_DONE;
1099 req->to_forward = 0;
1100 }
1101
1102 done:
1103 /* we don't want to forward closes on DONE except in tunnel mode. */
1104 if (!(txn->flags & TX_CON_WANT_TUN))
1105 channel_dont_close(req);
1106
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001107 http_end_request(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001108 if (!(req->analysers & an_bit)) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001109 http_end_response(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001110 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
1111 if (req->flags & CF_SHUTW) {
1112 /* request errors are most likely due to the
1113 * server aborting the transfer. */
Christopher Faulet93e02d82019-03-08 14:18:50 +01001114 goto return_srv_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001115 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001116 goto return_bad_req;
1117 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001118 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001119 return 1;
1120 }
1121
1122 /* If "option abortonclose" is set on the backend, we want to monitor
1123 * the client's connection and forward any shutdown notification to the
1124 * server, which will decide whether to close or to go on processing the
1125 * request. We only do that in tunnel mode, and not in other modes since
1126 * it can be abused to exhaust source ports. */
Christopher Faulet769d0e92019-03-22 14:23:18 +01001127 if (s->be->options & PR_O_ABRT_CLOSE) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001128 channel_auto_read(req);
Christopher Fauletc41547b2019-07-16 14:32:23 +02001129 if ((req->flags & (CF_SHUTR|CF_READ_NULL)) && !(txn->flags & TX_CON_WANT_TUN))
Christopher Faulete0768eb2018-10-03 16:38:02 +02001130 s->si[1].flags |= SI_FL_NOLINGER;
1131 channel_auto_close(req);
1132 }
1133 else if (s->txn->meth == HTTP_METH_POST) {
1134 /* POST requests may require to read extra CRLF sent by broken
1135 * browsers and which could cause an RST to be sent upon close
1136 * on some systems (eg: Linux). */
1137 channel_auto_read(req);
1138 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001139 DBG_TRACE_DEVEL("waiting for the end of the HTTP txn",
1140 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001141 return 0;
1142
1143 missing_data_or_waiting:
1144 /* stop waiting for data if the input is closed before the end */
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001145 if (msg->msg_state < HTTP_MSG_ENDING && req->flags & CF_SHUTR)
Christopher Faulet93e02d82019-03-08 14:18:50 +01001146 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001147
1148 waiting:
1149 /* waiting for the last bits to leave the buffer */
1150 if (req->flags & CF_SHUTW)
Christopher Faulet93e02d82019-03-08 14:18:50 +01001151 goto return_srv_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001152
1153 /* When TE: chunked is used, we need to get there again to parse remaining
1154 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
1155 * And when content-length is used, we never want to let the possible
1156 * shutdown be forwarded to the other side, as the state machine will
1157 * take care of it once the client responds. It's also important to
1158 * prevent TIME_WAITs from accumulating on the backend side, and for
1159 * HTTP/2 where the last frame comes with a shutdown.
1160 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001161 if (msg->flags & HTTP_MSGF_XFER_LEN)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001162 channel_dont_close(req);
1163
1164 /* We know that more data are expected, but we couldn't send more that
1165 * what we did. So we always set the CF_EXPECT_MORE flag so that the
1166 * system knows it must not set a PUSH on this first part. Interactive
1167 * modes are already handled by the stream sock layer. We must not do
1168 * this in content-length mode because it could present the MSG_MORE
1169 * flag with the last block of forwarded data, which would cause an
1170 * additional delay to be observed by the receiver.
1171 */
Christopher Faulet2151cdd2020-07-22 16:34:59 +02001172 if (HAS_REQ_DATA_FILTERS(s))
Christopher Faulete0768eb2018-10-03 16:38:02 +02001173 req->flags |= CF_EXPECT_MORE;
1174
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001175 DBG_TRACE_DEVEL("waiting for more data to forward",
1176 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001177 return 0;
1178
Christopher Faulet93e02d82019-03-08 14:18:50 +01001179 return_cli_abort:
1180 _HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
1181 _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001182 if (sess->listener->counters)
1183 _HA_ATOMIC_ADD(&sess->listener->counters->cli_aborts, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001184 if (objt_server(s->target))
Christopher Fauletcff0f732019-12-16 16:13:44 +01001185 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.cli_aborts, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001186 if (!(s->flags & SF_ERR_MASK))
1187 s->flags |= SF_ERR_CLICL;
1188 status = 400;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001189 goto return_prx_cond;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001190
1191 return_srv_abort:
1192 _HA_ATOMIC_ADD(&sess->fe->fe_counters.srv_aborts, 1);
1193 _HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001194 if (sess->listener->counters)
1195 _HA_ATOMIC_ADD(&sess->listener->counters->srv_aborts, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001196 if (objt_server(s->target))
Christopher Fauletcff0f732019-12-16 16:13:44 +01001197 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.srv_aborts, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001198 if (!(s->flags & SF_ERR_MASK))
1199 s->flags |= SF_ERR_SRVCL;
1200 status = 502;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001201 goto return_prx_cond;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001202
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001203 return_int_err:
1204 if (!(s->flags & SF_ERR_MASK))
1205 s->flags |= SF_ERR_INTERNAL;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001206 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001207 _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01001208 if (sess->listener->counters)
1209 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001210 if (objt_server(s->target))
1211 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.internal_errors, 1);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001212 status = 500;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001213 goto return_prx_cond;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001214
Christopher Faulet93e02d82019-03-08 14:18:50 +01001215 return_bad_req:
Olivier Houcharda798bf52019-03-08 18:52:00 +01001216 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001217 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +01001218 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001219 status = 400;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001220 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001221
Christopher Fauletb8a53712019-12-16 11:29:38 +01001222 return_prx_cond:
Christopher Faulet9768c262018-10-22 09:34:31 +02001223 if (txn->status > 0) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001224 /* Note: we don't send any error if some data were already sent */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001225 http_reply_and_close(s, txn->status, NULL);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001226 } else {
Christopher Faulet93e02d82019-03-08 14:18:50 +01001227 txn->status = status;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001228 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001229 }
1230 req->analysers &= AN_REQ_FLT_END;
1231 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 +01001232 if (!(s->flags & SF_ERR_MASK))
1233 s->flags |= SF_ERR_PRXCOND;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001234 if (!(s->flags & SF_FINST_MASK))
1235 s->flags |= ((txn->rsp.msg_state < HTTP_MSG_ERROR) ? SF_FINST_H : SF_FINST_D);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001236 DBG_TRACE_DEVEL("leaving on error ",
1237 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001238 return 0;
1239}
1240
Olivier Houcharda254a372019-04-05 15:30:12 +02001241/* Reset the stream and the backend stream_interface to a situation suitable for attemption connection */
1242/* Returns 0 if we can attempt to retry, -1 otherwise */
1243static __inline int do_l7_retry(struct stream *s, struct stream_interface *si)
1244{
1245 struct channel *req, *res;
1246 int co_data;
1247
1248 si->conn_retries--;
1249 if (si->conn_retries < 0)
1250 return -1;
1251
Willy Tarreau223995e2019-05-04 10:38:31 +02001252 if (objt_server(s->target))
1253 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.retries, 1);
1254 _HA_ATOMIC_ADD(&s->be->be_counters.retries, 1);
1255
Olivier Houcharda254a372019-04-05 15:30:12 +02001256 req = &s->req;
1257 res = &s->res;
1258 /* Remove any write error from the request, and read error from the response */
1259 req->flags &= ~(CF_WRITE_ERROR | CF_WRITE_TIMEOUT | CF_SHUTW | CF_SHUTW_NOW);
1260 res->flags &= ~(CF_READ_ERROR | CF_READ_TIMEOUT | CF_SHUTR | CF_EOI | CF_READ_NULL | CF_SHUTR_NOW);
1261 res->analysers = 0;
1262 si->flags &= ~(SI_FL_ERR | SI_FL_EXP | SI_FL_RXBLK_SHUT);
Olivier Houchard8cabc972020-05-12 22:18:14 +02001263 s->flags &= ~SF_ADDR_SET;
Olivier Houchard4bd58672019-07-12 16:16:59 +02001264 stream_choose_redispatch(s);
Olivier Houcharda254a372019-04-05 15:30:12 +02001265 si->exp = TICK_ETERNITY;
1266 res->rex = TICK_ETERNITY;
1267 res->to_forward = 0;
1268 res->analyse_exp = TICK_ETERNITY;
1269 res->total = 0;
Olivier Houchard4bd58672019-07-12 16:16:59 +02001270 s->flags &= ~(SF_ERR_SRVTO | SF_ERR_SRVCL);
Olivier Houcharda254a372019-04-05 15:30:12 +02001271 si_release_endpoint(&s->si[1]);
1272 b_free(&req->buf);
1273 /* Swap the L7 buffer with the channel buffer */
1274 /* We know we stored the co_data as b_data, so get it there */
1275 co_data = b_data(&si->l7_buffer);
1276 b_set_data(&si->l7_buffer, b_size(&si->l7_buffer));
1277 b_xfer(&req->buf, &si->l7_buffer, b_data(&si->l7_buffer));
1278
1279 co_set_data(req, co_data);
1280 b_reset(&res->buf);
1281 co_set_data(res, 0);
1282 return 0;
1283}
1284
Christopher Faulete0768eb2018-10-03 16:38:02 +02001285/* This stream analyser waits for a complete HTTP response. It returns 1 if the
1286 * processing can continue on next analysers, or zero if it either needs more
1287 * data or wants to immediately abort the response (eg: timeout, error, ...). It
1288 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->res.analysers
1289 * when it has nothing left to do, and may remove any analyser when it wants to
1290 * abort.
1291 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001292int http_wait_for_response(struct stream *s, struct channel *rep, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001293{
Christopher Faulet9768c262018-10-22 09:34:31 +02001294 /*
1295 * We will analyze a complete HTTP response to check the its syntax.
1296 *
1297 * Once the start line and all headers are received, we may perform a
1298 * capture of the error (if any), and we will set a few fields. We also
1299 * logging and finally headers capture.
1300 */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001301 struct session *sess = s->sess;
1302 struct http_txn *txn = s->txn;
1303 struct http_msg *msg = &txn->rsp;
Christopher Faulet9768c262018-10-22 09:34:31 +02001304 struct htx *htx;
Olivier Houcharda254a372019-04-05 15:30:12 +02001305 struct stream_interface *si_b = &s->si[1];
Christopher Faulet61608322018-11-23 16:23:45 +01001306 struct connection *srv_conn;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001307 struct htx_sl *sl;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001308 int n;
1309
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001310 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001311
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001312 htx = htxbuf(&rep->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001313
Willy Tarreau4236f032019-03-05 10:43:32 +01001314 /* Parsing errors are caught here */
1315 if (htx->flags & HTX_FL_PARSING_ERROR)
1316 goto return_bad_res;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001317 if (htx->flags & HTX_FL_PROCESSING_ERROR)
1318 goto return_int_err;
Willy Tarreau4236f032019-03-05 10:43:32 +01001319
Christopher Faulete0768eb2018-10-03 16:38:02 +02001320 /*
1321 * Now we quickly check if we have found a full valid response.
1322 * If not so, we check the FD and buffer states before leaving.
1323 * A full response is indicated by the fact that we have seen
1324 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
1325 * responses are checked first.
1326 *
1327 * Depending on whether the client is still there or not, we
1328 * may send an error response back or not. Note that normally
1329 * we should only check for HTTP status there, and check I/O
1330 * errors somewhere else.
1331 */
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001332 next_one:
Christopher Faulet29f17582019-05-23 11:03:26 +02001333 if (unlikely(htx_is_empty(htx) || htx->first == -1)) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001334 /* 1: have we encountered a read error ? */
1335 if (rep->flags & CF_READ_ERROR) {
Olivier Houchard865d8392019-05-03 22:46:27 +02001336 struct connection *conn = NULL;
1337
Olivier Houchard865d8392019-05-03 22:46:27 +02001338 if (objt_cs(s->si[1].end))
1339 conn = objt_cs(s->si[1].end)->conn;
1340
1341 if (si_b->flags & SI_FL_L7_RETRY &&
1342 (!conn || conn->err_code != CO_ER_SSL_EARLY_FAILED)) {
Olivier Houcharda254a372019-04-05 15:30:12 +02001343 /* If we arrive here, then CF_READ_ERROR was
1344 * set by si_cs_recv() because we matched a
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05001345 * status, otherwise it would have removed
Olivier Houcharda254a372019-04-05 15:30:12 +02001346 * the SI_FL_L7_RETRY flag, so it's ok not
1347 * to check s->be->retry_type.
1348 */
1349 if (co_data(rep) || do_l7_retry(s, si_b) == 0)
1350 return 0;
1351 }
1352
Olivier Houchard6db16992019-05-17 15:40:49 +02001353 if (txn->flags & TX_NOT_FIRST)
1354 goto abort_keep_alive;
1355
Olivier Houcharda798bf52019-03-08 18:52:00 +01001356 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001357 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001358 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001359 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001360 }
1361
Christopher Faulete0768eb2018-10-03 16:38:02 +02001362 rep->analysers &= AN_RES_FLT_END;
1363 txn->status = 502;
1364
1365 /* Check to see if the server refused the early data.
1366 * If so, just send a 425
1367 */
Willy Tarreauee99aaf2020-06-23 05:58:20 +02001368 if (conn && conn->err_code == CO_ER_SSL_EARLY_FAILED) {
Olivier Houchard865d8392019-05-03 22:46:27 +02001369 if ((s->be->retry_type & PR_RE_EARLY_ERROR) &&
Olivier Houchardad26d8d2019-05-10 17:48:28 +02001370 (si_b->flags & SI_FL_L7_RETRY) &&
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001371 do_l7_retry(s, si_b) == 0) {
1372 DBG_TRACE_DEVEL("leaving on L7 retry",
1373 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houchard865d8392019-05-03 22:46:27 +02001374 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001375 }
Olivier Houchard865d8392019-05-03 22:46:27 +02001376 txn->status = 425;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001377 }
1378
1379 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001380 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001381
1382 if (!(s->flags & SF_ERR_MASK))
1383 s->flags |= SF_ERR_SRVCL;
1384 if (!(s->flags & SF_FINST_MASK))
1385 s->flags |= SF_FINST_H;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001386 DBG_TRACE_DEVEL("leaving on error",
1387 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001388 return 0;
1389 }
1390
Christopher Faulet9768c262018-10-22 09:34:31 +02001391 /* 2: read timeout : return a 504 to the client. */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001392 else if (rep->flags & CF_READ_TIMEOUT) {
Olivier Houcharda254a372019-04-05 15:30:12 +02001393 if ((si_b->flags & SI_FL_L7_RETRY) &&
1394 (s->be->retry_type & PR_RE_TIMEOUT)) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001395 if (co_data(rep) || do_l7_retry(s, si_b) == 0) {
1396 DBG_TRACE_DEVEL("leaving on L7 retry",
1397 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharda254a372019-04-05 15:30:12 +02001398 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001399 }
Olivier Houcharda254a372019-04-05 15:30:12 +02001400 }
Olivier Houcharda798bf52019-03-08 18:52:00 +01001401 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001402 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001403 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001404 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001405 }
1406
Christopher Faulete0768eb2018-10-03 16:38:02 +02001407 rep->analysers &= AN_RES_FLT_END;
1408 txn->status = 504;
1409 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001410 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001411
1412 if (!(s->flags & SF_ERR_MASK))
1413 s->flags |= SF_ERR_SRVTO;
1414 if (!(s->flags & SF_FINST_MASK))
1415 s->flags |= SF_FINST_H;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001416 DBG_TRACE_DEVEL("leaving on error",
1417 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001418 return 0;
1419 }
1420
Christopher Faulet9768c262018-10-22 09:34:31 +02001421 /* 3: client abort with an abortonclose */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001422 else if ((rep->flags & CF_SHUTR) && ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001423 _HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
1424 _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001425 if (sess->listener->counters)
1426 _HA_ATOMIC_ADD(&sess->listener->counters->cli_aborts, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001427 if (objt_server(s->target))
Olivier Houcharda798bf52019-03-08 18:52:00 +01001428 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.cli_aborts, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001429
1430 rep->analysers &= AN_RES_FLT_END;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001431 txn->status = 400;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001432 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001433
1434 if (!(s->flags & SF_ERR_MASK))
1435 s->flags |= SF_ERR_CLICL;
1436 if (!(s->flags & SF_FINST_MASK))
1437 s->flags |= SF_FINST_H;
1438
1439 /* process_stream() will take care of the error */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001440 DBG_TRACE_DEVEL("leaving on error",
1441 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001442 return 0;
1443 }
1444
Christopher Faulet9768c262018-10-22 09:34:31 +02001445 /* 4: close from server, capture the response if the server has started to respond */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001446 else if (rep->flags & CF_SHUTR) {
Olivier Houcharda254a372019-04-05 15:30:12 +02001447 if ((si_b->flags & SI_FL_L7_RETRY) &&
1448 (s->be->retry_type & PR_RE_DISCONNECTED)) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001449 if (co_data(rep) || do_l7_retry(s, si_b) == 0) {
1450 DBG_TRACE_DEVEL("leaving on L7 retry",
1451 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharda254a372019-04-05 15:30:12 +02001452 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001453 }
Olivier Houcharda254a372019-04-05 15:30:12 +02001454 }
1455
Olivier Houchard6db16992019-05-17 15:40:49 +02001456 if (txn->flags & TX_NOT_FIRST)
1457 goto abort_keep_alive;
1458
Olivier Houcharda798bf52019-03-08 18:52:00 +01001459 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001460 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001461 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001462 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001463 }
1464
Christopher Faulete0768eb2018-10-03 16:38:02 +02001465 rep->analysers &= AN_RES_FLT_END;
1466 txn->status = 502;
1467 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001468 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001469
1470 if (!(s->flags & SF_ERR_MASK))
1471 s->flags |= SF_ERR_SRVCL;
1472 if (!(s->flags & SF_FINST_MASK))
1473 s->flags |= SF_FINST_H;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001474 DBG_TRACE_DEVEL("leaving on error",
1475 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001476 return 0;
1477 }
1478
Christopher Faulet9768c262018-10-22 09:34:31 +02001479 /* 5: write error to client (we don't send any message then) */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001480 else if (rep->flags & CF_WRITE_ERROR) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001481 if (txn->flags & TX_NOT_FIRST)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001482 goto abort_keep_alive;
1483
Olivier Houcharda798bf52019-03-08 18:52:00 +01001484 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001485 if (objt_server(s->target))
1486 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001487 rep->analysers &= AN_RES_FLT_END;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001488
1489 if (!(s->flags & SF_ERR_MASK))
1490 s->flags |= SF_ERR_CLICL;
1491 if (!(s->flags & SF_FINST_MASK))
1492 s->flags |= SF_FINST_H;
1493
1494 /* process_stream() will take care of the error */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001495 DBG_TRACE_DEVEL("leaving on error",
1496 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001497 return 0;
1498 }
1499
1500 channel_dont_close(rep);
1501 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001502 DBG_TRACE_DEVEL("waiting for more data",
1503 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001504 return 0;
1505 }
1506
1507 /* More interesting part now : we know that we have a complete
1508 * response which at least looks like HTTP. We have an indicator
1509 * of each header's length, so we can parse them quickly.
1510 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001511 msg->msg_state = HTTP_MSG_BODY;
Christopher Faulet29f17582019-05-23 11:03:26 +02001512 BUG_ON(htx_get_first_type(htx) != HTX_BLK_RES_SL);
Christopher Faulet297fbb42019-05-13 14:41:27 +02001513 sl = http_get_stline(htx);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001514
Christopher Faulet9768c262018-10-22 09:34:31 +02001515 /* 0: we might have to print this header in debug mode */
1516 if (unlikely((global.mode & MODE_DEBUG) &&
1517 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
1518 int32_t pos;
1519
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001520 http_debug_stline("srvrep", s, sl);
Christopher Faulet9768c262018-10-22 09:34:31 +02001521
Christopher Fauleta3f15502019-05-13 15:27:23 +02001522 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001523 struct htx_blk *blk = htx_get_blk(htx, pos);
1524 enum htx_blk_type type = htx_get_blk_type(blk);
1525
1526 if (type == HTX_BLK_EOH)
1527 break;
1528 if (type != HTX_BLK_HDR)
1529 continue;
1530
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001531 http_debug_hdr("srvhdr", s,
1532 htx_get_blk_name(htx, blk),
1533 htx_get_blk_value(htx, blk));
Christopher Faulet9768c262018-10-22 09:34:31 +02001534 }
1535 }
1536
Christopher Faulet03599112018-11-27 11:21:21 +01001537 /* 1: get the status code and the version. Also set HTTP flags */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001538 txn->status = sl->info.res.status;
Christopher Faulet03599112018-11-27 11:21:21 +01001539 if (sl->flags & HTX_SL_F_VER_11)
Christopher Faulet9768c262018-10-22 09:34:31 +02001540 msg->flags |= HTTP_MSGF_VER_11;
Christopher Faulet03599112018-11-27 11:21:21 +01001541 if (sl->flags & HTX_SL_F_XFER_LEN) {
1542 msg->flags |= HTTP_MSGF_XFER_LEN;
Christopher Faulet2a408542020-11-20 14:22:37 +01001543 if (sl->flags & HTX_SL_F_CLEN)
1544 msg->flags |= HTTP_MSGF_CNT_LEN;
1545 else if (sl->flags & HTX_SL_F_CHNK)
1546 msg->flags |= HTTP_MSGF_TE_CHNK;
Christopher Faulet03599112018-11-27 11:21:21 +01001547 }
Christopher Faulet2a408542020-11-20 14:22:37 +01001548 if (sl->flags & HTX_SL_F_BODYLESS)
1549 msg->flags |= HTTP_MSGF_BODYLESS;
Christopher Faulet9768c262018-10-22 09:34:31 +02001550
1551 n = txn->status / 100;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001552 if (n < 1 || n > 5)
1553 n = 0;
Christopher Faulet9768c262018-10-22 09:34:31 +02001554
Christopher Faulete0768eb2018-10-03 16:38:02 +02001555 /* when the client triggers a 4xx from the server, it's most often due
1556 * to a missing object or permission. These events should be tracked
1557 * because if they happen often, it may indicate a brute force or a
1558 * vulnerability scan.
1559 */
1560 if (n == 4)
1561 stream_inc_http_err_ctr(s);
1562
Marcin Deranek3c27dda2020-05-15 18:32:51 +02001563 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001564 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.p.http.rsp[n], 1);
Marcin Deranek3c27dda2020-05-15 18:32:51 +02001565 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.p.http.cum_req, 1);
1566 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001567
Christopher Faulete0768eb2018-10-03 16:38:02 +02001568 /* Adjust server's health based on status code. Note: status codes 501
1569 * and 505 are triggered on demand by client request, so we must not
1570 * count them as server failures.
1571 */
1572 if (objt_server(s->target)) {
1573 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001574 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_OK);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001575 else
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001576 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_STS);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001577 }
1578
1579 /*
1580 * We may be facing a 100-continue response, or any other informational
1581 * 1xx response which is non-final, in which case this is not the right
1582 * response, and we're waiting for the next one. Let's allow this response
1583 * to go to the client and wait for the next one. There's an exception for
1584 * 101 which is used later in the code to switch protocols.
1585 */
1586 if (txn->status < 200 &&
1587 (txn->status == 100 || txn->status >= 102)) {
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001588 FLT_STRM_CB(s, flt_http_reset(s, msg));
Christopher Faulet421e7692019-06-13 11:16:45 +02001589 htx->first = channel_htx_fwd_headers(rep, htx);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001590 msg->msg_state = HTTP_MSG_RPBEFORE;
Christopher Faulet3499f622019-09-03 15:23:54 +02001591 msg->flags = 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001592 txn->status = 0;
1593 s->logs.t_data = -1; /* was not a response yet */
Christopher Faulet7d518452020-08-31 11:07:07 +02001594 rep->flags |= CF_SEND_DONTWAIT; /* Send ASAP informational messages */
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001595 goto next_one;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001596 }
1597
1598 /*
1599 * 2: check for cacheability.
1600 */
1601
1602 switch (txn->status) {
1603 case 200:
1604 case 203:
1605 case 204:
1606 case 206:
1607 case 300:
1608 case 301:
1609 case 404:
1610 case 405:
1611 case 410:
1612 case 414:
1613 case 501:
1614 break;
1615 default:
1616 /* RFC7231#6.1:
1617 * Responses with status codes that are defined as
1618 * cacheable by default (e.g., 200, 203, 204, 206,
1619 * 300, 301, 404, 405, 410, 414, and 501 in this
1620 * specification) can be reused by a cache with
1621 * heuristic expiration unless otherwise indicated
1622 * by the method definition or explicit cache
1623 * controls [RFC7234]; all other status codes are
1624 * not cacheable by default.
1625 */
1626 txn->flags &= ~(TX_CACHEABLE | TX_CACHE_COOK);
1627 break;
1628 }
1629
1630 /*
1631 * 3: we may need to capture headers
1632 */
1633 s->logs.logwait &= ~LW_RESP;
1634 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->res_cap))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001635 http_capture_headers(htx, s->res_cap, sess->fe->rsp_cap);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001636
Christopher Faulet9768c262018-10-22 09:34:31 +02001637 /* Skip parsing if no content length is possible. */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001638 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
1639 txn->status == 101)) {
1640 /* Either we've established an explicit tunnel, or we're
1641 * switching the protocol. In both cases, we're very unlikely
1642 * to understand the next protocols. We have to switch to tunnel
1643 * mode, so that we transfer the request and responses then let
1644 * this protocol pass unmodified. When we later implement specific
1645 * parsers for such protocols, we'll want to check the Upgrade
1646 * header which contains information about that protocol for
1647 * responses with status 101 (eg: see RFC2817 about TLS).
1648 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02001649 txn->flags |= TX_CON_WANT_TUN;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001650 }
1651
Christopher Faulet61608322018-11-23 16:23:45 +01001652 /* check for NTML authentication headers in 401 (WWW-Authenticate) and
1653 * 407 (Proxy-Authenticate) responses and set the connection to private
1654 */
1655 srv_conn = cs_conn(objt_cs(s->si[1].end));
1656 if (srv_conn) {
1657 struct ist hdr;
1658 struct http_hdr_ctx ctx;
1659
1660 if (txn->status == 401)
1661 hdr = ist("WWW-Authenticate");
1662 else if (txn->status == 407)
1663 hdr = ist("Proxy-Authenticate");
1664 else
1665 goto end;
1666
1667 ctx.blk = NULL;
1668 while (http_find_header(htx, hdr, &ctx, 0)) {
Willy Tarreauf1dcced2020-05-07 19:27:02 +02001669 /* If www-authenticate contains "Negotiate", "Nego2", or "NTLM",
1670 * possibly followed by blanks and a base64 string, the connection
1671 * is private. Since it's a mess to deal with, we only check for
1672 * values starting with "NTLM" or "Nego". Note that often multiple
1673 * headers are sent by the server there.
1674 */
1675 if ((ctx.value.len >= 4 && strncasecmp(ctx.value.ptr, "Nego", 4) == 0) ||
Willy Tarreau49a1d282020-05-07 19:10:15 +02001676 (ctx.value.len >= 4 && strncasecmp(ctx.value.ptr, "NTLM", 4) == 0)) {
Olivier Houchard250031e2019-05-29 15:01:50 +02001677 sess->flags |= SESS_FL_PREFER_LAST;
Christopher Faulet08016ab2020-07-01 16:10:06 +02001678 conn_set_owner(srv_conn, sess, NULL);
Christopher Faulet21ddc742020-07-01 15:26:14 +02001679 conn_set_private(srv_conn);
Ilya Shipitsin6b79f382020-07-23 00:32:55 +05001680 /* If it fail now, the same will be done in mux->detach() callback */
Christopher Faulet08016ab2020-07-01 16:10:06 +02001681 session_add_conn(srv_conn->owner, srv_conn, srv_conn->target);
Willy Tarreauf1dcced2020-05-07 19:27:02 +02001682 break;
Olivier Houchard250031e2019-05-29 15:01:50 +02001683 }
Christopher Faulet61608322018-11-23 16:23:45 +01001684 }
1685 }
1686
1687 end:
Christopher Faulete0768eb2018-10-03 16:38:02 +02001688 /* we want to have the response time before we start processing it */
1689 s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now);
1690
1691 /* end of job, return OK */
1692 rep->analysers &= ~an_bit;
1693 rep->analyse_exp = TICK_ETERNITY;
1694 channel_auto_close(rep);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001695 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001696 return 1;
1697
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001698 return_int_err:
Christopher Fauletcff0f732019-12-16 16:13:44 +01001699 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01001700 _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001701 if (sess->listener->counters)
1702 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01001703 if (objt_server(s->target))
1704 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.internal_errors, 1);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001705 txn->status = 500;
1706 if (!(s->flags & SF_ERR_MASK))
1707 s->flags |= SF_ERR_INTERNAL;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001708 goto return_prx_cond;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001709
1710 return_bad_res:
Olivier Houcharda798bf52019-03-08 18:52:00 +01001711 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulet47365272018-10-31 17:40:50 +01001712 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001713 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001714 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Christopher Faulet47365272018-10-31 17:40:50 +01001715 }
Olivier Houcharde3249a92019-05-03 23:01:47 +02001716 if ((s->be->retry_type & PR_RE_JUNK_REQUEST) &&
Olivier Houchardad26d8d2019-05-10 17:48:28 +02001717 (si_b->flags & SI_FL_L7_RETRY) &&
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001718 do_l7_retry(s, si_b) == 0) {
1719 DBG_TRACE_DEVEL("leaving on L7 retry",
1720 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharde3249a92019-05-03 23:01:47 +02001721 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001722 }
Christopher Faulet47365272018-10-31 17:40:50 +01001723 txn->status = 502;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001724 /* fall through */
1725
Christopher Fauletb8a53712019-12-16 11:29:38 +01001726 return_prx_cond:
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001727 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet47365272018-10-31 17:40:50 +01001728
1729 if (!(s->flags & SF_ERR_MASK))
1730 s->flags |= SF_ERR_PRXCOND;
1731 if (!(s->flags & SF_FINST_MASK))
1732 s->flags |= SF_FINST_H;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001733
1734 s->si[1].flags |= SI_FL_NOLINGER;
1735 rep->analysers &= AN_RES_FLT_END;
Christopher Faulete58c0002020-03-02 16:21:01 +01001736 s->req.analysers &= AN_REQ_FLT_END;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001737 rep->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001738 DBG_TRACE_DEVEL("leaving on error",
1739 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulet47365272018-10-31 17:40:50 +01001740 return 0;
1741
Christopher Faulete0768eb2018-10-03 16:38:02 +02001742 abort_keep_alive:
1743 /* A keep-alive request to the server failed on a network error.
1744 * The client is required to retry. We need to close without returning
1745 * any other information so that the client retries.
1746 */
1747 txn->status = 0;
1748 rep->analysers &= AN_RES_FLT_END;
1749 s->req.analysers &= AN_REQ_FLT_END;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001750 s->logs.logwait = 0;
1751 s->logs.level = 0;
1752 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001753 http_reply_and_close(s, txn->status, NULL);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001754 DBG_TRACE_DEVEL("leaving by closing K/A connection",
1755 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001756 return 0;
1757}
1758
1759/* This function performs all the processing enabled for the current response.
1760 * It normally returns 1 unless it wants to break. It relies on buffers flags,
1761 * and updates s->res.analysers. It might make sense to explode it into several
1762 * other functions. It works like process_request (see indications above).
1763 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001764int http_process_res_common(struct stream *s, struct channel *rep, int an_bit, struct proxy *px)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001765{
1766 struct session *sess = s->sess;
1767 struct http_txn *txn = s->txn;
1768 struct http_msg *msg = &txn->rsp;
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001769 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001770 struct proxy *cur_proxy;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001771 enum rule_result ret = HTTP_RULE_RES_CONT;
1772
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001773 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
1774 return 0;
Christopher Faulet9768c262018-10-22 09:34:31 +02001775
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001776 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001777
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001778 htx = htxbuf(&rep->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001779
1780 /* The stats applet needs to adjust the Connection header but we don't
1781 * apply any filter there.
1782 */
1783 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
1784 rep->analysers &= ~an_bit;
1785 rep->analyse_exp = TICK_ETERNITY;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001786 goto end;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001787 }
1788
1789 /*
1790 * We will have to evaluate the filters.
1791 * As opposed to version 1.2, now they will be evaluated in the
1792 * filters order and not in the header order. This means that
1793 * each filter has to be validated among all headers.
1794 *
1795 * Filters are tried with ->be first, then with ->fe if it is
1796 * different from ->be.
1797 *
1798 * Maybe we are in resume condiion. In this case I choose the
1799 * "struct proxy" which contains the rule list matching the resume
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05001800 * pointer. If none of these "struct proxy" match, I initialise
Christopher Faulete0768eb2018-10-03 16:38:02 +02001801 * the process with the first one.
1802 *
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05001803 * In fact, I check only correspondence between the current list
Christopher Faulete0768eb2018-10-03 16:38:02 +02001804 * pointer and the ->fe rule list. If it doesn't match, I initialize
1805 * the loop with the ->be.
1806 */
1807 if (s->current_rule_list == &sess->fe->http_res_rules)
1808 cur_proxy = sess->fe;
1809 else
1810 cur_proxy = s->be;
1811 while (1) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001812 /* evaluate http-response rules */
1813 if (ret == HTTP_RULE_RES_CONT) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001814 ret = http_res_get_intercept_rule(cur_proxy, &cur_proxy->http_res_rules, s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001815
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001816 switch (ret) {
1817 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
1818 goto return_prx_yield;
1819
1820 case HTTP_RULE_RES_CONT:
1821 case HTTP_RULE_RES_STOP: /* nothing to do */
1822 break;
1823
1824 case HTTP_RULE_RES_DENY: /* deny or tarpit */
1825 goto deny;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001826
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001827 case HTTP_RULE_RES_ABRT: /* abort request, response already sent */
1828 goto return_prx_cond;
1829
1830 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
Christopher Fauletb8a53712019-12-16 11:29:38 +01001831 goto done;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001832
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001833 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
1834 goto return_bad_res;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001835
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001836 case HTTP_RULE_RES_ERROR: /* failed with a bad request */
1837 goto return_int_err;
1838 }
1839
1840 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001841
Christopher Faulete0768eb2018-10-03 16:38:02 +02001842 /* check whether we're already working on the frontend */
1843 if (cur_proxy == sess->fe)
1844 break;
1845 cur_proxy = sess->fe;
1846 }
1847
Christopher Faulete0768eb2018-10-03 16:38:02 +02001848 /* OK that's all we can do for 1xx responses */
1849 if (unlikely(txn->status < 200 && txn->status != 101))
Christopher Fauletf2824e62018-10-01 12:12:37 +02001850 goto end;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001851
1852 /*
1853 * Now check for a server cookie.
1854 */
1855 if (s->be->cookie_name || sess->fe->capture_name || (s->be->options & PR_O_CHK_CACHE))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001856 http_manage_server_side_cookies(s, rep);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001857
1858 /*
1859 * Check for cache-control or pragma headers if required.
1860 */
1861 if ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001862 http_check_response_for_cacheability(s, rep);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001863
1864 /*
1865 * Add server cookie in the response if needed
1866 */
1867 if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) &&
1868 !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) &&
1869 (!(s->flags & SF_DIRECT) ||
1870 ((s->be->cookie_maxidle || txn->cookie_last_date) &&
1871 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
1872 (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
1873 (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
1874 (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
1875 !(s->flags & SF_IGNORE_PRST)) {
1876 /* the server is known, it's not the one the client requested, or the
1877 * cookie's last seen date needs to be refreshed. We have to
1878 * insert a set-cookie here, except if we want to insert only on POST
1879 * requests and this one isn't. Note that servers which don't have cookies
1880 * (eg: some backup servers) will return a full cookie removal request.
1881 */
1882 if (!objt_server(s->target)->cookie) {
1883 chunk_printf(&trash,
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001884 "%s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
Christopher Faulete0768eb2018-10-03 16:38:02 +02001885 s->be->cookie_name);
1886 }
1887 else {
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001888 chunk_printf(&trash, "%s=%s", s->be->cookie_name, objt_server(s->target)->cookie);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001889
1890 if (s->be->cookie_maxidle || s->be->cookie_maxlife) {
1891 /* emit last_date, which is mandatory */
1892 trash.area[trash.data++] = COOKIE_DELIM_DATE;
1893 s30tob64((date.tv_sec+3) >> 2,
1894 trash.area + trash.data);
1895 trash.data += 5;
1896
1897 if (s->be->cookie_maxlife) {
1898 /* emit first_date, which is either the original one or
1899 * the current date.
1900 */
1901 trash.area[trash.data++] = COOKIE_DELIM_DATE;
1902 s30tob64(txn->cookie_first_date ?
1903 txn->cookie_first_date >> 2 :
1904 (date.tv_sec+3) >> 2,
1905 trash.area + trash.data);
1906 trash.data += 5;
1907 }
1908 }
1909 chunk_appendf(&trash, "; path=/");
1910 }
1911
1912 if (s->be->cookie_domain)
1913 chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain);
1914
1915 if (s->be->ck_opts & PR_CK_HTTPONLY)
1916 chunk_appendf(&trash, "; HttpOnly");
1917
1918 if (s->be->ck_opts & PR_CK_SECURE)
1919 chunk_appendf(&trash, "; Secure");
1920
Christopher Faulet2f533902020-01-21 11:06:48 +01001921 if (s->be->cookie_attrs)
1922 chunk_appendf(&trash, "; %s", s->be->cookie_attrs);
1923
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001924 if (unlikely(!http_add_header(htx, ist("Set-Cookie"), ist2(trash.area, trash.data))))
Christopher Fauletb8a53712019-12-16 11:29:38 +01001925 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001926
1927 txn->flags &= ~TX_SCK_MASK;
1928 if (__objt_server(s->target)->cookie && (s->flags & SF_DIRECT))
1929 /* the server did not change, only the date was updated */
1930 txn->flags |= TX_SCK_UPDATED;
1931 else
1932 txn->flags |= TX_SCK_INSERTED;
1933
1934 /* Here, we will tell an eventual cache on the client side that we don't
1935 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
1936 * Some caches understand the correct form: 'no-cache="set-cookie"', but
1937 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
1938 */
1939 if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
1940
1941 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
1942
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001943 if (unlikely(!http_add_header(htx, ist("Cache-control"), ist("private"))))
Christopher Fauletb8a53712019-12-16 11:29:38 +01001944 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001945 }
1946 }
1947
1948 /*
1949 * Check if result will be cacheable with a cookie.
1950 * We'll block the response if security checks have caught
1951 * nasty things such as a cacheable cookie.
1952 */
1953 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
1954 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
1955 (s->be->options & PR_O_CHK_CACHE)) {
1956 /* we're in presence of a cacheable response containing
1957 * a set-cookie header. We'll block it as requested by
1958 * the 'checkcache' option, and send an alert.
1959 */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001960 ha_alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
1961 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
1962 send_log(s->be, LOG_ALERT,
1963 "Blocking cacheable cookie in response from instance %s, server %s.\n",
1964 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
Christopher Fauletb8a53712019-12-16 11:29:38 +01001965 goto deny;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001966 }
1967
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001968 end:
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01001969 /*
1970 * Evaluate after-response rules before forwarding the response. rules
1971 * from the backend are evaluated first, then one from the frontend if
1972 * it differs.
1973 */
1974 if (!http_eval_after_res_rules(s))
1975 goto return_int_err;
1976
Christopher Faulete0768eb2018-10-03 16:38:02 +02001977 /* Always enter in the body analyzer */
1978 rep->analysers &= ~AN_RES_FLT_XFER_DATA;
1979 rep->analysers |= AN_RES_HTTP_XFER_BODY;
1980
1981 /* if the user wants to log as soon as possible, without counting
1982 * bytes from the server, then this is the right moment. We have
1983 * to temporarily assign bytes_out to log what we currently have.
1984 */
1985 if (!LIST_ISEMPTY(&sess->fe->logformat) && !(s->logs.logwait & LW_BYTES)) {
1986 s->logs.t_close = s->logs.t_data; /* to get a valid end date */
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001987 s->logs.bytes_out = htx->data;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001988 s->do_log(s);
1989 s->logs.bytes_out = 0;
1990 }
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001991
Christopher Fauletb8a53712019-12-16 11:29:38 +01001992 done:
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01001993 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletb8a53712019-12-16 11:29:38 +01001994 rep->analysers &= ~an_bit;
1995 rep->analyse_exp = TICK_ETERNITY;
1996 return 1;
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001997
Christopher Fauletb8a53712019-12-16 11:29:38 +01001998 deny:
Christopher Fauletb8a53712019-12-16 11:29:38 +01001999 _HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_resp, 1);
Christopher Fauleta08546b2019-12-16 16:07:34 +01002000 _HA_ATOMIC_ADD(&s->be->be_counters.denied_resp, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002001 if (sess->listener->counters)
2002 _HA_ATOMIC_ADD(&sess->listener->counters->denied_resp, 1);
Christopher Fauleta08546b2019-12-16 16:07:34 +01002003 if (objt_server(s->target))
2004 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.denied_resp, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002005 goto return_prx_err;
2006
2007 return_int_err:
2008 txn->status = 500;
2009 if (!(s->flags & SF_ERR_MASK))
2010 s->flags |= SF_ERR_INTERNAL;
Christopher Fauletcff0f732019-12-16 16:13:44 +01002011 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002012 _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1);
2013 if (objt_server(s->target))
2014 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01002015 if (objt_server(s->target))
2016 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002017 goto return_prx_err;
2018
2019 return_bad_res:
2020 txn->status = 502;
Christopher Fauleta20a6532020-02-05 10:16:41 +01002021 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
2022 if (objt_server(s->target)) {
2023 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
2024 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_RSP);
2025 }
Christopher Fauletb8a53712019-12-16 11:29:38 +01002026 /* fall through */
2027
2028 return_prx_err:
2029 http_reply_and_close(s, txn->status, http_error_message(s));
2030 /* fall through */
2031
2032 return_prx_cond:
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002033 s->logs.t_data = -1; /* was not a valid response */
2034 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002035
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002036 if (!(s->flags & SF_ERR_MASK))
2037 s->flags |= SF_ERR_PRXCOND;
2038 if (!(s->flags & SF_FINST_MASK))
2039 s->flags |= SF_FINST_H;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002040
Christopher Faulete58c0002020-03-02 16:21:01 +01002041 rep->analysers &= AN_RES_FLT_END;
2042 s->req.analysers &= AN_REQ_FLT_END;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002043 rep->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002044 DBG_TRACE_DEVEL("leaving on error",
2045 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002046 return 0;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002047
2048 return_prx_yield:
2049 channel_dont_close(rep);
2050 DBG_TRACE_DEVEL("waiting for more data",
2051 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
2052 return 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002053}
2054
2055/* This function is an analyser which forwards response body (including chunk
2056 * sizes if any). It is called as soon as we must forward, even if we forward
2057 * zero byte. The only situation where it must not be called is when we're in
2058 * tunnel mode and we want to forward till the close. It's used both to forward
2059 * remaining data and to resync after end of body. It expects the msg_state to
2060 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
2061 * read more data, or 1 once we can go on with next request or end the stream.
2062 *
2063 * It is capable of compressing response data both in content-length mode and
2064 * in chunked mode. The state machines follows different flows depending on
2065 * whether content-length and chunked modes are used, since there are no
2066 * trailers in content-length :
2067 *
2068 * chk-mode cl-mode
2069 * ,----- BODY -----.
2070 * / \
2071 * V size > 0 V chk-mode
2072 * .--> SIZE -------------> DATA -------------> CRLF
2073 * | | size == 0 | last byte |
2074 * | v final crlf v inspected |
2075 * | TRAILERS -----------> DONE |
2076 * | |
2077 * `----------------------------------------------'
2078 *
2079 * Compression only happens in the DATA state, and must be flushed in final
2080 * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding
2081 * is performed at once on final states for all bytes parsed, or when leaving
2082 * on missing data.
2083 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002084int http_response_forward_body(struct stream *s, struct channel *res, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +02002085{
2086 struct session *sess = s->sess;
2087 struct http_txn *txn = s->txn;
2088 struct http_msg *msg = &s->txn->rsp;
Christopher Faulet9768c262018-10-22 09:34:31 +02002089 struct htx *htx;
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002090 int ret;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002091
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002092 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002093
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002094 htx = htxbuf(&res->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002095
Christopher Fauletb9a92f32019-09-09 10:15:21 +02002096 if (htx->flags & HTX_FL_PARSING_ERROR)
2097 goto return_bad_res;
2098 if (htx->flags & HTX_FL_PROCESSING_ERROR)
2099 goto return_int_err;
2100
Christopher Faulete0768eb2018-10-03 16:38:02 +02002101 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Christopher Fauletf2824e62018-10-01 12:12:37 +02002102 ((res->flags & CF_SHUTW) && (res->to_forward || co_data(res)))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02002103 /* Output closed while we were sending data. We must abort and
2104 * wake the other side up.
2105 */
Christopher Faulete0768eb2018-10-03 16:38:02 +02002106 msg->msg_state = HTTP_MSG_ERROR;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002107 http_end_response(s);
2108 http_end_request(s);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002109 DBG_TRACE_DEVEL("leaving on error",
2110 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002111 return 1;
2112 }
2113
Christopher Faulet9768c262018-10-22 09:34:31 +02002114 if (msg->msg_state == HTTP_MSG_BODY)
2115 msg->msg_state = HTTP_MSG_DATA;
2116
Christopher Faulete0768eb2018-10-03 16:38:02 +02002117 /* in most states, we should abort in case of early close */
2118 channel_auto_close(res);
2119
Christopher Faulete0768eb2018-10-03 16:38:02 +02002120 if (res->to_forward) {
Christopher Faulet66af0b22019-03-22 14:54:52 +01002121 if (res->to_forward == CHN_INFINITE_FORWARD) {
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002122 if (res->flags & CF_EOI)
2123 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet66af0b22019-03-22 14:54:52 +01002124 }
2125 else {
2126 /* We can't process the buffer's contents yet */
2127 res->flags |= CF_WAKE_WRITE;
2128 goto missing_data_or_waiting;
2129 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02002130 }
2131
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002132 if (msg->msg_state >= HTTP_MSG_ENDING)
2133 goto ending;
2134
2135 if ((txn->meth == HTTP_METH_CONNECT && txn->status == 200) || txn->status == 101 ||
2136 (!(msg->flags & HTTP_MSGF_XFER_LEN) && !HAS_RSP_DATA_FILTERS(s))) {
2137 msg->msg_state = HTTP_MSG_ENDING;
2138 goto ending;
2139 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002140
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002141 /* Forward input data. We get it by removing all outgoing data not
2142 * forwarded yet from HTX data size. If there are some data filters, we
2143 * let them decide the amount of data to forward.
Christopher Faulet9768c262018-10-22 09:34:31 +02002144 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002145 if (HAS_RSP_DATA_FILTERS(s)) {
2146 ret = flt_http_payload(s, msg, htx->data);
2147 if (ret < 0)
2148 goto return_bad_res;
Christopher Faulet421e7692019-06-13 11:16:45 +02002149 c_adv(res, ret);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002150 }
2151 else {
Christopher Faulet421e7692019-06-13 11:16:45 +02002152 c_adv(res, htx->data - co_data(res));
Christopher Faulet66af0b22019-03-22 14:54:52 +01002153 if (msg->flags & HTTP_MSGF_XFER_LEN)
2154 channel_htx_forward_forever(res, htx);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002155 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002156
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002157 if (htx->data != co_data(res))
2158 goto missing_data_or_waiting;
2159
2160 if (!(msg->flags & HTTP_MSGF_XFER_LEN) && res->flags & CF_SHUTR) {
2161 msg->msg_state = HTTP_MSG_ENDING;
2162 goto ending;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002163 }
2164
Christopher Faulet9768c262018-10-22 09:34:31 +02002165 /* Check if the end-of-message is reached and if so, switch the message
Christopher Fauletd20fdb02019-06-13 16:43:22 +02002166 * in HTTP_MSG_ENDING state. Then if all data was marked to be
2167 * forwarded, set the state to HTTP_MSG_DONE.
Christopher Faulet9768c262018-10-22 09:34:31 +02002168 */
2169 if (htx_get_tail_type(htx) != HTX_BLK_EOM)
2170 goto missing_data_or_waiting;
2171
Christopher Fauletd20fdb02019-06-13 16:43:22 +02002172 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet9768c262018-10-22 09:34:31 +02002173
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002174 ending:
Christopher Faulet2151cdd2020-07-22 16:34:59 +02002175 res->flags &= ~CF_EXPECT_MORE; /* no more data are expected */
2176
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002177 /* other states, ENDING...TUNNEL */
2178 if (msg->msg_state >= HTTP_MSG_DONE)
2179 goto done;
Christopher Faulet9768c262018-10-22 09:34:31 +02002180
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002181 if (HAS_RSP_DATA_FILTERS(s)) {
2182 ret = flt_http_end(s, msg);
2183 if (ret <= 0) {
2184 if (!ret)
2185 goto missing_data_or_waiting;
2186 goto return_bad_res;
2187 }
2188 }
2189
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002190 if ((txn->meth == HTTP_METH_CONNECT && txn->status == 200) || txn->status == 101 ||
2191 !(msg->flags & HTTP_MSGF_XFER_LEN)) {
2192 msg->msg_state = HTTP_MSG_TUNNEL;
2193 goto ending;
2194 }
2195 else {
2196 msg->msg_state = HTTP_MSG_DONE;
2197 res->to_forward = 0;
2198 }
2199
2200 done:
2201
2202 channel_dont_close(res);
2203
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002204 http_end_response(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002205 if (!(res->analysers & an_bit)) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002206 http_end_request(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002207 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
2208 if (res->flags & CF_SHUTW) {
2209 /* response errors are most likely due to the
2210 * client aborting the transfer. */
Christopher Faulet93e02d82019-03-08 14:18:50 +01002211 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002212 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02002213 goto return_bad_res;
2214 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002215 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002216 return 1;
2217 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002218 DBG_TRACE_DEVEL("waiting for the end of the HTTP txn",
2219 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002220 return 0;
2221
2222 missing_data_or_waiting:
2223 if (res->flags & CF_SHUTW)
Christopher Faulet93e02d82019-03-08 14:18:50 +01002224 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002225
2226 /* stop waiting for data if the input is closed before the end. If the
2227 * client side was already closed, it means that the client has aborted,
2228 * so we don't want to count this as a server abort. Otherwise it's a
2229 * server abort.
2230 */
Christopher Fauletd20fdb02019-06-13 16:43:22 +02002231 if (msg->msg_state < HTTP_MSG_ENDING && res->flags & CF_SHUTR) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02002232 if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))
Christopher Faulet93e02d82019-03-08 14:18:50 +01002233 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002234 /* If we have some pending data, we continue the processing */
Christopher Faulet93e02d82019-03-08 14:18:50 +01002235 if (htx_is_empty(htx))
2236 goto return_srv_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002237 }
2238
Christopher Faulete0768eb2018-10-03 16:38:02 +02002239 /* When TE: chunked is used, we need to get there again to parse
2240 * remaining chunks even if the server has closed, so we don't want to
Christopher Faulet9768c262018-10-22 09:34:31 +02002241 * set CF_DONTCLOSE. Similarly when there is a content-leng or if there
2242 * are filters registered on the stream, we don't want to forward a
2243 * close
Christopher Faulete0768eb2018-10-03 16:38:02 +02002244 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002245 if ((msg->flags & HTTP_MSGF_XFER_LEN) || HAS_RSP_DATA_FILTERS(s))
Christopher Faulete0768eb2018-10-03 16:38:02 +02002246 channel_dont_close(res);
2247
2248 /* We know that more data are expected, but we couldn't send more that
2249 * what we did. So we always set the CF_EXPECT_MORE flag so that the
2250 * system knows it must not set a PUSH on this first part. Interactive
2251 * modes are already handled by the stream sock layer. We must not do
2252 * this in content-length mode because it could present the MSG_MORE
2253 * flag with the last block of forwarded data, which would cause an
2254 * additional delay to be observed by the receiver.
2255 */
Christopher Faulet2151cdd2020-07-22 16:34:59 +02002256 if (HAS_RSP_DATA_FILTERS(s))
Christopher Faulete0768eb2018-10-03 16:38:02 +02002257 res->flags |= CF_EXPECT_MORE;
2258
2259 /* the stream handler will take care of timeouts and errors */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002260 DBG_TRACE_DEVEL("waiting for more data to forward",
2261 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002262 return 0;
2263
Christopher Faulet93e02d82019-03-08 14:18:50 +01002264 return_srv_abort:
2265 _HA_ATOMIC_ADD(&sess->fe->fe_counters.srv_aborts, 1);
2266 _HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01002267 if (sess->listener->counters)
2268 _HA_ATOMIC_ADD(&sess->listener->counters->srv_aborts, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002269 if (objt_server(s->target))
Christopher Fauletcff0f732019-12-16 16:13:44 +01002270 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.srv_aborts, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002271 if (!(s->flags & SF_ERR_MASK))
2272 s->flags |= SF_ERR_SRVCL;
2273 goto return_error;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002274
Christopher Faulet93e02d82019-03-08 14:18:50 +01002275 return_cli_abort:
2276 _HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
2277 _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01002278 if (sess->listener->counters)
2279 _HA_ATOMIC_ADD(&sess->listener->counters->cli_aborts, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002280 if (objt_server(s->target))
Christopher Fauletcff0f732019-12-16 16:13:44 +01002281 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.cli_aborts, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002282 if (!(s->flags & SF_ERR_MASK))
2283 s->flags |= SF_ERR_CLICL;
2284 goto return_error;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002285
Christopher Fauletb9a92f32019-09-09 10:15:21 +02002286 return_int_err:
Christopher Fauletcff0f732019-12-16 16:13:44 +01002287 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002288 _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01002289 if (sess->listener->counters)
2290 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002291 if (objt_server(s->target))
2292 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.internal_errors, 1);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02002293 if (!(s->flags & SF_ERR_MASK))
2294 s->flags |= SF_ERR_INTERNAL;
2295 goto return_error;
2296
Christopher Faulet93e02d82019-03-08 14:18:50 +01002297 return_bad_res:
2298 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
2299 if (objt_server(s->target)) {
Christopher Fauletcff0f732019-12-16 16:13:44 +01002300 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002301 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_RSP);
2302 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02002303 if (!(s->flags & SF_ERR_MASK))
Christopher Faulet93e02d82019-03-08 14:18:50 +01002304 s->flags |= SF_ERR_SRVCL;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002305 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +02002306
Christopher Faulet93e02d82019-03-08 14:18:50 +01002307 return_error:
Christopher Faulete0768eb2018-10-03 16:38:02 +02002308 /* don't send any error message as we're in the body */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002309 http_reply_and_close(s, txn->status, NULL);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002310 res->analysers &= AN_RES_FLT_END;
2311 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 +02002312 if (!(s->flags & SF_FINST_MASK))
2313 s->flags |= SF_FINST_D;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002314 DBG_TRACE_DEVEL("leaving on error",
2315 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002316 return 0;
2317}
2318
Christopher Fauletf2824e62018-10-01 12:12:37 +02002319/* Perform an HTTP redirect based on the information in <rule>. The function
Christopher Faulet99daf282018-11-28 22:58:13 +01002320 * returns zero on success, or zero in case of a, irrecoverable error such
Christopher Fauletf2824e62018-10-01 12:12:37 +02002321 * as too large a request to build a valid response.
2322 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002323int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn)
Christopher Fauletf2824e62018-10-01 12:12:37 +02002324{
Christopher Faulet99daf282018-11-28 22:58:13 +01002325 struct channel *req = &s->req;
2326 struct channel *res = &s->res;
2327 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01002328 struct htx_sl *sl;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002329 struct buffer *chunk;
Christopher Faulet99daf282018-11-28 22:58:13 +01002330 struct ist status, reason, location;
2331 unsigned int flags;
Christopher Faulet08e66462019-05-23 16:44:59 +02002332 int close = 0; /* Try to keep the connection alive byt default */
Christopher Fauletf2824e62018-10-01 12:12:37 +02002333
2334 chunk = alloc_trash_chunk();
Christopher Fauletb8a53712019-12-16 11:29:38 +01002335 if (!chunk) {
2336 if (!(s->flags & SF_ERR_MASK))
2337 s->flags |= SF_ERR_RESOURCE;
Christopher Faulet99daf282018-11-28 22:58:13 +01002338 goto fail;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002339 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002340
Christopher Faulet99daf282018-11-28 22:58:13 +01002341 /*
2342 * Create the location
2343 */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002344 htx = htxbuf(&req->buf);
Christopher Fauletf2824e62018-10-01 12:12:37 +02002345 switch(rule->type) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002346 case REDIRECT_TYPE_SCHEME: {
2347 struct http_hdr_ctx ctx;
2348 struct ist path, host;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002349
Christopher Faulet99daf282018-11-28 22:58:13 +01002350 host = ist("");
2351 ctx.blk = NULL;
2352 if (http_find_header(htx, ist("Host"), &ctx, 0))
2353 host = ctx.value;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002354
Christopher Faulet297fbb42019-05-13 14:41:27 +02002355 sl = http_get_stline(htx);
Christopher Faulet99daf282018-11-28 22:58:13 +01002356 path = http_get_path(htx_sl_req_uri(sl));
2357 /* build message using path */
Tim Duesterhused526372020-03-05 17:56:33 +01002358 if (isttest(path)) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002359 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2360 int qs = 0;
2361 while (qs < path.len) {
2362 if (*(path.ptr + qs) == '?') {
2363 path.len = qs;
2364 break;
2365 }
2366 qs++;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002367 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002368 }
2369 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002370 else
2371 path = ist("/");
Christopher Fauletf2824e62018-10-01 12:12:37 +02002372
Christopher Faulet99daf282018-11-28 22:58:13 +01002373 if (rule->rdr_str) { /* this is an old "redirect" rule */
2374 /* add scheme */
2375 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2376 goto fail;
2377 }
2378 else {
2379 /* add scheme with executing log format */
2380 chunk->data += build_logline(s, chunk->area + chunk->data,
2381 chunk->size - chunk->data,
2382 &rule->rdr_fmt);
2383 }
2384 /* add "://" + host + path */
2385 if (!chunk_memcat(chunk, "://", 3) ||
2386 !chunk_memcat(chunk, host.ptr, host.len) ||
2387 !chunk_memcat(chunk, path.ptr, path.len))
2388 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002389
Christopher Faulet99daf282018-11-28 22:58:13 +01002390 /* append a slash at the end of the location if needed and missing */
2391 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
2392 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
2393 if (chunk->data + 1 >= chunk->size)
2394 goto fail;
2395 chunk->area[chunk->data++] = '/';
2396 }
2397 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002398 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002399
Christopher Faulet99daf282018-11-28 22:58:13 +01002400 case REDIRECT_TYPE_PREFIX: {
2401 struct ist path;
2402
Christopher Faulet297fbb42019-05-13 14:41:27 +02002403 sl = http_get_stline(htx);
Christopher Faulet99daf282018-11-28 22:58:13 +01002404 path = http_get_path(htx_sl_req_uri(sl));
2405 /* build message using path */
Tim Duesterhused526372020-03-05 17:56:33 +01002406 if (isttest(path)) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002407 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2408 int qs = 0;
2409 while (qs < path.len) {
2410 if (*(path.ptr + qs) == '?') {
2411 path.len = qs;
2412 break;
2413 }
2414 qs++;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002415 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002416 }
2417 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002418 else
2419 path = ist("/");
Christopher Fauletf2824e62018-10-01 12:12:37 +02002420
Christopher Faulet99daf282018-11-28 22:58:13 +01002421 if (rule->rdr_str) { /* this is an old "redirect" rule */
2422 /* add prefix. Note that if prefix == "/", we don't want to
2423 * add anything, otherwise it makes it hard for the user to
2424 * configure a self-redirection.
2425 */
2426 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
2427 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2428 goto fail;
2429 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002430 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002431 else {
2432 /* add prefix with executing log format */
2433 chunk->data += build_logline(s, chunk->area + chunk->data,
2434 chunk->size - chunk->data,
2435 &rule->rdr_fmt);
2436 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002437
Christopher Faulet99daf282018-11-28 22:58:13 +01002438 /* add path */
2439 if (!chunk_memcat(chunk, path.ptr, path.len))
2440 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002441
Christopher Faulet99daf282018-11-28 22:58:13 +01002442 /* append a slash at the end of the location if needed and missing */
2443 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
2444 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
2445 if (chunk->data + 1 >= chunk->size)
2446 goto fail;
2447 chunk->area[chunk->data++] = '/';
2448 }
2449 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002450 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002451 case REDIRECT_TYPE_LOCATION:
2452 default:
2453 if (rule->rdr_str) { /* this is an old "redirect" rule */
2454 /* add location */
2455 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2456 goto fail;
2457 }
2458 else {
2459 /* add location with executing log format */
2460 chunk->data += build_logline(s, chunk->area + chunk->data,
2461 chunk->size - chunk->data,
2462 &rule->rdr_fmt);
2463 }
2464 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002465 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002466 location = ist2(chunk->area, chunk->data);
2467
2468 /*
2469 * Create the 30x response
2470 */
2471 switch (rule->code) {
2472 case 308:
2473 status = ist("308");
2474 reason = ist("Permanent Redirect");
2475 break;
2476 case 307:
2477 status = ist("307");
2478 reason = ist("Temporary Redirect");
2479 break;
2480 case 303:
2481 status = ist("303");
2482 reason = ist("See Other");
2483 break;
2484 case 301:
2485 status = ist("301");
2486 reason = ist("Moved Permanently");
2487 break;
2488 case 302:
2489 default:
2490 status = ist("302");
2491 reason = ist("Found");
2492 break;
2493 }
2494
Christopher Faulet08e66462019-05-23 16:44:59 +02002495 if (!(txn->req.flags & HTTP_MSGF_BODYLESS) && txn->req.msg_state != HTTP_MSG_DONE)
2496 close = 1;
2497
Christopher Faulet99daf282018-11-28 22:58:13 +01002498 htx = htx_from_buf(&res->buf);
Kevin Zhu96b36392020-01-07 09:42:55 +01002499 /* Trim any possible response */
2500 channel_htx_truncate(&s->res, htx);
Christopher Faulet99daf282018-11-28 22:58:13 +01002501 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
2502 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), status, reason);
2503 if (!sl)
2504 goto fail;
2505 sl->info.res.status = rule->code;
2506 s->txn->status = rule->code;
2507
Christopher Faulet08e66462019-05-23 16:44:59 +02002508 if (close && !htx_add_header(htx, ist("Connection"), ist("close")))
2509 goto fail;
2510
2511 if (!htx_add_header(htx, ist("Content-length"), ist("0")) ||
Christopher Faulet99daf282018-11-28 22:58:13 +01002512 !htx_add_header(htx, ist("Location"), location))
2513 goto fail;
2514
2515 if (rule->code == 302 || rule->code == 303 || rule->code == 307) {
2516 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")))
2517 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002518 }
2519
2520 if (rule->cookie_len) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002521 if (!htx_add_header(htx, ist("Set-Cookie"), ist2(rule->cookie_str, rule->cookie_len)))
2522 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002523 }
2524
Christopher Faulet99daf282018-11-28 22:58:13 +01002525 if (!htx_add_endof(htx, HTX_BLK_EOH) || !htx_add_endof(htx, HTX_BLK_EOM))
2526 goto fail;
2527
Kevin Zhu96b36392020-01-07 09:42:55 +01002528 htx_to_buf(htx, &res->buf);
Christopher Fauleta72a7e42020-01-28 09:28:11 +01002529 if (!http_forward_proxy_resp(s, 1))
2530 goto fail;
Christopher Faulet99daf282018-11-28 22:58:13 +01002531
Christopher Faulet60b33a52020-01-28 09:18:10 +01002532 if (rule->flags & REDIRECT_FLAG_FROM_REQ) {
2533 /* let's log the request time */
2534 s->logs.tv_request = now;
2535 req->analysers &= AN_REQ_FLT_END;
Christopher Faulet99daf282018-11-28 22:58:13 +01002536
Christopher Faulet60b33a52020-01-28 09:18:10 +01002537 if (s->sess->fe == s->be) /* report it if the request was intercepted by the frontend */
2538 _HA_ATOMIC_ADD(&s->sess->fe->fe_counters.intercepted_req, 1);
2539 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002540
2541 if (!(s->flags & SF_ERR_MASK))
2542 s->flags |= SF_ERR_LOCAL;
2543 if (!(s->flags & SF_FINST_MASK))
Christopher Faulet60b33a52020-01-28 09:18:10 +01002544 s->flags |= ((rule->flags & REDIRECT_FLAG_FROM_REQ) ? SF_FINST_R : SF_FINST_H);
Christopher Fauletf2824e62018-10-01 12:12:37 +02002545
Christopher Faulet99daf282018-11-28 22:58:13 +01002546 free_trash_chunk(chunk);
2547 return 1;
2548
2549 fail:
2550 /* If an error occurred, remove the incomplete HTTP response from the
2551 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01002552 channel_htx_truncate(res, htxbuf(&res->buf));
Christopher Fauletf2824e62018-10-01 12:12:37 +02002553 free_trash_chunk(chunk);
Christopher Faulet99daf282018-11-28 22:58:13 +01002554 return 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002555}
2556
Christopher Faulet92d34fe2019-12-17 09:20:34 +01002557/* Replace all headers matching the name <name>. The header value is replaced if
2558 * it matches the regex <re>. <str> is used for the replacement. If <full> is
2559 * set to 1, the full-line is matched and replaced. Otherwise, comma-separated
2560 * values are evaluated one by one. It returns 0 on success and -1 on error.
2561 */
2562int http_replace_hdrs(struct stream* s, struct htx *htx, struct ist name,
2563 const char *str, struct my_regex *re, int full)
Christopher Faulet72333522018-10-24 11:25:02 +02002564{
2565 struct http_hdr_ctx ctx;
2566 struct buffer *output = get_trash_chunk();
2567
Christopher Faulet72333522018-10-24 11:25:02 +02002568 ctx.blk = NULL;
Christopher Faulet92d34fe2019-12-17 09:20:34 +01002569 while (http_find_header(htx, name, &ctx, full)) {
Christopher Faulet72333522018-10-24 11:25:02 +02002570 if (!regex_exec_match2(re, ctx.value.ptr, ctx.value.len, MAX_MATCH, pmatch, 0))
2571 continue;
2572
2573 output->data = exp_replace(output->area, output->size, ctx.value.ptr, str, pmatch);
2574 if (output->data == -1)
2575 return -1;
2576 if (!http_replace_header_value(htx, &ctx, ist2(output->area, output->data)))
2577 return -1;
2578 }
2579 return 0;
2580}
2581
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002582/* This function executes one of the set-{method,path,query,uri} actions. It
2583 * takes the string from the variable 'replace' with length 'len', then modifies
2584 * the relevant part of the request line accordingly. Then it updates various
2585 * pointers to the next elements which were moved, and the total buffer length.
2586 * It finds the action to be performed in p[2], previously filled by function
2587 * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal
2588 * error, though this can be revisited when this code is finally exploited.
2589 *
2590 * 'action' can be '0' to replace method, '1' to replace path, '2' to replace
Christopher Faulet312294f2020-09-02 17:17:44 +02002591 * query string, 3 to replace uri or 4 to replace the path+query.
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002592 *
2593 * In query string case, the mark question '?' must be set at the start of the
2594 * string by the caller, event if the replacement query string is empty.
2595 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002596int http_req_replace_stline(int action, const char *replace, int len,
2597 struct proxy *px, struct stream *s)
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002598{
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002599 struct htx *htx = htxbuf(&s->req.buf);
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002600
2601 switch (action) {
2602 case 0: // method
2603 if (!http_replace_req_meth(htx, ist2(replace, len)))
2604 return -1;
2605 break;
2606
2607 case 1: // path
Christopher Fauletb8ce5052020-08-31 16:11:57 +02002608 if (!http_replace_req_path(htx, ist2(replace, len), 0))
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002609 return -1;
2610 break;
2611
2612 case 2: // query
2613 if (!http_replace_req_query(htx, ist2(replace, len)))
2614 return -1;
2615 break;
2616
2617 case 3: // uri
2618 if (!http_replace_req_uri(htx, ist2(replace, len)))
2619 return -1;
2620 break;
2621
Christopher Faulet312294f2020-09-02 17:17:44 +02002622 case 4: // path + query
2623 if (!http_replace_req_path(htx, ist2(replace, len), 1))
2624 return -1;
2625 break;
2626
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002627 default:
2628 return -1;
2629 }
2630 return 0;
2631}
2632
2633/* This function replace the HTTP status code and the associated message. The
Christopher Faulete00d06c2019-12-16 17:18:42 +01002634 * variable <status> contains the new status code. This function never fails. It
2635 * returns 0 in case of success, -1 in case of internal error.
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002636 */
Christopher Faulet96bff762019-12-17 13:46:18 +01002637int http_res_set_status(unsigned int status, struct ist reason, struct stream *s)
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002638{
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002639 struct htx *htx = htxbuf(&s->res.buf);
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002640 char *res;
2641
2642 chunk_reset(&trash);
2643 res = ultoa_o(status, trash.area, trash.size);
2644 trash.data = res - trash.area;
2645
2646 /* Do we have a custom reason format string? */
Tim Duesterhuse296d3e2020-03-05 17:56:31 +01002647 if (!isttest(reason)) {
Christopher Faulet96bff762019-12-17 13:46:18 +01002648 const char *str = http_get_reason(status);
2649 reason = ist2(str, strlen(str));
2650 }
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002651
Christopher Fauletbde2c4c2020-08-31 16:43:34 +02002652 if (!http_replace_res_status(htx, ist2(trash.area, trash.data), reason))
Christopher Faulete00d06c2019-12-16 17:18:42 +01002653 return -1;
2654 return 0;
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002655}
2656
Christopher Faulet3e964192018-10-24 11:39:23 +02002657/* Executes the http-request rules <rules> for stream <s>, proxy <px> and
2658 * transaction <txn>. Returns the verdict of the first rule that prevents
2659 * further processing of the request (auth, deny, ...), and defaults to
2660 * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or
2661 * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT
2662 * on txn->flags if it encounters a tarpit rule. If <deny_status> is not NULL
2663 * and a deny/tarpit rule is matched, it will be filled with this rule's deny
2664 * status.
2665 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002666static enum rule_result http_req_get_intercept_rule(struct proxy *px, struct list *rules,
Christopher Fauletb58f62b2020-01-13 16:40:13 +01002667 struct stream *s)
Christopher Faulet3e964192018-10-24 11:39:23 +02002668{
2669 struct session *sess = strm_sess(s);
2670 struct http_txn *txn = s->txn;
Christopher Faulet3e964192018-10-24 11:39:23 +02002671 struct act_rule *rule;
Christopher Faulet3e964192018-10-24 11:39:23 +02002672 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002673 int act_opts = 0;
Christopher Faulet3e964192018-10-24 11:39:23 +02002674
Christopher Faulet3e964192018-10-24 11:39:23 +02002675 /* If "the current_rule_list" match the executed rule list, we are in
2676 * resume condition. If a resume is needed it is always in the action
2677 * and never in the ACL or converters. In this case, we initialise the
2678 * current rule, and go to the action execution point.
2679 */
2680 if (s->current_rule) {
2681 rule = s->current_rule;
2682 s->current_rule = NULL;
2683 if (s->current_rule_list == rules)
2684 goto resume_execution;
2685 }
2686 s->current_rule_list = rules;
2687
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002688 /* start the ruleset evaluation in strict mode */
2689 txn->req.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01002690
Christopher Faulet3e964192018-10-24 11:39:23 +02002691 list_for_each_entry(rule, rules, list) {
2692 /* check optional condition */
2693 if (rule->cond) {
2694 int ret;
2695
2696 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
2697 ret = acl_pass(ret);
2698
2699 if (rule->cond->pol == ACL_COND_UNLESS)
2700 ret = !ret;
2701
2702 if (!ret) /* condition not matched */
2703 continue;
2704 }
2705
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002706 act_opts |= ACT_OPT_FIRST;
Christopher Faulet3e964192018-10-24 11:39:23 +02002707 resume_execution:
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002708 /* Always call the action function if defined */
2709 if (rule->action_ptr) {
2710 if ((s->req.flags & CF_READ_ERROR) ||
2711 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
2712 (px->options & PR_O_ABRT_CLOSE)))
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002713 act_opts |= ACT_OPT_FINAL;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002714
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002715 switch (rule->action_ptr(rule, px, sess, s, act_opts)) {
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002716 case ACT_RET_CONT:
2717 break;
2718 case ACT_RET_STOP:
2719 rule_ret = HTTP_RULE_RES_STOP;
2720 goto end;
2721 case ACT_RET_YIELD:
2722 s->current_rule = rule;
2723 rule_ret = HTTP_RULE_RES_YIELD;
2724 goto end;
2725 case ACT_RET_ERR:
2726 rule_ret = HTTP_RULE_RES_ERROR;
2727 goto end;
2728 case ACT_RET_DONE:
2729 rule_ret = HTTP_RULE_RES_DONE;
2730 goto end;
2731 case ACT_RET_DENY:
Christopher Fauletb58f62b2020-01-13 16:40:13 +01002732 if (txn->status == -1)
2733 txn->status = 403;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002734 rule_ret = HTTP_RULE_RES_DENY;
2735 goto end;
2736 case ACT_RET_ABRT:
2737 rule_ret = HTTP_RULE_RES_ABRT;
2738 goto end;
2739 case ACT_RET_INV:
2740 rule_ret = HTTP_RULE_RES_BADREQ;
2741 goto end;
2742 }
2743 continue; /* eval the next rule */
2744 }
2745
2746 /* If not action function defined, check for known actions */
Christopher Faulet3e964192018-10-24 11:39:23 +02002747 switch (rule->action) {
2748 case ACT_ACTION_ALLOW:
2749 rule_ret = HTTP_RULE_RES_STOP;
2750 goto end;
2751
2752 case ACT_ACTION_DENY:
Christopher Faulet5cb513a2020-05-13 17:56:56 +02002753 txn->status = rule->arg.http_reply->status;
2754 txn->http_reply = rule->arg.http_reply;
Christopher Faulet3e964192018-10-24 11:39:23 +02002755 rule_ret = HTTP_RULE_RES_DENY;
2756 goto end;
2757
2758 case ACT_HTTP_REQ_TARPIT:
2759 txn->flags |= TX_CLTARPIT;
Christopher Faulet5cb513a2020-05-13 17:56:56 +02002760 txn->status = rule->arg.http_reply->status;
2761 txn->http_reply = rule->arg.http_reply;
Christopher Faulet3e964192018-10-24 11:39:23 +02002762 rule_ret = HTTP_RULE_RES_DENY;
2763 goto end;
2764
Christopher Faulet3e964192018-10-24 11:39:23 +02002765 case ACT_HTTP_REDIR:
Christopher Faulet90d22a82020-03-06 11:18:39 +01002766 rule_ret = HTTP_RULE_RES_ABRT;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002767 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
Christopher Faulet3a26bee2019-12-16 12:47:40 +01002768 rule_ret = HTTP_RULE_RES_ERROR;
Christopher Faulet3e964192018-10-24 11:39:23 +02002769 goto end;
2770
2771 case ACT_HTTP_SET_NICE:
Christopher Faulet96bff762019-12-17 13:46:18 +01002772 s->task->nice = rule->arg.http.i;
Christopher Faulet3e964192018-10-24 11:39:23 +02002773 break;
2774
2775 case ACT_HTTP_SET_TOS:
Christopher Faulet96bff762019-12-17 13:46:18 +01002776 conn_set_tos(objt_conn(sess->origin), rule->arg.http.i);
Christopher Faulet3e964192018-10-24 11:39:23 +02002777 break;
2778
2779 case ACT_HTTP_SET_MARK:
Christopher Faulet96bff762019-12-17 13:46:18 +01002780 conn_set_mark(objt_conn(sess->origin), rule->arg.http.i);
Christopher Faulet3e964192018-10-24 11:39:23 +02002781 break;
2782
2783 case ACT_HTTP_SET_LOGL:
Christopher Faulet96bff762019-12-17 13:46:18 +01002784 s->logs.level = rule->arg.http.i;
Christopher Faulet3e964192018-10-24 11:39:23 +02002785 break;
2786
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002787 /* other flags exists, but normally, they never be matched. */
Christopher Faulet3e964192018-10-24 11:39:23 +02002788 default:
2789 break;
2790 }
2791 }
2792
2793 end:
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002794 /* if the ruleset evaluation is finished reset the strict mode */
Christopher Faulet46f95542019-12-20 10:07:22 +01002795 if (rule_ret != HTTP_RULE_RES_YIELD)
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002796 txn->req.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01002797
Christopher Faulet3e964192018-10-24 11:39:23 +02002798 /* we reached the end of the rules, nothing to report */
2799 return rule_ret;
2800}
2801
2802/* Executes the http-response rules <rules> for stream <s> and proxy <px>. It
2803 * returns one of 5 possible statuses: HTTP_RULE_RES_CONT, HTTP_RULE_RES_STOP,
2804 * HTTP_RULE_RES_DONE, HTTP_RULE_RES_YIELD, or HTTP_RULE_RES_BADREQ. If *CONT
2805 * is returned, the process can continue the evaluation of next rule list. If
2806 * *STOP or *DONE is returned, the process must stop the evaluation. If *BADREQ
2807 * is returned, it means the operation could not be processed and a server error
Christopher Fauleta53abad2020-05-13 08:12:22 +02002808 * must be returned. If *YIELD is returned, the caller must call again the
2809 * function with the same context.
Christopher Faulet3e964192018-10-24 11:39:23 +02002810 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002811static enum rule_result http_res_get_intercept_rule(struct proxy *px, struct list *rules,
2812 struct stream *s)
Christopher Faulet3e964192018-10-24 11:39:23 +02002813{
2814 struct session *sess = strm_sess(s);
2815 struct http_txn *txn = s->txn;
Christopher Faulet3e964192018-10-24 11:39:23 +02002816 struct act_rule *rule;
Christopher Faulet3e964192018-10-24 11:39:23 +02002817 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002818 int act_opts = 0;
Christopher Faulet3e964192018-10-24 11:39:23 +02002819
Christopher Faulet3e964192018-10-24 11:39:23 +02002820 /* If "the current_rule_list" match the executed rule list, we are in
2821 * resume condition. If a resume is needed it is always in the action
2822 * and never in the ACL or converters. In this case, we initialise the
2823 * current rule, and go to the action execution point.
2824 */
2825 if (s->current_rule) {
2826 rule = s->current_rule;
2827 s->current_rule = NULL;
2828 if (s->current_rule_list == rules)
2829 goto resume_execution;
2830 }
2831 s->current_rule_list = rules;
2832
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002833 /* start the ruleset evaluation in strict mode */
2834 txn->rsp.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01002835
Christopher Faulet3e964192018-10-24 11:39:23 +02002836 list_for_each_entry(rule, rules, list) {
2837 /* check optional condition */
2838 if (rule->cond) {
2839 int ret;
2840
2841 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
2842 ret = acl_pass(ret);
2843
2844 if (rule->cond->pol == ACL_COND_UNLESS)
2845 ret = !ret;
2846
2847 if (!ret) /* condition not matched */
2848 continue;
2849 }
2850
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002851 act_opts |= ACT_OPT_FIRST;
Christopher Faulet3e964192018-10-24 11:39:23 +02002852resume_execution:
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002853
2854 /* Always call the action function if defined */
2855 if (rule->action_ptr) {
2856 if ((s->req.flags & CF_READ_ERROR) ||
2857 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
2858 (px->options & PR_O_ABRT_CLOSE)))
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002859 act_opts |= ACT_OPT_FINAL;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002860
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002861 switch (rule->action_ptr(rule, px, sess, s, act_opts)) {
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002862 case ACT_RET_CONT:
2863 break;
2864 case ACT_RET_STOP:
2865 rule_ret = HTTP_RULE_RES_STOP;
2866 goto end;
2867 case ACT_RET_YIELD:
2868 s->current_rule = rule;
2869 rule_ret = HTTP_RULE_RES_YIELD;
2870 goto end;
2871 case ACT_RET_ERR:
2872 rule_ret = HTTP_RULE_RES_ERROR;
2873 goto end;
2874 case ACT_RET_DONE:
2875 rule_ret = HTTP_RULE_RES_DONE;
2876 goto end;
2877 case ACT_RET_DENY:
Christopher Fauletb58f62b2020-01-13 16:40:13 +01002878 if (txn->status == -1)
2879 txn->status = 502;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002880 rule_ret = HTTP_RULE_RES_DENY;
2881 goto end;
2882 case ACT_RET_ABRT:
2883 rule_ret = HTTP_RULE_RES_ABRT;
2884 goto end;
2885 case ACT_RET_INV:
2886 rule_ret = HTTP_RULE_RES_BADREQ;
2887 goto end;
2888 }
2889 continue; /* eval the next rule */
2890 }
2891
2892 /* If not action function defined, check for known actions */
Christopher Faulet3e964192018-10-24 11:39:23 +02002893 switch (rule->action) {
2894 case ACT_ACTION_ALLOW:
2895 rule_ret = HTTP_RULE_RES_STOP; /* "allow" rules are OK */
2896 goto end;
2897
2898 case ACT_ACTION_DENY:
Christopher Faulet5cb513a2020-05-13 17:56:56 +02002899 txn->status = rule->arg.http_reply->status;
2900 txn->http_reply = rule->arg.http_reply;
Christopher Faulet3a26bee2019-12-16 12:47:40 +01002901 rule_ret = HTTP_RULE_RES_DENY;
Christopher Faulet3e964192018-10-24 11:39:23 +02002902 goto end;
2903
2904 case ACT_HTTP_SET_NICE:
Christopher Faulet96bff762019-12-17 13:46:18 +01002905 s->task->nice = rule->arg.http.i;
Christopher Faulet3e964192018-10-24 11:39:23 +02002906 break;
2907
2908 case ACT_HTTP_SET_TOS:
Christopher Faulet96bff762019-12-17 13:46:18 +01002909 conn_set_tos(objt_conn(sess->origin), rule->arg.http.i);
Christopher Faulet3e964192018-10-24 11:39:23 +02002910 break;
2911
2912 case ACT_HTTP_SET_MARK:
Christopher Faulet96bff762019-12-17 13:46:18 +01002913 conn_set_mark(objt_conn(sess->origin), rule->arg.http.i);
Christopher Faulet3e964192018-10-24 11:39:23 +02002914 break;
2915
2916 case ACT_HTTP_SET_LOGL:
Christopher Faulet96bff762019-12-17 13:46:18 +01002917 s->logs.level = rule->arg.http.i;
Christopher Faulet3e964192018-10-24 11:39:23 +02002918 break;
2919
Christopher Faulet3e964192018-10-24 11:39:23 +02002920 case ACT_HTTP_REDIR:
Christopher Faulet49c2a702020-03-06 15:44:37 +01002921 rule_ret = HTTP_RULE_RES_ABRT;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002922 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
Christopher Faulet3a26bee2019-12-16 12:47:40 +01002923 rule_ret = HTTP_RULE_RES_ERROR;
Christopher Faulet3e964192018-10-24 11:39:23 +02002924 goto end;
2925
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002926 /* other flags exists, but normally, they never be matched. */
Christopher Faulet3e964192018-10-24 11:39:23 +02002927 default:
2928 break;
2929 }
2930 }
2931
2932 end:
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002933 /* if the ruleset evaluation is finished reset the strict mode */
Christopher Faulet46f95542019-12-20 10:07:22 +01002934 if (rule_ret != HTTP_RULE_RES_YIELD)
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002935 txn->rsp.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01002936
Christopher Faulet3e964192018-10-24 11:39:23 +02002937 /* we reached the end of the rules, nothing to report */
2938 return rule_ret;
2939}
2940
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002941/* Executes backend and frontend http-after-response rules for the stream <s>,
2942 * in that order. it return 1 on success and 0 on error. It is the caller
2943 * responsibility to catch error or ignore it. If it catches it, this function
2944 * may be called a second time, for the internal error.
2945 */
2946int http_eval_after_res_rules(struct stream *s)
2947{
2948 struct session *sess = s->sess;
2949 enum rule_result ret = HTTP_RULE_RES_CONT;
2950
Christopher Faulet507479b2020-05-15 12:29:46 +02002951 /* Eval after-response ruleset only if the reply is not const */
2952 if (s->txn->flags & TX_CONST_REPLY)
2953 goto end;
2954
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002955 /* prune the request variables if not already done and swap to the response variables. */
2956 if (s->vars_reqres.scope != SCOPE_RES) {
2957 if (!LIST_ISEMPTY(&s->vars_reqres.head))
2958 vars_prune(&s->vars_reqres, s->sess, s);
2959 vars_init(&s->vars_reqres, SCOPE_RES);
2960 }
2961
2962 ret = http_res_get_intercept_rule(s->be, &s->be->http_after_res_rules, s);
2963 if ((ret == HTTP_RULE_RES_CONT || ret == HTTP_RULE_RES_STOP) && sess->fe != s->be)
2964 ret = http_res_get_intercept_rule(sess->fe, &sess->fe->http_after_res_rules, s);
2965
Christopher Faulet507479b2020-05-15 12:29:46 +02002966 end:
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002967 /* All other codes than CONTINUE, STOP or DONE are forbidden */
2968 return (ret == HTTP_RULE_RES_CONT || ret == HTTP_RULE_RES_STOP || ret == HTTP_RULE_RES_DONE);
2969}
2970
Christopher Fauletfcda7c62018-10-24 11:56:22 +02002971/*
2972 * Manage client-side cookie. It can impact performance by about 2% so it is
2973 * desirable to call it only when needed. This code is quite complex because
2974 * of the multiple very crappy and ambiguous syntaxes we have to support. it
2975 * highly recommended not to touch this part without a good reason !
2976 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002977static void http_manage_client_side_cookies(struct stream *s, struct channel *req)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02002978{
2979 struct session *sess = s->sess;
2980 struct http_txn *txn = s->txn;
2981 struct htx *htx;
2982 struct http_hdr_ctx ctx;
2983 char *hdr_beg, *hdr_end, *del_from;
2984 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
2985 int preserve_hdr;
2986
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002987 htx = htxbuf(&req->buf);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02002988 ctx.blk = NULL;
2989 while (http_find_header(htx, ist("Cookie"), &ctx, 1)) {
Olivier Houchardf0f42382019-07-22 17:43:46 +02002990 int is_first = 1;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02002991 del_from = NULL; /* nothing to be deleted */
2992 preserve_hdr = 0; /* assume we may kill the whole header */
2993
2994 /* Now look for cookies. Conforming to RFC2109, we have to support
2995 * attributes whose name begin with a '$', and associate them with
2996 * the right cookie, if we want to delete this cookie.
2997 * So there are 3 cases for each cookie read :
2998 * 1) it's a special attribute, beginning with a '$' : ignore it.
2999 * 2) it's a server id cookie that we *MAY* want to delete : save
3000 * some pointers on it (last semi-colon, beginning of cookie...)
3001 * 3) it's an application cookie : we *MAY* have to delete a previous
3002 * "special" cookie.
3003 * At the end of loop, if a "special" cookie remains, we may have to
3004 * remove it. If no application cookie persists in the header, we
3005 * *MUST* delete it.
3006 *
3007 * Note: RFC2965 is unclear about the processing of spaces around
3008 * the equal sign in the ATTR=VALUE form. A careful inspection of
3009 * the RFC explicitly allows spaces before it, and not within the
3010 * tokens (attrs or values). An inspection of RFC2109 allows that
3011 * too but section 10.1.3 lets one think that spaces may be allowed
3012 * after the equal sign too, resulting in some (rare) buggy
3013 * implementations trying to do that. So let's do what servers do.
3014 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
3015 * allowed quoted strings in values, with any possible character
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003016 * after a backslash, including control chars and delimiters, which
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003017 * causes parsing to become ambiguous. Browsers also allow spaces
3018 * within values even without quotes.
3019 *
3020 * We have to keep multiple pointers in order to support cookie
3021 * removal at the beginning, middle or end of header without
3022 * corrupting the header. All of these headers are valid :
3023 *
3024 * hdr_beg hdr_end
3025 * | |
3026 * v |
3027 * NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3 |
3028 * NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3 v
3029 * NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3
3030 * | | | | | | |
3031 * | | | | | | |
3032 * | | | | | | +--> next
3033 * | | | | | +----> val_end
3034 * | | | | +-----------> val_beg
3035 * | | | +--------------> equal
3036 * | | +----------------> att_end
3037 * | +---------------------> att_beg
3038 * +--------------------------> prev
3039 *
3040 */
3041 hdr_beg = ctx.value.ptr;
3042 hdr_end = hdr_beg + ctx.value.len;
3043 for (prev = hdr_beg; prev < hdr_end; prev = next) {
3044 /* Iterate through all cookies on this line */
3045
3046 /* find att_beg */
3047 att_beg = prev;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003048 if (!is_first)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003049 att_beg++;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003050 is_first = 0;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003051
3052 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
3053 att_beg++;
3054
3055 /* find att_end : this is the first character after the last non
3056 * space before the equal. It may be equal to hdr_end.
3057 */
3058 equal = att_end = att_beg;
3059 while (equal < hdr_end) {
3060 if (*equal == '=' || *equal == ',' || *equal == ';')
3061 break;
3062 if (HTTP_IS_SPHT(*equal++))
3063 continue;
3064 att_end = equal;
3065 }
3066
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003067 /* here, <equal> points to '=', a delimiter or the end. <att_end>
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003068 * is between <att_beg> and <equal>, both may be identical.
3069 */
3070 /* look for end of cookie if there is an equal sign */
3071 if (equal < hdr_end && *equal == '=') {
3072 /* look for the beginning of the value */
3073 val_beg = equal + 1;
3074 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
3075 val_beg++;
3076
3077 /* find the end of the value, respecting quotes */
3078 next = http_find_cookie_value_end(val_beg, hdr_end);
3079
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003080 /* make val_end point to the first white space or delimiter after the value */
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003081 val_end = next;
3082 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
3083 val_end--;
3084 }
3085 else
3086 val_beg = val_end = next = equal;
3087
3088 /* We have nothing to do with attributes beginning with
3089 * '$'. However, they will automatically be removed if a
3090 * header before them is removed, since they're supposed
3091 * to be linked together.
3092 */
3093 if (*att_beg == '$')
3094 continue;
3095
3096 /* Ignore cookies with no equal sign */
3097 if (equal == next) {
3098 /* This is not our cookie, so we must preserve it. But if we already
3099 * scheduled another cookie for removal, we cannot remove the
3100 * complete header, but we can remove the previous block itself.
3101 */
3102 preserve_hdr = 1;
3103 if (del_from != NULL) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003104 int delta = http_del_hdr_value(hdr_beg, hdr_end, &del_from, prev);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003105 val_end += delta;
3106 next += delta;
3107 hdr_end += delta;
3108 prev = del_from;
3109 del_from = NULL;
3110 }
3111 continue;
3112 }
3113
3114 /* if there are spaces around the equal sign, we need to
3115 * strip them otherwise we'll get trouble for cookie captures,
3116 * or even for rewrites. Since this happens extremely rarely,
3117 * it does not hurt performance.
3118 */
3119 if (unlikely(att_end != equal || val_beg > equal + 1)) {
3120 int stripped_before = 0;
3121 int stripped_after = 0;
3122
3123 if (att_end != equal) {
3124 memmove(att_end, equal, hdr_end - equal);
3125 stripped_before = (att_end - equal);
3126 equal += stripped_before;
3127 val_beg += stripped_before;
3128 }
3129
3130 if (val_beg > equal + 1) {
3131 memmove(equal + 1, val_beg, hdr_end + stripped_before - val_beg);
3132 stripped_after = (equal + 1) - val_beg;
3133 val_beg += stripped_after;
3134 stripped_before += stripped_after;
3135 }
3136
3137 val_end += stripped_before;
3138 next += stripped_before;
3139 hdr_end += stripped_before;
3140 }
3141 /* now everything is as on the diagram above */
3142
3143 /* First, let's see if we want to capture this cookie. We check
3144 * that we don't already have a client side cookie, because we
3145 * can only capture one. Also as an optimisation, we ignore
3146 * cookies shorter than the declared name.
3147 */
3148 if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL &&
3149 (val_end - att_beg >= sess->fe->capture_namelen) &&
3150 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
3151 int log_len = val_end - att_beg;
3152
3153 if ((txn->cli_cookie = pool_alloc(pool_head_capture)) == NULL) {
3154 ha_alert("HTTP logging : out of memory.\n");
3155 } else {
3156 if (log_len > sess->fe->capture_len)
3157 log_len = sess->fe->capture_len;
3158 memcpy(txn->cli_cookie, att_beg, log_len);
3159 txn->cli_cookie[log_len] = 0;
3160 }
3161 }
3162
3163 /* Persistence cookies in passive, rewrite or insert mode have the
3164 * following form :
3165 *
3166 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
3167 *
3168 * For cookies in prefix mode, the form is :
3169 *
3170 * Cookie: NAME=SRV~VALUE
3171 */
3172 if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
3173 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
3174 struct server *srv = s->be->srv;
3175 char *delim;
3176
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003177 /* if we're in cookie prefix mode, we'll search the delimiter so that we
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003178 * have the server ID between val_beg and delim, and the original cookie between
3179 * delim+1 and val_end. Otherwise, delim==val_end :
3180 *
3181 * hdr_beg
3182 * |
3183 * v
3184 * NAME=SRV; # in all but prefix modes
3185 * NAME=SRV~OPAQUE ; # in prefix mode
3186 * || || | |+-> next
3187 * || || | +--> val_end
3188 * || || +---------> delim
3189 * || |+------------> val_beg
3190 * || +-------------> att_end = equal
3191 * |+-----------------> att_beg
3192 * +------------------> prev
3193 *
3194 */
3195 if (s->be->ck_opts & PR_CK_PFX) {
3196 for (delim = val_beg; delim < val_end; delim++)
3197 if (*delim == COOKIE_DELIM)
3198 break;
3199 }
3200 else {
3201 char *vbar1;
3202 delim = val_end;
3203 /* Now check if the cookie contains a date field, which would
3204 * appear after a vertical bar ('|') just after the server name
3205 * and before the delimiter.
3206 */
3207 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
3208 if (vbar1) {
3209 /* OK, so left of the bar is the server's cookie and
3210 * right is the last seen date. It is a base64 encoded
3211 * 30-bit value representing the UNIX date since the
3212 * epoch in 4-second quantities.
3213 */
3214 int val;
3215 delim = vbar1++;
3216 if (val_end - vbar1 >= 5) {
3217 val = b64tos30(vbar1);
3218 if (val > 0)
3219 txn->cookie_last_date = val << 2;
3220 }
3221 /* look for a second vertical bar */
3222 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
3223 if (vbar1 && (val_end - vbar1 > 5)) {
3224 val = b64tos30(vbar1 + 1);
3225 if (val > 0)
3226 txn->cookie_first_date = val << 2;
3227 }
3228 }
3229 }
3230
3231 /* if the cookie has an expiration date and the proxy wants to check
3232 * it, then we do that now. We first check if the cookie is too old,
3233 * then only if it has expired. We detect strict overflow because the
3234 * time resolution here is not great (4 seconds). Cookies with dates
3235 * in the future are ignored if their offset is beyond one day. This
3236 * allows an admin to fix timezone issues without expiring everyone
3237 * and at the same time avoids keeping unwanted side effects for too
3238 * long.
3239 */
3240 if (txn->cookie_first_date && s->be->cookie_maxlife &&
3241 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) ||
3242 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
3243 txn->flags &= ~TX_CK_MASK;
3244 txn->flags |= TX_CK_OLD;
3245 delim = val_beg; // let's pretend we have not found the cookie
3246 txn->cookie_first_date = 0;
3247 txn->cookie_last_date = 0;
3248 }
3249 else if (txn->cookie_last_date && s->be->cookie_maxidle &&
3250 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) ||
3251 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
3252 txn->flags &= ~TX_CK_MASK;
3253 txn->flags |= TX_CK_EXPIRED;
3254 delim = val_beg; // let's pretend we have not found the cookie
3255 txn->cookie_first_date = 0;
3256 txn->cookie_last_date = 0;
3257 }
3258
3259 /* Here, we'll look for the first running server which supports the cookie.
3260 * This allows to share a same cookie between several servers, for example
3261 * to dedicate backup servers to specific servers only.
3262 * However, to prevent clients from sticking to cookie-less backup server
3263 * when they have incidentely learned an empty cookie, we simply ignore
3264 * empty cookies and mark them as invalid.
3265 * The same behaviour is applied when persistence must be ignored.
3266 */
3267 if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
3268 srv = NULL;
3269
3270 while (srv) {
3271 if (srv->cookie && (srv->cklen == delim - val_beg) &&
3272 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
3273 if ((srv->cur_state != SRV_ST_STOPPED) ||
3274 (s->be->options & PR_O_PERSIST) ||
3275 (s->flags & SF_FORCE_PRST)) {
3276 /* we found the server and we can use it */
3277 txn->flags &= ~TX_CK_MASK;
3278 txn->flags |= (srv->cur_state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
3279 s->flags |= SF_DIRECT | SF_ASSIGNED;
3280 s->target = &srv->obj_type;
3281 break;
3282 } else {
3283 /* we found a server, but it's down,
3284 * mark it as such and go on in case
3285 * another one is available.
3286 */
3287 txn->flags &= ~TX_CK_MASK;
3288 txn->flags |= TX_CK_DOWN;
3289 }
3290 }
3291 srv = srv->next;
3292 }
3293
3294 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
3295 /* no server matched this cookie or we deliberately skipped it */
3296 txn->flags &= ~TX_CK_MASK;
3297 if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
3298 txn->flags |= TX_CK_UNUSED;
3299 else
3300 txn->flags |= TX_CK_INVALID;
3301 }
3302
3303 /* depending on the cookie mode, we may have to either :
3304 * - delete the complete cookie if we're in insert+indirect mode, so that
3305 * the server never sees it ;
3306 * - remove the server id from the cookie value, and tag the cookie as an
Joseph Herlante9d5c722018-11-25 11:00:25 -08003307 * application cookie so that it does not get accidentally removed later,
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003308 * if we're in cookie prefix mode
3309 */
3310 if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
3311 int delta; /* negative */
3312
3313 memmove(val_beg, delim + 1, hdr_end - (delim + 1));
3314 delta = val_beg - (delim + 1);
3315 val_end += delta;
3316 next += delta;
3317 hdr_end += delta;
3318 del_from = NULL;
3319 preserve_hdr = 1; /* we want to keep this cookie */
3320 }
3321 else if (del_from == NULL &&
3322 (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
3323 del_from = prev;
3324 }
3325 }
3326 else {
3327 /* This is not our cookie, so we must preserve it. But if we already
3328 * scheduled another cookie for removal, we cannot remove the
3329 * complete header, but we can remove the previous block itself.
3330 */
3331 preserve_hdr = 1;
3332
3333 if (del_from != NULL) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003334 int delta = http_del_hdr_value(hdr_beg, hdr_end, &del_from, prev);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003335 if (att_beg >= del_from)
3336 att_beg += delta;
3337 if (att_end >= del_from)
3338 att_end += delta;
3339 val_beg += delta;
3340 val_end += delta;
3341 next += delta;
3342 hdr_end += delta;
3343 prev = del_from;
3344 del_from = NULL;
3345 }
3346 }
3347
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003348 } /* for each cookie */
3349
3350
3351 /* There are no more cookies on this line.
3352 * We may still have one (or several) marked for deletion at the
3353 * end of the line. We must do this now in two ways :
3354 * - if some cookies must be preserved, we only delete from the
3355 * mark to the end of line ;
3356 * - if nothing needs to be preserved, simply delete the whole header
3357 */
3358 if (del_from) {
3359 hdr_end = (preserve_hdr ? del_from : hdr_beg);
3360 }
3361 if ((hdr_end - hdr_beg) != ctx.value.len) {
Christopher Faulet3e2638e2019-06-18 09:49:16 +02003362 if (hdr_beg != hdr_end)
3363 htx_change_blk_value_len(htx, ctx.blk, hdr_end - hdr_beg);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003364 else
3365 http_remove_header(htx, &ctx);
3366 }
3367 } /* for each "Cookie header */
3368}
3369
3370/*
3371 * Manage server-side cookies. It can impact performance by about 2% so it is
3372 * desirable to call it only when needed. This function is also used when we
3373 * just need to know if there is a cookie (eg: for check-cache).
3374 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003375static void http_manage_server_side_cookies(struct stream *s, struct channel *res)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003376{
3377 struct session *sess = s->sess;
3378 struct http_txn *txn = s->txn;
3379 struct htx *htx;
3380 struct http_hdr_ctx ctx;
3381 struct server *srv;
3382 char *hdr_beg, *hdr_end;
3383 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau6f7a02a2019-04-15 21:49:49 +02003384 int is_cookie2 = 0;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003385
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003386 htx = htxbuf(&res->buf);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003387
3388 ctx.blk = NULL;
3389 while (1) {
Olivier Houchardf0f42382019-07-22 17:43:46 +02003390 int is_first = 1;
3391
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003392 if (!http_find_header(htx, ist("Set-Cookie"), &ctx, 1)) {
3393 if (!http_find_header(htx, ist("Set-Cookie2"), &ctx, 1))
3394 break;
3395 is_cookie2 = 1;
3396 }
3397
3398 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
3399 * <prev> points to the colon.
3400 */
3401 txn->flags |= TX_SCK_PRESENT;
3402
3403 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
3404 * check-cache is enabled) and we are not interested in checking
3405 * them. Warning, the cookie capture is declared in the frontend.
3406 */
3407 if (s->be->cookie_name == NULL && sess->fe->capture_name == NULL)
3408 break;
3409
3410 /* OK so now we know we have to process this response cookie.
3411 * The format of the Set-Cookie header is slightly different
3412 * from the format of the Cookie header in that it does not
3413 * support the comma as a cookie delimiter (thus the header
3414 * cannot be folded) because the Expires attribute described in
3415 * the original Netscape's spec may contain an unquoted date
3416 * with a comma inside. We have to live with this because
3417 * many browsers don't support Max-Age and some browsers don't
3418 * support quoted strings. However the Set-Cookie2 header is
3419 * clean.
3420 *
3421 * We have to keep multiple pointers in order to support cookie
3422 * removal at the beginning, middle or end of header without
3423 * corrupting the header (in case of set-cookie2). A special
3424 * pointer, <scav> points to the beginning of the set-cookie-av
3425 * fields after the first semi-colon. The <next> pointer points
3426 * either to the end of line (set-cookie) or next unquoted comma
3427 * (set-cookie2). All of these headers are valid :
3428 *
3429 * hdr_beg hdr_end
3430 * | |
3431 * v |
3432 * NAME1 = VALUE 1 ; Secure; Path="/" |
3433 * NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT v
3434 * NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT
3435 * NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard
3436 * | | | | | | | |
3437 * | | | | | | | +-> next
3438 * | | | | | | +------------> scav
3439 * | | | | | +--------------> val_end
3440 * | | | | +--------------------> val_beg
3441 * | | | +----------------------> equal
3442 * | | +------------------------> att_end
3443 * | +----------------------------> att_beg
3444 * +------------------------------> prev
3445 * -------------------------------> hdr_beg
3446 */
3447 hdr_beg = ctx.value.ptr;
3448 hdr_end = hdr_beg + ctx.value.len;
3449 for (prev = hdr_beg; prev < hdr_end; prev = next) {
3450
3451 /* Iterate through all cookies on this line */
3452
3453 /* find att_beg */
3454 att_beg = prev;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003455 if (!is_first)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003456 att_beg++;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003457 is_first = 0;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003458
3459 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
3460 att_beg++;
3461
3462 /* find att_end : this is the first character after the last non
3463 * space before the equal. It may be equal to hdr_end.
3464 */
3465 equal = att_end = att_beg;
3466
3467 while (equal < hdr_end) {
3468 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
3469 break;
3470 if (HTTP_IS_SPHT(*equal++))
3471 continue;
3472 att_end = equal;
3473 }
3474
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003475 /* here, <equal> points to '=', a delimiter or the end. <att_end>
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003476 * is between <att_beg> and <equal>, both may be identical.
3477 */
3478
3479 /* look for end of cookie if there is an equal sign */
3480 if (equal < hdr_end && *equal == '=') {
3481 /* look for the beginning of the value */
3482 val_beg = equal + 1;
3483 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
3484 val_beg++;
3485
3486 /* find the end of the value, respecting quotes */
3487 next = http_find_cookie_value_end(val_beg, hdr_end);
3488
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003489 /* make val_end point to the first white space or delimiter after the value */
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003490 val_end = next;
3491 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
3492 val_end--;
3493 }
3494 else {
3495 /* <equal> points to next comma, semi-colon or EOL */
3496 val_beg = val_end = next = equal;
3497 }
3498
3499 if (next < hdr_end) {
3500 /* Set-Cookie2 supports multiple cookies, and <next> points to
3501 * a colon or semi-colon before the end. So skip all attr-value
3502 * pairs and look for the next comma. For Set-Cookie, since
3503 * commas are permitted in values, skip to the end.
3504 */
3505 if (is_cookie2)
3506 next = http_find_hdr_value_end(next, hdr_end);
3507 else
3508 next = hdr_end;
3509 }
3510
3511 /* Now everything is as on the diagram above */
3512
3513 /* Ignore cookies with no equal sign */
3514 if (equal == val_end)
3515 continue;
3516
3517 /* If there are spaces around the equal sign, we need to
3518 * strip them otherwise we'll get trouble for cookie captures,
3519 * or even for rewrites. Since this happens extremely rarely,
3520 * it does not hurt performance.
3521 */
3522 if (unlikely(att_end != equal || val_beg > equal + 1)) {
3523 int stripped_before = 0;
3524 int stripped_after = 0;
3525
3526 if (att_end != equal) {
3527 memmove(att_end, equal, hdr_end - equal);
3528 stripped_before = (att_end - equal);
3529 equal += stripped_before;
3530 val_beg += stripped_before;
3531 }
3532
3533 if (val_beg > equal + 1) {
3534 memmove(equal + 1, val_beg, hdr_end + stripped_before - val_beg);
3535 stripped_after = (equal + 1) - val_beg;
3536 val_beg += stripped_after;
3537 stripped_before += stripped_after;
3538 }
3539
3540 val_end += stripped_before;
3541 next += stripped_before;
3542 hdr_end += stripped_before;
3543
Christopher Faulet3e2638e2019-06-18 09:49:16 +02003544 htx_change_blk_value_len(htx, ctx.blk, hdr_end - hdr_beg);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003545 ctx.value.len = hdr_end - hdr_beg;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003546 }
3547
3548 /* First, let's see if we want to capture this cookie. We check
3549 * that we don't already have a server side cookie, because we
3550 * can only capture one. Also as an optimisation, we ignore
3551 * cookies shorter than the declared name.
3552 */
3553 if (sess->fe->capture_name != NULL &&
3554 txn->srv_cookie == NULL &&
3555 (val_end - att_beg >= sess->fe->capture_namelen) &&
3556 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
3557 int log_len = val_end - att_beg;
3558 if ((txn->srv_cookie = pool_alloc(pool_head_capture)) == NULL) {
3559 ha_alert("HTTP logging : out of memory.\n");
3560 }
3561 else {
3562 if (log_len > sess->fe->capture_len)
3563 log_len = sess->fe->capture_len;
3564 memcpy(txn->srv_cookie, att_beg, log_len);
3565 txn->srv_cookie[log_len] = 0;
3566 }
3567 }
3568
3569 srv = objt_server(s->target);
3570 /* now check if we need to process it for persistence */
3571 if (!(s->flags & SF_IGNORE_PRST) &&
3572 (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
3573 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
3574 /* assume passive cookie by default */
3575 txn->flags &= ~TX_SCK_MASK;
3576 txn->flags |= TX_SCK_FOUND;
3577
3578 /* If the cookie is in insert mode on a known server, we'll delete
3579 * this occurrence because we'll insert another one later.
3580 * We'll delete it too if the "indirect" option is set and we're in
3581 * a direct access.
3582 */
3583 if (s->be->ck_opts & PR_CK_PSV) {
3584 /* The "preserve" flag was set, we don't want to touch the
3585 * server's cookie.
3586 */
3587 }
3588 else if ((srv && (s->be->ck_opts & PR_CK_INS)) ||
3589 ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) {
3590 /* this cookie must be deleted */
3591 if (prev == hdr_beg && next == hdr_end) {
3592 /* whole header */
3593 http_remove_header(htx, &ctx);
3594 /* note: while both invalid now, <next> and <hdr_end>
3595 * are still equal, so the for() will stop as expected.
3596 */
3597 } else {
3598 /* just remove the value */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003599 int delta = http_del_hdr_value(hdr_beg, hdr_end, &prev, next);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003600 next = prev;
3601 hdr_end += delta;
3602 }
3603 txn->flags &= ~TX_SCK_MASK;
3604 txn->flags |= TX_SCK_DELETED;
3605 /* and go on with next cookie */
3606 }
3607 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) {
3608 /* replace bytes val_beg->val_end with the cookie name associated
3609 * with this server since we know it.
3610 */
3611 int sliding, delta;
3612
3613 ctx.value = ist2(val_beg, val_end - val_beg);
3614 ctx.lws_before = ctx.lws_after = 0;
3615 http_replace_header_value(htx, &ctx, ist2(srv->cookie, srv->cklen));
3616 delta = srv->cklen - (val_end - val_beg);
3617 sliding = (ctx.value.ptr - val_beg);
3618 hdr_beg += sliding;
3619 val_beg += sliding;
3620 next += sliding + delta;
3621 hdr_end += sliding + delta;
3622
3623 txn->flags &= ~TX_SCK_MASK;
3624 txn->flags |= TX_SCK_REPLACED;
3625 }
3626 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) {
3627 /* insert the cookie name associated with this server
3628 * before existing cookie, and insert a delimiter between them..
3629 */
3630 int sliding, delta;
3631 ctx.value = ist2(val_beg, 0);
3632 ctx.lws_before = ctx.lws_after = 0;
3633 http_replace_header_value(htx, &ctx, ist2(srv->cookie, srv->cklen + 1));
3634 delta = srv->cklen + 1;
3635 sliding = (ctx.value.ptr - val_beg);
3636 hdr_beg += sliding;
3637 val_beg += sliding;
3638 next += sliding + delta;
3639 hdr_end += sliding + delta;
3640
3641 val_beg[srv->cklen] = COOKIE_DELIM;
3642 txn->flags &= ~TX_SCK_MASK;
3643 txn->flags |= TX_SCK_REPLACED;
3644 }
3645 }
3646 /* that's done for this cookie, check the next one on the same
3647 * line when next != hdr_end (only if is_cookie2).
3648 */
3649 }
3650 }
3651}
3652
Christopher Faulet25a02f62018-10-24 12:00:25 +02003653/*
3654 * Parses the Cache-Control and Pragma request header fields to determine if
3655 * the request may be served from the cache and/or if it is cacheable. Updates
3656 * s->txn->flags.
3657 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003658void http_check_request_for_cacheability(struct stream *s, struct channel *req)
Christopher Faulet25a02f62018-10-24 12:00:25 +02003659{
3660 struct http_txn *txn = s->txn;
3661 struct htx *htx;
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003662 struct http_hdr_ctx ctx = { .blk = NULL };
3663 int pragma_found, cc_found;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003664
3665 if ((txn->flags & (TX_CACHEABLE|TX_CACHE_IGNORE)) == TX_CACHE_IGNORE)
3666 return; /* nothing more to do here */
3667
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003668 htx = htxbuf(&req->buf);
Christopher Faulet25a02f62018-10-24 12:00:25 +02003669 pragma_found = cc_found = 0;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003670
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003671 /* Check "pragma" header for HTTP/1.0 compatibility. */
3672 if (http_find_header(htx, ist("pragma"), &ctx, 1)) {
3673 if (isteqi(ctx.value, ist("no-cache"))) {
3674 pragma_found = 1;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003675 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003676 }
Christopher Faulet25a02f62018-10-24 12:00:25 +02003677
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003678 ctx.blk = NULL;
3679 /* Don't use the cache and don't try to store if we found the
3680 * Authorization header */
3681 if (http_find_header(htx, ist("authorization"), &ctx, 1)) {
3682 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3683 txn->flags |= TX_CACHE_IGNORE;
3684 }
Christopher Faulet25a02f62018-10-24 12:00:25 +02003685
Christopher Faulet25a02f62018-10-24 12:00:25 +02003686
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003687 /* Look for "cache-control" header and iterate over all the values
3688 * until we find one that specifies that caching is possible or not. */
3689 ctx.blk = NULL;
3690 while (http_find_header(htx, ist("cache-control"), &ctx, 0)) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003691 cc_found = 1;
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003692 /* We don't check the values after max-age, max-stale nor min-fresh,
3693 * we simply don't use the cache when they're specified. */
3694 if (istmatchi(ctx.value, ist("max-age")) ||
3695 istmatchi(ctx.value, ist("no-cache")) ||
3696 istmatchi(ctx.value, ist("max-stale")) ||
3697 istmatchi(ctx.value, ist("min-fresh"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003698 txn->flags |= TX_CACHE_IGNORE;
3699 continue;
3700 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003701 if (istmatchi(ctx.value, ist("no-store"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003702 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3703 continue;
3704 }
3705 }
3706
3707 /* RFC7234#5.4:
3708 * When the Cache-Control header field is also present and
3709 * understood in a request, Pragma is ignored.
3710 * When the Cache-Control header field is not present in a
3711 * request, caches MUST consider the no-cache request
3712 * pragma-directive as having the same effect as if
3713 * "Cache-Control: no-cache" were present.
3714 */
3715 if (!cc_found && pragma_found)
3716 txn->flags |= TX_CACHE_IGNORE;
3717}
3718
3719/*
3720 * Check if response is cacheable or not. Updates s->txn->flags.
3721 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003722void http_check_response_for_cacheability(struct stream *s, struct channel *res)
Christopher Faulet25a02f62018-10-24 12:00:25 +02003723{
3724 struct http_txn *txn = s->txn;
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003725 struct http_hdr_ctx ctx = { .blk = NULL };
Christopher Faulet25a02f62018-10-24 12:00:25 +02003726 struct htx *htx;
Remi Tricot-Le Bretoncc9bf2e2020-11-12 11:14:41 +01003727 int has_freshness_info = 0;
3728 int has_validator = 0;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003729
3730 if (txn->status < 200) {
3731 /* do not try to cache interim responses! */
3732 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3733 return;
3734 }
3735
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003736 htx = htxbuf(&res->buf);
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003737 /* Check "pragma" header for HTTP/1.0 compatibility. */
3738 if (http_find_header(htx, ist("pragma"), &ctx, 1)) {
3739 if (isteqi(ctx.value, ist("no-cache"))) {
3740 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3741 return;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003742 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003743 }
Christopher Faulet25a02f62018-10-24 12:00:25 +02003744
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003745 /* Look for "cache-control" header and iterate over all the values
3746 * until we find one that specifies that caching is possible or not. */
3747 ctx.blk = NULL;
3748 while (http_find_header(htx, ist("cache-control"), &ctx, 0)) {
3749 if (isteqi(ctx.value, ist("public"))) {
3750 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003751 continue;
3752 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003753 if (isteqi(ctx.value, ist("private")) ||
3754 isteqi(ctx.value, ist("no-cache")) ||
3755 isteqi(ctx.value, ist("no-store")) ||
3756 isteqi(ctx.value, ist("max-age=0")) ||
3757 isteqi(ctx.value, ist("s-maxage=0"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003758 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003759 continue;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003760 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003761 /* We might have a no-cache="set-cookie" form. */
3762 if (istmatchi(ctx.value, ist("no-cache=\"set-cookie"))) {
3763 txn->flags &= ~TX_CACHE_COOK;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003764 continue;
3765 }
Remi Tricot-Le Bretoncc9bf2e2020-11-12 11:14:41 +01003766
3767 if (istmatchi(ctx.value, ist("s-maxage")) ||
3768 istmatchi(ctx.value, ist("max-age"))) {
3769 has_freshness_info = 1;
3770 continue;
3771 }
3772 }
3773
3774 /* If no freshness information could be found in Cache-Control values,
3775 * look for an Expires header. */
3776 if (!has_freshness_info) {
3777 ctx.blk = NULL;
3778 has_freshness_info = http_find_header(htx, ist("expires"), &ctx, 0);
Christopher Faulet25a02f62018-10-24 12:00:25 +02003779 }
Remi Tricot-Le Bretoncc9bf2e2020-11-12 11:14:41 +01003780
3781 /* If no freshness information could be found in Cache-Control or Expires
3782 * values, look for an explicit validator. */
3783 if (!has_freshness_info) {
3784 ctx.blk = NULL;
3785 has_validator = 1;
3786 if (!http_find_header(htx, ist("etag"), &ctx, 0)) {
3787 ctx.blk = NULL;
3788 if (!http_find_header(htx, ist("last-modified"), &ctx, 0))
3789 has_validator = 0;
3790 }
3791 }
3792
3793 /* We won't store an entry that has neither a cache validator nor an
3794 * explicit expiration time, as suggested in RFC 7234#3. */
3795 if (!has_freshness_info && !has_validator)
3796 txn->flags |= TX_CACHE_IGNORE;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003797}
3798
Christopher Faulet377c5a52018-10-24 21:21:30 +02003799/*
3800 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
3801 * for the current backend.
3802 *
3803 * It is assumed that the request is either a HEAD, GET, or POST and that the
3804 * uri_auth field is valid.
3805 *
3806 * Returns 1 if stats should be provided, otherwise 0.
3807 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003808static int http_stats_check_uri(struct stream *s, struct http_txn *txn, struct proxy *backend)
Christopher Faulet377c5a52018-10-24 21:21:30 +02003809{
3810 struct uri_auth *uri_auth = backend->uri_auth;
3811 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003812 struct htx_sl *sl;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003813 struct ist uri;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003814
3815 if (!uri_auth)
3816 return 0;
3817
3818 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
3819 return 0;
3820
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003821 htx = htxbuf(&s->req.buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02003822 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003823 uri = htx_sl_req_uri(sl);
Willy Tarreau1eb3b482019-10-31 15:50:28 +01003824 if (*uri_auth->uri_prefix == '/')
3825 uri = http_get_path(uri);
Christopher Faulet377c5a52018-10-24 21:21:30 +02003826
3827 /* check URI size */
3828 if (uri_auth->uri_len > uri.len)
3829 return 0;
3830
3831 if (memcmp(uri.ptr, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
3832 return 0;
3833
3834 return 1;
3835}
3836
3837/* This function prepares an applet to handle the stats. It can deal with the
3838 * "100-continue" expectation, check that admin rules are met for POST requests,
3839 * and program a response message if something was unexpected. It cannot fail
3840 * and always relies on the stats applet to complete the job. It does not touch
3841 * analysers nor counters, which are left to the caller. It does not touch
3842 * s->target which is supposed to already point to the stats applet. The caller
3843 * is expected to have already assigned an appctx to the stream.
3844 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003845static int http_handle_stats(struct stream *s, struct channel *req)
Christopher Faulet377c5a52018-10-24 21:21:30 +02003846{
3847 struct stats_admin_rule *stats_admin_rule;
3848 struct stream_interface *si = &s->si[1];
3849 struct session *sess = s->sess;
3850 struct http_txn *txn = s->txn;
3851 struct http_msg *msg = &txn->req;
3852 struct uri_auth *uri_auth = s->be->uri_auth;
3853 const char *h, *lookup, *end;
3854 struct appctx *appctx;
3855 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003856 struct htx_sl *sl;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003857
3858 appctx = si_appctx(si);
3859 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
3860 appctx->st1 = appctx->st2 = 0;
3861 appctx->ctx.stats.st_code = STAT_STATUS_INIT;
Willy Tarreau676c29e2019-10-09 10:50:01 +02003862 appctx->ctx.stats.flags |= uri_auth->flags;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003863 appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */
3864 if ((msg->flags & HTTP_MSGF_VER_11) && (txn->meth != HTTP_METH_HEAD))
3865 appctx->ctx.stats.flags |= STAT_CHUNKED;
3866
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003867 htx = htxbuf(&req->buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02003868 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003869 lookup = HTX_SL_REQ_UPTR(sl) + uri_auth->uri_len;
3870 end = HTX_SL_REQ_UPTR(sl) + HTX_SL_REQ_ULEN(sl);
Christopher Faulet377c5a52018-10-24 21:21:30 +02003871
3872 for (h = lookup; h <= end - 3; h++) {
3873 if (memcmp(h, ";up", 3) == 0) {
3874 appctx->ctx.stats.flags |= STAT_HIDE_DOWN;
3875 break;
3876 }
Willy Tarreau3e320362020-10-23 17:28:57 +02003877 if (memcmp(h, ";no-maint", 3) == 0) {
3878 appctx->ctx.stats.flags |= STAT_HIDE_MAINT;
3879 break;
3880 }
Christopher Faulet377c5a52018-10-24 21:21:30 +02003881 }
3882
3883 if (uri_auth->refresh) {
3884 for (h = lookup; h <= end - 10; h++) {
3885 if (memcmp(h, ";norefresh", 10) == 0) {
3886 appctx->ctx.stats.flags |= STAT_NO_REFRESH;
3887 break;
3888 }
3889 }
3890 }
3891
3892 for (h = lookup; h <= end - 4; h++) {
3893 if (memcmp(h, ";csv", 4) == 0) {
Christopher Faulet6338a082019-09-09 15:50:54 +02003894 appctx->ctx.stats.flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
Christopher Faulet377c5a52018-10-24 21:21:30 +02003895 break;
3896 }
3897 }
3898
3899 for (h = lookup; h <= end - 6; h++) {
3900 if (memcmp(h, ";typed", 6) == 0) {
Christopher Faulet6338a082019-09-09 15:50:54 +02003901 appctx->ctx.stats.flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
Christopher Faulet377c5a52018-10-24 21:21:30 +02003902 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
3903 break;
3904 }
3905 }
3906
Christopher Faulet6338a082019-09-09 15:50:54 +02003907 for (h = lookup; h <= end - 5; h++) {
3908 if (memcmp(h, ";json", 5) == 0) {
3909 appctx->ctx.stats.flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
3910 appctx->ctx.stats.flags |= STAT_FMT_JSON;
3911 break;
3912 }
3913 }
3914
3915 for (h = lookup; h <= end - 12; h++) {
3916 if (memcmp(h, ";json-schema", 12) == 0) {
3917 appctx->ctx.stats.flags &= ~STAT_FMT_MASK;
3918 appctx->ctx.stats.flags |= STAT_JSON_SCHM;
3919 break;
3920 }
3921 }
3922
Christopher Faulet377c5a52018-10-24 21:21:30 +02003923 for (h = lookup; h <= end - 8; h++) {
3924 if (memcmp(h, ";st=", 4) == 0) {
3925 int i;
3926 h += 4;
3927 appctx->ctx.stats.st_code = STAT_STATUS_UNKN;
3928 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
3929 if (strncmp(stat_status_codes[i], h, 4) == 0) {
3930 appctx->ctx.stats.st_code = i;
3931 break;
3932 }
3933 }
3934 break;
3935 }
3936 }
3937
3938 appctx->ctx.stats.scope_str = 0;
3939 appctx->ctx.stats.scope_len = 0;
3940 for (h = lookup; h <= end - 8; h++) {
3941 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
3942 int itx = 0;
3943 const char *h2;
3944 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
3945 const char *err;
3946
3947 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
3948 h2 = h;
Christopher Fauleted7a0662019-01-14 11:07:34 +01003949 appctx->ctx.stats.scope_str = h2 - HTX_SL_REQ_UPTR(sl);
3950 while (h < end) {
Christopher Faulet377c5a52018-10-24 21:21:30 +02003951 if (*h == ';' || *h == '&' || *h == ' ')
3952 break;
3953 itx++;
3954 h++;
3955 }
3956
3957 if (itx > STAT_SCOPE_TXT_MAXLEN)
3958 itx = STAT_SCOPE_TXT_MAXLEN;
3959 appctx->ctx.stats.scope_len = itx;
3960
3961 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
3962 memcpy(scope_txt, h2, itx);
3963 scope_txt[itx] = '\0';
3964 err = invalid_char(scope_txt);
3965 if (err) {
3966 /* bad char in search text => clear scope */
3967 appctx->ctx.stats.scope_str = 0;
3968 appctx->ctx.stats.scope_len = 0;
3969 }
3970 break;
3971 }
3972 }
3973
3974 /* now check whether we have some admin rules for this request */
3975 list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) {
3976 int ret = 1;
3977
3978 if (stats_admin_rule->cond) {
3979 ret = acl_exec_cond(stats_admin_rule->cond, s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
3980 ret = acl_pass(ret);
3981 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
3982 ret = !ret;
3983 }
3984
3985 if (ret) {
3986 /* no rule, or the rule matches */
3987 appctx->ctx.stats.flags |= STAT_ADMIN;
3988 break;
3989 }
3990 }
3991
Christopher Faulet5d45e382019-02-27 15:15:23 +01003992 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
3993 appctx->st0 = STAT_HTTP_HEAD;
3994 else if (txn->meth == HTTP_METH_POST) {
Christopher Fauletbd9e8422019-08-15 22:26:48 +02003995 if (appctx->ctx.stats.flags & STAT_ADMIN) {
Christopher Faulet377c5a52018-10-24 21:21:30 +02003996 appctx->st0 = STAT_HTTP_POST;
Christopher Fauletbd9e8422019-08-15 22:26:48 +02003997 if (msg->msg_state < HTTP_MSG_DATA)
3998 req->analysers |= AN_REQ_HTTP_BODY;
3999 }
Christopher Faulet377c5a52018-10-24 21:21:30 +02004000 else {
Christopher Faulet5d45e382019-02-27 15:15:23 +01004001 /* POST without admin level */
Christopher Faulet377c5a52018-10-24 21:21:30 +02004002 appctx->ctx.stats.flags &= ~STAT_CHUNKED;
4003 appctx->ctx.stats.st_code = STAT_STATUS_DENY;
4004 appctx->st0 = STAT_HTTP_LAST;
4005 }
4006 }
4007 else {
Christopher Faulet5d45e382019-02-27 15:15:23 +01004008 /* Unsupported method */
4009 appctx->ctx.stats.flags &= ~STAT_CHUNKED;
4010 appctx->ctx.stats.st_code = STAT_STATUS_IVAL;
4011 appctx->st0 = STAT_HTTP_LAST;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004012 }
4013
4014 s->task->nice = -32; /* small boost for HTTP statistics */
4015 return 1;
4016}
4017
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004018void http_perform_server_redirect(struct stream *s, struct stream_interface *si)
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004019{
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004020 struct channel *req = &s->req;
4021 struct channel *res = &s->res;
4022 struct server *srv;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004023 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004024 struct htx_sl *sl;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004025 struct ist path, location;
4026 unsigned int flags;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004027
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004028 /*
4029 * Create the location
4030 */
4031 chunk_reset(&trash);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004032
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004033 /* 1: add the server's prefix */
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004034 /* special prefix "/" means don't change URL */
4035 srv = __objt_server(s->target);
4036 if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') {
4037 if (!chunk_memcat(&trash, srv->rdr_pfx, srv->rdr_len))
4038 return;
4039 }
4040
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004041 /* 2: add the request Path */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004042 htx = htxbuf(&req->buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02004043 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004044 path = http_get_path(htx_sl_req_uri(sl));
Tim Duesterhused526372020-03-05 17:56:33 +01004045 if (!isttest(path))
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004046 return;
4047
4048 if (!chunk_memcat(&trash, path.ptr, path.len))
4049 return;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004050 location = ist2(trash.area, trash.data);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004051
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004052 /*
4053 * Create the 302 respone
4054 */
4055 htx = htx_from_buf(&res->buf);
4056 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
4057 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
4058 ist("HTTP/1.1"), ist("302"), ist("Found"));
4059 if (!sl)
4060 goto fail;
4061 sl->info.res.status = 302;
4062 s->txn->status = 302;
4063
4064 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) ||
4065 !htx_add_header(htx, ist("Connection"), ist("close")) ||
4066 !htx_add_header(htx, ist("Content-length"), ist("0")) ||
4067 !htx_add_header(htx, ist("Location"), location))
4068 goto fail;
4069
4070 if (!htx_add_endof(htx, HTX_BLK_EOH) || !htx_add_endof(htx, HTX_BLK_EOM))
4071 goto fail;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004072
Christopher Fauletc20afb82020-01-24 19:16:26 +01004073 htx_to_buf(htx, &res->buf);
Christopher Fauleta72a7e42020-01-28 09:28:11 +01004074 if (!http_forward_proxy_resp(s, 1))
4075 goto fail;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004076
4077 /* return without error. */
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004078 si_shutr(si);
4079 si_shutw(si);
4080 si->err_type = SI_ET_NONE;
4081 si->state = SI_ST_CLO;
4082
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004083 if (!(s->flags & SF_ERR_MASK))
4084 s->flags |= SF_ERR_LOCAL;
4085 if (!(s->flags & SF_FINST_MASK))
4086 s->flags |= SF_FINST_C;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004087
4088 /* FIXME: we should increase a counter of redirects per server and per backend. */
4089 srv_inc_sess_ctr(srv);
4090 srv_set_sess_last(srv);
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004091 return;
4092
4093 fail:
4094 /* If an error occurred, remove the incomplete HTTP response from the
4095 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004096 channel_htx_truncate(res, htx);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004097}
4098
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05004099/* This function terminates the request because it was completely analyzed or
Christopher Fauletf2824e62018-10-01 12:12:37 +02004100 * because an error was triggered during the body forwarding.
4101 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004102static void http_end_request(struct stream *s)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004103{
4104 struct channel *chn = &s->req;
4105 struct http_txn *txn = s->txn;
4106
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004107 DBG_TRACE_ENTER(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004108
Christopher Fauletb42a8b62018-11-19 21:59:00 +01004109 if (unlikely(txn->req.msg_state == HTTP_MSG_ERROR ||
4110 txn->rsp.msg_state == HTTP_MSG_ERROR)) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004111 channel_abort(chn);
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004112 channel_htx_truncate(chn, htxbuf(&chn->buf));
Christopher Fauletf2824e62018-10-01 12:12:37 +02004113 goto end;
4114 }
4115
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004116 if (unlikely(txn->req.msg_state < HTTP_MSG_DONE)) {
4117 DBG_TRACE_DEVEL("waiting end of the request", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004118 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004119 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004120
4121 if (txn->req.msg_state == HTTP_MSG_DONE) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004122 /* No need to read anymore, the request was completely parsed.
4123 * We can shut the read side unless we want to abort_on_close,
4124 * or we have a POST request. The issue with POST requests is
4125 * that some browsers still send a CRLF after the request, and
4126 * this CRLF must be read so that it does not remain in the kernel
4127 * buffers, otherwise a close could cause an RST on some systems
4128 * (eg: Linux).
4129 */
Christopher Faulet769d0e92019-03-22 14:23:18 +01004130 if (!(s->be->options & PR_O_ABRT_CLOSE) && txn->meth != HTTP_METH_POST)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004131 channel_dont_read(chn);
4132
4133 /* if the server closes the connection, we want to immediately react
4134 * and close the socket to save packets and syscalls.
4135 */
4136 s->si[1].flags |= SI_FL_NOHALF;
4137
4138 /* In any case we've finished parsing the request so we must
4139 * disable Nagle when sending data because 1) we're not going
4140 * to shut this side, and 2) the server is waiting for us to
4141 * send pending data.
4142 */
4143 chn->flags |= CF_NEVER_WAIT;
4144
Christopher Fauletd01ce402019-01-02 17:44:13 +01004145 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
4146 /* The server has not finished to respond, so we
4147 * don't want to move in order not to upset it.
4148 */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004149 DBG_TRACE_DEVEL("waiting end of the response", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletd01ce402019-01-02 17:44:13 +01004150 return;
4151 }
4152
Christopher Fauletf2824e62018-10-01 12:12:37 +02004153 /* When we get here, it means that both the request and the
4154 * response have finished receiving. Depending on the connection
4155 * mode, we'll have to wait for the last bytes to leave in either
4156 * direction, and sometimes for a close to be effective.
4157 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02004158 if (txn->flags & TX_CON_WANT_TUN) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004159 /* Tunnel mode will not have any analyser so it needs to
4160 * poll for reads.
4161 */
4162 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004163 if (b_data(&chn->buf)) {
4164 DBG_TRACE_DEVEL("waiting to flush the request", STRM_EV_HTTP_ANA, s, txn);
Christopher Faulet9768c262018-10-22 09:34:31 +02004165 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004166 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004167 txn->req.msg_state = HTTP_MSG_TUNNEL;
4168 }
4169 else {
4170 /* we're not expecting any new data to come for this
4171 * transaction, so we can close it.
Christopher Faulet9768c262018-10-22 09:34:31 +02004172 *
4173 * However, there is an exception if the response
4174 * length is undefined. In this case, we need to wait
4175 * the close from the server. The response will be
4176 * switched in TUNNEL mode until the end.
Christopher Fauletf2824e62018-10-01 12:12:37 +02004177 */
4178 if (!(txn->rsp.flags & HTTP_MSGF_XFER_LEN) &&
4179 txn->rsp.msg_state != HTTP_MSG_CLOSED)
Christopher Faulet9768c262018-10-22 09:34:31 +02004180 goto check_channel_flags;
Christopher Fauletf2824e62018-10-01 12:12:37 +02004181
4182 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4183 channel_shutr_now(chn);
4184 channel_shutw_now(chn);
4185 }
4186 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004187 goto check_channel_flags;
4188 }
4189
4190 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
4191 http_msg_closing:
4192 /* nothing else to forward, just waiting for the output buffer
4193 * to be empty and for the shutw_now to take effect.
4194 */
4195 if (channel_is_empty(chn)) {
4196 txn->req.msg_state = HTTP_MSG_CLOSED;
4197 goto http_msg_closed;
4198 }
4199 else if (chn->flags & CF_SHUTW) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004200 txn->req.msg_state = HTTP_MSG_ERROR;
4201 goto end;
4202 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004203 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004204 return;
4205 }
4206
4207 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
4208 http_msg_closed:
Christopher Fauletf2824e62018-10-01 12:12:37 +02004209 /* if we don't know whether the server will close, we need to hard close */
4210 if (txn->rsp.flags & HTTP_MSGF_XFER_LEN)
4211 s->si[1].flags |= SI_FL_NOLINGER; /* we want to close ASAP */
Christopher Fauletf2824e62018-10-01 12:12:37 +02004212 /* see above in MSG_DONE why we only do this in these states */
Christopher Faulet769d0e92019-03-22 14:23:18 +01004213 if (!(s->be->options & PR_O_ABRT_CLOSE))
Christopher Fauletf2824e62018-10-01 12:12:37 +02004214 channel_dont_read(chn);
4215 goto end;
4216 }
4217
4218 check_channel_flags:
4219 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4220 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4221 /* if we've just closed an output, let's switch */
4222 txn->req.msg_state = HTTP_MSG_CLOSING;
4223 goto http_msg_closing;
4224 }
4225
4226 end:
4227 chn->analysers &= AN_REQ_FLT_END;
4228 if (txn->req.msg_state == HTTP_MSG_TUNNEL && HAS_REQ_DATA_FILTERS(s))
4229 chn->analysers |= AN_REQ_FLT_XFER_DATA;
4230 channel_auto_close(chn);
4231 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004232 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004233}
4234
4235
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05004236/* This function terminates the response because it was completely analyzed or
Christopher Fauletf2824e62018-10-01 12:12:37 +02004237 * because an error was triggered during the body forwarding.
4238 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004239static void http_end_response(struct stream *s)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004240{
4241 struct channel *chn = &s->res;
4242 struct http_txn *txn = s->txn;
4243
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004244 DBG_TRACE_ENTER(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004245
Christopher Fauletb42a8b62018-11-19 21:59:00 +01004246 if (unlikely(txn->req.msg_state == HTTP_MSG_ERROR ||
4247 txn->rsp.msg_state == HTTP_MSG_ERROR)) {
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004248 channel_htx_truncate(&s->req, htxbuf(&s->req.buf));
Christopher Faulet9768c262018-10-22 09:34:31 +02004249 channel_abort(&s->req);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004250 goto end;
4251 }
4252
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004253 if (unlikely(txn->rsp.msg_state < HTTP_MSG_DONE)) {
4254 DBG_TRACE_DEVEL("waiting end of the response", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004255 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004256 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004257
4258 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
4259 /* In theory, we don't need to read anymore, but we must
4260 * still monitor the server connection for a possible close
4261 * while the request is being uploaded, so we don't disable
4262 * reading.
4263 */
4264 /* channel_dont_read(chn); */
4265
4266 if (txn->req.msg_state < HTTP_MSG_DONE) {
4267 /* The client seems to still be sending data, probably
4268 * because we got an error response during an upload.
4269 * We have the choice of either breaking the connection
4270 * or letting it pass through. Let's do the later.
4271 */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004272 DBG_TRACE_DEVEL("waiting end of the request", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004273 return;
4274 }
4275
4276 /* When we get here, it means that both the request and the
4277 * response have finished receiving. Depending on the connection
4278 * mode, we'll have to wait for the last bytes to leave in either
4279 * direction, and sometimes for a close to be effective.
4280 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02004281 if (txn->flags & TX_CON_WANT_TUN) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004282 channel_auto_read(chn);
4283 chn->flags |= CF_NEVER_WAIT;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004284 if (b_data(&chn->buf)) {
4285 DBG_TRACE_DEVEL("waiting to flush the respone", STRM_EV_HTTP_ANA, s, txn);
Christopher Faulet9768c262018-10-22 09:34:31 +02004286 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004287 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004288 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
4289 }
4290 else {
4291 /* we're not expecting any new data to come for this
4292 * transaction, so we can close it.
4293 */
4294 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4295 channel_shutr_now(chn);
4296 channel_shutw_now(chn);
4297 }
4298 }
4299 goto check_channel_flags;
4300 }
4301
4302 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
4303 http_msg_closing:
4304 /* nothing else to forward, just waiting for the output buffer
4305 * to be empty and for the shutw_now to take effect.
4306 */
4307 if (channel_is_empty(chn)) {
4308 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4309 goto http_msg_closed;
4310 }
4311 else if (chn->flags & CF_SHUTW) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004312 txn->rsp.msg_state = HTTP_MSG_ERROR;
Christopher Fauletcff0f732019-12-16 16:13:44 +01004313 _HA_ATOMIC_ADD(&strm_sess(s)->fe->fe_counters.cli_aborts, 1);
Olivier Houcharda798bf52019-03-08 18:52:00 +01004314 _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01004315 if (strm_sess(s)->listener->counters)
4316 _HA_ATOMIC_ADD(&strm_sess(s)->listener->counters->cli_aborts, 1);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004317 if (objt_server(s->target))
Christopher Fauletcff0f732019-12-16 16:13:44 +01004318 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.cli_aborts, 1);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004319 goto end;
4320 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004321 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004322 return;
4323 }
4324
4325 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
4326 http_msg_closed:
4327 /* drop any pending data */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004328 channel_htx_truncate(&s->req, htxbuf(&s->req.buf));
Christopher Faulet9768c262018-10-22 09:34:31 +02004329 channel_abort(&s->req);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004330 goto end;
4331 }
4332
4333 check_channel_flags:
4334 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4335 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4336 /* if we've just closed an output, let's switch */
4337 txn->rsp.msg_state = HTTP_MSG_CLOSING;
4338 goto http_msg_closing;
4339 }
4340
4341 end:
4342 chn->analysers &= AN_RES_FLT_END;
4343 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL && HAS_RSP_DATA_FILTERS(s))
4344 chn->analysers |= AN_RES_FLT_XFER_DATA;
4345 channel_auto_close(chn);
4346 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004347 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004348}
4349
Christopher Fauletef70e252020-01-28 09:26:19 +01004350/* Forward a response generated by HAProxy (error/redirect/return). This
4351 * function forwards all pending incoming data. If <final> is set to 0, nothing
4352 * more is performed. It is used for 1xx informational messages. Otherwise, the
Christopher Faulet507479b2020-05-15 12:29:46 +02004353 * transaction is terminated and the request is emptied. On success 1 is
Christopher Faulet40e6b552020-06-25 16:04:50 +02004354 * returned. If an error occurred, 0 is returned. If it fails, this function
4355 * only exits. It is the caller responsibility to do the cleanup.
Christopher Fauletef70e252020-01-28 09:26:19 +01004356 */
4357int http_forward_proxy_resp(struct stream *s, int final)
4358{
4359 struct channel *req = &s->req;
4360 struct channel *res = &s->res;
4361 struct htx *htx = htxbuf(&res->buf);
4362 size_t data;
4363
4364 if (final) {
4365 htx->flags |= HTX_FL_PROXY_RESP;
Christopher Faulet507479b2020-05-15 12:29:46 +02004366
Christopher Fauletaab1b672020-11-18 16:44:02 +01004367 if (!htx_is_empty(htx) && !http_eval_after_res_rules(s))
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01004368 return 0;
Christopher Fauletef70e252020-01-28 09:26:19 +01004369
Christopher Fauletd6c48362020-10-19 18:01:38 +02004370 if (s->txn->meth == HTTP_METH_HEAD)
4371 htx_skip_msg_payload(htx);
4372
Christopher Fauletef70e252020-01-28 09:26:19 +01004373 channel_auto_read(req);
4374 channel_abort(req);
4375 channel_auto_close(req);
4376 channel_htx_erase(req, htxbuf(&req->buf));
4377
4378 res->wex = tick_add_ifset(now_ms, res->wto);
4379 channel_auto_read(res);
4380 channel_auto_close(res);
4381 channel_shutr_now(res);
Christopher Faulet1a9db7c2020-06-25 15:36:45 +02004382 res->flags |= CF_EOI; /* The response is terminated, add EOI */
Christopher Faulet810df062020-07-22 16:20:34 +02004383 htxbuf(&res->buf)->flags |= HTX_FL_EOI; /* no more data are expected */
Christopher Fauletef70e252020-01-28 09:26:19 +01004384 }
Christopher Fauletcf6898c2020-06-25 15:55:11 +02004385 else {
4386 /* Send ASAP informational messages. Rely on CF_EOI for final
4387 * response.
4388 */
4389 res->flags |= CF_SEND_DONTWAIT;
4390 }
Christopher Fauletef70e252020-01-28 09:26:19 +01004391
4392 data = htx->data - co_data(res);
4393 c_adv(res, data);
4394 htx->first = -1;
4395 res->total += data;
4396 return 1;
4397}
4398
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004399void http_server_error(struct stream *s, struct stream_interface *si, int err,
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004400 int finst, struct http_reply *msg)
Christopher Faulet0f226952018-10-22 09:29:56 +02004401{
Christopher Faulet72c7d8d2020-01-27 15:32:25 +01004402 http_reply_and_close(s, s->txn->status, msg);
Christopher Faulet0f226952018-10-22 09:29:56 +02004403 if (!(s->flags & SF_ERR_MASK))
4404 s->flags |= err;
4405 if (!(s->flags & SF_FINST_MASK))
4406 s->flags |= finst;
4407}
4408
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004409void http_reply_and_close(struct stream *s, short status, struct http_reply *msg)
Christopher Faulet0f226952018-10-22 09:29:56 +02004410{
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004411 if (!msg) {
4412 channel_htx_truncate(&s->res, htxbuf(&s->res.buf));
4413 goto end;
4414 }
4415
4416 if (http_reply_message(s, msg) == -1) {
4417 /* On error, return a 500 error message, but don't rewrite it if
Christopher Faulet40e6b552020-06-25 16:04:50 +02004418 * it is already an internal error. If it was already a "const"
4419 * 500 error, just fail.
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004420 */
Christopher Faulet40e6b552020-06-25 16:04:50 +02004421 if (s->txn->status == 500) {
4422 if (s->txn->flags & TX_CONST_REPLY)
4423 goto end;
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004424 s->txn->flags |= TX_CONST_REPLY;
Christopher Faulet40e6b552020-06-25 16:04:50 +02004425 }
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004426 s->txn->status = 500;
4427 s->txn->http_reply = NULL;
4428 return http_reply_and_close(s, s->txn->status, http_error_message(s));
4429 }
4430
4431end:
4432 s->res.wex = tick_add_ifset(now_ms, s->res.wto);
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004433
Christopher Faulet0f226952018-10-22 09:29:56 +02004434 channel_auto_read(&s->req);
4435 channel_abort(&s->req);
4436 channel_auto_close(&s->req);
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004437 channel_htx_erase(&s->req, htxbuf(&s->req.buf));
Christopher Faulet72c7d8d2020-01-27 15:32:25 +01004438 channel_auto_read(&s->res);
4439 channel_auto_close(&s->res);
4440 channel_shutr_now(&s->res);
Christopher Faulet0f226952018-10-22 09:29:56 +02004441}
4442
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004443struct http_reply *http_error_message(struct stream *s)
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004444{
4445 const int msgnum = http_get_status_idx(s->txn->status);
4446
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004447 if (s->txn->http_reply)
4448 return s->txn->http_reply;
4449 else if (s->be->replies[msgnum])
4450 return s->be->replies[msgnum];
4451 else if (strm_fe(s)->replies[msgnum])
4452 return strm_fe(s)->replies[msgnum];
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004453 else
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004454 return &http_err_replies[msgnum];
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004455}
4456
Christopher Faulet40e6b552020-06-25 16:04:50 +02004457/* Produces an HTX message from an http reply. Depending on the http reply type,
4458 * a, errorfile, an raw file or a log-format string is used. On success, it
4459 * returns 0. If an error occurs -1 is returned. If it fails, this function only
4460 * exits. It is the caller responsibility to do the cleanup.
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004461 */
Christopher Fauletae43b6c2020-05-27 15:24:22 +02004462int http_reply_to_htx(struct stream *s, struct htx *htx, struct http_reply *reply)
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004463{
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004464 struct buffer *errmsg;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004465 struct htx_sl *sl;
4466 struct buffer *body = NULL;
4467 const char *status, *reason, *clen, *ctype;
4468 unsigned int slflags;
4469 int ret = 0;
4470
Christopher Faulete29a97e2020-05-14 14:49:25 +02004471 /*
4472 * - HTTP_REPLY_ERRFILES unexpected here. handled as no payload if so
4473 *
4474 * - HTTP_REPLY_INDIRECT: switch on another reply if defined or handled
4475 * as no payload if NULL. the TXN status code is set with the status
4476 * of the original reply.
4477 */
4478
4479 if (reply->type == HTTP_REPLY_INDIRECT) {
4480 if (reply->body.reply)
4481 reply = reply->body.reply;
4482 }
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004483 if (reply->type == HTTP_REPLY_ERRMSG && !reply->body.errmsg) {
4484 /* get default error message */
4485 if (reply == s->txn->http_reply)
4486 s->txn->http_reply = NULL;
4487 reply = http_error_message(s);
4488 if (reply->type == HTTP_REPLY_INDIRECT) {
4489 if (reply->body.reply)
4490 reply = reply->body.reply;
4491 }
4492 }
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004493
4494 if (reply->type == HTTP_REPLY_ERRMSG) {
4495 /* implicit or explicit error message*/
4496 errmsg = reply->body.errmsg;
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004497 if (errmsg && !b_is_null(errmsg)) {
Christopher Faulet20567362020-05-15 14:52:49 +02004498 if (!htx_copy_msg(htx, errmsg))
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004499 goto fail;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004500 }
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004501 }
4502 else {
4503 /* no payload, file or log-format string */
4504 if (reply->type == HTTP_REPLY_RAW) {
4505 /* file */
4506 body = &reply->body.obj;
4507 }
4508 else if (reply->type == HTTP_REPLY_LOGFMT) {
4509 /* log-format string */
4510 body = alloc_trash_chunk();
4511 if (!body)
4512 goto fail_alloc;
4513 body->data = build_logline(s, body->area, body->size, &reply->body.fmt);
4514 }
4515 /* else no payload */
4516
4517 status = ultoa(reply->status);
4518 reason = http_get_reason(reply->status);
4519 slflags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_CLEN);
4520 if (!body || !b_data(body))
4521 slflags |= HTX_SL_F_BODYLESS;
4522 sl = htx_add_stline(htx, HTX_BLK_RES_SL, slflags, ist("HTTP/1.1"), ist(status), ist(reason));
4523 if (!sl)
4524 goto fail;
4525 sl->info.res.status = reply->status;
4526
4527 clen = (body ? ultoa(b_data(body)) : "0");
4528 ctype = reply->ctype;
4529
4530 if (!LIST_ISEMPTY(&reply->hdrs)) {
4531 struct http_reply_hdr *hdr;
4532 struct buffer *value = alloc_trash_chunk();
4533
4534 if (!value)
4535 goto fail;
4536
4537 list_for_each_entry(hdr, &reply->hdrs, list) {
4538 chunk_reset(value);
4539 value->data = build_logline(s, value->area, value->size, &hdr->value);
4540 if (b_data(value) && !htx_add_header(htx, hdr->name, ist2(b_head(value), b_data(value)))) {
4541 free_trash_chunk(value);
4542 goto fail;
4543 }
4544 chunk_reset(value);
4545 }
4546 free_trash_chunk(value);
4547 }
4548
4549 if (!htx_add_header(htx, ist("content-length"), ist(clen)) ||
4550 (body && b_data(body) && ctype && !htx_add_header(htx, ist("content-type"), ist(ctype))) ||
4551 !htx_add_endof(htx, HTX_BLK_EOH) ||
4552 (body && b_data(body) && !htx_add_data_atonce(htx, ist2(b_head(body), b_data(body)))) ||
4553 !htx_add_endof(htx, HTX_BLK_EOM))
4554 goto fail;
4555 }
4556
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004557 leave:
4558 if (reply->type == HTTP_REPLY_LOGFMT)
4559 free_trash_chunk(body);
4560 return ret;
4561
4562 fail_alloc:
4563 if (!(s->flags & SF_ERR_MASK))
4564 s->flags |= SF_ERR_RESOURCE;
Christopher Faulet97e466c2020-05-15 15:12:47 +02004565 /* fall through */
4566 fail:
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004567 ret = -1;
4568 goto leave;
Christopher Faulet97e466c2020-05-15 15:12:47 +02004569}
4570
4571/* Send an http reply to the client. On success, it returns 0. If an error
Christopher Faulet40e6b552020-06-25 16:04:50 +02004572 * occurs -1 is returned and the response channel is truncated, removing this
4573 * way the faulty reply. This function may fail when the reply is formatted
4574 * (http_reply_to_htx) or when the reply is forwarded
4575 * (http_forward_proxy_resp). On the last case, it is because a
4576 * http-after-response rule fails.
Christopher Faulet97e466c2020-05-15 15:12:47 +02004577 */
4578int http_reply_message(struct stream *s, struct http_reply *reply)
4579{
4580 struct channel *res = &s->res;
4581 struct htx *htx = htx_from_buf(&res->buf);
4582
4583 if (s->txn->status == -1)
4584 s->txn->status = reply->status;
4585 channel_htx_truncate(res, htx);
4586
4587 if (http_reply_to_htx(s, htx, reply) == -1)
4588 goto fail;
4589
4590 htx_to_buf(htx, &s->res.buf);
4591 if (!http_forward_proxy_resp(s, 1))
4592 goto fail;
4593 return 0;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004594
4595 fail:
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004596 channel_htx_truncate(res, htx);
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004597 if (!(s->flags & SF_ERR_MASK))
4598 s->flags |= SF_ERR_PRXCOND;
Christopher Faulet97e466c2020-05-15 15:12:47 +02004599 return -1;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004600}
4601
Christopher Faulet304cc402019-07-15 15:46:28 +02004602/* Return the error message corresponding to si->err_type. It is assumed
4603 * that the server side is closed. Note that err_type is actually a
4604 * bitmask, where almost only aborts may be cumulated with other
4605 * values. We consider that aborted operations are more important
4606 * than timeouts or errors due to the fact that nobody else in the
4607 * logs might explain incomplete retries. All others should avoid
4608 * being cumulated. It should normally not be possible to have multiple
4609 * aborts at once, but just in case, the first one in sequence is reported.
4610 * Note that connection errors appearing on the second request of a keep-alive
4611 * connection are not reported since this allows the client to retry.
4612 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004613void http_return_srv_error(struct stream *s, struct stream_interface *si)
Christopher Faulet304cc402019-07-15 15:46:28 +02004614{
4615 int err_type = si->err_type;
4616
4617 /* set s->txn->status for http_error_message(s) */
4618 s->txn->status = 503;
4619
4620 if (err_type & SI_ET_QUEUE_ABRT)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004621 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_Q,
4622 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004623 else if (err_type & SI_ET_CONN_ABRT)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004624 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_C,
4625 (s->txn->flags & TX_NOT_FIRST) ? NULL :
4626 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004627 else if (err_type & SI_ET_QUEUE_TO)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004628 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_Q,
4629 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004630 else if (err_type & SI_ET_QUEUE_ERR)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004631 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_Q,
4632 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004633 else if (err_type & SI_ET_CONN_TO)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004634 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_C,
4635 (s->txn->flags & TX_NOT_FIRST) ? NULL :
4636 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004637 else if (err_type & SI_ET_CONN_ERR)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004638 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_C,
4639 (s->flags & SF_SRV_REUSED) ? NULL :
4640 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004641 else if (err_type & SI_ET_CONN_RES)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004642 http_server_error(s, si, SF_ERR_RESOURCE, SF_FINST_C,
4643 (s->txn->flags & TX_NOT_FIRST) ? NULL :
4644 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004645 else { /* SI_ET_CONN_OTHER and others */
4646 s->txn->status = 500;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004647 http_server_error(s, si, SF_ERR_INTERNAL, SF_FINST_C,
4648 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004649 }
4650}
4651
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004652
Christopher Faulet4a28a532019-03-01 11:19:40 +01004653/* Handle Expect: 100-continue for HTTP/1.1 messages if necessary. It returns 0
4654 * on success and -1 on error.
4655 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004656static int http_handle_expect_hdr(struct stream *s, struct htx *htx, struct http_msg *msg)
Christopher Faulet4a28a532019-03-01 11:19:40 +01004657{
4658 /* If we have HTTP/1.1 message with a body and Expect: 100-continue,
4659 * then we must send an HTTP/1.1 100 Continue intermediate response.
4660 */
4661 if (msg->msg_state == HTTP_MSG_BODY && (msg->flags & HTTP_MSGF_VER_11) &&
4662 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
4663 struct ist hdr = { .ptr = "Expect", .len = 6 };
4664 struct http_hdr_ctx ctx;
4665
4666 ctx.blk = NULL;
4667 /* Expect is allowed in 1.1, look for it */
4668 if (http_find_header(htx, hdr, &ctx, 0) &&
4669 unlikely(isteqi(ctx.value, ist2("100-continue", 12)))) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004670 if (http_reply_100_continue(s) == -1)
Christopher Faulet4a28a532019-03-01 11:19:40 +01004671 return -1;
4672 http_remove_header(htx, &ctx);
4673 }
4674 }
4675 return 0;
4676}
4677
Christopher Faulet23a3c792018-11-28 10:01:23 +01004678/* Send a 100-Continue response to the client. It returns 0 on success and -1
4679 * on error. The response channel is updated accordingly.
4680 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004681static int http_reply_100_continue(struct stream *s)
Christopher Faulet23a3c792018-11-28 10:01:23 +01004682{
4683 struct channel *res = &s->res;
4684 struct htx *htx = htx_from_buf(&res->buf);
4685 struct htx_sl *sl;
4686 unsigned int flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|
4687 HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
Christopher Faulet23a3c792018-11-28 10:01:23 +01004688
4689 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
4690 ist("HTTP/1.1"), ist("100"), ist("Continue"));
4691 if (!sl)
4692 goto fail;
4693 sl->info.res.status = 100;
4694
Christopher Faulet1d5ec092019-06-26 14:23:54 +02004695 if (!htx_add_endof(htx, HTX_BLK_EOH))
Christopher Faulet23a3c792018-11-28 10:01:23 +01004696 goto fail;
4697
Christopher Fauleta72a7e42020-01-28 09:28:11 +01004698 if (!http_forward_proxy_resp(s, 0))
4699 goto fail;
Christopher Faulet23a3c792018-11-28 10:01:23 +01004700 return 0;
4701
4702 fail:
4703 /* If an error occurred, remove the incomplete HTTP response from the
4704 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004705 channel_htx_truncate(res, htx);
Christopher Faulet23a3c792018-11-28 10:01:23 +01004706 return -1;
4707}
4708
Christopher Faulet12c51e22018-11-28 15:59:42 +01004709
Christopher Faulet0f226952018-10-22 09:29:56 +02004710/*
4711 * Capture headers from message <htx> according to header list <cap_hdr>, and
4712 * fill the <cap> pointers appropriately.
4713 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004714static void http_capture_headers(struct htx *htx, char **cap, struct cap_hdr *cap_hdr)
Christopher Faulet0f226952018-10-22 09:29:56 +02004715{
4716 struct cap_hdr *h;
4717 int32_t pos;
4718
Christopher Fauleta3f15502019-05-13 15:27:23 +02004719 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet0f226952018-10-22 09:29:56 +02004720 struct htx_blk *blk = htx_get_blk(htx, pos);
4721 enum htx_blk_type type = htx_get_blk_type(blk);
4722 struct ist n, v;
4723
4724 if (type == HTX_BLK_EOH)
4725 break;
4726 if (type != HTX_BLK_HDR)
4727 continue;
4728
4729 n = htx_get_blk_name(htx, blk);
4730
4731 for (h = cap_hdr; h; h = h->next) {
4732 if (h->namelen && (h->namelen == n.len) &&
4733 (strncasecmp(n.ptr, h->name, h->namelen) == 0)) {
4734 if (cap[h->index] == NULL)
4735 cap[h->index] =
4736 pool_alloc(h->pool);
4737
4738 if (cap[h->index] == NULL) {
4739 ha_alert("HTTP capture : out of memory.\n");
4740 break;
4741 }
4742
4743 v = htx_get_blk_value(htx, blk);
4744 if (v.len > h->len)
4745 v.len = h->len;
4746
4747 memcpy(cap[h->index], v.ptr, v.len);
4748 cap[h->index][v.len]=0;
4749 }
4750 }
4751 }
4752}
4753
Christopher Faulet0b6bdc52018-10-24 11:05:36 +02004754/* Delete a value in a header between delimiters <from> and <next>. The header
4755 * itself is delimited by <start> and <end> pointers. The number of characters
4756 * displaced is returned, and the pointer to the first delimiter is updated if
4757 * required. The function tries as much as possible to respect the following
4758 * principles :
4759 * - replace <from> delimiter by the <next> one unless <from> points to <start>,
4760 * in which case <next> is simply removed
4761 * - set exactly one space character after the new first delimiter, unless there
4762 * are not enough characters in the block being moved to do so.
4763 * - remove unneeded spaces before the previous delimiter and after the new
4764 * one.
4765 *
4766 * It is the caller's responsibility to ensure that :
4767 * - <from> points to a valid delimiter or <start> ;
4768 * - <next> points to a valid delimiter or <end> ;
4769 * - there are non-space chars before <from>.
4770 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004771static int http_del_hdr_value(char *start, char *end, char **from, char *next)
Christopher Faulet0b6bdc52018-10-24 11:05:36 +02004772{
4773 char *prev = *from;
4774
4775 if (prev == start) {
4776 /* We're removing the first value. eat the semicolon, if <next>
4777 * is lower than <end> */
4778 if (next < end)
4779 next++;
4780
4781 while (next < end && HTTP_IS_SPHT(*next))
4782 next++;
4783 }
4784 else {
4785 /* Remove useless spaces before the old delimiter. */
4786 while (HTTP_IS_SPHT(*(prev-1)))
4787 prev--;
4788 *from = prev;
4789
4790 /* copy the delimiter and if possible a space if we're
4791 * not at the end of the line.
4792 */
4793 if (next < end) {
4794 *prev++ = *next++;
4795 if (prev + 1 < next)
4796 *prev++ = ' ';
4797 while (next < end && HTTP_IS_SPHT(*next))
4798 next++;
4799 }
4800 }
4801 memmove(prev, next, end - next);
4802 return (prev - next);
4803}
4804
Christopher Faulet0f226952018-10-22 09:29:56 +02004805
4806/* Formats the start line of the request (without CRLF) and puts it in <str> and
Joseph Herlantc42c0e92018-11-25 10:43:27 -08004807 * return the written length. The line can be truncated if it exceeds <len>.
Christopher Faulet0f226952018-10-22 09:29:56 +02004808 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004809static size_t http_fmt_req_line(const struct htx_sl *sl, char *str, size_t len)
Christopher Faulet0f226952018-10-22 09:29:56 +02004810{
4811 struct ist dst = ist2(str, 0);
4812
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004813 if (istcat(&dst, htx_sl_req_meth(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 if (istcat(&dst, htx_sl_req_uri(sl), len) == -1)
Christopher Faulet0f226952018-10-22 09:29:56 +02004820 goto end;
4821 if (dst.len + 1 > len)
4822 goto end;
4823 dst.ptr[dst.len++] = ' ';
4824
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004825 istcat(&dst, htx_sl_req_vsn(sl), len);
Christopher Faulet0f226952018-10-22 09:29:56 +02004826 end:
4827 return dst.len;
4828}
4829
4830/*
4831 * Print a debug line with a start line.
4832 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004833static void http_debug_stline(const char *dir, struct stream *s, const struct htx_sl *sl)
Christopher Faulet0f226952018-10-22 09:29:56 +02004834{
4835 struct session *sess = strm_sess(s);
4836 int max;
4837
4838 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
4839 dir,
4840 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->handle.fd : -1,
4841 objt_cs(s->si[1].end) ? (unsigned short)objt_cs(s->si[1].end)->conn->handle.fd : -1);
4842
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004843 max = HTX_SL_P1_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02004844 UBOUND(max, trash.size - trash.data - 3);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004845 chunk_memcat(&trash, HTX_SL_P1_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02004846 trash.area[trash.data++] = ' ';
4847
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004848 max = HTX_SL_P2_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02004849 UBOUND(max, trash.size - trash.data - 2);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004850 chunk_memcat(&trash, HTX_SL_P2_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02004851 trash.area[trash.data++] = ' ';
4852
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004853 max = HTX_SL_P3_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02004854 UBOUND(max, trash.size - trash.data - 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004855 chunk_memcat(&trash, HTX_SL_P3_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02004856 trash.area[trash.data++] = '\n';
4857
Willy Tarreau2e8ab6b2020-03-14 11:03:20 +01004858 DISGUISE(write(1, trash.area, trash.data));
Christopher Faulet0f226952018-10-22 09:29:56 +02004859}
4860
4861/*
4862 * Print a debug line with a header.
4863 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004864static 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 +02004865{
4866 struct session *sess = strm_sess(s);
4867 int max;
4868
4869 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
4870 dir,
4871 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->handle.fd : -1,
4872 objt_cs(s->si[1].end) ? (unsigned short)objt_cs(s->si[1].end)->conn->handle.fd : -1);
4873
4874 max = n.len;
4875 UBOUND(max, trash.size - trash.data - 3);
4876 chunk_memcat(&trash, n.ptr, max);
4877 trash.area[trash.data++] = ':';
4878 trash.area[trash.data++] = ' ';
4879
4880 max = v.len;
4881 UBOUND(max, trash.size - trash.data - 1);
4882 chunk_memcat(&trash, v.ptr, max);
4883 trash.area[trash.data++] = '\n';
4884
Willy Tarreau2e8ab6b2020-03-14 11:03:20 +01004885 DISGUISE(write(1, trash.area, trash.data));
Christopher Faulet0f226952018-10-22 09:29:56 +02004886}
4887
Christopher Fauleta8a46e22019-07-16 14:53:09 +02004888/* Allocate a new HTTP transaction for stream <s> unless there is one already.
4889 * In case of allocation failure, everything allocated is freed and NULL is
4890 * returned. Otherwise the new transaction is assigned to the stream and
4891 * returned.
4892 */
4893struct http_txn *http_alloc_txn(struct stream *s)
4894{
4895 struct http_txn *txn = s->txn;
4896
4897 if (txn)
4898 return txn;
4899
4900 txn = pool_alloc(pool_head_http_txn);
4901 if (!txn)
4902 return txn;
4903
4904 s->txn = txn;
4905 return txn;
4906}
4907
4908void http_txn_reset_req(struct http_txn *txn)
4909{
Christopher Faulet1aea50e2020-01-17 16:03:53 +01004910 txn->req.flags = 0;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02004911 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
4912}
4913
4914void http_txn_reset_res(struct http_txn *txn)
4915{
Christopher Faulet1aea50e2020-01-17 16:03:53 +01004916 txn->rsp.flags = 0;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02004917 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
4918}
4919
4920/*
4921 * Initialize a new HTTP transaction for stream <s>. It is assumed that all
4922 * the required fields are properly allocated and that we only need to (re)init
4923 * them. This should be used before processing any new request.
4924 */
4925void http_init_txn(struct stream *s)
4926{
4927 struct http_txn *txn = s->txn;
4928 struct conn_stream *cs = objt_cs(s->si[0].end);
4929
Christopher Fauletda831fa2020-10-06 17:58:43 +02004930 txn->flags = ((cs && cs->flags & CS_FL_NOT_FIRST) ? TX_NOT_FIRST : 0);
Christopher Fauleta8a46e22019-07-16 14:53:09 +02004931 txn->status = -1;
Christopher Faulet5cb513a2020-05-13 17:56:56 +02004932 txn->http_reply = NULL;
Willy Tarreau8b507582020-02-25 09:35:07 +01004933 write_u32(txn->cache_hash, 0);
Christopher Fauleta8a46e22019-07-16 14:53:09 +02004934
4935 txn->cookie_first_date = 0;
4936 txn->cookie_last_date = 0;
4937
4938 txn->srv_cookie = NULL;
4939 txn->cli_cookie = NULL;
4940 txn->uri = NULL;
4941
4942 http_txn_reset_req(txn);
4943 http_txn_reset_res(txn);
4944
4945 txn->req.chn = &s->req;
4946 txn->rsp.chn = &s->res;
4947
4948 txn->auth.method = HTTP_AUTH_UNKNOWN;
4949
4950 vars_init(&s->vars_txn, SCOPE_TXN);
4951 vars_init(&s->vars_reqres, SCOPE_REQ);
4952}
4953
4954/* to be used at the end of a transaction */
4955void http_end_txn(struct stream *s)
4956{
4957 struct http_txn *txn = s->txn;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02004958
4959 /* these ones will have been dynamically allocated */
4960 pool_free(pool_head_requri, txn->uri);
4961 pool_free(pool_head_capture, txn->cli_cookie);
4962 pool_free(pool_head_capture, txn->srv_cookie);
Tim Duesterhusa17e6622020-03-05 20:19:02 +01004963 pool_free(pool_head_uniqueid, s->unique_id.ptr);
Christopher Fauleta8a46e22019-07-16 14:53:09 +02004964
Tim Duesterhusa17e6622020-03-05 20:19:02 +01004965 s->unique_id = IST_NULL;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02004966 txn->uri = NULL;
4967 txn->srv_cookie = NULL;
4968 txn->cli_cookie = NULL;
4969
Christopher Faulet59399252019-11-07 14:27:52 +01004970 if (!LIST_ISEMPTY(&s->vars_txn.head))
4971 vars_prune(&s->vars_txn, s->sess, s);
4972 if (!LIST_ISEMPTY(&s->vars_reqres.head))
4973 vars_prune(&s->vars_reqres, s->sess, s);
4974}
4975
Christopher Fauleta8a46e22019-07-16 14:53:09 +02004976
4977DECLARE_POOL(pool_head_http_txn, "http_txn", sizeof(struct http_txn));
Christopher Faulet0f226952018-10-22 09:29:56 +02004978
Christopher Fauletf4eb75d2018-10-11 15:55:07 +02004979__attribute__((constructor))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004980static void __http_protocol_init(void)
Christopher Fauletf4eb75d2018-10-11 15:55:07 +02004981{
4982}
4983
4984
4985/*
4986 * Local variables:
4987 * c-indent-level: 8
4988 * c-basic-offset: 8
4989 * End:
4990 */