blob: 46858c8674aa519692cf0627bc8de9fb55a30abf [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
Christopher Faulete0768eb2018-10-03 16:38:02 +020013#include <common/base64.h>
14#include <common/config.h>
15#include <common/debug.h>
Willy Tarreaub96b77e2018-12-11 10:22:41 +010016#include <common/htx.h>
Willy Tarreau8b507582020-02-25 09:35:07 +010017#include <common/net_helper.h>
Christopher Faulete0768eb2018-10-03 16:38:02 +020018#include <common/uri_auth.h>
19
Christopher Faulet0f226952018-10-22 09:29:56 +020020#include <types/capture.h>
Christopher Faulete0768eb2018-10-03 16:38:02 +020021
22#include <proto/acl.h>
Christopher Faulet3e964192018-10-24 11:39:23 +020023#include <proto/action.h>
Christopher Faulete0768eb2018-10-03 16:38:02 +020024#include <proto/channel.h>
25#include <proto/checks.h>
26#include <proto/connection.h>
27#include <proto/filters.h>
Christopher Faulet0f226952018-10-22 09:29:56 +020028#include <proto/http_htx.h>
Christopher Faulete0768eb2018-10-03 16:38:02 +020029#include <proto/log.h>
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020030#include <proto/http_ana.h>
Christopher Faulete0768eb2018-10-03 16:38:02 +020031#include <proto/proxy.h>
Christopher Fauletfefc73d2018-10-24 21:18:04 +020032#include <proto/server.h>
Christopher Faulete0768eb2018-10-03 16:38:02 +020033#include <proto/stream.h>
34#include <proto/stream_interface.h>
35#include <proto/stats.h>
Christopher Fauleta8a46e22019-07-16 14:53:09 +020036#include <proto/vars.h>
Christopher Faulete0768eb2018-10-03 16:38:02 +020037
Christopher Fauleteea8fc72019-11-05 16:18:10 +010038#define TRACE_SOURCE &trace_strm
39
Christopher Faulet377c5a52018-10-24 21:21:30 +020040extern const char *stat_status_codes[];
Christopher Fauletf2824e62018-10-01 12:12:37 +020041
Christopher Fauleta8a46e22019-07-16 14:53:09 +020042struct pool_head *pool_head_requri = NULL;
43struct pool_head *pool_head_capture = NULL;
44
45
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020046static void http_end_request(struct stream *s);
47static void http_end_response(struct stream *s);
Christopher Fauletf2824e62018-10-01 12:12:37 +020048
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020049static void http_capture_headers(struct htx *htx, char **cap, struct cap_hdr *cap_hdr);
50static int http_del_hdr_value(char *start, char *end, char **from, char *next);
51static size_t http_fmt_req_line(const struct htx_sl *sl, char *str, size_t len);
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020052static void http_debug_stline(const char *dir, struct stream *s, const struct htx_sl *sl);
53static 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 +020054
Christopher Fauletb58f62b2020-01-13 16:40:13 +010055static enum rule_result http_req_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s);
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020056static enum rule_result http_res_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s);
Christopher Faulet3e964192018-10-24 11:39:23 +020057
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020058static void http_manage_client_side_cookies(struct stream *s, struct channel *req);
59static void http_manage_server_side_cookies(struct stream *s, struct channel *res);
Christopher Fauletfcda7c62018-10-24 11:56:22 +020060
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020061static int http_stats_check_uri(struct stream *s, struct http_txn *txn, struct proxy *backend);
62static int http_handle_stats(struct stream *s, struct channel *req);
Christopher Faulet377c5a52018-10-24 21:21:30 +020063
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020064static int http_handle_expect_hdr(struct stream *s, struct htx *htx, struct http_msg *msg);
65static int http_reply_100_continue(struct stream *s);
66static int http_reply_40x_unauthorized(struct stream *s, const char *auth_realm);
Christopher Faulet23a3c792018-11-28 10:01:23 +010067
Christopher Faulete0768eb2018-10-03 16:38:02 +020068/* This stream analyser waits for a complete HTTP request. It returns 1 if the
69 * processing can continue on next analysers, or zero if it either needs more
70 * data or wants to immediately abort the request (eg: timeout, error, ...). It
71 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req.analysers
72 * when it has nothing left to do, and may remove any analyser when it wants to
73 * abort.
74 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020075int http_wait_for_request(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +020076{
Christopher Faulet9768c262018-10-22 09:34:31 +020077
Christopher Faulete0768eb2018-10-03 16:38:02 +020078 /*
Christopher Faulet9768c262018-10-22 09:34:31 +020079 * We will analyze a complete HTTP request to check the its syntax.
Christopher Faulete0768eb2018-10-03 16:38:02 +020080 *
Christopher Faulet9768c262018-10-22 09:34:31 +020081 * Once the start line and all headers are received, we may perform a
82 * capture of the error (if any), and we will set a few fields. We also
83 * check for monitor-uri, logging and finally headers capture.
Christopher Faulete0768eb2018-10-03 16:38:02 +020084 */
Christopher Faulete0768eb2018-10-03 16:38:02 +020085 struct session *sess = s->sess;
86 struct http_txn *txn = s->txn;
87 struct http_msg *msg = &txn->req;
Christopher Faulet9768c262018-10-22 09:34:31 +020088 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +010089 struct htx_sl *sl;
Christopher Faulete0768eb2018-10-03 16:38:02 +020090
Christopher Fauleteea8fc72019-11-05 16:18:10 +010091 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +020092
Christopher Faulet27ba2dc2018-12-05 11:53:24 +010093 htx = htxbuf(&req->buf);
Christopher Faulet9768c262018-10-22 09:34:31 +020094
Willy Tarreau4236f032019-03-05 10:43:32 +010095 /* Parsing errors are caught here */
Christopher Fauletb9a92f32019-09-09 10:15:21 +020096 if (htx->flags & (HTX_FL_PARSING_ERROR|HTX_FL_PROCESSING_ERROR)) {
Willy Tarreau4236f032019-03-05 10:43:32 +010097 stream_inc_http_req_ctr(s);
98 stream_inc_http_err_ctr(s);
99 proxy_inc_fe_req_ctr(sess->fe);
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200100 if (htx->flags & HTX_FL_PARSING_ERROR)
101 goto return_bad_req;
102 else
103 goto return_int_err;
Willy Tarreau4236f032019-03-05 10:43:32 +0100104 }
105
Christopher Faulete0768eb2018-10-03 16:38:02 +0200106 /* we're speaking HTTP here, so let's speak HTTP to the client */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200107 s->srv_error = http_return_srv_error;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200108
109 /* If there is data available for analysis, log the end of the idle time. */
Christopher Faulet870aad92018-11-29 15:23:46 +0100110 if (c_data(req) && s->logs.t_idle == -1) {
111 const struct cs_info *csinfo = si_get_cs_info(objt_cs(s->si[0].end));
112
113 s->logs.t_idle = ((csinfo)
114 ? csinfo->t_idle
115 : tv_ms_elapsed(&s->logs.tv_accept, &now) - s->logs.t_handshake);
116 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200117
Christopher Faulete0768eb2018-10-03 16:38:02 +0200118 /*
119 * Now we quickly check if we have found a full valid request.
120 * If not so, we check the FD and buffer states before leaving.
121 * A full request is indicated by the fact that we have seen
122 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
123 * requests are checked first. When waiting for a second request
124 * on a keep-alive stream, if we encounter and error, close, t/o,
125 * we note the error in the stream flags but don't set any state.
126 * Since the error will be noted there, it will not be counted by
127 * process_stream() as a frontend error.
128 * Last, we may increase some tracked counters' http request errors on
129 * the cases that are deliberately the client's fault. For instance,
130 * a timeout or connection reset is not counted as an error. However
131 * a bad request is.
132 */
Christopher Faulet29f17582019-05-23 11:03:26 +0200133 if (unlikely(htx_is_empty(htx) || htx->first == -1)) {
Christopher Faulet0ef372a2019-04-08 10:57:20 +0200134 if (htx->flags & HTX_FL_UPGRADE)
135 goto failed_keep_alive;
136
Christopher Faulet9768c262018-10-22 09:34:31 +0200137 /* 1: have we encountered a read error ? */
138 if (req->flags & CF_READ_ERROR) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200139 if (!(s->flags & SF_ERR_MASK))
140 s->flags |= SF_ERR_CLICL;
141
142 if (txn->flags & TX_WAIT_NEXT_RQ)
143 goto failed_keep_alive;
144
145 if (sess->fe->options & PR_O_IGNORE_PRB)
146 goto failed_keep_alive;
147
Christopher Faulet9768c262018-10-22 09:34:31 +0200148 stream_inc_http_err_ctr(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200149 stream_inc_http_req_ctr(s);
150 proxy_inc_fe_req_ctr(sess->fe);
Olivier Houcharda798bf52019-03-08 18:52:00 +0100151 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200152 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100153 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200154
Christopher Faulet9768c262018-10-22 09:34:31 +0200155 txn->status = 400;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200156 http_reply_and_close(s, txn->status, NULL);
Christopher Faulet9768c262018-10-22 09:34:31 +0200157 req->analysers &= AN_REQ_FLT_END;
158
Christopher Faulete0768eb2018-10-03 16:38:02 +0200159 if (!(s->flags & SF_FINST_MASK))
160 s->flags |= SF_FINST_R;
161 return 0;
162 }
163
Christopher Faulet9768c262018-10-22 09:34:31 +0200164 /* 2: has the read timeout expired ? */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200165 else if (req->flags & CF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
166 if (!(s->flags & SF_ERR_MASK))
167 s->flags |= SF_ERR_CLITO;
168
169 if (txn->flags & TX_WAIT_NEXT_RQ)
170 goto failed_keep_alive;
171
172 if (sess->fe->options & PR_O_IGNORE_PRB)
173 goto failed_keep_alive;
174
Christopher Faulet9768c262018-10-22 09:34:31 +0200175 stream_inc_http_err_ctr(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200176 stream_inc_http_req_ctr(s);
177 proxy_inc_fe_req_ctr(sess->fe);
Olivier Houcharda798bf52019-03-08 18:52:00 +0100178 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200179 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100180 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200181
Christopher Faulet9768c262018-10-22 09:34:31 +0200182 txn->status = 408;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200183 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet9768c262018-10-22 09:34:31 +0200184 req->analysers &= AN_REQ_FLT_END;
185
Christopher Faulete0768eb2018-10-03 16:38:02 +0200186 if (!(s->flags & SF_FINST_MASK))
187 s->flags |= SF_FINST_R;
188 return 0;
189 }
190
Christopher Faulet9768c262018-10-22 09:34:31 +0200191 /* 3: have we encountered a close ? */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200192 else if (req->flags & CF_SHUTR) {
193 if (!(s->flags & SF_ERR_MASK))
194 s->flags |= SF_ERR_CLICL;
195
196 if (txn->flags & TX_WAIT_NEXT_RQ)
197 goto failed_keep_alive;
198
199 if (sess->fe->options & PR_O_IGNORE_PRB)
200 goto failed_keep_alive;
201
Christopher Faulete0768eb2018-10-03 16:38:02 +0200202 stream_inc_http_err_ctr(s);
203 stream_inc_http_req_ctr(s);
204 proxy_inc_fe_req_ctr(sess->fe);
Olivier Houcharda798bf52019-03-08 18:52:00 +0100205 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200206 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100207 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200208
Christopher Faulet9768c262018-10-22 09:34:31 +0200209 txn->status = 400;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200210 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet9768c262018-10-22 09:34:31 +0200211 req->analysers &= AN_REQ_FLT_END;
212
Christopher Faulete0768eb2018-10-03 16:38:02 +0200213 if (!(s->flags & SF_FINST_MASK))
214 s->flags |= SF_FINST_R;
215 return 0;
216 }
217
218 channel_dont_connect(req);
219 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
220 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau1a18b542018-12-11 16:37:42 +0100221
Christopher Faulet9768c262018-10-22 09:34:31 +0200222 if (sess->listener->options & LI_O_NOQUICKACK && htx_is_not_empty(htx) &&
Christopher Faulete0768eb2018-10-03 16:38:02 +0200223 objt_conn(sess->origin) && conn_ctrl_ready(__objt_conn(sess->origin))) {
224 /* We need more data, we have to re-enable quick-ack in case we
225 * previously disabled it, otherwise we might cause the client
226 * to delay next data.
227 */
Willy Tarreau1a18b542018-12-11 16:37:42 +0100228 conn_set_quickack(objt_conn(sess->origin), 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200229 }
Willy Tarreau1a18b542018-12-11 16:37:42 +0100230
Christopher Faulet47365272018-10-31 17:40:50 +0100231 if ((req->flags & CF_READ_PARTIAL) && (txn->flags & TX_WAIT_NEXT_RQ)) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200232 /* If the client starts to talk, let's fall back to
233 * request timeout processing.
234 */
235 txn->flags &= ~TX_WAIT_NEXT_RQ;
236 req->analyse_exp = TICK_ETERNITY;
237 }
238
239 /* just set the request timeout once at the beginning of the request */
240 if (!tick_isset(req->analyse_exp)) {
Christopher Faulet47365272018-10-31 17:40:50 +0100241 if ((txn->flags & TX_WAIT_NEXT_RQ) && tick_isset(s->be->timeout.httpka))
Christopher Faulete0768eb2018-10-03 16:38:02 +0200242 req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka);
243 else
244 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
245 }
246
247 /* we're not ready yet */
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100248 DBG_TRACE_DEVEL("waiting for the request",
249 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200250 return 0;
251
252 failed_keep_alive:
253 /* Here we process low-level errors for keep-alive requests. In
254 * short, if the request is not the first one and it experiences
255 * a timeout, read error or shutdown, we just silently close so
256 * that the client can try again.
257 */
258 txn->status = 0;
259 msg->msg_state = HTTP_MSG_RQBEFORE;
260 req->analysers &= AN_REQ_FLT_END;
261 s->logs.logwait = 0;
262 s->logs.level = 0;
263 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200264 http_reply_and_close(s, txn->status, NULL);
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100265 DBG_TRACE_DEVEL("leaving by closing K/A connection",
266 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200267 return 0;
268 }
269
Christopher Faulet9768c262018-10-22 09:34:31 +0200270 msg->msg_state = HTTP_MSG_BODY;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200271 stream_inc_http_req_ctr(s);
272 proxy_inc_fe_req_ctr(sess->fe); /* one more valid request for this FE */
273
Christopher Faulet9768c262018-10-22 09:34:31 +0200274 /* kill the pending keep-alive timeout */
275 txn->flags &= ~TX_WAIT_NEXT_RQ;
276 req->analyse_exp = TICK_ETERNITY;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200277
Christopher Faulet29f17582019-05-23 11:03:26 +0200278 BUG_ON(htx_get_first_type(htx) != HTX_BLK_REQ_SL);
Christopher Faulet297fbb42019-05-13 14:41:27 +0200279 sl = http_get_stline(htx);
Christopher Faulet03599112018-11-27 11:21:21 +0100280
Christopher Faulet9768c262018-10-22 09:34:31 +0200281 /* 0: we might have to print this header in debug mode */
282 if (unlikely((global.mode & MODE_DEBUG) &&
283 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
284 int32_t pos;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200285
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200286 http_debug_stline("clireq", s, sl);
Christopher Faulet9768c262018-10-22 09:34:31 +0200287
Christopher Fauleta3f15502019-05-13 15:27:23 +0200288 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet9768c262018-10-22 09:34:31 +0200289 struct htx_blk *blk = htx_get_blk(htx, pos);
290 enum htx_blk_type type = htx_get_blk_type(blk);
291
292 if (type == HTX_BLK_EOH)
293 break;
294 if (type != HTX_BLK_HDR)
295 continue;
296
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200297 http_debug_hdr("clihdr", s,
298 htx_get_blk_name(htx, blk),
299 htx_get_blk_value(htx, blk));
Christopher Faulet9768c262018-10-22 09:34:31 +0200300 }
301 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200302
303 /*
Christopher Faulet03599112018-11-27 11:21:21 +0100304 * 1: identify the method and the version. Also set HTTP flags
Christopher Faulete0768eb2018-10-03 16:38:02 +0200305 */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100306 txn->meth = sl->info.req.meth;
Christopher Faulet03599112018-11-27 11:21:21 +0100307 if (sl->flags & HTX_SL_F_VER_11)
Christopher Faulet9768c262018-10-22 09:34:31 +0200308 msg->flags |= HTTP_MSGF_VER_11;
Christopher Faulet03599112018-11-27 11:21:21 +0100309 msg->flags |= HTTP_MSGF_XFER_LEN;
Christopher Faulet834eee72019-02-18 11:35:02 +0100310 msg->flags |= ((sl->flags & HTX_SL_F_CLEN) ? HTTP_MSGF_CNT_LEN : HTTP_MSGF_TE_CHNK);
Christopher Fauletb2db4fa2018-11-27 16:51:09 +0100311 if (sl->flags & HTX_SL_F_BODYLESS)
312 msg->flags |= HTTP_MSGF_BODYLESS;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200313
314 /* we can make use of server redirect on GET and HEAD */
315 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
316 s->flags |= SF_REDIRECTABLE;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100317 else if (txn->meth == HTTP_METH_OTHER && isteqi(htx_sl_req_meth(sl), ist("PRI"))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200318 /* PRI is reserved for the HTTP/2 preface */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200319 goto return_bad_req;
320 }
321
322 /*
Christopher Faulet6072beb2020-02-18 15:34:58 +0100323 * 2: check if the URI matches the monitor_uri. We have to do this for
324 * every request which gets in, because the monitor-uri is defined by
325 * the frontend. If the monitor-uri starts with a '/', the matching is
326 * done against the request's path. Otherwise, the request's uri is
327 * used. It is a workaround to let HTTP/2 health-checks work as
328 * expected.
Christopher Faulete0768eb2018-10-03 16:38:02 +0200329 */
330 if (unlikely((sess->fe->monitor_uri_len != 0) &&
Christopher Faulet6072beb2020-02-18 15:34:58 +0100331 ((*sess->fe->monitor_uri == '/' && isteq(http_get_path(htx_sl_req_uri(sl)),
332 ist2(sess->fe->monitor_uri, sess->fe->monitor_uri_len))) ||
333 isteq(htx_sl_req_uri(sl), ist2(sess->fe->monitor_uri, sess->fe->monitor_uri_len))))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200334 /*
335 * We have found the monitor URI
336 */
337 struct acl_cond *cond;
338
339 s->flags |= SF_MONITOR;
Olivier Houcharda798bf52019-03-08 18:52:00 +0100340 _HA_ATOMIC_ADD(&sess->fe->fe_counters.intercepted_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200341
342 /* Check if we want to fail this monitor request or not */
343 list_for_each_entry(cond, &sess->fe->mon_fail_cond, list) {
344 int ret = acl_exec_cond(cond, sess->fe, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
345
346 ret = acl_pass(ret);
347 if (cond->pol == ACL_COND_UNLESS)
348 ret = !ret;
349
350 if (ret) {
351 /* we fail this request, let's return 503 service unavail */
352 txn->status = 503;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200353 if (!(s->flags & SF_ERR_MASK))
354 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
355 goto return_prx_cond;
356 }
357 }
358
Joseph Herlantc42c0e92018-11-25 10:43:27 -0800359 /* nothing to fail, let's reply normally */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200360 txn->status = 200;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200361 if (!(s->flags & SF_ERR_MASK))
362 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
363 goto return_prx_cond;
364 }
365
366 /*
367 * 3: Maybe we have to copy the original REQURI for the logs ?
368 * Note: we cannot log anymore if the request has been
369 * classified as invalid.
370 */
371 if (unlikely(s->logs.logwait & LW_REQ)) {
372 /* we have a complete HTTP request that we must log */
373 if ((txn->uri = pool_alloc(pool_head_requri)) != NULL) {
Christopher Faulet9768c262018-10-22 09:34:31 +0200374 size_t len;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200375
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200376 len = http_fmt_req_line(sl, txn->uri, global.tune.requri_len - 1);
Christopher Faulet9768c262018-10-22 09:34:31 +0200377 txn->uri[len] = 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200378
379 if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT)))
380 s->do_log(s);
381 } else {
382 ha_alert("HTTP logging : out of memory.\n");
383 }
384 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200385
Christopher Faulete0768eb2018-10-03 16:38:02 +0200386 /* if the frontend has "option http-use-proxy-header", we'll check if
387 * we have what looks like a proxied connection instead of a connection,
388 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
389 * Note that this is *not* RFC-compliant, however browsers and proxies
390 * happen to do that despite being non-standard :-(
391 * We consider that a request not beginning with either '/' or '*' is
392 * a proxied connection, which covers both "scheme://location" and
393 * CONNECT ip:port.
394 */
395 if ((sess->fe->options2 & PR_O2_USE_PXHDR) &&
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100396 *HTX_SL_REQ_UPTR(sl) != '/' && *HTX_SL_REQ_UPTR(sl) != '*')
Christopher Faulete0768eb2018-10-03 16:38:02 +0200397 txn->flags |= TX_USE_PX_CONN;
398
Christopher Faulete0768eb2018-10-03 16:38:02 +0200399 /* 5: we may need to capture headers */
400 if (unlikely((s->logs.logwait & LW_REQHDR) && s->req_cap))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200401 http_capture_headers(htx, s->req_cap, sess->fe->req_cap);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200402
Christopher Faulete0768eb2018-10-03 16:38:02 +0200403 /* we may have to wait for the request's body */
Christopher Faulet9768c262018-10-22 09:34:31 +0200404 if (s->be->options & PR_O_WREQ_BODY)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200405 req->analysers |= AN_REQ_HTTP_BODY;
406
407 /*
408 * RFC7234#4:
409 * A cache MUST write through requests with methods
410 * that are unsafe (Section 4.2.1 of [RFC7231]) to
411 * the origin server; i.e., a cache is not allowed
412 * to generate a reply to such a request before
413 * having forwarded the request and having received
414 * a corresponding response.
415 *
416 * RFC7231#4.2.1:
417 * Of the request methods defined by this
418 * specification, the GET, HEAD, OPTIONS, and TRACE
419 * methods are defined to be safe.
420 */
421 if (likely(txn->meth == HTTP_METH_GET ||
422 txn->meth == HTTP_METH_HEAD ||
423 txn->meth == HTTP_METH_OPTIONS ||
424 txn->meth == HTTP_METH_TRACE))
425 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
426
427 /* end of job, return OK */
428 req->analysers &= ~an_bit;
429 req->analyse_exp = TICK_ETERNITY;
Christopher Faulet9768c262018-10-22 09:34:31 +0200430
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100431 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200432 return 1;
433
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200434 return_int_err:
435 txn->status = 500;
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200436 if (!(s->flags & SF_ERR_MASK))
437 s->flags |= SF_ERR_INTERNAL;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100438 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200439 if (sess->listener->counters)
Christopher Fauletb8a53712019-12-16 11:29:38 +0100440 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200441 goto return_prx_cond;
442
Christopher Faulete0768eb2018-10-03 16:38:02 +0200443 return_bad_req:
Christopher Faulet9768c262018-10-22 09:34:31 +0200444 txn->status = 400;
Olivier Houcharda798bf52019-03-08 18:52:00 +0100445 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200446 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100447 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200448 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200449
450 return_prx_cond:
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200451 http_reply_and_close(s, txn->status, http_error_message(s));
452
Christopher Faulete0768eb2018-10-03 16:38:02 +0200453 if (!(s->flags & SF_ERR_MASK))
454 s->flags |= SF_ERR_PRXCOND;
455 if (!(s->flags & SF_FINST_MASK))
456 s->flags |= SF_FINST_R;
457
458 req->analysers &= AN_REQ_FLT_END;
459 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100460 DBG_TRACE_DEVEL("leaving on error",
461 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200462 return 0;
463}
464
465
466/* This stream analyser runs all HTTP request processing which is common to
467 * frontends and backends, which means blocking ACLs, filters, connection-close,
468 * reqadd, stats and redirects. This is performed for the designated proxy.
469 * It returns 1 if the processing can continue on next analysers, or zero if it
470 * either needs more data or wants to immediately abort the request (eg: deny,
471 * error, ...).
472 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200473int http_process_req_common(struct stream *s, struct channel *req, int an_bit, struct proxy *px)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200474{
475 struct session *sess = s->sess;
476 struct http_txn *txn = s->txn;
477 struct http_msg *msg = &txn->req;
Christopher Fauletff2759f2018-10-24 11:13:16 +0200478 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200479 struct redirect_rule *rule;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200480 enum rule_result verdict;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200481 struct connection *conn = objt_conn(sess->origin);
482
483 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
484 /* we need more data */
485 goto return_prx_yield;
486 }
487
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100488 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200489
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100490 htx = htxbuf(&req->buf);
Christopher Fauletff2759f2018-10-24 11:13:16 +0200491
Christopher Faulet1907ccc2019-04-29 13:12:02 +0200492 /* just in case we have some per-backend tracking. Only called the first
493 * execution of the analyser. */
494 if (!s->current_rule || s->current_rule_list != &px->http_req_rules)
495 stream_inc_be_http_req_ctr(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200496
497 /* evaluate http-request rules */
498 if (!LIST_ISEMPTY(&px->http_req_rules)) {
Christopher Fauletb58f62b2020-01-13 16:40:13 +0100499 verdict = http_req_get_intercept_rule(px, &px->http_req_rules, s);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200500
501 switch (verdict) {
502 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
503 goto return_prx_yield;
504
505 case HTTP_RULE_RES_CONT:
506 case HTTP_RULE_RES_STOP: /* nothing to do */
507 break;
508
509 case HTTP_RULE_RES_DENY: /* deny or tarpit */
510 if (txn->flags & TX_CLTARPIT)
511 goto tarpit;
512 goto deny;
513
514 case HTTP_RULE_RES_ABRT: /* abort request, response already sent. Eg: auth */
515 goto return_prx_cond;
516
517 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
518 goto done;
519
520 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
521 goto return_bad_req;
Christopher Faulet3a26bee2019-12-16 12:47:40 +0100522
523 case HTTP_RULE_RES_ERROR: /* failed with a bad request */
524 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200525 }
526 }
527
528 if (conn && (conn->flags & CO_FL_EARLY_DATA) &&
Olivier Houchard220a26c2020-01-23 14:57:36 +0100529 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_SSL_WAIT_HS))) {
Christopher Fauletff2759f2018-10-24 11:13:16 +0200530 struct http_hdr_ctx ctx;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200531
Christopher Fauletff2759f2018-10-24 11:13:16 +0200532 ctx.blk = NULL;
533 if (!http_find_header(htx, ist("Early-Data"), &ctx, 0)) {
534 if (unlikely(!http_add_header(htx, ist("Early-Data"), ist("1"))))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100535 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200536 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200537 }
538
539 /* OK at this stage, we know that the request was accepted according to
540 * the http-request rules, we can check for the stats. Note that the
541 * URI is detected *before* the req* rules in order not to be affected
542 * by a possible reqrep, while they are processed *after* so that a
543 * reqdeny can still block them. This clearly needs to change in 1.6!
544 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200545 if (!s->target && http_stats_check_uri(s, txn, px)) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200546 s->target = &http_stats_applet.obj_type;
Willy Tarreau14bfe9a2018-12-19 15:19:27 +0100547 if (unlikely(!si_register_handler(&s->si[1], objt_applet(s->target)))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200548 s->logs.tv_request = now;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200549 if (!(s->flags & SF_ERR_MASK))
550 s->flags |= SF_ERR_RESOURCE;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100551 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200552 }
553
554 /* parse the whole stats request and extract the relevant information */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200555 http_handle_stats(s, req);
Christopher Fauletb58f62b2020-01-13 16:40:13 +0100556 verdict = http_req_get_intercept_rule(px, &px->uri_auth->http_req_rules, s);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200557 /* not all actions implemented: deny, allow, auth */
558
559 if (verdict == HTTP_RULE_RES_DENY) /* stats http-request deny */
560 goto deny;
561
562 if (verdict == HTTP_RULE_RES_ABRT) /* stats auth / stats http-request auth */
563 goto return_prx_cond;
Christopher Faulet3a26bee2019-12-16 12:47:40 +0100564
565 if (verdict == HTTP_RULE_RES_BADREQ) /* failed with a bad request */
566 goto return_bad_req;
567
568 if (verdict == HTTP_RULE_RES_ERROR) /* failed with a bad request */
569 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200570 }
571
Christopher Faulet2571bc62019-03-01 11:44:26 +0100572 /* Proceed with the applets now. */
573 if (unlikely(objt_applet(s->target))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200574 if (sess->fe == s->be) /* report it if the request was intercepted by the frontend */
Olivier Houcharda798bf52019-03-08 18:52:00 +0100575 _HA_ATOMIC_ADD(&sess->fe->fe_counters.intercepted_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200576
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200577 if (http_handle_expect_hdr(s, htx, msg) == -1)
Christopher Fauletb8a53712019-12-16 11:29:38 +0100578 goto return_int_err;
Christopher Fauletbcf242a2019-03-01 11:36:26 +0100579
Christopher Faulete0768eb2018-10-03 16:38:02 +0200580 if (!(s->flags & SF_ERR_MASK)) // this is not really an error but it is
581 s->flags |= SF_ERR_LOCAL; // to mark that it comes from the proxy
582 if (!(s->flags & SF_FINST_MASK))
583 s->flags |= SF_FINST_R;
584
585 /* enable the minimally required analyzers to handle keep-alive and compression on the HTTP response */
586 req->analysers &= (AN_REQ_HTTP_BODY | AN_REQ_FLT_HTTP_HDRS | AN_REQ_FLT_END);
587 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
588 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Christopher Fauletbcf242a2019-03-01 11:36:26 +0100589
590 req->flags |= CF_SEND_DONTWAIT;
591 s->flags |= SF_ASSIGNED;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200592 goto done;
593 }
594
595 /* check whether we have some ACLs set to redirect this request */
596 list_for_each_entry(rule, &px->redirect_rules, list) {
597 if (rule->cond) {
598 int ret;
599
600 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
601 ret = acl_pass(ret);
602 if (rule->cond->pol == ACL_COND_UNLESS)
603 ret = !ret;
604 if (!ret)
605 continue;
606 }
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200607 if (!http_apply_redirect_rule(rule, s, txn))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100608 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200609 goto done;
610 }
611
612 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
613 * If this happens, then the data will not come immediately, so we must
614 * send all what we have without waiting. Note that due to the small gain
615 * in waiting for the body of the request, it's easier to simply put the
616 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
617 * itself once used.
618 */
619 req->flags |= CF_SEND_DONTWAIT;
620
621 done: /* done with this analyser, continue with next ones that the calling
622 * points will have set, if any.
623 */
624 req->analyse_exp = TICK_ETERNITY;
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +0500625 done_without_exp: /* done with this analyser, but don't reset the analyse_exp. */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200626 req->analysers &= ~an_bit;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100627 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200628 return 1;
629
630 tarpit:
631 /* Allow cookie logging
632 */
633 if (s->be->cookie_name || sess->fe->capture_name)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200634 http_manage_client_side_cookies(s, req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200635
636 /* When a connection is tarpitted, we use the tarpit timeout,
637 * which may be the same as the connect timeout if unspecified.
638 * If unset, then set it to zero because we really want it to
639 * eventually expire. We build the tarpit as an analyser.
640 */
Christopher Faulet202c6ce2019-01-07 14:57:35 +0100641 channel_htx_erase(&s->req, htx);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200642
643 /* wipe the request out so that we can drop the connection early
644 * if the client closes first.
645 */
646 channel_dont_connect(req);
647
Christopher Faulete0768eb2018-10-03 16:38:02 +0200648 req->analysers &= AN_REQ_FLT_END; /* remove switching rules etc... */
649 req->analysers |= AN_REQ_HTTP_TARPIT;
650 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
651 if (!req->analyse_exp)
652 req->analyse_exp = tick_add(now_ms, 0);
653 stream_inc_http_err_ctr(s);
Olivier Houcharda798bf52019-03-08 18:52:00 +0100654 _HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100655 if (s->flags & SF_BE_ASSIGNED)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100656 _HA_ATOMIC_ADD(&s->be->be_counters.denied_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200657 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100658 _HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200659 goto done_without_exp;
660
661 deny: /* this request was blocked (denied) */
662
663 /* Allow cookie logging
664 */
665 if (s->be->cookie_name || sess->fe->capture_name)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200666 http_manage_client_side_cookies(s, req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200667
Christopher Faulete0768eb2018-10-03 16:38:02 +0200668 s->logs.tv_request = now;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200669 stream_inc_http_err_ctr(s);
Olivier Houcharda798bf52019-03-08 18:52:00 +0100670 _HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100671 if (s->flags & SF_BE_ASSIGNED)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100672 _HA_ATOMIC_ADD(&s->be->be_counters.denied_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200673 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100674 _HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100675 goto return_prx_err;
676
677 return_int_err:
678 txn->status = 500;
679 if (!(s->flags & SF_ERR_MASK))
680 s->flags |= SF_ERR_INTERNAL;
681 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100682 if (s->flags & SF_BE_ASSIGNED)
683 _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100684 if (sess->listener->counters)
685 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
686 goto return_prx_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200687
688 return_bad_req:
Christopher Faulete0768eb2018-10-03 16:38:02 +0200689 txn->status = 400;
Olivier Houcharda798bf52019-03-08 18:52:00 +0100690 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200691 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100692 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100693 /* fall through */
694
695 return_prx_err:
696 http_reply_and_close(s, txn->status, http_error_message(s));
697 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200698
699 return_prx_cond:
700 if (!(s->flags & SF_ERR_MASK))
701 s->flags |= SF_ERR_PRXCOND;
702 if (!(s->flags & SF_FINST_MASK))
703 s->flags |= SF_FINST_R;
704
705 req->analysers &= AN_REQ_FLT_END;
706 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100707 DBG_TRACE_DEVEL("leaving on error",
708 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200709 return 0;
710
711 return_prx_yield:
712 channel_dont_connect(req);
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100713 DBG_TRACE_DEVEL("waiting for more data",
714 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200715 return 0;
716}
717
718/* This function performs all the processing enabled for the current request.
719 * It returns 1 if the processing can continue on next analysers, or zero if it
720 * needs more data, encounters an error, or wants to immediately abort the
721 * request. It relies on buffers flags, and updates s->req.analysers.
722 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200723int http_process_request(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200724{
725 struct session *sess = s->sess;
726 struct http_txn *txn = s->txn;
727 struct http_msg *msg = &txn->req;
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200728 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200729 struct connection *cli_conn = objt_conn(strm_sess(s)->origin);
730
731 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
732 /* we need more data */
733 channel_dont_connect(req);
734 return 0;
735 }
736
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100737 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200738
739 /*
740 * Right now, we know that we have processed the entire headers
741 * and that unwanted requests have been filtered out. We can do
742 * whatever we want with the remaining request. Also, now we
743 * may have separate values for ->fe, ->be.
744 */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100745 htx = htxbuf(&req->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200746
747 /*
748 * If HTTP PROXY is set we simply get remote server address parsing
Willy Tarreau1c8d32b2019-07-18 15:47:45 +0200749 * incoming request.
Christopher Faulete0768eb2018-10-03 16:38:02 +0200750 */
751 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SF_ADDR_SET)) {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100752 struct htx_sl *sl;
753 struct ist uri, path;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200754
Willy Tarreau1c8d32b2019-07-18 15:47:45 +0200755 if (!sockaddr_alloc(&s->target_addr)) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200756 if (!(s->flags & SF_ERR_MASK))
757 s->flags |= SF_ERR_RESOURCE;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100758 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200759 }
Christopher Faulet297fbb42019-05-13 14:41:27 +0200760 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100761 uri = htx_sl_req_uri(sl);
762 path = http_get_path(uri);
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200763
Willy Tarreau1c8d32b2019-07-18 15:47:45 +0200764 if (url2sa(uri.ptr, uri.len - path.len, s->target_addr, NULL) == -1)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200765 goto return_bad_req;
766
Willy Tarreau1c8d32b2019-07-18 15:47:45 +0200767 s->target = &s->be->obj_type;
768 s->flags |= SF_ADDR_SET | SF_ASSIGNED;
769
Christopher Faulete0768eb2018-10-03 16:38:02 +0200770 /* if the path was found, we have to remove everything between
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200771 * uri.ptr and path.ptr (excluded). If it was not found, we need
772 * to replace from all the uri by a single "/".
773 *
774 * Instead of rewritting the whole start line, we just update
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100775 * the star-line URI. Some space will be lost but it should be
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200776 * insignificant.
Christopher Faulete0768eb2018-10-03 16:38:02 +0200777 */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100778 istcpy(&uri, (path.len ? path : ist("/")), uri.len);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200779 }
780
781 /*
782 * 7: Now we can work with the cookies.
783 * Note that doing so might move headers in the request, but
784 * the fields will stay coherent and the URI will not move.
785 * This should only be performed in the backend.
786 */
787 if (s->be->cookie_name || sess->fe->capture_name)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200788 http_manage_client_side_cookies(s, req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200789
Tim Duesterhus2825b4b2020-02-28 15:13:34 +0100790 /* 8: Generate unique ID if a "unique-id-format" is defined.
791 *
792 * A unique ID is generated even when it is not sent to ensure that the ID can make use of
793 * fetches only available in the HTTP request processing stage.
794 */
795 if (!LIST_ISEMPTY(&sess->fe->format_unique_id)) {
Tim Duesterhusa17e6622020-03-05 20:19:02 +0100796 struct ist unique_id = stream_generate_unique_id(s, &sess->fe->format_unique_id);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200797
Tim Duesterhusa17e6622020-03-05 20:19:02 +0100798 if (!isttest(unique_id)) {
Christopher Fauletb8a53712019-12-16 11:29:38 +0100799 if (!(s->flags & SF_ERR_MASK))
800 s->flags |= SF_ERR_RESOURCE;
801 goto return_int_err;
802 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200803
Tim Duesterhus2825b4b2020-02-28 15:13:34 +0100804 /* send unique ID if a "unique-id-header" is defined */
Tim Duesterhus0643b0e2020-03-05 17:56:35 +0100805 if (isttest(sess->fe->header_unique_id) &&
Tim Duesterhusa17e6622020-03-05 20:19:02 +0100806 unlikely(!http_add_header(htx, sess->fe->header_unique_id, s->unique_id)))
Tim Duesterhus2825b4b2020-02-28 15:13:34 +0100807 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200808 }
809
810 /*
811 * 9: add X-Forwarded-For if either the frontend or the backend
812 * asks for it.
813 */
814 if ((sess->fe->options | s->be->options) & PR_O_FWDFOR) {
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200815 struct http_hdr_ctx ctx = { .blk = NULL };
816 struct ist hdr = ist2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : sess->fe->fwdfor_hdr_name,
817 s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : sess->fe->fwdfor_hdr_len);
818
Christopher Faulete0768eb2018-10-03 16:38:02 +0200819 if (!((sess->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200820 http_find_header(htx, hdr, &ctx, 0)) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200821 /* The header is set to be added only if none is present
822 * and we found it, so don't do anything.
823 */
824 }
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200825 else if (cli_conn && conn_get_src(cli_conn) && cli_conn->src->ss_family == AF_INET) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200826 /* Add an X-Forwarded-For header unless the source IP is
827 * in the 'except' network range.
828 */
829 if ((!sess->fe->except_mask.s_addr ||
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200830 (((struct sockaddr_in *)cli_conn->src)->sin_addr.s_addr & sess->fe->except_mask.s_addr)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200831 != sess->fe->except_net.s_addr) &&
832 (!s->be->except_mask.s_addr ||
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200833 (((struct sockaddr_in *)cli_conn->src)->sin_addr.s_addr & s->be->except_mask.s_addr)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200834 != s->be->except_net.s_addr)) {
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200835 unsigned char *pn = (unsigned char *)&((struct sockaddr_in *)cli_conn->src)->sin_addr;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200836
837 /* Note: we rely on the backend to get the header name to be used for
838 * x-forwarded-for, because the header is really meant for the backends.
839 * However, if the backend did not specify any option, we have to rely
840 * on the frontend's header name.
841 */
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200842 chunk_printf(&trash, "%d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
843 if (unlikely(!http_add_header(htx, hdr, ist2(trash.area, trash.data))))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100844 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200845 }
846 }
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200847 else if (cli_conn && conn_get_src(cli_conn) && cli_conn->src->ss_family == AF_INET6) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200848 /* FIXME: for the sake of completeness, we should also support
849 * 'except' here, although it is mostly useless in this case.
850 */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200851 char pn[INET6_ADDRSTRLEN];
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200852
Christopher Faulete0768eb2018-10-03 16:38:02 +0200853 inet_ntop(AF_INET6,
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200854 (const void *)&((struct sockaddr_in6 *)(cli_conn->src))->sin6_addr,
Christopher Faulete0768eb2018-10-03 16:38:02 +0200855 pn, sizeof(pn));
856
857 /* Note: we rely on the backend to get the header name to be used for
858 * x-forwarded-for, because the header is really meant for the backends.
859 * However, if the backend did not specify any option, we have to rely
860 * on the frontend's header name.
861 */
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200862 chunk_printf(&trash, "%s", pn);
863 if (unlikely(!http_add_header(htx, hdr, ist2(trash.area, trash.data))))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100864 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200865 }
866 }
867
868 /*
869 * 10: add X-Original-To if either the frontend or the backend
870 * asks for it.
871 */
872 if ((sess->fe->options | s->be->options) & PR_O_ORGTO) {
873
874 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200875 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 +0200876 /* Add an X-Original-To header unless the destination IP is
877 * in the 'except' network range.
878 */
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200879 if (cli_conn->dst->ss_family == AF_INET &&
Christopher Faulete0768eb2018-10-03 16:38:02 +0200880 ((!sess->fe->except_mask_to.s_addr ||
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200881 (((struct sockaddr_in *)cli_conn->dst)->sin_addr.s_addr & sess->fe->except_mask_to.s_addr)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200882 != sess->fe->except_to.s_addr) &&
883 (!s->be->except_mask_to.s_addr ||
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200884 (((struct sockaddr_in *)cli_conn->dst)->sin_addr.s_addr & s->be->except_mask_to.s_addr)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200885 != s->be->except_to.s_addr))) {
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200886 struct ist hdr;
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200887 unsigned char *pn = (unsigned char *)&((struct sockaddr_in *)cli_conn->dst)->sin_addr;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200888
889 /* Note: we rely on the backend to get the header name to be used for
890 * x-original-to, because the header is really meant for the backends.
891 * However, if the backend did not specify any option, we have to rely
892 * on the frontend's header name.
893 */
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200894 if (s->be->orgto_hdr_len)
895 hdr = ist2(s->be->orgto_hdr_name, s->be->orgto_hdr_len);
896 else
897 hdr = ist2(sess->fe->orgto_hdr_name, sess->fe->orgto_hdr_len);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200898
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200899 chunk_printf(&trash, "%d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
900 if (unlikely(!http_add_header(htx, hdr, ist2(trash.area, trash.data))))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100901 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200902 }
903 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200904 }
905
Christopher Faulete0768eb2018-10-03 16:38:02 +0200906 /* If we have no server assigned yet and we're balancing on url_param
907 * with a POST request, we may be interested in checking the body for
908 * that parameter. This will be done in another analyser.
909 */
910 if (!(s->flags & (SF_ASSIGNED|SF_DIRECT)) &&
Willy Tarreau089eaa02019-01-14 15:17:46 +0100911 s->txn->meth == HTTP_METH_POST &&
912 (s->be->lbprm.algo & BE_LB_ALGO) == BE_LB_ALGO_PH) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200913 channel_dont_connect(req);
914 req->analysers |= AN_REQ_HTTP_BODY;
915 }
916
917 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
918 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau1a18b542018-12-11 16:37:42 +0100919
Christopher Faulete0768eb2018-10-03 16:38:02 +0200920 /* We expect some data from the client. Unless we know for sure
921 * we already have a full request, we have to re-enable quick-ack
922 * in case we previously disabled it, otherwise we might cause
923 * the client to delay further data.
924 */
925 if ((sess->listener->options & LI_O_NOQUICKACK) &&
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200926 (htx_get_tail_type(htx) != HTX_BLK_EOM))
Willy Tarreau1a18b542018-12-11 16:37:42 +0100927 conn_set_quickack(cli_conn, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200928
929 /*************************************************************
930 * OK, that's finished for the headers. We have done what we *
931 * could. Let's switch to the DATA state. *
932 ************************************************************/
933 req->analyse_exp = TICK_ETERNITY;
934 req->analysers &= ~an_bit;
935
936 s->logs.tv_request = now;
937 /* OK let's go on with the BODY now */
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100938 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200939 return 1;
940
Christopher Fauletb8a53712019-12-16 11:29:38 +0100941 return_int_err:
942 txn->status = 500;
943 if (!(s->flags & SF_ERR_MASK))
944 s->flags |= SF_ERR_INTERNAL;
945 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100946 if (s->flags & SF_BE_ASSIGNED)
Christopher Fauletbe20cf32020-01-24 11:41:38 +0100947 _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100948 if (sess->listener->counters)
949 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
950 goto return_prx_cond;
951
Christopher Faulete0768eb2018-10-03 16:38:02 +0200952 return_bad_req: /* let's centralize all bad requests */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200953 txn->status = 400;
Olivier Houcharda798bf52019-03-08 18:52:00 +0100954 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200955 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100956 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100957 /* fall through */
958
959 return_prx_cond:
960 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +0200961
962 if (!(s->flags & SF_ERR_MASK))
963 s->flags |= SF_ERR_PRXCOND;
964 if (!(s->flags & SF_FINST_MASK))
965 s->flags |= SF_FINST_R;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100966
967 req->analysers &= AN_REQ_FLT_END;
968 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100969 DBG_TRACE_DEVEL("leaving on error",
970 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200971 return 0;
972}
973
974/* This function is an analyser which processes the HTTP tarpit. It always
975 * returns zero, at the beginning because it prevents any other processing
976 * from occurring, and at the end because it terminates the request.
977 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200978int http_process_tarpit(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200979{
980 struct http_txn *txn = s->txn;
981
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100982 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, &txn->req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200983 /* This connection is being tarpitted. The CLIENT side has
984 * already set the connect expiration date to the right
985 * timeout. We just have to check that the client is still
986 * there and that the timeout has not expired.
987 */
988 channel_dont_connect(req);
989 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100990 !tick_is_expired(req->analyse_exp, now_ms)) {
991 DBG_TRACE_DEVEL("waiting for tarpit timeout expiry",
992 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200993 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100994 }
995
Christopher Faulete0768eb2018-10-03 16:38:02 +0200996
997 /* We will set the queue timer to the time spent, just for
998 * logging purposes. We fake a 500 server error, so that the
999 * attacker will not suspect his connection has been tarpitted.
1000 * It will not cause trouble to the logs because we can exclude
1001 * the tarpitted connections by filtering on the 'PT' status flags.
1002 */
1003 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
1004
Christopher Faulet9d9d6452020-02-21 10:20:46 +01001005 http_reply_and_close(s, txn->status, (!(req->flags & CF_READ_ERROR) ? http_error_message(s) : NULL));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001006
1007 req->analysers &= AN_REQ_FLT_END;
1008 req->analyse_exp = TICK_ETERNITY;
1009
1010 if (!(s->flags & SF_ERR_MASK))
1011 s->flags |= SF_ERR_PRXCOND;
1012 if (!(s->flags & SF_FINST_MASK))
1013 s->flags |= SF_FINST_T;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001014
1015 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001016 return 0;
1017}
1018
1019/* This function is an analyser which waits for the HTTP request body. It waits
1020 * for either the buffer to be full, or the full advertised contents to have
1021 * reached the buffer. It must only be called after the standard HTTP request
1022 * processing has occurred, because it expects the request to be parsed and will
1023 * look for the Expect header. It may send a 100-Continue interim response. It
1024 * takes in input any state starting from HTTP_MSG_BODY and leaves with one of
1025 * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it
1026 * needs to read more data, or 1 once it has completed its analysis.
1027 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001028int http_wait_for_request_body(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001029{
1030 struct session *sess = s->sess;
1031 struct http_txn *txn = s->txn;
1032 struct http_msg *msg = &s->txn->req;
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001033 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001034
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001035 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001036
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001037 htx = htxbuf(&req->buf);
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001038
Willy Tarreau4236f032019-03-05 10:43:32 +01001039 if (htx->flags & HTX_FL_PARSING_ERROR)
1040 goto return_bad_req;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001041 if (htx->flags & HTX_FL_PROCESSING_ERROR)
1042 goto return_int_err;
Willy Tarreau4236f032019-03-05 10:43:32 +01001043
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001044 if (msg->msg_state < HTTP_MSG_BODY)
1045 goto missing_data;
Christopher Faulet9768c262018-10-22 09:34:31 +02001046
Christopher Faulete0768eb2018-10-03 16:38:02 +02001047 /* We have to parse the HTTP request body to find any required data.
1048 * "balance url_param check_post" should have been the only way to get
1049 * into this. We were brought here after HTTP header analysis, so all
1050 * related structures are ready.
1051 */
1052
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001053 if (msg->msg_state < HTTP_MSG_DATA) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001054 if (http_handle_expect_hdr(s, htx, msg) == -1)
Christopher Fauletb8a53712019-12-16 11:29:38 +01001055 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001056 }
1057
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001058 msg->msg_state = HTTP_MSG_DATA;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001059
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001060 /* Now we're in HTTP_MSG_DATA. We just need to know if all data have
1061 * been received or if the buffer is full.
Christopher Faulete0768eb2018-10-03 16:38:02 +02001062 */
Christopher Faulet54b5e212019-06-04 10:08:28 +02001063 if (htx_get_tail_type(htx) > HTX_BLK_DATA ||
Christopher Fauletdcd8c5e2019-01-21 11:24:38 +01001064 channel_htx_full(req, htx, global.tune.maxrewrite))
Christopher Faulete0768eb2018-10-03 16:38:02 +02001065 goto http_end;
1066
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001067 missing_data:
Christopher Faulete0768eb2018-10-03 16:38:02 +02001068 if ((req->flags & CF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
1069 txn->status = 408;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001070 if (!(s->flags & SF_ERR_MASK))
1071 s->flags |= SF_ERR_CLITO;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001072 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
1073 if (sess->listener->counters)
1074 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
1075 goto return_prx_cond;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001076 }
1077
1078 /* we get here if we need to wait for more data */
1079 if (!(req->flags & (CF_SHUTR | CF_READ_ERROR))) {
1080 /* Not enough data. We'll re-use the http-request
1081 * timeout here. Ideally, we should set the timeout
1082 * relative to the accept() date. We just set the
1083 * request timeout once at the beginning of the
1084 * request.
1085 */
1086 channel_dont_connect(req);
1087 if (!tick_isset(req->analyse_exp))
1088 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001089 DBG_TRACE_DEVEL("waiting for more data",
1090 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001091 return 0;
1092 }
1093
1094 http_end:
1095 /* The situation will not evolve, so let's give up on the analysis. */
1096 s->logs.tv_request = now; /* update the request timer to reflect full request */
1097 req->analysers &= ~an_bit;
1098 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001099 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001100 return 1;
1101
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001102 return_int_err:
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001103 txn->status = 500;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001104 if (!(s->flags & SF_ERR_MASK))
1105 s->flags |= SF_ERR_INTERNAL;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001106 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001107 if (s->flags & SF_BE_ASSIGNED)
Christopher Fauletbe20cf32020-01-24 11:41:38 +01001108 _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01001109 if (sess->listener->counters)
1110 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
1111 goto return_prx_cond;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001112
Christopher Faulete0768eb2018-10-03 16:38:02 +02001113 return_bad_req: /* let's centralize all bad requests */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001114 txn->status = 400;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001115 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
1116 if (sess->listener->counters)
1117 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
1118 /* fall through */
1119
1120 return_prx_cond:
1121 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001122
1123 if (!(s->flags & SF_ERR_MASK))
1124 s->flags |= SF_ERR_PRXCOND;
1125 if (!(s->flags & SF_FINST_MASK))
Christopher Fauletb8a53712019-12-16 11:29:38 +01001126 s->flags |= (msg->msg_state < HTTP_MSG_DATA ? SF_FINST_R : SF_FINST_D);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001127
Christopher Faulete0768eb2018-10-03 16:38:02 +02001128 req->analysers &= AN_REQ_FLT_END;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001129 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001130 DBG_TRACE_DEVEL("leaving on error",
1131 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001132 return 0;
1133}
1134
1135/* This function is an analyser which forwards request body (including chunk
1136 * sizes if any). It is called as soon as we must forward, even if we forward
1137 * zero byte. The only situation where it must not be called is when we're in
1138 * tunnel mode and we want to forward till the close. It's used both to forward
1139 * remaining data and to resync after end of body. It expects the msg_state to
1140 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
1141 * read more data, or 1 once we can go on with next request or end the stream.
1142 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
1143 * bytes of pending data + the headers if not already done.
1144 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001145int http_request_forward_body(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001146{
1147 struct session *sess = s->sess;
1148 struct http_txn *txn = s->txn;
Christopher Faulet9768c262018-10-22 09:34:31 +02001149 struct http_msg *msg = &txn->req;
1150 struct htx *htx;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001151 short status = 0;
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001152 int ret;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001153
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001154 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001155
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001156 htx = htxbuf(&req->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001157
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001158 if (htx->flags & HTX_FL_PARSING_ERROR)
1159 goto return_bad_req;
1160 if (htx->flags & HTX_FL_PROCESSING_ERROR)
1161 goto return_int_err;
1162
Christopher Faulete0768eb2018-10-03 16:38:02 +02001163 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
1164 ((req->flags & CF_SHUTW) && (req->to_forward || co_data(req)))) {
1165 /* Output closed while we were sending data. We must abort and
1166 * wake the other side up.
1167 */
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001168
Olivier Houchard29cac3c2019-07-12 15:48:58 +02001169 /* Don't abort yet if we had L7 retries activated and it
1170 * was a write error, we may recover.
1171 */
1172 if (!(req->flags & (CF_READ_ERROR | CF_READ_TIMEOUT)) &&
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001173 (s->si[1].flags & SI_FL_L7_RETRY)) {
1174 DBG_TRACE_DEVEL("leaving on L7 retry",
1175 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Olivier Houchard29cac3c2019-07-12 15:48:58 +02001176 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001177 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001178 msg->msg_state = HTTP_MSG_ERROR;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001179 http_end_request(s);
1180 http_end_response(s);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001181 DBG_TRACE_DEVEL("leaving on error",
1182 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001183 return 1;
1184 }
1185
1186 /* Note that we don't have to send 100-continue back because we don't
1187 * need the data to complete our job, and it's up to the server to
1188 * decide whether to return 100, 417 or anything else in return of
1189 * an "Expect: 100-continue" header.
1190 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001191 if (msg->msg_state == HTTP_MSG_BODY)
1192 msg->msg_state = HTTP_MSG_DATA;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001193
Christopher Faulete0768eb2018-10-03 16:38:02 +02001194 /* in most states, we should abort in case of early close */
1195 channel_auto_close(req);
1196
1197 if (req->to_forward) {
Christopher Faulet66af0b22019-03-22 14:54:52 +01001198 if (req->to_forward == CHN_INFINITE_FORWARD) {
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001199 if (req->flags & CF_EOI)
1200 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet66af0b22019-03-22 14:54:52 +01001201 }
1202 else {
1203 /* We can't process the buffer's contents yet */
1204 req->flags |= CF_WAKE_WRITE;
1205 goto missing_data_or_waiting;
1206 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001207 }
1208
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001209 if (msg->msg_state >= HTTP_MSG_ENDING)
1210 goto ending;
1211
1212 if (txn->meth == HTTP_METH_CONNECT) {
1213 msg->msg_state = HTTP_MSG_ENDING;
1214 goto ending;
1215 }
1216
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001217 /* Forward input data. We get it by removing all outgoing data not
1218 * forwarded yet from HTX data size. If there are some data filters, we
1219 * let them decide the amount of data to forward.
Christopher Faulet9768c262018-10-22 09:34:31 +02001220 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001221 if (HAS_REQ_DATA_FILTERS(s)) {
1222 ret = flt_http_payload(s, msg, htx->data);
1223 if (ret < 0)
1224 goto return_bad_req;
Christopher Faulet421e7692019-06-13 11:16:45 +02001225 c_adv(req, ret);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001226 }
1227 else {
Christopher Faulet421e7692019-06-13 11:16:45 +02001228 c_adv(req, htx->data - co_data(req));
Christopher Faulet66af0b22019-03-22 14:54:52 +01001229 if (msg->flags & HTTP_MSGF_XFER_LEN)
1230 channel_htx_forward_forever(req, htx);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001231 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001232
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001233 if (htx->data != co_data(req))
1234 goto missing_data_or_waiting;
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001235
Christopher Faulet9768c262018-10-22 09:34:31 +02001236 /* Check if the end-of-message is reached and if so, switch the message
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001237 * in HTTP_MSG_ENDING state. Then if all data was marked to be
1238 * forwarded, set the state to HTTP_MSG_DONE.
Christopher Faulet9768c262018-10-22 09:34:31 +02001239 */
1240 if (htx_get_tail_type(htx) != HTX_BLK_EOM)
1241 goto missing_data_or_waiting;
1242
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001243 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet9768c262018-10-22 09:34:31 +02001244
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001245 ending:
1246 /* other states, ENDING...TUNNEL */
1247 if (msg->msg_state >= HTTP_MSG_DONE)
1248 goto done;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001249
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001250 if (HAS_REQ_DATA_FILTERS(s)) {
1251 ret = flt_http_end(s, msg);
1252 if (ret <= 0) {
1253 if (!ret)
1254 goto missing_data_or_waiting;
1255 goto return_bad_req;
1256 }
1257 }
1258
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001259 if (txn->meth == HTTP_METH_CONNECT)
1260 msg->msg_state = HTTP_MSG_TUNNEL;
1261 else {
1262 msg->msg_state = HTTP_MSG_DONE;
1263 req->to_forward = 0;
1264 }
1265
1266 done:
1267 /* we don't want to forward closes on DONE except in tunnel mode. */
1268 if (!(txn->flags & TX_CON_WANT_TUN))
1269 channel_dont_close(req);
1270
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001271 http_end_request(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001272 if (!(req->analysers & an_bit)) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001273 http_end_response(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001274 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
1275 if (req->flags & CF_SHUTW) {
1276 /* request errors are most likely due to the
1277 * server aborting the transfer. */
Christopher Faulet93e02d82019-03-08 14:18:50 +01001278 goto return_srv_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001279 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001280 goto return_bad_req;
1281 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001282 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001283 return 1;
1284 }
1285
1286 /* If "option abortonclose" is set on the backend, we want to monitor
1287 * the client's connection and forward any shutdown notification to the
1288 * server, which will decide whether to close or to go on processing the
1289 * request. We only do that in tunnel mode, and not in other modes since
1290 * it can be abused to exhaust source ports. */
Christopher Faulet769d0e92019-03-22 14:23:18 +01001291 if (s->be->options & PR_O_ABRT_CLOSE) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001292 channel_auto_read(req);
Christopher Fauletc41547b2019-07-16 14:32:23 +02001293 if ((req->flags & (CF_SHUTR|CF_READ_NULL)) && !(txn->flags & TX_CON_WANT_TUN))
Christopher Faulete0768eb2018-10-03 16:38:02 +02001294 s->si[1].flags |= SI_FL_NOLINGER;
1295 channel_auto_close(req);
1296 }
1297 else if (s->txn->meth == HTTP_METH_POST) {
1298 /* POST requests may require to read extra CRLF sent by broken
1299 * browsers and which could cause an RST to be sent upon close
1300 * on some systems (eg: Linux). */
1301 channel_auto_read(req);
1302 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001303 DBG_TRACE_DEVEL("waiting for the end of the HTTP txn",
1304 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001305 return 0;
1306
1307 missing_data_or_waiting:
1308 /* stop waiting for data if the input is closed before the end */
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001309 if (msg->msg_state < HTTP_MSG_ENDING && req->flags & CF_SHUTR)
Christopher Faulet93e02d82019-03-08 14:18:50 +01001310 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001311
1312 waiting:
1313 /* waiting for the last bits to leave the buffer */
1314 if (req->flags & CF_SHUTW)
Christopher Faulet93e02d82019-03-08 14:18:50 +01001315 goto return_srv_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001316
1317 /* When TE: chunked is used, we need to get there again to parse remaining
1318 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
1319 * And when content-length is used, we never want to let the possible
1320 * shutdown be forwarded to the other side, as the state machine will
1321 * take care of it once the client responds. It's also important to
1322 * prevent TIME_WAITs from accumulating on the backend side, and for
1323 * HTTP/2 where the last frame comes with a shutdown.
1324 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001325 if (msg->flags & HTTP_MSGF_XFER_LEN)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001326 channel_dont_close(req);
1327
1328 /* We know that more data are expected, but we couldn't send more that
1329 * what we did. So we always set the CF_EXPECT_MORE flag so that the
1330 * system knows it must not set a PUSH on this first part. Interactive
1331 * modes are already handled by the stream sock layer. We must not do
1332 * this in content-length mode because it could present the MSG_MORE
1333 * flag with the last block of forwarded data, which would cause an
1334 * additional delay to be observed by the receiver.
1335 */
1336 if (msg->flags & HTTP_MSGF_TE_CHNK)
1337 req->flags |= CF_EXPECT_MORE;
1338
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001339 DBG_TRACE_DEVEL("waiting for more data to forward",
1340 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001341 return 0;
1342
Christopher Faulet93e02d82019-03-08 14:18:50 +01001343 return_cli_abort:
1344 _HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
1345 _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001346 if (sess->listener->counters)
1347 _HA_ATOMIC_ADD(&sess->listener->counters->cli_aborts, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001348 if (objt_server(s->target))
Christopher Fauletcff0f732019-12-16 16:13:44 +01001349 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.cli_aborts, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001350 if (!(s->flags & SF_ERR_MASK))
1351 s->flags |= SF_ERR_CLICL;
1352 status = 400;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001353 goto return_prx_cond;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001354
1355 return_srv_abort:
1356 _HA_ATOMIC_ADD(&sess->fe->fe_counters.srv_aborts, 1);
1357 _HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001358 if (sess->listener->counters)
1359 _HA_ATOMIC_ADD(&sess->listener->counters->srv_aborts, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001360 if (objt_server(s->target))
Christopher Fauletcff0f732019-12-16 16:13:44 +01001361 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.srv_aborts, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001362 if (!(s->flags & SF_ERR_MASK))
1363 s->flags |= SF_ERR_SRVCL;
1364 status = 502;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001365 goto return_prx_cond;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001366
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001367 return_int_err:
1368 if (!(s->flags & SF_ERR_MASK))
1369 s->flags |= SF_ERR_INTERNAL;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001370 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001371 _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01001372 if (sess->listener->counters)
1373 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001374 if (objt_server(s->target))
1375 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.internal_errors, 1);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001376 status = 500;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001377 goto return_prx_cond;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001378
Christopher Faulet93e02d82019-03-08 14:18:50 +01001379 return_bad_req:
Olivier Houcharda798bf52019-03-08 18:52:00 +01001380 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001381 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +01001382 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001383 status = 400;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001384 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001385
Christopher Fauletb8a53712019-12-16 11:29:38 +01001386 return_prx_cond:
Christopher Faulet9768c262018-10-22 09:34:31 +02001387 if (txn->status > 0) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001388 /* Note: we don't send any error if some data were already sent */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001389 http_reply_and_close(s, txn->status, NULL);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001390 } else {
Christopher Faulet93e02d82019-03-08 14:18:50 +01001391 txn->status = status;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001392 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001393 }
1394 req->analysers &= AN_REQ_FLT_END;
1395 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 +01001396 if (!(s->flags & SF_ERR_MASK))
1397 s->flags |= SF_ERR_PRXCOND;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001398 if (!(s->flags & SF_FINST_MASK))
1399 s->flags |= ((txn->rsp.msg_state < HTTP_MSG_ERROR) ? SF_FINST_H : SF_FINST_D);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001400 DBG_TRACE_DEVEL("leaving on error ",
1401 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001402 return 0;
1403}
1404
Olivier Houcharda254a372019-04-05 15:30:12 +02001405/* Reset the stream and the backend stream_interface to a situation suitable for attemption connection */
1406/* Returns 0 if we can attempt to retry, -1 otherwise */
1407static __inline int do_l7_retry(struct stream *s, struct stream_interface *si)
1408{
1409 struct channel *req, *res;
1410 int co_data;
1411
1412 si->conn_retries--;
1413 if (si->conn_retries < 0)
1414 return -1;
1415
Willy Tarreau223995e2019-05-04 10:38:31 +02001416 if (objt_server(s->target))
1417 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.retries, 1);
1418 _HA_ATOMIC_ADD(&s->be->be_counters.retries, 1);
1419
Olivier Houcharda254a372019-04-05 15:30:12 +02001420 req = &s->req;
1421 res = &s->res;
1422 /* Remove any write error from the request, and read error from the response */
1423 req->flags &= ~(CF_WRITE_ERROR | CF_WRITE_TIMEOUT | CF_SHUTW | CF_SHUTW_NOW);
1424 res->flags &= ~(CF_READ_ERROR | CF_READ_TIMEOUT | CF_SHUTR | CF_EOI | CF_READ_NULL | CF_SHUTR_NOW);
1425 res->analysers = 0;
1426 si->flags &= ~(SI_FL_ERR | SI_FL_EXP | SI_FL_RXBLK_SHUT);
Olivier Houchard4bd58672019-07-12 16:16:59 +02001427 stream_choose_redispatch(s);
Olivier Houcharda254a372019-04-05 15:30:12 +02001428 si->exp = TICK_ETERNITY;
1429 res->rex = TICK_ETERNITY;
1430 res->to_forward = 0;
1431 res->analyse_exp = TICK_ETERNITY;
1432 res->total = 0;
Olivier Houchard4bd58672019-07-12 16:16:59 +02001433 s->flags &= ~(SF_ERR_SRVTO | SF_ERR_SRVCL);
Olivier Houcharda254a372019-04-05 15:30:12 +02001434 si_release_endpoint(&s->si[1]);
1435 b_free(&req->buf);
1436 /* Swap the L7 buffer with the channel buffer */
1437 /* We know we stored the co_data as b_data, so get it there */
1438 co_data = b_data(&si->l7_buffer);
1439 b_set_data(&si->l7_buffer, b_size(&si->l7_buffer));
1440 b_xfer(&req->buf, &si->l7_buffer, b_data(&si->l7_buffer));
1441
1442 co_set_data(req, co_data);
1443 b_reset(&res->buf);
1444 co_set_data(res, 0);
1445 return 0;
1446}
1447
Christopher Faulete0768eb2018-10-03 16:38:02 +02001448/* This stream analyser waits for a complete HTTP response. It returns 1 if the
1449 * processing can continue on next analysers, or zero if it either needs more
1450 * data or wants to immediately abort the response (eg: timeout, error, ...). It
1451 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->res.analysers
1452 * when it has nothing left to do, and may remove any analyser when it wants to
1453 * abort.
1454 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001455int http_wait_for_response(struct stream *s, struct channel *rep, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001456{
Christopher Faulet9768c262018-10-22 09:34:31 +02001457 /*
1458 * We will analyze a complete HTTP response to check the its syntax.
1459 *
1460 * Once the start line and all headers are received, we may perform a
1461 * capture of the error (if any), and we will set a few fields. We also
1462 * logging and finally headers capture.
1463 */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001464 struct session *sess = s->sess;
1465 struct http_txn *txn = s->txn;
1466 struct http_msg *msg = &txn->rsp;
Christopher Faulet9768c262018-10-22 09:34:31 +02001467 struct htx *htx;
Olivier Houcharda254a372019-04-05 15:30:12 +02001468 struct stream_interface *si_b = &s->si[1];
Christopher Faulet61608322018-11-23 16:23:45 +01001469 struct connection *srv_conn;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001470 struct htx_sl *sl;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001471 int n;
1472
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001473 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001474
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001475 htx = htxbuf(&rep->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001476
Willy Tarreau4236f032019-03-05 10:43:32 +01001477 /* Parsing errors are caught here */
1478 if (htx->flags & HTX_FL_PARSING_ERROR)
1479 goto return_bad_res;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001480 if (htx->flags & HTX_FL_PROCESSING_ERROR)
1481 goto return_int_err;
Willy Tarreau4236f032019-03-05 10:43:32 +01001482
Christopher Faulete0768eb2018-10-03 16:38:02 +02001483 /*
1484 * Now we quickly check if we have found a full valid response.
1485 * If not so, we check the FD and buffer states before leaving.
1486 * A full response is indicated by the fact that we have seen
1487 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
1488 * responses are checked first.
1489 *
1490 * Depending on whether the client is still there or not, we
1491 * may send an error response back or not. Note that normally
1492 * we should only check for HTTP status there, and check I/O
1493 * errors somewhere else.
1494 */
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001495 next_one:
Christopher Faulet29f17582019-05-23 11:03:26 +02001496 if (unlikely(htx_is_empty(htx) || htx->first == -1)) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001497 /* 1: have we encountered a read error ? */
1498 if (rep->flags & CF_READ_ERROR) {
Olivier Houchard865d8392019-05-03 22:46:27 +02001499 struct connection *conn = NULL;
1500
Olivier Houchard865d8392019-05-03 22:46:27 +02001501 if (objt_cs(s->si[1].end))
1502 conn = objt_cs(s->si[1].end)->conn;
1503
1504 if (si_b->flags & SI_FL_L7_RETRY &&
1505 (!conn || conn->err_code != CO_ER_SSL_EARLY_FAILED)) {
Olivier Houcharda254a372019-04-05 15:30:12 +02001506 /* If we arrive here, then CF_READ_ERROR was
1507 * set by si_cs_recv() because we matched a
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05001508 * status, otherwise it would have removed
Olivier Houcharda254a372019-04-05 15:30:12 +02001509 * the SI_FL_L7_RETRY flag, so it's ok not
1510 * to check s->be->retry_type.
1511 */
1512 if (co_data(rep) || do_l7_retry(s, si_b) == 0)
1513 return 0;
1514 }
1515
Olivier Houchard6db16992019-05-17 15:40:49 +02001516 if (txn->flags & TX_NOT_FIRST)
1517 goto abort_keep_alive;
1518
Olivier Houcharda798bf52019-03-08 18:52:00 +01001519 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001520 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001521 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001522 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001523 }
1524
Christopher Faulete0768eb2018-10-03 16:38:02 +02001525 rep->analysers &= AN_RES_FLT_END;
1526 txn->status = 502;
1527
1528 /* Check to see if the server refused the early data.
1529 * If so, just send a 425
1530 */
Olivier Houchard865d8392019-05-03 22:46:27 +02001531 if (conn->err_code == CO_ER_SSL_EARLY_FAILED) {
1532 if ((s->be->retry_type & PR_RE_EARLY_ERROR) &&
Olivier Houchardad26d8d2019-05-10 17:48:28 +02001533 (si_b->flags & SI_FL_L7_RETRY) &&
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001534 do_l7_retry(s, si_b) == 0) {
1535 DBG_TRACE_DEVEL("leaving on L7 retry",
1536 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houchard865d8392019-05-03 22:46:27 +02001537 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001538 }
Olivier Houchard865d8392019-05-03 22:46:27 +02001539 txn->status = 425;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001540 }
1541
1542 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001543 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001544
1545 if (!(s->flags & SF_ERR_MASK))
1546 s->flags |= SF_ERR_SRVCL;
1547 if (!(s->flags & SF_FINST_MASK))
1548 s->flags |= SF_FINST_H;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001549 DBG_TRACE_DEVEL("leaving on error",
1550 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001551 return 0;
1552 }
1553
Christopher Faulet9768c262018-10-22 09:34:31 +02001554 /* 2: read timeout : return a 504 to the client. */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001555 else if (rep->flags & CF_READ_TIMEOUT) {
Olivier Houcharda254a372019-04-05 15:30:12 +02001556 if ((si_b->flags & SI_FL_L7_RETRY) &&
1557 (s->be->retry_type & PR_RE_TIMEOUT)) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001558 if (co_data(rep) || do_l7_retry(s, si_b) == 0) {
1559 DBG_TRACE_DEVEL("leaving on L7 retry",
1560 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharda254a372019-04-05 15:30:12 +02001561 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001562 }
Olivier Houcharda254a372019-04-05 15:30:12 +02001563 }
Olivier Houcharda798bf52019-03-08 18:52:00 +01001564 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001565 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001566 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001567 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001568 }
1569
Christopher Faulete0768eb2018-10-03 16:38:02 +02001570 rep->analysers &= AN_RES_FLT_END;
1571 txn->status = 504;
1572 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001573 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001574
1575 if (!(s->flags & SF_ERR_MASK))
1576 s->flags |= SF_ERR_SRVTO;
1577 if (!(s->flags & SF_FINST_MASK))
1578 s->flags |= SF_FINST_H;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001579 DBG_TRACE_DEVEL("leaving on error",
1580 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001581 return 0;
1582 }
1583
Christopher Faulet9768c262018-10-22 09:34:31 +02001584 /* 3: client abort with an abortonclose */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001585 else if ((rep->flags & CF_SHUTR) && ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001586 _HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
1587 _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001588 if (sess->listener->counters)
1589 _HA_ATOMIC_ADD(&sess->listener->counters->cli_aborts, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001590 if (objt_server(s->target))
Olivier Houcharda798bf52019-03-08 18:52:00 +01001591 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.cli_aborts, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001592
1593 rep->analysers &= AN_RES_FLT_END;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001594 txn->status = 400;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001595 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001596
1597 if (!(s->flags & SF_ERR_MASK))
1598 s->flags |= SF_ERR_CLICL;
1599 if (!(s->flags & SF_FINST_MASK))
1600 s->flags |= SF_FINST_H;
1601
1602 /* process_stream() will take care of the error */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001603 DBG_TRACE_DEVEL("leaving on error",
1604 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001605 return 0;
1606 }
1607
Christopher Faulet9768c262018-10-22 09:34:31 +02001608 /* 4: close from server, capture the response if the server has started to respond */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001609 else if (rep->flags & CF_SHUTR) {
Olivier Houcharda254a372019-04-05 15:30:12 +02001610 if ((si_b->flags & SI_FL_L7_RETRY) &&
1611 (s->be->retry_type & PR_RE_DISCONNECTED)) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001612 if (co_data(rep) || do_l7_retry(s, si_b) == 0) {
1613 DBG_TRACE_DEVEL("leaving on L7 retry",
1614 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharda254a372019-04-05 15:30:12 +02001615 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001616 }
Olivier Houcharda254a372019-04-05 15:30:12 +02001617 }
1618
Olivier Houchard6db16992019-05-17 15:40:49 +02001619 if (txn->flags & TX_NOT_FIRST)
1620 goto abort_keep_alive;
1621
Olivier Houcharda798bf52019-03-08 18:52:00 +01001622 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001623 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001624 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001625 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001626 }
1627
Christopher Faulete0768eb2018-10-03 16:38:02 +02001628 rep->analysers &= AN_RES_FLT_END;
1629 txn->status = 502;
1630 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001631 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001632
1633 if (!(s->flags & SF_ERR_MASK))
1634 s->flags |= SF_ERR_SRVCL;
1635 if (!(s->flags & SF_FINST_MASK))
1636 s->flags |= SF_FINST_H;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001637 DBG_TRACE_DEVEL("leaving on error",
1638 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001639 return 0;
1640 }
1641
Christopher Faulet9768c262018-10-22 09:34:31 +02001642 /* 5: write error to client (we don't send any message then) */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001643 else if (rep->flags & CF_WRITE_ERROR) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001644 if (txn->flags & TX_NOT_FIRST)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001645 goto abort_keep_alive;
1646
Olivier Houcharda798bf52019-03-08 18:52:00 +01001647 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001648 if (objt_server(s->target))
1649 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001650 rep->analysers &= AN_RES_FLT_END;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001651
1652 if (!(s->flags & SF_ERR_MASK))
1653 s->flags |= SF_ERR_CLICL;
1654 if (!(s->flags & SF_FINST_MASK))
1655 s->flags |= SF_FINST_H;
1656
1657 /* process_stream() will take care of the error */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001658 DBG_TRACE_DEVEL("leaving on error",
1659 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001660 return 0;
1661 }
1662
1663 channel_dont_close(rep);
1664 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001665 DBG_TRACE_DEVEL("waiting for more data",
1666 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001667 return 0;
1668 }
1669
1670 /* More interesting part now : we know that we have a complete
1671 * response which at least looks like HTTP. We have an indicator
1672 * of each header's length, so we can parse them quickly.
1673 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001674 msg->msg_state = HTTP_MSG_BODY;
Christopher Faulet29f17582019-05-23 11:03:26 +02001675 BUG_ON(htx_get_first_type(htx) != HTX_BLK_RES_SL);
Christopher Faulet297fbb42019-05-13 14:41:27 +02001676 sl = http_get_stline(htx);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001677
Christopher Faulet9768c262018-10-22 09:34:31 +02001678 /* 0: we might have to print this header in debug mode */
1679 if (unlikely((global.mode & MODE_DEBUG) &&
1680 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
1681 int32_t pos;
1682
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001683 http_debug_stline("srvrep", s, sl);
Christopher Faulet9768c262018-10-22 09:34:31 +02001684
Christopher Fauleta3f15502019-05-13 15:27:23 +02001685 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001686 struct htx_blk *blk = htx_get_blk(htx, pos);
1687 enum htx_blk_type type = htx_get_blk_type(blk);
1688
1689 if (type == HTX_BLK_EOH)
1690 break;
1691 if (type != HTX_BLK_HDR)
1692 continue;
1693
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001694 http_debug_hdr("srvhdr", s,
1695 htx_get_blk_name(htx, blk),
1696 htx_get_blk_value(htx, blk));
Christopher Faulet9768c262018-10-22 09:34:31 +02001697 }
1698 }
1699
Christopher Faulet03599112018-11-27 11:21:21 +01001700 /* 1: get the status code and the version. Also set HTTP flags */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001701 txn->status = sl->info.res.status;
Christopher Faulet03599112018-11-27 11:21:21 +01001702 if (sl->flags & HTX_SL_F_VER_11)
Christopher Faulet9768c262018-10-22 09:34:31 +02001703 msg->flags |= HTTP_MSGF_VER_11;
Christopher Faulet03599112018-11-27 11:21:21 +01001704 if (sl->flags & HTX_SL_F_XFER_LEN) {
1705 msg->flags |= HTTP_MSGF_XFER_LEN;
Christopher Faulet834eee72019-02-18 11:35:02 +01001706 msg->flags |= ((sl->flags & HTX_SL_F_CLEN) ? HTTP_MSGF_CNT_LEN : HTTP_MSGF_TE_CHNK);
Christopher Fauletb2db4fa2018-11-27 16:51:09 +01001707 if (sl->flags & HTX_SL_F_BODYLESS)
1708 msg->flags |= HTTP_MSGF_BODYLESS;
Christopher Faulet03599112018-11-27 11:21:21 +01001709 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001710
1711 n = txn->status / 100;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001712 if (n < 1 || n > 5)
1713 n = 0;
Christopher Faulet9768c262018-10-22 09:34:31 +02001714
Christopher Faulete0768eb2018-10-03 16:38:02 +02001715 /* when the client triggers a 4xx from the server, it's most often due
1716 * to a missing object or permission. These events should be tracked
1717 * because if they happen often, it may indicate a brute force or a
1718 * vulnerability scan.
1719 */
1720 if (n == 4)
1721 stream_inc_http_err_ctr(s);
1722
1723 if (objt_server(s->target))
Olivier Houcharda798bf52019-03-08 18:52:00 +01001724 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.p.http.rsp[n], 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001725
Christopher Faulete0768eb2018-10-03 16:38:02 +02001726 /* Adjust server's health based on status code. Note: status codes 501
1727 * and 505 are triggered on demand by client request, so we must not
1728 * count them as server failures.
1729 */
1730 if (objt_server(s->target)) {
1731 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001732 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_OK);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001733 else
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001734 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_STS);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001735 }
1736
1737 /*
1738 * We may be facing a 100-continue response, or any other informational
1739 * 1xx response which is non-final, in which case this is not the right
1740 * response, and we're waiting for the next one. Let's allow this response
1741 * to go to the client and wait for the next one. There's an exception for
1742 * 101 which is used later in the code to switch protocols.
1743 */
1744 if (txn->status < 200 &&
1745 (txn->status == 100 || txn->status >= 102)) {
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001746 FLT_STRM_CB(s, flt_http_reset(s, msg));
Christopher Faulet421e7692019-06-13 11:16:45 +02001747 htx->first = channel_htx_fwd_headers(rep, htx);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001748 msg->msg_state = HTTP_MSG_RPBEFORE;
Christopher Faulet3499f622019-09-03 15:23:54 +02001749 msg->flags = 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001750 txn->status = 0;
1751 s->logs.t_data = -1; /* was not a response yet */
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001752 goto next_one;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001753 }
1754
1755 /*
1756 * 2: check for cacheability.
1757 */
1758
1759 switch (txn->status) {
1760 case 200:
1761 case 203:
1762 case 204:
1763 case 206:
1764 case 300:
1765 case 301:
1766 case 404:
1767 case 405:
1768 case 410:
1769 case 414:
1770 case 501:
1771 break;
1772 default:
1773 /* RFC7231#6.1:
1774 * Responses with status codes that are defined as
1775 * cacheable by default (e.g., 200, 203, 204, 206,
1776 * 300, 301, 404, 405, 410, 414, and 501 in this
1777 * specification) can be reused by a cache with
1778 * heuristic expiration unless otherwise indicated
1779 * by the method definition or explicit cache
1780 * controls [RFC7234]; all other status codes are
1781 * not cacheable by default.
1782 */
1783 txn->flags &= ~(TX_CACHEABLE | TX_CACHE_COOK);
1784 break;
1785 }
1786
1787 /*
1788 * 3: we may need to capture headers
1789 */
1790 s->logs.logwait &= ~LW_RESP;
1791 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->res_cap))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001792 http_capture_headers(htx, s->res_cap, sess->fe->rsp_cap);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001793
Christopher Faulet9768c262018-10-22 09:34:31 +02001794 /* Skip parsing if no content length is possible. */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001795 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
1796 txn->status == 101)) {
1797 /* Either we've established an explicit tunnel, or we're
1798 * switching the protocol. In both cases, we're very unlikely
1799 * to understand the next protocols. We have to switch to tunnel
1800 * mode, so that we transfer the request and responses then let
1801 * this protocol pass unmodified. When we later implement specific
1802 * parsers for such protocols, we'll want to check the Upgrade
1803 * header which contains information about that protocol for
1804 * responses with status 101 (eg: see RFC2817 about TLS).
1805 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02001806 txn->flags |= TX_CON_WANT_TUN;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001807 }
1808
Christopher Faulet61608322018-11-23 16:23:45 +01001809 /* check for NTML authentication headers in 401 (WWW-Authenticate) and
1810 * 407 (Proxy-Authenticate) responses and set the connection to private
1811 */
1812 srv_conn = cs_conn(objt_cs(s->si[1].end));
1813 if (srv_conn) {
1814 struct ist hdr;
1815 struct http_hdr_ctx ctx;
1816
1817 if (txn->status == 401)
1818 hdr = ist("WWW-Authenticate");
1819 else if (txn->status == 407)
1820 hdr = ist("Proxy-Authenticate");
1821 else
1822 goto end;
1823
1824 ctx.blk = NULL;
1825 while (http_find_header(htx, hdr, &ctx, 0)) {
Willy Tarreauf1dcced2020-05-07 19:27:02 +02001826 /* If www-authenticate contains "Negotiate", "Nego2", or "NTLM",
1827 * possibly followed by blanks and a base64 string, the connection
1828 * is private. Since it's a mess to deal with, we only check for
1829 * values starting with "NTLM" or "Nego". Note that often multiple
1830 * headers are sent by the server there.
1831 */
1832 if ((ctx.value.len >= 4 && strncasecmp(ctx.value.ptr, "Nego", 4) == 0) ||
Willy Tarreau49a1d282020-05-07 19:10:15 +02001833 (ctx.value.len >= 4 && strncasecmp(ctx.value.ptr, "NTLM", 4) == 0)) {
Olivier Houchard250031e2019-05-29 15:01:50 +02001834 sess->flags |= SESS_FL_PREFER_LAST;
Christopher Faulet61608322018-11-23 16:23:45 +01001835 srv_conn->flags |= CO_FL_PRIVATE;
Willy Tarreauf1dcced2020-05-07 19:27:02 +02001836 break;
Olivier Houchard250031e2019-05-29 15:01:50 +02001837 }
Christopher Faulet61608322018-11-23 16:23:45 +01001838 }
1839 }
1840
1841 end:
Christopher Faulete0768eb2018-10-03 16:38:02 +02001842 /* we want to have the response time before we start processing it */
1843 s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now);
1844
1845 /* end of job, return OK */
1846 rep->analysers &= ~an_bit;
1847 rep->analyse_exp = TICK_ETERNITY;
1848 channel_auto_close(rep);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001849 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001850 return 1;
1851
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001852 return_int_err:
Christopher Fauletcff0f732019-12-16 16:13:44 +01001853 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01001854 _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001855 if (sess->listener->counters)
1856 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01001857 if (objt_server(s->target))
1858 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.internal_errors, 1);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001859 txn->status = 500;
1860 if (!(s->flags & SF_ERR_MASK))
1861 s->flags |= SF_ERR_INTERNAL;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001862 goto return_prx_cond;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001863
1864 return_bad_res:
Olivier Houcharda798bf52019-03-08 18:52:00 +01001865 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulet47365272018-10-31 17:40:50 +01001866 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001867 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001868 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Christopher Faulet47365272018-10-31 17:40:50 +01001869 }
Olivier Houcharde3249a92019-05-03 23:01:47 +02001870 if ((s->be->retry_type & PR_RE_JUNK_REQUEST) &&
Olivier Houchardad26d8d2019-05-10 17:48:28 +02001871 (si_b->flags & SI_FL_L7_RETRY) &&
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001872 do_l7_retry(s, si_b) == 0) {
1873 DBG_TRACE_DEVEL("leaving on L7 retry",
1874 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharde3249a92019-05-03 23:01:47 +02001875 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001876 }
Christopher Faulet47365272018-10-31 17:40:50 +01001877 txn->status = 502;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001878 /* fall through */
1879
Christopher Fauletb8a53712019-12-16 11:29:38 +01001880 return_prx_cond:
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001881 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet47365272018-10-31 17:40:50 +01001882
1883 if (!(s->flags & SF_ERR_MASK))
1884 s->flags |= SF_ERR_PRXCOND;
1885 if (!(s->flags & SF_FINST_MASK))
1886 s->flags |= SF_FINST_H;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001887
1888 s->si[1].flags |= SI_FL_NOLINGER;
1889 rep->analysers &= AN_RES_FLT_END;
Christopher Faulete58c0002020-03-02 16:21:01 +01001890 s->req.analysers &= AN_REQ_FLT_END;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001891 rep->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001892 DBG_TRACE_DEVEL("leaving on error",
1893 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulet47365272018-10-31 17:40:50 +01001894 return 0;
1895
Christopher Faulete0768eb2018-10-03 16:38:02 +02001896 abort_keep_alive:
1897 /* A keep-alive request to the server failed on a network error.
1898 * The client is required to retry. We need to close without returning
1899 * any other information so that the client retries.
1900 */
1901 txn->status = 0;
1902 rep->analysers &= AN_RES_FLT_END;
1903 s->req.analysers &= AN_REQ_FLT_END;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001904 s->logs.logwait = 0;
1905 s->logs.level = 0;
1906 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001907 http_reply_and_close(s, txn->status, NULL);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001908 DBG_TRACE_DEVEL("leaving by closing K/A connection",
1909 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001910 return 0;
1911}
1912
1913/* This function performs all the processing enabled for the current response.
1914 * It normally returns 1 unless it wants to break. It relies on buffers flags,
1915 * and updates s->res.analysers. It might make sense to explode it into several
1916 * other functions. It works like process_request (see indications above).
1917 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001918int http_process_res_common(struct stream *s, struct channel *rep, int an_bit, struct proxy *px)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001919{
1920 struct session *sess = s->sess;
1921 struct http_txn *txn = s->txn;
1922 struct http_msg *msg = &txn->rsp;
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001923 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001924 struct proxy *cur_proxy;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001925 enum rule_result ret = HTTP_RULE_RES_CONT;
1926
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001927 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
1928 return 0;
Christopher Faulet9768c262018-10-22 09:34:31 +02001929
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001930 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001931
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001932 htx = htxbuf(&rep->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001933
1934 /* The stats applet needs to adjust the Connection header but we don't
1935 * apply any filter there.
1936 */
1937 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
1938 rep->analysers &= ~an_bit;
1939 rep->analyse_exp = TICK_ETERNITY;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001940 goto end;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001941 }
1942
1943 /*
1944 * We will have to evaluate the filters.
1945 * As opposed to version 1.2, now they will be evaluated in the
1946 * filters order and not in the header order. This means that
1947 * each filter has to be validated among all headers.
1948 *
1949 * Filters are tried with ->be first, then with ->fe if it is
1950 * different from ->be.
1951 *
1952 * Maybe we are in resume condiion. In this case I choose the
1953 * "struct proxy" which contains the rule list matching the resume
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05001954 * pointer. If none of these "struct proxy" match, I initialise
Christopher Faulete0768eb2018-10-03 16:38:02 +02001955 * the process with the first one.
1956 *
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05001957 * In fact, I check only correspondence between the current list
Christopher Faulete0768eb2018-10-03 16:38:02 +02001958 * pointer and the ->fe rule list. If it doesn't match, I initialize
1959 * the loop with the ->be.
1960 */
1961 if (s->current_rule_list == &sess->fe->http_res_rules)
1962 cur_proxy = sess->fe;
1963 else
1964 cur_proxy = s->be;
1965 while (1) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001966 /* evaluate http-response rules */
1967 if (ret == HTTP_RULE_RES_CONT) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001968 ret = http_res_get_intercept_rule(cur_proxy, &cur_proxy->http_res_rules, s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001969
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001970 switch (ret) {
1971 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
1972 goto return_prx_yield;
1973
1974 case HTTP_RULE_RES_CONT:
1975 case HTTP_RULE_RES_STOP: /* nothing to do */
1976 break;
1977
1978 case HTTP_RULE_RES_DENY: /* deny or tarpit */
1979 goto deny;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001980
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001981 case HTTP_RULE_RES_ABRT: /* abort request, response already sent */
1982 goto return_prx_cond;
1983
1984 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
Christopher Fauletb8a53712019-12-16 11:29:38 +01001985 goto done;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001986
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001987 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
1988 goto return_bad_res;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001989
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001990 case HTTP_RULE_RES_ERROR: /* failed with a bad request */
1991 goto return_int_err;
1992 }
1993
1994 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001995
Christopher Faulete0768eb2018-10-03 16:38:02 +02001996 /* check whether we're already working on the frontend */
1997 if (cur_proxy == sess->fe)
1998 break;
1999 cur_proxy = sess->fe;
2000 }
2001
Christopher Faulete0768eb2018-10-03 16:38:02 +02002002 /* OK that's all we can do for 1xx responses */
2003 if (unlikely(txn->status < 200 && txn->status != 101))
Christopher Fauletf2824e62018-10-01 12:12:37 +02002004 goto end;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002005
2006 /*
2007 * Now check for a server cookie.
2008 */
2009 if (s->be->cookie_name || sess->fe->capture_name || (s->be->options & PR_O_CHK_CACHE))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002010 http_manage_server_side_cookies(s, rep);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002011
2012 /*
2013 * Check for cache-control or pragma headers if required.
2014 */
2015 if ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002016 http_check_response_for_cacheability(s, rep);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002017
2018 /*
2019 * Add server cookie in the response if needed
2020 */
2021 if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) &&
2022 !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) &&
2023 (!(s->flags & SF_DIRECT) ||
2024 ((s->be->cookie_maxidle || txn->cookie_last_date) &&
2025 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
2026 (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
2027 (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
2028 (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
2029 !(s->flags & SF_IGNORE_PRST)) {
2030 /* the server is known, it's not the one the client requested, or the
2031 * cookie's last seen date needs to be refreshed. We have to
2032 * insert a set-cookie here, except if we want to insert only on POST
2033 * requests and this one isn't. Note that servers which don't have cookies
2034 * (eg: some backup servers) will return a full cookie removal request.
2035 */
2036 if (!objt_server(s->target)->cookie) {
2037 chunk_printf(&trash,
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002038 "%s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
Christopher Faulete0768eb2018-10-03 16:38:02 +02002039 s->be->cookie_name);
2040 }
2041 else {
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002042 chunk_printf(&trash, "%s=%s", s->be->cookie_name, objt_server(s->target)->cookie);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002043
2044 if (s->be->cookie_maxidle || s->be->cookie_maxlife) {
2045 /* emit last_date, which is mandatory */
2046 trash.area[trash.data++] = COOKIE_DELIM_DATE;
2047 s30tob64((date.tv_sec+3) >> 2,
2048 trash.area + trash.data);
2049 trash.data += 5;
2050
2051 if (s->be->cookie_maxlife) {
2052 /* emit first_date, which is either the original one or
2053 * the current date.
2054 */
2055 trash.area[trash.data++] = COOKIE_DELIM_DATE;
2056 s30tob64(txn->cookie_first_date ?
2057 txn->cookie_first_date >> 2 :
2058 (date.tv_sec+3) >> 2,
2059 trash.area + trash.data);
2060 trash.data += 5;
2061 }
2062 }
2063 chunk_appendf(&trash, "; path=/");
2064 }
2065
2066 if (s->be->cookie_domain)
2067 chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain);
2068
2069 if (s->be->ck_opts & PR_CK_HTTPONLY)
2070 chunk_appendf(&trash, "; HttpOnly");
2071
2072 if (s->be->ck_opts & PR_CK_SECURE)
2073 chunk_appendf(&trash, "; Secure");
2074
Christopher Faulet2f533902020-01-21 11:06:48 +01002075 if (s->be->cookie_attrs)
2076 chunk_appendf(&trash, "; %s", s->be->cookie_attrs);
2077
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002078 if (unlikely(!http_add_header(htx, ist("Set-Cookie"), ist2(trash.area, trash.data))))
Christopher Fauletb8a53712019-12-16 11:29:38 +01002079 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002080
2081 txn->flags &= ~TX_SCK_MASK;
2082 if (__objt_server(s->target)->cookie && (s->flags & SF_DIRECT))
2083 /* the server did not change, only the date was updated */
2084 txn->flags |= TX_SCK_UPDATED;
2085 else
2086 txn->flags |= TX_SCK_INSERTED;
2087
2088 /* Here, we will tell an eventual cache on the client side that we don't
2089 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
2090 * Some caches understand the correct form: 'no-cache="set-cookie"', but
2091 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
2092 */
2093 if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
2094
2095 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
2096
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002097 if (unlikely(!http_add_header(htx, ist("Cache-control"), ist("private"))))
Christopher Fauletb8a53712019-12-16 11:29:38 +01002098 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002099 }
2100 }
2101
2102 /*
2103 * Check if result will be cacheable with a cookie.
2104 * We'll block the response if security checks have caught
2105 * nasty things such as a cacheable cookie.
2106 */
2107 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
2108 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
2109 (s->be->options & PR_O_CHK_CACHE)) {
2110 /* we're in presence of a cacheable response containing
2111 * a set-cookie header. We'll block it as requested by
2112 * the 'checkcache' option, and send an alert.
2113 */
Christopher Faulete0768eb2018-10-03 16:38:02 +02002114 ha_alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
2115 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
2116 send_log(s->be, LOG_ALERT,
2117 "Blocking cacheable cookie in response from instance %s, server %s.\n",
2118 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
Christopher Fauletb8a53712019-12-16 11:29:38 +01002119 goto deny;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002120 }
2121
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002122 end:
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002123 /*
2124 * Evaluate after-response rules before forwarding the response. rules
2125 * from the backend are evaluated first, then one from the frontend if
2126 * it differs.
2127 */
2128 if (!http_eval_after_res_rules(s))
2129 goto return_int_err;
2130
Christopher Faulete0768eb2018-10-03 16:38:02 +02002131 /* Always enter in the body analyzer */
2132 rep->analysers &= ~AN_RES_FLT_XFER_DATA;
2133 rep->analysers |= AN_RES_HTTP_XFER_BODY;
2134
2135 /* if the user wants to log as soon as possible, without counting
2136 * bytes from the server, then this is the right moment. We have
2137 * to temporarily assign bytes_out to log what we currently have.
2138 */
2139 if (!LIST_ISEMPTY(&sess->fe->logformat) && !(s->logs.logwait & LW_BYTES)) {
2140 s->logs.t_close = s->logs.t_data; /* to get a valid end date */
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002141 s->logs.bytes_out = htx->data;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002142 s->do_log(s);
2143 s->logs.bytes_out = 0;
2144 }
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002145
Christopher Fauletb8a53712019-12-16 11:29:38 +01002146 done:
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002147 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002148 rep->analysers &= ~an_bit;
2149 rep->analyse_exp = TICK_ETERNITY;
2150 return 1;
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002151
Christopher Fauletb8a53712019-12-16 11:29:38 +01002152 deny:
Christopher Fauletb8a53712019-12-16 11:29:38 +01002153 _HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_resp, 1);
Christopher Fauleta08546b2019-12-16 16:07:34 +01002154 _HA_ATOMIC_ADD(&s->be->be_counters.denied_resp, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002155 if (sess->listener->counters)
2156 _HA_ATOMIC_ADD(&sess->listener->counters->denied_resp, 1);
Christopher Fauleta08546b2019-12-16 16:07:34 +01002157 if (objt_server(s->target))
2158 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.denied_resp, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002159 goto return_prx_err;
2160
2161 return_int_err:
2162 txn->status = 500;
2163 if (!(s->flags & SF_ERR_MASK))
2164 s->flags |= SF_ERR_INTERNAL;
Christopher Fauletcff0f732019-12-16 16:13:44 +01002165 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002166 _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1);
2167 if (objt_server(s->target))
2168 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01002169 if (objt_server(s->target))
2170 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002171 goto return_prx_err;
2172
2173 return_bad_res:
2174 txn->status = 502;
Christopher Fauleta20a6532020-02-05 10:16:41 +01002175 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
2176 if (objt_server(s->target)) {
2177 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
2178 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_RSP);
2179 }
Christopher Fauletb8a53712019-12-16 11:29:38 +01002180 /* fall through */
2181
2182 return_prx_err:
2183 http_reply_and_close(s, txn->status, http_error_message(s));
2184 /* fall through */
2185
2186 return_prx_cond:
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002187 s->logs.t_data = -1; /* was not a valid response */
2188 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002189
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002190 if (!(s->flags & SF_ERR_MASK))
2191 s->flags |= SF_ERR_PRXCOND;
2192 if (!(s->flags & SF_FINST_MASK))
2193 s->flags |= SF_FINST_H;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002194
Christopher Faulete58c0002020-03-02 16:21:01 +01002195 rep->analysers &= AN_RES_FLT_END;
2196 s->req.analysers &= AN_REQ_FLT_END;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002197 rep->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002198 DBG_TRACE_DEVEL("leaving on error",
2199 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002200 return 0;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002201
2202 return_prx_yield:
2203 channel_dont_close(rep);
2204 DBG_TRACE_DEVEL("waiting for more data",
2205 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
2206 return 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002207}
2208
2209/* This function is an analyser which forwards response body (including chunk
2210 * sizes if any). It is called as soon as we must forward, even if we forward
2211 * zero byte. The only situation where it must not be called is when we're in
2212 * tunnel mode and we want to forward till the close. It's used both to forward
2213 * remaining data and to resync after end of body. It expects the msg_state to
2214 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
2215 * read more data, or 1 once we can go on with next request or end the stream.
2216 *
2217 * It is capable of compressing response data both in content-length mode and
2218 * in chunked mode. The state machines follows different flows depending on
2219 * whether content-length and chunked modes are used, since there are no
2220 * trailers in content-length :
2221 *
2222 * chk-mode cl-mode
2223 * ,----- BODY -----.
2224 * / \
2225 * V size > 0 V chk-mode
2226 * .--> SIZE -------------> DATA -------------> CRLF
2227 * | | size == 0 | last byte |
2228 * | v final crlf v inspected |
2229 * | TRAILERS -----------> DONE |
2230 * | |
2231 * `----------------------------------------------'
2232 *
2233 * Compression only happens in the DATA state, and must be flushed in final
2234 * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding
2235 * is performed at once on final states for all bytes parsed, or when leaving
2236 * on missing data.
2237 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002238int http_response_forward_body(struct stream *s, struct channel *res, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +02002239{
2240 struct session *sess = s->sess;
2241 struct http_txn *txn = s->txn;
2242 struct http_msg *msg = &s->txn->rsp;
Christopher Faulet9768c262018-10-22 09:34:31 +02002243 struct htx *htx;
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002244 int ret;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002245
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002246 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002247
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002248 htx = htxbuf(&res->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002249
Christopher Fauletb9a92f32019-09-09 10:15:21 +02002250 if (htx->flags & HTX_FL_PARSING_ERROR)
2251 goto return_bad_res;
2252 if (htx->flags & HTX_FL_PROCESSING_ERROR)
2253 goto return_int_err;
2254
Christopher Faulete0768eb2018-10-03 16:38:02 +02002255 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Christopher Fauletf2824e62018-10-01 12:12:37 +02002256 ((res->flags & CF_SHUTW) && (res->to_forward || co_data(res)))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02002257 /* Output closed while we were sending data. We must abort and
2258 * wake the other side up.
2259 */
Christopher Faulete0768eb2018-10-03 16:38:02 +02002260 msg->msg_state = HTTP_MSG_ERROR;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002261 http_end_response(s);
2262 http_end_request(s);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002263 DBG_TRACE_DEVEL("leaving on error",
2264 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002265 return 1;
2266 }
2267
Christopher Faulet9768c262018-10-22 09:34:31 +02002268 if (msg->msg_state == HTTP_MSG_BODY)
2269 msg->msg_state = HTTP_MSG_DATA;
2270
Christopher Faulete0768eb2018-10-03 16:38:02 +02002271 /* in most states, we should abort in case of early close */
2272 channel_auto_close(res);
2273
Christopher Faulete0768eb2018-10-03 16:38:02 +02002274 if (res->to_forward) {
Christopher Faulet66af0b22019-03-22 14:54:52 +01002275 if (res->to_forward == CHN_INFINITE_FORWARD) {
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002276 if (res->flags & CF_EOI)
2277 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet66af0b22019-03-22 14:54:52 +01002278 }
2279 else {
2280 /* We can't process the buffer's contents yet */
2281 res->flags |= CF_WAKE_WRITE;
2282 goto missing_data_or_waiting;
2283 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02002284 }
2285
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002286 if (msg->msg_state >= HTTP_MSG_ENDING)
2287 goto ending;
2288
2289 if ((txn->meth == HTTP_METH_CONNECT && txn->status == 200) || txn->status == 101 ||
2290 (!(msg->flags & HTTP_MSGF_XFER_LEN) && !HAS_RSP_DATA_FILTERS(s))) {
2291 msg->msg_state = HTTP_MSG_ENDING;
2292 goto ending;
2293 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002294
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002295 /* Forward input data. We get it by removing all outgoing data not
2296 * forwarded yet from HTX data size. If there are some data filters, we
2297 * let them decide the amount of data to forward.
Christopher Faulet9768c262018-10-22 09:34:31 +02002298 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002299 if (HAS_RSP_DATA_FILTERS(s)) {
2300 ret = flt_http_payload(s, msg, htx->data);
2301 if (ret < 0)
2302 goto return_bad_res;
Christopher Faulet421e7692019-06-13 11:16:45 +02002303 c_adv(res, ret);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002304 }
2305 else {
Christopher Faulet421e7692019-06-13 11:16:45 +02002306 c_adv(res, htx->data - co_data(res));
Christopher Faulet66af0b22019-03-22 14:54:52 +01002307 if (msg->flags & HTTP_MSGF_XFER_LEN)
2308 channel_htx_forward_forever(res, htx);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002309 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002310
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002311 if (htx->data != co_data(res))
2312 goto missing_data_or_waiting;
2313
2314 if (!(msg->flags & HTTP_MSGF_XFER_LEN) && res->flags & CF_SHUTR) {
2315 msg->msg_state = HTTP_MSG_ENDING;
2316 goto ending;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002317 }
2318
Christopher Faulet9768c262018-10-22 09:34:31 +02002319 /* Check if the end-of-message is reached and if so, switch the message
Christopher Fauletd20fdb02019-06-13 16:43:22 +02002320 * in HTTP_MSG_ENDING state. Then if all data was marked to be
2321 * forwarded, set the state to HTTP_MSG_DONE.
Christopher Faulet9768c262018-10-22 09:34:31 +02002322 */
2323 if (htx_get_tail_type(htx) != HTX_BLK_EOM)
2324 goto missing_data_or_waiting;
2325
Christopher Fauletd20fdb02019-06-13 16:43:22 +02002326 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet9768c262018-10-22 09:34:31 +02002327
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002328 ending:
2329 /* other states, ENDING...TUNNEL */
2330 if (msg->msg_state >= HTTP_MSG_DONE)
2331 goto done;
Christopher Faulet9768c262018-10-22 09:34:31 +02002332
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002333 if (HAS_RSP_DATA_FILTERS(s)) {
2334 ret = flt_http_end(s, msg);
2335 if (ret <= 0) {
2336 if (!ret)
2337 goto missing_data_or_waiting;
2338 goto return_bad_res;
2339 }
2340 }
2341
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002342 if ((txn->meth == HTTP_METH_CONNECT && txn->status == 200) || txn->status == 101 ||
2343 !(msg->flags & HTTP_MSGF_XFER_LEN)) {
2344 msg->msg_state = HTTP_MSG_TUNNEL;
2345 goto ending;
2346 }
2347 else {
2348 msg->msg_state = HTTP_MSG_DONE;
2349 res->to_forward = 0;
2350 }
2351
2352 done:
2353
2354 channel_dont_close(res);
2355
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002356 http_end_response(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002357 if (!(res->analysers & an_bit)) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002358 http_end_request(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002359 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
2360 if (res->flags & CF_SHUTW) {
2361 /* response errors are most likely due to the
2362 * client aborting the transfer. */
Christopher Faulet93e02d82019-03-08 14:18:50 +01002363 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002364 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02002365 goto return_bad_res;
2366 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002367 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002368 return 1;
2369 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002370 DBG_TRACE_DEVEL("waiting for the end of the HTTP txn",
2371 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002372 return 0;
2373
2374 missing_data_or_waiting:
2375 if (res->flags & CF_SHUTW)
Christopher Faulet93e02d82019-03-08 14:18:50 +01002376 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002377
2378 /* stop waiting for data if the input is closed before the end. If the
2379 * client side was already closed, it means that the client has aborted,
2380 * so we don't want to count this as a server abort. Otherwise it's a
2381 * server abort.
2382 */
Christopher Fauletd20fdb02019-06-13 16:43:22 +02002383 if (msg->msg_state < HTTP_MSG_ENDING && res->flags & CF_SHUTR) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02002384 if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))
Christopher Faulet93e02d82019-03-08 14:18:50 +01002385 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002386 /* If we have some pending data, we continue the processing */
Christopher Faulet93e02d82019-03-08 14:18:50 +01002387 if (htx_is_empty(htx))
2388 goto return_srv_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002389 }
2390
Christopher Faulete0768eb2018-10-03 16:38:02 +02002391 /* When TE: chunked is used, we need to get there again to parse
2392 * remaining chunks even if the server has closed, so we don't want to
Christopher Faulet9768c262018-10-22 09:34:31 +02002393 * set CF_DONTCLOSE. Similarly when there is a content-leng or if there
2394 * are filters registered on the stream, we don't want to forward a
2395 * close
Christopher Faulete0768eb2018-10-03 16:38:02 +02002396 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002397 if ((msg->flags & HTTP_MSGF_XFER_LEN) || HAS_RSP_DATA_FILTERS(s))
Christopher Faulete0768eb2018-10-03 16:38:02 +02002398 channel_dont_close(res);
2399
2400 /* We know that more data are expected, but we couldn't send more that
2401 * what we did. So we always set the CF_EXPECT_MORE flag so that the
2402 * system knows it must not set a PUSH on this first part. Interactive
2403 * modes are already handled by the stream sock layer. We must not do
2404 * this in content-length mode because it could present the MSG_MORE
2405 * flag with the last block of forwarded data, which would cause an
2406 * additional delay to be observed by the receiver.
2407 */
2408 if ((msg->flags & HTTP_MSGF_TE_CHNK) || (msg->flags & HTTP_MSGF_COMPRESSING))
2409 res->flags |= CF_EXPECT_MORE;
2410
2411 /* the stream handler will take care of timeouts and errors */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002412 DBG_TRACE_DEVEL("waiting for more data to forward",
2413 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002414 return 0;
2415
Christopher Faulet93e02d82019-03-08 14:18:50 +01002416 return_srv_abort:
2417 _HA_ATOMIC_ADD(&sess->fe->fe_counters.srv_aborts, 1);
2418 _HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01002419 if (sess->listener->counters)
2420 _HA_ATOMIC_ADD(&sess->listener->counters->srv_aborts, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002421 if (objt_server(s->target))
Christopher Fauletcff0f732019-12-16 16:13:44 +01002422 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.srv_aborts, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002423 if (!(s->flags & SF_ERR_MASK))
2424 s->flags |= SF_ERR_SRVCL;
2425 goto return_error;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002426
Christopher Faulet93e02d82019-03-08 14:18:50 +01002427 return_cli_abort:
2428 _HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
2429 _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01002430 if (sess->listener->counters)
2431 _HA_ATOMIC_ADD(&sess->listener->counters->cli_aborts, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002432 if (objt_server(s->target))
Christopher Fauletcff0f732019-12-16 16:13:44 +01002433 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.cli_aborts, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002434 if (!(s->flags & SF_ERR_MASK))
2435 s->flags |= SF_ERR_CLICL;
2436 goto return_error;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002437
Christopher Fauletb9a92f32019-09-09 10:15:21 +02002438 return_int_err:
Christopher Fauletcff0f732019-12-16 16:13:44 +01002439 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002440 _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01002441 if (sess->listener->counters)
2442 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002443 if (objt_server(s->target))
2444 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.internal_errors, 1);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02002445 if (!(s->flags & SF_ERR_MASK))
2446 s->flags |= SF_ERR_INTERNAL;
2447 goto return_error;
2448
Christopher Faulet93e02d82019-03-08 14:18:50 +01002449 return_bad_res:
2450 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
2451 if (objt_server(s->target)) {
Christopher Fauletcff0f732019-12-16 16:13:44 +01002452 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002453 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_RSP);
2454 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02002455 if (!(s->flags & SF_ERR_MASK))
Christopher Faulet93e02d82019-03-08 14:18:50 +01002456 s->flags |= SF_ERR_SRVCL;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002457 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +02002458
Christopher Faulet93e02d82019-03-08 14:18:50 +01002459 return_error:
Christopher Faulete0768eb2018-10-03 16:38:02 +02002460 /* don't send any error message as we're in the body */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002461 http_reply_and_close(s, txn->status, NULL);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002462 res->analysers &= AN_RES_FLT_END;
2463 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 +02002464 if (!(s->flags & SF_FINST_MASK))
2465 s->flags |= SF_FINST_D;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002466 DBG_TRACE_DEVEL("leaving on error",
2467 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002468 return 0;
2469}
2470
Christopher Fauletf2824e62018-10-01 12:12:37 +02002471/* Perform an HTTP redirect based on the information in <rule>. The function
Christopher Faulet99daf282018-11-28 22:58:13 +01002472 * returns zero on success, or zero in case of a, irrecoverable error such
Christopher Fauletf2824e62018-10-01 12:12:37 +02002473 * as too large a request to build a valid response.
2474 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002475int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn)
Christopher Fauletf2824e62018-10-01 12:12:37 +02002476{
Christopher Faulet99daf282018-11-28 22:58:13 +01002477 struct channel *req = &s->req;
2478 struct channel *res = &s->res;
2479 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01002480 struct htx_sl *sl;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002481 struct buffer *chunk;
Christopher Faulet99daf282018-11-28 22:58:13 +01002482 struct ist status, reason, location;
2483 unsigned int flags;
Christopher Faulet08e66462019-05-23 16:44:59 +02002484 int close = 0; /* Try to keep the connection alive byt default */
Christopher Fauletf2824e62018-10-01 12:12:37 +02002485
2486 chunk = alloc_trash_chunk();
Christopher Fauletb8a53712019-12-16 11:29:38 +01002487 if (!chunk) {
2488 if (!(s->flags & SF_ERR_MASK))
2489 s->flags |= SF_ERR_RESOURCE;
Christopher Faulet99daf282018-11-28 22:58:13 +01002490 goto fail;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002491 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002492
Christopher Faulet99daf282018-11-28 22:58:13 +01002493 /*
2494 * Create the location
2495 */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002496 htx = htxbuf(&req->buf);
Christopher Fauletf2824e62018-10-01 12:12:37 +02002497 switch(rule->type) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002498 case REDIRECT_TYPE_SCHEME: {
2499 struct http_hdr_ctx ctx;
2500 struct ist path, host;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002501
Christopher Faulet99daf282018-11-28 22:58:13 +01002502 host = ist("");
2503 ctx.blk = NULL;
2504 if (http_find_header(htx, ist("Host"), &ctx, 0))
2505 host = ctx.value;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002506
Christopher Faulet297fbb42019-05-13 14:41:27 +02002507 sl = http_get_stline(htx);
Christopher Faulet99daf282018-11-28 22:58:13 +01002508 path = http_get_path(htx_sl_req_uri(sl));
2509 /* build message using path */
Tim Duesterhused526372020-03-05 17:56:33 +01002510 if (isttest(path)) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002511 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2512 int qs = 0;
2513 while (qs < path.len) {
2514 if (*(path.ptr + qs) == '?') {
2515 path.len = qs;
2516 break;
2517 }
2518 qs++;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002519 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002520 }
2521 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002522 else
2523 path = ist("/");
Christopher Fauletf2824e62018-10-01 12:12:37 +02002524
Christopher Faulet99daf282018-11-28 22:58:13 +01002525 if (rule->rdr_str) { /* this is an old "redirect" rule */
2526 /* add scheme */
2527 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2528 goto fail;
2529 }
2530 else {
2531 /* add scheme with executing log format */
2532 chunk->data += build_logline(s, chunk->area + chunk->data,
2533 chunk->size - chunk->data,
2534 &rule->rdr_fmt);
2535 }
2536 /* add "://" + host + path */
2537 if (!chunk_memcat(chunk, "://", 3) ||
2538 !chunk_memcat(chunk, host.ptr, host.len) ||
2539 !chunk_memcat(chunk, path.ptr, path.len))
2540 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002541
Christopher Faulet99daf282018-11-28 22:58:13 +01002542 /* append a slash at the end of the location if needed and missing */
2543 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
2544 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
2545 if (chunk->data + 1 >= chunk->size)
2546 goto fail;
2547 chunk->area[chunk->data++] = '/';
2548 }
2549 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002550 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002551
Christopher Faulet99daf282018-11-28 22:58:13 +01002552 case REDIRECT_TYPE_PREFIX: {
2553 struct ist path;
2554
Christopher Faulet297fbb42019-05-13 14:41:27 +02002555 sl = http_get_stline(htx);
Christopher Faulet99daf282018-11-28 22:58:13 +01002556 path = http_get_path(htx_sl_req_uri(sl));
2557 /* build message using path */
Tim Duesterhused526372020-03-05 17:56:33 +01002558 if (isttest(path)) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002559 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2560 int qs = 0;
2561 while (qs < path.len) {
2562 if (*(path.ptr + qs) == '?') {
2563 path.len = qs;
2564 break;
2565 }
2566 qs++;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002567 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002568 }
2569 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002570 else
2571 path = ist("/");
Christopher Fauletf2824e62018-10-01 12:12:37 +02002572
Christopher Faulet99daf282018-11-28 22:58:13 +01002573 if (rule->rdr_str) { /* this is an old "redirect" rule */
2574 /* add prefix. Note that if prefix == "/", we don't want to
2575 * add anything, otherwise it makes it hard for the user to
2576 * configure a self-redirection.
2577 */
2578 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
2579 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2580 goto fail;
2581 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002582 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002583 else {
2584 /* add prefix with executing log format */
2585 chunk->data += build_logline(s, chunk->area + chunk->data,
2586 chunk->size - chunk->data,
2587 &rule->rdr_fmt);
2588 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002589
Christopher Faulet99daf282018-11-28 22:58:13 +01002590 /* add path */
2591 if (!chunk_memcat(chunk, path.ptr, path.len))
2592 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002593
Christopher Faulet99daf282018-11-28 22:58:13 +01002594 /* append a slash at the end of the location if needed and missing */
2595 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
2596 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
2597 if (chunk->data + 1 >= chunk->size)
2598 goto fail;
2599 chunk->area[chunk->data++] = '/';
2600 }
2601 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002602 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002603 case REDIRECT_TYPE_LOCATION:
2604 default:
2605 if (rule->rdr_str) { /* this is an old "redirect" rule */
2606 /* add location */
2607 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2608 goto fail;
2609 }
2610 else {
2611 /* add location with executing log format */
2612 chunk->data += build_logline(s, chunk->area + chunk->data,
2613 chunk->size - chunk->data,
2614 &rule->rdr_fmt);
2615 }
2616 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002617 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002618 location = ist2(chunk->area, chunk->data);
2619
2620 /*
2621 * Create the 30x response
2622 */
2623 switch (rule->code) {
2624 case 308:
2625 status = ist("308");
2626 reason = ist("Permanent Redirect");
2627 break;
2628 case 307:
2629 status = ist("307");
2630 reason = ist("Temporary Redirect");
2631 break;
2632 case 303:
2633 status = ist("303");
2634 reason = ist("See Other");
2635 break;
2636 case 301:
2637 status = ist("301");
2638 reason = ist("Moved Permanently");
2639 break;
2640 case 302:
2641 default:
2642 status = ist("302");
2643 reason = ist("Found");
2644 break;
2645 }
2646
Christopher Faulet08e66462019-05-23 16:44:59 +02002647 if (!(txn->req.flags & HTTP_MSGF_BODYLESS) && txn->req.msg_state != HTTP_MSG_DONE)
2648 close = 1;
2649
Christopher Faulet99daf282018-11-28 22:58:13 +01002650 htx = htx_from_buf(&res->buf);
Kevin Zhu96b36392020-01-07 09:42:55 +01002651 /* Trim any possible response */
2652 channel_htx_truncate(&s->res, htx);
Christopher Faulet99daf282018-11-28 22:58:13 +01002653 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
2654 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), status, reason);
2655 if (!sl)
2656 goto fail;
2657 sl->info.res.status = rule->code;
2658 s->txn->status = rule->code;
2659
Christopher Faulet08e66462019-05-23 16:44:59 +02002660 if (close && !htx_add_header(htx, ist("Connection"), ist("close")))
2661 goto fail;
2662
2663 if (!htx_add_header(htx, ist("Content-length"), ist("0")) ||
Christopher Faulet99daf282018-11-28 22:58:13 +01002664 !htx_add_header(htx, ist("Location"), location))
2665 goto fail;
2666
2667 if (rule->code == 302 || rule->code == 303 || rule->code == 307) {
2668 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")))
2669 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002670 }
2671
2672 if (rule->cookie_len) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002673 if (!htx_add_header(htx, ist("Set-Cookie"), ist2(rule->cookie_str, rule->cookie_len)))
2674 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002675 }
2676
Christopher Faulet99daf282018-11-28 22:58:13 +01002677 if (!htx_add_endof(htx, HTX_BLK_EOH) || !htx_add_endof(htx, HTX_BLK_EOM))
2678 goto fail;
2679
Kevin Zhu96b36392020-01-07 09:42:55 +01002680 htx_to_buf(htx, &res->buf);
Christopher Fauleta72a7e42020-01-28 09:28:11 +01002681 if (!http_forward_proxy_resp(s, 1))
2682 goto fail;
Christopher Faulet99daf282018-11-28 22:58:13 +01002683
Christopher Faulet60b33a52020-01-28 09:18:10 +01002684 if (rule->flags & REDIRECT_FLAG_FROM_REQ) {
2685 /* let's log the request time */
2686 s->logs.tv_request = now;
2687 req->analysers &= AN_REQ_FLT_END;
Christopher Faulet99daf282018-11-28 22:58:13 +01002688
Christopher Faulet60b33a52020-01-28 09:18:10 +01002689 if (s->sess->fe == s->be) /* report it if the request was intercepted by the frontend */
2690 _HA_ATOMIC_ADD(&s->sess->fe->fe_counters.intercepted_req, 1);
2691 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002692
2693 if (!(s->flags & SF_ERR_MASK))
2694 s->flags |= SF_ERR_LOCAL;
2695 if (!(s->flags & SF_FINST_MASK))
Christopher Faulet60b33a52020-01-28 09:18:10 +01002696 s->flags |= ((rule->flags & REDIRECT_FLAG_FROM_REQ) ? SF_FINST_R : SF_FINST_H);
Christopher Fauletf2824e62018-10-01 12:12:37 +02002697
Christopher Faulet99daf282018-11-28 22:58:13 +01002698 free_trash_chunk(chunk);
2699 return 1;
2700
2701 fail:
2702 /* If an error occurred, remove the incomplete HTTP response from the
2703 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01002704 channel_htx_truncate(res, htxbuf(&res->buf));
Christopher Fauletf2824e62018-10-01 12:12:37 +02002705 free_trash_chunk(chunk);
Christopher Faulet99daf282018-11-28 22:58:13 +01002706 return 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002707}
2708
Christopher Faulet92d34fe2019-12-17 09:20:34 +01002709/* Replace all headers matching the name <name>. The header value is replaced if
2710 * it matches the regex <re>. <str> is used for the replacement. If <full> is
2711 * set to 1, the full-line is matched and replaced. Otherwise, comma-separated
2712 * values are evaluated one by one. It returns 0 on success and -1 on error.
2713 */
2714int http_replace_hdrs(struct stream* s, struct htx *htx, struct ist name,
2715 const char *str, struct my_regex *re, int full)
Christopher Faulet72333522018-10-24 11:25:02 +02002716{
2717 struct http_hdr_ctx ctx;
2718 struct buffer *output = get_trash_chunk();
2719
Christopher Faulet72333522018-10-24 11:25:02 +02002720 ctx.blk = NULL;
Christopher Faulet92d34fe2019-12-17 09:20:34 +01002721 while (http_find_header(htx, name, &ctx, full)) {
Christopher Faulet72333522018-10-24 11:25:02 +02002722 if (!regex_exec_match2(re, ctx.value.ptr, ctx.value.len, MAX_MATCH, pmatch, 0))
2723 continue;
2724
2725 output->data = exp_replace(output->area, output->size, ctx.value.ptr, str, pmatch);
2726 if (output->data == -1)
2727 return -1;
2728 if (!http_replace_header_value(htx, &ctx, ist2(output->area, output->data)))
2729 return -1;
2730 }
2731 return 0;
2732}
2733
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002734/* This function executes one of the set-{method,path,query,uri} actions. It
2735 * takes the string from the variable 'replace' with length 'len', then modifies
2736 * the relevant part of the request line accordingly. Then it updates various
2737 * pointers to the next elements which were moved, and the total buffer length.
2738 * It finds the action to be performed in p[2], previously filled by function
2739 * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal
2740 * error, though this can be revisited when this code is finally exploited.
2741 *
2742 * 'action' can be '0' to replace method, '1' to replace path, '2' to replace
2743 * query string and 3 to replace uri.
2744 *
2745 * In query string case, the mark question '?' must be set at the start of the
2746 * string by the caller, event if the replacement query string is empty.
2747 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002748int http_req_replace_stline(int action, const char *replace, int len,
2749 struct proxy *px, struct stream *s)
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002750{
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002751 struct htx *htx = htxbuf(&s->req.buf);
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002752
2753 switch (action) {
2754 case 0: // method
2755 if (!http_replace_req_meth(htx, ist2(replace, len)))
2756 return -1;
2757 break;
2758
2759 case 1: // path
2760 if (!http_replace_req_path(htx, ist2(replace, len)))
2761 return -1;
2762 break;
2763
2764 case 2: // query
2765 if (!http_replace_req_query(htx, ist2(replace, len)))
2766 return -1;
2767 break;
2768
2769 case 3: // uri
2770 if (!http_replace_req_uri(htx, ist2(replace, len)))
2771 return -1;
2772 break;
2773
2774 default:
2775 return -1;
2776 }
2777 return 0;
2778}
2779
2780/* This function replace the HTTP status code and the associated message. The
Christopher Faulete00d06c2019-12-16 17:18:42 +01002781 * variable <status> contains the new status code. This function never fails. It
2782 * returns 0 in case of success, -1 in case of internal error.
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002783 */
Christopher Faulet96bff762019-12-17 13:46:18 +01002784int http_res_set_status(unsigned int status, struct ist reason, struct stream *s)
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002785{
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002786 struct htx *htx = htxbuf(&s->res.buf);
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002787 char *res;
2788
2789 chunk_reset(&trash);
2790 res = ultoa_o(status, trash.area, trash.size);
2791 trash.data = res - trash.area;
2792
2793 /* Do we have a custom reason format string? */
Tim Duesterhuse296d3e2020-03-05 17:56:31 +01002794 if (!isttest(reason)) {
Christopher Faulet96bff762019-12-17 13:46:18 +01002795 const char *str = http_get_reason(status);
2796 reason = ist2(str, strlen(str));
2797 }
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002798
Christopher Faulete00d06c2019-12-16 17:18:42 +01002799 if (!http_replace_res_status(htx, ist2(trash.area, trash.data)))
2800 return -1;
Christopher Faulet96bff762019-12-17 13:46:18 +01002801 if (!http_replace_res_reason(htx, reason))
Christopher Faulete00d06c2019-12-16 17:18:42 +01002802 return -1;
2803 return 0;
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002804}
2805
Christopher Faulet3e964192018-10-24 11:39:23 +02002806/* Executes the http-request rules <rules> for stream <s>, proxy <px> and
2807 * transaction <txn>. Returns the verdict of the first rule that prevents
2808 * further processing of the request (auth, deny, ...), and defaults to
2809 * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or
2810 * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT
2811 * on txn->flags if it encounters a tarpit rule. If <deny_status> is not NULL
2812 * and a deny/tarpit rule is matched, it will be filled with this rule's deny
2813 * status.
2814 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002815static enum rule_result http_req_get_intercept_rule(struct proxy *px, struct list *rules,
Christopher Fauletb58f62b2020-01-13 16:40:13 +01002816 struct stream *s)
Christopher Faulet3e964192018-10-24 11:39:23 +02002817{
2818 struct session *sess = strm_sess(s);
2819 struct http_txn *txn = s->txn;
2820 struct htx *htx;
Christopher Faulet3e964192018-10-24 11:39:23 +02002821 struct act_rule *rule;
2822 struct http_hdr_ctx ctx;
2823 const char *auth_realm;
Christopher Faulet3e964192018-10-24 11:39:23 +02002824 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002825 int act_opts = 0;
Christopher Faulet3e964192018-10-24 11:39:23 +02002826
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002827 htx = htxbuf(&s->req.buf);
Christopher Faulet3e964192018-10-24 11:39:23 +02002828
2829 /* If "the current_rule_list" match the executed rule list, we are in
2830 * resume condition. If a resume is needed it is always in the action
2831 * and never in the ACL or converters. In this case, we initialise the
2832 * current rule, and go to the action execution point.
2833 */
2834 if (s->current_rule) {
2835 rule = s->current_rule;
2836 s->current_rule = NULL;
2837 if (s->current_rule_list == rules)
2838 goto resume_execution;
2839 }
2840 s->current_rule_list = rules;
2841
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002842 /* start the ruleset evaluation in strict mode */
2843 txn->req.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01002844
Christopher Faulet3e964192018-10-24 11:39:23 +02002845 list_for_each_entry(rule, rules, list) {
2846 /* check optional condition */
2847 if (rule->cond) {
2848 int ret;
2849
2850 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
2851 ret = acl_pass(ret);
2852
2853 if (rule->cond->pol == ACL_COND_UNLESS)
2854 ret = !ret;
2855
2856 if (!ret) /* condition not matched */
2857 continue;
2858 }
2859
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002860 act_opts |= ACT_OPT_FIRST;
Christopher Faulet3e964192018-10-24 11:39:23 +02002861 resume_execution:
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002862 /* Always call the action function if defined */
2863 if (rule->action_ptr) {
2864 if ((s->req.flags & CF_READ_ERROR) ||
2865 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
2866 (px->options & PR_O_ABRT_CLOSE)))
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002867 act_opts |= ACT_OPT_FINAL;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002868
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002869 switch (rule->action_ptr(rule, px, sess, s, act_opts)) {
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002870 case ACT_RET_CONT:
2871 break;
2872 case ACT_RET_STOP:
2873 rule_ret = HTTP_RULE_RES_STOP;
2874 goto end;
2875 case ACT_RET_YIELD:
2876 s->current_rule = rule;
2877 rule_ret = HTTP_RULE_RES_YIELD;
2878 goto end;
2879 case ACT_RET_ERR:
2880 rule_ret = HTTP_RULE_RES_ERROR;
2881 goto end;
2882 case ACT_RET_DONE:
2883 rule_ret = HTTP_RULE_RES_DONE;
2884 goto end;
2885 case ACT_RET_DENY:
Christopher Fauletb58f62b2020-01-13 16:40:13 +01002886 txn->flags |= TX_CLDENY;
2887 if (txn->status == -1)
2888 txn->status = 403;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002889 rule_ret = HTTP_RULE_RES_DENY;
2890 goto end;
2891 case ACT_RET_ABRT:
2892 rule_ret = HTTP_RULE_RES_ABRT;
2893 goto end;
2894 case ACT_RET_INV:
2895 rule_ret = HTTP_RULE_RES_BADREQ;
2896 goto end;
2897 }
2898 continue; /* eval the next rule */
2899 }
2900
2901 /* If not action function defined, check for known actions */
Christopher Faulet3e964192018-10-24 11:39:23 +02002902 switch (rule->action) {
2903 case ACT_ACTION_ALLOW:
2904 rule_ret = HTTP_RULE_RES_STOP;
2905 goto end;
2906
2907 case ACT_ACTION_DENY:
Christopher Fauletb58f62b2020-01-13 16:40:13 +01002908 txn->flags |= TX_CLDENY;
Christopher Faulet554c0eb2020-01-14 12:00:28 +01002909 txn->status = rule->arg.http_deny.status;
2910 if (rule->arg.http_deny.errmsg)
2911 txn->errmsg = rule->arg.http_deny.errmsg;
Christopher Faulet3e964192018-10-24 11:39:23 +02002912 rule_ret = HTTP_RULE_RES_DENY;
2913 goto end;
2914
2915 case ACT_HTTP_REQ_TARPIT:
2916 txn->flags |= TX_CLTARPIT;
Christopher Faulet554c0eb2020-01-14 12:00:28 +01002917 txn->status = rule->arg.http_deny.status;
2918 if (rule->arg.http_deny.errmsg)
2919 txn->errmsg = rule->arg.http_deny.errmsg;
Christopher Faulet3e964192018-10-24 11:39:23 +02002920 rule_ret = HTTP_RULE_RES_DENY;
2921 goto end;
2922
2923 case ACT_HTTP_REQ_AUTH:
Christopher Faulet3e964192018-10-24 11:39:23 +02002924 /* Auth might be performed on regular http-req rules as well as on stats */
Christopher Faulet96bff762019-12-17 13:46:18 +01002925 auth_realm = rule->arg.http.str.ptr;
Christopher Faulet3e964192018-10-24 11:39:23 +02002926 if (!auth_realm) {
2927 if (px->uri_auth && rules == &px->uri_auth->http_req_rules)
2928 auth_realm = STATS_DEFAULT_REALM;
2929 else
2930 auth_realm = px->id;
2931 }
2932 /* send 401/407 depending on whether we use a proxy or not. We still
2933 * count one error, because normal browsing won't significantly
2934 * increase the counter but brute force attempts will.
2935 */
Christopher Faulet3e964192018-10-24 11:39:23 +02002936 rule_ret = HTTP_RULE_RES_ABRT;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002937 if (http_reply_40x_unauthorized(s, auth_realm) == -1)
Christopher Faulet3a26bee2019-12-16 12:47:40 +01002938 rule_ret = HTTP_RULE_RES_ERROR;
Christopher Faulet12c51e22018-11-28 15:59:42 +01002939 stream_inc_http_err_ctr(s);
Christopher Faulet3e964192018-10-24 11:39:23 +02002940 goto end;
2941
2942 case ACT_HTTP_REDIR:
Christopher Faulet90d22a82020-03-06 11:18:39 +01002943 rule_ret = HTTP_RULE_RES_ABRT;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002944 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
Christopher Faulet3a26bee2019-12-16 12:47:40 +01002945 rule_ret = HTTP_RULE_RES_ERROR;
Christopher Faulet3e964192018-10-24 11:39:23 +02002946 goto end;
2947
2948 case ACT_HTTP_SET_NICE:
Christopher Faulet96bff762019-12-17 13:46:18 +01002949 s->task->nice = rule->arg.http.i;
Christopher Faulet3e964192018-10-24 11:39:23 +02002950 break;
2951
2952 case ACT_HTTP_SET_TOS:
Christopher Faulet96bff762019-12-17 13:46:18 +01002953 conn_set_tos(objt_conn(sess->origin), rule->arg.http.i);
Christopher Faulet3e964192018-10-24 11:39:23 +02002954 break;
2955
2956 case ACT_HTTP_SET_MARK:
Christopher Faulet96bff762019-12-17 13:46:18 +01002957 conn_set_mark(objt_conn(sess->origin), rule->arg.http.i);
Christopher Faulet3e964192018-10-24 11:39:23 +02002958 break;
2959
2960 case ACT_HTTP_SET_LOGL:
Christopher Faulet96bff762019-12-17 13:46:18 +01002961 s->logs.level = rule->arg.http.i;
Christopher Faulet3e964192018-10-24 11:39:23 +02002962 break;
2963
Christopher Faulet3e964192018-10-24 11:39:23 +02002964 case ACT_HTTP_DEL_HDR:
2965 /* remove all occurrences of the header */
2966 ctx.blk = NULL;
Christopher Faulet96bff762019-12-17 13:46:18 +01002967 while (http_find_header(htx, rule->arg.http.str, &ctx, 1))
Christopher Faulet3e964192018-10-24 11:39:23 +02002968 http_remove_header(htx, &ctx);
2969 break;
2970
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002971 /* other flags exists, but normally, they never be matched. */
Christopher Faulet3e964192018-10-24 11:39:23 +02002972 default:
2973 break;
2974 }
2975 }
2976
2977 end:
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002978 /* if the ruleset evaluation is finished reset the strict mode */
Christopher Faulet46f95542019-12-20 10:07:22 +01002979 if (rule_ret != HTTP_RULE_RES_YIELD)
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002980 txn->req.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01002981
Christopher Faulet3e964192018-10-24 11:39:23 +02002982 /* we reached the end of the rules, nothing to report */
2983 return rule_ret;
2984}
2985
2986/* Executes the http-response rules <rules> for stream <s> and proxy <px>. It
2987 * returns one of 5 possible statuses: HTTP_RULE_RES_CONT, HTTP_RULE_RES_STOP,
2988 * HTTP_RULE_RES_DONE, HTTP_RULE_RES_YIELD, or HTTP_RULE_RES_BADREQ. If *CONT
2989 * is returned, the process can continue the evaluation of next rule list. If
2990 * *STOP or *DONE is returned, the process must stop the evaluation. If *BADREQ
2991 * is returned, it means the operation could not be processed and a server error
2992 * must be returned. It may set the TX_SVDENY on txn->flags if it encounters a
2993 * deny rule. If *YIELD is returned, the caller must call again the function
2994 * with the same context.
2995 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002996static enum rule_result http_res_get_intercept_rule(struct proxy *px, struct list *rules,
2997 struct stream *s)
Christopher Faulet3e964192018-10-24 11:39:23 +02002998{
2999 struct session *sess = strm_sess(s);
3000 struct http_txn *txn = s->txn;
3001 struct htx *htx;
Christopher Faulet3e964192018-10-24 11:39:23 +02003002 struct act_rule *rule;
3003 struct http_hdr_ctx ctx;
3004 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
Christopher Faulet105ba6c2019-12-18 14:41:51 +01003005 int act_opts = 0;
Christopher Faulet3e964192018-10-24 11:39:23 +02003006
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003007 htx = htxbuf(&s->res.buf);
Christopher Faulet3e964192018-10-24 11:39:23 +02003008
3009 /* If "the current_rule_list" match the executed rule list, we are in
3010 * resume condition. If a resume is needed it is always in the action
3011 * and never in the ACL or converters. In this case, we initialise the
3012 * current rule, and go to the action execution point.
3013 */
3014 if (s->current_rule) {
3015 rule = s->current_rule;
3016 s->current_rule = NULL;
3017 if (s->current_rule_list == rules)
3018 goto resume_execution;
3019 }
3020 s->current_rule_list = rules;
3021
Christopher Faulet1aea50e2020-01-17 16:03:53 +01003022 /* start the ruleset evaluation in strict mode */
3023 txn->rsp.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01003024
Christopher Faulet3e964192018-10-24 11:39:23 +02003025 list_for_each_entry(rule, rules, list) {
3026 /* check optional condition */
3027 if (rule->cond) {
3028 int ret;
3029
3030 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
3031 ret = acl_pass(ret);
3032
3033 if (rule->cond->pol == ACL_COND_UNLESS)
3034 ret = !ret;
3035
3036 if (!ret) /* condition not matched */
3037 continue;
3038 }
3039
Christopher Faulet105ba6c2019-12-18 14:41:51 +01003040 act_opts |= ACT_OPT_FIRST;
Christopher Faulet3e964192018-10-24 11:39:23 +02003041resume_execution:
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01003042
3043 /* Always call the action function if defined */
3044 if (rule->action_ptr) {
3045 if ((s->req.flags & CF_READ_ERROR) ||
3046 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
3047 (px->options & PR_O_ABRT_CLOSE)))
Christopher Faulet105ba6c2019-12-18 14:41:51 +01003048 act_opts |= ACT_OPT_FINAL;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01003049
Christopher Faulet105ba6c2019-12-18 14:41:51 +01003050 switch (rule->action_ptr(rule, px, sess, s, act_opts)) {
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01003051 case ACT_RET_CONT:
3052 break;
3053 case ACT_RET_STOP:
3054 rule_ret = HTTP_RULE_RES_STOP;
3055 goto end;
3056 case ACT_RET_YIELD:
3057 s->current_rule = rule;
3058 rule_ret = HTTP_RULE_RES_YIELD;
3059 goto end;
3060 case ACT_RET_ERR:
3061 rule_ret = HTTP_RULE_RES_ERROR;
3062 goto end;
3063 case ACT_RET_DONE:
3064 rule_ret = HTTP_RULE_RES_DONE;
3065 goto end;
3066 case ACT_RET_DENY:
Christopher Fauletb58f62b2020-01-13 16:40:13 +01003067 txn->flags |= TX_CLDENY;
3068 if (txn->status == -1)
3069 txn->status = 502;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01003070 rule_ret = HTTP_RULE_RES_DENY;
3071 goto end;
3072 case ACT_RET_ABRT:
3073 rule_ret = HTTP_RULE_RES_ABRT;
3074 goto end;
3075 case ACT_RET_INV:
3076 rule_ret = HTTP_RULE_RES_BADREQ;
3077 goto end;
3078 }
3079 continue; /* eval the next rule */
3080 }
3081
3082 /* If not action function defined, check for known actions */
Christopher Faulet3e964192018-10-24 11:39:23 +02003083 switch (rule->action) {
3084 case ACT_ACTION_ALLOW:
3085 rule_ret = HTTP_RULE_RES_STOP; /* "allow" rules are OK */
3086 goto end;
3087
3088 case ACT_ACTION_DENY:
Christopher Fauletb58f62b2020-01-13 16:40:13 +01003089 txn->flags |= TX_CLDENY;
Christopher Faulet554c0eb2020-01-14 12:00:28 +01003090 txn->status = rule->arg.http_deny.status;
3091 if (rule->arg.http_deny.errmsg)
3092 txn->errmsg = rule->arg.http_deny.errmsg;
Christopher Faulet3a26bee2019-12-16 12:47:40 +01003093 rule_ret = HTTP_RULE_RES_DENY;
Christopher Faulet3e964192018-10-24 11:39:23 +02003094 goto end;
3095
3096 case ACT_HTTP_SET_NICE:
Christopher Faulet96bff762019-12-17 13:46:18 +01003097 s->task->nice = rule->arg.http.i;
Christopher Faulet3e964192018-10-24 11:39:23 +02003098 break;
3099
3100 case ACT_HTTP_SET_TOS:
Christopher Faulet96bff762019-12-17 13:46:18 +01003101 conn_set_tos(objt_conn(sess->origin), rule->arg.http.i);
Christopher Faulet3e964192018-10-24 11:39:23 +02003102 break;
3103
3104 case ACT_HTTP_SET_MARK:
Christopher Faulet96bff762019-12-17 13:46:18 +01003105 conn_set_mark(objt_conn(sess->origin), rule->arg.http.i);
Christopher Faulet3e964192018-10-24 11:39:23 +02003106 break;
3107
3108 case ACT_HTTP_SET_LOGL:
Christopher Faulet96bff762019-12-17 13:46:18 +01003109 s->logs.level = rule->arg.http.i;
Christopher Faulet3e964192018-10-24 11:39:23 +02003110 break;
3111
Christopher Faulet3e964192018-10-24 11:39:23 +02003112 case ACT_HTTP_DEL_HDR:
3113 /* remove all occurrences of the header */
3114 ctx.blk = NULL;
Christopher Faulet96bff762019-12-17 13:46:18 +01003115 while (http_find_header(htx, rule->arg.http.str, &ctx, 1))
Christopher Faulet3e964192018-10-24 11:39:23 +02003116 http_remove_header(htx, &ctx);
3117 break;
3118
Christopher Faulet3e964192018-10-24 11:39:23 +02003119 case ACT_HTTP_REDIR:
Christopher Faulet49c2a702020-03-06 15:44:37 +01003120 rule_ret = HTTP_RULE_RES_ABRT;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003121 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
Christopher Faulet3a26bee2019-12-16 12:47:40 +01003122 rule_ret = HTTP_RULE_RES_ERROR;
Christopher Faulet3e964192018-10-24 11:39:23 +02003123 goto end;
3124
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01003125 /* other flags exists, but normally, they never be matched. */
Christopher Faulet3e964192018-10-24 11:39:23 +02003126 default:
3127 break;
3128 }
3129 }
3130
3131 end:
Christopher Faulet1aea50e2020-01-17 16:03:53 +01003132 /* if the ruleset evaluation is finished reset the strict mode */
Christopher Faulet46f95542019-12-20 10:07:22 +01003133 if (rule_ret != HTTP_RULE_RES_YIELD)
Christopher Faulet1aea50e2020-01-17 16:03:53 +01003134 txn->rsp.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01003135
Christopher Faulet3e964192018-10-24 11:39:23 +02003136 /* we reached the end of the rules, nothing to report */
3137 return rule_ret;
3138}
3139
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01003140/* Executes backend and frontend http-after-response rules for the stream <s>,
3141 * in that order. it return 1 on success and 0 on error. It is the caller
3142 * responsibility to catch error or ignore it. If it catches it, this function
3143 * may be called a second time, for the internal error.
3144 */
3145int http_eval_after_res_rules(struct stream *s)
3146{
3147 struct session *sess = s->sess;
3148 enum rule_result ret = HTTP_RULE_RES_CONT;
3149
3150 /* prune the request variables if not already done and swap to the response variables. */
3151 if (s->vars_reqres.scope != SCOPE_RES) {
3152 if (!LIST_ISEMPTY(&s->vars_reqres.head))
3153 vars_prune(&s->vars_reqres, s->sess, s);
3154 vars_init(&s->vars_reqres, SCOPE_RES);
3155 }
3156
3157 ret = http_res_get_intercept_rule(s->be, &s->be->http_after_res_rules, s);
3158 if ((ret == HTTP_RULE_RES_CONT || ret == HTTP_RULE_RES_STOP) && sess->fe != s->be)
3159 ret = http_res_get_intercept_rule(sess->fe, &sess->fe->http_after_res_rules, s);
3160
3161 /* All other codes than CONTINUE, STOP or DONE are forbidden */
3162 return (ret == HTTP_RULE_RES_CONT || ret == HTTP_RULE_RES_STOP || ret == HTTP_RULE_RES_DONE);
3163}
3164
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003165/*
3166 * Manage client-side cookie. It can impact performance by about 2% so it is
3167 * desirable to call it only when needed. This code is quite complex because
3168 * of the multiple very crappy and ambiguous syntaxes we have to support. it
3169 * highly recommended not to touch this part without a good reason !
3170 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003171static void http_manage_client_side_cookies(struct stream *s, struct channel *req)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003172{
3173 struct session *sess = s->sess;
3174 struct http_txn *txn = s->txn;
3175 struct htx *htx;
3176 struct http_hdr_ctx ctx;
3177 char *hdr_beg, *hdr_end, *del_from;
3178 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
3179 int preserve_hdr;
3180
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003181 htx = htxbuf(&req->buf);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003182 ctx.blk = NULL;
3183 while (http_find_header(htx, ist("Cookie"), &ctx, 1)) {
Olivier Houchardf0f42382019-07-22 17:43:46 +02003184 int is_first = 1;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003185 del_from = NULL; /* nothing to be deleted */
3186 preserve_hdr = 0; /* assume we may kill the whole header */
3187
3188 /* Now look for cookies. Conforming to RFC2109, we have to support
3189 * attributes whose name begin with a '$', and associate them with
3190 * the right cookie, if we want to delete this cookie.
3191 * So there are 3 cases for each cookie read :
3192 * 1) it's a special attribute, beginning with a '$' : ignore it.
3193 * 2) it's a server id cookie that we *MAY* want to delete : save
3194 * some pointers on it (last semi-colon, beginning of cookie...)
3195 * 3) it's an application cookie : we *MAY* have to delete a previous
3196 * "special" cookie.
3197 * At the end of loop, if a "special" cookie remains, we may have to
3198 * remove it. If no application cookie persists in the header, we
3199 * *MUST* delete it.
3200 *
3201 * Note: RFC2965 is unclear about the processing of spaces around
3202 * the equal sign in the ATTR=VALUE form. A careful inspection of
3203 * the RFC explicitly allows spaces before it, and not within the
3204 * tokens (attrs or values). An inspection of RFC2109 allows that
3205 * too but section 10.1.3 lets one think that spaces may be allowed
3206 * after the equal sign too, resulting in some (rare) buggy
3207 * implementations trying to do that. So let's do what servers do.
3208 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
3209 * allowed quoted strings in values, with any possible character
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003210 * after a backslash, including control chars and delimiters, which
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003211 * causes parsing to become ambiguous. Browsers also allow spaces
3212 * within values even without quotes.
3213 *
3214 * We have to keep multiple pointers in order to support cookie
3215 * removal at the beginning, middle or end of header without
3216 * corrupting the header. All of these headers are valid :
3217 *
3218 * hdr_beg hdr_end
3219 * | |
3220 * v |
3221 * NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3 |
3222 * NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3 v
3223 * NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3
3224 * | | | | | | |
3225 * | | | | | | |
3226 * | | | | | | +--> next
3227 * | | | | | +----> val_end
3228 * | | | | +-----------> val_beg
3229 * | | | +--------------> equal
3230 * | | +----------------> att_end
3231 * | +---------------------> att_beg
3232 * +--------------------------> prev
3233 *
3234 */
3235 hdr_beg = ctx.value.ptr;
3236 hdr_end = hdr_beg + ctx.value.len;
3237 for (prev = hdr_beg; prev < hdr_end; prev = next) {
3238 /* Iterate through all cookies on this line */
3239
3240 /* find att_beg */
3241 att_beg = prev;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003242 if (!is_first)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003243 att_beg++;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003244 is_first = 0;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003245
3246 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
3247 att_beg++;
3248
3249 /* find att_end : this is the first character after the last non
3250 * space before the equal. It may be equal to hdr_end.
3251 */
3252 equal = att_end = att_beg;
3253 while (equal < hdr_end) {
3254 if (*equal == '=' || *equal == ',' || *equal == ';')
3255 break;
3256 if (HTTP_IS_SPHT(*equal++))
3257 continue;
3258 att_end = equal;
3259 }
3260
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003261 /* here, <equal> points to '=', a delimiter or the end. <att_end>
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003262 * is between <att_beg> and <equal>, both may be identical.
3263 */
3264 /* look for end of cookie if there is an equal sign */
3265 if (equal < hdr_end && *equal == '=') {
3266 /* look for the beginning of the value */
3267 val_beg = equal + 1;
3268 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
3269 val_beg++;
3270
3271 /* find the end of the value, respecting quotes */
3272 next = http_find_cookie_value_end(val_beg, hdr_end);
3273
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003274 /* make val_end point to the first white space or delimiter after the value */
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003275 val_end = next;
3276 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
3277 val_end--;
3278 }
3279 else
3280 val_beg = val_end = next = equal;
3281
3282 /* We have nothing to do with attributes beginning with
3283 * '$'. However, they will automatically be removed if a
3284 * header before them is removed, since they're supposed
3285 * to be linked together.
3286 */
3287 if (*att_beg == '$')
3288 continue;
3289
3290 /* Ignore cookies with no equal sign */
3291 if (equal == next) {
3292 /* This is not our cookie, so we must preserve it. But if we already
3293 * scheduled another cookie for removal, we cannot remove the
3294 * complete header, but we can remove the previous block itself.
3295 */
3296 preserve_hdr = 1;
3297 if (del_from != NULL) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003298 int delta = http_del_hdr_value(hdr_beg, hdr_end, &del_from, prev);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003299 val_end += delta;
3300 next += delta;
3301 hdr_end += delta;
3302 prev = del_from;
3303 del_from = NULL;
3304 }
3305 continue;
3306 }
3307
3308 /* if there are spaces around the equal sign, we need to
3309 * strip them otherwise we'll get trouble for cookie captures,
3310 * or even for rewrites. Since this happens extremely rarely,
3311 * it does not hurt performance.
3312 */
3313 if (unlikely(att_end != equal || val_beg > equal + 1)) {
3314 int stripped_before = 0;
3315 int stripped_after = 0;
3316
3317 if (att_end != equal) {
3318 memmove(att_end, equal, hdr_end - equal);
3319 stripped_before = (att_end - equal);
3320 equal += stripped_before;
3321 val_beg += stripped_before;
3322 }
3323
3324 if (val_beg > equal + 1) {
3325 memmove(equal + 1, val_beg, hdr_end + stripped_before - val_beg);
3326 stripped_after = (equal + 1) - val_beg;
3327 val_beg += stripped_after;
3328 stripped_before += stripped_after;
3329 }
3330
3331 val_end += stripped_before;
3332 next += stripped_before;
3333 hdr_end += stripped_before;
3334 }
3335 /* now everything is as on the diagram above */
3336
3337 /* First, let's see if we want to capture this cookie. We check
3338 * that we don't already have a client side cookie, because we
3339 * can only capture one. Also as an optimisation, we ignore
3340 * cookies shorter than the declared name.
3341 */
3342 if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL &&
3343 (val_end - att_beg >= sess->fe->capture_namelen) &&
3344 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
3345 int log_len = val_end - att_beg;
3346
3347 if ((txn->cli_cookie = pool_alloc(pool_head_capture)) == NULL) {
3348 ha_alert("HTTP logging : out of memory.\n");
3349 } else {
3350 if (log_len > sess->fe->capture_len)
3351 log_len = sess->fe->capture_len;
3352 memcpy(txn->cli_cookie, att_beg, log_len);
3353 txn->cli_cookie[log_len] = 0;
3354 }
3355 }
3356
3357 /* Persistence cookies in passive, rewrite or insert mode have the
3358 * following form :
3359 *
3360 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
3361 *
3362 * For cookies in prefix mode, the form is :
3363 *
3364 * Cookie: NAME=SRV~VALUE
3365 */
3366 if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
3367 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
3368 struct server *srv = s->be->srv;
3369 char *delim;
3370
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003371 /* if we're in cookie prefix mode, we'll search the delimiter so that we
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003372 * have the server ID between val_beg and delim, and the original cookie between
3373 * delim+1 and val_end. Otherwise, delim==val_end :
3374 *
3375 * hdr_beg
3376 * |
3377 * v
3378 * NAME=SRV; # in all but prefix modes
3379 * NAME=SRV~OPAQUE ; # in prefix mode
3380 * || || | |+-> next
3381 * || || | +--> val_end
3382 * || || +---------> delim
3383 * || |+------------> val_beg
3384 * || +-------------> att_end = equal
3385 * |+-----------------> att_beg
3386 * +------------------> prev
3387 *
3388 */
3389 if (s->be->ck_opts & PR_CK_PFX) {
3390 for (delim = val_beg; delim < val_end; delim++)
3391 if (*delim == COOKIE_DELIM)
3392 break;
3393 }
3394 else {
3395 char *vbar1;
3396 delim = val_end;
3397 /* Now check if the cookie contains a date field, which would
3398 * appear after a vertical bar ('|') just after the server name
3399 * and before the delimiter.
3400 */
3401 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
3402 if (vbar1) {
3403 /* OK, so left of the bar is the server's cookie and
3404 * right is the last seen date. It is a base64 encoded
3405 * 30-bit value representing the UNIX date since the
3406 * epoch in 4-second quantities.
3407 */
3408 int val;
3409 delim = vbar1++;
3410 if (val_end - vbar1 >= 5) {
3411 val = b64tos30(vbar1);
3412 if (val > 0)
3413 txn->cookie_last_date = val << 2;
3414 }
3415 /* look for a second vertical bar */
3416 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
3417 if (vbar1 && (val_end - vbar1 > 5)) {
3418 val = b64tos30(vbar1 + 1);
3419 if (val > 0)
3420 txn->cookie_first_date = val << 2;
3421 }
3422 }
3423 }
3424
3425 /* if the cookie has an expiration date and the proxy wants to check
3426 * it, then we do that now. We first check if the cookie is too old,
3427 * then only if it has expired. We detect strict overflow because the
3428 * time resolution here is not great (4 seconds). Cookies with dates
3429 * in the future are ignored if their offset is beyond one day. This
3430 * allows an admin to fix timezone issues without expiring everyone
3431 * and at the same time avoids keeping unwanted side effects for too
3432 * long.
3433 */
3434 if (txn->cookie_first_date && s->be->cookie_maxlife &&
3435 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) ||
3436 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
3437 txn->flags &= ~TX_CK_MASK;
3438 txn->flags |= TX_CK_OLD;
3439 delim = val_beg; // let's pretend we have not found the cookie
3440 txn->cookie_first_date = 0;
3441 txn->cookie_last_date = 0;
3442 }
3443 else if (txn->cookie_last_date && s->be->cookie_maxidle &&
3444 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) ||
3445 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
3446 txn->flags &= ~TX_CK_MASK;
3447 txn->flags |= TX_CK_EXPIRED;
3448 delim = val_beg; // let's pretend we have not found the cookie
3449 txn->cookie_first_date = 0;
3450 txn->cookie_last_date = 0;
3451 }
3452
3453 /* Here, we'll look for the first running server which supports the cookie.
3454 * This allows to share a same cookie between several servers, for example
3455 * to dedicate backup servers to specific servers only.
3456 * However, to prevent clients from sticking to cookie-less backup server
3457 * when they have incidentely learned an empty cookie, we simply ignore
3458 * empty cookies and mark them as invalid.
3459 * The same behaviour is applied when persistence must be ignored.
3460 */
3461 if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
3462 srv = NULL;
3463
3464 while (srv) {
3465 if (srv->cookie && (srv->cklen == delim - val_beg) &&
3466 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
3467 if ((srv->cur_state != SRV_ST_STOPPED) ||
3468 (s->be->options & PR_O_PERSIST) ||
3469 (s->flags & SF_FORCE_PRST)) {
3470 /* we found the server and we can use it */
3471 txn->flags &= ~TX_CK_MASK;
3472 txn->flags |= (srv->cur_state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
3473 s->flags |= SF_DIRECT | SF_ASSIGNED;
3474 s->target = &srv->obj_type;
3475 break;
3476 } else {
3477 /* we found a server, but it's down,
3478 * mark it as such and go on in case
3479 * another one is available.
3480 */
3481 txn->flags &= ~TX_CK_MASK;
3482 txn->flags |= TX_CK_DOWN;
3483 }
3484 }
3485 srv = srv->next;
3486 }
3487
3488 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
3489 /* no server matched this cookie or we deliberately skipped it */
3490 txn->flags &= ~TX_CK_MASK;
3491 if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
3492 txn->flags |= TX_CK_UNUSED;
3493 else
3494 txn->flags |= TX_CK_INVALID;
3495 }
3496
3497 /* depending on the cookie mode, we may have to either :
3498 * - delete the complete cookie if we're in insert+indirect mode, so that
3499 * the server never sees it ;
3500 * - remove the server id from the cookie value, and tag the cookie as an
Joseph Herlante9d5c722018-11-25 11:00:25 -08003501 * application cookie so that it does not get accidentally removed later,
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003502 * if we're in cookie prefix mode
3503 */
3504 if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
3505 int delta; /* negative */
3506
3507 memmove(val_beg, delim + 1, hdr_end - (delim + 1));
3508 delta = val_beg - (delim + 1);
3509 val_end += delta;
3510 next += delta;
3511 hdr_end += delta;
3512 del_from = NULL;
3513 preserve_hdr = 1; /* we want to keep this cookie */
3514 }
3515 else if (del_from == NULL &&
3516 (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
3517 del_from = prev;
3518 }
3519 }
3520 else {
3521 /* This is not our cookie, so we must preserve it. But if we already
3522 * scheduled another cookie for removal, we cannot remove the
3523 * complete header, but we can remove the previous block itself.
3524 */
3525 preserve_hdr = 1;
3526
3527 if (del_from != NULL) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003528 int delta = http_del_hdr_value(hdr_beg, hdr_end, &del_from, prev);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003529 if (att_beg >= del_from)
3530 att_beg += delta;
3531 if (att_end >= del_from)
3532 att_end += delta;
3533 val_beg += delta;
3534 val_end += delta;
3535 next += delta;
3536 hdr_end += delta;
3537 prev = del_from;
3538 del_from = NULL;
3539 }
3540 }
3541
3542 /* continue with next cookie on this header line */
3543 att_beg = next;
3544 } /* for each cookie */
3545
3546
3547 /* There are no more cookies on this line.
3548 * We may still have one (or several) marked for deletion at the
3549 * end of the line. We must do this now in two ways :
3550 * - if some cookies must be preserved, we only delete from the
3551 * mark to the end of line ;
3552 * - if nothing needs to be preserved, simply delete the whole header
3553 */
3554 if (del_from) {
3555 hdr_end = (preserve_hdr ? del_from : hdr_beg);
3556 }
3557 if ((hdr_end - hdr_beg) != ctx.value.len) {
Christopher Faulet3e2638e2019-06-18 09:49:16 +02003558 if (hdr_beg != hdr_end)
3559 htx_change_blk_value_len(htx, ctx.blk, hdr_end - hdr_beg);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003560 else
3561 http_remove_header(htx, &ctx);
3562 }
3563 } /* for each "Cookie header */
3564}
3565
3566/*
3567 * Manage server-side cookies. It can impact performance by about 2% so it is
3568 * desirable to call it only when needed. This function is also used when we
3569 * just need to know if there is a cookie (eg: for check-cache).
3570 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003571static void http_manage_server_side_cookies(struct stream *s, struct channel *res)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003572{
3573 struct session *sess = s->sess;
3574 struct http_txn *txn = s->txn;
3575 struct htx *htx;
3576 struct http_hdr_ctx ctx;
3577 struct server *srv;
3578 char *hdr_beg, *hdr_end;
3579 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau6f7a02a2019-04-15 21:49:49 +02003580 int is_cookie2 = 0;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003581
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003582 htx = htxbuf(&res->buf);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003583
3584 ctx.blk = NULL;
3585 while (1) {
Olivier Houchardf0f42382019-07-22 17:43:46 +02003586 int is_first = 1;
3587
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003588 if (!http_find_header(htx, ist("Set-Cookie"), &ctx, 1)) {
3589 if (!http_find_header(htx, ist("Set-Cookie2"), &ctx, 1))
3590 break;
3591 is_cookie2 = 1;
3592 }
3593
3594 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
3595 * <prev> points to the colon.
3596 */
3597 txn->flags |= TX_SCK_PRESENT;
3598
3599 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
3600 * check-cache is enabled) and we are not interested in checking
3601 * them. Warning, the cookie capture is declared in the frontend.
3602 */
3603 if (s->be->cookie_name == NULL && sess->fe->capture_name == NULL)
3604 break;
3605
3606 /* OK so now we know we have to process this response cookie.
3607 * The format of the Set-Cookie header is slightly different
3608 * from the format of the Cookie header in that it does not
3609 * support the comma as a cookie delimiter (thus the header
3610 * cannot be folded) because the Expires attribute described in
3611 * the original Netscape's spec may contain an unquoted date
3612 * with a comma inside. We have to live with this because
3613 * many browsers don't support Max-Age and some browsers don't
3614 * support quoted strings. However the Set-Cookie2 header is
3615 * clean.
3616 *
3617 * We have to keep multiple pointers in order to support cookie
3618 * removal at the beginning, middle or end of header without
3619 * corrupting the header (in case of set-cookie2). A special
3620 * pointer, <scav> points to the beginning of the set-cookie-av
3621 * fields after the first semi-colon. The <next> pointer points
3622 * either to the end of line (set-cookie) or next unquoted comma
3623 * (set-cookie2). All of these headers are valid :
3624 *
3625 * hdr_beg hdr_end
3626 * | |
3627 * v |
3628 * NAME1 = VALUE 1 ; Secure; Path="/" |
3629 * NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT v
3630 * NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT
3631 * NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard
3632 * | | | | | | | |
3633 * | | | | | | | +-> next
3634 * | | | | | | +------------> scav
3635 * | | | | | +--------------> val_end
3636 * | | | | +--------------------> val_beg
3637 * | | | +----------------------> equal
3638 * | | +------------------------> att_end
3639 * | +----------------------------> att_beg
3640 * +------------------------------> prev
3641 * -------------------------------> hdr_beg
3642 */
3643 hdr_beg = ctx.value.ptr;
3644 hdr_end = hdr_beg + ctx.value.len;
3645 for (prev = hdr_beg; prev < hdr_end; prev = next) {
3646
3647 /* Iterate through all cookies on this line */
3648
3649 /* find att_beg */
3650 att_beg = prev;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003651 if (!is_first)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003652 att_beg++;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003653 is_first = 0;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003654
3655 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
3656 att_beg++;
3657
3658 /* find att_end : this is the first character after the last non
3659 * space before the equal. It may be equal to hdr_end.
3660 */
3661 equal = att_end = att_beg;
3662
3663 while (equal < hdr_end) {
3664 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
3665 break;
3666 if (HTTP_IS_SPHT(*equal++))
3667 continue;
3668 att_end = equal;
3669 }
3670
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003671 /* here, <equal> points to '=', a delimiter or the end. <att_end>
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003672 * is between <att_beg> and <equal>, both may be identical.
3673 */
3674
3675 /* look for end of cookie if there is an equal sign */
3676 if (equal < hdr_end && *equal == '=') {
3677 /* look for the beginning of the value */
3678 val_beg = equal + 1;
3679 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
3680 val_beg++;
3681
3682 /* find the end of the value, respecting quotes */
3683 next = http_find_cookie_value_end(val_beg, hdr_end);
3684
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003685 /* make val_end point to the first white space or delimiter after the value */
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003686 val_end = next;
3687 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
3688 val_end--;
3689 }
3690 else {
3691 /* <equal> points to next comma, semi-colon or EOL */
3692 val_beg = val_end = next = equal;
3693 }
3694
3695 if (next < hdr_end) {
3696 /* Set-Cookie2 supports multiple cookies, and <next> points to
3697 * a colon or semi-colon before the end. So skip all attr-value
3698 * pairs and look for the next comma. For Set-Cookie, since
3699 * commas are permitted in values, skip to the end.
3700 */
3701 if (is_cookie2)
3702 next = http_find_hdr_value_end(next, hdr_end);
3703 else
3704 next = hdr_end;
3705 }
3706
3707 /* Now everything is as on the diagram above */
3708
3709 /* Ignore cookies with no equal sign */
3710 if (equal == val_end)
3711 continue;
3712
3713 /* If there are spaces around the equal sign, we need to
3714 * strip them otherwise we'll get trouble for cookie captures,
3715 * or even for rewrites. Since this happens extremely rarely,
3716 * it does not hurt performance.
3717 */
3718 if (unlikely(att_end != equal || val_beg > equal + 1)) {
3719 int stripped_before = 0;
3720 int stripped_after = 0;
3721
3722 if (att_end != equal) {
3723 memmove(att_end, equal, hdr_end - equal);
3724 stripped_before = (att_end - equal);
3725 equal += stripped_before;
3726 val_beg += stripped_before;
3727 }
3728
3729 if (val_beg > equal + 1) {
3730 memmove(equal + 1, val_beg, hdr_end + stripped_before - val_beg);
3731 stripped_after = (equal + 1) - val_beg;
3732 val_beg += stripped_after;
3733 stripped_before += stripped_after;
3734 }
3735
3736 val_end += stripped_before;
3737 next += stripped_before;
3738 hdr_end += stripped_before;
3739
Christopher Faulet3e2638e2019-06-18 09:49:16 +02003740 htx_change_blk_value_len(htx, ctx.blk, hdr_end - hdr_beg);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003741 ctx.value.len = hdr_end - hdr_beg;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003742 }
3743
3744 /* First, let's see if we want to capture this cookie. We check
3745 * that we don't already have a server side cookie, because we
3746 * can only capture one. Also as an optimisation, we ignore
3747 * cookies shorter than the declared name.
3748 */
3749 if (sess->fe->capture_name != NULL &&
3750 txn->srv_cookie == NULL &&
3751 (val_end - att_beg >= sess->fe->capture_namelen) &&
3752 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
3753 int log_len = val_end - att_beg;
3754 if ((txn->srv_cookie = pool_alloc(pool_head_capture)) == NULL) {
3755 ha_alert("HTTP logging : out of memory.\n");
3756 }
3757 else {
3758 if (log_len > sess->fe->capture_len)
3759 log_len = sess->fe->capture_len;
3760 memcpy(txn->srv_cookie, att_beg, log_len);
3761 txn->srv_cookie[log_len] = 0;
3762 }
3763 }
3764
3765 srv = objt_server(s->target);
3766 /* now check if we need to process it for persistence */
3767 if (!(s->flags & SF_IGNORE_PRST) &&
3768 (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
3769 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
3770 /* assume passive cookie by default */
3771 txn->flags &= ~TX_SCK_MASK;
3772 txn->flags |= TX_SCK_FOUND;
3773
3774 /* If the cookie is in insert mode on a known server, we'll delete
3775 * this occurrence because we'll insert another one later.
3776 * We'll delete it too if the "indirect" option is set and we're in
3777 * a direct access.
3778 */
3779 if (s->be->ck_opts & PR_CK_PSV) {
3780 /* The "preserve" flag was set, we don't want to touch the
3781 * server's cookie.
3782 */
3783 }
3784 else if ((srv && (s->be->ck_opts & PR_CK_INS)) ||
3785 ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) {
3786 /* this cookie must be deleted */
3787 if (prev == hdr_beg && next == hdr_end) {
3788 /* whole header */
3789 http_remove_header(htx, &ctx);
3790 /* note: while both invalid now, <next> and <hdr_end>
3791 * are still equal, so the for() will stop as expected.
3792 */
3793 } else {
3794 /* just remove the value */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003795 int delta = http_del_hdr_value(hdr_beg, hdr_end, &prev, next);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003796 next = prev;
3797 hdr_end += delta;
3798 }
3799 txn->flags &= ~TX_SCK_MASK;
3800 txn->flags |= TX_SCK_DELETED;
3801 /* and go on with next cookie */
3802 }
3803 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) {
3804 /* replace bytes val_beg->val_end with the cookie name associated
3805 * with this server since we know it.
3806 */
3807 int sliding, delta;
3808
3809 ctx.value = ist2(val_beg, val_end - val_beg);
3810 ctx.lws_before = ctx.lws_after = 0;
3811 http_replace_header_value(htx, &ctx, ist2(srv->cookie, srv->cklen));
3812 delta = srv->cklen - (val_end - val_beg);
3813 sliding = (ctx.value.ptr - val_beg);
3814 hdr_beg += sliding;
3815 val_beg += sliding;
3816 next += sliding + delta;
3817 hdr_end += sliding + delta;
3818
3819 txn->flags &= ~TX_SCK_MASK;
3820 txn->flags |= TX_SCK_REPLACED;
3821 }
3822 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) {
3823 /* insert the cookie name associated with this server
3824 * before existing cookie, and insert a delimiter between them..
3825 */
3826 int sliding, delta;
3827 ctx.value = ist2(val_beg, 0);
3828 ctx.lws_before = ctx.lws_after = 0;
3829 http_replace_header_value(htx, &ctx, ist2(srv->cookie, srv->cklen + 1));
3830 delta = srv->cklen + 1;
3831 sliding = (ctx.value.ptr - val_beg);
3832 hdr_beg += sliding;
3833 val_beg += sliding;
3834 next += sliding + delta;
3835 hdr_end += sliding + delta;
3836
3837 val_beg[srv->cklen] = COOKIE_DELIM;
3838 txn->flags &= ~TX_SCK_MASK;
3839 txn->flags |= TX_SCK_REPLACED;
3840 }
3841 }
3842 /* that's done for this cookie, check the next one on the same
3843 * line when next != hdr_end (only if is_cookie2).
3844 */
3845 }
3846 }
3847}
3848
Christopher Faulet25a02f62018-10-24 12:00:25 +02003849/*
3850 * Parses the Cache-Control and Pragma request header fields to determine if
3851 * the request may be served from the cache and/or if it is cacheable. Updates
3852 * s->txn->flags.
3853 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003854void http_check_request_for_cacheability(struct stream *s, struct channel *req)
Christopher Faulet25a02f62018-10-24 12:00:25 +02003855{
3856 struct http_txn *txn = s->txn;
3857 struct htx *htx;
3858 int32_t pos;
3859 int pragma_found, cc_found, i;
3860
3861 if ((txn->flags & (TX_CACHEABLE|TX_CACHE_IGNORE)) == TX_CACHE_IGNORE)
3862 return; /* nothing more to do here */
3863
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003864 htx = htxbuf(&req->buf);
Christopher Faulet25a02f62018-10-24 12:00:25 +02003865 pragma_found = cc_found = 0;
Christopher Fauleta3f15502019-05-13 15:27:23 +02003866 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003867 struct htx_blk *blk = htx_get_blk(htx, pos);
3868 enum htx_blk_type type = htx_get_blk_type(blk);
3869 struct ist n, v;
3870
3871 if (type == HTX_BLK_EOH)
3872 break;
3873 if (type != HTX_BLK_HDR)
3874 continue;
3875
3876 n = htx_get_blk_name(htx, blk);
3877 v = htx_get_blk_value(htx, blk);
3878
Willy Tarreau2e754bf2018-12-07 11:38:03 +01003879 if (isteq(n, ist("pragma"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003880 if (v.len >= 8 && strncasecmp(v.ptr, "no-cache", 8) == 0) {
3881 pragma_found = 1;
3882 continue;
3883 }
3884 }
3885
3886 /* Don't use the cache and don't try to store if we found the
3887 * Authorization header */
Willy Tarreau2e754bf2018-12-07 11:38:03 +01003888 if (isteq(n, ist("authorization"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003889 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3890 txn->flags |= TX_CACHE_IGNORE;
3891 continue;
3892 }
3893
Willy Tarreau2e754bf2018-12-07 11:38:03 +01003894 if (!isteq(n, ist("cache-control")))
Christopher Faulet25a02f62018-10-24 12:00:25 +02003895 continue;
3896
3897 /* OK, right now we know we have a cache-control header */
3898 cc_found = 1;
3899 if (!v.len) /* no info */
3900 continue;
3901
3902 i = 0;
3903 while (i < v.len && *(v.ptr+i) != '=' && *(v.ptr+i) != ',' &&
3904 !isspace((unsigned char)*(v.ptr+i)))
3905 i++;
3906
3907 /* we have a complete value between v.ptr and (v.ptr+i). We don't check the
3908 * values after max-age, max-stale nor min-fresh, we simply don't
3909 * use the cache when they're specified.
3910 */
3911 if (((i == 7) && strncasecmp(v.ptr, "max-age", 7) == 0) ||
3912 ((i == 8) && strncasecmp(v.ptr, "no-cache", 8) == 0) ||
3913 ((i == 9) && strncasecmp(v.ptr, "max-stale", 9) == 0) ||
3914 ((i == 9) && strncasecmp(v.ptr, "min-fresh", 9) == 0)) {
3915 txn->flags |= TX_CACHE_IGNORE;
3916 continue;
3917 }
3918
3919 if ((i == 8) && strncasecmp(v.ptr, "no-store", 8) == 0) {
3920 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3921 continue;
3922 }
3923 }
3924
3925 /* RFC7234#5.4:
3926 * When the Cache-Control header field is also present and
3927 * understood in a request, Pragma is ignored.
3928 * When the Cache-Control header field is not present in a
3929 * request, caches MUST consider the no-cache request
3930 * pragma-directive as having the same effect as if
3931 * "Cache-Control: no-cache" were present.
3932 */
3933 if (!cc_found && pragma_found)
3934 txn->flags |= TX_CACHE_IGNORE;
3935}
3936
3937/*
3938 * Check if response is cacheable or not. Updates s->txn->flags.
3939 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003940void http_check_response_for_cacheability(struct stream *s, struct channel *res)
Christopher Faulet25a02f62018-10-24 12:00:25 +02003941{
3942 struct http_txn *txn = s->txn;
3943 struct htx *htx;
3944 int32_t pos;
3945 int i;
3946
3947 if (txn->status < 200) {
3948 /* do not try to cache interim responses! */
3949 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3950 return;
3951 }
3952
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003953 htx = htxbuf(&res->buf);
Christopher Fauleta3f15502019-05-13 15:27:23 +02003954 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003955 struct htx_blk *blk = htx_get_blk(htx, pos);
3956 enum htx_blk_type type = htx_get_blk_type(blk);
3957 struct ist n, v;
3958
3959 if (type == HTX_BLK_EOH)
3960 break;
3961 if (type != HTX_BLK_HDR)
3962 continue;
3963
3964 n = htx_get_blk_name(htx, blk);
3965 v = htx_get_blk_value(htx, blk);
3966
Willy Tarreau2e754bf2018-12-07 11:38:03 +01003967 if (isteq(n, ist("pragma"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003968 if ((v.len >= 8) && strncasecmp(v.ptr, "no-cache", 8) == 0) {
3969 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3970 return;
3971 }
3972 }
3973
Willy Tarreau2e754bf2018-12-07 11:38:03 +01003974 if (!isteq(n, ist("cache-control")))
Christopher Faulet25a02f62018-10-24 12:00:25 +02003975 continue;
3976
3977 /* OK, right now we know we have a cache-control header */
3978 if (!v.len) /* no info */
3979 continue;
3980
3981 i = 0;
3982 while (i < v.len && *(v.ptr+i) != '=' && *(v.ptr+i) != ',' &&
3983 !isspace((unsigned char)*(v.ptr+i)))
3984 i++;
3985
3986 /* we have a complete value between v.ptr and (v.ptr+i) */
3987 if (i < v.len && *(v.ptr + i) == '=') {
3988 if (((v.len - i) > 1 && (i == 7) && strncasecmp(v.ptr, "max-age=0", 9) == 0) ||
3989 ((v.len - i) > 1 && (i == 8) && strncasecmp(v.ptr, "s-maxage=0", 10) == 0)) {
3990 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3991 continue;
3992 }
3993
3994 /* we have something of the form no-cache="set-cookie" */
3995 if ((v.len >= 21) &&
3996 strncasecmp(v.ptr, "no-cache=\"set-cookie", 20) == 0
3997 && (*(v.ptr + 20) == '"' || *(v.ptr + 20 ) == ','))
3998 txn->flags &= ~TX_CACHE_COOK;
3999 continue;
4000 }
4001
4002 /* OK, so we know that either p2 points to the end of string or to a comma */
4003 if (((i == 7) && strncasecmp(v.ptr, "private", 7) == 0) ||
4004 ((i == 8) && strncasecmp(v.ptr, "no-cache", 8) == 0) ||
4005 ((i == 8) && strncasecmp(v.ptr, "no-store", 8) == 0)) {
4006 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
4007 return;
4008 }
4009
4010 if ((i == 6) && strncasecmp(v.ptr, "public", 6) == 0) {
4011 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
4012 continue;
4013 }
4014 }
4015}
4016
Christopher Faulet377c5a52018-10-24 21:21:30 +02004017/*
4018 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
4019 * for the current backend.
4020 *
4021 * It is assumed that the request is either a HEAD, GET, or POST and that the
4022 * uri_auth field is valid.
4023 *
4024 * Returns 1 if stats should be provided, otherwise 0.
4025 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004026static int http_stats_check_uri(struct stream *s, struct http_txn *txn, struct proxy *backend)
Christopher Faulet377c5a52018-10-24 21:21:30 +02004027{
4028 struct uri_auth *uri_auth = backend->uri_auth;
4029 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004030 struct htx_sl *sl;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004031 struct ist uri;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004032
4033 if (!uri_auth)
4034 return 0;
4035
4036 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
4037 return 0;
4038
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004039 htx = htxbuf(&s->req.buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02004040 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004041 uri = htx_sl_req_uri(sl);
Willy Tarreau1eb3b482019-10-31 15:50:28 +01004042 if (*uri_auth->uri_prefix == '/')
4043 uri = http_get_path(uri);
Christopher Faulet377c5a52018-10-24 21:21:30 +02004044
4045 /* check URI size */
4046 if (uri_auth->uri_len > uri.len)
4047 return 0;
4048
4049 if (memcmp(uri.ptr, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
4050 return 0;
4051
4052 return 1;
4053}
4054
4055/* This function prepares an applet to handle the stats. It can deal with the
4056 * "100-continue" expectation, check that admin rules are met for POST requests,
4057 * and program a response message if something was unexpected. It cannot fail
4058 * and always relies on the stats applet to complete the job. It does not touch
4059 * analysers nor counters, which are left to the caller. It does not touch
4060 * s->target which is supposed to already point to the stats applet. The caller
4061 * is expected to have already assigned an appctx to the stream.
4062 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004063static int http_handle_stats(struct stream *s, struct channel *req)
Christopher Faulet377c5a52018-10-24 21:21:30 +02004064{
4065 struct stats_admin_rule *stats_admin_rule;
4066 struct stream_interface *si = &s->si[1];
4067 struct session *sess = s->sess;
4068 struct http_txn *txn = s->txn;
4069 struct http_msg *msg = &txn->req;
4070 struct uri_auth *uri_auth = s->be->uri_auth;
4071 const char *h, *lookup, *end;
4072 struct appctx *appctx;
4073 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004074 struct htx_sl *sl;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004075
4076 appctx = si_appctx(si);
4077 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
4078 appctx->st1 = appctx->st2 = 0;
4079 appctx->ctx.stats.st_code = STAT_STATUS_INIT;
Willy Tarreau676c29e2019-10-09 10:50:01 +02004080 appctx->ctx.stats.flags |= uri_auth->flags;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004081 appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */
4082 if ((msg->flags & HTTP_MSGF_VER_11) && (txn->meth != HTTP_METH_HEAD))
4083 appctx->ctx.stats.flags |= STAT_CHUNKED;
4084
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004085 htx = htxbuf(&req->buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02004086 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004087 lookup = HTX_SL_REQ_UPTR(sl) + uri_auth->uri_len;
4088 end = HTX_SL_REQ_UPTR(sl) + HTX_SL_REQ_ULEN(sl);
Christopher Faulet377c5a52018-10-24 21:21:30 +02004089
4090 for (h = lookup; h <= end - 3; h++) {
4091 if (memcmp(h, ";up", 3) == 0) {
4092 appctx->ctx.stats.flags |= STAT_HIDE_DOWN;
4093 break;
4094 }
4095 }
4096
4097 if (uri_auth->refresh) {
4098 for (h = lookup; h <= end - 10; h++) {
4099 if (memcmp(h, ";norefresh", 10) == 0) {
4100 appctx->ctx.stats.flags |= STAT_NO_REFRESH;
4101 break;
4102 }
4103 }
4104 }
4105
4106 for (h = lookup; h <= end - 4; h++) {
4107 if (memcmp(h, ";csv", 4) == 0) {
Christopher Faulet6338a082019-09-09 15:50:54 +02004108 appctx->ctx.stats.flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
Christopher Faulet377c5a52018-10-24 21:21:30 +02004109 break;
4110 }
4111 }
4112
4113 for (h = lookup; h <= end - 6; h++) {
4114 if (memcmp(h, ";typed", 6) == 0) {
Christopher Faulet6338a082019-09-09 15:50:54 +02004115 appctx->ctx.stats.flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
Christopher Faulet377c5a52018-10-24 21:21:30 +02004116 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
4117 break;
4118 }
4119 }
4120
Christopher Faulet6338a082019-09-09 15:50:54 +02004121 for (h = lookup; h <= end - 5; h++) {
4122 if (memcmp(h, ";json", 5) == 0) {
4123 appctx->ctx.stats.flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
4124 appctx->ctx.stats.flags |= STAT_FMT_JSON;
4125 break;
4126 }
4127 }
4128
4129 for (h = lookup; h <= end - 12; h++) {
4130 if (memcmp(h, ";json-schema", 12) == 0) {
4131 appctx->ctx.stats.flags &= ~STAT_FMT_MASK;
4132 appctx->ctx.stats.flags |= STAT_JSON_SCHM;
4133 break;
4134 }
4135 }
4136
Christopher Faulet377c5a52018-10-24 21:21:30 +02004137 for (h = lookup; h <= end - 8; h++) {
4138 if (memcmp(h, ";st=", 4) == 0) {
4139 int i;
4140 h += 4;
4141 appctx->ctx.stats.st_code = STAT_STATUS_UNKN;
4142 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
4143 if (strncmp(stat_status_codes[i], h, 4) == 0) {
4144 appctx->ctx.stats.st_code = i;
4145 break;
4146 }
4147 }
4148 break;
4149 }
4150 }
4151
4152 appctx->ctx.stats.scope_str = 0;
4153 appctx->ctx.stats.scope_len = 0;
4154 for (h = lookup; h <= end - 8; h++) {
4155 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
4156 int itx = 0;
4157 const char *h2;
4158 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
4159 const char *err;
4160
4161 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
4162 h2 = h;
Christopher Fauleted7a0662019-01-14 11:07:34 +01004163 appctx->ctx.stats.scope_str = h2 - HTX_SL_REQ_UPTR(sl);
4164 while (h < end) {
Christopher Faulet377c5a52018-10-24 21:21:30 +02004165 if (*h == ';' || *h == '&' || *h == ' ')
4166 break;
4167 itx++;
4168 h++;
4169 }
4170
4171 if (itx > STAT_SCOPE_TXT_MAXLEN)
4172 itx = STAT_SCOPE_TXT_MAXLEN;
4173 appctx->ctx.stats.scope_len = itx;
4174
4175 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
4176 memcpy(scope_txt, h2, itx);
4177 scope_txt[itx] = '\0';
4178 err = invalid_char(scope_txt);
4179 if (err) {
4180 /* bad char in search text => clear scope */
4181 appctx->ctx.stats.scope_str = 0;
4182 appctx->ctx.stats.scope_len = 0;
4183 }
4184 break;
4185 }
4186 }
4187
4188 /* now check whether we have some admin rules for this request */
4189 list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) {
4190 int ret = 1;
4191
4192 if (stats_admin_rule->cond) {
4193 ret = acl_exec_cond(stats_admin_rule->cond, s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
4194 ret = acl_pass(ret);
4195 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
4196 ret = !ret;
4197 }
4198
4199 if (ret) {
4200 /* no rule, or the rule matches */
4201 appctx->ctx.stats.flags |= STAT_ADMIN;
4202 break;
4203 }
4204 }
4205
Christopher Faulet5d45e382019-02-27 15:15:23 +01004206 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
4207 appctx->st0 = STAT_HTTP_HEAD;
4208 else if (txn->meth == HTTP_METH_POST) {
Christopher Fauletbd9e8422019-08-15 22:26:48 +02004209 if (appctx->ctx.stats.flags & STAT_ADMIN) {
Christopher Faulet377c5a52018-10-24 21:21:30 +02004210 appctx->st0 = STAT_HTTP_POST;
Christopher Fauletbd9e8422019-08-15 22:26:48 +02004211 if (msg->msg_state < HTTP_MSG_DATA)
4212 req->analysers |= AN_REQ_HTTP_BODY;
4213 }
Christopher Faulet377c5a52018-10-24 21:21:30 +02004214 else {
Christopher Faulet5d45e382019-02-27 15:15:23 +01004215 /* POST without admin level */
Christopher Faulet377c5a52018-10-24 21:21:30 +02004216 appctx->ctx.stats.flags &= ~STAT_CHUNKED;
4217 appctx->ctx.stats.st_code = STAT_STATUS_DENY;
4218 appctx->st0 = STAT_HTTP_LAST;
4219 }
4220 }
4221 else {
Christopher Faulet5d45e382019-02-27 15:15:23 +01004222 /* Unsupported method */
4223 appctx->ctx.stats.flags &= ~STAT_CHUNKED;
4224 appctx->ctx.stats.st_code = STAT_STATUS_IVAL;
4225 appctx->st0 = STAT_HTTP_LAST;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004226 }
4227
4228 s->task->nice = -32; /* small boost for HTTP statistics */
4229 return 1;
4230}
4231
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004232void http_perform_server_redirect(struct stream *s, struct stream_interface *si)
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004233{
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004234 struct channel *req = &s->req;
4235 struct channel *res = &s->res;
4236 struct server *srv;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004237 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004238 struct htx_sl *sl;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004239 struct ist path, location;
4240 unsigned int flags;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004241
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004242 /*
4243 * Create the location
4244 */
4245 chunk_reset(&trash);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004246
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004247 /* 1: add the server's prefix */
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004248 /* special prefix "/" means don't change URL */
4249 srv = __objt_server(s->target);
4250 if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') {
4251 if (!chunk_memcat(&trash, srv->rdr_pfx, srv->rdr_len))
4252 return;
4253 }
4254
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004255 /* 2: add the request Path */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004256 htx = htxbuf(&req->buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02004257 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004258 path = http_get_path(htx_sl_req_uri(sl));
Tim Duesterhused526372020-03-05 17:56:33 +01004259 if (!isttest(path))
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004260 return;
4261
4262 if (!chunk_memcat(&trash, path.ptr, path.len))
4263 return;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004264 location = ist2(trash.area, trash.data);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004265
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004266 /*
4267 * Create the 302 respone
4268 */
4269 htx = htx_from_buf(&res->buf);
4270 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
4271 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
4272 ist("HTTP/1.1"), ist("302"), ist("Found"));
4273 if (!sl)
4274 goto fail;
4275 sl->info.res.status = 302;
4276 s->txn->status = 302;
4277
4278 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) ||
4279 !htx_add_header(htx, ist("Connection"), ist("close")) ||
4280 !htx_add_header(htx, ist("Content-length"), ist("0")) ||
4281 !htx_add_header(htx, ist("Location"), location))
4282 goto fail;
4283
4284 if (!htx_add_endof(htx, HTX_BLK_EOH) || !htx_add_endof(htx, HTX_BLK_EOM))
4285 goto fail;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004286
Christopher Fauletc20afb82020-01-24 19:16:26 +01004287 htx_to_buf(htx, &res->buf);
Christopher Fauleta72a7e42020-01-28 09:28:11 +01004288 if (!http_forward_proxy_resp(s, 1))
4289 goto fail;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004290
4291 /* return without error. */
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004292 si_shutr(si);
4293 si_shutw(si);
4294 si->err_type = SI_ET_NONE;
4295 si->state = SI_ST_CLO;
4296
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004297 if (!(s->flags & SF_ERR_MASK))
4298 s->flags |= SF_ERR_LOCAL;
4299 if (!(s->flags & SF_FINST_MASK))
4300 s->flags |= SF_FINST_C;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004301
4302 /* FIXME: we should increase a counter of redirects per server and per backend. */
4303 srv_inc_sess_ctr(srv);
4304 srv_set_sess_last(srv);
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004305 return;
4306
4307 fail:
4308 /* If an error occurred, remove the incomplete HTTP response from the
4309 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004310 channel_htx_truncate(res, htx);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004311}
4312
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05004313/* This function terminates the request because it was completely analyzed or
Christopher Fauletf2824e62018-10-01 12:12:37 +02004314 * because an error was triggered during the body forwarding.
4315 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004316static void http_end_request(struct stream *s)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004317{
4318 struct channel *chn = &s->req;
4319 struct http_txn *txn = s->txn;
4320
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004321 DBG_TRACE_ENTER(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004322
Christopher Fauletb42a8b62018-11-19 21:59:00 +01004323 if (unlikely(txn->req.msg_state == HTTP_MSG_ERROR ||
4324 txn->rsp.msg_state == HTTP_MSG_ERROR)) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004325 channel_abort(chn);
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004326 channel_htx_truncate(chn, htxbuf(&chn->buf));
Christopher Fauletf2824e62018-10-01 12:12:37 +02004327 goto end;
4328 }
4329
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004330 if (unlikely(txn->req.msg_state < HTTP_MSG_DONE)) {
4331 DBG_TRACE_DEVEL("waiting end of the request", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004332 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004333 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004334
4335 if (txn->req.msg_state == HTTP_MSG_DONE) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004336 /* No need to read anymore, the request was completely parsed.
4337 * We can shut the read side unless we want to abort_on_close,
4338 * or we have a POST request. The issue with POST requests is
4339 * that some browsers still send a CRLF after the request, and
4340 * this CRLF must be read so that it does not remain in the kernel
4341 * buffers, otherwise a close could cause an RST on some systems
4342 * (eg: Linux).
4343 */
Christopher Faulet769d0e92019-03-22 14:23:18 +01004344 if (!(s->be->options & PR_O_ABRT_CLOSE) && txn->meth != HTTP_METH_POST)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004345 channel_dont_read(chn);
4346
4347 /* if the server closes the connection, we want to immediately react
4348 * and close the socket to save packets and syscalls.
4349 */
4350 s->si[1].flags |= SI_FL_NOHALF;
4351
4352 /* In any case we've finished parsing the request so we must
4353 * disable Nagle when sending data because 1) we're not going
4354 * to shut this side, and 2) the server is waiting for us to
4355 * send pending data.
4356 */
4357 chn->flags |= CF_NEVER_WAIT;
4358
Christopher Fauletd01ce402019-01-02 17:44:13 +01004359 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
4360 /* The server has not finished to respond, so we
4361 * don't want to move in order not to upset it.
4362 */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004363 DBG_TRACE_DEVEL("waiting end of the response", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletd01ce402019-01-02 17:44:13 +01004364 return;
4365 }
4366
Christopher Fauletf2824e62018-10-01 12:12:37 +02004367 /* When we get here, it means that both the request and the
4368 * response have finished receiving. Depending on the connection
4369 * mode, we'll have to wait for the last bytes to leave in either
4370 * direction, and sometimes for a close to be effective.
4371 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02004372 if (txn->flags & TX_CON_WANT_TUN) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004373 /* Tunnel mode will not have any analyser so it needs to
4374 * poll for reads.
4375 */
4376 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004377 if (b_data(&chn->buf)) {
4378 DBG_TRACE_DEVEL("waiting to flush the request", STRM_EV_HTTP_ANA, s, txn);
Christopher Faulet9768c262018-10-22 09:34:31 +02004379 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004380 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004381 txn->req.msg_state = HTTP_MSG_TUNNEL;
4382 }
4383 else {
4384 /* we're not expecting any new data to come for this
4385 * transaction, so we can close it.
Christopher Faulet9768c262018-10-22 09:34:31 +02004386 *
4387 * However, there is an exception if the response
4388 * length is undefined. In this case, we need to wait
4389 * the close from the server. The response will be
4390 * switched in TUNNEL mode until the end.
Christopher Fauletf2824e62018-10-01 12:12:37 +02004391 */
4392 if (!(txn->rsp.flags & HTTP_MSGF_XFER_LEN) &&
4393 txn->rsp.msg_state != HTTP_MSG_CLOSED)
Christopher Faulet9768c262018-10-22 09:34:31 +02004394 goto check_channel_flags;
Christopher Fauletf2824e62018-10-01 12:12:37 +02004395
4396 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4397 channel_shutr_now(chn);
4398 channel_shutw_now(chn);
4399 }
4400 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004401 goto check_channel_flags;
4402 }
4403
4404 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
4405 http_msg_closing:
4406 /* nothing else to forward, just waiting for the output buffer
4407 * to be empty and for the shutw_now to take effect.
4408 */
4409 if (channel_is_empty(chn)) {
4410 txn->req.msg_state = HTTP_MSG_CLOSED;
4411 goto http_msg_closed;
4412 }
4413 else if (chn->flags & CF_SHUTW) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004414 txn->req.msg_state = HTTP_MSG_ERROR;
4415 goto end;
4416 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004417 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004418 return;
4419 }
4420
4421 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
4422 http_msg_closed:
Christopher Fauletf2824e62018-10-01 12:12:37 +02004423 /* if we don't know whether the server will close, we need to hard close */
4424 if (txn->rsp.flags & HTTP_MSGF_XFER_LEN)
4425 s->si[1].flags |= SI_FL_NOLINGER; /* we want to close ASAP */
Christopher Fauletf2824e62018-10-01 12:12:37 +02004426 /* see above in MSG_DONE why we only do this in these states */
Christopher Faulet769d0e92019-03-22 14:23:18 +01004427 if (!(s->be->options & PR_O_ABRT_CLOSE))
Christopher Fauletf2824e62018-10-01 12:12:37 +02004428 channel_dont_read(chn);
4429 goto end;
4430 }
4431
4432 check_channel_flags:
4433 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4434 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4435 /* if we've just closed an output, let's switch */
4436 txn->req.msg_state = HTTP_MSG_CLOSING;
4437 goto http_msg_closing;
4438 }
4439
4440 end:
4441 chn->analysers &= AN_REQ_FLT_END;
4442 if (txn->req.msg_state == HTTP_MSG_TUNNEL && HAS_REQ_DATA_FILTERS(s))
4443 chn->analysers |= AN_REQ_FLT_XFER_DATA;
4444 channel_auto_close(chn);
4445 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004446 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004447}
4448
4449
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05004450/* This function terminates the response because it was completely analyzed or
Christopher Fauletf2824e62018-10-01 12:12:37 +02004451 * because an error was triggered during the body forwarding.
4452 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004453static void http_end_response(struct stream *s)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004454{
4455 struct channel *chn = &s->res;
4456 struct http_txn *txn = s->txn;
4457
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004458 DBG_TRACE_ENTER(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004459
Christopher Fauletb42a8b62018-11-19 21:59:00 +01004460 if (unlikely(txn->req.msg_state == HTTP_MSG_ERROR ||
4461 txn->rsp.msg_state == HTTP_MSG_ERROR)) {
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004462 channel_htx_truncate(&s->req, htxbuf(&s->req.buf));
Christopher Faulet9768c262018-10-22 09:34:31 +02004463 channel_abort(&s->req);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004464 goto end;
4465 }
4466
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004467 if (unlikely(txn->rsp.msg_state < HTTP_MSG_DONE)) {
4468 DBG_TRACE_DEVEL("waiting end of the response", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004469 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004470 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004471
4472 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
4473 /* In theory, we don't need to read anymore, but we must
4474 * still monitor the server connection for a possible close
4475 * while the request is being uploaded, so we don't disable
4476 * reading.
4477 */
4478 /* channel_dont_read(chn); */
4479
4480 if (txn->req.msg_state < HTTP_MSG_DONE) {
4481 /* The client seems to still be sending data, probably
4482 * because we got an error response during an upload.
4483 * We have the choice of either breaking the connection
4484 * or letting it pass through. Let's do the later.
4485 */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004486 DBG_TRACE_DEVEL("waiting end of the request", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004487 return;
4488 }
4489
4490 /* When we get here, it means that both the request and the
4491 * response have finished receiving. Depending on the connection
4492 * mode, we'll have to wait for the last bytes to leave in either
4493 * direction, and sometimes for a close to be effective.
4494 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02004495 if (txn->flags & TX_CON_WANT_TUN) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004496 channel_auto_read(chn);
4497 chn->flags |= CF_NEVER_WAIT;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004498 if (b_data(&chn->buf)) {
4499 DBG_TRACE_DEVEL("waiting to flush the respone", STRM_EV_HTTP_ANA, s, txn);
Christopher Faulet9768c262018-10-22 09:34:31 +02004500 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004501 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004502 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
4503 }
4504 else {
4505 /* we're not expecting any new data to come for this
4506 * transaction, so we can close it.
4507 */
4508 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4509 channel_shutr_now(chn);
4510 channel_shutw_now(chn);
4511 }
4512 }
4513 goto check_channel_flags;
4514 }
4515
4516 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
4517 http_msg_closing:
4518 /* nothing else to forward, just waiting for the output buffer
4519 * to be empty and for the shutw_now to take effect.
4520 */
4521 if (channel_is_empty(chn)) {
4522 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4523 goto http_msg_closed;
4524 }
4525 else if (chn->flags & CF_SHUTW) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004526 txn->rsp.msg_state = HTTP_MSG_ERROR;
Christopher Fauletcff0f732019-12-16 16:13:44 +01004527 _HA_ATOMIC_ADD(&strm_sess(s)->fe->fe_counters.cli_aborts, 1);
Olivier Houcharda798bf52019-03-08 18:52:00 +01004528 _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01004529 if (strm_sess(s)->listener->counters)
4530 _HA_ATOMIC_ADD(&strm_sess(s)->listener->counters->cli_aborts, 1);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004531 if (objt_server(s->target))
Christopher Fauletcff0f732019-12-16 16:13:44 +01004532 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.cli_aborts, 1);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004533 goto end;
4534 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004535 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004536 return;
4537 }
4538
4539 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
4540 http_msg_closed:
4541 /* drop any pending data */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004542 channel_htx_truncate(&s->req, htxbuf(&s->req.buf));
Christopher Faulet9768c262018-10-22 09:34:31 +02004543 channel_abort(&s->req);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004544 goto end;
4545 }
4546
4547 check_channel_flags:
4548 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4549 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4550 /* if we've just closed an output, let's switch */
4551 txn->rsp.msg_state = HTTP_MSG_CLOSING;
4552 goto http_msg_closing;
4553 }
4554
4555 end:
4556 chn->analysers &= AN_RES_FLT_END;
4557 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL && HAS_RSP_DATA_FILTERS(s))
4558 chn->analysers |= AN_RES_FLT_XFER_DATA;
4559 channel_auto_close(chn);
4560 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004561 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004562}
4563
Christopher Fauletef70e252020-01-28 09:26:19 +01004564/* Forward a response generated by HAProxy (error/redirect/return). This
4565 * function forwards all pending incoming data. If <final> is set to 0, nothing
4566 * more is performed. It is used for 1xx informational messages. Otherwise, the
Christopher Faulet8d945d62020-04-20 14:58:38 +02004567 * transaction is terminated and the request is emptied. if <final> is greater
4568 * than 1, it means after-response ruleset must not be evaluated. On success 1
4569 * is returned. If an error occurred, 0 is returned.
Christopher Fauletef70e252020-01-28 09:26:19 +01004570 */
4571int http_forward_proxy_resp(struct stream *s, int final)
4572{
4573 struct channel *req = &s->req;
4574 struct channel *res = &s->res;
4575 struct htx *htx = htxbuf(&res->buf);
4576 size_t data;
4577
4578 if (final) {
4579 htx->flags |= HTX_FL_PROXY_RESP;
Christopher Faulet8d945d62020-04-20 14:58:38 +02004580 if (final == 1 && !http_eval_after_res_rules(s))
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01004581 return 0;
Christopher Fauletef70e252020-01-28 09:26:19 +01004582
4583 channel_auto_read(req);
4584 channel_abort(req);
4585 channel_auto_close(req);
4586 channel_htx_erase(req, htxbuf(&req->buf));
4587
4588 res->wex = tick_add_ifset(now_ms, res->wto);
4589 channel_auto_read(res);
4590 channel_auto_close(res);
4591 channel_shutr_now(res);
4592 }
4593
4594 data = htx->data - co_data(res);
4595 c_adv(res, data);
4596 htx->first = -1;
4597 res->total += data;
4598 return 1;
4599}
4600
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004601void http_server_error(struct stream *s, struct stream_interface *si, int err,
4602 int finst, const struct buffer *msg)
Christopher Faulet0f226952018-10-22 09:29:56 +02004603{
Christopher Faulet72c7d8d2020-01-27 15:32:25 +01004604 http_reply_and_close(s, s->txn->status, msg);
Christopher Faulet0f226952018-10-22 09:29:56 +02004605 if (!(s->flags & SF_ERR_MASK))
4606 s->flags |= err;
4607 if (!(s->flags & SF_FINST_MASK))
4608 s->flags |= finst;
4609}
4610
Christopher Faulet72c7d8d2020-01-27 15:32:25 +01004611void http_reply_and_close(struct stream *s, short status, const struct buffer *msg)
Christopher Faulet0f226952018-10-22 09:29:56 +02004612{
Christopher Faulet8d945d62020-04-20 14:58:38 +02004613 int final = 1;
4614
4615 retry:
Christopher Faulet0f226952018-10-22 09:29:56 +02004616 channel_auto_read(&s->req);
4617 channel_abort(&s->req);
4618 channel_auto_close(&s->req);
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004619 channel_htx_erase(&s->req, htxbuf(&s->req.buf));
4620 channel_htx_truncate(&s->res, htxbuf(&s->res.buf));
Christopher Faulet72c7d8d2020-01-27 15:32:25 +01004621 channel_auto_read(&s->res);
4622 channel_auto_close(&s->res);
4623 channel_shutr_now(&s->res);
Christopher Faulet0f226952018-10-22 09:29:56 +02004624
Christopher Faulet72c7d8d2020-01-27 15:32:25 +01004625 s->res.wex = tick_add_ifset(now_ms, s->res.wto);
Christopher Faulet0f226952018-10-22 09:29:56 +02004626 s->txn->flags &= ~TX_WAIT_NEXT_RQ;
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004627
4628 /* <msg> is an HTX structure. So we copy it in the response's
4629 * channel */
Christopher Faulet9f5839c2019-07-22 16:41:43 +02004630 if (msg && !b_is_null(msg)) {
Christopher Faulet0f226952018-10-22 09:29:56 +02004631 struct channel *chn = &s->res;
4632 struct htx *htx;
4633
Christopher Fauletaed82cf2018-11-30 22:22:32 +01004634 FLT_STRM_CB(s, flt_http_reply(s, s->txn->status, msg));
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004635 htx = htx_from_buf(&chn->buf);
Christopher Faulet637259e2020-01-23 11:57:31 +01004636 if (channel_htx_copy_msg(chn, htx, msg)) {
Christopher Faulet8d945d62020-04-20 14:58:38 +02004637 if (!http_forward_proxy_resp(s, final)) {
4638 /* On error, return a 500 error message, but
4639 * don't rewrite it if it is already an internal
4640 * error.
4641 */
4642 if (s->txn->status == 500)
4643 final++;
Christopher Fauleta72a7e42020-01-28 09:28:11 +01004644 s->txn->status = 500;
Christopher Faulet8d945d62020-04-20 14:58:38 +02004645 msg = http_error_message(s);
4646 goto retry;
Christopher Fauleta72a7e42020-01-28 09:28:11 +01004647 }
Christopher Faulet637259e2020-01-23 11:57:31 +01004648 }
Christopher Faulet0f226952018-10-22 09:29:56 +02004649 }
Christopher Faulet0f226952018-10-22 09:29:56 +02004650}
4651
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004652struct buffer *http_error_message(struct stream *s)
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004653{
4654 const int msgnum = http_get_status_idx(s->txn->status);
4655
Christopher Faulet53a87e12020-01-21 10:13:03 +01004656 if (s->txn->errmsg)
Christopher Faulet473e8802020-01-14 11:12:37 +01004657 return s->txn->errmsg;
4658 else if (s->be->errmsg[msgnum])
Christopher Faulet58857752020-01-15 15:19:50 +01004659 return s->be->errmsg[msgnum];
4660 else if (strm_fe(s)->errmsg[msgnum])
4661 return strm_fe(s)->errmsg[msgnum];
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004662 else
Christopher Fauletf7346382019-07-17 22:02:08 +02004663 return &http_err_chunks[msgnum];
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004664}
4665
Christopher Faulet304cc402019-07-15 15:46:28 +02004666/* Return the error message corresponding to si->err_type. It is assumed
4667 * that the server side is closed. Note that err_type is actually a
4668 * bitmask, where almost only aborts may be cumulated with other
4669 * values. We consider that aborted operations are more important
4670 * than timeouts or errors due to the fact that nobody else in the
4671 * logs might explain incomplete retries. All others should avoid
4672 * being cumulated. It should normally not be possible to have multiple
4673 * aborts at once, but just in case, the first one in sequence is reported.
4674 * Note that connection errors appearing on the second request of a keep-alive
4675 * connection are not reported since this allows the client to retry.
4676 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004677void http_return_srv_error(struct stream *s, struct stream_interface *si)
Christopher Faulet304cc402019-07-15 15:46:28 +02004678{
4679 int err_type = si->err_type;
4680
4681 /* set s->txn->status for http_error_message(s) */
4682 s->txn->status = 503;
4683
4684 if (err_type & SI_ET_QUEUE_ABRT)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004685 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_Q,
4686 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004687 else if (err_type & SI_ET_CONN_ABRT)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004688 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_C,
4689 (s->txn->flags & TX_NOT_FIRST) ? NULL :
4690 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004691 else if (err_type & SI_ET_QUEUE_TO)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004692 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_Q,
4693 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004694 else if (err_type & SI_ET_QUEUE_ERR)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004695 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_Q,
4696 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004697 else if (err_type & SI_ET_CONN_TO)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004698 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_C,
4699 (s->txn->flags & TX_NOT_FIRST) ? NULL :
4700 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004701 else if (err_type & SI_ET_CONN_ERR)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004702 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_C,
4703 (s->flags & SF_SRV_REUSED) ? NULL :
4704 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004705 else if (err_type & SI_ET_CONN_RES)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004706 http_server_error(s, si, SF_ERR_RESOURCE, SF_FINST_C,
4707 (s->txn->flags & TX_NOT_FIRST) ? NULL :
4708 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004709 else { /* SI_ET_CONN_OTHER and others */
4710 s->txn->status = 500;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004711 http_server_error(s, si, SF_ERR_INTERNAL, SF_FINST_C,
4712 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004713 }
4714}
4715
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004716
Christopher Faulet4a28a532019-03-01 11:19:40 +01004717/* Handle Expect: 100-continue for HTTP/1.1 messages if necessary. It returns 0
4718 * on success and -1 on error.
4719 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004720static int http_handle_expect_hdr(struct stream *s, struct htx *htx, struct http_msg *msg)
Christopher Faulet4a28a532019-03-01 11:19:40 +01004721{
4722 /* If we have HTTP/1.1 message with a body and Expect: 100-continue,
4723 * then we must send an HTTP/1.1 100 Continue intermediate response.
4724 */
4725 if (msg->msg_state == HTTP_MSG_BODY && (msg->flags & HTTP_MSGF_VER_11) &&
4726 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
4727 struct ist hdr = { .ptr = "Expect", .len = 6 };
4728 struct http_hdr_ctx ctx;
4729
4730 ctx.blk = NULL;
4731 /* Expect is allowed in 1.1, look for it */
4732 if (http_find_header(htx, hdr, &ctx, 0) &&
4733 unlikely(isteqi(ctx.value, ist2("100-continue", 12)))) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004734 if (http_reply_100_continue(s) == -1)
Christopher Faulet4a28a532019-03-01 11:19:40 +01004735 return -1;
4736 http_remove_header(htx, &ctx);
4737 }
4738 }
4739 return 0;
4740}
4741
Christopher Faulet23a3c792018-11-28 10:01:23 +01004742/* Send a 100-Continue response to the client. It returns 0 on success and -1
4743 * on error. The response channel is updated accordingly.
4744 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004745static int http_reply_100_continue(struct stream *s)
Christopher Faulet23a3c792018-11-28 10:01:23 +01004746{
4747 struct channel *res = &s->res;
4748 struct htx *htx = htx_from_buf(&res->buf);
4749 struct htx_sl *sl;
4750 unsigned int flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|
4751 HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
Christopher Faulet23a3c792018-11-28 10:01:23 +01004752
4753 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
4754 ist("HTTP/1.1"), ist("100"), ist("Continue"));
4755 if (!sl)
4756 goto fail;
4757 sl->info.res.status = 100;
4758
Christopher Faulet1d5ec092019-06-26 14:23:54 +02004759 if (!htx_add_endof(htx, HTX_BLK_EOH))
Christopher Faulet23a3c792018-11-28 10:01:23 +01004760 goto fail;
4761
Christopher Fauleta72a7e42020-01-28 09:28:11 +01004762 if (!http_forward_proxy_resp(s, 0))
4763 goto fail;
Christopher Faulet23a3c792018-11-28 10:01:23 +01004764 return 0;
4765
4766 fail:
4767 /* If an error occurred, remove the incomplete HTTP response from the
4768 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004769 channel_htx_truncate(res, htx);
Christopher Faulet23a3c792018-11-28 10:01:23 +01004770 return -1;
4771}
4772
Christopher Faulet12c51e22018-11-28 15:59:42 +01004773
4774/* Send a 401-Unauthorized or 407-Unauthorized response to the client, depending
4775 * ont whether we use a proxy or not. It returns 0 on success and -1 on
4776 * error. The response channel is updated accordingly.
4777 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004778static int http_reply_40x_unauthorized(struct stream *s, const char *auth_realm)
Christopher Faulet12c51e22018-11-28 15:59:42 +01004779{
4780 struct channel *res = &s->res;
4781 struct htx *htx = htx_from_buf(&res->buf);
4782 struct htx_sl *sl;
4783 struct ist code, body;
4784 int status;
4785 unsigned int flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11);
Christopher Faulet12c51e22018-11-28 15:59:42 +01004786
4787 if (!(s->txn->flags & TX_USE_PX_CONN)) {
4788 status = 401;
4789 code = ist("401");
4790 body = ist("<html><body><h1>401 Unauthorized</h1>\n"
4791 "You need a valid user and password to access this content.\n"
4792 "</body></html>\n");
4793 }
4794 else {
4795 status = 407;
4796 code = ist("407");
4797 body = ist("<html><body><h1>407 Unauthorized</h1>\n"
4798 "You need a valid user and password to access this content.\n"
4799 "</body></html>\n");
4800 }
4801
4802 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
4803 ist("HTTP/1.1"), code, ist("Unauthorized"));
4804 if (!sl)
4805 goto fail;
4806 sl->info.res.status = status;
4807 s->txn->status = status;
4808
4809 if (chunk_printf(&trash, "Basic realm=\"%s\"", auth_realm) == -1)
4810 goto fail;
4811
Willy Tarreaub5ba2b02019-06-11 16:08:25 +02004812 if (!htx_add_header(htx, ist("Content-length"), ist("112")) ||
4813 !htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) ||
Christopher Faulet12c51e22018-11-28 15:59:42 +01004814 !htx_add_header(htx, ist("Connection"), ist("close")) ||
Jérôme Magnin86cef232018-12-28 14:49:08 +01004815 !htx_add_header(htx, ist("Content-Type"), ist("text/html")))
4816 goto fail;
4817 if (status == 401 && !htx_add_header(htx, ist("WWW-Authenticate"), ist2(trash.area, trash.data)))
4818 goto fail;
4819 if (status == 407 && !htx_add_header(htx, ist("Proxy-Authenticate"), ist2(trash.area, trash.data)))
Christopher Faulet12c51e22018-11-28 15:59:42 +01004820 goto fail;
Willy Tarreau0a7ef022019-05-28 10:30:11 +02004821 if (!htx_add_endof(htx, HTX_BLK_EOH))
4822 goto fail;
4823
4824 while (body.len) {
4825 size_t sent = htx_add_data(htx, body);
4826 if (!sent)
4827 goto fail;
4828 body.ptr += sent;
4829 body.len -= sent;
4830 }
4831
4832 if (!htx_add_endof(htx, HTX_BLK_EOM))
Christopher Faulet12c51e22018-11-28 15:59:42 +01004833 goto fail;
4834
Christopher Fauleta72a7e42020-01-28 09:28:11 +01004835 if (!http_forward_proxy_resp(s, 1))
4836 goto fail;
Christopher Faulet12c51e22018-11-28 15:59:42 +01004837 return 0;
4838
4839 fail:
4840 /* If an error occurred, remove the incomplete HTTP response from the
4841 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004842 channel_htx_truncate(res, htx);
Christopher Faulet12c51e22018-11-28 15:59:42 +01004843 return -1;
4844}
4845
Christopher Faulet0f226952018-10-22 09:29:56 +02004846/*
4847 * Capture headers from message <htx> according to header list <cap_hdr>, and
4848 * fill the <cap> pointers appropriately.
4849 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004850static void http_capture_headers(struct htx *htx, char **cap, struct cap_hdr *cap_hdr)
Christopher Faulet0f226952018-10-22 09:29:56 +02004851{
4852 struct cap_hdr *h;
4853 int32_t pos;
4854
Christopher Fauleta3f15502019-05-13 15:27:23 +02004855 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet0f226952018-10-22 09:29:56 +02004856 struct htx_blk *blk = htx_get_blk(htx, pos);
4857 enum htx_blk_type type = htx_get_blk_type(blk);
4858 struct ist n, v;
4859
4860 if (type == HTX_BLK_EOH)
4861 break;
4862 if (type != HTX_BLK_HDR)
4863 continue;
4864
4865 n = htx_get_blk_name(htx, blk);
4866
4867 for (h = cap_hdr; h; h = h->next) {
4868 if (h->namelen && (h->namelen == n.len) &&
4869 (strncasecmp(n.ptr, h->name, h->namelen) == 0)) {
4870 if (cap[h->index] == NULL)
4871 cap[h->index] =
4872 pool_alloc(h->pool);
4873
4874 if (cap[h->index] == NULL) {
4875 ha_alert("HTTP capture : out of memory.\n");
4876 break;
4877 }
4878
4879 v = htx_get_blk_value(htx, blk);
4880 if (v.len > h->len)
4881 v.len = h->len;
4882
4883 memcpy(cap[h->index], v.ptr, v.len);
4884 cap[h->index][v.len]=0;
4885 }
4886 }
4887 }
4888}
4889
Christopher Faulet0b6bdc52018-10-24 11:05:36 +02004890/* Delete a value in a header between delimiters <from> and <next>. The header
4891 * itself is delimited by <start> and <end> pointers. The number of characters
4892 * displaced is returned, and the pointer to the first delimiter is updated if
4893 * required. The function tries as much as possible to respect the following
4894 * principles :
4895 * - replace <from> delimiter by the <next> one unless <from> points to <start>,
4896 * in which case <next> is simply removed
4897 * - set exactly one space character after the new first delimiter, unless there
4898 * are not enough characters in the block being moved to do so.
4899 * - remove unneeded spaces before the previous delimiter and after the new
4900 * one.
4901 *
4902 * It is the caller's responsibility to ensure that :
4903 * - <from> points to a valid delimiter or <start> ;
4904 * - <next> points to a valid delimiter or <end> ;
4905 * - there are non-space chars before <from>.
4906 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004907static int http_del_hdr_value(char *start, char *end, char **from, char *next)
Christopher Faulet0b6bdc52018-10-24 11:05:36 +02004908{
4909 char *prev = *from;
4910
4911 if (prev == start) {
4912 /* We're removing the first value. eat the semicolon, if <next>
4913 * is lower than <end> */
4914 if (next < end)
4915 next++;
4916
4917 while (next < end && HTTP_IS_SPHT(*next))
4918 next++;
4919 }
4920 else {
4921 /* Remove useless spaces before the old delimiter. */
4922 while (HTTP_IS_SPHT(*(prev-1)))
4923 prev--;
4924 *from = prev;
4925
4926 /* copy the delimiter and if possible a space if we're
4927 * not at the end of the line.
4928 */
4929 if (next < end) {
4930 *prev++ = *next++;
4931 if (prev + 1 < next)
4932 *prev++ = ' ';
4933 while (next < end && HTTP_IS_SPHT(*next))
4934 next++;
4935 }
4936 }
4937 memmove(prev, next, end - next);
4938 return (prev - next);
4939}
4940
Christopher Faulet0f226952018-10-22 09:29:56 +02004941
4942/* Formats the start line of the request (without CRLF) and puts it in <str> and
Joseph Herlantc42c0e92018-11-25 10:43:27 -08004943 * return the written length. The line can be truncated if it exceeds <len>.
Christopher Faulet0f226952018-10-22 09:29:56 +02004944 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004945static size_t http_fmt_req_line(const struct htx_sl *sl, char *str, size_t len)
Christopher Faulet0f226952018-10-22 09:29:56 +02004946{
4947 struct ist dst = ist2(str, 0);
4948
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004949 if (istcat(&dst, htx_sl_req_meth(sl), len) == -1)
Christopher Faulet0f226952018-10-22 09:29:56 +02004950 goto end;
4951 if (dst.len + 1 > len)
4952 goto end;
4953 dst.ptr[dst.len++] = ' ';
4954
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004955 if (istcat(&dst, htx_sl_req_uri(sl), len) == -1)
Christopher Faulet0f226952018-10-22 09:29:56 +02004956 goto end;
4957 if (dst.len + 1 > len)
4958 goto end;
4959 dst.ptr[dst.len++] = ' ';
4960
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004961 istcat(&dst, htx_sl_req_vsn(sl), len);
Christopher Faulet0f226952018-10-22 09:29:56 +02004962 end:
4963 return dst.len;
4964}
4965
4966/*
4967 * Print a debug line with a start line.
4968 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004969static void http_debug_stline(const char *dir, struct stream *s, const struct htx_sl *sl)
Christopher Faulet0f226952018-10-22 09:29:56 +02004970{
4971 struct session *sess = strm_sess(s);
4972 int max;
4973
4974 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
4975 dir,
4976 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->handle.fd : -1,
4977 objt_cs(s->si[1].end) ? (unsigned short)objt_cs(s->si[1].end)->conn->handle.fd : -1);
4978
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004979 max = HTX_SL_P1_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02004980 UBOUND(max, trash.size - trash.data - 3);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004981 chunk_memcat(&trash, HTX_SL_P1_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02004982 trash.area[trash.data++] = ' ';
4983
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004984 max = HTX_SL_P2_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02004985 UBOUND(max, trash.size - trash.data - 2);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004986 chunk_memcat(&trash, HTX_SL_P2_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02004987 trash.area[trash.data++] = ' ';
4988
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004989 max = HTX_SL_P3_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02004990 UBOUND(max, trash.size - trash.data - 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004991 chunk_memcat(&trash, HTX_SL_P3_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02004992 trash.area[trash.data++] = '\n';
4993
Willy Tarreau2e8ab6b2020-03-14 11:03:20 +01004994 DISGUISE(write(1, trash.area, trash.data));
Christopher Faulet0f226952018-10-22 09:29:56 +02004995}
4996
4997/*
4998 * Print a debug line with a header.
4999 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005000static 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 +02005001{
5002 struct session *sess = strm_sess(s);
5003 int max;
5004
5005 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
5006 dir,
5007 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->handle.fd : -1,
5008 objt_cs(s->si[1].end) ? (unsigned short)objt_cs(s->si[1].end)->conn->handle.fd : -1);
5009
5010 max = n.len;
5011 UBOUND(max, trash.size - trash.data - 3);
5012 chunk_memcat(&trash, n.ptr, max);
5013 trash.area[trash.data++] = ':';
5014 trash.area[trash.data++] = ' ';
5015
5016 max = v.len;
5017 UBOUND(max, trash.size - trash.data - 1);
5018 chunk_memcat(&trash, v.ptr, max);
5019 trash.area[trash.data++] = '\n';
5020
Willy Tarreau2e8ab6b2020-03-14 11:03:20 +01005021 DISGUISE(write(1, trash.area, trash.data));
Christopher Faulet0f226952018-10-22 09:29:56 +02005022}
5023
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005024/* Allocate a new HTTP transaction for stream <s> unless there is one already.
5025 * In case of allocation failure, everything allocated is freed and NULL is
5026 * returned. Otherwise the new transaction is assigned to the stream and
5027 * returned.
5028 */
5029struct http_txn *http_alloc_txn(struct stream *s)
5030{
5031 struct http_txn *txn = s->txn;
5032
5033 if (txn)
5034 return txn;
5035
5036 txn = pool_alloc(pool_head_http_txn);
5037 if (!txn)
5038 return txn;
5039
5040 s->txn = txn;
5041 return txn;
5042}
5043
5044void http_txn_reset_req(struct http_txn *txn)
5045{
Christopher Faulet1aea50e2020-01-17 16:03:53 +01005046 txn->req.flags = 0;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005047 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
5048}
5049
5050void http_txn_reset_res(struct http_txn *txn)
5051{
Christopher Faulet1aea50e2020-01-17 16:03:53 +01005052 txn->rsp.flags = 0;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005053 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
5054}
5055
5056/*
5057 * Initialize a new HTTP transaction for stream <s>. It is assumed that all
5058 * the required fields are properly allocated and that we only need to (re)init
5059 * them. This should be used before processing any new request.
5060 */
5061void http_init_txn(struct stream *s)
5062{
5063 struct http_txn *txn = s->txn;
5064 struct conn_stream *cs = objt_cs(s->si[0].end);
5065
5066 txn->flags = ((cs && cs->flags & CS_FL_NOT_FIRST)
5067 ? (TX_NOT_FIRST|TX_WAIT_NEXT_RQ)
5068 : 0);
5069 txn->status = -1;
Christopher Faulet473e8802020-01-14 11:12:37 +01005070 txn->errmsg = NULL;
Willy Tarreau8b507582020-02-25 09:35:07 +01005071 write_u32(txn->cache_hash, 0);
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005072
5073 txn->cookie_first_date = 0;
5074 txn->cookie_last_date = 0;
5075
5076 txn->srv_cookie = NULL;
5077 txn->cli_cookie = NULL;
5078 txn->uri = NULL;
5079
5080 http_txn_reset_req(txn);
5081 http_txn_reset_res(txn);
5082
5083 txn->req.chn = &s->req;
5084 txn->rsp.chn = &s->res;
5085
5086 txn->auth.method = HTTP_AUTH_UNKNOWN;
5087
5088 vars_init(&s->vars_txn, SCOPE_TXN);
5089 vars_init(&s->vars_reqres, SCOPE_REQ);
5090}
5091
5092/* to be used at the end of a transaction */
5093void http_end_txn(struct stream *s)
5094{
5095 struct http_txn *txn = s->txn;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005096
5097 /* these ones will have been dynamically allocated */
5098 pool_free(pool_head_requri, txn->uri);
5099 pool_free(pool_head_capture, txn->cli_cookie);
5100 pool_free(pool_head_capture, txn->srv_cookie);
Tim Duesterhusa17e6622020-03-05 20:19:02 +01005101 pool_free(pool_head_uniqueid, s->unique_id.ptr);
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005102
Tim Duesterhusa17e6622020-03-05 20:19:02 +01005103 s->unique_id = IST_NULL;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005104 txn->uri = NULL;
5105 txn->srv_cookie = NULL;
5106 txn->cli_cookie = NULL;
5107
Christopher Faulet59399252019-11-07 14:27:52 +01005108 if (!LIST_ISEMPTY(&s->vars_txn.head))
5109 vars_prune(&s->vars_txn, s->sess, s);
5110 if (!LIST_ISEMPTY(&s->vars_reqres.head))
5111 vars_prune(&s->vars_reqres, s->sess, s);
5112}
5113
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005114
5115DECLARE_POOL(pool_head_http_txn, "http_txn", sizeof(struct http_txn));
Christopher Faulet0f226952018-10-22 09:29:56 +02005116
Christopher Fauletf4eb75d2018-10-11 15:55:07 +02005117__attribute__((constructor))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005118static void __http_protocol_init(void)
Christopher Fauletf4eb75d2018-10-11 15:55:07 +02005119{
5120}
5121
5122
5123/*
5124 * Local variables:
5125 * c-indent-level: 8
5126 * c-basic-offset: 8
5127 * End:
5128 */