blob: 7fd371ea562bf73f7da8eebc1dd31b0d7c20bee1 [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 Houchard8cabc972020-05-12 22:18:14 +02001427 s->flags &= ~SF_ADDR_SET;
Olivier Houchard4bd58672019-07-12 16:16:59 +02001428 stream_choose_redispatch(s);
Olivier Houcharda254a372019-04-05 15:30:12 +02001429 si->exp = TICK_ETERNITY;
1430 res->rex = TICK_ETERNITY;
1431 res->to_forward = 0;
1432 res->analyse_exp = TICK_ETERNITY;
1433 res->total = 0;
Olivier Houchard4bd58672019-07-12 16:16:59 +02001434 s->flags &= ~(SF_ERR_SRVTO | SF_ERR_SRVCL);
Olivier Houcharda254a372019-04-05 15:30:12 +02001435 si_release_endpoint(&s->si[1]);
1436 b_free(&req->buf);
1437 /* Swap the L7 buffer with the channel buffer */
1438 /* We know we stored the co_data as b_data, so get it there */
1439 co_data = b_data(&si->l7_buffer);
1440 b_set_data(&si->l7_buffer, b_size(&si->l7_buffer));
1441 b_xfer(&req->buf, &si->l7_buffer, b_data(&si->l7_buffer));
1442
1443 co_set_data(req, co_data);
1444 b_reset(&res->buf);
1445 co_set_data(res, 0);
1446 return 0;
1447}
1448
Christopher Faulete0768eb2018-10-03 16:38:02 +02001449/* This stream analyser waits for a complete HTTP response. It returns 1 if the
1450 * processing can continue on next analysers, or zero if it either needs more
1451 * data or wants to immediately abort the response (eg: timeout, error, ...). It
1452 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->res.analysers
1453 * when it has nothing left to do, and may remove any analyser when it wants to
1454 * abort.
1455 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001456int http_wait_for_response(struct stream *s, struct channel *rep, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001457{
Christopher Faulet9768c262018-10-22 09:34:31 +02001458 /*
1459 * We will analyze a complete HTTP response to check the its syntax.
1460 *
1461 * Once the start line and all headers are received, we may perform a
1462 * capture of the error (if any), and we will set a few fields. We also
1463 * logging and finally headers capture.
1464 */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001465 struct session *sess = s->sess;
1466 struct http_txn *txn = s->txn;
1467 struct http_msg *msg = &txn->rsp;
Christopher Faulet9768c262018-10-22 09:34:31 +02001468 struct htx *htx;
Olivier Houcharda254a372019-04-05 15:30:12 +02001469 struct stream_interface *si_b = &s->si[1];
Christopher Faulet61608322018-11-23 16:23:45 +01001470 struct connection *srv_conn;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001471 struct htx_sl *sl;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001472 int n;
1473
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001474 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001475
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001476 htx = htxbuf(&rep->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001477
Willy Tarreau4236f032019-03-05 10:43:32 +01001478 /* Parsing errors are caught here */
1479 if (htx->flags & HTX_FL_PARSING_ERROR)
1480 goto return_bad_res;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001481 if (htx->flags & HTX_FL_PROCESSING_ERROR)
1482 goto return_int_err;
Willy Tarreau4236f032019-03-05 10:43:32 +01001483
Christopher Faulete0768eb2018-10-03 16:38:02 +02001484 /*
1485 * Now we quickly check if we have found a full valid response.
1486 * If not so, we check the FD and buffer states before leaving.
1487 * A full response is indicated by the fact that we have seen
1488 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
1489 * responses are checked first.
1490 *
1491 * Depending on whether the client is still there or not, we
1492 * may send an error response back or not. Note that normally
1493 * we should only check for HTTP status there, and check I/O
1494 * errors somewhere else.
1495 */
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001496 next_one:
Christopher Faulet29f17582019-05-23 11:03:26 +02001497 if (unlikely(htx_is_empty(htx) || htx->first == -1)) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001498 /* 1: have we encountered a read error ? */
1499 if (rep->flags & CF_READ_ERROR) {
Olivier Houchard865d8392019-05-03 22:46:27 +02001500 struct connection *conn = NULL;
1501
Olivier Houchard865d8392019-05-03 22:46:27 +02001502 if (objt_cs(s->si[1].end))
1503 conn = objt_cs(s->si[1].end)->conn;
1504
1505 if (si_b->flags & SI_FL_L7_RETRY &&
1506 (!conn || conn->err_code != CO_ER_SSL_EARLY_FAILED)) {
Olivier Houcharda254a372019-04-05 15:30:12 +02001507 /* If we arrive here, then CF_READ_ERROR was
1508 * set by si_cs_recv() because we matched a
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05001509 * status, otherwise it would have removed
Olivier Houcharda254a372019-04-05 15:30:12 +02001510 * the SI_FL_L7_RETRY flag, so it's ok not
1511 * to check s->be->retry_type.
1512 */
1513 if (co_data(rep) || do_l7_retry(s, si_b) == 0)
1514 return 0;
1515 }
1516
Olivier Houchard6db16992019-05-17 15:40:49 +02001517 if (txn->flags & TX_NOT_FIRST)
1518 goto abort_keep_alive;
1519
Olivier Houcharda798bf52019-03-08 18:52:00 +01001520 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001521 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001522 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001523 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001524 }
1525
Christopher Faulete0768eb2018-10-03 16:38:02 +02001526 rep->analysers &= AN_RES_FLT_END;
1527 txn->status = 502;
1528
1529 /* Check to see if the server refused the early data.
1530 * If so, just send a 425
1531 */
Olivier Houchard865d8392019-05-03 22:46:27 +02001532 if (conn->err_code == CO_ER_SSL_EARLY_FAILED) {
1533 if ((s->be->retry_type & PR_RE_EARLY_ERROR) &&
Olivier Houchardad26d8d2019-05-10 17:48:28 +02001534 (si_b->flags & SI_FL_L7_RETRY) &&
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001535 do_l7_retry(s, si_b) == 0) {
1536 DBG_TRACE_DEVEL("leaving on L7 retry",
1537 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houchard865d8392019-05-03 22:46:27 +02001538 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001539 }
Olivier Houchard865d8392019-05-03 22:46:27 +02001540 txn->status = 425;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001541 }
1542
1543 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001544 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001545
1546 if (!(s->flags & SF_ERR_MASK))
1547 s->flags |= SF_ERR_SRVCL;
1548 if (!(s->flags & SF_FINST_MASK))
1549 s->flags |= SF_FINST_H;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001550 DBG_TRACE_DEVEL("leaving on error",
1551 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001552 return 0;
1553 }
1554
Christopher Faulet9768c262018-10-22 09:34:31 +02001555 /* 2: read timeout : return a 504 to the client. */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001556 else if (rep->flags & CF_READ_TIMEOUT) {
Olivier Houcharda254a372019-04-05 15:30:12 +02001557 if ((si_b->flags & SI_FL_L7_RETRY) &&
1558 (s->be->retry_type & PR_RE_TIMEOUT)) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001559 if (co_data(rep) || do_l7_retry(s, si_b) == 0) {
1560 DBG_TRACE_DEVEL("leaving on L7 retry",
1561 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharda254a372019-04-05 15:30:12 +02001562 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001563 }
Olivier Houcharda254a372019-04-05 15:30:12 +02001564 }
Olivier Houcharda798bf52019-03-08 18:52:00 +01001565 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001566 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001567 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001568 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001569 }
1570
Christopher Faulete0768eb2018-10-03 16:38:02 +02001571 rep->analysers &= AN_RES_FLT_END;
1572 txn->status = 504;
1573 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001574 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001575
1576 if (!(s->flags & SF_ERR_MASK))
1577 s->flags |= SF_ERR_SRVTO;
1578 if (!(s->flags & SF_FINST_MASK))
1579 s->flags |= SF_FINST_H;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001580 DBG_TRACE_DEVEL("leaving on error",
1581 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001582 return 0;
1583 }
1584
Christopher Faulet9768c262018-10-22 09:34:31 +02001585 /* 3: client abort with an abortonclose */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001586 else if ((rep->flags & CF_SHUTR) && ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001587 _HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
1588 _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001589 if (sess->listener->counters)
1590 _HA_ATOMIC_ADD(&sess->listener->counters->cli_aborts, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001591 if (objt_server(s->target))
Olivier Houcharda798bf52019-03-08 18:52:00 +01001592 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.cli_aborts, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001593
1594 rep->analysers &= AN_RES_FLT_END;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001595 txn->status = 400;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001596 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001597
1598 if (!(s->flags & SF_ERR_MASK))
1599 s->flags |= SF_ERR_CLICL;
1600 if (!(s->flags & SF_FINST_MASK))
1601 s->flags |= SF_FINST_H;
1602
1603 /* process_stream() will take care of the error */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001604 DBG_TRACE_DEVEL("leaving on error",
1605 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001606 return 0;
1607 }
1608
Christopher Faulet9768c262018-10-22 09:34:31 +02001609 /* 4: close from server, capture the response if the server has started to respond */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001610 else if (rep->flags & CF_SHUTR) {
Olivier Houcharda254a372019-04-05 15:30:12 +02001611 if ((si_b->flags & SI_FL_L7_RETRY) &&
1612 (s->be->retry_type & PR_RE_DISCONNECTED)) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001613 if (co_data(rep) || do_l7_retry(s, si_b) == 0) {
1614 DBG_TRACE_DEVEL("leaving on L7 retry",
1615 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharda254a372019-04-05 15:30:12 +02001616 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001617 }
Olivier Houcharda254a372019-04-05 15:30:12 +02001618 }
1619
Olivier Houchard6db16992019-05-17 15:40:49 +02001620 if (txn->flags & TX_NOT_FIRST)
1621 goto abort_keep_alive;
1622
Olivier Houcharda798bf52019-03-08 18:52:00 +01001623 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001624 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001625 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001626 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001627 }
1628
Christopher Faulete0768eb2018-10-03 16:38:02 +02001629 rep->analysers &= AN_RES_FLT_END;
1630 txn->status = 502;
1631 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001632 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001633
1634 if (!(s->flags & SF_ERR_MASK))
1635 s->flags |= SF_ERR_SRVCL;
1636 if (!(s->flags & SF_FINST_MASK))
1637 s->flags |= SF_FINST_H;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001638 DBG_TRACE_DEVEL("leaving on error",
1639 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001640 return 0;
1641 }
1642
Christopher Faulet9768c262018-10-22 09:34:31 +02001643 /* 5: write error to client (we don't send any message then) */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001644 else if (rep->flags & CF_WRITE_ERROR) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001645 if (txn->flags & TX_NOT_FIRST)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001646 goto abort_keep_alive;
1647
Olivier Houcharda798bf52019-03-08 18:52:00 +01001648 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001649 if (objt_server(s->target))
1650 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001651 rep->analysers &= AN_RES_FLT_END;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001652
1653 if (!(s->flags & SF_ERR_MASK))
1654 s->flags |= SF_ERR_CLICL;
1655 if (!(s->flags & SF_FINST_MASK))
1656 s->flags |= SF_FINST_H;
1657
1658 /* process_stream() will take care of the error */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001659 DBG_TRACE_DEVEL("leaving on error",
1660 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001661 return 0;
1662 }
1663
1664 channel_dont_close(rep);
1665 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001666 DBG_TRACE_DEVEL("waiting for more data",
1667 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001668 return 0;
1669 }
1670
1671 /* More interesting part now : we know that we have a complete
1672 * response which at least looks like HTTP. We have an indicator
1673 * of each header's length, so we can parse them quickly.
1674 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001675 msg->msg_state = HTTP_MSG_BODY;
Christopher Faulet29f17582019-05-23 11:03:26 +02001676 BUG_ON(htx_get_first_type(htx) != HTX_BLK_RES_SL);
Christopher Faulet297fbb42019-05-13 14:41:27 +02001677 sl = http_get_stline(htx);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001678
Christopher Faulet9768c262018-10-22 09:34:31 +02001679 /* 0: we might have to print this header in debug mode */
1680 if (unlikely((global.mode & MODE_DEBUG) &&
1681 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
1682 int32_t pos;
1683
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001684 http_debug_stline("srvrep", s, sl);
Christopher Faulet9768c262018-10-22 09:34:31 +02001685
Christopher Fauleta3f15502019-05-13 15:27:23 +02001686 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001687 struct htx_blk *blk = htx_get_blk(htx, pos);
1688 enum htx_blk_type type = htx_get_blk_type(blk);
1689
1690 if (type == HTX_BLK_EOH)
1691 break;
1692 if (type != HTX_BLK_HDR)
1693 continue;
1694
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001695 http_debug_hdr("srvhdr", s,
1696 htx_get_blk_name(htx, blk),
1697 htx_get_blk_value(htx, blk));
Christopher Faulet9768c262018-10-22 09:34:31 +02001698 }
1699 }
1700
Christopher Faulet03599112018-11-27 11:21:21 +01001701 /* 1: get the status code and the version. Also set HTTP flags */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001702 txn->status = sl->info.res.status;
Christopher Faulet03599112018-11-27 11:21:21 +01001703 if (sl->flags & HTX_SL_F_VER_11)
Christopher Faulet9768c262018-10-22 09:34:31 +02001704 msg->flags |= HTTP_MSGF_VER_11;
Christopher Faulet03599112018-11-27 11:21:21 +01001705 if (sl->flags & HTX_SL_F_XFER_LEN) {
1706 msg->flags |= HTTP_MSGF_XFER_LEN;
Christopher Faulet834eee72019-02-18 11:35:02 +01001707 msg->flags |= ((sl->flags & HTX_SL_F_CLEN) ? HTTP_MSGF_CNT_LEN : HTTP_MSGF_TE_CHNK);
Christopher Fauletb2db4fa2018-11-27 16:51:09 +01001708 if (sl->flags & HTX_SL_F_BODYLESS)
1709 msg->flags |= HTTP_MSGF_BODYLESS;
Christopher Faulet03599112018-11-27 11:21:21 +01001710 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001711
1712 n = txn->status / 100;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001713 if (n < 1 || n > 5)
1714 n = 0;
Christopher Faulet9768c262018-10-22 09:34:31 +02001715
Christopher Faulete0768eb2018-10-03 16:38:02 +02001716 /* when the client triggers a 4xx from the server, it's most often due
1717 * to a missing object or permission. These events should be tracked
1718 * because if they happen often, it may indicate a brute force or a
1719 * vulnerability scan.
1720 */
1721 if (n == 4)
1722 stream_inc_http_err_ctr(s);
1723
Marcin Deranek3c27dda2020-05-15 18:32:51 +02001724 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001725 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.p.http.rsp[n], 1);
Marcin Deranek3c27dda2020-05-15 18:32:51 +02001726 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.p.http.cum_req, 1);
1727 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001728
Christopher Faulete0768eb2018-10-03 16:38:02 +02001729 /* Adjust server's health based on status code. Note: status codes 501
1730 * and 505 are triggered on demand by client request, so we must not
1731 * count them as server failures.
1732 */
1733 if (objt_server(s->target)) {
1734 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001735 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_OK);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001736 else
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001737 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_STS);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001738 }
1739
1740 /*
1741 * We may be facing a 100-continue response, or any other informational
1742 * 1xx response which is non-final, in which case this is not the right
1743 * response, and we're waiting for the next one. Let's allow this response
1744 * to go to the client and wait for the next one. There's an exception for
1745 * 101 which is used later in the code to switch protocols.
1746 */
1747 if (txn->status < 200 &&
1748 (txn->status == 100 || txn->status >= 102)) {
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001749 FLT_STRM_CB(s, flt_http_reset(s, msg));
Christopher Faulet421e7692019-06-13 11:16:45 +02001750 htx->first = channel_htx_fwd_headers(rep, htx);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001751 msg->msg_state = HTTP_MSG_RPBEFORE;
Christopher Faulet3499f622019-09-03 15:23:54 +02001752 msg->flags = 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001753 txn->status = 0;
1754 s->logs.t_data = -1; /* was not a response yet */
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001755 goto next_one;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001756 }
1757
1758 /*
1759 * 2: check for cacheability.
1760 */
1761
1762 switch (txn->status) {
1763 case 200:
1764 case 203:
1765 case 204:
1766 case 206:
1767 case 300:
1768 case 301:
1769 case 404:
1770 case 405:
1771 case 410:
1772 case 414:
1773 case 501:
1774 break;
1775 default:
1776 /* RFC7231#6.1:
1777 * Responses with status codes that are defined as
1778 * cacheable by default (e.g., 200, 203, 204, 206,
1779 * 300, 301, 404, 405, 410, 414, and 501 in this
1780 * specification) can be reused by a cache with
1781 * heuristic expiration unless otherwise indicated
1782 * by the method definition or explicit cache
1783 * controls [RFC7234]; all other status codes are
1784 * not cacheable by default.
1785 */
1786 txn->flags &= ~(TX_CACHEABLE | TX_CACHE_COOK);
1787 break;
1788 }
1789
1790 /*
1791 * 3: we may need to capture headers
1792 */
1793 s->logs.logwait &= ~LW_RESP;
1794 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->res_cap))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001795 http_capture_headers(htx, s->res_cap, sess->fe->rsp_cap);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001796
Christopher Faulet9768c262018-10-22 09:34:31 +02001797 /* Skip parsing if no content length is possible. */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001798 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
1799 txn->status == 101)) {
1800 /* Either we've established an explicit tunnel, or we're
1801 * switching the protocol. In both cases, we're very unlikely
1802 * to understand the next protocols. We have to switch to tunnel
1803 * mode, so that we transfer the request and responses then let
1804 * this protocol pass unmodified. When we later implement specific
1805 * parsers for such protocols, we'll want to check the Upgrade
1806 * header which contains information about that protocol for
1807 * responses with status 101 (eg: see RFC2817 about TLS).
1808 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02001809 txn->flags |= TX_CON_WANT_TUN;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001810 }
1811
Christopher Faulet61608322018-11-23 16:23:45 +01001812 /* check for NTML authentication headers in 401 (WWW-Authenticate) and
1813 * 407 (Proxy-Authenticate) responses and set the connection to private
1814 */
1815 srv_conn = cs_conn(objt_cs(s->si[1].end));
1816 if (srv_conn) {
1817 struct ist hdr;
1818 struct http_hdr_ctx ctx;
1819
1820 if (txn->status == 401)
1821 hdr = ist("WWW-Authenticate");
1822 else if (txn->status == 407)
1823 hdr = ist("Proxy-Authenticate");
1824 else
1825 goto end;
1826
1827 ctx.blk = NULL;
1828 while (http_find_header(htx, hdr, &ctx, 0)) {
Willy Tarreauf1dcced2020-05-07 19:27:02 +02001829 /* If www-authenticate contains "Negotiate", "Nego2", or "NTLM",
1830 * possibly followed by blanks and a base64 string, the connection
1831 * is private. Since it's a mess to deal with, we only check for
1832 * values starting with "NTLM" or "Nego". Note that often multiple
1833 * headers are sent by the server there.
1834 */
1835 if ((ctx.value.len >= 4 && strncasecmp(ctx.value.ptr, "Nego", 4) == 0) ||
Willy Tarreau49a1d282020-05-07 19:10:15 +02001836 (ctx.value.len >= 4 && strncasecmp(ctx.value.ptr, "NTLM", 4) == 0)) {
Olivier Houchard250031e2019-05-29 15:01:50 +02001837 sess->flags |= SESS_FL_PREFER_LAST;
Christopher Faulet61608322018-11-23 16:23:45 +01001838 srv_conn->flags |= CO_FL_PRIVATE;
Willy Tarreauf1dcced2020-05-07 19:27:02 +02001839 break;
Olivier Houchard250031e2019-05-29 15:01:50 +02001840 }
Christopher Faulet61608322018-11-23 16:23:45 +01001841 }
1842 }
1843
1844 end:
Christopher Faulete0768eb2018-10-03 16:38:02 +02001845 /* we want to have the response time before we start processing it */
1846 s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now);
1847
1848 /* end of job, return OK */
1849 rep->analysers &= ~an_bit;
1850 rep->analyse_exp = TICK_ETERNITY;
1851 channel_auto_close(rep);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001852 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001853 return 1;
1854
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001855 return_int_err:
Christopher Fauletcff0f732019-12-16 16:13:44 +01001856 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01001857 _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001858 if (sess->listener->counters)
1859 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01001860 if (objt_server(s->target))
1861 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.internal_errors, 1);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001862 txn->status = 500;
1863 if (!(s->flags & SF_ERR_MASK))
1864 s->flags |= SF_ERR_INTERNAL;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001865 goto return_prx_cond;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001866
1867 return_bad_res:
Olivier Houcharda798bf52019-03-08 18:52:00 +01001868 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulet47365272018-10-31 17:40:50 +01001869 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001870 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001871 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Christopher Faulet47365272018-10-31 17:40:50 +01001872 }
Olivier Houcharde3249a92019-05-03 23:01:47 +02001873 if ((s->be->retry_type & PR_RE_JUNK_REQUEST) &&
Olivier Houchardad26d8d2019-05-10 17:48:28 +02001874 (si_b->flags & SI_FL_L7_RETRY) &&
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001875 do_l7_retry(s, si_b) == 0) {
1876 DBG_TRACE_DEVEL("leaving on L7 retry",
1877 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharde3249a92019-05-03 23:01:47 +02001878 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001879 }
Christopher Faulet47365272018-10-31 17:40:50 +01001880 txn->status = 502;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001881 /* fall through */
1882
Christopher Fauletb8a53712019-12-16 11:29:38 +01001883 return_prx_cond:
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001884 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet47365272018-10-31 17:40:50 +01001885
1886 if (!(s->flags & SF_ERR_MASK))
1887 s->flags |= SF_ERR_PRXCOND;
1888 if (!(s->flags & SF_FINST_MASK))
1889 s->flags |= SF_FINST_H;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001890
1891 s->si[1].flags |= SI_FL_NOLINGER;
1892 rep->analysers &= AN_RES_FLT_END;
Christopher Faulete58c0002020-03-02 16:21:01 +01001893 s->req.analysers &= AN_REQ_FLT_END;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001894 rep->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001895 DBG_TRACE_DEVEL("leaving on error",
1896 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulet47365272018-10-31 17:40:50 +01001897 return 0;
1898
Christopher Faulete0768eb2018-10-03 16:38:02 +02001899 abort_keep_alive:
1900 /* A keep-alive request to the server failed on a network error.
1901 * The client is required to retry. We need to close without returning
1902 * any other information so that the client retries.
1903 */
1904 txn->status = 0;
1905 rep->analysers &= AN_RES_FLT_END;
1906 s->req.analysers &= AN_REQ_FLT_END;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001907 s->logs.logwait = 0;
1908 s->logs.level = 0;
1909 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001910 http_reply_and_close(s, txn->status, NULL);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001911 DBG_TRACE_DEVEL("leaving by closing K/A connection",
1912 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001913 return 0;
1914}
1915
1916/* This function performs all the processing enabled for the current response.
1917 * It normally returns 1 unless it wants to break. It relies on buffers flags,
1918 * and updates s->res.analysers. It might make sense to explode it into several
1919 * other functions. It works like process_request (see indications above).
1920 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001921int http_process_res_common(struct stream *s, struct channel *rep, int an_bit, struct proxy *px)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001922{
1923 struct session *sess = s->sess;
1924 struct http_txn *txn = s->txn;
1925 struct http_msg *msg = &txn->rsp;
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001926 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001927 struct proxy *cur_proxy;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001928 enum rule_result ret = HTTP_RULE_RES_CONT;
1929
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001930 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
1931 return 0;
Christopher Faulet9768c262018-10-22 09:34:31 +02001932
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001933 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001934
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001935 htx = htxbuf(&rep->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001936
1937 /* The stats applet needs to adjust the Connection header but we don't
1938 * apply any filter there.
1939 */
1940 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
1941 rep->analysers &= ~an_bit;
1942 rep->analyse_exp = TICK_ETERNITY;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001943 goto end;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001944 }
1945
1946 /*
1947 * We will have to evaluate the filters.
1948 * As opposed to version 1.2, now they will be evaluated in the
1949 * filters order and not in the header order. This means that
1950 * each filter has to be validated among all headers.
1951 *
1952 * Filters are tried with ->be first, then with ->fe if it is
1953 * different from ->be.
1954 *
1955 * Maybe we are in resume condiion. In this case I choose the
1956 * "struct proxy" which contains the rule list matching the resume
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05001957 * pointer. If none of these "struct proxy" match, I initialise
Christopher Faulete0768eb2018-10-03 16:38:02 +02001958 * the process with the first one.
1959 *
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05001960 * In fact, I check only correspondence between the current list
Christopher Faulete0768eb2018-10-03 16:38:02 +02001961 * pointer and the ->fe rule list. If it doesn't match, I initialize
1962 * the loop with the ->be.
1963 */
1964 if (s->current_rule_list == &sess->fe->http_res_rules)
1965 cur_proxy = sess->fe;
1966 else
1967 cur_proxy = s->be;
1968 while (1) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001969 /* evaluate http-response rules */
1970 if (ret == HTTP_RULE_RES_CONT) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001971 ret = http_res_get_intercept_rule(cur_proxy, &cur_proxy->http_res_rules, s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001972
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001973 switch (ret) {
1974 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
1975 goto return_prx_yield;
1976
1977 case HTTP_RULE_RES_CONT:
1978 case HTTP_RULE_RES_STOP: /* nothing to do */
1979 break;
1980
1981 case HTTP_RULE_RES_DENY: /* deny or tarpit */
1982 goto deny;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001983
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001984 case HTTP_RULE_RES_ABRT: /* abort request, response already sent */
1985 goto return_prx_cond;
1986
1987 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
Christopher Fauletb8a53712019-12-16 11:29:38 +01001988 goto done;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001989
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001990 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
1991 goto return_bad_res;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001992
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001993 case HTTP_RULE_RES_ERROR: /* failed with a bad request */
1994 goto return_int_err;
1995 }
1996
1997 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001998
Christopher Faulete0768eb2018-10-03 16:38:02 +02001999 /* check whether we're already working on the frontend */
2000 if (cur_proxy == sess->fe)
2001 break;
2002 cur_proxy = sess->fe;
2003 }
2004
Christopher Faulete0768eb2018-10-03 16:38:02 +02002005 /* OK that's all we can do for 1xx responses */
2006 if (unlikely(txn->status < 200 && txn->status != 101))
Christopher Fauletf2824e62018-10-01 12:12:37 +02002007 goto end;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002008
2009 /*
2010 * Now check for a server cookie.
2011 */
2012 if (s->be->cookie_name || sess->fe->capture_name || (s->be->options & PR_O_CHK_CACHE))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002013 http_manage_server_side_cookies(s, rep);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002014
2015 /*
2016 * Check for cache-control or pragma headers if required.
2017 */
2018 if ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002019 http_check_response_for_cacheability(s, rep);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002020
2021 /*
2022 * Add server cookie in the response if needed
2023 */
2024 if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) &&
2025 !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) &&
2026 (!(s->flags & SF_DIRECT) ||
2027 ((s->be->cookie_maxidle || txn->cookie_last_date) &&
2028 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
2029 (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
2030 (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
2031 (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
2032 !(s->flags & SF_IGNORE_PRST)) {
2033 /* the server is known, it's not the one the client requested, or the
2034 * cookie's last seen date needs to be refreshed. We have to
2035 * insert a set-cookie here, except if we want to insert only on POST
2036 * requests and this one isn't. Note that servers which don't have cookies
2037 * (eg: some backup servers) will return a full cookie removal request.
2038 */
2039 if (!objt_server(s->target)->cookie) {
2040 chunk_printf(&trash,
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002041 "%s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
Christopher Faulete0768eb2018-10-03 16:38:02 +02002042 s->be->cookie_name);
2043 }
2044 else {
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002045 chunk_printf(&trash, "%s=%s", s->be->cookie_name, objt_server(s->target)->cookie);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002046
2047 if (s->be->cookie_maxidle || s->be->cookie_maxlife) {
2048 /* emit last_date, which is mandatory */
2049 trash.area[trash.data++] = COOKIE_DELIM_DATE;
2050 s30tob64((date.tv_sec+3) >> 2,
2051 trash.area + trash.data);
2052 trash.data += 5;
2053
2054 if (s->be->cookie_maxlife) {
2055 /* emit first_date, which is either the original one or
2056 * the current date.
2057 */
2058 trash.area[trash.data++] = COOKIE_DELIM_DATE;
2059 s30tob64(txn->cookie_first_date ?
2060 txn->cookie_first_date >> 2 :
2061 (date.tv_sec+3) >> 2,
2062 trash.area + trash.data);
2063 trash.data += 5;
2064 }
2065 }
2066 chunk_appendf(&trash, "; path=/");
2067 }
2068
2069 if (s->be->cookie_domain)
2070 chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain);
2071
2072 if (s->be->ck_opts & PR_CK_HTTPONLY)
2073 chunk_appendf(&trash, "; HttpOnly");
2074
2075 if (s->be->ck_opts & PR_CK_SECURE)
2076 chunk_appendf(&trash, "; Secure");
2077
Christopher Faulet2f533902020-01-21 11:06:48 +01002078 if (s->be->cookie_attrs)
2079 chunk_appendf(&trash, "; %s", s->be->cookie_attrs);
2080
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002081 if (unlikely(!http_add_header(htx, ist("Set-Cookie"), ist2(trash.area, trash.data))))
Christopher Fauletb8a53712019-12-16 11:29:38 +01002082 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002083
2084 txn->flags &= ~TX_SCK_MASK;
2085 if (__objt_server(s->target)->cookie && (s->flags & SF_DIRECT))
2086 /* the server did not change, only the date was updated */
2087 txn->flags |= TX_SCK_UPDATED;
2088 else
2089 txn->flags |= TX_SCK_INSERTED;
2090
2091 /* Here, we will tell an eventual cache on the client side that we don't
2092 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
2093 * Some caches understand the correct form: 'no-cache="set-cookie"', but
2094 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
2095 */
2096 if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
2097
2098 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
2099
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002100 if (unlikely(!http_add_header(htx, ist("Cache-control"), ist("private"))))
Christopher Fauletb8a53712019-12-16 11:29:38 +01002101 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002102 }
2103 }
2104
2105 /*
2106 * Check if result will be cacheable with a cookie.
2107 * We'll block the response if security checks have caught
2108 * nasty things such as a cacheable cookie.
2109 */
2110 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
2111 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
2112 (s->be->options & PR_O_CHK_CACHE)) {
2113 /* we're in presence of a cacheable response containing
2114 * a set-cookie header. We'll block it as requested by
2115 * the 'checkcache' option, and send an alert.
2116 */
Christopher Faulete0768eb2018-10-03 16:38:02 +02002117 ha_alert("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>");
2119 send_log(s->be, LOG_ALERT,
2120 "Blocking cacheable cookie in response from instance %s, server %s.\n",
2121 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
Christopher Fauletb8a53712019-12-16 11:29:38 +01002122 goto deny;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002123 }
2124
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002125 end:
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002126 /*
2127 * Evaluate after-response rules before forwarding the response. rules
2128 * from the backend are evaluated first, then one from the frontend if
2129 * it differs.
2130 */
2131 if (!http_eval_after_res_rules(s))
2132 goto return_int_err;
2133
Christopher Faulete0768eb2018-10-03 16:38:02 +02002134 /* Always enter in the body analyzer */
2135 rep->analysers &= ~AN_RES_FLT_XFER_DATA;
2136 rep->analysers |= AN_RES_HTTP_XFER_BODY;
2137
2138 /* if the user wants to log as soon as possible, without counting
2139 * bytes from the server, then this is the right moment. We have
2140 * to temporarily assign bytes_out to log what we currently have.
2141 */
2142 if (!LIST_ISEMPTY(&sess->fe->logformat) && !(s->logs.logwait & LW_BYTES)) {
2143 s->logs.t_close = s->logs.t_data; /* to get a valid end date */
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002144 s->logs.bytes_out = htx->data;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002145 s->do_log(s);
2146 s->logs.bytes_out = 0;
2147 }
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002148
Christopher Fauletb8a53712019-12-16 11:29:38 +01002149 done:
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002150 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002151 rep->analysers &= ~an_bit;
2152 rep->analyse_exp = TICK_ETERNITY;
2153 return 1;
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002154
Christopher Fauletb8a53712019-12-16 11:29:38 +01002155 deny:
Christopher Fauletb8a53712019-12-16 11:29:38 +01002156 _HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_resp, 1);
Christopher Fauleta08546b2019-12-16 16:07:34 +01002157 _HA_ATOMIC_ADD(&s->be->be_counters.denied_resp, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002158 if (sess->listener->counters)
2159 _HA_ATOMIC_ADD(&sess->listener->counters->denied_resp, 1);
Christopher Fauleta08546b2019-12-16 16:07:34 +01002160 if (objt_server(s->target))
2161 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.denied_resp, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002162 goto return_prx_err;
2163
2164 return_int_err:
2165 txn->status = 500;
2166 if (!(s->flags & SF_ERR_MASK))
2167 s->flags |= SF_ERR_INTERNAL;
Christopher Fauletcff0f732019-12-16 16:13:44 +01002168 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002169 _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1);
2170 if (objt_server(s->target))
2171 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01002172 if (objt_server(s->target))
2173 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002174 goto return_prx_err;
2175
2176 return_bad_res:
2177 txn->status = 502;
Christopher Fauleta20a6532020-02-05 10:16:41 +01002178 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
2179 if (objt_server(s->target)) {
2180 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
2181 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_RSP);
2182 }
Christopher Fauletb8a53712019-12-16 11:29:38 +01002183 /* fall through */
2184
2185 return_prx_err:
2186 http_reply_and_close(s, txn->status, http_error_message(s));
2187 /* fall through */
2188
2189 return_prx_cond:
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002190 s->logs.t_data = -1; /* was not a valid response */
2191 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002192
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002193 if (!(s->flags & SF_ERR_MASK))
2194 s->flags |= SF_ERR_PRXCOND;
2195 if (!(s->flags & SF_FINST_MASK))
2196 s->flags |= SF_FINST_H;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002197
Christopher Faulete58c0002020-03-02 16:21:01 +01002198 rep->analysers &= AN_RES_FLT_END;
2199 s->req.analysers &= AN_REQ_FLT_END;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002200 rep->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002201 DBG_TRACE_DEVEL("leaving on error",
2202 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002203 return 0;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002204
2205 return_prx_yield:
2206 channel_dont_close(rep);
2207 DBG_TRACE_DEVEL("waiting for more data",
2208 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
2209 return 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002210}
2211
2212/* This function is an analyser which forwards response body (including chunk
2213 * sizes if any). It is called as soon as we must forward, even if we forward
2214 * zero byte. The only situation where it must not be called is when we're in
2215 * tunnel mode and we want to forward till the close. It's used both to forward
2216 * remaining data and to resync after end of body. It expects the msg_state to
2217 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
2218 * read more data, or 1 once we can go on with next request or end the stream.
2219 *
2220 * It is capable of compressing response data both in content-length mode and
2221 * in chunked mode. The state machines follows different flows depending on
2222 * whether content-length and chunked modes are used, since there are no
2223 * trailers in content-length :
2224 *
2225 * chk-mode cl-mode
2226 * ,----- BODY -----.
2227 * / \
2228 * V size > 0 V chk-mode
2229 * .--> SIZE -------------> DATA -------------> CRLF
2230 * | | size == 0 | last byte |
2231 * | v final crlf v inspected |
2232 * | TRAILERS -----------> DONE |
2233 * | |
2234 * `----------------------------------------------'
2235 *
2236 * Compression only happens in the DATA state, and must be flushed in final
2237 * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding
2238 * is performed at once on final states for all bytes parsed, or when leaving
2239 * on missing data.
2240 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002241int http_response_forward_body(struct stream *s, struct channel *res, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +02002242{
2243 struct session *sess = s->sess;
2244 struct http_txn *txn = s->txn;
2245 struct http_msg *msg = &s->txn->rsp;
Christopher Faulet9768c262018-10-22 09:34:31 +02002246 struct htx *htx;
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002247 int ret;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002248
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002249 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002250
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002251 htx = htxbuf(&res->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002252
Christopher Fauletb9a92f32019-09-09 10:15:21 +02002253 if (htx->flags & HTX_FL_PARSING_ERROR)
2254 goto return_bad_res;
2255 if (htx->flags & HTX_FL_PROCESSING_ERROR)
2256 goto return_int_err;
2257
Christopher Faulete0768eb2018-10-03 16:38:02 +02002258 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Christopher Fauletf2824e62018-10-01 12:12:37 +02002259 ((res->flags & CF_SHUTW) && (res->to_forward || co_data(res)))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02002260 /* Output closed while we were sending data. We must abort and
2261 * wake the other side up.
2262 */
Christopher Faulete0768eb2018-10-03 16:38:02 +02002263 msg->msg_state = HTTP_MSG_ERROR;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002264 http_end_response(s);
2265 http_end_request(s);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002266 DBG_TRACE_DEVEL("leaving on error",
2267 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002268 return 1;
2269 }
2270
Christopher Faulet9768c262018-10-22 09:34:31 +02002271 if (msg->msg_state == HTTP_MSG_BODY)
2272 msg->msg_state = HTTP_MSG_DATA;
2273
Christopher Faulete0768eb2018-10-03 16:38:02 +02002274 /* in most states, we should abort in case of early close */
2275 channel_auto_close(res);
2276
Christopher Faulete0768eb2018-10-03 16:38:02 +02002277 if (res->to_forward) {
Christopher Faulet66af0b22019-03-22 14:54:52 +01002278 if (res->to_forward == CHN_INFINITE_FORWARD) {
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002279 if (res->flags & CF_EOI)
2280 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet66af0b22019-03-22 14:54:52 +01002281 }
2282 else {
2283 /* We can't process the buffer's contents yet */
2284 res->flags |= CF_WAKE_WRITE;
2285 goto missing_data_or_waiting;
2286 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02002287 }
2288
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002289 if (msg->msg_state >= HTTP_MSG_ENDING)
2290 goto ending;
2291
2292 if ((txn->meth == HTTP_METH_CONNECT && txn->status == 200) || txn->status == 101 ||
2293 (!(msg->flags & HTTP_MSGF_XFER_LEN) && !HAS_RSP_DATA_FILTERS(s))) {
2294 msg->msg_state = HTTP_MSG_ENDING;
2295 goto ending;
2296 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002297
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002298 /* Forward input data. We get it by removing all outgoing data not
2299 * forwarded yet from HTX data size. If there are some data filters, we
2300 * let them decide the amount of data to forward.
Christopher Faulet9768c262018-10-22 09:34:31 +02002301 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002302 if (HAS_RSP_DATA_FILTERS(s)) {
2303 ret = flt_http_payload(s, msg, htx->data);
2304 if (ret < 0)
2305 goto return_bad_res;
Christopher Faulet421e7692019-06-13 11:16:45 +02002306 c_adv(res, ret);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002307 }
2308 else {
Christopher Faulet421e7692019-06-13 11:16:45 +02002309 c_adv(res, htx->data - co_data(res));
Christopher Faulet66af0b22019-03-22 14:54:52 +01002310 if (msg->flags & HTTP_MSGF_XFER_LEN)
2311 channel_htx_forward_forever(res, htx);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002312 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002313
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002314 if (htx->data != co_data(res))
2315 goto missing_data_or_waiting;
2316
2317 if (!(msg->flags & HTTP_MSGF_XFER_LEN) && res->flags & CF_SHUTR) {
2318 msg->msg_state = HTTP_MSG_ENDING;
2319 goto ending;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002320 }
2321
Christopher Faulet9768c262018-10-22 09:34:31 +02002322 /* Check if the end-of-message is reached and if so, switch the message
Christopher Fauletd20fdb02019-06-13 16:43:22 +02002323 * in HTTP_MSG_ENDING state. Then if all data was marked to be
2324 * forwarded, set the state to HTTP_MSG_DONE.
Christopher Faulet9768c262018-10-22 09:34:31 +02002325 */
2326 if (htx_get_tail_type(htx) != HTX_BLK_EOM)
2327 goto missing_data_or_waiting;
2328
Christopher Fauletd20fdb02019-06-13 16:43:22 +02002329 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet9768c262018-10-22 09:34:31 +02002330
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002331 ending:
2332 /* other states, ENDING...TUNNEL */
2333 if (msg->msg_state >= HTTP_MSG_DONE)
2334 goto done;
Christopher Faulet9768c262018-10-22 09:34:31 +02002335
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002336 if (HAS_RSP_DATA_FILTERS(s)) {
2337 ret = flt_http_end(s, msg);
2338 if (ret <= 0) {
2339 if (!ret)
2340 goto missing_data_or_waiting;
2341 goto return_bad_res;
2342 }
2343 }
2344
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002345 if ((txn->meth == HTTP_METH_CONNECT && txn->status == 200) || txn->status == 101 ||
2346 !(msg->flags & HTTP_MSGF_XFER_LEN)) {
2347 msg->msg_state = HTTP_MSG_TUNNEL;
2348 goto ending;
2349 }
2350 else {
2351 msg->msg_state = HTTP_MSG_DONE;
2352 res->to_forward = 0;
2353 }
2354
2355 done:
2356
2357 channel_dont_close(res);
2358
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002359 http_end_response(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002360 if (!(res->analysers & an_bit)) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002361 http_end_request(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002362 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
2363 if (res->flags & CF_SHUTW) {
2364 /* response errors are most likely due to the
2365 * client aborting the transfer. */
Christopher Faulet93e02d82019-03-08 14:18:50 +01002366 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002367 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02002368 goto return_bad_res;
2369 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002370 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002371 return 1;
2372 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002373 DBG_TRACE_DEVEL("waiting for the end of the HTTP txn",
2374 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002375 return 0;
2376
2377 missing_data_or_waiting:
2378 if (res->flags & CF_SHUTW)
Christopher Faulet93e02d82019-03-08 14:18:50 +01002379 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002380
2381 /* stop waiting for data if the input is closed before the end. If the
2382 * client side was already closed, it means that the client has aborted,
2383 * so we don't want to count this as a server abort. Otherwise it's a
2384 * server abort.
2385 */
Christopher Fauletd20fdb02019-06-13 16:43:22 +02002386 if (msg->msg_state < HTTP_MSG_ENDING && res->flags & CF_SHUTR) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02002387 if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))
Christopher Faulet93e02d82019-03-08 14:18:50 +01002388 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002389 /* If we have some pending data, we continue the processing */
Christopher Faulet93e02d82019-03-08 14:18:50 +01002390 if (htx_is_empty(htx))
2391 goto return_srv_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002392 }
2393
Christopher Faulete0768eb2018-10-03 16:38:02 +02002394 /* When TE: chunked is used, we need to get there again to parse
2395 * remaining chunks even if the server has closed, so we don't want to
Christopher Faulet9768c262018-10-22 09:34:31 +02002396 * set CF_DONTCLOSE. Similarly when there is a content-leng or if there
2397 * are filters registered on the stream, we don't want to forward a
2398 * close
Christopher Faulete0768eb2018-10-03 16:38:02 +02002399 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002400 if ((msg->flags & HTTP_MSGF_XFER_LEN) || HAS_RSP_DATA_FILTERS(s))
Christopher Faulete0768eb2018-10-03 16:38:02 +02002401 channel_dont_close(res);
2402
2403 /* We know that more data are expected, but we couldn't send more that
2404 * what we did. So we always set the CF_EXPECT_MORE flag so that the
2405 * system knows it must not set a PUSH on this first part. Interactive
2406 * modes are already handled by the stream sock layer. We must not do
2407 * this in content-length mode because it could present the MSG_MORE
2408 * flag with the last block of forwarded data, which would cause an
2409 * additional delay to be observed by the receiver.
2410 */
2411 if ((msg->flags & HTTP_MSGF_TE_CHNK) || (msg->flags & HTTP_MSGF_COMPRESSING))
2412 res->flags |= CF_EXPECT_MORE;
2413
2414 /* the stream handler will take care of timeouts and errors */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002415 DBG_TRACE_DEVEL("waiting for more data to forward",
2416 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002417 return 0;
2418
Christopher Faulet93e02d82019-03-08 14:18:50 +01002419 return_srv_abort:
2420 _HA_ATOMIC_ADD(&sess->fe->fe_counters.srv_aborts, 1);
2421 _HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01002422 if (sess->listener->counters)
2423 _HA_ATOMIC_ADD(&sess->listener->counters->srv_aborts, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002424 if (objt_server(s->target))
Christopher Fauletcff0f732019-12-16 16:13:44 +01002425 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.srv_aborts, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002426 if (!(s->flags & SF_ERR_MASK))
2427 s->flags |= SF_ERR_SRVCL;
2428 goto return_error;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002429
Christopher Faulet93e02d82019-03-08 14:18:50 +01002430 return_cli_abort:
2431 _HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
2432 _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01002433 if (sess->listener->counters)
2434 _HA_ATOMIC_ADD(&sess->listener->counters->cli_aborts, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002435 if (objt_server(s->target))
Christopher Fauletcff0f732019-12-16 16:13:44 +01002436 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.cli_aborts, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002437 if (!(s->flags & SF_ERR_MASK))
2438 s->flags |= SF_ERR_CLICL;
2439 goto return_error;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002440
Christopher Fauletb9a92f32019-09-09 10:15:21 +02002441 return_int_err:
Christopher Fauletcff0f732019-12-16 16:13:44 +01002442 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002443 _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01002444 if (sess->listener->counters)
2445 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002446 if (objt_server(s->target))
2447 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.internal_errors, 1);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02002448 if (!(s->flags & SF_ERR_MASK))
2449 s->flags |= SF_ERR_INTERNAL;
2450 goto return_error;
2451
Christopher Faulet93e02d82019-03-08 14:18:50 +01002452 return_bad_res:
2453 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
2454 if (objt_server(s->target)) {
Christopher Fauletcff0f732019-12-16 16:13:44 +01002455 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002456 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_RSP);
2457 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02002458 if (!(s->flags & SF_ERR_MASK))
Christopher Faulet93e02d82019-03-08 14:18:50 +01002459 s->flags |= SF_ERR_SRVCL;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002460 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +02002461
Christopher Faulet93e02d82019-03-08 14:18:50 +01002462 return_error:
Christopher Faulete0768eb2018-10-03 16:38:02 +02002463 /* don't send any error message as we're in the body */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002464 http_reply_and_close(s, txn->status, NULL);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002465 res->analysers &= AN_RES_FLT_END;
2466 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 +02002467 if (!(s->flags & SF_FINST_MASK))
2468 s->flags |= SF_FINST_D;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002469 DBG_TRACE_DEVEL("leaving on error",
2470 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002471 return 0;
2472}
2473
Christopher Fauletf2824e62018-10-01 12:12:37 +02002474/* Perform an HTTP redirect based on the information in <rule>. The function
Christopher Faulet99daf282018-11-28 22:58:13 +01002475 * returns zero on success, or zero in case of a, irrecoverable error such
Christopher Fauletf2824e62018-10-01 12:12:37 +02002476 * as too large a request to build a valid response.
2477 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002478int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn)
Christopher Fauletf2824e62018-10-01 12:12:37 +02002479{
Christopher Faulet99daf282018-11-28 22:58:13 +01002480 struct channel *req = &s->req;
2481 struct channel *res = &s->res;
2482 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01002483 struct htx_sl *sl;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002484 struct buffer *chunk;
Christopher Faulet99daf282018-11-28 22:58:13 +01002485 struct ist status, reason, location;
2486 unsigned int flags;
Christopher Faulet08e66462019-05-23 16:44:59 +02002487 int close = 0; /* Try to keep the connection alive byt default */
Christopher Fauletf2824e62018-10-01 12:12:37 +02002488
2489 chunk = alloc_trash_chunk();
Christopher Fauletb8a53712019-12-16 11:29:38 +01002490 if (!chunk) {
2491 if (!(s->flags & SF_ERR_MASK))
2492 s->flags |= SF_ERR_RESOURCE;
Christopher Faulet99daf282018-11-28 22:58:13 +01002493 goto fail;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002494 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002495
Christopher Faulet99daf282018-11-28 22:58:13 +01002496 /*
2497 * Create the location
2498 */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002499 htx = htxbuf(&req->buf);
Christopher Fauletf2824e62018-10-01 12:12:37 +02002500 switch(rule->type) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002501 case REDIRECT_TYPE_SCHEME: {
2502 struct http_hdr_ctx ctx;
2503 struct ist path, host;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002504
Christopher Faulet99daf282018-11-28 22:58:13 +01002505 host = ist("");
2506 ctx.blk = NULL;
2507 if (http_find_header(htx, ist("Host"), &ctx, 0))
2508 host = ctx.value;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002509
Christopher Faulet297fbb42019-05-13 14:41:27 +02002510 sl = http_get_stline(htx);
Christopher Faulet99daf282018-11-28 22:58:13 +01002511 path = http_get_path(htx_sl_req_uri(sl));
2512 /* build message using path */
Tim Duesterhused526372020-03-05 17:56:33 +01002513 if (isttest(path)) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002514 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2515 int qs = 0;
2516 while (qs < path.len) {
2517 if (*(path.ptr + qs) == '?') {
2518 path.len = qs;
2519 break;
2520 }
2521 qs++;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002522 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002523 }
2524 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002525 else
2526 path = ist("/");
Christopher Fauletf2824e62018-10-01 12:12:37 +02002527
Christopher Faulet99daf282018-11-28 22:58:13 +01002528 if (rule->rdr_str) { /* this is an old "redirect" rule */
2529 /* add scheme */
2530 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2531 goto fail;
2532 }
2533 else {
2534 /* add scheme with executing log format */
2535 chunk->data += build_logline(s, chunk->area + chunk->data,
2536 chunk->size - chunk->data,
2537 &rule->rdr_fmt);
2538 }
2539 /* add "://" + host + path */
2540 if (!chunk_memcat(chunk, "://", 3) ||
2541 !chunk_memcat(chunk, host.ptr, host.len) ||
2542 !chunk_memcat(chunk, path.ptr, path.len))
2543 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002544
Christopher Faulet99daf282018-11-28 22:58:13 +01002545 /* append a slash at the end of the location if needed and missing */
2546 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
2547 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
2548 if (chunk->data + 1 >= chunk->size)
2549 goto fail;
2550 chunk->area[chunk->data++] = '/';
2551 }
2552 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002553 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002554
Christopher Faulet99daf282018-11-28 22:58:13 +01002555 case REDIRECT_TYPE_PREFIX: {
2556 struct ist path;
2557
Christopher Faulet297fbb42019-05-13 14:41:27 +02002558 sl = http_get_stline(htx);
Christopher Faulet99daf282018-11-28 22:58:13 +01002559 path = http_get_path(htx_sl_req_uri(sl));
2560 /* build message using path */
Tim Duesterhused526372020-03-05 17:56:33 +01002561 if (isttest(path)) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002562 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2563 int qs = 0;
2564 while (qs < path.len) {
2565 if (*(path.ptr + qs) == '?') {
2566 path.len = qs;
2567 break;
2568 }
2569 qs++;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002570 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002571 }
2572 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002573 else
2574 path = ist("/");
Christopher Fauletf2824e62018-10-01 12:12:37 +02002575
Christopher Faulet99daf282018-11-28 22:58:13 +01002576 if (rule->rdr_str) { /* this is an old "redirect" rule */
2577 /* add prefix. Note that if prefix == "/", we don't want to
2578 * add anything, otherwise it makes it hard for the user to
2579 * configure a self-redirection.
2580 */
2581 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
2582 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2583 goto fail;
2584 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002585 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002586 else {
2587 /* add prefix with executing log format */
2588 chunk->data += build_logline(s, chunk->area + chunk->data,
2589 chunk->size - chunk->data,
2590 &rule->rdr_fmt);
2591 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002592
Christopher Faulet99daf282018-11-28 22:58:13 +01002593 /* add path */
2594 if (!chunk_memcat(chunk, path.ptr, path.len))
2595 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002596
Christopher Faulet99daf282018-11-28 22:58:13 +01002597 /* append a slash at the end of the location if needed and missing */
2598 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
2599 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
2600 if (chunk->data + 1 >= chunk->size)
2601 goto fail;
2602 chunk->area[chunk->data++] = '/';
2603 }
2604 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002605 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002606 case REDIRECT_TYPE_LOCATION:
2607 default:
2608 if (rule->rdr_str) { /* this is an old "redirect" rule */
2609 /* add location */
2610 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2611 goto fail;
2612 }
2613 else {
2614 /* add location with executing log format */
2615 chunk->data += build_logline(s, chunk->area + chunk->data,
2616 chunk->size - chunk->data,
2617 &rule->rdr_fmt);
2618 }
2619 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002620 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002621 location = ist2(chunk->area, chunk->data);
2622
2623 /*
2624 * Create the 30x response
2625 */
2626 switch (rule->code) {
2627 case 308:
2628 status = ist("308");
2629 reason = ist("Permanent Redirect");
2630 break;
2631 case 307:
2632 status = ist("307");
2633 reason = ist("Temporary Redirect");
2634 break;
2635 case 303:
2636 status = ist("303");
2637 reason = ist("See Other");
2638 break;
2639 case 301:
2640 status = ist("301");
2641 reason = ist("Moved Permanently");
2642 break;
2643 case 302:
2644 default:
2645 status = ist("302");
2646 reason = ist("Found");
2647 break;
2648 }
2649
Christopher Faulet08e66462019-05-23 16:44:59 +02002650 if (!(txn->req.flags & HTTP_MSGF_BODYLESS) && txn->req.msg_state != HTTP_MSG_DONE)
2651 close = 1;
2652
Christopher Faulet99daf282018-11-28 22:58:13 +01002653 htx = htx_from_buf(&res->buf);
Kevin Zhu96b36392020-01-07 09:42:55 +01002654 /* Trim any possible response */
2655 channel_htx_truncate(&s->res, htx);
Christopher Faulet99daf282018-11-28 22:58:13 +01002656 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
2657 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), status, reason);
2658 if (!sl)
2659 goto fail;
2660 sl->info.res.status = rule->code;
2661 s->txn->status = rule->code;
2662
Christopher Faulet08e66462019-05-23 16:44:59 +02002663 if (close && !htx_add_header(htx, ist("Connection"), ist("close")))
2664 goto fail;
2665
2666 if (!htx_add_header(htx, ist("Content-length"), ist("0")) ||
Christopher Faulet99daf282018-11-28 22:58:13 +01002667 !htx_add_header(htx, ist("Location"), location))
2668 goto fail;
2669
2670 if (rule->code == 302 || rule->code == 303 || rule->code == 307) {
2671 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")))
2672 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002673 }
2674
2675 if (rule->cookie_len) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002676 if (!htx_add_header(htx, ist("Set-Cookie"), ist2(rule->cookie_str, rule->cookie_len)))
2677 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002678 }
2679
Christopher Faulet99daf282018-11-28 22:58:13 +01002680 if (!htx_add_endof(htx, HTX_BLK_EOH) || !htx_add_endof(htx, HTX_BLK_EOM))
2681 goto fail;
2682
Kevin Zhu96b36392020-01-07 09:42:55 +01002683 htx_to_buf(htx, &res->buf);
Christopher Fauleta72a7e42020-01-28 09:28:11 +01002684 if (!http_forward_proxy_resp(s, 1))
2685 goto fail;
Christopher Faulet99daf282018-11-28 22:58:13 +01002686
Christopher Faulet60b33a52020-01-28 09:18:10 +01002687 if (rule->flags & REDIRECT_FLAG_FROM_REQ) {
2688 /* let's log the request time */
2689 s->logs.tv_request = now;
2690 req->analysers &= AN_REQ_FLT_END;
Christopher Faulet99daf282018-11-28 22:58:13 +01002691
Christopher Faulet60b33a52020-01-28 09:18:10 +01002692 if (s->sess->fe == s->be) /* report it if the request was intercepted by the frontend */
2693 _HA_ATOMIC_ADD(&s->sess->fe->fe_counters.intercepted_req, 1);
2694 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002695
2696 if (!(s->flags & SF_ERR_MASK))
2697 s->flags |= SF_ERR_LOCAL;
2698 if (!(s->flags & SF_FINST_MASK))
Christopher Faulet60b33a52020-01-28 09:18:10 +01002699 s->flags |= ((rule->flags & REDIRECT_FLAG_FROM_REQ) ? SF_FINST_R : SF_FINST_H);
Christopher Fauletf2824e62018-10-01 12:12:37 +02002700
Christopher Faulet99daf282018-11-28 22:58:13 +01002701 free_trash_chunk(chunk);
2702 return 1;
2703
2704 fail:
2705 /* If an error occurred, remove the incomplete HTTP response from the
2706 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01002707 channel_htx_truncate(res, htxbuf(&res->buf));
Christopher Fauletf2824e62018-10-01 12:12:37 +02002708 free_trash_chunk(chunk);
Christopher Faulet99daf282018-11-28 22:58:13 +01002709 return 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002710}
2711
Christopher Faulet92d34fe2019-12-17 09:20:34 +01002712/* Replace all headers matching the name <name>. The header value is replaced if
2713 * it matches the regex <re>. <str> is used for the replacement. If <full> is
2714 * set to 1, the full-line is matched and replaced. Otherwise, comma-separated
2715 * values are evaluated one by one. It returns 0 on success and -1 on error.
2716 */
2717int http_replace_hdrs(struct stream* s, struct htx *htx, struct ist name,
2718 const char *str, struct my_regex *re, int full)
Christopher Faulet72333522018-10-24 11:25:02 +02002719{
2720 struct http_hdr_ctx ctx;
2721 struct buffer *output = get_trash_chunk();
2722
Christopher Faulet72333522018-10-24 11:25:02 +02002723 ctx.blk = NULL;
Christopher Faulet92d34fe2019-12-17 09:20:34 +01002724 while (http_find_header(htx, name, &ctx, full)) {
Christopher Faulet72333522018-10-24 11:25:02 +02002725 if (!regex_exec_match2(re, ctx.value.ptr, ctx.value.len, MAX_MATCH, pmatch, 0))
2726 continue;
2727
2728 output->data = exp_replace(output->area, output->size, ctx.value.ptr, str, pmatch);
2729 if (output->data == -1)
2730 return -1;
2731 if (!http_replace_header_value(htx, &ctx, ist2(output->area, output->data)))
2732 return -1;
2733 }
2734 return 0;
2735}
2736
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002737/* This function executes one of the set-{method,path,query,uri} actions. It
2738 * takes the string from the variable 'replace' with length 'len', then modifies
2739 * the relevant part of the request line accordingly. Then it updates various
2740 * pointers to the next elements which were moved, and the total buffer length.
2741 * It finds the action to be performed in p[2], previously filled by function
2742 * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal
2743 * error, though this can be revisited when this code is finally exploited.
2744 *
2745 * 'action' can be '0' to replace method, '1' to replace path, '2' to replace
2746 * query string and 3 to replace uri.
2747 *
2748 * In query string case, the mark question '?' must be set at the start of the
2749 * string by the caller, event if the replacement query string is empty.
2750 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002751int http_req_replace_stline(int action, const char *replace, int len,
2752 struct proxy *px, struct stream *s)
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002753{
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002754 struct htx *htx = htxbuf(&s->req.buf);
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002755
2756 switch (action) {
2757 case 0: // method
2758 if (!http_replace_req_meth(htx, ist2(replace, len)))
2759 return -1;
2760 break;
2761
2762 case 1: // path
2763 if (!http_replace_req_path(htx, ist2(replace, len)))
2764 return -1;
2765 break;
2766
2767 case 2: // query
2768 if (!http_replace_req_query(htx, ist2(replace, len)))
2769 return -1;
2770 break;
2771
2772 case 3: // uri
2773 if (!http_replace_req_uri(htx, ist2(replace, len)))
2774 return -1;
2775 break;
2776
2777 default:
2778 return -1;
2779 }
2780 return 0;
2781}
2782
2783/* This function replace the HTTP status code and the associated message. The
Christopher Faulete00d06c2019-12-16 17:18:42 +01002784 * variable <status> contains the new status code. This function never fails. It
2785 * returns 0 in case of success, -1 in case of internal error.
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002786 */
Christopher Faulet96bff762019-12-17 13:46:18 +01002787int http_res_set_status(unsigned int status, struct ist reason, struct stream *s)
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002788{
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002789 struct htx *htx = htxbuf(&s->res.buf);
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002790 char *res;
2791
2792 chunk_reset(&trash);
2793 res = ultoa_o(status, trash.area, trash.size);
2794 trash.data = res - trash.area;
2795
2796 /* Do we have a custom reason format string? */
Tim Duesterhuse296d3e2020-03-05 17:56:31 +01002797 if (!isttest(reason)) {
Christopher Faulet96bff762019-12-17 13:46:18 +01002798 const char *str = http_get_reason(status);
2799 reason = ist2(str, strlen(str));
2800 }
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002801
Christopher Faulete00d06c2019-12-16 17:18:42 +01002802 if (!http_replace_res_status(htx, ist2(trash.area, trash.data)))
2803 return -1;
Christopher Faulet96bff762019-12-17 13:46:18 +01002804 if (!http_replace_res_reason(htx, reason))
Christopher Faulete00d06c2019-12-16 17:18:42 +01002805 return -1;
2806 return 0;
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002807}
2808
Christopher Faulet3e964192018-10-24 11:39:23 +02002809/* Executes the http-request rules <rules> for stream <s>, proxy <px> and
2810 * transaction <txn>. Returns the verdict of the first rule that prevents
2811 * further processing of the request (auth, deny, ...), and defaults to
2812 * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or
2813 * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT
2814 * on txn->flags if it encounters a tarpit rule. If <deny_status> is not NULL
2815 * and a deny/tarpit rule is matched, it will be filled with this rule's deny
2816 * status.
2817 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002818static enum rule_result http_req_get_intercept_rule(struct proxy *px, struct list *rules,
Christopher Fauletb58f62b2020-01-13 16:40:13 +01002819 struct stream *s)
Christopher Faulet3e964192018-10-24 11:39:23 +02002820{
2821 struct session *sess = strm_sess(s);
2822 struct http_txn *txn = s->txn;
2823 struct htx *htx;
Christopher Faulet3e964192018-10-24 11:39:23 +02002824 struct act_rule *rule;
2825 struct http_hdr_ctx ctx;
2826 const char *auth_realm;
Christopher Faulet3e964192018-10-24 11:39:23 +02002827 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002828 int act_opts = 0;
Christopher Faulet3e964192018-10-24 11:39:23 +02002829
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002830 htx = htxbuf(&s->req.buf);
Christopher Faulet3e964192018-10-24 11:39:23 +02002831
2832 /* If "the current_rule_list" match the executed rule list, we are in
2833 * resume condition. If a resume is needed it is always in the action
2834 * and never in the ACL or converters. In this case, we initialise the
2835 * current rule, and go to the action execution point.
2836 */
2837 if (s->current_rule) {
2838 rule = s->current_rule;
2839 s->current_rule = NULL;
2840 if (s->current_rule_list == rules)
2841 goto resume_execution;
2842 }
2843 s->current_rule_list = rules;
2844
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002845 /* start the ruleset evaluation in strict mode */
2846 txn->req.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01002847
Christopher Faulet3e964192018-10-24 11:39:23 +02002848 list_for_each_entry(rule, rules, list) {
2849 /* check optional condition */
2850 if (rule->cond) {
2851 int ret;
2852
2853 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
2854 ret = acl_pass(ret);
2855
2856 if (rule->cond->pol == ACL_COND_UNLESS)
2857 ret = !ret;
2858
2859 if (!ret) /* condition not matched */
2860 continue;
2861 }
2862
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002863 act_opts |= ACT_OPT_FIRST;
Christopher Faulet3e964192018-10-24 11:39:23 +02002864 resume_execution:
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002865 /* Always call the action function if defined */
2866 if (rule->action_ptr) {
2867 if ((s->req.flags & CF_READ_ERROR) ||
2868 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
2869 (px->options & PR_O_ABRT_CLOSE)))
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002870 act_opts |= ACT_OPT_FINAL;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002871
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002872 switch (rule->action_ptr(rule, px, sess, s, act_opts)) {
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002873 case ACT_RET_CONT:
2874 break;
2875 case ACT_RET_STOP:
2876 rule_ret = HTTP_RULE_RES_STOP;
2877 goto end;
2878 case ACT_RET_YIELD:
2879 s->current_rule = rule;
2880 rule_ret = HTTP_RULE_RES_YIELD;
2881 goto end;
2882 case ACT_RET_ERR:
2883 rule_ret = HTTP_RULE_RES_ERROR;
2884 goto end;
2885 case ACT_RET_DONE:
2886 rule_ret = HTTP_RULE_RES_DONE;
2887 goto end;
2888 case ACT_RET_DENY:
Christopher Fauletb58f62b2020-01-13 16:40:13 +01002889 if (txn->status == -1)
2890 txn->status = 403;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002891 rule_ret = HTTP_RULE_RES_DENY;
2892 goto end;
2893 case ACT_RET_ABRT:
2894 rule_ret = HTTP_RULE_RES_ABRT;
2895 goto end;
2896 case ACT_RET_INV:
2897 rule_ret = HTTP_RULE_RES_BADREQ;
2898 goto end;
2899 }
2900 continue; /* eval the next rule */
2901 }
2902
2903 /* If not action function defined, check for known actions */
Christopher Faulet3e964192018-10-24 11:39:23 +02002904 switch (rule->action) {
2905 case ACT_ACTION_ALLOW:
2906 rule_ret = HTTP_RULE_RES_STOP;
2907 goto end;
2908
2909 case ACT_ACTION_DENY:
Christopher Faulet554c0eb2020-01-14 12:00:28 +01002910 txn->status = rule->arg.http_deny.status;
2911 if (rule->arg.http_deny.errmsg)
2912 txn->errmsg = rule->arg.http_deny.errmsg;
Christopher Faulet3e964192018-10-24 11:39:23 +02002913 rule_ret = HTTP_RULE_RES_DENY;
2914 goto end;
2915
2916 case ACT_HTTP_REQ_TARPIT:
2917 txn->flags |= TX_CLTARPIT;
Christopher Faulet554c0eb2020-01-14 12:00:28 +01002918 txn->status = rule->arg.http_deny.status;
2919 if (rule->arg.http_deny.errmsg)
2920 txn->errmsg = rule->arg.http_deny.errmsg;
Christopher Faulet3e964192018-10-24 11:39:23 +02002921 rule_ret = HTTP_RULE_RES_DENY;
2922 goto end;
2923
2924 case ACT_HTTP_REQ_AUTH:
Christopher Faulet3e964192018-10-24 11:39:23 +02002925 /* Auth might be performed on regular http-req rules as well as on stats */
Christopher Faulet96bff762019-12-17 13:46:18 +01002926 auth_realm = rule->arg.http.str.ptr;
Christopher Faulet3e964192018-10-24 11:39:23 +02002927 if (!auth_realm) {
2928 if (px->uri_auth && rules == &px->uri_auth->http_req_rules)
2929 auth_realm = STATS_DEFAULT_REALM;
2930 else
2931 auth_realm = px->id;
2932 }
2933 /* send 401/407 depending on whether we use a proxy or not. We still
2934 * count one error, because normal browsing won't significantly
2935 * increase the counter but brute force attempts will.
2936 */
Christopher Faulet3e964192018-10-24 11:39:23 +02002937 rule_ret = HTTP_RULE_RES_ABRT;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002938 if (http_reply_40x_unauthorized(s, auth_realm) == -1)
Christopher Faulet3a26bee2019-12-16 12:47:40 +01002939 rule_ret = HTTP_RULE_RES_ERROR;
Christopher Faulet12c51e22018-11-28 15:59:42 +01002940 stream_inc_http_err_ctr(s);
Christopher Faulet3e964192018-10-24 11:39:23 +02002941 goto end;
2942
2943 case ACT_HTTP_REDIR:
Christopher Faulet90d22a82020-03-06 11:18:39 +01002944 rule_ret = HTTP_RULE_RES_ABRT;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002945 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
Christopher Faulet3a26bee2019-12-16 12:47:40 +01002946 rule_ret = HTTP_RULE_RES_ERROR;
Christopher Faulet3e964192018-10-24 11:39:23 +02002947 goto end;
2948
2949 case ACT_HTTP_SET_NICE:
Christopher Faulet96bff762019-12-17 13:46:18 +01002950 s->task->nice = rule->arg.http.i;
Christopher Faulet3e964192018-10-24 11:39:23 +02002951 break;
2952
2953 case ACT_HTTP_SET_TOS:
Christopher Faulet96bff762019-12-17 13:46:18 +01002954 conn_set_tos(objt_conn(sess->origin), rule->arg.http.i);
Christopher Faulet3e964192018-10-24 11:39:23 +02002955 break;
2956
2957 case ACT_HTTP_SET_MARK:
Christopher Faulet96bff762019-12-17 13:46:18 +01002958 conn_set_mark(objt_conn(sess->origin), rule->arg.http.i);
Christopher Faulet3e964192018-10-24 11:39:23 +02002959 break;
2960
2961 case ACT_HTTP_SET_LOGL:
Christopher Faulet96bff762019-12-17 13:46:18 +01002962 s->logs.level = rule->arg.http.i;
Christopher Faulet3e964192018-10-24 11:39:23 +02002963 break;
2964
Christopher Faulet3e964192018-10-24 11:39:23 +02002965 case ACT_HTTP_DEL_HDR:
2966 /* remove all occurrences of the header */
2967 ctx.blk = NULL;
Christopher Faulet96bff762019-12-17 13:46:18 +01002968 while (http_find_header(htx, rule->arg.http.str, &ctx, 1))
Christopher Faulet3e964192018-10-24 11:39:23 +02002969 http_remove_header(htx, &ctx);
2970 break;
2971
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002972 /* other flags exists, but normally, they never be matched. */
Christopher Faulet3e964192018-10-24 11:39:23 +02002973 default:
2974 break;
2975 }
2976 }
2977
2978 end:
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002979 /* if the ruleset evaluation is finished reset the strict mode */
Christopher Faulet46f95542019-12-20 10:07:22 +01002980 if (rule_ret != HTTP_RULE_RES_YIELD)
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002981 txn->req.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01002982
Christopher Faulet3e964192018-10-24 11:39:23 +02002983 /* we reached the end of the rules, nothing to report */
2984 return rule_ret;
2985}
2986
2987/* Executes the http-response rules <rules> for stream <s> and proxy <px>. It
2988 * returns one of 5 possible statuses: HTTP_RULE_RES_CONT, HTTP_RULE_RES_STOP,
2989 * HTTP_RULE_RES_DONE, HTTP_RULE_RES_YIELD, or HTTP_RULE_RES_BADREQ. If *CONT
2990 * is returned, the process can continue the evaluation of next rule list. If
2991 * *STOP or *DONE is returned, the process must stop the evaluation. If *BADREQ
2992 * is returned, it means the operation could not be processed and a server error
Christopher Fauleta53abad2020-05-13 08:12:22 +02002993 * must be returned. If *YIELD is returned, the caller must call again the
2994 * function with the same context.
Christopher Faulet3e964192018-10-24 11:39:23 +02002995 */
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 if (txn->status == -1)
3068 txn->status = 502;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01003069 rule_ret = HTTP_RULE_RES_DENY;
3070 goto end;
3071 case ACT_RET_ABRT:
3072 rule_ret = HTTP_RULE_RES_ABRT;
3073 goto end;
3074 case ACT_RET_INV:
3075 rule_ret = HTTP_RULE_RES_BADREQ;
3076 goto end;
3077 }
3078 continue; /* eval the next rule */
3079 }
3080
3081 /* If not action function defined, check for known actions */
Christopher Faulet3e964192018-10-24 11:39:23 +02003082 switch (rule->action) {
3083 case ACT_ACTION_ALLOW:
3084 rule_ret = HTTP_RULE_RES_STOP; /* "allow" rules are OK */
3085 goto end;
3086
3087 case ACT_ACTION_DENY:
Christopher Faulet554c0eb2020-01-14 12:00:28 +01003088 txn->status = rule->arg.http_deny.status;
3089 if (rule->arg.http_deny.errmsg)
3090 txn->errmsg = rule->arg.http_deny.errmsg;
Christopher Faulet3a26bee2019-12-16 12:47:40 +01003091 rule_ret = HTTP_RULE_RES_DENY;
Christopher Faulet3e964192018-10-24 11:39:23 +02003092 goto end;
3093
3094 case ACT_HTTP_SET_NICE:
Christopher Faulet96bff762019-12-17 13:46:18 +01003095 s->task->nice = rule->arg.http.i;
Christopher Faulet3e964192018-10-24 11:39:23 +02003096 break;
3097
3098 case ACT_HTTP_SET_TOS:
Christopher Faulet96bff762019-12-17 13:46:18 +01003099 conn_set_tos(objt_conn(sess->origin), rule->arg.http.i);
Christopher Faulet3e964192018-10-24 11:39:23 +02003100 break;
3101
3102 case ACT_HTTP_SET_MARK:
Christopher Faulet96bff762019-12-17 13:46:18 +01003103 conn_set_mark(objt_conn(sess->origin), rule->arg.http.i);
Christopher Faulet3e964192018-10-24 11:39:23 +02003104 break;
3105
3106 case ACT_HTTP_SET_LOGL:
Christopher Faulet96bff762019-12-17 13:46:18 +01003107 s->logs.level = rule->arg.http.i;
Christopher Faulet3e964192018-10-24 11:39:23 +02003108 break;
3109
Christopher Faulet3e964192018-10-24 11:39:23 +02003110 case ACT_HTTP_DEL_HDR:
3111 /* remove all occurrences of the header */
3112 ctx.blk = NULL;
Christopher Faulet96bff762019-12-17 13:46:18 +01003113 while (http_find_header(htx, rule->arg.http.str, &ctx, 1))
Christopher Faulet3e964192018-10-24 11:39:23 +02003114 http_remove_header(htx, &ctx);
3115 break;
3116
Christopher Faulet3e964192018-10-24 11:39:23 +02003117 case ACT_HTTP_REDIR:
Christopher Faulet49c2a702020-03-06 15:44:37 +01003118 rule_ret = HTTP_RULE_RES_ABRT;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003119 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
Christopher Faulet3a26bee2019-12-16 12:47:40 +01003120 rule_ret = HTTP_RULE_RES_ERROR;
Christopher Faulet3e964192018-10-24 11:39:23 +02003121 goto end;
3122
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01003123 /* other flags exists, but normally, they never be matched. */
Christopher Faulet3e964192018-10-24 11:39:23 +02003124 default:
3125 break;
3126 }
3127 }
3128
3129 end:
Christopher Faulet1aea50e2020-01-17 16:03:53 +01003130 /* if the ruleset evaluation is finished reset the strict mode */
Christopher Faulet46f95542019-12-20 10:07:22 +01003131 if (rule_ret != HTTP_RULE_RES_YIELD)
Christopher Faulet1aea50e2020-01-17 16:03:53 +01003132 txn->rsp.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01003133
Christopher Faulet3e964192018-10-24 11:39:23 +02003134 /* we reached the end of the rules, nothing to report */
3135 return rule_ret;
3136}
3137
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01003138/* Executes backend and frontend http-after-response rules for the stream <s>,
3139 * in that order. it return 1 on success and 0 on error. It is the caller
3140 * responsibility to catch error or ignore it. If it catches it, this function
3141 * may be called a second time, for the internal error.
3142 */
3143int http_eval_after_res_rules(struct stream *s)
3144{
3145 struct session *sess = s->sess;
3146 enum rule_result ret = HTTP_RULE_RES_CONT;
3147
3148 /* prune the request variables if not already done and swap to the response variables. */
3149 if (s->vars_reqres.scope != SCOPE_RES) {
3150 if (!LIST_ISEMPTY(&s->vars_reqres.head))
3151 vars_prune(&s->vars_reqres, s->sess, s);
3152 vars_init(&s->vars_reqres, SCOPE_RES);
3153 }
3154
3155 ret = http_res_get_intercept_rule(s->be, &s->be->http_after_res_rules, s);
3156 if ((ret == HTTP_RULE_RES_CONT || ret == HTTP_RULE_RES_STOP) && sess->fe != s->be)
3157 ret = http_res_get_intercept_rule(sess->fe, &sess->fe->http_after_res_rules, s);
3158
3159 /* All other codes than CONTINUE, STOP or DONE are forbidden */
3160 return (ret == HTTP_RULE_RES_CONT || ret == HTTP_RULE_RES_STOP || ret == HTTP_RULE_RES_DONE);
3161}
3162
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003163/*
3164 * Manage client-side cookie. It can impact performance by about 2% so it is
3165 * desirable to call it only when needed. This code is quite complex because
3166 * of the multiple very crappy and ambiguous syntaxes we have to support. it
3167 * highly recommended not to touch this part without a good reason !
3168 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003169static void http_manage_client_side_cookies(struct stream *s, struct channel *req)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003170{
3171 struct session *sess = s->sess;
3172 struct http_txn *txn = s->txn;
3173 struct htx *htx;
3174 struct http_hdr_ctx ctx;
3175 char *hdr_beg, *hdr_end, *del_from;
3176 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
3177 int preserve_hdr;
3178
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003179 htx = htxbuf(&req->buf);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003180 ctx.blk = NULL;
3181 while (http_find_header(htx, ist("Cookie"), &ctx, 1)) {
Olivier Houchardf0f42382019-07-22 17:43:46 +02003182 int is_first = 1;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003183 del_from = NULL; /* nothing to be deleted */
3184 preserve_hdr = 0; /* assume we may kill the whole header */
3185
3186 /* Now look for cookies. Conforming to RFC2109, we have to support
3187 * attributes whose name begin with a '$', and associate them with
3188 * the right cookie, if we want to delete this cookie.
3189 * So there are 3 cases for each cookie read :
3190 * 1) it's a special attribute, beginning with a '$' : ignore it.
3191 * 2) it's a server id cookie that we *MAY* want to delete : save
3192 * some pointers on it (last semi-colon, beginning of cookie...)
3193 * 3) it's an application cookie : we *MAY* have to delete a previous
3194 * "special" cookie.
3195 * At the end of loop, if a "special" cookie remains, we may have to
3196 * remove it. If no application cookie persists in the header, we
3197 * *MUST* delete it.
3198 *
3199 * Note: RFC2965 is unclear about the processing of spaces around
3200 * the equal sign in the ATTR=VALUE form. A careful inspection of
3201 * the RFC explicitly allows spaces before it, and not within the
3202 * tokens (attrs or values). An inspection of RFC2109 allows that
3203 * too but section 10.1.3 lets one think that spaces may be allowed
3204 * after the equal sign too, resulting in some (rare) buggy
3205 * implementations trying to do that. So let's do what servers do.
3206 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
3207 * allowed quoted strings in values, with any possible character
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003208 * after a backslash, including control chars and delimiters, which
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003209 * causes parsing to become ambiguous. Browsers also allow spaces
3210 * within values even without quotes.
3211 *
3212 * We have to keep multiple pointers in order to support cookie
3213 * removal at the beginning, middle or end of header without
3214 * corrupting the header. All of these headers are valid :
3215 *
3216 * hdr_beg hdr_end
3217 * | |
3218 * v |
3219 * NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3 |
3220 * NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3 v
3221 * NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3
3222 * | | | | | | |
3223 * | | | | | | |
3224 * | | | | | | +--> next
3225 * | | | | | +----> val_end
3226 * | | | | +-----------> val_beg
3227 * | | | +--------------> equal
3228 * | | +----------------> att_end
3229 * | +---------------------> att_beg
3230 * +--------------------------> prev
3231 *
3232 */
3233 hdr_beg = ctx.value.ptr;
3234 hdr_end = hdr_beg + ctx.value.len;
3235 for (prev = hdr_beg; prev < hdr_end; prev = next) {
3236 /* Iterate through all cookies on this line */
3237
3238 /* find att_beg */
3239 att_beg = prev;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003240 if (!is_first)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003241 att_beg++;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003242 is_first = 0;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003243
3244 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
3245 att_beg++;
3246
3247 /* find att_end : this is the first character after the last non
3248 * space before the equal. It may be equal to hdr_end.
3249 */
3250 equal = att_end = att_beg;
3251 while (equal < hdr_end) {
3252 if (*equal == '=' || *equal == ',' || *equal == ';')
3253 break;
3254 if (HTTP_IS_SPHT(*equal++))
3255 continue;
3256 att_end = equal;
3257 }
3258
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003259 /* here, <equal> points to '=', a delimiter or the end. <att_end>
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003260 * is between <att_beg> and <equal>, both may be identical.
3261 */
3262 /* look for end of cookie if there is an equal sign */
3263 if (equal < hdr_end && *equal == '=') {
3264 /* look for the beginning of the value */
3265 val_beg = equal + 1;
3266 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
3267 val_beg++;
3268
3269 /* find the end of the value, respecting quotes */
3270 next = http_find_cookie_value_end(val_beg, hdr_end);
3271
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003272 /* make val_end point to the first white space or delimiter after the value */
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003273 val_end = next;
3274 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
3275 val_end--;
3276 }
3277 else
3278 val_beg = val_end = next = equal;
3279
3280 /* We have nothing to do with attributes beginning with
3281 * '$'. However, they will automatically be removed if a
3282 * header before them is removed, since they're supposed
3283 * to be linked together.
3284 */
3285 if (*att_beg == '$')
3286 continue;
3287
3288 /* Ignore cookies with no equal sign */
3289 if (equal == next) {
3290 /* This is not our cookie, so we must preserve it. But if we already
3291 * scheduled another cookie for removal, we cannot remove the
3292 * complete header, but we can remove the previous block itself.
3293 */
3294 preserve_hdr = 1;
3295 if (del_from != NULL) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003296 int delta = http_del_hdr_value(hdr_beg, hdr_end, &del_from, prev);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003297 val_end += delta;
3298 next += delta;
3299 hdr_end += delta;
3300 prev = del_from;
3301 del_from = NULL;
3302 }
3303 continue;
3304 }
3305
3306 /* if there are spaces around the equal sign, we need to
3307 * strip them otherwise we'll get trouble for cookie captures,
3308 * or even for rewrites. Since this happens extremely rarely,
3309 * it does not hurt performance.
3310 */
3311 if (unlikely(att_end != equal || val_beg > equal + 1)) {
3312 int stripped_before = 0;
3313 int stripped_after = 0;
3314
3315 if (att_end != equal) {
3316 memmove(att_end, equal, hdr_end - equal);
3317 stripped_before = (att_end - equal);
3318 equal += stripped_before;
3319 val_beg += stripped_before;
3320 }
3321
3322 if (val_beg > equal + 1) {
3323 memmove(equal + 1, val_beg, hdr_end + stripped_before - val_beg);
3324 stripped_after = (equal + 1) - val_beg;
3325 val_beg += stripped_after;
3326 stripped_before += stripped_after;
3327 }
3328
3329 val_end += stripped_before;
3330 next += stripped_before;
3331 hdr_end += stripped_before;
3332 }
3333 /* now everything is as on the diagram above */
3334
3335 /* First, let's see if we want to capture this cookie. We check
3336 * that we don't already have a client side cookie, because we
3337 * can only capture one. Also as an optimisation, we ignore
3338 * cookies shorter than the declared name.
3339 */
3340 if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL &&
3341 (val_end - att_beg >= sess->fe->capture_namelen) &&
3342 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
3343 int log_len = val_end - att_beg;
3344
3345 if ((txn->cli_cookie = pool_alloc(pool_head_capture)) == NULL) {
3346 ha_alert("HTTP logging : out of memory.\n");
3347 } else {
3348 if (log_len > sess->fe->capture_len)
3349 log_len = sess->fe->capture_len;
3350 memcpy(txn->cli_cookie, att_beg, log_len);
3351 txn->cli_cookie[log_len] = 0;
3352 }
3353 }
3354
3355 /* Persistence cookies in passive, rewrite or insert mode have the
3356 * following form :
3357 *
3358 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
3359 *
3360 * For cookies in prefix mode, the form is :
3361 *
3362 * Cookie: NAME=SRV~VALUE
3363 */
3364 if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
3365 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
3366 struct server *srv = s->be->srv;
3367 char *delim;
3368
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003369 /* if we're in cookie prefix mode, we'll search the delimiter so that we
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003370 * have the server ID between val_beg and delim, and the original cookie between
3371 * delim+1 and val_end. Otherwise, delim==val_end :
3372 *
3373 * hdr_beg
3374 * |
3375 * v
3376 * NAME=SRV; # in all but prefix modes
3377 * NAME=SRV~OPAQUE ; # in prefix mode
3378 * || || | |+-> next
3379 * || || | +--> val_end
3380 * || || +---------> delim
3381 * || |+------------> val_beg
3382 * || +-------------> att_end = equal
3383 * |+-----------------> att_beg
3384 * +------------------> prev
3385 *
3386 */
3387 if (s->be->ck_opts & PR_CK_PFX) {
3388 for (delim = val_beg; delim < val_end; delim++)
3389 if (*delim == COOKIE_DELIM)
3390 break;
3391 }
3392 else {
3393 char *vbar1;
3394 delim = val_end;
3395 /* Now check if the cookie contains a date field, which would
3396 * appear after a vertical bar ('|') just after the server name
3397 * and before the delimiter.
3398 */
3399 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
3400 if (vbar1) {
3401 /* OK, so left of the bar is the server's cookie and
3402 * right is the last seen date. It is a base64 encoded
3403 * 30-bit value representing the UNIX date since the
3404 * epoch in 4-second quantities.
3405 */
3406 int val;
3407 delim = vbar1++;
3408 if (val_end - vbar1 >= 5) {
3409 val = b64tos30(vbar1);
3410 if (val > 0)
3411 txn->cookie_last_date = val << 2;
3412 }
3413 /* look for a second vertical bar */
3414 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
3415 if (vbar1 && (val_end - vbar1 > 5)) {
3416 val = b64tos30(vbar1 + 1);
3417 if (val > 0)
3418 txn->cookie_first_date = val << 2;
3419 }
3420 }
3421 }
3422
3423 /* if the cookie has an expiration date and the proxy wants to check
3424 * it, then we do that now. We first check if the cookie is too old,
3425 * then only if it has expired. We detect strict overflow because the
3426 * time resolution here is not great (4 seconds). Cookies with dates
3427 * in the future are ignored if their offset is beyond one day. This
3428 * allows an admin to fix timezone issues without expiring everyone
3429 * and at the same time avoids keeping unwanted side effects for too
3430 * long.
3431 */
3432 if (txn->cookie_first_date && s->be->cookie_maxlife &&
3433 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) ||
3434 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
3435 txn->flags &= ~TX_CK_MASK;
3436 txn->flags |= TX_CK_OLD;
3437 delim = val_beg; // let's pretend we have not found the cookie
3438 txn->cookie_first_date = 0;
3439 txn->cookie_last_date = 0;
3440 }
3441 else if (txn->cookie_last_date && s->be->cookie_maxidle &&
3442 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) ||
3443 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
3444 txn->flags &= ~TX_CK_MASK;
3445 txn->flags |= TX_CK_EXPIRED;
3446 delim = val_beg; // let's pretend we have not found the cookie
3447 txn->cookie_first_date = 0;
3448 txn->cookie_last_date = 0;
3449 }
3450
3451 /* Here, we'll look for the first running server which supports the cookie.
3452 * This allows to share a same cookie between several servers, for example
3453 * to dedicate backup servers to specific servers only.
3454 * However, to prevent clients from sticking to cookie-less backup server
3455 * when they have incidentely learned an empty cookie, we simply ignore
3456 * empty cookies and mark them as invalid.
3457 * The same behaviour is applied when persistence must be ignored.
3458 */
3459 if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
3460 srv = NULL;
3461
3462 while (srv) {
3463 if (srv->cookie && (srv->cklen == delim - val_beg) &&
3464 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
3465 if ((srv->cur_state != SRV_ST_STOPPED) ||
3466 (s->be->options & PR_O_PERSIST) ||
3467 (s->flags & SF_FORCE_PRST)) {
3468 /* we found the server and we can use it */
3469 txn->flags &= ~TX_CK_MASK;
3470 txn->flags |= (srv->cur_state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
3471 s->flags |= SF_DIRECT | SF_ASSIGNED;
3472 s->target = &srv->obj_type;
3473 break;
3474 } else {
3475 /* we found a server, but it's down,
3476 * mark it as such and go on in case
3477 * another one is available.
3478 */
3479 txn->flags &= ~TX_CK_MASK;
3480 txn->flags |= TX_CK_DOWN;
3481 }
3482 }
3483 srv = srv->next;
3484 }
3485
3486 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
3487 /* no server matched this cookie or we deliberately skipped it */
3488 txn->flags &= ~TX_CK_MASK;
3489 if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
3490 txn->flags |= TX_CK_UNUSED;
3491 else
3492 txn->flags |= TX_CK_INVALID;
3493 }
3494
3495 /* depending on the cookie mode, we may have to either :
3496 * - delete the complete cookie if we're in insert+indirect mode, so that
3497 * the server never sees it ;
3498 * - remove the server id from the cookie value, and tag the cookie as an
Joseph Herlante9d5c722018-11-25 11:00:25 -08003499 * application cookie so that it does not get accidentally removed later,
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003500 * if we're in cookie prefix mode
3501 */
3502 if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
3503 int delta; /* negative */
3504
3505 memmove(val_beg, delim + 1, hdr_end - (delim + 1));
3506 delta = val_beg - (delim + 1);
3507 val_end += delta;
3508 next += delta;
3509 hdr_end += delta;
3510 del_from = NULL;
3511 preserve_hdr = 1; /* we want to keep this cookie */
3512 }
3513 else if (del_from == NULL &&
3514 (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
3515 del_from = prev;
3516 }
3517 }
3518 else {
3519 /* This is not our cookie, so we must preserve it. But if we already
3520 * scheduled another cookie for removal, we cannot remove the
3521 * complete header, but we can remove the previous block itself.
3522 */
3523 preserve_hdr = 1;
3524
3525 if (del_from != NULL) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003526 int delta = http_del_hdr_value(hdr_beg, hdr_end, &del_from, prev);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003527 if (att_beg >= del_from)
3528 att_beg += delta;
3529 if (att_end >= del_from)
3530 att_end += delta;
3531 val_beg += delta;
3532 val_end += delta;
3533 next += delta;
3534 hdr_end += delta;
3535 prev = del_from;
3536 del_from = NULL;
3537 }
3538 }
3539
3540 /* continue with next cookie on this header line */
3541 att_beg = next;
3542 } /* for each cookie */
3543
3544
3545 /* There are no more cookies on this line.
3546 * We may still have one (or several) marked for deletion at the
3547 * end of the line. We must do this now in two ways :
3548 * - if some cookies must be preserved, we only delete from the
3549 * mark to the end of line ;
3550 * - if nothing needs to be preserved, simply delete the whole header
3551 */
3552 if (del_from) {
3553 hdr_end = (preserve_hdr ? del_from : hdr_beg);
3554 }
3555 if ((hdr_end - hdr_beg) != ctx.value.len) {
Christopher Faulet3e2638e2019-06-18 09:49:16 +02003556 if (hdr_beg != hdr_end)
3557 htx_change_blk_value_len(htx, ctx.blk, hdr_end - hdr_beg);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003558 else
3559 http_remove_header(htx, &ctx);
3560 }
3561 } /* for each "Cookie header */
3562}
3563
3564/*
3565 * Manage server-side cookies. It can impact performance by about 2% so it is
3566 * desirable to call it only when needed. This function is also used when we
3567 * just need to know if there is a cookie (eg: for check-cache).
3568 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003569static void http_manage_server_side_cookies(struct stream *s, struct channel *res)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003570{
3571 struct session *sess = s->sess;
3572 struct http_txn *txn = s->txn;
3573 struct htx *htx;
3574 struct http_hdr_ctx ctx;
3575 struct server *srv;
3576 char *hdr_beg, *hdr_end;
3577 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau6f7a02a2019-04-15 21:49:49 +02003578 int is_cookie2 = 0;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003579
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003580 htx = htxbuf(&res->buf);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003581
3582 ctx.blk = NULL;
3583 while (1) {
Olivier Houchardf0f42382019-07-22 17:43:46 +02003584 int is_first = 1;
3585
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003586 if (!http_find_header(htx, ist("Set-Cookie"), &ctx, 1)) {
3587 if (!http_find_header(htx, ist("Set-Cookie2"), &ctx, 1))
3588 break;
3589 is_cookie2 = 1;
3590 }
3591
3592 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
3593 * <prev> points to the colon.
3594 */
3595 txn->flags |= TX_SCK_PRESENT;
3596
3597 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
3598 * check-cache is enabled) and we are not interested in checking
3599 * them. Warning, the cookie capture is declared in the frontend.
3600 */
3601 if (s->be->cookie_name == NULL && sess->fe->capture_name == NULL)
3602 break;
3603
3604 /* OK so now we know we have to process this response cookie.
3605 * The format of the Set-Cookie header is slightly different
3606 * from the format of the Cookie header in that it does not
3607 * support the comma as a cookie delimiter (thus the header
3608 * cannot be folded) because the Expires attribute described in
3609 * the original Netscape's spec may contain an unquoted date
3610 * with a comma inside. We have to live with this because
3611 * many browsers don't support Max-Age and some browsers don't
3612 * support quoted strings. However the Set-Cookie2 header is
3613 * clean.
3614 *
3615 * We have to keep multiple pointers in order to support cookie
3616 * removal at the beginning, middle or end of header without
3617 * corrupting the header (in case of set-cookie2). A special
3618 * pointer, <scav> points to the beginning of the set-cookie-av
3619 * fields after the first semi-colon. The <next> pointer points
3620 * either to the end of line (set-cookie) or next unquoted comma
3621 * (set-cookie2). All of these headers are valid :
3622 *
3623 * hdr_beg hdr_end
3624 * | |
3625 * v |
3626 * NAME1 = VALUE 1 ; Secure; Path="/" |
3627 * NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT v
3628 * NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT
3629 * NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard
3630 * | | | | | | | |
3631 * | | | | | | | +-> next
3632 * | | | | | | +------------> scav
3633 * | | | | | +--------------> val_end
3634 * | | | | +--------------------> val_beg
3635 * | | | +----------------------> equal
3636 * | | +------------------------> att_end
3637 * | +----------------------------> att_beg
3638 * +------------------------------> prev
3639 * -------------------------------> hdr_beg
3640 */
3641 hdr_beg = ctx.value.ptr;
3642 hdr_end = hdr_beg + ctx.value.len;
3643 for (prev = hdr_beg; prev < hdr_end; prev = next) {
3644
3645 /* Iterate through all cookies on this line */
3646
3647 /* find att_beg */
3648 att_beg = prev;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003649 if (!is_first)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003650 att_beg++;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003651 is_first = 0;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003652
3653 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
3654 att_beg++;
3655
3656 /* find att_end : this is the first character after the last non
3657 * space before the equal. It may be equal to hdr_end.
3658 */
3659 equal = att_end = att_beg;
3660
3661 while (equal < hdr_end) {
3662 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
3663 break;
3664 if (HTTP_IS_SPHT(*equal++))
3665 continue;
3666 att_end = equal;
3667 }
3668
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003669 /* here, <equal> points to '=', a delimiter or the end. <att_end>
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003670 * is between <att_beg> and <equal>, both may be identical.
3671 */
3672
3673 /* look for end of cookie if there is an equal sign */
3674 if (equal < hdr_end && *equal == '=') {
3675 /* look for the beginning of the value */
3676 val_beg = equal + 1;
3677 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
3678 val_beg++;
3679
3680 /* find the end of the value, respecting quotes */
3681 next = http_find_cookie_value_end(val_beg, hdr_end);
3682
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003683 /* make val_end point to the first white space or delimiter after the value */
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003684 val_end = next;
3685 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
3686 val_end--;
3687 }
3688 else {
3689 /* <equal> points to next comma, semi-colon or EOL */
3690 val_beg = val_end = next = equal;
3691 }
3692
3693 if (next < hdr_end) {
3694 /* Set-Cookie2 supports multiple cookies, and <next> points to
3695 * a colon or semi-colon before the end. So skip all attr-value
3696 * pairs and look for the next comma. For Set-Cookie, since
3697 * commas are permitted in values, skip to the end.
3698 */
3699 if (is_cookie2)
3700 next = http_find_hdr_value_end(next, hdr_end);
3701 else
3702 next = hdr_end;
3703 }
3704
3705 /* Now everything is as on the diagram above */
3706
3707 /* Ignore cookies with no equal sign */
3708 if (equal == val_end)
3709 continue;
3710
3711 /* If there are spaces around the equal sign, we need to
3712 * strip them otherwise we'll get trouble for cookie captures,
3713 * or even for rewrites. Since this happens extremely rarely,
3714 * it does not hurt performance.
3715 */
3716 if (unlikely(att_end != equal || val_beg > equal + 1)) {
3717 int stripped_before = 0;
3718 int stripped_after = 0;
3719
3720 if (att_end != equal) {
3721 memmove(att_end, equal, hdr_end - equal);
3722 stripped_before = (att_end - equal);
3723 equal += stripped_before;
3724 val_beg += stripped_before;
3725 }
3726
3727 if (val_beg > equal + 1) {
3728 memmove(equal + 1, val_beg, hdr_end + stripped_before - val_beg);
3729 stripped_after = (equal + 1) - val_beg;
3730 val_beg += stripped_after;
3731 stripped_before += stripped_after;
3732 }
3733
3734 val_end += stripped_before;
3735 next += stripped_before;
3736 hdr_end += stripped_before;
3737
Christopher Faulet3e2638e2019-06-18 09:49:16 +02003738 htx_change_blk_value_len(htx, ctx.blk, hdr_end - hdr_beg);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003739 ctx.value.len = hdr_end - hdr_beg;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003740 }
3741
3742 /* First, let's see if we want to capture this cookie. We check
3743 * that we don't already have a server side cookie, because we
3744 * can only capture one. Also as an optimisation, we ignore
3745 * cookies shorter than the declared name.
3746 */
3747 if (sess->fe->capture_name != NULL &&
3748 txn->srv_cookie == NULL &&
3749 (val_end - att_beg >= sess->fe->capture_namelen) &&
3750 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
3751 int log_len = val_end - att_beg;
3752 if ((txn->srv_cookie = pool_alloc(pool_head_capture)) == NULL) {
3753 ha_alert("HTTP logging : out of memory.\n");
3754 }
3755 else {
3756 if (log_len > sess->fe->capture_len)
3757 log_len = sess->fe->capture_len;
3758 memcpy(txn->srv_cookie, att_beg, log_len);
3759 txn->srv_cookie[log_len] = 0;
3760 }
3761 }
3762
3763 srv = objt_server(s->target);
3764 /* now check if we need to process it for persistence */
3765 if (!(s->flags & SF_IGNORE_PRST) &&
3766 (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
3767 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
3768 /* assume passive cookie by default */
3769 txn->flags &= ~TX_SCK_MASK;
3770 txn->flags |= TX_SCK_FOUND;
3771
3772 /* If the cookie is in insert mode on a known server, we'll delete
3773 * this occurrence because we'll insert another one later.
3774 * We'll delete it too if the "indirect" option is set and we're in
3775 * a direct access.
3776 */
3777 if (s->be->ck_opts & PR_CK_PSV) {
3778 /* The "preserve" flag was set, we don't want to touch the
3779 * server's cookie.
3780 */
3781 }
3782 else if ((srv && (s->be->ck_opts & PR_CK_INS)) ||
3783 ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) {
3784 /* this cookie must be deleted */
3785 if (prev == hdr_beg && next == hdr_end) {
3786 /* whole header */
3787 http_remove_header(htx, &ctx);
3788 /* note: while both invalid now, <next> and <hdr_end>
3789 * are still equal, so the for() will stop as expected.
3790 */
3791 } else {
3792 /* just remove the value */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003793 int delta = http_del_hdr_value(hdr_beg, hdr_end, &prev, next);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003794 next = prev;
3795 hdr_end += delta;
3796 }
3797 txn->flags &= ~TX_SCK_MASK;
3798 txn->flags |= TX_SCK_DELETED;
3799 /* and go on with next cookie */
3800 }
3801 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) {
3802 /* replace bytes val_beg->val_end with the cookie name associated
3803 * with this server since we know it.
3804 */
3805 int sliding, delta;
3806
3807 ctx.value = ist2(val_beg, val_end - val_beg);
3808 ctx.lws_before = ctx.lws_after = 0;
3809 http_replace_header_value(htx, &ctx, ist2(srv->cookie, srv->cklen));
3810 delta = srv->cklen - (val_end - val_beg);
3811 sliding = (ctx.value.ptr - val_beg);
3812 hdr_beg += sliding;
3813 val_beg += sliding;
3814 next += sliding + delta;
3815 hdr_end += sliding + delta;
3816
3817 txn->flags &= ~TX_SCK_MASK;
3818 txn->flags |= TX_SCK_REPLACED;
3819 }
3820 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) {
3821 /* insert the cookie name associated with this server
3822 * before existing cookie, and insert a delimiter between them..
3823 */
3824 int sliding, delta;
3825 ctx.value = ist2(val_beg, 0);
3826 ctx.lws_before = ctx.lws_after = 0;
3827 http_replace_header_value(htx, &ctx, ist2(srv->cookie, srv->cklen + 1));
3828 delta = srv->cklen + 1;
3829 sliding = (ctx.value.ptr - val_beg);
3830 hdr_beg += sliding;
3831 val_beg += sliding;
3832 next += sliding + delta;
3833 hdr_end += sliding + delta;
3834
3835 val_beg[srv->cklen] = COOKIE_DELIM;
3836 txn->flags &= ~TX_SCK_MASK;
3837 txn->flags |= TX_SCK_REPLACED;
3838 }
3839 }
3840 /* that's done for this cookie, check the next one on the same
3841 * line when next != hdr_end (only if is_cookie2).
3842 */
3843 }
3844 }
3845}
3846
Christopher Faulet25a02f62018-10-24 12:00:25 +02003847/*
3848 * Parses the Cache-Control and Pragma request header fields to determine if
3849 * the request may be served from the cache and/or if it is cacheable. Updates
3850 * s->txn->flags.
3851 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003852void http_check_request_for_cacheability(struct stream *s, struct channel *req)
Christopher Faulet25a02f62018-10-24 12:00:25 +02003853{
3854 struct http_txn *txn = s->txn;
3855 struct htx *htx;
3856 int32_t pos;
3857 int pragma_found, cc_found, i;
3858
3859 if ((txn->flags & (TX_CACHEABLE|TX_CACHE_IGNORE)) == TX_CACHE_IGNORE)
3860 return; /* nothing more to do here */
3861
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003862 htx = htxbuf(&req->buf);
Christopher Faulet25a02f62018-10-24 12:00:25 +02003863 pragma_found = cc_found = 0;
Christopher Fauleta3f15502019-05-13 15:27:23 +02003864 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003865 struct htx_blk *blk = htx_get_blk(htx, pos);
3866 enum htx_blk_type type = htx_get_blk_type(blk);
3867 struct ist n, v;
3868
3869 if (type == HTX_BLK_EOH)
3870 break;
3871 if (type != HTX_BLK_HDR)
3872 continue;
3873
3874 n = htx_get_blk_name(htx, blk);
3875 v = htx_get_blk_value(htx, blk);
3876
Willy Tarreau2e754bf2018-12-07 11:38:03 +01003877 if (isteq(n, ist("pragma"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003878 if (v.len >= 8 && strncasecmp(v.ptr, "no-cache", 8) == 0) {
3879 pragma_found = 1;
3880 continue;
3881 }
3882 }
3883
3884 /* Don't use the cache and don't try to store if we found the
3885 * Authorization header */
Willy Tarreau2e754bf2018-12-07 11:38:03 +01003886 if (isteq(n, ist("authorization"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003887 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3888 txn->flags |= TX_CACHE_IGNORE;
3889 continue;
3890 }
3891
Willy Tarreau2e754bf2018-12-07 11:38:03 +01003892 if (!isteq(n, ist("cache-control")))
Christopher Faulet25a02f62018-10-24 12:00:25 +02003893 continue;
3894
3895 /* OK, right now we know we have a cache-control header */
3896 cc_found = 1;
3897 if (!v.len) /* no info */
3898 continue;
3899
3900 i = 0;
3901 while (i < v.len && *(v.ptr+i) != '=' && *(v.ptr+i) != ',' &&
3902 !isspace((unsigned char)*(v.ptr+i)))
3903 i++;
3904
3905 /* we have a complete value between v.ptr and (v.ptr+i). We don't check the
3906 * values after max-age, max-stale nor min-fresh, we simply don't
3907 * use the cache when they're specified.
3908 */
3909 if (((i == 7) && strncasecmp(v.ptr, "max-age", 7) == 0) ||
3910 ((i == 8) && strncasecmp(v.ptr, "no-cache", 8) == 0) ||
3911 ((i == 9) && strncasecmp(v.ptr, "max-stale", 9) == 0) ||
3912 ((i == 9) && strncasecmp(v.ptr, "min-fresh", 9) == 0)) {
3913 txn->flags |= TX_CACHE_IGNORE;
3914 continue;
3915 }
3916
3917 if ((i == 8) && strncasecmp(v.ptr, "no-store", 8) == 0) {
3918 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3919 continue;
3920 }
3921 }
3922
3923 /* RFC7234#5.4:
3924 * When the Cache-Control header field is also present and
3925 * understood in a request, Pragma is ignored.
3926 * When the Cache-Control header field is not present in a
3927 * request, caches MUST consider the no-cache request
3928 * pragma-directive as having the same effect as if
3929 * "Cache-Control: no-cache" were present.
3930 */
3931 if (!cc_found && pragma_found)
3932 txn->flags |= TX_CACHE_IGNORE;
3933}
3934
3935/*
3936 * Check if response is cacheable or not. Updates s->txn->flags.
3937 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003938void http_check_response_for_cacheability(struct stream *s, struct channel *res)
Christopher Faulet25a02f62018-10-24 12:00:25 +02003939{
3940 struct http_txn *txn = s->txn;
3941 struct htx *htx;
3942 int32_t pos;
3943 int i;
3944
3945 if (txn->status < 200) {
3946 /* do not try to cache interim responses! */
3947 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3948 return;
3949 }
3950
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003951 htx = htxbuf(&res->buf);
Christopher Fauleta3f15502019-05-13 15:27:23 +02003952 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003953 struct htx_blk *blk = htx_get_blk(htx, pos);
3954 enum htx_blk_type type = htx_get_blk_type(blk);
3955 struct ist n, v;
3956
3957 if (type == HTX_BLK_EOH)
3958 break;
3959 if (type != HTX_BLK_HDR)
3960 continue;
3961
3962 n = htx_get_blk_name(htx, blk);
3963 v = htx_get_blk_value(htx, blk);
3964
Willy Tarreau2e754bf2018-12-07 11:38:03 +01003965 if (isteq(n, ist("pragma"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003966 if ((v.len >= 8) && strncasecmp(v.ptr, "no-cache", 8) == 0) {
3967 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3968 return;
3969 }
3970 }
3971
Willy Tarreau2e754bf2018-12-07 11:38:03 +01003972 if (!isteq(n, ist("cache-control")))
Christopher Faulet25a02f62018-10-24 12:00:25 +02003973 continue;
3974
3975 /* OK, right now we know we have a cache-control header */
3976 if (!v.len) /* no info */
3977 continue;
3978
3979 i = 0;
3980 while (i < v.len && *(v.ptr+i) != '=' && *(v.ptr+i) != ',' &&
3981 !isspace((unsigned char)*(v.ptr+i)))
3982 i++;
3983
3984 /* we have a complete value between v.ptr and (v.ptr+i) */
3985 if (i < v.len && *(v.ptr + i) == '=') {
3986 if (((v.len - i) > 1 && (i == 7) && strncasecmp(v.ptr, "max-age=0", 9) == 0) ||
3987 ((v.len - i) > 1 && (i == 8) && strncasecmp(v.ptr, "s-maxage=0", 10) == 0)) {
3988 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3989 continue;
3990 }
3991
3992 /* we have something of the form no-cache="set-cookie" */
3993 if ((v.len >= 21) &&
3994 strncasecmp(v.ptr, "no-cache=\"set-cookie", 20) == 0
3995 && (*(v.ptr + 20) == '"' || *(v.ptr + 20 ) == ','))
3996 txn->flags &= ~TX_CACHE_COOK;
3997 continue;
3998 }
3999
4000 /* OK, so we know that either p2 points to the end of string or to a comma */
4001 if (((i == 7) && strncasecmp(v.ptr, "private", 7) == 0) ||
4002 ((i == 8) && strncasecmp(v.ptr, "no-cache", 8) == 0) ||
4003 ((i == 8) && strncasecmp(v.ptr, "no-store", 8) == 0)) {
4004 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
4005 return;
4006 }
4007
4008 if ((i == 6) && strncasecmp(v.ptr, "public", 6) == 0) {
4009 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
4010 continue;
4011 }
4012 }
4013}
4014
Christopher Faulet377c5a52018-10-24 21:21:30 +02004015/*
4016 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
4017 * for the current backend.
4018 *
4019 * It is assumed that the request is either a HEAD, GET, or POST and that the
4020 * uri_auth field is valid.
4021 *
4022 * Returns 1 if stats should be provided, otherwise 0.
4023 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004024static int http_stats_check_uri(struct stream *s, struct http_txn *txn, struct proxy *backend)
Christopher Faulet377c5a52018-10-24 21:21:30 +02004025{
4026 struct uri_auth *uri_auth = backend->uri_auth;
4027 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004028 struct htx_sl *sl;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004029 struct ist uri;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004030
4031 if (!uri_auth)
4032 return 0;
4033
4034 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
4035 return 0;
4036
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004037 htx = htxbuf(&s->req.buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02004038 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004039 uri = htx_sl_req_uri(sl);
Willy Tarreau1eb3b482019-10-31 15:50:28 +01004040 if (*uri_auth->uri_prefix == '/')
4041 uri = http_get_path(uri);
Christopher Faulet377c5a52018-10-24 21:21:30 +02004042
4043 /* check URI size */
4044 if (uri_auth->uri_len > uri.len)
4045 return 0;
4046
4047 if (memcmp(uri.ptr, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
4048 return 0;
4049
4050 return 1;
4051}
4052
4053/* This function prepares an applet to handle the stats. It can deal with the
4054 * "100-continue" expectation, check that admin rules are met for POST requests,
4055 * and program a response message if something was unexpected. It cannot fail
4056 * and always relies on the stats applet to complete the job. It does not touch
4057 * analysers nor counters, which are left to the caller. It does not touch
4058 * s->target which is supposed to already point to the stats applet. The caller
4059 * is expected to have already assigned an appctx to the stream.
4060 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004061static int http_handle_stats(struct stream *s, struct channel *req)
Christopher Faulet377c5a52018-10-24 21:21:30 +02004062{
4063 struct stats_admin_rule *stats_admin_rule;
4064 struct stream_interface *si = &s->si[1];
4065 struct session *sess = s->sess;
4066 struct http_txn *txn = s->txn;
4067 struct http_msg *msg = &txn->req;
4068 struct uri_auth *uri_auth = s->be->uri_auth;
4069 const char *h, *lookup, *end;
4070 struct appctx *appctx;
4071 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004072 struct htx_sl *sl;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004073
4074 appctx = si_appctx(si);
4075 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
4076 appctx->st1 = appctx->st2 = 0;
4077 appctx->ctx.stats.st_code = STAT_STATUS_INIT;
Willy Tarreau676c29e2019-10-09 10:50:01 +02004078 appctx->ctx.stats.flags |= uri_auth->flags;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004079 appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */
4080 if ((msg->flags & HTTP_MSGF_VER_11) && (txn->meth != HTTP_METH_HEAD))
4081 appctx->ctx.stats.flags |= STAT_CHUNKED;
4082
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004083 htx = htxbuf(&req->buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02004084 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004085 lookup = HTX_SL_REQ_UPTR(sl) + uri_auth->uri_len;
4086 end = HTX_SL_REQ_UPTR(sl) + HTX_SL_REQ_ULEN(sl);
Christopher Faulet377c5a52018-10-24 21:21:30 +02004087
4088 for (h = lookup; h <= end - 3; h++) {
4089 if (memcmp(h, ";up", 3) == 0) {
4090 appctx->ctx.stats.flags |= STAT_HIDE_DOWN;
4091 break;
4092 }
4093 }
4094
4095 if (uri_auth->refresh) {
4096 for (h = lookup; h <= end - 10; h++) {
4097 if (memcmp(h, ";norefresh", 10) == 0) {
4098 appctx->ctx.stats.flags |= STAT_NO_REFRESH;
4099 break;
4100 }
4101 }
4102 }
4103
4104 for (h = lookup; h <= end - 4; h++) {
4105 if (memcmp(h, ";csv", 4) == 0) {
Christopher Faulet6338a082019-09-09 15:50:54 +02004106 appctx->ctx.stats.flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
Christopher Faulet377c5a52018-10-24 21:21:30 +02004107 break;
4108 }
4109 }
4110
4111 for (h = lookup; h <= end - 6; h++) {
4112 if (memcmp(h, ";typed", 6) == 0) {
Christopher Faulet6338a082019-09-09 15:50:54 +02004113 appctx->ctx.stats.flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
Christopher Faulet377c5a52018-10-24 21:21:30 +02004114 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
4115 break;
4116 }
4117 }
4118
Christopher Faulet6338a082019-09-09 15:50:54 +02004119 for (h = lookup; h <= end - 5; h++) {
4120 if (memcmp(h, ";json", 5) == 0) {
4121 appctx->ctx.stats.flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
4122 appctx->ctx.stats.flags |= STAT_FMT_JSON;
4123 break;
4124 }
4125 }
4126
4127 for (h = lookup; h <= end - 12; h++) {
4128 if (memcmp(h, ";json-schema", 12) == 0) {
4129 appctx->ctx.stats.flags &= ~STAT_FMT_MASK;
4130 appctx->ctx.stats.flags |= STAT_JSON_SCHM;
4131 break;
4132 }
4133 }
4134
Christopher Faulet377c5a52018-10-24 21:21:30 +02004135 for (h = lookup; h <= end - 8; h++) {
4136 if (memcmp(h, ";st=", 4) == 0) {
4137 int i;
4138 h += 4;
4139 appctx->ctx.stats.st_code = STAT_STATUS_UNKN;
4140 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
4141 if (strncmp(stat_status_codes[i], h, 4) == 0) {
4142 appctx->ctx.stats.st_code = i;
4143 break;
4144 }
4145 }
4146 break;
4147 }
4148 }
4149
4150 appctx->ctx.stats.scope_str = 0;
4151 appctx->ctx.stats.scope_len = 0;
4152 for (h = lookup; h <= end - 8; h++) {
4153 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
4154 int itx = 0;
4155 const char *h2;
4156 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
4157 const char *err;
4158
4159 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
4160 h2 = h;
Christopher Fauleted7a0662019-01-14 11:07:34 +01004161 appctx->ctx.stats.scope_str = h2 - HTX_SL_REQ_UPTR(sl);
4162 while (h < end) {
Christopher Faulet377c5a52018-10-24 21:21:30 +02004163 if (*h == ';' || *h == '&' || *h == ' ')
4164 break;
4165 itx++;
4166 h++;
4167 }
4168
4169 if (itx > STAT_SCOPE_TXT_MAXLEN)
4170 itx = STAT_SCOPE_TXT_MAXLEN;
4171 appctx->ctx.stats.scope_len = itx;
4172
4173 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
4174 memcpy(scope_txt, h2, itx);
4175 scope_txt[itx] = '\0';
4176 err = invalid_char(scope_txt);
4177 if (err) {
4178 /* bad char in search text => clear scope */
4179 appctx->ctx.stats.scope_str = 0;
4180 appctx->ctx.stats.scope_len = 0;
4181 }
4182 break;
4183 }
4184 }
4185
4186 /* now check whether we have some admin rules for this request */
4187 list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) {
4188 int ret = 1;
4189
4190 if (stats_admin_rule->cond) {
4191 ret = acl_exec_cond(stats_admin_rule->cond, s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
4192 ret = acl_pass(ret);
4193 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
4194 ret = !ret;
4195 }
4196
4197 if (ret) {
4198 /* no rule, or the rule matches */
4199 appctx->ctx.stats.flags |= STAT_ADMIN;
4200 break;
4201 }
4202 }
4203
Christopher Faulet5d45e382019-02-27 15:15:23 +01004204 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
4205 appctx->st0 = STAT_HTTP_HEAD;
4206 else if (txn->meth == HTTP_METH_POST) {
Christopher Fauletbd9e8422019-08-15 22:26:48 +02004207 if (appctx->ctx.stats.flags & STAT_ADMIN) {
Christopher Faulet377c5a52018-10-24 21:21:30 +02004208 appctx->st0 = STAT_HTTP_POST;
Christopher Fauletbd9e8422019-08-15 22:26:48 +02004209 if (msg->msg_state < HTTP_MSG_DATA)
4210 req->analysers |= AN_REQ_HTTP_BODY;
4211 }
Christopher Faulet377c5a52018-10-24 21:21:30 +02004212 else {
Christopher Faulet5d45e382019-02-27 15:15:23 +01004213 /* POST without admin level */
Christopher Faulet377c5a52018-10-24 21:21:30 +02004214 appctx->ctx.stats.flags &= ~STAT_CHUNKED;
4215 appctx->ctx.stats.st_code = STAT_STATUS_DENY;
4216 appctx->st0 = STAT_HTTP_LAST;
4217 }
4218 }
4219 else {
Christopher Faulet5d45e382019-02-27 15:15:23 +01004220 /* Unsupported method */
4221 appctx->ctx.stats.flags &= ~STAT_CHUNKED;
4222 appctx->ctx.stats.st_code = STAT_STATUS_IVAL;
4223 appctx->st0 = STAT_HTTP_LAST;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004224 }
4225
4226 s->task->nice = -32; /* small boost for HTTP statistics */
4227 return 1;
4228}
4229
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004230void http_perform_server_redirect(struct stream *s, struct stream_interface *si)
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004231{
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004232 struct channel *req = &s->req;
4233 struct channel *res = &s->res;
4234 struct server *srv;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004235 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004236 struct htx_sl *sl;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004237 struct ist path, location;
4238 unsigned int flags;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004239
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004240 /*
4241 * Create the location
4242 */
4243 chunk_reset(&trash);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004244
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004245 /* 1: add the server's prefix */
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004246 /* special prefix "/" means don't change URL */
4247 srv = __objt_server(s->target);
4248 if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') {
4249 if (!chunk_memcat(&trash, srv->rdr_pfx, srv->rdr_len))
4250 return;
4251 }
4252
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004253 /* 2: add the request Path */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004254 htx = htxbuf(&req->buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02004255 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004256 path = http_get_path(htx_sl_req_uri(sl));
Tim Duesterhused526372020-03-05 17:56:33 +01004257 if (!isttest(path))
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004258 return;
4259
4260 if (!chunk_memcat(&trash, path.ptr, path.len))
4261 return;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004262 location = ist2(trash.area, trash.data);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004263
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004264 /*
4265 * Create the 302 respone
4266 */
4267 htx = htx_from_buf(&res->buf);
4268 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
4269 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
4270 ist("HTTP/1.1"), ist("302"), ist("Found"));
4271 if (!sl)
4272 goto fail;
4273 sl->info.res.status = 302;
4274 s->txn->status = 302;
4275
4276 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) ||
4277 !htx_add_header(htx, ist("Connection"), ist("close")) ||
4278 !htx_add_header(htx, ist("Content-length"), ist("0")) ||
4279 !htx_add_header(htx, ist("Location"), location))
4280 goto fail;
4281
4282 if (!htx_add_endof(htx, HTX_BLK_EOH) || !htx_add_endof(htx, HTX_BLK_EOM))
4283 goto fail;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004284
Christopher Fauletc20afb82020-01-24 19:16:26 +01004285 htx_to_buf(htx, &res->buf);
Christopher Fauleta72a7e42020-01-28 09:28:11 +01004286 if (!http_forward_proxy_resp(s, 1))
4287 goto fail;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004288
4289 /* return without error. */
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004290 si_shutr(si);
4291 si_shutw(si);
4292 si->err_type = SI_ET_NONE;
4293 si->state = SI_ST_CLO;
4294
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004295 if (!(s->flags & SF_ERR_MASK))
4296 s->flags |= SF_ERR_LOCAL;
4297 if (!(s->flags & SF_FINST_MASK))
4298 s->flags |= SF_FINST_C;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004299
4300 /* FIXME: we should increase a counter of redirects per server and per backend. */
4301 srv_inc_sess_ctr(srv);
4302 srv_set_sess_last(srv);
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004303 return;
4304
4305 fail:
4306 /* If an error occurred, remove the incomplete HTTP response from the
4307 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004308 channel_htx_truncate(res, htx);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004309}
4310
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05004311/* This function terminates the request because it was completely analyzed or
Christopher Fauletf2824e62018-10-01 12:12:37 +02004312 * because an error was triggered during the body forwarding.
4313 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004314static void http_end_request(struct stream *s)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004315{
4316 struct channel *chn = &s->req;
4317 struct http_txn *txn = s->txn;
4318
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004319 DBG_TRACE_ENTER(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004320
Christopher Fauletb42a8b62018-11-19 21:59:00 +01004321 if (unlikely(txn->req.msg_state == HTTP_MSG_ERROR ||
4322 txn->rsp.msg_state == HTTP_MSG_ERROR)) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004323 channel_abort(chn);
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004324 channel_htx_truncate(chn, htxbuf(&chn->buf));
Christopher Fauletf2824e62018-10-01 12:12:37 +02004325 goto end;
4326 }
4327
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004328 if (unlikely(txn->req.msg_state < HTTP_MSG_DONE)) {
4329 DBG_TRACE_DEVEL("waiting end of the request", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004330 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004331 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004332
4333 if (txn->req.msg_state == HTTP_MSG_DONE) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004334 /* No need to read anymore, the request was completely parsed.
4335 * We can shut the read side unless we want to abort_on_close,
4336 * or we have a POST request. The issue with POST requests is
4337 * that some browsers still send a CRLF after the request, and
4338 * this CRLF must be read so that it does not remain in the kernel
4339 * buffers, otherwise a close could cause an RST on some systems
4340 * (eg: Linux).
4341 */
Christopher Faulet769d0e92019-03-22 14:23:18 +01004342 if (!(s->be->options & PR_O_ABRT_CLOSE) && txn->meth != HTTP_METH_POST)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004343 channel_dont_read(chn);
4344
4345 /* if the server closes the connection, we want to immediately react
4346 * and close the socket to save packets and syscalls.
4347 */
4348 s->si[1].flags |= SI_FL_NOHALF;
4349
4350 /* In any case we've finished parsing the request so we must
4351 * disable Nagle when sending data because 1) we're not going
4352 * to shut this side, and 2) the server is waiting for us to
4353 * send pending data.
4354 */
4355 chn->flags |= CF_NEVER_WAIT;
4356
Christopher Fauletd01ce402019-01-02 17:44:13 +01004357 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
4358 /* The server has not finished to respond, so we
4359 * don't want to move in order not to upset it.
4360 */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004361 DBG_TRACE_DEVEL("waiting end of the response", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletd01ce402019-01-02 17:44:13 +01004362 return;
4363 }
4364
Christopher Fauletf2824e62018-10-01 12:12:37 +02004365 /* When we get here, it means that both the request and the
4366 * response have finished receiving. Depending on the connection
4367 * mode, we'll have to wait for the last bytes to leave in either
4368 * direction, and sometimes for a close to be effective.
4369 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02004370 if (txn->flags & TX_CON_WANT_TUN) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004371 /* Tunnel mode will not have any analyser so it needs to
4372 * poll for reads.
4373 */
4374 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004375 if (b_data(&chn->buf)) {
4376 DBG_TRACE_DEVEL("waiting to flush the request", STRM_EV_HTTP_ANA, s, txn);
Christopher Faulet9768c262018-10-22 09:34:31 +02004377 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004378 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004379 txn->req.msg_state = HTTP_MSG_TUNNEL;
4380 }
4381 else {
4382 /* we're not expecting any new data to come for this
4383 * transaction, so we can close it.
Christopher Faulet9768c262018-10-22 09:34:31 +02004384 *
4385 * However, there is an exception if the response
4386 * length is undefined. In this case, we need to wait
4387 * the close from the server. The response will be
4388 * switched in TUNNEL mode until the end.
Christopher Fauletf2824e62018-10-01 12:12:37 +02004389 */
4390 if (!(txn->rsp.flags & HTTP_MSGF_XFER_LEN) &&
4391 txn->rsp.msg_state != HTTP_MSG_CLOSED)
Christopher Faulet9768c262018-10-22 09:34:31 +02004392 goto check_channel_flags;
Christopher Fauletf2824e62018-10-01 12:12:37 +02004393
4394 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4395 channel_shutr_now(chn);
4396 channel_shutw_now(chn);
4397 }
4398 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004399 goto check_channel_flags;
4400 }
4401
4402 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
4403 http_msg_closing:
4404 /* nothing else to forward, just waiting for the output buffer
4405 * to be empty and for the shutw_now to take effect.
4406 */
4407 if (channel_is_empty(chn)) {
4408 txn->req.msg_state = HTTP_MSG_CLOSED;
4409 goto http_msg_closed;
4410 }
4411 else if (chn->flags & CF_SHUTW) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004412 txn->req.msg_state = HTTP_MSG_ERROR;
4413 goto end;
4414 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004415 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004416 return;
4417 }
4418
4419 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
4420 http_msg_closed:
Christopher Fauletf2824e62018-10-01 12:12:37 +02004421 /* if we don't know whether the server will close, we need to hard close */
4422 if (txn->rsp.flags & HTTP_MSGF_XFER_LEN)
4423 s->si[1].flags |= SI_FL_NOLINGER; /* we want to close ASAP */
Christopher Fauletf2824e62018-10-01 12:12:37 +02004424 /* see above in MSG_DONE why we only do this in these states */
Christopher Faulet769d0e92019-03-22 14:23:18 +01004425 if (!(s->be->options & PR_O_ABRT_CLOSE))
Christopher Fauletf2824e62018-10-01 12:12:37 +02004426 channel_dont_read(chn);
4427 goto end;
4428 }
4429
4430 check_channel_flags:
4431 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4432 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4433 /* if we've just closed an output, let's switch */
4434 txn->req.msg_state = HTTP_MSG_CLOSING;
4435 goto http_msg_closing;
4436 }
4437
4438 end:
4439 chn->analysers &= AN_REQ_FLT_END;
4440 if (txn->req.msg_state == HTTP_MSG_TUNNEL && HAS_REQ_DATA_FILTERS(s))
4441 chn->analysers |= AN_REQ_FLT_XFER_DATA;
4442 channel_auto_close(chn);
4443 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004444 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004445}
4446
4447
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05004448/* This function terminates the response because it was completely analyzed or
Christopher Fauletf2824e62018-10-01 12:12:37 +02004449 * because an error was triggered during the body forwarding.
4450 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004451static void http_end_response(struct stream *s)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004452{
4453 struct channel *chn = &s->res;
4454 struct http_txn *txn = s->txn;
4455
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004456 DBG_TRACE_ENTER(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004457
Christopher Fauletb42a8b62018-11-19 21:59:00 +01004458 if (unlikely(txn->req.msg_state == HTTP_MSG_ERROR ||
4459 txn->rsp.msg_state == HTTP_MSG_ERROR)) {
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004460 channel_htx_truncate(&s->req, htxbuf(&s->req.buf));
Christopher Faulet9768c262018-10-22 09:34:31 +02004461 channel_abort(&s->req);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004462 goto end;
4463 }
4464
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004465 if (unlikely(txn->rsp.msg_state < HTTP_MSG_DONE)) {
4466 DBG_TRACE_DEVEL("waiting end of the response", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004467 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004468 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004469
4470 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
4471 /* In theory, we don't need to read anymore, but we must
4472 * still monitor the server connection for a possible close
4473 * while the request is being uploaded, so we don't disable
4474 * reading.
4475 */
4476 /* channel_dont_read(chn); */
4477
4478 if (txn->req.msg_state < HTTP_MSG_DONE) {
4479 /* The client seems to still be sending data, probably
4480 * because we got an error response during an upload.
4481 * We have the choice of either breaking the connection
4482 * or letting it pass through. Let's do the later.
4483 */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004484 DBG_TRACE_DEVEL("waiting end of the request", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004485 return;
4486 }
4487
4488 /* When we get here, it means that both the request and the
4489 * response have finished receiving. Depending on the connection
4490 * mode, we'll have to wait for the last bytes to leave in either
4491 * direction, and sometimes for a close to be effective.
4492 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02004493 if (txn->flags & TX_CON_WANT_TUN) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004494 channel_auto_read(chn);
4495 chn->flags |= CF_NEVER_WAIT;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004496 if (b_data(&chn->buf)) {
4497 DBG_TRACE_DEVEL("waiting to flush the respone", STRM_EV_HTTP_ANA, s, txn);
Christopher Faulet9768c262018-10-22 09:34:31 +02004498 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004499 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004500 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
4501 }
4502 else {
4503 /* we're not expecting any new data to come for this
4504 * transaction, so we can close it.
4505 */
4506 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4507 channel_shutr_now(chn);
4508 channel_shutw_now(chn);
4509 }
4510 }
4511 goto check_channel_flags;
4512 }
4513
4514 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
4515 http_msg_closing:
4516 /* nothing else to forward, just waiting for the output buffer
4517 * to be empty and for the shutw_now to take effect.
4518 */
4519 if (channel_is_empty(chn)) {
4520 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4521 goto http_msg_closed;
4522 }
4523 else if (chn->flags & CF_SHUTW) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004524 txn->rsp.msg_state = HTTP_MSG_ERROR;
Christopher Fauletcff0f732019-12-16 16:13:44 +01004525 _HA_ATOMIC_ADD(&strm_sess(s)->fe->fe_counters.cli_aborts, 1);
Olivier Houcharda798bf52019-03-08 18:52:00 +01004526 _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01004527 if (strm_sess(s)->listener->counters)
4528 _HA_ATOMIC_ADD(&strm_sess(s)->listener->counters->cli_aborts, 1);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004529 if (objt_server(s->target))
Christopher Fauletcff0f732019-12-16 16:13:44 +01004530 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.cli_aborts, 1);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004531 goto end;
4532 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004533 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004534 return;
4535 }
4536
4537 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
4538 http_msg_closed:
4539 /* drop any pending data */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004540 channel_htx_truncate(&s->req, htxbuf(&s->req.buf));
Christopher Faulet9768c262018-10-22 09:34:31 +02004541 channel_abort(&s->req);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004542 goto end;
4543 }
4544
4545 check_channel_flags:
4546 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4547 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4548 /* if we've just closed an output, let's switch */
4549 txn->rsp.msg_state = HTTP_MSG_CLOSING;
4550 goto http_msg_closing;
4551 }
4552
4553 end:
4554 chn->analysers &= AN_RES_FLT_END;
4555 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL && HAS_RSP_DATA_FILTERS(s))
4556 chn->analysers |= AN_RES_FLT_XFER_DATA;
4557 channel_auto_close(chn);
4558 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004559 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004560}
4561
Christopher Fauletef70e252020-01-28 09:26:19 +01004562/* Forward a response generated by HAProxy (error/redirect/return). This
4563 * function forwards all pending incoming data. If <final> is set to 0, nothing
4564 * more is performed. It is used for 1xx informational messages. Otherwise, the
Christopher Faulet8d945d62020-04-20 14:58:38 +02004565 * transaction is terminated and the request is emptied. if <final> is greater
4566 * than 1, it means after-response ruleset must not be evaluated. On success 1
4567 * is returned. If an error occurred, 0 is returned.
Christopher Fauletef70e252020-01-28 09:26:19 +01004568 */
4569int http_forward_proxy_resp(struct stream *s, int final)
4570{
4571 struct channel *req = &s->req;
4572 struct channel *res = &s->res;
4573 struct htx *htx = htxbuf(&res->buf);
4574 size_t data;
4575
4576 if (final) {
4577 htx->flags |= HTX_FL_PROXY_RESP;
Christopher Faulet8d945d62020-04-20 14:58:38 +02004578 if (final == 1 && !http_eval_after_res_rules(s))
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01004579 return 0;
Christopher Fauletef70e252020-01-28 09:26:19 +01004580
4581 channel_auto_read(req);
4582 channel_abort(req);
4583 channel_auto_close(req);
4584 channel_htx_erase(req, htxbuf(&req->buf));
4585
4586 res->wex = tick_add_ifset(now_ms, res->wto);
4587 channel_auto_read(res);
4588 channel_auto_close(res);
4589 channel_shutr_now(res);
4590 }
4591
4592 data = htx->data - co_data(res);
4593 c_adv(res, data);
4594 htx->first = -1;
4595 res->total += data;
4596 return 1;
4597}
4598
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004599void http_server_error(struct stream *s, struct stream_interface *si, int err,
4600 int finst, const struct buffer *msg)
Christopher Faulet0f226952018-10-22 09:29:56 +02004601{
Christopher Faulet72c7d8d2020-01-27 15:32:25 +01004602 http_reply_and_close(s, s->txn->status, msg);
Christopher Faulet0f226952018-10-22 09:29:56 +02004603 if (!(s->flags & SF_ERR_MASK))
4604 s->flags |= err;
4605 if (!(s->flags & SF_FINST_MASK))
4606 s->flags |= finst;
4607}
4608
Christopher Faulet72c7d8d2020-01-27 15:32:25 +01004609void http_reply_and_close(struct stream *s, short status, const struct buffer *msg)
Christopher Faulet0f226952018-10-22 09:29:56 +02004610{
Christopher Faulet8d945d62020-04-20 14:58:38 +02004611 int final = 1;
4612
4613 retry:
Christopher Faulet0f226952018-10-22 09:29:56 +02004614 channel_auto_read(&s->req);
4615 channel_abort(&s->req);
4616 channel_auto_close(&s->req);
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004617 channel_htx_erase(&s->req, htxbuf(&s->req.buf));
4618 channel_htx_truncate(&s->res, htxbuf(&s->res.buf));
Christopher Faulet72c7d8d2020-01-27 15:32:25 +01004619 channel_auto_read(&s->res);
4620 channel_auto_close(&s->res);
4621 channel_shutr_now(&s->res);
Christopher Faulet0f226952018-10-22 09:29:56 +02004622
Christopher Faulet72c7d8d2020-01-27 15:32:25 +01004623 s->res.wex = tick_add_ifset(now_ms, s->res.wto);
Christopher Faulet0f226952018-10-22 09:29:56 +02004624 s->txn->flags &= ~TX_WAIT_NEXT_RQ;
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004625
4626 /* <msg> is an HTX structure. So we copy it in the response's
4627 * channel */
Christopher Faulet9f5839c2019-07-22 16:41:43 +02004628 if (msg && !b_is_null(msg)) {
Christopher Faulet0f226952018-10-22 09:29:56 +02004629 struct channel *chn = &s->res;
4630 struct htx *htx;
4631
Christopher Fauletaed82cf2018-11-30 22:22:32 +01004632 FLT_STRM_CB(s, flt_http_reply(s, s->txn->status, msg));
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004633 htx = htx_from_buf(&chn->buf);
Christopher Faulet637259e2020-01-23 11:57:31 +01004634 if (channel_htx_copy_msg(chn, htx, msg)) {
Christopher Faulet8d945d62020-04-20 14:58:38 +02004635 if (!http_forward_proxy_resp(s, final)) {
4636 /* On error, return a 500 error message, but
4637 * don't rewrite it if it is already an internal
4638 * error.
4639 */
4640 if (s->txn->status == 500)
4641 final++;
Christopher Fauleta72a7e42020-01-28 09:28:11 +01004642 s->txn->status = 500;
Christopher Faulet8d945d62020-04-20 14:58:38 +02004643 msg = http_error_message(s);
4644 goto retry;
Christopher Fauleta72a7e42020-01-28 09:28:11 +01004645 }
Christopher Faulet637259e2020-01-23 11:57:31 +01004646 }
Christopher Faulet0f226952018-10-22 09:29:56 +02004647 }
Christopher Faulet0f226952018-10-22 09:29:56 +02004648}
4649
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004650struct buffer *http_error_message(struct stream *s)
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004651{
4652 const int msgnum = http_get_status_idx(s->txn->status);
4653
Christopher Faulet53a87e12020-01-21 10:13:03 +01004654 if (s->txn->errmsg)
Christopher Faulet473e8802020-01-14 11:12:37 +01004655 return s->txn->errmsg;
4656 else if (s->be->errmsg[msgnum])
Christopher Faulet58857752020-01-15 15:19:50 +01004657 return s->be->errmsg[msgnum];
4658 else if (strm_fe(s)->errmsg[msgnum])
4659 return strm_fe(s)->errmsg[msgnum];
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004660 else
Christopher Fauletf7346382019-07-17 22:02:08 +02004661 return &http_err_chunks[msgnum];
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004662}
4663
Christopher Faulet304cc402019-07-15 15:46:28 +02004664/* Return the error message corresponding to si->err_type. It is assumed
4665 * that the server side is closed. Note that err_type is actually a
4666 * bitmask, where almost only aborts may be cumulated with other
4667 * values. We consider that aborted operations are more important
4668 * than timeouts or errors due to the fact that nobody else in the
4669 * logs might explain incomplete retries. All others should avoid
4670 * being cumulated. It should normally not be possible to have multiple
4671 * aborts at once, but just in case, the first one in sequence is reported.
4672 * Note that connection errors appearing on the second request of a keep-alive
4673 * connection are not reported since this allows the client to retry.
4674 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004675void http_return_srv_error(struct stream *s, struct stream_interface *si)
Christopher Faulet304cc402019-07-15 15:46:28 +02004676{
4677 int err_type = si->err_type;
4678
4679 /* set s->txn->status for http_error_message(s) */
4680 s->txn->status = 503;
4681
4682 if (err_type & SI_ET_QUEUE_ABRT)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004683 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_Q,
4684 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004685 else if (err_type & SI_ET_CONN_ABRT)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004686 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_C,
4687 (s->txn->flags & TX_NOT_FIRST) ? NULL :
4688 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004689 else if (err_type & SI_ET_QUEUE_TO)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004690 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_Q,
4691 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004692 else if (err_type & SI_ET_QUEUE_ERR)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004693 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_Q,
4694 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004695 else if (err_type & SI_ET_CONN_TO)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004696 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_C,
4697 (s->txn->flags & TX_NOT_FIRST) ? NULL :
4698 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004699 else if (err_type & SI_ET_CONN_ERR)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004700 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_C,
4701 (s->flags & SF_SRV_REUSED) ? NULL :
4702 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004703 else if (err_type & SI_ET_CONN_RES)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004704 http_server_error(s, si, SF_ERR_RESOURCE, SF_FINST_C,
4705 (s->txn->flags & TX_NOT_FIRST) ? NULL :
4706 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004707 else { /* SI_ET_CONN_OTHER and others */
4708 s->txn->status = 500;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004709 http_server_error(s, si, SF_ERR_INTERNAL, SF_FINST_C,
4710 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004711 }
4712}
4713
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004714
Christopher Faulet4a28a532019-03-01 11:19:40 +01004715/* Handle Expect: 100-continue for HTTP/1.1 messages if necessary. It returns 0
4716 * on success and -1 on error.
4717 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004718static int http_handle_expect_hdr(struct stream *s, struct htx *htx, struct http_msg *msg)
Christopher Faulet4a28a532019-03-01 11:19:40 +01004719{
4720 /* If we have HTTP/1.1 message with a body and Expect: 100-continue,
4721 * then we must send an HTTP/1.1 100 Continue intermediate response.
4722 */
4723 if (msg->msg_state == HTTP_MSG_BODY && (msg->flags & HTTP_MSGF_VER_11) &&
4724 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
4725 struct ist hdr = { .ptr = "Expect", .len = 6 };
4726 struct http_hdr_ctx ctx;
4727
4728 ctx.blk = NULL;
4729 /* Expect is allowed in 1.1, look for it */
4730 if (http_find_header(htx, hdr, &ctx, 0) &&
4731 unlikely(isteqi(ctx.value, ist2("100-continue", 12)))) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004732 if (http_reply_100_continue(s) == -1)
Christopher Faulet4a28a532019-03-01 11:19:40 +01004733 return -1;
4734 http_remove_header(htx, &ctx);
4735 }
4736 }
4737 return 0;
4738}
4739
Christopher Faulet23a3c792018-11-28 10:01:23 +01004740/* Send a 100-Continue response to the client. It returns 0 on success and -1
4741 * on error. The response channel is updated accordingly.
4742 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004743static int http_reply_100_continue(struct stream *s)
Christopher Faulet23a3c792018-11-28 10:01:23 +01004744{
4745 struct channel *res = &s->res;
4746 struct htx *htx = htx_from_buf(&res->buf);
4747 struct htx_sl *sl;
4748 unsigned int flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|
4749 HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
Christopher Faulet23a3c792018-11-28 10:01:23 +01004750
4751 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
4752 ist("HTTP/1.1"), ist("100"), ist("Continue"));
4753 if (!sl)
4754 goto fail;
4755 sl->info.res.status = 100;
4756
Christopher Faulet1d5ec092019-06-26 14:23:54 +02004757 if (!htx_add_endof(htx, HTX_BLK_EOH))
Christopher Faulet23a3c792018-11-28 10:01:23 +01004758 goto fail;
4759
Christopher Fauleta72a7e42020-01-28 09:28:11 +01004760 if (!http_forward_proxy_resp(s, 0))
4761 goto fail;
Christopher Faulet23a3c792018-11-28 10:01:23 +01004762 return 0;
4763
4764 fail:
4765 /* If an error occurred, remove the incomplete HTTP response from the
4766 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004767 channel_htx_truncate(res, htx);
Christopher Faulet23a3c792018-11-28 10:01:23 +01004768 return -1;
4769}
4770
Christopher Faulet12c51e22018-11-28 15:59:42 +01004771
4772/* Send a 401-Unauthorized or 407-Unauthorized response to the client, depending
4773 * ont whether we use a proxy or not. It returns 0 on success and -1 on
4774 * error. The response channel is updated accordingly.
4775 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004776static int http_reply_40x_unauthorized(struct stream *s, const char *auth_realm)
Christopher Faulet12c51e22018-11-28 15:59:42 +01004777{
4778 struct channel *res = &s->res;
4779 struct htx *htx = htx_from_buf(&res->buf);
4780 struct htx_sl *sl;
4781 struct ist code, body;
4782 int status;
4783 unsigned int flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11);
Christopher Faulet12c51e22018-11-28 15:59:42 +01004784
4785 if (!(s->txn->flags & TX_USE_PX_CONN)) {
4786 status = 401;
4787 code = ist("401");
4788 body = ist("<html><body><h1>401 Unauthorized</h1>\n"
4789 "You need a valid user and password to access this content.\n"
4790 "</body></html>\n");
4791 }
4792 else {
4793 status = 407;
4794 code = ist("407");
4795 body = ist("<html><body><h1>407 Unauthorized</h1>\n"
4796 "You need a valid user and password to access this content.\n"
4797 "</body></html>\n");
4798 }
4799
4800 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
4801 ist("HTTP/1.1"), code, ist("Unauthorized"));
4802 if (!sl)
4803 goto fail;
4804 sl->info.res.status = status;
4805 s->txn->status = status;
4806
4807 if (chunk_printf(&trash, "Basic realm=\"%s\"", auth_realm) == -1)
4808 goto fail;
4809
Willy Tarreaub5ba2b02019-06-11 16:08:25 +02004810 if (!htx_add_header(htx, ist("Content-length"), ist("112")) ||
4811 !htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) ||
Christopher Faulet12c51e22018-11-28 15:59:42 +01004812 !htx_add_header(htx, ist("Connection"), ist("close")) ||
Jérôme Magnin86cef232018-12-28 14:49:08 +01004813 !htx_add_header(htx, ist("Content-Type"), ist("text/html")))
4814 goto fail;
4815 if (status == 401 && !htx_add_header(htx, ist("WWW-Authenticate"), ist2(trash.area, trash.data)))
4816 goto fail;
4817 if (status == 407 && !htx_add_header(htx, ist("Proxy-Authenticate"), ist2(trash.area, trash.data)))
Christopher Faulet12c51e22018-11-28 15:59:42 +01004818 goto fail;
Willy Tarreau0a7ef022019-05-28 10:30:11 +02004819 if (!htx_add_endof(htx, HTX_BLK_EOH))
4820 goto fail;
4821
4822 while (body.len) {
4823 size_t sent = htx_add_data(htx, body);
4824 if (!sent)
4825 goto fail;
4826 body.ptr += sent;
4827 body.len -= sent;
4828 }
4829
4830 if (!htx_add_endof(htx, HTX_BLK_EOM))
Christopher Faulet12c51e22018-11-28 15:59:42 +01004831 goto fail;
4832
Christopher Fauleta72a7e42020-01-28 09:28:11 +01004833 if (!http_forward_proxy_resp(s, 1))
4834 goto fail;
Christopher Faulet12c51e22018-11-28 15:59:42 +01004835 return 0;
4836
4837 fail:
4838 /* If an error occurred, remove the incomplete HTTP response from the
4839 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004840 channel_htx_truncate(res, htx);
Christopher Faulet12c51e22018-11-28 15:59:42 +01004841 return -1;
4842}
4843
Christopher Faulet0f226952018-10-22 09:29:56 +02004844/*
4845 * Capture headers from message <htx> according to header list <cap_hdr>, and
4846 * fill the <cap> pointers appropriately.
4847 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004848static void http_capture_headers(struct htx *htx, char **cap, struct cap_hdr *cap_hdr)
Christopher Faulet0f226952018-10-22 09:29:56 +02004849{
4850 struct cap_hdr *h;
4851 int32_t pos;
4852
Christopher Fauleta3f15502019-05-13 15:27:23 +02004853 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet0f226952018-10-22 09:29:56 +02004854 struct htx_blk *blk = htx_get_blk(htx, pos);
4855 enum htx_blk_type type = htx_get_blk_type(blk);
4856 struct ist n, v;
4857
4858 if (type == HTX_BLK_EOH)
4859 break;
4860 if (type != HTX_BLK_HDR)
4861 continue;
4862
4863 n = htx_get_blk_name(htx, blk);
4864
4865 for (h = cap_hdr; h; h = h->next) {
4866 if (h->namelen && (h->namelen == n.len) &&
4867 (strncasecmp(n.ptr, h->name, h->namelen) == 0)) {
4868 if (cap[h->index] == NULL)
4869 cap[h->index] =
4870 pool_alloc(h->pool);
4871
4872 if (cap[h->index] == NULL) {
4873 ha_alert("HTTP capture : out of memory.\n");
4874 break;
4875 }
4876
4877 v = htx_get_blk_value(htx, blk);
4878 if (v.len > h->len)
4879 v.len = h->len;
4880
4881 memcpy(cap[h->index], v.ptr, v.len);
4882 cap[h->index][v.len]=0;
4883 }
4884 }
4885 }
4886}
4887
Christopher Faulet0b6bdc52018-10-24 11:05:36 +02004888/* Delete a value in a header between delimiters <from> and <next>. The header
4889 * itself is delimited by <start> and <end> pointers. The number of characters
4890 * displaced is returned, and the pointer to the first delimiter is updated if
4891 * required. The function tries as much as possible to respect the following
4892 * principles :
4893 * - replace <from> delimiter by the <next> one unless <from> points to <start>,
4894 * in which case <next> is simply removed
4895 * - set exactly one space character after the new first delimiter, unless there
4896 * are not enough characters in the block being moved to do so.
4897 * - remove unneeded spaces before the previous delimiter and after the new
4898 * one.
4899 *
4900 * It is the caller's responsibility to ensure that :
4901 * - <from> points to a valid delimiter or <start> ;
4902 * - <next> points to a valid delimiter or <end> ;
4903 * - there are non-space chars before <from>.
4904 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004905static int http_del_hdr_value(char *start, char *end, char **from, char *next)
Christopher Faulet0b6bdc52018-10-24 11:05:36 +02004906{
4907 char *prev = *from;
4908
4909 if (prev == start) {
4910 /* We're removing the first value. eat the semicolon, if <next>
4911 * is lower than <end> */
4912 if (next < end)
4913 next++;
4914
4915 while (next < end && HTTP_IS_SPHT(*next))
4916 next++;
4917 }
4918 else {
4919 /* Remove useless spaces before the old delimiter. */
4920 while (HTTP_IS_SPHT(*(prev-1)))
4921 prev--;
4922 *from = prev;
4923
4924 /* copy the delimiter and if possible a space if we're
4925 * not at the end of the line.
4926 */
4927 if (next < end) {
4928 *prev++ = *next++;
4929 if (prev + 1 < next)
4930 *prev++ = ' ';
4931 while (next < end && HTTP_IS_SPHT(*next))
4932 next++;
4933 }
4934 }
4935 memmove(prev, next, end - next);
4936 return (prev - next);
4937}
4938
Christopher Faulet0f226952018-10-22 09:29:56 +02004939
4940/* Formats the start line of the request (without CRLF) and puts it in <str> and
Joseph Herlantc42c0e92018-11-25 10:43:27 -08004941 * return the written length. The line can be truncated if it exceeds <len>.
Christopher Faulet0f226952018-10-22 09:29:56 +02004942 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004943static size_t http_fmt_req_line(const struct htx_sl *sl, char *str, size_t len)
Christopher Faulet0f226952018-10-22 09:29:56 +02004944{
4945 struct ist dst = ist2(str, 0);
4946
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004947 if (istcat(&dst, htx_sl_req_meth(sl), len) == -1)
Christopher Faulet0f226952018-10-22 09:29:56 +02004948 goto end;
4949 if (dst.len + 1 > len)
4950 goto end;
4951 dst.ptr[dst.len++] = ' ';
4952
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004953 if (istcat(&dst, htx_sl_req_uri(sl), len) == -1)
Christopher Faulet0f226952018-10-22 09:29:56 +02004954 goto end;
4955 if (dst.len + 1 > len)
4956 goto end;
4957 dst.ptr[dst.len++] = ' ';
4958
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004959 istcat(&dst, htx_sl_req_vsn(sl), len);
Christopher Faulet0f226952018-10-22 09:29:56 +02004960 end:
4961 return dst.len;
4962}
4963
4964/*
4965 * Print a debug line with a start line.
4966 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004967static void http_debug_stline(const char *dir, struct stream *s, const struct htx_sl *sl)
Christopher Faulet0f226952018-10-22 09:29:56 +02004968{
4969 struct session *sess = strm_sess(s);
4970 int max;
4971
4972 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
4973 dir,
4974 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->handle.fd : -1,
4975 objt_cs(s->si[1].end) ? (unsigned short)objt_cs(s->si[1].end)->conn->handle.fd : -1);
4976
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004977 max = HTX_SL_P1_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02004978 UBOUND(max, trash.size - trash.data - 3);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004979 chunk_memcat(&trash, HTX_SL_P1_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02004980 trash.area[trash.data++] = ' ';
4981
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004982 max = HTX_SL_P2_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02004983 UBOUND(max, trash.size - trash.data - 2);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004984 chunk_memcat(&trash, HTX_SL_P2_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02004985 trash.area[trash.data++] = ' ';
4986
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004987 max = HTX_SL_P3_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02004988 UBOUND(max, trash.size - trash.data - 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004989 chunk_memcat(&trash, HTX_SL_P3_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02004990 trash.area[trash.data++] = '\n';
4991
Willy Tarreau2e8ab6b2020-03-14 11:03:20 +01004992 DISGUISE(write(1, trash.area, trash.data));
Christopher Faulet0f226952018-10-22 09:29:56 +02004993}
4994
4995/*
4996 * Print a debug line with a header.
4997 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004998static 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 +02004999{
5000 struct session *sess = strm_sess(s);
5001 int max;
5002
5003 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
5004 dir,
5005 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->handle.fd : -1,
5006 objt_cs(s->si[1].end) ? (unsigned short)objt_cs(s->si[1].end)->conn->handle.fd : -1);
5007
5008 max = n.len;
5009 UBOUND(max, trash.size - trash.data - 3);
5010 chunk_memcat(&trash, n.ptr, max);
5011 trash.area[trash.data++] = ':';
5012 trash.area[trash.data++] = ' ';
5013
5014 max = v.len;
5015 UBOUND(max, trash.size - trash.data - 1);
5016 chunk_memcat(&trash, v.ptr, max);
5017 trash.area[trash.data++] = '\n';
5018
Willy Tarreau2e8ab6b2020-03-14 11:03:20 +01005019 DISGUISE(write(1, trash.area, trash.data));
Christopher Faulet0f226952018-10-22 09:29:56 +02005020}
5021
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005022/* Allocate a new HTTP transaction for stream <s> unless there is one already.
5023 * In case of allocation failure, everything allocated is freed and NULL is
5024 * returned. Otherwise the new transaction is assigned to the stream and
5025 * returned.
5026 */
5027struct http_txn *http_alloc_txn(struct stream *s)
5028{
5029 struct http_txn *txn = s->txn;
5030
5031 if (txn)
5032 return txn;
5033
5034 txn = pool_alloc(pool_head_http_txn);
5035 if (!txn)
5036 return txn;
5037
5038 s->txn = txn;
5039 return txn;
5040}
5041
5042void http_txn_reset_req(struct http_txn *txn)
5043{
Christopher Faulet1aea50e2020-01-17 16:03:53 +01005044 txn->req.flags = 0;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005045 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
5046}
5047
5048void http_txn_reset_res(struct http_txn *txn)
5049{
Christopher Faulet1aea50e2020-01-17 16:03:53 +01005050 txn->rsp.flags = 0;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005051 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
5052}
5053
5054/*
5055 * Initialize a new HTTP transaction for stream <s>. It is assumed that all
5056 * the required fields are properly allocated and that we only need to (re)init
5057 * them. This should be used before processing any new request.
5058 */
5059void http_init_txn(struct stream *s)
5060{
5061 struct http_txn *txn = s->txn;
5062 struct conn_stream *cs = objt_cs(s->si[0].end);
5063
5064 txn->flags = ((cs && cs->flags & CS_FL_NOT_FIRST)
5065 ? (TX_NOT_FIRST|TX_WAIT_NEXT_RQ)
5066 : 0);
5067 txn->status = -1;
Christopher Faulet473e8802020-01-14 11:12:37 +01005068 txn->errmsg = NULL;
Willy Tarreau8b507582020-02-25 09:35:07 +01005069 write_u32(txn->cache_hash, 0);
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005070
5071 txn->cookie_first_date = 0;
5072 txn->cookie_last_date = 0;
5073
5074 txn->srv_cookie = NULL;
5075 txn->cli_cookie = NULL;
5076 txn->uri = NULL;
5077
5078 http_txn_reset_req(txn);
5079 http_txn_reset_res(txn);
5080
5081 txn->req.chn = &s->req;
5082 txn->rsp.chn = &s->res;
5083
5084 txn->auth.method = HTTP_AUTH_UNKNOWN;
5085
5086 vars_init(&s->vars_txn, SCOPE_TXN);
5087 vars_init(&s->vars_reqres, SCOPE_REQ);
5088}
5089
5090/* to be used at the end of a transaction */
5091void http_end_txn(struct stream *s)
5092{
5093 struct http_txn *txn = s->txn;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005094
5095 /* these ones will have been dynamically allocated */
5096 pool_free(pool_head_requri, txn->uri);
5097 pool_free(pool_head_capture, txn->cli_cookie);
5098 pool_free(pool_head_capture, txn->srv_cookie);
Tim Duesterhusa17e6622020-03-05 20:19:02 +01005099 pool_free(pool_head_uniqueid, s->unique_id.ptr);
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005100
Tim Duesterhusa17e6622020-03-05 20:19:02 +01005101 s->unique_id = IST_NULL;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005102 txn->uri = NULL;
5103 txn->srv_cookie = NULL;
5104 txn->cli_cookie = NULL;
5105
Christopher Faulet59399252019-11-07 14:27:52 +01005106 if (!LIST_ISEMPTY(&s->vars_txn.head))
5107 vars_prune(&s->vars_txn, s->sess, s);
5108 if (!LIST_ISEMPTY(&s->vars_reqres.head))
5109 vars_prune(&s->vars_reqres, s->sess, s);
5110}
5111
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005112
5113DECLARE_POOL(pool_head_http_txn, "http_txn", sizeof(struct http_txn));
Christopher Faulet0f226952018-10-22 09:29:56 +02005114
Christopher Fauletf4eb75d2018-10-11 15:55:07 +02005115__attribute__((constructor))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005116static void __http_protocol_init(void)
Christopher Fauletf4eb75d2018-10-11 15:55:07 +02005117{
5118}
5119
5120
5121/*
5122 * Local variables:
5123 * c-indent-level: 8
5124 * c-basic-offset: 8
5125 * End:
5126 */